Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SSLv3/TLSv1 client-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_CLI_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 29 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 30 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 31 | #include "mbedtls/platform.h" |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 32 | #else |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 33 | #include <stdlib.h> |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 34 | #define mbedtls_calloc calloc |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 35 | #define mbedtls_free free |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 36 | #endif |
| 37 | |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 38 | #include "mbedtls/ssl.h" |
| 39 | #include "mbedtls/ssl_internal.h" |
Janos Follath | 73c616b | 2019-12-18 15:07:04 +0000 | [diff] [blame] | 40 | #include "mbedtls/debug.h" |
| 41 | #include "mbedtls/error.h" |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 42 | |
Hanno Becker | bb89e27 | 2019-01-08 12:54:37 +0000 | [diff] [blame] | 43 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 44 | #include "mbedtls/psa_util.h" |
| 45 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 46 | |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 47 | #include <string.h> |
| 48 | |
Manuel Pégourié-Gonnard | 9386664 | 2015-06-22 19:21:23 +0200 | [diff] [blame] | 49 | #include <stdint.h> |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 50 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 51 | #if defined(MBEDTLS_HAVE_TIME) |
Simon Butcher | b5b6af2 | 2016-07-13 14:46:18 +0100 | [diff] [blame] | 52 | #include "mbedtls/platform_time.h" |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 53 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 54 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 55 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 56 | #include "mbedtls/platform_util.h" |
Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 57 | #endif |
| 58 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 59 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Hanno Becker | 520224e | 2018-10-26 11:38:07 +0100 | [diff] [blame] | 60 | static int ssl_conf_has_static_psk( mbedtls_ssl_config const *conf ) |
Hanno Becker | 2e4f616 | 2018-10-23 11:54:44 +0100 | [diff] [blame] | 61 | { |
| 62 | if( conf->psk_identity == NULL || |
| 63 | conf->psk_identity_len == 0 ) |
| 64 | { |
| 65 | return( 0 ); |
| 66 | } |
| 67 | |
| 68 | if( conf->psk != NULL && conf->psk_len != 0 ) |
| 69 | return( 1 ); |
| 70 | |
| 71 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 72 | if( conf->psk_opaque != 0 ) |
| 73 | return( 1 ); |
| 74 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 75 | |
| 76 | return( 0 ); |
| 77 | } |
Hanno Becker | dfab8e2 | 2018-10-23 11:59:34 +0100 | [diff] [blame] | 78 | |
| 79 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Hanno Becker | 520224e | 2018-10-26 11:38:07 +0100 | [diff] [blame] | 80 | static int ssl_conf_has_static_raw_psk( mbedtls_ssl_config const *conf ) |
Hanno Becker | dfab8e2 | 2018-10-23 11:59:34 +0100 | [diff] [blame] | 81 | { |
| 82 | if( conf->psk_identity == NULL || |
| 83 | conf->psk_identity_len == 0 ) |
| 84 | { |
| 85 | return( 0 ); |
| 86 | } |
| 87 | |
| 88 | if( conf->psk != NULL && conf->psk_len != 0 ) |
| 89 | return( 1 ); |
| 90 | |
| 91 | return( 0 ); |
| 92 | } |
| 93 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 94 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 95 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
Hanno Becker | 2e4f616 | 2018-10-23 11:54:44 +0100 | [diff] [blame] | 96 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 97 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 98 | static void ssl_write_hostname_ext( mbedtls_ssl_context *ssl, |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 99 | unsigned char *buf, |
| 100 | size_t *olen ) |
| 101 | { |
| 102 | unsigned char *p = buf; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 103 | const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN; |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 104 | size_t hostname_len; |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 105 | |
| 106 | *olen = 0; |
| 107 | |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 108 | if( ssl->hostname == NULL ) |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 109 | return; |
| 110 | |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 111 | MBEDTLS_SSL_DEBUG_MSG( 3, |
| 112 | ( "client hello, adding server name extension: %s", |
| 113 | ssl->hostname ) ); |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 114 | |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 115 | hostname_len = strlen( ssl->hostname ); |
| 116 | |
Simon Butcher | 0fc94e9 | 2015-09-28 20:52:04 +0100 | [diff] [blame] | 117 | if( end < p || (size_t)( end - p ) < hostname_len + 9 ) |
Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 118 | { |
Simon Butcher | 0fc94e9 | 2015-09-28 20:52:04 +0100 | [diff] [blame] | 119 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) ); |
Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 120 | return; |
| 121 | } |
| 122 | |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 123 | /* |
Hanno Becker | 1a9a51c | 2017-04-07 13:02:16 +0100 | [diff] [blame] | 124 | * Sect. 3, RFC 6066 (TLS Extensions Definitions) |
| 125 | * |
| 126 | * In order to provide any of the server names, clients MAY include an |
| 127 | * extension of type "server_name" in the (extended) client hello. The |
| 128 | * "extension_data" field of this extension SHALL contain |
| 129 | * "ServerNameList" where: |
| 130 | * |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 131 | * struct { |
| 132 | * NameType name_type; |
| 133 | * select (name_type) { |
| 134 | * case host_name: HostName; |
| 135 | * } name; |
| 136 | * } ServerName; |
| 137 | * |
| 138 | * enum { |
| 139 | * host_name(0), (255) |
| 140 | * } NameType; |
| 141 | * |
| 142 | * opaque HostName<1..2^16-1>; |
| 143 | * |
| 144 | * struct { |
| 145 | * ServerName server_name_list<1..2^16-1> |
| 146 | * } ServerNameList; |
Hanno Becker | 1a9a51c | 2017-04-07 13:02:16 +0100 | [diff] [blame] | 147 | * |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 148 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 149 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SERVERNAME >> 8 ) & 0xFF ); |
| 150 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SERVERNAME ) & 0xFF ); |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 151 | |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 152 | *p++ = (unsigned char)( ( (hostname_len + 5) >> 8 ) & 0xFF ); |
| 153 | *p++ = (unsigned char)( ( (hostname_len + 5) ) & 0xFF ); |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 154 | |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 155 | *p++ = (unsigned char)( ( (hostname_len + 3) >> 8 ) & 0xFF ); |
| 156 | *p++ = (unsigned char)( ( (hostname_len + 3) ) & 0xFF ); |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 157 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 158 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME ) & 0xFF ); |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 159 | *p++ = (unsigned char)( ( hostname_len >> 8 ) & 0xFF ); |
| 160 | *p++ = (unsigned char)( ( hostname_len ) & 0xFF ); |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 161 | |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 162 | memcpy( p, ssl->hostname, hostname_len ); |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 163 | |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 164 | *olen = hostname_len + 9; |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 165 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 166 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 167 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 168 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 169 | static void ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl, |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 170 | unsigned char *buf, |
| 171 | size_t *olen ) |
| 172 | { |
| 173 | unsigned char *p = buf; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 174 | const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN; |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 175 | |
| 176 | *olen = 0; |
| 177 | |
Hanno Becker | 40f8b51 | 2017-10-12 14:58:55 +0100 | [diff] [blame] | 178 | /* We're always including an TLS_EMPTY_RENEGOTIATION_INFO_SCSV in the |
| 179 | * initial ClientHello, in which case also adding the renegotiation |
| 180 | * info extension is NOT RECOMMENDED as per RFC 5746 Section 3.4. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 181 | if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 182 | return; |
| 183 | |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 184 | MBEDTLS_SSL_DEBUG_MSG( 3, |
| 185 | ( "client hello, adding renegotiation extension" ) ); |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 186 | |
Simon Butcher | 0fc94e9 | 2015-09-28 20:52:04 +0100 | [diff] [blame] | 187 | if( end < p || (size_t)( end - p ) < 5 + ssl->verify_data_len ) |
Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 188 | { |
| 189 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) ); |
| 190 | return; |
| 191 | } |
| 192 | |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 193 | /* |
| 194 | * Secure renegotiation |
| 195 | */ |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 196 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO >> 8 ) |
| 197 | & 0xFF ); |
| 198 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO ) |
| 199 | & 0xFF ); |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 200 | |
| 201 | *p++ = 0x00; |
| 202 | *p++ = ( ssl->verify_data_len + 1 ) & 0xFF; |
| 203 | *p++ = ssl->verify_data_len & 0xFF; |
| 204 | |
| 205 | memcpy( p, ssl->own_verify_data, ssl->verify_data_len ); |
| 206 | |
| 207 | *olen = 5 + ssl->verify_data_len; |
| 208 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 209 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 210 | |
Manuel Pégourié-Gonnard | d942323 | 2014-12-02 11:57:29 +0100 | [diff] [blame] | 211 | /* |
| 212 | * Only if we handle at least one key exchange that needs signatures. |
| 213 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 214 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 215 | defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 216 | static void ssl_write_signature_algorithms_ext( mbedtls_ssl_context *ssl, |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 217 | unsigned char *buf, |
| 218 | size_t *olen ) |
| 219 | { |
| 220 | unsigned char *p = buf; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 221 | const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN; |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 222 | size_t sig_alg_len = 0; |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 223 | const int *md; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 224 | #if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) |
Manuel Pégourié-Gonnard | 5bfd968 | 2014-06-24 15:18:11 +0200 | [diff] [blame] | 225 | unsigned char *sig_alg_list = buf + 6; |
| 226 | #endif |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 227 | |
| 228 | *olen = 0; |
| 229 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 230 | if( ssl->conf->max_minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 ) |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 231 | return; |
| 232 | |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 233 | MBEDTLS_SSL_DEBUG_MSG( 3, |
| 234 | ( "client hello, adding signature_algorithms extension" ) ); |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 235 | |
Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 236 | for( md = ssl->conf->sig_hashes; *md != MBEDTLS_MD_NONE; md++ ) |
| 237 | { |
| 238 | #if defined(MBEDTLS_ECDSA_C) |
| 239 | sig_alg_len += 2; |
| 240 | #endif |
| 241 | #if defined(MBEDTLS_RSA_C) |
| 242 | sig_alg_len += 2; |
| 243 | #endif |
| 244 | } |
| 245 | |
Simon Butcher | 0fc94e9 | 2015-09-28 20:52:04 +0100 | [diff] [blame] | 246 | if( end < p || (size_t)( end - p ) < sig_alg_len + 6 ) |
Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 247 | { |
| 248 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) ); |
| 249 | return; |
| 250 | } |
| 251 | |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 252 | /* |
| 253 | * Prepare signature_algorithms extension (TLS 1.2) |
| 254 | */ |
Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 255 | sig_alg_len = 0; |
| 256 | |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 257 | for( md = ssl->conf->sig_hashes; *md != MBEDTLS_MD_NONE; md++ ) |
| 258 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 259 | #if defined(MBEDTLS_ECDSA_C) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 260 | sig_alg_list[sig_alg_len++] = mbedtls_ssl_hash_from_md_alg( *md ); |
| 261 | sig_alg_list[sig_alg_len++] = MBEDTLS_SSL_SIG_ECDSA; |
Manuel Pégourié-Gonnard | d11eb7c | 2013-08-22 15:57:15 +0200 | [diff] [blame] | 262 | #endif |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 263 | #if defined(MBEDTLS_RSA_C) |
| 264 | sig_alg_list[sig_alg_len++] = mbedtls_ssl_hash_from_md_alg( *md ); |
| 265 | sig_alg_list[sig_alg_len++] = MBEDTLS_SSL_SIG_RSA; |
Manuel Pégourié-Gonnard | d11eb7c | 2013-08-22 15:57:15 +0200 | [diff] [blame] | 266 | #endif |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 267 | } |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 268 | |
| 269 | /* |
| 270 | * enum { |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 271 | * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), |
| 272 | * sha512(6), (255) |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 273 | * } HashAlgorithm; |
| 274 | * |
| 275 | * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } |
| 276 | * SignatureAlgorithm; |
| 277 | * |
| 278 | * struct { |
| 279 | * HashAlgorithm hash; |
| 280 | * SignatureAlgorithm signature; |
| 281 | * } SignatureAndHashAlgorithm; |
| 282 | * |
| 283 | * SignatureAndHashAlgorithm |
| 284 | * supported_signature_algorithms<2..2^16-2>; |
| 285 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 286 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SIG_ALG >> 8 ) & 0xFF ); |
| 287 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SIG_ALG ) & 0xFF ); |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 288 | |
| 289 | *p++ = (unsigned char)( ( ( sig_alg_len + 2 ) >> 8 ) & 0xFF ); |
| 290 | *p++ = (unsigned char)( ( ( sig_alg_len + 2 ) ) & 0xFF ); |
| 291 | |
| 292 | *p++ = (unsigned char)( ( sig_alg_len >> 8 ) & 0xFF ); |
| 293 | *p++ = (unsigned char)( ( sig_alg_len ) & 0xFF ); |
| 294 | |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 295 | *olen = 6 + sig_alg_len; |
| 296 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 297 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 298 | MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 299 | |
Manuel Pégourié-Gonnard | f472179 | 2015-09-15 10:53:51 +0200 | [diff] [blame] | 300 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 301 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 302 | static void ssl_write_supported_elliptic_curves_ext( mbedtls_ssl_context *ssl, |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 303 | unsigned char *buf, |
| 304 | size_t *olen ) |
| 305 | { |
| 306 | unsigned char *p = buf; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 307 | const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN; |
Manuel Pégourié-Gonnard | 8e205fc | 2014-01-23 17:27:10 +0100 | [diff] [blame] | 308 | unsigned char *elliptic_curve_list = p + 6; |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 309 | size_t elliptic_curve_len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 310 | const mbedtls_ecp_curve_info *info; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 311 | const mbedtls_ecp_group_id *grp_id; |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 312 | |
| 313 | *olen = 0; |
| 314 | |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 315 | MBEDTLS_SSL_DEBUG_MSG( 3, |
| 316 | ( "client hello, adding supported_elliptic_curves extension" ) ); |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 317 | |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 318 | for( grp_id = ssl->conf->curve_list; |
| 319 | *grp_id != MBEDTLS_ECP_DP_NONE; |
| 320 | grp_id++ ) |
Manuel Pégourié-Gonnard | cd49f76 | 2014-02-04 15:14:13 +0100 | [diff] [blame] | 321 | { |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 322 | info = mbedtls_ecp_curve_info_from_grp_id( *grp_id ); |
Janos Follath | 8a31705 | 2016-04-21 23:37:09 +0100 | [diff] [blame] | 323 | if( info == NULL ) |
| 324 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 325 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 326 | ( "invalid curve in ssl configuration" ) ); |
Janos Follath | 8a31705 | 2016-04-21 23:37:09 +0100 | [diff] [blame] | 327 | return; |
| 328 | } |
| 329 | |
Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 330 | elliptic_curve_len += 2; |
| 331 | } |
| 332 | |
Simon Butcher | 0fc94e9 | 2015-09-28 20:52:04 +0100 | [diff] [blame] | 333 | if( end < p || (size_t)( end - p ) < 6 + elliptic_curve_len ) |
Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 334 | { |
| 335 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) ); |
| 336 | return; |
| 337 | } |
| 338 | |
| 339 | elliptic_curve_len = 0; |
| 340 | |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 341 | for( grp_id = ssl->conf->curve_list; |
| 342 | *grp_id != MBEDTLS_ECP_DP_NONE; |
| 343 | grp_id++ ) |
Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 344 | { |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 345 | info = mbedtls_ecp_curve_info_from_grp_id( *grp_id ); |
Manuel Pégourié-Gonnard | cd49f76 | 2014-02-04 15:14:13 +0100 | [diff] [blame] | 346 | elliptic_curve_list[elliptic_curve_len++] = info->tls_id >> 8; |
| 347 | elliptic_curve_list[elliptic_curve_len++] = info->tls_id & 0xFF; |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 348 | } |
Paul Bakker | 5dc6b5f | 2013-06-29 23:26:34 +0200 | [diff] [blame] | 349 | |
| 350 | if( elliptic_curve_len == 0 ) |
| 351 | return; |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 352 | |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 353 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES >> 8 ) |
| 354 | & 0xFF ); |
| 355 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES ) |
| 356 | & 0xFF ); |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 357 | |
| 358 | *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) >> 8 ) & 0xFF ); |
| 359 | *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) ) & 0xFF ); |
| 360 | |
| 361 | *p++ = (unsigned char)( ( ( elliptic_curve_len ) >> 8 ) & 0xFF ); |
| 362 | *p++ = (unsigned char)( ( ( elliptic_curve_len ) ) & 0xFF ); |
| 363 | |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 364 | *olen = 6 + elliptic_curve_len; |
| 365 | } |
| 366 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 367 | static void ssl_write_supported_point_formats_ext( mbedtls_ssl_context *ssl, |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 368 | unsigned char *buf, |
| 369 | size_t *olen ) |
| 370 | { |
| 371 | unsigned char *p = buf; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 372 | const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN; |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 373 | |
| 374 | *olen = 0; |
| 375 | |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 376 | MBEDTLS_SSL_DEBUG_MSG( 3, |
| 377 | ( "client hello, adding supported_point_formats extension" ) ); |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 378 | |
Simon Butcher | 0fc94e9 | 2015-09-28 20:52:04 +0100 | [diff] [blame] | 379 | if( end < p || (size_t)( end - p ) < 6 ) |
Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 380 | { |
| 381 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) ); |
| 382 | return; |
| 383 | } |
| 384 | |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 385 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) |
| 386 | & 0xFF ); |
| 387 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS ) |
| 388 | & 0xFF ); |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 389 | |
| 390 | *p++ = 0x00; |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 391 | *p++ = 2; |
Manuel Pégourié-Gonnard | 6b8846d | 2013-08-15 17:42:02 +0200 | [diff] [blame] | 392 | |
| 393 | *p++ = 1; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 394 | *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED; |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 395 | |
Manuel Pégourié-Gonnard | 6b8846d | 2013-08-15 17:42:02 +0200 | [diff] [blame] | 396 | *olen = 6; |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 397 | } |
Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 398 | #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || |
Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 399 | MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 400 | |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 401 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 294139b | 2015-09-15 16:55:05 +0200 | [diff] [blame] | 402 | static void ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl, |
| 403 | unsigned char *buf, |
| 404 | size_t *olen ) |
| 405 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 406 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 294139b | 2015-09-15 16:55:05 +0200 | [diff] [blame] | 407 | unsigned char *p = buf; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 408 | const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN; |
Manuel Pégourié-Gonnard | 294139b | 2015-09-15 16:55:05 +0200 | [diff] [blame] | 409 | size_t kkpp_len; |
| 410 | |
| 411 | *olen = 0; |
| 412 | |
| 413 | /* Skip costly extension if we can't use EC J-PAKE anyway */ |
| 414 | if( mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 ) |
| 415 | return; |
| 416 | |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 417 | MBEDTLS_SSL_DEBUG_MSG( 3, |
| 418 | ( "client hello, adding ecjpake_kkpp extension" ) ); |
Manuel Pégourié-Gonnard | 294139b | 2015-09-15 16:55:05 +0200 | [diff] [blame] | 419 | |
| 420 | if( end - p < 4 ) |
| 421 | { |
| 422 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) ); |
| 423 | return; |
| 424 | } |
| 425 | |
| 426 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP >> 8 ) & 0xFF ); |
| 427 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP ) & 0xFF ); |
| 428 | |
Manuel Pégourié-Gonnard | d0d8cb3 | 2015-09-17 14:16:30 +0200 | [diff] [blame] | 429 | /* |
| 430 | * We may need to send ClientHello multiple times for Hello verification. |
| 431 | * We don't want to compute fresh values every time (both for performance |
| 432 | * and consistency reasons), so cache the extension content. |
| 433 | */ |
| 434 | if( ssl->handshake->ecjpake_cache == NULL || |
| 435 | ssl->handshake->ecjpake_cache_len == 0 ) |
Manuel Pégourié-Gonnard | 294139b | 2015-09-15 16:55:05 +0200 | [diff] [blame] | 436 | { |
Manuel Pégourié-Gonnard | d0d8cb3 | 2015-09-17 14:16:30 +0200 | [diff] [blame] | 437 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "generating new ecjpake parameters" ) ); |
| 438 | |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 439 | ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx, |
| 440 | p + 2, end - p - 2, &kkpp_len, |
| 441 | ssl->conf->f_rng, ssl->conf->p_rng ); |
| 442 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | d0d8cb3 | 2015-09-17 14:16:30 +0200 | [diff] [blame] | 443 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 444 | MBEDTLS_SSL_DEBUG_RET( 1 , |
| 445 | "mbedtls_ecjpake_write_round_one", ret ); |
Manuel Pégourié-Gonnard | d0d8cb3 | 2015-09-17 14:16:30 +0200 | [diff] [blame] | 446 | return; |
| 447 | } |
| 448 | |
| 449 | ssl->handshake->ecjpake_cache = mbedtls_calloc( 1, kkpp_len ); |
| 450 | if( ssl->handshake->ecjpake_cache == NULL ) |
| 451 | { |
| 452 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "allocation failed" ) ); |
| 453 | return; |
| 454 | } |
| 455 | |
| 456 | memcpy( ssl->handshake->ecjpake_cache, p + 2, kkpp_len ); |
| 457 | ssl->handshake->ecjpake_cache_len = kkpp_len; |
| 458 | } |
| 459 | else |
| 460 | { |
| 461 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "re-using cached ecjpake parameters" ) ); |
| 462 | |
| 463 | kkpp_len = ssl->handshake->ecjpake_cache_len; |
| 464 | |
| 465 | if( (size_t)( end - p - 2 ) < kkpp_len ) |
| 466 | { |
| 467 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) ); |
| 468 | return; |
| 469 | } |
| 470 | |
| 471 | memcpy( p + 2, ssl->handshake->ecjpake_cache, kkpp_len ); |
Manuel Pégourié-Gonnard | 294139b | 2015-09-15 16:55:05 +0200 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | *p++ = (unsigned char)( ( kkpp_len >> 8 ) & 0xFF ); |
| 475 | *p++ = (unsigned char)( ( kkpp_len ) & 0xFF ); |
| 476 | |
| 477 | *olen = kkpp_len + 4; |
| 478 | } |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 479 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Paul Bakker | c3f177a | 2012-04-11 16:11:49 +0000 | [diff] [blame] | 480 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 481 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 49770ff | 2019-04-25 16:55:15 +0100 | [diff] [blame] | 482 | static void ssl_write_cid_ext( mbedtls_ssl_context *ssl, |
| 483 | unsigned char *buf, |
| 484 | size_t *olen ) |
| 485 | { |
| 486 | unsigned char *p = buf; |
| 487 | size_t ext_len; |
| 488 | const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN; |
| 489 | |
| 490 | /* |
Hanno Becker | ebcc913 | 2019-05-15 10:26:32 +0100 | [diff] [blame] | 491 | * Quoting draft-ietf-tls-dtls-connection-id-05 |
| 492 | * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05 |
Hanno Becker | 49770ff | 2019-04-25 16:55:15 +0100 | [diff] [blame] | 493 | * |
| 494 | * struct { |
| 495 | * opaque cid<0..2^8-1>; |
| 496 | * } ConnectionId; |
| 497 | */ |
| 498 | |
| 499 | *olen = 0; |
| 500 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
| 501 | ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED ) |
| 502 | { |
| 503 | return; |
| 504 | } |
| 505 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding CID extension" ) ); |
| 506 | |
| 507 | /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX |
| 508 | * which is at most 255, so the increment cannot overflow. */ |
| 509 | if( end < p || (size_t)( end - p ) < (unsigned)( ssl->own_cid_len + 5 ) ) |
| 510 | { |
| 511 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) ); |
| 512 | return; |
| 513 | } |
| 514 | |
| 515 | /* Add extension ID + size */ |
| 516 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_CID >> 8 ) & 0xFF ); |
| 517 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_CID ) & 0xFF ); |
| 518 | ext_len = (size_t) ssl->own_cid_len + 1; |
| 519 | *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF ); |
| 520 | *p++ = (unsigned char)( ( ext_len ) & 0xFF ); |
| 521 | |
| 522 | *p++ = (uint8_t) ssl->own_cid_len; |
| 523 | memcpy( p, ssl->own_cid, ssl->own_cid_len ); |
| 524 | |
| 525 | *olen = ssl->own_cid_len + 5; |
| 526 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 527 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 49770ff | 2019-04-25 16:55:15 +0100 | [diff] [blame] | 528 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 529 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 530 | static void ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | a052849 | 2013-07-16 17:26:28 +0200 | [diff] [blame] | 531 | unsigned char *buf, |
| 532 | size_t *olen ) |
| 533 | { |
| 534 | unsigned char *p = buf; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 535 | const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN; |
Manuel Pégourié-Gonnard | a052849 | 2013-07-16 17:26:28 +0200 | [diff] [blame] | 536 | |
Simon Butcher | 0fc94e9 | 2015-09-28 20:52:04 +0100 | [diff] [blame] | 537 | *olen = 0; |
| 538 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 539 | if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ) { |
Manuel Pégourié-Gonnard | a052849 | 2013-07-16 17:26:28 +0200 | [diff] [blame] | 540 | return; |
| 541 | } |
| 542 | |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 543 | MBEDTLS_SSL_DEBUG_MSG( 3, |
| 544 | ( "client hello, adding max_fragment_length extension" ) ); |
Manuel Pégourié-Gonnard | a052849 | 2013-07-16 17:26:28 +0200 | [diff] [blame] | 545 | |
Simon Butcher | 0fc94e9 | 2015-09-28 20:52:04 +0100 | [diff] [blame] | 546 | if( end < p || (size_t)( end - p ) < 5 ) |
Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 547 | { |
| 548 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) ); |
| 549 | return; |
| 550 | } |
| 551 | |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 552 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) |
| 553 | & 0xFF ); |
| 554 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH ) |
| 555 | & 0xFF ); |
Manuel Pégourié-Gonnard | a052849 | 2013-07-16 17:26:28 +0200 | [diff] [blame] | 556 | |
| 557 | *p++ = 0x00; |
| 558 | *p++ = 1; |
| 559 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 560 | *p++ = ssl->conf->mfl_code; |
Manuel Pégourié-Gonnard | a052849 | 2013-07-16 17:26:28 +0200 | [diff] [blame] | 561 | |
| 562 | *olen = 5; |
| 563 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 564 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | a052849 | 2013-07-16 17:26:28 +0200 | [diff] [blame] | 565 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 566 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 567 | 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] | 568 | unsigned char *buf, size_t *olen ) |
| 569 | { |
| 570 | unsigned char *p = buf; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 571 | const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN; |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 572 | |
Simon Butcher | 0fc94e9 | 2015-09-28 20:52:04 +0100 | [diff] [blame] | 573 | *olen = 0; |
| 574 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 575 | if( ssl->conf->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_DISABLED ) |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 576 | { |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 577 | return; |
| 578 | } |
| 579 | |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 580 | MBEDTLS_SSL_DEBUG_MSG( 3, |
| 581 | ( "client hello, adding truncated_hmac extension" ) ); |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 582 | |
Simon Butcher | 0fc94e9 | 2015-09-28 20:52:04 +0100 | [diff] [blame] | 583 | if( end < p || (size_t)( end - p ) < 4 ) |
Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 584 | { |
| 585 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) ); |
| 586 | return; |
| 587 | } |
| 588 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 589 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF ); |
| 590 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_TRUNCATED_HMAC ) & 0xFF ); |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 591 | |
| 592 | *p++ = 0x00; |
| 593 | *p++ = 0x00; |
| 594 | |
| 595 | *olen = 4; |
| 596 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 597 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 598 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 599 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 600 | 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] | 601 | unsigned char *buf, size_t *olen ) |
| 602 | { |
| 603 | unsigned char *p = buf; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 604 | const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 605 | |
Simon Butcher | 0fc94e9 | 2015-09-28 20:52:04 +0100 | [diff] [blame] | 606 | *olen = 0; |
| 607 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 608 | if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED || |
| 609 | ssl->conf->max_minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 610 | { |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 611 | return; |
| 612 | } |
| 613 | |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 614 | MBEDTLS_SSL_DEBUG_MSG( 3, |
| 615 | ( "client hello, adding encrypt_then_mac extension" ) ); |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 616 | |
Simon Butcher | 0fc94e9 | 2015-09-28 20:52:04 +0100 | [diff] [blame] | 617 | if( end < p || (size_t)( end - p ) < 4 ) |
Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 618 | { |
| 619 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) ); |
| 620 | return; |
| 621 | } |
| 622 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 623 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF ); |
| 624 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF ); |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 625 | |
| 626 | *p++ = 0x00; |
| 627 | *p++ = 0x00; |
| 628 | |
| 629 | *olen = 4; |
| 630 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 631 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 632 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 633 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 634 | 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] | 635 | unsigned char *buf, size_t *olen ) |
| 636 | { |
| 637 | unsigned char *p = buf; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 638 | const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 639 | |
Simon Butcher | 0fc94e9 | 2015-09-28 20:52:04 +0100 | [diff] [blame] | 640 | *olen = 0; |
| 641 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 642 | if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED || |
| 643 | ssl->conf->max_minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 644 | { |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 645 | return; |
| 646 | } |
| 647 | |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 648 | MBEDTLS_SSL_DEBUG_MSG( 3, |
| 649 | ( "client hello, adding extended_master_secret extension" ) ); |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 650 | |
Simon Butcher | 0fc94e9 | 2015-09-28 20:52:04 +0100 | [diff] [blame] | 651 | if( end < p || (size_t)( end - p ) < 4 ) |
Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 652 | { |
| 653 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) ); |
| 654 | return; |
| 655 | } |
| 656 | |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 657 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) |
| 658 | & 0xFF ); |
| 659 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET ) |
| 660 | & 0xFF ); |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 661 | |
| 662 | *p++ = 0x00; |
| 663 | *p++ = 0x00; |
| 664 | |
| 665 | *olen = 4; |
| 666 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 667 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 668 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 669 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 670 | static void ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 671 | unsigned char *buf, size_t *olen ) |
| 672 | { |
| 673 | unsigned char *p = buf; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 674 | const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN; |
Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 675 | size_t tlen = ssl->session_negotiate->ticket_len; |
| 676 | |
Simon Butcher | 0fc94e9 | 2015-09-28 20:52:04 +0100 | [diff] [blame] | 677 | *olen = 0; |
| 678 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 679 | if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED ) |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 680 | { |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 681 | return; |
| 682 | } |
| 683 | |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 684 | MBEDTLS_SSL_DEBUG_MSG( 3, |
| 685 | ( "client hello, adding session ticket extension" ) ); |
Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 686 | |
Simon Butcher | 0fc94e9 | 2015-09-28 20:52:04 +0100 | [diff] [blame] | 687 | if( end < p || (size_t)( end - p ) < 4 + tlen ) |
Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 688 | { |
| 689 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) ); |
| 690 | return; |
| 691 | } |
| 692 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 693 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF ); |
| 694 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET ) & 0xFF ); |
Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 695 | |
| 696 | *p++ = (unsigned char)( ( tlen >> 8 ) & 0xFF ); |
| 697 | *p++ = (unsigned char)( ( tlen ) & 0xFF ); |
| 698 | |
| 699 | *olen = 4; |
| 700 | |
Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 701 | if( ssl->session_negotiate->ticket == NULL || tlen == 0 ) |
Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 702 | { |
| 703 | return; |
| 704 | } |
| 705 | |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 706 | MBEDTLS_SSL_DEBUG_MSG( 3, |
| 707 | ( "sending session ticket of length %d", tlen ) ); |
Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 708 | |
| 709 | memcpy( p, ssl->session_negotiate->ticket, tlen ); |
| 710 | |
| 711 | *olen += tlen; |
| 712 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 713 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 714 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 715 | #if defined(MBEDTLS_SSL_ALPN) |
| 716 | static void ssl_write_alpn_ext( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 717 | unsigned char *buf, size_t *olen ) |
| 718 | { |
| 719 | unsigned char *p = buf; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 720 | const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN; |
Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 721 | size_t alpnlen = 0; |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 722 | const char **cur; |
| 723 | |
Simon Butcher | 0fc94e9 | 2015-09-28 20:52:04 +0100 | [diff] [blame] | 724 | *olen = 0; |
| 725 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 726 | if( ssl->conf->alpn_list == NULL ) |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 727 | { |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 728 | return; |
| 729 | } |
| 730 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 731 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) ); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 732 | |
Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 733 | for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ ) |
Simon Butcher | 04799a4 | 2015-09-29 00:31:09 +0100 | [diff] [blame] | 734 | alpnlen += (unsigned char)( strlen( *cur ) & 0xFF ) + 1; |
Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 735 | |
Simon Butcher | 0fc94e9 | 2015-09-28 20:52:04 +0100 | [diff] [blame] | 736 | if( end < p || (size_t)( end - p ) < 6 + alpnlen ) |
Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 737 | { |
| 738 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) ); |
| 739 | return; |
| 740 | } |
| 741 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 742 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN >> 8 ) & 0xFF ); |
| 743 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN ) & 0xFF ); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 744 | |
| 745 | /* |
| 746 | * opaque ProtocolName<1..2^8-1>; |
| 747 | * |
| 748 | * struct { |
| 749 | * ProtocolName protocol_name_list<2..2^16-1> |
| 750 | * } ProtocolNameList; |
| 751 | */ |
| 752 | |
| 753 | /* Skip writing extension and list length for now */ |
| 754 | p += 4; |
| 755 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 756 | for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ ) |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 757 | { |
| 758 | *p = (unsigned char)( strlen( *cur ) & 0xFF ); |
| 759 | memcpy( p + 1, *cur, *p ); |
| 760 | p += 1 + *p; |
| 761 | } |
| 762 | |
| 763 | *olen = p - buf; |
| 764 | |
| 765 | /* List length = olen - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */ |
| 766 | buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF ); |
| 767 | buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF ); |
| 768 | |
| 769 | /* Extension length = olen - 2 (ext_type) - 2 (ext_len) */ |
| 770 | buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF ); |
| 771 | buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF ); |
| 772 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 773 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 774 | |
Manuel Pégourié-Gonnard | b760f00 | 2014-07-22 15:53:27 +0200 | [diff] [blame] | 775 | /* |
| 776 | * Generate random bytes for ClientHello |
| 777 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 778 | static int ssl_generate_random( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | b760f00 | 2014-07-22 15:53:27 +0200 | [diff] [blame] | 779 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 780 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | b760f00 | 2014-07-22 15:53:27 +0200 | [diff] [blame] | 781 | unsigned char *p = ssl->handshake->randbytes; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 782 | #if defined(MBEDTLS_HAVE_TIME) |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 783 | mbedtls_time_t t; |
Manuel Pégourié-Gonnard | b760f00 | 2014-07-22 15:53:27 +0200 | [diff] [blame] | 784 | #endif |
| 785 | |
Manuel Pégourié-Gonnard | fb2d223 | 2014-07-22 15:59:14 +0200 | [diff] [blame] | 786 | /* |
| 787 | * When responding to a verify request, MUST reuse random (RFC 6347 4.2.1) |
| 788 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 789 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 790 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | fb2d223 | 2014-07-22 15:59:14 +0200 | [diff] [blame] | 791 | ssl->handshake->verify_cookie != NULL ) |
| 792 | { |
| 793 | return( 0 ); |
| 794 | } |
| 795 | #endif |
| 796 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 797 | #if defined(MBEDTLS_HAVE_TIME) |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 798 | t = mbedtls_time( NULL ); |
Manuel Pégourié-Gonnard | b760f00 | 2014-07-22 15:53:27 +0200 | [diff] [blame] | 799 | *p++ = (unsigned char)( t >> 24 ); |
| 800 | *p++ = (unsigned char)( t >> 16 ); |
| 801 | *p++ = (unsigned char)( t >> 8 ); |
| 802 | *p++ = (unsigned char)( t ); |
| 803 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 804 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, current time: %lu", t ) ); |
Manuel Pégourié-Gonnard | b760f00 | 2014-07-22 15:53:27 +0200 | [diff] [blame] | 805 | #else |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 806 | if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 4 ) ) != 0 ) |
Manuel Pégourié-Gonnard | b760f00 | 2014-07-22 15:53:27 +0200 | [diff] [blame] | 807 | return( ret ); |
| 808 | |
| 809 | p += 4; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 810 | #endif /* MBEDTLS_HAVE_TIME */ |
Manuel Pégourié-Gonnard | b760f00 | 2014-07-22 15:53:27 +0200 | [diff] [blame] | 811 | |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 812 | if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 28 ) ) != 0 ) |
Manuel Pégourié-Gonnard | b760f00 | 2014-07-22 15:53:27 +0200 | [diff] [blame] | 813 | return( ret ); |
| 814 | |
| 815 | return( 0 ); |
| 816 | } |
| 817 | |
Mohammad Azim Khan | 1d3b508 | 2018-04-18 19:35:00 +0100 | [diff] [blame] | 818 | /** |
| 819 | * \brief Validate cipher suite against config in SSL context. |
| 820 | * |
| 821 | * \param suite_info cipher suite to validate |
| 822 | * \param ssl SSL context |
Andrzej Kurek | 03bac44 | 2018-04-25 05:06:07 -0400 | [diff] [blame] | 823 | * \param min_minor_ver Minimal minor version to accept a cipher suite |
| 824 | * \param max_minor_ver Maximal minor version to accept a cipher suite |
Mohammad Azim Khan | 1d3b508 | 2018-04-18 19:35:00 +0100 | [diff] [blame] | 825 | * |
| 826 | * \return 0 if valid, else 1 |
| 827 | */ |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 828 | static int ssl_validate_ciphersuite( |
| 829 | const mbedtls_ssl_ciphersuite_t * suite_info, |
| 830 | const mbedtls_ssl_context * ssl, |
| 831 | int min_minor_ver, int max_minor_ver ) |
Mohammad Azim Khan | 1d3b508 | 2018-04-18 19:35:00 +0100 | [diff] [blame] | 832 | { |
Andrzej Kurek | 03bac44 | 2018-04-25 05:06:07 -0400 | [diff] [blame] | 833 | (void) ssl; |
Mohammad Azim Khan | 1d3b508 | 2018-04-18 19:35:00 +0100 | [diff] [blame] | 834 | if( suite_info == NULL ) |
| 835 | return( 1 ); |
| 836 | |
Andrzej Kurek | 03bac44 | 2018-04-25 05:06:07 -0400 | [diff] [blame] | 837 | if( suite_info->min_minor_ver > max_minor_ver || |
| 838 | suite_info->max_minor_ver < min_minor_ver ) |
Mohammad Azim Khan | 1d3b508 | 2018-04-18 19:35:00 +0100 | [diff] [blame] | 839 | return( 1 ); |
| 840 | |
| 841 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 842 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 843 | ( suite_info->flags & MBEDTLS_CIPHERSUITE_NODTLS ) ) |
| 844 | return( 1 ); |
| 845 | #endif |
| 846 | |
| 847 | #if defined(MBEDTLS_ARC4_C) |
| 848 | if( ssl->conf->arc4_disabled == MBEDTLS_SSL_ARC4_DISABLED && |
| 849 | suite_info->cipher == MBEDTLS_CIPHER_ARC4_128 ) |
| 850 | return( 1 ); |
| 851 | #endif |
| 852 | |
| 853 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 854 | if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE && |
| 855 | mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 ) |
| 856 | return( 1 ); |
| 857 | #endif |
| 858 | |
Hanno Becker | 2e4f616 | 2018-10-23 11:54:44 +0100 | [diff] [blame] | 859 | /* Don't suggest PSK-based ciphersuite if no PSK is available. */ |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 860 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Hanno Becker | 2e4f616 | 2018-10-23 11:54:44 +0100 | [diff] [blame] | 861 | if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) && |
Hanno Becker | 520224e | 2018-10-26 11:38:07 +0100 | [diff] [blame] | 862 | ssl_conf_has_static_psk( ssl->conf ) == 0 ) |
Hanno Becker | 2e4f616 | 2018-10-23 11:54:44 +0100 | [diff] [blame] | 863 | { |
| 864 | return( 1 ); |
| 865 | } |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 866 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
Hanno Becker | 2e4f616 | 2018-10-23 11:54:44 +0100 | [diff] [blame] | 867 | |
Mohammad Azim Khan | 1d3b508 | 2018-04-18 19:35:00 +0100 | [diff] [blame] | 868 | return( 0 ); |
| 869 | } |
| 870 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 871 | static int ssl_write_client_hello( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 872 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 873 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 874 | size_t i, n, olen, ext_len = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 875 | unsigned char *buf; |
Paul Bakker | 2fbefde | 2013-06-29 16:01:15 +0200 | [diff] [blame] | 876 | unsigned char *p, *q; |
Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 877 | unsigned char offer_compress; |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 878 | const int *ciphersuites; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 879 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
Ron Eldor | 755bb6a | 2018-02-14 19:30:48 +0200 | [diff] [blame] | 880 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 881 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 882 | int uses_ec = 0; |
| 883 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 884 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 885 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 886 | |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 887 | if( ssl->conf->f_rng == NULL ) |
Paul Bakker | a9a028e | 2013-11-21 17:31:06 +0100 | [diff] [blame] | 888 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 889 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided") ); |
| 890 | return( MBEDTLS_ERR_SSL_NO_RNG ); |
Paul Bakker | a9a028e | 2013-11-21 17:31:06 +0100 | [diff] [blame] | 891 | } |
| 892 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 893 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 894 | if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE ) |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 895 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 896 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 897 | ssl->major_ver = ssl->conf->min_major_ver; |
| 898 | ssl->minor_ver = ssl->conf->min_minor_ver; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 899 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 900 | |
Manuel Pégourié-Gonnard | 1897af9 | 2015-05-10 23:27:38 +0200 | [diff] [blame] | 901 | if( ssl->conf->max_major_ver == 0 ) |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 902 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 903 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 904 | ( "configured max major version is invalid, consider using mbedtls_ssl_config_defaults()" ) ); |
Manuel Pégourié-Gonnard | 1897af9 | 2015-05-10 23:27:38 +0200 | [diff] [blame] | 905 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 906 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 907 | |
| 908 | /* |
| 909 | * 0 . 0 handshake type |
| 910 | * 1 . 3 handshake length |
| 911 | * 4 . 5 highest version supported |
| 912 | * 6 . 9 current UNIX time |
| 913 | * 10 . 37 random bytes |
| 914 | */ |
| 915 | buf = ssl->out_msg; |
| 916 | p = buf + 4; |
| 917 | |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 918 | mbedtls_ssl_write_version( ssl->conf->max_major_ver, |
| 919 | ssl->conf->max_minor_ver, |
| 920 | ssl->conf->transport, p ); |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 921 | p += 2; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 922 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 923 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, max version: [%d:%d]", |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 924 | buf[4], buf[5] ) ); |
| 925 | |
Manuel Pégourié-Gonnard | b760f00 | 2014-07-22 15:53:27 +0200 | [diff] [blame] | 926 | if( ( ret = ssl_generate_random( ssl ) ) != 0 ) |
| 927 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 928 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_generate_random", ret ); |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 929 | return( ret ); |
Manuel Pégourié-Gonnard | b760f00 | 2014-07-22 15:53:27 +0200 | [diff] [blame] | 930 | } |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 931 | |
Manuel Pégourié-Gonnard | b760f00 | 2014-07-22 15:53:27 +0200 | [diff] [blame] | 932 | memcpy( p, ssl->handshake->randbytes, 32 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 933 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", p, 32 ); |
Manuel Pégourié-Gonnard | b760f00 | 2014-07-22 15:53:27 +0200 | [diff] [blame] | 934 | p += 32; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 935 | |
| 936 | /* |
| 937 | * 38 . 38 session id length |
| 938 | * 39 . 39+n session id |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 939 | * 39+n . 39+n DTLS only: cookie length (1 byte) |
| 940 | * 40+n . .. DTSL only: cookie |
| 941 | * .. . .. ciphersuitelist length (2 bytes) |
| 942 | * .. . .. ciphersuitelist |
| 943 | * .. . .. compression methods length (1 byte) |
Paul Bakker | c3f177a | 2012-04-11 16:11:49 +0000 | [diff] [blame] | 944 | * .. . .. compression methods |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 945 | * .. . .. extensions length (2 bytes) |
Paul Bakker | c3f177a | 2012-04-11 16:11:49 +0000 | [diff] [blame] | 946 | * .. . .. extensions |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 947 | */ |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 948 | n = ssl->session_negotiate->id_len; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 949 | |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 950 | if( n < 16 || n > 32 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 951 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 952 | ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE || |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 953 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 954 | ssl->handshake->resume == 0 ) |
Manuel Pégourié-Gonnard | 6377e41 | 2013-07-31 16:31:33 +0200 | [diff] [blame] | 955 | { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 956 | n = 0; |
Manuel Pégourié-Gonnard | 6377e41 | 2013-07-31 16:31:33 +0200 | [diff] [blame] | 957 | } |
| 958 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 959 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | 6377e41 | 2013-07-31 16:31:33 +0200 | [diff] [blame] | 960 | /* |
| 961 | * RFC 5077 section 3.4: "When presenting a ticket, the client MAY |
| 962 | * generate and include a Session ID in the TLS ClientHello." |
| 963 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 964 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 965 | if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE ) |
Manuel Pégourié-Gonnard | 59c6f2e | 2015-01-22 11:06:40 +0000 | [diff] [blame] | 966 | #endif |
Manuel Pégourié-Gonnard | d2b35ec | 2015-03-10 11:40:43 +0000 | [diff] [blame] | 967 | { |
Manuel Pégourié-Gonnard | 59c6f2e | 2015-01-22 11:06:40 +0000 | [diff] [blame] | 968 | if( ssl->session_negotiate->ticket != NULL && |
| 969 | ssl->session_negotiate->ticket_len != 0 ) |
| 970 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 971 | ret = ssl->conf->f_rng( ssl->conf->p_rng, |
| 972 | ssl->session_negotiate->id, 32 ); |
Manuel Pégourié-Gonnard | 6377e41 | 2013-07-31 16:31:33 +0200 | [diff] [blame] | 973 | |
Manuel Pégourié-Gonnard | 59c6f2e | 2015-01-22 11:06:40 +0000 | [diff] [blame] | 974 | if( ret != 0 ) |
| 975 | return( ret ); |
Manuel Pégourié-Gonnard | 6377e41 | 2013-07-31 16:31:33 +0200 | [diff] [blame] | 976 | |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 977 | ssl->session_negotiate->id_len = n = 32; |
Manuel Pégourié-Gonnard | 59c6f2e | 2015-01-22 11:06:40 +0000 | [diff] [blame] | 978 | } |
Manuel Pégourié-Gonnard | 6377e41 | 2013-07-31 16:31:33 +0200 | [diff] [blame] | 979 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 980 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 981 | |
| 982 | *p++ = (unsigned char) n; |
| 983 | |
| 984 | for( i = 0; i < n; i++ ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 985 | *p++ = ssl->session_negotiate->id[i]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 986 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 987 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, session id len.: %d", n ) ); |
| 988 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id", buf + 39, n ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 989 | |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 990 | /* |
| 991 | * DTLS cookie |
| 992 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 993 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 994 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 995 | { |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 996 | if( ssl->handshake->verify_cookie == NULL ) |
| 997 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 998 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no verify cookie to send" ) ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 999 | *p++ = 0; |
| 1000 | } |
| 1001 | else |
| 1002 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1003 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie", |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1004 | ssl->handshake->verify_cookie, |
| 1005 | ssl->handshake->verify_cookie_len ); |
| 1006 | |
| 1007 | *p++ = ssl->handshake->verify_cookie_len; |
| 1008 | memcpy( p, ssl->handshake->verify_cookie, |
| 1009 | ssl->handshake->verify_cookie_len ); |
| 1010 | p += ssl->handshake->verify_cookie_len; |
| 1011 | } |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1012 | } |
| 1013 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1014 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1015 | /* |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1016 | * Ciphersuite list |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1017 | */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1018 | ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver]; |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1019 | |
| 1020 | /* Skip writing ciphersuite length for now */ |
| 1021 | n = 0; |
| 1022 | q = p; |
| 1023 | p += 2; |
| 1024 | |
Paul Bakker | 2fbefde | 2013-06-29 16:01:15 +0200 | [diff] [blame] | 1025 | for( i = 0; ciphersuites[i] != 0; i++ ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1026 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1027 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuites[i] ); |
Paul Bakker | 2fbefde | 2013-06-29 16:01:15 +0200 | [diff] [blame] | 1028 | |
Andrzej Kurek | 03bac44 | 2018-04-25 05:06:07 -0400 | [diff] [blame] | 1029 | if( ssl_validate_ciphersuite( ciphersuite_info, ssl, |
| 1030 | ssl->conf->min_minor_ver, |
| 1031 | ssl->conf->max_minor_ver ) != 0 ) |
Paul Bakker | 2fbefde | 2013-06-29 16:01:15 +0200 | [diff] [blame] | 1032 | continue; |
| 1033 | |
Manuel Pégourié-Gonnard | 60884a1 | 2015-09-16 11:13:41 +0200 | [diff] [blame] | 1034 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x", |
| 1035 | ciphersuites[i] ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1036 | |
Ron Eldor | 755bb6a | 2018-02-14 19:30:48 +0200 | [diff] [blame] | 1037 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 1038 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 1039 | uses_ec |= mbedtls_ssl_ciphersuite_uses_ec( ciphersuite_info ); |
| 1040 | #endif |
| 1041 | |
Paul Bakker | 2fbefde | 2013-06-29 16:01:15 +0200 | [diff] [blame] | 1042 | n++; |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 1043 | *p++ = (unsigned char)( ciphersuites[i] >> 8 ); |
| 1044 | *p++ = (unsigned char)( ciphersuites[i] ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1047 | MBEDTLS_SSL_DEBUG_MSG( 3, |
| 1048 | ( "client hello, got %d ciphersuites (excluding SCSVs)", n ) ); |
Ron Eldor | 714785d | 2017-08-28 13:55:55 +0300 | [diff] [blame] | 1049 | |
Manuel Pégourié-Gonnard | 5d9cde2 | 2015-01-22 10:49:41 +0000 | [diff] [blame] | 1050 | /* |
| 1051 | * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV |
| 1052 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1053 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1054 | if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE ) |
Manuel Pégourié-Gonnard | 5d9cde2 | 2015-01-22 10:49:41 +0000 | [diff] [blame] | 1055 | #endif |
| 1056 | { |
Ron Eldor | 4a2fb4c | 2017-09-10 17:03:50 +0300 | [diff] [blame] | 1057 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding EMPTY_RENEGOTIATION_INFO_SCSV" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1058 | *p++ = (unsigned char)( MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO >> 8 ); |
| 1059 | *p++ = (unsigned char)( MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO ); |
Manuel Pégourié-Gonnard | 5d9cde2 | 2015-01-22 10:49:41 +0000 | [diff] [blame] | 1060 | n++; |
| 1061 | } |
| 1062 | |
Manuel Pégourié-Gonnard | 1cbd39d | 2014-10-20 13:34:59 +0200 | [diff] [blame] | 1063 | /* Some versions of OpenSSL don't handle it correctly if not at end */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1064 | #if defined(MBEDTLS_SSL_FALLBACK_SCSV) |
Manuel Pégourié-Gonnard | 684b059 | 2015-05-06 09:27:31 +0100 | [diff] [blame] | 1065 | if( ssl->conf->fallback == MBEDTLS_SSL_IS_FALLBACK ) |
Manuel Pégourié-Gonnard | 1cbd39d | 2014-10-20 13:34:59 +0200 | [diff] [blame] | 1066 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1067 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding FALLBACK_SCSV" ) ); |
| 1068 | *p++ = (unsigned char)( MBEDTLS_SSL_FALLBACK_SCSV_VALUE >> 8 ); |
| 1069 | *p++ = (unsigned char)( MBEDTLS_SSL_FALLBACK_SCSV_VALUE ); |
Manuel Pégourié-Gonnard | 1cbd39d | 2014-10-20 13:34:59 +0200 | [diff] [blame] | 1070 | n++; |
| 1071 | } |
| 1072 | #endif |
| 1073 | |
Paul Bakker | 2fbefde | 2013-06-29 16:01:15 +0200 | [diff] [blame] | 1074 | *q++ = (unsigned char)( n >> 7 ); |
| 1075 | *q++ = (unsigned char)( n << 1 ); |
| 1076 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1077 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1078 | offer_compress = 1; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1079 | #else |
Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1080 | offer_compress = 0; |
| 1081 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1082 | |
Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1083 | /* |
Johannes H | 4e5d23f | 2018-01-06 09:46:57 +0100 | [diff] [blame] | 1084 | * We don't support compression with DTLS right now: if many records come |
Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1085 | * in the same datagram, uncompressing one could overwrite the next one. |
| 1086 | * We don't want to add complexity for handling that case unless there is |
| 1087 | * an actual need for it. |
| 1088 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1089 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1090 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1091 | offer_compress = 0; |
| 1092 | #endif |
| 1093 | |
| 1094 | if( offer_compress ) |
| 1095 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1096 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 2 ) ); |
| 1097 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d %d", |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1098 | MBEDTLS_SSL_COMPRESS_DEFLATE, |
| 1099 | MBEDTLS_SSL_COMPRESS_NULL ) ); |
Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1100 | |
| 1101 | *p++ = 2; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1102 | *p++ = MBEDTLS_SSL_COMPRESS_DEFLATE; |
| 1103 | *p++ = MBEDTLS_SSL_COMPRESS_NULL; |
Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1104 | } |
| 1105 | else |
| 1106 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1107 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 1 ) ); |
| 1108 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d", |
| 1109 | MBEDTLS_SSL_COMPRESS_NULL ) ); |
Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1110 | |
| 1111 | *p++ = 1; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1112 | *p++ = MBEDTLS_SSL_COMPRESS_NULL; |
Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1113 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1114 | |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 1115 | // First write extensions, then the total length |
| 1116 | // |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1117 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 1118 | ssl_write_hostname_ext( ssl, p + 2 + ext_len, &olen ); |
| 1119 | ext_len += olen; |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 1120 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1121 | |
Hanno Becker | 40f8b51 | 2017-10-12 14:58:55 +0100 | [diff] [blame] | 1122 | /* Note that TLS_EMPTY_RENEGOTIATION_INFO_SCSV is always added |
| 1123 | * even if MBEDTLS_SSL_RENEGOTIATION is not defined. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1124 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 1125 | ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen ); |
| 1126 | ext_len += olen; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1127 | #endif |
Paul Bakker | c3f177a | 2012-04-11 16:11:49 +0000 | [diff] [blame] | 1128 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1129 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1130 | defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 1131 | ssl_write_signature_algorithms_ext( ssl, p + 2 + ext_len, &olen ); |
| 1132 | ext_len += olen; |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 1133 | #endif |
Paul Bakker | c3f177a | 2012-04-11 16:11:49 +0000 | [diff] [blame] | 1134 | |
Manuel Pégourié-Gonnard | f472179 | 2015-09-15 10:53:51 +0200 | [diff] [blame] | 1135 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 1136 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Ron Eldor | 755bb6a | 2018-02-14 19:30:48 +0200 | [diff] [blame] | 1137 | if( uses_ec ) |
| 1138 | { |
| 1139 | ssl_write_supported_elliptic_curves_ext( ssl, p + 2 + ext_len, &olen ); |
| 1140 | ext_len += olen; |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1141 | |
Ron Eldor | 755bb6a | 2018-02-14 19:30:48 +0200 | [diff] [blame] | 1142 | ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen ); |
| 1143 | ext_len += olen; |
| 1144 | } |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1145 | #endif |
| 1146 | |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 1147 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 294139b | 2015-09-15 16:55:05 +0200 | [diff] [blame] | 1148 | ssl_write_ecjpake_kkpp_ext( ssl, p + 2 + ext_len, &olen ); |
| 1149 | ext_len += olen; |
| 1150 | #endif |
| 1151 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1152 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 49770ff | 2019-04-25 16:55:15 +0100 | [diff] [blame] | 1153 | ssl_write_cid_ext( ssl, p + 2 + ext_len, &olen ); |
| 1154 | ext_len += olen; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1155 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 49770ff | 2019-04-25 16:55:15 +0100 | [diff] [blame] | 1156 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1157 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | a052849 | 2013-07-16 17:26:28 +0200 | [diff] [blame] | 1158 | ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen ); |
| 1159 | ext_len += olen; |
Paul Bakker | 05decb2 | 2013-08-15 13:33:48 +0200 | [diff] [blame] | 1160 | #endif |
Manuel Pégourié-Gonnard | a052849 | 2013-07-16 17:26:28 +0200 | [diff] [blame] | 1161 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1162 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 1163 | ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen ); |
| 1164 | ext_len += olen; |
Paul Bakker | 1f2bc62 | 2013-08-15 13:45:55 +0200 | [diff] [blame] | 1165 | #endif |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 1166 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1167 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 1168 | ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen ); |
| 1169 | ext_len += olen; |
| 1170 | #endif |
| 1171 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1172 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 1173 | ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen ); |
| 1174 | ext_len += olen; |
| 1175 | #endif |
| 1176 | |
Simon Butcher | 5624ec8 | 2015-09-29 01:06:06 +0100 | [diff] [blame] | 1177 | #if defined(MBEDTLS_SSL_ALPN) |
| 1178 | ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen ); |
Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 1179 | ext_len += olen; |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 1180 | #endif |
Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 1181 | |
Simon Butcher | 5624ec8 | 2015-09-29 01:06:06 +0100 | [diff] [blame] | 1182 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 1183 | ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen ); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 1184 | ext_len += olen; |
| 1185 | #endif |
| 1186 | |
Manuel Pégourié-Gonnard | eaecbd3 | 2014-11-06 02:38:02 +0100 | [diff] [blame] | 1187 | /* olen unused if all extensions are disabled */ |
| 1188 | ((void) olen); |
| 1189 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1190 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %d", |
Paul Bakker | c3f177a | 2012-04-11 16:11:49 +0000 | [diff] [blame] | 1191 | ext_len ) ); |
| 1192 | |
Paul Bakker | a703663 | 2014-04-30 10:15:38 +0200 | [diff] [blame] | 1193 | if( ext_len > 0 ) |
| 1194 | { |
| 1195 | *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF ); |
| 1196 | *p++ = (unsigned char)( ( ext_len ) & 0xFF ); |
| 1197 | p += ext_len; |
| 1198 | } |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1199 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1200 | ssl->out_msglen = p - buf; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1201 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 1202 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_HELLO; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1203 | |
| 1204 | ssl->state++; |
| 1205 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1206 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1207 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1208 | mbedtls_ssl_send_flight_completed( ssl ); |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 1209 | #endif |
| 1210 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 1211 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1212 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 1213 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1214 | return( ret ); |
| 1215 | } |
| 1216 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 1217 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 1218 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 1219 | ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 1220 | { |
| 1221 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); |
| 1222 | return( ret ); |
| 1223 | } |
Hanno Becker | bc2498a | 2018-08-28 10:13:29 +0100 | [diff] [blame] | 1224 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 1225 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1226 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1227 | |
| 1228 | return( 0 ); |
| 1229 | } |
| 1230 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1231 | static int ssl_parse_renegotiation_info( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | e048b67 | 2013-07-19 12:47:00 +0200 | [diff] [blame] | 1232 | const unsigned char *buf, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1233 | size_t len ) |
| 1234 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1235 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1236 | if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1237 | { |
Manuel Pégourié-Gonnard | 31ff1d2 | 2013-10-28 13:46:11 +0100 | [diff] [blame] | 1238 | /* Check verify-data in constant-time. The length OTOH is no secret */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1239 | if( len != 1 + ssl->verify_data_len * 2 || |
| 1240 | buf[0] != ssl->verify_data_len * 2 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1241 | mbedtls_ssl_safer_memcmp( buf + 1, |
Manuel Pégourié-Gonnard | 31ff1d2 | 2013-10-28 13:46:11 +0100 | [diff] [blame] | 1242 | ssl->own_verify_data, ssl->verify_data_len ) != 0 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1243 | mbedtls_ssl_safer_memcmp( buf + 1 + ssl->verify_data_len, |
Manuel Pégourié-Gonnard | 31ff1d2 | 2013-10-28 13:46:11 +0100 | [diff] [blame] | 1244 | ssl->peer_verify_data, ssl->verify_data_len ) != 0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1245 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1246 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) ); |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1247 | mbedtls_ssl_send_alert_message( |
| 1248 | ssl, |
| 1249 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1250 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1251 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1252 | } |
| 1253 | } |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1254 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1255 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1256 | { |
| 1257 | if( len != 1 || buf[0] != 0x00 ) |
| 1258 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1259 | MBEDTLS_SSL_DEBUG_MSG( |
| 1260 | 1, ( "non-zero length renegotiation info" ) ); |
| 1261 | mbedtls_ssl_send_alert_message( |
| 1262 | ssl, |
| 1263 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1264 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1265 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1266 | } |
| 1267 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1268 | ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1269 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1270 | |
| 1271 | return( 0 ); |
| 1272 | } |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 1273 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1274 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 1275 | static int ssl_parse_max_fragment_length_ext( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | e048b67 | 2013-07-19 12:47:00 +0200 | [diff] [blame] | 1276 | const unsigned char *buf, |
Manuel Pégourié-Gonnard | de600e5 | 2013-07-17 10:14:38 +0200 | [diff] [blame] | 1277 | size_t len ) |
| 1278 | { |
| 1279 | /* |
| 1280 | * server should use the extension only if we did, |
| 1281 | * and if so the server's value should match ours (and len is always 1) |
| 1282 | */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1283 | if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE || |
Manuel Pégourié-Gonnard | de600e5 | 2013-07-17 10:14:38 +0200 | [diff] [blame] | 1284 | len != 1 || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1285 | buf[0] != ssl->conf->mfl_code ) |
Manuel Pégourié-Gonnard | de600e5 | 2013-07-17 10:14:38 +0200 | [diff] [blame] | 1286 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1287 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 1288 | ( "non-matching max fragment length extension" ) ); |
| 1289 | mbedtls_ssl_send_alert_message( |
| 1290 | ssl, |
| 1291 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1292 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1293 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Manuel Pégourié-Gonnard | de600e5 | 2013-07-17 10:14:38 +0200 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | return( 0 ); |
| 1297 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1298 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1299 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1300 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 1301 | 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] | 1302 | const unsigned char *buf, |
| 1303 | size_t len ) |
| 1304 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1305 | if( ssl->conf->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_DISABLED || |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 1306 | len != 0 ) |
| 1307 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1308 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 1309 | ( "non-matching truncated HMAC extension" ) ); |
| 1310 | mbedtls_ssl_send_alert_message( |
| 1311 | ssl, |
| 1312 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1313 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1314 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 1315 | } |
| 1316 | |
| 1317 | ((void) buf); |
| 1318 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1319 | ssl->session_negotiate->trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 1320 | |
| 1321 | return( 0 ); |
| 1322 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1323 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 1324 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1325 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 1326 | static int ssl_parse_cid_ext( mbedtls_ssl_context *ssl, |
| 1327 | const unsigned char *buf, |
| 1328 | size_t len ) |
| 1329 | { |
| 1330 | size_t peer_cid_len; |
| 1331 | |
| 1332 | if( /* CID extension only makes sense in DTLS */ |
| 1333 | ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
| 1334 | /* The server must only send the CID extension if we have offered it. */ |
Hanno Becker | 2262648 | 2019-05-03 12:46:59 +0100 | [diff] [blame] | 1335 | ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED ) |
Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 1336 | { |
Hanno Becker | 2262648 | 2019-05-03 12:46:59 +0100 | [diff] [blame] | 1337 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension unexpected" ) ); |
Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 1338 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
Hanno Becker | 2262648 | 2019-05-03 12:46:59 +0100 | [diff] [blame] | 1339 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
| 1340 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
| 1341 | } |
| 1342 | |
| 1343 | if( len == 0 ) |
| 1344 | { |
| 1345 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) ); |
| 1346 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1347 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 1348 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
| 1349 | } |
| 1350 | |
| 1351 | peer_cid_len = *buf++; |
| 1352 | len--; |
| 1353 | |
| 1354 | if( peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX ) |
| 1355 | { |
Hanno Becker | 2262648 | 2019-05-03 12:46:59 +0100 | [diff] [blame] | 1356 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) ); |
Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 1357 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
Hanno Becker | 2262648 | 2019-05-03 12:46:59 +0100 | [diff] [blame] | 1358 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 1359 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
| 1360 | } |
| 1361 | |
| 1362 | if( len != peer_cid_len ) |
| 1363 | { |
Hanno Becker | 2262648 | 2019-05-03 12:46:59 +0100 | [diff] [blame] | 1364 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) ); |
Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 1365 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
Hanno Becker | 2262648 | 2019-05-03 12:46:59 +0100 | [diff] [blame] | 1366 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 1367 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
| 1368 | } |
| 1369 | |
Hanno Becker | 5a29990 | 2019-05-03 12:47:49 +0100 | [diff] [blame] | 1370 | ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED; |
Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 1371 | ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len; |
| 1372 | memcpy( ssl->handshake->peer_cid, buf, peer_cid_len ); |
| 1373 | |
| 1374 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use of CID extension negotiated" ) ); |
| 1375 | MBEDTLS_SSL_DEBUG_BUF( 3, "Server CID", buf, peer_cid_len ); |
| 1376 | |
Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 1377 | return( 0 ); |
| 1378 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1379 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 1380 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1381 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 1382 | 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] | 1383 | const unsigned char *buf, |
| 1384 | size_t len ) |
| 1385 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1386 | if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1387 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 || |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 1388 | len != 0 ) |
| 1389 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1390 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 1391 | ( "non-matching encrypt-then-MAC extension" ) ); |
| 1392 | mbedtls_ssl_send_alert_message( |
| 1393 | ssl, |
| 1394 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1395 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1396 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | ((void) buf); |
| 1400 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1401 | ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 1402 | |
| 1403 | return( 0 ); |
| 1404 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1405 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 1406 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1407 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 1408 | 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] | 1409 | const unsigned char *buf, |
| 1410 | size_t len ) |
| 1411 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1412 | if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1413 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 || |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 1414 | len != 0 ) |
| 1415 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1416 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 1417 | ( "non-matching extended master secret extension" ) ); |
| 1418 | mbedtls_ssl_send_alert_message( |
| 1419 | ssl, |
| 1420 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1421 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1422 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 1423 | } |
| 1424 | |
| 1425 | ((void) buf); |
| 1426 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1427 | ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 1428 | |
| 1429 | return( 0 ); |
| 1430 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1431 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 1432 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1433 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 1434 | static int ssl_parse_session_ticket_ext( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 1435 | const unsigned char *buf, |
| 1436 | size_t len ) |
| 1437 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1438 | if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED || |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 1439 | len != 0 ) |
| 1440 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1441 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 1442 | ( "non-matching session ticket extension" ) ); |
| 1443 | mbedtls_ssl_send_alert_message( |
| 1444 | ssl, |
| 1445 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1446 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1447 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 1448 | } |
Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 1449 | |
| 1450 | ((void) buf); |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 1451 | |
| 1452 | ssl->handshake->new_session_ticket = 1; |
Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 1453 | |
| 1454 | return( 0 ); |
| 1455 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1456 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 1457 | |
Robert Cragie | 136884c | 2015-10-02 13:34:31 +0100 | [diff] [blame] | 1458 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 1459 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1460 | static int ssl_parse_supported_point_formats_ext( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 1461 | const unsigned char *buf, |
| 1462 | size_t len ) |
| 1463 | { |
| 1464 | size_t list_size; |
| 1465 | const unsigned char *p; |
| 1466 | |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 1467 | if( len == 0 || (size_t)( buf[0] + 1 ) != len ) |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 1468 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1469 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1470 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1471 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1472 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 1473 | } |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 1474 | list_size = buf[0]; |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 1475 | |
Manuel Pégourié-Gonnard | fd35af1 | 2014-06-23 14:10:13 +0200 | [diff] [blame] | 1476 | p = buf + 1; |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 1477 | while( list_size > 0 ) |
| 1478 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1479 | if( p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED || |
| 1480 | p[0] == MBEDTLS_ECP_PF_COMPRESSED ) |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 1481 | { |
Robert Cragie | 136884c | 2015-10-02 13:34:31 +0100 | [diff] [blame] | 1482 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) |
Manuel Pégourié-Gonnard | 5734b2d | 2013-08-15 19:04:02 +0200 | [diff] [blame] | 1483 | ssl->handshake->ecdh_ctx.point_format = p[0]; |
Gilles Peskine | 064a85c | 2017-05-10 10:46:40 +0200 | [diff] [blame] | 1484 | #endif |
Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 1485 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Robert Cragie | 136884c | 2015-10-02 13:34:31 +0100 | [diff] [blame] | 1486 | ssl->handshake->ecjpake_ctx.point_format = p[0]; |
| 1487 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1488 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) ); |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 1489 | return( 0 ); |
| 1490 | } |
| 1491 | |
| 1492 | list_size--; |
| 1493 | p++; |
| 1494 | } |
| 1495 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1496 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no point format in common" ) ); |
Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 1497 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1498 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1499 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 1500 | } |
Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 1501 | #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || |
Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 1502 | MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 1503 | |
Manuel Pégourié-Gonnard | 0a1324a | 2015-09-16 16:01:00 +0200 | [diff] [blame] | 1504 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 1505 | static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl, |
| 1506 | const unsigned char *buf, |
| 1507 | size_t len ) |
| 1508 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1509 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 0a1324a | 2015-09-16 16:01:00 +0200 | [diff] [blame] | 1510 | |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1511 | if( ssl->handshake->ciphersuite_info->key_exchange != |
Manuel Pégourié-Gonnard | 0a1324a | 2015-09-16 16:01:00 +0200 | [diff] [blame] | 1512 | MBEDTLS_KEY_EXCHANGE_ECJPAKE ) |
| 1513 | { |
| 1514 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) ); |
| 1515 | return( 0 ); |
| 1516 | } |
| 1517 | |
Manuel Pégourié-Gonnard | d0d8cb3 | 2015-09-17 14:16:30 +0200 | [diff] [blame] | 1518 | /* If we got here, we no longer need our cached extension */ |
| 1519 | mbedtls_free( ssl->handshake->ecjpake_cache ); |
| 1520 | ssl->handshake->ecjpake_cache = NULL; |
| 1521 | ssl->handshake->ecjpake_cache_len = 0; |
| 1522 | |
Manuel Pégourié-Gonnard | 0a1324a | 2015-09-16 16:01:00 +0200 | [diff] [blame] | 1523 | if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx, |
| 1524 | buf, len ) ) != 0 ) |
| 1525 | { |
| 1526 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret ); |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1527 | mbedtls_ssl_send_alert_message( |
| 1528 | ssl, |
| 1529 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1530 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 0a1324a | 2015-09-16 16:01:00 +0200 | [diff] [blame] | 1531 | return( ret ); |
| 1532 | } |
| 1533 | |
| 1534 | return( 0 ); |
| 1535 | } |
| 1536 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1537 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1538 | #if defined(MBEDTLS_SSL_ALPN) |
| 1539 | static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 1540 | const unsigned char *buf, size_t len ) |
| 1541 | { |
| 1542 | size_t list_len, name_len; |
| 1543 | const char **p; |
| 1544 | |
| 1545 | /* If we didn't send it, the server shouldn't send it */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1546 | if( ssl->conf->alpn_list == NULL ) |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1547 | { |
| 1548 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching ALPN extension" ) ); |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1549 | mbedtls_ssl_send_alert_message( |
| 1550 | ssl, |
| 1551 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1552 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1553 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1554 | } |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 1555 | |
| 1556 | /* |
| 1557 | * opaque ProtocolName<1..2^8-1>; |
| 1558 | * |
| 1559 | * struct { |
| 1560 | * ProtocolName protocol_name_list<2..2^16-1> |
| 1561 | * } ProtocolNameList; |
| 1562 | * |
| 1563 | * the "ProtocolNameList" MUST contain exactly one "ProtocolName" |
| 1564 | */ |
| 1565 | |
| 1566 | /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */ |
| 1567 | if( len < 4 ) |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1568 | { |
| 1569 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1570 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1571 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1572 | } |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 1573 | |
| 1574 | list_len = ( buf[0] << 8 ) | buf[1]; |
| 1575 | if( list_len != len - 2 ) |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1576 | { |
| 1577 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1578 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1579 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1580 | } |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 1581 | |
| 1582 | name_len = buf[2]; |
| 1583 | if( name_len != list_len - 1 ) |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1584 | { |
| 1585 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1586 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1587 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1588 | } |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 1589 | |
| 1590 | /* Check that the server chosen protocol was in our list and save it */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1591 | for( p = ssl->conf->alpn_list; *p != NULL; p++ ) |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 1592 | { |
| 1593 | if( name_len == strlen( *p ) && |
| 1594 | memcmp( buf + 3, *p, name_len ) == 0 ) |
| 1595 | { |
| 1596 | ssl->alpn_chosen = *p; |
| 1597 | return( 0 ); |
| 1598 | } |
| 1599 | } |
| 1600 | |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1601 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "ALPN extension: no matching protocol" ) ); |
Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 1602 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1603 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1604 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 1605 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1606 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 1607 | |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1608 | /* |
| 1609 | * Parse HelloVerifyRequest. Only called after verifying the HS type. |
| 1610 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1611 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 1612 | static int ssl_parse_hello_verify_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1613 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1614 | const unsigned char *p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1615 | int major_ver, minor_ver; |
| 1616 | unsigned char cookie_len; |
| 1617 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1618 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse hello verify request" ) ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1619 | |
Gilles Peskine | b64bf06 | 2019-09-27 14:02:44 +0200 | [diff] [blame] | 1620 | /* Check that there is enough room for: |
| 1621 | * - 2 bytes of version |
| 1622 | * - 1 byte of cookie_len |
| 1623 | */ |
| 1624 | if( mbedtls_ssl_hs_hdr_len( ssl ) + 3 > ssl->in_msglen ) |
| 1625 | { |
| 1626 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 1627 | ( "incoming HelloVerifyRequest message is too short" ) ); |
| 1628 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1629 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 1630 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
| 1631 | } |
| 1632 | |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1633 | /* |
| 1634 | * struct { |
| 1635 | * ProtocolVersion server_version; |
| 1636 | * opaque cookie<0..2^8-1>; |
| 1637 | * } HelloVerifyRequest; |
| 1638 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1639 | MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 ); |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1640 | mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, p ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1641 | p += 2; |
| 1642 | |
Manuel Pégourié-Gonnard | b35fe56 | 2014-08-09 17:00:46 +0200 | [diff] [blame] | 1643 | /* |
| 1644 | * Since the RFC is not clear on this point, accept DTLS 1.0 (TLS 1.1) |
| 1645 | * even is lower than our min version. |
| 1646 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1647 | if( major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 || |
| 1648 | minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1649 | major_ver > ssl->conf->max_major_ver || |
| 1650 | minor_ver > ssl->conf->max_minor_ver ) |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1651 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1652 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server version" ) ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1653 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1654 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1655 | MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1656 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1657 | return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1658 | } |
| 1659 | |
| 1660 | cookie_len = *p++; |
Andres AG | 5a87c93 | 2016-09-26 14:53:05 +0100 | [diff] [blame] | 1661 | if( ( ssl->in_msg + ssl->in_msglen ) - p < cookie_len ) |
| 1662 | { |
| 1663 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 1664 | ( "cookie length does not match incoming message size" ) ); |
| 1665 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1666 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 1667 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
| 1668 | } |
Gilles Peskine | b51130d | 2019-09-27 14:00:36 +0200 | [diff] [blame] | 1669 | MBEDTLS_SSL_DEBUG_BUF( 3, "cookie", p, cookie_len ); |
Andres AG | 5a87c93 | 2016-09-26 14:53:05 +0100 | [diff] [blame] | 1670 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1671 | mbedtls_free( ssl->handshake->verify_cookie ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1672 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 1673 | ssl->handshake->verify_cookie = mbedtls_calloc( 1, cookie_len ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1674 | if( ssl->handshake->verify_cookie == NULL ) |
| 1675 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 1676 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", cookie_len ) ); |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1677 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1678 | } |
| 1679 | |
| 1680 | memcpy( ssl->handshake->verify_cookie, p, cookie_len ); |
| 1681 | ssl->handshake->verify_cookie_len = cookie_len; |
| 1682 | |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 1683 | /* Start over at ClientHello */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1684 | ssl->state = MBEDTLS_SSL_CLIENT_HELLO; |
| 1685 | mbedtls_ssl_reset_checksum( ssl ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1686 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1687 | mbedtls_ssl_recv_flight_completed( ssl ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 1688 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1689 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse hello verify request" ) ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1690 | |
| 1691 | return( 0 ); |
| 1692 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1693 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1694 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1695 | static int ssl_parse_server_hello( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1696 | { |
Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1697 | int ret, i; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 1698 | size_t n; |
Manuel Pégourié-Gonnard | f7cdbc0 | 2014-10-17 17:02:10 +0200 | [diff] [blame] | 1699 | size_t ext_len; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1700 | unsigned char *buf, *ext; |
Manuel Pégourié-Gonnard | 1cf7b30 | 2015-06-24 22:28:19 +0200 | [diff] [blame] | 1701 | unsigned char comp; |
| 1702 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
| 1703 | int accept_comp; |
| 1704 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1705 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1706 | int renegotiation_info_seen = 0; |
Manuel Pégourié-Gonnard | eaecbd3 | 2014-11-06 02:38:02 +0100 | [diff] [blame] | 1707 | #endif |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1708 | int handshake_failure = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1709 | const mbedtls_ssl_ciphersuite_t *suite_info; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1710 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1711 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1712 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 1713 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1714 | { |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1715 | /* No alert on a read error. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1716 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1717 | return( ret ); |
| 1718 | } |
| 1719 | |
Hanno Becker | 79594fd | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 1720 | buf = ssl->in_msg; |
| 1721 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1722 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1723 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1724 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1725 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | 6591962 | 2014-08-19 12:50:30 +0200 | [diff] [blame] | 1726 | { |
Manuel Pégourié-Gonnard | 44ade65 | 2014-08-19 13:58:40 +0200 | [diff] [blame] | 1727 | ssl->renego_records_seen++; |
| 1728 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1729 | if( ssl->conf->renego_max_records >= 0 && |
| 1730 | ssl->renego_records_seen > ssl->conf->renego_max_records ) |
Manuel Pégourié-Gonnard | 44ade65 | 2014-08-19 13:58:40 +0200 | [diff] [blame] | 1731 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1732 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 1733 | ( "renegotiation requested, but not honored by server" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1734 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 44ade65 | 2014-08-19 13:58:40 +0200 | [diff] [blame] | 1735 | } |
| 1736 | |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1737 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 1738 | ( "non-handshake message during renegotiation" ) ); |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 1739 | |
| 1740 | ssl->keep_current_message = 1; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1741 | return( MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO ); |
Manuel Pégourié-Gonnard | 6591962 | 2014-08-19 12:50:30 +0200 | [diff] [blame] | 1742 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1743 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 6591962 | 2014-08-19 12:50:30 +0200 | [diff] [blame] | 1744 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1745 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1746 | mbedtls_ssl_send_alert_message( |
| 1747 | ssl, |
| 1748 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1749 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1750 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1751 | } |
| 1752 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1753 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1754 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1755 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1756 | if( buf[0] == MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST ) |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1757 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1758 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "received hello verify request" ) ); |
| 1759 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1760 | return( ssl_parse_hello_verify_request( ssl ) ); |
| 1761 | } |
| 1762 | else |
| 1763 | { |
| 1764 | /* We made it through the verification process */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1765 | mbedtls_free( ssl->handshake->verify_cookie ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1766 | ssl->handshake->verify_cookie = NULL; |
| 1767 | ssl->handshake->verify_cookie_len = 0; |
| 1768 | } |
| 1769 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1770 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1771 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1772 | if( ssl->in_hslen < 38 + mbedtls_ssl_hs_hdr_len( ssl ) || |
| 1773 | buf[0] != MBEDTLS_SSL_HS_SERVER_HELLO ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1774 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1775 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1776 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1777 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1778 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1779 | } |
| 1780 | |
Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 1781 | /* |
| 1782 | * 0 . 1 server_version |
| 1783 | * 2 . 33 random (maybe including 4 bytes of Unix time) |
| 1784 | * 34 . 34 session_id length = n |
| 1785 | * 35 . 34+n session_id |
| 1786 | * 35+n . 36+n cipher_suite |
| 1787 | * 37+n . 37+n compression_method |
| 1788 | * |
| 1789 | * 38+n . 39+n extensions length (optional) |
| 1790 | * 40+n . .. extensions |
| 1791 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1792 | buf += mbedtls_ssl_hs_hdr_len( ssl ); |
Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 1793 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1794 | MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", buf + 0, 2 ); |
| 1795 | mbedtls_ssl_read_version( &ssl->major_ver, &ssl->minor_ver, |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1796 | ssl->conf->transport, buf + 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1797 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1798 | if( ssl->major_ver < ssl->conf->min_major_ver || |
| 1799 | ssl->minor_ver < ssl->conf->min_minor_ver || |
| 1800 | ssl->major_ver > ssl->conf->max_major_ver || |
| 1801 | ssl->minor_ver > ssl->conf->max_minor_ver ) |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 1802 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1803 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 1804 | ( "server version out of bounds - min: [%d:%d], server: [%d:%d], max: [%d:%d]", |
| 1805 | ssl->conf->min_major_ver, |
| 1806 | ssl->conf->min_minor_ver, |
| 1807 | ssl->major_ver, ssl->minor_ver, |
| 1808 | ssl->conf->max_major_ver, |
| 1809 | ssl->conf->max_minor_ver ) ); |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 1810 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1811 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1812 | MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION ); |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 1813 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1814 | return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION ); |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 1815 | } |
| 1816 | |
Andres Amaya Garcia | 6bce9cb | 2017-09-06 15:33:34 +0100 | [diff] [blame] | 1817 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", |
| 1818 | ( (uint32_t) buf[2] << 24 ) | |
| 1819 | ( (uint32_t) buf[3] << 16 ) | |
| 1820 | ( (uint32_t) buf[4] << 8 ) | |
| 1821 | ( (uint32_t) buf[5] ) ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1822 | |
Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 1823 | memcpy( ssl->handshake->randbytes + 32, buf + 2, 32 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1824 | |
Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 1825 | n = buf[34]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1826 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1827 | MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 2, 32 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1828 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1829 | if( n > 32 ) |
| 1830 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1831 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1832 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1833 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1834 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1835 | } |
| 1836 | |
Manuel Pégourié-Gonnard | a6e5bd5 | 2015-07-23 12:14:13 +0200 | [diff] [blame] | 1837 | if( ssl->in_hslen > mbedtls_ssl_hs_hdr_len( ssl ) + 39 + n ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1838 | { |
Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 1839 | ext_len = ( ( buf[38 + n] << 8 ) |
| 1840 | | ( buf[39 + n] ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1841 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1842 | if( ( ext_len > 0 && ext_len < 4 ) || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1843 | ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 40 + n + ext_len ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1844 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1845 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1846 | mbedtls_ssl_send_alert_message( |
| 1847 | ssl, |
| 1848 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1849 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1850 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1851 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1852 | } |
Manuel Pégourié-Gonnard | a6e5bd5 | 2015-07-23 12:14:13 +0200 | [diff] [blame] | 1853 | else if( ssl->in_hslen == mbedtls_ssl_hs_hdr_len( ssl ) + 38 + n ) |
Manuel Pégourié-Gonnard | f7cdbc0 | 2014-10-17 17:02:10 +0200 | [diff] [blame] | 1854 | { |
| 1855 | ext_len = 0; |
| 1856 | } |
| 1857 | else |
| 1858 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1859 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1860 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1861 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1862 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Manuel Pégourié-Gonnard | f7cdbc0 | 2014-10-17 17:02:10 +0200 | [diff] [blame] | 1863 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1864 | |
Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1865 | /* ciphersuite (used later) */ |
Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 1866 | i = ( buf[35 + n] << 8 ) | buf[36 + n]; |
Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1867 | |
| 1868 | /* |
| 1869 | * Read and check compression |
| 1870 | */ |
Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 1871 | comp = buf[37 + n]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1872 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1873 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1874 | /* See comments in ssl_write_client_hello() */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1875 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1876 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1877 | accept_comp = 0; |
Manuel Pégourié-Gonnard | 1cf7b30 | 2015-06-24 22:28:19 +0200 | [diff] [blame] | 1878 | else |
Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1879 | #endif |
Manuel Pégourié-Gonnard | 1cf7b30 | 2015-06-24 22:28:19 +0200 | [diff] [blame] | 1880 | accept_comp = 1; |
Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1881 | |
Manuel Pégourié-Gonnard | 1cf7b30 | 2015-06-24 22:28:19 +0200 | [diff] [blame] | 1882 | if( comp != MBEDTLS_SSL_COMPRESS_NULL && |
| 1883 | ( comp != MBEDTLS_SSL_COMPRESS_DEFLATE || accept_comp == 0 ) ) |
| 1884 | #else /* MBEDTLS_ZLIB_SUPPORT */ |
| 1885 | if( comp != MBEDTLS_SSL_COMPRESS_NULL ) |
| 1886 | #endif/* MBEDTLS_ZLIB_SUPPORT */ |
Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1887 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1888 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 1889 | ( "server hello, bad compression: %d", comp ) ); |
| 1890 | mbedtls_ssl_send_alert_message( |
| 1891 | ssl, |
| 1892 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1893 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1894 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1895 | } |
| 1896 | |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1897 | /* |
| 1898 | * Initialize update checksum functions |
| 1899 | */ |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1900 | ssl->handshake->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( i ); |
| 1901 | if( ssl->handshake->ciphersuite_info == NULL ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1902 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1903 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 1904 | ( "ciphersuite info for %04x not found", i ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1905 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1906 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1907 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1908 | } |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1909 | |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1910 | mbedtls_ssl_optimize_checksum( ssl, ssl->handshake->ciphersuite_info ); |
Manuel Pégourié-Gonnard | 3c599f1 | 2014-03-10 13:25:07 +0100 | [diff] [blame] | 1911 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1912 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) ); |
| 1913 | MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 35, n ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1914 | |
| 1915 | /* |
| 1916 | * Check if the session can be resumed |
| 1917 | */ |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1918 | if( ssl->handshake->resume == 0 || n == 0 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1919 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1920 | ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE || |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1921 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1922 | ssl->session_negotiate->ciphersuite != i || |
| 1923 | ssl->session_negotiate->compression != comp || |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 1924 | ssl->session_negotiate->id_len != n || |
Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 1925 | memcmp( ssl->session_negotiate->id, buf + 35, n ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1926 | { |
| 1927 | ssl->state++; |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 1928 | ssl->handshake->resume = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1929 | #if defined(MBEDTLS_HAVE_TIME) |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 1930 | ssl->session_negotiate->start = mbedtls_time( NULL ); |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 1931 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1932 | ssl->session_negotiate->ciphersuite = i; |
| 1933 | ssl->session_negotiate->compression = comp; |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 1934 | ssl->session_negotiate->id_len = n; |
Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 1935 | memcpy( ssl->session_negotiate->id, buf + 35, n ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1936 | } |
| 1937 | else |
| 1938 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1939 | ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC; |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 1940 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1941 | if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 ) |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 1942 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1943 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret ); |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1944 | mbedtls_ssl_send_alert_message( |
| 1945 | ssl, |
| 1946 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1947 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 1948 | return( ret ); |
| 1949 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1950 | } |
| 1951 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1952 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed", |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 1953 | ssl->handshake->resume ? "a" : "no" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1954 | |
Manuel Pégourié-Gonnard | 60884a1 | 2015-09-16 11:13:41 +0200 | [diff] [blame] | 1955 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %04x", i ) ); |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1956 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d", |
| 1957 | buf[37 + n] ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1958 | |
Andrzej Kurek | 03bac44 | 2018-04-25 05:06:07 -0400 | [diff] [blame] | 1959 | /* |
| 1960 | * Perform cipher suite validation in same way as in ssl_write_client_hello. |
Mohammad Azim Khan | 1d3b508 | 2018-04-18 19:35:00 +0100 | [diff] [blame] | 1961 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1962 | i = 0; |
| 1963 | while( 1 ) |
| 1964 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1965 | if( ssl->conf->ciphersuite_list[ssl->minor_ver][i] == 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1966 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1967 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1968 | mbedtls_ssl_send_alert_message( |
| 1969 | ssl, |
| 1970 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1971 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1972 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1973 | } |
| 1974 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1975 | if( ssl->conf->ciphersuite_list[ssl->minor_ver][i++] == |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 1976 | ssl->session_negotiate->ciphersuite ) |
| 1977 | { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1978 | break; |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 1979 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1980 | } |
| 1981 | |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1982 | suite_info = mbedtls_ssl_ciphersuite_from_id( |
| 1983 | ssl->session_negotiate->ciphersuite ); |
| 1984 | if( ssl_validate_ciphersuite( suite_info, ssl, ssl->minor_ver, |
| 1985 | ssl->minor_ver ) != 0 ) |
Mohammad Azim Khan | 1d3b508 | 2018-04-18 19:35:00 +0100 | [diff] [blame] | 1986 | { |
| 1987 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1988 | mbedtls_ssl_send_alert_message( |
| 1989 | ssl, |
| 1990 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1991 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Mohammad Azim Khan | 1d3b508 | 2018-04-18 19:35:00 +0100 | [diff] [blame] | 1992 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
| 1993 | } |
| 1994 | |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1995 | MBEDTLS_SSL_DEBUG_MSG( 3, |
| 1996 | ( "server hello, chosen ciphersuite: %s", suite_info->name ) ); |
Mohammad Azim Khan | 1d3b508 | 2018-04-18 19:35:00 +0100 | [diff] [blame] | 1997 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1998 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 1999 | if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA && |
| 2000 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
| 2001 | { |
| 2002 | ssl->handshake->ecrs_enabled = 1; |
| 2003 | } |
| 2004 | #endif |
| 2005 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2006 | if( comp != MBEDTLS_SSL_COMPRESS_NULL |
| 2007 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
| 2008 | && comp != MBEDTLS_SSL_COMPRESS_DEFLATE |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2009 | #endif |
| 2010 | ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2011 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2012 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2013 | mbedtls_ssl_send_alert_message( |
| 2014 | ssl, |
| 2015 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2016 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2017 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2018 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2019 | ssl->session_negotiate->compression = comp; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2020 | |
Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 2021 | ext = buf + 40 + n; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2022 | |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2023 | MBEDTLS_SSL_DEBUG_MSG( 2, |
| 2024 | ( "server hello, total extension length: %d", ext_len ) ); |
Manuel Pégourié-Gonnard | a052849 | 2013-07-16 17:26:28 +0200 | [diff] [blame] | 2025 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2026 | while( ext_len ) |
| 2027 | { |
| 2028 | unsigned int ext_id = ( ( ext[0] << 8 ) |
| 2029 | | ( ext[1] ) ); |
| 2030 | unsigned int ext_size = ( ( ext[2] << 8 ) |
| 2031 | | ( ext[3] ) ); |
| 2032 | |
| 2033 | if( ext_size + 4 > ext_len ) |
| 2034 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2035 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2036 | mbedtls_ssl_send_alert_message( |
| 2037 | ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2038 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2039 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2040 | } |
| 2041 | |
| 2042 | switch( ext_id ) |
| 2043 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2044 | case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO: |
| 2045 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) ); |
| 2046 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2047 | renegotiation_info_seen = 1; |
Manuel Pégourié-Gonnard | eaecbd3 | 2014-11-06 02:38:02 +0100 | [diff] [blame] | 2048 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2049 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 2050 | if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4, |
| 2051 | ext_size ) ) != 0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2052 | return( ret ); |
| 2053 | |
| 2054 | break; |
| 2055 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2056 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 2057 | case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH: |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2058 | MBEDTLS_SSL_DEBUG_MSG( 3, |
| 2059 | ( "found max_fragment_length extension" ) ); |
Manuel Pégourié-Gonnard | de600e5 | 2013-07-17 10:14:38 +0200 | [diff] [blame] | 2060 | |
| 2061 | if( ( ret = ssl_parse_max_fragment_length_ext( ssl, |
| 2062 | ext + 4, ext_size ) ) != 0 ) |
| 2063 | { |
| 2064 | return( ret ); |
| 2065 | } |
| 2066 | |
| 2067 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2068 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | de600e5 | 2013-07-17 10:14:38 +0200 | [diff] [blame] | 2069 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2070 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 2071 | case MBEDTLS_TLS_EXT_TRUNCATED_HMAC: |
| 2072 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found truncated_hmac extension" ) ); |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 2073 | |
| 2074 | if( ( ret = ssl_parse_truncated_hmac_ext( ssl, |
| 2075 | ext + 4, ext_size ) ) != 0 ) |
| 2076 | { |
| 2077 | return( ret ); |
| 2078 | } |
| 2079 | |
| 2080 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2081 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 2082 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2083 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 2084 | case MBEDTLS_TLS_EXT_CID: |
| 2085 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found CID extension" ) ); |
| 2086 | |
| 2087 | if( ( ret = ssl_parse_cid_ext( ssl, |
| 2088 | ext + 4, |
| 2089 | ext_size ) ) != 0 ) |
| 2090 | { |
| 2091 | return( ret ); |
| 2092 | } |
| 2093 | |
| 2094 | break; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2095 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 2096 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2097 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 2098 | case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC: |
| 2099 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt_then_mac extension" ) ); |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2100 | |
| 2101 | if( ( ret = ssl_parse_encrypt_then_mac_ext( ssl, |
| 2102 | ext + 4, ext_size ) ) != 0 ) |
| 2103 | { |
| 2104 | return( ret ); |
| 2105 | } |
| 2106 | |
| 2107 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2108 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2109 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2110 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 2111 | case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET: |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2112 | MBEDTLS_SSL_DEBUG_MSG( 3, |
| 2113 | ( "found extended_master_secret extension" ) ); |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2114 | |
| 2115 | if( ( ret = ssl_parse_extended_ms_ext( ssl, |
| 2116 | ext + 4, ext_size ) ) != 0 ) |
| 2117 | { |
| 2118 | return( ret ); |
| 2119 | } |
| 2120 | |
| 2121 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2122 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2123 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2124 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 2125 | case MBEDTLS_TLS_EXT_SESSION_TICKET: |
| 2126 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) ); |
Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 2127 | |
| 2128 | if( ( ret = ssl_parse_session_ticket_ext( ssl, |
| 2129 | ext + 4, ext_size ) ) != 0 ) |
| 2130 | { |
| 2131 | return( ret ); |
| 2132 | } |
| 2133 | |
| 2134 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2135 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 2136 | |
Robert Cragie | 136884c | 2015-10-02 13:34:31 +0100 | [diff] [blame] | 2137 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 2138 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2139 | case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS: |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2140 | MBEDTLS_SSL_DEBUG_MSG( 3, |
| 2141 | ( "found supported_point_formats extension" ) ); |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 2142 | |
| 2143 | if( ( ret = ssl_parse_supported_point_formats_ext( ssl, |
| 2144 | ext + 4, ext_size ) ) != 0 ) |
| 2145 | { |
| 2146 | return( ret ); |
| 2147 | } |
| 2148 | |
| 2149 | break; |
Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 2150 | #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || |
| 2151 | MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 2152 | |
Manuel Pégourié-Gonnard | 0a1324a | 2015-09-16 16:01:00 +0200 | [diff] [blame] | 2153 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 2154 | case MBEDTLS_TLS_EXT_ECJPAKE_KKPP: |
| 2155 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake_kkpp extension" ) ); |
| 2156 | |
| 2157 | if( ( ret = ssl_parse_ecjpake_kkpp( ssl, |
| 2158 | ext + 4, ext_size ) ) != 0 ) |
| 2159 | { |
| 2160 | return( ret ); |
| 2161 | } |
| 2162 | |
| 2163 | break; |
| 2164 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2165 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2166 | #if defined(MBEDTLS_SSL_ALPN) |
| 2167 | case MBEDTLS_TLS_EXT_ALPN: |
| 2168 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) ); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2169 | |
| 2170 | if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 ) |
| 2171 | return( ret ); |
| 2172 | |
| 2173 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2174 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2175 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2176 | default: |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2177 | MBEDTLS_SSL_DEBUG_MSG( 3, |
| 2178 | ( "unknown extension found: %d (ignoring)", ext_id ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2179 | } |
| 2180 | |
| 2181 | ext_len -= 4 + ext_size; |
| 2182 | ext += 4 + ext_size; |
| 2183 | |
| 2184 | if( ext_len > 0 && ext_len < 4 ) |
| 2185 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2186 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
| 2187 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2188 | } |
| 2189 | } |
| 2190 | |
| 2191 | /* |
| 2192 | * Renegotiation security checks |
| 2193 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2194 | if( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2195 | ssl->conf->allow_legacy_renegotiation == |
| 2196 | MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2197 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2198 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 2199 | ( "legacy renegotiation, breaking off handshake" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2200 | handshake_failure = 1; |
| 2201 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2202 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 2203 | else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && |
| 2204 | ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION && |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2205 | renegotiation_info_seen == 0 ) |
| 2206 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2207 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 2208 | ( "renegotiation_info extension missing (secure)" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2209 | handshake_failure = 1; |
| 2210 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2211 | else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && |
| 2212 | ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2213 | ssl->conf->allow_legacy_renegotiation == |
| 2214 | MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2215 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2216 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) ); |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2217 | handshake_failure = 1; |
| 2218 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2219 | else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && |
| 2220 | ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2221 | renegotiation_info_seen == 1 ) |
| 2222 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2223 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 2224 | ( "renegotiation_info extension present (legacy)" ) ); |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2225 | handshake_failure = 1; |
| 2226 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2227 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2228 | |
| 2229 | if( handshake_failure == 1 ) |
| 2230 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2231 | mbedtls_ssl_send_alert_message( |
| 2232 | ssl, |
| 2233 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2234 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2235 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2236 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2237 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2238 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2239 | |
| 2240 | return( 0 ); |
| 2241 | } |
| 2242 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2243 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \ |
| 2244 | defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2245 | static int ssl_parse_server_dh_params( mbedtls_ssl_context *ssl, |
| 2246 | unsigned char **p, |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2247 | unsigned char *end ) |
| 2248 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2249 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2250 | |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2251 | /* |
| 2252 | * Ephemeral DH parameters: |
| 2253 | * |
| 2254 | * struct { |
| 2255 | * opaque dh_p<1..2^16-1>; |
| 2256 | * opaque dh_g<1..2^16-1>; |
| 2257 | * opaque dh_Ys<1..2^16-1>; |
| 2258 | * } ServerDHParams; |
| 2259 | */ |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2260 | if( ( ret = mbedtls_dhm_read_params( &ssl->handshake->dhm_ctx, |
| 2261 | p, end ) ) != 0 ) |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2262 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2263 | MBEDTLS_SSL_DEBUG_RET( 2, ( "mbedtls_dhm_read_params" ), ret ); |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2264 | return( ret ); |
| 2265 | } |
| 2266 | |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 2267 | if( ssl->handshake->dhm_ctx.len * 8 < ssl->conf->dhm_min_bitlen ) |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2268 | { |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 2269 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DHM prime too short: %d < %d", |
| 2270 | ssl->handshake->dhm_ctx.len * 8, |
| 2271 | ssl->conf->dhm_min_bitlen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2272 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2273 | } |
| 2274 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2275 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P ); |
| 2276 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G ); |
| 2277 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY ); |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2278 | |
| 2279 | return( ret ); |
| 2280 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2281 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED || |
| 2282 | MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2283 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2284 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ |
| 2285 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \ |
| 2286 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \ |
| 2287 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ |
| 2288 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) |
| 2289 | static int ssl_check_server_ecdh_params( const mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2290 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2291 | const mbedtls_ecp_curve_info *curve_info; |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 2292 | mbedtls_ecp_group_id grp_id; |
| 2293 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
| 2294 | grp_id = ssl->handshake->ecdh_ctx.grp.id; |
| 2295 | #else |
| 2296 | grp_id = ssl->handshake->ecdh_ctx.grp_id; |
| 2297 | #endif |
Manuel Pégourié-Gonnard | c3f6b62c | 2014-02-06 10:13:09 +0100 | [diff] [blame] | 2298 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 2299 | curve_info = mbedtls_ecp_curve_info_from_grp_id( grp_id ); |
Manuel Pégourié-Gonnard | c3f6b62c | 2014-02-06 10:13:09 +0100 | [diff] [blame] | 2300 | if( curve_info == NULL ) |
| 2301 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2302 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2303 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | c3f6b62c | 2014-02-06 10:13:09 +0100 | [diff] [blame] | 2304 | } |
| 2305 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2306 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) ); |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2307 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 2308 | #if defined(MBEDTLS_ECP_C) |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 2309 | if( mbedtls_ssl_check_curve( ssl, grp_id ) != 0 ) |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 2310 | #else |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2311 | if( ssl->handshake->ecdh_ctx.grp.nbits < 163 || |
| 2312 | ssl->handshake->ecdh_ctx.grp.nbits > 521 ) |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 2313 | #endif |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2314 | return( -1 ); |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2315 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 2316 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 2317 | MBEDTLS_DEBUG_ECDH_QP ); |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2318 | |
| 2319 | return( 0 ); |
| 2320 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2321 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || |
| 2322 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED || |
| 2323 | MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED || |
| 2324 | MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED || |
| 2325 | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */ |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2326 | |
Hanno Becker | bb89e27 | 2019-01-08 12:54:37 +0000 | [diff] [blame] | 2327 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 2328 | ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ |
| 2329 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) ) |
| 2330 | static int ssl_parse_server_ecdh_params_psa( mbedtls_ssl_context *ssl, |
| 2331 | unsigned char **p, |
| 2332 | unsigned char *end ) |
| 2333 | { |
| 2334 | uint16_t tls_id; |
Gilles Peskine | 4245980 | 2019-12-19 13:31:53 +0100 | [diff] [blame] | 2335 | size_t ecdh_bits = 0; |
Hanno Becker | bb89e27 | 2019-01-08 12:54:37 +0000 | [diff] [blame] | 2336 | uint8_t ecpoint_len; |
| 2337 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
| 2338 | |
| 2339 | /* |
| 2340 | * Parse ECC group |
| 2341 | */ |
| 2342 | |
| 2343 | if( end - *p < 4 ) |
| 2344 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
| 2345 | |
| 2346 | /* First byte is curve_type; only named_curve is handled */ |
| 2347 | if( *(*p)++ != MBEDTLS_ECP_TLS_NAMED_CURVE ) |
| 2348 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
| 2349 | |
| 2350 | /* Next two bytes are the namedcurve value */ |
| 2351 | tls_id = *(*p)++; |
| 2352 | tls_id <<= 8; |
| 2353 | tls_id |= *(*p)++; |
| 2354 | |
| 2355 | /* Convert EC group to PSA key type. */ |
Gilles Peskine | 4245980 | 2019-12-19 13:31:53 +0100 | [diff] [blame] | 2356 | if( ( handshake->ecdh_psa_type = |
| 2357 | mbedtls_psa_parse_tls_ecc_group( tls_id, &ecdh_bits ) ) == 0 ) |
Hanno Becker | bb89e27 | 2019-01-08 12:54:37 +0000 | [diff] [blame] | 2358 | { |
| 2359 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
| 2360 | } |
Gilles Peskine | 4245980 | 2019-12-19 13:31:53 +0100 | [diff] [blame] | 2361 | if( ecdh_bits > 0xffff ) |
| 2362 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
| 2363 | handshake->ecdh_bits = (uint16_t) ecdh_bits; |
Hanno Becker | bb89e27 | 2019-01-08 12:54:37 +0000 | [diff] [blame] | 2364 | |
| 2365 | /* |
| 2366 | * Put peer's ECDH public key in the format understood by PSA. |
| 2367 | */ |
| 2368 | |
| 2369 | ecpoint_len = *(*p)++; |
| 2370 | if( (size_t)( end - *p ) < ecpoint_len ) |
| 2371 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
| 2372 | |
Gilles Peskine | 4245980 | 2019-12-19 13:31:53 +0100 | [diff] [blame] | 2373 | if( mbedtls_psa_tls_ecpoint_to_psa_ec( |
Hanno Becker | bb89e27 | 2019-01-08 12:54:37 +0000 | [diff] [blame] | 2374 | *p, ecpoint_len, |
| 2375 | handshake->ecdh_psa_peerkey, |
| 2376 | sizeof( handshake->ecdh_psa_peerkey ), |
| 2377 | &handshake->ecdh_psa_peerkey_len ) != 0 ) |
| 2378 | { |
| 2379 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 2380 | } |
| 2381 | |
| 2382 | *p += ecpoint_len; |
| 2383 | return( 0 ); |
| 2384 | } |
| 2385 | #endif /* MBEDTLS_USE_PSA_CRYPTO && |
| 2386 | ( MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || |
| 2387 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ) */ |
| 2388 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2389 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ |
| 2390 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \ |
| 2391 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
| 2392 | static int ssl_parse_server_ecdh_params( mbedtls_ssl_context *ssl, |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2393 | unsigned char **p, |
| 2394 | unsigned char *end ) |
| 2395 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2396 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2397 | |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2398 | /* |
| 2399 | * Ephemeral ECDH parameters: |
| 2400 | * |
| 2401 | * struct { |
| 2402 | * ECParameters curve_params; |
| 2403 | * ECPoint public; |
| 2404 | * } ServerECDHParams; |
| 2405 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2406 | if( ( ret = mbedtls_ecdh_read_params( &ssl->handshake->ecdh_ctx, |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2407 | (const unsigned char **) p, end ) ) != 0 ) |
| 2408 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2409 | MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_read_params" ), ret ); |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2410 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 1c1c20e | 2018-09-12 10:34:43 +0200 | [diff] [blame] | 2411 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 2412 | ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS; |
Manuel Pégourié-Gonnard | 558da9c | 2018-06-13 12:02:12 +0200 | [diff] [blame] | 2413 | #endif |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2414 | return( ret ); |
| 2415 | } |
| 2416 | |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2417 | if( ssl_check_server_ecdh_params( ssl ) != 0 ) |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2418 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2419 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 2420 | ( "bad server key exchange message (ECDHE curve)" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2421 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2422 | } |
| 2423 | |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2424 | return( ret ); |
| 2425 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2426 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || |
| 2427 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED || |
| 2428 | MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2429 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2430 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2431 | static int ssl_parse_server_psk_hint( mbedtls_ssl_context *ssl, |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2432 | unsigned char **p, |
| 2433 | unsigned char *end ) |
| 2434 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2435 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
irwir | 6527bd6 | 2019-09-21 18:51:25 +0300 | [diff] [blame] | 2436 | uint16_t len; |
Paul Bakker | c5a79cc | 2013-06-26 15:08:35 +0200 | [diff] [blame] | 2437 | ((void) ssl); |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2438 | |
| 2439 | /* |
| 2440 | * PSK parameters: |
| 2441 | * |
| 2442 | * opaque psk_identity_hint<0..2^16-1>; |
| 2443 | */ |
Hanno Becker | 0c161d1 | 2018-10-08 13:40:50 +0100 | [diff] [blame] | 2444 | if( end - (*p) < 2 ) |
Krzysztof Stachowiak | 740b218 | 2018-03-13 11:31:14 +0100 | [diff] [blame] | 2445 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2446 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 2447 | ( "bad server key exchange message (psk_identity_hint length)" ) ); |
Krzysztof Stachowiak | 740b218 | 2018-03-13 11:31:14 +0100 | [diff] [blame] | 2448 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
| 2449 | } |
Manuel Pégourié-Gonnard | 59b9fe2 | 2013-10-15 11:55:33 +0200 | [diff] [blame] | 2450 | len = (*p)[0] << 8 | (*p)[1]; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2451 | *p += 2; |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2452 | |
irwir | 6527bd6 | 2019-09-21 18:51:25 +0300 | [diff] [blame] | 2453 | if( end - (*p) < len ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2454 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2455 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 2456 | ( "bad server key exchange message (psk_identity_hint length)" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2457 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2458 | } |
| 2459 | |
Manuel Pégourié-Gonnard | 9d62412 | 2016-02-22 11:10:14 +0100 | [diff] [blame] | 2460 | /* |
| 2461 | * Note: we currently ignore the PKS identity hint, as we only allow one |
| 2462 | * PSK to be provisionned on the client. This could be changed later if |
| 2463 | * someone needs that feature. |
| 2464 | */ |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2465 | *p += len; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2466 | ret = 0; |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2467 | |
| 2468 | return( ret ); |
| 2469 | } |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2470 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2471 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2472 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \ |
| 2473 | defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2474 | /* |
| 2475 | * Generate a pre-master secret and encrypt it with the server's RSA key |
| 2476 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2477 | static int ssl_write_encrypted_pms( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2478 | size_t offset, size_t *olen, |
| 2479 | size_t pms_offset ) |
| 2480 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2481 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2482 | size_t len_bytes = ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ? 0 : 2; |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2483 | unsigned char *p = ssl->handshake->premaster + pms_offset; |
Hanno Becker | c7d7e29 | 2019-02-06 16:49:54 +0000 | [diff] [blame] | 2484 | mbedtls_pk_context * peer_pk; |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2485 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2486 | if( offset + len_bytes > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 2487 | { |
| 2488 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small for encrypted pms" ) ); |
| 2489 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2490 | } |
| 2491 | |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2492 | /* |
| 2493 | * Generate (part of) the pre-master as |
| 2494 | * struct { |
| 2495 | * ProtocolVersion client_version; |
| 2496 | * opaque random[46]; |
| 2497 | * } PreMasterSecret; |
| 2498 | */ |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2499 | mbedtls_ssl_write_version( ssl->conf->max_major_ver, |
| 2500 | ssl->conf->max_minor_ver, |
| 2501 | ssl->conf->transport, p ); |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2502 | |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 2503 | if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p + 2, 46 ) ) != 0 ) |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2504 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2505 | MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret ); |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2506 | return( ret ); |
| 2507 | } |
| 2508 | |
| 2509 | ssl->handshake->pmslen = 48; |
| 2510 | |
Hanno Becker | c7d7e29 | 2019-02-06 16:49:54 +0000 | [diff] [blame] | 2511 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 2512 | peer_pk = &ssl->handshake->peer_pubkey; |
| 2513 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | 7f2f062 | 2015-09-03 10:44:32 +0200 | [diff] [blame] | 2514 | if( ssl->session_negotiate->peer_cert == NULL ) |
| 2515 | { |
Hanno Becker | 8273df8 | 2019-02-06 17:37:32 +0000 | [diff] [blame] | 2516 | /* Should never happen */ |
Hanno Becker | 62d58ed | 2019-02-26 11:51:06 +0000 | [diff] [blame] | 2517 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Hanno Becker | 8273df8 | 2019-02-06 17:37:32 +0000 | [diff] [blame] | 2518 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7f2f062 | 2015-09-03 10:44:32 +0200 | [diff] [blame] | 2519 | } |
Hanno Becker | c7d7e29 | 2019-02-06 16:49:54 +0000 | [diff] [blame] | 2520 | peer_pk = &ssl->session_negotiate->peer_cert->pk; |
| 2521 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | 7f2f062 | 2015-09-03 10:44:32 +0200 | [diff] [blame] | 2522 | |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2523 | /* |
| 2524 | * Now write it out, encrypted |
| 2525 | */ |
Hanno Becker | c7d7e29 | 2019-02-06 16:49:54 +0000 | [diff] [blame] | 2526 | if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_RSA ) ) |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2527 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2528 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) ); |
| 2529 | return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH ); |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2530 | } |
| 2531 | |
Hanno Becker | c7d7e29 | 2019-02-06 16:49:54 +0000 | [diff] [blame] | 2532 | if( ( ret = mbedtls_pk_encrypt( peer_pk, |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2533 | p, ssl->handshake->pmslen, |
| 2534 | ssl->out_msg + offset + len_bytes, olen, |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2535 | MBEDTLS_SSL_OUT_CONTENT_LEN - offset - len_bytes, |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 2536 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2537 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2538 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_rsa_pkcs1_encrypt", ret ); |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2539 | return( ret ); |
| 2540 | } |
| 2541 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2542 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 2543 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2544 | if( len_bytes == 2 ) |
| 2545 | { |
| 2546 | ssl->out_msg[offset+0] = (unsigned char)( *olen >> 8 ); |
| 2547 | ssl->out_msg[offset+1] = (unsigned char)( *olen ); |
| 2548 | *olen += 2; |
| 2549 | } |
| 2550 | #endif |
| 2551 | |
Hanno Becker | ae553dd | 2019-02-08 14:06:00 +0000 | [diff] [blame] | 2552 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 2553 | /* We don't need the peer's public key anymore. Free it. */ |
| 2554 | mbedtls_pk_free( peer_pk ); |
| 2555 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2556 | return( 0 ); |
| 2557 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2558 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED || |
| 2559 | MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2560 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2561 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Manuel Pégourié-Gonnard | 5c2a7ca | 2015-10-23 08:48:41 +0200 | [diff] [blame] | 2562 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \ |
| 2563 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ |
| 2564 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2565 | static int ssl_parse_signature_algorithm( mbedtls_ssl_context *ssl, |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2566 | unsigned char **p, |
| 2567 | unsigned char *end, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2568 | mbedtls_md_type_t *md_alg, |
| 2569 | mbedtls_pk_type_t *pk_alg ) |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2570 | { |
Paul Bakker | c5a79cc | 2013-06-26 15:08:35 +0200 | [diff] [blame] | 2571 | ((void) ssl); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2572 | *md_alg = MBEDTLS_MD_NONE; |
| 2573 | *pk_alg = MBEDTLS_PK_NONE; |
Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 2574 | |
| 2575 | /* Only in TLS 1.2 */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2576 | if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 2577 | { |
Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 2578 | return( 0 ); |
| 2579 | } |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2580 | |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2581 | if( (*p) + 2 > end ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2582 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2583 | |
Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 2584 | /* |
| 2585 | * Get hash algorithm |
| 2586 | */ |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2587 | if( ( *md_alg = mbedtls_ssl_md_alg_from_hash( (*p)[0] ) ) |
| 2588 | == MBEDTLS_MD_NONE ) |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2589 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2590 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 2591 | ( "Server used unsupported HashAlgorithm %d", *(p)[0] ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2592 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2593 | } |
| 2594 | |
Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 2595 | /* |
Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 2596 | * Get signature algorithm |
| 2597 | */ |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2598 | if( ( *pk_alg = mbedtls_ssl_pk_alg_from_sig( (*p)[1] ) ) |
| 2599 | == MBEDTLS_PK_NONE ) |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2600 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2601 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 2602 | ( "server used unsupported SignatureAlgorithm %d", (*p)[1] ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2603 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2604 | } |
| 2605 | |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 2606 | /* |
| 2607 | * Check if the hash is acceptable |
| 2608 | */ |
| 2609 | if( mbedtls_ssl_check_sig_hash( ssl, *md_alg ) != 0 ) |
| 2610 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2611 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 2612 | ( "server used HashAlgorithm %d that was not offered", *(p)[0] ) ); |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 2613 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
| 2614 | } |
| 2615 | |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2616 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d", |
| 2617 | (*p)[1] ) ); |
| 2618 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d", |
| 2619 | (*p)[0] ) ); |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2620 | *p += 2; |
| 2621 | |
| 2622 | return( 0 ); |
| 2623 | } |
Manuel Pégourié-Gonnard | 5c2a7ca | 2015-10-23 08:48:41 +0200 | [diff] [blame] | 2624 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED || |
| 2625 | MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || |
| 2626 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2627 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2628 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2629 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ |
| 2630 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) |
| 2631 | static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2632 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2633 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2634 | const mbedtls_ecp_keypair *peer_key; |
Hanno Becker | be7f508 | 2019-02-06 17:44:07 +0000 | [diff] [blame] | 2635 | mbedtls_pk_context * peer_pk; |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2636 | |
Hanno Becker | be7f508 | 2019-02-06 17:44:07 +0000 | [diff] [blame] | 2637 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 2638 | peer_pk = &ssl->handshake->peer_pubkey; |
| 2639 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | 7f2f062 | 2015-09-03 10:44:32 +0200 | [diff] [blame] | 2640 | if( ssl->session_negotiate->peer_cert == NULL ) |
| 2641 | { |
Hanno Becker | 8273df8 | 2019-02-06 17:37:32 +0000 | [diff] [blame] | 2642 | /* Should never happen */ |
Hanno Becker | bd5580a | 2019-02-26 12:36:01 +0000 | [diff] [blame] | 2643 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Hanno Becker | 8273df8 | 2019-02-06 17:37:32 +0000 | [diff] [blame] | 2644 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7f2f062 | 2015-09-03 10:44:32 +0200 | [diff] [blame] | 2645 | } |
Hanno Becker | be7f508 | 2019-02-06 17:44:07 +0000 | [diff] [blame] | 2646 | peer_pk = &ssl->session_negotiate->peer_cert->pk; |
| 2647 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | 7f2f062 | 2015-09-03 10:44:32 +0200 | [diff] [blame] | 2648 | |
Hanno Becker | be7f508 | 2019-02-06 17:44:07 +0000 | [diff] [blame] | 2649 | if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_ECKEY ) ) |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2650 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2651 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) ); |
| 2652 | return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH ); |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2653 | } |
| 2654 | |
Hanno Becker | be7f508 | 2019-02-06 17:44:07 +0000 | [diff] [blame] | 2655 | peer_key = mbedtls_pk_ec( *peer_pk ); |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2656 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2657 | if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key, |
| 2658 | MBEDTLS_ECDH_THEIRS ) ) != 0 ) |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2659 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2660 | MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret ); |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2661 | return( ret ); |
| 2662 | } |
| 2663 | |
| 2664 | if( ssl_check_server_ecdh_params( ssl ) != 0 ) |
| 2665 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2666 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) ); |
| 2667 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2668 | } |
| 2669 | |
Hanno Becker | ae553dd | 2019-02-08 14:06:00 +0000 | [diff] [blame] | 2670 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 2671 | /* We don't need the peer's public key anymore. Free it, |
| 2672 | * so that more RAM is available for upcoming expensive |
| 2673 | * operations like ECDHE. */ |
| 2674 | mbedtls_pk_free( peer_pk ); |
| 2675 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 2676 | |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2677 | return( ret ); |
| 2678 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2679 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || |
| 2680 | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */ |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2681 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2682 | static int ssl_parse_server_key_exchange( mbedtls_ssl_context *ssl ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2683 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2684 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 2685 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 2686 | ssl->handshake->ciphersuite_info; |
Andres Amaya Garcia | 53c77cc | 2017-06-27 16:15:06 +0100 | [diff] [blame] | 2687 | unsigned char *p = NULL, *end = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2688 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2689 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2690 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2691 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) |
| 2692 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2693 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2694 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2695 | ssl->state++; |
| 2696 | return( 0 ); |
| 2697 | } |
Manuel Pégourié-Gonnard | bac0e3b | 2013-10-15 11:54:47 +0200 | [diff] [blame] | 2698 | ((void) p); |
| 2699 | ((void) end); |
| 2700 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2701 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2702 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ |
| 2703 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) |
| 2704 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA || |
| 2705 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA ) |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2706 | { |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 2707 | if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 ) |
| 2708 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2709 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret ); |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2710 | mbedtls_ssl_send_alert_message( |
| 2711 | ssl, |
| 2712 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2713 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 2714 | return( ret ); |
| 2715 | } |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2716 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2717 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) ); |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2718 | ssl->state++; |
| 2719 | return( 0 ); |
| 2720 | } |
| 2721 | ((void) p); |
| 2722 | ((void) end); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2723 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED || |
| 2724 | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */ |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2725 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2726 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 2727 | if( ssl->handshake->ecrs_enabled && |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 2728 | ssl->handshake->ecrs_state == ssl_ecrs_ske_start_processing ) |
Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 2729 | { |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 2730 | goto start_processing; |
Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 2731 | } |
Manuel Pégourié-Gonnard | 1f1f2a1 | 2017-05-18 11:27:06 +0200 | [diff] [blame] | 2732 | #endif |
| 2733 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 2734 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2735 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2736 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2737 | return( ret ); |
| 2738 | } |
| 2739 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2740 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2741 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2742 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2743 | mbedtls_ssl_send_alert_message( |
| 2744 | ssl, |
| 2745 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2746 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2747 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2748 | } |
| 2749 | |
Manuel Pégourié-Gonnard | 09258b9 | 2013-10-15 10:43:36 +0200 | [diff] [blame] | 2750 | /* |
| 2751 | * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server |
| 2752 | * doesn't use a psk_identity_hint |
| 2753 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2754 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2755 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2756 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || |
| 2757 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
Paul Bakker | 188c8de | 2013-04-19 09:13:37 +0200 | [diff] [blame] | 2758 | { |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 2759 | /* Current message is probably either |
| 2760 | * CertificateRequest or ServerHelloDone */ |
| 2761 | ssl->keep_current_message = 1; |
Paul Bakker | 188c8de | 2013-04-19 09:13:37 +0200 | [diff] [blame] | 2762 | goto exit; |
| 2763 | } |
| 2764 | |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2765 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 2766 | ( "server key exchange message must not be skipped" ) ); |
| 2767 | mbedtls_ssl_send_alert_message( |
| 2768 | ssl, |
| 2769 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2770 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 2771 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2772 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2773 | } |
| 2774 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2775 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 2776 | if( ssl->handshake->ecrs_enabled ) |
| 2777 | ssl->handshake->ecrs_state = ssl_ecrs_ske_start_processing; |
| 2778 | |
| 2779 | start_processing: |
| 2780 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2781 | p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ); |
Paul Bakker | 3b6a07b | 2013-03-21 11:56:50 +0100 | [diff] [blame] | 2782 | end = ssl->in_msg + ssl->in_hslen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2783 | MBEDTLS_SSL_DEBUG_BUF( 3, "server key exchange", p, end - p ); |
Paul Bakker | 3b6a07b | 2013-03-21 11:56:50 +0100 | [diff] [blame] | 2784 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2785 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2786 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || |
| 2787 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK || |
| 2788 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK || |
| 2789 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
Manuel Pégourié-Gonnard | 09258b9 | 2013-10-15 10:43:36 +0200 | [diff] [blame] | 2790 | { |
| 2791 | if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 ) |
| 2792 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2793 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2794 | mbedtls_ssl_send_alert_message( |
| 2795 | ssl, |
| 2796 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2797 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2798 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
Manuel Pégourié-Gonnard | 09258b9 | 2013-10-15 10:43:36 +0200 | [diff] [blame] | 2799 | } |
| 2800 | } /* FALLTROUGH */ |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2801 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | 09258b9 | 2013-10-15 10:43:36 +0200 | [diff] [blame] | 2802 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2803 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \ |
| 2804 | defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
| 2805 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || |
| 2806 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
Manuel Pégourié-Gonnard | 09258b9 | 2013-10-15 10:43:36 +0200 | [diff] [blame] | 2807 | ; /* nothing more to do */ |
| 2808 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2809 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED || |
| 2810 | MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
| 2811 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \ |
| 2812 | defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 2813 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA || |
| 2814 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2815 | { |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2816 | if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2817 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2818 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2819 | mbedtls_ssl_send_alert_message( |
| 2820 | ssl, |
| 2821 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2822 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2823 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2824 | } |
| 2825 | } |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2826 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2827 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED || |
| 2828 | MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
Hanno Becker | bb89e27 | 2019-01-08 12:54:37 +0000 | [diff] [blame] | 2829 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 2830 | ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ |
| 2831 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) ) |
| 2832 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA || |
| 2833 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ) |
| 2834 | { |
| 2835 | if( ssl_parse_server_ecdh_params_psa( ssl, &p, end ) != 0 ) |
| 2836 | { |
| 2837 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2838 | mbedtls_ssl_send_alert_message( |
| 2839 | ssl, |
| 2840 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2841 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Hanno Becker | bb89e27 | 2019-01-08 12:54:37 +0000 | [diff] [blame] | 2842 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
| 2843 | } |
| 2844 | } |
| 2845 | else |
| 2846 | #endif /* MBEDTLS_USE_PSA_CRYPTO && |
| 2847 | ( MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || |
| 2848 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ) */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2849 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ |
| 2850 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \ |
| 2851 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) |
| 2852 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA || |
| 2853 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK || |
| 2854 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2855 | { |
| 2856 | if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 ) |
| 2857 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2858 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2859 | mbedtls_ssl_send_alert_message( |
| 2860 | ssl, |
| 2861 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2862 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2863 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2864 | } |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2865 | } |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2866 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2867 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || |
| 2868 | MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED || |
| 2869 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */ |
Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 2870 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 2871 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ) |
| 2872 | { |
| 2873 | ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx, |
| 2874 | p, end - p ); |
| 2875 | if( ret != 0 ) |
| 2876 | { |
| 2877 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret ); |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2878 | mbedtls_ssl_send_alert_message( |
| 2879 | ssl, |
| 2880 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2881 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 2882 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
| 2883 | } |
| 2884 | } |
| 2885 | else |
| 2886 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2887 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2888 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2889 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2890 | } |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2891 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2892 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 2893 | if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2894 | { |
Manuel Pégourié-Gonnard | d92d6a1 | 2014-09-10 15:25:02 +0000 | [diff] [blame] | 2895 | size_t sig_len, hashlen; |
| 2896 | unsigned char hash[64]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2897 | mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE; |
| 2898 | mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE; |
| 2899 | unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ); |
Manuel Pégourié-Gonnard | d92d6a1 | 2014-09-10 15:25:02 +0000 | [diff] [blame] | 2900 | size_t params_len = p - params; |
Manuel Pégourié-Gonnard | 1f1f2a1 | 2017-05-18 11:27:06 +0200 | [diff] [blame] | 2901 | void *rs_ctx = NULL; |
Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 2902 | |
Hanno Becker | a6899bb | 2019-02-06 18:26:03 +0000 | [diff] [blame] | 2903 | mbedtls_pk_context * peer_pk; |
| 2904 | |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2905 | /* |
| 2906 | * Handle the digitally-signed structure |
| 2907 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2908 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2909 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2910 | { |
Paul Bakker | 9659dae | 2013-08-28 16:21:34 +0200 | [diff] [blame] | 2911 | if( ssl_parse_signature_algorithm( ssl, &p, end, |
| 2912 | &md_alg, &pk_alg ) != 0 ) |
| 2913 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2914 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 2915 | ( "bad server key exchange message" ) ); |
| 2916 | mbedtls_ssl_send_alert_message( |
| 2917 | ssl, |
| 2918 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2919 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2920 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
Paul Bakker | 9659dae | 2013-08-28 16:21:34 +0200 | [diff] [blame] | 2921 | } |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2922 | |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2923 | if( pk_alg != |
| 2924 | mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2925 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2926 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 2927 | ( "bad server key exchange message" ) ); |
| 2928 | mbedtls_ssl_send_alert_message( |
| 2929 | ssl, |
| 2930 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2931 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2932 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2933 | } |
| 2934 | } |
Manuel Pégourié-Gonnard | 09edda8 | 2013-08-19 13:50:33 +0200 | [diff] [blame] | 2935 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2936 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 2937 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 2938 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 2939 | if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | 09edda8 | 2013-08-19 13:50:33 +0200 | [diff] [blame] | 2940 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2941 | pk_alg = mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2942 | |
Paul Bakker | 9659dae | 2013-08-28 16:21:34 +0200 | [diff] [blame] | 2943 | /* Default hash for ECDSA is SHA-1 */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2944 | if( pk_alg == MBEDTLS_PK_ECDSA && md_alg == MBEDTLS_MD_NONE ) |
| 2945 | md_alg = MBEDTLS_MD_SHA1; |
Paul Bakker | 9659dae | 2013-08-28 16:21:34 +0200 | [diff] [blame] | 2946 | } |
| 2947 | else |
| 2948 | #endif |
| 2949 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2950 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2951 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 9659dae | 2013-08-28 16:21:34 +0200 | [diff] [blame] | 2952 | } |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 2953 | |
| 2954 | /* |
| 2955 | * Read signature |
| 2956 | */ |
Krzysztof Stachowiak | a1098f8 | 2018-03-13 11:28:49 +0100 | [diff] [blame] | 2957 | |
| 2958 | if( p > end - 2 ) |
| 2959 | { |
| 2960 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2961 | mbedtls_ssl_send_alert_message( |
| 2962 | ssl, |
| 2963 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2964 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Krzysztof Stachowiak | a1098f8 | 2018-03-13 11:28:49 +0100 | [diff] [blame] | 2965 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
| 2966 | } |
Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 2967 | sig_len = ( p[0] << 8 ) | p[1]; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2968 | p += 2; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2969 | |
Krzysztof Stachowiak | 027f84c | 2018-03-13 11:29:24 +0100 | [diff] [blame] | 2970 | if( p != end - sig_len ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2971 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2972 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2973 | mbedtls_ssl_send_alert_message( |
| 2974 | ssl, |
| 2975 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2976 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2977 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2978 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2979 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2980 | MBEDTLS_SSL_DEBUG_BUF( 3, "signature", p, sig_len ); |
Manuel Pégourié-Gonnard | ff56da3 | 2013-07-11 10:46:21 +0200 | [diff] [blame] | 2981 | |
Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 2982 | /* |
| 2983 | * Compute the hash that has been signed |
| 2984 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2985 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 2986 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 2987 | if( md_alg == MBEDTLS_MD_NONE ) |
Paul Bakker | c3f177a | 2012-04-11 16:11:49 +0000 | [diff] [blame] | 2988 | { |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 2989 | hashlen = 36; |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 2990 | ret = mbedtls_ssl_get_key_exchange_md_ssl_tls( ssl, hash, params, |
| 2991 | params_len ); |
| 2992 | if( ret != 0 ) |
Andres Amaya Garcia | f0e521e | 2017-06-28 12:11:42 +0100 | [diff] [blame] | 2993 | return( ret ); |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2994 | } |
| 2995 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2996 | #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \ |
| 2997 | MBEDTLS_SSL_PROTO_TLS1_1 */ |
| 2998 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 2999 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3000 | if( md_alg != MBEDTLS_MD_NONE ) |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 3001 | { |
Gilles Peskine | ca1d742 | 2018-04-24 11:53:22 +0200 | [diff] [blame] | 3002 | ret = mbedtls_ssl_get_key_exchange_md_tls1_2( ssl, hash, &hashlen, |
| 3003 | params, params_len, |
| 3004 | md_alg ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 3005 | if( ret != 0 ) |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 3006 | return( ret ); |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 3007 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 3008 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3009 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 3010 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 3011 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3012 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3013 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 3014 | } |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 3015 | |
Gilles Peskine | ca1d742 | 2018-04-24 11:53:22 +0200 | [diff] [blame] | 3016 | MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen ); |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 3017 | |
Hanno Becker | a6899bb | 2019-02-06 18:26:03 +0000 | [diff] [blame] | 3018 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 3019 | peer_pk = &ssl->handshake->peer_pubkey; |
| 3020 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | 7f2f062 | 2015-09-03 10:44:32 +0200 | [diff] [blame] | 3021 | if( ssl->session_negotiate->peer_cert == NULL ) |
| 3022 | { |
Hanno Becker | 8273df8 | 2019-02-06 17:37:32 +0000 | [diff] [blame] | 3023 | /* Should never happen */ |
Hanno Becker | bd5580a | 2019-02-26 12:36:01 +0000 | [diff] [blame] | 3024 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Hanno Becker | 8273df8 | 2019-02-06 17:37:32 +0000 | [diff] [blame] | 3025 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7f2f062 | 2015-09-03 10:44:32 +0200 | [diff] [blame] | 3026 | } |
Hanno Becker | a6899bb | 2019-02-06 18:26:03 +0000 | [diff] [blame] | 3027 | peer_pk = &ssl->session_negotiate->peer_cert->pk; |
| 3028 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | 7f2f062 | 2015-09-03 10:44:32 +0200 | [diff] [blame] | 3029 | |
Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 3030 | /* |
| 3031 | * Verify signature |
| 3032 | */ |
Hanno Becker | a6899bb | 2019-02-06 18:26:03 +0000 | [diff] [blame] | 3033 | if( !mbedtls_pk_can_do( peer_pk, pk_alg ) ) |
Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 3034 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3035 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3036 | mbedtls_ssl_send_alert_message( |
| 3037 | ssl, |
| 3038 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3039 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3040 | return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH ); |
Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 3041 | } |
| 3042 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3043 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 3044 | if( ssl->handshake->ecrs_enabled ) |
Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 3045 | rs_ctx = &ssl->handshake->ecrs_ctx.pk; |
Manuel Pégourié-Gonnard | 1f1f2a1 | 2017-05-18 11:27:06 +0200 | [diff] [blame] | 3046 | #endif |
| 3047 | |
Hanno Becker | a6899bb | 2019-02-06 18:26:03 +0000 | [diff] [blame] | 3048 | if( ( ret = mbedtls_pk_verify_restartable( peer_pk, |
Manuel Pégourié-Gonnard | 1f1f2a1 | 2017-05-18 11:27:06 +0200 | [diff] [blame] | 3049 | md_alg, hash, hashlen, p, sig_len, rs_ctx ) ) != 0 ) |
Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 3050 | { |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3051 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 1f1f2a1 | 2017-05-18 11:27:06 +0200 | [diff] [blame] | 3052 | if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 3053 | #endif |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3054 | mbedtls_ssl_send_alert_message( |
| 3055 | ssl, |
| 3056 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3057 | MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3058 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret ); |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3059 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 558da9c | 2018-06-13 12:02:12 +0200 | [diff] [blame] | 3060 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 3061 | ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS; |
| 3062 | #endif |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 3063 | return( ret ); |
Paul Bakker | c3f177a | 2012-04-11 16:11:49 +0000 | [diff] [blame] | 3064 | } |
Hanno Becker | ae553dd | 2019-02-08 14:06:00 +0000 | [diff] [blame] | 3065 | |
| 3066 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 3067 | /* We don't need the peer's public key anymore. Free it, |
| 3068 | * so that more RAM is available for upcoming expensive |
| 3069 | * operations like ECDHE. */ |
| 3070 | mbedtls_pk_free( peer_pk ); |
| 3071 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3072 | } |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3073 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3074 | |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 3075 | exit: |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3076 | ssl->state++; |
| 3077 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3078 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3079 | |
| 3080 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3081 | } |
| 3082 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3083 | #if ! defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3084 | static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | da1ff38 | 2013-11-25 17:38:36 +0100 | [diff] [blame] | 3085 | { |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3086 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 3087 | ssl->handshake->ciphersuite_info; |
Manuel Pégourié-Gonnard | da1ff38 | 2013-11-25 17:38:36 +0100 | [diff] [blame] | 3088 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3089 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) ); |
Manuel Pégourié-Gonnard | da1ff38 | 2013-11-25 17:38:36 +0100 | [diff] [blame] | 3090 | |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3091 | if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ) |
Manuel Pégourié-Gonnard | da1ff38 | 2013-11-25 17:38:36 +0100 | [diff] [blame] | 3092 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3093 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) ); |
Manuel Pégourié-Gonnard | da1ff38 | 2013-11-25 17:38:36 +0100 | [diff] [blame] | 3094 | ssl->state++; |
| 3095 | return( 0 ); |
| 3096 | } |
| 3097 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3098 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3099 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | da1ff38 | 2013-11-25 17:38:36 +0100 | [diff] [blame] | 3100 | } |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3101 | #else /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3102 | static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3103 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3104 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | d1b7f2b | 2016-02-24 14:13:22 +0000 | [diff] [blame] | 3105 | unsigned char *buf; |
| 3106 | size_t n = 0; |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 3107 | size_t cert_type_len = 0, dn_len = 0; |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3108 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 3109 | ssl->handshake->ciphersuite_info; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3110 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3111 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3112 | |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3113 | if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ) |
Manuel Pégourié-Gonnard | da1ff38 | 2013-11-25 17:38:36 +0100 | [diff] [blame] | 3114 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3115 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) ); |
Manuel Pégourié-Gonnard | da1ff38 | 2013-11-25 17:38:36 +0100 | [diff] [blame] | 3116 | ssl->state++; |
| 3117 | return( 0 ); |
| 3118 | } |
| 3119 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 3120 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3121 | { |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3122 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 3123 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3124 | } |
| 3125 | |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3126 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
| 3127 | { |
| 3128 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) ); |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3129 | mbedtls_ssl_send_alert_message( |
| 3130 | ssl, |
| 3131 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3132 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3133 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 3134 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3135 | |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3136 | ssl->state++; |
| 3137 | ssl->client_auth = ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3138 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3139 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request", |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3140 | ssl->client_auth ? "a" : "no" ) ); |
| 3141 | |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3142 | if( ssl->client_auth == 0 ) |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3143 | { |
| 3144 | /* Current message is probably the ServerHelloDone */ |
| 3145 | ssl->keep_current_message = 1; |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3146 | goto exit; |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3147 | } |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 3148 | |
Manuel Pégourié-Gonnard | 04c1b4e | 2014-09-10 19:25:43 +0200 | [diff] [blame] | 3149 | /* |
| 3150 | * struct { |
| 3151 | * ClientCertificateType certificate_types<1..2^8-1>; |
| 3152 | * SignatureAndHashAlgorithm |
| 3153 | * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only |
| 3154 | * DistinguishedName certificate_authorities<0..2^16-1>; |
| 3155 | * } CertificateRequest; |
Manuel Pégourié-Gonnard | d1b7f2b | 2016-02-24 14:13:22 +0000 | [diff] [blame] | 3156 | * |
| 3157 | * Since we only support a single certificate on clients, let's just |
| 3158 | * ignore all the information that's supposed to help us pick a |
| 3159 | * certificate. |
| 3160 | * |
| 3161 | * We could check that our certificate matches the request, and bail out |
| 3162 | * if it doesn't, but it's simpler to just send the certificate anyway, |
| 3163 | * and give the server the opportunity to decide if it should terminate |
| 3164 | * the connection when it doesn't like our certificate. |
| 3165 | * |
| 3166 | * Same goes for the hash in TLS 1.2's signature_algorithms: at this |
| 3167 | * point we only have one hash available (see comments in |
Simon Butcher | c0957bd | 2016-03-01 13:16:57 +0000 | [diff] [blame] | 3168 | * write_certificate_verify), so let's just use what we have. |
Manuel Pégourié-Gonnard | d1b7f2b | 2016-02-24 14:13:22 +0000 | [diff] [blame] | 3169 | * |
| 3170 | * However, we still minimally parse the message to check it is at least |
| 3171 | * superficially sane. |
Manuel Pégourié-Gonnard | 04c1b4e | 2014-09-10 19:25:43 +0200 | [diff] [blame] | 3172 | */ |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3173 | buf = ssl->in_msg; |
Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 3174 | |
Manuel Pégourié-Gonnard | d1b7f2b | 2016-02-24 14:13:22 +0000 | [diff] [blame] | 3175 | /* certificate_types */ |
Krzysztof Stachowiak | 73b183c | 2018-04-05 10:20:09 +0200 | [diff] [blame] | 3176 | if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) ) |
| 3177 | { |
| 3178 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) ); |
| 3179 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3180 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 3181 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST ); |
| 3182 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3183 | cert_type_len = buf[mbedtls_ssl_hs_hdr_len( ssl )]; |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3184 | n = cert_type_len; |
| 3185 | |
Krzysztof Stachowiak | bc145f7 | 2018-03-20 11:19:50 +0100 | [diff] [blame] | 3186 | /* |
Krzysztof Stachowiak | 94d4997 | 2018-04-05 14:48:55 +0200 | [diff] [blame] | 3187 | * In the subsequent code there are two paths that read from buf: |
Krzysztof Stachowiak | bc145f7 | 2018-03-20 11:19:50 +0100 | [diff] [blame] | 3188 | * * the length of the signature algorithms field (if minor version of |
| 3189 | * SSL is 3), |
| 3190 | * * distinguished name length otherwise. |
| 3191 | * Both reach at most the index: |
| 3192 | * ...hdr_len + 2 + n, |
| 3193 | * therefore the buffer length at this point must be greater than that |
| 3194 | * regardless of the actual code path. |
| 3195 | */ |
| 3196 | if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n ) |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3197 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3198 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 3199 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3200 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3201 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST ); |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3202 | } |
| 3203 | |
Manuel Pégourié-Gonnard | d1b7f2b | 2016-02-24 14:13:22 +0000 | [diff] [blame] | 3204 | /* supported_signature_algorithms */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3205 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3206 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3207 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3208 | size_t sig_alg_len = |
| 3209 | ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 ) |
| 3210 | | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3211 | #if defined(MBEDTLS_DEBUG_C) |
Krzysztof Stachowiak | bc231cc | 2018-03-20 14:09:53 +0100 | [diff] [blame] | 3212 | unsigned char* sig_alg; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3213 | size_t i; |
Krzysztof Stachowiak | bc231cc | 2018-03-20 14:09:53 +0100 | [diff] [blame] | 3214 | #endif |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3215 | |
Krzysztof Stachowiak | bc231cc | 2018-03-20 14:09:53 +0100 | [diff] [blame] | 3216 | /* |
Krzysztof Stachowiak | 94d4997 | 2018-04-05 14:48:55 +0200 | [diff] [blame] | 3217 | * The furthest access in buf is in the loop few lines below: |
Krzysztof Stachowiak | bc231cc | 2018-03-20 14:09:53 +0100 | [diff] [blame] | 3218 | * sig_alg[i + 1], |
| 3219 | * where: |
| 3220 | * sig_alg = buf + ...hdr_len + 3 + n, |
| 3221 | * max(i) = sig_alg_len - 1. |
Krzysztof Stachowiak | 94d4997 | 2018-04-05 14:48:55 +0200 | [diff] [blame] | 3222 | * Therefore the furthest access is: |
Krzysztof Stachowiak | bc231cc | 2018-03-20 14:09:53 +0100 | [diff] [blame] | 3223 | * buf[...hdr_len + 3 + n + sig_alg_len - 1 + 1], |
| 3224 | * which reduces to: |
| 3225 | * buf[...hdr_len + 3 + n + sig_alg_len], |
| 3226 | * which is one less than we need the buf to be. |
| 3227 | */ |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3228 | if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) |
| 3229 | + 3 + n + sig_alg_len ) |
Krzysztof Stachowiak | bc231cc | 2018-03-20 14:09:53 +0100 | [diff] [blame] | 3230 | { |
| 3231 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) ); |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3232 | mbedtls_ssl_send_alert_message( |
| 3233 | ssl, |
| 3234 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3235 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Krzysztof Stachowiak | bc231cc | 2018-03-20 14:09:53 +0100 | [diff] [blame] | 3236 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST ); |
| 3237 | } |
| 3238 | |
| 3239 | #if defined(MBEDTLS_DEBUG_C) |
| 3240 | sig_alg = buf + mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3241 | for( i = 0; i < sig_alg_len; i += 2 ) |
| 3242 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3243 | MBEDTLS_SSL_DEBUG_MSG( 3, |
| 3244 | ( "Supported Signature Algorithm found: %d,%d", |
| 3245 | sig_alg[i], sig_alg[i + 1] ) ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3246 | } |
| 3247 | #endif |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3248 | |
Manuel Pégourié-Gonnard | d1b7f2b | 2016-02-24 14:13:22 +0000 | [diff] [blame] | 3249 | n += 2 + sig_alg_len; |
Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 3250 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3251 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3252 | |
Manuel Pégourié-Gonnard | d1b7f2b | 2016-02-24 14:13:22 +0000 | [diff] [blame] | 3253 | /* certificate_authorities */ |
| 3254 | dn_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 ) |
| 3255 | | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) ); |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3256 | |
| 3257 | n += dn_len; |
Manuel Pégourié-Gonnard | d1b7f2b | 2016-02-24 14:13:22 +0000 | [diff] [blame] | 3258 | if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n ) |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3259 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3260 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 3261 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3262 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3263 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST ); |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3264 | } |
| 3265 | |
| 3266 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3267 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3268 | |
| 3269 | return( 0 ); |
| 3270 | } |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3271 | #endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3272 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3273 | static int ssl_parse_server_hello_done( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3274 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3275 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3276 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3277 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3278 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 3279 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3280 | { |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3281 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 3282 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3283 | } |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3284 | |
| 3285 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
| 3286 | { |
| 3287 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) ); |
| 3288 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 3289 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3290 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3291 | if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) || |
| 3292 | ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3293 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3294 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 3295 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3296 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3297 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO_DONE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3298 | } |
| 3299 | |
| 3300 | ssl->state++; |
| 3301 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3302 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3303 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3304 | mbedtls_ssl_recv_flight_completed( ssl ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3305 | #endif |
| 3306 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3307 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3308 | |
| 3309 | return( 0 ); |
| 3310 | } |
| 3311 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3312 | static int ssl_write_client_key_exchange( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3313 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3314 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3315 | |
| 3316 | size_t header_len; |
| 3317 | size_t content_len; |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3318 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 3319 | ssl->handshake->ciphersuite_info; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3320 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3321 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3322 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3323 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) |
| 3324 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3325 | { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3326 | /* |
| 3327 | * DHM key exchange -- send G^X mod P |
| 3328 | */ |
Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3329 | content_len = ssl->handshake->dhm_ctx.len; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3330 | |
Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3331 | ssl->out_msg[4] = (unsigned char)( content_len >> 8 ); |
| 3332 | ssl->out_msg[5] = (unsigned char)( content_len ); |
| 3333 | header_len = 6; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3334 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3335 | ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx, |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3336 | (int) mbedtls_mpi_size( &ssl->handshake->dhm_ctx.P ), |
| 3337 | &ssl->out_msg[header_len], content_len, |
| 3338 | ssl->conf->f_rng, ssl->conf->p_rng ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3339 | if( ret != 0 ) |
| 3340 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3341 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3342 | return( ret ); |
| 3343 | } |
| 3344 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3345 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X ); |
| 3346 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3347 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3348 | if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx, |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3349 | ssl->handshake->premaster, |
| 3350 | MBEDTLS_PREMASTER_SIZE, |
| 3351 | &ssl->handshake->pmslen, |
| 3352 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3353 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3354 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3355 | return( ret ); |
| 3356 | } |
| 3357 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3358 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3359 | } |
| 3360 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3361 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */ |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 3362 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 3363 | ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ |
| 3364 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) ) |
| 3365 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA || |
| 3366 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ) |
| 3367 | { |
| 3368 | psa_status_t status; |
Janos Follath | 53b8ec2 | 2019-08-08 10:28:27 +0100 | [diff] [blame] | 3369 | psa_key_attributes_t key_attributes; |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 3370 | |
| 3371 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
| 3372 | |
| 3373 | unsigned char own_pubkey[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH]; |
| 3374 | size_t own_pubkey_len; |
| 3375 | unsigned char *own_pubkey_ecpoint; |
| 3376 | size_t own_pubkey_ecpoint_len; |
| 3377 | |
Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3378 | header_len = 4; |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 3379 | |
Hanno Becker | 0a94a64 | 2019-01-11 14:35:30 +0000 | [diff] [blame] | 3380 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) ); |
| 3381 | |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 3382 | /* |
| 3383 | * Generate EC private key for ECDHE exchange. |
| 3384 | */ |
| 3385 | |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 3386 | /* The master secret is obtained from the shared ECDH secret by |
| 3387 | * applying the TLS 1.2 PRF with a specific salt and label. While |
| 3388 | * the PSA Crypto API encourages combining key agreement schemes |
| 3389 | * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not |
| 3390 | * yet support the provisioning of salt + label to the KDF. |
| 3391 | * For the time being, we therefore need to split the computation |
| 3392 | * of the ECDH secret and the application of the TLS 1.2 PRF. */ |
Janos Follath | 53b8ec2 | 2019-08-08 10:28:27 +0100 | [diff] [blame] | 3393 | key_attributes = psa_key_attributes_init(); |
| 3394 | psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE ); |
Janos Follath | df3b089 | 2019-08-08 11:12:24 +0100 | [diff] [blame] | 3395 | psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH ); |
Gilles Peskine | 4245980 | 2019-12-19 13:31:53 +0100 | [diff] [blame] | 3396 | psa_set_key_type( &key_attributes, handshake->ecdh_psa_type ); |
| 3397 | psa_set_key_bits( &key_attributes, handshake->ecdh_bits ); |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 3398 | |
| 3399 | /* Generate ECDH private key. */ |
Janos Follath | 53b8ec2 | 2019-08-08 10:28:27 +0100 | [diff] [blame] | 3400 | status = psa_generate_key( &key_attributes, |
Janos Follath | df3b089 | 2019-08-08 11:12:24 +0100 | [diff] [blame] | 3401 | &handshake->ecdh_psa_privkey ); |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 3402 | if( status != PSA_SUCCESS ) |
| 3403 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 3404 | |
| 3405 | /* Export the public part of the ECDH private key from PSA |
| 3406 | * and convert it to ECPoint format used in ClientKeyExchange. */ |
| 3407 | status = psa_export_public_key( handshake->ecdh_psa_privkey, |
| 3408 | own_pubkey, sizeof( own_pubkey ), |
| 3409 | &own_pubkey_len ); |
| 3410 | if( status != PSA_SUCCESS ) |
| 3411 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 3412 | |
| 3413 | if( mbedtls_psa_tls_psa_ec_to_ecpoint( own_pubkey, |
| 3414 | own_pubkey_len, |
| 3415 | &own_pubkey_ecpoint, |
| 3416 | &own_pubkey_ecpoint_len ) != 0 ) |
| 3417 | { |
| 3418 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 3419 | } |
| 3420 | |
| 3421 | /* Copy ECPoint structure to outgoing message buffer. */ |
Andrzej Kurek | ade9e28 | 2019-05-07 08:19:33 -0400 | [diff] [blame] | 3422 | ssl->out_msg[header_len] = (unsigned char) own_pubkey_ecpoint_len; |
Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3423 | memcpy( ssl->out_msg + header_len + 1, |
| 3424 | own_pubkey_ecpoint, own_pubkey_ecpoint_len ); |
| 3425 | content_len = own_pubkey_ecpoint_len + 1; |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 3426 | |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 3427 | /* The ECDH secret is the premaster secret used for key derivation. */ |
| 3428 | |
Janos Follath | df3b089 | 2019-08-08 11:12:24 +0100 | [diff] [blame] | 3429 | /* Compute ECDH shared secret. */ |
| 3430 | status = psa_raw_key_agreement( PSA_ALG_ECDH, |
| 3431 | handshake->ecdh_psa_privkey, |
| 3432 | handshake->ecdh_psa_peerkey, |
| 3433 | handshake->ecdh_psa_peerkey_len, |
| 3434 | ssl->handshake->premaster, |
| 3435 | sizeof( ssl->handshake->premaster ), |
| 3436 | &ssl->handshake->pmslen ); |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 3437 | if( status != PSA_SUCCESS ) |
| 3438 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 3439 | |
| 3440 | status = psa_destroy_key( handshake->ecdh_psa_privkey ); |
| 3441 | if( status != PSA_SUCCESS ) |
| 3442 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 3443 | handshake->ecdh_psa_privkey = 0; |
| 3444 | } |
| 3445 | else |
| 3446 | #endif /* MBEDTLS_USE_PSA_CRYPTO && |
| 3447 | ( MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || |
| 3448 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ) */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3449 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ |
| 3450 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \ |
| 3451 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ |
| 3452 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) |
| 3453 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA || |
| 3454 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA || |
| 3455 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA || |
| 3456 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3457 | { |
| 3458 | /* |
| 3459 | * ECDH key exchange -- send client public value |
| 3460 | */ |
Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3461 | header_len = 4; |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3462 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3463 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 3464 | if( ssl->handshake->ecrs_enabled ) |
| 3465 | { |
Manuel Pégourié-Gonnard | c37423f | 2018-10-16 10:28:17 +0200 | [diff] [blame] | 3466 | if( ssl->handshake->ecrs_state == ssl_ecrs_cke_ecdh_calc_secret ) |
Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 3467 | goto ecdh_calc_secret; |
Manuel Pégourié-Gonnard | 23e4162 | 2017-05-18 12:35:37 +0200 | [diff] [blame] | 3468 | |
Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 3469 | mbedtls_ecdh_enable_restart( &ssl->handshake->ecdh_ctx ); |
| 3470 | } |
Manuel Pégourié-Gonnard | 2350b4e | 2017-05-16 09:26:48 +0200 | [diff] [blame] | 3471 | #endif |
| 3472 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3473 | ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx, |
Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3474 | &content_len, |
| 3475 | &ssl->out_msg[header_len], 1000, |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 3476 | ssl->conf->f_rng, ssl->conf->p_rng ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3477 | if( ret != 0 ) |
| 3478 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3479 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret ); |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3480 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 558da9c | 2018-06-13 12:02:12 +0200 | [diff] [blame] | 3481 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 3482 | ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS; |
| 3483 | #endif |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3484 | return( ret ); |
| 3485 | } |
| 3486 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 3487 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 3488 | MBEDTLS_DEBUG_ECDH_Q ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3489 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3490 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 3491 | if( ssl->handshake->ecrs_enabled ) |
| 3492 | { |
Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3493 | ssl->handshake->ecrs_n = content_len; |
Manuel Pégourié-Gonnard | c37423f | 2018-10-16 10:28:17 +0200 | [diff] [blame] | 3494 | ssl->handshake->ecrs_state = ssl_ecrs_cke_ecdh_calc_secret; |
Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 3495 | } |
Manuel Pégourié-Gonnard | 2350b4e | 2017-05-16 09:26:48 +0200 | [diff] [blame] | 3496 | |
| 3497 | ecdh_calc_secret: |
Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 3498 | if( ssl->handshake->ecrs_enabled ) |
Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3499 | content_len = ssl->handshake->ecrs_n; |
Manuel Pégourié-Gonnard | 2350b4e | 2017-05-16 09:26:48 +0200 | [diff] [blame] | 3500 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3501 | if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3502 | &ssl->handshake->pmslen, |
| 3503 | ssl->handshake->premaster, |
| 3504 | MBEDTLS_MPI_MAX_SIZE, |
| 3505 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3506 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3507 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret ); |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3508 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 558da9c | 2018-06-13 12:02:12 +0200 | [diff] [blame] | 3509 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 3510 | ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS; |
| 3511 | #endif |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3512 | return( ret ); |
| 3513 | } |
| 3514 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 3515 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 3516 | MBEDTLS_DEBUG_ECDH_Z ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3517 | } |
| 3518 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3519 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || |
| 3520 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED || |
| 3521 | MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED || |
| 3522 | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */ |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3523 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3524 | if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 3525 | { |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 3526 | /* |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 3527 | * opaque psk_identity<0..2^16-1>; |
| 3528 | */ |
Hanno Becker | 520224e | 2018-10-26 11:38:07 +0100 | [diff] [blame] | 3529 | if( ssl_conf_has_static_psk( ssl->conf ) == 0 ) |
Manuel Pégourié-Gonnard | b4b19f3 | 2015-07-07 11:41:21 +0200 | [diff] [blame] | 3530 | { |
Hanno Becker | 2e4f616 | 2018-10-23 11:54:44 +0100 | [diff] [blame] | 3531 | /* We don't offer PSK suites if we don't have a PSK, |
| 3532 | * and we check that the server's choice is among the |
| 3533 | * ciphersuites we offered, so this should never happen. */ |
| 3534 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | b4b19f3 | 2015-07-07 11:41:21 +0200 | [diff] [blame] | 3535 | } |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 3536 | |
Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3537 | header_len = 4; |
| 3538 | content_len = ssl->conf->psk_identity_len; |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 3539 | |
Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3540 | if( header_len + 2 + content_len > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 3541 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3542 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 3543 | ( "psk identity too long or SSL buffer too short" ) ); |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 3544 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 3545 | } |
| 3546 | |
Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3547 | ssl->out_msg[header_len++] = (unsigned char)( content_len >> 8 ); |
| 3548 | ssl->out_msg[header_len++] = (unsigned char)( content_len ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3549 | |
Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3550 | memcpy( ssl->out_msg + header_len, |
| 3551 | ssl->conf->psk_identity, |
| 3552 | ssl->conf->psk_identity_len ); |
| 3553 | header_len += ssl->conf->psk_identity_len; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3554 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3555 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 3556 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3557 | { |
Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3558 | content_len = 0; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 3559 | } |
Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3560 | else |
| 3561 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3562 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
| 3563 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 3564 | { |
Hanno Becker | dfab8e2 | 2018-10-23 11:59:34 +0100 | [diff] [blame] | 3565 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3566 | /* Opaque PSKs are currently only supported for PSK-only suites. */ |
Hanno Becker | 520224e | 2018-10-26 11:38:07 +0100 | [diff] [blame] | 3567 | if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 ) |
Hanno Becker | dfab8e2 | 2018-10-23 11:59:34 +0100 | [diff] [blame] | 3568 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 3569 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 3570 | |
Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3571 | if( ( ret = ssl_write_encrypted_pms( ssl, header_len, |
| 3572 | &content_len, 2 ) ) != 0 ) |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 3573 | return( ret ); |
| 3574 | } |
| 3575 | else |
| 3576 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3577 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 3578 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3579 | { |
Hanno Becker | dfab8e2 | 2018-10-23 11:59:34 +0100 | [diff] [blame] | 3580 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3581 | /* Opaque PSKs are currently only supported for PSK-only suites. */ |
Hanno Becker | 520224e | 2018-10-26 11:38:07 +0100 | [diff] [blame] | 3582 | if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 ) |
Hanno Becker | dfab8e2 | 2018-10-23 11:59:34 +0100 | [diff] [blame] | 3583 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 3584 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 3585 | |
Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3586 | /* |
| 3587 | * ClientDiffieHellmanPublic public (DHM send G^X mod P) |
| 3588 | */ |
Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3589 | content_len = ssl->handshake->dhm_ctx.len; |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 3590 | |
Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3591 | if( header_len + 2 + content_len > |
| 3592 | MBEDTLS_SSL_OUT_CONTENT_LEN ) |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 3593 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3594 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 3595 | ( "psk identity or DHM size too long or SSL buffer too short" ) ); |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 3596 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 3597 | } |
| 3598 | |
Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3599 | ssl->out_msg[header_len++] = (unsigned char)( content_len >> 8 ); |
| 3600 | ssl->out_msg[header_len++] = (unsigned char)( content_len ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3601 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3602 | ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx, |
| 3603 | (int) mbedtls_mpi_size( &ssl->handshake->dhm_ctx.P ), |
Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3604 | &ssl->out_msg[header_len], content_len, |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 3605 | ssl->conf->f_rng, ssl->conf->p_rng ); |
Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3606 | if( ret != 0 ) |
| 3607 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3608 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret ); |
Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3609 | return( ret ); |
| 3610 | } |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3611 | } |
Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3612 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3613 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
| 3614 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
| 3615 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 3616 | { |
Hanno Becker | dfab8e2 | 2018-10-23 11:59:34 +0100 | [diff] [blame] | 3617 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3618 | /* Opaque PSKs are currently only supported for PSK-only suites. */ |
Hanno Becker | 520224e | 2018-10-26 11:38:07 +0100 | [diff] [blame] | 3619 | if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 ) |
Hanno Becker | dfab8e2 | 2018-10-23 11:59:34 +0100 | [diff] [blame] | 3620 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 3621 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 3622 | |
Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3623 | /* |
| 3624 | * ClientECDiffieHellmanPublic public; |
| 3625 | */ |
Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3626 | ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx, |
| 3627 | &content_len, |
| 3628 | &ssl->out_msg[header_len], |
| 3629 | MBEDTLS_SSL_OUT_CONTENT_LEN - header_len, |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 3630 | ssl->conf->f_rng, ssl->conf->p_rng ); |
Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3631 | if( ret != 0 ) |
| 3632 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3633 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret ); |
Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3634 | return( ret ); |
| 3635 | } |
Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 3636 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 3637 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 3638 | MBEDTLS_DEBUG_ECDH_Q ); |
Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3639 | } |
| 3640 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3641 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3642 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3643 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3644 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3645 | } |
| 3646 | |
Hanno Becker | afd311e | 2018-10-23 15:26:40 +0100 | [diff] [blame] | 3647 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 3648 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 3649 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK && |
| 3650 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 && |
Hanno Becker | 520224e | 2018-10-26 11:38:07 +0100 | [diff] [blame] | 3651 | ssl_conf_has_static_raw_psk( ssl->conf ) == 0 ) |
Hanno Becker | afd311e | 2018-10-23 15:26:40 +0100 | [diff] [blame] | 3652 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3653 | MBEDTLS_SSL_DEBUG_MSG( |
| 3654 | 1, ( "skip PMS generation for opaque PSK" ) ); |
Hanno Becker | afd311e | 2018-10-23 15:26:40 +0100 | [diff] [blame] | 3655 | } |
| 3656 | else |
| 3657 | #endif /* MBEDTLS_USE_PSA_CRYPTO && |
| 3658 | MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3659 | if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl, |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 3660 | ciphersuite_info->key_exchange ) ) != 0 ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3661 | { |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3662 | MBEDTLS_SSL_DEBUG_RET( |
| 3663 | 1, "mbedtls_ssl_psk_derive_premaster", ret ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3664 | return( ret ); |
| 3665 | } |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3666 | } |
| 3667 | else |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3668 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3669 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) |
| 3670 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3671 | { |
Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3672 | header_len = 4; |
| 3673 | if( ( ret = ssl_write_encrypted_pms( ssl, header_len, |
| 3674 | &content_len, 0 ) ) != 0 ) |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 3675 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3676 | } |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 3677 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3678 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */ |
Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 3679 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 3680 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ) |
| 3681 | { |
Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3682 | header_len = 4; |
Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 3683 | |
| 3684 | ret = mbedtls_ecjpake_write_round_two( &ssl->handshake->ecjpake_ctx, |
Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3685 | ssl->out_msg + header_len, |
| 3686 | MBEDTLS_SSL_OUT_CONTENT_LEN - header_len, |
| 3687 | &content_len, |
Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 3688 | ssl->conf->f_rng, ssl->conf->p_rng ); |
| 3689 | if( ret != 0 ) |
| 3690 | { |
| 3691 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret ); |
| 3692 | return( ret ); |
| 3693 | } |
| 3694 | |
| 3695 | ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx, |
| 3696 | ssl->handshake->premaster, 32, &ssl->handshake->pmslen, |
| 3697 | ssl->conf->f_rng, ssl->conf->p_rng ); |
| 3698 | if( ret != 0 ) |
| 3699 | { |
| 3700 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret ); |
| 3701 | return( ret ); |
| 3702 | } |
| 3703 | } |
| 3704 | else |
| 3705 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */ |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 3706 | { |
| 3707 | ((void) ciphersuite_info); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3708 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3709 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 3710 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3711 | |
Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3712 | ssl->out_msglen = header_len + content_len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3713 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 3714 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3715 | |
| 3716 | ssl->state++; |
| 3717 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3718 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3719 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3720 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3721 | return( ret ); |
| 3722 | } |
| 3723 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3724 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3725 | |
| 3726 | return( 0 ); |
| 3727 | } |
| 3728 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3729 | #if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3730 | static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3731 | { |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3732 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 3733 | ssl->handshake->ciphersuite_info; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3734 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3735 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3736 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3737 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3738 | if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 3739 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3740 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret ); |
Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 3741 | return( ret ); |
| 3742 | } |
| 3743 | |
Hanno Becker | 77adddc | 2019-02-07 12:32:43 +0000 | [diff] [blame] | 3744 | if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ) |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 3745 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3746 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 3747 | ssl->state++; |
| 3748 | return( 0 ); |
| 3749 | } |
| 3750 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3751 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3752 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3753 | } |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3754 | #else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3755 | static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3756 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3757 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3758 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 3759 | ssl->handshake->ciphersuite_info; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3760 | size_t n = 0, offset = 0; |
| 3761 | unsigned char hash[48]; |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3762 | unsigned char *hash_start = hash; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3763 | mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE; |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 3764 | size_t hashlen; |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 3765 | void *rs_ctx = NULL; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3766 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3767 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3768 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3769 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 3770 | if( ssl->handshake->ecrs_enabled && |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 3771 | ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign ) |
Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 3772 | { |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 3773 | goto sign; |
Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 3774 | } |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 3775 | #endif |
| 3776 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3777 | if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 3778 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3779 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret ); |
Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 3780 | return( ret ); |
| 3781 | } |
| 3782 | |
Hanno Becker | 77adddc | 2019-02-07 12:32:43 +0000 | [diff] [blame] | 3783 | if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3784 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3785 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3786 | ssl->state++; |
| 3787 | return( 0 ); |
| 3788 | } |
| 3789 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3790 | if( ssl->client_auth == 0 || mbedtls_ssl_own_cert( ssl ) == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3791 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3792 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3793 | ssl->state++; |
| 3794 | return( 0 ); |
| 3795 | } |
| 3796 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3797 | if( mbedtls_ssl_own_key( ssl ) == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3798 | { |
Manuel Pégourié-Gonnard | b4b19f3 | 2015-07-07 11:41:21 +0200 | [diff] [blame] | 3799 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key for certificate" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3800 | return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3801 | } |
| 3802 | |
| 3803 | /* |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 3804 | * Make a signature of the handshake digests |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3805 | */ |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3806 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 3807 | if( ssl->handshake->ecrs_enabled ) |
| 3808 | ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign; |
| 3809 | |
| 3810 | sign: |
| 3811 | #endif |
| 3812 | |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 3813 | ssl->handshake->calc_verify( ssl, hash, &hashlen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3814 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3815 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 3816 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 3817 | if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3818 | { |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3819 | /* |
| 3820 | * digitally-signed struct { |
| 3821 | * opaque md5_hash[16]; |
| 3822 | * opaque sha_hash[20]; |
| 3823 | * }; |
| 3824 | * |
| 3825 | * md5_hash |
| 3826 | * MD5(handshake_messages); |
| 3827 | * |
| 3828 | * sha_hash |
| 3829 | * SHA(handshake_messages); |
| 3830 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3831 | md_alg = MBEDTLS_MD_NONE; |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3832 | |
| 3833 | /* |
| 3834 | * For ECDSA, default hash is SHA-1 only |
| 3835 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3836 | if( mbedtls_pk_can_do( mbedtls_ssl_own_key( ssl ), MBEDTLS_PK_ECDSA ) ) |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3837 | { |
| 3838 | hash_start += 16; |
| 3839 | hashlen -= 16; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3840 | md_alg = MBEDTLS_MD_SHA1; |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3841 | } |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3842 | } |
| 3843 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3844 | #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \ |
| 3845 | MBEDTLS_SSL_PROTO_TLS1_1 */ |
| 3846 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3847 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3848 | { |
| 3849 | /* |
| 3850 | * digitally-signed struct { |
| 3851 | * opaque handshake_messages[handshake_messages_length]; |
| 3852 | * }; |
| 3853 | * |
| 3854 | * Taking shortcut here. We assume that the server always allows the |
| 3855 | * PRF Hash function and has sent it in the allowed signature |
| 3856 | * algorithms list received in the Certificate Request message. |
| 3857 | * |
| 3858 | * Until we encounter a server that does not, we will take this |
| 3859 | * shortcut. |
| 3860 | * |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3861 | * Reason: Otherwise we should have running hashes for SHA512 and |
| 3862 | * SHA224 in order to satisfy 'weird' needs from the server |
| 3863 | * side. |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3864 | */ |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 3865 | if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3866 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3867 | md_alg = MBEDTLS_MD_SHA384; |
| 3868 | ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3869 | } |
| 3870 | else |
| 3871 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3872 | md_alg = MBEDTLS_MD_SHA256; |
| 3873 | ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3874 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3875 | ssl->out_msg[5] = mbedtls_ssl_sig_from_pk( mbedtls_ssl_own_key( ssl ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3876 | |
Manuel Pégourié-Gonnard | bfe32ef | 2013-08-22 14:55:30 +0200 | [diff] [blame] | 3877 | /* Info from md_alg will be used instead */ |
| 3878 | hashlen = 0; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3879 | offset = 2; |
| 3880 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 3881 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3882 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 3883 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3884 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3885 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 3886 | } |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3887 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3888 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 3889 | if( ssl->handshake->ecrs_enabled ) |
Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 3890 | rs_ctx = &ssl->handshake->ecrs_ctx.pk; |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 3891 | #endif |
| 3892 | |
| 3893 | if( ( ret = mbedtls_pk_sign_restartable( mbedtls_ssl_own_key( ssl ), |
| 3894 | md_alg, hash_start, hashlen, |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 3895 | ssl->out_msg + 6 + offset, &n, |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 3896 | ssl->conf->f_rng, ssl->conf->p_rng, rs_ctx ) ) != 0 ) |
Manuel Pégourié-Gonnard | 76c18a1 | 2013-08-20 16:50:40 +0200 | [diff] [blame] | 3897 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3898 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret ); |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3899 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 558da9c | 2018-06-13 12:02:12 +0200 | [diff] [blame] | 3900 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 3901 | ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS; |
| 3902 | #endif |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 3903 | return( ret ); |
Manuel Pégourié-Gonnard | 76c18a1 | 2013-08-20 16:50:40 +0200 | [diff] [blame] | 3904 | } |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3905 | |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3906 | ssl->out_msg[4 + offset] = (unsigned char)( n >> 8 ); |
| 3907 | ssl->out_msg[5 + offset] = (unsigned char)( n ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3908 | |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3909 | ssl->out_msglen = 6 + n + offset; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3910 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 3911 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3912 | |
| 3913 | ssl->state++; |
| 3914 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3915 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3916 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3917 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3918 | return( ret ); |
| 3919 | } |
| 3920 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3921 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3922 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 3923 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3924 | } |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3925 | #endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3926 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3927 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 3928 | static int ssl_parse_new_session_ticket( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3929 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3930 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3931 | uint32_t lifetime; |
| 3932 | size_t ticket_len; |
| 3933 | unsigned char *ticket; |
Manuel Pégourié-Gonnard | 000d5ae | 2014-09-10 21:52:12 +0200 | [diff] [blame] | 3934 | const unsigned char *msg; |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3935 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3936 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) ); |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3937 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 3938 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3939 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3940 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3941 | return( ret ); |
| 3942 | } |
| 3943 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3944 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3945 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3946 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) ); |
Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3947 | mbedtls_ssl_send_alert_message( |
| 3948 | ssl, |
| 3949 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3950 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3951 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3952 | } |
| 3953 | |
| 3954 | /* |
| 3955 | * struct { |
| 3956 | * uint32 ticket_lifetime_hint; |
| 3957 | * opaque ticket<0..2^16-1>; |
| 3958 | * } NewSessionTicket; |
| 3959 | * |
Manuel Pégourié-Gonnard | 000d5ae | 2014-09-10 21:52:12 +0200 | [diff] [blame] | 3960 | * 0 . 3 ticket_lifetime_hint |
| 3961 | * 4 . 5 ticket_len (n) |
| 3962 | * 6 . 5+n ticket content |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3963 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3964 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET || |
| 3965 | ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len( ssl ) ) |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3966 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3967 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 3968 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3969 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3970 | return( MBEDTLS_ERR_SSL_BAD_HS_NEW_SESSION_TICKET ); |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3971 | } |
| 3972 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3973 | msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ); |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3974 | |
Philippe Antoine | b5b2543 | 2018-05-11 11:06:29 +0200 | [diff] [blame] | 3975 | lifetime = ( ((uint32_t) msg[0]) << 24 ) | ( msg[1] << 16 ) | |
| 3976 | ( msg[2] << 8 ) | ( msg[3] ); |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3977 | |
Manuel Pégourié-Gonnard | 000d5ae | 2014-09-10 21:52:12 +0200 | [diff] [blame] | 3978 | ticket_len = ( msg[4] << 8 ) | ( msg[5] ); |
| 3979 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3980 | if( ticket_len + 6 + mbedtls_ssl_hs_hdr_len( ssl ) != ssl->in_hslen ) |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3981 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3982 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 3983 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3984 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3985 | return( MBEDTLS_ERR_SSL_BAD_HS_NEW_SESSION_TICKET ); |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3986 | } |
| 3987 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3988 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %d", ticket_len ) ); |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3989 | |
Manuel Pégourié-Gonnard | 7cd5924 | 2013-08-02 13:24:41 +0200 | [diff] [blame] | 3990 | /* We're not waiting for a NewSessionTicket message any more */ |
| 3991 | ssl->handshake->new_session_ticket = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3992 | ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC; |
Manuel Pégourié-Gonnard | 7cd5924 | 2013-08-02 13:24:41 +0200 | [diff] [blame] | 3993 | |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3994 | /* |
| 3995 | * Zero-length ticket means the server changed his mind and doesn't want |
| 3996 | * to send a ticket after all, so just forget it |
| 3997 | */ |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 3998 | if( ticket_len == 0 ) |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3999 | return( 0 ); |
| 4000 | |
Hanno Becker | b2964cb | 2019-01-30 14:46:35 +0000 | [diff] [blame] | 4001 | if( ssl->session != NULL && ssl->session->ticket != NULL ) |
| 4002 | { |
| 4003 | mbedtls_platform_zeroize( ssl->session->ticket, |
| 4004 | ssl->session->ticket_len ); |
| 4005 | mbedtls_free( ssl->session->ticket ); |
| 4006 | ssl->session->ticket = NULL; |
| 4007 | ssl->session->ticket_len = 0; |
| 4008 | } |
| 4009 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4010 | mbedtls_platform_zeroize( ssl->session_negotiate->ticket, |
| 4011 | ssl->session_negotiate->ticket_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4012 | mbedtls_free( ssl->session_negotiate->ticket ); |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4013 | ssl->session_negotiate->ticket = NULL; |
| 4014 | ssl->session_negotiate->ticket_len = 0; |
| 4015 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 4016 | if( ( ticket = mbedtls_calloc( 1, ticket_len ) ) == NULL ) |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4017 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 4018 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "ticket alloc failed" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 4019 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 4020 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 4021 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4022 | } |
| 4023 | |
Manuel Pégourié-Gonnard | 000d5ae | 2014-09-10 21:52:12 +0200 | [diff] [blame] | 4024 | memcpy( ticket, msg + 6, ticket_len ); |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4025 | |
| 4026 | ssl->session_negotiate->ticket = ticket; |
| 4027 | ssl->session_negotiate->ticket_len = ticket_len; |
| 4028 | ssl->session_negotiate->ticket_lifetime = lifetime; |
| 4029 | |
| 4030 | /* |
| 4031 | * RFC 5077 section 3.4: |
| 4032 | * "If the client receives a session ticket from the server, then it |
| 4033 | * discards any Session ID that was sent in the ServerHello." |
| 4034 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4035 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) ); |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 4036 | ssl->session_negotiate->id_len = 0; |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4037 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4038 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) ); |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4039 | |
| 4040 | return( 0 ); |
| 4041 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4042 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4043 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4044 | /* |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4045 | * SSL handshake -- client side -- single step |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4046 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4047 | int mbedtls_ssl_handshake_client_step( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4048 | { |
| 4049 | int ret = 0; |
| 4050 | |
Manuel Pégourié-Gonnard | dba460f | 2015-06-24 22:59:30 +0200 | [diff] [blame] | 4051 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4052 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4053 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4054 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4055 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4056 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4057 | return( ret ); |
| 4058 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4059 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4060 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4061 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4062 | { |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4063 | if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4064 | return( ret ); |
| 4065 | } |
Hanno Becker | bc2498a | 2018-08-28 10:13:29 +0100 | [diff] [blame] | 4066 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4067 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4068 | /* Change state now, so that it is right in mbedtls_ssl_read_record(), used |
Manuel Pégourié-Gonnard | cd32a50 | 2014-09-20 13:54:12 +0200 | [diff] [blame] | 4069 | * by DTLS for dropping out-of-sequence ChangeCipherSpec records */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4070 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 4071 | if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC && |
Manuel Pégourié-Gonnard | cd32a50 | 2014-09-20 13:54:12 +0200 | [diff] [blame] | 4072 | ssl->handshake->new_session_ticket != 0 ) |
| 4073 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4074 | ssl->state = MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET; |
Manuel Pégourié-Gonnard | cd32a50 | 2014-09-20 13:54:12 +0200 | [diff] [blame] | 4075 | } |
| 4076 | #endif |
| 4077 | |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4078 | switch( ssl->state ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4079 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4080 | case MBEDTLS_SSL_HELLO_REQUEST: |
| 4081 | ssl->state = MBEDTLS_SSL_CLIENT_HELLO; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4082 | break; |
| 4083 | |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4084 | /* |
| 4085 | * ==> ClientHello |
| 4086 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4087 | case MBEDTLS_SSL_CLIENT_HELLO: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4088 | ret = ssl_write_client_hello( ssl ); |
| 4089 | break; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4090 | |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4091 | /* |
| 4092 | * <== ServerHello |
| 4093 | * Certificate |
| 4094 | * ( ServerKeyExchange ) |
| 4095 | * ( CertificateRequest ) |
| 4096 | * ServerHelloDone |
| 4097 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4098 | case MBEDTLS_SSL_SERVER_HELLO: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4099 | ret = ssl_parse_server_hello( ssl ); |
| 4100 | break; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4101 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4102 | case MBEDTLS_SSL_SERVER_CERTIFICATE: |
| 4103 | ret = mbedtls_ssl_parse_certificate( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4104 | break; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4105 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4106 | case MBEDTLS_SSL_SERVER_KEY_EXCHANGE: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4107 | ret = ssl_parse_server_key_exchange( ssl ); |
| 4108 | break; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4109 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4110 | case MBEDTLS_SSL_CERTIFICATE_REQUEST: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4111 | ret = ssl_parse_certificate_request( ssl ); |
| 4112 | break; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4113 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4114 | case MBEDTLS_SSL_SERVER_HELLO_DONE: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4115 | ret = ssl_parse_server_hello_done( ssl ); |
| 4116 | break; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4117 | |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4118 | /* |
| 4119 | * ==> ( Certificate/Alert ) |
| 4120 | * ClientKeyExchange |
| 4121 | * ( CertificateVerify ) |
| 4122 | * ChangeCipherSpec |
| 4123 | * Finished |
| 4124 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4125 | case MBEDTLS_SSL_CLIENT_CERTIFICATE: |
| 4126 | ret = mbedtls_ssl_write_certificate( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4127 | break; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4128 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4129 | case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4130 | ret = ssl_write_client_key_exchange( ssl ); |
| 4131 | break; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4132 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4133 | case MBEDTLS_SSL_CERTIFICATE_VERIFY: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4134 | ret = ssl_write_certificate_verify( ssl ); |
| 4135 | break; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4136 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4137 | case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC: |
| 4138 | ret = mbedtls_ssl_write_change_cipher_spec( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4139 | break; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4140 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4141 | case MBEDTLS_SSL_CLIENT_FINISHED: |
| 4142 | ret = mbedtls_ssl_write_finished( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4143 | break; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4144 | |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4145 | /* |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4146 | * <== ( NewSessionTicket ) |
| 4147 | * ChangeCipherSpec |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4148 | * Finished |
| 4149 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4150 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 4151 | case MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET: |
Manuel Pégourié-Gonnard | cd32a50 | 2014-09-20 13:54:12 +0200 | [diff] [blame] | 4152 | ret = ssl_parse_new_session_ticket( ssl ); |
| 4153 | break; |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 4154 | #endif |
Manuel Pégourié-Gonnard | cd32a50 | 2014-09-20 13:54:12 +0200 | [diff] [blame] | 4155 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4156 | case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC: |
| 4157 | ret = mbedtls_ssl_parse_change_cipher_spec( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4158 | break; |
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 | case MBEDTLS_SSL_SERVER_FINISHED: |
| 4161 | ret = mbedtls_ssl_parse_finished( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4162 | break; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4163 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4164 | case MBEDTLS_SSL_FLUSH_BUFFERS: |
| 4165 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) ); |
| 4166 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4167 | break; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4168 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4169 | case MBEDTLS_SSL_HANDSHAKE_WRAPUP: |
| 4170 | mbedtls_ssl_handshake_wrapup( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4171 | break; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4172 | |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4173 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4174 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) ); |
| 4175 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4176 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4177 | |
| 4178 | return( ret ); |
| 4179 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4180 | #endif /* MBEDTLS_SSL_CLI_C */ |