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 client-side 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 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 20 | #include "common.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 21 | |
Jerry Yu | fb4b647 | 2022-01-27 15:03:26 +0800 | [diff] [blame] | 22 | #if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 23 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 24 | #include "mbedtls/platform.h" |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 25 | |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 26 | #include "mbedtls/ssl.h" |
Ronald Cron | 7320e64 | 2022-03-08 13:34:49 +0100 | [diff] [blame] | 27 | #include "ssl_client.h" |
Chris Jones | 84a773f | 2021-03-05 18:38:47 +0000 | [diff] [blame] | 28 | #include "ssl_misc.h" |
Janos Follath | 73c616b | 2019-12-18 15:07:04 +0000 | [diff] [blame] | 29 | #include "mbedtls/debug.h" |
| 30 | #include "mbedtls/error.h" |
Gabor Mezei | 765862c | 2021-10-19 12:22:25 +0200 | [diff] [blame] | 31 | #include "mbedtls/constant_time.h" |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 32 | |
Hanno Becker | bb89e27 | 2019-01-08 12:54:37 +0000 | [diff] [blame] | 33 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 34 | #include "mbedtls/psa_util.h" |
Ronald Cron | 69a6342 | 2021-10-18 09:47:58 +0200 | [diff] [blame] | 35 | #include "psa/crypto.h" |
Hanno Becker | bb89e27 | 2019-01-08 12:54:37 +0000 | [diff] [blame] | 36 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 37 | |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 38 | #include <string.h> |
| 39 | |
Manuel Pégourié-Gonnard | 9386664 | 2015-06-22 19:21:23 +0200 | [diff] [blame] | 40 | #include <stdint.h> |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 41 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 42 | #if defined(MBEDTLS_HAVE_TIME) |
Simon Butcher | b5b6af2 | 2016-07-13 14:46:18 +0100 | [diff] [blame] | 43 | #include "mbedtls/platform_time.h" |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 44 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 45 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 46 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 47 | #include "mbedtls/platform_util.h" |
Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 48 | #endif |
| 49 | |
Andrzej Kurek | 0ce5921 | 2022-08-17 07:54:34 -0400 | [diff] [blame] | 50 | #include "hash_info.h" |
| 51 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 52 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 53 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 54 | static int ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl, |
| 55 | unsigned char *buf, |
| 56 | const unsigned char *end, |
| 57 | size_t *olen ) |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 58 | { |
| 59 | unsigned char *p = buf; |
| 60 | |
| 61 | *olen = 0; |
| 62 | |
Tom Cosgrove | ce7f18c | 2022-07-28 05:50:56 +0100 | [diff] [blame] | 63 | /* We're always including a TLS_EMPTY_RENEGOTIATION_INFO_SCSV in the |
Hanno Becker | 40f8b51 | 2017-10-12 14:58:55 +0100 | [diff] [blame] | 64 | * initial ClientHello, in which case also adding the renegotiation |
| 65 | * info extension is NOT RECOMMENDED as per RFC 5746 Section 3.4. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 66 | if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 67 | return( 0 ); |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 68 | |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 69 | MBEDTLS_SSL_DEBUG_MSG( 3, |
| 70 | ( "client hello, adding renegotiation extension" ) ); |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 71 | |
Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 72 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 + ssl->verify_data_len ); |
Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 73 | |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 74 | /* |
| 75 | * Secure renegotiation |
| 76 | */ |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 77 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO, p, 0 ); |
| 78 | p += 2; |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 79 | |
| 80 | *p++ = 0x00; |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 81 | *p++ = MBEDTLS_BYTE_0( ssl->verify_data_len + 1 ); |
| 82 | *p++ = MBEDTLS_BYTE_0( ssl->verify_data_len ); |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 83 | |
| 84 | memcpy( p, ssl->own_verify_data, ssl->verify_data_len ); |
| 85 | |
| 86 | *olen = 5 + ssl->verify_data_len; |
Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 87 | |
| 88 | return( 0 ); |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 89 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 90 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 91 | |
Manuel Pégourié-Gonnard | f472179 | 2015-09-15 10:53:51 +0200 | [diff] [blame] | 92 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 93 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 94 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 95 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 96 | static int ssl_write_supported_point_formats_ext( mbedtls_ssl_context *ssl, |
| 97 | unsigned char *buf, |
| 98 | const unsigned char *end, |
| 99 | size_t *olen ) |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 100 | { |
| 101 | unsigned char *p = buf; |
Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 102 | (void) ssl; /* ssl used for debugging only */ |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 103 | |
| 104 | *olen = 0; |
| 105 | |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 106 | MBEDTLS_SSL_DEBUG_MSG( 3, |
| 107 | ( "client hello, adding supported_point_formats extension" ) ); |
Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 108 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 ); |
Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 109 | |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 110 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, p, 0 ); |
| 111 | p += 2; |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 112 | |
| 113 | *p++ = 0x00; |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 114 | *p++ = 2; |
Manuel Pégourié-Gonnard | 6b8846d | 2013-08-15 17:42:02 +0200 | [diff] [blame] | 115 | |
| 116 | *p++ = 1; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 117 | *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED; |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 118 | |
Manuel Pégourié-Gonnard | 6b8846d | 2013-08-15 17:42:02 +0200 | [diff] [blame] | 119 | *olen = 6; |
Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 120 | |
| 121 | return( 0 ); |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 122 | } |
Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 123 | #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || |
Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 124 | MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 125 | |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 126 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 127 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 128 | static int ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl, |
| 129 | unsigned char *buf, |
| 130 | const unsigned char *end, |
| 131 | size_t *olen ) |
Manuel Pégourié-Gonnard | 294139b | 2015-09-15 16:55:05 +0200 | [diff] [blame] | 132 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 133 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 294139b | 2015-09-15 16:55:05 +0200 | [diff] [blame] | 134 | unsigned char *p = buf; |
Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 135 | size_t kkpp_len = 0; |
Manuel Pégourié-Gonnard | 294139b | 2015-09-15 16:55:05 +0200 | [diff] [blame] | 136 | |
| 137 | *olen = 0; |
| 138 | |
| 139 | /* Skip costly extension if we can't use EC J-PAKE anyway */ |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 140 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 141 | if( ssl->handshake->psa_pake_ctx_is_ok != 1 ) |
| 142 | return( 0 ); |
| 143 | #else |
Manuel Pégourié-Gonnard | 294139b | 2015-09-15 16:55:05 +0200 | [diff] [blame] | 144 | if( mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 ) |
Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 145 | return( 0 ); |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 146 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 294139b | 2015-09-15 16:55:05 +0200 | [diff] [blame] | 147 | |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 148 | MBEDTLS_SSL_DEBUG_MSG( 3, |
| 149 | ( "client hello, adding ecjpake_kkpp extension" ) ); |
Manuel Pégourié-Gonnard | 294139b | 2015-09-15 16:55:05 +0200 | [diff] [blame] | 150 | |
Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 151 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 ); |
Manuel Pégourié-Gonnard | 294139b | 2015-09-15 16:55:05 +0200 | [diff] [blame] | 152 | |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 153 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ECJPAKE_KKPP, p, 0 ); |
| 154 | p += 2; |
Manuel Pégourié-Gonnard | 294139b | 2015-09-15 16:55:05 +0200 | [diff] [blame] | 155 | |
Manuel Pégourié-Gonnard | d0d8cb3 | 2015-09-17 14:16:30 +0200 | [diff] [blame] | 156 | /* |
| 157 | * We may need to send ClientHello multiple times for Hello verification. |
| 158 | * We don't want to compute fresh values every time (both for performance |
| 159 | * and consistency reasons), so cache the extension content. |
| 160 | */ |
| 161 | if( ssl->handshake->ecjpake_cache == NULL || |
| 162 | ssl->handshake->ecjpake_cache_len == 0 ) |
Manuel Pégourié-Gonnard | 294139b | 2015-09-15 16:55:05 +0200 | [diff] [blame] | 163 | { |
Manuel Pégourié-Gonnard | d0d8cb3 | 2015-09-17 14:16:30 +0200 | [diff] [blame] | 164 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "generating new ecjpake parameters" ) ); |
| 165 | |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 166 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame^] | 167 | ret = mbedtls_psa_ecjpake_write_round_one(&ssl->handshake->psa_pake_ctx, |
Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 168 | p + 2, end - p - 2, &kkpp_len ); |
| 169 | if ( ret != 0 ) |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 170 | { |
Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 171 | psa_destroy_key( ssl->handshake->psa_pake_password ); |
| 172 | psa_pake_abort( &ssl->handshake->psa_pake_ctx ); |
| 173 | MBEDTLS_SSL_DEBUG_RET( 1 , "psa_pake_output", ret ); |
| 174 | return( ret ); |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 175 | } |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 176 | #else |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 177 | ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx, |
Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 178 | p + 2, end - p - 2, &kkpp_len, |
| 179 | ssl->conf->f_rng, ssl->conf->p_rng ); |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 180 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | d0d8cb3 | 2015-09-17 14:16:30 +0200 | [diff] [blame] | 181 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 182 | MBEDTLS_SSL_DEBUG_RET( 1 , |
| 183 | "mbedtls_ecjpake_write_round_one", ret ); |
Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 184 | return( ret ); |
Manuel Pégourié-Gonnard | d0d8cb3 | 2015-09-17 14:16:30 +0200 | [diff] [blame] | 185 | } |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 186 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | d0d8cb3 | 2015-09-17 14:16:30 +0200 | [diff] [blame] | 187 | |
| 188 | ssl->handshake->ecjpake_cache = mbedtls_calloc( 1, kkpp_len ); |
| 189 | if( ssl->handshake->ecjpake_cache == NULL ) |
| 190 | { |
| 191 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "allocation failed" ) ); |
Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 192 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | d0d8cb3 | 2015-09-17 14:16:30 +0200 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | memcpy( ssl->handshake->ecjpake_cache, p + 2, kkpp_len ); |
| 196 | ssl->handshake->ecjpake_cache_len = kkpp_len; |
| 197 | } |
| 198 | else |
| 199 | { |
| 200 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "re-using cached ecjpake parameters" ) ); |
| 201 | |
| 202 | kkpp_len = ssl->handshake->ecjpake_cache_len; |
Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 203 | MBEDTLS_SSL_CHK_BUF_PTR( p + 2, end, kkpp_len ); |
Manuel Pégourié-Gonnard | d0d8cb3 | 2015-09-17 14:16:30 +0200 | [diff] [blame] | 204 | |
| 205 | memcpy( p + 2, ssl->handshake->ecjpake_cache, kkpp_len ); |
Manuel Pégourié-Gonnard | 294139b | 2015-09-15 16:55:05 +0200 | [diff] [blame] | 206 | } |
| 207 | |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 208 | MBEDTLS_PUT_UINT16_BE( kkpp_len, p, 0 ); |
| 209 | p += 2; |
Manuel Pégourié-Gonnard | 294139b | 2015-09-15 16:55:05 +0200 | [diff] [blame] | 210 | |
| 211 | *olen = kkpp_len + 4; |
Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 212 | |
| 213 | return( 0 ); |
Manuel Pégourié-Gonnard | 294139b | 2015-09-15 16:55:05 +0200 | [diff] [blame] | 214 | } |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 215 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Paul Bakker | c3f177a | 2012-04-11 16:11:49 +0000 | [diff] [blame] | 216 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 217 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 218 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 219 | static int ssl_write_cid_ext( mbedtls_ssl_context *ssl, |
| 220 | unsigned char *buf, |
| 221 | const unsigned char *end, |
| 222 | size_t *olen ) |
Hanno Becker | 49770ff | 2019-04-25 16:55:15 +0100 | [diff] [blame] | 223 | { |
| 224 | unsigned char *p = buf; |
| 225 | size_t ext_len; |
Hanno Becker | 49770ff | 2019-04-25 16:55:15 +0100 | [diff] [blame] | 226 | |
| 227 | /* |
Hanno Becker | ebcc913 | 2019-05-15 10:26:32 +0100 | [diff] [blame] | 228 | * Quoting draft-ietf-tls-dtls-connection-id-05 |
| 229 | * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05 |
Hanno Becker | 49770ff | 2019-04-25 16:55:15 +0100 | [diff] [blame] | 230 | * |
| 231 | * struct { |
| 232 | * opaque cid<0..2^8-1>; |
| 233 | * } ConnectionId; |
| 234 | */ |
| 235 | |
| 236 | *olen = 0; |
| 237 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
| 238 | ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED ) |
| 239 | { |
Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 240 | return( 0 ); |
Hanno Becker | 49770ff | 2019-04-25 16:55:15 +0100 | [diff] [blame] | 241 | } |
| 242 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding CID extension" ) ); |
| 243 | |
| 244 | /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX |
| 245 | * which is at most 255, so the increment cannot overflow. */ |
Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 246 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, (unsigned)( ssl->own_cid_len + 5 ) ); |
Hanno Becker | 49770ff | 2019-04-25 16:55:15 +0100 | [diff] [blame] | 247 | |
| 248 | /* Add extension ID + size */ |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 249 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_CID, p, 0 ); |
| 250 | p += 2; |
Hanno Becker | 49770ff | 2019-04-25 16:55:15 +0100 | [diff] [blame] | 251 | ext_len = (size_t) ssl->own_cid_len + 1; |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 252 | MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 ); |
| 253 | p += 2; |
Hanno Becker | 49770ff | 2019-04-25 16:55:15 +0100 | [diff] [blame] | 254 | |
| 255 | *p++ = (uint8_t) ssl->own_cid_len; |
| 256 | memcpy( p, ssl->own_cid, ssl->own_cid_len ); |
| 257 | |
| 258 | *olen = ssl->own_cid_len + 5; |
Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 259 | |
| 260 | return( 0 ); |
Hanno Becker | 49770ff | 2019-04-25 16:55:15 +0100 | [diff] [blame] | 261 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 262 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 49770ff | 2019-04-25 16:55:15 +0100 | [diff] [blame] | 263 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 264 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 265 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 266 | static int ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl, |
| 267 | unsigned char *buf, |
| 268 | const unsigned char *end, |
| 269 | size_t *olen ) |
Manuel Pégourié-Gonnard | a052849 | 2013-07-16 17:26:28 +0200 | [diff] [blame] | 270 | { |
| 271 | unsigned char *p = buf; |
| 272 | |
Simon Butcher | 0fc94e9 | 2015-09-28 20:52:04 +0100 | [diff] [blame] | 273 | *olen = 0; |
| 274 | |
Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 275 | if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ) |
| 276 | return( 0 ); |
Manuel Pégourié-Gonnard | a052849 | 2013-07-16 17:26:28 +0200 | [diff] [blame] | 277 | |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 278 | MBEDTLS_SSL_DEBUG_MSG( 3, |
| 279 | ( "client hello, adding max_fragment_length extension" ) ); |
Manuel Pégourié-Gonnard | a052849 | 2013-07-16 17:26:28 +0200 | [diff] [blame] | 280 | |
Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 281 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 ); |
Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 282 | |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 283 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, p, 0 ); |
| 284 | p += 2; |
Manuel Pégourié-Gonnard | a052849 | 2013-07-16 17:26:28 +0200 | [diff] [blame] | 285 | |
| 286 | *p++ = 0x00; |
| 287 | *p++ = 1; |
| 288 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 289 | *p++ = ssl->conf->mfl_code; |
Manuel Pégourié-Gonnard | a052849 | 2013-07-16 17:26:28 +0200 | [diff] [blame] | 290 | |
| 291 | *olen = 5; |
Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 292 | |
| 293 | return( 0 ); |
Manuel Pégourié-Gonnard | a052849 | 2013-07-16 17:26:28 +0200 | [diff] [blame] | 294 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 295 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | a052849 | 2013-07-16 17:26:28 +0200 | [diff] [blame] | 296 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 297 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 298 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 299 | static int ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl, |
| 300 | unsigned char *buf, |
| 301 | const unsigned char *end, |
| 302 | size_t *olen ) |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 303 | { |
| 304 | unsigned char *p = buf; |
| 305 | |
Simon Butcher | 0fc94e9 | 2015-09-28 20:52:04 +0100 | [diff] [blame] | 306 | *olen = 0; |
| 307 | |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 308 | if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ) |
Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 309 | return( 0 ); |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 310 | |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 311 | MBEDTLS_SSL_DEBUG_MSG( 3, |
| 312 | ( "client hello, adding encrypt_then_mac extension" ) ); |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 313 | |
Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 314 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 ); |
Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 315 | |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 316 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, p, 0 ); |
| 317 | p += 2; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 318 | |
| 319 | *p++ = 0x00; |
| 320 | *p++ = 0x00; |
| 321 | |
| 322 | *olen = 4; |
Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 323 | |
| 324 | return( 0 ); |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 325 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 326 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 327 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 328 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 329 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 330 | static int ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl, |
| 331 | unsigned char *buf, |
| 332 | const unsigned char *end, |
| 333 | size_t *olen ) |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 334 | { |
| 335 | unsigned char *p = buf; |
| 336 | |
Simon Butcher | 0fc94e9 | 2015-09-28 20:52:04 +0100 | [diff] [blame] | 337 | *olen = 0; |
| 338 | |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 339 | if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ) |
Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 340 | return( 0 ); |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 341 | |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 342 | MBEDTLS_SSL_DEBUG_MSG( 3, |
| 343 | ( "client hello, adding extended_master_secret extension" ) ); |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 344 | |
Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 345 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 ); |
Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 346 | |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 347 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, p, 0 ); |
| 348 | p += 2; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 349 | |
| 350 | *p++ = 0x00; |
| 351 | *p++ = 0x00; |
| 352 | |
| 353 | *olen = 4; |
Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 354 | |
| 355 | return( 0 ); |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 356 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 357 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 358 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 359 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 360 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 361 | static int ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl, |
| 362 | unsigned char *buf, |
| 363 | const unsigned char *end, |
| 364 | size_t *olen ) |
Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 365 | { |
| 366 | unsigned char *p = buf; |
| 367 | size_t tlen = ssl->session_negotiate->ticket_len; |
| 368 | |
Simon Butcher | 0fc94e9 | 2015-09-28 20:52:04 +0100 | [diff] [blame] | 369 | *olen = 0; |
| 370 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 371 | if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED ) |
Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 372 | return( 0 ); |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 373 | |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 374 | MBEDTLS_SSL_DEBUG_MSG( 3, |
| 375 | ( "client hello, adding session ticket extension" ) ); |
Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 376 | |
Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 377 | /* The addition is safe here since the ticket length is 16 bit. */ |
| 378 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 + tlen ); |
Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 379 | |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 380 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SESSION_TICKET, p, 0 ); |
| 381 | p += 2; |
Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 382 | |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 383 | MBEDTLS_PUT_UINT16_BE( tlen, p, 0 ); |
| 384 | p += 2; |
Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 385 | |
| 386 | *olen = 4; |
| 387 | |
Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 388 | if( ssl->session_negotiate->ticket == NULL || tlen == 0 ) |
Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 389 | return( 0 ); |
Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 390 | |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 391 | MBEDTLS_SSL_DEBUG_MSG( 3, |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 392 | ( "sending session ticket of length %" MBEDTLS_PRINTF_SIZET, tlen ) ); |
Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 393 | |
| 394 | memcpy( p, ssl->session_negotiate->ticket, tlen ); |
| 395 | |
| 396 | *olen += tlen; |
Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 397 | |
| 398 | return( 0 ); |
Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 399 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 400 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 401 | |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 402 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 403 | MBEDTLS_CHECK_RETURN_CRITICAL |
Johan Pascal | 77696ee | 2020-09-22 21:49:40 +0200 | [diff] [blame] | 404 | static int ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl, |
Johan Pascal | d387aa0 | 2020-09-23 18:47:56 +0200 | [diff] [blame] | 405 | unsigned char *buf, |
| 406 | const unsigned char *end, |
| 407 | size_t *olen ) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 408 | { |
| 409 | unsigned char *p = buf; |
Johan Pascal | f6417ec | 2020-09-22 15:15:19 +0200 | [diff] [blame] | 410 | size_t protection_profiles_index = 0, ext_len = 0; |
| 411 | uint16_t mki_len = 0, profile_value = 0; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 412 | |
| 413 | *olen = 0; |
| 414 | |
Johan Pascal | c3ccd98 | 2020-10-28 17:18:18 +0100 | [diff] [blame] | 415 | if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) || |
| 416 | ( ssl->conf->dtls_srtp_profile_list == NULL ) || |
| 417 | ( ssl->conf->dtls_srtp_profile_list_len == 0 ) ) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 418 | { |
Johan Pascal | 77696ee | 2020-09-22 21:49:40 +0200 | [diff] [blame] | 419 | return( 0 ); |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 420 | } |
| 421 | |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 422 | /* RFC 5764 section 4.1.1 |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 423 | * uint8 SRTPProtectionProfile[2]; |
| 424 | * |
| 425 | * struct { |
| 426 | * SRTPProtectionProfiles SRTPProtectionProfiles; |
| 427 | * opaque srtp_mki<0..255>; |
| 428 | * } UseSRTPData; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 429 | * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 430 | */ |
Johan Pascal | 9bc97ca | 2020-09-21 23:44:45 +0200 | [diff] [blame] | 431 | if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED ) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 432 | { |
| 433 | mki_len = ssl->dtls_srtp_info.mki_len; |
| 434 | } |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 435 | /* Extension length = 2 bytes for profiles length, |
| 436 | * ssl->conf->dtls_srtp_profile_list_len * 2 (each profile is 2 bytes length ), |
| 437 | * 1 byte for srtp_mki vector length and the mki_len value |
| 438 | */ |
Ron Eldor | 089c9fe | 2018-12-06 17:12:49 +0200 | [diff] [blame] | 439 | ext_len = 2 + 2 * ( ssl->conf->dtls_srtp_profile_list_len ) + 1 + mki_len; |
| 440 | |
Johan Pascal | 77696ee | 2020-09-22 21:49:40 +0200 | [diff] [blame] | 441 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding use_srtp extension" ) ); |
| 442 | |
| 443 | /* Check there is room in the buffer for the extension + 4 bytes |
| 444 | * - the extension tag (2 bytes) |
| 445 | * - the extension length (2 bytes) |
| 446 | */ |
| 447 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, ext_len + 4 ); |
| 448 | |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 449 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_USE_SRTP, p, 0 ); |
| 450 | p += 2; |
Johan Pascal | 77696ee | 2020-09-22 21:49:40 +0200 | [diff] [blame] | 451 | |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 452 | MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 ); |
| 453 | p += 2; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 454 | |
Ron Eldor | 3adb992 | 2017-12-21 10:15:08 +0200 | [diff] [blame] | 455 | /* protection profile length: 2*(ssl->conf->dtls_srtp_profile_list_len) */ |
Johan Pascal | aae4d22 | 2020-09-22 21:21:39 +0200 | [diff] [blame] | 456 | /* micro-optimization: |
| 457 | * the list size is limited to MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH |
| 458 | * which is lower than 127, so the upper byte of the length is always 0 |
| 459 | * For the documentation, the more generic code is left in comments |
| 460 | * *p++ = (unsigned char)( ( ( 2 * ssl->conf->dtls_srtp_profile_list_len ) |
| 461 | * >> 8 ) & 0xFF ); |
| 462 | */ |
| 463 | *p++ = 0; |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 464 | *p++ = MBEDTLS_BYTE_0( 2 * ssl->conf->dtls_srtp_profile_list_len ); |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 465 | |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 466 | for( protection_profiles_index=0; |
| 467 | protection_profiles_index < ssl->conf->dtls_srtp_profile_list_len; |
| 468 | protection_profiles_index++ ) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 469 | { |
Johan Pascal | 43f9490 | 2020-09-22 12:25:52 +0200 | [diff] [blame] | 470 | profile_value = mbedtls_ssl_check_srtp_profile_value |
Ron Eldor | 089c9fe | 2018-12-06 17:12:49 +0200 | [diff] [blame] | 471 | ( ssl->conf->dtls_srtp_profile_list[protection_profiles_index] ); |
Johan Pascal | 43f9490 | 2020-09-22 12:25:52 +0200 | [diff] [blame] | 472 | if( profile_value != MBEDTLS_TLS_SRTP_UNSET ) |
Ron Eldor | 089c9fe | 2018-12-06 17:12:49 +0200 | [diff] [blame] | 473 | { |
| 474 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_write_use_srtp_ext, add profile: %04x", |
| 475 | profile_value ) ); |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 476 | MBEDTLS_PUT_UINT16_BE( profile_value, p, 0 ); |
| 477 | p += 2; |
Ron Eldor | 089c9fe | 2018-12-06 17:12:49 +0200 | [diff] [blame] | 478 | } |
| 479 | else |
| 480 | { |
| 481 | /* |
| 482 | * Note: we shall never arrive here as protection profiles |
Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 483 | * is checked by mbedtls_ssl_conf_dtls_srtp_protection_profiles function |
Ron Eldor | 089c9fe | 2018-12-06 17:12:49 +0200 | [diff] [blame] | 484 | */ |
Johan Pascal | e79c1e8 | 2020-09-22 15:51:27 +0200 | [diff] [blame] | 485 | MBEDTLS_SSL_DEBUG_MSG( 3, |
| 486 | ( "client hello, " |
Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 487 | "illegal DTLS-SRTP protection profile %d", |
Johan Pascal | e79c1e8 | 2020-09-22 15:51:27 +0200 | [diff] [blame] | 488 | ssl->conf->dtls_srtp_profile_list[protection_profiles_index] |
| 489 | ) ); |
Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 490 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 494 | *p++ = mki_len & 0xFF; |
| 495 | |
| 496 | if( mki_len != 0 ) |
| 497 | { |
Ron Eldor | 75870ec | 2018-12-06 17:31:55 +0200 | [diff] [blame] | 498 | memcpy( p, ssl->dtls_srtp_info.mki_value, mki_len ); |
Ron Eldor | 313d7b5 | 2018-12-10 14:56:21 +0200 | [diff] [blame] | 499 | /* |
| 500 | * Increment p to point to the current position. |
| 501 | */ |
| 502 | p += mki_len; |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 503 | MBEDTLS_SSL_DEBUG_BUF( 3, "sending mki", ssl->dtls_srtp_info.mki_value, |
| 504 | ssl->dtls_srtp_info.mki_len ); |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 505 | } |
| 506 | |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 507 | /* |
| 508 | * total extension length: extension type (2 bytes) |
| 509 | * + extension length (2 bytes) |
| 510 | * + protection profile length (2 bytes) |
| 511 | * + 2 * number of protection profiles |
| 512 | * + srtp_mki vector length(1 byte) |
Ron Eldor | 313d7b5 | 2018-12-10 14:56:21 +0200 | [diff] [blame] | 513 | * + mki value |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 514 | */ |
Ron Eldor | 313d7b5 | 2018-12-10 14:56:21 +0200 | [diff] [blame] | 515 | *olen = p - buf; |
Johan Pascal | 77696ee | 2020-09-22 21:49:40 +0200 | [diff] [blame] | 516 | |
| 517 | return( 0 ); |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 518 | } |
| 519 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ |
| 520 | |
Ronald Cron | 4079abc | 2022-02-20 10:35:26 +0100 | [diff] [blame] | 521 | int mbedtls_ssl_tls12_write_client_hello_exts( mbedtls_ssl_context *ssl, |
| 522 | unsigned char *buf, |
| 523 | const unsigned char *end, |
| 524 | int uses_ec, |
| 525 | size_t *out_len ) |
Ronald Cron | 12dcdf0 | 2022-02-16 15:28:22 +0100 | [diff] [blame] | 526 | { |
| 527 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 528 | unsigned char *p = buf; |
| 529 | size_t ext_len = 0; |
| 530 | |
| 531 | (void) ssl; |
| 532 | (void) end; |
| 533 | (void) uses_ec; |
| 534 | (void) ret; |
| 535 | (void) ext_len; |
| 536 | |
| 537 | *out_len = 0; |
| 538 | |
| 539 | /* Note that TLS_EMPTY_RENEGOTIATION_INFO_SCSV is always added |
| 540 | * even if MBEDTLS_SSL_RENEGOTIATION is not defined. */ |
| 541 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 542 | if( ( ret = ssl_write_renegotiation_ext( ssl, p, end, &ext_len ) ) != 0 ) |
| 543 | { |
| 544 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_renegotiation_ext", ret ); |
| 545 | return( ret ); |
| 546 | } |
| 547 | p += ext_len; |
| 548 | #endif |
| 549 | |
| 550 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 551 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 552 | if( uses_ec ) |
| 553 | { |
| 554 | if( ( ret = ssl_write_supported_point_formats_ext( ssl, p, end, |
| 555 | &ext_len ) ) != 0 ) |
| 556 | { |
| 557 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_supported_point_formats_ext", ret ); |
| 558 | return( ret ); |
| 559 | } |
| 560 | p += ext_len; |
| 561 | } |
| 562 | #endif |
| 563 | |
| 564 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 565 | if( ( ret = ssl_write_ecjpake_kkpp_ext( ssl, p, end, &ext_len ) ) != 0 ) |
| 566 | { |
| 567 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_ecjpake_kkpp_ext", ret ); |
| 568 | return( ret ); |
| 569 | } |
| 570 | p += ext_len; |
| 571 | #endif |
| 572 | |
| 573 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 574 | if( ( ret = ssl_write_cid_ext( ssl, p, end, &ext_len ) ) != 0 ) |
| 575 | { |
| 576 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_cid_ext", ret ); |
| 577 | return( ret ); |
| 578 | } |
| 579 | p += ext_len; |
| 580 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 581 | |
| 582 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 583 | if( ( ret = ssl_write_max_fragment_length_ext( ssl, p, end, |
| 584 | &ext_len ) ) != 0 ) |
| 585 | { |
| 586 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_max_fragment_length_ext", ret ); |
| 587 | return( ret ); |
| 588 | } |
| 589 | p += ext_len; |
| 590 | #endif |
| 591 | |
| 592 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 593 | if( ( ret = ssl_write_encrypt_then_mac_ext( ssl, p, end, &ext_len ) ) != 0 ) |
| 594 | { |
| 595 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_encrypt_then_mac_ext", ret ); |
| 596 | return( ret ); |
| 597 | } |
| 598 | p += ext_len; |
| 599 | #endif |
| 600 | |
| 601 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 602 | if( ( ret = ssl_write_extended_ms_ext( ssl, p, end, &ext_len ) ) != 0 ) |
| 603 | { |
| 604 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_extended_ms_ext", ret ); |
| 605 | return( ret ); |
| 606 | } |
| 607 | p += ext_len; |
| 608 | #endif |
| 609 | |
| 610 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
| 611 | if( ( ret = ssl_write_use_srtp_ext( ssl, p, end, &ext_len ) ) != 0 ) |
| 612 | { |
| 613 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_use_srtp_ext", ret ); |
| 614 | return( ret ); |
| 615 | } |
| 616 | p += ext_len; |
| 617 | #endif |
| 618 | |
| 619 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 620 | if( ( ret = ssl_write_session_ticket_ext( ssl, p, end, &ext_len ) ) != 0 ) |
| 621 | { |
| 622 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_session_ticket_ext", ret ); |
| 623 | return( ret ); |
| 624 | } |
| 625 | p += ext_len; |
| 626 | #endif |
| 627 | |
| 628 | *out_len = p - buf; |
| 629 | |
| 630 | return( 0 ); |
| 631 | } |
| 632 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 633 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 634 | static int ssl_parse_renegotiation_info( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | e048b67 | 2013-07-19 12:47:00 +0200 | [diff] [blame] | 635 | const unsigned char *buf, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 636 | size_t len ) |
| 637 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 638 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 639 | if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 640 | { |
Manuel Pégourié-Gonnard | 31ff1d2 | 2013-10-28 13:46:11 +0100 | [diff] [blame] | 641 | /* Check verify-data in constant-time. The length OTOH is no secret */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 642 | if( len != 1 + ssl->verify_data_len * 2 || |
| 643 | buf[0] != ssl->verify_data_len * 2 || |
Gabor Mezei | 90437e3 | 2021-10-20 11:59:27 +0200 | [diff] [blame] | 644 | mbedtls_ct_memcmp( buf + 1, |
Manuel Pégourié-Gonnard | 31ff1d2 | 2013-10-28 13:46:11 +0100 | [diff] [blame] | 645 | ssl->own_verify_data, ssl->verify_data_len ) != 0 || |
Gabor Mezei | 90437e3 | 2021-10-20 11:59:27 +0200 | [diff] [blame] | 646 | mbedtls_ct_memcmp( buf + 1 + ssl->verify_data_len, |
Manuel Pégourié-Gonnard | 31ff1d2 | 2013-10-28 13:46:11 +0100 | [diff] [blame] | 647 | ssl->peer_verify_data, ssl->verify_data_len ) != 0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 648 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 649 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) ); |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 650 | mbedtls_ssl_send_alert_message( |
| 651 | ssl, |
| 652 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 653 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Dave Rodgman | 43fcb8d | 2021-06-28 21:49:15 +0100 | [diff] [blame] | 654 | return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 655 | } |
| 656 | } |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 657 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 658 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 659 | { |
| 660 | if( len != 1 || buf[0] != 0x00 ) |
| 661 | { |
Ronald Cron | 5ee5707 | 2020-06-11 09:34:06 +0200 | [diff] [blame] | 662 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 663 | ( "non-zero length renegotiation info" ) ); |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 664 | mbedtls_ssl_send_alert_message( |
| 665 | ssl, |
| 666 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 667 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Dave Rodgman | 43fcb8d | 2021-06-28 21:49:15 +0100 | [diff] [blame] | 668 | return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 669 | } |
| 670 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 671 | ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 672 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 673 | |
| 674 | return( 0 ); |
| 675 | } |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 676 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 677 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 678 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 679 | static int ssl_parse_max_fragment_length_ext( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | e048b67 | 2013-07-19 12:47:00 +0200 | [diff] [blame] | 680 | const unsigned char *buf, |
Manuel Pégourié-Gonnard | de600e5 | 2013-07-17 10:14:38 +0200 | [diff] [blame] | 681 | size_t len ) |
| 682 | { |
| 683 | /* |
| 684 | * server should use the extension only if we did, |
| 685 | * and if so the server's value should match ours (and len is always 1) |
| 686 | */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 687 | if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE || |
Manuel Pégourié-Gonnard | de600e5 | 2013-07-17 10:14:38 +0200 | [diff] [blame] | 688 | len != 1 || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 689 | buf[0] != ssl->conf->mfl_code ) |
Manuel Pégourié-Gonnard | de600e5 | 2013-07-17 10:14:38 +0200 | [diff] [blame] | 690 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 691 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 692 | ( "non-matching max fragment length extension" ) ); |
| 693 | mbedtls_ssl_send_alert_message( |
| 694 | ssl, |
| 695 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
Hanno Becker | c3411d4 | 2021-06-24 11:09:00 +0100 | [diff] [blame] | 696 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
| 697 | return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER ); |
Manuel Pégourié-Gonnard | de600e5 | 2013-07-17 10:14:38 +0200 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | return( 0 ); |
| 701 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 702 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 703 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 704 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 705 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 706 | static int ssl_parse_cid_ext( mbedtls_ssl_context *ssl, |
| 707 | const unsigned char *buf, |
| 708 | size_t len ) |
| 709 | { |
| 710 | size_t peer_cid_len; |
| 711 | |
| 712 | if( /* CID extension only makes sense in DTLS */ |
| 713 | ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
| 714 | /* The server must only send the CID extension if we have offered it. */ |
Hanno Becker | 2262648 | 2019-05-03 12:46:59 +0100 | [diff] [blame] | 715 | ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED ) |
Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 716 | { |
Hanno Becker | 2262648 | 2019-05-03 12:46:59 +0100 | [diff] [blame] | 717 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension unexpected" ) ); |
Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 718 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
Dave Rodgman | 43fcb8d | 2021-06-28 21:49:15 +0100 | [diff] [blame] | 719 | MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT ); |
Dave Rodgman | 53c8689 | 2021-06-29 10:02:06 +0100 | [diff] [blame] | 720 | return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION ); |
Hanno Becker | 2262648 | 2019-05-03 12:46:59 +0100 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | if( len == 0 ) |
| 724 | { |
| 725 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) ); |
| 726 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
Hanno Becker | c3411d4 | 2021-06-24 11:09:00 +0100 | [diff] [blame] | 727 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 728 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | peer_cid_len = *buf++; |
| 732 | len--; |
| 733 | |
| 734 | if( peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX ) |
| 735 | { |
Hanno Becker | 2262648 | 2019-05-03 12:46:59 +0100 | [diff] [blame] | 736 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) ); |
Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 737 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
Hanno Becker | c3411d4 | 2021-06-24 11:09:00 +0100 | [diff] [blame] | 738 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
| 739 | return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER ); |
Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | if( len != peer_cid_len ) |
| 743 | { |
Hanno Becker | 2262648 | 2019-05-03 12:46:59 +0100 | [diff] [blame] | 744 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) ); |
Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 745 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
Hanno Becker | c3411d4 | 2021-06-24 11:09:00 +0100 | [diff] [blame] | 746 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 747 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 748 | } |
| 749 | |
Hanno Becker | 5a29990 | 2019-05-03 12:47:49 +0100 | [diff] [blame] | 750 | ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED; |
Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 751 | ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len; |
| 752 | memcpy( ssl->handshake->peer_cid, buf, peer_cid_len ); |
| 753 | |
| 754 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use of CID extension negotiated" ) ); |
| 755 | MBEDTLS_SSL_DEBUG_BUF( 3, "Server CID", buf, peer_cid_len ); |
| 756 | |
Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 757 | return( 0 ); |
| 758 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 759 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 760 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 761 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 762 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 763 | static int ssl_parse_encrypt_then_mac_ext( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 764 | const unsigned char *buf, |
| 765 | size_t len ) |
| 766 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 767 | if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED || |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 768 | len != 0 ) |
| 769 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 770 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 771 | ( "non-matching encrypt-then-MAC extension" ) ); |
| 772 | mbedtls_ssl_send_alert_message( |
| 773 | ssl, |
| 774 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
Dave Rodgman | 43fcb8d | 2021-06-28 21:49:15 +0100 | [diff] [blame] | 775 | MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT ); |
Dave Rodgman | 53c8689 | 2021-06-29 10:02:06 +0100 | [diff] [blame] | 776 | return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION ); |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | ((void) buf); |
| 780 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 781 | ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 782 | |
| 783 | return( 0 ); |
| 784 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 785 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 786 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 787 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 788 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 789 | static int ssl_parse_extended_ms_ext( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 790 | const unsigned char *buf, |
| 791 | size_t len ) |
| 792 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 793 | if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED || |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 794 | len != 0 ) |
| 795 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 796 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 797 | ( "non-matching extended master secret extension" ) ); |
| 798 | mbedtls_ssl_send_alert_message( |
| 799 | ssl, |
| 800 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
Dave Rodgman | bed8927 | 2021-06-29 12:06:32 +0100 | [diff] [blame] | 801 | MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT ); |
| 802 | return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION ); |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | ((void) buf); |
| 806 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 807 | ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 808 | |
| 809 | return( 0 ); |
| 810 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 811 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 812 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 813 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 814 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 815 | static int ssl_parse_session_ticket_ext( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 816 | const unsigned char *buf, |
| 817 | size_t len ) |
| 818 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 819 | if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED || |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 820 | len != 0 ) |
| 821 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 822 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 823 | ( "non-matching session ticket extension" ) ); |
| 824 | mbedtls_ssl_send_alert_message( |
| 825 | ssl, |
| 826 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
Dave Rodgman | bed8927 | 2021-06-29 12:06:32 +0100 | [diff] [blame] | 827 | MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT ); |
| 828 | return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION ); |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 829 | } |
Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 830 | |
| 831 | ((void) buf); |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 832 | |
| 833 | ssl->handshake->new_session_ticket = 1; |
Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 834 | |
| 835 | return( 0 ); |
| 836 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 837 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 838 | |
Robert Cragie | 136884c | 2015-10-02 13:34:31 +0100 | [diff] [blame] | 839 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 840 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 841 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 842 | static int ssl_parse_supported_point_formats_ext( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 843 | const unsigned char *buf, |
| 844 | size_t len ) |
| 845 | { |
| 846 | size_t list_size; |
| 847 | const unsigned char *p; |
| 848 | |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 849 | if( len == 0 || (size_t)( buf[0] + 1 ) != len ) |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 850 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 851 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 852 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 853 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Hanno Becker | c3411d4 | 2021-06-24 11:09:00 +0100 | [diff] [blame] | 854 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 855 | } |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 856 | list_size = buf[0]; |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 857 | |
Manuel Pégourié-Gonnard | fd35af1 | 2014-06-23 14:10:13 +0200 | [diff] [blame] | 858 | p = buf + 1; |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 859 | while( list_size > 0 ) |
| 860 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 861 | if( p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED || |
| 862 | p[0] == MBEDTLS_ECP_PF_COMPRESSED ) |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 863 | { |
Neil Armstrong | 3ea0149 | 2022-04-12 14:41:50 +0200 | [diff] [blame] | 864 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 865 | ( defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) ) |
Manuel Pégourié-Gonnard | 5734b2d | 2013-08-15 19:04:02 +0200 | [diff] [blame] | 866 | ssl->handshake->ecdh_ctx.point_format = p[0]; |
Neil Armstrong | 3ea0149 | 2022-04-12 14:41:50 +0200 | [diff] [blame] | 867 | #endif /* !MBEDTLS_USE_PSA_CRYPTO && |
| 868 | ( MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ) */ |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 869 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 870 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Gilles Peskine | cd07e22 | 2021-05-27 23:17:34 +0200 | [diff] [blame] | 871 | mbedtls_ecjpake_set_point_format( &ssl->handshake->ecjpake_ctx, |
| 872 | p[0] ); |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 873 | #endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 874 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) ); |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 875 | return( 0 ); |
| 876 | } |
| 877 | |
| 878 | list_size--; |
| 879 | p++; |
| 880 | } |
| 881 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 882 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no point format in common" ) ); |
Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 883 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 884 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Hanno Becker | c3411d4 | 2021-06-24 11:09:00 +0100 | [diff] [blame] | 885 | return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 886 | } |
Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 887 | #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || |
Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 888 | MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 889 | |
Manuel Pégourié-Gonnard | 0a1324a | 2015-09-16 16:01:00 +0200 | [diff] [blame] | 890 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 891 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 0a1324a | 2015-09-16 16:01:00 +0200 | [diff] [blame] | 892 | static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl, |
| 893 | const unsigned char *buf, |
| 894 | size_t len ) |
| 895 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 896 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 0a1324a | 2015-09-16 16:01:00 +0200 | [diff] [blame] | 897 | |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 898 | if( ssl->handshake->ciphersuite_info->key_exchange != |
Manuel Pégourié-Gonnard | 0a1324a | 2015-09-16 16:01:00 +0200 | [diff] [blame] | 899 | MBEDTLS_KEY_EXCHANGE_ECJPAKE ) |
| 900 | { |
| 901 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) ); |
| 902 | return( 0 ); |
| 903 | } |
| 904 | |
Manuel Pégourié-Gonnard | d0d8cb3 | 2015-09-17 14:16:30 +0200 | [diff] [blame] | 905 | /* If we got here, we no longer need our cached extension */ |
| 906 | mbedtls_free( ssl->handshake->ecjpake_cache ); |
| 907 | ssl->handshake->ecjpake_cache = NULL; |
| 908 | ssl->handshake->ecjpake_cache_len = 0; |
| 909 | |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 910 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame^] | 911 | if( ( ret = mbedtls_psa_ecjpake_read_round_one( |
Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 912 | &ssl->handshake->psa_pake_ctx, buf, len ) ) != 0 ) |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 913 | { |
Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 914 | psa_destroy_key( ssl->handshake->psa_pake_password ); |
| 915 | psa_pake_abort( &ssl->handshake->psa_pake_ctx ); |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 916 | |
Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 917 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_pake_input round one", ret ); |
| 918 | mbedtls_ssl_send_alert_message( |
| 919 | ssl, |
| 920 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 921 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
| 922 | return( ret ); |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | return( 0 ); |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 926 | #else |
Manuel Pégourié-Gonnard | 0a1324a | 2015-09-16 16:01:00 +0200 | [diff] [blame] | 927 | if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx, |
| 928 | buf, len ) ) != 0 ) |
| 929 | { |
| 930 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret ); |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 931 | mbedtls_ssl_send_alert_message( |
| 932 | ssl, |
| 933 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 934 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 0a1324a | 2015-09-16 16:01:00 +0200 | [diff] [blame] | 935 | return( ret ); |
| 936 | } |
| 937 | |
| 938 | return( 0 ); |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 939 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 0a1324a | 2015-09-16 16:01:00 +0200 | [diff] [blame] | 940 | } |
| 941 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 942 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 943 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 944 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 945 | static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 946 | const unsigned char *buf, size_t len ) |
| 947 | { |
| 948 | size_t list_len, name_len; |
| 949 | const char **p; |
| 950 | |
| 951 | /* If we didn't send it, the server shouldn't send it */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 952 | if( ssl->conf->alpn_list == NULL ) |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 953 | { |
| 954 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching ALPN extension" ) ); |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 955 | mbedtls_ssl_send_alert_message( |
| 956 | ssl, |
| 957 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
Dave Rodgman | bed8927 | 2021-06-29 12:06:32 +0100 | [diff] [blame] | 958 | MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT ); |
| 959 | return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 960 | } |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 961 | |
| 962 | /* |
| 963 | * opaque ProtocolName<1..2^8-1>; |
| 964 | * |
| 965 | * struct { |
| 966 | * ProtocolName protocol_name_list<2..2^16-1> |
| 967 | * } ProtocolNameList; |
| 968 | * |
| 969 | * the "ProtocolNameList" MUST contain exactly one "ProtocolName" |
| 970 | */ |
| 971 | |
| 972 | /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */ |
| 973 | if( len < 4 ) |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 974 | { |
| 975 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 976 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Hanno Becker | c3411d4 | 2021-06-24 11:09:00 +0100 | [diff] [blame] | 977 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 978 | } |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 979 | |
| 980 | list_len = ( buf[0] << 8 ) | buf[1]; |
| 981 | if( list_len != len - 2 ) |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 982 | { |
| 983 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 984 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Hanno Becker | c3411d4 | 2021-06-24 11:09:00 +0100 | [diff] [blame] | 985 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 986 | } |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 987 | |
| 988 | name_len = buf[2]; |
| 989 | if( name_len != list_len - 1 ) |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 990 | { |
| 991 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 992 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Hanno Becker | c3411d4 | 2021-06-24 11:09:00 +0100 | [diff] [blame] | 993 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 994 | } |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 995 | |
| 996 | /* Check that the server chosen protocol was in our list and save it */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 997 | for( p = ssl->conf->alpn_list; *p != NULL; p++ ) |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 998 | { |
| 999 | if( name_len == strlen( *p ) && |
| 1000 | memcmp( buf + 3, *p, name_len ) == 0 ) |
| 1001 | { |
| 1002 | ssl->alpn_chosen = *p; |
| 1003 | return( 0 ); |
| 1004 | } |
| 1005 | } |
| 1006 | |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1007 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "ALPN extension: no matching protocol" ) ); |
Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 1008 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1009 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Hanno Becker | c3411d4 | 2021-06-24 11:09:00 +0100 | [diff] [blame] | 1010 | return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 1011 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1012 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 1013 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1014 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1015 | MBEDTLS_CHECK_RETURN_CRITICAL |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1016 | static int ssl_parse_use_srtp_ext( mbedtls_ssl_context *ssl, |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 1017 | const unsigned char *buf, |
| 1018 | size_t len ) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1019 | { |
Johan Pascal | 43f9490 | 2020-09-22 12:25:52 +0200 | [diff] [blame] | 1020 | mbedtls_ssl_srtp_profile server_protection = MBEDTLS_TLS_SRTP_UNSET; |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 1021 | size_t i, mki_len = 0; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1022 | uint16_t server_protection_profile_value = 0; |
| 1023 | |
| 1024 | /* If use_srtp is not configured, just ignore the extension */ |
Johan Pascal | c3ccd98 | 2020-10-28 17:18:18 +0100 | [diff] [blame] | 1025 | if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) || |
| 1026 | ( ssl->conf->dtls_srtp_profile_list == NULL ) || |
| 1027 | ( ssl->conf->dtls_srtp_profile_list_len == 0 ) ) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1028 | return( 0 ); |
| 1029 | |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 1030 | /* RFC 5764 section 4.1.1 |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1031 | * uint8 SRTPProtectionProfile[2]; |
| 1032 | * |
| 1033 | * struct { |
| 1034 | * SRTPProtectionProfiles SRTPProtectionProfiles; |
| 1035 | * opaque srtp_mki<0..255>; |
| 1036 | * } UseSRTPData; |
| 1037 | |
| 1038 | * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>; |
| 1039 | * |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1040 | */ |
Johan Pascal | f6417ec | 2020-09-22 15:15:19 +0200 | [diff] [blame] | 1041 | if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED ) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 1042 | { |
| 1043 | mki_len = ssl->dtls_srtp_info.mki_len; |
| 1044 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1045 | |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 1046 | /* |
Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 1047 | * Length is 5 + optional mki_value : one protection profile length (2 bytes) |
| 1048 | * + protection profile (2 bytes) |
| 1049 | * + mki_len(1 byte) |
Ron Eldor | 313d7b5 | 2018-12-10 14:56:21 +0200 | [diff] [blame] | 1050 | * and optional srtp_mki |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 1051 | */ |
Johan Pascal | adbd944 | 2020-10-26 21:24:25 +0100 | [diff] [blame] | 1052 | if( ( len < 5 ) || ( len != ( buf[4] + 5u ) ) ) |
Hanno Becker | c3411d4 | 2021-06-24 11:09:00 +0100 | [diff] [blame] | 1053 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1054 | |
| 1055 | /* |
| 1056 | * get the server protection profile |
| 1057 | */ |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 1058 | |
| 1059 | /* |
| 1060 | * protection profile length must be 0x0002 as we must have only |
| 1061 | * one protection profile in server Hello |
| 1062 | */ |
Ron Eldor | 089c9fe | 2018-12-06 17:12:49 +0200 | [diff] [blame] | 1063 | if( ( buf[0] != 0 ) || ( buf[1] != 2 ) ) |
Hanno Becker | c3411d4 | 2021-06-24 11:09:00 +0100 | [diff] [blame] | 1064 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Ron Eldor | 089c9fe | 2018-12-06 17:12:49 +0200 | [diff] [blame] | 1065 | |
| 1066 | server_protection_profile_value = ( buf[2] << 8 ) | buf[3]; |
Johan Pascal | 43f9490 | 2020-09-22 12:25:52 +0200 | [diff] [blame] | 1067 | server_protection = mbedtls_ssl_check_srtp_profile_value( |
| 1068 | server_protection_profile_value ); |
| 1069 | if( server_protection != MBEDTLS_TLS_SRTP_UNSET ) |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 1070 | { |
Johan Pascal | 43f9490 | 2020-09-22 12:25:52 +0200 | [diff] [blame] | 1071 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found srtp profile: %s", |
| 1072 | mbedtls_ssl_get_srtp_profile_as_string( |
| 1073 | server_protection ) ) ); |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1074 | } |
| 1075 | |
Johan Pascal | 43f9490 | 2020-09-22 12:25:52 +0200 | [diff] [blame] | 1076 | ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET; |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 1077 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1078 | /* |
| 1079 | * Check we have the server profile in our list |
| 1080 | */ |
Ron Eldor | 3adb992 | 2017-12-21 10:15:08 +0200 | [diff] [blame] | 1081 | for( i=0; i < ssl->conf->dtls_srtp_profile_list_len; i++) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1082 | { |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 1083 | if( server_protection == ssl->conf->dtls_srtp_profile_list[i] ) |
| 1084 | { |
Ron Eldor | 3adb992 | 2017-12-21 10:15:08 +0200 | [diff] [blame] | 1085 | ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i]; |
Johan Pascal | 43f9490 | 2020-09-22 12:25:52 +0200 | [diff] [blame] | 1086 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected srtp profile: %s", |
| 1087 | mbedtls_ssl_get_srtp_profile_as_string( |
| 1088 | server_protection ) ) ); |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 1089 | break; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1090 | } |
| 1091 | } |
| 1092 | |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 1093 | /* If no match was found : server problem, it shall never answer with incompatible profile */ |
Johan Pascal | 43f9490 | 2020-09-22 12:25:52 +0200 | [diff] [blame] | 1094 | if( ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET ) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 1095 | { |
| 1096 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 1097 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Hanno Becker | c3411d4 | 2021-06-24 11:09:00 +0100 | [diff] [blame] | 1098 | return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE ); |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 1099 | } |
Johan Pascal | 20c7db3 | 2020-10-26 22:45:58 +0100 | [diff] [blame] | 1100 | |
| 1101 | /* If server does not use mki in its reply, make sure the client won't keep |
| 1102 | * one as negotiated */ |
| 1103 | if( len == 5 ) |
| 1104 | { |
| 1105 | ssl->dtls_srtp_info.mki_len = 0; |
| 1106 | } |
| 1107 | |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 1108 | /* |
| 1109 | * RFC5764: |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 1110 | * If the client detects a nonzero-length MKI in the server's response |
| 1111 | * that is different than the one the client offered, then the client |
| 1112 | * MUST abort the handshake and SHOULD send an invalid_parameter alert. |
| 1113 | */ |
Ron Eldor | 313d7b5 | 2018-12-10 14:56:21 +0200 | [diff] [blame] | 1114 | if( len > 5 && ( buf[4] != mki_len || |
| 1115 | ( memcmp( ssl->dtls_srtp_info.mki_value, &buf[5], mki_len ) ) ) ) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 1116 | { |
| 1117 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1118 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Hanno Becker | c3411d4 | 2021-06-24 11:09:00 +0100 | [diff] [blame] | 1119 | return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER ); |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 1120 | } |
Ron Eldor | b465539 | 2018-07-05 18:25:39 +0300 | [diff] [blame] | 1121 | #if defined (MBEDTLS_DEBUG_C) |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 1122 | if( len > 5 ) |
Ron Eldor | b465539 | 2018-07-05 18:25:39 +0300 | [diff] [blame] | 1123 | { |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 1124 | MBEDTLS_SSL_DEBUG_BUF( 3, "received mki", ssl->dtls_srtp_info.mki_value, |
| 1125 | ssl->dtls_srtp_info.mki_len ); |
Ron Eldor | b465539 | 2018-07-05 18:25:39 +0300 | [diff] [blame] | 1126 | } |
| 1127 | #endif |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 1128 | return( 0 ); |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1129 | } |
| 1130 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ |
| 1131 | |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1132 | /* |
| 1133 | * Parse HelloVerifyRequest. Only called after verifying the HS type. |
| 1134 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1135 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1136 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1137 | static int ssl_parse_hello_verify_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1138 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1139 | const unsigned char *p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ); |
Glenn Strauss | 8315811 | 2022-04-13 14:59:34 -0400 | [diff] [blame] | 1140 | uint16_t dtls_legacy_version; |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1141 | unsigned char cookie_len; |
| 1142 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1143 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse hello verify request" ) ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1144 | |
Gilles Peskine | b64bf06 | 2019-09-27 14:02:44 +0200 | [diff] [blame] | 1145 | /* Check that there is enough room for: |
| 1146 | * - 2 bytes of version |
| 1147 | * - 1 byte of cookie_len |
| 1148 | */ |
| 1149 | if( mbedtls_ssl_hs_hdr_len( ssl ) + 3 > ssl->in_msglen ) |
| 1150 | { |
| 1151 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 1152 | ( "incoming HelloVerifyRequest message is too short" ) ); |
| 1153 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1154 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Hanno Becker | c3411d4 | 2021-06-24 11:09:00 +0100 | [diff] [blame] | 1155 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Gilles Peskine | b64bf06 | 2019-09-27 14:02:44 +0200 | [diff] [blame] | 1156 | } |
| 1157 | |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1158 | /* |
| 1159 | * struct { |
| 1160 | * ProtocolVersion server_version; |
| 1161 | * opaque cookie<0..2^8-1>; |
| 1162 | * } HelloVerifyRequest; |
| 1163 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1164 | MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 ); |
Glenn Strauss | 8315811 | 2022-04-13 14:59:34 -0400 | [diff] [blame] | 1165 | dtls_legacy_version = MBEDTLS_GET_UINT16_BE( p, 0 ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1166 | p += 2; |
| 1167 | |
TRodziewicz | 2d8800e | 2021-05-13 19:14:19 +0200 | [diff] [blame] | 1168 | /* |
Glenn Strauss | 8315811 | 2022-04-13 14:59:34 -0400 | [diff] [blame] | 1169 | * Since the RFC is not clear on this point, accept DTLS 1.0 (0xfeff) |
| 1170 | * The DTLS 1.3 (current draft) renames ProtocolVersion server_version to |
| 1171 | * legacy_version and locks the value of legacy_version to 0xfefd (DTLS 1.2) |
TRodziewicz | 2d8800e | 2021-05-13 19:14:19 +0200 | [diff] [blame] | 1172 | */ |
Glenn Strauss | 8315811 | 2022-04-13 14:59:34 -0400 | [diff] [blame] | 1173 | if( dtls_legacy_version != 0xfefd && dtls_legacy_version != 0xfeff ) |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1174 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1175 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server version" ) ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1176 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1177 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1178 | MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1179 | |
Hanno Becker | bc00044 | 2021-06-24 09:18:19 +0100 | [diff] [blame] | 1180 | return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1181 | } |
| 1182 | |
| 1183 | cookie_len = *p++; |
Andres AG | 5a87c93 | 2016-09-26 14:53:05 +0100 | [diff] [blame] | 1184 | if( ( ssl->in_msg + ssl->in_msglen ) - p < cookie_len ) |
| 1185 | { |
| 1186 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 1187 | ( "cookie length does not match incoming message size" ) ); |
| 1188 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1189 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Hanno Becker | c3411d4 | 2021-06-24 11:09:00 +0100 | [diff] [blame] | 1190 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Andres AG | 5a87c93 | 2016-09-26 14:53:05 +0100 | [diff] [blame] | 1191 | } |
Gilles Peskine | b51130d | 2019-09-27 14:00:36 +0200 | [diff] [blame] | 1192 | MBEDTLS_SSL_DEBUG_BUF( 3, "cookie", p, cookie_len ); |
Andres AG | 5a87c93 | 2016-09-26 14:53:05 +0100 | [diff] [blame] | 1193 | |
XiaokangQian | 9b93c0d | 2022-02-09 06:02:25 +0000 | [diff] [blame] | 1194 | mbedtls_free( ssl->handshake->cookie ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1195 | |
XiaokangQian | 9b93c0d | 2022-02-09 06:02:25 +0000 | [diff] [blame] | 1196 | ssl->handshake->cookie = mbedtls_calloc( 1, cookie_len ); |
| 1197 | if( ssl->handshake->cookie == NULL ) |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1198 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 1199 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", cookie_len ) ); |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1200 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1201 | } |
| 1202 | |
XiaokangQian | 9b93c0d | 2022-02-09 06:02:25 +0000 | [diff] [blame] | 1203 | memcpy( ssl->handshake->cookie, p, cookie_len ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1204 | ssl->handshake->verify_cookie_len = cookie_len; |
| 1205 | |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 1206 | /* Start over at ClientHello */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1207 | ssl->state = MBEDTLS_SSL_CLIENT_HELLO; |
| 1208 | mbedtls_ssl_reset_checksum( ssl ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1209 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1210 | mbedtls_ssl_recv_flight_completed( ssl ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 1211 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1212 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse hello verify request" ) ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1213 | |
| 1214 | return( 0 ); |
| 1215 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1216 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1217 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1218 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1219 | static int ssl_parse_server_hello( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1220 | { |
Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1221 | int ret, i; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 1222 | size_t n; |
Manuel Pégourié-Gonnard | f7cdbc0 | 2014-10-17 17:02:10 +0200 | [diff] [blame] | 1223 | size_t ext_len; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1224 | unsigned char *buf, *ext; |
Manuel Pégourié-Gonnard | 1cf7b30 | 2015-06-24 22:28:19 +0200 | [diff] [blame] | 1225 | unsigned char comp; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1226 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1227 | int renegotiation_info_seen = 0; |
Manuel Pégourié-Gonnard | eaecbd3 | 2014-11-06 02:38:02 +0100 | [diff] [blame] | 1228 | #endif |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1229 | int handshake_failure = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1230 | const mbedtls_ssl_ciphersuite_t *suite_info; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1231 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1232 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1233 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 1234 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1235 | { |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1236 | /* No alert on a read error. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1237 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1238 | return( ret ); |
| 1239 | } |
| 1240 | |
Hanno Becker | 79594fd | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 1241 | buf = ssl->in_msg; |
| 1242 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1243 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1244 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1245 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1246 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | 6591962 | 2014-08-19 12:50:30 +0200 | [diff] [blame] | 1247 | { |
Manuel Pégourié-Gonnard | 44ade65 | 2014-08-19 13:58:40 +0200 | [diff] [blame] | 1248 | ssl->renego_records_seen++; |
| 1249 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1250 | if( ssl->conf->renego_max_records >= 0 && |
| 1251 | ssl->renego_records_seen > ssl->conf->renego_max_records ) |
Manuel Pégourié-Gonnard | 44ade65 | 2014-08-19 13:58:40 +0200 | [diff] [blame] | 1252 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1253 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 1254 | ( "renegotiation requested, but not honored by server" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1255 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 44ade65 | 2014-08-19 13:58:40 +0200 | [diff] [blame] | 1256 | } |
| 1257 | |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1258 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 1259 | ( "non-handshake message during renegotiation" ) ); |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 1260 | |
| 1261 | ssl->keep_current_message = 1; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1262 | return( MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO ); |
Manuel Pégourié-Gonnard | 6591962 | 2014-08-19 12:50:30 +0200 | [diff] [blame] | 1263 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1264 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 6591962 | 2014-08-19 12:50:30 +0200 | [diff] [blame] | 1265 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1266 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1267 | mbedtls_ssl_send_alert_message( |
| 1268 | ssl, |
| 1269 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1270 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1271 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1272 | } |
| 1273 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1274 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1275 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1276 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1277 | if( buf[0] == MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST ) |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1278 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1279 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "received hello verify request" ) ); |
| 1280 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1281 | return( ssl_parse_hello_verify_request( ssl ) ); |
| 1282 | } |
| 1283 | else |
| 1284 | { |
| 1285 | /* We made it through the verification process */ |
XiaokangQian | 9b93c0d | 2022-02-09 06:02:25 +0000 | [diff] [blame] | 1286 | mbedtls_free( ssl->handshake->cookie ); |
| 1287 | ssl->handshake->cookie = NULL; |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1288 | ssl->handshake->verify_cookie_len = 0; |
| 1289 | } |
| 1290 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1291 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1292 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1293 | if( ssl->in_hslen < 38 + mbedtls_ssl_hs_hdr_len( ssl ) || |
| 1294 | buf[0] != MBEDTLS_SSL_HS_SERVER_HELLO ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1295 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1296 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1297 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1298 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Hanno Becker | c3411d4 | 2021-06-24 11:09:00 +0100 | [diff] [blame] | 1299 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1300 | } |
| 1301 | |
Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 1302 | /* |
| 1303 | * 0 . 1 server_version |
| 1304 | * 2 . 33 random (maybe including 4 bytes of Unix time) |
| 1305 | * 34 . 34 session_id length = n |
| 1306 | * 35 . 34+n session_id |
| 1307 | * 35+n . 36+n cipher_suite |
| 1308 | * 37+n . 37+n compression_method |
| 1309 | * |
| 1310 | * 38+n . 39+n extensions length (optional) |
| 1311 | * 40+n . .. extensions |
| 1312 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1313 | buf += mbedtls_ssl_hs_hdr_len( ssl ); |
Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 1314 | |
Glenn Strauss | e3af4cb | 2022-03-15 03:23:42 -0400 | [diff] [blame] | 1315 | MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", buf, 2 ); |
| 1316 | ssl->tls_version = mbedtls_ssl_read_version( buf, ssl->conf->transport ); |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 1317 | ssl->session_negotiate->tls_version = ssl->tls_version; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1318 | |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 1319 | if( ssl->tls_version < ssl->conf->min_tls_version || |
| 1320 | ssl->tls_version > ssl->conf->max_tls_version ) |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 1321 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1322 | MBEDTLS_SSL_DEBUG_MSG( 1, |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 1323 | ( "server version out of bounds - min: [0x%x], server: [0x%x], max: [0x%x]", |
| 1324 | (unsigned)ssl->conf->min_tls_version, |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 1325 | (unsigned)ssl->tls_version, |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 1326 | (unsigned)ssl->conf->max_tls_version ) ); |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 1327 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1328 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1329 | MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION ); |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 1330 | |
Hanno Becker | bc00044 | 2021-06-24 09:18:19 +0100 | [diff] [blame] | 1331 | return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION ); |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 1332 | } |
| 1333 | |
Andres Amaya Garcia | 6bce9cb | 2017-09-06 15:33:34 +0100 | [diff] [blame] | 1334 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", |
Paul Elliott | 9f35211 | 2020-12-09 14:55:45 +0000 | [diff] [blame] | 1335 | ( (unsigned long) buf[2] << 24 ) | |
| 1336 | ( (unsigned long) buf[3] << 16 ) | |
| 1337 | ( (unsigned long) buf[4] << 8 ) | |
| 1338 | ( (unsigned long) buf[5] ) ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1339 | |
Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 1340 | memcpy( ssl->handshake->randbytes + 32, buf + 2, 32 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1341 | |
Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 1342 | n = buf[34]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1343 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1344 | MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 2, 32 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1345 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1346 | if( n > 32 ) |
| 1347 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1348 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1349 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1350 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Hanno Becker | c3411d4 | 2021-06-24 11:09:00 +0100 | [diff] [blame] | 1351 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1352 | } |
| 1353 | |
Manuel Pégourié-Gonnard | a6e5bd5 | 2015-07-23 12:14:13 +0200 | [diff] [blame] | 1354 | if( ssl->in_hslen > mbedtls_ssl_hs_hdr_len( ssl ) + 39 + n ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1355 | { |
Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 1356 | ext_len = ( ( buf[38 + n] << 8 ) |
| 1357 | | ( buf[39 + n] ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1358 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1359 | if( ( ext_len > 0 && ext_len < 4 ) || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1360 | ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 40 + n + ext_len ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1361 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1362 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1363 | mbedtls_ssl_send_alert_message( |
| 1364 | ssl, |
| 1365 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1366 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Hanno Becker | c3411d4 | 2021-06-24 11:09:00 +0100 | [diff] [blame] | 1367 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1368 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1369 | } |
Manuel Pégourié-Gonnard | a6e5bd5 | 2015-07-23 12:14:13 +0200 | [diff] [blame] | 1370 | else if( ssl->in_hslen == mbedtls_ssl_hs_hdr_len( ssl ) + 38 + n ) |
Manuel Pégourié-Gonnard | f7cdbc0 | 2014-10-17 17:02:10 +0200 | [diff] [blame] | 1371 | { |
| 1372 | ext_len = 0; |
| 1373 | } |
| 1374 | else |
| 1375 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1376 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1377 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1378 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Hanno Becker | c3411d4 | 2021-06-24 11:09:00 +0100 | [diff] [blame] | 1379 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | f7cdbc0 | 2014-10-17 17:02:10 +0200 | [diff] [blame] | 1380 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1381 | |
Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1382 | /* ciphersuite (used later) */ |
Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 1383 | i = ( buf[35 + n] << 8 ) | buf[36 + n]; |
Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1384 | |
| 1385 | /* |
| 1386 | * Read and check compression |
| 1387 | */ |
Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 1388 | comp = buf[37 + n]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1389 | |
Manuel Pégourié-Gonnard | 1cf7b30 | 2015-06-24 22:28:19 +0200 | [diff] [blame] | 1390 | if( comp != MBEDTLS_SSL_COMPRESS_NULL ) |
Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1391 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1392 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 1393 | ( "server hello, bad compression: %d", comp ) ); |
| 1394 | mbedtls_ssl_send_alert_message( |
| 1395 | ssl, |
| 1396 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1397 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1398 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1399 | } |
| 1400 | |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1401 | /* |
| 1402 | * Initialize update checksum functions |
| 1403 | */ |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1404 | ssl->handshake->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( i ); |
| 1405 | if( ssl->handshake->ciphersuite_info == NULL ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1406 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1407 | MBEDTLS_SSL_DEBUG_MSG( 1, |
Paul Elliott | 9f35211 | 2020-12-09 14:55:45 +0000 | [diff] [blame] | 1408 | ( "ciphersuite info for %04x not found", (unsigned int)i ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1409 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1410 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1411 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1412 | } |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1413 | |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1414 | mbedtls_ssl_optimize_checksum( ssl, ssl->handshake->ciphersuite_info ); |
Manuel Pégourié-Gonnard | 3c599f1 | 2014-03-10 13:25:07 +0100 | [diff] [blame] | 1415 | |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 1416 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1417 | MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 35, n ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1418 | |
| 1419 | /* |
| 1420 | * Check if the session can be resumed |
| 1421 | */ |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1422 | if( ssl->handshake->resume == 0 || n == 0 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1423 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1424 | ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE || |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1425 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1426 | ssl->session_negotiate->ciphersuite != i || |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 1427 | ssl->session_negotiate->id_len != n || |
Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 1428 | memcmp( ssl->session_negotiate->id, buf + 35, n ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1429 | { |
| 1430 | ssl->state++; |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 1431 | ssl->handshake->resume = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1432 | #if defined(MBEDTLS_HAVE_TIME) |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 1433 | ssl->session_negotiate->start = mbedtls_time( NULL ); |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 1434 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1435 | ssl->session_negotiate->ciphersuite = i; |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 1436 | ssl->session_negotiate->id_len = n; |
Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 1437 | memcpy( ssl->session_negotiate->id, buf + 35, n ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1438 | } |
| 1439 | else |
| 1440 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1441 | ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1442 | } |
| 1443 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1444 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed", |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 1445 | ssl->handshake->resume ? "a" : "no" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1446 | |
Paul Elliott | 3891caf | 2020-12-17 18:42:40 +0000 | [diff] [blame] | 1447 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %04x", (unsigned) i ) ); |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1448 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d", |
| 1449 | buf[37 + n] ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1450 | |
Andrzej Kurek | 03bac44 | 2018-04-25 05:06:07 -0400 | [diff] [blame] | 1451 | /* |
| 1452 | * Perform cipher suite validation in same way as in ssl_write_client_hello. |
Mohammad Azim Khan | 1d3b508 | 2018-04-18 19:35:00 +0100 | [diff] [blame] | 1453 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1454 | i = 0; |
| 1455 | while( 1 ) |
| 1456 | { |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 1457 | if( ssl->conf->ciphersuite_list[i] == 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1458 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1459 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1460 | mbedtls_ssl_send_alert_message( |
| 1461 | ssl, |
| 1462 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1463 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Hanno Becker | c3411d4 | 2021-06-24 11:09:00 +0100 | [diff] [blame] | 1464 | return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1465 | } |
| 1466 | |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 1467 | if( ssl->conf->ciphersuite_list[i++] == |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 1468 | ssl->session_negotiate->ciphersuite ) |
| 1469 | { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1470 | break; |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 1471 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1472 | } |
| 1473 | |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1474 | suite_info = mbedtls_ssl_ciphersuite_from_id( |
| 1475 | ssl->session_negotiate->ciphersuite ); |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 1476 | if( mbedtls_ssl_validate_ciphersuite( ssl, suite_info, ssl->tls_version, |
| 1477 | ssl->tls_version ) != 0 ) |
Mohammad Azim Khan | 1d3b508 | 2018-04-18 19:35:00 +0100 | [diff] [blame] | 1478 | { |
| 1479 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1480 | mbedtls_ssl_send_alert_message( |
| 1481 | ssl, |
| 1482 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
Hanno Becker | c3411d4 | 2021-06-24 11:09:00 +0100 | [diff] [blame] | 1483 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
| 1484 | return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE ); |
Mohammad Azim Khan | 1d3b508 | 2018-04-18 19:35:00 +0100 | [diff] [blame] | 1485 | } |
| 1486 | |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1487 | MBEDTLS_SSL_DEBUG_MSG( 3, |
| 1488 | ( "server hello, chosen ciphersuite: %s", suite_info->name ) ); |
Mohammad Azim Khan | 1d3b508 | 2018-04-18 19:35:00 +0100 | [diff] [blame] | 1489 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1490 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 1491 | if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA && |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 1492 | ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 ) |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 1493 | { |
| 1494 | ssl->handshake->ecrs_enabled = 1; |
| 1495 | } |
| 1496 | #endif |
| 1497 | |
Thomas Daubney | 20f89a9 | 2022-06-20 15:12:19 +0100 | [diff] [blame] | 1498 | if( comp != MBEDTLS_SSL_COMPRESS_NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1499 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1500 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1501 | mbedtls_ssl_send_alert_message( |
| 1502 | ssl, |
| 1503 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1504 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Hanno Becker | c3411d4 | 2021-06-24 11:09:00 +0100 | [diff] [blame] | 1505 | return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1506 | } |
| 1507 | |
Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 1508 | ext = buf + 40 + n; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1509 | |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1510 | MBEDTLS_SSL_DEBUG_MSG( 2, |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 1511 | ( "server hello, total extension length: %" MBEDTLS_PRINTF_SIZET, ext_len ) ); |
Manuel Pégourié-Gonnard | a052849 | 2013-07-16 17:26:28 +0200 | [diff] [blame] | 1512 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1513 | while( ext_len ) |
| 1514 | { |
| 1515 | unsigned int ext_id = ( ( ext[0] << 8 ) |
| 1516 | | ( ext[1] ) ); |
| 1517 | unsigned int ext_size = ( ( ext[2] << 8 ) |
| 1518 | | ( ext[3] ) ); |
| 1519 | |
| 1520 | if( ext_size + 4 > ext_len ) |
| 1521 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1522 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1523 | mbedtls_ssl_send_alert_message( |
| 1524 | ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1525 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Hanno Becker | c3411d4 | 2021-06-24 11:09:00 +0100 | [diff] [blame] | 1526 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1527 | } |
| 1528 | |
| 1529 | switch( ext_id ) |
| 1530 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1531 | case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO: |
| 1532 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) ); |
| 1533 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1534 | renegotiation_info_seen = 1; |
Manuel Pégourié-Gonnard | eaecbd3 | 2014-11-06 02:38:02 +0100 | [diff] [blame] | 1535 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1536 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 1537 | if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4, |
| 1538 | ext_size ) ) != 0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1539 | return( ret ); |
| 1540 | |
| 1541 | break; |
| 1542 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1543 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 1544 | case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH: |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1545 | MBEDTLS_SSL_DEBUG_MSG( 3, |
| 1546 | ( "found max_fragment_length extension" ) ); |
Manuel Pégourié-Gonnard | de600e5 | 2013-07-17 10:14:38 +0200 | [diff] [blame] | 1547 | |
| 1548 | if( ( ret = ssl_parse_max_fragment_length_ext( ssl, |
| 1549 | ext + 4, ext_size ) ) != 0 ) |
| 1550 | { |
| 1551 | return( ret ); |
| 1552 | } |
| 1553 | |
| 1554 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1555 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | de600e5 | 2013-07-17 10:14:38 +0200 | [diff] [blame] | 1556 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1557 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 1558 | case MBEDTLS_TLS_EXT_CID: |
| 1559 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found CID extension" ) ); |
| 1560 | |
| 1561 | if( ( ret = ssl_parse_cid_ext( ssl, |
| 1562 | ext + 4, |
| 1563 | ext_size ) ) != 0 ) |
| 1564 | { |
| 1565 | return( ret ); |
| 1566 | } |
| 1567 | |
| 1568 | break; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1569 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 1570 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1571 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 1572 | case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC: |
| 1573 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt_then_mac extension" ) ); |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 1574 | |
| 1575 | if( ( ret = ssl_parse_encrypt_then_mac_ext( ssl, |
| 1576 | ext + 4, ext_size ) ) != 0 ) |
| 1577 | { |
| 1578 | return( ret ); |
| 1579 | } |
| 1580 | |
| 1581 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1582 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 1583 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1584 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 1585 | case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET: |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1586 | MBEDTLS_SSL_DEBUG_MSG( 3, |
| 1587 | ( "found extended_master_secret extension" ) ); |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 1588 | |
| 1589 | if( ( ret = ssl_parse_extended_ms_ext( ssl, |
| 1590 | ext + 4, ext_size ) ) != 0 ) |
| 1591 | { |
| 1592 | return( ret ); |
| 1593 | } |
| 1594 | |
| 1595 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1596 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 1597 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1598 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 1599 | case MBEDTLS_TLS_EXT_SESSION_TICKET: |
| 1600 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) ); |
Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 1601 | |
| 1602 | if( ( ret = ssl_parse_session_ticket_ext( ssl, |
| 1603 | ext + 4, ext_size ) ) != 0 ) |
| 1604 | { |
| 1605 | return( ret ); |
| 1606 | } |
| 1607 | |
| 1608 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1609 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 1610 | |
Robert Cragie | 136884c | 2015-10-02 13:34:31 +0100 | [diff] [blame] | 1611 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 1612 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1613 | case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS: |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1614 | MBEDTLS_SSL_DEBUG_MSG( 3, |
| 1615 | ( "found supported_point_formats extension" ) ); |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 1616 | |
| 1617 | if( ( ret = ssl_parse_supported_point_formats_ext( ssl, |
| 1618 | ext + 4, ext_size ) ) != 0 ) |
| 1619 | { |
| 1620 | return( ret ); |
| 1621 | } |
| 1622 | |
| 1623 | break; |
Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 1624 | #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || |
| 1625 | MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 1626 | |
Manuel Pégourié-Gonnard | 0a1324a | 2015-09-16 16:01:00 +0200 | [diff] [blame] | 1627 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 1628 | case MBEDTLS_TLS_EXT_ECJPAKE_KKPP: |
| 1629 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake_kkpp extension" ) ); |
| 1630 | |
| 1631 | if( ( ret = ssl_parse_ecjpake_kkpp( ssl, |
| 1632 | ext + 4, ext_size ) ) != 0 ) |
| 1633 | { |
| 1634 | return( ret ); |
| 1635 | } |
| 1636 | |
| 1637 | break; |
| 1638 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1639 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1640 | #if defined(MBEDTLS_SSL_ALPN) |
| 1641 | case MBEDTLS_TLS_EXT_ALPN: |
| 1642 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) ); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 1643 | |
Johan Pascal | 275874b | 2020-10-27 10:43:53 +0100 | [diff] [blame] | 1644 | if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 ) |
| 1645 | return( ret ); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 1646 | |
| 1647 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1648 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 1649 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1650 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
| 1651 | case MBEDTLS_TLS_EXT_USE_SRTP: |
| 1652 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found use_srtp extension" ) ); |
| 1653 | |
Johan Pascal | c3ccd98 | 2020-10-28 17:18:18 +0100 | [diff] [blame] | 1654 | if( ( ret = ssl_parse_use_srtp_ext( ssl, ext + 4, ext_size ) ) != 0 ) |
| 1655 | return( ret ); |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1656 | |
| 1657 | break; |
| 1658 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ |
| 1659 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1660 | default: |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1661 | MBEDTLS_SSL_DEBUG_MSG( 3, |
Paul Elliott | 9f35211 | 2020-12-09 14:55:45 +0000 | [diff] [blame] | 1662 | ( "unknown extension found: %u (ignoring)", ext_id ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1663 | } |
| 1664 | |
| 1665 | ext_len -= 4 + ext_size; |
| 1666 | ext += 4 + ext_size; |
| 1667 | |
| 1668 | if( ext_len > 0 && ext_len < 4 ) |
| 1669 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1670 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
Hanno Becker | c3411d4 | 2021-06-24 11:09:00 +0100 | [diff] [blame] | 1671 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1672 | } |
| 1673 | } |
| 1674 | |
| 1675 | /* |
Andrzej Kurek | 21b5080 | 2022-07-06 03:26:55 -0400 | [diff] [blame] | 1676 | * mbedtls_ssl_derive_keys() has to be called after the parsing of the |
| 1677 | * extensions. It sets the transform data for the resumed session which in |
| 1678 | * case of DTLS includes the server CID extracted from the CID extension. |
| 1679 | */ |
| 1680 | if( ssl->handshake->resume ) |
Andrzej Kurek | 7cf8725 | 2022-06-14 07:12:33 -0400 | [diff] [blame] | 1681 | { |
| 1682 | if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 ) |
| 1683 | { |
| 1684 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret ); |
| 1685 | mbedtls_ssl_send_alert_message( |
| 1686 | ssl, |
| 1687 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1688 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 1689 | return( ret ); |
| 1690 | } |
| 1691 | } |
| 1692 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1693 | /* |
| 1694 | * Renegotiation security checks |
| 1695 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1696 | if( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1697 | ssl->conf->allow_legacy_renegotiation == |
| 1698 | MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1699 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1700 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 1701 | ( "legacy renegotiation, breaking off handshake" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1702 | handshake_failure = 1; |
| 1703 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1704 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1705 | else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && |
| 1706 | ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION && |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1707 | renegotiation_info_seen == 0 ) |
| 1708 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1709 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 1710 | ( "renegotiation_info extension missing (secure)" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1711 | handshake_failure = 1; |
| 1712 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1713 | else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && |
| 1714 | ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1715 | ssl->conf->allow_legacy_renegotiation == |
| 1716 | MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1717 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1718 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) ); |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1719 | handshake_failure = 1; |
| 1720 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1721 | else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && |
| 1722 | ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1723 | renegotiation_info_seen == 1 ) |
| 1724 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1725 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 1726 | ( "renegotiation_info extension present (legacy)" ) ); |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1727 | handshake_failure = 1; |
| 1728 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1729 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1730 | |
| 1731 | if( handshake_failure == 1 ) |
| 1732 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1733 | mbedtls_ssl_send_alert_message( |
| 1734 | ssl, |
| 1735 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1736 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Hanno Becker | c3411d4 | 2021-06-24 11:09:00 +0100 | [diff] [blame] | 1737 | return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1738 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1739 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1740 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1741 | |
| 1742 | return( 0 ); |
| 1743 | } |
| 1744 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1745 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \ |
| 1746 | defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1747 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1748 | static int ssl_parse_server_dh_params( mbedtls_ssl_context *ssl, |
| 1749 | unsigned char **p, |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 1750 | unsigned char *end ) |
| 1751 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1752 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Gilles Peskine | e8a2fc8 | 2020-12-08 22:46:11 +0100 | [diff] [blame] | 1753 | size_t dhm_actual_bitlen; |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 1754 | |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 1755 | /* |
| 1756 | * Ephemeral DH parameters: |
| 1757 | * |
| 1758 | * struct { |
| 1759 | * opaque dh_p<1..2^16-1>; |
| 1760 | * opaque dh_g<1..2^16-1>; |
| 1761 | * opaque dh_Ys<1..2^16-1>; |
| 1762 | * } ServerDHParams; |
| 1763 | */ |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1764 | if( ( ret = mbedtls_dhm_read_params( &ssl->handshake->dhm_ctx, |
| 1765 | p, end ) ) != 0 ) |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 1766 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1767 | MBEDTLS_SSL_DEBUG_RET( 2, ( "mbedtls_dhm_read_params" ), ret ); |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 1768 | return( ret ); |
| 1769 | } |
| 1770 | |
Gilles Peskine | 487bbf6 | 2021-05-27 22:17:07 +0200 | [diff] [blame] | 1771 | dhm_actual_bitlen = mbedtls_dhm_get_bitlen( &ssl->handshake->dhm_ctx ); |
Gilles Peskine | e8a2fc8 | 2020-12-08 22:46:11 +0100 | [diff] [blame] | 1772 | if( dhm_actual_bitlen < ssl->conf->dhm_min_bitlen ) |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 1773 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 1774 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DHM prime too short: %" MBEDTLS_PRINTF_SIZET " < %u", |
Gilles Peskine | e8a2fc8 | 2020-12-08 22:46:11 +0100 | [diff] [blame] | 1775 | dhm_actual_bitlen, |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 1776 | ssl->conf->dhm_min_bitlen ) ); |
Hanno Becker | cbc8f6f | 2021-06-24 10:32:31 +0100 | [diff] [blame] | 1777 | return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE ); |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 1778 | } |
| 1779 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1780 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P ); |
| 1781 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G ); |
| 1782 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY ); |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 1783 | |
| 1784 | return( ret ); |
| 1785 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1786 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED || |
| 1787 | MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 1788 | |
Neil Armstrong | d8419ff | 2022-04-12 14:39:12 +0200 | [diff] [blame] | 1789 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 468c506 | 2022-10-24 10:30:14 -0400 | [diff] [blame] | 1790 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ |
| 1791 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \ |
| 1792 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1793 | MBEDTLS_CHECK_RETURN_CRITICAL |
Neil Armstrong | d8419ff | 2022-04-12 14:39:12 +0200 | [diff] [blame] | 1794 | static int ssl_parse_server_ecdh_params( mbedtls_ssl_context *ssl, |
Hanno Becker | bb89e27 | 2019-01-08 12:54:37 +0000 | [diff] [blame] | 1795 | unsigned char **p, |
| 1796 | unsigned char *end ) |
| 1797 | { |
| 1798 | uint16_t tls_id; |
Gilles Peskine | 4245980 | 2019-12-19 13:31:53 +0100 | [diff] [blame] | 1799 | size_t ecdh_bits = 0; |
Hanno Becker | bb89e27 | 2019-01-08 12:54:37 +0000 | [diff] [blame] | 1800 | uint8_t ecpoint_len; |
| 1801 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
| 1802 | |
| 1803 | /* |
Manuel Pégourié-Gonnard | e511989 | 2021-12-09 11:45:03 +0100 | [diff] [blame] | 1804 | * struct { |
| 1805 | * ECParameters curve_params; |
| 1806 | * ECPoint public; |
| 1807 | * } ServerECDHParams; |
| 1808 | * |
Manuel Pégourié-Gonnard | 422370d | 2022-02-07 11:55:21 +0100 | [diff] [blame] | 1809 | * 1 curve_type (must be "named_curve") |
Manuel Pégourié-Gonnard | e511989 | 2021-12-09 11:45:03 +0100 | [diff] [blame] | 1810 | * 2..3 NamedCurve |
| 1811 | * 4 ECPoint.len |
| 1812 | * 5+ ECPoint contents |
Hanno Becker | bb89e27 | 2019-01-08 12:54:37 +0000 | [diff] [blame] | 1813 | */ |
Hanno Becker | bb89e27 | 2019-01-08 12:54:37 +0000 | [diff] [blame] | 1814 | if( end - *p < 4 ) |
Hanno Becker | cbc8f6f | 2021-06-24 10:32:31 +0100 | [diff] [blame] | 1815 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Hanno Becker | bb89e27 | 2019-01-08 12:54:37 +0000 | [diff] [blame] | 1816 | |
| 1817 | /* First byte is curve_type; only named_curve is handled */ |
| 1818 | if( *(*p)++ != MBEDTLS_ECP_TLS_NAMED_CURVE ) |
Hanno Becker | 2fc9a65 | 2021-06-24 15:40:11 +0100 | [diff] [blame] | 1819 | return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE ); |
Hanno Becker | bb89e27 | 2019-01-08 12:54:37 +0000 | [diff] [blame] | 1820 | |
| 1821 | /* Next two bytes are the namedcurve value */ |
| 1822 | tls_id = *(*p)++; |
| 1823 | tls_id <<= 8; |
| 1824 | tls_id |= *(*p)++; |
| 1825 | |
Manuel Pégourié-Gonnard | 141be6c | 2022-01-25 11:46:19 +0100 | [diff] [blame] | 1826 | /* Check it's a curve we offered */ |
| 1827 | if( mbedtls_ssl_check_curve_tls_id( ssl, tls_id ) != 0 ) |
Manuel Pégourié-Gonnard | ff229cf | 2022-02-07 12:00:32 +0100 | [diff] [blame] | 1828 | { |
| 1829 | MBEDTLS_SSL_DEBUG_MSG( 2, |
| 1830 | ( "bad server key exchange message (ECDHE curve): %u", |
| 1831 | (unsigned) tls_id ) ); |
Manuel Pégourié-Gonnard | 141be6c | 2022-01-25 11:46:19 +0100 | [diff] [blame] | 1832 | return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | ff229cf | 2022-02-07 12:00:32 +0100 | [diff] [blame] | 1833 | } |
Manuel Pégourié-Gonnard | 141be6c | 2022-01-25 11:46:19 +0100 | [diff] [blame] | 1834 | |
Hanno Becker | bb89e27 | 2019-01-08 12:54:37 +0000 | [diff] [blame] | 1835 | /* Convert EC group to PSA key type. */ |
Gilles Peskine | 4245980 | 2019-12-19 13:31:53 +0100 | [diff] [blame] | 1836 | if( ( handshake->ecdh_psa_type = |
| 1837 | mbedtls_psa_parse_tls_ecc_group( tls_id, &ecdh_bits ) ) == 0 ) |
Hanno Becker | bb89e27 | 2019-01-08 12:54:37 +0000 | [diff] [blame] | 1838 | { |
Hanno Becker | 2fc9a65 | 2021-06-24 15:40:11 +0100 | [diff] [blame] | 1839 | return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE ); |
Hanno Becker | bb89e27 | 2019-01-08 12:54:37 +0000 | [diff] [blame] | 1840 | } |
Neil Armstrong | 91477a7 | 2022-03-25 15:42:20 +0100 | [diff] [blame] | 1841 | handshake->ecdh_bits = ecdh_bits; |
Hanno Becker | bb89e27 | 2019-01-08 12:54:37 +0000 | [diff] [blame] | 1842 | |
Manuel Pégourié-Gonnard | 4a0ac1f | 2022-01-18 12:30:40 +0100 | [diff] [blame] | 1843 | /* Keep a copy of the peer's public key */ |
Hanno Becker | bb89e27 | 2019-01-08 12:54:37 +0000 | [diff] [blame] | 1844 | ecpoint_len = *(*p)++; |
| 1845 | if( (size_t)( end - *p ) < ecpoint_len ) |
Hanno Becker | cbc8f6f | 2021-06-24 10:32:31 +0100 | [diff] [blame] | 1846 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Hanno Becker | bb89e27 | 2019-01-08 12:54:37 +0000 | [diff] [blame] | 1847 | |
Manuel Pégourié-Gonnard | 4a0ac1f | 2022-01-18 12:30:40 +0100 | [diff] [blame] | 1848 | if( ecpoint_len > sizeof( handshake->ecdh_psa_peerkey ) ) |
| 1849 | return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE ); |
Hanno Becker | bb89e27 | 2019-01-08 12:54:37 +0000 | [diff] [blame] | 1850 | |
Manuel Pégourié-Gonnard | 4a0ac1f | 2022-01-18 12:30:40 +0100 | [diff] [blame] | 1851 | memcpy( handshake->ecdh_psa_peerkey, *p, ecpoint_len ); |
| 1852 | handshake->ecdh_psa_peerkey_len = ecpoint_len; |
Hanno Becker | bb89e27 | 2019-01-08 12:54:37 +0000 | [diff] [blame] | 1853 | *p += ecpoint_len; |
Manuel Pégourié-Gonnard | 4a0ac1f | 2022-01-18 12:30:40 +0100 | [diff] [blame] | 1854 | |
Hanno Becker | bb89e27 | 2019-01-08 12:54:37 +0000 | [diff] [blame] | 1855 | return( 0 ); |
| 1856 | } |
Andrzej Kurek | 468c506 | 2022-10-24 10:30:14 -0400 | [diff] [blame] | 1857 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || |
| 1858 | MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED || |
| 1859 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */ |
Neil Armstrong | d8419ff | 2022-04-12 14:39:12 +0200 | [diff] [blame] | 1860 | #else |
Andrzej Kurek | 468c506 | 2022-10-24 10:30:14 -0400 | [diff] [blame] | 1861 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ |
| 1862 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ |
| 1863 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \ |
| 1864 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \ |
| 1865 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1866 | MBEDTLS_CHECK_RETURN_CRITICAL |
Neil Armstrong | 1f198d8 | 2022-04-13 15:02:30 +0200 | [diff] [blame] | 1867 | static int ssl_check_server_ecdh_params( const mbedtls_ssl_context *ssl ) |
| 1868 | { |
| 1869 | const mbedtls_ecp_curve_info *curve_info; |
| 1870 | mbedtls_ecp_group_id grp_id; |
| 1871 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
| 1872 | grp_id = ssl->handshake->ecdh_ctx.grp.id; |
| 1873 | #else |
| 1874 | grp_id = ssl->handshake->ecdh_ctx.grp_id; |
| 1875 | #endif |
Hanno Becker | bb89e27 | 2019-01-08 12:54:37 +0000 | [diff] [blame] | 1876 | |
Neil Armstrong | 1f198d8 | 2022-04-13 15:02:30 +0200 | [diff] [blame] | 1877 | curve_info = mbedtls_ecp_curve_info_from_grp_id( grp_id ); |
| 1878 | if( curve_info == NULL ) |
| 1879 | { |
| 1880 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1881 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 1882 | } |
| 1883 | |
| 1884 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) ); |
| 1885 | |
| 1886 | if( mbedtls_ssl_check_curve( ssl, grp_id ) != 0 ) |
| 1887 | return( -1 ); |
| 1888 | |
| 1889 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 1890 | MBEDTLS_DEBUG_ECDH_QP ); |
| 1891 | |
| 1892 | return( 0 ); |
| 1893 | } |
| 1894 | |
Andrzej Kurek | 468c506 | 2022-10-24 10:30:14 -0400 | [diff] [blame] | 1895 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || |
| 1896 | MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED || |
| 1897 | MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED || |
| 1898 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED || |
| 1899 | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */ |
| 1900 | |
| 1901 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ |
| 1902 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \ |
| 1903 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1904 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1905 | static int ssl_parse_server_ecdh_params( mbedtls_ssl_context *ssl, |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 1906 | unsigned char **p, |
| 1907 | unsigned char *end ) |
| 1908 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1909 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 1910 | |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 1911 | /* |
| 1912 | * Ephemeral ECDH parameters: |
| 1913 | * |
| 1914 | * struct { |
| 1915 | * ECParameters curve_params; |
| 1916 | * ECPoint public; |
| 1917 | * } ServerECDHParams; |
| 1918 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1919 | if( ( ret = mbedtls_ecdh_read_params( &ssl->handshake->ecdh_ctx, |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 1920 | (const unsigned char **) p, end ) ) != 0 ) |
| 1921 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1922 | MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_read_params" ), ret ); |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1923 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 1c1c20e | 2018-09-12 10:34:43 +0200 | [diff] [blame] | 1924 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 1925 | ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS; |
Manuel Pégourié-Gonnard | 558da9c | 2018-06-13 12:02:12 +0200 | [diff] [blame] | 1926 | #endif |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 1927 | return( ret ); |
| 1928 | } |
| 1929 | |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 1930 | if( ssl_check_server_ecdh_params( ssl ) != 0 ) |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 1931 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1932 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 1933 | ( "bad server key exchange message (ECDHE curve)" ) ); |
Hanno Becker | 2fc9a65 | 2021-06-24 15:40:11 +0100 | [diff] [blame] | 1934 | return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE ); |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 1935 | } |
| 1936 | |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 1937 | return( ret ); |
| 1938 | } |
Andrzej Kurek | 468c506 | 2022-10-24 10:30:14 -0400 | [diff] [blame] | 1939 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || \ |
| 1940 | MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED || \ |
| 1941 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */ |
| 1942 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1943 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1944 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1945 | static int ssl_parse_server_psk_hint( mbedtls_ssl_context *ssl, |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 1946 | unsigned char **p, |
| 1947 | unsigned char *end ) |
| 1948 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1949 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
irwir | 6527bd6 | 2019-09-21 18:51:25 +0300 | [diff] [blame] | 1950 | uint16_t len; |
Paul Bakker | c5a79cc | 2013-06-26 15:08:35 +0200 | [diff] [blame] | 1951 | ((void) ssl); |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 1952 | |
| 1953 | /* |
| 1954 | * PSK parameters: |
| 1955 | * |
| 1956 | * opaque psk_identity_hint<0..2^16-1>; |
| 1957 | */ |
Hanno Becker | 0c161d1 | 2018-10-08 13:40:50 +0100 | [diff] [blame] | 1958 | if( end - (*p) < 2 ) |
Krzysztof Stachowiak | 740b218 | 2018-03-13 11:31:14 +0100 | [diff] [blame] | 1959 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1960 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 1961 | ( "bad server key exchange message (psk_identity_hint length)" ) ); |
Hanno Becker | cbc8f6f | 2021-06-24 10:32:31 +0100 | [diff] [blame] | 1962 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Krzysztof Stachowiak | 740b218 | 2018-03-13 11:31:14 +0100 | [diff] [blame] | 1963 | } |
Manuel Pégourié-Gonnard | 59b9fe2 | 2013-10-15 11:55:33 +0200 | [diff] [blame] | 1964 | len = (*p)[0] << 8 | (*p)[1]; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1965 | *p += 2; |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 1966 | |
irwir | 6527bd6 | 2019-09-21 18:51:25 +0300 | [diff] [blame] | 1967 | if( end - (*p) < len ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 1968 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1969 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 1970 | ( "bad server key exchange message (psk_identity_hint length)" ) ); |
Hanno Becker | cbc8f6f | 2021-06-24 10:32:31 +0100 | [diff] [blame] | 1971 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 1972 | } |
| 1973 | |
Manuel Pégourié-Gonnard | 9d62412 | 2016-02-22 11:10:14 +0100 | [diff] [blame] | 1974 | /* |
| 1975 | * Note: we currently ignore the PKS identity hint, as we only allow one |
| 1976 | * PSK to be provisionned on the client. This could be changed later if |
| 1977 | * someone needs that feature. |
| 1978 | */ |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 1979 | *p += len; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1980 | ret = 0; |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 1981 | |
| 1982 | return( ret ); |
| 1983 | } |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1984 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 1985 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1986 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \ |
| 1987 | defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 1988 | /* |
| 1989 | * Generate a pre-master secret and encrypt it with the server's RSA key |
| 1990 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1991 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1992 | static int ssl_write_encrypted_pms( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 1993 | size_t offset, size_t *olen, |
| 1994 | size_t pms_offset ) |
| 1995 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1996 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1997 | size_t len_bytes = 2; |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 1998 | unsigned char *p = ssl->handshake->premaster + pms_offset; |
Hanno Becker | c7d7e29 | 2019-02-06 16:49:54 +0000 | [diff] [blame] | 1999 | mbedtls_pk_context * peer_pk; |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2000 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2001 | if( offset + len_bytes > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 2002 | { |
| 2003 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small for encrypted pms" ) ); |
| 2004 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2005 | } |
| 2006 | |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2007 | /* |
| 2008 | * Generate (part of) the pre-master as |
| 2009 | * struct { |
| 2010 | * ProtocolVersion client_version; |
| 2011 | * opaque random[46]; |
| 2012 | * } PreMasterSecret; |
| 2013 | */ |
Glenn Strauss | e3af4cb | 2022-03-15 03:23:42 -0400 | [diff] [blame] | 2014 | mbedtls_ssl_write_version( p, ssl->conf->transport, |
| 2015 | MBEDTLS_SSL_VERSION_TLS1_2 ); |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2016 | |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 2017 | if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p + 2, 46 ) ) != 0 ) |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2018 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2019 | MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret ); |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2020 | return( ret ); |
| 2021 | } |
| 2022 | |
| 2023 | ssl->handshake->pmslen = 48; |
| 2024 | |
Hanno Becker | c7d7e29 | 2019-02-06 16:49:54 +0000 | [diff] [blame] | 2025 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 2026 | peer_pk = &ssl->handshake->peer_pubkey; |
| 2027 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | 7f2f062 | 2015-09-03 10:44:32 +0200 | [diff] [blame] | 2028 | if( ssl->session_negotiate->peer_cert == NULL ) |
| 2029 | { |
Hanno Becker | 8273df8 | 2019-02-06 17:37:32 +0000 | [diff] [blame] | 2030 | /* Should never happen */ |
Hanno Becker | 62d58ed | 2019-02-26 11:51:06 +0000 | [diff] [blame] | 2031 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Hanno Becker | 8273df8 | 2019-02-06 17:37:32 +0000 | [diff] [blame] | 2032 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7f2f062 | 2015-09-03 10:44:32 +0200 | [diff] [blame] | 2033 | } |
Hanno Becker | c7d7e29 | 2019-02-06 16:49:54 +0000 | [diff] [blame] | 2034 | peer_pk = &ssl->session_negotiate->peer_cert->pk; |
| 2035 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | 7f2f062 | 2015-09-03 10:44:32 +0200 | [diff] [blame] | 2036 | |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2037 | /* |
| 2038 | * Now write it out, encrypted |
| 2039 | */ |
Hanno Becker | c7d7e29 | 2019-02-06 16:49:54 +0000 | [diff] [blame] | 2040 | if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_RSA ) ) |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2041 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2042 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) ); |
| 2043 | return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH ); |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2044 | } |
| 2045 | |
Hanno Becker | c7d7e29 | 2019-02-06 16:49:54 +0000 | [diff] [blame] | 2046 | if( ( ret = mbedtls_pk_encrypt( peer_pk, |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2047 | p, ssl->handshake->pmslen, |
| 2048 | ssl->out_msg + offset + len_bytes, olen, |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2049 | MBEDTLS_SSL_OUT_CONTENT_LEN - offset - len_bytes, |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 2050 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2051 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2052 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_rsa_pkcs1_encrypt", ret ); |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2053 | return( ret ); |
| 2054 | } |
| 2055 | |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2056 | if( len_bytes == 2 ) |
| 2057 | { |
Joe Subbiani | 6dd7364 | 2021-07-19 11:56:54 +0100 | [diff] [blame] | 2058 | MBEDTLS_PUT_UINT16_BE( *olen, ssl->out_msg, offset ); |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2059 | *olen += 2; |
| 2060 | } |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2061 | |
Hanno Becker | ae553dd | 2019-02-08 14:06:00 +0000 | [diff] [blame] | 2062 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 2063 | /* We don't need the peer's public key anymore. Free it. */ |
| 2064 | mbedtls_pk_free( peer_pk ); |
| 2065 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2066 | return( 0 ); |
| 2067 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2068 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED || |
| 2069 | MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2070 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2071 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ |
| 2072 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 2073 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2074 | static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2075 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2076 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2077 | const mbedtls_ecp_keypair *peer_key; |
Hanno Becker | be7f508 | 2019-02-06 17:44:07 +0000 | [diff] [blame] | 2078 | mbedtls_pk_context * peer_pk; |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2079 | |
Hanno Becker | be7f508 | 2019-02-06 17:44:07 +0000 | [diff] [blame] | 2080 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 2081 | peer_pk = &ssl->handshake->peer_pubkey; |
| 2082 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | 7f2f062 | 2015-09-03 10:44:32 +0200 | [diff] [blame] | 2083 | if( ssl->session_negotiate->peer_cert == NULL ) |
| 2084 | { |
Hanno Becker | 8273df8 | 2019-02-06 17:37:32 +0000 | [diff] [blame] | 2085 | /* Should never happen */ |
Hanno Becker | bd5580a | 2019-02-26 12:36:01 +0000 | [diff] [blame] | 2086 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Hanno Becker | 8273df8 | 2019-02-06 17:37:32 +0000 | [diff] [blame] | 2087 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7f2f062 | 2015-09-03 10:44:32 +0200 | [diff] [blame] | 2088 | } |
Hanno Becker | be7f508 | 2019-02-06 17:44:07 +0000 | [diff] [blame] | 2089 | peer_pk = &ssl->session_negotiate->peer_cert->pk; |
| 2090 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | 7f2f062 | 2015-09-03 10:44:32 +0200 | [diff] [blame] | 2091 | |
Manuel Pégourié-Gonnard | 66b0d61 | 2022-06-17 10:49:29 +0200 | [diff] [blame] | 2092 | /* This is a public key, so it can't be opaque, so can_do() is a good |
| 2093 | * enough check to ensure pk_ec() is safe to use below. */ |
Hanno Becker | be7f508 | 2019-02-06 17:44:07 +0000 | [diff] [blame] | 2094 | if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_ECKEY ) ) |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2095 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2096 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) ); |
| 2097 | return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH ); |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2098 | } |
| 2099 | |
Hanno Becker | be7f508 | 2019-02-06 17:44:07 +0000 | [diff] [blame] | 2100 | peer_key = mbedtls_pk_ec( *peer_pk ); |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2101 | |
Przemek Stekiel | ea4000f | 2022-03-16 09:49:33 +0100 | [diff] [blame] | 2102 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2103 | size_t ecdh_bits = 0; |
Przemek Stekiel | 561a423 | 2022-03-16 13:16:24 +0100 | [diff] [blame] | 2104 | size_t olen = 0; |
| 2105 | |
| 2106 | if( mbedtls_ssl_check_curve( ssl, peer_key->grp.id ) != 0 ) |
| 2107 | { |
| 2108 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) ); |
| 2109 | return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE ); |
| 2110 | } |
Przemek Stekiel | ea4000f | 2022-03-16 09:49:33 +0100 | [diff] [blame] | 2111 | |
| 2112 | ssl->handshake->ecdh_psa_type = |
| 2113 | PSA_KEY_TYPE_ECC_KEY_PAIR( mbedtls_ecc_group_to_psa( peer_key->grp.id, |
| 2114 | &ecdh_bits ) ); |
| 2115 | |
| 2116 | if( ssl->handshake->ecdh_psa_type == 0 || ecdh_bits > 0xffff ) |
| 2117 | { |
| 2118 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid ecc group conversion to psa." ) ); |
| 2119 | return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER ); |
| 2120 | } |
| 2121 | |
| 2122 | ssl->handshake->ecdh_bits = (uint16_t) ecdh_bits; |
| 2123 | |
Przemek Stekiel | ea4000f | 2022-03-16 09:49:33 +0100 | [diff] [blame] | 2124 | /* Store peer's public key in psa format. */ |
Przemek Stekiel | 561a423 | 2022-03-16 13:16:24 +0100 | [diff] [blame] | 2125 | ret = mbedtls_ecp_point_write_binary( &peer_key->grp, &peer_key->Q, |
| 2126 | MBEDTLS_ECP_PF_UNCOMPRESSED, &olen, |
| 2127 | ssl->handshake->ecdh_psa_peerkey, |
| 2128 | MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ); |
Przemek Stekiel | ea4000f | 2022-03-16 09:49:33 +0100 | [diff] [blame] | 2129 | |
Przemek Stekiel | 561a423 | 2022-03-16 13:16:24 +0100 | [diff] [blame] | 2130 | if ( ret != 0 ) |
| 2131 | { |
| 2132 | MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecp_point_write_binary" ), ret ); |
| 2133 | return( ret ); |
| 2134 | } |
Przemek Stekiel | ea4000f | 2022-03-16 09:49:33 +0100 | [diff] [blame] | 2135 | |
Przemek Stekiel | 561a423 | 2022-03-16 13:16:24 +0100 | [diff] [blame] | 2136 | ssl->handshake->ecdh_psa_peerkey_len = olen; |
Przemek Stekiel | ea4000f | 2022-03-16 09:49:33 +0100 | [diff] [blame] | 2137 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2138 | if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key, |
| 2139 | MBEDTLS_ECDH_THEIRS ) ) != 0 ) |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2140 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2141 | MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret ); |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2142 | return( ret ); |
| 2143 | } |
| 2144 | |
| 2145 | if( ssl_check_server_ecdh_params( ssl ) != 0 ) |
| 2146 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2147 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) ); |
Hanno Becker | 9ed1ba5 | 2021-06-24 11:03:13 +0100 | [diff] [blame] | 2148 | return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE ); |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2149 | } |
Przemek Stekiel | ea4000f | 2022-03-16 09:49:33 +0100 | [diff] [blame] | 2150 | #endif |
Hanno Becker | ae553dd | 2019-02-08 14:06:00 +0000 | [diff] [blame] | 2151 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 2152 | /* We don't need the peer's public key anymore. Free it, |
| 2153 | * so that more RAM is available for upcoming expensive |
| 2154 | * operations like ECDHE. */ |
| 2155 | mbedtls_pk_free( peer_pk ); |
| 2156 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 2157 | |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2158 | return( ret ); |
| 2159 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2160 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || |
| 2161 | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */ |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2162 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 2163 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2164 | static int ssl_parse_server_key_exchange( mbedtls_ssl_context *ssl ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2165 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2166 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 2167 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 2168 | ssl->handshake->ciphersuite_info; |
Andres Amaya Garcia | 53c77cc | 2017-06-27 16:15:06 +0100 | [diff] [blame] | 2169 | unsigned char *p = NULL, *end = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2170 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2171 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2172 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2173 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) |
| 2174 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2175 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2176 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2177 | ssl->state++; |
| 2178 | return( 0 ); |
| 2179 | } |
Manuel Pégourié-Gonnard | bac0e3b | 2013-10-15 11:54:47 +0200 | [diff] [blame] | 2180 | ((void) p); |
| 2181 | ((void) end); |
| 2182 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2183 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2184 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ |
| 2185 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) |
| 2186 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA || |
| 2187 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA ) |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2188 | { |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 2189 | if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 ) |
| 2190 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2191 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret ); |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2192 | mbedtls_ssl_send_alert_message( |
| 2193 | ssl, |
| 2194 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2195 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 2196 | return( ret ); |
| 2197 | } |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2198 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2199 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) ); |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2200 | ssl->state++; |
| 2201 | return( 0 ); |
| 2202 | } |
| 2203 | ((void) p); |
| 2204 | ((void) end); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2205 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED || |
| 2206 | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */ |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2207 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2208 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 2209 | if( ssl->handshake->ecrs_enabled && |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 2210 | ssl->handshake->ecrs_state == ssl_ecrs_ske_start_processing ) |
Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 2211 | { |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 2212 | goto start_processing; |
Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 2213 | } |
Manuel Pégourié-Gonnard | 1f1f2a1 | 2017-05-18 11:27:06 +0200 | [diff] [blame] | 2214 | #endif |
| 2215 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 2216 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2217 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2218 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2219 | return( ret ); |
| 2220 | } |
| 2221 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2222 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2223 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2224 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2225 | mbedtls_ssl_send_alert_message( |
| 2226 | ssl, |
| 2227 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2228 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2229 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2230 | } |
| 2231 | |
Manuel Pégourié-Gonnard | 09258b9 | 2013-10-15 10:43:36 +0200 | [diff] [blame] | 2232 | /* |
| 2233 | * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server |
| 2234 | * doesn't use a psk_identity_hint |
| 2235 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2236 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2237 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2238 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || |
| 2239 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
Paul Bakker | 188c8de | 2013-04-19 09:13:37 +0200 | [diff] [blame] | 2240 | { |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 2241 | /* Current message is probably either |
| 2242 | * CertificateRequest or ServerHelloDone */ |
| 2243 | ssl->keep_current_message = 1; |
Paul Bakker | 188c8de | 2013-04-19 09:13:37 +0200 | [diff] [blame] | 2244 | goto exit; |
| 2245 | } |
| 2246 | |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2247 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 2248 | ( "server key exchange message must not be skipped" ) ); |
| 2249 | mbedtls_ssl_send_alert_message( |
| 2250 | ssl, |
| 2251 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2252 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 2253 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2254 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2255 | } |
| 2256 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2257 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 2258 | if( ssl->handshake->ecrs_enabled ) |
| 2259 | ssl->handshake->ecrs_state = ssl_ecrs_ske_start_processing; |
| 2260 | |
| 2261 | start_processing: |
| 2262 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2263 | p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ); |
Paul Bakker | 3b6a07b | 2013-03-21 11:56:50 +0100 | [diff] [blame] | 2264 | end = ssl->in_msg + ssl->in_hslen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2265 | MBEDTLS_SSL_DEBUG_BUF( 3, "server key exchange", p, end - p ); |
Paul Bakker | 3b6a07b | 2013-03-21 11:56:50 +0100 | [diff] [blame] | 2266 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2267 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2268 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || |
| 2269 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK || |
| 2270 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK || |
| 2271 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
Manuel Pégourié-Gonnard | 09258b9 | 2013-10-15 10:43:36 +0200 | [diff] [blame] | 2272 | { |
| 2273 | if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 ) |
| 2274 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2275 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2276 | mbedtls_ssl_send_alert_message( |
| 2277 | ssl, |
| 2278 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
Dave Rodgman | 8f12739 | 2021-06-28 12:02:21 +0100 | [diff] [blame] | 2279 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Hanno Becker | cbc8f6f | 2021-06-24 10:32:31 +0100 | [diff] [blame] | 2280 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 09258b9 | 2013-10-15 10:43:36 +0200 | [diff] [blame] | 2281 | } |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 2282 | } /* FALLTHROUGH */ |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2283 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | 09258b9 | 2013-10-15 10:43:36 +0200 | [diff] [blame] | 2284 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2285 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \ |
| 2286 | defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
| 2287 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || |
| 2288 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
Manuel Pégourié-Gonnard | 09258b9 | 2013-10-15 10:43:36 +0200 | [diff] [blame] | 2289 | ; /* nothing more to do */ |
| 2290 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2291 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED || |
| 2292 | MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
| 2293 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \ |
| 2294 | defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 2295 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA || |
| 2296 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2297 | { |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2298 | if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2299 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2300 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2301 | mbedtls_ssl_send_alert_message( |
| 2302 | ssl, |
| 2303 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2304 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Dave Rodgman | bed8927 | 2021-06-29 12:06:32 +0100 | [diff] [blame] | 2305 | return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER ); |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2306 | } |
| 2307 | } |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2308 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2309 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED || |
| 2310 | MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
Neil Armstrong | d8419ff | 2022-04-12 14:39:12 +0200 | [diff] [blame] | 2311 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ |
| 2312 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2313 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) |
| 2314 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA || |
| 2315 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK || |
| 2316 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2317 | { |
| 2318 | if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 ) |
| 2319 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2320 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2321 | mbedtls_ssl_send_alert_message( |
| 2322 | ssl, |
| 2323 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2324 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Dave Rodgman | 39bd5a6 | 2021-06-29 15:25:21 +0100 | [diff] [blame] | 2325 | return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2326 | } |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2327 | } |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2328 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2329 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || |
| 2330 | MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED || |
| 2331 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */ |
Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 2332 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 2333 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ) |
| 2334 | { |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 2335 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame^] | 2336 | if( ( ret = mbedtls_psa_ecjpake_read_round_two( |
Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 2337 | &ssl->handshake->psa_pake_ctx, p, end - p, |
| 2338 | ssl->conf->endpoint ) ) != 0 ) |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 2339 | { |
| 2340 | psa_destroy_key( ssl->handshake->psa_pake_password ); |
| 2341 | psa_pake_abort( &ssl->handshake->psa_pake_ctx ); |
| 2342 | |
| 2343 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_pake_input round two", ret ); |
| 2344 | mbedtls_ssl_send_alert_message( |
| 2345 | ssl, |
| 2346 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2347 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
| 2348 | return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE ); |
| 2349 | } |
| 2350 | #else |
Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 2351 | ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx, |
| 2352 | p, end - p ); |
| 2353 | if( ret != 0 ) |
| 2354 | { |
| 2355 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret ); |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2356 | mbedtls_ssl_send_alert_message( |
| 2357 | ssl, |
| 2358 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
Hanno Becker | 77b4a65 | 2021-06-24 16:27:09 +0100 | [diff] [blame] | 2359 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
| 2360 | return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 2361 | } |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 2362 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 2363 | } |
| 2364 | else |
| 2365 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2366 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2367 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2368 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2369 | } |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2370 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2371 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 2372 | if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2373 | { |
Manuel Pégourié-Gonnard | d92d6a1 | 2014-09-10 15:25:02 +0000 | [diff] [blame] | 2374 | size_t sig_len, hashlen; |
Przemek Stekiel | 40afdd2 | 2022-09-06 13:08:28 +0200 | [diff] [blame] | 2375 | unsigned char hash[MBEDTLS_HASH_MAX_SIZE]; |
| 2376 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2377 | mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE; |
| 2378 | mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE; |
| 2379 | unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ); |
Manuel Pégourié-Gonnard | d92d6a1 | 2014-09-10 15:25:02 +0000 | [diff] [blame] | 2380 | size_t params_len = p - params; |
Manuel Pégourié-Gonnard | 1f1f2a1 | 2017-05-18 11:27:06 +0200 | [diff] [blame] | 2381 | void *rs_ctx = NULL; |
Jerry Yu | 693a47a | 2022-06-23 14:02:28 +0800 | [diff] [blame] | 2382 | uint16_t sig_alg; |
Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 2383 | |
Hanno Becker | a6899bb | 2019-02-06 18:26:03 +0000 | [diff] [blame] | 2384 | mbedtls_pk_context * peer_pk; |
| 2385 | |
Jerry Yu | 693a47a | 2022-06-23 14:02:28 +0800 | [diff] [blame] | 2386 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 2387 | peer_pk = &ssl->handshake->peer_pubkey; |
| 2388 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 2389 | if( ssl->session_negotiate->peer_cert == NULL ) |
| 2390 | { |
| 2391 | /* Should never happen */ |
| 2392 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2393 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 2394 | } |
| 2395 | peer_pk = &ssl->session_negotiate->peer_cert->pk; |
| 2396 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 2397 | |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2398 | /* |
| 2399 | * Handle the digitally-signed structure |
| 2400 | */ |
Jerry Yu | 693a47a | 2022-06-23 14:02:28 +0800 | [diff] [blame] | 2401 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 ); |
| 2402 | sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 ); |
Jerry Yu | 95b743c | 2022-07-23 11:37:50 +0800 | [diff] [blame] | 2403 | if( mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg( |
Jerry Yu | c3bf748 | 2022-07-29 10:27:17 +0800 | [diff] [blame] | 2404 | sig_alg, &pk_alg, &md_alg ) != 0 && |
Jerry Yu | 693a47a | 2022-06-23 14:02:28 +0800 | [diff] [blame] | 2405 | ! mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) && |
| 2406 | ! mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2407 | { |
Ronald Cron | 90915f2 | 2022-03-07 11:11:36 +0100 | [diff] [blame] | 2408 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 2409 | ( "bad server key exchange message" ) ); |
| 2410 | mbedtls_ssl_send_alert_message( |
| 2411 | ssl, |
| 2412 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2413 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
| 2414 | return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER ); |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2415 | } |
Jerry Yu | 693a47a | 2022-06-23 14:02:28 +0800 | [diff] [blame] | 2416 | p += 2; |
Ronald Cron | 90915f2 | 2022-03-07 11:11:36 +0100 | [diff] [blame] | 2417 | |
Jerry Yu | 693a47a | 2022-06-23 14:02:28 +0800 | [diff] [blame] | 2418 | if( !mbedtls_pk_can_do( peer_pk, pk_alg ) ) |
Paul Bakker | 9659dae | 2013-08-28 16:21:34 +0200 | [diff] [blame] | 2419 | { |
Ronald Cron | 90915f2 | 2022-03-07 11:11:36 +0100 | [diff] [blame] | 2420 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 2421 | ( "bad server key exchange message" ) ); |
| 2422 | mbedtls_ssl_send_alert_message( |
| 2423 | ssl, |
| 2424 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2425 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
| 2426 | return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER ); |
Paul Bakker | 9659dae | 2013-08-28 16:21:34 +0200 | [diff] [blame] | 2427 | } |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 2428 | |
| 2429 | /* |
| 2430 | * Read signature |
| 2431 | */ |
Krzysztof Stachowiak | a1098f8 | 2018-03-13 11:28:49 +0100 | [diff] [blame] | 2432 | |
| 2433 | if( p > end - 2 ) |
| 2434 | { |
| 2435 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2436 | mbedtls_ssl_send_alert_message( |
| 2437 | ssl, |
| 2438 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2439 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Hanno Becker | cbc8f6f | 2021-06-24 10:32:31 +0100 | [diff] [blame] | 2440 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Krzysztof Stachowiak | a1098f8 | 2018-03-13 11:28:49 +0100 | [diff] [blame] | 2441 | } |
Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 2442 | sig_len = ( p[0] << 8 ) | p[1]; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2443 | p += 2; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2444 | |
Krzysztof Stachowiak | 027f84c | 2018-03-13 11:29:24 +0100 | [diff] [blame] | 2445 | if( p != end - sig_len ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2446 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2447 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2448 | mbedtls_ssl_send_alert_message( |
| 2449 | ssl, |
| 2450 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2451 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Hanno Becker | cbc8f6f | 2021-06-24 10:32:31 +0100 | [diff] [blame] | 2452 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2453 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2454 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2455 | MBEDTLS_SSL_DEBUG_BUF( 3, "signature", p, sig_len ); |
Manuel Pégourié-Gonnard | ff56da3 | 2013-07-11 10:46:21 +0200 | [diff] [blame] | 2456 | |
Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 2457 | /* |
| 2458 | * Compute the hash that has been signed |
| 2459 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2460 | if( md_alg != MBEDTLS_MD_NONE ) |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2461 | { |
Gilles Peskine | ca1d742 | 2018-04-24 11:53:22 +0200 | [diff] [blame] | 2462 | ret = mbedtls_ssl_get_key_exchange_md_tls1_2( ssl, hash, &hashlen, |
| 2463 | params, params_len, |
| 2464 | md_alg ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 2465 | if( ret != 0 ) |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2466 | return( ret ); |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2467 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2468 | else |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2469 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2470 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2471 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 2472 | } |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2473 | |
Gilles Peskine | ca1d742 | 2018-04-24 11:53:22 +0200 | [diff] [blame] | 2474 | MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen ); |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2475 | |
Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 2476 | /* |
| 2477 | * Verify signature |
| 2478 | */ |
Hanno Becker | a6899bb | 2019-02-06 18:26:03 +0000 | [diff] [blame] | 2479 | if( !mbedtls_pk_can_do( peer_pk, pk_alg ) ) |
Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 2480 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2481 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2482 | mbedtls_ssl_send_alert_message( |
| 2483 | ssl, |
| 2484 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2485 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2486 | return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH ); |
Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 2487 | } |
| 2488 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2489 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 2490 | if( ssl->handshake->ecrs_enabled ) |
Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 2491 | rs_ctx = &ssl->handshake->ecrs_ctx.pk; |
Manuel Pégourié-Gonnard | 1f1f2a1 | 2017-05-18 11:27:06 +0200 | [diff] [blame] | 2492 | #endif |
| 2493 | |
Jerry Yu | 693a47a | 2022-06-23 14:02:28 +0800 | [diff] [blame] | 2494 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 2495 | if( pk_alg == MBEDTLS_PK_RSASSA_PSS ) |
| 2496 | { |
Jerry Yu | 693a47a | 2022-06-23 14:02:28 +0800 | [diff] [blame] | 2497 | mbedtls_pk_rsassa_pss_options rsassa_pss_options; |
| 2498 | rsassa_pss_options.mgf1_hash_id = md_alg; |
Andrzej Kurek | 0ce5921 | 2022-08-17 07:54:34 -0400 | [diff] [blame] | 2499 | rsassa_pss_options.expected_salt_len = |
| 2500 | mbedtls_hash_info_get_size( md_alg ); |
| 2501 | if( rsassa_pss_options.expected_salt_len == 0 ) |
Jerry Yu | 693a47a | 2022-06-23 14:02:28 +0800 | [diff] [blame] | 2502 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Andrzej Kurek | 0ce5921 | 2022-08-17 07:54:34 -0400 | [diff] [blame] | 2503 | |
Jerry Yu | 693a47a | 2022-06-23 14:02:28 +0800 | [diff] [blame] | 2504 | ret = mbedtls_pk_verify_ext( pk_alg, &rsassa_pss_options, |
| 2505 | peer_pk, |
| 2506 | md_alg, hash, hashlen, |
| 2507 | p, sig_len ); |
| 2508 | } |
| 2509 | else |
| 2510 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
| 2511 | ret = mbedtls_pk_verify_restartable( peer_pk, |
| 2512 | md_alg, hash, hashlen, p, sig_len, rs_ctx ); |
| 2513 | |
| 2514 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 2515 | { |
David Horstmann | b21bbef | 2022-10-06 17:49:31 +0100 | [diff] [blame] | 2516 | int send_alert_msg = 1; |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2517 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
David Horstmann | b21bbef | 2022-10-06 17:49:31 +0100 | [diff] [blame] | 2518 | send_alert_msg = ( ret != MBEDTLS_ERR_ECP_IN_PROGRESS ); |
Manuel Pégourié-Gonnard | 1f1f2a1 | 2017-05-18 11:27:06 +0200 | [diff] [blame] | 2519 | #endif |
David Horstmann | b21bbef | 2022-10-06 17:49:31 +0100 | [diff] [blame] | 2520 | if( send_alert_msg ) |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2521 | mbedtls_ssl_send_alert_message( |
| 2522 | ssl, |
| 2523 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2524 | MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2525 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret ); |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2526 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 558da9c | 2018-06-13 12:02:12 +0200 | [diff] [blame] | 2527 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 2528 | ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS; |
| 2529 | #endif |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 2530 | return( ret ); |
Paul Bakker | c3f177a | 2012-04-11 16:11:49 +0000 | [diff] [blame] | 2531 | } |
Hanno Becker | ae553dd | 2019-02-08 14:06:00 +0000 | [diff] [blame] | 2532 | |
| 2533 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 2534 | /* We don't need the peer's public key anymore. Free it, |
| 2535 | * so that more RAM is available for upcoming expensive |
| 2536 | * operations like ECDHE. */ |
| 2537 | mbedtls_pk_free( peer_pk ); |
| 2538 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2539 | } |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2540 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2541 | |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2542 | exit: |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2543 | ssl->state++; |
| 2544 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2545 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2546 | |
| 2547 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2548 | } |
| 2549 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2550 | #if ! defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 2551 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2552 | static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | da1ff38 | 2013-11-25 17:38:36 +0100 | [diff] [blame] | 2553 | { |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 2554 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 2555 | ssl->handshake->ciphersuite_info; |
Manuel Pégourié-Gonnard | da1ff38 | 2013-11-25 17:38:36 +0100 | [diff] [blame] | 2556 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2557 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) ); |
Manuel Pégourié-Gonnard | da1ff38 | 2013-11-25 17:38:36 +0100 | [diff] [blame] | 2558 | |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 2559 | if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ) |
Manuel Pégourié-Gonnard | da1ff38 | 2013-11-25 17:38:36 +0100 | [diff] [blame] | 2560 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2561 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) ); |
Manuel Pégourié-Gonnard | da1ff38 | 2013-11-25 17:38:36 +0100 | [diff] [blame] | 2562 | ssl->state++; |
| 2563 | return( 0 ); |
| 2564 | } |
| 2565 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2566 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2567 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | da1ff38 | 2013-11-25 17:38:36 +0100 | [diff] [blame] | 2568 | } |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2569 | #else /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 2570 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2571 | static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2572 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2573 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | d1b7f2b | 2016-02-24 14:13:22 +0000 | [diff] [blame] | 2574 | unsigned char *buf; |
| 2575 | size_t n = 0; |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2576 | size_t cert_type_len = 0, dn_len = 0; |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 2577 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 2578 | ssl->handshake->ciphersuite_info; |
Ronald Cron | 90915f2 | 2022-03-07 11:11:36 +0100 | [diff] [blame] | 2579 | size_t sig_alg_len; |
| 2580 | #if defined(MBEDTLS_DEBUG_C) |
| 2581 | unsigned char *sig_alg; |
Glenn Strauss | bd10c4e | 2022-06-25 03:15:48 -0400 | [diff] [blame] | 2582 | unsigned char *dn; |
Ronald Cron | 90915f2 | 2022-03-07 11:11:36 +0100 | [diff] [blame] | 2583 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2584 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2585 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2586 | |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 2587 | if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ) |
Manuel Pégourié-Gonnard | da1ff38 | 2013-11-25 17:38:36 +0100 | [diff] [blame] | 2588 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2589 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) ); |
Manuel Pégourié-Gonnard | da1ff38 | 2013-11-25 17:38:36 +0100 | [diff] [blame] | 2590 | ssl->state++; |
| 2591 | return( 0 ); |
| 2592 | } |
| 2593 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 2594 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2595 | { |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 2596 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 2597 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2598 | } |
| 2599 | |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 2600 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
| 2601 | { |
| 2602 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) ); |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2603 | mbedtls_ssl_send_alert_message( |
| 2604 | ssl, |
| 2605 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2606 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 2607 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 2608 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2609 | |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 2610 | ssl->state++; |
Jerry Yu | fb28b88 | 2022-01-28 11:05:58 +0800 | [diff] [blame] | 2611 | ssl->handshake->client_auth = |
| 2612 | ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2613 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2614 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request", |
Jerry Yu | fb28b88 | 2022-01-28 11:05:58 +0800 | [diff] [blame] | 2615 | ssl->handshake->client_auth ? "a" : "no" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2616 | |
Jerry Yu | fb28b88 | 2022-01-28 11:05:58 +0800 | [diff] [blame] | 2617 | if( ssl->handshake->client_auth == 0 ) |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 2618 | { |
Johan Pascal | a89ca86 | 2020-08-25 10:03:19 +0200 | [diff] [blame] | 2619 | /* Current message is probably the ServerHelloDone */ |
| 2620 | ssl->keep_current_message = 1; |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2621 | goto exit; |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 2622 | } |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2623 | |
Manuel Pégourié-Gonnard | 04c1b4e | 2014-09-10 19:25:43 +0200 | [diff] [blame] | 2624 | /* |
| 2625 | * struct { |
| 2626 | * ClientCertificateType certificate_types<1..2^8-1>; |
| 2627 | * SignatureAndHashAlgorithm |
| 2628 | * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only |
| 2629 | * DistinguishedName certificate_authorities<0..2^16-1>; |
| 2630 | * } CertificateRequest; |
Manuel Pégourié-Gonnard | d1b7f2b | 2016-02-24 14:13:22 +0000 | [diff] [blame] | 2631 | * |
| 2632 | * Since we only support a single certificate on clients, let's just |
| 2633 | * ignore all the information that's supposed to help us pick a |
| 2634 | * certificate. |
| 2635 | * |
| 2636 | * We could check that our certificate matches the request, and bail out |
| 2637 | * if it doesn't, but it's simpler to just send the certificate anyway, |
| 2638 | * and give the server the opportunity to decide if it should terminate |
| 2639 | * the connection when it doesn't like our certificate. |
| 2640 | * |
| 2641 | * Same goes for the hash in TLS 1.2's signature_algorithms: at this |
| 2642 | * point we only have one hash available (see comments in |
Simon Butcher | c0957bd | 2016-03-01 13:16:57 +0000 | [diff] [blame] | 2643 | * write_certificate_verify), so let's just use what we have. |
Manuel Pégourié-Gonnard | d1b7f2b | 2016-02-24 14:13:22 +0000 | [diff] [blame] | 2644 | * |
| 2645 | * However, we still minimally parse the message to check it is at least |
| 2646 | * superficially sane. |
Manuel Pégourié-Gonnard | 04c1b4e | 2014-09-10 19:25:43 +0200 | [diff] [blame] | 2647 | */ |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2648 | buf = ssl->in_msg; |
Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 2649 | |
Manuel Pégourié-Gonnard | d1b7f2b | 2016-02-24 14:13:22 +0000 | [diff] [blame] | 2650 | /* certificate_types */ |
Krzysztof Stachowiak | 73b183c | 2018-04-05 10:20:09 +0200 | [diff] [blame] | 2651 | if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) ) |
| 2652 | { |
| 2653 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) ); |
| 2654 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2655 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Hanno Becker | 5697af0 | 2021-06-24 10:33:51 +0100 | [diff] [blame] | 2656 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Krzysztof Stachowiak | 73b183c | 2018-04-05 10:20:09 +0200 | [diff] [blame] | 2657 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2658 | cert_type_len = buf[mbedtls_ssl_hs_hdr_len( ssl )]; |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2659 | n = cert_type_len; |
| 2660 | |
Krzysztof Stachowiak | bc145f7 | 2018-03-20 11:19:50 +0100 | [diff] [blame] | 2661 | /* |
Krzysztof Stachowiak | 94d4997 | 2018-04-05 14:48:55 +0200 | [diff] [blame] | 2662 | * In the subsequent code there are two paths that read from buf: |
Krzysztof Stachowiak | bc145f7 | 2018-03-20 11:19:50 +0100 | [diff] [blame] | 2663 | * * the length of the signature algorithms field (if minor version of |
| 2664 | * SSL is 3), |
| 2665 | * * distinguished name length otherwise. |
| 2666 | * Both reach at most the index: |
| 2667 | * ...hdr_len + 2 + n, |
| 2668 | * therefore the buffer length at this point must be greater than that |
| 2669 | * regardless of the actual code path. |
| 2670 | */ |
| 2671 | if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n ) |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2672 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2673 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2674 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2675 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Hanno Becker | 5697af0 | 2021-06-24 10:33:51 +0100 | [diff] [blame] | 2676 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2677 | } |
| 2678 | |
Manuel Pégourié-Gonnard | d1b7f2b | 2016-02-24 14:13:22 +0000 | [diff] [blame] | 2679 | /* supported_signature_algorithms */ |
Ronald Cron | 90915f2 | 2022-03-07 11:11:36 +0100 | [diff] [blame] | 2680 | sig_alg_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 ) |
| 2681 | | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) ); |
| 2682 | |
| 2683 | /* |
| 2684 | * The furthest access in buf is in the loop few lines below: |
| 2685 | * sig_alg[i + 1], |
| 2686 | * where: |
| 2687 | * sig_alg = buf + ...hdr_len + 3 + n, |
| 2688 | * max(i) = sig_alg_len - 1. |
| 2689 | * Therefore the furthest access is: |
| 2690 | * buf[...hdr_len + 3 + n + sig_alg_len - 1 + 1], |
| 2691 | * which reduces to: |
| 2692 | * buf[...hdr_len + 3 + n + sig_alg_len], |
| 2693 | * which is one less than we need the buf to be. |
| 2694 | */ |
| 2695 | if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n + sig_alg_len ) |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2696 | { |
Ronald Cron | 90915f2 | 2022-03-07 11:11:36 +0100 | [diff] [blame] | 2697 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) ); |
| 2698 | mbedtls_ssl_send_alert_message( |
| 2699 | ssl, |
| 2700 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2701 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 2702 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 2703 | } |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2704 | |
Ronald Cron | 90915f2 | 2022-03-07 11:11:36 +0100 | [diff] [blame] | 2705 | #if defined(MBEDTLS_DEBUG_C) |
| 2706 | sig_alg = buf + mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n; |
| 2707 | for( size_t i = 0; i < sig_alg_len; i += 2 ) |
| 2708 | { |
| 2709 | MBEDTLS_SSL_DEBUG_MSG( 3, |
| 2710 | ( "Supported Signature Algorithm found: %d,%d", |
| 2711 | sig_alg[i], sig_alg[i + 1] ) ); |
| 2712 | } |
| 2713 | #endif |
| 2714 | |
| 2715 | n += 2 + sig_alg_len; |
| 2716 | |
Manuel Pégourié-Gonnard | d1b7f2b | 2016-02-24 14:13:22 +0000 | [diff] [blame] | 2717 | /* certificate_authorities */ |
| 2718 | dn_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 ) |
| 2719 | | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) ); |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2720 | |
| 2721 | n += dn_len; |
Manuel Pégourié-Gonnard | d1b7f2b | 2016-02-24 14:13:22 +0000 | [diff] [blame] | 2722 | if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n ) |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2723 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2724 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2725 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2726 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Hanno Becker | 5697af0 | 2021-06-24 10:33:51 +0100 | [diff] [blame] | 2727 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2728 | } |
| 2729 | |
Glenn Strauss | bd10c4e | 2022-06-25 03:15:48 -0400 | [diff] [blame] | 2730 | #if defined(MBEDTLS_DEBUG_C) |
| 2731 | dn = buf + mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n - dn_len; |
| 2732 | for( size_t i = 0, dni_len = 0; i < dn_len; i += 2 + dni_len ) |
| 2733 | { |
| 2734 | unsigned char *p = dn + i + 2; |
| 2735 | mbedtls_x509_name name; |
| 2736 | mbedtls_x509_name *name_cur, *name_prv; |
| 2737 | size_t asn1_len; |
| 2738 | char s[MBEDTLS_X509_MAX_DN_NAME_SIZE]; |
| 2739 | memset( &name, 0, sizeof( name ) ); |
| 2740 | dni_len = MBEDTLS_GET_UINT16_BE( dn + i, 0 ); |
| 2741 | if( dni_len > dn_len - i - 2 || |
| 2742 | mbedtls_asn1_get_tag( &p, p + dni_len, &asn1_len, |
| 2743 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) != 0 || |
| 2744 | mbedtls_x509_get_name( &p, p + asn1_len, &name ) != 0 ) |
| 2745 | { |
| 2746 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) ); |
| 2747 | mbedtls_ssl_send_alert_message( |
| 2748 | ssl, |
| 2749 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2750 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 2751 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 2752 | } |
| 2753 | MBEDTLS_SSL_DEBUG_MSG( 3, |
| 2754 | ( "DN hint: %.*s", |
| 2755 | mbedtls_x509_dn_gets( s, sizeof(s), &name ), s ) ); |
| 2756 | name_cur = name.next; |
| 2757 | while( name_cur != NULL ) |
| 2758 | { |
| 2759 | name_prv = name_cur; |
| 2760 | name_cur = name_cur->next; |
| 2761 | mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) ); |
| 2762 | mbedtls_free( name_prv ); |
| 2763 | } |
| 2764 | } |
| 2765 | #endif |
| 2766 | |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2767 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2768 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2769 | |
| 2770 | return( 0 ); |
| 2771 | } |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2772 | #endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2773 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 2774 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2775 | static int ssl_parse_server_hello_done( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2776 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2777 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2778 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2779 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2780 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 2781 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2782 | { |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 2783 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 2784 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2785 | } |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 2786 | |
| 2787 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
| 2788 | { |
| 2789 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) ); |
| 2790 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 2791 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2792 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2793 | if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) || |
| 2794 | ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2795 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2796 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2797 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2798 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Hanno Becker | 029cc2f | 2021-06-24 10:09:50 +0100 | [diff] [blame] | 2799 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2800 | } |
| 2801 | |
| 2802 | ssl->state++; |
| 2803 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2804 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2805 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2806 | mbedtls_ssl_recv_flight_completed( ssl ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2807 | #endif |
| 2808 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2809 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2810 | |
| 2811 | return( 0 ); |
| 2812 | } |
| 2813 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 2814 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2815 | static int ssl_write_client_key_exchange( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2816 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2817 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 2818 | |
| 2819 | size_t header_len; |
| 2820 | size_t content_len; |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 2821 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 2822 | ssl->handshake->ciphersuite_info; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2823 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2824 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2825 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2826 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) |
| 2827 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2828 | { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2829 | /* |
| 2830 | * DHM key exchange -- send G^X mod P |
| 2831 | */ |
Gilles Peskine | 487bbf6 | 2021-05-27 22:17:07 +0200 | [diff] [blame] | 2832 | content_len = mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2833 | |
Joe Subbiani | 6dd7364 | 2021-07-19 11:56:54 +0100 | [diff] [blame] | 2834 | MBEDTLS_PUT_UINT16_BE( content_len, ssl->out_msg, 4 ); |
Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 2835 | header_len = 6; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2836 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2837 | ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx, |
Gilles Peskine | 487bbf6 | 2021-05-27 22:17:07 +0200 | [diff] [blame] | 2838 | (int) mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx ), |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2839 | &ssl->out_msg[header_len], content_len, |
| 2840 | ssl->conf->f_rng, ssl->conf->p_rng ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2841 | if( ret != 0 ) |
| 2842 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2843 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2844 | return( ret ); |
| 2845 | } |
| 2846 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2847 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X ); |
| 2848 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2849 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2850 | if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx, |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2851 | ssl->handshake->premaster, |
| 2852 | MBEDTLS_PREMASTER_SIZE, |
| 2853 | &ssl->handshake->pmslen, |
| 2854 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2855 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2856 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2857 | return( ret ); |
| 2858 | } |
| 2859 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2860 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2861 | } |
| 2862 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2863 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */ |
Neil Armstrong | d8419ff | 2022-04-12 14:39:12 +0200 | [diff] [blame] | 2864 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ |
| 2865 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \ |
| 2866 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ |
| 2867 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 2868 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA || |
Przemek Stekiel | d905d33 | 2022-03-16 09:50:56 +0100 | [diff] [blame] | 2869 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA || |
| 2870 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA || |
| 2871 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA ) |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 2872 | { |
Neil Armstrong | 11d4945 | 2022-04-13 15:03:43 +0200 | [diff] [blame] | 2873 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | a0237f8 | 2022-02-24 13:24:52 -0500 | [diff] [blame] | 2874 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2875 | psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED; |
Janos Follath | 53b8ec2 | 2019-08-08 10:28:27 +0100 | [diff] [blame] | 2876 | psa_key_attributes_t key_attributes; |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 2877 | |
| 2878 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
| 2879 | |
Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 2880 | header_len = 4; |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 2881 | |
Hanno Becker | 0a94a64 | 2019-01-11 14:35:30 +0000 | [diff] [blame] | 2882 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) ); |
| 2883 | |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 2884 | /* |
| 2885 | * Generate EC private key for ECDHE exchange. |
| 2886 | */ |
| 2887 | |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 2888 | /* The master secret is obtained from the shared ECDH secret by |
| 2889 | * applying the TLS 1.2 PRF with a specific salt and label. While |
| 2890 | * the PSA Crypto API encourages combining key agreement schemes |
| 2891 | * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not |
| 2892 | * yet support the provisioning of salt + label to the KDF. |
| 2893 | * For the time being, we therefore need to split the computation |
| 2894 | * of the ECDH secret and the application of the TLS 1.2 PRF. */ |
Janos Follath | 53b8ec2 | 2019-08-08 10:28:27 +0100 | [diff] [blame] | 2895 | key_attributes = psa_key_attributes_init(); |
| 2896 | psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE ); |
Janos Follath | df3b089 | 2019-08-08 11:12:24 +0100 | [diff] [blame] | 2897 | psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH ); |
Gilles Peskine | 4245980 | 2019-12-19 13:31:53 +0100 | [diff] [blame] | 2898 | psa_set_key_type( &key_attributes, handshake->ecdh_psa_type ); |
| 2899 | psa_set_key_bits( &key_attributes, handshake->ecdh_bits ); |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 2900 | |
| 2901 | /* Generate ECDH private key. */ |
Janos Follath | 53b8ec2 | 2019-08-08 10:28:27 +0100 | [diff] [blame] | 2902 | status = psa_generate_key( &key_attributes, |
Janos Follath | df3b089 | 2019-08-08 11:12:24 +0100 | [diff] [blame] | 2903 | &handshake->ecdh_psa_privkey ); |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 2904 | if( status != PSA_SUCCESS ) |
| 2905 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 2906 | |
Manuel Pégourié-Gonnard | 58d2383 | 2022-01-18 12:17:15 +0100 | [diff] [blame] | 2907 | /* Export the public part of the ECDH private key from PSA. |
Manuel Pégourié-Gonnard | 5d6053f | 2022-02-08 10:26:19 +0100 | [diff] [blame] | 2908 | * The export format is an ECPoint structure as expected by TLS, |
Manuel Pégourié-Gonnard | 58d2383 | 2022-01-18 12:17:15 +0100 | [diff] [blame] | 2909 | * but we just need to add a length byte before that. */ |
| 2910 | unsigned char *own_pubkey = ssl->out_msg + header_len + 1; |
| 2911 | unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN; |
| 2912 | size_t own_pubkey_max_len = (size_t)( end - own_pubkey ); |
| 2913 | size_t own_pubkey_len; |
| 2914 | |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 2915 | status = psa_export_public_key( handshake->ecdh_psa_privkey, |
Manuel Pégourié-Gonnard | 58d2383 | 2022-01-18 12:17:15 +0100 | [diff] [blame] | 2916 | own_pubkey, own_pubkey_max_len, |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 2917 | &own_pubkey_len ); |
| 2918 | if( status != PSA_SUCCESS ) |
Andrzej Kurek | a0237f8 | 2022-02-24 13:24:52 -0500 | [diff] [blame] | 2919 | { |
| 2920 | psa_destroy_key( handshake->ecdh_psa_privkey ); |
| 2921 | handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 2922 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Andrzej Kurek | a0237f8 | 2022-02-24 13:24:52 -0500 | [diff] [blame] | 2923 | } |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 2924 | |
Manuel Pégourié-Gonnard | 58d2383 | 2022-01-18 12:17:15 +0100 | [diff] [blame] | 2925 | ssl->out_msg[header_len] = (unsigned char) own_pubkey_len; |
| 2926 | content_len = own_pubkey_len + 1; |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 2927 | |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 2928 | /* The ECDH secret is the premaster secret used for key derivation. */ |
| 2929 | |
Janos Follath | df3b089 | 2019-08-08 11:12:24 +0100 | [diff] [blame] | 2930 | /* Compute ECDH shared secret. */ |
| 2931 | status = psa_raw_key_agreement( PSA_ALG_ECDH, |
| 2932 | handshake->ecdh_psa_privkey, |
| 2933 | handshake->ecdh_psa_peerkey, |
| 2934 | handshake->ecdh_psa_peerkey_len, |
| 2935 | ssl->handshake->premaster, |
| 2936 | sizeof( ssl->handshake->premaster ), |
| 2937 | &ssl->handshake->pmslen ); |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 2938 | |
Andrzej Kurek | a0237f8 | 2022-02-24 13:24:52 -0500 | [diff] [blame] | 2939 | destruction_status = psa_destroy_key( handshake->ecdh_psa_privkey ); |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2940 | handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT; |
Andrzej Kurek | a0237f8 | 2022-02-24 13:24:52 -0500 | [diff] [blame] | 2941 | |
| 2942 | if( status != PSA_SUCCESS || destruction_status != PSA_SUCCESS ) |
| 2943 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Neil Armstrong | d8419ff | 2022-04-12 14:39:12 +0200 | [diff] [blame] | 2944 | #else |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2945 | /* |
| 2946 | * ECDH key exchange -- send client public value |
| 2947 | */ |
Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 2948 | header_len = 4; |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2949 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2950 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 2951 | if( ssl->handshake->ecrs_enabled ) |
| 2952 | { |
Manuel Pégourié-Gonnard | c37423f | 2018-10-16 10:28:17 +0200 | [diff] [blame] | 2953 | if( ssl->handshake->ecrs_state == ssl_ecrs_cke_ecdh_calc_secret ) |
Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 2954 | goto ecdh_calc_secret; |
Manuel Pégourié-Gonnard | 23e4162 | 2017-05-18 12:35:37 +0200 | [diff] [blame] | 2955 | |
Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 2956 | mbedtls_ecdh_enable_restart( &ssl->handshake->ecdh_ctx ); |
| 2957 | } |
Manuel Pégourié-Gonnard | 2350b4e | 2017-05-16 09:26:48 +0200 | [diff] [blame] | 2958 | #endif |
| 2959 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2960 | ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx, |
Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 2961 | &content_len, |
| 2962 | &ssl->out_msg[header_len], 1000, |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 2963 | ssl->conf->f_rng, ssl->conf->p_rng ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2964 | if( ret != 0 ) |
| 2965 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2966 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret ); |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2967 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 558da9c | 2018-06-13 12:02:12 +0200 | [diff] [blame] | 2968 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 2969 | ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS; |
| 2970 | #endif |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2971 | return( ret ); |
| 2972 | } |
| 2973 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 2974 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 2975 | MBEDTLS_DEBUG_ECDH_Q ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2976 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2977 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 2978 | if( ssl->handshake->ecrs_enabled ) |
| 2979 | { |
Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 2980 | ssl->handshake->ecrs_n = content_len; |
Manuel Pégourié-Gonnard | c37423f | 2018-10-16 10:28:17 +0200 | [diff] [blame] | 2981 | ssl->handshake->ecrs_state = ssl_ecrs_cke_ecdh_calc_secret; |
Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 2982 | } |
Manuel Pégourié-Gonnard | 2350b4e | 2017-05-16 09:26:48 +0200 | [diff] [blame] | 2983 | |
| 2984 | ecdh_calc_secret: |
Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 2985 | if( ssl->handshake->ecrs_enabled ) |
Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 2986 | content_len = ssl->handshake->ecrs_n; |
Manuel Pégourié-Gonnard | 2350b4e | 2017-05-16 09:26:48 +0200 | [diff] [blame] | 2987 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2988 | if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2989 | &ssl->handshake->pmslen, |
| 2990 | ssl->handshake->premaster, |
| 2991 | MBEDTLS_MPI_MAX_SIZE, |
| 2992 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2993 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2994 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret ); |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2995 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 558da9c | 2018-06-13 12:02:12 +0200 | [diff] [blame] | 2996 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 2997 | ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS; |
| 2998 | #endif |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2999 | return( ret ); |
| 3000 | } |
| 3001 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 3002 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 3003 | MBEDTLS_DEBUG_ECDH_Z ); |
Neil Armstrong | 11d4945 | 2022-04-13 15:03:43 +0200 | [diff] [blame] | 3004 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3005 | } |
| 3006 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3007 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || |
| 3008 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED || |
| 3009 | MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED || |
| 3010 | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */ |
Neil Armstrong | 868af82 | 2022-03-09 10:26:25 +0100 | [diff] [blame] | 3011 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 3012 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
| 3013 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
| 3014 | { |
| 3015 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3016 | psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3017 | psa_key_attributes_t key_attributes; |
| 3018 | |
| 3019 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
| 3020 | |
| 3021 | /* |
| 3022 | * opaque psk_identity<0..2^16-1>; |
| 3023 | */ |
| 3024 | if( mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 ) |
Neil Armstrong | 868af82 | 2022-03-09 10:26:25 +0100 | [diff] [blame] | 3025 | /* We don't offer PSK suites if we don't have a PSK, |
| 3026 | * and we check that the server's choice is among the |
| 3027 | * ciphersuites we offered, so this should never happen. */ |
| 3028 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Neil Armstrong | 868af82 | 2022-03-09 10:26:25 +0100 | [diff] [blame] | 3029 | |
Neil Armstrong | fc834f2 | 2022-03-23 17:54:38 +0100 | [diff] [blame] | 3030 | /* uint16 to store content length */ |
| 3031 | const size_t content_len_size = 2; |
| 3032 | |
Neil Armstrong | 868af82 | 2022-03-09 10:26:25 +0100 | [diff] [blame] | 3033 | header_len = 4; |
Neil Armstrong | 868af82 | 2022-03-09 10:26:25 +0100 | [diff] [blame] | 3034 | |
Neil Armstrong | b7ca76b | 2022-04-04 18:27:15 +0200 | [diff] [blame] | 3035 | if( header_len + content_len_size + ssl->conf->psk_identity_len |
Neil Armstrong | fc834f2 | 2022-03-23 17:54:38 +0100 | [diff] [blame] | 3036 | > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
Neil Armstrong | 868af82 | 2022-03-09 10:26:25 +0100 | [diff] [blame] | 3037 | { |
| 3038 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 3039 | ( "psk identity too long or SSL buffer too short" ) ); |
| 3040 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 3041 | } |
| 3042 | |
Neil Armstrong | b7ca76b | 2022-04-04 18:27:15 +0200 | [diff] [blame] | 3043 | unsigned char *p = ssl->out_msg + header_len; |
Neil Armstrong | 868af82 | 2022-03-09 10:26:25 +0100 | [diff] [blame] | 3044 | |
Neil Armstrong | b7ca76b | 2022-04-04 18:27:15 +0200 | [diff] [blame] | 3045 | *p++ = MBEDTLS_BYTE_1( ssl->conf->psk_identity_len ); |
| 3046 | *p++ = MBEDTLS_BYTE_0( ssl->conf->psk_identity_len ); |
| 3047 | header_len += content_len_size; |
| 3048 | |
| 3049 | memcpy( p, ssl->conf->psk_identity, |
Neil Armstrong | 868af82 | 2022-03-09 10:26:25 +0100 | [diff] [blame] | 3050 | ssl->conf->psk_identity_len ); |
Neil Armstrong | b7ca76b | 2022-04-04 18:27:15 +0200 | [diff] [blame] | 3051 | p += ssl->conf->psk_identity_len; |
| 3052 | |
Neil Armstrong | 868af82 | 2022-03-09 10:26:25 +0100 | [diff] [blame] | 3053 | header_len += ssl->conf->psk_identity_len; |
| 3054 | |
| 3055 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) ); |
| 3056 | |
| 3057 | /* |
| 3058 | * Generate EC private key for ECDHE exchange. |
| 3059 | */ |
| 3060 | |
| 3061 | /* The master secret is obtained from the shared ECDH secret by |
| 3062 | * applying the TLS 1.2 PRF with a specific salt and label. While |
| 3063 | * the PSA Crypto API encourages combining key agreement schemes |
| 3064 | * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not |
| 3065 | * yet support the provisioning of salt + label to the KDF. |
| 3066 | * For the time being, we therefore need to split the computation |
| 3067 | * of the ECDH secret and the application of the TLS 1.2 PRF. */ |
| 3068 | key_attributes = psa_key_attributes_init(); |
| 3069 | psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE ); |
| 3070 | psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH ); |
| 3071 | psa_set_key_type( &key_attributes, handshake->ecdh_psa_type ); |
| 3072 | psa_set_key_bits( &key_attributes, handshake->ecdh_bits ); |
| 3073 | |
| 3074 | /* Generate ECDH private key. */ |
| 3075 | status = psa_generate_key( &key_attributes, |
| 3076 | &handshake->ecdh_psa_privkey ); |
| 3077 | if( status != PSA_SUCCESS ) |
Neil Armstrong | c530aa6 | 2022-03-23 17:45:01 +0100 | [diff] [blame] | 3078 | return( psa_ssl_status_to_mbedtls( status ) ); |
Neil Armstrong | 868af82 | 2022-03-09 10:26:25 +0100 | [diff] [blame] | 3079 | |
| 3080 | /* Export the public part of the ECDH private key from PSA. |
| 3081 | * The export format is an ECPoint structure as expected by TLS, |
| 3082 | * but we just need to add a length byte before that. */ |
Neil Armstrong | b7ca76b | 2022-04-04 18:27:15 +0200 | [diff] [blame] | 3083 | unsigned char *own_pubkey = p + 1; |
Neil Armstrong | 868af82 | 2022-03-09 10:26:25 +0100 | [diff] [blame] | 3084 | unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN; |
| 3085 | size_t own_pubkey_max_len = (size_t)( end - own_pubkey ); |
Neil Armstrong | bc5e8f9 | 2022-03-23 17:42:50 +0100 | [diff] [blame] | 3086 | size_t own_pubkey_len = 0; |
Neil Armstrong | 868af82 | 2022-03-09 10:26:25 +0100 | [diff] [blame] | 3087 | |
| 3088 | status = psa_export_public_key( handshake->ecdh_psa_privkey, |
| 3089 | own_pubkey, own_pubkey_max_len, |
| 3090 | &own_pubkey_len ); |
| 3091 | if( status != PSA_SUCCESS ) |
| 3092 | { |
| 3093 | psa_destroy_key( handshake->ecdh_psa_privkey ); |
| 3094 | handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT; |
Neil Armstrong | c530aa6 | 2022-03-23 17:45:01 +0100 | [diff] [blame] | 3095 | return( psa_ssl_status_to_mbedtls( status ) ); |
Neil Armstrong | 868af82 | 2022-03-09 10:26:25 +0100 | [diff] [blame] | 3096 | } |
| 3097 | |
Neil Armstrong | b7ca76b | 2022-04-04 18:27:15 +0200 | [diff] [blame] | 3098 | *p = (unsigned char) own_pubkey_len; |
Neil Armstrong | 868af82 | 2022-03-09 10:26:25 +0100 | [diff] [blame] | 3099 | content_len = own_pubkey_len + 1; |
| 3100 | |
Neil Armstrong | 2540045 | 2022-03-23 17:44:07 +0100 | [diff] [blame] | 3101 | /* As RFC 5489 section 2, the premaster secret is formed as follows: |
| 3102 | * - a uint16 containing the length (in octets) of the ECDH computation |
| 3103 | * - the octet string produced by the ECDH computation |
| 3104 | * - a uint16 containing the length (in octets) of the PSK |
| 3105 | * - the PSK itself |
| 3106 | */ |
Neil Armstrong | b7ca76b | 2022-04-04 18:27:15 +0200 | [diff] [blame] | 3107 | unsigned char *pms = ssl->handshake->premaster; |
| 3108 | const unsigned char* const pms_end = pms + |
Neil Armstrong | d8420ca | 2022-03-23 17:46:04 +0100 | [diff] [blame] | 3109 | sizeof( ssl->handshake->premaster ); |
Neil Armstrong | 0bdb68a | 2022-03-23 17:46:32 +0100 | [diff] [blame] | 3110 | /* uint16 to store length (in octets) of the ECDH computation */ |
| 3111 | const size_t zlen_size = 2; |
Neil Armstrong | bc5e8f9 | 2022-03-23 17:42:50 +0100 | [diff] [blame] | 3112 | size_t zlen = 0; |
Neil Armstrong | 868af82 | 2022-03-09 10:26:25 +0100 | [diff] [blame] | 3113 | |
Neil Armstrong | 2540045 | 2022-03-23 17:44:07 +0100 | [diff] [blame] | 3114 | /* Perform ECDH computation after the uint16 reserved for the length */ |
Neil Armstrong | 868af82 | 2022-03-09 10:26:25 +0100 | [diff] [blame] | 3115 | status = psa_raw_key_agreement( PSA_ALG_ECDH, |
| 3116 | handshake->ecdh_psa_privkey, |
| 3117 | handshake->ecdh_psa_peerkey, |
| 3118 | handshake->ecdh_psa_peerkey_len, |
Neil Armstrong | b7ca76b | 2022-04-04 18:27:15 +0200 | [diff] [blame] | 3119 | pms + zlen_size, |
| 3120 | pms_end - ( pms + zlen_size ), |
Neil Armstrong | 868af82 | 2022-03-09 10:26:25 +0100 | [diff] [blame] | 3121 | &zlen ); |
| 3122 | |
| 3123 | destruction_status = psa_destroy_key( handshake->ecdh_psa_privkey ); |
| 3124 | handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT; |
| 3125 | |
Neil Armstrong | c530aa6 | 2022-03-23 17:45:01 +0100 | [diff] [blame] | 3126 | if( status != PSA_SUCCESS ) |
| 3127 | return( psa_ssl_status_to_mbedtls( status ) ); |
| 3128 | else if( destruction_status != PSA_SUCCESS ) |
| 3129 | return( psa_ssl_status_to_mbedtls( destruction_status ) ); |
Neil Armstrong | 868af82 | 2022-03-09 10:26:25 +0100 | [diff] [blame] | 3130 | |
Neil Armstrong | 2540045 | 2022-03-23 17:44:07 +0100 | [diff] [blame] | 3131 | /* Write the ECDH computation length before the ECDH computation */ |
Neil Armstrong | b7ca76b | 2022-04-04 18:27:15 +0200 | [diff] [blame] | 3132 | MBEDTLS_PUT_UINT16_BE( zlen, pms, 0 ); |
| 3133 | pms += zlen_size + zlen; |
Neil Armstrong | 868af82 | 2022-03-09 10:26:25 +0100 | [diff] [blame] | 3134 | } |
| 3135 | else |
| 3136 | #endif /* MBEDTLS_USE_PSA_CRYPTO && |
| 3137 | MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3138 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3139 | if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 3140 | { |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 3141 | /* |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 3142 | * opaque psk_identity<0..2^16-1>; |
| 3143 | */ |
Ronald Cron | d491c2d | 2022-02-19 18:30:46 +0100 | [diff] [blame] | 3144 | if( mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 ) |
Manuel Pégourié-Gonnard | b4b19f3 | 2015-07-07 11:41:21 +0200 | [diff] [blame] | 3145 | { |
Hanno Becker | 2e4f616 | 2018-10-23 11:54:44 +0100 | [diff] [blame] | 3146 | /* We don't offer PSK suites if we don't have a PSK, |
| 3147 | * and we check that the server's choice is among the |
| 3148 | * ciphersuites we offered, so this should never happen. */ |
| 3149 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | b4b19f3 | 2015-07-07 11:41:21 +0200 | [diff] [blame] | 3150 | } |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 3151 | |
Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3152 | header_len = 4; |
| 3153 | content_len = ssl->conf->psk_identity_len; |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 3154 | |
Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3155 | if( header_len + 2 + content_len > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 3156 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3157 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 3158 | ( "psk identity too long or SSL buffer too short" ) ); |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 3159 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 3160 | } |
| 3161 | |
Joe Subbiani | fbeb692 | 2021-07-16 14:27:50 +0100 | [diff] [blame] | 3162 | ssl->out_msg[header_len++] = MBEDTLS_BYTE_1( content_len ); |
| 3163 | ssl->out_msg[header_len++] = MBEDTLS_BYTE_0( content_len ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3164 | |
Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3165 | memcpy( ssl->out_msg + header_len, |
| 3166 | ssl->conf->psk_identity, |
| 3167 | ssl->conf->psk_identity_len ); |
| 3168 | header_len += ssl->conf->psk_identity_len; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3169 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3170 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 3171 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3172 | { |
Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3173 | content_len = 0; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 3174 | } |
Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3175 | else |
| 3176 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3177 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
| 3178 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 3179 | { |
Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3180 | if( ( ret = ssl_write_encrypted_pms( ssl, header_len, |
| 3181 | &content_len, 2 ) ) != 0 ) |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 3182 | return( ret ); |
| 3183 | } |
| 3184 | else |
| 3185 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3186 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 3187 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3188 | { |
Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3189 | /* |
| 3190 | * ClientDiffieHellmanPublic public (DHM send G^X mod P) |
| 3191 | */ |
Gilles Peskine | 487bbf6 | 2021-05-27 22:17:07 +0200 | [diff] [blame] | 3192 | content_len = mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx ); |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 3193 | |
Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3194 | if( header_len + 2 + content_len > |
| 3195 | MBEDTLS_SSL_OUT_CONTENT_LEN ) |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 3196 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3197 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 3198 | ( "psk identity or DHM size too long or SSL buffer too short" ) ); |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 3199 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 3200 | } |
| 3201 | |
Joe Subbiani | fbeb692 | 2021-07-16 14:27:50 +0100 | [diff] [blame] | 3202 | ssl->out_msg[header_len++] = MBEDTLS_BYTE_1( content_len ); |
| 3203 | ssl->out_msg[header_len++] = MBEDTLS_BYTE_0( content_len ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3204 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3205 | ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx, |
Gilles Peskine | 487bbf6 | 2021-05-27 22:17:07 +0200 | [diff] [blame] | 3206 | (int) mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx ), |
Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3207 | &ssl->out_msg[header_len], content_len, |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 3208 | ssl->conf->f_rng, ssl->conf->p_rng ); |
Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3209 | if( ret != 0 ) |
| 3210 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3211 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret ); |
Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3212 | return( ret ); |
| 3213 | } |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 3214 | |
| 3215 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3216 | unsigned char *pms = ssl->handshake->premaster; |
| 3217 | unsigned char *pms_end = pms + sizeof( ssl->handshake->premaster ); |
| 3218 | size_t pms_len; |
| 3219 | |
| 3220 | /* Write length only when we know the actual value */ |
| 3221 | if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx, |
| 3222 | pms + 2, pms_end - ( pms + 2 ), &pms_len, |
| 3223 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
| 3224 | { |
| 3225 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); |
| 3226 | return( ret ); |
| 3227 | } |
| 3228 | MBEDTLS_PUT_UINT16_BE( pms_len, pms, 0 ); |
| 3229 | pms += 2 + pms_len; |
| 3230 | |
| 3231 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K ); |
| 3232 | #endif |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3233 | } |
Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3234 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3235 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
Neil Armstrong | d8419ff | 2022-04-12 14:39:12 +0200 | [diff] [blame] | 3236 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 3237 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3238 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 3239 | { |
Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3240 | /* |
| 3241 | * ClientECDiffieHellmanPublic public; |
| 3242 | */ |
Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3243 | ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx, |
| 3244 | &content_len, |
| 3245 | &ssl->out_msg[header_len], |
| 3246 | MBEDTLS_SSL_OUT_CONTENT_LEN - header_len, |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 3247 | ssl->conf->f_rng, ssl->conf->p_rng ); |
Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3248 | if( ret != 0 ) |
| 3249 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3250 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret ); |
Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3251 | return( ret ); |
| 3252 | } |
Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 3253 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 3254 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 3255 | MBEDTLS_DEBUG_ECDH_Q ); |
Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3256 | } |
| 3257 | else |
Neil Armstrong | d8419ff | 2022-04-12 14:39:12 +0200 | [diff] [blame] | 3258 | #endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3259 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3260 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3261 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3262 | } |
| 3263 | |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 3264 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3265 | if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl, |
| 3266 | ciphersuite_info->key_exchange ) ) != 0 ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3267 | { |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 3268 | MBEDTLS_SSL_DEBUG_RET( 1, |
Neil Armstrong | cd05f0b | 2022-05-03 10:28:37 +0200 | [diff] [blame] | 3269 | "mbedtls_ssl_psk_derive_premaster", ret ); |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 3270 | return( ret ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3271 | } |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 3272 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3273 | } |
| 3274 | else |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3275 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3276 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) |
| 3277 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3278 | { |
Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3279 | header_len = 4; |
| 3280 | if( ( ret = ssl_write_encrypted_pms( ssl, header_len, |
| 3281 | &content_len, 0 ) ) != 0 ) |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 3282 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3283 | } |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 3284 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3285 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */ |
Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 3286 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 3287 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ) |
| 3288 | { |
Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3289 | header_len = 4; |
Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 3290 | |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 3291 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3292 | unsigned char *out_p = ssl->out_msg + header_len; |
| 3293 | unsigned char *end_p = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN - |
| 3294 | header_len; |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame^] | 3295 | ret = mbedtls_psa_ecjpake_write_round_two( &ssl->handshake->psa_pake_ctx, |
Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 3296 | out_p, end_p - out_p, &content_len ); |
| 3297 | if ( ret != 0 ) |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 3298 | { |
Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 3299 | psa_destroy_key( ssl->handshake->psa_pake_password ); |
| 3300 | psa_pake_abort( &ssl->handshake->psa_pake_ctx ); |
| 3301 | MBEDTLS_SSL_DEBUG_RET( 1 , "psa_pake_output", ret ); |
| 3302 | return( ret ); |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 3303 | } |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 3304 | #else |
Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 3305 | ret = mbedtls_ecjpake_write_round_two( &ssl->handshake->ecjpake_ctx, |
Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3306 | ssl->out_msg + header_len, |
| 3307 | MBEDTLS_SSL_OUT_CONTENT_LEN - header_len, |
| 3308 | &content_len, |
Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 3309 | ssl->conf->f_rng, ssl->conf->p_rng ); |
| 3310 | if( ret != 0 ) |
| 3311 | { |
| 3312 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret ); |
| 3313 | return( ret ); |
| 3314 | } |
| 3315 | |
| 3316 | ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx, |
| 3317 | ssl->handshake->premaster, 32, &ssl->handshake->pmslen, |
| 3318 | ssl->conf->f_rng, ssl->conf->p_rng ); |
| 3319 | if( ret != 0 ) |
| 3320 | { |
| 3321 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret ); |
| 3322 | return( ret ); |
| 3323 | } |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 3324 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 3325 | } |
| 3326 | else |
| 3327 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */ |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 3328 | { |
| 3329 | ((void) ciphersuite_info); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3330 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3331 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 3332 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3333 | |
Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3334 | ssl->out_msglen = header_len + content_len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3335 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 3336 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3337 | |
| 3338 | ssl->state++; |
| 3339 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3340 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3341 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3342 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3343 | return( ret ); |
| 3344 | } |
| 3345 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3346 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3347 | |
| 3348 | return( 0 ); |
| 3349 | } |
| 3350 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3351 | #if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3352 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3353 | static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3354 | { |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3355 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 3356 | ssl->handshake->ciphersuite_info; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3357 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3358 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3359 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3360 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3361 | if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 3362 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3363 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret ); |
Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 3364 | return( ret ); |
| 3365 | } |
| 3366 | |
Hanno Becker | 77adddc | 2019-02-07 12:32:43 +0000 | [diff] [blame] | 3367 | if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ) |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 3368 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3369 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 3370 | ssl->state++; |
| 3371 | return( 0 ); |
| 3372 | } |
| 3373 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3374 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3375 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3376 | } |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3377 | #else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3378 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3379 | static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3380 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3381 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3382 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 3383 | ssl->handshake->ciphersuite_info; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3384 | size_t n = 0, offset = 0; |
| 3385 | unsigned char hash[48]; |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3386 | unsigned char *hash_start = hash; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3387 | mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE; |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 3388 | size_t hashlen; |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 3389 | void *rs_ctx = NULL; |
Gilles Peskine | f00f152 | 2021-06-22 00:09:00 +0200 | [diff] [blame] | 3390 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 3391 | size_t out_buf_len = ssl->out_buf_len - ( ssl->out_msg - ssl->out_buf ); |
| 3392 | #else |
| 3393 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN - ( ssl->out_msg - ssl->out_buf ); |
| 3394 | #endif |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3395 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3396 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3397 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3398 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 3399 | if( ssl->handshake->ecrs_enabled && |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 3400 | ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign ) |
Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 3401 | { |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 3402 | goto sign; |
Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 3403 | } |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 3404 | #endif |
| 3405 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3406 | if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 3407 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3408 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret ); |
Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 3409 | return( ret ); |
| 3410 | } |
| 3411 | |
Hanno Becker | 77adddc | 2019-02-07 12:32:43 +0000 | [diff] [blame] | 3412 | if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3413 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3414 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3415 | ssl->state++; |
| 3416 | return( 0 ); |
| 3417 | } |
| 3418 | |
Jerry Yu | fb28b88 | 2022-01-28 11:05:58 +0800 | [diff] [blame] | 3419 | if( ssl->handshake->client_auth == 0 || |
| 3420 | mbedtls_ssl_own_cert( ssl ) == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3421 | { |
Johan Pascal | a89ca86 | 2020-08-25 10:03:19 +0200 | [diff] [blame] | 3422 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) ); |
| 3423 | ssl->state++; |
| 3424 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3425 | } |
| 3426 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3427 | if( mbedtls_ssl_own_key( ssl ) == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3428 | { |
Manuel Pégourié-Gonnard | b4b19f3 | 2015-07-07 11:41:21 +0200 | [diff] [blame] | 3429 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key for certificate" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3430 | return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3431 | } |
| 3432 | |
| 3433 | /* |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 3434 | * Make a signature of the handshake digests |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3435 | */ |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3436 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 3437 | if( ssl->handshake->ecrs_enabled ) |
| 3438 | ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign; |
| 3439 | |
| 3440 | sign: |
| 3441 | #endif |
| 3442 | |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 3443 | ssl->handshake->calc_verify( ssl, hash, &hashlen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3444 | |
Ronald Cron | 90915f2 | 2022-03-07 11:11:36 +0100 | [diff] [blame] | 3445 | /* |
| 3446 | * digitally-signed struct { |
| 3447 | * opaque handshake_messages[handshake_messages_length]; |
| 3448 | * }; |
| 3449 | * |
| 3450 | * Taking shortcut here. We assume that the server always allows the |
| 3451 | * PRF Hash function and has sent it in the allowed signature |
| 3452 | * algorithms list received in the Certificate Request message. |
| 3453 | * |
| 3454 | * Until we encounter a server that does not, we will take this |
| 3455 | * shortcut. |
| 3456 | * |
| 3457 | * Reason: Otherwise we should have running hashes for SHA512 and |
| 3458 | * SHA224 in order to satisfy 'weird' needs from the server |
| 3459 | * side. |
| 3460 | */ |
| 3461 | if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3462 | { |
Ronald Cron | 90915f2 | 2022-03-07 11:11:36 +0100 | [diff] [blame] | 3463 | md_alg = MBEDTLS_MD_SHA384; |
| 3464 | ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3465 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 3466 | else |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 3467 | { |
Ronald Cron | 90915f2 | 2022-03-07 11:11:36 +0100 | [diff] [blame] | 3468 | md_alg = MBEDTLS_MD_SHA256; |
| 3469 | ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256; |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 3470 | } |
Ronald Cron | 90915f2 | 2022-03-07 11:11:36 +0100 | [diff] [blame] | 3471 | ssl->out_msg[5] = mbedtls_ssl_sig_from_pk( mbedtls_ssl_own_key( ssl ) ); |
| 3472 | |
| 3473 | /* Info from md_alg will be used instead */ |
| 3474 | hashlen = 0; |
| 3475 | offset = 2; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3476 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3477 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 3478 | if( ssl->handshake->ecrs_enabled ) |
Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 3479 | rs_ctx = &ssl->handshake->ecrs_ctx.pk; |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 3480 | #endif |
| 3481 | |
| 3482 | if( ( ret = mbedtls_pk_sign_restartable( mbedtls_ssl_own_key( ssl ), |
| 3483 | md_alg, hash_start, hashlen, |
Gilles Peskine | f00f152 | 2021-06-22 00:09:00 +0200 | [diff] [blame] | 3484 | ssl->out_msg + 6 + offset, |
| 3485 | out_buf_len - 6 - offset, |
| 3486 | &n, |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 3487 | ssl->conf->f_rng, ssl->conf->p_rng, rs_ctx ) ) != 0 ) |
Manuel Pégourié-Gonnard | 76c18a1 | 2013-08-20 16:50:40 +0200 | [diff] [blame] | 3488 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3489 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret ); |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3490 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 558da9c | 2018-06-13 12:02:12 +0200 | [diff] [blame] | 3491 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 3492 | ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS; |
| 3493 | #endif |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 3494 | return( ret ); |
Manuel Pégourié-Gonnard | 76c18a1 | 2013-08-20 16:50:40 +0200 | [diff] [blame] | 3495 | } |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3496 | |
Joe Subbiani | 6dd7364 | 2021-07-19 11:56:54 +0100 | [diff] [blame] | 3497 | MBEDTLS_PUT_UINT16_BE( n, ssl->out_msg, offset + 4 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3498 | |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3499 | ssl->out_msglen = 6 + n + offset; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3500 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 3501 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3502 | |
| 3503 | ssl->state++; |
| 3504 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3505 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3506 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3507 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3508 | return( ret ); |
| 3509 | } |
| 3510 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3511 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3512 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 3513 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3514 | } |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3515 | #endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3516 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3517 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3518 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3519 | static int ssl_parse_new_session_ticket( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3520 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3521 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3522 | uint32_t lifetime; |
| 3523 | size_t ticket_len; |
| 3524 | unsigned char *ticket; |
Manuel Pégourié-Gonnard | 000d5ae | 2014-09-10 21:52:12 +0200 | [diff] [blame] | 3525 | const unsigned char *msg; |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3526 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3527 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) ); |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3528 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 3529 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3530 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3531 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3532 | return( ret ); |
| 3533 | } |
| 3534 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3535 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3536 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3537 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) ); |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3538 | mbedtls_ssl_send_alert_message( |
| 3539 | ssl, |
| 3540 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3541 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3542 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3543 | } |
| 3544 | |
| 3545 | /* |
| 3546 | * struct { |
| 3547 | * uint32 ticket_lifetime_hint; |
| 3548 | * opaque ticket<0..2^16-1>; |
| 3549 | * } NewSessionTicket; |
| 3550 | * |
Manuel Pégourié-Gonnard | 000d5ae | 2014-09-10 21:52:12 +0200 | [diff] [blame] | 3551 | * 0 . 3 ticket_lifetime_hint |
| 3552 | * 4 . 5 ticket_len (n) |
| 3553 | * 6 . 5+n ticket content |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3554 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3555 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET || |
| 3556 | ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len( ssl ) ) |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3557 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3558 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 3559 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3560 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Hanno Becker | 241c197 | 2021-06-24 09:44:26 +0100 | [diff] [blame] | 3561 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3562 | } |
| 3563 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3564 | msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ); |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3565 | |
Philippe Antoine | b5b2543 | 2018-05-11 11:06:29 +0200 | [diff] [blame] | 3566 | lifetime = ( ((uint32_t) msg[0]) << 24 ) | ( msg[1] << 16 ) | |
| 3567 | ( msg[2] << 8 ) | ( msg[3] ); |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3568 | |
Manuel Pégourié-Gonnard | 000d5ae | 2014-09-10 21:52:12 +0200 | [diff] [blame] | 3569 | ticket_len = ( msg[4] << 8 ) | ( msg[5] ); |
| 3570 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3571 | if( ticket_len + 6 + mbedtls_ssl_hs_hdr_len( ssl ) != ssl->in_hslen ) |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3572 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3573 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 3574 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3575 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Hanno Becker | 241c197 | 2021-06-24 09:44:26 +0100 | [diff] [blame] | 3576 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3577 | } |
| 3578 | |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 3579 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %" MBEDTLS_PRINTF_SIZET, ticket_len ) ); |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3580 | |
Manuel Pégourié-Gonnard | 7cd5924 | 2013-08-02 13:24:41 +0200 | [diff] [blame] | 3581 | /* We're not waiting for a NewSessionTicket message any more */ |
| 3582 | ssl->handshake->new_session_ticket = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3583 | ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC; |
Manuel Pégourié-Gonnard | 7cd5924 | 2013-08-02 13:24:41 +0200 | [diff] [blame] | 3584 | |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3585 | /* |
| 3586 | * Zero-length ticket means the server changed his mind and doesn't want |
| 3587 | * to send a ticket after all, so just forget it |
| 3588 | */ |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 3589 | if( ticket_len == 0 ) |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3590 | return( 0 ); |
| 3591 | |
Hanno Becker | b2964cb | 2019-01-30 14:46:35 +0000 | [diff] [blame] | 3592 | if( ssl->session != NULL && ssl->session->ticket != NULL ) |
| 3593 | { |
| 3594 | mbedtls_platform_zeroize( ssl->session->ticket, |
| 3595 | ssl->session->ticket_len ); |
| 3596 | mbedtls_free( ssl->session->ticket ); |
| 3597 | ssl->session->ticket = NULL; |
| 3598 | ssl->session->ticket_len = 0; |
| 3599 | } |
| 3600 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3601 | mbedtls_platform_zeroize( ssl->session_negotiate->ticket, |
| 3602 | ssl->session_negotiate->ticket_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3603 | mbedtls_free( ssl->session_negotiate->ticket ); |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3604 | ssl->session_negotiate->ticket = NULL; |
| 3605 | ssl->session_negotiate->ticket_len = 0; |
| 3606 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 3607 | if( ( ticket = mbedtls_calloc( 1, ticket_len ) ) == NULL ) |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3608 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 3609 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "ticket alloc failed" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 3610 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3611 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 3612 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3613 | } |
| 3614 | |
Manuel Pégourié-Gonnard | 000d5ae | 2014-09-10 21:52:12 +0200 | [diff] [blame] | 3615 | memcpy( ticket, msg + 6, ticket_len ); |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3616 | |
| 3617 | ssl->session_negotiate->ticket = ticket; |
| 3618 | ssl->session_negotiate->ticket_len = ticket_len; |
| 3619 | ssl->session_negotiate->ticket_lifetime = lifetime; |
| 3620 | |
| 3621 | /* |
| 3622 | * RFC 5077 section 3.4: |
| 3623 | * "If the client receives a session ticket from the server, then it |
| 3624 | * discards any Session ID that was sent in the ServerHello." |
| 3625 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3626 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) ); |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 3627 | ssl->session_negotiate->id_len = 0; |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3628 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3629 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) ); |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3630 | |
| 3631 | return( 0 ); |
| 3632 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3633 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3634 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3635 | /* |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3636 | * SSL handshake -- client side -- single step |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3637 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3638 | int mbedtls_ssl_handshake_client_step( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3639 | { |
| 3640 | int ret = 0; |
| 3641 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3642 | /* Change state now, so that it is right in mbedtls_ssl_read_record(), used |
Manuel Pégourié-Gonnard | cd32a50 | 2014-09-20 13:54:12 +0200 | [diff] [blame] | 3643 | * by DTLS for dropping out-of-sequence ChangeCipherSpec records */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3644 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 3645 | if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC && |
Manuel Pégourié-Gonnard | cd32a50 | 2014-09-20 13:54:12 +0200 | [diff] [blame] | 3646 | ssl->handshake->new_session_ticket != 0 ) |
| 3647 | { |
Jerry Yu | a357cf4 | 2022-07-12 05:36:45 +0000 | [diff] [blame] | 3648 | ssl->state = MBEDTLS_SSL_NEW_SESSION_TICKET; |
Manuel Pégourié-Gonnard | cd32a50 | 2014-09-20 13:54:12 +0200 | [diff] [blame] | 3649 | } |
| 3650 | #endif |
| 3651 | |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3652 | switch( ssl->state ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3653 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3654 | case MBEDTLS_SSL_HELLO_REQUEST: |
| 3655 | ssl->state = MBEDTLS_SSL_CLIENT_HELLO; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3656 | break; |
| 3657 | |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3658 | /* |
| 3659 | * ==> ClientHello |
| 3660 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3661 | case MBEDTLS_SSL_CLIENT_HELLO: |
Ronald Cron | 7320e64 | 2022-03-08 13:34:49 +0100 | [diff] [blame] | 3662 | ret = mbedtls_ssl_write_client_hello( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3663 | break; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3664 | |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3665 | /* |
| 3666 | * <== ServerHello |
| 3667 | * Certificate |
| 3668 | * ( ServerKeyExchange ) |
| 3669 | * ( CertificateRequest ) |
| 3670 | * ServerHelloDone |
| 3671 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3672 | case MBEDTLS_SSL_SERVER_HELLO: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3673 | ret = ssl_parse_server_hello( ssl ); |
| 3674 | break; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3675 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3676 | case MBEDTLS_SSL_SERVER_CERTIFICATE: |
| 3677 | ret = mbedtls_ssl_parse_certificate( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3678 | break; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3679 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3680 | case MBEDTLS_SSL_SERVER_KEY_EXCHANGE: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3681 | ret = ssl_parse_server_key_exchange( ssl ); |
| 3682 | break; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3683 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3684 | case MBEDTLS_SSL_CERTIFICATE_REQUEST: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3685 | ret = ssl_parse_certificate_request( ssl ); |
| 3686 | break; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3687 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3688 | case MBEDTLS_SSL_SERVER_HELLO_DONE: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3689 | ret = ssl_parse_server_hello_done( ssl ); |
| 3690 | break; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3691 | |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3692 | /* |
| 3693 | * ==> ( Certificate/Alert ) |
| 3694 | * ClientKeyExchange |
| 3695 | * ( CertificateVerify ) |
| 3696 | * ChangeCipherSpec |
| 3697 | * Finished |
| 3698 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3699 | case MBEDTLS_SSL_CLIENT_CERTIFICATE: |
| 3700 | ret = mbedtls_ssl_write_certificate( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3701 | break; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3702 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3703 | case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3704 | ret = ssl_write_client_key_exchange( ssl ); |
| 3705 | break; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3706 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3707 | case MBEDTLS_SSL_CERTIFICATE_VERIFY: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3708 | ret = ssl_write_certificate_verify( ssl ); |
| 3709 | break; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3710 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3711 | case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC: |
| 3712 | ret = mbedtls_ssl_write_change_cipher_spec( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3713 | break; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3714 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3715 | case MBEDTLS_SSL_CLIENT_FINISHED: |
| 3716 | ret = mbedtls_ssl_write_finished( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3717 | break; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3718 | |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3719 | /* |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3720 | * <== ( NewSessionTicket ) |
| 3721 | * ChangeCipherSpec |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3722 | * Finished |
| 3723 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3724 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Jerry Yu | a357cf4 | 2022-07-12 05:36:45 +0000 | [diff] [blame] | 3725 | case MBEDTLS_SSL_NEW_SESSION_TICKET: |
Manuel Pégourié-Gonnard | cd32a50 | 2014-09-20 13:54:12 +0200 | [diff] [blame] | 3726 | ret = ssl_parse_new_session_ticket( ssl ); |
| 3727 | break; |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 3728 | #endif |
Manuel Pégourié-Gonnard | cd32a50 | 2014-09-20 13:54:12 +0200 | [diff] [blame] | 3729 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3730 | case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC: |
| 3731 | ret = mbedtls_ssl_parse_change_cipher_spec( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3732 | break; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3733 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3734 | case MBEDTLS_SSL_SERVER_FINISHED: |
| 3735 | ret = mbedtls_ssl_parse_finished( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3736 | break; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3737 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3738 | case MBEDTLS_SSL_FLUSH_BUFFERS: |
| 3739 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) ); |
| 3740 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3741 | break; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3742 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3743 | case MBEDTLS_SSL_HANDSHAKE_WRAPUP: |
| 3744 | mbedtls_ssl_handshake_wrapup( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3745 | break; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3746 | |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3747 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3748 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) ); |
| 3749 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3750 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3751 | |
| 3752 | return( ret ); |
| 3753 | } |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 3754 | |
Jerry Yu | fb4b647 | 2022-01-27 15:03:26 +0800 | [diff] [blame] | 3755 | #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_2 */ |