| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* | 
|  | 2 | *  SSLv3/TLSv1 server-side functions | 
|  | 3 | * | 
| Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | *  Copyright The Mbed TLS Contributors | 
| Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | *  SPDX-License-Identifier: Apache-2.0 | 
|  | 6 | * | 
|  | 7 | *  Licensed under the Apache License, Version 2.0 (the "License"); you may | 
|  | 8 | *  not use this file except in compliance with the License. | 
|  | 9 | *  You may obtain a copy of the License at | 
|  | 10 | * | 
|  | 11 | *  http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 12 | * | 
|  | 13 | *  Unless required by applicable law or agreed to in writing, software | 
|  | 14 | *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | 
|  | 15 | *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 16 | *  See the License for the specific language governing permissions and | 
|  | 17 | *  limitations under the License. | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 18 | */ | 
|  | 19 |  | 
| Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 20 | #include "common.h" | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 21 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 22 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 23 |  | 
| SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 24 | #if defined(MBEDTLS_PLATFORM_C) | 
|  | 25 | #include "mbedtls/platform.h" | 
|  | 26 | #else | 
|  | 27 | #include <stdlib.h> | 
|  | 28 | #define mbedtls_calloc    calloc | 
|  | 29 | #define mbedtls_free      free | 
| SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 30 | #endif | 
|  | 31 |  | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 32 | #include "mbedtls/ssl.h" | 
| Manuel Pégourié-Gonnard | 5e94dde | 2015-05-26 11:57:05 +0200 | [diff] [blame] | 33 | #include "mbedtls/ssl_internal.h" | 
| Janos Follath | 73c616b | 2019-12-18 15:07:04 +0000 | [diff] [blame] | 34 | #include "mbedtls/debug.h" | 
|  | 35 | #include "mbedtls/error.h" | 
| Andres Amaya Garcia | 8491406 | 2018-04-24 08:40:46 -0500 | [diff] [blame] | 36 | #include "mbedtls/platform_util.h" | 
| Gabor Mezei | c0ae1cf | 2021-10-20 12:09:35 +0200 | [diff] [blame] | 37 | #include "constant_time_internal.h" | 
| Gabor Mezei | e24dea8 | 2021-10-19 12:22:25 +0200 | [diff] [blame] | 38 | #include "mbedtls/constant_time.h" | 
| Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 39 |  | 
|  | 40 | #include <string.h> | 
|  | 41 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 42 | #if defined(MBEDTLS_ECP_C) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 43 | #include "mbedtls/ecp.h" | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 44 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 45 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 46 | #if defined(MBEDTLS_HAVE_TIME) | 
| Simon Butcher | b5b6af2 | 2016-07-13 14:46:18 +0100 | [diff] [blame] | 47 | #include "mbedtls/platform_time.h" | 
| Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 48 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 49 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 50 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) | 
|  | 51 | int mbedtls_ssl_set_client_transport_id( mbedtls_ssl_context *ssl, | 
| Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 52 | const unsigned char *info, | 
|  | 53 | size_t ilen ) | 
|  | 54 | { | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 55 | if( ssl->conf->endpoint != MBEDTLS_SSL_IS_SERVER ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 56 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 57 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 58 | mbedtls_free( ssl->cli_id ); | 
| Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 59 |  | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 60 | if( ( ssl->cli_id = mbedtls_calloc( 1, ilen ) ) == NULL ) | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 61 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
| Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 62 |  | 
|  | 63 | memcpy( ssl->cli_id, info, ilen ); | 
|  | 64 | ssl->cli_id_len = ilen; | 
|  | 65 |  | 
|  | 66 | return( 0 ); | 
|  | 67 | } | 
| Manuel Pégourié-Gonnard | d485d19 | 2014-07-23 14:56:15 +0200 | [diff] [blame] | 68 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 69 | void mbedtls_ssl_conf_dtls_cookies( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 70 | mbedtls_ssl_cookie_write_t *f_cookie_write, | 
|  | 71 | mbedtls_ssl_cookie_check_t *f_cookie_check, | 
| Manuel Pégourié-Gonnard | d485d19 | 2014-07-23 14:56:15 +0200 | [diff] [blame] | 72 | void *p_cookie ) | 
|  | 73 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 74 | conf->f_cookie_write = f_cookie_write; | 
|  | 75 | conf->f_cookie_check = f_cookie_check; | 
|  | 76 | conf->p_cookie       = p_cookie; | 
| Manuel Pégourié-Gonnard | d485d19 | 2014-07-23 14:56:15 +0200 | [diff] [blame] | 77 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 78 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */ | 
| Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 79 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 80 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
| Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 81 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 82 | static int ssl_parse_servername_ext( mbedtls_ssl_context *ssl, | 
| Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 83 | const unsigned char *buf, | 
| Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 84 | size_t len ) | 
|  | 85 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 86 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 87 | size_t servername_list_size, hostname_len; | 
| Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 88 | const unsigned char *p; | 
| Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 89 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 90 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) ); | 
| Manuel Pégourié-Gonnard | 96ea2f2 | 2014-02-25 12:26:29 +0100 | [diff] [blame] | 91 |  | 
| Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 92 | if( len < 2 ) | 
|  | 93 | { | 
|  | 94 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
|  | 95 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 96 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
|  | 97 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
|  | 98 | } | 
| Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 99 | servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) ); | 
|  | 100 | if( servername_list_size + 2 != len ) | 
|  | 101 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 102 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 103 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 104 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 105 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 106 | } | 
|  | 107 |  | 
|  | 108 | p = buf + 2; | 
| Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 109 | while( servername_list_size > 2 ) | 
| Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 110 | { | 
|  | 111 | hostname_len = ( ( p[1] << 8 ) | p[2] ); | 
|  | 112 | if( hostname_len + 3 > servername_list_size ) | 
|  | 113 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 114 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 115 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 116 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 117 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 118 | } | 
|  | 119 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 120 | if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME ) | 
| Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 121 | { | 
| Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 122 | ret = ssl->conf->f_sni( ssl->conf->p_sni, | 
|  | 123 | ssl, p + 3, hostname_len ); | 
| Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 124 | if( ret != 0 ) | 
|  | 125 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 126 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret ); | 
|  | 127 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 128 | MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME ); | 
|  | 129 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 130 | } | 
| Paul Bakker | 81420ab | 2012-10-23 10:31:15 +0000 | [diff] [blame] | 131 | return( 0 ); | 
| Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 132 | } | 
|  | 133 |  | 
|  | 134 | servername_list_size -= hostname_len + 3; | 
| Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 135 | p += hostname_len + 3; | 
|  | 136 | } | 
|  | 137 |  | 
|  | 138 | if( servername_list_size != 0 ) | 
|  | 139 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 140 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 141 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 142 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 143 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 144 | } | 
|  | 145 |  | 
|  | 146 | return( 0 ); | 
|  | 147 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 148 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ | 
| Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 149 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 150 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) | 
| Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 151 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Hanno Becker | 845b946 | 2018-10-26 12:07:29 +0100 | [diff] [blame] | 152 | static int ssl_conf_has_psk_or_cb( mbedtls_ssl_config const *conf ) | 
|  | 153 | { | 
|  | 154 | if( conf->f_psk != NULL ) | 
|  | 155 | return( 1 ); | 
|  | 156 |  | 
|  | 157 | if( conf->psk_identity_len == 0 || conf->psk_identity == NULL ) | 
|  | 158 | return( 0 ); | 
|  | 159 |  | 
|  | 160 | if( conf->psk != NULL && conf->psk_len != 0 ) | 
|  | 161 | return( 1 ); | 
|  | 162 |  | 
|  | 163 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 164 | if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) | 
| Hanno Becker | 845b946 | 2018-10-26 12:07:29 +0100 | [diff] [blame] | 165 | return( 1 ); | 
|  | 166 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 167 |  | 
|  | 168 | return( 0 ); | 
|  | 169 | } | 
|  | 170 |  | 
|  | 171 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 172 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Hanno Becker | 845b946 | 2018-10-26 12:07:29 +0100 | [diff] [blame] | 173 | static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl ) | 
|  | 174 | { | 
|  | 175 | if( ssl->conf->f_psk != NULL ) | 
|  | 176 | { | 
|  | 177 | /* If we've used a callback to select the PSK, | 
|  | 178 | * the static configuration is irrelevant. */ | 
|  | 179 |  | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 180 | if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) ) | 
| Hanno Becker | 845b946 | 2018-10-26 12:07:29 +0100 | [diff] [blame] | 181 | return( 1 ); | 
|  | 182 |  | 
|  | 183 | return( 0 ); | 
|  | 184 | } | 
|  | 185 |  | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 186 | if( ! mbedtls_svc_key_id_is_null( ssl->conf->psk_opaque ) ) | 
| Hanno Becker | 845b946 | 2018-10-26 12:07:29 +0100 | [diff] [blame] | 187 | return( 1 ); | 
|  | 188 |  | 
|  | 189 | return( 0 ); | 
|  | 190 | } | 
|  | 191 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 192 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ | 
| Hanno Becker | 845b946 | 2018-10-26 12:07:29 +0100 | [diff] [blame] | 193 |  | 
| Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 194 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 195 | static int ssl_parse_renegotiation_info( mbedtls_ssl_context *ssl, | 
| Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 196 | const unsigned char *buf, | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 197 | size_t len ) | 
|  | 198 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 199 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 200 | if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ) | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 201 | { | 
|  | 202 | /* Check verify-data in constant-time. The length OTOH is no secret */ | 
|  | 203 | if( len    != 1 + ssl->verify_data_len || | 
|  | 204 | buf[0] !=     ssl->verify_data_len || | 
| Gabor Mezei | 18a4494 | 2021-10-20 11:59:27 +0200 | [diff] [blame] | 205 | mbedtls_ct_memcmp( buf + 1, ssl->peer_verify_data, | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 206 | ssl->verify_data_len ) != 0 ) | 
|  | 207 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 208 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) ); | 
| Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 209 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 210 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 211 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 212 | } | 
|  | 213 | } | 
|  | 214 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 215 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 216 | { | 
|  | 217 | if( len != 1 || buf[0] != 0x0 ) | 
|  | 218 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 219 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) ); | 
| Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 220 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 221 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 222 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 223 | } | 
|  | 224 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 225 | ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 226 | } | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 227 |  | 
|  | 228 | return( 0 ); | 
|  | 229 | } | 
|  | 230 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 231 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 232 | defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 233 |  | 
|  | 234 | /* | 
|  | 235 | * Status of the implementation of signature-algorithms extension: | 
|  | 236 | * | 
|  | 237 | * Currently, we are only considering the signature-algorithm extension | 
|  | 238 | * to pick a ciphersuite which allows us to send the ServerKeyExchange | 
|  | 239 | * message with a signature-hash combination that the user allows. | 
|  | 240 | * | 
|  | 241 | * We do *not* check whether all certificates in our certificate | 
|  | 242 | * chain are signed with an allowed signature-hash pair. | 
|  | 243 | * This needs to be done at a later stage. | 
|  | 244 | * | 
|  | 245 | */ | 
| Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 246 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 247 | static int ssl_parse_signature_algorithms_ext( mbedtls_ssl_context *ssl, | 
| Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 248 | const unsigned char *buf, | 
|  | 249 | size_t len ) | 
|  | 250 | { | 
|  | 251 | size_t sig_alg_list_size; | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 252 |  | 
| Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 253 | const unsigned char *p; | 
| Manuel Pégourié-Gonnard | 08e81e0 | 2014-07-08 12:56:25 +0200 | [diff] [blame] | 254 | const unsigned char *end = buf + len; | 
| Manuel Pégourié-Gonnard | 08e81e0 | 2014-07-08 12:56:25 +0200 | [diff] [blame] | 255 |  | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 256 | mbedtls_md_type_t md_cur; | 
|  | 257 | mbedtls_pk_type_t sig_cur; | 
| Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 258 |  | 
| Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 259 | if ( len < 2 ) { | 
|  | 260 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
|  | 261 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 262 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
|  | 263 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
|  | 264 | } | 
| Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 265 | sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) ); | 
|  | 266 | if( sig_alg_list_size + 2 != len || | 
| Manuel Pégourié-Gonnard | 08e81e0 | 2014-07-08 12:56:25 +0200 | [diff] [blame] | 267 | sig_alg_list_size % 2 != 0 ) | 
| Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 268 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 269 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 270 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 271 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 272 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 273 | } | 
|  | 274 |  | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 275 | /* Currently we only guarantee signing the ServerKeyExchange message according | 
|  | 276 | * to the constraints specified in this extension (see above), so it suffices | 
|  | 277 | * to remember only one suitable hash for each possible signature algorithm. | 
| Manuel Pégourié-Gonnard | 08e81e0 | 2014-07-08 12:56:25 +0200 | [diff] [blame] | 278 | * | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 279 | * This will change when we also consider certificate signatures, | 
|  | 280 | * in which case we will need to remember the whole signature-hash | 
|  | 281 | * pair list from the extension. | 
| Manuel Pégourié-Gonnard | 08e81e0 | 2014-07-08 12:56:25 +0200 | [diff] [blame] | 282 | */ | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 283 |  | 
|  | 284 | for( p = buf + 2; p < end; p += 2 ) | 
|  | 285 | { | 
|  | 286 | /* Silently ignore unknown signature or hash algorithms. */ | 
|  | 287 |  | 
|  | 288 | if( ( sig_cur = mbedtls_ssl_pk_alg_from_sig( p[1] ) ) == MBEDTLS_PK_NONE ) | 
|  | 289 | { | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 290 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext" | 
|  | 291 | " unknown sig alg encoding %d", p[1] ) ); | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 292 | continue; | 
|  | 293 | } | 
|  | 294 |  | 
|  | 295 | /* Check if we support the hash the user proposes */ | 
|  | 296 | md_cur = mbedtls_ssl_md_alg_from_hash( p[0] ); | 
|  | 297 | if( md_cur == MBEDTLS_MD_NONE ) | 
|  | 298 | { | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 299 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext:" | 
|  | 300 | " unknown hash alg encoding %d", p[0] ) ); | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 301 | continue; | 
|  | 302 | } | 
|  | 303 |  | 
|  | 304 | if( mbedtls_ssl_check_sig_hash( ssl, md_cur ) == 0 ) | 
|  | 305 | { | 
|  | 306 | mbedtls_ssl_sig_hash_set_add( &ssl->handshake->hash_algs, sig_cur, md_cur ); | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 307 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext:" | 
| Paul Elliott | 9f35211 | 2020-12-09 14:55:45 +0000 | [diff] [blame] | 308 | " match sig %u and hash %u", | 
| Paul Elliott | 3891caf | 2020-12-17 18:42:40 +0000 | [diff] [blame] | 309 | (unsigned) sig_cur, (unsigned) md_cur ) ); | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 310 | } | 
|  | 311 | else | 
|  | 312 | { | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 313 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: " | 
| Paul Elliott | 3891caf | 2020-12-17 18:42:40 +0000 | [diff] [blame] | 314 | "hash alg %u not supported", (unsigned) md_cur ) ); | 
| Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 315 | } | 
| Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 316 | } | 
|  | 317 |  | 
| Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 318 | return( 0 ); | 
|  | 319 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 320 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 321 | MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ | 
| Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 322 |  | 
| Robert Cragie | 136884c | 2015-10-02 13:34:31 +0100 | [diff] [blame] | 323 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ | 
| Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 324 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 325 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 326 | static int ssl_parse_supported_elliptic_curves( mbedtls_ssl_context *ssl, | 
| Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 327 | const unsigned char *buf, | 
|  | 328 | size_t len ) | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 329 | { | 
| Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 330 | size_t list_size, our_size; | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 331 | const unsigned char *p; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 332 | const mbedtls_ecp_curve_info *curve_info, **curves; | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 333 |  | 
| Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 334 | if ( len < 2 ) { | 
|  | 335 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
|  | 336 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 337 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
|  | 338 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
|  | 339 | } | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 340 | list_size = ( ( buf[0] << 8 ) | ( buf[1] ) ); | 
|  | 341 | if( list_size + 2 != len || | 
|  | 342 | list_size % 2 != 0 ) | 
|  | 343 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 344 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 345 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 346 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 347 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 348 | } | 
|  | 349 |  | 
| Manuel Pégourié-Gonnard | 43c3b28 | 2014-10-17 12:42:11 +0200 | [diff] [blame] | 350 | /* Should never happen unless client duplicates the extension */ | 
|  | 351 | if( ssl->handshake->curves != NULL ) | 
|  | 352 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 353 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 354 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 355 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 356 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Manuel Pégourié-Gonnard | 43c3b28 | 2014-10-17 12:42:11 +0200 | [diff] [blame] | 357 | } | 
|  | 358 |  | 
| Manuel Pégourié-Gonnard | c3f6b62c | 2014-02-06 10:13:09 +0100 | [diff] [blame] | 359 | /* Don't allow our peer to make us allocate too much memory, | 
| Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 360 | * and leave room for a final 0 */ | 
|  | 361 | our_size = list_size / 2 + 1; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 362 | if( our_size > MBEDTLS_ECP_DP_MAX ) | 
|  | 363 | our_size = MBEDTLS_ECP_DP_MAX; | 
| Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 364 |  | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 365 | if( ( curves = mbedtls_calloc( our_size, sizeof( *curves ) ) ) == NULL ) | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 366 | { | 
|  | 367 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 368 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 369 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 370 | } | 
| Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 371 |  | 
| Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 372 | ssl->handshake->curves = curves; | 
|  | 373 |  | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 374 | p = buf + 2; | 
| Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 375 | while( list_size > 0 && our_size > 1 ) | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 376 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 377 | curve_info = mbedtls_ecp_curve_info_from_tls_id( ( p[0] << 8 ) | p[1] ); | 
| Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 378 |  | 
| Manuel Pégourié-Gonnard | f24b4a7 | 2013-09-23 18:14:50 +0200 | [diff] [blame] | 379 | if( curve_info != NULL ) | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 380 | { | 
| Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 381 | *curves++ = curve_info; | 
|  | 382 | our_size--; | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 383 | } | 
|  | 384 |  | 
|  | 385 | list_size -= 2; | 
|  | 386 | p += 2; | 
|  | 387 | } | 
|  | 388 |  | 
|  | 389 | return( 0 ); | 
|  | 390 | } | 
|  | 391 |  | 
| Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 392 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 393 | static int ssl_parse_supported_point_formats( mbedtls_ssl_context *ssl, | 
| Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 394 | const unsigned char *buf, | 
|  | 395 | size_t len ) | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 396 | { | 
|  | 397 | size_t list_size; | 
|  | 398 | const unsigned char *p; | 
|  | 399 |  | 
| Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 400 | if( len == 0 || (size_t)( buf[0] + 1 ) != len ) | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 401 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 402 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 403 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 404 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 405 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 406 | } | 
| Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 407 | list_size = buf[0]; | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 408 |  | 
| Manuel Pégourié-Gonnard | c1b46d0 | 2015-09-16 11:18:32 +0200 | [diff] [blame] | 409 | p = buf + 1; | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 410 | while( list_size > 0 ) | 
|  | 411 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 412 | if( p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED || | 
|  | 413 | p[0] == MBEDTLS_ECP_PF_COMPRESSED ) | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 414 | { | 
| Robert Cragie | 136884c | 2015-10-02 13:34:31 +0100 | [diff] [blame] | 415 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) | 
| Manuel Pégourié-Gonnard | 5734b2d | 2013-08-15 19:04:02 +0200 | [diff] [blame] | 416 | ssl->handshake->ecdh_ctx.point_format = p[0]; | 
| Robert Cragie | 136884c | 2015-10-02 13:34:31 +0100 | [diff] [blame] | 417 | #endif | 
| Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 418 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Robert Cragie | 136884c | 2015-10-02 13:34:31 +0100 | [diff] [blame] | 419 | ssl->handshake->ecjpake_ctx.point_format = p[0]; | 
|  | 420 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 421 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) ); | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 422 | return( 0 ); | 
|  | 423 | } | 
|  | 424 |  | 
|  | 425 | list_size--; | 
|  | 426 | p++; | 
|  | 427 | } | 
|  | 428 |  | 
|  | 429 | return( 0 ); | 
|  | 430 | } | 
| Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 431 | #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || | 
|  | 432 | MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 433 |  | 
| Manuel Pégourié-Gonnard | bf57be6 | 2015-09-16 15:04:01 +0200 | [diff] [blame] | 434 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 435 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Manuel Pégourié-Gonnard | bf57be6 | 2015-09-16 15:04:01 +0200 | [diff] [blame] | 436 | static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl, | 
|  | 437 | const unsigned char *buf, | 
|  | 438 | size_t len ) | 
|  | 439 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 440 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | bf57be6 | 2015-09-16 15:04:01 +0200 | [diff] [blame] | 441 |  | 
|  | 442 | if( mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 ) | 
|  | 443 | { | 
|  | 444 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) ); | 
|  | 445 | return( 0 ); | 
|  | 446 | } | 
|  | 447 |  | 
|  | 448 | if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx, | 
|  | 449 | buf, len ) ) != 0 ) | 
|  | 450 | { | 
|  | 451 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 452 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 453 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); | 
| Manuel Pégourié-Gonnard | bf57be6 | 2015-09-16 15:04:01 +0200 | [diff] [blame] | 454 | return( ret ); | 
|  | 455 | } | 
|  | 456 |  | 
|  | 457 | /* Only mark the extension as OK when we're sure it is */ | 
|  | 458 | ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK; | 
|  | 459 |  | 
|  | 460 | return( 0 ); | 
|  | 461 | } | 
|  | 462 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ | 
|  | 463 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 464 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
| Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 465 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 466 | static int ssl_parse_max_fragment_length_ext( mbedtls_ssl_context *ssl, | 
| Manuel Pégourié-Gonnard | 48f8d0d | 2013-07-17 10:25:37 +0200 | [diff] [blame] | 467 | const unsigned char *buf, | 
|  | 468 | size_t len ) | 
|  | 469 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 470 | if( len != 1 || buf[0] >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ) | 
| Manuel Pégourié-Gonnard | 48f8d0d | 2013-07-17 10:25:37 +0200 | [diff] [blame] | 471 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 472 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 473 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 474 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 475 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Manuel Pégourié-Gonnard | 48f8d0d | 2013-07-17 10:25:37 +0200 | [diff] [blame] | 476 | } | 
|  | 477 |  | 
| Manuel Pégourié-Gonnard | ed4af8b | 2013-07-18 14:07:09 +0200 | [diff] [blame] | 478 | ssl->session_negotiate->mfl_code = buf[0]; | 
|  | 479 |  | 
| Manuel Pégourié-Gonnard | 48f8d0d | 2013-07-17 10:25:37 +0200 | [diff] [blame] | 480 | return( 0 ); | 
|  | 481 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 482 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ | 
| Manuel Pégourié-Gonnard | 48f8d0d | 2013-07-17 10:25:37 +0200 | [diff] [blame] | 483 |  | 
| Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 484 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
| Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 485 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Hanno Becker | 89dcc88 | 2019-04-26 13:56:39 +0100 | [diff] [blame] | 486 | static int ssl_parse_cid_ext( mbedtls_ssl_context *ssl, | 
|  | 487 | const unsigned char *buf, | 
|  | 488 | size_t len ) | 
|  | 489 | { | 
|  | 490 | size_t peer_cid_len; | 
|  | 491 |  | 
|  | 492 | /* CID extension only makes sense in DTLS */ | 
|  | 493 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
|  | 494 | { | 
|  | 495 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
|  | 496 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 497 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); | 
|  | 498 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
|  | 499 | } | 
|  | 500 |  | 
|  | 501 | /* | 
| Hanno Becker | ebcc913 | 2019-05-15 10:26:32 +0100 | [diff] [blame] | 502 | * Quoting draft-ietf-tls-dtls-connection-id-05 | 
|  | 503 | * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05 | 
| Hanno Becker | 89dcc88 | 2019-04-26 13:56:39 +0100 | [diff] [blame] | 504 | * | 
|  | 505 | *   struct { | 
|  | 506 | *      opaque cid<0..2^8-1>; | 
|  | 507 | *   } ConnectionId; | 
|  | 508 | */ | 
|  | 509 |  | 
|  | 510 | if( len < 1 ) | 
|  | 511 | { | 
|  | 512 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
|  | 513 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 514 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); | 
|  | 515 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
|  | 516 | } | 
|  | 517 |  | 
|  | 518 | peer_cid_len = *buf++; | 
|  | 519 | len--; | 
|  | 520 |  | 
|  | 521 | if( len != peer_cid_len ) | 
|  | 522 | { | 
|  | 523 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
|  | 524 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 525 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); | 
|  | 526 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
|  | 527 | } | 
|  | 528 |  | 
|  | 529 | /* Ignore CID if the user has disabled its use. */ | 
|  | 530 | if( ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED ) | 
|  | 531 | { | 
|  | 532 | /* Leave ssl->handshake->cid_in_use in its default | 
|  | 533 | * value of MBEDTLS_SSL_CID_DISABLED. */ | 
|  | 534 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Client sent CID extension, but CID disabled" ) ); | 
|  | 535 | return( 0 ); | 
|  | 536 | } | 
|  | 537 |  | 
|  | 538 | if( peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX ) | 
|  | 539 | { | 
|  | 540 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
|  | 541 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 542 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); | 
|  | 543 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
|  | 544 | } | 
|  | 545 |  | 
| Hanno Becker | 08556bf | 2019-05-03 12:43:44 +0100 | [diff] [blame] | 546 | ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED; | 
| Hanno Becker | 89dcc88 | 2019-04-26 13:56:39 +0100 | [diff] [blame] | 547 | ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len; | 
|  | 548 | memcpy( ssl->handshake->peer_cid, buf, peer_cid_len ); | 
|  | 549 |  | 
|  | 550 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use of CID extension negotiated" ) ); | 
|  | 551 | MBEDTLS_SSL_DEBUG_BUF( 3, "Client CID", buf, peer_cid_len ); | 
|  | 552 |  | 
| Hanno Becker | 89dcc88 | 2019-04-26 13:56:39 +0100 | [diff] [blame] | 553 | return( 0 ); | 
|  | 554 | } | 
| Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 555 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
| Hanno Becker | 89dcc88 | 2019-04-26 13:56:39 +0100 | [diff] [blame] | 556 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 557 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) | 
| Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 558 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 559 | static int ssl_parse_truncated_hmac_ext( mbedtls_ssl_context *ssl, | 
| Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 560 | const unsigned char *buf, | 
|  | 561 | size_t len ) | 
|  | 562 | { | 
|  | 563 | if( len != 0 ) | 
|  | 564 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 565 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 566 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 567 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 568 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 569 | } | 
|  | 570 |  | 
|  | 571 | ((void) buf); | 
|  | 572 |  | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 573 | if( ssl->conf->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 574 | ssl->session_negotiate->trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; | 
| Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 575 |  | 
|  | 576 | return( 0 ); | 
|  | 577 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 578 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ | 
| Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 579 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 580 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
| Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 581 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 582 | static int ssl_parse_encrypt_then_mac_ext( mbedtls_ssl_context *ssl, | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 583 | const unsigned char *buf, | 
|  | 584 | size_t len ) | 
|  | 585 | { | 
|  | 586 | if( len != 0 ) | 
|  | 587 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 588 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 589 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 590 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 591 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 592 | } | 
|  | 593 |  | 
|  | 594 | ((void) buf); | 
|  | 595 |  | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 596 | if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED && | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 597 | ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 ) | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 598 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 599 | ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 600 | } | 
|  | 601 |  | 
|  | 602 | return( 0 ); | 
|  | 603 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 604 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 605 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 606 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) | 
| Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 607 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 608 | static int ssl_parse_extended_ms_ext( mbedtls_ssl_context *ssl, | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 609 | const unsigned char *buf, | 
|  | 610 | size_t len ) | 
|  | 611 | { | 
|  | 612 | if( len != 0 ) | 
|  | 613 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 614 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 615 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 616 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 617 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 618 | } | 
|  | 619 |  | 
|  | 620 | ((void) buf); | 
|  | 621 |  | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 622 | if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED && | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 623 | ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 ) | 
| Manuel Pégourié-Gonnard | b575b54 | 2014-10-24 15:12:31 +0200 | [diff] [blame] | 624 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 625 | ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; | 
| Manuel Pégourié-Gonnard | b575b54 | 2014-10-24 15:12:31 +0200 | [diff] [blame] | 626 | } | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 627 |  | 
|  | 628 | return( 0 ); | 
|  | 629 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 630 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 631 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 632 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | 
| Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 633 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 634 | static int ssl_parse_session_ticket_ext( mbedtls_ssl_context *ssl, | 
| Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 635 | unsigned char *buf, | 
| Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 636 | size_t len ) | 
|  | 637 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 638 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 639 | mbedtls_ssl_session session; | 
| Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 640 |  | 
| Manuel Pégourié-Gonnard | bae389b | 2015-06-24 10:45:58 +0200 | [diff] [blame] | 641 | mbedtls_ssl_session_init( &session ); | 
|  | 642 |  | 
| Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 643 | if( ssl->conf->f_ticket_parse == NULL || | 
|  | 644 | ssl->conf->f_ticket_write == NULL ) | 
|  | 645 | { | 
| Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 646 | return( 0 ); | 
| Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 647 | } | 
| Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 648 |  | 
| Manuel Pégourié-Gonnard | 306827e | 2013-08-02 18:05:14 +0200 | [diff] [blame] | 649 | /* Remember the client asked us to send a new ticket */ | 
| Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 650 | ssl->handshake->new_session_ticket = 1; | 
|  | 651 |  | 
| Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 652 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %" MBEDTLS_PRINTF_SIZET, len ) ); | 
| Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 653 |  | 
| Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 654 | if( len == 0 ) | 
|  | 655 | return( 0 ); | 
|  | 656 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 657 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 658 | if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ) | 
| Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 659 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 660 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) ); | 
| Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 661 | return( 0 ); | 
|  | 662 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 663 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ | 
| Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 664 |  | 
|  | 665 | /* | 
| Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 666 | * Failures are ok: just ignore the ticket and proceed. | 
|  | 667 | */ | 
| Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 668 | if( ( ret = ssl->conf->f_ticket_parse( ssl->conf->p_ticket, &session, | 
|  | 669 | buf, len ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 670 | { | 
| Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 671 | mbedtls_ssl_session_free( &session ); | 
| Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 672 |  | 
|  | 673 | if( ret == MBEDTLS_ERR_SSL_INVALID_MAC ) | 
|  | 674 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket is not authentic" ) ); | 
|  | 675 | else if( ret == MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED ) | 
|  | 676 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket is expired" ) ); | 
|  | 677 | else | 
|  | 678 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_ticket_parse", ret ); | 
|  | 679 |  | 
| Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 680 | return( 0 ); | 
| Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 681 | } | 
| Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 682 |  | 
| Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 683 | /* | 
|  | 684 | * Keep the session ID sent by the client, since we MUST send it back to | 
|  | 685 | * inform them we're accepting the ticket  (RFC 5077 section 3.4) | 
|  | 686 | */ | 
| Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 687 | session.id_len = ssl->session_negotiate->id_len; | 
|  | 688 | memcpy( &session.id, ssl->session_negotiate->id, session.id_len ); | 
| Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 689 |  | 
|  | 690 | mbedtls_ssl_session_free( ssl->session_negotiate ); | 
|  | 691 | memcpy( ssl->session_negotiate, &session, sizeof( mbedtls_ssl_session ) ); | 
|  | 692 |  | 
|  | 693 | /* Zeroize instead of free as we copied the content */ | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 694 | mbedtls_platform_zeroize( &session, sizeof( mbedtls_ssl_session ) ); | 
| Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 695 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 696 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) ); | 
| Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 697 |  | 
| Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 698 | ssl->handshake->resume = 1; | 
| Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 699 |  | 
| Manuel Pégourié-Gonnard | 306827e | 2013-08-02 18:05:14 +0200 | [diff] [blame] | 700 | /* Don't send a new ticket after all, this one is OK */ | 
|  | 701 | ssl->handshake->new_session_ticket = 0; | 
|  | 702 |  | 
| Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 703 | return( 0 ); | 
|  | 704 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 705 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ | 
| Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 706 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 707 | #if defined(MBEDTLS_SSL_ALPN) | 
| Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 708 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 709 | static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl, | 
| Manuel Pégourié-Gonnard | 14beb08 | 2014-07-08 13:50:35 +0200 | [diff] [blame] | 710 | const unsigned char *buf, size_t len ) | 
| Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 711 | { | 
| Paul Bakker | 14b16c6 | 2014-05-28 11:33:54 +0200 | [diff] [blame] | 712 | size_t list_len, cur_len, ours_len; | 
| Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 713 | const unsigned char *theirs, *start, *end; | 
|  | 714 | const char **ours; | 
|  | 715 |  | 
|  | 716 | /* If ALPN not configured, just ignore the extension */ | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 717 | if( ssl->conf->alpn_list == NULL ) | 
| Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 718 | return( 0 ); | 
|  | 719 |  | 
|  | 720 | /* | 
|  | 721 | * opaque ProtocolName<1..2^8-1>; | 
|  | 722 | * | 
|  | 723 | * struct { | 
|  | 724 | *     ProtocolName protocol_name_list<2..2^16-1> | 
|  | 725 | * } ProtocolNameList; | 
|  | 726 | */ | 
|  | 727 |  | 
|  | 728 | /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */ | 
|  | 729 | if( len < 4 ) | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 730 | { | 
|  | 731 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 732 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 733 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 734 | } | 
| Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 735 |  | 
|  | 736 | list_len = ( buf[0] << 8 ) | buf[1]; | 
|  | 737 | if( list_len != len - 2 ) | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 738 | { | 
|  | 739 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 740 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 741 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 742 | } | 
| Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 743 |  | 
|  | 744 | /* | 
| Manuel Pégourié-Gonnard | 239987f | 2018-01-09 10:43:43 +0100 | [diff] [blame] | 745 | * Validate peer's list (lengths) | 
| Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 746 | */ | 
|  | 747 | start = buf + 2; | 
|  | 748 | end = buf + len; | 
| Manuel Pégourié-Gonnard | 239987f | 2018-01-09 10:43:43 +0100 | [diff] [blame] | 749 | for( theirs = start; theirs != end; theirs += cur_len ) | 
|  | 750 | { | 
|  | 751 | cur_len = *theirs++; | 
|  | 752 |  | 
|  | 753 | /* Current identifier must fit in list */ | 
|  | 754 | if( cur_len > (size_t)( end - theirs ) ) | 
|  | 755 | { | 
|  | 756 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 757 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
|  | 758 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
|  | 759 | } | 
|  | 760 |  | 
|  | 761 | /* Empty strings MUST NOT be included */ | 
|  | 762 | if( cur_len == 0 ) | 
|  | 763 | { | 
|  | 764 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 765 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); | 
|  | 766 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
|  | 767 | } | 
|  | 768 | } | 
|  | 769 |  | 
|  | 770 | /* | 
|  | 771 | * Use our order of preference | 
|  | 772 | */ | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 773 | for( ours = ssl->conf->alpn_list; *ours != NULL; ours++ ) | 
| Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 774 | { | 
| Paul Bakker | 14b16c6 | 2014-05-28 11:33:54 +0200 | [diff] [blame] | 775 | ours_len = strlen( *ours ); | 
| Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 776 | for( theirs = start; theirs != end; theirs += cur_len ) | 
|  | 777 | { | 
| Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 778 | cur_len = *theirs++; | 
|  | 779 |  | 
| Paul Bakker | 14b16c6 | 2014-05-28 11:33:54 +0200 | [diff] [blame] | 780 | if( cur_len == ours_len && | 
| Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 781 | memcmp( theirs, *ours, cur_len ) == 0 ) | 
|  | 782 | { | 
|  | 783 | ssl->alpn_chosen = *ours; | 
|  | 784 | return( 0 ); | 
|  | 785 | } | 
|  | 786 | } | 
|  | 787 | } | 
|  | 788 |  | 
|  | 789 | /* If we get there, no match was found */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 790 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 791 | MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL ); | 
|  | 792 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 793 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 794 | #endif /* MBEDTLS_SSL_ALPN */ | 
| Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 795 |  | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 796 | #if defined(MBEDTLS_SSL_DTLS_SRTP) | 
| Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 797 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 798 | static int ssl_parse_use_srtp_ext( mbedtls_ssl_context *ssl, | 
| Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 799 | const unsigned char *buf, | 
|  | 800 | size_t len ) | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 801 | { | 
| Johan Pascal | 43f9490 | 2020-09-22 12:25:52 +0200 | [diff] [blame] | 802 | mbedtls_ssl_srtp_profile client_protection = MBEDTLS_TLS_SRTP_UNSET; | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 803 | size_t i,j; | 
| Johan Pascal | f6417ec | 2020-09-22 15:15:19 +0200 | [diff] [blame] | 804 | size_t profile_length; | 
|  | 805 | uint16_t mki_length; | 
| Ron Eldor | 313d7b5 | 2018-12-10 14:56:21 +0200 | [diff] [blame] | 806 | /*! 2 bytes for profile length and 1 byte for mki len */ | 
|  | 807 | const size_t size_of_lengths = 3; | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 808 |  | 
|  | 809 | /* If use_srtp is not configured, just ignore the extension */ | 
| Johan Pascal | c3ccd98 | 2020-10-28 17:18:18 +0100 | [diff] [blame] | 810 | if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) || | 
|  | 811 | ( ssl->conf->dtls_srtp_profile_list == NULL ) || | 
|  | 812 | ( ssl->conf->dtls_srtp_profile_list_len == 0 ) ) | 
| Johan Pascal | 8526957 | 2020-08-25 10:01:54 +0200 | [diff] [blame] | 813 | { | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 814 | return( 0 ); | 
| Johan Pascal | 8526957 | 2020-08-25 10:01:54 +0200 | [diff] [blame] | 815 | } | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 816 |  | 
|  | 817 | /* RFC5764 section 4.1.1 | 
|  | 818 | * uint8 SRTPProtectionProfile[2]; | 
|  | 819 | * | 
|  | 820 | * struct { | 
|  | 821 | *   SRTPProtectionProfiles SRTPProtectionProfiles; | 
|  | 822 | *   opaque srtp_mki<0..255>; | 
|  | 823 | * } UseSRTPData; | 
|  | 824 |  | 
|  | 825 | * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>; | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 826 | */ | 
|  | 827 |  | 
| Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 828 | /* | 
|  | 829 | * Min length is 5: at least one protection profile(2 bytes) | 
|  | 830 | *                  and length(2 bytes) + srtp_mki length(1 byte) | 
| Johan Pascal | 042d456 | 2020-08-25 12:14:02 +0200 | [diff] [blame] | 831 | * Check here that we have at least 2 bytes of protection profiles length | 
| Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 832 | * and one of srtp_mki length | 
| Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 833 | */ | 
| Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 834 | if( len < size_of_lengths ) | 
| Ron Eldor | 313d7b5 | 2018-12-10 14:56:21 +0200 | [diff] [blame] | 835 | { | 
|  | 836 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 837 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 838 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Ron Eldor | 313d7b5 | 2018-12-10 14:56:21 +0200 | [diff] [blame] | 839 | } | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 840 |  | 
| Johan Pascal | 43f9490 | 2020-09-22 12:25:52 +0200 | [diff] [blame] | 841 | ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET; | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 842 |  | 
| Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 843 | /* first 2 bytes are protection profile length(in bytes) */ | 
|  | 844 | profile_length = ( buf[0] << 8 ) | buf[1]; | 
| Johan Pascal | 042d456 | 2020-08-25 12:14:02 +0200 | [diff] [blame] | 845 | buf += 2; | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 846 |  | 
| Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 847 | /* The profile length cannot be bigger than input buffer size - lengths fields */ | 
|  | 848 | if( profile_length > len - size_of_lengths || | 
| Johan Pascal | 042d456 | 2020-08-25 12:14:02 +0200 | [diff] [blame] | 849 | profile_length % 2 != 0 ) /* profiles are 2 bytes long, so the length must be even */ | 
| Ron Eldor | 313d7b5 | 2018-12-10 14:56:21 +0200 | [diff] [blame] | 850 | { | 
|  | 851 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 852 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
|  | 853 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
|  | 854 | } | 
| Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 855 | /* | 
|  | 856 | * parse the extension list values are defined in | 
|  | 857 | * http://www.iana.org/assignments/srtp-protection/srtp-protection.xhtml | 
|  | 858 | */ | 
| Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 859 | for( j = 0; j < profile_length; j += 2 ) | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 860 | { | 
| Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 861 | uint16_t protection_profile_value = buf[j] << 8 | buf[j + 1]; | 
| Johan Pascal | 43f9490 | 2020-09-22 12:25:52 +0200 | [diff] [blame] | 862 | client_protection = mbedtls_ssl_check_srtp_profile_value( protection_profile_value ); | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 863 |  | 
| Johan Pascal | 43f9490 | 2020-09-22 12:25:52 +0200 | [diff] [blame] | 864 | if( client_protection != MBEDTLS_TLS_SRTP_UNSET ) | 
| Ron Eldor | b465539 | 2018-07-05 18:25:39 +0300 | [diff] [blame] | 865 | { | 
| Johan Pascal | 43f9490 | 2020-09-22 12:25:52 +0200 | [diff] [blame] | 866 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found srtp profile: %s", | 
|  | 867 | mbedtls_ssl_get_srtp_profile_as_string( | 
|  | 868 | client_protection ) ) ); | 
| Ron Eldor | b465539 | 2018-07-05 18:25:39 +0300 | [diff] [blame] | 869 | } | 
| Johan Pascal | 8526957 | 2020-08-25 10:01:54 +0200 | [diff] [blame] | 870 | else | 
|  | 871 | { | 
|  | 872 | continue; | 
|  | 873 | } | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 874 | /* check if suggested profile is in our list */ | 
| Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 875 | for( i = 0; i < ssl->conf->dtls_srtp_profile_list_len; i++) | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 876 | { | 
|  | 877 | if( client_protection == ssl->conf->dtls_srtp_profile_list[i] ) | 
|  | 878 | { | 
| Ron Eldor | 3adb992 | 2017-12-21 10:15:08 +0200 | [diff] [blame] | 879 | ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i]; | 
| Johan Pascal | 43f9490 | 2020-09-22 12:25:52 +0200 | [diff] [blame] | 880 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected srtp profile: %s", | 
|  | 881 | mbedtls_ssl_get_srtp_profile_as_string( | 
|  | 882 | client_protection ) ) ); | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 883 | break; | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 884 | } | 
|  | 885 | } | 
| Johan Pascal | 43f9490 | 2020-09-22 12:25:52 +0200 | [diff] [blame] | 886 | if( ssl->dtls_srtp_info.chosen_dtls_srtp_profile != MBEDTLS_TLS_SRTP_UNSET ) | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 887 | break; | 
|  | 888 | } | 
| Johan Pascal | 042d456 | 2020-08-25 12:14:02 +0200 | [diff] [blame] | 889 | buf += profile_length; /* buf points to the mki length */ | 
|  | 890 | mki_length = *buf; | 
|  | 891 | buf++; | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 892 |  | 
| Johan Pascal | 042d456 | 2020-08-25 12:14:02 +0200 | [diff] [blame] | 893 | if( mki_length > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH || | 
|  | 894 | mki_length + profile_length + size_of_lengths != len ) | 
|  | 895 | { | 
|  | 896 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 897 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
|  | 898 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
|  | 899 | } | 
|  | 900 |  | 
|  | 901 | /* Parse the mki only if present and mki is supported locally */ | 
|  | 902 | if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED && | 
|  | 903 | mki_length > 0 ) | 
|  | 904 | { | 
|  | 905 | ssl->dtls_srtp_info.mki_len = mki_length; | 
|  | 906 |  | 
| Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 907 | memcpy( ssl->dtls_srtp_info.mki_value, buf, mki_length ); | 
| Ron Eldor | b465539 | 2018-07-05 18:25:39 +0300 | [diff] [blame] | 908 |  | 
| Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 909 | MBEDTLS_SSL_DEBUG_BUF( 3, "using mki",  ssl->dtls_srtp_info.mki_value, | 
|  | 910 | ssl->dtls_srtp_info.mki_len ); | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 911 | } | 
|  | 912 |  | 
| Johan Pascal | 042d456 | 2020-08-25 12:14:02 +0200 | [diff] [blame] | 913 | return( 0 ); | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 914 | } | 
|  | 915 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ | 
|  | 916 |  | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 917 | /* | 
|  | 918 | * Auxiliary functions for ServerHello parsing and related actions | 
|  | 919 | */ | 
|  | 920 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 921 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 922 | /* | 
| Manuel Pégourié-Gonnard | 6458e3b | 2015-01-08 14:16:56 +0100 | [diff] [blame] | 923 | * Return 0 if the given key uses one of the acceptable curves, -1 otherwise | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 924 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 925 | #if defined(MBEDTLS_ECDSA_C) | 
| Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 926 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 927 | static int ssl_check_key_curve( mbedtls_pk_context *pk, | 
|  | 928 | const mbedtls_ecp_curve_info **curves ) | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 929 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 930 | const mbedtls_ecp_curve_info **crv = curves; | 
|  | 931 | mbedtls_ecp_group_id grp_id = mbedtls_pk_ec( *pk )->grp.id; | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 932 |  | 
|  | 933 | while( *crv != NULL ) | 
|  | 934 | { | 
|  | 935 | if( (*crv)->grp_id == grp_id ) | 
| Manuel Pégourié-Gonnard | 6458e3b | 2015-01-08 14:16:56 +0100 | [diff] [blame] | 936 | return( 0 ); | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 937 | crv++; | 
|  | 938 | } | 
|  | 939 |  | 
| Manuel Pégourié-Gonnard | 6458e3b | 2015-01-08 14:16:56 +0100 | [diff] [blame] | 940 | return( -1 ); | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 941 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 942 | #endif /* MBEDTLS_ECDSA_C */ | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 943 |  | 
|  | 944 | /* | 
|  | 945 | * Try picking a certificate for this ciphersuite, | 
|  | 946 | * return 0 on success and -1 on failure. | 
|  | 947 | */ | 
| Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 948 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 949 | static int ssl_pick_cert( mbedtls_ssl_context *ssl, | 
|  | 950 | const mbedtls_ssl_ciphersuite_t * ciphersuite_info ) | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 951 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 952 | mbedtls_ssl_key_cert *cur, *list, *fallback = NULL; | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 953 | mbedtls_pk_type_t pk_alg = | 
|  | 954 | mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ); | 
| Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 955 | uint32_t flags; | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 956 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 957 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 958 | if( ssl->handshake->sni_key_cert != NULL ) | 
|  | 959 | list = ssl->handshake->sni_key_cert; | 
|  | 960 | else | 
|  | 961 | #endif | 
| Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 962 | list = ssl->conf->key_cert; | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 963 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 964 | if( pk_alg == MBEDTLS_PK_NONE ) | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 965 | return( 0 ); | 
|  | 966 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 967 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite requires certificate" ) ); | 
| Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 968 |  | 
| Manuel Pégourié-Gonnard | e540b49 | 2015-07-07 12:44:38 +0200 | [diff] [blame] | 969 | if( list == NULL ) | 
|  | 970 | { | 
|  | 971 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server has no certificate" ) ); | 
|  | 972 | return( -1 ); | 
|  | 973 | } | 
|  | 974 |  | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 975 | for( cur = list; cur != NULL; cur = cur->next ) | 
|  | 976 | { | 
| Andrzej Kurek | 7ed01e8 | 2020-03-18 11:51:59 -0400 | [diff] [blame] | 977 | flags = 0; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 978 | MBEDTLS_SSL_DEBUG_CRT( 3, "candidate certificate chain, certificate", | 
| Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 979 | cur->cert ); | 
|  | 980 |  | 
| Gilles Peskine | e198df5 | 2018-01-05 21:17:45 +0100 | [diff] [blame] | 981 | if( ! mbedtls_pk_can_do( &cur->cert->pk, pk_alg ) ) | 
| Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 982 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 983 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: key type" ) ); | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 984 | continue; | 
| Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 985 | } | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 986 |  | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 987 | /* | 
|  | 988 | * This avoids sending the client a cert it'll reject based on | 
|  | 989 | * keyUsage or other extensions. | 
|  | 990 | * | 
|  | 991 | * It also allows the user to provision different certificates for | 
|  | 992 | * different uses based on keyUsage, eg if they want to avoid signing | 
|  | 993 | * and decrypting with the same RSA key. | 
|  | 994 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 995 | if( mbedtls_ssl_check_cert_usage( cur->cert, ciphersuite_info, | 
| Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 996 | MBEDTLS_SSL_IS_SERVER, &flags ) != 0 ) | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 997 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 998 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: " | 
| Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 999 | "(extended) key usage extension" ) ); | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 1000 | continue; | 
|  | 1001 | } | 
|  | 1002 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1003 | #if defined(MBEDTLS_ECDSA_C) | 
|  | 1004 | if( pk_alg == MBEDTLS_PK_ECDSA && | 
| Gilles Peskine | 81d4e89 | 2017-10-27 10:18:44 +0200 | [diff] [blame] | 1005 | ssl_check_key_curve( &cur->cert->pk, ssl->handshake->curves ) != 0 ) | 
| Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 1006 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1007 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: elliptic curve" ) ); | 
| Manuel Pégourié-Gonnard | 846ba47 | 2015-01-08 13:54:38 +0100 | [diff] [blame] | 1008 | continue; | 
| Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 1009 | } | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 1010 | #endif | 
| Manuel Pégourié-Gonnard | 846ba47 | 2015-01-08 13:54:38 +0100 | [diff] [blame] | 1011 |  | 
| Manuel Pégourié-Gonnard | df331a5 | 2015-01-08 16:43:07 +0100 | [diff] [blame] | 1012 | /* | 
|  | 1013 | * Try to select a SHA-1 certificate for pre-1.2 clients, but still | 
|  | 1014 | * present them a SHA-higher cert rather than failing if it's the only | 
|  | 1015 | * one we got that satisfies the other conditions. | 
|  | 1016 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1017 | if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 && | 
|  | 1018 | cur->cert->sig_md != MBEDTLS_MD_SHA1 ) | 
| Manuel Pégourié-Gonnard | df331a5 | 2015-01-08 16:43:07 +0100 | [diff] [blame] | 1019 | { | 
|  | 1020 | if( fallback == NULL ) | 
|  | 1021 | fallback = cur; | 
| Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 1022 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1023 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate not preferred: " | 
| Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 1024 | "sha-2 with pre-TLS 1.2 client" ) ); | 
| Manuel Pégourié-Gonnard | df331a5 | 2015-01-08 16:43:07 +0100 | [diff] [blame] | 1025 | continue; | 
| Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 1026 | } | 
| Manuel Pégourié-Gonnard | df331a5 | 2015-01-08 16:43:07 +0100 | [diff] [blame] | 1027 | } | 
|  | 1028 |  | 
| Manuel Pégourié-Gonnard | 846ba47 | 2015-01-08 13:54:38 +0100 | [diff] [blame] | 1029 | /* If we get there, we got a winner */ | 
|  | 1030 | break; | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 1031 | } | 
|  | 1032 |  | 
| Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 1033 | if( cur == NULL ) | 
|  | 1034 | cur = fallback; | 
|  | 1035 |  | 
| Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1036 | /* Do not update ssl->handshake->key_cert unless there is a match */ | 
| Manuel Pégourié-Gonnard | df331a5 | 2015-01-08 16:43:07 +0100 | [diff] [blame] | 1037 | if( cur != NULL ) | 
|  | 1038 | { | 
|  | 1039 | ssl->handshake->key_cert = cur; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1040 | MBEDTLS_SSL_DEBUG_CRT( 3, "selected certificate chain, certificate", | 
| Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 1041 | ssl->handshake->key_cert->cert ); | 
| Manuel Pégourié-Gonnard | df331a5 | 2015-01-08 16:43:07 +0100 | [diff] [blame] | 1042 | return( 0 ); | 
|  | 1043 | } | 
|  | 1044 |  | 
|  | 1045 | return( -1 ); | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 1046 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1047 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 1048 |  | 
|  | 1049 | /* | 
|  | 1050 | * Check if a given ciphersuite is suitable for use with our config/keys/etc | 
|  | 1051 | * Sets ciphersuite_info only if the suite matches. | 
|  | 1052 | */ | 
| Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 1053 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1054 | static int ssl_ciphersuite_match( mbedtls_ssl_context *ssl, int suite_id, | 
|  | 1055 | const mbedtls_ssl_ciphersuite_t **ciphersuite_info ) | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 1056 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1057 | const mbedtls_ssl_ciphersuite_t *suite_info; | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 1058 |  | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 1059 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1060 | defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 1061 | mbedtls_pk_type_t sig_type; | 
|  | 1062 | #endif | 
|  | 1063 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1064 | suite_info = mbedtls_ssl_ciphersuite_from_id( suite_id ); | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 1065 | if( suite_info == NULL ) | 
|  | 1066 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1067 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 1068 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 1069 | } | 
|  | 1070 |  | 
| Hanno Becker | 5c5efdf | 2019-01-28 14:59:35 +0000 | [diff] [blame] | 1071 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "trying ciphersuite: %#04x (%s)", | 
| Paul Elliott | 3891caf | 2020-12-17 18:42:40 +0000 | [diff] [blame] | 1072 | (unsigned int) suite_id, suite_info->name ) ); | 
| Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 1073 |  | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 1074 | if( suite_info->min_minor_ver > ssl->minor_ver || | 
|  | 1075 | suite_info->max_minor_ver < ssl->minor_ver ) | 
| Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 1076 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1077 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: version" ) ); | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 1078 | return( 0 ); | 
| Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 1079 | } | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 1080 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1081 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1082 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1083 | ( suite_info->flags & MBEDTLS_CIPHERSUITE_NODTLS ) ) | 
| Manuel Pégourié-Gonnard | d666451 | 2014-02-06 13:26:57 +0100 | [diff] [blame] | 1084 | return( 0 ); | 
|  | 1085 | #endif | 
|  | 1086 |  | 
| Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 1087 | #if defined(MBEDTLS_ARC4_C) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1088 | if( ssl->conf->arc4_disabled == MBEDTLS_SSL_ARC4_DISABLED && | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1089 | suite_info->cipher == MBEDTLS_CIPHER_ARC4_128 ) | 
| Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 1090 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1091 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: rc4" ) ); | 
| Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 1092 | return( 0 ); | 
| Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 1093 | } | 
| Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 1094 | #endif | 
| Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 1095 |  | 
| Manuel Pégourié-Gonnard | e511b4e | 2015-09-16 14:11:09 +0200 | [diff] [blame] | 1096 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
|  | 1097 | if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE && | 
| Manuel Pégourié-Gonnard | bf57be6 | 2015-09-16 15:04:01 +0200 | [diff] [blame] | 1098 | ( ssl->handshake->cli_exts & MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK ) == 0 ) | 
| Manuel Pégourié-Gonnard | e511b4e | 2015-09-16 14:11:09 +0200 | [diff] [blame] | 1099 | { | 
| Manuel Pégourié-Gonnard | bf57be6 | 2015-09-16 15:04:01 +0200 | [diff] [blame] | 1100 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: ecjpake " | 
|  | 1101 | "not configured or ext missing" ) ); | 
| Manuel Pégourié-Gonnard | e511b4e | 2015-09-16 14:11:09 +0200 | [diff] [blame] | 1102 | return( 0 ); | 
|  | 1103 | } | 
|  | 1104 | #endif | 
|  | 1105 |  | 
|  | 1106 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1107 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) | 
|  | 1108 | if( mbedtls_ssl_ciphersuite_uses_ec( suite_info ) && | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 1109 | ( ssl->handshake->curves == NULL || | 
|  | 1110 | ssl->handshake->curves[0] == NULL ) ) | 
| Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 1111 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1112 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: " | 
| Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 1113 | "no common elliptic curve" ) ); | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 1114 | return( 0 ); | 
| Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 1115 | } | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 1116 | #endif | 
|  | 1117 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1118 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 1119 | /* If the ciphersuite requires a pre-shared key and we don't | 
|  | 1120 | * have one, skip it now rather than failing later */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1121 | if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) && | 
| Hanno Becker | 845b946 | 2018-10-26 12:07:29 +0100 | [diff] [blame] | 1122 | ssl_conf_has_psk_or_cb( ssl->conf ) == 0 ) | 
| Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 1123 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1124 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no pre-shared key" ) ); | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 1125 | return( 0 ); | 
| Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 1126 | } | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 1127 | #endif | 
|  | 1128 |  | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 1129 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1130 | defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 1131 | /* If the ciphersuite requires signing, check whether | 
|  | 1132 | * a suitable hash algorithm is present. */ | 
|  | 1133 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) | 
|  | 1134 | { | 
|  | 1135 | sig_type = mbedtls_ssl_get_ciphersuite_sig_alg( suite_info ); | 
|  | 1136 | if( sig_type != MBEDTLS_PK_NONE && | 
|  | 1137 | mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs, sig_type ) == MBEDTLS_MD_NONE ) | 
|  | 1138 | { | 
|  | 1139 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no suitable hash algorithm " | 
| Paul Elliott | 3891caf | 2020-12-17 18:42:40 +0000 | [diff] [blame] | 1140 | "for signature algorithm %u", (unsigned) sig_type ) ); | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 1141 | return( 0 ); | 
|  | 1142 | } | 
|  | 1143 | } | 
|  | 1144 |  | 
|  | 1145 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1146 | MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 1147 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1148 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 1149 | /* | 
|  | 1150 | * Final check: if ciphersuite requires us to have a | 
|  | 1151 | * certificate/key of a particular type: | 
|  | 1152 | * - select the appropriate certificate if we have one, or | 
|  | 1153 | * - try the next ciphersuite if we don't | 
|  | 1154 | * This must be done last since we modify the key_cert list. | 
|  | 1155 | */ | 
|  | 1156 | if( ssl_pick_cert( ssl, suite_info ) != 0 ) | 
| Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 1157 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1158 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: " | 
| Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 1159 | "no suitable certificate" ) ); | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 1160 | return( 0 ); | 
| Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 1161 | } | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 1162 | #endif | 
|  | 1163 |  | 
|  | 1164 | *ciphersuite_info = suite_info; | 
|  | 1165 | return( 0 ); | 
|  | 1166 | } | 
|  | 1167 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1168 | #if defined(MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO) | 
| Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 1169 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1170 | static int ssl_parse_client_hello_v2( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1171 | { | 
| Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1172 | int ret, got_common_suite; | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1173 | unsigned int i, j; | 
|  | 1174 | size_t n; | 
|  | 1175 | unsigned int ciph_len, sess_len, chal_len; | 
|  | 1176 | unsigned char *buf, *p; | 
| Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 1177 | const int *ciphersuites; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1178 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1179 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1180 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) ); | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1181 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1182 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 1183 | if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ) | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1184 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1185 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) ); | 
| Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 1186 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1187 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1188 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1189 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1190 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1191 |  | 
|  | 1192 | buf = ssl->in_hdr; | 
|  | 1193 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1194 | MBEDTLS_SSL_DEBUG_BUF( 4, "record header", buf, 5 ); | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1195 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1196 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d", | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1197 | buf[2] ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1198 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d", | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1199 | ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1200 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]", | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1201 | buf[3], buf[4] ) ); | 
|  | 1202 |  | 
|  | 1203 | /* | 
|  | 1204 | * SSLv2 Client Hello | 
|  | 1205 | * | 
|  | 1206 | * Record layer: | 
|  | 1207 | *     0  .   1   message length | 
|  | 1208 | * | 
|  | 1209 | * SSL layer: | 
|  | 1210 | *     2  .   2   message type | 
|  | 1211 | *     3  .   4   protocol version | 
|  | 1212 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1213 | if( buf[2] != MBEDTLS_SSL_HS_CLIENT_HELLO || | 
|  | 1214 | buf[3] != MBEDTLS_SSL_MAJOR_VERSION_3 ) | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1215 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1216 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
|  | 1217 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1218 | } | 
|  | 1219 |  | 
|  | 1220 | n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF; | 
|  | 1221 |  | 
|  | 1222 | if( n < 17 || n > 512 ) | 
|  | 1223 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1224 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
|  | 1225 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1226 | } | 
|  | 1227 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1228 | ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3; | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1229 | ssl->minor_ver = ( buf[4] <= ssl->conf->max_minor_ver ) | 
|  | 1230 | ? buf[4]  : ssl->conf->max_minor_ver; | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1231 |  | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1232 | if( ssl->minor_ver < ssl->conf->min_minor_ver ) | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1233 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1234 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum" | 
| Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 1235 | " [%d:%d] < [%d:%d]", | 
|  | 1236 | ssl->major_ver, ssl->minor_ver, | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1237 | ssl->conf->min_major_ver, ssl->conf->min_minor_ver ) ); | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1238 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1239 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1240 | MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION ); | 
|  | 1241 | return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION ); | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1242 | } | 
|  | 1243 |  | 
| Paul Bakker | 2fbefde | 2013-06-29 16:01:15 +0200 | [diff] [blame] | 1244 | ssl->handshake->max_major_ver = buf[3]; | 
|  | 1245 | ssl->handshake->max_minor_ver = buf[4]; | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1246 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1247 | if( ( ret = mbedtls_ssl_fetch_input( ssl, 2 + n ) ) != 0 ) | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1248 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1249 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret ); | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1250 | return( ret ); | 
|  | 1251 | } | 
|  | 1252 |  | 
|  | 1253 | ssl->handshake->update_checksum( ssl, buf + 2, n ); | 
|  | 1254 |  | 
|  | 1255 | buf = ssl->in_msg; | 
|  | 1256 | n = ssl->in_left - 5; | 
|  | 1257 |  | 
|  | 1258 | /* | 
|  | 1259 | *    0  .   1   ciphersuitelist length | 
|  | 1260 | *    2  .   3   session id length | 
|  | 1261 | *    4  .   5   challenge length | 
|  | 1262 | *    6  .  ..   ciphersuitelist | 
|  | 1263 | *   ..  .  ..   session id | 
|  | 1264 | *   ..  .  ..   challenge | 
|  | 1265 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1266 | MBEDTLS_SSL_DEBUG_BUF( 4, "record contents", buf, n ); | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1267 |  | 
|  | 1268 | ciph_len = ( buf[0] << 8 ) | buf[1]; | 
|  | 1269 | sess_len = ( buf[2] << 8 ) | buf[3]; | 
|  | 1270 | chal_len = ( buf[4] << 8 ) | buf[5]; | 
|  | 1271 |  | 
| Paul Elliott | 9f35211 | 2020-12-09 14:55:45 +0000 | [diff] [blame] | 1272 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciph_len: %u, sess_len: %u, chal_len: %u", | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1273 | ciph_len, sess_len, chal_len ) ); | 
|  | 1274 |  | 
|  | 1275 | /* | 
|  | 1276 | * Make sure each parameter length is valid | 
|  | 1277 | */ | 
|  | 1278 | if( ciph_len < 3 || ( ciph_len % 3 ) != 0 ) | 
|  | 1279 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1280 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
|  | 1281 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1282 | } | 
|  | 1283 |  | 
|  | 1284 | if( sess_len > 32 ) | 
|  | 1285 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1286 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
|  | 1287 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1288 | } | 
|  | 1289 |  | 
|  | 1290 | if( chal_len < 8 || chal_len > 32 ) | 
|  | 1291 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1292 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
|  | 1293 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1294 | } | 
|  | 1295 |  | 
|  | 1296 | if( n != 6 + ciph_len + sess_len + chal_len ) | 
|  | 1297 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1298 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
|  | 1299 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1300 | } | 
|  | 1301 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1302 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist", | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1303 | buf + 6, ciph_len ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1304 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id", | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1305 | buf + 6 + ciph_len, sess_len ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1306 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, challenge", | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1307 | buf + 6 + ciph_len + sess_len, chal_len ); | 
|  | 1308 |  | 
|  | 1309 | p = buf + 6 + ciph_len; | 
| Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 1310 | ssl->session_negotiate->id_len = sess_len; | 
| Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 1311 | memset( ssl->session_negotiate->id, 0, | 
|  | 1312 | sizeof( ssl->session_negotiate->id ) ); | 
| Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 1313 | memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->id_len ); | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1314 |  | 
|  | 1315 | p += sess_len; | 
|  | 1316 | memset( ssl->handshake->randbytes, 0, 64 ); | 
|  | 1317 | memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len ); | 
|  | 1318 |  | 
|  | 1319 | /* | 
|  | 1320 | * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV | 
|  | 1321 | */ | 
|  | 1322 | for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 ) | 
|  | 1323 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1324 | if( p[0] == 0 && p[1] == 0 && p[2] == MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO ) | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1325 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1326 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) ); | 
|  | 1327 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 1328 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1329 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1330 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV " | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1331 | "during renegotiation" ) ); | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1332 |  | 
| Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 1333 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1334 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1335 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1336 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1337 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ | 
|  | 1338 | ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION; | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1339 | break; | 
|  | 1340 | } | 
|  | 1341 | } | 
|  | 1342 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1343 | #if defined(MBEDTLS_SSL_FALLBACK_SCSV) | 
| Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1344 | for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 ) | 
|  | 1345 | { | 
|  | 1346 | if( p[0] == 0 && | 
| Joe Subbiani | efb8fae | 2021-08-20 12:57:09 +0100 | [diff] [blame] | 1347 | MBEDTLS_GET_UINT16_BE(p, 1) != MBEDTLS_SSL_FALLBACK_SCSV_VALUE ) | 
| Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1348 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1349 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "received FALLBACK_SCSV" ) ); | 
| Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1350 |  | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1351 | if( ssl->minor_ver < ssl->conf->max_minor_ver ) | 
| Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1352 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1353 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) ); | 
| Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1354 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1355 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1356 | MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK ); | 
| Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1357 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1358 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1359 | } | 
|  | 1360 |  | 
|  | 1361 | break; | 
|  | 1362 | } | 
|  | 1363 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1364 | #endif /* MBEDTLS_SSL_FALLBACK_SCSV */ | 
| Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1365 |  | 
| Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1366 | got_common_suite = 0; | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1367 | ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver]; | 
| Manuel Pégourié-Gonnard | 011a8db | 2013-11-30 18:11:07 +0100 | [diff] [blame] | 1368 | ciphersuite_info = NULL; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1369 | #if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE) | 
| Manuel Pégourié-Gonnard | 1a9f2c7 | 2013-11-30 18:30:06 +0100 | [diff] [blame] | 1370 | for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 ) | 
| Manuel Pégourié-Gonnard | 1a9f2c7 | 2013-11-30 18:30:06 +0100 | [diff] [blame] | 1371 | for( i = 0; ciphersuites[i] != 0; i++ ) | 
|  | 1372 | #else | 
| Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 1373 | for( i = 0; ciphersuites[i] != 0; i++ ) | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1374 | for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 ) | 
| Manuel Pégourié-Gonnard | 1a9f2c7 | 2013-11-30 18:30:06 +0100 | [diff] [blame] | 1375 | #endif | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1376 | { | 
| Manuel Pégourié-Gonnard | 011a8db | 2013-11-30 18:11:07 +0100 | [diff] [blame] | 1377 | if( p[0] != 0 || | 
| Joe Subbiani | efb8fae | 2021-08-20 12:57:09 +0100 | [diff] [blame] | 1378 | MBEDTLS_GET_UINT16_BE(p, 1) != ciphersuites[i] ) | 
| Manuel Pégourié-Gonnard | 011a8db | 2013-11-30 18:11:07 +0100 | [diff] [blame] | 1379 | continue; | 
| Paul Bakker | 59c28a2 | 2013-06-29 15:33:42 +0200 | [diff] [blame] | 1380 |  | 
| Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1381 | got_common_suite = 1; | 
|  | 1382 |  | 
| Manuel Pégourié-Gonnard | 011a8db | 2013-11-30 18:11:07 +0100 | [diff] [blame] | 1383 | if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i], | 
|  | 1384 | &ciphersuite_info ) ) != 0 ) | 
|  | 1385 | return( ret ); | 
| Paul Bakker | 59c28a2 | 2013-06-29 15:33:42 +0200 | [diff] [blame] | 1386 |  | 
| Manuel Pégourié-Gonnard | 011a8db | 2013-11-30 18:11:07 +0100 | [diff] [blame] | 1387 | if( ciphersuite_info != NULL ) | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1388 | goto have_ciphersuite_v2; | 
|  | 1389 | } | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1390 |  | 
| Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1391 | if( got_common_suite ) | 
|  | 1392 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1393 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, " | 
| Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1394 | "but none of them usable" ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1395 | return( MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE ); | 
| Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1396 | } | 
|  | 1397 | else | 
|  | 1398 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1399 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) ); | 
|  | 1400 | return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN ); | 
| Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1401 | } | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1402 |  | 
|  | 1403 | have_ciphersuite_v2: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1404 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) ); | 
| Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 1405 |  | 
| Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 1406 | ssl->session_negotiate->ciphersuite = ciphersuites[i]; | 
| Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1407 | ssl->handshake->ciphersuite_info = ciphersuite_info; | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1408 |  | 
|  | 1409 | /* | 
|  | 1410 | * SSLv2 Client Hello relevant renegotiation security checks | 
|  | 1411 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1412 | if( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1413 | ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE ) | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1414 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1415 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) ); | 
| Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 1416 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1417 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1418 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1419 | } | 
|  | 1420 |  | 
|  | 1421 | ssl->in_left = 0; | 
|  | 1422 | ssl->state++; | 
|  | 1423 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1424 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) ); | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1425 |  | 
|  | 1426 | return( 0 ); | 
|  | 1427 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1428 | #endif /* MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */ | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1429 |  | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1430 | /* This function doesn't alert on errors that happen early during | 
|  | 1431 | ClientHello parsing because they might indicate that the client is | 
|  | 1432 | not talking SSL/TLS at all and would not understand our alert. */ | 
| Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 1433 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1434 | static int ssl_parse_client_hello( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1435 | { | 
| Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1436 | int ret, got_common_suite; | 
| Manuel Pégourié-Gonnard | 9de64f5 | 2015-07-01 15:51:43 +0200 | [diff] [blame] | 1437 | size_t i, j; | 
|  | 1438 | size_t ciph_offset, comp_offset, ext_offset; | 
|  | 1439 | size_t msg_len, ciph_len, sess_len, comp_len, ext_len; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1440 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 9de64f5 | 2015-07-01 15:51:43 +0200 | [diff] [blame] | 1441 | size_t cookie_offset, cookie_len; | 
| Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1442 | #endif | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1443 | unsigned char *buf, *p, *ext; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1444 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1445 | int renegotiation_info_seen = 0; | 
| Manuel Pégourié-Gonnard | eaecbd3 | 2014-11-06 02:38:02 +0100 | [diff] [blame] | 1446 | #endif | 
| Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1447 | int handshake_failure = 0; | 
| Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 1448 | const int *ciphersuites; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1449 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; | 
| Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 1450 | int major, minor; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1451 |  | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 1452 | /* If there is no signature-algorithm extension present, | 
|  | 1453 | * we need to fall back to the default values for allowed | 
|  | 1454 | * signature-hash pairs. */ | 
|  | 1455 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1456 | defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 1457 | int sig_hash_alg_ext_present = 0; | 
|  | 1458 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1459 | MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 1460 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1461 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1462 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1463 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | 
| Manuel Pégourié-Gonnard | f03c7aa | 2014-09-24 14:54:06 +0200 | [diff] [blame] | 1464 | read_record_header: | 
|  | 1465 | #endif | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1466 | /* | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1467 | * If renegotiating, then the input was read with mbedtls_ssl_read_record(), | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1468 | * otherwise read it ourselves manually in order to support SSLv2 | 
|  | 1469 | * ClientHello, which doesn't use the same record layer format. | 
|  | 1470 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1471 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 1472 | if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE ) | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1473 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1474 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1475 | if( ( ret = mbedtls_ssl_fetch_input( ssl, 5 ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 59c6f2e | 2015-01-22 11:06:40 +0000 | [diff] [blame] | 1476 | { | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1477 | /* No alert on a read error. */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1478 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret ); | 
| Manuel Pégourié-Gonnard | 59c6f2e | 2015-01-22 11:06:40 +0000 | [diff] [blame] | 1479 | return( ret ); | 
|  | 1480 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1481 | } | 
|  | 1482 |  | 
|  | 1483 | buf = ssl->in_hdr; | 
|  | 1484 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1485 | #if defined(MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO) | 
|  | 1486 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1487 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM ) | 
| Manuel Pégourié-Gonnard | 8a7cf25 | 2014-10-09 17:35:53 +0200 | [diff] [blame] | 1488 | #endif | 
|  | 1489 | if( ( buf[0] & 0x80 ) != 0 ) | 
| Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 1490 | return( ssl_parse_client_hello_v2( ssl ) ); | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1491 | #endif | 
|  | 1492 |  | 
| Hanno Becker | 5903de4 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 1493 | MBEDTLS_SSL_DEBUG_BUF( 4, "record header", buf, mbedtls_ssl_in_hdr_len( ssl ) ); | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1494 |  | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1495 | /* | 
| Manuel Pégourié-Gonnard | 6b1e207 | 2014-02-12 10:14:54 +0100 | [diff] [blame] | 1496 | * SSLv3/TLS Client Hello | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1497 | * | 
|  | 1498 | * Record layer: | 
|  | 1499 | *     0  .   0   message type | 
|  | 1500 | *     1  .   2   protocol version | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1501 | *     3  .   11  DTLS: epoch + record sequence number | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1502 | *     3  .   4   message length | 
|  | 1503 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1504 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d", | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1505 | buf[0] ) ); | 
|  | 1506 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1507 | if( buf[0] != MBEDTLS_SSL_MSG_HANDSHAKE ) | 
| Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 1508 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1509 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
|  | 1510 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 1511 | } | 
|  | 1512 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1513 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d", | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1514 | ( ssl->in_len[0] << 8 ) | ssl->in_len[1] ) ); | 
|  | 1515 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1516 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, protocol version: [%d:%d]", | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1517 | buf[1], buf[2] ) ); | 
|  | 1518 |  | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1519 | mbedtls_ssl_read_version( &major, &minor, ssl->conf->transport, buf + 1 ); | 
| Manuel Pégourié-Gonnard | 6b1e207 | 2014-02-12 10:14:54 +0100 | [diff] [blame] | 1520 |  | 
|  | 1521 | /* According to RFC 5246 Appendix E.1, the version here is typically | 
|  | 1522 | * "{03,00}, the lowest version number supported by the client, [or] the | 
|  | 1523 | * value of ClientHello.client_version", so the only meaningful check here | 
|  | 1524 | * is the major version shouldn't be less than 3 */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1525 | if( major < MBEDTLS_SSL_MAJOR_VERSION_3 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1526 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1527 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
|  | 1528 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1529 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1530 |  | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1531 | /* For DTLS if this is the initial handshake, remember the client sequence | 
|  | 1532 | * number to use it in our next message (RFC 6347 4.2.1) */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1533 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1534 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1535 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 1536 | && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE | 
| Manuel Pégourié-Gonnard | 3a173f4 | 2015-01-22 13:30:33 +0000 | [diff] [blame] | 1537 | #endif | 
|  | 1538 | ) | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1539 | { | 
|  | 1540 | /* Epoch should be 0 for initial handshakes */ | 
|  | 1541 | if( ssl->in_ctr[0] != 0 || ssl->in_ctr[1] != 0 ) | 
|  | 1542 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1543 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
|  | 1544 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1545 | } | 
|  | 1546 |  | 
| Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 1547 | memcpy( ssl->cur_out_ctr + 2, ssl->in_ctr + 2, 6 ); | 
| Manuel Pégourié-Gonnard | f03c7aa | 2014-09-24 14:54:06 +0200 | [diff] [blame] | 1548 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1549 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | 
|  | 1550 | if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 ) | 
| Manuel Pégourié-Gonnard | f03c7aa | 2014-09-24 14:54:06 +0200 | [diff] [blame] | 1551 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1552 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record, discarding" ) ); | 
| Manuel Pégourié-Gonnard | f03c7aa | 2014-09-24 14:54:06 +0200 | [diff] [blame] | 1553 | ssl->next_record_offset = 0; | 
|  | 1554 | ssl->in_left = 0; | 
|  | 1555 | goto read_record_header; | 
|  | 1556 | } | 
|  | 1557 |  | 
|  | 1558 | /* No MAC to check yet, so we can update right now */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1559 | mbedtls_ssl_dtls_replay_update( ssl ); | 
| Manuel Pégourié-Gonnard | f03c7aa | 2014-09-24 14:54:06 +0200 | [diff] [blame] | 1560 | #endif | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1561 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1562 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1563 |  | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1564 | msg_len = ( ssl->in_len[0] << 8 ) | ssl->in_len[1]; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1565 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1566 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 1567 | if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ) | 
| Manuel Pégourié-Gonnard | b89c4f3 | 2015-01-21 13:24:10 +0000 | [diff] [blame] | 1568 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1569 | /* Set by mbedtls_ssl_read_record() */ | 
| Manuel Pégourié-Gonnard | b89c4f3 | 2015-01-21 13:24:10 +0000 | [diff] [blame] | 1570 | msg_len = ssl->in_hslen; | 
|  | 1571 | } | 
|  | 1572 | else | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1573 | #endif | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1574 | { | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1575 | if( msg_len > MBEDTLS_SSL_IN_CONTENT_LEN ) | 
| Manuel Pégourié-Gonnard | d6b721c | 2014-03-24 12:13:54 +0100 | [diff] [blame] | 1576 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1577 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
|  | 1578 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Manuel Pégourié-Gonnard | d6b721c | 2014-03-24 12:13:54 +0100 | [diff] [blame] | 1579 | } | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1580 |  | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 1581 | if( ( ret = mbedtls_ssl_fetch_input( ssl, | 
| Hanno Becker | 5903de4 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 1582 | mbedtls_ssl_in_hdr_len( ssl ) + msg_len ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | d6b721c | 2014-03-24 12:13:54 +0100 | [diff] [blame] | 1583 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1584 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret ); | 
| Manuel Pégourié-Gonnard | d6b721c | 2014-03-24 12:13:54 +0100 | [diff] [blame] | 1585 | return( ret ); | 
|  | 1586 | } | 
| Manuel Pégourié-Gonnard | 30d16eb | 2014-08-19 17:43:50 +0200 | [diff] [blame] | 1587 |  | 
|  | 1588 | /* Done reading this record, get ready for the next one */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1589 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1590 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Hanno Becker | 5903de4 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 1591 | ssl->next_record_offset = msg_len + mbedtls_ssl_in_hdr_len( ssl ); | 
| Manuel Pégourié-Gonnard | 30d16eb | 2014-08-19 17:43:50 +0200 | [diff] [blame] | 1592 | else | 
|  | 1593 | #endif | 
|  | 1594 | ssl->in_left = 0; | 
| Manuel Pégourié-Gonnard | d6b721c | 2014-03-24 12:13:54 +0100 | [diff] [blame] | 1595 | } | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1596 |  | 
|  | 1597 | buf = ssl->in_msg; | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1598 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1599 | MBEDTLS_SSL_DEBUG_BUF( 4, "record contents", buf, msg_len ); | 
| Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 1600 |  | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1601 | ssl->handshake->update_checksum( ssl, buf, msg_len ); | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1602 |  | 
|  | 1603 | /* | 
| Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1604 | * Handshake layer: | 
|  | 1605 | *     0  .   0   handshake type | 
|  | 1606 | *     1  .   3   handshake length | 
| Shaun Case | 0e7791f | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1607 | *     4  .   5   DTLS only: message sequence number | 
| Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1608 | *     6  .   8   DTLS only: fragment offset | 
|  | 1609 | *     9  .  11   DTLS only: fragment length | 
| Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 1610 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1611 | if( msg_len < mbedtls_ssl_hs_hdr_len( ssl ) ) | 
| Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1612 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1613 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
|  | 1614 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1615 | } | 
|  | 1616 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1617 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d", buf[0] ) ); | 
| Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1618 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1619 | if( buf[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) | 
| Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1620 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1621 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
|  | 1622 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1623 | } | 
|  | 1624 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1625 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d", | 
| Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1626 | ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) ); | 
|  | 1627 |  | 
| Andrzej Kurek | 4353d3d | 2022-06-08 11:53:59 -0400 | [diff] [blame] | 1628 | if( buf[1] != 0 ) | 
|  | 1629 | { | 
|  | 1630 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message: %u != 0", | 
|  | 1631 | (unsigned) buf[1] ) ); | 
|  | 1632 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
|  | 1633 | } | 
| Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1634 | /* We don't support fragmentation of ClientHello (yet?) */ | 
| Andrzej Kurek | 4353d3d | 2022-06-08 11:53:59 -0400 | [diff] [blame] | 1635 | if( msg_len != mbedtls_ssl_hs_hdr_len( ssl ) + ( ( buf[2] << 8 ) | buf[3] ) ) | 
| Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1636 | { | 
| Gilles Peskine | f333dfa | 2022-02-15 23:53:36 +0100 | [diff] [blame] | 1637 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message: %u != %u + %u", | 
|  | 1638 | (unsigned) msg_len, | 
|  | 1639 | (unsigned) mbedtls_ssl_hs_hdr_len( ssl ), | 
|  | 1640 | (unsigned) ( buf[2] << 8 ) | buf[3] ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1641 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1642 | } | 
|  | 1643 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1644 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1645 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 1646 | { | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1647 | /* | 
| Manuel Pégourié-Gonnard | 69849f8 | 2015-03-10 11:54:02 +0000 | [diff] [blame] | 1648 | * Copy the client's handshake message_seq on initial handshakes, | 
|  | 1649 | * check sequence number on renego. | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1650 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1651 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 1652 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) | 
| Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 1653 | { | 
|  | 1654 | /* This couldn't be done in ssl_prepare_handshake_record() */ | 
|  | 1655 | unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) | | 
|  | 1656 | ssl->in_msg[5]; | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1657 |  | 
| Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 1658 | if( cli_msg_seq != ssl->handshake->in_msg_seq ) | 
|  | 1659 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1660 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message_seq: " | 
| Paul Elliott | 9f35211 | 2020-12-09 14:55:45 +0000 | [diff] [blame] | 1661 | "%u (expected %u)", cli_msg_seq, | 
| Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 1662 | ssl->handshake->in_msg_seq ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1663 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 1664 | } | 
|  | 1665 |  | 
|  | 1666 | ssl->handshake->in_msg_seq++; | 
|  | 1667 | } | 
| Manuel Pégourié-Gonnard | 69849f8 | 2015-03-10 11:54:02 +0000 | [diff] [blame] | 1668 | else | 
|  | 1669 | #endif | 
|  | 1670 | { | 
|  | 1671 | unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) | | 
|  | 1672 | ssl->in_msg[5]; | 
|  | 1673 | ssl->handshake->out_msg_seq = cli_msg_seq; | 
|  | 1674 | ssl->handshake->in_msg_seq  = cli_msg_seq + 1; | 
|  | 1675 | } | 
| Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 1676 |  | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1677 | /* | 
|  | 1678 | * For now we don't support fragmentation, so make sure | 
|  | 1679 | * fragment_offset == 0 and fragment_length == length | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1680 | */ | 
| Gilles Peskine | f333dfa | 2022-02-15 23:53:36 +0100 | [diff] [blame] | 1681 | MBEDTLS_SSL_DEBUG_MSG( | 
|  | 1682 | 4, ( "fragment_offset=%u fragment_length=%u length=%u", | 
|  | 1683 | (unsigned) ( ssl->in_msg[6] << 16 | ssl->in_msg[7] << 8 | ssl->in_msg[8] ), | 
|  | 1684 | (unsigned) ( ssl->in_msg[9] << 16 | ssl->in_msg[10] << 8 | ssl->in_msg[11] ), | 
|  | 1685 | (unsigned) ( ssl->in_msg[1] << 16 | ssl->in_msg[2] << 8 | ssl->in_msg[3] ) ) ); | 
| Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 1686 | if( ssl->in_msg[6] != 0 || ssl->in_msg[7] != 0 || ssl->in_msg[8] != 0 || | 
|  | 1687 | memcmp( ssl->in_msg + 1, ssl->in_msg + 9, 3 ) != 0 ) | 
|  | 1688 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1689 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "ClientHello fragmentation not supported" ) ); | 
|  | 1690 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
| Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 1691 | } | 
| Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 1692 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1693 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 1694 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1695 | buf += mbedtls_ssl_hs_hdr_len( ssl ); | 
|  | 1696 | msg_len -= mbedtls_ssl_hs_hdr_len( ssl ); | 
| Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1697 |  | 
| Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 1698 | /* | 
| Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1699 | * ClientHello layer: | 
|  | 1700 | *     0  .   1   protocol version | 
|  | 1701 | *     2  .  33   random bytes (starting with 4 bytes of Unix time) | 
|  | 1702 | *    34  .  35   session id length (1 byte) | 
|  | 1703 | *    35  . 34+x  session id | 
|  | 1704 | *   35+x . 35+x  DTLS only: cookie length (1 byte) | 
|  | 1705 | *   36+x .  ..   DTLS only: cookie | 
| Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1706 | *    ..  .  ..   ciphersuite list length (2 bytes) | 
|  | 1707 | *    ..  .  ..   ciphersuite list | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1708 | *    ..  .  ..   compression alg. list length (1 byte) | 
|  | 1709 | *    ..  .  ..   compression alg. list | 
|  | 1710 | *    ..  .  ..   extensions length (2 bytes, optional) | 
|  | 1711 | *    ..  .  ..   extensions (optional) | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1712 | */ | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1713 |  | 
|  | 1714 | /* | 
| Antonin Décimo | 36e89b5 | 2019-01-23 15:24:37 +0100 | [diff] [blame] | 1715 | * Minimal length (with everything empty and extensions omitted) is | 
| Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1716 | * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can | 
|  | 1717 | * read at least up to session id length without worrying. | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1718 | */ | 
| Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1719 | if( msg_len < 38 ) | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1720 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1721 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
|  | 1722 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1723 | } | 
|  | 1724 |  | 
|  | 1725 | /* | 
|  | 1726 | * Check and save the protocol version | 
|  | 1727 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1728 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, version", buf, 2 ); | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1729 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1730 | mbedtls_ssl_read_version( &ssl->major_ver, &ssl->minor_ver, | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1731 | ssl->conf->transport, buf ); | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1732 |  | 
| Manuel Pégourié-Gonnard | 6b1e207 | 2014-02-12 10:14:54 +0100 | [diff] [blame] | 1733 | ssl->handshake->max_major_ver = ssl->major_ver; | 
|  | 1734 | ssl->handshake->max_minor_ver = ssl->minor_ver; | 
|  | 1735 |  | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1736 | if( ssl->major_ver < ssl->conf->min_major_ver || | 
|  | 1737 | ssl->minor_ver < ssl->conf->min_minor_ver ) | 
| Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 1738 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1739 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum" | 
| Manuel Pégourié-Gonnard | 6b1e207 | 2014-02-12 10:14:54 +0100 | [diff] [blame] | 1740 | " [%d:%d] < [%d:%d]", | 
|  | 1741 | ssl->major_ver, ssl->minor_ver, | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1742 | ssl->conf->min_major_ver, ssl->conf->min_minor_ver ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1743 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1744 | MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1745 | return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION ); | 
| Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 1746 | } | 
|  | 1747 |  | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1748 | if( ssl->major_ver > ssl->conf->max_major_ver ) | 
| Manuel Pégourié-Gonnard | 6b1e207 | 2014-02-12 10:14:54 +0100 | [diff] [blame] | 1749 | { | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1750 | ssl->major_ver = ssl->conf->max_major_ver; | 
|  | 1751 | ssl->minor_ver = ssl->conf->max_minor_ver; | 
| Manuel Pégourié-Gonnard | 6b1e207 | 2014-02-12 10:14:54 +0100 | [diff] [blame] | 1752 | } | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1753 | else if( ssl->minor_ver > ssl->conf->max_minor_ver ) | 
|  | 1754 | ssl->minor_ver = ssl->conf->max_minor_ver; | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1755 |  | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1756 | /* | 
|  | 1757 | * Save client random (inc. Unix time) | 
|  | 1758 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1759 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 2, 32 ); | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1760 |  | 
| Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1761 | memcpy( ssl->handshake->randbytes, buf + 2, 32 ); | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1762 |  | 
|  | 1763 | /* | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1764 | * Check the session ID length and save session ID | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1765 | */ | 
| Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1766 | sess_len = buf[34]; | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1767 |  | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1768 | if( sess_len > sizeof( ssl->session_negotiate->id ) || | 
| Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1769 | sess_len + 34 + 2 > msg_len ) /* 2 for cipherlist length field */ | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1770 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1771 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1772 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1773 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1774 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1775 | } | 
|  | 1776 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1777 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id", buf + 35, sess_len ); | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1778 |  | 
| Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 1779 | ssl->session_negotiate->id_len = sess_len; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1780 | memset( ssl->session_negotiate->id, 0, | 
|  | 1781 | sizeof( ssl->session_negotiate->id ) ); | 
| Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1782 | memcpy( ssl->session_negotiate->id, buf + 35, | 
| Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 1783 | ssl->session_negotiate->id_len ); | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1784 |  | 
|  | 1785 | /* | 
| Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1786 | * Check the cookie length and content | 
|  | 1787 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1788 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1789 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1790 | { | 
| Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1791 | cookie_offset = 35 + sess_len; | 
| Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1792 | cookie_len = buf[cookie_offset]; | 
|  | 1793 |  | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1794 | if( cookie_offset + 1 + cookie_len + 2 > msg_len ) | 
| Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1795 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1796 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1797 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1798 | MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1799 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1800 | } | 
|  | 1801 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1802 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie", | 
| Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1803 | buf + cookie_offset + 1, cookie_len ); | 
|  | 1804 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1805 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1806 | if( ssl->conf->f_cookie_check != NULL | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1807 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 1808 | && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE | 
| Manuel Pégourié-Gonnard | 69849f8 | 2015-03-10 11:54:02 +0000 | [diff] [blame] | 1809 | #endif | 
|  | 1810 | ) | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1811 | { | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1812 | if( ssl->conf->f_cookie_check( ssl->conf->p_cookie, | 
| Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1813 | buf + cookie_offset + 1, cookie_len, | 
|  | 1814 | ssl->cli_id, ssl->cli_id_len ) != 0 ) | 
|  | 1815 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1816 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification failed" ) ); | 
| Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1817 | ssl->handshake->verify_cookie_len = 1; | 
|  | 1818 | } | 
|  | 1819 | else | 
|  | 1820 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1821 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification passed" ) ); | 
| Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1822 | ssl->handshake->verify_cookie_len = 0; | 
|  | 1823 | } | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1824 | } | 
| Manuel Pégourié-Gonnard | d7f9bc5 | 2014-07-23 11:09:27 +0200 | [diff] [blame] | 1825 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1826 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */ | 
| Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1827 | { | 
|  | 1828 | /* We know we didn't send a cookie, so it should be empty */ | 
|  | 1829 | if( cookie_len != 0 ) | 
|  | 1830 | { | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1831 | /* This may be an attacker's probe, so don't send an alert */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1832 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
|  | 1833 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1834 | } | 
|  | 1835 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1836 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification skipped" ) ); | 
| Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1837 | } | 
| Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1838 |  | 
|  | 1839 | /* | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1840 | * Check the ciphersuitelist length (will be parsed later) | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1841 | */ | 
| Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1842 | ciph_offset = cookie_offset + 1 + cookie_len; | 
| Manuel Pégourié-Gonnard | a06d7fe | 2015-03-13 10:36:55 +0000 | [diff] [blame] | 1843 | } | 
| Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1844 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1845 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1846 | ciph_offset = 35 + sess_len; | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1847 |  | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1848 | ciph_len = ( buf[ciph_offset + 0] << 8 ) | 
|  | 1849 | | ( buf[ciph_offset + 1]      ); | 
|  | 1850 |  | 
|  | 1851 | if( ciph_len < 2 || | 
|  | 1852 | ciph_len + 2 + ciph_offset + 1 > msg_len || /* 1 for comp. alg. len */ | 
|  | 1853 | ( ciph_len % 2 ) != 0 ) | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1854 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1855 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1856 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1857 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1858 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1859 | } | 
|  | 1860 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1861 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist", | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1862 | buf + ciph_offset + 2,  ciph_len ); | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1863 |  | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1864 | /* | 
|  | 1865 | * Check the compression algorithms length and pick one | 
|  | 1866 | */ | 
|  | 1867 | comp_offset = ciph_offset + 2 + ciph_len; | 
|  | 1868 |  | 
|  | 1869 | comp_len = buf[comp_offset]; | 
|  | 1870 |  | 
|  | 1871 | if( comp_len < 1 || | 
|  | 1872 | comp_len > 16 || | 
|  | 1873 | comp_len + comp_offset + 1 > msg_len ) | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1874 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1875 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1876 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1877 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1878 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1879 | } | 
|  | 1880 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1881 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, compression", | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1882 | buf + comp_offset + 1, comp_len ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1883 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1884 | ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL; | 
|  | 1885 | #if defined(MBEDTLS_ZLIB_SUPPORT) | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1886 | for( i = 0; i < comp_len; ++i ) | 
|  | 1887 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1888 | if( buf[comp_offset + 1 + i] == MBEDTLS_SSL_COMPRESS_DEFLATE ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1889 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1890 | ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_DEFLATE; | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1891 | break; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1892 | } | 
|  | 1893 | } | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1894 | #endif | 
|  | 1895 |  | 
| Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1896 | /* See comments in ssl_write_client_hello() */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1897 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1898 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1899 | ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL; | 
| Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1900 | #endif | 
| Manuel Pégourié-Gonnard | 82202f0 | 2014-07-23 00:28:58 +0200 | [diff] [blame] | 1901 |  | 
| Janos Follath | c6dab2b | 2016-05-23 14:27:02 +0100 | [diff] [blame] | 1902 | /* Do not parse the extensions if the protocol is SSLv3 */ | 
|  | 1903 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
|  | 1904 | if( ( ssl->major_ver != 3 ) || ( ssl->minor_ver != 0 ) ) | 
|  | 1905 | { | 
|  | 1906 | #endif | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1907 | /* | 
|  | 1908 | * Check the extension length | 
|  | 1909 | */ | 
|  | 1910 | ext_offset = comp_offset + 1 + comp_len; | 
|  | 1911 | if( msg_len > ext_offset ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1912 | { | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1913 | if( msg_len < ext_offset + 2 ) | 
|  | 1914 | { | 
|  | 1915 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1916 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1917 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1918 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
|  | 1919 | } | 
|  | 1920 |  | 
|  | 1921 | ext_len = ( buf[ext_offset + 0] << 8 ) | 
|  | 1922 | | ( buf[ext_offset + 1]      ); | 
|  | 1923 |  | 
| Glenn Strauss | 8a8a83b | 2021-02-02 02:21:23 -0500 | [diff] [blame] | 1924 | if( msg_len != ext_offset + 2 + ext_len ) | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1925 | { | 
|  | 1926 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1927 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1928 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1929 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
|  | 1930 | } | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1931 | } | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1932 | else | 
|  | 1933 | ext_len = 0; | 
| Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1934 |  | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1935 | ext = buf + ext_offset + 2; | 
|  | 1936 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", ext, ext_len ); | 
| Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1937 |  | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1938 | while( ext_len != 0 ) | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1939 | { | 
| Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 1940 | unsigned int ext_id; | 
|  | 1941 | unsigned int ext_size; | 
|  | 1942 | if ( ext_len < 4 ) { | 
|  | 1943 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
|  | 1944 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1945 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
|  | 1946 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
|  | 1947 | } | 
|  | 1948 | ext_id   = ( ( ext[0] <<  8 ) | ( ext[1] ) ); | 
|  | 1949 | ext_size = ( ( ext[2] <<  8 ) | ( ext[3] ) ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1950 |  | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1951 | if( ext_size + 4 > ext_len ) | 
|  | 1952 | { | 
|  | 1953 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1954 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1955 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1956 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
|  | 1957 | } | 
|  | 1958 | switch( ext_id ) | 
|  | 1959 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1960 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1961 | case MBEDTLS_TLS_EXT_SERVERNAME: | 
|  | 1962 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) ); | 
|  | 1963 | if( ssl->conf->f_sni == NULL ) | 
|  | 1964 | break; | 
| Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 1965 |  | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1966 | ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size ); | 
|  | 1967 | if( ret != 0 ) | 
|  | 1968 | return( ret ); | 
|  | 1969 | break; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1970 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ | 
| Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 1971 |  | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1972 | case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO: | 
|  | 1973 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1974 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1975 | renegotiation_info_seen = 1; | 
| Manuel Pégourié-Gonnard | eaecbd3 | 2014-11-06 02:38:02 +0100 | [diff] [blame] | 1976 | #endif | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1977 |  | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1978 | ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size ); | 
|  | 1979 | if( ret != 0 ) | 
|  | 1980 | return( ret ); | 
|  | 1981 | break; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1982 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1983 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1984 | defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1985 | case MBEDTLS_TLS_EXT_SIG_ALG: | 
| Ron Eldor | 73a3817 | 2017-10-03 15:58:26 +0300 | [diff] [blame] | 1986 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) ); | 
|  | 1987 |  | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1988 | ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size ); | 
|  | 1989 | if( ret != 0 ) | 
|  | 1990 | return( ret ); | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 1991 |  | 
|  | 1992 | sig_hash_alg_ext_present = 1; | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1993 | break; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1994 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1995 | MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1996 |  | 
| Robert Cragie | 136884c | 2015-10-02 13:34:31 +0100 | [diff] [blame] | 1997 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ | 
| Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 1998 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1999 | case MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES: | 
|  | 2000 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) ); | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2001 |  | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2002 | ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size ); | 
|  | 2003 | if( ret != 0 ) | 
|  | 2004 | return( ret ); | 
|  | 2005 | break; | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2006 |  | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2007 | case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS: | 
|  | 2008 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) ); | 
|  | 2009 | ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT; | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2010 |  | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2011 | ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size ); | 
|  | 2012 | if( ret != 0 ) | 
|  | 2013 | return( ret ); | 
|  | 2014 | break; | 
| Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 2015 | #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || | 
|  | 2016 | MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2017 |  | 
| Manuel Pégourié-Gonnard | bf57be6 | 2015-09-16 15:04:01 +0200 | [diff] [blame] | 2018 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2019 | case MBEDTLS_TLS_EXT_ECJPAKE_KKPP: | 
|  | 2020 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake kkpp extension" ) ); | 
| Manuel Pégourié-Gonnard | bf57be6 | 2015-09-16 15:04:01 +0200 | [diff] [blame] | 2021 |  | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2022 | ret = ssl_parse_ecjpake_kkpp( ssl, ext + 4, ext_size ); | 
|  | 2023 | if( ret != 0 ) | 
|  | 2024 | return( ret ); | 
|  | 2025 | break; | 
| Manuel Pégourié-Gonnard | bf57be6 | 2015-09-16 15:04:01 +0200 | [diff] [blame] | 2026 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ | 
|  | 2027 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2028 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2029 | case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH: | 
|  | 2030 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) ); | 
| Manuel Pégourié-Gonnard | 48f8d0d | 2013-07-17 10:25:37 +0200 | [diff] [blame] | 2031 |  | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2032 | ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size ); | 
|  | 2033 | if( ret != 0 ) | 
|  | 2034 | return( ret ); | 
|  | 2035 | break; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2036 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ | 
| Manuel Pégourié-Gonnard | 48f8d0d | 2013-07-17 10:25:37 +0200 | [diff] [blame] | 2037 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2038 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2039 | case MBEDTLS_TLS_EXT_TRUNCATED_HMAC: | 
|  | 2040 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) ); | 
| Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 2041 |  | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2042 | ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size ); | 
|  | 2043 | if( ret != 0 ) | 
|  | 2044 | return( ret ); | 
|  | 2045 | break; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2046 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ | 
| Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 2047 |  | 
| Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2048 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
| Hanno Becker | 89dcc88 | 2019-04-26 13:56:39 +0100 | [diff] [blame] | 2049 | case MBEDTLS_TLS_EXT_CID: | 
|  | 2050 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found CID extension" ) ); | 
|  | 2051 |  | 
|  | 2052 | ret = ssl_parse_cid_ext( ssl, ext + 4, ext_size ); | 
|  | 2053 | if( ret != 0 ) | 
|  | 2054 | return( ret ); | 
|  | 2055 | break; | 
|  | 2056 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ | 
|  | 2057 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2058 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2059 | case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC: | 
|  | 2060 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) ); | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2061 |  | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2062 | ret = ssl_parse_encrypt_then_mac_ext( ssl, ext + 4, ext_size ); | 
|  | 2063 | if( ret != 0 ) | 
|  | 2064 | return( ret ); | 
|  | 2065 | break; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2066 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2067 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2068 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2069 | case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET: | 
|  | 2070 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) ); | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2071 |  | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2072 | ret = ssl_parse_extended_ms_ext( ssl, ext + 4, ext_size ); | 
|  | 2073 | if( ret != 0 ) | 
|  | 2074 | return( ret ); | 
|  | 2075 | break; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2076 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2077 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2078 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2079 | case MBEDTLS_TLS_EXT_SESSION_TICKET: | 
|  | 2080 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) ); | 
| Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 2081 |  | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2082 | ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size ); | 
|  | 2083 | if( ret != 0 ) | 
|  | 2084 | return( ret ); | 
|  | 2085 | break; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2086 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ | 
| Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 2087 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2088 | #if defined(MBEDTLS_SSL_ALPN) | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2089 | case MBEDTLS_TLS_EXT_ALPN: | 
|  | 2090 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) ); | 
| Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 2091 |  | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2092 | ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ); | 
|  | 2093 | if( ret != 0 ) | 
|  | 2094 | return( ret ); | 
|  | 2095 | break; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2096 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ | 
| Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 2097 |  | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2098 | #if defined(MBEDTLS_SSL_DTLS_SRTP) | 
|  | 2099 | case MBEDTLS_TLS_EXT_USE_SRTP: | 
|  | 2100 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found use_srtp extension" ) ); | 
| Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2101 |  | 
| Johan Pascal | c3ccd98 | 2020-10-28 17:18:18 +0100 | [diff] [blame] | 2102 | ret = ssl_parse_use_srtp_ext( ssl, ext + 4, ext_size ); | 
|  | 2103 | if( ret != 0 ) | 
|  | 2104 | return( ret ); | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2105 | break; | 
|  | 2106 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ | 
|  | 2107 |  | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2108 | default: | 
| Paul Elliott | 9f35211 | 2020-12-09 14:55:45 +0000 | [diff] [blame] | 2109 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "unknown extension found: %u (ignoring)", | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2110 | ext_id ) ); | 
|  | 2111 | } | 
|  | 2112 |  | 
|  | 2113 | ext_len -= 4 + ext_size; | 
|  | 2114 | ext += 4 + ext_size; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2115 | } | 
| Janos Follath | c6dab2b | 2016-05-23 14:27:02 +0100 | [diff] [blame] | 2116 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
|  | 2117 | } | 
|  | 2118 | #endif | 
|  | 2119 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2120 | #if defined(MBEDTLS_SSL_FALLBACK_SCSV) | 
| Gilles Peskine | d50177f | 2017-05-16 17:53:03 +0200 | [diff] [blame] | 2121 | for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 ) | 
| Manuel Pégourié-Gonnard | fedba98 | 2014-11-05 16:12:09 +0100 | [diff] [blame] | 2122 | { | 
| Joe Subbiani | efb8fae | 2021-08-20 12:57:09 +0100 | [diff] [blame] | 2123 | if( MBEDTLS_GET_UINT16_BE( p, 0 ) == MBEDTLS_SSL_FALLBACK_SCSV_VALUE ) | 
| Manuel Pégourié-Gonnard | fedba98 | 2014-11-05 16:12:09 +0100 | [diff] [blame] | 2124 | { | 
| Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 2125 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "received FALLBACK_SCSV" ) ); | 
| Manuel Pégourié-Gonnard | fedba98 | 2014-11-05 16:12:09 +0100 | [diff] [blame] | 2126 |  | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2127 | if( ssl->minor_ver < ssl->conf->max_minor_ver ) | 
| Manuel Pégourié-Gonnard | fedba98 | 2014-11-05 16:12:09 +0100 | [diff] [blame] | 2128 | { | 
| Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 2129 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) ); | 
| Manuel Pégourié-Gonnard | fedba98 | 2014-11-05 16:12:09 +0100 | [diff] [blame] | 2130 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2131 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 2132 | MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK ); | 
| Manuel Pégourié-Gonnard | fedba98 | 2014-11-05 16:12:09 +0100 | [diff] [blame] | 2133 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2134 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Manuel Pégourié-Gonnard | fedba98 | 2014-11-05 16:12:09 +0100 | [diff] [blame] | 2135 | } | 
|  | 2136 |  | 
|  | 2137 | break; | 
|  | 2138 | } | 
|  | 2139 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2140 | #endif /* MBEDTLS_SSL_FALLBACK_SCSV */ | 
| Manuel Pégourié-Gonnard | fedba98 | 2014-11-05 16:12:09 +0100 | [diff] [blame] | 2141 |  | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 2142 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2143 | defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 2144 |  | 
|  | 2145 | /* | 
|  | 2146 | * Try to fall back to default hash SHA1 if the client | 
|  | 2147 | * hasn't provided any preferred signature-hash combinations. | 
|  | 2148 | */ | 
|  | 2149 | if( sig_hash_alg_ext_present == 0 ) | 
|  | 2150 | { | 
|  | 2151 | mbedtls_md_type_t md_default = MBEDTLS_MD_SHA1; | 
|  | 2152 |  | 
|  | 2153 | if( mbedtls_ssl_check_sig_hash( ssl, md_default ) != 0 ) | 
|  | 2154 | md_default = MBEDTLS_MD_NONE; | 
|  | 2155 |  | 
|  | 2156 | mbedtls_ssl_sig_hash_set_const_hash( &ssl->handshake->hash_algs, md_default ); | 
|  | 2157 | } | 
|  | 2158 |  | 
|  | 2159 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2160 | MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 2161 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2162 | /* | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 2163 | * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV | 
|  | 2164 | */ | 
|  | 2165 | for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 ) | 
|  | 2166 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2167 | if( p[0] == 0 && p[1] == MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO ) | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 2168 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2169 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) ); | 
|  | 2170 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 2171 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 2172 | { | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 2173 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV " | 
|  | 2174 | "during renegotiation" ) ); | 
| Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 2175 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 2176 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2177 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 2178 | } | 
| Manuel Pégourié-Gonnard | 69849f8 | 2015-03-10 11:54:02 +0000 | [diff] [blame] | 2179 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2180 | ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION; | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 2181 | break; | 
|  | 2182 | } | 
|  | 2183 | } | 
|  | 2184 |  | 
|  | 2185 | /* | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2186 | * Renegotiation security checks | 
|  | 2187 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2188 | if( ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION && | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2189 | ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE ) | 
| Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2190 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2191 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) ); | 
| Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2192 | handshake_failure = 1; | 
|  | 2193 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2194 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 2195 | else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && | 
|  | 2196 | ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION && | 
| Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2197 | renegotiation_info_seen == 0 ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2198 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2199 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) ); | 
| Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2200 | handshake_failure = 1; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2201 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2202 | else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && | 
|  | 2203 | ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2204 | ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2205 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2206 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) ); | 
| Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2207 | handshake_failure = 1; | 
|  | 2208 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2209 | else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && | 
|  | 2210 | ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && | 
| Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2211 | renegotiation_info_seen == 1 ) | 
|  | 2212 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2213 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) ); | 
| Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2214 | handshake_failure = 1; | 
|  | 2215 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2216 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ | 
| Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2217 |  | 
|  | 2218 | if( handshake_failure == 1 ) | 
|  | 2219 | { | 
| Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 2220 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 2221 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2222 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2223 | } | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2224 |  | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2225 | /* | 
|  | 2226 | * Search for a matching ciphersuite | 
| Manuel Pégourié-Gonnard | 3ebb2cd | 2013-09-23 17:00:18 +0200 | [diff] [blame] | 2227 | * (At the end because we need information from the EC-based extensions | 
|  | 2228 | * and certificate from the SNI callback triggered by the SNI extension.) | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2229 | */ | 
| Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 2230 | got_common_suite = 0; | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2231 | ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver]; | 
| Manuel Pégourié-Gonnard | 59b81d7 | 2013-11-30 17:46:04 +0100 | [diff] [blame] | 2232 | ciphersuite_info = NULL; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2233 | #if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE) | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 2234 | for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 ) | 
| Manuel Pégourié-Gonnard | 1a9f2c7 | 2013-11-30 18:30:06 +0100 | [diff] [blame] | 2235 | for( i = 0; ciphersuites[i] != 0; i++ ) | 
|  | 2236 | #else | 
| Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 2237 | for( i = 0; ciphersuites[i] != 0; i++ ) | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 2238 | for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 ) | 
| Manuel Pégourié-Gonnard | 1a9f2c7 | 2013-11-30 18:30:06 +0100 | [diff] [blame] | 2239 | #endif | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2240 | { | 
| Joe Subbiani | efb8fae | 2021-08-20 12:57:09 +0100 | [diff] [blame] | 2241 | if( MBEDTLS_GET_UINT16_BE(p, 0) != ciphersuites[i] ) | 
| Manuel Pégourié-Gonnard | 011a8db | 2013-11-30 18:11:07 +0100 | [diff] [blame] | 2242 | continue; | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2243 |  | 
| Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 2244 | got_common_suite = 1; | 
|  | 2245 |  | 
| Manuel Pégourié-Gonnard | 011a8db | 2013-11-30 18:11:07 +0100 | [diff] [blame] | 2246 | if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i], | 
|  | 2247 | &ciphersuite_info ) ) != 0 ) | 
|  | 2248 | return( ret ); | 
|  | 2249 |  | 
|  | 2250 | if( ciphersuite_info != NULL ) | 
|  | 2251 | goto have_ciphersuite; | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2252 | } | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2253 |  | 
| Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 2254 | if( got_common_suite ) | 
|  | 2255 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2256 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, " | 
| Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 2257 | "but none of them usable" ) ); | 
| Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 2258 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 2259 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2260 | return( MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE ); | 
| Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 2261 | } | 
|  | 2262 | else | 
|  | 2263 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2264 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) ); | 
| Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 2265 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 2266 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2267 | return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN ); | 
| Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 2268 | } | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2269 |  | 
|  | 2270 | have_ciphersuite: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2271 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) ); | 
| Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 2272 |  | 
| Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 2273 | ssl->session_negotiate->ciphersuite = ciphersuites[i]; | 
| Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 2274 | ssl->handshake->ciphersuite_info = ciphersuite_info; | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2275 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2276 | ssl->state++; | 
|  | 2277 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2278 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2279 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2280 | mbedtls_ssl_recv_flight_completed( ssl ); | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2281 | #endif | 
|  | 2282 |  | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 2283 | /* Debugging-only output for testsuite */ | 
|  | 2284 | #if defined(MBEDTLS_DEBUG_C)                         && \ | 
|  | 2285 | defined(MBEDTLS_SSL_PROTO_TLS1_2)                && \ | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2286 | defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 2287 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) | 
|  | 2288 | { | 
|  | 2289 | mbedtls_pk_type_t sig_alg = mbedtls_ssl_get_ciphersuite_sig_alg( ciphersuite_info ); | 
|  | 2290 | if( sig_alg != MBEDTLS_PK_NONE ) | 
|  | 2291 | { | 
|  | 2292 | mbedtls_md_type_t md_alg = mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs, | 
|  | 2293 | sig_alg ); | 
|  | 2294 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d", | 
|  | 2295 | mbedtls_ssl_hash_from_md_alg( md_alg ) ) ); | 
|  | 2296 | } | 
|  | 2297 | else | 
|  | 2298 | { | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 2299 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no hash algorithm for signature algorithm " | 
| Paul Elliott | 3891caf | 2020-12-17 18:42:40 +0000 | [diff] [blame] | 2300 | "%u - should not happen", (unsigned) sig_alg ) ); | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 2301 | } | 
|  | 2302 | } | 
|  | 2303 | #endif | 
|  | 2304 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2305 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2306 |  | 
|  | 2307 | return( 0 ); | 
|  | 2308 | } | 
|  | 2309 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2310 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) | 
|  | 2311 | static void ssl_write_truncated_hmac_ext( mbedtls_ssl_context *ssl, | 
| Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 2312 | unsigned char *buf, | 
|  | 2313 | size_t *olen ) | 
|  | 2314 | { | 
|  | 2315 | unsigned char *p = buf; | 
|  | 2316 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2317 | if( ssl->session_negotiate->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_DISABLED ) | 
| Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 2318 | { | 
|  | 2319 | *olen = 0; | 
|  | 2320 | return; | 
|  | 2321 | } | 
|  | 2322 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2323 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) ); | 
| Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 2324 |  | 
| Joe Subbiani | 2f98d79 | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 2325 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_TRUNCATED_HMAC, p, 0 ); | 
|  | 2326 | p += 2; | 
| Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 2327 |  | 
|  | 2328 | *p++ = 0x00; | 
|  | 2329 | *p++ = 0x00; | 
|  | 2330 |  | 
|  | 2331 | *olen = 4; | 
|  | 2332 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2333 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ | 
| Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 2334 |  | 
| Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2335 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
| Hanno Becker | 51de2d3 | 2019-04-26 15:46:55 +0100 | [diff] [blame] | 2336 | static void ssl_write_cid_ext( mbedtls_ssl_context *ssl, | 
|  | 2337 | unsigned char *buf, | 
|  | 2338 | size_t *olen ) | 
|  | 2339 | { | 
|  | 2340 | unsigned char *p = buf; | 
|  | 2341 | size_t ext_len; | 
|  | 2342 | const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN; | 
|  | 2343 |  | 
|  | 2344 | *olen = 0; | 
|  | 2345 |  | 
|  | 2346 | /* Skip writing the extension if we don't want to use it or if | 
|  | 2347 | * the client hasn't offered it. */ | 
|  | 2348 | if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_DISABLED ) | 
|  | 2349 | return; | 
|  | 2350 |  | 
|  | 2351 | /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX | 
|  | 2352 | * which is at most 255, so the increment cannot overflow. */ | 
|  | 2353 | if( end < p || (size_t)( end - p ) < (unsigned)( ssl->own_cid_len + 5 ) ) | 
|  | 2354 | { | 
|  | 2355 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) ); | 
|  | 2356 | return; | 
|  | 2357 | } | 
|  | 2358 |  | 
|  | 2359 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding CID extension" ) ); | 
|  | 2360 |  | 
|  | 2361 | /* | 
| Hanno Becker | ebcc913 | 2019-05-15 10:26:32 +0100 | [diff] [blame] | 2362 | * Quoting draft-ietf-tls-dtls-connection-id-05 | 
|  | 2363 | * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05 | 
| Hanno Becker | 51de2d3 | 2019-04-26 15:46:55 +0100 | [diff] [blame] | 2364 | * | 
|  | 2365 | *   struct { | 
|  | 2366 | *      opaque cid<0..2^8-1>; | 
|  | 2367 | *   } ConnectionId; | 
|  | 2368 | */ | 
| Joe Subbiani | 2f98d79 | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 2369 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_CID, p, 0 ); | 
|  | 2370 | p += 2; | 
| Hanno Becker | 51de2d3 | 2019-04-26 15:46:55 +0100 | [diff] [blame] | 2371 | ext_len = (size_t) ssl->own_cid_len + 1; | 
| Joe Subbiani | 2f98d79 | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 2372 | MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 ); | 
|  | 2373 | p += 2; | 
| Hanno Becker | 51de2d3 | 2019-04-26 15:46:55 +0100 | [diff] [blame] | 2374 |  | 
|  | 2375 | *p++ = (uint8_t) ssl->own_cid_len; | 
|  | 2376 | memcpy( p, ssl->own_cid, ssl->own_cid_len ); | 
|  | 2377 |  | 
|  | 2378 | *olen = ssl->own_cid_len + 5; | 
|  | 2379 | } | 
| Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2380 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
| Hanno Becker | 51de2d3 | 2019-04-26 15:46:55 +0100 | [diff] [blame] | 2381 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2382 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
|  | 2383 | static void ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl, | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2384 | unsigned char *buf, | 
|  | 2385 | size_t *olen ) | 
|  | 2386 | { | 
|  | 2387 | unsigned char *p = buf; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2388 | const mbedtls_ssl_ciphersuite_t *suite = NULL; | 
|  | 2389 | const mbedtls_cipher_info_t *cipher = NULL; | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2390 |  | 
| Ronald Cron | f1ed595 | 2022-03-24 14:15:28 +0100 | [diff] [blame] | 2391 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) | 
|  | 2392 | ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_DISABLED; | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2393 |  | 
| Manuel Pégourié-Gonnard | 78e745f | 2014-11-04 15:44:06 +0100 | [diff] [blame] | 2394 | /* | 
|  | 2395 | * RFC 7366: "If a server receives an encrypt-then-MAC request extension | 
|  | 2396 | * from a client and then selects a stream or Authenticated Encryption | 
|  | 2397 | * with Associated Data (AEAD) ciphersuite, it MUST NOT send an | 
|  | 2398 | * encrypt-then-MAC response extension back to the client." | 
|  | 2399 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2400 | if( ( suite = mbedtls_ssl_ciphersuite_from_id( | 
| Manuel Pégourié-Gonnard | 78e745f | 2014-11-04 15:44:06 +0100 | [diff] [blame] | 2401 | ssl->session_negotiate->ciphersuite ) ) == NULL || | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2402 | ( cipher = mbedtls_cipher_info_from_type( suite->cipher ) ) == NULL || | 
|  | 2403 | cipher->mode != MBEDTLS_MODE_CBC ) | 
| Manuel Pégourié-Gonnard | 78e745f | 2014-11-04 15:44:06 +0100 | [diff] [blame] | 2404 | { | 
| Ronald Cron | f1ed595 | 2022-03-24 14:15:28 +0100 | [diff] [blame] | 2405 | ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_DISABLED; | 
|  | 2406 | } | 
|  | 2407 |  | 
|  | 2408 | if( ssl->session_negotiate->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ) | 
|  | 2409 | { | 
| Manuel Pégourié-Gonnard | 78e745f | 2014-11-04 15:44:06 +0100 | [diff] [blame] | 2410 | *olen = 0; | 
|  | 2411 | return; | 
|  | 2412 | } | 
|  | 2413 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2414 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding encrypt then mac extension" ) ); | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2415 |  | 
| Joe Subbiani | 2f98d79 | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 2416 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, p, 0 ); | 
|  | 2417 | p += 2; | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2418 |  | 
|  | 2419 | *p++ = 0x00; | 
|  | 2420 | *p++ = 0x00; | 
|  | 2421 |  | 
|  | 2422 | *olen = 4; | 
|  | 2423 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2424 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2425 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2426 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) | 
|  | 2427 | static void ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl, | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2428 | unsigned char *buf, | 
|  | 2429 | size_t *olen ) | 
|  | 2430 | { | 
|  | 2431 | unsigned char *p = buf; | 
|  | 2432 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2433 | if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED || | 
|  | 2434 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2435 | { | 
|  | 2436 | *olen = 0; | 
|  | 2437 | return; | 
|  | 2438 | } | 
|  | 2439 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2440 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding extended master secret " | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2441 | "extension" ) ); | 
|  | 2442 |  | 
| Joe Subbiani | 2f98d79 | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 2443 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, p, 0 ); | 
|  | 2444 | p += 2; | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2445 |  | 
|  | 2446 | *p++ = 0x00; | 
|  | 2447 | *p++ = 0x00; | 
|  | 2448 |  | 
|  | 2449 | *olen = 4; | 
|  | 2450 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2451 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2452 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2453 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | 
|  | 2454 | static void ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl, | 
| Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 2455 | unsigned char *buf, | 
|  | 2456 | size_t *olen ) | 
|  | 2457 | { | 
|  | 2458 | unsigned char *p = buf; | 
|  | 2459 |  | 
|  | 2460 | if( ssl->handshake->new_session_ticket == 0 ) | 
|  | 2461 | { | 
|  | 2462 | *olen = 0; | 
|  | 2463 | return; | 
|  | 2464 | } | 
|  | 2465 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2466 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) ); | 
| Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 2467 |  | 
| Joe Subbiani | 2f98d79 | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 2468 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SESSION_TICKET, p, 0 ); | 
|  | 2469 | p += 2; | 
| Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 2470 |  | 
|  | 2471 | *p++ = 0x00; | 
|  | 2472 | *p++ = 0x00; | 
|  | 2473 |  | 
|  | 2474 | *olen = 4; | 
|  | 2475 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2476 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ | 
| Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 2477 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2478 | static void ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl, | 
| Manuel Pégourié-Gonnard | f11a6d7 | 2013-07-17 11:17:14 +0200 | [diff] [blame] | 2479 | unsigned char *buf, | 
|  | 2480 | size_t *olen ) | 
|  | 2481 | { | 
|  | 2482 | unsigned char *p = buf; | 
|  | 2483 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2484 | if( ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION ) | 
| Manuel Pégourié-Gonnard | f11a6d7 | 2013-07-17 11:17:14 +0200 | [diff] [blame] | 2485 | { | 
|  | 2486 | *olen = 0; | 
|  | 2487 | return; | 
|  | 2488 | } | 
|  | 2489 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2490 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) ); | 
| Manuel Pégourié-Gonnard | f11a6d7 | 2013-07-17 11:17:14 +0200 | [diff] [blame] | 2491 |  | 
| Joe Subbiani | 2f98d79 | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 2492 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO, p, 0 ); | 
|  | 2493 | p += 2; | 
| Manuel Pégourié-Gonnard | f11a6d7 | 2013-07-17 11:17:14 +0200 | [diff] [blame] | 2494 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2495 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 2496 | if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ) | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2497 | { | 
|  | 2498 | *p++ = 0x00; | 
|  | 2499 | *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF; | 
|  | 2500 | *p++ = ssl->verify_data_len * 2 & 0xFF; | 
| Manuel Pégourié-Gonnard | f11a6d7 | 2013-07-17 11:17:14 +0200 | [diff] [blame] | 2501 |  | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2502 | memcpy( p, ssl->peer_verify_data, ssl->verify_data_len ); | 
|  | 2503 | p += ssl->verify_data_len; | 
|  | 2504 | memcpy( p, ssl->own_verify_data, ssl->verify_data_len ); | 
|  | 2505 | p += ssl->verify_data_len; | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2506 | } | 
|  | 2507 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2508 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2509 | { | 
|  | 2510 | *p++ = 0x00; | 
|  | 2511 | *p++ = 0x01; | 
|  | 2512 | *p++ = 0x00; | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2513 | } | 
| Manuel Pégourié-Gonnard | 1938975 | 2015-06-23 13:46:44 +0200 | [diff] [blame] | 2514 |  | 
|  | 2515 | *olen = p - buf; | 
| Manuel Pégourié-Gonnard | f11a6d7 | 2013-07-17 11:17:14 +0200 | [diff] [blame] | 2516 | } | 
|  | 2517 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2518 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
|  | 2519 | static void ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl, | 
| Manuel Pégourié-Gonnard | 7bb7899 | 2013-07-17 13:50:08 +0200 | [diff] [blame] | 2520 | unsigned char *buf, | 
|  | 2521 | size_t *olen ) | 
|  | 2522 | { | 
|  | 2523 | unsigned char *p = buf; | 
|  | 2524 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2525 | if( ssl->session_negotiate->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ) | 
| Manuel Pégourié-Gonnard | e048b67 | 2013-07-19 12:47:00 +0200 | [diff] [blame] | 2526 | { | 
| Manuel Pégourié-Gonnard | 7bb7899 | 2013-07-17 13:50:08 +0200 | [diff] [blame] | 2527 | *olen = 0; | 
|  | 2528 | return; | 
|  | 2529 | } | 
|  | 2530 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2531 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) ); | 
| Manuel Pégourié-Gonnard | 7bb7899 | 2013-07-17 13:50:08 +0200 | [diff] [blame] | 2532 |  | 
| Joe Subbiani | 2f98d79 | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 2533 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, p, 0 ); | 
|  | 2534 | p += 2; | 
| Manuel Pégourié-Gonnard | 7bb7899 | 2013-07-17 13:50:08 +0200 | [diff] [blame] | 2535 |  | 
|  | 2536 | *p++ = 0x00; | 
|  | 2537 | *p++ = 1; | 
|  | 2538 |  | 
| Manuel Pégourié-Gonnard | ed4af8b | 2013-07-18 14:07:09 +0200 | [diff] [blame] | 2539 | *p++ = ssl->session_negotiate->mfl_code; | 
| Manuel Pégourié-Gonnard | 7bb7899 | 2013-07-17 13:50:08 +0200 | [diff] [blame] | 2540 |  | 
|  | 2541 | *olen = 5; | 
|  | 2542 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2543 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ | 
| Manuel Pégourié-Gonnard | 7bb7899 | 2013-07-17 13:50:08 +0200 | [diff] [blame] | 2544 |  | 
| Manuel Pégourié-Gonnard | f472179 | 2015-09-15 10:53:51 +0200 | [diff] [blame] | 2545 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ | 
| Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 2546 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2547 | static void ssl_write_supported_point_formats_ext( mbedtls_ssl_context *ssl, | 
| Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 2548 | unsigned char *buf, | 
|  | 2549 | size_t *olen ) | 
|  | 2550 | { | 
|  | 2551 | unsigned char *p = buf; | 
|  | 2552 | ((void) ssl); | 
|  | 2553 |  | 
| Paul Bakker | 677377f | 2013-10-28 12:54:26 +0100 | [diff] [blame] | 2554 | if( ( ssl->handshake->cli_exts & | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2555 | MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 ) | 
| Paul Bakker | 677377f | 2013-10-28 12:54:26 +0100 | [diff] [blame] | 2556 | { | 
|  | 2557 | *olen = 0; | 
|  | 2558 | return; | 
|  | 2559 | } | 
| Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 2560 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2561 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) ); | 
| Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 2562 |  | 
| Joe Subbiani | 2f98d79 | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 2563 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, p, 0 ); | 
|  | 2564 | p += 2; | 
| Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 2565 |  | 
|  | 2566 | *p++ = 0x00; | 
|  | 2567 | *p++ = 2; | 
|  | 2568 |  | 
|  | 2569 | *p++ = 1; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2570 | *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED; | 
| Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 2571 |  | 
|  | 2572 | *olen = 6; | 
|  | 2573 | } | 
| Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 2574 | #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ | 
| Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 2575 |  | 
| Manuel Pégourié-Gonnard | 55c7f99 | 2015-09-16 15:35:27 +0200 | [diff] [blame] | 2576 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
|  | 2577 | static void ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl, | 
|  | 2578 | unsigned char *buf, | 
|  | 2579 | size_t *olen ) | 
|  | 2580 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2581 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | 55c7f99 | 2015-09-16 15:35:27 +0200 | [diff] [blame] | 2582 | unsigned char *p = buf; | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2583 | const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN; | 
| Manuel Pégourié-Gonnard | 55c7f99 | 2015-09-16 15:35:27 +0200 | [diff] [blame] | 2584 | size_t kkpp_len; | 
|  | 2585 |  | 
|  | 2586 | *olen = 0; | 
|  | 2587 |  | 
|  | 2588 | /* Skip costly computation if not needed */ | 
| Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 2589 | if( ssl->handshake->ciphersuite_info->key_exchange != | 
| Manuel Pégourié-Gonnard | 55c7f99 | 2015-09-16 15:35:27 +0200 | [diff] [blame] | 2590 | MBEDTLS_KEY_EXCHANGE_ECJPAKE ) | 
|  | 2591 | return; | 
|  | 2592 |  | 
|  | 2593 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, ecjpake kkpp extension" ) ); | 
|  | 2594 |  | 
|  | 2595 | if( end - p < 4 ) | 
|  | 2596 | { | 
|  | 2597 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) ); | 
|  | 2598 | return; | 
|  | 2599 | } | 
|  | 2600 |  | 
| Joe Subbiani | 2f98d79 | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 2601 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ECJPAKE_KKPP, p, 0 ); | 
|  | 2602 | p += 2; | 
| Manuel Pégourié-Gonnard | 55c7f99 | 2015-09-16 15:35:27 +0200 | [diff] [blame] | 2603 |  | 
| Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 2604 | ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx, | 
|  | 2605 | p + 2, end - p - 2, &kkpp_len, | 
|  | 2606 | ssl->conf->f_rng, ssl->conf->p_rng ); | 
|  | 2607 | if( ret != 0 ) | 
| Manuel Pégourié-Gonnard | 55c7f99 | 2015-09-16 15:35:27 +0200 | [diff] [blame] | 2608 | { | 
|  | 2609 | MBEDTLS_SSL_DEBUG_RET( 1 , "mbedtls_ecjpake_write_round_one", ret ); | 
|  | 2610 | return; | 
|  | 2611 | } | 
|  | 2612 |  | 
| Joe Subbiani | 2f98d79 | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 2613 | MBEDTLS_PUT_UINT16_BE( kkpp_len, p, 0 ); | 
|  | 2614 | p += 2; | 
| Manuel Pégourié-Gonnard | 55c7f99 | 2015-09-16 15:35:27 +0200 | [diff] [blame] | 2615 |  | 
|  | 2616 | *olen = kkpp_len + 4; | 
|  | 2617 | } | 
|  | 2618 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ | 
|  | 2619 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2620 | #if defined(MBEDTLS_SSL_ALPN ) | 
|  | 2621 | static void ssl_write_alpn_ext( mbedtls_ssl_context *ssl, | 
| Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 2622 | unsigned char *buf, size_t *olen ) | 
|  | 2623 | { | 
|  | 2624 | if( ssl->alpn_chosen == NULL ) | 
|  | 2625 | { | 
|  | 2626 | *olen = 0; | 
|  | 2627 | return; | 
|  | 2628 | } | 
|  | 2629 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2630 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) ); | 
| Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 2631 |  | 
|  | 2632 | /* | 
|  | 2633 | * 0 . 1    ext identifier | 
|  | 2634 | * 2 . 3    ext length | 
|  | 2635 | * 4 . 5    protocol list length | 
|  | 2636 | * 6 . 6    protocol name length | 
|  | 2637 | * 7 . 7+n  protocol name | 
|  | 2638 | */ | 
| Joe Subbiani | c54e908 | 2021-07-19 11:56:54 +0100 | [diff] [blame] | 2639 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, buf, 0); | 
| Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 2640 |  | 
|  | 2641 | *olen = 7 + strlen( ssl->alpn_chosen ); | 
|  | 2642 |  | 
| Joe Subbiani | c54e908 | 2021-07-19 11:56:54 +0100 | [diff] [blame] | 2643 | MBEDTLS_PUT_UINT16_BE( *olen - 4, buf, 2 ); | 
| Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 2644 |  | 
| Joe Subbiani | c54e908 | 2021-07-19 11:56:54 +0100 | [diff] [blame] | 2645 | MBEDTLS_PUT_UINT16_BE( *olen - 6, buf, 4 ); | 
| Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 2646 |  | 
| Joe Subbiani | c045dc1 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 2647 | buf[6] = MBEDTLS_BYTE_0( *olen - 7 ); | 
| Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 2648 |  | 
|  | 2649 | memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 ); | 
|  | 2650 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2651 | #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */ | 
| Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 2652 |  | 
| Ron Eldor | 3adb992 | 2017-12-21 10:15:08 +0200 | [diff] [blame] | 2653 | #if defined(MBEDTLS_SSL_DTLS_SRTP ) && defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2654 | static void ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl, | 
| Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 2655 | unsigned char *buf, | 
|  | 2656 | size_t *olen ) | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2657 | { | 
| Ron Eldor | 75870ec | 2018-12-06 17:31:55 +0200 | [diff] [blame] | 2658 | size_t mki_len = 0, ext_len = 0; | 
| Ron Eldor | 089c9fe | 2018-12-06 17:12:49 +0200 | [diff] [blame] | 2659 | uint16_t profile_value = 0; | 
| Johan Pascal | 8f70fba | 2020-09-02 10:32:06 +0200 | [diff] [blame] | 2660 | const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN; | 
|  | 2661 |  | 
|  | 2662 | *olen = 0; | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2663 |  | 
| Johan Pascal | c3ccd98 | 2020-10-28 17:18:18 +0100 | [diff] [blame] | 2664 | if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) || | 
|  | 2665 | ( ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET ) ) | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2666 | { | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2667 | return; | 
|  | 2668 | } | 
|  | 2669 |  | 
|  | 2670 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding use_srtp extension" ) ); | 
|  | 2671 |  | 
| Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2672 | 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] | 2673 | { | 
|  | 2674 | mki_len = ssl->dtls_srtp_info.mki_len; | 
|  | 2675 | } | 
|  | 2676 |  | 
| Johan Pascal | 9bc97ca | 2020-09-21 23:44:45 +0200 | [diff] [blame] | 2677 | /* The extension total size is 9 bytes : | 
|  | 2678 | * - 2 bytes for the extension tag | 
|  | 2679 | * - 2 bytes for the total size | 
|  | 2680 | * - 2 bytes for the protection profile length | 
|  | 2681 | * - 2 bytes for the protection profile | 
|  | 2682 | * - 1 byte for the mki length | 
|  | 2683 | * +  the actual mki length | 
|  | 2684 | * Check we have enough room in the output buffer */ | 
| Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2685 | if( (size_t)( end - buf ) < mki_len + 9 ) | 
| Johan Pascal | 8f70fba | 2020-09-02 10:32:06 +0200 | [diff] [blame] | 2686 | { | 
|  | 2687 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) ); | 
|  | 2688 | return; | 
|  | 2689 | } | 
|  | 2690 |  | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2691 | /* extension */ | 
| Joe Subbiani | c54e908 | 2021-07-19 11:56:54 +0100 | [diff] [blame] | 2692 | MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_USE_SRTP, buf, 0 ); | 
| Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 2693 | /* | 
|  | 2694 | * total length 5 and mki value: only one profile(2 bytes) | 
|  | 2695 | *              and length(2 bytes) and srtp_mki  ) | 
|  | 2696 | */ | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2697 | ext_len = 5 + mki_len; | 
| Joe Subbiani | c54e908 | 2021-07-19 11:56:54 +0100 | [diff] [blame] | 2698 | MBEDTLS_PUT_UINT16_BE( ext_len, buf, 2 ); | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2699 |  | 
|  | 2700 | /* protection profile length: 2 */ | 
|  | 2701 | buf[4] = 0x00; | 
|  | 2702 | buf[5] = 0x02; | 
| Johan Pascal | 43f9490 | 2020-09-22 12:25:52 +0200 | [diff] [blame] | 2703 | profile_value = mbedtls_ssl_check_srtp_profile_value( | 
| Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2704 | ssl->dtls_srtp_info.chosen_dtls_srtp_profile ); | 
| Johan Pascal | 43f9490 | 2020-09-22 12:25:52 +0200 | [diff] [blame] | 2705 | if( profile_value != MBEDTLS_TLS_SRTP_UNSET ) | 
| Ron Eldor | 089c9fe | 2018-12-06 17:12:49 +0200 | [diff] [blame] | 2706 | { | 
| Joe Subbiani | c54e908 | 2021-07-19 11:56:54 +0100 | [diff] [blame] | 2707 | MBEDTLS_PUT_UINT16_BE( profile_value, buf, 6 ); | 
| Ron Eldor | 089c9fe | 2018-12-06 17:12:49 +0200 | [diff] [blame] | 2708 | } | 
|  | 2709 | else | 
|  | 2710 | { | 
| Johan Pascal | 8f70fba | 2020-09-02 10:32:06 +0200 | [diff] [blame] | 2711 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "use_srtp extension invalid profile" ) ); | 
| Ron Eldor | 089c9fe | 2018-12-06 17:12:49 +0200 | [diff] [blame] | 2712 | return; | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2713 | } | 
|  | 2714 |  | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2715 | buf[8] = mki_len & 0xFF; | 
| Ron Eldor | 75870ec | 2018-12-06 17:31:55 +0200 | [diff] [blame] | 2716 | memcpy( &buf[9], ssl->dtls_srtp_info.mki_value, mki_len ); | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2717 |  | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2718 | *olen = 9 + mki_len; | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2719 | } | 
|  | 2720 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ | 
|  | 2721 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2722 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) | 
| Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 2723 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2724 | static int ssl_write_hello_verify_request( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2725 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2726 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2727 | unsigned char *p = ssl->out_msg + 4; | 
| Manuel Pégourié-Gonnard | d7f9bc5 | 2014-07-23 11:09:27 +0200 | [diff] [blame] | 2728 | unsigned char *cookie_len_byte; | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2729 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2730 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello verify request" ) ); | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2731 |  | 
|  | 2732 | /* | 
|  | 2733 | * struct { | 
|  | 2734 | *   ProtocolVersion server_version; | 
|  | 2735 | *   opaque cookie<0..2^8-1>; | 
|  | 2736 | * } HelloVerifyRequest; | 
|  | 2737 | */ | 
|  | 2738 |  | 
| Manuel Pégourié-Gonnard | b35fe56 | 2014-08-09 17:00:46 +0200 | [diff] [blame] | 2739 | /* The RFC is not clear on this point, but sending the actual negotiated | 
|  | 2740 | * version looks like the most interoperable thing to do. */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2741 | mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver, | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2742 | ssl->conf->transport, p ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2743 | MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 ); | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2744 | p += 2; | 
|  | 2745 |  | 
| Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 2746 | /* If we get here, f_cookie_check is not null */ | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2747 | if( ssl->conf->f_cookie_write == NULL ) | 
| Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 2748 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2749 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "inconsistent cookie callbacks" ) ); | 
|  | 2750 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 2751 | } | 
|  | 2752 |  | 
| Manuel Pégourié-Gonnard | d7f9bc5 | 2014-07-23 11:09:27 +0200 | [diff] [blame] | 2753 | /* Skip length byte until we know the length */ | 
|  | 2754 | cookie_len_byte = p++; | 
|  | 2755 |  | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2756 | if( ( ret = ssl->conf->f_cookie_write( ssl->conf->p_cookie, | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2757 | &p, ssl->out_buf + MBEDTLS_SSL_OUT_BUFFER_LEN, | 
| Manuel Pégourié-Gonnard | d485d19 | 2014-07-23 14:56:15 +0200 | [diff] [blame] | 2758 | ssl->cli_id, ssl->cli_id_len ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | d7f9bc5 | 2014-07-23 11:09:27 +0200 | [diff] [blame] | 2759 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2760 | MBEDTLS_SSL_DEBUG_RET( 1, "f_cookie_write", ret ); | 
| Manuel Pégourié-Gonnard | d7f9bc5 | 2014-07-23 11:09:27 +0200 | [diff] [blame] | 2761 | return( ret ); | 
|  | 2762 | } | 
|  | 2763 |  | 
|  | 2764 | *cookie_len_byte = (unsigned char)( p - ( cookie_len_byte + 1 ) ); | 
|  | 2765 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2766 | MBEDTLS_SSL_DEBUG_BUF( 3, "cookie sent", cookie_len_byte + 1, *cookie_len_byte ); | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2767 |  | 
|  | 2768 | ssl->out_msglen  = p - ssl->out_msg; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2769 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; | 
|  | 2770 | ssl->out_msg[0]  = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST; | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2771 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2772 | ssl->state = MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT; | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2773 |  | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2774 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2775 | { | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2776 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2777 | return( ret ); | 
|  | 2778 | } | 
|  | 2779 |  | 
| Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2780 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 2781 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | 
|  | 2782 | ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) | 
|  | 2783 | { | 
|  | 2784 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); | 
|  | 2785 | return( ret ); | 
|  | 2786 | } | 
| Hanno Becker | bc2498a | 2018-08-28 10:13:29 +0100 | [diff] [blame] | 2787 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2788 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2789 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello verify request" ) ); | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2790 |  | 
|  | 2791 | return( 0 ); | 
|  | 2792 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2793 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */ | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2794 |  | 
| Hanno Becker | 83e3671 | 2021-04-15 08:19:40 +0100 | [diff] [blame] | 2795 | static void ssl_handle_id_based_session_resumption( mbedtls_ssl_context *ssl ) | 
|  | 2796 | { | 
|  | 2797 | int ret; | 
|  | 2798 | mbedtls_ssl_session session_tmp; | 
|  | 2799 | mbedtls_ssl_session * const session = ssl->session_negotiate; | 
|  | 2800 |  | 
|  | 2801 | /* Resume is 0  by default, see ssl_handshake_init(). | 
|  | 2802 | * It may be already set to 1 by ssl_parse_session_ticket_ext(). */ | 
|  | 2803 | if( ssl->handshake->resume == 1 ) | 
|  | 2804 | return; | 
|  | 2805 | if( session->id_len == 0 ) | 
|  | 2806 | return; | 
|  | 2807 | if( ssl->conf->f_get_cache == NULL ) | 
|  | 2808 | return; | 
|  | 2809 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 2810 | if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ) | 
|  | 2811 | return; | 
|  | 2812 | #endif | 
|  | 2813 |  | 
|  | 2814 | mbedtls_ssl_session_init( &session_tmp ); | 
|  | 2815 |  | 
|  | 2816 | session_tmp.id_len = session->id_len; | 
|  | 2817 | memcpy( session_tmp.id, session->id, session->id_len ); | 
|  | 2818 |  | 
|  | 2819 | ret = ssl->conf->f_get_cache( ssl->conf->p_cache, | 
|  | 2820 | &session_tmp ); | 
|  | 2821 | if( ret != 0 ) | 
|  | 2822 | goto exit; | 
|  | 2823 |  | 
|  | 2824 | if( session->ciphersuite != session_tmp.ciphersuite || | 
|  | 2825 | session->compression != session_tmp.compression ) | 
|  | 2826 | { | 
|  | 2827 | /* Mismatch between cached and negotiated session */ | 
|  | 2828 | goto exit; | 
|  | 2829 | } | 
|  | 2830 |  | 
|  | 2831 | /* Move semantics */ | 
|  | 2832 | mbedtls_ssl_session_free( session ); | 
|  | 2833 | *session = session_tmp; | 
|  | 2834 | memset( &session_tmp, 0, sizeof( session_tmp ) ); | 
|  | 2835 |  | 
|  | 2836 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) ); | 
|  | 2837 | ssl->handshake->resume = 1; | 
|  | 2838 |  | 
|  | 2839 | exit: | 
|  | 2840 |  | 
|  | 2841 | mbedtls_ssl_session_free( &session_tmp ); | 
|  | 2842 | } | 
|  | 2843 |  | 
| Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 2844 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2845 | static int ssl_write_server_hello( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2846 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2847 | #if defined(MBEDTLS_HAVE_TIME) | 
| SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 2848 | mbedtls_time_t t; | 
| Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 2849 | #endif | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2850 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 2851 | size_t olen, ext_len = 0, n; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2852 | unsigned char *buf, *p; | 
|  | 2853 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2854 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2855 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2856 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2857 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | 
| Manuel Pégourié-Gonnard | d7f9bc5 | 2014-07-23 11:09:27 +0200 | [diff] [blame] | 2858 | ssl->handshake->verify_cookie_len != 0 ) | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2859 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2860 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "client hello was not authenticated" ) ); | 
|  | 2861 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) ); | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2862 |  | 
| Manuel Pégourié-Gonnard | 579950c | 2014-09-29 17:47:33 +0200 | [diff] [blame] | 2863 | return( ssl_write_hello_verify_request( ssl ) ); | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2864 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2865 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */ | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2866 |  | 
| Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 2867 | if( ssl->conf->f_rng == NULL ) | 
| Paul Bakker | a9a028e | 2013-11-21 17:31:06 +0100 | [diff] [blame] | 2868 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2869 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided") ); | 
|  | 2870 | return( MBEDTLS_ERR_SSL_NO_RNG ); | 
| Paul Bakker | a9a028e | 2013-11-21 17:31:06 +0100 | [diff] [blame] | 2871 | } | 
|  | 2872 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2873 | /* | 
|  | 2874 | *     0  .   0   handshake type | 
|  | 2875 | *     1  .   3   handshake length | 
|  | 2876 | *     4  .   5   protocol version | 
|  | 2877 | *     6  .   9   UNIX time() | 
|  | 2878 | *    10  .  37   random bytes | 
|  | 2879 | */ | 
|  | 2880 | buf = ssl->out_msg; | 
|  | 2881 | p = buf + 4; | 
|  | 2882 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2883 | mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver, | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2884 | ssl->conf->transport, p ); | 
| Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 2885 | p += 2; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2886 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2887 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]", | 
| Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 2888 | buf[4], buf[5] ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2889 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2890 | #if defined(MBEDTLS_HAVE_TIME) | 
| SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 2891 | t = mbedtls_time( NULL ); | 
| Joe Subbiani | 2f98d79 | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 2892 | MBEDTLS_PUT_UINT32_BE( t, p, 0 ); | 
|  | 2893 | p += 4; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2894 |  | 
| Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 2895 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %" MBEDTLS_PRINTF_LONGLONG, | 
|  | 2896 | (long long) t ) ); | 
| Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 2897 | #else | 
| Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 2898 | if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 4 ) ) != 0 ) | 
| Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 2899 | return( ret ); | 
|  | 2900 |  | 
|  | 2901 | p += 4; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2902 | #endif /* MBEDTLS_HAVE_TIME */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2903 |  | 
| Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 2904 | if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 28 ) ) != 0 ) | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 2905 | return( ret ); | 
|  | 2906 |  | 
|  | 2907 | p += 28; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2908 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2909 | memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2910 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2911 | MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2912 |  | 
| Hanno Becker | 83e3671 | 2021-04-15 08:19:40 +0100 | [diff] [blame] | 2913 | ssl_handle_id_based_session_resumption( ssl ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2914 |  | 
| Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2915 | if( ssl->handshake->resume == 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2916 | { | 
|  | 2917 | /* | 
| Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2918 | * New session, create a new session id, | 
|  | 2919 | * unless we're about to issue a session ticket | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2920 | */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2921 | ssl->state++; | 
|  | 2922 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2923 | #if defined(MBEDTLS_HAVE_TIME) | 
| SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 2924 | ssl->session_negotiate->start = mbedtls_time( NULL ); | 
| Manuel Pégourié-Gonnard | 164d894 | 2013-09-23 22:01:39 +0200 | [diff] [blame] | 2925 | #endif | 
|  | 2926 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2927 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | 
| Manuel Pégourié-Gonnard | 164d894 | 2013-09-23 22:01:39 +0200 | [diff] [blame] | 2928 | if( ssl->handshake->new_session_ticket != 0 ) | 
|  | 2929 | { | 
| Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 2930 | ssl->session_negotiate->id_len = n = 0; | 
| Manuel Pégourié-Gonnard | 164d894 | 2013-09-23 22:01:39 +0200 | [diff] [blame] | 2931 | memset( ssl->session_negotiate->id, 0, 32 ); | 
|  | 2932 | } | 
|  | 2933 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2934 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ | 
| Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2935 | { | 
| Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 2936 | ssl->session_negotiate->id_len = n = 32; | 
| Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 2937 | if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->session_negotiate->id, | 
| Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 2938 | n ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2939 | return( ret ); | 
|  | 2940 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2941 | } | 
|  | 2942 | else | 
|  | 2943 | { | 
|  | 2944 | /* | 
| Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2945 | * Resuming a session | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2946 | */ | 
| Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 2947 | n = ssl->session_negotiate->id_len; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2948 | ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC; | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 2949 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2950 | if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 ) | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 2951 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2952 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret ); | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 2953 | return( ret ); | 
|  | 2954 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2955 | } | 
|  | 2956 |  | 
| Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2957 | /* | 
|  | 2958 | *    38  .  38     session id length | 
|  | 2959 | *    39  . 38+n    session id | 
|  | 2960 | *   39+n . 40+n    chosen ciphersuite | 
|  | 2961 | *   41+n . 41+n    chosen compression alg. | 
|  | 2962 | *   42+n . 43+n    extensions length | 
|  | 2963 | *   44+n . 43+n+m  extensions | 
|  | 2964 | */ | 
| Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 2965 | *p++ = (unsigned char) ssl->session_negotiate->id_len; | 
|  | 2966 | memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len ); | 
|  | 2967 | p += ssl->session_negotiate->id_len; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2968 |  | 
| Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 2969 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2970 | MBEDTLS_SSL_DEBUG_BUF( 3,   "server hello, session id", buf + 39, n ); | 
|  | 2971 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed", | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 2972 | ssl->handshake->resume ? "a" : "no" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2973 |  | 
| Joe Subbiani | 2f98d79 | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 2974 | MBEDTLS_PUT_UINT16_BE( ssl->session_negotiate->ciphersuite, p, 0 ); | 
|  | 2975 | p += 2; | 
| Joe Subbiani | ad1115a | 2021-07-16 14:27:50 +0100 | [diff] [blame] | 2976 | *p++ = MBEDTLS_BYTE_0( ssl->session_negotiate->compression ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2977 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2978 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s", | 
|  | 2979 | mbedtls_ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) ); | 
|  | 2980 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X", | 
| Paul Elliott | 3891caf | 2020-12-17 18:42:40 +0000 | [diff] [blame] | 2981 | (unsigned int) ssl->session_negotiate->compression ) ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2982 |  | 
| Janos Follath | c6dab2b | 2016-05-23 14:27:02 +0100 | [diff] [blame] | 2983 | /* Do not write the extensions if the protocol is SSLv3 */ | 
|  | 2984 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
|  | 2985 | if( ( ssl->major_ver != 3 ) || ( ssl->minor_ver != 0 ) ) | 
|  | 2986 | { | 
|  | 2987 | #endif | 
|  | 2988 |  | 
| Manuel Pégourié-Gonnard | f11a6d7 | 2013-07-17 11:17:14 +0200 | [diff] [blame] | 2989 | /* | 
|  | 2990 | *  First write extensions, then the total length | 
|  | 2991 | */ | 
|  | 2992 | ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen ); | 
|  | 2993 | ext_len += olen; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2994 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2995 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
| Manuel Pégourié-Gonnard | 7bb7899 | 2013-07-17 13:50:08 +0200 | [diff] [blame] | 2996 | ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen ); | 
|  | 2997 | ext_len += olen; | 
| Paul Bakker | 05decb2 | 2013-08-15 13:33:48 +0200 | [diff] [blame] | 2998 | #endif | 
| Manuel Pégourié-Gonnard | 7bb7899 | 2013-07-17 13:50:08 +0200 | [diff] [blame] | 2999 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3000 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) | 
| Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 3001 | ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen ); | 
|  | 3002 | ext_len += olen; | 
| Paul Bakker | 1f2bc62 | 2013-08-15 13:45:55 +0200 | [diff] [blame] | 3003 | #endif | 
| Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 3004 |  | 
| Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3005 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
| Hanno Becker | 51de2d3 | 2019-04-26 15:46:55 +0100 | [diff] [blame] | 3006 | ssl_write_cid_ext( ssl, p + 2 + ext_len, &olen ); | 
|  | 3007 | ext_len += olen; | 
|  | 3008 | #endif | 
|  | 3009 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3010 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 3011 | ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen ); | 
|  | 3012 | ext_len += olen; | 
|  | 3013 | #endif | 
|  | 3014 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3015 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 3016 | ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen ); | 
|  | 3017 | ext_len += olen; | 
|  | 3018 | #endif | 
|  | 3019 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3020 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | 
| Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 3021 | ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen ); | 
|  | 3022 | ext_len += olen; | 
| Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 3023 | #endif | 
| Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 3024 |  | 
| Manuel Pégourié-Gonnard | f472179 | 2015-09-15 10:53:51 +0200 | [diff] [blame] | 3025 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ | 
| Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 3026 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Ron Eldor | 755bb6a | 2018-02-14 19:30:48 +0200 | [diff] [blame] | 3027 | if ( mbedtls_ssl_ciphersuite_uses_ec( | 
|  | 3028 | mbedtls_ssl_ciphersuite_from_id( ssl->session_negotiate->ciphersuite ) ) ) | 
|  | 3029 | { | 
|  | 3030 | ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen ); | 
|  | 3031 | ext_len += olen; | 
|  | 3032 | } | 
| Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 3033 | #endif | 
|  | 3034 |  | 
| Manuel Pégourié-Gonnard | 55c7f99 | 2015-09-16 15:35:27 +0200 | [diff] [blame] | 3035 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
|  | 3036 | ssl_write_ecjpake_kkpp_ext( ssl, p + 2 + ext_len, &olen ); | 
|  | 3037 | ext_len += olen; | 
|  | 3038 | #endif | 
|  | 3039 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3040 | #if defined(MBEDTLS_SSL_ALPN) | 
| Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 3041 | ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen ); | 
|  | 3042 | ext_len += olen; | 
|  | 3043 | #endif | 
|  | 3044 |  | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3045 | #if defined(MBEDTLS_SSL_DTLS_SRTP) | 
| Johan Pascal | c3ccd98 | 2020-10-28 17:18:18 +0100 | [diff] [blame] | 3046 | ssl_write_use_srtp_ext( ssl, p + 2 + ext_len, &olen ); | 
|  | 3047 | ext_len += olen; | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3048 | #endif | 
|  | 3049 |  | 
| Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 3050 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %" MBEDTLS_PRINTF_SIZET, | 
|  | 3051 | ext_len ) ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3052 |  | 
| Paul Bakker | a703663 | 2014-04-30 10:15:38 +0200 | [diff] [blame] | 3053 | if( ext_len > 0 ) | 
|  | 3054 | { | 
| Joe Subbiani | 2f98d79 | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3055 | MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 ); | 
| Joe Subbiani | 24647c5 | 2021-08-20 15:56:22 +0100 | [diff] [blame] | 3056 | p += 2 + ext_len; | 
| Paul Bakker | a703663 | 2014-04-30 10:15:38 +0200 | [diff] [blame] | 3057 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3058 |  | 
| Janos Follath | c6dab2b | 2016-05-23 14:27:02 +0100 | [diff] [blame] | 3059 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
|  | 3060 | } | 
|  | 3061 | #endif | 
|  | 3062 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3063 | ssl->out_msglen  = p - buf; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3064 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; | 
|  | 3065 | ssl->out_msg[0]  = MBEDTLS_SSL_HS_SERVER_HELLO; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3066 |  | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3067 | ret = mbedtls_ssl_write_handshake_msg( ssl ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3068 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3069 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3070 |  | 
|  | 3071 | return( ret ); | 
|  | 3072 | } | 
|  | 3073 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3074 | #if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED) | 
| Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 3075 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3076 | static int ssl_write_certificate_request( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3077 | { | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3078 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | 
| Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 3079 | ssl->handshake->ciphersuite_info; | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3080 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3081 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3082 |  | 
| Hanno Becker | 77adddc | 2019-02-07 12:32:43 +0000 | [diff] [blame] | 3083 | if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ) | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3084 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3085 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3086 | ssl->state++; | 
|  | 3087 | return( 0 ); | 
|  | 3088 | } | 
|  | 3089 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3090 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 3091 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3092 | } | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3093 | #else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */ | 
| Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 3094 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3095 | static int ssl_write_certificate_request( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3096 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3097 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3098 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | 
| Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 3099 | ssl->handshake->ciphersuite_info; | 
| irwir | c9bc300 | 2020-04-01 13:46:36 +0300 | [diff] [blame] | 3100 | uint16_t dn_size, total_dn_size; /* excluding length bytes */ | 
| Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3101 | size_t ct_len, sa_len; /* including length bytes */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3102 | unsigned char *buf, *p; | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3103 | const unsigned char * const end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3104 | const mbedtls_x509_crt *crt; | 
| Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 3105 | int authmode; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3106 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3107 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3108 |  | 
|  | 3109 | ssl->state++; | 
|  | 3110 |  | 
| Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 3111 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
|  | 3112 | if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET ) | 
|  | 3113 | authmode = ssl->handshake->sni_authmode; | 
|  | 3114 | else | 
|  | 3115 | #endif | 
|  | 3116 | authmode = ssl->conf->authmode; | 
|  | 3117 |  | 
| Hanno Becker | 77adddc | 2019-02-07 12:32:43 +0000 | [diff] [blame] | 3118 | if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) || | 
| Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 3119 | authmode == MBEDTLS_SSL_VERIFY_NONE ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3120 | { | 
| Johan Pascal | 4f09926 | 2020-09-22 10:59:26 +0200 | [diff] [blame] | 3121 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) ); | 
|  | 3122 | return( 0 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3123 | } | 
|  | 3124 |  | 
|  | 3125 | /* | 
|  | 3126 | *     0  .   0   handshake type | 
|  | 3127 | *     1  .   3   handshake length | 
|  | 3128 | *     4  .   4   cert type count | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3129 | *     5  .. m-1  cert types | 
|  | 3130 | *     m  .. m+1  sig alg length (TLS 1.2 only) | 
| Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 3131 | *    m+1 .. n-1  SignatureAndHashAlgorithms (TLS 1.2 only) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3132 | *     n  .. n+1  length of all DNs | 
|  | 3133 | *    n+2 .. n+3  length of DN 1 | 
|  | 3134 | *    n+4 .. ...  Distinguished Name #1 | 
|  | 3135 | *    ... .. ...  length of DN 2, etc. | 
|  | 3136 | */ | 
|  | 3137 | buf = ssl->out_msg; | 
|  | 3138 | p = buf + 4; | 
|  | 3139 |  | 
|  | 3140 | /* | 
| Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3141 | * Supported certificate types | 
|  | 3142 | * | 
|  | 3143 | *     ClientCertificateType certificate_types<1..2^8-1>; | 
|  | 3144 | *     enum { (255) } ClientCertificateType; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3145 | */ | 
| Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3146 | ct_len = 0; | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3147 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3148 | #if defined(MBEDTLS_RSA_C) | 
|  | 3149 | p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_RSA_SIGN; | 
| Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3150 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3151 | #if defined(MBEDTLS_ECDSA_C) | 
|  | 3152 | p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_ECDSA_SIGN; | 
| Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3153 | #endif | 
|  | 3154 |  | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 3155 | p[0] = (unsigned char) ct_len++; | 
| Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3156 | p += ct_len; | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3157 |  | 
| Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 3158 | sa_len = 0; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3159 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3160 | /* | 
|  | 3161 | * Add signature_algorithms for verify (TLS 1.2) | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3162 | * | 
| Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3163 | *     SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>; | 
|  | 3164 | * | 
|  | 3165 | *     struct { | 
|  | 3166 | *           HashAlgorithm hash; | 
|  | 3167 | *           SignatureAlgorithm signature; | 
|  | 3168 | *     } SignatureAndHashAlgorithm; | 
|  | 3169 | * | 
|  | 3170 | *     enum { (255) } HashAlgorithm; | 
|  | 3171 | *     enum { (255) } SignatureAlgorithm; | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3172 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3173 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3174 | { | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3175 | const int *cur; | 
| Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 3176 |  | 
| Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3177 | /* | 
|  | 3178 | * Supported signature algorithms | 
|  | 3179 | */ | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3180 | for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ ) | 
|  | 3181 | { | 
|  | 3182 | unsigned char hash = mbedtls_ssl_hash_from_md_alg( *cur ); | 
|  | 3183 |  | 
|  | 3184 | if( MBEDTLS_SSL_HASH_NONE == hash || mbedtls_ssl_set_calc_verify_md( ssl, hash ) ) | 
|  | 3185 | continue; | 
|  | 3186 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3187 | #if defined(MBEDTLS_RSA_C) | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3188 | p[2 + sa_len++] = hash; | 
|  | 3189 | p[2 + sa_len++] = MBEDTLS_SSL_SIG_RSA; | 
| Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3190 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3191 | #if defined(MBEDTLS_ECDSA_C) | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3192 | p[2 + sa_len++] = hash; | 
|  | 3193 | p[2 + sa_len++] = MBEDTLS_SSL_SIG_ECDSA; | 
| Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3194 | #endif | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3195 | } | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3196 |  | 
| Joe Subbiani | c54e908 | 2021-07-19 11:56:54 +0100 | [diff] [blame] | 3197 | MBEDTLS_PUT_UINT16_BE( sa_len, p, 0 ); | 
| Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3198 | sa_len += 2; | 
|  | 3199 | p += sa_len; | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3200 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3201 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3202 |  | 
| Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3203 | /* | 
|  | 3204 | * DistinguishedName certificate_authorities<0..2^16-1>; | 
|  | 3205 | * opaque DistinguishedName<1..2^16-1>; | 
|  | 3206 | */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3207 | p += 2; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3208 |  | 
| Paul Bakker | bc3d984 | 2012-11-26 16:12:02 +0100 | [diff] [blame] | 3209 | total_dn_size = 0; | 
| Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 3210 |  | 
|  | 3211 | if( ssl->conf->cert_req_ca_list ==  MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3212 | { | 
| Hanno Becker | 8bf74f3 | 2019-03-27 11:01:30 +0000 | [diff] [blame] | 3213 | /* NOTE: If trusted certificates are provisioned | 
|  | 3214 | *       via a CA callback (configured through | 
|  | 3215 | *       `mbedtls_ssl_conf_ca_cb()`, then the | 
|  | 3216 | *       CertificateRequest is currently left empty. */ | 
|  | 3217 |  | 
| Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 3218 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
|  | 3219 | if( ssl->handshake->sni_ca_chain != NULL ) | 
|  | 3220 | crt = ssl->handshake->sni_ca_chain; | 
|  | 3221 | else | 
|  | 3222 | #endif | 
|  | 3223 | crt = ssl->conf->ca_chain; | 
| Manuel Pégourié-Gonnard | bc1babb | 2015-10-02 11:16:47 +0200 | [diff] [blame] | 3224 |  | 
| Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 3225 | while( crt != NULL && crt->version != 0 ) | 
| Manuel Pégourié-Gonnard | bc1babb | 2015-10-02 11:16:47 +0200 | [diff] [blame] | 3226 | { | 
| irwir | c9bc300 | 2020-04-01 13:46:36 +0300 | [diff] [blame] | 3227 | /* It follows from RFC 5280 A.1 that this length | 
|  | 3228 | * can be represented in at most 11 bits. */ | 
|  | 3229 | dn_size = (uint16_t) crt->subject_raw.len; | 
| Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 3230 |  | 
| irwir | c9bc300 | 2020-04-01 13:46:36 +0300 | [diff] [blame] | 3231 | if( end < p || (size_t)( end - p ) < 2 + (size_t) dn_size ) | 
| Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 3232 | { | 
|  | 3233 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "skipping CAs: buffer too short" ) ); | 
|  | 3234 | break; | 
|  | 3235 | } | 
|  | 3236 |  | 
| Joe Subbiani | 2f98d79 | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3237 | MBEDTLS_PUT_UINT16_BE( dn_size, p, 0 ); | 
|  | 3238 | p += 2; | 
| Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 3239 | memcpy( p, crt->subject_raw.p, dn_size ); | 
|  | 3240 | p += dn_size; | 
|  | 3241 |  | 
|  | 3242 | MBEDTLS_SSL_DEBUG_BUF( 3, "requested DN", p - dn_size, dn_size ); | 
|  | 3243 |  | 
|  | 3244 | total_dn_size += 2 + dn_size; | 
|  | 3245 | crt = crt->next; | 
| Manuel Pégourié-Gonnard | bc1babb | 2015-10-02 11:16:47 +0200 | [diff] [blame] | 3246 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3247 | } | 
|  | 3248 |  | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3249 | ssl->out_msglen  = p - buf; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3250 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; | 
|  | 3251 | ssl->out_msg[0]  = MBEDTLS_SSL_HS_CERTIFICATE_REQUEST; | 
| Joe Subbiani | c54e908 | 2021-07-19 11:56:54 +0100 | [diff] [blame] | 3252 | MBEDTLS_PUT_UINT16_BE( total_dn_size, ssl->out_msg, 4 + ct_len + sa_len ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3253 |  | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3254 | ret = mbedtls_ssl_write_handshake_msg( ssl ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3255 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3256 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3257 |  | 
|  | 3258 | return( ret ); | 
|  | 3259 | } | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3260 | #endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3261 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3262 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ | 
|  | 3263 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) | 
| Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 3264 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3265 | static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | 5538970 | 2013-12-12 11:14:16 +0100 | [diff] [blame] | 3266 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3267 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | 37e5999 | 2022-06-10 09:25:01 +0200 | [diff] [blame] | 3268 | mbedtls_pk_context *own_key = mbedtls_ssl_own_key( ssl ); | 
| Manuel Pégourié-Gonnard | 5538970 | 2013-12-12 11:14:16 +0100 | [diff] [blame] | 3269 |  | 
| Manuel Pégourié-Gonnard | b9c7ea4 | 2022-06-14 09:25:17 +0200 | [diff] [blame] | 3270 | /* Check if the key is a transparent ECDH key. | 
|  | 3271 | * This also ensures that it is safe to call mbedtls_pk_ec(). */ | 
| Manuel Pégourié-Gonnard | 37e5999 | 2022-06-10 09:25:01 +0200 | [diff] [blame] | 3272 | if( mbedtls_pk_get_type( own_key ) != MBEDTLS_PK_ECKEY && | 
|  | 3273 | mbedtls_pk_get_type( own_key ) != MBEDTLS_PK_ECKEY_DH ) | 
| Manuel Pégourié-Gonnard | 5538970 | 2013-12-12 11:14:16 +0100 | [diff] [blame] | 3274 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3275 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) ); | 
|  | 3276 | return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH ); | 
| Manuel Pégourié-Gonnard | 5538970 | 2013-12-12 11:14:16 +0100 | [diff] [blame] | 3277 | } | 
|  | 3278 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3279 | if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx, | 
| Manuel Pégourié-Gonnard | 37e5999 | 2022-06-10 09:25:01 +0200 | [diff] [blame] | 3280 | mbedtls_pk_ec( *own_key ), | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3281 | MBEDTLS_ECDH_OURS ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 5538970 | 2013-12-12 11:14:16 +0100 | [diff] [blame] | 3282 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3283 | MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret ); | 
| Manuel Pégourié-Gonnard | 5538970 | 2013-12-12 11:14:16 +0100 | [diff] [blame] | 3284 | return( ret ); | 
|  | 3285 | } | 
|  | 3286 |  | 
|  | 3287 | return( 0 ); | 
|  | 3288 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3289 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || | 
|  | 3290 | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */ | 
| Manuel Pégourié-Gonnard | 5538970 | 2013-12-12 11:14:16 +0100 | [diff] [blame] | 3291 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3292 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) && \ | 
| Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3293 | defined(MBEDTLS_SSL_ASYNC_PRIVATE) | 
| Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 3294 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3295 | static int ssl_resume_server_key_exchange( mbedtls_ssl_context *ssl, | 
| Gilles Peskine | 0fd90dd | 2018-04-26 07:41:09 +0200 | [diff] [blame] | 3296 | size_t *signature_len ) | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3297 | { | 
| Gilles Peskine | 0fd90dd | 2018-04-26 07:41:09 +0200 | [diff] [blame] | 3298 | /* Append the signature to ssl->out_msg, leaving 2 bytes for the | 
|  | 3299 | * signature length which will be added in ssl_write_server_key_exchange | 
|  | 3300 | * after the call to ssl_prepare_server_key_exchange. | 
|  | 3301 | * ssl_write_server_key_exchange also takes care of incrementing | 
|  | 3302 | * ssl->out_msglen. */ | 
|  | 3303 | unsigned char *sig_start = ssl->out_msg + ssl->out_msglen + 2; | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3304 | size_t sig_max_len = ( ssl->out_buf + MBEDTLS_SSL_OUT_CONTENT_LEN | 
| Gilles Peskine | 0fd90dd | 2018-04-26 07:41:09 +0200 | [diff] [blame] | 3305 | - sig_start ); | 
| Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 3306 | int ret = ssl->conf->f_async_resume( ssl, | 
| Gilles Peskine | 0fd90dd | 2018-04-26 07:41:09 +0200 | [diff] [blame] | 3307 | sig_start, signature_len, sig_max_len ); | 
| Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3308 | if( ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS ) | 
|  | 3309 | { | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3310 | ssl->handshake->async_in_progress = 0; | 
| Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 3311 | mbedtls_ssl_set_async_operation_data( ssl, NULL ); | 
| Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3312 | } | 
| Gilles Peskine | d3eb061 | 2018-01-08 17:07:44 +0100 | [diff] [blame] | 3313 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl_resume_server_key_exchange", ret ); | 
| Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3314 | return( ret ); | 
|  | 3315 | } | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3316 | #endif /* defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) && | 
| Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3317 | defined(MBEDTLS_SSL_ASYNC_PRIVATE) */ | 
| Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3318 |  | 
| Gilles Peskine | d3eb061 | 2018-01-08 17:07:44 +0100 | [diff] [blame] | 3319 | /* Prepare the ServerKeyExchange message, up to and including | 
| Gilles Peskine | 168dae8 | 2018-04-25 23:35:42 +0200 | [diff] [blame] | 3320 | * calculating the signature if any, but excluding formatting the | 
|  | 3321 | * signature and sending the message. */ | 
| Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 3322 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 7ab013a | 2018-01-08 17:04:16 +0100 | [diff] [blame] | 3323 | static int ssl_prepare_server_key_exchange( mbedtls_ssl_context *ssl, | 
|  | 3324 | size_t *signature_len ) | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 3325 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3326 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | 
| Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 3327 | ssl->handshake->ciphersuite_info; | 
|  | 3328 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3329 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PFS_ENABLED) | 
|  | 3330 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) | 
| Gilles Peskine | 3ce9b90 | 2018-01-06 01:34:21 +0100 | [diff] [blame] | 3331 | unsigned char *dig_signed = NULL; | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3332 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */ | 
|  | 3333 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PFS_ENABLED */ | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3334 |  | 
| Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3335 | (void) ciphersuite_info; /* unused in some configurations */ | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3336 | #if !defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) | 
| Gilles Peskine | 22e695f | 2018-04-26 00:22:50 +0200 | [diff] [blame] | 3337 | (void) signature_len; | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3338 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */ | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3339 |  | 
| Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 3340 | ssl->out_msglen = 4; /* header (type:1, length:3) to be written later */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3341 |  | 
| Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3342 | /* | 
|  | 3343 | * | 
| Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3344 | * Part 1: Provide key exchange parameters for chosen ciphersuite. | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3345 | * | 
|  | 3346 | */ | 
|  | 3347 |  | 
|  | 3348 | /* | 
|  | 3349 | * - ECJPAKE key exchanges | 
|  | 3350 | */ | 
| Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 3351 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
|  | 3352 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ) | 
|  | 3353 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3354 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Simon Butcher | 600c5e6 | 2018-06-14 08:58:59 +0100 | [diff] [blame] | 3355 | size_t len = 0; | 
| Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 3356 |  | 
| Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 3357 | ret = mbedtls_ecjpake_write_round_two( | 
|  | 3358 | &ssl->handshake->ecjpake_ctx, | 
|  | 3359 | ssl->out_msg + ssl->out_msglen, | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3360 | MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen, &len, | 
| Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 3361 | ssl->conf->f_rng, ssl->conf->p_rng ); | 
| Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 3362 | if( ret != 0 ) | 
|  | 3363 | { | 
|  | 3364 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret ); | 
|  | 3365 | return( ret ); | 
|  | 3366 | } | 
|  | 3367 |  | 
| Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 3368 | ssl->out_msglen += len; | 
| Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 3369 | } | 
|  | 3370 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ | 
|  | 3371 |  | 
| Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3372 | /* | 
|  | 3373 | * For (EC)DHE key exchanges with PSK, parameters are prefixed by support | 
|  | 3374 | * identity hint (RFC 4279, Sec. 3). Until someone needs this feature, | 
|  | 3375 | * we use empty support identity hints here. | 
|  | 3376 | **/ | 
|  | 3377 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)   || \ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3378 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) | 
|  | 3379 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK || | 
|  | 3380 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3381 | { | 
| Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 3382 | ssl->out_msg[ssl->out_msglen++] = 0x00; | 
|  | 3383 | ssl->out_msg[ssl->out_msglen++] = 0x00; | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3384 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3385 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED || | 
|  | 3386 | MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3387 |  | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 3388 | /* | 
| Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3389 | * - DHE key exchanges | 
| Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3390 | */ | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3391 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_DHE_ENABLED) | 
| Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3392 | if( mbedtls_ssl_ciphersuite_uses_dhe( ciphersuite_info ) ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3393 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3394 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Simon Butcher | 600c5e6 | 2018-06-14 08:58:59 +0100 | [diff] [blame] | 3395 | size_t len = 0; | 
| Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3396 |  | 
| Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 3397 | if( ssl->conf->dhm_P.p == NULL || ssl->conf->dhm_G.p == NULL ) | 
|  | 3398 | { | 
|  | 3399 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no DH parameters set" ) ); | 
|  | 3400 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 3401 | } | 
|  | 3402 |  | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3403 | /* | 
|  | 3404 | * Ephemeral DH parameters: | 
|  | 3405 | * | 
|  | 3406 | * struct { | 
|  | 3407 | *     opaque dh_p<1..2^16-1>; | 
|  | 3408 | *     opaque dh_g<1..2^16-1>; | 
|  | 3409 | *     opaque dh_Ys<1..2^16-1>; | 
|  | 3410 | * } ServerDHParams; | 
|  | 3411 | */ | 
| Hanno Becker | ab74056 | 2017-10-04 13:15:37 +0100 | [diff] [blame] | 3412 | if( ( ret = mbedtls_dhm_set_group( &ssl->handshake->dhm_ctx, | 
|  | 3413 | &ssl->conf->dhm_P, | 
|  | 3414 | &ssl->conf->dhm_G ) ) != 0 ) | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3415 | { | 
| Hanno Becker | ab74056 | 2017-10-04 13:15:37 +0100 | [diff] [blame] | 3416 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_set_group", ret ); | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3417 | return( ret ); | 
|  | 3418 | } | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3419 |  | 
| Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 3420 | if( ( ret = mbedtls_dhm_make_params( | 
|  | 3421 | &ssl->handshake->dhm_ctx, | 
|  | 3422 | (int) mbedtls_mpi_size( &ssl->handshake->dhm_ctx.P ), | 
|  | 3423 | ssl->out_msg + ssl->out_msglen, &len, | 
|  | 3424 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3425 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3426 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_params", ret ); | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3427 | return( ret ); | 
|  | 3428 | } | 
|  | 3429 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3430 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) | 
| Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 3431 | dig_signed = ssl->out_msg + ssl->out_msglen; | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3432 | #endif | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3433 |  | 
| Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 3434 | ssl->out_msglen += len; | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3435 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3436 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X  ); | 
|  | 3437 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P  ); | 
|  | 3438 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G  ); | 
|  | 3439 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX ); | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3440 | } | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3441 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_DHE_ENABLED */ | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3442 |  | 
| Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3443 | /* | 
| Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3444 | * - ECDHE key exchanges | 
| Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3445 | */ | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3446 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED) | 
| Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3447 | if( mbedtls_ssl_ciphersuite_uses_ecdhe( ciphersuite_info ) ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3448 | { | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3449 | /* | 
|  | 3450 | * Ephemeral ECDH parameters: | 
|  | 3451 | * | 
|  | 3452 | * struct { | 
|  | 3453 | *     ECParameters curve_params; | 
|  | 3454 | *     ECPoint      public; | 
|  | 3455 | * } ServerECDHParams; | 
|  | 3456 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3457 | const mbedtls_ecp_curve_info **curve = NULL; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3458 | const mbedtls_ecp_group_id *gid; | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3459 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Simon Butcher | 600c5e6 | 2018-06-14 08:58:59 +0100 | [diff] [blame] | 3460 | size_t len = 0; | 
| Gergely Budai | 987bfb5 | 2014-01-19 21:48:42 +0100 | [diff] [blame] | 3461 |  | 
| Manuel Pégourié-Gonnard | c3f6b62c | 2014-02-06 10:13:09 +0100 | [diff] [blame] | 3462 | /* Match our preference list against the offered curves */ | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3463 | for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ ) | 
| Manuel Pégourié-Gonnard | c3f6b62c | 2014-02-06 10:13:09 +0100 | [diff] [blame] | 3464 | for( curve = ssl->handshake->curves; *curve != NULL; curve++ ) | 
|  | 3465 | if( (*curve)->grp_id == *gid ) | 
|  | 3466 | goto curve_matching_done; | 
|  | 3467 |  | 
|  | 3468 | curve_matching_done: | 
| Manuel Pégourié-Gonnard | b86145e | 2015-06-23 14:11:39 +0200 | [diff] [blame] | 3469 | if( curve == NULL || *curve == NULL ) | 
| Gergely Budai | 987bfb5 | 2014-01-19 21:48:42 +0100 | [diff] [blame] | 3470 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3471 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) ); | 
|  | 3472 | return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN ); | 
| Gergely Budai | 987bfb5 | 2014-01-19 21:48:42 +0100 | [diff] [blame] | 3473 | } | 
| Manuel Pégourié-Gonnard | de05390 | 2014-02-04 13:58:39 +0100 | [diff] [blame] | 3474 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3475 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) ); | 
| Gergely Budai | 987bfb5 | 2014-01-19 21:48:42 +0100 | [diff] [blame] | 3476 |  | 
| Andrzej Kurek | f093a3d | 2019-02-01 02:50:36 -0500 | [diff] [blame] | 3477 | if( ( ret = mbedtls_ecdh_setup( &ssl->handshake->ecdh_ctx, | 
|  | 3478 | (*curve)->grp_id ) ) != 0 ) | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3479 | { | 
| Manuel Pégourié-Gonnard | e3a062b | 2015-05-11 18:46:47 +0200 | [diff] [blame] | 3480 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecp_group_load", ret ); | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3481 | return( ret ); | 
|  | 3482 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3483 |  | 
| Gilles Peskine | fe1c093 | 2017-11-23 13:35:02 +0100 | [diff] [blame] | 3484 | if( ( ret = mbedtls_ecdh_make_params( | 
|  | 3485 | &ssl->handshake->ecdh_ctx, &len, | 
| Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 3486 | ssl->out_msg + ssl->out_msglen, | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3487 | MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen, | 
| Gilles Peskine | fe1c093 | 2017-11-23 13:35:02 +0100 | [diff] [blame] | 3488 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3489 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3490 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_params", ret ); | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3491 | return( ret ); | 
|  | 3492 | } | 
|  | 3493 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3494 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) | 
| Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 3495 | dig_signed = ssl->out_msg + ssl->out_msglen; | 
| Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3496 | #endif | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3497 |  | 
| Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 3498 | ssl->out_msglen += len; | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3499 |  | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 3500 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, | 
|  | 3501 | MBEDTLS_DEBUG_ECDH_Q ); | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3502 | } | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3503 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3504 |  | 
| Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3505 | /* | 
| Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3506 | * | 
| Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3507 | * Part 2: For key exchanges involving the server signing the | 
| Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3508 | *         exchange parameters, compute and add the signature here. | 
|  | 3509 | * | 
| Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3510 | */ | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3511 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) | 
| Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3512 | if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) ) | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3513 | { | 
| Gilles Peskine | 1004c19 | 2018-01-08 16:59:14 +0100 | [diff] [blame] | 3514 | size_t dig_signed_len = ssl->out_msg + ssl->out_msglen - dig_signed; | 
| Gilles Peskine | ca1d742 | 2018-04-24 11:53:22 +0200 | [diff] [blame] | 3515 | size_t hashlen = 0; | 
| Ronald Cron | 3a95d2b | 2021-10-18 09:47:58 +0200 | [diff] [blame] | 3516 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 3517 | unsigned char hash[PSA_HASH_MAX_SIZE]; | 
|  | 3518 | #else | 
| Gilles Peskine | e1efdf9 | 2018-01-05 21:18:37 +0100 | [diff] [blame] | 3519 | unsigned char hash[MBEDTLS_MD_MAX_SIZE]; | 
| Ronald Cron | 3a95d2b | 2021-10-18 09:47:58 +0200 | [diff] [blame] | 3520 | #endif | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3521 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 3522 |  | 
| Manuel Pégourié-Gonnard | abae74c | 2013-08-20 13:53:44 +0200 | [diff] [blame] | 3523 | /* | 
| Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3524 | * 2.1: Choose hash algorithm: | 
| Hanno Becker | 4cb1f4d | 2017-10-10 15:59:57 +0100 | [diff] [blame] | 3525 | * A: For TLS 1.2, obey signature-hash-algorithm extension | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3526 | *    to choose appropriate hash. | 
|  | 3527 | * B: For SSL3, TLS1.0, TLS1.1 and ECDHE_ECDSA, use SHA1 | 
|  | 3528 | *    (RFC 4492, Sec. 5.4) | 
|  | 3529 | * C: Otherwise, use MD5 + SHA1 (RFC 4346, Sec. 7.4.3) | 
| Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3530 | */ | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 3531 |  | 
|  | 3532 | mbedtls_md_type_t md_alg; | 
|  | 3533 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3534 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3535 | mbedtls_pk_type_t sig_alg = | 
|  | 3536 | mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3537 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) | 
| Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3538 | { | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3539 | /* A: For TLS 1.2, obey signature-hash-algorithm extension | 
|  | 3540 | *    (RFC 5246, Sec. 7.4.1.4.1). */ | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 3541 | if( sig_alg == MBEDTLS_PK_NONE || | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3542 | ( md_alg = mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs, | 
|  | 3543 | sig_alg ) ) == MBEDTLS_MD_NONE ) | 
| Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3544 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3545 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
| Hanno Becker | 4cb1f4d | 2017-10-10 15:59:57 +0100 | [diff] [blame] | 3546 | /* (... because we choose a cipher suite | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3547 | *      only if there is a matching hash.) */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3548 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3549 | } | 
|  | 3550 | } | 
| Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 3551 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3552 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 3553 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ | 
|  | 3554 | defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
| Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3555 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ) | 
| Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3556 | { | 
| Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3557 | /* B: Default hash SHA1 */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3558 | md_alg = MBEDTLS_MD_SHA1; | 
| Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3559 | } | 
|  | 3560 | else | 
| Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3561 | #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \ | 
|  | 3562 | MBEDTLS_SSL_PROTO_TLS1_1 */ | 
| Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3563 | { | 
| Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3564 | /* C: MD5 + SHA1 */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3565 | md_alg = MBEDTLS_MD_NONE; | 
| Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3566 | } | 
|  | 3567 |  | 
| Paul Elliott | 3891caf | 2020-12-17 18:42:40 +0000 | [diff] [blame] | 3568 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "pick hash algorithm %u for signing", (unsigned) md_alg ) ); | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 3569 |  | 
| Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3570 | /* | 
| Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3571 | * 2.2: Compute the hash to be signed | 
| Manuel Pégourié-Gonnard | abae74c | 2013-08-20 13:53:44 +0200 | [diff] [blame] | 3572 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3573 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ | 
|  | 3574 | defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
|  | 3575 | if( md_alg == MBEDTLS_MD_NONE ) | 
| Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 3576 | { | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3577 | hashlen = 36; | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 3578 | ret = mbedtls_ssl_get_key_exchange_md_ssl_tls( ssl, hash, | 
|  | 3579 | dig_signed, | 
|  | 3580 | dig_signed_len ); | 
|  | 3581 | if( ret != 0 ) | 
|  | 3582 | return( ret ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3583 | } | 
|  | 3584 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3585 | #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \ | 
|  | 3586 | MBEDTLS_SSL_PROTO_TLS1_1 */ | 
|  | 3587 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ | 
|  | 3588 | defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 3589 | if( md_alg != MBEDTLS_MD_NONE ) | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3590 | { | 
| Gilles Peskine | ca1d742 | 2018-04-24 11:53:22 +0200 | [diff] [blame] | 3591 | ret = mbedtls_ssl_get_key_exchange_md_tls1_2( ssl, hash, &hashlen, | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 3592 | dig_signed, | 
|  | 3593 | dig_signed_len, | 
|  | 3594 | md_alg ); | 
|  | 3595 | if( ret != 0 ) | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3596 | return( ret ); | 
| Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 3597 | } | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 3598 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3599 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ | 
|  | 3600 | MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 3601 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3602 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 3603 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 3604 | } | 
| Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 3605 |  | 
| Gilles Peskine | ebd652f | 2018-01-05 21:18:59 +0100 | [diff] [blame] | 3606 | MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3607 |  | 
| Manuel Pégourié-Gonnard | abae74c | 2013-08-20 13:53:44 +0200 | [diff] [blame] | 3608 | /* | 
| Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3609 | * 2.3: Compute and add the signature | 
| Manuel Pégourié-Gonnard | abae74c | 2013-08-20 13:53:44 +0200 | [diff] [blame] | 3610 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3611 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 3612 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) | 
| Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 3613 | { | 
| Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3614 | /* | 
|  | 3615 | * For TLS 1.2, we need to specify signature and hash algorithm | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 3616 | * explicitly through a prefix to the signature. | 
|  | 3617 | * | 
|  | 3618 | * struct { | 
|  | 3619 | *    HashAlgorithm hash; | 
|  | 3620 | *    SignatureAlgorithm signature; | 
|  | 3621 | * } SignatureAndHashAlgorithm; | 
|  | 3622 | * | 
|  | 3623 | * struct { | 
|  | 3624 | *    SignatureAndHashAlgorithm algorithm; | 
|  | 3625 | *    opaque signature<0..2^16-1>; | 
|  | 3626 | * } DigitallySigned; | 
|  | 3627 | * | 
|  | 3628 | */ | 
|  | 3629 |  | 
| Gilles Peskine | 1004c19 | 2018-01-08 16:59:14 +0100 | [diff] [blame] | 3630 | ssl->out_msg[ssl->out_msglen++] = | 
|  | 3631 | mbedtls_ssl_hash_from_md_alg( md_alg ); | 
|  | 3632 | ssl->out_msg[ssl->out_msglen++] = | 
|  | 3633 | mbedtls_ssl_sig_from_pk_alg( sig_alg ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3634 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3635 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3636 |  | 
| Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3637 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) | 
| Gilles Peskine | 4bf9a28 | 2018-01-05 21:20:50 +0100 | [diff] [blame] | 3638 | if( ssl->conf->f_async_sign_start != NULL ) | 
|  | 3639 | { | 
| Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 3640 | ret = ssl->conf->f_async_sign_start( ssl, | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3641 | mbedtls_ssl_own_cert( ssl ), | 
|  | 3642 | md_alg, hash, hashlen ); | 
| Gilles Peskine | 4bf9a28 | 2018-01-05 21:20:50 +0100 | [diff] [blame] | 3643 | switch( ret ) | 
|  | 3644 | { | 
|  | 3645 | case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH: | 
|  | 3646 | /* act as if f_async_sign was null */ | 
|  | 3647 | break; | 
|  | 3648 | case 0: | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3649 | ssl->handshake->async_in_progress = 1; | 
| Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3650 | return( ssl_resume_server_key_exchange( ssl, signature_len ) ); | 
| Gilles Peskine | 4bf9a28 | 2018-01-05 21:20:50 +0100 | [diff] [blame] | 3651 | case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS: | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3652 | ssl->handshake->async_in_progress = 1; | 
| Gilles Peskine | 4bf9a28 | 2018-01-05 21:20:50 +0100 | [diff] [blame] | 3653 | return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS ); | 
|  | 3654 | default: | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3655 | MBEDTLS_SSL_DEBUG_RET( 1, "f_async_sign_start", ret ); | 
| Gilles Peskine | 4bf9a28 | 2018-01-05 21:20:50 +0100 | [diff] [blame] | 3656 | return( ret ); | 
|  | 3657 | } | 
|  | 3658 | } | 
| Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3659 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ | 
| Gilles Peskine | 4bf9a28 | 2018-01-05 21:20:50 +0100 | [diff] [blame] | 3660 |  | 
|  | 3661 | if( mbedtls_ssl_own_key( ssl ) == NULL ) | 
|  | 3662 | { | 
|  | 3663 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key" ) ); | 
|  | 3664 | return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ); | 
|  | 3665 | } | 
|  | 3666 |  | 
| Gilles Peskine | 0fd90dd | 2018-04-26 07:41:09 +0200 | [diff] [blame] | 3667 | /* Append the signature to ssl->out_msg, leaving 2 bytes for the | 
|  | 3668 | * signature length which will be added in ssl_write_server_key_exchange | 
|  | 3669 | * after the call to ssl_prepare_server_key_exchange. | 
|  | 3670 | * ssl_write_server_key_exchange also takes care of incrementing | 
|  | 3671 | * ssl->out_msglen. */ | 
| Gilles Peskine | 1004c19 | 2018-01-08 16:59:14 +0100 | [diff] [blame] | 3672 | if( ( ret = mbedtls_pk_sign( mbedtls_ssl_own_key( ssl ), | 
|  | 3673 | md_alg, hash, hashlen, | 
|  | 3674 | ssl->out_msg + ssl->out_msglen + 2, | 
| Gilles Peskine | 7ab013a | 2018-01-08 17:04:16 +0100 | [diff] [blame] | 3675 | signature_len, | 
| Gilles Peskine | 1004c19 | 2018-01-08 16:59:14 +0100 | [diff] [blame] | 3676 | ssl->conf->f_rng, | 
|  | 3677 | ssl->conf->p_rng ) ) != 0 ) | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3678 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3679 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret ); | 
| Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 3680 | return( ret ); | 
| Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 3681 | } | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3682 | } | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3683 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */ | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3684 |  | 
| Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3685 | return( 0 ); | 
|  | 3686 | } | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3687 |  | 
| Gilles Peskine | d3eb061 | 2018-01-08 17:07:44 +0100 | [diff] [blame] | 3688 | /* Prepare the ServerKeyExchange message and send it. For ciphersuites | 
| Gilles Peskine | 168dae8 | 2018-04-25 23:35:42 +0200 | [diff] [blame] | 3689 | * that do not include a ServerKeyExchange message, do nothing. Either | 
|  | 3690 | * way, if successful, move on to the next step in the SSL state | 
|  | 3691 | * machine. */ | 
| Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 3692 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3693 | static int ssl_write_server_key_exchange( mbedtls_ssl_context *ssl ) | 
|  | 3694 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3695 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Gilles Peskine | 7ab013a | 2018-01-08 17:04:16 +0100 | [diff] [blame] | 3696 | size_t signature_len = 0; | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3697 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED) | 
| Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3698 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | 
| Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 3699 | ssl->handshake->ciphersuite_info; | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3700 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED */ | 
| Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3701 |  | 
| Gilles Peskine | d3eb061 | 2018-01-08 17:07:44 +0100 | [diff] [blame] | 3702 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) ); | 
|  | 3703 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3704 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED) | 
| Gilles Peskine | d3eb061 | 2018-01-08 17:07:44 +0100 | [diff] [blame] | 3705 | /* Extract static ECDH parameters and abort if ServerKeyExchange | 
|  | 3706 | * is not needed. */ | 
| Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3707 | if( mbedtls_ssl_ciphersuite_no_pfs( ciphersuite_info ) ) | 
|  | 3708 | { | 
|  | 3709 | /* For suites involving ECDH, extract DH parameters | 
|  | 3710 | * from certificate at this point. */ | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3711 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_ENABLED) | 
| Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3712 | if( mbedtls_ssl_ciphersuite_uses_ecdh( ciphersuite_info ) ) | 
|  | 3713 | { | 
| Manuel Pégourié-Gonnard | 5b3f24f | 2022-06-10 09:34:20 +0200 | [diff] [blame] | 3714 | ret = ssl_get_ecdh_params_from_cert( ssl ); | 
|  | 3715 | if( ret != 0 ) | 
|  | 3716 | { | 
|  | 3717 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret ); | 
|  | 3718 | return( ret ); | 
|  | 3719 | } | 
| Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3720 | } | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3721 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_ENABLED */ | 
| Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3722 |  | 
|  | 3723 | /* Key exchanges not involving ephemeral keys don't use | 
|  | 3724 | * ServerKeyExchange, so end here. */ | 
|  | 3725 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) ); | 
|  | 3726 | ssl->state++; | 
|  | 3727 | return( 0 ); | 
|  | 3728 | } | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3729 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED */ | 
| Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3730 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3731 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) && \ | 
| Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3732 | defined(MBEDTLS_SSL_ASYNC_PRIVATE) | 
| Gilles Peskine | d3eb061 | 2018-01-08 17:07:44 +0100 | [diff] [blame] | 3733 | /* If we have already prepared the message and there is an ongoing | 
| Gilles Peskine | 168dae8 | 2018-04-25 23:35:42 +0200 | [diff] [blame] | 3734 | * signature operation, resume signing. */ | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3735 | if( ssl->handshake->async_in_progress != 0 ) | 
| Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3736 | { | 
|  | 3737 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "resuming signature operation" ) ); | 
|  | 3738 | ret = ssl_resume_server_key_exchange( ssl, &signature_len ); | 
| Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3739 | } | 
|  | 3740 | else | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3741 | #endif /* defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) && | 
| Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3742 | defined(MBEDTLS_SSL_ASYNC_PRIVATE) */ | 
| Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3743 | { | 
|  | 3744 | /* ServerKeyExchange is needed. Prepare the message. */ | 
|  | 3745 | ret = ssl_prepare_server_key_exchange( ssl, &signature_len ); | 
| Gilles Peskine | d3eb061 | 2018-01-08 17:07:44 +0100 | [diff] [blame] | 3746 | } | 
|  | 3747 |  | 
|  | 3748 | if( ret != 0 ) | 
|  | 3749 | { | 
| Gilles Peskine | ad28bf0 | 2018-04-26 00:19:16 +0200 | [diff] [blame] | 3750 | /* If we're starting to write a new message, set ssl->out_msglen | 
|  | 3751 | * to 0. But if we're resuming after an asynchronous message, | 
|  | 3752 | * out_msglen is the amount of data written so far and mst be | 
|  | 3753 | * preserved. */ | 
| Gilles Peskine | d3eb061 | 2018-01-08 17:07:44 +0100 | [diff] [blame] | 3754 | if( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS ) | 
|  | 3755 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server key exchange (pending)" ) ); | 
|  | 3756 | else | 
|  | 3757 | ssl->out_msglen = 0; | 
|  | 3758 | return( ret ); | 
| Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3759 | } | 
| Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3760 |  | 
| Gilles Peskine | 7ab013a | 2018-01-08 17:04:16 +0100 | [diff] [blame] | 3761 | /* If there is a signature, write its length. | 
| Gilles Peskine | 168dae8 | 2018-04-25 23:35:42 +0200 | [diff] [blame] | 3762 | * ssl_prepare_server_key_exchange already wrote the signature | 
|  | 3763 | * itself at its proper place in the output buffer. */ | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3764 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) | 
| Gilles Peskine | 7ab013a | 2018-01-08 17:04:16 +0100 | [diff] [blame] | 3765 | if( signature_len != 0 ) | 
|  | 3766 | { | 
| Joe Subbiani | ad1115a | 2021-07-16 14:27:50 +0100 | [diff] [blame] | 3767 | ssl->out_msg[ssl->out_msglen++] = MBEDTLS_BYTE_1( signature_len ); | 
|  | 3768 | ssl->out_msg[ssl->out_msglen++] = MBEDTLS_BYTE_0( signature_len ); | 
| Gilles Peskine | 7ab013a | 2018-01-08 17:04:16 +0100 | [diff] [blame] | 3769 |  | 
|  | 3770 | MBEDTLS_SSL_DEBUG_BUF( 3, "my signature", | 
|  | 3771 | ssl->out_msg + ssl->out_msglen, | 
|  | 3772 | signature_len ); | 
|  | 3773 |  | 
|  | 3774 | /* Skip over the already-written signature */ | 
|  | 3775 | ssl->out_msglen += signature_len; | 
|  | 3776 | } | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3777 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */ | 
| Gilles Peskine | 7ab013a | 2018-01-08 17:04:16 +0100 | [diff] [blame] | 3778 |  | 
| Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3779 | /* Add header and send. */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3780 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; | 
|  | 3781 | ssl->out_msg[0]  = MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3782 |  | 
|  | 3783 | ssl->state++; | 
|  | 3784 |  | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3785 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3786 | { | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3787 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3788 | return( ret ); | 
|  | 3789 | } | 
|  | 3790 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3791 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3792 | return( 0 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3793 | } | 
|  | 3794 |  | 
| Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 3795 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3796 | static int ssl_write_server_hello_done( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3797 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3798 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3799 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3800 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3801 |  | 
|  | 3802 | ssl->out_msglen  = 4; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3803 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; | 
|  | 3804 | ssl->out_msg[0]  = MBEDTLS_SSL_HS_SERVER_HELLO_DONE; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3805 |  | 
|  | 3806 | ssl->state++; | 
|  | 3807 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3808 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3809 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3810 | mbedtls_ssl_send_flight_completed( ssl ); | 
| Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3811 | #endif | 
|  | 3812 |  | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3813 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3814 | { | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3815 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3816 | return( ret ); | 
|  | 3817 | } | 
|  | 3818 |  | 
| Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3819 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 3820 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | 
|  | 3821 | ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) | 
|  | 3822 | { | 
|  | 3823 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); | 
|  | 3824 | return( ret ); | 
|  | 3825 | } | 
| Hanno Becker | bc2498a | 2018-08-28 10:13:29 +0100 | [diff] [blame] | 3826 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3827 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3828 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3829 |  | 
|  | 3830 | return( 0 ); | 
|  | 3831 | } | 
|  | 3832 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3833 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) ||                       \ | 
|  | 3834 | defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) | 
| Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 3835 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3836 | static int ssl_parse_client_dh_public( mbedtls_ssl_context *ssl, unsigned char **p, | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3837 | const unsigned char *end ) | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3838 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3839 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3840 | size_t n; | 
|  | 3841 |  | 
|  | 3842 | /* | 
|  | 3843 | * Receive G^Y mod P, premaster = (G^Y)^X mod P | 
|  | 3844 | */ | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3845 | if( *p + 2 > end ) | 
|  | 3846 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3847 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); | 
|  | 3848 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3849 | } | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3850 |  | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3851 | n = ( (*p)[0] << 8 ) | (*p)[1]; | 
|  | 3852 | *p += 2; | 
|  | 3853 |  | 
| Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 3854 | if( *p + n > end ) | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3855 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3856 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); | 
|  | 3857 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3858 | } | 
|  | 3859 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3860 | if( ( ret = mbedtls_dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 ) | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3861 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3862 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_read_public", ret ); | 
|  | 3863 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP ); | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3864 | } | 
|  | 3865 |  | 
| Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 3866 | *p += n; | 
|  | 3867 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3868 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY ); | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3869 |  | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3870 | return( ret ); | 
|  | 3871 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3872 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED || | 
|  | 3873 | MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3874 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3875 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) ||                           \ | 
|  | 3876 | defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) | 
| Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3877 |  | 
| Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3878 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) | 
| Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 3879 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3880 | static int ssl_resume_decrypt_pms( mbedtls_ssl_context *ssl, | 
|  | 3881 | unsigned char *peer_pms, | 
|  | 3882 | size_t *peer_pmslen, | 
|  | 3883 | size_t peer_pmssize ) | 
|  | 3884 | { | 
| Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 3885 | int ret = ssl->conf->f_async_resume( ssl, | 
| Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3886 | peer_pms, peer_pmslen, peer_pmssize ); | 
|  | 3887 | if( ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS ) | 
|  | 3888 | { | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3889 | ssl->handshake->async_in_progress = 0; | 
| Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 3890 | mbedtls_ssl_set_async_operation_data( ssl, NULL ); | 
| Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3891 | } | 
|  | 3892 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl_decrypt_encrypted_pms", ret ); | 
|  | 3893 | return( ret ); | 
|  | 3894 | } | 
| Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3895 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ | 
| Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3896 |  | 
| Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 3897 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | bcd98a5 | 2018-01-11 21:30:40 +0100 | [diff] [blame] | 3898 | static int ssl_decrypt_encrypted_pms( mbedtls_ssl_context *ssl, | 
|  | 3899 | const unsigned char *p, | 
|  | 3900 | const unsigned char *end, | 
|  | 3901 | unsigned char *peer_pms, | 
|  | 3902 | size_t *peer_pmslen, | 
|  | 3903 | size_t peer_pmssize ) | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3904 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3905 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Gilles Peskine | 422ccab | 2018-01-11 18:29:01 +0100 | [diff] [blame] | 3906 | mbedtls_pk_context *private_key = mbedtls_ssl_own_key( ssl ); | 
|  | 3907 | mbedtls_pk_context *public_key = &mbedtls_ssl_own_cert( ssl )->pk; | 
|  | 3908 | size_t len = mbedtls_pk_get_len( public_key ); | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3909 |  | 
| Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3910 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) | 
| Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3911 | /* If we have already started decoding the message and there is an ongoing | 
| Gilles Peskine | 168dae8 | 2018-04-25 23:35:42 +0200 | [diff] [blame] | 3912 | * decryption operation, resume signing. */ | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3913 | if( ssl->handshake->async_in_progress != 0 ) | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3914 | { | 
| Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3915 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "resuming decryption operation" ) ); | 
|  | 3916 | return( ssl_resume_decrypt_pms( ssl, | 
|  | 3917 | peer_pms, peer_pmslen, peer_pmssize ) ); | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3918 | } | 
| Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3919 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3920 |  | 
|  | 3921 | /* | 
| Gilles Peskine | 422ccab | 2018-01-11 18:29:01 +0100 | [diff] [blame] | 3922 | * Prepare to decrypt the premaster using own private RSA key | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3923 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3924 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ | 
|  | 3925 | defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 3926 | if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 ) | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3927 | { | 
| Joe Subbiani | a724ef9 | 2021-08-18 12:06:57 +0100 | [diff] [blame] | 3928 | if ( p + 2 > end ) | 
|  | 3929 | { | 
| Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 3930 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); | 
|  | 3931 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); | 
|  | 3932 | } | 
| Joe Subbiani | f15da89 | 2021-08-03 16:10:38 +0100 | [diff] [blame] | 3933 | if( *p++ != MBEDTLS_BYTE_1( len ) || | 
| Joe Subbiani | a724ef9 | 2021-08-18 12:06:57 +0100 | [diff] [blame] | 3934 | *p++ != MBEDTLS_BYTE_0( len ) ) | 
|  | 3935 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3936 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); | 
|  | 3937 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3938 | } | 
|  | 3939 | } | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 3940 | #endif | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3941 |  | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 3942 | if( p + len != end ) | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3943 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3944 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); | 
|  | 3945 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3946 | } | 
|  | 3947 |  | 
| Gilles Peskine | 422ccab | 2018-01-11 18:29:01 +0100 | [diff] [blame] | 3948 | /* | 
|  | 3949 | * Decrypt the premaster secret | 
|  | 3950 | */ | 
| Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3951 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) | 
| Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3952 | if( ssl->conf->f_async_decrypt_start != NULL ) | 
|  | 3953 | { | 
| Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 3954 | ret = ssl->conf->f_async_decrypt_start( ssl, | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3955 | mbedtls_ssl_own_cert( ssl ), | 
|  | 3956 | p, len ); | 
| Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3957 | switch( ret ) | 
|  | 3958 | { | 
|  | 3959 | case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH: | 
|  | 3960 | /* act as if f_async_decrypt_start was null */ | 
|  | 3961 | break; | 
|  | 3962 | case 0: | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3963 | ssl->handshake->async_in_progress = 1; | 
| Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3964 | return( ssl_resume_decrypt_pms( ssl, | 
|  | 3965 | peer_pms, | 
|  | 3966 | peer_pmslen, | 
|  | 3967 | peer_pmssize ) ); | 
|  | 3968 | case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS: | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3969 | ssl->handshake->async_in_progress = 1; | 
| Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3970 | return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS ); | 
|  | 3971 | default: | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3972 | MBEDTLS_SSL_DEBUG_RET( 1, "f_async_decrypt_start", ret ); | 
| Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3973 | return( ret ); | 
|  | 3974 | } | 
|  | 3975 | } | 
| Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3976 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ | 
| Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3977 |  | 
| Gilles Peskine | 422ccab | 2018-01-11 18:29:01 +0100 | [diff] [blame] | 3978 | if( ! mbedtls_pk_can_do( private_key, MBEDTLS_PK_RSA ) ) | 
|  | 3979 | { | 
| Gilles Peskine | 422ccab | 2018-01-11 18:29:01 +0100 | [diff] [blame] | 3980 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) ); | 
|  | 3981 | return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ); | 
|  | 3982 | } | 
|  | 3983 |  | 
|  | 3984 | ret = mbedtls_pk_decrypt( private_key, p, len, | 
| Gilles Peskine | bcd98a5 | 2018-01-11 21:30:40 +0100 | [diff] [blame] | 3985 | peer_pms, peer_pmslen, peer_pmssize, | 
|  | 3986 | ssl->conf->f_rng, ssl->conf->p_rng ); | 
|  | 3987 | return( ret ); | 
|  | 3988 | } | 
|  | 3989 |  | 
| Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 3990 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | bcd98a5 | 2018-01-11 21:30:40 +0100 | [diff] [blame] | 3991 | static int ssl_parse_encrypted_pms( mbedtls_ssl_context *ssl, | 
|  | 3992 | const unsigned char *p, | 
|  | 3993 | const unsigned char *end, | 
|  | 3994 | size_t pms_offset ) | 
|  | 3995 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3996 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Gilles Peskine | bcd98a5 | 2018-01-11 21:30:40 +0100 | [diff] [blame] | 3997 | unsigned char *pms = ssl->handshake->premaster + pms_offset; | 
|  | 3998 | unsigned char ver[2]; | 
|  | 3999 | unsigned char fake_pms[48], peer_pms[48]; | 
|  | 4000 | unsigned char mask; | 
|  | 4001 | size_t i, peer_pmslen; | 
|  | 4002 | unsigned int diff; | 
|  | 4003 |  | 
| Gilles Peskine | 0a8352b | 2018-06-13 18:16:41 +0200 | [diff] [blame] | 4004 | /* In case of a failure in decryption, the decryption may write less than | 
|  | 4005 | * 2 bytes of output, but we always read the first two bytes. It doesn't | 
|  | 4006 | * matter in the end because diff will be nonzero in that case due to | 
| André Maroneze | 7953329 | 2020-11-12 09:37:42 +0100 | [diff] [blame] | 4007 | * ret being nonzero, and we only care whether diff is 0. | 
|  | 4008 | * But do initialize peer_pms and peer_pmslen for robustness anyway. This | 
|  | 4009 | * also makes memory analyzers happy (don't access uninitialized memory, | 
|  | 4010 | * even if it's an unsigned char). */ | 
| Gilles Peskine | 0a8352b | 2018-06-13 18:16:41 +0200 | [diff] [blame] | 4011 | peer_pms[0] = peer_pms[1] = ~0; | 
| André Maroneze | 7953329 | 2020-11-12 09:37:42 +0100 | [diff] [blame] | 4012 | peer_pmslen = 0; | 
| Gilles Peskine | 0a8352b | 2018-06-13 18:16:41 +0200 | [diff] [blame] | 4013 |  | 
| Gilles Peskine | bcd98a5 | 2018-01-11 21:30:40 +0100 | [diff] [blame] | 4014 | ret = ssl_decrypt_encrypted_pms( ssl, p, end, | 
|  | 4015 | peer_pms, | 
|  | 4016 | &peer_pmslen, | 
|  | 4017 | sizeof( peer_pms ) ); | 
|  | 4018 |  | 
| Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 4019 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) | 
| Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 4020 | if ( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS ) | 
|  | 4021 | return( ret ); | 
| Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 4022 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ | 
| Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 4023 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4024 | mbedtls_ssl_write_version( ssl->handshake->max_major_ver, | 
| Gilles Peskine | 2e33337 | 2018-04-24 13:22:10 +0200 | [diff] [blame] | 4025 | ssl->handshake->max_minor_ver, | 
|  | 4026 | ssl->conf->transport, ver ); | 
|  | 4027 |  | 
|  | 4028 | /* Avoid data-dependent branches while checking for invalid | 
|  | 4029 | * padding, to protect against timing-based Bleichenbacher-type | 
|  | 4030 | * attacks. */ | 
|  | 4031 | diff  = (unsigned int) ret; | 
|  | 4032 | diff |= peer_pmslen ^ 48; | 
|  | 4033 | diff |= peer_pms[0] ^ ver[0]; | 
|  | 4034 | diff |= peer_pms[1] ^ ver[1]; | 
|  | 4035 |  | 
|  | 4036 | /* mask = diff ? 0xff : 0x00 using bit operations to avoid branches */ | 
| Gabor Mezei | 18a4494 | 2021-10-20 11:59:27 +0200 | [diff] [blame] | 4037 | mask = mbedtls_ct_uint_mask( diff ); | 
| Manuel Pégourié-Gonnard | b9c93d0 | 2015-06-23 13:53:15 +0200 | [diff] [blame] | 4038 |  | 
| Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 4039 | /* | 
|  | 4040 | * Protection against Bleichenbacher's attack: invalid PKCS#1 v1.5 padding | 
|  | 4041 | * must not cause the connection to end immediately; instead, send a | 
|  | 4042 | * bad_record_mac later in the handshake. | 
| Gilles Peskine | bcd98a5 | 2018-01-11 21:30:40 +0100 | [diff] [blame] | 4043 | * To protect against timing-based variants of the attack, we must | 
|  | 4044 | * not have any branch that depends on whether the decryption was | 
|  | 4045 | * successful. In particular, always generate the fake premaster secret, | 
|  | 4046 | * regardless of whether it will ultimately influence the output or not. | 
| Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 4047 | */ | 
| Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 4048 | ret = ssl->conf->f_rng( ssl->conf->p_rng, fake_pms, sizeof( fake_pms ) ); | 
| Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 4049 | if( ret != 0 ) | 
| Gilles Peskine | bcd98a5 | 2018-01-11 21:30:40 +0100 | [diff] [blame] | 4050 | { | 
| Gilles Peskine | e141638 | 2018-04-26 10:23:21 +0200 | [diff] [blame] | 4051 | /* It's ok to abort on an RNG failure, since this does not reveal | 
|  | 4052 | * anything about the RSA decryption. */ | 
| Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 4053 | return( ret ); | 
| Gilles Peskine | bcd98a5 | 2018-01-11 21:30:40 +0100 | [diff] [blame] | 4054 | } | 
| Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 4055 |  | 
| Manuel Pégourié-Gonnard | 331ba57 | 2015-04-20 12:33:57 +0100 | [diff] [blame] | 4056 | #if defined(MBEDTLS_SSL_DEBUG_ALL) | 
| Manuel Pégourié-Gonnard | ce60fbe | 2015-04-15 16:45:52 +0200 | [diff] [blame] | 4057 | if( diff != 0 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4058 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); | 
| Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 4059 | #endif | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 4060 |  | 
| Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 4061 | if( sizeof( ssl->handshake->premaster ) < pms_offset || | 
|  | 4062 | sizeof( ssl->handshake->premaster ) - pms_offset < 48 ) | 
|  | 4063 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4064 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 4065 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 4066 | } | 
| Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 4067 | ssl->handshake->pmslen = 48; | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 4068 |  | 
| Gilles Peskine | 422ccab | 2018-01-11 18:29:01 +0100 | [diff] [blame] | 4069 | /* Set pms to either the true or the fake PMS, without | 
|  | 4070 | * data-dependent branches. */ | 
| Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 4071 | for( i = 0; i < ssl->handshake->pmslen; i++ ) | 
|  | 4072 | pms[i] = ( mask & fake_pms[i] ) | ( (~mask) & peer_pms[i] ); | 
|  | 4073 |  | 
|  | 4074 | return( 0 ); | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 4075 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4076 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED || | 
|  | 4077 | MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 4078 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4079 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) | 
| Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 4080 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4081 | static int ssl_parse_client_psk_identity( mbedtls_ssl_context *ssl, unsigned char **p, | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4082 | const unsigned char *end ) | 
| Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4083 | { | 
| Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 4084 | int ret = 0; | 
| irwir | 6527bd6 | 2019-09-21 18:51:25 +0300 | [diff] [blame] | 4085 | uint16_t n; | 
| Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4086 |  | 
| Hanno Becker | 845b946 | 2018-10-26 12:07:29 +0100 | [diff] [blame] | 4087 | if( ssl_conf_has_psk_or_cb( ssl->conf ) == 0 ) | 
| Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4088 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4089 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) ); | 
|  | 4090 | return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ); | 
| Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4091 | } | 
|  | 4092 |  | 
|  | 4093 | /* | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4094 | * Receive client pre-shared key identity name | 
| Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4095 | */ | 
| Hanno Becker | 83c9f49 | 2017-06-26 13:52:14 +0100 | [diff] [blame] | 4096 | if( end - *p < 2 ) | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4097 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4098 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); | 
|  | 4099 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4100 | } | 
| Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4101 |  | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4102 | n = ( (*p)[0] << 8 ) | (*p)[1]; | 
|  | 4103 | *p += 2; | 
|  | 4104 |  | 
| irwir | 6527bd6 | 2019-09-21 18:51:25 +0300 | [diff] [blame] | 4105 | if( n == 0 || n > end - *p ) | 
| Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4106 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4107 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); | 
|  | 4108 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); | 
| Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4109 | } | 
|  | 4110 |  | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4111 | if( ssl->conf->f_psk != NULL ) | 
| Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 4112 | { | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4113 | if( ssl->conf->f_psk( ssl->conf->p_psk, ssl, *p, n ) != 0 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4114 | ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY; | 
| Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 4115 | } | 
| Manuel Pégourié-Gonnard | d27680b | 2014-07-08 14:15:55 +0200 | [diff] [blame] | 4116 | else | 
| Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 4117 | { | 
| Manuel Pégourié-Gonnard | 31ff1d2 | 2013-10-28 13:46:11 +0100 | [diff] [blame] | 4118 | /* Identity is not a big secret since clients send it in the clear, | 
|  | 4119 | * but treat it carefully anyway, just in case */ | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4120 | if( n != ssl->conf->psk_identity_len || | 
| Gabor Mezei | 18a4494 | 2021-10-20 11:59:27 +0200 | [diff] [blame] | 4121 | mbedtls_ct_memcmp( ssl->conf->psk_identity, *p, n ) != 0 ) | 
| Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 4122 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4123 | ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY; | 
| Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 4124 | } | 
|  | 4125 | } | 
|  | 4126 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4127 | if( ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY ) | 
| Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4128 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4129 | MBEDTLS_SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n ); | 
| Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 4130 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 4131 | MBEDTLS_SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4132 | return( MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY ); | 
| Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4133 | } | 
|  | 4134 |  | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4135 | *p += n; | 
| Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4136 |  | 
| Manuel Pégourié-Gonnard | d27680b | 2014-07-08 14:15:55 +0200 | [diff] [blame] | 4137 | return( 0 ); | 
| Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4138 | } | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4139 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ | 
| Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4140 |  | 
| Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 4141 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4142 | static int ssl_parse_client_key_exchange( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4143 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4144 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4145 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; | 
| Manuel Pégourié-Gonnard | 2114d72 | 2014-09-10 13:59:41 +0000 | [diff] [blame] | 4146 | unsigned char *p, *end; | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 4147 |  | 
| Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 4148 | ciphersuite_info = ssl->handshake->ciphersuite_info; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4149 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4150 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4151 |  | 
| Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 4152 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) && \ | 
| Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 4153 | ( defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \ | 
|  | 4154 | defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) ) | 
|  | 4155 | if( ( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK || | 
|  | 4156 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA ) && | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4157 | ( ssl->handshake->async_in_progress != 0 ) ) | 
| Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 4158 | { | 
|  | 4159 | /* We've already read a record and there is an asynchronous | 
|  | 4160 | * operation in progress to decrypt it. So skip reading the | 
| Gilles Peskine | 168dae8 | 2018-04-25 23:35:42 +0200 | [diff] [blame] | 4161 | * record. */ | 
| Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 4162 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "will resume decryption of previously-read record" ) ); | 
|  | 4163 | } | 
|  | 4164 | else | 
|  | 4165 | #endif | 
| Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 4166 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4167 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4168 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4169 | return( ret ); | 
|  | 4170 | } | 
|  | 4171 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4172 | p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ); | 
| Manuel Pégourié-Gonnard | 2114d72 | 2014-09-10 13:59:41 +0000 | [diff] [blame] | 4173 | end = ssl->in_msg + ssl->in_hslen; | 
| Manuel Pégourié-Gonnard | f899583 | 2014-09-10 08:25:12 +0000 | [diff] [blame] | 4174 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4175 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4176 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4177 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); | 
|  | 4178 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4179 | } | 
|  | 4180 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4181 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4182 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4183 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); | 
|  | 4184 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4185 | } | 
|  | 4186 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4187 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) | 
|  | 4188 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4189 | { | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4190 | if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4191 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4192 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret ); | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 4193 | return( ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4194 | } | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4195 |  | 
| Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 4196 | if( p != end ) | 
|  | 4197 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4198 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) ); | 
|  | 4199 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); | 
| Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 4200 | } | 
|  | 4201 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4202 | if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx, | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4203 | ssl->handshake->premaster, | 
| Manuel Pégourié-Gonnard | 3335205 | 2015-06-02 16:17:08 +0100 | [diff] [blame] | 4204 | MBEDTLS_PREMASTER_SIZE, | 
| Manuel Pégourié-Gonnard | 2d62764 | 2013-09-04 14:22:07 +0200 | [diff] [blame] | 4205 | &ssl->handshake->pmslen, | 
| Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 4206 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4207 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4208 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); | 
|  | 4209 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4210 | } | 
|  | 4211 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4212 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K  ); | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 4213 | } | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4214 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4215 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */ | 
|  | 4216 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) ||                     \ | 
|  | 4217 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) ||                   \ | 
|  | 4218 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||                      \ | 
|  | 4219 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) | 
|  | 4220 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA || | 
|  | 4221 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA || | 
|  | 4222 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA || | 
|  | 4223 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA ) | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 4224 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4225 | if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx, | 
| Manuel Pégourié-Gonnard | 2114d72 | 2014-09-10 13:59:41 +0000 | [diff] [blame] | 4226 | p, end - p) ) != 0 ) | 
| Manuel Pégourié-Gonnard | b59d699 | 2013-10-14 12:00:45 +0200 | [diff] [blame] | 4227 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4228 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret ); | 
|  | 4229 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP ); | 
| Manuel Pégourié-Gonnard | b59d699 | 2013-10-14 12:00:45 +0200 | [diff] [blame] | 4230 | } | 
|  | 4231 |  | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 4232 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, | 
|  | 4233 | MBEDTLS_DEBUG_ECDH_QP ); | 
| Manuel Pégourié-Gonnard | b59d699 | 2013-10-14 12:00:45 +0200 | [diff] [blame] | 4234 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4235 | if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4236 | &ssl->handshake->pmslen, | 
|  | 4237 | ssl->handshake->premaster, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4238 | MBEDTLS_MPI_MAX_SIZE, | 
| Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 4239 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4240 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4241 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret ); | 
|  | 4242 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4243 | } | 
|  | 4244 |  | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 4245 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, | 
|  | 4246 | MBEDTLS_DEBUG_ECDH_Z ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4247 | } | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4248 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4249 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || | 
|  | 4250 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED || | 
|  | 4251 | MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED || | 
|  | 4252 | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */ | 
|  | 4253 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) | 
|  | 4254 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ) | 
| Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4255 | { | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4256 | if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 ) | 
| Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4257 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4258 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret ); | 
| Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4259 | return( ret ); | 
|  | 4260 | } | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4261 |  | 
| Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 4262 | if( p != end ) | 
|  | 4263 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4264 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) ); | 
|  | 4265 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); | 
| Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 4266 | } | 
|  | 4267 |  | 
| Hanno Becker | 845b946 | 2018-10-26 12:07:29 +0100 | [diff] [blame] | 4268 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Shaun Case | 0e7791f | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 4269 | /* For opaque PSKs, we perform the PSK-to-MS derivation automatically | 
| Hanno Becker | 845b946 | 2018-10-26 12:07:29 +0100 | [diff] [blame] | 4270 | * and skip the intermediate PMS. */ | 
| Hanno Becker | c1385c1 | 2018-11-05 12:44:27 +0000 | [diff] [blame] | 4271 | if( ssl_use_opaque_psk( ssl ) == 1 ) | 
| Hanno Becker | 845b946 | 2018-10-26 12:07:29 +0100 | [diff] [blame] | 4272 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "skip PMS generation for opaque PSK" ) ); | 
|  | 4273 | else | 
|  | 4274 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4275 | if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl, | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 4276 | ciphersuite_info->key_exchange ) ) != 0 ) | 
|  | 4277 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4278 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret ); | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 4279 | return( ret ); | 
|  | 4280 | } | 
| Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4281 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4282 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4283 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ | 
|  | 4284 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) | 
|  | 4285 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 4286 | { | 
| Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 4287 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4288 | if ( ssl->handshake->async_in_progress != 0 ) | 
| Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 4289 | { | 
|  | 4290 | /* There is an asynchronous operation in progress to | 
|  | 4291 | * decrypt the encrypted premaster secret, so skip | 
|  | 4292 | * directly to resuming this operation. */ | 
|  | 4293 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "PSK identity already parsed" ) ); | 
|  | 4294 | /* Update p to skip the PSK identity. ssl_parse_encrypted_pms | 
|  | 4295 | * won't actually use it, but maintain p anyway for robustness. */ | 
|  | 4296 | p += ssl->conf->psk_identity_len + 2; | 
|  | 4297 | } | 
|  | 4298 | else | 
| Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 4299 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 4300 | if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 ) | 
|  | 4301 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4302 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret ); | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 4303 | return( ret ); | 
|  | 4304 | } | 
|  | 4305 |  | 
| Hanno Becker | 845b946 | 2018-10-26 12:07:29 +0100 | [diff] [blame] | 4306 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 4307 | /* Opaque PSKs are currently only supported for PSK-only. */ | 
|  | 4308 | if( ssl_use_opaque_psk( ssl ) == 1 ) | 
| Manuel Pégourié-Gonnard | d80d8a4 | 2022-06-14 10:53:15 +0200 | [diff] [blame] | 4309 | { | 
|  | 4310 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "opaque PSK not supported with RSA-PSK" ) ); | 
| Hanno Becker | 845b946 | 2018-10-26 12:07:29 +0100 | [diff] [blame] | 4311 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
| Manuel Pégourié-Gonnard | d80d8a4 | 2022-06-14 10:53:15 +0200 | [diff] [blame] | 4312 | } | 
| Hanno Becker | 845b946 | 2018-10-26 12:07:29 +0100 | [diff] [blame] | 4313 | #endif | 
|  | 4314 |  | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 4315 | if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 ) | 
|  | 4316 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4317 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret ); | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 4318 | return( ret ); | 
|  | 4319 | } | 
|  | 4320 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4321 | if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl, | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 4322 | ciphersuite_info->key_exchange ) ) != 0 ) | 
|  | 4323 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4324 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret ); | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 4325 | return( ret ); | 
|  | 4326 | } | 
|  | 4327 | } | 
|  | 4328 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4329 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ | 
|  | 4330 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) | 
|  | 4331 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4332 | { | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4333 | if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 ) | 
|  | 4334 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4335 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4336 | return( ret ); | 
|  | 4337 | } | 
|  | 4338 | if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 ) | 
|  | 4339 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4340 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4341 | return( ret ); | 
|  | 4342 | } | 
|  | 4343 |  | 
| Hanno Becker | 845b946 | 2018-10-26 12:07:29 +0100 | [diff] [blame] | 4344 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 4345 | /* Opaque PSKs are currently only supported for PSK-only. */ | 
|  | 4346 | if( ssl_use_opaque_psk( ssl ) == 1 ) | 
| Manuel Pégourié-Gonnard | d80d8a4 | 2022-06-14 10:53:15 +0200 | [diff] [blame] | 4347 | { | 
|  | 4348 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "opaque PSK not supported with DHE-PSK" ) ); | 
| Hanno Becker | 845b946 | 2018-10-26 12:07:29 +0100 | [diff] [blame] | 4349 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
| Manuel Pégourié-Gonnard | d80d8a4 | 2022-06-14 10:53:15 +0200 | [diff] [blame] | 4350 | } | 
| Hanno Becker | 845b946 | 2018-10-26 12:07:29 +0100 | [diff] [blame] | 4351 | #endif | 
|  | 4352 |  | 
| Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 4353 | if( p != end ) | 
|  | 4354 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4355 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) ); | 
|  | 4356 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); | 
| Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 4357 | } | 
|  | 4358 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4359 | if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl, | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 4360 | ciphersuite_info->key_exchange ) ) != 0 ) | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4361 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4362 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret ); | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 4363 | return( ret ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4364 | } | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4365 | } | 
|  | 4366 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4367 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ | 
|  | 4368 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) | 
|  | 4369 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) | 
| Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 4370 | { | 
| Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 4371 | if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 ) | 
|  | 4372 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4373 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret ); | 
| Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 4374 | return( ret ); | 
|  | 4375 | } | 
| Manuel Pégourié-Gonnard | b59d699 | 2013-10-14 12:00:45 +0200 | [diff] [blame] | 4376 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4377 | if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx, | 
| Manuel Pégourié-Gonnard | b59d699 | 2013-10-14 12:00:45 +0200 | [diff] [blame] | 4378 | p, end - p ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 4379 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4380 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret ); | 
|  | 4381 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP ); | 
| Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 4382 | } | 
|  | 4383 |  | 
| Hanno Becker | 845b946 | 2018-10-26 12:07:29 +0100 | [diff] [blame] | 4384 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 4385 | /* Opaque PSKs are currently only supported for PSK-only. */ | 
|  | 4386 | if( ssl_use_opaque_psk( ssl ) == 1 ) | 
| Manuel Pégourié-Gonnard | d80d8a4 | 2022-06-14 10:53:15 +0200 | [diff] [blame] | 4387 | { | 
|  | 4388 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "opaque PSK not supported with ECDHE-PSK" ) ); | 
| Hanno Becker | 845b946 | 2018-10-26 12:07:29 +0100 | [diff] [blame] | 4389 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
| Manuel Pégourié-Gonnard | d80d8a4 | 2022-06-14 10:53:15 +0200 | [diff] [blame] | 4390 | } | 
| Hanno Becker | 845b946 | 2018-10-26 12:07:29 +0100 | [diff] [blame] | 4391 | #endif | 
|  | 4392 |  | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 4393 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, | 
|  | 4394 | MBEDTLS_DEBUG_ECDH_QP ); | 
| Manuel Pégourié-Gonnard | b59d699 | 2013-10-14 12:00:45 +0200 | [diff] [blame] | 4395 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4396 | if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl, | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 4397 | ciphersuite_info->key_exchange ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 4398 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4399 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret ); | 
| Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 4400 | return( ret ); | 
|  | 4401 | } | 
| Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 4402 | } | 
|  | 4403 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4404 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ | 
|  | 4405 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) | 
|  | 4406 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA ) | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 4407 | { | 
| Manuel Pégourié-Gonnard | 2114d72 | 2014-09-10 13:59:41 +0000 | [diff] [blame] | 4408 | if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 0 ) ) != 0 ) | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 4409 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4410 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret ); | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 4411 | return( ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4412 | } | 
|  | 4413 | } | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4414 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4415 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */ | 
| Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 4416 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
|  | 4417 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ) | 
|  | 4418 | { | 
|  | 4419 | ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx, | 
|  | 4420 | p, end - p ); | 
|  | 4421 | if( ret != 0 ) | 
|  | 4422 | { | 
|  | 4423 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret ); | 
|  | 4424 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); | 
|  | 4425 | } | 
|  | 4426 |  | 
|  | 4427 | ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx, | 
|  | 4428 | ssl->handshake->premaster, 32, &ssl->handshake->pmslen, | 
|  | 4429 | ssl->conf->f_rng, ssl->conf->p_rng ); | 
|  | 4430 | if( ret != 0 ) | 
|  | 4431 | { | 
|  | 4432 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret ); | 
|  | 4433 | return( ret ); | 
|  | 4434 | } | 
|  | 4435 | } | 
|  | 4436 | else | 
|  | 4437 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4438 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4439 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 4440 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4441 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4442 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4443 | if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 ) | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 4444 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4445 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret ); | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 4446 | return( ret ); | 
|  | 4447 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4448 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4449 | ssl->state++; | 
|  | 4450 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4451 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4452 |  | 
|  | 4453 | return( 0 ); | 
|  | 4454 | } | 
|  | 4455 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4456 | #if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED) | 
| Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 4457 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4458 | static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4459 | { | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 4460 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | 
| Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 4461 | ssl->handshake->ciphersuite_info; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4462 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4463 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4464 |  | 
| Hanno Becker | 77adddc | 2019-02-07 12:32:43 +0000 | [diff] [blame] | 4465 | if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ) | 
| Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 4466 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4467 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) ); | 
| Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 4468 | ssl->state++; | 
|  | 4469 | return( 0 ); | 
|  | 4470 | } | 
|  | 4471 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4472 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 4473 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4474 | } | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4475 | #else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */ | 
| Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 4476 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4477 | static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4478 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4479 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; | 
| Manuel Pégourié-Gonnard | 4528f3f | 2014-09-10 14:17:23 +0000 | [diff] [blame] | 4480 | size_t i, sig_len; | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4481 | unsigned char hash[48]; | 
| Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 4482 | unsigned char *hash_start = hash; | 
| Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4483 | size_t hashlen; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4484 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 4485 | mbedtls_pk_type_t pk_alg; | 
| Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 4486 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4487 | mbedtls_md_type_t md_alg; | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 4488 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | 
| Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 4489 | ssl->handshake->ciphersuite_info; | 
| Hanno Becker | a1ab9be | 2019-02-06 18:31:04 +0000 | [diff] [blame] | 4490 | mbedtls_pk_context * peer_pk; | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4491 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4492 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4493 |  | 
| Hanno Becker | 2a831a4 | 2019-02-07 13:17:25 +0000 | [diff] [blame] | 4494 | if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ) | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4495 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4496 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4497 | ssl->state++; | 
|  | 4498 | return( 0 ); | 
|  | 4499 | } | 
|  | 4500 |  | 
| Hanno Becker | 2a831a4 | 2019-02-07 13:17:25 +0000 | [diff] [blame] | 4501 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 4502 | if( ssl->session_negotiate->peer_cert == NULL ) | 
|  | 4503 | { | 
|  | 4504 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) ); | 
|  | 4505 | ssl->state++; | 
|  | 4506 | return( 0 ); | 
|  | 4507 | } | 
|  | 4508 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 4509 | if( ssl->session_negotiate->peer_cert_digest == NULL ) | 
|  | 4510 | { | 
|  | 4511 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) ); | 
|  | 4512 | ssl->state++; | 
|  | 4513 | return( 0 ); | 
|  | 4514 | } | 
|  | 4515 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 4516 |  | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4517 | /* Read the message without adding it to the checksum */ | 
| Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 4518 | ret = mbedtls_ssl_read_record( ssl, 0 /* no checksum update */ ); | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4519 | if( 0 != ret ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4520 | { | 
| Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 4521 | MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record" ), ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4522 | return( ret ); | 
|  | 4523 | } | 
|  | 4524 |  | 
|  | 4525 | ssl->state++; | 
|  | 4526 |  | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4527 | /* Process the message contents */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4528 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE || | 
|  | 4529 | ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE_VERIFY ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4530 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4531 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) ); | 
|  | 4532 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4533 | } | 
|  | 4534 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4535 | i = mbedtls_ssl_hs_hdr_len( ssl ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4536 |  | 
| Hanno Becker | a1ab9be | 2019-02-06 18:31:04 +0000 | [diff] [blame] | 4537 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 4538 | peer_pk = &ssl->handshake->peer_pubkey; | 
|  | 4539 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 4540 | if( ssl->session_negotiate->peer_cert == NULL ) | 
|  | 4541 | { | 
|  | 4542 | /* Should never happen */ | 
|  | 4543 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 4544 | } | 
|  | 4545 | peer_pk = &ssl->session_negotiate->peer_cert->pk; | 
|  | 4546 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 4547 |  | 
| Manuel Pégourié-Gonnard | 4528f3f | 2014-09-10 14:17:23 +0000 | [diff] [blame] | 4548 | /* | 
|  | 4549 | *  struct { | 
|  | 4550 | *     SignatureAndHashAlgorithm algorithm; -- TLS 1.2 only | 
|  | 4551 | *     opaque signature<0..2^16-1>; | 
|  | 4552 | *  } DigitallySigned; | 
|  | 4553 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4554 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ | 
|  | 4555 | defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
|  | 4556 | if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 ) | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 4557 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4558 | md_alg = MBEDTLS_MD_NONE; | 
| Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4559 | hashlen = 36; | 
| Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 4560 |  | 
|  | 4561 | /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */ | 
| Hanno Becker | a1ab9be | 2019-02-06 18:31:04 +0000 | [diff] [blame] | 4562 | if( mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_ECDSA ) ) | 
| Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 4563 | { | 
|  | 4564 | hash_start += 16; | 
|  | 4565 | hashlen -= 16; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4566 | md_alg = MBEDTLS_MD_SHA1; | 
| Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 4567 | } | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 4568 | } | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 4569 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4570 | #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || | 
|  | 4571 | MBEDTLS_SSL_PROTO_TLS1_1 */ | 
|  | 4572 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 4573 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) | 
| Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 4574 | { | 
| Manuel Pégourié-Gonnard | 5ee9654 | 2014-09-10 14:27:21 +0000 | [diff] [blame] | 4575 | if( i + 2 > ssl->in_hslen ) | 
|  | 4576 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4577 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) ); | 
|  | 4578 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY ); | 
| Manuel Pégourié-Gonnard | 5ee9654 | 2014-09-10 14:27:21 +0000 | [diff] [blame] | 4579 | } | 
|  | 4580 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4581 | /* | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4582 | * Hash | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4583 | */ | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4584 | md_alg = mbedtls_ssl_md_alg_from_hash( ssl->in_msg[i] ); | 
|  | 4585 |  | 
|  | 4586 | if( md_alg == MBEDTLS_MD_NONE || mbedtls_ssl_set_calc_verify_md( ssl, ssl->in_msg[i] ) ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4587 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4588 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg" | 
| Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4589 | " for verify message" ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4590 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY ); | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 4591 | } | 
|  | 4592 |  | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4593 | #if !defined(MBEDTLS_MD_SHA1) | 
|  | 4594 | if( MBEDTLS_MD_SHA1 == md_alg ) | 
|  | 4595 | hash_start += 16; | 
|  | 4596 | #endif | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 4597 |  | 
| Manuel Pégourié-Gonnard | bfe32ef | 2013-08-22 14:55:30 +0200 | [diff] [blame] | 4598 | /* Info from md_alg will be used instead */ | 
|  | 4599 | hashlen = 0; | 
| Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4600 |  | 
| Manuel Pégourié-Gonnard | 4528f3f | 2014-09-10 14:17:23 +0000 | [diff] [blame] | 4601 | i++; | 
|  | 4602 |  | 
| Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4603 | /* | 
|  | 4604 | * Signature | 
|  | 4605 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4606 | if( ( pk_alg = mbedtls_ssl_pk_alg_from_sig( ssl->in_msg[i] ) ) | 
|  | 4607 | == MBEDTLS_PK_NONE ) | 
| Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4608 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4609 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg" | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4610 | " for verify message" ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4611 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY ); | 
| Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4612 | } | 
|  | 4613 |  | 
| Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4614 | /* | 
|  | 4615 | * Check the certificate's key type matches the signature alg | 
|  | 4616 | */ | 
| Hanno Becker | a1ab9be | 2019-02-06 18:31:04 +0000 | [diff] [blame] | 4617 | if( !mbedtls_pk_can_do( peer_pk, pk_alg ) ) | 
| Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4618 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4619 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) ); | 
|  | 4620 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY ); | 
| Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4621 | } | 
| Manuel Pégourié-Gonnard | 4528f3f | 2014-09-10 14:17:23 +0000 | [diff] [blame] | 4622 |  | 
|  | 4623 | i++; | 
| Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 4624 | } | 
|  | 4625 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4626 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 4627 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4628 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 4629 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 4630 | } | 
| Manuel Pégourié-Gonnard | ff56da3 | 2013-07-11 10:46:21 +0200 | [diff] [blame] | 4631 |  | 
| Manuel Pégourié-Gonnard | 5ee9654 | 2014-09-10 14:27:21 +0000 | [diff] [blame] | 4632 | if( i + 2 > ssl->in_hslen ) | 
|  | 4633 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4634 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) ); | 
|  | 4635 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY ); | 
| Manuel Pégourié-Gonnard | 5ee9654 | 2014-09-10 14:27:21 +0000 | [diff] [blame] | 4636 | } | 
|  | 4637 |  | 
| Manuel Pégourié-Gonnard | 4528f3f | 2014-09-10 14:17:23 +0000 | [diff] [blame] | 4638 | sig_len = ( ssl->in_msg[i] << 8 ) | ssl->in_msg[i+1]; | 
|  | 4639 | i += 2; | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 4640 |  | 
| Manuel Pégourié-Gonnard | 4528f3f | 2014-09-10 14:17:23 +0000 | [diff] [blame] | 4641 | if( i + sig_len != ssl->in_hslen ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4642 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4643 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) ); | 
|  | 4644 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4645 | } | 
|  | 4646 |  | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4647 | /* Calculate hash and verify signature */ | 
| Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 4648 | { | 
|  | 4649 | size_t dummy_hlen; | 
|  | 4650 | ssl->handshake->calc_verify( ssl, hash, &dummy_hlen ); | 
|  | 4651 | } | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4652 |  | 
| Hanno Becker | a1ab9be | 2019-02-06 18:31:04 +0000 | [diff] [blame] | 4653 | if( ( ret = mbedtls_pk_verify( peer_pk, | 
| Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 4654 | md_alg, hash_start, hashlen, | 
| Manuel Pégourié-Gonnard | 4528f3f | 2014-09-10 14:17:23 +0000 | [diff] [blame] | 4655 | ssl->in_msg + i, sig_len ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4656 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4657 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4658 | return( ret ); | 
|  | 4659 | } | 
|  | 4660 |  | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4661 | mbedtls_ssl_update_handshake_status( ssl ); | 
|  | 4662 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4663 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4664 |  | 
| Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 4665 | return( ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4666 | } | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4667 | #endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4668 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4669 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | 
| Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 4670 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4671 | static int ssl_write_new_session_ticket( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4672 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4673 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 4674 | size_t tlen; | 
| Manuel Pégourié-Gonnard | b0394be | 2015-05-19 11:40:30 +0200 | [diff] [blame] | 4675 | uint32_t lifetime; | 
| Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4676 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4677 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) ); | 
| Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4678 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4679 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; | 
|  | 4680 | ssl->out_msg[0]  = MBEDTLS_SSL_HS_NEW_SESSION_TICKET; | 
| Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4681 |  | 
|  | 4682 | /* | 
|  | 4683 | * struct { | 
|  | 4684 | *     uint32 ticket_lifetime_hint; | 
|  | 4685 | *     opaque ticket<0..2^16-1>; | 
|  | 4686 | * } NewSessionTicket; | 
|  | 4687 | * | 
|  | 4688 | * 4  .  7   ticket_lifetime_hint (0 = unspecified) | 
|  | 4689 | * 8  .  9   ticket_len (n) | 
|  | 4690 | * 10 .  9+n ticket content | 
|  | 4691 | */ | 
| Manuel Pégourié-Gonnard | 164d894 | 2013-09-23 22:01:39 +0200 | [diff] [blame] | 4692 |  | 
| Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 4693 | if( ( ret = ssl->conf->f_ticket_write( ssl->conf->p_ticket, | 
| Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 4694 | ssl->session_negotiate, | 
|  | 4695 | ssl->out_msg + 10, | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4696 | ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN, | 
| Manuel Pégourié-Gonnard | b0394be | 2015-05-19 11:40:30 +0200 | [diff] [blame] | 4697 | &tlen, &lifetime ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 4698 | { | 
| Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 4699 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_ticket_write", ret ); | 
| Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 4700 | tlen = 0; | 
|  | 4701 | } | 
| Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4702 |  | 
| Joe Subbiani | c54e908 | 2021-07-19 11:56:54 +0100 | [diff] [blame] | 4703 | MBEDTLS_PUT_UINT32_BE( lifetime, ssl->out_msg, 4 ); | 
|  | 4704 | MBEDTLS_PUT_UINT16_BE( tlen, ssl->out_msg, 8 ); | 
| Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 4705 | ssl->out_msglen = 10 + tlen; | 
| Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4706 |  | 
| Manuel Pégourié-Gonnard | 145dfcb | 2014-02-26 14:23:33 +0100 | [diff] [blame] | 4707 | /* | 
|  | 4708 | * Morally equivalent to updating ssl->state, but NewSessionTicket and | 
|  | 4709 | * ChangeCipherSpec share the same state. | 
|  | 4710 | */ | 
|  | 4711 | ssl->handshake->new_session_ticket = 0; | 
|  | 4712 |  | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4713 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4714 | { | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4715 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); | 
| Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4716 | return( ret ); | 
|  | 4717 | } | 
|  | 4718 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4719 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) ); | 
| Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4720 |  | 
|  | 4721 | return( 0 ); | 
|  | 4722 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4723 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ | 
| Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4724 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4725 | /* | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4726 | * SSL handshake -- server side -- single step | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4727 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4728 | int mbedtls_ssl_handshake_server_step( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4729 | { | 
|  | 4730 | int ret = 0; | 
|  | 4731 |  | 
| Manuel Pégourié-Gonnard | dba460f | 2015-06-24 22:59:30 +0200 | [diff] [blame] | 4732 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4733 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4734 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4735 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) ); | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4736 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4737 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4738 | return( ret ); | 
|  | 4739 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4740 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4741 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4742 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4743 | { | 
| Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4744 | if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4745 | return( ret ); | 
|  | 4746 | } | 
| Hanno Becker | bc2498a | 2018-08-28 10:13:29 +0100 | [diff] [blame] | 4747 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4748 |  | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4749 | switch( ssl->state ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4750 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4751 | case MBEDTLS_SSL_HELLO_REQUEST: | 
|  | 4752 | ssl->state = MBEDTLS_SSL_CLIENT_HELLO; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4753 | break; | 
|  | 4754 |  | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4755 | /* | 
|  | 4756 | *  <==   ClientHello | 
|  | 4757 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4758 | case MBEDTLS_SSL_CLIENT_HELLO: | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4759 | ret = ssl_parse_client_hello( ssl ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4760 | break; | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4761 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4762 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 4763 | case MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT: | 
|  | 4764 | return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED ); | 
| Manuel Pégourié-Gonnard | 579950c | 2014-09-29 17:47:33 +0200 | [diff] [blame] | 4765 | #endif | 
|  | 4766 |  | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4767 | /* | 
|  | 4768 | *  ==>   ServerHello | 
|  | 4769 | *        Certificate | 
|  | 4770 | *      ( ServerKeyExchange  ) | 
|  | 4771 | *      ( CertificateRequest ) | 
|  | 4772 | *        ServerHelloDone | 
|  | 4773 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4774 | case MBEDTLS_SSL_SERVER_HELLO: | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4775 | ret = ssl_write_server_hello( ssl ); | 
|  | 4776 | break; | 
|  | 4777 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4778 | case MBEDTLS_SSL_SERVER_CERTIFICATE: | 
|  | 4779 | ret = mbedtls_ssl_write_certificate( ssl ); | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4780 | break; | 
|  | 4781 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4782 | case MBEDTLS_SSL_SERVER_KEY_EXCHANGE: | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4783 | ret = ssl_write_server_key_exchange( ssl ); | 
|  | 4784 | break; | 
|  | 4785 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4786 | case MBEDTLS_SSL_CERTIFICATE_REQUEST: | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4787 | ret = ssl_write_certificate_request( ssl ); | 
|  | 4788 | break; | 
|  | 4789 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4790 | case MBEDTLS_SSL_SERVER_HELLO_DONE: | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4791 | ret = ssl_write_server_hello_done( ssl ); | 
|  | 4792 | break; | 
|  | 4793 |  | 
|  | 4794 | /* | 
|  | 4795 | *  <== ( Certificate/Alert  ) | 
|  | 4796 | *        ClientKeyExchange | 
|  | 4797 | *      ( CertificateVerify  ) | 
|  | 4798 | *        ChangeCipherSpec | 
|  | 4799 | *        Finished | 
|  | 4800 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4801 | case MBEDTLS_SSL_CLIENT_CERTIFICATE: | 
|  | 4802 | ret = mbedtls_ssl_parse_certificate( ssl ); | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4803 | break; | 
|  | 4804 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4805 | case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE: | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4806 | ret = ssl_parse_client_key_exchange( ssl ); | 
|  | 4807 | break; | 
|  | 4808 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4809 | case MBEDTLS_SSL_CERTIFICATE_VERIFY: | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4810 | ret = ssl_parse_certificate_verify( ssl ); | 
|  | 4811 | break; | 
|  | 4812 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4813 | case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC: | 
|  | 4814 | ret = mbedtls_ssl_parse_change_cipher_spec( ssl ); | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4815 | break; | 
|  | 4816 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4817 | case MBEDTLS_SSL_CLIENT_FINISHED: | 
|  | 4818 | ret = mbedtls_ssl_parse_finished( ssl ); | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4819 | break; | 
|  | 4820 |  | 
|  | 4821 | /* | 
| Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4822 | *  ==> ( NewSessionTicket ) | 
|  | 4823 | *        ChangeCipherSpec | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4824 | *        Finished | 
|  | 4825 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4826 | case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC: | 
|  | 4827 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | 
| Manuel Pégourié-Gonnard | 7cd5924 | 2013-08-02 13:24:41 +0200 | [diff] [blame] | 4828 | if( ssl->handshake->new_session_ticket != 0 ) | 
|  | 4829 | ret = ssl_write_new_session_ticket( ssl ); | 
|  | 4830 | else | 
| Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 4831 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4832 | ret = mbedtls_ssl_write_change_cipher_spec( ssl ); | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4833 | break; | 
|  | 4834 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4835 | case MBEDTLS_SSL_SERVER_FINISHED: | 
|  | 4836 | ret = mbedtls_ssl_write_finished( ssl ); | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4837 | break; | 
|  | 4838 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4839 | case MBEDTLS_SSL_FLUSH_BUFFERS: | 
|  | 4840 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) ); | 
|  | 4841 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4842 | break; | 
|  | 4843 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4844 | case MBEDTLS_SSL_HANDSHAKE_WRAPUP: | 
|  | 4845 | mbedtls_ssl_handshake_wrapup( ssl ); | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4846 | break; | 
|  | 4847 |  | 
|  | 4848 | default: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4849 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) ); | 
|  | 4850 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4851 | } | 
|  | 4852 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4853 | return( ret ); | 
|  | 4854 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4855 | #endif /* MBEDTLS_SSL_SRV_C */ |