| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* | 
|  | 2 | *  SSLv3/TLSv1 server-side functions | 
|  | 3 | * | 
| Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 4 | *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved | 
| Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | *  SPDX-License-Identifier: Apache-2.0 | 
|  | 6 | * | 
|  | 7 | *  Licensed under the Apache License, Version 2.0 (the "License"); you may | 
|  | 8 | *  not use this file except in compliance with the License. | 
|  | 9 | *  You may obtain a copy of the License at | 
|  | 10 | * | 
|  | 11 | *  http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 12 | * | 
|  | 13 | *  Unless required by applicable law or agreed to in writing, software | 
|  | 14 | *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | 
|  | 15 | *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 16 | *  See the License for the specific language governing permissions and | 
|  | 17 | *  limitations under the License. | 
| Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 18 | * | 
| Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 19 | *  This file is part of mbed TLS (https://tls.mbed.org) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 20 | */ | 
|  | 21 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 22 | #if !defined(MBEDTLS_CONFIG_FILE) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 23 | #include "mbedtls/config.h" | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 24 | #else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 25 | #include MBEDTLS_CONFIG_FILE | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 26 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 27 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 28 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 29 |  | 
| SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 30 | #if defined(MBEDTLS_PLATFORM_C) | 
|  | 31 | #include "mbedtls/platform.h" | 
|  | 32 | #else | 
|  | 33 | #include <stdlib.h> | 
|  | 34 | #define mbedtls_calloc    calloc | 
|  | 35 | #define mbedtls_free      free | 
| SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 36 | #endif | 
|  | 37 |  | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 38 | #include "mbedtls/debug.h" | 
|  | 39 | #include "mbedtls/ssl.h" | 
| Manuel Pégourié-Gonnard | 5e94dde | 2015-05-26 11:57:05 +0200 | [diff] [blame] | 40 | #include "mbedtls/ssl_internal.h" | 
| Andres Amaya Garcia | 8491406 | 2018-04-24 08:40:46 -0500 | [diff] [blame] | 41 | #include "mbedtls/platform_util.h" | 
| Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 42 |  | 
|  | 43 | #include <string.h> | 
|  | 44 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 45 | #if defined(MBEDTLS_ECP_C) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 46 | #include "mbedtls/ecp.h" | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 47 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 48 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 49 | #if defined(MBEDTLS_HAVE_TIME) | 
| Simon Butcher | b5b6af2 | 2016-07-13 14:46:18 +0100 | [diff] [blame] | 50 | #include "mbedtls/platform_time.h" | 
| Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 51 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 52 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 53 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) | 
|  | 54 | int mbedtls_ssl_set_client_transport_id( mbedtls_ssl_context *ssl, | 
| Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 55 | const unsigned char *info, | 
|  | 56 | size_t ilen ) | 
|  | 57 | { | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 58 | if( ssl->conf->endpoint != MBEDTLS_SSL_IS_SERVER ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 59 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 60 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 61 | mbedtls_free( ssl->cli_id ); | 
| Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 62 |  | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 63 | if( ( ssl->cli_id = mbedtls_calloc( 1, ilen ) ) == NULL ) | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 64 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
| Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 65 |  | 
|  | 66 | memcpy( ssl->cli_id, info, ilen ); | 
|  | 67 | ssl->cli_id_len = ilen; | 
|  | 68 |  | 
|  | 69 | return( 0 ); | 
|  | 70 | } | 
| Manuel Pégourié-Gonnard | d485d19 | 2014-07-23 14:56:15 +0200 | [diff] [blame] | 71 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 72 | void mbedtls_ssl_conf_dtls_cookies( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 73 | mbedtls_ssl_cookie_write_t *f_cookie_write, | 
|  | 74 | mbedtls_ssl_cookie_check_t *f_cookie_check, | 
| Manuel Pégourié-Gonnard | d485d19 | 2014-07-23 14:56:15 +0200 | [diff] [blame] | 75 | void *p_cookie ) | 
|  | 76 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 77 | conf->f_cookie_write = f_cookie_write; | 
|  | 78 | conf->f_cookie_check = f_cookie_check; | 
|  | 79 | conf->p_cookie       = p_cookie; | 
| Manuel Pégourié-Gonnard | d485d19 | 2014-07-23 14:56:15 +0200 | [diff] [blame] | 80 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 81 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */ | 
| Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 82 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 83 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 84 | static int ssl_parse_servername_ext( mbedtls_ssl_context *ssl, | 
| Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 85 | const unsigned char *buf, | 
| Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 86 | size_t len ) | 
|  | 87 | { | 
|  | 88 | int ret; | 
|  | 89 | size_t servername_list_size, hostname_len; | 
| Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 90 | const unsigned char *p; | 
| Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 91 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 92 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) ); | 
| Manuel Pégourié-Gonnard | 96ea2f2 | 2014-02-25 12:26:29 +0100 | [diff] [blame] | 93 |  | 
| Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 94 | if( len < 2 ) | 
|  | 95 | { | 
|  | 96 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
|  | 97 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 98 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
|  | 99 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
|  | 100 | } | 
| Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 101 | servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) ); | 
|  | 102 | if( servername_list_size + 2 != len ) | 
|  | 103 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 104 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 105 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 106 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 107 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 108 | } | 
|  | 109 |  | 
|  | 110 | p = buf + 2; | 
| Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 111 | while( servername_list_size > 2 ) | 
| Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 112 | { | 
|  | 113 | hostname_len = ( ( p[1] << 8 ) | p[2] ); | 
|  | 114 | if( hostname_len + 3 > servername_list_size ) | 
|  | 115 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 116 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 117 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 118 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 119 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 120 | } | 
|  | 121 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 122 | if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME ) | 
| Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 123 | { | 
| Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 124 | ret = ssl->conf->f_sni( ssl->conf->p_sni, | 
|  | 125 | ssl, p + 3, hostname_len ); | 
| Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 126 | if( ret != 0 ) | 
|  | 127 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 128 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret ); | 
|  | 129 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 130 | MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME ); | 
|  | 131 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 132 | } | 
| Paul Bakker | 81420ab | 2012-10-23 10:31:15 +0000 | [diff] [blame] | 133 | return( 0 ); | 
| Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 134 | } | 
|  | 135 |  | 
|  | 136 | servername_list_size -= hostname_len + 3; | 
| Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 137 | p += hostname_len + 3; | 
|  | 138 | } | 
|  | 139 |  | 
|  | 140 | if( servername_list_size != 0 ) | 
|  | 141 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 142 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 143 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 144 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 145 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 146 | } | 
|  | 147 |  | 
|  | 148 | return( 0 ); | 
|  | 149 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 150 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ | 
| Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 151 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 152 | static int ssl_parse_renegotiation_info( mbedtls_ssl_context *ssl, | 
| Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 153 | const unsigned char *buf, | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 154 | size_t len ) | 
|  | 155 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 156 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 157 | if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ) | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 158 | { | 
|  | 159 | /* Check verify-data in constant-time. The length OTOH is no secret */ | 
|  | 160 | if( len    != 1 + ssl->verify_data_len || | 
|  | 161 | buf[0] !=     ssl->verify_data_len || | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 162 | mbedtls_ssl_safer_memcmp( buf + 1, ssl->peer_verify_data, | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 163 | ssl->verify_data_len ) != 0 ) | 
|  | 164 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 165 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) ); | 
| Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 166 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 167 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 168 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 169 | } | 
|  | 170 | } | 
|  | 171 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 172 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 173 | { | 
|  | 174 | if( len != 1 || buf[0] != 0x0 ) | 
|  | 175 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 176 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) ); | 
| Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 177 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 178 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 179 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 180 | } | 
|  | 181 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 182 | ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 183 | } | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 184 |  | 
|  | 185 | return( 0 ); | 
|  | 186 | } | 
|  | 187 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 188 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ | 
|  | 189 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 190 |  | 
|  | 191 | /* | 
|  | 192 | * Status of the implementation of signature-algorithms extension: | 
|  | 193 | * | 
|  | 194 | * Currently, we are only considering the signature-algorithm extension | 
|  | 195 | * to pick a ciphersuite which allows us to send the ServerKeyExchange | 
|  | 196 | * message with a signature-hash combination that the user allows. | 
|  | 197 | * | 
|  | 198 | * We do *not* check whether all certificates in our certificate | 
|  | 199 | * chain are signed with an allowed signature-hash pair. | 
|  | 200 | * This needs to be done at a later stage. | 
|  | 201 | * | 
|  | 202 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 203 | static int ssl_parse_signature_algorithms_ext( mbedtls_ssl_context *ssl, | 
| Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 204 | const unsigned char *buf, | 
|  | 205 | size_t len ) | 
|  | 206 | { | 
|  | 207 | size_t sig_alg_list_size; | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 208 |  | 
| Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 209 | const unsigned char *p; | 
| Manuel Pégourié-Gonnard | 08e81e0 | 2014-07-08 12:56:25 +0200 | [diff] [blame] | 210 | const unsigned char *end = buf + len; | 
| Manuel Pégourié-Gonnard | 08e81e0 | 2014-07-08 12:56:25 +0200 | [diff] [blame] | 211 |  | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 212 | mbedtls_md_type_t md_cur; | 
|  | 213 | mbedtls_pk_type_t sig_cur; | 
| Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 214 |  | 
| Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 215 | if ( len < 2 ) { | 
|  | 216 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
|  | 217 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 218 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
|  | 219 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
|  | 220 | } | 
| Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 221 | sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) ); | 
|  | 222 | if( sig_alg_list_size + 2 != len || | 
| Manuel Pégourié-Gonnard | 08e81e0 | 2014-07-08 12:56:25 +0200 | [diff] [blame] | 223 | sig_alg_list_size % 2 != 0 ) | 
| Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 224 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 225 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 226 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 227 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 228 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 229 | } | 
|  | 230 |  | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 231 | /* Currently we only guarantee signing the ServerKeyExchange message according | 
|  | 232 | * to the constraints specified in this extension (see above), so it suffices | 
|  | 233 | * to remember only one suitable hash for each possible signature algorithm. | 
| Manuel Pégourié-Gonnard | 08e81e0 | 2014-07-08 12:56:25 +0200 | [diff] [blame] | 234 | * | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 235 | * This will change when we also consider certificate signatures, | 
|  | 236 | * in which case we will need to remember the whole signature-hash | 
|  | 237 | * pair list from the extension. | 
| Manuel Pégourié-Gonnard | 08e81e0 | 2014-07-08 12:56:25 +0200 | [diff] [blame] | 238 | */ | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 239 |  | 
|  | 240 | for( p = buf + 2; p < end; p += 2 ) | 
|  | 241 | { | 
|  | 242 | /* Silently ignore unknown signature or hash algorithms. */ | 
|  | 243 |  | 
|  | 244 | if( ( sig_cur = mbedtls_ssl_pk_alg_from_sig( p[1] ) ) == MBEDTLS_PK_NONE ) | 
|  | 245 | { | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 246 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext" | 
|  | 247 | " unknown sig alg encoding %d", p[1] ) ); | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 248 | continue; | 
|  | 249 | } | 
|  | 250 |  | 
|  | 251 | /* Check if we support the hash the user proposes */ | 
|  | 252 | md_cur = mbedtls_ssl_md_alg_from_hash( p[0] ); | 
|  | 253 | if( md_cur == MBEDTLS_MD_NONE ) | 
|  | 254 | { | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 255 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext:" | 
|  | 256 | " unknown hash alg encoding %d", p[0] ) ); | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 257 | continue; | 
|  | 258 | } | 
|  | 259 |  | 
|  | 260 | if( mbedtls_ssl_check_sig_hash( ssl, md_cur ) == 0 ) | 
|  | 261 | { | 
|  | 262 | mbedtls_ssl_sig_hash_set_add( &ssl->handshake->hash_algs, sig_cur, md_cur ); | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 263 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext:" | 
|  | 264 | " match sig %d and hash %d", | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 265 | sig_cur, md_cur ) ); | 
|  | 266 | } | 
|  | 267 | else | 
|  | 268 | { | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 269 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: " | 
|  | 270 | "hash alg %d not supported", md_cur ) ); | 
| Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 271 | } | 
| Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 272 | } | 
|  | 273 |  | 
| Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 274 | return( 0 ); | 
|  | 275 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 276 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && | 
|  | 277 | MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ | 
| Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 278 |  | 
| Robert Cragie | 136884c | 2015-10-02 13:34:31 +0100 | [diff] [blame] | 279 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ | 
| Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 280 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 281 | static int ssl_parse_supported_elliptic_curves( mbedtls_ssl_context *ssl, | 
| Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 282 | const unsigned char *buf, | 
|  | 283 | size_t len ) | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 284 | { | 
| Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 285 | size_t list_size, our_size; | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 286 | const unsigned char *p; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 287 | const mbedtls_ecp_curve_info *curve_info, **curves; | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 288 |  | 
| Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 289 | if ( len < 2 ) { | 
|  | 290 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
|  | 291 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 292 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
|  | 293 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
|  | 294 | } | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 295 | list_size = ( ( buf[0] << 8 ) | ( buf[1] ) ); | 
|  | 296 | if( list_size + 2 != len || | 
|  | 297 | list_size % 2 != 0 ) | 
|  | 298 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 299 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 300 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 301 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 302 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 303 | } | 
|  | 304 |  | 
| Manuel Pégourié-Gonnard | 43c3b28 | 2014-10-17 12:42:11 +0200 | [diff] [blame] | 305 | /* Should never happen unless client duplicates the extension */ | 
|  | 306 | if( ssl->handshake->curves != NULL ) | 
|  | 307 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 308 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 309 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 310 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 311 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Manuel Pégourié-Gonnard | 43c3b28 | 2014-10-17 12:42:11 +0200 | [diff] [blame] | 312 | } | 
|  | 313 |  | 
| Manuel Pégourié-Gonnard | c3f6b62c | 2014-02-06 10:13:09 +0100 | [diff] [blame] | 314 | /* 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] | 315 | * and leave room for a final 0 */ | 
|  | 316 | our_size = list_size / 2 + 1; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 317 | if( our_size > MBEDTLS_ECP_DP_MAX ) | 
|  | 318 | our_size = MBEDTLS_ECP_DP_MAX; | 
| Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 319 |  | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 320 | if( ( curves = mbedtls_calloc( our_size, sizeof( *curves ) ) ) == NULL ) | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 321 | { | 
|  | 322 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 323 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 324 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 325 | } | 
| Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 326 |  | 
| Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 327 | ssl->handshake->curves = curves; | 
|  | 328 |  | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 329 | p = buf + 2; | 
| Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 330 | while( list_size > 0 && our_size > 1 ) | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 331 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 332 | 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] | 333 |  | 
| Manuel Pégourié-Gonnard | f24b4a7 | 2013-09-23 18:14:50 +0200 | [diff] [blame] | 334 | if( curve_info != NULL ) | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 335 | { | 
| Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 336 | *curves++ = curve_info; | 
|  | 337 | our_size--; | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 338 | } | 
|  | 339 |  | 
|  | 340 | list_size -= 2; | 
|  | 341 | p += 2; | 
|  | 342 | } | 
|  | 343 |  | 
|  | 344 | return( 0 ); | 
|  | 345 | } | 
|  | 346 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 347 | static int ssl_parse_supported_point_formats( mbedtls_ssl_context *ssl, | 
| Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 348 | const unsigned char *buf, | 
|  | 349 | size_t len ) | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 350 | { | 
|  | 351 | size_t list_size; | 
|  | 352 | const unsigned char *p; | 
|  | 353 |  | 
| Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 354 | if( len == 0 || (size_t)( buf[0] + 1 ) != len ) | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 355 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 356 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 357 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 358 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 359 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 360 | } | 
| Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 361 | list_size = buf[0]; | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 362 |  | 
| Manuel Pégourié-Gonnard | c1b46d0 | 2015-09-16 11:18:32 +0200 | [diff] [blame] | 363 | p = buf + 1; | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 364 | while( list_size > 0 ) | 
|  | 365 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 366 | if( p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED || | 
|  | 367 | p[0] == MBEDTLS_ECP_PF_COMPRESSED ) | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 368 | { | 
| Robert Cragie | 136884c | 2015-10-02 13:34:31 +0100 | [diff] [blame] | 369 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) | 
| Manuel Pégourié-Gonnard | 5734b2d | 2013-08-15 19:04:02 +0200 | [diff] [blame] | 370 | ssl->handshake->ecdh_ctx.point_format = p[0]; | 
| Robert Cragie | 136884c | 2015-10-02 13:34:31 +0100 | [diff] [blame] | 371 | #endif | 
| Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 372 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Robert Cragie | 136884c | 2015-10-02 13:34:31 +0100 | [diff] [blame] | 373 | ssl->handshake->ecjpake_ctx.point_format = p[0]; | 
|  | 374 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 375 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) ); | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 376 | return( 0 ); | 
|  | 377 | } | 
|  | 378 |  | 
|  | 379 | list_size--; | 
|  | 380 | p++; | 
|  | 381 | } | 
|  | 382 |  | 
|  | 383 | return( 0 ); | 
|  | 384 | } | 
| Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 385 | #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || | 
|  | 386 | MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 387 |  | 
| Manuel Pégourié-Gonnard | bf57be6 | 2015-09-16 15:04:01 +0200 | [diff] [blame] | 388 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
|  | 389 | static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl, | 
|  | 390 | const unsigned char *buf, | 
|  | 391 | size_t len ) | 
|  | 392 | { | 
|  | 393 | int ret; | 
|  | 394 |  | 
|  | 395 | if( mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 ) | 
|  | 396 | { | 
|  | 397 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) ); | 
|  | 398 | return( 0 ); | 
|  | 399 | } | 
|  | 400 |  | 
|  | 401 | if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx, | 
|  | 402 | buf, len ) ) != 0 ) | 
|  | 403 | { | 
|  | 404 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 405 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 406 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); | 
| Manuel Pégourié-Gonnard | bf57be6 | 2015-09-16 15:04:01 +0200 | [diff] [blame] | 407 | return( ret ); | 
|  | 408 | } | 
|  | 409 |  | 
|  | 410 | /* Only mark the extension as OK when we're sure it is */ | 
|  | 411 | ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK; | 
|  | 412 |  | 
|  | 413 | return( 0 ); | 
|  | 414 | } | 
|  | 415 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ | 
|  | 416 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 417 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
|  | 418 | 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] | 419 | const unsigned char *buf, | 
|  | 420 | size_t len ) | 
|  | 421 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 422 | 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] | 423 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 424 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 425 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 426 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 427 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Manuel Pégourié-Gonnard | 48f8d0d | 2013-07-17 10:25:37 +0200 | [diff] [blame] | 428 | } | 
|  | 429 |  | 
| Manuel Pégourié-Gonnard | ed4af8b | 2013-07-18 14:07:09 +0200 | [diff] [blame] | 430 | ssl->session_negotiate->mfl_code = buf[0]; | 
|  | 431 |  | 
| Manuel Pégourié-Gonnard | 48f8d0d | 2013-07-17 10:25:37 +0200 | [diff] [blame] | 432 | return( 0 ); | 
|  | 433 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 434 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ | 
| Manuel Pégourié-Gonnard | 48f8d0d | 2013-07-17 10:25:37 +0200 | [diff] [blame] | 435 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 436 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) | 
|  | 437 | 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] | 438 | const unsigned char *buf, | 
|  | 439 | size_t len ) | 
|  | 440 | { | 
|  | 441 | if( len != 0 ) | 
|  | 442 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 443 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 444 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 445 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 446 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 447 | } | 
|  | 448 |  | 
|  | 449 | ((void) buf); | 
|  | 450 |  | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 451 | if( ssl->conf->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 452 | ssl->session_negotiate->trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; | 
| Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 453 |  | 
|  | 454 | return( 0 ); | 
|  | 455 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 456 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ | 
| Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 457 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 458 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
|  | 459 | 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] | 460 | const unsigned char *buf, | 
|  | 461 | size_t len ) | 
|  | 462 | { | 
|  | 463 | if( len != 0 ) | 
|  | 464 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 465 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 466 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 467 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 468 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 469 | } | 
|  | 470 |  | 
|  | 471 | ((void) buf); | 
|  | 472 |  | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 473 | if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED && | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 474 | ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 ) | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 475 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 476 | ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 477 | } | 
|  | 478 |  | 
|  | 479 | return( 0 ); | 
|  | 480 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 481 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 482 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 483 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) | 
|  | 484 | 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] | 485 | const unsigned char *buf, | 
|  | 486 | size_t len ) | 
|  | 487 | { | 
|  | 488 | if( len != 0 ) | 
|  | 489 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 490 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 491 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 492 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 493 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 494 | } | 
|  | 495 |  | 
|  | 496 | ((void) buf); | 
|  | 497 |  | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 498 | if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED && | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 499 | ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 ) | 
| Manuel Pégourié-Gonnard | b575b54 | 2014-10-24 15:12:31 +0200 | [diff] [blame] | 500 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 501 | ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; | 
| Manuel Pégourié-Gonnard | b575b54 | 2014-10-24 15:12:31 +0200 | [diff] [blame] | 502 | } | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 503 |  | 
|  | 504 | return( 0 ); | 
|  | 505 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 506 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 507 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 508 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | 
|  | 509 | 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] | 510 | unsigned char *buf, | 
| Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 511 | size_t len ) | 
|  | 512 | { | 
| Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 513 | int ret; | 
| Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 514 | mbedtls_ssl_session session; | 
| Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 515 |  | 
| Manuel Pégourié-Gonnard | bae389b | 2015-06-24 10:45:58 +0200 | [diff] [blame] | 516 | mbedtls_ssl_session_init( &session ); | 
|  | 517 |  | 
| Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 518 | if( ssl->conf->f_ticket_parse == NULL || | 
|  | 519 | ssl->conf->f_ticket_write == NULL ) | 
|  | 520 | { | 
| Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 521 | return( 0 ); | 
| Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 522 | } | 
| Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 523 |  | 
| Manuel Pégourié-Gonnard | 306827e | 2013-08-02 18:05:14 +0200 | [diff] [blame] | 524 | /* Remember the client asked us to send a new ticket */ | 
| Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 525 | ssl->handshake->new_session_ticket = 1; | 
|  | 526 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 527 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) ); | 
| Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 528 |  | 
| Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 529 | if( len == 0 ) | 
|  | 530 | return( 0 ); | 
|  | 531 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 532 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 533 | if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ) | 
| Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 534 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 535 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) ); | 
| Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 536 | return( 0 ); | 
|  | 537 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 538 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ | 
| Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 539 |  | 
|  | 540 | /* | 
| Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 541 | * Failures are ok: just ignore the ticket and proceed. | 
|  | 542 | */ | 
| Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 543 | if( ( ret = ssl->conf->f_ticket_parse( ssl->conf->p_ticket, &session, | 
|  | 544 | buf, len ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 545 | { | 
| Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 546 | mbedtls_ssl_session_free( &session ); | 
| Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 547 |  | 
|  | 548 | if( ret == MBEDTLS_ERR_SSL_INVALID_MAC ) | 
|  | 549 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket is not authentic" ) ); | 
|  | 550 | else if( ret == MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED ) | 
|  | 551 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket is expired" ) ); | 
|  | 552 | else | 
|  | 553 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_ticket_parse", ret ); | 
|  | 554 |  | 
| Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 555 | return( 0 ); | 
| Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 556 | } | 
| Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 557 |  | 
| Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 558 | /* | 
|  | 559 | * Keep the session ID sent by the client, since we MUST send it back to | 
|  | 560 | * inform them we're accepting the ticket  (RFC 5077 section 3.4) | 
|  | 561 | */ | 
| Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 562 | session.id_len = ssl->session_negotiate->id_len; | 
|  | 563 | memcpy( &session.id, ssl->session_negotiate->id, session.id_len ); | 
| Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 564 |  | 
|  | 565 | mbedtls_ssl_session_free( ssl->session_negotiate ); | 
|  | 566 | memcpy( ssl->session_negotiate, &session, sizeof( mbedtls_ssl_session ) ); | 
|  | 567 |  | 
|  | 568 | /* Zeroize instead of free as we copied the content */ | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 569 | mbedtls_platform_zeroize( &session, sizeof( mbedtls_ssl_session ) ); | 
| Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 570 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 571 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) ); | 
| Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 572 |  | 
| Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 573 | ssl->handshake->resume = 1; | 
| Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 574 |  | 
| Manuel Pégourié-Gonnard | 306827e | 2013-08-02 18:05:14 +0200 | [diff] [blame] | 575 | /* Don't send a new ticket after all, this one is OK */ | 
|  | 576 | ssl->handshake->new_session_ticket = 0; | 
|  | 577 |  | 
| Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 578 | return( 0 ); | 
|  | 579 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 580 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ | 
| Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 581 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 582 | #if defined(MBEDTLS_SSL_ALPN) | 
|  | 583 | static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl, | 
| Manuel Pégourié-Gonnard | 14beb08 | 2014-07-08 13:50:35 +0200 | [diff] [blame] | 584 | const unsigned char *buf, size_t len ) | 
| Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 585 | { | 
| Paul Bakker | 14b16c6 | 2014-05-28 11:33:54 +0200 | [diff] [blame] | 586 | size_t list_len, cur_len, ours_len; | 
| Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 587 | const unsigned char *theirs, *start, *end; | 
|  | 588 | const char **ours; | 
|  | 589 |  | 
|  | 590 | /* If ALPN not configured, just ignore the extension */ | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 591 | if( ssl->conf->alpn_list == NULL ) | 
| Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 592 | return( 0 ); | 
|  | 593 |  | 
|  | 594 | /* | 
|  | 595 | * opaque ProtocolName<1..2^8-1>; | 
|  | 596 | * | 
|  | 597 | * struct { | 
|  | 598 | *     ProtocolName protocol_name_list<2..2^16-1> | 
|  | 599 | * } ProtocolNameList; | 
|  | 600 | */ | 
|  | 601 |  | 
|  | 602 | /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */ | 
|  | 603 | if( len < 4 ) | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 604 | { | 
|  | 605 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 606 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 607 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 608 | } | 
| Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 609 |  | 
|  | 610 | list_len = ( buf[0] << 8 ) | buf[1]; | 
|  | 611 | if( list_len != len - 2 ) | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 612 | { | 
|  | 613 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 614 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 615 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 616 | } | 
| Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 617 |  | 
|  | 618 | /* | 
| Manuel Pégourié-Gonnard | 239987f | 2018-01-09 10:43:43 +0100 | [diff] [blame] | 619 | * Validate peer's list (lengths) | 
| Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 620 | */ | 
|  | 621 | start = buf + 2; | 
|  | 622 | end = buf + len; | 
| Manuel Pégourié-Gonnard | 239987f | 2018-01-09 10:43:43 +0100 | [diff] [blame] | 623 | for( theirs = start; theirs != end; theirs += cur_len ) | 
|  | 624 | { | 
|  | 625 | cur_len = *theirs++; | 
|  | 626 |  | 
|  | 627 | /* Current identifier must fit in list */ | 
|  | 628 | if( cur_len > (size_t)( end - theirs ) ) | 
|  | 629 | { | 
|  | 630 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 631 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
|  | 632 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
|  | 633 | } | 
|  | 634 |  | 
|  | 635 | /* Empty strings MUST NOT be included */ | 
|  | 636 | if( cur_len == 0 ) | 
|  | 637 | { | 
|  | 638 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 639 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); | 
|  | 640 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
|  | 641 | } | 
|  | 642 | } | 
|  | 643 |  | 
|  | 644 | /* | 
|  | 645 | * Use our order of preference | 
|  | 646 | */ | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 647 | for( ours = ssl->conf->alpn_list; *ours != NULL; ours++ ) | 
| Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 648 | { | 
| Paul Bakker | 14b16c6 | 2014-05-28 11:33:54 +0200 | [diff] [blame] | 649 | ours_len = strlen( *ours ); | 
| Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 650 | for( theirs = start; theirs != end; theirs += cur_len ) | 
|  | 651 | { | 
| Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 652 | cur_len = *theirs++; | 
|  | 653 |  | 
| Paul Bakker | 14b16c6 | 2014-05-28 11:33:54 +0200 | [diff] [blame] | 654 | if( cur_len == ours_len && | 
| Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 655 | memcmp( theirs, *ours, cur_len ) == 0 ) | 
|  | 656 | { | 
|  | 657 | ssl->alpn_chosen = *ours; | 
|  | 658 | return( 0 ); | 
|  | 659 | } | 
|  | 660 | } | 
|  | 661 | } | 
|  | 662 |  | 
|  | 663 | /* If we get there, no match was found */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 664 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 665 | MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL ); | 
|  | 666 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 667 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 668 | #endif /* MBEDTLS_SSL_ALPN */ | 
| Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 669 |  | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 670 | /* | 
|  | 671 | * Auxiliary functions for ServerHello parsing and related actions | 
|  | 672 | */ | 
|  | 673 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 674 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 675 | /* | 
| Manuel Pégourié-Gonnard | 6458e3b | 2015-01-08 14:16:56 +0100 | [diff] [blame] | 676 | * 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] | 677 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 678 | #if defined(MBEDTLS_ECDSA_C) | 
|  | 679 | static int ssl_check_key_curve( mbedtls_pk_context *pk, | 
|  | 680 | const mbedtls_ecp_curve_info **curves ) | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 681 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 682 | const mbedtls_ecp_curve_info **crv = curves; | 
|  | 683 | 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] | 684 |  | 
|  | 685 | while( *crv != NULL ) | 
|  | 686 | { | 
|  | 687 | if( (*crv)->grp_id == grp_id ) | 
| Manuel Pégourié-Gonnard | 6458e3b | 2015-01-08 14:16:56 +0100 | [diff] [blame] | 688 | return( 0 ); | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 689 | crv++; | 
|  | 690 | } | 
|  | 691 |  | 
| Manuel Pégourié-Gonnard | 6458e3b | 2015-01-08 14:16:56 +0100 | [diff] [blame] | 692 | return( -1 ); | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 693 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 694 | #endif /* MBEDTLS_ECDSA_C */ | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 695 |  | 
|  | 696 | /* | 
|  | 697 | * Try picking a certificate for this ciphersuite, | 
|  | 698 | * return 0 on success and -1 on failure. | 
|  | 699 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 700 | static int ssl_pick_cert( mbedtls_ssl_context *ssl, | 
|  | 701 | const mbedtls_ssl_ciphersuite_t * ciphersuite_info ) | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 702 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 703 | mbedtls_ssl_key_cert *cur, *list, *fallback = NULL; | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 704 | mbedtls_pk_type_t pk_alg = | 
|  | 705 | mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ); | 
| Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 706 | uint32_t flags; | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 707 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 708 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 709 | if( ssl->handshake->sni_key_cert != NULL ) | 
|  | 710 | list = ssl->handshake->sni_key_cert; | 
|  | 711 | else | 
|  | 712 | #endif | 
| Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 713 | list = ssl->conf->key_cert; | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 714 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 715 | if( pk_alg == MBEDTLS_PK_NONE ) | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 716 | return( 0 ); | 
|  | 717 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 718 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite requires certificate" ) ); | 
| Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 719 |  | 
| Manuel Pégourié-Gonnard | e540b49 | 2015-07-07 12:44:38 +0200 | [diff] [blame] | 720 | if( list == NULL ) | 
|  | 721 | { | 
|  | 722 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server has no certificate" ) ); | 
|  | 723 | return( -1 ); | 
|  | 724 | } | 
|  | 725 |  | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 726 | for( cur = list; cur != NULL; cur = cur->next ) | 
|  | 727 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 728 | MBEDTLS_SSL_DEBUG_CRT( 3, "candidate certificate chain, certificate", | 
| Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 729 | cur->cert ); | 
|  | 730 |  | 
| Gilles Peskine | e198df5 | 2018-01-05 21:17:45 +0100 | [diff] [blame] | 731 | if( ! mbedtls_pk_can_do( &cur->cert->pk, pk_alg ) ) | 
| Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 732 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 733 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: key type" ) ); | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 734 | continue; | 
| Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 735 | } | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 736 |  | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 737 | /* | 
|  | 738 | * This avoids sending the client a cert it'll reject based on | 
|  | 739 | * keyUsage or other extensions. | 
|  | 740 | * | 
|  | 741 | * It also allows the user to provision different certificates for | 
|  | 742 | * different uses based on keyUsage, eg if they want to avoid signing | 
|  | 743 | * and decrypting with the same RSA key. | 
|  | 744 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 745 | if( mbedtls_ssl_check_cert_usage( cur->cert, ciphersuite_info, | 
| Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 746 | MBEDTLS_SSL_IS_SERVER, &flags ) != 0 ) | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 747 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 748 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: " | 
| Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 749 | "(extended) key usage extension" ) ); | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 750 | continue; | 
|  | 751 | } | 
|  | 752 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 753 | #if defined(MBEDTLS_ECDSA_C) | 
|  | 754 | if( pk_alg == MBEDTLS_PK_ECDSA && | 
| Gilles Peskine | 81d4e89 | 2017-10-27 10:18:44 +0200 | [diff] [blame] | 755 | 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] | 756 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 757 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: elliptic curve" ) ); | 
| Manuel Pégourié-Gonnard | 846ba47 | 2015-01-08 13:54:38 +0100 | [diff] [blame] | 758 | continue; | 
| Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 759 | } | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 760 | #endif | 
| Manuel Pégourié-Gonnard | 846ba47 | 2015-01-08 13:54:38 +0100 | [diff] [blame] | 761 |  | 
| Manuel Pégourié-Gonnard | df331a5 | 2015-01-08 16:43:07 +0100 | [diff] [blame] | 762 | /* | 
|  | 763 | * Try to select a SHA-1 certificate for pre-1.2 clients, but still | 
|  | 764 | * present them a SHA-higher cert rather than failing if it's the only | 
|  | 765 | * one we got that satisfies the other conditions. | 
|  | 766 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 767 | if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 && | 
|  | 768 | cur->cert->sig_md != MBEDTLS_MD_SHA1 ) | 
| Manuel Pégourié-Gonnard | df331a5 | 2015-01-08 16:43:07 +0100 | [diff] [blame] | 769 | { | 
|  | 770 | if( fallback == NULL ) | 
|  | 771 | fallback = cur; | 
| Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 772 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 773 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate not preferred: " | 
| Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 774 | "sha-2 with pre-TLS 1.2 client" ) ); | 
| Manuel Pégourié-Gonnard | df331a5 | 2015-01-08 16:43:07 +0100 | [diff] [blame] | 775 | continue; | 
| Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 776 | } | 
| Manuel Pégourié-Gonnard | df331a5 | 2015-01-08 16:43:07 +0100 | [diff] [blame] | 777 | } | 
|  | 778 |  | 
| Manuel Pégourié-Gonnard | 846ba47 | 2015-01-08 13:54:38 +0100 | [diff] [blame] | 779 | /* If we get there, we got a winner */ | 
|  | 780 | break; | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 781 | } | 
|  | 782 |  | 
| Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 783 | if( cur == NULL ) | 
|  | 784 | cur = fallback; | 
|  | 785 |  | 
| Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 786 | /* 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] | 787 | if( cur != NULL ) | 
|  | 788 | { | 
|  | 789 | ssl->handshake->key_cert = cur; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 790 | MBEDTLS_SSL_DEBUG_CRT( 3, "selected certificate chain, certificate", | 
| Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 791 | ssl->handshake->key_cert->cert ); | 
| Manuel Pégourié-Gonnard | df331a5 | 2015-01-08 16:43:07 +0100 | [diff] [blame] | 792 | return( 0 ); | 
|  | 793 | } | 
|  | 794 |  | 
|  | 795 | return( -1 ); | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 796 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 797 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 798 |  | 
|  | 799 | /* | 
|  | 800 | * Check if a given ciphersuite is suitable for use with our config/keys/etc | 
|  | 801 | * Sets ciphersuite_info only if the suite matches. | 
|  | 802 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 803 | static int ssl_ciphersuite_match( mbedtls_ssl_context *ssl, int suite_id, | 
|  | 804 | const mbedtls_ssl_ciphersuite_t **ciphersuite_info ) | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 805 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 806 | const mbedtls_ssl_ciphersuite_t *suite_info; | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 807 |  | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 808 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ | 
| Hanno Becker | 4cb1f4d | 2017-10-10 15:59:57 +0100 | [diff] [blame] | 809 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 810 | mbedtls_pk_type_t sig_type; | 
|  | 811 | #endif | 
|  | 812 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 813 | suite_info = mbedtls_ssl_ciphersuite_from_id( suite_id ); | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 814 | if( suite_info == NULL ) | 
|  | 815 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 816 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 817 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 818 | } | 
|  | 819 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 820 | 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] | 821 |  | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 822 | if( suite_info->min_minor_ver > ssl->minor_ver || | 
|  | 823 | suite_info->max_minor_ver < ssl->minor_ver ) | 
| Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 824 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 825 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: version" ) ); | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 826 | return( 0 ); | 
| Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 827 | } | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 828 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 829 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 830 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 831 | ( suite_info->flags & MBEDTLS_CIPHERSUITE_NODTLS ) ) | 
| Manuel Pégourié-Gonnard | d666451 | 2014-02-06 13:26:57 +0100 | [diff] [blame] | 832 | return( 0 ); | 
|  | 833 | #endif | 
|  | 834 |  | 
| Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 835 | #if defined(MBEDTLS_ARC4_C) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 836 | if( ssl->conf->arc4_disabled == MBEDTLS_SSL_ARC4_DISABLED && | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 837 | suite_info->cipher == MBEDTLS_CIPHER_ARC4_128 ) | 
| Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 838 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 839 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: rc4" ) ); | 
| Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 840 | return( 0 ); | 
| Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 841 | } | 
| Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 842 | #endif | 
| Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 843 |  | 
| Manuel Pégourié-Gonnard | e511b4e | 2015-09-16 14:11:09 +0200 | [diff] [blame] | 844 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
|  | 845 | if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE && | 
| Manuel Pégourié-Gonnard | bf57be6 | 2015-09-16 15:04:01 +0200 | [diff] [blame] | 846 | ( 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] | 847 | { | 
| Manuel Pégourié-Gonnard | bf57be6 | 2015-09-16 15:04:01 +0200 | [diff] [blame] | 848 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: ecjpake " | 
|  | 849 | "not configured or ext missing" ) ); | 
| Manuel Pégourié-Gonnard | e511b4e | 2015-09-16 14:11:09 +0200 | [diff] [blame] | 850 | return( 0 ); | 
|  | 851 | } | 
|  | 852 | #endif | 
|  | 853 |  | 
|  | 854 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 855 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) | 
|  | 856 | if( mbedtls_ssl_ciphersuite_uses_ec( suite_info ) && | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 857 | ( ssl->handshake->curves == NULL || | 
|  | 858 | ssl->handshake->curves[0] == NULL ) ) | 
| Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 859 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 860 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: " | 
| Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 861 | "no common elliptic curve" ) ); | 
| 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 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 866 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 867 | /* If the ciphersuite requires a pre-shared key and we don't | 
|  | 868 | * have one, skip it now rather than failing later */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 869 | if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) && | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 870 | ssl->conf->f_psk == NULL && | 
|  | 871 | ( ssl->conf->psk == NULL || ssl->conf->psk_identity == NULL || | 
|  | 872 | 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] | 873 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 874 | 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] | 875 | return( 0 ); | 
| Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 876 | } | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 877 | #endif | 
|  | 878 |  | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 879 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ | 
|  | 880 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) | 
|  | 881 | /* If the ciphersuite requires signing, check whether | 
|  | 882 | * a suitable hash algorithm is present. */ | 
|  | 883 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) | 
|  | 884 | { | 
|  | 885 | sig_type = mbedtls_ssl_get_ciphersuite_sig_alg( suite_info ); | 
|  | 886 | if( sig_type != MBEDTLS_PK_NONE && | 
|  | 887 | mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs, sig_type ) == MBEDTLS_MD_NONE ) | 
|  | 888 | { | 
|  | 889 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no suitable hash algorithm " | 
|  | 890 | "for signature algorithm %d", sig_type ) ); | 
|  | 891 | return( 0 ); | 
|  | 892 | } | 
|  | 893 | } | 
|  | 894 |  | 
|  | 895 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && | 
|  | 896 | MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ | 
|  | 897 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 898 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 899 | /* | 
|  | 900 | * Final check: if ciphersuite requires us to have a | 
|  | 901 | * certificate/key of a particular type: | 
|  | 902 | * - select the appropriate certificate if we have one, or | 
|  | 903 | * - try the next ciphersuite if we don't | 
|  | 904 | * This must be done last since we modify the key_cert list. | 
|  | 905 | */ | 
|  | 906 | if( ssl_pick_cert( ssl, suite_info ) != 0 ) | 
| Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 907 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 908 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: " | 
| Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 909 | "no suitable certificate" ) ); | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 910 | return( 0 ); | 
| Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 911 | } | 
| Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 912 | #endif | 
|  | 913 |  | 
|  | 914 | *ciphersuite_info = suite_info; | 
|  | 915 | return( 0 ); | 
|  | 916 | } | 
|  | 917 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 918 | #if defined(MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO) | 
|  | 919 | static int ssl_parse_client_hello_v2( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 920 | { | 
| Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 921 | int ret, got_common_suite; | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 922 | unsigned int i, j; | 
|  | 923 | size_t n; | 
|  | 924 | unsigned int ciph_len, sess_len, chal_len; | 
|  | 925 | unsigned char *buf, *p; | 
| Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 926 | const int *ciphersuites; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 927 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 928 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 929 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) ); | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 930 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 931 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 932 | if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ) | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 933 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 934 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) ); | 
| Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 935 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 936 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 937 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 938 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 939 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 940 |  | 
|  | 941 | buf = ssl->in_hdr; | 
|  | 942 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 943 | MBEDTLS_SSL_DEBUG_BUF( 4, "record header", buf, 5 ); | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 944 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 945 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d", | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 946 | buf[2] ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 947 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d", | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 948 | ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 949 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]", | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 950 | buf[3], buf[4] ) ); | 
|  | 951 |  | 
|  | 952 | /* | 
|  | 953 | * SSLv2 Client Hello | 
|  | 954 | * | 
|  | 955 | * Record layer: | 
|  | 956 | *     0  .   1   message length | 
|  | 957 | * | 
|  | 958 | * SSL layer: | 
|  | 959 | *     2  .   2   message type | 
|  | 960 | *     3  .   4   protocol version | 
|  | 961 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 962 | if( buf[2] != MBEDTLS_SSL_HS_CLIENT_HELLO || | 
|  | 963 | buf[3] != MBEDTLS_SSL_MAJOR_VERSION_3 ) | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 964 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 965 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
|  | 966 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 967 | } | 
|  | 968 |  | 
|  | 969 | n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF; | 
|  | 970 |  | 
|  | 971 | if( n < 17 || n > 512 ) | 
|  | 972 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 973 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
|  | 974 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 975 | } | 
|  | 976 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 977 | ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3; | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 978 | ssl->minor_ver = ( buf[4] <= ssl->conf->max_minor_ver ) | 
|  | 979 | ? buf[4]  : ssl->conf->max_minor_ver; | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 980 |  | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 981 | if( ssl->minor_ver < ssl->conf->min_minor_ver ) | 
| 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 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum" | 
| Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 984 | " [%d:%d] < [%d:%d]", | 
|  | 985 | ssl->major_ver, ssl->minor_ver, | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 986 | ssl->conf->min_major_ver, ssl->conf->min_minor_ver ) ); | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 987 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 988 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 989 | MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION ); | 
|  | 990 | return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION ); | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 991 | } | 
|  | 992 |  | 
| Paul Bakker | 2fbefde | 2013-06-29 16:01:15 +0200 | [diff] [blame] | 993 | ssl->handshake->max_major_ver = buf[3]; | 
|  | 994 | ssl->handshake->max_minor_ver = buf[4]; | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 995 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 996 | if( ( ret = mbedtls_ssl_fetch_input( ssl, 2 + n ) ) != 0 ) | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 997 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 998 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret ); | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 999 | return( ret ); | 
|  | 1000 | } | 
|  | 1001 |  | 
|  | 1002 | ssl->handshake->update_checksum( ssl, buf + 2, n ); | 
|  | 1003 |  | 
|  | 1004 | buf = ssl->in_msg; | 
|  | 1005 | n = ssl->in_left - 5; | 
|  | 1006 |  | 
|  | 1007 | /* | 
|  | 1008 | *    0  .   1   ciphersuitelist length | 
|  | 1009 | *    2  .   3   session id length | 
|  | 1010 | *    4  .   5   challenge length | 
|  | 1011 | *    6  .  ..   ciphersuitelist | 
|  | 1012 | *   ..  .  ..   session id | 
|  | 1013 | *   ..  .  ..   challenge | 
|  | 1014 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1015 | MBEDTLS_SSL_DEBUG_BUF( 4, "record contents", buf, n ); | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1016 |  | 
|  | 1017 | ciph_len = ( buf[0] << 8 ) | buf[1]; | 
|  | 1018 | sess_len = ( buf[2] << 8 ) | buf[3]; | 
|  | 1019 | chal_len = ( buf[4] << 8 ) | buf[5]; | 
|  | 1020 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1021 | 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] | 1022 | ciph_len, sess_len, chal_len ) ); | 
|  | 1023 |  | 
|  | 1024 | /* | 
|  | 1025 | * Make sure each parameter length is valid | 
|  | 1026 | */ | 
|  | 1027 | if( ciph_len < 3 || ( ciph_len % 3 ) != 0 ) | 
|  | 1028 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1029 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
|  | 1030 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1031 | } | 
|  | 1032 |  | 
|  | 1033 | if( sess_len > 32 ) | 
|  | 1034 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1035 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
|  | 1036 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1037 | } | 
|  | 1038 |  | 
|  | 1039 | if( chal_len < 8 || chal_len > 32 ) | 
|  | 1040 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1041 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
|  | 1042 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1043 | } | 
|  | 1044 |  | 
|  | 1045 | if( n != 6 + ciph_len + sess_len + chal_len ) | 
|  | 1046 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1047 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
|  | 1048 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1049 | } | 
|  | 1050 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1051 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist", | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1052 | buf + 6, ciph_len ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1053 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id", | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1054 | buf + 6 + ciph_len, sess_len ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1055 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, challenge", | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1056 | buf + 6 + ciph_len + sess_len, chal_len ); | 
|  | 1057 |  | 
|  | 1058 | p = buf + 6 + ciph_len; | 
| Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 1059 | ssl->session_negotiate->id_len = sess_len; | 
| Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 1060 | memset( ssl->session_negotiate->id, 0, | 
|  | 1061 | sizeof( ssl->session_negotiate->id ) ); | 
| Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 1062 | memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->id_len ); | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1063 |  | 
|  | 1064 | p += sess_len; | 
|  | 1065 | memset( ssl->handshake->randbytes, 0, 64 ); | 
|  | 1066 | memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len ); | 
|  | 1067 |  | 
|  | 1068 | /* | 
|  | 1069 | * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV | 
|  | 1070 | */ | 
|  | 1071 | for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 ) | 
|  | 1072 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1073 | 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] | 1074 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1075 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) ); | 
|  | 1076 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 1077 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1078 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1079 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV " | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1080 | "during renegotiation" ) ); | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1081 |  | 
| Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 1082 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1083 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1084 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1085 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1086 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ | 
|  | 1087 | ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION; | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1088 | break; | 
|  | 1089 | } | 
|  | 1090 | } | 
|  | 1091 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1092 | #if defined(MBEDTLS_SSL_FALLBACK_SCSV) | 
| Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1093 | for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 ) | 
|  | 1094 | { | 
|  | 1095 | if( p[0] == 0 && | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1096 | p[1] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE >> 8 ) & 0xff ) && | 
|  | 1097 | p[2] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE      ) & 0xff ) ) | 
| Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1098 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1099 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "received FALLBACK_SCSV" ) ); | 
| Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1100 |  | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1101 | if( ssl->minor_ver < ssl->conf->max_minor_ver ) | 
| Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1102 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1103 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) ); | 
| Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1104 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1105 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1106 | MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK ); | 
| Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1107 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1108 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1109 | } | 
|  | 1110 |  | 
|  | 1111 | break; | 
|  | 1112 | } | 
|  | 1113 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1114 | #endif /* MBEDTLS_SSL_FALLBACK_SCSV */ | 
| Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1115 |  | 
| Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1116 | got_common_suite = 0; | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1117 | ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver]; | 
| Manuel Pégourié-Gonnard | 011a8db | 2013-11-30 18:11:07 +0100 | [diff] [blame] | 1118 | ciphersuite_info = NULL; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1119 | #if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE) | 
| Manuel Pégourié-Gonnard | 1a9f2c7 | 2013-11-30 18:30:06 +0100 | [diff] [blame] | 1120 | 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] | 1121 | for( i = 0; ciphersuites[i] != 0; i++ ) | 
|  | 1122 | #else | 
| Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 1123 | for( i = 0; ciphersuites[i] != 0; i++ ) | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1124 | 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] | 1125 | #endif | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1126 | { | 
| Manuel Pégourié-Gonnard | 011a8db | 2013-11-30 18:11:07 +0100 | [diff] [blame] | 1127 | if( p[0] != 0 || | 
|  | 1128 | p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) || | 
|  | 1129 | p[2] != ( ( ciphersuites[i]      ) & 0xFF ) ) | 
|  | 1130 | continue; | 
| Paul Bakker | 59c28a2 | 2013-06-29 15:33:42 +0200 | [diff] [blame] | 1131 |  | 
| Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1132 | got_common_suite = 1; | 
|  | 1133 |  | 
| Manuel Pégourié-Gonnard | 011a8db | 2013-11-30 18:11:07 +0100 | [diff] [blame] | 1134 | if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i], | 
|  | 1135 | &ciphersuite_info ) ) != 0 ) | 
|  | 1136 | return( ret ); | 
| Paul Bakker | 59c28a2 | 2013-06-29 15:33:42 +0200 | [diff] [blame] | 1137 |  | 
| Manuel Pégourié-Gonnard | 011a8db | 2013-11-30 18:11:07 +0100 | [diff] [blame] | 1138 | if( ciphersuite_info != NULL ) | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1139 | goto have_ciphersuite_v2; | 
|  | 1140 | } | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1141 |  | 
| Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1142 | if( got_common_suite ) | 
|  | 1143 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1144 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, " | 
| Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1145 | "but none of them usable" ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1146 | return( MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE ); | 
| Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1147 | } | 
|  | 1148 | else | 
|  | 1149 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1150 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) ); | 
|  | 1151 | return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN ); | 
| Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1152 | } | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1153 |  | 
|  | 1154 | have_ciphersuite_v2: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1155 | 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] | 1156 |  | 
| Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 1157 | ssl->session_negotiate->ciphersuite = ciphersuites[i]; | 
| Paul Bakker | 59c28a2 | 2013-06-29 15:33:42 +0200 | [diff] [blame] | 1158 | ssl->transform_negotiate->ciphersuite_info = ciphersuite_info; | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1159 |  | 
|  | 1160 | /* | 
|  | 1161 | * SSLv2 Client Hello relevant renegotiation security checks | 
|  | 1162 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1163 | if( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1164 | ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE ) | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1165 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1166 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) ); | 
| Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 1167 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1168 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1169 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1170 | } | 
|  | 1171 |  | 
|  | 1172 | ssl->in_left = 0; | 
|  | 1173 | ssl->state++; | 
|  | 1174 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1175 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) ); | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1176 |  | 
|  | 1177 | return( 0 ); | 
|  | 1178 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1179 | #endif /* MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */ | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1180 |  | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1181 | /* This function doesn't alert on errors that happen early during | 
|  | 1182 | ClientHello parsing because they might indicate that the client is | 
|  | 1183 | 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] | 1184 | static int ssl_parse_client_hello( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1185 | { | 
| Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1186 | int ret, got_common_suite; | 
| Manuel Pégourié-Gonnard | 9de64f5 | 2015-07-01 15:51:43 +0200 | [diff] [blame] | 1187 | size_t i, j; | 
|  | 1188 | size_t ciph_offset, comp_offset, ext_offset; | 
|  | 1189 | 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] | 1190 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 9de64f5 | 2015-07-01 15:51:43 +0200 | [diff] [blame] | 1191 | size_t cookie_offset, cookie_len; | 
| Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1192 | #endif | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1193 | unsigned char *buf, *p, *ext; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1194 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1195 | int renegotiation_info_seen = 0; | 
| Manuel Pégourié-Gonnard | eaecbd3 | 2014-11-06 02:38:02 +0100 | [diff] [blame] | 1196 | #endif | 
| Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1197 | int handshake_failure = 0; | 
| Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 1198 | const int *ciphersuites; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1199 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; | 
| Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 1200 | int major, minor; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1201 |  | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 1202 | /* If there is no signature-algorithm extension present, | 
|  | 1203 | * we need to fall back to the default values for allowed | 
|  | 1204 | * signature-hash pairs. */ | 
|  | 1205 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ | 
|  | 1206 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) | 
|  | 1207 | int sig_hash_alg_ext_present = 0; | 
|  | 1208 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && | 
|  | 1209 | MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ | 
|  | 1210 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1211 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1212 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1213 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | 
| Manuel Pégourié-Gonnard | f03c7aa | 2014-09-24 14:54:06 +0200 | [diff] [blame] | 1214 | read_record_header: | 
|  | 1215 | #endif | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1216 | /* | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1217 | * 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] | 1218 | * otherwise read it ourselves manually in order to support SSLv2 | 
|  | 1219 | * ClientHello, which doesn't use the same record layer format. | 
|  | 1220 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1221 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 1222 | if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE ) | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1223 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1224 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1225 | if( ( ret = mbedtls_ssl_fetch_input( ssl, 5 ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 59c6f2e | 2015-01-22 11:06:40 +0000 | [diff] [blame] | 1226 | { | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1227 | /* No alert on a read error. */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1228 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret ); | 
| Manuel Pégourié-Gonnard | 59c6f2e | 2015-01-22 11:06:40 +0000 | [diff] [blame] | 1229 | return( ret ); | 
|  | 1230 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1231 | } | 
|  | 1232 |  | 
|  | 1233 | buf = ssl->in_hdr; | 
|  | 1234 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1235 | #if defined(MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO) | 
|  | 1236 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1237 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM ) | 
| Manuel Pégourié-Gonnard | 8a7cf25 | 2014-10-09 17:35:53 +0200 | [diff] [blame] | 1238 | #endif | 
|  | 1239 | if( ( buf[0] & 0x80 ) != 0 ) | 
| Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 1240 | return( ssl_parse_client_hello_v2( ssl ) ); | 
| Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1241 | #endif | 
|  | 1242 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1243 | 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] | 1244 |  | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1245 | /* | 
| Manuel Pégourié-Gonnard | 6b1e207 | 2014-02-12 10:14:54 +0100 | [diff] [blame] | 1246 | * SSLv3/TLS Client Hello | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1247 | * | 
|  | 1248 | * Record layer: | 
|  | 1249 | *     0  .   0   message type | 
|  | 1250 | *     1  .   2   protocol version | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1251 | *     3  .   11  DTLS: epoch + record sequence number | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1252 | *     3  .   4   message length | 
|  | 1253 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1254 | 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] | 1255 | buf[0] ) ); | 
|  | 1256 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1257 | if( buf[0] != MBEDTLS_SSL_MSG_HANDSHAKE ) | 
| Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 1258 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1259 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
|  | 1260 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 1261 | } | 
|  | 1262 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1263 | 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] | 1264 | ( ssl->in_len[0] << 8 ) | ssl->in_len[1] ) ); | 
|  | 1265 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1266 | 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] | 1267 | buf[1], buf[2] ) ); | 
|  | 1268 |  | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1269 | 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] | 1270 |  | 
|  | 1271 | /* According to RFC 5246 Appendix E.1, the version here is typically | 
|  | 1272 | * "{03,00}, the lowest version number supported by the client, [or] the | 
|  | 1273 | * value of ClientHello.client_version", so the only meaningful check here | 
|  | 1274 | * is the major version shouldn't be less than 3 */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1275 | if( major < MBEDTLS_SSL_MAJOR_VERSION_3 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1276 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1277 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
|  | 1278 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1279 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1280 |  | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1281 | /* For DTLS if this is the initial handshake, remember the client sequence | 
|  | 1282 | * 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] | 1283 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1284 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1285 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 1286 | && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE | 
| Manuel Pégourié-Gonnard | 3a173f4 | 2015-01-22 13:30:33 +0000 | [diff] [blame] | 1287 | #endif | 
|  | 1288 | ) | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1289 | { | 
|  | 1290 | /* Epoch should be 0 for initial handshakes */ | 
|  | 1291 | if( ssl->in_ctr[0] != 0 || ssl->in_ctr[1] != 0 ) | 
|  | 1292 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1293 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
|  | 1294 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1295 | } | 
|  | 1296 |  | 
| Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 1297 | memcpy( ssl->cur_out_ctr + 2, ssl->in_ctr + 2, 6 ); | 
| Manuel Pégourié-Gonnard | f03c7aa | 2014-09-24 14:54:06 +0200 | [diff] [blame] | 1298 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1299 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | 
|  | 1300 | if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 ) | 
| Manuel Pégourié-Gonnard | f03c7aa | 2014-09-24 14:54:06 +0200 | [diff] [blame] | 1301 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1302 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record, discarding" ) ); | 
| Manuel Pégourié-Gonnard | f03c7aa | 2014-09-24 14:54:06 +0200 | [diff] [blame] | 1303 | ssl->next_record_offset = 0; | 
|  | 1304 | ssl->in_left = 0; | 
|  | 1305 | goto read_record_header; | 
|  | 1306 | } | 
|  | 1307 |  | 
|  | 1308 | /* 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] | 1309 | mbedtls_ssl_dtls_replay_update( ssl ); | 
| Manuel Pégourié-Gonnard | f03c7aa | 2014-09-24 14:54:06 +0200 | [diff] [blame] | 1310 | #endif | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1311 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1312 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1313 |  | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1314 | msg_len = ( ssl->in_len[0] << 8 ) | ssl->in_len[1]; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1315 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1316 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 1317 | if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ) | 
| Manuel Pégourié-Gonnard | b89c4f3 | 2015-01-21 13:24:10 +0000 | [diff] [blame] | 1318 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1319 | /* Set by mbedtls_ssl_read_record() */ | 
| Manuel Pégourié-Gonnard | b89c4f3 | 2015-01-21 13:24:10 +0000 | [diff] [blame] | 1320 | msg_len = ssl->in_hslen; | 
|  | 1321 | } | 
|  | 1322 | else | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1323 | #endif | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1324 | { | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1325 | if( msg_len > MBEDTLS_SSL_IN_CONTENT_LEN ) | 
| Manuel Pégourié-Gonnard | d6b721c | 2014-03-24 12:13:54 +0100 | [diff] [blame] | 1326 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1327 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
|  | 1328 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Manuel Pégourié-Gonnard | d6b721c | 2014-03-24 12:13:54 +0100 | [diff] [blame] | 1329 | } | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1330 |  | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 1331 | if( ( ret = mbedtls_ssl_fetch_input( ssl, | 
|  | 1332 | mbedtls_ssl_hdr_len( ssl ) + msg_len ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | d6b721c | 2014-03-24 12:13:54 +0100 | [diff] [blame] | 1333 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1334 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret ); | 
| Manuel Pégourié-Gonnard | d6b721c | 2014-03-24 12:13:54 +0100 | [diff] [blame] | 1335 | return( ret ); | 
|  | 1336 | } | 
| Manuel Pégourié-Gonnard | 30d16eb | 2014-08-19 17:43:50 +0200 | [diff] [blame] | 1337 |  | 
|  | 1338 | /* Done reading this record, get ready for the next one */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1339 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1340 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1341 | 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] | 1342 | else | 
|  | 1343 | #endif | 
|  | 1344 | ssl->in_left = 0; | 
| Manuel Pégourié-Gonnard | d6b721c | 2014-03-24 12:13:54 +0100 | [diff] [blame] | 1345 | } | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1346 |  | 
|  | 1347 | buf = ssl->in_msg; | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1348 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1349 | MBEDTLS_SSL_DEBUG_BUF( 4, "record contents", buf, msg_len ); | 
| Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 1350 |  | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1351 | ssl->handshake->update_checksum( ssl, buf, msg_len ); | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1352 |  | 
|  | 1353 | /* | 
| Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1354 | * Handshake layer: | 
|  | 1355 | *     0  .   0   handshake type | 
|  | 1356 | *     1  .   3   handshake length | 
|  | 1357 | *     4  .   5   DTLS only: message seqence number | 
|  | 1358 | *     6  .   8   DTLS only: fragment offset | 
|  | 1359 | *     9  .  11   DTLS only: fragment length | 
| Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 1360 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1361 | if( msg_len < mbedtls_ssl_hs_hdr_len( ssl ) ) | 
| Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1362 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1363 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
|  | 1364 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1365 | } | 
|  | 1366 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1367 | 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] | 1368 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1369 | if( buf[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) | 
| Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1370 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1371 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
|  | 1372 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1373 | } | 
|  | 1374 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1375 | 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] | 1376 | ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) ); | 
|  | 1377 |  | 
|  | 1378 | /* We don't support fragmentation of ClientHello (yet?) */ | 
|  | 1379 | if( buf[1] != 0 || | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1380 | 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] | 1381 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1382 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
|  | 1383 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1384 | } | 
|  | 1385 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1386 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1387 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 1388 | { | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1389 | /* | 
| Manuel Pégourié-Gonnard | 69849f8 | 2015-03-10 11:54:02 +0000 | [diff] [blame] | 1390 | * Copy the client's handshake message_seq on initial handshakes, | 
|  | 1391 | * check sequence number on renego. | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1392 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1393 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 1394 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) | 
| Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 1395 | { | 
|  | 1396 | /* This couldn't be done in ssl_prepare_handshake_record() */ | 
|  | 1397 | unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) | | 
|  | 1398 | ssl->in_msg[5]; | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1399 |  | 
| Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 1400 | if( cli_msg_seq != ssl->handshake->in_msg_seq ) | 
|  | 1401 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1402 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message_seq: " | 
| Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 1403 | "%d (expected %d)", cli_msg_seq, | 
|  | 1404 | ssl->handshake->in_msg_seq ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1405 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 1406 | } | 
|  | 1407 |  | 
|  | 1408 | ssl->handshake->in_msg_seq++; | 
|  | 1409 | } | 
| Manuel Pégourié-Gonnard | 69849f8 | 2015-03-10 11:54:02 +0000 | [diff] [blame] | 1410 | else | 
|  | 1411 | #endif | 
|  | 1412 | { | 
|  | 1413 | unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) | | 
|  | 1414 | ssl->in_msg[5]; | 
|  | 1415 | ssl->handshake->out_msg_seq = cli_msg_seq; | 
|  | 1416 | ssl->handshake->in_msg_seq  = cli_msg_seq + 1; | 
|  | 1417 | } | 
| Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 1418 |  | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1419 | /* | 
|  | 1420 | * For now we don't support fragmentation, so make sure | 
|  | 1421 | * fragment_offset == 0 and fragment_length == length | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1422 | */ | 
| Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 1423 | if( ssl->in_msg[6] != 0 || ssl->in_msg[7] != 0 || ssl->in_msg[8] != 0 || | 
|  | 1424 | memcmp( ssl->in_msg + 1, ssl->in_msg + 9, 3 ) != 0 ) | 
|  | 1425 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1426 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "ClientHello fragmentation not supported" ) ); | 
|  | 1427 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
| Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 1428 | } | 
| Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 1429 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1430 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 1431 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1432 | buf += mbedtls_ssl_hs_hdr_len( ssl ); | 
|  | 1433 | msg_len -= mbedtls_ssl_hs_hdr_len( ssl ); | 
| Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1434 |  | 
| Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 1435 | /* | 
| Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1436 | * ClientHello layer: | 
|  | 1437 | *     0  .   1   protocol version | 
|  | 1438 | *     2  .  33   random bytes (starting with 4 bytes of Unix time) | 
|  | 1439 | *    34  .  35   session id length (1 byte) | 
|  | 1440 | *    35  . 34+x  session id | 
|  | 1441 | *   35+x . 35+x  DTLS only: cookie length (1 byte) | 
|  | 1442 | *   36+x .  ..   DTLS only: cookie | 
| Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1443 | *    ..  .  ..   ciphersuite list length (2 bytes) | 
|  | 1444 | *    ..  .  ..   ciphersuite list | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1445 | *    ..  .  ..   compression alg. list length (1 byte) | 
|  | 1446 | *    ..  .  ..   compression alg. list | 
|  | 1447 | *    ..  .  ..   extensions length (2 bytes, optional) | 
|  | 1448 | *    ..  .  ..   extensions (optional) | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1449 | */ | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1450 |  | 
|  | 1451 | /* | 
| Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1452 | * Minimal length (with everything empty and extensions ommitted) is | 
|  | 1453 | * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can | 
|  | 1454 | * read at least up to session id length without worrying. | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1455 | */ | 
| Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1456 | if( msg_len < 38 ) | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1457 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1458 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
|  | 1459 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1460 | } | 
|  | 1461 |  | 
|  | 1462 | /* | 
|  | 1463 | * Check and save the protocol version | 
|  | 1464 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1465 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, version", buf, 2 ); | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1466 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1467 | mbedtls_ssl_read_version( &ssl->major_ver, &ssl->minor_ver, | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1468 | ssl->conf->transport, buf ); | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1469 |  | 
| Manuel Pégourié-Gonnard | 6b1e207 | 2014-02-12 10:14:54 +0100 | [diff] [blame] | 1470 | ssl->handshake->max_major_ver = ssl->major_ver; | 
|  | 1471 | ssl->handshake->max_minor_ver = ssl->minor_ver; | 
|  | 1472 |  | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1473 | if( ssl->major_ver < ssl->conf->min_major_ver || | 
|  | 1474 | ssl->minor_ver < ssl->conf->min_minor_ver ) | 
| Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 1475 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1476 | 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] | 1477 | " [%d:%d] < [%d:%d]", | 
|  | 1478 | ssl->major_ver, ssl->minor_ver, | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1479 | ssl->conf->min_major_ver, ssl->conf->min_minor_ver ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1480 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1481 | MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1482 | return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION ); | 
| Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 1483 | } | 
|  | 1484 |  | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1485 | if( ssl->major_ver > ssl->conf->max_major_ver ) | 
| Manuel Pégourié-Gonnard | 6b1e207 | 2014-02-12 10:14:54 +0100 | [diff] [blame] | 1486 | { | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1487 | ssl->major_ver = ssl->conf->max_major_ver; | 
|  | 1488 | ssl->minor_ver = ssl->conf->max_minor_ver; | 
| Manuel Pégourié-Gonnard | 6b1e207 | 2014-02-12 10:14:54 +0100 | [diff] [blame] | 1489 | } | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1490 | else if( ssl->minor_ver > ssl->conf->max_minor_ver ) | 
|  | 1491 | ssl->minor_ver = ssl->conf->max_minor_ver; | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1492 |  | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1493 | /* | 
|  | 1494 | * Save client random (inc. Unix time) | 
|  | 1495 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1496 | 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] | 1497 |  | 
| Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1498 | memcpy( ssl->handshake->randbytes, buf + 2, 32 ); | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1499 |  | 
|  | 1500 | /* | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1501 | * Check the session ID length and save session ID | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1502 | */ | 
| Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1503 | sess_len = buf[34]; | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1504 |  | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1505 | if( sess_len > sizeof( ssl->session_negotiate->id ) || | 
| Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1506 | sess_len + 34 + 2 > msg_len ) /* 2 for cipherlist length field */ | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1507 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1508 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1509 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1510 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1511 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1512 | } | 
|  | 1513 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1514 | 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] | 1515 |  | 
| Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 1516 | ssl->session_negotiate->id_len = sess_len; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1517 | memset( ssl->session_negotiate->id, 0, | 
|  | 1518 | sizeof( ssl->session_negotiate->id ) ); | 
| Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1519 | memcpy( ssl->session_negotiate->id, buf + 35, | 
| Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 1520 | ssl->session_negotiate->id_len ); | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1521 |  | 
|  | 1522 | /* | 
| Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1523 | * Check the cookie length and content | 
|  | 1524 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1525 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1526 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1527 | { | 
| Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1528 | cookie_offset = 35 + sess_len; | 
| Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1529 | cookie_len = buf[cookie_offset]; | 
|  | 1530 |  | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1531 | if( cookie_offset + 1 + cookie_len + 2 > msg_len ) | 
| Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1532 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1533 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1534 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1535 | MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1536 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1537 | } | 
|  | 1538 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1539 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie", | 
| Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1540 | buf + cookie_offset + 1, cookie_len ); | 
|  | 1541 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1542 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1543 | if( ssl->conf->f_cookie_check != NULL | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1544 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 1545 | && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE | 
| Manuel Pégourié-Gonnard | 69849f8 | 2015-03-10 11:54:02 +0000 | [diff] [blame] | 1546 | #endif | 
|  | 1547 | ) | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1548 | { | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1549 | if( ssl->conf->f_cookie_check( ssl->conf->p_cookie, | 
| Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1550 | buf + cookie_offset + 1, cookie_len, | 
|  | 1551 | ssl->cli_id, ssl->cli_id_len ) != 0 ) | 
|  | 1552 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1553 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification failed" ) ); | 
| Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1554 | ssl->handshake->verify_cookie_len = 1; | 
|  | 1555 | } | 
|  | 1556 | else | 
|  | 1557 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1558 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification passed" ) ); | 
| Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1559 | ssl->handshake->verify_cookie_len = 0; | 
|  | 1560 | } | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 1561 | } | 
| Manuel Pégourié-Gonnard | d7f9bc5 | 2014-07-23 11:09:27 +0200 | [diff] [blame] | 1562 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1563 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */ | 
| Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1564 | { | 
|  | 1565 | /* We know we didn't send a cookie, so it should be empty */ | 
|  | 1566 | if( cookie_len != 0 ) | 
|  | 1567 | { | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1568 | /* 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] | 1569 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
|  | 1570 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1571 | } | 
|  | 1572 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1573 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification skipped" ) ); | 
| Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1574 | } | 
| Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1575 |  | 
|  | 1576 | /* | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1577 | * Check the ciphersuitelist length (will be parsed later) | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1578 | */ | 
| Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1579 | ciph_offset = cookie_offset + 1 + cookie_len; | 
| Manuel Pégourié-Gonnard | a06d7fe | 2015-03-13 10:36:55 +0000 | [diff] [blame] | 1580 | } | 
| Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1581 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1582 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Manuel Pégourié-Gonnard | 19d438f | 2014-09-09 17:08:52 +0200 | [diff] [blame] | 1583 | ciph_offset = 35 + sess_len; | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1584 |  | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1585 | ciph_len = ( buf[ciph_offset + 0] << 8 ) | 
|  | 1586 | | ( buf[ciph_offset + 1]      ); | 
|  | 1587 |  | 
|  | 1588 | if( ciph_len < 2 || | 
|  | 1589 | ciph_len + 2 + ciph_offset + 1 > msg_len || /* 1 for comp. alg. len */ | 
|  | 1590 | ( ciph_len % 2 ) != 0 ) | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1591 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1592 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1593 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1594 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1595 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1596 | } | 
|  | 1597 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1598 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist", | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1599 | buf + ciph_offset + 2,  ciph_len ); | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1600 |  | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1601 | /* | 
|  | 1602 | * Check the compression algorithms length and pick one | 
|  | 1603 | */ | 
|  | 1604 | comp_offset = ciph_offset + 2 + ciph_len; | 
|  | 1605 |  | 
|  | 1606 | comp_len = buf[comp_offset]; | 
|  | 1607 |  | 
|  | 1608 | if( comp_len < 1 || | 
|  | 1609 | comp_len > 16 || | 
|  | 1610 | comp_len + comp_offset + 1 > msg_len ) | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1611 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1612 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1613 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1614 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1615 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1616 | } | 
|  | 1617 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1618 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, compression", | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1619 | buf + comp_offset + 1, comp_len ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1620 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1621 | ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL; | 
|  | 1622 | #if defined(MBEDTLS_ZLIB_SUPPORT) | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1623 | for( i = 0; i < comp_len; ++i ) | 
|  | 1624 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1625 | if( buf[comp_offset + 1 + i] == MBEDTLS_SSL_COMPRESS_DEFLATE ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1626 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1627 | ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_DEFLATE; | 
| Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1628 | break; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1629 | } | 
|  | 1630 | } | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1631 | #endif | 
|  | 1632 |  | 
| Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1633 | /* See comments in ssl_write_client_hello() */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1634 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1635 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1636 | ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL; | 
| Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1637 | #endif | 
| Manuel Pégourié-Gonnard | 82202f0 | 2014-07-23 00:28:58 +0200 | [diff] [blame] | 1638 |  | 
| Janos Follath | c6dab2b | 2016-05-23 14:27:02 +0100 | [diff] [blame] | 1639 | /* Do not parse the extensions if the protocol is SSLv3 */ | 
|  | 1640 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
|  | 1641 | if( ( ssl->major_ver != 3 ) || ( ssl->minor_ver != 0 ) ) | 
|  | 1642 | { | 
|  | 1643 | #endif | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1644 | /* | 
|  | 1645 | * Check the extension length | 
|  | 1646 | */ | 
|  | 1647 | ext_offset = comp_offset + 1 + comp_len; | 
|  | 1648 | if( msg_len > ext_offset ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1649 | { | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1650 | if( msg_len < ext_offset + 2 ) | 
|  | 1651 | { | 
|  | 1652 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1653 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1654 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1655 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
|  | 1656 | } | 
|  | 1657 |  | 
|  | 1658 | ext_len = ( buf[ext_offset + 0] << 8 ) | 
|  | 1659 | | ( buf[ext_offset + 1]      ); | 
|  | 1660 |  | 
|  | 1661 | if( ( ext_len > 0 && ext_len < 4 ) || | 
|  | 1662 | msg_len != ext_offset + 2 + ext_len ) | 
|  | 1663 | { | 
|  | 1664 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1665 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1666 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1667 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
|  | 1668 | } | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1669 | } | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1670 | else | 
|  | 1671 | ext_len = 0; | 
| Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1672 |  | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1673 | ext = buf + ext_offset + 2; | 
|  | 1674 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", ext, ext_len ); | 
| Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1675 |  | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1676 | while( ext_len != 0 ) | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1677 | { | 
| Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 1678 | unsigned int ext_id; | 
|  | 1679 | unsigned int ext_size; | 
|  | 1680 | if ( ext_len < 4 ) { | 
|  | 1681 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
|  | 1682 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1683 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
|  | 1684 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
|  | 1685 | } | 
|  | 1686 | ext_id   = ( ( ext[0] <<  8 ) | ( ext[1] ) ); | 
|  | 1687 | ext_size = ( ( ext[2] <<  8 ) | ( ext[3] ) ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1688 |  | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1689 | if( ext_size + 4 > ext_len ) | 
|  | 1690 | { | 
|  | 1691 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1692 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1693 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1694 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
|  | 1695 | } | 
|  | 1696 | switch( ext_id ) | 
|  | 1697 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1698 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1699 | case MBEDTLS_TLS_EXT_SERVERNAME: | 
|  | 1700 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) ); | 
|  | 1701 | if( ssl->conf->f_sni == NULL ) | 
|  | 1702 | break; | 
| Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 1703 |  | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1704 | ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size ); | 
|  | 1705 | if( ret != 0 ) | 
|  | 1706 | return( ret ); | 
|  | 1707 | break; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1708 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ | 
| Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 1709 |  | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1710 | case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO: | 
|  | 1711 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1712 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1713 | renegotiation_info_seen = 1; | 
| Manuel Pégourié-Gonnard | eaecbd3 | 2014-11-06 02:38:02 +0100 | [diff] [blame] | 1714 | #endif | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1715 |  | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1716 | ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size ); | 
|  | 1717 | if( ret != 0 ) | 
|  | 1718 | return( ret ); | 
|  | 1719 | break; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1720 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1721 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ | 
|  | 1722 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1723 | case MBEDTLS_TLS_EXT_SIG_ALG: | 
| Ron Eldor | 73a3817 | 2017-10-03 15:58:26 +0300 | [diff] [blame] | 1724 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) ); | 
|  | 1725 |  | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1726 | ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size ); | 
|  | 1727 | if( ret != 0 ) | 
|  | 1728 | return( ret ); | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 1729 |  | 
|  | 1730 | sig_hash_alg_ext_present = 1; | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1731 | break; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1732 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && | 
|  | 1733 | MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1734 |  | 
| Robert Cragie | 136884c | 2015-10-02 13:34:31 +0100 | [diff] [blame] | 1735 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ | 
| Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 1736 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1737 | case MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES: | 
|  | 1738 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) ); | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1739 |  | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1740 | ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size ); | 
|  | 1741 | if( ret != 0 ) | 
|  | 1742 | return( ret ); | 
|  | 1743 | break; | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1744 |  | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1745 | case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS: | 
|  | 1746 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) ); | 
|  | 1747 | ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT; | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1748 |  | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1749 | ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size ); | 
|  | 1750 | if( ret != 0 ) | 
|  | 1751 | return( ret ); | 
|  | 1752 | break; | 
| Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 1753 | #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || | 
|  | 1754 | MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1755 |  | 
| Manuel Pégourié-Gonnard | bf57be6 | 2015-09-16 15:04:01 +0200 | [diff] [blame] | 1756 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1757 | case MBEDTLS_TLS_EXT_ECJPAKE_KKPP: | 
|  | 1758 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake kkpp extension" ) ); | 
| Manuel Pégourié-Gonnard | bf57be6 | 2015-09-16 15:04:01 +0200 | [diff] [blame] | 1759 |  | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1760 | ret = ssl_parse_ecjpake_kkpp( ssl, ext + 4, ext_size ); | 
|  | 1761 | if( ret != 0 ) | 
|  | 1762 | return( ret ); | 
|  | 1763 | break; | 
| Manuel Pégourié-Gonnard | bf57be6 | 2015-09-16 15:04:01 +0200 | [diff] [blame] | 1764 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ | 
|  | 1765 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1766 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1767 | case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH: | 
|  | 1768 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) ); | 
| Manuel Pégourié-Gonnard | 48f8d0d | 2013-07-17 10:25:37 +0200 | [diff] [blame] | 1769 |  | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1770 | ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size ); | 
|  | 1771 | if( ret != 0 ) | 
|  | 1772 | return( ret ); | 
|  | 1773 | break; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1774 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ | 
| Manuel Pégourié-Gonnard | 48f8d0d | 2013-07-17 10:25:37 +0200 | [diff] [blame] | 1775 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1776 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1777 | case MBEDTLS_TLS_EXT_TRUNCATED_HMAC: | 
|  | 1778 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) ); | 
| Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 1779 |  | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1780 | ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size ); | 
|  | 1781 | if( ret != 0 ) | 
|  | 1782 | return( ret ); | 
|  | 1783 | break; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1784 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ | 
| Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 1785 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1786 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1787 | case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC: | 
|  | 1788 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) ); | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 1789 |  | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1790 | ret = ssl_parse_encrypt_then_mac_ext( ssl, ext + 4, ext_size ); | 
|  | 1791 | if( ret != 0 ) | 
|  | 1792 | return( ret ); | 
|  | 1793 | break; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1794 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 1795 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1796 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1797 | case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET: | 
|  | 1798 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) ); | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 1799 |  | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1800 | ret = ssl_parse_extended_ms_ext( ssl, ext + 4, ext_size ); | 
|  | 1801 | if( ret != 0 ) | 
|  | 1802 | return( ret ); | 
|  | 1803 | break; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1804 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 1805 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1806 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1807 | case MBEDTLS_TLS_EXT_SESSION_TICKET: | 
|  | 1808 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) ); | 
| Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 1809 |  | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1810 | ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size ); | 
|  | 1811 | if( ret != 0 ) | 
|  | 1812 | return( ret ); | 
|  | 1813 | break; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1814 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ | 
| Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 1815 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1816 | #if defined(MBEDTLS_SSL_ALPN) | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1817 | case MBEDTLS_TLS_EXT_ALPN: | 
|  | 1818 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) ); | 
| Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 1819 |  | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1820 | ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ); | 
|  | 1821 | if( ret != 0 ) | 
|  | 1822 | return( ret ); | 
|  | 1823 | break; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1824 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ | 
| Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 1825 |  | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1826 | default: | 
|  | 1827 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)", | 
|  | 1828 | ext_id ) ); | 
|  | 1829 | } | 
|  | 1830 |  | 
|  | 1831 | ext_len -= 4 + ext_size; | 
|  | 1832 | ext += 4 + ext_size; | 
|  | 1833 |  | 
|  | 1834 | if( ext_len > 0 && ext_len < 4 ) | 
|  | 1835 | { | 
|  | 1836 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1837 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1838 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Simon Butcher | 584a547 | 2016-05-23 16:24:52 +0100 | [diff] [blame] | 1839 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
|  | 1840 | } | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1841 | } | 
| Janos Follath | c6dab2b | 2016-05-23 14:27:02 +0100 | [diff] [blame] | 1842 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
|  | 1843 | } | 
|  | 1844 | #endif | 
|  | 1845 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1846 | #if defined(MBEDTLS_SSL_FALLBACK_SCSV) | 
| Gilles Peskine | d50177f | 2017-05-16 17:53:03 +0200 | [diff] [blame] | 1847 | 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] | 1848 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1849 | if( p[0] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE >> 8 ) & 0xff ) && | 
|  | 1850 | p[1] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE      ) & 0xff ) ) | 
| Manuel Pégourié-Gonnard | fedba98 | 2014-11-05 16:12:09 +0100 | [diff] [blame] | 1851 | { | 
| Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 1852 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "received FALLBACK_SCSV" ) ); | 
| Manuel Pégourié-Gonnard | fedba98 | 2014-11-05 16:12:09 +0100 | [diff] [blame] | 1853 |  | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1854 | if( ssl->minor_ver < ssl->conf->max_minor_ver ) | 
| Manuel Pégourié-Gonnard | fedba98 | 2014-11-05 16:12:09 +0100 | [diff] [blame] | 1855 | { | 
| Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 1856 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) ); | 
| Manuel Pégourié-Gonnard | fedba98 | 2014-11-05 16:12:09 +0100 | [diff] [blame] | 1857 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1858 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1859 | MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK ); | 
| Manuel Pégourié-Gonnard | fedba98 | 2014-11-05 16:12:09 +0100 | [diff] [blame] | 1860 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1861 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Manuel Pégourié-Gonnard | fedba98 | 2014-11-05 16:12:09 +0100 | [diff] [blame] | 1862 | } | 
|  | 1863 |  | 
|  | 1864 | break; | 
|  | 1865 | } | 
|  | 1866 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1867 | #endif /* MBEDTLS_SSL_FALLBACK_SCSV */ | 
| Manuel Pégourié-Gonnard | fedba98 | 2014-11-05 16:12:09 +0100 | [diff] [blame] | 1868 |  | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 1869 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ | 
|  | 1870 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) | 
|  | 1871 |  | 
|  | 1872 | /* | 
|  | 1873 | * Try to fall back to default hash SHA1 if the client | 
|  | 1874 | * hasn't provided any preferred signature-hash combinations. | 
|  | 1875 | */ | 
|  | 1876 | if( sig_hash_alg_ext_present == 0 ) | 
|  | 1877 | { | 
|  | 1878 | mbedtls_md_type_t md_default = MBEDTLS_MD_SHA1; | 
|  | 1879 |  | 
|  | 1880 | if( mbedtls_ssl_check_sig_hash( ssl, md_default ) != 0 ) | 
|  | 1881 | md_default = MBEDTLS_MD_NONE; | 
|  | 1882 |  | 
|  | 1883 | mbedtls_ssl_sig_hash_set_const_hash( &ssl->handshake->hash_algs, md_default ); | 
|  | 1884 | } | 
|  | 1885 |  | 
|  | 1886 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && | 
|  | 1887 | MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ | 
|  | 1888 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1889 | /* | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1890 | * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV | 
|  | 1891 | */ | 
|  | 1892 | for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 ) | 
|  | 1893 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1894 | 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] | 1895 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1896 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) ); | 
|  | 1897 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 1898 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1899 | { | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 1900 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV " | 
|  | 1901 | "during renegotiation" ) ); | 
| Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 1902 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1903 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1904 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1905 | } | 
| Manuel Pégourié-Gonnard | 69849f8 | 2015-03-10 11:54:02 +0000 | [diff] [blame] | 1906 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1907 | ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION; | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1908 | break; | 
|  | 1909 | } | 
|  | 1910 | } | 
|  | 1911 |  | 
|  | 1912 | /* | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1913 | * Renegotiation security checks | 
|  | 1914 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1915 | if( ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION && | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1916 | ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE ) | 
| Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1917 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1918 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) ); | 
| Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1919 | handshake_failure = 1; | 
|  | 1920 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1921 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 1922 | else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && | 
|  | 1923 | ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION && | 
| Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1924 | renegotiation_info_seen == 0 ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1925 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1926 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) ); | 
| Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1927 | handshake_failure = 1; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1928 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1929 | else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && | 
|  | 1930 | ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1931 | ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1932 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1933 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) ); | 
| Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1934 | handshake_failure = 1; | 
|  | 1935 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1936 | else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && | 
|  | 1937 | ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && | 
| Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1938 | renegotiation_info_seen == 1 ) | 
|  | 1939 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1940 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) ); | 
| Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1941 | handshake_failure = 1; | 
|  | 1942 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1943 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ | 
| Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1944 |  | 
|  | 1945 | if( handshake_failure == 1 ) | 
|  | 1946 | { | 
| Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 1947 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1948 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1949 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1950 | } | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1951 |  | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1952 | /* | 
|  | 1953 | * Search for a matching ciphersuite | 
| Manuel Pégourié-Gonnard | 3ebb2cd | 2013-09-23 17:00:18 +0200 | [diff] [blame] | 1954 | * (At the end because we need information from the EC-based extensions | 
|  | 1955 | * and certificate from the SNI callback triggered by the SNI extension.) | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1956 | */ | 
| Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1957 | got_common_suite = 0; | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1958 | ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver]; | 
| Manuel Pégourié-Gonnard | 59b81d7 | 2013-11-30 17:46:04 +0100 | [diff] [blame] | 1959 | ciphersuite_info = NULL; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1960 | #if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE) | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1961 | 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] | 1962 | for( i = 0; ciphersuites[i] != 0; i++ ) | 
|  | 1963 | #else | 
| Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 1964 | for( i = 0; ciphersuites[i] != 0; i++ ) | 
| Manuel Pégourié-Gonnard | 8933a65 | 2014-03-20 17:29:27 +0100 | [diff] [blame] | 1965 | 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] | 1966 | #endif | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1967 | { | 
| Manuel Pégourié-Gonnard | 011a8db | 2013-11-30 18:11:07 +0100 | [diff] [blame] | 1968 | if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) || | 
|  | 1969 | p[1] != ( ( ciphersuites[i]      ) & 0xFF ) ) | 
|  | 1970 | continue; | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1971 |  | 
| Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1972 | got_common_suite = 1; | 
|  | 1973 |  | 
| Manuel Pégourié-Gonnard | 011a8db | 2013-11-30 18:11:07 +0100 | [diff] [blame] | 1974 | if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i], | 
|  | 1975 | &ciphersuite_info ) ) != 0 ) | 
|  | 1976 | return( ret ); | 
|  | 1977 |  | 
|  | 1978 | if( ciphersuite_info != NULL ) | 
|  | 1979 | goto have_ciphersuite; | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1980 | } | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1981 |  | 
| Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1982 | if( got_common_suite ) | 
|  | 1983 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1984 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, " | 
| Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1985 | "but none of them usable" ) ); | 
| Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 1986 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1987 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1988 | return( MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE ); | 
| Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1989 | } | 
|  | 1990 | else | 
|  | 1991 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1992 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) ); | 
| Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 1993 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1994 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1995 | return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN ); | 
| Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1996 | } | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1997 |  | 
|  | 1998 | have_ciphersuite: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1999 | 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] | 2000 |  | 
| Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 2001 | ssl->session_negotiate->ciphersuite = ciphersuites[i]; | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2002 | ssl->transform_negotiate->ciphersuite_info = ciphersuite_info; | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2003 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2004 | ssl->state++; | 
|  | 2005 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2006 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2007 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2008 | mbedtls_ssl_recv_flight_completed( ssl ); | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2009 | #endif | 
|  | 2010 |  | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 2011 | /* Debugging-only output for testsuite */ | 
|  | 2012 | #if defined(MBEDTLS_DEBUG_C)                         && \ | 
|  | 2013 | defined(MBEDTLS_SSL_PROTO_TLS1_2)                && \ | 
|  | 2014 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) | 
|  | 2015 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) | 
|  | 2016 | { | 
|  | 2017 | mbedtls_pk_type_t sig_alg = mbedtls_ssl_get_ciphersuite_sig_alg( ciphersuite_info ); | 
|  | 2018 | if( sig_alg != MBEDTLS_PK_NONE ) | 
|  | 2019 | { | 
|  | 2020 | mbedtls_md_type_t md_alg = mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs, | 
|  | 2021 | sig_alg ); | 
|  | 2022 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d", | 
|  | 2023 | mbedtls_ssl_hash_from_md_alg( md_alg ) ) ); | 
|  | 2024 | } | 
|  | 2025 | else | 
|  | 2026 | { | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 2027 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no hash algorithm for signature algorithm " | 
|  | 2028 | "%d - should not happen", sig_alg ) ); | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 2029 | } | 
|  | 2030 | } | 
|  | 2031 | #endif | 
|  | 2032 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2033 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2034 |  | 
|  | 2035 | return( 0 ); | 
|  | 2036 | } | 
|  | 2037 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2038 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) | 
|  | 2039 | 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] | 2040 | unsigned char *buf, | 
|  | 2041 | size_t *olen ) | 
|  | 2042 | { | 
|  | 2043 | unsigned char *p = buf; | 
|  | 2044 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2045 | 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] | 2046 | { | 
|  | 2047 | *olen = 0; | 
|  | 2048 | return; | 
|  | 2049 | } | 
|  | 2050 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2051 | 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] | 2052 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2053 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF ); | 
|  | 2054 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_TRUNCATED_HMAC      ) & 0xFF ); | 
| Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 2055 |  | 
|  | 2056 | *p++ = 0x00; | 
|  | 2057 | *p++ = 0x00; | 
|  | 2058 |  | 
|  | 2059 | *olen = 4; | 
|  | 2060 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2061 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ | 
| Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 2062 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2063 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
|  | 2064 | 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] | 2065 | unsigned char *buf, | 
|  | 2066 | size_t *olen ) | 
|  | 2067 | { | 
|  | 2068 | unsigned char *p = buf; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2069 | const mbedtls_ssl_ciphersuite_t *suite = NULL; | 
|  | 2070 | const mbedtls_cipher_info_t *cipher = NULL; | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2071 |  | 
| Hanno Becker | 27b34d5 | 2017-10-20 14:24:51 +0100 | [diff] [blame] | 2072 | 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] | 2073 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2074 | { | 
|  | 2075 | *olen = 0; | 
|  | 2076 | return; | 
|  | 2077 | } | 
|  | 2078 |  | 
| Manuel Pégourié-Gonnard | 78e745f | 2014-11-04 15:44:06 +0100 | [diff] [blame] | 2079 | /* | 
|  | 2080 | * RFC 7366: "If a server receives an encrypt-then-MAC request extension | 
|  | 2081 | * from a client and then selects a stream or Authenticated Encryption | 
|  | 2082 | * with Associated Data (AEAD) ciphersuite, it MUST NOT send an | 
|  | 2083 | * encrypt-then-MAC response extension back to the client." | 
|  | 2084 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2085 | if( ( suite = mbedtls_ssl_ciphersuite_from_id( | 
| Manuel Pégourié-Gonnard | 78e745f | 2014-11-04 15:44:06 +0100 | [diff] [blame] | 2086 | ssl->session_negotiate->ciphersuite ) ) == NULL || | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2087 | ( cipher = mbedtls_cipher_info_from_type( suite->cipher ) ) == NULL || | 
|  | 2088 | cipher->mode != MBEDTLS_MODE_CBC ) | 
| Manuel Pégourié-Gonnard | 78e745f | 2014-11-04 15:44:06 +0100 | [diff] [blame] | 2089 | { | 
|  | 2090 | *olen = 0; | 
|  | 2091 | return; | 
|  | 2092 | } | 
|  | 2093 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2094 | 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] | 2095 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2096 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF ); | 
|  | 2097 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC      ) & 0xFF ); | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2098 |  | 
|  | 2099 | *p++ = 0x00; | 
|  | 2100 | *p++ = 0x00; | 
|  | 2101 |  | 
|  | 2102 | *olen = 4; | 
|  | 2103 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2104 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2105 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2106 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) | 
|  | 2107 | 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] | 2108 | unsigned char *buf, | 
|  | 2109 | size_t *olen ) | 
|  | 2110 | { | 
|  | 2111 | unsigned char *p = buf; | 
|  | 2112 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2113 | if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED || | 
|  | 2114 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2115 | { | 
|  | 2116 | *olen = 0; | 
|  | 2117 | return; | 
|  | 2118 | } | 
|  | 2119 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2120 | 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] | 2121 | "extension" ) ); | 
|  | 2122 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2123 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF ); | 
|  | 2124 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET      ) & 0xFF ); | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2125 |  | 
|  | 2126 | *p++ = 0x00; | 
|  | 2127 | *p++ = 0x00; | 
|  | 2128 |  | 
|  | 2129 | *olen = 4; | 
|  | 2130 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2131 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2132 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2133 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | 
|  | 2134 | 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] | 2135 | unsigned char *buf, | 
|  | 2136 | size_t *olen ) | 
|  | 2137 | { | 
|  | 2138 | unsigned char *p = buf; | 
|  | 2139 |  | 
|  | 2140 | if( ssl->handshake->new_session_ticket == 0 ) | 
|  | 2141 | { | 
|  | 2142 | *olen = 0; | 
|  | 2143 | return; | 
|  | 2144 | } | 
|  | 2145 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2146 | 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] | 2147 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2148 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF ); | 
|  | 2149 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET      ) & 0xFF ); | 
| Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 2150 |  | 
|  | 2151 | *p++ = 0x00; | 
|  | 2152 | *p++ = 0x00; | 
|  | 2153 |  | 
|  | 2154 | *olen = 4; | 
|  | 2155 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2156 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ | 
| Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 2157 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2158 | static void ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl, | 
| Manuel Pégourié-Gonnard | f11a6d7 | 2013-07-17 11:17:14 +0200 | [diff] [blame] | 2159 | unsigned char *buf, | 
|  | 2160 | size_t *olen ) | 
|  | 2161 | { | 
|  | 2162 | unsigned char *p = buf; | 
|  | 2163 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2164 | if( ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION ) | 
| Manuel Pégourié-Gonnard | f11a6d7 | 2013-07-17 11:17:14 +0200 | [diff] [blame] | 2165 | { | 
|  | 2166 | *olen = 0; | 
|  | 2167 | return; | 
|  | 2168 | } | 
|  | 2169 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2170 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) ); | 
| Manuel Pégourié-Gonnard | f11a6d7 | 2013-07-17 11:17:14 +0200 | [diff] [blame] | 2171 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2172 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF ); | 
|  | 2173 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO      ) & 0xFF ); | 
| Manuel Pégourié-Gonnard | f11a6d7 | 2013-07-17 11:17:14 +0200 | [diff] [blame] | 2174 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2175 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 2176 | if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ) | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2177 | { | 
|  | 2178 | *p++ = 0x00; | 
|  | 2179 | *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF; | 
|  | 2180 | *p++ = ssl->verify_data_len * 2 & 0xFF; | 
| Manuel Pégourié-Gonnard | f11a6d7 | 2013-07-17 11:17:14 +0200 | [diff] [blame] | 2181 |  | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2182 | memcpy( p, ssl->peer_verify_data, ssl->verify_data_len ); | 
|  | 2183 | p += ssl->verify_data_len; | 
|  | 2184 | memcpy( p, ssl->own_verify_data, ssl->verify_data_len ); | 
|  | 2185 | p += ssl->verify_data_len; | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2186 | } | 
|  | 2187 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2188 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2189 | { | 
|  | 2190 | *p++ = 0x00; | 
|  | 2191 | *p++ = 0x01; | 
|  | 2192 | *p++ = 0x00; | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2193 | } | 
| Manuel Pégourié-Gonnard | 1938975 | 2015-06-23 13:46:44 +0200 | [diff] [blame] | 2194 |  | 
|  | 2195 | *olen = p - buf; | 
| Manuel Pégourié-Gonnard | f11a6d7 | 2013-07-17 11:17:14 +0200 | [diff] [blame] | 2196 | } | 
|  | 2197 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2198 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
|  | 2199 | 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] | 2200 | unsigned char *buf, | 
|  | 2201 | size_t *olen ) | 
|  | 2202 | { | 
|  | 2203 | unsigned char *p = buf; | 
|  | 2204 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2205 | 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] | 2206 | { | 
| Manuel Pégourié-Gonnard | 7bb7899 | 2013-07-17 13:50:08 +0200 | [diff] [blame] | 2207 | *olen = 0; | 
|  | 2208 | return; | 
|  | 2209 | } | 
|  | 2210 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2211 | 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] | 2212 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2213 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF ); | 
|  | 2214 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH      ) & 0xFF ); | 
| Manuel Pégourié-Gonnard | 7bb7899 | 2013-07-17 13:50:08 +0200 | [diff] [blame] | 2215 |  | 
|  | 2216 | *p++ = 0x00; | 
|  | 2217 | *p++ = 1; | 
|  | 2218 |  | 
| Manuel Pégourié-Gonnard | ed4af8b | 2013-07-18 14:07:09 +0200 | [diff] [blame] | 2219 | *p++ = ssl->session_negotiate->mfl_code; | 
| Manuel Pégourié-Gonnard | 7bb7899 | 2013-07-17 13:50:08 +0200 | [diff] [blame] | 2220 |  | 
|  | 2221 | *olen = 5; | 
|  | 2222 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2223 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ | 
| Manuel Pégourié-Gonnard | 7bb7899 | 2013-07-17 13:50:08 +0200 | [diff] [blame] | 2224 |  | 
| Manuel Pégourié-Gonnard | f472179 | 2015-09-15 10:53:51 +0200 | [diff] [blame] | 2225 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ | 
| Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 2226 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2227 | 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] | 2228 | unsigned char *buf, | 
|  | 2229 | size_t *olen ) | 
|  | 2230 | { | 
|  | 2231 | unsigned char *p = buf; | 
|  | 2232 | ((void) ssl); | 
|  | 2233 |  | 
| Paul Bakker | 677377f | 2013-10-28 12:54:26 +0100 | [diff] [blame] | 2234 | if( ( ssl->handshake->cli_exts & | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2235 | MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 ) | 
| Paul Bakker | 677377f | 2013-10-28 12:54:26 +0100 | [diff] [blame] | 2236 | { | 
|  | 2237 | *olen = 0; | 
|  | 2238 | return; | 
|  | 2239 | } | 
| Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 2240 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2241 | 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] | 2242 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2243 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF ); | 
|  | 2244 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS      ) & 0xFF ); | 
| Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 2245 |  | 
|  | 2246 | *p++ = 0x00; | 
|  | 2247 | *p++ = 2; | 
|  | 2248 |  | 
|  | 2249 | *p++ = 1; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2250 | *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED; | 
| Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 2251 |  | 
|  | 2252 | *olen = 6; | 
|  | 2253 | } | 
| Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 2254 | #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] | 2255 |  | 
| Manuel Pégourié-Gonnard | 55c7f99 | 2015-09-16 15:35:27 +0200 | [diff] [blame] | 2256 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
|  | 2257 | static void ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl, | 
|  | 2258 | unsigned char *buf, | 
|  | 2259 | size_t *olen ) | 
|  | 2260 | { | 
|  | 2261 | int ret; | 
|  | 2262 | unsigned char *p = buf; | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2263 | const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN; | 
| Manuel Pégourié-Gonnard | 55c7f99 | 2015-09-16 15:35:27 +0200 | [diff] [blame] | 2264 | size_t kkpp_len; | 
|  | 2265 |  | 
|  | 2266 | *olen = 0; | 
|  | 2267 |  | 
|  | 2268 | /* Skip costly computation if not needed */ | 
|  | 2269 | if( ssl->transform_negotiate->ciphersuite_info->key_exchange != | 
|  | 2270 | MBEDTLS_KEY_EXCHANGE_ECJPAKE ) | 
|  | 2271 | return; | 
|  | 2272 |  | 
|  | 2273 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, ecjpake kkpp extension" ) ); | 
|  | 2274 |  | 
|  | 2275 | if( end - p < 4 ) | 
|  | 2276 | { | 
|  | 2277 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) ); | 
|  | 2278 | return; | 
|  | 2279 | } | 
|  | 2280 |  | 
|  | 2281 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP >> 8 ) & 0xFF ); | 
|  | 2282 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP      ) & 0xFF ); | 
|  | 2283 |  | 
| Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 2284 | ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx, | 
|  | 2285 | p + 2, end - p - 2, &kkpp_len, | 
|  | 2286 | ssl->conf->f_rng, ssl->conf->p_rng ); | 
|  | 2287 | if( ret != 0 ) | 
| Manuel Pégourié-Gonnard | 55c7f99 | 2015-09-16 15:35:27 +0200 | [diff] [blame] | 2288 | { | 
|  | 2289 | MBEDTLS_SSL_DEBUG_RET( 1 , "mbedtls_ecjpake_write_round_one", ret ); | 
|  | 2290 | return; | 
|  | 2291 | } | 
|  | 2292 |  | 
|  | 2293 | *p++ = (unsigned char)( ( kkpp_len >> 8 ) & 0xFF ); | 
|  | 2294 | *p++ = (unsigned char)( ( kkpp_len      ) & 0xFF ); | 
|  | 2295 |  | 
|  | 2296 | *olen = kkpp_len + 4; | 
|  | 2297 | } | 
|  | 2298 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ | 
|  | 2299 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2300 | #if defined(MBEDTLS_SSL_ALPN ) | 
|  | 2301 | static void ssl_write_alpn_ext( mbedtls_ssl_context *ssl, | 
| Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 2302 | unsigned char *buf, size_t *olen ) | 
|  | 2303 | { | 
|  | 2304 | if( ssl->alpn_chosen == NULL ) | 
|  | 2305 | { | 
|  | 2306 | *olen = 0; | 
|  | 2307 | return; | 
|  | 2308 | } | 
|  | 2309 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2310 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) ); | 
| Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 2311 |  | 
|  | 2312 | /* | 
|  | 2313 | * 0 . 1    ext identifier | 
|  | 2314 | * 2 . 3    ext length | 
|  | 2315 | * 4 . 5    protocol list length | 
|  | 2316 | * 6 . 6    protocol name length | 
|  | 2317 | * 7 . 7+n  protocol name | 
|  | 2318 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2319 | buf[0] = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN >> 8 ) & 0xFF ); | 
|  | 2320 | buf[1] = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN      ) & 0xFF ); | 
| Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 2321 |  | 
|  | 2322 | *olen = 7 + strlen( ssl->alpn_chosen ); | 
|  | 2323 |  | 
|  | 2324 | buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF ); | 
|  | 2325 | buf[3] = (unsigned char)( ( ( *olen - 4 )      ) & 0xFF ); | 
|  | 2326 |  | 
|  | 2327 | buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF ); | 
|  | 2328 | buf[5] = (unsigned char)( ( ( *olen - 6 )      ) & 0xFF ); | 
|  | 2329 |  | 
|  | 2330 | buf[6] = (unsigned char)( ( ( *olen - 7 )      ) & 0xFF ); | 
|  | 2331 |  | 
|  | 2332 | memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 ); | 
|  | 2333 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2334 | #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */ | 
| Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 2335 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2336 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) | 
|  | 2337 | 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] | 2338 | { | 
|  | 2339 | int ret; | 
|  | 2340 | unsigned char *p = ssl->out_msg + 4; | 
| Manuel Pégourié-Gonnard | d7f9bc5 | 2014-07-23 11:09:27 +0200 | [diff] [blame] | 2341 | unsigned char *cookie_len_byte; | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2342 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2343 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello verify request" ) ); | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2344 |  | 
|  | 2345 | /* | 
|  | 2346 | * struct { | 
|  | 2347 | *   ProtocolVersion server_version; | 
|  | 2348 | *   opaque cookie<0..2^8-1>; | 
|  | 2349 | * } HelloVerifyRequest; | 
|  | 2350 | */ | 
|  | 2351 |  | 
| Manuel Pégourié-Gonnard | b35fe56 | 2014-08-09 17:00:46 +0200 | [diff] [blame] | 2352 | /* The RFC is not clear on this point, but sending the actual negotiated | 
|  | 2353 | * version looks like the most interoperable thing to do. */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2354 | mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver, | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2355 | ssl->conf->transport, p ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2356 | MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 ); | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2357 | p += 2; | 
|  | 2358 |  | 
| Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 2359 | /* If we get here, f_cookie_check is not null */ | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2360 | if( ssl->conf->f_cookie_write == NULL ) | 
| Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 2361 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2362 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "inconsistent cookie callbacks" ) ); | 
|  | 2363 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 2364 | } | 
|  | 2365 |  | 
| Manuel Pégourié-Gonnard | d7f9bc5 | 2014-07-23 11:09:27 +0200 | [diff] [blame] | 2366 | /* Skip length byte until we know the length */ | 
|  | 2367 | cookie_len_byte = p++; | 
|  | 2368 |  | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2369 | if( ( ret = ssl->conf->f_cookie_write( ssl->conf->p_cookie, | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2370 | &p, ssl->out_buf + MBEDTLS_SSL_OUT_BUFFER_LEN, | 
| Manuel Pégourié-Gonnard | d485d19 | 2014-07-23 14:56:15 +0200 | [diff] [blame] | 2371 | ssl->cli_id, ssl->cli_id_len ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | d7f9bc5 | 2014-07-23 11:09:27 +0200 | [diff] [blame] | 2372 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2373 | MBEDTLS_SSL_DEBUG_RET( 1, "f_cookie_write", ret ); | 
| Manuel Pégourié-Gonnard | d7f9bc5 | 2014-07-23 11:09:27 +0200 | [diff] [blame] | 2374 | return( ret ); | 
|  | 2375 | } | 
|  | 2376 |  | 
|  | 2377 | *cookie_len_byte = (unsigned char)( p - ( cookie_len_byte + 1 ) ); | 
|  | 2378 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2379 | 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] | 2380 |  | 
|  | 2381 | ssl->out_msglen  = p - ssl->out_msg; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2382 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; | 
|  | 2383 | ssl->out_msg[0]  = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST; | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2384 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2385 | ssl->state = MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT; | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2386 |  | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2387 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2388 | { | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2389 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2390 | return( ret ); | 
|  | 2391 | } | 
|  | 2392 |  | 
| Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2393 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 2394 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | 
|  | 2395 | ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) | 
|  | 2396 | { | 
|  | 2397 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); | 
|  | 2398 | return( ret ); | 
|  | 2399 | } | 
| Hanno Becker | bc2498a | 2018-08-28 10:13:29 +0100 | [diff] [blame] | 2400 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2401 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2402 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello verify request" ) ); | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2403 |  | 
|  | 2404 | return( 0 ); | 
|  | 2405 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2406 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */ | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2407 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2408 | static int ssl_write_server_hello( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2409 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2410 | #if defined(MBEDTLS_HAVE_TIME) | 
| SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 2411 | mbedtls_time_t t; | 
| Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 2412 | #endif | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 2413 | int ret; | 
|  | 2414 | size_t olen, ext_len = 0, n; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2415 | unsigned char *buf, *p; | 
|  | 2416 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2417 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2418 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2419 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2420 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | 
| Manuel Pégourié-Gonnard | d7f9bc5 | 2014-07-23 11:09:27 +0200 | [diff] [blame] | 2421 | ssl->handshake->verify_cookie_len != 0 ) | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2422 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2423 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "client hello was not authenticated" ) ); | 
|  | 2424 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) ); | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2425 |  | 
| Manuel Pégourié-Gonnard | 579950c | 2014-09-29 17:47:33 +0200 | [diff] [blame] | 2426 | return( ssl_write_hello_verify_request( ssl ) ); | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2427 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2428 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */ | 
| Manuel Pégourié-Gonnard | 2c9ee81 | 2014-07-22 11:45:03 +0200 | [diff] [blame] | 2429 |  | 
| Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 2430 | if( ssl->conf->f_rng == NULL ) | 
| Paul Bakker | a9a028e | 2013-11-21 17:31:06 +0100 | [diff] [blame] | 2431 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2432 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided") ); | 
|  | 2433 | return( MBEDTLS_ERR_SSL_NO_RNG ); | 
| Paul Bakker | a9a028e | 2013-11-21 17:31:06 +0100 | [diff] [blame] | 2434 | } | 
|  | 2435 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2436 | /* | 
|  | 2437 | *     0  .   0   handshake type | 
|  | 2438 | *     1  .   3   handshake length | 
|  | 2439 | *     4  .   5   protocol version | 
|  | 2440 | *     6  .   9   UNIX time() | 
|  | 2441 | *    10  .  37   random bytes | 
|  | 2442 | */ | 
|  | 2443 | buf = ssl->out_msg; | 
|  | 2444 | p = buf + 4; | 
|  | 2445 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2446 | mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver, | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2447 | ssl->conf->transport, p ); | 
| Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 2448 | p += 2; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2449 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2450 | 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] | 2451 | buf[4], buf[5] ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2452 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2453 | #if defined(MBEDTLS_HAVE_TIME) | 
| SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 2454 | t = mbedtls_time( NULL ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2455 | *p++ = (unsigned char)( t >> 24 ); | 
|  | 2456 | *p++ = (unsigned char)( t >> 16 ); | 
|  | 2457 | *p++ = (unsigned char)( t >>  8 ); | 
|  | 2458 | *p++ = (unsigned char)( t       ); | 
|  | 2459 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2460 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) ); | 
| Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 2461 | #else | 
| Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 2462 | 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] | 2463 | return( ret ); | 
|  | 2464 |  | 
|  | 2465 | p += 4; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2466 | #endif /* MBEDTLS_HAVE_TIME */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2467 |  | 
| Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 2468 | 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] | 2469 | return( ret ); | 
|  | 2470 |  | 
|  | 2471 | p += 28; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2472 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2473 | memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2474 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2475 | MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2476 |  | 
|  | 2477 | /* | 
| Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2478 | * Resume is 0  by default, see ssl_handshake_init(). | 
|  | 2479 | * It may be already set to 1 by ssl_parse_session_ticket_ext(). | 
|  | 2480 | * If not, try looking up session ID in our cache. | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2481 | */ | 
| Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2482 | if( ssl->handshake->resume == 0 && | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2483 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 2484 | ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE && | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2485 | #endif | 
| Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 2486 | ssl->session_negotiate->id_len != 0 && | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2487 | ssl->conf->f_get_cache != NULL && | 
| Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 2488 | 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] | 2489 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2490 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) ); | 
| Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2491 | ssl->handshake->resume = 1; | 
|  | 2492 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2493 |  | 
| Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2494 | if( ssl->handshake->resume == 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2495 | { | 
|  | 2496 | /* | 
| Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2497 | * New session, create a new session id, | 
|  | 2498 | * unless we're about to issue a session ticket | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2499 | */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2500 | ssl->state++; | 
|  | 2501 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2502 | #if defined(MBEDTLS_HAVE_TIME) | 
| SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 2503 | ssl->session_negotiate->start = mbedtls_time( NULL ); | 
| Manuel Pégourié-Gonnard | 164d894 | 2013-09-23 22:01:39 +0200 | [diff] [blame] | 2504 | #endif | 
|  | 2505 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2506 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | 
| Manuel Pégourié-Gonnard | 164d894 | 2013-09-23 22:01:39 +0200 | [diff] [blame] | 2507 | if( ssl->handshake->new_session_ticket != 0 ) | 
|  | 2508 | { | 
| Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 2509 | ssl->session_negotiate->id_len = n = 0; | 
| Manuel Pégourié-Gonnard | 164d894 | 2013-09-23 22:01:39 +0200 | [diff] [blame] | 2510 | memset( ssl->session_negotiate->id, 0, 32 ); | 
|  | 2511 | } | 
|  | 2512 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2513 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ | 
| Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2514 | { | 
| Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 2515 | ssl->session_negotiate->id_len = n = 32; | 
| Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 2516 | 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] | 2517 | n ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2518 | return( ret ); | 
|  | 2519 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2520 | } | 
|  | 2521 | else | 
|  | 2522 | { | 
|  | 2523 | /* | 
| Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2524 | * Resuming a session | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2525 | */ | 
| Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 2526 | n = ssl->session_negotiate->id_len; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2527 | ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC; | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 2528 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2529 | if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 ) | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 2530 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2531 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret ); | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 2532 | return( ret ); | 
|  | 2533 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2534 | } | 
|  | 2535 |  | 
| Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2536 | /* | 
|  | 2537 | *    38  .  38     session id length | 
|  | 2538 | *    39  . 38+n    session id | 
|  | 2539 | *   39+n . 40+n    chosen ciphersuite | 
|  | 2540 | *   41+n . 41+n    chosen compression alg. | 
|  | 2541 | *   42+n . 43+n    extensions length | 
|  | 2542 | *   44+n . 43+n+m  extensions | 
|  | 2543 | */ | 
| Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 2544 | *p++ = (unsigned char) ssl->session_negotiate->id_len; | 
|  | 2545 | memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len ); | 
|  | 2546 | p += ssl->session_negotiate->id_len; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2547 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2548 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) ); | 
|  | 2549 | MBEDTLS_SSL_DEBUG_BUF( 3,   "server hello, session id", buf + 39, n ); | 
|  | 2550 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed", | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 2551 | ssl->handshake->resume ? "a" : "no" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2552 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2553 | *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 ); | 
|  | 2554 | *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite      ); | 
|  | 2555 | *p++ = (unsigned char)( ssl->session_negotiate->compression      ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2556 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2557 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s", | 
|  | 2558 | mbedtls_ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) ); | 
|  | 2559 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X", | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2560 | ssl->session_negotiate->compression ) ); | 
|  | 2561 |  | 
| Janos Follath | c6dab2b | 2016-05-23 14:27:02 +0100 | [diff] [blame] | 2562 | /* Do not write the extensions if the protocol is SSLv3 */ | 
|  | 2563 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
|  | 2564 | if( ( ssl->major_ver != 3 ) || ( ssl->minor_ver != 0 ) ) | 
|  | 2565 | { | 
|  | 2566 | #endif | 
|  | 2567 |  | 
| Manuel Pégourié-Gonnard | f11a6d7 | 2013-07-17 11:17:14 +0200 | [diff] [blame] | 2568 | /* | 
|  | 2569 | *  First write extensions, then the total length | 
|  | 2570 | */ | 
|  | 2571 | ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen ); | 
|  | 2572 | ext_len += olen; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2573 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2574 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
| Manuel Pégourié-Gonnard | 7bb7899 | 2013-07-17 13:50:08 +0200 | [diff] [blame] | 2575 | ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen ); | 
|  | 2576 | ext_len += olen; | 
| Paul Bakker | 05decb2 | 2013-08-15 13:33:48 +0200 | [diff] [blame] | 2577 | #endif | 
| Manuel Pégourié-Gonnard | 7bb7899 | 2013-07-17 13:50:08 +0200 | [diff] [blame] | 2578 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2579 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) | 
| Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 2580 | ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen ); | 
|  | 2581 | ext_len += olen; | 
| Paul Bakker | 1f2bc62 | 2013-08-15 13:45:55 +0200 | [diff] [blame] | 2582 | #endif | 
| Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 2583 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2584 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2585 | ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen ); | 
|  | 2586 | ext_len += olen; | 
|  | 2587 | #endif | 
|  | 2588 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2589 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2590 | ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen ); | 
|  | 2591 | ext_len += olen; | 
|  | 2592 | #endif | 
|  | 2593 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2594 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | 
| Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 2595 | ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen ); | 
|  | 2596 | ext_len += olen; | 
| Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 2597 | #endif | 
| Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 2598 |  | 
| Manuel Pégourié-Gonnard | f472179 | 2015-09-15 10:53:51 +0200 | [diff] [blame] | 2599 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ | 
| Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 2600 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Ron Eldor | 755bb6a | 2018-02-14 19:30:48 +0200 | [diff] [blame] | 2601 | if ( mbedtls_ssl_ciphersuite_uses_ec( | 
|  | 2602 | mbedtls_ssl_ciphersuite_from_id( ssl->session_negotiate->ciphersuite ) ) ) | 
|  | 2603 | { | 
|  | 2604 | ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen ); | 
|  | 2605 | ext_len += olen; | 
|  | 2606 | } | 
| Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 2607 | #endif | 
|  | 2608 |  | 
| Manuel Pégourié-Gonnard | 55c7f99 | 2015-09-16 15:35:27 +0200 | [diff] [blame] | 2609 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
|  | 2610 | ssl_write_ecjpake_kkpp_ext( ssl, p + 2 + ext_len, &olen ); | 
|  | 2611 | ext_len += olen; | 
|  | 2612 | #endif | 
|  | 2613 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2614 | #if defined(MBEDTLS_SSL_ALPN) | 
| Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 2615 | ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen ); | 
|  | 2616 | ext_len += olen; | 
|  | 2617 | #endif | 
|  | 2618 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2619 | 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] | 2620 |  | 
| Paul Bakker | a703663 | 2014-04-30 10:15:38 +0200 | [diff] [blame] | 2621 | if( ext_len > 0 ) | 
|  | 2622 | { | 
|  | 2623 | *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF ); | 
|  | 2624 | *p++ = (unsigned char)( ( ext_len      ) & 0xFF ); | 
|  | 2625 | p += ext_len; | 
|  | 2626 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2627 |  | 
| Janos Follath | c6dab2b | 2016-05-23 14:27:02 +0100 | [diff] [blame] | 2628 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
|  | 2629 | } | 
|  | 2630 | #endif | 
|  | 2631 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2632 | ssl->out_msglen  = p - buf; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2633 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; | 
|  | 2634 | ssl->out_msg[0]  = MBEDTLS_SSL_HS_SERVER_HELLO; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2635 |  | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2636 | ret = mbedtls_ssl_write_handshake_msg( ssl ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2637 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2638 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2639 |  | 
|  | 2640 | return( ret ); | 
|  | 2641 | } | 
|  | 2642 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2643 | #if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)       && \ | 
|  | 2644 | !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)   && \ | 
| Paul Bakker | 29f221f | 2016-07-22 13:49:02 +0100 | [diff] [blame] | 2645 | !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED)  && \ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2646 | !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \ | 
| Paul Bakker | 29f221f | 2016-07-22 13:49:02 +0100 | [diff] [blame] | 2647 | !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)&& \ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2648 | !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) | 
|  | 2649 | static int ssl_write_certificate_request( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2650 | { | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 2651 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | 
|  | 2652 | ssl->transform_negotiate->ciphersuite_info; | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2653 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2654 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2655 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2656 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || | 
|  | 2657 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK || | 
|  | 2658 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK || | 
| Manuel Pégourié-Gonnard | 25dbeb0 | 2015-09-16 17:30:03 +0200 | [diff] [blame] | 2659 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK || | 
|  | 2660 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ) | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2661 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2662 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2663 | ssl->state++; | 
|  | 2664 | return( 0 ); | 
|  | 2665 | } | 
|  | 2666 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2667 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 2668 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2669 | } | 
|  | 2670 | #else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2671 | static int ssl_write_certificate_request( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2672 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2673 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 2674 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | 
|  | 2675 | ssl->transform_negotiate->ciphersuite_info; | 
| Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 2676 | size_t dn_size, total_dn_size; /* excluding length bytes */ | 
|  | 2677 | size_t ct_len, sa_len; /* including length bytes */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2678 | unsigned char *buf, *p; | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2679 | const unsigned char * const end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2680 | const mbedtls_x509_crt *crt; | 
| Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 2681 | int authmode; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2682 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2683 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2684 |  | 
|  | 2685 | ssl->state++; | 
|  | 2686 |  | 
| Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 2687 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
|  | 2688 | if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET ) | 
|  | 2689 | authmode = ssl->handshake->sni_authmode; | 
|  | 2690 | else | 
|  | 2691 | #endif | 
|  | 2692 | authmode = ssl->conf->authmode; | 
|  | 2693 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2694 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || | 
|  | 2695 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK || | 
|  | 2696 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK || | 
|  | 2697 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK || | 
| Manuel Pégourié-Gonnard | 25dbeb0 | 2015-09-16 17:30:03 +0200 | [diff] [blame] | 2698 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE || | 
| Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 2699 | authmode == MBEDTLS_SSL_VERIFY_NONE ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2700 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2701 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2702 | return( 0 ); | 
|  | 2703 | } | 
|  | 2704 |  | 
|  | 2705 | /* | 
|  | 2706 | *     0  .   0   handshake type | 
|  | 2707 | *     1  .   3   handshake length | 
|  | 2708 | *     4  .   4   cert type count | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2709 | *     5  .. m-1  cert types | 
|  | 2710 | *     m  .. m+1  sig alg length (TLS 1.2 only) | 
| Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 2711 | *    m+1 .. n-1  SignatureAndHashAlgorithms (TLS 1.2 only) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2712 | *     n  .. n+1  length of all DNs | 
|  | 2713 | *    n+2 .. n+3  length of DN 1 | 
|  | 2714 | *    n+4 .. ...  Distinguished Name #1 | 
|  | 2715 | *    ... .. ...  length of DN 2, etc. | 
|  | 2716 | */ | 
|  | 2717 | buf = ssl->out_msg; | 
|  | 2718 | p = buf + 4; | 
|  | 2719 |  | 
|  | 2720 | /* | 
| Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 2721 | * Supported certificate types | 
|  | 2722 | * | 
|  | 2723 | *     ClientCertificateType certificate_types<1..2^8-1>; | 
|  | 2724 | *     enum { (255) } ClientCertificateType; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2725 | */ | 
| Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 2726 | ct_len = 0; | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2727 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2728 | #if defined(MBEDTLS_RSA_C) | 
|  | 2729 | p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_RSA_SIGN; | 
| Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 2730 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2731 | #if defined(MBEDTLS_ECDSA_C) | 
|  | 2732 | p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_ECDSA_SIGN; | 
| Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 2733 | #endif | 
|  | 2734 |  | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 2735 | p[0] = (unsigned char) ct_len++; | 
| Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 2736 | p += ct_len; | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2737 |  | 
| Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 2738 | sa_len = 0; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2739 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2740 | /* | 
|  | 2741 | * Add signature_algorithms for verify (TLS 1.2) | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2742 | * | 
| Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 2743 | *     SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>; | 
|  | 2744 | * | 
|  | 2745 | *     struct { | 
|  | 2746 | *           HashAlgorithm hash; | 
|  | 2747 | *           SignatureAlgorithm signature; | 
|  | 2748 | *     } SignatureAndHashAlgorithm; | 
|  | 2749 | * | 
|  | 2750 | *     enum { (255) } HashAlgorithm; | 
|  | 2751 | *     enum { (255) } SignatureAlgorithm; | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2752 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2753 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2754 | { | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 2755 | const int *cur; | 
| Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 2756 |  | 
| Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 2757 | /* | 
|  | 2758 | * Supported signature algorithms | 
|  | 2759 | */ | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 2760 | for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ ) | 
|  | 2761 | { | 
|  | 2762 | unsigned char hash = mbedtls_ssl_hash_from_md_alg( *cur ); | 
|  | 2763 |  | 
|  | 2764 | if( MBEDTLS_SSL_HASH_NONE == hash || mbedtls_ssl_set_calc_verify_md( ssl, hash ) ) | 
|  | 2765 | continue; | 
|  | 2766 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2767 | #if defined(MBEDTLS_RSA_C) | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 2768 | p[2 + sa_len++] = hash; | 
|  | 2769 | p[2 + sa_len++] = MBEDTLS_SSL_SIG_RSA; | 
| Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 2770 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2771 | #if defined(MBEDTLS_ECDSA_C) | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 2772 | p[2 + sa_len++] = hash; | 
|  | 2773 | p[2 + sa_len++] = MBEDTLS_SSL_SIG_ECDSA; | 
| Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 2774 | #endif | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 2775 | } | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2776 |  | 
| Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 2777 | p[0] = (unsigned char)( sa_len >> 8 ); | 
|  | 2778 | p[1] = (unsigned char)( sa_len      ); | 
|  | 2779 | sa_len += 2; | 
|  | 2780 | p += sa_len; | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2781 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2782 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2783 |  | 
| Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 2784 | /* | 
|  | 2785 | * DistinguishedName certificate_authorities<0..2^16-1>; | 
|  | 2786 | * opaque DistinguishedName<1..2^16-1>; | 
|  | 2787 | */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2788 | p += 2; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2789 |  | 
| Paul Bakker | bc3d984 | 2012-11-26 16:12:02 +0100 | [diff] [blame] | 2790 | total_dn_size = 0; | 
| Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 2791 |  | 
|  | 2792 | 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] | 2793 | { | 
| Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 2794 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
|  | 2795 | if( ssl->handshake->sni_ca_chain != NULL ) | 
|  | 2796 | crt = ssl->handshake->sni_ca_chain; | 
|  | 2797 | else | 
|  | 2798 | #endif | 
|  | 2799 | crt = ssl->conf->ca_chain; | 
| Manuel Pégourié-Gonnard | bc1babb | 2015-10-02 11:16:47 +0200 | [diff] [blame] | 2800 |  | 
| Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 2801 | while( crt != NULL && crt->version != 0 ) | 
| Manuel Pégourié-Gonnard | bc1babb | 2015-10-02 11:16:47 +0200 | [diff] [blame] | 2802 | { | 
| Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 2803 | dn_size = crt->subject_raw.len; | 
|  | 2804 |  | 
|  | 2805 | if( end < p || | 
|  | 2806 | (size_t)( end - p ) < dn_size || | 
|  | 2807 | (size_t)( end - p ) < 2 + dn_size ) | 
|  | 2808 | { | 
|  | 2809 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "skipping CAs: buffer too short" ) ); | 
|  | 2810 | break; | 
|  | 2811 | } | 
|  | 2812 |  | 
|  | 2813 | *p++ = (unsigned char)( dn_size >> 8 ); | 
|  | 2814 | *p++ = (unsigned char)( dn_size      ); | 
|  | 2815 | memcpy( p, crt->subject_raw.p, dn_size ); | 
|  | 2816 | p += dn_size; | 
|  | 2817 |  | 
|  | 2818 | MBEDTLS_SSL_DEBUG_BUF( 3, "requested DN", p - dn_size, dn_size ); | 
|  | 2819 |  | 
|  | 2820 | total_dn_size += 2 + dn_size; | 
|  | 2821 | crt = crt->next; | 
| Manuel Pégourié-Gonnard | bc1babb | 2015-10-02 11:16:47 +0200 | [diff] [blame] | 2822 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2823 | } | 
|  | 2824 |  | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2825 | ssl->out_msglen  = p - buf; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2826 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; | 
|  | 2827 | ssl->out_msg[0]  = MBEDTLS_SSL_HS_CERTIFICATE_REQUEST; | 
| Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 2828 | ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size  >> 8 ); | 
|  | 2829 | 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] | 2830 |  | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2831 | ret = mbedtls_ssl_write_handshake_msg( ssl ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2832 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2833 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2834 |  | 
|  | 2835 | return( ret ); | 
|  | 2836 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2837 | #endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED && | 
|  | 2838 | !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED && | 
| Paul Bakker | 29f221f | 2016-07-22 13:49:02 +0100 | [diff] [blame] | 2839 | !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED && | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2840 | !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED && | 
| Paul Bakker | 29f221f | 2016-07-22 13:49:02 +0100 | [diff] [blame] | 2841 | !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED && | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2842 | !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2843 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2844 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ | 
|  | 2845 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) | 
|  | 2846 | 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] | 2847 | { | 
|  | 2848 | int ret; | 
|  | 2849 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2850 | 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] | 2851 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2852 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) ); | 
|  | 2853 | return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH ); | 
| Manuel Pégourié-Gonnard | 5538970 | 2013-12-12 11:14:16 +0100 | [diff] [blame] | 2854 | } | 
|  | 2855 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2856 | if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx, | 
|  | 2857 | mbedtls_pk_ec( *mbedtls_ssl_own_key( ssl ) ), | 
|  | 2858 | MBEDTLS_ECDH_OURS ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 5538970 | 2013-12-12 11:14:16 +0100 | [diff] [blame] | 2859 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2860 | MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret ); | 
| Manuel Pégourié-Gonnard | 5538970 | 2013-12-12 11:14:16 +0100 | [diff] [blame] | 2861 | return( ret ); | 
|  | 2862 | } | 
|  | 2863 |  | 
|  | 2864 | return( 0 ); | 
|  | 2865 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2866 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || | 
|  | 2867 | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */ | 
| Manuel Pégourié-Gonnard | 5538970 | 2013-12-12 11:14:16 +0100 | [diff] [blame] | 2868 |  | 
| Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 2869 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) && \ | 
| Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 2870 | defined(MBEDTLS_SSL_ASYNC_PRIVATE) | 
| Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 2871 | static int ssl_resume_server_key_exchange( mbedtls_ssl_context *ssl, | 
| Gilles Peskine | 0fd90dd | 2018-04-26 07:41:09 +0200 | [diff] [blame] | 2872 | size_t *signature_len ) | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2873 | { | 
| Gilles Peskine | 0fd90dd | 2018-04-26 07:41:09 +0200 | [diff] [blame] | 2874 | /* Append the signature to ssl->out_msg, leaving 2 bytes for the | 
|  | 2875 | * signature length which will be added in ssl_write_server_key_exchange | 
|  | 2876 | * after the call to ssl_prepare_server_key_exchange. | 
|  | 2877 | * ssl_write_server_key_exchange also takes care of incrementing | 
|  | 2878 | * ssl->out_msglen. */ | 
|  | 2879 | unsigned char *sig_start = ssl->out_msg + ssl->out_msglen + 2; | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2880 | size_t sig_max_len = ( ssl->out_buf + MBEDTLS_SSL_OUT_CONTENT_LEN | 
| Gilles Peskine | 0fd90dd | 2018-04-26 07:41:09 +0200 | [diff] [blame] | 2881 | - sig_start ); | 
| Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 2882 | int ret = ssl->conf->f_async_resume( ssl, | 
| Gilles Peskine | 0fd90dd | 2018-04-26 07:41:09 +0200 | [diff] [blame] | 2883 | sig_start, signature_len, sig_max_len ); | 
| Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 2884 | if( ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS ) | 
|  | 2885 | { | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2886 | ssl->handshake->async_in_progress = 0; | 
| Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 2887 | mbedtls_ssl_set_async_operation_data( ssl, NULL ); | 
| Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 2888 | } | 
| Gilles Peskine | d3eb061 | 2018-01-08 17:07:44 +0100 | [diff] [blame] | 2889 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl_resume_server_key_exchange", ret ); | 
| Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 2890 | return( ret ); | 
|  | 2891 | } | 
|  | 2892 | #endif /* defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) && | 
| Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 2893 | defined(MBEDTLS_SSL_ASYNC_PRIVATE) */ | 
| Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 2894 |  | 
| Gilles Peskine | d3eb061 | 2018-01-08 17:07:44 +0100 | [diff] [blame] | 2895 | /* Prepare the ServerKeyExchange message, up to and including | 
| Gilles Peskine | 168dae8 | 2018-04-25 23:35:42 +0200 | [diff] [blame] | 2896 | * calculating the signature if any, but excluding formatting the | 
|  | 2897 | * signature and sending the message. */ | 
| Gilles Peskine | 7ab013a | 2018-01-08 17:04:16 +0100 | [diff] [blame] | 2898 | static int ssl_prepare_server_key_exchange( mbedtls_ssl_context *ssl, | 
|  | 2899 | size_t *signature_len ) | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 2900 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2901 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | 
| Manuel Pégourié-Gonnard | 09258b9 | 2013-10-15 10:43:36 +0200 | [diff] [blame] | 2902 | ssl->transform_negotiate->ciphersuite_info; | 
| Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 2903 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME_PFS__ENABLED) | 
| Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 2904 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) | 
| Gilles Peskine | 3ce9b90 | 2018-01-06 01:34:21 +0100 | [diff] [blame] | 2905 | unsigned char *dig_signed = NULL; | 
| Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 2906 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED */ | 
|  | 2907 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME_PFS__ENABLED */ | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2908 |  | 
| Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 2909 | (void) ciphersuite_info; /* unused in some configurations */ | 
| Gilles Peskine | 22e695f | 2018-04-26 00:22:50 +0200 | [diff] [blame] | 2910 | #if !defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) | 
|  | 2911 | (void) signature_len; | 
|  | 2912 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED */ | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2913 |  | 
| Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 2914 | ssl->out_msglen = 4; /* header (type:1, length:3) to be written later */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2915 |  | 
| Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 2916 | /* | 
|  | 2917 | * | 
| Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 2918 | * Part 1: Provide key exchange parameters for chosen ciphersuite. | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 2919 | * | 
|  | 2920 | */ | 
|  | 2921 |  | 
|  | 2922 | /* | 
|  | 2923 | * - ECJPAKE key exchanges | 
|  | 2924 | */ | 
| Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 2925 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
|  | 2926 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ) | 
|  | 2927 | { | 
| Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 2928 | int ret; | 
| Simon Butcher | 600c5e6 | 2018-06-14 08:58:59 +0100 | [diff] [blame] | 2929 | size_t len = 0; | 
| Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 2930 |  | 
| Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 2931 | ret = mbedtls_ecjpake_write_round_two( | 
|  | 2932 | &ssl->handshake->ecjpake_ctx, | 
|  | 2933 | ssl->out_msg + ssl->out_msglen, | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2934 | MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen, &len, | 
| Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 2935 | ssl->conf->f_rng, ssl->conf->p_rng ); | 
| Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 2936 | if( ret != 0 ) | 
|  | 2937 | { | 
|  | 2938 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret ); | 
|  | 2939 | return( ret ); | 
|  | 2940 | } | 
|  | 2941 |  | 
| Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 2942 | ssl->out_msglen += len; | 
| Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 2943 | } | 
|  | 2944 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ | 
|  | 2945 |  | 
| Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 2946 | /* | 
|  | 2947 | * For (EC)DHE key exchanges with PSK, parameters are prefixed by support | 
|  | 2948 | * identity hint (RFC 4279, Sec. 3). Until someone needs this feature, | 
|  | 2949 | * we use empty support identity hints here. | 
|  | 2950 | **/ | 
|  | 2951 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)   || \ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2952 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) | 
|  | 2953 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK || | 
|  | 2954 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2955 | { | 
| Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 2956 | ssl->out_msg[ssl->out_msglen++] = 0x00; | 
|  | 2957 | ssl->out_msg[ssl->out_msglen++] = 0x00; | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2958 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2959 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED || | 
|  | 2960 | MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2961 |  | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 2962 | /* | 
| Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 2963 | * - DHE key exchanges | 
| Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 2964 | */ | 
|  | 2965 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__DHE_ENABLED) | 
|  | 2966 | if( mbedtls_ssl_ciphersuite_uses_dhe( ciphersuite_info ) ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2967 | { | 
| Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 2968 | int ret; | 
| Simon Butcher | 600c5e6 | 2018-06-14 08:58:59 +0100 | [diff] [blame] | 2969 | size_t len = 0; | 
| Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 2970 |  | 
| Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 2971 | if( ssl->conf->dhm_P.p == NULL || ssl->conf->dhm_G.p == NULL ) | 
|  | 2972 | { | 
|  | 2973 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no DH parameters set" ) ); | 
|  | 2974 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 2975 | } | 
|  | 2976 |  | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2977 | /* | 
|  | 2978 | * Ephemeral DH parameters: | 
|  | 2979 | * | 
|  | 2980 | * struct { | 
|  | 2981 | *     opaque dh_p<1..2^16-1>; | 
|  | 2982 | *     opaque dh_g<1..2^16-1>; | 
|  | 2983 | *     opaque dh_Ys<1..2^16-1>; | 
|  | 2984 | * } ServerDHParams; | 
|  | 2985 | */ | 
| Hanno Becker | ab74056 | 2017-10-04 13:15:37 +0100 | [diff] [blame] | 2986 | if( ( ret = mbedtls_dhm_set_group( &ssl->handshake->dhm_ctx, | 
|  | 2987 | &ssl->conf->dhm_P, | 
|  | 2988 | &ssl->conf->dhm_G ) ) != 0 ) | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2989 | { | 
| Hanno Becker | ab74056 | 2017-10-04 13:15:37 +0100 | [diff] [blame] | 2990 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_set_group", ret ); | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2991 | return( ret ); | 
|  | 2992 | } | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2993 |  | 
| Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 2994 | if( ( ret = mbedtls_dhm_make_params( | 
|  | 2995 | &ssl->handshake->dhm_ctx, | 
|  | 2996 | (int) mbedtls_mpi_size( &ssl->handshake->dhm_ctx.P ), | 
|  | 2997 | ssl->out_msg + ssl->out_msglen, &len, | 
|  | 2998 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2999 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3000 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_params", ret ); | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3001 | return( ret ); | 
|  | 3002 | } | 
|  | 3003 |  | 
| Hanno Becker | 4cb1f4d | 2017-10-10 15:59:57 +0100 | [diff] [blame] | 3004 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) | 
| Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 3005 | dig_signed = ssl->out_msg + ssl->out_msglen; | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3006 | #endif | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3007 |  | 
| Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 3008 | ssl->out_msglen += len; | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3009 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3010 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X  ); | 
|  | 3011 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P  ); | 
|  | 3012 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G  ); | 
|  | 3013 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX ); | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3014 | } | 
| Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3015 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__DHE_ENABLED */ | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3016 |  | 
| Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3017 | /* | 
| Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3018 | * - ECDHE key exchanges | 
| Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3019 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3020 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__ECDHE_ENABLED) | 
| Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3021 | if( mbedtls_ssl_ciphersuite_uses_ecdhe( ciphersuite_info ) ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3022 | { | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3023 | /* | 
|  | 3024 | * Ephemeral ECDH parameters: | 
|  | 3025 | * | 
|  | 3026 | * struct { | 
|  | 3027 | *     ECParameters curve_params; | 
|  | 3028 | *     ECPoint      public; | 
|  | 3029 | * } ServerECDHParams; | 
|  | 3030 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3031 | const mbedtls_ecp_curve_info **curve = NULL; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3032 | const mbedtls_ecp_group_id *gid; | 
| Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3033 | int ret; | 
| Simon Butcher | 600c5e6 | 2018-06-14 08:58:59 +0100 | [diff] [blame] | 3034 | size_t len = 0; | 
| Gergely Budai | 987bfb5 | 2014-01-19 21:48:42 +0100 | [diff] [blame] | 3035 |  | 
| Manuel Pégourié-Gonnard | c3f6b62c | 2014-02-06 10:13:09 +0100 | [diff] [blame] | 3036 | /* Match our preference list against the offered curves */ | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3037 | 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] | 3038 | for( curve = ssl->handshake->curves; *curve != NULL; curve++ ) | 
|  | 3039 | if( (*curve)->grp_id == *gid ) | 
|  | 3040 | goto curve_matching_done; | 
|  | 3041 |  | 
|  | 3042 | curve_matching_done: | 
| Manuel Pégourié-Gonnard | b86145e | 2015-06-23 14:11:39 +0200 | [diff] [blame] | 3043 | if( curve == NULL || *curve == NULL ) | 
| Gergely Budai | 987bfb5 | 2014-01-19 21:48:42 +0100 | [diff] [blame] | 3044 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3045 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) ); | 
|  | 3046 | return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN ); | 
| Gergely Budai | 987bfb5 | 2014-01-19 21:48:42 +0100 | [diff] [blame] | 3047 | } | 
| Manuel Pégourié-Gonnard | de05390 | 2014-02-04 13:58:39 +0100 | [diff] [blame] | 3048 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3049 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) ); | 
| Gergely Budai | 987bfb5 | 2014-01-19 21:48:42 +0100 | [diff] [blame] | 3050 |  | 
| Janos Follath | 3fbdada | 2018-08-15 10:26:53 +0100 | [diff] [blame] | 3051 | if( ( ret = mbedtls_ecdh_setup( &ssl->handshake->ecdh_ctx, | 
|  | 3052 | (*curve)->grp_id ) ) != 0 ) | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3053 | { | 
| Manuel Pégourié-Gonnard | e3a062b | 2015-05-11 18:46:47 +0200 | [diff] [blame] | 3054 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecp_group_load", ret ); | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3055 | return( ret ); | 
|  | 3056 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3057 |  | 
| Gilles Peskine | fe1c093 | 2017-11-23 13:35:02 +0100 | [diff] [blame] | 3058 | if( ( ret = mbedtls_ecdh_make_params( | 
|  | 3059 | &ssl->handshake->ecdh_ctx, &len, | 
| Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 3060 | ssl->out_msg + ssl->out_msglen, | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3061 | MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen, | 
| Gilles Peskine | fe1c093 | 2017-11-23 13:35:02 +0100 | [diff] [blame] | 3062 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3063 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3064 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_params", ret ); | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3065 | return( ret ); | 
|  | 3066 | } | 
|  | 3067 |  | 
| Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3068 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) | 
| Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 3069 | dig_signed = ssl->out_msg + ssl->out_msglen; | 
| Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3070 | #endif | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3071 |  | 
| Gilles Peskine | f9f15ae | 2018-01-08 17:13:01 +0100 | [diff] [blame] | 3072 | ssl->out_msglen += len; | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3073 |  | 
| Janos Follath | 3fbdada | 2018-08-15 10:26:53 +0100 | [diff] [blame] | 3074 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, | 
|  | 3075 | MBEDTLS_DEBUG_ECDH_Q ); | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3076 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3077 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__ECDHE_ENABLED */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3078 |  | 
| Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3079 | /* | 
| Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3080 | * | 
| Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3081 | * Part 2: For key exchanges involving the server signing the | 
| Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3082 | *         exchange parameters, compute and add the signature here. | 
|  | 3083 | * | 
| Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3084 | */ | 
|  | 3085 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) | 
|  | 3086 | if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) ) | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3087 | { | 
| Gilles Peskine | 1004c19 | 2018-01-08 16:59:14 +0100 | [diff] [blame] | 3088 | 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] | 3089 | size_t hashlen = 0; | 
| Gilles Peskine | e1efdf9 | 2018-01-05 21:18:37 +0100 | [diff] [blame] | 3090 | unsigned char hash[MBEDTLS_MD_MAX_SIZE]; | 
| Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3091 | int ret; | 
| Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 3092 |  | 
| Manuel Pégourié-Gonnard | abae74c | 2013-08-20 13:53:44 +0200 | [diff] [blame] | 3093 | /* | 
| Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3094 | * 2.1: Choose hash algorithm: | 
| Hanno Becker | 4cb1f4d | 2017-10-10 15:59:57 +0100 | [diff] [blame] | 3095 | * A: For TLS 1.2, obey signature-hash-algorithm extension | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3096 | *    to choose appropriate hash. | 
|  | 3097 | * B: For SSL3, TLS1.0, TLS1.1 and ECDHE_ECDSA, use SHA1 | 
|  | 3098 | *    (RFC 4492, Sec. 5.4) | 
|  | 3099 | * 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] | 3100 | */ | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 3101 |  | 
|  | 3102 | mbedtls_md_type_t md_alg; | 
|  | 3103 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3104 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3105 | mbedtls_pk_type_t sig_alg = | 
|  | 3106 | mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3107 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) | 
| Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3108 | { | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3109 | /* A: For TLS 1.2, obey signature-hash-algorithm extension | 
|  | 3110 | *    (RFC 5246, Sec. 7.4.1.4.1). */ | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 3111 | if( sig_alg == MBEDTLS_PK_NONE || | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3112 | ( md_alg = mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs, | 
|  | 3113 | sig_alg ) ) == MBEDTLS_MD_NONE ) | 
| Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3114 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3115 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
| Hanno Becker | 4cb1f4d | 2017-10-10 15:59:57 +0100 | [diff] [blame] | 3116 | /* (... because we choose a cipher suite | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3117 | *      only if there is a matching hash.) */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3118 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3119 | } | 
|  | 3120 | } | 
| Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 3121 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3122 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 3123 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ | 
|  | 3124 | defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
| Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3125 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ) | 
| Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3126 | { | 
| Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3127 | /* B: Default hash SHA1 */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3128 | md_alg = MBEDTLS_MD_SHA1; | 
| Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3129 | } | 
|  | 3130 | else | 
| Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3131 | #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \ | 
|  | 3132 | MBEDTLS_SSL_PROTO_TLS1_1 */ | 
| Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3133 | { | 
| Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3134 | /* C: MD5 + SHA1 */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3135 | md_alg = MBEDTLS_MD_NONE; | 
| Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3136 | } | 
|  | 3137 |  | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 3138 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "pick hash algorithm %d for signing", md_alg ) ); | 
|  | 3139 |  | 
| Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3140 | /* | 
| Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3141 | * 2.2: Compute the hash to be signed | 
| Manuel Pégourié-Gonnard | abae74c | 2013-08-20 13:53:44 +0200 | [diff] [blame] | 3142 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3143 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ | 
|  | 3144 | defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
|  | 3145 | if( md_alg == MBEDTLS_MD_NONE ) | 
| Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 3146 | { | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3147 | hashlen = 36; | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 3148 | ret = mbedtls_ssl_get_key_exchange_md_ssl_tls( ssl, hash, | 
|  | 3149 | dig_signed, | 
|  | 3150 | dig_signed_len ); | 
|  | 3151 | if( ret != 0 ) | 
|  | 3152 | return( ret ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3153 | } | 
|  | 3154 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3155 | #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \ | 
|  | 3156 | MBEDTLS_SSL_PROTO_TLS1_1 */ | 
|  | 3157 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ | 
|  | 3158 | defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 3159 | if( md_alg != MBEDTLS_MD_NONE ) | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3160 | { | 
| Gilles Peskine | ca1d742 | 2018-04-24 11:53:22 +0200 | [diff] [blame] | 3161 | 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] | 3162 | dig_signed, | 
|  | 3163 | dig_signed_len, | 
|  | 3164 | md_alg ); | 
|  | 3165 | if( ret != 0 ) | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3166 | return( ret ); | 
| Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 3167 | } | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 3168 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3169 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ | 
|  | 3170 | MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 3171 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3172 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 3173 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 3174 | } | 
| Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 3175 |  | 
| Gilles Peskine | ebd652f | 2018-01-05 21:18:59 +0100 | [diff] [blame] | 3176 | MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3177 |  | 
| Manuel Pégourié-Gonnard | abae74c | 2013-08-20 13:53:44 +0200 | [diff] [blame] | 3178 | /* | 
| Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3179 | * 2.3: Compute and add the signature | 
| Manuel Pégourié-Gonnard | abae74c | 2013-08-20 13:53:44 +0200 | [diff] [blame] | 3180 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3181 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 3182 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) | 
| Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 3183 | { | 
| Hanno Becker | cf7ae7e | 2017-05-11 14:07:25 +0100 | [diff] [blame] | 3184 | /* | 
|  | 3185 | * For TLS 1.2, we need to specify signature and hash algorithm | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 3186 | * explicitly through a prefix to the signature. | 
|  | 3187 | * | 
|  | 3188 | * struct { | 
|  | 3189 | *    HashAlgorithm hash; | 
|  | 3190 | *    SignatureAlgorithm signature; | 
|  | 3191 | * } SignatureAndHashAlgorithm; | 
|  | 3192 | * | 
|  | 3193 | * struct { | 
|  | 3194 | *    SignatureAndHashAlgorithm algorithm; | 
|  | 3195 | *    opaque signature<0..2^16-1>; | 
|  | 3196 | * } DigitallySigned; | 
|  | 3197 | * | 
|  | 3198 | */ | 
|  | 3199 |  | 
| Gilles Peskine | 1004c19 | 2018-01-08 16:59:14 +0100 | [diff] [blame] | 3200 | ssl->out_msg[ssl->out_msglen++] = | 
|  | 3201 | mbedtls_ssl_hash_from_md_alg( md_alg ); | 
|  | 3202 | ssl->out_msg[ssl->out_msglen++] = | 
|  | 3203 | mbedtls_ssl_sig_from_pk_alg( sig_alg ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3204 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3205 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3206 |  | 
| Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3207 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) | 
| Gilles Peskine | 4bf9a28 | 2018-01-05 21:20:50 +0100 | [diff] [blame] | 3208 | if( ssl->conf->f_async_sign_start != NULL ) | 
|  | 3209 | { | 
| Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 3210 | ret = ssl->conf->f_async_sign_start( ssl, | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3211 | mbedtls_ssl_own_cert( ssl ), | 
|  | 3212 | md_alg, hash, hashlen ); | 
| Gilles Peskine | 4bf9a28 | 2018-01-05 21:20:50 +0100 | [diff] [blame] | 3213 | switch( ret ) | 
|  | 3214 | { | 
|  | 3215 | case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH: | 
|  | 3216 | /* act as if f_async_sign was null */ | 
|  | 3217 | break; | 
|  | 3218 | case 0: | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3219 | ssl->handshake->async_in_progress = 1; | 
| Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3220 | return( ssl_resume_server_key_exchange( ssl, signature_len ) ); | 
| Gilles Peskine | 4bf9a28 | 2018-01-05 21:20:50 +0100 | [diff] [blame] | 3221 | case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS: | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3222 | ssl->handshake->async_in_progress = 1; | 
| Gilles Peskine | 4bf9a28 | 2018-01-05 21:20:50 +0100 | [diff] [blame] | 3223 | return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS ); | 
|  | 3224 | default: | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3225 | MBEDTLS_SSL_DEBUG_RET( 1, "f_async_sign_start", ret ); | 
| Gilles Peskine | 4bf9a28 | 2018-01-05 21:20:50 +0100 | [diff] [blame] | 3226 | return( ret ); | 
|  | 3227 | } | 
|  | 3228 | } | 
| Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3229 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ | 
| Gilles Peskine | 4bf9a28 | 2018-01-05 21:20:50 +0100 | [diff] [blame] | 3230 |  | 
|  | 3231 | if( mbedtls_ssl_own_key( ssl ) == NULL ) | 
|  | 3232 | { | 
|  | 3233 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key" ) ); | 
|  | 3234 | return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ); | 
|  | 3235 | } | 
|  | 3236 |  | 
| Gilles Peskine | 0fd90dd | 2018-04-26 07:41:09 +0200 | [diff] [blame] | 3237 | /* Append the signature to ssl->out_msg, leaving 2 bytes for the | 
|  | 3238 | * signature length which will be added in ssl_write_server_key_exchange | 
|  | 3239 | * after the call to ssl_prepare_server_key_exchange. | 
|  | 3240 | * ssl_write_server_key_exchange also takes care of incrementing | 
|  | 3241 | * ssl->out_msglen. */ | 
| Gilles Peskine | 1004c19 | 2018-01-08 16:59:14 +0100 | [diff] [blame] | 3242 | if( ( ret = mbedtls_pk_sign( mbedtls_ssl_own_key( ssl ), | 
|  | 3243 | md_alg, hash, hashlen, | 
|  | 3244 | ssl->out_msg + ssl->out_msglen + 2, | 
| Gilles Peskine | 7ab013a | 2018-01-08 17:04:16 +0100 | [diff] [blame] | 3245 | signature_len, | 
| Gilles Peskine | 1004c19 | 2018-01-08 16:59:14 +0100 | [diff] [blame] | 3246 | ssl->conf->f_rng, | 
|  | 3247 | ssl->conf->p_rng ) ) != 0 ) | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3248 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3249 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret ); | 
| Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 3250 | return( ret ); | 
| Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 3251 | } | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3252 | } | 
| Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3253 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED */ | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3254 |  | 
| Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3255 | return( 0 ); | 
|  | 3256 | } | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3257 |  | 
| Gilles Peskine | d3eb061 | 2018-01-08 17:07:44 +0100 | [diff] [blame] | 3258 | /* Prepare the ServerKeyExchange message and send it. For ciphersuites | 
| Gilles Peskine | 168dae8 | 2018-04-25 23:35:42 +0200 | [diff] [blame] | 3259 | * that do not include a ServerKeyExchange message, do nothing. Either | 
|  | 3260 | * way, if successful, move on to the next step in the SSL state | 
|  | 3261 | * machine. */ | 
| Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3262 | static int ssl_write_server_key_exchange( mbedtls_ssl_context *ssl ) | 
|  | 3263 | { | 
|  | 3264 | int ret; | 
| Gilles Peskine | 7ab013a | 2018-01-08 17:04:16 +0100 | [diff] [blame] | 3265 | size_t signature_len = 0; | 
| Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3266 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME_NON_PFS__ENABLED) | 
|  | 3267 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | 
|  | 3268 | ssl->transform_negotiate->ciphersuite_info; | 
| Gilles Peskine | f112725 | 2018-04-24 13:05:39 +0200 | [diff] [blame] | 3269 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME_NON_PFS__ENABLED */ | 
| Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3270 |  | 
| Gilles Peskine | d3eb061 | 2018-01-08 17:07:44 +0100 | [diff] [blame] | 3271 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) ); | 
|  | 3272 |  | 
|  | 3273 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME_NON_PFS__ENABLED) | 
|  | 3274 | /* Extract static ECDH parameters and abort if ServerKeyExchange | 
|  | 3275 | * is not needed. */ | 
| Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3276 | if( mbedtls_ssl_ciphersuite_no_pfs( ciphersuite_info ) ) | 
|  | 3277 | { | 
|  | 3278 | /* For suites involving ECDH, extract DH parameters | 
|  | 3279 | * from certificate at this point. */ | 
|  | 3280 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__ECDH_ENABLED) | 
|  | 3281 | if( mbedtls_ssl_ciphersuite_uses_ecdh( ciphersuite_info ) ) | 
|  | 3282 | { | 
|  | 3283 | ssl_get_ecdh_params_from_cert( ssl ); | 
|  | 3284 | } | 
|  | 3285 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__ECDH_ENABLED */ | 
|  | 3286 |  | 
|  | 3287 | /* Key exchanges not involving ephemeral keys don't use | 
|  | 3288 | * ServerKeyExchange, so end here. */ | 
|  | 3289 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) ); | 
|  | 3290 | ssl->state++; | 
|  | 3291 | return( 0 ); | 
|  | 3292 | } | 
| Gilles Peskine | f112725 | 2018-04-24 13:05:39 +0200 | [diff] [blame] | 3293 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME_NON_PFS__ENABLED */ | 
| Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3294 |  | 
| Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3295 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) && \ | 
| Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3296 | defined(MBEDTLS_SSL_ASYNC_PRIVATE) | 
| Gilles Peskine | d3eb061 | 2018-01-08 17:07:44 +0100 | [diff] [blame] | 3297 | /* If we have already prepared the message and there is an ongoing | 
| Gilles Peskine | 168dae8 | 2018-04-25 23:35:42 +0200 | [diff] [blame] | 3298 | * signature operation, resume signing. */ | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3299 | if( ssl->handshake->async_in_progress != 0 ) | 
| Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3300 | { | 
|  | 3301 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "resuming signature operation" ) ); | 
|  | 3302 | ret = ssl_resume_server_key_exchange( ssl, &signature_len ); | 
| Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3303 | } | 
|  | 3304 | else | 
|  | 3305 | #endif /* defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) && | 
| Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3306 | defined(MBEDTLS_SSL_ASYNC_PRIVATE) */ | 
| Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3307 | { | 
|  | 3308 | /* ServerKeyExchange is needed. Prepare the message. */ | 
|  | 3309 | ret = ssl_prepare_server_key_exchange( ssl, &signature_len ); | 
| Gilles Peskine | d3eb061 | 2018-01-08 17:07:44 +0100 | [diff] [blame] | 3310 | } | 
|  | 3311 |  | 
|  | 3312 | if( ret != 0 ) | 
|  | 3313 | { | 
| Gilles Peskine | ad28bf0 | 2018-04-26 00:19:16 +0200 | [diff] [blame] | 3314 | /* If we're starting to write a new message, set ssl->out_msglen | 
|  | 3315 | * to 0. But if we're resuming after an asynchronous message, | 
|  | 3316 | * out_msglen is the amount of data written so far and mst be | 
|  | 3317 | * preserved. */ | 
| Gilles Peskine | d3eb061 | 2018-01-08 17:07:44 +0100 | [diff] [blame] | 3318 | if( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS ) | 
|  | 3319 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server key exchange (pending)" ) ); | 
|  | 3320 | else | 
|  | 3321 | ssl->out_msglen = 0; | 
|  | 3322 | return( ret ); | 
| Gilles Peskine | ebd30ae | 2018-01-06 03:34:20 +0100 | [diff] [blame] | 3323 | } | 
| Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3324 |  | 
| Gilles Peskine | 7ab013a | 2018-01-08 17:04:16 +0100 | [diff] [blame] | 3325 | /* If there is a signature, write its length. | 
| Gilles Peskine | 168dae8 | 2018-04-25 23:35:42 +0200 | [diff] [blame] | 3326 | * ssl_prepare_server_key_exchange already wrote the signature | 
|  | 3327 | * itself at its proper place in the output buffer. */ | 
| Gilles Peskine | 7ab013a | 2018-01-08 17:04:16 +0100 | [diff] [blame] | 3328 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) | 
|  | 3329 | if( signature_len != 0 ) | 
|  | 3330 | { | 
|  | 3331 | ssl->out_msg[ssl->out_msglen++] = (unsigned char)( signature_len >> 8 ); | 
|  | 3332 | ssl->out_msg[ssl->out_msglen++] = (unsigned char)( signature_len      ); | 
|  | 3333 |  | 
|  | 3334 | MBEDTLS_SSL_DEBUG_BUF( 3, "my signature", | 
|  | 3335 | ssl->out_msg + ssl->out_msglen, | 
|  | 3336 | signature_len ); | 
|  | 3337 |  | 
|  | 3338 | /* Skip over the already-written signature */ | 
|  | 3339 | ssl->out_msglen += signature_len; | 
|  | 3340 | } | 
|  | 3341 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED */ | 
|  | 3342 |  | 
| Gilles Peskine | 184a3fa | 2018-01-06 01:46:17 +0100 | [diff] [blame] | 3343 | /* Add header and send. */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3344 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; | 
|  | 3345 | ssl->out_msg[0]  = MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3346 |  | 
|  | 3347 | ssl->state++; | 
|  | 3348 |  | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3349 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3350 | { | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3351 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3352 | return( ret ); | 
|  | 3353 | } | 
|  | 3354 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3355 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3356 | return( 0 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3357 | } | 
|  | 3358 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3359 | static int ssl_write_server_hello_done( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3360 | { | 
|  | 3361 | int ret; | 
|  | 3362 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3363 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3364 |  | 
|  | 3365 | ssl->out_msglen  = 4; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3366 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; | 
|  | 3367 | ssl->out_msg[0]  = MBEDTLS_SSL_HS_SERVER_HELLO_DONE; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3368 |  | 
|  | 3369 | ssl->state++; | 
|  | 3370 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3371 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3372 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3373 | mbedtls_ssl_send_flight_completed( ssl ); | 
| Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3374 | #endif | 
|  | 3375 |  | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3376 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3377 | { | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3378 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3379 | return( ret ); | 
|  | 3380 | } | 
|  | 3381 |  | 
| Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3382 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 3383 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | 
|  | 3384 | ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) | 
|  | 3385 | { | 
|  | 3386 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); | 
|  | 3387 | return( ret ); | 
|  | 3388 | } | 
| Hanno Becker | bc2498a | 2018-08-28 10:13:29 +0100 | [diff] [blame] | 3389 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3390 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3391 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3392 |  | 
|  | 3393 | return( 0 ); | 
|  | 3394 | } | 
|  | 3395 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3396 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) ||                       \ | 
|  | 3397 | defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) | 
|  | 3398 | 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] | 3399 | const unsigned char *end ) | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3400 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3401 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3402 | size_t n; | 
|  | 3403 |  | 
|  | 3404 | /* | 
|  | 3405 | * Receive G^Y mod P, premaster = (G^Y)^X mod P | 
|  | 3406 | */ | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3407 | if( *p + 2 > end ) | 
|  | 3408 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3409 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); | 
|  | 3410 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3411 | } | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3412 |  | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3413 | n = ( (*p)[0] << 8 ) | (*p)[1]; | 
|  | 3414 | *p += 2; | 
|  | 3415 |  | 
| Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 3416 | if( *p + n > end ) | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3417 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3418 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); | 
|  | 3419 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3420 | } | 
|  | 3421 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3422 | 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] | 3423 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3424 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_read_public", ret ); | 
|  | 3425 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP ); | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3426 | } | 
|  | 3427 |  | 
| Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 3428 | *p += n; | 
|  | 3429 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3430 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY ); | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3431 |  | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3432 | return( ret ); | 
|  | 3433 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3434 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED || | 
|  | 3435 | MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3436 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3437 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) ||                           \ | 
|  | 3438 | defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) | 
| Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3439 |  | 
| Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3440 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) | 
| Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3441 | static int ssl_resume_decrypt_pms( mbedtls_ssl_context *ssl, | 
|  | 3442 | unsigned char *peer_pms, | 
|  | 3443 | size_t *peer_pmslen, | 
|  | 3444 | size_t peer_pmssize ) | 
|  | 3445 | { | 
| Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 3446 | int ret = ssl->conf->f_async_resume( ssl, | 
| Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3447 | peer_pms, peer_pmslen, peer_pmssize ); | 
|  | 3448 | if( ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS ) | 
|  | 3449 | { | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3450 | ssl->handshake->async_in_progress = 0; | 
| Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 3451 | mbedtls_ssl_set_async_operation_data( ssl, NULL ); | 
| Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3452 | } | 
|  | 3453 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl_decrypt_encrypted_pms", ret ); | 
|  | 3454 | return( ret ); | 
|  | 3455 | } | 
| Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3456 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ | 
| Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3457 |  | 
| Gilles Peskine | bcd98a5 | 2018-01-11 21:30:40 +0100 | [diff] [blame] | 3458 | static int ssl_decrypt_encrypted_pms( mbedtls_ssl_context *ssl, | 
|  | 3459 | const unsigned char *p, | 
|  | 3460 | const unsigned char *end, | 
|  | 3461 | unsigned char *peer_pms, | 
|  | 3462 | size_t *peer_pmslen, | 
|  | 3463 | size_t peer_pmssize ) | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3464 | { | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 3465 | int ret; | 
| Gilles Peskine | 422ccab | 2018-01-11 18:29:01 +0100 | [diff] [blame] | 3466 | mbedtls_pk_context *private_key = mbedtls_ssl_own_key( ssl ); | 
|  | 3467 | mbedtls_pk_context *public_key = &mbedtls_ssl_own_cert( ssl )->pk; | 
|  | 3468 | size_t len = mbedtls_pk_get_len( public_key ); | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3469 |  | 
| Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3470 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) | 
| Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3471 | /* 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] | 3472 | * decryption operation, resume signing. */ | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3473 | if( ssl->handshake->async_in_progress != 0 ) | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3474 | { | 
| Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3475 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "resuming decryption operation" ) ); | 
|  | 3476 | return( ssl_resume_decrypt_pms( ssl, | 
|  | 3477 | peer_pms, peer_pmslen, peer_pmssize ) ); | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3478 | } | 
| Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3479 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3480 |  | 
|  | 3481 | /* | 
| Gilles Peskine | 422ccab | 2018-01-11 18:29:01 +0100 | [diff] [blame] | 3482 | * Prepare to decrypt the premaster using own private RSA key | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3483 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3484 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ | 
|  | 3485 | defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 3486 | if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 ) | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3487 | { | 
| Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 3488 | if ( p + 2 > end ) { | 
|  | 3489 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); | 
|  | 3490 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); | 
|  | 3491 | } | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 3492 | if( *p++ != ( ( len >> 8 ) & 0xFF ) || | 
|  | 3493 | *p++ != ( ( len      ) & 0xFF ) ) | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3494 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3495 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); | 
|  | 3496 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3497 | } | 
|  | 3498 | } | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 3499 | #endif | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3500 |  | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 3501 | if( p + len != end ) | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3502 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3503 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); | 
|  | 3504 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3505 | } | 
|  | 3506 |  | 
| Gilles Peskine | 422ccab | 2018-01-11 18:29:01 +0100 | [diff] [blame] | 3507 | /* | 
|  | 3508 | * Decrypt the premaster secret | 
|  | 3509 | */ | 
| Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3510 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) | 
| Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3511 | if( ssl->conf->f_async_decrypt_start != NULL ) | 
|  | 3512 | { | 
| Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 3513 | ret = ssl->conf->f_async_decrypt_start( ssl, | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3514 | mbedtls_ssl_own_cert( ssl ), | 
|  | 3515 | p, len ); | 
| Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3516 | switch( ret ) | 
|  | 3517 | { | 
|  | 3518 | case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH: | 
|  | 3519 | /* act as if f_async_decrypt_start was null */ | 
|  | 3520 | break; | 
|  | 3521 | case 0: | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3522 | ssl->handshake->async_in_progress = 1; | 
| Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3523 | return( ssl_resume_decrypt_pms( ssl, | 
|  | 3524 | peer_pms, | 
|  | 3525 | peer_pmslen, | 
|  | 3526 | peer_pmssize ) ); | 
|  | 3527 | case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS: | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3528 | ssl->handshake->async_in_progress = 1; | 
| Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3529 | return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS ); | 
|  | 3530 | default: | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3531 | MBEDTLS_SSL_DEBUG_RET( 1, "f_async_decrypt_start", ret ); | 
| Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3532 | return( ret ); | 
|  | 3533 | } | 
|  | 3534 | } | 
| Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3535 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ | 
| Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3536 |  | 
| Gilles Peskine | 422ccab | 2018-01-11 18:29:01 +0100 | [diff] [blame] | 3537 | if( ! mbedtls_pk_can_do( private_key, MBEDTLS_PK_RSA ) ) | 
|  | 3538 | { | 
| Gilles Peskine | 422ccab | 2018-01-11 18:29:01 +0100 | [diff] [blame] | 3539 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) ); | 
|  | 3540 | return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ); | 
|  | 3541 | } | 
|  | 3542 |  | 
|  | 3543 | ret = mbedtls_pk_decrypt( private_key, p, len, | 
| Gilles Peskine | bcd98a5 | 2018-01-11 21:30:40 +0100 | [diff] [blame] | 3544 | peer_pms, peer_pmslen, peer_pmssize, | 
|  | 3545 | ssl->conf->f_rng, ssl->conf->p_rng ); | 
|  | 3546 | return( ret ); | 
|  | 3547 | } | 
|  | 3548 |  | 
|  | 3549 | static int ssl_parse_encrypted_pms( mbedtls_ssl_context *ssl, | 
|  | 3550 | const unsigned char *p, | 
|  | 3551 | const unsigned char *end, | 
|  | 3552 | size_t pms_offset ) | 
|  | 3553 | { | 
|  | 3554 | int ret; | 
|  | 3555 | unsigned char *pms = ssl->handshake->premaster + pms_offset; | 
|  | 3556 | unsigned char ver[2]; | 
|  | 3557 | unsigned char fake_pms[48], peer_pms[48]; | 
|  | 3558 | unsigned char mask; | 
|  | 3559 | size_t i, peer_pmslen; | 
|  | 3560 | unsigned int diff; | 
|  | 3561 |  | 
| Gilles Peskine | 0a8352b | 2018-06-13 18:16:41 +0200 | [diff] [blame] | 3562 | /* In case of a failure in decryption, the decryption may write less than | 
|  | 3563 | * 2 bytes of output, but we always read the first two bytes. It doesn't | 
|  | 3564 | * matter in the end because diff will be nonzero in that case due to | 
|  | 3565 | * peer_pmslen being less than 48, and we only care whether diff is 0. | 
|  | 3566 | * But do initialize peer_pms for robustness anyway. This also makes | 
|  | 3567 | * memory analyzers happy (don't access uninitialized memory, even | 
|  | 3568 | * if it's an unsigned char). */ | 
|  | 3569 | peer_pms[0] = peer_pms[1] = ~0; | 
|  | 3570 |  | 
| Gilles Peskine | bcd98a5 | 2018-01-11 21:30:40 +0100 | [diff] [blame] | 3571 | ret = ssl_decrypt_encrypted_pms( ssl, p, end, | 
|  | 3572 | peer_pms, | 
|  | 3573 | &peer_pmslen, | 
|  | 3574 | sizeof( peer_pms ) ); | 
|  | 3575 |  | 
| Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3576 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) | 
| Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3577 | if ( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS ) | 
|  | 3578 | return( ret ); | 
| Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3579 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ | 
| Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3580 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3581 | mbedtls_ssl_write_version( ssl->handshake->max_major_ver, | 
| Gilles Peskine | 2e33337 | 2018-04-24 13:22:10 +0200 | [diff] [blame] | 3582 | ssl->handshake->max_minor_ver, | 
|  | 3583 | ssl->conf->transport, ver ); | 
|  | 3584 |  | 
|  | 3585 | /* Avoid data-dependent branches while checking for invalid | 
|  | 3586 | * padding, to protect against timing-based Bleichenbacher-type | 
|  | 3587 | * attacks. */ | 
|  | 3588 | diff  = (unsigned int) ret; | 
|  | 3589 | diff |= peer_pmslen ^ 48; | 
|  | 3590 | diff |= peer_pms[0] ^ ver[0]; | 
|  | 3591 | diff |= peer_pms[1] ^ ver[1]; | 
|  | 3592 |  | 
|  | 3593 | /* mask = diff ? 0xff : 0x00 using bit operations to avoid branches */ | 
|  | 3594 | /* MSVC has a warning about unary minus on unsigned, but this is | 
|  | 3595 | * well-defined and precisely what we want to do here */ | 
|  | 3596 | #if defined(_MSC_VER) | 
|  | 3597 | #pragma warning( push ) | 
|  | 3598 | #pragma warning( disable : 4146 ) | 
|  | 3599 | #endif | 
|  | 3600 | mask = - ( ( diff | - diff ) >> ( sizeof( unsigned int ) * 8 - 1 ) ); | 
|  | 3601 | #if defined(_MSC_VER) | 
|  | 3602 | #pragma warning( pop ) | 
|  | 3603 | #endif | 
| Manuel Pégourié-Gonnard | b9c93d0 | 2015-06-23 13:53:15 +0200 | [diff] [blame] | 3604 |  | 
| Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 3605 | /* | 
|  | 3606 | * Protection against Bleichenbacher's attack: invalid PKCS#1 v1.5 padding | 
|  | 3607 | * must not cause the connection to end immediately; instead, send a | 
|  | 3608 | * bad_record_mac later in the handshake. | 
| Gilles Peskine | bcd98a5 | 2018-01-11 21:30:40 +0100 | [diff] [blame] | 3609 | * To protect against timing-based variants of the attack, we must | 
|  | 3610 | * not have any branch that depends on whether the decryption was | 
|  | 3611 | * successful. In particular, always generate the fake premaster secret, | 
|  | 3612 | * regardless of whether it will ultimately influence the output or not. | 
| Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 3613 | */ | 
| Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 3614 | ret = ssl->conf->f_rng( ssl->conf->p_rng, fake_pms, sizeof( fake_pms ) ); | 
| Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 3615 | if( ret != 0 ) | 
| Gilles Peskine | bcd98a5 | 2018-01-11 21:30:40 +0100 | [diff] [blame] | 3616 | { | 
| Gilles Peskine | e141638 | 2018-04-26 10:23:21 +0200 | [diff] [blame] | 3617 | /* It's ok to abort on an RNG failure, since this does not reveal | 
|  | 3618 | * anything about the RSA decryption. */ | 
| Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 3619 | return( ret ); | 
| Gilles Peskine | bcd98a5 | 2018-01-11 21:30:40 +0100 | [diff] [blame] | 3620 | } | 
| Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 3621 |  | 
| Manuel Pégourié-Gonnard | 331ba57 | 2015-04-20 12:33:57 +0100 | [diff] [blame] | 3622 | #if defined(MBEDTLS_SSL_DEBUG_ALL) | 
| Manuel Pégourié-Gonnard | ce60fbe | 2015-04-15 16:45:52 +0200 | [diff] [blame] | 3623 | if( diff != 0 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3624 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); | 
| Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 3625 | #endif | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3626 |  | 
| Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 3627 | if( sizeof( ssl->handshake->premaster ) < pms_offset || | 
|  | 3628 | sizeof( ssl->handshake->premaster ) - pms_offset < 48 ) | 
|  | 3629 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3630 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 3631 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3632 | } | 
| Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 3633 | ssl->handshake->pmslen = 48; | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3634 |  | 
| Gilles Peskine | 422ccab | 2018-01-11 18:29:01 +0100 | [diff] [blame] | 3635 | /* Set pms to either the true or the fake PMS, without | 
|  | 3636 | * data-dependent branches. */ | 
| Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 3637 | for( i = 0; i < ssl->handshake->pmslen; i++ ) | 
|  | 3638 | pms[i] = ( mask & fake_pms[i] ) | ( (~mask) & peer_pms[i] ); | 
|  | 3639 |  | 
|  | 3640 | return( 0 ); | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3641 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3642 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED || | 
|  | 3643 | MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3644 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3645 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) | 
|  | 3646 | 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] | 3647 | const unsigned char *end ) | 
| Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 3648 | { | 
| Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 3649 | int ret = 0; | 
| Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 3650 | size_t n; | 
| Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 3651 |  | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3652 | if( ssl->conf->f_psk == NULL && | 
|  | 3653 | ( ssl->conf->psk == NULL || ssl->conf->psk_identity == NULL || | 
|  | 3654 | ssl->conf->psk_identity_len == 0 || ssl->conf->psk_len == 0 ) ) | 
| Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 3655 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3656 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) ); | 
|  | 3657 | return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ); | 
| Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 3658 | } | 
|  | 3659 |  | 
|  | 3660 | /* | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3661 | * Receive client pre-shared key identity name | 
| Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 3662 | */ | 
| Hanno Becker | 83c9f49 | 2017-06-26 13:52:14 +0100 | [diff] [blame] | 3663 | if( end - *p < 2 ) | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3664 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3665 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); | 
|  | 3666 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3667 | } | 
| Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 3668 |  | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3669 | n = ( (*p)[0] << 8 ) | (*p)[1]; | 
|  | 3670 | *p += 2; | 
|  | 3671 |  | 
| Hanno Becker | 83c9f49 | 2017-06-26 13:52:14 +0100 | [diff] [blame] | 3672 | if( n < 1 || n > 65535 || n > (size_t) ( end - *p ) ) | 
| Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 3673 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3674 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); | 
|  | 3675 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); | 
| Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 3676 | } | 
|  | 3677 |  | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3678 | if( ssl->conf->f_psk != NULL ) | 
| Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 3679 | { | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3680 | 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] | 3681 | ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY; | 
| Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 3682 | } | 
| Manuel Pégourié-Gonnard | d27680b | 2014-07-08 14:15:55 +0200 | [diff] [blame] | 3683 | else | 
| Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 3684 | { | 
| Manuel Pégourié-Gonnard | 31ff1d2 | 2013-10-28 13:46:11 +0100 | [diff] [blame] | 3685 | /* Identity is not a big secret since clients send it in the clear, | 
|  | 3686 | * but treat it carefully anyway, just in case */ | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3687 | if( n != ssl->conf->psk_identity_len || | 
|  | 3688 | mbedtls_ssl_safer_memcmp( ssl->conf->psk_identity, *p, n ) != 0 ) | 
| Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 3689 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3690 | ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY; | 
| Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 3691 | } | 
|  | 3692 | } | 
|  | 3693 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3694 | if( ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY ) | 
| Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 3695 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3696 | MBEDTLS_SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n ); | 
| Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 3697 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 3698 | MBEDTLS_SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3699 | return( MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY ); | 
| Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 3700 | } | 
|  | 3701 |  | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3702 | *p += n; | 
| Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 3703 |  | 
| Manuel Pégourié-Gonnard | d27680b | 2014-07-08 14:15:55 +0200 | [diff] [blame] | 3704 | return( 0 ); | 
| Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 3705 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3706 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ | 
| Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 3707 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3708 | static int ssl_parse_client_key_exchange( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3709 | { | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 3710 | int ret; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3711 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; | 
| Manuel Pégourié-Gonnard | 2114d72 | 2014-09-10 13:59:41 +0000 | [diff] [blame] | 3712 | unsigned char *p, *end; | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3713 |  | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3714 | ciphersuite_info = ssl->transform_negotiate->ciphersuite_info; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3715 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3716 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3717 |  | 
| Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3718 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) && \ | 
| Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3719 | ( defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \ | 
|  | 3720 | defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) ) | 
|  | 3721 | if( ( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK || | 
|  | 3722 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA ) && | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3723 | ( ssl->handshake->async_in_progress != 0 ) ) | 
| Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3724 | { | 
|  | 3725 | /* We've already read a record and there is an asynchronous | 
|  | 3726 | * operation in progress to decrypt it. So skip reading the | 
| Gilles Peskine | 168dae8 | 2018-04-25 23:35:42 +0200 | [diff] [blame] | 3727 | * record. */ | 
| Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3728 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "will resume decryption of previously-read record" ) ); | 
|  | 3729 | } | 
|  | 3730 | else | 
|  | 3731 | #endif | 
| Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 3732 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3733 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3734 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3735 | return( ret ); | 
|  | 3736 | } | 
|  | 3737 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3738 | p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ); | 
| Manuel Pégourié-Gonnard | 2114d72 | 2014-09-10 13:59:41 +0000 | [diff] [blame] | 3739 | end = ssl->in_msg + ssl->in_hslen; | 
| Manuel Pégourié-Gonnard | f899583 | 2014-09-10 08:25:12 +0000 | [diff] [blame] | 3740 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3741 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3742 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3743 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); | 
|  | 3744 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3745 | } | 
|  | 3746 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3747 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3748 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3749 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); | 
|  | 3750 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3751 | } | 
|  | 3752 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3753 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) | 
|  | 3754 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3755 | { | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3756 | if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3757 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3758 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret ); | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3759 | return( ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3760 | } | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3761 |  | 
| Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 3762 | if( p != end ) | 
|  | 3763 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3764 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) ); | 
|  | 3765 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); | 
| Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 3766 | } | 
|  | 3767 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3768 | if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx, | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3769 | ssl->handshake->premaster, | 
| Manuel Pégourié-Gonnard | 3335205 | 2015-06-02 16:17:08 +0100 | [diff] [blame] | 3770 | MBEDTLS_PREMASTER_SIZE, | 
| Manuel Pégourié-Gonnard | 2d62764 | 2013-09-04 14:22:07 +0200 | [diff] [blame] | 3771 | &ssl->handshake->pmslen, | 
| Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 3772 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3773 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3774 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); | 
|  | 3775 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3776 | } | 
|  | 3777 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3778 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K  ); | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3779 | } | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3780 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3781 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */ | 
|  | 3782 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) ||                     \ | 
|  | 3783 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) ||                   \ | 
|  | 3784 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||                      \ | 
|  | 3785 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) | 
|  | 3786 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA || | 
|  | 3787 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA || | 
|  | 3788 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA || | 
|  | 3789 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA ) | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3790 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3791 | if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx, | 
| Manuel Pégourié-Gonnard | 2114d72 | 2014-09-10 13:59:41 +0000 | [diff] [blame] | 3792 | p, end - p) ) != 0 ) | 
| Manuel Pégourié-Gonnard | b59d699 | 2013-10-14 12:00:45 +0200 | [diff] [blame] | 3793 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3794 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret ); | 
|  | 3795 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP ); | 
| Manuel Pégourié-Gonnard | b59d699 | 2013-10-14 12:00:45 +0200 | [diff] [blame] | 3796 | } | 
|  | 3797 |  | 
| Janos Follath | 3fbdada | 2018-08-15 10:26:53 +0100 | [diff] [blame] | 3798 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, | 
|  | 3799 | MBEDTLS_DEBUG_ECDH_QP ); | 
| Manuel Pégourié-Gonnard | b59d699 | 2013-10-14 12:00:45 +0200 | [diff] [blame] | 3800 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3801 | if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3802 | &ssl->handshake->pmslen, | 
|  | 3803 | ssl->handshake->premaster, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3804 | MBEDTLS_MPI_MAX_SIZE, | 
| Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 3805 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3806 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3807 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret ); | 
|  | 3808 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3809 | } | 
|  | 3810 |  | 
| Janos Follath | 3fbdada | 2018-08-15 10:26:53 +0100 | [diff] [blame] | 3811 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, | 
|  | 3812 | MBEDTLS_DEBUG_ECDH_Z ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3813 | } | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3814 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3815 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || | 
|  | 3816 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED || | 
|  | 3817 | MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED || | 
|  | 3818 | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */ | 
|  | 3819 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) | 
|  | 3820 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ) | 
| Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 3821 | { | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3822 | if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 ) | 
| Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 3823 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3824 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret ); | 
| Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 3825 | return( ret ); | 
|  | 3826 | } | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3827 |  | 
| Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 3828 | if( p != end ) | 
|  | 3829 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3830 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) ); | 
|  | 3831 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); | 
| Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 3832 | } | 
|  | 3833 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3834 | if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl, | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 3835 | ciphersuite_info->key_exchange ) ) != 0 ) | 
|  | 3836 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3837 | 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] | 3838 | return( ret ); | 
|  | 3839 | } | 
| Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 3840 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3841 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3842 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ | 
|  | 3843 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) | 
|  | 3844 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 3845 | { | 
| Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3846 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3847 | if ( ssl->handshake->async_in_progress != 0 ) | 
| Gilles Peskine | 2c6078e | 2018-01-12 13:46:43 +0100 | [diff] [blame] | 3848 | { | 
|  | 3849 | /* There is an asynchronous operation in progress to | 
|  | 3850 | * decrypt the encrypted premaster secret, so skip | 
|  | 3851 | * directly to resuming this operation. */ | 
|  | 3852 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "PSK identity already parsed" ) ); | 
|  | 3853 | /* Update p to skip the PSK identity. ssl_parse_encrypted_pms | 
|  | 3854 | * won't actually use it, but maintain p anyway for robustness. */ | 
|  | 3855 | p += ssl->conf->psk_identity_len + 2; | 
|  | 3856 | } | 
|  | 3857 | else | 
| Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3858 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 3859 | if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 ) | 
|  | 3860 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3861 | 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] | 3862 | return( ret ); | 
|  | 3863 | } | 
|  | 3864 |  | 
|  | 3865 | if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 ) | 
|  | 3866 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3867 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret ); | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 3868 | return( ret ); | 
|  | 3869 | } | 
|  | 3870 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3871 | if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl, | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 3872 | ciphersuite_info->key_exchange ) ) != 0 ) | 
|  | 3873 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3874 | 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] | 3875 | return( ret ); | 
|  | 3876 | } | 
|  | 3877 | } | 
|  | 3878 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3879 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ | 
|  | 3880 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) | 
|  | 3881 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3882 | { | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3883 | if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 ) | 
|  | 3884 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3885 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3886 | return( ret ); | 
|  | 3887 | } | 
|  | 3888 | if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 ) | 
|  | 3889 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3890 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3891 | return( ret ); | 
|  | 3892 | } | 
|  | 3893 |  | 
| Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 3894 | if( p != end ) | 
|  | 3895 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3896 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) ); | 
|  | 3897 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); | 
| Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 3898 | } | 
|  | 3899 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3900 | if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl, | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 3901 | ciphersuite_info->key_exchange ) ) != 0 ) | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3902 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3903 | 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] | 3904 | return( ret ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3905 | } | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3906 | } | 
|  | 3907 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3908 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ | 
|  | 3909 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) | 
|  | 3910 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) | 
| Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 3911 | { | 
| Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 3912 | if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 ) | 
|  | 3913 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3914 | 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] | 3915 | return( ret ); | 
|  | 3916 | } | 
| Manuel Pégourié-Gonnard | b59d699 | 2013-10-14 12:00:45 +0200 | [diff] [blame] | 3917 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3918 | if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx, | 
| Manuel Pégourié-Gonnard | b59d699 | 2013-10-14 12:00:45 +0200 | [diff] [blame] | 3919 | p, end - p ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 3920 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3921 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret ); | 
|  | 3922 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP ); | 
| Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 3923 | } | 
|  | 3924 |  | 
| Janos Follath | 3fbdada | 2018-08-15 10:26:53 +0100 | [diff] [blame] | 3925 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, | 
|  | 3926 | MBEDTLS_DEBUG_ECDH_QP ); | 
| Manuel Pégourié-Gonnard | b59d699 | 2013-10-14 12:00:45 +0200 | [diff] [blame] | 3927 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3928 | if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl, | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 3929 | ciphersuite_info->key_exchange ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 3930 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3931 | 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] | 3932 | return( ret ); | 
|  | 3933 | } | 
| Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 3934 | } | 
|  | 3935 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3936 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ | 
|  | 3937 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) | 
|  | 3938 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA ) | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3939 | { | 
| Manuel Pégourié-Gonnard | 2114d72 | 2014-09-10 13:59:41 +0000 | [diff] [blame] | 3940 | if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 0 ) ) != 0 ) | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3941 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3942 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret ); | 
| Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3943 | return( ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3944 | } | 
|  | 3945 | } | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3946 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3947 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */ | 
| Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 3948 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
|  | 3949 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ) | 
|  | 3950 | { | 
|  | 3951 | ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx, | 
|  | 3952 | p, end - p ); | 
|  | 3953 | if( ret != 0 ) | 
|  | 3954 | { | 
|  | 3955 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret ); | 
|  | 3956 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); | 
|  | 3957 | } | 
|  | 3958 |  | 
|  | 3959 | ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx, | 
|  | 3960 | ssl->handshake->premaster, 32, &ssl->handshake->pmslen, | 
|  | 3961 | ssl->conf->f_rng, ssl->conf->p_rng ); | 
|  | 3962 | if( ret != 0 ) | 
|  | 3963 | { | 
|  | 3964 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret ); | 
|  | 3965 | return( ret ); | 
|  | 3966 | } | 
|  | 3967 | } | 
|  | 3968 | else | 
|  | 3969 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3970 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3971 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 3972 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3973 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3974 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3975 | if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 ) | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 3976 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3977 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret ); | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 3978 | return( ret ); | 
|  | 3979 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3980 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3981 | ssl->state++; | 
|  | 3982 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3983 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3984 |  | 
|  | 3985 | return( 0 ); | 
|  | 3986 | } | 
|  | 3987 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3988 | #if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)       && \ | 
|  | 3989 | !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)   && \ | 
| Paul Bakker | 29f221f | 2016-07-22 13:49:02 +0100 | [diff] [blame] | 3990 | !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED)  && \ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3991 | !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \ | 
| Paul Bakker | 29f221f | 2016-07-22 13:49:02 +0100 | [diff] [blame] | 3992 | !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)&& \ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3993 | !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) | 
|  | 3994 | static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3995 | { | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3996 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | 
|  | 3997 | ssl->transform_negotiate->ciphersuite_info; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3998 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3999 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4000 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4001 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || | 
|  | 4002 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK || | 
|  | 4003 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK || | 
| Manuel Pégourié-Gonnard | 25dbeb0 | 2015-09-16 17:30:03 +0200 | [diff] [blame] | 4004 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK || | 
|  | 4005 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ) | 
| Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 4006 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4007 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) ); | 
| Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 4008 | ssl->state++; | 
|  | 4009 | return( 0 ); | 
|  | 4010 | } | 
|  | 4011 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4012 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 4013 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4014 | } | 
|  | 4015 | #else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4016 | static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4017 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4018 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; | 
| Manuel Pégourié-Gonnard | 4528f3f | 2014-09-10 14:17:23 +0000 | [diff] [blame] | 4019 | size_t i, sig_len; | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4020 | unsigned char hash[48]; | 
| Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 4021 | unsigned char *hash_start = hash; | 
| Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4022 | size_t hashlen; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4023 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 4024 | mbedtls_pk_type_t pk_alg; | 
| Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 4025 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4026 | mbedtls_md_type_t md_alg; | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 4027 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | 
|  | 4028 | ssl->transform_negotiate->ciphersuite_info; | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4029 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4030 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4031 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4032 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || | 
|  | 4033 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK || | 
|  | 4034 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK || | 
|  | 4035 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK || | 
| Manuel Pégourié-Gonnard | 25dbeb0 | 2015-09-16 17:30:03 +0200 | [diff] [blame] | 4036 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE || | 
| Manuel Pégourié-Gonnard | 7222621 | 2014-09-10 14:23:38 +0000 | [diff] [blame] | 4037 | ssl->session_negotiate->peer_cert == NULL ) | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4038 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4039 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4040 | ssl->state++; | 
|  | 4041 | return( 0 ); | 
|  | 4042 | } | 
|  | 4043 |  | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4044 | /* Read the message without adding it to the checksum */ | 
| Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 4045 | ret = mbedtls_ssl_read_record( ssl, 0 /* no checksum update */ ); | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4046 | if( 0 != ret ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4047 | { | 
| Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 4048 | MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record" ), ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4049 | return( ret ); | 
|  | 4050 | } | 
|  | 4051 |  | 
|  | 4052 | ssl->state++; | 
|  | 4053 |  | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4054 | /* Process the message contents */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4055 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE || | 
|  | 4056 | ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE_VERIFY ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4057 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4058 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) ); | 
|  | 4059 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4060 | } | 
|  | 4061 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4062 | i = mbedtls_ssl_hs_hdr_len( ssl ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4063 |  | 
| Manuel Pégourié-Gonnard | 4528f3f | 2014-09-10 14:17:23 +0000 | [diff] [blame] | 4064 | /* | 
|  | 4065 | *  struct { | 
|  | 4066 | *     SignatureAndHashAlgorithm algorithm; -- TLS 1.2 only | 
|  | 4067 | *     opaque signature<0..2^16-1>; | 
|  | 4068 | *  } DigitallySigned; | 
|  | 4069 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4070 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ | 
|  | 4071 | defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
|  | 4072 | if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 ) | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 4073 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4074 | md_alg = MBEDTLS_MD_NONE; | 
| Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4075 | hashlen = 36; | 
| Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 4076 |  | 
|  | 4077 | /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4078 | if( mbedtls_pk_can_do( &ssl->session_negotiate->peer_cert->pk, | 
|  | 4079 | MBEDTLS_PK_ECDSA ) ) | 
| Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 4080 | { | 
|  | 4081 | hash_start += 16; | 
|  | 4082 | hashlen -= 16; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4083 | md_alg = MBEDTLS_MD_SHA1; | 
| Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 4084 | } | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 4085 | } | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 4086 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4087 | #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || | 
|  | 4088 | MBEDTLS_SSL_PROTO_TLS1_1 */ | 
|  | 4089 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 4090 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) | 
| Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 4091 | { | 
| Manuel Pégourié-Gonnard | 5ee9654 | 2014-09-10 14:27:21 +0000 | [diff] [blame] | 4092 | if( i + 2 > ssl->in_hslen ) | 
|  | 4093 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4094 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) ); | 
|  | 4095 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY ); | 
| Manuel Pégourié-Gonnard | 5ee9654 | 2014-09-10 14:27:21 +0000 | [diff] [blame] | 4096 | } | 
|  | 4097 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4098 | /* | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4099 | * Hash | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4100 | */ | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4101 | md_alg = mbedtls_ssl_md_alg_from_hash( ssl->in_msg[i] ); | 
|  | 4102 |  | 
|  | 4103 | 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] | 4104 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4105 | 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] | 4106 | " for verify message" ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4107 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY ); | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 4108 | } | 
|  | 4109 |  | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4110 | #if !defined(MBEDTLS_MD_SHA1) | 
|  | 4111 | if( MBEDTLS_MD_SHA1 == md_alg ) | 
|  | 4112 | hash_start += 16; | 
|  | 4113 | #endif | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 4114 |  | 
| Manuel Pégourié-Gonnard | bfe32ef | 2013-08-22 14:55:30 +0200 | [diff] [blame] | 4115 | /* Info from md_alg will be used instead */ | 
|  | 4116 | hashlen = 0; | 
| Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4117 |  | 
| Manuel Pégourié-Gonnard | 4528f3f | 2014-09-10 14:17:23 +0000 | [diff] [blame] | 4118 | i++; | 
|  | 4119 |  | 
| Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4120 | /* | 
|  | 4121 | * Signature | 
|  | 4122 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4123 | if( ( pk_alg = mbedtls_ssl_pk_alg_from_sig( ssl->in_msg[i] ) ) | 
|  | 4124 | == MBEDTLS_PK_NONE ) | 
| Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4125 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4126 | 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] | 4127 | " for verify message" ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4128 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY ); | 
| Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4129 | } | 
|  | 4130 |  | 
| Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4131 | /* | 
|  | 4132 | * Check the certificate's key type matches the signature alg | 
|  | 4133 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4134 | 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] | 4135 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4136 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) ); | 
|  | 4137 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY ); | 
| Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 4138 | } | 
| Manuel Pégourié-Gonnard | 4528f3f | 2014-09-10 14:17:23 +0000 | [diff] [blame] | 4139 |  | 
|  | 4140 | i++; | 
| Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 4141 | } | 
|  | 4142 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4143 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 4144 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4145 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 4146 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 4147 | } | 
| Manuel Pégourié-Gonnard | ff56da3 | 2013-07-11 10:46:21 +0200 | [diff] [blame] | 4148 |  | 
| Manuel Pégourié-Gonnard | 5ee9654 | 2014-09-10 14:27:21 +0000 | [diff] [blame] | 4149 | if( i + 2 > ssl->in_hslen ) | 
|  | 4150 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4151 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) ); | 
|  | 4152 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY ); | 
| Manuel Pégourié-Gonnard | 5ee9654 | 2014-09-10 14:27:21 +0000 | [diff] [blame] | 4153 | } | 
|  | 4154 |  | 
| Manuel Pégourié-Gonnard | 4528f3f | 2014-09-10 14:17:23 +0000 | [diff] [blame] | 4155 | sig_len = ( ssl->in_msg[i] << 8 ) | ssl->in_msg[i+1]; | 
|  | 4156 | i += 2; | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 4157 |  | 
| Manuel Pégourié-Gonnard | 4528f3f | 2014-09-10 14:17:23 +0000 | [diff] [blame] | 4158 | if( i + sig_len != ssl->in_hslen ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4159 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4160 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) ); | 
|  | 4161 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4162 | } | 
|  | 4163 |  | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4164 | /* Calculate hash and verify signature */ | 
|  | 4165 | ssl->handshake->calc_verify( ssl, hash ); | 
|  | 4166 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4167 | 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] | 4168 | md_alg, hash_start, hashlen, | 
| Manuel Pégourié-Gonnard | 4528f3f | 2014-09-10 14:17:23 +0000 | [diff] [blame] | 4169 | ssl->in_msg + i, sig_len ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4170 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4171 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4172 | return( ret ); | 
|  | 4173 | } | 
|  | 4174 |  | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4175 | mbedtls_ssl_update_handshake_status( ssl ); | 
|  | 4176 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4177 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4178 |  | 
| Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 4179 | return( ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4180 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4181 | #endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED && | 
|  | 4182 | !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED && | 
| Paul Bakker | 29f221f | 2016-07-22 13:49:02 +0100 | [diff] [blame] | 4183 | !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED && | 
|  | 4184 | !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED && | 
|  | 4185 | !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED && | 
|  | 4186 | !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4187 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4188 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | 
|  | 4189 | 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] | 4190 | { | 
|  | 4191 | int ret; | 
| Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 4192 | size_t tlen; | 
| Manuel Pégourié-Gonnard | b0394be | 2015-05-19 11:40:30 +0200 | [diff] [blame] | 4193 | uint32_t lifetime; | 
| Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4194 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4195 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) ); | 
| Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4196 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4197 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; | 
|  | 4198 | ssl->out_msg[0]  = MBEDTLS_SSL_HS_NEW_SESSION_TICKET; | 
| Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4199 |  | 
|  | 4200 | /* | 
|  | 4201 | * struct { | 
|  | 4202 | *     uint32 ticket_lifetime_hint; | 
|  | 4203 | *     opaque ticket<0..2^16-1>; | 
|  | 4204 | * } NewSessionTicket; | 
|  | 4205 | * | 
|  | 4206 | * 4  .  7   ticket_lifetime_hint (0 = unspecified) | 
|  | 4207 | * 8  .  9   ticket_len (n) | 
|  | 4208 | * 10 .  9+n ticket content | 
|  | 4209 | */ | 
| Manuel Pégourié-Gonnard | 164d894 | 2013-09-23 22:01:39 +0200 | [diff] [blame] | 4210 |  | 
| Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 4211 | 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] | 4212 | ssl->session_negotiate, | 
|  | 4213 | ssl->out_msg + 10, | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4214 | ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN, | 
| Manuel Pégourié-Gonnard | b0394be | 2015-05-19 11:40:30 +0200 | [diff] [blame] | 4215 | &tlen, &lifetime ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 4216 | { | 
| Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 4217 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_ticket_write", ret ); | 
| Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 4218 | tlen = 0; | 
|  | 4219 | } | 
| Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4220 |  | 
| Manuel Pégourié-Gonnard | b0394be | 2015-05-19 11:40:30 +0200 | [diff] [blame] | 4221 | ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF; | 
|  | 4222 | ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF; | 
|  | 4223 | ssl->out_msg[6] = ( lifetime >>  8 ) & 0xFF; | 
|  | 4224 | ssl->out_msg[7] = ( lifetime       ) & 0xFF; | 
|  | 4225 |  | 
| Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 4226 | ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF ); | 
|  | 4227 | ssl->out_msg[9] = (unsigned char)( ( tlen      ) & 0xFF ); | 
| Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4228 |  | 
| Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 4229 | ssl->out_msglen = 10 + tlen; | 
| Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4230 |  | 
| Manuel Pégourié-Gonnard | 145dfcb | 2014-02-26 14:23:33 +0100 | [diff] [blame] | 4231 | /* | 
|  | 4232 | * Morally equivalent to updating ssl->state, but NewSessionTicket and | 
|  | 4233 | * ChangeCipherSpec share the same state. | 
|  | 4234 | */ | 
|  | 4235 | ssl->handshake->new_session_ticket = 0; | 
|  | 4236 |  | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4237 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4238 | { | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4239 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); | 
| Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4240 | return( ret ); | 
|  | 4241 | } | 
|  | 4242 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4243 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) ); | 
| Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4244 |  | 
|  | 4245 | return( 0 ); | 
|  | 4246 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4247 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ | 
| Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4248 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4249 | /* | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4250 | * SSL handshake -- server side -- single step | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4251 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4252 | int mbedtls_ssl_handshake_server_step( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4253 | { | 
|  | 4254 | int ret = 0; | 
|  | 4255 |  | 
| Manuel Pégourié-Gonnard | dba460f | 2015-06-24 22:59:30 +0200 | [diff] [blame] | 4256 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4257 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4258 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4259 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) ); | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4260 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4261 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4262 | return( ret ); | 
|  | 4263 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4264 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4265 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4266 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4267 | { | 
| Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4268 | if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4269 | return( ret ); | 
|  | 4270 | } | 
| Hanno Becker | bc2498a | 2018-08-28 10:13:29 +0100 | [diff] [blame] | 4271 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4272 |  | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4273 | switch( ssl->state ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4274 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4275 | case MBEDTLS_SSL_HELLO_REQUEST: | 
|  | 4276 | ssl->state = MBEDTLS_SSL_CLIENT_HELLO; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4277 | break; | 
|  | 4278 |  | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4279 | /* | 
|  | 4280 | *  <==   ClientHello | 
|  | 4281 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4282 | case MBEDTLS_SSL_CLIENT_HELLO: | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4283 | ret = ssl_parse_client_hello( ssl ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4284 | break; | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4285 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4286 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 4287 | case MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT: | 
|  | 4288 | return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED ); | 
| Manuel Pégourié-Gonnard | 579950c | 2014-09-29 17:47:33 +0200 | [diff] [blame] | 4289 | #endif | 
|  | 4290 |  | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4291 | /* | 
|  | 4292 | *  ==>   ServerHello | 
|  | 4293 | *        Certificate | 
|  | 4294 | *      ( ServerKeyExchange  ) | 
|  | 4295 | *      ( CertificateRequest ) | 
|  | 4296 | *        ServerHelloDone | 
|  | 4297 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4298 | case MBEDTLS_SSL_SERVER_HELLO: | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4299 | ret = ssl_write_server_hello( ssl ); | 
|  | 4300 | break; | 
|  | 4301 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4302 | case MBEDTLS_SSL_SERVER_CERTIFICATE: | 
|  | 4303 | ret = mbedtls_ssl_write_certificate( ssl ); | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4304 | break; | 
|  | 4305 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4306 | case MBEDTLS_SSL_SERVER_KEY_EXCHANGE: | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4307 | ret = ssl_write_server_key_exchange( ssl ); | 
|  | 4308 | break; | 
|  | 4309 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4310 | case MBEDTLS_SSL_CERTIFICATE_REQUEST: | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4311 | ret = ssl_write_certificate_request( ssl ); | 
|  | 4312 | break; | 
|  | 4313 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4314 | case MBEDTLS_SSL_SERVER_HELLO_DONE: | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4315 | ret = ssl_write_server_hello_done( ssl ); | 
|  | 4316 | break; | 
|  | 4317 |  | 
|  | 4318 | /* | 
|  | 4319 | *  <== ( Certificate/Alert  ) | 
|  | 4320 | *        ClientKeyExchange | 
|  | 4321 | *      ( CertificateVerify  ) | 
|  | 4322 | *        ChangeCipherSpec | 
|  | 4323 | *        Finished | 
|  | 4324 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4325 | case MBEDTLS_SSL_CLIENT_CERTIFICATE: | 
|  | 4326 | ret = mbedtls_ssl_parse_certificate( ssl ); | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4327 | break; | 
|  | 4328 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4329 | case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE: | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4330 | ret = ssl_parse_client_key_exchange( ssl ); | 
|  | 4331 | break; | 
|  | 4332 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4333 | case MBEDTLS_SSL_CERTIFICATE_VERIFY: | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4334 | ret = ssl_parse_certificate_verify( ssl ); | 
|  | 4335 | break; | 
|  | 4336 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4337 | case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC: | 
|  | 4338 | ret = mbedtls_ssl_parse_change_cipher_spec( ssl ); | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4339 | break; | 
|  | 4340 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4341 | case MBEDTLS_SSL_CLIENT_FINISHED: | 
|  | 4342 | ret = mbedtls_ssl_parse_finished( ssl ); | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4343 | break; | 
|  | 4344 |  | 
|  | 4345 | /* | 
| Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 4346 | *  ==> ( NewSessionTicket ) | 
|  | 4347 | *        ChangeCipherSpec | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4348 | *        Finished | 
|  | 4349 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4350 | case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC: | 
|  | 4351 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | 
| Manuel Pégourié-Gonnard | 7cd5924 | 2013-08-02 13:24:41 +0200 | [diff] [blame] | 4352 | if( ssl->handshake->new_session_ticket != 0 ) | 
|  | 4353 | ret = ssl_write_new_session_ticket( ssl ); | 
|  | 4354 | else | 
| Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 4355 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4356 | ret = mbedtls_ssl_write_change_cipher_spec( ssl ); | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4357 | break; | 
|  | 4358 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4359 | case MBEDTLS_SSL_SERVER_FINISHED: | 
|  | 4360 | ret = mbedtls_ssl_write_finished( ssl ); | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4361 | break; | 
|  | 4362 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4363 | case MBEDTLS_SSL_FLUSH_BUFFERS: | 
|  | 4364 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) ); | 
|  | 4365 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4366 | break; | 
|  | 4367 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4368 | case MBEDTLS_SSL_HANDSHAKE_WRAPUP: | 
|  | 4369 | mbedtls_ssl_handshake_wrapup( ssl ); | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4370 | break; | 
|  | 4371 |  | 
|  | 4372 | default: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4373 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) ); | 
|  | 4374 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4375 | } | 
|  | 4376 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4377 | return( ret ); | 
|  | 4378 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4379 | #endif /* MBEDTLS_SSL_SRV_C */ |