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