| 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, | 
|  | 688 | ( "sending session ticket of length %d", 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 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 908 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, current time: %lu", t ) ); | 
| Manuel Pégourié-Gonnard | b760f00 | 2014-07-22 15:53:27 +0200 | [diff] [blame] | 909 | #else | 
| Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 910 | 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] | 911 | return( ret ); | 
|  | 912 |  | 
|  | 913 | p += 4; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 914 | #endif /* MBEDTLS_HAVE_TIME */ | 
| Manuel Pégourié-Gonnard | b760f00 | 2014-07-22 15:53:27 +0200 | [diff] [blame] | 915 |  | 
| Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 916 | 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] | 917 | return( ret ); | 
|  | 918 |  | 
|  | 919 | return( 0 ); | 
|  | 920 | } | 
|  | 921 |  | 
| Mohammad Azim Khan | 1d3b508 | 2018-04-18 19:35:00 +0100 | [diff] [blame] | 922 | /** | 
|  | 923 | * \brief           Validate cipher suite against config in SSL context. | 
|  | 924 | * | 
|  | 925 | * \param suite_info    cipher suite to validate | 
|  | 926 | * \param ssl           SSL context | 
| Andrzej Kurek | 03bac44 | 2018-04-25 05:06:07 -0400 | [diff] [blame] | 927 | * \param min_minor_ver Minimal minor version to accept a cipher suite | 
|  | 928 | * \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] | 929 | * | 
|  | 930 | * \return          0 if valid, else 1 | 
|  | 931 | */ | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 932 | static int ssl_validate_ciphersuite( | 
|  | 933 | const mbedtls_ssl_ciphersuite_t * suite_info, | 
|  | 934 | const mbedtls_ssl_context * ssl, | 
|  | 935 | int min_minor_ver, int max_minor_ver ) | 
| Mohammad Azim Khan | 1d3b508 | 2018-04-18 19:35:00 +0100 | [diff] [blame] | 936 | { | 
| Andrzej Kurek | 03bac44 | 2018-04-25 05:06:07 -0400 | [diff] [blame] | 937 | (void) ssl; | 
| Mohammad Azim Khan | 1d3b508 | 2018-04-18 19:35:00 +0100 | [diff] [blame] | 938 | if( suite_info == NULL ) | 
|  | 939 | return( 1 ); | 
|  | 940 |  | 
| Andrzej Kurek | 03bac44 | 2018-04-25 05:06:07 -0400 | [diff] [blame] | 941 | if( suite_info->min_minor_ver > max_minor_ver || | 
|  | 942 | suite_info->max_minor_ver < min_minor_ver ) | 
| Mohammad Azim Khan | 1d3b508 | 2018-04-18 19:35:00 +0100 | [diff] [blame] | 943 | return( 1 ); | 
|  | 944 |  | 
|  | 945 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 946 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | 
|  | 947 | ( suite_info->flags & MBEDTLS_CIPHERSUITE_NODTLS ) ) | 
|  | 948 | return( 1 ); | 
|  | 949 | #endif | 
|  | 950 |  | 
|  | 951 | #if defined(MBEDTLS_ARC4_C) | 
|  | 952 | if( ssl->conf->arc4_disabled == MBEDTLS_SSL_ARC4_DISABLED && | 
|  | 953 | suite_info->cipher == MBEDTLS_CIPHER_ARC4_128 ) | 
|  | 954 | return( 1 ); | 
|  | 955 | #endif | 
|  | 956 |  | 
|  | 957 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
|  | 958 | if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE && | 
|  | 959 | mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 ) | 
|  | 960 | return( 1 ); | 
|  | 961 | #endif | 
|  | 962 |  | 
| Hanno Becker | 2e4f616 | 2018-10-23 11:54:44 +0100 | [diff] [blame] | 963 | /* Don't suggest PSK-based ciphersuite if no PSK is available. */ | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 964 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) | 
| Hanno Becker | 2e4f616 | 2018-10-23 11:54:44 +0100 | [diff] [blame] | 965 | if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) && | 
| Hanno Becker | 520224e | 2018-10-26 11:38:07 +0100 | [diff] [blame] | 966 | ssl_conf_has_static_psk( ssl->conf ) == 0 ) | 
| Hanno Becker | 2e4f616 | 2018-10-23 11:54:44 +0100 | [diff] [blame] | 967 | { | 
|  | 968 | return( 1 ); | 
|  | 969 | } | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 970 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ | 
| Hanno Becker | 2e4f616 | 2018-10-23 11:54:44 +0100 | [diff] [blame] | 971 |  | 
| Mohammad Azim Khan | 1d3b508 | 2018-04-18 19:35:00 +0100 | [diff] [blame] | 972 | return( 0 ); | 
|  | 973 | } | 
|  | 974 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 975 | static int ssl_write_client_hello( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 976 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 977 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 978 | size_t i, n, olen, ext_len = 0; | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 979 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 980 | unsigned char *buf; | 
| Paul Bakker | 2fbefde | 2013-06-29 16:01:15 +0200 | [diff] [blame] | 981 | unsigned char *p, *q; | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 982 | const unsigned char *end; | 
|  | 983 |  | 
| Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 984 | unsigned char offer_compress; | 
| Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 985 | const int *ciphersuites; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 986 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; | 
| Ron Eldor | 755bb6a | 2018-02-14 19:30:48 +0200 | [diff] [blame] | 987 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ | 
|  | 988 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
|  | 989 | int uses_ec = 0; | 
|  | 990 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 991 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 992 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 993 |  | 
| Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 994 | if( ssl->conf->f_rng == NULL ) | 
| Paul Bakker | a9a028e | 2013-11-21 17:31:06 +0100 | [diff] [blame] | 995 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 996 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided") ); | 
|  | 997 | return( MBEDTLS_ERR_SSL_NO_RNG ); | 
| Paul Bakker | a9a028e | 2013-11-21 17:31:06 +0100 | [diff] [blame] | 998 | } | 
|  | 999 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1000 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 1001 | if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE ) | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1002 | #endif | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1003 | { | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1004 | ssl->major_ver = ssl->conf->min_major_ver; | 
|  | 1005 | ssl->minor_ver = ssl->conf->min_minor_ver; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1006 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1007 |  | 
| Manuel Pégourié-Gonnard | 1897af9 | 2015-05-10 23:27:38 +0200 | [diff] [blame] | 1008 | if( ssl->conf->max_major_ver == 0 ) | 
| Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 1009 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1010 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 1011 | ( "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] | 1012 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 1013 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1014 |  | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1015 | buf = ssl->out_msg; | 
|  | 1016 | end = buf + MBEDTLS_SSL_OUT_CONTENT_LEN; | 
|  | 1017 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1018 | /* | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1019 | * Check if there's enough space for the first part of the ClientHello | 
|  | 1020 | * consisting of the 38 bytes described below, the session identifier (at | 
|  | 1021 | * most 32 bytes) and its length (1 byte). | 
|  | 1022 | * | 
|  | 1023 | * Use static upper bounds instead of the actual values | 
|  | 1024 | * to allow the compiler to optimize this away. | 
|  | 1025 | */ | 
|  | 1026 | MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 38 + 1 + 32 ); | 
|  | 1027 |  | 
|  | 1028 | /* | 
|  | 1029 | * The 38 first bytes of the ClientHello: | 
|  | 1030 | *     0  .   0   handshake type (written later) | 
|  | 1031 | *     1  .   3   handshake length (written later) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1032 | *     4  .   5   highest version supported | 
|  | 1033 | *     6  .   9   current UNIX time | 
|  | 1034 | *    10  .  37   random bytes | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1035 | * | 
|  | 1036 | * The current UNIX time (4 bytes) and following 28 random bytes are written | 
|  | 1037 | * by ssl_generate_random() into ssl->handshake->randbytes buffer and then | 
|  | 1038 | * copied from there into the output buffer. | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1039 | */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1040 |  | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1041 | p = buf + 4; | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1042 | mbedtls_ssl_write_version( ssl->conf->max_major_ver, | 
|  | 1043 | ssl->conf->max_minor_ver, | 
|  | 1044 | ssl->conf->transport, p ); | 
| Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 1045 | p += 2; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1046 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1047 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, max version: [%d:%d]", | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1048 | buf[4], buf[5] ) ); | 
|  | 1049 |  | 
| Manuel Pégourié-Gonnard | b760f00 | 2014-07-22 15:53:27 +0200 | [diff] [blame] | 1050 | if( ( ret = ssl_generate_random( ssl ) ) != 0 ) | 
|  | 1051 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1052 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_generate_random", ret ); | 
| Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 1053 | return( ret ); | 
| Manuel Pégourié-Gonnard | b760f00 | 2014-07-22 15:53:27 +0200 | [diff] [blame] | 1054 | } | 
| Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 1055 |  | 
| Manuel Pégourié-Gonnard | b760f00 | 2014-07-22 15:53:27 +0200 | [diff] [blame] | 1056 | memcpy( p, ssl->handshake->randbytes, 32 ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1057 | 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] | 1058 | p += 32; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1059 |  | 
|  | 1060 | /* | 
|  | 1061 | *    38  .  38   session id length | 
|  | 1062 | *    39  . 39+n  session id | 
| Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1063 | *   39+n . 39+n  DTLS only: cookie length (1 byte) | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1064 | *   40+n .  ..   DTLS only: cookie | 
| Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1065 | *   ..   . ..    ciphersuitelist length (2 bytes) | 
|  | 1066 | *   ..   . ..    ciphersuitelist | 
|  | 1067 | *   ..   . ..    compression methods length (1 byte) | 
| Paul Bakker | c3f177a | 2012-04-11 16:11:49 +0000 | [diff] [blame] | 1068 | *   ..   . ..    compression methods | 
| Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1069 | *   ..   . ..    extensions length (2 bytes) | 
| Paul Bakker | c3f177a | 2012-04-11 16:11:49 +0000 | [diff] [blame] | 1070 | *   ..   . ..    extensions | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1071 | */ | 
| Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 1072 | n = ssl->session_negotiate->id_len; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1073 |  | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1074 | if( n < 16 || n > 32 || | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1075 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 1076 | ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE || | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1077 | #endif | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 1078 | ssl->handshake->resume == 0 ) | 
| Manuel Pégourié-Gonnard | 6377e41 | 2013-07-31 16:31:33 +0200 | [diff] [blame] | 1079 | { | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1080 | n = 0; | 
| Manuel Pégourié-Gonnard | 6377e41 | 2013-07-31 16:31:33 +0200 | [diff] [blame] | 1081 | } | 
|  | 1082 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1083 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | 
| Manuel Pégourié-Gonnard | 6377e41 | 2013-07-31 16:31:33 +0200 | [diff] [blame] | 1084 | /* | 
|  | 1085 | * RFC 5077 section 3.4: "When presenting a ticket, the client MAY | 
|  | 1086 | * generate and include a Session ID in the TLS ClientHello." | 
|  | 1087 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1088 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 1089 | if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE ) | 
| Manuel Pégourié-Gonnard | 59c6f2e | 2015-01-22 11:06:40 +0000 | [diff] [blame] | 1090 | #endif | 
| Manuel Pégourié-Gonnard | d2b35ec | 2015-03-10 11:40:43 +0000 | [diff] [blame] | 1091 | { | 
| Manuel Pégourié-Gonnard | 59c6f2e | 2015-01-22 11:06:40 +0000 | [diff] [blame] | 1092 | if( ssl->session_negotiate->ticket != NULL && | 
|  | 1093 | ssl->session_negotiate->ticket_len != 0 ) | 
|  | 1094 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1095 | ret = ssl->conf->f_rng( ssl->conf->p_rng, | 
|  | 1096 | ssl->session_negotiate->id, 32 ); | 
| Manuel Pégourié-Gonnard | 6377e41 | 2013-07-31 16:31:33 +0200 | [diff] [blame] | 1097 |  | 
| Manuel Pégourié-Gonnard | 59c6f2e | 2015-01-22 11:06:40 +0000 | [diff] [blame] | 1098 | if( ret != 0 ) | 
|  | 1099 | return( ret ); | 
| Manuel Pégourié-Gonnard | 6377e41 | 2013-07-31 16:31:33 +0200 | [diff] [blame] | 1100 |  | 
| Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 1101 | ssl->session_negotiate->id_len = n = 32; | 
| Manuel Pégourié-Gonnard | 59c6f2e | 2015-01-22 11:06:40 +0000 | [diff] [blame] | 1102 | } | 
| Manuel Pégourié-Gonnard | 6377e41 | 2013-07-31 16:31:33 +0200 | [diff] [blame] | 1103 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1104 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1105 |  | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1106 | /* | 
|  | 1107 | * The first check of the output buffer size above ( | 
|  | 1108 | * MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 38 + 1 + 32 );) | 
|  | 1109 | * has checked that there is enough space in the output buffer for the | 
|  | 1110 | * session identifier length byte and the session identifier (n <= 32). | 
|  | 1111 | */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1112 | *p++ = (unsigned char) n; | 
|  | 1113 |  | 
|  | 1114 | for( i = 0; i < n; i++ ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1115 | *p++ = ssl->session_negotiate->id[i]; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1116 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1117 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, session id len.: %d", n ) ); | 
|  | 1118 | MBEDTLS_SSL_DEBUG_BUF( 3,   "client hello, session id", buf + 39, n ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1119 |  | 
| Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1120 | /* | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1121 | *   With 'n' being the length of the session identifier | 
|  | 1122 | * | 
|  | 1123 | *   39+n . 39+n  DTLS only: cookie length (1 byte) | 
|  | 1124 | *   40+n .  ..   DTLS only: cookie | 
|  | 1125 | *   ..   . ..    ciphersuitelist length (2 bytes) | 
|  | 1126 | *   ..   . ..    ciphersuitelist | 
|  | 1127 | *   ..   . ..    compression methods length (1 byte) | 
|  | 1128 | *   ..   . ..    compression methods | 
|  | 1129 | *   ..   . ..    extensions length (2 bytes) | 
|  | 1130 | *   ..   . ..    extensions | 
|  | 1131 | */ | 
|  | 1132 |  | 
|  | 1133 | /* | 
| Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1134 | * DTLS cookie | 
|  | 1135 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1136 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1137 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1138 | { | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1139 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 ); | 
|  | 1140 |  | 
| Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1141 | if( ssl->handshake->verify_cookie == NULL ) | 
|  | 1142 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1143 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no verify cookie to send" ) ); | 
| Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1144 | *p++ = 0; | 
|  | 1145 | } | 
|  | 1146 | else | 
|  | 1147 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1148 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie", | 
| Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1149 | ssl->handshake->verify_cookie, | 
|  | 1150 | ssl->handshake->verify_cookie_len ); | 
|  | 1151 |  | 
|  | 1152 | *p++ = ssl->handshake->verify_cookie_len; | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1153 |  | 
|  | 1154 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, | 
|  | 1155 | ssl->handshake->verify_cookie_len ); | 
| Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1156 | memcpy( p, ssl->handshake->verify_cookie, | 
|  | 1157 | ssl->handshake->verify_cookie_len ); | 
|  | 1158 | p += ssl->handshake->verify_cookie_len; | 
|  | 1159 | } | 
| Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1160 | } | 
|  | 1161 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1162 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1163 | /* | 
| Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1164 | * Ciphersuite list | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1165 | */ | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1166 | ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver]; | 
| Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1167 |  | 
|  | 1168 | /* Skip writing ciphersuite length for now */ | 
|  | 1169 | n = 0; | 
|  | 1170 | q = p; | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1171 |  | 
|  | 1172 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); | 
| Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 1173 | p += 2; | 
|  | 1174 |  | 
| Paul Bakker | 2fbefde | 2013-06-29 16:01:15 +0200 | [diff] [blame] | 1175 | for( i = 0; ciphersuites[i] != 0; i++ ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1176 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1177 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuites[i] ); | 
| Paul Bakker | 2fbefde | 2013-06-29 16:01:15 +0200 | [diff] [blame] | 1178 |  | 
| Andrzej Kurek | 03bac44 | 2018-04-25 05:06:07 -0400 | [diff] [blame] | 1179 | if( ssl_validate_ciphersuite( ciphersuite_info, ssl, | 
|  | 1180 | ssl->conf->min_minor_ver, | 
|  | 1181 | ssl->conf->max_minor_ver ) != 0 ) | 
| Paul Bakker | 2fbefde | 2013-06-29 16:01:15 +0200 | [diff] [blame] | 1182 | continue; | 
|  | 1183 |  | 
| Hanno Becker | 3c88c65 | 2019-01-02 11:17:25 +0000 | [diff] [blame] | 1184 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %#04x (%s)", | 
| Hanno Becker | ecea07d | 2018-11-07 16:24:35 +0000 | [diff] [blame] | 1185 | ciphersuites[i], ciphersuite_info->name ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1186 |  | 
| Ron Eldor | 755bb6a | 2018-02-14 19:30:48 +0200 | [diff] [blame] | 1187 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ | 
|  | 1188 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
|  | 1189 | uses_ec |= mbedtls_ssl_ciphersuite_uses_ec( ciphersuite_info ); | 
|  | 1190 | #endif | 
|  | 1191 |  | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1192 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); | 
|  | 1193 |  | 
| Paul Bakker | 2fbefde | 2013-06-29 16:01:15 +0200 | [diff] [blame] | 1194 | n++; | 
| Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 1195 | *p++ = (unsigned char)( ciphersuites[i] >> 8 ); | 
|  | 1196 | *p++ = (unsigned char)( ciphersuites[i]      ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1197 | } | 
|  | 1198 |  | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1199 | MBEDTLS_SSL_DEBUG_MSG( 3, | 
|  | 1200 | ( "client hello, got %d ciphersuites (excluding SCSVs)", n ) ); | 
| Ron Eldor | 714785d | 2017-08-28 13:55:55 +0300 | [diff] [blame] | 1201 |  | 
| Manuel Pégourié-Gonnard | 5d9cde2 | 2015-01-22 10:49:41 +0000 | [diff] [blame] | 1202 | /* | 
|  | 1203 | * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV | 
|  | 1204 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1205 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 1206 | if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE ) | 
| Manuel Pégourié-Gonnard | 5d9cde2 | 2015-01-22 10:49:41 +0000 | [diff] [blame] | 1207 | #endif | 
|  | 1208 | { | 
| Ron Eldor | 4a2fb4c | 2017-09-10 17:03:50 +0300 | [diff] [blame] | 1209 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding EMPTY_RENEGOTIATION_INFO_SCSV" ) ); | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1210 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1211 | *p++ = (unsigned char)( MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO >> 8 ); | 
|  | 1212 | *p++ = (unsigned char)( MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO      ); | 
| Manuel Pégourié-Gonnard | 5d9cde2 | 2015-01-22 10:49:41 +0000 | [diff] [blame] | 1213 | n++; | 
|  | 1214 | } | 
|  | 1215 |  | 
| Manuel Pégourié-Gonnard | 1cbd39d | 2014-10-20 13:34:59 +0200 | [diff] [blame] | 1216 | /* 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] | 1217 | #if defined(MBEDTLS_SSL_FALLBACK_SCSV) | 
| Manuel Pégourié-Gonnard | 684b059 | 2015-05-06 09:27:31 +0100 | [diff] [blame] | 1218 | if( ssl->conf->fallback == MBEDTLS_SSL_IS_FALLBACK ) | 
| Manuel Pégourié-Gonnard | 1cbd39d | 2014-10-20 13:34:59 +0200 | [diff] [blame] | 1219 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1220 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding FALLBACK_SCSV" ) ); | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1221 |  | 
|  | 1222 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1223 | *p++ = (unsigned char)( MBEDTLS_SSL_FALLBACK_SCSV_VALUE >> 8 ); | 
|  | 1224 | *p++ = (unsigned char)( MBEDTLS_SSL_FALLBACK_SCSV_VALUE      ); | 
| Manuel Pégourié-Gonnard | 1cbd39d | 2014-10-20 13:34:59 +0200 | [diff] [blame] | 1225 | n++; | 
|  | 1226 | } | 
|  | 1227 | #endif | 
|  | 1228 |  | 
| Paul Bakker | 2fbefde | 2013-06-29 16:01:15 +0200 | [diff] [blame] | 1229 | *q++ = (unsigned char)( n >> 7 ); | 
|  | 1230 | *q++ = (unsigned char)( n << 1 ); | 
|  | 1231 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1232 | #if defined(MBEDTLS_ZLIB_SUPPORT) | 
| Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1233 | offer_compress = 1; | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1234 | #else | 
| Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1235 | offer_compress = 0; | 
|  | 1236 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1237 |  | 
| Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1238 | /* | 
| Johannes H | 4e5d23f | 2018-01-06 09:46:57 +0100 | [diff] [blame] | 1239 | * 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] | 1240 | * in the same datagram, uncompressing one could overwrite the next one. | 
|  | 1241 | * We don't want to add complexity for handling that case unless there is | 
|  | 1242 | * an actual need for it. | 
|  | 1243 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1244 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1245 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1246 | offer_compress = 0; | 
|  | 1247 | #endif | 
|  | 1248 |  | 
|  | 1249 | if( offer_compress ) | 
|  | 1250 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1251 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 2 ) ); | 
|  | 1252 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d %d", | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1253 | MBEDTLS_SSL_COMPRESS_DEFLATE, | 
|  | 1254 | MBEDTLS_SSL_COMPRESS_NULL ) ); | 
| Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1255 |  | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1256 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 3 ); | 
| Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1257 | *p++ = 2; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1258 | *p++ = MBEDTLS_SSL_COMPRESS_DEFLATE; | 
|  | 1259 | *p++ = MBEDTLS_SSL_COMPRESS_NULL; | 
| Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1260 | } | 
|  | 1261 | else | 
|  | 1262 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1263 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 1 ) ); | 
|  | 1264 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d", | 
|  | 1265 | MBEDTLS_SSL_COMPRESS_NULL ) ); | 
| Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1266 |  | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1267 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); | 
| Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1268 | *p++ = 1; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1269 | *p++ = MBEDTLS_SSL_COMPRESS_NULL; | 
| Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1270 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1271 |  | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1272 | /* First write extensions, then the total length */ | 
|  | 1273 |  | 
|  | 1274 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); | 
|  | 1275 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1276 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1277 | if( ( ret = ssl_write_hostname_ext( ssl, p + 2 + ext_len, | 
|  | 1278 | end, &olen ) ) != 0 ) | 
|  | 1279 | { | 
|  | 1280 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_hostname_ext", ret ); | 
|  | 1281 | return( ret ); | 
|  | 1282 | } | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 1283 | ext_len += olen; | 
| Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 1284 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1285 |  | 
| Hanno Becker | 40f8b51 | 2017-10-12 14:58:55 +0100 | [diff] [blame] | 1286 | /* Note that TLS_EMPTY_RENEGOTIATION_INFO_SCSV is always added | 
|  | 1287 | * even if MBEDTLS_SSL_RENEGOTIATION is not defined. */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1288 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1289 | if( ( ret = ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, | 
|  | 1290 | end, &olen ) ) != 0 ) | 
|  | 1291 | { | 
|  | 1292 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_renegotiation_ext", ret ); | 
|  | 1293 | return( ret ); | 
|  | 1294 | } | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 1295 | ext_len += olen; | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1296 | #endif | 
| Paul Bakker | c3f177a | 2012-04-11 16:11:49 +0000 | [diff] [blame] | 1297 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1298 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1299 | defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1300 | if( ( ret = ssl_write_signature_algorithms_ext( ssl, p + 2 + ext_len, | 
|  | 1301 | end, &olen ) ) != 0 ) | 
|  | 1302 | { | 
|  | 1303 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_signature_algorithms_ext", ret ); | 
|  | 1304 | return( ret ); | 
|  | 1305 | } | 
| Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 1306 | ext_len += olen; | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 1307 | #endif | 
| Paul Bakker | c3f177a | 2012-04-11 16:11:49 +0000 | [diff] [blame] | 1308 |  | 
| Manuel Pégourié-Gonnard | f472179 | 2015-09-15 10:53:51 +0200 | [diff] [blame] | 1309 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ | 
| Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 1310 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Ron Eldor | 755bb6a | 2018-02-14 19:30:48 +0200 | [diff] [blame] | 1311 | if( uses_ec ) | 
|  | 1312 | { | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1313 | if( ( ret = ssl_write_supported_elliptic_curves_ext( ssl, p + 2 + ext_len, | 
|  | 1314 | end, &olen ) ) != 0 ) | 
|  | 1315 | { | 
|  | 1316 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_supported_elliptic_curves_ext", ret ); | 
|  | 1317 | return( ret ); | 
|  | 1318 | } | 
| Ron Eldor | 755bb6a | 2018-02-14 19:30:48 +0200 | [diff] [blame] | 1319 | ext_len += olen; | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1320 |  | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1321 | if( ( ret = ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, | 
|  | 1322 | end, &olen ) ) != 0 ) | 
|  | 1323 | { | 
|  | 1324 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_supported_point_formats_ext", ret ); | 
|  | 1325 | return( ret ); | 
|  | 1326 | } | 
| Ron Eldor | 755bb6a | 2018-02-14 19:30:48 +0200 | [diff] [blame] | 1327 | ext_len += olen; | 
|  | 1328 | } | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1329 | #endif | 
|  | 1330 |  | 
| Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 1331 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1332 | if( ( ret = ssl_write_ecjpake_kkpp_ext( ssl, p + 2 + ext_len, | 
|  | 1333 | end, &olen ) ) != 0 ) | 
|  | 1334 | { | 
|  | 1335 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_ecjpake_kkpp_ext", ret ); | 
|  | 1336 | return( ret ); | 
|  | 1337 | } | 
| Manuel Pégourié-Gonnard | 294139b | 2015-09-15 16:55:05 +0200 | [diff] [blame] | 1338 | ext_len += olen; | 
|  | 1339 | #endif | 
|  | 1340 |  | 
| Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1341 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1342 | if( ( ret = ssl_write_cid_ext( ssl, p + 2 + ext_len, end, &olen ) ) != 0 ) | 
|  | 1343 | { | 
|  | 1344 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_cid_ext", ret ); | 
|  | 1345 | return( ret ); | 
|  | 1346 | } | 
| Hanno Becker | 49770ff | 2019-04-25 16:55:15 +0100 | [diff] [blame] | 1347 | ext_len += olen; | 
| Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1348 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
| Hanno Becker | 49770ff | 2019-04-25 16:55:15 +0100 | [diff] [blame] | 1349 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1350 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1351 | if( ( ret = ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, | 
|  | 1352 | end, &olen ) ) != 0 ) | 
|  | 1353 | { | 
|  | 1354 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_max_fragment_length_ext", ret ); | 
|  | 1355 | return( ret ); | 
|  | 1356 | } | 
| Manuel Pégourié-Gonnard | a052849 | 2013-07-16 17:26:28 +0200 | [diff] [blame] | 1357 | ext_len += olen; | 
| Paul Bakker | 05decb2 | 2013-08-15 13:33:48 +0200 | [diff] [blame] | 1358 | #endif | 
| Manuel Pégourié-Gonnard | a052849 | 2013-07-16 17:26:28 +0200 | [diff] [blame] | 1359 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1360 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1361 | if( ( ret = ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, | 
|  | 1362 | end, &olen ) ) != 0 ) | 
|  | 1363 | { | 
|  | 1364 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_truncated_hmac_ext", ret ); | 
|  | 1365 | return( ret ); | 
|  | 1366 | } | 
| Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 1367 | ext_len += olen; | 
| Paul Bakker | 1f2bc62 | 2013-08-15 13:45:55 +0200 | [diff] [blame] | 1368 | #endif | 
| Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 1369 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1370 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1371 | if( ( ret = ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, | 
|  | 1372 | end, &olen ) ) != 0 ) | 
|  | 1373 | { | 
|  | 1374 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_encrypt_then_mac_ext", ret ); | 
|  | 1375 | return( ret ); | 
|  | 1376 | } | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 1377 | ext_len += olen; | 
|  | 1378 | #endif | 
|  | 1379 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1380 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1381 | if( ( ret = ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, | 
|  | 1382 | end, &olen ) ) != 0 ) | 
|  | 1383 | { | 
|  | 1384 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_extended_ms_ext", ret ); | 
|  | 1385 | return( ret ); | 
|  | 1386 | } | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 1387 | ext_len += olen; | 
|  | 1388 | #endif | 
|  | 1389 |  | 
| Simon Butcher | 5624ec8 | 2015-09-29 01:06:06 +0100 | [diff] [blame] | 1390 | #if defined(MBEDTLS_SSL_ALPN) | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1391 | if( ( ret = ssl_write_alpn_ext( ssl, p + 2 + ext_len, | 
|  | 1392 | end, &olen ) ) != 0 ) | 
|  | 1393 | { | 
|  | 1394 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_alpn_ext", ret ); | 
|  | 1395 | return( ret ); | 
|  | 1396 | } | 
| Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 1397 | ext_len += olen; | 
| Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 1398 | #endif | 
| Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 1399 |  | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1400 | #if defined(MBEDTLS_SSL_DTLS_SRTP) | 
| Johan Pascal | c3ccd98 | 2020-10-28 17:18:18 +0100 | [diff] [blame] | 1401 | if( ( ret = ssl_write_use_srtp_ext( ssl, p + 2 + ext_len, | 
|  | 1402 | end, &olen ) ) != 0 ) | 
| Ron Eldor | 3adb992 | 2017-12-21 10:15:08 +0200 | [diff] [blame] | 1403 | { | 
| Johan Pascal | c3ccd98 | 2020-10-28 17:18:18 +0100 | [diff] [blame] | 1404 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_use_srtp_ext", ret ); | 
|  | 1405 | return( ret ); | 
| Ron Eldor | 3adb992 | 2017-12-21 10:15:08 +0200 | [diff] [blame] | 1406 | } | 
| Johan Pascal | c3ccd98 | 2020-10-28 17:18:18 +0100 | [diff] [blame] | 1407 | ext_len += olen; | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1408 | #endif | 
|  | 1409 |  | 
| Simon Butcher | 5624ec8 | 2015-09-29 01:06:06 +0100 | [diff] [blame] | 1410 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1411 | if( ( ret = ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, | 
|  | 1412 | end, &olen ) ) != 0 ) | 
|  | 1413 | { | 
|  | 1414 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_session_ticket_ext", ret ); | 
|  | 1415 | return( ret ); | 
|  | 1416 | } | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 1417 | ext_len += olen; | 
|  | 1418 | #endif | 
|  | 1419 |  | 
| Manuel Pégourié-Gonnard | eaecbd3 | 2014-11-06 02:38:02 +0100 | [diff] [blame] | 1420 | /* olen unused if all extensions are disabled */ | 
|  | 1421 | ((void) olen); | 
|  | 1422 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1423 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %d", | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1424 | ext_len ) ); | 
| Paul Bakker | c3f177a | 2012-04-11 16:11:49 +0000 | [diff] [blame] | 1425 |  | 
| Paul Bakker | a703663 | 2014-04-30 10:15:38 +0200 | [diff] [blame] | 1426 | if( ext_len > 0 ) | 
|  | 1427 | { | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 1428 | /* No need to check for space here, because the extension | 
|  | 1429 | * writing functions already took care of that. */ | 
| Paul Bakker | a703663 | 2014-04-30 10:15:38 +0200 | [diff] [blame] | 1430 | *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF ); | 
|  | 1431 | *p++ = (unsigned char)( ( ext_len      ) & 0xFF ); | 
|  | 1432 | p += ext_len; | 
|  | 1433 | } | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1434 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1435 | ssl->out_msglen  = p - buf; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1436 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; | 
|  | 1437 | ssl->out_msg[0]  = MBEDTLS_SSL_HS_CLIENT_HELLO; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1438 |  | 
|  | 1439 | ssl->state++; | 
|  | 1440 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1441 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1442 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1443 | mbedtls_ssl_send_flight_completed( ssl ); | 
| Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 1444 | #endif | 
|  | 1445 |  | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 1446 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1447 | { | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 1448 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1449 | return( ret ); | 
|  | 1450 | } | 
|  | 1451 |  | 
| Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 1452 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 1453 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | 
|  | 1454 | ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) | 
|  | 1455 | { | 
|  | 1456 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); | 
|  | 1457 | return( ret ); | 
|  | 1458 | } | 
| Hanno Becker | bc2498a | 2018-08-28 10:13:29 +0100 | [diff] [blame] | 1459 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 1460 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1461 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1462 |  | 
|  | 1463 | return( 0 ); | 
|  | 1464 | } | 
|  | 1465 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1466 | static int ssl_parse_renegotiation_info( mbedtls_ssl_context *ssl, | 
| Manuel Pégourié-Gonnard | e048b67 | 2013-07-19 12:47:00 +0200 | [diff] [blame] | 1467 | const unsigned char *buf, | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1468 | size_t len ) | 
|  | 1469 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1470 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 1471 | if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1472 | { | 
| Manuel Pégourié-Gonnard | 31ff1d2 | 2013-10-28 13:46:11 +0100 | [diff] [blame] | 1473 | /* Check verify-data in constant-time. The length OTOH is no secret */ | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1474 | if( len    != 1 + ssl->verify_data_len * 2 || | 
|  | 1475 | buf[0] !=     ssl->verify_data_len * 2 || | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1476 | mbedtls_ssl_safer_memcmp( buf + 1, | 
| Manuel Pégourié-Gonnard | 31ff1d2 | 2013-10-28 13:46:11 +0100 | [diff] [blame] | 1477 | ssl->own_verify_data, ssl->verify_data_len ) != 0 || | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1478 | mbedtls_ssl_safer_memcmp( buf + 1 + ssl->verify_data_len, | 
| Manuel Pégourié-Gonnard | 31ff1d2 | 2013-10-28 13:46:11 +0100 | [diff] [blame] | 1479 | ssl->peer_verify_data, ssl->verify_data_len ) != 0 ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1480 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1481 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1482 | mbedtls_ssl_send_alert_message( | 
|  | 1483 | ssl, | 
|  | 1484 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1485 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1486 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1487 | } | 
|  | 1488 | } | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1489 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1490 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1491 | { | 
|  | 1492 | if( len != 1 || buf[0] != 0x00 ) | 
|  | 1493 | { | 
| Ronald Cron | 5ee5707 | 2020-06-11 09:34:06 +0200 | [diff] [blame] | 1494 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 1495 | ( "non-zero length renegotiation info" ) ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1496 | mbedtls_ssl_send_alert_message( | 
|  | 1497 | ssl, | 
|  | 1498 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1499 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1500 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1501 | } | 
|  | 1502 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1503 | ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION; | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1504 | } | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1505 |  | 
|  | 1506 | return( 0 ); | 
|  | 1507 | } | 
| Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 1508 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1509 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
|  | 1510 | 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] | 1511 | const unsigned char *buf, | 
| Manuel Pégourié-Gonnard | de600e5 | 2013-07-17 10:14:38 +0200 | [diff] [blame] | 1512 | size_t len ) | 
|  | 1513 | { | 
|  | 1514 | /* | 
|  | 1515 | * server should use the extension only if we did, | 
|  | 1516 | * and if so the server's value should match ours (and len is always 1) | 
|  | 1517 | */ | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1518 | 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] | 1519 | len != 1 || | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1520 | buf[0] != ssl->conf->mfl_code ) | 
| Manuel Pégourié-Gonnard | de600e5 | 2013-07-17 10:14:38 +0200 | [diff] [blame] | 1521 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1522 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 1523 | ( "non-matching max fragment length extension" ) ); | 
|  | 1524 | mbedtls_ssl_send_alert_message( | 
|  | 1525 | ssl, | 
|  | 1526 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1527 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1528 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
| Manuel Pégourié-Gonnard | de600e5 | 2013-07-17 10:14:38 +0200 | [diff] [blame] | 1529 | } | 
|  | 1530 |  | 
|  | 1531 | return( 0 ); | 
|  | 1532 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1533 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1534 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1535 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) | 
|  | 1536 | 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] | 1537 | const unsigned char *buf, | 
|  | 1538 | size_t len ) | 
|  | 1539 | { | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1540 | if( ssl->conf->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_DISABLED || | 
| Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 1541 | len != 0 ) | 
|  | 1542 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1543 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 1544 | ( "non-matching truncated HMAC extension" ) ); | 
|  | 1545 | mbedtls_ssl_send_alert_message( | 
|  | 1546 | ssl, | 
|  | 1547 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1548 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1549 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
| Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 1550 | } | 
|  | 1551 |  | 
|  | 1552 | ((void) buf); | 
|  | 1553 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1554 | ssl->session_negotiate->trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; | 
| Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 1555 |  | 
|  | 1556 | return( 0 ); | 
|  | 1557 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1558 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ | 
| Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 1559 |  | 
| Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1560 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
| Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 1561 | static int ssl_parse_cid_ext( mbedtls_ssl_context *ssl, | 
|  | 1562 | const unsigned char *buf, | 
|  | 1563 | size_t len ) | 
|  | 1564 | { | 
|  | 1565 | size_t peer_cid_len; | 
|  | 1566 |  | 
|  | 1567 | if( /* CID extension only makes sense in DTLS */ | 
|  | 1568 | ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || | 
|  | 1569 | /* 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] | 1570 | ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED ) | 
| Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 1571 | { | 
| Hanno Becker | 2262648 | 2019-05-03 12:46:59 +0100 | [diff] [blame] | 1572 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension unexpected" ) ); | 
| Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 1573 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
| Hanno Becker | 2262648 | 2019-05-03 12:46:59 +0100 | [diff] [blame] | 1574 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
|  | 1575 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
|  | 1576 | } | 
|  | 1577 |  | 
|  | 1578 | if( len == 0 ) | 
|  | 1579 | { | 
|  | 1580 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) ); | 
|  | 1581 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1582 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 1583 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
|  | 1584 | } | 
|  | 1585 |  | 
|  | 1586 | peer_cid_len = *buf++; | 
|  | 1587 | len--; | 
|  | 1588 |  | 
|  | 1589 | if( peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX ) | 
|  | 1590 | { | 
| Hanno Becker | 2262648 | 2019-05-03 12:46:59 +0100 | [diff] [blame] | 1591 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) ); | 
| Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 1592 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
| Hanno Becker | 2262648 | 2019-05-03 12:46:59 +0100 | [diff] [blame] | 1593 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 1594 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
|  | 1595 | } | 
|  | 1596 |  | 
|  | 1597 | if( len != peer_cid_len ) | 
|  | 1598 | { | 
| Hanno Becker | 2262648 | 2019-05-03 12:46:59 +0100 | [diff] [blame] | 1599 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) ); | 
| Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 1600 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
| Hanno Becker | 2262648 | 2019-05-03 12:46:59 +0100 | [diff] [blame] | 1601 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); | 
| Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 1602 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
|  | 1603 | } | 
|  | 1604 |  | 
| Hanno Becker | 5a29990 | 2019-05-03 12:47:49 +0100 | [diff] [blame] | 1605 | ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED; | 
| Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 1606 | ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len; | 
|  | 1607 | memcpy( ssl->handshake->peer_cid, buf, peer_cid_len ); | 
|  | 1608 |  | 
|  | 1609 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use of CID extension negotiated" ) ); | 
|  | 1610 | MBEDTLS_SSL_DEBUG_BUF( 3, "Server CID", buf, peer_cid_len ); | 
|  | 1611 |  | 
| Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 1612 | return( 0 ); | 
|  | 1613 | } | 
| Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1614 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
| Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 1615 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1616 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
|  | 1617 | 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] | 1618 | const unsigned char *buf, | 
|  | 1619 | size_t len ) | 
|  | 1620 | { | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1621 | if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED || | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1622 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 || | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 1623 | len != 0 ) | 
|  | 1624 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1625 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 1626 | ( "non-matching encrypt-then-MAC extension" ) ); | 
|  | 1627 | mbedtls_ssl_send_alert_message( | 
|  | 1628 | ssl, | 
|  | 1629 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1630 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1631 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 1632 | } | 
|  | 1633 |  | 
|  | 1634 | ((void) buf); | 
|  | 1635 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1636 | ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 1637 |  | 
|  | 1638 | return( 0 ); | 
|  | 1639 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1640 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 1641 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1642 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) | 
|  | 1643 | 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] | 1644 | const unsigned char *buf, | 
|  | 1645 | size_t len ) | 
|  | 1646 | { | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1647 | if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED || | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1648 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 || | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 1649 | len != 0 ) | 
|  | 1650 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1651 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 1652 | ( "non-matching extended master secret extension" ) ); | 
|  | 1653 | mbedtls_ssl_send_alert_message( | 
|  | 1654 | ssl, | 
|  | 1655 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1656 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1657 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 1658 | } | 
|  | 1659 |  | 
|  | 1660 | ((void) buf); | 
|  | 1661 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1662 | ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 1663 |  | 
|  | 1664 | return( 0 ); | 
|  | 1665 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1666 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 1667 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1668 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | 
|  | 1669 | 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] | 1670 | const unsigned char *buf, | 
|  | 1671 | size_t len ) | 
|  | 1672 | { | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1673 | if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED || | 
| Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 1674 | len != 0 ) | 
|  | 1675 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1676 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 1677 | ( "non-matching session ticket extension" ) ); | 
|  | 1678 | mbedtls_ssl_send_alert_message( | 
|  | 1679 | ssl, | 
|  | 1680 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1681 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1682 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
| Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 1683 | } | 
| Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 1684 |  | 
|  | 1685 | ((void) buf); | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 1686 |  | 
|  | 1687 | ssl->handshake->new_session_ticket = 1; | 
| Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 1688 |  | 
|  | 1689 | return( 0 ); | 
|  | 1690 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1691 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ | 
| Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 1692 |  | 
| Robert Cragie | 136884c | 2015-10-02 13:34:31 +0100 | [diff] [blame] | 1693 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ | 
| Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 1694 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1695 | 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] | 1696 | const unsigned char *buf, | 
|  | 1697 | size_t len ) | 
|  | 1698 | { | 
|  | 1699 | size_t list_size; | 
|  | 1700 | const unsigned char *p; | 
|  | 1701 |  | 
| Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 1702 | if( len == 0 || (size_t)( buf[0] + 1 ) != len ) | 
| Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 1703 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1704 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1705 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1706 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1707 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
| Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 1708 | } | 
| Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 1709 | list_size = buf[0]; | 
| Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 1710 |  | 
| Manuel Pégourié-Gonnard | fd35af1 | 2014-06-23 14:10:13 +0200 | [diff] [blame] | 1711 | p = buf + 1; | 
| Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 1712 | while( list_size > 0 ) | 
|  | 1713 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1714 | if( p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED || | 
|  | 1715 | p[0] == MBEDTLS_ECP_PF_COMPRESSED ) | 
| Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 1716 | { | 
| Robert Cragie | 136884c | 2015-10-02 13:34:31 +0100 | [diff] [blame] | 1717 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) | 
| Manuel Pégourié-Gonnard | 5734b2d | 2013-08-15 19:04:02 +0200 | [diff] [blame] | 1718 | ssl->handshake->ecdh_ctx.point_format = p[0]; | 
| Gilles Peskine | 064a85c | 2017-05-10 10:46:40 +0200 | [diff] [blame] | 1719 | #endif | 
| Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 1720 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Robert Cragie | 136884c | 2015-10-02 13:34:31 +0100 | [diff] [blame] | 1721 | ssl->handshake->ecjpake_ctx.point_format = p[0]; | 
|  | 1722 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1723 | 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] | 1724 | return( 0 ); | 
|  | 1725 | } | 
|  | 1726 |  | 
|  | 1727 | list_size--; | 
|  | 1728 | p++; | 
|  | 1729 | } | 
|  | 1730 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1731 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no point format in common" ) ); | 
| Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 1732 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1733 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1734 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
| Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 1735 | } | 
| Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 1736 | #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || | 
| Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 1737 | MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ | 
| Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 1738 |  | 
| Manuel Pégourié-Gonnard | 0a1324a | 2015-09-16 16:01:00 +0200 | [diff] [blame] | 1739 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
|  | 1740 | static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl, | 
|  | 1741 | const unsigned char *buf, | 
|  | 1742 | size_t len ) | 
|  | 1743 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1744 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | 0a1324a | 2015-09-16 16:01:00 +0200 | [diff] [blame] | 1745 |  | 
| Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1746 | if( ssl->handshake->ciphersuite_info->key_exchange != | 
| Manuel Pégourié-Gonnard | 0a1324a | 2015-09-16 16:01:00 +0200 | [diff] [blame] | 1747 | MBEDTLS_KEY_EXCHANGE_ECJPAKE ) | 
|  | 1748 | { | 
|  | 1749 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) ); | 
|  | 1750 | return( 0 ); | 
|  | 1751 | } | 
|  | 1752 |  | 
| Manuel Pégourié-Gonnard | d0d8cb3 | 2015-09-17 14:16:30 +0200 | [diff] [blame] | 1753 | /* If we got here, we no longer need our cached extension */ | 
|  | 1754 | mbedtls_free( ssl->handshake->ecjpake_cache ); | 
|  | 1755 | ssl->handshake->ecjpake_cache = NULL; | 
|  | 1756 | ssl->handshake->ecjpake_cache_len = 0; | 
|  | 1757 |  | 
| Manuel Pégourié-Gonnard | 0a1324a | 2015-09-16 16:01:00 +0200 | [diff] [blame] | 1758 | if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx, | 
|  | 1759 | buf, len ) ) != 0 ) | 
|  | 1760 | { | 
|  | 1761 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1762 | mbedtls_ssl_send_alert_message( | 
|  | 1763 | ssl, | 
|  | 1764 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1765 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Manuel Pégourié-Gonnard | 0a1324a | 2015-09-16 16:01:00 +0200 | [diff] [blame] | 1766 | return( ret ); | 
|  | 1767 | } | 
|  | 1768 |  | 
|  | 1769 | return( 0 ); | 
|  | 1770 | } | 
|  | 1771 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1772 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1773 | #if defined(MBEDTLS_SSL_ALPN) | 
|  | 1774 | static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl, | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 1775 | const unsigned char *buf, size_t len ) | 
|  | 1776 | { | 
|  | 1777 | size_t list_len, name_len; | 
|  | 1778 | const char **p; | 
|  | 1779 |  | 
|  | 1780 | /* 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] | 1781 | if( ssl->conf->alpn_list == NULL ) | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1782 | { | 
|  | 1783 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching ALPN extension" ) ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 1784 | mbedtls_ssl_send_alert_message( | 
|  | 1785 | ssl, | 
|  | 1786 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1787 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1788 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1789 | } | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 1790 |  | 
|  | 1791 | /* | 
|  | 1792 | * opaque ProtocolName<1..2^8-1>; | 
|  | 1793 | * | 
|  | 1794 | * struct { | 
|  | 1795 | *     ProtocolName protocol_name_list<2..2^16-1> | 
|  | 1796 | * } ProtocolNameList; | 
|  | 1797 | * | 
|  | 1798 | * the "ProtocolNameList" MUST contain exactly one "ProtocolName" | 
|  | 1799 | */ | 
|  | 1800 |  | 
|  | 1801 | /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */ | 
|  | 1802 | if( len < 4 ) | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1803 | { | 
|  | 1804 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1805 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1806 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1807 | } | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 1808 |  | 
|  | 1809 | list_len = ( buf[0] << 8 ) | buf[1]; | 
|  | 1810 | if( list_len != len - 2 ) | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1811 | { | 
|  | 1812 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1813 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1814 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1815 | } | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 1816 |  | 
|  | 1817 | name_len = buf[2]; | 
|  | 1818 | if( name_len != list_len - 1 ) | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1819 | { | 
|  | 1820 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1821 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1822 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1823 | } | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 1824 |  | 
|  | 1825 | /* 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] | 1826 | for( p = ssl->conf->alpn_list; *p != NULL; p++ ) | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 1827 | { | 
|  | 1828 | if( name_len == strlen( *p ) && | 
|  | 1829 | memcmp( buf + 3, *p, name_len ) == 0 ) | 
|  | 1830 | { | 
|  | 1831 | ssl->alpn_chosen = *p; | 
|  | 1832 | return( 0 ); | 
|  | 1833 | } | 
|  | 1834 | } | 
|  | 1835 |  | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1836 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "ALPN extension: no matching protocol" ) ); | 
| Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 1837 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1838 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1839 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 1840 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1841 | #endif /* MBEDTLS_SSL_ALPN */ | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 1842 |  | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1843 | #if defined(MBEDTLS_SSL_DTLS_SRTP) | 
|  | 1844 | static int ssl_parse_use_srtp_ext( mbedtls_ssl_context *ssl, | 
| Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 1845 | const unsigned char *buf, | 
|  | 1846 | size_t len ) | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1847 | { | 
| Johan Pascal | 43f9490 | 2020-09-22 12:25:52 +0200 | [diff] [blame] | 1848 | mbedtls_ssl_srtp_profile server_protection = MBEDTLS_TLS_SRTP_UNSET; | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 1849 | size_t i, mki_len = 0; | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1850 | uint16_t server_protection_profile_value = 0; | 
|  | 1851 |  | 
|  | 1852 | /* If use_srtp is not configured, just ignore the extension */ | 
| Johan Pascal | c3ccd98 | 2020-10-28 17:18:18 +0100 | [diff] [blame] | 1853 | if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) || | 
|  | 1854 | ( ssl->conf->dtls_srtp_profile_list == NULL ) || | 
|  | 1855 | ( ssl->conf->dtls_srtp_profile_list_len == 0 ) ) | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1856 | return( 0 ); | 
|  | 1857 |  | 
| Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 1858 | /* RFC 5764 section 4.1.1 | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1859 | * uint8 SRTPProtectionProfile[2]; | 
|  | 1860 | * | 
|  | 1861 | * struct { | 
|  | 1862 | *   SRTPProtectionProfiles SRTPProtectionProfiles; | 
|  | 1863 | *   opaque srtp_mki<0..255>; | 
|  | 1864 | * } UseSRTPData; | 
|  | 1865 |  | 
|  | 1866 | * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>; | 
|  | 1867 | * | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1868 | */ | 
| Johan Pascal | f6417ec | 2020-09-22 15:15:19 +0200 | [diff] [blame] | 1869 | 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] | 1870 | { | 
|  | 1871 | mki_len = ssl->dtls_srtp_info.mki_len; | 
|  | 1872 | } | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1873 |  | 
| Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 1874 | /* | 
| Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 1875 | * Length is 5 + optional mki_value : one protection profile length (2 bytes) | 
|  | 1876 | *                                      + protection profile (2 bytes) | 
|  | 1877 | *                                      + mki_len(1 byte) | 
| Ron Eldor | 313d7b5 | 2018-12-10 14:56:21 +0200 | [diff] [blame] | 1878 | *                                      and optional srtp_mki | 
| Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 1879 | */ | 
| Johan Pascal | adbd944 | 2020-10-26 21:24:25 +0100 | [diff] [blame] | 1880 | if( ( len < 5 ) || ( len != ( buf[4] + 5u ) ) ) | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1881 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
|  | 1882 |  | 
|  | 1883 | /* | 
|  | 1884 | * get the server protection profile | 
|  | 1885 | */ | 
| Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 1886 |  | 
|  | 1887 | /* | 
|  | 1888 | * protection profile length must be 0x0002 as we must have only | 
|  | 1889 | * one protection profile in server Hello | 
|  | 1890 | */ | 
| Ron Eldor | 089c9fe | 2018-12-06 17:12:49 +0200 | [diff] [blame] | 1891 | if( (  buf[0] != 0 ) || ( buf[1] != 2 ) ) | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1892 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
| Ron Eldor | 089c9fe | 2018-12-06 17:12:49 +0200 | [diff] [blame] | 1893 |  | 
|  | 1894 | server_protection_profile_value = ( buf[2] << 8 ) | buf[3]; | 
| Johan Pascal | 43f9490 | 2020-09-22 12:25:52 +0200 | [diff] [blame] | 1895 | server_protection = mbedtls_ssl_check_srtp_profile_value( | 
|  | 1896 | server_protection_profile_value ); | 
|  | 1897 | if( server_protection != MBEDTLS_TLS_SRTP_UNSET ) | 
| Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 1898 | { | 
| Johan Pascal | 43f9490 | 2020-09-22 12:25:52 +0200 | [diff] [blame] | 1899 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found srtp profile: %s", | 
|  | 1900 | mbedtls_ssl_get_srtp_profile_as_string( | 
|  | 1901 | server_protection ) ) ); | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1902 | } | 
|  | 1903 |  | 
| Johan Pascal | 43f9490 | 2020-09-22 12:25:52 +0200 | [diff] [blame] | 1904 | ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET; | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 1905 |  | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1906 | /* | 
|  | 1907 | * Check we have the server profile in our list | 
|  | 1908 | */ | 
| Ron Eldor | 3adb992 | 2017-12-21 10:15:08 +0200 | [diff] [blame] | 1909 | for( i=0; i < ssl->conf->dtls_srtp_profile_list_len; i++) | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1910 | { | 
| Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 1911 | if( server_protection == ssl->conf->dtls_srtp_profile_list[i] ) | 
|  | 1912 | { | 
| Ron Eldor | 3adb992 | 2017-12-21 10:15:08 +0200 | [diff] [blame] | 1913 | 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] | 1914 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected srtp profile: %s", | 
|  | 1915 | mbedtls_ssl_get_srtp_profile_as_string( | 
|  | 1916 | server_protection ) ) ); | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 1917 | break; | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1918 | } | 
|  | 1919 | } | 
|  | 1920 |  | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 1921 | /* 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] | 1922 | 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] | 1923 | { | 
|  | 1924 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
| Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 1925 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 1926 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
|  | 1927 | } | 
| Johan Pascal | 20c7db3 | 2020-10-26 22:45:58 +0100 | [diff] [blame] | 1928 |  | 
|  | 1929 | /* If server does not use mki in its reply, make sure the client won't keep | 
|  | 1930 | * one as negotiated */ | 
|  | 1931 | if( len == 5 ) | 
|  | 1932 | { | 
|  | 1933 | ssl->dtls_srtp_info.mki_len = 0; | 
|  | 1934 | } | 
|  | 1935 |  | 
| Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 1936 | /* | 
|  | 1937 | * RFC5764: | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 1938 | *  If the client detects a nonzero-length MKI in the server's response | 
|  | 1939 | *  that is different than the one the client offered, then the client | 
|  | 1940 | *  MUST abort the handshake and SHOULD send an invalid_parameter alert. | 
|  | 1941 | */ | 
| Ron Eldor | 313d7b5 | 2018-12-10 14:56:21 +0200 | [diff] [blame] | 1942 | if( len > 5  && ( buf[4] != mki_len || | 
|  | 1943 | ( memcmp( ssl->dtls_srtp_info.mki_value, &buf[5], mki_len ) ) ) ) | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 1944 | { | 
|  | 1945 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1946 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); | 
|  | 1947 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
|  | 1948 | } | 
| Ron Eldor | b465539 | 2018-07-05 18:25:39 +0300 | [diff] [blame] | 1949 | #if defined (MBEDTLS_DEBUG_C) | 
| Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 1950 | if( len > 5 ) | 
| Ron Eldor | b465539 | 2018-07-05 18:25:39 +0300 | [diff] [blame] | 1951 | { | 
| Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 1952 | MBEDTLS_SSL_DEBUG_BUF( 3, "received mki", ssl->dtls_srtp_info.mki_value, | 
|  | 1953 | ssl->dtls_srtp_info.mki_len ); | 
| Ron Eldor | b465539 | 2018-07-05 18:25:39 +0300 | [diff] [blame] | 1954 | } | 
|  | 1955 | #endif | 
| Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 1956 | return( 0 ); | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1957 | } | 
|  | 1958 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ | 
|  | 1959 |  | 
| Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1960 | /* | 
|  | 1961 | * Parse HelloVerifyRequest.  Only called after verifying the HS type. | 
|  | 1962 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1963 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 1964 | 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] | 1965 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1966 | 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] | 1967 | int major_ver, minor_ver; | 
|  | 1968 | unsigned char cookie_len; | 
|  | 1969 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1970 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse hello verify request" ) ); | 
| Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1971 |  | 
| Gilles Peskine | b64bf06 | 2019-09-27 14:02:44 +0200 | [diff] [blame] | 1972 | /* Check that there is enough room for: | 
|  | 1973 | * - 2 bytes of version | 
|  | 1974 | * - 1 byte of cookie_len | 
|  | 1975 | */ | 
|  | 1976 | if( mbedtls_ssl_hs_hdr_len( ssl ) + 3 > ssl->in_msglen ) | 
|  | 1977 | { | 
|  | 1978 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 1979 | ( "incoming HelloVerifyRequest message is too short" ) ); | 
|  | 1980 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 1981 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
|  | 1982 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
|  | 1983 | } | 
|  | 1984 |  | 
| Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1985 | /* | 
|  | 1986 | * struct { | 
|  | 1987 | *   ProtocolVersion server_version; | 
|  | 1988 | *   opaque cookie<0..2^8-1>; | 
|  | 1989 | * } HelloVerifyRequest; | 
|  | 1990 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1991 | MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 ); | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1992 | 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] | 1993 | p += 2; | 
|  | 1994 |  | 
| Manuel Pégourié-Gonnard | b35fe56 | 2014-08-09 17:00:46 +0200 | [diff] [blame] | 1995 | /* | 
|  | 1996 | * Since the RFC is not clear on this point, accept DTLS 1.0 (TLS 1.1) | 
|  | 1997 | * even is lower than our min version. | 
|  | 1998 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1999 | if( major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 || | 
|  | 2000 | minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 || | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2001 | major_ver > ssl->conf->max_major_ver  || | 
|  | 2002 | minor_ver > ssl->conf->max_minor_ver  ) | 
| Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 2003 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2004 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server version" ) ); | 
| Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 2005 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2006 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 2007 | MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION ); | 
| Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 2008 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2009 | return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION ); | 
| Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 2010 | } | 
|  | 2011 |  | 
|  | 2012 | cookie_len = *p++; | 
| Andres AG | 5a87c93 | 2016-09-26 14:53:05 +0100 | [diff] [blame] | 2013 | if( ( ssl->in_msg + ssl->in_msglen ) - p < cookie_len ) | 
|  | 2014 | { | 
|  | 2015 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 2016 | ( "cookie length does not match incoming message size" ) ); | 
|  | 2017 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 2018 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
|  | 2019 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
|  | 2020 | } | 
| Gilles Peskine | b51130d | 2019-09-27 14:00:36 +0200 | [diff] [blame] | 2021 | MBEDTLS_SSL_DEBUG_BUF( 3, "cookie", p, cookie_len ); | 
| Andres AG | 5a87c93 | 2016-09-26 14:53:05 +0100 | [diff] [blame] | 2022 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2023 | mbedtls_free( ssl->handshake->verify_cookie ); | 
| Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 2024 |  | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 2025 | ssl->handshake->verify_cookie = mbedtls_calloc( 1, cookie_len ); | 
| Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 2026 | if( ssl->handshake->verify_cookie  == NULL ) | 
|  | 2027 | { | 
| Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 2028 | 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] | 2029 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
| Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 2030 | } | 
|  | 2031 |  | 
|  | 2032 | memcpy( ssl->handshake->verify_cookie, p, cookie_len ); | 
|  | 2033 | ssl->handshake->verify_cookie_len = cookie_len; | 
|  | 2034 |  | 
| Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 2035 | /* Start over at ClientHello */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2036 | ssl->state = MBEDTLS_SSL_CLIENT_HELLO; | 
|  | 2037 | mbedtls_ssl_reset_checksum( ssl ); | 
| Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 2038 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2039 | mbedtls_ssl_recv_flight_completed( ssl ); | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2040 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2041 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse hello verify request" ) ); | 
| Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 2042 |  | 
|  | 2043 | return( 0 ); | 
|  | 2044 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2045 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 2046 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2047 | static int ssl_parse_server_hello( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2048 | { | 
| Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 2049 | int ret, i; | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 2050 | size_t n; | 
| Manuel Pégourié-Gonnard | f7cdbc0 | 2014-10-17 17:02:10 +0200 | [diff] [blame] | 2051 | size_t ext_len; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2052 | unsigned char *buf, *ext; | 
| Manuel Pégourié-Gonnard | 1cf7b30 | 2015-06-24 22:28:19 +0200 | [diff] [blame] | 2053 | unsigned char comp; | 
|  | 2054 | #if defined(MBEDTLS_ZLIB_SUPPORT) | 
|  | 2055 | int accept_comp; | 
|  | 2056 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2057 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2058 | int renegotiation_info_seen = 0; | 
| Manuel Pégourié-Gonnard | eaecbd3 | 2014-11-06 02:38:02 +0100 | [diff] [blame] | 2059 | #endif | 
| Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2060 | int handshake_failure = 0; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2061 | const mbedtls_ssl_ciphersuite_t *suite_info; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2062 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2063 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2064 |  | 
| Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 2065 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2066 | { | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2067 | /* No alert on a read error. */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2068 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2069 | return( ret ); | 
|  | 2070 | } | 
|  | 2071 |  | 
| Hanno Becker | 79594fd | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 2072 | buf = ssl->in_msg; | 
|  | 2073 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2074 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2075 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2076 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 2077 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) | 
| Manuel Pégourié-Gonnard | 6591962 | 2014-08-19 12:50:30 +0200 | [diff] [blame] | 2078 | { | 
| Manuel Pégourié-Gonnard | 44ade65 | 2014-08-19 13:58:40 +0200 | [diff] [blame] | 2079 | ssl->renego_records_seen++; | 
|  | 2080 |  | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2081 | if( ssl->conf->renego_max_records >= 0 && | 
|  | 2082 | ssl->renego_records_seen > ssl->conf->renego_max_records ) | 
| Manuel Pégourié-Gonnard | 44ade65 | 2014-08-19 13:58:40 +0200 | [diff] [blame] | 2083 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2084 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 2085 | ( "renegotiation requested, but not honored by server" ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2086 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); | 
| Manuel Pégourié-Gonnard | 44ade65 | 2014-08-19 13:58:40 +0200 | [diff] [blame] | 2087 | } | 
|  | 2088 |  | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2089 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 2090 | ( "non-handshake message during renegotiation" ) ); | 
| Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 2091 |  | 
|  | 2092 | ssl->keep_current_message = 1; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2093 | return( MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO ); | 
| Manuel Pégourié-Gonnard | 6591962 | 2014-08-19 12:50:30 +0200 | [diff] [blame] | 2094 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2095 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ | 
| Manuel Pégourié-Gonnard | 6591962 | 2014-08-19 12:50:30 +0200 | [diff] [blame] | 2096 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2097 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2098 | mbedtls_ssl_send_alert_message( | 
|  | 2099 | ssl, | 
|  | 2100 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 2101 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2102 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2103 | } | 
|  | 2104 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2105 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2106 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 2107 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2108 | if( buf[0] == MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST ) | 
| Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 2109 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2110 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "received hello verify request" ) ); | 
|  | 2111 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) ); | 
| Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 2112 | return( ssl_parse_hello_verify_request( ssl ) ); | 
|  | 2113 | } | 
|  | 2114 | else | 
|  | 2115 | { | 
|  | 2116 | /* We made it through the verification process */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2117 | mbedtls_free( ssl->handshake->verify_cookie ); | 
| Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 2118 | ssl->handshake->verify_cookie = NULL; | 
|  | 2119 | ssl->handshake->verify_cookie_len = 0; | 
|  | 2120 | } | 
|  | 2121 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2122 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2123 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2124 | if( ssl->in_hslen < 38 + mbedtls_ssl_hs_hdr_len( ssl ) || | 
|  | 2125 | buf[0] != MBEDTLS_SSL_HS_SERVER_HELLO ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2126 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2127 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2128 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 2129 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2130 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2131 | } | 
|  | 2132 |  | 
| Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 2133 | /* | 
|  | 2134 | *  0   .  1    server_version | 
|  | 2135 | *  2   . 33    random (maybe including 4 bytes of Unix time) | 
|  | 2136 | * 34   . 34    session_id length = n | 
|  | 2137 | * 35   . 34+n  session_id | 
|  | 2138 | * 35+n . 36+n  cipher_suite | 
|  | 2139 | * 37+n . 37+n  compression_method | 
|  | 2140 | * | 
|  | 2141 | * 38+n . 39+n  extensions length (optional) | 
|  | 2142 | * 40+n .  ..   extensions | 
|  | 2143 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2144 | buf += mbedtls_ssl_hs_hdr_len( ssl ); | 
| Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 2145 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2146 | MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", buf + 0, 2 ); | 
|  | 2147 | mbedtls_ssl_read_version( &ssl->major_ver, &ssl->minor_ver, | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2148 | ssl->conf->transport, buf + 0 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2149 |  | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2150 | if( ssl->major_ver < ssl->conf->min_major_ver || | 
|  | 2151 | ssl->minor_ver < ssl->conf->min_minor_ver || | 
|  | 2152 | ssl->major_ver > ssl->conf->max_major_ver || | 
|  | 2153 | ssl->minor_ver > ssl->conf->max_minor_ver ) | 
| Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 2154 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2155 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 2156 | ( "server version out of bounds -  min: [%d:%d], server: [%d:%d], max: [%d:%d]", | 
|  | 2157 | ssl->conf->min_major_ver, | 
|  | 2158 | ssl->conf->min_minor_ver, | 
|  | 2159 | ssl->major_ver, ssl->minor_ver, | 
|  | 2160 | ssl->conf->max_major_ver, | 
|  | 2161 | ssl->conf->max_minor_ver ) ); | 
| Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 2162 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2163 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 2164 | MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION ); | 
| Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 2165 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2166 | return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION ); | 
| Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 2167 | } | 
|  | 2168 |  | 
| Andres Amaya Garcia | 6bce9cb | 2017-09-06 15:33:34 +0100 | [diff] [blame] | 2169 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", | 
|  | 2170 | ( (uint32_t) buf[2] << 24 ) | | 
|  | 2171 | ( (uint32_t) buf[3] << 16 ) | | 
|  | 2172 | ( (uint32_t) buf[4] <<  8 ) | | 
|  | 2173 | ( (uint32_t) buf[5]       ) ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2174 |  | 
| Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 2175 | memcpy( ssl->handshake->randbytes + 32, buf + 2, 32 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2176 |  | 
| Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 2177 | n = buf[34]; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2178 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2179 | MBEDTLS_SSL_DEBUG_BUF( 3,   "server hello, random bytes", buf + 2, 32 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2180 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2181 | if( n > 32 ) | 
|  | 2182 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2183 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2184 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 2185 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2186 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2187 | } | 
|  | 2188 |  | 
| Manuel Pégourié-Gonnard | a6e5bd5 | 2015-07-23 12:14:13 +0200 | [diff] [blame] | 2189 | if( ssl->in_hslen > mbedtls_ssl_hs_hdr_len( ssl ) + 39 + n ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2190 | { | 
| Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 2191 | ext_len = ( ( buf[38 + n] <<  8 ) | 
|  | 2192 | | ( buf[39 + n]       ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2193 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2194 | if( ( ext_len > 0 && ext_len < 4 ) || | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2195 | 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] | 2196 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2197 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2198 | mbedtls_ssl_send_alert_message( | 
|  | 2199 | ssl, | 
|  | 2200 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 2201 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2202 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2203 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2204 | } | 
| Manuel Pégourié-Gonnard | a6e5bd5 | 2015-07-23 12:14:13 +0200 | [diff] [blame] | 2205 | 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] | 2206 | { | 
|  | 2207 | ext_len = 0; | 
|  | 2208 | } | 
|  | 2209 | else | 
|  | 2210 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2211 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2212 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 2213 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2214 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
| Manuel Pégourié-Gonnard | f7cdbc0 | 2014-10-17 17:02:10 +0200 | [diff] [blame] | 2215 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2216 |  | 
| Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 2217 | /* ciphersuite (used later) */ | 
| Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 2218 | i = ( buf[35 + n] << 8 ) | buf[36 + n]; | 
| Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 2219 |  | 
|  | 2220 | /* | 
|  | 2221 | * Read and check compression | 
|  | 2222 | */ | 
| Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 2223 | comp = buf[37 + n]; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2224 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2225 | #if defined(MBEDTLS_ZLIB_SUPPORT) | 
| Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 2226 | /* See comments in ssl_write_client_hello() */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2227 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2228 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 2229 | accept_comp = 0; | 
| Manuel Pégourié-Gonnard | 1cf7b30 | 2015-06-24 22:28:19 +0200 | [diff] [blame] | 2230 | else | 
| Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 2231 | #endif | 
| Manuel Pégourié-Gonnard | 1cf7b30 | 2015-06-24 22:28:19 +0200 | [diff] [blame] | 2232 | accept_comp = 1; | 
| Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 2233 |  | 
| Manuel Pégourié-Gonnard | 1cf7b30 | 2015-06-24 22:28:19 +0200 | [diff] [blame] | 2234 | if( comp != MBEDTLS_SSL_COMPRESS_NULL && | 
|  | 2235 | ( comp != MBEDTLS_SSL_COMPRESS_DEFLATE || accept_comp == 0 ) ) | 
|  | 2236 | #else /* MBEDTLS_ZLIB_SUPPORT */ | 
|  | 2237 | if( comp != MBEDTLS_SSL_COMPRESS_NULL ) | 
|  | 2238 | #endif/* MBEDTLS_ZLIB_SUPPORT */ | 
| Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 2239 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2240 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 2241 | ( "server hello, bad compression: %d", comp ) ); | 
|  | 2242 | mbedtls_ssl_send_alert_message( | 
|  | 2243 | ssl, | 
|  | 2244 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 2245 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2246 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
| Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 2247 | } | 
|  | 2248 |  | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2249 | /* | 
|  | 2250 | * Initialize update checksum functions | 
|  | 2251 | */ | 
| Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 2252 | ssl->handshake->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( i ); | 
|  | 2253 | if( ssl->handshake->ciphersuite_info == NULL ) | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2254 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2255 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 2256 | ( "ciphersuite info for %04x not found", i ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2257 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 2258 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2259 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2260 | } | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2261 |  | 
| Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 2262 | mbedtls_ssl_optimize_checksum( ssl, ssl->handshake->ciphersuite_info ); | 
| Manuel Pégourié-Gonnard | 3c599f1 | 2014-03-10 13:25:07 +0100 | [diff] [blame] | 2263 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2264 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) ); | 
|  | 2265 | MBEDTLS_SSL_DEBUG_BUF( 3,   "server hello, session id", buf + 35, n ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2266 |  | 
|  | 2267 | /* | 
|  | 2268 | * Check if the session can be resumed | 
|  | 2269 | */ | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2270 | if( ssl->handshake->resume == 0 || n == 0 || | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2271 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 2272 | ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE || | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2273 | #endif | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2274 | ssl->session_negotiate->ciphersuite != i || | 
|  | 2275 | ssl->session_negotiate->compression != comp || | 
| Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 2276 | ssl->session_negotiate->id_len != n || | 
| Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 2277 | memcmp( ssl->session_negotiate->id, buf + 35, n ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2278 | { | 
|  | 2279 | ssl->state++; | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 2280 | ssl->handshake->resume = 0; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2281 | #if defined(MBEDTLS_HAVE_TIME) | 
| SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 2282 | ssl->session_negotiate->start = mbedtls_time( NULL ); | 
| Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 2283 | #endif | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2284 | ssl->session_negotiate->ciphersuite = i; | 
|  | 2285 | ssl->session_negotiate->compression = comp; | 
| Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 2286 | ssl->session_negotiate->id_len = n; | 
| Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 2287 | memcpy( ssl->session_negotiate->id, buf + 35, n ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2288 | } | 
|  | 2289 | else | 
|  | 2290 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2291 | ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC; | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 2292 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2293 | if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 ) | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 2294 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2295 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2296 | mbedtls_ssl_send_alert_message( | 
|  | 2297 | ssl, | 
|  | 2298 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 2299 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 2300 | return( ret ); | 
|  | 2301 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2302 | } | 
|  | 2303 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2304 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed", | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 2305 | ssl->handshake->resume ? "a" : "no" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2306 |  | 
| Manuel Pégourié-Gonnard | 60884a1 | 2015-09-16 11:13:41 +0200 | [diff] [blame] | 2307 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %04x", i ) ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2308 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d", | 
|  | 2309 | buf[37 + n] ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2310 |  | 
| Andrzej Kurek | 03bac44 | 2018-04-25 05:06:07 -0400 | [diff] [blame] | 2311 | /* | 
|  | 2312 | * 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] | 2313 | */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2314 | i = 0; | 
|  | 2315 | while( 1 ) | 
|  | 2316 | { | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2317 | if( ssl->conf->ciphersuite_list[ssl->minor_ver][i] == 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2318 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2319 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2320 | mbedtls_ssl_send_alert_message( | 
|  | 2321 | ssl, | 
|  | 2322 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 2323 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2324 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2325 | } | 
|  | 2326 |  | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2327 | if( ssl->conf->ciphersuite_list[ssl->minor_ver][i++] == | 
| Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 2328 | ssl->session_negotiate->ciphersuite ) | 
|  | 2329 | { | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2330 | break; | 
| Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 2331 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2332 | } | 
|  | 2333 |  | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2334 | suite_info = mbedtls_ssl_ciphersuite_from_id( | 
|  | 2335 | ssl->session_negotiate->ciphersuite ); | 
|  | 2336 | if( ssl_validate_ciphersuite( suite_info, ssl, ssl->minor_ver, | 
|  | 2337 | ssl->minor_ver ) != 0 ) | 
| Mohammad Azim Khan | 1d3b508 | 2018-04-18 19:35:00 +0100 | [diff] [blame] | 2338 | { | 
|  | 2339 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2340 | mbedtls_ssl_send_alert_message( | 
|  | 2341 | ssl, | 
|  | 2342 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 2343 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); | 
| Mohammad Azim Khan | 1d3b508 | 2018-04-18 19:35:00 +0100 | [diff] [blame] | 2344 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
|  | 2345 | } | 
|  | 2346 |  | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2347 | MBEDTLS_SSL_DEBUG_MSG( 3, | 
|  | 2348 | ( "server hello, chosen ciphersuite: %s", suite_info->name ) ); | 
| Mohammad Azim Khan | 1d3b508 | 2018-04-18 19:35:00 +0100 | [diff] [blame] | 2349 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2350 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
| Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 2351 | if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA && | 
|  | 2352 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) | 
|  | 2353 | { | 
|  | 2354 | ssl->handshake->ecrs_enabled = 1; | 
|  | 2355 | } | 
|  | 2356 | #endif | 
|  | 2357 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2358 | if( comp != MBEDTLS_SSL_COMPRESS_NULL | 
|  | 2359 | #if defined(MBEDTLS_ZLIB_SUPPORT) | 
|  | 2360 | && comp != MBEDTLS_SSL_COMPRESS_DEFLATE | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2361 | #endif | 
|  | 2362 | ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2363 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2364 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2365 | mbedtls_ssl_send_alert_message( | 
|  | 2366 | ssl, | 
|  | 2367 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 2368 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2369 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2370 | } | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2371 | ssl->session_negotiate->compression = comp; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2372 |  | 
| Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 2373 | ext = buf + 40 + n; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2374 |  | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2375 | MBEDTLS_SSL_DEBUG_MSG( 2, | 
|  | 2376 | ( "server hello, total extension length: %d", ext_len ) ); | 
| Manuel Pégourié-Gonnard | a052849 | 2013-07-16 17:26:28 +0200 | [diff] [blame] | 2377 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2378 | while( ext_len ) | 
|  | 2379 | { | 
|  | 2380 | unsigned int ext_id   = ( ( ext[0] <<  8 ) | 
|  | 2381 | | ( ext[1]       ) ); | 
|  | 2382 | unsigned int ext_size = ( ( ext[2] <<  8 ) | 
|  | 2383 | | ( ext[3]       ) ); | 
|  | 2384 |  | 
|  | 2385 | if( ext_size + 4 > ext_len ) | 
|  | 2386 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2387 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2388 | mbedtls_ssl_send_alert_message( | 
|  | 2389 | ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 2390 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2391 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2392 | } | 
|  | 2393 |  | 
|  | 2394 | switch( ext_id ) | 
|  | 2395 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2396 | case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO: | 
|  | 2397 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) ); | 
|  | 2398 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2399 | renegotiation_info_seen = 1; | 
| Manuel Pégourié-Gonnard | eaecbd3 | 2014-11-06 02:38:02 +0100 | [diff] [blame] | 2400 | #endif | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2401 |  | 
| Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 2402 | if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4, | 
|  | 2403 | ext_size ) ) != 0 ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2404 | return( ret ); | 
|  | 2405 |  | 
|  | 2406 | break; | 
|  | 2407 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2408 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
|  | 2409 | case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH: | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2410 | MBEDTLS_SSL_DEBUG_MSG( 3, | 
|  | 2411 | ( "found max_fragment_length extension" ) ); | 
| Manuel Pégourié-Gonnard | de600e5 | 2013-07-17 10:14:38 +0200 | [diff] [blame] | 2412 |  | 
|  | 2413 | if( ( ret = ssl_parse_max_fragment_length_ext( ssl, | 
|  | 2414 | ext + 4, ext_size ) ) != 0 ) | 
|  | 2415 | { | 
|  | 2416 | return( ret ); | 
|  | 2417 | } | 
|  | 2418 |  | 
|  | 2419 | break; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2420 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ | 
| Manuel Pégourié-Gonnard | de600e5 | 2013-07-17 10:14:38 +0200 | [diff] [blame] | 2421 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2422 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) | 
|  | 2423 | case MBEDTLS_TLS_EXT_TRUNCATED_HMAC: | 
|  | 2424 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found truncated_hmac extension" ) ); | 
| Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 2425 |  | 
|  | 2426 | if( ( ret = ssl_parse_truncated_hmac_ext( ssl, | 
|  | 2427 | ext + 4, ext_size ) ) != 0 ) | 
|  | 2428 | { | 
|  | 2429 | return( ret ); | 
|  | 2430 | } | 
|  | 2431 |  | 
|  | 2432 | break; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2433 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ | 
| Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 2434 |  | 
| Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2435 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
| Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 2436 | case MBEDTLS_TLS_EXT_CID: | 
|  | 2437 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found CID extension" ) ); | 
|  | 2438 |  | 
|  | 2439 | if( ( ret = ssl_parse_cid_ext( ssl, | 
|  | 2440 | ext + 4, | 
|  | 2441 | ext_size ) ) != 0 ) | 
|  | 2442 | { | 
|  | 2443 | return( ret ); | 
|  | 2444 | } | 
|  | 2445 |  | 
|  | 2446 | break; | 
| Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2447 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
| Hanno Becker | a8373a1 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 2448 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2449 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
|  | 2450 | case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC: | 
|  | 2451 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt_then_mac extension" ) ); | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2452 |  | 
|  | 2453 | if( ( ret = ssl_parse_encrypt_then_mac_ext( ssl, | 
|  | 2454 | ext + 4, ext_size ) ) != 0 ) | 
|  | 2455 | { | 
|  | 2456 | return( ret ); | 
|  | 2457 | } | 
|  | 2458 |  | 
|  | 2459 | break; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2460 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2461 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2462 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) | 
|  | 2463 | case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET: | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2464 | MBEDTLS_SSL_DEBUG_MSG( 3, | 
|  | 2465 | ( "found extended_master_secret extension" ) ); | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2466 |  | 
|  | 2467 | if( ( ret = ssl_parse_extended_ms_ext( ssl, | 
|  | 2468 | ext + 4, ext_size ) ) != 0 ) | 
|  | 2469 | { | 
|  | 2470 | return( ret ); | 
|  | 2471 | } | 
|  | 2472 |  | 
|  | 2473 | break; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2474 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2475 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2476 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | 
|  | 2477 | case MBEDTLS_TLS_EXT_SESSION_TICKET: | 
|  | 2478 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) ); | 
| Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 2479 |  | 
|  | 2480 | if( ( ret = ssl_parse_session_ticket_ext( ssl, | 
|  | 2481 | ext + 4, ext_size ) ) != 0 ) | 
|  | 2482 | { | 
|  | 2483 | return( ret ); | 
|  | 2484 | } | 
|  | 2485 |  | 
|  | 2486 | break; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2487 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ | 
| Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 2488 |  | 
| Robert Cragie | 136884c | 2015-10-02 13:34:31 +0100 | [diff] [blame] | 2489 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ | 
| Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 2490 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2491 | case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS: | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2492 | MBEDTLS_SSL_DEBUG_MSG( 3, | 
|  | 2493 | ( "found supported_point_formats extension" ) ); | 
| Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 2494 |  | 
|  | 2495 | if( ( ret = ssl_parse_supported_point_formats_ext( ssl, | 
|  | 2496 | ext + 4, ext_size ) ) != 0 ) | 
|  | 2497 | { | 
|  | 2498 | return( ret ); | 
|  | 2499 | } | 
|  | 2500 |  | 
|  | 2501 | break; | 
| Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 2502 | #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || | 
|  | 2503 | MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ | 
| Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 2504 |  | 
| Manuel Pégourié-Gonnard | 0a1324a | 2015-09-16 16:01:00 +0200 | [diff] [blame] | 2505 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
|  | 2506 | case MBEDTLS_TLS_EXT_ECJPAKE_KKPP: | 
|  | 2507 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake_kkpp extension" ) ); | 
|  | 2508 |  | 
|  | 2509 | if( ( ret = ssl_parse_ecjpake_kkpp( ssl, | 
|  | 2510 | ext + 4, ext_size ) ) != 0 ) | 
|  | 2511 | { | 
|  | 2512 | return( ret ); | 
|  | 2513 | } | 
|  | 2514 |  | 
|  | 2515 | break; | 
|  | 2516 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ | 
| Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2517 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2518 | #if defined(MBEDTLS_SSL_ALPN) | 
|  | 2519 | case MBEDTLS_TLS_EXT_ALPN: | 
|  | 2520 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) ); | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2521 |  | 
| Johan Pascal | 275874b | 2020-10-27 10:43:53 +0100 | [diff] [blame] | 2522 | if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 ) | 
|  | 2523 | return( ret ); | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2524 |  | 
|  | 2525 | break; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2526 | #endif /* MBEDTLS_SSL_ALPN */ | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2527 |  | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2528 | #if defined(MBEDTLS_SSL_DTLS_SRTP) | 
|  | 2529 | case MBEDTLS_TLS_EXT_USE_SRTP: | 
|  | 2530 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found use_srtp extension" ) ); | 
|  | 2531 |  | 
| Johan Pascal | c3ccd98 | 2020-10-28 17:18:18 +0100 | [diff] [blame] | 2532 | if( ( ret = ssl_parse_use_srtp_ext( ssl, ext + 4, ext_size ) ) != 0 ) | 
|  | 2533 | return( ret ); | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2534 |  | 
|  | 2535 | break; | 
|  | 2536 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ | 
|  | 2537 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2538 | default: | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2539 | MBEDTLS_SSL_DEBUG_MSG( 3, | 
|  | 2540 | ( "unknown extension found: %d (ignoring)", ext_id ) ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2541 | } | 
|  | 2542 |  | 
|  | 2543 | ext_len -= 4 + ext_size; | 
|  | 2544 | ext += 4 + ext_size; | 
|  | 2545 |  | 
|  | 2546 | if( ext_len > 0 && ext_len < 4 ) | 
|  | 2547 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2548 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); | 
|  | 2549 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2550 | } | 
|  | 2551 | } | 
|  | 2552 |  | 
|  | 2553 | /* | 
|  | 2554 | * Renegotiation security checks | 
|  | 2555 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2556 | if( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2557 | ssl->conf->allow_legacy_renegotiation == | 
|  | 2558 | MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2559 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2560 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 2561 | ( "legacy renegotiation, breaking off handshake" ) ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2562 | handshake_failure = 1; | 
|  | 2563 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2564 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 2565 | else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && | 
|  | 2566 | ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION && | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2567 | renegotiation_info_seen == 0 ) | 
|  | 2568 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2569 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 2570 | ( "renegotiation_info extension missing (secure)" ) ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2571 | handshake_failure = 1; | 
|  | 2572 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2573 | else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && | 
|  | 2574 | ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2575 | ssl->conf->allow_legacy_renegotiation == | 
|  | 2576 | MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2577 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2578 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) ); | 
| Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2579 | handshake_failure = 1; | 
|  | 2580 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2581 | else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && | 
|  | 2582 | ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && | 
| Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2583 | renegotiation_info_seen == 1 ) | 
|  | 2584 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2585 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 2586 | ( "renegotiation_info extension present (legacy)" ) ); | 
| Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2587 | handshake_failure = 1; | 
|  | 2588 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2589 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ | 
| Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2590 |  | 
|  | 2591 | if( handshake_failure == 1 ) | 
|  | 2592 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2593 | mbedtls_ssl_send_alert_message( | 
|  | 2594 | ssl, | 
|  | 2595 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 2596 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2597 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2598 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2599 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2600 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2601 |  | 
|  | 2602 | return( 0 ); | 
|  | 2603 | } | 
|  | 2604 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2605 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) ||                       \ | 
|  | 2606 | defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2607 | static int ssl_parse_server_dh_params( mbedtls_ssl_context *ssl, | 
|  | 2608 | unsigned char **p, | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2609 | unsigned char *end ) | 
|  | 2610 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2611 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2612 |  | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2613 | /* | 
|  | 2614 | * Ephemeral DH parameters: | 
|  | 2615 | * | 
|  | 2616 | * struct { | 
|  | 2617 | *     opaque dh_p<1..2^16-1>; | 
|  | 2618 | *     opaque dh_g<1..2^16-1>; | 
|  | 2619 | *     opaque dh_Ys<1..2^16-1>; | 
|  | 2620 | * } ServerDHParams; | 
|  | 2621 | */ | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2622 | if( ( ret = mbedtls_dhm_read_params( &ssl->handshake->dhm_ctx, | 
|  | 2623 | p, end ) ) != 0 ) | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2624 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2625 | MBEDTLS_SSL_DEBUG_RET( 2, ( "mbedtls_dhm_read_params" ), ret ); | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2626 | return( ret ); | 
|  | 2627 | } | 
|  | 2628 |  | 
| Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 2629 | if( ssl->handshake->dhm_ctx.len * 8 < ssl->conf->dhm_min_bitlen ) | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2630 | { | 
| Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 2631 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DHM prime too short: %d < %d", | 
|  | 2632 | ssl->handshake->dhm_ctx.len * 8, | 
|  | 2633 | ssl->conf->dhm_min_bitlen ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2634 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2635 | } | 
|  | 2636 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2637 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P  ); | 
|  | 2638 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G  ); | 
|  | 2639 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY ); | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2640 |  | 
|  | 2641 | return( ret ); | 
|  | 2642 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2643 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED || | 
|  | 2644 | MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2645 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2646 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) ||                     \ | 
|  | 2647 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) ||                   \ | 
|  | 2648 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) ||                     \ | 
|  | 2649 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||                      \ | 
|  | 2650 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) | 
|  | 2651 | 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] | 2652 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2653 | const mbedtls_ecp_curve_info *curve_info; | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 2654 | mbedtls_ecp_group_id grp_id; | 
|  | 2655 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) | 
|  | 2656 | grp_id = ssl->handshake->ecdh_ctx.grp.id; | 
|  | 2657 | #else | 
|  | 2658 | grp_id = ssl->handshake->ecdh_ctx.grp_id; | 
|  | 2659 | #endif | 
| Manuel Pégourié-Gonnard | c3f6b62c | 2014-02-06 10:13:09 +0100 | [diff] [blame] | 2660 |  | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 2661 | 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] | 2662 | if( curve_info == NULL ) | 
|  | 2663 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2664 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 2665 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | c3f6b62c | 2014-02-06 10:13:09 +0100 | [diff] [blame] | 2666 | } | 
|  | 2667 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2668 | 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] | 2669 |  | 
| Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 2670 | #if defined(MBEDTLS_ECP_C) | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 2671 | if( mbedtls_ssl_check_curve( ssl, grp_id ) != 0 ) | 
| Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 2672 | #else | 
| Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2673 | if( ssl->handshake->ecdh_ctx.grp.nbits < 163 || | 
|  | 2674 | ssl->handshake->ecdh_ctx.grp.nbits > 521 ) | 
| Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 2675 | #endif | 
| Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2676 | return( -1 ); | 
| Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2677 |  | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 2678 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, | 
|  | 2679 | MBEDTLS_DEBUG_ECDH_QP ); | 
| Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2680 |  | 
|  | 2681 | return( 0 ); | 
|  | 2682 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2683 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || | 
|  | 2684 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED || | 
|  | 2685 | MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED || | 
|  | 2686 | MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED || | 
|  | 2687 | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */ | 
| Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2688 |  | 
| Hanno Becker | bb89e27 | 2019-01-08 12:54:37 +0000 | [diff] [blame] | 2689 | #if defined(MBEDTLS_USE_PSA_CRYPTO) &&                           \ | 
|  | 2690 | ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) ||     \ | 
|  | 2691 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) ) | 
|  | 2692 | static int ssl_parse_server_ecdh_params_psa( mbedtls_ssl_context *ssl, | 
|  | 2693 | unsigned char **p, | 
|  | 2694 | unsigned char *end ) | 
|  | 2695 | { | 
|  | 2696 | uint16_t tls_id; | 
| Gilles Peskine | 4245980 | 2019-12-19 13:31:53 +0100 | [diff] [blame] | 2697 | size_t ecdh_bits = 0; | 
| Hanno Becker | bb89e27 | 2019-01-08 12:54:37 +0000 | [diff] [blame] | 2698 | uint8_t ecpoint_len; | 
|  | 2699 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; | 
|  | 2700 |  | 
|  | 2701 | /* | 
|  | 2702 | * Parse ECC group | 
|  | 2703 | */ | 
|  | 2704 |  | 
|  | 2705 | if( end - *p < 4 ) | 
|  | 2706 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); | 
|  | 2707 |  | 
|  | 2708 | /* First byte is curve_type; only named_curve is handled */ | 
|  | 2709 | if( *(*p)++ != MBEDTLS_ECP_TLS_NAMED_CURVE ) | 
|  | 2710 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); | 
|  | 2711 |  | 
|  | 2712 | /* Next two bytes are the namedcurve value */ | 
|  | 2713 | tls_id = *(*p)++; | 
|  | 2714 | tls_id <<= 8; | 
|  | 2715 | tls_id |= *(*p)++; | 
|  | 2716 |  | 
|  | 2717 | /* Convert EC group to PSA key type. */ | 
| Gilles Peskine | 4245980 | 2019-12-19 13:31:53 +0100 | [diff] [blame] | 2718 | if( ( handshake->ecdh_psa_type = | 
|  | 2719 | mbedtls_psa_parse_tls_ecc_group( tls_id, &ecdh_bits ) ) == 0 ) | 
| Hanno Becker | bb89e27 | 2019-01-08 12:54:37 +0000 | [diff] [blame] | 2720 | { | 
|  | 2721 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); | 
|  | 2722 | } | 
| Gilles Peskine | 4245980 | 2019-12-19 13:31:53 +0100 | [diff] [blame] | 2723 | if( ecdh_bits > 0xffff ) | 
|  | 2724 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); | 
|  | 2725 | handshake->ecdh_bits = (uint16_t) ecdh_bits; | 
| Hanno Becker | bb89e27 | 2019-01-08 12:54:37 +0000 | [diff] [blame] | 2726 |  | 
|  | 2727 | /* | 
|  | 2728 | * Put peer's ECDH public key in the format understood by PSA. | 
|  | 2729 | */ | 
|  | 2730 |  | 
|  | 2731 | ecpoint_len = *(*p)++; | 
|  | 2732 | if( (size_t)( end - *p ) < ecpoint_len ) | 
|  | 2733 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); | 
|  | 2734 |  | 
| Gilles Peskine | 4245980 | 2019-12-19 13:31:53 +0100 | [diff] [blame] | 2735 | if( mbedtls_psa_tls_ecpoint_to_psa_ec( | 
| Hanno Becker | bb89e27 | 2019-01-08 12:54:37 +0000 | [diff] [blame] | 2736 | *p, ecpoint_len, | 
|  | 2737 | handshake->ecdh_psa_peerkey, | 
|  | 2738 | sizeof( handshake->ecdh_psa_peerkey ), | 
|  | 2739 | &handshake->ecdh_psa_peerkey_len ) != 0 ) | 
|  | 2740 | { | 
|  | 2741 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
|  | 2742 | } | 
|  | 2743 |  | 
|  | 2744 | *p += ecpoint_len; | 
|  | 2745 | return( 0 ); | 
|  | 2746 | } | 
|  | 2747 | #endif /* MBEDTLS_USE_PSA_CRYPTO && | 
|  | 2748 | ( MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || | 
|  | 2749 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ) */ | 
|  | 2750 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2751 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) ||                     \ | 
|  | 2752 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) ||                   \ | 
|  | 2753 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) | 
|  | 2754 | static int ssl_parse_server_ecdh_params( mbedtls_ssl_context *ssl, | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2755 | unsigned char **p, | 
|  | 2756 | unsigned char *end ) | 
|  | 2757 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2758 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2759 |  | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2760 | /* | 
|  | 2761 | * Ephemeral ECDH parameters: | 
|  | 2762 | * | 
|  | 2763 | * struct { | 
|  | 2764 | *     ECParameters curve_params; | 
|  | 2765 | *     ECPoint      public; | 
|  | 2766 | * } ServerECDHParams; | 
|  | 2767 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2768 | if( ( ret = mbedtls_ecdh_read_params( &ssl->handshake->ecdh_ctx, | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2769 | (const unsigned char **) p, end ) ) != 0 ) | 
|  | 2770 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2771 | MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_read_params" ), ret ); | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2772 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
| Manuel Pégourié-Gonnard | 1c1c20e | 2018-09-12 10:34:43 +0200 | [diff] [blame] | 2773 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) | 
|  | 2774 | ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS; | 
| Manuel Pégourié-Gonnard | 558da9c | 2018-06-13 12:02:12 +0200 | [diff] [blame] | 2775 | #endif | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2776 | return( ret ); | 
|  | 2777 | } | 
|  | 2778 |  | 
| Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2779 | if( ssl_check_server_ecdh_params( ssl ) != 0 ) | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2780 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2781 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 2782 | ( "bad server key exchange message (ECDHE curve)" ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2783 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2784 | } | 
|  | 2785 |  | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2786 | return( ret ); | 
|  | 2787 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2788 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || | 
|  | 2789 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED || | 
|  | 2790 | MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2791 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2792 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2793 | static int ssl_parse_server_psk_hint( mbedtls_ssl_context *ssl, | 
| Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2794 | unsigned char **p, | 
|  | 2795 | unsigned char *end ) | 
|  | 2796 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2797 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; | 
| irwir | 6527bd6 | 2019-09-21 18:51:25 +0300 | [diff] [blame] | 2798 | uint16_t  len; | 
| Paul Bakker | c5a79cc | 2013-06-26 15:08:35 +0200 | [diff] [blame] | 2799 | ((void) ssl); | 
| Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2800 |  | 
|  | 2801 | /* | 
|  | 2802 | * PSK parameters: | 
|  | 2803 | * | 
|  | 2804 | * opaque psk_identity_hint<0..2^16-1>; | 
|  | 2805 | */ | 
| Hanno Becker | 0c161d1 | 2018-10-08 13:40:50 +0100 | [diff] [blame] | 2806 | if( end - (*p) < 2 ) | 
| Krzysztof Stachowiak | 740b218 | 2018-03-13 11:31:14 +0100 | [diff] [blame] | 2807 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2808 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 2809 | ( "bad server key exchange message (psk_identity_hint length)" ) ); | 
| Krzysztof Stachowiak | 740b218 | 2018-03-13 11:31:14 +0100 | [diff] [blame] | 2810 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); | 
|  | 2811 | } | 
| Manuel Pégourié-Gonnard | 59b9fe2 | 2013-10-15 11:55:33 +0200 | [diff] [blame] | 2812 | len = (*p)[0] << 8 | (*p)[1]; | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2813 | *p += 2; | 
| Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2814 |  | 
| irwir | 6527bd6 | 2019-09-21 18:51:25 +0300 | [diff] [blame] | 2815 | if( end - (*p) < len ) | 
| Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2816 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2817 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 2818 | ( "bad server key exchange message (psk_identity_hint length)" ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2819 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); | 
| Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2820 | } | 
|  | 2821 |  | 
| Manuel Pégourié-Gonnard | 9d62412 | 2016-02-22 11:10:14 +0100 | [diff] [blame] | 2822 | /* | 
|  | 2823 | * Note: we currently ignore the PKS identity hint, as we only allow one | 
|  | 2824 | * PSK to be provisionned on the client. This could be changed later if | 
|  | 2825 | * someone needs that feature. | 
|  | 2826 | */ | 
| Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2827 | *p += len; | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2828 | ret = 0; | 
| Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2829 |  | 
|  | 2830 | return( ret ); | 
|  | 2831 | } | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2832 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ | 
| Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2833 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2834 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) ||                           \ | 
|  | 2835 | defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2836 | /* | 
|  | 2837 | * Generate a pre-master secret and encrypt it with the server's RSA key | 
|  | 2838 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2839 | static int ssl_write_encrypted_pms( mbedtls_ssl_context *ssl, | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2840 | size_t offset, size_t *olen, | 
|  | 2841 | size_t pms_offset ) | 
|  | 2842 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2843 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2844 | 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] | 2845 | unsigned char *p = ssl->handshake->premaster + pms_offset; | 
| Hanno Becker | c7d7e29 | 2019-02-06 16:49:54 +0000 | [diff] [blame] | 2846 | mbedtls_pk_context * peer_pk; | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2847 |  | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2848 | if( offset + len_bytes > MBEDTLS_SSL_OUT_CONTENT_LEN ) | 
| Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 2849 | { | 
|  | 2850 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small for encrypted pms" ) ); | 
|  | 2851 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); | 
|  | 2852 | } | 
|  | 2853 |  | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2854 | /* | 
|  | 2855 | * Generate (part of) the pre-master as | 
|  | 2856 | *  struct { | 
|  | 2857 | *      ProtocolVersion client_version; | 
|  | 2858 | *      opaque random[46]; | 
|  | 2859 | *  } PreMasterSecret; | 
|  | 2860 | */ | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2861 | mbedtls_ssl_write_version( ssl->conf->max_major_ver, | 
|  | 2862 | ssl->conf->max_minor_ver, | 
|  | 2863 | ssl->conf->transport, p ); | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2864 |  | 
| Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 2865 | 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] | 2866 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2867 | MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret ); | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2868 | return( ret ); | 
|  | 2869 | } | 
|  | 2870 |  | 
|  | 2871 | ssl->handshake->pmslen = 48; | 
|  | 2872 |  | 
| Hanno Becker | c7d7e29 | 2019-02-06 16:49:54 +0000 | [diff] [blame] | 2873 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 2874 | peer_pk = &ssl->handshake->peer_pubkey; | 
|  | 2875 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
| Manuel Pégourié-Gonnard | 7f2f062 | 2015-09-03 10:44:32 +0200 | [diff] [blame] | 2876 | if( ssl->session_negotiate->peer_cert == NULL ) | 
|  | 2877 | { | 
| Hanno Becker | 8273df8 | 2019-02-06 17:37:32 +0000 | [diff] [blame] | 2878 | /* Should never happen */ | 
| Hanno Becker | 62d58ed | 2019-02-26 11:51:06 +0000 | [diff] [blame] | 2879 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
| Hanno Becker | 8273df8 | 2019-02-06 17:37:32 +0000 | [diff] [blame] | 2880 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | 7f2f062 | 2015-09-03 10:44:32 +0200 | [diff] [blame] | 2881 | } | 
| Hanno Becker | c7d7e29 | 2019-02-06 16:49:54 +0000 | [diff] [blame] | 2882 | peer_pk = &ssl->session_negotiate->peer_cert->pk; | 
|  | 2883 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
| Manuel Pégourié-Gonnard | 7f2f062 | 2015-09-03 10:44:32 +0200 | [diff] [blame] | 2884 |  | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2885 | /* | 
|  | 2886 | * Now write it out, encrypted | 
|  | 2887 | */ | 
| Hanno Becker | c7d7e29 | 2019-02-06 16:49:54 +0000 | [diff] [blame] | 2888 | if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_RSA ) ) | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2889 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2890 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) ); | 
|  | 2891 | return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH ); | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2892 | } | 
|  | 2893 |  | 
| Hanno Becker | c7d7e29 | 2019-02-06 16:49:54 +0000 | [diff] [blame] | 2894 | if( ( ret = mbedtls_pk_encrypt( peer_pk, | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2895 | p, ssl->handshake->pmslen, | 
|  | 2896 | ssl->out_msg + offset + len_bytes, olen, | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2897 | MBEDTLS_SSL_OUT_CONTENT_LEN - offset - len_bytes, | 
| Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 2898 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2899 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2900 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_rsa_pkcs1_encrypt", ret ); | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2901 | return( ret ); | 
|  | 2902 | } | 
|  | 2903 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2904 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ | 
|  | 2905 | defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2906 | if( len_bytes == 2 ) | 
|  | 2907 | { | 
|  | 2908 | ssl->out_msg[offset+0] = (unsigned char)( *olen >> 8 ); | 
|  | 2909 | ssl->out_msg[offset+1] = (unsigned char)( *olen      ); | 
|  | 2910 | *olen += 2; | 
|  | 2911 | } | 
|  | 2912 | #endif | 
|  | 2913 |  | 
| Hanno Becker | ae553dd | 2019-02-08 14:06:00 +0000 | [diff] [blame] | 2914 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 2915 | /* We don't need the peer's public key anymore. Free it. */ | 
|  | 2916 | mbedtls_pk_free( peer_pk ); | 
|  | 2917 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2918 | return( 0 ); | 
|  | 2919 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2920 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED || | 
|  | 2921 | MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2922 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2923 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Manuel Pégourié-Gonnard | 5c2a7ca | 2015-10-23 08:48:41 +0200 | [diff] [blame] | 2924 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) ||                       \ | 
|  | 2925 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) ||                     \ | 
|  | 2926 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2927 | static int ssl_parse_signature_algorithm( mbedtls_ssl_context *ssl, | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2928 | unsigned char **p, | 
|  | 2929 | unsigned char *end, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2930 | mbedtls_md_type_t *md_alg, | 
|  | 2931 | mbedtls_pk_type_t *pk_alg ) | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2932 | { | 
| Paul Bakker | c5a79cc | 2013-06-26 15:08:35 +0200 | [diff] [blame] | 2933 | ((void) ssl); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2934 | *md_alg = MBEDTLS_MD_NONE; | 
|  | 2935 | *pk_alg = MBEDTLS_PK_NONE; | 
| Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 2936 |  | 
|  | 2937 | /* Only in TLS 1.2 */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2938 | if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 ) | 
| Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 2939 | { | 
| Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 2940 | return( 0 ); | 
|  | 2941 | } | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2942 |  | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2943 | if( (*p) + 2 > end ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2944 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2945 |  | 
| Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 2946 | /* | 
|  | 2947 | * Get hash algorithm | 
|  | 2948 | */ | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2949 | if( ( *md_alg = mbedtls_ssl_md_alg_from_hash( (*p)[0] ) ) | 
|  | 2950 | == MBEDTLS_MD_NONE ) | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2951 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2952 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 2953 | ( "Server used unsupported HashAlgorithm %d", *(p)[0] ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2954 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2955 | } | 
|  | 2956 |  | 
| Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 2957 | /* | 
| Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 2958 | * Get signature algorithm | 
|  | 2959 | */ | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2960 | if( ( *pk_alg = mbedtls_ssl_pk_alg_from_sig( (*p)[1] ) ) | 
|  | 2961 | == MBEDTLS_PK_NONE ) | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2962 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2963 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 2964 | ( "server used unsupported SignatureAlgorithm %d", (*p)[1] ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2965 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2966 | } | 
|  | 2967 |  | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 2968 | /* | 
|  | 2969 | * Check if the hash is acceptable | 
|  | 2970 | */ | 
|  | 2971 | if( mbedtls_ssl_check_sig_hash( ssl, *md_alg ) != 0 ) | 
|  | 2972 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2973 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 2974 | ( "server used HashAlgorithm %d that was not offered", *(p)[0] ) ); | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 2975 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); | 
|  | 2976 | } | 
|  | 2977 |  | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 2978 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d", | 
|  | 2979 | (*p)[1] ) ); | 
|  | 2980 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d", | 
|  | 2981 | (*p)[0] ) ); | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2982 | *p += 2; | 
|  | 2983 |  | 
|  | 2984 | return( 0 ); | 
|  | 2985 | } | 
| Manuel Pégourié-Gonnard | 5c2a7ca | 2015-10-23 08:48:41 +0200 | [diff] [blame] | 2986 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED || | 
|  | 2987 | MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || | 
|  | 2988 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2989 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2990 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2991 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ | 
|  | 2992 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) | 
|  | 2993 | 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] | 2994 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2995 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2996 | const mbedtls_ecp_keypair *peer_key; | 
| Hanno Becker | be7f508 | 2019-02-06 17:44:07 +0000 | [diff] [blame] | 2997 | mbedtls_pk_context * peer_pk; | 
| Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2998 |  | 
| Hanno Becker | be7f508 | 2019-02-06 17:44:07 +0000 | [diff] [blame] | 2999 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 3000 | peer_pk = &ssl->handshake->peer_pubkey; | 
|  | 3001 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
| Manuel Pégourié-Gonnard | 7f2f062 | 2015-09-03 10:44:32 +0200 | [diff] [blame] | 3002 | if( ssl->session_negotiate->peer_cert == NULL ) | 
|  | 3003 | { | 
| Hanno Becker | 8273df8 | 2019-02-06 17:37:32 +0000 | [diff] [blame] | 3004 | /* Should never happen */ | 
| Hanno Becker | bd5580a | 2019-02-26 12:36:01 +0000 | [diff] [blame] | 3005 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
| Hanno Becker | 8273df8 | 2019-02-06 17:37:32 +0000 | [diff] [blame] | 3006 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | 7f2f062 | 2015-09-03 10:44:32 +0200 | [diff] [blame] | 3007 | } | 
| Hanno Becker | be7f508 | 2019-02-06 17:44:07 +0000 | [diff] [blame] | 3008 | peer_pk = &ssl->session_negotiate->peer_cert->pk; | 
|  | 3009 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
| Manuel Pégourié-Gonnard | 7f2f062 | 2015-09-03 10:44:32 +0200 | [diff] [blame] | 3010 |  | 
| Hanno Becker | be7f508 | 2019-02-06 17:44:07 +0000 | [diff] [blame] | 3011 | if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_ECKEY ) ) | 
| Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 3012 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3013 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) ); | 
|  | 3014 | return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH ); | 
| Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 3015 | } | 
|  | 3016 |  | 
| Hanno Becker | be7f508 | 2019-02-06 17:44:07 +0000 | [diff] [blame] | 3017 | peer_key = mbedtls_pk_ec( *peer_pk ); | 
| Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 3018 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3019 | if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key, | 
|  | 3020 | MBEDTLS_ECDH_THEIRS ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 3021 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3022 | MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret ); | 
| Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 3023 | return( ret ); | 
|  | 3024 | } | 
|  | 3025 |  | 
|  | 3026 | if( ssl_check_server_ecdh_params( ssl ) != 0 ) | 
|  | 3027 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3028 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) ); | 
|  | 3029 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); | 
| Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 3030 | } | 
|  | 3031 |  | 
| Hanno Becker | ae553dd | 2019-02-08 14:06:00 +0000 | [diff] [blame] | 3032 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 3033 | /* We don't need the peer's public key anymore. Free it, | 
|  | 3034 | * so that more RAM is available for upcoming expensive | 
|  | 3035 | * operations like ECDHE. */ | 
|  | 3036 | mbedtls_pk_free( peer_pk ); | 
|  | 3037 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 3038 |  | 
| Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 3039 | return( ret ); | 
|  | 3040 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3041 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || | 
|  | 3042 | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */ | 
| Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 3043 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3044 | static int ssl_parse_server_key_exchange( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3045 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3046 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3047 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | 
| Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 3048 | ssl->handshake->ciphersuite_info; | 
| Andres Amaya Garcia | 53c77cc | 2017-06-27 16:15:06 +0100 | [diff] [blame] | 3049 | unsigned char *p = NULL, *end = NULL; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3050 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3051 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3052 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3053 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) | 
|  | 3054 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3055 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3056 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3057 | ssl->state++; | 
|  | 3058 | return( 0 ); | 
|  | 3059 | } | 
| Manuel Pégourié-Gonnard | bac0e3b | 2013-10-15 11:54:47 +0200 | [diff] [blame] | 3060 | ((void) p); | 
|  | 3061 | ((void) end); | 
|  | 3062 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3063 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3064 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ | 
|  | 3065 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) | 
|  | 3066 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA || | 
|  | 3067 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA ) | 
| Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 3068 | { | 
| Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 3069 | if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 ) | 
|  | 3070 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3071 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3072 | mbedtls_ssl_send_alert_message( | 
|  | 3073 | ssl, | 
|  | 3074 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 3075 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 3076 | return( ret ); | 
|  | 3077 | } | 
| Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 3078 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3079 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) ); | 
| Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 3080 | ssl->state++; | 
|  | 3081 | return( 0 ); | 
|  | 3082 | } | 
|  | 3083 | ((void) p); | 
|  | 3084 | ((void) end); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3085 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED || | 
|  | 3086 | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */ | 
| Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 3087 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3088 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
| Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 3089 | if( ssl->handshake->ecrs_enabled && | 
| Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 3090 | ssl->handshake->ecrs_state == ssl_ecrs_ske_start_processing ) | 
| Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 3091 | { | 
| Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 3092 | goto start_processing; | 
| Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 3093 | } | 
| Manuel Pégourié-Gonnard | 1f1f2a1 | 2017-05-18 11:27:06 +0200 | [diff] [blame] | 3094 | #endif | 
|  | 3095 |  | 
| Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 3096 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3097 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3098 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3099 | return( ret ); | 
|  | 3100 | } | 
|  | 3101 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3102 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3103 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3104 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3105 | mbedtls_ssl_send_alert_message( | 
|  | 3106 | ssl, | 
|  | 3107 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 3108 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3109 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3110 | } | 
|  | 3111 |  | 
| Manuel Pégourié-Gonnard | 09258b9 | 2013-10-15 10:43:36 +0200 | [diff] [blame] | 3112 | /* | 
|  | 3113 | * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server | 
|  | 3114 | * doesn't use a psk_identity_hint | 
|  | 3115 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3116 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3117 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3118 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || | 
|  | 3119 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) | 
| Paul Bakker | 188c8de | 2013-04-19 09:13:37 +0200 | [diff] [blame] | 3120 | { | 
| Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3121 | /* Current message is probably either | 
|  | 3122 | * CertificateRequest or ServerHelloDone */ | 
|  | 3123 | ssl->keep_current_message = 1; | 
| Paul Bakker | 188c8de | 2013-04-19 09:13:37 +0200 | [diff] [blame] | 3124 | goto exit; | 
|  | 3125 | } | 
|  | 3126 |  | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3127 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 3128 | ( "server key exchange message must not be skipped" ) ); | 
|  | 3129 | mbedtls_ssl_send_alert_message( | 
|  | 3130 | ssl, | 
|  | 3131 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 3132 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); | 
| Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3133 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3134 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3135 | } | 
|  | 3136 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3137 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
| Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 3138 | if( ssl->handshake->ecrs_enabled ) | 
|  | 3139 | ssl->handshake->ecrs_state = ssl_ecrs_ske_start_processing; | 
|  | 3140 |  | 
|  | 3141 | start_processing: | 
|  | 3142 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3143 | p   = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ); | 
| Paul Bakker | 3b6a07b | 2013-03-21 11:56:50 +0100 | [diff] [blame] | 3144 | end = ssl->in_msg + ssl->in_hslen; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3145 | MBEDTLS_SSL_DEBUG_BUF( 3,   "server key exchange", p, end - p ); | 
| Paul Bakker | 3b6a07b | 2013-03-21 11:56:50 +0100 | [diff] [blame] | 3146 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3147 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3148 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || | 
|  | 3149 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK || | 
|  | 3150 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK || | 
|  | 3151 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) | 
| Manuel Pégourié-Gonnard | 09258b9 | 2013-10-15 10:43:36 +0200 | [diff] [blame] | 3152 | { | 
|  | 3153 | if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 ) | 
|  | 3154 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3155 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3156 | mbedtls_ssl_send_alert_message( | 
|  | 3157 | ssl, | 
|  | 3158 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 3159 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3160 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); | 
| Manuel Pégourié-Gonnard | 09258b9 | 2013-10-15 10:43:36 +0200 | [diff] [blame] | 3161 | } | 
|  | 3162 | } /* FALLTROUGH */ | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3163 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ | 
| Manuel Pégourié-Gonnard | 09258b9 | 2013-10-15 10:43:36 +0200 | [diff] [blame] | 3164 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3165 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) ||                       \ | 
|  | 3166 | defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) | 
|  | 3167 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || | 
|  | 3168 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) | 
| Manuel Pégourié-Gonnard | 09258b9 | 2013-10-15 10:43:36 +0200 | [diff] [blame] | 3169 | ; /* nothing more to do */ | 
|  | 3170 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3171 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED || | 
|  | 3172 | MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ | 
|  | 3173 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) ||                       \ | 
|  | 3174 | defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) | 
|  | 3175 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA || | 
|  | 3176 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3177 | { | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 3178 | if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 ) | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3179 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3180 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3181 | mbedtls_ssl_send_alert_message( | 
|  | 3182 | ssl, | 
|  | 3183 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 3184 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3185 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); | 
| Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 3186 | } | 
|  | 3187 | } | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3188 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3189 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED || | 
|  | 3190 | MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ | 
| Hanno Becker | bb89e27 | 2019-01-08 12:54:37 +0000 | [diff] [blame] | 3191 | #if defined(MBEDTLS_USE_PSA_CRYPTO) &&                           \ | 
|  | 3192 | ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) ||     \ | 
|  | 3193 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) ) | 
|  | 3194 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA || | 
|  | 3195 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ) | 
|  | 3196 | { | 
|  | 3197 | if( ssl_parse_server_ecdh_params_psa( ssl, &p, end ) != 0 ) | 
|  | 3198 | { | 
|  | 3199 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3200 | mbedtls_ssl_send_alert_message( | 
|  | 3201 | ssl, | 
|  | 3202 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 3203 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); | 
| Hanno Becker | bb89e27 | 2019-01-08 12:54:37 +0000 | [diff] [blame] | 3204 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); | 
|  | 3205 | } | 
|  | 3206 | } | 
|  | 3207 | else | 
|  | 3208 | #endif /* MBEDTLS_USE_PSA_CRYPTO && | 
|  | 3209 | ( MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || | 
|  | 3210 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ) */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3211 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) ||                     \ | 
|  | 3212 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) ||                     \ | 
|  | 3213 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) | 
|  | 3214 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA || | 
|  | 3215 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK || | 
|  | 3216 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ) | 
| Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 3217 | { | 
|  | 3218 | if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 ) | 
|  | 3219 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3220 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3221 | mbedtls_ssl_send_alert_message( | 
|  | 3222 | ssl, | 
|  | 3223 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 3224 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3225 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3226 | } | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3227 | } | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3228 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3229 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || | 
|  | 3230 | MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED || | 
|  | 3231 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */ | 
| Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 3232 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
|  | 3233 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ) | 
|  | 3234 | { | 
|  | 3235 | ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx, | 
|  | 3236 | p, end - p ); | 
|  | 3237 | if( ret != 0 ) | 
|  | 3238 | { | 
|  | 3239 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3240 | mbedtls_ssl_send_alert_message( | 
|  | 3241 | ssl, | 
|  | 3242 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 3243 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); | 
| Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 3244 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); | 
|  | 3245 | } | 
|  | 3246 | } | 
|  | 3247 | else | 
|  | 3248 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3249 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3250 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 3251 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3252 | } | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3253 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3254 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) | 
| Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3255 | if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) ) | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3256 | { | 
| Manuel Pégourié-Gonnard | d92d6a1 | 2014-09-10 15:25:02 +0000 | [diff] [blame] | 3257 | size_t sig_len, hashlen; | 
|  | 3258 | unsigned char hash[64]; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3259 | mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE; | 
|  | 3260 | mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE; | 
|  | 3261 | 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] | 3262 | size_t params_len = p - params; | 
| Manuel Pégourié-Gonnard | 1f1f2a1 | 2017-05-18 11:27:06 +0200 | [diff] [blame] | 3263 | void *rs_ctx = NULL; | 
| Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 3264 |  | 
| Hanno Becker | a6899bb | 2019-02-06 18:26:03 +0000 | [diff] [blame] | 3265 | mbedtls_pk_context * peer_pk; | 
|  | 3266 |  | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 3267 | /* | 
|  | 3268 | * Handle the digitally-signed structure | 
|  | 3269 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3270 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 3271 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3272 | { | 
| Paul Bakker | 9659dae | 2013-08-28 16:21:34 +0200 | [diff] [blame] | 3273 | if( ssl_parse_signature_algorithm( ssl, &p, end, | 
|  | 3274 | &md_alg, &pk_alg ) != 0 ) | 
|  | 3275 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3276 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 3277 | ( "bad server key exchange message" ) ); | 
|  | 3278 | mbedtls_ssl_send_alert_message( | 
|  | 3279 | ssl, | 
|  | 3280 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 3281 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3282 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); | 
| Paul Bakker | 9659dae | 2013-08-28 16:21:34 +0200 | [diff] [blame] | 3283 | } | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3284 |  | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3285 | if( pk_alg != | 
|  | 3286 | mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) ) | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3287 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3288 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 3289 | ( "bad server key exchange message" ) ); | 
|  | 3290 | mbedtls_ssl_send_alert_message( | 
|  | 3291 | ssl, | 
|  | 3292 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 3293 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3294 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3295 | } | 
|  | 3296 | } | 
| Manuel Pégourié-Gonnard | 09edda8 | 2013-08-19 13:50:33 +0200 | [diff] [blame] | 3297 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3298 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 3299 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ | 
|  | 3300 | defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
|  | 3301 | if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ) | 
| Manuel Pégourié-Gonnard | 09edda8 | 2013-08-19 13:50:33 +0200 | [diff] [blame] | 3302 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3303 | pk_alg = mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3304 |  | 
| Paul Bakker | 9659dae | 2013-08-28 16:21:34 +0200 | [diff] [blame] | 3305 | /* Default hash for ECDSA is SHA-1 */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3306 | if( pk_alg == MBEDTLS_PK_ECDSA && md_alg == MBEDTLS_MD_NONE ) | 
|  | 3307 | md_alg = MBEDTLS_MD_SHA1; | 
| Paul Bakker | 9659dae | 2013-08-28 16:21:34 +0200 | [diff] [blame] | 3308 | } | 
|  | 3309 | else | 
|  | 3310 | #endif | 
|  | 3311 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3312 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 3313 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Paul Bakker | 9659dae | 2013-08-28 16:21:34 +0200 | [diff] [blame] | 3314 | } | 
| Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3315 |  | 
|  | 3316 | /* | 
|  | 3317 | * Read signature | 
|  | 3318 | */ | 
| Krzysztof Stachowiak | a1098f8 | 2018-03-13 11:28:49 +0100 | [diff] [blame] | 3319 |  | 
|  | 3320 | if( p > end - 2 ) | 
|  | 3321 | { | 
|  | 3322 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3323 | mbedtls_ssl_send_alert_message( | 
|  | 3324 | ssl, | 
|  | 3325 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 3326 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Krzysztof Stachowiak | a1098f8 | 2018-03-13 11:28:49 +0100 | [diff] [blame] | 3327 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); | 
|  | 3328 | } | 
| Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 3329 | sig_len = ( p[0] << 8 ) | p[1]; | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3330 | p += 2; | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3331 |  | 
| Krzysztof Stachowiak | 027f84c | 2018-03-13 11:29:24 +0100 | [diff] [blame] | 3332 | if( p != end - sig_len ) | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3333 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3334 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3335 | mbedtls_ssl_send_alert_message( | 
|  | 3336 | ssl, | 
|  | 3337 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 3338 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3339 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3340 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3341 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3342 | MBEDTLS_SSL_DEBUG_BUF( 3, "signature", p, sig_len ); | 
| Manuel Pégourié-Gonnard | ff56da3 | 2013-07-11 10:46:21 +0200 | [diff] [blame] | 3343 |  | 
| Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 3344 | /* | 
|  | 3345 | * Compute the hash that has been signed | 
|  | 3346 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3347 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ | 
|  | 3348 | defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
|  | 3349 | if( md_alg == MBEDTLS_MD_NONE ) | 
| Paul Bakker | c3f177a | 2012-04-11 16:11:49 +0000 | [diff] [blame] | 3350 | { | 
| Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3351 | hashlen = 36; | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 3352 | ret = mbedtls_ssl_get_key_exchange_md_ssl_tls( ssl, hash, params, | 
|  | 3353 | params_len ); | 
|  | 3354 | if( ret != 0 ) | 
| Andres Amaya Garcia | f0e521e | 2017-06-28 12:11:42 +0100 | [diff] [blame] | 3355 | return( ret ); | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 3356 | } | 
|  | 3357 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3358 | #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \ | 
|  | 3359 | MBEDTLS_SSL_PROTO_TLS1_1 */ | 
|  | 3360 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ | 
|  | 3361 | defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 3362 | if( md_alg != MBEDTLS_MD_NONE ) | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 3363 | { | 
| Gilles Peskine | ca1d742 | 2018-04-24 11:53:22 +0200 | [diff] [blame] | 3364 | ret = mbedtls_ssl_get_key_exchange_md_tls1_2( ssl, hash, &hashlen, | 
|  | 3365 | params, params_len, | 
|  | 3366 | md_alg ); | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 3367 | if( ret != 0 ) | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 3368 | return( ret ); | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 3369 | } | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 3370 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3371 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ | 
|  | 3372 | MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 3373 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3374 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 3375 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 3376 | } | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 3377 |  | 
| Gilles Peskine | ca1d742 | 2018-04-24 11:53:22 +0200 | [diff] [blame] | 3378 | MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen ); | 
| Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 3379 |  | 
| Hanno Becker | a6899bb | 2019-02-06 18:26:03 +0000 | [diff] [blame] | 3380 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 3381 | peer_pk = &ssl->handshake->peer_pubkey; | 
|  | 3382 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
| Manuel Pégourié-Gonnard | 7f2f062 | 2015-09-03 10:44:32 +0200 | [diff] [blame] | 3383 | if( ssl->session_negotiate->peer_cert == NULL ) | 
|  | 3384 | { | 
| Hanno Becker | 8273df8 | 2019-02-06 17:37:32 +0000 | [diff] [blame] | 3385 | /* Should never happen */ | 
| Hanno Becker | bd5580a | 2019-02-26 12:36:01 +0000 | [diff] [blame] | 3386 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
| Hanno Becker | 8273df8 | 2019-02-06 17:37:32 +0000 | [diff] [blame] | 3387 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | 7f2f062 | 2015-09-03 10:44:32 +0200 | [diff] [blame] | 3388 | } | 
| Hanno Becker | a6899bb | 2019-02-06 18:26:03 +0000 | [diff] [blame] | 3389 | peer_pk = &ssl->session_negotiate->peer_cert->pk; | 
|  | 3390 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
| Manuel Pégourié-Gonnard | 7f2f062 | 2015-09-03 10:44:32 +0200 | [diff] [blame] | 3391 |  | 
| Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 3392 | /* | 
|  | 3393 | * Verify signature | 
|  | 3394 | */ | 
| Hanno Becker | a6899bb | 2019-02-06 18:26:03 +0000 | [diff] [blame] | 3395 | if( !mbedtls_pk_can_do( peer_pk, pk_alg ) ) | 
| Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 3396 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3397 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3398 | mbedtls_ssl_send_alert_message( | 
|  | 3399 | ssl, | 
|  | 3400 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 3401 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3402 | return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH ); | 
| Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 3403 | } | 
|  | 3404 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3405 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
| Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 3406 | if( ssl->handshake->ecrs_enabled ) | 
| Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 3407 | rs_ctx = &ssl->handshake->ecrs_ctx.pk; | 
| Manuel Pégourié-Gonnard | 1f1f2a1 | 2017-05-18 11:27:06 +0200 | [diff] [blame] | 3408 | #endif | 
|  | 3409 |  | 
| Hanno Becker | a6899bb | 2019-02-06 18:26:03 +0000 | [diff] [blame] | 3410 | if( ( ret = mbedtls_pk_verify_restartable( peer_pk, | 
| Manuel Pégourié-Gonnard | 1f1f2a1 | 2017-05-18 11:27:06 +0200 | [diff] [blame] | 3411 | md_alg, hash, hashlen, p, sig_len, rs_ctx ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 3412 | { | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3413 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
| Manuel Pégourié-Gonnard | 1f1f2a1 | 2017-05-18 11:27:06 +0200 | [diff] [blame] | 3414 | if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS ) | 
|  | 3415 | #endif | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3416 | mbedtls_ssl_send_alert_message( | 
|  | 3417 | ssl, | 
|  | 3418 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 3419 | MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3420 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret ); | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3421 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
| Manuel Pégourié-Gonnard | 558da9c | 2018-06-13 12:02:12 +0200 | [diff] [blame] | 3422 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) | 
|  | 3423 | ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS; | 
|  | 3424 | #endif | 
| Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 3425 | return( ret ); | 
| Paul Bakker | c3f177a | 2012-04-11 16:11:49 +0000 | [diff] [blame] | 3426 | } | 
| Hanno Becker | ae553dd | 2019-02-08 14:06:00 +0000 | [diff] [blame] | 3427 |  | 
|  | 3428 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 3429 | /* We don't need the peer's public key anymore. Free it, | 
|  | 3430 | * so that more RAM is available for upcoming expensive | 
|  | 3431 | * operations like ECDHE. */ | 
|  | 3432 | mbedtls_pk_free( peer_pk ); | 
|  | 3433 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3434 | } | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3435 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3436 |  | 
| Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 3437 | exit: | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3438 | ssl->state++; | 
|  | 3439 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3440 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3441 |  | 
|  | 3442 | return( 0 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3443 | } | 
|  | 3444 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3445 | #if ! defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3446 | static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | da1ff38 | 2013-11-25 17:38:36 +0100 | [diff] [blame] | 3447 | { | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3448 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | 
| Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 3449 | ssl->handshake->ciphersuite_info; | 
| Manuel Pégourié-Gonnard | da1ff38 | 2013-11-25 17:38:36 +0100 | [diff] [blame] | 3450 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3451 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) ); | 
| Manuel Pégourié-Gonnard | da1ff38 | 2013-11-25 17:38:36 +0100 | [diff] [blame] | 3452 |  | 
| Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3453 | if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ) | 
| Manuel Pégourié-Gonnard | da1ff38 | 2013-11-25 17:38:36 +0100 | [diff] [blame] | 3454 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3455 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) ); | 
| Manuel Pégourié-Gonnard | da1ff38 | 2013-11-25 17:38:36 +0100 | [diff] [blame] | 3456 | ssl->state++; | 
|  | 3457 | return( 0 ); | 
|  | 3458 | } | 
|  | 3459 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3460 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 3461 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | da1ff38 | 2013-11-25 17:38:36 +0100 | [diff] [blame] | 3462 | } | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3463 | #else /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3464 | static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3465 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3466 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | d1b7f2b | 2016-02-24 14:13:22 +0000 | [diff] [blame] | 3467 | unsigned char *buf; | 
|  | 3468 | size_t n = 0; | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 3469 | size_t cert_type_len = 0, dn_len = 0; | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3470 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | 
| Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 3471 | ssl->handshake->ciphersuite_info; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3472 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3473 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3474 |  | 
| Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3475 | if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ) | 
| Manuel Pégourié-Gonnard | da1ff38 | 2013-11-25 17:38:36 +0100 | [diff] [blame] | 3476 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3477 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) ); | 
| Manuel Pégourié-Gonnard | da1ff38 | 2013-11-25 17:38:36 +0100 | [diff] [blame] | 3478 | ssl->state++; | 
|  | 3479 | return( 0 ); | 
|  | 3480 | } | 
|  | 3481 |  | 
| Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 3482 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3483 | { | 
| Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3484 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); | 
|  | 3485 | return( ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3486 | } | 
|  | 3487 |  | 
| Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3488 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) | 
|  | 3489 | { | 
|  | 3490 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3491 | mbedtls_ssl_send_alert_message( | 
|  | 3492 | ssl, | 
|  | 3493 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 3494 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); | 
| Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3495 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); | 
|  | 3496 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3497 |  | 
| Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3498 | ssl->state++; | 
|  | 3499 | ssl->client_auth = ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3500 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3501 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request", | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3502 | ssl->client_auth ? "a" : "no" ) ); | 
|  | 3503 |  | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3504 | if( ssl->client_auth == 0 ) | 
| Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3505 | { | 
| Johan Pascal | a89ca86 | 2020-08-25 10:03:19 +0200 | [diff] [blame] | 3506 | /* Current message is probably the ServerHelloDone */ | 
|  | 3507 | ssl->keep_current_message = 1; | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3508 | goto exit; | 
| Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3509 | } | 
| Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 3510 |  | 
| Manuel Pégourié-Gonnard | 04c1b4e | 2014-09-10 19:25:43 +0200 | [diff] [blame] | 3511 | /* | 
|  | 3512 | *  struct { | 
|  | 3513 | *      ClientCertificateType certificate_types<1..2^8-1>; | 
|  | 3514 | *      SignatureAndHashAlgorithm | 
|  | 3515 | *        supported_signature_algorithms<2^16-1>; -- TLS 1.2 only | 
|  | 3516 | *      DistinguishedName certificate_authorities<0..2^16-1>; | 
|  | 3517 | *  } CertificateRequest; | 
| Manuel Pégourié-Gonnard | d1b7f2b | 2016-02-24 14:13:22 +0000 | [diff] [blame] | 3518 | * | 
|  | 3519 | *  Since we only support a single certificate on clients, let's just | 
|  | 3520 | *  ignore all the information that's supposed to help us pick a | 
|  | 3521 | *  certificate. | 
|  | 3522 | * | 
|  | 3523 | *  We could check that our certificate matches the request, and bail out | 
|  | 3524 | *  if it doesn't, but it's simpler to just send the certificate anyway, | 
|  | 3525 | *  and give the server the opportunity to decide if it should terminate | 
|  | 3526 | *  the connection when it doesn't like our certificate. | 
|  | 3527 | * | 
|  | 3528 | *  Same goes for the hash in TLS 1.2's signature_algorithms: at this | 
|  | 3529 | *  point we only have one hash available (see comments in | 
| Simon Butcher | c0957bd | 2016-03-01 13:16:57 +0000 | [diff] [blame] | 3530 | *  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] | 3531 | * | 
|  | 3532 | *  However, we still minimally parse the message to check it is at least | 
|  | 3533 | *  superficially sane. | 
| Manuel Pégourié-Gonnard | 04c1b4e | 2014-09-10 19:25:43 +0200 | [diff] [blame] | 3534 | */ | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3535 | buf = ssl->in_msg; | 
| Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 3536 |  | 
| Manuel Pégourié-Gonnard | d1b7f2b | 2016-02-24 14:13:22 +0000 | [diff] [blame] | 3537 | /* certificate_types */ | 
| Krzysztof Stachowiak | 73b183c | 2018-04-05 10:20:09 +0200 | [diff] [blame] | 3538 | if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) ) | 
|  | 3539 | { | 
|  | 3540 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) ); | 
|  | 3541 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 3542 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
|  | 3543 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST ); | 
|  | 3544 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3545 | cert_type_len = buf[mbedtls_ssl_hs_hdr_len( ssl )]; | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3546 | n = cert_type_len; | 
|  | 3547 |  | 
| Krzysztof Stachowiak | bc145f7 | 2018-03-20 11:19:50 +0100 | [diff] [blame] | 3548 | /* | 
| Krzysztof Stachowiak | 94d4997 | 2018-04-05 14:48:55 +0200 | [diff] [blame] | 3549 | * In the subsequent code there are two paths that read from buf: | 
| Krzysztof Stachowiak | bc145f7 | 2018-03-20 11:19:50 +0100 | [diff] [blame] | 3550 | *     * the length of the signature algorithms field (if minor version of | 
|  | 3551 | *       SSL is 3), | 
|  | 3552 | *     * distinguished name length otherwise. | 
|  | 3553 | * Both reach at most the index: | 
|  | 3554 | *    ...hdr_len + 2 + n, | 
|  | 3555 | * therefore the buffer length at this point must be greater than that | 
|  | 3556 | * regardless of the actual code path. | 
|  | 3557 | */ | 
|  | 3558 | if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n ) | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3559 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3560 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 3561 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 3562 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3563 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST ); | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3564 | } | 
|  | 3565 |  | 
| Manuel Pégourié-Gonnard | d1b7f2b | 2016-02-24 14:13:22 +0000 | [diff] [blame] | 3566 | /* supported_signature_algorithms */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3567 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 3568 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3569 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3570 | size_t sig_alg_len = | 
|  | 3571 | ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] <<  8 ) | 
|  | 3572 | | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n]   ) ); | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3573 | #if defined(MBEDTLS_DEBUG_C) | 
| Krzysztof Stachowiak | bc231cc | 2018-03-20 14:09:53 +0100 | [diff] [blame] | 3574 | unsigned char* sig_alg; | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3575 | size_t i; | 
| Krzysztof Stachowiak | bc231cc | 2018-03-20 14:09:53 +0100 | [diff] [blame] | 3576 | #endif | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3577 |  | 
| Krzysztof Stachowiak | bc231cc | 2018-03-20 14:09:53 +0100 | [diff] [blame] | 3578 | /* | 
| Krzysztof Stachowiak | 94d4997 | 2018-04-05 14:48:55 +0200 | [diff] [blame] | 3579 | * The furthest access in buf is in the loop few lines below: | 
| Krzysztof Stachowiak | bc231cc | 2018-03-20 14:09:53 +0100 | [diff] [blame] | 3580 | *     sig_alg[i + 1], | 
|  | 3581 | * where: | 
|  | 3582 | *     sig_alg = buf + ...hdr_len + 3 + n, | 
|  | 3583 | *     max(i) = sig_alg_len - 1. | 
| Krzysztof Stachowiak | 94d4997 | 2018-04-05 14:48:55 +0200 | [diff] [blame] | 3584 | * Therefore the furthest access is: | 
| Krzysztof Stachowiak | bc231cc | 2018-03-20 14:09:53 +0100 | [diff] [blame] | 3585 | *     buf[...hdr_len + 3 + n + sig_alg_len - 1 + 1], | 
|  | 3586 | * which reduces to: | 
|  | 3587 | *     buf[...hdr_len + 3 + n + sig_alg_len], | 
|  | 3588 | * which is one less than we need the buf to be. | 
|  | 3589 | */ | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3590 | if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) | 
|  | 3591 | + 3 + n + sig_alg_len ) | 
| Krzysztof Stachowiak | bc231cc | 2018-03-20 14:09:53 +0100 | [diff] [blame] | 3592 | { | 
|  | 3593 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3594 | mbedtls_ssl_send_alert_message( | 
|  | 3595 | ssl, | 
|  | 3596 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 3597 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Krzysztof Stachowiak | bc231cc | 2018-03-20 14:09:53 +0100 | [diff] [blame] | 3598 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST ); | 
|  | 3599 | } | 
|  | 3600 |  | 
|  | 3601 | #if defined(MBEDTLS_DEBUG_C) | 
|  | 3602 | sig_alg = buf + mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n; | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3603 | for( i = 0; i < sig_alg_len; i += 2 ) | 
|  | 3604 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3605 | MBEDTLS_SSL_DEBUG_MSG( 3, | 
|  | 3606 | ( "Supported Signature Algorithm found: %d,%d", | 
|  | 3607 | sig_alg[i], sig_alg[i + 1]  ) ); | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3608 | } | 
|  | 3609 | #endif | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3610 |  | 
| Manuel Pégourié-Gonnard | d1b7f2b | 2016-02-24 14:13:22 +0000 | [diff] [blame] | 3611 | n += 2 + sig_alg_len; | 
| Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 3612 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3613 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3614 |  | 
| Manuel Pégourié-Gonnard | d1b7f2b | 2016-02-24 14:13:22 +0000 | [diff] [blame] | 3615 | /* certificate_authorities */ | 
|  | 3616 | dn_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] <<  8 ) | 
|  | 3617 | | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n]       ) ); | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3618 |  | 
|  | 3619 | n += dn_len; | 
| Manuel Pégourié-Gonnard | d1b7f2b | 2016-02-24 14:13:22 +0000 | [diff] [blame] | 3620 | if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n ) | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3621 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3622 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 3623 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 3624 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3625 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST ); | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3626 | } | 
|  | 3627 |  | 
|  | 3628 | exit: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3629 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3630 |  | 
|  | 3631 | return( 0 ); | 
|  | 3632 | } | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3633 | #endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3634 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3635 | static int ssl_parse_server_hello_done( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3636 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3637 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3638 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3639 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3640 |  | 
| Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 3641 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3642 | { | 
| Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3643 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); | 
|  | 3644 | return( ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3645 | } | 
| Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3646 |  | 
|  | 3647 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) | 
|  | 3648 | { | 
|  | 3649 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) ); | 
|  | 3650 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); | 
|  | 3651 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3652 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3653 | if( ssl->in_hslen  != mbedtls_ssl_hs_hdr_len( ssl ) || | 
|  | 3654 | ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3655 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3656 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 3657 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 3658 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3659 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO_DONE ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3660 | } | 
|  | 3661 |  | 
|  | 3662 | ssl->state++; | 
|  | 3663 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3664 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3665 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3666 | mbedtls_ssl_recv_flight_completed( ssl ); | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3667 | #endif | 
|  | 3668 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3669 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3670 |  | 
|  | 3671 | return( 0 ); | 
|  | 3672 | } | 
|  | 3673 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3674 | static int ssl_write_client_key_exchange( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3675 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3676 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3677 |  | 
|  | 3678 | size_t header_len; | 
|  | 3679 | size_t content_len; | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3680 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | 
| Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 3681 | ssl->handshake->ciphersuite_info; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3682 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3683 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3684 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3685 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) | 
|  | 3686 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3687 | { | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3688 | /* | 
|  | 3689 | * DHM key exchange -- send G^X mod P | 
|  | 3690 | */ | 
| Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3691 | content_len = ssl->handshake->dhm_ctx.len; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3692 |  | 
| Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3693 | ssl->out_msg[4] = (unsigned char)( content_len >> 8 ); | 
|  | 3694 | ssl->out_msg[5] = (unsigned char)( content_len      ); | 
|  | 3695 | header_len = 6; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3696 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3697 | ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx, | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3698 | (int) mbedtls_mpi_size( &ssl->handshake->dhm_ctx.P ), | 
|  | 3699 | &ssl->out_msg[header_len], content_len, | 
|  | 3700 | ssl->conf->f_rng, ssl->conf->p_rng ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3701 | if( ret != 0 ) | 
|  | 3702 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3703 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3704 | return( ret ); | 
|  | 3705 | } | 
|  | 3706 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3707 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X  ); | 
|  | 3708 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3709 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3710 | if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx, | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3711 | ssl->handshake->premaster, | 
|  | 3712 | MBEDTLS_PREMASTER_SIZE, | 
|  | 3713 | &ssl->handshake->pmslen, | 
|  | 3714 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3715 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3716 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3717 | return( ret ); | 
|  | 3718 | } | 
|  | 3719 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3720 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K  ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3721 | } | 
|  | 3722 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3723 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */ | 
| Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 3724 | #if defined(MBEDTLS_USE_PSA_CRYPTO) &&                           \ | 
|  | 3725 | ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) ||     \ | 
|  | 3726 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) ) | 
|  | 3727 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA || | 
|  | 3728 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ) | 
|  | 3729 | { | 
|  | 3730 | psa_status_t status; | 
| Janos Follath | 53b8ec2 | 2019-08-08 10:28:27 +0100 | [diff] [blame] | 3731 | psa_key_attributes_t key_attributes; | 
| Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 3732 |  | 
|  | 3733 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; | 
|  | 3734 |  | 
|  | 3735 | unsigned char own_pubkey[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH]; | 
|  | 3736 | size_t own_pubkey_len; | 
|  | 3737 | unsigned char *own_pubkey_ecpoint; | 
|  | 3738 | size_t own_pubkey_ecpoint_len; | 
|  | 3739 |  | 
| Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3740 | header_len = 4; | 
| Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 3741 |  | 
| Hanno Becker | 0a94a64 | 2019-01-11 14:35:30 +0000 | [diff] [blame] | 3742 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) ); | 
|  | 3743 |  | 
| Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 3744 | /* | 
|  | 3745 | * Generate EC private key for ECDHE exchange. | 
|  | 3746 | */ | 
|  | 3747 |  | 
| Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 3748 | /* The master secret is obtained from the shared ECDH secret by | 
|  | 3749 | * applying the TLS 1.2 PRF with a specific salt and label. While | 
|  | 3750 | * the PSA Crypto API encourages combining key agreement schemes | 
|  | 3751 | * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not | 
|  | 3752 | * yet support the provisioning of salt + label to the KDF. | 
|  | 3753 | * For the time being, we therefore need to split the computation | 
|  | 3754 | * 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] | 3755 | key_attributes = psa_key_attributes_init(); | 
|  | 3756 | psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE ); | 
| Janos Follath | df3b089 | 2019-08-08 11:12:24 +0100 | [diff] [blame] | 3757 | psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH ); | 
| Gilles Peskine | 4245980 | 2019-12-19 13:31:53 +0100 | [diff] [blame] | 3758 | psa_set_key_type( &key_attributes, handshake->ecdh_psa_type ); | 
|  | 3759 | psa_set_key_bits( &key_attributes, handshake->ecdh_bits ); | 
| Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 3760 |  | 
|  | 3761 | /* Generate ECDH private key. */ | 
| Janos Follath | 53b8ec2 | 2019-08-08 10:28:27 +0100 | [diff] [blame] | 3762 | status = psa_generate_key( &key_attributes, | 
| Janos Follath | df3b089 | 2019-08-08 11:12:24 +0100 | [diff] [blame] | 3763 | &handshake->ecdh_psa_privkey ); | 
| Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 3764 | if( status != PSA_SUCCESS ) | 
|  | 3765 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
|  | 3766 |  | 
|  | 3767 | /* Export the public part of the ECDH private key from PSA | 
|  | 3768 | * and convert it to ECPoint format used in ClientKeyExchange. */ | 
|  | 3769 | status = psa_export_public_key( handshake->ecdh_psa_privkey, | 
|  | 3770 | own_pubkey, sizeof( own_pubkey ), | 
|  | 3771 | &own_pubkey_len ); | 
|  | 3772 | if( status != PSA_SUCCESS ) | 
|  | 3773 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
|  | 3774 |  | 
|  | 3775 | if( mbedtls_psa_tls_psa_ec_to_ecpoint( own_pubkey, | 
|  | 3776 | own_pubkey_len, | 
|  | 3777 | &own_pubkey_ecpoint, | 
|  | 3778 | &own_pubkey_ecpoint_len ) != 0 ) | 
|  | 3779 | { | 
|  | 3780 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
|  | 3781 | } | 
|  | 3782 |  | 
|  | 3783 | /* Copy ECPoint structure to outgoing message buffer. */ | 
| Andrzej Kurek | ade9e28 | 2019-05-07 08:19:33 -0400 | [diff] [blame] | 3784 | ssl->out_msg[header_len] = (unsigned char) own_pubkey_ecpoint_len; | 
| Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3785 | memcpy( ssl->out_msg + header_len + 1, | 
|  | 3786 | own_pubkey_ecpoint, own_pubkey_ecpoint_len ); | 
|  | 3787 | content_len = own_pubkey_ecpoint_len + 1; | 
| Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 3788 |  | 
| Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 3789 | /* The ECDH secret is the premaster secret used for key derivation. */ | 
|  | 3790 |  | 
| Janos Follath | df3b089 | 2019-08-08 11:12:24 +0100 | [diff] [blame] | 3791 | /* Compute ECDH shared secret. */ | 
|  | 3792 | status = psa_raw_key_agreement( PSA_ALG_ECDH, | 
|  | 3793 | handshake->ecdh_psa_privkey, | 
|  | 3794 | handshake->ecdh_psa_peerkey, | 
|  | 3795 | handshake->ecdh_psa_peerkey_len, | 
|  | 3796 | ssl->handshake->premaster, | 
|  | 3797 | sizeof( ssl->handshake->premaster ), | 
|  | 3798 | &ssl->handshake->pmslen ); | 
| Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 3799 | if( status != PSA_SUCCESS ) | 
|  | 3800 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
|  | 3801 |  | 
|  | 3802 | status = psa_destroy_key( handshake->ecdh_psa_privkey ); | 
|  | 3803 | if( status != PSA_SUCCESS ) | 
|  | 3804 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3805 | handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT; | 
| Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 3806 | } | 
|  | 3807 | else | 
|  | 3808 | #endif /* MBEDTLS_USE_PSA_CRYPTO && | 
|  | 3809 | ( MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || | 
|  | 3810 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ) */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3811 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) ||                     \ | 
|  | 3812 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) ||                   \ | 
|  | 3813 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||                      \ | 
|  | 3814 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) | 
|  | 3815 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA || | 
|  | 3816 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA || | 
|  | 3817 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA || | 
|  | 3818 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA ) | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3819 | { | 
|  | 3820 | /* | 
|  | 3821 | * ECDH key exchange -- send client public value | 
|  | 3822 | */ | 
| Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3823 | header_len = 4; | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3824 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3825 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
| Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 3826 | if( ssl->handshake->ecrs_enabled ) | 
|  | 3827 | { | 
| Manuel Pégourié-Gonnard | c37423f | 2018-10-16 10:28:17 +0200 | [diff] [blame] | 3828 | 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] | 3829 | goto ecdh_calc_secret; | 
| Manuel Pégourié-Gonnard | 23e4162 | 2017-05-18 12:35:37 +0200 | [diff] [blame] | 3830 |  | 
| Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 3831 | mbedtls_ecdh_enable_restart( &ssl->handshake->ecdh_ctx ); | 
|  | 3832 | } | 
| Manuel Pégourié-Gonnard | 2350b4e | 2017-05-16 09:26:48 +0200 | [diff] [blame] | 3833 | #endif | 
|  | 3834 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3835 | ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx, | 
| Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3836 | &content_len, | 
|  | 3837 | &ssl->out_msg[header_len], 1000, | 
| Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 3838 | ssl->conf->f_rng, ssl->conf->p_rng ); | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3839 | if( ret != 0 ) | 
|  | 3840 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3841 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret ); | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3842 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
| Manuel Pégourié-Gonnard | 558da9c | 2018-06-13 12:02:12 +0200 | [diff] [blame] | 3843 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) | 
|  | 3844 | ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS; | 
|  | 3845 | #endif | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3846 | return( ret ); | 
|  | 3847 | } | 
|  | 3848 |  | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 3849 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, | 
|  | 3850 | MBEDTLS_DEBUG_ECDH_Q ); | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3851 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3852 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
| Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 3853 | if( ssl->handshake->ecrs_enabled ) | 
|  | 3854 | { | 
| Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3855 | ssl->handshake->ecrs_n = content_len; | 
| Manuel Pégourié-Gonnard | c37423f | 2018-10-16 10:28:17 +0200 | [diff] [blame] | 3856 | ssl->handshake->ecrs_state = ssl_ecrs_cke_ecdh_calc_secret; | 
| Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 3857 | } | 
| Manuel Pégourié-Gonnard | 2350b4e | 2017-05-16 09:26:48 +0200 | [diff] [blame] | 3858 |  | 
|  | 3859 | ecdh_calc_secret: | 
| Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 3860 | if( ssl->handshake->ecrs_enabled ) | 
| Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3861 | content_len = ssl->handshake->ecrs_n; | 
| Manuel Pégourié-Gonnard | 2350b4e | 2017-05-16 09:26:48 +0200 | [diff] [blame] | 3862 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3863 | if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3864 | &ssl->handshake->pmslen, | 
|  | 3865 | ssl->handshake->premaster, | 
|  | 3866 | MBEDTLS_MPI_MAX_SIZE, | 
|  | 3867 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3868 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3869 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret ); | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3870 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
| Manuel Pégourié-Gonnard | 558da9c | 2018-06-13 12:02:12 +0200 | [diff] [blame] | 3871 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) | 
|  | 3872 | ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS; | 
|  | 3873 | #endif | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3874 | return( ret ); | 
|  | 3875 | } | 
|  | 3876 |  | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 3877 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, | 
|  | 3878 | MBEDTLS_DEBUG_ECDH_Z ); | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3879 | } | 
|  | 3880 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3881 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || | 
|  | 3882 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED || | 
|  | 3883 | MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED || | 
|  | 3884 | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */ | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3885 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) | 
| Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3886 | if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) ) | 
| Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 3887 | { | 
| 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 | * opaque psk_identity<0..2^16-1>; | 
|  | 3890 | */ | 
| Hanno Becker | 520224e | 2018-10-26 11:38:07 +0100 | [diff] [blame] | 3891 | if( ssl_conf_has_static_psk( ssl->conf ) == 0 ) | 
| Manuel Pégourié-Gonnard | b4b19f3 | 2015-07-07 11:41:21 +0200 | [diff] [blame] | 3892 | { | 
| Hanno Becker | 2e4f616 | 2018-10-23 11:54:44 +0100 | [diff] [blame] | 3893 | /* We don't offer PSK suites if we don't have a PSK, | 
|  | 3894 | * and we check that the server's choice is among the | 
|  | 3895 | * ciphersuites we offered, so this should never happen. */ | 
|  | 3896 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | b4b19f3 | 2015-07-07 11:41:21 +0200 | [diff] [blame] | 3897 | } | 
| Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 3898 |  | 
| Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3899 | header_len = 4; | 
|  | 3900 | content_len = ssl->conf->psk_identity_len; | 
| Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 3901 |  | 
| Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3902 | 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] | 3903 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3904 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 3905 | ( "psk identity too long or SSL buffer too short" ) ); | 
| Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 3906 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); | 
|  | 3907 | } | 
|  | 3908 |  | 
| Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3909 | ssl->out_msg[header_len++] = (unsigned char)( content_len >> 8 ); | 
|  | 3910 | ssl->out_msg[header_len++] = (unsigned char)( content_len      ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3911 |  | 
| Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3912 | memcpy( ssl->out_msg + header_len, | 
|  | 3913 | ssl->conf->psk_identity, | 
|  | 3914 | ssl->conf->psk_identity_len ); | 
|  | 3915 | header_len += ssl->conf->psk_identity_len; | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3916 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3917 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) | 
|  | 3918 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ) | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3919 | { | 
| Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3920 | content_len = 0; | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 3921 | } | 
| Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3922 | else | 
|  | 3923 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3924 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) | 
|  | 3925 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 3926 | { | 
| Hanno Becker | dfab8e2 | 2018-10-23 11:59:34 +0100 | [diff] [blame] | 3927 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 3928 | /* Opaque PSKs are currently only supported for PSK-only suites. */ | 
| Hanno Becker | 520224e | 2018-10-26 11:38:07 +0100 | [diff] [blame] | 3929 | if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 ) | 
| Hanno Becker | dfab8e2 | 2018-10-23 11:59:34 +0100 | [diff] [blame] | 3930 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
|  | 3931 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 3932 |  | 
| Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3933 | if( ( ret = ssl_write_encrypted_pms( ssl, header_len, | 
|  | 3934 | &content_len, 2 ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 3935 | return( ret ); | 
|  | 3936 | } | 
|  | 3937 | else | 
|  | 3938 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3939 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) | 
|  | 3940 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3941 | { | 
| Hanno Becker | dfab8e2 | 2018-10-23 11:59:34 +0100 | [diff] [blame] | 3942 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 3943 | /* Opaque PSKs are currently only supported for PSK-only suites. */ | 
| Hanno Becker | 520224e | 2018-10-26 11:38:07 +0100 | [diff] [blame] | 3944 | if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 ) | 
| Hanno Becker | dfab8e2 | 2018-10-23 11:59:34 +0100 | [diff] [blame] | 3945 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
|  | 3946 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 3947 |  | 
| Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3948 | /* | 
|  | 3949 | * ClientDiffieHellmanPublic public (DHM send G^X mod P) | 
|  | 3950 | */ | 
| Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3951 | content_len = ssl->handshake->dhm_ctx.len; | 
| Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 3952 |  | 
| Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3953 | if( header_len + 2 + content_len > | 
|  | 3954 | MBEDTLS_SSL_OUT_CONTENT_LEN ) | 
| Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 3955 | { | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 3956 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 3957 | ( "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] | 3958 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); | 
|  | 3959 | } | 
|  | 3960 |  | 
| Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3961 | ssl->out_msg[header_len++] = (unsigned char)( content_len >> 8 ); | 
|  | 3962 | ssl->out_msg[header_len++] = (unsigned char)( content_len      ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3963 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3964 | ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx, | 
|  | 3965 | (int) mbedtls_mpi_size( &ssl->handshake->dhm_ctx.P ), | 
| Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3966 | &ssl->out_msg[header_len], content_len, | 
| Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 3967 | ssl->conf->f_rng, ssl->conf->p_rng ); | 
| Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3968 | if( ret != 0 ) | 
|  | 3969 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3970 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret ); | 
| Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3971 | return( ret ); | 
|  | 3972 | } | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3973 | } | 
| Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3974 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3975 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ | 
|  | 3976 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) | 
|  | 3977 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) | 
| Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 3978 | { | 
| Hanno Becker | dfab8e2 | 2018-10-23 11:59:34 +0100 | [diff] [blame] | 3979 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 3980 | /* Opaque PSKs are currently only supported for PSK-only suites. */ | 
| Hanno Becker | 520224e | 2018-10-26 11:38:07 +0100 | [diff] [blame] | 3981 | if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 ) | 
| Hanno Becker | dfab8e2 | 2018-10-23 11:59:34 +0100 | [diff] [blame] | 3982 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
|  | 3983 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 3984 |  | 
| Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3985 | /* | 
|  | 3986 | * ClientECDiffieHellmanPublic public; | 
|  | 3987 | */ | 
| Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 3988 | ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx, | 
|  | 3989 | &content_len, | 
|  | 3990 | &ssl->out_msg[header_len], | 
|  | 3991 | MBEDTLS_SSL_OUT_CONTENT_LEN - header_len, | 
| Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 3992 | ssl->conf->f_rng, ssl->conf->p_rng ); | 
| Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3993 | if( ret != 0 ) | 
|  | 3994 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3995 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret ); | 
| Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3996 | return( ret ); | 
|  | 3997 | } | 
| Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 3998 |  | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 3999 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, | 
|  | 4000 | MBEDTLS_DEBUG_ECDH_Q ); | 
| Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 4001 | } | 
|  | 4002 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4003 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ | 
| Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 4004 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4005 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 4006 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4007 | } | 
|  | 4008 |  | 
| Hanno Becker | afd311e | 2018-10-23 15:26:40 +0100 | [diff] [blame] | 4009 | #if defined(MBEDTLS_USE_PSA_CRYPTO) &&          \ | 
|  | 4010 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) | 
|  | 4011 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK && | 
|  | 4012 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 && | 
| Hanno Becker | 520224e | 2018-10-26 11:38:07 +0100 | [diff] [blame] | 4013 | ssl_conf_has_static_raw_psk( ssl->conf ) == 0 ) | 
| Hanno Becker | afd311e | 2018-10-23 15:26:40 +0100 | [diff] [blame] | 4014 | { | 
| Ronald Cron | 5ee5707 | 2020-06-11 09:34:06 +0200 | [diff] [blame] | 4015 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 4016 | ( "skip PMS generation for opaque PSK" ) ); | 
| Hanno Becker | afd311e | 2018-10-23 15:26:40 +0100 | [diff] [blame] | 4017 | } | 
|  | 4018 | else | 
|  | 4019 | #endif /* MBEDTLS_USE_PSA_CRYPTO && | 
|  | 4020 | MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4021 | if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl, | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 4022 | ciphersuite_info->key_exchange ) ) != 0 ) | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4023 | { | 
| Ronald Cron | 5ee5707 | 2020-06-11 09:34:06 +0200 | [diff] [blame] | 4024 | MBEDTLS_SSL_DEBUG_RET( 1, | 
|  | 4025 | "mbedtls_ssl_psk_derive_premaster", ret ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4026 | return( ret ); | 
|  | 4027 | } | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4028 | } | 
|  | 4029 | else | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4030 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4031 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) | 
|  | 4032 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4033 | { | 
| Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 4034 | header_len = 4; | 
|  | 4035 | if( ( ret = ssl_write_encrypted_pms( ssl, header_len, | 
|  | 4036 | &content_len, 0 ) ) != 0 ) | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 4037 | return( ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4038 | } | 
| Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 4039 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4040 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */ | 
| Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 4041 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
|  | 4042 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ) | 
|  | 4043 | { | 
| Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 4044 | header_len = 4; | 
| Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 4045 |  | 
|  | 4046 | ret = mbedtls_ecjpake_write_round_two( &ssl->handshake->ecjpake_ctx, | 
| Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 4047 | ssl->out_msg + header_len, | 
|  | 4048 | MBEDTLS_SSL_OUT_CONTENT_LEN - header_len, | 
|  | 4049 | &content_len, | 
| Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 4050 | ssl->conf->f_rng, ssl->conf->p_rng ); | 
|  | 4051 | if( ret != 0 ) | 
|  | 4052 | { | 
|  | 4053 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret ); | 
|  | 4054 | return( ret ); | 
|  | 4055 | } | 
|  | 4056 |  | 
|  | 4057 | ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx, | 
|  | 4058 | ssl->handshake->premaster, 32, &ssl->handshake->pmslen, | 
|  | 4059 | ssl->conf->f_rng, ssl->conf->p_rng ); | 
|  | 4060 | if( ret != 0 ) | 
|  | 4061 | { | 
|  | 4062 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret ); | 
|  | 4063 | return( ret ); | 
|  | 4064 | } | 
|  | 4065 | } | 
|  | 4066 | else | 
|  | 4067 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */ | 
| Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 4068 | { | 
|  | 4069 | ((void) ciphersuite_info); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4070 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 4071 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 4072 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4073 |  | 
| Hanno Becker | c14a3bb | 2019-01-14 09:41:16 +0000 | [diff] [blame] | 4074 | ssl->out_msglen  = header_len + content_len; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4075 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; | 
|  | 4076 | ssl->out_msg[0]  = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4077 |  | 
|  | 4078 | ssl->state++; | 
|  | 4079 |  | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4080 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4081 | { | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4082 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4083 | return( ret ); | 
|  | 4084 | } | 
|  | 4085 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4086 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4087 |  | 
|  | 4088 | return( 0 ); | 
|  | 4089 | } | 
|  | 4090 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4091 | #if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4092 | static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4093 | { | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 4094 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | 
| Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 4095 | ssl->handshake->ciphersuite_info; | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4096 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4097 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4098 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4099 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4100 | if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 4101 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4102 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret ); | 
| Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 4103 | return( ret ); | 
|  | 4104 | } | 
|  | 4105 |  | 
| Hanno Becker | 77adddc | 2019-02-07 12:32:43 +0000 | [diff] [blame] | 4106 | if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ) | 
| Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 4107 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4108 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) ); | 
| Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 4109 | ssl->state++; | 
|  | 4110 | return( 0 ); | 
|  | 4111 | } | 
|  | 4112 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4113 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 4114 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4115 | } | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4116 | #else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4117 | static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4118 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4119 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; | 
| Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 4120 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | 
| Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 4121 | ssl->handshake->ciphersuite_info; | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4122 | size_t n = 0, offset = 0; | 
|  | 4123 | unsigned char hash[48]; | 
| Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 4124 | unsigned char *hash_start = hash; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4125 | mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE; | 
| Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 4126 | size_t hashlen; | 
| Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 4127 | void *rs_ctx = NULL; | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4128 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4129 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4130 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4131 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
| Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 4132 | if( ssl->handshake->ecrs_enabled && | 
| Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 4133 | ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign ) | 
| Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 4134 | { | 
| Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 4135 | goto sign; | 
| Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 4136 | } | 
| Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 4137 | #endif | 
|  | 4138 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4139 | if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 4140 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4141 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret ); | 
| Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 4142 | return( ret ); | 
|  | 4143 | } | 
|  | 4144 |  | 
| Hanno Becker | 77adddc | 2019-02-07 12:32:43 +0000 | [diff] [blame] | 4145 | if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ) | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4146 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4147 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 4148 | ssl->state++; | 
|  | 4149 | return( 0 ); | 
|  | 4150 | } | 
|  | 4151 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4152 | if( ssl->client_auth == 0 || mbedtls_ssl_own_cert( ssl ) == NULL ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4153 | { | 
| Johan Pascal | a89ca86 | 2020-08-25 10:03:19 +0200 | [diff] [blame] | 4154 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) ); | 
|  | 4155 | ssl->state++; | 
|  | 4156 | return( 0 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4157 | } | 
|  | 4158 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4159 | if( mbedtls_ssl_own_key( ssl ) == NULL ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4160 | { | 
| Manuel Pégourié-Gonnard | b4b19f3 | 2015-07-07 11:41:21 +0200 | [diff] [blame] | 4161 | 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] | 4162 | return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4163 | } | 
|  | 4164 |  | 
|  | 4165 | /* | 
| Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 4166 | * Make a signature of the handshake digests | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4167 | */ | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4168 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
| Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 4169 | if( ssl->handshake->ecrs_enabled ) | 
|  | 4170 | ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign; | 
|  | 4171 |  | 
|  | 4172 | sign: | 
|  | 4173 | #endif | 
|  | 4174 |  | 
| Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 4175 | ssl->handshake->calc_verify( ssl, hash, &hashlen ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4176 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4177 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ | 
|  | 4178 | defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
|  | 4179 | if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 ) | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4180 | { | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 4181 | /* | 
|  | 4182 | * digitally-signed struct { | 
|  | 4183 | *     opaque md5_hash[16]; | 
|  | 4184 | *     opaque sha_hash[20]; | 
|  | 4185 | * }; | 
|  | 4186 | * | 
|  | 4187 | * md5_hash | 
|  | 4188 | *     MD5(handshake_messages); | 
|  | 4189 | * | 
|  | 4190 | * sha_hash | 
|  | 4191 | *     SHA(handshake_messages); | 
|  | 4192 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4193 | md_alg = MBEDTLS_MD_NONE; | 
| Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 4194 |  | 
|  | 4195 | /* | 
|  | 4196 | * For ECDSA, default hash is SHA-1 only | 
|  | 4197 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4198 | 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] | 4199 | { | 
|  | 4200 | hash_start += 16; | 
|  | 4201 | hashlen -= 16; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4202 | md_alg = MBEDTLS_MD_SHA1; | 
| Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 4203 | } | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 4204 | } | 
|  | 4205 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4206 | #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \ | 
|  | 4207 | MBEDTLS_SSL_PROTO_TLS1_1 */ | 
|  | 4208 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 4209 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 4210 | { | 
|  | 4211 | /* | 
|  | 4212 | * digitally-signed struct { | 
|  | 4213 | *     opaque handshake_messages[handshake_messages_length]; | 
|  | 4214 | * }; | 
|  | 4215 | * | 
|  | 4216 | * Taking shortcut here. We assume that the server always allows the | 
|  | 4217 | * PRF Hash function and has sent it in the allowed signature | 
|  | 4218 | * algorithms list received in the Certificate Request message. | 
|  | 4219 | * | 
|  | 4220 | * Until we encounter a server that does not, we will take this | 
|  | 4221 | * shortcut. | 
|  | 4222 | * | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 4223 | * Reason: Otherwise we should have running hashes for SHA512 and | 
|  | 4224 | *         SHA224 in order to satisfy 'weird' needs from the server | 
|  | 4225 | *         side. | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 4226 | */ | 
| Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 4227 | if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 4228 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4229 | md_alg = MBEDTLS_MD_SHA384; | 
|  | 4230 | ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384; | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 4231 | } | 
|  | 4232 | else | 
|  | 4233 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4234 | md_alg = MBEDTLS_MD_SHA256; | 
|  | 4235 | ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256; | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 4236 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4237 | 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] | 4238 |  | 
| Manuel Pégourié-Gonnard | bfe32ef | 2013-08-22 14:55:30 +0200 | [diff] [blame] | 4239 | /* Info from md_alg will be used instead */ | 
|  | 4240 | hashlen = 0; | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4241 | offset = 2; | 
|  | 4242 | } | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 4243 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4244 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 4245 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4246 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 4247 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 4248 | } | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4249 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4250 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
| Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 4251 | if( ssl->handshake->ecrs_enabled ) | 
| Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 4252 | rs_ctx = &ssl->handshake->ecrs_ctx.pk; | 
| Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 4253 | #endif | 
|  | 4254 |  | 
|  | 4255 | if( ( ret = mbedtls_pk_sign_restartable( mbedtls_ssl_own_key( ssl ), | 
|  | 4256 | md_alg, hash_start, hashlen, | 
| Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4257 | ssl->out_msg + 6 + offset, &n, | 
| Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 4258 | 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] | 4259 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4260 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret ); | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4261 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
| Manuel Pégourié-Gonnard | 558da9c | 2018-06-13 12:02:12 +0200 | [diff] [blame] | 4262 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) | 
|  | 4263 | ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS; | 
|  | 4264 | #endif | 
| Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4265 | return( ret ); | 
| Manuel Pégourié-Gonnard | 76c18a1 | 2013-08-20 16:50:40 +0200 | [diff] [blame] | 4266 | } | 
| Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 4267 |  | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4268 | ssl->out_msg[4 + offset] = (unsigned char)( n >> 8 ); | 
|  | 4269 | ssl->out_msg[5 + offset] = (unsigned char)( n      ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4270 |  | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4271 | ssl->out_msglen  = 6 + n + offset; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4272 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; | 
|  | 4273 | ssl->out_msg[0]  = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4274 |  | 
|  | 4275 | ssl->state++; | 
|  | 4276 |  | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4277 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4278 | { | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4279 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4280 | return( ret ); | 
|  | 4281 | } | 
|  | 4282 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4283 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4284 |  | 
| Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 4285 | return( ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4286 | } | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4287 | #endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4288 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4289 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | 
|  | 4290 | 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] | 4291 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4292 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4293 | uint32_t lifetime; | 
|  | 4294 | size_t ticket_len; | 
|  | 4295 | unsigned char *ticket; | 
| Manuel Pégourié-Gonnard | 000d5ae | 2014-09-10 21:52:12 +0200 | [diff] [blame] | 4296 | const unsigned char *msg; | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4297 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4298 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) ); | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4299 |  | 
| Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 4300 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4301 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4302 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4303 | return( ret ); | 
|  | 4304 | } | 
|  | 4305 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4306 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4307 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4308 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) ); | 
| Hanno Becker | b2fff6d | 2017-05-08 11:06:19 +0100 | [diff] [blame] | 4309 | mbedtls_ssl_send_alert_message( | 
|  | 4310 | ssl, | 
|  | 4311 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 4312 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4313 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4314 | } | 
|  | 4315 |  | 
|  | 4316 | /* | 
|  | 4317 | * struct { | 
|  | 4318 | *     uint32 ticket_lifetime_hint; | 
|  | 4319 | *     opaque ticket<0..2^16-1>; | 
|  | 4320 | * } NewSessionTicket; | 
|  | 4321 | * | 
| Manuel Pégourié-Gonnard | 000d5ae | 2014-09-10 21:52:12 +0200 | [diff] [blame] | 4322 | * 0  .  3   ticket_lifetime_hint | 
|  | 4323 | * 4  .  5   ticket_len (n) | 
|  | 4324 | * 6  .  5+n ticket content | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4325 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4326 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET || | 
|  | 4327 | ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len( ssl ) ) | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4328 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4329 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 4330 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 4331 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4332 | return( MBEDTLS_ERR_SSL_BAD_HS_NEW_SESSION_TICKET ); | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4333 | } | 
|  | 4334 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4335 | msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ); | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4336 |  | 
| Philippe Antoine | b5b2543 | 2018-05-11 11:06:29 +0200 | [diff] [blame] | 4337 | lifetime = ( ((uint32_t) msg[0]) << 24 ) | ( msg[1] << 16 ) | | 
|  | 4338 | ( msg[2] << 8 ) | ( msg[3] ); | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4339 |  | 
| Manuel Pégourié-Gonnard | 000d5ae | 2014-09-10 21:52:12 +0200 | [diff] [blame] | 4340 | ticket_len = ( msg[4] << 8 ) | ( msg[5] ); | 
|  | 4341 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4342 | 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] | 4343 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4344 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 4345 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 4346 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4347 | return( MBEDTLS_ERR_SSL_BAD_HS_NEW_SESSION_TICKET ); | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4348 | } | 
|  | 4349 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4350 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %d", ticket_len ) ); | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4351 |  | 
| Manuel Pégourié-Gonnard | 7cd5924 | 2013-08-02 13:24:41 +0200 | [diff] [blame] | 4352 | /* We're not waiting for a NewSessionTicket message any more */ | 
|  | 4353 | ssl->handshake->new_session_ticket = 0; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4354 | ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC; | 
| Manuel Pégourié-Gonnard | 7cd5924 | 2013-08-02 13:24:41 +0200 | [diff] [blame] | 4355 |  | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4356 | /* | 
|  | 4357 | * Zero-length ticket means the server changed his mind and doesn't want | 
|  | 4358 | * to send a ticket after all, so just forget it | 
|  | 4359 | */ | 
| Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 4360 | if( ticket_len == 0 ) | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4361 | return( 0 ); | 
|  | 4362 |  | 
| Hanno Becker | b2964cb | 2019-01-30 14:46:35 +0000 | [diff] [blame] | 4363 | if( ssl->session != NULL && ssl->session->ticket != NULL ) | 
|  | 4364 | { | 
|  | 4365 | mbedtls_platform_zeroize( ssl->session->ticket, | 
|  | 4366 | ssl->session->ticket_len ); | 
|  | 4367 | mbedtls_free( ssl->session->ticket ); | 
|  | 4368 | ssl->session->ticket = NULL; | 
|  | 4369 | ssl->session->ticket_len = 0; | 
|  | 4370 | } | 
|  | 4371 |  | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4372 | mbedtls_platform_zeroize( ssl->session_negotiate->ticket, | 
|  | 4373 | ssl->session_negotiate->ticket_len ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4374 | mbedtls_free( ssl->session_negotiate->ticket ); | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4375 | ssl->session_negotiate->ticket = NULL; | 
|  | 4376 | ssl->session_negotiate->ticket_len = 0; | 
|  | 4377 |  | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 4378 | if( ( ticket = mbedtls_calloc( 1, ticket_len ) ) == NULL ) | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4379 | { | 
| Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 4380 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "ticket alloc failed" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 4381 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 4382 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 4383 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4384 | } | 
|  | 4385 |  | 
| Manuel Pégourié-Gonnard | 000d5ae | 2014-09-10 21:52:12 +0200 | [diff] [blame] | 4386 | memcpy( ticket, msg + 6, ticket_len ); | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4387 |  | 
|  | 4388 | ssl->session_negotiate->ticket = ticket; | 
|  | 4389 | ssl->session_negotiate->ticket_len = ticket_len; | 
|  | 4390 | ssl->session_negotiate->ticket_lifetime = lifetime; | 
|  | 4391 |  | 
|  | 4392 | /* | 
|  | 4393 | * RFC 5077 section 3.4: | 
|  | 4394 | * "If the client receives a session ticket from the server, then it | 
|  | 4395 | * discards any Session ID that was sent in the ServerHello." | 
|  | 4396 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4397 | 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] | 4398 | ssl->session_negotiate->id_len = 0; | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4399 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4400 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) ); | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4401 |  | 
|  | 4402 | return( 0 ); | 
|  | 4403 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4404 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4405 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4406 | /* | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4407 | * SSL handshake -- client side -- single step | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4408 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4409 | int mbedtls_ssl_handshake_client_step( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4410 | { | 
|  | 4411 | int ret = 0; | 
|  | 4412 |  | 
| Manuel Pégourié-Gonnard | dba460f | 2015-06-24 22:59:30 +0200 | [diff] [blame] | 4413 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4414 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4415 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4416 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) ); | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4417 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4418 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4419 | return( ret ); | 
|  | 4420 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4421 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4422 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4423 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4424 | { | 
| Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4425 | if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4426 | return( ret ); | 
|  | 4427 | } | 
| Hanno Becker | bc2498a | 2018-08-28 10:13:29 +0100 | [diff] [blame] | 4428 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4429 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4430 | /* 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] | 4431 | * by DTLS for dropping out-of-sequence ChangeCipherSpec records */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4432 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | 
|  | 4433 | if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC && | 
| Manuel Pégourié-Gonnard | cd32a50 | 2014-09-20 13:54:12 +0200 | [diff] [blame] | 4434 | ssl->handshake->new_session_ticket != 0 ) | 
|  | 4435 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4436 | ssl->state = MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET; | 
| Manuel Pégourié-Gonnard | cd32a50 | 2014-09-20 13:54:12 +0200 | [diff] [blame] | 4437 | } | 
|  | 4438 | #endif | 
|  | 4439 |  | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4440 | switch( ssl->state ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4441 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4442 | case MBEDTLS_SSL_HELLO_REQUEST: | 
|  | 4443 | ssl->state = MBEDTLS_SSL_CLIENT_HELLO; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4444 | break; | 
|  | 4445 |  | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4446 | /* | 
|  | 4447 | *  ==>   ClientHello | 
|  | 4448 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4449 | case MBEDTLS_SSL_CLIENT_HELLO: | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4450 | ret = ssl_write_client_hello( ssl ); | 
|  | 4451 | break; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4452 |  | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4453 | /* | 
|  | 4454 | *  <==   ServerHello | 
|  | 4455 | *        Certificate | 
|  | 4456 | *      ( ServerKeyExchange  ) | 
|  | 4457 | *      ( CertificateRequest ) | 
|  | 4458 | *        ServerHelloDone | 
|  | 4459 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4460 | case MBEDTLS_SSL_SERVER_HELLO: | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4461 | ret = ssl_parse_server_hello( ssl ); | 
|  | 4462 | break; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4463 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4464 | case MBEDTLS_SSL_SERVER_CERTIFICATE: | 
|  | 4465 | ret = mbedtls_ssl_parse_certificate( ssl ); | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4466 | break; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4467 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4468 | case MBEDTLS_SSL_SERVER_KEY_EXCHANGE: | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4469 | ret = ssl_parse_server_key_exchange( ssl ); | 
|  | 4470 | break; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4471 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4472 | case MBEDTLS_SSL_CERTIFICATE_REQUEST: | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4473 | ret = ssl_parse_certificate_request( ssl ); | 
|  | 4474 | break; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4475 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4476 | case MBEDTLS_SSL_SERVER_HELLO_DONE: | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4477 | ret = ssl_parse_server_hello_done( ssl ); | 
|  | 4478 | break; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4479 |  | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4480 | /* | 
|  | 4481 | *  ==> ( Certificate/Alert  ) | 
|  | 4482 | *        ClientKeyExchange | 
|  | 4483 | *      ( CertificateVerify  ) | 
|  | 4484 | *        ChangeCipherSpec | 
|  | 4485 | *        Finished | 
|  | 4486 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4487 | case MBEDTLS_SSL_CLIENT_CERTIFICATE: | 
|  | 4488 | ret = mbedtls_ssl_write_certificate( ssl ); | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4489 | break; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4490 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4491 | case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE: | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4492 | ret = ssl_write_client_key_exchange( ssl ); | 
|  | 4493 | break; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4494 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4495 | case MBEDTLS_SSL_CERTIFICATE_VERIFY: | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4496 | ret = ssl_write_certificate_verify( ssl ); | 
|  | 4497 | break; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4498 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4499 | case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC: | 
|  | 4500 | ret = mbedtls_ssl_write_change_cipher_spec( ssl ); | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4501 | break; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4502 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4503 | case MBEDTLS_SSL_CLIENT_FINISHED: | 
|  | 4504 | ret = mbedtls_ssl_write_finished( ssl ); | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4505 | break; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4506 |  | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4507 | /* | 
| Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 4508 | *  <==   ( NewSessionTicket ) | 
|  | 4509 | *        ChangeCipherSpec | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4510 | *        Finished | 
|  | 4511 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4512 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | 
|  | 4513 | case MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET: | 
| Manuel Pégourié-Gonnard | cd32a50 | 2014-09-20 13:54:12 +0200 | [diff] [blame] | 4514 | ret = ssl_parse_new_session_ticket( ssl ); | 
|  | 4515 | break; | 
| Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 4516 | #endif | 
| Manuel Pégourié-Gonnard | cd32a50 | 2014-09-20 13:54:12 +0200 | [diff] [blame] | 4517 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4518 | case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC: | 
|  | 4519 | ret = mbedtls_ssl_parse_change_cipher_spec( ssl ); | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4520 | break; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4521 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4522 | case MBEDTLS_SSL_SERVER_FINISHED: | 
|  | 4523 | ret = mbedtls_ssl_parse_finished( ssl ); | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4524 | break; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4525 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4526 | case MBEDTLS_SSL_FLUSH_BUFFERS: | 
|  | 4527 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) ); | 
|  | 4528 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4529 | break; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4530 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4531 | case MBEDTLS_SSL_HANDSHAKE_WRAPUP: | 
|  | 4532 | mbedtls_ssl_handshake_wrapup( ssl ); | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4533 | break; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4534 |  | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4535 | default: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4536 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) ); | 
|  | 4537 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4538 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4539 |  | 
|  | 4540 | return( ret ); | 
|  | 4541 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4542 | #endif /* MBEDTLS_SSL_CLI_C */ |