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 | } |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 101 | servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) ); |
| 102 | if( servername_list_size + 2 != len ) |
| 103 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 104 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 105 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 106 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 107 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | p = buf + 2; |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 111 | while( servername_list_size > 2 ) |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 112 | { |
| 113 | hostname_len = ( ( p[1] << 8 ) | p[2] ); |
| 114 | if( hostname_len + 3 > servername_list_size ) |
| 115 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 116 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 117 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 118 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 119 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 122 | if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME ) |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 123 | { |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 124 | ret = ssl->conf->f_sni( ssl->conf->p_sni, |
| 125 | ssl, p + 3, hostname_len ); |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 126 | if( ret != 0 ) |
| 127 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 128 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 129 | mbedtls_ssl_pend_fatal_alert( ssl, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 130 | MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME ); |
| 131 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 132 | } |
Paul Bakker | 81420ab | 2012-10-23 10:31:15 +0000 | [diff] [blame] | 133 | return( 0 ); |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | servername_list_size -= hostname_len + 3; |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 137 | p += hostname_len + 3; |
| 138 | } |
| 139 | |
| 140 | if( servername_list_size != 0 ) |
| 141 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 142 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 143 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 144 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 145 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | return( 0 ); |
| 149 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 150 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 151 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 152 | static int ssl_parse_renegotiation_info( mbedtls_ssl_context *ssl, |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 153 | const unsigned char *buf, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 154 | size_t len ) |
| 155 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 156 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 157 | if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ) |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 158 | { |
| 159 | /* Check verify-data in constant-time. The length OTOH is no secret */ |
| 160 | if( len != 1 + ssl->verify_data_len || |
| 161 | buf[0] != ssl->verify_data_len || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 162 | mbedtls_ssl_safer_memcmp( buf + 1, ssl->peer_verify_data, |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 163 | ssl->verify_data_len ) != 0 ) |
| 164 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 165 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 166 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 167 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 168 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 172 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 173 | { |
| 174 | if( len != 1 || buf[0] != 0x0 ) |
| 175 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 176 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 177 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 178 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 179 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 182 | ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 183 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 184 | |
| 185 | return( 0 ); |
| 186 | } |
| 187 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 188 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 189 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 190 | |
| 191 | /* |
| 192 | * Status of the implementation of signature-algorithms extension: |
| 193 | * |
| 194 | * Currently, we are only considering the signature-algorithm extension |
| 195 | * to pick a ciphersuite which allows us to send the ServerKeyExchange |
| 196 | * message with a signature-hash combination that the user allows. |
| 197 | * |
| 198 | * We do *not* check whether all certificates in our certificate |
| 199 | * chain are signed with an allowed signature-hash pair. |
| 200 | * This needs to be done at a later stage. |
| 201 | * |
| 202 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 203 | static int ssl_parse_signature_algorithms_ext( mbedtls_ssl_context *ssl, |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 204 | const unsigned char *buf, |
| 205 | size_t len ) |
| 206 | { |
| 207 | size_t sig_alg_list_size; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 208 | |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 209 | const unsigned char *p; |
Manuel Pégourié-Gonnard | 08e81e0 | 2014-07-08 12:56:25 +0200 | [diff] [blame] | 210 | const unsigned char *end = buf + len; |
Manuel Pégourié-Gonnard | 08e81e0 | 2014-07-08 12:56:25 +0200 | [diff] [blame] | 211 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 212 | mbedtls_md_type_t md_cur; |
| 213 | mbedtls_pk_type_t sig_cur; |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 214 | |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 215 | if ( len < 2 ) { |
| 216 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 217 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 218 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 219 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 220 | } |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 221 | sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) ); |
| 222 | if( sig_alg_list_size + 2 != len || |
Manuel Pégourié-Gonnard | 08e81e0 | 2014-07-08 12:56:25 +0200 | [diff] [blame] | 223 | sig_alg_list_size % 2 != 0 ) |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 224 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 225 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 226 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 227 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 228 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 231 | /* Currently we only guarantee signing the ServerKeyExchange message according |
| 232 | * to the constraints specified in this extension (see above), so it suffices |
| 233 | * 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] | 234 | * |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 235 | * This will change when we also consider certificate signatures, |
| 236 | * in which case we will need to remember the whole signature-hash |
| 237 | * pair list from the extension. |
Manuel Pégourié-Gonnard | 08e81e0 | 2014-07-08 12:56:25 +0200 | [diff] [blame] | 238 | */ |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 239 | |
| 240 | for( p = buf + 2; p < end; p += 2 ) |
| 241 | { |
| 242 | /* Silently ignore unknown signature or hash algorithms. */ |
| 243 | |
| 244 | if( ( sig_cur = mbedtls_ssl_pk_alg_from_sig( p[1] ) ) == MBEDTLS_PK_NONE ) |
| 245 | { |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 246 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext" |
| 247 | " unknown sig alg encoding %d", p[1] ) ); |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 248 | continue; |
| 249 | } |
| 250 | |
| 251 | /* Check if we support the hash the user proposes */ |
| 252 | md_cur = mbedtls_ssl_md_alg_from_hash( p[0] ); |
| 253 | if( md_cur == MBEDTLS_MD_NONE ) |
| 254 | { |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 255 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext:" |
| 256 | " unknown hash alg encoding %d", p[0] ) ); |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 257 | continue; |
| 258 | } |
| 259 | |
| 260 | if( mbedtls_ssl_check_sig_hash( ssl, md_cur ) == 0 ) |
| 261 | { |
| 262 | 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] | 263 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext:" |
| 264 | " match sig %d and hash %d", |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 265 | sig_cur, md_cur ) ); |
| 266 | } |
| 267 | else |
| 268 | { |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 269 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: " |
| 270 | "hash alg %d not supported", md_cur ) ); |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 271 | } |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 274 | return( 0 ); |
| 275 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 276 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && |
| 277 | MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 278 | |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 279 | #if defined(MBEDTLS_ECDH_C) || \ |
| 280 | defined(MBEDTLS_ECDSA_C) || \ |
| 281 | defined(MBEDTLS_USE_TINYCRYPT) || \ |
Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 282 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 283 | static int ssl_parse_supported_elliptic_curves( mbedtls_ssl_context *ssl, |
Hanno Becker | d3b2fcb | 2019-06-17 14:30:05 +0100 | [diff] [blame] | 284 | const unsigned char *buf, size_t len, |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 285 | unsigned char const **list_start, size_t *list_len ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 286 | { |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 287 | size_t list_size; |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 288 | const unsigned char *p; |
| 289 | |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 290 | if ( len < 2 ) { |
| 291 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 292 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 293 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 294 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 295 | } |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 296 | list_size = ( ( buf[0] << 8 ) | ( buf[1] ) ); |
| 297 | if( list_size + 2 != len || |
| 298 | list_size % 2 != 0 ) |
| 299 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 300 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 301 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 302 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 303 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | p = buf + 2; |
Hanno Becker | d3b2fcb | 2019-06-17 14:30:05 +0100 | [diff] [blame] | 307 | |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 308 | /* Remember list for later. */ |
| 309 | *list_start = p; |
| 310 | *list_len = list_size / 2; |
| 311 | |
| 312 | while( list_size > 0 ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 313 | { |
Hanno Becker | a4a9c69 | 2019-06-18 16:55:47 +0100 | [diff] [blame] | 314 | uint16_t const peer_tls_id = ( p[0] << 8 ) | p[1]; |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 315 | |
Hanno Becker | a4a9c69 | 2019-06-18 16:55:47 +0100 | [diff] [blame] | 316 | MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_EC_TLS_ID( own_tls_id ) |
| 317 | if( own_tls_id == peer_tls_id && |
| 318 | ssl->handshake->curve_tls_id == 0 ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 319 | { |
Hanno Becker | a4a9c69 | 2019-06-18 16:55:47 +0100 | [diff] [blame] | 320 | ssl->handshake->curve_tls_id = own_tls_id; |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 321 | } |
Hanno Becker | a4a9c69 | 2019-06-18 16:55:47 +0100 | [diff] [blame] | 322 | MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_EC_TLS_ID |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 323 | |
| 324 | list_size -= 2; |
| 325 | p += 2; |
| 326 | } |
| 327 | |
| 328 | return( 0 ); |
| 329 | } |
| 330 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 331 | static int ssl_parse_supported_point_formats( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 332 | const unsigned char *buf, |
| 333 | size_t len ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 334 | { |
| 335 | size_t list_size; |
| 336 | const unsigned char *p; |
| 337 | |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 338 | if( len == 0 || (size_t)( buf[0] + 1 ) != len ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 339 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 340 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 341 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 342 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 343 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 344 | } |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 345 | list_size = buf[0]; |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 346 | |
Manuel Pégourié-Gonnard | c1b46d0 | 2015-09-16 11:18:32 +0200 | [diff] [blame] | 347 | p = buf + 1; |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 348 | while( list_size > 0 ) |
| 349 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 350 | if( p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED || |
| 351 | p[0] == MBEDTLS_ECP_PF_COMPRESSED ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 352 | { |
Hanno Becker | 975b9ee | 2019-07-24 10:09:27 +0100 | [diff] [blame] | 353 | #if defined(MBEDTLS_ECDH_C) |
Manuel Pégourié-Gonnard | 5734b2d | 2013-08-15 19:04:02 +0200 | [diff] [blame] | 354 | ssl->handshake->ecdh_ctx.point_format = p[0]; |
Robert Cragie | 136884c | 2015-10-02 13:34:31 +0100 | [diff] [blame] | 355 | #endif |
Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 356 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Robert Cragie | 136884c | 2015-10-02 13:34:31 +0100 | [diff] [blame] | 357 | ssl->handshake->ecjpake_ctx.point_format = p[0]; |
| 358 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 359 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 360 | return( 0 ); |
| 361 | } |
| 362 | |
| 363 | list_size--; |
| 364 | p++; |
| 365 | } |
| 366 | |
| 367 | return( 0 ); |
| 368 | } |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 369 | #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || MBEDTLS_USE_TINYCRYPT |
Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 370 | MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 371 | |
Manuel Pégourié-Gonnard | bf57be6 | 2015-09-16 15:04:01 +0200 | [diff] [blame] | 372 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 373 | static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl, |
| 374 | const unsigned char *buf, |
| 375 | size_t len ) |
| 376 | { |
| 377 | int ret; |
| 378 | |
| 379 | if( mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 ) |
| 380 | { |
| 381 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) ); |
| 382 | return( 0 ); |
| 383 | } |
| 384 | |
| 385 | if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx, |
| 386 | buf, len ) ) != 0 ) |
| 387 | { |
| 388 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 389 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 390 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Manuel Pégourié-Gonnard | bf57be6 | 2015-09-16 15:04:01 +0200 | [diff] [blame] | 391 | return( ret ); |
| 392 | } |
| 393 | |
| 394 | /* Only mark the extension as OK when we're sure it is */ |
| 395 | ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK; |
| 396 | |
| 397 | return( 0 ); |
| 398 | } |
| 399 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
| 400 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 401 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 402 | 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] | 403 | const unsigned char *buf, |
| 404 | size_t len ) |
| 405 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 406 | 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] | 407 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 408 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 409 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 410 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 411 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 48f8d0d | 2013-07-17 10:25:37 +0200 | [diff] [blame] | 412 | } |
| 413 | |
Manuel Pégourié-Gonnard | ed4af8b | 2013-07-18 14:07:09 +0200 | [diff] [blame] | 414 | ssl->session_negotiate->mfl_code = buf[0]; |
| 415 | |
Manuel Pégourié-Gonnard | 48f8d0d | 2013-07-17 10:25:37 +0200 | [diff] [blame] | 416 | return( 0 ); |
| 417 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 418 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 48f8d0d | 2013-07-17 10:25:37 +0200 | [diff] [blame] | 419 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 420 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | c403b26 | 2019-04-26 13:56:39 +0100 | [diff] [blame] | 421 | static int ssl_parse_cid_ext( mbedtls_ssl_context *ssl, |
| 422 | const unsigned char *buf, |
| 423 | size_t len ) |
| 424 | { |
| 425 | size_t peer_cid_len; |
| 426 | |
| 427 | /* CID extension only makes sense in DTLS */ |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 428 | if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ) |
Hanno Becker | c403b26 | 2019-04-26 13:56:39 +0100 | [diff] [blame] | 429 | { |
| 430 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 431 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 432 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Hanno Becker | c403b26 | 2019-04-26 13:56:39 +0100 | [diff] [blame] | 433 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 434 | } |
| 435 | |
| 436 | /* |
Hanno Becker | 3cdf8fe | 2019-05-15 10:26:32 +0100 | [diff] [blame] | 437 | * Quoting draft-ietf-tls-dtls-connection-id-05 |
| 438 | * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05 |
Hanno Becker | c403b26 | 2019-04-26 13:56:39 +0100 | [diff] [blame] | 439 | * |
| 440 | * struct { |
| 441 | * opaque cid<0..2^8-1>; |
| 442 | * } ConnectionId; |
| 443 | */ |
| 444 | |
| 445 | if( len < 1 ) |
| 446 | { |
| 447 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 448 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 449 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Hanno Becker | c403b26 | 2019-04-26 13:56:39 +0100 | [diff] [blame] | 450 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 451 | } |
| 452 | |
| 453 | peer_cid_len = *buf++; |
| 454 | len--; |
| 455 | |
| 456 | if( len != peer_cid_len ) |
| 457 | { |
| 458 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 459 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 460 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Hanno Becker | c403b26 | 2019-04-26 13:56:39 +0100 | [diff] [blame] | 461 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 462 | } |
| 463 | |
| 464 | /* Ignore CID if the user has disabled its use. */ |
| 465 | if( ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED ) |
| 466 | { |
| 467 | /* Leave ssl->handshake->cid_in_use in its default |
| 468 | * value of MBEDTLS_SSL_CID_DISABLED. */ |
| 469 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Client sent CID extension, but CID disabled" ) ); |
| 470 | return( 0 ); |
| 471 | } |
| 472 | |
| 473 | if( peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX ) |
| 474 | { |
| 475 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 476 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 477 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Hanno Becker | c403b26 | 2019-04-26 13:56:39 +0100 | [diff] [blame] | 478 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 479 | } |
| 480 | |
Hanno Becker | 19976b5 | 2019-05-03 12:43:44 +0100 | [diff] [blame] | 481 | ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED; |
Hanno Becker | c403b26 | 2019-04-26 13:56:39 +0100 | [diff] [blame] | 482 | ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len; |
| 483 | memcpy( ssl->handshake->peer_cid, buf, peer_cid_len ); |
| 484 | |
| 485 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use of CID extension negotiated" ) ); |
| 486 | MBEDTLS_SSL_DEBUG_BUF( 3, "Client CID", buf, peer_cid_len ); |
| 487 | |
Hanno Becker | c403b26 | 2019-04-26 13:56:39 +0100 | [diff] [blame] | 488 | return( 0 ); |
| 489 | } |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 490 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | c403b26 | 2019-04-26 13:56:39 +0100 | [diff] [blame] | 491 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 492 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 493 | 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] | 494 | const unsigned char *buf, |
| 495 | size_t len ) |
| 496 | { |
| 497 | if( len != 0 ) |
| 498 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 499 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 500 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 501 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 502 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | ((void) buf); |
| 506 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 507 | if( ssl->conf->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 508 | ssl->session_negotiate->trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 509 | |
| 510 | return( 0 ); |
| 511 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 512 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 513 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 514 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 515 | 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] | 516 | const unsigned char *buf, |
| 517 | size_t len ) |
| 518 | { |
| 519 | if( len != 0 ) |
| 520 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 521 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 522 | mbedtls_ssl_pend_fatal_alert( ssl, |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 523 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 524 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | ((void) buf); |
| 528 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 529 | if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED && |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 530 | 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] | 531 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 532 | ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | return( 0 ); |
| 536 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 537 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 538 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 539 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 540 | 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] | 541 | const unsigned char *buf, |
| 542 | size_t len ) |
| 543 | { |
| 544 | if( len != 0 ) |
| 545 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 546 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 547 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 548 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 549 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | ((void) buf); |
| 553 | |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 554 | return( 0 ); |
| 555 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 556 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 557 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 558 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 559 | 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] | 560 | unsigned char *buf, |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 561 | size_t len ) |
| 562 | { |
Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 563 | int ret; |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 564 | mbedtls_ssl_session session; |
Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 565 | |
Manuel Pégourié-Gonnard | bae389b | 2015-06-24 10:45:58 +0200 | [diff] [blame] | 566 | mbedtls_ssl_session_init( &session ); |
| 567 | |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 568 | if( ssl->conf->f_ticket_parse == NULL || |
| 569 | ssl->conf->f_ticket_write == NULL ) |
| 570 | { |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 571 | return( 0 ); |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 572 | } |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 573 | |
Manuel Pégourié-Gonnard | 306827e | 2013-08-02 18:05:14 +0200 | [diff] [blame] | 574 | /* Remember the client asked us to send a new ticket */ |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 575 | ssl->handshake->new_session_ticket = 1; |
| 576 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 577 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) ); |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 578 | |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 579 | if( len == 0 ) |
| 580 | return( 0 ); |
| 581 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 582 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 583 | if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ) |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 584 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 585 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) ); |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 586 | return( 0 ); |
| 587 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 588 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 589 | |
| 590 | /* |
Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 591 | * Failures are ok: just ignore the ticket and proceed. |
| 592 | */ |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 593 | if( ( ret = ssl->conf->f_ticket_parse( ssl->conf->p_ticket, &session, |
| 594 | buf, len ) ) != 0 ) |
Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 595 | { |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 596 | mbedtls_ssl_session_free( &session ); |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 597 | |
| 598 | if( ret == MBEDTLS_ERR_SSL_INVALID_MAC ) |
| 599 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket is not authentic" ) ); |
| 600 | else if( ret == MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED ) |
| 601 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket is expired" ) ); |
| 602 | else |
| 603 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_ticket_parse", ret ); |
| 604 | |
Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 605 | return( 0 ); |
Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 606 | } |
Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 607 | |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 608 | /* |
| 609 | * Keep the session ID sent by the client, since we MUST send it back to |
| 610 | * inform them we're accepting the ticket (RFC 5077 section 3.4) |
| 611 | */ |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 612 | session.id_len = ssl->session_negotiate->id_len; |
| 613 | memcpy( &session.id, ssl->session_negotiate->id, session.id_len ); |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 614 | |
| 615 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
| 616 | memcpy( ssl->session_negotiate, &session, sizeof( mbedtls_ssl_session ) ); |
| 617 | |
| 618 | /* Zeroize instead of free as we copied the content */ |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 619 | mbedtls_platform_zeroize( &session, sizeof( mbedtls_ssl_session ) ); |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 620 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 621 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) ); |
Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 622 | |
Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 623 | ssl->handshake->resume = 1; |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 624 | |
Manuel Pégourié-Gonnard | 306827e | 2013-08-02 18:05:14 +0200 | [diff] [blame] | 625 | /* Don't send a new ticket after all, this one is OK */ |
| 626 | ssl->handshake->new_session_ticket = 0; |
| 627 | |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 628 | return( 0 ); |
| 629 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 630 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 631 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 632 | #if defined(MBEDTLS_SSL_ALPN) |
| 633 | static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 14beb08 | 2014-07-08 13:50:35 +0200 | [diff] [blame] | 634 | const unsigned char *buf, size_t len ) |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 635 | { |
Paul Bakker | 14b16c6 | 2014-05-28 11:33:54 +0200 | [diff] [blame] | 636 | size_t list_len, cur_len, ours_len; |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 637 | const unsigned char *theirs, *start, *end; |
| 638 | const char **ours; |
| 639 | |
| 640 | /* If ALPN not configured, just ignore the extension */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 641 | if( ssl->conf->alpn_list == NULL ) |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 642 | return( 0 ); |
| 643 | |
| 644 | /* |
| 645 | * opaque ProtocolName<1..2^8-1>; |
| 646 | * |
| 647 | * struct { |
| 648 | * ProtocolName protocol_name_list<2..2^16-1> |
| 649 | * } ProtocolNameList; |
| 650 | */ |
| 651 | |
| 652 | /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */ |
| 653 | if( len < 4 ) |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 654 | { |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 655 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 656 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 657 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 658 | } |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 659 | |
| 660 | list_len = ( buf[0] << 8 ) | buf[1]; |
| 661 | if( list_len != len - 2 ) |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 662 | { |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 663 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 664 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 665 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 666 | } |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 667 | |
| 668 | /* |
Manuel Pégourié-Gonnard | 239987f | 2018-01-09 10:43:43 +0100 | [diff] [blame] | 669 | * Validate peer's list (lengths) |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 670 | */ |
| 671 | start = buf + 2; |
| 672 | end = buf + len; |
Manuel Pégourié-Gonnard | 239987f | 2018-01-09 10:43:43 +0100 | [diff] [blame] | 673 | for( theirs = start; theirs != end; theirs += cur_len ) |
| 674 | { |
| 675 | cur_len = *theirs++; |
| 676 | |
| 677 | /* Current identifier must fit in list */ |
| 678 | if( cur_len > (size_t)( end - theirs ) ) |
| 679 | { |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 680 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 681 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 239987f | 2018-01-09 10:43:43 +0100 | [diff] [blame] | 682 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 683 | } |
| 684 | |
| 685 | /* Empty strings MUST NOT be included */ |
| 686 | if( cur_len == 0 ) |
| 687 | { |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 688 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 689 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Manuel Pégourié-Gonnard | 239987f | 2018-01-09 10:43:43 +0100 | [diff] [blame] | 690 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | /* |
| 695 | * Use our order of preference |
| 696 | */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 697 | for( ours = ssl->conf->alpn_list; *ours != NULL; ours++ ) |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 698 | { |
Paul Bakker | 14b16c6 | 2014-05-28 11:33:54 +0200 | [diff] [blame] | 699 | ours_len = strlen( *ours ); |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 700 | for( theirs = start; theirs != end; theirs += cur_len ) |
| 701 | { |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 702 | cur_len = *theirs++; |
| 703 | |
Paul Bakker | 14b16c6 | 2014-05-28 11:33:54 +0200 | [diff] [blame] | 704 | if( cur_len == ours_len && |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 705 | memcmp( theirs, *ours, cur_len ) == 0 ) |
| 706 | { |
| 707 | ssl->alpn_chosen = *ours; |
| 708 | return( 0 ); |
| 709 | } |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | /* If we get there, no match was found */ |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 714 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 715 | MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 716 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 717 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 718 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 719 | |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 720 | /* |
| 721 | * Auxiliary functions for ServerHello parsing and related actions |
| 722 | */ |
| 723 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 724 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 725 | /* |
Manuel Pégourié-Gonnard | 6458e3b | 2015-01-08 14:16:56 +0100 | [diff] [blame] | 726 | * 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] | 727 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 728 | #if defined(MBEDTLS_ECDSA_C) |
| 729 | static int ssl_check_key_curve( mbedtls_pk_context *pk, |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 730 | unsigned char const *acceptable_ec_tls_ids, |
| 731 | size_t ec_tls_ids_len ) |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 732 | { |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 733 | mbedtls_ecp_curve_info const *info; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 734 | 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] | 735 | |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 736 | info = mbedtls_ecp_curve_info_from_grp_id( grp_id ); |
| 737 | if( info == NULL ) |
Hanno Becker | d3b2fcb | 2019-06-17 14:30:05 +0100 | [diff] [blame] | 738 | return( -1 ); |
| 739 | |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 740 | if( acceptable_ec_tls_ids == NULL ) |
| 741 | return( -1 ); |
| 742 | |
| 743 | while( ec_tls_ids_len-- != 0 ) |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 744 | { |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 745 | uint16_t const cur_tls_id = |
| 746 | ( acceptable_ec_tls_ids[0] << 8 ) | acceptable_ec_tls_ids[1]; |
| 747 | |
| 748 | if( cur_tls_id == info->tls_id ) |
Manuel Pégourié-Gonnard | 6458e3b | 2015-01-08 14:16:56 +0100 | [diff] [blame] | 749 | return( 0 ); |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 750 | |
| 751 | acceptable_ec_tls_ids += 2; |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 752 | } |
| 753 | |
Manuel Pégourié-Gonnard | 6458e3b | 2015-01-08 14:16:56 +0100 | [diff] [blame] | 754 | return( -1 ); |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 755 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 756 | #endif /* MBEDTLS_ECDSA_C */ |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 757 | |
| 758 | /* |
| 759 | * Try picking a certificate for this ciphersuite, |
| 760 | * return 0 on success and -1 on failure. |
| 761 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 762 | static int ssl_pick_cert( mbedtls_ssl_context *ssl, |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 763 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info, |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 764 | unsigned char const *acceptable_ec_tls_ids, |
| 765 | size_t ec_tls_ids_len ) |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 766 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 767 | mbedtls_ssl_key_cert *cur, *list, *fallback = NULL; |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 768 | mbedtls_pk_type_t pk_alg = |
| 769 | mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ); |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 770 | uint32_t flags; |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 771 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 772 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 773 | if( ssl->handshake->sni_key_cert != NULL ) |
| 774 | list = ssl->handshake->sni_key_cert; |
| 775 | else |
| 776 | #endif |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 777 | list = ssl->conf->key_cert; |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 778 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 779 | if( pk_alg == MBEDTLS_PK_NONE ) |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 780 | return( 0 ); |
| 781 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 782 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite requires certificate" ) ); |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 783 | |
Manuel Pégourié-Gonnard | e540b49 | 2015-07-07 12:44:38 +0200 | [diff] [blame] | 784 | if( list == NULL ) |
| 785 | { |
| 786 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server has no certificate" ) ); |
| 787 | return( -1 ); |
| 788 | } |
| 789 | |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 790 | for( cur = list; cur != NULL; cur = cur->next ) |
| 791 | { |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 792 | int match = 1; |
| 793 | mbedtls_pk_context *pk; |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 794 | |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 795 | /* WARNING: With the current X.509 caching architecture, this MUST |
| 796 | * happen outside of the PK acquire/release block, because it moves |
| 797 | * the cached PK context. In a threading-enabled build, this would |
| 798 | * rightfully fail, but lead to a use-after-free otherwise. */ |
| 799 | MBEDTLS_SSL_DEBUG_CRT( 3, "candidate certificate chain, certificate", |
| 800 | cur->cert ); |
| 801 | |
Hanno Becker | 74b89f6 | 2019-02-17 21:22:07 +0000 | [diff] [blame] | 802 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
| 803 | /* ASYNC_PRIVATE may use a NULL entry for the opaque private key, so |
| 804 | * we have to use the public key context to infer the capabilities |
| 805 | * of the key. */ |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 806 | { |
| 807 | int ret; |
| 808 | ret = mbedtls_x509_crt_pk_acquire( cur->cert, &pk ); |
| 809 | if( ret != 0 ) |
Hanno Becker | 2224ccf | 2019-06-28 10:52:45 +0100 | [diff] [blame] | 810 | { |
| 811 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_pk_acquire", ret ); |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 812 | return( ret ); |
Hanno Becker | 2224ccf | 2019-06-28 10:52:45 +0100 | [diff] [blame] | 813 | } |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 814 | } |
Hanno Becker | 74b89f6 | 2019-02-17 21:22:07 +0000 | [diff] [blame] | 815 | #else |
| 816 | /* Outside of ASYNC_PRIVATE, use private key context directly |
| 817 | * instead of querying for the public key context from the |
| 818 | * certificate, so save a few bytes of code. */ |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 819 | pk = cur->key; |
Hanno Becker | 74b89f6 | 2019-02-17 21:22:07 +0000 | [diff] [blame] | 820 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 821 | |
Hanno Becker | 74b89f6 | 2019-02-17 21:22:07 +0000 | [diff] [blame] | 822 | if( ! mbedtls_pk_can_do( pk, pk_alg ) ) |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 823 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 824 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: key type" ) ); |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 825 | match = 0; |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 826 | } |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 827 | |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 828 | #if defined(MBEDTLS_ECDSA_C) |
| 829 | if( pk_alg == MBEDTLS_PK_ECDSA && |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 830 | ssl_check_key_curve( pk, |
| 831 | acceptable_ec_tls_ids, |
| 832 | ec_tls_ids_len ) != 0 ) |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 833 | { |
| 834 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: elliptic curve" ) ); |
| 835 | match = 0; |
| 836 | } |
Hanno Becker | d3b2fcb | 2019-06-17 14:30:05 +0100 | [diff] [blame] | 837 | #else |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 838 | ((void) acceptable_ec_tls_ids); |
| 839 | ((void) ec_tls_ids_len); |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 840 | #endif |
| 841 | |
| 842 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Hanno Becker | c6d1c3e | 2019-03-05 13:50:56 +0000 | [diff] [blame] | 843 | mbedtls_x509_crt_pk_release( cur->cert ); |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 844 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
| 845 | |
| 846 | if( match == 0 ) |
| 847 | continue; |
| 848 | |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 849 | /* |
| 850 | * This avoids sending the client a cert it'll reject based on |
| 851 | * keyUsage or other extensions. |
| 852 | * |
| 853 | * It also allows the user to provision different certificates for |
| 854 | * different uses based on keyUsage, eg if they want to avoid signing |
| 855 | * and decrypting with the same RSA key. |
| 856 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 857 | if( mbedtls_ssl_check_cert_usage( cur->cert, ciphersuite_info, |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 858 | MBEDTLS_SSL_IS_SERVER, &flags ) != 0 ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 859 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 860 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: " |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 861 | "(extended) key usage extension" ) ); |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 862 | continue; |
| 863 | } |
| 864 | |
Hanno Becker | 828a8c0 | 2019-02-26 16:48:55 +0000 | [diff] [blame] | 865 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 866 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Manuel Pégourié-Gonnard | df331a5 | 2015-01-08 16:43:07 +0100 | [diff] [blame] | 867 | /* |
| 868 | * Try to select a SHA-1 certificate for pre-1.2 clients, but still |
| 869 | * present them a SHA-higher cert rather than failing if it's the only |
| 870 | * one we got that satisfies the other conditions. |
| 871 | */ |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 872 | 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] | 873 | { |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 874 | mbedtls_md_type_t sig_md; |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 875 | { |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 876 | int ret; |
Hanno Becker | 5f268b3 | 2019-05-20 16:26:34 +0100 | [diff] [blame] | 877 | mbedtls_x509_crt_frame const *frame; |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 878 | ret = mbedtls_x509_crt_frame_acquire( cur->cert, &frame ); |
| 879 | if( ret != 0 ) |
Hanno Becker | 2224ccf | 2019-06-28 10:52:45 +0100 | [diff] [blame] | 880 | { |
| 881 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_frame_acquire", ret ); |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 882 | return( ret ); |
Hanno Becker | 2224ccf | 2019-06-28 10:52:45 +0100 | [diff] [blame] | 883 | } |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 884 | sig_md = frame->sig_md; |
Hanno Becker | c6d1c3e | 2019-03-05 13:50:56 +0000 | [diff] [blame] | 885 | mbedtls_x509_crt_frame_release( cur->cert ); |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | if( sig_md != MBEDTLS_MD_SHA1 ) |
| 889 | { |
| 890 | if( fallback == NULL ) |
| 891 | fallback = cur; |
| 892 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 893 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate not preferred: " |
Hanno Becker | 30649f7 | 2019-02-26 16:49:40 +0000 | [diff] [blame] | 894 | "sha-2 with pre-TLS 1.2 client" ) ); |
| 895 | continue; |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 896 | } |
Manuel Pégourié-Gonnard | df331a5 | 2015-01-08 16:43:07 +0100 | [diff] [blame] | 897 | } |
Hanno Becker | 828a8c0 | 2019-02-26 16:48:55 +0000 | [diff] [blame] | 898 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || |
| 899 | MBEDTLS_SSL_PROTO_TLS1_1 || |
| 900 | MBEDTLS_SSL_PROTO_SSL3 */ |
Manuel Pégourié-Gonnard | df331a5 | 2015-01-08 16:43:07 +0100 | [diff] [blame] | 901 | |
Manuel Pégourié-Gonnard | 846ba47 | 2015-01-08 13:54:38 +0100 | [diff] [blame] | 902 | /* If we get there, we got a winner */ |
| 903 | break; |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 904 | } |
| 905 | |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 906 | if( cur == NULL ) |
| 907 | cur = fallback; |
| 908 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 909 | /* 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] | 910 | if( cur != NULL ) |
| 911 | { |
| 912 | ssl->handshake->key_cert = cur; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 913 | MBEDTLS_SSL_DEBUG_CRT( 3, "selected certificate chain, certificate", |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 914 | ssl->handshake->key_cert->cert ); |
Manuel Pégourié-Gonnard | df331a5 | 2015-01-08 16:43:07 +0100 | [diff] [blame] | 915 | return( 0 ); |
| 916 | } |
| 917 | |
| 918 | return( -1 ); |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 919 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 920 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 921 | |
| 922 | /* |
| 923 | * Check if a given ciphersuite is suitable for use with our config/keys/etc |
| 924 | * Sets ciphersuite_info only if the suite matches. |
| 925 | */ |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 926 | static int ssl_ciphersuite_is_match( mbedtls_ssl_context *ssl, |
| 927 | mbedtls_ssl_ciphersuite_handle_t suite_info, |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 928 | unsigned char const *acceptable_ec_tls_ids, |
| 929 | size_t ec_tls_ids_len ) |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 930 | { |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 931 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
Hanno Becker | 4cb1f4d | 2017-10-10 15:59:57 +0100 | [diff] [blame] | 932 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 933 | mbedtls_pk_type_t sig_type; |
| 934 | #endif |
| 935 | |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 936 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "trying ciphersuite: %s", |
| 937 | mbedtls_ssl_suite_get_name( suite_info ) ) ); |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 938 | |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 939 | if( mbedtls_ssl_suite_get_min_minor_ver( suite_info ) |
| 940 | > mbedtls_ssl_get_minor_ver( ssl ) || |
| 941 | mbedtls_ssl_suite_get_max_minor_ver( suite_info ) |
| 942 | < mbedtls_ssl_get_minor_ver( ssl ) ) |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 943 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 944 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: version" ) ); |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 945 | return( 0 ); |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 946 | } |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 947 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 948 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 949 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 950 | ( mbedtls_ssl_suite_get_flags( suite_info ) & |
| 951 | MBEDTLS_CIPHERSUITE_NODTLS ) ) |
| 952 | { |
Manuel Pégourié-Gonnard | d666451 | 2014-02-06 13:26:57 +0100 | [diff] [blame] | 953 | return( 0 ); |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 954 | } |
Manuel Pégourié-Gonnard | d666451 | 2014-02-06 13:26:57 +0100 | [diff] [blame] | 955 | #endif |
| 956 | |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 957 | #if defined(MBEDTLS_ARC4_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 958 | if( ssl->conf->arc4_disabled == MBEDTLS_SSL_ARC4_DISABLED && |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 959 | 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] | 960 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 961 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: rc4" ) ); |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 962 | return( 0 ); |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 963 | } |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 964 | #endif |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 965 | |
Manuel Pégourié-Gonnard | e511b4e | 2015-09-16 14:11:09 +0200 | [diff] [blame] | 966 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 967 | if( mbedtls_ssl_suite_get_key_exchange( suite_info ) == |
| 968 | MBEDTLS_KEY_EXCHANGE_ECJPAKE && |
Manuel Pégourié-Gonnard | bf57be6 | 2015-09-16 15:04:01 +0200 | [diff] [blame] | 969 | ( 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] | 970 | { |
Manuel Pégourié-Gonnard | bf57be6 | 2015-09-16 15:04:01 +0200 | [diff] [blame] | 971 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: ecjpake " |
| 972 | "not configured or ext missing" ) ); |
Manuel Pégourié-Gonnard | e511b4e | 2015-09-16 14:11:09 +0200 | [diff] [blame] | 973 | return( 0 ); |
| 974 | } |
| 975 | #endif |
| 976 | |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 977 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 978 | defined(MBEDTLS_USE_TINYCRYPT) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 979 | if( mbedtls_ssl_ciphersuite_uses_ec( suite_info ) && |
Hanno Becker | 004619f | 2019-06-18 16:07:32 +0100 | [diff] [blame] | 980 | ssl->handshake->curve_tls_id == 0 ) |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 981 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 982 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: " |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 983 | "no common elliptic curve" ) ); |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 984 | return( 0 ); |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 985 | } |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 986 | #endif |
| 987 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 988 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 989 | /* If the ciphersuite requires a pre-shared key and we don't |
| 990 | * have one, skip it now rather than failing later */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 991 | if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) && |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 992 | ssl->conf->f_psk == NULL && |
| 993 | ( ssl->conf->psk == NULL || ssl->conf->psk_identity == NULL || |
| 994 | 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] | 995 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 996 | 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] | 997 | return( 0 ); |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 998 | } |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 999 | #endif |
| 1000 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 1001 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 1002 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
| 1003 | /* If the ciphersuite requires signing, check whether |
| 1004 | * a suitable hash algorithm is present. */ |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 1005 | if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 1006 | { |
| 1007 | sig_type = mbedtls_ssl_get_ciphersuite_sig_alg( suite_info ); |
| 1008 | if( sig_type != MBEDTLS_PK_NONE && |
| 1009 | mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs, sig_type ) == MBEDTLS_MD_NONE ) |
| 1010 | { |
| 1011 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no suitable hash algorithm " |
| 1012 | "for signature algorithm %d", sig_type ) ); |
| 1013 | return( 0 ); |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && |
| 1018 | MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
| 1019 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1020 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 1021 | /* |
| 1022 | * Final check: if ciphersuite requires us to have a |
| 1023 | * certificate/key of a particular type: |
| 1024 | * - select the appropriate certificate if we have one, or |
| 1025 | * - try the next ciphersuite if we don't |
| 1026 | * This must be done last since we modify the key_cert list. |
| 1027 | */ |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 1028 | if( ssl_pick_cert( ssl, suite_info, |
| 1029 | acceptable_ec_tls_ids, |
| 1030 | ec_tls_ids_len ) != 0 ) |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 1031 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1032 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: " |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 1033 | "no suitable certificate" ) ); |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 1034 | return( 0 ); |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 1035 | } |
Hanno Becker | d3b2fcb | 2019-06-17 14:30:05 +0100 | [diff] [blame] | 1036 | #else |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 1037 | ((void) acceptable_ec_tls_ids); |
| 1038 | ((void) ec_tls_ids_len); |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 1039 | #endif |
| 1040 | |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 1041 | return( 1 ); |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 1042 | } |
| 1043 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1044 | #if defined(MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO) |
| 1045 | static int ssl_parse_client_hello_v2( mbedtls_ssl_context *ssl ) |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1046 | { |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1047 | int ret, got_common_suite; |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1048 | unsigned int i, j; |
| 1049 | size_t n; |
| 1050 | unsigned int ciph_len, sess_len, chal_len; |
| 1051 | unsigned char *buf, *p; |
Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 1052 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 1053 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info; |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 1054 | #endif |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1055 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1056 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1057 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1058 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1059 | if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ) |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1060 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1061 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 1062 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 1063 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1064 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1065 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1066 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1067 | |
| 1068 | buf = ssl->in_hdr; |
| 1069 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1070 | MBEDTLS_SSL_DEBUG_BUF( 4, "record header", buf, 5 ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1071 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1072 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d", |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1073 | buf[2] ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1074 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d", |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1075 | ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1076 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]", |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1077 | buf[3], buf[4] ) ); |
| 1078 | |
| 1079 | /* |
| 1080 | * SSLv2 Client Hello |
| 1081 | * |
| 1082 | * Record layer: |
| 1083 | * 0 . 1 message length |
| 1084 | * |
| 1085 | * SSL layer: |
| 1086 | * 2 . 2 message type |
| 1087 | * 3 . 4 protocol version |
| 1088 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1089 | if( buf[2] != MBEDTLS_SSL_HS_CLIENT_HELLO || |
| 1090 | buf[3] != MBEDTLS_SSL_MAJOR_VERSION_3 ) |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1091 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1092 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1093 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF; |
| 1097 | |
| 1098 | if( n < 17 || n > 512 ) |
| 1099 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1100 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1101 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1102 | } |
| 1103 | |
Hanno Becker | 93de296 | 2019-07-25 12:38:18 +0100 | [diff] [blame] | 1104 | #if !defined(MBEDTLS_SSL_CONF_FIXED_MAJOR_VER) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1105 | ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3; |
Hanno Becker | 93de296 | 2019-07-25 12:38:18 +0100 | [diff] [blame] | 1106 | #endif |
| 1107 | |
| 1108 | #if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER) |
Hanno Becker | e965bd3 | 2019-06-12 14:04:34 +0100 | [diff] [blame] | 1109 | ssl->minor_ver = |
| 1110 | ( buf[4] <= mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) ) |
| 1111 | ? buf[4] : mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ); |
Hanno Becker | 93de296 | 2019-07-25 12:38:18 +0100 | [diff] [blame] | 1112 | #endif |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1113 | |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 1114 | 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] | 1115 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1116 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum" |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 1117 | " [%d:%d] < [%d:%d]", |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 1118 | mbedtls_ssl_get_major_ver( ssl ), |
| 1119 | mbedtls_ssl_get_minor_ver( ssl ), |
Hanno Becker | e965bd3 | 2019-06-12 14:04:34 +0100 | [diff] [blame] | 1120 | mbedtls_ssl_conf_get_min_major_ver( ssl->conf ), |
| 1121 | mbedtls_ssl_conf_get_min_minor_ver( ssl->conf ) ) ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1122 | |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 1123 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 1124 | MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1125 | return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1126 | } |
| 1127 | |
Hanno Becker | 18729ae | 2019-06-12 14:47:21 +0100 | [diff] [blame] | 1128 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \ |
| 1129 | defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
Paul Bakker | 2fbefde | 2013-06-29 16:01:15 +0200 | [diff] [blame] | 1130 | ssl->handshake->max_major_ver = buf[3]; |
| 1131 | ssl->handshake->max_minor_ver = buf[4]; |
Hanno Becker | 18729ae | 2019-06-12 14:47:21 +0100 | [diff] [blame] | 1132 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED || |
| 1133 | MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1134 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1135 | if( ( ret = mbedtls_ssl_fetch_input( ssl, 2 + n ) ) != 0 ) |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1136 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1137 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1138 | return( ret ); |
| 1139 | } |
| 1140 | |
| 1141 | ssl->handshake->update_checksum( ssl, buf + 2, n ); |
| 1142 | |
| 1143 | buf = ssl->in_msg; |
| 1144 | n = ssl->in_left - 5; |
| 1145 | |
| 1146 | /* |
| 1147 | * 0 . 1 ciphersuitelist length |
| 1148 | * 2 . 3 session id length |
| 1149 | * 4 . 5 challenge length |
| 1150 | * 6 . .. ciphersuitelist |
| 1151 | * .. . .. session id |
| 1152 | * .. . .. challenge |
| 1153 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1154 | MBEDTLS_SSL_DEBUG_BUF( 4, "record contents", buf, n ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1155 | |
| 1156 | ciph_len = ( buf[0] << 8 ) | buf[1]; |
| 1157 | sess_len = ( buf[2] << 8 ) | buf[3]; |
| 1158 | chal_len = ( buf[4] << 8 ) | buf[5]; |
| 1159 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1160 | 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] | 1161 | ciph_len, sess_len, chal_len ) ); |
| 1162 | |
| 1163 | /* |
| 1164 | * Make sure each parameter length is valid |
| 1165 | */ |
| 1166 | if( ciph_len < 3 || ( ciph_len % 3 ) != 0 ) |
| 1167 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1168 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1169 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1170 | } |
| 1171 | |
| 1172 | if( sess_len > 32 ) |
| 1173 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1174 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1175 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1176 | } |
| 1177 | |
| 1178 | if( chal_len < 8 || chal_len > 32 ) |
| 1179 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1180 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1181 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1182 | } |
| 1183 | |
| 1184 | if( n != 6 + ciph_len + sess_len + chal_len ) |
| 1185 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1186 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1187 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1188 | } |
| 1189 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1190 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist", |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1191 | buf + 6, ciph_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1192 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id", |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1193 | buf + 6 + ciph_len, sess_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1194 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, challenge", |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1195 | buf + 6 + ciph_len + sess_len, chal_len ); |
| 1196 | |
| 1197 | p = buf + 6 + ciph_len; |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 1198 | ssl->session_negotiate->id_len = sess_len; |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 1199 | memset( ssl->session_negotiate->id, 0, |
| 1200 | sizeof( ssl->session_negotiate->id ) ); |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 1201 | memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->id_len ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1202 | |
| 1203 | p += sess_len; |
| 1204 | memset( ssl->handshake->randbytes, 0, 64 ); |
| 1205 | memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len ); |
| 1206 | |
| 1207 | /* |
| 1208 | * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV |
| 1209 | */ |
| 1210 | for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 ) |
| 1211 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1212 | 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] | 1213 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1214 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) ); |
| 1215 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1216 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1217 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1218 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV " |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1219 | "during renegotiation" ) ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1220 | |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 1221 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 1222 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1223 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1224 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1225 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
| 1226 | ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION; |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1227 | break; |
| 1228 | } |
| 1229 | } |
| 1230 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1231 | #if defined(MBEDTLS_SSL_FALLBACK_SCSV) |
Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1232 | for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 ) |
| 1233 | { |
| 1234 | if( p[0] == 0 && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1235 | p[1] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE >> 8 ) & 0xff ) && |
| 1236 | p[2] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE ) & 0xff ) ) |
Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1237 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1238 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "received FALLBACK_SCSV" ) ); |
Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1239 | |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 1240 | if( mbedtls_ssl_get_minor_ver( ssl ) < |
Hanno Becker | e965bd3 | 2019-06-12 14:04:34 +0100 | [diff] [blame] | 1241 | mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) ) |
Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1242 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1243 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) ); |
Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1244 | |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 1245 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 1246 | MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK ); |
Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1247 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1248 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1249 | } |
| 1250 | |
| 1251 | break; |
| 1252 | } |
| 1253 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1254 | #endif /* MBEDTLS_SSL_FALLBACK_SCSV */ |
Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1255 | |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1256 | got_common_suite = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1257 | #if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE) |
Manuel Pégourié-Gonnard | 1a9f2c7 | 2013-11-30 18:30:06 +0100 | [diff] [blame] | 1258 | 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] | 1259 | { |
Hanno Becker | 981f81d | 2019-07-19 16:10:49 +0100 | [diff] [blame] | 1260 | MBEDTLS_SSL_BEGIN_FOR_EACH_CIPHERSUITE( ssl, \ |
| 1261 | mbedtls_ssl_get_minor_ver( ssl ), \ |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 1262 | cur_info ) |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1263 | { |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 1264 | #else |
Hanno Becker | 981f81d | 2019-07-19 16:10:49 +0100 | [diff] [blame] | 1265 | MBEDTLS_SSL_BEGIN_FOR_EACH_CIPHERSUITE( ssl, \ |
| 1266 | mbedtls_ssl_get_minor_ver( ssl ), \ |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 1267 | cur_info ) |
| 1268 | { |
| 1269 | for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 ) |
| 1270 | { |
| 1271 | #endif |
| 1272 | const int ciphersuite_id = |
| 1273 | mbedtls_ssl_suite_get_id( cur_info ); |
| 1274 | |
Manuel Pégourié-Gonnard | 011a8db | 2013-11-30 18:11:07 +0100 | [diff] [blame] | 1275 | if( p[0] != 0 || |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 1276 | p[1] != ( ( ciphersuite_id >> 8 ) & 0xFF ) || |
| 1277 | p[2] != ( ( ciphersuite_id ) & 0xFF ) ) |
| 1278 | { |
Hanno Becker | f4d6b49 | 2019-07-02 17:13:14 +0100 | [diff] [blame] | 1279 | continue; |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 1280 | } |
Paul Bakker | 59c28a2 | 2013-06-29 15:33:42 +0200 | [diff] [blame] | 1281 | |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1282 | got_common_suite = 1; |
| 1283 | |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 1284 | if( ssl_ciphersuite_is_match( ssl, cur_info, NULL, 0 ) ) |
Hanno Becker | d3b2fcb | 2019-06-17 14:30:05 +0100 | [diff] [blame] | 1285 | { |
Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 1286 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 1287 | ciphersuite_info = cur_info; |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 1288 | #endif |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 1289 | goto have_ciphersuite_v2; |
Hanno Becker | d3b2fcb | 2019-06-17 14:30:05 +0100 | [diff] [blame] | 1290 | } |
Paul Bakker | 59c28a2 | 2013-06-29 15:33:42 +0200 | [diff] [blame] | 1291 | |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 1292 | #if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE) |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1293 | } |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 1294 | MBEDTLS_SSL_END_FOR_EACH_CIPHERSUITE |
| 1295 | } |
| 1296 | #else |
| 1297 | } |
| 1298 | } |
| 1299 | MBEDTLS_SSL_END_FOR_EACH_CIPHERSUITE |
| 1300 | #endif |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1301 | |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1302 | if( got_common_suite ) |
| 1303 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1304 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, " |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1305 | "but none of them usable" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1306 | return( MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE ); |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1307 | } |
| 1308 | else |
| 1309 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1310 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) ); |
| 1311 | return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN ); |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1312 | } |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1313 | |
| 1314 | have_ciphersuite_v2: |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 1315 | |
Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 1316 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 1317 | ssl->session_negotiate->ciphersuite = |
| 1318 | mbedtls_ssl_suite_get_id( ciphersuite_info ); |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1319 | ssl->handshake->ciphersuite_info = ciphersuite_info; |
Hanno Becker | df64596 | 2019-06-26 13:02:22 +0100 | [diff] [blame] | 1320 | #endif |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1321 | |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 1322 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", |
| 1323 | mbedtls_ssl_get_ciphersuite_name( |
| 1324 | mbedtls_ssl_session_get_ciphersuite( ssl->session_negotiate ) ) ) ); |
| 1325 | |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1326 | /* |
| 1327 | * SSLv2 Client Hello relevant renegotiation security checks |
| 1328 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1329 | if( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && |
Hanno Becker | b0b2b67 | 2019-06-12 16:58:10 +0100 | [diff] [blame] | 1330 | mbedtls_ssl_conf_get_allow_legacy_renegotiation( ssl->conf ) == |
| 1331 | MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE ) |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1332 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1333 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 1334 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 1335 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1336 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1337 | } |
| 1338 | |
| 1339 | ssl->in_left = 0; |
| 1340 | ssl->state++; |
| 1341 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1342 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1343 | |
| 1344 | return( 0 ); |
| 1345 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1346 | #endif /* MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */ |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1347 | |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1348 | /* This function doesn't alert on errors that happen early during |
| 1349 | ClientHello parsing because they might indicate that the client is |
| 1350 | 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] | 1351 | static int ssl_parse_client_hello( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1352 | { |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1353 | int ret, got_common_suite; |
Manuel Pégourié-Gonnard | 9de64f5 | 2015-07-01 15:51:43 +0200 | [diff] [blame] | 1354 | size_t i, j; |
| 1355 | size_t ciph_offset, comp_offset, ext_offset; |
| 1356 | 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] | 1357 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 9de64f5 | 2015-07-01 15:51:43 +0200 | [diff] [blame] | 1358 | size_t cookie_offset, cookie_len; |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1359 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1360 | unsigned char *buf, *p, *ext; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1361 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1362 | int renegotiation_info_seen = 0; |
Manuel Pégourié-Gonnard | eaecbd3 | 2014-11-06 02:38:02 +0100 | [diff] [blame] | 1363 | #endif |
Hanno Becker | 03b64fa | 2019-06-11 14:39:38 +0100 | [diff] [blame] | 1364 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 1365 | int extended_ms_seen = 0; |
| 1366 | #endif |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1367 | int handshake_failure = 0; |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 1368 | |
Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 1369 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 1370 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info; |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 1371 | #endif |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 1372 | int major, minor; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1373 | |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 1374 | unsigned char const *acceptable_ec_tls_ids = NULL; |
| 1375 | size_t ec_tls_ids_len = 0; |
Hanno Becker | d3b2fcb | 2019-06-17 14:30:05 +0100 | [diff] [blame] | 1376 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 1377 | /* If there is no signature-algorithm extension present, |
| 1378 | * we need to fall back to the default values for allowed |
| 1379 | * signature-hash pairs. */ |
| 1380 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 1381 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
| 1382 | int sig_hash_alg_ext_present = 0; |
| 1383 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && |
| 1384 | MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
| 1385 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1386 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1387 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1388 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | f03c7aa | 2014-09-24 14:54:06 +0200 | [diff] [blame] | 1389 | read_record_header: |
| 1390 | #endif |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1391 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1392 | * 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] | 1393 | * otherwise read it ourselves manually in order to support SSLv2 |
| 1394 | * ClientHello, which doesn't use the same record layer format. |
| 1395 | */ |
Manuel Pégourié-Gonnard | 754b9f3 | 2019-07-01 12:20:54 +0200 | [diff] [blame] | 1396 | if( mbedtls_ssl_get_renego_status( ssl ) == MBEDTLS_SSL_INITIAL_HANDSHAKE && |
| 1397 | ( ret = mbedtls_ssl_fetch_input( ssl, 5 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1398 | { |
Manuel Pégourié-Gonnard | 754b9f3 | 2019-07-01 12:20:54 +0200 | [diff] [blame] | 1399 | /* No alert on a read error. */ |
| 1400 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret ); |
| 1401 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1402 | } |
| 1403 | |
| 1404 | buf = ssl->in_hdr; |
| 1405 | |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 1406 | #if defined(MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO) && \ |
| 1407 | defined(MBEDTLS_SSL_PROTO_TLS) |
| 1408 | if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) && |
| 1409 | ( buf[0] & 0x80 ) != 0 ) |
| 1410 | { |
| 1411 | return( ssl_parse_client_hello_v2( ssl ) ); |
| 1412 | } |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1413 | #endif |
| 1414 | |
Hanno Becker | 4339576 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 1415 | 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] | 1416 | |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1417 | /* |
Manuel Pégourié-Gonnard | 6b1e207 | 2014-02-12 10:14:54 +0100 | [diff] [blame] | 1418 | * SSLv3/TLS Client Hello |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1419 | * |
| 1420 | * Record layer: |
| 1421 | * 0 . 0 message type |
| 1422 | * 1 . 2 protocol version |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1423 | * 3 . 11 DTLS: epoch + record sequence number |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1424 | * 3 . 4 message length |
| 1425 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1426 | 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] | 1427 | buf[0] ) ); |
| 1428 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1429 | if( buf[0] != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 1430 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1431 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1432 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 1433 | } |
| 1434 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1435 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d", |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1436 | ( ssl->in_len[0] << 8 ) | ssl->in_len[1] ) ); |
| 1437 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1438 | 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] | 1439 | buf[1], buf[2] ) ); |
| 1440 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1441 | 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] | 1442 | |
| 1443 | /* According to RFC 5246 Appendix E.1, the version here is typically |
| 1444 | * "{03,00}, the lowest version number supported by the client, [or] the |
| 1445 | * value of ClientHello.client_version", so the only meaningful check here |
| 1446 | * is the major version shouldn't be less than 3 */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1447 | if( major < MBEDTLS_SSL_MAJOR_VERSION_3 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1448 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1449 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1450 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1451 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1452 | |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1453 | /* For DTLS if this is the initial handshake, remember the client sequence |
| 1454 | * 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] | 1455 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 754b9f3 | 2019-07-01 12:20:54 +0200 | [diff] [blame] | 1456 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
| 1457 | mbedtls_ssl_get_renego_status( ssl ) == MBEDTLS_SSL_INITIAL_HANDSHAKE ) |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1458 | { |
| 1459 | /* Epoch should be 0 for initial handshakes */ |
| 1460 | if( ssl->in_ctr[0] != 0 || ssl->in_ctr[1] != 0 ) |
| 1461 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1462 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1463 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1464 | } |
| 1465 | |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 1466 | 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] | 1467 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1468 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 1469 | if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 ) |
Manuel Pégourié-Gonnard | f03c7aa | 2014-09-24 14:54:06 +0200 | [diff] [blame] | 1470 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1471 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record, discarding" ) ); |
Manuel Pégourié-Gonnard | f03c7aa | 2014-09-24 14:54:06 +0200 | [diff] [blame] | 1472 | ssl->next_record_offset = 0; |
| 1473 | ssl->in_left = 0; |
| 1474 | goto read_record_header; |
| 1475 | } |
| 1476 | |
| 1477 | /* 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] | 1478 | mbedtls_ssl_dtls_replay_update( ssl ); |
Manuel Pégourié-Gonnard | f03c7aa | 2014-09-24 14:54:06 +0200 | [diff] [blame] | 1479 | #endif |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1480 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1481 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1482 | |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1483 | msg_len = ( ssl->in_len[0] << 8 ) | ssl->in_len[1]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1484 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1485 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1486 | if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ) |
Manuel Pégourié-Gonnard | b89c4f3 | 2015-01-21 13:24:10 +0000 | [diff] [blame] | 1487 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1488 | /* Set by mbedtls_ssl_read_record() */ |
Manuel Pégourié-Gonnard | b89c4f3 | 2015-01-21 13:24:10 +0000 | [diff] [blame] | 1489 | msg_len = ssl->in_hslen; |
| 1490 | } |
| 1491 | else |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1492 | #endif |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1493 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1494 | if( msg_len > MBEDTLS_SSL_IN_CONTENT_LEN ) |
Manuel Pégourié-Gonnard | d6b721c | 2014-03-24 12:13:54 +0100 | [diff] [blame] | 1495 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1496 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1497 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | d6b721c | 2014-03-24 12:13:54 +0100 | [diff] [blame] | 1498 | } |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1499 | |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 1500 | if( ( ret = mbedtls_ssl_fetch_input( ssl, |
Hanno Becker | 4339576 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 1501 | mbedtls_ssl_in_hdr_len( ssl ) + msg_len ) ) != 0 ) |
Manuel Pégourié-Gonnard | d6b721c | 2014-03-24 12:13:54 +0100 | [diff] [blame] | 1502 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1503 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret ); |
Manuel Pégourié-Gonnard | d6b721c | 2014-03-24 12:13:54 +0100 | [diff] [blame] | 1504 | return( ret ); |
| 1505 | } |
Manuel Pégourié-Gonnard | 30d16eb | 2014-08-19 17:43:50 +0200 | [diff] [blame] | 1506 | |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 1507 | /* Done reading this record, get ready for the next one */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1508 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 1509 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
| 1510 | { |
Hanno Becker | 4339576 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 1511 | 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] | 1512 | } |
| 1513 | MBEDTLS_SSL_TRANSPORT_ELSE |
Manuel Pégourié-Gonnard | 30d16eb | 2014-08-19 17:43:50 +0200 | [diff] [blame] | 1514 | #endif |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 1515 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
| 1516 | { |
Manuel Pégourié-Gonnard | 30d16eb | 2014-08-19 17:43:50 +0200 | [diff] [blame] | 1517 | ssl->in_left = 0; |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 1518 | } |
| 1519 | #endif |
Manuel Pégourié-Gonnard | d6b721c | 2014-03-24 12:13:54 +0100 | [diff] [blame] | 1520 | } |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1521 | |
| 1522 | buf = ssl->in_msg; |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1523 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1524 | MBEDTLS_SSL_DEBUG_BUF( 4, "record contents", buf, msg_len ); |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 1525 | |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1526 | ssl->handshake->update_checksum( ssl, buf, msg_len ); |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1527 | |
| 1528 | /* |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1529 | * Handshake layer: |
| 1530 | * 0 . 0 handshake type |
| 1531 | * 1 . 3 handshake length |
| 1532 | * 4 . 5 DTLS only: message seqence number |
| 1533 | * 6 . 8 DTLS only: fragment offset |
| 1534 | * 9 . 11 DTLS only: fragment length |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 1535 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1536 | if( msg_len < mbedtls_ssl_hs_hdr_len( ssl ) ) |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1537 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1538 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1539 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1540 | } |
| 1541 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1542 | 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] | 1543 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1544 | if( buf[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1545 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1546 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1547 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1548 | } |
| 1549 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1550 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d", |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1551 | ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) ); |
| 1552 | |
| 1553 | /* We don't support fragmentation of ClientHello (yet?) */ |
| 1554 | if( buf[1] != 0 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1555 | msg_len != mbedtls_ssl_hs_hdr_len( ssl ) + ( ( buf[2] << 8 ) | buf[3] ) ) |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1556 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1557 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1558 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1559 | } |
| 1560 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1561 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 1562 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 1563 | { |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1564 | /* |
Manuel Pégourié-Gonnard | 69849f8 | 2015-03-10 11:54:02 +0000 | [diff] [blame] | 1565 | * Copy the client's handshake message_seq on initial handshakes, |
| 1566 | * check sequence number on renego. |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1567 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1568 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1569 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 1570 | { |
| 1571 | /* This couldn't be done in ssl_prepare_handshake_record() */ |
| 1572 | unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) | |
| 1573 | ssl->in_msg[5]; |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1574 | |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 1575 | if( cli_msg_seq != ssl->handshake->in_msg_seq ) |
| 1576 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1577 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message_seq: " |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 1578 | "%d (expected %d)", cli_msg_seq, |
| 1579 | ssl->handshake->in_msg_seq ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1580 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 1581 | } |
| 1582 | |
| 1583 | ssl->handshake->in_msg_seq++; |
| 1584 | } |
Manuel Pégourié-Gonnard | 69849f8 | 2015-03-10 11:54:02 +0000 | [diff] [blame] | 1585 | else |
| 1586 | #endif |
| 1587 | { |
| 1588 | unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) | |
| 1589 | ssl->in_msg[5]; |
| 1590 | ssl->handshake->out_msg_seq = cli_msg_seq; |
| 1591 | ssl->handshake->in_msg_seq = cli_msg_seq + 1; |
| 1592 | } |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 1593 | |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1594 | /* |
| 1595 | * For now we don't support fragmentation, so make sure |
| 1596 | * fragment_offset == 0 and fragment_length == length |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1597 | */ |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 1598 | if( ssl->in_msg[6] != 0 || ssl->in_msg[7] != 0 || ssl->in_msg[8] != 0 || |
| 1599 | memcmp( ssl->in_msg + 1, ssl->in_msg + 9, 3 ) != 0 ) |
| 1600 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1601 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "ClientHello fragmentation not supported" ) ); |
| 1602 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 1603 | } |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 1604 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1605 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 1606 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1607 | buf += mbedtls_ssl_hs_hdr_len( ssl ); |
| 1608 | msg_len -= mbedtls_ssl_hs_hdr_len( ssl ); |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1609 | |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 1610 | /* |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1611 | * ClientHello layer: |
| 1612 | * 0 . 1 protocol version |
| 1613 | * 2 . 33 random bytes (starting with 4 bytes of Unix time) |
| 1614 | * 34 . 35 session id length (1 byte) |
| 1615 | * 35 . 34+x session id |
| 1616 | * 35+x . 35+x DTLS only: cookie length (1 byte) |
| 1617 | * 36+x . .. DTLS only: cookie |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1618 | * .. . .. ciphersuite list length (2 bytes) |
| 1619 | * .. . .. ciphersuite list |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1620 | * .. . .. compression alg. list length (1 byte) |
| 1621 | * .. . .. compression alg. list |
| 1622 | * .. . .. extensions length (2 bytes, optional) |
| 1623 | * .. . .. extensions (optional) |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1624 | */ |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1625 | |
| 1626 | /* |
Antonin Décimo | d5f4759 | 2019-01-23 15:24:37 +0100 | [diff] [blame] | 1627 | * Minimal length (with everything empty and extensions omitted) is |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1628 | * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can |
| 1629 | * read at least up to session id length without worrying. |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1630 | */ |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1631 | if( msg_len < 38 ) |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1632 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1633 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1634 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1635 | } |
| 1636 | |
| 1637 | /* |
| 1638 | * Check and save the protocol version |
| 1639 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1640 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, version", buf, 2 ); |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1641 | |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 1642 | { |
Hanno Becker | 3fa1ee5 | 2019-05-22 14:44:37 +0100 | [diff] [blame] | 1643 | int minor_ver, major_ver; |
| 1644 | mbedtls_ssl_read_version( &major_ver, &minor_ver, |
| 1645 | ssl->conf->transport, |
| 1646 | buf ); |
| 1647 | |
Hanno Becker | 18729ae | 2019-06-12 14:47:21 +0100 | [diff] [blame] | 1648 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \ |
| 1649 | defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
Hanno Becker | 3fa1ee5 | 2019-05-22 14:44:37 +0100 | [diff] [blame] | 1650 | ssl->handshake->max_major_ver = major_ver; |
| 1651 | ssl->handshake->max_minor_ver = minor_ver; |
Hanno Becker | 18729ae | 2019-06-12 14:47:21 +0100 | [diff] [blame] | 1652 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED || |
| 1653 | MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
Hanno Becker | 3fa1ee5 | 2019-05-22 14:44:37 +0100 | [diff] [blame] | 1654 | |
| 1655 | if( major_ver < mbedtls_ssl_conf_get_min_major_ver( ssl->conf ) || |
| 1656 | minor_ver < mbedtls_ssl_conf_get_min_minor_ver( ssl->conf ) ) |
| 1657 | { |
| 1658 | 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] | 1659 | " [%d:%d] < [%d:%d]", |
Hanno Becker | 3fa1ee5 | 2019-05-22 14:44:37 +0100 | [diff] [blame] | 1660 | major_ver, minor_ver, |
Hanno Becker | e965bd3 | 2019-06-12 14:04:34 +0100 | [diff] [blame] | 1661 | mbedtls_ssl_conf_get_min_major_ver( ssl->conf ), |
| 1662 | mbedtls_ssl_conf_get_min_minor_ver( ssl->conf ) ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 1663 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 1664 | MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION ); |
Hanno Becker | 3fa1ee5 | 2019-05-22 14:44:37 +0100 | [diff] [blame] | 1665 | return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION ); |
| 1666 | } |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 1667 | |
Hanno Becker | 3fa1ee5 | 2019-05-22 14:44:37 +0100 | [diff] [blame] | 1668 | if( major_ver > mbedtls_ssl_conf_get_max_major_ver( ssl->conf ) ) |
| 1669 | { |
| 1670 | major_ver = mbedtls_ssl_conf_get_max_major_ver( ssl->conf ); |
| 1671 | minor_ver = mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ); |
| 1672 | } |
| 1673 | else if( minor_ver > mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) ) |
| 1674 | minor_ver = mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ); |
| 1675 | |
Hanno Becker | 381eaa5 | 2019-06-12 14:43:01 +0100 | [diff] [blame] | 1676 | #if !defined(MBEDTLS_SSL_CONF_FIXED_MAJOR_VER) |
Hanno Becker | 3fa1ee5 | 2019-05-22 14:44:37 +0100 | [diff] [blame] | 1677 | ssl->major_ver = major_ver; |
Hanno Becker | 381eaa5 | 2019-06-12 14:43:01 +0100 | [diff] [blame] | 1678 | #endif /* MBEDTLS_SSL_CONF_FIXED_MAJOR_VER */ |
| 1679 | #if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER) |
Hanno Becker | 3fa1ee5 | 2019-05-22 14:44:37 +0100 | [diff] [blame] | 1680 | ssl->minor_ver = minor_ver; |
Hanno Becker | 381eaa5 | 2019-06-12 14:43:01 +0100 | [diff] [blame] | 1681 | #endif /* MBEDTLS_SSL_CONF_FIXED_MINOR_VER */ |
Manuel Pégourié-Gonnard | 6b1e207 | 2014-02-12 10:14:54 +0100 | [diff] [blame] | 1682 | } |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1683 | |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1684 | /* |
| 1685 | * Save client random (inc. Unix time) |
| 1686 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1687 | 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] | 1688 | |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1689 | memcpy( ssl->handshake->randbytes, buf + 2, 32 ); |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1690 | |
| 1691 | /* |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1692 | * Check the session ID length and save session ID |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1693 | */ |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1694 | sess_len = buf[34]; |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1695 | |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1696 | if( sess_len > sizeof( ssl->session_negotiate->id ) || |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1697 | sess_len + 34 + 2 > msg_len ) /* 2 for cipherlist length field */ |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1698 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1699 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 1700 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 1701 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1702 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1703 | } |
| 1704 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1705 | 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] | 1706 | |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 1707 | ssl->session_negotiate->id_len = sess_len; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1708 | memset( ssl->session_negotiate->id, 0, |
| 1709 | sizeof( ssl->session_negotiate->id ) ); |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1710 | memcpy( ssl->session_negotiate->id, buf + 35, |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 1711 | ssl->session_negotiate->id_len ); |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1712 | |
| 1713 | /* |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1714 | * Check the cookie length and content |
| 1715 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1716 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 1717 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1718 | { |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1719 | cookie_offset = 35 + sess_len; |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1720 | cookie_len = buf[cookie_offset]; |
| 1721 | |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1722 | if( cookie_offset + 1 + cookie_len + 2 > msg_len ) |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1723 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1724 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 1725 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 1726 | MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1727 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1728 | } |
| 1729 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1730 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie", |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1731 | buf + cookie_offset + 1, cookie_len ); |
| 1732 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1733 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) |
Manuel Pégourié-Gonnard | 754b9f3 | 2019-07-01 12:20:54 +0200 | [diff] [blame] | 1734 | if( ssl->conf->f_cookie_check != NULL && |
| 1735 | mbedtls_ssl_get_renego_status( ssl ) == MBEDTLS_SSL_INITIAL_HANDSHAKE ) |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1736 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1737 | if( ssl->conf->f_cookie_check( ssl->conf->p_cookie, |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1738 | buf + cookie_offset + 1, cookie_len, |
| 1739 | ssl->cli_id, ssl->cli_id_len ) != 0 ) |
| 1740 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1741 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification failed" ) ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1742 | ssl->handshake->verify_cookie_len = 1; |
| 1743 | } |
| 1744 | else |
| 1745 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1746 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification passed" ) ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1747 | ssl->handshake->verify_cookie_len = 0; |
| 1748 | } |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1749 | } |
Manuel Pégourié-Gonnard | d7f9bc5 | 2014-07-23 11:09:27 +0200 | [diff] [blame] | 1750 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1751 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */ |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1752 | { |
| 1753 | /* We know we didn't send a cookie, so it should be empty */ |
| 1754 | if( cookie_len != 0 ) |
| 1755 | { |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1756 | /* 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] | 1757 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1758 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1759 | } |
| 1760 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1761 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification skipped" ) ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1762 | } |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1763 | |
| 1764 | /* |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1765 | * Check the ciphersuitelist length (will be parsed later) |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1766 | */ |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1767 | ciph_offset = cookie_offset + 1 + cookie_len; |
Manuel Pégourié-Gonnard | a06d7fe | 2015-03-13 10:36:55 +0000 | [diff] [blame] | 1768 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 1769 | MBEDTLS_SSL_TRANSPORT_ELSE |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1770 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 1771 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
| 1772 | { |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1773 | ciph_offset = 35 + sess_len; |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 1774 | } |
Manuel Pégourié-Gonnard | ec1c222 | 2019-06-12 10:18:26 +0200 | [diff] [blame] | 1775 | #endif /* MBEDTLS_SSL_PROTO_TLS */ |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1776 | |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1777 | ciph_len = ( buf[ciph_offset + 0] << 8 ) |
| 1778 | | ( buf[ciph_offset + 1] ); |
| 1779 | |
| 1780 | if( ciph_len < 2 || |
| 1781 | ciph_len + 2 + ciph_offset + 1 > msg_len || /* 1 for comp. alg. len */ |
| 1782 | ( ciph_len % 2 ) != 0 ) |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1783 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1784 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 1785 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 1786 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1787 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1788 | } |
| 1789 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1790 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist", |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1791 | buf + ciph_offset + 2, ciph_len ); |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1792 | |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1793 | /* |
| 1794 | * Check the compression algorithms length and pick one |
| 1795 | */ |
| 1796 | comp_offset = ciph_offset + 2 + ciph_len; |
| 1797 | |
| 1798 | comp_len = buf[comp_offset]; |
| 1799 | |
| 1800 | if( comp_len < 1 || |
| 1801 | comp_len > 16 || |
| 1802 | comp_len + comp_offset + 1 > msg_len ) |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1803 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1804 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 1805 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 1806 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1807 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1808 | } |
| 1809 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1810 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, compression", |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1811 | buf + comp_offset + 1, comp_len ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1812 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1813 | ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL; |
| 1814 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1815 | for( i = 0; i < comp_len; ++i ) |
| 1816 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1817 | if( buf[comp_offset + 1 + i] == MBEDTLS_SSL_COMPRESS_DEFLATE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1818 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1819 | ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_DEFLATE; |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1820 | break; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1821 | } |
| 1822 | } |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1823 | #endif |
| 1824 | |
Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1825 | /* See comments in ssl_write_client_hello() */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1826 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 1827 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1828 | ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL; |
Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1829 | #endif |
Manuel Pégourié-Gonnard | 82202f0 | 2014-07-23 00:28:58 +0200 | [diff] [blame] | 1830 | |
Janos Follath | c6dab2b | 2016-05-23 14:27:02 +0100 | [diff] [blame] | 1831 | /* Do not parse the extensions if the protocol is SSLv3 */ |
| 1832 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 1833 | if( ( mbedtls_ssl_get_major_ver( ssl ) != 3 ) || |
| 1834 | ( mbedtls_ssl_get_minor_ver( ssl ) != 0 ) ) |
Janos Follath | c6dab2b | 2016-05-23 14:27:02 +0100 | [diff] [blame] | 1835 | { |
| 1836 | #endif |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1837 | /* |
| 1838 | * Check the extension length |
| 1839 | */ |
| 1840 | ext_offset = comp_offset + 1 + comp_len; |
| 1841 | if( msg_len > ext_offset ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1842 | { |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1843 | if( msg_len < ext_offset + 2 ) |
| 1844 | { |
| 1845 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 1846 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 1847 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1848 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 1849 | } |
| 1850 | |
| 1851 | ext_len = ( buf[ext_offset + 0] << 8 ) |
| 1852 | | ( buf[ext_offset + 1] ); |
| 1853 | |
| 1854 | if( ( ext_len > 0 && ext_len < 4 ) || |
| 1855 | msg_len != ext_offset + 2 + ext_len ) |
| 1856 | { |
| 1857 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 1858 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 1859 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1860 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 1861 | } |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1862 | } |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1863 | else |
| 1864 | ext_len = 0; |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1865 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1866 | ext = buf + ext_offset + 2; |
| 1867 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", ext, ext_len ); |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1868 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1869 | while( ext_len != 0 ) |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1870 | { |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 1871 | unsigned int ext_id; |
| 1872 | unsigned int ext_size; |
| 1873 | if ( ext_len < 4 ) { |
| 1874 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 1875 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 1876 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 1877 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 1878 | } |
| 1879 | ext_id = ( ( ext[0] << 8 ) | ( ext[1] ) ); |
| 1880 | ext_size = ( ( ext[2] << 8 ) | ( ext[3] ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1881 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1882 | if( ext_size + 4 > ext_len ) |
| 1883 | { |
| 1884 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 1885 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 1886 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1887 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 1888 | } |
| 1889 | switch( ext_id ) |
| 1890 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1891 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1892 | case MBEDTLS_TLS_EXT_SERVERNAME: |
| 1893 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) ); |
| 1894 | if( ssl->conf->f_sni == NULL ) |
| 1895 | break; |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 1896 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1897 | ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size ); |
| 1898 | if( ret != 0 ) |
| 1899 | return( ret ); |
| 1900 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1901 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 1902 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1903 | case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO: |
| 1904 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1905 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1906 | renegotiation_info_seen = 1; |
Manuel Pégourié-Gonnard | eaecbd3 | 2014-11-06 02:38:02 +0100 | [diff] [blame] | 1907 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1908 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1909 | ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size ); |
| 1910 | if( ret != 0 ) |
| 1911 | return( ret ); |
| 1912 | break; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1913 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1914 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 1915 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1916 | case MBEDTLS_TLS_EXT_SIG_ALG: |
Ron Eldor | 73a3817 | 2017-10-03 15:58:26 +0300 | [diff] [blame] | 1917 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) ); |
| 1918 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1919 | ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size ); |
| 1920 | if( ret != 0 ) |
| 1921 | return( ret ); |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 1922 | |
| 1923 | sig_hash_alg_ext_present = 1; |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1924 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1925 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && |
| 1926 | MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1927 | |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 1928 | #if defined(MBEDTLS_ECDH_C) || \ |
| 1929 | defined(MBEDTLS_ECDSA_C) || \ |
| 1930 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) || \ |
| 1931 | defined(MBEDTLS_USE_TINYCRYPT) |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1932 | case MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES: |
| 1933 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1934 | |
Hanno Becker | d3b2fcb | 2019-06-17 14:30:05 +0100 | [diff] [blame] | 1935 | ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, |
| 1936 | ext_size, |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 1937 | &acceptable_ec_tls_ids, |
| 1938 | &ec_tls_ids_len ); |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1939 | if( ret != 0 ) |
| 1940 | return( ret ); |
| 1941 | break; |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1942 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1943 | case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS: |
| 1944 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) ); |
| 1945 | ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT; |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1946 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1947 | ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size ); |
| 1948 | if( ret != 0 ) |
| 1949 | return( ret ); |
| 1950 | break; |
Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 1951 | #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 1952 | MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED || |
| 1953 | MBEDTLS_USE_TINYCRYPT */ |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1954 | |
Manuel Pégourié-Gonnard | bf57be6 | 2015-09-16 15:04:01 +0200 | [diff] [blame] | 1955 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1956 | case MBEDTLS_TLS_EXT_ECJPAKE_KKPP: |
| 1957 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake kkpp extension" ) ); |
Manuel Pégourié-Gonnard | bf57be6 | 2015-09-16 15:04:01 +0200 | [diff] [blame] | 1958 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1959 | ret = ssl_parse_ecjpake_kkpp( ssl, ext + 4, ext_size ); |
| 1960 | if( ret != 0 ) |
| 1961 | return( ret ); |
| 1962 | break; |
Manuel Pégourié-Gonnard | bf57be6 | 2015-09-16 15:04:01 +0200 | [diff] [blame] | 1963 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
| 1964 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1965 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1966 | case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH: |
| 1967 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) ); |
Manuel Pégourié-Gonnard | 48f8d0d | 2013-07-17 10:25:37 +0200 | [diff] [blame] | 1968 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1969 | ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size ); |
| 1970 | if( ret != 0 ) |
| 1971 | return( ret ); |
| 1972 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1973 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 48f8d0d | 2013-07-17 10:25:37 +0200 | [diff] [blame] | 1974 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1975 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1976 | case MBEDTLS_TLS_EXT_TRUNCATED_HMAC: |
| 1977 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) ); |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 1978 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1979 | ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size ); |
| 1980 | if( ret != 0 ) |
| 1981 | return( ret ); |
| 1982 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1983 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 1984 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1985 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | c403b26 | 2019-04-26 13:56:39 +0100 | [diff] [blame] | 1986 | case MBEDTLS_TLS_EXT_CID: |
| 1987 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found CID extension" ) ); |
| 1988 | |
| 1989 | ret = ssl_parse_cid_ext( ssl, ext + 4, ext_size ); |
| 1990 | if( ret != 0 ) |
| 1991 | return( ret ); |
| 1992 | break; |
| 1993 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
| 1994 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1995 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1996 | case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC: |
| 1997 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) ); |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 1998 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1999 | ret = ssl_parse_encrypt_then_mac_ext( ssl, ext + 4, ext_size ); |
| 2000 | if( ret != 0 ) |
| 2001 | return( ret ); |
| 2002 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2003 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2004 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2005 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2006 | case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET: |
| 2007 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) ); |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2008 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2009 | ret = ssl_parse_extended_ms_ext( ssl, ext + 4, ext_size ); |
| 2010 | if( ret != 0 ) |
| 2011 | return( ret ); |
Hanno Becker | 03b64fa | 2019-06-11 14:39:38 +0100 | [diff] [blame] | 2012 | extended_ms_seen = 1; |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2013 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2014 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2015 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2016 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2017 | case MBEDTLS_TLS_EXT_SESSION_TICKET: |
| 2018 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) ); |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 2019 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2020 | ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size ); |
| 2021 | if( ret != 0 ) |
| 2022 | return( ret ); |
| 2023 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2024 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 2025 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2026 | #if defined(MBEDTLS_SSL_ALPN) |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2027 | case MBEDTLS_TLS_EXT_ALPN: |
| 2028 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) ); |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 2029 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2030 | ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ); |
| 2031 | if( ret != 0 ) |
| 2032 | return( ret ); |
| 2033 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2034 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 2035 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2036 | default: |
| 2037 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)", |
| 2038 | ext_id ) ); |
| 2039 | } |
| 2040 | |
| 2041 | ext_len -= 4 + ext_size; |
| 2042 | ext += 4 + ext_size; |
| 2043 | |
| 2044 | if( ext_len > 0 && ext_len < 4 ) |
| 2045 | { |
| 2046 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 2047 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 2048 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 2049 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 2050 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2051 | } |
Janos Follath | c6dab2b | 2016-05-23 14:27:02 +0100 | [diff] [blame] | 2052 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 2053 | } |
| 2054 | #endif |
| 2055 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2056 | #if defined(MBEDTLS_SSL_FALLBACK_SCSV) |
Gilles Peskine | d50177f | 2017-05-16 17:53:03 +0200 | [diff] [blame] | 2057 | 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] | 2058 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2059 | if( p[0] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE >> 8 ) & 0xff ) && |
| 2060 | p[1] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE ) & 0xff ) ) |
Manuel Pégourié-Gonnard | fedba98 | 2014-11-05 16:12:09 +0100 | [diff] [blame] | 2061 | { |
Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 2062 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "received FALLBACK_SCSV" ) ); |
Manuel Pégourié-Gonnard | fedba98 | 2014-11-05 16:12:09 +0100 | [diff] [blame] | 2063 | |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 2064 | if( mbedtls_ssl_get_minor_ver( ssl ) < |
Hanno Becker | e965bd3 | 2019-06-12 14:04:34 +0100 | [diff] [blame] | 2065 | mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) ) |
Manuel Pégourié-Gonnard | fedba98 | 2014-11-05 16:12:09 +0100 | [diff] [blame] | 2066 | { |
Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 2067 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) ); |
Manuel Pégourié-Gonnard | fedba98 | 2014-11-05 16:12:09 +0100 | [diff] [blame] | 2068 | |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 2069 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 2070 | MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK ); |
Manuel Pégourié-Gonnard | fedba98 | 2014-11-05 16:12:09 +0100 | [diff] [blame] | 2071 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2072 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | fedba98 | 2014-11-05 16:12:09 +0100 | [diff] [blame] | 2073 | } |
| 2074 | |
| 2075 | break; |
| 2076 | } |
| 2077 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2078 | #endif /* MBEDTLS_SSL_FALLBACK_SCSV */ |
Manuel Pégourié-Gonnard | fedba98 | 2014-11-05 16:12:09 +0100 | [diff] [blame] | 2079 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 2080 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 2081 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
| 2082 | |
| 2083 | /* |
| 2084 | * Try to fall back to default hash SHA1 if the client |
| 2085 | * hasn't provided any preferred signature-hash combinations. |
| 2086 | */ |
| 2087 | if( sig_hash_alg_ext_present == 0 ) |
| 2088 | { |
| 2089 | mbedtls_md_type_t md_default = MBEDTLS_MD_SHA1; |
| 2090 | |
| 2091 | if( mbedtls_ssl_check_sig_hash( ssl, md_default ) != 0 ) |
| 2092 | md_default = MBEDTLS_MD_NONE; |
| 2093 | |
| 2094 | mbedtls_ssl_sig_hash_set_const_hash( &ssl->handshake->hash_algs, md_default ); |
| 2095 | } |
| 2096 | |
| 2097 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && |
| 2098 | MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
| 2099 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2100 | /* |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 2101 | * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV |
| 2102 | */ |
| 2103 | for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 ) |
| 2104 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2105 | 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] | 2106 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2107 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) ); |
| 2108 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 2109 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 2110 | { |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 2111 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV " |
| 2112 | "during renegotiation" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 2113 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 2114 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2115 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 2116 | } |
Manuel Pégourié-Gonnard | 69849f8 | 2015-03-10 11:54:02 +0000 | [diff] [blame] | 2117 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2118 | ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION; |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 2119 | break; |
| 2120 | } |
| 2121 | } |
| 2122 | |
| 2123 | /* |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2124 | * Renegotiation security checks |
| 2125 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2126 | if( ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION && |
Hanno Becker | b0b2b67 | 2019-06-12 16:58:10 +0100 | [diff] [blame] | 2127 | mbedtls_ssl_conf_get_allow_legacy_renegotiation( ssl->conf ) == |
| 2128 | MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE ) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2129 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2130 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) ); |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2131 | handshake_failure = 1; |
| 2132 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2133 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 2134 | else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && |
| 2135 | ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION && |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2136 | renegotiation_info_seen == 0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2137 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2138 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) ); |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2139 | handshake_failure = 1; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2140 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2141 | else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && |
| 2142 | ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_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_NO_RENEGOTIATION ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +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 not allowed" ) ); |
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 | else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && |
| 2150 | ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2151 | renegotiation_info_seen == 1 ) |
| 2152 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2153 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) ); |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2154 | handshake_failure = 1; |
| 2155 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2156 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2157 | |
Jarno Lamsa | 842be16 | 2019-06-10 15:05:33 +0300 | [diff] [blame] | 2158 | /* |
| 2159 | * Check if extended master secret is being enforced |
| 2160 | */ |
| 2161 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Hanno Becker | aabbb58 | 2019-06-11 13:43:27 +0100 | [diff] [blame] | 2162 | if( mbedtls_ssl_conf_get_ems( ssl->conf ) == |
Hanno Becker | 03b64fa | 2019-06-11 14:39:38 +0100 | [diff] [blame] | 2163 | MBEDTLS_SSL_EXTENDED_MS_ENABLED ) |
Jarno Lamsa | 842be16 | 2019-06-10 15:05:33 +0300 | [diff] [blame] | 2164 | { |
Hanno Becker | 03b64fa | 2019-06-11 14:39:38 +0100 | [diff] [blame] | 2165 | if( extended_ms_seen ) |
| 2166 | { |
Hanno Becker | 1ab322b | 2019-06-11 14:50:54 +0100 | [diff] [blame] | 2167 | #if !defined(MBEDTLS_SSL_EXTENDED_MS_ENFORCED) |
Hanno Becker | 03b64fa | 2019-06-11 14:39:38 +0100 | [diff] [blame] | 2168 | ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; |
Hanno Becker | 1ab322b | 2019-06-11 14:50:54 +0100 | [diff] [blame] | 2169 | #endif /* !MBEDTLS_SSL_EXTENDED_MS_ENFORCED */ |
Hanno Becker | 03b64fa | 2019-06-11 14:39:38 +0100 | [diff] [blame] | 2170 | } |
| 2171 | else if( mbedtls_ssl_conf_get_ems_enforced( ssl->conf ) == |
| 2172 | MBEDTLS_SSL_EXTENDED_MS_ENFORCE_ENABLED ) |
| 2173 | { |
| 2174 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Peer not offering extended master " |
| 2175 | "secret, while it is enforced") ); |
| 2176 | handshake_failure = 1; |
| 2177 | } |
Jarno Lamsa | 842be16 | 2019-06-10 15:05:33 +0300 | [diff] [blame] | 2178 | } |
| 2179 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
| 2180 | |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2181 | if( handshake_failure == 1 ) |
| 2182 | { |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 2183 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 2184 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2185 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2186 | } |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2187 | |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2188 | /* |
| 2189 | * Search for a matching ciphersuite |
Manuel Pégourié-Gonnard | 3ebb2cd | 2013-09-23 17:00:18 +0200 | [diff] [blame] | 2190 | * (At the end because we need information from the EC-based extensions |
| 2191 | * and certificate from the SNI callback triggered by the SNI extension.) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2192 | */ |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 2193 | got_common_suite = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2194 | #if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE) |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 2195 | 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] | 2196 | { |
Hanno Becker | 981f81d | 2019-07-19 16:10:49 +0100 | [diff] [blame] | 2197 | MBEDTLS_SSL_BEGIN_FOR_EACH_CIPHERSUITE( ssl, \ |
| 2198 | mbedtls_ssl_get_minor_ver( ssl ), \ |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 2199 | cur_info ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2200 | { |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 2201 | #else |
Hanno Becker | 981f81d | 2019-07-19 16:10:49 +0100 | [diff] [blame] | 2202 | MBEDTLS_SSL_BEGIN_FOR_EACH_CIPHERSUITE( ssl, \ |
| 2203 | mbedtls_ssl_get_minor_ver( ssl ), \ |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 2204 | cur_info ) |
| 2205 | { |
| 2206 | for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 ) |
| 2207 | { |
| 2208 | #endif |
| 2209 | const int ciphersuite_id = |
| 2210 | mbedtls_ssl_suite_get_id( cur_info ); |
| 2211 | |
| 2212 | if( p[0] != ( ( ciphersuite_id >> 8 ) & 0xFF ) || |
| 2213 | p[1] != ( ( ciphersuite_id ) & 0xFF ) ) |
| 2214 | { |
Hanno Becker | f4d6b49 | 2019-07-02 17:13:14 +0100 | [diff] [blame] | 2215 | continue; |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 2216 | } |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2217 | |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 2218 | got_common_suite = 1; |
| 2219 | |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 2220 | if( ssl_ciphersuite_is_match( ssl, cur_info, |
Hanno Becker | 84fb902 | 2019-06-18 16:46:26 +0100 | [diff] [blame] | 2221 | acceptable_ec_tls_ids, |
| 2222 | ec_tls_ids_len ) != 0 ) |
Hanno Becker | d3b2fcb | 2019-06-17 14:30:05 +0100 | [diff] [blame] | 2223 | { |
Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 2224 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 2225 | ciphersuite_info = cur_info; |
Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 2226 | #endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 2227 | goto have_ciphersuite; |
Hanno Becker | d3b2fcb | 2019-06-17 14:30:05 +0100 | [diff] [blame] | 2228 | } |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 2229 | #if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2230 | } |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 2231 | MBEDTLS_SSL_END_FOR_EACH_CIPHERSUITE |
| 2232 | } |
| 2233 | #else |
| 2234 | } |
| 2235 | } |
| 2236 | MBEDTLS_SSL_END_FOR_EACH_CIPHERSUITE |
| 2237 | #endif |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2238 | |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 2239 | if( got_common_suite ) |
| 2240 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2241 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, " |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 2242 | "but none of them usable" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 2243 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 2244 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2245 | return( MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE ); |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 2246 | } |
| 2247 | else |
| 2248 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2249 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 2250 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 2251 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2252 | return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN ); |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 2253 | } |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2254 | |
| 2255 | have_ciphersuite: |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 2256 | |
Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 2257 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 2258 | ssl->session_negotiate->ciphersuite = |
| 2259 | mbedtls_ssl_suite_get_id( ciphersuite_info ); |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 2260 | ssl->handshake->ciphersuite_info = ciphersuite_info; |
Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 2261 | #endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2262 | |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 2263 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", |
| 2264 | mbedtls_ssl_get_ciphersuite_name( |
| 2265 | mbedtls_ssl_session_get_ciphersuite( ssl->session_negotiate ) ) ) ); |
| 2266 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2267 | ssl->state++; |
| 2268 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2269 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 2270 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2271 | mbedtls_ssl_recv_flight_completed( ssl ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2272 | #endif |
| 2273 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 2274 | /* Debugging-only output for testsuite */ |
| 2275 | #if defined(MBEDTLS_DEBUG_C) && \ |
| 2276 | defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 2277 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 2278 | if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 2279 | { |
Hanno Becker | 2d46b4f | 2019-07-01 11:06:34 +0100 | [diff] [blame] | 2280 | mbedtls_pk_type_t sig_alg = mbedtls_ssl_get_ciphersuite_sig_alg( |
| 2281 | mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ) ); |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 2282 | if( sig_alg != MBEDTLS_PK_NONE ) |
| 2283 | { |
| 2284 | mbedtls_md_type_t md_alg = mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs, |
| 2285 | sig_alg ); |
| 2286 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d", |
| 2287 | mbedtls_ssl_hash_from_md_alg( md_alg ) ) ); |
| 2288 | } |
| 2289 | else |
| 2290 | { |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 2291 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no hash algorithm for signature algorithm " |
| 2292 | "%d - should not happen", sig_alg ) ); |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 2293 | } |
| 2294 | } |
| 2295 | #endif |
| 2296 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2297 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2298 | |
| 2299 | return( 0 ); |
| 2300 | } |
| 2301 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2302 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 2303 | 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] | 2304 | unsigned char *buf, |
| 2305 | size_t *olen ) |
| 2306 | { |
| 2307 | unsigned char *p = buf; |
| 2308 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2309 | 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] | 2310 | { |
| 2311 | *olen = 0; |
| 2312 | return; |
| 2313 | } |
| 2314 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2315 | 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] | 2316 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2317 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF ); |
| 2318 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_TRUNCATED_HMAC ) & 0xFF ); |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 2319 | |
| 2320 | *p++ = 0x00; |
| 2321 | *p++ = 0x00; |
| 2322 | |
| 2323 | *olen = 4; |
| 2324 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2325 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 2326 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2327 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 072d4ec | 2019-04-26 15:46:55 +0100 | [diff] [blame] | 2328 | static void ssl_write_cid_ext( mbedtls_ssl_context *ssl, |
| 2329 | unsigned char *buf, |
| 2330 | size_t *olen ) |
| 2331 | { |
| 2332 | unsigned char *p = buf; |
| 2333 | size_t ext_len; |
| 2334 | const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN; |
| 2335 | |
| 2336 | *olen = 0; |
| 2337 | |
| 2338 | /* Skip writing the extension if we don't want to use it or if |
| 2339 | * the client hasn't offered it. */ |
| 2340 | if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_DISABLED ) |
| 2341 | return; |
| 2342 | |
| 2343 | /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX |
| 2344 | * which is at most 255, so the increment cannot overflow. */ |
| 2345 | if( end < p || (size_t)( end - p ) < (unsigned)( ssl->own_cid_len + 5 ) ) |
| 2346 | { |
| 2347 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) ); |
| 2348 | return; |
| 2349 | } |
| 2350 | |
| 2351 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding CID extension" ) ); |
| 2352 | |
| 2353 | /* |
Hanno Becker | 3cdf8fe | 2019-05-15 10:26:32 +0100 | [diff] [blame] | 2354 | * Quoting draft-ietf-tls-dtls-connection-id-05 |
| 2355 | * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05 |
Hanno Becker | 072d4ec | 2019-04-26 15:46:55 +0100 | [diff] [blame] | 2356 | * |
| 2357 | * struct { |
| 2358 | * opaque cid<0..2^8-1>; |
| 2359 | * } ConnectionId; |
| 2360 | */ |
| 2361 | |
| 2362 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_CID >> 8 ) & 0xFF ); |
| 2363 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_CID ) & 0xFF ); |
| 2364 | ext_len = (size_t) ssl->own_cid_len + 1; |
| 2365 | *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF ); |
| 2366 | *p++ = (unsigned char)( ( ext_len ) & 0xFF ); |
| 2367 | |
| 2368 | *p++ = (uint8_t) ssl->own_cid_len; |
| 2369 | memcpy( p, ssl->own_cid, ssl->own_cid_len ); |
| 2370 | |
| 2371 | *olen = ssl->own_cid_len + 5; |
| 2372 | } |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2373 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 072d4ec | 2019-04-26 15:46:55 +0100 | [diff] [blame] | 2374 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2375 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 2376 | 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] | 2377 | unsigned char *buf, |
| 2378 | size_t *olen ) |
| 2379 | { |
| 2380 | unsigned char *p = buf; |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 2381 | mbedtls_ssl_ciphersuite_handle_t suite = |
| 2382 | MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2383 | const mbedtls_cipher_info_t *cipher = NULL; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2384 | |
Hanno Becker | 27b34d5 | 2017-10-20 14:24:51 +0100 | [diff] [blame] | 2385 | if( ssl->session_negotiate->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED || |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 2386 | 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] | 2387 | { |
| 2388 | *olen = 0; |
| 2389 | return; |
| 2390 | } |
| 2391 | |
Manuel Pégourié-Gonnard | 78e745f | 2014-11-04 15:44:06 +0100 | [diff] [blame] | 2392 | /* |
| 2393 | * RFC 7366: "If a server receives an encrypt-then-MAC request extension |
| 2394 | * from a client and then selects a stream or Authenticated Encryption |
| 2395 | * with Associated Data (AEAD) ciphersuite, it MUST NOT send an |
| 2396 | * encrypt-then-MAC response extension back to the client." |
| 2397 | */ |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 2398 | suite = mbedtls_ssl_ciphersuite_from_id( |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 2399 | mbedtls_ssl_session_get_ciphersuite( ssl->session_negotiate ) ); |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 2400 | if( suite == MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE ) |
| 2401 | { |
| 2402 | *olen = 0; |
| 2403 | return; |
| 2404 | } |
| 2405 | |
| 2406 | cipher = mbedtls_cipher_info_from_type( |
| 2407 | mbedtls_ssl_suite_get_cipher( suite ) ); |
| 2408 | if( cipher == NULL || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2409 | cipher->mode != MBEDTLS_MODE_CBC ) |
Manuel Pégourié-Gonnard | 78e745f | 2014-11-04 15:44:06 +0100 | [diff] [blame] | 2410 | { |
| 2411 | *olen = 0; |
| 2412 | return; |
| 2413 | } |
| 2414 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2415 | 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] | 2416 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2417 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF ); |
| 2418 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF ); |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2419 | |
| 2420 | *p++ = 0x00; |
| 2421 | *p++ = 0x00; |
| 2422 | |
| 2423 | *olen = 4; |
| 2424 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2425 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2426 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2427 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 2428 | 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] | 2429 | unsigned char *buf, |
| 2430 | size_t *olen ) |
| 2431 | { |
| 2432 | unsigned char *p = buf; |
| 2433 | |
Hanno Becker | a49ec56 | 2019-06-11 14:47:55 +0100 | [diff] [blame] | 2434 | if( mbedtls_ssl_hs_get_extended_ms( ssl->handshake ) |
| 2435 | == MBEDTLS_SSL_EXTENDED_MS_DISABLED || |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 2436 | 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] | 2437 | { |
| 2438 | *olen = 0; |
| 2439 | return; |
| 2440 | } |
| 2441 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2442 | 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] | 2443 | "extension" ) ); |
| 2444 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2445 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF ); |
| 2446 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF ); |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2447 | |
| 2448 | *p++ = 0x00; |
| 2449 | *p++ = 0x00; |
| 2450 | |
| 2451 | *olen = 4; |
| 2452 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2453 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2454 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2455 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 2456 | 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] | 2457 | unsigned char *buf, |
| 2458 | size_t *olen ) |
| 2459 | { |
| 2460 | unsigned char *p = buf; |
| 2461 | |
| 2462 | if( ssl->handshake->new_session_ticket == 0 ) |
| 2463 | { |
| 2464 | *olen = 0; |
| 2465 | return; |
| 2466 | } |
| 2467 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2468 | 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] | 2469 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2470 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF ); |
| 2471 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET ) & 0xFF ); |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 2472 | |
| 2473 | *p++ = 0x00; |
| 2474 | *p++ = 0x00; |
| 2475 | |
| 2476 | *olen = 4; |
| 2477 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2478 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 2479 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2480 | static void ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | f11a6d7 | 2013-07-17 11:17:14 +0200 | [diff] [blame] | 2481 | unsigned char *buf, |
| 2482 | size_t *olen ) |
| 2483 | { |
| 2484 | unsigned char *p = buf; |
| 2485 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2486 | if( ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION ) |
Manuel Pégourié-Gonnard | f11a6d7 | 2013-07-17 11:17:14 +0200 | [diff] [blame] | 2487 | { |
| 2488 | *olen = 0; |
| 2489 | return; |
| 2490 | } |
| 2491 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2492 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) ); |
Manuel Pégourié-Gonnard | f11a6d7 | 2013-07-17 11:17:14 +0200 | [diff] [blame] | 2493 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2494 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF ); |
| 2495 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO ) & 0xFF ); |
Manuel Pégourié-Gonnard | f11a6d7 | 2013-07-17 11:17:14 +0200 | [diff] [blame] | 2496 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2497 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 2498 | if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ) |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2499 | { |
| 2500 | *p++ = 0x00; |
| 2501 | *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF; |
| 2502 | *p++ = ssl->verify_data_len * 2 & 0xFF; |
Manuel Pégourié-Gonnard | f11a6d7 | 2013-07-17 11:17:14 +0200 | [diff] [blame] | 2503 | |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2504 | memcpy( p, ssl->peer_verify_data, ssl->verify_data_len ); |
| 2505 | p += ssl->verify_data_len; |
| 2506 | memcpy( p, ssl->own_verify_data, ssl->verify_data_len ); |
| 2507 | p += ssl->verify_data_len; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2508 | } |
| 2509 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2510 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2511 | { |
| 2512 | *p++ = 0x00; |
| 2513 | *p++ = 0x01; |
| 2514 | *p++ = 0x00; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2515 | } |
Manuel Pégourié-Gonnard | 1938975 | 2015-06-23 13:46:44 +0200 | [diff] [blame] | 2516 | |
| 2517 | *olen = p - buf; |
Manuel Pégourié-Gonnard | f11a6d7 | 2013-07-17 11:17:14 +0200 | [diff] [blame] | 2518 | } |
| 2519 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2520 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 2521 | 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] | 2522 | unsigned char *buf, |
| 2523 | size_t *olen ) |
| 2524 | { |
| 2525 | unsigned char *p = buf; |
| 2526 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2527 | 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] | 2528 | { |
Manuel Pégourié-Gonnard | 7bb7899 | 2013-07-17 13:50:08 +0200 | [diff] [blame] | 2529 | *olen = 0; |
| 2530 | return; |
| 2531 | } |
| 2532 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2533 | 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] | 2534 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2535 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF ); |
| 2536 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF ); |
Manuel Pégourié-Gonnard | 7bb7899 | 2013-07-17 13:50:08 +0200 | [diff] [blame] | 2537 | |
| 2538 | *p++ = 0x00; |
| 2539 | *p++ = 1; |
| 2540 | |
Manuel Pégourié-Gonnard | ed4af8b | 2013-07-18 14:07:09 +0200 | [diff] [blame] | 2541 | *p++ = ssl->session_negotiate->mfl_code; |
Manuel Pégourié-Gonnard | 7bb7899 | 2013-07-17 13:50:08 +0200 | [diff] [blame] | 2542 | |
| 2543 | *olen = 5; |
| 2544 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2545 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 7bb7899 | 2013-07-17 13:50:08 +0200 | [diff] [blame] | 2546 | |
Manuel Pégourié-Gonnard | f472179 | 2015-09-15 10:53:51 +0200 | [diff] [blame] | 2547 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 2548 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) || \ |
| 2549 | defined(MBEDTLS_USE_TINYCRYPT) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2550 | 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] | 2551 | unsigned char *buf, |
| 2552 | size_t *olen ) |
| 2553 | { |
| 2554 | unsigned char *p = buf; |
| 2555 | ((void) ssl); |
| 2556 | |
Paul Bakker | 677377f | 2013-10-28 12:54:26 +0100 | [diff] [blame] | 2557 | if( ( ssl->handshake->cli_exts & |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2558 | MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 ) |
Paul Bakker | 677377f | 2013-10-28 12:54:26 +0100 | [diff] [blame] | 2559 | { |
| 2560 | *olen = 0; |
| 2561 | return; |
| 2562 | } |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 2563 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2564 | 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] | 2565 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2566 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF ); |
| 2567 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF ); |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 2568 | |
| 2569 | *p++ = 0x00; |
| 2570 | *p++ = 2; |
| 2571 | |
| 2572 | *p++ = 1; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2573 | *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED; |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 2574 | |
| 2575 | *olen = 6; |
| 2576 | } |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 2577 | #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] | 2578 | |
Manuel Pégourié-Gonnard | 55c7f99 | 2015-09-16 15:35:27 +0200 | [diff] [blame] | 2579 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 2580 | static void ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl, |
| 2581 | unsigned char *buf, |
| 2582 | size_t *olen ) |
| 2583 | { |
| 2584 | int ret; |
| 2585 | unsigned char *p = buf; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2586 | 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] | 2587 | size_t kkpp_len; |
| 2588 | |
| 2589 | *olen = 0; |
| 2590 | |
| 2591 | /* Skip costly computation if not needed */ |
Hanno Becker | df64596 | 2019-06-26 13:02:22 +0100 | [diff] [blame] | 2592 | if( mbedtls_ssl_suite_get_key_exchange( |
| 2593 | mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ) ) != |
Manuel Pégourié-Gonnard | 55c7f99 | 2015-09-16 15:35:27 +0200 | [diff] [blame] | 2594 | MBEDTLS_KEY_EXCHANGE_ECJPAKE ) |
Hanno Becker | df64596 | 2019-06-26 13:02:22 +0100 | [diff] [blame] | 2595 | { |
Manuel Pégourié-Gonnard | 55c7f99 | 2015-09-16 15:35:27 +0200 | [diff] [blame] | 2596 | return; |
Hanno Becker | df64596 | 2019-06-26 13:02:22 +0100 | [diff] [blame] | 2597 | } |
Manuel Pégourié-Gonnard | 55c7f99 | 2015-09-16 15:35:27 +0200 | [diff] [blame] | 2598 | |
| 2599 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, ecjpake kkpp extension" ) ); |
| 2600 | |
| 2601 | if( end - p < 4 ) |
| 2602 | { |
| 2603 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) ); |
| 2604 | return; |
| 2605 | } |
| 2606 | |
| 2607 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP >> 8 ) & 0xFF ); |
| 2608 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP ) & 0xFF ); |
| 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 | |
| 2620 | *p++ = (unsigned char)( ( kkpp_len >> 8 ) & 0xFF ); |
| 2621 | *p++ = (unsigned char)( ( kkpp_len ) & 0xFF ); |
| 2622 | |
| 2623 | *olen = kkpp_len + 4; |
| 2624 | } |
| 2625 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
| 2626 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2627 | #if defined(MBEDTLS_SSL_ALPN ) |
| 2628 | static void ssl_write_alpn_ext( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 2629 | unsigned char *buf, size_t *olen ) |
| 2630 | { |
| 2631 | if( ssl->alpn_chosen == NULL ) |
| 2632 | { |
| 2633 | *olen = 0; |
| 2634 | return; |
| 2635 | } |
| 2636 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2637 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) ); |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 2638 | |
| 2639 | /* |
| 2640 | * 0 . 1 ext identifier |
| 2641 | * 2 . 3 ext length |
| 2642 | * 4 . 5 protocol list length |
| 2643 | * 6 . 6 protocol name length |
| 2644 | * 7 . 7+n protocol name |
| 2645 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2646 | buf[0] = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN >> 8 ) & 0xFF ); |
| 2647 | buf[1] = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN ) & 0xFF ); |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 2648 | |
| 2649 | *olen = 7 + strlen( ssl->alpn_chosen ); |
| 2650 | |
| 2651 | buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF ); |
| 2652 | buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF ); |
| 2653 | |
| 2654 | buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF ); |
| 2655 | buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF ); |
| 2656 | |
| 2657 | buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF ); |
| 2658 | |
| 2659 | memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 ); |
| 2660 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2661 | #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */ |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 2662 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2663 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) |
| 2664 | 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] | 2665 | { |
| 2666 | int ret; |
| 2667 | unsigned char *p = ssl->out_msg + 4; |
Manuel Pégourié-Gonnard | d7f9bc5 | 2014-07-23 11:09:27 +0200 | [diff] [blame] | 2668 | unsigned char *cookie_len_byte; |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2669 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2670 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello verify request" ) ); |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2671 | |
| 2672 | /* |
| 2673 | * struct { |
| 2674 | * ProtocolVersion server_version; |
| 2675 | * opaque cookie<0..2^8-1>; |
| 2676 | * } HelloVerifyRequest; |
| 2677 | */ |
| 2678 | |
Manuel Pégourié-Gonnard | b35fe56 | 2014-08-09 17:00:46 +0200 | [diff] [blame] | 2679 | /* The RFC is not clear on this point, but sending the actual negotiated |
| 2680 | * version looks like the most interoperable thing to do. */ |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 2681 | mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ), |
| 2682 | mbedtls_ssl_get_minor_ver( ssl ), |
| 2683 | ssl->conf->transport, p ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2684 | MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 ); |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2685 | p += 2; |
| 2686 | |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 2687 | /* If we get here, f_cookie_check is not null */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2688 | if( ssl->conf->f_cookie_write == NULL ) |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 2689 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2690 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "inconsistent cookie callbacks" ) ); |
| 2691 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 2692 | } |
| 2693 | |
Manuel Pégourié-Gonnard | d7f9bc5 | 2014-07-23 11:09:27 +0200 | [diff] [blame] | 2694 | /* Skip length byte until we know the length */ |
| 2695 | cookie_len_byte = p++; |
| 2696 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2697 | if( ( ret = ssl->conf->f_cookie_write( ssl->conf->p_cookie, |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2698 | &p, ssl->out_buf + MBEDTLS_SSL_OUT_BUFFER_LEN, |
Manuel Pégourié-Gonnard | d485d19 | 2014-07-23 14:56:15 +0200 | [diff] [blame] | 2699 | ssl->cli_id, ssl->cli_id_len ) ) != 0 ) |
Manuel Pégourié-Gonnard | d7f9bc5 | 2014-07-23 11:09:27 +0200 | [diff] [blame] | 2700 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2701 | MBEDTLS_SSL_DEBUG_RET( 1, "f_cookie_write", ret ); |
Manuel Pégourié-Gonnard | d7f9bc5 | 2014-07-23 11:09:27 +0200 | [diff] [blame] | 2702 | return( ret ); |
| 2703 | } |
| 2704 | |
| 2705 | *cookie_len_byte = (unsigned char)( p - ( cookie_len_byte + 1 ) ); |
| 2706 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2707 | 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] | 2708 | |
| 2709 | ssl->out_msglen = p - ssl->out_msg; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2710 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 2711 | ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST; |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2712 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2713 | ssl->state = MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT; |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2714 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2715 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2716 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2717 | 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] | 2718 | return( ret ); |
| 2719 | } |
| 2720 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2721 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 2722 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2723 | ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 2724 | { |
| 2725 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); |
| 2726 | return( ret ); |
| 2727 | } |
Hanno Becker | bc2498a | 2018-08-28 10:13:29 +0100 | [diff] [blame] | 2728 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2729 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2730 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello verify request" ) ); |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2731 | |
| 2732 | return( 0 ); |
| 2733 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2734 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */ |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2735 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2736 | static int ssl_write_server_hello( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2737 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2738 | #if defined(MBEDTLS_HAVE_TIME) |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 2739 | mbedtls_time_t t; |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 2740 | #endif |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 2741 | int ret; |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 2742 | int ciphersuite; |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 2743 | size_t olen, ext_len = 0, n; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2744 | unsigned char *buf, *p; |
| 2745 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2746 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2747 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2748 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 2749 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Manuel Pégourié-Gonnard | d7f9bc5 | 2014-07-23 11:09:27 +0200 | [diff] [blame] | 2750 | ssl->handshake->verify_cookie_len != 0 ) |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2751 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2752 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "client hello was not authenticated" ) ); |
| 2753 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) ); |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2754 | |
Manuel Pégourié-Gonnard | 579950c | 2014-09-29 17:47:33 +0200 | [diff] [blame] | 2755 | return( ssl_write_hello_verify_request( ssl ) ); |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2756 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2757 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */ |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2758 | |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 2759 | if( mbedtls_ssl_conf_get_frng( ssl->conf ) == NULL ) |
Paul Bakker | a9a028e | 2013-11-21 17:31:06 +0100 | [diff] [blame] | 2760 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2761 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided") ); |
| 2762 | return( MBEDTLS_ERR_SSL_NO_RNG ); |
Paul Bakker | a9a028e | 2013-11-21 17:31:06 +0100 | [diff] [blame] | 2763 | } |
| 2764 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2765 | /* |
| 2766 | * 0 . 0 handshake type |
| 2767 | * 1 . 3 handshake length |
| 2768 | * 4 . 5 protocol version |
| 2769 | * 6 . 9 UNIX time() |
| 2770 | * 10 . 37 random bytes |
| 2771 | */ |
| 2772 | buf = ssl->out_msg; |
| 2773 | p = buf + 4; |
| 2774 | |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 2775 | mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ), |
| 2776 | mbedtls_ssl_get_minor_ver( ssl ), |
| 2777 | ssl->conf->transport, p ); |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 2778 | p += 2; |
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, chosen version: [%d:%d]", |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 2781 | buf[4], buf[5] ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2782 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2783 | #if defined(MBEDTLS_HAVE_TIME) |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 2784 | t = mbedtls_time( NULL ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2785 | *p++ = (unsigned char)( t >> 24 ); |
| 2786 | *p++ = (unsigned char)( t >> 16 ); |
| 2787 | *p++ = (unsigned char)( t >> 8 ); |
| 2788 | *p++ = (unsigned char)( t ); |
| 2789 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2790 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) ); |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 2791 | #else |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 2792 | if( ( ret = mbedtls_ssl_conf_get_frng( ssl->conf ) |
Hanno Becker | 9a12243 | 2019-07-23 13:24:02 +0100 | [diff] [blame] | 2793 | ( mbedtls_ssl_conf_get_prng( ssl->conf ), p, 4 ) ) != 0 ) |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 2794 | { |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 2795 | return( ret ); |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 2796 | } |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 2797 | |
| 2798 | p += 4; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2799 | #endif /* MBEDTLS_HAVE_TIME */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2800 | |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 2801 | if( ( ret = mbedtls_ssl_conf_get_frng( ssl->conf ) |
Hanno Becker | 9a12243 | 2019-07-23 13:24:02 +0100 | [diff] [blame] | 2802 | ( mbedtls_ssl_conf_get_prng( ssl->conf ), p, 28 ) ) != 0 ) |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 2803 | { |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 2804 | return( ret ); |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 2805 | } |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 2806 | |
| 2807 | p += 28; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2808 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2809 | memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2810 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2811 | MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2812 | |
Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 2813 | #if !defined(MBEDTLS_SSL_NO_SESSION_CACHE) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2814 | /* |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2815 | * Resume is 0 by default, see ssl_handshake_init(). |
| 2816 | * It may be already set to 1 by ssl_parse_session_ticket_ext(). |
| 2817 | * If not, try looking up session ID in our cache. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2818 | */ |
Manuel Pégourié-Gonnard | 3652e99 | 2019-07-01 12:09:22 +0200 | [diff] [blame] | 2819 | if( mbedtls_ssl_handshake_get_resume( ssl->handshake ) == 0 && |
Manuel Pégourié-Gonnard | 754b9f3 | 2019-07-01 12:20:54 +0200 | [diff] [blame] | 2820 | mbedtls_ssl_get_renego_status( ssl ) == MBEDTLS_SSL_INITIAL_HANDSHAKE && |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 2821 | ssl->session_negotiate->id_len != 0 && |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2822 | ssl->conf->f_get_cache != NULL && |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 2823 | 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] | 2824 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2825 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) ); |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2826 | ssl->handshake->resume = 1; |
| 2827 | } |
Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 2828 | #endif /* !MBEDTLS_SSL_NO_SESSION_CACHE */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2829 | |
Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 2830 | #if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION) |
Manuel Pégourié-Gonnard | 3652e99 | 2019-07-01 12:09:22 +0200 | [diff] [blame] | 2831 | if( mbedtls_ssl_handshake_get_resume( ssl->handshake ) == 1 ) |
Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 2832 | { |
| 2833 | /* |
| 2834 | * Resuming a session |
| 2835 | */ |
| 2836 | n = ssl->session_negotiate->id_len; |
| 2837 | ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC; |
| 2838 | |
| 2839 | if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 ) |
| 2840 | { |
| 2841 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret ); |
| 2842 | return( ret ); |
| 2843 | } |
| 2844 | } |
| 2845 | else |
| 2846 | #endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2847 | { |
| 2848 | /* |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2849 | * New session, create a new session id, |
| 2850 | * unless we're about to issue a session ticket |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2851 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2852 | ssl->state++; |
| 2853 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2854 | #if defined(MBEDTLS_HAVE_TIME) |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 2855 | ssl->session_negotiate->start = mbedtls_time( NULL ); |
Manuel Pégourié-Gonnard | 164d894 | 2013-09-23 22:01:39 +0200 | [diff] [blame] | 2856 | #endif |
| 2857 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2858 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | 164d894 | 2013-09-23 22:01:39 +0200 | [diff] [blame] | 2859 | if( ssl->handshake->new_session_ticket != 0 ) |
| 2860 | { |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 2861 | ssl->session_negotiate->id_len = n = 0; |
Manuel Pégourié-Gonnard | 164d894 | 2013-09-23 22:01:39 +0200 | [diff] [blame] | 2862 | memset( ssl->session_negotiate->id, 0, 32 ); |
| 2863 | } |
| 2864 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2865 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2866 | { |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 2867 | ssl->session_negotiate->id_len = n = 32; |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 2868 | if( ( ret = mbedtls_ssl_conf_get_frng( ssl->conf ) |
Hanno Becker | 9a12243 | 2019-07-23 13:24:02 +0100 | [diff] [blame] | 2869 | ( 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] | 2870 | { |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2871 | return( ret ); |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 2872 | } |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2873 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2874 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2875 | |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2876 | /* |
| 2877 | * 38 . 38 session id length |
| 2878 | * 39 . 38+n session id |
| 2879 | * 39+n . 40+n chosen ciphersuite |
| 2880 | * 41+n . 41+n chosen compression alg. |
| 2881 | * 42+n . 43+n extensions length |
| 2882 | * 44+n . 43+n+m extensions |
| 2883 | */ |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 2884 | *p++ = (unsigned char) ssl->session_negotiate->id_len; |
| 2885 | memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len ); |
| 2886 | p += ssl->session_negotiate->id_len; |
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, session id len.: %d", n ) ); |
| 2889 | MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n ); |
| 2890 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed", |
Manuel Pégourié-Gonnard | 3652e99 | 2019-07-01 12:09:22 +0200 | [diff] [blame] | 2891 | mbedtls_ssl_handshake_get_resume( ssl->handshake ) ? "a" : "no" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2892 | |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 2893 | ciphersuite = mbedtls_ssl_session_get_ciphersuite( ssl->session_negotiate ); |
| 2894 | *p++ = (unsigned char)( ciphersuite >> 8 ); |
| 2895 | *p++ = (unsigned char)( ciphersuite ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2896 | *p++ = (unsigned char)( ssl->session_negotiate->compression ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2897 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2898 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s", |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 2899 | mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2900 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X", |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2901 | ssl->session_negotiate->compression ) ); |
| 2902 | |
Janos Follath | c6dab2b | 2016-05-23 14:27:02 +0100 | [diff] [blame] | 2903 | /* Do not write the extensions if the protocol is SSLv3 */ |
| 2904 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 2905 | 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] | 2906 | { |
| 2907 | #endif |
| 2908 | |
Manuel Pégourié-Gonnard | f11a6d7 | 2013-07-17 11:17:14 +0200 | [diff] [blame] | 2909 | /* |
| 2910 | * First write extensions, then the total length |
| 2911 | */ |
| 2912 | ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen ); |
| 2913 | ext_len += olen; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2914 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2915 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 7bb7899 | 2013-07-17 13:50:08 +0200 | [diff] [blame] | 2916 | ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen ); |
| 2917 | ext_len += olen; |
Paul Bakker | 05decb2 | 2013-08-15 13:33:48 +0200 | [diff] [blame] | 2918 | #endif |
Manuel Pégourié-Gonnard | 7bb7899 | 2013-07-17 13:50:08 +0200 | [diff] [blame] | 2919 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2920 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 2921 | ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen ); |
| 2922 | ext_len += olen; |
Paul Bakker | 1f2bc62 | 2013-08-15 13:45:55 +0200 | [diff] [blame] | 2923 | #endif |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 2924 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2925 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 072d4ec | 2019-04-26 15:46:55 +0100 | [diff] [blame] | 2926 | ssl_write_cid_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_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2931 | ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen ); |
| 2932 | ext_len += olen; |
| 2933 | #endif |
| 2934 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2935 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2936 | ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen ); |
| 2937 | ext_len += olen; |
| 2938 | #endif |
| 2939 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2940 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 2941 | ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen ); |
| 2942 | ext_len += olen; |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 2943 | #endif |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 2944 | |
Manuel Pégourié-Gonnard | f472179 | 2015-09-15 10:53:51 +0200 | [diff] [blame] | 2945 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 2946 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) || \ |
| 2947 | defined(MBEDTLS_USE_TINYCRYPT) |
Ron Eldor | 755bb6a | 2018-02-14 19:30:48 +0200 | [diff] [blame] | 2948 | if ( mbedtls_ssl_ciphersuite_uses_ec( |
Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 2949 | mbedtls_ssl_ciphersuite_from_id( |
| 2950 | mbedtls_ssl_session_get_ciphersuite( ssl->session_negotiate ) ) ) ) |
Ron Eldor | 755bb6a | 2018-02-14 19:30:48 +0200 | [diff] [blame] | 2951 | { |
| 2952 | ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen ); |
| 2953 | ext_len += olen; |
| 2954 | } |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 2955 | #endif |
| 2956 | |
Manuel Pégourié-Gonnard | 55c7f99 | 2015-09-16 15:35:27 +0200 | [diff] [blame] | 2957 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 2958 | ssl_write_ecjpake_kkpp_ext( ssl, p + 2 + ext_len, &olen ); |
| 2959 | ext_len += olen; |
| 2960 | #endif |
| 2961 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2962 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 2963 | ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen ); |
| 2964 | ext_len += olen; |
| 2965 | #endif |
| 2966 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2967 | 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] | 2968 | |
Paul Bakker | a703663 | 2014-04-30 10:15:38 +0200 | [diff] [blame] | 2969 | if( ext_len > 0 ) |
| 2970 | { |
| 2971 | *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF ); |
| 2972 | *p++ = (unsigned char)( ( ext_len ) & 0xFF ); |
| 2973 | p += ext_len; |
| 2974 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2975 | |
Janos Follath | c6dab2b | 2016-05-23 14:27:02 +0100 | [diff] [blame] | 2976 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 2977 | } |
| 2978 | #endif |
| 2979 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2980 | ssl->out_msglen = p - buf; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2981 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 2982 | ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2983 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2984 | ret = mbedtls_ssl_write_handshake_msg( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2985 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2986 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2987 | |
| 2988 | return( ret ); |
| 2989 | } |
| 2990 | |
Hanno Becker | ae39b9e | 2019-02-07 12:32:43 +0000 | [diff] [blame] | 2991 | #if !defined(MBEDTLS_KEY_EXCHANGE__CERT_REQ_ALLOWED__ENABLED) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2992 | static int ssl_write_certificate_request( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2993 | { |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 2994 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = |
Hanno Becker | df64596 | 2019-06-26 13:02:22 +0100 | [diff] [blame] | 2995 | mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2996 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2997 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2998 | |
Hanno Becker | ae39b9e | 2019-02-07 12:32:43 +0000 | [diff] [blame] | 2999 | if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ) |
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 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3002 | ssl->state++; |
| 3003 | return( 0 ); |
| 3004 | } |
| 3005 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3006 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3007 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3008 | } |
Hanno Becker | ae39b9e | 2019-02-07 12:32:43 +0000 | [diff] [blame] | 3009 | #else /* !MBEDTLS_KEY_EXCHANGE__CERT_REQ_ALLOWED__ENABLED */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3010 | static int ssl_write_certificate_request( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3011 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3012 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 3013 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = |
Hanno Becker | df64596 | 2019-06-26 13:02:22 +0100 | [diff] [blame] | 3014 | mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3015 | size_t dn_size, total_dn_size; /* excluding length bytes */ |
| 3016 | size_t ct_len, sa_len; /* including length bytes */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3017 | unsigned char *buf, *p; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3018 | 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] | 3019 | const mbedtls_x509_crt *crt; |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 3020 | int authmode; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3021 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3022 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3023 | |
| 3024 | ssl->state++; |
| 3025 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 3026 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 3027 | if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET ) |
| 3028 | authmode = ssl->handshake->sni_authmode; |
| 3029 | else |
| 3030 | #endif |
Hanno Becker | acd4fc0 | 2019-06-12 16:40:50 +0100 | [diff] [blame] | 3031 | authmode = mbedtls_ssl_conf_get_authmode( ssl->conf ); |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 3032 | |
Hanno Becker | ae39b9e | 2019-02-07 12:32:43 +0000 | [diff] [blame] | 3033 | if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) || |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 3034 | authmode == MBEDTLS_SSL_VERIFY_NONE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3035 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3036 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3037 | return( 0 ); |
| 3038 | } |
| 3039 | |
| 3040 | /* |
| 3041 | * 0 . 0 handshake type |
| 3042 | * 1 . 3 handshake length |
| 3043 | * 4 . 4 cert type count |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3044 | * 5 .. m-1 cert types |
| 3045 | * m .. m+1 sig alg length (TLS 1.2 only) |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 3046 | * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3047 | * n .. n+1 length of all DNs |
| 3048 | * n+2 .. n+3 length of DN 1 |
| 3049 | * n+4 .. ... Distinguished Name #1 |
| 3050 | * ... .. ... length of DN 2, etc. |
| 3051 | */ |
| 3052 | buf = ssl->out_msg; |
| 3053 | p = buf + 4; |
| 3054 | |
| 3055 | /* |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3056 | * Supported certificate types |
| 3057 | * |
| 3058 | * ClientCertificateType certificate_types<1..2^8-1>; |
| 3059 | * enum { (255) } ClientCertificateType; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3060 | */ |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3061 | ct_len = 0; |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3062 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3063 | #if defined(MBEDTLS_RSA_C) |
| 3064 | p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_RSA_SIGN; |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3065 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3066 | #if defined(MBEDTLS_ECDSA_C) |
| 3067 | p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_ECDSA_SIGN; |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3068 | #endif |
| 3069 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 3070 | p[0] = (unsigned char) ct_len++; |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3071 | p += ct_len; |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3072 | |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 3073 | sa_len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3074 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3075 | /* |
| 3076 | * Add signature_algorithms for verify (TLS 1.2) |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3077 | * |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3078 | * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>; |
| 3079 | * |
| 3080 | * struct { |
| 3081 | * HashAlgorithm hash; |
| 3082 | * SignatureAlgorithm signature; |
| 3083 | * } SignatureAndHashAlgorithm; |
| 3084 | * |
| 3085 | * enum { (255) } HashAlgorithm; |
| 3086 | * enum { (255) } SignatureAlgorithm; |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3087 | */ |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 3088 | if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3089 | { |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3090 | /* |
| 3091 | * Supported signature algorithms |
| 3092 | */ |
Hanno Becker | f1bc9e1 | 2019-06-19 16:23:21 +0100 | [diff] [blame] | 3093 | MBEDTLS_SSL_BEGIN_FOR_EACH_SIG_HASH_TLS( hash ) |
| 3094 | if( 0 |
Hanno Becker | 0af717b | 2019-06-24 11:36:30 +0100 | [diff] [blame] | 3095 | #if defined(MBEDTLS_SHA512_C) |
Hanno Becker | f1bc9e1 | 2019-06-19 16:23:21 +0100 | [diff] [blame] | 3096 | || hash == MBEDTLS_SSL_HASH_SHA384 |
Hanno Becker | 0af717b | 2019-06-24 11:36:30 +0100 | [diff] [blame] | 3097 | #endif |
| 3098 | #if defined(MBEDTLS_SHA256_C) |
Hanno Becker | f1bc9e1 | 2019-06-19 16:23:21 +0100 | [diff] [blame] | 3099 | || hash == MBEDTLS_SSL_HASH_SHA256 |
Hanno Becker | 0af717b | 2019-06-24 11:36:30 +0100 | [diff] [blame] | 3100 | #endif |
Hanno Becker | f1bc9e1 | 2019-06-19 16:23:21 +0100 | [diff] [blame] | 3101 | ) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3102 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3103 | #if defined(MBEDTLS_RSA_C) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3104 | p[2 + sa_len++] = hash; |
| 3105 | p[2 + sa_len++] = MBEDTLS_SSL_SIG_RSA; |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3106 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3107 | #if defined(MBEDTLS_ECDSA_C) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3108 | p[2 + sa_len++] = hash; |
| 3109 | p[2 + sa_len++] = MBEDTLS_SSL_SIG_ECDSA; |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3110 | #endif |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3111 | } |
Hanno Becker | f1bc9e1 | 2019-06-19 16:23:21 +0100 | [diff] [blame] | 3112 | MBEDTLS_SSL_END_FOR_EACH_SIG_HASH_TLS |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3113 | |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3114 | p[0] = (unsigned char)( sa_len >> 8 ); |
| 3115 | p[1] = (unsigned char)( sa_len ); |
| 3116 | sa_len += 2; |
| 3117 | p += sa_len; |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3118 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3119 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3120 | |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3121 | /* |
| 3122 | * DistinguishedName certificate_authorities<0..2^16-1>; |
| 3123 | * opaque DistinguishedName<1..2^16-1>; |
| 3124 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3125 | p += 2; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3126 | |
Paul Bakker | bc3d984 | 2012-11-26 16:12:02 +0100 | [diff] [blame] | 3127 | total_dn_size = 0; |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 3128 | |
Hanno Becker | c2cfdaa | 2019-06-13 12:33:03 +0100 | [diff] [blame] | 3129 | if( mbedtls_ssl_conf_get_cert_req_ca_list( ssl->conf ) |
| 3130 | == MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3131 | { |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 3132 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 3133 | if( ssl->handshake->sni_ca_chain != NULL ) |
| 3134 | crt = ssl->handshake->sni_ca_chain; |
| 3135 | else |
| 3136 | #endif |
| 3137 | crt = ssl->conf->ca_chain; |
Manuel Pégourié-Gonnard | bc1babb | 2015-10-02 11:16:47 +0200 | [diff] [blame] | 3138 | |
Hanno Becker | 371e0e4 | 2019-02-25 18:08:59 +0000 | [diff] [blame] | 3139 | while( crt != NULL && crt->raw.p != NULL ) |
Manuel Pégourié-Gonnard | bc1babb | 2015-10-02 11:16:47 +0200 | [diff] [blame] | 3140 | { |
Hanno Becker | 5f268b3 | 2019-05-20 16:26:34 +0100 | [diff] [blame] | 3141 | mbedtls_x509_crt_frame const *frame; |
Hanno Becker | 232f8fa | 2019-02-26 16:49:57 +0000 | [diff] [blame] | 3142 | ret = mbedtls_x509_crt_frame_acquire( crt, &frame ); |
| 3143 | if( ret != 0 ) |
Hanno Becker | 2224ccf | 2019-06-28 10:52:45 +0100 | [diff] [blame] | 3144 | { |
| 3145 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_frame_acquire", ret ); |
Hanno Becker | 232f8fa | 2019-02-26 16:49:57 +0000 | [diff] [blame] | 3146 | return( ret ); |
Hanno Becker | 2224ccf | 2019-06-28 10:52:45 +0100 | [diff] [blame] | 3147 | } |
Hanno Becker | 232f8fa | 2019-02-26 16:49:57 +0000 | [diff] [blame] | 3148 | |
Hanno Becker | 1e11f21 | 2019-03-04 14:43:43 +0000 | [diff] [blame] | 3149 | dn_size = frame->subject_raw.len; |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 3150 | |
| 3151 | if( end < p || |
| 3152 | (size_t)( end - p ) < dn_size || |
| 3153 | (size_t)( end - p ) < 2 + dn_size ) |
| 3154 | { |
| 3155 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "skipping CAs: buffer too short" ) ); |
Hanno Becker | c6d1c3e | 2019-03-05 13:50:56 +0000 | [diff] [blame] | 3156 | mbedtls_x509_crt_frame_release( crt ); |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 3157 | break; |
| 3158 | } |
| 3159 | |
| 3160 | *p++ = (unsigned char)( dn_size >> 8 ); |
| 3161 | *p++ = (unsigned char)( dn_size ); |
Hanno Becker | 1e11f21 | 2019-03-04 14:43:43 +0000 | [diff] [blame] | 3162 | memcpy( p, frame->subject_raw.p, dn_size ); |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 3163 | p += dn_size; |
| 3164 | |
| 3165 | MBEDTLS_SSL_DEBUG_BUF( 3, "requested DN", p - dn_size, dn_size ); |
| 3166 | |
| 3167 | total_dn_size += 2 + dn_size; |
Hanno Becker | 232f8fa | 2019-02-26 16:49:57 +0000 | [diff] [blame] | 3168 | |
Hanno Becker | c6d1c3e | 2019-03-05 13:50:56 +0000 | [diff] [blame] | 3169 | mbedtls_x509_crt_frame_release( crt ); |
Hanno Becker | 232f8fa | 2019-02-26 16:49:57 +0000 | [diff] [blame] | 3170 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 3171 | crt = crt->next; |
Manuel Pégourié-Gonnard | bc1babb | 2015-10-02 11:16:47 +0200 | [diff] [blame] | 3172 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3173 | } |
| 3174 | |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3175 | ssl->out_msglen = p - buf; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3176 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 3177 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_REQUEST; |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3178 | ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 ); |
| 3179 | ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3180 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3181 | ret = mbedtls_ssl_write_handshake_msg( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3182 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3183 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3184 | |
| 3185 | return( ret ); |
| 3186 | } |
Hanno Becker | ae39b9e | 2019-02-07 12:32:43 +0000 | [diff] [blame] | 3187 | #endif /* MBEDTLS_KEY_EXCHANGE__CERT_REQ_ALLOWED__ENABLED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3188 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3189 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ |
| 3190 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) |
| 3191 | 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] | 3192 | { |
| 3193 | int ret; |
| 3194 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3195 | 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] | 3196 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3197 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) ); |
| 3198 | return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH ); |
Manuel Pégourié-Gonnard | 5538970 | 2013-12-12 11:14:16 +0100 | [diff] [blame] | 3199 | } |
| 3200 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3201 | if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx, |
| 3202 | mbedtls_pk_ec( *mbedtls_ssl_own_key( ssl ) ), |
| 3203 | MBEDTLS_ECDH_OURS ) ) != 0 ) |
Manuel Pégourié-Gonnard | 5538970 | 2013-12-12 11:14:16 +0100 | [diff] [blame] | 3204 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3205 | MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret ); |
Manuel Pégourié-Gonnard | 5538970 | 2013-12-12 11:14:16 +0100 | [diff] [blame] | 3206 | return( ret ); |
| 3207 | } |
| 3208 | |
| 3209 | return( 0 ); |
| 3210 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3211 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || |
| 3212 | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */ |
Manuel Pégourié-Gonnard | 5538970 | 2013-12-12 11:14:16 +0100 | [diff] [blame] | 3213 | |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3214 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) && \ |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3215 | defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3216 | static int ssl_resume_server_key_exchange( mbedtls_ssl_context *ssl, |
Gilles Peskine | 0fd90dd | 2018-04-26 07:41:09 +0200 | [diff] [blame] | 3217 | size_t *signature_len ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3218 | { |
Gilles Peskine | 0fd90dd | 2018-04-26 07:41:09 +0200 | [diff] [blame] | 3219 | /* Append the signature to ssl->out_msg, leaving 2 bytes for the |
| 3220 | * signature length which will be added in ssl_write_server_key_exchange |
| 3221 | * after the call to ssl_prepare_server_key_exchange. |
| 3222 | * ssl_write_server_key_exchange also takes care of incrementing |
| 3223 | * ssl->out_msglen. */ |
| 3224 | unsigned char *sig_start = ssl->out_msg + ssl->out_msglen + 2; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3225 | 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] | 3226 | - sig_start ); |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 3227 | int ret = ssl->conf->f_async_resume( ssl, |
Gilles Peskine | 0fd90dd | 2018-04-26 07:41:09 +0200 | [diff] [blame] | 3228 | sig_start, signature_len, sig_max_len ); |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3229 | if( ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS ) |
| 3230 | { |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3231 | ssl->handshake->async_in_progress = 0; |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 3232 | mbedtls_ssl_set_async_operation_data( ssl, NULL ); |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3233 | } |
Gilles Peskine | d3eb061 | 2018-01-08 17:07:44 +0100 | [diff] [blame] | 3234 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl_resume_server_key_exchange", ret ); |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3235 | return( ret ); |
| 3236 | } |
| 3237 | #endif /* defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) && |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3238 | defined(MBEDTLS_SSL_ASYNC_PRIVATE) */ |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3239 | |
Gilles Peskine | d3eb061 | 2018-01-08 17:07:44 +0100 | [diff] [blame] | 3240 | /* Prepare the ServerKeyExchange message, up to and including |
Gilles Peskine | 168dae8 | 2018-04-25 23:35:42 +0200 | [diff] [blame] | 3241 | * calculating the signature if any, but excluding formatting the |
| 3242 | * signature and sending the message. */ |
Gilles Peskine | 7ab013a | 2018-01-08 17:04:16 +0100 | [diff] [blame] | 3243 | static int ssl_prepare_server_key_exchange( mbedtls_ssl_context *ssl, |
| 3244 | size_t *signature_len ) |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 3245 | { |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 3246 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = |
Hanno Becker | df64596 | 2019-06-26 13:02:22 +0100 | [diff] [blame] | 3247 | mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 3248 | |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3249 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME_PFS__ENABLED) |
Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3250 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) |
Gilles Peskine | 3ce9b90 | 2018-01-06 01:34:21 +0100 | [diff] [blame] | 3251 | unsigned char *dig_signed = NULL; |
Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3252 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED */ |
| 3253 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME_PFS__ENABLED */ |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 3254 | #if defined(MBEDTLS_USE_TINYCRYPT) |
| 3255 | const struct uECC_Curve_t * uecc_curve = uECC_secp256r1(); |
| 3256 | #endif |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3257 | |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3258 | (void) ciphersuite_info; /* unused in some configurations */ |
Gilles Peskine | 22e695f | 2018-04-26 00:22:50 +0200 | [diff] [blame] | 3259 | #if !defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) |
| 3260 | (void) signature_len; |
| 3261 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED */ |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3262 | |
Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 3263 | 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] | 3264 | |
Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3265 | /* |
| 3266 | * |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3267 | * Part 1: Provide key exchange parameters for chosen ciphersuite. |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3268 | * |
| 3269 | */ |
| 3270 | |
| 3271 | /* |
| 3272 | * - ECJPAKE key exchanges |
| 3273 | */ |
Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 3274 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 3275 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 3276 | == MBEDTLS_KEY_EXCHANGE_ECJPAKE ) |
Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 3277 | { |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3278 | int ret; |
Simon Butcher | 600c5e6 | 2018-06-14 08:58:59 +0100 | [diff] [blame] | 3279 | size_t len = 0; |
Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 3280 | |
Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 3281 | ret = mbedtls_ecjpake_write_round_two( |
| 3282 | &ssl->handshake->ecjpake_ctx, |
| 3283 | ssl->out_msg + ssl->out_msglen, |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3284 | MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen, &len, |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 3285 | mbedtls_ssl_conf_get_frng( ssl->conf ), |
Hanno Becker | 9a12243 | 2019-07-23 13:24:02 +0100 | [diff] [blame] | 3286 | mbedtls_ssl_conf_get_prng( ssl->conf ) ); |
Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 3287 | if( ret != 0 ) |
| 3288 | { |
| 3289 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret ); |
| 3290 | return( ret ); |
| 3291 | } |
| 3292 | |
Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 3293 | ssl->out_msglen += len; |
Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 3294 | } |
| 3295 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
| 3296 | |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3297 | /* |
| 3298 | * For (EC)DHE key exchanges with PSK, parameters are prefixed by support |
| 3299 | * identity hint (RFC 4279, Sec. 3). Until someone needs this feature, |
| 3300 | * we use empty support identity hints here. |
| 3301 | **/ |
| 3302 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) || \ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3303 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 3304 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) == |
| 3305 | MBEDTLS_KEY_EXCHANGE_DHE_PSK || |
| 3306 | mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) == |
| 3307 | MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3308 | { |
Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 3309 | ssl->out_msg[ssl->out_msglen++] = 0x00; |
| 3310 | ssl->out_msg[ssl->out_msglen++] = 0x00; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3311 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3312 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED || |
| 3313 | MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3314 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 3315 | /* |
Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3316 | * - DHE key exchanges |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3317 | */ |
| 3318 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__DHE_ENABLED) |
| 3319 | if( mbedtls_ssl_ciphersuite_uses_dhe( ciphersuite_info ) ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3320 | { |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3321 | int ret; |
Simon Butcher | 600c5e6 | 2018-06-14 08:58:59 +0100 | [diff] [blame] | 3322 | size_t len = 0; |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3323 | |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 3324 | if( ssl->conf->dhm_P.p == NULL || ssl->conf->dhm_G.p == NULL ) |
| 3325 | { |
| 3326 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no DH parameters set" ) ); |
| 3327 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3328 | } |
| 3329 | |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3330 | /* |
| 3331 | * Ephemeral DH parameters: |
| 3332 | * |
| 3333 | * struct { |
| 3334 | * opaque dh_p<1..2^16-1>; |
| 3335 | * opaque dh_g<1..2^16-1>; |
| 3336 | * opaque dh_Ys<1..2^16-1>; |
| 3337 | * } ServerDHParams; |
| 3338 | */ |
Hanno Becker | ab74056 | 2017-10-04 13:15:37 +0100 | [diff] [blame] | 3339 | if( ( ret = mbedtls_dhm_set_group( &ssl->handshake->dhm_ctx, |
| 3340 | &ssl->conf->dhm_P, |
| 3341 | &ssl->conf->dhm_G ) ) != 0 ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3342 | { |
Hanno Becker | ab74056 | 2017-10-04 13:15:37 +0100 | [diff] [blame] | 3343 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_set_group", ret ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3344 | return( ret ); |
| 3345 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3346 | |
Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 3347 | if( ( ret = mbedtls_dhm_make_params( |
| 3348 | &ssl->handshake->dhm_ctx, |
| 3349 | (int) mbedtls_mpi_size( &ssl->handshake->dhm_ctx.P ), |
| 3350 | ssl->out_msg + ssl->out_msglen, &len, |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 3351 | mbedtls_ssl_conf_get_frng( ssl->conf ), |
Hanno Becker | 9a12243 | 2019-07-23 13:24:02 +0100 | [diff] [blame] | 3352 | mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3353 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3354 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_params", ret ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3355 | return( ret ); |
| 3356 | } |
| 3357 | |
Hanno Becker | 4cb1f4d | 2017-10-10 15:59:57 +0100 | [diff] [blame] | 3358 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) |
Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 3359 | dig_signed = ssl->out_msg + ssl->out_msglen; |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3360 | #endif |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3361 | |
Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 3362 | ssl->out_msglen += len; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3363 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3364 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X ); |
| 3365 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P ); |
| 3366 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G ); |
| 3367 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3368 | } |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3369 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__DHE_ENABLED */ |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3370 | |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3371 | /* |
Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3372 | * - ECDHE key exchanges |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3373 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3374 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__ECDHE_ENABLED) |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3375 | if( mbedtls_ssl_ciphersuite_uses_ecdhe( ciphersuite_info ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3376 | { |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3377 | /* |
| 3378 | * Ephemeral ECDH parameters: |
| 3379 | * |
| 3380 | * struct { |
| 3381 | * ECParameters curve_params; |
| 3382 | * ECPoint public; |
| 3383 | * } ServerECDHParams; |
| 3384 | */ |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 3385 | |
| 3386 | #if defined(MBEDTLS_USE_TINYCRYPT) |
Hanno Becker | 9cf087d | 2019-07-24 11:19:03 +0100 | [diff] [blame] | 3387 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 3388 | == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA || |
| 3389 | mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 3390 | == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ) |
Hanno Becker | d849c7c | 2019-07-23 15:59:58 +0100 | [diff] [blame] | 3391 | { |
Hanno Becker | 9cf087d | 2019-07-24 11:19:03 +0100 | [diff] [blame] | 3392 | static const unsigned char ecdh_param_hdr[] = { |
| 3393 | MBEDTLS_ECP_TLS_NAMED_CURVE, |
Hanno Becker | b72fc6a | 2019-07-24 15:23:37 +0100 | [diff] [blame^] | 3394 | 0 /* high bits of secp256r1 TLS ID */, |
| 3395 | 23 /* low bits of secp256r1 TLS ID */, |
Hanno Becker | 9cf087d | 2019-07-24 11:19:03 +0100 | [diff] [blame] | 3396 | 2 * NUM_ECC_BYTES + 1, |
| 3397 | 0x04 /* Uncompressed */ |
| 3398 | }; |
| 3399 | |
Hanno Becker | 9cf087d | 2019-07-24 11:19:03 +0100 | [diff] [blame] | 3400 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) |
| 3401 | dig_signed = ssl->out_msg + ssl->out_msglen; |
| 3402 | #endif |
| 3403 | |
| 3404 | memcpy( ssl->out_msg + ssl->out_msglen, |
| 3405 | ecdh_param_hdr, sizeof( ecdh_param_hdr ) ); |
| 3406 | ssl->out_msglen += sizeof( ecdh_param_hdr ); |
| 3407 | |
Hanno Becker | 8295ff0 | 2019-07-24 12:45:52 +0100 | [diff] [blame] | 3408 | if( !uECC_make_key( &ssl->out_msg[ ssl->out_msglen ], |
| 3409 | ssl->handshake->ecdh_privkey, |
| 3410 | uecc_curve ) ) |
| 3411 | { |
| 3412 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Key creation failed" ) ); |
| 3413 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3414 | } |
| 3415 | |
Hanno Becker | 9cf087d | 2019-07-24 11:19:03 +0100 | [diff] [blame] | 3416 | ssl->out_msglen += 2*NUM_ECC_BYTES; |
Hanno Becker | d849c7c | 2019-07-23 15:59:58 +0100 | [diff] [blame] | 3417 | } |
Hanno Becker | 9cf087d | 2019-07-24 11:19:03 +0100 | [diff] [blame] | 3418 | else |
| 3419 | #endif /* MBEDTLS_ECDH_C */ |
| 3420 | #if !defined(MBEDTLS_ECDH_C) |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 3421 | { |
Hanno Becker | 9cf087d | 2019-07-24 11:19:03 +0100 | [diff] [blame] | 3422 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 3423 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3424 | } |
Hanno Becker | 9cf087d | 2019-07-24 11:19:03 +0100 | [diff] [blame] | 3425 | #else |
| 3426 | { |
| 3427 | const mbedtls_ecp_curve_info *curve = |
| 3428 | mbedtls_ecp_curve_info_from_tls_id( ssl->handshake->curve_tls_id ); |
| 3429 | int ret; |
| 3430 | size_t len = 0; |
| 3431 | |
| 3432 | if( curve == NULL ) |
| 3433 | { |
| 3434 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) ); |
| 3435 | return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN ); |
| 3436 | } |
| 3437 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", curve->name ) ); |
| 3438 | |
| 3439 | if( ( ret = mbedtls_ecdh_setup( &ssl->handshake->ecdh_ctx, |
| 3440 | curve->grp_id ) ) != 0 ) |
| 3441 | { |
| 3442 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecp_group_load", ret ); |
| 3443 | return( ret ); |
| 3444 | } |
| 3445 | |
| 3446 | if( ( ret = mbedtls_ecdh_make_params( |
| 3447 | &ssl->handshake->ecdh_ctx, &len, |
| 3448 | ssl->out_msg + ssl->out_msglen, |
| 3449 | MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen, |
| 3450 | mbedtls_ssl_conf_get_frng( ssl->conf ), |
| 3451 | mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 ) |
| 3452 | { |
| 3453 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_params", ret ); |
| 3454 | return( ret ); |
| 3455 | } |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 3456 | |
Hanno Becker | d849c7c | 2019-07-23 15:59:58 +0100 | [diff] [blame] | 3457 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) |
Hanno Becker | 9cf087d | 2019-07-24 11:19:03 +0100 | [diff] [blame] | 3458 | dig_signed = ssl->out_msg + ssl->out_msglen; |
Hanno Becker | d849c7c | 2019-07-23 15:59:58 +0100 | [diff] [blame] | 3459 | #endif |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 3460 | |
Hanno Becker | 9cf087d | 2019-07-24 11:19:03 +0100 | [diff] [blame] | 3461 | ssl->out_msglen += len; |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 3462 | |
Hanno Becker | 9cf087d | 2019-07-24 11:19:03 +0100 | [diff] [blame] | 3463 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 3464 | MBEDTLS_DEBUG_ECDH_Q ); |
Gergely Budai | 987bfb5 | 2014-01-19 21:48:42 +0100 | [diff] [blame] | 3465 | } |
Hanno Becker | 9cf087d | 2019-07-24 11:19:03 +0100 | [diff] [blame] | 3466 | #endif /* MBEDTLS_ECDH_C */ |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3467 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3468 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__ECDHE_ENABLED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3469 | |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3470 | /* |
Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3471 | * |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3472 | * Part 2: For key exchanges involving the server signing the |
Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3473 | * exchange parameters, compute and add the signature here. |
| 3474 | * |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3475 | */ |
| 3476 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) |
| 3477 | if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3478 | { |
Gilles Peskine | 1004c19 | 2018-01-08 16:59:14 +0100 | [diff] [blame] | 3479 | 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] | 3480 | size_t hashlen = 0; |
Gilles Peskine | e1efdf9 | 2018-01-05 21:18:37 +0100 | [diff] [blame] | 3481 | unsigned char hash[MBEDTLS_MD_MAX_SIZE]; |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3482 | int ret; |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 3483 | |
Manuel Pégourié-Gonnard | abae74c | 2013-08-20 13:53:44 +0200 | [diff] [blame] | 3484 | /* |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3485 | * 2.1: Choose hash algorithm: |
Hanno Becker | 4cb1f4d | 2017-10-10 15:59:57 +0100 | [diff] [blame] | 3486 | * A: For TLS 1.2, obey signature-hash-algorithm extension |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3487 | * to choose appropriate hash. |
| 3488 | * B: For SSL3, TLS1.0, TLS1.1 and ECDHE_ECDSA, use SHA1 |
| 3489 | * (RFC 4492, Sec. 5.4) |
| 3490 | * 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] | 3491 | */ |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 3492 | |
| 3493 | mbedtls_md_type_t md_alg; |
| 3494 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3495 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3496 | mbedtls_pk_type_t sig_alg = |
| 3497 | mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ); |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 3498 | 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] | 3499 | { |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3500 | /* A: For TLS 1.2, obey signature-hash-algorithm extension |
| 3501 | * (RFC 5246, Sec. 7.4.1.4.1). */ |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 3502 | if( sig_alg == MBEDTLS_PK_NONE || |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3503 | ( md_alg = mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs, |
| 3504 | sig_alg ) ) == MBEDTLS_MD_NONE ) |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3505 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3506 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Hanno Becker | 4cb1f4d | 2017-10-10 15:59:57 +0100 | [diff] [blame] | 3507 | /* (... because we choose a cipher suite |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3508 | * only if there is a matching hash.) */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3509 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3510 | } |
| 3511 | } |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 3512 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3513 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3514 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 3515 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 3516 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 3517 | == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ) |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3518 | { |
Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3519 | /* B: Default hash SHA1 */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3520 | md_alg = MBEDTLS_MD_SHA1; |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3521 | } |
| 3522 | else |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3523 | #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \ |
| 3524 | MBEDTLS_SSL_PROTO_TLS1_1 */ |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3525 | { |
Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3526 | /* C: MD5 + SHA1 */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3527 | md_alg = MBEDTLS_MD_NONE; |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3528 | } |
| 3529 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 3530 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "pick hash algorithm %d for signing", md_alg ) ); |
| 3531 | |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3532 | /* |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3533 | * 2.2: Compute the hash to be signed |
Manuel Pégourié-Gonnard | abae74c | 2013-08-20 13:53:44 +0200 | [diff] [blame] | 3534 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3535 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 3536 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 3537 | if( md_alg == MBEDTLS_MD_NONE ) |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 3538 | { |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3539 | hashlen = 36; |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 3540 | ret = mbedtls_ssl_get_key_exchange_md_ssl_tls( ssl, hash, |
| 3541 | dig_signed, |
| 3542 | dig_signed_len ); |
| 3543 | if( ret != 0 ) |
| 3544 | return( ret ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3545 | } |
| 3546 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3547 | #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \ |
| 3548 | MBEDTLS_SSL_PROTO_TLS1_1 */ |
| 3549 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 3550 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3551 | if( md_alg != MBEDTLS_MD_NONE ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3552 | { |
Gilles Peskine | ca1d742 | 2018-04-24 11:53:22 +0200 | [diff] [blame] | 3553 | 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] | 3554 | dig_signed, |
| 3555 | dig_signed_len, |
| 3556 | md_alg ); |
| 3557 | if( ret != 0 ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3558 | return( ret ); |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 3559 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 3560 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3561 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 3562 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 3563 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3564 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3565 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 3566 | } |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 3567 | |
Gilles Peskine | ebd652f | 2018-01-05 21:18:59 +0100 | [diff] [blame] | 3568 | MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3569 | |
Manuel Pégourié-Gonnard | abae74c | 2013-08-20 13:53:44 +0200 | [diff] [blame] | 3570 | /* |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3571 | * 2.3: Compute and add the signature |
Manuel Pégourié-Gonnard | abae74c | 2013-08-20 13:53:44 +0200 | [diff] [blame] | 3572 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3573 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 3574 | if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 3575 | { |
Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3576 | /* |
| 3577 | * For TLS 1.2, we need to specify signature and hash algorithm |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 3578 | * explicitly through a prefix to the signature. |
| 3579 | * |
| 3580 | * struct { |
| 3581 | * HashAlgorithm hash; |
| 3582 | * SignatureAlgorithm signature; |
| 3583 | * } SignatureAndHashAlgorithm; |
| 3584 | * |
| 3585 | * struct { |
| 3586 | * SignatureAndHashAlgorithm algorithm; |
| 3587 | * opaque signature<0..2^16-1>; |
| 3588 | * } DigitallySigned; |
| 3589 | * |
| 3590 | */ |
| 3591 | |
Gilles Peskine | 1004c19 | 2018-01-08 16:59:14 +0100 | [diff] [blame] | 3592 | ssl->out_msg[ssl->out_msglen++] = |
| 3593 | mbedtls_ssl_hash_from_md_alg( md_alg ); |
| 3594 | ssl->out_msg[ssl->out_msglen++] = |
| 3595 | mbedtls_ssl_sig_from_pk_alg( sig_alg ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3596 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3597 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3598 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3599 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 4bf9a28 | 2018-01-05 21:20:50 +0100 | [diff] [blame] | 3600 | if( ssl->conf->f_async_sign_start != NULL ) |
| 3601 | { |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 3602 | ret = ssl->conf->f_async_sign_start( ssl, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3603 | mbedtls_ssl_own_cert( ssl ), |
| 3604 | md_alg, hash, hashlen ); |
Gilles Peskine | 4bf9a28 | 2018-01-05 21:20:50 +0100 | [diff] [blame] | 3605 | switch( ret ) |
| 3606 | { |
| 3607 | case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH: |
| 3608 | /* act as if f_async_sign was null */ |
| 3609 | break; |
| 3610 | case 0: |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3611 | ssl->handshake->async_in_progress = 1; |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3612 | return( ssl_resume_server_key_exchange( ssl, signature_len ) ); |
Gilles Peskine | 4bf9a28 | 2018-01-05 21:20:50 +0100 | [diff] [blame] | 3613 | case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS: |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3614 | ssl->handshake->async_in_progress = 1; |
Gilles Peskine | 4bf9a28 | 2018-01-05 21:20:50 +0100 | [diff] [blame] | 3615 | return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS ); |
| 3616 | default: |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3617 | MBEDTLS_SSL_DEBUG_RET( 1, "f_async_sign_start", ret ); |
Gilles Peskine | 4bf9a28 | 2018-01-05 21:20:50 +0100 | [diff] [blame] | 3618 | return( ret ); |
| 3619 | } |
| 3620 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3621 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 4bf9a28 | 2018-01-05 21:20:50 +0100 | [diff] [blame] | 3622 | |
| 3623 | if( mbedtls_ssl_own_key( ssl ) == NULL ) |
| 3624 | { |
| 3625 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key" ) ); |
| 3626 | return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ); |
| 3627 | } |
| 3628 | |
Gilles Peskine | 0fd90dd | 2018-04-26 07:41:09 +0200 | [diff] [blame] | 3629 | /* Append the signature to ssl->out_msg, leaving 2 bytes for the |
| 3630 | * signature length which will be added in ssl_write_server_key_exchange |
| 3631 | * after the call to ssl_prepare_server_key_exchange. |
| 3632 | * ssl_write_server_key_exchange also takes care of incrementing |
| 3633 | * ssl->out_msglen. */ |
Gilles Peskine | 1004c19 | 2018-01-08 16:59:14 +0100 | [diff] [blame] | 3634 | if( ( ret = mbedtls_pk_sign( mbedtls_ssl_own_key( ssl ), |
| 3635 | md_alg, hash, hashlen, |
| 3636 | ssl->out_msg + ssl->out_msglen + 2, |
Gilles Peskine | 7ab013a | 2018-01-08 17:04:16 +0100 | [diff] [blame] | 3637 | signature_len, |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 3638 | mbedtls_ssl_conf_get_frng( ssl->conf ), |
Hanno Becker | 9a12243 | 2019-07-23 13:24:02 +0100 | [diff] [blame] | 3639 | mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3640 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3641 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 3642 | return( ret ); |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 3643 | } |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3644 | } |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3645 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3646 | |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3647 | return( 0 ); |
| 3648 | } |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3649 | |
Gilles Peskine | d3eb061 | 2018-01-08 17:07:44 +0100 | [diff] [blame] | 3650 | /* Prepare the ServerKeyExchange message and send it. For ciphersuites |
Gilles Peskine | 168dae8 | 2018-04-25 23:35:42 +0200 | [diff] [blame] | 3651 | * that do not include a ServerKeyExchange message, do nothing. Either |
| 3652 | * way, if successful, move on to the next step in the SSL state |
| 3653 | * machine. */ |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3654 | static int ssl_write_server_key_exchange( mbedtls_ssl_context *ssl ) |
| 3655 | { |
| 3656 | int ret; |
Gilles Peskine | 7ab013a | 2018-01-08 17:04:16 +0100 | [diff] [blame] | 3657 | size_t signature_len = 0; |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3658 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME_NON_PFS__ENABLED) |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 3659 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = |
Hanno Becker | df64596 | 2019-06-26 13:02:22 +0100 | [diff] [blame] | 3660 | mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); |
Gilles Peskine | f112725 | 2018-04-24 13:05:39 +0200 | [diff] [blame] | 3661 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME_NON_PFS__ENABLED */ |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3662 | |
Gilles Peskine | d3eb061 | 2018-01-08 17:07:44 +0100 | [diff] [blame] | 3663 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) ); |
| 3664 | |
| 3665 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME_NON_PFS__ENABLED) |
| 3666 | /* Extract static ECDH parameters and abort if ServerKeyExchange |
| 3667 | * is not needed. */ |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3668 | if( mbedtls_ssl_ciphersuite_no_pfs( ciphersuite_info ) ) |
| 3669 | { |
| 3670 | /* For suites involving ECDH, extract DH parameters |
| 3671 | * from certificate at this point. */ |
| 3672 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__ECDH_ENABLED) |
| 3673 | if( mbedtls_ssl_ciphersuite_uses_ecdh( ciphersuite_info ) ) |
| 3674 | { |
| 3675 | ssl_get_ecdh_params_from_cert( ssl ); |
| 3676 | } |
| 3677 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__ECDH_ENABLED */ |
| 3678 | |
| 3679 | /* Key exchanges not involving ephemeral keys don't use |
| 3680 | * ServerKeyExchange, so end here. */ |
| 3681 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) ); |
| 3682 | ssl->state++; |
| 3683 | return( 0 ); |
| 3684 | } |
Gilles Peskine | f112725 | 2018-04-24 13:05:39 +0200 | [diff] [blame] | 3685 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME_NON_PFS__ENABLED */ |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3686 | |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3687 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) && \ |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3688 | defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | d3eb061 | 2018-01-08 17:07:44 +0100 | [diff] [blame] | 3689 | /* If we have already prepared the message and there is an ongoing |
Gilles Peskine | 168dae8 | 2018-04-25 23:35:42 +0200 | [diff] [blame] | 3690 | * signature operation, resume signing. */ |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3691 | if( ssl->handshake->async_in_progress != 0 ) |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3692 | { |
| 3693 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "resuming signature operation" ) ); |
| 3694 | ret = ssl_resume_server_key_exchange( ssl, &signature_len ); |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3695 | } |
| 3696 | else |
| 3697 | #endif /* defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) && |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3698 | defined(MBEDTLS_SSL_ASYNC_PRIVATE) */ |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3699 | { |
| 3700 | /* ServerKeyExchange is needed. Prepare the message. */ |
| 3701 | ret = ssl_prepare_server_key_exchange( ssl, &signature_len ); |
Gilles Peskine | d3eb061 | 2018-01-08 17:07:44 +0100 | [diff] [blame] | 3702 | } |
| 3703 | |
| 3704 | if( ret != 0 ) |
| 3705 | { |
Gilles Peskine | ad28bf0 | 2018-04-26 00:19:16 +0200 | [diff] [blame] | 3706 | /* If we're starting to write a new message, set ssl->out_msglen |
| 3707 | * to 0. But if we're resuming after an asynchronous message, |
| 3708 | * out_msglen is the amount of data written so far and mst be |
| 3709 | * preserved. */ |
Gilles Peskine | d3eb061 | 2018-01-08 17:07:44 +0100 | [diff] [blame] | 3710 | if( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS ) |
| 3711 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server key exchange (pending)" ) ); |
| 3712 | else |
| 3713 | ssl->out_msglen = 0; |
| 3714 | return( ret ); |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3715 | } |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3716 | |
Gilles Peskine | 7ab013a | 2018-01-08 17:04:16 +0100 | [diff] [blame] | 3717 | /* If there is a signature, write its length. |
Gilles Peskine | 168dae8 | 2018-04-25 23:35:42 +0200 | [diff] [blame] | 3718 | * ssl_prepare_server_key_exchange already wrote the signature |
| 3719 | * itself at its proper place in the output buffer. */ |
Gilles Peskine | 7ab013a | 2018-01-08 17:04:16 +0100 | [diff] [blame] | 3720 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) |
| 3721 | if( signature_len != 0 ) |
| 3722 | { |
| 3723 | ssl->out_msg[ssl->out_msglen++] = (unsigned char)( signature_len >> 8 ); |
| 3724 | ssl->out_msg[ssl->out_msglen++] = (unsigned char)( signature_len ); |
| 3725 | |
| 3726 | MBEDTLS_SSL_DEBUG_BUF( 3, "my signature", |
| 3727 | ssl->out_msg + ssl->out_msglen, |
| 3728 | signature_len ); |
| 3729 | |
| 3730 | /* Skip over the already-written signature */ |
| 3731 | ssl->out_msglen += signature_len; |
| 3732 | } |
| 3733 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED */ |
| 3734 | |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3735 | /* Add header and send. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3736 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 3737 | ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3738 | |
| 3739 | ssl->state++; |
| 3740 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3741 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3742 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3743 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3744 | return( ret ); |
| 3745 | } |
| 3746 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3747 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3748 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3749 | } |
| 3750 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3751 | static int ssl_write_server_hello_done( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3752 | { |
| 3753 | int ret; |
| 3754 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3755 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3756 | |
| 3757 | ssl->out_msglen = 4; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3758 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 3759 | ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO_DONE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3760 | |
| 3761 | ssl->state++; |
| 3762 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3763 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 3764 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3765 | mbedtls_ssl_send_flight_completed( ssl ); |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3766 | #endif |
| 3767 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3768 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3769 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3770 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3771 | return( ret ); |
| 3772 | } |
| 3773 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3774 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 3775 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3776 | ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 3777 | { |
| 3778 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); |
| 3779 | return( ret ); |
| 3780 | } |
Hanno Becker | bc2498a | 2018-08-28 10:13:29 +0100 | [diff] [blame] | 3781 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3782 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3783 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3784 | |
| 3785 | return( 0 ); |
| 3786 | } |
| 3787 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3788 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \ |
| 3789 | defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 3790 | 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] | 3791 | const unsigned char *end ) |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3792 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3793 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3794 | size_t n; |
| 3795 | |
| 3796 | /* |
| 3797 | * Receive G^Y mod P, premaster = (G^Y)^X mod P |
| 3798 | */ |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3799 | if( *p + 2 > end ) |
| 3800 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3801 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); |
| 3802 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3803 | } |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3804 | |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3805 | n = ( (*p)[0] << 8 ) | (*p)[1]; |
| 3806 | *p += 2; |
| 3807 | |
Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 3808 | if( *p + n > end ) |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3809 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3810 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); |
| 3811 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3812 | } |
| 3813 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3814 | 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] | 3815 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3816 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_read_public", ret ); |
| 3817 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP ); |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3818 | } |
| 3819 | |
Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 3820 | *p += n; |
| 3821 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3822 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY ); |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3823 | |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3824 | return( ret ); |
| 3825 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3826 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED || |
| 3827 | MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3828 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3829 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \ |
| 3830 | defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3831 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3832 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3833 | static int ssl_resume_decrypt_pms( mbedtls_ssl_context *ssl, |
| 3834 | unsigned char *peer_pms, |
| 3835 | size_t *peer_pmslen, |
| 3836 | size_t peer_pmssize ) |
| 3837 | { |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 3838 | int ret = ssl->conf->f_async_resume( ssl, |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3839 | peer_pms, peer_pmslen, peer_pmssize ); |
| 3840 | if( ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS ) |
| 3841 | { |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3842 | ssl->handshake->async_in_progress = 0; |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 3843 | mbedtls_ssl_set_async_operation_data( ssl, NULL ); |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3844 | } |
| 3845 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl_decrypt_encrypted_pms", ret ); |
| 3846 | return( ret ); |
| 3847 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3848 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3849 | |
Gilles Peskine | bcd98a5 | 2018-01-11 21:30:40 +0100 | [diff] [blame] | 3850 | static int ssl_decrypt_encrypted_pms( mbedtls_ssl_context *ssl, |
| 3851 | const unsigned char *p, |
| 3852 | const unsigned char *end, |
| 3853 | unsigned char *peer_pms, |
| 3854 | size_t *peer_pmslen, |
| 3855 | size_t peer_pmssize ) |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3856 | { |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 3857 | int ret; |
Hanno Becker | 81bb4d0 | 2019-02-16 11:03:48 +0000 | [diff] [blame] | 3858 | 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] | 3859 | mbedtls_pk_context *private_key = mbedtls_ssl_own_key( ssl ); |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3860 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3861 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3862 | /* 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] | 3863 | * decryption operation, resume signing. */ |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3864 | if( ssl->handshake->async_in_progress != 0 ) |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3865 | { |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3866 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "resuming decryption operation" ) ); |
| 3867 | return( ssl_resume_decrypt_pms( ssl, |
| 3868 | peer_pms, peer_pmslen, peer_pmssize ) ); |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3869 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3870 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3871 | |
| 3872 | /* |
Gilles Peskine | 422ccab | 2018-01-11 18:29:01 +0100 | [diff] [blame] | 3873 | * Prepare to decrypt the premaster using own private RSA key |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3874 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3875 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 3876 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | 81bb4d0 | 2019-02-16 11:03:48 +0000 | [diff] [blame] | 3877 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 3878 | if( mbedtls_ssl_get_minor_ver( ssl ) != MBEDTLS_SSL_MINOR_VERSION_0 ) |
Hanno Becker | 81bb4d0 | 2019-02-16 11:03:48 +0000 | [diff] [blame] | 3879 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3880 | { |
Hanno Becker | 81bb4d0 | 2019-02-16 11:03:48 +0000 | [diff] [blame] | 3881 | if( len < 2 ) |
| 3882 | { |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 3883 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); |
| 3884 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
| 3885 | } |
Hanno Becker | 81bb4d0 | 2019-02-16 11:03:48 +0000 | [diff] [blame] | 3886 | len -= 2; |
| 3887 | |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 3888 | if( *p++ != ( ( len >> 8 ) & 0xFF ) || |
| 3889 | *p++ != ( ( len ) & 0xFF ) ) |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3890 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3891 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); |
| 3892 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3893 | } |
| 3894 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 3895 | #endif |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3896 | |
Gilles Peskine | 422ccab | 2018-01-11 18:29:01 +0100 | [diff] [blame] | 3897 | /* |
| 3898 | * Decrypt the premaster secret |
| 3899 | */ |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3900 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3901 | if( ssl->conf->f_async_decrypt_start != NULL ) |
| 3902 | { |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 3903 | ret = ssl->conf->f_async_decrypt_start( ssl, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3904 | mbedtls_ssl_own_cert( ssl ), |
| 3905 | p, len ); |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3906 | switch( ret ) |
| 3907 | { |
| 3908 | case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH: |
| 3909 | /* act as if f_async_decrypt_start was null */ |
| 3910 | break; |
| 3911 | case 0: |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3912 | ssl->handshake->async_in_progress = 1; |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3913 | return( ssl_resume_decrypt_pms( ssl, |
| 3914 | peer_pms, |
| 3915 | peer_pmslen, |
| 3916 | peer_pmssize ) ); |
| 3917 | case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS: |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3918 | ssl->handshake->async_in_progress = 1; |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3919 | return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS ); |
| 3920 | default: |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3921 | MBEDTLS_SSL_DEBUG_RET( 1, "f_async_decrypt_start", ret ); |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3922 | return( ret ); |
| 3923 | } |
| 3924 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3925 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3926 | |
Gilles Peskine | 422ccab | 2018-01-11 18:29:01 +0100 | [diff] [blame] | 3927 | if( ! mbedtls_pk_can_do( private_key, MBEDTLS_PK_RSA ) ) |
| 3928 | { |
Gilles Peskine | 422ccab | 2018-01-11 18:29:01 +0100 | [diff] [blame] | 3929 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) ); |
| 3930 | return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ); |
| 3931 | } |
| 3932 | |
| 3933 | ret = mbedtls_pk_decrypt( private_key, p, len, |
Gilles Peskine | bcd98a5 | 2018-01-11 21:30:40 +0100 | [diff] [blame] | 3934 | peer_pms, peer_pmslen, peer_pmssize, |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 3935 | mbedtls_ssl_conf_get_frng( ssl->conf ), |
Hanno Becker | 9a12243 | 2019-07-23 13:24:02 +0100 | [diff] [blame] | 3936 | mbedtls_ssl_conf_get_prng( ssl->conf ) ); |
Gilles Peskine | bcd98a5 | 2018-01-11 21:30:40 +0100 | [diff] [blame] | 3937 | return( ret ); |
| 3938 | } |
| 3939 | |
| 3940 | static int ssl_parse_encrypted_pms( mbedtls_ssl_context *ssl, |
| 3941 | const unsigned char *p, |
| 3942 | const unsigned char *end, |
| 3943 | size_t pms_offset ) |
| 3944 | { |
| 3945 | int ret; |
| 3946 | unsigned char *pms = ssl->handshake->premaster + pms_offset; |
| 3947 | unsigned char ver[2]; |
| 3948 | unsigned char fake_pms[48], peer_pms[48]; |
| 3949 | unsigned char mask; |
| 3950 | size_t i, peer_pmslen; |
| 3951 | unsigned int diff; |
| 3952 | |
Gilles Peskine | 0a8352b | 2018-06-13 18:16:41 +0200 | [diff] [blame] | 3953 | /* In case of a failure in decryption, the decryption may write less than |
| 3954 | * 2 bytes of output, but we always read the first two bytes. It doesn't |
| 3955 | * matter in the end because diff will be nonzero in that case due to |
| 3956 | * peer_pmslen being less than 48, and we only care whether diff is 0. |
| 3957 | * But do initialize peer_pms for robustness anyway. This also makes |
| 3958 | * memory analyzers happy (don't access uninitialized memory, even |
| 3959 | * if it's an unsigned char). */ |
| 3960 | peer_pms[0] = peer_pms[1] = ~0; |
| 3961 | |
Gilles Peskine | bcd98a5 | 2018-01-11 21:30:40 +0100 | [diff] [blame] | 3962 | ret = ssl_decrypt_encrypted_pms( ssl, p, end, |
| 3963 | peer_pms, |
| 3964 | &peer_pmslen, |
| 3965 | sizeof( peer_pms ) ); |
| 3966 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3967 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3968 | if ( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS ) |
| 3969 | return( ret ); |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3970 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3971 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3972 | mbedtls_ssl_write_version( ssl->handshake->max_major_ver, |
Gilles Peskine | 2e33337 | 2018-04-24 13:22:10 +0200 | [diff] [blame] | 3973 | ssl->handshake->max_minor_ver, |
| 3974 | ssl->conf->transport, ver ); |
| 3975 | |
| 3976 | /* Avoid data-dependent branches while checking for invalid |
| 3977 | * padding, to protect against timing-based Bleichenbacher-type |
| 3978 | * attacks. */ |
| 3979 | diff = (unsigned int) ret; |
| 3980 | diff |= peer_pmslen ^ 48; |
| 3981 | diff |= peer_pms[0] ^ ver[0]; |
| 3982 | diff |= peer_pms[1] ^ ver[1]; |
| 3983 | |
| 3984 | /* mask = diff ? 0xff : 0x00 using bit operations to avoid branches */ |
| 3985 | /* MSVC has a warning about unary minus on unsigned, but this is |
| 3986 | * well-defined and precisely what we want to do here */ |
| 3987 | #if defined(_MSC_VER) |
| 3988 | #pragma warning( push ) |
| 3989 | #pragma warning( disable : 4146 ) |
| 3990 | #endif |
| 3991 | mask = - ( ( diff | - diff ) >> ( sizeof( unsigned int ) * 8 - 1 ) ); |
| 3992 | #if defined(_MSC_VER) |
| 3993 | #pragma warning( pop ) |
| 3994 | #endif |
Manuel Pégourié-Gonnard | b9c93d0 | 2015-06-23 13:53:15 +0200 | [diff] [blame] | 3995 | |
Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 3996 | /* |
| 3997 | * Protection against Bleichenbacher's attack: invalid PKCS#1 v1.5 padding |
| 3998 | * must not cause the connection to end immediately; instead, send a |
| 3999 | * bad_record_mac later in the handshake. |
Gilles Peskine | bcd98a5 | 2018-01-11 21:30:40 +0100 | [diff] [blame] | 4000 | * To protect against timing-based variants of the attack, we must |
| 4001 | * not have any branch that depends on whether the decryption was |
| 4002 | * successful. In particular, always generate the fake premaster secret, |
| 4003 | * 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] | 4004 | */ |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 4005 | ret = mbedtls_ssl_conf_get_frng( ssl->conf ) |
Hanno Becker | 9a12243 | 2019-07-23 13:24:02 +0100 | [diff] [blame] | 4006 | ( 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] | 4007 | if( ret != 0 ) |
Gilles Peskine | bcd98a5 | 2018-01-11 21:30:40 +0100 | [diff] [blame] | 4008 | { |
Gilles Peskine | e141638 | 2018-04-26 10:23:21 +0200 | [diff] [blame] | 4009 | /* It's ok to abort on an RNG failure, since this does not reveal |
| 4010 | * anything about the RSA decryption. */ |
Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 4011 | return( ret ); |
Gilles Peskine | bcd98a5 | 2018-01-11 21:30:40 +0100 | [diff] [blame] | 4012 | } |
Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 4013 | |
Manuel Pégourié-Gonnard | 331ba57 | 2015-04-20 12:33:57 +0100 | [diff] [blame] | 4014 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Manuel Pégourié-Gonnard | ce60fbe | 2015-04-15 16:45:52 +0200 | [diff] [blame] | 4015 | if( diff != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4016 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); |
Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 4017 | #endif |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 4018 | |
Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 4019 | if( sizeof( ssl->handshake->premaster ) < pms_offset || |
| 4020 | sizeof( ssl->handshake->premaster ) - pms_offset < 48 ) |
| 4021 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4022 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 4023 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 4024 | } |
Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 4025 | ssl->handshake->pmslen = 48; |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 4026 | |
Gilles Peskine | 422ccab | 2018-01-11 18:29:01 +0100 | [diff] [blame] | 4027 | /* Set pms to either the true or the fake PMS, without |
| 4028 | * data-dependent branches. */ |
Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 4029 | for( i = 0; i < ssl->handshake->pmslen; i++ ) |
| 4030 | pms[i] = ( mask & fake_pms[i] ) | ( (~mask) & peer_pms[i] ); |
| 4031 | |
| 4032 | return( 0 ); |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 4033 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4034 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED || |
| 4035 | MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 4036 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4037 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
| 4038 | 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] | 4039 | const unsigned char *end ) |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4040 | { |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 4041 | int ret = 0; |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4042 | size_t n; |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4043 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4044 | if( ssl->conf->f_psk == NULL && |
| 4045 | ( ssl->conf->psk == NULL || ssl->conf->psk_identity == NULL || |
| 4046 | ssl->conf->psk_identity_len == 0 || ssl->conf->psk_len == 0 ) ) |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4047 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4048 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) ); |
| 4049 | return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ); |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4050 | } |
| 4051 | |
| 4052 | /* |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4053 | * Receive client pre-shared key identity name |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4054 | */ |
Hanno Becker | 83c9f49 | 2017-06-26 13:52:14 +0100 | [diff] [blame] | 4055 | if( end - *p < 2 ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4056 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4057 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); |
| 4058 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4059 | } |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4060 | |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4061 | n = ( (*p)[0] << 8 ) | (*p)[1]; |
| 4062 | *p += 2; |
| 4063 | |
Hanno Becker | 83c9f49 | 2017-06-26 13:52:14 +0100 | [diff] [blame] | 4064 | if( n < 1 || n > 65535 || n > (size_t) ( end - *p ) ) |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4065 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4066 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); |
| 4067 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4068 | } |
| 4069 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4070 | if( ssl->conf->f_psk != NULL ) |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 4071 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4072 | 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] | 4073 | ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY; |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 4074 | } |
Manuel Pégourié-Gonnard | d27680b | 2014-07-08 14:15:55 +0200 | [diff] [blame] | 4075 | else |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 4076 | { |
Manuel Pégourié-Gonnard | 31ff1d2 | 2013-10-28 13:46:11 +0100 | [diff] [blame] | 4077 | /* Identity is not a big secret since clients send it in the clear, |
| 4078 | * but treat it carefully anyway, just in case */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4079 | if( n != ssl->conf->psk_identity_len || |
| 4080 | mbedtls_ssl_safer_memcmp( ssl->conf->psk_identity, *p, n ) != 0 ) |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 4081 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4082 | ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY; |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 4083 | } |
| 4084 | } |
| 4085 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4086 | if( ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY ) |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4087 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4088 | MBEDTLS_SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n ); |
Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 4089 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 4090 | MBEDTLS_SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4091 | return( MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY ); |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4092 | } |
| 4093 | |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4094 | *p += n; |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4095 | |
Manuel Pégourié-Gonnard | d27680b | 2014-07-08 14:15:55 +0200 | [diff] [blame] | 4096 | return( 0 ); |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4097 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4098 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 4099 | |
Hanno Becker | 7ec345d | 2018-05-21 17:16:42 +0100 | [diff] [blame] | 4100 | /* |
| 4101 | * |
| 4102 | * STATE HANDLING: Client Key Exchange |
| 4103 | * |
| 4104 | */ |
| 4105 | |
| 4106 | /* |
| 4107 | * Overview |
| 4108 | */ |
| 4109 | |
| 4110 | /* Main entry point; orchestrates the other functions. */ |
Hanno Becker | 7d552fa | 2019-08-12 15:04:59 +0100 | [diff] [blame] | 4111 | static int ssl_process_in_client_key_exchange( mbedtls_ssl_context *ssl ); |
Hanno Becker | 7ec345d | 2018-05-21 17:16:42 +0100 | [diff] [blame] | 4112 | |
Hanno Becker | 7d552fa | 2019-08-12 15:04:59 +0100 | [diff] [blame] | 4113 | static int ssl_in_client_key_exchange_parse( mbedtls_ssl_context *ssl, |
| 4114 | unsigned char *buf, |
| 4115 | size_t buflen ); |
Hanno Becker | 7ec345d | 2018-05-21 17:16:42 +0100 | [diff] [blame] | 4116 | /* Update the handshake state */ |
Hanno Becker | 7d552fa | 2019-08-12 15:04:59 +0100 | [diff] [blame] | 4117 | static int ssl_in_client_key_exchange_postprocess( mbedtls_ssl_context *ssl ); |
Hanno Becker | 7ec345d | 2018-05-21 17:16:42 +0100 | [diff] [blame] | 4118 | |
| 4119 | /* |
| 4120 | * Implementation |
| 4121 | */ |
| 4122 | |
Hanno Becker | 7d552fa | 2019-08-12 15:04:59 +0100 | [diff] [blame] | 4123 | static int ssl_process_in_client_key_exchange( mbedtls_ssl_context *ssl ) |
Hanno Becker | 7ec345d | 2018-05-21 17:16:42 +0100 | [diff] [blame] | 4124 | { |
| 4125 | int ret; |
| 4126 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> process client key exchange" ) ); |
| 4127 | |
| 4128 | /* The ClientKeyExchange message is never skipped. */ |
| 4129 | |
| 4130 | /* Reading step */ |
Hanno Becker | 1e23af8 | 2018-05-21 17:26:17 +0100 | [diff] [blame] | 4131 | if( ( ret = mbedtls_ssl_read_record( ssl, |
| 4132 | 1 /* update checksum */ ) ) != 0 ) |
Hanno Becker | 7ec345d | 2018-05-21 17:16:42 +0100 | [diff] [blame] | 4133 | { |
| 4134 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 4135 | return( ret ); |
| 4136 | } |
| 4137 | |
| 4138 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE || |
| 4139 | ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE ) |
| 4140 | { |
| 4141 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); |
| 4142 | mbedtls_ssl_pend_fatal_alert( ssl, |
| 4143 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 4144 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 4145 | goto cleanup; |
| 4146 | } |
| 4147 | |
Hanno Becker | 7d552fa | 2019-08-12 15:04:59 +0100 | [diff] [blame] | 4148 | MBEDTLS_SSL_CHK( ssl_in_client_key_exchange_parse( ssl, ssl->in_msg, |
| 4149 | ssl->in_hslen ) ); |
Hanno Becker | 7ec345d | 2018-05-21 17:16:42 +0100 | [diff] [blame] | 4150 | |
| 4151 | /* Update state */ |
Hanno Becker | 7d552fa | 2019-08-12 15:04:59 +0100 | [diff] [blame] | 4152 | MBEDTLS_SSL_CHK( ssl_in_client_key_exchange_postprocess( ssl ) ); |
Hanno Becker | 7ec345d | 2018-05-21 17:16:42 +0100 | [diff] [blame] | 4153 | |
| 4154 | cleanup: |
| 4155 | |
Hanno Becker | 9db697e | 2019-07-23 12:19:48 +0100 | [diff] [blame] | 4156 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
| 4157 | if ( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS ) |
| 4158 | ssl->keep_current_message = 1; |
| 4159 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
| 4160 | |
Hanno Becker | 7ec345d | 2018-05-21 17:16:42 +0100 | [diff] [blame] | 4161 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= process client key exchange" ) ); |
| 4162 | return( ret ); |
| 4163 | } |
| 4164 | |
Hanno Becker | 4e46709 | 2019-08-12 15:12:35 +0100 | [diff] [blame] | 4165 | /* Warning: Despite accepting a length argument, this function is currently |
| 4166 | * still lacking some bounds checks and assumes that `buf` has length |
| 4167 | * `MBEDTLS_SSL_IN_CONTENT_LEN`. Eventually, it should be rewritten to work |
| 4168 | * with any buffer + length pair, returning MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL |
| 4169 | * on insufficient parsing space. */ |
Hanno Becker | 7d552fa | 2019-08-12 15:04:59 +0100 | [diff] [blame] | 4170 | static int ssl_in_client_key_exchange_parse( mbedtls_ssl_context *ssl, |
Hanno Becker | 7ec345d | 2018-05-21 17:16:42 +0100 | [diff] [blame] | 4171 | unsigned char *buf, |
| 4172 | size_t buflen ) |
| 4173 | { |
Hanno Becker | e7c4eed | 2018-05-21 17:30:11 +0100 | [diff] [blame] | 4174 | int ret; |
| 4175 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = |
| 4176 | mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); |
| 4177 | unsigned char *p, *end; |
| 4178 | |
| 4179 | p = buf + mbedtls_ssl_hs_hdr_len( ssl ); |
| 4180 | end = buf + buflen; |
| 4181 | |
| 4182 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) |
| 4183 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 4184 | == MBEDTLS_KEY_EXCHANGE_DHE_RSA ) |
| 4185 | { |
| 4186 | if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 ) |
| 4187 | { |
| 4188 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret ); |
| 4189 | return( ret ); |
| 4190 | } |
| 4191 | |
| 4192 | if( p != end ) |
| 4193 | { |
| 4194 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) ); |
| 4195 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
| 4196 | } |
| 4197 | } |
| 4198 | else |
| 4199 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */ |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 4200 | #if defined(MBEDTLS_USE_TINYCRYPT) |
| 4201 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 4202 | == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA || |
| 4203 | mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 4204 | == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ) |
| 4205 | { |
Hanno Becker | 7a19633 | 2019-07-24 11:12:41 +0100 | [diff] [blame] | 4206 | ((void) ret); |
Hanno Becker | a3c2c17 | 2019-07-23 16:51:57 +0100 | [diff] [blame] | 4207 | if( mbedtls_ssl_ecdh_read_peerkey( ssl, &p, end ) != 0 ) |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 4208 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Jarno Lamsa | e12aafb | 2019-04-04 18:32:56 +0300 | [diff] [blame] | 4209 | } |
| 4210 | else |
| 4211 | #endif |
Hanno Becker | 29d1655 | 2019-07-24 11:11:45 +0100 | [diff] [blame] | 4212 | #if defined(MBEDTLS_ECDH_C) && \ |
| 4213 | ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ |
| 4214 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \ |
| 4215 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ |
| 4216 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) ) |
Hanno Becker | e7c4eed | 2018-05-21 17:30:11 +0100 | [diff] [blame] | 4217 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 4218 | == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA || |
| 4219 | mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 4220 | == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA || |
| 4221 | mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 4222 | == MBEDTLS_KEY_EXCHANGE_ECDH_RSA || |
| 4223 | mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 4224 | == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA ) |
| 4225 | { |
| 4226 | if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx, |
| 4227 | p, end - p) ) != 0 ) |
| 4228 | { |
Hanno Becker | 7ba0a88 | 2019-07-24 17:07:41 +0100 | [diff] [blame] | 4229 | ((void) ret); |
Hanno Becker | e7c4eed | 2018-05-21 17:30:11 +0100 | [diff] [blame] | 4230 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret ); |
| 4231 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP ); |
| 4232 | } |
| 4233 | |
| 4234 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 4235 | MBEDTLS_DEBUG_ECDH_QP ); |
| 4236 | } |
| 4237 | else |
| 4238 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || |
| 4239 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED || |
| 4240 | MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED || |
| 4241 | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */ |
| 4242 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 4243 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 4244 | == MBEDTLS_KEY_EXCHANGE_PSK ) |
| 4245 | { |
| 4246 | if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 ) |
| 4247 | { |
| 4248 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret ); |
| 4249 | return( ret ); |
| 4250 | } |
| 4251 | |
| 4252 | if( p != end ) |
| 4253 | { |
| 4254 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) ); |
| 4255 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
| 4256 | } |
| 4257 | } |
| 4258 | else |
| 4259 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
| 4260 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
| 4261 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 4262 | == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
| 4263 | { |
| 4264 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
| 4265 | if ( ssl->handshake->async_in_progress != 0 ) |
| 4266 | { |
| 4267 | /* There is an asynchronous operation in progress to |
| 4268 | * decrypt the encrypted premaster secret, so skip |
| 4269 | * directly to resuming this operation. */ |
| 4270 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "PSK identity already parsed" ) ); |
| 4271 | /* Update p to skip the PSK identity. ssl_parse_encrypted_pms |
| 4272 | * won't actually use it, but maintain p anyway for robustness. */ |
| 4273 | p += ssl->conf->psk_identity_len + 2; |
| 4274 | } |
| 4275 | else |
| 4276 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
| 4277 | if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 ) |
| 4278 | { |
| 4279 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret ); |
| 4280 | return( ret ); |
| 4281 | } |
| 4282 | |
| 4283 | if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 ) |
| 4284 | { |
| 4285 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret ); |
| 4286 | return( ret ); |
| 4287 | } |
| 4288 | } |
| 4289 | else |
| 4290 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
| 4291 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 4292 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 4293 | == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
| 4294 | { |
| 4295 | if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 ) |
| 4296 | { |
| 4297 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret ); |
| 4298 | return( ret ); |
| 4299 | } |
| 4300 | if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 ) |
| 4301 | { |
| 4302 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret ); |
| 4303 | return( ret ); |
| 4304 | } |
| 4305 | |
| 4306 | if( p != end ) |
| 4307 | { |
| 4308 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) ); |
| 4309 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
| 4310 | } |
| 4311 | } |
| 4312 | else |
| 4313 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
| 4314 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
| 4315 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 4316 | == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
| 4317 | { |
| 4318 | if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 ) |
| 4319 | { |
| 4320 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret ); |
| 4321 | return( ret ); |
| 4322 | } |
| 4323 | |
| 4324 | if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx, |
| 4325 | p, end - p ) ) != 0 ) |
| 4326 | { |
| 4327 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret ); |
| 4328 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP ); |
| 4329 | } |
| 4330 | |
| 4331 | MBEDTLS_SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp ); |
| 4332 | } |
| 4333 | else |
| 4334 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
| 4335 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) |
| 4336 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 4337 | == MBEDTLS_KEY_EXCHANGE_RSA ) |
| 4338 | { |
| 4339 | if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 0 ) ) != 0 ) |
| 4340 | { |
| 4341 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret ); |
| 4342 | return( ret ); |
| 4343 | } |
| 4344 | } |
| 4345 | else |
| 4346 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */ |
| 4347 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 4348 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) |
| 4349 | == MBEDTLS_KEY_EXCHANGE_ECJPAKE ) |
| 4350 | { |
| 4351 | ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx, |
| 4352 | p, end - p ); |
| 4353 | if( ret != 0 ) |
| 4354 | { |
| 4355 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret ); |
| 4356 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
| 4357 | } |
| 4358 | } |
| 4359 | else |
| 4360 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
| 4361 | { |
| 4362 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 4363 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4364 | } |
| 4365 | |
| 4366 | return( 0 ); |
Hanno Becker | 7ec345d | 2018-05-21 17:16:42 +0100 | [diff] [blame] | 4367 | } |
| 4368 | |
| 4369 | /* Update the handshake state */ |
Hanno Becker | 7d552fa | 2019-08-12 15:04:59 +0100 | [diff] [blame] | 4370 | static int ssl_in_client_key_exchange_postprocess( mbedtls_ssl_context *ssl ) |
Hanno Becker | 7ec345d | 2018-05-21 17:16:42 +0100 | [diff] [blame] | 4371 | { |
Hanno Becker | dc8bfb9 | 2018-05-21 17:24:27 +0100 | [diff] [blame] | 4372 | int ret; |
Hanno Becker | 1e23af8 | 2018-05-21 17:26:17 +0100 | [diff] [blame] | 4373 | |
Hanno Becker | 09d2364 | 2019-07-22 17:18:18 +0100 | [diff] [blame] | 4374 | if( ( ret = mbedtls_ssl_build_pms( ssl ) ) != 0 ) |
Hanno Becker | 1e23af8 | 2018-05-21 17:26:17 +0100 | [diff] [blame] | 4375 | { |
Hanno Becker | 09d2364 | 2019-07-22 17:18:18 +0100 | [diff] [blame] | 4376 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_build_pms", ret ); |
| 4377 | return( ret ); |
Hanno Becker | 1e23af8 | 2018-05-21 17:26:17 +0100 | [diff] [blame] | 4378 | } |
Hanno Becker | dc8bfb9 | 2018-05-21 17:24:27 +0100 | [diff] [blame] | 4379 | |
| 4380 | if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 ) |
| 4381 | { |
| 4382 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret ); |
| 4383 | return( ret ); |
| 4384 | } |
| 4385 | |
Hanno Becker | 7ec345d | 2018-05-21 17:16:42 +0100 | [diff] [blame] | 4386 | ssl->state = MBEDTLS_SSL_CERTIFICATE_VERIFY; |
| 4387 | return( 0 ); |
| 4388 | } |
| 4389 | |
Hanno Becker | ae39b9e | 2019-02-07 12:32:43 +0000 | [diff] [blame] | 4390 | #if !defined(MBEDTLS_KEY_EXCHANGE__CERT_REQ_ALLOWED__ENABLED) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4391 | static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4392 | { |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 4393 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = |
Hanno Becker | df64596 | 2019-06-26 13:02:22 +0100 | [diff] [blame] | 4394 | mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4395 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4396 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4397 | |
Hanno Becker | ae39b9e | 2019-02-07 12:32:43 +0000 | [diff] [blame] | 4398 | if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ) |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 4399 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4400 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 4401 | ssl->state++; |
| 4402 | return( 0 ); |
| 4403 | } |
| 4404 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4405 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 4406 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4407 | } |
Hanno Becker | ae39b9e | 2019-02-07 12:32:43 +0000 | [diff] [blame] | 4408 | #else /* !MBEDTLS_KEY_EXCHANGE__CERT_REQ_ALLOWED__ENABLED */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4409 | static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4410 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4411 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 4528f3f | 2014-09-10 14:17:23 +0000 | [diff] [blame] | 4412 | size_t i, sig_len; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4413 | unsigned char hash[48]; |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 4414 | unsigned char *hash_start = hash; |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4415 | size_t hashlen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4416 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4417 | mbedtls_pk_type_t pk_alg; |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 4418 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4419 | mbedtls_md_type_t md_alg; |
Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 4420 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = |
Hanno Becker | df64596 | 2019-06-26 13:02:22 +0100 | [diff] [blame] | 4421 | mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 4422 | mbedtls_pk_context *peer_pk = NULL; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4423 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4424 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4425 | |
Hanno Becker | cd90126 | 2019-02-07 13:17:25 +0000 | [diff] [blame] | 4426 | if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4427 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4428 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4429 | ssl->state++; |
| 4430 | return( 0 ); |
| 4431 | } |
| 4432 | |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 4433 | /* Skip if we haven't received a certificate from the client. |
| 4434 | * If MBEDTLS_SSL_KEEP_PEER_CERTIFICATE is set, this can be |
| 4435 | * inferred from the setting of mbedtls_ssl_session::peer_cert. |
| 4436 | * If MBEDTLS_SSL_KEEP_PEER_CERTIFICATE is not set, it can |
| 4437 | * be inferred from whether we've held back the peer CRT's |
| 4438 | * public key in mbedtls_ssl_handshake_params::peer_pubkey. */ |
| 4439 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 4440 | /* Because the peer CRT pubkey is embedded into the handshake |
| 4441 | * params currently, and there's no 'is_init' functions for PK |
| 4442 | * contexts, we need to break the abstraction and peek into |
| 4443 | * the PK context to see if it has been initialized. */ |
| 4444 | if( ssl->handshake->peer_pubkey.pk_info != NULL ) |
| 4445 | peer_pk = &ssl->handshake->peer_pubkey; |
| 4446 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 4447 | if( ssl->session_negotiate->peer_cert != NULL ) |
Hanno Becker | 73cd8d8 | 2019-02-28 14:04:16 +0000 | [diff] [blame] | 4448 | { |
| 4449 | ret = mbedtls_x509_crt_pk_acquire( ssl->session_negotiate->peer_cert, |
| 4450 | &peer_pk ); |
| 4451 | if( ret != 0 ) |
| 4452 | { |
Hanno Becker | 2224ccf | 2019-06-28 10:52:45 +0100 | [diff] [blame] | 4453 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_pk_acquire", ret ); |
| 4454 | return( ret ); |
Hanno Becker | 73cd8d8 | 2019-02-28 14:04:16 +0000 | [diff] [blame] | 4455 | } |
| 4456 | } |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 4457 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 4458 | |
| 4459 | if( peer_pk == NULL ) |
Hanno Becker | cd90126 | 2019-02-07 13:17:25 +0000 | [diff] [blame] | 4460 | { |
| 4461 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) ); |
| 4462 | ssl->state++; |
| 4463 | return( 0 ); |
| 4464 | } |
Hanno Becker | cd90126 | 2019-02-07 13:17:25 +0000 | [diff] [blame] | 4465 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4466 | /* Read the message without adding it to the checksum */ |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 4467 | ret = mbedtls_ssl_read_record( ssl, 0 /* no checksum update */ ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4468 | if( 0 != ret ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4469 | { |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 4470 | MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record" ), ret ); |
Hanno Becker | bc6b598 | 2019-07-02 15:36:44 +0100 | [diff] [blame] | 4471 | goto exit; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4472 | } |
| 4473 | |
| 4474 | ssl->state++; |
| 4475 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4476 | /* Process the message contents */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4477 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE || |
| 4478 | ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE_VERIFY ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4479 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4480 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) ); |
Hanno Becker | bc6b598 | 2019-07-02 15:36:44 +0100 | [diff] [blame] | 4481 | ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY; |
| 4482 | goto exit; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4483 | } |
| 4484 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4485 | i = mbedtls_ssl_hs_hdr_len( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4486 | |
Manuel Pégourié-Gonnard | 4528f3f | 2014-09-10 14:17:23 +0000 | [diff] [blame] | 4487 | /* |
| 4488 | * struct { |
| 4489 | * SignatureAndHashAlgorithm algorithm; -- TLS 1.2 only |
| 4490 | * opaque signature<0..2^16-1>; |
| 4491 | * } DigitallySigned; |
| 4492 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4493 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 4494 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 4495 | if( mbedtls_ssl_get_minor_ver( ssl ) != MBEDTLS_SSL_MINOR_VERSION_3 ) |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 4496 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4497 | md_alg = MBEDTLS_MD_NONE; |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4498 | hashlen = 36; |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 4499 | |
| 4500 | /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */ |
Hanno Becker | 0833c10 | 2019-02-06 18:31:04 +0000 | [diff] [blame] | 4501 | if( mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_ECDSA ) ) |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 4502 | { |
| 4503 | hash_start += 16; |
| 4504 | hashlen -= 16; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4505 | md_alg = MBEDTLS_MD_SHA1; |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 4506 | } |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 4507 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 4508 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4509 | #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || |
| 4510 | MBEDTLS_SSL_PROTO_TLS1_1 */ |
| 4511 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 4512 | 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] | 4513 | { |
Manuel Pégourié-Gonnard | 5ee9654 | 2014-09-10 14:27:21 +0000 | [diff] [blame] | 4514 | if( i + 2 > ssl->in_hslen ) |
| 4515 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4516 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) ); |
Hanno Becker | 73cd8d8 | 2019-02-28 14:04:16 +0000 | [diff] [blame] | 4517 | ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY; |
| 4518 | goto exit; |
Manuel Pégourié-Gonnard | 5ee9654 | 2014-09-10 14:27:21 +0000 | [diff] [blame] | 4519 | } |
| 4520 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4521 | /* |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4522 | * Hash |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4523 | */ |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4524 | md_alg = mbedtls_ssl_md_alg_from_hash( ssl->in_msg[i] ); |
| 4525 | |
| 4526 | if( md_alg == MBEDTLS_MD_NONE || mbedtls_ssl_set_calc_verify_md( ssl, ssl->in_msg[i] ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4527 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4528 | 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] | 4529 | " for verify message" ) ); |
Hanno Becker | 73cd8d8 | 2019-02-28 14:04:16 +0000 | [diff] [blame] | 4530 | ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY; |
| 4531 | goto exit; |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 4532 | } |
| 4533 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4534 | #if !defined(MBEDTLS_MD_SHA1) |
| 4535 | if( MBEDTLS_MD_SHA1 == md_alg ) |
| 4536 | hash_start += 16; |
| 4537 | #endif |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 4538 | |
Manuel Pégourié-Gonnard | bfe32ef | 2013-08-22 14:55:30 +0200 | [diff] [blame] | 4539 | /* Info from md_alg will be used instead */ |
| 4540 | hashlen = 0; |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4541 | |
Manuel Pégourié-Gonnard | 4528f3f | 2014-09-10 14:17:23 +0000 | [diff] [blame] | 4542 | i++; |
| 4543 | |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4544 | /* |
| 4545 | * Signature |
| 4546 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4547 | if( ( pk_alg = mbedtls_ssl_pk_alg_from_sig( ssl->in_msg[i] ) ) |
| 4548 | == MBEDTLS_PK_NONE ) |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4549 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4550 | 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] | 4551 | " for verify message" ) ); |
Hanno Becker | 73cd8d8 | 2019-02-28 14:04:16 +0000 | [diff] [blame] | 4552 | ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY; |
| 4553 | goto exit; |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4554 | } |
| 4555 | |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4556 | /* |
| 4557 | * Check the certificate's key type matches the signature alg |
| 4558 | */ |
Hanno Becker | 0833c10 | 2019-02-06 18:31:04 +0000 | [diff] [blame] | 4559 | if( !mbedtls_pk_can_do( peer_pk, pk_alg ) ) |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4560 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4561 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) ); |
Hanno Becker | 73cd8d8 | 2019-02-28 14:04:16 +0000 | [diff] [blame] | 4562 | ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY; |
| 4563 | goto exit; |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4564 | } |
Manuel Pégourié-Gonnard | 4528f3f | 2014-09-10 14:17:23 +0000 | [diff] [blame] | 4565 | |
| 4566 | i++; |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 4567 | } |
| 4568 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4569 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 4570 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4571 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 4572 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 4573 | } |
Manuel Pégourié-Gonnard | ff56da3 | 2013-07-11 10:46:21 +0200 | [diff] [blame] | 4574 | |
Manuel Pégourié-Gonnard | 5ee9654 | 2014-09-10 14:27:21 +0000 | [diff] [blame] | 4575 | if( i + 2 > ssl->in_hslen ) |
| 4576 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4577 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) ); |
Hanno Becker | 73cd8d8 | 2019-02-28 14:04:16 +0000 | [diff] [blame] | 4578 | ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY; |
| 4579 | goto exit; |
Manuel Pégourié-Gonnard | 5ee9654 | 2014-09-10 14:27:21 +0000 | [diff] [blame] | 4580 | } |
| 4581 | |
Manuel Pégourié-Gonnard | 4528f3f | 2014-09-10 14:17:23 +0000 | [diff] [blame] | 4582 | sig_len = ( ssl->in_msg[i] << 8 ) | ssl->in_msg[i+1]; |
| 4583 | i += 2; |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 4584 | |
Manuel Pégourié-Gonnard | 4528f3f | 2014-09-10 14:17:23 +0000 | [diff] [blame] | 4585 | if( i + sig_len != ssl->in_hslen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4586 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4587 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) ); |
Hanno Becker | 73cd8d8 | 2019-02-28 14:04:16 +0000 | [diff] [blame] | 4588 | ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY; |
| 4589 | goto exit; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4590 | } |
| 4591 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4592 | /* Calculate hash and verify signature */ |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 4593 | { |
| 4594 | size_t dummy_hlen; |
| 4595 | ssl->handshake->calc_verify( ssl, hash, &dummy_hlen ); |
| 4596 | } |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4597 | |
Hanno Becker | 0833c10 | 2019-02-06 18:31:04 +0000 | [diff] [blame] | 4598 | if( ( ret = mbedtls_pk_verify( peer_pk, |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 4599 | md_alg, hash_start, hashlen, |
Manuel Pégourié-Gonnard | 4528f3f | 2014-09-10 14:17:23 +0000 | [diff] [blame] | 4600 | ssl->in_msg + i, sig_len ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4601 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4602 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret ); |
Hanno Becker | 73cd8d8 | 2019-02-28 14:04:16 +0000 | [diff] [blame] | 4603 | goto exit; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4604 | } |
| 4605 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4606 | mbedtls_ssl_update_handshake_status( ssl ); |
| 4607 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4608 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4609 | |
Hanno Becker | 73cd8d8 | 2019-02-28 14:04:16 +0000 | [diff] [blame] | 4610 | exit: |
| 4611 | |
| 4612 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Hanno Becker | c6d1c3e | 2019-03-05 13:50:56 +0000 | [diff] [blame] | 4613 | mbedtls_x509_crt_pk_release( ssl->session_negotiate->peer_cert ); |
Hanno Becker | 73cd8d8 | 2019-02-28 14:04:16 +0000 | [diff] [blame] | 4614 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 4615 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 4616 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4617 | } |
Hanno Becker | ae39b9e | 2019-02-07 12:32:43 +0000 | [diff] [blame] | 4618 | #endif /* MBEDTLS_KEY_EXCHANGE__CERT_REQ_ALLOWED__ENABLED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4619 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4620 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 4621 | 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] | 4622 | { |
| 4623 | int ret; |
Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 4624 | size_t tlen; |
Manuel Pégourié-Gonnard | b0394be | 2015-05-19 11:40:30 +0200 | [diff] [blame] | 4625 | uint32_t lifetime; |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4626 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4627 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) ); |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4628 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4629 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 4630 | ssl->out_msg[0] = MBEDTLS_SSL_HS_NEW_SESSION_TICKET; |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4631 | |
| 4632 | /* |
| 4633 | * struct { |
| 4634 | * uint32 ticket_lifetime_hint; |
| 4635 | * opaque ticket<0..2^16-1>; |
| 4636 | * } NewSessionTicket; |
| 4637 | * |
| 4638 | * 4 . 7 ticket_lifetime_hint (0 = unspecified) |
| 4639 | * 8 . 9 ticket_len (n) |
| 4640 | * 10 . 9+n ticket content |
| 4641 | */ |
Manuel Pégourié-Gonnard | 164d894 | 2013-09-23 22:01:39 +0200 | [diff] [blame] | 4642 | |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 4643 | 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] | 4644 | ssl->session_negotiate, |
| 4645 | ssl->out_msg + 10, |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4646 | ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN, |
Manuel Pégourié-Gonnard | b0394be | 2015-05-19 11:40:30 +0200 | [diff] [blame] | 4647 | &tlen, &lifetime ) ) != 0 ) |
Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 4648 | { |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 4649 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_ticket_write", ret ); |
Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 4650 | tlen = 0; |
| 4651 | } |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4652 | |
Manuel Pégourié-Gonnard | b0394be | 2015-05-19 11:40:30 +0200 | [diff] [blame] | 4653 | ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF; |
| 4654 | ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF; |
| 4655 | ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF; |
| 4656 | ssl->out_msg[7] = ( lifetime ) & 0xFF; |
| 4657 | |
Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 4658 | ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF ); |
| 4659 | ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF ); |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4660 | |
Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 4661 | ssl->out_msglen = 10 + tlen; |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4662 | |
Manuel Pégourié-Gonnard | 145dfcb | 2014-02-26 14:23:33 +0100 | [diff] [blame] | 4663 | /* |
| 4664 | * Morally equivalent to updating ssl->state, but NewSessionTicket and |
| 4665 | * ChangeCipherSpec share the same state. |
| 4666 | */ |
| 4667 | ssl->handshake->new_session_ticket = 0; |
| 4668 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4669 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4670 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4671 | 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] | 4672 | return( ret ); |
| 4673 | } |
| 4674 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4675 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) ); |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4676 | |
| 4677 | return( 0 ); |
| 4678 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4679 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4680 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4681 | /* |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4682 | * SSL handshake -- server side -- single step |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4683 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4684 | int mbedtls_ssl_handshake_server_step( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4685 | { |
| 4686 | int ret = 0; |
| 4687 | |
Manuel Pégourié-Gonnard | dba460f | 2015-06-24 22:59:30 +0200 | [diff] [blame] | 4688 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4689 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4690 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4691 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4692 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4693 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4694 | return( ret ); |
| 4695 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4696 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 4697 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4698 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4699 | { |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4700 | if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4701 | return( ret ); |
| 4702 | } |
Hanno Becker | bc2498a | 2018-08-28 10:13:29 +0100 | [diff] [blame] | 4703 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4704 | |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4705 | switch( ssl->state ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4706 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4707 | case MBEDTLS_SSL_HELLO_REQUEST: |
| 4708 | ssl->state = MBEDTLS_SSL_CLIENT_HELLO; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4709 | break; |
| 4710 | |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4711 | /* |
| 4712 | * <== ClientHello |
| 4713 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4714 | case MBEDTLS_SSL_CLIENT_HELLO: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4715 | ret = ssl_parse_client_hello( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4716 | break; |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4717 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4718 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4719 | case MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT: |
| 4720 | return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED ); |
Manuel Pégourié-Gonnard | 579950c | 2014-09-29 17:47:33 +0200 | [diff] [blame] | 4721 | #endif |
| 4722 | |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4723 | /* |
| 4724 | * ==> ServerHello |
| 4725 | * Certificate |
| 4726 | * ( ServerKeyExchange ) |
| 4727 | * ( CertificateRequest ) |
| 4728 | * ServerHelloDone |
| 4729 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4730 | case MBEDTLS_SSL_SERVER_HELLO: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4731 | ret = ssl_write_server_hello( ssl ); |
| 4732 | break; |
| 4733 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4734 | case MBEDTLS_SSL_SERVER_CERTIFICATE: |
| 4735 | ret = mbedtls_ssl_write_certificate( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4736 | break; |
| 4737 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4738 | case MBEDTLS_SSL_SERVER_KEY_EXCHANGE: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4739 | ret = ssl_write_server_key_exchange( ssl ); |
| 4740 | break; |
| 4741 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4742 | case MBEDTLS_SSL_CERTIFICATE_REQUEST: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4743 | ret = ssl_write_certificate_request( ssl ); |
| 4744 | break; |
| 4745 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4746 | case MBEDTLS_SSL_SERVER_HELLO_DONE: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4747 | ret = ssl_write_server_hello_done( ssl ); |
| 4748 | break; |
| 4749 | |
| 4750 | /* |
| 4751 | * <== ( Certificate/Alert ) |
| 4752 | * ClientKeyExchange |
| 4753 | * ( CertificateVerify ) |
| 4754 | * ChangeCipherSpec |
| 4755 | * Finished |
| 4756 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4757 | case MBEDTLS_SSL_CLIENT_CERTIFICATE: |
| 4758 | ret = mbedtls_ssl_parse_certificate( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4759 | break; |
| 4760 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4761 | case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE: |
Hanno Becker | 7d552fa | 2019-08-12 15:04:59 +0100 | [diff] [blame] | 4762 | ret = ssl_process_in_client_key_exchange( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4763 | break; |
| 4764 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4765 | case MBEDTLS_SSL_CERTIFICATE_VERIFY: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4766 | ret = ssl_parse_certificate_verify( ssl ); |
| 4767 | break; |
| 4768 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4769 | case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC: |
| 4770 | ret = mbedtls_ssl_parse_change_cipher_spec( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4771 | break; |
| 4772 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4773 | case MBEDTLS_SSL_CLIENT_FINISHED: |
| 4774 | ret = mbedtls_ssl_parse_finished( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4775 | break; |
| 4776 | |
| 4777 | /* |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4778 | * ==> ( NewSessionTicket ) |
| 4779 | * ChangeCipherSpec |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4780 | * Finished |
| 4781 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4782 | case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC: |
| 4783 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | 7cd5924 | 2013-08-02 13:24:41 +0200 | [diff] [blame] | 4784 | if( ssl->handshake->new_session_ticket != 0 ) |
| 4785 | ret = ssl_write_new_session_ticket( ssl ); |
| 4786 | else |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 4787 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4788 | ret = mbedtls_ssl_write_change_cipher_spec( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4789 | break; |
| 4790 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4791 | case MBEDTLS_SSL_SERVER_FINISHED: |
| 4792 | ret = mbedtls_ssl_write_finished( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4793 | break; |
| 4794 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4795 | case MBEDTLS_SSL_FLUSH_BUFFERS: |
| 4796 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) ); |
| 4797 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
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_HANDSHAKE_WRAPUP: |
| 4801 | mbedtls_ssl_handshake_wrapup( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4802 | break; |
| 4803 | |
| 4804 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4805 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) ); |
| 4806 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4807 | } |
| 4808 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4809 | return( ret ); |
| 4810 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4811 | #endif /* MBEDTLS_SSL_SRV_C */ |