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