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