Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SSLv3/TLSv1 server-side functions |
| 3 | * |
Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
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 | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 18 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 22 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 23 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 24 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 25 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 26 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 27 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 28 | #if defined(MBEDTLS_SSL_SRV_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 29 | |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 30 | #if defined(MBEDTLS_PLATFORM_C) |
| 31 | #include "mbedtls/platform.h" |
| 32 | #else |
| 33 | #include <stdlib.h> |
| 34 | #define mbedtls_calloc calloc |
| 35 | #define mbedtls_free free |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 36 | #endif |
| 37 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 38 | #include "mbedtls/debug.h" |
| 39 | #include "mbedtls/ssl.h" |
Manuel Pégourié-Gonnard | 5e94dde | 2015-05-26 11:57:05 +0200 | [diff] [blame] | 40 | #include "mbedtls/ssl_internal.h" |
Andres Amaya Garcia | 8491406 | 2018-04-24 08:40:46 -0500 | [diff] [blame] | 41 | #include "mbedtls/platform_util.h" |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 42 | |
| 43 | #include <string.h> |
| 44 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 45 | #if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 46 | #include "mbedtls/ecp.h" |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 47 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 48 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 49 | #if defined(MBEDTLS_HAVE_TIME) |
Simon Butcher | b5b6af2 | 2016-07-13 14:46:18 +0100 | [diff] [blame] | 50 | #include "mbedtls/platform_time.h" |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 51 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 52 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 53 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) |
| 54 | 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] | 55 | const unsigned char *info, |
| 56 | size_t ilen ) |
| 57 | { |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 58 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 59 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 60 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 61 | mbedtls_free( ssl->cli_id ); |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 62 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 63 | if( ( ssl->cli_id = mbedtls_calloc( 1, ilen ) ) == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 64 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 65 | |
Teppo Järvelin | 6f4e030 | 2019-10-04 13:53:53 +0300 | [diff] [blame] | 66 | /* Not using more secure mbedtls_platform_memcpy as id is public*/ |
| 67 | memcpy( ssl->cli_id, info, ilen ); |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 68 | ssl->cli_id_len = ilen; |
| 69 | |
| 70 | return( 0 ); |
| 71 | } |
Manuel Pégourié-Gonnard | d485d19 | 2014-07-23 14:56:15 +0200 | [diff] [blame] | 72 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 73 | void mbedtls_ssl_conf_dtls_cookies( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 74 | mbedtls_ssl_cookie_write_t *f_cookie_write, |
| 75 | mbedtls_ssl_cookie_check_t *f_cookie_check, |
Manuel Pégourié-Gonnard | d485d19 | 2014-07-23 14:56:15 +0200 | [diff] [blame] | 76 | void *p_cookie ) |
| 77 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 78 | conf->f_cookie_write = f_cookie_write; |
| 79 | conf->f_cookie_check = f_cookie_check; |
| 80 | conf->p_cookie = p_cookie; |
Manuel Pégourié-Gonnard | d485d19 | 2014-07-23 14:56:15 +0200 | [diff] [blame] | 81 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 82 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */ |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 83 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 84 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 85 | static int ssl_parse_servername_ext( mbedtls_ssl_context *ssl, |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 86 | const unsigned char *buf, |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 87 | size_t len ) |
| 88 | { |
| 89 | int ret; |
| 90 | size_t servername_list_size, hostname_len; |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 91 | const unsigned char *p; |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 92 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 93 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) ); |
Manuel Pégourié-Gonnard | 96ea2f2 | 2014-02-25 12:26:29 +0100 | [diff] [blame] | 94 | |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 95 | if( len < 2 ) |
| 96 | { |
| 97 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 98 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 99 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 100 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 101 | } |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 102 | servername_list_size = mbedtls_platform_get_uint16_be( buf ); |
| 103 | |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 104 | if( servername_list_size + 2 != len ) |
| 105 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 106 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 107 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 108 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 109 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | p = buf + 2; |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 113 | while( servername_list_size > 2 ) |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 114 | { |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 115 | hostname_len = mbedtls_platform_get_uint16_be( &p[1] ); |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 116 | if( hostname_len + 3 > servername_list_size ) |
| 117 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 118 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 119 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 120 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 121 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 124 | if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME ) |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 125 | { |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 126 | ret = ssl->conf->f_sni( ssl->conf->p_sni, |
| 127 | ssl, p + 3, hostname_len ); |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 128 | if( ret != 0 ) |
| 129 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 130 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 131 | mbedtls_ssl_pend_fatal_alert( ssl, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 132 | MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME ); |
| 133 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 134 | } |
Paul Bakker | 81420ab | 2012-10-23 10:31:15 +0000 | [diff] [blame] | 135 | return( 0 ); |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | servername_list_size -= hostname_len + 3; |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 139 | p += hostname_len + 3; |
| 140 | } |
| 141 | |
| 142 | if( servername_list_size != 0 ) |
| 143 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 144 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 145 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 146 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 147 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | return( 0 ); |
| 151 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 152 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 153 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 154 | static int ssl_parse_renegotiation_info( mbedtls_ssl_context *ssl, |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 155 | const unsigned char *buf, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 156 | size_t len ) |
| 157 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 158 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 159 | if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ) |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 160 | { |
| 161 | /* Check verify-data in constant-time. The length OTOH is no secret */ |
| 162 | if( len != 1 + ssl->verify_data_len || |
| 163 | buf[0] != ssl->verify_data_len || |
Teppo Järvelin | 707ceb8 | 2019-10-04 07:49:39 +0300 | [diff] [blame] | 164 | mbedtls_platform_memcmp( buf + 1, ssl->peer_verify_data, |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 165 | ssl->verify_data_len ) != 0 ) |
| 166 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 167 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 168 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 169 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 170 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 174 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 175 | { |
| 176 | if( len != 1 || buf[0] != 0x0 ) |
| 177 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 178 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 179 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 180 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 181 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 184 | ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 185 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 186 | |
| 187 | return( 0 ); |
| 188 | } |
| 189 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 190 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 191 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 192 | |
| 193 | /* |
| 194 | * Status of the implementation of signature-algorithms extension: |
| 195 | * |
| 196 | * Currently, we are only considering the signature-algorithm extension |
| 197 | * to pick a ciphersuite which allows us to send the ServerKeyExchange |
| 198 | * message with a signature-hash combination that the user allows. |
| 199 | * |
| 200 | * We do *not* check whether all certificates in our certificate |
| 201 | * chain are signed with an allowed signature-hash pair. |
| 202 | * This needs to be done at a later stage. |
| 203 | * |
| 204 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 205 | static int ssl_parse_signature_algorithms_ext( mbedtls_ssl_context *ssl, |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 206 | const unsigned char *buf, |
| 207 | size_t len ) |
| 208 | { |
| 209 | size_t sig_alg_list_size; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 210 | |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 211 | const unsigned char *p; |
Manuel Pégourié-Gonnard | 08e81e0 | 2014-07-08 12:56:25 +0200 | [diff] [blame] | 212 | const unsigned char *end = buf + len; |
Manuel Pégourié-Gonnard | 08e81e0 | 2014-07-08 12:56:25 +0200 | [diff] [blame] | 213 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 214 | mbedtls_md_type_t md_cur; |
| 215 | mbedtls_pk_type_t sig_cur; |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 216 | |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 217 | if ( len < 2 ) { |
| 218 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 219 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 220 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 221 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 222 | } |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 223 | |
| 224 | sig_alg_list_size = mbedtls_platform_get_uint16_be( buf ); |
| 225 | |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 226 | if( sig_alg_list_size + 2 != len || |
Manuel Pégourié-Gonnard | 08e81e0 | 2014-07-08 12:56:25 +0200 | [diff] [blame] | 227 | sig_alg_list_size % 2 != 0 ) |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 228 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 229 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 230 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 231 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 232 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 235 | /* Currently we only guarantee signing the ServerKeyExchange message according |
| 236 | * to the constraints specified in this extension (see above), so it suffices |
| 237 | * 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] | 238 | * |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 239 | * This will change when we also consider certificate signatures, |
| 240 | * in which case we will need to remember the whole signature-hash |
| 241 | * pair list from the extension. |
Manuel Pégourié-Gonnard | 08e81e0 | 2014-07-08 12:56:25 +0200 | [diff] [blame] | 242 | */ |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 243 | |
| 244 | for( p = buf + 2; p < end; p += 2 ) |
| 245 | { |
| 246 | /* Silently ignore unknown signature or hash algorithms. */ |
| 247 | |
| 248 | if( ( sig_cur = mbedtls_ssl_pk_alg_from_sig( p[1] ) ) == MBEDTLS_PK_NONE ) |
| 249 | { |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 250 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext" |
| 251 | " unknown sig alg encoding %d", p[1] ) ); |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 252 | continue; |
| 253 | } |
| 254 | |
| 255 | /* Check if we support the hash the user proposes */ |
| 256 | md_cur = mbedtls_ssl_md_alg_from_hash( p[0] ); |
| 257 | if( md_cur == MBEDTLS_MD_NONE ) |
| 258 | { |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 259 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext:" |
| 260 | " unknown hash alg encoding %d", p[0] ) ); |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 261 | continue; |
| 262 | } |
| 263 | |
| 264 | if( mbedtls_ssl_check_sig_hash( ssl, md_cur ) == 0 ) |
| 265 | { |
| 266 | 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] | 267 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext:" |
| 268 | " match sig %d and hash %d", |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 269 | sig_cur, md_cur ) ); |
| 270 | } |
| 271 | else |
| 272 | { |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 273 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: " |
| 274 | "hash alg %d not supported", md_cur ) ); |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 275 | } |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 278 | return( 0 ); |
| 279 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 280 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && |
| 281 | MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 282 | |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 283 | #if defined(MBEDTLS_ECDH_C) || \ |
| 284 | defined(MBEDTLS_ECDSA_C) || \ |
| 285 | defined(MBEDTLS_USE_TINYCRYPT) || \ |
Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 286 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 287 | static int ssl_parse_supported_elliptic_curves( mbedtls_ssl_context *ssl, |
Hanno Becker | d3b2fcb | 2019-06-17 14:30:05 +0100 | [diff] [blame] | 288 | const unsigned char *buf, size_t len, |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 289 | unsigned char const **list_start, size_t *list_len ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 290 | { |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 291 | size_t list_size; |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 292 | const unsigned char *p; |
| 293 | |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 294 | if ( len < 2 ) { |
| 295 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 296 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 297 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 298 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 299 | } |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 300 | |
| 301 | list_size = mbedtls_platform_get_uint16_be( buf ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 302 | if( list_size + 2 != len || |
| 303 | list_size % 2 != 0 ) |
| 304 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 305 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 306 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 307 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 308 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | p = buf + 2; |
Hanno Becker | d3b2fcb | 2019-06-17 14:30:05 +0100 | [diff] [blame] | 312 | |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 313 | /* Remember list for later. */ |
| 314 | *list_start = p; |
| 315 | *list_len = list_size / 2; |
| 316 | |
| 317 | while( list_size > 0 ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 318 | { |
Arto Kinnunen | a3fa06e | 2019-09-09 12:22:51 +0300 | [diff] [blame] | 319 | uint16_t const peer_tls_id = (uint16_t) |
| 320 | mbedtls_platform_get_uint16_be( p ); |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 321 | |
Hanno Becker | a4a9c69 | 2019-06-18 16:55:47 +0100 | [diff] [blame] | 322 | MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_EC_TLS_ID( own_tls_id ) |
| 323 | if( own_tls_id == peer_tls_id && |
| 324 | ssl->handshake->curve_tls_id == 0 ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 325 | { |
Hanno Becker | a4a9c69 | 2019-06-18 16:55:47 +0100 | [diff] [blame] | 326 | ssl->handshake->curve_tls_id = own_tls_id; |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 327 | } |
Hanno Becker | a4a9c69 | 2019-06-18 16:55:47 +0100 | [diff] [blame] | 328 | MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_EC_TLS_ID |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 329 | |
| 330 | list_size -= 2; |
| 331 | p += 2; |
| 332 | } |
| 333 | |
| 334 | return( 0 ); |
| 335 | } |
| 336 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 337 | static int ssl_parse_supported_point_formats( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 338 | const unsigned char *buf, |
| 339 | size_t len ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 340 | { |
| 341 | size_t list_size; |
| 342 | const unsigned char *p; |
| 343 | |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 344 | if( len == 0 || (size_t)( buf[0] + 1 ) != len ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 345 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 346 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 347 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 348 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 349 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 350 | } |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 351 | list_size = buf[0]; |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 352 | |
Manuel Pégourié-Gonnard | c1b46d0 | 2015-09-16 11:18:32 +0200 | [diff] [blame] | 353 | p = buf + 1; |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 354 | while( list_size > 0 ) |
| 355 | { |
Hanno Becker | 27b7e50 | 2019-08-23 14:39:50 +0100 | [diff] [blame] | 356 | if( p[0] == MBEDTLS_SSL_EC_PF_UNCOMPRESSED || |
| 357 | p[0] == MBEDTLS_SSL_EC_PF_COMPRESSED ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 358 | { |
Hanno Becker | 975b9ee | 2019-07-24 10:09:27 +0100 | [diff] [blame] | 359 | #if defined(MBEDTLS_ECDH_C) |
Manuel Pégourié-Gonnard | 5734b2d | 2013-08-15 19:04:02 +0200 | [diff] [blame] | 360 | ssl->handshake->ecdh_ctx.point_format = p[0]; |
Robert Cragie | 136884c | 2015-10-02 13:34:31 +0100 | [diff] [blame] | 361 | #endif |
Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 362 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Robert Cragie | 136884c | 2015-10-02 13:34:31 +0100 | [diff] [blame] | 363 | ssl->handshake->ecjpake_ctx.point_format = p[0]; |
| 364 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 365 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 366 | return( 0 ); |
| 367 | } |
| 368 | |
| 369 | list_size--; |
| 370 | p++; |
| 371 | } |
| 372 | |
| 373 | return( 0 ); |
| 374 | } |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 375 | #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || MBEDTLS_USE_TINYCRYPT |
Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 376 | MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 377 | |
Manuel Pégourié-Gonnard | bf57be6 | 2015-09-16 15:04:01 +0200 | [diff] [blame] | 378 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 379 | static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl, |
| 380 | const unsigned char *buf, |
| 381 | size_t len ) |
| 382 | { |
| 383 | int ret; |
| 384 | |
| 385 | if( mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 ) |
| 386 | { |
| 387 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) ); |
| 388 | return( 0 ); |
| 389 | } |
| 390 | |
| 391 | if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx, |
| 392 | buf, len ) ) != 0 ) |
| 393 | { |
| 394 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 395 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 396 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Manuel Pégourié-Gonnard | bf57be6 | 2015-09-16 15:04:01 +0200 | [diff] [blame] | 397 | return( ret ); |
| 398 | } |
| 399 | |
| 400 | /* Only mark the extension as OK when we're sure it is */ |
| 401 | ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK; |
| 402 | |
| 403 | return( 0 ); |
| 404 | } |
| 405 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
| 406 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 407 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 408 | 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] | 409 | const unsigned char *buf, |
| 410 | size_t len ) |
| 411 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 412 | 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] | 413 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 414 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 415 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 416 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 417 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 48f8d0d | 2013-07-17 10:25:37 +0200 | [diff] [blame] | 418 | } |
| 419 | |
Manuel Pégourié-Gonnard | ed4af8b | 2013-07-18 14:07:09 +0200 | [diff] [blame] | 420 | ssl->session_negotiate->mfl_code = buf[0]; |
| 421 | |
Manuel Pégourié-Gonnard | 48f8d0d | 2013-07-17 10:25:37 +0200 | [diff] [blame] | 422 | return( 0 ); |
| 423 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 424 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 48f8d0d | 2013-07-17 10:25:37 +0200 | [diff] [blame] | 425 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 426 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | c403b26 | 2019-04-26 13:56:39 +0100 | [diff] [blame] | 427 | static int ssl_parse_cid_ext( mbedtls_ssl_context *ssl, |
| 428 | const unsigned char *buf, |
| 429 | size_t len ) |
| 430 | { |
| 431 | size_t peer_cid_len; |
| 432 | |
| 433 | /* CID extension only makes sense in DTLS */ |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 434 | if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ) |
Hanno Becker | c403b26 | 2019-04-26 13:56:39 +0100 | [diff] [blame] | 435 | { |
| 436 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 437 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 438 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Hanno Becker | c403b26 | 2019-04-26 13:56:39 +0100 | [diff] [blame] | 439 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 440 | } |
| 441 | |
| 442 | /* |
Hanno Becker | 3cdf8fe | 2019-05-15 10:26:32 +0100 | [diff] [blame] | 443 | * Quoting draft-ietf-tls-dtls-connection-id-05 |
| 444 | * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05 |
Hanno Becker | c403b26 | 2019-04-26 13:56:39 +0100 | [diff] [blame] | 445 | * |
| 446 | * struct { |
| 447 | * opaque cid<0..2^8-1>; |
| 448 | * } ConnectionId; |
| 449 | */ |
| 450 | |
| 451 | if( len < 1 ) |
| 452 | { |
| 453 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 454 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 455 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Hanno Becker | c403b26 | 2019-04-26 13:56:39 +0100 | [diff] [blame] | 456 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 457 | } |
| 458 | |
| 459 | peer_cid_len = *buf++; |
| 460 | len--; |
| 461 | |
| 462 | if( len != peer_cid_len ) |
| 463 | { |
| 464 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 465 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 466 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Hanno Becker | c403b26 | 2019-04-26 13:56:39 +0100 | [diff] [blame] | 467 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 468 | } |
| 469 | |
| 470 | /* Ignore CID if the user has disabled its use. */ |
| 471 | if( ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED ) |
| 472 | { |
| 473 | /* Leave ssl->handshake->cid_in_use in its default |
| 474 | * value of MBEDTLS_SSL_CID_DISABLED. */ |
| 475 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Client sent CID extension, but CID disabled" ) ); |
| 476 | return( 0 ); |
| 477 | } |
| 478 | |
| 479 | if( peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX ) |
| 480 | { |
| 481 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 482 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 483 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Hanno Becker | c403b26 | 2019-04-26 13:56:39 +0100 | [diff] [blame] | 484 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 485 | } |
| 486 | |
Hanno Becker | 19976b5 | 2019-05-03 12:43:44 +0100 | [diff] [blame] | 487 | ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED; |
Hanno Becker | c403b26 | 2019-04-26 13:56:39 +0100 | [diff] [blame] | 488 | ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len; |
Teppo Järvelin | 6f4e030 | 2019-10-04 13:53:53 +0300 | [diff] [blame] | 489 | /* Not using more secure mbedtls_platform_memcpy as peer_cid is is public */ |
| 490 | memcpy( ssl->handshake->peer_cid, buf, peer_cid_len ); |
Hanno Becker | c403b26 | 2019-04-26 13:56:39 +0100 | [diff] [blame] | 491 | |
| 492 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use of CID extension negotiated" ) ); |
| 493 | MBEDTLS_SSL_DEBUG_BUF( 3, "Client CID", buf, peer_cid_len ); |
| 494 | |
Hanno Becker | c403b26 | 2019-04-26 13:56:39 +0100 | [diff] [blame] | 495 | return( 0 ); |
| 496 | } |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 497 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | c403b26 | 2019-04-26 13:56:39 +0100 | [diff] [blame] | 498 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 499 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 500 | 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] | 501 | const unsigned char *buf, |
| 502 | size_t len ) |
| 503 | { |
| 504 | if( len != 0 ) |
| 505 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 506 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 507 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 508 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 509 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | ((void) buf); |
| 513 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 514 | if( ssl->conf->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 515 | ssl->session_negotiate->trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 516 | |
| 517 | return( 0 ); |
| 518 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 519 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 520 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 521 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 522 | 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] | 523 | const unsigned char *buf, |
| 524 | size_t len ) |
| 525 | { |
| 526 | if( len != 0 ) |
| 527 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 528 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 529 | mbedtls_ssl_pend_fatal_alert( ssl, |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 530 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 531 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | ((void) buf); |
| 535 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 536 | if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED && |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 537 | mbedtls_ssl_get_minor_ver( ssl ) != MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 538 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 539 | ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | return( 0 ); |
| 543 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 544 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 545 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 546 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 547 | 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] | 548 | const unsigned char *buf, |
| 549 | size_t len ) |
| 550 | { |
| 551 | if( len != 0 ) |
| 552 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 553 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 554 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 555 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 556 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | ((void) buf); |
| 560 | |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 561 | return( 0 ); |
| 562 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 563 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 564 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 565 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 566 | 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] | 567 | unsigned char *buf, |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 568 | size_t len ) |
| 569 | { |
Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 570 | int ret; |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 571 | mbedtls_ssl_session session; |
Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 572 | |
Manuel Pégourié-Gonnard | bae389b | 2015-06-24 10:45:58 +0200 | [diff] [blame] | 573 | mbedtls_ssl_session_init( &session ); |
| 574 | |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 575 | if( ssl->conf->f_ticket_parse == NULL || |
| 576 | ssl->conf->f_ticket_write == NULL ) |
| 577 | { |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 578 | return( 0 ); |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 579 | } |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 580 | |
Manuel Pégourié-Gonnard | 306827e | 2013-08-02 18:05:14 +0200 | [diff] [blame] | 581 | /* Remember the client asked us to send a new ticket */ |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 582 | ssl->handshake->new_session_ticket = 1; |
| 583 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 584 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) ); |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 585 | |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 586 | if( len == 0 ) |
| 587 | return( 0 ); |
| 588 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 589 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 590 | if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ) |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 591 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 592 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) ); |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 593 | return( 0 ); |
| 594 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 595 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 596 | |
| 597 | /* |
Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 598 | * Failures are ok: just ignore the ticket and proceed. |
| 599 | */ |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 600 | if( ( ret = ssl->conf->f_ticket_parse( ssl->conf->p_ticket, &session, |
| 601 | buf, len ) ) != 0 ) |
Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 602 | { |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 603 | mbedtls_ssl_session_free( &session ); |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 604 | |
| 605 | if( ret == MBEDTLS_ERR_SSL_INVALID_MAC ) |
| 606 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket is not authentic" ) ); |
| 607 | else if( ret == MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED ) |
| 608 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket is expired" ) ); |
| 609 | else |
| 610 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_ticket_parse", ret ); |
| 611 | |
Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 612 | return( 0 ); |
Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 613 | } |
Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 614 | |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 615 | /* |
| 616 | * Keep the session ID sent by the client, since we MUST send it back to |
| 617 | * inform them we're accepting the ticket (RFC 5077 section 3.4) |
| 618 | */ |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 619 | session.id_len = ssl->session_negotiate->id_len; |
Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 620 | mbedtls_platform_memcpy( &session.id, ssl->session_negotiate->id, session.id_len ); |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 621 | |
| 622 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 623 | mbedtls_platform_memcpy( ssl->session_negotiate, &session, sizeof( mbedtls_ssl_session ) ); |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 624 | |
| 625 | /* Zeroize instead of free as we copied the content */ |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 626 | mbedtls_platform_zeroize( &session, sizeof( mbedtls_ssl_session ) ); |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 627 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 628 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) ); |
Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 629 | |
Jarno Lamsa | 8d09e57 | 2019-12-19 15:20:19 +0200 | [diff] [blame^] | 630 | ssl->handshake->resume = MBEDTLS_SSL_FI_FLAG_SET; |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 631 | |
Manuel Pégourié-Gonnard | 306827e | 2013-08-02 18:05:14 +0200 | [diff] [blame] | 632 | /* Don't send a new ticket after all, this one is OK */ |
| 633 | ssl->handshake->new_session_ticket = 0; |
| 634 | |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 635 | return( 0 ); |
| 636 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 637 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 638 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 639 | #if defined(MBEDTLS_SSL_ALPN) |
| 640 | static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 14beb08 | 2014-07-08 13:50:35 +0200 | [diff] [blame] | 641 | const unsigned char *buf, size_t len ) |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 642 | { |
Paul Bakker | 14b16c6 | 2014-05-28 11:33:54 +0200 | [diff] [blame] | 643 | size_t list_len, cur_len, ours_len; |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 644 | const unsigned char *theirs, *start, *end; |
| 645 | const char **ours; |
| 646 | |
| 647 | /* If ALPN not configured, just ignore the extension */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 648 | if( ssl->conf->alpn_list == NULL ) |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 649 | return( 0 ); |
| 650 | |
| 651 | /* |
| 652 | * opaque ProtocolName<1..2^8-1>; |
| 653 | * |
| 654 | * struct { |
| 655 | * ProtocolName protocol_name_list<2..2^16-1> |
| 656 | * } ProtocolNameList; |
| 657 | */ |
| 658 | |
| 659 | /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */ |
| 660 | if( len < 4 ) |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 661 | { |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 662 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 663 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 664 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 665 | } |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 666 | |
Arto Kinnunen | a3fa06e | 2019-09-09 12:22:51 +0300 | [diff] [blame] | 667 | list_len = mbedtls_platform_get_uint16_be ( buf ); |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 668 | |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 669 | if( list_len != len - 2 ) |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 670 | { |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 671 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 672 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 673 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 674 | } |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 675 | |
| 676 | /* |
Manuel Pégourié-Gonnard | 239987f | 2018-01-09 10:43:43 +0100 | [diff] [blame] | 677 | * Validate peer's list (lengths) |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 678 | */ |
| 679 | start = buf + 2; |
| 680 | end = buf + len; |
Manuel Pégourié-Gonnard | 239987f | 2018-01-09 10:43:43 +0100 | [diff] [blame] | 681 | for( theirs = start; theirs != end; theirs += cur_len ) |
| 682 | { |
| 683 | cur_len = *theirs++; |
| 684 | |
| 685 | /* Current identifier must fit in list */ |
| 686 | if( cur_len > (size_t)( end - theirs ) ) |
| 687 | { |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 688 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 689 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 239987f | 2018-01-09 10:43:43 +0100 | [diff] [blame] | 690 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 691 | } |
| 692 | |
| 693 | /* Empty strings MUST NOT be included */ |
| 694 | if( cur_len == 0 ) |
| 695 | { |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 696 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 697 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Manuel Pégourié-Gonnard | 239987f | 2018-01-09 10:43:43 +0100 | [diff] [blame] | 698 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | /* |
| 703 | * Use our order of preference |
| 704 | */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 705 | for( ours = ssl->conf->alpn_list; *ours != NULL; ours++ ) |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 706 | { |
Paul Bakker | 14b16c6 | 2014-05-28 11:33:54 +0200 | [diff] [blame] | 707 | ours_len = strlen( *ours ); |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 708 | for( theirs = start; theirs != end; theirs += cur_len ) |
| 709 | { |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 710 | cur_len = *theirs++; |
| 711 | |
Paul Bakker | 14b16c6 | 2014-05-28 11:33:54 +0200 | [diff] [blame] | 712 | if( cur_len == ours_len && |
Teppo Järvelin | 61f412e | 2019-10-03 12:25:22 +0300 | [diff] [blame] | 713 | mbedtls_platform_memcmp( theirs, *ours, cur_len ) == 0 ) |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 714 | { |
| 715 | ssl->alpn_chosen = *ours; |
| 716 | return( 0 ); |
| 717 | } |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | /* If we get there, no match was found */ |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 722 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 723 | MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 724 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 725 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 726 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 727 | |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 728 | /* |
| 729 | * Auxiliary functions for ServerHello parsing and related actions |
| 730 | */ |
| 731 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 732 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 733 | /* |
Manuel Pégourié-Gonnard | 6458e3b | 2015-01-08 14:16:56 +0100 | [diff] [blame] | 734 | * 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] | 735 | */ |
Hanno Becker | 461fa72 | 2019-08-21 17:05:03 +0100 | [diff] [blame] | 736 | #if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 737 | static int ssl_check_key_curve( mbedtls_pk_context *pk, |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 738 | unsigned char const *acceptable_ec_tls_ids, |
| 739 | size_t ec_tls_ids_len ) |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 740 | { |
Hanno Becker | 461fa72 | 2019-08-21 17:05:03 +0100 | [diff] [blame] | 741 | uint16_t tls_id; |
| 742 | |
| 743 | #if defined(MBEDTLS_USE_TINYCRYPT) |
| 744 | ((void) pk); |
| 745 | tls_id = 23; /* TLS ID for Secp256r1. */ |
| 746 | #else |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 747 | mbedtls_ecp_curve_info const *info; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 748 | 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] | 749 | |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 750 | info = mbedtls_ecp_curve_info_from_grp_id( grp_id ); |
| 751 | if( info == NULL ) |
Hanno Becker | d3b2fcb | 2019-06-17 14:30:05 +0100 | [diff] [blame] | 752 | return( -1 ); |
| 753 | |
Hanno Becker | 461fa72 | 2019-08-21 17:05:03 +0100 | [diff] [blame] | 754 | tls_id = info->tls_id; |
| 755 | #endif /* MBEDTLS_USE_TINYCRYPT */ |
| 756 | |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 757 | if( acceptable_ec_tls_ids == NULL ) |
| 758 | return( -1 ); |
| 759 | |
| 760 | while( ec_tls_ids_len-- != 0 ) |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 761 | { |
Arto Kinnunen | 4f4849a | 2019-09-09 10:21:18 +0300 | [diff] [blame] | 762 | uint16_t const cur_tls_id = (uint16_t) |
| 763 | mbedtls_platform_get_uint16_be( acceptable_ec_tls_ids ); |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 764 | |
Hanno Becker | 461fa72 | 2019-08-21 17:05:03 +0100 | [diff] [blame] | 765 | if( cur_tls_id == tls_id ) |
Manuel Pégourié-Gonnard | 6458e3b | 2015-01-08 14:16:56 +0100 | [diff] [blame] | 766 | return( 0 ); |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 767 | |
| 768 | acceptable_ec_tls_ids += 2; |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 769 | } |
| 770 | |
Manuel Pégourié-Gonnard | 6458e3b | 2015-01-08 14:16:56 +0100 | [diff] [blame] | 771 | return( -1 ); |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 772 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 773 | #endif /* MBEDTLS_ECDSA_C */ |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 774 | |
| 775 | /* |
| 776 | * Try picking a certificate for this ciphersuite, |
| 777 | * return 0 on success and -1 on failure. |
| 778 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 779 | static int ssl_pick_cert( mbedtls_ssl_context *ssl, |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 780 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info, |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 781 | unsigned char const *acceptable_ec_tls_ids, |
| 782 | size_t ec_tls_ids_len ) |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 783 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 784 | mbedtls_ssl_key_cert *cur, *list, *fallback = NULL; |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 785 | mbedtls_pk_type_t pk_alg = |
| 786 | mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ); |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 787 | uint32_t flags; |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 788 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 789 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 790 | if( ssl->handshake->sni_key_cert != NULL ) |
| 791 | list = ssl->handshake->sni_key_cert; |
| 792 | else |
| 793 | #endif |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 794 | list = ssl->conf->key_cert; |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 795 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 796 | if( pk_alg == MBEDTLS_PK_NONE ) |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 797 | return( 0 ); |
| 798 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 799 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite requires certificate" ) ); |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 800 | |
Manuel Pégourié-Gonnard | e540b49 | 2015-07-07 12:44:38 +0200 | [diff] [blame] | 801 | if( list == NULL ) |
| 802 | { |
| 803 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server has no certificate" ) ); |
| 804 | return( -1 ); |
| 805 | } |
| 806 | |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 807 | for( cur = list; cur != NULL; cur = cur->next ) |
| 808 | { |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 809 | int match = 1; |
| 810 | mbedtls_pk_context *pk; |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 811 | |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 812 | /* WARNING: With the current X.509 caching architecture, this MUST |
| 813 | * happen outside of the PK acquire/release block, because it moves |
| 814 | * the cached PK context. In a threading-enabled build, this would |
| 815 | * rightfully fail, but lead to a use-after-free otherwise. */ |
| 816 | MBEDTLS_SSL_DEBUG_CRT( 3, "candidate certificate chain, certificate", |
| 817 | cur->cert ); |
| 818 | |
Hanno Becker | 74b89f6 | 2019-02-17 21:22:07 +0000 | [diff] [blame] | 819 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
| 820 | /* ASYNC_PRIVATE may use a NULL entry for the opaque private key, so |
| 821 | * we have to use the public key context to infer the capabilities |
| 822 | * of the key. */ |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 823 | { |
| 824 | int ret; |
| 825 | ret = mbedtls_x509_crt_pk_acquire( cur->cert, &pk ); |
| 826 | if( ret != 0 ) |
Hanno Becker | 2224ccf | 2019-06-28 10:52:45 +0100 | [diff] [blame] | 827 | { |
| 828 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_pk_acquire", ret ); |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 829 | return( ret ); |
Hanno Becker | 2224ccf | 2019-06-28 10:52:45 +0100 | [diff] [blame] | 830 | } |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 831 | } |
Hanno Becker | 74b89f6 | 2019-02-17 21:22:07 +0000 | [diff] [blame] | 832 | #else |
| 833 | /* Outside of ASYNC_PRIVATE, use private key context directly |
| 834 | * instead of querying for the public key context from the |
| 835 | * certificate, so save a few bytes of code. */ |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 836 | pk = cur->key; |
Hanno Becker | 74b89f6 | 2019-02-17 21:22:07 +0000 | [diff] [blame] | 837 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 838 | |
Hanno Becker | 74b89f6 | 2019-02-17 21:22:07 +0000 | [diff] [blame] | 839 | if( ! mbedtls_pk_can_do( pk, pk_alg ) ) |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 840 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 841 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: key type" ) ); |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 842 | match = 0; |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 843 | } |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 844 | |
Hanno Becker | 461fa72 | 2019-08-21 17:05:03 +0100 | [diff] [blame] | 845 | #if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT) |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 846 | if( pk_alg == MBEDTLS_PK_ECDSA && |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 847 | ssl_check_key_curve( pk, |
| 848 | acceptable_ec_tls_ids, |
| 849 | ec_tls_ids_len ) != 0 ) |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 850 | { |
| 851 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: elliptic curve" ) ); |
| 852 | match = 0; |
| 853 | } |
Hanno Becker | d3b2fcb | 2019-06-17 14:30:05 +0100 | [diff] [blame] | 854 | #else |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 855 | ((void) acceptable_ec_tls_ids); |
| 856 | ((void) ec_tls_ids_len); |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 857 | #endif |
| 858 | |
| 859 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Hanno Becker | c6d1c3e | 2019-03-05 13:50:56 +0000 | [diff] [blame] | 860 | mbedtls_x509_crt_pk_release( cur->cert ); |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 861 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
| 862 | |
| 863 | if( match == 0 ) |
| 864 | continue; |
| 865 | |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 866 | /* |
| 867 | * This avoids sending the client a cert it'll reject based on |
| 868 | * keyUsage or other extensions. |
| 869 | * |
| 870 | * It also allows the user to provision different certificates for |
| 871 | * different uses based on keyUsage, eg if they want to avoid signing |
| 872 | * and decrypting with the same RSA key. |
| 873 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 874 | if( mbedtls_ssl_check_cert_usage( cur->cert, ciphersuite_info, |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 875 | MBEDTLS_SSL_IS_SERVER, &flags ) != 0 ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 876 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 877 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: " |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 878 | "(extended) key usage extension" ) ); |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 879 | continue; |
| 880 | } |
| 881 | |
Hanno Becker | 828a8c0 | 2019-02-26 16:48:55 +0000 | [diff] [blame] | 882 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 883 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Manuel Pégourié-Gonnard | df331a5 | 2015-01-08 16:43:07 +0100 | [diff] [blame] | 884 | /* |
| 885 | * Try to select a SHA-1 certificate for pre-1.2 clients, but still |
| 886 | * present them a SHA-higher cert rather than failing if it's the only |
| 887 | * one we got that satisfies the other conditions. |
| 888 | */ |
Hanno Becker | 7bcf2b5 | 2019-07-26 09:02:40 +0100 | [diff] [blame] | 889 | if( mbedtls_ssl_ver_lt( mbedtls_ssl_get_minor_ver( ssl ), |
| 890 | MBEDTLS_SSL_MINOR_VERSION_3 ) ) |
Manuel Pégourié-Gonnard | df331a5 | 2015-01-08 16:43:07 +0100 | [diff] [blame] | 891 | { |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 892 | mbedtls_md_type_t sig_md; |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 893 | { |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 894 | int ret; |
Hanno Becker | 5f268b3 | 2019-05-20 16:26:34 +0100 | [diff] [blame] | 895 | mbedtls_x509_crt_frame const *frame; |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 896 | ret = mbedtls_x509_crt_frame_acquire( cur->cert, &frame ); |
| 897 | if( ret != 0 ) |
Hanno Becker | 2224ccf | 2019-06-28 10:52:45 +0100 | [diff] [blame] | 898 | { |
| 899 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_frame_acquire", ret ); |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 900 | return( ret ); |
Hanno Becker | 2224ccf | 2019-06-28 10:52:45 +0100 | [diff] [blame] | 901 | } |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 902 | sig_md = frame->sig_md; |
Hanno Becker | c6d1c3e | 2019-03-05 13:50:56 +0000 | [diff] [blame] | 903 | mbedtls_x509_crt_frame_release( cur->cert ); |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | if( sig_md != MBEDTLS_MD_SHA1 ) |
| 907 | { |
| 908 | if( fallback == NULL ) |
| 909 | fallback = cur; |
| 910 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 911 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate not preferred: " |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 912 | "sha-2 with pre-TLS 1.2 client" ) ); |
| 913 | continue; |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 914 | } |
Manuel Pégourié-Gonnard | df331a5 | 2015-01-08 16:43:07 +0100 | [diff] [blame] | 915 | } |
Hanno Becker | 828a8c0 | 2019-02-26 16:48:55 +0000 | [diff] [blame] | 916 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || |
| 917 | MBEDTLS_SSL_PROTO_TLS1_1 || |
| 918 | MBEDTLS_SSL_PROTO_SSL3 */ |
Manuel Pégourié-Gonnard | df331a5 | 2015-01-08 16:43:07 +0100 | [diff] [blame] | 919 | |
Manuel Pégourié-Gonnard | 846ba47 | 2015-01-08 13:54:38 +0100 | [diff] [blame] | 920 | /* If we get there, we got a winner */ |
| 921 | break; |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 922 | } |
| 923 | |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 924 | if( cur == NULL ) |
| 925 | cur = fallback; |
| 926 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 927 | /* 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] | 928 | if( cur != NULL ) |
| 929 | { |
| 930 | ssl->handshake->key_cert = cur; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 931 | MBEDTLS_SSL_DEBUG_CRT( 3, "selected certificate chain, certificate", |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 932 | ssl->handshake->key_cert->cert ); |
Manuel Pégourié-Gonnard | df331a5 | 2015-01-08 16:43:07 +0100 | [diff] [blame] | 933 | return( 0 ); |
| 934 | } |
| 935 | |
| 936 | return( -1 ); |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 937 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 938 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 939 | |
| 940 | /* |
| 941 | * Check if a given ciphersuite is suitable for use with our config/keys/etc |
| 942 | * Sets ciphersuite_info only if the suite matches. |
| 943 | */ |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 944 | static int ssl_ciphersuite_is_match( mbedtls_ssl_context *ssl, |
| 945 | mbedtls_ssl_ciphersuite_handle_t suite_info, |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 946 | unsigned char const *acceptable_ec_tls_ids, |
| 947 | size_t ec_tls_ids_len ) |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 948 | { |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 949 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
Hanno Becker | 4cb1f4d | 2017-10-10 15:59:57 +0100 | [diff] [blame] | 950 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 951 | mbedtls_pk_type_t sig_type; |
| 952 | #endif |
| 953 | |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 954 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "trying ciphersuite: %s", |
| 955 | mbedtls_ssl_suite_get_name( suite_info ) ) ); |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 956 | |
Hanno Becker | 7bcf2b5 | 2019-07-26 09:02:40 +0100 | [diff] [blame] | 957 | if( mbedtls_ssl_ver_gt( |
| 958 | mbedtls_ssl_suite_get_min_minor_ver( suite_info ), |
| 959 | mbedtls_ssl_get_minor_ver( ssl ) ) || |
| 960 | mbedtls_ssl_ver_lt( |
| 961 | mbedtls_ssl_suite_get_max_minor_ver( suite_info ), |
| 962 | mbedtls_ssl_get_minor_ver( ssl ) ) ) |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 963 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 964 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: version" ) ); |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 965 | return( 0 ); |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 966 | } |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 967 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 968 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 969 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 970 | ( mbedtls_ssl_suite_get_flags( suite_info ) & |
| 971 | MBEDTLS_CIPHERSUITE_NODTLS ) ) |
| 972 | { |
Manuel Pégourié-Gonnard | d666451 | 2014-02-06 13:26:57 +0100 | [diff] [blame] | 973 | return( 0 ); |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 974 | } |
Manuel Pégourié-Gonnard | d666451 | 2014-02-06 13:26:57 +0100 | [diff] [blame] | 975 | #endif |
| 976 | |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 977 | #if defined(MBEDTLS_ARC4_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 978 | if( ssl->conf->arc4_disabled == MBEDTLS_SSL_ARC4_DISABLED && |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 979 | mbedtls_ssl_suite_get_cipher( suite_info ) == MBEDTLS_CIPHER_ARC4_128 ) |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 980 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 981 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: rc4" ) ); |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 982 | return( 0 ); |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 983 | } |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 984 | #endif |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 985 | |
Manuel Pégourié-Gonnard | e511b4e | 2015-09-16 14:11:09 +0200 | [diff] [blame] | 986 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 987 | if( mbedtls_ssl_suite_get_key_exchange( suite_info ) == |
| 988 | MBEDTLS_KEY_EXCHANGE_ECJPAKE && |
Manuel Pégourié-Gonnard | bf57be6 | 2015-09-16 15:04:01 +0200 | [diff] [blame] | 989 | ( 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] | 990 | { |
Manuel Pégourié-Gonnard | bf57be6 | 2015-09-16 15:04:01 +0200 | [diff] [blame] | 991 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: ecjpake " |
| 992 | "not configured or ext missing" ) ); |
Manuel Pégourié-Gonnard | e511b4e | 2015-09-16 14:11:09 +0200 | [diff] [blame] | 993 | return( 0 ); |
| 994 | } |
| 995 | #endif |
| 996 | |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 997 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 998 | defined(MBEDTLS_USE_TINYCRYPT) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 999 | if( mbedtls_ssl_ciphersuite_uses_ec( suite_info ) && |
Hanno Becker | 004619f | 2019-06-18 16:07:32 +0100 | [diff] [blame] | 1000 | ssl->handshake->curve_tls_id == 0 ) |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 1001 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1002 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: " |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 1003 | "no common elliptic curve" ) ); |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 1004 | return( 0 ); |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 1005 | } |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 1006 | #endif |
| 1007 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1008 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 1009 | /* If the ciphersuite requires a pre-shared key and we don't |
| 1010 | * have one, skip it now rather than failing later */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1011 | if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) && |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1012 | ssl->conf->f_psk == NULL && |
| 1013 | ( ssl->conf->psk == NULL || ssl->conf->psk_identity == NULL || |
| 1014 | ssl->conf->psk_identity_len == 0 || ssl->conf->psk_len == 0 ) ) |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 1015 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1016 | 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] | 1017 | return( 0 ); |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 1018 | } |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 1019 | #endif |
| 1020 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 1021 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 1022 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
| 1023 | /* If the ciphersuite requires signing, check whether |
| 1024 | * a suitable hash algorithm is present. */ |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 1025 | if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 1026 | { |
| 1027 | sig_type = mbedtls_ssl_get_ciphersuite_sig_alg( suite_info ); |
| 1028 | if( sig_type != MBEDTLS_PK_NONE && |
| 1029 | mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs, sig_type ) == MBEDTLS_MD_NONE ) |
| 1030 | { |
| 1031 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no suitable hash algorithm " |
| 1032 | "for signature algorithm %d", sig_type ) ); |
| 1033 | return( 0 ); |
| 1034 | } |
| 1035 | } |
| 1036 | |
| 1037 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && |
| 1038 | MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
| 1039 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1040 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 1041 | /* |
| 1042 | * Final check: if ciphersuite requires us to have a |
| 1043 | * certificate/key of a particular type: |
| 1044 | * - select the appropriate certificate if we have one, or |
| 1045 | * - try the next ciphersuite if we don't |
| 1046 | * This must be done last since we modify the key_cert list. |
| 1047 | */ |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 1048 | if( ssl_pick_cert( ssl, suite_info, |
| 1049 | acceptable_ec_tls_ids, |
| 1050 | ec_tls_ids_len ) != 0 ) |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 1051 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1052 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: " |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 1053 | "no suitable certificate" ) ); |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 1054 | return( 0 ); |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 1055 | } |
Hanno Becker | d3b2fcb | 2019-06-17 14:30:05 +0100 | [diff] [blame] | 1056 | #else |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 1057 | ((void) acceptable_ec_tls_ids); |
| 1058 | ((void) ec_tls_ids_len); |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 1059 | #endif |
| 1060 | |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 1061 | return( 1 ); |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 1062 | } |
| 1063 | |
Arto Kinnunen | d198672 | 2019-09-24 12:10:19 +0300 | [diff] [blame] | 1064 | #if defined(MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO) && \ |
| 1065 | defined(MBEDTLS_SSL_PROTO_TLS) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1066 | static int ssl_parse_client_hello_v2( mbedtls_ssl_context *ssl ) |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1067 | { |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1068 | int ret, got_common_suite; |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1069 | unsigned int i, j; |
| 1070 | size_t n; |
| 1071 | unsigned int ciph_len, sess_len, chal_len; |
| 1072 | unsigned char *buf, *p; |
Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 1073 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 1074 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info; |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 1075 | #endif |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1076 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1077 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1078 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1079 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1080 | if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ) |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1081 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1082 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 1083 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 1084 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1085 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1086 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1087 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1088 | |
| 1089 | buf = ssl->in_hdr; |
| 1090 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1091 | MBEDTLS_SSL_DEBUG_BUF( 4, "record header", buf, 5 ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1092 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1093 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d", |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1094 | buf[2] ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1095 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d", |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1096 | ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1097 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]", |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1098 | buf[3], buf[4] ) ); |
| 1099 | |
| 1100 | /* |
| 1101 | * SSLv2 Client Hello |
| 1102 | * |
| 1103 | * Record layer: |
| 1104 | * 0 . 1 message length |
| 1105 | * |
| 1106 | * SSL layer: |
| 1107 | * 2 . 2 message type |
| 1108 | * 3 . 4 protocol version |
| 1109 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1110 | if( buf[2] != MBEDTLS_SSL_HS_CLIENT_HELLO || |
| 1111 | buf[3] != MBEDTLS_SSL_MAJOR_VERSION_3 ) |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1112 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1113 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1114 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1115 | } |
| 1116 | |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 1117 | n = mbedtls_platform_get_uint16_be( buf ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1118 | |
| 1119 | if( n < 17 || n > 512 ) |
| 1120 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1121 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1122 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1123 | } |
| 1124 | |
Hanno Becker | 93de296 | 2019-07-25 12:38:18 +0100 | [diff] [blame] | 1125 | #if !defined(MBEDTLS_SSL_CONF_FIXED_MAJOR_VER) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1126 | ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3; |
Hanno Becker | 93de296 | 2019-07-25 12:38:18 +0100 | [diff] [blame] | 1127 | #endif |
| 1128 | |
| 1129 | #if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER) |
Hanno Becker | e965bd3 | 2019-06-12 14:04:34 +0100 | [diff] [blame] | 1130 | ssl->minor_ver = |
| 1131 | ( buf[4] <= mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) ) |
| 1132 | ? buf[4] : mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ); |
Hanno Becker | 93de296 | 2019-07-25 12:38:18 +0100 | [diff] [blame] | 1133 | #endif |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1134 | |
Hanno Becker | 7bcf2b5 | 2019-07-26 09:02:40 +0100 | [diff] [blame] | 1135 | if( mbedtls_ssl_ver_lt( mbedtls_ssl_get_minor_ver( ssl ), |
| 1136 | mbedtls_ssl_conf_get_min_minor_ver( ssl->conf ) ) ) |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1137 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1138 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum" |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 1139 | " [%d:%d] < [%d:%d]", |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 1140 | mbedtls_ssl_get_major_ver( ssl ), |
| 1141 | mbedtls_ssl_get_minor_ver( ssl ), |
Hanno Becker | e965bd3 | 2019-06-12 14:04:34 +0100 | [diff] [blame] | 1142 | mbedtls_ssl_conf_get_min_major_ver( ssl->conf ), |
| 1143 | mbedtls_ssl_conf_get_min_minor_ver( ssl->conf ) ) ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1144 | |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 1145 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 1146 | MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1147 | return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1148 | } |
| 1149 | |
Hanno Becker | 18729ae | 2019-06-12 14:47:21 +0100 | [diff] [blame] | 1150 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \ |
| 1151 | defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
Paul Bakker | 2fbefde | 2013-06-29 16:01:15 +0200 | [diff] [blame] | 1152 | ssl->handshake->max_major_ver = buf[3]; |
| 1153 | ssl->handshake->max_minor_ver = buf[4]; |
Hanno Becker | 18729ae | 2019-06-12 14:47:21 +0100 | [diff] [blame] | 1154 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED || |
| 1155 | MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1156 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1157 | if( ( ret = mbedtls_ssl_fetch_input( ssl, 2 + n ) ) != 0 ) |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1158 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1159 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1160 | return( ret ); |
| 1161 | } |
| 1162 | |
Hanno Becker | 8a4b590 | 2019-08-15 17:04:57 +0100 | [diff] [blame] | 1163 | mbedtls_ssl_update_checksum( ssl, buf + 2, n ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1164 | |
| 1165 | buf = ssl->in_msg; |
| 1166 | n = ssl->in_left - 5; |
| 1167 | |
| 1168 | /* |
| 1169 | * 0 . 1 ciphersuitelist length |
| 1170 | * 2 . 3 session id length |
| 1171 | * 4 . 5 challenge length |
| 1172 | * 6 . .. ciphersuitelist |
| 1173 | * .. . .. session id |
| 1174 | * .. . .. challenge |
| 1175 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1176 | MBEDTLS_SSL_DEBUG_BUF( 4, "record contents", buf, n ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1177 | |
Arto Kinnunen | 4f4849a | 2019-09-09 10:21:18 +0300 | [diff] [blame] | 1178 | ciph_len = (unsigned int)mbedtls_platform_get_uint16_be( &buf[0] ); |
| 1179 | sess_len = (unsigned int)mbedtls_platform_get_uint16_be( &buf[2] ); |
| 1180 | chal_len = (unsigned int)mbedtls_platform_get_uint16_be( &buf[4] ); |
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 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d", |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1183 | ciph_len, sess_len, chal_len ) ); |
| 1184 | |
| 1185 | /* |
| 1186 | * Make sure each parameter length is valid |
| 1187 | */ |
| 1188 | if( ciph_len < 3 || ( ciph_len % 3 ) != 0 ) |
| 1189 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1190 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1191 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | if( sess_len > 32 ) |
| 1195 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1196 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1197 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1198 | } |
| 1199 | |
| 1200 | if( chal_len < 8 || chal_len > 32 ) |
| 1201 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1202 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1203 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1204 | } |
| 1205 | |
| 1206 | if( n != 6 + ciph_len + sess_len + chal_len ) |
| 1207 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1208 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1209 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1210 | } |
| 1211 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1212 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist", |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1213 | buf + 6, ciph_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1214 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id", |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1215 | buf + 6 + ciph_len, sess_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1216 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, challenge", |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1217 | buf + 6 + ciph_len + sess_len, chal_len ); |
| 1218 | |
| 1219 | p = buf + 6 + ciph_len; |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 1220 | ssl->session_negotiate->id_len = sess_len; |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 1221 | memset( ssl->session_negotiate->id, 0, |
| 1222 | sizeof( ssl->session_negotiate->id ) ); |
Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 1223 | mbedtls_platform_memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->id_len ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1224 | |
| 1225 | p += sess_len; |
Jarno Lamsa | 98801af | 2019-12-17 15:57:41 +0200 | [diff] [blame] | 1226 | |
| 1227 | ssl->handshake->hello_random_set = MBEDTLS_SSL_FI_FLAG_UNSET; |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1228 | memset( ssl->handshake->randbytes, 0, 64 ); |
Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 1229 | mbedtls_platform_memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len ); |
Jarno Lamsa | 98801af | 2019-12-17 15:57:41 +0200 | [diff] [blame] | 1230 | if( mbedtls_platform_memcmp( ssl->handshake->randbytes + 32 - chal_len, p, chal_len ) == 0 ) |
| 1231 | { |
| 1232 | ssl->handshake->hello_random_set = MBEDTLS_SSL_FI_FLAG_SET; |
| 1233 | } |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1234 | |
| 1235 | /* |
| 1236 | * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV |
| 1237 | */ |
| 1238 | for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 ) |
| 1239 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1240 | 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] | 1241 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1242 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) ); |
| 1243 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1244 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1245 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1246 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV " |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1247 | "during renegotiation" ) ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1248 | |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 1249 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 1250 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1251 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1252 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1253 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
| 1254 | ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION; |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1255 | break; |
| 1256 | } |
| 1257 | } |
| 1258 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1259 | #if defined(MBEDTLS_SSL_FALLBACK_SCSV) |
Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1260 | for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 ) |
| 1261 | { |
| 1262 | if( p[0] == 0 && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1263 | p[1] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE >> 8 ) & 0xff ) && |
| 1264 | p[2] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE ) & 0xff ) ) |
Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1265 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1266 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "received FALLBACK_SCSV" ) ); |
Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1267 | |
Hanno Becker | 7bcf2b5 | 2019-07-26 09:02:40 +0100 | [diff] [blame] | 1268 | if( mbedtls_ssl_ver_lt( |
| 1269 | mbedtls_ssl_get_minor_ver( ssl ), |
| 1270 | mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) ) ) |
Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1271 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1272 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) ); |
Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1273 | |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 1274 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 1275 | MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK ); |
Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1276 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1277 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1278 | } |
| 1279 | |
| 1280 | break; |
| 1281 | } |
| 1282 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1283 | #endif /* MBEDTLS_SSL_FALLBACK_SCSV */ |
Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1284 | |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1285 | got_common_suite = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1286 | #if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE) |
Manuel Pégourié-Gonnard | 1a9f2c7 | 2013-11-30 18:30:06 +0100 | [diff] [blame] | 1287 | for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 ) |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 1288 | { |
Hanno Becker | 981f81d | 2019-07-19 16:10:49 +0100 | [diff] [blame] | 1289 | MBEDTLS_SSL_BEGIN_FOR_EACH_CIPHERSUITE( ssl, \ |
| 1290 | mbedtls_ssl_get_minor_ver( ssl ), \ |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 1291 | cur_info ) |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1292 | { |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 1293 | #else |
Hanno Becker | 981f81d | 2019-07-19 16:10:49 +0100 | [diff] [blame] | 1294 | MBEDTLS_SSL_BEGIN_FOR_EACH_CIPHERSUITE( ssl, \ |
| 1295 | mbedtls_ssl_get_minor_ver( ssl ), \ |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 1296 | cur_info ) |
| 1297 | { |
| 1298 | for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 ) |
| 1299 | { |
| 1300 | #endif |
| 1301 | const int ciphersuite_id = |
| 1302 | mbedtls_ssl_suite_get_id( cur_info ); |
| 1303 | |
Manuel Pégourié-Gonnard | 011a8db | 2013-11-30 18:11:07 +0100 | [diff] [blame] | 1304 | if( p[0] != 0 || |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 1305 | p[1] != ( ( ciphersuite_id >> 8 ) & 0xFF ) || |
| 1306 | p[2] != ( ( ciphersuite_id ) & 0xFF ) ) |
| 1307 | { |
Hanno Becker | f4d6b49 | 2019-07-02 17:13:14 +0100 | [diff] [blame] | 1308 | continue; |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 1309 | } |
Paul Bakker | 59c28a2 | 2013-06-29 15:33:42 +0200 | [diff] [blame] | 1310 | |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1311 | got_common_suite = 1; |
| 1312 | |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 1313 | if( ssl_ciphersuite_is_match( ssl, cur_info, NULL, 0 ) ) |
Hanno Becker | d3b2fcb | 2019-06-17 14:30:05 +0100 | [diff] [blame] | 1314 | { |
Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 1315 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 1316 | ciphersuite_info = cur_info; |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 1317 | #endif |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 1318 | goto have_ciphersuite_v2; |
Hanno Becker | d3b2fcb | 2019-06-17 14:30:05 +0100 | [diff] [blame] | 1319 | } |
Paul Bakker | 59c28a2 | 2013-06-29 15:33:42 +0200 | [diff] [blame] | 1320 | |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 1321 | #if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE) |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1322 | } |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 1323 | MBEDTLS_SSL_END_FOR_EACH_CIPHERSUITE |
| 1324 | } |
| 1325 | #else |
| 1326 | } |
| 1327 | } |
| 1328 | MBEDTLS_SSL_END_FOR_EACH_CIPHERSUITE |
| 1329 | #endif |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1330 | |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1331 | if( got_common_suite ) |
| 1332 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1333 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, " |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1334 | "but none of them usable" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1335 | return( MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE ); |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1336 | } |
| 1337 | else |
| 1338 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1339 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) ); |
| 1340 | return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN ); |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1341 | } |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1342 | |
| 1343 | have_ciphersuite_v2: |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 1344 | |
Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 1345 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 1346 | ssl->session_negotiate->ciphersuite = |
| 1347 | mbedtls_ssl_suite_get_id( ciphersuite_info ); |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1348 | ssl->handshake->ciphersuite_info = ciphersuite_info; |
Hanno Becker | df64596 | 2019-06-26 13:02:22 +0100 | [diff] [blame] | 1349 | #endif |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1350 | |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 1351 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", |
| 1352 | mbedtls_ssl_get_ciphersuite_name( |
| 1353 | mbedtls_ssl_session_get_ciphersuite( ssl->session_negotiate ) ) ) ); |
| 1354 | |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1355 | /* |
| 1356 | * SSLv2 Client Hello relevant renegotiation security checks |
| 1357 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1358 | if( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && |
Hanno Becker | b0b2b67 | 2019-06-12 16:58:10 +0100 | [diff] [blame] | 1359 | mbedtls_ssl_conf_get_allow_legacy_renegotiation( ssl->conf ) == |
| 1360 | MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE ) |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1361 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1362 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 1363 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 1364 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1365 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1366 | } |
| 1367 | |
| 1368 | ssl->in_left = 0; |
Jarno Lamsa | 2b20516 | 2019-11-12 15:36:21 +0200 | [diff] [blame] | 1369 | ssl->state = MBEDTLS_SSL_SERVER_HELLO; |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1370 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1371 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1372 | |
| 1373 | return( 0 ); |
| 1374 | } |
Arto Kinnunen | d198672 | 2019-09-24 12:10:19 +0300 | [diff] [blame] | 1375 | #endif /* MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO && MBEDTLS_SSL_PROTO_TLS */ |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1376 | |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1377 | /* This function doesn't alert on errors that happen early during |
| 1378 | ClientHello parsing because they might indicate that the client is |
| 1379 | not talking SSL/TLS at all and would not understand our alert. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1380 | static int ssl_parse_client_hello( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1381 | { |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1382 | int ret, got_common_suite; |
Manuel Pégourié-Gonnard | 9de64f5 | 2015-07-01 15:51:43 +0200 | [diff] [blame] | 1383 | size_t i, j; |
| 1384 | size_t ciph_offset, comp_offset, ext_offset; |
| 1385 | 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] | 1386 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 9de64f5 | 2015-07-01 15:51:43 +0200 | [diff] [blame] | 1387 | size_t cookie_offset, cookie_len; |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1388 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1389 | unsigned char *buf, *p, *ext; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1390 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1391 | int renegotiation_info_seen = 0; |
Manuel Pégourié-Gonnard | eaecbd3 | 2014-11-06 02:38:02 +0100 | [diff] [blame] | 1392 | #endif |
Hanno Becker | 03b64fa | 2019-06-11 14:39:38 +0100 | [diff] [blame] | 1393 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 1394 | int extended_ms_seen = 0; |
| 1395 | #endif |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1396 | int handshake_failure = 0; |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 1397 | |
Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 1398 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 1399 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info; |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 1400 | #endif |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 1401 | int major, minor; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1402 | |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 1403 | unsigned char const *acceptable_ec_tls_ids = NULL; |
| 1404 | size_t ec_tls_ids_len = 0; |
Hanno Becker | d3b2fcb | 2019-06-17 14:30:05 +0100 | [diff] [blame] | 1405 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 1406 | /* If there is no signature-algorithm extension present, |
| 1407 | * we need to fall back to the default values for allowed |
| 1408 | * signature-hash pairs. */ |
| 1409 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 1410 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
| 1411 | int sig_hash_alg_ext_present = 0; |
| 1412 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && |
| 1413 | MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
| 1414 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1415 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1416 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1417 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | f03c7aa | 2014-09-24 14:54:06 +0200 | [diff] [blame] | 1418 | read_record_header: |
| 1419 | #endif |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1420 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1421 | * 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] | 1422 | * otherwise read it ourselves manually in order to support SSLv2 |
| 1423 | * ClientHello, which doesn't use the same record layer format. |
| 1424 | */ |
Manuel Pégourié-Gonnard | 754b9f3 | 2019-07-01 12:20:54 +0200 | [diff] [blame] | 1425 | if( mbedtls_ssl_get_renego_status( ssl ) == MBEDTLS_SSL_INITIAL_HANDSHAKE && |
| 1426 | ( ret = mbedtls_ssl_fetch_input( ssl, 5 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1427 | { |
Manuel Pégourié-Gonnard | 754b9f3 | 2019-07-01 12:20:54 +0200 | [diff] [blame] | 1428 | /* No alert on a read error. */ |
| 1429 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret ); |
| 1430 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1431 | } |
| 1432 | |
| 1433 | buf = ssl->in_hdr; |
| 1434 | |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 1435 | #if defined(MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO) && \ |
| 1436 | defined(MBEDTLS_SSL_PROTO_TLS) |
| 1437 | if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) && |
| 1438 | ( buf[0] & 0x80 ) != 0 ) |
| 1439 | { |
| 1440 | return( ssl_parse_client_hello_v2( ssl ) ); |
| 1441 | } |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1442 | #endif |
| 1443 | |
Hanno Becker | 4339576 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 1444 | 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] | 1445 | |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1446 | /* |
Manuel Pégourié-Gonnard | 6b1e207 | 2014-02-12 10:14:54 +0100 | [diff] [blame] | 1447 | * SSLv3/TLS Client Hello |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1448 | * |
| 1449 | * Record layer: |
| 1450 | * 0 . 0 message type |
| 1451 | * 1 . 2 protocol version |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1452 | * 3 . 11 DTLS: epoch + record sequence number |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1453 | * 3 . 4 message length |
| 1454 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1455 | 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] | 1456 | buf[0] ) ); |
| 1457 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1458 | if( buf[0] != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 1459 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1460 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1461 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 1462 | } |
| 1463 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1464 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d", |
Arto Kinnunen | a3fa06e | 2019-09-09 12:22:51 +0300 | [diff] [blame] | 1465 | (int)mbedtls_platform_get_uint16_be( ssl->in_len ) ) ); |
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 | 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] | 1468 | buf[1], buf[2] ) ); |
| 1469 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1470 | 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] | 1471 | |
| 1472 | /* According to RFC 5246 Appendix E.1, the version here is typically |
| 1473 | * "{03,00}, the lowest version number supported by the client, [or] the |
| 1474 | * value of ClientHello.client_version", so the only meaningful check here |
| 1475 | * is the major version shouldn't be less than 3 */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1476 | if( major < MBEDTLS_SSL_MAJOR_VERSION_3 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1477 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1478 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1479 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1480 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1481 | |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1482 | /* For DTLS if this is the initial handshake, remember the client sequence |
| 1483 | * 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] | 1484 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 754b9f3 | 2019-07-01 12:20:54 +0200 | [diff] [blame] | 1485 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
| 1486 | mbedtls_ssl_get_renego_status( ssl ) == MBEDTLS_SSL_INITIAL_HANDSHAKE ) |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1487 | { |
| 1488 | /* Epoch should be 0 for initial handshakes */ |
| 1489 | if( ssl->in_ctr[0] != 0 || ssl->in_ctr[1] != 0 ) |
| 1490 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1491 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1492 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1493 | } |
| 1494 | |
Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 1495 | mbedtls_platform_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] | 1496 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1497 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 1498 | if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 ) |
Manuel Pégourié-Gonnard | f03c7aa | 2014-09-24 14:54:06 +0200 | [diff] [blame] | 1499 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1500 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record, discarding" ) ); |
Manuel Pégourié-Gonnard | f03c7aa | 2014-09-24 14:54:06 +0200 | [diff] [blame] | 1501 | ssl->next_record_offset = 0; |
| 1502 | ssl->in_left = 0; |
| 1503 | goto read_record_header; |
| 1504 | } |
| 1505 | |
| 1506 | /* 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] | 1507 | mbedtls_ssl_dtls_replay_update( ssl ); |
Manuel Pégourié-Gonnard | f03c7aa | 2014-09-24 14:54:06 +0200 | [diff] [blame] | 1508 | #endif |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1509 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1510 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1511 | |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 1512 | msg_len = mbedtls_platform_get_uint16_be( ssl->in_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1513 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1514 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1515 | if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ) |
Manuel Pégourié-Gonnard | b89c4f3 | 2015-01-21 13:24:10 +0000 | [diff] [blame] | 1516 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1517 | /* Set by mbedtls_ssl_read_record() */ |
Manuel Pégourié-Gonnard | b89c4f3 | 2015-01-21 13:24:10 +0000 | [diff] [blame] | 1518 | msg_len = ssl->in_hslen; |
| 1519 | } |
| 1520 | else |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1521 | #endif |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1522 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1523 | if( msg_len > MBEDTLS_SSL_IN_CONTENT_LEN ) |
Manuel Pégourié-Gonnard | d6b721c | 2014-03-24 12:13:54 +0100 | [diff] [blame] | 1524 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1525 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1526 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | d6b721c | 2014-03-24 12:13:54 +0100 | [diff] [blame] | 1527 | } |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1528 | |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 1529 | if( ( ret = mbedtls_ssl_fetch_input( ssl, |
Hanno Becker | 4339576 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 1530 | mbedtls_ssl_in_hdr_len( ssl ) + msg_len ) ) != 0 ) |
Manuel Pégourié-Gonnard | d6b721c | 2014-03-24 12:13:54 +0100 | [diff] [blame] | 1531 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1532 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret ); |
Manuel Pégourié-Gonnard | d6b721c | 2014-03-24 12:13:54 +0100 | [diff] [blame] | 1533 | return( ret ); |
| 1534 | } |
Manuel Pégourié-Gonnard | 30d16eb | 2014-08-19 17:43:50 +0200 | [diff] [blame] | 1535 | |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 1536 | /* Done reading this record, get ready for the next one */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1537 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 1538 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
| 1539 | { |
Hanno Becker | 4339576 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 1540 | ssl->next_record_offset = msg_len + mbedtls_ssl_in_hdr_len( ssl ); |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 1541 | } |
| 1542 | MBEDTLS_SSL_TRANSPORT_ELSE |
Manuel Pégourié-Gonnard | 30d16eb | 2014-08-19 17:43:50 +0200 | [diff] [blame] | 1543 | #endif |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 1544 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
| 1545 | { |
Manuel Pégourié-Gonnard | 30d16eb | 2014-08-19 17:43:50 +0200 | [diff] [blame] | 1546 | ssl->in_left = 0; |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 1547 | } |
| 1548 | #endif |
Manuel Pégourié-Gonnard | d6b721c | 2014-03-24 12:13:54 +0100 | [diff] [blame] | 1549 | } |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1550 | |
| 1551 | buf = ssl->in_msg; |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1552 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1553 | MBEDTLS_SSL_DEBUG_BUF( 4, "record contents", buf, msg_len ); |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 1554 | |
Hanno Becker | 8a4b590 | 2019-08-15 17:04:57 +0100 | [diff] [blame] | 1555 | mbedtls_ssl_update_checksum( ssl, buf, msg_len ); |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1556 | |
| 1557 | /* |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1558 | * Handshake layer: |
| 1559 | * 0 . 0 handshake type |
| 1560 | * 1 . 3 handshake length |
| 1561 | * 4 . 5 DTLS only: message seqence number |
| 1562 | * 6 . 8 DTLS only: fragment offset |
| 1563 | * 9 . 11 DTLS only: fragment length |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 1564 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1565 | if( msg_len < mbedtls_ssl_hs_hdr_len( ssl ) ) |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1566 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1567 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1568 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1569 | } |
| 1570 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1571 | 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] | 1572 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1573 | if( buf[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1574 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1575 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1576 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1577 | } |
| 1578 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1579 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d", |
Arto Kinnunen | a3fa06e | 2019-09-09 12:22:51 +0300 | [diff] [blame] | 1580 | (int)mbedtls_platform_get_uint24_be( &buf[1]) ) ); |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1581 | |
| 1582 | /* We don't support fragmentation of ClientHello (yet?) */ |
| 1583 | if( buf[1] != 0 || |
Arto Kinnunen | a3fa06e | 2019-09-09 12:22:51 +0300 | [diff] [blame] | 1584 | msg_len != ( mbedtls_ssl_hs_hdr_len( ssl ) + |
| 1585 | mbedtls_platform_get_uint16_be( &buf[2]) ) ) |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1586 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1587 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1588 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1589 | } |
| 1590 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1591 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 1592 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 1593 | { |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1594 | /* |
Manuel Pégourié-Gonnard | 69849f8 | 2015-03-10 11:54:02 +0000 | [diff] [blame] | 1595 | * Copy the client's handshake message_seq on initial handshakes, |
| 1596 | * check sequence number on renego. |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1597 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1598 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1599 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 1600 | { |
| 1601 | /* This couldn't be done in ssl_prepare_handshake_record() */ |
Arto Kinnunen | 4f4849a | 2019-09-09 10:21:18 +0300 | [diff] [blame] | 1602 | unsigned int cli_msg_seq = (unsigned int) |
| 1603 | mbedtls_platform_get_uint16_be( &ssl->in_msg[4] ); |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1604 | |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 1605 | if( cli_msg_seq != ssl->handshake->in_msg_seq ) |
| 1606 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1607 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message_seq: " |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 1608 | "%d (expected %d)", cli_msg_seq, |
| 1609 | ssl->handshake->in_msg_seq ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1610 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 1611 | } |
| 1612 | |
| 1613 | ssl->handshake->in_msg_seq++; |
| 1614 | } |
Manuel Pégourié-Gonnard | 69849f8 | 2015-03-10 11:54:02 +0000 | [diff] [blame] | 1615 | else |
| 1616 | #endif |
| 1617 | { |
Arto Kinnunen | 4f4849a | 2019-09-09 10:21:18 +0300 | [diff] [blame] | 1618 | unsigned int cli_msg_seq = (unsigned int) |
| 1619 | mbedtls_platform_get_uint16_be( &ssl->in_msg[4] ); |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 1620 | |
Manuel Pégourié-Gonnard | 69849f8 | 2015-03-10 11:54:02 +0000 | [diff] [blame] | 1621 | ssl->handshake->out_msg_seq = cli_msg_seq; |
| 1622 | ssl->handshake->in_msg_seq = cli_msg_seq + 1; |
| 1623 | } |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 1624 | |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1625 | /* |
| 1626 | * For now we don't support fragmentation, so make sure |
| 1627 | * fragment_offset == 0 and fragment_length == length |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1628 | */ |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 1629 | if( ssl->in_msg[6] != 0 || ssl->in_msg[7] != 0 || ssl->in_msg[8] != 0 || |
Teppo Järvelin | 61f412e | 2019-10-03 12:25:22 +0300 | [diff] [blame] | 1630 | mbedtls_platform_memcmp( ssl->in_msg + 1, ssl->in_msg + 9, 3 ) != 0 ) |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 1631 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1632 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "ClientHello fragmentation not supported" ) ); |
| 1633 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 1634 | } |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 1635 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1636 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 1637 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1638 | buf += mbedtls_ssl_hs_hdr_len( ssl ); |
| 1639 | msg_len -= mbedtls_ssl_hs_hdr_len( ssl ); |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1640 | |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 1641 | /* |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1642 | * ClientHello layer: |
| 1643 | * 0 . 1 protocol version |
| 1644 | * 2 . 33 random bytes (starting with 4 bytes of Unix time) |
| 1645 | * 34 . 35 session id length (1 byte) |
| 1646 | * 35 . 34+x session id |
| 1647 | * 35+x . 35+x DTLS only: cookie length (1 byte) |
| 1648 | * 36+x . .. DTLS only: cookie |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1649 | * .. . .. ciphersuite list length (2 bytes) |
| 1650 | * .. . .. ciphersuite list |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1651 | * .. . .. compression alg. list length (1 byte) |
| 1652 | * .. . .. compression alg. list |
| 1653 | * .. . .. extensions length (2 bytes, optional) |
| 1654 | * .. . .. extensions (optional) |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1655 | */ |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1656 | |
| 1657 | /* |
Antonin Décimo | d5f4759 | 2019-01-23 15:24:37 +0100 | [diff] [blame] | 1658 | * Minimal length (with everything empty and extensions omitted) is |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1659 | * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can |
| 1660 | * read at least up to session id length without worrying. |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1661 | */ |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1662 | if( msg_len < 38 ) |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1663 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1664 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1665 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1666 | } |
| 1667 | |
| 1668 | /* |
| 1669 | * Check and save the protocol version |
| 1670 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1671 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, version", buf, 2 ); |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1672 | |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 1673 | { |
Hanno Becker | 3fa1ee5 | 2019-05-22 14:44:37 +0100 | [diff] [blame] | 1674 | int minor_ver, major_ver; |
| 1675 | mbedtls_ssl_read_version( &major_ver, &minor_ver, |
| 1676 | ssl->conf->transport, |
| 1677 | buf ); |
| 1678 | |
Hanno Becker | 18729ae | 2019-06-12 14:47:21 +0100 | [diff] [blame] | 1679 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \ |
| 1680 | defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
Hanno Becker | 3fa1ee5 | 2019-05-22 14:44:37 +0100 | [diff] [blame] | 1681 | ssl->handshake->max_major_ver = major_ver; |
| 1682 | ssl->handshake->max_minor_ver = minor_ver; |
Hanno Becker | 18729ae | 2019-06-12 14:47:21 +0100 | [diff] [blame] | 1683 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED || |
| 1684 | MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
Hanno Becker | 3fa1ee5 | 2019-05-22 14:44:37 +0100 | [diff] [blame] | 1685 | |
Hanno Becker | 7bcf2b5 | 2019-07-26 09:02:40 +0100 | [diff] [blame] | 1686 | if( mbedtls_ssl_ver_lt( major_ver, |
| 1687 | mbedtls_ssl_conf_get_min_major_ver( ssl->conf ) ) || |
| 1688 | mbedtls_ssl_ver_lt( minor_ver, |
| 1689 | mbedtls_ssl_conf_get_min_minor_ver( ssl->conf ) ) ) |
Hanno Becker | 3fa1ee5 | 2019-05-22 14:44:37 +0100 | [diff] [blame] | 1690 | { |
| 1691 | 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] | 1692 | " [%d:%d] < [%d:%d]", |
Hanno Becker | 3fa1ee5 | 2019-05-22 14:44:37 +0100 | [diff] [blame] | 1693 | major_ver, minor_ver, |
Hanno Becker | e965bd3 | 2019-06-12 14:04:34 +0100 | [diff] [blame] | 1694 | mbedtls_ssl_conf_get_min_major_ver( ssl->conf ), |
| 1695 | mbedtls_ssl_conf_get_min_minor_ver( ssl->conf ) ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 1696 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 1697 | MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION ); |
Hanno Becker | 3fa1ee5 | 2019-05-22 14:44:37 +0100 | [diff] [blame] | 1698 | return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION ); |
| 1699 | } |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 1700 | |
Hanno Becker | 7bcf2b5 | 2019-07-26 09:02:40 +0100 | [diff] [blame] | 1701 | if( mbedtls_ssl_ver_gt( |
| 1702 | major_ver, |
| 1703 | mbedtls_ssl_conf_get_max_major_ver( ssl->conf ) ) ) |
Hanno Becker | 3fa1ee5 | 2019-05-22 14:44:37 +0100 | [diff] [blame] | 1704 | { |
| 1705 | major_ver = mbedtls_ssl_conf_get_max_major_ver( ssl->conf ); |
| 1706 | minor_ver = mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ); |
| 1707 | } |
Hanno Becker | 7bcf2b5 | 2019-07-26 09:02:40 +0100 | [diff] [blame] | 1708 | else if( mbedtls_ssl_ver_gt( |
| 1709 | minor_ver, |
| 1710 | mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) ) ) |
| 1711 | { |
Hanno Becker | 3fa1ee5 | 2019-05-22 14:44:37 +0100 | [diff] [blame] | 1712 | minor_ver = mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ); |
Hanno Becker | 7bcf2b5 | 2019-07-26 09:02:40 +0100 | [diff] [blame] | 1713 | } |
Hanno Becker | 3fa1ee5 | 2019-05-22 14:44:37 +0100 | [diff] [blame] | 1714 | |
Hanno Becker | 381eaa5 | 2019-06-12 14:43:01 +0100 | [diff] [blame] | 1715 | #if !defined(MBEDTLS_SSL_CONF_FIXED_MAJOR_VER) |
Hanno Becker | 3fa1ee5 | 2019-05-22 14:44:37 +0100 | [diff] [blame] | 1716 | ssl->major_ver = major_ver; |
Hanno Becker | 381eaa5 | 2019-06-12 14:43:01 +0100 | [diff] [blame] | 1717 | #endif /* MBEDTLS_SSL_CONF_FIXED_MAJOR_VER */ |
| 1718 | #if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER) |
Hanno Becker | 3fa1ee5 | 2019-05-22 14:44:37 +0100 | [diff] [blame] | 1719 | ssl->minor_ver = minor_ver; |
Hanno Becker | 381eaa5 | 2019-06-12 14:43:01 +0100 | [diff] [blame] | 1720 | #endif /* MBEDTLS_SSL_CONF_FIXED_MINOR_VER */ |
Manuel Pégourié-Gonnard | 6b1e207 | 2014-02-12 10:14:54 +0100 | [diff] [blame] | 1721 | } |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1722 | |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1723 | /* |
| 1724 | * Save client random (inc. Unix time) |
| 1725 | */ |
Jarno Lamsa | 98801af | 2019-12-17 15:57:41 +0200 | [diff] [blame] | 1726 | ssl->handshake->hello_random_set = MBEDTLS_SSL_FI_FLAG_UNSET; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1727 | 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] | 1728 | |
Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 1729 | mbedtls_platform_memcpy( ssl->handshake->randbytes, buf + 2, 32 ); |
Jarno Lamsa | 98801af | 2019-12-17 15:57:41 +0200 | [diff] [blame] | 1730 | if( mbedtls_platform_memcmp( ssl->handshake->randbytes, buf + 2, 32 ) == 0 ) |
| 1731 | { |
| 1732 | ssl->handshake->hello_random_set = MBEDTLS_SSL_FI_FLAG_SET; |
| 1733 | } |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1734 | /* |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1735 | * Check the session ID length and save session ID |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1736 | */ |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1737 | sess_len = buf[34]; |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1738 | |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1739 | if( sess_len > sizeof( ssl->session_negotiate->id ) || |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1740 | sess_len + 34 + 2 > msg_len ) /* 2 for cipherlist length field */ |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1741 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1742 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 1743 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 1744 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1745 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1746 | } |
| 1747 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1748 | 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] | 1749 | |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 1750 | ssl->session_negotiate->id_len = sess_len; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1751 | memset( ssl->session_negotiate->id, 0, |
| 1752 | sizeof( ssl->session_negotiate->id ) ); |
Teppo Järvelin | 6f4e030 | 2019-10-04 13:53:53 +0300 | [diff] [blame] | 1753 | /* Not using more secure mbedtls_platform_memcpy as id is public */ |
| 1754 | memcpy( ssl->session_negotiate->id, buf + 35, |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 1755 | ssl->session_negotiate->id_len ); |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1756 | |
| 1757 | /* |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1758 | * Check the cookie length and content |
| 1759 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1760 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 1761 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1762 | { |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1763 | cookie_offset = 35 + sess_len; |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1764 | cookie_len = buf[cookie_offset]; |
| 1765 | |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1766 | if( cookie_offset + 1 + cookie_len + 2 > msg_len ) |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1767 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1768 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 1769 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 1770 | MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1771 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1772 | } |
| 1773 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1774 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie", |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1775 | buf + cookie_offset + 1, cookie_len ); |
| 1776 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1777 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) |
Manuel Pégourié-Gonnard | 754b9f3 | 2019-07-01 12:20:54 +0200 | [diff] [blame] | 1778 | if( ssl->conf->f_cookie_check != NULL && |
| 1779 | mbedtls_ssl_get_renego_status( ssl ) == MBEDTLS_SSL_INITIAL_HANDSHAKE ) |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1780 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1781 | if( ssl->conf->f_cookie_check( ssl->conf->p_cookie, |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1782 | buf + cookie_offset + 1, cookie_len, |
| 1783 | ssl->cli_id, ssl->cli_id_len ) != 0 ) |
| 1784 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1785 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification failed" ) ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1786 | ssl->handshake->verify_cookie_len = 1; |
| 1787 | } |
| 1788 | else |
| 1789 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1790 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification passed" ) ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1791 | ssl->handshake->verify_cookie_len = 0; |
| 1792 | } |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1793 | } |
Manuel Pégourié-Gonnard | d7f9bc5 | 2014-07-23 11:09:27 +0200 | [diff] [blame] | 1794 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1795 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */ |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1796 | { |
| 1797 | /* We know we didn't send a cookie, so it should be empty */ |
| 1798 | if( cookie_len != 0 ) |
| 1799 | { |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1800 | /* 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] | 1801 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1802 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1803 | } |
| 1804 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1805 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification skipped" ) ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1806 | } |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1807 | |
| 1808 | /* |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1809 | * Check the ciphersuitelist length (will be parsed later) |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1810 | */ |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1811 | ciph_offset = cookie_offset + 1 + cookie_len; |
Manuel Pégourié-Gonnard | a06d7fe | 2015-03-13 10:36:55 +0000 | [diff] [blame] | 1812 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 1813 | MBEDTLS_SSL_TRANSPORT_ELSE |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1814 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 1815 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
| 1816 | { |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1817 | ciph_offset = 35 + sess_len; |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 1818 | } |
Manuel Pégourié-Gonnard | ec1c222 | 2019-06-12 10:18:26 +0200 | [diff] [blame] | 1819 | #endif /* MBEDTLS_SSL_PROTO_TLS */ |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1820 | |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 1821 | |
| 1822 | ciph_len = mbedtls_platform_get_uint16_be( &buf[ciph_offset + 0] ); |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1823 | |
| 1824 | if( ciph_len < 2 || |
| 1825 | ciph_len + 2 + ciph_offset + 1 > msg_len || /* 1 for comp. alg. len */ |
| 1826 | ( ciph_len % 2 ) != 0 ) |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1827 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1828 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 1829 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 1830 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1831 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1832 | } |
| 1833 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1834 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist", |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1835 | buf + ciph_offset + 2, ciph_len ); |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1836 | |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1837 | /* |
| 1838 | * Check the compression algorithms length and pick one |
| 1839 | */ |
| 1840 | comp_offset = ciph_offset + 2 + ciph_len; |
| 1841 | |
| 1842 | comp_len = buf[comp_offset]; |
| 1843 | |
| 1844 | if( comp_len < 1 || |
| 1845 | comp_len > 16 || |
| 1846 | comp_len + comp_offset + 1 > msg_len ) |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1847 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1848 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 1849 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 1850 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1851 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1852 | } |
| 1853 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1854 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, compression", |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1855 | buf + comp_offset + 1, comp_len ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1856 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1857 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Hanno Becker | 8844055 | 2019-07-03 14:16:13 +0100 | [diff] [blame] | 1858 | ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL; |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1859 | for( i = 0; i < comp_len; ++i ) |
| 1860 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1861 | if( buf[comp_offset + 1 + i] == MBEDTLS_SSL_COMPRESS_DEFLATE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1862 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1863 | ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_DEFLATE; |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1864 | break; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1865 | } |
| 1866 | } |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1867 | |
Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1868 | /* See comments in ssl_write_client_hello() */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1869 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 1870 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1871 | ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL; |
Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1872 | #endif |
Hanno Becker | 8844055 | 2019-07-03 14:16:13 +0100 | [diff] [blame] | 1873 | #endif /* MBEDTLS_ZLIB_SUPPORT */ |
Manuel Pégourié-Gonnard | 82202f0 | 2014-07-23 00:28:58 +0200 | [diff] [blame] | 1874 | |
Janos Follath | c6dab2b | 2016-05-23 14:27:02 +0100 | [diff] [blame] | 1875 | /* Do not parse the extensions if the protocol is SSLv3 */ |
| 1876 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 1877 | if( ( mbedtls_ssl_get_major_ver( ssl ) != 3 ) || |
| 1878 | ( mbedtls_ssl_get_minor_ver( ssl ) != 0 ) ) |
Janos Follath | c6dab2b | 2016-05-23 14:27:02 +0100 | [diff] [blame] | 1879 | { |
| 1880 | #endif |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1881 | /* |
| 1882 | * Check the extension length |
| 1883 | */ |
| 1884 | ext_offset = comp_offset + 1 + comp_len; |
| 1885 | if( msg_len > ext_offset ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1886 | { |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1887 | if( msg_len < ext_offset + 2 ) |
| 1888 | { |
| 1889 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 1890 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 1891 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1892 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 1893 | } |
| 1894 | |
Arto Kinnunen | a3fa06e | 2019-09-09 12:22:51 +0300 | [diff] [blame] | 1895 | ext_len = mbedtls_platform_get_uint16_be( &buf[ext_offset + 0] ); |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1896 | |
| 1897 | if( ( ext_len > 0 && ext_len < 4 ) || |
| 1898 | msg_len != ext_offset + 2 + ext_len ) |
| 1899 | { |
| 1900 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 1901 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 1902 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1903 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 1904 | } |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1905 | } |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1906 | else |
| 1907 | ext_len = 0; |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1908 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1909 | ext = buf + ext_offset + 2; |
| 1910 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", ext, ext_len ); |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1911 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1912 | while( ext_len != 0 ) |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1913 | { |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 1914 | unsigned int ext_id; |
| 1915 | unsigned int ext_size; |
| 1916 | if ( ext_len < 4 ) { |
| 1917 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 1918 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 1919 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 1920 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 1921 | } |
Arto Kinnunen | a3fa06e | 2019-09-09 12:22:51 +0300 | [diff] [blame] | 1922 | ext_id = (unsigned int)mbedtls_platform_get_uint16_be( ext ); |
| 1923 | ext_size = (unsigned int)mbedtls_platform_get_uint16_be( &ext[2] ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1924 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1925 | if( ext_size + 4 > ext_len ) |
| 1926 | { |
| 1927 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 1928 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 1929 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1930 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 1931 | } |
| 1932 | switch( ext_id ) |
| 1933 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1934 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1935 | case MBEDTLS_TLS_EXT_SERVERNAME: |
| 1936 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) ); |
| 1937 | if( ssl->conf->f_sni == NULL ) |
| 1938 | break; |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 1939 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1940 | ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size ); |
| 1941 | if( ret != 0 ) |
| 1942 | return( ret ); |
| 1943 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1944 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 1945 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1946 | case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO: |
| 1947 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1948 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1949 | renegotiation_info_seen = 1; |
Manuel Pégourié-Gonnard | eaecbd3 | 2014-11-06 02:38:02 +0100 | [diff] [blame] | 1950 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1951 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1952 | ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size ); |
| 1953 | if( ret != 0 ) |
| 1954 | return( ret ); |
| 1955 | break; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1956 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1957 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 1958 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1959 | case MBEDTLS_TLS_EXT_SIG_ALG: |
Ron Eldor | 73a3817 | 2017-10-03 15:58:26 +0300 | [diff] [blame] | 1960 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) ); |
| 1961 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1962 | ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size ); |
| 1963 | if( ret != 0 ) |
| 1964 | return( ret ); |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 1965 | |
| 1966 | sig_hash_alg_ext_present = 1; |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1967 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1968 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && |
| 1969 | MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1970 | |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 1971 | #if defined(MBEDTLS_ECDH_C) || \ |
| 1972 | defined(MBEDTLS_ECDSA_C) || \ |
| 1973 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) || \ |
| 1974 | defined(MBEDTLS_USE_TINYCRYPT) |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1975 | case MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES: |
| 1976 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1977 | |
Hanno Becker | d3b2fcb | 2019-06-17 14:30:05 +0100 | [diff] [blame] | 1978 | ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, |
| 1979 | ext_size, |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 1980 | &acceptable_ec_tls_ids, |
| 1981 | &ec_tls_ids_len ); |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1982 | if( ret != 0 ) |
| 1983 | return( ret ); |
| 1984 | break; |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1985 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1986 | case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS: |
| 1987 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) ); |
| 1988 | ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT; |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1989 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1990 | ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size ); |
| 1991 | if( ret != 0 ) |
| 1992 | return( ret ); |
| 1993 | break; |
Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 1994 | #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 1995 | MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED || |
| 1996 | MBEDTLS_USE_TINYCRYPT */ |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1997 | |
Manuel Pégourié-Gonnard | bf57be6 | 2015-09-16 15:04:01 +0200 | [diff] [blame] | 1998 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1999 | case MBEDTLS_TLS_EXT_ECJPAKE_KKPP: |
| 2000 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake kkpp extension" ) ); |
Manuel Pégourié-Gonnard | bf57be6 | 2015-09-16 15:04:01 +0200 | [diff] [blame] | 2001 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2002 | ret = ssl_parse_ecjpake_kkpp( ssl, ext + 4, ext_size ); |
| 2003 | if( ret != 0 ) |
| 2004 | return( ret ); |
| 2005 | break; |
Manuel Pégourié-Gonnard | bf57be6 | 2015-09-16 15:04:01 +0200 | [diff] [blame] | 2006 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
| 2007 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2008 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2009 | case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH: |
| 2010 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) ); |
Manuel Pégourié-Gonnard | 48f8d0d | 2013-07-17 10:25:37 +0200 | [diff] [blame] | 2011 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2012 | ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size ); |
| 2013 | if( ret != 0 ) |
| 2014 | return( ret ); |
| 2015 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2016 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 48f8d0d | 2013-07-17 10:25:37 +0200 | [diff] [blame] | 2017 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2018 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2019 | case MBEDTLS_TLS_EXT_TRUNCATED_HMAC: |
| 2020 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) ); |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 2021 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2022 | ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size ); |
| 2023 | if( ret != 0 ) |
| 2024 | return( ret ); |
| 2025 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2026 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 2027 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2028 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | c403b26 | 2019-04-26 13:56:39 +0100 | [diff] [blame] | 2029 | case MBEDTLS_TLS_EXT_CID: |
| 2030 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found CID extension" ) ); |
| 2031 | |
| 2032 | ret = ssl_parse_cid_ext( ssl, ext + 4, ext_size ); |
| 2033 | if( ret != 0 ) |
| 2034 | return( ret ); |
| 2035 | break; |
| 2036 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
| 2037 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2038 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2039 | case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC: |
| 2040 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) ); |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2041 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2042 | ret = ssl_parse_encrypt_then_mac_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_ENCRYPT_THEN_MAC */ |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2047 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2048 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2049 | case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET: |
| 2050 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) ); |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2051 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2052 | ret = ssl_parse_extended_ms_ext( ssl, ext + 4, ext_size ); |
| 2053 | if( ret != 0 ) |
| 2054 | return( ret ); |
Hanno Becker | 03b64fa | 2019-06-11 14:39:38 +0100 | [diff] [blame] | 2055 | extended_ms_seen = 1; |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2056 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2057 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2058 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2059 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2060 | case MBEDTLS_TLS_EXT_SESSION_TICKET: |
| 2061 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) ); |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 2062 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2063 | ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size ); |
| 2064 | if( ret != 0 ) |
| 2065 | return( ret ); |
| 2066 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2067 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 2068 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2069 | #if defined(MBEDTLS_SSL_ALPN) |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2070 | case MBEDTLS_TLS_EXT_ALPN: |
| 2071 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) ); |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 2072 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2073 | ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ); |
| 2074 | if( ret != 0 ) |
| 2075 | return( ret ); |
| 2076 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2077 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 2078 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2079 | default: |
| 2080 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)", |
| 2081 | ext_id ) ); |
| 2082 | } |
| 2083 | |
| 2084 | ext_len -= 4 + ext_size; |
| 2085 | ext += 4 + ext_size; |
| 2086 | |
| 2087 | if( ext_len > 0 && ext_len < 4 ) |
| 2088 | { |
| 2089 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 2090 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 2091 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2092 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 2093 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2094 | } |
Janos Follath | c6dab2b | 2016-05-23 14:27:02 +0100 | [diff] [blame] | 2095 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 2096 | } |
| 2097 | #endif |
| 2098 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2099 | #if defined(MBEDTLS_SSL_FALLBACK_SCSV) |
Gilles Peskine | d50177f | 2017-05-16 17:53:03 +0200 | [diff] [blame] | 2100 | 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] | 2101 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2102 | if( p[0] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE >> 8 ) & 0xff ) && |
| 2103 | p[1] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE ) & 0xff ) ) |
Manuel Pégourié-Gonnard | fedba98 | 2014-11-05 16:12:09 +0100 | [diff] [blame] | 2104 | { |
Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 2105 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "received FALLBACK_SCSV" ) ); |
Manuel Pégourié-Gonnard | fedba98 | 2014-11-05 16:12:09 +0100 | [diff] [blame] | 2106 | |
Hanno Becker | 7bcf2b5 | 2019-07-26 09:02:40 +0100 | [diff] [blame] | 2107 | if( mbedtls_ssl_ver_lt( |
| 2108 | mbedtls_ssl_get_minor_ver( ssl ), |
| 2109 | mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) ) ) |
Manuel Pégourié-Gonnard | fedba98 | 2014-11-05 16:12:09 +0100 | [diff] [blame] | 2110 | { |
Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 2111 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) ); |
Manuel Pégourié-Gonnard | fedba98 | 2014-11-05 16:12:09 +0100 | [diff] [blame] | 2112 | |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 2113 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 2114 | MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK ); |
Manuel Pégourié-Gonnard | fedba98 | 2014-11-05 16:12:09 +0100 | [diff] [blame] | 2115 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2116 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | fedba98 | 2014-11-05 16:12:09 +0100 | [diff] [blame] | 2117 | } |
| 2118 | |
| 2119 | break; |
| 2120 | } |
| 2121 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2122 | #endif /* MBEDTLS_SSL_FALLBACK_SCSV */ |
Manuel Pégourié-Gonnard | fedba98 | 2014-11-05 16:12:09 +0100 | [diff] [blame] | 2123 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 2124 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 2125 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
| 2126 | |
| 2127 | /* |
| 2128 | * Try to fall back to default hash SHA1 if the client |
| 2129 | * hasn't provided any preferred signature-hash combinations. |
| 2130 | */ |
| 2131 | if( sig_hash_alg_ext_present == 0 ) |
| 2132 | { |
| 2133 | mbedtls_md_type_t md_default = MBEDTLS_MD_SHA1; |
| 2134 | |
| 2135 | if( mbedtls_ssl_check_sig_hash( ssl, md_default ) != 0 ) |
| 2136 | md_default = MBEDTLS_MD_NONE; |
| 2137 | |
| 2138 | mbedtls_ssl_sig_hash_set_const_hash( &ssl->handshake->hash_algs, md_default ); |
| 2139 | } |
| 2140 | |
| 2141 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && |
| 2142 | MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
| 2143 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2144 | /* |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 2145 | * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV |
| 2146 | */ |
| 2147 | for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 ) |
| 2148 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2149 | 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] | 2150 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2151 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) ); |
| 2152 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 2153 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 2154 | { |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 2155 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV " |
| 2156 | "during renegotiation" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 2157 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 2158 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2159 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 2160 | } |
Manuel Pégourié-Gonnard | 69849f8 | 2015-03-10 11:54:02 +0000 | [diff] [blame] | 2161 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2162 | ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION; |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 2163 | break; |
| 2164 | } |
| 2165 | } |
| 2166 | |
| 2167 | /* |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2168 | * Renegotiation security checks |
| 2169 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2170 | if( ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION && |
Hanno Becker | b0b2b67 | 2019-06-12 16:58:10 +0100 | [diff] [blame] | 2171 | mbedtls_ssl_conf_get_allow_legacy_renegotiation( ssl->conf ) == |
| 2172 | MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE ) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2173 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2174 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) ); |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2175 | handshake_failure = 1; |
| 2176 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2177 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 2178 | else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && |
| 2179 | ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION && |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2180 | renegotiation_info_seen == 0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2181 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2182 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) ); |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2183 | handshake_failure = 1; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2184 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2185 | else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && |
| 2186 | ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && |
Hanno Becker | b0b2b67 | 2019-06-12 16:58:10 +0100 | [diff] [blame] | 2187 | mbedtls_ssl_conf_get_allow_legacy_renegotiation( ssl->conf ) |
| 2188 | == MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2189 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2190 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) ); |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2191 | handshake_failure = 1; |
| 2192 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2193 | else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && |
| 2194 | ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2195 | renegotiation_info_seen == 1 ) |
| 2196 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2197 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) ); |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2198 | handshake_failure = 1; |
| 2199 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2200 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2201 | |
Jarno Lamsa | 842be16 | 2019-06-10 15:05:33 +0300 | [diff] [blame] | 2202 | /* |
| 2203 | * Check if extended master secret is being enforced |
| 2204 | */ |
| 2205 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Hanno Becker | aabbb58 | 2019-06-11 13:43:27 +0100 | [diff] [blame] | 2206 | if( mbedtls_ssl_conf_get_ems( ssl->conf ) == |
Hanno Becker | 03b64fa | 2019-06-11 14:39:38 +0100 | [diff] [blame] | 2207 | MBEDTLS_SSL_EXTENDED_MS_ENABLED ) |
Jarno Lamsa | 842be16 | 2019-06-10 15:05:33 +0300 | [diff] [blame] | 2208 | { |
Hanno Becker | 03b64fa | 2019-06-11 14:39:38 +0100 | [diff] [blame] | 2209 | if( extended_ms_seen ) |
| 2210 | { |
Hanno Becker | 1ab322b | 2019-06-11 14:50:54 +0100 | [diff] [blame] | 2211 | #if !defined(MBEDTLS_SSL_EXTENDED_MS_ENFORCED) |
Hanno Becker | 03b64fa | 2019-06-11 14:39:38 +0100 | [diff] [blame] | 2212 | ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; |
Hanno Becker | 1ab322b | 2019-06-11 14:50:54 +0100 | [diff] [blame] | 2213 | #endif /* !MBEDTLS_SSL_EXTENDED_MS_ENFORCED */ |
Hanno Becker | 03b64fa | 2019-06-11 14:39:38 +0100 | [diff] [blame] | 2214 | } |
| 2215 | else if( mbedtls_ssl_conf_get_ems_enforced( ssl->conf ) == |
| 2216 | MBEDTLS_SSL_EXTENDED_MS_ENFORCE_ENABLED ) |
| 2217 | { |
| 2218 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Peer not offering extended master " |
| 2219 | "secret, while it is enforced") ); |
| 2220 | handshake_failure = 1; |
| 2221 | } |
Jarno Lamsa | 842be16 | 2019-06-10 15:05:33 +0300 | [diff] [blame] | 2222 | } |
| 2223 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
| 2224 | |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2225 | if( handshake_failure == 1 ) |
| 2226 | { |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 2227 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 2228 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2229 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2230 | } |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2231 | |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2232 | /* |
| 2233 | * Search for a matching ciphersuite |
Manuel Pégourié-Gonnard | 3ebb2cd | 2013-09-23 17:00:18 +0200 | [diff] [blame] | 2234 | * (At the end because we need information from the EC-based extensions |
| 2235 | * and certificate from the SNI callback triggered by the SNI extension.) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2236 | */ |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 2237 | got_common_suite = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2238 | #if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE) |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 2239 | for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 ) |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 2240 | { |
Hanno Becker | 981f81d | 2019-07-19 16:10:49 +0100 | [diff] [blame] | 2241 | MBEDTLS_SSL_BEGIN_FOR_EACH_CIPHERSUITE( ssl, \ |
| 2242 | mbedtls_ssl_get_minor_ver( ssl ), \ |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 2243 | cur_info ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2244 | { |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 2245 | #else |
Hanno Becker | 981f81d | 2019-07-19 16:10:49 +0100 | [diff] [blame] | 2246 | MBEDTLS_SSL_BEGIN_FOR_EACH_CIPHERSUITE( ssl, \ |
| 2247 | mbedtls_ssl_get_minor_ver( ssl ), \ |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 2248 | cur_info ) |
| 2249 | { |
| 2250 | for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 ) |
| 2251 | { |
| 2252 | #endif |
| 2253 | const int ciphersuite_id = |
| 2254 | mbedtls_ssl_suite_get_id( cur_info ); |
| 2255 | |
| 2256 | if( p[0] != ( ( ciphersuite_id >> 8 ) & 0xFF ) || |
| 2257 | p[1] != ( ( ciphersuite_id ) & 0xFF ) ) |
| 2258 | { |
Hanno Becker | f4d6b49 | 2019-07-02 17:13:14 +0100 | [diff] [blame] | 2259 | continue; |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 2260 | } |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2261 | |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 2262 | got_common_suite = 1; |
| 2263 | |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 2264 | if( ssl_ciphersuite_is_match( ssl, cur_info, |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 2265 | acceptable_ec_tls_ids, |
| 2266 | ec_tls_ids_len ) != 0 ) |
Hanno Becker | d3b2fcb | 2019-06-17 14:30:05 +0100 | [diff] [blame] | 2267 | { |
Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 2268 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 2269 | ciphersuite_info = cur_info; |
Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 2270 | #endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 2271 | goto have_ciphersuite; |
Hanno Becker | d3b2fcb | 2019-06-17 14:30:05 +0100 | [diff] [blame] | 2272 | } |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 2273 | #if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2274 | } |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 2275 | MBEDTLS_SSL_END_FOR_EACH_CIPHERSUITE |
| 2276 | } |
| 2277 | #else |
| 2278 | } |
| 2279 | } |
| 2280 | MBEDTLS_SSL_END_FOR_EACH_CIPHERSUITE |
| 2281 | #endif |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2282 | |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 2283 | if( got_common_suite ) |
| 2284 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2285 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, " |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 2286 | "but none of them usable" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 2287 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 2288 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2289 | return( MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE ); |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 2290 | } |
| 2291 | else |
| 2292 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2293 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 2294 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 2295 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2296 | return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN ); |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 2297 | } |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2298 | |
| 2299 | have_ciphersuite: |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 2300 | |
Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 2301 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 2302 | ssl->session_negotiate->ciphersuite = |
| 2303 | mbedtls_ssl_suite_get_id( ciphersuite_info ); |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 2304 | ssl->handshake->ciphersuite_info = ciphersuite_info; |
Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 2305 | #endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2306 | |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 2307 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", |
| 2308 | mbedtls_ssl_get_ciphersuite_name( |
| 2309 | mbedtls_ssl_session_get_ciphersuite( ssl->session_negotiate ) ) ) ); |
| 2310 | |
Jarno Lamsa | 2b20516 | 2019-11-12 15:36:21 +0200 | [diff] [blame] | 2311 | ssl->state = MBEDTLS_SSL_SERVER_HELLO; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2312 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2313 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 2314 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2315 | mbedtls_ssl_recv_flight_completed( ssl ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2316 | #endif |
| 2317 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 2318 | /* Debugging-only output for testsuite */ |
| 2319 | #if defined(MBEDTLS_DEBUG_C) && \ |
| 2320 | defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 2321 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 2322 | if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 2323 | { |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 2324 | mbedtls_pk_type_t sig_alg = mbedtls_ssl_get_ciphersuite_sig_alg( |
| 2325 | mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ) ); |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 2326 | if( sig_alg != MBEDTLS_PK_NONE ) |
| 2327 | { |
| 2328 | mbedtls_md_type_t md_alg = mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs, |
| 2329 | sig_alg ); |
| 2330 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d", |
| 2331 | mbedtls_ssl_hash_from_md_alg( md_alg ) ) ); |
| 2332 | } |
| 2333 | else |
| 2334 | { |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 2335 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no hash algorithm for signature algorithm " |
| 2336 | "%d - should not happen", sig_alg ) ); |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 2337 | } |
| 2338 | } |
| 2339 | #endif |
| 2340 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2341 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2342 | |
| 2343 | return( 0 ); |
| 2344 | } |
| 2345 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2346 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 2347 | 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] | 2348 | unsigned char *buf, |
| 2349 | size_t *olen ) |
| 2350 | { |
| 2351 | unsigned char *p = buf; |
| 2352 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2353 | 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] | 2354 | { |
| 2355 | *olen = 0; |
| 2356 | return; |
| 2357 | } |
| 2358 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2359 | 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] | 2360 | |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 2361 | p = mbedtls_platform_put_uint16_be( p, MBEDTLS_TLS_EXT_TRUNCATED_HMAC ); |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 2362 | *p++ = 0x00; |
| 2363 | *p++ = 0x00; |
| 2364 | |
| 2365 | *olen = 4; |
| 2366 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2367 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 2368 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2369 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 072d4ec | 2019-04-26 15:46:55 +0100 | [diff] [blame] | 2370 | static void ssl_write_cid_ext( mbedtls_ssl_context *ssl, |
| 2371 | unsigned char *buf, |
| 2372 | size_t *olen ) |
| 2373 | { |
| 2374 | unsigned char *p = buf; |
| 2375 | size_t ext_len; |
| 2376 | const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN; |
| 2377 | |
| 2378 | *olen = 0; |
| 2379 | |
| 2380 | /* Skip writing the extension if we don't want to use it or if |
| 2381 | * the client hasn't offered it. */ |
| 2382 | if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_DISABLED ) |
| 2383 | return; |
| 2384 | |
| 2385 | /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX |
| 2386 | * which is at most 255, so the increment cannot overflow. */ |
| 2387 | if( end < p || (size_t)( end - p ) < (unsigned)( ssl->own_cid_len + 5 ) ) |
| 2388 | { |
| 2389 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) ); |
| 2390 | return; |
| 2391 | } |
| 2392 | |
| 2393 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding CID extension" ) ); |
| 2394 | |
| 2395 | /* |
Hanno Becker | 3cdf8fe | 2019-05-15 10:26:32 +0100 | [diff] [blame] | 2396 | * Quoting draft-ietf-tls-dtls-connection-id-05 |
| 2397 | * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05 |
Hanno Becker | 072d4ec | 2019-04-26 15:46:55 +0100 | [diff] [blame] | 2398 | * |
| 2399 | * struct { |
| 2400 | * opaque cid<0..2^8-1>; |
| 2401 | * } ConnectionId; |
| 2402 | */ |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 2403 | p = mbedtls_platform_put_uint16_be( p, MBEDTLS_TLS_EXT_CID ); |
Hanno Becker | 072d4ec | 2019-04-26 15:46:55 +0100 | [diff] [blame] | 2404 | ext_len = (size_t) ssl->own_cid_len + 1; |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 2405 | p = mbedtls_platform_put_uint16_be( p, ext_len ); |
Hanno Becker | 072d4ec | 2019-04-26 15:46:55 +0100 | [diff] [blame] | 2406 | *p++ = (uint8_t) ssl->own_cid_len; |
Teppo Järvelin | 6f4e030 | 2019-10-04 13:53:53 +0300 | [diff] [blame] | 2407 | /* Not using more secure mbedtls_platform_memcpy as cid is public */ |
| 2408 | memcpy( p, ssl->own_cid, ssl->own_cid_len ); |
Hanno Becker | 072d4ec | 2019-04-26 15:46:55 +0100 | [diff] [blame] | 2409 | |
| 2410 | *olen = ssl->own_cid_len + 5; |
| 2411 | } |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2412 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 072d4ec | 2019-04-26 15:46:55 +0100 | [diff] [blame] | 2413 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2414 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 2415 | 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] | 2416 | unsigned char *buf, |
| 2417 | size_t *olen ) |
| 2418 | { |
| 2419 | unsigned char *p = buf; |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 2420 | mbedtls_ssl_ciphersuite_handle_t suite = |
| 2421 | MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2422 | const mbedtls_cipher_info_t *cipher = NULL; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2423 | |
Hanno Becker | 27b34d5 | 2017-10-20 14:24:51 +0100 | [diff] [blame] | 2424 | if( ssl->session_negotiate->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED || |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 2425 | mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2426 | { |
| 2427 | *olen = 0; |
| 2428 | return; |
| 2429 | } |
| 2430 | |
Manuel Pégourié-Gonnard | 78e745f | 2014-11-04 15:44:06 +0100 | [diff] [blame] | 2431 | /* |
| 2432 | * RFC 7366: "If a server receives an encrypt-then-MAC request extension |
| 2433 | * from a client and then selects a stream or Authenticated Encryption |
| 2434 | * with Associated Data (AEAD) ciphersuite, it MUST NOT send an |
| 2435 | * encrypt-then-MAC response extension back to the client." |
| 2436 | */ |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 2437 | suite = mbedtls_ssl_ciphersuite_from_id( |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 2438 | mbedtls_ssl_session_get_ciphersuite( ssl->session_negotiate ) ); |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 2439 | if( suite == MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE ) |
| 2440 | { |
| 2441 | *olen = 0; |
| 2442 | return; |
| 2443 | } |
| 2444 | |
| 2445 | cipher = mbedtls_cipher_info_from_type( |
| 2446 | mbedtls_ssl_suite_get_cipher( suite ) ); |
| 2447 | if( cipher == NULL || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2448 | cipher->mode != MBEDTLS_MODE_CBC ) |
Manuel Pégourié-Gonnard | 78e745f | 2014-11-04 15:44:06 +0100 | [diff] [blame] | 2449 | { |
| 2450 | *olen = 0; |
| 2451 | return; |
| 2452 | } |
| 2453 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2454 | 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] | 2455 | |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 2456 | p = mbedtls_platform_put_uint16_be( p, MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC ); |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2457 | *p++ = 0x00; |
| 2458 | *p++ = 0x00; |
| 2459 | |
| 2460 | *olen = 4; |
| 2461 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2462 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2463 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2464 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 2465 | 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] | 2466 | unsigned char *buf, |
| 2467 | size_t *olen ) |
| 2468 | { |
| 2469 | unsigned char *p = buf; |
| 2470 | |
Hanno Becker | a49ec56 | 2019-06-11 14:47:55 +0100 | [diff] [blame] | 2471 | if( mbedtls_ssl_hs_get_extended_ms( ssl->handshake ) |
| 2472 | == MBEDTLS_SSL_EXTENDED_MS_DISABLED || |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 2473 | mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2474 | { |
| 2475 | *olen = 0; |
| 2476 | return; |
| 2477 | } |
| 2478 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2479 | 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] | 2480 | "extension" ) ); |
| 2481 | |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 2482 | p = mbedtls_platform_put_uint16_be( p, MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET ); |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2483 | |
| 2484 | *p++ = 0x00; |
| 2485 | *p++ = 0x00; |
| 2486 | |
| 2487 | *olen = 4; |
| 2488 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2489 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2490 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2491 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 2492 | 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] | 2493 | unsigned char *buf, |
| 2494 | size_t *olen ) |
| 2495 | { |
| 2496 | unsigned char *p = buf; |
| 2497 | |
| 2498 | if( ssl->handshake->new_session_ticket == 0 ) |
| 2499 | { |
| 2500 | *olen = 0; |
| 2501 | return; |
| 2502 | } |
| 2503 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2504 | 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] | 2505 | |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 2506 | p = mbedtls_platform_put_uint16_be( p, MBEDTLS_TLS_EXT_SESSION_TICKET ); |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 2507 | *p++ = 0x00; |
| 2508 | *p++ = 0x00; |
| 2509 | |
| 2510 | *olen = 4; |
| 2511 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2512 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 2513 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2514 | static void ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | f11a6d7 | 2013-07-17 11:17:14 +0200 | [diff] [blame] | 2515 | unsigned char *buf, |
| 2516 | size_t *olen ) |
| 2517 | { |
| 2518 | unsigned char *p = buf; |
| 2519 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2520 | if( ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION ) |
Manuel Pégourié-Gonnard | f11a6d7 | 2013-07-17 11:17:14 +0200 | [diff] [blame] | 2521 | { |
| 2522 | *olen = 0; |
| 2523 | return; |
| 2524 | } |
| 2525 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2526 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) ); |
Manuel Pégourié-Gonnard | f11a6d7 | 2013-07-17 11:17:14 +0200 | [diff] [blame] | 2527 | |
Arto Kinnunen | a3fa06e | 2019-09-09 12:22:51 +0300 | [diff] [blame] | 2528 | p = mbedtls_platform_put_uint16_be( p, MBEDTLS_TLS_EXT_RENEGOTIATION_INFO ); |
Manuel Pégourié-Gonnard | f11a6d7 | 2013-07-17 11:17:14 +0200 | [diff] [blame] | 2529 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2530 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 2531 | if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ) |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2532 | { |
| 2533 | *p++ = 0x00; |
| 2534 | *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF; |
| 2535 | *p++ = ssl->verify_data_len * 2 & 0xFF; |
Manuel Pégourié-Gonnard | f11a6d7 | 2013-07-17 11:17:14 +0200 | [diff] [blame] | 2536 | |
Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 2537 | mbedtls_platform_memcpy( p, ssl->peer_verify_data, ssl->verify_data_len ); |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2538 | p += ssl->verify_data_len; |
Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 2539 | mbedtls_platform_memcpy( p, ssl->own_verify_data, ssl->verify_data_len ); |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2540 | p += ssl->verify_data_len; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2541 | } |
| 2542 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2543 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2544 | { |
| 2545 | *p++ = 0x00; |
| 2546 | *p++ = 0x01; |
| 2547 | *p++ = 0x00; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2548 | } |
Manuel Pégourié-Gonnard | 1938975 | 2015-06-23 13:46:44 +0200 | [diff] [blame] | 2549 | |
| 2550 | *olen = p - buf; |
Manuel Pégourié-Gonnard | f11a6d7 | 2013-07-17 11:17:14 +0200 | [diff] [blame] | 2551 | } |
| 2552 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2553 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 2554 | 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] | 2555 | unsigned char *buf, |
| 2556 | size_t *olen ) |
| 2557 | { |
| 2558 | unsigned char *p = buf; |
| 2559 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2560 | 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] | 2561 | { |
Manuel Pégourié-Gonnard | 7bb7899 | 2013-07-17 13:50:08 +0200 | [diff] [blame] | 2562 | *olen = 0; |
| 2563 | return; |
| 2564 | } |
| 2565 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2566 | 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] | 2567 | |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 2568 | p = mbedtls_platform_put_uint16_be( p, MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH ); |
Manuel Pégourié-Gonnard | 7bb7899 | 2013-07-17 13:50:08 +0200 | [diff] [blame] | 2569 | *p++ = 0x00; |
| 2570 | *p++ = 1; |
| 2571 | |
Manuel Pégourié-Gonnard | ed4af8b | 2013-07-18 14:07:09 +0200 | [diff] [blame] | 2572 | *p++ = ssl->session_negotiate->mfl_code; |
Manuel Pégourié-Gonnard | 7bb7899 | 2013-07-17 13:50:08 +0200 | [diff] [blame] | 2573 | |
| 2574 | *olen = 5; |
| 2575 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2576 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 7bb7899 | 2013-07-17 13:50:08 +0200 | [diff] [blame] | 2577 | |
Manuel Pégourié-Gonnard | f472179 | 2015-09-15 10:53:51 +0200 | [diff] [blame] | 2578 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 2579 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) || \ |
| 2580 | defined(MBEDTLS_USE_TINYCRYPT) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2581 | 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] | 2582 | unsigned char *buf, |
| 2583 | size_t *olen ) |
| 2584 | { |
| 2585 | unsigned char *p = buf; |
| 2586 | ((void) ssl); |
| 2587 | |
Paul Bakker | 677377f | 2013-10-28 12:54:26 +0100 | [diff] [blame] | 2588 | if( ( ssl->handshake->cli_exts & |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2589 | MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 ) |
Paul Bakker | 677377f | 2013-10-28 12:54:26 +0100 | [diff] [blame] | 2590 | { |
| 2591 | *olen = 0; |
| 2592 | return; |
| 2593 | } |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 2594 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2595 | 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] | 2596 | |
Arto Kinnunen | a3fa06e | 2019-09-09 12:22:51 +0300 | [diff] [blame] | 2597 | p = mbedtls_platform_put_uint16_be( p, MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS ); |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 2598 | |
| 2599 | *p++ = 0x00; |
| 2600 | *p++ = 2; |
| 2601 | |
| 2602 | *p++ = 1; |
Hanno Becker | 27b7e50 | 2019-08-23 14:39:50 +0100 | [diff] [blame] | 2603 | *p++ = MBEDTLS_SSL_EC_PF_UNCOMPRESSED; |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 2604 | |
| 2605 | *olen = 6; |
| 2606 | } |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 2607 | #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] | 2608 | |
Manuel Pégourié-Gonnard | 55c7f99 | 2015-09-16 15:35:27 +0200 | [diff] [blame] | 2609 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 2610 | static void ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl, |
| 2611 | unsigned char *buf, |
| 2612 | size_t *olen ) |
| 2613 | { |
| 2614 | int ret; |
| 2615 | unsigned char *p = buf; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2616 | 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] | 2617 | size_t kkpp_len; |
| 2618 | |
| 2619 | *olen = 0; |
| 2620 | |
| 2621 | /* Skip costly computation if not needed */ |
Hanno Becker | df64596 | 2019-06-26 13:02:22 +0100 | [diff] [blame] | 2622 | if( mbedtls_ssl_suite_get_key_exchange( |
| 2623 | mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ) ) != |
Manuel Pégourié-Gonnard | 55c7f99 | 2015-09-16 15:35:27 +0200 | [diff] [blame] | 2624 | MBEDTLS_KEY_EXCHANGE_ECJPAKE ) |
Hanno Becker | df64596 | 2019-06-26 13:02:22 +0100 | [diff] [blame] | 2625 | { |
Manuel Pégourié-Gonnard | 55c7f99 | 2015-09-16 15:35:27 +0200 | [diff] [blame] | 2626 | return; |
Hanno Becker | df64596 | 2019-06-26 13:02:22 +0100 | [diff] [blame] | 2627 | } |
Manuel Pégourié-Gonnard | 55c7f99 | 2015-09-16 15:35:27 +0200 | [diff] [blame] | 2628 | |
| 2629 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, ecjpake kkpp extension" ) ); |
| 2630 | |
| 2631 | if( end - p < 4 ) |
| 2632 | { |
| 2633 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) ); |
| 2634 | return; |
| 2635 | } |
| 2636 | |
Arto Kinnunen | a3fa06e | 2019-09-09 12:22:51 +0300 | [diff] [blame] | 2637 | p = mbedtls_platform_put_uint16_be( p, MBEDTLS_TLS_EXT_ECJPAKE_KKPP ); |
Manuel Pégourié-Gonnard | 55c7f99 | 2015-09-16 15:35:27 +0200 | [diff] [blame] | 2638 | |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 2639 | ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx, |
| 2640 | p + 2, end - p - 2, &kkpp_len, |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 2641 | mbedtls_ssl_conf_get_frng( ssl->conf ), |
Hanno Becker | 9a12243 | 2019-07-23 13:24:02 +0100 | [diff] [blame] | 2642 | mbedtls_ssl_conf_get_prng( ssl->conf ) ); |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 2643 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 55c7f99 | 2015-09-16 15:35:27 +0200 | [diff] [blame] | 2644 | { |
| 2645 | MBEDTLS_SSL_DEBUG_RET( 1 , "mbedtls_ecjpake_write_round_one", ret ); |
| 2646 | return; |
| 2647 | } |
| 2648 | |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 2649 | p = mbedtls_platform_put_uint16_be( p, kkpp_len ); |
Manuel Pégourié-Gonnard | 55c7f99 | 2015-09-16 15:35:27 +0200 | [diff] [blame] | 2650 | *olen = kkpp_len + 4; |
| 2651 | } |
| 2652 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
| 2653 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2654 | #if defined(MBEDTLS_SSL_ALPN ) |
| 2655 | static void ssl_write_alpn_ext( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 2656 | unsigned char *buf, size_t *olen ) |
| 2657 | { |
| 2658 | if( ssl->alpn_chosen == NULL ) |
| 2659 | { |
| 2660 | *olen = 0; |
| 2661 | return; |
| 2662 | } |
| 2663 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2664 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) ); |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 2665 | |
| 2666 | /* |
| 2667 | * 0 . 1 ext identifier |
| 2668 | * 2 . 3 ext length |
| 2669 | * 4 . 5 protocol list length |
| 2670 | * 6 . 6 protocol name length |
| 2671 | * 7 . 7+n protocol name |
| 2672 | */ |
Arto Kinnunen | 3d7439e | 2019-09-10 11:30:40 +0300 | [diff] [blame] | 2673 | (void)mbedtls_platform_put_uint16_be( &buf[0], MBEDTLS_TLS_EXT_ALPN ); |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 2674 | |
| 2675 | *olen = 7 + strlen( ssl->alpn_chosen ); |
Arto Kinnunen | 3d7439e | 2019-09-10 11:30:40 +0300 | [diff] [blame] | 2676 | (void)mbedtls_platform_put_uint16_be( &buf[2], ( *olen - 4 ) ); |
| 2677 | (void)mbedtls_platform_put_uint16_be( &buf[4], ( *olen - 6 ) ); |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 2678 | |
| 2679 | buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF ); |
| 2680 | |
Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 2681 | mbedtls_platform_memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 ); |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 2682 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2683 | #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */ |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 2684 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2685 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) |
| 2686 | 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] | 2687 | { |
| 2688 | int ret; |
| 2689 | unsigned char *p = ssl->out_msg + 4; |
Manuel Pégourié-Gonnard | d7f9bc5 | 2014-07-23 11:09:27 +0200 | [diff] [blame] | 2690 | unsigned char *cookie_len_byte; |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2691 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2692 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello verify request" ) ); |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2693 | |
| 2694 | /* |
| 2695 | * struct { |
| 2696 | * ProtocolVersion server_version; |
| 2697 | * opaque cookie<0..2^8-1>; |
| 2698 | * } HelloVerifyRequest; |
| 2699 | */ |
| 2700 | |
Manuel Pégourié-Gonnard | b35fe56 | 2014-08-09 17:00:46 +0200 | [diff] [blame] | 2701 | /* The RFC is not clear on this point, but sending the actual negotiated |
| 2702 | * version looks like the most interoperable thing to do. */ |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 2703 | mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ), |
| 2704 | mbedtls_ssl_get_minor_ver( ssl ), |
| 2705 | ssl->conf->transport, p ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2706 | MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 ); |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2707 | p += 2; |
| 2708 | |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 2709 | /* If we get here, f_cookie_check is not null */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2710 | if( ssl->conf->f_cookie_write == NULL ) |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 2711 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2712 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "inconsistent cookie callbacks" ) ); |
| 2713 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 2714 | } |
| 2715 | |
Manuel Pégourié-Gonnard | d7f9bc5 | 2014-07-23 11:09:27 +0200 | [diff] [blame] | 2716 | /* Skip length byte until we know the length */ |
| 2717 | cookie_len_byte = p++; |
| 2718 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2719 | if( ( ret = ssl->conf->f_cookie_write( ssl->conf->p_cookie, |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2720 | &p, ssl->out_buf + MBEDTLS_SSL_OUT_BUFFER_LEN, |
Manuel Pégourié-Gonnard | d485d19 | 2014-07-23 14:56:15 +0200 | [diff] [blame] | 2721 | ssl->cli_id, ssl->cli_id_len ) ) != 0 ) |
Manuel Pégourié-Gonnard | d7f9bc5 | 2014-07-23 11:09:27 +0200 | [diff] [blame] | 2722 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2723 | MBEDTLS_SSL_DEBUG_RET( 1, "f_cookie_write", ret ); |
Manuel Pégourié-Gonnard | d7f9bc5 | 2014-07-23 11:09:27 +0200 | [diff] [blame] | 2724 | return( ret ); |
| 2725 | } |
| 2726 | |
| 2727 | *cookie_len_byte = (unsigned char)( p - ( cookie_len_byte + 1 ) ); |
| 2728 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2729 | 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] | 2730 | |
| 2731 | ssl->out_msglen = p - ssl->out_msg; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2732 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 2733 | ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST; |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2734 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2735 | ssl->state = MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT; |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2736 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2737 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2738 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2739 | 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] | 2740 | return( ret ); |
| 2741 | } |
| 2742 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2743 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 2744 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2745 | ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 2746 | { |
| 2747 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); |
| 2748 | return( ret ); |
| 2749 | } |
Hanno Becker | bc2498a | 2018-08-28 10:13:29 +0100 | [diff] [blame] | 2750 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2751 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2752 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello verify request" ) ); |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2753 | |
| 2754 | return( 0 ); |
| 2755 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2756 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */ |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2757 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2758 | static int ssl_write_server_hello( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2759 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2760 | #if defined(MBEDTLS_HAVE_TIME) |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 2761 | mbedtls_time_t t; |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 2762 | #endif |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 2763 | int ret; |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 2764 | int ciphersuite; |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 2765 | size_t olen, ext_len = 0, n; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2766 | unsigned char *buf, *p; |
| 2767 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2768 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2769 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2770 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 2771 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Manuel Pégourié-Gonnard | d7f9bc5 | 2014-07-23 11:09:27 +0200 | [diff] [blame] | 2772 | ssl->handshake->verify_cookie_len != 0 ) |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2773 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2774 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "client hello was not authenticated" ) ); |
| 2775 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) ); |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2776 | |
Manuel Pégourié-Gonnard | 579950c | 2014-09-29 17:47:33 +0200 | [diff] [blame] | 2777 | return( ssl_write_hello_verify_request( ssl ) ); |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2778 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2779 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */ |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2780 | |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 2781 | if( mbedtls_ssl_conf_get_frng( ssl->conf ) == NULL ) |
Paul Bakker | a9a028e | 2013-11-21 17:31:06 +0100 | [diff] [blame] | 2782 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2783 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided") ); |
| 2784 | return( MBEDTLS_ERR_SSL_NO_RNG ); |
Paul Bakker | a9a028e | 2013-11-21 17:31:06 +0100 | [diff] [blame] | 2785 | } |
| 2786 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2787 | /* |
| 2788 | * 0 . 0 handshake type |
| 2789 | * 1 . 3 handshake length |
| 2790 | * 4 . 5 protocol version |
| 2791 | * 6 . 9 UNIX time() |
| 2792 | * 10 . 37 random bytes |
| 2793 | */ |
| 2794 | buf = ssl->out_msg; |
| 2795 | p = buf + 4; |
| 2796 | |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 2797 | mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ), |
| 2798 | mbedtls_ssl_get_minor_ver( ssl ), |
| 2799 | ssl->conf->transport, p ); |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 2800 | p += 2; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2801 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2802 | 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] | 2803 | buf[4], buf[5] ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2804 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2805 | #if defined(MBEDTLS_HAVE_TIME) |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 2806 | t = mbedtls_time( NULL ); |
Arto Kinnunen | 6e3f09b | 2019-09-06 17:37:01 +0300 | [diff] [blame] | 2807 | p = mbedtls_platform_put_uint32_be( p, (uint32_t) t ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2808 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2809 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) ); |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 2810 | #else |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 2811 | if( ( ret = mbedtls_ssl_conf_get_frng( ssl->conf ) |
Hanno Becker | 9a12243 | 2019-07-23 13:24:02 +0100 | [diff] [blame] | 2812 | ( mbedtls_ssl_conf_get_prng( ssl->conf ), p, 4 ) ) != 0 ) |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 2813 | { |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 2814 | return( ret ); |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 2815 | } |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 2816 | |
| 2817 | p += 4; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2818 | #endif /* MBEDTLS_HAVE_TIME */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2819 | |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 2820 | if( ( ret = mbedtls_ssl_conf_get_frng( ssl->conf ) |
Hanno Becker | 9a12243 | 2019-07-23 13:24:02 +0100 | [diff] [blame] | 2821 | ( mbedtls_ssl_conf_get_prng( ssl->conf ), p, 28 ) ) != 0 ) |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 2822 | { |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 2823 | return( ret ); |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 2824 | } |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 2825 | |
| 2826 | p += 28; |
Jarno Lamsa | 98801af | 2019-12-17 15:57:41 +0200 | [diff] [blame] | 2827 | ssl->handshake->hello_random_set = MBEDTLS_SSL_FI_FLAG_UNSET; |
Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 2828 | mbedtls_platform_memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 ); |
Jarno Lamsa | 98801af | 2019-12-17 15:57:41 +0200 | [diff] [blame] | 2829 | if( mbedtls_platform_memcmp( ssl->handshake->randbytes + 32, buf + 6, 32 ) == 0 ) |
| 2830 | { |
| 2831 | ssl->handshake->hello_random_set = MBEDTLS_SSL_FI_FLAG_SET; |
| 2832 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2833 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2834 | MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2835 | |
Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 2836 | #if !defined(MBEDTLS_SSL_NO_SESSION_CACHE) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2837 | /* |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2838 | * Resume is 0 by default, see ssl_handshake_init(). |
| 2839 | * It may be already set to 1 by ssl_parse_session_ticket_ext(). |
| 2840 | * If not, try looking up session ID in our cache. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2841 | */ |
Jarno Lamsa | 8d09e57 | 2019-12-19 15:20:19 +0200 | [diff] [blame^] | 2842 | if( mbedtls_ssl_handshake_get_resume( ssl->handshake ) == MBEDTLS_SSL_FI_FLAG_UNSET && |
Manuel Pégourié-Gonnard | 754b9f3 | 2019-07-01 12:20:54 +0200 | [diff] [blame] | 2843 | mbedtls_ssl_get_renego_status( ssl ) == MBEDTLS_SSL_INITIAL_HANDSHAKE && |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 2844 | ssl->session_negotiate->id_len != 0 && |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2845 | ssl->conf->f_get_cache != NULL && |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 2846 | ssl->conf->f_get_cache( ssl->conf->p_cache, ssl->session_negotiate ) == 0 ) |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2847 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2848 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) ); |
Jarno Lamsa | 8d09e57 | 2019-12-19 15:20:19 +0200 | [diff] [blame^] | 2849 | ssl->handshake->resume = MBEDTLS_SSL_FI_FLAG_SET; |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2850 | } |
Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 2851 | #endif /* !MBEDTLS_SSL_NO_SESSION_CACHE */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2852 | |
Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 2853 | #if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION) |
Jarno Lamsa | 8d09e57 | 2019-12-19 15:20:19 +0200 | [diff] [blame^] | 2854 | if( mbedtls_ssl_handshake_get_resume( ssl->handshake ) == MBEDTLS_SSL_FI_FLAG_SET ) |
Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 2855 | { |
| 2856 | /* |
| 2857 | * Resuming a session |
| 2858 | */ |
| 2859 | n = ssl->session_negotiate->id_len; |
| 2860 | ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC; |
| 2861 | |
| 2862 | if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 ) |
| 2863 | { |
| 2864 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret ); |
| 2865 | return( ret ); |
| 2866 | } |
| 2867 | } |
| 2868 | else |
| 2869 | #endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2870 | { |
| 2871 | /* |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2872 | * New session, create a new session id, |
| 2873 | * unless we're about to issue a session ticket |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2874 | */ |
Jarno Lamsa | 2b20516 | 2019-11-12 15:36:21 +0200 | [diff] [blame] | 2875 | ssl->state = MBEDTLS_SSL_SERVER_CERTIFICATE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2876 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2877 | #if defined(MBEDTLS_HAVE_TIME) |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 2878 | ssl->session_negotiate->start = mbedtls_time( NULL ); |
Manuel Pégourié-Gonnard | 164d894 | 2013-09-23 22:01:39 +0200 | [diff] [blame] | 2879 | #endif |
| 2880 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2881 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | 164d894 | 2013-09-23 22:01:39 +0200 | [diff] [blame] | 2882 | if( ssl->handshake->new_session_ticket != 0 ) |
| 2883 | { |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 2884 | ssl->session_negotiate->id_len = n = 0; |
Manuel Pégourié-Gonnard | 164d894 | 2013-09-23 22:01:39 +0200 | [diff] [blame] | 2885 | memset( ssl->session_negotiate->id, 0, 32 ); |
| 2886 | } |
| 2887 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2888 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2889 | { |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 2890 | ssl->session_negotiate->id_len = n = 32; |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 2891 | if( ( ret = mbedtls_ssl_conf_get_frng( ssl->conf ) |
Hanno Becker | 9a12243 | 2019-07-23 13:24:02 +0100 | [diff] [blame] | 2892 | ( mbedtls_ssl_conf_get_prng( ssl->conf ), ssl->session_negotiate->id, n ) ) != 0 ) |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 2893 | { |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2894 | return( ret ); |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 2895 | } |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2896 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2897 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2898 | |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2899 | /* |
| 2900 | * 38 . 38 session id length |
| 2901 | * 39 . 38+n session id |
| 2902 | * 39+n . 40+n chosen ciphersuite |
| 2903 | * 41+n . 41+n chosen compression alg. |
| 2904 | * 42+n . 43+n extensions length |
| 2905 | * 44+n . 43+n+m extensions |
| 2906 | */ |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 2907 | *p++ = (unsigned char) ssl->session_negotiate->id_len; |
Teppo Järvelin | 6f4e030 | 2019-10-04 13:53:53 +0300 | [diff] [blame] | 2908 | /* Not using more secure mbedtls_platform_memcpy as id is public */ |
| 2909 | memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len ); |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 2910 | p += ssl->session_negotiate->id_len; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2911 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2912 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) ); |
| 2913 | MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n ); |
| 2914 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed", |
Manuel Pégourié-Gonnard | 3652e99 | 2019-07-01 12:09:22 +0200 | [diff] [blame] | 2915 | mbedtls_ssl_handshake_get_resume( ssl->handshake ) ? "a" : "no" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2916 | |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 2917 | ciphersuite = mbedtls_ssl_session_get_ciphersuite( ssl->session_negotiate ); |
Arto Kinnunen | a3fa06e | 2019-09-09 12:22:51 +0300 | [diff] [blame] | 2918 | p = mbedtls_platform_put_uint16_be( p, ciphersuite ); |
Hanno Becker | 8844055 | 2019-07-03 14:16:13 +0100 | [diff] [blame] | 2919 | *p++ = (unsigned char)( |
| 2920 | mbedtls_ssl_session_get_compression( ssl->session_negotiate ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2921 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2922 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s", |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 2923 | mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2924 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X", |
Hanno Becker | 8844055 | 2019-07-03 14:16:13 +0100 | [diff] [blame] | 2925 | mbedtls_ssl_session_get_compression( ssl->session_negotiate ) ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2926 | |
Janos Follath | c6dab2b | 2016-05-23 14:27:02 +0100 | [diff] [blame] | 2927 | /* Do not write the extensions if the protocol is SSLv3 */ |
| 2928 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 2929 | if( ( mbedtls_ssl_get_major_ver( ssl ) != 3 ) || ( mbedtls_ssl_get_minor_ver( ssl ) != 0 ) ) |
Janos Follath | c6dab2b | 2016-05-23 14:27:02 +0100 | [diff] [blame] | 2930 | { |
| 2931 | #endif |
| 2932 | |
Manuel Pégourié-Gonnard | f11a6d7 | 2013-07-17 11:17:14 +0200 | [diff] [blame] | 2933 | /* |
| 2934 | * First write extensions, then the total length |
| 2935 | */ |
| 2936 | ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen ); |
| 2937 | ext_len += olen; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2938 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2939 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 7bb7899 | 2013-07-17 13:50:08 +0200 | [diff] [blame] | 2940 | ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen ); |
| 2941 | ext_len += olen; |
Paul Bakker | 05decb2 | 2013-08-15 13:33:48 +0200 | [diff] [blame] | 2942 | #endif |
Manuel Pégourié-Gonnard | 7bb7899 | 2013-07-17 13:50:08 +0200 | [diff] [blame] | 2943 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2944 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 2945 | ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen ); |
| 2946 | ext_len += olen; |
Paul Bakker | 1f2bc62 | 2013-08-15 13:45:55 +0200 | [diff] [blame] | 2947 | #endif |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 2948 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2949 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 072d4ec | 2019-04-26 15:46:55 +0100 | [diff] [blame] | 2950 | ssl_write_cid_ext( ssl, p + 2 + ext_len, &olen ); |
| 2951 | ext_len += olen; |
| 2952 | #endif |
| 2953 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2954 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2955 | ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen ); |
| 2956 | ext_len += olen; |
| 2957 | #endif |
| 2958 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2959 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2960 | ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen ); |
| 2961 | ext_len += olen; |
| 2962 | #endif |
| 2963 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2964 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 2965 | ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen ); |
| 2966 | ext_len += olen; |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 2967 | #endif |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 2968 | |
Manuel Pégourié-Gonnard | f472179 | 2015-09-15 10:53:51 +0200 | [diff] [blame] | 2969 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 2970 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) || \ |
| 2971 | defined(MBEDTLS_USE_TINYCRYPT) |
Ron Eldor | 755bb6a | 2018-02-14 19:30:48 +0200 | [diff] [blame] | 2972 | if ( mbedtls_ssl_ciphersuite_uses_ec( |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 2973 | mbedtls_ssl_ciphersuite_from_id( |
| 2974 | mbedtls_ssl_session_get_ciphersuite( ssl->session_negotiate ) ) ) ) |
Ron Eldor | 755bb6a | 2018-02-14 19:30:48 +0200 | [diff] [blame] | 2975 | { |
| 2976 | ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen ); |
| 2977 | ext_len += olen; |
| 2978 | } |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 2979 | #endif |
| 2980 | |
Manuel Pégourié-Gonnard | 55c7f99 | 2015-09-16 15:35:27 +0200 | [diff] [blame] | 2981 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 2982 | ssl_write_ecjpake_kkpp_ext( ssl, p + 2 + ext_len, &olen ); |
| 2983 | ext_len += olen; |
| 2984 | #endif |
| 2985 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2986 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 2987 | ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen ); |
| 2988 | ext_len += olen; |
| 2989 | #endif |
| 2990 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2991 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2992 | |
Paul Bakker | a703663 | 2014-04-30 10:15:38 +0200 | [diff] [blame] | 2993 | if( ext_len > 0 ) |
| 2994 | { |
Arto Kinnunen | a3fa06e | 2019-09-09 12:22:51 +0300 | [diff] [blame] | 2995 | p = mbedtls_platform_put_uint16_be( p, ext_len ); |
Paul Bakker | a703663 | 2014-04-30 10:15:38 +0200 | [diff] [blame] | 2996 | p += ext_len; |
| 2997 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2998 | |
Janos Follath | c6dab2b | 2016-05-23 14:27:02 +0100 | [diff] [blame] | 2999 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 3000 | } |
| 3001 | #endif |
| 3002 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3003 | ssl->out_msglen = p - buf; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3004 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 3005 | ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3006 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3007 | ret = mbedtls_ssl_write_handshake_msg( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3008 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3009 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3010 | |
| 3011 | return( ret ); |
| 3012 | } |
| 3013 | |
Hanno Becker | ae39b9e | 2019-02-07 12:32:43 +0000 | [diff] [blame] | 3014 | #if !defined(MBEDTLS_KEY_EXCHANGE__CERT_REQ_ALLOWED__ENABLED) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3015 | static int ssl_write_certificate_request( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3016 | { |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 3017 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = |
Hanno Becker | df64596 | 2019-06-26 13:02:22 +0100 | [diff] [blame] | 3018 | mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3019 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3020 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3021 | |
Hanno Becker | ae39b9e | 2019-02-07 12:32:43 +0000 | [diff] [blame] | 3022 | if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3023 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3024 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) ); |
Jarno Lamsa | 2b20516 | 2019-11-12 15:36:21 +0200 | [diff] [blame] | 3025 | ssl->state = MBEDTLS_SSL_SERVER_HELLO_DONE; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3026 | return( 0 ); |
| 3027 | } |
| 3028 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3029 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3030 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3031 | } |
Hanno Becker | ae39b9e | 2019-02-07 12:32:43 +0000 | [diff] [blame] | 3032 | #else /* !MBEDTLS_KEY_EXCHANGE__CERT_REQ_ALLOWED__ENABLED */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3033 | static int ssl_write_certificate_request( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3034 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3035 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 3036 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = |
Hanno Becker | df64596 | 2019-06-26 13:02:22 +0100 | [diff] [blame] | 3037 | mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3038 | size_t dn_size, total_dn_size; /* excluding length bytes */ |
| 3039 | size_t ct_len, sa_len; /* including length bytes */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3040 | unsigned char *buf, *p; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3041 | 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] | 3042 | const mbedtls_x509_crt *crt; |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 3043 | int authmode; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3044 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3045 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3046 | |
Jarno Lamsa | 2b20516 | 2019-11-12 15:36:21 +0200 | [diff] [blame] | 3047 | ssl->state = MBEDTLS_SSL_SERVER_HELLO_DONE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3048 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 3049 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 3050 | if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET ) |
| 3051 | authmode = ssl->handshake->sni_authmode; |
| 3052 | else |
| 3053 | #endif |
Hanno Becker | acd4fc0 | 2019-06-12 16:40:50 +0100 | [diff] [blame] | 3054 | authmode = mbedtls_ssl_conf_get_authmode( ssl->conf ); |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 3055 | |
Hanno Becker | ae39b9e | 2019-02-07 12:32:43 +0000 | [diff] [blame] | 3056 | if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) || |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 3057 | authmode == MBEDTLS_SSL_VERIFY_NONE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3058 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3059 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3060 | return( 0 ); |
| 3061 | } |
| 3062 | |
| 3063 | /* |
| 3064 | * 0 . 0 handshake type |
| 3065 | * 1 . 3 handshake length |
| 3066 | * 4 . 4 cert type count |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3067 | * 5 .. m-1 cert types |
| 3068 | * m .. m+1 sig alg length (TLS 1.2 only) |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 3069 | * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3070 | * n .. n+1 length of all DNs |
| 3071 | * n+2 .. n+3 length of DN 1 |
| 3072 | * n+4 .. ... Distinguished Name #1 |
| 3073 | * ... .. ... length of DN 2, etc. |
| 3074 | */ |
| 3075 | buf = ssl->out_msg; |
| 3076 | p = buf + 4; |
| 3077 | |
| 3078 | /* |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3079 | * Supported certificate types |
| 3080 | * |
| 3081 | * ClientCertificateType certificate_types<1..2^8-1>; |
| 3082 | * enum { (255) } ClientCertificateType; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3083 | */ |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3084 | ct_len = 0; |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3085 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3086 | #if defined(MBEDTLS_RSA_C) |
| 3087 | p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_RSA_SIGN; |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3088 | #endif |
Jarno Lamsa | 7cb5c11 | 2019-04-23 15:54:56 +0300 | [diff] [blame] | 3089 | #if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3090 | p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_ECDSA_SIGN; |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3091 | #endif |
| 3092 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 3093 | p[0] = (unsigned char) ct_len++; |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3094 | p += ct_len; |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3095 | |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 3096 | sa_len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3097 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3098 | /* |
| 3099 | * Add signature_algorithms for verify (TLS 1.2) |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3100 | * |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3101 | * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>; |
| 3102 | * |
| 3103 | * struct { |
| 3104 | * HashAlgorithm hash; |
| 3105 | * SignatureAlgorithm signature; |
| 3106 | * } SignatureAndHashAlgorithm; |
| 3107 | * |
| 3108 | * enum { (255) } HashAlgorithm; |
| 3109 | * enum { (255) } SignatureAlgorithm; |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3110 | */ |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 3111 | if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3112 | { |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3113 | /* |
| 3114 | * Supported signature algorithms |
| 3115 | */ |
Hanno Becker | f1bc9e1 | 2019-06-19 16:23:21 +0100 | [diff] [blame] | 3116 | MBEDTLS_SSL_BEGIN_FOR_EACH_SIG_HASH_TLS( hash ) |
| 3117 | if( 0 |
Hanno Becker | 0af717b | 2019-06-24 11:36:30 +0100 | [diff] [blame] | 3118 | #if defined(MBEDTLS_SHA512_C) |
Hanno Becker | f1bc9e1 | 2019-06-19 16:23:21 +0100 | [diff] [blame] | 3119 | || hash == MBEDTLS_SSL_HASH_SHA384 |
Hanno Becker | 0af717b | 2019-06-24 11:36:30 +0100 | [diff] [blame] | 3120 | #endif |
| 3121 | #if defined(MBEDTLS_SHA256_C) |
Hanno Becker | f1bc9e1 | 2019-06-19 16:23:21 +0100 | [diff] [blame] | 3122 | || hash == MBEDTLS_SSL_HASH_SHA256 |
Hanno Becker | 0af717b | 2019-06-24 11:36:30 +0100 | [diff] [blame] | 3123 | #endif |
Hanno Becker | f1bc9e1 | 2019-06-19 16:23:21 +0100 | [diff] [blame] | 3124 | ) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3125 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3126 | #if defined(MBEDTLS_RSA_C) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3127 | p[2 + sa_len++] = hash; |
| 3128 | p[2 + sa_len++] = MBEDTLS_SSL_SIG_RSA; |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3129 | #endif |
Jarno Lamsa | 7cb5c11 | 2019-04-23 15:54:56 +0300 | [diff] [blame] | 3130 | #if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3131 | p[2 + sa_len++] = hash; |
| 3132 | p[2 + sa_len++] = MBEDTLS_SSL_SIG_ECDSA; |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3133 | #endif |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3134 | } |
Hanno Becker | f1bc9e1 | 2019-06-19 16:23:21 +0100 | [diff] [blame] | 3135 | MBEDTLS_SSL_END_FOR_EACH_SIG_HASH_TLS |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3136 | |
Arto Kinnunen | 3d7439e | 2019-09-10 11:30:40 +0300 | [diff] [blame] | 3137 | (void)mbedtls_platform_put_uint16_be( p, sa_len ); |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3138 | sa_len += 2; |
| 3139 | p += sa_len; |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3140 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3141 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3142 | |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3143 | /* |
| 3144 | * DistinguishedName certificate_authorities<0..2^16-1>; |
| 3145 | * opaque DistinguishedName<1..2^16-1>; |
| 3146 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3147 | p += 2; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3148 | |
Paul Bakker | bc3d984 | 2012-11-26 16:12:02 +0100 | [diff] [blame] | 3149 | total_dn_size = 0; |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 3150 | |
Hanno Becker | c2cfdaa | 2019-06-13 12:33:03 +0100 | [diff] [blame] | 3151 | if( mbedtls_ssl_conf_get_cert_req_ca_list( ssl->conf ) |
| 3152 | == MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3153 | { |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 3154 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 3155 | if( ssl->handshake->sni_ca_chain != NULL ) |
| 3156 | crt = ssl->handshake->sni_ca_chain; |
| 3157 | else |
| 3158 | #endif |
| 3159 | crt = ssl->conf->ca_chain; |
Manuel Pégourié-Gonnard | bc1babb | 2015-10-02 11:16:47 +0200 | [diff] [blame] | 3160 | |
Hanno Becker | 371e0e4 | 2019-02-25 18:08:59 +0000 | [diff] [blame] | 3161 | while( crt != NULL && crt->raw.p != NULL ) |
Manuel Pégourié-Gonnard | bc1babb | 2015-10-02 11:16:47 +0200 | [diff] [blame] | 3162 | { |
Hanno Becker | 5f268b3 | 2019-05-20 16:26:34 +0100 | [diff] [blame] | 3163 | mbedtls_x509_crt_frame const *frame; |
Hanno Becker | 232f8fa | 2019-02-26 16:49:57 +0000 | [diff] [blame] | 3164 | ret = mbedtls_x509_crt_frame_acquire( crt, &frame ); |
| 3165 | if( ret != 0 ) |
Hanno Becker | 2224ccf | 2019-06-28 10:52:45 +0100 | [diff] [blame] | 3166 | { |
| 3167 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_frame_acquire", ret ); |
Hanno Becker | 232f8fa | 2019-02-26 16:49:57 +0000 | [diff] [blame] | 3168 | return( ret ); |
Hanno Becker | 2224ccf | 2019-06-28 10:52:45 +0100 | [diff] [blame] | 3169 | } |
Hanno Becker | 232f8fa | 2019-02-26 16:49:57 +0000 | [diff] [blame] | 3170 | |
Hanno Becker | 1e11f21 | 2019-03-04 14:43:43 +0000 | [diff] [blame] | 3171 | dn_size = frame->subject_raw.len; |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 3172 | |
| 3173 | if( end < p || |
| 3174 | (size_t)( end - p ) < dn_size || |
| 3175 | (size_t)( end - p ) < 2 + dn_size ) |
| 3176 | { |
| 3177 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "skipping CAs: buffer too short" ) ); |
Hanno Becker | c6d1c3e | 2019-03-05 13:50:56 +0000 | [diff] [blame] | 3178 | mbedtls_x509_crt_frame_release( crt ); |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 3179 | break; |
| 3180 | } |
| 3181 | |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 3182 | p = mbedtls_platform_put_uint16_be( p, dn_size ); |
Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 3183 | mbedtls_platform_memcpy( p, frame->subject_raw.p, dn_size ); |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 3184 | p += dn_size; |
| 3185 | |
| 3186 | MBEDTLS_SSL_DEBUG_BUF( 3, "requested DN", p - dn_size, dn_size ); |
| 3187 | |
| 3188 | total_dn_size += 2 + dn_size; |
Hanno Becker | 232f8fa | 2019-02-26 16:49:57 +0000 | [diff] [blame] | 3189 | |
Hanno Becker | c6d1c3e | 2019-03-05 13:50:56 +0000 | [diff] [blame] | 3190 | mbedtls_x509_crt_frame_release( crt ); |
Hanno Becker | 232f8fa | 2019-02-26 16:49:57 +0000 | [diff] [blame] | 3191 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 3192 | crt = crt->next; |
Manuel Pégourié-Gonnard | bc1babb | 2015-10-02 11:16:47 +0200 | [diff] [blame] | 3193 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3194 | } |
| 3195 | |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3196 | ssl->out_msglen = p - buf; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3197 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 3198 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_REQUEST; |
Arto Kinnunen | 3d7439e | 2019-09-10 11:30:40 +0300 | [diff] [blame] | 3199 | (void)mbedtls_platform_put_uint16_be( &ssl->out_msg[4 + ct_len + sa_len], |
| 3200 | total_dn_size ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3201 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3202 | ret = mbedtls_ssl_write_handshake_msg( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3203 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3204 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3205 | |
| 3206 | return( ret ); |
| 3207 | } |
Hanno Becker | ae39b9e | 2019-02-07 12:32:43 +0000 | [diff] [blame] | 3208 | #endif /* MBEDTLS_KEY_EXCHANGE__CERT_REQ_ALLOWED__ENABLED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3209 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3210 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ |
| 3211 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) |
Hanno Becker | b3a2448 | 2019-09-01 09:47:23 +0100 | [diff] [blame] | 3212 | |
| 3213 | #if defined(MBEDTLS_USE_TINYCRYPT) |
| 3214 | static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl ) |
| 3215 | { |
| 3216 | mbedtls_uecc_keypair *own_key = |
| 3217 | mbedtls_pk_uecc( *mbedtls_ssl_own_key( ssl ) ); |
| 3218 | |
| 3219 | if( ! mbedtls_pk_can_do( mbedtls_ssl_own_key( ssl ), MBEDTLS_PK_ECKEY ) ) |
| 3220 | { |
| 3221 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) ); |
| 3222 | return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH ); |
| 3223 | } |
| 3224 | |
Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 3225 | mbedtls_platform_memcpy( ssl->handshake->ecdh_privkey, |
Hanno Becker | b3a2448 | 2019-09-01 09:47:23 +0100 | [diff] [blame] | 3226 | own_key->private_key, |
| 3227 | sizeof( ssl->handshake->ecdh_privkey ) ); |
| 3228 | |
| 3229 | return( 0 ); |
| 3230 | } |
| 3231 | #else /* MBEDTLS_USE_TINYCRYPT */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3232 | 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] | 3233 | { |
| 3234 | int ret; |
| 3235 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3236 | if( ! mbedtls_pk_can_do( mbedtls_ssl_own_key( ssl ), MBEDTLS_PK_ECKEY ) ) |
Manuel Pégourié-Gonnard | 5538970 | 2013-12-12 11:14:16 +0100 | [diff] [blame] | 3237 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3238 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) ); |
| 3239 | return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH ); |
Manuel Pégourié-Gonnard | 5538970 | 2013-12-12 11:14:16 +0100 | [diff] [blame] | 3240 | } |
| 3241 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3242 | if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx, |
| 3243 | mbedtls_pk_ec( *mbedtls_ssl_own_key( ssl ) ), |
| 3244 | MBEDTLS_ECDH_OURS ) ) != 0 ) |
Manuel Pégourié-Gonnard | 5538970 | 2013-12-12 11:14:16 +0100 | [diff] [blame] | 3245 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3246 | MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret ); |
Manuel Pégourié-Gonnard | 5538970 | 2013-12-12 11:14:16 +0100 | [diff] [blame] | 3247 | return( ret ); |
| 3248 | } |
| 3249 | |
| 3250 | return( 0 ); |
| 3251 | } |
Hanno Becker | b3a2448 | 2019-09-01 09:47:23 +0100 | [diff] [blame] | 3252 | #endif /* MBEDTLS_USE_TINYCRYPT */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3253 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || |
| 3254 | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */ |
Manuel Pégourié-Gonnard | 5538970 | 2013-12-12 11:14:16 +0100 | [diff] [blame] | 3255 | |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3256 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) && \ |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3257 | defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3258 | static int ssl_resume_server_key_exchange( mbedtls_ssl_context *ssl, |
Gilles Peskine | 0fd90dd | 2018-04-26 07:41:09 +0200 | [diff] [blame] | 3259 | size_t *signature_len ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3260 | { |
Gilles Peskine | 0fd90dd | 2018-04-26 07:41:09 +0200 | [diff] [blame] | 3261 | /* Append the signature to ssl->out_msg, leaving 2 bytes for the |
| 3262 | * signature length which will be added in ssl_write_server_key_exchange |
| 3263 | * after the call to ssl_prepare_server_key_exchange. |
| 3264 | * ssl_write_server_key_exchange also takes care of incrementing |
| 3265 | * ssl->out_msglen. */ |
| 3266 | unsigned char *sig_start = ssl->out_msg + ssl->out_msglen + 2; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3267 | 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] | 3268 | - sig_start ); |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 3269 | int ret = ssl->conf->f_async_resume( ssl, |
Gilles Peskine | 0fd90dd | 2018-04-26 07:41:09 +0200 | [diff] [blame] | 3270 | sig_start, signature_len, sig_max_len ); |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3271 | if( ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS ) |
| 3272 | { |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3273 | ssl->handshake->async_in_progress = 0; |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 3274 | mbedtls_ssl_set_async_operation_data( ssl, NULL ); |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3275 | } |
Gilles Peskine | d3eb061 | 2018-01-08 17:07:44 +0100 | [diff] [blame] | 3276 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl_resume_server_key_exchange", ret ); |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3277 | return( ret ); |
| 3278 | } |
| 3279 | #endif /* defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) && |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3280 | defined(MBEDTLS_SSL_ASYNC_PRIVATE) */ |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3281 | |
Gilles Peskine | d3eb061 | 2018-01-08 17:07:44 +0100 | [diff] [blame] | 3282 | /* Prepare the ServerKeyExchange message, up to and including |
Gilles Peskine | 168dae8 | 2018-04-25 23:35:42 +0200 | [diff] [blame] | 3283 | * calculating the signature if any, but excluding formatting the |
| 3284 | * signature and sending the message. */ |
Gilles Peskine | 7ab013a | 2018-01-08 17:04:16 +0100 | [diff] [blame] | 3285 | static int ssl_prepare_server_key_exchange( mbedtls_ssl_context *ssl, |
| 3286 | size_t *signature_len ) |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 3287 | { |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 3288 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = |
Hanno Becker | df64596 | 2019-06-26 13:02:22 +0100 | [diff] [blame] | 3289 | mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 3290 | |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3291 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME_PFS__ENABLED) |
Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3292 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) |
Gilles Peskine | 3ce9b90 | 2018-01-06 01:34:21 +0100 | [diff] [blame] | 3293 | unsigned char *dig_signed = NULL; |
Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3294 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED */ |
| 3295 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME_PFS__ENABLED */ |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3296 | |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3297 | (void) ciphersuite_info; /* unused in some configurations */ |
Gilles Peskine | 22e695f | 2018-04-26 00:22:50 +0200 | [diff] [blame] | 3298 | #if !defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) |
| 3299 | (void) signature_len; |
| 3300 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED */ |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3301 | |
Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 3302 | 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] | 3303 | |
Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3304 | /* |
| 3305 | * |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3306 | * Part 1: Provide key exchange parameters for chosen ciphersuite. |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3307 | * |
| 3308 | */ |
| 3309 | |
| 3310 | /* |
| 3311 | * - ECJPAKE key exchanges |
| 3312 | */ |
Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 3313 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 3314 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 3315 | == MBEDTLS_KEY_EXCHANGE_ECJPAKE ) |
Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 3316 | { |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3317 | int ret; |
Simon Butcher | 600c5e6 | 2018-06-14 08:58:59 +0100 | [diff] [blame] | 3318 | size_t len = 0; |
Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 3319 | |
Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 3320 | ret = mbedtls_ecjpake_write_round_two( |
| 3321 | &ssl->handshake->ecjpake_ctx, |
| 3322 | ssl->out_msg + ssl->out_msglen, |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3323 | MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen, &len, |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 3324 | mbedtls_ssl_conf_get_frng( ssl->conf ), |
Hanno Becker | 9a12243 | 2019-07-23 13:24:02 +0100 | [diff] [blame] | 3325 | mbedtls_ssl_conf_get_prng( ssl->conf ) ); |
Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 3326 | if( ret != 0 ) |
| 3327 | { |
| 3328 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret ); |
| 3329 | return( ret ); |
| 3330 | } |
| 3331 | |
Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 3332 | ssl->out_msglen += len; |
Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 3333 | } |
| 3334 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
| 3335 | |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3336 | /* |
| 3337 | * For (EC)DHE key exchanges with PSK, parameters are prefixed by support |
| 3338 | * identity hint (RFC 4279, Sec. 3). Until someone needs this feature, |
| 3339 | * we use empty support identity hints here. |
| 3340 | **/ |
| 3341 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) || \ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3342 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 3343 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) == |
| 3344 | MBEDTLS_KEY_EXCHANGE_DHE_PSK || |
| 3345 | mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) == |
| 3346 | MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3347 | { |
Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 3348 | ssl->out_msg[ssl->out_msglen++] = 0x00; |
| 3349 | ssl->out_msg[ssl->out_msglen++] = 0x00; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3350 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3351 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED || |
| 3352 | MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3353 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 3354 | /* |
Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3355 | * - DHE key exchanges |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3356 | */ |
| 3357 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__DHE_ENABLED) |
| 3358 | if( mbedtls_ssl_ciphersuite_uses_dhe( ciphersuite_info ) ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3359 | { |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3360 | int ret; |
Simon Butcher | 600c5e6 | 2018-06-14 08:58:59 +0100 | [diff] [blame] | 3361 | size_t len = 0; |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3362 | |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 3363 | if( ssl->conf->dhm_P.p == NULL || ssl->conf->dhm_G.p == NULL ) |
| 3364 | { |
| 3365 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no DH parameters set" ) ); |
| 3366 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3367 | } |
| 3368 | |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3369 | /* |
| 3370 | * Ephemeral DH parameters: |
| 3371 | * |
| 3372 | * struct { |
| 3373 | * opaque dh_p<1..2^16-1>; |
| 3374 | * opaque dh_g<1..2^16-1>; |
| 3375 | * opaque dh_Ys<1..2^16-1>; |
| 3376 | * } ServerDHParams; |
| 3377 | */ |
Hanno Becker | ab74056 | 2017-10-04 13:15:37 +0100 | [diff] [blame] | 3378 | if( ( ret = mbedtls_dhm_set_group( &ssl->handshake->dhm_ctx, |
| 3379 | &ssl->conf->dhm_P, |
| 3380 | &ssl->conf->dhm_G ) ) != 0 ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3381 | { |
Hanno Becker | ab74056 | 2017-10-04 13:15:37 +0100 | [diff] [blame] | 3382 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_set_group", ret ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3383 | return( ret ); |
| 3384 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3385 | |
Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 3386 | if( ( ret = mbedtls_dhm_make_params( |
| 3387 | &ssl->handshake->dhm_ctx, |
| 3388 | (int) mbedtls_mpi_size( &ssl->handshake->dhm_ctx.P ), |
| 3389 | ssl->out_msg + ssl->out_msglen, &len, |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 3390 | mbedtls_ssl_conf_get_frng( ssl->conf ), |
Hanno Becker | 9a12243 | 2019-07-23 13:24:02 +0100 | [diff] [blame] | 3391 | mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3392 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3393 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_params", ret ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3394 | return( ret ); |
| 3395 | } |
| 3396 | |
Hanno Becker | 4cb1f4d | 2017-10-10 15:59:57 +0100 | [diff] [blame] | 3397 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) |
Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 3398 | dig_signed = ssl->out_msg + ssl->out_msglen; |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3399 | #endif |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3400 | |
Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 3401 | ssl->out_msglen += len; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3402 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3403 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X ); |
| 3404 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P ); |
| 3405 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G ); |
| 3406 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3407 | } |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3408 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__DHE_ENABLED */ |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3409 | |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3410 | /* |
Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3411 | * - ECDHE key exchanges |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3412 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3413 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__ECDHE_ENABLED) |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3414 | if( mbedtls_ssl_ciphersuite_uses_ecdhe( ciphersuite_info ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3415 | { |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3416 | /* |
| 3417 | * Ephemeral ECDH parameters: |
| 3418 | * |
| 3419 | * struct { |
| 3420 | * ECParameters curve_params; |
| 3421 | * ECPoint public; |
| 3422 | * } ServerECDHParams; |
| 3423 | */ |
Gergely Budai | 987bfb5 | 2014-01-19 21:48:42 +0100 | [diff] [blame] | 3424 | |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 3425 | #if defined(MBEDTLS_USE_TINYCRYPT) |
Gergely Budai | 987bfb5 | 2014-01-19 21:48:42 +0100 | [diff] [blame] | 3426 | { |
Manuel Pégourié-Gonnard | 9d6a535 | 2019-11-25 13:06:05 +0100 | [diff] [blame] | 3427 | int ret; |
Hanno Becker | 9cf087d | 2019-07-24 11:19:03 +0100 | [diff] [blame] | 3428 | static const unsigned char ecdh_param_hdr[] = { |
Hanno Becker | 27b7e50 | 2019-08-23 14:39:50 +0100 | [diff] [blame] | 3429 | MBEDTLS_SSL_EC_TLS_NAMED_CURVE, |
Hanno Becker | b72fc6a | 2019-07-24 15:23:37 +0100 | [diff] [blame] | 3430 | 0 /* high bits of secp256r1 TLS ID */, |
| 3431 | 23 /* low bits of secp256r1 TLS ID */, |
Hanno Becker | 9cf087d | 2019-07-24 11:19:03 +0100 | [diff] [blame] | 3432 | 2 * NUM_ECC_BYTES + 1, |
| 3433 | 0x04 /* Uncompressed */ |
| 3434 | }; |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3435 | |
Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3436 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) |
Hanno Becker | 9cf087d | 2019-07-24 11:19:03 +0100 | [diff] [blame] | 3437 | dig_signed = ssl->out_msg + ssl->out_msglen; |
Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3438 | #endif |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3439 | |
Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 3440 | mbedtls_platform_memcpy( ssl->out_msg + ssl->out_msglen, |
Hanno Becker | 9cf087d | 2019-07-24 11:19:03 +0100 | [diff] [blame] | 3441 | ecdh_param_hdr, sizeof( ecdh_param_hdr ) ); |
| 3442 | ssl->out_msglen += sizeof( ecdh_param_hdr ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3443 | |
Manuel Pégourié-Gonnard | 9d6a535 | 2019-11-25 13:06:05 +0100 | [diff] [blame] | 3444 | ret = uECC_make_key( &ssl->out_msg[ ssl->out_msglen ], |
| 3445 | ssl->handshake->ecdh_privkey ); |
| 3446 | if( ret == UECC_FAULT_DETECTED ) |
| 3447 | return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED ); |
| 3448 | if( ret != UECC_SUCCESS ) |
| 3449 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Hanno Becker | 8295ff0 | 2019-07-24 12:45:52 +0100 | [diff] [blame] | 3450 | |
Hanno Becker | 9cf087d | 2019-07-24 11:19:03 +0100 | [diff] [blame] | 3451 | ssl->out_msglen += 2*NUM_ECC_BYTES; |
Hanno Becker | d849c7c | 2019-07-23 15:59:58 +0100 | [diff] [blame] | 3452 | } |
Hanno Becker | 40ee0d4 | 2019-09-01 09:40:53 +0100 | [diff] [blame] | 3453 | #else /* MBEDTLS_USE_TINYCRYPT */ |
Hanno Becker | 9cf087d | 2019-07-24 11:19:03 +0100 | [diff] [blame] | 3454 | { |
| 3455 | const mbedtls_ecp_curve_info *curve = |
| 3456 | mbedtls_ecp_curve_info_from_tls_id( ssl->handshake->curve_tls_id ); |
| 3457 | int ret; |
| 3458 | size_t len = 0; |
| 3459 | |
| 3460 | if( curve == NULL ) |
| 3461 | { |
| 3462 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) ); |
| 3463 | return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN ); |
| 3464 | } |
| 3465 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", curve->name ) ); |
| 3466 | |
| 3467 | if( ( ret = mbedtls_ecdh_setup( &ssl->handshake->ecdh_ctx, |
| 3468 | curve->grp_id ) ) != 0 ) |
| 3469 | { |
| 3470 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecp_group_load", ret ); |
| 3471 | return( ret ); |
| 3472 | } |
| 3473 | |
| 3474 | if( ( ret = mbedtls_ecdh_make_params( |
| 3475 | &ssl->handshake->ecdh_ctx, &len, |
| 3476 | ssl->out_msg + ssl->out_msglen, |
| 3477 | MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen, |
| 3478 | mbedtls_ssl_conf_get_frng( ssl->conf ), |
| 3479 | mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 ) |
| 3480 | { |
| 3481 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_params", ret ); |
| 3482 | return( ret ); |
| 3483 | } |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 3484 | |
Hanno Becker | d849c7c | 2019-07-23 15:59:58 +0100 | [diff] [blame] | 3485 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) |
Hanno Becker | 9cf087d | 2019-07-24 11:19:03 +0100 | [diff] [blame] | 3486 | dig_signed = ssl->out_msg + ssl->out_msglen; |
Hanno Becker | d849c7c | 2019-07-23 15:59:58 +0100 | [diff] [blame] | 3487 | #endif |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 3488 | |
Hanno Becker | 9cf087d | 2019-07-24 11:19:03 +0100 | [diff] [blame] | 3489 | ssl->out_msglen += len; |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 3490 | |
Hanno Becker | 9cf087d | 2019-07-24 11:19:03 +0100 | [diff] [blame] | 3491 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 3492 | MBEDTLS_DEBUG_ECDH_Q ); |
Paul Bakker | 2292d1f | 2013-09-15 17:06:49 +0200 | [diff] [blame] | 3493 | } |
Hanno Becker | 40ee0d4 | 2019-09-01 09:40:53 +0100 | [diff] [blame] | 3494 | #endif /* MBEDTLS_USE_TINYCRYPT */ |
Manuel Pégourié-Gonnard | abae74c | 2013-08-20 13:53:44 +0200 | [diff] [blame] | 3495 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3496 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__ECDHE_ENABLED */ |
Manuel Pégourié-Gonnard | abae74c | 2013-08-20 13:53:44 +0200 | [diff] [blame] | 3497 | |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3498 | /* |
Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3499 | * |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3500 | * Part 2: For key exchanges involving the server signing the |
Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3501 | * exchange parameters, compute and add the signature here. |
| 3502 | * |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3503 | */ |
| 3504 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) |
| 3505 | if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) ) |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 3506 | { |
Gilles Peskine | 1004c19 | 2018-01-08 16:59:14 +0100 | [diff] [blame] | 3507 | 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] | 3508 | size_t hashlen = 0; |
Gilles Peskine | e1efdf9 | 2018-01-05 21:18:37 +0100 | [diff] [blame] | 3509 | unsigned char hash[MBEDTLS_MD_MAX_SIZE]; |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3510 | int ret; |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 3511 | |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3512 | /* |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3513 | * 2.1: Choose hash algorithm: |
Hanno Becker | 4cb1f4d | 2017-10-10 15:59:57 +0100 | [diff] [blame] | 3514 | * A: For TLS 1.2, obey signature-hash-algorithm extension |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3515 | * to choose appropriate hash. |
| 3516 | * B: For SSL3, TLS1.0, TLS1.1 and ECDHE_ECDSA, use SHA1 |
| 3517 | * (RFC 4492, Sec. 5.4) |
| 3518 | * 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] | 3519 | */ |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 3520 | |
| 3521 | mbedtls_md_type_t md_alg; |
| 3522 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3523 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3524 | mbedtls_pk_type_t sig_alg = |
| 3525 | mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ); |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 3526 | if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3527 | { |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3528 | /* A: For TLS 1.2, obey signature-hash-algorithm extension |
| 3529 | * (RFC 5246, Sec. 7.4.1.4.1). */ |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 3530 | if( sig_alg == MBEDTLS_PK_NONE || |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3531 | ( md_alg = mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs, |
| 3532 | sig_alg ) ) == MBEDTLS_MD_NONE ) |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3533 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3534 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Hanno Becker | 4cb1f4d | 2017-10-10 15:59:57 +0100 | [diff] [blame] | 3535 | /* (... because we choose a cipher suite |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3536 | * only if there is a matching hash.) */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3537 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3538 | } |
| 3539 | } |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 3540 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3541 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3542 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 3543 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 3544 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 3545 | == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ) |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3546 | { |
Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3547 | /* B: Default hash SHA1 */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3548 | md_alg = MBEDTLS_MD_SHA1; |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3549 | } |
| 3550 | else |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3551 | #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \ |
| 3552 | MBEDTLS_SSL_PROTO_TLS1_1 */ |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3553 | { |
Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3554 | /* C: MD5 + SHA1 */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3555 | md_alg = MBEDTLS_MD_NONE; |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3556 | } |
| 3557 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 3558 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "pick hash algorithm %d for signing", md_alg ) ); |
| 3559 | |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3560 | /* |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3561 | * 2.2: Compute the hash to be signed |
Manuel Pégourié-Gonnard | abae74c | 2013-08-20 13:53:44 +0200 | [diff] [blame] | 3562 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3563 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 3564 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 3565 | if( md_alg == MBEDTLS_MD_NONE ) |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 3566 | { |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3567 | hashlen = 36; |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 3568 | ret = mbedtls_ssl_get_key_exchange_md_ssl_tls( ssl, hash, |
| 3569 | dig_signed, |
| 3570 | dig_signed_len ); |
| 3571 | if( ret != 0 ) |
| 3572 | return( ret ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3573 | } |
| 3574 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3575 | #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \ |
| 3576 | MBEDTLS_SSL_PROTO_TLS1_1 */ |
| 3577 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 3578 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3579 | if( md_alg != MBEDTLS_MD_NONE ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3580 | { |
Gilles Peskine | ca1d742 | 2018-04-24 11:53:22 +0200 | [diff] [blame] | 3581 | 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] | 3582 | dig_signed, |
| 3583 | dig_signed_len, |
| 3584 | md_alg ); |
| 3585 | if( ret != 0 ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3586 | return( ret ); |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 3587 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 3588 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3589 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 3590 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 3591 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3592 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3593 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 3594 | } |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 3595 | |
Gilles Peskine | ebd652f | 2018-01-05 21:18:59 +0100 | [diff] [blame] | 3596 | MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3597 | |
Manuel Pégourié-Gonnard | abae74c | 2013-08-20 13:53:44 +0200 | [diff] [blame] | 3598 | /* |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3599 | * 2.3: Compute and add the signature |
Manuel Pégourié-Gonnard | abae74c | 2013-08-20 13:53:44 +0200 | [diff] [blame] | 3600 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3601 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 3602 | if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 3603 | { |
Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3604 | /* |
| 3605 | * For TLS 1.2, we need to specify signature and hash algorithm |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 3606 | * explicitly through a prefix to the signature. |
| 3607 | * |
| 3608 | * struct { |
| 3609 | * HashAlgorithm hash; |
| 3610 | * SignatureAlgorithm signature; |
| 3611 | * } SignatureAndHashAlgorithm; |
| 3612 | * |
| 3613 | * struct { |
| 3614 | * SignatureAndHashAlgorithm algorithm; |
| 3615 | * opaque signature<0..2^16-1>; |
| 3616 | * } DigitallySigned; |
| 3617 | * |
| 3618 | */ |
| 3619 | |
Gilles Peskine | 1004c19 | 2018-01-08 16:59:14 +0100 | [diff] [blame] | 3620 | ssl->out_msg[ssl->out_msglen++] = |
| 3621 | mbedtls_ssl_hash_from_md_alg( md_alg ); |
| 3622 | ssl->out_msg[ssl->out_msglen++] = |
| 3623 | mbedtls_ssl_sig_from_pk_alg( sig_alg ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3624 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3625 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3626 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3627 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 4bf9a28 | 2018-01-05 21:20:50 +0100 | [diff] [blame] | 3628 | if( ssl->conf->f_async_sign_start != NULL ) |
| 3629 | { |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 3630 | ret = ssl->conf->f_async_sign_start( ssl, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3631 | mbedtls_ssl_own_cert( ssl ), |
| 3632 | md_alg, hash, hashlen ); |
Gilles Peskine | 4bf9a28 | 2018-01-05 21:20:50 +0100 | [diff] [blame] | 3633 | switch( ret ) |
| 3634 | { |
| 3635 | case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH: |
| 3636 | /* act as if f_async_sign was null */ |
| 3637 | break; |
| 3638 | case 0: |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3639 | ssl->handshake->async_in_progress = 1; |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3640 | return( ssl_resume_server_key_exchange( ssl, signature_len ) ); |
Gilles Peskine | 4bf9a28 | 2018-01-05 21:20:50 +0100 | [diff] [blame] | 3641 | case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS: |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3642 | ssl->handshake->async_in_progress = 1; |
Gilles Peskine | 4bf9a28 | 2018-01-05 21:20:50 +0100 | [diff] [blame] | 3643 | return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS ); |
| 3644 | default: |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3645 | MBEDTLS_SSL_DEBUG_RET( 1, "f_async_sign_start", ret ); |
Gilles Peskine | 4bf9a28 | 2018-01-05 21:20:50 +0100 | [diff] [blame] | 3646 | return( ret ); |
| 3647 | } |
| 3648 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3649 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 4bf9a28 | 2018-01-05 21:20:50 +0100 | [diff] [blame] | 3650 | |
| 3651 | if( mbedtls_ssl_own_key( ssl ) == NULL ) |
| 3652 | { |
| 3653 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key" ) ); |
| 3654 | return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ); |
| 3655 | } |
| 3656 | |
Gilles Peskine | 0fd90dd | 2018-04-26 07:41:09 +0200 | [diff] [blame] | 3657 | /* Append the signature to ssl->out_msg, leaving 2 bytes for the |
| 3658 | * signature length which will be added in ssl_write_server_key_exchange |
| 3659 | * after the call to ssl_prepare_server_key_exchange. |
| 3660 | * ssl_write_server_key_exchange also takes care of incrementing |
| 3661 | * ssl->out_msglen. */ |
Gilles Peskine | 1004c19 | 2018-01-08 16:59:14 +0100 | [diff] [blame] | 3662 | if( ( ret = mbedtls_pk_sign( mbedtls_ssl_own_key( ssl ), |
| 3663 | md_alg, hash, hashlen, |
| 3664 | ssl->out_msg + ssl->out_msglen + 2, |
Gilles Peskine | 7ab013a | 2018-01-08 17:04:16 +0100 | [diff] [blame] | 3665 | signature_len, |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 3666 | mbedtls_ssl_conf_get_frng( ssl->conf ), |
Hanno Becker | 9a12243 | 2019-07-23 13:24:02 +0100 | [diff] [blame] | 3667 | mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3668 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3669 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 3670 | return( ret ); |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 3671 | } |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3672 | } |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3673 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3674 | |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3675 | return( 0 ); |
| 3676 | } |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3677 | |
Gilles Peskine | d3eb061 | 2018-01-08 17:07:44 +0100 | [diff] [blame] | 3678 | /* Prepare the ServerKeyExchange message and send it. For ciphersuites |
Gilles Peskine | 168dae8 | 2018-04-25 23:35:42 +0200 | [diff] [blame] | 3679 | * that do not include a ServerKeyExchange message, do nothing. Either |
| 3680 | * way, if successful, move on to the next step in the SSL state |
| 3681 | * machine. */ |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3682 | static int ssl_write_server_key_exchange( mbedtls_ssl_context *ssl ) |
| 3683 | { |
| 3684 | int ret; |
Gilles Peskine | 7ab013a | 2018-01-08 17:04:16 +0100 | [diff] [blame] | 3685 | size_t signature_len = 0; |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3686 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME_NON_PFS__ENABLED) |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 3687 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = |
Hanno Becker | df64596 | 2019-06-26 13:02:22 +0100 | [diff] [blame] | 3688 | mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); |
Gilles Peskine | f112725 | 2018-04-24 13:05:39 +0200 | [diff] [blame] | 3689 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME_NON_PFS__ENABLED */ |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3690 | |
Gilles Peskine | d3eb061 | 2018-01-08 17:07:44 +0100 | [diff] [blame] | 3691 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) ); |
| 3692 | |
| 3693 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME_NON_PFS__ENABLED) |
| 3694 | /* Extract static ECDH parameters and abort if ServerKeyExchange |
| 3695 | * is not needed. */ |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3696 | if( mbedtls_ssl_ciphersuite_no_pfs( ciphersuite_info ) ) |
| 3697 | { |
| 3698 | /* For suites involving ECDH, extract DH parameters |
| 3699 | * from certificate at this point. */ |
| 3700 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__ECDH_ENABLED) |
| 3701 | if( mbedtls_ssl_ciphersuite_uses_ecdh( ciphersuite_info ) ) |
| 3702 | { |
| 3703 | ssl_get_ecdh_params_from_cert( ssl ); |
| 3704 | } |
| 3705 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__ECDH_ENABLED */ |
| 3706 | |
| 3707 | /* Key exchanges not involving ephemeral keys don't use |
| 3708 | * ServerKeyExchange, so end here. */ |
| 3709 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) ); |
Jarno Lamsa | 2b20516 | 2019-11-12 15:36:21 +0200 | [diff] [blame] | 3710 | ssl->state = MBEDTLS_SSL_CERTIFICATE_REQUEST; |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3711 | return( 0 ); |
| 3712 | } |
Gilles Peskine | f112725 | 2018-04-24 13:05:39 +0200 | [diff] [blame] | 3713 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME_NON_PFS__ENABLED */ |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3714 | |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3715 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) && \ |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3716 | defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | d3eb061 | 2018-01-08 17:07:44 +0100 | [diff] [blame] | 3717 | /* If we have already prepared the message and there is an ongoing |
Gilles Peskine | 168dae8 | 2018-04-25 23:35:42 +0200 | [diff] [blame] | 3718 | * signature operation, resume signing. */ |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3719 | if( ssl->handshake->async_in_progress != 0 ) |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3720 | { |
| 3721 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "resuming signature operation" ) ); |
| 3722 | ret = ssl_resume_server_key_exchange( ssl, &signature_len ); |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3723 | } |
| 3724 | else |
| 3725 | #endif /* defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) && |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3726 | defined(MBEDTLS_SSL_ASYNC_PRIVATE) */ |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3727 | { |
| 3728 | /* ServerKeyExchange is needed. Prepare the message. */ |
| 3729 | ret = ssl_prepare_server_key_exchange( ssl, &signature_len ); |
Gilles Peskine | d3eb061 | 2018-01-08 17:07:44 +0100 | [diff] [blame] | 3730 | } |
| 3731 | |
| 3732 | if( ret != 0 ) |
| 3733 | { |
Gilles Peskine | ad28bf0 | 2018-04-26 00:19:16 +0200 | [diff] [blame] | 3734 | /* If we're starting to write a new message, set ssl->out_msglen |
| 3735 | * to 0. But if we're resuming after an asynchronous message, |
| 3736 | * out_msglen is the amount of data written so far and mst be |
| 3737 | * preserved. */ |
Gilles Peskine | d3eb061 | 2018-01-08 17:07:44 +0100 | [diff] [blame] | 3738 | if( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS ) |
| 3739 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server key exchange (pending)" ) ); |
| 3740 | else |
| 3741 | ssl->out_msglen = 0; |
| 3742 | return( ret ); |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3743 | } |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3744 | |
Gilles Peskine | 7ab013a | 2018-01-08 17:04:16 +0100 | [diff] [blame] | 3745 | /* If there is a signature, write its length. |
Gilles Peskine | 168dae8 | 2018-04-25 23:35:42 +0200 | [diff] [blame] | 3746 | * ssl_prepare_server_key_exchange already wrote the signature |
| 3747 | * itself at its proper place in the output buffer. */ |
Gilles Peskine | 7ab013a | 2018-01-08 17:04:16 +0100 | [diff] [blame] | 3748 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) |
| 3749 | if( signature_len != 0 ) |
| 3750 | { |
Arto Kinnunen | 3d7439e | 2019-09-10 11:30:40 +0300 | [diff] [blame] | 3751 | (void)mbedtls_platform_put_uint16_be( &ssl->out_msg[ssl->out_msglen], |
| 3752 | signature_len ); |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 3753 | ssl->out_msglen += 2; |
Gilles Peskine | 7ab013a | 2018-01-08 17:04:16 +0100 | [diff] [blame] | 3754 | |
| 3755 | MBEDTLS_SSL_DEBUG_BUF( 3, "my signature", |
| 3756 | ssl->out_msg + ssl->out_msglen, |
| 3757 | signature_len ); |
| 3758 | |
| 3759 | /* Skip over the already-written signature */ |
| 3760 | ssl->out_msglen += signature_len; |
| 3761 | } |
| 3762 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED */ |
| 3763 | |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3764 | /* Add header and send. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3765 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 3766 | ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3767 | |
Jarno Lamsa | 2b20516 | 2019-11-12 15:36:21 +0200 | [diff] [blame] | 3768 | ssl->state = MBEDTLS_SSL_CERTIFICATE_REQUEST; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3769 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3770 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3771 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3772 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3773 | return( ret ); |
| 3774 | } |
| 3775 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3776 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3777 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3778 | } |
| 3779 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3780 | static int ssl_write_server_hello_done( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3781 | { |
| 3782 | int ret; |
| 3783 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3784 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3785 | |
| 3786 | ssl->out_msglen = 4; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3787 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 3788 | ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO_DONE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3789 | |
Jarno Lamsa | 2b20516 | 2019-11-12 15:36:21 +0200 | [diff] [blame] | 3790 | ssl->state = MBEDTLS_SSL_CLIENT_CERTIFICATE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3791 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3792 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 3793 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3794 | mbedtls_ssl_send_flight_completed( ssl ); |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3795 | #endif |
| 3796 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3797 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3798 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3799 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3800 | return( ret ); |
| 3801 | } |
| 3802 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3803 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 3804 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3805 | ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 3806 | { |
| 3807 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); |
| 3808 | return( ret ); |
| 3809 | } |
Hanno Becker | bc2498a | 2018-08-28 10:13:29 +0100 | [diff] [blame] | 3810 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3811 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3812 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3813 | |
| 3814 | return( 0 ); |
| 3815 | } |
| 3816 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3817 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \ |
| 3818 | defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 3819 | 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] | 3820 | const unsigned char *end ) |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3821 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3822 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3823 | size_t n; |
| 3824 | |
| 3825 | /* |
| 3826 | * Receive G^Y mod P, premaster = (G^Y)^X mod P |
| 3827 | */ |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3828 | if( *p + 2 > end ) |
| 3829 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3830 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); |
| 3831 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3832 | } |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3833 | |
Arto Kinnunen | a3fa06e | 2019-09-09 12:22:51 +0300 | [diff] [blame] | 3834 | n = mbedtls_platform_get_uint16_be ( *p ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3835 | *p += 2; |
| 3836 | |
Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 3837 | if( *p + n > 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 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); |
| 3840 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3841 | } |
| 3842 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3843 | 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] | 3844 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3845 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_read_public", ret ); |
| 3846 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP ); |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3847 | } |
| 3848 | |
Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 3849 | *p += n; |
| 3850 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3851 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY ); |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3852 | |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3853 | return( ret ); |
| 3854 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3855 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED || |
| 3856 | MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3857 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3858 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \ |
| 3859 | defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3860 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3861 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3862 | static int ssl_resume_decrypt_pms( mbedtls_ssl_context *ssl, |
| 3863 | unsigned char *peer_pms, |
| 3864 | size_t *peer_pmslen, |
| 3865 | size_t peer_pmssize ) |
| 3866 | { |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 3867 | int ret = ssl->conf->f_async_resume( ssl, |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3868 | peer_pms, peer_pmslen, peer_pmssize ); |
| 3869 | if( ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS ) |
| 3870 | { |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3871 | ssl->handshake->async_in_progress = 0; |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 3872 | mbedtls_ssl_set_async_operation_data( ssl, NULL ); |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3873 | } |
| 3874 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl_decrypt_encrypted_pms", ret ); |
| 3875 | return( ret ); |
| 3876 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3877 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3878 | |
Gilles Peskine | bcd98a5 | 2018-01-11 21:30:40 +0100 | [diff] [blame] | 3879 | static int ssl_decrypt_encrypted_pms( mbedtls_ssl_context *ssl, |
| 3880 | const unsigned char *p, |
| 3881 | const unsigned char *end, |
| 3882 | unsigned char *peer_pms, |
| 3883 | size_t *peer_pmslen, |
| 3884 | size_t peer_pmssize ) |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3885 | { |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 3886 | int ret; |
Hanno Becker | 81bb4d0 | 2019-02-16 11:03:48 +0000 | [diff] [blame] | 3887 | size_t len = (size_t)( end - p ); /* Cast is safe because p <= end. */ |
Gilles Peskine | 422ccab | 2018-01-11 18:29:01 +0100 | [diff] [blame] | 3888 | mbedtls_pk_context *private_key = mbedtls_ssl_own_key( ssl ); |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3889 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3890 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3891 | /* 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] | 3892 | * decryption operation, resume signing. */ |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3893 | if( ssl->handshake->async_in_progress != 0 ) |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3894 | { |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3895 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "resuming decryption operation" ) ); |
| 3896 | return( ssl_resume_decrypt_pms( ssl, |
| 3897 | peer_pms, peer_pmslen, peer_pmssize ) ); |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3898 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3899 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3900 | |
| 3901 | /* |
Gilles Peskine | 422ccab | 2018-01-11 18:29:01 +0100 | [diff] [blame] | 3902 | * Prepare to decrypt the premaster using own private RSA key |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3903 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3904 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 3905 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | 81bb4d0 | 2019-02-16 11:03:48 +0000 | [diff] [blame] | 3906 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 3907 | if( mbedtls_ssl_get_minor_ver( ssl ) != MBEDTLS_SSL_MINOR_VERSION_0 ) |
Hanno Becker | 81bb4d0 | 2019-02-16 11:03:48 +0000 | [diff] [blame] | 3908 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3909 | { |
Hanno Becker | 81bb4d0 | 2019-02-16 11:03:48 +0000 | [diff] [blame] | 3910 | if( len < 2 ) |
| 3911 | { |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 3912 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); |
| 3913 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
| 3914 | } |
Hanno Becker | 81bb4d0 | 2019-02-16 11:03:48 +0000 | [diff] [blame] | 3915 | len -= 2; |
| 3916 | |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 3917 | if( *p++ != ( ( len >> 8 ) & 0xFF ) || |
| 3918 | *p++ != ( ( len ) & 0xFF ) ) |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3919 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3920 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); |
| 3921 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3922 | } |
| 3923 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 3924 | #endif |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3925 | |
Gilles Peskine | 422ccab | 2018-01-11 18:29:01 +0100 | [diff] [blame] | 3926 | /* |
| 3927 | * Decrypt the premaster secret |
| 3928 | */ |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3929 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3930 | if( ssl->conf->f_async_decrypt_start != NULL ) |
| 3931 | { |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 3932 | ret = ssl->conf->f_async_decrypt_start( ssl, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3933 | mbedtls_ssl_own_cert( ssl ), |
| 3934 | p, len ); |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3935 | switch( ret ) |
| 3936 | { |
| 3937 | case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH: |
| 3938 | /* act as if f_async_decrypt_start was null */ |
| 3939 | break; |
| 3940 | case 0: |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3941 | ssl->handshake->async_in_progress = 1; |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3942 | return( ssl_resume_decrypt_pms( ssl, |
| 3943 | peer_pms, |
| 3944 | peer_pmslen, |
| 3945 | peer_pmssize ) ); |
| 3946 | case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS: |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3947 | ssl->handshake->async_in_progress = 1; |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3948 | return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS ); |
| 3949 | default: |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3950 | MBEDTLS_SSL_DEBUG_RET( 1, "f_async_decrypt_start", ret ); |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3951 | return( ret ); |
| 3952 | } |
| 3953 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3954 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3955 | |
Gilles Peskine | 422ccab | 2018-01-11 18:29:01 +0100 | [diff] [blame] | 3956 | if( ! mbedtls_pk_can_do( private_key, MBEDTLS_PK_RSA ) ) |
| 3957 | { |
Gilles Peskine | 422ccab | 2018-01-11 18:29:01 +0100 | [diff] [blame] | 3958 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) ); |
| 3959 | return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ); |
| 3960 | } |
| 3961 | |
| 3962 | ret = mbedtls_pk_decrypt( private_key, p, len, |
Gilles Peskine | bcd98a5 | 2018-01-11 21:30:40 +0100 | [diff] [blame] | 3963 | peer_pms, peer_pmslen, peer_pmssize, |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 3964 | mbedtls_ssl_conf_get_frng( ssl->conf ), |
Hanno Becker | 9a12243 | 2019-07-23 13:24:02 +0100 | [diff] [blame] | 3965 | mbedtls_ssl_conf_get_prng( ssl->conf ) ); |
Gilles Peskine | bcd98a5 | 2018-01-11 21:30:40 +0100 | [diff] [blame] | 3966 | return( ret ); |
| 3967 | } |
| 3968 | |
| 3969 | static int ssl_parse_encrypted_pms( mbedtls_ssl_context *ssl, |
| 3970 | const unsigned char *p, |
| 3971 | const unsigned char *end, |
| 3972 | size_t pms_offset ) |
| 3973 | { |
| 3974 | int ret; |
| 3975 | unsigned char *pms = ssl->handshake->premaster + pms_offset; |
| 3976 | unsigned char ver[2]; |
| 3977 | unsigned char fake_pms[48], peer_pms[48]; |
| 3978 | unsigned char mask; |
| 3979 | size_t i, peer_pmslen; |
| 3980 | unsigned int diff; |
Jarno Lamsa | 67f0a1e | 2019-12-18 16:28:51 +0200 | [diff] [blame] | 3981 | volatile unsigned int pmscounter = 0; |
Gilles Peskine | bcd98a5 | 2018-01-11 21:30:40 +0100 | [diff] [blame] | 3982 | |
Gilles Peskine | 0a8352b | 2018-06-13 18:16:41 +0200 | [diff] [blame] | 3983 | /* In case of a failure in decryption, the decryption may write less than |
| 3984 | * 2 bytes of output, but we always read the first two bytes. It doesn't |
| 3985 | * matter in the end because diff will be nonzero in that case due to |
| 3986 | * peer_pmslen being less than 48, and we only care whether diff is 0. |
| 3987 | * But do initialize peer_pms for robustness anyway. This also makes |
| 3988 | * memory analyzers happy (don't access uninitialized memory, even |
| 3989 | * if it's an unsigned char). */ |
| 3990 | peer_pms[0] = peer_pms[1] = ~0; |
| 3991 | |
Gilles Peskine | bcd98a5 | 2018-01-11 21:30:40 +0100 | [diff] [blame] | 3992 | ret = ssl_decrypt_encrypted_pms( ssl, p, end, |
| 3993 | peer_pms, |
| 3994 | &peer_pmslen, |
| 3995 | sizeof( peer_pms ) ); |
| 3996 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3997 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3998 | if ( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS ) |
| 3999 | return( ret ); |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 4000 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 4001 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4002 | mbedtls_ssl_write_version( ssl->handshake->max_major_ver, |
Gilles Peskine | 2e33337 | 2018-04-24 13:22:10 +0200 | [diff] [blame] | 4003 | ssl->handshake->max_minor_ver, |
| 4004 | ssl->conf->transport, ver ); |
| 4005 | |
| 4006 | /* Avoid data-dependent branches while checking for invalid |
| 4007 | * padding, to protect against timing-based Bleichenbacher-type |
| 4008 | * attacks. */ |
| 4009 | diff = (unsigned int) ret; |
| 4010 | diff |= peer_pmslen ^ 48; |
| 4011 | diff |= peer_pms[0] ^ ver[0]; |
| 4012 | diff |= peer_pms[1] ^ ver[1]; |
| 4013 | |
| 4014 | /* mask = diff ? 0xff : 0x00 using bit operations to avoid branches */ |
| 4015 | /* MSVC has a warning about unary minus on unsigned, but this is |
| 4016 | * well-defined and precisely what we want to do here */ |
| 4017 | #if defined(_MSC_VER) |
| 4018 | #pragma warning( push ) |
| 4019 | #pragma warning( disable : 4146 ) |
| 4020 | #endif |
| 4021 | mask = - ( ( diff | - diff ) >> ( sizeof( unsigned int ) * 8 - 1 ) ); |
| 4022 | #if defined(_MSC_VER) |
| 4023 | #pragma warning( pop ) |
| 4024 | #endif |
Manuel Pégourié-Gonnard | b9c93d0 | 2015-06-23 13:53:15 +0200 | [diff] [blame] | 4025 | |
Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 4026 | /* |
| 4027 | * Protection against Bleichenbacher's attack: invalid PKCS#1 v1.5 padding |
| 4028 | * must not cause the connection to end immediately; instead, send a |
| 4029 | * bad_record_mac later in the handshake. |
Gilles Peskine | bcd98a5 | 2018-01-11 21:30:40 +0100 | [diff] [blame] | 4030 | * To protect against timing-based variants of the attack, we must |
| 4031 | * not have any branch that depends on whether the decryption was |
| 4032 | * successful. In particular, always generate the fake premaster secret, |
| 4033 | * 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] | 4034 | */ |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 4035 | ret = mbedtls_ssl_conf_get_frng( ssl->conf ) |
Hanno Becker | 9a12243 | 2019-07-23 13:24:02 +0100 | [diff] [blame] | 4036 | ( mbedtls_ssl_conf_get_prng( ssl->conf ), fake_pms, sizeof( fake_pms ) ); |
Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 4037 | if( ret != 0 ) |
Gilles Peskine | bcd98a5 | 2018-01-11 21:30:40 +0100 | [diff] [blame] | 4038 | { |
Gilles Peskine | e141638 | 2018-04-26 10:23:21 +0200 | [diff] [blame] | 4039 | /* It's ok to abort on an RNG failure, since this does not reveal |
| 4040 | * anything about the RSA decryption. */ |
Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 4041 | return( ret ); |
Gilles Peskine | bcd98a5 | 2018-01-11 21:30:40 +0100 | [diff] [blame] | 4042 | } |
Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 4043 | |
Manuel Pégourié-Gonnard | 331ba57 | 2015-04-20 12:33:57 +0100 | [diff] [blame] | 4044 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Manuel Pégourié-Gonnard | ce60fbe | 2015-04-15 16:45:52 +0200 | [diff] [blame] | 4045 | if( diff != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4046 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); |
Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 4047 | #endif |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 4048 | |
Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 4049 | if( sizeof( ssl->handshake->premaster ) < pms_offset || |
| 4050 | sizeof( ssl->handshake->premaster ) - pms_offset < 48 ) |
| 4051 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4052 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 4053 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 4054 | } |
Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 4055 | ssl->handshake->pmslen = 48; |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 4056 | |
Gilles Peskine | 422ccab | 2018-01-11 18:29:01 +0100 | [diff] [blame] | 4057 | /* Set pms to either the true or the fake PMS, without |
| 4058 | * data-dependent branches. */ |
Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 4059 | for( i = 0; i < ssl->handshake->pmslen; i++ ) |
Jarno Lamsa | 67f0a1e | 2019-12-18 16:28:51 +0200 | [diff] [blame] | 4060 | { |
Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 4061 | pms[i] = ( mask & fake_pms[i] ) | ( (~mask) & peer_pms[i] ); |
Jarno Lamsa | 67f0a1e | 2019-12-18 16:28:51 +0200 | [diff] [blame] | 4062 | pmscounter++; |
| 4063 | } |
| 4064 | |
| 4065 | if( pmscounter == ssl->handshake->pmslen ) |
| 4066 | { |
| 4067 | mbedtls_platform_enforce_volatile_reads(); |
| 4068 | if( pmscounter == ssl->handshake->pmslen ) |
| 4069 | { |
| 4070 | ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET; |
| 4071 | } |
| 4072 | } |
Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 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 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4079 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
| 4080 | 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] | 4081 | const unsigned char *end ) |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4082 | { |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 4083 | int ret = 0; |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4084 | size_t n; |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4085 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4086 | if( ssl->conf->f_psk == NULL && |
| 4087 | ( ssl->conf->psk == NULL || ssl->conf->psk_identity == NULL || |
| 4088 | ssl->conf->psk_identity_len == 0 || ssl->conf->psk_len == 0 ) ) |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4089 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4090 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) ); |
| 4091 | return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ); |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4092 | } |
| 4093 | |
| 4094 | /* |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4095 | * Receive client pre-shared key identity name |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4096 | */ |
Hanno Becker | 83c9f49 | 2017-06-26 13:52:14 +0100 | [diff] [blame] | 4097 | if( end - *p < 2 ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4098 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4099 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); |
| 4100 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4101 | } |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4102 | |
Arto Kinnunen | a3fa06e | 2019-09-09 12:22:51 +0300 | [diff] [blame] | 4103 | n = mbedtls_platform_get_uint16_be( *p ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4104 | *p += 2; |
| 4105 | |
Hanno Becker | 83c9f49 | 2017-06-26 13:52:14 +0100 | [diff] [blame] | 4106 | if( n < 1 || n > 65535 || n > (size_t) ( end - *p ) ) |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4107 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4108 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); |
| 4109 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4110 | } |
| 4111 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4112 | if( ssl->conf->f_psk != NULL ) |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 4113 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4114 | 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] | 4115 | ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY; |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 4116 | } |
Manuel Pégourié-Gonnard | d27680b | 2014-07-08 14:15:55 +0200 | [diff] [blame] | 4117 | else |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 4118 | { |
Manuel Pégourié-Gonnard | 31ff1d2 | 2013-10-28 13:46:11 +0100 | [diff] [blame] | 4119 | /* Identity is not a big secret since clients send it in the clear, |
| 4120 | * but treat it carefully anyway, just in case */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4121 | if( n != ssl->conf->psk_identity_len || |
Teppo Järvelin | 707ceb8 | 2019-10-04 07:49:39 +0300 | [diff] [blame] | 4122 | mbedtls_platform_memcmp( ssl->conf->psk_identity, *p, n ) != 0 ) |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 4123 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4124 | ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY; |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 4125 | } |
| 4126 | } |
| 4127 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4128 | if( ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY ) |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4129 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4130 | MBEDTLS_SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 4131 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 4132 | MBEDTLS_SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4133 | return( MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY ); |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4134 | } |
| 4135 | |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4136 | *p += n; |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4137 | |
Manuel Pégourié-Gonnard | d27680b | 2014-07-08 14:15:55 +0200 | [diff] [blame] | 4138 | return( 0 ); |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4139 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4140 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4141 | |
Hanno Becker | 7ec345d | 2018-05-21 17:16:42 +0100 | [diff] [blame] | 4142 | /* |
| 4143 | * |
| 4144 | * STATE HANDLING: Client Key Exchange |
| 4145 | * |
| 4146 | */ |
| 4147 | |
| 4148 | /* |
| 4149 | * Overview |
| 4150 | */ |
| 4151 | |
| 4152 | /* Main entry point; orchestrates the other functions. */ |
Hanno Becker | 7d552fa | 2019-08-12 15:04:59 +0100 | [diff] [blame] | 4153 | static int ssl_process_in_client_key_exchange( mbedtls_ssl_context *ssl ); |
Hanno Becker | 7ec345d | 2018-05-21 17:16:42 +0100 | [diff] [blame] | 4154 | |
Hanno Becker | 7d552fa | 2019-08-12 15:04:59 +0100 | [diff] [blame] | 4155 | static int ssl_in_client_key_exchange_parse( mbedtls_ssl_context *ssl, |
| 4156 | unsigned char *buf, |
| 4157 | size_t buflen ); |
Hanno Becker | 7ec345d | 2018-05-21 17:16:42 +0100 | [diff] [blame] | 4158 | /* Update the handshake state */ |
Hanno Becker | 7d552fa | 2019-08-12 15:04:59 +0100 | [diff] [blame] | 4159 | static int ssl_in_client_key_exchange_postprocess( mbedtls_ssl_context *ssl ); |
Hanno Becker | 7ec345d | 2018-05-21 17:16:42 +0100 | [diff] [blame] | 4160 | |
| 4161 | /* |
| 4162 | * Implementation |
| 4163 | */ |
| 4164 | |
Hanno Becker | 7d552fa | 2019-08-12 15:04:59 +0100 | [diff] [blame] | 4165 | static int ssl_process_in_client_key_exchange( mbedtls_ssl_context *ssl ) |
Hanno Becker | 7ec345d | 2018-05-21 17:16:42 +0100 | [diff] [blame] | 4166 | { |
| 4167 | int ret; |
| 4168 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> process client key exchange" ) ); |
| 4169 | |
| 4170 | /* The ClientKeyExchange message is never skipped. */ |
| 4171 | |
| 4172 | /* Reading step */ |
Hanno Becker | 1e23af8 | 2018-05-21 17:26:17 +0100 | [diff] [blame] | 4173 | if( ( ret = mbedtls_ssl_read_record( ssl, |
| 4174 | 1 /* update checksum */ ) ) != 0 ) |
Hanno Becker | 7ec345d | 2018-05-21 17:16:42 +0100 | [diff] [blame] | 4175 | { |
| 4176 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 4177 | return( ret ); |
| 4178 | } |
| 4179 | |
| 4180 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE || |
| 4181 | ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE ) |
| 4182 | { |
| 4183 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); |
| 4184 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 4185 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 4186 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 4187 | goto cleanup; |
| 4188 | } |
| 4189 | |
Hanno Becker | 7d552fa | 2019-08-12 15:04:59 +0100 | [diff] [blame] | 4190 | MBEDTLS_SSL_CHK( ssl_in_client_key_exchange_parse( ssl, ssl->in_msg, |
| 4191 | ssl->in_hslen ) ); |
Hanno Becker | 7ec345d | 2018-05-21 17:16:42 +0100 | [diff] [blame] | 4192 | |
| 4193 | /* Update state */ |
Hanno Becker | 7d552fa | 2019-08-12 15:04:59 +0100 | [diff] [blame] | 4194 | MBEDTLS_SSL_CHK( ssl_in_client_key_exchange_postprocess( ssl ) ); |
Hanno Becker | 7ec345d | 2018-05-21 17:16:42 +0100 | [diff] [blame] | 4195 | |
| 4196 | cleanup: |
| 4197 | |
Hanno Becker | 9db697e | 2019-07-23 12:19:48 +0100 | [diff] [blame] | 4198 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
| 4199 | if ( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS ) |
| 4200 | ssl->keep_current_message = 1; |
| 4201 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
| 4202 | |
Hanno Becker | 7ec345d | 2018-05-21 17:16:42 +0100 | [diff] [blame] | 4203 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= process client key exchange" ) ); |
| 4204 | return( ret ); |
| 4205 | } |
| 4206 | |
Hanno Becker | 4e46709 | 2019-08-12 15:12:35 +0100 | [diff] [blame] | 4207 | /* Warning: Despite accepting a length argument, this function is currently |
| 4208 | * still lacking some bounds checks and assumes that `buf` has length |
| 4209 | * `MBEDTLS_SSL_IN_CONTENT_LEN`. Eventually, it should be rewritten to work |
| 4210 | * with any buffer + length pair, returning MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL |
| 4211 | * on insufficient parsing space. */ |
Hanno Becker | 7d552fa | 2019-08-12 15:04:59 +0100 | [diff] [blame] | 4212 | static int ssl_in_client_key_exchange_parse( mbedtls_ssl_context *ssl, |
Hanno Becker | 7ec345d | 2018-05-21 17:16:42 +0100 | [diff] [blame] | 4213 | unsigned char *buf, |
| 4214 | size_t buflen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4215 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 4216 | int ret; |
Hanno Becker | df64596 | 2019-06-26 13:02:22 +0100 | [diff] [blame] | 4217 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = |
| 4218 | mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); |
Manuel Pégourié-Gonnard | 2114d72 | 2014-09-10 13:59:41 +0000 | [diff] [blame] | 4219 | unsigned char *p, *end; |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 4220 | |
Hanno Becker | e7c4eed | 2018-05-21 17:30:11 +0100 | [diff] [blame] | 4221 | p = buf + mbedtls_ssl_hs_hdr_len( ssl ); |
| 4222 | end = buf + buflen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4223 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4224 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 4225 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 4226 | == MBEDTLS_KEY_EXCHANGE_DHE_RSA ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4227 | { |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4228 | if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4229 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4230 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret ); |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 4231 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4232 | } |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4233 | |
Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 4234 | if( p != end ) |
| 4235 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4236 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) ); |
| 4237 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 4238 | } |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 4239 | } |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4240 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4241 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */ |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 4242 | #if defined(MBEDTLS_USE_TINYCRYPT) |
| 4243 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
Hanno Becker | b42e238 | 2019-09-01 07:49:04 +0100 | [diff] [blame] | 4244 | == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA || |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 4245 | mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
Hanno Becker | b42e238 | 2019-09-01 07:49:04 +0100 | [diff] [blame] | 4246 | == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA || |
| 4247 | mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 4248 | == MBEDTLS_KEY_EXCHANGE_ECDH_RSA || |
| 4249 | mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 4250 | == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA ) |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 4251 | { |
Hanno Becker | 7a19633 | 2019-07-24 11:12:41 +0100 | [diff] [blame] | 4252 | ((void) ret); |
Hanno Becker | a3c2c17 | 2019-07-23 16:51:57 +0100 | [diff] [blame] | 4253 | if( mbedtls_ssl_ecdh_read_peerkey( ssl, &p, end ) != 0 ) |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 4254 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 4255 | } |
| 4256 | else |
| 4257 | #endif |
Hanno Becker | 29d1655 | 2019-07-24 11:11:45 +0100 | [diff] [blame] | 4258 | #if defined(MBEDTLS_ECDH_C) && \ |
| 4259 | ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ |
| 4260 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \ |
| 4261 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ |
| 4262 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) ) |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 4263 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
Hanno Becker | e7c4eed | 2018-05-21 17:30:11 +0100 | [diff] [blame] | 4264 | == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA || |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 4265 | mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
Hanno Becker | e7c4eed | 2018-05-21 17:30:11 +0100 | [diff] [blame] | 4266 | == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA || |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 4267 | mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
Hanno Becker | e7c4eed | 2018-05-21 17:30:11 +0100 | [diff] [blame] | 4268 | == MBEDTLS_KEY_EXCHANGE_ECDH_RSA || |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 4269 | mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 4270 | == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA ) |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 4271 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4272 | if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx, |
Manuel Pégourié-Gonnard | 2114d72 | 2014-09-10 13:59:41 +0000 | [diff] [blame] | 4273 | p, end - p) ) != 0 ) |
Manuel Pégourié-Gonnard | b59d699 | 2013-10-14 12:00:45 +0200 | [diff] [blame] | 4274 | { |
Hanno Becker | 7ba0a88 | 2019-07-24 17:07:41 +0100 | [diff] [blame] | 4275 | ((void) ret); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4276 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret ); |
| 4277 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP ); |
Manuel Pégourié-Gonnard | b59d699 | 2013-10-14 12:00:45 +0200 | [diff] [blame] | 4278 | } |
| 4279 | |
Janos Follath | 3fbdada | 2018-08-15 10:26:53 +0100 | [diff] [blame] | 4280 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 4281 | MBEDTLS_DEBUG_ECDH_QP ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4282 | } |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4283 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4284 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || |
| 4285 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED || |
| 4286 | MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED || |
| 4287 | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */ |
| 4288 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
Hanno Becker | e7c4eed | 2018-05-21 17:30:11 +0100 | [diff] [blame] | 4289 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 4290 | == MBEDTLS_KEY_EXCHANGE_PSK ) |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4291 | { |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4292 | if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 ) |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4293 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4294 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret ); |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4295 | return( ret ); |
| 4296 | } |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4297 | |
Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 4298 | if( p != end ) |
| 4299 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4300 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) ); |
| 4301 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 4302 | } |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4303 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4304 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4305 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
| 4306 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
Hanno Becker | e7c4eed | 2018-05-21 17:30:11 +0100 | [diff] [blame] | 4307 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 4308 | == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 4309 | { |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 4310 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4311 | if ( ssl->handshake->async_in_progress != 0 ) |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 4312 | { |
| 4313 | /* There is an asynchronous operation in progress to |
| 4314 | * decrypt the encrypted premaster secret, so skip |
| 4315 | * directly to resuming this operation. */ |
| 4316 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "PSK identity already parsed" ) ); |
| 4317 | /* Update p to skip the PSK identity. ssl_parse_encrypted_pms |
| 4318 | * won't actually use it, but maintain p anyway for robustness. */ |
| 4319 | p += ssl->conf->psk_identity_len + 2; |
| 4320 | } |
| 4321 | else |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 4322 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 4323 | if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 ) |
| 4324 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4325 | 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] | 4326 | return( ret ); |
| 4327 | } |
| 4328 | |
| 4329 | if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 ) |
| 4330 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4331 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret ); |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 4332 | return( ret ); |
| 4333 | } |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 4334 | } |
| 4335 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4336 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
| 4337 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
Hanno Becker | e7c4eed | 2018-05-21 17:30:11 +0100 | [diff] [blame] | 4338 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 4339 | == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4340 | { |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4341 | if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 ) |
| 4342 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4343 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4344 | return( ret ); |
| 4345 | } |
| 4346 | if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 ) |
| 4347 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4348 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4349 | return( ret ); |
| 4350 | } |
| 4351 | |
Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 4352 | if( p != end ) |
| 4353 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4354 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) ); |
| 4355 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 4356 | } |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4357 | } |
| 4358 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4359 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
| 4360 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
Hanno Becker | e7c4eed | 2018-05-21 17:30:11 +0100 | [diff] [blame] | 4361 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 4362 | == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 4363 | { |
Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 4364 | if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 ) |
| 4365 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4366 | 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] | 4367 | return( ret ); |
| 4368 | } |
Manuel Pégourié-Gonnard | b59d699 | 2013-10-14 12:00:45 +0200 | [diff] [blame] | 4369 | |
Hanno Becker | 358b300 | 2019-09-02 09:48:02 +0100 | [diff] [blame] | 4370 | #if defined(MBEDTLS_USE_TINYCRYPT) |
| 4371 | if( mbedtls_ssl_ecdh_read_peerkey( ssl, &p, end ) != 0 ) |
| 4372 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 4373 | #else /* MBEDTLS_USE_TINYCRYPT */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4374 | if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx, |
Manuel Pégourié-Gonnard | b59d699 | 2013-10-14 12:00:45 +0200 | [diff] [blame] | 4375 | p, end - p ) ) != 0 ) |
Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 4376 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4377 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret ); |
| 4378 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP ); |
Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 4379 | } |
| 4380 | |
Hanno Becker | e7c4eed | 2018-05-21 17:30:11 +0100 | [diff] [blame] | 4381 | MBEDTLS_SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp ); |
Hanno Becker | 358b300 | 2019-09-02 09:48:02 +0100 | [diff] [blame] | 4382 | #endif /* MBEDTLS_USE_TINYCRYPT */ |
Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 4383 | } |
| 4384 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4385 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
| 4386 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) |
Hanno Becker | e7c4eed | 2018-05-21 17:30:11 +0100 | [diff] [blame] | 4387 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 4388 | == MBEDTLS_KEY_EXCHANGE_RSA ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 4389 | { |
Manuel Pégourié-Gonnard | 2114d72 | 2014-09-10 13:59:41 +0000 | [diff] [blame] | 4390 | if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 0 ) ) != 0 ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 4391 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4392 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret ); |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 4393 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4394 | } |
| 4395 | } |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4396 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4397 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */ |
Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 4398 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Hanno Becker | e7c4eed | 2018-05-21 17:30:11 +0100 | [diff] [blame] | 4399 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 4400 | == MBEDTLS_KEY_EXCHANGE_ECJPAKE ) |
Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 4401 | { |
| 4402 | ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx, |
| 4403 | p, end - p ); |
| 4404 | if( ret != 0 ) |
| 4405 | { |
| 4406 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret ); |
| 4407 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
| 4408 | } |
Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 4409 | } |
| 4410 | else |
| 4411 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4412 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4413 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 4414 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4415 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4416 | |
Hanno Becker | e7c4eed | 2018-05-21 17:30:11 +0100 | [diff] [blame] | 4417 | return( 0 ); |
Hanno Becker | 7ec345d | 2018-05-21 17:16:42 +0100 | [diff] [blame] | 4418 | } |
| 4419 | |
| 4420 | /* Update the handshake state */ |
Hanno Becker | 7d552fa | 2019-08-12 15:04:59 +0100 | [diff] [blame] | 4421 | static int ssl_in_client_key_exchange_postprocess( mbedtls_ssl_context *ssl ) |
Hanno Becker | 7ec345d | 2018-05-21 17:16:42 +0100 | [diff] [blame] | 4422 | { |
Hanno Becker | dc8bfb9 | 2018-05-21 17:24:27 +0100 | [diff] [blame] | 4423 | int ret; |
Hanno Becker | 1e23af8 | 2018-05-21 17:26:17 +0100 | [diff] [blame] | 4424 | |
Hanno Becker | 09d2364 | 2019-07-22 17:18:18 +0100 | [diff] [blame] | 4425 | if( ( ret = mbedtls_ssl_build_pms( ssl ) ) != 0 ) |
Hanno Becker | 1e23af8 | 2018-05-21 17:26:17 +0100 | [diff] [blame] | 4426 | { |
Hanno Becker | 09d2364 | 2019-07-22 17:18:18 +0100 | [diff] [blame] | 4427 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_build_pms", ret ); |
| 4428 | return( ret ); |
Hanno Becker | 1e23af8 | 2018-05-21 17:26:17 +0100 | [diff] [blame] | 4429 | } |
Hanno Becker | dc8bfb9 | 2018-05-21 17:24:27 +0100 | [diff] [blame] | 4430 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4431 | if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 ) |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 4432 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4433 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret ); |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 4434 | return( ret ); |
| 4435 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4436 | |
Hanno Becker | 7ec345d | 2018-05-21 17:16:42 +0100 | [diff] [blame] | 4437 | ssl->state = MBEDTLS_SSL_CERTIFICATE_VERIFY; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4438 | return( 0 ); |
| 4439 | } |
| 4440 | |
Hanno Becker | ae39b9e | 2019-02-07 12:32:43 +0000 | [diff] [blame] | 4441 | #if !defined(MBEDTLS_KEY_EXCHANGE__CERT_REQ_ALLOWED__ENABLED) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4442 | static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4443 | { |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 4444 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = |
Hanno Becker | df64596 | 2019-06-26 13:02:22 +0100 | [diff] [blame] | 4445 | mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4446 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4447 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4448 | |
Hanno Becker | ae39b9e | 2019-02-07 12:32:43 +0000 | [diff] [blame] | 4449 | if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ) |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 4450 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4451 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) ); |
Jarno Lamsa | e1621d4 | 2019-12-19 08:58:56 +0200 | [diff] [blame] | 4452 | ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET; |
Jarno Lamsa | 2b20516 | 2019-11-12 15:36:21 +0200 | [diff] [blame] | 4453 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 4454 | return( 0 ); |
| 4455 | } |
| 4456 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4457 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 4458 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4459 | } |
Hanno Becker | ae39b9e | 2019-02-07 12:32:43 +0000 | [diff] [blame] | 4460 | #else /* !MBEDTLS_KEY_EXCHANGE__CERT_REQ_ALLOWED__ENABLED */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4461 | static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4462 | { |
Jarno Lamsa | 83a56a6 | 2019-12-11 15:00:27 +0200 | [diff] [blame] | 4463 | volatile int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 4528f3f | 2014-09-10 14:17:23 +0000 | [diff] [blame] | 4464 | size_t i, sig_len; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4465 | unsigned char hash[48]; |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 4466 | unsigned char *hash_start = hash; |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4467 | size_t hashlen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4468 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4469 | mbedtls_pk_type_t pk_alg; |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 4470 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4471 | mbedtls_md_type_t md_alg; |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 4472 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = |
Hanno Becker | df64596 | 2019-06-26 13:02:22 +0100 | [diff] [blame] | 4473 | mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 4474 | mbedtls_pk_context *peer_pk = NULL; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4475 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4476 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4477 | |
Hanno Becker | cd90126 | 2019-02-07 13:17:25 +0000 | [diff] [blame] | 4478 | if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4479 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4480 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) ); |
Jarno Lamsa | e1621d4 | 2019-12-19 08:58:56 +0200 | [diff] [blame] | 4481 | ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET; |
Jarno Lamsa | 2b20516 | 2019-11-12 15:36:21 +0200 | [diff] [blame] | 4482 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4483 | return( 0 ); |
| 4484 | } |
| 4485 | |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 4486 | /* Skip if we haven't received a certificate from the client. |
| 4487 | * If MBEDTLS_SSL_KEEP_PEER_CERTIFICATE is set, this can be |
| 4488 | * inferred from the setting of mbedtls_ssl_session::peer_cert. |
Manuel Pégourié-Gonnard | 2829bbf | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 4489 | * If MBEDTLS_SSL_KEEP_PEER_CERTIFICATE is not set, it is tracked in a |
| 4490 | * specific variable. */ |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 4491 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | 2829bbf | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 4492 | if( ssl->handshake->got_peer_pubkey ) |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 4493 | peer_pk = &ssl->handshake->peer_pubkey; |
| 4494 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 4495 | if( ssl->session_negotiate->peer_cert != NULL ) |
Hanno Becker | 73cd8d8 | 2019-02-28 14:04:16 +0000 | [diff] [blame] | 4496 | { |
| 4497 | ret = mbedtls_x509_crt_pk_acquire( ssl->session_negotiate->peer_cert, |
| 4498 | &peer_pk ); |
| 4499 | if( ret != 0 ) |
| 4500 | { |
Hanno Becker | 2224ccf | 2019-06-28 10:52:45 +0100 | [diff] [blame] | 4501 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_pk_acquire", ret ); |
| 4502 | return( ret ); |
Hanno Becker | 73cd8d8 | 2019-02-28 14:04:16 +0000 | [diff] [blame] | 4503 | } |
| 4504 | } |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 4505 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 4506 | |
| 4507 | if( peer_pk == NULL ) |
Hanno Becker | cd90126 | 2019-02-07 13:17:25 +0000 | [diff] [blame] | 4508 | { |
| 4509 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) ); |
Jarno Lamsa | e1621d4 | 2019-12-19 08:58:56 +0200 | [diff] [blame] | 4510 | ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET; |
Jarno Lamsa | 2b20516 | 2019-11-12 15:36:21 +0200 | [diff] [blame] | 4511 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
Hanno Becker | cd90126 | 2019-02-07 13:17:25 +0000 | [diff] [blame] | 4512 | return( 0 ); |
| 4513 | } |
Hanno Becker | cd90126 | 2019-02-07 13:17:25 +0000 | [diff] [blame] | 4514 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4515 | /* Read the message without adding it to the checksum */ |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 4516 | ret = mbedtls_ssl_read_record( ssl, 0 /* no checksum update */ ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4517 | if( 0 != ret ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4518 | { |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 4519 | MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record" ), ret ); |
Hanno Becker | bc6b598 | 2019-07-02 15:36:44 +0100 | [diff] [blame] | 4520 | goto exit; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4521 | } |
| 4522 | |
Jarno Lamsa | 2b20516 | 2019-11-12 15:36:21 +0200 | [diff] [blame] | 4523 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4524 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4525 | /* Process the message contents */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4526 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE || |
| 4527 | ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE_VERIFY ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4528 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4529 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) ); |
Hanno Becker | bc6b598 | 2019-07-02 15:36:44 +0100 | [diff] [blame] | 4530 | ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY; |
| 4531 | goto exit; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4532 | } |
| 4533 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4534 | i = mbedtls_ssl_hs_hdr_len( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4535 | |
Manuel Pégourié-Gonnard | 4528f3f | 2014-09-10 14:17:23 +0000 | [diff] [blame] | 4536 | /* |
| 4537 | * struct { |
| 4538 | * SignatureAndHashAlgorithm algorithm; -- TLS 1.2 only |
| 4539 | * opaque signature<0..2^16-1>; |
| 4540 | * } DigitallySigned; |
| 4541 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4542 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 4543 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 4544 | if( mbedtls_ssl_get_minor_ver( ssl ) != MBEDTLS_SSL_MINOR_VERSION_3 ) |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 4545 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4546 | md_alg = MBEDTLS_MD_NONE; |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4547 | hashlen = 36; |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 4548 | |
| 4549 | /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */ |
Hanno Becker | 0833c10 | 2019-02-06 18:31:04 +0000 | [diff] [blame] | 4550 | if( mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_ECDSA ) ) |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 4551 | { |
| 4552 | hash_start += 16; |
| 4553 | hashlen -= 16; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4554 | md_alg = MBEDTLS_MD_SHA1; |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 4555 | } |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 4556 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 4557 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4558 | #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || |
| 4559 | MBEDTLS_SSL_PROTO_TLS1_1 */ |
| 4560 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 4561 | if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 4562 | { |
Manuel Pégourié-Gonnard | 5ee9654 | 2014-09-10 14:27:21 +0000 | [diff] [blame] | 4563 | if( i + 2 > ssl->in_hslen ) |
| 4564 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4565 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) ); |
Hanno Becker | 73cd8d8 | 2019-02-28 14:04:16 +0000 | [diff] [blame] | 4566 | ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY; |
| 4567 | goto exit; |
Manuel Pégourié-Gonnard | 5ee9654 | 2014-09-10 14:27:21 +0000 | [diff] [blame] | 4568 | } |
| 4569 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4570 | /* |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4571 | * Hash |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4572 | */ |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4573 | md_alg = mbedtls_ssl_md_alg_from_hash( ssl->in_msg[i] ); |
| 4574 | |
Hanno Becker | 2f41b24 | 2019-08-15 17:29:43 +0100 | [diff] [blame] | 4575 | if( |
| 4576 | #if defined(MBEDTLS_SHA512_C) |
| 4577 | md_alg != MBEDTLS_MD_SHA384 && |
| 4578 | #endif |
| 4579 | #if defined(MBEDTLS_SHA256_C) |
| 4580 | md_alg != MBEDTLS_MD_SHA256 && |
| 4581 | #endif |
| 4582 | 1 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4583 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4584 | 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] | 4585 | " for verify message" ) ); |
Hanno Becker | 73cd8d8 | 2019-02-28 14:04:16 +0000 | [diff] [blame] | 4586 | ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY; |
| 4587 | goto exit; |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 4588 | } |
| 4589 | |
Manuel Pégourié-Gonnard | bfe32ef | 2013-08-22 14:55:30 +0200 | [diff] [blame] | 4590 | /* Info from md_alg will be used instead */ |
| 4591 | hashlen = 0; |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4592 | |
Manuel Pégourié-Gonnard | 4528f3f | 2014-09-10 14:17:23 +0000 | [diff] [blame] | 4593 | i++; |
| 4594 | |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4595 | /* |
| 4596 | * Signature |
| 4597 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4598 | if( ( pk_alg = mbedtls_ssl_pk_alg_from_sig( ssl->in_msg[i] ) ) |
| 4599 | == MBEDTLS_PK_NONE ) |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4600 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4601 | 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] | 4602 | " for verify message" ) ); |
Hanno Becker | 73cd8d8 | 2019-02-28 14:04:16 +0000 | [diff] [blame] | 4603 | ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY; |
| 4604 | goto exit; |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4605 | } |
| 4606 | |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4607 | /* |
| 4608 | * Check the certificate's key type matches the signature alg |
| 4609 | */ |
Hanno Becker | 0833c10 | 2019-02-06 18:31:04 +0000 | [diff] [blame] | 4610 | if( !mbedtls_pk_can_do( peer_pk, pk_alg ) ) |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4611 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4612 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) ); |
Hanno Becker | 73cd8d8 | 2019-02-28 14:04:16 +0000 | [diff] [blame] | 4613 | ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY; |
| 4614 | goto exit; |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4615 | } |
Manuel Pégourié-Gonnard | 4528f3f | 2014-09-10 14:17:23 +0000 | [diff] [blame] | 4616 | |
| 4617 | i++; |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 4618 | } |
| 4619 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4620 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 4621 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4622 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 4623 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 4624 | } |
Manuel Pégourié-Gonnard | ff56da3 | 2013-07-11 10:46:21 +0200 | [diff] [blame] | 4625 | |
Manuel Pégourié-Gonnard | 5ee9654 | 2014-09-10 14:27:21 +0000 | [diff] [blame] | 4626 | if( i + 2 > ssl->in_hslen ) |
| 4627 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4628 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) ); |
Hanno Becker | 73cd8d8 | 2019-02-28 14:04:16 +0000 | [diff] [blame] | 4629 | ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY; |
| 4630 | goto exit; |
Manuel Pégourié-Gonnard | 5ee9654 | 2014-09-10 14:27:21 +0000 | [diff] [blame] | 4631 | } |
| 4632 | |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 4633 | sig_len = mbedtls_platform_get_uint16_be( &ssl->in_msg[i] ); |
Manuel Pégourié-Gonnard | 4528f3f | 2014-09-10 14:17:23 +0000 | [diff] [blame] | 4634 | i += 2; |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 4635 | |
Manuel Pégourié-Gonnard | 4528f3f | 2014-09-10 14:17:23 +0000 | [diff] [blame] | 4636 | if( i + sig_len != ssl->in_hslen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4637 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4638 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) ); |
Hanno Becker | 73cd8d8 | 2019-02-28 14:04:16 +0000 | [diff] [blame] | 4639 | ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY; |
| 4640 | goto exit; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4641 | } |
| 4642 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4643 | /* Calculate hash and verify signature */ |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 4644 | { |
| 4645 | size_t dummy_hlen; |
Hanno Becker | 2f41b24 | 2019-08-15 17:29:43 +0100 | [diff] [blame] | 4646 | mbedtls_ssl_calc_verify( |
| 4647 | mbedtls_ssl_get_minor_ver( ssl ), |
| 4648 | md_alg, ssl, hash, &dummy_hlen ); |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 4649 | } |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4650 | |
Jarno Lamsa | 83a56a6 | 2019-12-11 15:00:27 +0200 | [diff] [blame] | 4651 | ret = mbedtls_pk_verify( peer_pk, |
| 4652 | md_alg, hash_start, hashlen, |
| 4653 | ssl->in_msg + i, sig_len ); |
| 4654 | |
| 4655 | if( ret == 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4656 | { |
Jarno Lamsa | 83a56a6 | 2019-12-11 15:00:27 +0200 | [diff] [blame] | 4657 | mbedtls_platform_enforce_volatile_reads(); |
| 4658 | |
| 4659 | if( ret == 0 ) |
| 4660 | { |
| 4661 | mbedtls_ssl_update_handshake_status( ssl ); |
| 4662 | |
| 4663 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) ); |
| 4664 | goto exit; |
Jarno Lamsa | 46afd5d | 2019-12-17 08:50:53 +0200 | [diff] [blame] | 4665 | } |
| 4666 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4667 | } |
| 4668 | |
Jarno Lamsa | 83a56a6 | 2019-12-11 15:00:27 +0200 | [diff] [blame] | 4669 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4670 | |
Hanno Becker | 73cd8d8 | 2019-02-28 14:04:16 +0000 | [diff] [blame] | 4671 | exit: |
| 4672 | |
| 4673 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Hanno Becker | c6d1c3e | 2019-03-05 13:50:56 +0000 | [diff] [blame] | 4674 | mbedtls_x509_crt_pk_release( ssl->session_negotiate->peer_cert ); |
Hanno Becker | 73cd8d8 | 2019-02-28 14:04:16 +0000 | [diff] [blame] | 4675 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 4676 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 4677 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4678 | } |
Hanno Becker | ae39b9e | 2019-02-07 12:32:43 +0000 | [diff] [blame] | 4679 | #endif /* MBEDTLS_KEY_EXCHANGE__CERT_REQ_ALLOWED__ENABLED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4680 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4681 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 4682 | 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] | 4683 | { |
| 4684 | int ret; |
Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 4685 | size_t tlen; |
Manuel Pégourié-Gonnard | b0394be | 2015-05-19 11:40:30 +0200 | [diff] [blame] | 4686 | uint32_t lifetime; |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4687 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4688 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) ); |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4689 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4690 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 4691 | ssl->out_msg[0] = MBEDTLS_SSL_HS_NEW_SESSION_TICKET; |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4692 | |
| 4693 | /* |
| 4694 | * struct { |
| 4695 | * uint32 ticket_lifetime_hint; |
| 4696 | * opaque ticket<0..2^16-1>; |
| 4697 | * } NewSessionTicket; |
| 4698 | * |
| 4699 | * 4 . 7 ticket_lifetime_hint (0 = unspecified) |
| 4700 | * 8 . 9 ticket_len (n) |
| 4701 | * 10 . 9+n ticket content |
| 4702 | */ |
Manuel Pégourié-Gonnard | 164d894 | 2013-09-23 22:01:39 +0200 | [diff] [blame] | 4703 | |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 4704 | 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] | 4705 | ssl->session_negotiate, |
| 4706 | ssl->out_msg + 10, |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4707 | ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN, |
Manuel Pégourié-Gonnard | b0394be | 2015-05-19 11:40:30 +0200 | [diff] [blame] | 4708 | &tlen, &lifetime ) ) != 0 ) |
Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 4709 | { |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 4710 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_ticket_write", ret ); |
Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 4711 | tlen = 0; |
| 4712 | } |
Arto Kinnunen | 3d7439e | 2019-09-10 11:30:40 +0300 | [diff] [blame] | 4713 | (void)mbedtls_platform_put_uint32_be( &ssl->out_msg[4], lifetime ); |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4714 | |
Arto Kinnunen | 3d7439e | 2019-09-10 11:30:40 +0300 | [diff] [blame] | 4715 | (void)mbedtls_platform_put_uint16_be( &ssl->out_msg[8], tlen ); |
Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 4716 | ssl->out_msglen = 10 + tlen; |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4717 | |
Manuel Pégourié-Gonnard | 145dfcb | 2014-02-26 14:23:33 +0100 | [diff] [blame] | 4718 | /* |
| 4719 | * Morally equivalent to updating ssl->state, but NewSessionTicket and |
| 4720 | * ChangeCipherSpec share the same state. |
| 4721 | */ |
| 4722 | ssl->handshake->new_session_ticket = 0; |
| 4723 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4724 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4725 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4726 | 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] | 4727 | return( ret ); |
| 4728 | } |
| 4729 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4730 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) ); |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4731 | |
| 4732 | return( 0 ); |
| 4733 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4734 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4735 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4736 | /* |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4737 | * SSL handshake -- server side -- single step |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4738 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4739 | int mbedtls_ssl_handshake_server_step( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4740 | { |
| 4741 | int ret = 0; |
| 4742 | |
Manuel Pégourié-Gonnard | dba460f | 2015-06-24 22:59:30 +0200 | [diff] [blame] | 4743 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4744 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4745 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4746 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4747 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4748 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4749 | return( ret ); |
| 4750 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4751 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 4752 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4753 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4754 | { |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4755 | if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4756 | return( ret ); |
| 4757 | } |
Hanno Becker | bc2498a | 2018-08-28 10:13:29 +0100 | [diff] [blame] | 4758 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4759 | |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4760 | switch( ssl->state ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4761 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4762 | case MBEDTLS_SSL_HELLO_REQUEST: |
| 4763 | ssl->state = MBEDTLS_SSL_CLIENT_HELLO; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4764 | break; |
| 4765 | |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4766 | /* |
| 4767 | * <== ClientHello |
| 4768 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4769 | case MBEDTLS_SSL_CLIENT_HELLO: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4770 | ret = ssl_parse_client_hello( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4771 | break; |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4772 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4773 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4774 | case MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT: |
| 4775 | return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED ); |
Manuel Pégourié-Gonnard | 579950c | 2014-09-29 17:47:33 +0200 | [diff] [blame] | 4776 | #endif |
| 4777 | |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4778 | /* |
| 4779 | * ==> ServerHello |
| 4780 | * Certificate |
| 4781 | * ( ServerKeyExchange ) |
| 4782 | * ( CertificateRequest ) |
| 4783 | * ServerHelloDone |
| 4784 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4785 | case MBEDTLS_SSL_SERVER_HELLO: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4786 | ret = ssl_write_server_hello( ssl ); |
| 4787 | break; |
| 4788 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4789 | case MBEDTLS_SSL_SERVER_CERTIFICATE: |
| 4790 | ret = mbedtls_ssl_write_certificate( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4791 | break; |
| 4792 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4793 | case MBEDTLS_SSL_SERVER_KEY_EXCHANGE: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4794 | ret = ssl_write_server_key_exchange( ssl ); |
| 4795 | break; |
| 4796 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4797 | case MBEDTLS_SSL_CERTIFICATE_REQUEST: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4798 | ret = ssl_write_certificate_request( ssl ); |
| 4799 | break; |
| 4800 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4801 | case MBEDTLS_SSL_SERVER_HELLO_DONE: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4802 | ret = ssl_write_server_hello_done( ssl ); |
| 4803 | break; |
| 4804 | |
| 4805 | /* |
| 4806 | * <== ( Certificate/Alert ) |
| 4807 | * ClientKeyExchange |
| 4808 | * ( CertificateVerify ) |
| 4809 | * ChangeCipherSpec |
| 4810 | * Finished |
| 4811 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4812 | case MBEDTLS_SSL_CLIENT_CERTIFICATE: |
| 4813 | ret = mbedtls_ssl_parse_certificate( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4814 | break; |
| 4815 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4816 | case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE: |
Hanno Becker | 7d552fa | 2019-08-12 15:04:59 +0100 | [diff] [blame] | 4817 | ret = ssl_process_in_client_key_exchange( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4818 | break; |
| 4819 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4820 | case MBEDTLS_SSL_CERTIFICATE_VERIFY: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4821 | ret = ssl_parse_certificate_verify( ssl ); |
| 4822 | break; |
| 4823 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4824 | case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC: |
| 4825 | ret = mbedtls_ssl_parse_change_cipher_spec( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4826 | break; |
| 4827 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4828 | case MBEDTLS_SSL_CLIENT_FINISHED: |
| 4829 | ret = mbedtls_ssl_parse_finished( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4830 | break; |
| 4831 | |
| 4832 | /* |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4833 | * ==> ( NewSessionTicket ) |
| 4834 | * ChangeCipherSpec |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4835 | * Finished |
| 4836 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4837 | case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC: |
| 4838 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | 7cd5924 | 2013-08-02 13:24:41 +0200 | [diff] [blame] | 4839 | if( ssl->handshake->new_session_ticket != 0 ) |
| 4840 | ret = ssl_write_new_session_ticket( ssl ); |
| 4841 | else |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 4842 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4843 | ret = mbedtls_ssl_write_change_cipher_spec( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4844 | break; |
| 4845 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4846 | case MBEDTLS_SSL_SERVER_FINISHED: |
| 4847 | ret = mbedtls_ssl_write_finished( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4848 | break; |
| 4849 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4850 | case MBEDTLS_SSL_FLUSH_BUFFERS: |
| 4851 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) ); |
| 4852 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4853 | break; |
| 4854 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4855 | case MBEDTLS_SSL_HANDSHAKE_WRAPUP: |
Jarno Lamsa | e1621d4 | 2019-12-19 08:58:56 +0200 | [diff] [blame] | 4856 | ret = mbedtls_ssl_handshake_wrapup( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4857 | break; |
| 4858 | |
Jarno Lamsa | 70abd7a | 2019-11-12 15:39:38 +0200 | [diff] [blame] | 4859 | case MBEDTLS_SSL_INVALID: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4860 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4861 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) ); |
| 4862 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4863 | } |
| 4864 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4865 | return( ret ); |
| 4866 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4867 | #endif /* MBEDTLS_SSL_SRV_C */ |