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