| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* | 
|  | 2 | *  SSLv3/TLSv1 client-side functions | 
|  | 3 | * | 
| Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | *  Copyright The Mbed TLS Contributors | 
| 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 | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 18 | */ | 
|  | 19 |  | 
| Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 20 | #include "common.h" | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 21 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 22 | #if defined(MBEDTLS_SSL_CLI_C) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 23 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 24 | #if defined(MBEDTLS_PLATFORM_C) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 25 | #include "mbedtls/platform.h" | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 26 | #else | 
| Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 27 | #include <stdlib.h> | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 28 | #define mbedtls_calloc    calloc | 
| SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 29 | #define mbedtls_free      free | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 30 | #endif | 
|  | 31 |  | 
| SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 32 | #include "mbedtls/ssl.h" | 
|  | 33 | #include "mbedtls/ssl_internal.h" | 
| Janos Follath | 73c616b | 2019-12-18 15:07:04 +0000 | [diff] [blame] | 34 | #include "mbedtls/debug.h" | 
|  | 35 | #include "mbedtls/error.h" | 
| SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 36 |  | 
| Hanno Becker | bb89e27 | 2019-01-08 12:54:37 +0000 | [diff] [blame] | 37 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 38 | #include "mbedtls/psa_util.h" | 
|  | 39 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 40 |  | 
| SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 41 | #include <string.h> | 
|  | 42 |  | 
| Manuel Pégourié-Gonnard | 9386664 | 2015-06-22 19:21:23 +0200 | [diff] [blame] | 43 | #include <stdint.h> | 
| Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 44 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 45 | #if defined(MBEDTLS_HAVE_TIME) | 
| Simon Butcher | b5b6af2 | 2016-07-13 14:46:18 +0100 | [diff] [blame] | 46 | #include "mbedtls/platform_time.h" | 
| Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 47 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 48 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 49 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 50 | #include "mbedtls/platform_util.h" | 
| Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 51 | #endif | 
|  | 52 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 53 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) | 
| Hanno Becker | 520224e | 2018-10-26 11:38:07 +0100 | [diff] [blame] | 54 | static int ssl_conf_has_static_psk( mbedtls_ssl_config const *conf ) | 
| Hanno Becker | 2e4f616 | 2018-10-23 11:54:44 +0100 | [diff] [blame] | 55 | { | 
|  | 56 | if( conf->psk_identity     == NULL || | 
|  | 57 | conf->psk_identity_len == 0     ) | 
|  | 58 | { | 
|  | 59 | return( 0 ); | 
|  | 60 | } | 
|  | 61 |  | 
|  | 62 | if( conf->psk != NULL && conf->psk_len != 0 ) | 
|  | 63 | return( 1 ); | 
|  | 64 |  | 
|  | 65 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 66 | if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) | 
| Hanno Becker | 2e4f616 | 2018-10-23 11:54:44 +0100 | [diff] [blame] | 67 | return( 1 ); | 
|  | 68 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 69 |  | 
|  | 70 | return( 0 ); | 
|  | 71 | } | 
| Hanno Becker | dfab8e2 | 2018-10-23 11:59:34 +0100 | [diff] [blame] | 72 |  | 
|  | 73 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Hanno Becker | 520224e | 2018-10-26 11:38:07 +0100 | [diff] [blame] | 74 | 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] | 75 | { | 
|  | 76 | if( conf->psk_identity     == NULL || | 
|  | 77 | conf->psk_identity_len == 0     ) | 
|  | 78 | { | 
|  | 79 | return( 0 ); | 
|  | 80 | } | 
|  | 81 |  | 
|  | 82 | if( conf->psk != NULL && conf->psk_len != 0 ) | 
|  | 83 | return( 1 ); | 
|  | 84 |  | 
|  | 85 | return( 0 ); | 
|  | 86 | } | 
|  | 87 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 88 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 89 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ | 
| Hanno Becker | 2e4f616 | 2018-10-23 11:54:44 +0100 | [diff] [blame] | 90 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 91 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 92 | static int ssl_write_hostname_ext( mbedtls_ssl_context *ssl, | 
|  | 93 | unsigned char *buf, | 
|  | 94 | const unsigned char *end, | 
|  | 95 | size_t *olen ) | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 96 | { | 
|  | 97 | unsigned char *p = buf; | 
| Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 98 | size_t hostname_len; | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 99 |  | 
|  | 100 | *olen = 0; | 
|  | 101 |  | 
| Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 102 | if( ssl->hostname == NULL ) | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 103 | return( 0 ); | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 104 |  | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 105 | MBEDTLS_SSL_DEBUG_MSG( 3, | 
|  | 106 | ( "client hello, adding server name extension: %s", | 
|  | 107 | ssl->hostname ) ); | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 108 |  | 
| Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 109 | hostname_len = strlen( ssl->hostname ); | 
|  | 110 |  | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 111 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, hostname_len + 9 ); | 
| Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 112 |  | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 113 | /* | 
| Hanno Becker | 1a9a51c | 2017-04-07 13:02:16 +0100 | [diff] [blame] | 114 | * Sect. 3, RFC 6066 (TLS Extensions Definitions) | 
|  | 115 | * | 
|  | 116 | * In order to provide any of the server names, clients MAY include an | 
|  | 117 | * extension of type "server_name" in the (extended) client hello. The | 
|  | 118 | * "extension_data" field of this extension SHALL contain | 
|  | 119 | * "ServerNameList" where: | 
|  | 120 | * | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 121 | * struct { | 
|  | 122 | *     NameType name_type; | 
|  | 123 | *     select (name_type) { | 
|  | 124 | *         case host_name: HostName; | 
|  | 125 | *     } name; | 
|  | 126 | * } ServerName; | 
|  | 127 | * | 
|  | 128 | * enum { | 
|  | 129 | *     host_name(0), (255) | 
|  | 130 | * } NameType; | 
|  | 131 | * | 
|  | 132 | * opaque HostName<1..2^16-1>; | 
|  | 133 | * | 
|  | 134 | * struct { | 
|  | 135 | *     ServerName server_name_list<1..2^16-1> | 
|  | 136 | * } ServerNameList; | 
| Hanno Becker | 1a9a51c | 2017-04-07 13:02:16 +0100 | [diff] [blame] | 137 | * | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 138 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 139 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SERVERNAME >> 8 ) & 0xFF ); | 
|  | 140 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SERVERNAME      ) & 0xFF ); | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 141 |  | 
| Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 142 | *p++ = (unsigned char)( ( (hostname_len + 5) >> 8 ) & 0xFF ); | 
|  | 143 | *p++ = (unsigned char)( ( (hostname_len + 5)      ) & 0xFF ); | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 144 |  | 
| Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 145 | *p++ = (unsigned char)( ( (hostname_len + 3) >> 8 ) & 0xFF ); | 
|  | 146 | *p++ = (unsigned char)( ( (hostname_len + 3)      ) & 0xFF ); | 
| 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_HOSTNAME ) & 0xFF ); | 
| Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 149 | *p++ = (unsigned char)( ( hostname_len >> 8 ) & 0xFF ); | 
|  | 150 | *p++ = (unsigned char)( ( hostname_len      ) & 0xFF ); | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 151 |  | 
| Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 152 | memcpy( p, ssl->hostname, hostname_len ); | 
| 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 | *olen = hostname_len + 9; | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 155 |  | 
|  | 156 | return( 0 ); | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 157 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 158 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 159 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 160 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 161 | static int ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl, | 
|  | 162 | unsigned char *buf, | 
|  | 163 | const unsigned char *end, | 
|  | 164 | size_t *olen ) | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 165 | { | 
|  | 166 | unsigned char *p = buf; | 
|  | 167 |  | 
|  | 168 | *olen = 0; | 
|  | 169 |  | 
| Hanno Becker | 40f8b51 | 2017-10-12 14:58:55 +0100 | [diff] [blame] | 170 | /* We're always including an TLS_EMPTY_RENEGOTIATION_INFO_SCSV in the | 
|  | 171 | * initial ClientHello, in which case also adding the renegotiation | 
|  | 172 | * 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] | 173 | if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 174 | return( 0 ); | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 175 |  | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 176 | MBEDTLS_SSL_DEBUG_MSG( 3, | 
|  | 177 | ( "client hello, adding renegotiation extension" ) ); | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 178 |  | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 179 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 + ssl->verify_data_len ); | 
| Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 180 |  | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 181 | /* | 
|  | 182 | * Secure renegotiation | 
|  | 183 | */ | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 184 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO >> 8 ) | 
|  | 185 | & 0xFF ); | 
|  | 186 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO      ) | 
|  | 187 | & 0xFF ); | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 188 |  | 
|  | 189 | *p++ = 0x00; | 
|  | 190 | *p++ = ( ssl->verify_data_len + 1 ) & 0xFF; | 
|  | 191 | *p++ = ssl->verify_data_len & 0xFF; | 
|  | 192 |  | 
|  | 193 | memcpy( p, ssl->own_verify_data, ssl->verify_data_len ); | 
|  | 194 |  | 
|  | 195 | *olen = 5 + ssl->verify_data_len; | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 196 |  | 
|  | 197 | return( 0 ); | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 198 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 199 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 200 |  | 
| Manuel Pégourié-Gonnard | d942323 | 2014-12-02 11:57:29 +0100 | [diff] [blame] | 201 | /* | 
|  | 202 | * Only if we handle at least one key exchange that needs signatures. | 
|  | 203 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 204 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 205 | defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 206 | static int ssl_write_signature_algorithms_ext( mbedtls_ssl_context *ssl, | 
|  | 207 | unsigned char *buf, | 
|  | 208 | const unsigned char *end, | 
|  | 209 | size_t *olen ) | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 210 | { | 
|  | 211 | unsigned char *p = buf; | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 212 | size_t sig_alg_len = 0; | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 213 | const int *md; | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 214 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 215 | #if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) | 
| Manuel Pégourié-Gonnard | 5bfd968 | 2014-06-24 15:18:11 +0200 | [diff] [blame] | 216 | unsigned char *sig_alg_list = buf + 6; | 
|  | 217 | #endif | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 218 |  | 
|  | 219 | *olen = 0; | 
|  | 220 |  | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 221 | if( ssl->conf->max_minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 ) | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 222 | return( 0 ); | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 223 |  | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 224 | MBEDTLS_SSL_DEBUG_MSG( 3, | 
|  | 225 | ( "client hello, adding signature_algorithms extension" ) ); | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 226 |  | 
| Hanno Becker | e131bfe | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 227 | if( ssl->conf->sig_hashes == NULL ) | 
|  | 228 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); | 
|  | 229 |  | 
| Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 230 | for( md = ssl->conf->sig_hashes; *md != MBEDTLS_MD_NONE; md++ ) | 
|  | 231 | { | 
|  | 232 | #if defined(MBEDTLS_ECDSA_C) | 
|  | 233 | sig_alg_len += 2; | 
|  | 234 | #endif | 
|  | 235 | #if defined(MBEDTLS_RSA_C) | 
|  | 236 | sig_alg_len += 2; | 
|  | 237 | #endif | 
| Hanno Becker | e131bfe | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 238 | if( sig_alg_len > MBEDTLS_SSL_MAX_SIG_HASH_ALG_LIST_LEN ) | 
|  | 239 | { | 
|  | 240 | MBEDTLS_SSL_DEBUG_MSG( 3, | 
|  | 241 | ( "length in bytes of sig-hash-alg extension too big" ) ); | 
|  | 242 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); | 
|  | 243 | } | 
| Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 244 | } | 
|  | 245 |  | 
| Hanno Becker | e131bfe | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 246 | /* Empty signature algorithms list, this is a configuration error. */ | 
|  | 247 | if( sig_alg_len == 0 ) | 
|  | 248 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); | 
|  | 249 |  | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 250 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, sig_alg_len + 6 ); | 
| Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 251 |  | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 252 | /* | 
|  | 253 | * Prepare signature_algorithms extension (TLS 1.2) | 
|  | 254 | */ | 
| Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 255 | sig_alg_len = 0; | 
|  | 256 |  | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 257 | for( md = ssl->conf->sig_hashes; *md != MBEDTLS_MD_NONE; md++ ) | 
|  | 258 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 259 | #if defined(MBEDTLS_ECDSA_C) | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 260 | sig_alg_list[sig_alg_len++] = mbedtls_ssl_hash_from_md_alg( *md ); | 
|  | 261 | sig_alg_list[sig_alg_len++] = MBEDTLS_SSL_SIG_ECDSA; | 
| Manuel Pégourié-Gonnard | d11eb7c | 2013-08-22 15:57:15 +0200 | [diff] [blame] | 262 | #endif | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 263 | #if defined(MBEDTLS_RSA_C) | 
|  | 264 | sig_alg_list[sig_alg_len++] = mbedtls_ssl_hash_from_md_alg( *md ); | 
|  | 265 | sig_alg_list[sig_alg_len++] = MBEDTLS_SSL_SIG_RSA; | 
| Manuel Pégourié-Gonnard | d11eb7c | 2013-08-22 15:57:15 +0200 | [diff] [blame] | 266 | #endif | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 267 | } | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 268 |  | 
|  | 269 | /* | 
|  | 270 | * enum { | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 271 | *     none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), | 
|  | 272 | *     sha512(6), (255) | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 273 | * } HashAlgorithm; | 
|  | 274 | * | 
|  | 275 | * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } | 
|  | 276 | *   SignatureAlgorithm; | 
|  | 277 | * | 
|  | 278 | * struct { | 
|  | 279 | *     HashAlgorithm hash; | 
|  | 280 | *     SignatureAlgorithm signature; | 
|  | 281 | * } SignatureAndHashAlgorithm; | 
|  | 282 | * | 
|  | 283 | * SignatureAndHashAlgorithm | 
|  | 284 | *   supported_signature_algorithms<2..2^16-2>; | 
|  | 285 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 286 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SIG_ALG >> 8 ) & 0xFF ); | 
|  | 287 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SIG_ALG      ) & 0xFF ); | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 288 |  | 
|  | 289 | *p++ = (unsigned char)( ( ( sig_alg_len + 2 ) >> 8 ) & 0xFF ); | 
|  | 290 | *p++ = (unsigned char)( ( ( sig_alg_len + 2 )      ) & 0xFF ); | 
|  | 291 |  | 
|  | 292 | *p++ = (unsigned char)( ( sig_alg_len >> 8 ) & 0xFF ); | 
|  | 293 | *p++ = (unsigned char)( ( sig_alg_len      ) & 0xFF ); | 
|  | 294 |  | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 295 | *olen = 6 + sig_alg_len; | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 296 |  | 
|  | 297 | return( 0 ); | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 298 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 299 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 300 | MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 301 |  | 
| Manuel Pégourié-Gonnard | f472179 | 2015-09-15 10:53:51 +0200 | [diff] [blame] | 302 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ | 
| Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 303 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 304 | static int ssl_write_supported_elliptic_curves_ext( mbedtls_ssl_context *ssl, | 
|  | 305 | unsigned char *buf, | 
|  | 306 | const unsigned char *end, | 
|  | 307 | size_t *olen ) | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 308 | { | 
|  | 309 | unsigned char *p = buf; | 
| Manuel Pégourié-Gonnard | 8e205fc | 2014-01-23 17:27:10 +0100 | [diff] [blame] | 310 | unsigned char *elliptic_curve_list = p + 6; | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 311 | size_t elliptic_curve_len = 0; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 312 | const mbedtls_ecp_curve_info *info; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 313 | const mbedtls_ecp_group_id *grp_id; | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 314 |  | 
|  | 315 | *olen = 0; | 
|  | 316 |  | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 317 | MBEDTLS_SSL_DEBUG_MSG( 3, | 
|  | 318 | ( "client hello, adding supported_elliptic_curves extension" ) ); | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 319 |  | 
| Hanno Becker | e131bfe | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 320 | if( ssl->conf->curve_list == NULL ) | 
|  | 321 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); | 
|  | 322 |  | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 323 | for( grp_id = ssl->conf->curve_list; | 
|  | 324 | *grp_id != MBEDTLS_ECP_DP_NONE; | 
|  | 325 | grp_id++ ) | 
| Manuel Pégourié-Gonnard | cd49f76 | 2014-02-04 15:14:13 +0100 | [diff] [blame] | 326 | { | 
| Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 327 | info = mbedtls_ecp_curve_info_from_grp_id( *grp_id ); | 
| Janos Follath | 8a31705 | 2016-04-21 23:37:09 +0100 | [diff] [blame] | 328 | if( info == NULL ) | 
|  | 329 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 330 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 331 | ( "invalid curve in ssl configuration" ) ); | 
| Hanno Becker | e131bfe | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 332 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); | 
| Janos Follath | 8a31705 | 2016-04-21 23:37:09 +0100 | [diff] [blame] | 333 | } | 
| Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 334 | elliptic_curve_len += 2; | 
| Hanno Becker | e131bfe | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 335 |  | 
|  | 336 | if( elliptic_curve_len > MBEDTLS_SSL_MAX_CURVE_LIST_LEN ) | 
|  | 337 | { | 
|  | 338 | MBEDTLS_SSL_DEBUG_MSG( 3, | 
|  | 339 | ( "malformed supported_elliptic_curves extension in config" ) ); | 
|  | 340 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); | 
|  | 341 | } | 
| Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 342 | } | 
|  | 343 |  | 
| Hanno Becker | e131bfe | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 344 | /* Empty elliptic curve list, this is a configuration error. */ | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 345 | if( elliptic_curve_len == 0 ) | 
| Hanno Becker | e131bfe | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 346 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 347 |  | 
|  | 348 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 + elliptic_curve_len ); | 
| Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 349 |  | 
|  | 350 | elliptic_curve_len = 0; | 
|  | 351 |  | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 352 | for( grp_id = ssl->conf->curve_list; | 
|  | 353 | *grp_id != MBEDTLS_ECP_DP_NONE; | 
|  | 354 | grp_id++ ) | 
| Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 355 | { | 
| Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 356 | info = mbedtls_ecp_curve_info_from_grp_id( *grp_id ); | 
| Manuel Pégourié-Gonnard | cd49f76 | 2014-02-04 15:14:13 +0100 | [diff] [blame] | 357 | elliptic_curve_list[elliptic_curve_len++] = info->tls_id >> 8; | 
|  | 358 | elliptic_curve_list[elliptic_curve_len++] = info->tls_id & 0xFF; | 
| Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 359 | } | 
| Paul Bakker | 5dc6b5f | 2013-06-29 23:26:34 +0200 | [diff] [blame] | 360 |  | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 361 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES >> 8 ) | 
|  | 362 | & 0xFF ); | 
|  | 363 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES      ) | 
|  | 364 | & 0xFF ); | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 365 |  | 
|  | 366 | *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) >> 8 ) & 0xFF ); | 
|  | 367 | *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 )      ) & 0xFF ); | 
|  | 368 |  | 
|  | 369 | *p++ = (unsigned char)( ( ( elliptic_curve_len     ) >> 8 ) & 0xFF ); | 
|  | 370 | *p++ = (unsigned char)( ( ( elliptic_curve_len     )      ) & 0xFF ); | 
|  | 371 |  | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 372 | *olen = 6 + elliptic_curve_len; | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 373 |  | 
|  | 374 | return( 0 ); | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 375 | } | 
|  | 376 |  | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 377 | static int ssl_write_supported_point_formats_ext( mbedtls_ssl_context *ssl, | 
|  | 378 | unsigned char *buf, | 
|  | 379 | const unsigned char *end, | 
|  | 380 | size_t *olen ) | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 381 | { | 
|  | 382 | unsigned char *p = buf; | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 383 | (void) ssl; /* ssl used for debugging only */ | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 384 |  | 
|  | 385 | *olen = 0; | 
|  | 386 |  | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 387 | MBEDTLS_SSL_DEBUG_MSG( 3, | 
|  | 388 | ( "client hello, adding supported_point_formats extension" ) ); | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 389 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 ); | 
| Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 390 |  | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 391 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) | 
|  | 392 | & 0xFF ); | 
|  | 393 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS      ) | 
|  | 394 | & 0xFF ); | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 395 |  | 
|  | 396 | *p++ = 0x00; | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 397 | *p++ = 2; | 
| Manuel Pégourié-Gonnard | 6b8846d | 2013-08-15 17:42:02 +0200 | [diff] [blame] | 398 |  | 
|  | 399 | *p++ = 1; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 400 | *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED; | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 401 |  | 
| Manuel Pégourié-Gonnard | 6b8846d | 2013-08-15 17:42:02 +0200 | [diff] [blame] | 402 | *olen = 6; | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 403 |  | 
|  | 404 | return( 0 ); | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 405 | } | 
| Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 406 | #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || | 
| Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 407 | MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 408 |  | 
| Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 409 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 410 | static int ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl, | 
|  | 411 | unsigned char *buf, | 
|  | 412 | const unsigned char *end, | 
|  | 413 | size_t *olen ) | 
| Manuel Pégourié-Gonnard | 294139b | 2015-09-15 16:55:05 +0200 | [diff] [blame] | 414 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 415 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | 294139b | 2015-09-15 16:55:05 +0200 | [diff] [blame] | 416 | unsigned char *p = buf; | 
| Manuel Pégourié-Gonnard | 294139b | 2015-09-15 16:55:05 +0200 | [diff] [blame] | 417 | size_t kkpp_len; | 
|  | 418 |  | 
|  | 419 | *olen = 0; | 
|  | 420 |  | 
|  | 421 | /* Skip costly extension if we can't use EC J-PAKE anyway */ | 
|  | 422 | if( mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 ) | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 423 | return( 0 ); | 
| Manuel Pégourié-Gonnard | 294139b | 2015-09-15 16:55:05 +0200 | [diff] [blame] | 424 |  | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 425 | MBEDTLS_SSL_DEBUG_MSG( 3, | 
|  | 426 | ( "client hello, adding ecjpake_kkpp extension" ) ); | 
| Manuel Pégourié-Gonnard | 294139b | 2015-09-15 16:55:05 +0200 | [diff] [blame] | 427 |  | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 428 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 ); | 
| Manuel Pégourié-Gonnard | 294139b | 2015-09-15 16:55:05 +0200 | [diff] [blame] | 429 |  | 
|  | 430 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP >> 8 ) & 0xFF ); | 
|  | 431 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP      ) & 0xFF ); | 
|  | 432 |  | 
| Manuel Pégourié-Gonnard | d0d8cb3 | 2015-09-17 14:16:30 +0200 | [diff] [blame] | 433 | /* | 
|  | 434 | * We may need to send ClientHello multiple times for Hello verification. | 
|  | 435 | * We don't want to compute fresh values every time (both for performance | 
|  | 436 | * and consistency reasons), so cache the extension content. | 
|  | 437 | */ | 
|  | 438 | if( ssl->handshake->ecjpake_cache == NULL || | 
|  | 439 | ssl->handshake->ecjpake_cache_len == 0 ) | 
| Manuel Pégourié-Gonnard | 294139b | 2015-09-15 16:55:05 +0200 | [diff] [blame] | 440 | { | 
| Manuel Pégourié-Gonnard | d0d8cb3 | 2015-09-17 14:16:30 +0200 | [diff] [blame] | 441 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "generating new ecjpake parameters" ) ); | 
|  | 442 |  | 
| Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 443 | ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx, | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 444 | p + 2, end - p - 2, &kkpp_len, | 
|  | 445 | ssl->conf->f_rng, ssl->conf->p_rng ); | 
| Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 446 | if( ret != 0 ) | 
| Manuel Pégourié-Gonnard | d0d8cb3 | 2015-09-17 14:16:30 +0200 | [diff] [blame] | 447 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 448 | MBEDTLS_SSL_DEBUG_RET( 1 , | 
|  | 449 | "mbedtls_ecjpake_write_round_one", ret ); | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 450 | return( ret ); | 
| Manuel Pégourié-Gonnard | d0d8cb3 | 2015-09-17 14:16:30 +0200 | [diff] [blame] | 451 | } | 
|  | 452 |  | 
|  | 453 | ssl->handshake->ecjpake_cache = mbedtls_calloc( 1, kkpp_len ); | 
|  | 454 | if( ssl->handshake->ecjpake_cache == NULL ) | 
|  | 455 | { | 
|  | 456 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "allocation failed" ) ); | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 457 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
| Manuel Pégourié-Gonnard | d0d8cb3 | 2015-09-17 14:16:30 +0200 | [diff] [blame] | 458 | } | 
|  | 459 |  | 
|  | 460 | memcpy( ssl->handshake->ecjpake_cache, p + 2, kkpp_len ); | 
|  | 461 | ssl->handshake->ecjpake_cache_len = kkpp_len; | 
|  | 462 | } | 
|  | 463 | else | 
|  | 464 | { | 
|  | 465 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "re-using cached ecjpake parameters" ) ); | 
|  | 466 |  | 
|  | 467 | kkpp_len = ssl->handshake->ecjpake_cache_len; | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 468 | MBEDTLS_SSL_CHK_BUF_PTR( p + 2, end, kkpp_len ); | 
| Manuel Pégourié-Gonnard | d0d8cb3 | 2015-09-17 14:16:30 +0200 | [diff] [blame] | 469 |  | 
|  | 470 | memcpy( p + 2, ssl->handshake->ecjpake_cache, kkpp_len ); | 
| Manuel Pégourié-Gonnard | 294139b | 2015-09-15 16:55:05 +0200 | [diff] [blame] | 471 | } | 
|  | 472 |  | 
|  | 473 | *p++ = (unsigned char)( ( kkpp_len >> 8 ) & 0xFF ); | 
|  | 474 | *p++ = (unsigned char)( ( kkpp_len      ) & 0xFF ); | 
|  | 475 |  | 
|  | 476 | *olen = kkpp_len + 4; | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 477 |  | 
|  | 478 | return( 0 ); | 
| Manuel Pégourié-Gonnard | 294139b | 2015-09-15 16:55:05 +0200 | [diff] [blame] | 479 | } | 
| Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 480 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ | 
| Paul Bakker | c3f177a | 2012-04-11 16:11:49 +0000 | [diff] [blame] | 481 |  | 
| Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 482 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 483 | static int ssl_write_cid_ext( mbedtls_ssl_context *ssl, | 
|  | 484 | unsigned char *buf, | 
|  | 485 | const unsigned char *end, | 
|  | 486 | size_t *olen ) | 
| Hanno Becker | 49770ff | 2019-04-25 16:55:15 +0100 | [diff] [blame] | 487 | { | 
|  | 488 | unsigned char *p = buf; | 
|  | 489 | size_t ext_len; | 
| Hanno Becker | 49770ff | 2019-04-25 16:55:15 +0100 | [diff] [blame] | 490 |  | 
|  | 491 | /* | 
| Hanno Becker | ebcc913 | 2019-05-15 10:26:32 +0100 | [diff] [blame] | 492 | * Quoting draft-ietf-tls-dtls-connection-id-05 | 
|  | 493 | * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05 | 
| Hanno Becker | 49770ff | 2019-04-25 16:55:15 +0100 | [diff] [blame] | 494 | * | 
|  | 495 | *   struct { | 
|  | 496 | *      opaque cid<0..2^8-1>; | 
|  | 497 | *   } ConnectionId; | 
|  | 498 | */ | 
|  | 499 |  | 
|  | 500 | *olen = 0; | 
|  | 501 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || | 
|  | 502 | ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED ) | 
|  | 503 | { | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 504 | return( 0 ); | 
| Hanno Becker | 49770ff | 2019-04-25 16:55:15 +0100 | [diff] [blame] | 505 | } | 
|  | 506 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding CID extension" ) ); | 
|  | 507 |  | 
|  | 508 | /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX | 
|  | 509 | * which is at most 255, so the increment cannot overflow. */ | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 510 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, (unsigned)( ssl->own_cid_len + 5 ) ); | 
| Hanno Becker | 49770ff | 2019-04-25 16:55:15 +0100 | [diff] [blame] | 511 |  | 
|  | 512 | /* Add extension ID + size */ | 
|  | 513 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_CID >> 8 ) & 0xFF ); | 
|  | 514 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_CID      ) & 0xFF ); | 
|  | 515 | ext_len = (size_t) ssl->own_cid_len + 1; | 
|  | 516 | *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF ); | 
|  | 517 | *p++ = (unsigned char)( ( ext_len      ) & 0xFF ); | 
|  | 518 |  | 
|  | 519 | *p++ = (uint8_t) ssl->own_cid_len; | 
|  | 520 | memcpy( p, ssl->own_cid, ssl->own_cid_len ); | 
|  | 521 |  | 
|  | 522 | *olen = ssl->own_cid_len + 5; | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 523 |  | 
|  | 524 | return( 0 ); | 
| Hanno Becker | 49770ff | 2019-04-25 16:55:15 +0100 | [diff] [blame] | 525 | } | 
| Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 526 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
| Hanno Becker | 49770ff | 2019-04-25 16:55:15 +0100 | [diff] [blame] | 527 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 528 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 529 | static int ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl, | 
|  | 530 | unsigned char *buf, | 
|  | 531 | const unsigned char *end, | 
|  | 532 | size_t *olen ) | 
| Manuel Pégourié-Gonnard | a052849 | 2013-07-16 17:26:28 +0200 | [diff] [blame] | 533 | { | 
|  | 534 | unsigned char *p = buf; | 
|  | 535 |  | 
| Simon Butcher | 0fc94e9 | 2015-09-28 20:52:04 +0100 | [diff] [blame] | 536 | *olen = 0; | 
|  | 537 |  | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 538 | if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ) | 
|  | 539 | return( 0 ); | 
| Manuel Pégourié-Gonnard | a052849 | 2013-07-16 17:26:28 +0200 | [diff] [blame] | 540 |  | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 541 | MBEDTLS_SSL_DEBUG_MSG( 3, | 
|  | 542 | ( "client hello, adding max_fragment_length extension" ) ); | 
| Manuel Pégourié-Gonnard | a052849 | 2013-07-16 17:26:28 +0200 | [diff] [blame] | 543 |  | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 544 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 ); | 
| Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 545 |  | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 546 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) | 
|  | 547 | & 0xFF ); | 
|  | 548 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH      ) | 
|  | 549 | & 0xFF ); | 
| Manuel Pégourié-Gonnard | a052849 | 2013-07-16 17:26:28 +0200 | [diff] [blame] | 550 |  | 
|  | 551 | *p++ = 0x00; | 
|  | 552 | *p++ = 1; | 
|  | 553 |  | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 554 | *p++ = ssl->conf->mfl_code; | 
| Manuel Pégourié-Gonnard | a052849 | 2013-07-16 17:26:28 +0200 | [diff] [blame] | 555 |  | 
|  | 556 | *olen = 5; | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 557 |  | 
|  | 558 | return( 0 ); | 
| Manuel Pégourié-Gonnard | a052849 | 2013-07-16 17:26:28 +0200 | [diff] [blame] | 559 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 560 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ | 
| Manuel Pégourié-Gonnard | a052849 | 2013-07-16 17:26:28 +0200 | [diff] [blame] | 561 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 562 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 563 | static int ssl_write_truncated_hmac_ext( mbedtls_ssl_context *ssl, | 
|  | 564 | unsigned char *buf, | 
|  | 565 | const unsigned char *end, | 
|  | 566 | size_t *olen ) | 
| Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 567 | { | 
|  | 568 | unsigned char *p = buf; | 
|  | 569 |  | 
| Simon Butcher | 0fc94e9 | 2015-09-28 20:52:04 +0100 | [diff] [blame] | 570 | *olen = 0; | 
|  | 571 |  | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 572 | if( ssl->conf->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_DISABLED ) | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 573 | return( 0 ); | 
| Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 574 |  | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 575 | MBEDTLS_SSL_DEBUG_MSG( 3, | 
|  | 576 | ( "client hello, adding truncated_hmac extension" ) ); | 
| Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 577 |  | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 578 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 ); | 
| Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 579 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 580 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF ); | 
|  | 581 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_TRUNCATED_HMAC      ) & 0xFF ); | 
| Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 582 |  | 
|  | 583 | *p++ = 0x00; | 
|  | 584 | *p++ = 0x00; | 
|  | 585 |  | 
|  | 586 | *olen = 4; | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 587 |  | 
|  | 588 | return( 0 ); | 
| Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 589 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 590 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ | 
| Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 591 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 592 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 593 | static int ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl, | 
|  | 594 | unsigned char *buf, | 
|  | 595 | const unsigned char *end, | 
|  | 596 | size_t *olen ) | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 597 | { | 
|  | 598 | unsigned char *p = buf; | 
|  | 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 ) | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 604 | return( 0 ); | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 605 |  | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 606 | MBEDTLS_SSL_DEBUG_MSG( 3, | 
|  | 607 | ( "client hello, adding encrypt_then_mac extension" ) ); | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 608 |  | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 609 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 ); | 
| Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 610 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 611 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF ); | 
|  | 612 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC      ) & 0xFF ); | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 613 |  | 
|  | 614 | *p++ = 0x00; | 
|  | 615 | *p++ = 0x00; | 
|  | 616 |  | 
|  | 617 | *olen = 4; | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 618 |  | 
|  | 619 | return( 0 ); | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 620 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 621 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 622 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 623 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 624 | static int ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl, | 
|  | 625 | unsigned char *buf, | 
|  | 626 | const unsigned char *end, | 
|  | 627 | size_t *olen ) | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 628 | { | 
|  | 629 | unsigned char *p = buf; | 
|  | 630 |  | 
| Simon Butcher | 0fc94e9 | 2015-09-28 20:52:04 +0100 | [diff] [blame] | 631 | *olen = 0; | 
|  | 632 |  | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 633 | if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED || | 
|  | 634 | ssl->conf->max_minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 635 | return( 0 ); | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 636 |  | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 637 | MBEDTLS_SSL_DEBUG_MSG( 3, | 
|  | 638 | ( "client hello, adding extended_master_secret extension" ) ); | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 639 |  | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 640 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 ); | 
| Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 641 |  | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 642 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) | 
|  | 643 | & 0xFF ); | 
|  | 644 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET      ) | 
|  | 645 | & 0xFF ); | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 646 |  | 
|  | 647 | *p++ = 0x00; | 
|  | 648 | *p++ = 0x00; | 
|  | 649 |  | 
|  | 650 | *olen = 4; | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 651 |  | 
|  | 652 | return( 0 ); | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 653 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 654 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 655 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 656 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 657 | static int ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl, | 
|  | 658 | unsigned char *buf, | 
|  | 659 | const unsigned char *end, | 
|  | 660 | size_t *olen ) | 
| Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 661 | { | 
|  | 662 | unsigned char *p = buf; | 
|  | 663 | size_t tlen = ssl->session_negotiate->ticket_len; | 
|  | 664 |  | 
| Simon Butcher | 0fc94e9 | 2015-09-28 20:52:04 +0100 | [diff] [blame] | 665 | *olen = 0; | 
|  | 666 |  | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 667 | if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED ) | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 668 | return( 0 ); | 
| Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 669 |  | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 670 | MBEDTLS_SSL_DEBUG_MSG( 3, | 
|  | 671 | ( "client hello, adding session ticket extension" ) ); | 
| Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 672 |  | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 673 | /* The addition is safe here since the ticket length is 16 bit. */ | 
|  | 674 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 + tlen ); | 
| Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 675 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 676 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF ); | 
|  | 677 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET      ) & 0xFF ); | 
| Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 678 |  | 
|  | 679 | *p++ = (unsigned char)( ( tlen >> 8 ) & 0xFF ); | 
|  | 680 | *p++ = (unsigned char)( ( tlen      ) & 0xFF ); | 
|  | 681 |  | 
|  | 682 | *olen = 4; | 
|  | 683 |  | 
| Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 684 | if( ssl->session_negotiate->ticket == NULL || tlen == 0 ) | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 685 | return( 0 ); | 
| Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 686 |  | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 687 | MBEDTLS_SSL_DEBUG_MSG( 3, | 
| Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 688 | ( "sending session ticket of length %" MBEDTLS_PRINTF_SIZET, tlen ) ); | 
| Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 689 |  | 
|  | 690 | memcpy( p, ssl->session_negotiate->ticket, tlen ); | 
|  | 691 |  | 
|  | 692 | *olen += tlen; | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 693 |  | 
|  | 694 | return( 0 ); | 
| Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 695 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 696 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ | 
| Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 697 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 698 | #if defined(MBEDTLS_SSL_ALPN) | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 699 | static int ssl_write_alpn_ext( mbedtls_ssl_context *ssl, | 
|  | 700 | unsigned char *buf, | 
|  | 701 | const unsigned char *end, | 
|  | 702 | size_t *olen ) | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 703 | { | 
|  | 704 | unsigned char *p = buf; | 
| Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 705 | size_t alpnlen = 0; | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 706 | const char **cur; | 
|  | 707 |  | 
| Simon Butcher | 0fc94e9 | 2015-09-28 20:52:04 +0100 | [diff] [blame] | 708 | *olen = 0; | 
|  | 709 |  | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 710 | if( ssl->conf->alpn_list == NULL ) | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 711 | return( 0 ); | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 712 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 713 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) ); | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 714 |  | 
| Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 715 | for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ ) | 
| Hanno Becker | e131bfe | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 716 | alpnlen += strlen( *cur ) + 1; | 
| Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 717 |  | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 718 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 + alpnlen ); | 
| Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 719 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 720 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN >> 8 ) & 0xFF ); | 
|  | 721 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN      ) & 0xFF ); | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 722 |  | 
|  | 723 | /* | 
|  | 724 | * opaque ProtocolName<1..2^8-1>; | 
|  | 725 | * | 
|  | 726 | * struct { | 
|  | 727 | *     ProtocolName protocol_name_list<2..2^16-1> | 
|  | 728 | * } ProtocolNameList; | 
|  | 729 | */ | 
|  | 730 |  | 
|  | 731 | /* Skip writing extension and list length for now */ | 
|  | 732 | p += 4; | 
|  | 733 |  | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 734 | for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ ) | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 735 | { | 
| Hanno Becker | e131bfe | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 736 | /* | 
|  | 737 | * mbedtls_ssl_conf_set_alpn_protocols() checked that the length of | 
|  | 738 | * protocol names is less than 255. | 
|  | 739 | */ | 
|  | 740 | *p = (unsigned char)strlen( *cur ); | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 741 | memcpy( p + 1, *cur, *p ); | 
|  | 742 | p += 1 + *p; | 
|  | 743 | } | 
|  | 744 |  | 
|  | 745 | *olen = p - buf; | 
|  | 746 |  | 
|  | 747 | /* List length = olen - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */ | 
|  | 748 | buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF ); | 
|  | 749 | buf[5] = (unsigned char)( ( ( *olen - 6 )      ) & 0xFF ); | 
|  | 750 |  | 
|  | 751 | /* Extension length = olen - 2 (ext_type) - 2 (ext_len) */ | 
|  | 752 | buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF ); | 
|  | 753 | buf[3] = (unsigned char)( ( ( *olen - 4 )      ) & 0xFF ); | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 754 |  | 
|  | 755 | return( 0 ); | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 756 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 757 | #endif /* MBEDTLS_SSL_ALPN */ | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 758 |  | 
| Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 759 | #if defined(MBEDTLS_SSL_DTLS_SRTP) | 
| Johan Pascal | 77696ee | 2020-09-22 21:49:40 +0200 | [diff] [blame] | 760 | static int ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl, | 
| Johan Pascal | d387aa0 | 2020-09-23 18:47:56 +0200 | [diff] [blame] | 761 | unsigned char *buf, | 
|  | 762 | const unsigned char *end, | 
|  | 763 | size_t *olen ) | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 764 | { | 
|  | 765 | unsigned char *p = buf; | 
| Johan Pascal | f6417ec | 2020-09-22 15:15:19 +0200 | [diff] [blame] | 766 | size_t protection_profiles_index = 0, ext_len = 0; | 
|  | 767 | uint16_t mki_len = 0, profile_value = 0; | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 768 |  | 
|  | 769 | *olen = 0; | 
|  | 770 |  | 
| Johan Pascal | c3ccd98 | 2020-10-28 17:18:18 +0100 | [diff] [blame] | 771 | if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) || | 
|  | 772 | ( ssl->conf->dtls_srtp_profile_list == NULL ) || | 
|  | 773 | ( ssl->conf->dtls_srtp_profile_list_len == 0 ) ) | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 774 | { | 
| Johan Pascal | 77696ee | 2020-09-22 21:49:40 +0200 | [diff] [blame] | 775 | return( 0 ); | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 776 | } | 
|  | 777 |  | 
| Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 778 | /* RFC 5764 section 4.1.1 | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 779 | * uint8 SRTPProtectionProfile[2]; | 
|  | 780 | * | 
|  | 781 | * struct { | 
|  | 782 | *   SRTPProtectionProfiles SRTPProtectionProfiles; | 
|  | 783 | *   opaque srtp_mki<0..255>; | 
|  | 784 | * } UseSRTPData; | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 785 | * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>; | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 786 | */ | 
| Johan Pascal | 9bc97ca | 2020-09-21 23:44:45 +0200 | [diff] [blame] | 787 | if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED ) | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 788 | { | 
|  | 789 | mki_len = ssl->dtls_srtp_info.mki_len; | 
|  | 790 | } | 
| Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 791 | /* Extension length = 2 bytes for profiles length, | 
|  | 792 | *                    ssl->conf->dtls_srtp_profile_list_len * 2 (each profile is 2 bytes length ), | 
|  | 793 | *                    1 byte for srtp_mki vector length and the mki_len value | 
|  | 794 | */ | 
| Ron Eldor | 089c9fe | 2018-12-06 17:12:49 +0200 | [diff] [blame] | 795 | ext_len = 2 + 2 * ( ssl->conf->dtls_srtp_profile_list_len ) + 1 + mki_len; | 
|  | 796 |  | 
| Johan Pascal | 77696ee | 2020-09-22 21:49:40 +0200 | [diff] [blame] | 797 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding use_srtp extension" ) ); | 
|  | 798 |  | 
|  | 799 | /* Check there is room in the buffer for the extension + 4 bytes | 
|  | 800 | * - the extension tag (2 bytes) | 
|  | 801 | * - the extension length (2 bytes) | 
|  | 802 | */ | 
|  | 803 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, ext_len + 4 ); | 
|  | 804 |  | 
|  | 805 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_USE_SRTP >> 8 ) & 0xFF ); | 
|  | 806 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_USE_SRTP      ) & 0xFF ); | 
|  | 807 |  | 
|  | 808 |  | 
| Ron Eldor | 089c9fe | 2018-12-06 17:12:49 +0200 | [diff] [blame] | 809 | *p++ = (unsigned char)( ( ( ext_len & 0xFF00 ) >> 8 ) & 0xFF ); | 
|  | 810 | *p++ = (unsigned char)( ext_len & 0xFF ); | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 811 |  | 
| Ron Eldor | 3adb992 | 2017-12-21 10:15:08 +0200 | [diff] [blame] | 812 | /* protection profile length: 2*(ssl->conf->dtls_srtp_profile_list_len) */ | 
| Johan Pascal | aae4d22 | 2020-09-22 21:21:39 +0200 | [diff] [blame] | 813 | /* micro-optimization: | 
|  | 814 | * the list size is limited to MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH | 
|  | 815 | * which is lower than 127, so the upper byte of the length is always 0 | 
|  | 816 | * For the documentation, the more generic code is left in comments | 
|  | 817 | * *p++ = (unsigned char)( ( ( 2 * ssl->conf->dtls_srtp_profile_list_len ) | 
|  | 818 | *                        >> 8 ) & 0xFF ); | 
|  | 819 | */ | 
|  | 820 | *p++ = 0; | 
| Johan Pascal | e79c1e8 | 2020-09-22 15:51:27 +0200 | [diff] [blame] | 821 | *p++ = (unsigned char)( ( 2 * ssl->conf->dtls_srtp_profile_list_len ) | 
| Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 822 | & 0xFF ); | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 823 |  | 
| Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 824 | for( protection_profiles_index=0; | 
|  | 825 | protection_profiles_index < ssl->conf->dtls_srtp_profile_list_len; | 
|  | 826 | protection_profiles_index++ ) | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 827 | { | 
| Johan Pascal | 43f9490 | 2020-09-22 12:25:52 +0200 | [diff] [blame] | 828 | profile_value = mbedtls_ssl_check_srtp_profile_value | 
| Ron Eldor | 089c9fe | 2018-12-06 17:12:49 +0200 | [diff] [blame] | 829 | ( ssl->conf->dtls_srtp_profile_list[protection_profiles_index] ); | 
| Johan Pascal | 43f9490 | 2020-09-22 12:25:52 +0200 | [diff] [blame] | 830 | if( profile_value != MBEDTLS_TLS_SRTP_UNSET ) | 
| Ron Eldor | 089c9fe | 2018-12-06 17:12:49 +0200 | [diff] [blame] | 831 | { | 
|  | 832 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_write_use_srtp_ext, add profile: %04x", | 
|  | 833 | profile_value ) ); | 
|  | 834 | *p++ = ( ( profile_value >> 8 ) & 0xFF ); | 
|  | 835 | *p++ = ( profile_value & 0xFF ); | 
|  | 836 | } | 
|  | 837 | else | 
|  | 838 | { | 
|  | 839 | /* | 
|  | 840 | * Note: we shall never arrive here as protection profiles | 
| Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 841 | * is checked by mbedtls_ssl_conf_dtls_srtp_protection_profiles function | 
| Ron Eldor | 089c9fe | 2018-12-06 17:12:49 +0200 | [diff] [blame] | 842 | */ | 
| Johan Pascal | e79c1e8 | 2020-09-22 15:51:27 +0200 | [diff] [blame] | 843 | MBEDTLS_SSL_DEBUG_MSG( 3, | 
|  | 844 | ( "client hello, " | 
| Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 845 | "illegal DTLS-SRTP protection profile %d", | 
| Johan Pascal | e79c1e8 | 2020-09-22 15:51:27 +0200 | [diff] [blame] | 846 | ssl->conf->dtls_srtp_profile_list[protection_profiles_index] | 
|  | 847 | ) ); | 
| Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 848 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 849 | } | 
|  | 850 | } | 
|  | 851 |  | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 852 | *p++ = mki_len & 0xFF; | 
|  | 853 |  | 
|  | 854 | if( mki_len != 0 ) | 
|  | 855 | { | 
| Ron Eldor | 75870ec | 2018-12-06 17:31:55 +0200 | [diff] [blame] | 856 | memcpy( p, ssl->dtls_srtp_info.mki_value, mki_len ); | 
| Ron Eldor | 313d7b5 | 2018-12-10 14:56:21 +0200 | [diff] [blame] | 857 | /* | 
|  | 858 | * Increment p to point to the current position. | 
|  | 859 | */ | 
|  | 860 | p += mki_len; | 
| Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 861 | MBEDTLS_SSL_DEBUG_BUF( 3, "sending mki",  ssl->dtls_srtp_info.mki_value, | 
|  | 862 | ssl->dtls_srtp_info.mki_len ); | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 863 | } | 
|  | 864 |  | 
| Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 865 | /* | 
|  | 866 | * total extension length: extension type (2 bytes) | 
|  | 867 | *                         + extension length (2 bytes) | 
|  | 868 | *                         + protection profile length (2 bytes) | 
|  | 869 | *                         + 2 * number of protection profiles | 
|  | 870 | *                         + srtp_mki vector length(1 byte) | 
| Ron Eldor | 313d7b5 | 2018-12-10 14:56:21 +0200 | [diff] [blame] | 871 | *                         + mki value | 
| Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 872 | */ | 
| Ron Eldor | 313d7b5 | 2018-12-10 14:56:21 +0200 | [diff] [blame] | 873 | *olen = p - buf; | 
| Johan Pascal | 77696ee | 2020-09-22 21:49:40 +0200 | [diff] [blame] | 874 |  | 
|  | 875 | return( 0 ); | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 876 | } | 
|  | 877 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ | 
|  | 878 |  | 
| Manuel Pégourié-Gonnard | b760f00 | 2014-07-22 15:53:27 +0200 | [diff] [blame] | 879 | /* | 
|  | 880 | * Generate random bytes for ClientHello | 
|  | 881 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 882 | static int ssl_generate_random( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | b760f00 | 2014-07-22 15:53:27 +0200 | [diff] [blame] | 883 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 884 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | b760f00 | 2014-07-22 15:53:27 +0200 | [diff] [blame] | 885 | unsigned char *p = ssl->handshake->randbytes; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 886 | #if defined(MBEDTLS_HAVE_TIME) | 
| SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 887 | mbedtls_time_t t; | 
| Manuel Pégourié-Gonnard | b760f00 | 2014-07-22 15:53:27 +0200 | [diff] [blame] | 888 | #endif | 
|  | 889 |  | 
| Manuel Pégourié-Gonnard | fb2d223 | 2014-07-22 15:59:14 +0200 | [diff] [blame] | 890 | /* | 
|  | 891 | * When responding to a verify request, MUST reuse random (RFC 6347 4.2.1) | 
|  | 892 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 893 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 894 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | 
| Manuel Pégourié-Gonnard | fb2d223 | 2014-07-22 15:59:14 +0200 | [diff] [blame] | 895 | ssl->handshake->verify_cookie != NULL ) | 
|  | 896 | { | 
|  | 897 | return( 0 ); | 
|  | 898 | } | 
|  | 899 | #endif | 
|  | 900 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 901 | #if defined(MBEDTLS_HAVE_TIME) | 
| SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 902 | t = mbedtls_time( NULL ); | 
| Manuel Pégourié-Gonnard | b760f00 | 2014-07-22 15:53:27 +0200 | [diff] [blame] | 903 | *p++ = (unsigned char)( t >> 24 ); | 
|  | 904 | *p++ = (unsigned char)( t >> 16 ); | 
|  | 905 | *p++ = (unsigned char)( t >>  8 ); | 
|  | 906 | *p++ = (unsigned char)( t       ); | 
|  | 907 |  | 
| Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 908 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, current time: %" MBEDTLS_PRINTF_LONGLONG, | 
|  | 909 | (long long) t ) ); | 
| Manuel Pégourié-Gonnard | b760f00 | 2014-07-22 15:53:27 +0200 | [diff] [blame] | 910 | #else | 
| Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 911 | 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] | 912 | return( ret ); | 
|  | 913 |  | 
|  | 914 | p += 4; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 915 | #endif /* MBEDTLS_HAVE_TIME */ | 
| Manuel Pégourié-Gonnard | b760f00 | 2014-07-22 15:53:27 +0200 | [diff] [blame] | 916 |  | 
| Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 917 | 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] | 918 | return( ret ); | 
|  | 919 |  | 
|  | 920 | return( 0 ); | 
|  | 921 | } | 
|  | 922 |  | 
| Mohammad Azim Khan | 1d3b508 | 2018-04-18 19:35:00 +0100 | [diff] [blame] | 923 | /** | 
|  | 924 | * \brief           Validate cipher suite against config in SSL context. | 
|  | 925 | * | 
|  | 926 | * \param suite_info    cipher suite to validate | 
|  | 927 | * \param ssl           SSL context | 
| Andrzej Kurek | 03bac44 | 2018-04-25 05:06:07 -0400 | [diff] [blame] | 928 | * \param min_minor_ver Minimal minor version to accept a cipher suite | 
|  | 929 | * \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] | 930 | * | 
|  | 931 | * \return          0 if valid, else 1 | 
|  | 932 | */ | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 933 | static int ssl_validate_ciphersuite( | 
|  | 934 | const mbedtls_ssl_ciphersuite_t * suite_info, | 
|  | 935 | const mbedtls_ssl_context * ssl, | 
|  | 936 | int min_minor_ver, int max_minor_ver ) | 
| Mohammad Azim Khan | 1d3b508 | 2018-04-18 19:35:00 +0100 | [diff] [blame] | 937 | { | 
| Andrzej Kurek | 03bac44 | 2018-04-25 05:06:07 -0400 | [diff] [blame] | 938 | (void) ssl; | 
| Mohammad Azim Khan | 1d3b508 | 2018-04-18 19:35:00 +0100 | [diff] [blame] | 939 | if( suite_info == NULL ) | 
|  | 940 | return( 1 ); | 
|  | 941 |  | 
| Andrzej Kurek | 03bac44 | 2018-04-25 05:06:07 -0400 | [diff] [blame] | 942 | if( suite_info->min_minor_ver > max_minor_ver || | 
|  | 943 | suite_info->max_minor_ver < min_minor_ver ) | 
| Mohammad Azim Khan | 1d3b508 | 2018-04-18 19:35:00 +0100 | [diff] [blame] | 944 | return( 1 ); | 
|  | 945 |  | 
|  | 946 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 947 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | 
|  | 948 | ( suite_info->flags & MBEDTLS_CIPHERSUITE_NODTLS ) ) | 
|  | 949 | return( 1 ); | 
|  | 950 | #endif | 
|  | 951 |  | 
|  | 952 | #if defined(MBEDTLS_ARC4_C) | 
|  | 953 | if( ssl->conf->arc4_disabled == MBEDTLS_SSL_ARC4_DISABLED && | 
|  | 954 | suite_info->cipher == MBEDTLS_CIPHER_ARC4_128 ) | 
|  | 955 | return( 1 ); | 
|  | 956 | #endif | 
|  | 957 |  | 
|  | 958 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
|  | 959 | if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE && | 
|  | 960 | mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 ) | 
|  | 961 | return( 1 ); | 
|  | 962 | #endif | 
|  | 963 |  | 
| Hanno Becker | 2e4f616 | 2018-10-23 11:54:44 +0100 | [diff] [blame] | 964 | /* Don't suggest PSK-based ciphersuite if no PSK is available. */ | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 965 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) | 
| Hanno Becker | 2e4f616 | 2018-10-23 11:54:44 +0100 | [diff] [blame] | 966 | if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) && | 
| Hanno Becker | 520224e | 2018-10-26 11:38:07 +0100 | [diff] [blame] | 967 | ssl_conf_has_static_psk( ssl->conf ) == 0 ) | 
| Hanno Becker | 2e4f616 | 2018-10-23 11:54:44 +0100 | [diff] [blame] | 968 | { | 
|  | 969 | return( 1 ); | 
|  | 970 | } | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 971 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ | 
| Hanno Becker | 2e4f616 | 2018-10-23 11:54:44 +0100 | [diff] [blame] | 972 |  | 
| Mohammad Azim Khan | 1d3b508 | 2018-04-18 19:35:00 +0100 | [diff] [blame] | 973 | return( 0 ); | 
|  | 974 | } | 
|  | 975 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 976 | static int ssl_write_client_hello( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 977 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 978 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 979 | size_t i, n, olen, ext_len = 0; | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 980 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 981 | unsigned char *buf; | 
| Paul Bakker | 2fbefde | 2013-06-29 16:01:15 +0200 | [diff] [blame] | 982 | unsigned char *p, *q; | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 983 | const unsigned char *end; | 
|  | 984 |  | 
| Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 985 | unsigned char offer_compress; | 
| Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 986 | const int *ciphersuites; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 987 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; | 
| Ron Eldor | 755bb6a | 2018-02-14 19:30:48 +0200 | [diff] [blame] | 988 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ | 
|  | 989 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
|  | 990 | int uses_ec = 0; | 
|  | 991 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 992 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 993 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 994 |  | 
| Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 995 | if( ssl->conf->f_rng == NULL ) | 
| Paul Bakker | a9a028e | 2013-11-21 17:31:06 +0100 | [diff] [blame] | 996 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 997 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided") ); | 
|  | 998 | return( MBEDTLS_ERR_SSL_NO_RNG ); | 
| Paul Bakker | a9a028e | 2013-11-21 17:31:06 +0100 | [diff] [blame] | 999 | } | 
|  | 1000 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1001 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 1002 | if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE ) | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1003 | #endif | 
| 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 | ssl->major_ver = ssl->conf->min_major_ver; | 
|  | 1006 | ssl->minor_ver = ssl->conf->min_minor_ver; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1007 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1008 |  | 
| Manuel Pégourié-Gonnard | 1897af9 | 2015-05-10 23:27:38 +0200 | [diff] [blame] | 1009 | if( ssl->conf->max_major_ver == 0 ) | 
| Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 1010 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1011 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 1012 | ( "configured max major version is invalid, consider using mbedtls_ssl_config_defaults()" ) ); | 
| Manuel Pégourié-Gonnard | 1897af9 | 2015-05-10 23:27:38 +0200 | [diff] [blame] | 1013 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 1014 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1015 |  | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1016 | buf = ssl->out_msg; | 
|  | 1017 | end = buf + MBEDTLS_SSL_OUT_CONTENT_LEN; | 
|  | 1018 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1019 | /* | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1020 | * Check if there's enough space for the first part of the ClientHello | 
|  | 1021 | * consisting of the 38 bytes described below, the session identifier (at | 
|  | 1022 | * most 32 bytes) and its length (1 byte). | 
|  | 1023 | * | 
|  | 1024 | * Use static upper bounds instead of the actual values | 
|  | 1025 | * to allow the compiler to optimize this away. | 
|  | 1026 | */ | 
|  | 1027 | MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 38 + 1 + 32 ); | 
|  | 1028 |  | 
|  | 1029 | /* | 
|  | 1030 | * The 38 first bytes of the ClientHello: | 
|  | 1031 | *     0  .   0   handshake type (written later) | 
|  | 1032 | *     1  .   3   handshake length (written later) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1033 | *     4  .   5   highest version supported | 
|  | 1034 | *     6  .   9   current UNIX time | 
|  | 1035 | *    10  .  37   random bytes | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1036 | * | 
|  | 1037 | * The current UNIX time (4 bytes) and following 28 random bytes are written | 
|  | 1038 | * by ssl_generate_random() into ssl->handshake->randbytes buffer and then | 
|  | 1039 | * copied from there into the output buffer. | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1040 | */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1041 |  | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1042 | p = buf + 4; | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1043 | mbedtls_ssl_write_version( ssl->conf->max_major_ver, | 
|  | 1044 | ssl->conf->max_minor_ver, | 
|  | 1045 | ssl->conf->transport, p ); | 
| Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 1046 | p += 2; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1047 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1048 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, max version: [%d:%d]", | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1049 | buf[4], buf[5] ) ); | 
|  | 1050 |  | 
| Manuel Pégourié-Gonnard | b760f00 | 2014-07-22 15:53:27 +0200 | [diff] [blame] | 1051 | if( ( ret = ssl_generate_random( ssl ) ) != 0 ) | 
|  | 1052 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1053 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_generate_random", ret ); | 
| Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 1054 | return( ret ); | 
| Manuel Pégourié-Gonnard | b760f00 | 2014-07-22 15:53:27 +0200 | [diff] [blame] | 1055 | } | 
| Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 1056 |  | 
| Manuel Pégourié-Gonnard | b760f00 | 2014-07-22 15:53:27 +0200 | [diff] [blame] | 1057 | memcpy( p, ssl->handshake->randbytes, 32 ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1058 | 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] | 1059 | p += 32; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1060 |  | 
|  | 1061 | /* | 
|  | 1062 | *    38  .  38   session id length | 
|  | 1063 | *    39  . 39+n  session id | 
| Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1064 | *   39+n . 39+n  DTLS only: cookie length (1 byte) | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1065 | *   40+n .  ..   DTLS only: cookie | 
| Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1066 | *   ..   . ..    ciphersuitelist length (2 bytes) | 
|  | 1067 | *   ..   . ..    ciphersuitelist | 
|  | 1068 | *   ..   . ..    compression methods length (1 byte) | 
| Paul Bakker | c3f177a | 2012-04-11 16:11:49 +0000 | [diff] [blame] | 1069 | *   ..   . ..    compression methods | 
| Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1070 | *   ..   . ..    extensions length (2 bytes) | 
| Paul Bakker | c3f177a | 2012-04-11 16:11:49 +0000 | [diff] [blame] | 1071 | *   ..   . ..    extensions | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1072 | */ | 
| Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 1073 | n = ssl->session_negotiate->id_len; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1074 |  | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1075 | if( n < 16 || n > 32 || | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1076 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 1077 | ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE || | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1078 | #endif | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 1079 | ssl->handshake->resume == 0 ) | 
| Manuel Pégourié-Gonnard | 6377e41 | 2013-07-31 16:31:33 +0200 | [diff] [blame] | 1080 | { | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1081 | n = 0; | 
| Manuel Pégourié-Gonnard | 6377e41 | 2013-07-31 16:31:33 +0200 | [diff] [blame] | 1082 | } | 
|  | 1083 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1084 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | 
| Manuel Pégourié-Gonnard | 6377e41 | 2013-07-31 16:31:33 +0200 | [diff] [blame] | 1085 | /* | 
|  | 1086 | * RFC 5077 section 3.4: "When presenting a ticket, the client MAY | 
|  | 1087 | * generate and include a Session ID in the TLS ClientHello." | 
|  | 1088 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1089 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 1090 | if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE ) | 
| Manuel Pégourié-Gonnard | 59c6f2e | 2015-01-22 11:06:40 +0000 | [diff] [blame] | 1091 | #endif | 
| Manuel Pégourié-Gonnard | d2b35ec | 2015-03-10 11:40:43 +0000 | [diff] [blame] | 1092 | { | 
| Manuel Pégourié-Gonnard | 59c6f2e | 2015-01-22 11:06:40 +0000 | [diff] [blame] | 1093 | if( ssl->session_negotiate->ticket != NULL && | 
|  | 1094 | ssl->session_negotiate->ticket_len != 0 ) | 
|  | 1095 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1096 | ret = ssl->conf->f_rng( ssl->conf->p_rng, | 
|  | 1097 | ssl->session_negotiate->id, 32 ); | 
| Manuel Pégourié-Gonnard | 6377e41 | 2013-07-31 16:31:33 +0200 | [diff] [blame] | 1098 |  | 
| Manuel Pégourié-Gonnard | 59c6f2e | 2015-01-22 11:06:40 +0000 | [diff] [blame] | 1099 | if( ret != 0 ) | 
|  | 1100 | return( ret ); | 
| Manuel Pégourié-Gonnard | 6377e41 | 2013-07-31 16:31:33 +0200 | [diff] [blame] | 1101 |  | 
| Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 1102 | ssl->session_negotiate->id_len = n = 32; | 
| Manuel Pégourié-Gonnard | 59c6f2e | 2015-01-22 11:06:40 +0000 | [diff] [blame] | 1103 | } | 
| Manuel Pégourié-Gonnard | 6377e41 | 2013-07-31 16:31:33 +0200 | [diff] [blame] | 1104 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1105 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1106 |  | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1107 | /* | 
|  | 1108 | * The first check of the output buffer size above ( | 
|  | 1109 | * MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 38 + 1 + 32 );) | 
|  | 1110 | * has checked that there is enough space in the output buffer for the | 
|  | 1111 | * session identifier length byte and the session identifier (n <= 32). | 
|  | 1112 | */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1113 | *p++ = (unsigned char) n; | 
|  | 1114 |  | 
|  | 1115 | for( i = 0; i < n; i++ ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1116 | *p++ = ssl->session_negotiate->id[i]; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1117 |  | 
| Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 1118 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1119 | MBEDTLS_SSL_DEBUG_BUF( 3,   "client hello, session id", buf + 39, n ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1120 |  | 
| Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1121 | /* | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1122 | *   With 'n' being the length of the session identifier | 
|  | 1123 | * | 
|  | 1124 | *   39+n . 39+n  DTLS only: cookie length (1 byte) | 
|  | 1125 | *   40+n .  ..   DTLS only: cookie | 
|  | 1126 | *   ..   . ..    ciphersuitelist length (2 bytes) | 
|  | 1127 | *   ..   . ..    ciphersuitelist | 
|  | 1128 | *   ..   . ..    compression methods length (1 byte) | 
|  | 1129 | *   ..   . ..    compression methods | 
|  | 1130 | *   ..   . ..    extensions length (2 bytes) | 
|  | 1131 | *   ..   . ..    extensions | 
|  | 1132 | */ | 
|  | 1133 |  | 
|  | 1134 | /* | 
| Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1135 | * DTLS cookie | 
|  | 1136 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1137 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1138 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1139 | { | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1140 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 ); | 
|  | 1141 |  | 
| Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1142 | if( ssl->handshake->verify_cookie == NULL ) | 
|  | 1143 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1144 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no verify cookie to send" ) ); | 
| Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1145 | *p++ = 0; | 
|  | 1146 | } | 
|  | 1147 | else | 
|  | 1148 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1149 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie", | 
| Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1150 | ssl->handshake->verify_cookie, | 
|  | 1151 | ssl->handshake->verify_cookie_len ); | 
|  | 1152 |  | 
|  | 1153 | *p++ = ssl->handshake->verify_cookie_len; | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1154 |  | 
|  | 1155 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, | 
|  | 1156 | ssl->handshake->verify_cookie_len ); | 
| Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1157 | memcpy( p, ssl->handshake->verify_cookie, | 
|  | 1158 | ssl->handshake->verify_cookie_len ); | 
|  | 1159 | p += ssl->handshake->verify_cookie_len; | 
|  | 1160 | } | 
| Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1161 | } | 
|  | 1162 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1163 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1164 | /* | 
| Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1165 | * Ciphersuite list | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1166 | */ | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1167 | ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver]; | 
| Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1168 |  | 
|  | 1169 | /* Skip writing ciphersuite length for now */ | 
|  | 1170 | n = 0; | 
|  | 1171 | q = p; | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1172 |  | 
|  | 1173 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); | 
| Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1174 | p += 2; | 
|  | 1175 |  | 
| Paul Bakker | 2fbefde | 2013-06-29 16:01:15 +0200 | [diff] [blame] | 1176 | for( i = 0; ciphersuites[i] != 0; i++ ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1177 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1178 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuites[i] ); | 
| Paul Bakker | 2fbefde | 2013-06-29 16:01:15 +0200 | [diff] [blame] | 1179 |  | 
| Andrzej Kurek | 03bac44 | 2018-04-25 05:06:07 -0400 | [diff] [blame] | 1180 | if( ssl_validate_ciphersuite( ciphersuite_info, ssl, | 
|  | 1181 | ssl->conf->min_minor_ver, | 
|  | 1182 | ssl->conf->max_minor_ver ) != 0 ) | 
| Paul Bakker | 2fbefde | 2013-06-29 16:01:15 +0200 | [diff] [blame] | 1183 | continue; | 
|  | 1184 |  | 
| Hanno Becker | 3c88c65 | 2019-01-02 11:17:25 +0000 | [diff] [blame] | 1185 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %#04x (%s)", | 
| Paul Elliott | 9f35211 | 2020-12-09 14:55:45 +0000 | [diff] [blame] | 1186 | (unsigned int)ciphersuites[i], ciphersuite_info->name ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1187 |  | 
| Ron Eldor | 755bb6a | 2018-02-14 19:30:48 +0200 | [diff] [blame] | 1188 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ | 
|  | 1189 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
|  | 1190 | uses_ec |= mbedtls_ssl_ciphersuite_uses_ec( ciphersuite_info ); | 
|  | 1191 | #endif | 
|  | 1192 |  | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1193 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); | 
|  | 1194 |  | 
| Paul Bakker | 2fbefde | 2013-06-29 16:01:15 +0200 | [diff] [blame] | 1195 | n++; | 
| Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 1196 | *p++ = (unsigned char)( ciphersuites[i] >> 8 ); | 
|  | 1197 | *p++ = (unsigned char)( ciphersuites[i]      ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1198 | } | 
|  | 1199 |  | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1200 | MBEDTLS_SSL_DEBUG_MSG( 3, | 
| Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 1201 | ( "client hello, got %" MBEDTLS_PRINTF_SIZET " ciphersuites (excluding SCSVs)", n ) ); | 
| Ron Eldor | 714785d | 2017-08-28 13:55:55 +0300 | [diff] [blame] | 1202 |  | 
| Manuel Pégourié-Gonnard | 5d9cde2 | 2015-01-22 10:49:41 +0000 | [diff] [blame] | 1203 | /* | 
|  | 1204 | * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV | 
|  | 1205 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1206 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 1207 | if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE ) | 
| Manuel Pégourié-Gonnard | 5d9cde2 | 2015-01-22 10:49:41 +0000 | [diff] [blame] | 1208 | #endif | 
|  | 1209 | { | 
| Ron Eldor | 4a2fb4c | 2017-09-10 17:03:50 +0300 | [diff] [blame] | 1210 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding EMPTY_RENEGOTIATION_INFO_SCSV" ) ); | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1211 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1212 | *p++ = (unsigned char)( MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO >> 8 ); | 
|  | 1213 | *p++ = (unsigned char)( MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO      ); | 
| Manuel Pégourié-Gonnard | 5d9cde2 | 2015-01-22 10:49:41 +0000 | [diff] [blame] | 1214 | n++; | 
|  | 1215 | } | 
|  | 1216 |  | 
| Manuel Pégourié-Gonnard | 1cbd39d | 2014-10-20 13:34:59 +0200 | [diff] [blame] | 1217 | /* 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] | 1218 | #if defined(MBEDTLS_SSL_FALLBACK_SCSV) | 
| Manuel Pégourié-Gonnard | 684b059 | 2015-05-06 09:27:31 +0100 | [diff] [blame] | 1219 | if( ssl->conf->fallback == MBEDTLS_SSL_IS_FALLBACK ) | 
| Manuel Pégourié-Gonnard | 1cbd39d | 2014-10-20 13:34:59 +0200 | [diff] [blame] | 1220 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1221 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding FALLBACK_SCSV" ) ); | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1222 |  | 
|  | 1223 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1224 | *p++ = (unsigned char)( MBEDTLS_SSL_FALLBACK_SCSV_VALUE >> 8 ); | 
|  | 1225 | *p++ = (unsigned char)( MBEDTLS_SSL_FALLBACK_SCSV_VALUE      ); | 
| Manuel Pégourié-Gonnard | 1cbd39d | 2014-10-20 13:34:59 +0200 | [diff] [blame] | 1226 | n++; | 
|  | 1227 | } | 
|  | 1228 | #endif | 
|  | 1229 |  | 
| Paul Bakker | 2fbefde | 2013-06-29 16:01:15 +0200 | [diff] [blame] | 1230 | *q++ = (unsigned char)( n >> 7 ); | 
|  | 1231 | *q++ = (unsigned char)( n << 1 ); | 
|  | 1232 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1233 | #if defined(MBEDTLS_ZLIB_SUPPORT) | 
| Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1234 | offer_compress = 1; | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1235 | #else | 
| Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1236 | offer_compress = 0; | 
|  | 1237 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1238 |  | 
| Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1239 | /* | 
| Johannes H | 4e5d23f | 2018-01-06 09:46:57 +0100 | [diff] [blame] | 1240 | * 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] | 1241 | * in the same datagram, uncompressing one could overwrite the next one. | 
|  | 1242 | * We don't want to add complexity for handling that case unless there is | 
|  | 1243 | * an actual need for it. | 
|  | 1244 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1245 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1246 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1247 | offer_compress = 0; | 
|  | 1248 | #endif | 
|  | 1249 |  | 
|  | 1250 | if( offer_compress ) | 
|  | 1251 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1252 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 2 ) ); | 
|  | 1253 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d %d", | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1254 | MBEDTLS_SSL_COMPRESS_DEFLATE, | 
|  | 1255 | MBEDTLS_SSL_COMPRESS_NULL ) ); | 
| Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1256 |  | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1257 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 3 ); | 
| Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1258 | *p++ = 2; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1259 | *p++ = MBEDTLS_SSL_COMPRESS_DEFLATE; | 
|  | 1260 | *p++ = MBEDTLS_SSL_COMPRESS_NULL; | 
| Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1261 | } | 
|  | 1262 | else | 
|  | 1263 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1264 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 1 ) ); | 
|  | 1265 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d", | 
|  | 1266 | MBEDTLS_SSL_COMPRESS_NULL ) ); | 
| Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1267 |  | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1268 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); | 
| Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1269 | *p++ = 1; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1270 | *p++ = MBEDTLS_SSL_COMPRESS_NULL; | 
| Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1271 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1272 |  | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1273 | /* First write extensions, then the total length */ | 
|  | 1274 |  | 
|  | 1275 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); | 
|  | 1276 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1277 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1278 | if( ( ret = ssl_write_hostname_ext( ssl, p + 2 + ext_len, | 
|  | 1279 | end, &olen ) ) != 0 ) | 
|  | 1280 | { | 
|  | 1281 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_hostname_ext", ret ); | 
|  | 1282 | return( ret ); | 
|  | 1283 | } | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 1284 | ext_len += olen; | 
| Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 1285 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1286 |  | 
| Hanno Becker | 40f8b51 | 2017-10-12 14:58:55 +0100 | [diff] [blame] | 1287 | /* Note that TLS_EMPTY_RENEGOTIATION_INFO_SCSV is always added | 
|  | 1288 | * even if MBEDTLS_SSL_RENEGOTIATION is not defined. */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1289 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1290 | if( ( ret = ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, | 
|  | 1291 | end, &olen ) ) != 0 ) | 
|  | 1292 | { | 
|  | 1293 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_renegotiation_ext", ret ); | 
|  | 1294 | return( ret ); | 
|  | 1295 | } | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 1296 | ext_len += olen; | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1297 | #endif | 
| Paul Bakker | c3f177a | 2012-04-11 16:11:49 +0000 | [diff] [blame] | 1298 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1299 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1300 | defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1301 | if( ( ret = ssl_write_signature_algorithms_ext( ssl, p + 2 + ext_len, | 
|  | 1302 | end, &olen ) ) != 0 ) | 
|  | 1303 | { | 
|  | 1304 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_signature_algorithms_ext", ret ); | 
|  | 1305 | return( ret ); | 
|  | 1306 | } | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 1307 | ext_len += olen; | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 1308 | #endif | 
| Paul Bakker | c3f177a | 2012-04-11 16:11:49 +0000 | [diff] [blame] | 1309 |  | 
| Manuel Pégourié-Gonnard | f472179 | 2015-09-15 10:53:51 +0200 | [diff] [blame] | 1310 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ | 
| Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 1311 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Ron Eldor | 755bb6a | 2018-02-14 19:30:48 +0200 | [diff] [blame] | 1312 | if( uses_ec ) | 
|  | 1313 | { | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1314 | if( ( ret = ssl_write_supported_elliptic_curves_ext( ssl, p + 2 + ext_len, | 
|  | 1315 | end, &olen ) ) != 0 ) | 
|  | 1316 | { | 
|  | 1317 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_supported_elliptic_curves_ext", ret ); | 
|  | 1318 | return( ret ); | 
|  | 1319 | } | 
| Ron Eldor | 755bb6a | 2018-02-14 19:30:48 +0200 | [diff] [blame] | 1320 | ext_len += olen; | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1321 |  | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1322 | if( ( ret = ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, | 
|  | 1323 | end, &olen ) ) != 0 ) | 
|  | 1324 | { | 
|  | 1325 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_supported_point_formats_ext", ret ); | 
|  | 1326 | return( ret ); | 
|  | 1327 | } | 
| Ron Eldor | 755bb6a | 2018-02-14 19:30:48 +0200 | [diff] [blame] | 1328 | ext_len += olen; | 
|  | 1329 | } | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1330 | #endif | 
|  | 1331 |  | 
| Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 1332 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1333 | if( ( ret = ssl_write_ecjpake_kkpp_ext( ssl, p + 2 + ext_len, | 
|  | 1334 | end, &olen ) ) != 0 ) | 
|  | 1335 | { | 
|  | 1336 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_ecjpake_kkpp_ext", ret ); | 
|  | 1337 | return( ret ); | 
|  | 1338 | } | 
| Manuel Pégourié-Gonnard | 294139b | 2015-09-15 16:55:05 +0200 | [diff] [blame] | 1339 | ext_len += olen; | 
|  | 1340 | #endif | 
|  | 1341 |  | 
| Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1342 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1343 | if( ( ret = ssl_write_cid_ext( ssl, p + 2 + ext_len, end, &olen ) ) != 0 ) | 
|  | 1344 | { | 
|  | 1345 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_cid_ext", ret ); | 
|  | 1346 | return( ret ); | 
|  | 1347 | } | 
| Hanno Becker | 49770ff | 2019-04-25 16:55:15 +0100 | [diff] [blame] | 1348 | ext_len += olen; | 
| Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1349 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
| Hanno Becker | 49770ff | 2019-04-25 16:55:15 +0100 | [diff] [blame] | 1350 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1351 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1352 | if( ( ret = ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, | 
|  | 1353 | end, &olen ) ) != 0 ) | 
|  | 1354 | { | 
|  | 1355 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_max_fragment_length_ext", ret ); | 
|  | 1356 | return( ret ); | 
|  | 1357 | } | 
| Manuel Pégourié-Gonnard | a052849 | 2013-07-16 17:26:28 +0200 | [diff] [blame] | 1358 | ext_len += olen; | 
| Paul Bakker | 05decb2 | 2013-08-15 13:33:48 +0200 | [diff] [blame] | 1359 | #endif | 
| Manuel Pégourié-Gonnard | a052849 | 2013-07-16 17:26:28 +0200 | [diff] [blame] | 1360 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1361 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1362 | if( ( ret = ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, | 
|  | 1363 | end, &olen ) ) != 0 ) | 
|  | 1364 | { | 
|  | 1365 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_truncated_hmac_ext", ret ); | 
|  | 1366 | return( ret ); | 
|  | 1367 | } | 
| Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 1368 | ext_len += olen; | 
| Paul Bakker | 1f2bc62 | 2013-08-15 13:45:55 +0200 | [diff] [blame] | 1369 | #endif | 
| Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 1370 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1371 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1372 | if( ( ret = ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, | 
|  | 1373 | end, &olen ) ) != 0 ) | 
|  | 1374 | { | 
|  | 1375 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_encrypt_then_mac_ext", ret ); | 
|  | 1376 | return( ret ); | 
|  | 1377 | } | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 1378 | ext_len += olen; | 
|  | 1379 | #endif | 
|  | 1380 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1381 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1382 | if( ( ret = ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, | 
|  | 1383 | end, &olen ) ) != 0 ) | 
|  | 1384 | { | 
|  | 1385 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_extended_ms_ext", ret ); | 
|  | 1386 | return( ret ); | 
|  | 1387 | } | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 1388 | ext_len += olen; | 
|  | 1389 | #endif | 
|  | 1390 |  | 
| Simon Butcher | 5624ec8 | 2015-09-29 01:06:06 +0100 | [diff] [blame] | 1391 | #if defined(MBEDTLS_SSL_ALPN) | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1392 | if( ( ret = ssl_write_alpn_ext( ssl, p + 2 + ext_len, | 
|  | 1393 | end, &olen ) ) != 0 ) | 
|  | 1394 | { | 
|  | 1395 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_alpn_ext", ret ); | 
|  | 1396 | return( ret ); | 
|  | 1397 | } | 
| Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 1398 | ext_len += olen; | 
| Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 1399 | #endif | 
| Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 1400 |  | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1401 | #if defined(MBEDTLS_SSL_DTLS_SRTP) | 
| Johan Pascal | c3ccd98 | 2020-10-28 17:18:18 +0100 | [diff] [blame] | 1402 | if( ( ret = ssl_write_use_srtp_ext( ssl, p + 2 + ext_len, | 
|  | 1403 | end, &olen ) ) != 0 ) | 
| Ron Eldor | 3adb992 | 2017-12-21 10:15:08 +0200 | [diff] [blame] | 1404 | { | 
| Johan Pascal | c3ccd98 | 2020-10-28 17:18:18 +0100 | [diff] [blame] | 1405 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_use_srtp_ext", ret ); | 
|  | 1406 | return( ret ); | 
| Ron Eldor | 3adb992 | 2017-12-21 10:15:08 +0200 | [diff] [blame] | 1407 | } | 
| Johan Pascal | c3ccd98 | 2020-10-28 17:18:18 +0100 | [diff] [blame] | 1408 | ext_len += olen; | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1409 | #endif | 
|  | 1410 |  | 
| Simon Butcher | 5624ec8 | 2015-09-29 01:06:06 +0100 | [diff] [blame] | 1411 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1412 | if( ( ret = ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, | 
|  | 1413 | end, &olen ) ) != 0 ) | 
|  | 1414 | { | 
|  | 1415 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_session_ticket_ext", ret ); | 
|  | 1416 | return( ret ); | 
|  | 1417 | } | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 1418 | ext_len += olen; | 
|  | 1419 | #endif | 
|  | 1420 |  | 
| Manuel Pégourié-Gonnard | eaecbd3 | 2014-11-06 02:38:02 +0100 | [diff] [blame] | 1421 | /* olen unused if all extensions are disabled */ | 
|  | 1422 | ((void) olen); | 
|  | 1423 |  | 
| Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 1424 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET, | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1425 | ext_len ) ); | 
| Paul Bakker | c3f177a | 2012-04-11 16:11:49 +0000 | [diff] [blame] | 1426 |  | 
| Paul Bakker | a703663 | 2014-04-30 10:15:38 +0200 | [diff] [blame] | 1427 | if( ext_len > 0 ) | 
|  | 1428 | { | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1429 | /* No need to check for space here, because the extension | 
|  | 1430 | * writing functions already took care of that. */ | 
| Paul Bakker | a703663 | 2014-04-30 10:15:38 +0200 | [diff] [blame] | 1431 | *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF ); | 
|  | 1432 | *p++ = (unsigned char)( ( ext_len      ) & 0xFF ); | 
|  | 1433 | p += ext_len; | 
|  | 1434 | } | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1435 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1436 | ssl->out_msglen  = p - buf; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1437 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; | 
|  | 1438 | ssl->out_msg[0]  = MBEDTLS_SSL_HS_CLIENT_HELLO; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1439 |  | 
|  | 1440 | ssl->state++; | 
|  | 1441 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1442 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1443 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1444 | mbedtls_ssl_send_flight_completed( ssl ); | 
| Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 1445 | #endif | 
|  | 1446 |  | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 1447 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1448 | { | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 1449 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1450 | return( ret ); | 
|  | 1451 | } | 
|  | 1452 |  | 
| Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 1453 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 1454 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | 
|  | 1455 | ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) | 
|  | 1456 | { | 
|  | 1457 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); | 
|  | 1458 | return( ret ); | 
|  | 1459 | } | 
| Hanno Becker | bc2498a | 2018-08-28 10:13:29 +0100 | [diff] [blame] | 1460 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 1461 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1462 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1463 |  | 
|  | 1464 | return( 0 ); | 
|  | 1465 | } | 
|  | 1466 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1467 | static int ssl_parse_renegotiation_info( mbedtls_ssl_context *ssl, | 
| Manuel Pégourié-Gonnard | e048b67 | 2013-07-19 12:47:00 +0200 | [diff] [blame] | 1468 | const unsigned char *buf, | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1469 | size_t len ) | 
|  | 1470 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1471 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 1472 | if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1473 | { | 
| Manuel Pégourié-Gonnard | 31ff1d2 | 2013-10-28 13:46:11 +0100 | [diff] [blame] | 1474 | /* Check verify-data in constant-time. The length OTOH is no secret */ | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1475 | if( len    != 1 + ssl->verify_data_len * 2 || | 
|  | 1476 | buf[0] !=     ssl->verify_data_len * 2 || | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1477 | mbedtls_ssl_safer_memcmp( buf + 1, | 
| Manuel Pégourié-Gonnard | 31ff1d2 | 2013-10-28 13:46:11 +0100 | [diff] [blame] | 1478 | ssl->own_verify_data, ssl->verify_data_len ) != 0 || | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1479 | mbedtls_ssl_safer_memcmp( buf + 1 + ssl->verify_data_len, | 
| Manuel Pégourié-Gonnard | 31ff1d2 | 2013-10-28 13:46:11 +0100 | [diff] [blame] | 1480 | ssl->peer_verify_data, ssl->verify_data_len ) != 0 ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1481 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1482 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1483 | mbedtls_ssl_send_alert_message( | 
|  | 1484 | ssl, | 
|  | 1485 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1486 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1487 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1488 | } | 
|  | 1489 | } | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1490 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1491 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1492 | { | 
|  | 1493 | if( len != 1 || buf[0] != 0x00 ) | 
|  | 1494 | { | 
| Ronald Cron | 5ee5707 | 2020-06-11 09:34:06 +0200 | [diff] [blame] | 1495 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 1496 | ( "non-zero length renegotiation info" ) ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1497 | mbedtls_ssl_send_alert_message( | 
|  | 1498 | ssl, | 
|  | 1499 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1500 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1501 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1502 | } | 
|  | 1503 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1504 | ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION; | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1505 | } | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1506 |  | 
|  | 1507 | return( 0 ); | 
|  | 1508 | } | 
| Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 1509 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1510 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
|  | 1511 | 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] | 1512 | const unsigned char *buf, | 
| Manuel Pégourié-Gonnard | de600e5 | 2013-07-17 10:14:38 +0200 | [diff] [blame] | 1513 | size_t len ) | 
|  | 1514 | { | 
|  | 1515 | /* | 
|  | 1516 | * server should use the extension only if we did, | 
|  | 1517 | * and if so the server's value should match ours (and len is always 1) | 
|  | 1518 | */ | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1519 | 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] | 1520 | len != 1 || | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1521 | buf[0] != ssl->conf->mfl_code ) | 
| Manuel Pégourié-Gonnard | de600e5 | 2013-07-17 10:14:38 +0200 | [diff] [blame] | 1522 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1523 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 1524 | ( "non-matching max fragment length extension" ) ); | 
|  | 1525 | mbedtls_ssl_send_alert_message( | 
|  | 1526 | ssl, | 
|  | 1527 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1528 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1529 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
| Manuel Pégourié-Gonnard | de600e5 | 2013-07-17 10:14:38 +0200 | [diff] [blame] | 1530 | } | 
|  | 1531 |  | 
|  | 1532 | return( 0 ); | 
|  | 1533 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1534 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1535 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1536 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) | 
|  | 1537 | 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] | 1538 | const unsigned char *buf, | 
|  | 1539 | size_t len ) | 
|  | 1540 | { | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1541 | if( ssl->conf->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_DISABLED || | 
| Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 1542 | len != 0 ) | 
|  | 1543 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1544 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 1545 | ( "non-matching truncated HMAC extension" ) ); | 
|  | 1546 | mbedtls_ssl_send_alert_message( | 
|  | 1547 | ssl, | 
|  | 1548 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1549 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1550 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
| Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 1551 | } | 
|  | 1552 |  | 
|  | 1553 | ((void) buf); | 
|  | 1554 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1555 | ssl->session_negotiate->trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; | 
| Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 1556 |  | 
|  | 1557 | return( 0 ); | 
|  | 1558 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1559 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ | 
| Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 1560 |  | 
| Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1561 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
| Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 1562 | static int ssl_parse_cid_ext( mbedtls_ssl_context *ssl, | 
|  | 1563 | const unsigned char *buf, | 
|  | 1564 | size_t len ) | 
|  | 1565 | { | 
|  | 1566 | size_t peer_cid_len; | 
|  | 1567 |  | 
|  | 1568 | if( /* CID extension only makes sense in DTLS */ | 
|  | 1569 | ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || | 
|  | 1570 | /* 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] | 1571 | ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED ) | 
| Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 1572 | { | 
| Hanno Becker | 2262648 | 2019-05-03 12:46:59 +0100 | [diff] [blame] | 1573 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension unexpected" ) ); | 
| Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 1574 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
| Hanno Becker | 2262648 | 2019-05-03 12:46:59 +0100 | [diff] [blame] | 1575 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
|  | 1576 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
|  | 1577 | } | 
|  | 1578 |  | 
|  | 1579 | if( len == 0 ) | 
|  | 1580 | { | 
|  | 1581 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) ); | 
|  | 1582 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1583 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 1584 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
|  | 1585 | } | 
|  | 1586 |  | 
|  | 1587 | peer_cid_len = *buf++; | 
|  | 1588 | len--; | 
|  | 1589 |  | 
|  | 1590 | if( peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX ) | 
|  | 1591 | { | 
| Hanno Becker | 2262648 | 2019-05-03 12:46:59 +0100 | [diff] [blame] | 1592 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) ); | 
| Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 1593 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
| Hanno Becker | 2262648 | 2019-05-03 12:46:59 +0100 | [diff] [blame] | 1594 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 1595 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
|  | 1596 | } | 
|  | 1597 |  | 
|  | 1598 | if( len != peer_cid_len ) | 
|  | 1599 | { | 
| Hanno Becker | 2262648 | 2019-05-03 12:46:59 +0100 | [diff] [blame] | 1600 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) ); | 
| Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 1601 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
| Hanno Becker | 2262648 | 2019-05-03 12:46:59 +0100 | [diff] [blame] | 1602 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); | 
| Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 1603 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
|  | 1604 | } | 
|  | 1605 |  | 
| Hanno Becker | 5a29990 | 2019-05-03 12:47:49 +0100 | [diff] [blame] | 1606 | ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED; | 
| Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 1607 | ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len; | 
|  | 1608 | memcpy( ssl->handshake->peer_cid, buf, peer_cid_len ); | 
|  | 1609 |  | 
|  | 1610 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use of CID extension negotiated" ) ); | 
|  | 1611 | MBEDTLS_SSL_DEBUG_BUF( 3, "Server CID", buf, peer_cid_len ); | 
|  | 1612 |  | 
| Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 1613 | return( 0 ); | 
|  | 1614 | } | 
| Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1615 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
| Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 1616 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1617 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
|  | 1618 | 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] | 1619 | const unsigned char *buf, | 
|  | 1620 | size_t len ) | 
|  | 1621 | { | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1622 | if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED || | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1623 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 || | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 1624 | len != 0 ) | 
|  | 1625 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1626 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 1627 | ( "non-matching encrypt-then-MAC extension" ) ); | 
|  | 1628 | mbedtls_ssl_send_alert_message( | 
|  | 1629 | ssl, | 
|  | 1630 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1631 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1632 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 1633 | } | 
|  | 1634 |  | 
|  | 1635 | ((void) buf); | 
|  | 1636 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1637 | ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 1638 |  | 
|  | 1639 | return( 0 ); | 
|  | 1640 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1641 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 1642 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1643 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) | 
|  | 1644 | 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] | 1645 | const unsigned char *buf, | 
|  | 1646 | size_t len ) | 
|  | 1647 | { | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1648 | if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED || | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1649 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 || | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 1650 | len != 0 ) | 
|  | 1651 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1652 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 1653 | ( "non-matching extended master secret extension" ) ); | 
|  | 1654 | mbedtls_ssl_send_alert_message( | 
|  | 1655 | ssl, | 
|  | 1656 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1657 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1658 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 1659 | } | 
|  | 1660 |  | 
|  | 1661 | ((void) buf); | 
|  | 1662 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1663 | ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 1664 |  | 
|  | 1665 | return( 0 ); | 
|  | 1666 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1667 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 1668 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1669 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | 
|  | 1670 | 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] | 1671 | const unsigned char *buf, | 
|  | 1672 | size_t len ) | 
|  | 1673 | { | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1674 | if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED || | 
| Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 1675 | len != 0 ) | 
|  | 1676 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1677 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 1678 | ( "non-matching session ticket extension" ) ); | 
|  | 1679 | mbedtls_ssl_send_alert_message( | 
|  | 1680 | ssl, | 
|  | 1681 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1682 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1683 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
| Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 1684 | } | 
| Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 1685 |  | 
|  | 1686 | ((void) buf); | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 1687 |  | 
|  | 1688 | ssl->handshake->new_session_ticket = 1; | 
| Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 1689 |  | 
|  | 1690 | return( 0 ); | 
|  | 1691 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1692 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ | 
| Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 1693 |  | 
| Robert Cragie | 136884c | 2015-10-02 13:34:31 +0100 | [diff] [blame] | 1694 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ | 
| Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 1695 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1696 | 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] | 1697 | const unsigned char *buf, | 
|  | 1698 | size_t len ) | 
|  | 1699 | { | 
|  | 1700 | size_t list_size; | 
|  | 1701 | const unsigned char *p; | 
|  | 1702 |  | 
| Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 1703 | if( len == 0 || (size_t)( buf[0] + 1 ) != len ) | 
| Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 1704 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1705 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1706 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1707 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1708 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
| Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 1709 | } | 
| Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 1710 | list_size = buf[0]; | 
| Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 1711 |  | 
| Manuel Pégourié-Gonnard | fd35af1 | 2014-06-23 14:10:13 +0200 | [diff] [blame] | 1712 | p = buf + 1; | 
| Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 1713 | while( list_size > 0 ) | 
|  | 1714 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1715 | if( p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED || | 
|  | 1716 | p[0] == MBEDTLS_ECP_PF_COMPRESSED ) | 
| Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 1717 | { | 
| Robert Cragie | 136884c | 2015-10-02 13:34:31 +0100 | [diff] [blame] | 1718 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) | 
| Manuel Pégourié-Gonnard | 5734b2d | 2013-08-15 19:04:02 +0200 | [diff] [blame] | 1719 | ssl->handshake->ecdh_ctx.point_format = p[0]; | 
| Gilles Peskine | 064a85c | 2017-05-10 10:46:40 +0200 | [diff] [blame] | 1720 | #endif | 
| Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 1721 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Robert Cragie | 136884c | 2015-10-02 13:34:31 +0100 | [diff] [blame] | 1722 | ssl->handshake->ecjpake_ctx.point_format = p[0]; | 
|  | 1723 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1724 | 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] | 1725 | return( 0 ); | 
|  | 1726 | } | 
|  | 1727 |  | 
|  | 1728 | list_size--; | 
|  | 1729 | p++; | 
|  | 1730 | } | 
|  | 1731 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1732 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no point format in common" ) ); | 
| Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 1733 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1734 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1735 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
| Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 1736 | } | 
| Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 1737 | #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || | 
| Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 1738 | MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ | 
| Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 1739 |  | 
| Manuel Pégourié-Gonnard | 0a1324a | 2015-09-16 16:01:00 +0200 | [diff] [blame] | 1740 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
|  | 1741 | static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl, | 
|  | 1742 | const unsigned char *buf, | 
|  | 1743 | size_t len ) | 
|  | 1744 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1745 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | 0a1324a | 2015-09-16 16:01:00 +0200 | [diff] [blame] | 1746 |  | 
| Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1747 | if( ssl->handshake->ciphersuite_info->key_exchange != | 
| Manuel Pégourié-Gonnard | 0a1324a | 2015-09-16 16:01:00 +0200 | [diff] [blame] | 1748 | MBEDTLS_KEY_EXCHANGE_ECJPAKE ) | 
|  | 1749 | { | 
|  | 1750 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) ); | 
|  | 1751 | return( 0 ); | 
|  | 1752 | } | 
|  | 1753 |  | 
| Manuel Pégourié-Gonnard | d0d8cb3 | 2015-09-17 14:16:30 +0200 | [diff] [blame] | 1754 | /* If we got here, we no longer need our cached extension */ | 
|  | 1755 | mbedtls_free( ssl->handshake->ecjpake_cache ); | 
|  | 1756 | ssl->handshake->ecjpake_cache = NULL; | 
|  | 1757 | ssl->handshake->ecjpake_cache_len = 0; | 
|  | 1758 |  | 
| Manuel Pégourié-Gonnard | 0a1324a | 2015-09-16 16:01:00 +0200 | [diff] [blame] | 1759 | if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx, | 
|  | 1760 | buf, len ) ) != 0 ) | 
|  | 1761 | { | 
|  | 1762 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1763 | mbedtls_ssl_send_alert_message( | 
|  | 1764 | ssl, | 
|  | 1765 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1766 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Manuel Pégourié-Gonnard | 0a1324a | 2015-09-16 16:01:00 +0200 | [diff] [blame] | 1767 | return( ret ); | 
|  | 1768 | } | 
|  | 1769 |  | 
|  | 1770 | return( 0 ); | 
|  | 1771 | } | 
|  | 1772 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1773 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1774 | #if defined(MBEDTLS_SSL_ALPN) | 
|  | 1775 | static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl, | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 1776 | const unsigned char *buf, size_t len ) | 
|  | 1777 | { | 
|  | 1778 | size_t list_len, name_len; | 
|  | 1779 | const char **p; | 
|  | 1780 |  | 
|  | 1781 | /* 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] | 1782 | if( ssl->conf->alpn_list == NULL ) | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1783 | { | 
|  | 1784 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching ALPN extension" ) ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1785 | mbedtls_ssl_send_alert_message( | 
|  | 1786 | ssl, | 
|  | 1787 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1788 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1789 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1790 | } | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 1791 |  | 
|  | 1792 | /* | 
|  | 1793 | * opaque ProtocolName<1..2^8-1>; | 
|  | 1794 | * | 
|  | 1795 | * struct { | 
|  | 1796 | *     ProtocolName protocol_name_list<2..2^16-1> | 
|  | 1797 | * } ProtocolNameList; | 
|  | 1798 | * | 
|  | 1799 | * the "ProtocolNameList" MUST contain exactly one "ProtocolName" | 
|  | 1800 | */ | 
|  | 1801 |  | 
|  | 1802 | /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */ | 
|  | 1803 | if( len < 4 ) | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1804 | { | 
|  | 1805 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1806 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1807 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1808 | } | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 1809 |  | 
|  | 1810 | list_len = ( buf[0] << 8 ) | buf[1]; | 
|  | 1811 | if( list_len != len - 2 ) | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1812 | { | 
|  | 1813 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1814 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1815 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1816 | } | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 1817 |  | 
|  | 1818 | name_len = buf[2]; | 
|  | 1819 | if( name_len != list_len - 1 ) | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1820 | { | 
|  | 1821 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1822 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1823 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1824 | } | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 1825 |  | 
|  | 1826 | /* 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] | 1827 | for( p = ssl->conf->alpn_list; *p != NULL; p++ ) | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 1828 | { | 
|  | 1829 | if( name_len == strlen( *p ) && | 
|  | 1830 | memcmp( buf + 3, *p, name_len ) == 0 ) | 
|  | 1831 | { | 
|  | 1832 | ssl->alpn_chosen = *p; | 
|  | 1833 | return( 0 ); | 
|  | 1834 | } | 
|  | 1835 | } | 
|  | 1836 |  | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1837 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "ALPN extension: no matching protocol" ) ); | 
| Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 1838 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1839 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1840 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 1841 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1842 | #endif /* MBEDTLS_SSL_ALPN */ | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 1843 |  | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1844 | #if defined(MBEDTLS_SSL_DTLS_SRTP) | 
|  | 1845 | static int ssl_parse_use_srtp_ext( mbedtls_ssl_context *ssl, | 
| Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 1846 | const unsigned char *buf, | 
|  | 1847 | size_t len ) | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1848 | { | 
| Johan Pascal | 43f9490 | 2020-09-22 12:25:52 +0200 | [diff] [blame] | 1849 | mbedtls_ssl_srtp_profile server_protection = MBEDTLS_TLS_SRTP_UNSET; | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 1850 | size_t i, mki_len = 0; | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1851 | uint16_t server_protection_profile_value = 0; | 
|  | 1852 |  | 
|  | 1853 | /* If use_srtp is not configured, just ignore the extension */ | 
| Johan Pascal | c3ccd98 | 2020-10-28 17:18:18 +0100 | [diff] [blame] | 1854 | if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) || | 
|  | 1855 | ( ssl->conf->dtls_srtp_profile_list == NULL ) || | 
|  | 1856 | ( ssl->conf->dtls_srtp_profile_list_len == 0 ) ) | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1857 | return( 0 ); | 
|  | 1858 |  | 
| Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 1859 | /* RFC 5764 section 4.1.1 | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1860 | * uint8 SRTPProtectionProfile[2]; | 
|  | 1861 | * | 
|  | 1862 | * struct { | 
|  | 1863 | *   SRTPProtectionProfiles SRTPProtectionProfiles; | 
|  | 1864 | *   opaque srtp_mki<0..255>; | 
|  | 1865 | * } UseSRTPData; | 
|  | 1866 |  | 
|  | 1867 | * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>; | 
|  | 1868 | * | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1869 | */ | 
| Johan Pascal | f6417ec | 2020-09-22 15:15:19 +0200 | [diff] [blame] | 1870 | if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED ) | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 1871 | { | 
|  | 1872 | mki_len = ssl->dtls_srtp_info.mki_len; | 
|  | 1873 | } | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1874 |  | 
| Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 1875 | /* | 
| Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 1876 | * Length is 5 + optional mki_value : one protection profile length (2 bytes) | 
|  | 1877 | *                                      + protection profile (2 bytes) | 
|  | 1878 | *                                      + mki_len(1 byte) | 
| Ron Eldor | 313d7b5 | 2018-12-10 14:56:21 +0200 | [diff] [blame] | 1879 | *                                      and optional srtp_mki | 
| Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 1880 | */ | 
| Johan Pascal | adbd944 | 2020-10-26 21:24:25 +0100 | [diff] [blame] | 1881 | if( ( len < 5 ) || ( len != ( buf[4] + 5u ) ) ) | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1882 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
|  | 1883 |  | 
|  | 1884 | /* | 
|  | 1885 | * get the server protection profile | 
|  | 1886 | */ | 
| Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 1887 |  | 
|  | 1888 | /* | 
|  | 1889 | * protection profile length must be 0x0002 as we must have only | 
|  | 1890 | * one protection profile in server Hello | 
|  | 1891 | */ | 
| Ron Eldor | 089c9fe | 2018-12-06 17:12:49 +0200 | [diff] [blame] | 1892 | if( (  buf[0] != 0 ) || ( buf[1] != 2 ) ) | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1893 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
| Ron Eldor | 089c9fe | 2018-12-06 17:12:49 +0200 | [diff] [blame] | 1894 |  | 
|  | 1895 | server_protection_profile_value = ( buf[2] << 8 ) | buf[3]; | 
| Johan Pascal | 43f9490 | 2020-09-22 12:25:52 +0200 | [diff] [blame] | 1896 | server_protection = mbedtls_ssl_check_srtp_profile_value( | 
|  | 1897 | server_protection_profile_value ); | 
|  | 1898 | if( server_protection != MBEDTLS_TLS_SRTP_UNSET ) | 
| Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 1899 | { | 
| Johan Pascal | 43f9490 | 2020-09-22 12:25:52 +0200 | [diff] [blame] | 1900 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found srtp profile: %s", | 
|  | 1901 | mbedtls_ssl_get_srtp_profile_as_string( | 
|  | 1902 | server_protection ) ) ); | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1903 | } | 
|  | 1904 |  | 
| Johan Pascal | 43f9490 | 2020-09-22 12:25:52 +0200 | [diff] [blame] | 1905 | ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET; | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 1906 |  | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1907 | /* | 
|  | 1908 | * Check we have the server profile in our list | 
|  | 1909 | */ | 
| Ron Eldor | 3adb992 | 2017-12-21 10:15:08 +0200 | [diff] [blame] | 1910 | for( i=0; i < ssl->conf->dtls_srtp_profile_list_len; i++) | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1911 | { | 
| Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 1912 | if( server_protection == ssl->conf->dtls_srtp_profile_list[i] ) | 
|  | 1913 | { | 
| Ron Eldor | 3adb992 | 2017-12-21 10:15:08 +0200 | [diff] [blame] | 1914 | ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i]; | 
| Johan Pascal | 43f9490 | 2020-09-22 12:25:52 +0200 | [diff] [blame] | 1915 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected srtp profile: %s", | 
|  | 1916 | mbedtls_ssl_get_srtp_profile_as_string( | 
|  | 1917 | server_protection ) ) ); | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 1918 | break; | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1919 | } | 
|  | 1920 | } | 
|  | 1921 |  | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 1922 | /* If no match was found : server problem, it shall never answer with incompatible profile */ | 
| Johan Pascal | 43f9490 | 2020-09-22 12:25:52 +0200 | [diff] [blame] | 1923 | if( ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET ) | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 1924 | { | 
|  | 1925 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
| Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 1926 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 1927 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
|  | 1928 | } | 
| Johan Pascal | 20c7db3 | 2020-10-26 22:45:58 +0100 | [diff] [blame] | 1929 |  | 
|  | 1930 | /* If server does not use mki in its reply, make sure the client won't keep | 
|  | 1931 | * one as negotiated */ | 
|  | 1932 | if( len == 5 ) | 
|  | 1933 | { | 
|  | 1934 | ssl->dtls_srtp_info.mki_len = 0; | 
|  | 1935 | } | 
|  | 1936 |  | 
| Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 1937 | /* | 
|  | 1938 | * RFC5764: | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 1939 | *  If the client detects a nonzero-length MKI in the server's response | 
|  | 1940 | *  that is different than the one the client offered, then the client | 
|  | 1941 | *  MUST abort the handshake and SHOULD send an invalid_parameter alert. | 
|  | 1942 | */ | 
| Ron Eldor | 313d7b5 | 2018-12-10 14:56:21 +0200 | [diff] [blame] | 1943 | if( len > 5  && ( buf[4] != mki_len || | 
|  | 1944 | ( memcmp( ssl->dtls_srtp_info.mki_value, &buf[5], mki_len ) ) ) ) | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 1945 | { | 
|  | 1946 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1947 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); | 
|  | 1948 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
|  | 1949 | } | 
| Ron Eldor | b465539 | 2018-07-05 18:25:39 +0300 | [diff] [blame] | 1950 | #if defined (MBEDTLS_DEBUG_C) | 
| Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 1951 | if( len > 5 ) | 
| Ron Eldor | b465539 | 2018-07-05 18:25:39 +0300 | [diff] [blame] | 1952 | { | 
| Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 1953 | MBEDTLS_SSL_DEBUG_BUF( 3, "received mki", ssl->dtls_srtp_info.mki_value, | 
|  | 1954 | ssl->dtls_srtp_info.mki_len ); | 
| Ron Eldor | b465539 | 2018-07-05 18:25:39 +0300 | [diff] [blame] | 1955 | } | 
|  | 1956 | #endif | 
| Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 1957 | return( 0 ); | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1958 | } | 
|  | 1959 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ | 
|  | 1960 |  | 
| Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1961 | /* | 
|  | 1962 | * Parse HelloVerifyRequest.  Only called after verifying the HS type. | 
|  | 1963 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1964 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 1965 | 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] | 1966 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1967 | 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] | 1968 | int major_ver, minor_ver; | 
|  | 1969 | unsigned char cookie_len; | 
|  | 1970 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1971 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse hello verify request" ) ); | 
| Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1972 |  | 
| Gilles Peskine | b64bf06 | 2019-09-27 14:02:44 +0200 | [diff] [blame] | 1973 | /* Check that there is enough room for: | 
|  | 1974 | * - 2 bytes of version | 
|  | 1975 | * - 1 byte of cookie_len | 
|  | 1976 | */ | 
|  | 1977 | if( mbedtls_ssl_hs_hdr_len( ssl ) + 3 > ssl->in_msglen ) | 
|  | 1978 | { | 
|  | 1979 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 1980 | ( "incoming HelloVerifyRequest message is too short" ) ); | 
|  | 1981 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1982 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
|  | 1983 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
|  | 1984 | } | 
|  | 1985 |  | 
| Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1986 | /* | 
|  | 1987 | * struct { | 
|  | 1988 | *   ProtocolVersion server_version; | 
|  | 1989 | *   opaque cookie<0..2^8-1>; | 
|  | 1990 | * } HelloVerifyRequest; | 
|  | 1991 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1992 | MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 ); | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1993 | 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] | 1994 | p += 2; | 
|  | 1995 |  | 
| Manuel Pégourié-Gonnard | b35fe56 | 2014-08-09 17:00:46 +0200 | [diff] [blame] | 1996 | /* | 
|  | 1997 | * Since the RFC is not clear on this point, accept DTLS 1.0 (TLS 1.1) | 
|  | 1998 | * even is lower than our min version. | 
|  | 1999 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2000 | if( major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 || | 
|  | 2001 | minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 || | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2002 | major_ver > ssl->conf->max_major_ver  || | 
|  | 2003 | minor_ver > ssl->conf->max_minor_ver  ) | 
| Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 2004 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2005 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server version" ) ); | 
| Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 2006 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2007 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 2008 | MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION ); | 
| Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 2009 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2010 | return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION ); | 
| Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 2011 | } | 
|  | 2012 |  | 
|  | 2013 | cookie_len = *p++; | 
| Andres AG | 5a87c93 | 2016-09-26 14:53:05 +0100 | [diff] [blame] | 2014 | if( ( ssl->in_msg + ssl->in_msglen ) - p < cookie_len ) | 
|  | 2015 | { | 
|  | 2016 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 2017 | ( "cookie length does not match incoming message size" ) ); | 
|  | 2018 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 2019 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
|  | 2020 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
|  | 2021 | } | 
| Gilles Peskine | b51130d | 2019-09-27 14:00:36 +0200 | [diff] [blame] | 2022 | MBEDTLS_SSL_DEBUG_BUF( 3, "cookie", p, cookie_len ); | 
| Andres AG | 5a87c93 | 2016-09-26 14:53:05 +0100 | [diff] [blame] | 2023 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2024 | mbedtls_free( ssl->handshake->verify_cookie ); | 
| Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 2025 |  | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 2026 | ssl->handshake->verify_cookie = mbedtls_calloc( 1, cookie_len ); | 
| Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 2027 | if( ssl->handshake->verify_cookie  == NULL ) | 
|  | 2028 | { | 
| Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 2029 | 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] | 2030 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
| Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 2031 | } | 
|  | 2032 |  | 
|  | 2033 | memcpy( ssl->handshake->verify_cookie, p, cookie_len ); | 
|  | 2034 | ssl->handshake->verify_cookie_len = cookie_len; | 
|  | 2035 |  | 
| Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 2036 | /* Start over at ClientHello */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2037 | ssl->state = MBEDTLS_SSL_CLIENT_HELLO; | 
|  | 2038 | mbedtls_ssl_reset_checksum( ssl ); | 
| Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 2039 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2040 | mbedtls_ssl_recv_flight_completed( ssl ); | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2041 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2042 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse hello verify request" ) ); | 
| Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 2043 |  | 
|  | 2044 | return( 0 ); | 
|  | 2045 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2046 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 2047 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2048 | static int ssl_parse_server_hello( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2049 | { | 
| Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 2050 | int ret, i; | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 2051 | size_t n; | 
| Manuel Pégourié-Gonnard | f7cdbc0 | 2014-10-17 17:02:10 +0200 | [diff] [blame] | 2052 | size_t ext_len; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2053 | unsigned char *buf, *ext; | 
| Manuel Pégourié-Gonnard | 1cf7b30 | 2015-06-24 22:28:19 +0200 | [diff] [blame] | 2054 | unsigned char comp; | 
|  | 2055 | #if defined(MBEDTLS_ZLIB_SUPPORT) | 
|  | 2056 | int accept_comp; | 
|  | 2057 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2058 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2059 | int renegotiation_info_seen = 0; | 
| Manuel Pégourié-Gonnard | eaecbd3 | 2014-11-06 02:38:02 +0100 | [diff] [blame] | 2060 | #endif | 
| Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2061 | int handshake_failure = 0; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2062 | const mbedtls_ssl_ciphersuite_t *suite_info; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2063 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2064 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2065 |  | 
| Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 2066 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2067 | { | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2068 | /* No alert on a read error. */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2069 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2070 | return( ret ); | 
|  | 2071 | } | 
|  | 2072 |  | 
| Hanno Becker | 79594fd | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 2073 | buf = ssl->in_msg; | 
|  | 2074 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2075 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2076 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2077 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 2078 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) | 
| Manuel Pégourié-Gonnard | 6591962 | 2014-08-19 12:50:30 +0200 | [diff] [blame] | 2079 | { | 
| Manuel Pégourié-Gonnard | 44ade65 | 2014-08-19 13:58:40 +0200 | [diff] [blame] | 2080 | ssl->renego_records_seen++; | 
|  | 2081 |  | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2082 | if( ssl->conf->renego_max_records >= 0 && | 
|  | 2083 | ssl->renego_records_seen > ssl->conf->renego_max_records ) | 
| Manuel Pégourié-Gonnard | 44ade65 | 2014-08-19 13:58:40 +0200 | [diff] [blame] | 2084 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2085 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 2086 | ( "renegotiation requested, but not honored by server" ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2087 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); | 
| Manuel Pégourié-Gonnard | 44ade65 | 2014-08-19 13:58:40 +0200 | [diff] [blame] | 2088 | } | 
|  | 2089 |  | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2090 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 2091 | ( "non-handshake message during renegotiation" ) ); | 
| Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 2092 |  | 
|  | 2093 | ssl->keep_current_message = 1; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2094 | return( MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO ); | 
| Manuel Pégourié-Gonnard | 6591962 | 2014-08-19 12:50:30 +0200 | [diff] [blame] | 2095 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2096 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ | 
| Manuel Pégourié-Gonnard | 6591962 | 2014-08-19 12:50:30 +0200 | [diff] [blame] | 2097 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2098 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2099 | mbedtls_ssl_send_alert_message( | 
|  | 2100 | ssl, | 
|  | 2101 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 2102 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2103 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2104 | } | 
|  | 2105 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2106 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2107 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 2108 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2109 | if( buf[0] == MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST ) | 
| Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 2110 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2111 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "received hello verify request" ) ); | 
|  | 2112 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) ); | 
| Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 2113 | return( ssl_parse_hello_verify_request( ssl ) ); | 
|  | 2114 | } | 
|  | 2115 | else | 
|  | 2116 | { | 
|  | 2117 | /* We made it through the verification process */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2118 | mbedtls_free( ssl->handshake->verify_cookie ); | 
| Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 2119 | ssl->handshake->verify_cookie = NULL; | 
|  | 2120 | ssl->handshake->verify_cookie_len = 0; | 
|  | 2121 | } | 
|  | 2122 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2123 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2124 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2125 | if( ssl->in_hslen < 38 + mbedtls_ssl_hs_hdr_len( ssl ) || | 
|  | 2126 | buf[0] != MBEDTLS_SSL_HS_SERVER_HELLO ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2127 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2128 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2129 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 2130 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2131 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2132 | } | 
|  | 2133 |  | 
| Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 2134 | /* | 
|  | 2135 | *  0   .  1    server_version | 
|  | 2136 | *  2   . 33    random (maybe including 4 bytes of Unix time) | 
|  | 2137 | * 34   . 34    session_id length = n | 
|  | 2138 | * 35   . 34+n  session_id | 
|  | 2139 | * 35+n . 36+n  cipher_suite | 
|  | 2140 | * 37+n . 37+n  compression_method | 
|  | 2141 | * | 
|  | 2142 | * 38+n . 39+n  extensions length (optional) | 
|  | 2143 | * 40+n .  ..   extensions | 
|  | 2144 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2145 | buf += mbedtls_ssl_hs_hdr_len( ssl ); | 
| Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 2146 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2147 | MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", buf + 0, 2 ); | 
|  | 2148 | mbedtls_ssl_read_version( &ssl->major_ver, &ssl->minor_ver, | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2149 | ssl->conf->transport, buf + 0 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2150 |  | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2151 | if( ssl->major_ver < ssl->conf->min_major_ver || | 
|  | 2152 | ssl->minor_ver < ssl->conf->min_minor_ver || | 
|  | 2153 | ssl->major_ver > ssl->conf->max_major_ver || | 
|  | 2154 | ssl->minor_ver > ssl->conf->max_minor_ver ) | 
| Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 2155 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2156 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 2157 | ( "server version out of bounds -  min: [%d:%d], server: [%d:%d], max: [%d:%d]", | 
|  | 2158 | ssl->conf->min_major_ver, | 
|  | 2159 | ssl->conf->min_minor_ver, | 
|  | 2160 | ssl->major_ver, ssl->minor_ver, | 
|  | 2161 | ssl->conf->max_major_ver, | 
|  | 2162 | ssl->conf->max_minor_ver ) ); | 
| Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 2163 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2164 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 2165 | MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION ); | 
| Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 2166 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2167 | return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION ); | 
| Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 2168 | } | 
|  | 2169 |  | 
| Andres Amaya Garcia | 6bce9cb | 2017-09-06 15:33:34 +0100 | [diff] [blame] | 2170 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", | 
| Paul Elliott | 9f35211 | 2020-12-09 14:55:45 +0000 | [diff] [blame] | 2171 | ( (unsigned long) buf[2] << 24 ) | | 
|  | 2172 | ( (unsigned long) buf[3] << 16 ) | | 
|  | 2173 | ( (unsigned long) buf[4] <<  8 ) | | 
|  | 2174 | ( (unsigned long) buf[5]       ) ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2175 |  | 
| Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 2176 | memcpy( ssl->handshake->randbytes + 32, buf + 2, 32 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2177 |  | 
| Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 2178 | n = buf[34]; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2179 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2180 | MBEDTLS_SSL_DEBUG_BUF( 3,   "server hello, random bytes", buf + 2, 32 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2181 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2182 | if( n > 32 ) | 
|  | 2183 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2184 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2185 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 2186 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2187 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2188 | } | 
|  | 2189 |  | 
| Manuel Pégourié-Gonnard | a6e5bd5 | 2015-07-23 12:14:13 +0200 | [diff] [blame] | 2190 | if( ssl->in_hslen > mbedtls_ssl_hs_hdr_len( ssl ) + 39 + n ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2191 | { | 
| Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 2192 | ext_len = ( ( buf[38 + n] <<  8 ) | 
|  | 2193 | | ( buf[39 + n]       ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2194 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2195 | if( ( ext_len > 0 && ext_len < 4 ) || | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2196 | 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] | 2197 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2198 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2199 | mbedtls_ssl_send_alert_message( | 
|  | 2200 | ssl, | 
|  | 2201 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 2202 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2203 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2204 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2205 | } | 
| Manuel Pégourié-Gonnard | a6e5bd5 | 2015-07-23 12:14:13 +0200 | [diff] [blame] | 2206 | 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] | 2207 | { | 
|  | 2208 | ext_len = 0; | 
|  | 2209 | } | 
|  | 2210 | else | 
|  | 2211 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2212 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2213 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 2214 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2215 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
| Manuel Pégourié-Gonnard | f7cdbc0 | 2014-10-17 17:02:10 +0200 | [diff] [blame] | 2216 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2217 |  | 
| Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 2218 | /* ciphersuite (used later) */ | 
| Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 2219 | i = ( buf[35 + n] << 8 ) | buf[36 + n]; | 
| Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 2220 |  | 
|  | 2221 | /* | 
|  | 2222 | * Read and check compression | 
|  | 2223 | */ | 
| Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 2224 | comp = buf[37 + n]; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2225 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2226 | #if defined(MBEDTLS_ZLIB_SUPPORT) | 
| Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 2227 | /* See comments in ssl_write_client_hello() */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2228 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2229 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 2230 | accept_comp = 0; | 
| Manuel Pégourié-Gonnard | 1cf7b30 | 2015-06-24 22:28:19 +0200 | [diff] [blame] | 2231 | else | 
| Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 2232 | #endif | 
| Manuel Pégourié-Gonnard | 1cf7b30 | 2015-06-24 22:28:19 +0200 | [diff] [blame] | 2233 | accept_comp = 1; | 
| Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 2234 |  | 
| Manuel Pégourié-Gonnard | 1cf7b30 | 2015-06-24 22:28:19 +0200 | [diff] [blame] | 2235 | if( comp != MBEDTLS_SSL_COMPRESS_NULL && | 
|  | 2236 | ( comp != MBEDTLS_SSL_COMPRESS_DEFLATE || accept_comp == 0 ) ) | 
|  | 2237 | #else /* MBEDTLS_ZLIB_SUPPORT */ | 
|  | 2238 | if( comp != MBEDTLS_SSL_COMPRESS_NULL ) | 
|  | 2239 | #endif/* MBEDTLS_ZLIB_SUPPORT */ | 
| Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 2240 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2241 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 2242 | ( "server hello, bad compression: %d", comp ) ); | 
|  | 2243 | mbedtls_ssl_send_alert_message( | 
|  | 2244 | ssl, | 
|  | 2245 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 2246 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2247 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
| Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 2248 | } | 
|  | 2249 |  | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2250 | /* | 
|  | 2251 | * Initialize update checksum functions | 
|  | 2252 | */ | 
| Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 2253 | ssl->handshake->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( i ); | 
|  | 2254 | if( ssl->handshake->ciphersuite_info == NULL ) | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2255 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2256 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
| Paul Elliott | 9f35211 | 2020-12-09 14:55:45 +0000 | [diff] [blame] | 2257 | ( "ciphersuite info for %04x not found", (unsigned int)i ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2258 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 2259 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2260 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2261 | } | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2262 |  | 
| Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 2263 | mbedtls_ssl_optimize_checksum( ssl, ssl->handshake->ciphersuite_info ); | 
| Manuel Pégourié-Gonnard | 3c599f1 | 2014-03-10 13:25:07 +0100 | [diff] [blame] | 2264 |  | 
| Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 2265 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2266 | MBEDTLS_SSL_DEBUG_BUF( 3,   "server hello, session id", buf + 35, n ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2267 |  | 
|  | 2268 | /* | 
|  | 2269 | * Check if the session can be resumed | 
|  | 2270 | */ | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2271 | if( ssl->handshake->resume == 0 || n == 0 || | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2272 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 2273 | ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE || | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2274 | #endif | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2275 | ssl->session_negotiate->ciphersuite != i || | 
|  | 2276 | ssl->session_negotiate->compression != comp || | 
| Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 2277 | ssl->session_negotiate->id_len != n || | 
| Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 2278 | memcmp( ssl->session_negotiate->id, buf + 35, n ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2279 | { | 
|  | 2280 | ssl->state++; | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 2281 | ssl->handshake->resume = 0; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2282 | #if defined(MBEDTLS_HAVE_TIME) | 
| SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 2283 | ssl->session_negotiate->start = mbedtls_time( NULL ); | 
| Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 2284 | #endif | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2285 | ssl->session_negotiate->ciphersuite = i; | 
|  | 2286 | ssl->session_negotiate->compression = comp; | 
| Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 2287 | ssl->session_negotiate->id_len = n; | 
| Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 2288 | memcpy( ssl->session_negotiate->id, buf + 35, n ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2289 | } | 
|  | 2290 | else | 
|  | 2291 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2292 | ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC; | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 2293 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2294 | if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 ) | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 2295 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2296 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2297 | mbedtls_ssl_send_alert_message( | 
|  | 2298 | ssl, | 
|  | 2299 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 2300 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 2301 | return( ret ); | 
|  | 2302 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2303 | } | 
|  | 2304 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2305 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed", | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 2306 | ssl->handshake->resume ? "a" : "no" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2307 |  | 
| Paul Elliott | 3891caf | 2020-12-17 18:42:40 +0000 | [diff] [blame] | 2308 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %04x", (unsigned) i ) ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2309 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d", | 
|  | 2310 | buf[37 + n] ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2311 |  | 
| Andrzej Kurek | 03bac44 | 2018-04-25 05:06:07 -0400 | [diff] [blame] | 2312 | /* | 
|  | 2313 | * 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] | 2314 | */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2315 | i = 0; | 
|  | 2316 | while( 1 ) | 
|  | 2317 | { | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2318 | if( ssl->conf->ciphersuite_list[ssl->minor_ver][i] == 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2319 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2320 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2321 | mbedtls_ssl_send_alert_message( | 
|  | 2322 | ssl, | 
|  | 2323 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 2324 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2325 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2326 | } | 
|  | 2327 |  | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2328 | if( ssl->conf->ciphersuite_list[ssl->minor_ver][i++] == | 
| Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 2329 | ssl->session_negotiate->ciphersuite ) | 
|  | 2330 | { | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2331 | break; | 
| Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 2332 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2333 | } | 
|  | 2334 |  | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2335 | suite_info = mbedtls_ssl_ciphersuite_from_id( | 
|  | 2336 | ssl->session_negotiate->ciphersuite ); | 
|  | 2337 | if( ssl_validate_ciphersuite( suite_info, ssl, ssl->minor_ver, | 
|  | 2338 | ssl->minor_ver ) != 0 ) | 
| Mohammad Azim Khan | 1d3b508 | 2018-04-18 19:35:00 +0100 | [diff] [blame] | 2339 | { | 
|  | 2340 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2341 | mbedtls_ssl_send_alert_message( | 
|  | 2342 | ssl, | 
|  | 2343 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 2344 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); | 
| Mohammad Azim Khan | 1d3b508 | 2018-04-18 19:35:00 +0100 | [diff] [blame] | 2345 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
|  | 2346 | } | 
|  | 2347 |  | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2348 | MBEDTLS_SSL_DEBUG_MSG( 3, | 
|  | 2349 | ( "server hello, chosen ciphersuite: %s", suite_info->name ) ); | 
| Mohammad Azim Khan | 1d3b508 | 2018-04-18 19:35:00 +0100 | [diff] [blame] | 2350 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2351 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
| Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 2352 | if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA && | 
|  | 2353 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) | 
|  | 2354 | { | 
|  | 2355 | ssl->handshake->ecrs_enabled = 1; | 
|  | 2356 | } | 
|  | 2357 | #endif | 
|  | 2358 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2359 | if( comp != MBEDTLS_SSL_COMPRESS_NULL | 
|  | 2360 | #if defined(MBEDTLS_ZLIB_SUPPORT) | 
|  | 2361 | && comp != MBEDTLS_SSL_COMPRESS_DEFLATE | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2362 | #endif | 
|  | 2363 | ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2364 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2365 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2366 | mbedtls_ssl_send_alert_message( | 
|  | 2367 | ssl, | 
|  | 2368 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 2369 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2370 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2371 | } | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2372 | ssl->session_negotiate->compression = comp; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2373 |  | 
| Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 2374 | ext = buf + 40 + n; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2375 |  | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2376 | MBEDTLS_SSL_DEBUG_MSG( 2, | 
| Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 2377 | ( "server hello, total extension length: %" MBEDTLS_PRINTF_SIZET, ext_len ) ); | 
| Manuel Pégourié-Gonnard | a052849 | 2013-07-16 17:26:28 +0200 | [diff] [blame] | 2378 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2379 | while( ext_len ) | 
|  | 2380 | { | 
|  | 2381 | unsigned int ext_id   = ( ( ext[0] <<  8 ) | 
|  | 2382 | | ( ext[1]       ) ); | 
|  | 2383 | unsigned int ext_size = ( ( ext[2] <<  8 ) | 
|  | 2384 | | ( ext[3]       ) ); | 
|  | 2385 |  | 
|  | 2386 | if( ext_size + 4 > ext_len ) | 
|  | 2387 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2388 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2389 | mbedtls_ssl_send_alert_message( | 
|  | 2390 | ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 2391 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2392 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2393 | } | 
|  | 2394 |  | 
|  | 2395 | switch( ext_id ) | 
|  | 2396 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2397 | case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO: | 
|  | 2398 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) ); | 
|  | 2399 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2400 | renegotiation_info_seen = 1; | 
| Manuel Pégourié-Gonnard | eaecbd3 | 2014-11-06 02:38:02 +0100 | [diff] [blame] | 2401 | #endif | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2402 |  | 
| Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 2403 | if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4, | 
|  | 2404 | ext_size ) ) != 0 ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2405 | return( ret ); | 
|  | 2406 |  | 
|  | 2407 | break; | 
|  | 2408 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2409 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
|  | 2410 | case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH: | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2411 | MBEDTLS_SSL_DEBUG_MSG( 3, | 
|  | 2412 | ( "found max_fragment_length extension" ) ); | 
| Manuel Pégourié-Gonnard | de600e5 | 2013-07-17 10:14:38 +0200 | [diff] [blame] | 2413 |  | 
|  | 2414 | if( ( ret = ssl_parse_max_fragment_length_ext( ssl, | 
|  | 2415 | ext + 4, ext_size ) ) != 0 ) | 
|  | 2416 | { | 
|  | 2417 | return( ret ); | 
|  | 2418 | } | 
|  | 2419 |  | 
|  | 2420 | break; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2421 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ | 
| Manuel Pégourié-Gonnard | de600e5 | 2013-07-17 10:14:38 +0200 | [diff] [blame] | 2422 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2423 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) | 
|  | 2424 | case MBEDTLS_TLS_EXT_TRUNCATED_HMAC: | 
|  | 2425 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found truncated_hmac extension" ) ); | 
| Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 2426 |  | 
|  | 2427 | if( ( ret = ssl_parse_truncated_hmac_ext( ssl, | 
|  | 2428 | ext + 4, ext_size ) ) != 0 ) | 
|  | 2429 | { | 
|  | 2430 | return( ret ); | 
|  | 2431 | } | 
|  | 2432 |  | 
|  | 2433 | break; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2434 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ | 
| Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 2435 |  | 
| Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2436 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
| Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 2437 | case MBEDTLS_TLS_EXT_CID: | 
|  | 2438 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found CID extension" ) ); | 
|  | 2439 |  | 
|  | 2440 | if( ( ret = ssl_parse_cid_ext( ssl, | 
|  | 2441 | ext + 4, | 
|  | 2442 | ext_size ) ) != 0 ) | 
|  | 2443 | { | 
|  | 2444 | return( ret ); | 
|  | 2445 | } | 
|  | 2446 |  | 
|  | 2447 | break; | 
| Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2448 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
| Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 2449 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2450 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
|  | 2451 | case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC: | 
|  | 2452 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt_then_mac extension" ) ); | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2453 |  | 
|  | 2454 | if( ( ret = ssl_parse_encrypt_then_mac_ext( ssl, | 
|  | 2455 | ext + 4, ext_size ) ) != 0 ) | 
|  | 2456 | { | 
|  | 2457 | return( ret ); | 
|  | 2458 | } | 
|  | 2459 |  | 
|  | 2460 | break; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2461 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2462 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2463 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) | 
|  | 2464 | case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET: | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2465 | MBEDTLS_SSL_DEBUG_MSG( 3, | 
|  | 2466 | ( "found extended_master_secret extension" ) ); | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2467 |  | 
|  | 2468 | if( ( ret = ssl_parse_extended_ms_ext( ssl, | 
|  | 2469 | ext + 4, ext_size ) ) != 0 ) | 
|  | 2470 | { | 
|  | 2471 | return( ret ); | 
|  | 2472 | } | 
|  | 2473 |  | 
|  | 2474 | break; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2475 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2476 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2477 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | 
|  | 2478 | case MBEDTLS_TLS_EXT_SESSION_TICKET: | 
|  | 2479 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) ); | 
| Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 2480 |  | 
|  | 2481 | if( ( ret = ssl_parse_session_ticket_ext( ssl, | 
|  | 2482 | ext + 4, ext_size ) ) != 0 ) | 
|  | 2483 | { | 
|  | 2484 | return( ret ); | 
|  | 2485 | } | 
|  | 2486 |  | 
|  | 2487 | break; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2488 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ | 
| Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 2489 |  | 
| Robert Cragie | 136884c | 2015-10-02 13:34:31 +0100 | [diff] [blame] | 2490 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ | 
| Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 2491 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2492 | case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS: | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2493 | MBEDTLS_SSL_DEBUG_MSG( 3, | 
|  | 2494 | ( "found supported_point_formats extension" ) ); | 
| Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 2495 |  | 
|  | 2496 | if( ( ret = ssl_parse_supported_point_formats_ext( ssl, | 
|  | 2497 | ext + 4, ext_size ) ) != 0 ) | 
|  | 2498 | { | 
|  | 2499 | return( ret ); | 
|  | 2500 | } | 
|  | 2501 |  | 
|  | 2502 | break; | 
| Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 2503 | #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || | 
|  | 2504 | MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ | 
| Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 2505 |  | 
| Manuel Pégourié-Gonnard | 0a1324a | 2015-09-16 16:01:00 +0200 | [diff] [blame] | 2506 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
|  | 2507 | case MBEDTLS_TLS_EXT_ECJPAKE_KKPP: | 
|  | 2508 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake_kkpp extension" ) ); | 
|  | 2509 |  | 
|  | 2510 | if( ( ret = ssl_parse_ecjpake_kkpp( ssl, | 
|  | 2511 | ext + 4, ext_size ) ) != 0 ) | 
|  | 2512 | { | 
|  | 2513 | return( ret ); | 
|  | 2514 | } | 
|  | 2515 |  | 
|  | 2516 | break; | 
|  | 2517 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ | 
| Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2518 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2519 | #if defined(MBEDTLS_SSL_ALPN) | 
|  | 2520 | case MBEDTLS_TLS_EXT_ALPN: | 
|  | 2521 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) ); | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2522 |  | 
| Johan Pascal | 275874b | 2020-10-27 10:43:53 +0100 | [diff] [blame] | 2523 | if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 ) | 
|  | 2524 | return( ret ); | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2525 |  | 
|  | 2526 | break; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2527 | #endif /* MBEDTLS_SSL_ALPN */ | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2528 |  | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2529 | #if defined(MBEDTLS_SSL_DTLS_SRTP) | 
|  | 2530 | case MBEDTLS_TLS_EXT_USE_SRTP: | 
|  | 2531 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found use_srtp extension" ) ); | 
|  | 2532 |  | 
| Johan Pascal | c3ccd98 | 2020-10-28 17:18:18 +0100 | [diff] [blame] | 2533 | if( ( ret = ssl_parse_use_srtp_ext( ssl, ext + 4, ext_size ) ) != 0 ) | 
|  | 2534 | return( ret ); | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2535 |  | 
|  | 2536 | break; | 
|  | 2537 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ | 
|  | 2538 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2539 | default: | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2540 | MBEDTLS_SSL_DEBUG_MSG( 3, | 
| Paul Elliott | 9f35211 | 2020-12-09 14:55:45 +0000 | [diff] [blame] | 2541 | ( "unknown extension found: %u (ignoring)", ext_id ) ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2542 | } | 
|  | 2543 |  | 
|  | 2544 | ext_len -= 4 + ext_size; | 
|  | 2545 | ext += 4 + ext_size; | 
|  | 2546 |  | 
|  | 2547 | if( ext_len > 0 && ext_len < 4 ) | 
|  | 2548 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2549 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); | 
|  | 2550 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2551 | } | 
|  | 2552 | } | 
|  | 2553 |  | 
|  | 2554 | /* | 
|  | 2555 | * Renegotiation security checks | 
|  | 2556 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2557 | if( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2558 | ssl->conf->allow_legacy_renegotiation == | 
|  | 2559 | MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2560 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2561 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 2562 | ( "legacy renegotiation, breaking off handshake" ) ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2563 | handshake_failure = 1; | 
|  | 2564 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2565 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 2566 | else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && | 
|  | 2567 | ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION && | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2568 | renegotiation_info_seen == 0 ) | 
|  | 2569 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2570 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 2571 | ( "renegotiation_info extension missing (secure)" ) ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2572 | handshake_failure = 1; | 
|  | 2573 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2574 | else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && | 
|  | 2575 | ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2576 | ssl->conf->allow_legacy_renegotiation == | 
|  | 2577 | MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2578 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2579 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) ); | 
| Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2580 | handshake_failure = 1; | 
|  | 2581 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2582 | else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && | 
|  | 2583 | ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && | 
| Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2584 | renegotiation_info_seen == 1 ) | 
|  | 2585 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2586 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 2587 | ( "renegotiation_info extension present (legacy)" ) ); | 
| Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2588 | handshake_failure = 1; | 
|  | 2589 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2590 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ | 
| Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2591 |  | 
|  | 2592 | if( handshake_failure == 1 ) | 
|  | 2593 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2594 | mbedtls_ssl_send_alert_message( | 
|  | 2595 | ssl, | 
|  | 2596 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 2597 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2598 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2599 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2600 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2601 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2602 |  | 
|  | 2603 | return( 0 ); | 
|  | 2604 | } | 
|  | 2605 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2606 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) ||                       \ | 
|  | 2607 | defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2608 | static int ssl_parse_server_dh_params( mbedtls_ssl_context *ssl, | 
|  | 2609 | unsigned char **p, | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2610 | unsigned char *end ) | 
|  | 2611 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2612 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2613 |  | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2614 | /* | 
|  | 2615 | * Ephemeral DH parameters: | 
|  | 2616 | * | 
|  | 2617 | * struct { | 
|  | 2618 | *     opaque dh_p<1..2^16-1>; | 
|  | 2619 | *     opaque dh_g<1..2^16-1>; | 
|  | 2620 | *     opaque dh_Ys<1..2^16-1>; | 
|  | 2621 | * } ServerDHParams; | 
|  | 2622 | */ | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2623 | if( ( ret = mbedtls_dhm_read_params( &ssl->handshake->dhm_ctx, | 
|  | 2624 | p, end ) ) != 0 ) | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2625 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2626 | MBEDTLS_SSL_DEBUG_RET( 2, ( "mbedtls_dhm_read_params" ), ret ); | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2627 | return( ret ); | 
|  | 2628 | } | 
|  | 2629 |  | 
| Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 2630 | if( ssl->handshake->dhm_ctx.len * 8 < ssl->conf->dhm_min_bitlen ) | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2631 | { | 
| Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 2632 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DHM prime too short: %" MBEDTLS_PRINTF_SIZET " < %u", | 
| Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 2633 | ssl->handshake->dhm_ctx.len * 8, | 
|  | 2634 | ssl->conf->dhm_min_bitlen ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2635 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2636 | } | 
|  | 2637 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2638 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P  ); | 
|  | 2639 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G  ); | 
|  | 2640 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY ); | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2641 |  | 
|  | 2642 | return( ret ); | 
|  | 2643 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2644 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED || | 
|  | 2645 | MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2646 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2647 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) ||                     \ | 
|  | 2648 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) ||                   \ | 
|  | 2649 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) ||                     \ | 
|  | 2650 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||                      \ | 
|  | 2651 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) | 
|  | 2652 | 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] | 2653 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2654 | const mbedtls_ecp_curve_info *curve_info; | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 2655 | mbedtls_ecp_group_id grp_id; | 
|  | 2656 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) | 
|  | 2657 | grp_id = ssl->handshake->ecdh_ctx.grp.id; | 
|  | 2658 | #else | 
|  | 2659 | grp_id = ssl->handshake->ecdh_ctx.grp_id; | 
|  | 2660 | #endif | 
| Manuel Pégourié-Gonnard | c3f6b62c | 2014-02-06 10:13:09 +0100 | [diff] [blame] | 2661 |  | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 2662 | 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] | 2663 | if( curve_info == NULL ) | 
|  | 2664 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2665 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 2666 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | c3f6b62c | 2014-02-06 10:13:09 +0100 | [diff] [blame] | 2667 | } | 
|  | 2668 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2669 | 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] | 2670 |  | 
| Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 2671 | #if defined(MBEDTLS_ECP_C) | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 2672 | if( mbedtls_ssl_check_curve( ssl, grp_id ) != 0 ) | 
| Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 2673 | #else | 
| Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2674 | if( ssl->handshake->ecdh_ctx.grp.nbits < 163 || | 
|  | 2675 | ssl->handshake->ecdh_ctx.grp.nbits > 521 ) | 
| Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 2676 | #endif | 
| Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2677 | return( -1 ); | 
| Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2678 |  | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 2679 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, | 
|  | 2680 | MBEDTLS_DEBUG_ECDH_QP ); | 
| Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2681 |  | 
|  | 2682 | return( 0 ); | 
|  | 2683 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2684 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || | 
|  | 2685 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED || | 
|  | 2686 | MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED || | 
|  | 2687 | MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED || | 
|  | 2688 | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */ | 
| Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2689 |  | 
| Hanno Becker | bb89e27 | 2019-01-08 12:54:37 +0000 | [diff] [blame] | 2690 | #if defined(MBEDTLS_USE_PSA_CRYPTO) &&                           \ | 
|  | 2691 | ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) ||     \ | 
|  | 2692 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) ) | 
|  | 2693 | static int ssl_parse_server_ecdh_params_psa( mbedtls_ssl_context *ssl, | 
|  | 2694 | unsigned char **p, | 
|  | 2695 | unsigned char *end ) | 
|  | 2696 | { | 
|  | 2697 | uint16_t tls_id; | 
| Gilles Peskine | 4245980 | 2019-12-19 13:31:53 +0100 | [diff] [blame] | 2698 | size_t ecdh_bits = 0; | 
| Hanno Becker | bb89e27 | 2019-01-08 12:54:37 +0000 | [diff] [blame] | 2699 | uint8_t ecpoint_len; | 
|  | 2700 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; | 
|  | 2701 |  | 
|  | 2702 | /* | 
|  | 2703 | * Parse ECC group | 
|  | 2704 | */ | 
|  | 2705 |  | 
|  | 2706 | if( end - *p < 4 ) | 
|  | 2707 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); | 
|  | 2708 |  | 
|  | 2709 | /* First byte is curve_type; only named_curve is handled */ | 
|  | 2710 | if( *(*p)++ != MBEDTLS_ECP_TLS_NAMED_CURVE ) | 
|  | 2711 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); | 
|  | 2712 |  | 
|  | 2713 | /* Next two bytes are the namedcurve value */ | 
|  | 2714 | tls_id = *(*p)++; | 
|  | 2715 | tls_id <<= 8; | 
|  | 2716 | tls_id |= *(*p)++; | 
|  | 2717 |  | 
|  | 2718 | /* Convert EC group to PSA key type. */ | 
| Gilles Peskine | 4245980 | 2019-12-19 13:31:53 +0100 | [diff] [blame] | 2719 | if( ( handshake->ecdh_psa_type = | 
|  | 2720 | mbedtls_psa_parse_tls_ecc_group( tls_id, &ecdh_bits ) ) == 0 ) | 
| Hanno Becker | bb89e27 | 2019-01-08 12:54:37 +0000 | [diff] [blame] | 2721 | { | 
|  | 2722 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); | 
|  | 2723 | } | 
| Gilles Peskine | 4245980 | 2019-12-19 13:31:53 +0100 | [diff] [blame] | 2724 | if( ecdh_bits > 0xffff ) | 
|  | 2725 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); | 
|  | 2726 | handshake->ecdh_bits = (uint16_t) ecdh_bits; | 
| Hanno Becker | bb89e27 | 2019-01-08 12:54:37 +0000 | [diff] [blame] | 2727 |  | 
|  | 2728 | /* | 
|  | 2729 | * Put peer's ECDH public key in the format understood by PSA. | 
|  | 2730 | */ | 
|  | 2731 |  | 
|  | 2732 | ecpoint_len = *(*p)++; | 
|  | 2733 | if( (size_t)( end - *p ) < ecpoint_len ) | 
|  | 2734 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); | 
|  | 2735 |  | 
| Gilles Peskine | 4245980 | 2019-12-19 13:31:53 +0100 | [diff] [blame] | 2736 | if( mbedtls_psa_tls_ecpoint_to_psa_ec( | 
| Hanno Becker | bb89e27 | 2019-01-08 12:54:37 +0000 | [diff] [blame] | 2737 | *p, ecpoint_len, | 
|  | 2738 | handshake->ecdh_psa_peerkey, | 
|  | 2739 | sizeof( handshake->ecdh_psa_peerkey ), | 
|  | 2740 | &handshake->ecdh_psa_peerkey_len ) != 0 ) | 
|  | 2741 | { | 
|  | 2742 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
|  | 2743 | } | 
|  | 2744 |  | 
|  | 2745 | *p += ecpoint_len; | 
|  | 2746 | return( 0 ); | 
|  | 2747 | } | 
|  | 2748 | #endif /* MBEDTLS_USE_PSA_CRYPTO && | 
|  | 2749 | ( MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || | 
|  | 2750 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ) */ | 
|  | 2751 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2752 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) ||                     \ | 
|  | 2753 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) ||                   \ | 
|  | 2754 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) | 
|  | 2755 | static int ssl_parse_server_ecdh_params( mbedtls_ssl_context *ssl, | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2756 | unsigned char **p, | 
|  | 2757 | unsigned char *end ) | 
|  | 2758 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2759 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2760 |  | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2761 | /* | 
|  | 2762 | * Ephemeral ECDH parameters: | 
|  | 2763 | * | 
|  | 2764 | * struct { | 
|  | 2765 | *     ECParameters curve_params; | 
|  | 2766 | *     ECPoint      public; | 
|  | 2767 | * } ServerECDHParams; | 
|  | 2768 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2769 | if( ( ret = mbedtls_ecdh_read_params( &ssl->handshake->ecdh_ctx, | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2770 | (const unsigned char **) p, end ) ) != 0 ) | 
|  | 2771 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2772 | MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_read_params" ), ret ); | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2773 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
| Manuel Pégourié-Gonnard | 1c1c20e | 2018-09-12 10:34:43 +0200 | [diff] [blame] | 2774 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) | 
|  | 2775 | ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS; | 
| Manuel Pégourié-Gonnard | 558da9c | 2018-06-13 12:02:12 +0200 | [diff] [blame] | 2776 | #endif | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2777 | return( ret ); | 
|  | 2778 | } | 
|  | 2779 |  | 
| Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2780 | if( ssl_check_server_ecdh_params( ssl ) != 0 ) | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2781 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2782 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 2783 | ( "bad server key exchange message (ECDHE curve)" ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2784 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2785 | } | 
|  | 2786 |  | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2787 | return( ret ); | 
|  | 2788 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2789 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || | 
|  | 2790 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED || | 
|  | 2791 | MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2792 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2793 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2794 | static int ssl_parse_server_psk_hint( mbedtls_ssl_context *ssl, | 
| Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2795 | unsigned char **p, | 
|  | 2796 | unsigned char *end ) | 
|  | 2797 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2798 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; | 
| irwir | 6527bd6 | 2019-09-21 18:51:25 +0300 | [diff] [blame] | 2799 | uint16_t  len; | 
| Paul Bakker | c5a79cc | 2013-06-26 15:08:35 +0200 | [diff] [blame] | 2800 | ((void) ssl); | 
| Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2801 |  | 
|  | 2802 | /* | 
|  | 2803 | * PSK parameters: | 
|  | 2804 | * | 
|  | 2805 | * opaque psk_identity_hint<0..2^16-1>; | 
|  | 2806 | */ | 
| Hanno Becker | 0c161d1 | 2018-10-08 13:40:50 +0100 | [diff] [blame] | 2807 | if( end - (*p) < 2 ) | 
| Krzysztof Stachowiak | 740b218 | 2018-03-13 11:31:14 +0100 | [diff] [blame] | 2808 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2809 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 2810 | ( "bad server key exchange message (psk_identity_hint length)" ) ); | 
| Krzysztof Stachowiak | 740b218 | 2018-03-13 11:31:14 +0100 | [diff] [blame] | 2811 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); | 
|  | 2812 | } | 
| Manuel Pégourié-Gonnard | 59b9fe2 | 2013-10-15 11:55:33 +0200 | [diff] [blame] | 2813 | len = (*p)[0] << 8 | (*p)[1]; | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2814 | *p += 2; | 
| Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2815 |  | 
| irwir | 6527bd6 | 2019-09-21 18:51:25 +0300 | [diff] [blame] | 2816 | if( end - (*p) < len ) | 
| Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2817 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2818 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 2819 | ( "bad server key exchange message (psk_identity_hint length)" ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2820 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); | 
| Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2821 | } | 
|  | 2822 |  | 
| Manuel Pégourié-Gonnard | 9d62412 | 2016-02-22 11:10:14 +0100 | [diff] [blame] | 2823 | /* | 
|  | 2824 | * Note: we currently ignore the PKS identity hint, as we only allow one | 
|  | 2825 | * PSK to be provisionned on the client. This could be changed later if | 
|  | 2826 | * someone needs that feature. | 
|  | 2827 | */ | 
| Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2828 | *p += len; | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2829 | ret = 0; | 
| Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2830 |  | 
|  | 2831 | return( ret ); | 
|  | 2832 | } | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2833 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ | 
| Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2834 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2835 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) ||                           \ | 
|  | 2836 | defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2837 | /* | 
|  | 2838 | * Generate a pre-master secret and encrypt it with the server's RSA key | 
|  | 2839 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2840 | static int ssl_write_encrypted_pms( mbedtls_ssl_context *ssl, | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2841 | size_t offset, size_t *olen, | 
|  | 2842 | size_t pms_offset ) | 
|  | 2843 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2844 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2845 | 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] | 2846 | unsigned char *p = ssl->handshake->premaster + pms_offset; | 
| Hanno Becker | c7d7e29 | 2019-02-06 16:49:54 +0000 | [diff] [blame] | 2847 | mbedtls_pk_context * peer_pk; | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2848 |  | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2849 | if( offset + len_bytes > MBEDTLS_SSL_OUT_CONTENT_LEN ) | 
| Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 2850 | { | 
|  | 2851 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small for encrypted pms" ) ); | 
|  | 2852 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); | 
|  | 2853 | } | 
|  | 2854 |  | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2855 | /* | 
|  | 2856 | * Generate (part of) the pre-master as | 
|  | 2857 | *  struct { | 
|  | 2858 | *      ProtocolVersion client_version; | 
|  | 2859 | *      opaque random[46]; | 
|  | 2860 | *  } PreMasterSecret; | 
|  | 2861 | */ | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2862 | mbedtls_ssl_write_version( ssl->conf->max_major_ver, | 
|  | 2863 | ssl->conf->max_minor_ver, | 
|  | 2864 | ssl->conf->transport, p ); | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2865 |  | 
| Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 2866 | 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] | 2867 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2868 | MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret ); | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2869 | return( ret ); | 
|  | 2870 | } | 
|  | 2871 |  | 
|  | 2872 | ssl->handshake->pmslen = 48; | 
|  | 2873 |  | 
| Hanno Becker | c7d7e29 | 2019-02-06 16:49:54 +0000 | [diff] [blame] | 2874 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 2875 | peer_pk = &ssl->handshake->peer_pubkey; | 
|  | 2876 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
| Manuel Pégourié-Gonnard | 7f2f062 | 2015-09-03 10:44:32 +0200 | [diff] [blame] | 2877 | if( ssl->session_negotiate->peer_cert == NULL ) | 
|  | 2878 | { | 
| Hanno Becker | 8273df8 | 2019-02-06 17:37:32 +0000 | [diff] [blame] | 2879 | /* Should never happen */ | 
| Hanno Becker | 62d58ed | 2019-02-26 11:51:06 +0000 | [diff] [blame] | 2880 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
| Hanno Becker | 8273df8 | 2019-02-06 17:37:32 +0000 | [diff] [blame] | 2881 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | 7f2f062 | 2015-09-03 10:44:32 +0200 | [diff] [blame] | 2882 | } | 
| Hanno Becker | c7d7e29 | 2019-02-06 16:49:54 +0000 | [diff] [blame] | 2883 | peer_pk = &ssl->session_negotiate->peer_cert->pk; | 
|  | 2884 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
| Manuel Pégourié-Gonnard | 7f2f062 | 2015-09-03 10:44:32 +0200 | [diff] [blame] | 2885 |  | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2886 | /* | 
|  | 2887 | * Now write it out, encrypted | 
|  | 2888 | */ | 
| Hanno Becker | c7d7e29 | 2019-02-06 16:49:54 +0000 | [diff] [blame] | 2889 | if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_RSA ) ) | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2890 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2891 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) ); | 
|  | 2892 | return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH ); | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2893 | } | 
|  | 2894 |  | 
| Hanno Becker | c7d7e29 | 2019-02-06 16:49:54 +0000 | [diff] [blame] | 2895 | if( ( ret = mbedtls_pk_encrypt( peer_pk, | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2896 | p, ssl->handshake->pmslen, | 
|  | 2897 | ssl->out_msg + offset + len_bytes, olen, | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2898 | MBEDTLS_SSL_OUT_CONTENT_LEN - offset - len_bytes, | 
| Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 2899 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2900 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2901 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_rsa_pkcs1_encrypt", ret ); | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2902 | return( ret ); | 
|  | 2903 | } | 
|  | 2904 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2905 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ | 
|  | 2906 | defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2907 | if( len_bytes == 2 ) | 
|  | 2908 | { | 
|  | 2909 | ssl->out_msg[offset+0] = (unsigned char)( *olen >> 8 ); | 
|  | 2910 | ssl->out_msg[offset+1] = (unsigned char)( *olen      ); | 
|  | 2911 | *olen += 2; | 
|  | 2912 | } | 
|  | 2913 | #endif | 
|  | 2914 |  | 
| Hanno Becker | ae553dd | 2019-02-08 14:06:00 +0000 | [diff] [blame] | 2915 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 2916 | /* We don't need the peer's public key anymore. Free it. */ | 
|  | 2917 | mbedtls_pk_free( peer_pk ); | 
|  | 2918 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2919 | return( 0 ); | 
|  | 2920 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2921 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED || | 
|  | 2922 | MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2923 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2924 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Manuel Pégourié-Gonnard | 5c2a7ca | 2015-10-23 08:48:41 +0200 | [diff] [blame] | 2925 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) ||                       \ | 
|  | 2926 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) ||                     \ | 
|  | 2927 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2928 | static int ssl_parse_signature_algorithm( mbedtls_ssl_context *ssl, | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2929 | unsigned char **p, | 
|  | 2930 | unsigned char *end, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2931 | mbedtls_md_type_t *md_alg, | 
|  | 2932 | mbedtls_pk_type_t *pk_alg ) | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2933 | { | 
| Paul Bakker | c5a79cc | 2013-06-26 15:08:35 +0200 | [diff] [blame] | 2934 | ((void) ssl); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2935 | *md_alg = MBEDTLS_MD_NONE; | 
|  | 2936 | *pk_alg = MBEDTLS_PK_NONE; | 
| Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 2937 |  | 
|  | 2938 | /* Only in TLS 1.2 */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2939 | if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 ) | 
| Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 2940 | { | 
| Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 2941 | return( 0 ); | 
|  | 2942 | } | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2943 |  | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2944 | if( (*p) + 2 > end ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2945 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2946 |  | 
| Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 2947 | /* | 
|  | 2948 | * Get hash algorithm | 
|  | 2949 | */ | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2950 | if( ( *md_alg = mbedtls_ssl_md_alg_from_hash( (*p)[0] ) ) | 
|  | 2951 | == MBEDTLS_MD_NONE ) | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2952 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2953 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 2954 | ( "Server used unsupported HashAlgorithm %d", *(p)[0] ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2955 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2956 | } | 
|  | 2957 |  | 
| Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 2958 | /* | 
| Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 2959 | * Get signature algorithm | 
|  | 2960 | */ | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2961 | if( ( *pk_alg = mbedtls_ssl_pk_alg_from_sig( (*p)[1] ) ) | 
|  | 2962 | == MBEDTLS_PK_NONE ) | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2963 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2964 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 2965 | ( "server used unsupported SignatureAlgorithm %d", (*p)[1] ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2966 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2967 | } | 
|  | 2968 |  | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 2969 | /* | 
|  | 2970 | * Check if the hash is acceptable | 
|  | 2971 | */ | 
|  | 2972 | if( mbedtls_ssl_check_sig_hash( ssl, *md_alg ) != 0 ) | 
|  | 2973 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2974 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 2975 | ( "server used HashAlgorithm %d that was not offered", *(p)[0] ) ); | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 2976 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); | 
|  | 2977 | } | 
|  | 2978 |  | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2979 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d", | 
|  | 2980 | (*p)[1] ) ); | 
|  | 2981 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d", | 
|  | 2982 | (*p)[0] ) ); | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2983 | *p += 2; | 
|  | 2984 |  | 
|  | 2985 | return( 0 ); | 
|  | 2986 | } | 
| Manuel Pégourié-Gonnard | 5c2a7ca | 2015-10-23 08:48:41 +0200 | [diff] [blame] | 2987 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED || | 
|  | 2988 | MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || | 
|  | 2989 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2990 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2991 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2992 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ | 
|  | 2993 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) | 
|  | 2994 | 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] | 2995 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2996 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2997 | const mbedtls_ecp_keypair *peer_key; | 
| Hanno Becker | be7f508 | 2019-02-06 17:44:07 +0000 | [diff] [blame] | 2998 | mbedtls_pk_context * peer_pk; | 
| Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2999 |  | 
| Hanno Becker | be7f508 | 2019-02-06 17:44:07 +0000 | [diff] [blame] | 3000 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 3001 | peer_pk = &ssl->handshake->peer_pubkey; | 
|  | 3002 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
| Manuel Pégourié-Gonnard | 7f2f062 | 2015-09-03 10:44:32 +0200 | [diff] [blame] | 3003 | if( ssl->session_negotiate->peer_cert == NULL ) | 
|  | 3004 | { | 
| Hanno Becker | 8273df8 | 2019-02-06 17:37:32 +0000 | [diff] [blame] | 3005 | /* Should never happen */ | 
| Hanno Becker | bd5580a | 2019-02-26 12:36:01 +0000 | [diff] [blame] | 3006 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
| Hanno Becker | 8273df8 | 2019-02-06 17:37:32 +0000 | [diff] [blame] | 3007 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | 7f2f062 | 2015-09-03 10:44:32 +0200 | [diff] [blame] | 3008 | } | 
| Hanno Becker | be7f508 | 2019-02-06 17:44:07 +0000 | [diff] [blame] | 3009 | peer_pk = &ssl->session_negotiate->peer_cert->pk; | 
|  | 3010 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
| Manuel Pégourié-Gonnard | 7f2f062 | 2015-09-03 10:44:32 +0200 | [diff] [blame] | 3011 |  | 
| Hanno Becker | be7f508 | 2019-02-06 17:44:07 +0000 | [diff] [blame] | 3012 | if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_ECKEY ) ) | 
| Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 3013 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3014 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) ); | 
|  | 3015 | return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH ); | 
| Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 3016 | } | 
|  | 3017 |  | 
| Hanno Becker | be7f508 | 2019-02-06 17:44:07 +0000 | [diff] [blame] | 3018 | peer_key = mbedtls_pk_ec( *peer_pk ); | 
| Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 3019 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3020 | if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key, | 
|  | 3021 | MBEDTLS_ECDH_THEIRS ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 3022 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3023 | MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret ); | 
| Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 3024 | return( ret ); | 
|  | 3025 | } | 
|  | 3026 |  | 
|  | 3027 | if( ssl_check_server_ecdh_params( ssl ) != 0 ) | 
|  | 3028 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3029 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) ); | 
|  | 3030 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); | 
| Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 3031 | } | 
|  | 3032 |  | 
| Hanno Becker | ae553dd | 2019-02-08 14:06:00 +0000 | [diff] [blame] | 3033 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 3034 | /* We don't need the peer's public key anymore. Free it, | 
|  | 3035 | * so that more RAM is available for upcoming expensive | 
|  | 3036 | * operations like ECDHE. */ | 
|  | 3037 | mbedtls_pk_free( peer_pk ); | 
|  | 3038 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 3039 |  | 
| Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 3040 | return( ret ); | 
|  | 3041 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3042 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || | 
|  | 3043 | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */ | 
| Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 3044 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3045 | static int ssl_parse_server_key_exchange( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3046 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3047 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3048 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | 
| Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 3049 | ssl->handshake->ciphersuite_info; | 
| Andres Amaya Garcia | 53c77cc | 2017-06-27 16:15:06 +0100 | [diff] [blame] | 3050 | unsigned char *p = NULL, *end = NULL; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3051 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3052 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3053 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3054 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) | 
|  | 3055 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3056 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3057 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3058 | ssl->state++; | 
|  | 3059 | return( 0 ); | 
|  | 3060 | } | 
| Manuel Pégourié-Gonnard | bac0e3b | 2013-10-15 11:54:47 +0200 | [diff] [blame] | 3061 | ((void) p); | 
|  | 3062 | ((void) end); | 
|  | 3063 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3064 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3065 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ | 
|  | 3066 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) | 
|  | 3067 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA || | 
|  | 3068 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA ) | 
| Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 3069 | { | 
| Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 3070 | if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 ) | 
|  | 3071 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3072 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3073 | mbedtls_ssl_send_alert_message( | 
|  | 3074 | ssl, | 
|  | 3075 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 3076 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 3077 | return( ret ); | 
|  | 3078 | } | 
| Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 3079 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3080 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) ); | 
| Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 3081 | ssl->state++; | 
|  | 3082 | return( 0 ); | 
|  | 3083 | } | 
|  | 3084 | ((void) p); | 
|  | 3085 | ((void) end); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3086 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED || | 
|  | 3087 | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */ | 
| Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 3088 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3089 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
| Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 3090 | if( ssl->handshake->ecrs_enabled && | 
| Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 3091 | ssl->handshake->ecrs_state == ssl_ecrs_ske_start_processing ) | 
| Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 3092 | { | 
| Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 3093 | goto start_processing; | 
| Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 3094 | } | 
| Manuel Pégourié-Gonnard | 1f1f2a1 | 2017-05-18 11:27:06 +0200 | [diff] [blame] | 3095 | #endif | 
|  | 3096 |  | 
| Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 3097 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3098 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3099 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3100 | return( ret ); | 
|  | 3101 | } | 
|  | 3102 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3103 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3104 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3105 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3106 | mbedtls_ssl_send_alert_message( | 
|  | 3107 | ssl, | 
|  | 3108 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 3109 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3110 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3111 | } | 
|  | 3112 |  | 
| Manuel Pégourié-Gonnard | 09258b9 | 2013-10-15 10:43:36 +0200 | [diff] [blame] | 3113 | /* | 
|  | 3114 | * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server | 
|  | 3115 | * doesn't use a psk_identity_hint | 
|  | 3116 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3117 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3118 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3119 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || | 
|  | 3120 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) | 
| Paul Bakker | 188c8de | 2013-04-19 09:13:37 +0200 | [diff] [blame] | 3121 | { | 
| Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3122 | /* Current message is probably either | 
|  | 3123 | * CertificateRequest or ServerHelloDone */ | 
|  | 3124 | ssl->keep_current_message = 1; | 
| Paul Bakker | 188c8de | 2013-04-19 09:13:37 +0200 | [diff] [blame] | 3125 | goto exit; | 
|  | 3126 | } | 
|  | 3127 |  | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3128 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 3129 | ( "server key exchange message must not be skipped" ) ); | 
|  | 3130 | mbedtls_ssl_send_alert_message( | 
|  | 3131 | ssl, | 
|  | 3132 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 3133 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); | 
| Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3134 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3135 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3136 | } | 
|  | 3137 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3138 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
| Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 3139 | if( ssl->handshake->ecrs_enabled ) | 
|  | 3140 | ssl->handshake->ecrs_state = ssl_ecrs_ske_start_processing; | 
|  | 3141 |  | 
|  | 3142 | start_processing: | 
|  | 3143 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3144 | p   = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ); | 
| Paul Bakker | 3b6a07b | 2013-03-21 11:56:50 +0100 | [diff] [blame] | 3145 | end = ssl->in_msg + ssl->in_hslen; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3146 | MBEDTLS_SSL_DEBUG_BUF( 3,   "server key exchange", p, end - p ); | 
| Paul Bakker | 3b6a07b | 2013-03-21 11:56:50 +0100 | [diff] [blame] | 3147 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3148 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3149 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || | 
|  | 3150 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK || | 
|  | 3151 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK || | 
|  | 3152 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) | 
| Manuel Pégourié-Gonnard | 09258b9 | 2013-10-15 10:43:36 +0200 | [diff] [blame] | 3153 | { | 
|  | 3154 | if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 ) | 
|  | 3155 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3156 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3157 | mbedtls_ssl_send_alert_message( | 
|  | 3158 | ssl, | 
|  | 3159 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 3160 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3161 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); | 
| Manuel Pégourié-Gonnard | 09258b9 | 2013-10-15 10:43:36 +0200 | [diff] [blame] | 3162 | } | 
|  | 3163 | } /* FALLTROUGH */ | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3164 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ | 
| Manuel Pégourié-Gonnard | 09258b9 | 2013-10-15 10:43:36 +0200 | [diff] [blame] | 3165 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3166 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) ||                       \ | 
|  | 3167 | defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) | 
|  | 3168 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || | 
|  | 3169 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) | 
| Manuel Pégourié-Gonnard | 09258b9 | 2013-10-15 10:43:36 +0200 | [diff] [blame] | 3170 | ; /* nothing more to do */ | 
|  | 3171 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3172 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED || | 
|  | 3173 | MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ | 
|  | 3174 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) ||                       \ | 
|  | 3175 | defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) | 
|  | 3176 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA || | 
|  | 3177 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3178 | { | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 3179 | if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 ) | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3180 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3181 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3182 | mbedtls_ssl_send_alert_message( | 
|  | 3183 | ssl, | 
|  | 3184 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 3185 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3186 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); | 
| Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 3187 | } | 
|  | 3188 | } | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3189 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3190 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED || | 
|  | 3191 | MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ | 
| Hanno Becker | bb89e27 | 2019-01-08 12:54:37 +0000 | [diff] [blame] | 3192 | #if defined(MBEDTLS_USE_PSA_CRYPTO) &&                           \ | 
|  | 3193 | ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) ||     \ | 
|  | 3194 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) ) | 
|  | 3195 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA || | 
|  | 3196 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ) | 
|  | 3197 | { | 
|  | 3198 | if( ssl_parse_server_ecdh_params_psa( ssl, &p, end ) != 0 ) | 
|  | 3199 | { | 
|  | 3200 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3201 | mbedtls_ssl_send_alert_message( | 
|  | 3202 | ssl, | 
|  | 3203 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 3204 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); | 
| Hanno Becker | bb89e27 | 2019-01-08 12:54:37 +0000 | [diff] [blame] | 3205 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); | 
|  | 3206 | } | 
|  | 3207 | } | 
|  | 3208 | else | 
|  | 3209 | #endif /* MBEDTLS_USE_PSA_CRYPTO && | 
|  | 3210 | ( MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || | 
|  | 3211 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ) */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3212 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) ||                     \ | 
|  | 3213 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) ||                     \ | 
|  | 3214 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) | 
|  | 3215 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA || | 
|  | 3216 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK || | 
|  | 3217 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ) | 
| Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 3218 | { | 
|  | 3219 | if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 ) | 
|  | 3220 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3221 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3222 | mbedtls_ssl_send_alert_message( | 
|  | 3223 | ssl, | 
|  | 3224 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 3225 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3226 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3227 | } | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3228 | } | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3229 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3230 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || | 
|  | 3231 | MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED || | 
|  | 3232 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */ | 
| Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 3233 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
|  | 3234 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ) | 
|  | 3235 | { | 
|  | 3236 | ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx, | 
|  | 3237 | p, end - p ); | 
|  | 3238 | if( ret != 0 ) | 
|  | 3239 | { | 
|  | 3240 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3241 | mbedtls_ssl_send_alert_message( | 
|  | 3242 | ssl, | 
|  | 3243 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 3244 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); | 
| Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 3245 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); | 
|  | 3246 | } | 
|  | 3247 | } | 
|  | 3248 | else | 
|  | 3249 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3250 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3251 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 3252 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3253 | } | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3254 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3255 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) | 
| Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3256 | if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) ) | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3257 | { | 
| Manuel Pégourié-Gonnard | d92d6a1 | 2014-09-10 15:25:02 +0000 | [diff] [blame] | 3258 | size_t sig_len, hashlen; | 
|  | 3259 | unsigned char hash[64]; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3260 | mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE; | 
|  | 3261 | mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE; | 
|  | 3262 | 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] | 3263 | size_t params_len = p - params; | 
| Manuel Pégourié-Gonnard | 1f1f2a1 | 2017-05-18 11:27:06 +0200 | [diff] [blame] | 3264 | void *rs_ctx = NULL; | 
| Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 3265 |  | 
| Hanno Becker | a6899bb | 2019-02-06 18:26:03 +0000 | [diff] [blame] | 3266 | mbedtls_pk_context * peer_pk; | 
|  | 3267 |  | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 3268 | /* | 
|  | 3269 | * Handle the digitally-signed structure | 
|  | 3270 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3271 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 3272 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3273 | { | 
| Paul Bakker | 9659dae | 2013-08-28 16:21:34 +0200 | [diff] [blame] | 3274 | if( ssl_parse_signature_algorithm( ssl, &p, end, | 
|  | 3275 | &md_alg, &pk_alg ) != 0 ) | 
|  | 3276 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3277 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 3278 | ( "bad server key exchange message" ) ); | 
|  | 3279 | mbedtls_ssl_send_alert_message( | 
|  | 3280 | ssl, | 
|  | 3281 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 3282 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3283 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); | 
| Paul Bakker | 9659dae | 2013-08-28 16:21:34 +0200 | [diff] [blame] | 3284 | } | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3285 |  | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3286 | if( pk_alg != | 
|  | 3287 | mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) ) | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3288 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3289 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 3290 | ( "bad server key exchange message" ) ); | 
|  | 3291 | mbedtls_ssl_send_alert_message( | 
|  | 3292 | ssl, | 
|  | 3293 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 3294 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3295 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3296 | } | 
|  | 3297 | } | 
| Manuel Pégourié-Gonnard | 09edda8 | 2013-08-19 13:50:33 +0200 | [diff] [blame] | 3298 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3299 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 3300 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ | 
|  | 3301 | defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
|  | 3302 | if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ) | 
| Manuel Pégourié-Gonnard | 09edda8 | 2013-08-19 13:50:33 +0200 | [diff] [blame] | 3303 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3304 | pk_alg = mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3305 |  | 
| Paul Bakker | 9659dae | 2013-08-28 16:21:34 +0200 | [diff] [blame] | 3306 | /* Default hash for ECDSA is SHA-1 */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3307 | if( pk_alg == MBEDTLS_PK_ECDSA && md_alg == MBEDTLS_MD_NONE ) | 
|  | 3308 | md_alg = MBEDTLS_MD_SHA1; | 
| Paul Bakker | 9659dae | 2013-08-28 16:21:34 +0200 | [diff] [blame] | 3309 | } | 
|  | 3310 | else | 
|  | 3311 | #endif | 
|  | 3312 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3313 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 3314 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Paul Bakker | 9659dae | 2013-08-28 16:21:34 +0200 | [diff] [blame] | 3315 | } | 
| Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3316 |  | 
|  | 3317 | /* | 
|  | 3318 | * Read signature | 
|  | 3319 | */ | 
| Krzysztof Stachowiak | a1098f8 | 2018-03-13 11:28:49 +0100 | [diff] [blame] | 3320 |  | 
|  | 3321 | if( p > end - 2 ) | 
|  | 3322 | { | 
|  | 3323 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3324 | mbedtls_ssl_send_alert_message( | 
|  | 3325 | ssl, | 
|  | 3326 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 3327 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Krzysztof Stachowiak | a1098f8 | 2018-03-13 11:28:49 +0100 | [diff] [blame] | 3328 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); | 
|  | 3329 | } | 
| Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 3330 | sig_len = ( p[0] << 8 ) | p[1]; | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3331 | p += 2; | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3332 |  | 
| Krzysztof Stachowiak | 027f84c | 2018-03-13 11:29:24 +0100 | [diff] [blame] | 3333 | if( p != end - sig_len ) | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3334 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3335 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3336 | mbedtls_ssl_send_alert_message( | 
|  | 3337 | ssl, | 
|  | 3338 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 3339 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3340 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3341 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3342 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3343 | MBEDTLS_SSL_DEBUG_BUF( 3, "signature", p, sig_len ); | 
| Manuel Pégourié-Gonnard | ff56da3 | 2013-07-11 10:46:21 +0200 | [diff] [blame] | 3344 |  | 
| Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 3345 | /* | 
|  | 3346 | * Compute the hash that has been signed | 
|  | 3347 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3348 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ | 
|  | 3349 | defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
|  | 3350 | if( md_alg == MBEDTLS_MD_NONE ) | 
| Paul Bakker | c3f177a | 2012-04-11 16:11:49 +0000 | [diff] [blame] | 3351 | { | 
| Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3352 | hashlen = 36; | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 3353 | ret = mbedtls_ssl_get_key_exchange_md_ssl_tls( ssl, hash, params, | 
|  | 3354 | params_len ); | 
|  | 3355 | if( ret != 0 ) | 
| Andres Amaya Garcia | f0e521e | 2017-06-28 12:11:42 +0100 | [diff] [blame] | 3356 | return( ret ); | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 3357 | } | 
|  | 3358 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3359 | #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \ | 
|  | 3360 | MBEDTLS_SSL_PROTO_TLS1_1 */ | 
|  | 3361 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ | 
|  | 3362 | defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 3363 | if( md_alg != MBEDTLS_MD_NONE ) | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 3364 | { | 
| Gilles Peskine | ca1d742 | 2018-04-24 11:53:22 +0200 | [diff] [blame] | 3365 | ret = mbedtls_ssl_get_key_exchange_md_tls1_2( ssl, hash, &hashlen, | 
|  | 3366 | params, params_len, | 
|  | 3367 | md_alg ); | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 3368 | if( ret != 0 ) | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 3369 | return( ret ); | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 3370 | } | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 3371 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3372 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ | 
|  | 3373 | MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 3374 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3375 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 3376 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 3377 | } | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 3378 |  | 
| Gilles Peskine | ca1d742 | 2018-04-24 11:53:22 +0200 | [diff] [blame] | 3379 | MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen ); | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 3380 |  | 
| Hanno Becker | a6899bb | 2019-02-06 18:26:03 +0000 | [diff] [blame] | 3381 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 3382 | peer_pk = &ssl->handshake->peer_pubkey; | 
|  | 3383 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
| Manuel Pégourié-Gonnard | 7f2f062 | 2015-09-03 10:44:32 +0200 | [diff] [blame] | 3384 | if( ssl->session_negotiate->peer_cert == NULL ) | 
|  | 3385 | { | 
| Hanno Becker | 8273df8 | 2019-02-06 17:37:32 +0000 | [diff] [blame] | 3386 | /* Should never happen */ | 
| Hanno Becker | bd5580a | 2019-02-26 12:36:01 +0000 | [diff] [blame] | 3387 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
| Hanno Becker | 8273df8 | 2019-02-06 17:37:32 +0000 | [diff] [blame] | 3388 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | 7f2f062 | 2015-09-03 10:44:32 +0200 | [diff] [blame] | 3389 | } | 
| Hanno Becker | a6899bb | 2019-02-06 18:26:03 +0000 | [diff] [blame] | 3390 | peer_pk = &ssl->session_negotiate->peer_cert->pk; | 
|  | 3391 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
| Manuel Pégourié-Gonnard | 7f2f062 | 2015-09-03 10:44:32 +0200 | [diff] [blame] | 3392 |  | 
| Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 3393 | /* | 
|  | 3394 | * Verify signature | 
|  | 3395 | */ | 
| Hanno Becker | a6899bb | 2019-02-06 18:26:03 +0000 | [diff] [blame] | 3396 | if( !mbedtls_pk_can_do( peer_pk, pk_alg ) ) | 
| Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 3397 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3398 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3399 | mbedtls_ssl_send_alert_message( | 
|  | 3400 | ssl, | 
|  | 3401 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 3402 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3403 | return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH ); | 
| Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 3404 | } | 
|  | 3405 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3406 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
| Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 3407 | if( ssl->handshake->ecrs_enabled ) | 
| Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 3408 | rs_ctx = &ssl->handshake->ecrs_ctx.pk; | 
| Manuel Pégourié-Gonnard | 1f1f2a1 | 2017-05-18 11:27:06 +0200 | [diff] [blame] | 3409 | #endif | 
|  | 3410 |  | 
| Hanno Becker | a6899bb | 2019-02-06 18:26:03 +0000 | [diff] [blame] | 3411 | if( ( ret = mbedtls_pk_verify_restartable( peer_pk, | 
| Manuel Pégourié-Gonnard | 1f1f2a1 | 2017-05-18 11:27:06 +0200 | [diff] [blame] | 3412 | md_alg, hash, hashlen, p, sig_len, rs_ctx ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 3413 | { | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3414 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
| Manuel Pégourié-Gonnard | 1f1f2a1 | 2017-05-18 11:27:06 +0200 | [diff] [blame] | 3415 | if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS ) | 
|  | 3416 | #endif | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3417 | mbedtls_ssl_send_alert_message( | 
|  | 3418 | ssl, | 
|  | 3419 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 3420 | MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3421 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret ); | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3422 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
| Manuel Pégourié-Gonnard | 558da9c | 2018-06-13 12:02:12 +0200 | [diff] [blame] | 3423 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) | 
|  | 3424 | ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS; | 
|  | 3425 | #endif | 
| Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 3426 | return( ret ); | 
| Paul Bakker | c3f177a | 2012-04-11 16:11:49 +0000 | [diff] [blame] | 3427 | } | 
| Hanno Becker | ae553dd | 2019-02-08 14:06:00 +0000 | [diff] [blame] | 3428 |  | 
|  | 3429 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 3430 | /* We don't need the peer's public key anymore. Free it, | 
|  | 3431 | * so that more RAM is available for upcoming expensive | 
|  | 3432 | * operations like ECDHE. */ | 
|  | 3433 | mbedtls_pk_free( peer_pk ); | 
|  | 3434 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3435 | } | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3436 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3437 |  | 
| Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 3438 | exit: | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3439 | ssl->state++; | 
|  | 3440 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3441 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3442 |  | 
|  | 3443 | return( 0 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3444 | } | 
|  | 3445 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3446 | #if ! defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3447 | static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | da1ff38 | 2013-11-25 17:38:36 +0100 | [diff] [blame] | 3448 | { | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3449 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | 
| Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 3450 | ssl->handshake->ciphersuite_info; | 
| Manuel Pégourié-Gonnard | da1ff38 | 2013-11-25 17:38:36 +0100 | [diff] [blame] | 3451 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3452 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) ); | 
| Manuel Pégourié-Gonnard | da1ff38 | 2013-11-25 17:38:36 +0100 | [diff] [blame] | 3453 |  | 
| Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3454 | if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ) | 
| Manuel Pégourié-Gonnard | da1ff38 | 2013-11-25 17:38:36 +0100 | [diff] [blame] | 3455 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3456 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) ); | 
| Manuel Pégourié-Gonnard | da1ff38 | 2013-11-25 17:38:36 +0100 | [diff] [blame] | 3457 | ssl->state++; | 
|  | 3458 | return( 0 ); | 
|  | 3459 | } | 
|  | 3460 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3461 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 3462 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | da1ff38 | 2013-11-25 17:38:36 +0100 | [diff] [blame] | 3463 | } | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3464 | #else /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3465 | static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3466 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3467 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | d1b7f2b | 2016-02-24 14:13:22 +0000 | [diff] [blame] | 3468 | unsigned char *buf; | 
|  | 3469 | size_t n = 0; | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 3470 | size_t cert_type_len = 0, dn_len = 0; | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3471 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | 
| Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 3472 | ssl->handshake->ciphersuite_info; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3473 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3474 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3475 |  | 
| Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3476 | if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ) | 
| Manuel Pégourié-Gonnard | da1ff38 | 2013-11-25 17:38:36 +0100 | [diff] [blame] | 3477 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3478 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) ); | 
| Manuel Pégourié-Gonnard | da1ff38 | 2013-11-25 17:38:36 +0100 | [diff] [blame] | 3479 | ssl->state++; | 
|  | 3480 | return( 0 ); | 
|  | 3481 | } | 
|  | 3482 |  | 
| Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 3483 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3484 | { | 
| Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3485 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); | 
|  | 3486 | return( ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3487 | } | 
|  | 3488 |  | 
| Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3489 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) | 
|  | 3490 | { | 
|  | 3491 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3492 | mbedtls_ssl_send_alert_message( | 
|  | 3493 | ssl, | 
|  | 3494 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 3495 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); | 
| Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3496 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); | 
|  | 3497 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3498 |  | 
| Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3499 | ssl->state++; | 
|  | 3500 | ssl->client_auth = ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3501 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3502 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request", | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3503 | ssl->client_auth ? "a" : "no" ) ); | 
|  | 3504 |  | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3505 | if( ssl->client_auth == 0 ) | 
| Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3506 | { | 
| Johan Pascal | a89ca86 | 2020-08-25 10:03:19 +0200 | [diff] [blame] | 3507 | /* Current message is probably the ServerHelloDone */ | 
|  | 3508 | ssl->keep_current_message = 1; | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3509 | goto exit; | 
| Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3510 | } | 
| Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 3511 |  | 
| Manuel Pégourié-Gonnard | 04c1b4e | 2014-09-10 19:25:43 +0200 | [diff] [blame] | 3512 | /* | 
|  | 3513 | *  struct { | 
|  | 3514 | *      ClientCertificateType certificate_types<1..2^8-1>; | 
|  | 3515 | *      SignatureAndHashAlgorithm | 
|  | 3516 | *        supported_signature_algorithms<2^16-1>; -- TLS 1.2 only | 
|  | 3517 | *      DistinguishedName certificate_authorities<0..2^16-1>; | 
|  | 3518 | *  } CertificateRequest; | 
| Manuel Pégourié-Gonnard | d1b7f2b | 2016-02-24 14:13:22 +0000 | [diff] [blame] | 3519 | * | 
|  | 3520 | *  Since we only support a single certificate on clients, let's just | 
|  | 3521 | *  ignore all the information that's supposed to help us pick a | 
|  | 3522 | *  certificate. | 
|  | 3523 | * | 
|  | 3524 | *  We could check that our certificate matches the request, and bail out | 
|  | 3525 | *  if it doesn't, but it's simpler to just send the certificate anyway, | 
|  | 3526 | *  and give the server the opportunity to decide if it should terminate | 
|  | 3527 | *  the connection when it doesn't like our certificate. | 
|  | 3528 | * | 
|  | 3529 | *  Same goes for the hash in TLS 1.2's signature_algorithms: at this | 
|  | 3530 | *  point we only have one hash available (see comments in | 
| Simon Butcher | c0957bd | 2016-03-01 13:16:57 +0000 | [diff] [blame] | 3531 | *  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] | 3532 | * | 
|  | 3533 | *  However, we still minimally parse the message to check it is at least | 
|  | 3534 | *  superficially sane. | 
| Manuel Pégourié-Gonnard | 04c1b4e | 2014-09-10 19:25:43 +0200 | [diff] [blame] | 3535 | */ | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3536 | buf = ssl->in_msg; | 
| Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 3537 |  | 
| Manuel Pégourié-Gonnard | d1b7f2b | 2016-02-24 14:13:22 +0000 | [diff] [blame] | 3538 | /* certificate_types */ | 
| Krzysztof Stachowiak | 73b183c | 2018-04-05 10:20:09 +0200 | [diff] [blame] | 3539 | if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) ) | 
|  | 3540 | { | 
|  | 3541 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) ); | 
|  | 3542 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 3543 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
|  | 3544 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST ); | 
|  | 3545 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3546 | cert_type_len = buf[mbedtls_ssl_hs_hdr_len( ssl )]; | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3547 | n = cert_type_len; | 
|  | 3548 |  | 
| Krzysztof Stachowiak | bc145f7 | 2018-03-20 11:19:50 +0100 | [diff] [blame] | 3549 | /* | 
| Krzysztof Stachowiak | 94d4997 | 2018-04-05 14:48:55 +0200 | [diff] [blame] | 3550 | * In the subsequent code there are two paths that read from buf: | 
| Krzysztof Stachowiak | bc145f7 | 2018-03-20 11:19:50 +0100 | [diff] [blame] | 3551 | *     * the length of the signature algorithms field (if minor version of | 
|  | 3552 | *       SSL is 3), | 
|  | 3553 | *     * distinguished name length otherwise. | 
|  | 3554 | * Both reach at most the index: | 
|  | 3555 | *    ...hdr_len + 2 + n, | 
|  | 3556 | * therefore the buffer length at this point must be greater than that | 
|  | 3557 | * regardless of the actual code path. | 
|  | 3558 | */ | 
|  | 3559 | if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n ) | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3560 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3561 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 3562 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 3563 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3564 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST ); | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3565 | } | 
|  | 3566 |  | 
| Manuel Pégourié-Gonnard | d1b7f2b | 2016-02-24 14:13:22 +0000 | [diff] [blame] | 3567 | /* supported_signature_algorithms */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3568 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 3569 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3570 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3571 | size_t sig_alg_len = | 
|  | 3572 | ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] <<  8 ) | 
|  | 3573 | | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n]   ) ); | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3574 | #if defined(MBEDTLS_DEBUG_C) | 
| Krzysztof Stachowiak | bc231cc | 2018-03-20 14:09:53 +0100 | [diff] [blame] | 3575 | unsigned char* sig_alg; | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3576 | size_t i; | 
| Krzysztof Stachowiak | bc231cc | 2018-03-20 14:09:53 +0100 | [diff] [blame] | 3577 | #endif | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3578 |  | 
| Krzysztof Stachowiak | bc231cc | 2018-03-20 14:09:53 +0100 | [diff] [blame] | 3579 | /* | 
| Krzysztof Stachowiak | 94d4997 | 2018-04-05 14:48:55 +0200 | [diff] [blame] | 3580 | * The furthest access in buf is in the loop few lines below: | 
| Krzysztof Stachowiak | bc231cc | 2018-03-20 14:09:53 +0100 | [diff] [blame] | 3581 | *     sig_alg[i + 1], | 
|  | 3582 | * where: | 
|  | 3583 | *     sig_alg = buf + ...hdr_len + 3 + n, | 
|  | 3584 | *     max(i) = sig_alg_len - 1. | 
| Krzysztof Stachowiak | 94d4997 | 2018-04-05 14:48:55 +0200 | [diff] [blame] | 3585 | * Therefore the furthest access is: | 
| Krzysztof Stachowiak | bc231cc | 2018-03-20 14:09:53 +0100 | [diff] [blame] | 3586 | *     buf[...hdr_len + 3 + n + sig_alg_len - 1 + 1], | 
|  | 3587 | * which reduces to: | 
|  | 3588 | *     buf[...hdr_len + 3 + n + sig_alg_len], | 
|  | 3589 | * which is one less than we need the buf to be. | 
|  | 3590 | */ | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3591 | if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) | 
|  | 3592 | + 3 + n + sig_alg_len ) | 
| Krzysztof Stachowiak | bc231cc | 2018-03-20 14:09:53 +0100 | [diff] [blame] | 3593 | { | 
|  | 3594 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3595 | mbedtls_ssl_send_alert_message( | 
|  | 3596 | ssl, | 
|  | 3597 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 3598 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Krzysztof Stachowiak | bc231cc | 2018-03-20 14:09:53 +0100 | [diff] [blame] | 3599 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST ); | 
|  | 3600 | } | 
|  | 3601 |  | 
|  | 3602 | #if defined(MBEDTLS_DEBUG_C) | 
|  | 3603 | sig_alg = buf + mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n; | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3604 | for( i = 0; i < sig_alg_len; i += 2 ) | 
|  | 3605 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3606 | MBEDTLS_SSL_DEBUG_MSG( 3, | 
|  | 3607 | ( "Supported Signature Algorithm found: %d,%d", | 
|  | 3608 | sig_alg[i], sig_alg[i + 1]  ) ); | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3609 | } | 
|  | 3610 | #endif | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3611 |  | 
| Manuel Pégourié-Gonnard | d1b7f2b | 2016-02-24 14:13:22 +0000 | [diff] [blame] | 3612 | n += 2 + sig_alg_len; | 
| Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 3613 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3614 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3615 |  | 
| Manuel Pégourié-Gonnard | d1b7f2b | 2016-02-24 14:13:22 +0000 | [diff] [blame] | 3616 | /* certificate_authorities */ | 
|  | 3617 | dn_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] <<  8 ) | 
|  | 3618 | | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n]       ) ); | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3619 |  | 
|  | 3620 | n += dn_len; | 
| Manuel Pégourié-Gonnard | d1b7f2b | 2016-02-24 14:13:22 +0000 | [diff] [blame] | 3621 | if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n ) | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3622 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3623 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 3624 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 3625 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3626 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST ); | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3627 | } | 
|  | 3628 |  | 
|  | 3629 | exit: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3630 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3631 |  | 
|  | 3632 | return( 0 ); | 
|  | 3633 | } | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3634 | #endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3635 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3636 | static int ssl_parse_server_hello_done( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3637 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3638 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3639 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3640 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3641 |  | 
| Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 3642 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3643 | { | 
| Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3644 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); | 
|  | 3645 | return( ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3646 | } | 
| Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3647 |  | 
|  | 3648 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) | 
|  | 3649 | { | 
|  | 3650 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) ); | 
|  | 3651 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); | 
|  | 3652 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3653 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3654 | if( ssl->in_hslen  != mbedtls_ssl_hs_hdr_len( ssl ) || | 
|  | 3655 | ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE ) | 
| 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( 1, ( "bad server hello done message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 3658 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 3659 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3660 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO_DONE ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3661 | } | 
|  | 3662 |  | 
|  | 3663 | ssl->state++; | 
|  | 3664 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3665 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3666 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3667 | mbedtls_ssl_recv_flight_completed( ssl ); | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3668 | #endif | 
|  | 3669 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3670 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3671 |  | 
|  | 3672 | return( 0 ); | 
|  | 3673 | } | 
|  | 3674 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3675 | static int ssl_write_client_key_exchange( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3676 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3677 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3678 |  | 
|  | 3679 | size_t header_len; | 
|  | 3680 | size_t content_len; | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3681 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | 
| Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 3682 | ssl->handshake->ciphersuite_info; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3683 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3684 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3685 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3686 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) | 
|  | 3687 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3688 | { | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3689 | /* | 
|  | 3690 | * DHM key exchange -- send G^X mod P | 
|  | 3691 | */ | 
| Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3692 | content_len = ssl->handshake->dhm_ctx.len; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3693 |  | 
| Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3694 | ssl->out_msg[4] = (unsigned char)( content_len >> 8 ); | 
|  | 3695 | ssl->out_msg[5] = (unsigned char)( content_len      ); | 
|  | 3696 | header_len = 6; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3697 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3698 | ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx, | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3699 | (int) mbedtls_mpi_size( &ssl->handshake->dhm_ctx.P ), | 
|  | 3700 | &ssl->out_msg[header_len], content_len, | 
|  | 3701 | ssl->conf->f_rng, ssl->conf->p_rng ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3702 | if( ret != 0 ) | 
|  | 3703 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3704 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3705 | return( ret ); | 
|  | 3706 | } | 
|  | 3707 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3708 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X  ); | 
|  | 3709 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3710 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3711 | if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx, | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3712 | ssl->handshake->premaster, | 
|  | 3713 | MBEDTLS_PREMASTER_SIZE, | 
|  | 3714 | &ssl->handshake->pmslen, | 
|  | 3715 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3716 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3717 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3718 | return( ret ); | 
|  | 3719 | } | 
|  | 3720 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3721 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K  ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3722 | } | 
|  | 3723 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3724 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */ | 
| Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 3725 | #if defined(MBEDTLS_USE_PSA_CRYPTO) &&                           \ | 
|  | 3726 | ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) ||     \ | 
|  | 3727 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) ) | 
|  | 3728 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA || | 
|  | 3729 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ) | 
|  | 3730 | { | 
|  | 3731 | psa_status_t status; | 
| Janos Follath | 53b8ec2 | 2019-08-08 10:28:27 +0100 | [diff] [blame] | 3732 | psa_key_attributes_t key_attributes; | 
| Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 3733 |  | 
|  | 3734 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; | 
|  | 3735 |  | 
|  | 3736 | unsigned char own_pubkey[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH]; | 
|  | 3737 | size_t own_pubkey_len; | 
|  | 3738 | unsigned char *own_pubkey_ecpoint; | 
|  | 3739 | size_t own_pubkey_ecpoint_len; | 
|  | 3740 |  | 
| Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3741 | header_len = 4; | 
| Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 3742 |  | 
| Hanno Becker | 0a94a64 | 2019-01-11 14:35:30 +0000 | [diff] [blame] | 3743 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) ); | 
|  | 3744 |  | 
| Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 3745 | /* | 
|  | 3746 | * Generate EC private key for ECDHE exchange. | 
|  | 3747 | */ | 
|  | 3748 |  | 
| Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 3749 | /* The master secret is obtained from the shared ECDH secret by | 
|  | 3750 | * applying the TLS 1.2 PRF with a specific salt and label. While | 
|  | 3751 | * the PSA Crypto API encourages combining key agreement schemes | 
|  | 3752 | * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not | 
|  | 3753 | * yet support the provisioning of salt + label to the KDF. | 
|  | 3754 | * For the time being, we therefore need to split the computation | 
|  | 3755 | * 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] | 3756 | key_attributes = psa_key_attributes_init(); | 
|  | 3757 | psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE ); | 
| Janos Follath | df3b089 | 2019-08-08 11:12:24 +0100 | [diff] [blame] | 3758 | psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH ); | 
| Gilles Peskine | 4245980 | 2019-12-19 13:31:53 +0100 | [diff] [blame] | 3759 | psa_set_key_type( &key_attributes, handshake->ecdh_psa_type ); | 
|  | 3760 | psa_set_key_bits( &key_attributes, handshake->ecdh_bits ); | 
| Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 3761 |  | 
|  | 3762 | /* Generate ECDH private key. */ | 
| Janos Follath | 53b8ec2 | 2019-08-08 10:28:27 +0100 | [diff] [blame] | 3763 | status = psa_generate_key( &key_attributes, | 
| Janos Follath | df3b089 | 2019-08-08 11:12:24 +0100 | [diff] [blame] | 3764 | &handshake->ecdh_psa_privkey ); | 
| Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 3765 | if( status != PSA_SUCCESS ) | 
|  | 3766 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
|  | 3767 |  | 
|  | 3768 | /* Export the public part of the ECDH private key from PSA | 
|  | 3769 | * and convert it to ECPoint format used in ClientKeyExchange. */ | 
|  | 3770 | status = psa_export_public_key( handshake->ecdh_psa_privkey, | 
|  | 3771 | own_pubkey, sizeof( own_pubkey ), | 
|  | 3772 | &own_pubkey_len ); | 
|  | 3773 | if( status != PSA_SUCCESS ) | 
|  | 3774 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
|  | 3775 |  | 
|  | 3776 | if( mbedtls_psa_tls_psa_ec_to_ecpoint( own_pubkey, | 
|  | 3777 | own_pubkey_len, | 
|  | 3778 | &own_pubkey_ecpoint, | 
|  | 3779 | &own_pubkey_ecpoint_len ) != 0 ) | 
|  | 3780 | { | 
|  | 3781 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
|  | 3782 | } | 
|  | 3783 |  | 
|  | 3784 | /* Copy ECPoint structure to outgoing message buffer. */ | 
| Andrzej Kurek | ade9e28 | 2019-05-07 08:19:33 -0400 | [diff] [blame] | 3785 | ssl->out_msg[header_len] = (unsigned char) own_pubkey_ecpoint_len; | 
| Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3786 | memcpy( ssl->out_msg + header_len + 1, | 
|  | 3787 | own_pubkey_ecpoint, own_pubkey_ecpoint_len ); | 
|  | 3788 | content_len = own_pubkey_ecpoint_len + 1; | 
| Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 3789 |  | 
| Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 3790 | /* The ECDH secret is the premaster secret used for key derivation. */ | 
|  | 3791 |  | 
| Janos Follath | df3b089 | 2019-08-08 11:12:24 +0100 | [diff] [blame] | 3792 | /* Compute ECDH shared secret. */ | 
|  | 3793 | status = psa_raw_key_agreement( PSA_ALG_ECDH, | 
|  | 3794 | handshake->ecdh_psa_privkey, | 
|  | 3795 | handshake->ecdh_psa_peerkey, | 
|  | 3796 | handshake->ecdh_psa_peerkey_len, | 
|  | 3797 | ssl->handshake->premaster, | 
|  | 3798 | sizeof( ssl->handshake->premaster ), | 
|  | 3799 | &ssl->handshake->pmslen ); | 
| Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 3800 | if( status != PSA_SUCCESS ) | 
|  | 3801 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
|  | 3802 |  | 
|  | 3803 | status = psa_destroy_key( handshake->ecdh_psa_privkey ); | 
|  | 3804 | if( status != PSA_SUCCESS ) | 
|  | 3805 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3806 | handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT; | 
| Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 3807 | } | 
|  | 3808 | else | 
|  | 3809 | #endif /* MBEDTLS_USE_PSA_CRYPTO && | 
|  | 3810 | ( MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || | 
|  | 3811 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ) */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3812 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) ||                     \ | 
|  | 3813 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) ||                   \ | 
|  | 3814 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||                      \ | 
|  | 3815 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) | 
|  | 3816 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA || | 
|  | 3817 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA || | 
|  | 3818 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA || | 
|  | 3819 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA ) | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3820 | { | 
|  | 3821 | /* | 
|  | 3822 | * ECDH key exchange -- send client public value | 
|  | 3823 | */ | 
| Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3824 | header_len = 4; | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3825 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3826 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
| Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 3827 | if( ssl->handshake->ecrs_enabled ) | 
|  | 3828 | { | 
| Manuel Pégourié-Gonnard | c37423f | 2018-10-16 10:28:17 +0200 | [diff] [blame] | 3829 | 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] | 3830 | goto ecdh_calc_secret; | 
| Manuel Pégourié-Gonnard | 23e4162 | 2017-05-18 12:35:37 +0200 | [diff] [blame] | 3831 |  | 
| Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 3832 | mbedtls_ecdh_enable_restart( &ssl->handshake->ecdh_ctx ); | 
|  | 3833 | } | 
| Manuel Pégourié-Gonnard | 2350b4e | 2017-05-16 09:26:48 +0200 | [diff] [blame] | 3834 | #endif | 
|  | 3835 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3836 | ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx, | 
| Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3837 | &content_len, | 
|  | 3838 | &ssl->out_msg[header_len], 1000, | 
| Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 3839 | ssl->conf->f_rng, ssl->conf->p_rng ); | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3840 | if( ret != 0 ) | 
|  | 3841 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3842 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret ); | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3843 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
| Manuel Pégourié-Gonnard | 558da9c | 2018-06-13 12:02:12 +0200 | [diff] [blame] | 3844 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) | 
|  | 3845 | ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS; | 
|  | 3846 | #endif | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3847 | return( ret ); | 
|  | 3848 | } | 
|  | 3849 |  | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 3850 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, | 
|  | 3851 | MBEDTLS_DEBUG_ECDH_Q ); | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3852 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3853 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
| Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 3854 | if( ssl->handshake->ecrs_enabled ) | 
|  | 3855 | { | 
| Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3856 | ssl->handshake->ecrs_n = content_len; | 
| Manuel Pégourié-Gonnard | c37423f | 2018-10-16 10:28:17 +0200 | [diff] [blame] | 3857 | ssl->handshake->ecrs_state = ssl_ecrs_cke_ecdh_calc_secret; | 
| Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 3858 | } | 
| Manuel Pégourié-Gonnard | 2350b4e | 2017-05-16 09:26:48 +0200 | [diff] [blame] | 3859 |  | 
|  | 3860 | ecdh_calc_secret: | 
| Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 3861 | if( ssl->handshake->ecrs_enabled ) | 
| Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3862 | content_len = ssl->handshake->ecrs_n; | 
| Manuel Pégourié-Gonnard | 2350b4e | 2017-05-16 09:26:48 +0200 | [diff] [blame] | 3863 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3864 | if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3865 | &ssl->handshake->pmslen, | 
|  | 3866 | ssl->handshake->premaster, | 
|  | 3867 | MBEDTLS_MPI_MAX_SIZE, | 
|  | 3868 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3869 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3870 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret ); | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3871 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
| Manuel Pégourié-Gonnard | 558da9c | 2018-06-13 12:02:12 +0200 | [diff] [blame] | 3872 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) | 
|  | 3873 | ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS; | 
|  | 3874 | #endif | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3875 | return( ret ); | 
|  | 3876 | } | 
|  | 3877 |  | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 3878 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, | 
|  | 3879 | MBEDTLS_DEBUG_ECDH_Z ); | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3880 | } | 
|  | 3881 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3882 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || | 
|  | 3883 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED || | 
|  | 3884 | MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED || | 
|  | 3885 | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */ | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3886 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) | 
| Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3887 | if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) ) | 
| Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 3888 | { | 
| Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 3889 | /* | 
| Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 3890 | * opaque psk_identity<0..2^16-1>; | 
|  | 3891 | */ | 
| Hanno Becker | 520224e | 2018-10-26 11:38:07 +0100 | [diff] [blame] | 3892 | if( ssl_conf_has_static_psk( ssl->conf ) == 0 ) | 
| Manuel Pégourié-Gonnard | b4b19f3 | 2015-07-07 11:41:21 +0200 | [diff] [blame] | 3893 | { | 
| Hanno Becker | 2e4f616 | 2018-10-23 11:54:44 +0100 | [diff] [blame] | 3894 | /* We don't offer PSK suites if we don't have a PSK, | 
|  | 3895 | * and we check that the server's choice is among the | 
|  | 3896 | * ciphersuites we offered, so this should never happen. */ | 
|  | 3897 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | b4b19f3 | 2015-07-07 11:41:21 +0200 | [diff] [blame] | 3898 | } | 
| Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 3899 |  | 
| Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3900 | header_len = 4; | 
|  | 3901 | content_len = ssl->conf->psk_identity_len; | 
| Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 3902 |  | 
| Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3903 | 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] | 3904 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3905 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 3906 | ( "psk identity too long or SSL buffer too short" ) ); | 
| Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 3907 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); | 
|  | 3908 | } | 
|  | 3909 |  | 
| Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3910 | ssl->out_msg[header_len++] = (unsigned char)( content_len >> 8 ); | 
|  | 3911 | ssl->out_msg[header_len++] = (unsigned char)( content_len      ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3912 |  | 
| Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3913 | memcpy( ssl->out_msg + header_len, | 
|  | 3914 | ssl->conf->psk_identity, | 
|  | 3915 | ssl->conf->psk_identity_len ); | 
|  | 3916 | header_len += ssl->conf->psk_identity_len; | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3917 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3918 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) | 
|  | 3919 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ) | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3920 | { | 
| Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3921 | content_len = 0; | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 3922 | } | 
| Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3923 | else | 
|  | 3924 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3925 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) | 
|  | 3926 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 3927 | { | 
| Hanno Becker | dfab8e2 | 2018-10-23 11:59:34 +0100 | [diff] [blame] | 3928 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 3929 | /* Opaque PSKs are currently only supported for PSK-only suites. */ | 
| Hanno Becker | 520224e | 2018-10-26 11:38:07 +0100 | [diff] [blame] | 3930 | if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 ) | 
| Hanno Becker | dfab8e2 | 2018-10-23 11:59:34 +0100 | [diff] [blame] | 3931 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
|  | 3932 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 3933 |  | 
| Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3934 | if( ( ret = ssl_write_encrypted_pms( ssl, header_len, | 
|  | 3935 | &content_len, 2 ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 3936 | return( ret ); | 
|  | 3937 | } | 
|  | 3938 | else | 
|  | 3939 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3940 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) | 
|  | 3941 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3942 | { | 
| Hanno Becker | dfab8e2 | 2018-10-23 11:59:34 +0100 | [diff] [blame] | 3943 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 3944 | /* Opaque PSKs are currently only supported for PSK-only suites. */ | 
| Hanno Becker | 520224e | 2018-10-26 11:38:07 +0100 | [diff] [blame] | 3945 | if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 ) | 
| Hanno Becker | dfab8e2 | 2018-10-23 11:59:34 +0100 | [diff] [blame] | 3946 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
|  | 3947 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 3948 |  | 
| Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3949 | /* | 
|  | 3950 | * ClientDiffieHellmanPublic public (DHM send G^X mod P) | 
|  | 3951 | */ | 
| Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3952 | content_len = ssl->handshake->dhm_ctx.len; | 
| Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 3953 |  | 
| Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3954 | if( header_len + 2 + content_len > | 
|  | 3955 | MBEDTLS_SSL_OUT_CONTENT_LEN ) | 
| Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 3956 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3957 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 3958 | ( "psk identity or DHM size too long or SSL buffer too short" ) ); | 
| Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 3959 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); | 
|  | 3960 | } | 
|  | 3961 |  | 
| Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3962 | ssl->out_msg[header_len++] = (unsigned char)( content_len >> 8 ); | 
|  | 3963 | ssl->out_msg[header_len++] = (unsigned char)( content_len      ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3964 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3965 | ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx, | 
|  | 3966 | (int) mbedtls_mpi_size( &ssl->handshake->dhm_ctx.P ), | 
| Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3967 | &ssl->out_msg[header_len], content_len, | 
| Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 3968 | ssl->conf->f_rng, ssl->conf->p_rng ); | 
| Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3969 | if( ret != 0 ) | 
|  | 3970 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3971 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret ); | 
| Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3972 | return( ret ); | 
|  | 3973 | } | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3974 | } | 
| Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3975 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3976 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ | 
|  | 3977 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) | 
|  | 3978 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) | 
| Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 3979 | { | 
| Hanno Becker | dfab8e2 | 2018-10-23 11:59:34 +0100 | [diff] [blame] | 3980 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 3981 | /* Opaque PSKs are currently only supported for PSK-only suites. */ | 
| Hanno Becker | 520224e | 2018-10-26 11:38:07 +0100 | [diff] [blame] | 3982 | if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 ) | 
| Hanno Becker | dfab8e2 | 2018-10-23 11:59:34 +0100 | [diff] [blame] | 3983 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
|  | 3984 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 3985 |  | 
| Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3986 | /* | 
|  | 3987 | * ClientECDiffieHellmanPublic public; | 
|  | 3988 | */ | 
| Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3989 | ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx, | 
|  | 3990 | &content_len, | 
|  | 3991 | &ssl->out_msg[header_len], | 
|  | 3992 | MBEDTLS_SSL_OUT_CONTENT_LEN - header_len, | 
| Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 3993 | ssl->conf->f_rng, ssl->conf->p_rng ); | 
| Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3994 | if( ret != 0 ) | 
|  | 3995 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3996 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret ); | 
| Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3997 | return( ret ); | 
|  | 3998 | } | 
| Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 3999 |  | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 4000 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, | 
|  | 4001 | MBEDTLS_DEBUG_ECDH_Q ); | 
| Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 4002 | } | 
|  | 4003 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4004 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ | 
| Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 4005 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4006 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 4007 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4008 | } | 
|  | 4009 |  | 
| Hanno Becker | afd311e | 2018-10-23 15:26:40 +0100 | [diff] [blame] | 4010 | #if defined(MBEDTLS_USE_PSA_CRYPTO) &&          \ | 
|  | 4011 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) | 
|  | 4012 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK && | 
|  | 4013 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 && | 
| Hanno Becker | 520224e | 2018-10-26 11:38:07 +0100 | [diff] [blame] | 4014 | ssl_conf_has_static_raw_psk( ssl->conf ) == 0 ) | 
| Hanno Becker | afd311e | 2018-10-23 15:26:40 +0100 | [diff] [blame] | 4015 | { | 
| Ronald Cron | 5ee5707 | 2020-06-11 09:34:06 +0200 | [diff] [blame] | 4016 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 4017 | ( "skip PMS generation for opaque PSK" ) ); | 
| Hanno Becker | afd311e | 2018-10-23 15:26:40 +0100 | [diff] [blame] | 4018 | } | 
|  | 4019 | else | 
|  | 4020 | #endif /* MBEDTLS_USE_PSA_CRYPTO && | 
|  | 4021 | MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4022 | if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl, | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 4023 | ciphersuite_info->key_exchange ) ) != 0 ) | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4024 | { | 
| Ronald Cron | 5ee5707 | 2020-06-11 09:34:06 +0200 | [diff] [blame] | 4025 | MBEDTLS_SSL_DEBUG_RET( 1, | 
|  | 4026 | "mbedtls_ssl_psk_derive_premaster", ret ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4027 | return( ret ); | 
|  | 4028 | } | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4029 | } | 
|  | 4030 | else | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4031 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4032 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) | 
|  | 4033 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4034 | { | 
| Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 4035 | header_len = 4; | 
|  | 4036 | if( ( ret = ssl_write_encrypted_pms( ssl, header_len, | 
|  | 4037 | &content_len, 0 ) ) != 0 ) | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 4038 | return( ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4039 | } | 
| Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 4040 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4041 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */ | 
| Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 4042 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
|  | 4043 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ) | 
|  | 4044 | { | 
| Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 4045 | header_len = 4; | 
| Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 4046 |  | 
|  | 4047 | ret = mbedtls_ecjpake_write_round_two( &ssl->handshake->ecjpake_ctx, | 
| Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 4048 | ssl->out_msg + header_len, | 
|  | 4049 | MBEDTLS_SSL_OUT_CONTENT_LEN - header_len, | 
|  | 4050 | &content_len, | 
| Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 4051 | ssl->conf->f_rng, ssl->conf->p_rng ); | 
|  | 4052 | if( ret != 0 ) | 
|  | 4053 | { | 
|  | 4054 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret ); | 
|  | 4055 | return( ret ); | 
|  | 4056 | } | 
|  | 4057 |  | 
|  | 4058 | ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx, | 
|  | 4059 | ssl->handshake->premaster, 32, &ssl->handshake->pmslen, | 
|  | 4060 | ssl->conf->f_rng, ssl->conf->p_rng ); | 
|  | 4061 | if( ret != 0 ) | 
|  | 4062 | { | 
|  | 4063 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret ); | 
|  | 4064 | return( ret ); | 
|  | 4065 | } | 
|  | 4066 | } | 
|  | 4067 | else | 
|  | 4068 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */ | 
| Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 4069 | { | 
|  | 4070 | ((void) ciphersuite_info); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4071 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 4072 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 4073 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4074 |  | 
| Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 4075 | ssl->out_msglen  = header_len + content_len; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4076 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; | 
|  | 4077 | ssl->out_msg[0]  = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4078 |  | 
|  | 4079 | ssl->state++; | 
|  | 4080 |  | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4081 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4082 | { | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4083 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4084 | return( ret ); | 
|  | 4085 | } | 
|  | 4086 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4087 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4088 |  | 
|  | 4089 | return( 0 ); | 
|  | 4090 | } | 
|  | 4091 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4092 | #if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4093 | static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4094 | { | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 4095 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | 
| Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 4096 | ssl->handshake->ciphersuite_info; | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4097 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4098 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4099 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4100 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4101 | if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 4102 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4103 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret ); | 
| Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 4104 | return( ret ); | 
|  | 4105 | } | 
|  | 4106 |  | 
| Hanno Becker | 77adddc | 2019-02-07 12:32:43 +0000 | [diff] [blame] | 4107 | if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ) | 
| Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 4108 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4109 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) ); | 
| Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 4110 | ssl->state++; | 
|  | 4111 | return( 0 ); | 
|  | 4112 | } | 
|  | 4113 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4114 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 4115 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4116 | } | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4117 | #else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4118 | static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4119 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4120 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 4121 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | 
| Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 4122 | ssl->handshake->ciphersuite_info; | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4123 | size_t n = 0, offset = 0; | 
|  | 4124 | unsigned char hash[48]; | 
| Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 4125 | unsigned char *hash_start = hash; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4126 | mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE; | 
| Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 4127 | size_t hashlen; | 
| Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 4128 | void *rs_ctx = NULL; | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4129 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4130 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4131 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4132 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
| Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 4133 | if( ssl->handshake->ecrs_enabled && | 
| Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 4134 | ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign ) | 
| Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 4135 | { | 
| Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 4136 | goto sign; | 
| Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 4137 | } | 
| Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 4138 | #endif | 
|  | 4139 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4140 | if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 4141 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4142 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret ); | 
| Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 4143 | return( ret ); | 
|  | 4144 | } | 
|  | 4145 |  | 
| Hanno Becker | 77adddc | 2019-02-07 12:32:43 +0000 | [diff] [blame] | 4146 | if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ) | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4147 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4148 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4149 | ssl->state++; | 
|  | 4150 | return( 0 ); | 
|  | 4151 | } | 
|  | 4152 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4153 | if( ssl->client_auth == 0 || mbedtls_ssl_own_cert( ssl ) == NULL ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4154 | { | 
| Johan Pascal | a89ca86 | 2020-08-25 10:03:19 +0200 | [diff] [blame] | 4155 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) ); | 
|  | 4156 | ssl->state++; | 
|  | 4157 | return( 0 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4158 | } | 
|  | 4159 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4160 | if( mbedtls_ssl_own_key( ssl ) == NULL ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4161 | { | 
| Manuel Pégourié-Gonnard | b4b19f3 | 2015-07-07 11:41:21 +0200 | [diff] [blame] | 4162 | 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] | 4163 | return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4164 | } | 
|  | 4165 |  | 
|  | 4166 | /* | 
| Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 4167 | * Make a signature of the handshake digests | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4168 | */ | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4169 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
| Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 4170 | if( ssl->handshake->ecrs_enabled ) | 
|  | 4171 | ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign; | 
|  | 4172 |  | 
|  | 4173 | sign: | 
|  | 4174 | #endif | 
|  | 4175 |  | 
| Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 4176 | ssl->handshake->calc_verify( ssl, hash, &hashlen ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4177 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4178 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ | 
|  | 4179 | defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
|  | 4180 | if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 ) | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4181 | { | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 4182 | /* | 
|  | 4183 | * digitally-signed struct { | 
|  | 4184 | *     opaque md5_hash[16]; | 
|  | 4185 | *     opaque sha_hash[20]; | 
|  | 4186 | * }; | 
|  | 4187 | * | 
|  | 4188 | * md5_hash | 
|  | 4189 | *     MD5(handshake_messages); | 
|  | 4190 | * | 
|  | 4191 | * sha_hash | 
|  | 4192 | *     SHA(handshake_messages); | 
|  | 4193 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4194 | md_alg = MBEDTLS_MD_NONE; | 
| Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 4195 |  | 
|  | 4196 | /* | 
|  | 4197 | * For ECDSA, default hash is SHA-1 only | 
|  | 4198 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4199 | 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] | 4200 | { | 
|  | 4201 | hash_start += 16; | 
|  | 4202 | hashlen -= 16; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4203 | md_alg = MBEDTLS_MD_SHA1; | 
| Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 4204 | } | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 4205 | } | 
|  | 4206 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4207 | #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \ | 
|  | 4208 | MBEDTLS_SSL_PROTO_TLS1_1 */ | 
|  | 4209 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 4210 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 4211 | { | 
|  | 4212 | /* | 
|  | 4213 | * digitally-signed struct { | 
|  | 4214 | *     opaque handshake_messages[handshake_messages_length]; | 
|  | 4215 | * }; | 
|  | 4216 | * | 
|  | 4217 | * Taking shortcut here. We assume that the server always allows the | 
|  | 4218 | * PRF Hash function and has sent it in the allowed signature | 
|  | 4219 | * algorithms list received in the Certificate Request message. | 
|  | 4220 | * | 
|  | 4221 | * Until we encounter a server that does not, we will take this | 
|  | 4222 | * shortcut. | 
|  | 4223 | * | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 4224 | * Reason: Otherwise we should have running hashes for SHA512 and | 
|  | 4225 | *         SHA224 in order to satisfy 'weird' needs from the server | 
|  | 4226 | *         side. | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 4227 | */ | 
| Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 4228 | if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 4229 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4230 | md_alg = MBEDTLS_MD_SHA384; | 
|  | 4231 | ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384; | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 4232 | } | 
|  | 4233 | else | 
|  | 4234 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4235 | md_alg = MBEDTLS_MD_SHA256; | 
|  | 4236 | ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256; | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 4237 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4238 | 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] | 4239 |  | 
| Manuel Pégourié-Gonnard | bfe32ef | 2013-08-22 14:55:30 +0200 | [diff] [blame] | 4240 | /* Info from md_alg will be used instead */ | 
|  | 4241 | hashlen = 0; | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4242 | offset = 2; | 
|  | 4243 | } | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 4244 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4245 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 4246 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4247 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 4248 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 4249 | } | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4250 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4251 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
| Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 4252 | if( ssl->handshake->ecrs_enabled ) | 
| Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 4253 | rs_ctx = &ssl->handshake->ecrs_ctx.pk; | 
| Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 4254 | #endif | 
|  | 4255 |  | 
|  | 4256 | if( ( ret = mbedtls_pk_sign_restartable( mbedtls_ssl_own_key( ssl ), | 
|  | 4257 | md_alg, hash_start, hashlen, | 
| Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4258 | ssl->out_msg + 6 + offset, &n, | 
| Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 4259 | 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] | 4260 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4261 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret ); | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4262 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
| Manuel Pégourié-Gonnard | 558da9c | 2018-06-13 12:02:12 +0200 | [diff] [blame] | 4263 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) | 
|  | 4264 | ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS; | 
|  | 4265 | #endif | 
| Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4266 | return( ret ); | 
| Manuel Pégourié-Gonnard | 76c18a1 | 2013-08-20 16:50:40 +0200 | [diff] [blame] | 4267 | } | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 4268 |  | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4269 | ssl->out_msg[4 + offset] = (unsigned char)( n >> 8 ); | 
|  | 4270 | ssl->out_msg[5 + offset] = (unsigned char)( n      ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4271 |  | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4272 | ssl->out_msglen  = 6 + n + offset; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4273 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; | 
|  | 4274 | ssl->out_msg[0]  = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4275 |  | 
|  | 4276 | ssl->state++; | 
|  | 4277 |  | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4278 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4279 | { | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4280 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4281 | return( ret ); | 
|  | 4282 | } | 
|  | 4283 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4284 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4285 |  | 
| Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 4286 | return( ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4287 | } | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4288 | #endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4289 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4290 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | 
|  | 4291 | 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] | 4292 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4293 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4294 | uint32_t lifetime; | 
|  | 4295 | size_t ticket_len; | 
|  | 4296 | unsigned char *ticket; | 
| Manuel Pégourié-Gonnard | 000d5ae | 2014-09-10 21:52:12 +0200 | [diff] [blame] | 4297 | const unsigned char *msg; | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4298 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4299 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) ); | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4300 |  | 
| Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 4301 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4302 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4303 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4304 | return( ret ); | 
|  | 4305 | } | 
|  | 4306 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4307 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4308 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4309 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 4310 | mbedtls_ssl_send_alert_message( | 
|  | 4311 | ssl, | 
|  | 4312 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 4313 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4314 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4315 | } | 
|  | 4316 |  | 
|  | 4317 | /* | 
|  | 4318 | * struct { | 
|  | 4319 | *     uint32 ticket_lifetime_hint; | 
|  | 4320 | *     opaque ticket<0..2^16-1>; | 
|  | 4321 | * } NewSessionTicket; | 
|  | 4322 | * | 
| Manuel Pégourié-Gonnard | 000d5ae | 2014-09-10 21:52:12 +0200 | [diff] [blame] | 4323 | * 0  .  3   ticket_lifetime_hint | 
|  | 4324 | * 4  .  5   ticket_len (n) | 
|  | 4325 | * 6  .  5+n ticket content | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4326 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4327 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET || | 
|  | 4328 | ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len( ssl ) ) | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4329 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4330 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 4331 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 4332 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4333 | return( MBEDTLS_ERR_SSL_BAD_HS_NEW_SESSION_TICKET ); | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4334 | } | 
|  | 4335 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4336 | msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ); | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4337 |  | 
| Philippe Antoine | b5b2543 | 2018-05-11 11:06:29 +0200 | [diff] [blame] | 4338 | lifetime = ( ((uint32_t) msg[0]) << 24 ) | ( msg[1] << 16 ) | | 
|  | 4339 | ( msg[2] << 8 ) | ( msg[3] ); | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4340 |  | 
| Manuel Pégourié-Gonnard | 000d5ae | 2014-09-10 21:52:12 +0200 | [diff] [blame] | 4341 | ticket_len = ( msg[4] << 8 ) | ( msg[5] ); | 
|  | 4342 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4343 | 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] | 4344 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4345 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 4346 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 4347 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4348 | return( MBEDTLS_ERR_SSL_BAD_HS_NEW_SESSION_TICKET ); | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4349 | } | 
|  | 4350 |  | 
| Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 4351 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %" MBEDTLS_PRINTF_SIZET, ticket_len ) ); | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4352 |  | 
| Manuel Pégourié-Gonnard | 7cd5924 | 2013-08-02 13:24:41 +0200 | [diff] [blame] | 4353 | /* We're not waiting for a NewSessionTicket message any more */ | 
|  | 4354 | ssl->handshake->new_session_ticket = 0; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4355 | ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC; | 
| Manuel Pégourié-Gonnard | 7cd5924 | 2013-08-02 13:24:41 +0200 | [diff] [blame] | 4356 |  | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4357 | /* | 
|  | 4358 | * Zero-length ticket means the server changed his mind and doesn't want | 
|  | 4359 | * to send a ticket after all, so just forget it | 
|  | 4360 | */ | 
| Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 4361 | if( ticket_len == 0 ) | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4362 | return( 0 ); | 
|  | 4363 |  | 
| Hanno Becker | b2964cb | 2019-01-30 14:46:35 +0000 | [diff] [blame] | 4364 | if( ssl->session != NULL && ssl->session->ticket != NULL ) | 
|  | 4365 | { | 
|  | 4366 | mbedtls_platform_zeroize( ssl->session->ticket, | 
|  | 4367 | ssl->session->ticket_len ); | 
|  | 4368 | mbedtls_free( ssl->session->ticket ); | 
|  | 4369 | ssl->session->ticket = NULL; | 
|  | 4370 | ssl->session->ticket_len = 0; | 
|  | 4371 | } | 
|  | 4372 |  | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4373 | mbedtls_platform_zeroize( ssl->session_negotiate->ticket, | 
|  | 4374 | ssl->session_negotiate->ticket_len ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4375 | mbedtls_free( ssl->session_negotiate->ticket ); | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4376 | ssl->session_negotiate->ticket = NULL; | 
|  | 4377 | ssl->session_negotiate->ticket_len = 0; | 
|  | 4378 |  | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 4379 | if( ( ticket = mbedtls_calloc( 1, ticket_len ) ) == NULL ) | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4380 | { | 
| Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 4381 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "ticket alloc failed" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 4382 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 4383 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 4384 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4385 | } | 
|  | 4386 |  | 
| Manuel Pégourié-Gonnard | 000d5ae | 2014-09-10 21:52:12 +0200 | [diff] [blame] | 4387 | memcpy( ticket, msg + 6, ticket_len ); | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4388 |  | 
|  | 4389 | ssl->session_negotiate->ticket = ticket; | 
|  | 4390 | ssl->session_negotiate->ticket_len = ticket_len; | 
|  | 4391 | ssl->session_negotiate->ticket_lifetime = lifetime; | 
|  | 4392 |  | 
|  | 4393 | /* | 
|  | 4394 | * RFC 5077 section 3.4: | 
|  | 4395 | * "If the client receives a session ticket from the server, then it | 
|  | 4396 | * discards any Session ID that was sent in the ServerHello." | 
|  | 4397 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4398 | 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] | 4399 | ssl->session_negotiate->id_len = 0; | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4400 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4401 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) ); | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4402 |  | 
|  | 4403 | return( 0 ); | 
|  | 4404 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4405 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4406 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4407 | /* | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4408 | * SSL handshake -- client side -- single step | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4409 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4410 | int mbedtls_ssl_handshake_client_step( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4411 | { | 
|  | 4412 | int ret = 0; | 
|  | 4413 |  | 
| Manuel Pégourié-Gonnard | dba460f | 2015-06-24 22:59:30 +0200 | [diff] [blame] | 4414 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4415 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4416 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4417 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) ); | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4418 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4419 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4420 | return( ret ); | 
|  | 4421 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4422 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4423 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4424 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4425 | { | 
| Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4426 | if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4427 | return( ret ); | 
|  | 4428 | } | 
| Hanno Becker | bc2498a | 2018-08-28 10:13:29 +0100 | [diff] [blame] | 4429 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4430 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4431 | /* 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] | 4432 | * by DTLS for dropping out-of-sequence ChangeCipherSpec records */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4433 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | 
|  | 4434 | if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC && | 
| Manuel Pégourié-Gonnard | cd32a50 | 2014-09-20 13:54:12 +0200 | [diff] [blame] | 4435 | ssl->handshake->new_session_ticket != 0 ) | 
|  | 4436 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4437 | ssl->state = MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET; | 
| Manuel Pégourié-Gonnard | cd32a50 | 2014-09-20 13:54:12 +0200 | [diff] [blame] | 4438 | } | 
|  | 4439 | #endif | 
|  | 4440 |  | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4441 | switch( ssl->state ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4442 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4443 | case MBEDTLS_SSL_HELLO_REQUEST: | 
|  | 4444 | ssl->state = MBEDTLS_SSL_CLIENT_HELLO; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4445 | break; | 
|  | 4446 |  | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4447 | /* | 
|  | 4448 | *  ==>   ClientHello | 
|  | 4449 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4450 | case MBEDTLS_SSL_CLIENT_HELLO: | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4451 | ret = ssl_write_client_hello( ssl ); | 
|  | 4452 | break; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4453 |  | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4454 | /* | 
|  | 4455 | *  <==   ServerHello | 
|  | 4456 | *        Certificate | 
|  | 4457 | *      ( ServerKeyExchange  ) | 
|  | 4458 | *      ( CertificateRequest ) | 
|  | 4459 | *        ServerHelloDone | 
|  | 4460 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4461 | case MBEDTLS_SSL_SERVER_HELLO: | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4462 | ret = ssl_parse_server_hello( ssl ); | 
|  | 4463 | break; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4464 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4465 | case MBEDTLS_SSL_SERVER_CERTIFICATE: | 
|  | 4466 | ret = mbedtls_ssl_parse_certificate( ssl ); | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4467 | break; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4468 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4469 | case MBEDTLS_SSL_SERVER_KEY_EXCHANGE: | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4470 | ret = ssl_parse_server_key_exchange( ssl ); | 
|  | 4471 | break; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4472 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4473 | case MBEDTLS_SSL_CERTIFICATE_REQUEST: | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4474 | ret = ssl_parse_certificate_request( ssl ); | 
|  | 4475 | break; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4476 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4477 | case MBEDTLS_SSL_SERVER_HELLO_DONE: | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4478 | ret = ssl_parse_server_hello_done( ssl ); | 
|  | 4479 | break; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4480 |  | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4481 | /* | 
|  | 4482 | *  ==> ( Certificate/Alert  ) | 
|  | 4483 | *        ClientKeyExchange | 
|  | 4484 | *      ( CertificateVerify  ) | 
|  | 4485 | *        ChangeCipherSpec | 
|  | 4486 | *        Finished | 
|  | 4487 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4488 | case MBEDTLS_SSL_CLIENT_CERTIFICATE: | 
|  | 4489 | ret = mbedtls_ssl_write_certificate( ssl ); | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4490 | break; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4491 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4492 | case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE: | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4493 | ret = ssl_write_client_key_exchange( ssl ); | 
|  | 4494 | break; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4495 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4496 | case MBEDTLS_SSL_CERTIFICATE_VERIFY: | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4497 | ret = ssl_write_certificate_verify( ssl ); | 
|  | 4498 | break; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4499 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4500 | case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC: | 
|  | 4501 | ret = mbedtls_ssl_write_change_cipher_spec( ssl ); | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4502 | break; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4503 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4504 | case MBEDTLS_SSL_CLIENT_FINISHED: | 
|  | 4505 | ret = mbedtls_ssl_write_finished( ssl ); | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4506 | break; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4507 |  | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4508 | /* | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4509 | *  <==   ( NewSessionTicket ) | 
|  | 4510 | *        ChangeCipherSpec | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4511 | *        Finished | 
|  | 4512 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4513 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | 
|  | 4514 | case MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET: | 
| Manuel Pégourié-Gonnard | cd32a50 | 2014-09-20 13:54:12 +0200 | [diff] [blame] | 4515 | ret = ssl_parse_new_session_ticket( ssl ); | 
|  | 4516 | break; | 
| Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 4517 | #endif | 
| Manuel Pégourié-Gonnard | cd32a50 | 2014-09-20 13:54:12 +0200 | [diff] [blame] | 4518 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4519 | case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC: | 
|  | 4520 | ret = mbedtls_ssl_parse_change_cipher_spec( ssl ); | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4521 | break; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4522 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4523 | case MBEDTLS_SSL_SERVER_FINISHED: | 
|  | 4524 | ret = mbedtls_ssl_parse_finished( ssl ); | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4525 | break; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4526 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4527 | case MBEDTLS_SSL_FLUSH_BUFFERS: | 
|  | 4528 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) ); | 
|  | 4529 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4530 | break; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4531 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4532 | case MBEDTLS_SSL_HANDSHAKE_WRAPUP: | 
|  | 4533 | mbedtls_ssl_handshake_wrapup( ssl ); | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4534 | break; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4535 |  | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4536 | default: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4537 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) ); | 
|  | 4538 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4539 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4540 |  | 
|  | 4541 | return( ret ); | 
|  | 4542 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4543 | #endif /* MBEDTLS_SSL_CLI_C */ |