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 | |
| 66 | memcpy( ssl->cli_id, info, ilen ); |
| 67 | ssl->cli_id_len = ilen; |
| 68 | |
| 69 | return( 0 ); |
| 70 | } |
Manuel Pégourié-Gonnard | d485d19 | 2014-07-23 14:56:15 +0200 | [diff] [blame] | 71 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 72 | void mbedtls_ssl_conf_dtls_cookies( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 73 | mbedtls_ssl_cookie_write_t *f_cookie_write, |
| 74 | mbedtls_ssl_cookie_check_t *f_cookie_check, |
Manuel Pégourié-Gonnard | d485d19 | 2014-07-23 14:56:15 +0200 | [diff] [blame] | 75 | void *p_cookie ) |
| 76 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 77 | conf->f_cookie_write = f_cookie_write; |
| 78 | conf->f_cookie_check = f_cookie_check; |
| 79 | conf->p_cookie = p_cookie; |
Manuel Pégourié-Gonnard | d485d19 | 2014-07-23 14:56:15 +0200 | [diff] [blame] | 80 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 81 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */ |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 82 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 83 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 84 | static int ssl_parse_servername_ext( mbedtls_ssl_context *ssl, |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 85 | const unsigned char *buf, |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 86 | size_t len ) |
| 87 | { |
| 88 | int ret; |
| 89 | size_t servername_list_size, hostname_len; |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 90 | const unsigned char *p; |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 91 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 92 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) ); |
Manuel Pégourié-Gonnard | 96ea2f2 | 2014-02-25 12:26:29 +0100 | [diff] [blame] | 93 | |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 94 | if( len < 2 ) |
| 95 | { |
| 96 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 97 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 98 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 99 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 100 | } |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 101 | servername_list_size = mbedtls_platform_get_uint16_be( buf ); |
| 102 | |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 103 | if( servername_list_size + 2 != len ) |
| 104 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 105 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 106 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 107 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 108 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | p = buf + 2; |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 112 | while( servername_list_size > 2 ) |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 113 | { |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 114 | hostname_len = mbedtls_platform_get_uint16_be( &p[1] ); |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 115 | if( hostname_len + 3 > servername_list_size ) |
| 116 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 117 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 118 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 119 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 120 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 123 | if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME ) |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 124 | { |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 125 | ret = ssl->conf->f_sni( ssl->conf->p_sni, |
| 126 | ssl, p + 3, hostname_len ); |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 127 | if( ret != 0 ) |
| 128 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 129 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 130 | mbedtls_ssl_pend_fatal_alert( ssl, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 131 | MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME ); |
| 132 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 133 | } |
Paul Bakker | 81420ab | 2012-10-23 10:31:15 +0000 | [diff] [blame] | 134 | return( 0 ); |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | servername_list_size -= hostname_len + 3; |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 138 | p += hostname_len + 3; |
| 139 | } |
| 140 | |
| 141 | if( servername_list_size != 0 ) |
| 142 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 143 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 144 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 145 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 146 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | return( 0 ); |
| 150 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 151 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 152 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 153 | static int ssl_parse_renegotiation_info( mbedtls_ssl_context *ssl, |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 154 | const unsigned char *buf, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 155 | size_t len ) |
| 156 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 157 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 158 | if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ) |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 159 | { |
| 160 | /* Check verify-data in constant-time. The length OTOH is no secret */ |
| 161 | if( len != 1 + ssl->verify_data_len || |
| 162 | buf[0] != ssl->verify_data_len || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 163 | mbedtls_ssl_safer_memcmp( buf + 1, ssl->peer_verify_data, |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 164 | ssl->verify_data_len ) != 0 ) |
| 165 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 166 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 167 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 168 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 169 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 173 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 174 | { |
| 175 | if( len != 1 || buf[0] != 0x0 ) |
| 176 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 177 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 178 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 179 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 180 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 183 | ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 184 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 185 | |
| 186 | return( 0 ); |
| 187 | } |
| 188 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 189 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 190 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 191 | |
| 192 | /* |
| 193 | * Status of the implementation of signature-algorithms extension: |
| 194 | * |
| 195 | * Currently, we are only considering the signature-algorithm extension |
| 196 | * to pick a ciphersuite which allows us to send the ServerKeyExchange |
| 197 | * message with a signature-hash combination that the user allows. |
| 198 | * |
| 199 | * We do *not* check whether all certificates in our certificate |
| 200 | * chain are signed with an allowed signature-hash pair. |
| 201 | * This needs to be done at a later stage. |
| 202 | * |
| 203 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 204 | static int ssl_parse_signature_algorithms_ext( mbedtls_ssl_context *ssl, |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 205 | const unsigned char *buf, |
| 206 | size_t len ) |
| 207 | { |
| 208 | size_t sig_alg_list_size; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 209 | |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 210 | const unsigned char *p; |
Manuel Pégourié-Gonnard | 08e81e0 | 2014-07-08 12:56:25 +0200 | [diff] [blame] | 211 | const unsigned char *end = buf + len; |
Manuel Pégourié-Gonnard | 08e81e0 | 2014-07-08 12:56:25 +0200 | [diff] [blame] | 212 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 213 | mbedtls_md_type_t md_cur; |
| 214 | mbedtls_pk_type_t sig_cur; |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 215 | |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 216 | if ( len < 2 ) { |
| 217 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 218 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 219 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 220 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 221 | } |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 222 | |
| 223 | sig_alg_list_size = mbedtls_platform_get_uint16_be( buf ); |
| 224 | |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 225 | if( sig_alg_list_size + 2 != len || |
Manuel Pégourié-Gonnard | 08e81e0 | 2014-07-08 12:56:25 +0200 | [diff] [blame] | 226 | sig_alg_list_size % 2 != 0 ) |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 227 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 228 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 229 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 230 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 231 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 234 | /* Currently we only guarantee signing the ServerKeyExchange message according |
| 235 | * to the constraints specified in this extension (see above), so it suffices |
| 236 | * 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] | 237 | * |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 238 | * This will change when we also consider certificate signatures, |
| 239 | * in which case we will need to remember the whole signature-hash |
| 240 | * pair list from the extension. |
Manuel Pégourié-Gonnard | 08e81e0 | 2014-07-08 12:56:25 +0200 | [diff] [blame] | 241 | */ |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 242 | |
| 243 | for( p = buf + 2; p < end; p += 2 ) |
| 244 | { |
| 245 | /* Silently ignore unknown signature or hash algorithms. */ |
| 246 | |
| 247 | if( ( sig_cur = mbedtls_ssl_pk_alg_from_sig( p[1] ) ) == MBEDTLS_PK_NONE ) |
| 248 | { |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 249 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext" |
| 250 | " unknown sig alg encoding %d", p[1] ) ); |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 251 | continue; |
| 252 | } |
| 253 | |
| 254 | /* Check if we support the hash the user proposes */ |
| 255 | md_cur = mbedtls_ssl_md_alg_from_hash( p[0] ); |
| 256 | if( md_cur == MBEDTLS_MD_NONE ) |
| 257 | { |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 258 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext:" |
| 259 | " unknown hash alg encoding %d", p[0] ) ); |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 260 | continue; |
| 261 | } |
| 262 | |
| 263 | if( mbedtls_ssl_check_sig_hash( ssl, md_cur ) == 0 ) |
| 264 | { |
| 265 | 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] | 266 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext:" |
| 267 | " match sig %d and hash %d", |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 268 | sig_cur, md_cur ) ); |
| 269 | } |
| 270 | else |
| 271 | { |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 272 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: " |
| 273 | "hash alg %d not supported", md_cur ) ); |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 274 | } |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 277 | return( 0 ); |
| 278 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 279 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && |
| 280 | MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 281 | |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 282 | #if defined(MBEDTLS_ECDH_C) || \ |
| 283 | defined(MBEDTLS_ECDSA_C) || \ |
| 284 | defined(MBEDTLS_USE_TINYCRYPT) || \ |
Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 285 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 286 | static int ssl_parse_supported_elliptic_curves( mbedtls_ssl_context *ssl, |
Hanno Becker | d3b2fcb | 2019-06-17 14:30:05 +0100 | [diff] [blame] | 287 | const unsigned char *buf, size_t len, |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 288 | unsigned char const **list_start, size_t *list_len ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 289 | { |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 290 | size_t list_size; |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 291 | const unsigned char *p; |
| 292 | |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 293 | if ( len < 2 ) { |
| 294 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 295 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 296 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 297 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 298 | } |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 299 | |
| 300 | list_size = mbedtls_platform_get_uint16_be( buf ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 301 | if( list_size + 2 != len || |
| 302 | list_size % 2 != 0 ) |
| 303 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 304 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 305 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 306 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 307 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | p = buf + 2; |
Hanno Becker | d3b2fcb | 2019-06-17 14:30:05 +0100 | [diff] [blame] | 311 | |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 312 | /* Remember list for later. */ |
| 313 | *list_start = p; |
| 314 | *list_len = list_size / 2; |
| 315 | |
| 316 | while( list_size > 0 ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 317 | { |
Arto Kinnunen | a3fa06e | 2019-09-09 12:22:51 +0300 | [diff] [blame] | 318 | uint16_t const peer_tls_id = (uint16_t) |
| 319 | mbedtls_platform_get_uint16_be( p ); |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 320 | |
Hanno Becker | a4a9c69 | 2019-06-18 16:55:47 +0100 | [diff] [blame] | 321 | MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_EC_TLS_ID( own_tls_id ) |
| 322 | if( own_tls_id == peer_tls_id && |
| 323 | ssl->handshake->curve_tls_id == 0 ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 324 | { |
Hanno Becker | a4a9c69 | 2019-06-18 16:55:47 +0100 | [diff] [blame] | 325 | ssl->handshake->curve_tls_id = own_tls_id; |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 326 | } |
Hanno Becker | a4a9c69 | 2019-06-18 16:55:47 +0100 | [diff] [blame] | 327 | MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_EC_TLS_ID |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 328 | |
| 329 | list_size -= 2; |
| 330 | p += 2; |
| 331 | } |
| 332 | |
| 333 | return( 0 ); |
| 334 | } |
| 335 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 336 | static int ssl_parse_supported_point_formats( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 337 | const unsigned char *buf, |
| 338 | size_t len ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 339 | { |
| 340 | size_t list_size; |
| 341 | const unsigned char *p; |
| 342 | |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 343 | if( len == 0 || (size_t)( buf[0] + 1 ) != len ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 344 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 345 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 346 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 347 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 348 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 349 | } |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 350 | list_size = buf[0]; |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 351 | |
Manuel Pégourié-Gonnard | c1b46d0 | 2015-09-16 11:18:32 +0200 | [diff] [blame] | 352 | p = buf + 1; |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 353 | while( list_size > 0 ) |
| 354 | { |
Hanno Becker | 27b7e50 | 2019-08-23 14:39:50 +0100 | [diff] [blame] | 355 | if( p[0] == MBEDTLS_SSL_EC_PF_UNCOMPRESSED || |
| 356 | p[0] == MBEDTLS_SSL_EC_PF_COMPRESSED ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 357 | { |
Hanno Becker | 975b9ee | 2019-07-24 10:09:27 +0100 | [diff] [blame] | 358 | #if defined(MBEDTLS_ECDH_C) |
Manuel Pégourié-Gonnard | 5734b2d | 2013-08-15 19:04:02 +0200 | [diff] [blame] | 359 | ssl->handshake->ecdh_ctx.point_format = p[0]; |
Robert Cragie | 136884c | 2015-10-02 13:34:31 +0100 | [diff] [blame] | 360 | #endif |
Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 361 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Robert Cragie | 136884c | 2015-10-02 13:34:31 +0100 | [diff] [blame] | 362 | ssl->handshake->ecjpake_ctx.point_format = p[0]; |
| 363 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 364 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 365 | return( 0 ); |
| 366 | } |
| 367 | |
| 368 | list_size--; |
| 369 | p++; |
| 370 | } |
| 371 | |
| 372 | return( 0 ); |
| 373 | } |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 374 | #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || MBEDTLS_USE_TINYCRYPT |
Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 375 | MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 376 | |
Manuel Pégourié-Gonnard | bf57be6 | 2015-09-16 15:04:01 +0200 | [diff] [blame] | 377 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 378 | static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl, |
| 379 | const unsigned char *buf, |
| 380 | size_t len ) |
| 381 | { |
| 382 | int ret; |
| 383 | |
| 384 | if( mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 ) |
| 385 | { |
| 386 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) ); |
| 387 | return( 0 ); |
| 388 | } |
| 389 | |
| 390 | if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx, |
| 391 | buf, len ) ) != 0 ) |
| 392 | { |
| 393 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 394 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 395 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Manuel Pégourié-Gonnard | bf57be6 | 2015-09-16 15:04:01 +0200 | [diff] [blame] | 396 | return( ret ); |
| 397 | } |
| 398 | |
| 399 | /* Only mark the extension as OK when we're sure it is */ |
| 400 | ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK; |
| 401 | |
| 402 | return( 0 ); |
| 403 | } |
| 404 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
| 405 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 406 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 407 | 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] | 408 | const unsigned char *buf, |
| 409 | size_t len ) |
| 410 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 411 | 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] | 412 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 413 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 414 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 415 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 416 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 48f8d0d | 2013-07-17 10:25:37 +0200 | [diff] [blame] | 417 | } |
| 418 | |
Manuel Pégourié-Gonnard | ed4af8b | 2013-07-18 14:07:09 +0200 | [diff] [blame] | 419 | ssl->session_negotiate->mfl_code = buf[0]; |
| 420 | |
Manuel Pégourié-Gonnard | 48f8d0d | 2013-07-17 10:25:37 +0200 | [diff] [blame] | 421 | return( 0 ); |
| 422 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 423 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 48f8d0d | 2013-07-17 10:25:37 +0200 | [diff] [blame] | 424 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 425 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | c403b26 | 2019-04-26 13:56:39 +0100 | [diff] [blame] | 426 | static int ssl_parse_cid_ext( mbedtls_ssl_context *ssl, |
| 427 | const unsigned char *buf, |
| 428 | size_t len ) |
| 429 | { |
| 430 | size_t peer_cid_len; |
| 431 | |
| 432 | /* CID extension only makes sense in DTLS */ |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 433 | if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ) |
Hanno Becker | c403b26 | 2019-04-26 13:56:39 +0100 | [diff] [blame] | 434 | { |
| 435 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 436 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 437 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Hanno Becker | c403b26 | 2019-04-26 13:56:39 +0100 | [diff] [blame] | 438 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 439 | } |
| 440 | |
| 441 | /* |
Hanno Becker | 3cdf8fe | 2019-05-15 10:26:32 +0100 | [diff] [blame] | 442 | * Quoting draft-ietf-tls-dtls-connection-id-05 |
| 443 | * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05 |
Hanno Becker | c403b26 | 2019-04-26 13:56:39 +0100 | [diff] [blame] | 444 | * |
| 445 | * struct { |
| 446 | * opaque cid<0..2^8-1>; |
| 447 | * } ConnectionId; |
| 448 | */ |
| 449 | |
| 450 | if( len < 1 ) |
| 451 | { |
| 452 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 453 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 454 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Hanno Becker | c403b26 | 2019-04-26 13:56:39 +0100 | [diff] [blame] | 455 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 456 | } |
| 457 | |
| 458 | peer_cid_len = *buf++; |
| 459 | len--; |
| 460 | |
| 461 | if( len != peer_cid_len ) |
| 462 | { |
| 463 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 464 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 465 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Hanno Becker | c403b26 | 2019-04-26 13:56:39 +0100 | [diff] [blame] | 466 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 467 | } |
| 468 | |
| 469 | /* Ignore CID if the user has disabled its use. */ |
| 470 | if( ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED ) |
| 471 | { |
| 472 | /* Leave ssl->handshake->cid_in_use in its default |
| 473 | * value of MBEDTLS_SSL_CID_DISABLED. */ |
| 474 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Client sent CID extension, but CID disabled" ) ); |
| 475 | return( 0 ); |
| 476 | } |
| 477 | |
| 478 | if( peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX ) |
| 479 | { |
| 480 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 481 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 482 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Hanno Becker | c403b26 | 2019-04-26 13:56:39 +0100 | [diff] [blame] | 483 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 484 | } |
| 485 | |
Hanno Becker | 19976b5 | 2019-05-03 12:43:44 +0100 | [diff] [blame] | 486 | ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED; |
Hanno Becker | c403b26 | 2019-04-26 13:56:39 +0100 | [diff] [blame] | 487 | ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len; |
| 488 | memcpy( ssl->handshake->peer_cid, buf, peer_cid_len ); |
| 489 | |
| 490 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use of CID extension negotiated" ) ); |
| 491 | MBEDTLS_SSL_DEBUG_BUF( 3, "Client CID", buf, peer_cid_len ); |
| 492 | |
Hanno Becker | c403b26 | 2019-04-26 13:56:39 +0100 | [diff] [blame] | 493 | return( 0 ); |
| 494 | } |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 495 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | c403b26 | 2019-04-26 13:56:39 +0100 | [diff] [blame] | 496 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 497 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 498 | 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] | 499 | const unsigned char *buf, |
| 500 | size_t len ) |
| 501 | { |
| 502 | if( len != 0 ) |
| 503 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 504 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 505 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 506 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 507 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | ((void) buf); |
| 511 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 512 | if( ssl->conf->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 513 | ssl->session_negotiate->trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 514 | |
| 515 | return( 0 ); |
| 516 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 517 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 518 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 519 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 520 | 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] | 521 | const unsigned char *buf, |
| 522 | size_t len ) |
| 523 | { |
| 524 | if( len != 0 ) |
| 525 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 526 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 527 | mbedtls_ssl_pend_fatal_alert( ssl, |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 528 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 529 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | ((void) buf); |
| 533 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 534 | if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED && |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 535 | 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] | 536 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 537 | ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | return( 0 ); |
| 541 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 542 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 543 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 544 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 545 | 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] | 546 | const unsigned char *buf, |
| 547 | size_t len ) |
| 548 | { |
| 549 | if( len != 0 ) |
| 550 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 551 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 552 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 553 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 554 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | ((void) buf); |
| 558 | |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 559 | return( 0 ); |
| 560 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 561 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 562 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 563 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 564 | 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] | 565 | unsigned char *buf, |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 566 | size_t len ) |
| 567 | { |
Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 568 | int ret; |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 569 | mbedtls_ssl_session session; |
Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 570 | |
Manuel Pégourié-Gonnard | bae389b | 2015-06-24 10:45:58 +0200 | [diff] [blame] | 571 | mbedtls_ssl_session_init( &session ); |
| 572 | |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 573 | if( ssl->conf->f_ticket_parse == NULL || |
| 574 | ssl->conf->f_ticket_write == NULL ) |
| 575 | { |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 576 | return( 0 ); |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 577 | } |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 578 | |
Manuel Pégourié-Gonnard | 306827e | 2013-08-02 18:05:14 +0200 | [diff] [blame] | 579 | /* Remember the client asked us to send a new ticket */ |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 580 | ssl->handshake->new_session_ticket = 1; |
| 581 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 582 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) ); |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 583 | |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 584 | if( len == 0 ) |
| 585 | return( 0 ); |
| 586 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 587 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 588 | if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ) |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 589 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 590 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) ); |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 591 | return( 0 ); |
| 592 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 593 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 594 | |
| 595 | /* |
Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 596 | * Failures are ok: just ignore the ticket and proceed. |
| 597 | */ |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 598 | if( ( ret = ssl->conf->f_ticket_parse( ssl->conf->p_ticket, &session, |
| 599 | buf, len ) ) != 0 ) |
Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 600 | { |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 601 | mbedtls_ssl_session_free( &session ); |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 602 | |
| 603 | if( ret == MBEDTLS_ERR_SSL_INVALID_MAC ) |
| 604 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket is not authentic" ) ); |
| 605 | else if( ret == MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED ) |
| 606 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket is expired" ) ); |
| 607 | else |
| 608 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_ticket_parse", ret ); |
| 609 | |
Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 610 | return( 0 ); |
Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 611 | } |
Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 612 | |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 613 | /* |
| 614 | * Keep the session ID sent by the client, since we MUST send it back to |
| 615 | * inform them we're accepting the ticket (RFC 5077 section 3.4) |
| 616 | */ |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 617 | session.id_len = ssl->session_negotiate->id_len; |
| 618 | memcpy( &session.id, ssl->session_negotiate->id, session.id_len ); |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 619 | |
| 620 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
| 621 | memcpy( ssl->session_negotiate, &session, sizeof( mbedtls_ssl_session ) ); |
| 622 | |
| 623 | /* Zeroize instead of free as we copied the content */ |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 624 | mbedtls_platform_zeroize( &session, sizeof( mbedtls_ssl_session ) ); |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 625 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 626 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) ); |
Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 627 | |
Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 628 | ssl->handshake->resume = 1; |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 629 | |
Manuel Pégourié-Gonnard | 306827e | 2013-08-02 18:05:14 +0200 | [diff] [blame] | 630 | /* Don't send a new ticket after all, this one is OK */ |
| 631 | ssl->handshake->new_session_ticket = 0; |
| 632 | |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 633 | return( 0 ); |
| 634 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 635 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 636 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 637 | #if defined(MBEDTLS_SSL_ALPN) |
| 638 | static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 14beb08 | 2014-07-08 13:50:35 +0200 | [diff] [blame] | 639 | const unsigned char *buf, size_t len ) |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 640 | { |
Paul Bakker | 14b16c6 | 2014-05-28 11:33:54 +0200 | [diff] [blame] | 641 | size_t list_len, cur_len, ours_len; |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 642 | const unsigned char *theirs, *start, *end; |
| 643 | const char **ours; |
| 644 | |
| 645 | /* If ALPN not configured, just ignore the extension */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 646 | if( ssl->conf->alpn_list == NULL ) |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 647 | return( 0 ); |
| 648 | |
| 649 | /* |
| 650 | * opaque ProtocolName<1..2^8-1>; |
| 651 | * |
| 652 | * struct { |
| 653 | * ProtocolName protocol_name_list<2..2^16-1> |
| 654 | * } ProtocolNameList; |
| 655 | */ |
| 656 | |
| 657 | /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */ |
| 658 | if( len < 4 ) |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 659 | { |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 660 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 661 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 662 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 663 | } |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 664 | |
Arto Kinnunen | a3fa06e | 2019-09-09 12:22:51 +0300 | [diff] [blame] | 665 | list_len = mbedtls_platform_get_uint16_be ( buf ); |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 666 | |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 667 | if( list_len != len - 2 ) |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 668 | { |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 669 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 670 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 671 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 672 | } |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 673 | |
| 674 | /* |
Manuel Pégourié-Gonnard | 239987f | 2018-01-09 10:43:43 +0100 | [diff] [blame] | 675 | * Validate peer's list (lengths) |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 676 | */ |
| 677 | start = buf + 2; |
| 678 | end = buf + len; |
Manuel Pégourié-Gonnard | 239987f | 2018-01-09 10:43:43 +0100 | [diff] [blame] | 679 | for( theirs = start; theirs != end; theirs += cur_len ) |
| 680 | { |
| 681 | cur_len = *theirs++; |
| 682 | |
| 683 | /* Current identifier must fit in list */ |
| 684 | if( cur_len > (size_t)( end - theirs ) ) |
| 685 | { |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 686 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 687 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 239987f | 2018-01-09 10:43:43 +0100 | [diff] [blame] | 688 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 689 | } |
| 690 | |
| 691 | /* Empty strings MUST NOT be included */ |
| 692 | if( cur_len == 0 ) |
| 693 | { |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 694 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 695 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Manuel Pégourié-Gonnard | 239987f | 2018-01-09 10:43:43 +0100 | [diff] [blame] | 696 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | /* |
| 701 | * Use our order of preference |
| 702 | */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 703 | for( ours = ssl->conf->alpn_list; *ours != NULL; ours++ ) |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 704 | { |
Paul Bakker | 14b16c6 | 2014-05-28 11:33:54 +0200 | [diff] [blame] | 705 | ours_len = strlen( *ours ); |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 706 | for( theirs = start; theirs != end; theirs += cur_len ) |
| 707 | { |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 708 | cur_len = *theirs++; |
| 709 | |
Paul Bakker | 14b16c6 | 2014-05-28 11:33:54 +0200 | [diff] [blame] | 710 | if( cur_len == ours_len && |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 711 | memcmp( theirs, *ours, cur_len ) == 0 ) |
| 712 | { |
| 713 | ssl->alpn_chosen = *ours; |
| 714 | return( 0 ); |
| 715 | } |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | /* If we get there, no match was found */ |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 720 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 721 | MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 722 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 723 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 724 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 725 | |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 726 | /* |
| 727 | * Auxiliary functions for ServerHello parsing and related actions |
| 728 | */ |
| 729 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 730 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 731 | /* |
Manuel Pégourié-Gonnard | 6458e3b | 2015-01-08 14:16:56 +0100 | [diff] [blame] | 732 | * 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] | 733 | */ |
Hanno Becker | 461fa72 | 2019-08-21 17:05:03 +0100 | [diff] [blame] | 734 | #if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 735 | static int ssl_check_key_curve( mbedtls_pk_context *pk, |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 736 | unsigned char const *acceptable_ec_tls_ids, |
| 737 | size_t ec_tls_ids_len ) |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 738 | { |
Hanno Becker | 461fa72 | 2019-08-21 17:05:03 +0100 | [diff] [blame] | 739 | uint16_t tls_id; |
| 740 | |
| 741 | #if defined(MBEDTLS_USE_TINYCRYPT) |
| 742 | ((void) pk); |
| 743 | tls_id = 23; /* TLS ID for Secp256r1. */ |
| 744 | #else |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 745 | mbedtls_ecp_curve_info const *info; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 746 | 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] | 747 | |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 748 | info = mbedtls_ecp_curve_info_from_grp_id( grp_id ); |
| 749 | if( info == NULL ) |
Hanno Becker | d3b2fcb | 2019-06-17 14:30:05 +0100 | [diff] [blame] | 750 | return( -1 ); |
| 751 | |
Hanno Becker | 461fa72 | 2019-08-21 17:05:03 +0100 | [diff] [blame] | 752 | tls_id = info->tls_id; |
| 753 | #endif /* MBEDTLS_USE_TINYCRYPT */ |
| 754 | |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 755 | if( acceptable_ec_tls_ids == NULL ) |
| 756 | return( -1 ); |
| 757 | |
| 758 | while( ec_tls_ids_len-- != 0 ) |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 759 | { |
Arto Kinnunen | 4f4849a | 2019-09-09 10:21:18 +0300 | [diff] [blame] | 760 | uint16_t const cur_tls_id = (uint16_t) |
| 761 | mbedtls_platform_get_uint16_be( acceptable_ec_tls_ids ); |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 762 | |
Hanno Becker | 461fa72 | 2019-08-21 17:05:03 +0100 | [diff] [blame] | 763 | if( cur_tls_id == tls_id ) |
Manuel Pégourié-Gonnard | 6458e3b | 2015-01-08 14:16:56 +0100 | [diff] [blame] | 764 | return( 0 ); |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 765 | |
| 766 | acceptable_ec_tls_ids += 2; |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 767 | } |
| 768 | |
Manuel Pégourié-Gonnard | 6458e3b | 2015-01-08 14:16:56 +0100 | [diff] [blame] | 769 | return( -1 ); |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 770 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 771 | #endif /* MBEDTLS_ECDSA_C */ |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 772 | |
| 773 | /* |
| 774 | * Try picking a certificate for this ciphersuite, |
| 775 | * return 0 on success and -1 on failure. |
| 776 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 777 | static int ssl_pick_cert( mbedtls_ssl_context *ssl, |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 778 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info, |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 779 | unsigned char const *acceptable_ec_tls_ids, |
| 780 | size_t ec_tls_ids_len ) |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 781 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 782 | mbedtls_ssl_key_cert *cur, *list, *fallback = NULL; |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 783 | mbedtls_pk_type_t pk_alg = |
| 784 | mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ); |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 785 | uint32_t flags; |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 786 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 787 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 788 | if( ssl->handshake->sni_key_cert != NULL ) |
| 789 | list = ssl->handshake->sni_key_cert; |
| 790 | else |
| 791 | #endif |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 792 | list = ssl->conf->key_cert; |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 793 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 794 | if( pk_alg == MBEDTLS_PK_NONE ) |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 795 | return( 0 ); |
| 796 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 797 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite requires certificate" ) ); |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 798 | |
Manuel Pégourié-Gonnard | e540b49 | 2015-07-07 12:44:38 +0200 | [diff] [blame] | 799 | if( list == NULL ) |
| 800 | { |
| 801 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server has no certificate" ) ); |
| 802 | return( -1 ); |
| 803 | } |
| 804 | |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 805 | for( cur = list; cur != NULL; cur = cur->next ) |
| 806 | { |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 807 | int match = 1; |
| 808 | mbedtls_pk_context *pk; |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 809 | |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 810 | /* WARNING: With the current X.509 caching architecture, this MUST |
| 811 | * happen outside of the PK acquire/release block, because it moves |
| 812 | * the cached PK context. In a threading-enabled build, this would |
| 813 | * rightfully fail, but lead to a use-after-free otherwise. */ |
| 814 | MBEDTLS_SSL_DEBUG_CRT( 3, "candidate certificate chain, certificate", |
| 815 | cur->cert ); |
| 816 | |
Hanno Becker | 74b89f6 | 2019-02-17 21:22:07 +0000 | [diff] [blame] | 817 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
| 818 | /* ASYNC_PRIVATE may use a NULL entry for the opaque private key, so |
| 819 | * we have to use the public key context to infer the capabilities |
| 820 | * of the key. */ |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 821 | { |
| 822 | int ret; |
| 823 | ret = mbedtls_x509_crt_pk_acquire( cur->cert, &pk ); |
| 824 | if( ret != 0 ) |
Hanno Becker | 2224ccf | 2019-06-28 10:52:45 +0100 | [diff] [blame] | 825 | { |
| 826 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_pk_acquire", ret ); |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 827 | return( ret ); |
Hanno Becker | 2224ccf | 2019-06-28 10:52:45 +0100 | [diff] [blame] | 828 | } |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 829 | } |
Hanno Becker | 74b89f6 | 2019-02-17 21:22:07 +0000 | [diff] [blame] | 830 | #else |
| 831 | /* Outside of ASYNC_PRIVATE, use private key context directly |
| 832 | * instead of querying for the public key context from the |
| 833 | * certificate, so save a few bytes of code. */ |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 834 | pk = cur->key; |
Hanno Becker | 74b89f6 | 2019-02-17 21:22:07 +0000 | [diff] [blame] | 835 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 836 | |
Hanno Becker | 74b89f6 | 2019-02-17 21:22:07 +0000 | [diff] [blame] | 837 | if( ! mbedtls_pk_can_do( pk, pk_alg ) ) |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 838 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 839 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: key type" ) ); |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 840 | match = 0; |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 841 | } |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 842 | |
Hanno Becker | 461fa72 | 2019-08-21 17:05:03 +0100 | [diff] [blame] | 843 | #if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT) |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 844 | if( pk_alg == MBEDTLS_PK_ECDSA && |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 845 | ssl_check_key_curve( pk, |
| 846 | acceptable_ec_tls_ids, |
| 847 | ec_tls_ids_len ) != 0 ) |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 848 | { |
| 849 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: elliptic curve" ) ); |
| 850 | match = 0; |
| 851 | } |
Hanno Becker | d3b2fcb | 2019-06-17 14:30:05 +0100 | [diff] [blame] | 852 | #else |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 853 | ((void) acceptable_ec_tls_ids); |
| 854 | ((void) ec_tls_ids_len); |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 855 | #endif |
| 856 | |
| 857 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Hanno Becker | c6d1c3e | 2019-03-05 13:50:56 +0000 | [diff] [blame] | 858 | mbedtls_x509_crt_pk_release( cur->cert ); |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 859 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
| 860 | |
| 861 | if( match == 0 ) |
| 862 | continue; |
| 863 | |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 864 | /* |
| 865 | * This avoids sending the client a cert it'll reject based on |
| 866 | * keyUsage or other extensions. |
| 867 | * |
| 868 | * It also allows the user to provision different certificates for |
| 869 | * different uses based on keyUsage, eg if they want to avoid signing |
| 870 | * and decrypting with the same RSA key. |
| 871 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 872 | if( mbedtls_ssl_check_cert_usage( cur->cert, ciphersuite_info, |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 873 | MBEDTLS_SSL_IS_SERVER, &flags ) != 0 ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 874 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 875 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: " |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 876 | "(extended) key usage extension" ) ); |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 877 | continue; |
| 878 | } |
| 879 | |
Hanno Becker | 828a8c0 | 2019-02-26 16:48:55 +0000 | [diff] [blame] | 880 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 881 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Manuel Pégourié-Gonnard | df331a5 | 2015-01-08 16:43:07 +0100 | [diff] [blame] | 882 | /* |
| 883 | * Try to select a SHA-1 certificate for pre-1.2 clients, but still |
| 884 | * present them a SHA-higher cert rather than failing if it's the only |
| 885 | * one we got that satisfies the other conditions. |
| 886 | */ |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 887 | if( mbedtls_ssl_get_minor_ver( ssl ) < MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | df331a5 | 2015-01-08 16:43:07 +0100 | [diff] [blame] | 888 | { |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 889 | mbedtls_md_type_t sig_md; |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 890 | { |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 891 | int ret; |
Hanno Becker | 5f268b3 | 2019-05-20 16:26:34 +0100 | [diff] [blame] | 892 | mbedtls_x509_crt_frame const *frame; |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 893 | ret = mbedtls_x509_crt_frame_acquire( cur->cert, &frame ); |
| 894 | if( ret != 0 ) |
Hanno Becker | 2224ccf | 2019-06-28 10:52:45 +0100 | [diff] [blame] | 895 | { |
| 896 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_frame_acquire", ret ); |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 897 | return( ret ); |
Hanno Becker | 2224ccf | 2019-06-28 10:52:45 +0100 | [diff] [blame] | 898 | } |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 899 | sig_md = frame->sig_md; |
Hanno Becker | c6d1c3e | 2019-03-05 13:50:56 +0000 | [diff] [blame] | 900 | mbedtls_x509_crt_frame_release( cur->cert ); |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | if( sig_md != MBEDTLS_MD_SHA1 ) |
| 904 | { |
| 905 | if( fallback == NULL ) |
| 906 | fallback = cur; |
| 907 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 908 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate not preferred: " |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 909 | "sha-2 with pre-TLS 1.2 client" ) ); |
| 910 | continue; |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 911 | } |
Manuel Pégourié-Gonnard | df331a5 | 2015-01-08 16:43:07 +0100 | [diff] [blame] | 912 | } |
Hanno Becker | 828a8c0 | 2019-02-26 16:48:55 +0000 | [diff] [blame] | 913 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || |
| 914 | MBEDTLS_SSL_PROTO_TLS1_1 || |
| 915 | MBEDTLS_SSL_PROTO_SSL3 */ |
Manuel Pégourié-Gonnard | df331a5 | 2015-01-08 16:43:07 +0100 | [diff] [blame] | 916 | |
Manuel Pégourié-Gonnard | 846ba47 | 2015-01-08 13:54:38 +0100 | [diff] [blame] | 917 | /* If we get there, we got a winner */ |
| 918 | break; |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 919 | } |
| 920 | |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 921 | if( cur == NULL ) |
| 922 | cur = fallback; |
| 923 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 924 | /* 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] | 925 | if( cur != NULL ) |
| 926 | { |
| 927 | ssl->handshake->key_cert = cur; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 928 | MBEDTLS_SSL_DEBUG_CRT( 3, "selected certificate chain, certificate", |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 929 | ssl->handshake->key_cert->cert ); |
Manuel Pégourié-Gonnard | df331a5 | 2015-01-08 16:43:07 +0100 | [diff] [blame] | 930 | return( 0 ); |
| 931 | } |
| 932 | |
| 933 | return( -1 ); |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 934 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 935 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 936 | |
| 937 | /* |
| 938 | * Check if a given ciphersuite is suitable for use with our config/keys/etc |
| 939 | * Sets ciphersuite_info only if the suite matches. |
| 940 | */ |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 941 | static int ssl_ciphersuite_is_match( mbedtls_ssl_context *ssl, |
| 942 | mbedtls_ssl_ciphersuite_handle_t suite_info, |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 943 | unsigned char const *acceptable_ec_tls_ids, |
| 944 | size_t ec_tls_ids_len ) |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 945 | { |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 946 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
Hanno Becker | 4cb1f4d | 2017-10-10 15:59:57 +0100 | [diff] [blame] | 947 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 948 | mbedtls_pk_type_t sig_type; |
| 949 | #endif |
| 950 | |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 951 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "trying ciphersuite: %s", |
| 952 | mbedtls_ssl_suite_get_name( suite_info ) ) ); |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 953 | |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 954 | if( mbedtls_ssl_suite_get_min_minor_ver( suite_info ) |
| 955 | > mbedtls_ssl_get_minor_ver( ssl ) || |
| 956 | mbedtls_ssl_suite_get_max_minor_ver( suite_info ) |
| 957 | < mbedtls_ssl_get_minor_ver( ssl ) ) |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 958 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 959 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: version" ) ); |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 960 | return( 0 ); |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 961 | } |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 962 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 963 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 964 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 965 | ( mbedtls_ssl_suite_get_flags( suite_info ) & |
| 966 | MBEDTLS_CIPHERSUITE_NODTLS ) ) |
| 967 | { |
Manuel Pégourié-Gonnard | d666451 | 2014-02-06 13:26:57 +0100 | [diff] [blame] | 968 | return( 0 ); |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 969 | } |
Manuel Pégourié-Gonnard | d666451 | 2014-02-06 13:26:57 +0100 | [diff] [blame] | 970 | #endif |
| 971 | |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 972 | #if defined(MBEDTLS_ARC4_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 973 | if( ssl->conf->arc4_disabled == MBEDTLS_SSL_ARC4_DISABLED && |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 974 | 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] | 975 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 976 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: rc4" ) ); |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 977 | return( 0 ); |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 978 | } |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 979 | #endif |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 980 | |
Manuel Pégourié-Gonnard | e511b4e | 2015-09-16 14:11:09 +0200 | [diff] [blame] | 981 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 982 | if( mbedtls_ssl_suite_get_key_exchange( suite_info ) == |
| 983 | MBEDTLS_KEY_EXCHANGE_ECJPAKE && |
Manuel Pégourié-Gonnard | bf57be6 | 2015-09-16 15:04:01 +0200 | [diff] [blame] | 984 | ( 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] | 985 | { |
Manuel Pégourié-Gonnard | bf57be6 | 2015-09-16 15:04:01 +0200 | [diff] [blame] | 986 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: ecjpake " |
| 987 | "not configured or ext missing" ) ); |
Manuel Pégourié-Gonnard | e511b4e | 2015-09-16 14:11:09 +0200 | [diff] [blame] | 988 | return( 0 ); |
| 989 | } |
| 990 | #endif |
| 991 | |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 992 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 993 | defined(MBEDTLS_USE_TINYCRYPT) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 994 | if( mbedtls_ssl_ciphersuite_uses_ec( suite_info ) && |
Hanno Becker | 004619f | 2019-06-18 16:07:32 +0100 | [diff] [blame] | 995 | ssl->handshake->curve_tls_id == 0 ) |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 996 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 997 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: " |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 998 | "no common elliptic curve" ) ); |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 999 | return( 0 ); |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 1000 | } |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 1001 | #endif |
| 1002 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1003 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 1004 | /* If the ciphersuite requires a pre-shared key and we don't |
| 1005 | * have one, skip it now rather than failing later */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1006 | if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) && |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1007 | ssl->conf->f_psk == NULL && |
| 1008 | ( ssl->conf->psk == NULL || ssl->conf->psk_identity == NULL || |
| 1009 | 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] | 1010 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1011 | 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] | 1012 | return( 0 ); |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 1013 | } |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 1014 | #endif |
| 1015 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 1016 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 1017 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
| 1018 | /* If the ciphersuite requires signing, check whether |
| 1019 | * a suitable hash algorithm is present. */ |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 1020 | if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 1021 | { |
| 1022 | sig_type = mbedtls_ssl_get_ciphersuite_sig_alg( suite_info ); |
| 1023 | if( sig_type != MBEDTLS_PK_NONE && |
| 1024 | mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs, sig_type ) == MBEDTLS_MD_NONE ) |
| 1025 | { |
| 1026 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no suitable hash algorithm " |
| 1027 | "for signature algorithm %d", sig_type ) ); |
| 1028 | return( 0 ); |
| 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && |
| 1033 | MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
| 1034 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1035 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 1036 | /* |
| 1037 | * Final check: if ciphersuite requires us to have a |
| 1038 | * certificate/key of a particular type: |
| 1039 | * - select the appropriate certificate if we have one, or |
| 1040 | * - try the next ciphersuite if we don't |
| 1041 | * This must be done last since we modify the key_cert list. |
| 1042 | */ |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 1043 | if( ssl_pick_cert( ssl, suite_info, |
| 1044 | acceptable_ec_tls_ids, |
| 1045 | ec_tls_ids_len ) != 0 ) |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 1046 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1047 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: " |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 1048 | "no suitable certificate" ) ); |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 1049 | return( 0 ); |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 1050 | } |
Hanno Becker | d3b2fcb | 2019-06-17 14:30:05 +0100 | [diff] [blame] | 1051 | #else |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 1052 | ((void) acceptable_ec_tls_ids); |
| 1053 | ((void) ec_tls_ids_len); |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 1054 | #endif |
| 1055 | |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 1056 | return( 1 ); |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 1057 | } |
| 1058 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1059 | #if defined(MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO) |
| 1060 | static int ssl_parse_client_hello_v2( mbedtls_ssl_context *ssl ) |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1061 | { |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1062 | int ret, got_common_suite; |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1063 | unsigned int i, j; |
| 1064 | size_t n; |
| 1065 | unsigned int ciph_len, sess_len, chal_len; |
| 1066 | unsigned char *buf, *p; |
Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 1067 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 1068 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info; |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 1069 | #endif |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1070 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1071 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1072 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1073 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1074 | if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ) |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1075 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1076 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 1077 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 1078 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1079 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1080 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1081 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1082 | |
| 1083 | buf = ssl->in_hdr; |
| 1084 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1085 | MBEDTLS_SSL_DEBUG_BUF( 4, "record header", buf, 5 ); |
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 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d", |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1088 | buf[2] ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1089 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d", |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1090 | ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1091 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]", |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1092 | buf[3], buf[4] ) ); |
| 1093 | |
| 1094 | /* |
| 1095 | * SSLv2 Client Hello |
| 1096 | * |
| 1097 | * Record layer: |
| 1098 | * 0 . 1 message length |
| 1099 | * |
| 1100 | * SSL layer: |
| 1101 | * 2 . 2 message type |
| 1102 | * 3 . 4 protocol version |
| 1103 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1104 | if( buf[2] != MBEDTLS_SSL_HS_CLIENT_HELLO || |
| 1105 | buf[3] != MBEDTLS_SSL_MAJOR_VERSION_3 ) |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1106 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1107 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1108 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1109 | } |
| 1110 | |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 1111 | n = mbedtls_platform_get_uint16_be( buf ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1112 | |
| 1113 | if( n < 17 || n > 512 ) |
| 1114 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1115 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1116 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1117 | } |
| 1118 | |
Hanno Becker | 93de296 | 2019-07-25 12:38:18 +0100 | [diff] [blame] | 1119 | #if !defined(MBEDTLS_SSL_CONF_FIXED_MAJOR_VER) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1120 | ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3; |
Hanno Becker | 93de296 | 2019-07-25 12:38:18 +0100 | [diff] [blame] | 1121 | #endif |
| 1122 | |
| 1123 | #if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER) |
Hanno Becker | e965bd3 | 2019-06-12 14:04:34 +0100 | [diff] [blame] | 1124 | ssl->minor_ver = |
| 1125 | ( buf[4] <= mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) ) |
| 1126 | ? buf[4] : mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ); |
Hanno Becker | 93de296 | 2019-07-25 12:38:18 +0100 | [diff] [blame] | 1127 | #endif |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1128 | |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 1129 | if( mbedtls_ssl_get_minor_ver( ssl ) < mbedtls_ssl_conf_get_min_minor_ver( ssl->conf ) ) |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1130 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1131 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum" |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 1132 | " [%d:%d] < [%d:%d]", |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 1133 | mbedtls_ssl_get_major_ver( ssl ), |
| 1134 | mbedtls_ssl_get_minor_ver( ssl ), |
Hanno Becker | e965bd3 | 2019-06-12 14:04:34 +0100 | [diff] [blame] | 1135 | mbedtls_ssl_conf_get_min_major_ver( ssl->conf ), |
| 1136 | mbedtls_ssl_conf_get_min_minor_ver( ssl->conf ) ) ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1137 | |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 1138 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 1139 | MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1140 | return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1141 | } |
| 1142 | |
Hanno Becker | 18729ae | 2019-06-12 14:47:21 +0100 | [diff] [blame] | 1143 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \ |
| 1144 | defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
Paul Bakker | 2fbefde | 2013-06-29 16:01:15 +0200 | [diff] [blame] | 1145 | ssl->handshake->max_major_ver = buf[3]; |
| 1146 | ssl->handshake->max_minor_ver = buf[4]; |
Hanno Becker | 18729ae | 2019-06-12 14:47:21 +0100 | [diff] [blame] | 1147 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED || |
| 1148 | MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1149 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1150 | if( ( ret = mbedtls_ssl_fetch_input( ssl, 2 + n ) ) != 0 ) |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1151 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1152 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1153 | return( ret ); |
| 1154 | } |
| 1155 | |
Hanno Becker | 8a4b590 | 2019-08-15 17:04:57 +0100 | [diff] [blame] | 1156 | mbedtls_ssl_update_checksum( ssl, buf + 2, n ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1157 | |
| 1158 | buf = ssl->in_msg; |
| 1159 | n = ssl->in_left - 5; |
| 1160 | |
| 1161 | /* |
| 1162 | * 0 . 1 ciphersuitelist length |
| 1163 | * 2 . 3 session id length |
| 1164 | * 4 . 5 challenge length |
| 1165 | * 6 . .. ciphersuitelist |
| 1166 | * .. . .. session id |
| 1167 | * .. . .. challenge |
| 1168 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1169 | MBEDTLS_SSL_DEBUG_BUF( 4, "record contents", buf, n ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1170 | |
Arto Kinnunen | 4f4849a | 2019-09-09 10:21:18 +0300 | [diff] [blame] | 1171 | ciph_len = (unsigned int)mbedtls_platform_get_uint16_be( &buf[0] ); |
| 1172 | sess_len = (unsigned int)mbedtls_platform_get_uint16_be( &buf[2] ); |
| 1173 | chal_len = (unsigned int)mbedtls_platform_get_uint16_be( &buf[4] ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1174 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1175 | 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] | 1176 | ciph_len, sess_len, chal_len ) ); |
| 1177 | |
| 1178 | /* |
| 1179 | * Make sure each parameter length is valid |
| 1180 | */ |
| 1181 | if( ciph_len < 3 || ( ciph_len % 3 ) != 0 ) |
| 1182 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1183 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1184 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1185 | } |
| 1186 | |
| 1187 | if( sess_len > 32 ) |
| 1188 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1189 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1190 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1191 | } |
| 1192 | |
| 1193 | if( chal_len < 8 || chal_len > 32 ) |
| 1194 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1195 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1196 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | if( n != 6 + ciph_len + sess_len + chal_len ) |
| 1200 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1201 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1202 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1203 | } |
| 1204 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1205 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist", |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1206 | buf + 6, ciph_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1207 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id", |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1208 | buf + 6 + ciph_len, sess_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1209 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, challenge", |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1210 | buf + 6 + ciph_len + sess_len, chal_len ); |
| 1211 | |
| 1212 | p = buf + 6 + ciph_len; |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 1213 | ssl->session_negotiate->id_len = sess_len; |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 1214 | memset( ssl->session_negotiate->id, 0, |
| 1215 | sizeof( ssl->session_negotiate->id ) ); |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 1216 | memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->id_len ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1217 | |
| 1218 | p += sess_len; |
| 1219 | memset( ssl->handshake->randbytes, 0, 64 ); |
| 1220 | memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len ); |
| 1221 | |
| 1222 | /* |
| 1223 | * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV |
| 1224 | */ |
| 1225 | for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 ) |
| 1226 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1227 | 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] | 1228 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1229 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) ); |
| 1230 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1231 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1232 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1233 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV " |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1234 | "during renegotiation" ) ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1235 | |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 1236 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 1237 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1238 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1239 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1240 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
| 1241 | ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION; |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1242 | break; |
| 1243 | } |
| 1244 | } |
| 1245 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1246 | #if defined(MBEDTLS_SSL_FALLBACK_SCSV) |
Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1247 | for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 ) |
| 1248 | { |
| 1249 | if( p[0] == 0 && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1250 | p[1] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE >> 8 ) & 0xff ) && |
| 1251 | p[2] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE ) & 0xff ) ) |
Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1252 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1253 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "received FALLBACK_SCSV" ) ); |
Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1254 | |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 1255 | if( mbedtls_ssl_get_minor_ver( ssl ) < |
Hanno Becker | e965bd3 | 2019-06-12 14:04:34 +0100 | [diff] [blame] | 1256 | mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) ) |
Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1257 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1258 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) ); |
Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1259 | |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 1260 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 1261 | MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK ); |
Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1262 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1263 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1264 | } |
| 1265 | |
| 1266 | break; |
| 1267 | } |
| 1268 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1269 | #endif /* MBEDTLS_SSL_FALLBACK_SCSV */ |
Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1270 | |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1271 | got_common_suite = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1272 | #if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE) |
Manuel Pégourié-Gonnard | 1a9f2c7 | 2013-11-30 18:30:06 +0100 | [diff] [blame] | 1273 | 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] | 1274 | { |
Hanno Becker | 981f81d | 2019-07-19 16:10:49 +0100 | [diff] [blame] | 1275 | MBEDTLS_SSL_BEGIN_FOR_EACH_CIPHERSUITE( ssl, \ |
| 1276 | mbedtls_ssl_get_minor_ver( ssl ), \ |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 1277 | cur_info ) |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1278 | { |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 1279 | #else |
Hanno Becker | 981f81d | 2019-07-19 16:10:49 +0100 | [diff] [blame] | 1280 | MBEDTLS_SSL_BEGIN_FOR_EACH_CIPHERSUITE( ssl, \ |
| 1281 | mbedtls_ssl_get_minor_ver( ssl ), \ |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 1282 | cur_info ) |
| 1283 | { |
| 1284 | for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 ) |
| 1285 | { |
| 1286 | #endif |
| 1287 | const int ciphersuite_id = |
| 1288 | mbedtls_ssl_suite_get_id( cur_info ); |
| 1289 | |
Manuel Pégourié-Gonnard | 011a8db | 2013-11-30 18:11:07 +0100 | [diff] [blame] | 1290 | if( p[0] != 0 || |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 1291 | p[1] != ( ( ciphersuite_id >> 8 ) & 0xFF ) || |
| 1292 | p[2] != ( ( ciphersuite_id ) & 0xFF ) ) |
| 1293 | { |
Hanno Becker | f4d6b49 | 2019-07-02 17:13:14 +0100 | [diff] [blame] | 1294 | continue; |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 1295 | } |
Paul Bakker | 59c28a2 | 2013-06-29 15:33:42 +0200 | [diff] [blame] | 1296 | |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1297 | got_common_suite = 1; |
| 1298 | |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 1299 | if( ssl_ciphersuite_is_match( ssl, cur_info, NULL, 0 ) ) |
Hanno Becker | d3b2fcb | 2019-06-17 14:30:05 +0100 | [diff] [blame] | 1300 | { |
Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 1301 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 1302 | ciphersuite_info = cur_info; |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 1303 | #endif |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 1304 | goto have_ciphersuite_v2; |
Hanno Becker | d3b2fcb | 2019-06-17 14:30:05 +0100 | [diff] [blame] | 1305 | } |
Paul Bakker | 59c28a2 | 2013-06-29 15:33:42 +0200 | [diff] [blame] | 1306 | |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 1307 | #if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE) |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1308 | } |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 1309 | MBEDTLS_SSL_END_FOR_EACH_CIPHERSUITE |
| 1310 | } |
| 1311 | #else |
| 1312 | } |
| 1313 | } |
| 1314 | MBEDTLS_SSL_END_FOR_EACH_CIPHERSUITE |
| 1315 | #endif |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1316 | |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1317 | if( got_common_suite ) |
| 1318 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1319 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, " |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1320 | "but none of them usable" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1321 | return( MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE ); |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1322 | } |
| 1323 | else |
| 1324 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1325 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) ); |
| 1326 | return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN ); |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1327 | } |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1328 | |
| 1329 | have_ciphersuite_v2: |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 1330 | |
Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 1331 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 1332 | ssl->session_negotiate->ciphersuite = |
| 1333 | mbedtls_ssl_suite_get_id( ciphersuite_info ); |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1334 | ssl->handshake->ciphersuite_info = ciphersuite_info; |
Hanno Becker | df64596 | 2019-06-26 13:02:22 +0100 | [diff] [blame] | 1335 | #endif |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1336 | |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 1337 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", |
| 1338 | mbedtls_ssl_get_ciphersuite_name( |
| 1339 | mbedtls_ssl_session_get_ciphersuite( ssl->session_negotiate ) ) ) ); |
| 1340 | |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1341 | /* |
| 1342 | * SSLv2 Client Hello relevant renegotiation security checks |
| 1343 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1344 | if( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && |
Hanno Becker | b0b2b67 | 2019-06-12 16:58:10 +0100 | [diff] [blame] | 1345 | mbedtls_ssl_conf_get_allow_legacy_renegotiation( ssl->conf ) == |
| 1346 | MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE ) |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1347 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1348 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 1349 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 1350 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1351 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1352 | } |
| 1353 | |
| 1354 | ssl->in_left = 0; |
| 1355 | ssl->state++; |
| 1356 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1357 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1358 | |
| 1359 | return( 0 ); |
| 1360 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1361 | #endif /* MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */ |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1362 | |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1363 | /* This function doesn't alert on errors that happen early during |
| 1364 | ClientHello parsing because they might indicate that the client is |
| 1365 | 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] | 1366 | static int ssl_parse_client_hello( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1367 | { |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1368 | int ret, got_common_suite; |
Manuel Pégourié-Gonnard | 9de64f5 | 2015-07-01 15:51:43 +0200 | [diff] [blame] | 1369 | size_t i, j; |
| 1370 | size_t ciph_offset, comp_offset, ext_offset; |
| 1371 | 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] | 1372 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 9de64f5 | 2015-07-01 15:51:43 +0200 | [diff] [blame] | 1373 | size_t cookie_offset, cookie_len; |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1374 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1375 | unsigned char *buf, *p, *ext; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1376 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1377 | int renegotiation_info_seen = 0; |
Manuel Pégourié-Gonnard | eaecbd3 | 2014-11-06 02:38:02 +0100 | [diff] [blame] | 1378 | #endif |
Hanno Becker | 03b64fa | 2019-06-11 14:39:38 +0100 | [diff] [blame] | 1379 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 1380 | int extended_ms_seen = 0; |
| 1381 | #endif |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1382 | int handshake_failure = 0; |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 1383 | |
Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 1384 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 1385 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info; |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 1386 | #endif |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 1387 | int major, minor; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1388 | |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 1389 | unsigned char const *acceptable_ec_tls_ids = NULL; |
| 1390 | size_t ec_tls_ids_len = 0; |
Hanno Becker | d3b2fcb | 2019-06-17 14:30:05 +0100 | [diff] [blame] | 1391 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 1392 | /* If there is no signature-algorithm extension present, |
| 1393 | * we need to fall back to the default values for allowed |
| 1394 | * signature-hash pairs. */ |
| 1395 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 1396 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
| 1397 | int sig_hash_alg_ext_present = 0; |
| 1398 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && |
| 1399 | MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
| 1400 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1401 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1402 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1403 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | f03c7aa | 2014-09-24 14:54:06 +0200 | [diff] [blame] | 1404 | read_record_header: |
| 1405 | #endif |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1406 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1407 | * 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] | 1408 | * otherwise read it ourselves manually in order to support SSLv2 |
| 1409 | * ClientHello, which doesn't use the same record layer format. |
| 1410 | */ |
Manuel Pégourié-Gonnard | 754b9f3 | 2019-07-01 12:20:54 +0200 | [diff] [blame] | 1411 | if( mbedtls_ssl_get_renego_status( ssl ) == MBEDTLS_SSL_INITIAL_HANDSHAKE && |
| 1412 | ( ret = mbedtls_ssl_fetch_input( ssl, 5 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1413 | { |
Manuel Pégourié-Gonnard | 754b9f3 | 2019-07-01 12:20:54 +0200 | [diff] [blame] | 1414 | /* No alert on a read error. */ |
| 1415 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret ); |
| 1416 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1417 | } |
| 1418 | |
| 1419 | buf = ssl->in_hdr; |
| 1420 | |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 1421 | #if defined(MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO) && \ |
| 1422 | defined(MBEDTLS_SSL_PROTO_TLS) |
| 1423 | if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) && |
| 1424 | ( buf[0] & 0x80 ) != 0 ) |
| 1425 | { |
| 1426 | return( ssl_parse_client_hello_v2( ssl ) ); |
| 1427 | } |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1428 | #endif |
| 1429 | |
Hanno Becker | 4339576 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 1430 | 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] | 1431 | |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1432 | /* |
Manuel Pégourié-Gonnard | 6b1e207 | 2014-02-12 10:14:54 +0100 | [diff] [blame] | 1433 | * SSLv3/TLS Client Hello |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1434 | * |
| 1435 | * Record layer: |
| 1436 | * 0 . 0 message type |
| 1437 | * 1 . 2 protocol version |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1438 | * 3 . 11 DTLS: epoch + record sequence number |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1439 | * 3 . 4 message length |
| 1440 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1441 | 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] | 1442 | buf[0] ) ); |
| 1443 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1444 | if( buf[0] != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 1445 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1446 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1447 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 1448 | } |
| 1449 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1450 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d", |
Arto Kinnunen | a3fa06e | 2019-09-09 12:22:51 +0300 | [diff] [blame] | 1451 | (int)mbedtls_platform_get_uint16_be( ssl->in_len ) ) ); |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1452 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1453 | 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] | 1454 | buf[1], buf[2] ) ); |
| 1455 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1456 | 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] | 1457 | |
| 1458 | /* According to RFC 5246 Appendix E.1, the version here is typically |
| 1459 | * "{03,00}, the lowest version number supported by the client, [or] the |
| 1460 | * value of ClientHello.client_version", so the only meaningful check here |
| 1461 | * is the major version shouldn't be less than 3 */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1462 | if( major < MBEDTLS_SSL_MAJOR_VERSION_3 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1463 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1464 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1465 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1466 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1467 | |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1468 | /* For DTLS if this is the initial handshake, remember the client sequence |
| 1469 | * 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] | 1470 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 754b9f3 | 2019-07-01 12:20:54 +0200 | [diff] [blame] | 1471 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
| 1472 | mbedtls_ssl_get_renego_status( ssl ) == MBEDTLS_SSL_INITIAL_HANDSHAKE ) |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1473 | { |
| 1474 | /* Epoch should be 0 for initial handshakes */ |
| 1475 | if( ssl->in_ctr[0] != 0 || ssl->in_ctr[1] != 0 ) |
| 1476 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1477 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1478 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1479 | } |
| 1480 | |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 1481 | 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] | 1482 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1483 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 1484 | if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 ) |
Manuel Pégourié-Gonnard | f03c7aa | 2014-09-24 14:54:06 +0200 | [diff] [blame] | 1485 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1486 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record, discarding" ) ); |
Manuel Pégourié-Gonnard | f03c7aa | 2014-09-24 14:54:06 +0200 | [diff] [blame] | 1487 | ssl->next_record_offset = 0; |
| 1488 | ssl->in_left = 0; |
| 1489 | goto read_record_header; |
| 1490 | } |
| 1491 | |
| 1492 | /* 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] | 1493 | mbedtls_ssl_dtls_replay_update( ssl ); |
Manuel Pégourié-Gonnard | f03c7aa | 2014-09-24 14:54:06 +0200 | [diff] [blame] | 1494 | #endif |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1495 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1496 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1497 | |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 1498 | msg_len = mbedtls_platform_get_uint16_be( ssl->in_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1499 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1500 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1501 | if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ) |
Manuel Pégourié-Gonnard | b89c4f3 | 2015-01-21 13:24:10 +0000 | [diff] [blame] | 1502 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1503 | /* Set by mbedtls_ssl_read_record() */ |
Manuel Pégourié-Gonnard | b89c4f3 | 2015-01-21 13:24:10 +0000 | [diff] [blame] | 1504 | msg_len = ssl->in_hslen; |
| 1505 | } |
| 1506 | else |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1507 | #endif |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1508 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1509 | if( msg_len > MBEDTLS_SSL_IN_CONTENT_LEN ) |
Manuel Pégourié-Gonnard | d6b721c | 2014-03-24 12:13:54 +0100 | [diff] [blame] | 1510 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1511 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1512 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | d6b721c | 2014-03-24 12:13:54 +0100 | [diff] [blame] | 1513 | } |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1514 | |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 1515 | if( ( ret = mbedtls_ssl_fetch_input( ssl, |
Hanno Becker | 4339576 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 1516 | mbedtls_ssl_in_hdr_len( ssl ) + msg_len ) ) != 0 ) |
Manuel Pégourié-Gonnard | d6b721c | 2014-03-24 12:13:54 +0100 | [diff] [blame] | 1517 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1518 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret ); |
Manuel Pégourié-Gonnard | d6b721c | 2014-03-24 12:13:54 +0100 | [diff] [blame] | 1519 | return( ret ); |
| 1520 | } |
Manuel Pégourié-Gonnard | 30d16eb | 2014-08-19 17:43:50 +0200 | [diff] [blame] | 1521 | |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 1522 | /* Done reading this record, get ready for the next one */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1523 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 1524 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
| 1525 | { |
Hanno Becker | 4339576 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 1526 | 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] | 1527 | } |
| 1528 | MBEDTLS_SSL_TRANSPORT_ELSE |
Manuel Pégourié-Gonnard | 30d16eb | 2014-08-19 17:43:50 +0200 | [diff] [blame] | 1529 | #endif |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 1530 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
| 1531 | { |
Manuel Pégourié-Gonnard | 30d16eb | 2014-08-19 17:43:50 +0200 | [diff] [blame] | 1532 | ssl->in_left = 0; |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 1533 | } |
| 1534 | #endif |
Manuel Pégourié-Gonnard | d6b721c | 2014-03-24 12:13:54 +0100 | [diff] [blame] | 1535 | } |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1536 | |
| 1537 | buf = ssl->in_msg; |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1538 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1539 | MBEDTLS_SSL_DEBUG_BUF( 4, "record contents", buf, msg_len ); |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 1540 | |
Hanno Becker | 8a4b590 | 2019-08-15 17:04:57 +0100 | [diff] [blame] | 1541 | mbedtls_ssl_update_checksum( ssl, buf, msg_len ); |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1542 | |
| 1543 | /* |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1544 | * Handshake layer: |
| 1545 | * 0 . 0 handshake type |
| 1546 | * 1 . 3 handshake length |
| 1547 | * 4 . 5 DTLS only: message seqence number |
| 1548 | * 6 . 8 DTLS only: fragment offset |
| 1549 | * 9 . 11 DTLS only: fragment length |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 1550 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1551 | if( msg_len < mbedtls_ssl_hs_hdr_len( ssl ) ) |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1552 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1553 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1554 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1555 | } |
| 1556 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1557 | 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] | 1558 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1559 | if( buf[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1560 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1561 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1562 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1563 | } |
| 1564 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1565 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d", |
Arto Kinnunen | a3fa06e | 2019-09-09 12:22:51 +0300 | [diff] [blame] | 1566 | (int)mbedtls_platform_get_uint24_be( &buf[1]) ) ); |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1567 | |
| 1568 | /* We don't support fragmentation of ClientHello (yet?) */ |
| 1569 | if( buf[1] != 0 || |
Arto Kinnunen | a3fa06e | 2019-09-09 12:22:51 +0300 | [diff] [blame] | 1570 | msg_len != ( mbedtls_ssl_hs_hdr_len( ssl ) + |
| 1571 | mbedtls_platform_get_uint16_be( &buf[2]) ) ) |
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 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1574 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1575 | } |
| 1576 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1577 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 1578 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 1579 | { |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1580 | /* |
Manuel Pégourié-Gonnard | 69849f8 | 2015-03-10 11:54:02 +0000 | [diff] [blame] | 1581 | * Copy the client's handshake message_seq on initial handshakes, |
| 1582 | * check sequence number on renego. |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1583 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1584 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1585 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 1586 | { |
| 1587 | /* This couldn't be done in ssl_prepare_handshake_record() */ |
Arto Kinnunen | 4f4849a | 2019-09-09 10:21:18 +0300 | [diff] [blame] | 1588 | unsigned int cli_msg_seq = (unsigned int) |
| 1589 | mbedtls_platform_get_uint16_be( &ssl->in_msg[4] ); |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1590 | |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 1591 | if( cli_msg_seq != ssl->handshake->in_msg_seq ) |
| 1592 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1593 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message_seq: " |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 1594 | "%d (expected %d)", cli_msg_seq, |
| 1595 | ssl->handshake->in_msg_seq ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1596 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 1597 | } |
| 1598 | |
| 1599 | ssl->handshake->in_msg_seq++; |
| 1600 | } |
Manuel Pégourié-Gonnard | 69849f8 | 2015-03-10 11:54:02 +0000 | [diff] [blame] | 1601 | else |
| 1602 | #endif |
| 1603 | { |
Arto Kinnunen | 4f4849a | 2019-09-09 10:21:18 +0300 | [diff] [blame] | 1604 | unsigned int cli_msg_seq = (unsigned int) |
| 1605 | mbedtls_platform_get_uint16_be( &ssl->in_msg[4] ); |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 1606 | |
Manuel Pégourié-Gonnard | 69849f8 | 2015-03-10 11:54:02 +0000 | [diff] [blame] | 1607 | ssl->handshake->out_msg_seq = cli_msg_seq; |
| 1608 | ssl->handshake->in_msg_seq = cli_msg_seq + 1; |
| 1609 | } |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 1610 | |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1611 | /* |
| 1612 | * For now we don't support fragmentation, so make sure |
| 1613 | * fragment_offset == 0 and fragment_length == length |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1614 | */ |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 1615 | if( ssl->in_msg[6] != 0 || ssl->in_msg[7] != 0 || ssl->in_msg[8] != 0 || |
| 1616 | memcmp( ssl->in_msg + 1, ssl->in_msg + 9, 3 ) != 0 ) |
| 1617 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1618 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "ClientHello fragmentation not supported" ) ); |
| 1619 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 1620 | } |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 1621 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1622 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 1623 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1624 | buf += mbedtls_ssl_hs_hdr_len( ssl ); |
| 1625 | msg_len -= mbedtls_ssl_hs_hdr_len( ssl ); |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1626 | |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 1627 | /* |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1628 | * ClientHello layer: |
| 1629 | * 0 . 1 protocol version |
| 1630 | * 2 . 33 random bytes (starting with 4 bytes of Unix time) |
| 1631 | * 34 . 35 session id length (1 byte) |
| 1632 | * 35 . 34+x session id |
| 1633 | * 35+x . 35+x DTLS only: cookie length (1 byte) |
| 1634 | * 36+x . .. DTLS only: cookie |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1635 | * .. . .. ciphersuite list length (2 bytes) |
| 1636 | * .. . .. ciphersuite list |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1637 | * .. . .. compression alg. list length (1 byte) |
| 1638 | * .. . .. compression alg. list |
| 1639 | * .. . .. extensions length (2 bytes, optional) |
| 1640 | * .. . .. extensions (optional) |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1641 | */ |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1642 | |
| 1643 | /* |
Antonin Décimo | d5f4759 | 2019-01-23 15:24:37 +0100 | [diff] [blame] | 1644 | * Minimal length (with everything empty and extensions omitted) is |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1645 | * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can |
| 1646 | * read at least up to session id length without worrying. |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1647 | */ |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1648 | if( msg_len < 38 ) |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1649 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1650 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1651 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1652 | } |
| 1653 | |
| 1654 | /* |
| 1655 | * Check and save the protocol version |
| 1656 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1657 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, version", buf, 2 ); |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1658 | |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 1659 | { |
Hanno Becker | 3fa1ee5 | 2019-05-22 14:44:37 +0100 | [diff] [blame] | 1660 | int minor_ver, major_ver; |
| 1661 | mbedtls_ssl_read_version( &major_ver, &minor_ver, |
| 1662 | ssl->conf->transport, |
| 1663 | buf ); |
| 1664 | |
Hanno Becker | 18729ae | 2019-06-12 14:47:21 +0100 | [diff] [blame] | 1665 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \ |
| 1666 | defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
Hanno Becker | 3fa1ee5 | 2019-05-22 14:44:37 +0100 | [diff] [blame] | 1667 | ssl->handshake->max_major_ver = major_ver; |
| 1668 | ssl->handshake->max_minor_ver = minor_ver; |
Hanno Becker | 18729ae | 2019-06-12 14:47:21 +0100 | [diff] [blame] | 1669 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED || |
| 1670 | MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
Hanno Becker | 3fa1ee5 | 2019-05-22 14:44:37 +0100 | [diff] [blame] | 1671 | |
| 1672 | if( major_ver < mbedtls_ssl_conf_get_min_major_ver( ssl->conf ) || |
| 1673 | minor_ver < mbedtls_ssl_conf_get_min_minor_ver( ssl->conf ) ) |
| 1674 | { |
| 1675 | 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] | 1676 | " [%d:%d] < [%d:%d]", |
Hanno Becker | 3fa1ee5 | 2019-05-22 14:44:37 +0100 | [diff] [blame] | 1677 | major_ver, minor_ver, |
Hanno Becker | e965bd3 | 2019-06-12 14:04:34 +0100 | [diff] [blame] | 1678 | mbedtls_ssl_conf_get_min_major_ver( ssl->conf ), |
| 1679 | mbedtls_ssl_conf_get_min_minor_ver( ssl->conf ) ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 1680 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 1681 | MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION ); |
Hanno Becker | 3fa1ee5 | 2019-05-22 14:44:37 +0100 | [diff] [blame] | 1682 | return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION ); |
| 1683 | } |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 1684 | |
Hanno Becker | 3fa1ee5 | 2019-05-22 14:44:37 +0100 | [diff] [blame] | 1685 | if( major_ver > mbedtls_ssl_conf_get_max_major_ver( ssl->conf ) ) |
| 1686 | { |
| 1687 | major_ver = mbedtls_ssl_conf_get_max_major_ver( ssl->conf ); |
| 1688 | minor_ver = mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ); |
| 1689 | } |
| 1690 | else if( minor_ver > mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) ) |
| 1691 | minor_ver = mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ); |
| 1692 | |
Hanno Becker | 381eaa5 | 2019-06-12 14:43:01 +0100 | [diff] [blame] | 1693 | #if !defined(MBEDTLS_SSL_CONF_FIXED_MAJOR_VER) |
Hanno Becker | 3fa1ee5 | 2019-05-22 14:44:37 +0100 | [diff] [blame] | 1694 | ssl->major_ver = major_ver; |
Hanno Becker | 381eaa5 | 2019-06-12 14:43:01 +0100 | [diff] [blame] | 1695 | #endif /* MBEDTLS_SSL_CONF_FIXED_MAJOR_VER */ |
| 1696 | #if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER) |
Hanno Becker | 3fa1ee5 | 2019-05-22 14:44:37 +0100 | [diff] [blame] | 1697 | ssl->minor_ver = minor_ver; |
Hanno Becker | 381eaa5 | 2019-06-12 14:43:01 +0100 | [diff] [blame] | 1698 | #endif /* MBEDTLS_SSL_CONF_FIXED_MINOR_VER */ |
Manuel Pégourié-Gonnard | 6b1e207 | 2014-02-12 10:14:54 +0100 | [diff] [blame] | 1699 | } |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1700 | |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1701 | /* |
| 1702 | * Save client random (inc. Unix time) |
| 1703 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1704 | 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] | 1705 | |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1706 | memcpy( ssl->handshake->randbytes, buf + 2, 32 ); |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1707 | |
| 1708 | /* |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1709 | * Check the session ID length and save session ID |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1710 | */ |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1711 | sess_len = buf[34]; |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1712 | |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1713 | if( sess_len > sizeof( ssl->session_negotiate->id ) || |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1714 | sess_len + 34 + 2 > msg_len ) /* 2 for cipherlist length field */ |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1715 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1716 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 1717 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 1718 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1719 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1720 | } |
| 1721 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1722 | 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] | 1723 | |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 1724 | ssl->session_negotiate->id_len = sess_len; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1725 | memset( ssl->session_negotiate->id, 0, |
| 1726 | sizeof( ssl->session_negotiate->id ) ); |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1727 | memcpy( ssl->session_negotiate->id, buf + 35, |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 1728 | ssl->session_negotiate->id_len ); |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1729 | |
| 1730 | /* |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1731 | * Check the cookie length and content |
| 1732 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1733 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 1734 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1735 | { |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1736 | cookie_offset = 35 + sess_len; |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1737 | cookie_len = buf[cookie_offset]; |
| 1738 | |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1739 | if( cookie_offset + 1 + cookie_len + 2 > msg_len ) |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1740 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1741 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 1742 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 1743 | MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1744 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1745 | } |
| 1746 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1747 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie", |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1748 | buf + cookie_offset + 1, cookie_len ); |
| 1749 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1750 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) |
Manuel Pégourié-Gonnard | 754b9f3 | 2019-07-01 12:20:54 +0200 | [diff] [blame] | 1751 | if( ssl->conf->f_cookie_check != NULL && |
| 1752 | mbedtls_ssl_get_renego_status( ssl ) == MBEDTLS_SSL_INITIAL_HANDSHAKE ) |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1753 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1754 | if( ssl->conf->f_cookie_check( ssl->conf->p_cookie, |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1755 | buf + cookie_offset + 1, cookie_len, |
| 1756 | ssl->cli_id, ssl->cli_id_len ) != 0 ) |
| 1757 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1758 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification failed" ) ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1759 | ssl->handshake->verify_cookie_len = 1; |
| 1760 | } |
| 1761 | else |
| 1762 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1763 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification passed" ) ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1764 | ssl->handshake->verify_cookie_len = 0; |
| 1765 | } |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1766 | } |
Manuel Pégourié-Gonnard | d7f9bc5 | 2014-07-23 11:09:27 +0200 | [diff] [blame] | 1767 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1768 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */ |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1769 | { |
| 1770 | /* We know we didn't send a cookie, so it should be empty */ |
| 1771 | if( cookie_len != 0 ) |
| 1772 | { |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1773 | /* 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] | 1774 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1775 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1776 | } |
| 1777 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1778 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification skipped" ) ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1779 | } |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1780 | |
| 1781 | /* |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1782 | * Check the ciphersuitelist length (will be parsed later) |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1783 | */ |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1784 | ciph_offset = cookie_offset + 1 + cookie_len; |
Manuel Pégourié-Gonnard | a06d7fe | 2015-03-13 10:36:55 +0000 | [diff] [blame] | 1785 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 1786 | MBEDTLS_SSL_TRANSPORT_ELSE |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1787 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 1788 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
| 1789 | { |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1790 | ciph_offset = 35 + sess_len; |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 1791 | } |
Manuel Pégourié-Gonnard | ec1c222 | 2019-06-12 10:18:26 +0200 | [diff] [blame] | 1792 | #endif /* MBEDTLS_SSL_PROTO_TLS */ |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1793 | |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 1794 | |
| 1795 | 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] | 1796 | |
| 1797 | if( ciph_len < 2 || |
| 1798 | ciph_len + 2 + ciph_offset + 1 > msg_len || /* 1 for comp. alg. len */ |
| 1799 | ( ciph_len % 2 ) != 0 ) |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1800 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1801 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 1802 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 1803 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1804 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1805 | } |
| 1806 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1807 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist", |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1808 | buf + ciph_offset + 2, ciph_len ); |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1809 | |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1810 | /* |
| 1811 | * Check the compression algorithms length and pick one |
| 1812 | */ |
| 1813 | comp_offset = ciph_offset + 2 + ciph_len; |
| 1814 | |
| 1815 | comp_len = buf[comp_offset]; |
| 1816 | |
| 1817 | if( comp_len < 1 || |
| 1818 | comp_len > 16 || |
| 1819 | comp_len + comp_offset + 1 > msg_len ) |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1820 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1821 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 1822 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 1823 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1824 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1825 | } |
| 1826 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1827 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, compression", |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1828 | buf + comp_offset + 1, comp_len ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1829 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1830 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Hanno Becker | 8844055 | 2019-07-03 14:16:13 +0100 | [diff] [blame] | 1831 | ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL; |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1832 | for( i = 0; i < comp_len; ++i ) |
| 1833 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1834 | if( buf[comp_offset + 1 + i] == MBEDTLS_SSL_COMPRESS_DEFLATE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1835 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1836 | ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_DEFLATE; |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1837 | break; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1838 | } |
| 1839 | } |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1840 | |
Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1841 | /* See comments in ssl_write_client_hello() */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1842 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 1843 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1844 | ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL; |
Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1845 | #endif |
Hanno Becker | 8844055 | 2019-07-03 14:16:13 +0100 | [diff] [blame] | 1846 | #endif /* MBEDTLS_ZLIB_SUPPORT */ |
Manuel Pégourié-Gonnard | 82202f0 | 2014-07-23 00:28:58 +0200 | [diff] [blame] | 1847 | |
Janos Follath | c6dab2b | 2016-05-23 14:27:02 +0100 | [diff] [blame] | 1848 | /* Do not parse the extensions if the protocol is SSLv3 */ |
| 1849 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 1850 | if( ( mbedtls_ssl_get_major_ver( ssl ) != 3 ) || |
| 1851 | ( mbedtls_ssl_get_minor_ver( ssl ) != 0 ) ) |
Janos Follath | c6dab2b | 2016-05-23 14:27:02 +0100 | [diff] [blame] | 1852 | { |
| 1853 | #endif |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1854 | /* |
| 1855 | * Check the extension length |
| 1856 | */ |
| 1857 | ext_offset = comp_offset + 1 + comp_len; |
| 1858 | if( msg_len > ext_offset ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1859 | { |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1860 | if( msg_len < ext_offset + 2 ) |
| 1861 | { |
| 1862 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 1863 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 1864 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1865 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 1866 | } |
| 1867 | |
Arto Kinnunen | a3fa06e | 2019-09-09 12:22:51 +0300 | [diff] [blame] | 1868 | ext_len = mbedtls_platform_get_uint16_be( &buf[ext_offset + 0] ); |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1869 | |
| 1870 | if( ( ext_len > 0 && ext_len < 4 ) || |
| 1871 | msg_len != ext_offset + 2 + ext_len ) |
| 1872 | { |
| 1873 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 1874 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 1875 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1876 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 1877 | } |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1878 | } |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1879 | else |
| 1880 | ext_len = 0; |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1881 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1882 | ext = buf + ext_offset + 2; |
| 1883 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", ext, ext_len ); |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1884 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1885 | while( ext_len != 0 ) |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1886 | { |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 1887 | unsigned int ext_id; |
| 1888 | unsigned int ext_size; |
| 1889 | if ( ext_len < 4 ) { |
| 1890 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 1891 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 1892 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 1893 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 1894 | } |
Arto Kinnunen | a3fa06e | 2019-09-09 12:22:51 +0300 | [diff] [blame] | 1895 | ext_id = (unsigned int)mbedtls_platform_get_uint16_be( ext ); |
| 1896 | ext_size = (unsigned int)mbedtls_platform_get_uint16_be( &ext[2] ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1897 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1898 | if( ext_size + 4 > 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 | } |
| 1905 | switch( ext_id ) |
| 1906 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1907 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1908 | case MBEDTLS_TLS_EXT_SERVERNAME: |
| 1909 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) ); |
| 1910 | if( ssl->conf->f_sni == NULL ) |
| 1911 | break; |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 1912 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1913 | ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size ); |
| 1914 | if( ret != 0 ) |
| 1915 | return( ret ); |
| 1916 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1917 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 1918 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1919 | case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO: |
| 1920 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1921 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1922 | renegotiation_info_seen = 1; |
Manuel Pégourié-Gonnard | eaecbd3 | 2014-11-06 02:38:02 +0100 | [diff] [blame] | 1923 | #endif |
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 | ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size ); |
| 1926 | if( ret != 0 ) |
| 1927 | return( ret ); |
| 1928 | break; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1929 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1930 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 1931 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1932 | case MBEDTLS_TLS_EXT_SIG_ALG: |
Ron Eldor | 73a3817 | 2017-10-03 15:58:26 +0300 | [diff] [blame] | 1933 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) ); |
| 1934 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1935 | ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size ); |
| 1936 | if( ret != 0 ) |
| 1937 | return( ret ); |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 1938 | |
| 1939 | sig_hash_alg_ext_present = 1; |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1940 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1941 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && |
| 1942 | MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1943 | |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 1944 | #if defined(MBEDTLS_ECDH_C) || \ |
| 1945 | defined(MBEDTLS_ECDSA_C) || \ |
| 1946 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) || \ |
| 1947 | defined(MBEDTLS_USE_TINYCRYPT) |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1948 | case MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES: |
| 1949 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1950 | |
Hanno Becker | d3b2fcb | 2019-06-17 14:30:05 +0100 | [diff] [blame] | 1951 | ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, |
| 1952 | ext_size, |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 1953 | &acceptable_ec_tls_ids, |
| 1954 | &ec_tls_ids_len ); |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1955 | if( ret != 0 ) |
| 1956 | return( ret ); |
| 1957 | break; |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1958 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1959 | case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS: |
| 1960 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) ); |
| 1961 | ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT; |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1962 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1963 | ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size ); |
| 1964 | if( ret != 0 ) |
| 1965 | return( ret ); |
| 1966 | break; |
Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 1967 | #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 1968 | MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED || |
| 1969 | MBEDTLS_USE_TINYCRYPT */ |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1970 | |
Manuel Pégourié-Gonnard | bf57be6 | 2015-09-16 15:04:01 +0200 | [diff] [blame] | 1971 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1972 | case MBEDTLS_TLS_EXT_ECJPAKE_KKPP: |
| 1973 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake kkpp extension" ) ); |
Manuel Pégourié-Gonnard | bf57be6 | 2015-09-16 15:04:01 +0200 | [diff] [blame] | 1974 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1975 | ret = ssl_parse_ecjpake_kkpp( ssl, ext + 4, ext_size ); |
| 1976 | if( ret != 0 ) |
| 1977 | return( ret ); |
| 1978 | break; |
Manuel Pégourié-Gonnard | bf57be6 | 2015-09-16 15:04:01 +0200 | [diff] [blame] | 1979 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
| 1980 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1981 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1982 | case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH: |
| 1983 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) ); |
Manuel Pégourié-Gonnard | 48f8d0d | 2013-07-17 10:25:37 +0200 | [diff] [blame] | 1984 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1985 | ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size ); |
| 1986 | if( ret != 0 ) |
| 1987 | return( ret ); |
| 1988 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1989 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 48f8d0d | 2013-07-17 10:25:37 +0200 | [diff] [blame] | 1990 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1991 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1992 | case MBEDTLS_TLS_EXT_TRUNCATED_HMAC: |
| 1993 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) ); |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 1994 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1995 | ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size ); |
| 1996 | if( ret != 0 ) |
| 1997 | return( ret ); |
| 1998 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1999 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 2000 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2001 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | c403b26 | 2019-04-26 13:56:39 +0100 | [diff] [blame] | 2002 | case MBEDTLS_TLS_EXT_CID: |
| 2003 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found CID extension" ) ); |
| 2004 | |
| 2005 | ret = ssl_parse_cid_ext( ssl, ext + 4, ext_size ); |
| 2006 | if( ret != 0 ) |
| 2007 | return( ret ); |
| 2008 | break; |
| 2009 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
| 2010 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2011 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2012 | case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC: |
| 2013 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) ); |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2014 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2015 | ret = ssl_parse_encrypt_then_mac_ext( ssl, ext + 4, ext_size ); |
| 2016 | if( ret != 0 ) |
| 2017 | return( ret ); |
| 2018 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2019 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2020 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2021 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2022 | case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET: |
| 2023 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) ); |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2024 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2025 | ret = ssl_parse_extended_ms_ext( ssl, ext + 4, ext_size ); |
| 2026 | if( ret != 0 ) |
| 2027 | return( ret ); |
Hanno Becker | 03b64fa | 2019-06-11 14:39:38 +0100 | [diff] [blame] | 2028 | extended_ms_seen = 1; |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2029 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2030 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2031 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2032 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2033 | case MBEDTLS_TLS_EXT_SESSION_TICKET: |
| 2034 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) ); |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 2035 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2036 | ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size ); |
| 2037 | if( ret != 0 ) |
| 2038 | return( ret ); |
| 2039 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2040 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 2041 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2042 | #if defined(MBEDTLS_SSL_ALPN) |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2043 | case MBEDTLS_TLS_EXT_ALPN: |
| 2044 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) ); |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 2045 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2046 | ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ); |
| 2047 | if( ret != 0 ) |
| 2048 | return( ret ); |
| 2049 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2050 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 2051 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2052 | default: |
| 2053 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)", |
| 2054 | ext_id ) ); |
| 2055 | } |
| 2056 | |
| 2057 | ext_len -= 4 + ext_size; |
| 2058 | ext += 4 + ext_size; |
| 2059 | |
| 2060 | if( ext_len > 0 && ext_len < 4 ) |
| 2061 | { |
| 2062 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 2063 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 2064 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2065 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 2066 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2067 | } |
Janos Follath | c6dab2b | 2016-05-23 14:27:02 +0100 | [diff] [blame] | 2068 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 2069 | } |
| 2070 | #endif |
| 2071 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2072 | #if defined(MBEDTLS_SSL_FALLBACK_SCSV) |
Gilles Peskine | d50177f | 2017-05-16 17:53:03 +0200 | [diff] [blame] | 2073 | 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] | 2074 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2075 | if( p[0] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE >> 8 ) & 0xff ) && |
| 2076 | p[1] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE ) & 0xff ) ) |
Manuel Pégourié-Gonnard | fedba98 | 2014-11-05 16:12:09 +0100 | [diff] [blame] | 2077 | { |
Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 2078 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "received FALLBACK_SCSV" ) ); |
Manuel Pégourié-Gonnard | fedba98 | 2014-11-05 16:12:09 +0100 | [diff] [blame] | 2079 | |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 2080 | if( mbedtls_ssl_get_minor_ver( ssl ) < |
Hanno Becker | e965bd3 | 2019-06-12 14:04:34 +0100 | [diff] [blame] | 2081 | mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) ) |
Manuel Pégourié-Gonnard | fedba98 | 2014-11-05 16:12:09 +0100 | [diff] [blame] | 2082 | { |
Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 2083 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) ); |
Manuel Pégourié-Gonnard | fedba98 | 2014-11-05 16:12:09 +0100 | [diff] [blame] | 2084 | |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 2085 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 2086 | MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK ); |
Manuel Pégourié-Gonnard | fedba98 | 2014-11-05 16:12:09 +0100 | [diff] [blame] | 2087 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2088 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | fedba98 | 2014-11-05 16:12:09 +0100 | [diff] [blame] | 2089 | } |
| 2090 | |
| 2091 | break; |
| 2092 | } |
| 2093 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2094 | #endif /* MBEDTLS_SSL_FALLBACK_SCSV */ |
Manuel Pégourié-Gonnard | fedba98 | 2014-11-05 16:12:09 +0100 | [diff] [blame] | 2095 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 2096 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 2097 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
| 2098 | |
| 2099 | /* |
| 2100 | * Try to fall back to default hash SHA1 if the client |
| 2101 | * hasn't provided any preferred signature-hash combinations. |
| 2102 | */ |
| 2103 | if( sig_hash_alg_ext_present == 0 ) |
| 2104 | { |
| 2105 | mbedtls_md_type_t md_default = MBEDTLS_MD_SHA1; |
| 2106 | |
| 2107 | if( mbedtls_ssl_check_sig_hash( ssl, md_default ) != 0 ) |
| 2108 | md_default = MBEDTLS_MD_NONE; |
| 2109 | |
| 2110 | mbedtls_ssl_sig_hash_set_const_hash( &ssl->handshake->hash_algs, md_default ); |
| 2111 | } |
| 2112 | |
| 2113 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && |
| 2114 | MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
| 2115 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2116 | /* |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 2117 | * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV |
| 2118 | */ |
| 2119 | for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 ) |
| 2120 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2121 | 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] | 2122 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2123 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) ); |
| 2124 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 2125 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 2126 | { |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 2127 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV " |
| 2128 | "during renegotiation" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 2129 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 2130 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2131 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 2132 | } |
Manuel Pégourié-Gonnard | 69849f8 | 2015-03-10 11:54:02 +0000 | [diff] [blame] | 2133 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2134 | ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION; |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 2135 | break; |
| 2136 | } |
| 2137 | } |
| 2138 | |
| 2139 | /* |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2140 | * Renegotiation security checks |
| 2141 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2142 | if( ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION && |
Hanno Becker | b0b2b67 | 2019-06-12 16:58:10 +0100 | [diff] [blame] | 2143 | mbedtls_ssl_conf_get_allow_legacy_renegotiation( ssl->conf ) == |
| 2144 | MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE ) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2145 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2146 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) ); |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2147 | handshake_failure = 1; |
| 2148 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2149 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 2150 | else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && |
| 2151 | ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION && |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2152 | renegotiation_info_seen == 0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2153 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2154 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) ); |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2155 | handshake_failure = 1; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2156 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2157 | else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && |
| 2158 | ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && |
Hanno Becker | b0b2b67 | 2019-06-12 16:58:10 +0100 | [diff] [blame] | 2159 | mbedtls_ssl_conf_get_allow_legacy_renegotiation( ssl->conf ) |
| 2160 | == MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2161 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2162 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) ); |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2163 | handshake_failure = 1; |
| 2164 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2165 | else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && |
| 2166 | ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2167 | renegotiation_info_seen == 1 ) |
| 2168 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2169 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) ); |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2170 | handshake_failure = 1; |
| 2171 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2172 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2173 | |
Jarno Lamsa | 842be16 | 2019-06-10 15:05:33 +0300 | [diff] [blame] | 2174 | /* |
| 2175 | * Check if extended master secret is being enforced |
| 2176 | */ |
| 2177 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Hanno Becker | aabbb58 | 2019-06-11 13:43:27 +0100 | [diff] [blame] | 2178 | if( mbedtls_ssl_conf_get_ems( ssl->conf ) == |
Hanno Becker | 03b64fa | 2019-06-11 14:39:38 +0100 | [diff] [blame] | 2179 | MBEDTLS_SSL_EXTENDED_MS_ENABLED ) |
Jarno Lamsa | 842be16 | 2019-06-10 15:05:33 +0300 | [diff] [blame] | 2180 | { |
Hanno Becker | 03b64fa | 2019-06-11 14:39:38 +0100 | [diff] [blame] | 2181 | if( extended_ms_seen ) |
| 2182 | { |
Hanno Becker | 1ab322b | 2019-06-11 14:50:54 +0100 | [diff] [blame] | 2183 | #if !defined(MBEDTLS_SSL_EXTENDED_MS_ENFORCED) |
Hanno Becker | 03b64fa | 2019-06-11 14:39:38 +0100 | [diff] [blame] | 2184 | ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; |
Hanno Becker | 1ab322b | 2019-06-11 14:50:54 +0100 | [diff] [blame] | 2185 | #endif /* !MBEDTLS_SSL_EXTENDED_MS_ENFORCED */ |
Hanno Becker | 03b64fa | 2019-06-11 14:39:38 +0100 | [diff] [blame] | 2186 | } |
| 2187 | else if( mbedtls_ssl_conf_get_ems_enforced( ssl->conf ) == |
| 2188 | MBEDTLS_SSL_EXTENDED_MS_ENFORCE_ENABLED ) |
| 2189 | { |
| 2190 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Peer not offering extended master " |
| 2191 | "secret, while it is enforced") ); |
| 2192 | handshake_failure = 1; |
| 2193 | } |
Jarno Lamsa | 842be16 | 2019-06-10 15:05:33 +0300 | [diff] [blame] | 2194 | } |
| 2195 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
| 2196 | |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2197 | if( handshake_failure == 1 ) |
| 2198 | { |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 2199 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 2200 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2201 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2202 | } |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2203 | |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2204 | /* |
| 2205 | * Search for a matching ciphersuite |
Manuel Pégourié-Gonnard | 3ebb2cd | 2013-09-23 17:00:18 +0200 | [diff] [blame] | 2206 | * (At the end because we need information from the EC-based extensions |
| 2207 | * and certificate from the SNI callback triggered by the SNI extension.) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2208 | */ |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 2209 | got_common_suite = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2210 | #if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE) |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 2211 | 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] | 2212 | { |
Hanno Becker | 981f81d | 2019-07-19 16:10:49 +0100 | [diff] [blame] | 2213 | MBEDTLS_SSL_BEGIN_FOR_EACH_CIPHERSUITE( ssl, \ |
| 2214 | mbedtls_ssl_get_minor_ver( ssl ), \ |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 2215 | cur_info ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2216 | { |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 2217 | #else |
Hanno Becker | 981f81d | 2019-07-19 16:10:49 +0100 | [diff] [blame] | 2218 | MBEDTLS_SSL_BEGIN_FOR_EACH_CIPHERSUITE( ssl, \ |
| 2219 | mbedtls_ssl_get_minor_ver( ssl ), \ |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 2220 | cur_info ) |
| 2221 | { |
| 2222 | for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 ) |
| 2223 | { |
| 2224 | #endif |
| 2225 | const int ciphersuite_id = |
| 2226 | mbedtls_ssl_suite_get_id( cur_info ); |
| 2227 | |
| 2228 | if( p[0] != ( ( ciphersuite_id >> 8 ) & 0xFF ) || |
| 2229 | p[1] != ( ( ciphersuite_id ) & 0xFF ) ) |
| 2230 | { |
Hanno Becker | f4d6b49 | 2019-07-02 17:13:14 +0100 | [diff] [blame] | 2231 | continue; |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 2232 | } |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2233 | |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 2234 | got_common_suite = 1; |
| 2235 | |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 2236 | if( ssl_ciphersuite_is_match( ssl, cur_info, |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 2237 | acceptable_ec_tls_ids, |
| 2238 | ec_tls_ids_len ) != 0 ) |
Hanno Becker | d3b2fcb | 2019-06-17 14:30:05 +0100 | [diff] [blame] | 2239 | { |
Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 2240 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 2241 | ciphersuite_info = cur_info; |
Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 2242 | #endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 2243 | goto have_ciphersuite; |
Hanno Becker | d3b2fcb | 2019-06-17 14:30:05 +0100 | [diff] [blame] | 2244 | } |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 2245 | #if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2246 | } |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 2247 | MBEDTLS_SSL_END_FOR_EACH_CIPHERSUITE |
| 2248 | } |
| 2249 | #else |
| 2250 | } |
| 2251 | } |
| 2252 | MBEDTLS_SSL_END_FOR_EACH_CIPHERSUITE |
| 2253 | #endif |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2254 | |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 2255 | if( got_common_suite ) |
| 2256 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2257 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, " |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 2258 | "but none of them usable" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 2259 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 2260 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2261 | return( MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE ); |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 2262 | } |
| 2263 | else |
| 2264 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2265 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 2266 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 2267 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2268 | return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN ); |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 2269 | } |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2270 | |
| 2271 | have_ciphersuite: |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 2272 | |
Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 2273 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 2274 | ssl->session_negotiate->ciphersuite = |
| 2275 | mbedtls_ssl_suite_get_id( ciphersuite_info ); |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 2276 | ssl->handshake->ciphersuite_info = ciphersuite_info; |
Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 2277 | #endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2278 | |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 2279 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", |
| 2280 | mbedtls_ssl_get_ciphersuite_name( |
| 2281 | mbedtls_ssl_session_get_ciphersuite( ssl->session_negotiate ) ) ) ); |
| 2282 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2283 | ssl->state++; |
| 2284 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2285 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 2286 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2287 | mbedtls_ssl_recv_flight_completed( ssl ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2288 | #endif |
| 2289 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 2290 | /* Debugging-only output for testsuite */ |
| 2291 | #if defined(MBEDTLS_DEBUG_C) && \ |
| 2292 | defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 2293 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 2294 | if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 2295 | { |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 2296 | mbedtls_pk_type_t sig_alg = mbedtls_ssl_get_ciphersuite_sig_alg( |
| 2297 | mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ) ); |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 2298 | if( sig_alg != MBEDTLS_PK_NONE ) |
| 2299 | { |
| 2300 | mbedtls_md_type_t md_alg = mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs, |
| 2301 | sig_alg ); |
| 2302 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d", |
| 2303 | mbedtls_ssl_hash_from_md_alg( md_alg ) ) ); |
| 2304 | } |
| 2305 | else |
| 2306 | { |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 2307 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no hash algorithm for signature algorithm " |
| 2308 | "%d - should not happen", sig_alg ) ); |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 2309 | } |
| 2310 | } |
| 2311 | #endif |
| 2312 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2313 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2314 | |
| 2315 | return( 0 ); |
| 2316 | } |
| 2317 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2318 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 2319 | 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] | 2320 | unsigned char *buf, |
| 2321 | size_t *olen ) |
| 2322 | { |
| 2323 | unsigned char *p = buf; |
| 2324 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2325 | 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] | 2326 | { |
| 2327 | *olen = 0; |
| 2328 | return; |
| 2329 | } |
| 2330 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2331 | 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] | 2332 | |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 2333 | 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] | 2334 | *p++ = 0x00; |
| 2335 | *p++ = 0x00; |
| 2336 | |
| 2337 | *olen = 4; |
| 2338 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2339 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 2340 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2341 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 072d4ec | 2019-04-26 15:46:55 +0100 | [diff] [blame] | 2342 | static void ssl_write_cid_ext( mbedtls_ssl_context *ssl, |
| 2343 | unsigned char *buf, |
| 2344 | size_t *olen ) |
| 2345 | { |
| 2346 | unsigned char *p = buf; |
| 2347 | size_t ext_len; |
| 2348 | const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN; |
| 2349 | |
| 2350 | *olen = 0; |
| 2351 | |
| 2352 | /* Skip writing the extension if we don't want to use it or if |
| 2353 | * the client hasn't offered it. */ |
| 2354 | if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_DISABLED ) |
| 2355 | return; |
| 2356 | |
| 2357 | /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX |
| 2358 | * which is at most 255, so the increment cannot overflow. */ |
| 2359 | if( end < p || (size_t)( end - p ) < (unsigned)( ssl->own_cid_len + 5 ) ) |
| 2360 | { |
| 2361 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) ); |
| 2362 | return; |
| 2363 | } |
| 2364 | |
| 2365 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding CID extension" ) ); |
| 2366 | |
| 2367 | /* |
Hanno Becker | 3cdf8fe | 2019-05-15 10:26:32 +0100 | [diff] [blame] | 2368 | * Quoting draft-ietf-tls-dtls-connection-id-05 |
| 2369 | * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05 |
Hanno Becker | 072d4ec | 2019-04-26 15:46:55 +0100 | [diff] [blame] | 2370 | * |
| 2371 | * struct { |
| 2372 | * opaque cid<0..2^8-1>; |
| 2373 | * } ConnectionId; |
| 2374 | */ |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 2375 | p = mbedtls_platform_put_uint16_be( p, MBEDTLS_TLS_EXT_CID ); |
Hanno Becker | 072d4ec | 2019-04-26 15:46:55 +0100 | [diff] [blame] | 2376 | ext_len = (size_t) ssl->own_cid_len + 1; |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 2377 | p = mbedtls_platform_put_uint16_be( p, ext_len ); |
Hanno Becker | 072d4ec | 2019-04-26 15:46:55 +0100 | [diff] [blame] | 2378 | *p++ = (uint8_t) ssl->own_cid_len; |
| 2379 | memcpy( p, ssl->own_cid, ssl->own_cid_len ); |
| 2380 | |
| 2381 | *olen = ssl->own_cid_len + 5; |
| 2382 | } |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2383 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 072d4ec | 2019-04-26 15:46:55 +0100 | [diff] [blame] | 2384 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2385 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 2386 | 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] | 2387 | unsigned char *buf, |
| 2388 | size_t *olen ) |
| 2389 | { |
| 2390 | unsigned char *p = buf; |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 2391 | mbedtls_ssl_ciphersuite_handle_t suite = |
| 2392 | MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2393 | const mbedtls_cipher_info_t *cipher = NULL; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2394 | |
Hanno Becker | 27b34d5 | 2017-10-20 14:24:51 +0100 | [diff] [blame] | 2395 | if( ssl->session_negotiate->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED || |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 2396 | 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] | 2397 | { |
| 2398 | *olen = 0; |
| 2399 | return; |
| 2400 | } |
| 2401 | |
Manuel Pégourié-Gonnard | 78e745f | 2014-11-04 15:44:06 +0100 | [diff] [blame] | 2402 | /* |
| 2403 | * RFC 7366: "If a server receives an encrypt-then-MAC request extension |
| 2404 | * from a client and then selects a stream or Authenticated Encryption |
| 2405 | * with Associated Data (AEAD) ciphersuite, it MUST NOT send an |
| 2406 | * encrypt-then-MAC response extension back to the client." |
| 2407 | */ |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 2408 | suite = mbedtls_ssl_ciphersuite_from_id( |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 2409 | mbedtls_ssl_session_get_ciphersuite( ssl->session_negotiate ) ); |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 2410 | if( suite == MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE ) |
| 2411 | { |
| 2412 | *olen = 0; |
| 2413 | return; |
| 2414 | } |
| 2415 | |
| 2416 | cipher = mbedtls_cipher_info_from_type( |
| 2417 | mbedtls_ssl_suite_get_cipher( suite ) ); |
| 2418 | if( cipher == NULL || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2419 | cipher->mode != MBEDTLS_MODE_CBC ) |
Manuel Pégourié-Gonnard | 78e745f | 2014-11-04 15:44:06 +0100 | [diff] [blame] | 2420 | { |
| 2421 | *olen = 0; |
| 2422 | return; |
| 2423 | } |
| 2424 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2425 | 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] | 2426 | |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 2427 | 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] | 2428 | *p++ = 0x00; |
| 2429 | *p++ = 0x00; |
| 2430 | |
| 2431 | *olen = 4; |
| 2432 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2433 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2434 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2435 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 2436 | 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] | 2437 | unsigned char *buf, |
| 2438 | size_t *olen ) |
| 2439 | { |
| 2440 | unsigned char *p = buf; |
| 2441 | |
Hanno Becker | a49ec56 | 2019-06-11 14:47:55 +0100 | [diff] [blame] | 2442 | if( mbedtls_ssl_hs_get_extended_ms( ssl->handshake ) |
| 2443 | == MBEDTLS_SSL_EXTENDED_MS_DISABLED || |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 2444 | 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] | 2445 | { |
| 2446 | *olen = 0; |
| 2447 | return; |
| 2448 | } |
| 2449 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2450 | 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] | 2451 | "extension" ) ); |
| 2452 | |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 2453 | 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] | 2454 | |
| 2455 | *p++ = 0x00; |
| 2456 | *p++ = 0x00; |
| 2457 | |
| 2458 | *olen = 4; |
| 2459 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2460 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2461 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2462 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 2463 | 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] | 2464 | unsigned char *buf, |
| 2465 | size_t *olen ) |
| 2466 | { |
| 2467 | unsigned char *p = buf; |
| 2468 | |
| 2469 | if( ssl->handshake->new_session_ticket == 0 ) |
| 2470 | { |
| 2471 | *olen = 0; |
| 2472 | return; |
| 2473 | } |
| 2474 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2475 | 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] | 2476 | |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 2477 | 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] | 2478 | *p++ = 0x00; |
| 2479 | *p++ = 0x00; |
| 2480 | |
| 2481 | *olen = 4; |
| 2482 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2483 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 2484 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2485 | static void ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | f11a6d7 | 2013-07-17 11:17:14 +0200 | [diff] [blame] | 2486 | unsigned char *buf, |
| 2487 | size_t *olen ) |
| 2488 | { |
| 2489 | unsigned char *p = buf; |
| 2490 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2491 | if( ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION ) |
Manuel Pégourié-Gonnard | f11a6d7 | 2013-07-17 11:17:14 +0200 | [diff] [blame] | 2492 | { |
| 2493 | *olen = 0; |
| 2494 | return; |
| 2495 | } |
| 2496 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2497 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) ); |
Manuel Pégourié-Gonnard | f11a6d7 | 2013-07-17 11:17:14 +0200 | [diff] [blame] | 2498 | |
Arto Kinnunen | a3fa06e | 2019-09-09 12:22:51 +0300 | [diff] [blame] | 2499 | 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] | 2500 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2501 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 2502 | if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ) |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2503 | { |
| 2504 | *p++ = 0x00; |
| 2505 | *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF; |
| 2506 | *p++ = ssl->verify_data_len * 2 & 0xFF; |
Manuel Pégourié-Gonnard | f11a6d7 | 2013-07-17 11:17:14 +0200 | [diff] [blame] | 2507 | |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2508 | memcpy( p, ssl->peer_verify_data, ssl->verify_data_len ); |
| 2509 | p += ssl->verify_data_len; |
| 2510 | memcpy( p, ssl->own_verify_data, ssl->verify_data_len ); |
| 2511 | p += ssl->verify_data_len; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2512 | } |
| 2513 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2514 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2515 | { |
| 2516 | *p++ = 0x00; |
| 2517 | *p++ = 0x01; |
| 2518 | *p++ = 0x00; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2519 | } |
Manuel Pégourié-Gonnard | 1938975 | 2015-06-23 13:46:44 +0200 | [diff] [blame] | 2520 | |
| 2521 | *olen = p - buf; |
Manuel Pégourié-Gonnard | f11a6d7 | 2013-07-17 11:17:14 +0200 | [diff] [blame] | 2522 | } |
| 2523 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2524 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 2525 | 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] | 2526 | unsigned char *buf, |
| 2527 | size_t *olen ) |
| 2528 | { |
| 2529 | unsigned char *p = buf; |
| 2530 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2531 | 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] | 2532 | { |
Manuel Pégourié-Gonnard | 7bb7899 | 2013-07-17 13:50:08 +0200 | [diff] [blame] | 2533 | *olen = 0; |
| 2534 | return; |
| 2535 | } |
| 2536 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2537 | 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] | 2538 | |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 2539 | 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] | 2540 | *p++ = 0x00; |
| 2541 | *p++ = 1; |
| 2542 | |
Manuel Pégourié-Gonnard | ed4af8b | 2013-07-18 14:07:09 +0200 | [diff] [blame] | 2543 | *p++ = ssl->session_negotiate->mfl_code; |
Manuel Pégourié-Gonnard | 7bb7899 | 2013-07-17 13:50:08 +0200 | [diff] [blame] | 2544 | |
| 2545 | *olen = 5; |
| 2546 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2547 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 7bb7899 | 2013-07-17 13:50:08 +0200 | [diff] [blame] | 2548 | |
Manuel Pégourié-Gonnard | f472179 | 2015-09-15 10:53:51 +0200 | [diff] [blame] | 2549 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 2550 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) || \ |
| 2551 | defined(MBEDTLS_USE_TINYCRYPT) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2552 | 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] | 2553 | unsigned char *buf, |
| 2554 | size_t *olen ) |
| 2555 | { |
| 2556 | unsigned char *p = buf; |
| 2557 | ((void) ssl); |
| 2558 | |
Paul Bakker | 677377f | 2013-10-28 12:54:26 +0100 | [diff] [blame] | 2559 | if( ( ssl->handshake->cli_exts & |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2560 | MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 ) |
Paul Bakker | 677377f | 2013-10-28 12:54:26 +0100 | [diff] [blame] | 2561 | { |
| 2562 | *olen = 0; |
| 2563 | return; |
| 2564 | } |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 2565 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2566 | 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] | 2567 | |
Arto Kinnunen | a3fa06e | 2019-09-09 12:22:51 +0300 | [diff] [blame] | 2568 | 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] | 2569 | |
| 2570 | *p++ = 0x00; |
| 2571 | *p++ = 2; |
| 2572 | |
| 2573 | *p++ = 1; |
Hanno Becker | 27b7e50 | 2019-08-23 14:39:50 +0100 | [diff] [blame] | 2574 | *p++ = MBEDTLS_SSL_EC_PF_UNCOMPRESSED; |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 2575 | |
| 2576 | *olen = 6; |
| 2577 | } |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 2578 | #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] | 2579 | |
Manuel Pégourié-Gonnard | 55c7f99 | 2015-09-16 15:35:27 +0200 | [diff] [blame] | 2580 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 2581 | static void ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl, |
| 2582 | unsigned char *buf, |
| 2583 | size_t *olen ) |
| 2584 | { |
| 2585 | int ret; |
| 2586 | unsigned char *p = buf; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2587 | 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] | 2588 | size_t kkpp_len; |
| 2589 | |
| 2590 | *olen = 0; |
| 2591 | |
| 2592 | /* Skip costly computation if not needed */ |
Hanno Becker | df64596 | 2019-06-26 13:02:22 +0100 | [diff] [blame] | 2593 | if( mbedtls_ssl_suite_get_key_exchange( |
| 2594 | mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ) ) != |
Manuel Pégourié-Gonnard | 55c7f99 | 2015-09-16 15:35:27 +0200 | [diff] [blame] | 2595 | MBEDTLS_KEY_EXCHANGE_ECJPAKE ) |
Hanno Becker | df64596 | 2019-06-26 13:02:22 +0100 | [diff] [blame] | 2596 | { |
Manuel Pégourié-Gonnard | 55c7f99 | 2015-09-16 15:35:27 +0200 | [diff] [blame] | 2597 | return; |
Hanno Becker | df64596 | 2019-06-26 13:02:22 +0100 | [diff] [blame] | 2598 | } |
Manuel Pégourié-Gonnard | 55c7f99 | 2015-09-16 15:35:27 +0200 | [diff] [blame] | 2599 | |
| 2600 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, ecjpake kkpp extension" ) ); |
| 2601 | |
| 2602 | if( end - p < 4 ) |
| 2603 | { |
| 2604 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) ); |
| 2605 | return; |
| 2606 | } |
| 2607 | |
Arto Kinnunen | a3fa06e | 2019-09-09 12:22:51 +0300 | [diff] [blame] | 2608 | 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] | 2609 | |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 2610 | ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx, |
| 2611 | p + 2, end - p - 2, &kkpp_len, |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 2612 | mbedtls_ssl_conf_get_frng( ssl->conf ), |
Hanno Becker | 9a12243 | 2019-07-23 13:24:02 +0100 | [diff] [blame] | 2613 | mbedtls_ssl_conf_get_prng( ssl->conf ) ); |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 2614 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 55c7f99 | 2015-09-16 15:35:27 +0200 | [diff] [blame] | 2615 | { |
| 2616 | MBEDTLS_SSL_DEBUG_RET( 1 , "mbedtls_ecjpake_write_round_one", ret ); |
| 2617 | return; |
| 2618 | } |
| 2619 | |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 2620 | p = mbedtls_platform_put_uint16_be( p, kkpp_len ); |
Manuel Pégourié-Gonnard | 55c7f99 | 2015-09-16 15:35:27 +0200 | [diff] [blame] | 2621 | *olen = kkpp_len + 4; |
| 2622 | } |
| 2623 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
| 2624 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2625 | #if defined(MBEDTLS_SSL_ALPN ) |
| 2626 | static void ssl_write_alpn_ext( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 2627 | unsigned char *buf, size_t *olen ) |
| 2628 | { |
| 2629 | if( ssl->alpn_chosen == NULL ) |
| 2630 | { |
| 2631 | *olen = 0; |
| 2632 | return; |
| 2633 | } |
| 2634 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2635 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) ); |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 2636 | |
| 2637 | /* |
| 2638 | * 0 . 1 ext identifier |
| 2639 | * 2 . 3 ext length |
| 2640 | * 4 . 5 protocol list length |
| 2641 | * 6 . 6 protocol name length |
| 2642 | * 7 . 7+n protocol name |
| 2643 | */ |
Arto Kinnunen | 3d7439e | 2019-09-10 11:30:40 +0300 | [diff] [blame^] | 2644 | (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] | 2645 | |
| 2646 | *olen = 7 + strlen( ssl->alpn_chosen ); |
Arto Kinnunen | 3d7439e | 2019-09-10 11:30:40 +0300 | [diff] [blame^] | 2647 | (void)mbedtls_platform_put_uint16_be( &buf[2], ( *olen - 4 ) ); |
| 2648 | (void)mbedtls_platform_put_uint16_be( &buf[4], ( *olen - 6 ) ); |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 2649 | |
| 2650 | buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF ); |
| 2651 | |
| 2652 | memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 ); |
| 2653 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2654 | #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */ |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 2655 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2656 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) |
| 2657 | 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] | 2658 | { |
| 2659 | int ret; |
| 2660 | unsigned char *p = ssl->out_msg + 4; |
Manuel Pégourié-Gonnard | d7f9bc5 | 2014-07-23 11:09:27 +0200 | [diff] [blame] | 2661 | unsigned char *cookie_len_byte; |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2662 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2663 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello verify request" ) ); |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2664 | |
| 2665 | /* |
| 2666 | * struct { |
| 2667 | * ProtocolVersion server_version; |
| 2668 | * opaque cookie<0..2^8-1>; |
| 2669 | * } HelloVerifyRequest; |
| 2670 | */ |
| 2671 | |
Manuel Pégourié-Gonnard | b35fe56 | 2014-08-09 17:00:46 +0200 | [diff] [blame] | 2672 | /* The RFC is not clear on this point, but sending the actual negotiated |
| 2673 | * version looks like the most interoperable thing to do. */ |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 2674 | mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ), |
| 2675 | mbedtls_ssl_get_minor_ver( ssl ), |
| 2676 | ssl->conf->transport, p ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2677 | MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 ); |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2678 | p += 2; |
| 2679 | |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 2680 | /* If we get here, f_cookie_check is not null */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2681 | if( ssl->conf->f_cookie_write == NULL ) |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 2682 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2683 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "inconsistent cookie callbacks" ) ); |
| 2684 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 2685 | } |
| 2686 | |
Manuel Pégourié-Gonnard | d7f9bc5 | 2014-07-23 11:09:27 +0200 | [diff] [blame] | 2687 | /* Skip length byte until we know the length */ |
| 2688 | cookie_len_byte = p++; |
| 2689 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2690 | if( ( ret = ssl->conf->f_cookie_write( ssl->conf->p_cookie, |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2691 | &p, ssl->out_buf + MBEDTLS_SSL_OUT_BUFFER_LEN, |
Manuel Pégourié-Gonnard | d485d19 | 2014-07-23 14:56:15 +0200 | [diff] [blame] | 2692 | ssl->cli_id, ssl->cli_id_len ) ) != 0 ) |
Manuel Pégourié-Gonnard | d7f9bc5 | 2014-07-23 11:09:27 +0200 | [diff] [blame] | 2693 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2694 | MBEDTLS_SSL_DEBUG_RET( 1, "f_cookie_write", ret ); |
Manuel Pégourié-Gonnard | d7f9bc5 | 2014-07-23 11:09:27 +0200 | [diff] [blame] | 2695 | return( ret ); |
| 2696 | } |
| 2697 | |
| 2698 | *cookie_len_byte = (unsigned char)( p - ( cookie_len_byte + 1 ) ); |
| 2699 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2700 | 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] | 2701 | |
| 2702 | ssl->out_msglen = p - ssl->out_msg; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2703 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 2704 | ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST; |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2705 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2706 | ssl->state = MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT; |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2707 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2708 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2709 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2710 | 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] | 2711 | return( ret ); |
| 2712 | } |
| 2713 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2714 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 2715 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2716 | ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 2717 | { |
| 2718 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); |
| 2719 | return( ret ); |
| 2720 | } |
Hanno Becker | bc2498a | 2018-08-28 10:13:29 +0100 | [diff] [blame] | 2721 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2722 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2723 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello verify request" ) ); |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2724 | |
| 2725 | return( 0 ); |
| 2726 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2727 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */ |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2728 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2729 | static int ssl_write_server_hello( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2730 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2731 | #if defined(MBEDTLS_HAVE_TIME) |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 2732 | mbedtls_time_t t; |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 2733 | #endif |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 2734 | int ret; |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 2735 | int ciphersuite; |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 2736 | size_t olen, ext_len = 0, n; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2737 | unsigned char *buf, *p; |
| 2738 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2739 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2740 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2741 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 2742 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Manuel Pégourié-Gonnard | d7f9bc5 | 2014-07-23 11:09:27 +0200 | [diff] [blame] | 2743 | ssl->handshake->verify_cookie_len != 0 ) |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2744 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2745 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "client hello was not authenticated" ) ); |
| 2746 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) ); |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2747 | |
Manuel Pégourié-Gonnard | 579950c | 2014-09-29 17:47:33 +0200 | [diff] [blame] | 2748 | return( ssl_write_hello_verify_request( ssl ) ); |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2749 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2750 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */ |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2751 | |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 2752 | if( mbedtls_ssl_conf_get_frng( ssl->conf ) == NULL ) |
Paul Bakker | a9a028e | 2013-11-21 17:31:06 +0100 | [diff] [blame] | 2753 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2754 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided") ); |
| 2755 | return( MBEDTLS_ERR_SSL_NO_RNG ); |
Paul Bakker | a9a028e | 2013-11-21 17:31:06 +0100 | [diff] [blame] | 2756 | } |
| 2757 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2758 | /* |
| 2759 | * 0 . 0 handshake type |
| 2760 | * 1 . 3 handshake length |
| 2761 | * 4 . 5 protocol version |
| 2762 | * 6 . 9 UNIX time() |
| 2763 | * 10 . 37 random bytes |
| 2764 | */ |
| 2765 | buf = ssl->out_msg; |
| 2766 | p = buf + 4; |
| 2767 | |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 2768 | mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ), |
| 2769 | mbedtls_ssl_get_minor_ver( ssl ), |
| 2770 | ssl->conf->transport, p ); |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 2771 | p += 2; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2772 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2773 | 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] | 2774 | buf[4], buf[5] ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2775 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2776 | #if defined(MBEDTLS_HAVE_TIME) |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 2777 | t = mbedtls_time( NULL ); |
Arto Kinnunen | 6e3f09b | 2019-09-06 17:37:01 +0300 | [diff] [blame] | 2778 | p = mbedtls_platform_put_uint32_be( p, (uint32_t) t ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2779 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2780 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) ); |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 2781 | #else |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 2782 | if( ( ret = mbedtls_ssl_conf_get_frng( ssl->conf ) |
Hanno Becker | 9a12243 | 2019-07-23 13:24:02 +0100 | [diff] [blame] | 2783 | ( mbedtls_ssl_conf_get_prng( ssl->conf ), p, 4 ) ) != 0 ) |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 2784 | { |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 2785 | return( ret ); |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 2786 | } |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 2787 | |
| 2788 | p += 4; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2789 | #endif /* MBEDTLS_HAVE_TIME */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2790 | |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 2791 | if( ( ret = mbedtls_ssl_conf_get_frng( ssl->conf ) |
Hanno Becker | 9a12243 | 2019-07-23 13:24:02 +0100 | [diff] [blame] | 2792 | ( mbedtls_ssl_conf_get_prng( ssl->conf ), p, 28 ) ) != 0 ) |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 2793 | { |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 2794 | return( ret ); |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 2795 | } |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 2796 | |
| 2797 | p += 28; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2798 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2799 | memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2800 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2801 | MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2802 | |
Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 2803 | #if !defined(MBEDTLS_SSL_NO_SESSION_CACHE) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2804 | /* |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2805 | * Resume is 0 by default, see ssl_handshake_init(). |
| 2806 | * It may be already set to 1 by ssl_parse_session_ticket_ext(). |
| 2807 | * If not, try looking up session ID in our cache. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2808 | */ |
Manuel Pégourié-Gonnard | 3652e99 | 2019-07-01 12:09:22 +0200 | [diff] [blame] | 2809 | if( mbedtls_ssl_handshake_get_resume( ssl->handshake ) == 0 && |
Manuel Pégourié-Gonnard | 754b9f3 | 2019-07-01 12:20:54 +0200 | [diff] [blame] | 2810 | mbedtls_ssl_get_renego_status( ssl ) == MBEDTLS_SSL_INITIAL_HANDSHAKE && |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 2811 | ssl->session_negotiate->id_len != 0 && |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2812 | ssl->conf->f_get_cache != NULL && |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 2813 | 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] | 2814 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2815 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) ); |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2816 | ssl->handshake->resume = 1; |
| 2817 | } |
Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 2818 | #endif /* !MBEDTLS_SSL_NO_SESSION_CACHE */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2819 | |
Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 2820 | #if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION) |
Manuel Pégourié-Gonnard | 3652e99 | 2019-07-01 12:09:22 +0200 | [diff] [blame] | 2821 | if( mbedtls_ssl_handshake_get_resume( ssl->handshake ) == 1 ) |
Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 2822 | { |
| 2823 | /* |
| 2824 | * Resuming a session |
| 2825 | */ |
| 2826 | n = ssl->session_negotiate->id_len; |
| 2827 | ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC; |
| 2828 | |
| 2829 | if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 ) |
| 2830 | { |
| 2831 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret ); |
| 2832 | return( ret ); |
| 2833 | } |
| 2834 | } |
| 2835 | else |
| 2836 | #endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2837 | { |
| 2838 | /* |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2839 | * New session, create a new session id, |
| 2840 | * unless we're about to issue a session ticket |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2841 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2842 | ssl->state++; |
| 2843 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2844 | #if defined(MBEDTLS_HAVE_TIME) |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 2845 | ssl->session_negotiate->start = mbedtls_time( NULL ); |
Manuel Pégourié-Gonnard | 164d894 | 2013-09-23 22:01:39 +0200 | [diff] [blame] | 2846 | #endif |
| 2847 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2848 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | 164d894 | 2013-09-23 22:01:39 +0200 | [diff] [blame] | 2849 | if( ssl->handshake->new_session_ticket != 0 ) |
| 2850 | { |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 2851 | ssl->session_negotiate->id_len = n = 0; |
Manuel Pégourié-Gonnard | 164d894 | 2013-09-23 22:01:39 +0200 | [diff] [blame] | 2852 | memset( ssl->session_negotiate->id, 0, 32 ); |
| 2853 | } |
| 2854 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2855 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2856 | { |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 2857 | ssl->session_negotiate->id_len = n = 32; |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 2858 | if( ( ret = mbedtls_ssl_conf_get_frng( ssl->conf ) |
Hanno Becker | 9a12243 | 2019-07-23 13:24:02 +0100 | [diff] [blame] | 2859 | ( 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] | 2860 | { |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2861 | return( ret ); |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 2862 | } |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2863 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2864 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2865 | |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2866 | /* |
| 2867 | * 38 . 38 session id length |
| 2868 | * 39 . 38+n session id |
| 2869 | * 39+n . 40+n chosen ciphersuite |
| 2870 | * 41+n . 41+n chosen compression alg. |
| 2871 | * 42+n . 43+n extensions length |
| 2872 | * 44+n . 43+n+m extensions |
| 2873 | */ |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 2874 | *p++ = (unsigned char) ssl->session_negotiate->id_len; |
| 2875 | memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len ); |
| 2876 | p += ssl->session_negotiate->id_len; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2877 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2878 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) ); |
| 2879 | MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n ); |
| 2880 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed", |
Manuel Pégourié-Gonnard | 3652e99 | 2019-07-01 12:09:22 +0200 | [diff] [blame] | 2881 | mbedtls_ssl_handshake_get_resume( ssl->handshake ) ? "a" : "no" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2882 | |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 2883 | ciphersuite = mbedtls_ssl_session_get_ciphersuite( ssl->session_negotiate ); |
Arto Kinnunen | a3fa06e | 2019-09-09 12:22:51 +0300 | [diff] [blame] | 2884 | p = mbedtls_platform_put_uint16_be( p, ciphersuite ); |
Hanno Becker | 8844055 | 2019-07-03 14:16:13 +0100 | [diff] [blame] | 2885 | *p++ = (unsigned char)( |
| 2886 | mbedtls_ssl_session_get_compression( ssl->session_negotiate ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2887 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2888 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s", |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 2889 | mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2890 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X", |
Hanno Becker | 8844055 | 2019-07-03 14:16:13 +0100 | [diff] [blame] | 2891 | mbedtls_ssl_session_get_compression( ssl->session_negotiate ) ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2892 | |
Janos Follath | c6dab2b | 2016-05-23 14:27:02 +0100 | [diff] [blame] | 2893 | /* Do not write the extensions if the protocol is SSLv3 */ |
| 2894 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 2895 | 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] | 2896 | { |
| 2897 | #endif |
| 2898 | |
Manuel Pégourié-Gonnard | f11a6d7 | 2013-07-17 11:17:14 +0200 | [diff] [blame] | 2899 | /* |
| 2900 | * First write extensions, then the total length |
| 2901 | */ |
| 2902 | ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen ); |
| 2903 | ext_len += olen; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2904 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2905 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 7bb7899 | 2013-07-17 13:50:08 +0200 | [diff] [blame] | 2906 | ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen ); |
| 2907 | ext_len += olen; |
Paul Bakker | 05decb2 | 2013-08-15 13:33:48 +0200 | [diff] [blame] | 2908 | #endif |
Manuel Pégourié-Gonnard | 7bb7899 | 2013-07-17 13:50:08 +0200 | [diff] [blame] | 2909 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2910 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 2911 | ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen ); |
| 2912 | ext_len += olen; |
Paul Bakker | 1f2bc62 | 2013-08-15 13:45:55 +0200 | [diff] [blame] | 2913 | #endif |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 2914 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2915 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 072d4ec | 2019-04-26 15:46:55 +0100 | [diff] [blame] | 2916 | ssl_write_cid_ext( ssl, p + 2 + ext_len, &olen ); |
| 2917 | ext_len += olen; |
| 2918 | #endif |
| 2919 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2920 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2921 | ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen ); |
| 2922 | ext_len += olen; |
| 2923 | #endif |
| 2924 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2925 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2926 | ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen ); |
| 2927 | ext_len += olen; |
| 2928 | #endif |
| 2929 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2930 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 2931 | ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen ); |
| 2932 | ext_len += olen; |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 2933 | #endif |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 2934 | |
Manuel Pégourié-Gonnard | f472179 | 2015-09-15 10:53:51 +0200 | [diff] [blame] | 2935 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 2936 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) || \ |
| 2937 | defined(MBEDTLS_USE_TINYCRYPT) |
Ron Eldor | 755bb6a | 2018-02-14 19:30:48 +0200 | [diff] [blame] | 2938 | if ( mbedtls_ssl_ciphersuite_uses_ec( |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 2939 | mbedtls_ssl_ciphersuite_from_id( |
| 2940 | mbedtls_ssl_session_get_ciphersuite( ssl->session_negotiate ) ) ) ) |
Ron Eldor | 755bb6a | 2018-02-14 19:30:48 +0200 | [diff] [blame] | 2941 | { |
| 2942 | ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen ); |
| 2943 | ext_len += olen; |
| 2944 | } |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 2945 | #endif |
| 2946 | |
Manuel Pégourié-Gonnard | 55c7f99 | 2015-09-16 15:35:27 +0200 | [diff] [blame] | 2947 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 2948 | ssl_write_ecjpake_kkpp_ext( ssl, p + 2 + ext_len, &olen ); |
| 2949 | ext_len += olen; |
| 2950 | #endif |
| 2951 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2952 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 2953 | ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen ); |
| 2954 | ext_len += olen; |
| 2955 | #endif |
| 2956 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2957 | 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] | 2958 | |
Paul Bakker | a703663 | 2014-04-30 10:15:38 +0200 | [diff] [blame] | 2959 | if( ext_len > 0 ) |
| 2960 | { |
Arto Kinnunen | a3fa06e | 2019-09-09 12:22:51 +0300 | [diff] [blame] | 2961 | p = mbedtls_platform_put_uint16_be( p, ext_len ); |
Paul Bakker | a703663 | 2014-04-30 10:15:38 +0200 | [diff] [blame] | 2962 | p += ext_len; |
| 2963 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2964 | |
Janos Follath | c6dab2b | 2016-05-23 14:27:02 +0100 | [diff] [blame] | 2965 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 2966 | } |
| 2967 | #endif |
| 2968 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2969 | ssl->out_msglen = p - buf; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2970 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 2971 | ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2972 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2973 | ret = mbedtls_ssl_write_handshake_msg( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2974 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2975 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2976 | |
| 2977 | return( ret ); |
| 2978 | } |
| 2979 | |
Hanno Becker | ae39b9e | 2019-02-07 12:32:43 +0000 | [diff] [blame] | 2980 | #if !defined(MBEDTLS_KEY_EXCHANGE__CERT_REQ_ALLOWED__ENABLED) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2981 | static int ssl_write_certificate_request( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2982 | { |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 2983 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = |
Hanno Becker | df64596 | 2019-06-26 13:02:22 +0100 | [diff] [blame] | 2984 | mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2985 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2986 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2987 | |
Hanno Becker | ae39b9e | 2019-02-07 12:32:43 +0000 | [diff] [blame] | 2988 | if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2989 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2990 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2991 | ssl->state++; |
| 2992 | return( 0 ); |
| 2993 | } |
| 2994 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2995 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2996 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2997 | } |
Hanno Becker | ae39b9e | 2019-02-07 12:32:43 +0000 | [diff] [blame] | 2998 | #else /* !MBEDTLS_KEY_EXCHANGE__CERT_REQ_ALLOWED__ENABLED */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2999 | static int ssl_write_certificate_request( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3000 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3001 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 3002 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = |
Hanno Becker | df64596 | 2019-06-26 13:02:22 +0100 | [diff] [blame] | 3003 | mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3004 | size_t dn_size, total_dn_size; /* excluding length bytes */ |
| 3005 | size_t ct_len, sa_len; /* including length bytes */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3006 | unsigned char *buf, *p; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3007 | 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] | 3008 | const mbedtls_x509_crt *crt; |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 3009 | int authmode; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3010 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3011 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3012 | |
| 3013 | ssl->state++; |
| 3014 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 3015 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 3016 | if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET ) |
| 3017 | authmode = ssl->handshake->sni_authmode; |
| 3018 | else |
| 3019 | #endif |
Hanno Becker | acd4fc0 | 2019-06-12 16:40:50 +0100 | [diff] [blame] | 3020 | authmode = mbedtls_ssl_conf_get_authmode( ssl->conf ); |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +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 ) || |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 3023 | authmode == MBEDTLS_SSL_VERIFY_NONE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3024 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3025 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3026 | return( 0 ); |
| 3027 | } |
| 3028 | |
| 3029 | /* |
| 3030 | * 0 . 0 handshake type |
| 3031 | * 1 . 3 handshake length |
| 3032 | * 4 . 4 cert type count |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3033 | * 5 .. m-1 cert types |
| 3034 | * m .. m+1 sig alg length (TLS 1.2 only) |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 3035 | * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3036 | * n .. n+1 length of all DNs |
| 3037 | * n+2 .. n+3 length of DN 1 |
| 3038 | * n+4 .. ... Distinguished Name #1 |
| 3039 | * ... .. ... length of DN 2, etc. |
| 3040 | */ |
| 3041 | buf = ssl->out_msg; |
| 3042 | p = buf + 4; |
| 3043 | |
| 3044 | /* |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3045 | * Supported certificate types |
| 3046 | * |
| 3047 | * ClientCertificateType certificate_types<1..2^8-1>; |
| 3048 | * enum { (255) } ClientCertificateType; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3049 | */ |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3050 | ct_len = 0; |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3051 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3052 | #if defined(MBEDTLS_RSA_C) |
| 3053 | p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_RSA_SIGN; |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3054 | #endif |
Jarno Lamsa | 7cb5c11 | 2019-04-23 15:54:56 +0300 | [diff] [blame] | 3055 | #if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3056 | p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_ECDSA_SIGN; |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3057 | #endif |
| 3058 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 3059 | p[0] = (unsigned char) ct_len++; |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3060 | p += ct_len; |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3061 | |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 3062 | sa_len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3063 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3064 | /* |
| 3065 | * Add signature_algorithms for verify (TLS 1.2) |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3066 | * |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3067 | * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>; |
| 3068 | * |
| 3069 | * struct { |
| 3070 | * HashAlgorithm hash; |
| 3071 | * SignatureAlgorithm signature; |
| 3072 | * } SignatureAndHashAlgorithm; |
| 3073 | * |
| 3074 | * enum { (255) } HashAlgorithm; |
| 3075 | * enum { (255) } SignatureAlgorithm; |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3076 | */ |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 3077 | if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3078 | { |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3079 | /* |
| 3080 | * Supported signature algorithms |
| 3081 | */ |
Hanno Becker | f1bc9e1 | 2019-06-19 16:23:21 +0100 | [diff] [blame] | 3082 | MBEDTLS_SSL_BEGIN_FOR_EACH_SIG_HASH_TLS( hash ) |
| 3083 | if( 0 |
Hanno Becker | 0af717b | 2019-06-24 11:36:30 +0100 | [diff] [blame] | 3084 | #if defined(MBEDTLS_SHA512_C) |
Hanno Becker | f1bc9e1 | 2019-06-19 16:23:21 +0100 | [diff] [blame] | 3085 | || hash == MBEDTLS_SSL_HASH_SHA384 |
Hanno Becker | 0af717b | 2019-06-24 11:36:30 +0100 | [diff] [blame] | 3086 | #endif |
| 3087 | #if defined(MBEDTLS_SHA256_C) |
Hanno Becker | f1bc9e1 | 2019-06-19 16:23:21 +0100 | [diff] [blame] | 3088 | || hash == MBEDTLS_SSL_HASH_SHA256 |
Hanno Becker | 0af717b | 2019-06-24 11:36:30 +0100 | [diff] [blame] | 3089 | #endif |
Hanno Becker | f1bc9e1 | 2019-06-19 16:23:21 +0100 | [diff] [blame] | 3090 | ) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3091 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3092 | #if defined(MBEDTLS_RSA_C) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3093 | p[2 + sa_len++] = hash; |
| 3094 | p[2 + sa_len++] = MBEDTLS_SSL_SIG_RSA; |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3095 | #endif |
Jarno Lamsa | 7cb5c11 | 2019-04-23 15:54:56 +0300 | [diff] [blame] | 3096 | #if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3097 | p[2 + sa_len++] = hash; |
| 3098 | p[2 + sa_len++] = MBEDTLS_SSL_SIG_ECDSA; |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3099 | #endif |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3100 | } |
Hanno Becker | f1bc9e1 | 2019-06-19 16:23:21 +0100 | [diff] [blame] | 3101 | MBEDTLS_SSL_END_FOR_EACH_SIG_HASH_TLS |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3102 | |
Arto Kinnunen | 3d7439e | 2019-09-10 11:30:40 +0300 | [diff] [blame^] | 3103 | (void)mbedtls_platform_put_uint16_be( p, sa_len ); |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3104 | sa_len += 2; |
| 3105 | p += sa_len; |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3106 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3107 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3108 | |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3109 | /* |
| 3110 | * DistinguishedName certificate_authorities<0..2^16-1>; |
| 3111 | * opaque DistinguishedName<1..2^16-1>; |
| 3112 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3113 | p += 2; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3114 | |
Paul Bakker | bc3d984 | 2012-11-26 16:12:02 +0100 | [diff] [blame] | 3115 | total_dn_size = 0; |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 3116 | |
Hanno Becker | c2cfdaa | 2019-06-13 12:33:03 +0100 | [diff] [blame] | 3117 | if( mbedtls_ssl_conf_get_cert_req_ca_list( ssl->conf ) |
| 3118 | == MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3119 | { |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 3120 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 3121 | if( ssl->handshake->sni_ca_chain != NULL ) |
| 3122 | crt = ssl->handshake->sni_ca_chain; |
| 3123 | else |
| 3124 | #endif |
| 3125 | crt = ssl->conf->ca_chain; |
Manuel Pégourié-Gonnard | bc1babb | 2015-10-02 11:16:47 +0200 | [diff] [blame] | 3126 | |
Hanno Becker | 371e0e4 | 2019-02-25 18:08:59 +0000 | [diff] [blame] | 3127 | while( crt != NULL && crt->raw.p != NULL ) |
Manuel Pégourié-Gonnard | bc1babb | 2015-10-02 11:16:47 +0200 | [diff] [blame] | 3128 | { |
Hanno Becker | 5f268b3 | 2019-05-20 16:26:34 +0100 | [diff] [blame] | 3129 | mbedtls_x509_crt_frame const *frame; |
Hanno Becker | 232f8fa | 2019-02-26 16:49:57 +0000 | [diff] [blame] | 3130 | ret = mbedtls_x509_crt_frame_acquire( crt, &frame ); |
| 3131 | if( ret != 0 ) |
Hanno Becker | 2224ccf | 2019-06-28 10:52:45 +0100 | [diff] [blame] | 3132 | { |
| 3133 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_frame_acquire", ret ); |
Hanno Becker | 232f8fa | 2019-02-26 16:49:57 +0000 | [diff] [blame] | 3134 | return( ret ); |
Hanno Becker | 2224ccf | 2019-06-28 10:52:45 +0100 | [diff] [blame] | 3135 | } |
Hanno Becker | 232f8fa | 2019-02-26 16:49:57 +0000 | [diff] [blame] | 3136 | |
Hanno Becker | 1e11f21 | 2019-03-04 14:43:43 +0000 | [diff] [blame] | 3137 | dn_size = frame->subject_raw.len; |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 3138 | |
| 3139 | if( end < p || |
| 3140 | (size_t)( end - p ) < dn_size || |
| 3141 | (size_t)( end - p ) < 2 + dn_size ) |
| 3142 | { |
| 3143 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "skipping CAs: buffer too short" ) ); |
Hanno Becker | c6d1c3e | 2019-03-05 13:50:56 +0000 | [diff] [blame] | 3144 | mbedtls_x509_crt_frame_release( crt ); |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 3145 | break; |
| 3146 | } |
| 3147 | |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 3148 | p = mbedtls_platform_put_uint16_be( p, dn_size ); |
Hanno Becker | 1e11f21 | 2019-03-04 14:43:43 +0000 | [diff] [blame] | 3149 | memcpy( p, frame->subject_raw.p, dn_size ); |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 3150 | p += dn_size; |
| 3151 | |
| 3152 | MBEDTLS_SSL_DEBUG_BUF( 3, "requested DN", p - dn_size, dn_size ); |
| 3153 | |
| 3154 | total_dn_size += 2 + dn_size; |
Hanno Becker | 232f8fa | 2019-02-26 16:49:57 +0000 | [diff] [blame] | 3155 | |
Hanno Becker | c6d1c3e | 2019-03-05 13:50:56 +0000 | [diff] [blame] | 3156 | mbedtls_x509_crt_frame_release( crt ); |
Hanno Becker | 232f8fa | 2019-02-26 16:49:57 +0000 | [diff] [blame] | 3157 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 3158 | crt = crt->next; |
Manuel Pégourié-Gonnard | bc1babb | 2015-10-02 11:16:47 +0200 | [diff] [blame] | 3159 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3160 | } |
| 3161 | |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3162 | ssl->out_msglen = p - buf; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3163 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 3164 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_REQUEST; |
Arto Kinnunen | 3d7439e | 2019-09-10 11:30:40 +0300 | [diff] [blame^] | 3165 | (void)mbedtls_platform_put_uint16_be( &ssl->out_msg[4 + ct_len + sa_len], |
| 3166 | total_dn_size ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3167 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3168 | ret = mbedtls_ssl_write_handshake_msg( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3169 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3170 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3171 | |
| 3172 | return( ret ); |
| 3173 | } |
Hanno Becker | ae39b9e | 2019-02-07 12:32:43 +0000 | [diff] [blame] | 3174 | #endif /* MBEDTLS_KEY_EXCHANGE__CERT_REQ_ALLOWED__ENABLED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3175 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3176 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ |
| 3177 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) |
Hanno Becker | b3a2448 | 2019-09-01 09:47:23 +0100 | [diff] [blame] | 3178 | |
| 3179 | #if defined(MBEDTLS_USE_TINYCRYPT) |
| 3180 | static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl ) |
| 3181 | { |
| 3182 | mbedtls_uecc_keypair *own_key = |
| 3183 | mbedtls_pk_uecc( *mbedtls_ssl_own_key( ssl ) ); |
| 3184 | |
| 3185 | if( ! mbedtls_pk_can_do( mbedtls_ssl_own_key( ssl ), MBEDTLS_PK_ECKEY ) ) |
| 3186 | { |
| 3187 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) ); |
| 3188 | return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH ); |
| 3189 | } |
| 3190 | |
| 3191 | memcpy( ssl->handshake->ecdh_privkey, |
| 3192 | own_key->private_key, |
| 3193 | sizeof( ssl->handshake->ecdh_privkey ) ); |
| 3194 | |
| 3195 | return( 0 ); |
| 3196 | } |
| 3197 | #else /* MBEDTLS_USE_TINYCRYPT */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3198 | 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] | 3199 | { |
| 3200 | int ret; |
| 3201 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3202 | 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] | 3203 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3204 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) ); |
| 3205 | return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH ); |
Manuel Pégourié-Gonnard | 5538970 | 2013-12-12 11:14:16 +0100 | [diff] [blame] | 3206 | } |
| 3207 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3208 | if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx, |
| 3209 | mbedtls_pk_ec( *mbedtls_ssl_own_key( ssl ) ), |
| 3210 | MBEDTLS_ECDH_OURS ) ) != 0 ) |
Manuel Pégourié-Gonnard | 5538970 | 2013-12-12 11:14:16 +0100 | [diff] [blame] | 3211 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3212 | MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret ); |
Manuel Pégourié-Gonnard | 5538970 | 2013-12-12 11:14:16 +0100 | [diff] [blame] | 3213 | return( ret ); |
| 3214 | } |
| 3215 | |
| 3216 | return( 0 ); |
| 3217 | } |
Hanno Becker | b3a2448 | 2019-09-01 09:47:23 +0100 | [diff] [blame] | 3218 | #endif /* MBEDTLS_USE_TINYCRYPT */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3219 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || |
| 3220 | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */ |
Manuel Pégourié-Gonnard | 5538970 | 2013-12-12 11:14:16 +0100 | [diff] [blame] | 3221 | |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3222 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) && \ |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3223 | defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3224 | static int ssl_resume_server_key_exchange( mbedtls_ssl_context *ssl, |
Gilles Peskine | 0fd90dd | 2018-04-26 07:41:09 +0200 | [diff] [blame] | 3225 | size_t *signature_len ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3226 | { |
Gilles Peskine | 0fd90dd | 2018-04-26 07:41:09 +0200 | [diff] [blame] | 3227 | /* Append the signature to ssl->out_msg, leaving 2 bytes for the |
| 3228 | * signature length which will be added in ssl_write_server_key_exchange |
| 3229 | * after the call to ssl_prepare_server_key_exchange. |
| 3230 | * ssl_write_server_key_exchange also takes care of incrementing |
| 3231 | * ssl->out_msglen. */ |
| 3232 | unsigned char *sig_start = ssl->out_msg + ssl->out_msglen + 2; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3233 | 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] | 3234 | - sig_start ); |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 3235 | int ret = ssl->conf->f_async_resume( ssl, |
Gilles Peskine | 0fd90dd | 2018-04-26 07:41:09 +0200 | [diff] [blame] | 3236 | sig_start, signature_len, sig_max_len ); |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3237 | if( ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS ) |
| 3238 | { |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3239 | ssl->handshake->async_in_progress = 0; |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 3240 | mbedtls_ssl_set_async_operation_data( ssl, NULL ); |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3241 | } |
Gilles Peskine | d3eb061 | 2018-01-08 17:07:44 +0100 | [diff] [blame] | 3242 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl_resume_server_key_exchange", ret ); |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3243 | return( ret ); |
| 3244 | } |
| 3245 | #endif /* defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) && |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3246 | defined(MBEDTLS_SSL_ASYNC_PRIVATE) */ |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3247 | |
Gilles Peskine | d3eb061 | 2018-01-08 17:07:44 +0100 | [diff] [blame] | 3248 | /* Prepare the ServerKeyExchange message, up to and including |
Gilles Peskine | 168dae8 | 2018-04-25 23:35:42 +0200 | [diff] [blame] | 3249 | * calculating the signature if any, but excluding formatting the |
| 3250 | * signature and sending the message. */ |
Gilles Peskine | 7ab013a | 2018-01-08 17:04:16 +0100 | [diff] [blame] | 3251 | static int ssl_prepare_server_key_exchange( mbedtls_ssl_context *ssl, |
| 3252 | size_t *signature_len ) |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 3253 | { |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 3254 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = |
Hanno Becker | df64596 | 2019-06-26 13:02:22 +0100 | [diff] [blame] | 3255 | mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 3256 | |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3257 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME_PFS__ENABLED) |
Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3258 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) |
Gilles Peskine | 3ce9b90 | 2018-01-06 01:34:21 +0100 | [diff] [blame] | 3259 | unsigned char *dig_signed = NULL; |
Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3260 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED */ |
| 3261 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME_PFS__ENABLED */ |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 3262 | #if defined(MBEDTLS_USE_TINYCRYPT) |
| 3263 | const struct uECC_Curve_t * uecc_curve = uECC_secp256r1(); |
| 3264 | #endif |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3265 | |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3266 | (void) ciphersuite_info; /* unused in some configurations */ |
Gilles Peskine | 22e695f | 2018-04-26 00:22:50 +0200 | [diff] [blame] | 3267 | #if !defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) |
| 3268 | (void) signature_len; |
| 3269 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED */ |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3270 | |
Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 3271 | 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] | 3272 | |
Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3273 | /* |
| 3274 | * |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3275 | * Part 1: Provide key exchange parameters for chosen ciphersuite. |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3276 | * |
| 3277 | */ |
| 3278 | |
| 3279 | /* |
| 3280 | * - ECJPAKE key exchanges |
| 3281 | */ |
Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 3282 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 3283 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 3284 | == MBEDTLS_KEY_EXCHANGE_ECJPAKE ) |
Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 3285 | { |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3286 | int ret; |
Simon Butcher | 600c5e6 | 2018-06-14 08:58:59 +0100 | [diff] [blame] | 3287 | size_t len = 0; |
Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 3288 | |
Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 3289 | ret = mbedtls_ecjpake_write_round_two( |
| 3290 | &ssl->handshake->ecjpake_ctx, |
| 3291 | ssl->out_msg + ssl->out_msglen, |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3292 | MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen, &len, |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 3293 | mbedtls_ssl_conf_get_frng( ssl->conf ), |
Hanno Becker | 9a12243 | 2019-07-23 13:24:02 +0100 | [diff] [blame] | 3294 | mbedtls_ssl_conf_get_prng( ssl->conf ) ); |
Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 3295 | if( ret != 0 ) |
| 3296 | { |
| 3297 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret ); |
| 3298 | return( ret ); |
| 3299 | } |
| 3300 | |
Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 3301 | ssl->out_msglen += len; |
Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 3302 | } |
| 3303 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
| 3304 | |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3305 | /* |
| 3306 | * For (EC)DHE key exchanges with PSK, parameters are prefixed by support |
| 3307 | * identity hint (RFC 4279, Sec. 3). Until someone needs this feature, |
| 3308 | * we use empty support identity hints here. |
| 3309 | **/ |
| 3310 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) || \ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3311 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 3312 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) == |
| 3313 | MBEDTLS_KEY_EXCHANGE_DHE_PSK || |
| 3314 | mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) == |
| 3315 | MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3316 | { |
Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 3317 | ssl->out_msg[ssl->out_msglen++] = 0x00; |
| 3318 | ssl->out_msg[ssl->out_msglen++] = 0x00; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3319 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3320 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED || |
| 3321 | MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3322 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 3323 | /* |
Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3324 | * - DHE key exchanges |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3325 | */ |
| 3326 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__DHE_ENABLED) |
| 3327 | if( mbedtls_ssl_ciphersuite_uses_dhe( ciphersuite_info ) ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3328 | { |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3329 | int ret; |
Simon Butcher | 600c5e6 | 2018-06-14 08:58:59 +0100 | [diff] [blame] | 3330 | size_t len = 0; |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3331 | |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 3332 | if( ssl->conf->dhm_P.p == NULL || ssl->conf->dhm_G.p == NULL ) |
| 3333 | { |
| 3334 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no DH parameters set" ) ); |
| 3335 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3336 | } |
| 3337 | |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3338 | /* |
| 3339 | * Ephemeral DH parameters: |
| 3340 | * |
| 3341 | * struct { |
| 3342 | * opaque dh_p<1..2^16-1>; |
| 3343 | * opaque dh_g<1..2^16-1>; |
| 3344 | * opaque dh_Ys<1..2^16-1>; |
| 3345 | * } ServerDHParams; |
| 3346 | */ |
Hanno Becker | ab74056 | 2017-10-04 13:15:37 +0100 | [diff] [blame] | 3347 | if( ( ret = mbedtls_dhm_set_group( &ssl->handshake->dhm_ctx, |
| 3348 | &ssl->conf->dhm_P, |
| 3349 | &ssl->conf->dhm_G ) ) != 0 ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3350 | { |
Hanno Becker | ab74056 | 2017-10-04 13:15:37 +0100 | [diff] [blame] | 3351 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_set_group", ret ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3352 | return( ret ); |
| 3353 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3354 | |
Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 3355 | if( ( ret = mbedtls_dhm_make_params( |
| 3356 | &ssl->handshake->dhm_ctx, |
| 3357 | (int) mbedtls_mpi_size( &ssl->handshake->dhm_ctx.P ), |
| 3358 | ssl->out_msg + ssl->out_msglen, &len, |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 3359 | mbedtls_ssl_conf_get_frng( ssl->conf ), |
Hanno Becker | 9a12243 | 2019-07-23 13:24:02 +0100 | [diff] [blame] | 3360 | mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3361 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3362 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_params", ret ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3363 | return( ret ); |
| 3364 | } |
| 3365 | |
Hanno Becker | 4cb1f4d | 2017-10-10 15:59:57 +0100 | [diff] [blame] | 3366 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) |
Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 3367 | dig_signed = ssl->out_msg + ssl->out_msglen; |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3368 | #endif |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3369 | |
Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 3370 | ssl->out_msglen += len; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3371 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3372 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X ); |
| 3373 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P ); |
| 3374 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G ); |
| 3375 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3376 | } |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3377 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__DHE_ENABLED */ |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3378 | |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3379 | /* |
Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3380 | * - ECDHE key exchanges |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3381 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3382 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__ECDHE_ENABLED) |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3383 | if( mbedtls_ssl_ciphersuite_uses_ecdhe( ciphersuite_info ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3384 | { |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3385 | /* |
| 3386 | * Ephemeral ECDH parameters: |
| 3387 | * |
| 3388 | * struct { |
| 3389 | * ECParameters curve_params; |
| 3390 | * ECPoint public; |
| 3391 | * } ServerECDHParams; |
| 3392 | */ |
Gergely Budai | 987bfb5 | 2014-01-19 21:48:42 +0100 | [diff] [blame] | 3393 | |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 3394 | #if defined(MBEDTLS_USE_TINYCRYPT) |
Gergely Budai | 987bfb5 | 2014-01-19 21:48:42 +0100 | [diff] [blame] | 3395 | { |
Hanno Becker | 9cf087d | 2019-07-24 11:19:03 +0100 | [diff] [blame] | 3396 | static const unsigned char ecdh_param_hdr[] = { |
Hanno Becker | 27b7e50 | 2019-08-23 14:39:50 +0100 | [diff] [blame] | 3397 | MBEDTLS_SSL_EC_TLS_NAMED_CURVE, |
Hanno Becker | b72fc6a | 2019-07-24 15:23:37 +0100 | [diff] [blame] | 3398 | 0 /* high bits of secp256r1 TLS ID */, |
| 3399 | 23 /* low bits of secp256r1 TLS ID */, |
Hanno Becker | 9cf087d | 2019-07-24 11:19:03 +0100 | [diff] [blame] | 3400 | 2 * NUM_ECC_BYTES + 1, |
| 3401 | 0x04 /* Uncompressed */ |
| 3402 | }; |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3403 | |
Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3404 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) |
Hanno Becker | 9cf087d | 2019-07-24 11:19:03 +0100 | [diff] [blame] | 3405 | dig_signed = ssl->out_msg + ssl->out_msglen; |
Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3406 | #endif |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3407 | |
Hanno Becker | 9cf087d | 2019-07-24 11:19:03 +0100 | [diff] [blame] | 3408 | memcpy( ssl->out_msg + ssl->out_msglen, |
| 3409 | ecdh_param_hdr, sizeof( ecdh_param_hdr ) ); |
| 3410 | ssl->out_msglen += sizeof( ecdh_param_hdr ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3411 | |
Hanno Becker | 8295ff0 | 2019-07-24 12:45:52 +0100 | [diff] [blame] | 3412 | if( !uECC_make_key( &ssl->out_msg[ ssl->out_msglen ], |
| 3413 | ssl->handshake->ecdh_privkey, |
| 3414 | uecc_curve ) ) |
| 3415 | { |
| 3416 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Key creation failed" ) ); |
| 3417 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3418 | } |
| 3419 | |
Hanno Becker | 9cf087d | 2019-07-24 11:19:03 +0100 | [diff] [blame] | 3420 | ssl->out_msglen += 2*NUM_ECC_BYTES; |
Hanno Becker | d849c7c | 2019-07-23 15:59:58 +0100 | [diff] [blame] | 3421 | } |
Hanno Becker | 40ee0d4 | 2019-09-01 09:40:53 +0100 | [diff] [blame] | 3422 | #else /* MBEDTLS_USE_TINYCRYPT */ |
Hanno Becker | 9cf087d | 2019-07-24 11:19:03 +0100 | [diff] [blame] | 3423 | { |
| 3424 | const mbedtls_ecp_curve_info *curve = |
| 3425 | mbedtls_ecp_curve_info_from_tls_id( ssl->handshake->curve_tls_id ); |
| 3426 | int ret; |
| 3427 | size_t len = 0; |
| 3428 | |
| 3429 | if( curve == NULL ) |
| 3430 | { |
| 3431 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) ); |
| 3432 | return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN ); |
| 3433 | } |
| 3434 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", curve->name ) ); |
| 3435 | |
| 3436 | if( ( ret = mbedtls_ecdh_setup( &ssl->handshake->ecdh_ctx, |
| 3437 | curve->grp_id ) ) != 0 ) |
| 3438 | { |
| 3439 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecp_group_load", ret ); |
| 3440 | return( ret ); |
| 3441 | } |
| 3442 | |
| 3443 | if( ( ret = mbedtls_ecdh_make_params( |
| 3444 | &ssl->handshake->ecdh_ctx, &len, |
| 3445 | ssl->out_msg + ssl->out_msglen, |
| 3446 | MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen, |
| 3447 | mbedtls_ssl_conf_get_frng( ssl->conf ), |
| 3448 | mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 ) |
| 3449 | { |
| 3450 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_params", ret ); |
| 3451 | return( ret ); |
| 3452 | } |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 3453 | |
Hanno Becker | d849c7c | 2019-07-23 15:59:58 +0100 | [diff] [blame] | 3454 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) |
Hanno Becker | 9cf087d | 2019-07-24 11:19:03 +0100 | [diff] [blame] | 3455 | dig_signed = ssl->out_msg + ssl->out_msglen; |
Hanno Becker | d849c7c | 2019-07-23 15:59:58 +0100 | [diff] [blame] | 3456 | #endif |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 3457 | |
Hanno Becker | 9cf087d | 2019-07-24 11:19:03 +0100 | [diff] [blame] | 3458 | ssl->out_msglen += len; |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 3459 | |
Hanno Becker | 9cf087d | 2019-07-24 11:19:03 +0100 | [diff] [blame] | 3460 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 3461 | MBEDTLS_DEBUG_ECDH_Q ); |
Paul Bakker | 2292d1f | 2013-09-15 17:06:49 +0200 | [diff] [blame] | 3462 | } |
Hanno Becker | 40ee0d4 | 2019-09-01 09:40:53 +0100 | [diff] [blame] | 3463 | #endif /* MBEDTLS_USE_TINYCRYPT */ |
Manuel Pégourié-Gonnard | abae74c | 2013-08-20 13:53:44 +0200 | [diff] [blame] | 3464 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3465 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__ECDHE_ENABLED */ |
Manuel Pégourié-Gonnard | abae74c | 2013-08-20 13:53:44 +0200 | [diff] [blame] | 3466 | |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3467 | /* |
Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3468 | * |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3469 | * Part 2: For key exchanges involving the server signing the |
Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3470 | * exchange parameters, compute and add the signature here. |
| 3471 | * |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3472 | */ |
| 3473 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) |
| 3474 | if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) ) |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 3475 | { |
Gilles Peskine | 1004c19 | 2018-01-08 16:59:14 +0100 | [diff] [blame] | 3476 | 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] | 3477 | size_t hashlen = 0; |
Gilles Peskine | e1efdf9 | 2018-01-05 21:18:37 +0100 | [diff] [blame] | 3478 | unsigned char hash[MBEDTLS_MD_MAX_SIZE]; |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3479 | int ret; |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 3480 | |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3481 | /* |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3482 | * 2.1: Choose hash algorithm: |
Hanno Becker | 4cb1f4d | 2017-10-10 15:59:57 +0100 | [diff] [blame] | 3483 | * A: For TLS 1.2, obey signature-hash-algorithm extension |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3484 | * to choose appropriate hash. |
| 3485 | * B: For SSL3, TLS1.0, TLS1.1 and ECDHE_ECDSA, use SHA1 |
| 3486 | * (RFC 4492, Sec. 5.4) |
| 3487 | * 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] | 3488 | */ |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 3489 | |
| 3490 | mbedtls_md_type_t md_alg; |
| 3491 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3492 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3493 | mbedtls_pk_type_t sig_alg = |
| 3494 | mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ); |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 3495 | 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] | 3496 | { |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3497 | /* A: For TLS 1.2, obey signature-hash-algorithm extension |
| 3498 | * (RFC 5246, Sec. 7.4.1.4.1). */ |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 3499 | if( sig_alg == MBEDTLS_PK_NONE || |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3500 | ( md_alg = mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs, |
| 3501 | sig_alg ) ) == MBEDTLS_MD_NONE ) |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3502 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3503 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Hanno Becker | 4cb1f4d | 2017-10-10 15:59:57 +0100 | [diff] [blame] | 3504 | /* (... because we choose a cipher suite |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3505 | * only if there is a matching hash.) */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3506 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3507 | } |
| 3508 | } |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 3509 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3510 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3511 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 3512 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 3513 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 3514 | == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ) |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3515 | { |
Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3516 | /* B: Default hash SHA1 */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3517 | md_alg = MBEDTLS_MD_SHA1; |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3518 | } |
| 3519 | else |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3520 | #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \ |
| 3521 | MBEDTLS_SSL_PROTO_TLS1_1 */ |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3522 | { |
Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3523 | /* C: MD5 + SHA1 */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3524 | md_alg = MBEDTLS_MD_NONE; |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3525 | } |
| 3526 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 3527 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "pick hash algorithm %d for signing", md_alg ) ); |
| 3528 | |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3529 | /* |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3530 | * 2.2: Compute the hash to be signed |
Manuel Pégourié-Gonnard | abae74c | 2013-08-20 13:53:44 +0200 | [diff] [blame] | 3531 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3532 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 3533 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 3534 | if( md_alg == MBEDTLS_MD_NONE ) |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 3535 | { |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3536 | hashlen = 36; |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 3537 | ret = mbedtls_ssl_get_key_exchange_md_ssl_tls( ssl, hash, |
| 3538 | dig_signed, |
| 3539 | dig_signed_len ); |
| 3540 | if( ret != 0 ) |
| 3541 | return( ret ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3542 | } |
| 3543 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3544 | #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \ |
| 3545 | MBEDTLS_SSL_PROTO_TLS1_1 */ |
| 3546 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 3547 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3548 | if( md_alg != MBEDTLS_MD_NONE ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3549 | { |
Gilles Peskine | ca1d742 | 2018-04-24 11:53:22 +0200 | [diff] [blame] | 3550 | 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] | 3551 | dig_signed, |
| 3552 | dig_signed_len, |
| 3553 | md_alg ); |
| 3554 | if( ret != 0 ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3555 | return( ret ); |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 3556 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 3557 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3558 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 3559 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 3560 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3561 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3562 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 3563 | } |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 3564 | |
Gilles Peskine | ebd652f | 2018-01-05 21:18:59 +0100 | [diff] [blame] | 3565 | MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3566 | |
Manuel Pégourié-Gonnard | abae74c | 2013-08-20 13:53:44 +0200 | [diff] [blame] | 3567 | /* |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3568 | * 2.3: Compute and add the signature |
Manuel Pégourié-Gonnard | abae74c | 2013-08-20 13:53:44 +0200 | [diff] [blame] | 3569 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3570 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 3571 | if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 3572 | { |
Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3573 | /* |
| 3574 | * For TLS 1.2, we need to specify signature and hash algorithm |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 3575 | * explicitly through a prefix to the signature. |
| 3576 | * |
| 3577 | * struct { |
| 3578 | * HashAlgorithm hash; |
| 3579 | * SignatureAlgorithm signature; |
| 3580 | * } SignatureAndHashAlgorithm; |
| 3581 | * |
| 3582 | * struct { |
| 3583 | * SignatureAndHashAlgorithm algorithm; |
| 3584 | * opaque signature<0..2^16-1>; |
| 3585 | * } DigitallySigned; |
| 3586 | * |
| 3587 | */ |
| 3588 | |
Gilles Peskine | 1004c19 | 2018-01-08 16:59:14 +0100 | [diff] [blame] | 3589 | ssl->out_msg[ssl->out_msglen++] = |
| 3590 | mbedtls_ssl_hash_from_md_alg( md_alg ); |
| 3591 | ssl->out_msg[ssl->out_msglen++] = |
| 3592 | mbedtls_ssl_sig_from_pk_alg( sig_alg ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3593 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3594 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3595 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3596 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 4bf9a28 | 2018-01-05 21:20:50 +0100 | [diff] [blame] | 3597 | if( ssl->conf->f_async_sign_start != NULL ) |
| 3598 | { |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 3599 | ret = ssl->conf->f_async_sign_start( ssl, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3600 | mbedtls_ssl_own_cert( ssl ), |
| 3601 | md_alg, hash, hashlen ); |
Gilles Peskine | 4bf9a28 | 2018-01-05 21:20:50 +0100 | [diff] [blame] | 3602 | switch( ret ) |
| 3603 | { |
| 3604 | case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH: |
| 3605 | /* act as if f_async_sign was null */ |
| 3606 | break; |
| 3607 | case 0: |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3608 | ssl->handshake->async_in_progress = 1; |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3609 | return( ssl_resume_server_key_exchange( ssl, signature_len ) ); |
Gilles Peskine | 4bf9a28 | 2018-01-05 21:20:50 +0100 | [diff] [blame] | 3610 | case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS: |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3611 | ssl->handshake->async_in_progress = 1; |
Gilles Peskine | 4bf9a28 | 2018-01-05 21:20:50 +0100 | [diff] [blame] | 3612 | return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS ); |
| 3613 | default: |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3614 | MBEDTLS_SSL_DEBUG_RET( 1, "f_async_sign_start", ret ); |
Gilles Peskine | 4bf9a28 | 2018-01-05 21:20:50 +0100 | [diff] [blame] | 3615 | return( ret ); |
| 3616 | } |
| 3617 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3618 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 4bf9a28 | 2018-01-05 21:20:50 +0100 | [diff] [blame] | 3619 | |
| 3620 | if( mbedtls_ssl_own_key( ssl ) == NULL ) |
| 3621 | { |
| 3622 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key" ) ); |
| 3623 | return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ); |
| 3624 | } |
| 3625 | |
Gilles Peskine | 0fd90dd | 2018-04-26 07:41:09 +0200 | [diff] [blame] | 3626 | /* Append the signature to ssl->out_msg, leaving 2 bytes for the |
| 3627 | * signature length which will be added in ssl_write_server_key_exchange |
| 3628 | * after the call to ssl_prepare_server_key_exchange. |
| 3629 | * ssl_write_server_key_exchange also takes care of incrementing |
| 3630 | * ssl->out_msglen. */ |
Gilles Peskine | 1004c19 | 2018-01-08 16:59:14 +0100 | [diff] [blame] | 3631 | if( ( ret = mbedtls_pk_sign( mbedtls_ssl_own_key( ssl ), |
| 3632 | md_alg, hash, hashlen, |
| 3633 | ssl->out_msg + ssl->out_msglen + 2, |
Gilles Peskine | 7ab013a | 2018-01-08 17:04:16 +0100 | [diff] [blame] | 3634 | signature_len, |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 3635 | mbedtls_ssl_conf_get_frng( ssl->conf ), |
Hanno Becker | 9a12243 | 2019-07-23 13:24:02 +0100 | [diff] [blame] | 3636 | mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3637 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3638 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 3639 | return( ret ); |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 3640 | } |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3641 | } |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3642 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3643 | |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3644 | return( 0 ); |
| 3645 | } |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3646 | |
Gilles Peskine | d3eb061 | 2018-01-08 17:07:44 +0100 | [diff] [blame] | 3647 | /* Prepare the ServerKeyExchange message and send it. For ciphersuites |
Gilles Peskine | 168dae8 | 2018-04-25 23:35:42 +0200 | [diff] [blame] | 3648 | * that do not include a ServerKeyExchange message, do nothing. Either |
| 3649 | * way, if successful, move on to the next step in the SSL state |
| 3650 | * machine. */ |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3651 | static int ssl_write_server_key_exchange( mbedtls_ssl_context *ssl ) |
| 3652 | { |
| 3653 | int ret; |
Gilles Peskine | 7ab013a | 2018-01-08 17:04:16 +0100 | [diff] [blame] | 3654 | size_t signature_len = 0; |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3655 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME_NON_PFS__ENABLED) |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 3656 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = |
Hanno Becker | df64596 | 2019-06-26 13:02:22 +0100 | [diff] [blame] | 3657 | mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); |
Gilles Peskine | f112725 | 2018-04-24 13:05:39 +0200 | [diff] [blame] | 3658 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME_NON_PFS__ENABLED */ |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3659 | |
Gilles Peskine | d3eb061 | 2018-01-08 17:07:44 +0100 | [diff] [blame] | 3660 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) ); |
| 3661 | |
| 3662 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME_NON_PFS__ENABLED) |
| 3663 | /* Extract static ECDH parameters and abort if ServerKeyExchange |
| 3664 | * is not needed. */ |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3665 | if( mbedtls_ssl_ciphersuite_no_pfs( ciphersuite_info ) ) |
| 3666 | { |
| 3667 | /* For suites involving ECDH, extract DH parameters |
| 3668 | * from certificate at this point. */ |
| 3669 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__ECDH_ENABLED) |
| 3670 | if( mbedtls_ssl_ciphersuite_uses_ecdh( ciphersuite_info ) ) |
| 3671 | { |
| 3672 | ssl_get_ecdh_params_from_cert( ssl ); |
| 3673 | } |
| 3674 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__ECDH_ENABLED */ |
| 3675 | |
| 3676 | /* Key exchanges not involving ephemeral keys don't use |
| 3677 | * ServerKeyExchange, so end here. */ |
| 3678 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) ); |
| 3679 | ssl->state++; |
| 3680 | return( 0 ); |
| 3681 | } |
Gilles Peskine | f112725 | 2018-04-24 13:05:39 +0200 | [diff] [blame] | 3682 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME_NON_PFS__ENABLED */ |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3683 | |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3684 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) && \ |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3685 | defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | d3eb061 | 2018-01-08 17:07:44 +0100 | [diff] [blame] | 3686 | /* If we have already prepared the message and there is an ongoing |
Gilles Peskine | 168dae8 | 2018-04-25 23:35:42 +0200 | [diff] [blame] | 3687 | * signature operation, resume signing. */ |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3688 | if( ssl->handshake->async_in_progress != 0 ) |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3689 | { |
| 3690 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "resuming signature operation" ) ); |
| 3691 | ret = ssl_resume_server_key_exchange( ssl, &signature_len ); |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3692 | } |
| 3693 | else |
| 3694 | #endif /* defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) && |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3695 | defined(MBEDTLS_SSL_ASYNC_PRIVATE) */ |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3696 | { |
| 3697 | /* ServerKeyExchange is needed. Prepare the message. */ |
| 3698 | ret = ssl_prepare_server_key_exchange( ssl, &signature_len ); |
Gilles Peskine | d3eb061 | 2018-01-08 17:07:44 +0100 | [diff] [blame] | 3699 | } |
| 3700 | |
| 3701 | if( ret != 0 ) |
| 3702 | { |
Gilles Peskine | ad28bf0 | 2018-04-26 00:19:16 +0200 | [diff] [blame] | 3703 | /* If we're starting to write a new message, set ssl->out_msglen |
| 3704 | * to 0. But if we're resuming after an asynchronous message, |
| 3705 | * out_msglen is the amount of data written so far and mst be |
| 3706 | * preserved. */ |
Gilles Peskine | d3eb061 | 2018-01-08 17:07:44 +0100 | [diff] [blame] | 3707 | if( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS ) |
| 3708 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server key exchange (pending)" ) ); |
| 3709 | else |
| 3710 | ssl->out_msglen = 0; |
| 3711 | return( ret ); |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3712 | } |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3713 | |
Gilles Peskine | 7ab013a | 2018-01-08 17:04:16 +0100 | [diff] [blame] | 3714 | /* If there is a signature, write its length. |
Gilles Peskine | 168dae8 | 2018-04-25 23:35:42 +0200 | [diff] [blame] | 3715 | * ssl_prepare_server_key_exchange already wrote the signature |
| 3716 | * itself at its proper place in the output buffer. */ |
Gilles Peskine | 7ab013a | 2018-01-08 17:04:16 +0100 | [diff] [blame] | 3717 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) |
| 3718 | if( signature_len != 0 ) |
| 3719 | { |
Arto Kinnunen | 3d7439e | 2019-09-10 11:30:40 +0300 | [diff] [blame^] | 3720 | (void)mbedtls_platform_put_uint16_be( &ssl->out_msg[ssl->out_msglen], |
| 3721 | signature_len ); |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 3722 | ssl->out_msglen += 2; |
Gilles Peskine | 7ab013a | 2018-01-08 17:04:16 +0100 | [diff] [blame] | 3723 | |
| 3724 | MBEDTLS_SSL_DEBUG_BUF( 3, "my signature", |
| 3725 | ssl->out_msg + ssl->out_msglen, |
| 3726 | signature_len ); |
| 3727 | |
| 3728 | /* Skip over the already-written signature */ |
| 3729 | ssl->out_msglen += signature_len; |
| 3730 | } |
| 3731 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED */ |
| 3732 | |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3733 | /* Add header and send. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3734 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 3735 | ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3736 | |
| 3737 | ssl->state++; |
| 3738 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3739 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3740 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3741 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3742 | return( ret ); |
| 3743 | } |
| 3744 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3745 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3746 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3747 | } |
| 3748 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3749 | static int ssl_write_server_hello_done( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3750 | { |
| 3751 | int ret; |
| 3752 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3753 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3754 | |
| 3755 | ssl->out_msglen = 4; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3756 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 3757 | ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO_DONE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3758 | |
| 3759 | ssl->state++; |
| 3760 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3761 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 3762 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3763 | mbedtls_ssl_send_flight_completed( ssl ); |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3764 | #endif |
| 3765 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3766 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3767 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3768 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3769 | return( ret ); |
| 3770 | } |
| 3771 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3772 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 3773 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3774 | ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 3775 | { |
| 3776 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); |
| 3777 | return( ret ); |
| 3778 | } |
Hanno Becker | bc2498a | 2018-08-28 10:13:29 +0100 | [diff] [blame] | 3779 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3780 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3781 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3782 | |
| 3783 | return( 0 ); |
| 3784 | } |
| 3785 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3786 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \ |
| 3787 | defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 3788 | 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] | 3789 | const unsigned char *end ) |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3790 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3791 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3792 | size_t n; |
| 3793 | |
| 3794 | /* |
| 3795 | * Receive G^Y mod P, premaster = (G^Y)^X mod P |
| 3796 | */ |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3797 | if( *p + 2 > end ) |
| 3798 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3799 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); |
| 3800 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3801 | } |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3802 | |
Arto Kinnunen | a3fa06e | 2019-09-09 12:22:51 +0300 | [diff] [blame] | 3803 | n = mbedtls_platform_get_uint16_be ( *p ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3804 | *p += 2; |
| 3805 | |
Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 3806 | if( *p + n > end ) |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3807 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3808 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); |
| 3809 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3810 | } |
| 3811 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3812 | 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] | 3813 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3814 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_read_public", ret ); |
| 3815 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP ); |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3816 | } |
| 3817 | |
Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 3818 | *p += n; |
| 3819 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3820 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY ); |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3821 | |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3822 | return( ret ); |
| 3823 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3824 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED || |
| 3825 | MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3826 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3827 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \ |
| 3828 | defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3829 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3830 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3831 | static int ssl_resume_decrypt_pms( mbedtls_ssl_context *ssl, |
| 3832 | unsigned char *peer_pms, |
| 3833 | size_t *peer_pmslen, |
| 3834 | size_t peer_pmssize ) |
| 3835 | { |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 3836 | int ret = ssl->conf->f_async_resume( ssl, |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3837 | peer_pms, peer_pmslen, peer_pmssize ); |
| 3838 | if( ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS ) |
| 3839 | { |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3840 | ssl->handshake->async_in_progress = 0; |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 3841 | mbedtls_ssl_set_async_operation_data( ssl, NULL ); |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3842 | } |
| 3843 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl_decrypt_encrypted_pms", ret ); |
| 3844 | return( ret ); |
| 3845 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3846 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3847 | |
Gilles Peskine | bcd98a5 | 2018-01-11 21:30:40 +0100 | [diff] [blame] | 3848 | static int ssl_decrypt_encrypted_pms( mbedtls_ssl_context *ssl, |
| 3849 | const unsigned char *p, |
| 3850 | const unsigned char *end, |
| 3851 | unsigned char *peer_pms, |
| 3852 | size_t *peer_pmslen, |
| 3853 | size_t peer_pmssize ) |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3854 | { |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 3855 | int ret; |
Hanno Becker | 81bb4d0 | 2019-02-16 11:03:48 +0000 | [diff] [blame] | 3856 | 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] | 3857 | mbedtls_pk_context *private_key = mbedtls_ssl_own_key( ssl ); |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3858 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3859 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3860 | /* 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] | 3861 | * decryption operation, resume signing. */ |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3862 | if( ssl->handshake->async_in_progress != 0 ) |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3863 | { |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3864 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "resuming decryption operation" ) ); |
| 3865 | return( ssl_resume_decrypt_pms( ssl, |
| 3866 | peer_pms, peer_pmslen, peer_pmssize ) ); |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3867 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3868 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3869 | |
| 3870 | /* |
Gilles Peskine | 422ccab | 2018-01-11 18:29:01 +0100 | [diff] [blame] | 3871 | * Prepare to decrypt the premaster using own private RSA key |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3872 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3873 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 3874 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | 81bb4d0 | 2019-02-16 11:03:48 +0000 | [diff] [blame] | 3875 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 3876 | if( mbedtls_ssl_get_minor_ver( ssl ) != MBEDTLS_SSL_MINOR_VERSION_0 ) |
Hanno Becker | 81bb4d0 | 2019-02-16 11:03:48 +0000 | [diff] [blame] | 3877 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3878 | { |
Hanno Becker | 81bb4d0 | 2019-02-16 11:03:48 +0000 | [diff] [blame] | 3879 | if( len < 2 ) |
| 3880 | { |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 3881 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); |
| 3882 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
| 3883 | } |
Hanno Becker | 81bb4d0 | 2019-02-16 11:03:48 +0000 | [diff] [blame] | 3884 | len -= 2; |
| 3885 | |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 3886 | if( *p++ != ( ( len >> 8 ) & 0xFF ) || |
| 3887 | *p++ != ( ( len ) & 0xFF ) ) |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3888 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3889 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); |
| 3890 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3891 | } |
| 3892 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 3893 | #endif |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3894 | |
Gilles Peskine | 422ccab | 2018-01-11 18:29:01 +0100 | [diff] [blame] | 3895 | /* |
| 3896 | * Decrypt the premaster secret |
| 3897 | */ |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3898 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3899 | if( ssl->conf->f_async_decrypt_start != NULL ) |
| 3900 | { |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 3901 | ret = ssl->conf->f_async_decrypt_start( ssl, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3902 | mbedtls_ssl_own_cert( ssl ), |
| 3903 | p, len ); |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3904 | switch( ret ) |
| 3905 | { |
| 3906 | case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH: |
| 3907 | /* act as if f_async_decrypt_start was null */ |
| 3908 | break; |
| 3909 | case 0: |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3910 | ssl->handshake->async_in_progress = 1; |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3911 | return( ssl_resume_decrypt_pms( ssl, |
| 3912 | peer_pms, |
| 3913 | peer_pmslen, |
| 3914 | peer_pmssize ) ); |
| 3915 | case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS: |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3916 | ssl->handshake->async_in_progress = 1; |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3917 | return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS ); |
| 3918 | default: |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3919 | MBEDTLS_SSL_DEBUG_RET( 1, "f_async_decrypt_start", ret ); |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3920 | return( ret ); |
| 3921 | } |
| 3922 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3923 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3924 | |
Gilles Peskine | 422ccab | 2018-01-11 18:29:01 +0100 | [diff] [blame] | 3925 | if( ! mbedtls_pk_can_do( private_key, MBEDTLS_PK_RSA ) ) |
| 3926 | { |
Gilles Peskine | 422ccab | 2018-01-11 18:29:01 +0100 | [diff] [blame] | 3927 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) ); |
| 3928 | return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ); |
| 3929 | } |
| 3930 | |
| 3931 | ret = mbedtls_pk_decrypt( private_key, p, len, |
Gilles Peskine | bcd98a5 | 2018-01-11 21:30:40 +0100 | [diff] [blame] | 3932 | peer_pms, peer_pmslen, peer_pmssize, |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 3933 | mbedtls_ssl_conf_get_frng( ssl->conf ), |
Hanno Becker | 9a12243 | 2019-07-23 13:24:02 +0100 | [diff] [blame] | 3934 | mbedtls_ssl_conf_get_prng( ssl->conf ) ); |
Gilles Peskine | bcd98a5 | 2018-01-11 21:30:40 +0100 | [diff] [blame] | 3935 | return( ret ); |
| 3936 | } |
| 3937 | |
| 3938 | static int ssl_parse_encrypted_pms( mbedtls_ssl_context *ssl, |
| 3939 | const unsigned char *p, |
| 3940 | const unsigned char *end, |
| 3941 | size_t pms_offset ) |
| 3942 | { |
| 3943 | int ret; |
| 3944 | unsigned char *pms = ssl->handshake->premaster + pms_offset; |
| 3945 | unsigned char ver[2]; |
| 3946 | unsigned char fake_pms[48], peer_pms[48]; |
| 3947 | unsigned char mask; |
| 3948 | size_t i, peer_pmslen; |
| 3949 | unsigned int diff; |
| 3950 | |
Gilles Peskine | 0a8352b | 2018-06-13 18:16:41 +0200 | [diff] [blame] | 3951 | /* In case of a failure in decryption, the decryption may write less than |
| 3952 | * 2 bytes of output, but we always read the first two bytes. It doesn't |
| 3953 | * matter in the end because diff will be nonzero in that case due to |
| 3954 | * peer_pmslen being less than 48, and we only care whether diff is 0. |
| 3955 | * But do initialize peer_pms for robustness anyway. This also makes |
| 3956 | * memory analyzers happy (don't access uninitialized memory, even |
| 3957 | * if it's an unsigned char). */ |
| 3958 | peer_pms[0] = peer_pms[1] = ~0; |
| 3959 | |
Gilles Peskine | bcd98a5 | 2018-01-11 21:30:40 +0100 | [diff] [blame] | 3960 | ret = ssl_decrypt_encrypted_pms( ssl, p, end, |
| 3961 | peer_pms, |
| 3962 | &peer_pmslen, |
| 3963 | sizeof( peer_pms ) ); |
| 3964 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3965 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3966 | if ( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS ) |
| 3967 | return( ret ); |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3968 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3969 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3970 | mbedtls_ssl_write_version( ssl->handshake->max_major_ver, |
Gilles Peskine | 2e33337 | 2018-04-24 13:22:10 +0200 | [diff] [blame] | 3971 | ssl->handshake->max_minor_ver, |
| 3972 | ssl->conf->transport, ver ); |
| 3973 | |
| 3974 | /* Avoid data-dependent branches while checking for invalid |
| 3975 | * padding, to protect against timing-based Bleichenbacher-type |
| 3976 | * attacks. */ |
| 3977 | diff = (unsigned int) ret; |
| 3978 | diff |= peer_pmslen ^ 48; |
| 3979 | diff |= peer_pms[0] ^ ver[0]; |
| 3980 | diff |= peer_pms[1] ^ ver[1]; |
| 3981 | |
| 3982 | /* mask = diff ? 0xff : 0x00 using bit operations to avoid branches */ |
| 3983 | /* MSVC has a warning about unary minus on unsigned, but this is |
| 3984 | * well-defined and precisely what we want to do here */ |
| 3985 | #if defined(_MSC_VER) |
| 3986 | #pragma warning( push ) |
| 3987 | #pragma warning( disable : 4146 ) |
| 3988 | #endif |
| 3989 | mask = - ( ( diff | - diff ) >> ( sizeof( unsigned int ) * 8 - 1 ) ); |
| 3990 | #if defined(_MSC_VER) |
| 3991 | #pragma warning( pop ) |
| 3992 | #endif |
Manuel Pégourié-Gonnard | b9c93d0 | 2015-06-23 13:53:15 +0200 | [diff] [blame] | 3993 | |
Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 3994 | /* |
| 3995 | * Protection against Bleichenbacher's attack: invalid PKCS#1 v1.5 padding |
| 3996 | * must not cause the connection to end immediately; instead, send a |
| 3997 | * bad_record_mac later in the handshake. |
Gilles Peskine | bcd98a5 | 2018-01-11 21:30:40 +0100 | [diff] [blame] | 3998 | * To protect against timing-based variants of the attack, we must |
| 3999 | * not have any branch that depends on whether the decryption was |
| 4000 | * successful. In particular, always generate the fake premaster secret, |
| 4001 | * 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] | 4002 | */ |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 4003 | ret = mbedtls_ssl_conf_get_frng( ssl->conf ) |
Hanno Becker | 9a12243 | 2019-07-23 13:24:02 +0100 | [diff] [blame] | 4004 | ( 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] | 4005 | if( ret != 0 ) |
Gilles Peskine | bcd98a5 | 2018-01-11 21:30:40 +0100 | [diff] [blame] | 4006 | { |
Gilles Peskine | e141638 | 2018-04-26 10:23:21 +0200 | [diff] [blame] | 4007 | /* It's ok to abort on an RNG failure, since this does not reveal |
| 4008 | * anything about the RSA decryption. */ |
Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 4009 | return( ret ); |
Gilles Peskine | bcd98a5 | 2018-01-11 21:30:40 +0100 | [diff] [blame] | 4010 | } |
Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 4011 | |
Manuel Pégourié-Gonnard | 331ba57 | 2015-04-20 12:33:57 +0100 | [diff] [blame] | 4012 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Manuel Pégourié-Gonnard | ce60fbe | 2015-04-15 16:45:52 +0200 | [diff] [blame] | 4013 | if( diff != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4014 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); |
Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 4015 | #endif |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 4016 | |
Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 4017 | if( sizeof( ssl->handshake->premaster ) < pms_offset || |
| 4018 | sizeof( ssl->handshake->premaster ) - pms_offset < 48 ) |
| 4019 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4020 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 4021 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 4022 | } |
Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 4023 | ssl->handshake->pmslen = 48; |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 4024 | |
Gilles Peskine | 422ccab | 2018-01-11 18:29:01 +0100 | [diff] [blame] | 4025 | /* Set pms to either the true or the fake PMS, without |
| 4026 | * data-dependent branches. */ |
Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 4027 | for( i = 0; i < ssl->handshake->pmslen; i++ ) |
| 4028 | pms[i] = ( mask & fake_pms[i] ) | ( (~mask) & peer_pms[i] ); |
| 4029 | |
| 4030 | return( 0 ); |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 4031 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4032 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED || |
| 4033 | MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 4034 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4035 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
| 4036 | 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] | 4037 | const unsigned char *end ) |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4038 | { |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 4039 | int ret = 0; |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4040 | size_t n; |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4041 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4042 | if( ssl->conf->f_psk == NULL && |
| 4043 | ( ssl->conf->psk == NULL || ssl->conf->psk_identity == NULL || |
| 4044 | ssl->conf->psk_identity_len == 0 || ssl->conf->psk_len == 0 ) ) |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4045 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4046 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) ); |
| 4047 | return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ); |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4048 | } |
| 4049 | |
| 4050 | /* |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4051 | * Receive client pre-shared key identity name |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4052 | */ |
Hanno Becker | 83c9f49 | 2017-06-26 13:52:14 +0100 | [diff] [blame] | 4053 | if( end - *p < 2 ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4054 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4055 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); |
| 4056 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4057 | } |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4058 | |
Arto Kinnunen | a3fa06e | 2019-09-09 12:22:51 +0300 | [diff] [blame] | 4059 | n = mbedtls_platform_get_uint16_be( *p ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4060 | *p += 2; |
| 4061 | |
Hanno Becker | 83c9f49 | 2017-06-26 13:52:14 +0100 | [diff] [blame] | 4062 | if( n < 1 || n > 65535 || n > (size_t) ( end - *p ) ) |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4063 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4064 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); |
| 4065 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4066 | } |
| 4067 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4068 | if( ssl->conf->f_psk != NULL ) |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 4069 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4070 | 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] | 4071 | ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY; |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 4072 | } |
Manuel Pégourié-Gonnard | d27680b | 2014-07-08 14:15:55 +0200 | [diff] [blame] | 4073 | else |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 4074 | { |
Manuel Pégourié-Gonnard | 31ff1d2 | 2013-10-28 13:46:11 +0100 | [diff] [blame] | 4075 | /* Identity is not a big secret since clients send it in the clear, |
| 4076 | * but treat it carefully anyway, just in case */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4077 | if( n != ssl->conf->psk_identity_len || |
| 4078 | mbedtls_ssl_safer_memcmp( ssl->conf->psk_identity, *p, n ) != 0 ) |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 4079 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4080 | ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY; |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 4081 | } |
| 4082 | } |
| 4083 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4084 | if( ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY ) |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4085 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4086 | MBEDTLS_SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 4087 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 4088 | MBEDTLS_SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4089 | return( MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY ); |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4090 | } |
| 4091 | |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4092 | *p += n; |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4093 | |
Manuel Pégourié-Gonnard | d27680b | 2014-07-08 14:15:55 +0200 | [diff] [blame] | 4094 | return( 0 ); |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4095 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4096 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4097 | |
Hanno Becker | 7ec345d | 2018-05-21 17:16:42 +0100 | [diff] [blame] | 4098 | /* |
| 4099 | * |
| 4100 | * STATE HANDLING: Client Key Exchange |
| 4101 | * |
| 4102 | */ |
| 4103 | |
| 4104 | /* |
| 4105 | * Overview |
| 4106 | */ |
| 4107 | |
| 4108 | /* Main entry point; orchestrates the other functions. */ |
Hanno Becker | 7d552fa | 2019-08-12 15:04:59 +0100 | [diff] [blame] | 4109 | static int ssl_process_in_client_key_exchange( mbedtls_ssl_context *ssl ); |
Hanno Becker | 7ec345d | 2018-05-21 17:16:42 +0100 | [diff] [blame] | 4110 | |
Hanno Becker | 7d552fa | 2019-08-12 15:04:59 +0100 | [diff] [blame] | 4111 | static int ssl_in_client_key_exchange_parse( mbedtls_ssl_context *ssl, |
| 4112 | unsigned char *buf, |
| 4113 | size_t buflen ); |
Hanno Becker | 7ec345d | 2018-05-21 17:16:42 +0100 | [diff] [blame] | 4114 | /* Update the handshake state */ |
Hanno Becker | 7d552fa | 2019-08-12 15:04:59 +0100 | [diff] [blame] | 4115 | static int ssl_in_client_key_exchange_postprocess( mbedtls_ssl_context *ssl ); |
Hanno Becker | 7ec345d | 2018-05-21 17:16:42 +0100 | [diff] [blame] | 4116 | |
| 4117 | /* |
| 4118 | * Implementation |
| 4119 | */ |
| 4120 | |
Hanno Becker | 7d552fa | 2019-08-12 15:04:59 +0100 | [diff] [blame] | 4121 | static int ssl_process_in_client_key_exchange( mbedtls_ssl_context *ssl ) |
Hanno Becker | 7ec345d | 2018-05-21 17:16:42 +0100 | [diff] [blame] | 4122 | { |
| 4123 | int ret; |
| 4124 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> process client key exchange" ) ); |
| 4125 | |
| 4126 | /* The ClientKeyExchange message is never skipped. */ |
| 4127 | |
| 4128 | /* Reading step */ |
Hanno Becker | 1e23af8 | 2018-05-21 17:26:17 +0100 | [diff] [blame] | 4129 | if( ( ret = mbedtls_ssl_read_record( ssl, |
| 4130 | 1 /* update checksum */ ) ) != 0 ) |
Hanno Becker | 7ec345d | 2018-05-21 17:16:42 +0100 | [diff] [blame] | 4131 | { |
| 4132 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 4133 | return( ret ); |
| 4134 | } |
| 4135 | |
| 4136 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE || |
| 4137 | ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE ) |
| 4138 | { |
| 4139 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); |
| 4140 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 4141 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 4142 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 4143 | goto cleanup; |
| 4144 | } |
| 4145 | |
Hanno Becker | 7d552fa | 2019-08-12 15:04:59 +0100 | [diff] [blame] | 4146 | MBEDTLS_SSL_CHK( ssl_in_client_key_exchange_parse( ssl, ssl->in_msg, |
| 4147 | ssl->in_hslen ) ); |
Hanno Becker | 7ec345d | 2018-05-21 17:16:42 +0100 | [diff] [blame] | 4148 | |
| 4149 | /* Update state */ |
Hanno Becker | 7d552fa | 2019-08-12 15:04:59 +0100 | [diff] [blame] | 4150 | MBEDTLS_SSL_CHK( ssl_in_client_key_exchange_postprocess( ssl ) ); |
Hanno Becker | 7ec345d | 2018-05-21 17:16:42 +0100 | [diff] [blame] | 4151 | |
| 4152 | cleanup: |
| 4153 | |
Hanno Becker | 9db697e | 2019-07-23 12:19:48 +0100 | [diff] [blame] | 4154 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
| 4155 | if ( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS ) |
| 4156 | ssl->keep_current_message = 1; |
| 4157 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
| 4158 | |
Hanno Becker | 7ec345d | 2018-05-21 17:16:42 +0100 | [diff] [blame] | 4159 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= process client key exchange" ) ); |
| 4160 | return( ret ); |
| 4161 | } |
| 4162 | |
Hanno Becker | 4e46709 | 2019-08-12 15:12:35 +0100 | [diff] [blame] | 4163 | /* Warning: Despite accepting a length argument, this function is currently |
| 4164 | * still lacking some bounds checks and assumes that `buf` has length |
| 4165 | * `MBEDTLS_SSL_IN_CONTENT_LEN`. Eventually, it should be rewritten to work |
| 4166 | * with any buffer + length pair, returning MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL |
| 4167 | * on insufficient parsing space. */ |
Hanno Becker | 7d552fa | 2019-08-12 15:04:59 +0100 | [diff] [blame] | 4168 | static int ssl_in_client_key_exchange_parse( mbedtls_ssl_context *ssl, |
Hanno Becker | 7ec345d | 2018-05-21 17:16:42 +0100 | [diff] [blame] | 4169 | unsigned char *buf, |
| 4170 | size_t buflen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4171 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 4172 | int ret; |
Hanno Becker | df64596 | 2019-06-26 13:02:22 +0100 | [diff] [blame] | 4173 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = |
| 4174 | mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); |
Manuel Pégourié-Gonnard | 2114d72 | 2014-09-10 13:59:41 +0000 | [diff] [blame] | 4175 | unsigned char *p, *end; |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 4176 | |
Hanno Becker | e7c4eed | 2018-05-21 17:30:11 +0100 | [diff] [blame] | 4177 | p = buf + mbedtls_ssl_hs_hdr_len( ssl ); |
| 4178 | end = buf + buflen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4179 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4180 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 4181 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 4182 | == MBEDTLS_KEY_EXCHANGE_DHE_RSA ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4183 | { |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4184 | if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4185 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4186 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret ); |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 4187 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4188 | } |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4189 | |
Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 4190 | if( p != end ) |
| 4191 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4192 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) ); |
| 4193 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 4194 | } |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 4195 | } |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4196 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4197 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */ |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 4198 | #if defined(MBEDTLS_USE_TINYCRYPT) |
| 4199 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
Hanno Becker | b42e238 | 2019-09-01 07:49:04 +0100 | [diff] [blame] | 4200 | == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA || |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 4201 | mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
Hanno Becker | b42e238 | 2019-09-01 07:49:04 +0100 | [diff] [blame] | 4202 | == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA || |
| 4203 | mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 4204 | == MBEDTLS_KEY_EXCHANGE_ECDH_RSA || |
| 4205 | mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 4206 | == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA ) |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 4207 | { |
Hanno Becker | 7a19633 | 2019-07-24 11:12:41 +0100 | [diff] [blame] | 4208 | ((void) ret); |
Hanno Becker | a3c2c17 | 2019-07-23 16:51:57 +0100 | [diff] [blame] | 4209 | if( mbedtls_ssl_ecdh_read_peerkey( ssl, &p, end ) != 0 ) |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 4210 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 4211 | } |
| 4212 | else |
| 4213 | #endif |
Hanno Becker | 29d1655 | 2019-07-24 11:11:45 +0100 | [diff] [blame] | 4214 | #if defined(MBEDTLS_ECDH_C) && \ |
| 4215 | ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ |
| 4216 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \ |
| 4217 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ |
| 4218 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) ) |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 4219 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
Hanno Becker | e7c4eed | 2018-05-21 17:30:11 +0100 | [diff] [blame] | 4220 | == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA || |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 4221 | mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
Hanno Becker | e7c4eed | 2018-05-21 17:30:11 +0100 | [diff] [blame] | 4222 | == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA || |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 4223 | mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
Hanno Becker | e7c4eed | 2018-05-21 17:30:11 +0100 | [diff] [blame] | 4224 | == MBEDTLS_KEY_EXCHANGE_ECDH_RSA || |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 4225 | mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 4226 | == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA ) |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 4227 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4228 | if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx, |
Manuel Pégourié-Gonnard | 2114d72 | 2014-09-10 13:59:41 +0000 | [diff] [blame] | 4229 | p, end - p) ) != 0 ) |
Manuel Pégourié-Gonnard | b59d699 | 2013-10-14 12:00:45 +0200 | [diff] [blame] | 4230 | { |
Hanno Becker | 7ba0a88 | 2019-07-24 17:07:41 +0100 | [diff] [blame] | 4231 | ((void) ret); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4232 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret ); |
| 4233 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP ); |
Manuel Pégourié-Gonnard | b59d699 | 2013-10-14 12:00:45 +0200 | [diff] [blame] | 4234 | } |
| 4235 | |
Janos Follath | 3fbdada | 2018-08-15 10:26:53 +0100 | [diff] [blame] | 4236 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 4237 | MBEDTLS_DEBUG_ECDH_QP ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4238 | } |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4239 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4240 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || |
| 4241 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED || |
| 4242 | MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED || |
| 4243 | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */ |
| 4244 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
Hanno Becker | e7c4eed | 2018-05-21 17:30:11 +0100 | [diff] [blame] | 4245 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 4246 | == MBEDTLS_KEY_EXCHANGE_PSK ) |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4247 | { |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4248 | if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 ) |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4249 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4250 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret ); |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4251 | return( ret ); |
| 4252 | } |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4253 | |
Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 4254 | if( p != end ) |
| 4255 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4256 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) ); |
| 4257 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 4258 | } |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4259 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4260 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4261 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
| 4262 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
Hanno Becker | e7c4eed | 2018-05-21 17:30:11 +0100 | [diff] [blame] | 4263 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 4264 | == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 4265 | { |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 4266 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4267 | if ( ssl->handshake->async_in_progress != 0 ) |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 4268 | { |
| 4269 | /* There is an asynchronous operation in progress to |
| 4270 | * decrypt the encrypted premaster secret, so skip |
| 4271 | * directly to resuming this operation. */ |
| 4272 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "PSK identity already parsed" ) ); |
| 4273 | /* Update p to skip the PSK identity. ssl_parse_encrypted_pms |
| 4274 | * won't actually use it, but maintain p anyway for robustness. */ |
| 4275 | p += ssl->conf->psk_identity_len + 2; |
| 4276 | } |
| 4277 | else |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 4278 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 4279 | if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 ) |
| 4280 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4281 | 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] | 4282 | return( ret ); |
| 4283 | } |
| 4284 | |
| 4285 | if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 ) |
| 4286 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4287 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret ); |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 4288 | return( ret ); |
| 4289 | } |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 4290 | } |
| 4291 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4292 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
| 4293 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
Hanno Becker | e7c4eed | 2018-05-21 17:30:11 +0100 | [diff] [blame] | 4294 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 4295 | == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4296 | { |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4297 | if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 ) |
| 4298 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4299 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4300 | return( ret ); |
| 4301 | } |
| 4302 | if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 ) |
| 4303 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4304 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4305 | return( ret ); |
| 4306 | } |
| 4307 | |
Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 4308 | if( p != end ) |
| 4309 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4310 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) ); |
| 4311 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 4312 | } |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4313 | } |
| 4314 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4315 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
| 4316 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
Hanno Becker | e7c4eed | 2018-05-21 17:30:11 +0100 | [diff] [blame] | 4317 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 4318 | == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 4319 | { |
Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 4320 | if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 ) |
| 4321 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4322 | 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] | 4323 | return( ret ); |
| 4324 | } |
Manuel Pégourié-Gonnard | b59d699 | 2013-10-14 12:00:45 +0200 | [diff] [blame] | 4325 | |
Hanno Becker | 358b300 | 2019-09-02 09:48:02 +0100 | [diff] [blame] | 4326 | #if defined(MBEDTLS_USE_TINYCRYPT) |
| 4327 | if( mbedtls_ssl_ecdh_read_peerkey( ssl, &p, end ) != 0 ) |
| 4328 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 4329 | #else /* MBEDTLS_USE_TINYCRYPT */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4330 | if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx, |
Manuel Pégourié-Gonnard | b59d699 | 2013-10-14 12:00:45 +0200 | [diff] [blame] | 4331 | p, end - p ) ) != 0 ) |
Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 4332 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4333 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret ); |
| 4334 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP ); |
Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 4335 | } |
| 4336 | |
Hanno Becker | e7c4eed | 2018-05-21 17:30:11 +0100 | [diff] [blame] | 4337 | MBEDTLS_SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp ); |
Hanno Becker | 358b300 | 2019-09-02 09:48:02 +0100 | [diff] [blame] | 4338 | #endif /* MBEDTLS_USE_TINYCRYPT */ |
Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 4339 | } |
| 4340 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4341 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
| 4342 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) |
Hanno Becker | e7c4eed | 2018-05-21 17:30:11 +0100 | [diff] [blame] | 4343 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 4344 | == MBEDTLS_KEY_EXCHANGE_RSA ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 4345 | { |
Manuel Pégourié-Gonnard | 2114d72 | 2014-09-10 13:59:41 +0000 | [diff] [blame] | 4346 | if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 0 ) ) != 0 ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 4347 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4348 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret ); |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 4349 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4350 | } |
| 4351 | } |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4352 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4353 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */ |
Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 4354 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Hanno Becker | e7c4eed | 2018-05-21 17:30:11 +0100 | [diff] [blame] | 4355 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 4356 | == MBEDTLS_KEY_EXCHANGE_ECJPAKE ) |
Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 4357 | { |
| 4358 | ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx, |
| 4359 | p, end - p ); |
| 4360 | if( ret != 0 ) |
| 4361 | { |
| 4362 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret ); |
| 4363 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
| 4364 | } |
Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 4365 | } |
| 4366 | else |
| 4367 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4368 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4369 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 4370 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4371 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4372 | |
Hanno Becker | e7c4eed | 2018-05-21 17:30:11 +0100 | [diff] [blame] | 4373 | return( 0 ); |
Hanno Becker | 7ec345d | 2018-05-21 17:16:42 +0100 | [diff] [blame] | 4374 | } |
| 4375 | |
| 4376 | /* Update the handshake state */ |
Hanno Becker | 7d552fa | 2019-08-12 15:04:59 +0100 | [diff] [blame] | 4377 | static int ssl_in_client_key_exchange_postprocess( mbedtls_ssl_context *ssl ) |
Hanno Becker | 7ec345d | 2018-05-21 17:16:42 +0100 | [diff] [blame] | 4378 | { |
Hanno Becker | dc8bfb9 | 2018-05-21 17:24:27 +0100 | [diff] [blame] | 4379 | int ret; |
Hanno Becker | 1e23af8 | 2018-05-21 17:26:17 +0100 | [diff] [blame] | 4380 | |
Hanno Becker | 09d2364 | 2019-07-22 17:18:18 +0100 | [diff] [blame] | 4381 | if( ( ret = mbedtls_ssl_build_pms( ssl ) ) != 0 ) |
Hanno Becker | 1e23af8 | 2018-05-21 17:26:17 +0100 | [diff] [blame] | 4382 | { |
Hanno Becker | 09d2364 | 2019-07-22 17:18:18 +0100 | [diff] [blame] | 4383 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_build_pms", ret ); |
| 4384 | return( ret ); |
Hanno Becker | 1e23af8 | 2018-05-21 17:26:17 +0100 | [diff] [blame] | 4385 | } |
Hanno Becker | dc8bfb9 | 2018-05-21 17:24:27 +0100 | [diff] [blame] | 4386 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4387 | if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 ) |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 4388 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4389 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret ); |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 4390 | return( ret ); |
| 4391 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4392 | |
Hanno Becker | 7ec345d | 2018-05-21 17:16:42 +0100 | [diff] [blame] | 4393 | ssl->state = MBEDTLS_SSL_CERTIFICATE_VERIFY; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4394 | return( 0 ); |
| 4395 | } |
| 4396 | |
Hanno Becker | ae39b9e | 2019-02-07 12:32:43 +0000 | [diff] [blame] | 4397 | #if !defined(MBEDTLS_KEY_EXCHANGE__CERT_REQ_ALLOWED__ENABLED) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4398 | static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4399 | { |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 4400 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = |
Hanno Becker | df64596 | 2019-06-26 13:02:22 +0100 | [diff] [blame] | 4401 | mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4402 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4403 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4404 | |
Hanno Becker | ae39b9e | 2019-02-07 12:32:43 +0000 | [diff] [blame] | 4405 | if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ) |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 4406 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4407 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 4408 | ssl->state++; |
| 4409 | return( 0 ); |
| 4410 | } |
| 4411 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4412 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 4413 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4414 | } |
Hanno Becker | ae39b9e | 2019-02-07 12:32:43 +0000 | [diff] [blame] | 4415 | #else /* !MBEDTLS_KEY_EXCHANGE__CERT_REQ_ALLOWED__ENABLED */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4416 | static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4417 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4418 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 4528f3f | 2014-09-10 14:17:23 +0000 | [diff] [blame] | 4419 | size_t i, sig_len; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4420 | unsigned char hash[48]; |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 4421 | unsigned char *hash_start = hash; |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4422 | size_t hashlen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4423 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4424 | mbedtls_pk_type_t pk_alg; |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 4425 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4426 | mbedtls_md_type_t md_alg; |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 4427 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = |
Hanno Becker | df64596 | 2019-06-26 13:02:22 +0100 | [diff] [blame] | 4428 | mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 4429 | mbedtls_pk_context *peer_pk = NULL; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4430 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4431 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4432 | |
Hanno Becker | cd90126 | 2019-02-07 13:17:25 +0000 | [diff] [blame] | 4433 | if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4434 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4435 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4436 | ssl->state++; |
| 4437 | return( 0 ); |
| 4438 | } |
| 4439 | |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 4440 | /* Skip if we haven't received a certificate from the client. |
| 4441 | * If MBEDTLS_SSL_KEEP_PEER_CERTIFICATE is set, this can be |
| 4442 | * inferred from the setting of mbedtls_ssl_session::peer_cert. |
| 4443 | * If MBEDTLS_SSL_KEEP_PEER_CERTIFICATE is not set, it can |
| 4444 | * be inferred from whether we've held back the peer CRT's |
| 4445 | * public key in mbedtls_ssl_handshake_params::peer_pubkey. */ |
| 4446 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 4447 | /* Because the peer CRT pubkey is embedded into the handshake |
| 4448 | * params currently, and there's no 'is_init' functions for PK |
| 4449 | * contexts, we need to break the abstraction and peek into |
| 4450 | * the PK context to see if it has been initialized. */ |
| 4451 | if( ssl->handshake->peer_pubkey.pk_info != NULL ) |
| 4452 | peer_pk = &ssl->handshake->peer_pubkey; |
| 4453 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 4454 | if( ssl->session_negotiate->peer_cert != NULL ) |
Hanno Becker | 73cd8d8 | 2019-02-28 14:04:16 +0000 | [diff] [blame] | 4455 | { |
| 4456 | ret = mbedtls_x509_crt_pk_acquire( ssl->session_negotiate->peer_cert, |
| 4457 | &peer_pk ); |
| 4458 | if( ret != 0 ) |
| 4459 | { |
Hanno Becker | 2224ccf | 2019-06-28 10:52:45 +0100 | [diff] [blame] | 4460 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_pk_acquire", ret ); |
| 4461 | return( ret ); |
Hanno Becker | 73cd8d8 | 2019-02-28 14:04:16 +0000 | [diff] [blame] | 4462 | } |
| 4463 | } |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 4464 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 4465 | |
| 4466 | if( peer_pk == NULL ) |
Hanno Becker | cd90126 | 2019-02-07 13:17:25 +0000 | [diff] [blame] | 4467 | { |
| 4468 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) ); |
| 4469 | ssl->state++; |
| 4470 | return( 0 ); |
| 4471 | } |
Hanno Becker | cd90126 | 2019-02-07 13:17:25 +0000 | [diff] [blame] | 4472 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4473 | /* Read the message without adding it to the checksum */ |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 4474 | ret = mbedtls_ssl_read_record( ssl, 0 /* no checksum update */ ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4475 | if( 0 != ret ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4476 | { |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 4477 | MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record" ), ret ); |
Hanno Becker | bc6b598 | 2019-07-02 15:36:44 +0100 | [diff] [blame] | 4478 | goto exit; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4479 | } |
| 4480 | |
| 4481 | ssl->state++; |
| 4482 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4483 | /* Process the message contents */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4484 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE || |
| 4485 | ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE_VERIFY ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4486 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4487 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) ); |
Hanno Becker | bc6b598 | 2019-07-02 15:36:44 +0100 | [diff] [blame] | 4488 | ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY; |
| 4489 | goto exit; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4490 | } |
| 4491 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4492 | i = mbedtls_ssl_hs_hdr_len( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4493 | |
Manuel Pégourié-Gonnard | 4528f3f | 2014-09-10 14:17:23 +0000 | [diff] [blame] | 4494 | /* |
| 4495 | * struct { |
| 4496 | * SignatureAndHashAlgorithm algorithm; -- TLS 1.2 only |
| 4497 | * opaque signature<0..2^16-1>; |
| 4498 | * } DigitallySigned; |
| 4499 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4500 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 4501 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 4502 | if( mbedtls_ssl_get_minor_ver( ssl ) != MBEDTLS_SSL_MINOR_VERSION_3 ) |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 4503 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4504 | md_alg = MBEDTLS_MD_NONE; |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4505 | hashlen = 36; |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 4506 | |
| 4507 | /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */ |
Hanno Becker | 0833c10 | 2019-02-06 18:31:04 +0000 | [diff] [blame] | 4508 | if( mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_ECDSA ) ) |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 4509 | { |
| 4510 | hash_start += 16; |
| 4511 | hashlen -= 16; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4512 | md_alg = MBEDTLS_MD_SHA1; |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 4513 | } |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 4514 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 4515 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4516 | #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || |
| 4517 | MBEDTLS_SSL_PROTO_TLS1_1 */ |
| 4518 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 4519 | 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] | 4520 | { |
Manuel Pégourié-Gonnard | 5ee9654 | 2014-09-10 14:27:21 +0000 | [diff] [blame] | 4521 | if( i + 2 > ssl->in_hslen ) |
| 4522 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4523 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) ); |
Hanno Becker | 73cd8d8 | 2019-02-28 14:04:16 +0000 | [diff] [blame] | 4524 | ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY; |
| 4525 | goto exit; |
Manuel Pégourié-Gonnard | 5ee9654 | 2014-09-10 14:27:21 +0000 | [diff] [blame] | 4526 | } |
| 4527 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4528 | /* |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4529 | * Hash |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4530 | */ |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4531 | md_alg = mbedtls_ssl_md_alg_from_hash( ssl->in_msg[i] ); |
| 4532 | |
Hanno Becker | 2f41b24 | 2019-08-15 17:29:43 +0100 | [diff] [blame] | 4533 | if( |
| 4534 | #if defined(MBEDTLS_SHA512_C) |
| 4535 | md_alg != MBEDTLS_MD_SHA384 && |
| 4536 | #endif |
| 4537 | #if defined(MBEDTLS_SHA256_C) |
| 4538 | md_alg != MBEDTLS_MD_SHA256 && |
| 4539 | #endif |
| 4540 | 1 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4541 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4542 | 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] | 4543 | " for verify message" ) ); |
Hanno Becker | 73cd8d8 | 2019-02-28 14:04:16 +0000 | [diff] [blame] | 4544 | ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY; |
| 4545 | goto exit; |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 4546 | } |
| 4547 | |
Manuel Pégourié-Gonnard | bfe32ef | 2013-08-22 14:55:30 +0200 | [diff] [blame] | 4548 | /* Info from md_alg will be used instead */ |
| 4549 | hashlen = 0; |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4550 | |
Manuel Pégourié-Gonnard | 4528f3f | 2014-09-10 14:17:23 +0000 | [diff] [blame] | 4551 | i++; |
| 4552 | |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4553 | /* |
| 4554 | * Signature |
| 4555 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4556 | if( ( pk_alg = mbedtls_ssl_pk_alg_from_sig( ssl->in_msg[i] ) ) |
| 4557 | == MBEDTLS_PK_NONE ) |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4558 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4559 | 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] | 4560 | " for verify message" ) ); |
Hanno Becker | 73cd8d8 | 2019-02-28 14:04:16 +0000 | [diff] [blame] | 4561 | ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY; |
| 4562 | goto exit; |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4563 | } |
| 4564 | |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4565 | /* |
| 4566 | * Check the certificate's key type matches the signature alg |
| 4567 | */ |
Hanno Becker | 0833c10 | 2019-02-06 18:31:04 +0000 | [diff] [blame] | 4568 | if( !mbedtls_pk_can_do( peer_pk, pk_alg ) ) |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4569 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4570 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) ); |
Hanno Becker | 73cd8d8 | 2019-02-28 14:04:16 +0000 | [diff] [blame] | 4571 | ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY; |
| 4572 | goto exit; |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4573 | } |
Manuel Pégourié-Gonnard | 4528f3f | 2014-09-10 14:17:23 +0000 | [diff] [blame] | 4574 | |
| 4575 | i++; |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 4576 | } |
| 4577 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4578 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 4579 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4580 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 4581 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 4582 | } |
Manuel Pégourié-Gonnard | ff56da3 | 2013-07-11 10:46:21 +0200 | [diff] [blame] | 4583 | |
Manuel Pégourié-Gonnard | 5ee9654 | 2014-09-10 14:27:21 +0000 | [diff] [blame] | 4584 | if( i + 2 > ssl->in_hslen ) |
| 4585 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4586 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) ); |
Hanno Becker | 73cd8d8 | 2019-02-28 14:04:16 +0000 | [diff] [blame] | 4587 | ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY; |
| 4588 | goto exit; |
Manuel Pégourié-Gonnard | 5ee9654 | 2014-09-10 14:27:21 +0000 | [diff] [blame] | 4589 | } |
| 4590 | |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 4591 | 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] | 4592 | i += 2; |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 4593 | |
Manuel Pégourié-Gonnard | 4528f3f | 2014-09-10 14:17:23 +0000 | [diff] [blame] | 4594 | if( i + sig_len != ssl->in_hslen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4595 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4596 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) ); |
Hanno Becker | 73cd8d8 | 2019-02-28 14:04:16 +0000 | [diff] [blame] | 4597 | ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY; |
| 4598 | goto exit; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4599 | } |
| 4600 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4601 | /* Calculate hash and verify signature */ |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 4602 | { |
| 4603 | size_t dummy_hlen; |
Hanno Becker | 2f41b24 | 2019-08-15 17:29:43 +0100 | [diff] [blame] | 4604 | mbedtls_ssl_calc_verify( |
| 4605 | mbedtls_ssl_get_minor_ver( ssl ), |
| 4606 | md_alg, ssl, hash, &dummy_hlen ); |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 4607 | } |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4608 | |
Hanno Becker | 0833c10 | 2019-02-06 18:31:04 +0000 | [diff] [blame] | 4609 | if( ( ret = mbedtls_pk_verify( peer_pk, |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 4610 | md_alg, hash_start, hashlen, |
Manuel Pégourié-Gonnard | 4528f3f | 2014-09-10 14:17:23 +0000 | [diff] [blame] | 4611 | ssl->in_msg + i, sig_len ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4612 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4613 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret ); |
Hanno Becker | 73cd8d8 | 2019-02-28 14:04:16 +0000 | [diff] [blame] | 4614 | goto exit; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4615 | } |
| 4616 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4617 | mbedtls_ssl_update_handshake_status( ssl ); |
| 4618 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4619 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4620 | |
Hanno Becker | 73cd8d8 | 2019-02-28 14:04:16 +0000 | [diff] [blame] | 4621 | exit: |
| 4622 | |
| 4623 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Hanno Becker | c6d1c3e | 2019-03-05 13:50:56 +0000 | [diff] [blame] | 4624 | mbedtls_x509_crt_pk_release( ssl->session_negotiate->peer_cert ); |
Hanno Becker | 73cd8d8 | 2019-02-28 14:04:16 +0000 | [diff] [blame] | 4625 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 4626 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 4627 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4628 | } |
Hanno Becker | ae39b9e | 2019-02-07 12:32:43 +0000 | [diff] [blame] | 4629 | #endif /* MBEDTLS_KEY_EXCHANGE__CERT_REQ_ALLOWED__ENABLED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4630 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4631 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 4632 | 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] | 4633 | { |
| 4634 | int ret; |
Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 4635 | size_t tlen; |
Manuel Pégourié-Gonnard | b0394be | 2015-05-19 11:40:30 +0200 | [diff] [blame] | 4636 | uint32_t lifetime; |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4637 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4638 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) ); |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4639 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4640 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 4641 | ssl->out_msg[0] = MBEDTLS_SSL_HS_NEW_SESSION_TICKET; |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4642 | |
| 4643 | /* |
| 4644 | * struct { |
| 4645 | * uint32 ticket_lifetime_hint; |
| 4646 | * opaque ticket<0..2^16-1>; |
| 4647 | * } NewSessionTicket; |
| 4648 | * |
| 4649 | * 4 . 7 ticket_lifetime_hint (0 = unspecified) |
| 4650 | * 8 . 9 ticket_len (n) |
| 4651 | * 10 . 9+n ticket content |
| 4652 | */ |
Manuel Pégourié-Gonnard | 164d894 | 2013-09-23 22:01:39 +0200 | [diff] [blame] | 4653 | |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 4654 | 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] | 4655 | ssl->session_negotiate, |
| 4656 | ssl->out_msg + 10, |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4657 | ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN, |
Manuel Pégourié-Gonnard | b0394be | 2015-05-19 11:40:30 +0200 | [diff] [blame] | 4658 | &tlen, &lifetime ) ) != 0 ) |
Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 4659 | { |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 4660 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_ticket_write", ret ); |
Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 4661 | tlen = 0; |
| 4662 | } |
Arto Kinnunen | 3d7439e | 2019-09-10 11:30:40 +0300 | [diff] [blame^] | 4663 | (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] | 4664 | |
Arto Kinnunen | 3d7439e | 2019-09-10 11:30:40 +0300 | [diff] [blame^] | 4665 | (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] | 4666 | ssl->out_msglen = 10 + tlen; |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4667 | |
Manuel Pégourié-Gonnard | 145dfcb | 2014-02-26 14:23:33 +0100 | [diff] [blame] | 4668 | /* |
| 4669 | * Morally equivalent to updating ssl->state, but NewSessionTicket and |
| 4670 | * ChangeCipherSpec share the same state. |
| 4671 | */ |
| 4672 | ssl->handshake->new_session_ticket = 0; |
| 4673 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4674 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4675 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4676 | 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] | 4677 | return( ret ); |
| 4678 | } |
| 4679 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4680 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) ); |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4681 | |
| 4682 | return( 0 ); |
| 4683 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4684 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4685 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4686 | /* |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4687 | * SSL handshake -- server side -- single step |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4688 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4689 | int mbedtls_ssl_handshake_server_step( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4690 | { |
| 4691 | int ret = 0; |
| 4692 | |
Manuel Pégourié-Gonnard | dba460f | 2015-06-24 22:59:30 +0200 | [diff] [blame] | 4693 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4694 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4695 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4696 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4697 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4698 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4699 | return( ret ); |
| 4700 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4701 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 4702 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4703 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4704 | { |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4705 | if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4706 | return( ret ); |
| 4707 | } |
Hanno Becker | bc2498a | 2018-08-28 10:13:29 +0100 | [diff] [blame] | 4708 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4709 | |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4710 | switch( ssl->state ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4711 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4712 | case MBEDTLS_SSL_HELLO_REQUEST: |
| 4713 | ssl->state = MBEDTLS_SSL_CLIENT_HELLO; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4714 | break; |
| 4715 | |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4716 | /* |
| 4717 | * <== ClientHello |
| 4718 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4719 | case MBEDTLS_SSL_CLIENT_HELLO: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4720 | ret = ssl_parse_client_hello( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4721 | break; |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4722 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4723 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4724 | case MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT: |
| 4725 | return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED ); |
Manuel Pégourié-Gonnard | 579950c | 2014-09-29 17:47:33 +0200 | [diff] [blame] | 4726 | #endif |
| 4727 | |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4728 | /* |
| 4729 | * ==> ServerHello |
| 4730 | * Certificate |
| 4731 | * ( ServerKeyExchange ) |
| 4732 | * ( CertificateRequest ) |
| 4733 | * ServerHelloDone |
| 4734 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4735 | case MBEDTLS_SSL_SERVER_HELLO: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4736 | ret = ssl_write_server_hello( ssl ); |
| 4737 | break; |
| 4738 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4739 | case MBEDTLS_SSL_SERVER_CERTIFICATE: |
| 4740 | ret = mbedtls_ssl_write_certificate( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4741 | break; |
| 4742 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4743 | case MBEDTLS_SSL_SERVER_KEY_EXCHANGE: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4744 | ret = ssl_write_server_key_exchange( ssl ); |
| 4745 | break; |
| 4746 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4747 | case MBEDTLS_SSL_CERTIFICATE_REQUEST: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4748 | ret = ssl_write_certificate_request( ssl ); |
| 4749 | break; |
| 4750 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4751 | case MBEDTLS_SSL_SERVER_HELLO_DONE: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4752 | ret = ssl_write_server_hello_done( ssl ); |
| 4753 | break; |
| 4754 | |
| 4755 | /* |
| 4756 | * <== ( Certificate/Alert ) |
| 4757 | * ClientKeyExchange |
| 4758 | * ( CertificateVerify ) |
| 4759 | * ChangeCipherSpec |
| 4760 | * Finished |
| 4761 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4762 | case MBEDTLS_SSL_CLIENT_CERTIFICATE: |
| 4763 | ret = mbedtls_ssl_parse_certificate( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4764 | break; |
| 4765 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4766 | case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE: |
Hanno Becker | 7d552fa | 2019-08-12 15:04:59 +0100 | [diff] [blame] | 4767 | ret = ssl_process_in_client_key_exchange( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4768 | break; |
| 4769 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4770 | case MBEDTLS_SSL_CERTIFICATE_VERIFY: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4771 | ret = ssl_parse_certificate_verify( ssl ); |
| 4772 | break; |
| 4773 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4774 | case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC: |
| 4775 | ret = mbedtls_ssl_parse_change_cipher_spec( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4776 | break; |
| 4777 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4778 | case MBEDTLS_SSL_CLIENT_FINISHED: |
| 4779 | ret = mbedtls_ssl_parse_finished( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4780 | break; |
| 4781 | |
| 4782 | /* |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4783 | * ==> ( NewSessionTicket ) |
| 4784 | * ChangeCipherSpec |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4785 | * Finished |
| 4786 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4787 | case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC: |
| 4788 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | 7cd5924 | 2013-08-02 13:24:41 +0200 | [diff] [blame] | 4789 | if( ssl->handshake->new_session_ticket != 0 ) |
| 4790 | ret = ssl_write_new_session_ticket( ssl ); |
| 4791 | else |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 4792 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4793 | ret = mbedtls_ssl_write_change_cipher_spec( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4794 | break; |
| 4795 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4796 | case MBEDTLS_SSL_SERVER_FINISHED: |
| 4797 | ret = mbedtls_ssl_write_finished( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4798 | break; |
| 4799 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4800 | case MBEDTLS_SSL_FLUSH_BUFFERS: |
| 4801 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) ); |
| 4802 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4803 | break; |
| 4804 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4805 | case MBEDTLS_SSL_HANDSHAKE_WRAPUP: |
| 4806 | mbedtls_ssl_handshake_wrapup( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4807 | break; |
| 4808 | |
| 4809 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4810 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) ); |
| 4811 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4812 | } |
| 4813 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4814 | return( ret ); |
| 4815 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4816 | #endif /* MBEDTLS_SSL_SRV_C */ |