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