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 | #if defined(MBEDTLS_PLATFORM_C) |
| 31 | #include "mbedtls/platform.h" |
| 32 | #else |
| 33 | #include <stdlib.h> |
Jerry Yu | 6ade743 | 2022-01-25 10:39:33 +0800 | [diff] [blame] | 34 | #include <stdio.h> |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 35 | #define mbedtls_calloc calloc |
| 36 | #define mbedtls_free free |
Jerry Yu | 6ade743 | 2022-01-25 10:39:33 +0800 | [diff] [blame] | 37 | #define mbedtls_printf printf |
| 38 | #endif /* !MBEDTLS_PLATFORM_C */ |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 39 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 40 | #include "mbedtls/ssl.h" |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 41 | #include "ssl_client.h" |
Ronald Cron | 27c85e7 | 2022-03-08 11:37:55 +0100 | [diff] [blame] | 42 | #include "ssl_debug_helpers.h" |
Chris Jones | 84a773f | 2021-03-05 18:38:47 +0000 | [diff] [blame] | 43 | #include "ssl_misc.h" |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 44 | |
Janos Follath | 73c616b | 2019-12-18 15:07:04 +0000 | [diff] [blame] | 45 | #include "mbedtls/debug.h" |
| 46 | #include "mbedtls/error.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 47 | #include "mbedtls/platform_util.h" |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 48 | #include "mbedtls/version.h" |
Gabor Mezei | 765862c | 2021-10-19 12:22:25 +0200 | [diff] [blame] | 49 | #include "mbedtls/constant_time.h" |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 50 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 51 | #include <string.h> |
| 52 | |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 53 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 54 | #include "mbedtls/psa_util.h" |
| 55 | #include "psa/crypto.h" |
| 56 | #endif |
Manuel Pégourié-Gonnard | 07018f9 | 2022-09-15 11:29:35 +0200 | [diff] [blame] | 57 | #include "mbedtls/legacy_or_psa.h" |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 58 | |
Janos Follath | 23bdca0 | 2016-10-07 14:47:14 +0100 | [diff] [blame] | 59 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 60 | #include "mbedtls/oid.h" |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 61 | #endif |
| 62 | |
Ronald Cron | ad8c17b | 2022-06-10 17:18:09 +0200 | [diff] [blame] | 63 | #if defined(MBEDTLS_TEST_HOOKS) |
| 64 | static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args; |
| 65 | |
| 66 | void mbedtls_ssl_set_chk_buf_ptr_fail_args( |
| 67 | const uint8_t *cur, const uint8_t *end, size_t need ) |
| 68 | { |
| 69 | chk_buf_ptr_fail_args.cur = cur; |
| 70 | chk_buf_ptr_fail_args.end = end; |
| 71 | chk_buf_ptr_fail_args.need = need; |
| 72 | } |
| 73 | |
| 74 | void mbedtls_ssl_reset_chk_buf_ptr_fail_args( void ) |
| 75 | { |
| 76 | memset( &chk_buf_ptr_fail_args, 0, sizeof( chk_buf_ptr_fail_args ) ); |
| 77 | } |
| 78 | |
| 79 | int mbedtls_ssl_cmp_chk_buf_ptr_fail_args( mbedtls_ssl_chk_buf_ptr_args *args ) |
| 80 | { |
| 81 | return( ( chk_buf_ptr_fail_args.cur != args->cur ) || |
| 82 | ( chk_buf_ptr_fail_args.end != args->end ) || |
| 83 | ( chk_buf_ptr_fail_args.need != args->need ) ); |
| 84 | } |
| 85 | #endif /* MBEDTLS_TEST_HOOKS */ |
| 86 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 87 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 88 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 89 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 90 | /* Top-level Connection ID API */ |
| 91 | |
Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 92 | int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf, |
| 93 | size_t len, |
| 94 | int ignore_other_cid ) |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 95 | { |
| 96 | if( len > MBEDTLS_SSL_CID_IN_LEN_MAX ) |
| 97 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 98 | |
Hanno Becker | 611ac77 | 2019-05-14 11:45:26 +0100 | [diff] [blame] | 99 | if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL && |
| 100 | ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE ) |
| 101 | { |
| 102 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 103 | } |
| 104 | |
| 105 | conf->ignore_unexpected_cid = ignore_other_cid; |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 106 | conf->cid_len = len; |
| 107 | return( 0 ); |
| 108 | } |
| 109 | |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 110 | int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl, |
| 111 | int enable, |
| 112 | unsigned char const *own_cid, |
| 113 | size_t own_cid_len ) |
| 114 | { |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 115 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 116 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 117 | |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 118 | ssl->negotiate_cid = enable; |
| 119 | if( enable == MBEDTLS_SSL_CID_DISABLED ) |
| 120 | { |
| 121 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) ); |
| 122 | return( 0 ); |
| 123 | } |
| 124 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) ); |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 125 | MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len ); |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 126 | |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 127 | if( own_cid_len != ssl->conf->cid_len ) |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 128 | { |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 129 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config", |
| 130 | (unsigned) own_cid_len, |
| 131 | (unsigned) ssl->conf->cid_len ) ); |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 132 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 133 | } |
| 134 | |
| 135 | memcpy( ssl->own_cid, own_cid, own_cid_len ); |
Hanno Becker | b7ee0cf | 2019-04-30 14:07:31 +0100 | [diff] [blame] | 136 | /* Truncation is not an issue here because |
| 137 | * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */ |
| 138 | ssl->own_cid_len = (uint8_t) own_cid_len; |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 139 | |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 140 | return( 0 ); |
| 141 | } |
| 142 | |
Paul Elliott | 0113cf1 | 2022-03-11 20:26:47 +0000 | [diff] [blame] | 143 | int mbedtls_ssl_get_own_cid( mbedtls_ssl_context *ssl, |
| 144 | int *enabled, |
| 145 | unsigned char own_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX], |
| 146 | size_t *own_cid_len ) |
| 147 | { |
| 148 | *enabled = MBEDTLS_SSL_CID_DISABLED; |
| 149 | |
| 150 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 151 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 152 | |
| 153 | /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is |
| 154 | * zero as this is indistinguishable from not requesting to use |
| 155 | * the CID extension. */ |
| 156 | if( ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED ) |
| 157 | return( 0 ); |
| 158 | |
| 159 | if( own_cid_len != NULL ) |
| 160 | { |
| 161 | *own_cid_len = ssl->own_cid_len; |
| 162 | if( own_cid != NULL ) |
| 163 | memcpy( own_cid, ssl->own_cid, ssl->own_cid_len ); |
| 164 | } |
| 165 | |
| 166 | *enabled = MBEDTLS_SSL_CID_ENABLED; |
| 167 | |
| 168 | return( 0 ); |
| 169 | } |
| 170 | |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 171 | int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl, |
| 172 | int *enabled, |
| 173 | unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ], |
| 174 | size_t *peer_cid_len ) |
| 175 | { |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 176 | *enabled = MBEDTLS_SSL_CID_DISABLED; |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 177 | |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 178 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 179 | mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 180 | { |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 181 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 182 | } |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 183 | |
Hanno Becker | c5f2422 | 2019-05-03 12:54:52 +0100 | [diff] [blame] | 184 | /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions |
| 185 | * were used, but client and server requested the empty CID. |
| 186 | * This is indistinguishable from not using the CID extension |
| 187 | * in the first place. */ |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 188 | if( ssl->transform_in->in_cid_len == 0 && |
| 189 | ssl->transform_in->out_cid_len == 0 ) |
| 190 | { |
| 191 | return( 0 ); |
| 192 | } |
| 193 | |
Hanno Becker | 615ef17 | 2019-05-22 16:50:35 +0100 | [diff] [blame] | 194 | if( peer_cid_len != NULL ) |
| 195 | { |
| 196 | *peer_cid_len = ssl->transform_in->out_cid_len; |
| 197 | if( peer_cid != NULL ) |
| 198 | { |
| 199 | memcpy( peer_cid, ssl->transform_in->out_cid, |
| 200 | ssl->transform_in->out_cid_len ); |
| 201 | } |
| 202 | } |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 203 | |
| 204 | *enabled = MBEDTLS_SSL_CID_ENABLED; |
| 205 | |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 206 | return( 0 ); |
| 207 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 208 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 209 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 210 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 211 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 212 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 213 | /* |
| 214 | * Convert max_fragment_length codes to length. |
| 215 | * RFC 6066 says: |
| 216 | * enum{ |
| 217 | * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255) |
| 218 | * } MaxFragmentLength; |
| 219 | * and we add 0 -> extension unused |
| 220 | */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 221 | static unsigned int ssl_mfl_code_to_length( int mfl ) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 222 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 223 | switch( mfl ) |
| 224 | { |
| 225 | case MBEDTLS_SSL_MAX_FRAG_LEN_NONE: |
| 226 | return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ); |
| 227 | case MBEDTLS_SSL_MAX_FRAG_LEN_512: |
| 228 | return 512; |
| 229 | case MBEDTLS_SSL_MAX_FRAG_LEN_1024: |
| 230 | return 1024; |
| 231 | case MBEDTLS_SSL_MAX_FRAG_LEN_2048: |
| 232 | return 2048; |
| 233 | case MBEDTLS_SSL_MAX_FRAG_LEN_4096: |
| 234 | return 4096; |
| 235 | default: |
| 236 | return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ); |
| 237 | } |
| 238 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 239 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 240 | |
Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 241 | int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst, |
| 242 | const mbedtls_ssl_session *src ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 243 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 244 | mbedtls_ssl_session_free( dst ); |
| 245 | memcpy( dst, src, sizeof( mbedtls_ssl_session ) ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 246 | |
吴敬辉 | 0b71611 | 2021-11-29 10:46:35 +0800 | [diff] [blame] | 247 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 248 | dst->ticket = NULL; |
| 249 | #endif |
| 250 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 251 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 6d1986e | 2019-02-07 12:27:42 +0000 | [diff] [blame] | 252 | |
| 253 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 254 | if( src->peer_cert != NULL ) |
| 255 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 256 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 2292d1f | 2013-09-15 17:06:49 +0200 | [diff] [blame] | 257 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 258 | dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 259 | if( dst->peer_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 260 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
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_x509_crt_init( dst->peer_cert ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 263 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 264 | 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] | 265 | src->peer_cert->raw.len ) ) != 0 ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 266 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 267 | mbedtls_free( dst->peer_cert ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 268 | dst->peer_cert = NULL; |
| 269 | return( ret ); |
| 270 | } |
| 271 | } |
Hanno Becker | 6d1986e | 2019-02-07 12:27:42 +0000 | [diff] [blame] | 272 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 273 | if( src->peer_cert_digest != NULL ) |
| 274 | { |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 275 | dst->peer_cert_digest = |
Hanno Becker | accc599 | 2019-02-25 10:06:59 +0000 | [diff] [blame] | 276 | mbedtls_calloc( 1, src->peer_cert_digest_len ); |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 277 | if( dst->peer_cert_digest == NULL ) |
| 278 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 279 | |
| 280 | memcpy( dst->peer_cert_digest, src->peer_cert_digest, |
| 281 | src->peer_cert_digest_len ); |
| 282 | dst->peer_cert_digest_type = src->peer_cert_digest_type; |
Hanno Becker | accc599 | 2019-02-25 10:06:59 +0000 | [diff] [blame] | 283 | dst->peer_cert_digest_len = src->peer_cert_digest_len; |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 284 | } |
| 285 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 286 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 287 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 288 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 289 | #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] | 290 | if( src->ticket != NULL ) |
| 291 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 292 | dst->ticket = mbedtls_calloc( 1, src->ticket_len ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 293 | if( dst->ticket == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 294 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 295 | |
| 296 | memcpy( dst->ticket, src->ticket, src->ticket_len ); |
| 297 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 298 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 299 | |
| 300 | return( 0 ); |
| 301 | } |
| 302 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 303 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 304 | MBEDTLS_CHECK_RETURN_CRITICAL |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 305 | static int resize_buffer( unsigned char **buffer, size_t len_new, size_t *len_old ) |
| 306 | { |
| 307 | unsigned char* resized_buffer = mbedtls_calloc( 1, len_new ); |
| 308 | if( resized_buffer == NULL ) |
| 309 | return -1; |
| 310 | |
| 311 | /* We want to copy len_new bytes when downsizing the buffer, and |
| 312 | * len_old bytes when upsizing, so we choose the smaller of two sizes, |
| 313 | * to fit one buffer into another. Size checks, ensuring that no data is |
| 314 | * lost, are done outside of this function. */ |
| 315 | memcpy( resized_buffer, *buffer, |
| 316 | ( len_new < *len_old ) ? len_new : *len_old ); |
| 317 | mbedtls_platform_zeroize( *buffer, *len_old ); |
| 318 | mbedtls_free( *buffer ); |
| 319 | |
| 320 | *buffer = resized_buffer; |
| 321 | *len_old = len_new; |
| 322 | |
| 323 | return 0; |
| 324 | } |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 325 | |
| 326 | static void handle_buffer_resizing( mbedtls_ssl_context *ssl, int downsizing, |
Andrzej Kurek | 069fa96 | 2021-01-07 08:02:15 -0500 | [diff] [blame] | 327 | size_t in_buf_new_len, |
| 328 | size_t out_buf_new_len ) |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 329 | { |
| 330 | int modified = 0; |
| 331 | size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0; |
| 332 | size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0; |
| 333 | if( ssl->in_buf != NULL ) |
| 334 | { |
| 335 | written_in = ssl->in_msg - ssl->in_buf; |
| 336 | iv_offset_in = ssl->in_iv - ssl->in_buf; |
| 337 | len_offset_in = ssl->in_len - ssl->in_buf; |
| 338 | if( downsizing ? |
| 339 | ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len : |
| 340 | ssl->in_buf_len < in_buf_new_len ) |
| 341 | { |
| 342 | if( resize_buffer( &ssl->in_buf, in_buf_new_len, &ssl->in_buf_len ) != 0 ) |
| 343 | { |
| 344 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "input buffer resizing failed - out of memory" ) ); |
| 345 | } |
| 346 | else |
| 347 | { |
Paul Elliott | b744990 | 2021-03-10 18:14:58 +0000 | [diff] [blame] | 348 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET, |
| 349 | in_buf_new_len ) ); |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 350 | modified = 1; |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | if( ssl->out_buf != NULL ) |
| 356 | { |
| 357 | written_out = ssl->out_msg - ssl->out_buf; |
| 358 | iv_offset_out = ssl->out_iv - ssl->out_buf; |
| 359 | len_offset_out = ssl->out_len - ssl->out_buf; |
| 360 | if( downsizing ? |
| 361 | ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len : |
| 362 | ssl->out_buf_len < out_buf_new_len ) |
| 363 | { |
| 364 | if( resize_buffer( &ssl->out_buf, out_buf_new_len, &ssl->out_buf_len ) != 0 ) |
| 365 | { |
| 366 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "output buffer resizing failed - out of memory" ) ); |
| 367 | } |
| 368 | else |
| 369 | { |
Paul Elliott | b744990 | 2021-03-10 18:14:58 +0000 | [diff] [blame] | 370 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET, |
| 371 | out_buf_new_len ) ); |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 372 | modified = 1; |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | if( modified ) |
| 377 | { |
| 378 | /* Update pointers here to avoid doing it twice. */ |
| 379 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
| 380 | /* Fields below might not be properly updated with record |
| 381 | * splitting or with CID, so they are manually updated here. */ |
| 382 | ssl->out_msg = ssl->out_buf + written_out; |
| 383 | ssl->out_len = ssl->out_buf + len_offset_out; |
| 384 | ssl->out_iv = ssl->out_buf + iv_offset_out; |
| 385 | |
| 386 | ssl->in_msg = ssl->in_buf + written_in; |
| 387 | ssl->in_len = ssl->in_buf + len_offset_in; |
| 388 | ssl->in_iv = ssl->in_buf + iv_offset_in; |
| 389 | } |
| 390 | } |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 391 | #endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */ |
| 392 | |
Jerry Yu | db8c48a | 2022-01-27 14:54:54 +0800 | [diff] [blame] | 393 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 394 | |
| 395 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 396 | typedef int (*tls_prf_fn)( const unsigned char *secret, size_t slen, |
| 397 | const char *label, |
| 398 | const unsigned char *random, size_t rlen, |
| 399 | unsigned char *dstbuf, size_t dlen ); |
| 400 | |
| 401 | static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id ); |
| 402 | |
| 403 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
| 404 | |
| 405 | /* Type for the TLS PRF */ |
| 406 | typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *, |
| 407 | const unsigned char *, size_t, |
| 408 | unsigned char *, size_t); |
| 409 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 410 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 411 | static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform, |
| 412 | int ciphersuite, |
| 413 | const unsigned char master[48], |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 414 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 415 | int encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 416 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 417 | ssl_tls_prf_t tls_prf, |
| 418 | const unsigned char randbytes[64], |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 419 | mbedtls_ssl_protocol_version tls_version, |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 420 | unsigned endpoint, |
| 421 | const mbedtls_ssl_context *ssl ); |
| 422 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 423 | #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] | 424 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 425 | static int tls_prf_sha256( const unsigned char *secret, size_t slen, |
| 426 | const char *label, |
| 427 | const unsigned char *random, size_t rlen, |
| 428 | unsigned char *dstbuf, size_t dlen ); |
| 429 | static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char*, size_t * ); |
| 430 | static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int ); |
| 431 | |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 432 | #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] | 433 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 434 | #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] | 435 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 436 | static int tls_prf_sha384( const unsigned char *secret, size_t slen, |
| 437 | const char *label, |
| 438 | const unsigned char *random, size_t rlen, |
| 439 | unsigned char *dstbuf, size_t dlen ); |
| 440 | |
| 441 | static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char*, size_t * ); |
| 442 | 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] | 443 | #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] | 444 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 445 | static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session, |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 446 | unsigned char *buf, |
| 447 | size_t buf_len ); |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 448 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 449 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 450 | static int ssl_tls12_session_load( mbedtls_ssl_session *session, |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 451 | const unsigned char *buf, |
| 452 | size_t len ); |
| 453 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 454 | |
Jerry Yu | 53d23e2 | 2022-02-09 16:25:09 +0800 | [diff] [blame] | 455 | static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t ); |
| 456 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 457 | #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] | 458 | 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] | 459 | #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] | 460 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 461 | #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] | 462 | 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] | 463 | #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] | 464 | |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 465 | int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf, |
| 466 | const unsigned char *secret, size_t slen, |
| 467 | const char *label, |
| 468 | const unsigned char *random, size_t rlen, |
| 469 | unsigned char *dstbuf, size_t dlen ) |
| 470 | { |
| 471 | mbedtls_ssl_tls_prf_cb *tls_prf = NULL; |
| 472 | |
| 473 | switch( prf ) |
| 474 | { |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 475 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 476 | #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] | 477 | case MBEDTLS_SSL_TLS_PRF_SHA384: |
| 478 | tls_prf = tls_prf_sha384; |
| 479 | break; |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 480 | #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] | 481 | #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] | 482 | case MBEDTLS_SSL_TLS_PRF_SHA256: |
| 483 | tls_prf = tls_prf_sha256; |
| 484 | break; |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 485 | #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] | 486 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 487 | default: |
| 488 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 489 | } |
| 490 | |
| 491 | return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) ); |
| 492 | } |
| 493 | |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 494 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 495 | static void ssl_clear_peer_cert( mbedtls_ssl_session *session ) |
| 496 | { |
| 497 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 498 | if( session->peer_cert != NULL ) |
| 499 | { |
| 500 | mbedtls_x509_crt_free( session->peer_cert ); |
| 501 | mbedtls_free( session->peer_cert ); |
| 502 | session->peer_cert = NULL; |
| 503 | } |
| 504 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 505 | if( session->peer_cert_digest != NULL ) |
| 506 | { |
| 507 | /* Zeroization is not necessary. */ |
| 508 | mbedtls_free( session->peer_cert_digest ); |
| 509 | session->peer_cert_digest = NULL; |
| 510 | session->peer_cert_digest_type = MBEDTLS_MD_NONE; |
| 511 | session->peer_cert_digest_len = 0; |
| 512 | } |
| 513 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 514 | } |
| 515 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 516 | |
| 517 | void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl, |
| 518 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info ) |
| 519 | { |
| 520 | ((void) ciphersuite_info); |
| 521 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 522 | #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] | 523 | if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
| 524 | ssl->handshake->update_checksum = ssl_update_checksum_sha384; |
| 525 | else |
| 526 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 527 | #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] | 528 | if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 ) |
| 529 | ssl->handshake->update_checksum = ssl_update_checksum_sha256; |
| 530 | else |
| 531 | #endif |
| 532 | { |
| 533 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 534 | return; |
| 535 | } |
| 536 | } |
| 537 | |
XiaokangQian | adab9a6 | 2022-07-18 07:41:26 +0000 | [diff] [blame] | 538 | void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl, |
| 539 | unsigned hs_type, |
| 540 | size_t total_hs_len ) |
Ronald Cron | 8f6d39a | 2022-03-10 18:56:50 +0100 | [diff] [blame] | 541 | { |
| 542 | unsigned char hs_hdr[4]; |
| 543 | |
| 544 | /* Build HS header for checksum update. */ |
| 545 | hs_hdr[0] = MBEDTLS_BYTE_0( hs_type ); |
| 546 | hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len ); |
| 547 | hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len ); |
| 548 | hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len ); |
| 549 | |
| 550 | ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) ); |
| 551 | } |
| 552 | |
| 553 | void mbedtls_ssl_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl, |
| 554 | unsigned hs_type, |
| 555 | unsigned char const *msg, |
| 556 | size_t msg_len ) |
| 557 | { |
| 558 | mbedtls_ssl_add_hs_hdr_to_checksum( ssl, hs_type, msg_len ); |
| 559 | ssl->handshake->update_checksum( ssl, msg, msg_len ); |
| 560 | } |
| 561 | |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 562 | void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl ) |
| 563 | { |
| 564 | ((void) ssl); |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 565 | #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] | 566 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 567 | psa_hash_abort( &ssl->handshake->fin_sha256_psa ); |
| 568 | psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 ); |
| 569 | #else |
| 570 | mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 ); |
| 571 | #endif |
| 572 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 573 | #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] | 574 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 575 | psa_hash_abort( &ssl->handshake->fin_sha384_psa ); |
| 576 | psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 ); |
| 577 | #else |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 578 | mbedtls_sha512_starts( &ssl->handshake->fin_sha384, 1 ); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 579 | #endif |
| 580 | #endif |
| 581 | } |
| 582 | |
| 583 | static void ssl_update_checksum_start( mbedtls_ssl_context *ssl, |
| 584 | const unsigned char *buf, size_t len ) |
| 585 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 586 | #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] | 587 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 588 | psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len ); |
| 589 | #else |
| 590 | mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len ); |
| 591 | #endif |
| 592 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 593 | #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] | 594 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 595 | psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len ); |
| 596 | #else |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 597 | mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len ); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 598 | #endif |
| 599 | #endif |
| 600 | } |
| 601 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 602 | #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] | 603 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl, |
| 604 | const unsigned char *buf, size_t len ) |
| 605 | { |
| 606 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 607 | psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len ); |
| 608 | #else |
| 609 | mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len ); |
| 610 | #endif |
| 611 | } |
| 612 | #endif |
| 613 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 614 | #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] | 615 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl, |
| 616 | const unsigned char *buf, size_t len ) |
| 617 | { |
| 618 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 619 | psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len ); |
| 620 | #else |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 621 | mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len ); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 622 | #endif |
| 623 | } |
| 624 | #endif |
| 625 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 626 | static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 627 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 628 | memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 629 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 630 | #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] | 631 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 632 | handshake->fin_sha256_psa = psa_hash_operation_init(); |
| 633 | psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 ); |
| 634 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 635 | mbedtls_sha256_init( &handshake->fin_sha256 ); |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 636 | mbedtls_sha256_starts( &handshake->fin_sha256, 0 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 637 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 638 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 639 | #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] | 640 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 641 | handshake->fin_sha384_psa = psa_hash_operation_init(); |
| 642 | psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 643 | #else |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 644 | mbedtls_sha512_init( &handshake->fin_sha384 ); |
| 645 | mbedtls_sha512_starts( &handshake->fin_sha384, 1 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 646 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 647 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 648 | |
| 649 | handshake->update_checksum = ssl_update_checksum_start; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 650 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 651 | #if defined(MBEDTLS_DHM_C) |
| 652 | mbedtls_dhm_init( &handshake->dhm_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 653 | #endif |
Neil Armstrong | f3f4641 | 2022-04-12 14:43:39 +0200 | [diff] [blame] | 654 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 655 | mbedtls_ecdh_init( &handshake->ecdh_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 656 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 657 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 658 | mbedtls_ecjpake_init( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 659 | #if defined(MBEDTLS_SSL_CLI_C) |
| 660 | handshake->ecjpake_cache = NULL; |
| 661 | handshake->ecjpake_cache_len = 0; |
| 662 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 663 | #endif |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 664 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 665 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 666 | mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx ); |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 667 | #endif |
| 668 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 669 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 670 | handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET; |
| 671 | #endif |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 672 | |
| 673 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 674 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 675 | mbedtls_pk_init( &handshake->peer_pubkey ); |
| 676 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 677 | } |
| 678 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 679 | void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 680 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 681 | memset( transform, 0, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 682 | |
Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 683 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 684 | transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT; |
| 685 | transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT; |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 686 | #else |
| 687 | mbedtls_cipher_init( &transform->cipher_ctx_enc ); |
| 688 | mbedtls_cipher_init( &transform->cipher_ctx_dec ); |
Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 689 | #endif |
| 690 | |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 691 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Neil Armstrong | 39b8e7d | 2022-02-23 09:24:45 +0100 | [diff] [blame] | 692 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 693 | transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT; |
| 694 | transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT; |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 695 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 696 | mbedtls_md_init( &transform->md_ctx_enc ); |
| 697 | mbedtls_md_init( &transform->md_ctx_dec ); |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 698 | #endif |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 699 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 700 | } |
| 701 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 702 | void mbedtls_ssl_session_init( mbedtls_ssl_session *session ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 703 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 704 | memset( session, 0, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 705 | } |
| 706 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 707 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 708 | static int ssl_handshake_init( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 709 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 710 | /* Clear old handshake information if present */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 711 | if( ssl->transform_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 712 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 713 | if( ssl->session_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 714 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 715 | if( ssl->handshake ) |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 716 | mbedtls_ssl_handshake_free( ssl ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 717 | |
| 718 | /* |
| 719 | * Either the pointers are now NULL or cleared properly and can be freed. |
| 720 | * Now allocate missing structures. |
| 721 | */ |
| 722 | if( ssl->transform_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 723 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 724 | ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 725 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 726 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 727 | if( ssl->session_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 728 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 729 | ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 730 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 731 | |
Paul Bakker | 82788fb | 2014-10-20 13:59:19 +0200 | [diff] [blame] | 732 | if( ssl->handshake == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 733 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 734 | ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 735 | } |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 736 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 737 | /* If the buffers are too small - reallocate */ |
Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 738 | |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 739 | handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN, |
| 740 | MBEDTLS_SSL_OUT_BUFFER_LEN ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 741 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 742 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 743 | /* All pointers should exist and can be directly freed without issue */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 744 | if( ssl->handshake == NULL || |
| 745 | ssl->transform_negotiate == NULL || |
| 746 | ssl->session_negotiate == NULL ) |
| 747 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 748 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 749 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 750 | mbedtls_free( ssl->handshake ); |
| 751 | mbedtls_free( ssl->transform_negotiate ); |
| 752 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 753 | |
| 754 | ssl->handshake = NULL; |
| 755 | ssl->transform_negotiate = NULL; |
| 756 | ssl->session_negotiate = NULL; |
| 757 | |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 758 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 759 | } |
| 760 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 761 | /* Initialize structures */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 762 | mbedtls_ssl_session_init( ssl->session_negotiate ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 763 | mbedtls_ssl_transform_init( ssl->transform_negotiate ); |
Paul Bakker | 968afaa | 2014-07-09 11:09:24 +0200 | [diff] [blame] | 764 | ssl_handshake_params_init( ssl->handshake ); |
| 765 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 766 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 767 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 768 | { |
| 769 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 770 | |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 771 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 772 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
| 773 | else |
| 774 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 775 | |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 776 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 777 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 778 | #endif |
| 779 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 780 | /* |
| 781 | * curve_list is translated to IANA TLS group identifiers here because |
| 782 | * mbedtls_ssl_conf_curves returns void and so can't return |
| 783 | * any error codes. |
| 784 | */ |
| 785 | #if defined(MBEDTLS_ECP_C) |
| 786 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 787 | /* Heap allocate and translate curve_list from internal to IANA group ids */ |
| 788 | if ( ssl->conf->curve_list != NULL ) |
| 789 | { |
| 790 | size_t length; |
| 791 | const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list; |
| 792 | |
| 793 | for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) && |
| 794 | ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {} |
| 795 | |
| 796 | /* Leave room for zero termination */ |
| 797 | uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) ); |
| 798 | if ( group_list == NULL ) |
| 799 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 800 | |
| 801 | for( size_t i = 0; i < length; i++ ) |
| 802 | { |
| 803 | const mbedtls_ecp_curve_info *info = |
| 804 | mbedtls_ecp_curve_info_from_grp_id( curve_list[i] ); |
| 805 | if ( info == NULL ) |
| 806 | { |
| 807 | mbedtls_free( group_list ); |
| 808 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 809 | } |
| 810 | group_list[i] = info->tls_id; |
| 811 | } |
| 812 | |
| 813 | group_list[length] = 0; |
| 814 | |
| 815 | ssl->handshake->group_list = group_list; |
| 816 | ssl->handshake->group_list_heap_allocated = 1; |
| 817 | } |
| 818 | else |
| 819 | { |
| 820 | ssl->handshake->group_list = ssl->conf->group_list; |
| 821 | ssl->handshake->group_list_heap_allocated = 0; |
| 822 | } |
| 823 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 824 | #endif /* MBEDTLS_ECP_C */ |
| 825 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 826 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 827 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 828 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 829 | /* Heap allocate and translate sig_hashes from internal hash identifiers to |
| 830 | signature algorithms IANA identifiers. */ |
| 831 | if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) && |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 832 | ssl->conf->sig_hashes != NULL ) |
| 833 | { |
| 834 | const int *md; |
| 835 | const int *sig_hashes = ssl->conf->sig_hashes; |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 836 | size_t sig_algs_len = 0; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 837 | uint16_t *p; |
| 838 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 839 | #if defined(static_assert) |
| 840 | static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN |
| 841 | <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ), |
| 842 | "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" ); |
Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 843 | #endif |
| 844 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 845 | for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ ) |
| 846 | { |
| 847 | if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE ) |
| 848 | continue; |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 849 | #if defined(MBEDTLS_ECDSA_C) |
| 850 | sig_algs_len += sizeof( uint16_t ); |
| 851 | #endif |
Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 852 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 853 | #if defined(MBEDTLS_RSA_C) |
| 854 | sig_algs_len += sizeof( uint16_t ); |
| 855 | #endif |
| 856 | if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN ) |
| 857 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 858 | } |
| 859 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 860 | if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN ) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 861 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 862 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 863 | ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len + |
| 864 | sizeof( uint16_t )); |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 865 | if( ssl->handshake->sig_algs == NULL ) |
| 866 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 867 | |
| 868 | p = (uint16_t *)ssl->handshake->sig_algs; |
| 869 | for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ ) |
| 870 | { |
| 871 | unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md ); |
| 872 | if( hash == MBEDTLS_SSL_HASH_NONE ) |
| 873 | continue; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 874 | #if defined(MBEDTLS_ECDSA_C) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 875 | *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA); |
| 876 | p++; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 877 | #endif |
| 878 | #if defined(MBEDTLS_RSA_C) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 879 | *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA); |
| 880 | p++; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 881 | #endif |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 882 | } |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 883 | *p = MBEDTLS_TLS_SIG_NONE; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 884 | ssl->handshake->sig_algs_heap_allocated = 1; |
| 885 | } |
| 886 | else |
Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 887 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 888 | { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 889 | ssl->handshake->sig_algs_heap_allocated = 0; |
| 890 | } |
Jerry Yu | cc53910 | 2022-06-27 16:27:35 +0800 | [diff] [blame] | 891 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 892 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 893 | return( 0 ); |
| 894 | } |
| 895 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 896 | #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] | 897 | /* Dummy cookie callbacks for defaults */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 898 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 899 | static int ssl_cookie_write_dummy( void *ctx, |
| 900 | unsigned char **p, unsigned char *end, |
| 901 | const unsigned char *cli_id, size_t cli_id_len ) |
| 902 | { |
| 903 | ((void) ctx); |
| 904 | ((void) p); |
| 905 | ((void) end); |
| 906 | ((void) cli_id); |
| 907 | ((void) cli_id_len); |
| 908 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 909 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 910 | } |
| 911 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 912 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 913 | static int ssl_cookie_check_dummy( void *ctx, |
| 914 | const unsigned char *cookie, size_t cookie_len, |
| 915 | const unsigned char *cli_id, size_t cli_id_len ) |
| 916 | { |
| 917 | ((void) ctx); |
| 918 | ((void) cookie); |
| 919 | ((void) cookie_len); |
| 920 | ((void) cli_id); |
| 921 | ((void) cli_id_len); |
| 922 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 923 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 924 | } |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 925 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 926 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 927 | /* |
| 928 | * Initialize an SSL context |
| 929 | */ |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 930 | void mbedtls_ssl_init( mbedtls_ssl_context *ssl ) |
| 931 | { |
| 932 | memset( ssl, 0, sizeof( mbedtls_ssl_context ) ); |
| 933 | } |
| 934 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 935 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 936 | static int ssl_conf_version_check( const mbedtls_ssl_context *ssl ) |
| 937 | { |
Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 938 | const mbedtls_ssl_config *conf = ssl->conf; |
| 939 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 940 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 941 | if( mbedtls_ssl_conf_is_tls13_only( conf ) ) |
| 942 | { |
XiaokangQian | ed582dd | 2022-04-13 08:21:05 +0000 | [diff] [blame] | 943 | if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 944 | { |
| 945 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) ); |
| 946 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 947 | } |
| 948 | |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 949 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) ); |
| 950 | return( 0 ); |
| 951 | } |
| 952 | #endif |
| 953 | |
| 954 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 955 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 956 | { |
| 957 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) ); |
| 958 | return( 0 ); |
| 959 | } |
| 960 | #endif |
| 961 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 962 | #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] | 963 | if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) ) |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 964 | { |
XiaokangQian | ed582dd | 2022-04-13 08:21:05 +0000 | [diff] [blame] | 965 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 966 | { |
| 967 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2" ) ); |
| 968 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 969 | } |
| 970 | |
XiaokangQian | 8f9dfe4 | 2022-04-15 02:52:39 +0000 | [diff] [blame] | 971 | if( conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 972 | { |
| 973 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) ); |
| 974 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 975 | } |
| 976 | |
| 977 | |
Ronald Cron | e1d3f06 | 2022-02-10 14:50:54 +0100 | [diff] [blame] | 978 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) ); |
| 979 | return( 0 ); |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 980 | } |
| 981 | #endif |
| 982 | |
| 983 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) ); |
| 984 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 985 | } |
| 986 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 987 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 988 | static int ssl_conf_check(const mbedtls_ssl_context *ssl) |
| 989 | { |
| 990 | int ret; |
| 991 | ret = ssl_conf_version_check( ssl ); |
| 992 | if( ret != 0 ) |
| 993 | return( ret ); |
| 994 | |
| 995 | /* Space for further checks */ |
| 996 | |
| 997 | return( 0 ); |
| 998 | } |
| 999 | |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 1000 | /* |
| 1001 | * Setup an SSL context |
| 1002 | */ |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 1003 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 1004 | int mbedtls_ssl_setup( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 1897af9 | 2015-05-10 23:27:38 +0200 | [diff] [blame] | 1005 | const mbedtls_ssl_config *conf ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1006 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1007 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1008 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 1009 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1010 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 1011 | ssl->conf = conf; |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 1012 | |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1013 | if( ( ret = ssl_conf_check( ssl ) ) != 0 ) |
| 1014 | return( ret ); |
| 1015 | |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 1016 | /* |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 1017 | * Prepare base structures |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 1018 | */ |
k-stachowiak | c9a5f02 | 2018-07-24 13:53:31 +0200 | [diff] [blame] | 1019 | |
| 1020 | /* Set to NULL in case of an error condition */ |
| 1021 | ssl->out_buf = NULL; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1022 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1023 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1024 | ssl->in_buf_len = in_buf_len; |
| 1025 | #endif |
| 1026 | ssl->in_buf = mbedtls_calloc( 1, in_buf_len ); |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1027 | if( ssl->in_buf == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1028 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 1029 | 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] | 1030 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1031 | goto error; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1032 | } |
| 1033 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1034 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1035 | ssl->out_buf_len = out_buf_len; |
| 1036 | #endif |
| 1037 | ssl->out_buf = mbedtls_calloc( 1, out_buf_len ); |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1038 | if( ssl->out_buf == NULL ) |
| 1039 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 1040 | 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] | 1041 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1042 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 1045 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 1046 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1047 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Ron Eldor | 3adb992 | 2017-12-21 10:15:08 +0200 | [diff] [blame] | 1048 | memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) ); |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1049 | #endif |
| 1050 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1051 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1052 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1053 | |
| 1054 | return( 0 ); |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1055 | |
| 1056 | error: |
| 1057 | mbedtls_free( ssl->in_buf ); |
| 1058 | mbedtls_free( ssl->out_buf ); |
| 1059 | |
| 1060 | ssl->conf = NULL; |
| 1061 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1062 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1063 | ssl->in_buf_len = 0; |
| 1064 | ssl->out_buf_len = 0; |
| 1065 | #endif |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1066 | ssl->in_buf = NULL; |
| 1067 | ssl->out_buf = NULL; |
| 1068 | |
| 1069 | ssl->in_hdr = NULL; |
| 1070 | ssl->in_ctr = NULL; |
| 1071 | ssl->in_len = NULL; |
| 1072 | ssl->in_iv = NULL; |
| 1073 | ssl->in_msg = NULL; |
| 1074 | |
| 1075 | ssl->out_hdr = NULL; |
| 1076 | ssl->out_ctr = NULL; |
| 1077 | ssl->out_len = NULL; |
| 1078 | ssl->out_iv = NULL; |
| 1079 | ssl->out_msg = NULL; |
| 1080 | |
k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 1081 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | /* |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1085 | * Reset an initialized and used SSL context for re-use while retaining |
| 1086 | * all application-set variables, function pointers and data. |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1087 | * |
| 1088 | * If partial is non-zero, keep data in the input buffer and client ID. |
| 1089 | * (Use when a DTLS client reconnects from the same port.) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1090 | */ |
XiaokangQian | 78b1fa7 | 2022-01-19 06:56:30 +0000 | [diff] [blame] | 1091 | void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl, |
| 1092 | int partial ) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1093 | { |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1094 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1095 | size_t in_buf_len = ssl->in_buf_len; |
| 1096 | size_t out_buf_len = ssl->out_buf_len; |
| 1097 | #else |
| 1098 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 1099 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 1100 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1101 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1102 | #if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C) |
| 1103 | partial = 0; |
Hanno Becker | 7e77213 | 2018-08-10 12:38:21 +0100 | [diff] [blame] | 1104 | #endif |
| 1105 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1106 | /* Cancel any possibly running timer */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 1107 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1108 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1109 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
| 1110 | |
| 1111 | /* Reset incoming message parsing */ |
| 1112 | ssl->in_offt = NULL; |
| 1113 | ssl->nb_zero = 0; |
| 1114 | ssl->in_msgtype = 0; |
| 1115 | ssl->in_msglen = 0; |
| 1116 | ssl->in_hslen = 0; |
| 1117 | ssl->keep_current_message = 0; |
| 1118 | ssl->transform_in = NULL; |
| 1119 | |
| 1120 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 1121 | ssl->next_record_offset = 0; |
| 1122 | ssl->in_epoch = 0; |
| 1123 | #endif |
| 1124 | |
| 1125 | /* Keep current datagram if partial == 1 */ |
| 1126 | if( partial == 0 ) |
| 1127 | { |
| 1128 | ssl->in_left = 0; |
| 1129 | memset( ssl->in_buf, 0, in_buf_len ); |
| 1130 | } |
| 1131 | |
Ronald Cron | ad8c17b | 2022-06-10 17:18:09 +0200 | [diff] [blame] | 1132 | ssl->send_alert = 0; |
| 1133 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1134 | /* Reset outgoing message writing */ |
| 1135 | ssl->out_msgtype = 0; |
| 1136 | ssl->out_msglen = 0; |
| 1137 | ssl->out_left = 0; |
| 1138 | memset( ssl->out_buf, 0, out_buf_len ); |
| 1139 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); |
| 1140 | ssl->transform_out = NULL; |
| 1141 | |
| 1142 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 1143 | mbedtls_ssl_dtls_replay_reset( ssl ); |
| 1144 | #endif |
| 1145 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1146 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1147 | if( ssl->transform ) |
| 1148 | { |
| 1149 | mbedtls_ssl_transform_free( ssl->transform ); |
| 1150 | mbedtls_free( ssl->transform ); |
| 1151 | ssl->transform = NULL; |
| 1152 | } |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1153 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 1154 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1155 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 1156 | mbedtls_ssl_transform_free( ssl->transform_application ); |
| 1157 | mbedtls_free( ssl->transform_application ); |
| 1158 | ssl->transform_application = NULL; |
| 1159 | |
| 1160 | if( ssl->handshake != NULL ) |
| 1161 | { |
| 1162 | mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata ); |
| 1163 | mbedtls_free( ssl->handshake->transform_earlydata ); |
| 1164 | ssl->handshake->transform_earlydata = NULL; |
| 1165 | |
| 1166 | mbedtls_ssl_transform_free( ssl->handshake->transform_handshake ); |
| 1167 | mbedtls_free( ssl->handshake->transform_handshake ); |
| 1168 | ssl->handshake->transform_handshake = NULL; |
| 1169 | } |
| 1170 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1171 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1172 | } |
| 1173 | |
| 1174 | int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial ) |
| 1175 | { |
| 1176 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1177 | |
| 1178 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 1179 | |
XiaokangQian | 78b1fa7 | 2022-01-19 06:56:30 +0000 | [diff] [blame] | 1180 | mbedtls_ssl_session_reset_msg_layer( ssl, partial ); |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1181 | |
| 1182 | /* Reset renegotiation state */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1183 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1184 | ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1185 | ssl->renego_records_seen = 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1186 | |
| 1187 | ssl->verify_data_len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1188 | memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN ); |
| 1189 | 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] | 1190 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1191 | ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1192 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1193 | ssl->session_in = NULL; |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 1194 | ssl->session_out = NULL; |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 1195 | if( ssl->session ) |
| 1196 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1197 | mbedtls_ssl_session_free( ssl->session ); |
| 1198 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 1199 | ssl->session = NULL; |
| 1200 | } |
| 1201 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1202 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 1203 | ssl->alpn_chosen = NULL; |
| 1204 | #endif |
| 1205 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 1206 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 1207 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1208 | if( partial == 0 ) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 1209 | #endif |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1210 | { |
| 1211 | mbedtls_free( ssl->cli_id ); |
| 1212 | ssl->cli_id = NULL; |
| 1213 | ssl->cli_id_len = 0; |
| 1214 | } |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 1215 | #endif |
| 1216 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1217 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 1218 | return( ret ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1219 | |
| 1220 | return( 0 ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1221 | } |
| 1222 | |
Manuel Pégourié-Gonnard | 779e429 | 2013-08-03 13:50:48 +0200 | [diff] [blame] | 1223 | /* |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1224 | * Reset an initialized and used SSL context for re-use while retaining |
| 1225 | * all application-set variables, function pointers and data. |
| 1226 | */ |
| 1227 | int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl ) |
| 1228 | { |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 1229 | return( mbedtls_ssl_session_reset_int( ssl, 0 ) ); |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1233 | * SSL set accessors |
| 1234 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1235 | void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1236 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1237 | conf->endpoint = endpoint; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1238 | } |
| 1239 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 1240 | 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] | 1241 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1242 | conf->transport = transport; |
Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 1243 | } |
| 1244 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1245 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1246 | 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] | 1247 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1248 | conf->anti_replay = mode; |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 1249 | } |
| 1250 | #endif |
| 1251 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1252 | 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] | 1253 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1254 | conf->badmac_limit = limit; |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 1255 | } |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 1256 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1257 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 1258 | |
Hanno Becker | 1841b0a | 2018-08-24 11:13:57 +0100 | [diff] [blame] | 1259 | void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl, |
| 1260 | unsigned allow_packing ) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 1261 | { |
| 1262 | ssl->disable_datagram_packing = !allow_packing; |
| 1263 | } |
| 1264 | |
| 1265 | void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, |
| 1266 | uint32_t min, uint32_t max ) |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 1267 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1268 | conf->hs_timeout_min = min; |
| 1269 | conf->hs_timeout_max = max; |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 1270 | } |
| 1271 | #endif |
| 1272 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1273 | void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1274 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1275 | conf->authmode = authmode; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1278 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1279 | void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 1280 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1281 | void *p_vrfy ) |
| 1282 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1283 | conf->f_vrfy = f_vrfy; |
| 1284 | conf->p_vrfy = p_vrfy; |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1285 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1286 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1287 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1288 | void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 1289 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1290 | void *p_rng ) |
| 1291 | { |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 1292 | conf->f_rng = f_rng; |
| 1293 | conf->p_rng = p_rng; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1294 | } |
| 1295 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1296 | void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 1297 | void (*f_dbg)(void *, int, const char *, int, const char *), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1298 | void *p_dbg ) |
| 1299 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1300 | conf->f_dbg = f_dbg; |
| 1301 | conf->p_dbg = p_dbg; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1304 | void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1305 | void *p_bio, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 1306 | mbedtls_ssl_send_t *f_send, |
| 1307 | mbedtls_ssl_recv_t *f_recv, |
| 1308 | mbedtls_ssl_recv_timeout_t *f_recv_timeout ) |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1309 | { |
| 1310 | ssl->p_bio = p_bio; |
| 1311 | ssl->f_send = f_send; |
| 1312 | ssl->f_recv = f_recv; |
| 1313 | ssl->f_recv_timeout = f_recv_timeout; |
Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 1314 | } |
| 1315 | |
Manuel Pégourié-Gonnard | 6e7aaca | 2018-08-20 10:37:23 +0200 | [diff] [blame] | 1316 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 1317 | void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu ) |
| 1318 | { |
| 1319 | ssl->mtu = mtu; |
| 1320 | } |
| 1321 | #endif |
| 1322 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1323 | 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] | 1324 | { |
| 1325 | conf->read_timeout = timeout; |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1326 | } |
| 1327 | |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1328 | void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl, |
| 1329 | void *p_timer, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 1330 | mbedtls_ssl_set_timer_t *f_set_timer, |
| 1331 | mbedtls_ssl_get_timer_t *f_get_timer ) |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1332 | { |
| 1333 | ssl->p_timer = p_timer; |
| 1334 | ssl->f_set_timer = f_set_timer; |
| 1335 | ssl->f_get_timer = f_get_timer; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1336 | |
| 1337 | /* Make sure we start with no timer running */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 1338 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1339 | } |
| 1340 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1341 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1342 | void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf, |
Hanno Becker | a637ff6 | 2021-04-15 08:42:48 +0100 | [diff] [blame] | 1343 | void *p_cache, |
| 1344 | mbedtls_ssl_cache_get_t *f_get_cache, |
| 1345 | mbedtls_ssl_cache_set_t *f_set_cache ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1346 | { |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 1347 | conf->p_cache = p_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1348 | conf->f_get_cache = f_get_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1349 | conf->f_set_cache = f_set_cache; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1350 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1351 | #endif /* MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1352 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1353 | #if defined(MBEDTLS_SSL_CLI_C) |
| 1354 | 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] | 1355 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1356 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1357 | |
| 1358 | if( ssl == NULL || |
| 1359 | session == NULL || |
| 1360 | ssl->session_negotiate == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1361 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1362 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1363 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1364 | } |
| 1365 | |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 1366 | if( ssl->handshake->resume == 1 ) |
| 1367 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 1368 | |
Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 1369 | if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate, |
| 1370 | session ) ) != 0 ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1371 | return( ret ); |
| 1372 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 1373 | ssl->handshake->resume = 1; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1374 | |
| 1375 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1376 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1377 | #endif /* MBEDTLS_SSL_CLI_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1378 | |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1379 | void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf, |
| 1380 | const int *ciphersuites ) |
| 1381 | { |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 1382 | conf->ciphersuite_list = ciphersuites; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1385 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | cadebe5 | 2021-08-24 10:36:45 +0800 | [diff] [blame] | 1386 | void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf, |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1387 | const int kex_modes ) |
| 1388 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1389 | 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] | 1390 | } |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1391 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1392 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1393 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 1394 | void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf, |
Nicholas Wilson | 2088e2e | 2015-09-08 16:53:18 +0100 | [diff] [blame] | 1395 | const mbedtls_x509_crt_profile *profile ) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 1396 | { |
| 1397 | conf->cert_profile = profile; |
| 1398 | } |
| 1399 | |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1400 | static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert ) |
| 1401 | { |
| 1402 | mbedtls_ssl_key_cert *cur = key_cert, *next; |
| 1403 | |
| 1404 | while( cur != NULL ) |
| 1405 | { |
| 1406 | next = cur->next; |
| 1407 | mbedtls_free( cur ); |
| 1408 | cur = next; |
| 1409 | } |
| 1410 | } |
| 1411 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1412 | /* Append a new keycert entry to a (possibly empty) list */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1413 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1414 | static int ssl_append_key_cert( mbedtls_ssl_key_cert **head, |
| 1415 | mbedtls_x509_crt *cert, |
| 1416 | mbedtls_pk_context *key ) |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1417 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1418 | mbedtls_ssl_key_cert *new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1419 | |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1420 | if( cert == NULL ) |
| 1421 | { |
| 1422 | /* Free list if cert is null */ |
| 1423 | ssl_key_cert_free( *head ); |
| 1424 | *head = NULL; |
| 1425 | return( 0 ); |
| 1426 | } |
| 1427 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1428 | new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) ); |
| 1429 | if( new_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1430 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1431 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1432 | new_cert->cert = cert; |
| 1433 | new_cert->key = key; |
| 1434 | new_cert->next = NULL; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1435 | |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1436 | /* 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] | 1437 | if( *head == NULL ) |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 1438 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1439 | *head = new_cert; |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 1440 | } |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1441 | else |
| 1442 | { |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1443 | mbedtls_ssl_key_cert *cur = *head; |
| 1444 | while( cur->next != NULL ) |
| 1445 | cur = cur->next; |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1446 | cur->next = new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1447 | } |
| 1448 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1449 | return( 0 ); |
| 1450 | } |
| 1451 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1452 | int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1453 | mbedtls_x509_crt *own_cert, |
| 1454 | mbedtls_pk_context *pk_key ) |
| 1455 | { |
Manuel Pégourié-Gonnard | 17a40cd | 2015-05-10 23:17:17 +0200 | [diff] [blame] | 1456 | 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] | 1457 | } |
| 1458 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1459 | void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1460 | mbedtls_x509_crt *ca_chain, |
| 1461 | mbedtls_x509_crl *ca_crl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1462 | { |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1463 | conf->ca_chain = ca_chain; |
| 1464 | conf->ca_crl = ca_crl; |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1465 | |
| 1466 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 1467 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 1468 | * cannot be used together. */ |
| 1469 | conf->f_ca_cb = NULL; |
| 1470 | conf->p_ca_cb = NULL; |
| 1471 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1472 | } |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1473 | |
| 1474 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 1475 | void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf, |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 1476 | mbedtls_x509_crt_ca_cb_t f_ca_cb, |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1477 | void *p_ca_cb ) |
| 1478 | { |
| 1479 | conf->f_ca_cb = f_ca_cb; |
| 1480 | conf->p_ca_cb = p_ca_cb; |
| 1481 | |
| 1482 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 1483 | * cannot be used together. */ |
| 1484 | conf->ca_chain = NULL; |
| 1485 | conf->ca_crl = NULL; |
| 1486 | } |
| 1487 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1488 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 1489 | |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1490 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Glenn Strauss | 6989407 | 2022-01-24 12:58:00 -0500 | [diff] [blame] | 1491 | const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl, |
| 1492 | size_t *name_len ) |
| 1493 | { |
| 1494 | *name_len = ssl->handshake->sni_name_len; |
| 1495 | return( ssl->handshake->sni_name ); |
| 1496 | } |
| 1497 | |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1498 | int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl, |
| 1499 | mbedtls_x509_crt *own_cert, |
| 1500 | mbedtls_pk_context *pk_key ) |
| 1501 | { |
| 1502 | return( ssl_append_key_cert( &ssl->handshake->sni_key_cert, |
| 1503 | own_cert, pk_key ) ); |
| 1504 | } |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 1505 | |
| 1506 | void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl, |
| 1507 | mbedtls_x509_crt *ca_chain, |
| 1508 | mbedtls_x509_crl *ca_crl ) |
| 1509 | { |
| 1510 | ssl->handshake->sni_ca_chain = ca_chain; |
| 1511 | ssl->handshake->sni_ca_crl = ca_crl; |
| 1512 | } |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 1513 | |
Glenn Strauss | 999ef70 | 2022-03-11 01:37:23 -0500 | [diff] [blame] | 1514 | #if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED) |
| 1515 | void mbedtls_ssl_set_hs_dn_hints( mbedtls_ssl_context *ssl, |
| 1516 | const mbedtls_x509_crt *crt) |
| 1517 | { |
| 1518 | ssl->handshake->dn_hints = crt; |
| 1519 | } |
| 1520 | #endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */ |
| 1521 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 1522 | void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl, |
| 1523 | int authmode ) |
| 1524 | { |
| 1525 | ssl->handshake->sni_authmode = authmode; |
| 1526 | } |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1527 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 1528 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 1529 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 1530 | void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl, |
| 1531 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 1532 | void *p_vrfy ) |
| 1533 | { |
| 1534 | ssl->f_vrfy = f_vrfy; |
| 1535 | ssl->p_vrfy = p_vrfy; |
| 1536 | } |
| 1537 | #endif |
| 1538 | |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 1539 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1540 | /* |
| 1541 | * Set EC J-PAKE password for current handshake |
| 1542 | */ |
| 1543 | int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl, |
| 1544 | const unsigned char *pw, |
| 1545 | size_t pw_len ) |
| 1546 | { |
| 1547 | mbedtls_ecjpake_role role; |
| 1548 | |
Janos Follath | 8eb6413 | 2016-06-03 15:40:57 +0100 | [diff] [blame] | 1549 | if( ssl->handshake == NULL || ssl->conf == NULL ) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1550 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1551 | |
| 1552 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 1553 | role = MBEDTLS_ECJPAKE_SERVER; |
| 1554 | else |
| 1555 | role = MBEDTLS_ECJPAKE_CLIENT; |
| 1556 | |
| 1557 | return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx, |
| 1558 | role, |
| 1559 | MBEDTLS_MD_SHA256, |
| 1560 | MBEDTLS_ECP_DP_SECP256R1, |
| 1561 | pw, pw_len ) ); |
| 1562 | } |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 1563 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1564 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1565 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1566 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1567 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1568 | static int ssl_conf_psk_is_configured( mbedtls_ssl_config const *conf ) |
| 1569 | { |
| 1570 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1571 | if( !mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) |
| 1572 | return( 1 ); |
| 1573 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1574 | if( conf->psk != NULL ) |
| 1575 | return( 1 ); |
| 1576 | |
| 1577 | return( 0 ); |
| 1578 | } |
| 1579 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1580 | static void ssl_conf_remove_psk( mbedtls_ssl_config *conf ) |
| 1581 | { |
| 1582 | /* Remove reference to existing PSK, if any. */ |
| 1583 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1584 | if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1585 | { |
| 1586 | /* The maintenance of the PSK key slot is the |
| 1587 | * user's responsibility. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1588 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1589 | } |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1590 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1591 | if( conf->psk != NULL ) |
| 1592 | { |
| 1593 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
| 1594 | |
| 1595 | mbedtls_free( conf->psk ); |
| 1596 | conf->psk = NULL; |
| 1597 | conf->psk_len = 0; |
| 1598 | } |
| 1599 | |
| 1600 | /* Remove reference to PSK identity, if any. */ |
| 1601 | if( conf->psk_identity != NULL ) |
| 1602 | { |
| 1603 | mbedtls_free( conf->psk_identity ); |
| 1604 | conf->psk_identity = NULL; |
| 1605 | conf->psk_identity_len = 0; |
| 1606 | } |
| 1607 | } |
| 1608 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1609 | /* This function assumes that PSK identity in the SSL config is unset. |
| 1610 | * It checks that the provided identity is well-formed and attempts |
| 1611 | * to make a copy of it in the SSL config. |
| 1612 | * On failure, the PSK identity in the config remains unset. */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1613 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1614 | static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf, |
| 1615 | unsigned char const *psk_identity, |
| 1616 | size_t psk_identity_len ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 1617 | { |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 1618 | /* Identity len will be encoded on two bytes */ |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1619 | if( psk_identity == NULL || |
| 1620 | ( psk_identity_len >> 16 ) != 0 || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1621 | psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 1622 | { |
| 1623 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1624 | } |
| 1625 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1626 | conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ); |
| 1627 | if( conf->psk_identity == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1628 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 1629 | |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 1630 | conf->psk_identity_len = psk_identity_len; |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 1631 | memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len ); |
Paul Bakker | 5ad403f | 2013-09-18 21:21:30 +0200 | [diff] [blame] | 1632 | |
| 1633 | return( 0 ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 1634 | } |
| 1635 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1636 | int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf, |
| 1637 | const unsigned char *psk, size_t psk_len, |
| 1638 | const unsigned char *psk_identity, size_t psk_identity_len ) |
| 1639 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1640 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1641 | |
| 1642 | /* We currently only support one PSK, raw or opaque. */ |
| 1643 | if( ssl_conf_psk_is_configured( conf ) ) |
| 1644 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1645 | |
| 1646 | /* Check and set raw PSK */ |
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 1647 | if( psk == NULL ) |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1648 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 1649 | if( psk_len == 0 ) |
| 1650 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1651 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 1652 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1653 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1654 | if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
| 1655 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 1656 | conf->psk_len = psk_len; |
| 1657 | memcpy( conf->psk, psk, conf->psk_len ); |
| 1658 | |
| 1659 | /* Check and set PSK Identity */ |
| 1660 | ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len ); |
| 1661 | if( ret != 0 ) |
| 1662 | ssl_conf_remove_psk( conf ); |
| 1663 | |
| 1664 | return( ret ); |
| 1665 | } |
| 1666 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1667 | static void ssl_remove_psk( mbedtls_ssl_context *ssl ) |
| 1668 | { |
| 1669 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1670 | if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) ) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1671 | { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1672 | /* The maintenance of the external PSK key slot is the |
| 1673 | * user's responsibility. */ |
| 1674 | if( ssl->handshake->psk_opaque_is_internal ) |
| 1675 | { |
| 1676 | psa_destroy_key( ssl->handshake->psk_opaque ); |
| 1677 | ssl->handshake->psk_opaque_is_internal = 0; |
| 1678 | } |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1679 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1680 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 1681 | #else |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1682 | if( ssl->handshake->psk != NULL ) |
| 1683 | { |
| 1684 | mbedtls_platform_zeroize( ssl->handshake->psk, |
| 1685 | ssl->handshake->psk_len ); |
| 1686 | mbedtls_free( ssl->handshake->psk ); |
| 1687 | ssl->handshake->psk_len = 0; |
| 1688 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 1689 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1690 | } |
| 1691 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1692 | int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl, |
| 1693 | const unsigned char *psk, size_t psk_len ) |
| 1694 | { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1695 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1696 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); |
Jerry Yu | 5d01c05 | 2022-08-17 10:18:10 +0800 | [diff] [blame] | 1697 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Jerry Yu | 24b8c81 | 2022-08-20 19:06:56 +0800 | [diff] [blame] | 1698 | psa_algorithm_t alg = PSA_ALG_NONE; |
Jerry Yu | 5d01c05 | 2022-08-17 10:18:10 +0800 | [diff] [blame] | 1699 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1700 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1701 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1702 | if( psk == NULL || ssl->handshake == NULL ) |
| 1703 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1704 | |
| 1705 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 1706 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1707 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1708 | ssl_remove_psk( ssl ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1709 | |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1710 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 1711 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1712 | if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 ) |
| 1713 | { |
Jerry Yu | 6cf6b47 | 2022-08-16 14:50:28 +0800 | [diff] [blame] | 1714 | if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 1715 | alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 ); |
| 1716 | else |
| 1717 | alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 ); |
| 1718 | psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE ); |
| 1719 | } |
| 1720 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1721 | |
Jerry Yu | 568ec25 | 2022-07-22 21:27:34 +0800 | [diff] [blame] | 1722 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 1723 | if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
| 1724 | { |
| 1725 | alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH ); |
| 1726 | psa_set_key_usage_flags( &key_attributes, |
| 1727 | PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT ); |
| 1728 | } |
| 1729 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 1730 | |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1731 | psa_set_key_algorithm( &key_attributes, alg ); |
| 1732 | psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE ); |
| 1733 | |
| 1734 | status = psa_import_key( &key_attributes, psk, psk_len, &key ); |
| 1735 | if( status != PSA_SUCCESS ) |
| 1736 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 1737 | |
| 1738 | /* Allow calling psa_destroy_key() on psk remove */ |
| 1739 | ssl->handshake->psk_opaque_is_internal = 1; |
| 1740 | return mbedtls_ssl_set_hs_psk_opaque( ssl, key ); |
| 1741 | #else |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 1742 | if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1743 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1744 | |
| 1745 | ssl->handshake->psk_len = psk_len; |
| 1746 | memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len ); |
| 1747 | |
| 1748 | return( 0 ); |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1749 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1750 | } |
| 1751 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1752 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1753 | int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf, |
Andrzej Kurek | 03e0146 | 2022-01-03 12:53:24 +0100 | [diff] [blame] | 1754 | mbedtls_svc_key_id_t psk, |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1755 | const unsigned char *psk_identity, |
| 1756 | size_t psk_identity_len ) |
| 1757 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1758 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1759 | |
| 1760 | /* We currently only support one PSK, raw or opaque. */ |
| 1761 | if( ssl_conf_psk_is_configured( conf ) ) |
| 1762 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1763 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1764 | /* Check and set opaque PSK */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1765 | if( mbedtls_svc_key_id_is_null( psk ) ) |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1766 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1767 | conf->psk_opaque = psk; |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1768 | |
| 1769 | /* Check and set PSK Identity */ |
| 1770 | ret = ssl_conf_set_psk_identity( conf, psk_identity, |
| 1771 | psk_identity_len ); |
| 1772 | if( ret != 0 ) |
| 1773 | ssl_conf_remove_psk( conf ); |
| 1774 | |
| 1775 | return( ret ); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1776 | } |
| 1777 | |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 1778 | int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl, |
| 1779 | mbedtls_svc_key_id_t psk ) |
| 1780 | { |
| 1781 | if( ( mbedtls_svc_key_id_is_null( psk ) ) || |
| 1782 | ( ssl->handshake == NULL ) ) |
| 1783 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1784 | |
| 1785 | ssl_remove_psk( ssl ); |
| 1786 | ssl->handshake->psk_opaque = psk; |
| 1787 | return( 0 ); |
| 1788 | } |
| 1789 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1790 | |
| 1791 | void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf, |
| 1792 | int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, |
| 1793 | size_t), |
| 1794 | void *p_psk ) |
| 1795 | { |
| 1796 | conf->f_psk = f_psk; |
| 1797 | conf->p_psk = p_psk; |
| 1798 | } |
| 1799 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
| 1800 | |
| 1801 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 1802 | static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode( |
| 1803 | psa_algorithm_t alg ) |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1804 | { |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1805 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1806 | if( alg == PSA_ALG_CBC_NO_PADDING ) |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1807 | return( MBEDTLS_SSL_MODE_CBC ); |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1808 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1809 | if( PSA_ALG_IS_AEAD( alg ) ) |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1810 | return( MBEDTLS_SSL_MODE_AEAD ); |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 1811 | return( MBEDTLS_SSL_MODE_STREAM ); |
| 1812 | } |
| 1813 | |
| 1814 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1815 | |
| 1816 | static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode( |
| 1817 | mbedtls_cipher_mode_t mode ) |
| 1818 | { |
| 1819 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 1820 | if( mode == MBEDTLS_MODE_CBC ) |
| 1821 | return( MBEDTLS_SSL_MODE_CBC ); |
| 1822 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 1823 | |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1824 | #if defined(MBEDTLS_GCM_C) || \ |
| 1825 | defined(MBEDTLS_CCM_C) || \ |
| 1826 | defined(MBEDTLS_CHACHAPOLY_C) |
| 1827 | if( mode == MBEDTLS_MODE_GCM || |
| 1828 | mode == MBEDTLS_MODE_CCM || |
| 1829 | mode == MBEDTLS_MODE_CHACHAPOLY ) |
| 1830 | return( MBEDTLS_SSL_MODE_AEAD ); |
| 1831 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1832 | |
| 1833 | return( MBEDTLS_SSL_MODE_STREAM ); |
| 1834 | } |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 1835 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1836 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1837 | static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode( |
| 1838 | mbedtls_ssl_mode_t base_mode, |
| 1839 | int encrypt_then_mac ) |
| 1840 | { |
| 1841 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
| 1842 | if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED && |
| 1843 | base_mode == MBEDTLS_SSL_MODE_CBC ) |
| 1844 | { |
| 1845 | return( MBEDTLS_SSL_MODE_CBC_ETM ); |
| 1846 | } |
| 1847 | #else |
| 1848 | (void) encrypt_then_mac; |
| 1849 | #endif |
| 1850 | return( base_mode ); |
| 1851 | } |
| 1852 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 1853 | mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform( |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1854 | const mbedtls_ssl_transform *transform ) |
| 1855 | { |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1856 | mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode( |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1857 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1858 | transform->psa_alg |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1859 | #else |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1860 | mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) |
| 1861 | #endif |
| 1862 | ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1863 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1864 | int encrypt_then_mac = 0; |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 1865 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1866 | encrypt_then_mac = transform->encrypt_then_mac; |
| 1867 | #endif |
| 1868 | return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1869 | } |
| 1870 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 1871 | mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite( |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 1872 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1873 | int encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 1874 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1875 | const mbedtls_ssl_ciphersuite_t *suite ) |
| 1876 | { |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1877 | mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM; |
| 1878 | |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1879 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1880 | psa_status_t status; |
| 1881 | psa_algorithm_t alg; |
| 1882 | psa_key_type_t type; |
| 1883 | size_t size; |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1884 | status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size ); |
| 1885 | if( status == PSA_SUCCESS ) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1886 | base_mode = mbedtls_ssl_get_base_mode( alg ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1887 | #else |
| 1888 | const mbedtls_cipher_info_t *cipher = |
| 1889 | mbedtls_cipher_info_from_type( suite->cipher ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1890 | if( cipher != NULL ) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1891 | { |
| 1892 | base_mode = |
| 1893 | mbedtls_ssl_get_base_mode( |
| 1894 | mbedtls_cipher_info_get_mode( cipher ) ); |
| 1895 | } |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1896 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1897 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1898 | #if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
| 1899 | int encrypt_then_mac = 0; |
| 1900 | #endif |
| 1901 | return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1902 | } |
| 1903 | |
Neil Armstrong | 8395d7a | 2022-05-18 11:44:56 +0200 | [diff] [blame] | 1904 | #if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 1905 | |
| 1906 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 1907 | /* Serialization of TLS 1.3 sessions: |
| 1908 | * |
| 1909 | * struct { |
| 1910 | * uint64 ticket_received; |
| 1911 | * uint32 ticket_lifetime; |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 1912 | * opaque ticket<1..2^16-1>; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 1913 | * } ClientOnlyData; |
| 1914 | * |
| 1915 | * struct { |
| 1916 | * uint8 endpoint; |
| 1917 | * uint8 ciphersuite[2]; |
| 1918 | * uint32 ticket_age_add; |
| 1919 | * uint8 ticket_flags; |
| 1920 | * opaque resumption_key<0..255>; |
| 1921 | * select ( endpoint ) { |
| 1922 | * case client: ClientOnlyData; |
| 1923 | * case server: uint64 start_time; |
| 1924 | * }; |
| 1925 | * } serialized_session_tls13; |
| 1926 | * |
| 1927 | */ |
| 1928 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 1929 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 1930 | static int ssl_tls13_session_save( const mbedtls_ssl_session *session, |
| 1931 | unsigned char *buf, |
| 1932 | size_t buf_len, |
| 1933 | size_t *olen ) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 1934 | { |
| 1935 | unsigned char *p = buf; |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 1936 | size_t needed = 1 /* endpoint */ |
| 1937 | + 2 /* ciphersuite */ |
| 1938 | + 4 /* ticket_age_add */ |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 1939 | + 1 /* ticket_flags */ |
| 1940 | + 1; /* resumption_key length */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 1941 | *olen = 0; |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 1942 | |
| 1943 | if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN ) |
| 1944 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1945 | needed += session->resumption_key_len; /* resumption_key */ |
| 1946 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 1947 | #if defined(MBEDTLS_HAVE_TIME) |
| 1948 | needed += 8; /* start_time or ticket_received */ |
| 1949 | #endif |
| 1950 | |
| 1951 | #if defined(MBEDTLS_SSL_CLI_C) |
| 1952 | if( session->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 1953 | { |
| 1954 | needed += 4 /* ticket_lifetime */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 1955 | + 2; /* ticket_len */ |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 1956 | |
| 1957 | /* Check size_t overflow */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 1958 | if( session->ticket_len > SIZE_MAX - needed ) |
| 1959 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 1960 | |
Jerry Yu | e28d974 | 2022-08-18 15:44:03 +0800 | [diff] [blame] | 1961 | needed += session->ticket_len; /* ticket */ |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 1962 | } |
| 1963 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 1964 | |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 1965 | *olen = needed; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 1966 | if( needed > buf_len ) |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 1967 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 1968 | |
| 1969 | p[0] = session->endpoint; |
| 1970 | MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 ); |
| 1971 | MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 ); |
| 1972 | p[7] = session->ticket_flags; |
| 1973 | |
| 1974 | /* save resumption_key */ |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 1975 | p[8] = session->resumption_key_len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 1976 | p += 9; |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 1977 | memcpy( p, session->resumption_key, session->resumption_key_len ); |
| 1978 | p += session->resumption_key_len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 1979 | |
| 1980 | #if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C) |
| 1981 | if( session->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 1982 | { |
| 1983 | MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 ); |
| 1984 | p += 8; |
| 1985 | } |
| 1986 | #endif /* MBEDTLS_HAVE_TIME */ |
| 1987 | |
| 1988 | #if defined(MBEDTLS_SSL_CLI_C) |
| 1989 | if( session->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 1990 | { |
| 1991 | #if defined(MBEDTLS_HAVE_TIME) |
| 1992 | MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 ); |
| 1993 | p += 8; |
| 1994 | #endif |
| 1995 | MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 ); |
| 1996 | p += 4; |
| 1997 | |
| 1998 | MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 ); |
| 1999 | p += 2; |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2000 | |
| 2001 | if( session->ticket != NULL && session->ticket_len > 0 ) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2002 | { |
| 2003 | memcpy( p, session->ticket, session->ticket_len ); |
| 2004 | p += session->ticket_len; |
| 2005 | } |
| 2006 | } |
| 2007 | #endif /* MBEDTLS_SSL_CLI_C */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2008 | return( 0 ); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2009 | } |
| 2010 | |
| 2011 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 2012 | static int ssl_tls13_session_load( mbedtls_ssl_session *session, |
| 2013 | const unsigned char *buf, |
| 2014 | size_t len ) |
| 2015 | { |
| 2016 | const unsigned char *p = buf; |
| 2017 | const unsigned char *end = buf + len; |
| 2018 | |
| 2019 | if( end - p < 9 ) |
| 2020 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2021 | session->endpoint = p[0]; |
| 2022 | session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 ); |
| 2023 | session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 ); |
| 2024 | session->ticket_flags = p[7]; |
| 2025 | |
| 2026 | /* load resumption_key */ |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2027 | session->resumption_key_len = p[8]; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2028 | p += 9; |
| 2029 | |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2030 | if( end - p < session->resumption_key_len ) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2031 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2032 | |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2033 | if( sizeof( session->resumption_key ) < session->resumption_key_len ) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2034 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2035 | memcpy( session->resumption_key, p, session->resumption_key_len ); |
| 2036 | p += session->resumption_key_len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2037 | |
| 2038 | #if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C) |
| 2039 | if( session->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 2040 | { |
| 2041 | if( end - p < 8 ) |
| 2042 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2043 | session->start = MBEDTLS_GET_UINT64_BE( p, 0 ); |
| 2044 | p += 8; |
| 2045 | } |
| 2046 | #endif /* MBEDTLS_HAVE_TIME */ |
| 2047 | |
| 2048 | #if defined(MBEDTLS_SSL_CLI_C) |
| 2049 | if( session->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 2050 | { |
| 2051 | #if defined(MBEDTLS_HAVE_TIME) |
| 2052 | if( end - p < 8 ) |
| 2053 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2054 | session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 ); |
| 2055 | p += 8; |
| 2056 | #endif |
| 2057 | if( end - p < 4 ) |
| 2058 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2059 | session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 ); |
| 2060 | p += 4; |
| 2061 | |
| 2062 | if( end - p < 2 ) |
| 2063 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2064 | session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
| 2065 | p += 2; |
| 2066 | |
| 2067 | if( end - p < ( long int )session->ticket_len ) |
| 2068 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2069 | if( session->ticket_len > 0 ) |
| 2070 | { |
| 2071 | session->ticket = mbedtls_calloc( 1, session->ticket_len ); |
| 2072 | if( session->ticket == NULL ) |
| 2073 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 2074 | memcpy( session->ticket, p, session->ticket_len ); |
| 2075 | p += session->ticket_len; |
| 2076 | } |
| 2077 | } |
| 2078 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 2079 | |
| 2080 | return( 0 ); |
| 2081 | |
| 2082 | } |
| 2083 | #else /* MBEDTLS_SSL_SESSION_TICKETS */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2084 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 2085 | static int ssl_tls13_session_save( const mbedtls_ssl_session *session, |
| 2086 | unsigned char *buf, |
| 2087 | size_t buf_len, |
| 2088 | size_t *olen ) |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2089 | { |
| 2090 | ((void) session); |
| 2091 | ((void) buf); |
| 2092 | ((void) buf_len); |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2093 | *olen = 0; |
| 2094 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2095 | } |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2096 | |
| 2097 | static int ssl_tls13_session_load( const mbedtls_ssl_session *session, |
| 2098 | unsigned char *buf, |
| 2099 | size_t buf_len ) |
| 2100 | { |
| 2101 | ((void) session); |
| 2102 | ((void) buf); |
| 2103 | ((void) buf_len); |
| 2104 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 2105 | } |
| 2106 | #endif /* !MBEDTLS_SSL_SESSION_TICKETS */ |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2107 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 2108 | |
Przemyslaw Stekiel | f57b456 | 2022-01-25 00:04:18 +0100 | [diff] [blame] | 2109 | 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] | 2110 | size_t taglen, |
| 2111 | psa_algorithm_t *alg, |
| 2112 | psa_key_type_t *key_type, |
| 2113 | size_t *key_size ) |
| 2114 | { |
| 2115 | switch ( mbedtls_cipher_type ) |
| 2116 | { |
| 2117 | case MBEDTLS_CIPHER_AES_128_CBC: |
| 2118 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2119 | *key_type = PSA_KEY_TYPE_AES; |
| 2120 | *key_size = 128; |
| 2121 | break; |
| 2122 | case MBEDTLS_CIPHER_AES_128_CCM: |
| 2123 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2124 | *key_type = PSA_KEY_TYPE_AES; |
| 2125 | *key_size = 128; |
| 2126 | break; |
| 2127 | case MBEDTLS_CIPHER_AES_128_GCM: |
| 2128 | *alg = PSA_ALG_GCM; |
| 2129 | *key_type = PSA_KEY_TYPE_AES; |
| 2130 | *key_size = 128; |
| 2131 | break; |
| 2132 | case MBEDTLS_CIPHER_AES_192_CCM: |
| 2133 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2134 | *key_type = PSA_KEY_TYPE_AES; |
| 2135 | *key_size = 192; |
| 2136 | break; |
| 2137 | case MBEDTLS_CIPHER_AES_192_GCM: |
| 2138 | *alg = PSA_ALG_GCM; |
| 2139 | *key_type = PSA_KEY_TYPE_AES; |
| 2140 | *key_size = 192; |
| 2141 | break; |
| 2142 | case MBEDTLS_CIPHER_AES_256_CBC: |
| 2143 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2144 | *key_type = PSA_KEY_TYPE_AES; |
| 2145 | *key_size = 256; |
| 2146 | break; |
| 2147 | case MBEDTLS_CIPHER_AES_256_CCM: |
| 2148 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2149 | *key_type = PSA_KEY_TYPE_AES; |
| 2150 | *key_size = 256; |
| 2151 | break; |
| 2152 | case MBEDTLS_CIPHER_AES_256_GCM: |
| 2153 | *alg = PSA_ALG_GCM; |
| 2154 | *key_type = PSA_KEY_TYPE_AES; |
| 2155 | *key_size = 256; |
| 2156 | break; |
| 2157 | case MBEDTLS_CIPHER_ARIA_128_CBC: |
| 2158 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2159 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2160 | *key_size = 128; |
| 2161 | break; |
| 2162 | case MBEDTLS_CIPHER_ARIA_128_CCM: |
| 2163 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2164 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2165 | *key_size = 128; |
| 2166 | break; |
| 2167 | case MBEDTLS_CIPHER_ARIA_128_GCM: |
| 2168 | *alg = PSA_ALG_GCM; |
| 2169 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2170 | *key_size = 128; |
| 2171 | break; |
| 2172 | case MBEDTLS_CIPHER_ARIA_192_CCM: |
| 2173 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2174 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2175 | *key_size = 192; |
| 2176 | break; |
| 2177 | case MBEDTLS_CIPHER_ARIA_192_GCM: |
| 2178 | *alg = PSA_ALG_GCM; |
| 2179 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2180 | *key_size = 192; |
| 2181 | break; |
| 2182 | case MBEDTLS_CIPHER_ARIA_256_CBC: |
| 2183 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2184 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2185 | *key_size = 256; |
| 2186 | break; |
| 2187 | case MBEDTLS_CIPHER_ARIA_256_CCM: |
| 2188 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2189 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2190 | *key_size = 256; |
| 2191 | break; |
| 2192 | case MBEDTLS_CIPHER_ARIA_256_GCM: |
| 2193 | *alg = PSA_ALG_GCM; |
| 2194 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2195 | *key_size = 256; |
| 2196 | break; |
| 2197 | case MBEDTLS_CIPHER_CAMELLIA_128_CBC: |
| 2198 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2199 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2200 | *key_size = 128; |
| 2201 | break; |
| 2202 | case MBEDTLS_CIPHER_CAMELLIA_128_CCM: |
| 2203 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2204 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2205 | *key_size = 128; |
| 2206 | break; |
| 2207 | case MBEDTLS_CIPHER_CAMELLIA_128_GCM: |
| 2208 | *alg = PSA_ALG_GCM; |
| 2209 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2210 | *key_size = 128; |
| 2211 | break; |
| 2212 | case MBEDTLS_CIPHER_CAMELLIA_192_CCM: |
| 2213 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2214 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2215 | *key_size = 192; |
| 2216 | break; |
| 2217 | case MBEDTLS_CIPHER_CAMELLIA_192_GCM: |
| 2218 | *alg = PSA_ALG_GCM; |
| 2219 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2220 | *key_size = 192; |
| 2221 | break; |
| 2222 | case MBEDTLS_CIPHER_CAMELLIA_256_CBC: |
| 2223 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2224 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2225 | *key_size = 256; |
| 2226 | break; |
| 2227 | case MBEDTLS_CIPHER_CAMELLIA_256_CCM: |
| 2228 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 2229 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2230 | *key_size = 256; |
| 2231 | break; |
| 2232 | case MBEDTLS_CIPHER_CAMELLIA_256_GCM: |
| 2233 | *alg = PSA_ALG_GCM; |
| 2234 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2235 | *key_size = 256; |
| 2236 | break; |
| 2237 | case MBEDTLS_CIPHER_CHACHA20_POLY1305: |
| 2238 | *alg = PSA_ALG_CHACHA20_POLY1305; |
| 2239 | *key_type = PSA_KEY_TYPE_CHACHA20; |
| 2240 | *key_size = 256; |
| 2241 | break; |
| 2242 | case MBEDTLS_CIPHER_NULL: |
| 2243 | *alg = MBEDTLS_SSL_NULL_CIPHER; |
| 2244 | *key_type = 0; |
| 2245 | *key_size = 0; |
| 2246 | break; |
| 2247 | default: |
| 2248 | return PSA_ERROR_NOT_SUPPORTED; |
| 2249 | } |
| 2250 | |
| 2251 | return PSA_SUCCESS; |
| 2252 | } |
Neil Armstrong | 8395d7a | 2022-05-18 11:44:56 +0200 | [diff] [blame] | 2253 | #endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2254 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 2255 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2256 | int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf, |
| 2257 | const unsigned char *dhm_P, size_t P_len, |
| 2258 | const unsigned char *dhm_G, size_t G_len ) |
| 2259 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2260 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2261 | |
Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 2262 | mbedtls_mpi_free( &conf->dhm_P ); |
| 2263 | mbedtls_mpi_free( &conf->dhm_G ); |
| 2264 | |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2265 | if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 || |
| 2266 | ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 ) |
| 2267 | { |
| 2268 | mbedtls_mpi_free( &conf->dhm_P ); |
| 2269 | mbedtls_mpi_free( &conf->dhm_G ); |
| 2270 | return( ret ); |
| 2271 | } |
| 2272 | |
| 2273 | return( 0 ); |
| 2274 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2275 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2276 | 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] | 2277 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2278 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2279 | |
Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 2280 | mbedtls_mpi_free( &conf->dhm_P ); |
| 2281 | mbedtls_mpi_free( &conf->dhm_G ); |
| 2282 | |
Gilles Peskine | e570248 | 2021-06-11 21:59:08 +0200 | [diff] [blame] | 2283 | if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P, |
| 2284 | &conf->dhm_P ) ) != 0 || |
| 2285 | ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G, |
| 2286 | &conf->dhm_G ) ) != 0 ) |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 2287 | { |
| 2288 | mbedtls_mpi_free( &conf->dhm_P ); |
| 2289 | mbedtls_mpi_free( &conf->dhm_G ); |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2290 | return( ret ); |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 2291 | } |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2292 | |
| 2293 | return( 0 ); |
| 2294 | } |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 2295 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2296 | |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 2297 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 2298 | /* |
| 2299 | * Set the minimum length for Diffie-Hellman parameters |
| 2300 | */ |
| 2301 | void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf, |
| 2302 | unsigned int bitlen ) |
| 2303 | { |
| 2304 | conf->dhm_min_bitlen = bitlen; |
| 2305 | } |
| 2306 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */ |
| 2307 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2308 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 2309 | #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] | 2310 | /* |
| 2311 | * Set allowed/preferred hashes for handshake signatures |
| 2312 | */ |
| 2313 | void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf, |
| 2314 | const int *hashes ) |
| 2315 | { |
| 2316 | conf->sig_hashes = hashes; |
| 2317 | } |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 2318 | #endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2319 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 2320 | /* Configure allowed signature algorithms for handshake */ |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2321 | void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf, |
| 2322 | const uint16_t* sig_algs ) |
| 2323 | { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 2324 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 2325 | conf->sig_hashes = NULL; |
| 2326 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
| 2327 | conf->sig_algs = sig_algs; |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2328 | } |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2329 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 2330 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 2331 | #if defined(MBEDTLS_ECP_C) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2332 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2333 | /* |
| 2334 | * Set the allowed elliptic curves |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2335 | * |
| 2336 | * mbedtls_ssl_setup() takes the provided list |
| 2337 | * and translates it to a list of IANA TLS group identifiers, |
| 2338 | * stored in ssl->handshake->group_list. |
| 2339 | * |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2340 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2341 | void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2342 | const mbedtls_ecp_group_id *curve_list ) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2343 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2344 | conf->curve_list = curve_list; |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2345 | conf->group_list = NULL; |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2346 | } |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2347 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2348 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2349 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2350 | /* |
| 2351 | * Set the allowed groups |
| 2352 | */ |
| 2353 | void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf, |
| 2354 | const uint16_t *group_list ) |
| 2355 | { |
| 2356 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 2357 | conf->curve_list = NULL; |
| 2358 | #endif |
| 2359 | conf->group_list = group_list; |
| 2360 | } |
| 2361 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 2362 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2363 | int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2364 | { |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2365 | /* Initialize to suppress unnecessary compiler warning */ |
| 2366 | size_t hostname_len = 0; |
| 2367 | |
| 2368 | /* Check if new hostname is valid before |
| 2369 | * making any change to current one */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2370 | if( hostname != NULL ) |
| 2371 | { |
| 2372 | hostname_len = strlen( hostname ); |
| 2373 | |
| 2374 | if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN ) |
| 2375 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2376 | } |
| 2377 | |
| 2378 | /* Now it's clear that we will overwrite the old hostname, |
| 2379 | * so we can free it safely */ |
| 2380 | |
| 2381 | if( ssl->hostname != NULL ) |
| 2382 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 2383 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2384 | mbedtls_free( ssl->hostname ); |
| 2385 | } |
| 2386 | |
| 2387 | /* Passing NULL as hostname shall clear the old one */ |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 2388 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2389 | if( hostname == NULL ) |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2390 | { |
| 2391 | ssl->hostname = NULL; |
| 2392 | } |
| 2393 | else |
| 2394 | { |
| 2395 | ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2396 | if( ssl->hostname == NULL ) |
| 2397 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 2398 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2399 | memcpy( ssl->hostname, hostname, hostname_len ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 2400 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2401 | ssl->hostname[hostname_len] = '\0'; |
| 2402 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2403 | |
| 2404 | return( 0 ); |
| 2405 | } |
Hanno Becker | 1a9a51c | 2017-04-07 13:02:16 +0100 | [diff] [blame] | 2406 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2407 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 2408 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2409 | void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2410 | int (*f_sni)(void *, mbedtls_ssl_context *, |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2411 | const unsigned char *, size_t), |
| 2412 | void *p_sni ) |
| 2413 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2414 | conf->f_sni = f_sni; |
| 2415 | conf->p_sni = p_sni; |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2416 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2417 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2418 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2419 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2420 | 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] | 2421 | { |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2422 | size_t cur_len, tot_len; |
| 2423 | const char **p; |
| 2424 | |
| 2425 | /* |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 2426 | * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings |
| 2427 | * MUST NOT be truncated." |
| 2428 | * We check lengths now rather than later. |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2429 | */ |
| 2430 | tot_len = 0; |
| 2431 | for( p = protos; *p != NULL; p++ ) |
| 2432 | { |
| 2433 | cur_len = strlen( *p ); |
| 2434 | tot_len += cur_len; |
| 2435 | |
Ronald Cron | 8216dd3 | 2020-04-23 16:41:44 +0200 | [diff] [blame] | 2436 | if( ( cur_len == 0 ) || |
| 2437 | ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) || |
| 2438 | ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2439 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2440 | } |
| 2441 | |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2442 | conf->alpn_list = protos; |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2443 | |
| 2444 | return( 0 ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2445 | } |
| 2446 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2447 | 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] | 2448 | { |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2449 | return( ssl->alpn_chosen ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2450 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2451 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2452 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2453 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 2454 | void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf, |
| 2455 | int support_mki_value ) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2456 | { |
| 2457 | conf->dtls_srtp_mki_support = support_mki_value; |
| 2458 | } |
| 2459 | |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 2460 | int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl, |
| 2461 | unsigned char *mki_value, |
Johan Pascal | f6417ec | 2020-09-22 15:15:19 +0200 | [diff] [blame] | 2462 | uint16_t mki_len ) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2463 | { |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2464 | if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH ) |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2465 | { |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2466 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2467 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2468 | |
| 2469 | 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] | 2470 | { |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2471 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2472 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2473 | |
| 2474 | memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len ); |
| 2475 | ssl->dtls_srtp_info.mki_len = mki_len; |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2476 | return( 0 ); |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2477 | } |
| 2478 | |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 2479 | int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf, |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2480 | const mbedtls_ssl_srtp_profile *profiles ) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2481 | { |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2482 | const mbedtls_ssl_srtp_profile *p; |
| 2483 | size_t list_size = 0; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2484 | |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2485 | /* check the profiles list: all entry must be valid, |
| 2486 | * 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] | 2487 | for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET && |
| 2488 | list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH; |
| 2489 | p++ ) |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2490 | { |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2491 | if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET ) |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2492 | { |
Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 2493 | list_size++; |
| 2494 | } |
| 2495 | else |
| 2496 | { |
| 2497 | /* unsupported value, stop parsing and set the size to an error value */ |
| 2498 | list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2499 | } |
| 2500 | } |
| 2501 | |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2502 | if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH ) |
Johan Pascal | d387aa0 | 2020-09-23 18:47:56 +0200 | [diff] [blame] | 2503 | { |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2504 | conf->dtls_srtp_profile_list = NULL; |
| 2505 | conf->dtls_srtp_profile_list_len = 0; |
| 2506 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2507 | } |
| 2508 | |
Johan Pascal | 9bc97ca | 2020-09-21 23:44:45 +0200 | [diff] [blame] | 2509 | conf->dtls_srtp_profile_list = profiles; |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2510 | conf->dtls_srtp_profile_list_len = list_size; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2511 | |
| 2512 | return( 0 ); |
| 2513 | } |
| 2514 | |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2515 | void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl, |
| 2516 | mbedtls_dtls_srtp_info *dtls_srtp_info ) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2517 | { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2518 | dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile; |
| 2519 | /* do not copy the mki value if there is no chosen profile */ |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2520 | if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET ) |
Johan Pascal | 0dbcd1d | 2020-10-28 11:03:07 +0100 | [diff] [blame] | 2521 | { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2522 | dtls_srtp_info->mki_len = 0; |
Johan Pascal | 0dbcd1d | 2020-10-28 11:03:07 +0100 | [diff] [blame] | 2523 | } |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2524 | else |
| 2525 | { |
| 2526 | dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len; |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2527 | memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value, |
| 2528 | ssl->dtls_srtp_info.mki_len ); |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2529 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2530 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2531 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ |
| 2532 | |
Aditya Patwardhan | 3096f33 | 2022-07-26 14:31:46 +0530 | [diff] [blame] | 2533 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 2534 | 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] | 2535 | { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 2536 | conf->max_tls_version = (major << 8) | minor; |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 2537 | } |
| 2538 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 2539 | 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] | 2540 | { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 2541 | conf->min_tls_version = (major << 8) | minor; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 2542 | } |
Aditya Patwardhan | 3096f33 | 2022-07-26 14:31:46 +0530 | [diff] [blame] | 2543 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 2544 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 2545 | #if defined(MBEDTLS_SSL_SRV_C) |
| 2546 | void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf, |
| 2547 | char cert_req_ca_list ) |
| 2548 | { |
| 2549 | conf->cert_req_ca_list = cert_req_ca_list; |
| 2550 | } |
| 2551 | #endif |
| 2552 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2553 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2554 | 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] | 2555 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2556 | conf->encrypt_then_mac = etm; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2557 | } |
| 2558 | #endif |
| 2559 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2560 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2561 | 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] | 2562 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2563 | conf->extended_ms = ems; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2564 | } |
| 2565 | #endif |
| 2566 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2567 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2568 | 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] | 2569 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2570 | if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2571 | 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] | 2572 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2573 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2574 | } |
| 2575 | |
Manuel Pégourié-Gonnard | 6bf89d6 | 2015-05-05 17:01:57 +0100 | [diff] [blame] | 2576 | conf->mfl_code = mfl_code; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2577 | |
| 2578 | return( 0 ); |
| 2579 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2580 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2581 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2582 | 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] | 2583 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2584 | conf->allow_legacy_renegotiation = allow_legacy; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2585 | } |
| 2586 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2587 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2588 | 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] | 2589 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2590 | conf->disable_renegotiation = renegotiation; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2591 | } |
| 2592 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2593 | 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] | 2594 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2595 | conf->renego_max_records = max_records; |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 2596 | } |
| 2597 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2598 | void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 2599 | const unsigned char period[8] ) |
| 2600 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2601 | memcpy( conf->renego_period, period, 8 ); |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 2602 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2603 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2604 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2605 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2606 | #if defined(MBEDTLS_SSL_CLI_C) |
| 2607 | 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] | 2608 | { |
Manuel Pégourié-Gonnard | 2b49445 | 2015-05-06 10:05:11 +0100 | [diff] [blame] | 2609 | conf->session_tickets = use_tickets; |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 2610 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2611 | #endif |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 2612 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2613 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 2614 | void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf, |
| 2615 | mbedtls_ssl_ticket_write_t *f_ticket_write, |
| 2616 | mbedtls_ssl_ticket_parse_t *f_ticket_parse, |
| 2617 | void *p_ticket ) |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 2618 | { |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 2619 | conf->f_ticket_write = f_ticket_write; |
| 2620 | conf->f_ticket_parse = f_ticket_parse; |
| 2621 | conf->p_ticket = p_ticket; |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 2622 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2623 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2624 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 2625 | |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 2626 | void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl, |
| 2627 | mbedtls_ssl_export_keys_t *f_export_keys, |
| 2628 | void *p_export_keys ) |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 2629 | { |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 2630 | ssl->f_export_keys = f_export_keys; |
| 2631 | ssl->p_export_keys = p_export_keys; |
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 2632 | } |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 2633 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 2634 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2635 | void mbedtls_ssl_conf_async_private_cb( |
| 2636 | mbedtls_ssl_config *conf, |
| 2637 | mbedtls_ssl_async_sign_t *f_async_sign, |
| 2638 | mbedtls_ssl_async_decrypt_t *f_async_decrypt, |
| 2639 | mbedtls_ssl_async_resume_t *f_async_resume, |
| 2640 | mbedtls_ssl_async_cancel_t *f_async_cancel, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2641 | void *async_config_data ) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2642 | { |
| 2643 | conf->f_async_sign_start = f_async_sign; |
| 2644 | conf->f_async_decrypt_start = f_async_decrypt; |
| 2645 | conf->f_async_resume = f_async_resume; |
| 2646 | conf->f_async_cancel = f_async_cancel; |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2647 | conf->p_async_config_data = async_config_data; |
| 2648 | } |
| 2649 | |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 2650 | void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf ) |
| 2651 | { |
| 2652 | return( conf->p_async_config_data ); |
| 2653 | } |
| 2654 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 2655 | void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl ) |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2656 | { |
| 2657 | if( ssl->handshake == NULL ) |
| 2658 | return( NULL ); |
| 2659 | else |
| 2660 | return( ssl->handshake->user_async_ctx ); |
| 2661 | } |
| 2662 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 2663 | void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2664 | void *ctx ) |
| 2665 | { |
| 2666 | if( ssl->handshake != NULL ) |
| 2667 | ssl->handshake->user_async_ctx = ctx; |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2668 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 2669 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2670 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2671 | /* |
| 2672 | * SSL get accessors |
| 2673 | */ |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 2674 | uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2675 | { |
Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 2676 | if( ssl->session != NULL ) |
| 2677 | return( ssl->session->verify_result ); |
| 2678 | |
| 2679 | if( ssl->session_negotiate != NULL ) |
| 2680 | return( ssl->session_negotiate->verify_result ); |
| 2681 | |
Manuel Pégourié-Gonnard | 6ab9b00 | 2015-05-14 11:25:04 +0200 | [diff] [blame] | 2682 | return( 0xFFFFFFFF ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2683 | } |
| 2684 | |
Glenn Strauss | 8f52690 | 2022-01-13 00:04:49 -0500 | [diff] [blame] | 2685 | int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl ) |
| 2686 | { |
| 2687 | if( ssl == NULL || ssl->session == NULL ) |
| 2688 | return( 0 ); |
| 2689 | |
| 2690 | return( ssl->session->ciphersuite ); |
| 2691 | } |
| 2692 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2693 | const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 2694 | { |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 2695 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2696 | return( NULL ); |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 2697 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2698 | return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite ); |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 2699 | } |
| 2700 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2701 | const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2702 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2703 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2704 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2705 | { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2706 | switch( ssl->tls_version ) |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2707 | { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2708 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2709 | return( "DTLSv1.2" ); |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2710 | default: |
| 2711 | return( "unknown (DTLS)" ); |
| 2712 | } |
| 2713 | } |
| 2714 | #endif |
| 2715 | |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2716 | switch( ssl->tls_version ) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2717 | { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2718 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2719 | return( "TLSv1.2" ); |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2720 | case MBEDTLS_SSL_VERSION_TLS1_3: |
Gilles Peskine | c63a1e0 | 2022-01-13 01:10:24 +0100 | [diff] [blame] | 2721 | return( "TLSv1.3" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2722 | default: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2723 | return( "unknown" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2724 | } |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2725 | } |
| 2726 | |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 2727 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 2728 | size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl ) |
| 2729 | { |
David Horstmann | 95d516f | 2021-05-04 18:36:56 +0100 | [diff] [blame] | 2730 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 2731 | size_t read_mfl; |
| 2732 | |
| 2733 | /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */ |
| 2734 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 2735 | ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE ) |
| 2736 | { |
| 2737 | return ssl_mfl_code_to_length( ssl->conf->mfl_code ); |
| 2738 | } |
| 2739 | |
| 2740 | /* Check if a smaller max length was negotiated */ |
| 2741 | if( ssl->session_out != NULL ) |
| 2742 | { |
| 2743 | read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code ); |
| 2744 | if( read_mfl < max_len ) |
| 2745 | { |
| 2746 | max_len = read_mfl; |
| 2747 | } |
| 2748 | } |
| 2749 | |
| 2750 | // During a handshake, use the value being negotiated |
| 2751 | if( ssl->session_negotiate != NULL ) |
| 2752 | { |
| 2753 | read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); |
| 2754 | if( read_mfl < max_len ) |
| 2755 | { |
| 2756 | max_len = read_mfl; |
| 2757 | } |
| 2758 | } |
| 2759 | |
| 2760 | return( max_len ); |
| 2761 | } |
| 2762 | |
| 2763 | 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] | 2764 | { |
| 2765 | size_t max_len; |
| 2766 | |
| 2767 | /* |
| 2768 | * Assume mfl_code is correct since it was checked when set |
| 2769 | */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2770 | 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] | 2771 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 2772 | /* Check if a smaller max length was negotiated */ |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 2773 | if( ssl->session_out != NULL && |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2774 | 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] | 2775 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2776 | 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] | 2777 | } |
| 2778 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 2779 | /* During a handshake, use the value being negotiated */ |
| 2780 | if( ssl->session_negotiate != NULL && |
| 2781 | ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len ) |
| 2782 | { |
| 2783 | max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); |
| 2784 | } |
| 2785 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2786 | return( max_len ); |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 2787 | } |
| 2788 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 2789 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 2790 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 2791 | 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] | 2792 | { |
Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 2793 | /* Return unlimited mtu for client hello messages to avoid fragmentation. */ |
| 2794 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 2795 | ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO || |
| 2796 | ssl->state == MBEDTLS_SSL_SERVER_HELLO ) ) |
| 2797 | return ( 0 ); |
| 2798 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 2799 | if( ssl->handshake == NULL || ssl->handshake->mtu == 0 ) |
| 2800 | return( ssl->mtu ); |
| 2801 | |
| 2802 | if( ssl->mtu == 0 ) |
| 2803 | return( ssl->handshake->mtu ); |
| 2804 | |
| 2805 | return( ssl->mtu < ssl->handshake->mtu ? |
| 2806 | ssl->mtu : ssl->handshake->mtu ); |
| 2807 | } |
| 2808 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 2809 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2810 | int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl ) |
| 2811 | { |
| 2812 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
| 2813 | |
Manuel Pégourié-Gonnard | 000281e | 2018-08-21 11:20:58 +0200 | [diff] [blame] | 2814 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 2815 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2816 | (void) ssl; |
| 2817 | #endif |
| 2818 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2819 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 2820 | 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] | 2821 | |
| 2822 | if( max_len > mfl ) |
| 2823 | max_len = mfl; |
| 2824 | #endif |
| 2825 | |
| 2826 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 2827 | if( mbedtls_ssl_get_current_mtu( ssl ) != 0 ) |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2828 | { |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 2829 | const size_t mtu = mbedtls_ssl_get_current_mtu( ssl ); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2830 | const int ret = mbedtls_ssl_get_record_expansion( ssl ); |
| 2831 | const size_t overhead = (size_t) ret; |
| 2832 | |
| 2833 | if( ret < 0 ) |
| 2834 | return( ret ); |
| 2835 | |
| 2836 | if( mtu <= overhead ) |
| 2837 | { |
| 2838 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) ); |
| 2839 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 2840 | } |
| 2841 | |
| 2842 | if( max_len > mtu - overhead ) |
| 2843 | max_len = mtu - overhead; |
| 2844 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 2845 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2846 | |
Hanno Becker | 0defedb | 2018-08-10 12:35:02 +0100 | [diff] [blame] | 2847 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 2848 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2849 | ((void) ssl); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2850 | #endif |
| 2851 | |
| 2852 | return( (int) max_len ); |
| 2853 | } |
| 2854 | |
Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 2855 | int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl ) |
| 2856 | { |
| 2857 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
| 2858 | |
| 2859 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 2860 | (void) ssl; |
| 2861 | #endif |
| 2862 | |
| 2863 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 2864 | const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl ); |
| 2865 | |
| 2866 | if( max_len > mfl ) |
| 2867 | max_len = mfl; |
| 2868 | #endif |
| 2869 | |
| 2870 | return( (int) max_len ); |
| 2871 | } |
| 2872 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2873 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 2874 | 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] | 2875 | { |
| 2876 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2877 | return( NULL ); |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 2878 | |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 2879 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2880 | return( ssl->session->peer_cert ); |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 2881 | #else |
| 2882 | return( NULL ); |
| 2883 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 2884 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2885 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 2886 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2887 | #if defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | f852b1c | 2019-02-05 11:42:30 +0000 | [diff] [blame] | 2888 | int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, |
| 2889 | mbedtls_ssl_session *dst ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2890 | { |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 2891 | int ret; |
| 2892 | |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2893 | if( ssl == NULL || |
| 2894 | dst == NULL || |
| 2895 | ssl->session == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2896 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2897 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2898 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2899 | } |
| 2900 | |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 2901 | /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer |
| 2902 | * idempotent: Each session can only be exported once. |
| 2903 | * |
| 2904 | * (This is in preparation for TLS 1.3 support where we will |
| 2905 | * need the ability to export multiple sessions (aka tickets), |
| 2906 | * which will be achieved by calling mbedtls_ssl_get_session() |
| 2907 | * multiple times until it fails.) |
| 2908 | * |
| 2909 | * Check whether we have already exported the current session, |
| 2910 | * and fail if so. |
| 2911 | */ |
| 2912 | if( ssl->session->exported == 1 ) |
| 2913 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 2914 | |
| 2915 | ret = mbedtls_ssl_session_copy( dst, ssl->session ); |
| 2916 | if( ret != 0 ) |
| 2917 | return( ret ); |
| 2918 | |
| 2919 | /* Remember that we've exported the session. */ |
| 2920 | ssl->session->exported = 1; |
| 2921 | return( 0 ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2922 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2923 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2924 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2925 | /* |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 2926 | * Define ticket header determining Mbed TLS version |
| 2927 | * and structure of the ticket. |
| 2928 | */ |
| 2929 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2930 | /* |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 2931 | * Define bitflag determining compile-time settings influencing |
| 2932 | * structure of serialized SSL sessions. |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2933 | */ |
| 2934 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 2935 | #if defined(MBEDTLS_HAVE_TIME) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2936 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 1 |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 2937 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2938 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2939 | #endif /* MBEDTLS_HAVE_TIME */ |
| 2940 | |
| 2941 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2942 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2943 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2944 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2945 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 2946 | |
| 2947 | #if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2948 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2949 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2950 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2951 | #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */ |
| 2952 | |
| 2953 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2954 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2955 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2956 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2957 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 2958 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2959 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2960 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2961 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2962 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2963 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
| 2964 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2965 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 2966 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1 |
| 2967 | #else |
| 2968 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0 |
| 2969 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 2970 | |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2971 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0 |
| 2972 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1 |
| 2973 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2 |
| 2974 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3 |
Hanno Becker | 37bdbe6 | 2021-08-01 05:38:58 +0100 | [diff] [blame] | 2975 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4 |
| 2976 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5 |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2977 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 2978 | #define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \ |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2979 | ( (uint16_t) ( \ |
| 2980 | ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \ |
| 2981 | ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \ |
| 2982 | ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \ |
| 2983 | ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \ |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2984 | ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \ |
Hanno Becker | be34e8e | 2019-06-04 09:43:16 +0100 | [diff] [blame] | 2985 | ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) ) |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2986 | |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 2987 | static unsigned char ssl_serialized_session_header[] = { |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2988 | MBEDTLS_VERSION_MAJOR, |
| 2989 | MBEDTLS_VERSION_MINOR, |
| 2990 | MBEDTLS_VERSION_PATCH, |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 2991 | MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 2992 | MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 2993 | }; |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 2994 | |
| 2995 | /* |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 2996 | * Serialize a session in the following format: |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 2997 | * (in the presentation language of TLS, RFC 8446 section 3) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 2998 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2999 | * struct { |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 3000 | * |
Hanno Becker | dce5097 | 2021-08-01 05:39:23 +0100 | [diff] [blame] | 3001 | * opaque mbedtls_version[3]; // library version: major, minor, patch |
| 3002 | * opaque session_format[2]; // library-version specific 16-bit field |
| 3003 | * // determining the format of the remaining |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3004 | * // serialized data. |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 3005 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3006 | * Note: When updating the format, remember to keep |
| 3007 | * these version+format bytes. |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3008 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3009 | * // In this version, `session_format` determines |
| 3010 | * // the setting of those compile-time |
| 3011 | * // configuration options which influence |
| 3012 | * // the structure of mbedtls_ssl_session. |
| 3013 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3014 | * uint8_t minor_ver; // Protocol minor version. Possible values: |
| 3015 | * // - TLS 1.2 (3) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3016 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3017 | * select (serialized_session.tls_version) { |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3018 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3019 | * case MBEDTLS_SSL_VERSION_TLS1_2: |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3020 | * serialized_session_tls12 data; |
| 3021 | * |
| 3022 | * }; |
| 3023 | * |
| 3024 | * } serialized_session; |
| 3025 | * |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3026 | */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3027 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3028 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3029 | static int ssl_session_save( const mbedtls_ssl_session *session, |
| 3030 | unsigned char omit_header, |
| 3031 | unsigned char *buf, |
| 3032 | size_t buf_len, |
| 3033 | size_t *olen ) |
| 3034 | { |
| 3035 | unsigned char *p = buf; |
| 3036 | size_t used = 0; |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3037 | size_t remaining_len; |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 3038 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3039 | size_t out_len; |
| 3040 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 3041 | #endif |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3042 | if( session == NULL ) |
| 3043 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3044 | |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3045 | if( !omit_header ) |
| 3046 | { |
| 3047 | /* |
| 3048 | * Add Mbed TLS version identifier |
| 3049 | */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3050 | used += sizeof( ssl_serialized_session_header ); |
| 3051 | |
| 3052 | if( used <= buf_len ) |
| 3053 | { |
| 3054 | memcpy( p, ssl_serialized_session_header, |
| 3055 | sizeof( ssl_serialized_session_header ) ); |
| 3056 | p += sizeof( ssl_serialized_session_header ); |
| 3057 | } |
| 3058 | } |
| 3059 | |
| 3060 | /* |
| 3061 | * TLS version identifier |
| 3062 | */ |
| 3063 | used += 1; |
| 3064 | if( used <= buf_len ) |
| 3065 | { |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3066 | *p++ = MBEDTLS_BYTE_0( session->tls_version ); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3067 | } |
| 3068 | |
| 3069 | /* Forward to version-specific serialization routine. */ |
Jerry Yu | fca4d57 | 2022-07-21 10:37:48 +0800 | [diff] [blame] | 3070 | remaining_len = (buf_len >= used) ? buf_len - used : 0; |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3071 | switch( session->tls_version ) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3072 | { |
| 3073 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3074 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3075 | used += ssl_tls12_session_save( session, p, remaining_len ); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3076 | break; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3077 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3078 | |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3079 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3080 | case MBEDTLS_SSL_VERSION_TLS1_3: |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 3081 | ret = ssl_tls13_session_save( session, p, remaining_len, &out_len ); |
| 3082 | if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ) |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 3083 | return( ret ); |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 3084 | used += out_len; |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3085 | break; |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3086 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 3087 | |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3088 | default: |
| 3089 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 3090 | } |
| 3091 | |
| 3092 | *olen = used; |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 3093 | if( used > buf_len ) |
| 3094 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3095 | |
| 3096 | return( 0 ); |
| 3097 | } |
| 3098 | |
| 3099 | /* |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3100 | * Public wrapper for ssl_session_save() |
| 3101 | */ |
| 3102 | int mbedtls_ssl_session_save( const mbedtls_ssl_session *session, |
| 3103 | unsigned char *buf, |
| 3104 | size_t buf_len, |
| 3105 | size_t *olen ) |
| 3106 | { |
| 3107 | return( ssl_session_save( session, 0, buf, buf_len, olen ) ); |
| 3108 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3109 | |
Jerry Yu | f1b23ca | 2022-02-18 11:48:47 +0800 | [diff] [blame] | 3110 | /* |
| 3111 | * Deserialize session, see mbedtls_ssl_session_save() for format. |
| 3112 | * |
| 3113 | * This internal version is wrapped by a public function that cleans up in |
| 3114 | * case of error, and has an extra option omit_header. |
| 3115 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3116 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3117 | static int ssl_session_load( mbedtls_ssl_session *session, |
| 3118 | unsigned char omit_header, |
| 3119 | const unsigned char *buf, |
| 3120 | size_t len ) |
| 3121 | { |
| 3122 | const unsigned char *p = buf; |
| 3123 | const unsigned char * const end = buf + len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3124 | size_t remaining_len; |
| 3125 | |
| 3126 | |
| 3127 | if( session == NULL ) |
| 3128 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3129 | |
| 3130 | if( !omit_header ) |
| 3131 | { |
| 3132 | /* |
| 3133 | * Check Mbed TLS version identifier |
| 3134 | */ |
| 3135 | |
| 3136 | if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) ) |
| 3137 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3138 | |
| 3139 | if( memcmp( p, ssl_serialized_session_header, |
| 3140 | sizeof( ssl_serialized_session_header ) ) != 0 ) |
| 3141 | { |
| 3142 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
| 3143 | } |
| 3144 | p += sizeof( ssl_serialized_session_header ); |
| 3145 | } |
| 3146 | |
| 3147 | /* |
| 3148 | * TLS version identifier |
| 3149 | */ |
| 3150 | if( 1 > (size_t)( end - p ) ) |
| 3151 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3152 | session->tls_version = 0x0300 | *p++; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3153 | |
| 3154 | /* Dispatch according to TLS version. */ |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3155 | remaining_len = ( end - p ); |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3156 | switch( session->tls_version ) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3157 | { |
| 3158 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3159 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3160 | return( ssl_tls12_session_load( session, p, remaining_len ) ); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3161 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3162 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3163 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3164 | case MBEDTLS_SSL_VERSION_TLS1_3: |
| 3165 | return( ssl_tls13_session_load( session, p, remaining_len ) ); |
| 3166 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 3167 | |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3168 | default: |
| 3169 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3170 | } |
| 3171 | } |
| 3172 | |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3173 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 3174 | * Deserialize session: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3175 | */ |
| 3176 | int mbedtls_ssl_session_load( mbedtls_ssl_session *session, |
| 3177 | const unsigned char *buf, |
| 3178 | size_t len ) |
| 3179 | { |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3180 | int ret = ssl_session_load( session, 0, buf, len ); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3181 | |
| 3182 | if( ret != 0 ) |
| 3183 | mbedtls_ssl_session_free( session ); |
| 3184 | |
| 3185 | return( ret ); |
| 3186 | } |
| 3187 | |
| 3188 | /* |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3189 | * Perform a single step of the SSL handshake |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3190 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3191 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3192 | static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl ) |
| 3193 | { |
| 3194 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 3195 | |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 3196 | /* |
| 3197 | * 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] | 3198 | * that were written into the output buffer by the previous handshake step, |
| 3199 | * if the write to the network callback returned with the |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 3200 | * #MBEDTLS_ERR_SSL_WANT_WRITE error code. |
| 3201 | * We proceed to the next handshake step only when all data from the |
| 3202 | * previous one have been sent to the peer, thus we make sure that this is |
| 3203 | * the case here by calling `mbedtls_ssl_flush_output()`. The function may |
| 3204 | * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case |
| 3205 | * we have to wait before to go ahead. |
| 3206 | * In the case of TLS 1.3, handshake step handlers do not send data to the |
| 3207 | * peer. Data are only sent here and through |
| 3208 | * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an |
Andrzej Kurek | 5c65c57 | 2022-04-13 14:28:52 -0400 | [diff] [blame] | 3209 | * alert occurred. |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 3210 | */ |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3211 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
| 3212 | return( ret ); |
| 3213 | |
| 3214 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3215 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 3216 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) |
| 3217 | { |
| 3218 | if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 3219 | return( ret ); |
| 3220 | } |
| 3221 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3222 | |
| 3223 | return( ret ); |
| 3224 | } |
| 3225 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3226 | int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3227 | { |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3228 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3229 | |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3230 | if( ssl == NULL || |
| 3231 | ssl->conf == NULL || |
| 3232 | ssl->handshake == NULL || |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3233 | mbedtls_ssl_is_handshake_over( ssl ) == 1 ) |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3234 | { |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3235 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3236 | } |
| 3237 | |
| 3238 | ret = ssl_prepare_handshake_step( ssl ); |
| 3239 | if( ret != 0 ) |
| 3240 | return( ret ); |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3241 | |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3242 | ret = mbedtls_ssl_handle_pending_alert( ssl ); |
| 3243 | if( ret != 0 ) |
| 3244 | goto cleanup; |
| 3245 | |
Tom Cosgrove | 2fdc7b3 | 2022-09-21 12:33:17 +0100 | [diff] [blame^] | 3246 | /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or |
| 3247 | * MBEDTLS_SSL_IS_SERVER, this is the return code we give */ |
| 3248 | ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3249 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3250 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3251 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3252 | { |
Ronald Cron | 27c85e7 | 2022-03-08 11:37:55 +0100 | [diff] [blame] | 3253 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s", |
| 3254 | mbedtls_ssl_states_str( ssl->state ) ) ); |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3255 | |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3256 | switch( ssl->state ) |
| 3257 | { |
| 3258 | case MBEDTLS_SSL_HELLO_REQUEST: |
| 3259 | ssl->state = MBEDTLS_SSL_CLIENT_HELLO; |
| 3260 | break; |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3261 | |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3262 | case MBEDTLS_SSL_CLIENT_HELLO: |
| 3263 | ret = mbedtls_ssl_write_client_hello( ssl ); |
| 3264 | break; |
| 3265 | |
| 3266 | default: |
| 3267 | #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] | 3268 | if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3269 | ret = mbedtls_ssl_tls13_handshake_client_step( ssl ); |
| 3270 | else |
| 3271 | ret = mbedtls_ssl_handshake_client_step( ssl ); |
| 3272 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3273 | ret = mbedtls_ssl_handshake_client_step( ssl ); |
| 3274 | #else |
| 3275 | ret = mbedtls_ssl_tls13_handshake_client_step( ssl ); |
| 3276 | #endif |
| 3277 | } |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3278 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3279 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3280 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3281 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3282 | { |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3283 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3284 | if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) ) |
Jerry Yu | 2756193 | 2021-08-27 17:07:38 +0800 | [diff] [blame] | 3285 | ret = mbedtls_ssl_tls13_handshake_server_step( ssl ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3286 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3287 | |
| 3288 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3289 | if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) ) |
| 3290 | ret = mbedtls_ssl_handshake_server_step( ssl ); |
| 3291 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3292 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3293 | #endif |
| 3294 | |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3295 | if( ret != 0 ) |
| 3296 | { |
Jerry Yu | bbd5a3f | 2021-09-18 20:50:22 +0800 | [diff] [blame] | 3297 | /* handshake_step return error. And it is same |
| 3298 | * with alert_reason. |
| 3299 | */ |
Jerry Yu | 3bf1f97 | 2021-09-22 21:37:18 +0800 | [diff] [blame] | 3300 | if( ssl->send_alert ) |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3301 | { |
Jerry Yu | 3bf1f97 | 2021-09-22 21:37:18 +0800 | [diff] [blame] | 3302 | ret = mbedtls_ssl_handle_pending_alert( ssl ); |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3303 | goto cleanup; |
| 3304 | } |
| 3305 | } |
| 3306 | |
| 3307 | cleanup: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3308 | return( ret ); |
| 3309 | } |
| 3310 | |
| 3311 | /* |
| 3312 | * Perform the SSL handshake |
| 3313 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3314 | int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3315 | { |
| 3316 | int ret = 0; |
| 3317 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3318 | /* Sanity checks */ |
| 3319 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3320 | if( ssl == NULL || ssl->conf == NULL ) |
| 3321 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3322 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3323 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3324 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 3325 | ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) ) |
| 3326 | { |
| 3327 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use " |
| 3328 | "mbedtls_ssl_set_timer_cb() for DTLS" ) ); |
| 3329 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3330 | } |
| 3331 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3332 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3333 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3334 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3335 | /* Main handshake loop */ |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3336 | while( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3337 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3338 | ret = mbedtls_ssl_handshake_step( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3339 | |
| 3340 | if( ret != 0 ) |
| 3341 | break; |
| 3342 | } |
| 3343 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3344 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3345 | |
| 3346 | return( ret ); |
| 3347 | } |
| 3348 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3349 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 3350 | #if defined(MBEDTLS_SSL_SRV_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3351 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3352 | * Write HelloRequest to request renegotiation on server |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3353 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3354 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3355 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3356 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3357 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3358 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3359 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3360 | |
| 3361 | ssl->out_msglen = 4; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3362 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 3363 | ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3364 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3365 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3366 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3367 | 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] | 3368 | return( ret ); |
| 3369 | } |
| 3370 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3371 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3372 | |
| 3373 | return( 0 ); |
| 3374 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3375 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3376 | |
| 3377 | /* |
| 3378 | * Actually renegotiate current connection, triggered by either: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3379 | * - any side: calling mbedtls_ssl_renegotiate(), |
| 3380 | * - client: receiving a HelloRequest during mbedtls_ssl_read(), |
| 3381 | * - 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] | 3382 | * the initial handshake is completed. |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3383 | * 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] | 3384 | * 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] | 3385 | */ |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 3386 | int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3387 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3388 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3389 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3390 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3391 | |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3392 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 3393 | return( ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3394 | |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 3395 | /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and |
| 3396 | * the ServerHello will have message_seq = 1" */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3397 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3398 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3399 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 3400 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3401 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 3402 | ssl->handshake->out_msg_seq = 1; |
| 3403 | else |
| 3404 | ssl->handshake->in_msg_seq = 1; |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 3405 | } |
| 3406 | #endif |
| 3407 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3408 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 3409 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3410 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3411 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3412 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3413 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3414 | return( ret ); |
| 3415 | } |
| 3416 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3417 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3418 | |
| 3419 | return( 0 ); |
| 3420 | } |
| 3421 | |
| 3422 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3423 | * Renegotiate current connection on client, |
| 3424 | * or request renegotiation on server |
| 3425 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3426 | int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3427 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3428 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3429 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3430 | if( ssl == NULL || ssl->conf == NULL ) |
| 3431 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3432 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3433 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3434 | /* On server, just send the request */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3435 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3436 | { |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3437 | if( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3438 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3439 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3440 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 3441 | |
| 3442 | /* Did we already try/start sending HelloRequest? */ |
| 3443 | if( ssl->out_left != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3444 | return( mbedtls_ssl_flush_output( ssl ) ); |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 3445 | |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3446 | return( ssl_write_hello_request( ssl ) ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3447 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3448 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3449 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3450 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3451 | /* |
| 3452 | * On client, either start the renegotiation process or, |
| 3453 | * if already in progress, continue the handshake |
| 3454 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3455 | if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3456 | { |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3457 | if( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3458 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3459 | |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 3460 | if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3461 | { |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 3462 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3463 | return( ret ); |
| 3464 | } |
| 3465 | } |
| 3466 | else |
| 3467 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3468 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3469 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3470 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3471 | return( ret ); |
| 3472 | } |
| 3473 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3474 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3475 | |
Paul Bakker | 37ce0ff | 2013-10-31 14:32:04 +0100 | [diff] [blame] | 3476 | return( ret ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3477 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3478 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3479 | |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 3480 | void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3481 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 3482 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
| 3483 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3484 | if( handshake == NULL ) |
| 3485 | return; |
| 3486 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3487 | #if defined(MBEDTLS_ECP_C) |
| 3488 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 3489 | if ( ssl->handshake->group_list_heap_allocated ) |
| 3490 | mbedtls_free( (void*) handshake->group_list ); |
| 3491 | handshake->group_list = NULL; |
| 3492 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 3493 | #endif /* MBEDTLS_ECP_C */ |
| 3494 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3495 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 3496 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 3497 | if ( ssl->handshake->sig_algs_heap_allocated ) |
| 3498 | mbedtls_free( (void*) handshake->sig_algs ); |
| 3499 | handshake->sig_algs = NULL; |
| 3500 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Xiaofei Bai | c234ecf | 2022-02-08 09:59:23 +0000 | [diff] [blame] | 3501 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3502 | if( ssl->handshake->certificate_request_context ) |
| 3503 | { |
| 3504 | mbedtls_free( (void*) handshake->certificate_request_context ); |
| 3505 | } |
| 3506 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3507 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 3508 | |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3509 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
| 3510 | if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 ) |
| 3511 | { |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 3512 | ssl->conf->f_async_cancel( ssl ); |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3513 | handshake->async_in_progress = 0; |
| 3514 | } |
| 3515 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
| 3516 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 3517 | #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] | 3518 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3519 | psa_hash_abort( &handshake->fin_sha256_psa ); |
| 3520 | #else |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 3521 | mbedtls_sha256_free( &handshake->fin_sha256 ); |
| 3522 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3523 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 3524 | #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] | 3525 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 3526 | psa_hash_abort( &handshake->fin_sha384_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3527 | #else |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 3528 | mbedtls_sha512_free( &handshake->fin_sha384 ); |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 3529 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3530 | #endif |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 3531 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3532 | #if defined(MBEDTLS_DHM_C) |
| 3533 | mbedtls_dhm_free( &handshake->dhm_ctx ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3534 | #endif |
Neil Armstrong | f3f4641 | 2022-04-12 14:43:39 +0200 | [diff] [blame] | 3535 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3536 | mbedtls_ecdh_free( &handshake->ecdh_ctx ); |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 3537 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 3538 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 3539 | mbedtls_ecjpake_free( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 3540 | #if defined(MBEDTLS_SSL_CLI_C) |
| 3541 | mbedtls_free( handshake->ecjpake_cache ); |
| 3542 | handshake->ecjpake_cache = NULL; |
| 3543 | handshake->ecjpake_cache_len = 0; |
| 3544 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 3545 | #endif |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 3546 | |
Janos Follath | 4ae5c29 | 2016-02-10 11:27:43 +0000 | [diff] [blame] | 3547 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 3548 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 3549 | /* explicit void pointer cast for buggy MS compiler */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3550 | mbedtls_free( (void *) handshake->curves ); |
Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 3551 | #endif |
| 3552 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3553 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 3554 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3555 | if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) ) |
| 3556 | { |
| 3557 | /* The maintenance of the external PSK key slot is the |
| 3558 | * user's responsibility. */ |
| 3559 | if( ssl->handshake->psk_opaque_is_internal ) |
| 3560 | { |
| 3561 | psa_destroy_key( ssl->handshake->psk_opaque ); |
| 3562 | ssl->handshake->psk_opaque_is_internal = 0; |
| 3563 | } |
| 3564 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
| 3565 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 3566 | #else |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3567 | if( handshake->psk != NULL ) |
| 3568 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3569 | mbedtls_platform_zeroize( handshake->psk, handshake->psk_len ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3570 | mbedtls_free( handshake->psk ); |
| 3571 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 3572 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3573 | #endif |
| 3574 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3575 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 3576 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 3577 | /* |
| 3578 | * Free only the linked list wrapper, not the keys themselves |
| 3579 | * since the belong to the SNI callback |
| 3580 | */ |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 3581 | ssl_key_cert_free( handshake->sni_key_cert ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3582 | #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] | 3583 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3584 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 3585 | mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx ); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 3586 | if( handshake->ecrs_peer_cert != NULL ) |
| 3587 | { |
| 3588 | mbedtls_x509_crt_free( handshake->ecrs_peer_cert ); |
| 3589 | mbedtls_free( handshake->ecrs_peer_cert ); |
| 3590 | } |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 3591 | #endif |
| 3592 | |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 3593 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 3594 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 3595 | mbedtls_pk_free( &handshake->peer_pubkey ); |
| 3596 | #endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 3597 | |
XiaokangQian | 9b93c0d | 2022-02-09 06:02:25 +0000 | [diff] [blame] | 3598 | #if defined(MBEDTLS_SSL_CLI_C) && \ |
| 3599 | ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) ) |
| 3600 | mbedtls_free( handshake->cookie ); |
| 3601 | #endif /* MBEDTLS_SSL_CLI_C && |
| 3602 | ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */ |
XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 3603 | |
| 3604 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 533ab5f | 2020-02-05 10:49:13 +0000 | [diff] [blame] | 3605 | mbedtls_ssl_flight_free( handshake->flight ); |
| 3606 | mbedtls_ssl_buffering_free( ssl ); |
XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 3607 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 3608 | |
Ronald Cron | f12b81d | 2022-03-15 10:42:41 +0100 | [diff] [blame] | 3609 | #if defined(MBEDTLS_ECDH_C) && \ |
| 3610 | ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) ) |
Neil Armstrong | f716a70 | 2022-04-04 11:23:46 +0200 | [diff] [blame] | 3611 | if( handshake->ecdh_psa_privkey_is_external == 0 ) |
Neil Armstrong | 8113d25 | 2022-03-23 10:57:04 +0100 | [diff] [blame] | 3612 | psa_destroy_key( handshake->ecdh_psa_privkey ); |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 3613 | #endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */ |
| 3614 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3615 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | a1a568c | 2021-11-09 10:17:21 +0800 | [diff] [blame] | 3616 | mbedtls_ssl_transform_free( handshake->transform_handshake ); |
| 3617 | mbedtls_ssl_transform_free( handshake->transform_earlydata ); |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 3618 | mbedtls_free( handshake->transform_earlydata ); |
| 3619 | mbedtls_free( handshake->transform_handshake ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3620 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 3621 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3622 | |
| 3623 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 3624 | /* If the buffers are too big - reallocate. Because of the way Mbed TLS |
| 3625 | * processes datagrams and the fact that a datagram is allowed to have |
| 3626 | * several records in it, it is possible that the I/O buffers are not |
| 3627 | * empty at this stage */ |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 3628 | handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ), |
| 3629 | mbedtls_ssl_get_output_buflen( ssl ) ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3630 | #endif |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 3631 | |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 3632 | /* mbedtls_platform_zeroize MUST be last one in this function */ |
| 3633 | mbedtls_platform_zeroize( handshake, |
| 3634 | sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3635 | } |
| 3636 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3637 | void mbedtls_ssl_session_free( mbedtls_ssl_session *session ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3638 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3639 | if( session == NULL ) |
| 3640 | return; |
| 3641 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3642 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 1294a0b | 2019-02-05 12:38:15 +0000 | [diff] [blame] | 3643 | ssl_clear_peer_cert( session ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 3644 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 3645 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 3646 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3647 | mbedtls_free( session->ticket ); |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 3648 | #endif |
Manuel Pégourié-Gonnard | 75d4401 | 2013-08-02 14:44:04 +0200 | [diff] [blame] | 3649 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3650 | mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3651 | } |
| 3652 | |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 3653 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3654 | |
| 3655 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 3656 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u |
| 3657 | #else |
| 3658 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u |
| 3659 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 3660 | |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3661 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3662 | |
| 3663 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 3664 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u |
| 3665 | #else |
| 3666 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u |
| 3667 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 3668 | |
| 3669 | #if defined(MBEDTLS_SSL_ALPN) |
| 3670 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u |
| 3671 | #else |
| 3672 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u |
| 3673 | #endif /* MBEDTLS_SSL_ALPN */ |
| 3674 | |
| 3675 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0 |
| 3676 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1 |
| 3677 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2 |
| 3678 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3 |
| 3679 | |
| 3680 | #define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \ |
| 3681 | ( (uint32_t) ( \ |
| 3682 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \ |
| 3683 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \ |
| 3684 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \ |
| 3685 | ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \ |
| 3686 | 0u ) ) |
| 3687 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3688 | static unsigned char ssl_serialized_context_header[] = { |
| 3689 | MBEDTLS_VERSION_MAJOR, |
| 3690 | MBEDTLS_VERSION_MINOR, |
| 3691 | MBEDTLS_VERSION_PATCH, |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 3692 | MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 3693 | MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 3694 | MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
| 3695 | MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
| 3696 | MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3697 | }; |
| 3698 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3699 | /* |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3700 | * Serialize a full SSL context |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 3701 | * |
| 3702 | * The format of the serialized data is: |
| 3703 | * (in the presentation language of TLS, RFC 8446 section 3) |
| 3704 | * |
| 3705 | * // header |
| 3706 | * opaque mbedtls_version[3]; // major, minor, patch |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3707 | * opaque context_format[5]; // version-specific field determining |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 3708 | * // the format of the remaining |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3709 | * // serialized data. |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3710 | * Note: When updating the format, remember to keep these |
| 3711 | * 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] | 3712 | * |
| 3713 | * // session sub-structure |
| 3714 | * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save() |
| 3715 | * // transform sub-structure |
| 3716 | * uint8 random[64]; // ServerHello.random+ClientHello.random |
| 3717 | * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value |
| 3718 | * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use |
| 3719 | * // fields from ssl_context |
| 3720 | * uint32 badmac_seen; // DTLS: number of records with failing MAC |
| 3721 | * uint64 in_window_top; // DTLS: last validated record seq_num |
| 3722 | * uint64 in_window; // DTLS: bitmask for replay protection |
| 3723 | * uint8 disable_datagram_packing; // DTLS: only one record per datagram |
| 3724 | * uint64 cur_out_ctr; // Record layer: outgoing sequence number |
| 3725 | * uint16 mtu; // DTLS: path mtu (max outgoing fragment size) |
| 3726 | * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol |
| 3727 | * |
| 3728 | * Note that many fields of the ssl_context or sub-structures are not |
| 3729 | * serialized, as they fall in one of the following categories: |
| 3730 | * |
| 3731 | * 1. forced value (eg in_left must be 0) |
| 3732 | * 2. pointer to dynamically-allocated memory (eg session, transform) |
| 3733 | * 3. value can be re-derived from other data (eg session keys from MS) |
| 3734 | * 4. value was temporary (eg content of input buffer) |
| 3735 | * 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] | 3736 | */ |
| 3737 | int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl, |
| 3738 | unsigned char *buf, |
| 3739 | size_t buf_len, |
| 3740 | size_t *olen ) |
| 3741 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3742 | unsigned char *p = buf; |
| 3743 | size_t used = 0; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3744 | size_t session_len; |
| 3745 | int ret = 0; |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3746 | |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 3747 | /* |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3748 | * Enforce usage restrictions, see "return BAD_INPUT_DATA" in |
| 3749 | * this function's documentation. |
| 3750 | * |
| 3751 | * These are due to assumptions/limitations in the implementation. Some of |
| 3752 | * them are likely to stay (no handshake in progress) some might go away |
| 3753 | * (only DTLS) but are currently used to simplify the implementation. |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 3754 | */ |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3755 | /* The initial handshake must be over */ |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3756 | if( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3757 | { |
| 3758 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) ); |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 3759 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3760 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3761 | if( ssl->handshake != NULL ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3762 | { |
| 3763 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3764 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3765 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3766 | /* Double-check that sub-structures are indeed ready */ |
| 3767 | if( ssl->transform == NULL || ssl->session == NULL ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3768 | { |
| 3769 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3770 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3771 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3772 | /* There must be no pending incoming or outgoing data */ |
| 3773 | if( mbedtls_ssl_check_pending( ssl ) != 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3774 | { |
| 3775 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3776 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3777 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3778 | if( ssl->out_left != 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3779 | { |
| 3780 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3781 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3782 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3783 | /* Protocol must be DLTS, not TLS */ |
| 3784 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3785 | { |
| 3786 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3787 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3788 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3789 | /* Version must be 1.2 */ |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3790 | if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3791 | { |
| 3792 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3793 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3794 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3795 | /* We must be using an AEAD ciphersuite */ |
| 3796 | if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3797 | { |
| 3798 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3799 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3800 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3801 | /* Renegotiation must not be enabled */ |
| 3802 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 3803 | if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3804 | { |
| 3805 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3806 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3807 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3808 | #endif |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3809 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3810 | /* |
| 3811 | * Version and format identifier |
| 3812 | */ |
| 3813 | used += sizeof( ssl_serialized_context_header ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3814 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3815 | if( used <= buf_len ) |
| 3816 | { |
| 3817 | memcpy( p, ssl_serialized_context_header, |
| 3818 | sizeof( ssl_serialized_context_header ) ); |
| 3819 | p += sizeof( ssl_serialized_context_header ); |
| 3820 | } |
| 3821 | |
| 3822 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3823 | * Session (length + data) |
| 3824 | */ |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3825 | 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] | 3826 | if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ) |
| 3827 | return( ret ); |
| 3828 | |
| 3829 | used += 4 + session_len; |
| 3830 | if( used <= buf_len ) |
| 3831 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3832 | MBEDTLS_PUT_UINT32_BE( session_len, p, 0 ); |
| 3833 | p += 4; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3834 | |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3835 | ret = ssl_session_save( ssl->session, 1, |
| 3836 | p, session_len, &session_len ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3837 | if( ret != 0 ) |
| 3838 | return( ret ); |
| 3839 | |
| 3840 | p += session_len; |
| 3841 | } |
| 3842 | |
| 3843 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3844 | * Transform |
| 3845 | */ |
| 3846 | used += sizeof( ssl->transform->randbytes ); |
| 3847 | if( used <= buf_len ) |
| 3848 | { |
| 3849 | memcpy( p, ssl->transform->randbytes, |
| 3850 | sizeof( ssl->transform->randbytes ) ); |
| 3851 | p += sizeof( ssl->transform->randbytes ); |
| 3852 | } |
| 3853 | |
| 3854 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 3855 | used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len; |
| 3856 | if( used <= buf_len ) |
| 3857 | { |
| 3858 | *p++ = ssl->transform->in_cid_len; |
| 3859 | memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len ); |
| 3860 | p += ssl->transform->in_cid_len; |
| 3861 | |
| 3862 | *p++ = ssl->transform->out_cid_len; |
| 3863 | memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len ); |
| 3864 | p += ssl->transform->out_cid_len; |
| 3865 | } |
| 3866 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 3867 | |
| 3868 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3869 | * Saved fields from top-level ssl_context structure |
| 3870 | */ |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3871 | used += 4; |
| 3872 | if( used <= buf_len ) |
| 3873 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3874 | MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 ); |
| 3875 | p += 4; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3876 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3877 | |
| 3878 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 3879 | used += 16; |
| 3880 | if( used <= buf_len ) |
| 3881 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3882 | MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 ); |
| 3883 | p += 8; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3884 | |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3885 | MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 ); |
| 3886 | p += 8; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3887 | } |
| 3888 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 3889 | |
| 3890 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3891 | used += 1; |
| 3892 | if( used <= buf_len ) |
| 3893 | { |
| 3894 | *p++ = ssl->disable_datagram_packing; |
| 3895 | } |
| 3896 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3897 | |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 3898 | used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3899 | if( used <= buf_len ) |
| 3900 | { |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 3901 | memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN ); |
| 3902 | p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3903 | } |
| 3904 | |
| 3905 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3906 | used += 2; |
| 3907 | if( used <= buf_len ) |
| 3908 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3909 | MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 ); |
| 3910 | p += 2; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3911 | } |
| 3912 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3913 | |
| 3914 | #if defined(MBEDTLS_SSL_ALPN) |
| 3915 | { |
| 3916 | const uint8_t alpn_len = ssl->alpn_chosen |
Manuel Pégourié-Gonnard | f041f4e | 2019-07-24 00:58:27 +0200 | [diff] [blame] | 3917 | ? (uint8_t) strlen( ssl->alpn_chosen ) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3918 | : 0; |
| 3919 | |
| 3920 | used += 1 + alpn_len; |
| 3921 | if( used <= buf_len ) |
| 3922 | { |
| 3923 | *p++ = alpn_len; |
| 3924 | |
| 3925 | if( ssl->alpn_chosen != NULL ) |
| 3926 | { |
| 3927 | memcpy( p, ssl->alpn_chosen, alpn_len ); |
| 3928 | p += alpn_len; |
| 3929 | } |
| 3930 | } |
| 3931 | } |
| 3932 | #endif /* MBEDTLS_SSL_ALPN */ |
| 3933 | |
| 3934 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3935 | * Done |
| 3936 | */ |
| 3937 | *olen = used; |
| 3938 | |
| 3939 | if( used > buf_len ) |
| 3940 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3941 | |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3942 | MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used ); |
| 3943 | |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 3944 | return( mbedtls_ssl_session_reset_int( ssl, 0 ) ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3945 | } |
| 3946 | |
| 3947 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 3948 | * Deserialize context, see mbedtls_ssl_context_save() for format. |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3949 | * |
| 3950 | * This internal version is wrapped by a public function that cleans up in |
| 3951 | * case of error. |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3952 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3953 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3954 | static int ssl_context_load( mbedtls_ssl_context *ssl, |
| 3955 | const unsigned char *buf, |
| 3956 | size_t len ) |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3957 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3958 | const unsigned char *p = buf; |
| 3959 | const unsigned char * const end = buf + len; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3960 | size_t session_len; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3961 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3962 | |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 3963 | /* |
| 3964 | * The context should have been freshly setup or reset. |
| 3965 | * Give the user an error in case of obvious misuse. |
Manuel Pégourié-Gonnard | 4ca930f | 2019-07-26 16:31:53 +0200 | [diff] [blame] | 3966 | * (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] | 3967 | * renegotiating, or if the user mistakenly loaded a session first.) |
| 3968 | */ |
| 3969 | if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST || |
| 3970 | ssl->session != NULL ) |
| 3971 | { |
| 3972 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3973 | } |
| 3974 | |
| 3975 | /* |
| 3976 | * We can't check that the config matches the initial one, but we can at |
| 3977 | * least check it matches the requirements for serializing. |
| 3978 | */ |
| 3979 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 3980 | ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 || |
| 3981 | ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 || |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 3982 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 3983 | ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED || |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 3984 | #endif |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 3985 | 0 ) |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 3986 | { |
| 3987 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3988 | } |
| 3989 | |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3990 | MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len ); |
| 3991 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3992 | /* |
| 3993 | * Check version identifier |
| 3994 | */ |
| 3995 | if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) ) |
| 3996 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3997 | |
| 3998 | if( memcmp( p, ssl_serialized_context_header, |
| 3999 | sizeof( ssl_serialized_context_header ) ) != 0 ) |
| 4000 | { |
| 4001 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
| 4002 | } |
| 4003 | p += sizeof( ssl_serialized_context_header ); |
| 4004 | |
| 4005 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4006 | * Session |
| 4007 | */ |
| 4008 | if( (size_t)( end - p ) < 4 ) |
| 4009 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4010 | |
| 4011 | session_len = ( (size_t) p[0] << 24 ) | |
| 4012 | ( (size_t) p[1] << 16 ) | |
| 4013 | ( (size_t) p[2] << 8 ) | |
| 4014 | ( (size_t) p[3] ); |
| 4015 | p += 4; |
| 4016 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4017 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 4018 | * 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] | 4019 | ssl->session = ssl->session_negotiate; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4020 | ssl->session_in = ssl->session; |
| 4021 | ssl->session_out = ssl->session; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4022 | ssl->session_negotiate = NULL; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4023 | |
| 4024 | if( (size_t)( end - p ) < session_len ) |
| 4025 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4026 | |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4027 | ret = ssl_session_load( ssl->session, 1, p, session_len ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4028 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4029 | { |
| 4030 | mbedtls_ssl_session_free( ssl->session ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4031 | return( ret ); |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4032 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4033 | |
| 4034 | p += session_len; |
| 4035 | |
| 4036 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4037 | * Transform |
| 4038 | */ |
| 4039 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4040 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 4041 | * 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] | 4042 | ssl->transform = ssl->transform_negotiate; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4043 | ssl->transform_in = ssl->transform; |
| 4044 | ssl->transform_out = ssl->transform; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4045 | ssl->transform_negotiate = NULL; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4046 | |
| 4047 | /* Read random bytes and populate structure */ |
| 4048 | if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) ) |
| 4049 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jerry Yu | 840fbb2 | 2022-02-17 14:59:29 +0800 | [diff] [blame] | 4050 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 4051 | ret = ssl_tls12_populate_transform( ssl->transform, |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4052 | ssl->session->ciphersuite, |
| 4053 | ssl->session->master, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 4054 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4055 | ssl->session->encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 4056 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4057 | ssl_tls12prf_from_cs( ssl->session->ciphersuite ), |
| 4058 | p, /* currently pointing to randbytes */ |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 4059 | 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] | 4060 | ssl->conf->endpoint, |
| 4061 | ssl ); |
| 4062 | if( ret != 0 ) |
| 4063 | return( ret ); |
Jerry Yu | 840fbb2 | 2022-02-17 14:59:29 +0800 | [diff] [blame] | 4064 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4065 | p += sizeof( ssl->transform->randbytes ); |
| 4066 | |
| 4067 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 4068 | /* Read connection IDs and store them */ |
| 4069 | if( (size_t)( end - p ) < 1 ) |
| 4070 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4071 | |
| 4072 | ssl->transform->in_cid_len = *p++; |
| 4073 | |
Manuel Pégourié-Gonnard | 5ea13b8 | 2019-07-23 15:02:54 +0200 | [diff] [blame] | 4074 | 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] | 4075 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4076 | |
| 4077 | memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len ); |
| 4078 | p += ssl->transform->in_cid_len; |
| 4079 | |
| 4080 | ssl->transform->out_cid_len = *p++; |
| 4081 | |
| 4082 | if( (size_t)( end - p ) < ssl->transform->out_cid_len ) |
| 4083 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4084 | |
| 4085 | memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len ); |
| 4086 | p += ssl->transform->out_cid_len; |
| 4087 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 4088 | |
| 4089 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4090 | * Saved fields from top-level ssl_context structure |
| 4091 | */ |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4092 | if( (size_t)( end - p ) < 4 ) |
| 4093 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4094 | |
| 4095 | ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) | |
| 4096 | ( (uint32_t) p[1] << 16 ) | |
| 4097 | ( (uint32_t) p[2] << 8 ) | |
| 4098 | ( (uint32_t) p[3] ); |
| 4099 | p += 4; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4100 | |
| 4101 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 4102 | if( (size_t)( end - p ) < 16 ) |
| 4103 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4104 | |
| 4105 | ssl->in_window_top = ( (uint64_t) p[0] << 56 ) | |
| 4106 | ( (uint64_t) p[1] << 48 ) | |
| 4107 | ( (uint64_t) p[2] << 40 ) | |
| 4108 | ( (uint64_t) p[3] << 32 ) | |
| 4109 | ( (uint64_t) p[4] << 24 ) | |
| 4110 | ( (uint64_t) p[5] << 16 ) | |
| 4111 | ( (uint64_t) p[6] << 8 ) | |
| 4112 | ( (uint64_t) p[7] ); |
| 4113 | p += 8; |
| 4114 | |
| 4115 | ssl->in_window = ( (uint64_t) p[0] << 56 ) | |
| 4116 | ( (uint64_t) p[1] << 48 ) | |
| 4117 | ( (uint64_t) p[2] << 40 ) | |
| 4118 | ( (uint64_t) p[3] << 32 ) | |
| 4119 | ( (uint64_t) p[4] << 24 ) | |
| 4120 | ( (uint64_t) p[5] << 16 ) | |
| 4121 | ( (uint64_t) p[6] << 8 ) | |
| 4122 | ( (uint64_t) p[7] ); |
| 4123 | p += 8; |
| 4124 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 4125 | |
| 4126 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4127 | if( (size_t)( end - p ) < 1 ) |
| 4128 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4129 | |
| 4130 | ssl->disable_datagram_packing = *p++; |
| 4131 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4132 | |
Jerry Yu | d9a94fe | 2021-09-28 18:58:59 +0800 | [diff] [blame] | 4133 | if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) ) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4134 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jerry Yu | d9a94fe | 2021-09-28 18:58:59 +0800 | [diff] [blame] | 4135 | memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) ); |
| 4136 | p += sizeof( ssl->cur_out_ctr ); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4137 | |
| 4138 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4139 | if( (size_t)( end - p ) < 2 ) |
| 4140 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4141 | |
| 4142 | ssl->mtu = ( p[0] << 8 ) | p[1]; |
| 4143 | p += 2; |
| 4144 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4145 | |
| 4146 | #if defined(MBEDTLS_SSL_ALPN) |
| 4147 | { |
| 4148 | uint8_t alpn_len; |
| 4149 | const char **cur; |
| 4150 | |
| 4151 | if( (size_t)( end - p ) < 1 ) |
| 4152 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4153 | |
| 4154 | alpn_len = *p++; |
| 4155 | |
| 4156 | if( alpn_len != 0 && ssl->conf->alpn_list != NULL ) |
| 4157 | { |
| 4158 | /* alpn_chosen should point to an item in the configured list */ |
| 4159 | for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ ) |
| 4160 | { |
| 4161 | if( strlen( *cur ) == alpn_len && |
| 4162 | memcmp( p, cur, alpn_len ) == 0 ) |
| 4163 | { |
| 4164 | ssl->alpn_chosen = *cur; |
| 4165 | break; |
| 4166 | } |
| 4167 | } |
| 4168 | } |
| 4169 | |
| 4170 | /* can only happen on conf mismatch */ |
| 4171 | if( alpn_len != 0 && ssl->alpn_chosen == NULL ) |
| 4172 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4173 | |
| 4174 | p += alpn_len; |
| 4175 | } |
| 4176 | #endif /* MBEDTLS_SSL_ALPN */ |
| 4177 | |
| 4178 | /* |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4179 | * Forced fields from top-level ssl_context structure |
| 4180 | * |
| 4181 | * Most of them already set to the correct value by mbedtls_ssl_init() and |
| 4182 | * mbedtls_ssl_reset(), so we only need to set the remaining ones. |
| 4183 | */ |
| 4184 | ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER; |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 4185 | ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4186 | |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 4187 | /* Adjust pointers for header fields of outgoing records to |
| 4188 | * the given transform, accounting for explicit IV and CID. */ |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 4189 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform ); |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 4190 | |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4191 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4192 | ssl->in_epoch = 1; |
| 4193 | #endif |
| 4194 | |
| 4195 | /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated, |
| 4196 | * 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] | 4197 | * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform() |
| 4198 | * inappropriately. */ |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4199 | if( ssl->handshake != NULL ) |
| 4200 | { |
| 4201 | mbedtls_ssl_handshake_free( ssl ); |
| 4202 | mbedtls_free( ssl->handshake ); |
| 4203 | ssl->handshake = NULL; |
| 4204 | } |
| 4205 | |
| 4206 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4207 | * Done - should have consumed entire buffer |
| 4208 | */ |
| 4209 | if( p != end ) |
| 4210 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4211 | |
| 4212 | return( 0 ); |
| 4213 | } |
| 4214 | |
| 4215 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 4216 | * Deserialize context: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4217 | */ |
| 4218 | int mbedtls_ssl_context_load( mbedtls_ssl_context *context, |
| 4219 | const unsigned char *buf, |
| 4220 | size_t len ) |
| 4221 | { |
| 4222 | int ret = ssl_context_load( context, buf, len ); |
| 4223 | |
| 4224 | if( ret != 0 ) |
| 4225 | mbedtls_ssl_free( context ); |
| 4226 | |
| 4227 | return( ret ); |
| 4228 | } |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 4229 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4230 | |
| 4231 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4232 | * Free an SSL context |
| 4233 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4234 | void mbedtls_ssl_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4235 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4236 | if( ssl == NULL ) |
| 4237 | return; |
| 4238 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4239 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4240 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 4241 | if( ssl->out_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4242 | { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 4243 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 4244 | size_t out_buf_len = ssl->out_buf_len; |
| 4245 | #else |
| 4246 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 4247 | #endif |
| 4248 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 4249 | mbedtls_platform_zeroize( ssl->out_buf, out_buf_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4250 | mbedtls_free( ssl->out_buf ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4251 | ssl->out_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4252 | } |
| 4253 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 4254 | if( ssl->in_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4255 | { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 4256 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 4257 | size_t in_buf_len = ssl->in_buf_len; |
| 4258 | #else |
| 4259 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 4260 | #endif |
| 4261 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 4262 | mbedtls_platform_zeroize( ssl->in_buf, in_buf_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4263 | mbedtls_free( ssl->in_buf ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4264 | ssl->in_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4265 | } |
| 4266 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4267 | if( ssl->transform ) |
| 4268 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4269 | mbedtls_ssl_transform_free( ssl->transform ); |
| 4270 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4271 | } |
| 4272 | |
| 4273 | if( ssl->handshake ) |
| 4274 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 4275 | mbedtls_ssl_handshake_free( ssl ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4276 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
| 4277 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4278 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4279 | mbedtls_free( ssl->handshake ); |
| 4280 | mbedtls_free( ssl->transform_negotiate ); |
| 4281 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4282 | } |
| 4283 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4284 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 4285 | mbedtls_ssl_transform_free( ssl->transform_application ); |
| 4286 | mbedtls_free( ssl->transform_application ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4287 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 4288 | |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 4289 | if( ssl->session ) |
| 4290 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4291 | mbedtls_ssl_session_free( ssl->session ); |
| 4292 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 4293 | } |
| 4294 | |
Manuel Pégourié-Gonnard | 55fab2d | 2015-05-11 16:15:19 +0200 | [diff] [blame] | 4295 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 4296 | if( ssl->hostname != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4297 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4298 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4299 | mbedtls_free( ssl->hostname ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4300 | } |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 4301 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4302 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 4303 | #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] | 4304 | mbedtls_free( ssl->cli_id ); |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 4305 | #endif |
| 4306 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4307 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) ); |
Paul Bakker | 2da561c | 2009-02-05 18:00:28 +0000 | [diff] [blame] | 4308 | |
Paul Bakker | 86f04f4 | 2013-02-14 11:20:09 +0100 | [diff] [blame] | 4309 | /* Actually clear after last debug message */ |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4310 | mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4311 | } |
| 4312 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4313 | /* |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 4314 | * Initialize mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4315 | */ |
| 4316 | void mbedtls_ssl_config_init( mbedtls_ssl_config *conf ) |
| 4317 | { |
| 4318 | memset( conf, 0, sizeof( mbedtls_ssl_config ) ); |
| 4319 | } |
| 4320 | |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4321 | /* The selection should be the same as mbedtls_x509_crt_profile_default in |
| 4322 | * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters: |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4323 | * curves with a lower resource usage come first. |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4324 | * See the documentation of mbedtls_ssl_conf_curves() for what we promise |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4325 | * about this list. |
| 4326 | */ |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4327 | static uint16_t ssl_preset_default_groups[] = { |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4328 | #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4329 | MBEDTLS_SSL_IANA_TLS_GROUP_X25519, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4330 | #endif |
| 4331 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4332 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4333 | #endif |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4334 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4335 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4336 | #endif |
| 4337 | #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4338 | MBEDTLS_SSL_IANA_TLS_GROUP_X448, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4339 | #endif |
| 4340 | #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4341 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4342 | #endif |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4343 | #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4344 | MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4345 | #endif |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4346 | #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4347 | MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4348 | #endif |
| 4349 | #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4350 | MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4351 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4352 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4353 | }; |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4354 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4355 | static int ssl_preset_suiteb_ciphersuites[] = { |
| 4356 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
| 4357 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
| 4358 | 0 |
| 4359 | }; |
| 4360 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4361 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 4362 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4363 | /* NOTICE: |
Jerry Yu | 0b994b8 | 2022-01-25 17:22:12 +0800 | [diff] [blame] | 4364 | * 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] | 4365 | * rules SHOULD be upheld. |
| 4366 | * - No duplicate entries. |
| 4367 | * - 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] | 4368 | * - ssl_tls12_preset* is for TLS 1.2 use only. |
Jerry Yu | 370e146 | 2022-01-25 10:36:53 +0800 | [diff] [blame] | 4369 | * - 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] | 4370 | */ |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 4371 | static uint16_t ssl_preset_default_sig_algs[] = { |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4372 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4373 | #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] | 4374 | defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
| 4375 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4376 | #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] | 4377 | MBEDTLS_ECP_DP_SECP256R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4378 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4379 | #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] | 4380 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
| 4381 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4382 | #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] | 4383 | MBEDTLS_ECP_DP_SECP384R1_ENABLED */ |
| 4384 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4385 | #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] | 4386 | defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
| 4387 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4388 | #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] | 4389 | MBEDTLS_ECP_DP_SECP521R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4390 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4391 | #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] | 4392 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512, |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4393 | #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] | 4394 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4395 | #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] | 4396 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384, |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4397 | #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] | 4398 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4399 | #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] | 4400 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4401 | #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] | 4402 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4403 | #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] | 4404 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512, |
| 4405 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */ |
| 4406 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4407 | #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] | 4408 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384, |
| 4409 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */ |
| 4410 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4411 | #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] | 4412 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, |
| 4413 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */ |
| 4414 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 4415 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4416 | }; |
| 4417 | |
| 4418 | /* NOTICE: see above */ |
| 4419 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4420 | static uint16_t ssl_tls12_preset_default_sig_algs[] = { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4421 | #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] | 4422 | #if defined(MBEDTLS_ECDSA_C) |
| 4423 | 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] | 4424 | #endif |
Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 4425 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 4426 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512, |
| 4427 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4428 | #if defined(MBEDTLS_RSA_C) |
| 4429 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ), |
| 4430 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4431 | #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] | 4432 | #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] | 4433 | #if defined(MBEDTLS_ECDSA_C) |
| 4434 | 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] | 4435 | #endif |
Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 4436 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 4437 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384, |
| 4438 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4439 | #if defined(MBEDTLS_RSA_C) |
| 4440 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ), |
| 4441 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4442 | #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] | 4443 | #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] | 4444 | #if defined(MBEDTLS_ECDSA_C) |
| 4445 | 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] | 4446 | #endif |
Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 4447 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 4448 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
| 4449 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4450 | #if defined(MBEDTLS_RSA_C) |
| 4451 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ), |
| 4452 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4453 | #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] | 4454 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4455 | }; |
| 4456 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4457 | /* NOTICE: see above */ |
| 4458 | static uint16_t ssl_preset_suiteb_sig_algs[] = { |
| 4459 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4460 | #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] | 4461 | defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
| 4462 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4463 | #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] | 4464 | MBEDTLS_ECP_DP_SECP256R1_ENABLED */ |
| 4465 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4466 | #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] | 4467 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
| 4468 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4469 | #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] | 4470 | MBEDTLS_ECP_DP_SECP384R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4471 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4472 | #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] | 4473 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4474 | #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] | 4475 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4476 | #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] | 4477 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4478 | #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] | 4479 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 4480 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 4481 | }; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 4482 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4483 | /* NOTICE: see above */ |
| 4484 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4485 | static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4486 | #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] | 4487 | #if defined(MBEDTLS_ECDSA_C) |
| 4488 | 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] | 4489 | #endif |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4490 | #if defined(MBEDTLS_RSA_C) |
| 4491 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ), |
| 4492 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4493 | #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] | 4494 | #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] | 4495 | #if defined(MBEDTLS_ECDSA_C) |
| 4496 | 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] | 4497 | #endif |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4498 | #if defined(MBEDTLS_RSA_C) |
| 4499 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ), |
| 4500 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4501 | #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] | 4502 | MBEDTLS_TLS_SIG_NONE |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 4503 | }; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4504 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4505 | |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4506 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4507 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4508 | static uint16_t ssl_preset_suiteb_groups[] = { |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 4509 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4510 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 4511 | #endif |
| 4512 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4513 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 4514 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4515 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4516 | }; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4517 | |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4518 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4519 | /* 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] | 4520 | * to make sure there are no duplicated signature algorithm entries. */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 4521 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4522 | static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4523 | { |
| 4524 | size_t i, j; |
| 4525 | int ret = 0; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4526 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 4527 | for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4528 | { |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4529 | for( j = 0; j < i; j++ ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4530 | { |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4531 | if( sig_algs[i] != sig_algs[j] ) |
| 4532 | continue; |
| 4533 | mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET |
| 4534 | ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n", |
| 4535 | sig_algs[i], j, i ); |
| 4536 | ret = -1; |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4537 | } |
| 4538 | } |
| 4539 | return( ret ); |
| 4540 | } |
| 4541 | |
| 4542 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 4543 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4544 | /* |
Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 4545 | * Load default in mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4546 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 4547 | int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4548 | int endpoint, int transport, int preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4549 | { |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 4550 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4551 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 4552 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4553 | |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4554 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4555 | if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4556 | { |
| 4557 | mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" ); |
| 4558 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 4559 | } |
| 4560 | |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4561 | if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4562 | { |
| 4563 | mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" ); |
| 4564 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 4565 | } |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4566 | |
| 4567 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4568 | 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] | 4569 | { |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4570 | mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" ); |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4571 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 4572 | } |
| 4573 | |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4574 | 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] | 4575 | { |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4576 | mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" ); |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4577 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 4578 | } |
| 4579 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4580 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 4581 | |
Manuel Pégourié-Gonnard | 0de074f | 2015-05-14 12:58:01 +0200 | [diff] [blame] | 4582 | /* Use the functions here so that they are covered in tests, |
| 4583 | * but otherwise access member directly for efficiency */ |
| 4584 | mbedtls_ssl_conf_endpoint( conf, endpoint ); |
| 4585 | mbedtls_ssl_conf_transport( conf, transport ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4586 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4587 | /* |
| 4588 | * Things that are common to all presets |
| 4589 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 4590 | #if defined(MBEDTLS_SSL_CLI_C) |
| 4591 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 4592 | { |
| 4593 | conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED; |
| 4594 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 4595 | conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED; |
| 4596 | #endif |
| 4597 | } |
| 4598 | #endif |
| 4599 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4600 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 4601 | conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; |
| 4602 | #endif |
| 4603 | |
| 4604 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 4605 | conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; |
| 4606 | #endif |
| 4607 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 4608 | #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] | 4609 | conf->f_cookie_write = ssl_cookie_write_dummy; |
| 4610 | conf->f_cookie_check = ssl_cookie_check_dummy; |
| 4611 | #endif |
| 4612 | |
| 4613 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 4614 | conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED; |
| 4615 | #endif |
| 4616 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 4617 | #if defined(MBEDTLS_SSL_SRV_C) |
| 4618 | conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED; |
TRodziewicz | 3946f79 | 2021-06-14 12:11:18 +0200 | [diff] [blame] | 4619 | conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER; |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 4620 | #endif |
| 4621 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4622 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4623 | conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN; |
| 4624 | conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX; |
| 4625 | #endif |
| 4626 | |
| 4627 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 4628 | conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT; |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 4629 | memset( conf->renego_period, 0x00, 2 ); |
| 4630 | memset( conf->renego_period + 2, 0xFF, 6 ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4631 | #endif |
| 4632 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4633 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 4634 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 4635 | { |
| 4636 | const unsigned char dhm_p[] = |
| 4637 | MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN; |
| 4638 | const unsigned char dhm_g[] = |
| 4639 | MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN; |
Hanno Becker | 00d0a68 | 2017-10-04 13:14:29 +0100 | [diff] [blame] | 4640 | |
Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 4641 | if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf, |
| 4642 | dhm_p, sizeof( dhm_p ), |
| 4643 | dhm_g, sizeof( dhm_g ) ) ) != 0 ) |
| 4644 | { |
| 4645 | return( ret ); |
| 4646 | } |
| 4647 | } |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 4648 | #endif |
| 4649 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4650 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 4651 | /* |
| 4652 | * Allow all TLS 1.3 key exchange modes by default. |
| 4653 | */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4654 | conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL; |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4655 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 4656 | |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 4657 | if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 4658 | { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 4659 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 4660 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 4661 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 4662 | #else |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 4663 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 4664 | #endif |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 4665 | } |
| 4666 | else |
| 4667 | { |
| 4668 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 4669 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 4670 | { |
| 4671 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 4672 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 4673 | } |
| 4674 | else |
| 4675 | /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */ |
| 4676 | { |
| 4677 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 4678 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 4679 | } |
| 4680 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 4681 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 4682 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 4683 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4684 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 4685 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 4686 | #else |
| 4687 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 4688 | #endif |
| 4689 | } |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 4690 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4691 | /* |
| 4692 | * Preset-specific defaults |
| 4693 | */ |
| 4694 | switch( preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4695 | { |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4696 | /* |
| 4697 | * NSA Suite B |
| 4698 | */ |
| 4699 | case MBEDTLS_SSL_PRESET_SUITEB: |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4700 | |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 4701 | conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4702 | |
| 4703 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4704 | conf->cert_profile = &mbedtls_x509_crt_profile_suiteb; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4705 | #endif |
| 4706 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4707 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4708 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4709 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) |
| 4710 | conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs; |
| 4711 | else |
| 4712 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4713 | conf->sig_algs = ssl_preset_suiteb_sig_algs; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4714 | #endif |
| 4715 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4716 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 4717 | conf->curve_list = NULL; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4718 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4719 | conf->group_list = ssl_preset_suiteb_groups; |
Manuel Pégourié-Gonnard | c98204e | 2015-08-11 04:21:01 +0200 | [diff] [blame] | 4720 | break; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4721 | |
| 4722 | /* |
| 4723 | * Default |
| 4724 | */ |
| 4725 | default: |
Ronald Cron | f660655 | 2022-03-15 11:23:25 +0100 | [diff] [blame] | 4726 | |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 4727 | conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites(); |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4728 | |
| 4729 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4730 | conf->cert_profile = &mbedtls_x509_crt_profile_default; |
| 4731 | #endif |
| 4732 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4733 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4734 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4735 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) |
| 4736 | conf->sig_algs = ssl_tls12_preset_default_sig_algs; |
| 4737 | else |
| 4738 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4739 | conf->sig_algs = ssl_preset_default_sig_algs; |
Hanno Becker | deb68ce | 2021-08-10 16:04:05 +0100 | [diff] [blame] | 4740 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4741 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4742 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 4743 | conf->curve_list = NULL; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4744 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4745 | conf->group_list = ssl_preset_default_groups; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4746 | |
| 4747 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 4748 | conf->dhm_min_bitlen = 1024; |
| 4749 | #endif |
| 4750 | } |
| 4751 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4752 | return( 0 ); |
| 4753 | } |
| 4754 | |
| 4755 | /* |
| 4756 | * Free mbedtls_ssl_config |
| 4757 | */ |
| 4758 | void mbedtls_ssl_config_free( mbedtls_ssl_config *conf ) |
| 4759 | { |
| 4760 | #if defined(MBEDTLS_DHM_C) |
| 4761 | mbedtls_mpi_free( &conf->dhm_P ); |
| 4762 | mbedtls_mpi_free( &conf->dhm_G ); |
| 4763 | #endif |
| 4764 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4765 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 4766 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 4767 | if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) |
| 4768 | { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 4769 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
| 4770 | } |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 4771 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4772 | if( conf->psk != NULL ) |
| 4773 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4774 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4775 | mbedtls_free( conf->psk ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 4776 | conf->psk = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4777 | conf->psk_len = 0; |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 4778 | } |
| 4779 | |
| 4780 | if( conf->psk_identity != NULL ) |
| 4781 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4782 | mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len ); |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 4783 | mbedtls_free( conf->psk_identity ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 4784 | conf->psk_identity = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4785 | conf->psk_identity_len = 0; |
| 4786 | } |
| 4787 | #endif |
| 4788 | |
| 4789 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4790 | ssl_key_cert_free( conf->key_cert ); |
| 4791 | #endif |
| 4792 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4793 | mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4794 | } |
| 4795 | |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 4796 | #if defined(MBEDTLS_PK_C) && \ |
| 4797 | ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4798 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4799 | * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4800 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4801 | 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] | 4802 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4803 | #if defined(MBEDTLS_RSA_C) |
| 4804 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) ) |
| 4805 | return( MBEDTLS_SSL_SIG_RSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4806 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4807 | #if defined(MBEDTLS_ECDSA_C) |
| 4808 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) ) |
| 4809 | return( MBEDTLS_SSL_SIG_ECDSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4810 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4811 | return( MBEDTLS_SSL_SIG_ANON ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4812 | } |
| 4813 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 4814 | unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type ) |
| 4815 | { |
| 4816 | switch( type ) { |
| 4817 | case MBEDTLS_PK_RSA: |
| 4818 | return( MBEDTLS_SSL_SIG_RSA ); |
| 4819 | case MBEDTLS_PK_ECDSA: |
| 4820 | case MBEDTLS_PK_ECKEY: |
| 4821 | return( MBEDTLS_SSL_SIG_ECDSA ); |
| 4822 | default: |
| 4823 | return( MBEDTLS_SSL_SIG_ANON ); |
| 4824 | } |
| 4825 | } |
| 4826 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4827 | 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] | 4828 | { |
| 4829 | switch( sig ) |
| 4830 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4831 | #if defined(MBEDTLS_RSA_C) |
| 4832 | case MBEDTLS_SSL_SIG_RSA: |
| 4833 | return( MBEDTLS_PK_RSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4834 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4835 | #if defined(MBEDTLS_ECDSA_C) |
| 4836 | case MBEDTLS_SSL_SIG_ECDSA: |
| 4837 | return( MBEDTLS_PK_ECDSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4838 | #endif |
| 4839 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4840 | return( MBEDTLS_PK_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4841 | } |
| 4842 | } |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 4843 | #endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */ |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4844 | |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 4845 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4846 | * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 4847 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4848 | 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] | 4849 | { |
| 4850 | switch( hash ) |
| 4851 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4852 | #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] | 4853 | case MBEDTLS_SSL_HASH_MD5: |
| 4854 | return( MBEDTLS_MD_MD5 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4855 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4856 | #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] | 4857 | case MBEDTLS_SSL_HASH_SHA1: |
| 4858 | return( MBEDTLS_MD_SHA1 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4859 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4860 | #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] | 4861 | case MBEDTLS_SSL_HASH_SHA224: |
| 4862 | return( MBEDTLS_MD_SHA224 ); |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 4863 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4864 | #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] | 4865 | case MBEDTLS_SSL_HASH_SHA256: |
| 4866 | return( MBEDTLS_MD_SHA256 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4867 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4868 | #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] | 4869 | case MBEDTLS_SSL_HASH_SHA384: |
| 4870 | return( MBEDTLS_MD_SHA384 ); |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 4871 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4872 | #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] | 4873 | case MBEDTLS_SSL_HASH_SHA512: |
| 4874 | return( MBEDTLS_MD_SHA512 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4875 | #endif |
| 4876 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4877 | return( MBEDTLS_MD_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4878 | } |
| 4879 | } |
| 4880 | |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4881 | /* |
| 4882 | * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX |
| 4883 | */ |
| 4884 | unsigned char mbedtls_ssl_hash_from_md_alg( int md ) |
| 4885 | { |
| 4886 | switch( md ) |
| 4887 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4888 | #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] | 4889 | case MBEDTLS_MD_MD5: |
| 4890 | return( MBEDTLS_SSL_HASH_MD5 ); |
| 4891 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4892 | #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] | 4893 | case MBEDTLS_MD_SHA1: |
| 4894 | return( MBEDTLS_SSL_HASH_SHA1 ); |
| 4895 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4896 | #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] | 4897 | case MBEDTLS_MD_SHA224: |
| 4898 | return( MBEDTLS_SSL_HASH_SHA224 ); |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 4899 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4900 | #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] | 4901 | case MBEDTLS_MD_SHA256: |
| 4902 | return( MBEDTLS_SSL_HASH_SHA256 ); |
| 4903 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4904 | #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] | 4905 | case MBEDTLS_MD_SHA384: |
| 4906 | return( MBEDTLS_SSL_HASH_SHA384 ); |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 4907 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4908 | #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] | 4909 | case MBEDTLS_MD_SHA512: |
| 4910 | return( MBEDTLS_SSL_HASH_SHA512 ); |
| 4911 | #endif |
| 4912 | default: |
| 4913 | return( MBEDTLS_SSL_HASH_NONE ); |
| 4914 | } |
| 4915 | } |
| 4916 | |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 4917 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4918 | * 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] | 4919 | * 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] | 4920 | */ |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 4921 | 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] | 4922 | { |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4923 | const uint16_t *group_list = mbedtls_ssl_get_groups( ssl ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 4924 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4925 | if( group_list == NULL ) |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 4926 | return( -1 ); |
| 4927 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4928 | for( ; *group_list != 0; group_list++ ) |
| 4929 | { |
| 4930 | if( *group_list == tls_id ) |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 4931 | return( 0 ); |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4932 | } |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 4933 | |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 4934 | return( -1 ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 4935 | } |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 4936 | |
| 4937 | #if defined(MBEDTLS_ECP_C) |
| 4938 | /* |
| 4939 | * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id. |
| 4940 | */ |
| 4941 | int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id ) |
| 4942 | { |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 4943 | const mbedtls_ecp_curve_info *grp_info = |
Tom Cosgrove | bcc13c9 | 2022-08-24 15:08:16 +0100 | [diff] [blame] | 4944 | mbedtls_ecp_curve_info_from_grp_id( grp_id ); |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 4945 | |
Tom Cosgrove | bcc13c9 | 2022-08-24 15:08:16 +0100 | [diff] [blame] | 4946 | if ( grp_info == NULL ) |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 4947 | return -1; |
| 4948 | |
| 4949 | uint16_t tls_id = grp_info->tls_id; |
| 4950 | |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 4951 | return mbedtls_ssl_check_curve_tls_id( ssl, tls_id ); |
| 4952 | } |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 4953 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4954 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4955 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4956 | int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert, |
| 4957 | const mbedtls_ssl_ciphersuite_t *ciphersuite, |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 4958 | int cert_endpoint, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 4959 | uint32_t *flags ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4960 | { |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 4961 | int ret = 0; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4962 | int usage = 0; |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 4963 | const char *ext_oid; |
| 4964 | size_t ext_len; |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 4965 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4966 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4967 | { |
| 4968 | /* Server part of the key exchange */ |
| 4969 | switch( ciphersuite->key_exchange ) |
| 4970 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4971 | case MBEDTLS_KEY_EXCHANGE_RSA: |
| 4972 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 4973 | usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4974 | break; |
| 4975 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4976 | case MBEDTLS_KEY_EXCHANGE_DHE_RSA: |
| 4977 | case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: |
| 4978 | case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: |
| 4979 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4980 | break; |
| 4981 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4982 | case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: |
| 4983 | case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 4984 | usage = MBEDTLS_X509_KU_KEY_AGREEMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4985 | break; |
| 4986 | |
| 4987 | /* 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] | 4988 | case MBEDTLS_KEY_EXCHANGE_NONE: |
| 4989 | case MBEDTLS_KEY_EXCHANGE_PSK: |
| 4990 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
| 4991 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Manuel Pégourié-Gonnard | 557535d | 2015-09-15 17:53:32 +0200 | [diff] [blame] | 4992 | case MBEDTLS_KEY_EXCHANGE_ECJPAKE: |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4993 | usage = 0; |
| 4994 | } |
| 4995 | } |
| 4996 | else |
| 4997 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4998 | /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */ |
| 4999 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5000 | } |
| 5001 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5002 | if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 ) |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5003 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5004 | *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5005 | ret = -1; |
| 5006 | } |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5007 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5008 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5009 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5010 | ext_oid = MBEDTLS_OID_SERVER_AUTH; |
| 5011 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5012 | } |
| 5013 | else |
| 5014 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5015 | ext_oid = MBEDTLS_OID_CLIENT_AUTH; |
| 5016 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5017 | } |
| 5018 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5019 | 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] | 5020 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5021 | *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5022 | ret = -1; |
| 5023 | } |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5024 | |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5025 | return( ret ); |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5026 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5027 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 3a306b9 | 2014-04-29 15:11:17 +0200 | [diff] [blame] | 5028 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5029 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5030 | int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl, |
| 5031 | const mbedtls_md_type_t md, |
| 5032 | unsigned char *dst, |
| 5033 | size_t dst_len, |
| 5034 | size_t *olen ) |
| 5035 | { |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5036 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 5037 | psa_hash_operation_t *hash_operation_to_clone; |
| 5038 | psa_hash_operation_t hash_operation = psa_hash_operation_init(); |
| 5039 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5040 | *olen = 0; |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5041 | |
| 5042 | switch( md ) |
| 5043 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5044 | #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] | 5045 | case MBEDTLS_MD_SHA384: |
| 5046 | hash_operation_to_clone = &ssl->handshake->fin_sha384_psa; |
| 5047 | break; |
| 5048 | #endif |
| 5049 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5050 | #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] | 5051 | case MBEDTLS_MD_SHA256: |
| 5052 | hash_operation_to_clone = &ssl->handshake->fin_sha256_psa; |
| 5053 | break; |
| 5054 | #endif |
| 5055 | |
| 5056 | default: |
| 5057 | goto exit; |
| 5058 | } |
| 5059 | |
| 5060 | status = psa_hash_clone( hash_operation_to_clone, &hash_operation ); |
| 5061 | if( status != PSA_SUCCESS ) |
| 5062 | goto exit; |
| 5063 | |
| 5064 | status = psa_hash_finish( &hash_operation, dst, dst_len, olen ); |
| 5065 | if( status != PSA_SUCCESS ) |
| 5066 | goto exit; |
| 5067 | |
| 5068 | exit: |
Ronald Cron | b788c04 | 2022-02-15 09:14:15 +0100 | [diff] [blame] | 5069 | return( psa_ssl_status_to_mbedtls( status ) ); |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5070 | } |
| 5071 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5072 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5073 | #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] | 5074 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 5075 | static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl, |
| 5076 | unsigned char *dst, |
| 5077 | size_t dst_len, |
| 5078 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5079 | { |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5080 | int ret; |
| 5081 | mbedtls_sha512_context sha512; |
| 5082 | |
| 5083 | if( dst_len < 48 ) |
| 5084 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5085 | |
| 5086 | mbedtls_sha512_init( &sha512 ); |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 5087 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5088 | |
| 5089 | if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 ) |
| 5090 | { |
| 5091 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret ); |
| 5092 | goto exit; |
| 5093 | } |
| 5094 | |
| 5095 | *olen = 48; |
| 5096 | |
| 5097 | exit: |
| 5098 | |
| 5099 | mbedtls_sha512_free( &sha512 ); |
| 5100 | return( ret ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5101 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5102 | #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] | 5103 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5104 | #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] | 5105 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 5106 | static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl, |
| 5107 | unsigned char *dst, |
| 5108 | size_t dst_len, |
| 5109 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5110 | { |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5111 | int ret; |
| 5112 | mbedtls_sha256_context sha256; |
| 5113 | |
| 5114 | if( dst_len < 32 ) |
| 5115 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5116 | |
| 5117 | mbedtls_sha256_init( &sha256 ); |
| 5118 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 5119 | |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5120 | if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 ) |
| 5121 | { |
| 5122 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret ); |
| 5123 | goto exit; |
| 5124 | } |
| 5125 | |
| 5126 | *olen = 32; |
| 5127 | |
| 5128 | exit: |
| 5129 | |
| 5130 | mbedtls_sha256_free( &sha256 ); |
| 5131 | return( ret ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5132 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5133 | #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] | 5134 | |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 5135 | int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl, |
| 5136 | const mbedtls_md_type_t md, |
| 5137 | unsigned char *dst, |
| 5138 | size_t dst_len, |
| 5139 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5140 | { |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 5141 | switch( md ) |
| 5142 | { |
| 5143 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5144 | #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] | 5145 | case MBEDTLS_MD_SHA384: |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 5146 | return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) ); |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5147 | #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] | 5148 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5149 | #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] | 5150 | case MBEDTLS_MD_SHA256: |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 5151 | return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) ); |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5152 | #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] | 5153 | |
| 5154 | default: |
| 5155 | break; |
| 5156 | } |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 5157 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5158 | } |
XiaokangQian | 647719a | 2021-12-07 09:16:29 +0000 | [diff] [blame] | 5159 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5160 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5161 | |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5162 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 5163 | /* mbedtls_ssl_parse_sig_alg_ext() |
| 5164 | * |
| 5165 | * The `extension_data` field of signature algorithm contains a `SignatureSchemeList` |
| 5166 | * value (TLS 1.3 RFC8446): |
| 5167 | * enum { |
| 5168 | * .... |
| 5169 | * ecdsa_secp256r1_sha256( 0x0403 ), |
| 5170 | * ecdsa_secp384r1_sha384( 0x0503 ), |
| 5171 | * ecdsa_secp521r1_sha512( 0x0603 ), |
| 5172 | * .... |
| 5173 | * } SignatureScheme; |
| 5174 | * |
| 5175 | * struct { |
| 5176 | * SignatureScheme supported_signature_algorithms<2..2^16-2>; |
| 5177 | * } SignatureSchemeList; |
| 5178 | * |
| 5179 | * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm` |
| 5180 | * value (TLS 1.2 RFC5246): |
| 5181 | * enum { |
| 5182 | * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), |
| 5183 | * sha512(6), (255) |
| 5184 | * } HashAlgorithm; |
| 5185 | * |
| 5186 | * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } |
| 5187 | * SignatureAlgorithm; |
| 5188 | * |
| 5189 | * struct { |
| 5190 | * HashAlgorithm hash; |
| 5191 | * SignatureAlgorithm signature; |
| 5192 | * } SignatureAndHashAlgorithm; |
| 5193 | * |
| 5194 | * SignatureAndHashAlgorithm |
| 5195 | * supported_signature_algorithms<2..2^16-2>; |
| 5196 | * |
| 5197 | * The TLS 1.3 signature algorithm extension was defined to be a compatible |
| 5198 | * generalization of the TLS 1.2 signature algorithm extension. |
| 5199 | * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by |
| 5200 | * `SignatureScheme` field of TLS 1.3 |
| 5201 | * |
| 5202 | */ |
| 5203 | int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl, |
| 5204 | const unsigned char *buf, |
| 5205 | const unsigned char *end ) |
| 5206 | { |
| 5207 | const unsigned char *p = buf; |
| 5208 | size_t supported_sig_algs_len = 0; |
| 5209 | const unsigned char *supported_sig_algs_end; |
| 5210 | uint16_t sig_alg; |
| 5211 | uint32_t common_idx = 0; |
| 5212 | |
| 5213 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 ); |
| 5214 | supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
| 5215 | p += 2; |
| 5216 | |
| 5217 | memset( ssl->handshake->received_sig_algs, 0, |
| 5218 | sizeof(ssl->handshake->received_sig_algs) ); |
| 5219 | |
| 5220 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len ); |
| 5221 | supported_sig_algs_end = p + supported_sig_algs_len; |
| 5222 | while( p < supported_sig_algs_end ) |
| 5223 | { |
| 5224 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 ); |
| 5225 | sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 ); |
| 5226 | p += 2; |
Jerry Yu | f3b46b5 | 2022-06-19 16:52:27 +0800 | [diff] [blame] | 5227 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s", |
| 5228 | sig_alg, |
| 5229 | mbedtls_ssl_sig_alg_to_str( sig_alg ) ) ); |
Jerry Yu | 2fe6c63 | 2022-06-29 10:02:38 +0800 | [diff] [blame] | 5230 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | 52b7d92 | 2022-07-01 18:03:31 +0800 | [diff] [blame] | 5231 | if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 && |
Jerry Yu | 2fe6c63 | 2022-06-29 10:02:38 +0800 | [diff] [blame] | 5232 | ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) && |
| 5233 | mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) ) |
| 5234 | { |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5235 | continue; |
Jerry Yu | 2fe6c63 | 2022-06-29 10:02:38 +0800 | [diff] [blame] | 5236 | } |
| 5237 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5238 | |
Jerry Yu | f3b46b5 | 2022-06-19 16:52:27 +0800 | [diff] [blame] | 5239 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s", |
| 5240 | mbedtls_ssl_sig_alg_to_str( sig_alg ) ) ); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5241 | |
| 5242 | if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE ) |
| 5243 | { |
| 5244 | ssl->handshake->received_sig_algs[common_idx] = sig_alg; |
| 5245 | common_idx += 1; |
| 5246 | } |
| 5247 | } |
| 5248 | /* Check that we consumed all the message. */ |
| 5249 | if( p != end ) |
| 5250 | { |
| 5251 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 5252 | ( "Signature algorithms extension length misaligned" ) ); |
| 5253 | MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, |
| 5254 | MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 5255 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 5256 | } |
| 5257 | |
| 5258 | if( common_idx == 0 ) |
| 5259 | { |
| 5260 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) ); |
| 5261 | MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE, |
| 5262 | MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE ); |
| 5263 | return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE ); |
| 5264 | } |
| 5265 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 5266 | ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE; |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5267 | return( 0 ); |
| 5268 | } |
| 5269 | |
| 5270 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 5271 | |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5272 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5273 | |
| 5274 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5275 | |
| 5276 | static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation, |
| 5277 | mbedtls_svc_key_id_t key, |
| 5278 | psa_algorithm_t alg, |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 5279 | const unsigned char* raw_psk, size_t raw_psk_length, |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5280 | const unsigned char* seed, size_t seed_length, |
| 5281 | const unsigned char* label, size_t label_length, |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5282 | const unsigned char* other_secret, |
| 5283 | size_t other_secret_length, |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5284 | size_t capacity ) |
| 5285 | { |
| 5286 | psa_status_t status; |
| 5287 | |
| 5288 | status = psa_key_derivation_setup( derivation, alg ); |
| 5289 | if( status != PSA_SUCCESS ) |
| 5290 | return( status ); |
| 5291 | |
| 5292 | if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
| 5293 | { |
| 5294 | status = psa_key_derivation_input_bytes( derivation, |
| 5295 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 5296 | seed, seed_length ); |
| 5297 | if( status != PSA_SUCCESS ) |
| 5298 | return( status ); |
| 5299 | |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5300 | if ( other_secret != NULL ) |
Przemek Stekiel | 1f02703 | 2022-04-05 17:12:11 +0200 | [diff] [blame] | 5301 | { |
| 5302 | status = psa_key_derivation_input_bytes( derivation, |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5303 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 5304 | other_secret, other_secret_length ); |
Przemek Stekiel | 1f02703 | 2022-04-05 17:12:11 +0200 | [diff] [blame] | 5305 | if( status != PSA_SUCCESS ) |
| 5306 | return( status ); |
| 5307 | } |
| 5308 | |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5309 | if( mbedtls_svc_key_id_is_null( key ) ) |
| 5310 | { |
| 5311 | status = psa_key_derivation_input_bytes( |
| 5312 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 5313 | raw_psk, raw_psk_length ); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5314 | } |
| 5315 | else |
| 5316 | { |
| 5317 | status = psa_key_derivation_input_key( |
| 5318 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key ); |
| 5319 | } |
| 5320 | if( status != PSA_SUCCESS ) |
| 5321 | return( status ); |
| 5322 | |
| 5323 | status = psa_key_derivation_input_bytes( derivation, |
| 5324 | PSA_KEY_DERIVATION_INPUT_LABEL, |
| 5325 | label, label_length ); |
| 5326 | if( status != PSA_SUCCESS ) |
| 5327 | return( status ); |
| 5328 | } |
| 5329 | else |
| 5330 | { |
| 5331 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5332 | } |
| 5333 | |
| 5334 | status = psa_key_derivation_set_capacity( derivation, capacity ); |
| 5335 | if( status != PSA_SUCCESS ) |
| 5336 | return( status ); |
| 5337 | |
| 5338 | return( PSA_SUCCESS ); |
| 5339 | } |
| 5340 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5341 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5342 | static int tls_prf_generic( mbedtls_md_type_t md_type, |
| 5343 | const unsigned char *secret, size_t slen, |
| 5344 | const char *label, |
| 5345 | const unsigned char *random, size_t rlen, |
| 5346 | unsigned char *dstbuf, size_t dlen ) |
| 5347 | { |
| 5348 | psa_status_t status; |
| 5349 | psa_algorithm_t alg; |
| 5350 | mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5351 | psa_key_derivation_operation_t derivation = |
| 5352 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 5353 | |
| 5354 | if( md_type == MBEDTLS_MD_SHA384 ) |
| 5355 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384); |
| 5356 | else |
| 5357 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256); |
| 5358 | |
| 5359 | /* Normally a "secret" should be long enough to be impossible to |
| 5360 | * find by brute force, and in particular should not be empty. But |
| 5361 | * this PRF is also used to derive an IV, in particular in EAP-TLS, |
| 5362 | * and for this use case it makes sense to have a 0-length "secret". |
| 5363 | * Since the key API doesn't allow importing a key of length 0, |
| 5364 | * keep master_key=0, which setup_psa_key_derivation() understands |
| 5365 | * to mean a 0-length "secret" input. */ |
| 5366 | if( slen != 0 ) |
| 5367 | { |
| 5368 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); |
| 5369 | psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE ); |
| 5370 | psa_set_key_algorithm( &key_attributes, alg ); |
| 5371 | psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE ); |
| 5372 | |
| 5373 | status = psa_import_key( &key_attributes, secret, slen, &master_key ); |
| 5374 | if( status != PSA_SUCCESS ) |
| 5375 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5376 | } |
| 5377 | |
| 5378 | status = setup_psa_key_derivation( &derivation, |
| 5379 | master_key, alg, |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 5380 | NULL, 0, |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5381 | random, rlen, |
| 5382 | (unsigned char const *) label, |
| 5383 | (size_t) strlen( label ), |
Przemek Stekiel | 1f02703 | 2022-04-05 17:12:11 +0200 | [diff] [blame] | 5384 | NULL, 0, |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5385 | dlen ); |
| 5386 | if( status != PSA_SUCCESS ) |
| 5387 | { |
| 5388 | psa_key_derivation_abort( &derivation ); |
| 5389 | psa_destroy_key( master_key ); |
| 5390 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5391 | } |
| 5392 | |
| 5393 | status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen ); |
| 5394 | if( status != PSA_SUCCESS ) |
| 5395 | { |
| 5396 | psa_key_derivation_abort( &derivation ); |
| 5397 | psa_destroy_key( master_key ); |
| 5398 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5399 | } |
| 5400 | |
| 5401 | status = psa_key_derivation_abort( &derivation ); |
| 5402 | if( status != PSA_SUCCESS ) |
| 5403 | { |
| 5404 | psa_destroy_key( master_key ); |
| 5405 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5406 | } |
| 5407 | |
| 5408 | if( ! mbedtls_svc_key_id_is_null( master_key ) ) |
| 5409 | status = psa_destroy_key( master_key ); |
| 5410 | if( status != PSA_SUCCESS ) |
| 5411 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5412 | |
| 5413 | return( 0 ); |
| 5414 | } |
| 5415 | |
| 5416 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5417 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5418 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5419 | static int tls_prf_generic( mbedtls_md_type_t md_type, |
| 5420 | const unsigned char *secret, size_t slen, |
| 5421 | const char *label, |
| 5422 | const unsigned char *random, size_t rlen, |
| 5423 | unsigned char *dstbuf, size_t dlen ) |
| 5424 | { |
| 5425 | size_t nb; |
| 5426 | size_t i, j, k, md_len; |
| 5427 | unsigned char *tmp; |
| 5428 | size_t tmp_len = 0; |
| 5429 | unsigned char h_i[MBEDTLS_MD_MAX_SIZE]; |
| 5430 | const mbedtls_md_info_t *md_info; |
| 5431 | mbedtls_md_context_t md_ctx; |
| 5432 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5433 | |
| 5434 | mbedtls_md_init( &md_ctx ); |
| 5435 | |
| 5436 | if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL ) |
| 5437 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5438 | |
| 5439 | md_len = mbedtls_md_get_size( md_info ); |
| 5440 | |
| 5441 | tmp_len = md_len + strlen( label ) + rlen; |
| 5442 | tmp = mbedtls_calloc( 1, tmp_len ); |
| 5443 | if( tmp == NULL ) |
| 5444 | { |
| 5445 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 5446 | goto exit; |
| 5447 | } |
| 5448 | |
| 5449 | nb = strlen( label ); |
| 5450 | memcpy( tmp + md_len, label, nb ); |
| 5451 | memcpy( tmp + md_len + nb, random, rlen ); |
| 5452 | nb += rlen; |
| 5453 | |
| 5454 | /* |
| 5455 | * Compute P_<hash>(secret, label + random)[0..dlen] |
| 5456 | */ |
| 5457 | if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) |
| 5458 | goto exit; |
| 5459 | |
| 5460 | ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen ); |
| 5461 | if( ret != 0 ) |
| 5462 | goto exit; |
| 5463 | ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb ); |
| 5464 | if( ret != 0 ) |
| 5465 | goto exit; |
| 5466 | ret = mbedtls_md_hmac_finish( &md_ctx, tmp ); |
| 5467 | if( ret != 0 ) |
| 5468 | goto exit; |
| 5469 | |
| 5470 | for( i = 0; i < dlen; i += md_len ) |
| 5471 | { |
| 5472 | ret = mbedtls_md_hmac_reset ( &md_ctx ); |
| 5473 | if( ret != 0 ) |
| 5474 | goto exit; |
| 5475 | ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb ); |
| 5476 | if( ret != 0 ) |
| 5477 | goto exit; |
| 5478 | ret = mbedtls_md_hmac_finish( &md_ctx, h_i ); |
| 5479 | if( ret != 0 ) |
| 5480 | goto exit; |
| 5481 | |
| 5482 | ret = mbedtls_md_hmac_reset ( &md_ctx ); |
| 5483 | if( ret != 0 ) |
| 5484 | goto exit; |
| 5485 | ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len ); |
| 5486 | if( ret != 0 ) |
| 5487 | goto exit; |
| 5488 | ret = mbedtls_md_hmac_finish( &md_ctx, tmp ); |
| 5489 | if( ret != 0 ) |
| 5490 | goto exit; |
| 5491 | |
| 5492 | k = ( i + md_len > dlen ) ? dlen % md_len : md_len; |
| 5493 | |
| 5494 | for( j = 0; j < k; j++ ) |
| 5495 | dstbuf[i + j] = h_i[j]; |
| 5496 | } |
| 5497 | |
| 5498 | exit: |
| 5499 | mbedtls_md_free( &md_ctx ); |
| 5500 | |
| 5501 | mbedtls_platform_zeroize( tmp, tmp_len ); |
| 5502 | mbedtls_platform_zeroize( h_i, sizeof( h_i ) ); |
| 5503 | |
| 5504 | mbedtls_free( tmp ); |
| 5505 | |
| 5506 | return( ret ); |
| 5507 | } |
| 5508 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5509 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5510 | #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] | 5511 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5512 | static int tls_prf_sha256( const unsigned char *secret, size_t slen, |
| 5513 | const char *label, |
| 5514 | const unsigned char *random, size_t rlen, |
| 5515 | unsigned char *dstbuf, size_t dlen ) |
| 5516 | { |
| 5517 | return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen, |
| 5518 | label, random, rlen, dstbuf, dlen ) ); |
| 5519 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5520 | #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] | 5521 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5522 | #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] | 5523 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5524 | static int tls_prf_sha384( const unsigned char *secret, size_t slen, |
| 5525 | const char *label, |
| 5526 | const unsigned char *random, size_t rlen, |
| 5527 | unsigned char *dstbuf, size_t dlen ) |
| 5528 | { |
| 5529 | return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen, |
| 5530 | label, random, rlen, dstbuf, dlen ) ); |
| 5531 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5532 | #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] | 5533 | |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5534 | /* |
| 5535 | * Set appropriate PRF function and other SSL / TLS1.2 functions |
| 5536 | * |
| 5537 | * Inputs: |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5538 | * - hash associated with the ciphersuite (only used by TLS 1.2) |
| 5539 | * |
| 5540 | * Outputs: |
| 5541 | * - the tls_prf, calc_verify and calc_finished members of handshake structure |
| 5542 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5543 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5544 | static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake, |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5545 | mbedtls_md_type_t hash ) |
| 5546 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5547 | #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] | 5548 | if( hash == MBEDTLS_MD_SHA384 ) |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5549 | { |
| 5550 | handshake->tls_prf = tls_prf_sha384; |
| 5551 | handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 5552 | handshake->calc_finished = ssl_calc_finished_tls_sha384; |
| 5553 | } |
| 5554 | else |
| 5555 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5556 | #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] | 5557 | { |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 5558 | (void) hash; |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5559 | handshake->tls_prf = tls_prf_sha256; |
| 5560 | handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 5561 | handshake->calc_finished = ssl_calc_finished_tls_sha256; |
| 5562 | } |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 5563 | #else |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5564 | { |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5565 | (void) handshake; |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 5566 | (void) hash; |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5567 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5568 | } |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 5569 | #endif |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5570 | |
| 5571 | return( 0 ); |
| 5572 | } |
Jerry Yu | d6ab235 | 2022-02-17 14:03:43 +0800 | [diff] [blame] | 5573 | |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5574 | /* |
| 5575 | * Compute master secret if needed |
| 5576 | * |
| 5577 | * Parameters: |
| 5578 | * [in/out] handshake |
| 5579 | * [in] resume, premaster, extended_ms, calc_verify, tls_prf |
| 5580 | * (PSA-PSK) ciphersuite_info, psk_opaque |
| 5581 | * [out] premaster (cleared) |
| 5582 | * [out] master |
| 5583 | * [in] ssl: optionally used for debugging, EMS and PSA-PSK |
| 5584 | * debug: conf->f_dbg, conf->p_dbg |
| 5585 | * EMS: passed to calc_verify (debug + session_negotiate) |
Ronald Cron | a25cf58 | 2022-03-07 11:10:36 +0100 | [diff] [blame] | 5586 | * PSA-PSA: conf |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5587 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5588 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5589 | static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, |
| 5590 | unsigned char *master, |
| 5591 | const mbedtls_ssl_context *ssl ) |
| 5592 | { |
| 5593 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5594 | |
| 5595 | /* cf. RFC 5246, Section 8.1: |
| 5596 | * "The master secret is always exactly 48 bytes in length." */ |
| 5597 | size_t const master_secret_len = 48; |
| 5598 | |
| 5599 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 5600 | unsigned char session_hash[48]; |
| 5601 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
| 5602 | |
| 5603 | /* The label for the KDF used for key expansion. |
| 5604 | * This is either "master secret" or "extended master secret" |
| 5605 | * depending on whether the Extended Master Secret extension |
| 5606 | * is used. */ |
| 5607 | char const *lbl = "master secret"; |
| 5608 | |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5609 | /* The seed for the KDF used for key expansion. |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5610 | * - If the Extended Master Secret extension is not used, |
| 5611 | * this is ClientHello.Random + ServerHello.Random |
| 5612 | * (see Sect. 8.1 in RFC 5246). |
| 5613 | * - If the Extended Master Secret extension is used, |
| 5614 | * this is the transcript of the handshake so far. |
| 5615 | * (see Sect. 4 in RFC 7627). */ |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5616 | unsigned char const *seed = handshake->randbytes; |
| 5617 | size_t seed_len = 64; |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5618 | |
| 5619 | #if !defined(MBEDTLS_DEBUG_C) && \ |
| 5620 | !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \ |
| 5621 | !(defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 5622 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)) |
| 5623 | ssl = NULL; /* make sure we don't use it except for those cases */ |
| 5624 | (void) ssl; |
| 5625 | #endif |
| 5626 | |
| 5627 | if( handshake->resume != 0 ) |
| 5628 | { |
| 5629 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) ); |
| 5630 | return( 0 ); |
| 5631 | } |
| 5632 | |
| 5633 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 5634 | if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED ) |
| 5635 | { |
| 5636 | lbl = "extended master secret"; |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5637 | seed = session_hash; |
| 5638 | handshake->calc_verify( ssl, session_hash, &seed_len ); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5639 | |
| 5640 | MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret", |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5641 | session_hash, seed_len ); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5642 | } |
Przemek Stekiel | 169bf0b | 2022-04-29 07:53:29 +0200 | [diff] [blame] | 5643 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5644 | |
Przemek Stekiel | 99114f3 | 2022-04-22 11:20:09 +0200 | [diff] [blame] | 5645 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
Przemek Stekiel | 8a4b7fd | 2022-04-28 09:22:22 +0200 | [diff] [blame] | 5646 | defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 5647 | if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 ) |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5648 | { |
| 5649 | /* Perform PSK-to-MS expansion in a single step. */ |
| 5650 | psa_status_t status; |
| 5651 | psa_algorithm_t alg; |
| 5652 | mbedtls_svc_key_id_t psk; |
| 5653 | psa_key_derivation_operation_t derivation = |
| 5654 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 5655 | mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac; |
| 5656 | |
| 5657 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) ); |
| 5658 | |
| 5659 | psk = mbedtls_ssl_get_opaque_psk( ssl ); |
| 5660 | |
| 5661 | if( hash_alg == MBEDTLS_MD_SHA384 ) |
| 5662 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384); |
| 5663 | else |
| 5664 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); |
| 5665 | |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5666 | size_t other_secret_len = 0; |
| 5667 | unsigned char* other_secret = NULL; |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 5668 | |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5669 | switch( handshake->ciphersuite_info->key_exchange ) |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 5670 | { |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5671 | /* Provide other secret. |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5672 | * Other secret is stored in premaster, where first 2 bytes hold the |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5673 | * length of the other key. |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 5674 | */ |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5675 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
Przemek Stekiel | 8abcee9 | 2022-04-28 09:16:28 +0200 | [diff] [blame] | 5676 | /* For RSA-PSK other key length is always 48 bytes. */ |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5677 | other_secret_len = 48; |
| 5678 | other_secret = handshake->premaster + 2; |
| 5679 | break; |
| 5680 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 5681 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5682 | other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0); |
| 5683 | other_secret = handshake->premaster + 2; |
| 5684 | break; |
| 5685 | default: |
| 5686 | break; |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 5687 | } |
| 5688 | |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5689 | status = setup_psa_key_derivation( &derivation, psk, alg, |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 5690 | ssl->conf->psk, ssl->conf->psk_len, |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5691 | seed, seed_len, |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5692 | (unsigned char const *) lbl, |
| 5693 | (size_t) strlen( lbl ), |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5694 | other_secret, other_secret_len, |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5695 | master_secret_len ); |
| 5696 | if( status != PSA_SUCCESS ) |
| 5697 | { |
| 5698 | psa_key_derivation_abort( &derivation ); |
| 5699 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5700 | } |
| 5701 | |
| 5702 | status = psa_key_derivation_output_bytes( &derivation, |
| 5703 | master, |
| 5704 | master_secret_len ); |
| 5705 | if( status != PSA_SUCCESS ) |
| 5706 | { |
| 5707 | psa_key_derivation_abort( &derivation ); |
| 5708 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5709 | } |
| 5710 | |
| 5711 | status = psa_key_derivation_abort( &derivation ); |
| 5712 | if( status != PSA_SUCCESS ) |
| 5713 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5714 | } |
| 5715 | else |
| 5716 | #endif |
| 5717 | { |
| 5718 | ret = handshake->tls_prf( handshake->premaster, handshake->pmslen, |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5719 | lbl, seed, seed_len, |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5720 | master, |
| 5721 | master_secret_len ); |
| 5722 | if( ret != 0 ) |
| 5723 | { |
| 5724 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
| 5725 | return( ret ); |
| 5726 | } |
| 5727 | |
| 5728 | MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", |
| 5729 | handshake->premaster, |
| 5730 | handshake->pmslen ); |
| 5731 | |
| 5732 | mbedtls_platform_zeroize( handshake->premaster, |
| 5733 | sizeof(handshake->premaster) ); |
| 5734 | } |
| 5735 | |
| 5736 | return( 0 ); |
| 5737 | } |
| 5738 | |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 5739 | int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl ) |
| 5740 | { |
| 5741 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5742 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
| 5743 | ssl->handshake->ciphersuite_info; |
| 5744 | |
| 5745 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) ); |
| 5746 | |
| 5747 | /* Set PRF, calc_verify and calc_finished function pointers */ |
| 5748 | ret = ssl_set_handshake_prfs( ssl->handshake, |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 5749 | ciphersuite_info->mac ); |
| 5750 | if( ret != 0 ) |
| 5751 | { |
| 5752 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret ); |
| 5753 | return( ret ); |
| 5754 | } |
| 5755 | |
| 5756 | /* Compute master secret if needed */ |
| 5757 | ret = ssl_compute_master( ssl->handshake, |
| 5758 | ssl->session_negotiate->master, |
| 5759 | ssl ); |
| 5760 | if( ret != 0 ) |
| 5761 | { |
| 5762 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret ); |
| 5763 | return( ret ); |
| 5764 | } |
| 5765 | |
| 5766 | /* Swap the client and server random values: |
| 5767 | * - MS derivation wanted client+server (RFC 5246 8.1) |
| 5768 | * - key derivation wants server+client (RFC 5246 6.3) */ |
| 5769 | { |
| 5770 | unsigned char tmp[64]; |
| 5771 | memcpy( tmp, ssl->handshake->randbytes, 64 ); |
| 5772 | memcpy( ssl->handshake->randbytes, tmp + 32, 32 ); |
| 5773 | memcpy( ssl->handshake->randbytes + 32, tmp, 32 ); |
| 5774 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
| 5775 | } |
| 5776 | |
| 5777 | /* Populate transform structure */ |
| 5778 | ret = ssl_tls12_populate_transform( ssl->transform_negotiate, |
| 5779 | ssl->session_negotiate->ciphersuite, |
| 5780 | ssl->session_negotiate->master, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 5781 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 5782 | ssl->session_negotiate->encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 5783 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 5784 | ssl->handshake->tls_prf, |
| 5785 | ssl->handshake->randbytes, |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 5786 | ssl->tls_version, |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 5787 | ssl->conf->endpoint, |
| 5788 | ssl ); |
| 5789 | if( ret != 0 ) |
| 5790 | { |
| 5791 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret ); |
| 5792 | return( ret ); |
| 5793 | } |
| 5794 | |
| 5795 | /* We no longer need Server/ClientHello.random values */ |
| 5796 | mbedtls_platform_zeroize( ssl->handshake->randbytes, |
| 5797 | sizeof( ssl->handshake->randbytes ) ); |
| 5798 | |
| 5799 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) ); |
| 5800 | |
| 5801 | return( 0 ); |
| 5802 | } |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 5803 | |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 5804 | int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md ) |
| 5805 | { |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 5806 | switch( md ) |
| 5807 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5808 | #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] | 5809 | case MBEDTLS_SSL_HASH_SHA384: |
| 5810 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 5811 | break; |
| 5812 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5813 | #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] | 5814 | case MBEDTLS_SSL_HASH_SHA256: |
| 5815 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 5816 | break; |
| 5817 | #endif |
| 5818 | default: |
| 5819 | return( -1 ); |
| 5820 | } |
| 5821 | |
Ronald Cron | c2f13a0 | 2022-03-07 10:25:24 +0100 | [diff] [blame] | 5822 | return( 0 ); |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 5823 | } |
| 5824 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5825 | #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] | 5826 | void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl, |
| 5827 | unsigned char *hash, |
| 5828 | size_t *hlen ) |
| 5829 | { |
| 5830 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5831 | size_t hash_size; |
| 5832 | psa_status_t status; |
| 5833 | psa_hash_operation_t sha256_psa = psa_hash_operation_init(); |
| 5834 | |
| 5835 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) ); |
| 5836 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); |
| 5837 | if( status != PSA_SUCCESS ) |
| 5838 | { |
| 5839 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 5840 | return; |
| 5841 | } |
| 5842 | |
| 5843 | status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size ); |
| 5844 | if( status != PSA_SUCCESS ) |
| 5845 | { |
| 5846 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 5847 | return; |
| 5848 | } |
| 5849 | |
| 5850 | *hlen = 32; |
| 5851 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); |
| 5852 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); |
| 5853 | #else |
| 5854 | mbedtls_sha256_context sha256; |
| 5855 | |
| 5856 | mbedtls_sha256_init( &sha256 ); |
| 5857 | |
| 5858 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) ); |
| 5859 | |
| 5860 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
| 5861 | mbedtls_sha256_finish( &sha256, hash ); |
| 5862 | |
| 5863 | *hlen = 32; |
| 5864 | |
| 5865 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
| 5866 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
| 5867 | |
| 5868 | mbedtls_sha256_free( &sha256 ); |
| 5869 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5870 | return; |
| 5871 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5872 | #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] | 5873 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5874 | #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] | 5875 | void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl, |
| 5876 | unsigned char *hash, |
| 5877 | size_t *hlen ) |
| 5878 | { |
| 5879 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5880 | size_t hash_size; |
| 5881 | psa_status_t status; |
| 5882 | psa_hash_operation_t sha384_psa = psa_hash_operation_init(); |
| 5883 | |
| 5884 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) ); |
| 5885 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); |
| 5886 | if( status != PSA_SUCCESS ) |
| 5887 | { |
| 5888 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 5889 | return; |
| 5890 | } |
| 5891 | |
| 5892 | status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size ); |
| 5893 | if( status != PSA_SUCCESS ) |
| 5894 | { |
| 5895 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 5896 | return; |
| 5897 | } |
| 5898 | |
| 5899 | *hlen = 48; |
| 5900 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); |
| 5901 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); |
| 5902 | #else |
| 5903 | mbedtls_sha512_context sha512; |
| 5904 | |
| 5905 | mbedtls_sha512_init( &sha512 ); |
| 5906 | |
| 5907 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) ); |
| 5908 | |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 5909 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 ); |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 5910 | mbedtls_sha512_finish( &sha512, hash ); |
| 5911 | |
| 5912 | *hlen = 48; |
| 5913 | |
| 5914 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
| 5915 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
| 5916 | |
| 5917 | mbedtls_sha512_free( &sha512 ); |
| 5918 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5919 | return; |
| 5920 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5921 | #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] | 5922 | |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 5923 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 5924 | defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 5925 | int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex ) |
| 5926 | { |
| 5927 | unsigned char *p = ssl->handshake->premaster; |
| 5928 | unsigned char *end = p + sizeof( ssl->handshake->premaster ); |
| 5929 | const unsigned char *psk = NULL; |
| 5930 | size_t psk_len = 0; |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 5931 | int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len ); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 5932 | |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 5933 | if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ) |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 5934 | { |
| 5935 | /* |
| 5936 | * This should never happen because the existence of a PSK is always |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 5937 | * checked before calling this function. |
| 5938 | * |
| 5939 | * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with |
Przemek Stekiel | 8abcee9 | 2022-04-28 09:16:28 +0200 | [diff] [blame] | 5940 | * the shared secret without PSK. |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 5941 | */ |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 5942 | if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
| 5943 | { |
| 5944 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5945 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5946 | } |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 5947 | } |
| 5948 | |
| 5949 | /* |
| 5950 | * PMS = struct { |
| 5951 | * opaque other_secret<0..2^16-1>; |
| 5952 | * opaque psk<0..2^16-1>; |
| 5953 | * }; |
| 5954 | * with "other_secret" depending on the particular key exchange |
| 5955 | */ |
| 5956 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 5957 | if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK ) |
| 5958 | { |
| 5959 | if( end - p < 2 ) |
| 5960 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5961 | |
| 5962 | MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 ); |
| 5963 | p += 2; |
| 5964 | |
| 5965 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 5966 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5967 | |
| 5968 | memset( p, 0, psk_len ); |
| 5969 | p += psk_len; |
| 5970 | } |
| 5971 | else |
| 5972 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
| 5973 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
| 5974 | if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
| 5975 | { |
| 5976 | /* |
| 5977 | * other_secret already set by the ClientKeyExchange message, |
| 5978 | * and is 48 bytes long |
| 5979 | */ |
| 5980 | if( end - p < 2 ) |
| 5981 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5982 | |
| 5983 | *p++ = 0; |
| 5984 | *p++ = 48; |
| 5985 | p += 48; |
| 5986 | } |
| 5987 | else |
| 5988 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
| 5989 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 5990 | if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
| 5991 | { |
| 5992 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5993 | size_t len; |
| 5994 | |
| 5995 | /* Write length only when we know the actual value */ |
| 5996 | if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx, |
| 5997 | p + 2, end - ( p + 2 ), &len, |
| 5998 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
| 5999 | { |
| 6000 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); |
| 6001 | return( ret ); |
| 6002 | } |
| 6003 | MBEDTLS_PUT_UINT16_BE( len, p, 0 ); |
| 6004 | p += 2 + len; |
| 6005 | |
| 6006 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K ); |
| 6007 | } |
| 6008 | else |
| 6009 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6010 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6011 | if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
| 6012 | { |
| 6013 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6014 | size_t zlen; |
| 6015 | |
| 6016 | if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen, |
| 6017 | p + 2, end - ( p + 2 ), |
| 6018 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
| 6019 | { |
| 6020 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret ); |
| 6021 | return( ret ); |
| 6022 | } |
| 6023 | |
| 6024 | MBEDTLS_PUT_UINT16_BE( zlen, p, 0 ); |
| 6025 | p += 2 + zlen; |
| 6026 | |
| 6027 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 6028 | MBEDTLS_DEBUG_ECDH_Z ); |
| 6029 | } |
| 6030 | else |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6031 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6032 | { |
| 6033 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6034 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6035 | } |
| 6036 | |
| 6037 | /* opaque psk<0..2^16-1>; */ |
| 6038 | if( end - p < 2 ) |
| 6039 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6040 | |
| 6041 | MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 ); |
| 6042 | p += 2; |
| 6043 | |
| 6044 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 6045 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6046 | |
| 6047 | memcpy( p, psk, psk_len ); |
| 6048 | p += psk_len; |
| 6049 | |
| 6050 | ssl->handshake->pmslen = p - ssl->handshake->premaster; |
| 6051 | |
| 6052 | return( 0 ); |
| 6053 | } |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6054 | #endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 6055 | |
| 6056 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6057 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 6058 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ); |
| 6059 | |
| 6060 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6061 | int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl ) |
| 6062 | { |
| 6063 | /* If renegotiation is not enforced, retransmit until we would reach max |
| 6064 | * timeout if we were using the usual handshake doubling scheme */ |
| 6065 | if( ssl->conf->renego_max_records < 0 ) |
| 6066 | { |
| 6067 | uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1; |
| 6068 | unsigned char doublings = 1; |
| 6069 | |
| 6070 | while( ratio != 0 ) |
| 6071 | { |
| 6072 | ++doublings; |
| 6073 | ratio >>= 1; |
| 6074 | } |
| 6075 | |
| 6076 | if( ++ssl->renego_records_seen > doublings ) |
| 6077 | { |
| 6078 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) ); |
| 6079 | return( 0 ); |
| 6080 | } |
| 6081 | } |
| 6082 | |
| 6083 | return( ssl_write_hello_request( ssl ) ); |
| 6084 | } |
| 6085 | #endif |
| 6086 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6087 | |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6088 | /* |
| 6089 | * Handshake functions |
| 6090 | */ |
| 6091 | #if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 6092 | /* No certificate support -> dummy functions */ |
| 6093 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
| 6094 | { |
| 6095 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6096 | ssl->handshake->ciphersuite_info; |
| 6097 | |
| 6098 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
| 6099 | |
| 6100 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 6101 | { |
| 6102 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
| 6103 | ssl->state++; |
| 6104 | return( 0 ); |
| 6105 | } |
| 6106 | |
| 6107 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6108 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6109 | } |
| 6110 | |
| 6111 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
| 6112 | { |
| 6113 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6114 | ssl->handshake->ciphersuite_info; |
| 6115 | |
| 6116 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
| 6117 | |
| 6118 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 6119 | { |
| 6120 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
| 6121 | ssl->state++; |
| 6122 | return( 0 ); |
| 6123 | } |
| 6124 | |
| 6125 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6126 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6127 | } |
| 6128 | |
| 6129 | #else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 6130 | /* Some certificate support -> implement write and parse */ |
| 6131 | |
| 6132 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
| 6133 | { |
| 6134 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 6135 | size_t i, n; |
| 6136 | const mbedtls_x509_crt *crt; |
| 6137 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6138 | ssl->handshake->ciphersuite_info; |
| 6139 | |
| 6140 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
| 6141 | |
| 6142 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 6143 | { |
| 6144 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
| 6145 | ssl->state++; |
| 6146 | return( 0 ); |
| 6147 | } |
| 6148 | |
| 6149 | #if defined(MBEDTLS_SSL_CLI_C) |
| 6150 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 6151 | { |
| 6152 | if( ssl->handshake->client_auth == 0 ) |
| 6153 | { |
| 6154 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
| 6155 | ssl->state++; |
| 6156 | return( 0 ); |
| 6157 | } |
| 6158 | } |
| 6159 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 6160 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6161 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 6162 | { |
| 6163 | if( mbedtls_ssl_own_cert( ssl ) == NULL ) |
| 6164 | { |
| 6165 | /* Should never happen because we shouldn't have picked the |
| 6166 | * ciphersuite if we don't have a certificate. */ |
| 6167 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6168 | } |
| 6169 | } |
| 6170 | #endif |
| 6171 | |
| 6172 | MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) ); |
| 6173 | |
| 6174 | /* |
| 6175 | * 0 . 0 handshake type |
| 6176 | * 1 . 3 handshake length |
| 6177 | * 4 . 6 length of all certs |
| 6178 | * 7 . 9 length of cert. 1 |
| 6179 | * 10 . n-1 peer certificate |
| 6180 | * n . n+2 length of cert. 2 |
| 6181 | * n+3 . ... upper level cert, etc. |
| 6182 | */ |
| 6183 | i = 7; |
| 6184 | crt = mbedtls_ssl_own_cert( ssl ); |
| 6185 | |
| 6186 | while( crt != NULL ) |
| 6187 | { |
| 6188 | n = crt->raw.len; |
| 6189 | if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i ) |
| 6190 | { |
| 6191 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET |
| 6192 | " > %" MBEDTLS_PRINTF_SIZET, |
| 6193 | i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) ); |
| 6194 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 6195 | } |
| 6196 | |
| 6197 | ssl->out_msg[i ] = MBEDTLS_BYTE_2( n ); |
| 6198 | ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n ); |
| 6199 | ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n ); |
| 6200 | |
| 6201 | i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n ); |
| 6202 | i += n; crt = crt->next; |
| 6203 | } |
| 6204 | |
| 6205 | ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 ); |
| 6206 | ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 ); |
| 6207 | ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 ); |
| 6208 | |
| 6209 | ssl->out_msglen = i; |
| 6210 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 6211 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE; |
| 6212 | |
| 6213 | ssl->state++; |
| 6214 | |
| 6215 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
| 6216 | { |
| 6217 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
| 6218 | return( ret ); |
| 6219 | } |
| 6220 | |
| 6221 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) ); |
| 6222 | |
| 6223 | return( ret ); |
| 6224 | } |
| 6225 | |
| 6226 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 6227 | |
| 6228 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6229 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6230 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 6231 | unsigned char *crt_buf, |
| 6232 | size_t crt_buf_len ) |
| 6233 | { |
| 6234 | mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert; |
| 6235 | |
| 6236 | if( peer_crt == NULL ) |
| 6237 | return( -1 ); |
| 6238 | |
| 6239 | if( peer_crt->raw.len != crt_buf_len ) |
| 6240 | return( -1 ); |
| 6241 | |
| 6242 | return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) ); |
| 6243 | } |
| 6244 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6245 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6246 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 6247 | unsigned char *crt_buf, |
| 6248 | size_t crt_buf_len ) |
| 6249 | { |
| 6250 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6251 | unsigned char const * const peer_cert_digest = |
| 6252 | ssl->session->peer_cert_digest; |
| 6253 | mbedtls_md_type_t const peer_cert_digest_type = |
| 6254 | ssl->session->peer_cert_digest_type; |
| 6255 | mbedtls_md_info_t const * const digest_info = |
| 6256 | mbedtls_md_info_from_type( peer_cert_digest_type ); |
| 6257 | unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN]; |
| 6258 | size_t digest_len; |
| 6259 | |
| 6260 | if( peer_cert_digest == NULL || digest_info == NULL ) |
| 6261 | return( -1 ); |
| 6262 | |
| 6263 | digest_len = mbedtls_md_get_size( digest_info ); |
| 6264 | if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN ) |
| 6265 | return( -1 ); |
| 6266 | |
| 6267 | ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest ); |
| 6268 | if( ret != 0 ) |
| 6269 | return( -1 ); |
| 6270 | |
| 6271 | return( memcmp( tmp_digest, peer_cert_digest, digest_len ) ); |
| 6272 | } |
| 6273 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6274 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 6275 | |
| 6276 | /* |
| 6277 | * Once the certificate message is read, parse it into a cert chain and |
| 6278 | * perform basic checks, but leave actual verification to the caller |
| 6279 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6280 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6281 | static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl, |
| 6282 | mbedtls_x509_crt *chain ) |
| 6283 | { |
| 6284 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6285 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 6286 | int crt_cnt=0; |
| 6287 | #endif |
| 6288 | size_t i, n; |
| 6289 | uint8_t alert; |
| 6290 | |
| 6291 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
| 6292 | { |
| 6293 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6294 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6295 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 6296 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 6297 | } |
| 6298 | |
| 6299 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ) |
| 6300 | { |
| 6301 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6302 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 6303 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 6304 | } |
| 6305 | |
| 6306 | if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 ) |
| 6307 | { |
| 6308 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6309 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6310 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 6311 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 6312 | } |
| 6313 | |
| 6314 | i = mbedtls_ssl_hs_hdr_len( ssl ); |
| 6315 | |
| 6316 | /* |
| 6317 | * Same message structure as in mbedtls_ssl_write_certificate() |
| 6318 | */ |
| 6319 | n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2]; |
| 6320 | |
| 6321 | if( ssl->in_msg[i] != 0 || |
| 6322 | ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) ) |
| 6323 | { |
| 6324 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6325 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6326 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 6327 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 6328 | } |
| 6329 | |
| 6330 | /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */ |
| 6331 | i += 3; |
| 6332 | |
| 6333 | /* Iterate through and parse the CRTs in the provided chain. */ |
| 6334 | while( i < ssl->in_hslen ) |
| 6335 | { |
| 6336 | /* Check that there's room for the next CRT's length fields. */ |
| 6337 | if ( i + 3 > ssl->in_hslen ) { |
| 6338 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6339 | mbedtls_ssl_send_alert_message( ssl, |
| 6340 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6341 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 6342 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 6343 | } |
| 6344 | /* In theory, the CRT can be up to 2**24 Bytes, but we don't support |
| 6345 | * anything beyond 2**16 ~ 64K. */ |
| 6346 | if( ssl->in_msg[i] != 0 ) |
| 6347 | { |
| 6348 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6349 | mbedtls_ssl_send_alert_message( ssl, |
| 6350 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6351 | MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT ); |
| 6352 | return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE ); |
| 6353 | } |
| 6354 | |
| 6355 | /* Read length of the next CRT in the chain. */ |
| 6356 | n = ( (unsigned int) ssl->in_msg[i + 1] << 8 ) |
| 6357 | | (unsigned int) ssl->in_msg[i + 2]; |
| 6358 | i += 3; |
| 6359 | |
| 6360 | if( n < 128 || i + n > ssl->in_hslen ) |
| 6361 | { |
| 6362 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6363 | mbedtls_ssl_send_alert_message( ssl, |
| 6364 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6365 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 6366 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 6367 | } |
| 6368 | |
| 6369 | /* Check if we're handling the first CRT in the chain. */ |
| 6370 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 6371 | if( crt_cnt++ == 0 && |
| 6372 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 6373 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
| 6374 | { |
| 6375 | /* During client-side renegotiation, check that the server's |
| 6376 | * end-CRTs hasn't changed compared to the initial handshake, |
| 6377 | * mitigating the triple handshake attack. On success, reuse |
| 6378 | * the original end-CRT instead of parsing it again. */ |
| 6379 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) ); |
| 6380 | if( ssl_check_peer_crt_unchanged( ssl, |
| 6381 | &ssl->in_msg[i], |
| 6382 | n ) != 0 ) |
| 6383 | { |
| 6384 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) ); |
| 6385 | mbedtls_ssl_send_alert_message( ssl, |
| 6386 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6387 | MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED ); |
| 6388 | return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE ); |
| 6389 | } |
| 6390 | |
| 6391 | /* Now we can safely free the original chain. */ |
| 6392 | ssl_clear_peer_cert( ssl->session ); |
| 6393 | } |
| 6394 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 6395 | |
| 6396 | /* Parse the next certificate in the chain. */ |
| 6397 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 6398 | ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n ); |
| 6399 | #else |
| 6400 | /* If we don't need to store the CRT chain permanently, parse |
| 6401 | * it in-place from the input buffer instead of making a copy. */ |
| 6402 | ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n ); |
| 6403 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6404 | switch( ret ) |
| 6405 | { |
| 6406 | case 0: /*ok*/ |
| 6407 | case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND: |
| 6408 | /* Ignore certificate with an unknown algorithm: maybe a |
| 6409 | prior certificate was already trusted. */ |
| 6410 | break; |
| 6411 | |
| 6412 | case MBEDTLS_ERR_X509_ALLOC_FAILED: |
| 6413 | alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR; |
| 6414 | goto crt_parse_der_failed; |
| 6415 | |
| 6416 | case MBEDTLS_ERR_X509_UNKNOWN_VERSION: |
| 6417 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6418 | goto crt_parse_der_failed; |
| 6419 | |
| 6420 | default: |
| 6421 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 6422 | crt_parse_der_failed: |
| 6423 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert ); |
| 6424 | MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret ); |
| 6425 | return( ret ); |
| 6426 | } |
| 6427 | |
| 6428 | i += n; |
| 6429 | } |
| 6430 | |
| 6431 | MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain ); |
| 6432 | return( 0 ); |
| 6433 | } |
| 6434 | |
| 6435 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6436 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6437 | static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl ) |
| 6438 | { |
| 6439 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 6440 | return( -1 ); |
| 6441 | |
| 6442 | if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) && |
| 6443 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 6444 | ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE && |
| 6445 | memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 ) |
| 6446 | { |
Ronald Cron | 1938588 | 2022-06-15 16:26:13 +0200 | [diff] [blame] | 6447 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) ); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6448 | return( 0 ); |
| 6449 | } |
| 6450 | return( -1 ); |
| 6451 | } |
| 6452 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 6453 | |
| 6454 | /* Check if a certificate message is expected. |
| 6455 | * Return either |
| 6456 | * - SSL_CERTIFICATE_EXPECTED, or |
| 6457 | * - SSL_CERTIFICATE_SKIP |
| 6458 | * indicating whether a Certificate message is expected or not. |
| 6459 | */ |
| 6460 | #define SSL_CERTIFICATE_EXPECTED 0 |
| 6461 | #define SSL_CERTIFICATE_SKIP 1 |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6462 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6463 | static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl, |
| 6464 | int authmode ) |
| 6465 | { |
| 6466 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6467 | ssl->handshake->ciphersuite_info; |
| 6468 | |
| 6469 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 6470 | return( SSL_CERTIFICATE_SKIP ); |
| 6471 | |
| 6472 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6473 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 6474 | { |
| 6475 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
| 6476 | return( SSL_CERTIFICATE_SKIP ); |
| 6477 | |
| 6478 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 6479 | { |
| 6480 | ssl->session_negotiate->verify_result = |
| 6481 | MBEDTLS_X509_BADCERT_SKIP_VERIFY; |
| 6482 | return( SSL_CERTIFICATE_SKIP ); |
| 6483 | } |
| 6484 | } |
| 6485 | #else |
| 6486 | ((void) authmode); |
| 6487 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 6488 | |
| 6489 | return( SSL_CERTIFICATE_EXPECTED ); |
| 6490 | } |
| 6491 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6492 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6493 | static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl, |
| 6494 | int authmode, |
| 6495 | mbedtls_x509_crt *chain, |
| 6496 | void *rs_ctx ) |
| 6497 | { |
| 6498 | int ret = 0; |
| 6499 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6500 | ssl->handshake->ciphersuite_info; |
| 6501 | int have_ca_chain = 0; |
| 6502 | |
| 6503 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *); |
| 6504 | void *p_vrfy; |
| 6505 | |
| 6506 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 6507 | return( 0 ); |
| 6508 | |
| 6509 | if( ssl->f_vrfy != NULL ) |
| 6510 | { |
| 6511 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) ); |
| 6512 | f_vrfy = ssl->f_vrfy; |
| 6513 | p_vrfy = ssl->p_vrfy; |
| 6514 | } |
| 6515 | else |
| 6516 | { |
| 6517 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) ); |
| 6518 | f_vrfy = ssl->conf->f_vrfy; |
| 6519 | p_vrfy = ssl->conf->p_vrfy; |
| 6520 | } |
| 6521 | |
| 6522 | /* |
| 6523 | * Main check: verify certificate |
| 6524 | */ |
| 6525 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 6526 | if( ssl->conf->f_ca_cb != NULL ) |
| 6527 | { |
| 6528 | ((void) rs_ctx); |
| 6529 | have_ca_chain = 1; |
| 6530 | |
| 6531 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) ); |
| 6532 | ret = mbedtls_x509_crt_verify_with_ca_cb( |
| 6533 | chain, |
| 6534 | ssl->conf->f_ca_cb, |
| 6535 | ssl->conf->p_ca_cb, |
| 6536 | ssl->conf->cert_profile, |
| 6537 | ssl->hostname, |
| 6538 | &ssl->session_negotiate->verify_result, |
| 6539 | f_vrfy, p_vrfy ); |
| 6540 | } |
| 6541 | else |
| 6542 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 6543 | { |
| 6544 | mbedtls_x509_crt *ca_chain; |
| 6545 | mbedtls_x509_crl *ca_crl; |
| 6546 | |
| 6547 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 6548 | if( ssl->handshake->sni_ca_chain != NULL ) |
| 6549 | { |
| 6550 | ca_chain = ssl->handshake->sni_ca_chain; |
| 6551 | ca_crl = ssl->handshake->sni_ca_crl; |
| 6552 | } |
| 6553 | else |
| 6554 | #endif |
| 6555 | { |
| 6556 | ca_chain = ssl->conf->ca_chain; |
| 6557 | ca_crl = ssl->conf->ca_crl; |
| 6558 | } |
| 6559 | |
| 6560 | if( ca_chain != NULL ) |
| 6561 | have_ca_chain = 1; |
| 6562 | |
| 6563 | ret = mbedtls_x509_crt_verify_restartable( |
| 6564 | chain, |
| 6565 | ca_chain, ca_crl, |
| 6566 | ssl->conf->cert_profile, |
| 6567 | ssl->hostname, |
| 6568 | &ssl->session_negotiate->verify_result, |
| 6569 | f_vrfy, p_vrfy, rs_ctx ); |
| 6570 | } |
| 6571 | |
| 6572 | if( ret != 0 ) |
| 6573 | { |
| 6574 | MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret ); |
| 6575 | } |
| 6576 | |
| 6577 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 6578 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 6579 | return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ); |
| 6580 | #endif |
| 6581 | |
| 6582 | /* |
| 6583 | * Secondary checks: always done, but change 'ret' only if it was 0 |
| 6584 | */ |
| 6585 | |
| 6586 | #if defined(MBEDTLS_ECP_C) |
| 6587 | { |
| 6588 | const mbedtls_pk_context *pk = &chain->pk; |
| 6589 | |
Manuel Pégourié-Gonnard | 66b0d61 | 2022-06-17 10:49:29 +0200 | [diff] [blame] | 6590 | /* If certificate uses an EC key, make sure the curve is OK. |
| 6591 | * This is a public key, so it can't be opaque, so can_do() is a good |
| 6592 | * enough check to ensure pk_ec() is safe to use here. */ |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 6593 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) ) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6594 | { |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 6595 | /* and in the unlikely case the above assumption no longer holds |
| 6596 | * we are making sure that pk_ec() here does not return a NULL |
| 6597 | */ |
| 6598 | const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk ); |
| 6599 | if( ec == NULL ) |
| 6600 | { |
Tom Cosgrove | 20c1137 | 2022-08-24 15:06:13 +0100 | [diff] [blame] | 6601 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) ); |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 6602 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6603 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6604 | |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 6605 | if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 ) |
| 6606 | { |
| 6607 | ssl->session_negotiate->verify_result |= |
| 6608 | MBEDTLS_X509_BADCERT_BAD_KEY; |
| 6609 | |
| 6610 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) ); |
| 6611 | if( ret == 0 ) |
| 6612 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
| 6613 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6614 | } |
| 6615 | } |
| 6616 | #endif /* MBEDTLS_ECP_C */ |
| 6617 | |
| 6618 | if( mbedtls_ssl_check_cert_usage( chain, |
| 6619 | ciphersuite_info, |
| 6620 | ! ssl->conf->endpoint, |
| 6621 | &ssl->session_negotiate->verify_result ) != 0 ) |
| 6622 | { |
| 6623 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) ); |
| 6624 | if( ret == 0 ) |
| 6625 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
| 6626 | } |
| 6627 | |
| 6628 | /* mbedtls_x509_crt_verify_with_profile is supposed to report a |
| 6629 | * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED, |
| 6630 | * with details encoded in the verification flags. All other kinds |
| 6631 | * of error codes, including those from the user provided f_vrfy |
| 6632 | * functions, are treated as fatal and lead to a failure of |
| 6633 | * ssl_parse_certificate even if verification was optional. */ |
| 6634 | if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL && |
| 6635 | ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED || |
| 6636 | ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) ) |
| 6637 | { |
| 6638 | ret = 0; |
| 6639 | } |
| 6640 | |
| 6641 | if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED ) |
| 6642 | { |
| 6643 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) ); |
| 6644 | ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED; |
| 6645 | } |
| 6646 | |
| 6647 | if( ret != 0 ) |
| 6648 | { |
| 6649 | uint8_t alert; |
| 6650 | |
| 6651 | /* The certificate may have been rejected for several reasons. |
| 6652 | Pick one and send the corresponding alert. Which alert to send |
| 6653 | may be a subject of debate in some cases. */ |
| 6654 | if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER ) |
| 6655 | alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED; |
| 6656 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH ) |
| 6657 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 6658 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE ) |
| 6659 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6660 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE ) |
| 6661 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6662 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE ) |
| 6663 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6664 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK ) |
| 6665 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6666 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY ) |
| 6667 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6668 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED ) |
| 6669 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED; |
| 6670 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED ) |
| 6671 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED; |
| 6672 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED ) |
| 6673 | alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA; |
| 6674 | else |
| 6675 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN; |
| 6676 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6677 | alert ); |
| 6678 | } |
| 6679 | |
| 6680 | #if defined(MBEDTLS_DEBUG_C) |
| 6681 | if( ssl->session_negotiate->verify_result != 0 ) |
| 6682 | { |
| 6683 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x", |
| 6684 | (unsigned int) ssl->session_negotiate->verify_result ) ); |
| 6685 | } |
| 6686 | else |
| 6687 | { |
| 6688 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) ); |
| 6689 | } |
| 6690 | #endif /* MBEDTLS_DEBUG_C */ |
| 6691 | |
| 6692 | return( ret ); |
| 6693 | } |
| 6694 | |
| 6695 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6696 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6697 | static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl, |
| 6698 | unsigned char *start, size_t len ) |
| 6699 | { |
| 6700 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6701 | /* Remember digest of the peer's end-CRT. */ |
| 6702 | ssl->session_negotiate->peer_cert_digest = |
| 6703 | mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ); |
| 6704 | if( ssl->session_negotiate->peer_cert_digest == NULL ) |
| 6705 | { |
| 6706 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", |
| 6707 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ); |
| 6708 | mbedtls_ssl_send_alert_message( ssl, |
| 6709 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6710 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 6711 | |
| 6712 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 6713 | } |
| 6714 | |
| 6715 | ret = mbedtls_md( mbedtls_md_info_from_type( |
| 6716 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ), |
| 6717 | start, len, |
| 6718 | ssl->session_negotiate->peer_cert_digest ); |
| 6719 | |
| 6720 | ssl->session_negotiate->peer_cert_digest_type = |
| 6721 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; |
| 6722 | ssl->session_negotiate->peer_cert_digest_len = |
| 6723 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; |
| 6724 | |
| 6725 | return( ret ); |
| 6726 | } |
| 6727 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6728 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6729 | static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl, |
| 6730 | unsigned char *start, size_t len ) |
| 6731 | { |
| 6732 | unsigned char *end = start + len; |
| 6733 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6734 | |
| 6735 | /* Make a copy of the peer's raw public key. */ |
| 6736 | mbedtls_pk_init( &ssl->handshake->peer_pubkey ); |
| 6737 | ret = mbedtls_pk_parse_subpubkey( &start, end, |
| 6738 | &ssl->handshake->peer_pubkey ); |
| 6739 | if( ret != 0 ) |
| 6740 | { |
| 6741 | /* We should have parsed the public key before. */ |
| 6742 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6743 | } |
| 6744 | |
| 6745 | return( 0 ); |
| 6746 | } |
| 6747 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6748 | |
| 6749 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
| 6750 | { |
| 6751 | int ret = 0; |
| 6752 | int crt_expected; |
| 6753 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 6754 | const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET |
| 6755 | ? ssl->handshake->sni_authmode |
| 6756 | : ssl->conf->authmode; |
| 6757 | #else |
| 6758 | const int authmode = ssl->conf->authmode; |
| 6759 | #endif |
| 6760 | void *rs_ctx = NULL; |
| 6761 | mbedtls_x509_crt *chain = NULL; |
| 6762 | |
| 6763 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
| 6764 | |
| 6765 | crt_expected = ssl_parse_certificate_coordinate( ssl, authmode ); |
| 6766 | if( crt_expected == SSL_CERTIFICATE_SKIP ) |
| 6767 | { |
| 6768 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
| 6769 | goto exit; |
| 6770 | } |
| 6771 | |
| 6772 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 6773 | if( ssl->handshake->ecrs_enabled && |
| 6774 | ssl->handshake->ecrs_state == ssl_ecrs_crt_verify ) |
| 6775 | { |
| 6776 | chain = ssl->handshake->ecrs_peer_cert; |
| 6777 | ssl->handshake->ecrs_peer_cert = NULL; |
| 6778 | goto crt_verify; |
| 6779 | } |
| 6780 | #endif |
| 6781 | |
| 6782 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
| 6783 | { |
| 6784 | /* mbedtls_ssl_read_record may have sent an alert already. We |
| 6785 | let it decide whether to alert. */ |
| 6786 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 6787 | goto exit; |
| 6788 | } |
| 6789 | |
| 6790 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6791 | if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 ) |
| 6792 | { |
| 6793 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING; |
| 6794 | |
| 6795 | if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL ) |
| 6796 | ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE; |
| 6797 | |
| 6798 | goto exit; |
| 6799 | } |
| 6800 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 6801 | |
| 6802 | /* Clear existing peer CRT structure in case we tried to |
| 6803 | * reuse a session but it failed, and allocate a new one. */ |
| 6804 | ssl_clear_peer_cert( ssl->session_negotiate ); |
| 6805 | |
| 6806 | chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 6807 | if( chain == NULL ) |
| 6808 | { |
| 6809 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", |
| 6810 | sizeof( mbedtls_x509_crt ) ) ); |
| 6811 | mbedtls_ssl_send_alert_message( ssl, |
| 6812 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6813 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 6814 | |
| 6815 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 6816 | goto exit; |
| 6817 | } |
| 6818 | mbedtls_x509_crt_init( chain ); |
| 6819 | |
| 6820 | ret = ssl_parse_certificate_chain( ssl, chain ); |
| 6821 | if( ret != 0 ) |
| 6822 | goto exit; |
| 6823 | |
| 6824 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 6825 | if( ssl->handshake->ecrs_enabled) |
| 6826 | ssl->handshake->ecrs_state = ssl_ecrs_crt_verify; |
| 6827 | |
| 6828 | crt_verify: |
| 6829 | if( ssl->handshake->ecrs_enabled) |
| 6830 | rs_ctx = &ssl->handshake->ecrs_ctx; |
| 6831 | #endif |
| 6832 | |
| 6833 | ret = ssl_parse_certificate_verify( ssl, authmode, |
| 6834 | chain, rs_ctx ); |
| 6835 | if( ret != 0 ) |
| 6836 | goto exit; |
| 6837 | |
| 6838 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 6839 | { |
| 6840 | unsigned char *crt_start, *pk_start; |
| 6841 | size_t crt_len, pk_len; |
| 6842 | |
| 6843 | /* We parse the CRT chain without copying, so |
| 6844 | * these pointers point into the input buffer, |
| 6845 | * and are hence still valid after freeing the |
| 6846 | * CRT chain. */ |
| 6847 | |
| 6848 | crt_start = chain->raw.p; |
| 6849 | crt_len = chain->raw.len; |
| 6850 | |
| 6851 | pk_start = chain->pk_raw.p; |
| 6852 | pk_len = chain->pk_raw.len; |
| 6853 | |
| 6854 | /* Free the CRT structures before computing |
| 6855 | * digest and copying the peer's public key. */ |
| 6856 | mbedtls_x509_crt_free( chain ); |
| 6857 | mbedtls_free( chain ); |
| 6858 | chain = NULL; |
| 6859 | |
| 6860 | ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len ); |
| 6861 | if( ret != 0 ) |
| 6862 | goto exit; |
| 6863 | |
| 6864 | ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len ); |
| 6865 | if( ret != 0 ) |
| 6866 | goto exit; |
| 6867 | } |
| 6868 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6869 | /* Pass ownership to session structure. */ |
| 6870 | ssl->session_negotiate->peer_cert = chain; |
| 6871 | chain = NULL; |
| 6872 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6873 | |
| 6874 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) ); |
| 6875 | |
| 6876 | exit: |
| 6877 | |
| 6878 | if( ret == 0 ) |
| 6879 | ssl->state++; |
| 6880 | |
| 6881 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 6882 | if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ) |
| 6883 | { |
| 6884 | ssl->handshake->ecrs_peer_cert = chain; |
| 6885 | chain = NULL; |
| 6886 | } |
| 6887 | #endif |
| 6888 | |
| 6889 | if( chain != NULL ) |
| 6890 | { |
| 6891 | mbedtls_x509_crt_free( chain ); |
| 6892 | mbedtls_free( chain ); |
| 6893 | } |
| 6894 | |
| 6895 | return( ret ); |
| 6896 | } |
| 6897 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 6898 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6899 | #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] | 6900 | static void ssl_calc_finished_tls_sha256( |
| 6901 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
| 6902 | { |
| 6903 | int len = 12; |
| 6904 | const char *sender; |
| 6905 | unsigned char padbuf[32]; |
| 6906 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6907 | size_t hash_size; |
| 6908 | psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT; |
| 6909 | psa_status_t status; |
| 6910 | #else |
| 6911 | mbedtls_sha256_context sha256; |
| 6912 | #endif |
| 6913 | |
| 6914 | mbedtls_ssl_session *session = ssl->session_negotiate; |
| 6915 | if( !session ) |
| 6916 | session = ssl->session; |
| 6917 | |
| 6918 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
| 6919 | ? "client finished" |
| 6920 | : "server finished"; |
| 6921 | |
| 6922 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6923 | sha256_psa = psa_hash_operation_init(); |
| 6924 | |
| 6925 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) ); |
| 6926 | |
| 6927 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); |
| 6928 | if( status != PSA_SUCCESS ) |
| 6929 | { |
| 6930 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 6931 | return; |
| 6932 | } |
| 6933 | |
| 6934 | status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size ); |
| 6935 | if( status != PSA_SUCCESS ) |
| 6936 | { |
| 6937 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 6938 | return; |
| 6939 | } |
| 6940 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 ); |
| 6941 | #else |
| 6942 | |
| 6943 | mbedtls_sha256_init( &sha256 ); |
| 6944 | |
| 6945 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) ); |
| 6946 | |
| 6947 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
| 6948 | |
| 6949 | /* |
| 6950 | * TLSv1.2: |
| 6951 | * hash = PRF( master, finished_label, |
| 6952 | * Hash( handshake ) )[0.11] |
| 6953 | */ |
| 6954 | |
| 6955 | #if !defined(MBEDTLS_SHA256_ALT) |
| 6956 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *) |
| 6957 | sha256.state, sizeof( sha256.state ) ); |
| 6958 | #endif |
| 6959 | |
| 6960 | mbedtls_sha256_finish( &sha256, padbuf ); |
| 6961 | mbedtls_sha256_free( &sha256 ); |
| 6962 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 6963 | |
| 6964 | ssl->handshake->tls_prf( session->master, 48, sender, |
| 6965 | padbuf, 32, buf, len ); |
| 6966 | |
| 6967 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
| 6968 | |
| 6969 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
| 6970 | |
| 6971 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
| 6972 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 6973 | #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] | 6974 | |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 6975 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6976 | #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] | 6977 | static void ssl_calc_finished_tls_sha384( |
| 6978 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
| 6979 | { |
| 6980 | int len = 12; |
| 6981 | const char *sender; |
| 6982 | unsigned char padbuf[48]; |
| 6983 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6984 | size_t hash_size; |
| 6985 | psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT; |
| 6986 | psa_status_t status; |
| 6987 | #else |
| 6988 | mbedtls_sha512_context sha512; |
| 6989 | #endif |
| 6990 | |
| 6991 | mbedtls_ssl_session *session = ssl->session_negotiate; |
| 6992 | if( !session ) |
| 6993 | session = ssl->session; |
| 6994 | |
| 6995 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
| 6996 | ? "client finished" |
| 6997 | : "server finished"; |
| 6998 | |
| 6999 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7000 | sha384_psa = psa_hash_operation_init(); |
| 7001 | |
| 7002 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) ); |
| 7003 | |
| 7004 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); |
| 7005 | if( status != PSA_SUCCESS ) |
| 7006 | { |
| 7007 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 7008 | return; |
| 7009 | } |
| 7010 | |
| 7011 | status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size ); |
| 7012 | if( status != PSA_SUCCESS ) |
| 7013 | { |
| 7014 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 7015 | return; |
| 7016 | } |
| 7017 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 ); |
| 7018 | #else |
| 7019 | mbedtls_sha512_init( &sha512 ); |
| 7020 | |
| 7021 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) ); |
| 7022 | |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 7023 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 ); |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7024 | |
| 7025 | /* |
| 7026 | * TLSv1.2: |
| 7027 | * hash = PRF( master, finished_label, |
| 7028 | * Hash( handshake ) )[0.11] |
| 7029 | */ |
| 7030 | |
| 7031 | #if !defined(MBEDTLS_SHA512_ALT) |
| 7032 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *) |
| 7033 | sha512.state, sizeof( sha512.state ) ); |
| 7034 | #endif |
| 7035 | mbedtls_sha512_finish( &sha512, padbuf ); |
| 7036 | |
| 7037 | mbedtls_sha512_free( &sha512 ); |
| 7038 | #endif |
| 7039 | |
| 7040 | ssl->handshake->tls_prf( session->master, 48, sender, |
| 7041 | padbuf, 48, buf, len ); |
| 7042 | |
| 7043 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
| 7044 | |
| 7045 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
| 7046 | |
| 7047 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
| 7048 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 7049 | #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] | 7050 | |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 7051 | void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl ) |
| 7052 | { |
| 7053 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) ); |
| 7054 | |
| 7055 | /* |
| 7056 | * Free our handshake params |
| 7057 | */ |
| 7058 | mbedtls_ssl_handshake_free( ssl ); |
| 7059 | mbedtls_free( ssl->handshake ); |
| 7060 | ssl->handshake = NULL; |
| 7061 | |
| 7062 | /* |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 7063 | * Free the previous transform and switch in the current one |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 7064 | */ |
| 7065 | if( ssl->transform ) |
| 7066 | { |
| 7067 | mbedtls_ssl_transform_free( ssl->transform ); |
| 7068 | mbedtls_free( ssl->transform ); |
| 7069 | } |
| 7070 | ssl->transform = ssl->transform_negotiate; |
| 7071 | ssl->transform_negotiate = NULL; |
| 7072 | |
| 7073 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) ); |
| 7074 | } |
| 7075 | |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7076 | void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl ) |
| 7077 | { |
| 7078 | int resume = ssl->handshake->resume; |
| 7079 | |
| 7080 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) ); |
| 7081 | |
| 7082 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 7083 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
| 7084 | { |
| 7085 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE; |
| 7086 | ssl->renego_records_seen = 0; |
| 7087 | } |
| 7088 | #endif |
| 7089 | |
| 7090 | /* |
| 7091 | * Free the previous session and switch in the current one |
| 7092 | */ |
| 7093 | if( ssl->session ) |
| 7094 | { |
| 7095 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 7096 | /* RFC 7366 3.1: keep the EtM state */ |
| 7097 | ssl->session_negotiate->encrypt_then_mac = |
| 7098 | ssl->session->encrypt_then_mac; |
| 7099 | #endif |
| 7100 | |
| 7101 | mbedtls_ssl_session_free( ssl->session ); |
| 7102 | mbedtls_free( ssl->session ); |
| 7103 | } |
| 7104 | ssl->session = ssl->session_negotiate; |
| 7105 | ssl->session_negotiate = NULL; |
| 7106 | |
| 7107 | /* |
| 7108 | * Add cache entry |
| 7109 | */ |
| 7110 | if( ssl->conf->f_set_cache != NULL && |
| 7111 | ssl->session->id_len != 0 && |
| 7112 | resume == 0 ) |
| 7113 | { |
| 7114 | if( ssl->conf->f_set_cache( ssl->conf->p_cache, |
| 7115 | ssl->session->id, |
| 7116 | ssl->session->id_len, |
| 7117 | ssl->session ) != 0 ) |
| 7118 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) ); |
| 7119 | } |
| 7120 | |
| 7121 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7122 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 7123 | ssl->handshake->flight != NULL ) |
| 7124 | { |
| 7125 | /* Cancel handshake timer */ |
| 7126 | mbedtls_ssl_set_timer( ssl, 0 ); |
| 7127 | |
| 7128 | /* Keep last flight around in case we need to resend it: |
| 7129 | * we need the handshake and transform structures for that */ |
| 7130 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) ); |
| 7131 | } |
| 7132 | else |
| 7133 | #endif |
| 7134 | mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl ); |
| 7135 | |
| 7136 | ssl->state++; |
| 7137 | |
| 7138 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) ); |
| 7139 | } |
| 7140 | |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7141 | int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl ) |
| 7142 | { |
| 7143 | int ret, hash_len; |
| 7144 | |
| 7145 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) ); |
| 7146 | |
| 7147 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate ); |
| 7148 | |
| 7149 | ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint ); |
| 7150 | |
| 7151 | /* |
| 7152 | * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites |
| 7153 | * may define some other value. Currently (early 2016), no defined |
| 7154 | * ciphersuite does this (and this is unlikely to change as activity has |
| 7155 | * moved to TLS 1.3 now) so we can keep the hardcoded 12 here. |
| 7156 | */ |
| 7157 | hash_len = 12; |
| 7158 | |
| 7159 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 7160 | ssl->verify_data_len = hash_len; |
| 7161 | memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len ); |
| 7162 | #endif |
| 7163 | |
| 7164 | ssl->out_msglen = 4 + hash_len; |
| 7165 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 7166 | ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED; |
| 7167 | |
| 7168 | /* |
| 7169 | * In case of session resuming, invert the client and server |
| 7170 | * ChangeCipherSpec messages order. |
| 7171 | */ |
| 7172 | if( ssl->handshake->resume != 0 ) |
| 7173 | { |
| 7174 | #if defined(MBEDTLS_SSL_CLI_C) |
| 7175 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 7176 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
| 7177 | #endif |
| 7178 | #if defined(MBEDTLS_SSL_SRV_C) |
| 7179 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 7180 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
| 7181 | #endif |
| 7182 | } |
| 7183 | else |
| 7184 | ssl->state++; |
| 7185 | |
| 7186 | /* |
| 7187 | * Switch to our negotiated transform and session parameters for outbound |
| 7188 | * data. |
| 7189 | */ |
| 7190 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) ); |
| 7191 | |
| 7192 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7193 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 7194 | { |
| 7195 | unsigned char i; |
| 7196 | |
| 7197 | /* Remember current epoch settings for resending */ |
| 7198 | ssl->handshake->alt_transform_out = ssl->transform_out; |
| 7199 | memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, |
| 7200 | sizeof( ssl->handshake->alt_out_ctr ) ); |
| 7201 | |
| 7202 | /* Set sequence_number to zero */ |
| 7203 | memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 ); |
| 7204 | |
| 7205 | |
| 7206 | /* Increment epoch */ |
| 7207 | for( i = 2; i > 0; i-- ) |
| 7208 | if( ++ssl->cur_out_ctr[i - 1] != 0 ) |
| 7209 | break; |
| 7210 | |
| 7211 | /* The loop goes to its end iff the counter is wrapping */ |
| 7212 | if( i == 0 ) |
| 7213 | { |
| 7214 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) ); |
| 7215 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
| 7216 | } |
| 7217 | } |
| 7218 | else |
| 7219 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 7220 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); |
| 7221 | |
| 7222 | ssl->transform_out = ssl->transform_negotiate; |
| 7223 | ssl->session_out = ssl->session_negotiate; |
| 7224 | |
| 7225 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7226 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 7227 | mbedtls_ssl_send_flight_completed( ssl ); |
| 7228 | #endif |
| 7229 | |
| 7230 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
| 7231 | { |
| 7232 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
| 7233 | return( ret ); |
| 7234 | } |
| 7235 | |
| 7236 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7237 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 7238 | ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 7239 | { |
| 7240 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); |
| 7241 | return( ret ); |
| 7242 | } |
| 7243 | #endif |
| 7244 | |
| 7245 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) ); |
| 7246 | |
| 7247 | return( 0 ); |
| 7248 | } |
| 7249 | |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7250 | #define SSL_MAX_HASH_LEN 12 |
| 7251 | |
| 7252 | int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl ) |
| 7253 | { |
| 7254 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7255 | unsigned int hash_len = 12; |
| 7256 | unsigned char buf[SSL_MAX_HASH_LEN]; |
| 7257 | |
| 7258 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) ); |
| 7259 | |
| 7260 | ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 ); |
| 7261 | |
| 7262 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
| 7263 | { |
| 7264 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 7265 | goto exit; |
| 7266 | } |
| 7267 | |
| 7268 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
| 7269 | { |
| 7270 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
| 7271 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7272 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 7273 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 7274 | goto exit; |
| 7275 | } |
| 7276 | |
| 7277 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ) |
| 7278 | { |
| 7279 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7280 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 7281 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 7282 | goto exit; |
| 7283 | } |
| 7284 | |
| 7285 | if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len ) |
| 7286 | { |
| 7287 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
| 7288 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7289 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 7290 | ret = MBEDTLS_ERR_SSL_DECODE_ERROR; |
| 7291 | goto exit; |
| 7292 | } |
| 7293 | |
| 7294 | if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), |
| 7295 | buf, hash_len ) != 0 ) |
| 7296 | { |
| 7297 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
| 7298 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7299 | MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR ); |
| 7300 | ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
| 7301 | goto exit; |
| 7302 | } |
| 7303 | |
| 7304 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 7305 | ssl->verify_data_len = hash_len; |
| 7306 | memcpy( ssl->peer_verify_data, buf, hash_len ); |
| 7307 | #endif |
| 7308 | |
| 7309 | if( ssl->handshake->resume != 0 ) |
| 7310 | { |
| 7311 | #if defined(MBEDTLS_SSL_CLI_C) |
| 7312 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 7313 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
| 7314 | #endif |
| 7315 | #if defined(MBEDTLS_SSL_SRV_C) |
| 7316 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 7317 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
| 7318 | #endif |
| 7319 | } |
| 7320 | else |
| 7321 | ssl->state++; |
| 7322 | |
| 7323 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7324 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 7325 | mbedtls_ssl_recv_flight_completed( ssl ); |
| 7326 | #endif |
| 7327 | |
| 7328 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) ); |
| 7329 | |
| 7330 | exit: |
| 7331 | mbedtls_platform_zeroize( buf, hash_len ); |
| 7332 | return( ret ); |
| 7333 | } |
| 7334 | |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7335 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 7336 | /* |
| 7337 | * Helper to get TLS 1.2 PRF from ciphersuite |
| 7338 | * (Duplicates bits of logic from ssl_set_handshake_prfs().) |
| 7339 | */ |
| 7340 | static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id ) |
| 7341 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 7342 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7343 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
| 7344 | mbedtls_ssl_ciphersuite_from_id( ciphersuite_id ); |
| 7345 | |
Leonid Rozenboim | e9d8dcd | 2022-08-08 15:57:48 -0700 | [diff] [blame] | 7346 | if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7347 | return( tls_prf_sha384 ); |
| 7348 | #else |
| 7349 | (void) ciphersuite_id; |
| 7350 | #endif |
| 7351 | return( tls_prf_sha256 ); |
| 7352 | } |
| 7353 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 7354 | |
| 7355 | static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf ) |
| 7356 | { |
| 7357 | ((void) tls_prf); |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 7358 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 7359 | if( tls_prf == tls_prf_sha384 ) |
| 7360 | { |
| 7361 | return( MBEDTLS_SSL_TLS_PRF_SHA384 ); |
| 7362 | } |
| 7363 | else |
| 7364 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 7365 | #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] | 7366 | if( tls_prf == tls_prf_sha256 ) |
| 7367 | { |
| 7368 | return( MBEDTLS_SSL_TLS_PRF_SHA256 ); |
| 7369 | } |
| 7370 | else |
| 7371 | #endif |
| 7372 | return( MBEDTLS_SSL_TLS_PRF_NONE ); |
| 7373 | } |
| 7374 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7375 | /* |
| 7376 | * Populate a transform structure with session keys and all the other |
| 7377 | * necessary information. |
| 7378 | * |
| 7379 | * Parameters: |
| 7380 | * - [in/out]: transform: structure to populate |
| 7381 | * [in] must be just initialised with mbedtls_ssl_transform_init() |
| 7382 | * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf() |
| 7383 | * - [in] ciphersuite |
| 7384 | * - [in] master |
| 7385 | * - [in] encrypt_then_mac |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7386 | * - [in] tls_prf: pointer to PRF to use for key derivation |
| 7387 | * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7388 | * - [in] tls_version: TLS version |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7389 | * - [in] endpoint: client or server |
| 7390 | * - [in] ssl: used for: |
| 7391 | * - ssl->conf->{f,p}_export_keys |
| 7392 | * [in] optionally used for: |
| 7393 | * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg |
| 7394 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7395 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7396 | static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform, |
| 7397 | int ciphersuite, |
| 7398 | const unsigned char master[48], |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7399 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7400 | int encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7401 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7402 | ssl_tls_prf_t tls_prf, |
| 7403 | const unsigned char randbytes[64], |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7404 | mbedtls_ssl_protocol_version tls_version, |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7405 | unsigned endpoint, |
| 7406 | const mbedtls_ssl_context *ssl ) |
| 7407 | { |
| 7408 | int ret = 0; |
| 7409 | unsigned char keyblk[256]; |
| 7410 | unsigned char *key1; |
| 7411 | unsigned char *key2; |
| 7412 | unsigned char *mac_enc; |
| 7413 | unsigned char *mac_dec; |
| 7414 | size_t mac_key_len = 0; |
| 7415 | size_t iv_copy_len; |
| 7416 | size_t keylen; |
| 7417 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7418 | mbedtls_ssl_mode_t ssl_mode; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7419 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7420 | const mbedtls_cipher_info_t *cipher_info; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7421 | const mbedtls_md_info_t *md_info; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7422 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7423 | |
| 7424 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7425 | psa_key_type_t key_type; |
| 7426 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7427 | psa_algorithm_t alg; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7428 | psa_algorithm_t mac_alg = 0; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7429 | size_t key_bits; |
| 7430 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 7431 | #endif |
| 7432 | |
| 7433 | #if !defined(MBEDTLS_DEBUG_C) && \ |
| 7434 | !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 7435 | if( ssl->f_export_keys == NULL ) |
| 7436 | { |
| 7437 | ssl = NULL; /* make sure we don't use it except for these cases */ |
| 7438 | (void) ssl; |
| 7439 | } |
| 7440 | #endif |
| 7441 | |
| 7442 | /* |
| 7443 | * Some data just needs copying into the structure |
| 7444 | */ |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7445 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7446 | transform->encrypt_then_mac = encrypt_then_mac; |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7447 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7448 | transform->tls_version = tls_version; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7449 | |
| 7450 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 7451 | memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) ); |
| 7452 | #endif |
| 7453 | |
| 7454 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7455 | if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7456 | { |
| 7457 | /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform |
| 7458 | * generation separate. This should never happen. */ |
| 7459 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 7460 | } |
| 7461 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 7462 | |
| 7463 | /* |
| 7464 | * Get various info structures |
| 7465 | */ |
| 7466 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite ); |
| 7467 | if( ciphersuite_info == NULL ) |
| 7468 | { |
| 7469 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found", |
| 7470 | ciphersuite ) ); |
| 7471 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7472 | } |
| 7473 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 7474 | ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite( |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7475 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7476 | encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7477 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7478 | ciphersuite_info ); |
| 7479 | |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7480 | if( ssl_mode == MBEDTLS_SSL_MODE_AEAD ) |
| 7481 | transform->taglen = |
| 7482 | ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; |
| 7483 | |
| 7484 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7485 | if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher, |
| 7486 | transform->taglen, |
| 7487 | &alg, |
| 7488 | &key_type, |
| 7489 | &key_bits ) ) != PSA_SUCCESS ) |
| 7490 | { |
| 7491 | ret = psa_ssl_status_to_mbedtls( status ); |
| 7492 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret ); |
| 7493 | goto end; |
| 7494 | } |
| 7495 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7496 | cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher ); |
| 7497 | if( cipher_info == NULL ) |
| 7498 | { |
| 7499 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found", |
| 7500 | ciphersuite_info->cipher ) ); |
| 7501 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7502 | } |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7503 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7504 | |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7505 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | abac037 | 2022-07-18 13:41:11 +0200 | [diff] [blame] | 7506 | mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac ); |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7507 | if( mac_alg == 0 ) |
| 7508 | { |
Manuel Pégourié-Gonnard | abac037 | 2022-07-18 13:41:11 +0200 | [diff] [blame] | 7509 | 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] | 7510 | (unsigned) ciphersuite_info->mac ) ); |
| 7511 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7512 | } |
| 7513 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7514 | md_info = mbedtls_md_info_from_type( ciphersuite_info->mac ); |
| 7515 | if( md_info == NULL ) |
| 7516 | { |
| 7517 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found", |
| 7518 | (unsigned) ciphersuite_info->mac ) ); |
| 7519 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7520 | } |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7521 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7522 | |
| 7523 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 7524 | /* Copy own and peer's CID if the use of the CID |
| 7525 | * extension has been negotiated. */ |
| 7526 | if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED ) |
| 7527 | { |
| 7528 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) ); |
| 7529 | |
| 7530 | transform->in_cid_len = ssl->own_cid_len; |
| 7531 | memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len ); |
| 7532 | MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid, |
| 7533 | transform->in_cid_len ); |
| 7534 | |
| 7535 | transform->out_cid_len = ssl->handshake->peer_cid_len; |
| 7536 | memcpy( transform->out_cid, ssl->handshake->peer_cid, |
| 7537 | ssl->handshake->peer_cid_len ); |
| 7538 | MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid, |
| 7539 | transform->out_cid_len ); |
| 7540 | } |
| 7541 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 7542 | |
| 7543 | /* |
| 7544 | * Compute key block using the PRF |
| 7545 | */ |
| 7546 | ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 ); |
| 7547 | if( ret != 0 ) |
| 7548 | { |
| 7549 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
| 7550 | return( ret ); |
| 7551 | } |
| 7552 | |
| 7553 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s", |
| 7554 | mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) ); |
| 7555 | MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 ); |
| 7556 | MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 ); |
| 7557 | MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 ); |
| 7558 | |
| 7559 | /* |
| 7560 | * Determine the appropriate key, IV and MAC length. |
| 7561 | */ |
| 7562 | |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7563 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7564 | keylen = PSA_BITS_TO_BYTES(key_bits); |
| 7565 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7566 | keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7567 | #endif |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7568 | |
| 7569 | #if defined(MBEDTLS_GCM_C) || \ |
| 7570 | defined(MBEDTLS_CCM_C) || \ |
| 7571 | defined(MBEDTLS_CHACHAPOLY_C) |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7572 | if( ssl_mode == MBEDTLS_SSL_MODE_AEAD ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7573 | { |
| 7574 | size_t explicit_ivlen; |
| 7575 | |
| 7576 | transform->maclen = 0; |
| 7577 | mac_key_len = 0; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7578 | |
| 7579 | /* All modes haves 96-bit IVs, but the length of the static parts vary |
| 7580 | * with mode and version: |
| 7581 | * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes |
| 7582 | * (to be concatenated with a dynamically chosen IV of 8 Bytes) |
| 7583 | * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's |
| 7584 | * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record |
| 7585 | * sequence number). |
| 7586 | */ |
| 7587 | transform->ivlen = 12; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7588 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7589 | if( key_type == PSA_KEY_TYPE_CHACHA20 ) |
| 7590 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7591 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY ) |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7592 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7593 | transform->fixed_ivlen = 12; |
| 7594 | else |
| 7595 | transform->fixed_ivlen = 4; |
| 7596 | |
| 7597 | /* Minimum length of encrypted record */ |
| 7598 | explicit_ivlen = transform->ivlen - transform->fixed_ivlen; |
| 7599 | transform->minlen = explicit_ivlen + transform->taglen; |
| 7600 | } |
| 7601 | else |
| 7602 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
| 7603 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7604 | if( ssl_mode == MBEDTLS_SSL_MODE_STREAM || |
| 7605 | ssl_mode == MBEDTLS_SSL_MODE_CBC || |
| 7606 | ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7607 | { |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7608 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | d1be767 | 2022-04-04 11:21:41 +0200 | [diff] [blame] | 7609 | size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7610 | #else |
| 7611 | size_t block_size = cipher_info->block_size; |
| 7612 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7613 | |
| 7614 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7615 | /* Get MAC length */ |
| 7616 | mac_key_len = PSA_HASH_LENGTH(mac_alg); |
| 7617 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7618 | /* Initialize HMAC contexts */ |
| 7619 | if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 || |
| 7620 | ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 ) |
| 7621 | { |
| 7622 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
| 7623 | goto end; |
| 7624 | } |
| 7625 | |
| 7626 | /* Get MAC length */ |
| 7627 | mac_key_len = mbedtls_md_get_size( md_info ); |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7628 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7629 | transform->maclen = mac_key_len; |
| 7630 | |
| 7631 | /* IV length */ |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7632 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | 2230e6c | 2022-04-27 10:36:14 +0200 | [diff] [blame] | 7633 | transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg ); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7634 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7635 | transform->ivlen = cipher_info->iv_size; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7636 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7637 | |
| 7638 | /* Minimum length */ |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7639 | if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7640 | transform->minlen = transform->maclen; |
| 7641 | else |
| 7642 | { |
| 7643 | /* |
| 7644 | * GenericBlockCipher: |
| 7645 | * 1. if EtM is in use: one block plus MAC |
| 7646 | * otherwise: * first multiple of blocklen greater than maclen |
| 7647 | * 2. IV |
| 7648 | */ |
| 7649 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7650 | if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7651 | { |
| 7652 | transform->minlen = transform->maclen |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7653 | + block_size; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7654 | } |
| 7655 | else |
| 7656 | #endif |
| 7657 | { |
| 7658 | transform->minlen = transform->maclen |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7659 | + block_size |
| 7660 | - transform->maclen % block_size; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7661 | } |
| 7662 | |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7663 | if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7664 | { |
| 7665 | transform->minlen += transform->ivlen; |
| 7666 | } |
| 7667 | else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7668 | { |
| 7669 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 7670 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 7671 | goto end; |
| 7672 | } |
| 7673 | } |
| 7674 | } |
| 7675 | else |
| 7676 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 7677 | { |
| 7678 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 7679 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 7680 | } |
| 7681 | |
| 7682 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u", |
| 7683 | (unsigned) keylen, |
| 7684 | (unsigned) transform->minlen, |
| 7685 | (unsigned) transform->ivlen, |
| 7686 | (unsigned) transform->maclen ) ); |
| 7687 | |
| 7688 | /* |
| 7689 | * Finally setup the cipher contexts, IVs and MAC secrets. |
| 7690 | */ |
| 7691 | #if defined(MBEDTLS_SSL_CLI_C) |
| 7692 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 7693 | { |
| 7694 | key1 = keyblk + mac_key_len * 2; |
| 7695 | key2 = keyblk + mac_key_len * 2 + keylen; |
| 7696 | |
| 7697 | mac_enc = keyblk; |
| 7698 | mac_dec = keyblk + mac_key_len; |
| 7699 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7700 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 7701 | transform->fixed_ivlen : transform->ivlen; |
| 7702 | memcpy( transform->iv_enc, key2 + keylen, iv_copy_len ); |
| 7703 | memcpy( transform->iv_dec, key2 + keylen + iv_copy_len, |
| 7704 | iv_copy_len ); |
| 7705 | } |
| 7706 | else |
| 7707 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 7708 | #if defined(MBEDTLS_SSL_SRV_C) |
| 7709 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 7710 | { |
| 7711 | key1 = keyblk + mac_key_len * 2 + keylen; |
| 7712 | key2 = keyblk + mac_key_len * 2; |
| 7713 | |
| 7714 | mac_enc = keyblk + mac_key_len; |
| 7715 | mac_dec = keyblk; |
| 7716 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7717 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 7718 | transform->fixed_ivlen : transform->ivlen; |
| 7719 | memcpy( transform->iv_dec, key1 + keylen, iv_copy_len ); |
| 7720 | memcpy( transform->iv_enc, key1 + keylen + iv_copy_len, |
| 7721 | iv_copy_len ); |
| 7722 | } |
| 7723 | else |
| 7724 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 7725 | { |
| 7726 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 7727 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 7728 | goto end; |
| 7729 | } |
| 7730 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7731 | if( ssl != NULL && ssl->f_export_keys != NULL ) |
| 7732 | { |
| 7733 | ssl->f_export_keys( ssl->p_export_keys, |
| 7734 | MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET, |
| 7735 | master, 48, |
| 7736 | randbytes + 32, |
| 7737 | randbytes, |
| 7738 | tls_prf_get_type( tls_prf ) ); |
| 7739 | } |
| 7740 | |
| 7741 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7742 | transform->psa_alg = alg; |
| 7743 | |
| 7744 | if ( alg != MBEDTLS_SSL_NULL_CIPHER ) |
| 7745 | { |
| 7746 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 7747 | psa_set_key_algorithm( &attributes, alg ); |
| 7748 | psa_set_key_type( &attributes, key_type ); |
| 7749 | |
| 7750 | if( ( status = psa_import_key( &attributes, |
| 7751 | key1, |
| 7752 | PSA_BITS_TO_BYTES( key_bits ), |
| 7753 | &transform->psa_key_enc ) ) != PSA_SUCCESS ) |
| 7754 | { |
| 7755 | MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status ); |
| 7756 | ret = psa_ssl_status_to_mbedtls( status ); |
| 7757 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret ); |
| 7758 | goto end; |
| 7759 | } |
| 7760 | |
| 7761 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 7762 | |
| 7763 | if( ( status = psa_import_key( &attributes, |
| 7764 | key2, |
| 7765 | PSA_BITS_TO_BYTES( key_bits ), |
| 7766 | &transform->psa_key_dec ) ) != PSA_SUCCESS ) |
| 7767 | { |
| 7768 | ret = psa_ssl_status_to_mbedtls( status ); |
| 7769 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret ); |
| 7770 | goto end; |
| 7771 | } |
| 7772 | } |
| 7773 | #else |
| 7774 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc, |
| 7775 | cipher_info ) ) != 0 ) |
| 7776 | { |
| 7777 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
| 7778 | goto end; |
| 7779 | } |
| 7780 | |
| 7781 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec, |
| 7782 | cipher_info ) ) != 0 ) |
| 7783 | { |
| 7784 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
| 7785 | goto end; |
| 7786 | } |
| 7787 | |
| 7788 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1, |
| 7789 | (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ), |
| 7790 | MBEDTLS_ENCRYPT ) ) != 0 ) |
| 7791 | { |
| 7792 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
| 7793 | goto end; |
| 7794 | } |
| 7795 | |
| 7796 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2, |
| 7797 | (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ), |
| 7798 | MBEDTLS_DECRYPT ) ) != 0 ) |
| 7799 | { |
| 7800 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
| 7801 | goto end; |
| 7802 | } |
| 7803 | |
| 7804 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
| 7805 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC ) |
| 7806 | { |
| 7807 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc, |
| 7808 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
| 7809 | { |
| 7810 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
| 7811 | goto end; |
| 7812 | } |
| 7813 | |
| 7814 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec, |
| 7815 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
| 7816 | { |
| 7817 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
| 7818 | goto end; |
| 7819 | } |
| 7820 | } |
| 7821 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
| 7822 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7823 | |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 7824 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 7825 | /* For HMAC-based ciphersuites, initialize the HMAC transforms. |
| 7826 | For AEAD-based ciphersuites, there is nothing to do here. */ |
| 7827 | if( mac_key_len != 0 ) |
| 7828 | { |
| 7829 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7830 | transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg ); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 7831 | |
| 7832 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7833 | psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) ); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 7834 | psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC ); |
| 7835 | |
| 7836 | if( ( status = psa_import_key( &attributes, |
| 7837 | mac_enc, mac_key_len, |
| 7838 | &transform->psa_mac_enc ) ) != PSA_SUCCESS ) |
| 7839 | { |
| 7840 | ret = psa_ssl_status_to_mbedtls( status ); |
| 7841 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret ); |
| 7842 | goto end; |
| 7843 | } |
| 7844 | |
Ronald Cron | fb39f15 | 2022-03-25 14:36:28 +0100 | [diff] [blame] | 7845 | if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) || |
Andrzej Kurek | 8c95ac4 | 2022-08-17 16:17:00 -0400 | [diff] [blame] | 7846 | ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING ) |
| 7847 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
| 7848 | && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ) |
| 7849 | #endif |
| 7850 | ) ) |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 7851 | /* mbedtls_ct_hmac() requires the key to be exportable */ |
| 7852 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT | |
| 7853 | PSA_KEY_USAGE_VERIFY_HASH ); |
| 7854 | else |
| 7855 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
| 7856 | |
| 7857 | if( ( status = psa_import_key( &attributes, |
| 7858 | mac_dec, mac_key_len, |
| 7859 | &transform->psa_mac_dec ) ) != PSA_SUCCESS ) |
| 7860 | { |
| 7861 | ret = psa_ssl_status_to_mbedtls( status ); |
| 7862 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret ); |
| 7863 | goto end; |
| 7864 | } |
| 7865 | #else |
| 7866 | ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len ); |
| 7867 | if( ret != 0 ) |
| 7868 | goto end; |
| 7869 | ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len ); |
| 7870 | if( ret != 0 ) |
| 7871 | goto end; |
| 7872 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7873 | } |
| 7874 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 7875 | |
| 7876 | ((void) mac_dec); |
| 7877 | ((void) mac_enc); |
| 7878 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7879 | end: |
| 7880 | mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) ); |
| 7881 | return( ret ); |
| 7882 | } |
| 7883 | |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 7884 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7885 | int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, |
| 7886 | unsigned char *hash, size_t *hashlen, |
| 7887 | unsigned char *data, size_t data_len, |
| 7888 | mbedtls_md_type_t md_alg ) |
| 7889 | { |
| 7890 | psa_status_t status; |
| 7891 | psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT; |
Manuel Pégourié-Gonnard | abac037 | 2022-07-18 13:41:11 +0200 | [diff] [blame] | 7892 | 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] | 7893 | |
| 7894 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) ); |
| 7895 | |
| 7896 | if( ( status = psa_hash_setup( &hash_operation, |
| 7897 | hash_alg ) ) != PSA_SUCCESS ) |
| 7898 | { |
| 7899 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status ); |
| 7900 | goto exit; |
| 7901 | } |
| 7902 | |
| 7903 | if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes, |
| 7904 | 64 ) ) != PSA_SUCCESS ) |
| 7905 | { |
| 7906 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); |
| 7907 | goto exit; |
| 7908 | } |
| 7909 | |
| 7910 | if( ( status = psa_hash_update( &hash_operation, |
| 7911 | data, data_len ) ) != PSA_SUCCESS ) |
| 7912 | { |
| 7913 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); |
| 7914 | goto exit; |
| 7915 | } |
| 7916 | |
| 7917 | if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE, |
| 7918 | hashlen ) ) != PSA_SUCCESS ) |
| 7919 | { |
| 7920 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status ); |
| 7921 | goto exit; |
| 7922 | } |
| 7923 | |
| 7924 | exit: |
| 7925 | if( status != PSA_SUCCESS ) |
| 7926 | { |
| 7927 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7928 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 7929 | switch( status ) |
| 7930 | { |
| 7931 | case PSA_ERROR_NOT_SUPPORTED: |
| 7932 | return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE ); |
| 7933 | case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */ |
| 7934 | case PSA_ERROR_BUFFER_TOO_SMALL: |
| 7935 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
| 7936 | case PSA_ERROR_INSUFFICIENT_MEMORY: |
| 7937 | return( MBEDTLS_ERR_MD_ALLOC_FAILED ); |
| 7938 | default: |
| 7939 | return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED ); |
| 7940 | } |
| 7941 | } |
| 7942 | return( 0 ); |
| 7943 | } |
| 7944 | |
| 7945 | #else |
| 7946 | |
| 7947 | int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, |
| 7948 | unsigned char *hash, size_t *hashlen, |
| 7949 | unsigned char *data, size_t data_len, |
| 7950 | mbedtls_md_type_t md_alg ) |
| 7951 | { |
| 7952 | int ret = 0; |
| 7953 | mbedtls_md_context_t ctx; |
| 7954 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg ); |
| 7955 | *hashlen = mbedtls_md_get_size( md_info ); |
| 7956 | |
| 7957 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) ); |
| 7958 | |
| 7959 | mbedtls_md_init( &ctx ); |
| 7960 | |
| 7961 | /* |
| 7962 | * digitally-signed struct { |
| 7963 | * opaque client_random[32]; |
| 7964 | * opaque server_random[32]; |
| 7965 | * ServerDHParams params; |
| 7966 | * }; |
| 7967 | */ |
| 7968 | if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 ) |
| 7969 | { |
| 7970 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
| 7971 | goto exit; |
| 7972 | } |
| 7973 | if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 ) |
| 7974 | { |
| 7975 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret ); |
| 7976 | goto exit; |
| 7977 | } |
| 7978 | if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 7979 | { |
| 7980 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 7981 | goto exit; |
| 7982 | } |
| 7983 | if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 ) |
| 7984 | { |
| 7985 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 7986 | goto exit; |
| 7987 | } |
| 7988 | if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 ) |
| 7989 | { |
| 7990 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret ); |
| 7991 | goto exit; |
| 7992 | } |
| 7993 | |
| 7994 | exit: |
| 7995 | mbedtls_md_free( &ctx ); |
| 7996 | |
| 7997 | if( ret != 0 ) |
| 7998 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7999 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 8000 | |
| 8001 | return( ret ); |
| 8002 | } |
| 8003 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8004 | |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8005 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 8006 | |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8007 | /* Find the preferred hash for a given signature algorithm. */ |
| 8008 | unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg( |
| 8009 | mbedtls_ssl_context *ssl, |
| 8010 | unsigned int sig_alg ) |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8011 | { |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 8012 | unsigned int i; |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8013 | uint16_t *received_sig_algs = ssl->handshake->received_sig_algs; |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 8014 | |
| 8015 | if( sig_alg == MBEDTLS_SSL_SIG_ANON ) |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8016 | return( MBEDTLS_SSL_HASH_NONE ); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 8017 | |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8018 | for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ ) |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8019 | { |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8020 | unsigned int hash_alg_received = |
| 8021 | MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG( |
| 8022 | received_sig_algs[i] ); |
| 8023 | unsigned int sig_alg_received = |
| 8024 | MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG( |
| 8025 | received_sig_algs[i] ); |
| 8026 | |
| 8027 | if( sig_alg == sig_alg_received ) |
| 8028 | { |
| 8029 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8030 | if( ssl->handshake->key_cert && ssl->handshake->key_cert->key ) |
| 8031 | { |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 8032 | psa_algorithm_t psa_hash_alg = |
Manuel Pégourié-Gonnard | abac037 | 2022-07-18 13:41:11 +0200 | [diff] [blame] | 8033 | mbedtls_hash_info_psa_from_md( hash_alg_received ); |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8034 | |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 8035 | if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA && |
| 8036 | ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key, |
| 8037 | PSA_ALG_ECDSA( psa_hash_alg ), |
| 8038 | PSA_KEY_USAGE_SIGN_HASH ) ) |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8039 | continue; |
| 8040 | |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 8041 | if( sig_alg_received == MBEDTLS_SSL_SIG_RSA && |
Neil Armstrong | 971f30d | 2022-07-01 16:23:50 +0200 | [diff] [blame] | 8042 | ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key, |
| 8043 | PSA_ALG_RSA_PKCS1V15_SIGN( |
| 8044 | psa_hash_alg ), |
| 8045 | PSA_KEY_USAGE_SIGN_HASH ) ) |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8046 | continue; |
| 8047 | } |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8048 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 8049 | |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8050 | return( hash_alg_received ); |
| 8051 | } |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8052 | } |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8053 | |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8054 | return( MBEDTLS_SSL_HASH_NONE ); |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8055 | } |
| 8056 | |
| 8057 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 8058 | |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8059 | /* Serialization of TLS 1.2 sessions: |
| 8060 | * |
| 8061 | * struct { |
| 8062 | * uint64 start_time; |
| 8063 | * uint8 ciphersuite[2]; // defined by the standard |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8064 | * uint8 session_id_len; // at most 32 |
| 8065 | * opaque session_id[32]; |
| 8066 | * opaque master[48]; // fixed length in the standard |
| 8067 | * uint32 verify_result; |
| 8068 | * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert |
| 8069 | * opaque ticket<0..2^24-1>; // length 0 means no ticket |
| 8070 | * uint32 ticket_lifetime; |
| 8071 | * uint8 mfl_code; // up to 255 according to standard |
| 8072 | * uint8 encrypt_then_mac; // 0 or 1 |
| 8073 | * } serialized_session_tls12; |
| 8074 | * |
| 8075 | */ |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 8076 | static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session, |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8077 | unsigned char *buf, |
| 8078 | size_t buf_len ) |
| 8079 | { |
| 8080 | unsigned char *p = buf; |
| 8081 | size_t used = 0; |
| 8082 | |
| 8083 | #if defined(MBEDTLS_HAVE_TIME) |
| 8084 | uint64_t start; |
| 8085 | #endif |
| 8086 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8087 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8088 | size_t cert_len; |
| 8089 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8090 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8091 | |
| 8092 | /* |
| 8093 | * Time |
| 8094 | */ |
| 8095 | #if defined(MBEDTLS_HAVE_TIME) |
| 8096 | used += 8; |
| 8097 | |
| 8098 | if( used <= buf_len ) |
| 8099 | { |
| 8100 | start = (uint64_t) session->start; |
| 8101 | |
| 8102 | MBEDTLS_PUT_UINT64_BE( start, p, 0 ); |
| 8103 | p += 8; |
| 8104 | } |
| 8105 | #endif /* MBEDTLS_HAVE_TIME */ |
| 8106 | |
| 8107 | /* |
| 8108 | * Basic mandatory fields |
| 8109 | */ |
| 8110 | used += 2 /* ciphersuite */ |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8111 | + 1 /* id_len */ |
| 8112 | + sizeof( session->id ) |
| 8113 | + sizeof( session->master ) |
| 8114 | + 4; /* verify_result */ |
| 8115 | |
| 8116 | if( used <= buf_len ) |
| 8117 | { |
| 8118 | MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 ); |
| 8119 | p += 2; |
| 8120 | |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8121 | *p++ = MBEDTLS_BYTE_0( session->id_len ); |
| 8122 | memcpy( p, session->id, 32 ); |
| 8123 | p += 32; |
| 8124 | |
| 8125 | memcpy( p, session->master, 48 ); |
| 8126 | p += 48; |
| 8127 | |
| 8128 | MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 ); |
| 8129 | p += 4; |
| 8130 | } |
| 8131 | |
| 8132 | /* |
| 8133 | * Peer's end-entity certificate |
| 8134 | */ |
| 8135 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8136 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8137 | if( session->peer_cert == NULL ) |
| 8138 | cert_len = 0; |
| 8139 | else |
| 8140 | cert_len = session->peer_cert->raw.len; |
| 8141 | |
| 8142 | used += 3 + cert_len; |
| 8143 | |
| 8144 | if( used <= buf_len ) |
| 8145 | { |
| 8146 | *p++ = MBEDTLS_BYTE_2( cert_len ); |
| 8147 | *p++ = MBEDTLS_BYTE_1( cert_len ); |
| 8148 | *p++ = MBEDTLS_BYTE_0( cert_len ); |
| 8149 | |
| 8150 | if( session->peer_cert != NULL ) |
| 8151 | { |
| 8152 | memcpy( p, session->peer_cert->raw.p, cert_len ); |
| 8153 | p += cert_len; |
| 8154 | } |
| 8155 | } |
| 8156 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8157 | if( session->peer_cert_digest != NULL ) |
| 8158 | { |
| 8159 | used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len; |
| 8160 | if( used <= buf_len ) |
| 8161 | { |
| 8162 | *p++ = (unsigned char) session->peer_cert_digest_type; |
| 8163 | *p++ = (unsigned char) session->peer_cert_digest_len; |
| 8164 | memcpy( p, session->peer_cert_digest, |
| 8165 | session->peer_cert_digest_len ); |
| 8166 | p += session->peer_cert_digest_len; |
| 8167 | } |
| 8168 | } |
| 8169 | else |
| 8170 | { |
| 8171 | used += 2; |
| 8172 | if( used <= buf_len ) |
| 8173 | { |
| 8174 | *p++ = (unsigned char) MBEDTLS_MD_NONE; |
| 8175 | *p++ = 0; |
| 8176 | } |
| 8177 | } |
| 8178 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8179 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8180 | |
| 8181 | /* |
| 8182 | * Session ticket if any, plus associated data |
| 8183 | */ |
| 8184 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 8185 | used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */ |
| 8186 | |
| 8187 | if( used <= buf_len ) |
| 8188 | { |
| 8189 | *p++ = MBEDTLS_BYTE_2( session->ticket_len ); |
| 8190 | *p++ = MBEDTLS_BYTE_1( session->ticket_len ); |
| 8191 | *p++ = MBEDTLS_BYTE_0( session->ticket_len ); |
| 8192 | |
| 8193 | if( session->ticket != NULL ) |
| 8194 | { |
| 8195 | memcpy( p, session->ticket, session->ticket_len ); |
| 8196 | p += session->ticket_len; |
| 8197 | } |
| 8198 | |
| 8199 | MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 ); |
| 8200 | p += 4; |
| 8201 | } |
| 8202 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 8203 | |
| 8204 | /* |
| 8205 | * Misc extension-related info |
| 8206 | */ |
| 8207 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 8208 | used += 1; |
| 8209 | |
| 8210 | if( used <= buf_len ) |
| 8211 | *p++ = session->mfl_code; |
| 8212 | #endif |
| 8213 | |
| 8214 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 8215 | used += 1; |
| 8216 | |
| 8217 | if( used <= buf_len ) |
| 8218 | *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac ); |
| 8219 | #endif |
| 8220 | |
| 8221 | return( used ); |
| 8222 | } |
| 8223 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 8224 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 8225 | static int ssl_tls12_session_load( mbedtls_ssl_session *session, |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8226 | const unsigned char *buf, |
| 8227 | size_t len ) |
| 8228 | { |
| 8229 | #if defined(MBEDTLS_HAVE_TIME) |
| 8230 | uint64_t start; |
| 8231 | #endif |
| 8232 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8233 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8234 | size_t cert_len; |
| 8235 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8236 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8237 | |
| 8238 | const unsigned char *p = buf; |
| 8239 | const unsigned char * const end = buf + len; |
| 8240 | |
| 8241 | /* |
| 8242 | * Time |
| 8243 | */ |
| 8244 | #if defined(MBEDTLS_HAVE_TIME) |
| 8245 | if( 8 > (size_t)( end - p ) ) |
| 8246 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8247 | |
| 8248 | start = ( (uint64_t) p[0] << 56 ) | |
| 8249 | ( (uint64_t) p[1] << 48 ) | |
| 8250 | ( (uint64_t) p[2] << 40 ) | |
| 8251 | ( (uint64_t) p[3] << 32 ) | |
| 8252 | ( (uint64_t) p[4] << 24 ) | |
| 8253 | ( (uint64_t) p[5] << 16 ) | |
| 8254 | ( (uint64_t) p[6] << 8 ) | |
| 8255 | ( (uint64_t) p[7] ); |
| 8256 | p += 8; |
| 8257 | |
| 8258 | session->start = (time_t) start; |
| 8259 | #endif /* MBEDTLS_HAVE_TIME */ |
| 8260 | |
| 8261 | /* |
| 8262 | * Basic mandatory fields |
| 8263 | */ |
Thomas Daubney | 20f89a9 | 2022-06-20 15:12:19 +0100 | [diff] [blame] | 8264 | if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) ) |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8265 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8266 | |
| 8267 | session->ciphersuite = ( p[0] << 8 ) | p[1]; |
| 8268 | p += 2; |
| 8269 | |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8270 | session->id_len = *p++; |
| 8271 | memcpy( session->id, p, 32 ); |
| 8272 | p += 32; |
| 8273 | |
| 8274 | memcpy( session->master, p, 48 ); |
| 8275 | p += 48; |
| 8276 | |
| 8277 | session->verify_result = ( (uint32_t) p[0] << 24 ) | |
| 8278 | ( (uint32_t) p[1] << 16 ) | |
| 8279 | ( (uint32_t) p[2] << 8 ) | |
| 8280 | ( (uint32_t) p[3] ); |
| 8281 | p += 4; |
| 8282 | |
| 8283 | /* Immediately clear invalid pointer values that have been read, in case |
| 8284 | * we exit early before we replaced them with valid ones. */ |
| 8285 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8286 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8287 | session->peer_cert = NULL; |
| 8288 | #else |
| 8289 | session->peer_cert_digest = NULL; |
| 8290 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8291 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8292 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 8293 | session->ticket = NULL; |
| 8294 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 8295 | |
| 8296 | /* |
| 8297 | * Peer certificate |
| 8298 | */ |
| 8299 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8300 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8301 | /* Deserialize CRT from the end of the ticket. */ |
| 8302 | if( 3 > (size_t)( end - p ) ) |
| 8303 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8304 | |
| 8305 | cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 8306 | p += 3; |
| 8307 | |
| 8308 | if( cert_len != 0 ) |
| 8309 | { |
| 8310 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 8311 | |
| 8312 | if( cert_len > (size_t)( end - p ) ) |
| 8313 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8314 | |
| 8315 | session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 8316 | |
| 8317 | if( session->peer_cert == NULL ) |
| 8318 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 8319 | |
| 8320 | mbedtls_x509_crt_init( session->peer_cert ); |
| 8321 | |
| 8322 | if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert, |
| 8323 | p, cert_len ) ) != 0 ) |
| 8324 | { |
| 8325 | mbedtls_x509_crt_free( session->peer_cert ); |
| 8326 | mbedtls_free( session->peer_cert ); |
| 8327 | session->peer_cert = NULL; |
| 8328 | return( ret ); |
| 8329 | } |
| 8330 | |
| 8331 | p += cert_len; |
| 8332 | } |
| 8333 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8334 | /* Deserialize CRT digest from the end of the ticket. */ |
| 8335 | if( 2 > (size_t)( end - p ) ) |
| 8336 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8337 | |
| 8338 | session->peer_cert_digest_type = (mbedtls_md_type_t) *p++; |
| 8339 | session->peer_cert_digest_len = (size_t) *p++; |
| 8340 | |
| 8341 | if( session->peer_cert_digest_len != 0 ) |
| 8342 | { |
| 8343 | const mbedtls_md_info_t *md_info = |
| 8344 | mbedtls_md_info_from_type( session->peer_cert_digest_type ); |
| 8345 | if( md_info == NULL ) |
| 8346 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8347 | if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) ) |
| 8348 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8349 | |
| 8350 | if( session->peer_cert_digest_len > (size_t)( end - p ) ) |
| 8351 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8352 | |
| 8353 | session->peer_cert_digest = |
| 8354 | mbedtls_calloc( 1, session->peer_cert_digest_len ); |
| 8355 | if( session->peer_cert_digest == NULL ) |
| 8356 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 8357 | |
| 8358 | memcpy( session->peer_cert_digest, p, |
| 8359 | session->peer_cert_digest_len ); |
| 8360 | p += session->peer_cert_digest_len; |
| 8361 | } |
| 8362 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8363 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8364 | |
| 8365 | /* |
| 8366 | * Session ticket and associated data |
| 8367 | */ |
| 8368 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 8369 | if( 3 > (size_t)( end - p ) ) |
| 8370 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8371 | |
| 8372 | session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 8373 | p += 3; |
| 8374 | |
| 8375 | if( session->ticket_len != 0 ) |
| 8376 | { |
| 8377 | if( session->ticket_len > (size_t)( end - p ) ) |
| 8378 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8379 | |
| 8380 | session->ticket = mbedtls_calloc( 1, session->ticket_len ); |
| 8381 | if( session->ticket == NULL ) |
| 8382 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 8383 | |
| 8384 | memcpy( session->ticket, p, session->ticket_len ); |
| 8385 | p += session->ticket_len; |
| 8386 | } |
| 8387 | |
| 8388 | if( 4 > (size_t)( end - p ) ) |
| 8389 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8390 | |
| 8391 | session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) | |
| 8392 | ( (uint32_t) p[1] << 16 ) | |
| 8393 | ( (uint32_t) p[2] << 8 ) | |
| 8394 | ( (uint32_t) p[3] ); |
| 8395 | p += 4; |
| 8396 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 8397 | |
| 8398 | /* |
| 8399 | * Misc extension-related info |
| 8400 | */ |
| 8401 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 8402 | if( 1 > (size_t)( end - p ) ) |
| 8403 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8404 | |
| 8405 | session->mfl_code = *p++; |
| 8406 | #endif |
| 8407 | |
| 8408 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 8409 | if( 1 > (size_t)( end - p ) ) |
| 8410 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8411 | |
| 8412 | session->encrypt_then_mac = *p++; |
| 8413 | #endif |
| 8414 | |
| 8415 | /* Done, should have consumed entire buffer */ |
| 8416 | if( p != end ) |
| 8417 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8418 | |
| 8419 | return( 0 ); |
| 8420 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 8421 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 8422 | |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 8423 | int mbedtls_ssl_validate_ciphersuite( |
| 8424 | const mbedtls_ssl_context *ssl, |
| 8425 | const mbedtls_ssl_ciphersuite_t *suite_info, |
| 8426 | mbedtls_ssl_protocol_version min_tls_version, |
| 8427 | mbedtls_ssl_protocol_version max_tls_version ) |
| 8428 | { |
| 8429 | (void) ssl; |
| 8430 | |
| 8431 | if( suite_info == NULL ) |
| 8432 | return( -1 ); |
| 8433 | |
| 8434 | if( ( suite_info->min_tls_version > max_tls_version ) || |
| 8435 | ( suite_info->max_tls_version < min_tls_version ) ) |
| 8436 | { |
| 8437 | return( -1 ); |
| 8438 | } |
| 8439 | |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 8440 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C) |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 8441 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 8442 | if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE && |
| 8443 | mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 ) |
| 8444 | { |
| 8445 | return( -1 ); |
| 8446 | } |
| 8447 | #endif |
| 8448 | |
| 8449 | /* Don't suggest PSK-based ciphersuite if no PSK is available. */ |
| 8450 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
| 8451 | if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) && |
| 8452 | mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 ) |
| 8453 | { |
| 8454 | return( -1 ); |
| 8455 | } |
| 8456 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
| 8457 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 8458 | |
| 8459 | return( 0 ); |
| 8460 | } |
| 8461 | |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8462 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 8463 | /* |
| 8464 | * Function for writing a signature algorithm extension. |
| 8465 | * |
| 8466 | * The `extension_data` field of signature algorithm contains a `SignatureSchemeList` |
| 8467 | * value (TLS 1.3 RFC8446): |
| 8468 | * enum { |
| 8469 | * .... |
| 8470 | * ecdsa_secp256r1_sha256( 0x0403 ), |
| 8471 | * ecdsa_secp384r1_sha384( 0x0503 ), |
| 8472 | * ecdsa_secp521r1_sha512( 0x0603 ), |
| 8473 | * .... |
| 8474 | * } SignatureScheme; |
| 8475 | * |
| 8476 | * struct { |
| 8477 | * SignatureScheme supported_signature_algorithms<2..2^16-2>; |
| 8478 | * } SignatureSchemeList; |
| 8479 | * |
| 8480 | * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm` |
| 8481 | * value (TLS 1.2 RFC5246): |
| 8482 | * enum { |
| 8483 | * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), |
| 8484 | * sha512(6), (255) |
| 8485 | * } HashAlgorithm; |
| 8486 | * |
| 8487 | * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } |
| 8488 | * SignatureAlgorithm; |
| 8489 | * |
| 8490 | * struct { |
| 8491 | * HashAlgorithm hash; |
| 8492 | * SignatureAlgorithm signature; |
| 8493 | * } SignatureAndHashAlgorithm; |
| 8494 | * |
| 8495 | * SignatureAndHashAlgorithm |
| 8496 | * supported_signature_algorithms<2..2^16-2>; |
| 8497 | * |
| 8498 | * The TLS 1.3 signature algorithm extension was defined to be a compatible |
| 8499 | * generalization of the TLS 1.2 signature algorithm extension. |
| 8500 | * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by |
| 8501 | * `SignatureScheme` field of TLS 1.3 |
| 8502 | * |
| 8503 | */ |
| 8504 | int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf, |
| 8505 | const unsigned char *end, size_t *out_len ) |
| 8506 | { |
| 8507 | unsigned char *p = buf; |
| 8508 | unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */ |
| 8509 | size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */ |
| 8510 | |
| 8511 | *out_len = 0; |
| 8512 | |
| 8513 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) ); |
| 8514 | |
| 8515 | /* Check if we have space for header and length field: |
| 8516 | * - extension_type (2 bytes) |
| 8517 | * - extension_data_length (2 bytes) |
| 8518 | * - supported_signature_algorithms_length (2 bytes) |
| 8519 | */ |
| 8520 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 ); |
| 8521 | p += 6; |
| 8522 | |
| 8523 | /* |
| 8524 | * Write supported_signature_algorithms |
| 8525 | */ |
| 8526 | supported_sig_alg = p; |
| 8527 | const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl ); |
| 8528 | if( sig_alg == NULL ) |
| 8529 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 8530 | |
| 8531 | for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ ) |
| 8532 | { |
Jerry Yu | 53f5c15 | 2022-06-22 20:24:38 +0800 | [diff] [blame] | 8533 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s", |
| 8534 | *sig_alg, |
| 8535 | mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) ); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8536 | if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) ) |
| 8537 | continue; |
| 8538 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 8539 | MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 ); |
| 8540 | p += 2; |
Jerry Yu | 80dd5db | 2022-06-22 19:30:32 +0800 | [diff] [blame] | 8541 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s", |
Jerry Yu | f3b46b5 | 2022-06-19 16:52:27 +0800 | [diff] [blame] | 8542 | *sig_alg, |
| 8543 | mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) ); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8544 | } |
| 8545 | |
| 8546 | /* Length of supported_signature_algorithms */ |
| 8547 | supported_sig_alg_len = p - supported_sig_alg; |
| 8548 | if( supported_sig_alg_len == 0 ) |
| 8549 | { |
| 8550 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) ); |
| 8551 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 8552 | } |
| 8553 | |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8554 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 ); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8555 | MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 ); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8556 | MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 ); |
| 8557 | |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8558 | *out_len = p - buf; |
| 8559 | |
| 8560 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 8561 | ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG; |
| 8562 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 8563 | return( 0 ); |
| 8564 | } |
| 8565 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 8566 | |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8567 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8568 | /* |
| 8569 | * mbedtls_ssl_parse_server_name_ext |
| 8570 | * |
| 8571 | * Structure of server_name extension: |
| 8572 | * |
| 8573 | * enum { |
| 8574 | * host_name(0), (255) |
| 8575 | * } NameType; |
| 8576 | * opaque HostName<1..2^16-1>; |
| 8577 | * |
| 8578 | * struct { |
| 8579 | * NameType name_type; |
| 8580 | * select (name_type) { |
| 8581 | * case host_name: HostName; |
| 8582 | * } name; |
| 8583 | * } ServerName; |
| 8584 | * struct { |
| 8585 | * ServerName server_name_list<1..2^16-1> |
| 8586 | * } ServerNameList; |
| 8587 | */ |
Ronald Cron | ce7d76e | 2022-07-08 18:56:49 +0200 | [diff] [blame] | 8588 | MBEDTLS_CHECK_RETURN_CRITICAL |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8589 | int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl, |
| 8590 | const unsigned char *buf, |
| 8591 | const unsigned char *end ) |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8592 | { |
| 8593 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 8594 | const unsigned char *p = buf; |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8595 | size_t server_name_list_len, hostname_len; |
| 8596 | const unsigned char *server_name_list_end; |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8597 | |
XiaokangQian | f2a9420 | 2022-05-20 06:44:24 +0000 | [diff] [blame] | 8598 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8599 | |
| 8600 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 ); |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8601 | server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8602 | p += 2; |
| 8603 | |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8604 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len ); |
| 8605 | server_name_list_end = p + server_name_list_len; |
XiaokangQian | 75fe8c7 | 2022-06-15 09:42:45 +0000 | [diff] [blame] | 8606 | while( p < server_name_list_end ) |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8607 | { |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8608 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8609 | hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 ); |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8610 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, |
| 8611 | hostname_len + 3 ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8612 | |
| 8613 | if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME ) |
| 8614 | { |
XiaokangQian | 75fe8c7 | 2022-06-15 09:42:45 +0000 | [diff] [blame] | 8615 | /* sni_name is intended to be used only during the parsing of the |
| 8616 | * ClientHello message (it is reset to NULL before the end of |
| 8617 | * the message parsing). Thus it is ok to just point to the |
| 8618 | * reception buffer and not make a copy of it. |
| 8619 | */ |
XiaokangQian | f2a9420 | 2022-05-20 06:44:24 +0000 | [diff] [blame] | 8620 | ssl->handshake->sni_name = p + 3; |
| 8621 | ssl->handshake->sni_name_len = hostname_len; |
| 8622 | if( ssl->conf->f_sni == NULL ) |
| 8623 | return( 0 ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8624 | ret = ssl->conf->f_sni( ssl->conf->p_sni, |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8625 | ssl, p + 3, hostname_len ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8626 | if( ret != 0 ) |
| 8627 | { |
XiaokangQian | f2a9420 | 2022-05-20 06:44:24 +0000 | [diff] [blame] | 8628 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret ); |
XiaokangQian | 129aeb9 | 2022-06-02 09:29:18 +0000 | [diff] [blame] | 8629 | MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME, |
| 8630 | MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8631 | return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME ); |
| 8632 | } |
| 8633 | return( 0 ); |
| 8634 | } |
| 8635 | |
| 8636 | p += hostname_len + 3; |
| 8637 | } |
| 8638 | |
| 8639 | return( 0 ); |
| 8640 | } |
| 8641 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 8642 | |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8643 | #if defined(MBEDTLS_SSL_ALPN) |
Ronald Cron | ce7d76e | 2022-07-08 18:56:49 +0200 | [diff] [blame] | 8644 | MBEDTLS_CHECK_RETURN_CRITICAL |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8645 | int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl, |
| 8646 | const unsigned char *buf, |
| 8647 | const unsigned char *end ) |
| 8648 | { |
| 8649 | const unsigned char *p = buf; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8650 | size_t protocol_name_list_len; |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8651 | const unsigned char *protocol_name_list; |
| 8652 | const unsigned char *protocol_name_list_end; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8653 | size_t protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8654 | |
| 8655 | /* If ALPN not configured, just ignore the extension */ |
| 8656 | if( ssl->conf->alpn_list == NULL ) |
| 8657 | return( 0 ); |
| 8658 | |
| 8659 | /* |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8660 | * RFC7301, section 3.1 |
| 8661 | * opaque ProtocolName<1..2^8-1>; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8662 | * |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8663 | * struct { |
| 8664 | * ProtocolName protocol_name_list<2..2^16-1> |
| 8665 | * } ProtocolNameList; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8666 | */ |
| 8667 | |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8668 | /* |
XiaokangQian | 0b776e2 | 2022-06-24 09:04:59 +0000 | [diff] [blame] | 8669 | * protocol_name_list_len 2 bytes |
| 8670 | * protocol_name_len 1 bytes |
| 8671 | * protocol_name >=1 byte |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8672 | */ |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8673 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 ); |
| 8674 | |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8675 | protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8676 | p += 2; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8677 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len ); |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8678 | protocol_name_list = p; |
| 8679 | protocol_name_list_end = p + protocol_name_list_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8680 | |
| 8681 | /* Validate peer's list (lengths) */ |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8682 | while( p < protocol_name_list_end ) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8683 | { |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8684 | protocol_name_len = *p++; |
| 8685 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end, |
| 8686 | protocol_name_len ); |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8687 | if( protocol_name_len == 0 ) |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8688 | { |
| 8689 | MBEDTLS_SSL_PEND_FATAL_ALERT( |
| 8690 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER, |
| 8691 | MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER ); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8692 | return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER ); |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8693 | } |
| 8694 | |
| 8695 | p += protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8696 | } |
| 8697 | |
| 8698 | /* Use our order of preference */ |
| 8699 | for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ ) |
| 8700 | { |
| 8701 | size_t const alpn_len = strlen( *alpn ); |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8702 | p = protocol_name_list; |
| 8703 | while( p < protocol_name_list_end ) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8704 | { |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8705 | protocol_name_len = *p++; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8706 | if( protocol_name_len == alpn_len && |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8707 | memcmp( p, *alpn, alpn_len ) == 0 ) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8708 | { |
| 8709 | ssl->alpn_chosen = *alpn; |
| 8710 | return( 0 ); |
| 8711 | } |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8712 | |
| 8713 | p += protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8714 | } |
| 8715 | } |
| 8716 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8717 | /* If we get here, no match was found */ |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8718 | MBEDTLS_SSL_PEND_FATAL_ALERT( |
| 8719 | MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL, |
| 8720 | MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL ); |
| 8721 | return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL ); |
| 8722 | } |
| 8723 | |
| 8724 | int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl, |
| 8725 | unsigned char *buf, |
| 8726 | unsigned char *end, |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8727 | size_t *out_len ) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8728 | { |
| 8729 | unsigned char *p = buf; |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8730 | size_t protocol_name_len; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8731 | *out_len = 0; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8732 | |
| 8733 | if( ssl->alpn_chosen == NULL ) |
| 8734 | { |
| 8735 | return( 0 ); |
| 8736 | } |
| 8737 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8738 | protocol_name_len = strlen( ssl->alpn_chosen ); |
| 8739 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len ); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8740 | |
| 8741 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) ); |
| 8742 | /* |
| 8743 | * 0 . 1 ext identifier |
| 8744 | * 2 . 3 ext length |
| 8745 | * 4 . 5 protocol list length |
| 8746 | * 6 . 6 protocol name length |
| 8747 | * 7 . 7+n protocol name |
| 8748 | */ |
| 8749 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 ); |
| 8750 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8751 | *out_len = 7 + protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8752 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8753 | MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 ); |
| 8754 | MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 ); |
XiaokangQian | 0b776e2 | 2022-06-24 09:04:59 +0000 | [diff] [blame] | 8755 | /* Note: the length of the chosen protocol has been checked to be less |
| 8756 | * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`. |
| 8757 | */ |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8758 | p[6] = MBEDTLS_BYTE_0( protocol_name_len ); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8759 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8760 | memcpy( p + 7, ssl->alpn_chosen, protocol_name_len ); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8761 | return ( 0 ); |
| 8762 | } |
| 8763 | #endif /* MBEDTLS_SSL_ALPN */ |
| 8764 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8765 | #endif /* MBEDTLS_SSL_TLS_C */ |