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" |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 41 | |
| 42 | #include <string.h> |
| 43 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 44 | #if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 45 | #include "mbedtls/ecp.h" |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 46 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 47 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 48 | #if defined(MBEDTLS_HAVE_TIME) |
Simon Butcher | b5b6af2 | 2016-07-13 14:46:18 +0100 | [diff] [blame] | 49 | #include "mbedtls/platform_time.h" |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 50 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 51 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 52 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 53 | /* Implementation that should never be optimized out by the compiler */ |
| 54 | static void mbedtls_zeroize( void *v, size_t n ) { |
| 55 | volatile unsigned char *p = v; while( n-- ) *p++ = 0; |
| 56 | } |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 57 | #endif |
| 58 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 59 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) |
| 60 | 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] | 61 | const unsigned char *info, |
| 62 | size_t ilen ) |
| 63 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 64 | if( ssl->conf->endpoint != MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 65 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 66 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 67 | mbedtls_free( ssl->cli_id ); |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 68 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 69 | if( ( ssl->cli_id = mbedtls_calloc( 1, ilen ) ) == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 70 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 71 | |
| 72 | memcpy( ssl->cli_id, info, ilen ); |
| 73 | ssl->cli_id_len = ilen; |
| 74 | |
| 75 | return( 0 ); |
| 76 | } |
Manuel Pégourié-Gonnard | d485d19 | 2014-07-23 14:56:15 +0200 | [diff] [blame] | 77 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 78 | void mbedtls_ssl_conf_dtls_cookies( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 79 | mbedtls_ssl_cookie_write_t *f_cookie_write, |
| 80 | mbedtls_ssl_cookie_check_t *f_cookie_check, |
Manuel Pégourié-Gonnard | d485d19 | 2014-07-23 14:56:15 +0200 | [diff] [blame] | 81 | void *p_cookie ) |
| 82 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 83 | conf->f_cookie_write = f_cookie_write; |
| 84 | conf->f_cookie_check = f_cookie_check; |
| 85 | conf->p_cookie = p_cookie; |
Manuel Pégourié-Gonnard | d485d19 | 2014-07-23 14:56:15 +0200 | [diff] [blame] | 86 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 87 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */ |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 88 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 89 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 90 | static int ssl_parse_servername_ext( mbedtls_ssl_context *ssl, |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 91 | const unsigned char *buf, |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 92 | size_t len ) |
| 93 | { |
| 94 | int ret; |
| 95 | size_t servername_list_size, hostname_len; |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 96 | const unsigned char *p; |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 97 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 98 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) ); |
Manuel Pégourié-Gonnard | 96ea2f2 | 2014-02-25 12:26:29 +0100 | [diff] [blame] | 99 | |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 100 | servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) ); |
| 101 | if( servername_list_size + 2 != len ) |
| 102 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 103 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 104 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 105 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 106 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | p = buf + 2; |
| 110 | while( servername_list_size > 0 ) |
| 111 | { |
| 112 | hostname_len = ( ( p[1] << 8 ) | p[2] ); |
| 113 | if( hostname_len + 3 > servername_list_size ) |
| 114 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 115 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 116 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 117 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 118 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 121 | if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME ) |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 122 | { |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 123 | ret = ssl->conf->f_sni( ssl->conf->p_sni, |
| 124 | ssl, p + 3, hostname_len ); |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 125 | if( ret != 0 ) |
| 126 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 127 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret ); |
| 128 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 129 | MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME ); |
| 130 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 131 | } |
Paul Bakker | 81420ab | 2012-10-23 10:31:15 +0000 | [diff] [blame] | 132 | return( 0 ); |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | servername_list_size -= hostname_len + 3; |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 136 | p += hostname_len + 3; |
| 137 | } |
| 138 | |
| 139 | if( servername_list_size != 0 ) |
| 140 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 141 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 142 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 143 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 144 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | return( 0 ); |
| 148 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 149 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 150 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 151 | static int ssl_parse_renegotiation_info( mbedtls_ssl_context *ssl, |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 152 | const unsigned char *buf, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 153 | size_t len ) |
| 154 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 155 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 156 | if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ) |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 157 | { |
| 158 | /* Check verify-data in constant-time. The length OTOH is no secret */ |
| 159 | if( len != 1 + ssl->verify_data_len || |
| 160 | buf[0] != ssl->verify_data_len || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 161 | mbedtls_ssl_safer_memcmp( buf + 1, ssl->peer_verify_data, |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 162 | ssl->verify_data_len ) != 0 ) |
| 163 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 164 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) ); |
Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 165 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 166 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 167 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 171 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 172 | { |
| 173 | if( len != 1 || buf[0] != 0x0 ) |
| 174 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 175 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) ); |
Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 176 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 177 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 178 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 181 | ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 182 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 183 | |
| 184 | return( 0 ); |
| 185 | } |
| 186 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 187 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 188 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 189 | |
| 190 | /* |
| 191 | * Status of the implementation of signature-algorithms extension: |
| 192 | * |
| 193 | * Currently, we are only considering the signature-algorithm extension |
| 194 | * to pick a ciphersuite which allows us to send the ServerKeyExchange |
| 195 | * message with a signature-hash combination that the user allows. |
| 196 | * |
| 197 | * We do *not* check whether all certificates in our certificate |
| 198 | * chain are signed with an allowed signature-hash pair. |
| 199 | * This needs to be done at a later stage. |
| 200 | * |
| 201 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 202 | static int ssl_parse_signature_algorithms_ext( mbedtls_ssl_context *ssl, |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 203 | const unsigned char *buf, |
| 204 | size_t len ) |
| 205 | { |
| 206 | size_t sig_alg_list_size; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 207 | |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 208 | const unsigned char *p; |
Manuel Pégourié-Gonnard | 08e81e0 | 2014-07-08 12:56:25 +0200 | [diff] [blame] | 209 | const unsigned char *end = buf + len; |
Manuel Pégourié-Gonnard | 08e81e0 | 2014-07-08 12:56:25 +0200 | [diff] [blame] | 210 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 211 | mbedtls_md_type_t md_cur; |
| 212 | mbedtls_pk_type_t sig_cur; |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 213 | |
| 214 | sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) ); |
| 215 | if( sig_alg_list_size + 2 != len || |
Manuel Pégourié-Gonnard | 08e81e0 | 2014-07-08 12:56:25 +0200 | [diff] [blame] | 216 | sig_alg_list_size % 2 != 0 ) |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 217 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 218 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 219 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 220 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 221 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 224 | /* Currently we only guarantee signing the ServerKeyExchange message according |
| 225 | * to the constraints specified in this extension (see above), so it suffices |
| 226 | * 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] | 227 | * |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 228 | * This will change when we also consider certificate signatures, |
| 229 | * in which case we will need to remember the whole signature-hash |
| 230 | * pair list from the extension. |
Manuel Pégourié-Gonnard | 08e81e0 | 2014-07-08 12:56:25 +0200 | [diff] [blame] | 231 | */ |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 232 | |
| 233 | for( p = buf + 2; p < end; p += 2 ) |
| 234 | { |
| 235 | /* Silently ignore unknown signature or hash algorithms. */ |
| 236 | |
| 237 | if( ( sig_cur = mbedtls_ssl_pk_alg_from_sig( p[1] ) ) == MBEDTLS_PK_NONE ) |
| 238 | { |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 239 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext" |
| 240 | " unknown sig alg encoding %d", p[1] ) ); |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 241 | continue; |
| 242 | } |
| 243 | |
| 244 | /* Check if we support the hash the user proposes */ |
| 245 | md_cur = mbedtls_ssl_md_alg_from_hash( p[0] ); |
| 246 | if( md_cur == MBEDTLS_MD_NONE ) |
| 247 | { |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 248 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext:" |
| 249 | " unknown hash alg encoding %d", p[0] ) ); |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 250 | continue; |
| 251 | } |
| 252 | |
| 253 | if( mbedtls_ssl_check_sig_hash( ssl, md_cur ) == 0 ) |
| 254 | { |
| 255 | 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] | 256 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext:" |
| 257 | " match sig %d and hash %d", |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 258 | sig_cur, md_cur ) ); |
| 259 | } |
| 260 | else |
| 261 | { |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 262 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: " |
| 263 | "hash alg %d not supported", md_cur ) ); |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 264 | } |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 267 | return( 0 ); |
| 268 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 269 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && |
| 270 | MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 271 | |
Robert Cragie | 136884c | 2015-10-02 13:34:31 +0100 | [diff] [blame] | 272 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 273 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 274 | static int ssl_parse_supported_elliptic_curves( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 275 | const unsigned char *buf, |
| 276 | size_t len ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 277 | { |
Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 278 | size_t list_size, our_size; |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 279 | const unsigned char *p; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 280 | const mbedtls_ecp_curve_info *curve_info, **curves; |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 281 | |
| 282 | list_size = ( ( buf[0] << 8 ) | ( buf[1] ) ); |
| 283 | if( list_size + 2 != len || |
| 284 | list_size % 2 != 0 ) |
| 285 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 286 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 287 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 288 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 289 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 290 | } |
| 291 | |
Manuel Pégourié-Gonnard | 43c3b28 | 2014-10-17 12:42:11 +0200 | [diff] [blame] | 292 | /* Should never happen unless client duplicates the extension */ |
| 293 | if( ssl->handshake->curves != NULL ) |
| 294 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 295 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 296 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 297 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 298 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 43c3b28 | 2014-10-17 12:42:11 +0200 | [diff] [blame] | 299 | } |
| 300 | |
Manuel Pégourié-Gonnard | c3f6b62c | 2014-02-06 10:13:09 +0100 | [diff] [blame] | 301 | /* Don't allow our peer to make us allocate too much memory, |
Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 302 | * and leave room for a final 0 */ |
| 303 | our_size = list_size / 2 + 1; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 304 | if( our_size > MBEDTLS_ECP_DP_MAX ) |
| 305 | our_size = MBEDTLS_ECP_DP_MAX; |
Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 306 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 307 | if( ( curves = mbedtls_calloc( our_size, sizeof( *curves ) ) ) == NULL ) |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 308 | { |
| 309 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 310 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 311 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 312 | } |
Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 313 | |
Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 314 | ssl->handshake->curves = curves; |
| 315 | |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 316 | p = buf + 2; |
Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 317 | while( list_size > 0 && our_size > 1 ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 318 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 319 | curve_info = mbedtls_ecp_curve_info_from_tls_id( ( p[0] << 8 ) | p[1] ); |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 320 | |
Manuel Pégourié-Gonnard | f24b4a7 | 2013-09-23 18:14:50 +0200 | [diff] [blame] | 321 | if( curve_info != NULL ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 322 | { |
Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 323 | *curves++ = curve_info; |
| 324 | our_size--; |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | list_size -= 2; |
| 328 | p += 2; |
| 329 | } |
| 330 | |
| 331 | return( 0 ); |
| 332 | } |
| 333 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 334 | static int ssl_parse_supported_point_formats( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 335 | const unsigned char *buf, |
| 336 | size_t len ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 337 | { |
| 338 | size_t list_size; |
| 339 | const unsigned char *p; |
| 340 | |
| 341 | list_size = buf[0]; |
| 342 | if( list_size + 1 != len ) |
| 343 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 344 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 345 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 346 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 347 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 348 | } |
| 349 | |
Manuel Pégourié-Gonnard | c1b46d0 | 2015-09-16 11:18:32 +0200 | [diff] [blame] | 350 | p = buf + 1; |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 351 | while( list_size > 0 ) |
| 352 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 353 | if( p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED || |
| 354 | p[0] == MBEDTLS_ECP_PF_COMPRESSED ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 355 | { |
Robert Cragie | 136884c | 2015-10-02 13:34:31 +0100 | [diff] [blame] | 356 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) |
Manuel Pégourié-Gonnard | 5734b2d | 2013-08-15 19:04:02 +0200 | [diff] [blame] | 357 | ssl->handshake->ecdh_ctx.point_format = p[0]; |
Robert Cragie | 136884c | 2015-10-02 13:34:31 +0100 | [diff] [blame] | 358 | #endif |
Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 359 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Robert Cragie | 136884c | 2015-10-02 13:34:31 +0100 | [diff] [blame] | 360 | ssl->handshake->ecjpake_ctx.point_format = p[0]; |
| 361 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 362 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 363 | return( 0 ); |
| 364 | } |
| 365 | |
| 366 | list_size--; |
| 367 | p++; |
| 368 | } |
| 369 | |
| 370 | return( 0 ); |
| 371 | } |
Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 372 | #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || |
| 373 | MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 374 | |
Manuel Pégourié-Gonnard | bf57be6 | 2015-09-16 15:04:01 +0200 | [diff] [blame] | 375 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 376 | static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl, |
| 377 | const unsigned char *buf, |
| 378 | size_t len ) |
| 379 | { |
| 380 | int ret; |
| 381 | |
| 382 | if( mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 ) |
| 383 | { |
| 384 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) ); |
| 385 | return( 0 ); |
| 386 | } |
| 387 | |
| 388 | if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx, |
| 389 | buf, len ) ) != 0 ) |
| 390 | { |
| 391 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 392 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 393 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Manuel Pégourié-Gonnard | bf57be6 | 2015-09-16 15:04:01 +0200 | [diff] [blame] | 394 | return( ret ); |
| 395 | } |
| 396 | |
| 397 | /* Only mark the extension as OK when we're sure it is */ |
| 398 | ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK; |
| 399 | |
| 400 | return( 0 ); |
| 401 | } |
| 402 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
| 403 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 404 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 405 | 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] | 406 | const unsigned char *buf, |
| 407 | size_t len ) |
| 408 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 409 | 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] | 410 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 411 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 412 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 413 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 414 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 48f8d0d | 2013-07-17 10:25:37 +0200 | [diff] [blame] | 415 | } |
| 416 | |
Manuel Pégourié-Gonnard | ed4af8b | 2013-07-18 14:07:09 +0200 | [diff] [blame] | 417 | ssl->session_negotiate->mfl_code = buf[0]; |
| 418 | |
Manuel Pégourié-Gonnard | 48f8d0d | 2013-07-17 10:25:37 +0200 | [diff] [blame] | 419 | return( 0 ); |
| 420 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 421 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 48f8d0d | 2013-07-17 10:25:37 +0200 | [diff] [blame] | 422 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 423 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 424 | 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] | 425 | const unsigned char *buf, |
| 426 | size_t len ) |
| 427 | { |
| 428 | if( len != 0 ) |
| 429 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 430 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 431 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 432 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 433 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | ((void) buf); |
| 437 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 438 | if( ssl->conf->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 439 | ssl->session_negotiate->trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 440 | |
| 441 | return( 0 ); |
| 442 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 443 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 444 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 445 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 446 | 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] | 447 | const unsigned char *buf, |
| 448 | size_t len ) |
| 449 | { |
| 450 | if( len != 0 ) |
| 451 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 452 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 453 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 454 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 455 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | ((void) buf); |
| 459 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 460 | if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 461 | ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 462 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 463 | ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | return( 0 ); |
| 467 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 468 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 469 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 470 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 471 | 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] | 472 | const unsigned char *buf, |
| 473 | size_t len ) |
| 474 | { |
| 475 | if( len != 0 ) |
| 476 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 477 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 478 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 479 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 480 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | ((void) buf); |
| 484 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 485 | if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 486 | ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | b575b54 | 2014-10-24 15:12:31 +0200 | [diff] [blame] | 487 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 488 | ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; |
Manuel Pégourié-Gonnard | b575b54 | 2014-10-24 15:12:31 +0200 | [diff] [blame] | 489 | } |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 490 | |
| 491 | return( 0 ); |
| 492 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 493 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 494 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 495 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 496 | 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] | 497 | unsigned char *buf, |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 498 | size_t len ) |
| 499 | { |
Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 500 | int ret; |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 501 | mbedtls_ssl_session session; |
Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 502 | |
Manuel Pégourié-Gonnard | bae389b | 2015-06-24 10:45:58 +0200 | [diff] [blame] | 503 | mbedtls_ssl_session_init( &session ); |
| 504 | |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 505 | if( ssl->conf->f_ticket_parse == NULL || |
| 506 | ssl->conf->f_ticket_write == NULL ) |
| 507 | { |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 508 | return( 0 ); |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 509 | } |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 510 | |
Manuel Pégourié-Gonnard | 306827e | 2013-08-02 18:05:14 +0200 | [diff] [blame] | 511 | /* Remember the client asked us to send a new ticket */ |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 512 | ssl->handshake->new_session_ticket = 1; |
| 513 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 514 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) ); |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 515 | |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 516 | if( len == 0 ) |
| 517 | return( 0 ); |
| 518 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 519 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 520 | if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ) |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 521 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 522 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) ); |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 523 | return( 0 ); |
| 524 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 525 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 526 | |
| 527 | /* |
Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 528 | * Failures are ok: just ignore the ticket and proceed. |
| 529 | */ |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 530 | if( ( ret = ssl->conf->f_ticket_parse( ssl->conf->p_ticket, &session, |
| 531 | buf, len ) ) != 0 ) |
Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 532 | { |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 533 | mbedtls_ssl_session_free( &session ); |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 534 | |
| 535 | if( ret == MBEDTLS_ERR_SSL_INVALID_MAC ) |
| 536 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket is not authentic" ) ); |
| 537 | else if( ret == MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED ) |
| 538 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket is expired" ) ); |
| 539 | else |
| 540 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_ticket_parse", ret ); |
| 541 | |
Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 542 | return( 0 ); |
Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 543 | } |
Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 544 | |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 545 | /* |
| 546 | * Keep the session ID sent by the client, since we MUST send it back to |
| 547 | * inform them we're accepting the ticket (RFC 5077 section 3.4) |
| 548 | */ |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 549 | session.id_len = ssl->session_negotiate->id_len; |
| 550 | memcpy( &session.id, ssl->session_negotiate->id, session.id_len ); |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 551 | |
| 552 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
| 553 | memcpy( ssl->session_negotiate, &session, sizeof( mbedtls_ssl_session ) ); |
| 554 | |
| 555 | /* Zeroize instead of free as we copied the content */ |
| 556 | mbedtls_zeroize( &session, sizeof( mbedtls_ssl_session ) ); |
| 557 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 558 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) ); |
Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 559 | |
Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 560 | ssl->handshake->resume = 1; |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 561 | |
Manuel Pégourié-Gonnard | 306827e | 2013-08-02 18:05:14 +0200 | [diff] [blame] | 562 | /* Don't send a new ticket after all, this one is OK */ |
| 563 | ssl->handshake->new_session_ticket = 0; |
| 564 | |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 565 | return( 0 ); |
| 566 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 567 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 568 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 569 | #if defined(MBEDTLS_SSL_ALPN) |
| 570 | static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 14beb08 | 2014-07-08 13:50:35 +0200 | [diff] [blame] | 571 | const unsigned char *buf, size_t len ) |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 572 | { |
Paul Bakker | 14b16c6 | 2014-05-28 11:33:54 +0200 | [diff] [blame] | 573 | size_t list_len, cur_len, ours_len; |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 574 | const unsigned char *theirs, *start, *end; |
| 575 | const char **ours; |
| 576 | |
| 577 | /* If ALPN not configured, just ignore the extension */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 578 | if( ssl->conf->alpn_list == NULL ) |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 579 | return( 0 ); |
| 580 | |
| 581 | /* |
| 582 | * opaque ProtocolName<1..2^8-1>; |
| 583 | * |
| 584 | * struct { |
| 585 | * ProtocolName protocol_name_list<2..2^16-1> |
| 586 | * } ProtocolNameList; |
| 587 | */ |
| 588 | |
| 589 | /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */ |
| 590 | if( len < 4 ) |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 591 | { |
| 592 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 593 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 594 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 595 | } |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 596 | |
| 597 | list_len = ( buf[0] << 8 ) | buf[1]; |
| 598 | if( list_len != len - 2 ) |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 599 | { |
| 600 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 601 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 602 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 603 | } |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 604 | |
| 605 | /* |
Manuel Pégourié-Gonnard | 239987f | 2018-01-09 10:43:43 +0100 | [diff] [blame] | 606 | * Validate peer's list (lengths) |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 607 | */ |
| 608 | start = buf + 2; |
| 609 | end = buf + len; |
Manuel Pégourié-Gonnard | 239987f | 2018-01-09 10:43:43 +0100 | [diff] [blame] | 610 | for( theirs = start; theirs != end; theirs += cur_len ) |
| 611 | { |
| 612 | cur_len = *theirs++; |
| 613 | |
| 614 | /* Current identifier must fit in list */ |
| 615 | if( cur_len > (size_t)( end - theirs ) ) |
| 616 | { |
| 617 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 618 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 619 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 620 | } |
| 621 | |
| 622 | /* Empty strings MUST NOT be included */ |
| 623 | if( cur_len == 0 ) |
| 624 | { |
| 625 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 626 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
| 627 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | /* |
| 632 | * Use our order of preference |
| 633 | */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 634 | for( ours = ssl->conf->alpn_list; *ours != NULL; ours++ ) |
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 | ours_len = strlen( *ours ); |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 637 | for( theirs = start; theirs != end; theirs += cur_len ) |
| 638 | { |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 639 | cur_len = *theirs++; |
| 640 | |
Paul Bakker | 14b16c6 | 2014-05-28 11:33:54 +0200 | [diff] [blame] | 641 | if( cur_len == ours_len && |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 642 | memcmp( theirs, *ours, cur_len ) == 0 ) |
| 643 | { |
| 644 | ssl->alpn_chosen = *ours; |
| 645 | return( 0 ); |
| 646 | } |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | /* If we get there, no match was found */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 651 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 652 | MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL ); |
| 653 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 654 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 655 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 656 | |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 657 | /* |
| 658 | * Auxiliary functions for ServerHello parsing and related actions |
| 659 | */ |
| 660 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 661 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 662 | /* |
Manuel Pégourié-Gonnard | 6458e3b | 2015-01-08 14:16:56 +0100 | [diff] [blame] | 663 | * 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] | 664 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 665 | #if defined(MBEDTLS_ECDSA_C) |
| 666 | static int ssl_check_key_curve( mbedtls_pk_context *pk, |
| 667 | const mbedtls_ecp_curve_info **curves ) |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 668 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 669 | const mbedtls_ecp_curve_info **crv = curves; |
| 670 | 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] | 671 | |
| 672 | while( *crv != NULL ) |
| 673 | { |
| 674 | if( (*crv)->grp_id == grp_id ) |
Manuel Pégourié-Gonnard | 6458e3b | 2015-01-08 14:16:56 +0100 | [diff] [blame] | 675 | return( 0 ); |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 676 | crv++; |
| 677 | } |
| 678 | |
Manuel Pégourié-Gonnard | 6458e3b | 2015-01-08 14:16:56 +0100 | [diff] [blame] | 679 | return( -1 ); |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 680 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 681 | #endif /* MBEDTLS_ECDSA_C */ |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 682 | |
| 683 | /* |
| 684 | * Try picking a certificate for this ciphersuite, |
| 685 | * return 0 on success and -1 on failure. |
| 686 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 687 | static int ssl_pick_cert( mbedtls_ssl_context *ssl, |
| 688 | const mbedtls_ssl_ciphersuite_t * ciphersuite_info ) |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 689 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 690 | mbedtls_ssl_key_cert *cur, *list, *fallback = NULL; |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 691 | mbedtls_pk_type_t pk_alg = |
| 692 | mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ); |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 693 | uint32_t flags; |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 694 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 695 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 696 | if( ssl->handshake->sni_key_cert != NULL ) |
| 697 | list = ssl->handshake->sni_key_cert; |
| 698 | else |
| 699 | #endif |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 700 | list = ssl->conf->key_cert; |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 701 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 702 | if( pk_alg == MBEDTLS_PK_NONE ) |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 703 | return( 0 ); |
| 704 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 705 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite requires certificate" ) ); |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 706 | |
Manuel Pégourié-Gonnard | e540b49 | 2015-07-07 12:44:38 +0200 | [diff] [blame] | 707 | if( list == NULL ) |
| 708 | { |
| 709 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server has no certificate" ) ); |
| 710 | return( -1 ); |
| 711 | } |
| 712 | |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 713 | for( cur = list; cur != NULL; cur = cur->next ) |
| 714 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 715 | MBEDTLS_SSL_DEBUG_CRT( 3, "candidate certificate chain, certificate", |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 716 | cur->cert ); |
| 717 | |
Gilles Peskine | e198df5 | 2018-01-05 21:17:45 +0100 | [diff] [blame] | 718 | if( ! mbedtls_pk_can_do( &cur->cert->pk, pk_alg ) ) |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 719 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 720 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: key type" ) ); |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 721 | continue; |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 722 | } |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 723 | |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 724 | /* |
| 725 | * This avoids sending the client a cert it'll reject based on |
| 726 | * keyUsage or other extensions. |
| 727 | * |
| 728 | * It also allows the user to provision different certificates for |
| 729 | * different uses based on keyUsage, eg if they want to avoid signing |
| 730 | * and decrypting with the same RSA key. |
| 731 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 732 | if( mbedtls_ssl_check_cert_usage( cur->cert, ciphersuite_info, |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 733 | MBEDTLS_SSL_IS_SERVER, &flags ) != 0 ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 734 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 735 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: " |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 736 | "(extended) key usage extension" ) ); |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 737 | continue; |
| 738 | } |
| 739 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 740 | #if defined(MBEDTLS_ECDSA_C) |
| 741 | if( pk_alg == MBEDTLS_PK_ECDSA && |
Gilles Peskine | 81d4e89 | 2017-10-27 10:18:44 +0200 | [diff] [blame] | 742 | ssl_check_key_curve( &cur->cert->pk, ssl->handshake->curves ) != 0 ) |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 743 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 744 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: elliptic curve" ) ); |
Manuel Pégourié-Gonnard | 846ba47 | 2015-01-08 13:54:38 +0100 | [diff] [blame] | 745 | continue; |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 746 | } |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 747 | #endif |
Manuel Pégourié-Gonnard | 846ba47 | 2015-01-08 13:54:38 +0100 | [diff] [blame] | 748 | |
Manuel Pégourié-Gonnard | df331a5 | 2015-01-08 16:43:07 +0100 | [diff] [blame] | 749 | /* |
| 750 | * Try to select a SHA-1 certificate for pre-1.2 clients, but still |
| 751 | * present them a SHA-higher cert rather than failing if it's the only |
| 752 | * one we got that satisfies the other conditions. |
| 753 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 754 | if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 && |
| 755 | cur->cert->sig_md != MBEDTLS_MD_SHA1 ) |
Manuel Pégourié-Gonnard | df331a5 | 2015-01-08 16:43:07 +0100 | [diff] [blame] | 756 | { |
| 757 | if( fallback == NULL ) |
| 758 | fallback = cur; |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 759 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 760 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate not preferred: " |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 761 | "sha-2 with pre-TLS 1.2 client" ) ); |
Manuel Pégourié-Gonnard | df331a5 | 2015-01-08 16:43:07 +0100 | [diff] [blame] | 762 | continue; |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 763 | } |
Manuel Pégourié-Gonnard | df331a5 | 2015-01-08 16:43:07 +0100 | [diff] [blame] | 764 | } |
| 765 | |
Manuel Pégourié-Gonnard | 846ba47 | 2015-01-08 13:54:38 +0100 | [diff] [blame] | 766 | /* If we get there, we got a winner */ |
| 767 | break; |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 768 | } |
| 769 | |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 770 | if( cur == NULL ) |
| 771 | cur = fallback; |
| 772 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 773 | /* 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] | 774 | if( cur != NULL ) |
| 775 | { |
| 776 | ssl->handshake->key_cert = cur; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 777 | MBEDTLS_SSL_DEBUG_CRT( 3, "selected certificate chain, certificate", |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 778 | ssl->handshake->key_cert->cert ); |
Manuel Pégourié-Gonnard | df331a5 | 2015-01-08 16:43:07 +0100 | [diff] [blame] | 779 | return( 0 ); |
| 780 | } |
| 781 | |
| 782 | return( -1 ); |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 783 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 784 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 785 | |
| 786 | /* |
| 787 | * Check if a given ciphersuite is suitable for use with our config/keys/etc |
| 788 | * Sets ciphersuite_info only if the suite matches. |
| 789 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 790 | static int ssl_ciphersuite_match( mbedtls_ssl_context *ssl, int suite_id, |
| 791 | const mbedtls_ssl_ciphersuite_t **ciphersuite_info ) |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 792 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 793 | const mbedtls_ssl_ciphersuite_t *suite_info; |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 794 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 795 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
Hanno Becker | 4cb1f4d | 2017-10-10 15:59:57 +0100 | [diff] [blame] | 796 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 797 | mbedtls_pk_type_t sig_type; |
| 798 | #endif |
| 799 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 800 | suite_info = mbedtls_ssl_ciphersuite_from_id( suite_id ); |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 801 | if( suite_info == NULL ) |
| 802 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 803 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 804 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 805 | } |
| 806 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 807 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "trying ciphersuite: %s", suite_info->name ) ); |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 808 | |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 809 | if( suite_info->min_minor_ver > ssl->minor_ver || |
| 810 | suite_info->max_minor_ver < ssl->minor_ver ) |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 811 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 812 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: version" ) ); |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 813 | return( 0 ); |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 814 | } |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 815 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 816 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 817 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 818 | ( suite_info->flags & MBEDTLS_CIPHERSUITE_NODTLS ) ) |
Manuel Pégourié-Gonnard | d666451 | 2014-02-06 13:26:57 +0100 | [diff] [blame] | 819 | return( 0 ); |
| 820 | #endif |
| 821 | |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 822 | #if defined(MBEDTLS_ARC4_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 823 | if( ssl->conf->arc4_disabled == MBEDTLS_SSL_ARC4_DISABLED && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 824 | suite_info->cipher == MBEDTLS_CIPHER_ARC4_128 ) |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 825 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 826 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: rc4" ) ); |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 827 | return( 0 ); |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 828 | } |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 829 | #endif |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 830 | |
Manuel Pégourié-Gonnard | e511b4e | 2015-09-16 14:11:09 +0200 | [diff] [blame] | 831 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 832 | if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE && |
Manuel Pégourié-Gonnard | bf57be6 | 2015-09-16 15:04:01 +0200 | [diff] [blame] | 833 | ( 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] | 834 | { |
Manuel Pégourié-Gonnard | bf57be6 | 2015-09-16 15:04:01 +0200 | [diff] [blame] | 835 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: ecjpake " |
| 836 | "not configured or ext missing" ) ); |
Manuel Pégourié-Gonnard | e511b4e | 2015-09-16 14:11:09 +0200 | [diff] [blame] | 837 | return( 0 ); |
| 838 | } |
| 839 | #endif |
| 840 | |
| 841 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 842 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) |
| 843 | if( mbedtls_ssl_ciphersuite_uses_ec( suite_info ) && |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 844 | ( ssl->handshake->curves == NULL || |
| 845 | ssl->handshake->curves[0] == NULL ) ) |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 846 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 847 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: " |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 848 | "no common elliptic curve" ) ); |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 849 | return( 0 ); |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 850 | } |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 851 | #endif |
| 852 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 853 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 854 | /* If the ciphersuite requires a pre-shared key and we don't |
| 855 | * have one, skip it now rather than failing later */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 856 | if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) && |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 857 | ssl->conf->f_psk == NULL && |
| 858 | ( ssl->conf->psk == NULL || ssl->conf->psk_identity == NULL || |
| 859 | 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] | 860 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 861 | 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] | 862 | return( 0 ); |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 863 | } |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 864 | #endif |
| 865 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 866 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 867 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
| 868 | /* If the ciphersuite requires signing, check whether |
| 869 | * a suitable hash algorithm is present. */ |
| 870 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
| 871 | { |
| 872 | sig_type = mbedtls_ssl_get_ciphersuite_sig_alg( suite_info ); |
| 873 | if( sig_type != MBEDTLS_PK_NONE && |
| 874 | mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs, sig_type ) == MBEDTLS_MD_NONE ) |
| 875 | { |
| 876 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no suitable hash algorithm " |
| 877 | "for signature algorithm %d", sig_type ) ); |
| 878 | return( 0 ); |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && |
| 883 | MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
| 884 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 885 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 886 | /* |
| 887 | * Final check: if ciphersuite requires us to have a |
| 888 | * certificate/key of a particular type: |
| 889 | * - select the appropriate certificate if we have one, or |
| 890 | * - try the next ciphersuite if we don't |
| 891 | * This must be done last since we modify the key_cert list. |
| 892 | */ |
| 893 | if( ssl_pick_cert( ssl, suite_info ) != 0 ) |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 894 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 895 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: " |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 896 | "no suitable certificate" ) ); |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 897 | return( 0 ); |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 898 | } |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 899 | #endif |
| 900 | |
| 901 | *ciphersuite_info = suite_info; |
| 902 | return( 0 ); |
| 903 | } |
| 904 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 905 | #if defined(MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO) |
| 906 | static int ssl_parse_client_hello_v2( mbedtls_ssl_context *ssl ) |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 907 | { |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 908 | int ret, got_common_suite; |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 909 | unsigned int i, j; |
| 910 | size_t n; |
| 911 | unsigned int ciph_len, sess_len, chal_len; |
| 912 | unsigned char *buf, *p; |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 913 | const int *ciphersuites; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 914 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 915 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 916 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 917 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 918 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 919 | if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ) |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 920 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 921 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) ); |
Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 922 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 923 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 924 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 925 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 926 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 927 | |
| 928 | buf = ssl->in_hdr; |
| 929 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 930 | MBEDTLS_SSL_DEBUG_BUF( 4, "record header", buf, 5 ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 931 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 932 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d", |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 933 | buf[2] ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 934 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d", |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 935 | ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 936 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]", |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 937 | buf[3], buf[4] ) ); |
| 938 | |
| 939 | /* |
| 940 | * SSLv2 Client Hello |
| 941 | * |
| 942 | * Record layer: |
| 943 | * 0 . 1 message length |
| 944 | * |
| 945 | * SSL layer: |
| 946 | * 2 . 2 message type |
| 947 | * 3 . 4 protocol version |
| 948 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 949 | if( buf[2] != MBEDTLS_SSL_HS_CLIENT_HELLO || |
| 950 | buf[3] != MBEDTLS_SSL_MAJOR_VERSION_3 ) |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 951 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 952 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 953 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF; |
| 957 | |
| 958 | if( n < 17 || n > 512 ) |
| 959 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 960 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 961 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 962 | } |
| 963 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 964 | ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3; |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 965 | ssl->minor_ver = ( buf[4] <= ssl->conf->max_minor_ver ) |
| 966 | ? buf[4] : ssl->conf->max_minor_ver; |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 967 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 968 | if( ssl->minor_ver < ssl->conf->min_minor_ver ) |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 969 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 970 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum" |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 971 | " [%d:%d] < [%d:%d]", |
| 972 | ssl->major_ver, ssl->minor_ver, |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 973 | ssl->conf->min_major_ver, ssl->conf->min_minor_ver ) ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 974 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 975 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 976 | MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION ); |
| 977 | return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 978 | } |
| 979 | |
Paul Bakker | 2fbefde | 2013-06-29 16:01:15 +0200 | [diff] [blame] | 980 | ssl->handshake->max_major_ver = buf[3]; |
| 981 | ssl->handshake->max_minor_ver = buf[4]; |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 982 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 983 | if( ( ret = mbedtls_ssl_fetch_input( ssl, 2 + n ) ) != 0 ) |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 984 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 985 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 986 | return( ret ); |
| 987 | } |
| 988 | |
| 989 | ssl->handshake->update_checksum( ssl, buf + 2, n ); |
| 990 | |
| 991 | buf = ssl->in_msg; |
| 992 | n = ssl->in_left - 5; |
| 993 | |
| 994 | /* |
| 995 | * 0 . 1 ciphersuitelist length |
| 996 | * 2 . 3 session id length |
| 997 | * 4 . 5 challenge length |
| 998 | * 6 . .. ciphersuitelist |
| 999 | * .. . .. session id |
| 1000 | * .. . .. challenge |
| 1001 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1002 | MBEDTLS_SSL_DEBUG_BUF( 4, "record contents", buf, n ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1003 | |
| 1004 | ciph_len = ( buf[0] << 8 ) | buf[1]; |
| 1005 | sess_len = ( buf[2] << 8 ) | buf[3]; |
| 1006 | chal_len = ( buf[4] << 8 ) | buf[5]; |
| 1007 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1008 | 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] | 1009 | ciph_len, sess_len, chal_len ) ); |
| 1010 | |
| 1011 | /* |
| 1012 | * Make sure each parameter length is valid |
| 1013 | */ |
| 1014 | if( ciph_len < 3 || ( ciph_len % 3 ) != 0 ) |
| 1015 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1016 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1017 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | if( sess_len > 32 ) |
| 1021 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1022 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1023 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | if( chal_len < 8 || chal_len > 32 ) |
| 1027 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1028 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1029 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | if( n != 6 + ciph_len + sess_len + chal_len ) |
| 1033 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1034 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1035 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1036 | } |
| 1037 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1038 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist", |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1039 | buf + 6, ciph_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1040 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id", |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1041 | buf + 6 + ciph_len, sess_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1042 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, challenge", |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1043 | buf + 6 + ciph_len + sess_len, chal_len ); |
| 1044 | |
| 1045 | p = buf + 6 + ciph_len; |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 1046 | ssl->session_negotiate->id_len = sess_len; |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 1047 | memset( ssl->session_negotiate->id, 0, |
| 1048 | sizeof( ssl->session_negotiate->id ) ); |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 1049 | memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->id_len ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1050 | |
| 1051 | p += sess_len; |
| 1052 | memset( ssl->handshake->randbytes, 0, 64 ); |
| 1053 | memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len ); |
| 1054 | |
| 1055 | /* |
| 1056 | * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV |
| 1057 | */ |
| 1058 | for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 ) |
| 1059 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1060 | 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] | 1061 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1062 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) ); |
| 1063 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1064 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
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 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV " |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1067 | "during renegotiation" ) ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1068 | |
Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 1069 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1070 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1071 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1072 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1073 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
| 1074 | ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION; |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1075 | break; |
| 1076 | } |
| 1077 | } |
| 1078 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1079 | #if defined(MBEDTLS_SSL_FALLBACK_SCSV) |
Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1080 | for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 ) |
| 1081 | { |
| 1082 | if( p[0] == 0 && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1083 | p[1] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE >> 8 ) & 0xff ) && |
| 1084 | p[2] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE ) & 0xff ) ) |
Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1085 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1086 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "received FALLBACK_SCSV" ) ); |
Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1087 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1088 | if( ssl->minor_ver < ssl->conf->max_minor_ver ) |
Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1089 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1090 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) ); |
Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1091 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1092 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1093 | MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK ); |
Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1094 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1095 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1096 | } |
| 1097 | |
| 1098 | break; |
| 1099 | } |
| 1100 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1101 | #endif /* MBEDTLS_SSL_FALLBACK_SCSV */ |
Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1102 | |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1103 | got_common_suite = 0; |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1104 | ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver]; |
Manuel Pégourié-Gonnard | 011a8db | 2013-11-30 18:11:07 +0100 | [diff] [blame] | 1105 | ciphersuite_info = NULL; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1106 | #if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE) |
Manuel Pégourié-Gonnard | 1a9f2c7 | 2013-11-30 18:30:06 +0100 | [diff] [blame] | 1107 | for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 ) |
Manuel Pégourié-Gonnard | 1a9f2c7 | 2013-11-30 18:30:06 +0100 | [diff] [blame] | 1108 | for( i = 0; ciphersuites[i] != 0; i++ ) |
| 1109 | #else |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 1110 | for( i = 0; ciphersuites[i] != 0; i++ ) |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1111 | for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 ) |
Manuel Pégourié-Gonnard | 1a9f2c7 | 2013-11-30 18:30:06 +0100 | [diff] [blame] | 1112 | #endif |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1113 | { |
Manuel Pégourié-Gonnard | 011a8db | 2013-11-30 18:11:07 +0100 | [diff] [blame] | 1114 | if( p[0] != 0 || |
| 1115 | p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) || |
| 1116 | p[2] != ( ( ciphersuites[i] ) & 0xFF ) ) |
| 1117 | continue; |
Paul Bakker | 59c28a2 | 2013-06-29 15:33:42 +0200 | [diff] [blame] | 1118 | |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1119 | got_common_suite = 1; |
| 1120 | |
Manuel Pégourié-Gonnard | 011a8db | 2013-11-30 18:11:07 +0100 | [diff] [blame] | 1121 | if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i], |
| 1122 | &ciphersuite_info ) ) != 0 ) |
| 1123 | return( ret ); |
Paul Bakker | 59c28a2 | 2013-06-29 15:33:42 +0200 | [diff] [blame] | 1124 | |
Manuel Pégourié-Gonnard | 011a8db | 2013-11-30 18:11:07 +0100 | [diff] [blame] | 1125 | if( ciphersuite_info != NULL ) |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1126 | goto have_ciphersuite_v2; |
| 1127 | } |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1128 | |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1129 | if( got_common_suite ) |
| 1130 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1131 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, " |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1132 | "but none of them usable" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1133 | return( MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE ); |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1134 | } |
| 1135 | else |
| 1136 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1137 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) ); |
| 1138 | return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN ); |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1139 | } |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1140 | |
| 1141 | have_ciphersuite_v2: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1142 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) ); |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 1143 | |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 1144 | ssl->session_negotiate->ciphersuite = ciphersuites[i]; |
Paul Bakker | 59c28a2 | 2013-06-29 15:33:42 +0200 | [diff] [blame] | 1145 | ssl->transform_negotiate->ciphersuite_info = ciphersuite_info; |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1146 | |
| 1147 | /* |
| 1148 | * SSLv2 Client Hello relevant renegotiation security checks |
| 1149 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1150 | if( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1151 | ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE ) |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1152 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1153 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) ); |
Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 1154 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1155 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1156 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1157 | } |
| 1158 | |
| 1159 | ssl->in_left = 0; |
| 1160 | ssl->state++; |
| 1161 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1162 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1163 | |
| 1164 | return( 0 ); |
| 1165 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1166 | #endif /* MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */ |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1167 | |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1168 | /* This function doesn't alert on errors that happen early during |
| 1169 | ClientHello parsing because they might indicate that the client is |
| 1170 | 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] | 1171 | static int ssl_parse_client_hello( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1172 | { |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1173 | int ret, got_common_suite; |
Manuel Pégourié-Gonnard | 9de64f5 | 2015-07-01 15:51:43 +0200 | [diff] [blame] | 1174 | size_t i, j; |
| 1175 | size_t ciph_offset, comp_offset, ext_offset; |
| 1176 | 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] | 1177 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 9de64f5 | 2015-07-01 15:51:43 +0200 | [diff] [blame] | 1178 | size_t cookie_offset, cookie_len; |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1179 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1180 | unsigned char *buf, *p, *ext; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1181 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1182 | int renegotiation_info_seen = 0; |
Manuel Pégourié-Gonnard | eaecbd3 | 2014-11-06 02:38:02 +0100 | [diff] [blame] | 1183 | #endif |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1184 | int handshake_failure = 0; |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 1185 | const int *ciphersuites; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1186 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 1187 | int major, minor; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1188 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 1189 | /* If there is no signature-algorithm extension present, |
| 1190 | * we need to fall back to the default values for allowed |
| 1191 | * signature-hash pairs. */ |
| 1192 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 1193 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
| 1194 | int sig_hash_alg_ext_present = 0; |
| 1195 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && |
| 1196 | MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
| 1197 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1198 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1199 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1200 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | f03c7aa | 2014-09-24 14:54:06 +0200 | [diff] [blame] | 1201 | read_record_header: |
| 1202 | #endif |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1203 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1204 | * 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] | 1205 | * otherwise read it ourselves manually in order to support SSLv2 |
| 1206 | * ClientHello, which doesn't use the same record layer format. |
| 1207 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1208 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1209 | if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE ) |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1210 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1211 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1212 | if( ( ret = mbedtls_ssl_fetch_input( ssl, 5 ) ) != 0 ) |
Manuel Pégourié-Gonnard | 59c6f2e | 2015-01-22 11:06:40 +0000 | [diff] [blame] | 1213 | { |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1214 | /* No alert on a read error. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1215 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret ); |
Manuel Pégourié-Gonnard | 59c6f2e | 2015-01-22 11:06:40 +0000 | [diff] [blame] | 1216 | return( ret ); |
| 1217 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1218 | } |
| 1219 | |
| 1220 | buf = ssl->in_hdr; |
| 1221 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1222 | #if defined(MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO) |
| 1223 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1224 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM ) |
Manuel Pégourié-Gonnard | 8a7cf25 | 2014-10-09 17:35:53 +0200 | [diff] [blame] | 1225 | #endif |
| 1226 | if( ( buf[0] & 0x80 ) != 0 ) |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 1227 | return( ssl_parse_client_hello_v2( ssl ) ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1228 | #endif |
| 1229 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1230 | MBEDTLS_SSL_DEBUG_BUF( 4, "record header", buf, mbedtls_ssl_hdr_len( ssl ) ); |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1231 | |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1232 | /* |
Manuel Pégourié-Gonnard | 6b1e207 | 2014-02-12 10:14:54 +0100 | [diff] [blame] | 1233 | * SSLv3/TLS Client Hello |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1234 | * |
| 1235 | * Record layer: |
| 1236 | * 0 . 0 message type |
| 1237 | * 1 . 2 protocol version |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1238 | * 3 . 11 DTLS: epoch + record sequence number |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1239 | * 3 . 4 message length |
| 1240 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1241 | 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] | 1242 | buf[0] ) ); |
| 1243 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1244 | if( buf[0] != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 1245 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1246 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1247 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 1248 | } |
| 1249 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1250 | 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] | 1251 | ( ssl->in_len[0] << 8 ) | ssl->in_len[1] ) ); |
| 1252 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1253 | 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] | 1254 | buf[1], buf[2] ) ); |
| 1255 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1256 | 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] | 1257 | |
| 1258 | /* According to RFC 5246 Appendix E.1, the version here is typically |
| 1259 | * "{03,00}, the lowest version number supported by the client, [or] the |
| 1260 | * value of ClientHello.client_version", so the only meaningful check here |
| 1261 | * is the major version shouldn't be less than 3 */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1262 | if( major < MBEDTLS_SSL_MAJOR_VERSION_3 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1263 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1264 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1265 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1266 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1267 | |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1268 | /* For DTLS if this is the initial handshake, remember the client sequence |
| 1269 | * 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] | 1270 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1271 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1272 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1273 | && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE |
Manuel Pégourié-Gonnard | 3a173f4 | 2015-01-22 13:30:33 +0000 | [diff] [blame] | 1274 | #endif |
| 1275 | ) |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1276 | { |
| 1277 | /* Epoch should be 0 for initial handshakes */ |
| 1278 | if( ssl->in_ctr[0] != 0 || ssl->in_ctr[1] != 0 ) |
| 1279 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1280 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1281 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1282 | } |
| 1283 | |
| 1284 | memcpy( ssl->out_ctr + 2, ssl->in_ctr + 2, 6 ); |
Manuel Pégourié-Gonnard | f03c7aa | 2014-09-24 14:54:06 +0200 | [diff] [blame] | 1285 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1286 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 1287 | if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 ) |
Manuel Pégourié-Gonnard | f03c7aa | 2014-09-24 14:54:06 +0200 | [diff] [blame] | 1288 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1289 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record, discarding" ) ); |
Manuel Pégourié-Gonnard | f03c7aa | 2014-09-24 14:54:06 +0200 | [diff] [blame] | 1290 | ssl->next_record_offset = 0; |
| 1291 | ssl->in_left = 0; |
| 1292 | goto read_record_header; |
| 1293 | } |
| 1294 | |
| 1295 | /* 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] | 1296 | mbedtls_ssl_dtls_replay_update( ssl ); |
Manuel Pégourié-Gonnard | f03c7aa | 2014-09-24 14:54:06 +0200 | [diff] [blame] | 1297 | #endif |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1298 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1299 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1300 | |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1301 | msg_len = ( ssl->in_len[0] << 8 ) | ssl->in_len[1]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1302 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1303 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1304 | if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ) |
Manuel Pégourié-Gonnard | b89c4f3 | 2015-01-21 13:24:10 +0000 | [diff] [blame] | 1305 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1306 | /* Set by mbedtls_ssl_read_record() */ |
Manuel Pégourié-Gonnard | b89c4f3 | 2015-01-21 13:24:10 +0000 | [diff] [blame] | 1307 | msg_len = ssl->in_hslen; |
| 1308 | } |
| 1309 | else |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1310 | #endif |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1311 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1312 | if( msg_len > MBEDTLS_SSL_MAX_CONTENT_LEN ) |
Manuel Pégourié-Gonnard | d6b721c | 2014-03-24 12:13:54 +0100 | [diff] [blame] | 1313 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1314 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1315 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | d6b721c | 2014-03-24 12:13:54 +0100 | [diff] [blame] | 1316 | } |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1317 | |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 1318 | if( ( ret = mbedtls_ssl_fetch_input( ssl, |
| 1319 | mbedtls_ssl_hdr_len( ssl ) + msg_len ) ) != 0 ) |
Manuel Pégourié-Gonnard | d6b721c | 2014-03-24 12:13:54 +0100 | [diff] [blame] | 1320 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1321 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret ); |
Manuel Pégourié-Gonnard | d6b721c | 2014-03-24 12:13:54 +0100 | [diff] [blame] | 1322 | return( ret ); |
| 1323 | } |
Manuel Pégourié-Gonnard | 30d16eb | 2014-08-19 17:43:50 +0200 | [diff] [blame] | 1324 | |
| 1325 | /* Done reading this record, get ready for the next one */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1326 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1327 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1328 | ssl->next_record_offset = msg_len + mbedtls_ssl_hdr_len( ssl ); |
Manuel Pégourié-Gonnard | 30d16eb | 2014-08-19 17:43:50 +0200 | [diff] [blame] | 1329 | else |
| 1330 | #endif |
| 1331 | ssl->in_left = 0; |
Manuel Pégourié-Gonnard | d6b721c | 2014-03-24 12:13:54 +0100 | [diff] [blame] | 1332 | } |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1333 | |
| 1334 | buf = ssl->in_msg; |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1335 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1336 | MBEDTLS_SSL_DEBUG_BUF( 4, "record contents", buf, msg_len ); |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 1337 | |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1338 | ssl->handshake->update_checksum( ssl, buf, msg_len ); |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1339 | |
| 1340 | /* |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1341 | * Handshake layer: |
| 1342 | * 0 . 0 handshake type |
| 1343 | * 1 . 3 handshake length |
| 1344 | * 4 . 5 DTLS only: message seqence number |
| 1345 | * 6 . 8 DTLS only: fragment offset |
| 1346 | * 9 . 11 DTLS only: fragment length |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 1347 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1348 | if( msg_len < mbedtls_ssl_hs_hdr_len( ssl ) ) |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1349 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1350 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1351 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1352 | } |
| 1353 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1354 | 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] | 1355 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1356 | if( buf[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1357 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1358 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1359 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1360 | } |
| 1361 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1362 | 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] | 1363 | ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) ); |
| 1364 | |
| 1365 | /* We don't support fragmentation of ClientHello (yet?) */ |
| 1366 | if( buf[1] != 0 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1367 | 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] | 1368 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1369 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1370 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1371 | } |
| 1372 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1373 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1374 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 1375 | { |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1376 | /* |
Manuel Pégourié-Gonnard | 69849f8 | 2015-03-10 11:54:02 +0000 | [diff] [blame] | 1377 | * Copy the client's handshake message_seq on initial handshakes, |
| 1378 | * check sequence number on renego. |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1379 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1380 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1381 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 1382 | { |
| 1383 | /* This couldn't be done in ssl_prepare_handshake_record() */ |
| 1384 | unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) | |
| 1385 | ssl->in_msg[5]; |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1386 | |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 1387 | if( cli_msg_seq != ssl->handshake->in_msg_seq ) |
| 1388 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1389 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message_seq: " |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 1390 | "%d (expected %d)", cli_msg_seq, |
| 1391 | ssl->handshake->in_msg_seq ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1392 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 1393 | } |
| 1394 | |
| 1395 | ssl->handshake->in_msg_seq++; |
| 1396 | } |
Manuel Pégourié-Gonnard | 69849f8 | 2015-03-10 11:54:02 +0000 | [diff] [blame] | 1397 | else |
| 1398 | #endif |
| 1399 | { |
| 1400 | unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) | |
| 1401 | ssl->in_msg[5]; |
| 1402 | ssl->handshake->out_msg_seq = cli_msg_seq; |
| 1403 | ssl->handshake->in_msg_seq = cli_msg_seq + 1; |
| 1404 | } |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 1405 | |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1406 | /* |
| 1407 | * For now we don't support fragmentation, so make sure |
| 1408 | * fragment_offset == 0 and fragment_length == length |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1409 | */ |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 1410 | if( ssl->in_msg[6] != 0 || ssl->in_msg[7] != 0 || ssl->in_msg[8] != 0 || |
| 1411 | memcmp( ssl->in_msg + 1, ssl->in_msg + 9, 3 ) != 0 ) |
| 1412 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1413 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "ClientHello fragmentation not supported" ) ); |
| 1414 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 1415 | } |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 1416 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1417 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 1418 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1419 | buf += mbedtls_ssl_hs_hdr_len( ssl ); |
| 1420 | msg_len -= mbedtls_ssl_hs_hdr_len( ssl ); |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1421 | |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 1422 | /* |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1423 | * ClientHello layer: |
| 1424 | * 0 . 1 protocol version |
| 1425 | * 2 . 33 random bytes (starting with 4 bytes of Unix time) |
| 1426 | * 34 . 35 session id length (1 byte) |
| 1427 | * 35 . 34+x session id |
| 1428 | * 35+x . 35+x DTLS only: cookie length (1 byte) |
| 1429 | * 36+x . .. DTLS only: cookie |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1430 | * .. . .. ciphersuite list length (2 bytes) |
| 1431 | * .. . .. ciphersuite list |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1432 | * .. . .. compression alg. list length (1 byte) |
| 1433 | * .. . .. compression alg. list |
| 1434 | * .. . .. extensions length (2 bytes, optional) |
| 1435 | * .. . .. extensions (optional) |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1436 | */ |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1437 | |
| 1438 | /* |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1439 | * Minimal length (with everything empty and extensions ommitted) is |
| 1440 | * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can |
| 1441 | * read at least up to session id length without worrying. |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1442 | */ |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1443 | if( msg_len < 38 ) |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1444 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1445 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1446 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1447 | } |
| 1448 | |
| 1449 | /* |
| 1450 | * Check and save the protocol version |
| 1451 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1452 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, version", buf, 2 ); |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1453 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1454 | mbedtls_ssl_read_version( &ssl->major_ver, &ssl->minor_ver, |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1455 | ssl->conf->transport, buf ); |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1456 | |
Manuel Pégourié-Gonnard | 6b1e207 | 2014-02-12 10:14:54 +0100 | [diff] [blame] | 1457 | ssl->handshake->max_major_ver = ssl->major_ver; |
| 1458 | ssl->handshake->max_minor_ver = ssl->minor_ver; |
| 1459 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1460 | if( ssl->major_ver < ssl->conf->min_major_ver || |
| 1461 | ssl->minor_ver < ssl->conf->min_minor_ver ) |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 1462 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1463 | 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] | 1464 | " [%d:%d] < [%d:%d]", |
| 1465 | ssl->major_ver, ssl->minor_ver, |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1466 | ssl->conf->min_major_ver, ssl->conf->min_minor_ver ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1467 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1468 | MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1469 | return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION ); |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 1470 | } |
| 1471 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1472 | if( ssl->major_ver > ssl->conf->max_major_ver ) |
Manuel Pégourié-Gonnard | 6b1e207 | 2014-02-12 10:14:54 +0100 | [diff] [blame] | 1473 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1474 | ssl->major_ver = ssl->conf->max_major_ver; |
| 1475 | ssl->minor_ver = ssl->conf->max_minor_ver; |
Manuel Pégourié-Gonnard | 6b1e207 | 2014-02-12 10:14:54 +0100 | [diff] [blame] | 1476 | } |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1477 | else if( ssl->minor_ver > ssl->conf->max_minor_ver ) |
| 1478 | ssl->minor_ver = ssl->conf->max_minor_ver; |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1479 | |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1480 | /* |
| 1481 | * Save client random (inc. Unix time) |
| 1482 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1483 | 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] | 1484 | |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1485 | memcpy( ssl->handshake->randbytes, buf + 2, 32 ); |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1486 | |
| 1487 | /* |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1488 | * Check the session ID length and save session ID |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1489 | */ |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1490 | sess_len = buf[34]; |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1491 | |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1492 | if( sess_len > sizeof( ssl->session_negotiate->id ) || |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1493 | sess_len + 34 + 2 > msg_len ) /* 2 for cipherlist length field */ |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1494 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1495 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1496 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1497 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1498 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1499 | } |
| 1500 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1501 | 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] | 1502 | |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 1503 | ssl->session_negotiate->id_len = sess_len; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1504 | memset( ssl->session_negotiate->id, 0, |
| 1505 | sizeof( ssl->session_negotiate->id ) ); |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1506 | memcpy( ssl->session_negotiate->id, buf + 35, |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 1507 | ssl->session_negotiate->id_len ); |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1508 | |
| 1509 | /* |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1510 | * Check the cookie length and content |
| 1511 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1512 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1513 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1514 | { |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1515 | cookie_offset = 35 + sess_len; |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1516 | cookie_len = buf[cookie_offset]; |
| 1517 | |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1518 | if( cookie_offset + 1 + cookie_len + 2 > msg_len ) |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1519 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1520 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1521 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1522 | MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1523 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1524 | } |
| 1525 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1526 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie", |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1527 | buf + cookie_offset + 1, cookie_len ); |
| 1528 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1529 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1530 | if( ssl->conf->f_cookie_check != NULL |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1531 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1532 | && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE |
Manuel Pégourié-Gonnard | 69849f8 | 2015-03-10 11:54:02 +0000 | [diff] [blame] | 1533 | #endif |
| 1534 | ) |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1535 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1536 | if( ssl->conf->f_cookie_check( ssl->conf->p_cookie, |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1537 | buf + cookie_offset + 1, cookie_len, |
| 1538 | ssl->cli_id, ssl->cli_id_len ) != 0 ) |
| 1539 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1540 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification failed" ) ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1541 | ssl->handshake->verify_cookie_len = 1; |
| 1542 | } |
| 1543 | else |
| 1544 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1545 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification passed" ) ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1546 | ssl->handshake->verify_cookie_len = 0; |
| 1547 | } |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1548 | } |
Manuel Pégourié-Gonnard | d7f9bc5 | 2014-07-23 11:09:27 +0200 | [diff] [blame] | 1549 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1550 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */ |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1551 | { |
| 1552 | /* We know we didn't send a cookie, so it should be empty */ |
| 1553 | if( cookie_len != 0 ) |
| 1554 | { |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1555 | /* 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] | 1556 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1557 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1558 | } |
| 1559 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1560 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification skipped" ) ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1561 | } |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1562 | |
| 1563 | /* |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1564 | * Check the ciphersuitelist length (will be parsed later) |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1565 | */ |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1566 | ciph_offset = cookie_offset + 1 + cookie_len; |
Manuel Pégourié-Gonnard | a06d7fe | 2015-03-13 10:36:55 +0000 | [diff] [blame] | 1567 | } |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1568 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1569 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1570 | ciph_offset = 35 + sess_len; |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1571 | |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1572 | ciph_len = ( buf[ciph_offset + 0] << 8 ) |
| 1573 | | ( buf[ciph_offset + 1] ); |
| 1574 | |
| 1575 | if( ciph_len < 2 || |
| 1576 | ciph_len + 2 + ciph_offset + 1 > msg_len || /* 1 for comp. alg. len */ |
| 1577 | ( ciph_len % 2 ) != 0 ) |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1578 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1579 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1580 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1581 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1582 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1583 | } |
| 1584 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1585 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist", |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1586 | buf + ciph_offset + 2, ciph_len ); |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1587 | |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1588 | /* |
| 1589 | * Check the compression algorithms length and pick one |
| 1590 | */ |
| 1591 | comp_offset = ciph_offset + 2 + ciph_len; |
| 1592 | |
| 1593 | comp_len = buf[comp_offset]; |
| 1594 | |
| 1595 | if( comp_len < 1 || |
| 1596 | comp_len > 16 || |
| 1597 | comp_len + comp_offset + 1 > msg_len ) |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1598 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1599 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1600 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1601 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1602 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1603 | } |
| 1604 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1605 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, compression", |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1606 | buf + comp_offset + 1, comp_len ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1607 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1608 | ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL; |
| 1609 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1610 | for( i = 0; i < comp_len; ++i ) |
| 1611 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1612 | if( buf[comp_offset + 1 + i] == MBEDTLS_SSL_COMPRESS_DEFLATE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1613 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1614 | ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_DEFLATE; |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1615 | break; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1616 | } |
| 1617 | } |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1618 | #endif |
| 1619 | |
Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1620 | /* See comments in ssl_write_client_hello() */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1621 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1622 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1623 | ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL; |
Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1624 | #endif |
Manuel Pégourié-Gonnard | 82202f0 | 2014-07-23 00:28:58 +0200 | [diff] [blame] | 1625 | |
Janos Follath | c6dab2b | 2016-05-23 14:27:02 +0100 | [diff] [blame] | 1626 | /* Do not parse the extensions if the protocol is SSLv3 */ |
| 1627 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 1628 | if( ( ssl->major_ver != 3 ) || ( ssl->minor_ver != 0 ) ) |
| 1629 | { |
| 1630 | #endif |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1631 | /* |
| 1632 | * Check the extension length |
| 1633 | */ |
| 1634 | ext_offset = comp_offset + 1 + comp_len; |
| 1635 | if( msg_len > ext_offset ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1636 | { |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1637 | if( msg_len < ext_offset + 2 ) |
| 1638 | { |
| 1639 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1640 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1641 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1642 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 1643 | } |
| 1644 | |
| 1645 | ext_len = ( buf[ext_offset + 0] << 8 ) |
| 1646 | | ( buf[ext_offset + 1] ); |
| 1647 | |
| 1648 | if( ( ext_len > 0 && ext_len < 4 ) || |
| 1649 | msg_len != ext_offset + 2 + ext_len ) |
| 1650 | { |
| 1651 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1652 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1653 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1654 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 1655 | } |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1656 | } |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1657 | else |
| 1658 | ext_len = 0; |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1659 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1660 | ext = buf + ext_offset + 2; |
| 1661 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", ext, ext_len ); |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1662 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1663 | while( ext_len != 0 ) |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1664 | { |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1665 | unsigned int ext_id = ( ( ext[0] << 8 ) |
| 1666 | | ( ext[1] ) ); |
| 1667 | unsigned int ext_size = ( ( ext[2] << 8 ) |
| 1668 | | ( ext[3] ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1669 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1670 | if( ext_size + 4 > ext_len ) |
| 1671 | { |
| 1672 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1673 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1674 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1675 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 1676 | } |
| 1677 | switch( ext_id ) |
| 1678 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1679 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1680 | case MBEDTLS_TLS_EXT_SERVERNAME: |
| 1681 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) ); |
| 1682 | if( ssl->conf->f_sni == NULL ) |
| 1683 | break; |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 1684 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1685 | ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size ); |
| 1686 | if( ret != 0 ) |
| 1687 | return( ret ); |
| 1688 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1689 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 1690 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1691 | case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO: |
| 1692 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1693 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1694 | renegotiation_info_seen = 1; |
Manuel Pégourié-Gonnard | eaecbd3 | 2014-11-06 02:38:02 +0100 | [diff] [blame] | 1695 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1696 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1697 | ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size ); |
| 1698 | if( ret != 0 ) |
| 1699 | return( ret ); |
| 1700 | break; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1701 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1702 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 1703 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1704 | case MBEDTLS_TLS_EXT_SIG_ALG: |
Ron Eldor | 73a3817 | 2017-10-03 15:58:26 +0300 | [diff] [blame] | 1705 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) ); |
| 1706 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1707 | ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size ); |
| 1708 | if( ret != 0 ) |
| 1709 | return( ret ); |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 1710 | |
| 1711 | sig_hash_alg_ext_present = 1; |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1712 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1713 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && |
| 1714 | MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1715 | |
Robert Cragie | 136884c | 2015-10-02 13:34:31 +0100 | [diff] [blame] | 1716 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 1717 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1718 | case MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES: |
| 1719 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1720 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1721 | ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size ); |
| 1722 | if( ret != 0 ) |
| 1723 | return( ret ); |
| 1724 | break; |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1725 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1726 | case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS: |
| 1727 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) ); |
| 1728 | ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT; |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1729 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1730 | ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size ); |
| 1731 | if( ret != 0 ) |
| 1732 | return( ret ); |
| 1733 | break; |
Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 1734 | #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || |
| 1735 | MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1736 | |
Manuel Pégourié-Gonnard | bf57be6 | 2015-09-16 15:04:01 +0200 | [diff] [blame] | 1737 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1738 | case MBEDTLS_TLS_EXT_ECJPAKE_KKPP: |
| 1739 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake kkpp extension" ) ); |
Manuel Pégourié-Gonnard | bf57be6 | 2015-09-16 15:04:01 +0200 | [diff] [blame] | 1740 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1741 | ret = ssl_parse_ecjpake_kkpp( ssl, ext + 4, ext_size ); |
| 1742 | if( ret != 0 ) |
| 1743 | return( ret ); |
| 1744 | break; |
Manuel Pégourié-Gonnard | bf57be6 | 2015-09-16 15:04:01 +0200 | [diff] [blame] | 1745 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
| 1746 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1747 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1748 | case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH: |
| 1749 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) ); |
Manuel Pégourié-Gonnard | 48f8d0d | 2013-07-17 10:25:37 +0200 | [diff] [blame] | 1750 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1751 | ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size ); |
| 1752 | if( ret != 0 ) |
| 1753 | return( ret ); |
| 1754 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1755 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 48f8d0d | 2013-07-17 10:25:37 +0200 | [diff] [blame] | 1756 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1757 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1758 | case MBEDTLS_TLS_EXT_TRUNCATED_HMAC: |
| 1759 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) ); |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 1760 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1761 | ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size ); |
| 1762 | if( ret != 0 ) |
| 1763 | return( ret ); |
| 1764 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1765 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 1766 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1767 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1768 | case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC: |
| 1769 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) ); |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 1770 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1771 | ret = ssl_parse_encrypt_then_mac_ext( ssl, ext + 4, ext_size ); |
| 1772 | if( ret != 0 ) |
| 1773 | return( ret ); |
| 1774 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1775 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 1776 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1777 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1778 | case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET: |
| 1779 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) ); |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 1780 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1781 | ret = ssl_parse_extended_ms_ext( ssl, ext + 4, ext_size ); |
| 1782 | if( ret != 0 ) |
| 1783 | return( ret ); |
| 1784 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1785 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 1786 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1787 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1788 | case MBEDTLS_TLS_EXT_SESSION_TICKET: |
| 1789 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) ); |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 1790 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1791 | ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size ); |
| 1792 | if( ret != 0 ) |
| 1793 | return( ret ); |
| 1794 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1795 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 1796 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1797 | #if defined(MBEDTLS_SSL_ALPN) |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1798 | case MBEDTLS_TLS_EXT_ALPN: |
| 1799 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) ); |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 1800 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1801 | ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ); |
| 1802 | if( ret != 0 ) |
| 1803 | return( ret ); |
| 1804 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1805 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 1806 | |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1807 | default: |
| 1808 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)", |
| 1809 | ext_id ) ); |
| 1810 | } |
| 1811 | |
| 1812 | ext_len -= 4 + ext_size; |
| 1813 | ext += 4 + ext_size; |
| 1814 | |
| 1815 | if( ext_len > 0 && ext_len < 4 ) |
| 1816 | { |
| 1817 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1818 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1819 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1820 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 1821 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1822 | } |
Janos Follath | c6dab2b | 2016-05-23 14:27:02 +0100 | [diff] [blame] | 1823 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 1824 | } |
| 1825 | #endif |
| 1826 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1827 | #if defined(MBEDTLS_SSL_FALLBACK_SCSV) |
Gilles Peskine | d50177f | 2017-05-16 17:53:03 +0200 | [diff] [blame] | 1828 | 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] | 1829 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1830 | if( p[0] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE >> 8 ) & 0xff ) && |
| 1831 | p[1] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE ) & 0xff ) ) |
Manuel Pégourié-Gonnard | fedba98 | 2014-11-05 16:12:09 +0100 | [diff] [blame] | 1832 | { |
Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 1833 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "received FALLBACK_SCSV" ) ); |
Manuel Pégourié-Gonnard | fedba98 | 2014-11-05 16:12:09 +0100 | [diff] [blame] | 1834 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1835 | if( ssl->minor_ver < ssl->conf->max_minor_ver ) |
Manuel Pégourié-Gonnard | fedba98 | 2014-11-05 16:12:09 +0100 | [diff] [blame] | 1836 | { |
Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 1837 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) ); |
Manuel Pégourié-Gonnard | fedba98 | 2014-11-05 16:12:09 +0100 | [diff] [blame] | 1838 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1839 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1840 | MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK ); |
Manuel Pégourié-Gonnard | fedba98 | 2014-11-05 16:12:09 +0100 | [diff] [blame] | 1841 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1842 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | fedba98 | 2014-11-05 16:12:09 +0100 | [diff] [blame] | 1843 | } |
| 1844 | |
| 1845 | break; |
| 1846 | } |
| 1847 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1848 | #endif /* MBEDTLS_SSL_FALLBACK_SCSV */ |
Manuel Pégourié-Gonnard | fedba98 | 2014-11-05 16:12:09 +0100 | [diff] [blame] | 1849 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 1850 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 1851 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
| 1852 | |
| 1853 | /* |
| 1854 | * Try to fall back to default hash SHA1 if the client |
| 1855 | * hasn't provided any preferred signature-hash combinations. |
| 1856 | */ |
| 1857 | if( sig_hash_alg_ext_present == 0 ) |
| 1858 | { |
| 1859 | mbedtls_md_type_t md_default = MBEDTLS_MD_SHA1; |
| 1860 | |
| 1861 | if( mbedtls_ssl_check_sig_hash( ssl, md_default ) != 0 ) |
| 1862 | md_default = MBEDTLS_MD_NONE; |
| 1863 | |
| 1864 | mbedtls_ssl_sig_hash_set_const_hash( &ssl->handshake->hash_algs, md_default ); |
| 1865 | } |
| 1866 | |
| 1867 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && |
| 1868 | MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
| 1869 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1870 | /* |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1871 | * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV |
| 1872 | */ |
| 1873 | for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 ) |
| 1874 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1875 | 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] | 1876 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1877 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) ); |
| 1878 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1879 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1880 | { |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 1881 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV " |
| 1882 | "during renegotiation" ) ); |
Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 1883 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1884 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1885 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1886 | } |
Manuel Pégourié-Gonnard | 69849f8 | 2015-03-10 11:54:02 +0000 | [diff] [blame] | 1887 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1888 | ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION; |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1889 | break; |
| 1890 | } |
| 1891 | } |
| 1892 | |
| 1893 | /* |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1894 | * Renegotiation security checks |
| 1895 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1896 | if( ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION && |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1897 | ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE ) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1898 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1899 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) ); |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1900 | handshake_failure = 1; |
| 1901 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1902 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1903 | else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && |
| 1904 | ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION && |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1905 | renegotiation_info_seen == 0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1906 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1907 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) ); |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1908 | handshake_failure = 1; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1909 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1910 | else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && |
| 1911 | ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1912 | ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) |
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 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) ); |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1915 | handshake_failure = 1; |
| 1916 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1917 | else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && |
| 1918 | ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1919 | renegotiation_info_seen == 1 ) |
| 1920 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1921 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) ); |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1922 | handshake_failure = 1; |
| 1923 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1924 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1925 | |
| 1926 | if( handshake_failure == 1 ) |
| 1927 | { |
Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 1928 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1929 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1930 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1931 | } |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1932 | |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1933 | /* |
| 1934 | * Search for a matching ciphersuite |
Manuel Pégourié-Gonnard | 3ebb2cd | 2013-09-23 17:00:18 +0200 | [diff] [blame] | 1935 | * (At the end because we need information from the EC-based extensions |
| 1936 | * and certificate from the SNI callback triggered by the SNI extension.) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1937 | */ |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1938 | got_common_suite = 0; |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1939 | ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver]; |
Manuel Pégourié-Gonnard | 59b81d7 | 2013-11-30 17:46:04 +0100 | [diff] [blame] | 1940 | ciphersuite_info = NULL; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1941 | #if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE) |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1942 | for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 ) |
Manuel Pégourié-Gonnard | 1a9f2c7 | 2013-11-30 18:30:06 +0100 | [diff] [blame] | 1943 | for( i = 0; ciphersuites[i] != 0; i++ ) |
| 1944 | #else |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 1945 | for( i = 0; ciphersuites[i] != 0; i++ ) |
Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1946 | for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 ) |
Manuel Pégourié-Gonnard | 1a9f2c7 | 2013-11-30 18:30:06 +0100 | [diff] [blame] | 1947 | #endif |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1948 | { |
Manuel Pégourié-Gonnard | 011a8db | 2013-11-30 18:11:07 +0100 | [diff] [blame] | 1949 | if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) || |
| 1950 | p[1] != ( ( ciphersuites[i] ) & 0xFF ) ) |
| 1951 | continue; |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1952 | |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1953 | got_common_suite = 1; |
| 1954 | |
Manuel Pégourié-Gonnard | 011a8db | 2013-11-30 18:11:07 +0100 | [diff] [blame] | 1955 | if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i], |
| 1956 | &ciphersuite_info ) ) != 0 ) |
| 1957 | return( ret ); |
| 1958 | |
| 1959 | if( ciphersuite_info != NULL ) |
| 1960 | goto have_ciphersuite; |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1961 | } |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1962 | |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1963 | if( got_common_suite ) |
| 1964 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1965 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, " |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1966 | "but none of them usable" ) ); |
Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 1967 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1968 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1969 | return( MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE ); |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1970 | } |
| 1971 | else |
| 1972 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1973 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) ); |
Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 1974 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1975 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1976 | return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN ); |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1977 | } |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1978 | |
| 1979 | have_ciphersuite: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1980 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) ); |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 1981 | |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 1982 | ssl->session_negotiate->ciphersuite = ciphersuites[i]; |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1983 | ssl->transform_negotiate->ciphersuite_info = ciphersuite_info; |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1984 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1985 | ssl->state++; |
| 1986 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1987 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1988 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1989 | mbedtls_ssl_recv_flight_completed( ssl ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 1990 | #endif |
| 1991 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 1992 | /* Debugging-only output for testsuite */ |
| 1993 | #if defined(MBEDTLS_DEBUG_C) && \ |
| 1994 | defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 1995 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
| 1996 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
| 1997 | { |
| 1998 | mbedtls_pk_type_t sig_alg = mbedtls_ssl_get_ciphersuite_sig_alg( ciphersuite_info ); |
| 1999 | if( sig_alg != MBEDTLS_PK_NONE ) |
| 2000 | { |
| 2001 | mbedtls_md_type_t md_alg = mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs, |
| 2002 | sig_alg ); |
| 2003 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d", |
| 2004 | mbedtls_ssl_hash_from_md_alg( md_alg ) ) ); |
| 2005 | } |
| 2006 | else |
| 2007 | { |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 2008 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no hash algorithm for signature algorithm " |
| 2009 | "%d - should not happen", sig_alg ) ); |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 2010 | } |
| 2011 | } |
| 2012 | #endif |
| 2013 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2014 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2015 | |
| 2016 | return( 0 ); |
| 2017 | } |
| 2018 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2019 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 2020 | 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] | 2021 | unsigned char *buf, |
| 2022 | size_t *olen ) |
| 2023 | { |
| 2024 | unsigned char *p = buf; |
| 2025 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2026 | 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] | 2027 | { |
| 2028 | *olen = 0; |
| 2029 | return; |
| 2030 | } |
| 2031 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2032 | 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] | 2033 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2034 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF ); |
| 2035 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_TRUNCATED_HMAC ) & 0xFF ); |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 2036 | |
| 2037 | *p++ = 0x00; |
| 2038 | *p++ = 0x00; |
| 2039 | |
| 2040 | *olen = 4; |
| 2041 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2042 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 2043 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2044 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 2045 | 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] | 2046 | unsigned char *buf, |
| 2047 | size_t *olen ) |
| 2048 | { |
| 2049 | unsigned char *p = buf; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2050 | const mbedtls_ssl_ciphersuite_t *suite = NULL; |
| 2051 | const mbedtls_cipher_info_t *cipher = NULL; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2052 | |
Hanno Becker | 27b34d5 | 2017-10-20 14:24:51 +0100 | [diff] [blame] | 2053 | if( ssl->session_negotiate->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2054 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2055 | { |
| 2056 | *olen = 0; |
| 2057 | return; |
| 2058 | } |
| 2059 | |
Manuel Pégourié-Gonnard | 78e745f | 2014-11-04 15:44:06 +0100 | [diff] [blame] | 2060 | /* |
| 2061 | * RFC 7366: "If a server receives an encrypt-then-MAC request extension |
| 2062 | * from a client and then selects a stream or Authenticated Encryption |
| 2063 | * with Associated Data (AEAD) ciphersuite, it MUST NOT send an |
| 2064 | * encrypt-then-MAC response extension back to the client." |
| 2065 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2066 | if( ( suite = mbedtls_ssl_ciphersuite_from_id( |
Manuel Pégourié-Gonnard | 78e745f | 2014-11-04 15:44:06 +0100 | [diff] [blame] | 2067 | ssl->session_negotiate->ciphersuite ) ) == NULL || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2068 | ( cipher = mbedtls_cipher_info_from_type( suite->cipher ) ) == NULL || |
| 2069 | cipher->mode != MBEDTLS_MODE_CBC ) |
Manuel Pégourié-Gonnard | 78e745f | 2014-11-04 15:44:06 +0100 | [diff] [blame] | 2070 | { |
| 2071 | *olen = 0; |
| 2072 | return; |
| 2073 | } |
| 2074 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2075 | 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] | 2076 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2077 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF ); |
| 2078 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF ); |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2079 | |
| 2080 | *p++ = 0x00; |
| 2081 | *p++ = 0x00; |
| 2082 | |
| 2083 | *olen = 4; |
| 2084 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2085 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2086 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2087 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 2088 | 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] | 2089 | unsigned char *buf, |
| 2090 | size_t *olen ) |
| 2091 | { |
| 2092 | unsigned char *p = buf; |
| 2093 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2094 | if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED || |
| 2095 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2096 | { |
| 2097 | *olen = 0; |
| 2098 | return; |
| 2099 | } |
| 2100 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2101 | 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] | 2102 | "extension" ) ); |
| 2103 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2104 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF ); |
| 2105 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF ); |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2106 | |
| 2107 | *p++ = 0x00; |
| 2108 | *p++ = 0x00; |
| 2109 | |
| 2110 | *olen = 4; |
| 2111 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2112 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2113 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2114 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 2115 | 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] | 2116 | unsigned char *buf, |
| 2117 | size_t *olen ) |
| 2118 | { |
| 2119 | unsigned char *p = buf; |
| 2120 | |
| 2121 | if( ssl->handshake->new_session_ticket == 0 ) |
| 2122 | { |
| 2123 | *olen = 0; |
| 2124 | return; |
| 2125 | } |
| 2126 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2127 | 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] | 2128 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2129 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF ); |
| 2130 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET ) & 0xFF ); |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 2131 | |
| 2132 | *p++ = 0x00; |
| 2133 | *p++ = 0x00; |
| 2134 | |
| 2135 | *olen = 4; |
| 2136 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2137 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 2138 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2139 | static void ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | f11a6d7 | 2013-07-17 11:17:14 +0200 | [diff] [blame] | 2140 | unsigned char *buf, |
| 2141 | size_t *olen ) |
| 2142 | { |
| 2143 | unsigned char *p = buf; |
| 2144 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2145 | if( ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION ) |
Manuel Pégourié-Gonnard | f11a6d7 | 2013-07-17 11:17:14 +0200 | [diff] [blame] | 2146 | { |
| 2147 | *olen = 0; |
| 2148 | return; |
| 2149 | } |
| 2150 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2151 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) ); |
Manuel Pégourié-Gonnard | f11a6d7 | 2013-07-17 11:17:14 +0200 | [diff] [blame] | 2152 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2153 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF ); |
| 2154 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO ) & 0xFF ); |
Manuel Pégourié-Gonnard | f11a6d7 | 2013-07-17 11:17:14 +0200 | [diff] [blame] | 2155 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2156 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 2157 | if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ) |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2158 | { |
| 2159 | *p++ = 0x00; |
| 2160 | *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF; |
| 2161 | *p++ = ssl->verify_data_len * 2 & 0xFF; |
Manuel Pégourié-Gonnard | f11a6d7 | 2013-07-17 11:17:14 +0200 | [diff] [blame] | 2162 | |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2163 | memcpy( p, ssl->peer_verify_data, ssl->verify_data_len ); |
| 2164 | p += ssl->verify_data_len; |
| 2165 | memcpy( p, ssl->own_verify_data, ssl->verify_data_len ); |
| 2166 | p += ssl->verify_data_len; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2167 | } |
| 2168 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2169 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2170 | { |
| 2171 | *p++ = 0x00; |
| 2172 | *p++ = 0x01; |
| 2173 | *p++ = 0x00; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2174 | } |
Manuel Pégourié-Gonnard | 1938975 | 2015-06-23 13:46:44 +0200 | [diff] [blame] | 2175 | |
| 2176 | *olen = p - buf; |
Manuel Pégourié-Gonnard | f11a6d7 | 2013-07-17 11:17:14 +0200 | [diff] [blame] | 2177 | } |
| 2178 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2179 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 2180 | 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] | 2181 | unsigned char *buf, |
| 2182 | size_t *olen ) |
| 2183 | { |
| 2184 | unsigned char *p = buf; |
| 2185 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2186 | 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] | 2187 | { |
Manuel Pégourié-Gonnard | 7bb7899 | 2013-07-17 13:50:08 +0200 | [diff] [blame] | 2188 | *olen = 0; |
| 2189 | return; |
| 2190 | } |
| 2191 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2192 | 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] | 2193 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2194 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF ); |
| 2195 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF ); |
Manuel Pégourié-Gonnard | 7bb7899 | 2013-07-17 13:50:08 +0200 | [diff] [blame] | 2196 | |
| 2197 | *p++ = 0x00; |
| 2198 | *p++ = 1; |
| 2199 | |
Manuel Pégourié-Gonnard | ed4af8b | 2013-07-18 14:07:09 +0200 | [diff] [blame] | 2200 | *p++ = ssl->session_negotiate->mfl_code; |
Manuel Pégourié-Gonnard | 7bb7899 | 2013-07-17 13:50:08 +0200 | [diff] [blame] | 2201 | |
| 2202 | *olen = 5; |
| 2203 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2204 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 7bb7899 | 2013-07-17 13:50:08 +0200 | [diff] [blame] | 2205 | |
Manuel Pégourié-Gonnard | f472179 | 2015-09-15 10:53:51 +0200 | [diff] [blame] | 2206 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 2207 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2208 | 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] | 2209 | unsigned char *buf, |
| 2210 | size_t *olen ) |
| 2211 | { |
| 2212 | unsigned char *p = buf; |
| 2213 | ((void) ssl); |
| 2214 | |
Paul Bakker | 677377f | 2013-10-28 12:54:26 +0100 | [diff] [blame] | 2215 | if( ( ssl->handshake->cli_exts & |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2216 | MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 ) |
Paul Bakker | 677377f | 2013-10-28 12:54:26 +0100 | [diff] [blame] | 2217 | { |
| 2218 | *olen = 0; |
| 2219 | return; |
| 2220 | } |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 2221 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2222 | 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] | 2223 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2224 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF ); |
| 2225 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF ); |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 2226 | |
| 2227 | *p++ = 0x00; |
| 2228 | *p++ = 2; |
| 2229 | |
| 2230 | *p++ = 1; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2231 | *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED; |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 2232 | |
| 2233 | *olen = 6; |
| 2234 | } |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 2235 | #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] | 2236 | |
Manuel Pégourié-Gonnard | 55c7f99 | 2015-09-16 15:35:27 +0200 | [diff] [blame] | 2237 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 2238 | static void ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl, |
| 2239 | unsigned char *buf, |
| 2240 | size_t *olen ) |
| 2241 | { |
| 2242 | int ret; |
| 2243 | unsigned char *p = buf; |
Robert Cragie | 39a60de | 2015-10-02 13:57:59 +0100 | [diff] [blame] | 2244 | const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN; |
Manuel Pégourié-Gonnard | 55c7f99 | 2015-09-16 15:35:27 +0200 | [diff] [blame] | 2245 | size_t kkpp_len; |
| 2246 | |
| 2247 | *olen = 0; |
| 2248 | |
| 2249 | /* Skip costly computation if not needed */ |
| 2250 | if( ssl->transform_negotiate->ciphersuite_info->key_exchange != |
| 2251 | MBEDTLS_KEY_EXCHANGE_ECJPAKE ) |
| 2252 | return; |
| 2253 | |
| 2254 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, ecjpake kkpp extension" ) ); |
| 2255 | |
| 2256 | if( end - p < 4 ) |
| 2257 | { |
| 2258 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) ); |
| 2259 | return; |
| 2260 | } |
| 2261 | |
| 2262 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP >> 8 ) & 0xFF ); |
| 2263 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP ) & 0xFF ); |
| 2264 | |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 2265 | ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx, |
| 2266 | p + 2, end - p - 2, &kkpp_len, |
| 2267 | ssl->conf->f_rng, ssl->conf->p_rng ); |
| 2268 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 55c7f99 | 2015-09-16 15:35:27 +0200 | [diff] [blame] | 2269 | { |
| 2270 | MBEDTLS_SSL_DEBUG_RET( 1 , "mbedtls_ecjpake_write_round_one", ret ); |
| 2271 | return; |
| 2272 | } |
| 2273 | |
| 2274 | *p++ = (unsigned char)( ( kkpp_len >> 8 ) & 0xFF ); |
| 2275 | *p++ = (unsigned char)( ( kkpp_len ) & 0xFF ); |
| 2276 | |
| 2277 | *olen = kkpp_len + 4; |
| 2278 | } |
| 2279 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
| 2280 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2281 | #if defined(MBEDTLS_SSL_ALPN ) |
| 2282 | static void ssl_write_alpn_ext( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 2283 | unsigned char *buf, size_t *olen ) |
| 2284 | { |
| 2285 | if( ssl->alpn_chosen == NULL ) |
| 2286 | { |
| 2287 | *olen = 0; |
| 2288 | return; |
| 2289 | } |
| 2290 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2291 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) ); |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 2292 | |
| 2293 | /* |
| 2294 | * 0 . 1 ext identifier |
| 2295 | * 2 . 3 ext length |
| 2296 | * 4 . 5 protocol list length |
| 2297 | * 6 . 6 protocol name length |
| 2298 | * 7 . 7+n protocol name |
| 2299 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2300 | buf[0] = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN >> 8 ) & 0xFF ); |
| 2301 | buf[1] = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN ) & 0xFF ); |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 2302 | |
| 2303 | *olen = 7 + strlen( ssl->alpn_chosen ); |
| 2304 | |
| 2305 | buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF ); |
| 2306 | buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF ); |
| 2307 | |
| 2308 | buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF ); |
| 2309 | buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF ); |
| 2310 | |
| 2311 | buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF ); |
| 2312 | |
| 2313 | memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 ); |
| 2314 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2315 | #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */ |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 2316 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2317 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) |
| 2318 | 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] | 2319 | { |
| 2320 | int ret; |
| 2321 | unsigned char *p = ssl->out_msg + 4; |
Manuel Pégourié-Gonnard | d7f9bc5 | 2014-07-23 11:09:27 +0200 | [diff] [blame] | 2322 | unsigned char *cookie_len_byte; |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2323 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2324 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello verify request" ) ); |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2325 | |
| 2326 | /* |
| 2327 | * struct { |
| 2328 | * ProtocolVersion server_version; |
| 2329 | * opaque cookie<0..2^8-1>; |
| 2330 | * } HelloVerifyRequest; |
| 2331 | */ |
| 2332 | |
Manuel Pégourié-Gonnard | b35fe56 | 2014-08-09 17:00:46 +0200 | [diff] [blame] | 2333 | /* The RFC is not clear on this point, but sending the actual negotiated |
| 2334 | * version looks like the most interoperable thing to do. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2335 | mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver, |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2336 | ssl->conf->transport, p ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2337 | MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 ); |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2338 | p += 2; |
| 2339 | |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 2340 | /* If we get here, f_cookie_check is not null */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2341 | if( ssl->conf->f_cookie_write == NULL ) |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 2342 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2343 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "inconsistent cookie callbacks" ) ); |
| 2344 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 2345 | } |
| 2346 | |
Manuel Pégourié-Gonnard | d7f9bc5 | 2014-07-23 11:09:27 +0200 | [diff] [blame] | 2347 | /* Skip length byte until we know the length */ |
| 2348 | cookie_len_byte = p++; |
| 2349 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2350 | if( ( ret = ssl->conf->f_cookie_write( ssl->conf->p_cookie, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2351 | &p, ssl->out_buf + MBEDTLS_SSL_BUFFER_LEN, |
Manuel Pégourié-Gonnard | d485d19 | 2014-07-23 14:56:15 +0200 | [diff] [blame] | 2352 | ssl->cli_id, ssl->cli_id_len ) ) != 0 ) |
Manuel Pégourié-Gonnard | d7f9bc5 | 2014-07-23 11:09:27 +0200 | [diff] [blame] | 2353 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2354 | MBEDTLS_SSL_DEBUG_RET( 1, "f_cookie_write", ret ); |
Manuel Pégourié-Gonnard | d7f9bc5 | 2014-07-23 11:09:27 +0200 | [diff] [blame] | 2355 | return( ret ); |
| 2356 | } |
| 2357 | |
| 2358 | *cookie_len_byte = (unsigned char)( p - ( cookie_len_byte + 1 ) ); |
| 2359 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2360 | 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] | 2361 | |
| 2362 | ssl->out_msglen = p - ssl->out_msg; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2363 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 2364 | ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST; |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2365 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2366 | ssl->state = MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT; |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2367 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2368 | if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2369 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2370 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret ); |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2371 | return( ret ); |
| 2372 | } |
| 2373 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2374 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello verify request" ) ); |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2375 | |
| 2376 | return( 0 ); |
| 2377 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2378 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */ |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2379 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2380 | static int ssl_write_server_hello( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2381 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2382 | #if defined(MBEDTLS_HAVE_TIME) |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 2383 | mbedtls_time_t t; |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 2384 | #endif |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 2385 | int ret; |
| 2386 | size_t olen, ext_len = 0, n; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2387 | unsigned char *buf, *p; |
| 2388 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2389 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2390 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2391 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2392 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | d7f9bc5 | 2014-07-23 11:09:27 +0200 | [diff] [blame] | 2393 | ssl->handshake->verify_cookie_len != 0 ) |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2394 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2395 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "client hello was not authenticated" ) ); |
| 2396 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) ); |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2397 | |
Manuel Pégourié-Gonnard | 579950c | 2014-09-29 17:47:33 +0200 | [diff] [blame] | 2398 | return( ssl_write_hello_verify_request( ssl ) ); |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2399 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2400 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */ |
Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2401 | |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 2402 | if( ssl->conf->f_rng == NULL ) |
Paul Bakker | a9a028e | 2013-11-21 17:31:06 +0100 | [diff] [blame] | 2403 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2404 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided") ); |
| 2405 | return( MBEDTLS_ERR_SSL_NO_RNG ); |
Paul Bakker | a9a028e | 2013-11-21 17:31:06 +0100 | [diff] [blame] | 2406 | } |
| 2407 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2408 | /* |
| 2409 | * 0 . 0 handshake type |
| 2410 | * 1 . 3 handshake length |
| 2411 | * 4 . 5 protocol version |
| 2412 | * 6 . 9 UNIX time() |
| 2413 | * 10 . 37 random bytes |
| 2414 | */ |
| 2415 | buf = ssl->out_msg; |
| 2416 | p = buf + 4; |
| 2417 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2418 | mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver, |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2419 | ssl->conf->transport, p ); |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 2420 | p += 2; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2421 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2422 | 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] | 2423 | buf[4], buf[5] ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2424 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2425 | #if defined(MBEDTLS_HAVE_TIME) |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 2426 | t = mbedtls_time( NULL ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2427 | *p++ = (unsigned char)( t >> 24 ); |
| 2428 | *p++ = (unsigned char)( t >> 16 ); |
| 2429 | *p++ = (unsigned char)( t >> 8 ); |
| 2430 | *p++ = (unsigned char)( t ); |
| 2431 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2432 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) ); |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 2433 | #else |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 2434 | if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 4 ) ) != 0 ) |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 2435 | return( ret ); |
| 2436 | |
| 2437 | p += 4; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2438 | #endif /* MBEDTLS_HAVE_TIME */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2439 | |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 2440 | if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 28 ) ) != 0 ) |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 2441 | return( ret ); |
| 2442 | |
| 2443 | p += 28; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2444 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2445 | memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2446 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2447 | MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2448 | |
| 2449 | /* |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2450 | * Resume is 0 by default, see ssl_handshake_init(). |
| 2451 | * It may be already set to 1 by ssl_parse_session_ticket_ext(). |
| 2452 | * If not, try looking up session ID in our cache. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2453 | */ |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2454 | if( ssl->handshake->resume == 0 && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2455 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 2456 | ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE && |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2457 | #endif |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 2458 | ssl->session_negotiate->id_len != 0 && |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2459 | ssl->conf->f_get_cache != NULL && |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 2460 | 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] | 2461 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2462 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) ); |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2463 | ssl->handshake->resume = 1; |
| 2464 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2465 | |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2466 | if( ssl->handshake->resume == 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2467 | { |
| 2468 | /* |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2469 | * New session, create a new session id, |
| 2470 | * unless we're about to issue a session ticket |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2471 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2472 | ssl->state++; |
| 2473 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2474 | #if defined(MBEDTLS_HAVE_TIME) |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 2475 | ssl->session_negotiate->start = mbedtls_time( NULL ); |
Manuel Pégourié-Gonnard | 164d894 | 2013-09-23 22:01:39 +0200 | [diff] [blame] | 2476 | #endif |
| 2477 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2478 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | 164d894 | 2013-09-23 22:01:39 +0200 | [diff] [blame] | 2479 | if( ssl->handshake->new_session_ticket != 0 ) |
| 2480 | { |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 2481 | ssl->session_negotiate->id_len = n = 0; |
Manuel Pégourié-Gonnard | 164d894 | 2013-09-23 22:01:39 +0200 | [diff] [blame] | 2482 | memset( ssl->session_negotiate->id, 0, 32 ); |
| 2483 | } |
| 2484 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2485 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2486 | { |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 2487 | ssl->session_negotiate->id_len = n = 32; |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 2488 | if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->session_negotiate->id, |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 2489 | n ) ) != 0 ) |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2490 | return( ret ); |
| 2491 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2492 | } |
| 2493 | else |
| 2494 | { |
| 2495 | /* |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2496 | * Resuming a session |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2497 | */ |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 2498 | n = ssl->session_negotiate->id_len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2499 | ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC; |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 2500 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2501 | if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 ) |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 2502 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2503 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret ); |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 2504 | return( ret ); |
| 2505 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2506 | } |
| 2507 | |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2508 | /* |
| 2509 | * 38 . 38 session id length |
| 2510 | * 39 . 38+n session id |
| 2511 | * 39+n . 40+n chosen ciphersuite |
| 2512 | * 41+n . 41+n chosen compression alg. |
| 2513 | * 42+n . 43+n extensions length |
| 2514 | * 44+n . 43+n+m extensions |
| 2515 | */ |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 2516 | *p++ = (unsigned char) ssl->session_negotiate->id_len; |
| 2517 | memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len ); |
| 2518 | p += ssl->session_negotiate->id_len; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2519 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2520 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) ); |
| 2521 | MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n ); |
| 2522 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed", |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 2523 | ssl->handshake->resume ? "a" : "no" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2524 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2525 | *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 ); |
| 2526 | *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite ); |
| 2527 | *p++ = (unsigned char)( ssl->session_negotiate->compression ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2528 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2529 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s", |
| 2530 | mbedtls_ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) ); |
| 2531 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X", |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2532 | ssl->session_negotiate->compression ) ); |
| 2533 | |
Janos Follath | c6dab2b | 2016-05-23 14:27:02 +0100 | [diff] [blame] | 2534 | /* Do not write the extensions if the protocol is SSLv3 */ |
| 2535 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 2536 | if( ( ssl->major_ver != 3 ) || ( ssl->minor_ver != 0 ) ) |
| 2537 | { |
| 2538 | #endif |
| 2539 | |
Manuel Pégourié-Gonnard | f11a6d7 | 2013-07-17 11:17:14 +0200 | [diff] [blame] | 2540 | /* |
| 2541 | * First write extensions, then the total length |
| 2542 | */ |
| 2543 | ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen ); |
| 2544 | ext_len += olen; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2545 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2546 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 7bb7899 | 2013-07-17 13:50:08 +0200 | [diff] [blame] | 2547 | ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen ); |
| 2548 | ext_len += olen; |
Paul Bakker | 05decb2 | 2013-08-15 13:33:48 +0200 | [diff] [blame] | 2549 | #endif |
Manuel Pégourié-Gonnard | 7bb7899 | 2013-07-17 13:50:08 +0200 | [diff] [blame] | 2550 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2551 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 2552 | ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen ); |
| 2553 | ext_len += olen; |
Paul Bakker | 1f2bc62 | 2013-08-15 13:45:55 +0200 | [diff] [blame] | 2554 | #endif |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 2555 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2556 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2557 | ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen ); |
| 2558 | ext_len += olen; |
| 2559 | #endif |
| 2560 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2561 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2562 | ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen ); |
| 2563 | ext_len += olen; |
| 2564 | #endif |
| 2565 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2566 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 2567 | ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen ); |
| 2568 | ext_len += olen; |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 2569 | #endif |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 2570 | |
Manuel Pégourié-Gonnard | f472179 | 2015-09-15 10:53:51 +0200 | [diff] [blame] | 2571 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 2572 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 2573 | ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen ); |
| 2574 | ext_len += olen; |
| 2575 | #endif |
| 2576 | |
Manuel Pégourié-Gonnard | 55c7f99 | 2015-09-16 15:35:27 +0200 | [diff] [blame] | 2577 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 2578 | ssl_write_ecjpake_kkpp_ext( ssl, p + 2 + ext_len, &olen ); |
| 2579 | ext_len += olen; |
| 2580 | #endif |
| 2581 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2582 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 2583 | ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen ); |
| 2584 | ext_len += olen; |
| 2585 | #endif |
| 2586 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2587 | 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] | 2588 | |
Paul Bakker | a703663 | 2014-04-30 10:15:38 +0200 | [diff] [blame] | 2589 | if( ext_len > 0 ) |
| 2590 | { |
| 2591 | *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF ); |
| 2592 | *p++ = (unsigned char)( ( ext_len ) & 0xFF ); |
| 2593 | p += ext_len; |
| 2594 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2595 | |
Janos Follath | c6dab2b | 2016-05-23 14:27:02 +0100 | [diff] [blame] | 2596 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 2597 | } |
| 2598 | #endif |
| 2599 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2600 | ssl->out_msglen = p - buf; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2601 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 2602 | ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2603 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2604 | ret = mbedtls_ssl_write_record( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2605 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2606 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2607 | |
| 2608 | return( ret ); |
| 2609 | } |
| 2610 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2611 | #if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \ |
| 2612 | !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \ |
Paul Bakker | 29f221f | 2016-07-22 13:49:02 +0100 | [diff] [blame] | 2613 | !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2614 | !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \ |
Paul Bakker | 29f221f | 2016-07-22 13:49:02 +0100 | [diff] [blame] | 2615 | !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)&& \ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2616 | !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) |
| 2617 | static int ssl_write_certificate_request( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2618 | { |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 2619 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 2620 | ssl->transform_negotiate->ciphersuite_info; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2621 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2622 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2623 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2624 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || |
| 2625 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK || |
| 2626 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK || |
Manuel Pégourié-Gonnard | 25dbeb0 | 2015-09-16 17:30:03 +0200 | [diff] [blame] | 2627 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK || |
| 2628 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2629 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2630 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2631 | ssl->state++; |
| 2632 | return( 0 ); |
| 2633 | } |
| 2634 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2635 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2636 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2637 | } |
| 2638 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2639 | static int ssl_write_certificate_request( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2640 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2641 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 2642 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 2643 | ssl->transform_negotiate->ciphersuite_info; |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 2644 | size_t dn_size, total_dn_size; /* excluding length bytes */ |
| 2645 | size_t ct_len, sa_len; /* including length bytes */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2646 | unsigned char *buf, *p; |
Manuel Pégourié-Gonnard | bc1babb | 2015-10-02 11:16:47 +0200 | [diff] [blame] | 2647 | const unsigned char * const end = ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2648 | const mbedtls_x509_crt *crt; |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 2649 | int authmode; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2650 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2651 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2652 | |
| 2653 | ssl->state++; |
| 2654 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 2655 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 2656 | if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET ) |
| 2657 | authmode = ssl->handshake->sni_authmode; |
| 2658 | else |
| 2659 | #endif |
| 2660 | authmode = ssl->conf->authmode; |
| 2661 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2662 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || |
| 2663 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK || |
| 2664 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK || |
| 2665 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK || |
Manuel Pégourié-Gonnard | 25dbeb0 | 2015-09-16 17:30:03 +0200 | [diff] [blame] | 2666 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE || |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 2667 | authmode == MBEDTLS_SSL_VERIFY_NONE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2668 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2669 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2670 | return( 0 ); |
| 2671 | } |
| 2672 | |
| 2673 | /* |
| 2674 | * 0 . 0 handshake type |
| 2675 | * 1 . 3 handshake length |
| 2676 | * 4 . 4 cert type count |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2677 | * 5 .. m-1 cert types |
| 2678 | * m .. m+1 sig alg length (TLS 1.2 only) |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 2679 | * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2680 | * n .. n+1 length of all DNs |
| 2681 | * n+2 .. n+3 length of DN 1 |
| 2682 | * n+4 .. ... Distinguished Name #1 |
| 2683 | * ... .. ... length of DN 2, etc. |
| 2684 | */ |
| 2685 | buf = ssl->out_msg; |
| 2686 | p = buf + 4; |
| 2687 | |
| 2688 | /* |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 2689 | * Supported certificate types |
| 2690 | * |
| 2691 | * ClientCertificateType certificate_types<1..2^8-1>; |
| 2692 | * enum { (255) } ClientCertificateType; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2693 | */ |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 2694 | ct_len = 0; |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2695 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2696 | #if defined(MBEDTLS_RSA_C) |
| 2697 | p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_RSA_SIGN; |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 2698 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2699 | #if defined(MBEDTLS_ECDSA_C) |
| 2700 | p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_ECDSA_SIGN; |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 2701 | #endif |
| 2702 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 2703 | p[0] = (unsigned char) ct_len++; |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 2704 | p += ct_len; |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2705 | |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 2706 | sa_len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2707 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2708 | /* |
| 2709 | * Add signature_algorithms for verify (TLS 1.2) |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2710 | * |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 2711 | * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>; |
| 2712 | * |
| 2713 | * struct { |
| 2714 | * HashAlgorithm hash; |
| 2715 | * SignatureAlgorithm signature; |
| 2716 | * } SignatureAndHashAlgorithm; |
| 2717 | * |
| 2718 | * enum { (255) } HashAlgorithm; |
| 2719 | * enum { (255) } SignatureAlgorithm; |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2720 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2721 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2722 | { |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 2723 | const int *cur; |
Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 2724 | |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 2725 | /* |
| 2726 | * Supported signature algorithms |
| 2727 | */ |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 2728 | for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ ) |
| 2729 | { |
| 2730 | unsigned char hash = mbedtls_ssl_hash_from_md_alg( *cur ); |
| 2731 | |
| 2732 | if( MBEDTLS_SSL_HASH_NONE == hash || mbedtls_ssl_set_calc_verify_md( ssl, hash ) ) |
| 2733 | continue; |
| 2734 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2735 | #if defined(MBEDTLS_RSA_C) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 2736 | p[2 + sa_len++] = hash; |
| 2737 | p[2 + sa_len++] = MBEDTLS_SSL_SIG_RSA; |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 2738 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2739 | #if defined(MBEDTLS_ECDSA_C) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 2740 | p[2 + sa_len++] = hash; |
| 2741 | p[2 + sa_len++] = MBEDTLS_SSL_SIG_ECDSA; |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 2742 | #endif |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 2743 | } |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2744 | |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 2745 | p[0] = (unsigned char)( sa_len >> 8 ); |
| 2746 | p[1] = (unsigned char)( sa_len ); |
| 2747 | sa_len += 2; |
| 2748 | p += sa_len; |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2749 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2750 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2751 | |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 2752 | /* |
| 2753 | * DistinguishedName certificate_authorities<0..2^16-1>; |
| 2754 | * opaque DistinguishedName<1..2^16-1>; |
| 2755 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2756 | p += 2; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2757 | |
Paul Bakker | bc3d984 | 2012-11-26 16:12:02 +0100 | [diff] [blame] | 2758 | total_dn_size = 0; |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 2759 | |
| 2760 | if( ssl->conf->cert_req_ca_list == MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2761 | { |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 2762 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 2763 | if( ssl->handshake->sni_ca_chain != NULL ) |
| 2764 | crt = ssl->handshake->sni_ca_chain; |
| 2765 | else |
| 2766 | #endif |
| 2767 | crt = ssl->conf->ca_chain; |
Manuel Pégourié-Gonnard | bc1babb | 2015-10-02 11:16:47 +0200 | [diff] [blame] | 2768 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 2769 | while( crt != NULL && crt->version != 0 ) |
Manuel Pégourié-Gonnard | bc1babb | 2015-10-02 11:16:47 +0200 | [diff] [blame] | 2770 | { |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 2771 | dn_size = crt->subject_raw.len; |
| 2772 | |
| 2773 | if( end < p || |
| 2774 | (size_t)( end - p ) < dn_size || |
| 2775 | (size_t)( end - p ) < 2 + dn_size ) |
| 2776 | { |
| 2777 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "skipping CAs: buffer too short" ) ); |
| 2778 | break; |
| 2779 | } |
| 2780 | |
| 2781 | *p++ = (unsigned char)( dn_size >> 8 ); |
| 2782 | *p++ = (unsigned char)( dn_size ); |
| 2783 | memcpy( p, crt->subject_raw.p, dn_size ); |
| 2784 | p += dn_size; |
| 2785 | |
| 2786 | MBEDTLS_SSL_DEBUG_BUF( 3, "requested DN", p - dn_size, dn_size ); |
| 2787 | |
| 2788 | total_dn_size += 2 + dn_size; |
| 2789 | crt = crt->next; |
Manuel Pégourié-Gonnard | bc1babb | 2015-10-02 11:16:47 +0200 | [diff] [blame] | 2790 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2791 | } |
| 2792 | |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2793 | ssl->out_msglen = p - buf; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2794 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 2795 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_REQUEST; |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 2796 | ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 ); |
| 2797 | 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] | 2798 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2799 | ret = mbedtls_ssl_write_record( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2800 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2801 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2802 | |
| 2803 | return( ret ); |
| 2804 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2805 | #endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED && |
| 2806 | !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED && |
Paul Bakker | 29f221f | 2016-07-22 13:49:02 +0100 | [diff] [blame] | 2807 | !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2808 | !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED && |
Paul Bakker | 29f221f | 2016-07-22 13:49:02 +0100 | [diff] [blame] | 2809 | !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2810 | !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2811 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2812 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ |
| 2813 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) |
| 2814 | 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] | 2815 | { |
| 2816 | int ret; |
| 2817 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2818 | 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] | 2819 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2820 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) ); |
| 2821 | return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH ); |
Manuel Pégourié-Gonnard | 5538970 | 2013-12-12 11:14:16 +0100 | [diff] [blame] | 2822 | } |
| 2823 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2824 | if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx, |
| 2825 | mbedtls_pk_ec( *mbedtls_ssl_own_key( ssl ) ), |
| 2826 | MBEDTLS_ECDH_OURS ) ) != 0 ) |
Manuel Pégourié-Gonnard | 5538970 | 2013-12-12 11:14:16 +0100 | [diff] [blame] | 2827 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2828 | MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret ); |
Manuel Pégourié-Gonnard | 5538970 | 2013-12-12 11:14:16 +0100 | [diff] [blame] | 2829 | return( ret ); |
| 2830 | } |
| 2831 | |
| 2832 | return( 0 ); |
| 2833 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2834 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || |
| 2835 | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */ |
Manuel Pégourié-Gonnard | 5538970 | 2013-12-12 11:14:16 +0100 | [diff] [blame] | 2836 | |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 2837 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) && \ |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 2838 | defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 2839 | static int ssl_resume_server_key_exchange( mbedtls_ssl_context *ssl, |
Gilles Peskine | 0fd90dd | 2018-04-26 07:41:09 +0200 | [diff] [blame] | 2840 | size_t *signature_len ) |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 2841 | { |
Gilles Peskine | 0fd90dd | 2018-04-26 07:41:09 +0200 | [diff] [blame] | 2842 | /* Append the signature to ssl->out_msg, leaving 2 bytes for the |
| 2843 | * signature length which will be added in ssl_write_server_key_exchange |
| 2844 | * after the call to ssl_prepare_server_key_exchange. |
| 2845 | * ssl_write_server_key_exchange also takes care of incrementing |
| 2846 | * ssl->out_msglen. */ |
| 2847 | unsigned char *sig_start = ssl->out_msg + ssl->out_msglen + 2; |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 2848 | size_t sig_max_len = ( ssl->out_buf + MBEDTLS_SSL_MAX_CONTENT_LEN |
Gilles Peskine | 0fd90dd | 2018-04-26 07:41:09 +0200 | [diff] [blame] | 2849 | - sig_start ); |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame^] | 2850 | int ret = ssl->conf->f_async_resume( ssl, |
Gilles Peskine | 0fd90dd | 2018-04-26 07:41:09 +0200 | [diff] [blame] | 2851 | sig_start, signature_len, sig_max_len ); |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 2852 | if( ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS ) |
| 2853 | { |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2854 | ssl->handshake->async_in_progress = 0; |
| 2855 | mbedtls_ssl_async_set_data( ssl, NULL ); |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 2856 | } |
Gilles Peskine | d3eb061 | 2018-01-08 17:07:44 +0100 | [diff] [blame] | 2857 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl_resume_server_key_exchange", ret ); |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 2858 | return( ret ); |
| 2859 | } |
| 2860 | #endif /* defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) && |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 2861 | defined(MBEDTLS_SSL_ASYNC_PRIVATE) */ |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 2862 | |
Gilles Peskine | d3eb061 | 2018-01-08 17:07:44 +0100 | [diff] [blame] | 2863 | /* Prepare the ServerKeyExchange message, up to and including |
Gilles Peskine | 168dae8 | 2018-04-25 23:35:42 +0200 | [diff] [blame] | 2864 | * calculating the signature if any, but excluding formatting the |
| 2865 | * signature and sending the message. */ |
Gilles Peskine | 7ab013a | 2018-01-08 17:04:16 +0100 | [diff] [blame] | 2866 | static int ssl_prepare_server_key_exchange( mbedtls_ssl_context *ssl, |
| 2867 | size_t *signature_len ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2868 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2869 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
Manuel Pégourié-Gonnard | 09258b9 | 2013-10-15 10:43:36 +0200 | [diff] [blame] | 2870 | ssl->transform_negotiate->ciphersuite_info; |
Gilles Peskine | fe1c093 | 2017-11-23 13:35:02 +0100 | [diff] [blame] | 2871 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME_PFS__ENABLED) |
Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 2872 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) |
Gilles Peskine | 3ce9b90 | 2018-01-06 01:34:21 +0100 | [diff] [blame] | 2873 | unsigned char *dig_signed = NULL; |
Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 2874 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED */ |
| 2875 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME_PFS__ENABLED */ |
Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 2876 | |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 2877 | (void) ciphersuite_info; /* unused in some configurations */ |
Gilles Peskine | 22e695f | 2018-04-26 00:22:50 +0200 | [diff] [blame] | 2878 | #if !defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) |
| 2879 | (void) signature_len; |
| 2880 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED */ |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2881 | |
Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 2882 | ssl->out_msglen = 4; /* header (type:1, length:3) to be written later */ |
| 2883 | |
Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 2884 | /* |
| 2885 | * |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 2886 | * Part 1: Provide key exchange parameters for chosen ciphersuite. |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 2887 | * |
| 2888 | */ |
| 2889 | |
| 2890 | /* |
| 2891 | * - ECJPAKE key exchanges |
| 2892 | */ |
Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 2893 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 2894 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ) |
| 2895 | { |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 2896 | int ret; |
Gilles Peskine | d3eb061 | 2018-01-08 17:07:44 +0100 | [diff] [blame] | 2897 | size_t len; |
Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 2898 | |
Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 2899 | ret = mbedtls_ecjpake_write_round_two( |
| 2900 | &ssl->handshake->ecjpake_ctx, |
| 2901 | ssl->out_msg + ssl->out_msglen, |
| 2902 | MBEDTLS_SSL_MAX_CONTENT_LEN - ssl->out_msglen, &len, |
| 2903 | ssl->conf->f_rng, ssl->conf->p_rng ); |
Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 2904 | if( ret != 0 ) |
| 2905 | { |
| 2906 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret ); |
| 2907 | return( ret ); |
| 2908 | } |
| 2909 | |
Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 2910 | ssl->out_msglen += len; |
Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 2911 | } |
| 2912 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
| 2913 | |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 2914 | /* |
| 2915 | * For (EC)DHE key exchanges with PSK, parameters are prefixed by support |
| 2916 | * identity hint (RFC 4279, Sec. 3). Until someone needs this feature, |
| 2917 | * we use empty support identity hints here. |
| 2918 | **/ |
| 2919 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) || \ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2920 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
| 2921 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK || |
| 2922 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2923 | { |
Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 2924 | ssl->out_msg[ssl->out_msglen++] = 0x00; |
| 2925 | ssl->out_msg[ssl->out_msglen++] = 0x00; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2926 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2927 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED || |
| 2928 | MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2929 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 2930 | /* |
Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 2931 | * - DHE key exchanges |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 2932 | */ |
| 2933 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__DHE_ENABLED) |
| 2934 | if( mbedtls_ssl_ciphersuite_uses_dhe( ciphersuite_info ) ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2935 | { |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 2936 | int ret; |
Gilles Peskine | d3eb061 | 2018-01-08 17:07:44 +0100 | [diff] [blame] | 2937 | size_t len; |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 2938 | |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 2939 | if( ssl->conf->dhm_P.p == NULL || ssl->conf->dhm_G.p == NULL ) |
| 2940 | { |
| 2941 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no DH parameters set" ) ); |
| 2942 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2943 | } |
| 2944 | |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2945 | /* |
| 2946 | * Ephemeral DH parameters: |
| 2947 | * |
| 2948 | * struct { |
| 2949 | * opaque dh_p<1..2^16-1>; |
| 2950 | * opaque dh_g<1..2^16-1>; |
| 2951 | * opaque dh_Ys<1..2^16-1>; |
| 2952 | * } ServerDHParams; |
| 2953 | */ |
Hanno Becker | ab74056 | 2017-10-04 13:15:37 +0100 | [diff] [blame] | 2954 | if( ( ret = mbedtls_dhm_set_group( &ssl->handshake->dhm_ctx, |
| 2955 | &ssl->conf->dhm_P, |
| 2956 | &ssl->conf->dhm_G ) ) != 0 ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2957 | { |
Hanno Becker | ab74056 | 2017-10-04 13:15:37 +0100 | [diff] [blame] | 2958 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_set_group", ret ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2959 | return( ret ); |
| 2960 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2961 | |
Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 2962 | if( ( ret = mbedtls_dhm_make_params( |
| 2963 | &ssl->handshake->dhm_ctx, |
| 2964 | (int) mbedtls_mpi_size( &ssl->handshake->dhm_ctx.P ), |
| 2965 | ssl->out_msg + ssl->out_msglen, &len, |
| 2966 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2967 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2968 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_params", ret ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2969 | return( ret ); |
| 2970 | } |
| 2971 | |
Hanno Becker | 4cb1f4d | 2017-10-10 15:59:57 +0100 | [diff] [blame] | 2972 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) |
Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 2973 | dig_signed = ssl->out_msg + ssl->out_msglen; |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 2974 | #endif |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2975 | |
Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 2976 | ssl->out_msglen += len; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2977 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2978 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X ); |
| 2979 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P ); |
| 2980 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G ); |
| 2981 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2982 | } |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 2983 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__DHE_ENABLED */ |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2984 | |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 2985 | /* |
Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 2986 | * - ECDHE key exchanges |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 2987 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2988 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__ECDHE_ENABLED) |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 2989 | if( mbedtls_ssl_ciphersuite_uses_ecdhe( ciphersuite_info ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2990 | { |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2991 | /* |
| 2992 | * Ephemeral ECDH parameters: |
| 2993 | * |
| 2994 | * struct { |
| 2995 | * ECParameters curve_params; |
| 2996 | * ECPoint public; |
| 2997 | * } ServerECDHParams; |
| 2998 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2999 | const mbedtls_ecp_curve_info **curve = NULL; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3000 | const mbedtls_ecp_group_id *gid; |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3001 | int ret; |
Gilles Peskine | d3eb061 | 2018-01-08 17:07:44 +0100 | [diff] [blame] | 3002 | size_t len; |
Gergely Budai | 987bfb5 | 2014-01-19 21:48:42 +0100 | [diff] [blame] | 3003 | |
Manuel Pégourié-Gonnard | c3f6b62c | 2014-02-06 10:13:09 +0100 | [diff] [blame] | 3004 | /* Match our preference list against the offered curves */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3005 | for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ ) |
Manuel Pégourié-Gonnard | c3f6b62c | 2014-02-06 10:13:09 +0100 | [diff] [blame] | 3006 | for( curve = ssl->handshake->curves; *curve != NULL; curve++ ) |
| 3007 | if( (*curve)->grp_id == *gid ) |
| 3008 | goto curve_matching_done; |
| 3009 | |
| 3010 | curve_matching_done: |
Manuel Pégourié-Gonnard | b86145e | 2015-06-23 14:11:39 +0200 | [diff] [blame] | 3011 | if( curve == NULL || *curve == NULL ) |
Gergely Budai | 987bfb5 | 2014-01-19 21:48:42 +0100 | [diff] [blame] | 3012 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3013 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) ); |
| 3014 | return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN ); |
Gergely Budai | 987bfb5 | 2014-01-19 21:48:42 +0100 | [diff] [blame] | 3015 | } |
Manuel Pégourié-Gonnard | de05390 | 2014-02-04 13:58:39 +0100 | [diff] [blame] | 3016 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3017 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) ); |
Gergely Budai | 987bfb5 | 2014-01-19 21:48:42 +0100 | [diff] [blame] | 3018 | |
Manuel Pégourié-Gonnard | e3a062b | 2015-05-11 18:46:47 +0200 | [diff] [blame] | 3019 | if( ( ret = mbedtls_ecp_group_load( &ssl->handshake->ecdh_ctx.grp, |
Manuel Pégourié-Gonnard | c3f6b62c | 2014-02-06 10:13:09 +0100 | [diff] [blame] | 3020 | (*curve)->grp_id ) ) != 0 ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3021 | { |
Manuel Pégourié-Gonnard | e3a062b | 2015-05-11 18:46:47 +0200 | [diff] [blame] | 3022 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecp_group_load", ret ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3023 | return( ret ); |
| 3024 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3025 | |
Gilles Peskine | fe1c093 | 2017-11-23 13:35:02 +0100 | [diff] [blame] | 3026 | if( ( ret = mbedtls_ecdh_make_params( |
| 3027 | &ssl->handshake->ecdh_ctx, &len, |
Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 3028 | ssl->out_msg + ssl->out_msglen, |
| 3029 | MBEDTLS_SSL_MAX_CONTENT_LEN - ssl->out_msglen, |
Gilles Peskine | fe1c093 | 2017-11-23 13:35:02 +0100 | [diff] [blame] | 3030 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3031 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3032 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_params", ret ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3033 | return( ret ); |
| 3034 | } |
| 3035 | |
Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3036 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) |
Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 3037 | dig_signed = ssl->out_msg + ssl->out_msglen; |
Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3038 | #endif |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3039 | |
Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 3040 | ssl->out_msglen += len; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3041 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3042 | MBEDTLS_SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3043 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3044 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__ECDHE_ENABLED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3045 | |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3046 | /* |
Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3047 | * |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3048 | * Part 2: For key exchanges involving the server signing the |
Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3049 | * exchange parameters, compute and add the signature here. |
| 3050 | * |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3051 | */ |
| 3052 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) |
| 3053 | if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3054 | { |
Gilles Peskine | 1004c19 | 2018-01-08 16:59:14 +0100 | [diff] [blame] | 3055 | 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] | 3056 | size_t hashlen = 0; |
Gilles Peskine | e1efdf9 | 2018-01-05 21:18:37 +0100 | [diff] [blame] | 3057 | unsigned char hash[MBEDTLS_MD_MAX_SIZE]; |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3058 | int ret; |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 3059 | |
Manuel Pégourié-Gonnard | abae74c | 2013-08-20 13:53:44 +0200 | [diff] [blame] | 3060 | /* |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3061 | * 2.1: Choose hash algorithm: |
Hanno Becker | 4cb1f4d | 2017-10-10 15:59:57 +0100 | [diff] [blame] | 3062 | * A: For TLS 1.2, obey signature-hash-algorithm extension |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3063 | * to choose appropriate hash. |
| 3064 | * B: For SSL3, TLS1.0, TLS1.1 and ECDHE_ECDSA, use SHA1 |
| 3065 | * (RFC 4492, Sec. 5.4) |
| 3066 | * 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] | 3067 | */ |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 3068 | |
| 3069 | mbedtls_md_type_t md_alg; |
| 3070 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3071 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3072 | mbedtls_pk_type_t sig_alg = |
| 3073 | mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3074 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3075 | { |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3076 | /* A: For TLS 1.2, obey signature-hash-algorithm extension |
| 3077 | * (RFC 5246, Sec. 7.4.1.4.1). */ |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 3078 | if( sig_alg == MBEDTLS_PK_NONE || |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3079 | ( md_alg = mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs, |
| 3080 | sig_alg ) ) == MBEDTLS_MD_NONE ) |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3081 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3082 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Hanno Becker | 4cb1f4d | 2017-10-10 15:59:57 +0100 | [diff] [blame] | 3083 | /* (... because we choose a cipher suite |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3084 | * only if there is a matching hash.) */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3085 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3086 | } |
| 3087 | } |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 3088 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3089 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3090 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 3091 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3092 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ) |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3093 | { |
Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3094 | /* B: Default hash SHA1 */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3095 | md_alg = MBEDTLS_MD_SHA1; |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3096 | } |
| 3097 | else |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3098 | #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \ |
| 3099 | MBEDTLS_SSL_PROTO_TLS1_1 */ |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3100 | { |
Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3101 | /* C: MD5 + SHA1 */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3102 | md_alg = MBEDTLS_MD_NONE; |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3103 | } |
| 3104 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 3105 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "pick hash algorithm %d for signing", md_alg ) ); |
| 3106 | |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3107 | /* |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3108 | * 2.2: Compute the hash to be signed |
Manuel Pégourié-Gonnard | abae74c | 2013-08-20 13:53:44 +0200 | [diff] [blame] | 3109 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3110 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 3111 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 3112 | if( md_alg == MBEDTLS_MD_NONE ) |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 3113 | { |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3114 | hashlen = 36; |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 3115 | ret = mbedtls_ssl_get_key_exchange_md_ssl_tls( ssl, hash, |
| 3116 | dig_signed, |
| 3117 | dig_signed_len ); |
| 3118 | if( ret != 0 ) |
| 3119 | return( ret ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3120 | } |
| 3121 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3122 | #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \ |
| 3123 | MBEDTLS_SSL_PROTO_TLS1_1 */ |
| 3124 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 3125 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3126 | if( md_alg != MBEDTLS_MD_NONE ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3127 | { |
Gilles Peskine | ca1d742 | 2018-04-24 11:53:22 +0200 | [diff] [blame] | 3128 | 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] | 3129 | dig_signed, |
| 3130 | dig_signed_len, |
| 3131 | md_alg ); |
| 3132 | if( ret != 0 ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3133 | return( ret ); |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 3134 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 3135 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3136 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 3137 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 3138 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3139 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3140 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 3141 | } |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 3142 | |
Gilles Peskine | ebd652f | 2018-01-05 21:18:59 +0100 | [diff] [blame] | 3143 | MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3144 | |
Manuel Pégourié-Gonnard | abae74c | 2013-08-20 13:53:44 +0200 | [diff] [blame] | 3145 | /* |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3146 | * 2.3: Compute and add the signature |
Manuel Pégourié-Gonnard | abae74c | 2013-08-20 13:53:44 +0200 | [diff] [blame] | 3147 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3148 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3149 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 3150 | { |
Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3151 | /* |
| 3152 | * For TLS 1.2, we need to specify signature and hash algorithm |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 3153 | * explicitly through a prefix to the signature. |
| 3154 | * |
| 3155 | * struct { |
| 3156 | * HashAlgorithm hash; |
| 3157 | * SignatureAlgorithm signature; |
| 3158 | * } SignatureAndHashAlgorithm; |
| 3159 | * |
| 3160 | * struct { |
| 3161 | * SignatureAndHashAlgorithm algorithm; |
| 3162 | * opaque signature<0..2^16-1>; |
| 3163 | * } DigitallySigned; |
| 3164 | * |
| 3165 | */ |
| 3166 | |
Gilles Peskine | 1004c19 | 2018-01-08 16:59:14 +0100 | [diff] [blame] | 3167 | ssl->out_msg[ssl->out_msglen++] = |
| 3168 | mbedtls_ssl_hash_from_md_alg( md_alg ); |
| 3169 | ssl->out_msg[ssl->out_msglen++] = |
| 3170 | mbedtls_ssl_sig_from_pk_alg( sig_alg ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3171 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3172 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3173 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3174 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 4bf9a28 | 2018-01-05 21:20:50 +0100 | [diff] [blame] | 3175 | if( ssl->conf->f_async_sign_start != NULL ) |
| 3176 | { |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame^] | 3177 | ret = ssl->conf->f_async_sign_start( ssl, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3178 | mbedtls_ssl_own_cert( ssl ), |
| 3179 | md_alg, hash, hashlen ); |
Gilles Peskine | 4bf9a28 | 2018-01-05 21:20:50 +0100 | [diff] [blame] | 3180 | switch( ret ) |
| 3181 | { |
| 3182 | case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH: |
| 3183 | /* act as if f_async_sign was null */ |
| 3184 | break; |
| 3185 | case 0: |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3186 | ssl->handshake->async_in_progress = 1; |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3187 | return( ssl_resume_server_key_exchange( ssl, signature_len ) ); |
Gilles Peskine | 4bf9a28 | 2018-01-05 21:20:50 +0100 | [diff] [blame] | 3188 | case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS: |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3189 | ssl->handshake->async_in_progress = 1; |
Gilles Peskine | 4bf9a28 | 2018-01-05 21:20:50 +0100 | [diff] [blame] | 3190 | return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS ); |
| 3191 | default: |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3192 | MBEDTLS_SSL_DEBUG_RET( 1, "f_async_sign_start", ret ); |
Gilles Peskine | 4bf9a28 | 2018-01-05 21:20:50 +0100 | [diff] [blame] | 3193 | return( ret ); |
| 3194 | } |
| 3195 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3196 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 4bf9a28 | 2018-01-05 21:20:50 +0100 | [diff] [blame] | 3197 | |
| 3198 | if( mbedtls_ssl_own_key( ssl ) == NULL ) |
| 3199 | { |
| 3200 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key" ) ); |
| 3201 | return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ); |
| 3202 | } |
| 3203 | |
Gilles Peskine | 0fd90dd | 2018-04-26 07:41:09 +0200 | [diff] [blame] | 3204 | /* Append the signature to ssl->out_msg, leaving 2 bytes for the |
| 3205 | * signature length which will be added in ssl_write_server_key_exchange |
| 3206 | * after the call to ssl_prepare_server_key_exchange. |
| 3207 | * ssl_write_server_key_exchange also takes care of incrementing |
| 3208 | * ssl->out_msglen. */ |
Gilles Peskine | 1004c19 | 2018-01-08 16:59:14 +0100 | [diff] [blame] | 3209 | if( ( ret = mbedtls_pk_sign( mbedtls_ssl_own_key( ssl ), |
| 3210 | md_alg, hash, hashlen, |
| 3211 | ssl->out_msg + ssl->out_msglen + 2, |
Gilles Peskine | 7ab013a | 2018-01-08 17:04:16 +0100 | [diff] [blame] | 3212 | signature_len, |
Gilles Peskine | 1004c19 | 2018-01-08 16:59:14 +0100 | [diff] [blame] | 3213 | ssl->conf->f_rng, |
| 3214 | ssl->conf->p_rng ) ) != 0 ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3215 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3216 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 3217 | return( ret ); |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 3218 | } |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3219 | } |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3220 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3221 | |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3222 | return( 0 ); |
| 3223 | } |
| 3224 | |
Gilles Peskine | d3eb061 | 2018-01-08 17:07:44 +0100 | [diff] [blame] | 3225 | /* Prepare the ServerKeyExchange message and send it. For ciphersuites |
Gilles Peskine | 168dae8 | 2018-04-25 23:35:42 +0200 | [diff] [blame] | 3226 | * that do not include a ServerKeyExchange message, do nothing. Either |
| 3227 | * way, if successful, move on to the next step in the SSL state |
| 3228 | * machine. */ |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3229 | static int ssl_write_server_key_exchange( mbedtls_ssl_context *ssl ) |
| 3230 | { |
| 3231 | int ret; |
Gilles Peskine | 7ab013a | 2018-01-08 17:04:16 +0100 | [diff] [blame] | 3232 | size_t signature_len = 0; |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3233 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME_NON_PFS__ENABLED) |
| 3234 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 3235 | ssl->transform_negotiate->ciphersuite_info; |
Gilles Peskine | f112725 | 2018-04-24 13:05:39 +0200 | [diff] [blame] | 3236 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME_NON_PFS__ENABLED */ |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3237 | |
Gilles Peskine | d3eb061 | 2018-01-08 17:07:44 +0100 | [diff] [blame] | 3238 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) ); |
| 3239 | |
| 3240 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME_NON_PFS__ENABLED) |
| 3241 | /* Extract static ECDH parameters and abort if ServerKeyExchange |
| 3242 | * is not needed. */ |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3243 | if( mbedtls_ssl_ciphersuite_no_pfs( ciphersuite_info ) ) |
| 3244 | { |
| 3245 | /* For suites involving ECDH, extract DH parameters |
| 3246 | * from certificate at this point. */ |
| 3247 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__ECDH_ENABLED) |
| 3248 | if( mbedtls_ssl_ciphersuite_uses_ecdh( ciphersuite_info ) ) |
| 3249 | { |
| 3250 | ssl_get_ecdh_params_from_cert( ssl ); |
| 3251 | } |
| 3252 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__ECDH_ENABLED */ |
| 3253 | |
| 3254 | /* Key exchanges not involving ephemeral keys don't use |
| 3255 | * ServerKeyExchange, so end here. */ |
| 3256 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) ); |
| 3257 | ssl->state++; |
| 3258 | return( 0 ); |
| 3259 | } |
Gilles Peskine | f112725 | 2018-04-24 13:05:39 +0200 | [diff] [blame] | 3260 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME_NON_PFS__ENABLED */ |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3261 | |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3262 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) && \ |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3263 | defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | d3eb061 | 2018-01-08 17:07:44 +0100 | [diff] [blame] | 3264 | /* If we have already prepared the message and there is an ongoing |
Gilles Peskine | 168dae8 | 2018-04-25 23:35:42 +0200 | [diff] [blame] | 3265 | * signature operation, resume signing. */ |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3266 | if( ssl->handshake->async_in_progress != 0 ) |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3267 | { |
| 3268 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "resuming signature operation" ) ); |
| 3269 | ret = ssl_resume_server_key_exchange( ssl, &signature_len ); |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3270 | } |
| 3271 | else |
| 3272 | #endif /* defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) && |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3273 | defined(MBEDTLS_SSL_ASYNC_PRIVATE) */ |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3274 | { |
| 3275 | /* ServerKeyExchange is needed. Prepare the message. */ |
| 3276 | ret = ssl_prepare_server_key_exchange( ssl, &signature_len ); |
Gilles Peskine | d3eb061 | 2018-01-08 17:07:44 +0100 | [diff] [blame] | 3277 | } |
| 3278 | |
| 3279 | if( ret != 0 ) |
| 3280 | { |
Gilles Peskine | ad28bf0 | 2018-04-26 00:19:16 +0200 | [diff] [blame] | 3281 | /* If we're starting to write a new message, set ssl->out_msglen |
| 3282 | * to 0. But if we're resuming after an asynchronous message, |
| 3283 | * out_msglen is the amount of data written so far and mst be |
| 3284 | * preserved. */ |
Gilles Peskine | d3eb061 | 2018-01-08 17:07:44 +0100 | [diff] [blame] | 3285 | if( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS ) |
| 3286 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server key exchange (pending)" ) ); |
| 3287 | else |
| 3288 | ssl->out_msglen = 0; |
| 3289 | return( ret ); |
Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3290 | } |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3291 | |
Gilles Peskine | 7ab013a | 2018-01-08 17:04:16 +0100 | [diff] [blame] | 3292 | /* If there is a signature, write its length. |
Gilles Peskine | 168dae8 | 2018-04-25 23:35:42 +0200 | [diff] [blame] | 3293 | * ssl_prepare_server_key_exchange already wrote the signature |
| 3294 | * itself at its proper place in the output buffer. */ |
Gilles Peskine | 7ab013a | 2018-01-08 17:04:16 +0100 | [diff] [blame] | 3295 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) |
| 3296 | if( signature_len != 0 ) |
| 3297 | { |
| 3298 | ssl->out_msg[ssl->out_msglen++] = (unsigned char)( signature_len >> 8 ); |
| 3299 | ssl->out_msg[ssl->out_msglen++] = (unsigned char)( signature_len ); |
| 3300 | |
| 3301 | MBEDTLS_SSL_DEBUG_BUF( 3, "my signature", |
| 3302 | ssl->out_msg + ssl->out_msglen, |
| 3303 | signature_len ); |
| 3304 | |
| 3305 | /* Skip over the already-written signature */ |
| 3306 | ssl->out_msglen += signature_len; |
| 3307 | } |
| 3308 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED */ |
| 3309 | |
Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3310 | /* Add header and send. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3311 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 3312 | ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3313 | |
| 3314 | ssl->state++; |
| 3315 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3316 | if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3317 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3318 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3319 | return( ret ); |
| 3320 | } |
| 3321 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3322 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3323 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3324 | } |
| 3325 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3326 | static int ssl_write_server_hello_done( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3327 | { |
| 3328 | int ret; |
| 3329 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3330 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3331 | |
| 3332 | ssl->out_msglen = 4; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3333 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 3334 | ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO_DONE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3335 | |
| 3336 | ssl->state++; |
| 3337 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3338 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3339 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3340 | mbedtls_ssl_send_flight_completed( ssl ); |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3341 | #endif |
| 3342 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3343 | if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3344 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3345 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3346 | return( ret ); |
| 3347 | } |
| 3348 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3349 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3350 | |
| 3351 | return( 0 ); |
| 3352 | } |
| 3353 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3354 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \ |
| 3355 | defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 3356 | 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] | 3357 | const unsigned char *end ) |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3358 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3359 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3360 | size_t n; |
| 3361 | |
| 3362 | /* |
| 3363 | * Receive G^Y mod P, premaster = (G^Y)^X mod P |
| 3364 | */ |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3365 | if( *p + 2 > end ) |
| 3366 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3367 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); |
| 3368 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3369 | } |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3370 | |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3371 | n = ( (*p)[0] << 8 ) | (*p)[1]; |
| 3372 | *p += 2; |
| 3373 | |
Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 3374 | if( *p + n > end ) |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3375 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3376 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); |
| 3377 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3378 | } |
| 3379 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3380 | 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] | 3381 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3382 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_read_public", ret ); |
| 3383 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP ); |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3384 | } |
| 3385 | |
Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 3386 | *p += n; |
| 3387 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3388 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY ); |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3389 | |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3390 | return( ret ); |
| 3391 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3392 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED || |
| 3393 | MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3394 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3395 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \ |
| 3396 | defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3397 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3398 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3399 | static int ssl_resume_decrypt_pms( mbedtls_ssl_context *ssl, |
| 3400 | unsigned char *peer_pms, |
| 3401 | size_t *peer_pmslen, |
| 3402 | size_t peer_pmssize ) |
| 3403 | { |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame^] | 3404 | int ret = ssl->conf->f_async_resume( ssl, |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3405 | peer_pms, peer_pmslen, peer_pmssize ); |
| 3406 | if( ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS ) |
| 3407 | { |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3408 | ssl->handshake->async_in_progress = 0; |
| 3409 | mbedtls_ssl_async_set_data( ssl, NULL ); |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3410 | } |
| 3411 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl_decrypt_encrypted_pms", ret ); |
| 3412 | return( ret ); |
| 3413 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3414 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3415 | |
Gilles Peskine | bcd98a5 | 2018-01-11 21:30:40 +0100 | [diff] [blame] | 3416 | static int ssl_decrypt_encrypted_pms( mbedtls_ssl_context *ssl, |
| 3417 | const unsigned char *p, |
| 3418 | const unsigned char *end, |
| 3419 | unsigned char *peer_pms, |
| 3420 | size_t *peer_pmslen, |
| 3421 | size_t peer_pmssize ) |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3422 | { |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 3423 | int ret; |
Gilles Peskine | 422ccab | 2018-01-11 18:29:01 +0100 | [diff] [blame] | 3424 | mbedtls_pk_context *private_key = mbedtls_ssl_own_key( ssl ); |
| 3425 | mbedtls_pk_context *public_key = &mbedtls_ssl_own_cert( ssl )->pk; |
| 3426 | size_t len = mbedtls_pk_get_len( public_key ); |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3427 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3428 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3429 | /* 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] | 3430 | * decryption operation, resume signing. */ |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3431 | if( ssl->handshake->async_in_progress != 0 ) |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3432 | { |
| 3433 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "resuming decryption operation" ) ); |
| 3434 | return( ssl_resume_decrypt_pms( ssl, |
| 3435 | peer_pms, peer_pmslen, peer_pmssize ) ); |
| 3436 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3437 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3438 | |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3439 | /* |
Gilles Peskine | 422ccab | 2018-01-11 18:29:01 +0100 | [diff] [blame] | 3440 | * Prepare to decrypt the premaster using own private RSA key |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3441 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3442 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 3443 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3444 | if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3445 | { |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 3446 | if( *p++ != ( ( len >> 8 ) & 0xFF ) || |
| 3447 | *p++ != ( ( len ) & 0xFF ) ) |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3448 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3449 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); |
| 3450 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3451 | } |
| 3452 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 3453 | #endif |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3454 | |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 3455 | if( p + len != end ) |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3456 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3457 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); |
| 3458 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3459 | } |
| 3460 | |
Gilles Peskine | 422ccab | 2018-01-11 18:29:01 +0100 | [diff] [blame] | 3461 | /* |
| 3462 | * Decrypt the premaster secret |
| 3463 | */ |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3464 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3465 | if( ssl->conf->f_async_decrypt_start != NULL ) |
| 3466 | { |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame^] | 3467 | ret = ssl->conf->f_async_decrypt_start( ssl, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3468 | mbedtls_ssl_own_cert( ssl ), |
| 3469 | p, len ); |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3470 | switch( ret ) |
| 3471 | { |
| 3472 | case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH: |
| 3473 | /* act as if f_async_decrypt_start was null */ |
| 3474 | break; |
| 3475 | case 0: |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3476 | ssl->handshake->async_in_progress = 1; |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3477 | return( ssl_resume_decrypt_pms( ssl, |
| 3478 | peer_pms, |
| 3479 | peer_pmslen, |
| 3480 | peer_pmssize ) ); |
| 3481 | case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS: |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3482 | ssl->handshake->async_in_progress = 1; |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3483 | return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS ); |
| 3484 | default: |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3485 | MBEDTLS_SSL_DEBUG_RET( 1, "f_async_decrypt_start", ret ); |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3486 | return( ret ); |
| 3487 | } |
| 3488 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3489 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3490 | |
Gilles Peskine | 422ccab | 2018-01-11 18:29:01 +0100 | [diff] [blame] | 3491 | if( ! mbedtls_pk_can_do( private_key, MBEDTLS_PK_RSA ) ) |
| 3492 | { |
Gilles Peskine | 422ccab | 2018-01-11 18:29:01 +0100 | [diff] [blame] | 3493 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) ); |
| 3494 | return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ); |
| 3495 | } |
| 3496 | |
| 3497 | ret = mbedtls_pk_decrypt( private_key, p, len, |
Gilles Peskine | bcd98a5 | 2018-01-11 21:30:40 +0100 | [diff] [blame] | 3498 | peer_pms, peer_pmslen, peer_pmssize, |
| 3499 | ssl->conf->f_rng, ssl->conf->p_rng ); |
| 3500 | return( ret ); |
| 3501 | } |
| 3502 | |
| 3503 | static int ssl_parse_encrypted_pms( mbedtls_ssl_context *ssl, |
| 3504 | const unsigned char *p, |
| 3505 | const unsigned char *end, |
| 3506 | size_t pms_offset ) |
| 3507 | { |
| 3508 | int ret; |
| 3509 | unsigned char *pms = ssl->handshake->premaster + pms_offset; |
| 3510 | unsigned char ver[2]; |
| 3511 | unsigned char fake_pms[48], peer_pms[48]; |
| 3512 | unsigned char mask; |
| 3513 | size_t i, peer_pmslen; |
| 3514 | unsigned int diff; |
| 3515 | |
| 3516 | ret = ssl_decrypt_encrypted_pms( ssl, p, end, |
| 3517 | peer_pms, |
| 3518 | &peer_pmslen, |
| 3519 | sizeof( peer_pms ) ); |
| 3520 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3521 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3522 | if ( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS ) |
| 3523 | return( ret ); |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3524 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3525 | |
Gilles Peskine | 2e33337 | 2018-04-24 13:22:10 +0200 | [diff] [blame] | 3526 | mbedtls_ssl_write_version( ssl->handshake->max_major_ver, |
| 3527 | ssl->handshake->max_minor_ver, |
| 3528 | ssl->conf->transport, ver ); |
| 3529 | |
| 3530 | /* Avoid data-dependent branches while checking for invalid |
| 3531 | * padding, to protect against timing-based Bleichenbacher-type |
| 3532 | * attacks. */ |
| 3533 | diff = (unsigned int) ret; |
| 3534 | diff |= peer_pmslen ^ 48; |
| 3535 | diff |= peer_pms[0] ^ ver[0]; |
| 3536 | diff |= peer_pms[1] ^ ver[1]; |
| 3537 | |
| 3538 | /* mask = diff ? 0xff : 0x00 using bit operations to avoid branches */ |
| 3539 | /* MSVC has a warning about unary minus on unsigned, but this is |
| 3540 | * well-defined and precisely what we want to do here */ |
| 3541 | #if defined(_MSC_VER) |
| 3542 | #pragma warning( push ) |
| 3543 | #pragma warning( disable : 4146 ) |
| 3544 | #endif |
| 3545 | mask = - ( ( diff | - diff ) >> ( sizeof( unsigned int ) * 8 - 1 ) ); |
| 3546 | #if defined(_MSC_VER) |
| 3547 | #pragma warning( pop ) |
| 3548 | #endif |
| 3549 | |
Gilles Peskine | bcd98a5 | 2018-01-11 21:30:40 +0100 | [diff] [blame] | 3550 | /* |
| 3551 | * Protection against Bleichenbacher's attack: invalid PKCS#1 v1.5 padding |
| 3552 | * must not cause the connection to end immediately; instead, send a |
| 3553 | * bad_record_mac later in the handshake. |
| 3554 | * To protect against timing-based variants of the attack, we must |
| 3555 | * not have any branch that depends on whether the decryption was |
| 3556 | * successful. In particular, always generate the fake premaster secret, |
| 3557 | * regardless of whether it will ultimately influence the output or not. |
| 3558 | */ |
| 3559 | ret = ssl->conf->f_rng( ssl->conf->p_rng, fake_pms, sizeof( fake_pms ) ); |
| 3560 | if( ret != 0 ) |
| 3561 | { |
Gilles Peskine | e141638 | 2018-04-26 10:23:21 +0200 | [diff] [blame] | 3562 | /* It's ok to abort on an RNG failure, since this does not reveal |
| 3563 | * anything about the RSA decryption. */ |
Gilles Peskine | bcd98a5 | 2018-01-11 21:30:40 +0100 | [diff] [blame] | 3564 | return( ret ); |
| 3565 | } |
| 3566 | |
Manuel Pégourié-Gonnard | 331ba57 | 2015-04-20 12:33:57 +0100 | [diff] [blame] | 3567 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Manuel Pégourié-Gonnard | ce60fbe | 2015-04-15 16:45:52 +0200 | [diff] [blame] | 3568 | if( diff != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3569 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); |
Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 3570 | #endif |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3571 | |
Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 3572 | if( sizeof( ssl->handshake->premaster ) < pms_offset || |
| 3573 | sizeof( ssl->handshake->premaster ) - pms_offset < 48 ) |
| 3574 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3575 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3576 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3577 | } |
Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 3578 | ssl->handshake->pmslen = 48; |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3579 | |
Gilles Peskine | 422ccab | 2018-01-11 18:29:01 +0100 | [diff] [blame] | 3580 | /* Set pms to either the true or the fake PMS, without |
| 3581 | * data-dependent branches. */ |
Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 3582 | for( i = 0; i < ssl->handshake->pmslen; i++ ) |
| 3583 | pms[i] = ( mask & fake_pms[i] ) | ( (~mask) & peer_pms[i] ); |
| 3584 | |
| 3585 | return( 0 ); |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3586 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3587 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED || |
| 3588 | MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3589 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3590 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
| 3591 | 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] | 3592 | const unsigned char *end ) |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 3593 | { |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 3594 | int ret = 0; |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 3595 | size_t n; |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 3596 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3597 | if( ssl->conf->f_psk == NULL && |
| 3598 | ( ssl->conf->psk == NULL || ssl->conf->psk_identity == NULL || |
| 3599 | ssl->conf->psk_identity_len == 0 || ssl->conf->psk_len == 0 ) ) |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 3600 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3601 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) ); |
| 3602 | return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ); |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 3603 | } |
| 3604 | |
| 3605 | /* |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3606 | * Receive client pre-shared key identity name |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 3607 | */ |
Hanno Becker | 83c9f49 | 2017-06-26 13:52:14 +0100 | [diff] [blame] | 3608 | if( end - *p < 2 ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3609 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3610 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); |
| 3611 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3612 | } |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 3613 | |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3614 | n = ( (*p)[0] << 8 ) | (*p)[1]; |
| 3615 | *p += 2; |
| 3616 | |
Hanno Becker | 83c9f49 | 2017-06-26 13:52:14 +0100 | [diff] [blame] | 3617 | if( n < 1 || n > 65535 || n > (size_t) ( end - *p ) ) |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 3618 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3619 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); |
| 3620 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 3621 | } |
| 3622 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3623 | if( ssl->conf->f_psk != NULL ) |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 3624 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3625 | 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] | 3626 | ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY; |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 3627 | } |
Manuel Pégourié-Gonnard | d27680b | 2014-07-08 14:15:55 +0200 | [diff] [blame] | 3628 | else |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 3629 | { |
Manuel Pégourié-Gonnard | 31ff1d2 | 2013-10-28 13:46:11 +0100 | [diff] [blame] | 3630 | /* Identity is not a big secret since clients send it in the clear, |
| 3631 | * but treat it carefully anyway, just in case */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3632 | if( n != ssl->conf->psk_identity_len || |
| 3633 | mbedtls_ssl_safer_memcmp( ssl->conf->psk_identity, *p, n ) != 0 ) |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 3634 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3635 | ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY; |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 3636 | } |
| 3637 | } |
| 3638 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3639 | if( ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY ) |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 3640 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3641 | MBEDTLS_SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n ); |
Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 3642 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3643 | MBEDTLS_SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3644 | return( MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY ); |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 3645 | } |
| 3646 | |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3647 | *p += n; |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 3648 | |
Manuel Pégourié-Gonnard | d27680b | 2014-07-08 14:15:55 +0200 | [diff] [blame] | 3649 | return( 0 ); |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 3650 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3651 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 3652 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3653 | static int ssl_parse_client_key_exchange( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3654 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 3655 | int ret; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3656 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
Manuel Pégourié-Gonnard | 2114d72 | 2014-09-10 13:59:41 +0000 | [diff] [blame] | 3657 | unsigned char *p, *end; |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3658 | |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3659 | ciphersuite_info = ssl->transform_negotiate->ciphersuite_info; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3660 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3661 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3662 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3663 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) && \ |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3664 | ( defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \ |
| 3665 | defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) ) |
| 3666 | if( ( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK || |
| 3667 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA ) && |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3668 | ( ssl->handshake->async_in_progress != 0 ) ) |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3669 | { |
| 3670 | /* We've already read a record and there is an asynchronous |
| 3671 | * operation in progress to decrypt it. So skip reading the |
Gilles Peskine | 168dae8 | 2018-04-25 23:35:42 +0200 | [diff] [blame] | 3672 | * record. */ |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3673 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "will resume decryption of previously-read record" ) ); |
| 3674 | } |
| 3675 | else |
| 3676 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3677 | if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3678 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3679 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3680 | return( ret ); |
| 3681 | } |
| 3682 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3683 | p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ); |
Manuel Pégourié-Gonnard | 2114d72 | 2014-09-10 13:59:41 +0000 | [diff] [blame] | 3684 | end = ssl->in_msg + ssl->in_hslen; |
Manuel Pégourié-Gonnard | f899583 | 2014-09-10 08:25:12 +0000 | [diff] [blame] | 3685 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3686 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3687 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3688 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); |
| 3689 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3690 | } |
| 3691 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3692 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3693 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3694 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); |
| 3695 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3696 | } |
| 3697 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3698 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) |
| 3699 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3700 | { |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3701 | if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3702 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3703 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret ); |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3704 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3705 | } |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3706 | |
Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 3707 | if( p != end ) |
| 3708 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3709 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) ); |
| 3710 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 3711 | } |
| 3712 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3713 | if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx, |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3714 | ssl->handshake->premaster, |
Manuel Pégourié-Gonnard | 3335205 | 2015-06-02 16:17:08 +0100 | [diff] [blame] | 3715 | MBEDTLS_PREMASTER_SIZE, |
Manuel Pégourié-Gonnard | 2d62764 | 2013-09-04 14:22:07 +0200 | [diff] [blame] | 3716 | &ssl->handshake->pmslen, |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 3717 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3718 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3719 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); |
| 3720 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3721 | } |
| 3722 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3723 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K ); |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3724 | } |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3725 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3726 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */ |
| 3727 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ |
| 3728 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \ |
| 3729 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ |
| 3730 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) |
| 3731 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA || |
| 3732 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA || |
| 3733 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA || |
| 3734 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA ) |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3735 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3736 | if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx, |
Manuel Pégourié-Gonnard | 2114d72 | 2014-09-10 13:59:41 +0000 | [diff] [blame] | 3737 | p, end - p) ) != 0 ) |
Manuel Pégourié-Gonnard | b59d699 | 2013-10-14 12:00:45 +0200 | [diff] [blame] | 3738 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3739 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret ); |
| 3740 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP ); |
Manuel Pégourié-Gonnard | b59d699 | 2013-10-14 12:00:45 +0200 | [diff] [blame] | 3741 | } |
| 3742 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3743 | MBEDTLS_SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp ); |
Manuel Pégourié-Gonnard | b59d699 | 2013-10-14 12:00:45 +0200 | [diff] [blame] | 3744 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3745 | if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3746 | &ssl->handshake->pmslen, |
| 3747 | ssl->handshake->premaster, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3748 | MBEDTLS_MPI_MAX_SIZE, |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 3749 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3750 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3751 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret ); |
| 3752 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3753 | } |
| 3754 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3755 | MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3756 | } |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3757 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3758 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || |
| 3759 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED || |
| 3760 | MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED || |
| 3761 | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */ |
| 3762 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 3763 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ) |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 3764 | { |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3765 | if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 ) |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 3766 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3767 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret ); |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 3768 | return( ret ); |
| 3769 | } |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3770 | |
Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 3771 | if( p != end ) |
| 3772 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3773 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) ); |
| 3774 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 3775 | } |
| 3776 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3777 | if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl, |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 3778 | ciphersuite_info->key_exchange ) ) != 0 ) |
| 3779 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3780 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 3781 | return( ret ); |
| 3782 | } |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 3783 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3784 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3785 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
| 3786 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
| 3787 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 3788 | { |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3789 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3790 | if ( ssl->handshake->async_in_progress != 0 ) |
Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3791 | { |
| 3792 | /* There is an asynchronous operation in progress to |
| 3793 | * decrypt the encrypted premaster secret, so skip |
| 3794 | * directly to resuming this operation. */ |
| 3795 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "PSK identity already parsed" ) ); |
| 3796 | /* Update p to skip the PSK identity. ssl_parse_encrypted_pms |
| 3797 | * won't actually use it, but maintain p anyway for robustness. */ |
| 3798 | p += ssl->conf->psk_identity_len + 2; |
| 3799 | } |
| 3800 | else |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3801 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 3802 | if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 ) |
| 3803 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3804 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret ); |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 3805 | return( ret ); |
| 3806 | } |
| 3807 | |
| 3808 | if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 ) |
| 3809 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3810 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret ); |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 3811 | return( ret ); |
| 3812 | } |
| 3813 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3814 | if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl, |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 3815 | ciphersuite_info->key_exchange ) ) != 0 ) |
| 3816 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3817 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret ); |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 3818 | return( ret ); |
| 3819 | } |
| 3820 | } |
| 3821 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3822 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
| 3823 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 3824 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3825 | { |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3826 | if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 ) |
| 3827 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3828 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3829 | return( ret ); |
| 3830 | } |
| 3831 | if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 ) |
| 3832 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3833 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3834 | return( ret ); |
| 3835 | } |
| 3836 | |
Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 3837 | if( p != end ) |
| 3838 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3839 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) ); |
| 3840 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 3841 | } |
| 3842 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3843 | if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl, |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 3844 | ciphersuite_info->key_exchange ) ) != 0 ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3845 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3846 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 3847 | return( ret ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3848 | } |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3849 | } |
| 3850 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3851 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
| 3852 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
| 3853 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 3854 | { |
Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 3855 | if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 ) |
| 3856 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3857 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret ); |
Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 3858 | return( ret ); |
| 3859 | } |
Manuel Pégourié-Gonnard | b59d699 | 2013-10-14 12:00:45 +0200 | [diff] [blame] | 3860 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3861 | if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx, |
Manuel Pégourié-Gonnard | b59d699 | 2013-10-14 12:00:45 +0200 | [diff] [blame] | 3862 | p, end - p ) ) != 0 ) |
Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 3863 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3864 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret ); |
| 3865 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP ); |
Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 3866 | } |
| 3867 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3868 | MBEDTLS_SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp ); |
Manuel Pégourié-Gonnard | b59d699 | 2013-10-14 12:00:45 +0200 | [diff] [blame] | 3869 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3870 | if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl, |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 3871 | ciphersuite_info->key_exchange ) ) != 0 ) |
Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 3872 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3873 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret ); |
Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 3874 | return( ret ); |
| 3875 | } |
Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 3876 | } |
| 3877 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3878 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
| 3879 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) |
| 3880 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3881 | { |
Manuel Pégourié-Gonnard | 2114d72 | 2014-09-10 13:59:41 +0000 | [diff] [blame] | 3882 | if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 0 ) ) != 0 ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3883 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3884 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret ); |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3885 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3886 | } |
| 3887 | } |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3888 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3889 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */ |
Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 3890 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 3891 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ) |
| 3892 | { |
| 3893 | ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx, |
| 3894 | p, end - p ); |
| 3895 | if( ret != 0 ) |
| 3896 | { |
| 3897 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret ); |
| 3898 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
| 3899 | } |
| 3900 | |
| 3901 | ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx, |
| 3902 | ssl->handshake->premaster, 32, &ssl->handshake->pmslen, |
| 3903 | ssl->conf->f_rng, ssl->conf->p_rng ); |
| 3904 | if( ret != 0 ) |
| 3905 | { |
| 3906 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret ); |
| 3907 | return( ret ); |
| 3908 | } |
| 3909 | } |
| 3910 | else |
| 3911 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3912 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3913 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3914 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3915 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3916 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3917 | if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 ) |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 3918 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3919 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret ); |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 3920 | return( ret ); |
| 3921 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3922 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3923 | ssl->state++; |
| 3924 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3925 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3926 | |
| 3927 | return( 0 ); |
| 3928 | } |
| 3929 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3930 | #if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \ |
| 3931 | !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \ |
Paul Bakker | 29f221f | 2016-07-22 13:49:02 +0100 | [diff] [blame] | 3932 | !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3933 | !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \ |
Paul Bakker | 29f221f | 2016-07-22 13:49:02 +0100 | [diff] [blame] | 3934 | !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)&& \ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3935 | !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) |
| 3936 | static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3937 | { |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3938 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 3939 | ssl->transform_negotiate->ciphersuite_info; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3940 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3941 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3942 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3943 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || |
| 3944 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK || |
| 3945 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK || |
Manuel Pégourié-Gonnard | 25dbeb0 | 2015-09-16 17:30:03 +0200 | [diff] [blame] | 3946 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK || |
| 3947 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ) |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 3948 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3949 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 3950 | ssl->state++; |
| 3951 | return( 0 ); |
| 3952 | } |
| 3953 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3954 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3955 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3956 | } |
| 3957 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3958 | static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3959 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3960 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 4528f3f | 2014-09-10 14:17:23 +0000 | [diff] [blame] | 3961 | size_t i, sig_len; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3962 | unsigned char hash[48]; |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3963 | unsigned char *hash_start = hash; |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3964 | size_t hashlen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3965 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3966 | mbedtls_pk_type_t pk_alg; |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 3967 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3968 | mbedtls_md_type_t md_alg; |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3969 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 3970 | ssl->transform_negotiate->ciphersuite_info; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3971 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3972 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3973 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3974 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || |
| 3975 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK || |
| 3976 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK || |
| 3977 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK || |
Manuel Pégourié-Gonnard | 25dbeb0 | 2015-09-16 17:30:03 +0200 | [diff] [blame] | 3978 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE || |
Manuel Pégourié-Gonnard | 7222621 | 2014-09-10 14:23:38 +0000 | [diff] [blame] | 3979 | ssl->session_negotiate->peer_cert == NULL ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3980 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3981 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3982 | ssl->state++; |
| 3983 | return( 0 ); |
| 3984 | } |
| 3985 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3986 | /* Read the message without adding it to the checksum */ |
| 3987 | do { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3988 | |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 3989 | do ret = mbedtls_ssl_read_record_layer( ssl ); |
| 3990 | while( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING ); |
| 3991 | |
| 3992 | if( ret != 0 ) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3993 | { |
| 3994 | MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record_layer" ), ret ); |
| 3995 | return( ret ); |
| 3996 | } |
| 3997 | |
| 3998 | ret = mbedtls_ssl_handle_message_type( ssl ); |
| 3999 | |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 4000 | } while( MBEDTLS_ERR_SSL_NON_FATAL == ret || |
| 4001 | MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4002 | |
| 4003 | if( 0 != ret ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4004 | { |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4005 | MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4006 | return( ret ); |
| 4007 | } |
| 4008 | |
| 4009 | ssl->state++; |
| 4010 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4011 | /* Process the message contents */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4012 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE || |
| 4013 | ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE_VERIFY ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4014 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4015 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) ); |
| 4016 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4017 | } |
| 4018 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4019 | i = mbedtls_ssl_hs_hdr_len( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4020 | |
Manuel Pégourié-Gonnard | 4528f3f | 2014-09-10 14:17:23 +0000 | [diff] [blame] | 4021 | /* |
| 4022 | * struct { |
| 4023 | * SignatureAndHashAlgorithm algorithm; -- TLS 1.2 only |
| 4024 | * opaque signature<0..2^16-1>; |
| 4025 | * } DigitallySigned; |
| 4026 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4027 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 4028 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 4029 | if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 ) |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 4030 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4031 | md_alg = MBEDTLS_MD_NONE; |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4032 | hashlen = 36; |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 4033 | |
| 4034 | /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4035 | if( mbedtls_pk_can_do( &ssl->session_negotiate->peer_cert->pk, |
| 4036 | MBEDTLS_PK_ECDSA ) ) |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 4037 | { |
| 4038 | hash_start += 16; |
| 4039 | hashlen -= 16; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4040 | md_alg = MBEDTLS_MD_SHA1; |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 4041 | } |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 4042 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 4043 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4044 | #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || |
| 4045 | MBEDTLS_SSL_PROTO_TLS1_1 */ |
| 4046 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4047 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 4048 | { |
Manuel Pégourié-Gonnard | 5ee9654 | 2014-09-10 14:27:21 +0000 | [diff] [blame] | 4049 | if( i + 2 > ssl->in_hslen ) |
| 4050 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4051 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) ); |
| 4052 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY ); |
Manuel Pégourié-Gonnard | 5ee9654 | 2014-09-10 14:27:21 +0000 | [diff] [blame] | 4053 | } |
| 4054 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4055 | /* |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4056 | * Hash |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4057 | */ |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4058 | md_alg = mbedtls_ssl_md_alg_from_hash( ssl->in_msg[i] ); |
| 4059 | |
| 4060 | 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] | 4061 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4062 | 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] | 4063 | " for verify message" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4064 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY ); |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 4065 | } |
| 4066 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4067 | #if !defined(MBEDTLS_MD_SHA1) |
| 4068 | if( MBEDTLS_MD_SHA1 == md_alg ) |
| 4069 | hash_start += 16; |
| 4070 | #endif |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 4071 | |
Manuel Pégourié-Gonnard | bfe32ef | 2013-08-22 14:55:30 +0200 | [diff] [blame] | 4072 | /* Info from md_alg will be used instead */ |
| 4073 | hashlen = 0; |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4074 | |
Manuel Pégourié-Gonnard | 4528f3f | 2014-09-10 14:17:23 +0000 | [diff] [blame] | 4075 | i++; |
| 4076 | |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4077 | /* |
| 4078 | * Signature |
| 4079 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4080 | if( ( pk_alg = mbedtls_ssl_pk_alg_from_sig( ssl->in_msg[i] ) ) |
| 4081 | == MBEDTLS_PK_NONE ) |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4082 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4083 | 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] | 4084 | " for verify message" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4085 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY ); |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4086 | } |
| 4087 | |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4088 | /* |
| 4089 | * Check the certificate's key type matches the signature alg |
| 4090 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4091 | if( ! mbedtls_pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) ) |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4092 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4093 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) ); |
| 4094 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY ); |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4095 | } |
Manuel Pégourié-Gonnard | 4528f3f | 2014-09-10 14:17:23 +0000 | [diff] [blame] | 4096 | |
| 4097 | i++; |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 4098 | } |
| 4099 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4100 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 4101 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4102 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 4103 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 4104 | } |
Manuel Pégourié-Gonnard | ff56da3 | 2013-07-11 10:46:21 +0200 | [diff] [blame] | 4105 | |
Manuel Pégourié-Gonnard | 5ee9654 | 2014-09-10 14:27:21 +0000 | [diff] [blame] | 4106 | if( i + 2 > ssl->in_hslen ) |
| 4107 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4108 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) ); |
| 4109 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY ); |
Manuel Pégourié-Gonnard | 5ee9654 | 2014-09-10 14:27:21 +0000 | [diff] [blame] | 4110 | } |
| 4111 | |
Manuel Pégourié-Gonnard | 4528f3f | 2014-09-10 14:17:23 +0000 | [diff] [blame] | 4112 | sig_len = ( ssl->in_msg[i] << 8 ) | ssl->in_msg[i+1]; |
| 4113 | i += 2; |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 4114 | |
Manuel Pégourié-Gonnard | 4528f3f | 2014-09-10 14:17:23 +0000 | [diff] [blame] | 4115 | if( i + sig_len != ssl->in_hslen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4116 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4117 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) ); |
| 4118 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4119 | } |
| 4120 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4121 | /* Calculate hash and verify signature */ |
| 4122 | ssl->handshake->calc_verify( ssl, hash ); |
| 4123 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4124 | if( ( ret = mbedtls_pk_verify( &ssl->session_negotiate->peer_cert->pk, |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 4125 | md_alg, hash_start, hashlen, |
Manuel Pégourié-Gonnard | 4528f3f | 2014-09-10 14:17:23 +0000 | [diff] [blame] | 4126 | ssl->in_msg + i, sig_len ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4127 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4128 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4129 | return( ret ); |
| 4130 | } |
| 4131 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4132 | mbedtls_ssl_update_handshake_status( ssl ); |
| 4133 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4134 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4135 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 4136 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4137 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4138 | #endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED && |
| 4139 | !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED && |
Paul Bakker | 29f221f | 2016-07-22 13:49:02 +0100 | [diff] [blame] | 4140 | !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED && |
| 4141 | !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED && |
| 4142 | !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED && |
| 4143 | !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4144 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4145 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 4146 | 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] | 4147 | { |
| 4148 | int ret; |
Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 4149 | size_t tlen; |
Manuel Pégourié-Gonnard | b0394be | 2015-05-19 11:40:30 +0200 | [diff] [blame] | 4150 | uint32_t lifetime; |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4151 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4152 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) ); |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4153 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4154 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 4155 | ssl->out_msg[0] = MBEDTLS_SSL_HS_NEW_SESSION_TICKET; |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4156 | |
| 4157 | /* |
| 4158 | * struct { |
| 4159 | * uint32 ticket_lifetime_hint; |
| 4160 | * opaque ticket<0..2^16-1>; |
| 4161 | * } NewSessionTicket; |
| 4162 | * |
| 4163 | * 4 . 7 ticket_lifetime_hint (0 = unspecified) |
| 4164 | * 8 . 9 ticket_len (n) |
| 4165 | * 10 . 9+n ticket content |
| 4166 | */ |
Manuel Pégourié-Gonnard | 164d894 | 2013-09-23 22:01:39 +0200 | [diff] [blame] | 4167 | |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 4168 | 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] | 4169 | ssl->session_negotiate, |
| 4170 | ssl->out_msg + 10, |
| 4171 | ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN, |
Manuel Pégourié-Gonnard | b0394be | 2015-05-19 11:40:30 +0200 | [diff] [blame] | 4172 | &tlen, &lifetime ) ) != 0 ) |
Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 4173 | { |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 4174 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_ticket_write", ret ); |
Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 4175 | tlen = 0; |
| 4176 | } |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4177 | |
Manuel Pégourié-Gonnard | b0394be | 2015-05-19 11:40:30 +0200 | [diff] [blame] | 4178 | ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF; |
| 4179 | ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF; |
| 4180 | ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF; |
| 4181 | ssl->out_msg[7] = ( lifetime ) & 0xFF; |
| 4182 | |
Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 4183 | ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF ); |
| 4184 | ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF ); |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4185 | |
Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 4186 | ssl->out_msglen = 10 + tlen; |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4187 | |
Manuel Pégourié-Gonnard | 145dfcb | 2014-02-26 14:23:33 +0100 | [diff] [blame] | 4188 | /* |
| 4189 | * Morally equivalent to updating ssl->state, but NewSessionTicket and |
| 4190 | * ChangeCipherSpec share the same state. |
| 4191 | */ |
| 4192 | ssl->handshake->new_session_ticket = 0; |
| 4193 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4194 | if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4195 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4196 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret ); |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4197 | return( ret ); |
| 4198 | } |
| 4199 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4200 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) ); |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4201 | |
| 4202 | return( 0 ); |
| 4203 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4204 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4205 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4206 | /* |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4207 | * SSL handshake -- server side -- single step |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4208 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4209 | int mbedtls_ssl_handshake_server_step( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4210 | { |
| 4211 | int ret = 0; |
| 4212 | |
Manuel Pégourié-Gonnard | dba460f | 2015-06-24 22:59:30 +0200 | [diff] [blame] | 4213 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4214 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4215 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4216 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4217 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4218 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4219 | return( ret ); |
| 4220 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4221 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4222 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4223 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4224 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4225 | if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4226 | return( ret ); |
| 4227 | } |
| 4228 | #endif |
| 4229 | |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4230 | switch( ssl->state ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4231 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4232 | case MBEDTLS_SSL_HELLO_REQUEST: |
| 4233 | ssl->state = MBEDTLS_SSL_CLIENT_HELLO; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4234 | break; |
| 4235 | |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4236 | /* |
| 4237 | * <== ClientHello |
| 4238 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4239 | case MBEDTLS_SSL_CLIENT_HELLO: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4240 | ret = ssl_parse_client_hello( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4241 | break; |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4242 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4243 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4244 | case MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT: |
| 4245 | return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED ); |
Manuel Pégourié-Gonnard | 579950c | 2014-09-29 17:47:33 +0200 | [diff] [blame] | 4246 | #endif |
| 4247 | |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4248 | /* |
| 4249 | * ==> ServerHello |
| 4250 | * Certificate |
| 4251 | * ( ServerKeyExchange ) |
| 4252 | * ( CertificateRequest ) |
| 4253 | * ServerHelloDone |
| 4254 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4255 | case MBEDTLS_SSL_SERVER_HELLO: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4256 | ret = ssl_write_server_hello( ssl ); |
| 4257 | break; |
| 4258 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4259 | case MBEDTLS_SSL_SERVER_CERTIFICATE: |
| 4260 | ret = mbedtls_ssl_write_certificate( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4261 | break; |
| 4262 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4263 | case MBEDTLS_SSL_SERVER_KEY_EXCHANGE: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4264 | ret = ssl_write_server_key_exchange( ssl ); |
| 4265 | break; |
| 4266 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4267 | case MBEDTLS_SSL_CERTIFICATE_REQUEST: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4268 | ret = ssl_write_certificate_request( ssl ); |
| 4269 | break; |
| 4270 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4271 | case MBEDTLS_SSL_SERVER_HELLO_DONE: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4272 | ret = ssl_write_server_hello_done( ssl ); |
| 4273 | break; |
| 4274 | |
| 4275 | /* |
| 4276 | * <== ( Certificate/Alert ) |
| 4277 | * ClientKeyExchange |
| 4278 | * ( CertificateVerify ) |
| 4279 | * ChangeCipherSpec |
| 4280 | * Finished |
| 4281 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4282 | case MBEDTLS_SSL_CLIENT_CERTIFICATE: |
| 4283 | ret = mbedtls_ssl_parse_certificate( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4284 | break; |
| 4285 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4286 | case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4287 | ret = ssl_parse_client_key_exchange( ssl ); |
| 4288 | break; |
| 4289 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4290 | case MBEDTLS_SSL_CERTIFICATE_VERIFY: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4291 | ret = ssl_parse_certificate_verify( ssl ); |
| 4292 | break; |
| 4293 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4294 | case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC: |
| 4295 | ret = mbedtls_ssl_parse_change_cipher_spec( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4296 | break; |
| 4297 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4298 | case MBEDTLS_SSL_CLIENT_FINISHED: |
| 4299 | ret = mbedtls_ssl_parse_finished( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4300 | break; |
| 4301 | |
| 4302 | /* |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4303 | * ==> ( NewSessionTicket ) |
| 4304 | * ChangeCipherSpec |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4305 | * Finished |
| 4306 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4307 | case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC: |
| 4308 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | 7cd5924 | 2013-08-02 13:24:41 +0200 | [diff] [blame] | 4309 | if( ssl->handshake->new_session_ticket != 0 ) |
| 4310 | ret = ssl_write_new_session_ticket( ssl ); |
| 4311 | else |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 4312 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4313 | ret = mbedtls_ssl_write_change_cipher_spec( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4314 | break; |
| 4315 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4316 | case MBEDTLS_SSL_SERVER_FINISHED: |
| 4317 | ret = mbedtls_ssl_write_finished( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4318 | break; |
| 4319 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4320 | case MBEDTLS_SSL_FLUSH_BUFFERS: |
| 4321 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) ); |
| 4322 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4323 | break; |
| 4324 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4325 | case MBEDTLS_SSL_HANDSHAKE_WRAPUP: |
| 4326 | mbedtls_ssl_handshake_wrapup( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4327 | break; |
| 4328 | |
| 4329 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4330 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) ); |
| 4331 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4332 | } |
| 4333 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4334 | return( ret ); |
| 4335 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4336 | #endif /* MBEDTLS_SSL_SRV_C */ |