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