blob: a8ce5c9af63fec3fb2676c3dc6b36c5cad81586b [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002 * TLS client-side functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * 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 Bakker5121ce52009-01-03 21:22:43 +000018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000021
Jerry Yufb4b6472022-01-27 15:03:26 +080022#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuc5aef882021-12-23 20:15:02 +080023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000025#include "mbedtls/platform.h"
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +020026#else
Rich Evans00ab4702015-02-06 13:43:58 +000027#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020028#define mbedtls_calloc calloc
SimonBd5800b72016-04-26 07:43:27 +010029#define mbedtls_free free
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +020030#endif
31
SimonBd5800b72016-04-26 07:43:27 +010032#include "mbedtls/ssl.h"
Chris Jones84a773f2021-03-05 18:38:47 +000033#include "ssl_misc.h"
Janos Follath73c616b2019-12-18 15:07:04 +000034#include "mbedtls/debug.h"
35#include "mbedtls/error.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020036#include "mbedtls/constant_time.h"
SimonBd5800b72016-04-26 07:43:27 +010037
Hanno Beckerbb89e272019-01-08 12:54:37 +000038#if defined(MBEDTLS_USE_PSA_CRYPTO)
39#include "mbedtls/psa_util.h"
Ronald Cron69a63422021-10-18 09:47:58 +020040#include "psa/crypto.h"
Hanno Beckerbb89e272019-01-08 12:54:37 +000041#endif /* MBEDTLS_USE_PSA_CRYPTO */
42
SimonBd5800b72016-04-26 07:43:27 +010043#include <string.h>
44
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +020045#include <stdint.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if defined(MBEDTLS_HAVE_TIME)
Simon Butcherb5b6af22016-07-13 14:46:18 +010048#include "mbedtls/platform_time.h"
Paul Bakkerfa9b1002013-07-03 15:31:03 +020049#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050052#include "mbedtls/platform_util.h"
Paul Bakker34617722014-06-13 17:20:13 +020053#endif
54
Gilles Peskineeccd8882020-03-10 12:19:08 +010055#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Becker520224e2018-10-26 11:38:07 +010056static int ssl_conf_has_static_psk( mbedtls_ssl_config const *conf )
Hanno Becker2e4f6162018-10-23 11:54:44 +010057{
58 if( conf->psk_identity == NULL ||
59 conf->psk_identity_len == 0 )
60 {
61 return( 0 );
62 }
63
64 if( conf->psk != NULL && conf->psk_len != 0 )
65 return( 1 );
66
67#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +020068 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Becker2e4f6162018-10-23 11:54:44 +010069 return( 1 );
70#endif /* MBEDTLS_USE_PSA_CRYPTO */
71
72 return( 0 );
73}
Hanno Beckerdfab8e22018-10-23 11:59:34 +010074
75#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker520224e2018-10-26 11:38:07 +010076static int ssl_conf_has_static_raw_psk( mbedtls_ssl_config const *conf )
Hanno Beckerdfab8e22018-10-23 11:59:34 +010077{
78 if( conf->psk_identity == NULL ||
79 conf->psk_identity_len == 0 )
80 {
81 return( 0 );
82 }
83
84 if( conf->psk != NULL && conf->psk_len != 0 )
85 return( 1 );
86
87 return( 0 );
88}
89#endif /* MBEDTLS_USE_PSA_CRYPTO */
90
Gilles Peskineeccd8882020-03-10 12:19:08 +010091#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Hanno Becker2e4f6162018-10-23 11:54:44 +010092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker261602c2017-04-12 14:54:42 +010094static int ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl,
95 unsigned char *buf,
96 const unsigned char *end,
97 size_t *olen )
Paul Bakkerd3edc862013-03-20 16:07:17 +010098{
99 unsigned char *p = buf;
100
101 *olen = 0;
102
Hanno Becker40f8b512017-10-12 14:58:55 +0100103 /* We're always including an TLS_EMPTY_RENEGOTIATION_INFO_SCSV in the
104 * initial ClientHello, in which case also adding the renegotiation
105 * info extension is NOT RECOMMENDED as per RFC 5746 Section 3.4. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker261602c2017-04-12 14:54:42 +0100107 return( 0 );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100108
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100109 MBEDTLS_SSL_DEBUG_MSG( 3,
110 ( "client hello, adding renegotiation extension" ) );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100111
Hanno Becker261602c2017-04-12 14:54:42 +0100112 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 + ssl->verify_data_len );
Simon Butchered997662015-09-28 02:14:30 +0100113
Paul Bakkerd3edc862013-03-20 16:07:17 +0100114 /*
115 * Secure renegotiation
116 */
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100117 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO, p, 0 );
118 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100119
120 *p++ = 0x00;
Joe Subbiani2194dc42021-07-14 12:31:31 +0100121 *p++ = MBEDTLS_BYTE_0( ssl->verify_data_len + 1 );
122 *p++ = MBEDTLS_BYTE_0( ssl->verify_data_len );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100123
124 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
125
126 *olen = 5 + ssl->verify_data_len;
Hanno Becker261602c2017-04-12 14:54:42 +0100127
128 return( 0 );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100129}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100131
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +0200132#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +0100133 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100134
Hanno Becker261602c2017-04-12 14:54:42 +0100135static int ssl_write_supported_point_formats_ext( mbedtls_ssl_context *ssl,
136 unsigned char *buf,
137 const unsigned char *end,
138 size_t *olen )
Paul Bakkerd3edc862013-03-20 16:07:17 +0100139{
140 unsigned char *p = buf;
Hanno Becker261602c2017-04-12 14:54:42 +0100141 (void) ssl; /* ssl used for debugging only */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100142
143 *olen = 0;
144
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100145 MBEDTLS_SSL_DEBUG_MSG( 3,
146 ( "client hello, adding supported_point_formats extension" ) );
Hanno Becker261602c2017-04-12 14:54:42 +0100147 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
Simon Butchered997662015-09-28 02:14:30 +0100148
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100149 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, p, 0 );
150 p += 2;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100151
152 *p++ = 0x00;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100153 *p++ = 2;
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200154
155 *p++ = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100157
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200158 *olen = 6;
Hanno Becker261602c2017-04-12 14:54:42 +0100159
160 return( 0 );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100161}
Darryl Green11999bb2018-03-13 15:22:58 +0000162#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
Robert Cragieae8535d2015-10-06 17:11:18 +0100163 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100164
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200165#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Hanno Becker261602c2017-04-12 14:54:42 +0100166static int ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl,
167 unsigned char *buf,
168 const unsigned char *end,
169 size_t *olen )
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200170{
Janos Follath865b3eb2019-12-16 11:46:15 +0000171 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200172 unsigned char *p = buf;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200173 size_t kkpp_len;
174
175 *olen = 0;
176
177 /* Skip costly extension if we can't use EC J-PAKE anyway */
178 if( mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
Hanno Becker261602c2017-04-12 14:54:42 +0100179 return( 0 );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200180
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100181 MBEDTLS_SSL_DEBUG_MSG( 3,
182 ( "client hello, adding ecjpake_kkpp extension" ) );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200183
Hanno Becker261602c2017-04-12 14:54:42 +0100184 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200185
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100186 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ECJPAKE_KKPP, p, 0 );
187 p += 2;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200188
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200189 /*
190 * We may need to send ClientHello multiple times for Hello verification.
191 * We don't want to compute fresh values every time (both for performance
192 * and consistency reasons), so cache the extension content.
193 */
194 if( ssl->handshake->ecjpake_cache == NULL ||
195 ssl->handshake->ecjpake_cache_len == 0 )
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200196 {
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200197 MBEDTLS_SSL_DEBUG_MSG( 3, ( "generating new ecjpake parameters" ) );
198
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +0200199 ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx,
Hanno Becker261602c2017-04-12 14:54:42 +0100200 p + 2, end - p - 2, &kkpp_len,
201 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +0200202 if( ret != 0 )
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200203 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100204 MBEDTLS_SSL_DEBUG_RET( 1 ,
205 "mbedtls_ecjpake_write_round_one", ret );
Hanno Becker261602c2017-04-12 14:54:42 +0100206 return( ret );
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200207 }
208
209 ssl->handshake->ecjpake_cache = mbedtls_calloc( 1, kkpp_len );
210 if( ssl->handshake->ecjpake_cache == NULL )
211 {
212 MBEDTLS_SSL_DEBUG_MSG( 1, ( "allocation failed" ) );
Hanno Becker261602c2017-04-12 14:54:42 +0100213 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200214 }
215
216 memcpy( ssl->handshake->ecjpake_cache, p + 2, kkpp_len );
217 ssl->handshake->ecjpake_cache_len = kkpp_len;
218 }
219 else
220 {
221 MBEDTLS_SSL_DEBUG_MSG( 3, ( "re-using cached ecjpake parameters" ) );
222
223 kkpp_len = ssl->handshake->ecjpake_cache_len;
Hanno Becker261602c2017-04-12 14:54:42 +0100224 MBEDTLS_SSL_CHK_BUF_PTR( p + 2, end, kkpp_len );
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +0200225
226 memcpy( p + 2, ssl->handshake->ecjpake_cache, kkpp_len );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200227 }
228
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100229 MBEDTLS_PUT_UINT16_BE( kkpp_len, p, 0 );
230 p += 2;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200231
232 *olen = kkpp_len + 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100233
234 return( 0 );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +0200235}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200236#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000237
Hanno Beckera0e20d02019-05-15 14:03:01 +0100238#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker261602c2017-04-12 14:54:42 +0100239static int ssl_write_cid_ext( mbedtls_ssl_context *ssl,
240 unsigned char *buf,
241 const unsigned char *end,
242 size_t *olen )
Hanno Becker49770ff2019-04-25 16:55:15 +0100243{
244 unsigned char *p = buf;
245 size_t ext_len;
Hanno Becker49770ff2019-04-25 16:55:15 +0100246
247 /*
Hanno Beckerebcc9132019-05-15 10:26:32 +0100248 * Quoting draft-ietf-tls-dtls-connection-id-05
249 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
Hanno Becker49770ff2019-04-25 16:55:15 +0100250 *
251 * struct {
252 * opaque cid<0..2^8-1>;
253 * } ConnectionId;
254 */
255
256 *olen = 0;
257 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
258 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
259 {
Hanno Becker261602c2017-04-12 14:54:42 +0100260 return( 0 );
Hanno Becker49770ff2019-04-25 16:55:15 +0100261 }
262 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding CID extension" ) );
263
264 /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
265 * which is at most 255, so the increment cannot overflow. */
Hanno Becker261602c2017-04-12 14:54:42 +0100266 MBEDTLS_SSL_CHK_BUF_PTR( p, end, (unsigned)( ssl->own_cid_len + 5 ) );
Hanno Becker49770ff2019-04-25 16:55:15 +0100267
268 /* Add extension ID + size */
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100269 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_CID, p, 0 );
270 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100271 ext_len = (size_t) ssl->own_cid_len + 1;
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100272 MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
273 p += 2;
Hanno Becker49770ff2019-04-25 16:55:15 +0100274
275 *p++ = (uint8_t) ssl->own_cid_len;
276 memcpy( p, ssl->own_cid, ssl->own_cid_len );
277
278 *olen = ssl->own_cid_len + 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100279
280 return( 0 );
Hanno Becker49770ff2019-04-25 16:55:15 +0100281}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100282#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker49770ff2019-04-25 16:55:15 +0100283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker261602c2017-04-12 14:54:42 +0100285static int ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl,
286 unsigned char *buf,
287 const unsigned char *end,
288 size_t *olen )
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200289{
290 unsigned char *p = buf;
291
Simon Butcher0fc94e92015-09-28 20:52:04 +0100292 *olen = 0;
293
Hanno Becker261602c2017-04-12 14:54:42 +0100294 if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE )
295 return( 0 );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200296
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100297 MBEDTLS_SSL_DEBUG_MSG( 3,
298 ( "client hello, adding max_fragment_length extension" ) );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200299
Hanno Becker261602c2017-04-12 14:54:42 +0100300 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 );
Simon Butchered997662015-09-28 02:14:30 +0100301
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100302 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, p, 0 );
303 p += 2;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200304
305 *p++ = 0x00;
306 *p++ = 1;
307
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200308 *p++ = ssl->conf->mfl_code;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200309
310 *olen = 5;
Hanno Becker261602c2017-04-12 14:54:42 +0100311
312 return( 0 );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200313}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker261602c2017-04-12 14:54:42 +0100317static int ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
318 unsigned char *buf,
319 const unsigned char *end,
320 size_t *olen )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100321{
322 unsigned char *p = buf;
323
Simon Butcher0fc94e92015-09-28 20:52:04 +0100324 *olen = 0;
325
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +0100326 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
Hanno Becker261602c2017-04-12 14:54:42 +0100327 return( 0 );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100328
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100329 MBEDTLS_SSL_DEBUG_MSG( 3,
330 ( "client hello, adding encrypt_then_mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100331
Hanno Becker261602c2017-04-12 14:54:42 +0100332 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Simon Butchered997662015-09-28 02:14:30 +0100333
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100334 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, p, 0 );
335 p += 2;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100336
337 *p++ = 0x00;
338 *p++ = 0x00;
339
340 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100341
342 return( 0 );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100343}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Becker261602c2017-04-12 14:54:42 +0100347static int ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl,
348 unsigned char *buf,
349 const unsigned char *end,
350 size_t *olen )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200351{
352 unsigned char *p = buf;
353
Simon Butcher0fc94e92015-09-28 20:52:04 +0100354 *olen = 0;
355
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +0100356 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED )
Hanno Becker261602c2017-04-12 14:54:42 +0100357 return( 0 );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200358
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100359 MBEDTLS_SSL_DEBUG_MSG( 3,
360 ( "client hello, adding extended_master_secret extension" ) );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200361
Hanno Becker261602c2017-04-12 14:54:42 +0100362 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Simon Butchered997662015-09-28 02:14:30 +0100363
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100364 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, p, 0 );
365 p += 2;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200366
367 *p++ = 0x00;
368 *p++ = 0x00;
369
370 *olen = 4;
Hanno Becker261602c2017-04-12 14:54:42 +0100371
372 return( 0 );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200373}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker261602c2017-04-12 14:54:42 +0100377static int ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl,
378 unsigned char *buf,
379 const unsigned char *end,
380 size_t *olen )
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200381{
382 unsigned char *p = buf;
383 size_t tlen = ssl->session_negotiate->ticket_len;
384
Simon Butcher0fc94e92015-09-28 20:52:04 +0100385 *olen = 0;
386
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200387 if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED )
Hanno Becker261602c2017-04-12 14:54:42 +0100388 return( 0 );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200389
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100390 MBEDTLS_SSL_DEBUG_MSG( 3,
391 ( "client hello, adding session ticket extension" ) );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200392
Hanno Becker261602c2017-04-12 14:54:42 +0100393 /* The addition is safe here since the ticket length is 16 bit. */
394 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 + tlen );
Simon Butchered997662015-09-28 02:14:30 +0100395
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100396 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SESSION_TICKET, p, 0 );
397 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200398
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100399 MBEDTLS_PUT_UINT16_BE( tlen, p, 0 );
400 p += 2;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200401
402 *olen = 4;
403
Simon Butchered997662015-09-28 02:14:30 +0100404 if( ssl->session_negotiate->ticket == NULL || tlen == 0 )
Hanno Becker261602c2017-04-12 14:54:42 +0100405 return( 0 );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200406
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100407 MBEDTLS_SSL_DEBUG_MSG( 3,
Paul Elliottd48d5c62021-01-07 14:47:05 +0000408 ( "sending session ticket of length %" MBEDTLS_PRINTF_SIZET, tlen ) );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200409
410 memcpy( p, ssl->session_negotiate->ticket, tlen );
411
412 *olen += tlen;
Hanno Becker261602c2017-04-12 14:54:42 +0100413
414 return( 0 );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200415}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418#if defined(MBEDTLS_SSL_ALPN)
Hanno Becker261602c2017-04-12 14:54:42 +0100419static int ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
420 unsigned char *buf,
421 const unsigned char *end,
422 size_t *olen )
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200423{
424 unsigned char *p = buf;
Simon Butchered997662015-09-28 02:14:30 +0100425 size_t alpnlen = 0;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200426 const char **cur;
427
Simon Butcher0fc94e92015-09-28 20:52:04 +0100428 *olen = 0;
429
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200430 if( ssl->conf->alpn_list == NULL )
Hanno Becker261602c2017-04-12 14:54:42 +0100431 return( 0 );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200434
Simon Butchered997662015-09-28 02:14:30 +0100435 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
Hanno Beckere131bfe2017-04-12 14:54:42 +0100436 alpnlen += strlen( *cur ) + 1;
Simon Butchered997662015-09-28 02:14:30 +0100437
Hanno Becker261602c2017-04-12 14:54:42 +0100438 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 + alpnlen );
Simon Butchered997662015-09-28 02:14:30 +0100439
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100440 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
441 p += 2;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200442
443 /*
444 * opaque ProtocolName<1..2^8-1>;
445 *
446 * struct {
447 * ProtocolName protocol_name_list<2..2^16-1>
448 * } ProtocolNameList;
449 */
450
451 /* Skip writing extension and list length for now */
452 p += 4;
453
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200454 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200455 {
Hanno Beckere131bfe2017-04-12 14:54:42 +0100456 /*
457 * mbedtls_ssl_conf_set_alpn_protocols() checked that the length of
458 * protocol names is less than 255.
459 */
460 *p = (unsigned char)strlen( *cur );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200461 memcpy( p + 1, *cur, *p );
462 p += 1 + *p;
463 }
464
465 *olen = p - buf;
466
467 /* List length = olen - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
Joe Subbiani6dd73642021-07-19 11:56:54 +0100468 MBEDTLS_PUT_UINT16_BE( *olen - 6, buf, 4 );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200469
470 /* Extension length = olen - 2 (ext_type) - 2 (ext_len) */
Joe Subbiani6dd73642021-07-19 11:56:54 +0100471 MBEDTLS_PUT_UINT16_BE( *olen - 4, buf, 2 );
Hanno Becker261602c2017-04-12 14:54:42 +0100472
473 return( 0 );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200474}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200476
Ron Eldora9788042018-12-05 11:04:31 +0200477#if defined(MBEDTLS_SSL_DTLS_SRTP)
Johan Pascal77696ee2020-09-22 21:49:40 +0200478static int ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl,
Johan Pascald387aa02020-09-23 18:47:56 +0200479 unsigned char *buf,
480 const unsigned char *end,
481 size_t *olen )
Johan Pascalb62bb512015-12-03 21:56:45 +0100482{
483 unsigned char *p = buf;
Johan Pascalf6417ec2020-09-22 15:15:19 +0200484 size_t protection_profiles_index = 0, ext_len = 0;
485 uint16_t mki_len = 0, profile_value = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +0100486
487 *olen = 0;
488
Johan Pascalc3ccd982020-10-28 17:18:18 +0100489 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
490 ( ssl->conf->dtls_srtp_profile_list == NULL ) ||
491 ( ssl->conf->dtls_srtp_profile_list_len == 0 ) )
Johan Pascalb62bb512015-12-03 21:56:45 +0100492 {
Johan Pascal77696ee2020-09-22 21:49:40 +0200493 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +0100494 }
495
Ron Eldora9788042018-12-05 11:04:31 +0200496 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +0100497 * uint8 SRTPProtectionProfile[2];
498 *
499 * struct {
500 * SRTPProtectionProfiles SRTPProtectionProfiles;
501 * opaque srtp_mki<0..255>;
502 * } UseSRTPData;
Johan Pascalb62bb512015-12-03 21:56:45 +0100503 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
Johan Pascalb62bb512015-12-03 21:56:45 +0100504 */
Johan Pascal9bc97ca2020-09-21 23:44:45 +0200505 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED )
Ron Eldor591f1622018-01-22 12:30:04 +0200506 {
507 mki_len = ssl->dtls_srtp_info.mki_len;
508 }
Ron Eldoref72faf2018-07-12 11:54:20 +0300509 /* Extension length = 2 bytes for profiles length,
510 * ssl->conf->dtls_srtp_profile_list_len * 2 (each profile is 2 bytes length ),
511 * 1 byte for srtp_mki vector length and the mki_len value
512 */
Ron Eldor089c9fe2018-12-06 17:12:49 +0200513 ext_len = 2 + 2 * ( ssl->conf->dtls_srtp_profile_list_len ) + 1 + mki_len;
514
Johan Pascal77696ee2020-09-22 21:49:40 +0200515 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding use_srtp extension" ) );
516
517 /* Check there is room in the buffer for the extension + 4 bytes
518 * - the extension tag (2 bytes)
519 * - the extension length (2 bytes)
520 */
521 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ext_len + 4 );
522
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100523 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_USE_SRTP, p, 0 );
524 p += 2;
Johan Pascal77696ee2020-09-22 21:49:40 +0200525
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100526 MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
527 p += 2;
Johan Pascalb62bb512015-12-03 21:56:45 +0100528
Ron Eldor3adb9922017-12-21 10:15:08 +0200529 /* protection profile length: 2*(ssl->conf->dtls_srtp_profile_list_len) */
Johan Pascalaae4d222020-09-22 21:21:39 +0200530 /* micro-optimization:
531 * the list size is limited to MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH
532 * which is lower than 127, so the upper byte of the length is always 0
533 * For the documentation, the more generic code is left in comments
534 * *p++ = (unsigned char)( ( ( 2 * ssl->conf->dtls_srtp_profile_list_len )
535 * >> 8 ) & 0xFF );
536 */
537 *p++ = 0;
Joe Subbiani2194dc42021-07-14 12:31:31 +0100538 *p++ = MBEDTLS_BYTE_0( 2 * ssl->conf->dtls_srtp_profile_list_len );
Johan Pascalb62bb512015-12-03 21:56:45 +0100539
Ron Eldoref72faf2018-07-12 11:54:20 +0300540 for( protection_profiles_index=0;
541 protection_profiles_index < ssl->conf->dtls_srtp_profile_list_len;
542 protection_profiles_index++ )
Johan Pascalb62bb512015-12-03 21:56:45 +0100543 {
Johan Pascal43f94902020-09-22 12:25:52 +0200544 profile_value = mbedtls_ssl_check_srtp_profile_value
Ron Eldor089c9fe2018-12-06 17:12:49 +0200545 ( ssl->conf->dtls_srtp_profile_list[protection_profiles_index] );
Johan Pascal43f94902020-09-22 12:25:52 +0200546 if( profile_value != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor089c9fe2018-12-06 17:12:49 +0200547 {
548 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_write_use_srtp_ext, add profile: %04x",
549 profile_value ) );
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100550 MBEDTLS_PUT_UINT16_BE( profile_value, p, 0 );
551 p += 2;
Ron Eldor089c9fe2018-12-06 17:12:49 +0200552 }
553 else
554 {
555 /*
556 * Note: we shall never arrive here as protection profiles
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200557 * is checked by mbedtls_ssl_conf_dtls_srtp_protection_profiles function
Ron Eldor089c9fe2018-12-06 17:12:49 +0200558 */
Johan Pascale79c1e82020-09-22 15:51:27 +0200559 MBEDTLS_SSL_DEBUG_MSG( 3,
560 ( "client hello, "
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200561 "illegal DTLS-SRTP protection profile %d",
Johan Pascale79c1e82020-09-22 15:51:27 +0200562 ssl->conf->dtls_srtp_profile_list[protection_profiles_index]
563 ) );
Johan Pascal76fdf1d2020-10-22 23:31:00 +0200564 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
Johan Pascalb62bb512015-12-03 21:56:45 +0100565 }
566 }
567
Ron Eldor591f1622018-01-22 12:30:04 +0200568 *p++ = mki_len & 0xFF;
569
570 if( mki_len != 0 )
571 {
Ron Eldor75870ec2018-12-06 17:31:55 +0200572 memcpy( p, ssl->dtls_srtp_info.mki_value, mki_len );
Ron Eldor313d7b52018-12-10 14:56:21 +0200573 /*
574 * Increment p to point to the current position.
575 */
576 p += mki_len;
Ron Eldoref72faf2018-07-12 11:54:20 +0300577 MBEDTLS_SSL_DEBUG_BUF( 3, "sending mki", ssl->dtls_srtp_info.mki_value,
578 ssl->dtls_srtp_info.mki_len );
Ron Eldor591f1622018-01-22 12:30:04 +0200579 }
580
Ron Eldoref72faf2018-07-12 11:54:20 +0300581 /*
582 * total extension length: extension type (2 bytes)
583 * + extension length (2 bytes)
584 * + protection profile length (2 bytes)
585 * + 2 * number of protection profiles
586 * + srtp_mki vector length(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +0200587 * + mki value
Ron Eldoref72faf2018-07-12 11:54:20 +0300588 */
Ron Eldor313d7b52018-12-10 14:56:21 +0200589 *olen = p - buf;
Johan Pascal77696ee2020-09-22 21:49:40 +0200590
591 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +0100592}
593#endif /* MBEDTLS_SSL_DTLS_SRTP */
594
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200595/*
596 * Generate random bytes for ClientHello
597 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598static int ssl_generate_random( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200599{
Janos Follath865b3eb2019-12-16 11:46:15 +0000600 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200601 unsigned char *p = ssl->handshake->randbytes;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +0100603 mbedtls_time_t t;
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200604#endif
605
Manuel Pégourié-Gonnardfb2d2232014-07-22 15:59:14 +0200606 /*
607 * When responding to a verify request, MUST reuse random (RFC 6347 4.2.1)
608 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200610 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000611 ssl->handshake->cookie != NULL )
Manuel Pégourié-Gonnardfb2d2232014-07-22 15:59:14 +0200612 {
613 return( 0 );
614 }
615#endif
616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +0100618 t = mbedtls_time( NULL );
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +0100619 MBEDTLS_PUT_UINT32_BE( t, p, 0 );
620 p += 4;
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200621
Paul Elliottd48d5c62021-01-07 14:47:05 +0000622 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, current time: %" MBEDTLS_PRINTF_LONGLONG,
623 (long long) t ) );
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200624#else
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +0100625 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 4 ) ) != 0 )
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200626 return( ret );
627
628 p += 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629#endif /* MBEDTLS_HAVE_TIME */
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200630
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +0100631 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 28 ) ) != 0 )
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200632 return( ret );
633
634 return( 0 );
635}
636
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +0100637/**
638 * \brief Validate cipher suite against config in SSL context.
639 *
640 * \param suite_info cipher suite to validate
641 * \param ssl SSL context
Andrzej Kurek03bac442018-04-25 05:06:07 -0400642 * \param min_minor_ver Minimal minor version to accept a cipher suite
643 * \param max_minor_ver Maximal minor version to accept a cipher suite
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +0100644 *
645 * \return 0 if valid, else 1
646 */
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100647static int ssl_validate_ciphersuite(
648 const mbedtls_ssl_ciphersuite_t * suite_info,
649 const mbedtls_ssl_context * ssl,
650 int min_minor_ver, int max_minor_ver )
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +0100651{
Andrzej Kurek03bac442018-04-25 05:06:07 -0400652 (void) ssl;
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +0100653 if( suite_info == NULL )
654 return( 1 );
655
Andrzej Kurek03bac442018-04-25 05:06:07 -0400656 if( suite_info->min_minor_ver > max_minor_ver ||
657 suite_info->max_minor_ver < min_minor_ver )
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +0100658 return( 1 );
659
660#if defined(MBEDTLS_SSL_PROTO_DTLS)
661 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
662 ( suite_info->flags & MBEDTLS_CIPHERSUITE_NODTLS ) )
663 return( 1 );
664#endif
665
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +0100666#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
667 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
668 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
669 return( 1 );
670#endif
671
Hanno Becker2e4f6162018-10-23 11:54:44 +0100672 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
Gilles Peskineeccd8882020-03-10 12:19:08 +0100673#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Becker2e4f6162018-10-23 11:54:44 +0100674 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
Hanno Becker520224e2018-10-26 11:38:07 +0100675 ssl_conf_has_static_psk( ssl->conf ) == 0 )
Hanno Becker2e4f6162018-10-23 11:54:44 +0100676 {
677 return( 1 );
678 }
Gilles Peskineeccd8882020-03-10 12:19:08 +0100679#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Hanno Becker2e4f6162018-10-23 11:54:44 +0100680
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +0100681 return( 0 );
682}
683
Ronald Cron12dcdf02022-02-16 15:28:22 +0100684static int ssl_tls12_write_client_hello_exts( mbedtls_ssl_context *ssl,
685 unsigned char *buf,
686 const unsigned char *end,
687 int uses_ec,
688 size_t *out_len )
689{
690 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
691 unsigned char *p = buf;
692 size_t ext_len = 0;
693
694 (void) ssl;
695 (void) end;
696 (void) uses_ec;
697 (void) ret;
698 (void) ext_len;
699
700 *out_len = 0;
701
702 /* Note that TLS_EMPTY_RENEGOTIATION_INFO_SCSV is always added
703 * even if MBEDTLS_SSL_RENEGOTIATION is not defined. */
704#if defined(MBEDTLS_SSL_RENEGOTIATION)
705 if( ( ret = ssl_write_renegotiation_ext( ssl, p, end, &ext_len ) ) != 0 )
706 {
707 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_renegotiation_ext", ret );
708 return( ret );
709 }
710 p += ext_len;
711#endif
712
713#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
714 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
715 if( uses_ec )
716 {
717 if( ( ret = ssl_write_supported_point_formats_ext( ssl, p, end,
718 &ext_len ) ) != 0 )
719 {
720 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_supported_point_formats_ext", ret );
721 return( ret );
722 }
723 p += ext_len;
724 }
725#endif
726
727#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
728 if( ( ret = ssl_write_ecjpake_kkpp_ext( ssl, p, end, &ext_len ) ) != 0 )
729 {
730 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_ecjpake_kkpp_ext", ret );
731 return( ret );
732 }
733 p += ext_len;
734#endif
735
736#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
737 if( ( ret = ssl_write_cid_ext( ssl, p, end, &ext_len ) ) != 0 )
738 {
739 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_cid_ext", ret );
740 return( ret );
741 }
742 p += ext_len;
743#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
744
745#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
746 if( ( ret = ssl_write_max_fragment_length_ext( ssl, p, end,
747 &ext_len ) ) != 0 )
748 {
749 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_max_fragment_length_ext", ret );
750 return( ret );
751 }
752 p += ext_len;
753#endif
754
755#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
756 if( ( ret = ssl_write_encrypt_then_mac_ext( ssl, p, end, &ext_len ) ) != 0 )
757 {
758 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_encrypt_then_mac_ext", ret );
759 return( ret );
760 }
761 p += ext_len;
762#endif
763
764#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
765 if( ( ret = ssl_write_extended_ms_ext( ssl, p, end, &ext_len ) ) != 0 )
766 {
767 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_extended_ms_ext", ret );
768 return( ret );
769 }
770 p += ext_len;
771#endif
772
773#if defined(MBEDTLS_SSL_DTLS_SRTP)
774 if( ( ret = ssl_write_use_srtp_ext( ssl, p, end, &ext_len ) ) != 0 )
775 {
776 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_use_srtp_ext", ret );
777 return( ret );
778 }
779 p += ext_len;
780#endif
781
782#if defined(MBEDTLS_SSL_SESSION_TICKETS)
783 if( ( ret = ssl_write_session_ticket_ext( ssl, p, end, &ext_len ) ) != 0 )
784 {
785 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_session_ticket_ext", ret );
786 return( ret );
787 }
788 p += ext_len;
789#endif
790
791 *out_len = p - buf;
792
793 return( 0 );
794}
795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796static int ssl_write_client_hello( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000797{
Janos Follath865b3eb2019-12-16 11:46:15 +0000798 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100799 size_t i, n, olen, ext_len = 0;
Hanno Becker261602c2017-04-12 14:54:42 +0100800
Paul Bakker5121ce52009-01-03 21:22:43 +0000801 unsigned char *buf;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200802 unsigned char *p, *q;
Hanno Becker261602c2017-04-12 14:54:42 +0100803 const unsigned char *end;
804
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200805 const int *ciphersuites;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Ron Eldor755bb6a2018-02-14 19:30:48 +0200807 int uses_ec = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000810
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +0100811 if( ssl->conf->f_rng == NULL )
Paul Bakkera9a028e2013-11-21 17:31:06 +0100812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided") );
814 return( MBEDTLS_ERR_SSL_NO_RNG );
Paul Bakkera9a028e2013-11-21 17:31:06 +0100815 }
816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817#if defined(MBEDTLS_SSL_RENEGOTIATION)
818 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100819#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000820 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200821 ssl->major_ver = ssl->conf->min_major_ver;
822 ssl->minor_ver = ssl->conf->min_minor_ver;
Paul Bakker48916f92012-09-16 19:57:18 +0000823 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000824
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +0200825 if( ssl->conf->max_major_ver == 0 )
Paul Bakker490ecc82011-10-06 13:04:09 +0000826 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100827 MBEDTLS_SSL_DEBUG_MSG( 1,
828 ( "configured max major version is invalid, consider using mbedtls_ssl_config_defaults()" ) );
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +0200829 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker490ecc82011-10-06 13:04:09 +0000830 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000831
Hanno Becker261602c2017-04-12 14:54:42 +0100832 buf = ssl->out_msg;
833 end = buf + MBEDTLS_SSL_OUT_CONTENT_LEN;
834
Paul Bakker5121ce52009-01-03 21:22:43 +0000835 /*
Hanno Becker261602c2017-04-12 14:54:42 +0100836 * Check if there's enough space for the first part of the ClientHello
837 * consisting of the 38 bytes described below, the session identifier (at
838 * most 32 bytes) and its length (1 byte).
839 *
840 * Use static upper bounds instead of the actual values
841 * to allow the compiler to optimize this away.
842 */
843 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 38 + 1 + 32 );
844
845 /*
846 * The 38 first bytes of the ClientHello:
847 * 0 . 0 handshake type (written later)
848 * 1 . 3 handshake length (written later)
Paul Bakker5121ce52009-01-03 21:22:43 +0000849 * 4 . 5 highest version supported
850 * 6 . 9 current UNIX time
851 * 10 . 37 random bytes
Hanno Becker261602c2017-04-12 14:54:42 +0100852 *
853 * The current UNIX time (4 bytes) and following 28 random bytes are written
854 * by ssl_generate_random() into ssl->handshake->randbytes buffer and then
855 * copied from there into the output buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000856 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000857
Hanno Becker261602c2017-04-12 14:54:42 +0100858 p = buf + 4;
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100859 mbedtls_ssl_write_version( ssl->conf->max_major_ver,
860 ssl->conf->max_minor_ver,
861 ssl->conf->transport, p );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +0100862 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, max version: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +0000865 buf[4], buf[5] ) );
866
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200867 if( ( ret = ssl_generate_random( ssl ) ) != 0 )
868 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_generate_random", ret );
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200870 return( ret );
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200871 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200872
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200873 memcpy( p, ssl->handshake->randbytes, 32 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", p, 32 );
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200875 p += 32;
Paul Bakker5121ce52009-01-03 21:22:43 +0000876
877 /*
878 * 38 . 38 session id length
879 * 39 . 39+n session id
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100880 * 39+n . 39+n DTLS only: cookie length (1 byte)
Hanno Becker261602c2017-04-12 14:54:42 +0100881 * 40+n . .. DTLS only: cookie
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100882 * .. . .. ciphersuitelist length (2 bytes)
883 * .. . .. ciphersuitelist
884 * .. . .. compression methods length (1 byte)
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000885 * .. . .. compression methods
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100886 * .. . .. extensions length (2 bytes)
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000887 * .. . .. extensions
Paul Bakker5121ce52009-01-03 21:22:43 +0000888 */
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +0200889 n = ssl->session_negotiate->id_len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000890
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100891 if( n < 16 || n > 32 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892#if defined(MBEDTLS_SSL_RENEGOTIATION)
893 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100894#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000895 ssl->handshake->resume == 0 )
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200896 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000897 n = 0;
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200898 }
899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200900#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200901 /*
902 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
903 * generate and include a Session ID in the TLS ClientHello."
904 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905#if defined(MBEDTLS_SSL_RENEGOTIATION)
906 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +0000907#endif
Manuel Pégourié-Gonnardd2b35ec2015-03-10 11:40:43 +0000908 {
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +0000909 if( ssl->session_negotiate->ticket != NULL &&
910 ssl->session_negotiate->ticket_len != 0 )
911 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +0100912 ret = ssl->conf->f_rng( ssl->conf->p_rng,
913 ssl->session_negotiate->id, 32 );
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200914
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +0000915 if( ret != 0 )
916 return( ret );
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200917
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +0200918 ssl->session_negotiate->id_len = n = 32;
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +0000919 }
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200920 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Paul Bakker5121ce52009-01-03 21:22:43 +0000922
Hanno Becker261602c2017-04-12 14:54:42 +0100923 /*
924 * The first check of the output buffer size above (
925 * MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 38 + 1 + 32 );)
926 * has checked that there is enough space in the output buffer for the
927 * session identifier length byte and the session identifier (n <= 32).
928 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000929 *p++ = (unsigned char) n;
930
931 for( i = 0; i < n; i++ )
Paul Bakker48916f92012-09-16 19:57:18 +0000932 *p++ = ssl->session_negotiate->id[i];
Paul Bakker5121ce52009-01-03 21:22:43 +0000933
Paul Elliottd48d5c62021-01-07 14:47:05 +0000934 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id", buf + 39, n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000936
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100937 /*
Hanno Becker261602c2017-04-12 14:54:42 +0100938 * With 'n' being the length of the session identifier
939 *
940 * 39+n . 39+n DTLS only: cookie length (1 byte)
941 * 40+n . .. DTLS only: cookie
942 * .. . .. ciphersuitelist length (2 bytes)
943 * .. . .. ciphersuitelist
944 * .. . .. compression methods length (1 byte)
945 * .. . .. compression methods
946 * .. . .. extensions length (2 bytes)
947 * .. . .. extensions
948 */
949
950 /*
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100951 * DTLS cookie
952 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200954 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100955 {
Hanno Becker261602c2017-04-12 14:54:42 +0100956 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 );
957
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000958 if( ssl->handshake->cookie == NULL )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +0200959 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no verify cookie to send" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +0200961 *p++ = 0;
962 }
963 else
964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000966 ssl->handshake->cookie,
967 ssl->handshake->verify_cookie_len );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +0200968
969 *p++ = ssl->handshake->verify_cookie_len;
Hanno Becker261602c2017-04-12 14:54:42 +0100970
971 MBEDTLS_SSL_CHK_BUF_PTR( p, end,
972 ssl->handshake->verify_cookie_len );
XiaokangQian9b93c0d2022-02-09 06:02:25 +0000973 memcpy( p, ssl->handshake->cookie,
974 ssl->handshake->verify_cookie_len );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +0200975 p += ssl->handshake->verify_cookie_len;
976 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100977 }
978#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000979
Paul Bakker48916f92012-09-16 19:57:18 +0000980 /*
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100981 * Ciphersuite list
Paul Bakker48916f92012-09-16 19:57:18 +0000982 */
Hanno Beckerd60b6c62021-04-29 12:04:11 +0100983 ciphersuites = ssl->conf->ciphersuite_list;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100984
985 /* Skip writing ciphersuite length for now */
986 n = 0;
987 q = p;
Hanno Becker261602c2017-04-12 14:54:42 +0100988
989 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100990 p += 2;
991
Paul Bakker2fbefde2013-06-29 16:01:15 +0200992 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuites[i] );
Paul Bakker2fbefde2013-06-29 16:01:15 +0200995
Andrzej Kurek03bac442018-04-25 05:06:07 -0400996 if( ssl_validate_ciphersuite( ciphersuite_info, ssl,
997 ssl->conf->min_minor_ver,
998 ssl->conf->max_minor_ver ) != 0 )
Paul Bakker2fbefde2013-06-29 16:01:15 +0200999 continue;
1000
Hanno Becker3c88c652019-01-02 11:17:25 +00001001 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %#04x (%s)",
Paul Elliott9f352112020-12-09 14:55:45 +00001002 (unsigned int)ciphersuites[i], ciphersuite_info->name ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001003
Ron Eldor755bb6a2018-02-14 19:30:48 +02001004#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
1005 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1006 uses_ec |= mbedtls_ssl_ciphersuite_uses_ec( ciphersuite_info );
1007#endif
1008
Hanno Becker261602c2017-04-12 14:54:42 +01001009 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
1010
Paul Bakker2fbefde2013-06-29 16:01:15 +02001011 n++;
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01001012 MBEDTLS_PUT_UINT16_BE( ciphersuites[i], p, 0 );
1013 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001014 }
1015
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001016 MBEDTLS_SSL_DEBUG_MSG( 3,
Paul Elliottd48d5c62021-01-07 14:47:05 +00001017 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " ciphersuites (excluding SCSVs)", n ) );
Ron Eldor714785d2017-08-28 13:55:55 +03001018
Manuel Pégourié-Gonnard5d9cde22015-01-22 10:49:41 +00001019 /*
1020 * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1021 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022#if defined(MBEDTLS_SSL_RENEGOTIATION)
1023 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard5d9cde22015-01-22 10:49:41 +00001024#endif
1025 {
Ron Eldor4a2fb4c2017-09-10 17:03:50 +03001026 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding EMPTY_RENEGOTIATION_INFO_SCSV" ) );
Hanno Becker261602c2017-04-12 14:54:42 +01001027 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01001028 MBEDTLS_PUT_UINT16_BE( MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO, p, 0 );
1029 p += 2;
Manuel Pégourié-Gonnard5d9cde22015-01-22 10:49:41 +00001030 n++;
1031 }
1032
Paul Bakker2fbefde2013-06-29 16:01:15 +02001033 *q++ = (unsigned char)( n >> 7 );
1034 *q++ = (unsigned char)( n << 1 );
1035
Mateusz Starzyka3a99842021-02-19 14:27:22 +01001036 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 1 ) );
1037 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d",
1038 MBEDTLS_SSL_COMPRESS_NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001039
Mateusz Starzyka3a99842021-02-19 14:27:22 +01001040 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
1041 *p++ = 1;
1042 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00001043
Hanno Becker261602c2017-04-12 14:54:42 +01001044 /* First write extensions, then the total length */
1045
1046 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
1047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaofei Bai6f435f02021-11-09 04:08:32 +00001049 if( ( ret = mbedtls_ssl_write_hostname_ext( ssl, p + 2 + ext_len,
Xiaofei Baif36e1672021-11-09 09:28:25 +00001050 end, &olen ) ) != 0 )
Hanno Becker261602c2017-04-12 14:54:42 +01001051 {
Xiaofei Bai6f435f02021-11-09 04:08:32 +00001052 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_hostname_ext", ret );
Hanno Becker261602c2017-04-12 14:54:42 +01001053 return( ret );
1054 }
Paul Bakkerd3edc862013-03-20 16:07:17 +01001055 ext_len += olen;
Paul Bakker0be444a2013-08-27 21:55:01 +02001056#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001057
Ronald Cron12dcdf02022-02-16 15:28:22 +01001058#if defined(MBEDTLS_SSL_ALPN)
1059 if( ( ret = ssl_write_alpn_ext( ssl, p + 2 + ext_len, end, &olen ) ) != 0 )
Hanno Becker261602c2017-04-12 14:54:42 +01001060 {
Ronald Cron12dcdf02022-02-16 15:28:22 +01001061 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_alpn_ext", ret );
Hanno Becker261602c2017-04-12 14:54:42 +01001062 return( ret );
1063 }
Paul Bakkerd3edc862013-03-20 16:07:17 +01001064 ext_len += olen;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001065#endif
Paul Bakkerc3f177a2012-04-11 16:11:49 +00001066
Jerry Yuc5aef882021-12-23 20:15:02 +08001067#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08001068 if( ( ret = mbedtls_ssl_write_sig_alg_ext( ssl, p + 2 + ext_len,
1069 end, &olen ) ) != 0 )
Hanno Becker261602c2017-04-12 14:54:42 +01001070 {
Jerry Yuf017ee42022-01-12 15:49:48 +08001071 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_sig_alg_ext", ret );
Hanno Becker261602c2017-04-12 14:54:42 +01001072 return( ret );
1073 }
Paul Bakkerd3edc862013-03-20 16:07:17 +01001074 ext_len += olen;
Jerry Yuc5aef882021-12-23 20:15:02 +08001075#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakkerc3f177a2012-04-11 16:11:49 +00001076
Manuel Pégourié-Gonnardf4721792015-09-15 10:53:51 +02001077#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01001078 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Ron Eldor755bb6a2018-02-14 19:30:48 +02001079 if( uses_ec )
1080 {
Jerry Yu9d555ac2021-12-20 22:27:58 +08001081 if( ( ret = mbedtls_ssl_write_supported_groups_ext( ssl, p + 2 + ext_len,
1082 end, &olen ) ) != 0 )
Hanno Becker261602c2017-04-12 14:54:42 +01001083 {
Jerry Yub47d0f82021-12-20 17:34:40 +08001084 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_supported_groups_ext", ret );
Hanno Becker261602c2017-04-12 14:54:42 +01001085 return( ret );
1086 }
Ron Eldor755bb6a2018-02-14 19:30:48 +02001087 ext_len += olen;
Ron Eldor755bb6a2018-02-14 19:30:48 +02001088 }
Paul Bakker41c83d32013-03-20 14:39:14 +01001089#endif
1090
Ronald Cron12dcdf02022-02-16 15:28:22 +01001091 ret = ssl_tls12_write_client_hello_exts( ssl, p + 2 + ext_len, end, uses_ec,
1092 &olen );
1093 if( ret != 0 )
Hanno Becker261602c2017-04-12 14:54:42 +01001094 return( ret );
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +02001095 ext_len += olen;
Manuel Pégourié-Gonnard294139b2015-09-15 16:55:05 +02001096
Ronald Cron12dcdf02022-02-16 15:28:22 +01001097 MBEDTLS_SSL_DEBUG_MSG( 3,
1098 ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET,
1099 ext_len ) );
Paul Bakkerc3f177a2012-04-11 16:11:49 +00001100
Paul Bakkera7036632014-04-30 10:15:38 +02001101 if( ext_len > 0 )
1102 {
Hanno Becker261602c2017-04-12 14:54:42 +01001103 /* No need to check for space here, because the extension
1104 * writing functions already took care of that. */
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01001105 MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
Joe Subbiani94180e72021-08-20 16:20:44 +01001106 p += 2 + ext_len;
Paul Bakkera7036632014-04-30 10:15:38 +02001107 }
Paul Bakker41c83d32013-03-20 14:39:14 +01001108
Paul Bakker5121ce52009-01-03 21:22:43 +00001109 ssl->out_msglen = p - buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
1111 ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00001112
1113 ssl->state++;
1114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001115#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001116 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02001118#endif
1119
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02001120 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001121 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02001122 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001123 return( ret );
1124 }
1125
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02001126#if defined(MBEDTLS_SSL_PROTO_DTLS)
1127 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
1128 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
1129 {
1130 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
1131 return( ret );
1132 }
Hanno Beckerbc2498a2018-08-28 10:13:29 +01001133#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02001134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001136
1137 return( 0 );
1138}
1139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140static int ssl_parse_renegotiation_info( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001141 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +00001142 size_t len )
1143{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144#if defined(MBEDTLS_SSL_RENEGOTIATION)
1145 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001146 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001147 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +00001148 if( len != 1 + ssl->verify_data_len * 2 ||
1149 buf[0] != ssl->verify_data_len * 2 ||
Gabor Mezei90437e32021-10-20 11:59:27 +02001150 mbedtls_ct_memcmp( buf + 1,
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001151 ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
Gabor Mezei90437e32021-10-20 11:59:27 +02001152 mbedtls_ct_memcmp( buf + 1 + ssl->verify_data_len,
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001153 ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001156 mbedtls_ssl_send_alert_message(
1157 ssl,
1158 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1159 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Dave Rodgman43fcb8d2021-06-28 21:49:15 +01001160 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker48916f92012-09-16 19:57:18 +00001161 }
1162 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001163 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001165 {
1166 if( len != 1 || buf[0] != 0x00 )
1167 {
Ronald Cron5ee57072020-06-11 09:34:06 +02001168 MBEDTLS_SSL_DEBUG_MSG( 1,
1169 ( "non-zero length renegotiation info" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001170 mbedtls_ssl_send_alert_message(
1171 ssl,
1172 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1173 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Dave Rodgman43fcb8d2021-06-28 21:49:15 +01001174 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001175 }
1176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001178 }
Paul Bakker48916f92012-09-16 19:57:18 +00001179
1180 return( 0 );
1181}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1184static int ssl_parse_max_fragment_length_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001185 const unsigned char *buf,
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001186 size_t len )
1187{
1188 /*
1189 * server should use the extension only if we did,
1190 * and if so the server's value should match ours (and len is always 1)
1191 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001192 if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ||
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001193 len != 1 ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001194 buf[0] != ssl->conf->mfl_code )
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001195 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001196 MBEDTLS_SSL_DEBUG_MSG( 1,
1197 ( "non-matching max fragment length extension" ) );
1198 mbedtls_ssl_send_alert_message(
1199 ssl,
1200 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +01001201 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
1202 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001203 }
1204
1205 return( 0 );
1206}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +00001208
Hanno Beckera0e20d02019-05-15 14:03:01 +01001209#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckera8373a12019-04-26 15:37:26 +01001210static int ssl_parse_cid_ext( mbedtls_ssl_context *ssl,
1211 const unsigned char *buf,
1212 size_t len )
1213{
1214 size_t peer_cid_len;
1215
1216 if( /* CID extension only makes sense in DTLS */
1217 ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
1218 /* The server must only send the CID extension if we have offered it. */
Hanno Becker22626482019-05-03 12:46:59 +01001219 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
Hanno Beckera8373a12019-04-26 15:37:26 +01001220 {
Hanno Becker22626482019-05-03 12:46:59 +01001221 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension unexpected" ) );
Hanno Beckera8373a12019-04-26 15:37:26 +01001222 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman43fcb8d2021-06-28 21:49:15 +01001223 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
Dave Rodgman53c86892021-06-29 10:02:06 +01001224 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Hanno Becker22626482019-05-03 12:46:59 +01001225 }
1226
1227 if( len == 0 )
1228 {
1229 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
1230 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +01001231 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
1232 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckera8373a12019-04-26 15:37:26 +01001233 }
1234
1235 peer_cid_len = *buf++;
1236 len--;
1237
1238 if( peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX )
1239 {
Hanno Becker22626482019-05-03 12:46:59 +01001240 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
Hanno Beckera8373a12019-04-26 15:37:26 +01001241 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +01001242 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
1243 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Hanno Beckera8373a12019-04-26 15:37:26 +01001244 }
1245
1246 if( len != peer_cid_len )
1247 {
Hanno Becker22626482019-05-03 12:46:59 +01001248 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
Hanno Beckera8373a12019-04-26 15:37:26 +01001249 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +01001250 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
1251 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckera8373a12019-04-26 15:37:26 +01001252 }
1253
Hanno Becker5a299902019-05-03 12:47:49 +01001254 ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
Hanno Beckera8373a12019-04-26 15:37:26 +01001255 ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
1256 memcpy( ssl->handshake->peer_cid, buf, peer_cid_len );
1257
1258 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use of CID extension negotiated" ) );
1259 MBEDTLS_SSL_DEBUG_BUF( 3, "Server CID", buf, peer_cid_len );
1260
Hanno Beckera8373a12019-04-26 15:37:26 +01001261 return( 0 );
1262}
Hanno Beckera0e20d02019-05-15 14:03:01 +01001263#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +01001264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1266static int ssl_parse_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001267 const unsigned char *buf,
1268 size_t len )
1269{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001270 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001271 len != 0 )
1272 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001273 MBEDTLS_SSL_DEBUG_MSG( 1,
1274 ( "non-matching encrypt-then-MAC extension" ) );
1275 mbedtls_ssl_send_alert_message(
1276 ssl,
1277 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman43fcb8d2021-06-28 21:49:15 +01001278 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
Dave Rodgman53c86892021-06-29 10:02:06 +01001279 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001280 }
1281
1282 ((void) buf);
1283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001285
1286 return( 0 );
1287}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001290#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1291static int ssl_parse_extended_ms_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001292 const unsigned char *buf,
1293 size_t len )
1294{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001295 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001296 len != 0 )
1297 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001298 MBEDTLS_SSL_DEBUG_MSG( 1,
1299 ( "non-matching extended master secret extension" ) );
1300 mbedtls_ssl_send_alert_message(
1301 ssl,
1302 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgmanbed89272021-06-29 12:06:32 +01001303 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
1304 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001305 }
1306
1307 ((void) buf);
1308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309 ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001310
1311 return( 0 );
1312}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1316static int ssl_parse_session_ticket_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001317 const unsigned char *buf,
1318 size_t len )
1319{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001320 if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED ||
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001321 len != 0 )
1322 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001323 MBEDTLS_SSL_DEBUG_MSG( 1,
1324 ( "non-matching session ticket extension" ) );
1325 mbedtls_ssl_send_alert_message(
1326 ssl,
1327 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgmanbed89272021-06-29 12:06:32 +01001328 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
1329 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001330 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001331
1332 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02001333
1334 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001335
1336 return( 0 );
1337}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001338#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001339
Robert Cragie136884c2015-10-02 13:34:31 +01001340#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01001341 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001342static int ssl_parse_supported_point_formats_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001343 const unsigned char *buf,
1344 size_t len )
1345{
1346 size_t list_size;
1347 const unsigned char *p;
1348
Philippe Antoine747fd532018-05-30 09:13:21 +02001349 if( len == 0 || (size_t)( buf[0] + 1 ) != len )
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001352 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1353 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001354 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001355 }
Philippe Antoine747fd532018-05-30 09:13:21 +02001356 list_size = buf[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001357
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +02001358 p = buf + 1;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001359 while( list_size > 0 )
1360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361 if( p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
1362 p[0] == MBEDTLS_ECP_PF_COMPRESSED )
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001363 {
Robert Cragie136884c2015-10-02 13:34:31 +01001364#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +02001365 ssl->handshake->ecdh_ctx.point_format = p[0];
Gilles Peskine064a85c2017-05-10 10:46:40 +02001366#endif
Robert Cragieae8535d2015-10-06 17:11:18 +01001367#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskinecd07e222021-05-27 23:17:34 +02001368 mbedtls_ecjpake_set_point_format( &ssl->handshake->ecjpake_ctx,
1369 p[0] );
Robert Cragie136884c2015-10-02 13:34:31 +01001370#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371 MBEDTLS_SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001372 return( 0 );
1373 }
1374
1375 list_size--;
1376 p++;
1377 }
1378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02001380 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1381 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001382 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001383}
Darryl Green11999bb2018-03-13 15:22:58 +00001384#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
Robert Cragieae8535d2015-10-06 17:11:18 +01001385 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001386
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001387#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1388static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl,
1389 const unsigned char *buf,
1390 size_t len )
1391{
Janos Follath865b3eb2019-12-16 11:46:15 +00001392 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001393
Hanno Beckere694c3e2017-12-27 21:34:08 +00001394 if( ssl->handshake->ciphersuite_info->key_exchange !=
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001395 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
1396 {
1397 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) );
1398 return( 0 );
1399 }
1400
Manuel Pégourié-Gonnardd0d8cb32015-09-17 14:16:30 +02001401 /* If we got here, we no longer need our cached extension */
1402 mbedtls_free( ssl->handshake->ecjpake_cache );
1403 ssl->handshake->ecjpake_cache = NULL;
1404 ssl->handshake->ecjpake_cache_len = 0;
1405
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001406 if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx,
1407 buf, len ) ) != 0 )
1408 {
1409 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001410 mbedtls_ssl_send_alert_message(
1411 ssl,
1412 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1413 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02001414 return( ret );
1415 }
1416
1417 return( 0 );
1418}
1419#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421#if defined(MBEDTLS_SSL_ALPN)
1422static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001423 const unsigned char *buf, size_t len )
1424{
1425 size_t list_len, name_len;
1426 const char **p;
1427
1428 /* If we didn't send it, the server shouldn't send it */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001429 if( ssl->conf->alpn_list == NULL )
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001430 {
1431 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching ALPN extension" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001432 mbedtls_ssl_send_alert_message(
1433 ssl,
1434 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgmanbed89272021-06-29 12:06:32 +01001435 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
1436 return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001437 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001438
1439 /*
1440 * opaque ProtocolName<1..2^8-1>;
1441 *
1442 * struct {
1443 * ProtocolName protocol_name_list<2..2^16-1>
1444 * } ProtocolNameList;
1445 *
1446 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
1447 */
1448
1449 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
1450 if( len < 4 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001451 {
1452 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1453 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001454 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001455 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001456
1457 list_len = ( buf[0] << 8 ) | buf[1];
1458 if( list_len != len - 2 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001459 {
1460 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1461 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001462 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001463 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001464
1465 name_len = buf[2];
1466 if( name_len != list_len - 1 )
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001467 {
1468 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1469 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001470 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001471 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001472
1473 /* Check that the server chosen protocol was in our list and save it */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001474 for( p = ssl->conf->alpn_list; *p != NULL; p++ )
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001475 {
1476 if( name_len == strlen( *p ) &&
1477 memcmp( buf + 3, *p, name_len ) == 0 )
1478 {
1479 ssl->alpn_chosen = *p;
1480 return( 0 );
1481 }
1482 }
1483
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001484 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ALPN extension: no matching protocol" ) );
Gilles Peskinec94f7352017-05-10 16:37:56 +02001485 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1486 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001487 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001488}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001490
Johan Pascalb62bb512015-12-03 21:56:45 +01001491#if defined(MBEDTLS_SSL_DTLS_SRTP)
1492static int ssl_parse_use_srtp_ext( mbedtls_ssl_context *ssl,
Ron Eldoref72faf2018-07-12 11:54:20 +03001493 const unsigned char *buf,
1494 size_t len )
Johan Pascalb62bb512015-12-03 21:56:45 +01001495{
Johan Pascal43f94902020-09-22 12:25:52 +02001496 mbedtls_ssl_srtp_profile server_protection = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +02001497 size_t i, mki_len = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01001498 uint16_t server_protection_profile_value = 0;
1499
1500 /* If use_srtp is not configured, just ignore the extension */
Johan Pascalc3ccd982020-10-28 17:18:18 +01001501 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
1502 ( ssl->conf->dtls_srtp_profile_list == NULL ) ||
1503 ( ssl->conf->dtls_srtp_profile_list_len == 0 ) )
Johan Pascalb62bb512015-12-03 21:56:45 +01001504 return( 0 );
1505
Ron Eldora9788042018-12-05 11:04:31 +02001506 /* RFC 5764 section 4.1.1
Johan Pascalb62bb512015-12-03 21:56:45 +01001507 * uint8 SRTPProtectionProfile[2];
1508 *
1509 * struct {
1510 * SRTPProtectionProfiles SRTPProtectionProfiles;
1511 * opaque srtp_mki<0..255>;
1512 * } UseSRTPData;
1513
1514 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
1515 *
Johan Pascalb62bb512015-12-03 21:56:45 +01001516 */
Johan Pascalf6417ec2020-09-22 15:15:19 +02001517 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED )
Ron Eldor591f1622018-01-22 12:30:04 +02001518 {
1519 mki_len = ssl->dtls_srtp_info.mki_len;
1520 }
Johan Pascalb62bb512015-12-03 21:56:45 +01001521
Ron Eldoref72faf2018-07-12 11:54:20 +03001522 /*
Johan Pascal76fdf1d2020-10-22 23:31:00 +02001523 * Length is 5 + optional mki_value : one protection profile length (2 bytes)
1524 * + protection profile (2 bytes)
1525 * + mki_len(1 byte)
Ron Eldor313d7b52018-12-10 14:56:21 +02001526 * and optional srtp_mki
Ron Eldoref72faf2018-07-12 11:54:20 +03001527 */
Johan Pascaladbd9442020-10-26 21:24:25 +01001528 if( ( len < 5 ) || ( len != ( buf[4] + 5u ) ) )
Hanno Beckerc3411d42021-06-24 11:09:00 +01001529 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Johan Pascalb62bb512015-12-03 21:56:45 +01001530
1531 /*
1532 * get the server protection profile
1533 */
Ron Eldoref72faf2018-07-12 11:54:20 +03001534
1535 /*
1536 * protection profile length must be 0x0002 as we must have only
1537 * one protection profile in server Hello
1538 */
Ron Eldor089c9fe2018-12-06 17:12:49 +02001539 if( ( buf[0] != 0 ) || ( buf[1] != 2 ) )
Hanno Beckerc3411d42021-06-24 11:09:00 +01001540 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Ron Eldor089c9fe2018-12-06 17:12:49 +02001541
1542 server_protection_profile_value = ( buf[2] << 8 ) | buf[3];
Johan Pascal43f94902020-09-22 12:25:52 +02001543 server_protection = mbedtls_ssl_check_srtp_profile_value(
1544 server_protection_profile_value );
1545 if( server_protection != MBEDTLS_TLS_SRTP_UNSET )
Ron Eldoref72faf2018-07-12 11:54:20 +03001546 {
Johan Pascal43f94902020-09-22 12:25:52 +02001547 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found srtp profile: %s",
1548 mbedtls_ssl_get_srtp_profile_as_string(
1549 server_protection ) ) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001550 }
1551
Johan Pascal43f94902020-09-22 12:25:52 +02001552 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
Ron Eldor591f1622018-01-22 12:30:04 +02001553
Johan Pascalb62bb512015-12-03 21:56:45 +01001554 /*
1555 * Check we have the server profile in our list
1556 */
Ron Eldor3adb9922017-12-21 10:15:08 +02001557 for( i=0; i < ssl->conf->dtls_srtp_profile_list_len; i++)
Johan Pascalb62bb512015-12-03 21:56:45 +01001558 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01001559 if( server_protection == ssl->conf->dtls_srtp_profile_list[i] )
1560 {
Ron Eldor3adb9922017-12-21 10:15:08 +02001561 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
Johan Pascal43f94902020-09-22 12:25:52 +02001562 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected srtp profile: %s",
1563 mbedtls_ssl_get_srtp_profile_as_string(
1564 server_protection ) ) );
Ron Eldor591f1622018-01-22 12:30:04 +02001565 break;
Johan Pascalb62bb512015-12-03 21:56:45 +01001566 }
1567 }
1568
Ron Eldor591f1622018-01-22 12:30:04 +02001569 /* If no match was found : server problem, it shall never answer with incompatible profile */
Johan Pascal43f94902020-09-22 12:25:52 +02001570 if( ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Ron Eldor591f1622018-01-22 12:30:04 +02001571 {
1572 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Ron Eldoref72faf2018-07-12 11:54:20 +03001573 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001574 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Ron Eldor591f1622018-01-22 12:30:04 +02001575 }
Johan Pascal20c7db32020-10-26 22:45:58 +01001576
1577 /* If server does not use mki in its reply, make sure the client won't keep
1578 * one as negotiated */
1579 if( len == 5 )
1580 {
1581 ssl->dtls_srtp_info.mki_len = 0;
1582 }
1583
Ron Eldoref72faf2018-07-12 11:54:20 +03001584 /*
1585 * RFC5764:
Ron Eldor591f1622018-01-22 12:30:04 +02001586 * If the client detects a nonzero-length MKI in the server's response
1587 * that is different than the one the client offered, then the client
1588 * MUST abort the handshake and SHOULD send an invalid_parameter alert.
1589 */
Ron Eldor313d7b52018-12-10 14:56:21 +02001590 if( len > 5 && ( buf[4] != mki_len ||
1591 ( memcmp( ssl->dtls_srtp_info.mki_value, &buf[5], mki_len ) ) ) )
Ron Eldor591f1622018-01-22 12:30:04 +02001592 {
1593 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1594 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001595 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Ron Eldor591f1622018-01-22 12:30:04 +02001596 }
Ron Eldorb4655392018-07-05 18:25:39 +03001597#if defined (MBEDTLS_DEBUG_C)
Ron Eldoref72faf2018-07-12 11:54:20 +03001598 if( len > 5 )
Ron Eldorb4655392018-07-05 18:25:39 +03001599 {
Ron Eldora9788042018-12-05 11:04:31 +02001600 MBEDTLS_SSL_DEBUG_BUF( 3, "received mki", ssl->dtls_srtp_info.mki_value,
1601 ssl->dtls_srtp_info.mki_len );
Ron Eldorb4655392018-07-05 18:25:39 +03001602 }
1603#endif
Ron Eldora9788042018-12-05 11:04:31 +02001604 return( 0 );
Johan Pascalb62bb512015-12-03 21:56:45 +01001605}
1606#endif /* MBEDTLS_SSL_DTLS_SRTP */
1607
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001608/*
1609 * Parse HelloVerifyRequest. Only called after verifying the HS type.
1610 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001611#if defined(MBEDTLS_SSL_PROTO_DTLS)
1612static int ssl_parse_hello_verify_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001613{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614 const unsigned char *p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001615 int major_ver, minor_ver;
1616 unsigned char cookie_len;
1617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001618 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse hello verify request" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001619
Gilles Peskineb64bf062019-09-27 14:02:44 +02001620 /* Check that there is enough room for:
1621 * - 2 bytes of version
1622 * - 1 byte of cookie_len
1623 */
1624 if( mbedtls_ssl_hs_hdr_len( ssl ) + 3 > ssl->in_msglen )
1625 {
1626 MBEDTLS_SSL_DEBUG_MSG( 1,
1627 ( "incoming HelloVerifyRequest message is too short" ) );
1628 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1629 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001630 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskineb64bf062019-09-27 14:02:44 +02001631 }
1632
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001633 /*
1634 * struct {
1635 * ProtocolVersion server_version;
1636 * opaque cookie<0..2^8-1>;
1637 * } HelloVerifyRequest;
1638 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639 MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 );
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001640 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, p );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001641 p += 2;
1642
TRodziewicz2d8800e2021-05-13 19:14:19 +02001643 /*
1644 * Since the RFC is not clear on this point, accept DTLS 1.0 (TLS 1.1)
1645 * even is lower than our min version.
1646 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001647 if( major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 ||
TRodziewiczb5850c52021-05-13 17:11:23 +02001648 minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001649 major_ver > ssl->conf->max_major_ver ||
1650 minor_ver > ssl->conf->max_minor_ver )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001651 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001652 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server version" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001654 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1655 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001656
Hanno Beckerbc000442021-06-24 09:18:19 +01001657 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001658 }
1659
1660 cookie_len = *p++;
Andres AG5a87c932016-09-26 14:53:05 +01001661 if( ( ssl->in_msg + ssl->in_msglen ) - p < cookie_len )
1662 {
1663 MBEDTLS_SSL_DEBUG_MSG( 1,
1664 ( "cookie length does not match incoming message size" ) );
1665 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1666 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001667 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Andres AG5a87c932016-09-26 14:53:05 +01001668 }
Gilles Peskineb51130d2019-09-27 14:00:36 +02001669 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie", p, cookie_len );
Andres AG5a87c932016-09-26 14:53:05 +01001670
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001671 mbedtls_free( ssl->handshake->cookie );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001672
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001673 ssl->handshake->cookie = mbedtls_calloc( 1, cookie_len );
1674 if( ssl->handshake->cookie == NULL )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001675 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001676 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", cookie_len ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001677 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001678 }
1679
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001680 memcpy( ssl->handshake->cookie, p, cookie_len );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001681 ssl->handshake->verify_cookie_len = cookie_len;
1682
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02001683 /* Start over at ClientHello */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
1685 mbedtls_ssl_reset_checksum( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001687 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001689 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse hello verify request" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001690
1691 return( 0 );
1692}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001693#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001695static int ssl_parse_server_hello( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001696{
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001697 int ret, i;
Paul Bakker23986e52011-04-24 08:57:21 +00001698 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001699 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001700 unsigned char *buf, *ext;
Manuel Pégourié-Gonnard1cf7b302015-06-24 22:28:19 +02001701 unsigned char comp;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001702#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001703 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001704#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001705 int handshake_failure = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706 const mbedtls_ssl_ciphersuite_t *suite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001708 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001709
Hanno Becker327c93b2018-08-15 13:56:18 +01001710 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001711 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001712 /* No alert on a read error. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001713 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001714 return( ret );
1715 }
1716
Hanno Becker79594fd2019-05-08 09:38:41 +01001717 buf = ssl->in_msg;
1718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001719 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001721#if defined(MBEDTLS_SSL_RENEGOTIATION)
1722 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001723 {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001724 ssl->renego_records_seen++;
1725
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001726 if( ssl->conf->renego_max_records >= 0 &&
1727 ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001728 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001729 MBEDTLS_SSL_DEBUG_MSG( 1,
1730 ( "renegotiation requested, but not honored by server" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001731 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001732 }
1733
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001734 MBEDTLS_SSL_DEBUG_MSG( 1,
1735 ( "non-handshake message during renegotiation" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01001736
1737 ssl->keep_current_message = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001738 return( MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO );
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001739 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001740#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001742 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001743 mbedtls_ssl_send_alert_message(
1744 ssl,
1745 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1746 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001747 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001748 }
1749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001750#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001751 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001752 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001753 if( buf[0] == MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001754 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001755 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received hello verify request" ) );
1756 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001757 return( ssl_parse_hello_verify_request( ssl ) );
1758 }
1759 else
1760 {
1761 /* We made it through the verification process */
XiaokangQian9b93c0d2022-02-09 06:02:25 +00001762 mbedtls_free( ssl->handshake->cookie );
1763 ssl->handshake->cookie = NULL;
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001764 ssl->handshake->verify_cookie_len = 0;
1765 }
1766 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001767#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00001768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001769 if( ssl->in_hslen < 38 + mbedtls_ssl_hs_hdr_len( ssl ) ||
1770 buf[0] != MBEDTLS_SSL_HS_SERVER_HELLO )
Paul Bakker5121ce52009-01-03 21:22:43 +00001771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001772 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001773 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1774 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001775 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +00001776 }
1777
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001778 /*
1779 * 0 . 1 server_version
1780 * 2 . 33 random (maybe including 4 bytes of Unix time)
1781 * 34 . 34 session_id length = n
1782 * 35 . 34+n session_id
1783 * 35+n . 36+n cipher_suite
1784 * 37+n . 37+n compression_method
1785 *
1786 * 38+n . 39+n extensions length (optional)
1787 * 40+n . .. extensions
1788 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001789 buf += mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001791 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", buf + 0, 2 );
1792 mbedtls_ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001793 ssl->conf->transport, buf + 0 );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01001794 ssl->session_negotiate->minor_ver = ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00001795
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001796 if( ssl->major_ver < ssl->conf->min_major_ver ||
1797 ssl->minor_ver < ssl->conf->min_minor_ver ||
1798 ssl->major_ver > ssl->conf->max_major_ver ||
1799 ssl->minor_ver > ssl->conf->max_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001800 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001801 MBEDTLS_SSL_DEBUG_MSG( 1,
1802 ( "server version out of bounds - min: [%d:%d], server: [%d:%d], max: [%d:%d]",
1803 ssl->conf->min_major_ver,
1804 ssl->conf->min_minor_ver,
1805 ssl->major_ver, ssl->minor_ver,
1806 ssl->conf->max_major_ver,
1807 ssl->conf->max_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001809 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1810 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001811
Hanno Beckerbc000442021-06-24 09:18:19 +01001812 return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001813 }
1814
Andres Amaya Garcia6bce9cb2017-09-06 15:33:34 +01001815 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu",
Paul Elliott9f352112020-12-09 14:55:45 +00001816 ( (unsigned long) buf[2] << 24 ) |
1817 ( (unsigned long) buf[3] << 16 ) |
1818 ( (unsigned long) buf[4] << 8 ) |
1819 ( (unsigned long) buf[5] ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001820
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001821 memcpy( ssl->handshake->randbytes + 32, buf + 2, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001822
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001823 n = buf[34];
Paul Bakker5121ce52009-01-03 21:22:43 +00001824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001825 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 2, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001826
Paul Bakker48916f92012-09-16 19:57:18 +00001827 if( n > 32 )
1828 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001829 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001830 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1831 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001832 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001833 }
1834
Manuel Pégourié-Gonnarda6e5bd52015-07-23 12:14:13 +02001835 if( ssl->in_hslen > mbedtls_ssl_hs_hdr_len( ssl ) + 39 + n )
Paul Bakker5121ce52009-01-03 21:22:43 +00001836 {
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001837 ext_len = ( ( buf[38 + n] << 8 )
1838 | ( buf[39 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001839
Paul Bakker48916f92012-09-16 19:57:18 +00001840 if( ( ext_len > 0 && ext_len < 4 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001841 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 40 + n + ext_len )
Paul Bakker48916f92012-09-16 19:57:18 +00001842 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001843 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001844 mbedtls_ssl_send_alert_message(
1845 ssl,
1846 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1847 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001848 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00001849 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001850 }
Manuel Pégourié-Gonnarda6e5bd52015-07-23 12:14:13 +02001851 else if( ssl->in_hslen == mbedtls_ssl_hs_hdr_len( ssl ) + 38 + n )
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001852 {
1853 ext_len = 0;
1854 }
1855 else
1856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001857 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001858 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1859 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001860 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001861 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001862
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001863 /* ciphersuite (used later) */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001864 i = ( buf[35 + n] << 8 ) | buf[36 + n];
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001865
1866 /*
1867 * Read and check compression
1868 */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001869 comp = buf[37 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001870
Manuel Pégourié-Gonnard1cf7b302015-06-24 22:28:19 +02001871 if( comp != MBEDTLS_SSL_COMPRESS_NULL )
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001872 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001873 MBEDTLS_SSL_DEBUG_MSG( 1,
1874 ( "server hello, bad compression: %d", comp ) );
1875 mbedtls_ssl_send_alert_message(
1876 ssl,
1877 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1878 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001879 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001880 }
1881
Paul Bakker380da532012-04-18 16:10:25 +00001882 /*
1883 * Initialize update checksum functions
1884 */
Hanno Beckere694c3e2017-12-27 21:34:08 +00001885 ssl->handshake->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( i );
1886 if( ssl->handshake->ciphersuite_info == NULL )
Paul Bakker68884e32013-01-07 18:20:04 +01001887 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001888 MBEDTLS_SSL_DEBUG_MSG( 1,
Paul Elliott9f352112020-12-09 14:55:45 +00001889 ( "ciphersuite info for %04x not found", (unsigned int)i ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02001890 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1891 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001892 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001893 }
Paul Bakker380da532012-04-18 16:10:25 +00001894
Hanno Beckere694c3e2017-12-27 21:34:08 +00001895 mbedtls_ssl_optimize_checksum( ssl, ssl->handshake->ciphersuite_info );
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001896
Paul Elliottd48d5c62021-01-07 14:47:05 +00001897 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001898 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 35, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001899
1900 /*
1901 * Check if the session can be resumed
1902 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001903 if( ssl->handshake->resume == 0 || n == 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904#if defined(MBEDTLS_SSL_RENEGOTIATION)
1905 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001906#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001907 ssl->session_negotiate->ciphersuite != i ||
1908 ssl->session_negotiate->compression != comp ||
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001909 ssl->session_negotiate->id_len != n ||
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001910 memcmp( ssl->session_negotiate->id, buf + 35, n ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001911 {
1912 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001913 ssl->handshake->resume = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001914#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +01001915 ssl->session_negotiate->start = mbedtls_time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001916#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001917 ssl->session_negotiate->ciphersuite = i;
1918 ssl->session_negotiate->compression = comp;
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02001919 ssl->session_negotiate->id_len = n;
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001920 memcpy( ssl->session_negotiate->id, buf + 35, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001921 }
1922 else
1923 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001924 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001926 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Paul Bakkerff60ee62010-03-16 21:09:09 +00001927 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001928 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001929 mbedtls_ssl_send_alert_message(
1930 ssl,
1931 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1932 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Paul Bakkerff60ee62010-03-16 21:09:09 +00001933 return( ret );
1934 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001935 }
1936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001937 MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001938 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001939
Paul Elliott3891caf2020-12-17 18:42:40 +00001940 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %04x", (unsigned) i ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001941 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d",
1942 buf[37 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001943
Andrzej Kurek03bac442018-04-25 05:06:07 -04001944 /*
1945 * Perform cipher suite validation in same way as in ssl_write_client_hello.
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001946 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001947 i = 0;
1948 while( 1 )
1949 {
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001950 if( ssl->conf->ciphersuite_list[i] == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001951 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001952 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001953 mbedtls_ssl_send_alert_message(
1954 ssl,
1955 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1956 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001957 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker5121ce52009-01-03 21:22:43 +00001958 }
1959
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001960 if( ssl->conf->ciphersuite_list[i++] ==
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001961 ssl->session_negotiate->ciphersuite )
1962 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001963 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001964 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001965 }
1966
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001967 suite_info = mbedtls_ssl_ciphersuite_from_id(
1968 ssl->session_negotiate->ciphersuite );
1969 if( ssl_validate_ciphersuite( suite_info, ssl, ssl->minor_ver,
1970 ssl->minor_ver ) != 0 )
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001971 {
1972 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001973 mbedtls_ssl_send_alert_message(
1974 ssl,
1975 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerc3411d42021-06-24 11:09:00 +01001976 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
1977 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001978 }
1979
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001980 MBEDTLS_SSL_DEBUG_MSG( 3,
1981 ( "server hello, chosen ciphersuite: %s", suite_info->name ) );
Mohammad Azim Khan1d3b5082018-04-18 19:35:00 +01001982
Gilles Peskineeccd8882020-03-10 12:19:08 +01001983#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02001984 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA &&
1985 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1986 {
1987 ssl->handshake->ecrs_enabled = 1;
1988 }
1989#endif
1990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001991 if( comp != MBEDTLS_SSL_COMPRESS_NULL
Paul Bakker2770fbd2012-07-03 13:30:23 +00001992 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001994 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01001995 mbedtls_ssl_send_alert_message(
1996 ssl,
1997 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1998 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Hanno Beckerc3411d42021-06-24 11:09:00 +01001999 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker5121ce52009-01-03 21:22:43 +00002000 }
Paul Bakker48916f92012-09-16 19:57:18 +00002001 ssl->session_negotiate->compression = comp;
Paul Bakker5121ce52009-01-03 21:22:43 +00002002
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02002003 ext = buf + 40 + n;
Paul Bakker48916f92012-09-16 19:57:18 +00002004
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002005 MBEDTLS_SSL_DEBUG_MSG( 2,
Paul Elliottd48d5c62021-01-07 14:47:05 +00002006 ( "server hello, total extension length: %" MBEDTLS_PRINTF_SIZET, ext_len ) );
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02002007
Paul Bakker48916f92012-09-16 19:57:18 +00002008 while( ext_len )
2009 {
2010 unsigned int ext_id = ( ( ext[0] << 8 )
2011 | ( ext[1] ) );
2012 unsigned int ext_size = ( ( ext[2] << 8 )
2013 | ( ext[3] ) );
2014
2015 if( ext_size + 4 > ext_len )
2016 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002017 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002018 mbedtls_ssl_send_alert_message(
2019 ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2020 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckerc3411d42021-06-24 11:09:00 +01002021 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00002022 }
2023
2024 switch( ext_id )
2025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002026 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
2027 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
2028#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00002029 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01002030#endif
Paul Bakker48916f92012-09-16 19:57:18 +00002031
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002032 if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
2033 ext_size ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00002034 return( ret );
2035
2036 break;
2037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002038#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2039 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002040 MBEDTLS_SSL_DEBUG_MSG( 3,
2041 ( "found max_fragment_length extension" ) );
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02002042
2043 if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
2044 ext + 4, ext_size ) ) != 0 )
2045 {
2046 return( ret );
2047 }
2048
2049 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002050#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02002051
Hanno Beckera0e20d02019-05-15 14:03:01 +01002052#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckera8373a12019-04-26 15:37:26 +01002053 case MBEDTLS_TLS_EXT_CID:
2054 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found CID extension" ) );
2055
2056 if( ( ret = ssl_parse_cid_ext( ssl,
2057 ext + 4,
2058 ext_size ) ) != 0 )
2059 {
2060 return( ret );
2061 }
2062
2063 break;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002064#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera8373a12019-04-26 15:37:26 +01002065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002066#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2067 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
2068 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt_then_mac extension" ) );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002069
2070 if( ( ret = ssl_parse_encrypt_then_mac_ext( ssl,
2071 ext + 4, ext_size ) ) != 0 )
2072 {
2073 return( ret );
2074 }
2075
2076 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002077#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002079#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
2080 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002081 MBEDTLS_SSL_DEBUG_MSG( 3,
2082 ( "found extended_master_secret extension" ) );
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002083
2084 if( ( ret = ssl_parse_extended_ms_ext( ssl,
2085 ext + 4, ext_size ) ) != 0 )
2086 {
2087 return( ret );
2088 }
2089
2090 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002091#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002093#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2094 case MBEDTLS_TLS_EXT_SESSION_TICKET:
2095 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02002096
2097 if( ( ret = ssl_parse_session_ticket_ext( ssl,
2098 ext + 4, ext_size ) ) != 0 )
2099 {
2100 return( ret );
2101 }
2102
2103 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002104#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02002105
Robert Cragie136884c2015-10-02 13:34:31 +01002106#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
Robert Cragieae8535d2015-10-06 17:11:18 +01002107 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002108 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002109 MBEDTLS_SSL_DEBUG_MSG( 3,
2110 ( "found supported_point_formats extension" ) );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002111
2112 if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
2113 ext + 4, ext_size ) ) != 0 )
2114 {
2115 return( ret );
2116 }
2117
2118 break;
Robert Cragieae8535d2015-10-06 17:11:18 +01002119#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
2120 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002121
Manuel Pégourié-Gonnard0a1324a2015-09-16 16:01:00 +02002122#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2123 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
2124 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake_kkpp extension" ) );
2125
2126 if( ( ret = ssl_parse_ecjpake_kkpp( ssl,
2127 ext + 4, ext_size ) ) != 0 )
2128 {
2129 return( ret );
2130 }
2131
2132 break;
2133#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002135#if defined(MBEDTLS_SSL_ALPN)
2136 case MBEDTLS_TLS_EXT_ALPN:
2137 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002138
Johan Pascal275874b2020-10-27 10:43:53 +01002139 if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
2140 return( ret );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002141
2142 break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002143#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002144
Johan Pascalb62bb512015-12-03 21:56:45 +01002145#if defined(MBEDTLS_SSL_DTLS_SRTP)
2146 case MBEDTLS_TLS_EXT_USE_SRTP:
2147 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found use_srtp extension" ) );
2148
Johan Pascalc3ccd982020-10-28 17:18:18 +01002149 if( ( ret = ssl_parse_use_srtp_ext( ssl, ext + 4, ext_size ) ) != 0 )
2150 return( ret );
Johan Pascalb62bb512015-12-03 21:56:45 +01002151
2152 break;
2153#endif /* MBEDTLS_SSL_DTLS_SRTP */
2154
Paul Bakker48916f92012-09-16 19:57:18 +00002155 default:
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002156 MBEDTLS_SSL_DEBUG_MSG( 3,
Paul Elliott9f352112020-12-09 14:55:45 +00002157 ( "unknown extension found: %u (ignoring)", ext_id ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002158 }
2159
2160 ext_len -= 4 + ext_size;
2161 ext += 4 + ext_size;
2162
2163 if( ext_len > 0 && ext_len < 4 )
2164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002165 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Hanno Beckerc3411d42021-06-24 11:09:00 +01002166 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker48916f92012-09-16 19:57:18 +00002167 }
2168 }
2169
2170 /*
2171 * Renegotiation security checks
2172 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002173 if( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002174 ssl->conf->allow_legacy_renegotiation ==
2175 MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00002176 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002177 MBEDTLS_SSL_DEBUG_MSG( 1,
2178 ( "legacy renegotiation, breaking off handshake" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002179 handshake_failure = 1;
2180 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002181#if defined(MBEDTLS_SSL_RENEGOTIATION)
2182 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2183 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
Paul Bakker48916f92012-09-16 19:57:18 +00002184 renegotiation_info_seen == 0 )
2185 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002186 MBEDTLS_SSL_DEBUG_MSG( 1,
2187 ( "renegotiation_info extension missing (secure)" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002188 handshake_failure = 1;
2189 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002190 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2191 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002192 ssl->conf->allow_legacy_renegotiation ==
2193 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00002194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002195 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002196 handshake_failure = 1;
2197 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002198 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2199 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002200 renegotiation_info_seen == 1 )
2201 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002202 MBEDTLS_SSL_DEBUG_MSG( 1,
2203 ( "renegotiation_info extension present (legacy)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002204 handshake_failure = 1;
2205 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002206#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002207
2208 if( handshake_failure == 1 )
2209 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002210 mbedtls_ssl_send_alert_message(
2211 ssl,
2212 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2213 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Hanno Beckerc3411d42021-06-24 11:09:00 +01002214 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker48916f92012-09-16 19:57:18 +00002215 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002217 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002218
2219 return( 0 );
2220}
2221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002222#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2223 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002224static int ssl_parse_server_dh_params( mbedtls_ssl_context *ssl,
2225 unsigned char **p,
Paul Bakker29e1f122013-04-16 13:07:56 +02002226 unsigned char *end )
2227{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002228 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01002229 size_t dhm_actual_bitlen;
Paul Bakker29e1f122013-04-16 13:07:56 +02002230
Paul Bakker29e1f122013-04-16 13:07:56 +02002231 /*
2232 * Ephemeral DH parameters:
2233 *
2234 * struct {
2235 * opaque dh_p<1..2^16-1>;
2236 * opaque dh_g<1..2^16-1>;
2237 * opaque dh_Ys<1..2^16-1>;
2238 * } ServerDHParams;
2239 */
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002240 if( ( ret = mbedtls_dhm_read_params( &ssl->handshake->dhm_ctx,
2241 p, end ) ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02002242 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002243 MBEDTLS_SSL_DEBUG_RET( 2, ( "mbedtls_dhm_read_params" ), ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02002244 return( ret );
2245 }
2246
Gilles Peskine487bbf62021-05-27 22:17:07 +02002247 dhm_actual_bitlen = mbedtls_dhm_get_bitlen( &ssl->handshake->dhm_ctx );
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01002248 if( dhm_actual_bitlen < ssl->conf->dhm_min_bitlen )
Paul Bakker29e1f122013-04-16 13:07:56 +02002249 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00002250 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DHM prime too short: %" MBEDTLS_PRINTF_SIZET " < %u",
Gilles Peskinee8a2fc82020-12-08 22:46:11 +01002251 dhm_actual_bitlen,
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002252 ssl->conf->dhm_min_bitlen ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002253 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02002254 }
2255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2257 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2258 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker29e1f122013-04-16 13:07:56 +02002259
2260 return( ret );
2261}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002262#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2263 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02002264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2266 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2267 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Przemek Stekiel068a6b42022-03-17 07:54:09 +01002268 ( !defined(MBEDTLS_USE_PSA_CRYPTO) && \
2269 ( defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2270 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002271static int ssl_check_server_ecdh_params( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002272{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002273 const mbedtls_ecp_curve_info *curve_info;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002274 mbedtls_ecp_group_id grp_id;
2275#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
2276 grp_id = ssl->handshake->ecdh_ctx.grp.id;
2277#else
2278 grp_id = ssl->handshake->ecdh_ctx.grp_id;
2279#endif
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002280
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002281 curve_info = mbedtls_ecp_curve_info_from_grp_id( grp_id );
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002282 if( curve_info == NULL )
2283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002284 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2285 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002286 }
2287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002288 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002289
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002290 if( mbedtls_ssl_check_curve( ssl, grp_id ) != 0 )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002291 return( -1 );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002292
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002293 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2294 MBEDTLS_DEBUG_ECDH_QP );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002295
2296 return( 0 );
2297}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002298#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2299 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2300 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
Przemek Stekiel068a6b42022-03-17 07:54:09 +01002301 ( !MBEDTLS_USE_PSA_CRYPTO &&
2302 ( MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2303 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED ) ) */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002304
Hanno Beckerbb89e272019-01-08 12:54:37 +00002305#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
2306 ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2307 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) )
2308static int ssl_parse_server_ecdh_params_psa( mbedtls_ssl_context *ssl,
2309 unsigned char **p,
2310 unsigned char *end )
2311{
2312 uint16_t tls_id;
Gilles Peskine42459802019-12-19 13:31:53 +01002313 size_t ecdh_bits = 0;
Hanno Beckerbb89e272019-01-08 12:54:37 +00002314 uint8_t ecpoint_len;
2315 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2316
2317 /*
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01002318 * struct {
2319 * ECParameters curve_params;
2320 * ECPoint public;
2321 * } ServerECDHParams;
2322 *
Manuel Pégourié-Gonnard422370d2022-02-07 11:55:21 +01002323 * 1 curve_type (must be "named_curve")
Manuel Pégourié-Gonnarde5119892021-12-09 11:45:03 +01002324 * 2..3 NamedCurve
2325 * 4 ECPoint.len
2326 * 5+ ECPoint contents
Hanno Beckerbb89e272019-01-08 12:54:37 +00002327 */
Hanno Beckerbb89e272019-01-08 12:54:37 +00002328 if( end - *p < 4 )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002329 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckerbb89e272019-01-08 12:54:37 +00002330
2331 /* First byte is curve_type; only named_curve is handled */
2332 if( *(*p)++ != MBEDTLS_ECP_TLS_NAMED_CURVE )
Hanno Becker2fc9a652021-06-24 15:40:11 +01002333 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00002334
2335 /* Next two bytes are the namedcurve value */
2336 tls_id = *(*p)++;
2337 tls_id <<= 8;
2338 tls_id |= *(*p)++;
2339
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01002340 /* Check it's a curve we offered */
2341 if( mbedtls_ssl_check_curve_tls_id( ssl, tls_id ) != 0 )
Manuel Pégourié-Gonnardff229cf2022-02-07 12:00:32 +01002342 {
2343 MBEDTLS_SSL_DEBUG_MSG( 2,
2344 ( "bad server key exchange message (ECDHE curve): %u",
2345 (unsigned) tls_id ) );
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01002346 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnardff229cf2022-02-07 12:00:32 +01002347 }
Manuel Pégourié-Gonnard141be6c2022-01-25 11:46:19 +01002348
Hanno Beckerbb89e272019-01-08 12:54:37 +00002349 /* Convert EC group to PSA key type. */
Gilles Peskine42459802019-12-19 13:31:53 +01002350 if( ( handshake->ecdh_psa_type =
2351 mbedtls_psa_parse_tls_ecc_group( tls_id, &ecdh_bits ) ) == 0 )
Hanno Beckerbb89e272019-01-08 12:54:37 +00002352 {
Hanno Becker2fc9a652021-06-24 15:40:11 +01002353 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00002354 }
Neil Armstrong91477a72022-03-25 15:42:20 +01002355 handshake->ecdh_bits = ecdh_bits;
Hanno Beckerbb89e272019-01-08 12:54:37 +00002356
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01002357 /* Keep a copy of the peer's public key */
Hanno Beckerbb89e272019-01-08 12:54:37 +00002358 ecpoint_len = *(*p)++;
2359 if( (size_t)( end - *p ) < ecpoint_len )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002360 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckerbb89e272019-01-08 12:54:37 +00002361
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01002362 if( ecpoint_len > sizeof( handshake->ecdh_psa_peerkey ) )
2363 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00002364
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01002365 memcpy( handshake->ecdh_psa_peerkey, *p, ecpoint_len );
2366 handshake->ecdh_psa_peerkey_len = ecpoint_len;
Hanno Beckerbb89e272019-01-08 12:54:37 +00002367 *p += ecpoint_len;
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01002368
Hanno Beckerbb89e272019-01-08 12:54:37 +00002369 return( 0 );
2370}
2371#endif /* MBEDTLS_USE_PSA_CRYPTO &&
2372 ( MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2373 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ) */
2374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002375#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2376 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2377 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2378static int ssl_parse_server_ecdh_params( mbedtls_ssl_context *ssl,
Paul Bakker29e1f122013-04-16 13:07:56 +02002379 unsigned char **p,
2380 unsigned char *end )
2381{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002382 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker29e1f122013-04-16 13:07:56 +02002383
Paul Bakker29e1f122013-04-16 13:07:56 +02002384 /*
2385 * Ephemeral ECDH parameters:
2386 *
2387 * struct {
2388 * ECParameters curve_params;
2389 * ECPoint public;
2390 * } ServerECDHParams;
2391 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002392 if( ( ret = mbedtls_ecdh_read_params( &ssl->handshake->ecdh_ctx,
Paul Bakker29e1f122013-04-16 13:07:56 +02002393 (const unsigned char **) p, end ) ) != 0 )
2394 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002395 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_read_params" ), ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01002396#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard1c1c20e2018-09-12 10:34:43 +02002397 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2398 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002399#endif
Paul Bakker29e1f122013-04-16 13:07:56 +02002400 return( ret );
2401 }
2402
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002403 if( ssl_check_server_ecdh_params( ssl ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02002404 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002405 MBEDTLS_SSL_DEBUG_MSG( 1,
2406 ( "bad server key exchange message (ECDHE curve)" ) );
Hanno Becker2fc9a652021-06-24 15:40:11 +01002407 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02002408 }
2409
Paul Bakker29e1f122013-04-16 13:07:56 +02002410 return( ret );
2411}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002412#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2413 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2414 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02002415
Gilles Peskineeccd8882020-03-10 12:19:08 +01002416#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002417static int ssl_parse_server_psk_hint( mbedtls_ssl_context *ssl,
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002418 unsigned char **p,
2419 unsigned char *end )
2420{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002421 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
irwir6527bd62019-09-21 18:51:25 +03002422 uint16_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02002423 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002424
2425 /*
2426 * PSK parameters:
2427 *
2428 * opaque psk_identity_hint<0..2^16-1>;
2429 */
Hanno Becker0c161d12018-10-08 13:40:50 +01002430 if( end - (*p) < 2 )
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01002431 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002432 MBEDTLS_SSL_DEBUG_MSG( 1,
2433 ( "bad server key exchange message (psk_identity_hint length)" ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002434 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01002435 }
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02002436 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002437 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002438
irwir6527bd62019-09-21 18:51:25 +03002439 if( end - (*p) < len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002440 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002441 MBEDTLS_SSL_DEBUG_MSG( 1,
2442 ( "bad server key exchange message (psk_identity_hint length)" ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002443 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002444 }
2445
Manuel Pégourié-Gonnard9d624122016-02-22 11:10:14 +01002446 /*
2447 * Note: we currently ignore the PKS identity hint, as we only allow one
2448 * PSK to be provisionned on the client. This could be changed later if
2449 * someone needs that feature.
2450 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002451 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002452 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002453
2454 return( ret );
2455}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002456#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002458#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
2459 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002460/*
2461 * Generate a pre-master secret and encrypt it with the server's RSA key
2462 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002463static int ssl_write_encrypted_pms( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002464 size_t offset, size_t *olen,
2465 size_t pms_offset )
2466{
Janos Follath865b3eb2019-12-16 11:46:15 +00002467 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002468 size_t len_bytes = 2;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002469 unsigned char *p = ssl->handshake->premaster + pms_offset;
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002470 mbedtls_pk_context * peer_pk;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002471
Angus Grattond8213d02016-05-25 20:56:48 +10002472 if( offset + len_bytes > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002473 {
2474 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small for encrypted pms" ) );
2475 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2476 }
2477
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002478 /*
2479 * Generate (part of) the pre-master as
2480 * struct {
2481 * ProtocolVersion client_version;
2482 * opaque random[46];
2483 * } PreMasterSecret;
2484 */
Ronald Cron4e263fd2022-03-15 15:54:17 +01002485 mbedtls_ssl_write_version( MBEDTLS_SSL_MAJOR_VERSION_3,
2486 MBEDTLS_SSL_MINOR_VERSION_3,
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002487 ssl->conf->transport, p );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002488
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002489 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p + 2, 46 ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002491 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002492 return( ret );
2493 }
2494
2495 ssl->handshake->pmslen = 48;
2496
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002497#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2498 peer_pk = &ssl->handshake->peer_pubkey;
2499#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002500 if( ssl->session_negotiate->peer_cert == NULL )
2501 {
Hanno Becker8273df82019-02-06 17:37:32 +00002502 /* Should never happen */
Hanno Becker62d58ed2019-02-26 11:51:06 +00002503 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker8273df82019-02-06 17:37:32 +00002504 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002505 }
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002506 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2507#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002508
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002509 /*
2510 * Now write it out, encrypted
2511 */
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002512 if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_RSA ) )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002514 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
2515 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002516 }
2517
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002518 if( ( ret = mbedtls_pk_encrypt( peer_pk,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002519 p, ssl->handshake->pmslen,
2520 ssl->out_msg + offset + len_bytes, olen,
Angus Grattond8213d02016-05-25 20:56:48 +10002521 MBEDTLS_SSL_OUT_CONTENT_LEN - offset - len_bytes,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002522 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002524 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_rsa_pkcs1_encrypt", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002525 return( ret );
2526 }
2527
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002528 if( len_bytes == 2 )
2529 {
Joe Subbiani6dd73642021-07-19 11:56:54 +01002530 MBEDTLS_PUT_UINT16_BE( *olen, ssl->out_msg, offset );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002531 *olen += 2;
2532 }
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002533
Hanno Beckerae553dd2019-02-08 14:06:00 +00002534#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2535 /* We don't need the peer's public key anymore. Free it. */
2536 mbedtls_pk_free( peer_pk );
2537#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002538 return( 0 );
2539}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002540#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
2541 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02002542
Manuel Pégourié-Gonnard5c2a7ca2015-10-23 08:48:41 +02002543#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2544 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2545 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002546static int ssl_parse_signature_algorithm( mbedtls_ssl_context *ssl,
Paul Bakker29e1f122013-04-16 13:07:56 +02002547 unsigned char **p,
2548 unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002549 mbedtls_md_type_t *md_alg,
2550 mbedtls_pk_type_t *pk_alg )
Paul Bakker29e1f122013-04-16 13:07:56 +02002551{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002552 *md_alg = MBEDTLS_MD_NONE;
2553 *pk_alg = MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002554
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002555 if( (*p) + 2 > end )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002556 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker29e1f122013-04-16 13:07:56 +02002557
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002558 /*
2559 * Get hash algorithm
2560 */
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002561 if( ( *md_alg = mbedtls_ssl_md_alg_from_hash( (*p)[0] ) )
2562 == MBEDTLS_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02002563 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002564 MBEDTLS_SSL_DEBUG_MSG( 1,
2565 ( "Server used unsupported HashAlgorithm %d", *(p)[0] ) );
Hanno Becker2fc9a652021-06-24 15:40:11 +01002566 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02002567 }
2568
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002569 /*
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002570 * Get signature algorithm
2571 */
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002572 if( ( *pk_alg = mbedtls_ssl_pk_alg_from_sig( (*p)[1] ) )
2573 == MBEDTLS_PK_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02002574 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002575 MBEDTLS_SSL_DEBUG_MSG( 1,
2576 ( "server used unsupported SignatureAlgorithm %d", (*p)[1] ) );
Dave Rodgman5f8c18b2021-06-28 11:58:00 +01002577 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02002578 }
2579
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02002580 /*
Jerry Yu24811fb2022-01-19 18:02:15 +08002581 * Check if the signature algorithm is acceptable
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02002582 */
Jerry Yu24811fb2022-01-19 18:02:15 +08002583 if( !mbedtls_ssl_sig_alg_is_offered( ssl, MBEDTLS_GET_UINT16_BE( *p, 0 ) ) )
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02002584 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002585 MBEDTLS_SSL_DEBUG_MSG( 1,
2586 ( "server used HashAlgorithm %d that was not offered", *(p)[0] ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002587 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02002588 }
2589
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002590 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d",
2591 (*p)[1] ) );
2592 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d",
2593 (*p)[0] ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02002594 *p += 2;
2595
2596 return( 0 );
2597}
Manuel Pégourié-Gonnard5c2a7ca2015-10-23 08:48:41 +02002598#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2599 MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2600 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02002601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002602#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2603 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2604static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002605{
Janos Follath865b3eb2019-12-16 11:46:15 +00002606 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002607 const mbedtls_ecp_keypair *peer_key;
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002608 mbedtls_pk_context * peer_pk;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002609
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002610#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2611 peer_pk = &ssl->handshake->peer_pubkey;
2612#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002613 if( ssl->session_negotiate->peer_cert == NULL )
2614 {
Hanno Becker8273df82019-02-06 17:37:32 +00002615 /* Should never happen */
Hanno Beckerbd5580a2019-02-26 12:36:01 +00002616 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker8273df82019-02-06 17:37:32 +00002617 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002618 }
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002619 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2620#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002621
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002622 if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_ECKEY ) )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002624 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2625 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002626 }
2627
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002628 peer_key = mbedtls_pk_ec( *peer_pk );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002629
Przemek Stekielea4000f2022-03-16 09:49:33 +01002630#if defined(MBEDTLS_USE_PSA_CRYPTO)
2631 size_t ecdh_bits = 0;
Przemek Stekiel561a4232022-03-16 13:16:24 +01002632 size_t olen = 0;
2633
2634 if( mbedtls_ssl_check_curve( ssl, peer_key->grp.id ) != 0 )
2635 {
2636 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
2637 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
2638 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002639
2640 ssl->handshake->ecdh_psa_type =
2641 PSA_KEY_TYPE_ECC_KEY_PAIR( mbedtls_ecc_group_to_psa( peer_key->grp.id,
2642 &ecdh_bits ) );
2643
2644 if( ssl->handshake->ecdh_psa_type == 0 || ecdh_bits > 0xffff )
2645 {
2646 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid ecc group conversion to psa." ) );
2647 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
2648 }
2649
2650 ssl->handshake->ecdh_bits = (uint16_t) ecdh_bits;
2651
Przemek Stekielea4000f2022-03-16 09:49:33 +01002652 /* Store peer's public key in psa format. */
Przemek Stekiel561a4232022-03-16 13:16:24 +01002653 ret = mbedtls_ecp_point_write_binary( &peer_key->grp, &peer_key->Q,
2654 MBEDTLS_ECP_PF_UNCOMPRESSED, &olen,
2655 ssl->handshake->ecdh_psa_peerkey,
2656 MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH );
Przemek Stekielea4000f2022-03-16 09:49:33 +01002657
Przemek Stekiel561a4232022-03-16 13:16:24 +01002658 if ( ret != 0 )
2659 {
2660 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecp_point_write_binary" ), ret );
2661 return( ret );
2662 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002663
Przemek Stekiel561a4232022-03-16 13:16:24 +01002664 ssl->handshake->ecdh_psa_peerkey_len = olen;
Przemek Stekielea4000f2022-03-16 09:49:33 +01002665#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002666 if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
2667 MBEDTLS_ECDH_THEIRS ) ) != 0 )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002669 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002670 return( ret );
2671 }
2672
2673 if( ssl_check_server_ecdh_params( ssl ) != 0 )
2674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002675 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
Hanno Becker9ed1ba52021-06-24 11:03:13 +01002676 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002677 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002678#endif
Hanno Beckerae553dd2019-02-08 14:06:00 +00002679#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2680 /* We don't need the peer's public key anymore. Free it,
2681 * so that more RAM is available for upcoming expensive
2682 * operations like ECDHE. */
2683 mbedtls_pk_free( peer_pk );
2684#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2685
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002686 return( ret );
2687}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002688#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2689 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002691static int ssl_parse_server_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker41c83d32013-03-20 14:39:14 +01002692{
Janos Follath865b3eb2019-12-16 11:46:15 +00002693 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002694 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002695 ssl->handshake->ciphersuite_info;
Andres Amaya Garcia53c77cc2017-06-27 16:15:06 +01002696 unsigned char *p = NULL, *end = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002698 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002700#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
2701 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002702 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002703 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002704 ssl->state++;
2705 return( 0 );
2706 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002707 ((void) p);
2708 ((void) end);
2709#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002711#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2712 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2713 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2714 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002715 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002716 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
2717 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002718 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002719 mbedtls_ssl_send_alert_message(
2720 ssl,
2721 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2722 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002723 return( ret );
2724 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002726 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002727 ssl->state++;
2728 return( 0 );
2729 }
2730 ((void) p);
2731 ((void) end);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002732#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2733 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002734
Gilles Peskineeccd8882020-03-10 12:19:08 +01002735#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002736 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002737 ssl->handshake->ecrs_state == ssl_ecrs_ske_start_processing )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002738 {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002739 goto start_processing;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002740 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002741#endif
2742
Hanno Becker327c93b2018-08-15 13:56:18 +01002743 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002745 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002746 return( ret );
2747 }
2748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002749 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002750 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002751 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002752 mbedtls_ssl_send_alert_message(
2753 ssl,
2754 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2755 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002756 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002757 }
2758
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002759 /*
2760 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
2761 * doesn't use a psk_identity_hint
2762 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002763 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002764 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002765 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2766 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Paul Bakker188c8de2013-04-19 09:13:37 +02002767 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002768 /* Current message is probably either
2769 * CertificateRequest or ServerHelloDone */
2770 ssl->keep_current_message = 1;
Paul Bakker188c8de2013-04-19 09:13:37 +02002771 goto exit;
2772 }
2773
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002774 MBEDTLS_SSL_DEBUG_MSG( 1,
2775 ( "server key exchange message must not be skipped" ) );
2776 mbedtls_ssl_send_alert_message(
2777 ssl,
2778 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2779 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002781 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002782 }
2783
Gilles Peskineeccd8882020-03-10 12:19:08 +01002784#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002785 if( ssl->handshake->ecrs_enabled )
2786 ssl->handshake->ecrs_state = ssl_ecrs_ske_start_processing;
2787
2788start_processing:
2789#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002790 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002791 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002792 MBEDTLS_SSL_DEBUG_BUF( 3, "server key exchange", p, end - p );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002793
Gilles Peskineeccd8882020-03-10 12:19:08 +01002794#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002795 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2796 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
2797 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
2798 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002799 {
2800 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
2801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002802 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002803 mbedtls_ssl_send_alert_message(
2804 ssl,
2805 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman8f127392021-06-28 12:02:21 +01002806 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002807 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002808 }
2809 } /* FALLTROUGH */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002810#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002812#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \
2813 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2814 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2815 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002816 ; /* nothing more to do */
2817 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002818#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED ||
2819 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2820#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2821 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2822 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ||
2823 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002824 {
Paul Bakker29e1f122013-04-16 13:07:56 +02002825 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002826 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002827 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002828 mbedtls_ssl_send_alert_message(
2829 ssl,
2830 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2831 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Dave Rodgmanbed89272021-06-29 12:06:32 +01002832 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002833 }
2834 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002835 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002836#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2837 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Hanno Beckerbb89e272019-01-08 12:54:37 +00002838#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
2839 ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2840 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) )
2841 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2842 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
2843 {
2844 if( ssl_parse_server_ecdh_params_psa( ssl, &p, end ) != 0 )
2845 {
2846 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002847 mbedtls_ssl_send_alert_message(
2848 ssl,
2849 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2850 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Dave Rodgmanc50b7172021-06-29 14:40:23 +01002851 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Hanno Beckerbb89e272019-01-08 12:54:37 +00002852 }
2853 }
2854 else
2855#endif /* MBEDTLS_USE_PSA_CRYPTO &&
2856 ( MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2857 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002858#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2859 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
2860 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2861 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2862 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
2863 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002864 {
2865 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
2866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002867 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002868 mbedtls_ssl_send_alert_message(
2869 ssl,
2870 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2871 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Dave Rodgman39bd5a62021-06-29 15:25:21 +01002872 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker41c83d32013-03-20 14:39:14 +01002873 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002874 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002875 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002876#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2877 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
2878 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002879#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2880 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2881 {
2882 ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx,
2883 p, end - p );
2884 if( ret != 0 )
2885 {
2886 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002887 mbedtls_ssl_send_alert_message(
2888 ssl,
2889 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Becker77b4a652021-06-24 16:27:09 +01002890 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
2891 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002892 }
2893 }
2894 else
2895#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002896 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002897 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2898 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002899 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002900
Gilles Peskineeccd8882020-03-10 12:19:08 +01002901#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01002902 if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002903 {
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002904 size_t sig_len, hashlen;
Ronald Cron69a63422021-10-18 09:47:58 +02002905#if defined(MBEDTLS_USE_PSA_CRYPTO)
2906 unsigned char hash[PSA_HASH_MAX_SIZE];
2907#else
2908 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2909#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002910 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
2911 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
2912 unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002913 size_t params_len = p - params;
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002914 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002915
Hanno Beckera6899bb2019-02-06 18:26:03 +00002916 mbedtls_pk_context * peer_pk;
2917
Paul Bakker29e1f122013-04-16 13:07:56 +02002918 /*
2919 * Handle the digitally-signed structure
2920 */
Ronald Cron90915f22022-03-07 11:11:36 +01002921 if( ssl_parse_signature_algorithm( ssl, &p, end,
2922 &md_alg, &pk_alg ) != 0 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002923 {
Ronald Cron90915f22022-03-07 11:11:36 +01002924 MBEDTLS_SSL_DEBUG_MSG( 1,
2925 ( "bad server key exchange message" ) );
2926 mbedtls_ssl_send_alert_message(
2927 ssl,
2928 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2929 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
2930 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker29e1f122013-04-16 13:07:56 +02002931 }
Ronald Cron90915f22022-03-07 11:11:36 +01002932
2933 if( pk_alg !=
2934 mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
Paul Bakker9659dae2013-08-28 16:21:34 +02002935 {
Ronald Cron90915f22022-03-07 11:11:36 +01002936 MBEDTLS_SSL_DEBUG_MSG( 1,
2937 ( "bad server key exchange message" ) );
2938 mbedtls_ssl_send_alert_message(
2939 ssl,
2940 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2941 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
2942 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker9659dae2013-08-28 16:21:34 +02002943 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002944
2945 /*
2946 * Read signature
2947 */
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002948
2949 if( p > end - 2 )
2950 {
2951 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002952 mbedtls_ssl_send_alert_message(
2953 ssl,
2954 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2955 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002956 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002957 }
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002958 sig_len = ( p[0] << 8 ) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00002959 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002960
Krzysztof Stachowiak027f84c2018-03-13 11:29:24 +01002961 if( p != end - sig_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01002962 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002963 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002964 mbedtls_ssl_send_alert_message(
2965 ssl,
2966 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2967 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002968 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker41c83d32013-03-20 14:39:14 +01002969 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002971 MBEDTLS_SSL_DEBUG_BUF( 3, "signature", p, sig_len );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002972
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002973 /*
2974 * Compute the hash that has been signed
2975 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002976 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02002977 {
Gilles Peskineca1d7422018-04-24 11:53:22 +02002978 ret = mbedtls_ssl_get_key_exchange_md_tls1_2( ssl, hash, &hashlen,
2979 params, params_len,
2980 md_alg );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01002981 if( ret != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02002982 return( ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02002983 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002984 else
Paul Bakker29e1f122013-04-16 13:07:56 +02002985 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002986 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2987 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002988 }
Paul Bakker29e1f122013-04-16 13:07:56 +02002989
Gilles Peskineca1d7422018-04-24 11:53:22 +02002990 MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen );
Paul Bakker29e1f122013-04-16 13:07:56 +02002991
Hanno Beckera6899bb2019-02-06 18:26:03 +00002992#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2993 peer_pk = &ssl->handshake->peer_pubkey;
2994#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002995 if( ssl->session_negotiate->peer_cert == NULL )
2996 {
Hanno Becker8273df82019-02-06 17:37:32 +00002997 /* Should never happen */
Hanno Beckerbd5580a2019-02-26 12:36:01 +00002998 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker8273df82019-02-06 17:37:32 +00002999 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02003000 }
Hanno Beckera6899bb2019-02-06 18:26:03 +00003001 peer_pk = &ssl->session_negotiate->peer_cert->pk;
3002#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02003003
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02003004 /*
3005 * Verify signature
3006 */
Hanno Beckera6899bb2019-02-06 18:26:03 +00003007 if( !mbedtls_pk_can_do( peer_pk, pk_alg ) )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02003008 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003009 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003010 mbedtls_ssl_send_alert_message(
3011 ssl,
3012 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3013 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003014 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02003015 }
3016
Gilles Peskineeccd8882020-03-10 12:19:08 +01003017#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003018 if( ssl->handshake->ecrs_enabled )
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003019 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02003020#endif
3021
Hanno Beckera6899bb2019-02-06 18:26:03 +00003022 if( ( ret = mbedtls_pk_verify_restartable( peer_pk,
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02003023 md_alg, hash, hashlen, p, sig_len, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02003024 {
Gilles Peskineeccd8882020-03-10 12:19:08 +01003025#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02003026 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3027#endif
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003028 mbedtls_ssl_send_alert_message(
3029 ssl,
3030 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3031 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003032 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01003033#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003034 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3035 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
3036#endif
Paul Bakkerc70b9822013-04-07 22:00:46 +02003037 return( ret );
Paul Bakkerc3f177a2012-04-11 16:11:49 +00003038 }
Hanno Beckerae553dd2019-02-08 14:06:00 +00003039
3040#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3041 /* We don't need the peer's public key anymore. Free it,
3042 * so that more RAM is available for upcoming expensive
3043 * operations like ECDHE. */
3044 mbedtls_pk_free( peer_pk );
3045#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakker5121ce52009-01-03 21:22:43 +00003046 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003047#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003048
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003049exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00003050 ssl->state++;
3051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003052 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003053
3054 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003055}
3056
Gilles Peskineeccd8882020-03-10 12:19:08 +01003057#if ! defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003058static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01003059{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003060 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003061 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01003062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003063 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01003064
Hanno Becker1aa267c2017-04-28 17:08:27 +01003065 if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01003066 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003067 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01003068 ssl->state++;
3069 return( 0 );
3070 }
3071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003072 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3073 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01003074}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003075#else /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003076static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003077{
Janos Follath865b3eb2019-12-16 11:46:15 +00003078 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00003079 unsigned char *buf;
3080 size_t n = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003081 size_t cert_type_len = 0, dn_len = 0;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003082 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003083 ssl->handshake->ciphersuite_info;
Ronald Cron90915f22022-03-07 11:11:36 +01003084 size_t sig_alg_len;
3085#if defined(MBEDTLS_DEBUG_C)
3086 unsigned char *sig_alg;
3087#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003089 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003090
Hanno Becker1aa267c2017-04-28 17:08:27 +01003091 if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01003092 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003093 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01003094 ssl->state++;
3095 return( 0 );
3096 }
3097
Hanno Becker327c93b2018-08-15 13:56:18 +01003098 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003099 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003100 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
3101 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003102 }
3103
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003104 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
3105 {
3106 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003107 mbedtls_ssl_send_alert_message(
3108 ssl,
3109 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3110 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003111 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
3112 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003113
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003114 ssl->state++;
Jerry Yufb28b882022-01-28 11:05:58 +08003115 ssl->handshake->client_auth =
3116 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST );
Paul Bakker5121ce52009-01-03 21:22:43 +00003117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003118 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
Jerry Yufb28b882022-01-28 11:05:58 +08003119 ssl->handshake->client_auth ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003120
Jerry Yufb28b882022-01-28 11:05:58 +08003121 if( ssl->handshake->client_auth == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003122 {
Johan Pascala89ca862020-08-25 10:03:19 +02003123 /* Current message is probably the ServerHelloDone */
3124 ssl->keep_current_message = 1;
Paul Bakker926af752012-11-23 13:38:07 +01003125 goto exit;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003126 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003127
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02003128 /*
3129 * struct {
3130 * ClientCertificateType certificate_types<1..2^8-1>;
3131 * SignatureAndHashAlgorithm
3132 * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
3133 * DistinguishedName certificate_authorities<0..2^16-1>;
3134 * } CertificateRequest;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00003135 *
3136 * Since we only support a single certificate on clients, let's just
3137 * ignore all the information that's supposed to help us pick a
3138 * certificate.
3139 *
3140 * We could check that our certificate matches the request, and bail out
3141 * if it doesn't, but it's simpler to just send the certificate anyway,
3142 * and give the server the opportunity to decide if it should terminate
3143 * the connection when it doesn't like our certificate.
3144 *
3145 * Same goes for the hash in TLS 1.2's signature_algorithms: at this
3146 * point we only have one hash available (see comments in
Simon Butcherc0957bd2016-03-01 13:16:57 +00003147 * write_certificate_verify), so let's just use what we have.
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00003148 *
3149 * However, we still minimally parse the message to check it is at least
3150 * superficially sane.
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02003151 */
Paul Bakker926af752012-11-23 13:38:07 +01003152 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003153
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00003154 /* certificate_types */
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02003155 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) )
3156 {
3157 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
3158 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3159 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01003160 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02003161 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003162 cert_type_len = buf[mbedtls_ssl_hs_hdr_len( ssl )];
Paul Bakker926af752012-11-23 13:38:07 +01003163 n = cert_type_len;
3164
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01003165 /*
Krzysztof Stachowiak94d49972018-04-05 14:48:55 +02003166 * In the subsequent code there are two paths that read from buf:
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01003167 * * the length of the signature algorithms field (if minor version of
3168 * SSL is 3),
3169 * * distinguished name length otherwise.
3170 * Both reach at most the index:
3171 * ...hdr_len + 2 + n,
3172 * therefore the buffer length at this point must be greater than that
3173 * regardless of the actual code path.
3174 */
3175 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n )
Paul Bakker926af752012-11-23 13:38:07 +01003176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003177 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003178 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3179 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01003180 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker926af752012-11-23 13:38:07 +01003181 }
3182
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00003183 /* supported_signature_algorithms */
Ronald Cron90915f22022-03-07 11:11:36 +01003184 sig_alg_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
3185 | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
3186
3187 /*
3188 * The furthest access in buf is in the loop few lines below:
3189 * sig_alg[i + 1],
3190 * where:
3191 * sig_alg = buf + ...hdr_len + 3 + n,
3192 * max(i) = sig_alg_len - 1.
3193 * Therefore the furthest access is:
3194 * buf[...hdr_len + 3 + n + sig_alg_len - 1 + 1],
3195 * which reduces to:
3196 * buf[...hdr_len + 3 + n + sig_alg_len],
3197 * which is one less than we need the buf to be.
3198 */
3199 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n + sig_alg_len )
Paul Bakker926af752012-11-23 13:38:07 +01003200 {
Ronald Cron90915f22022-03-07 11:11:36 +01003201 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
3202 mbedtls_ssl_send_alert_message(
3203 ssl,
3204 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3205 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
3206 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerf7abd422013-04-16 13:15:56 +02003207 }
Paul Bakker926af752012-11-23 13:38:07 +01003208
Ronald Cron90915f22022-03-07 11:11:36 +01003209#if defined(MBEDTLS_DEBUG_C)
3210 sig_alg = buf + mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n;
3211 for( size_t i = 0; i < sig_alg_len; i += 2 )
3212 {
3213 MBEDTLS_SSL_DEBUG_MSG( 3,
3214 ( "Supported Signature Algorithm found: %d,%d",
3215 sig_alg[i], sig_alg[i + 1] ) );
3216 }
3217#endif
3218
3219 n += 2 + sig_alg_len;
3220
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00003221 /* certificate_authorities */
3222 dn_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
3223 | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01003224
3225 n += dn_len;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00003226 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n )
Paul Bakker926af752012-11-23 13:38:07 +01003227 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003228 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003229 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3230 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01003231 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker926af752012-11-23 13:38:07 +01003232 }
3233
3234exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003235 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003236
3237 return( 0 );
3238}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003239#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003241static int ssl_parse_server_hello_done( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003242{
Janos Follath865b3eb2019-12-16 11:46:15 +00003243 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003245 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003246
Hanno Becker327c93b2018-08-15 13:56:18 +01003247 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003248 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003249 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
3250 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003251 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003252
3253 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
3254 {
3255 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
3256 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
3257 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003259 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ||
3260 ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003262 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003263 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3264 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker029cc2f2021-06-24 10:09:50 +01003265 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +00003266 }
3267
3268 ssl->state++;
3269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003270#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003271 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003272 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003273#endif
3274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003275 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003276
3277 return( 0 );
3278}
3279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003280static int ssl_write_client_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003281{
Janos Follath865b3eb2019-12-16 11:46:15 +00003282 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003283
3284 size_t header_len;
3285 size_t content_len;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003286 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003287 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003289 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003291#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
3292 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003293 {
Paul Bakker5121ce52009-01-03 21:22:43 +00003294 /*
3295 * DHM key exchange -- send G^X mod P
3296 */
Gilles Peskine487bbf62021-05-27 22:17:07 +02003297 content_len = mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00003298
Joe Subbiani6dd73642021-07-19 11:56:54 +01003299 MBEDTLS_PUT_UINT16_BE( content_len, ssl->out_msg, 4 );
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003300 header_len = 6;
Paul Bakker5121ce52009-01-03 21:22:43 +00003301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003302 ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
Gilles Peskine487bbf62021-05-27 22:17:07 +02003303 (int) mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx ),
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003304 &ssl->out_msg[header_len], content_len,
3305 ssl->conf->f_rng, ssl->conf->p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +00003306 if( ret != 0 )
3307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003308 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003309 return( ret );
3310 }
3311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003312 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
3313 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker5121ce52009-01-03 21:22:43 +00003314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003315 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003316 ssl->handshake->premaster,
3317 MBEDTLS_PREMASTER_SIZE,
3318 &ssl->handshake->pmslen,
3319 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003321 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003322 return( ret );
3323 }
3324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003325 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker5121ce52009-01-03 21:22:43 +00003326 }
3327 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003328#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Hanno Becker4a63ed42019-01-08 11:39:35 +00003329#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
3330 ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Przemek Stekield905d332022-03-16 09:50:56 +01003331 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
3332 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3333 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) )
Hanno Becker4a63ed42019-01-08 11:39:35 +00003334 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Przemek Stekield905d332022-03-16 09:50:56 +01003335 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
3336 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
3337 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Hanno Becker4a63ed42019-01-08 11:39:35 +00003338 {
Andrzej Kureka0237f82022-02-24 13:24:52 -05003339 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3340 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
Janos Follath53b8ec22019-08-08 10:28:27 +01003341 psa_key_attributes_t key_attributes;
Hanno Becker4a63ed42019-01-08 11:39:35 +00003342
3343 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3344
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003345 header_len = 4;
Hanno Becker4a63ed42019-01-08 11:39:35 +00003346
Hanno Becker0a94a642019-01-11 14:35:30 +00003347 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
3348
Hanno Becker4a63ed42019-01-08 11:39:35 +00003349 /*
3350 * Generate EC private key for ECDHE exchange.
3351 */
3352
Hanno Becker4a63ed42019-01-08 11:39:35 +00003353 /* The master secret is obtained from the shared ECDH secret by
3354 * applying the TLS 1.2 PRF with a specific salt and label. While
3355 * the PSA Crypto API encourages combining key agreement schemes
3356 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
3357 * yet support the provisioning of salt + label to the KDF.
3358 * For the time being, we therefore need to split the computation
3359 * of the ECDH secret and the application of the TLS 1.2 PRF. */
Janos Follath53b8ec22019-08-08 10:28:27 +01003360 key_attributes = psa_key_attributes_init();
3361 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
Janos Follathdf3b0892019-08-08 11:12:24 +01003362 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
Gilles Peskine42459802019-12-19 13:31:53 +01003363 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
3364 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003365
3366 /* Generate ECDH private key. */
Janos Follath53b8ec22019-08-08 10:28:27 +01003367 status = psa_generate_key( &key_attributes,
Janos Follathdf3b0892019-08-08 11:12:24 +01003368 &handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003369 if( status != PSA_SUCCESS )
3370 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
3371
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01003372 /* Export the public part of the ECDH private key from PSA.
Manuel Pégourié-Gonnard5d6053f2022-02-08 10:26:19 +01003373 * The export format is an ECPoint structure as expected by TLS,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01003374 * but we just need to add a length byte before that. */
3375 unsigned char *own_pubkey = ssl->out_msg + header_len + 1;
3376 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
3377 size_t own_pubkey_max_len = (size_t)( end - own_pubkey );
3378 size_t own_pubkey_len;
3379
Hanno Becker4a63ed42019-01-08 11:39:35 +00003380 status = psa_export_public_key( handshake->ecdh_psa_privkey,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01003381 own_pubkey, own_pubkey_max_len,
Hanno Becker4a63ed42019-01-08 11:39:35 +00003382 &own_pubkey_len );
3383 if( status != PSA_SUCCESS )
Andrzej Kureka0237f82022-02-24 13:24:52 -05003384 {
3385 psa_destroy_key( handshake->ecdh_psa_privkey );
3386 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Becker4a63ed42019-01-08 11:39:35 +00003387 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kureka0237f82022-02-24 13:24:52 -05003388 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00003389
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01003390 ssl->out_msg[header_len] = (unsigned char) own_pubkey_len;
3391 content_len = own_pubkey_len + 1;
Hanno Becker4a63ed42019-01-08 11:39:35 +00003392
Hanno Becker4a63ed42019-01-08 11:39:35 +00003393 /* The ECDH secret is the premaster secret used for key derivation. */
3394
Janos Follathdf3b0892019-08-08 11:12:24 +01003395 /* Compute ECDH shared secret. */
3396 status = psa_raw_key_agreement( PSA_ALG_ECDH,
3397 handshake->ecdh_psa_privkey,
3398 handshake->ecdh_psa_peerkey,
3399 handshake->ecdh_psa_peerkey_len,
3400 ssl->handshake->premaster,
3401 sizeof( ssl->handshake->premaster ),
3402 &ssl->handshake->pmslen );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003403
Andrzej Kureka0237f82022-02-24 13:24:52 -05003404 destruction_status = psa_destroy_key( handshake->ecdh_psa_privkey );
Ronald Croncf56a0a2020-08-04 09:51:30 +02003405 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kureka0237f82022-02-24 13:24:52 -05003406
3407 if( status != PSA_SUCCESS || destruction_status != PSA_SUCCESS )
3408 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003409 }
3410 else
3411#endif /* MBEDTLS_USE_PSA_CRYPTO &&
3412 ( MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Przemek Stekield905d332022-03-16 09:50:56 +01003413 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
3414 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3415 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003416#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
3417 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
3418 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3419 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
3420 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
3421 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
3422 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
3423 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01003424 {
3425 /*
3426 * ECDH key exchange -- send client public value
3427 */
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003428 header_len = 4;
Paul Bakker41c83d32013-03-20 14:39:14 +01003429
Gilles Peskineeccd8882020-03-10 12:19:08 +01003430#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003431 if( ssl->handshake->ecrs_enabled )
3432 {
Manuel Pégourié-Gonnardc37423f2018-10-16 10:28:17 +02003433 if( ssl->handshake->ecrs_state == ssl_ecrs_cke_ecdh_calc_secret )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003434 goto ecdh_calc_secret;
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +02003435
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003436 mbedtls_ecdh_enable_restart( &ssl->handshake->ecdh_ctx );
3437 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02003438#endif
3439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003440 ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx,
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003441 &content_len,
3442 &ssl->out_msg[header_len], 1000,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003443 ssl->conf->f_rng, ssl->conf->p_rng );
Paul Bakker41c83d32013-03-20 14:39:14 +01003444 if( ret != 0 )
3445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003446 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01003447#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003448 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3449 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
3450#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01003451 return( ret );
3452 }
3453
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003454 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3455 MBEDTLS_DEBUG_ECDH_Q );
Paul Bakker41c83d32013-03-20 14:39:14 +01003456
Gilles Peskineeccd8882020-03-10 12:19:08 +01003457#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003458 if( ssl->handshake->ecrs_enabled )
3459 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003460 ssl->handshake->ecrs_n = content_len;
Manuel Pégourié-Gonnardc37423f2018-10-16 10:28:17 +02003461 ssl->handshake->ecrs_state = ssl_ecrs_cke_ecdh_calc_secret;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003462 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02003463
3464ecdh_calc_secret:
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003465 if( ssl->handshake->ecrs_enabled )
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003466 content_len = ssl->handshake->ecrs_n;
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02003467#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003468 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003469 &ssl->handshake->pmslen,
3470 ssl->handshake->premaster,
3471 MBEDTLS_MPI_MAX_SIZE,
3472 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003474 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01003475#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003476 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3477 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
3478#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01003479 return( ret );
3480 }
3481
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003482 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3483 MBEDTLS_DEBUG_ECDH_Z );
Paul Bakker41c83d32013-03-20 14:39:14 +01003484 }
3485 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003486#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
3487 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
3488 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3489 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003490#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003491 if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003492 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003493 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003494 * opaque psk_identity<0..2^16-1>;
3495 */
Hanno Becker520224e2018-10-26 11:38:07 +01003496 if( ssl_conf_has_static_psk( ssl->conf ) == 0 )
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003497 {
Hanno Becker2e4f6162018-10-23 11:54:44 +01003498 /* We don't offer PSK suites if we don't have a PSK,
3499 * and we check that the server's choice is among the
3500 * ciphersuites we offered, so this should never happen. */
3501 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003502 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003503
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003504 header_len = 4;
3505 content_len = ssl->conf->psk_identity_len;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003506
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003507 if( header_len + 2 + content_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003508 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003509 MBEDTLS_SSL_DEBUG_MSG( 1,
3510 ( "psk identity too long or SSL buffer too short" ) );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003511 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3512 }
3513
Joe Subbianifbeb6922021-07-16 14:27:50 +01003514 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1( content_len );
3515 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0( content_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003516
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003517 memcpy( ssl->out_msg + header_len,
3518 ssl->conf->psk_identity,
3519 ssl->conf->psk_identity_len );
3520 header_len += ssl->conf->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003522#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
3523 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003524 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003525 content_len = 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003526 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003527 else
3528#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003529#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
3530 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003531 {
Hanno Beckerdfab8e22018-10-23 11:59:34 +01003532#if defined(MBEDTLS_USE_PSA_CRYPTO)
3533 /* Opaque PSKs are currently only supported for PSK-only suites. */
Hanno Becker520224e2018-10-26 11:38:07 +01003534 if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
Hanno Beckerdfab8e22018-10-23 11:59:34 +01003535 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3536#endif /* MBEDTLS_USE_PSA_CRYPTO */
3537
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003538 if( ( ret = ssl_write_encrypted_pms( ssl, header_len,
3539 &content_len, 2 ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003540 return( ret );
3541 }
3542 else
3543#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003544#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3545 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003546 {
Hanno Beckerdfab8e22018-10-23 11:59:34 +01003547#if defined(MBEDTLS_USE_PSA_CRYPTO)
3548 /* Opaque PSKs are currently only supported for PSK-only suites. */
Hanno Becker520224e2018-10-26 11:38:07 +01003549 if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
Hanno Beckerdfab8e22018-10-23 11:59:34 +01003550 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3551#endif /* MBEDTLS_USE_PSA_CRYPTO */
3552
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003553 /*
3554 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
3555 */
Gilles Peskine487bbf62021-05-27 22:17:07 +02003556 content_len = mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003557
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003558 if( header_len + 2 + content_len >
3559 MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003560 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003561 MBEDTLS_SSL_DEBUG_MSG( 1,
3562 ( "psk identity or DHM size too long or SSL buffer too short" ) );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003563 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3564 }
3565
Joe Subbianifbeb6922021-07-16 14:27:50 +01003566 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1( content_len );
3567 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0( content_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003569 ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
Gilles Peskine487bbf62021-05-27 22:17:07 +02003570 (int) mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx ),
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003571 &ssl->out_msg[header_len], content_len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003572 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003573 if( ret != 0 )
3574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003575 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003576 return( ret );
3577 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003578 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003579 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003580#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
3581#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3582 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003583 {
Hanno Beckerdfab8e22018-10-23 11:59:34 +01003584#if defined(MBEDTLS_USE_PSA_CRYPTO)
3585 /* Opaque PSKs are currently only supported for PSK-only suites. */
Hanno Becker520224e2018-10-26 11:38:07 +01003586 if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
Hanno Beckerdfab8e22018-10-23 11:59:34 +01003587 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3588#endif /* MBEDTLS_USE_PSA_CRYPTO */
3589
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003590 /*
3591 * ClientECDiffieHellmanPublic public;
3592 */
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003593 ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx,
3594 &content_len,
3595 &ssl->out_msg[header_len],
3596 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003597 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003598 if( ret != 0 )
3599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003600 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003601 return( ret );
3602 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003603
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003604 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3605 MBEDTLS_DEBUG_ECDH_Q );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003606 }
3607 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003608#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003610 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3611 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003612 }
3613
Hanno Beckerafd311e2018-10-23 15:26:40 +01003614#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
3615 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
3616 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
Hanno Becker520224e2018-10-26 11:38:07 +01003617 ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
Hanno Beckerafd311e2018-10-23 15:26:40 +01003618 {
Ronald Cron5ee57072020-06-11 09:34:06 +02003619 MBEDTLS_SSL_DEBUG_MSG( 1,
3620 ( "skip PMS generation for opaque PSK" ) );
Hanno Beckerafd311e2018-10-23 15:26:40 +01003621 }
3622 else
3623#endif /* MBEDTLS_USE_PSA_CRYPTO &&
3624 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003625 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003626 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003627 {
Ronald Cron5ee57072020-06-11 09:34:06 +02003628 MBEDTLS_SSL_DEBUG_RET( 1,
3629 "mbedtls_ssl_psk_derive_premaster", ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003630 return( ret );
3631 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003632 }
3633 else
Gilles Peskineeccd8882020-03-10 12:19:08 +01003634#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003635#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
3636 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003637 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003638 header_len = 4;
3639 if( ( ret = ssl_write_encrypted_pms( ssl, header_len,
3640 &content_len, 0 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00003641 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003642 }
Paul Bakkered27a042013-04-18 22:46:23 +02003643 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003644#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003645#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3646 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
3647 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003648 header_len = 4;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003649
3650 ret = mbedtls_ecjpake_write_round_two( &ssl->handshake->ecjpake_ctx,
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003651 ssl->out_msg + header_len,
3652 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
3653 &content_len,
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003654 ssl->conf->f_rng, ssl->conf->p_rng );
3655 if( ret != 0 )
3656 {
3657 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret );
3658 return( ret );
3659 }
3660
3661 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
3662 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
3663 ssl->conf->f_rng, ssl->conf->p_rng );
3664 if( ret != 0 )
3665 {
3666 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
3667 return( ret );
3668 }
3669 }
3670 else
3671#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02003672 {
3673 ((void) ciphersuite_info);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003674 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3675 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkered27a042013-04-18 22:46:23 +02003676 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003677
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003678 ssl->out_msglen = header_len + content_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003679 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3680 ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003681
3682 ssl->state++;
3683
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003684 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003685 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003686 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003687 return( ret );
3688 }
3689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003690 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003691
3692 return( 0 );
3693}
3694
Gilles Peskineeccd8882020-03-10 12:19:08 +01003695#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003696static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003697{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003698 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003699 ssl->handshake->ciphersuite_info;
Janos Follath865b3eb2019-12-16 11:46:15 +00003700 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003702 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003704 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003705 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003706 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003707 return( ret );
3708 }
3709
Hanno Becker77adddc2019-02-07 12:32:43 +00003710 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakkered27a042013-04-18 22:46:23 +02003711 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003712 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
Paul Bakkered27a042013-04-18 22:46:23 +02003713 ssl->state++;
3714 return( 0 );
3715 }
3716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003717 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3718 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003719}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003720#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003721static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003722{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003723 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003724 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003725 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003726 size_t n = 0, offset = 0;
3727 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003728 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003729 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02003730 size_t hashlen;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003731 void *rs_ctx = NULL;
Gilles Peskinef00f1522021-06-22 00:09:00 +02003732#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3733 size_t out_buf_len = ssl->out_buf_len - ( ssl->out_msg - ssl->out_buf );
3734#else
3735 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN - ( ssl->out_msg - ssl->out_buf );
3736#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003738 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003739
Gilles Peskineeccd8882020-03-10 12:19:08 +01003740#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003741 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003742 ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003743 {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003744 goto sign;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003745 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003746#endif
3747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003748 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003750 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003751 return( ret );
3752 }
3753
Hanno Becker77adddc2019-02-07 12:32:43 +00003754 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003755 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003756 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003757 ssl->state++;
3758 return( 0 );
3759 }
3760
Jerry Yufb28b882022-01-28 11:05:58 +08003761 if( ssl->handshake->client_auth == 0 ||
3762 mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003763 {
Johan Pascala89ca862020-08-25 10:03:19 +02003764 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
3765 ssl->state++;
3766 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003767 }
3768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003769 if( mbedtls_ssl_own_key( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003770 {
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003771 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key for certificate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003772 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003773 }
3774
3775 /*
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003776 * Make a signature of the handshake digests
Paul Bakker5121ce52009-01-03 21:22:43 +00003777 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003778#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003779 if( ssl->handshake->ecrs_enabled )
3780 ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign;
3781
3782sign:
3783#endif
3784
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02003785 ssl->handshake->calc_verify( ssl, hash, &hashlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003786
Ronald Cron90915f22022-03-07 11:11:36 +01003787 /*
3788 * digitally-signed struct {
3789 * opaque handshake_messages[handshake_messages_length];
3790 * };
3791 *
3792 * Taking shortcut here. We assume that the server always allows the
3793 * PRF Hash function and has sent it in the allowed signature
3794 * algorithms list received in the Certificate Request message.
3795 *
3796 * Until we encounter a server that does not, we will take this
3797 * shortcut.
3798 *
3799 * Reason: Otherwise we should have running hashes for SHA512 and
3800 * SHA224 in order to satisfy 'weird' needs from the server
3801 * side.
3802 */
3803 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01003804 {
Ronald Cron90915f22022-03-07 11:11:36 +01003805 md_alg = MBEDTLS_MD_SHA384;
3806 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003807 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003808 else
Paul Bakker577e0062013-08-28 11:57:20 +02003809 {
Ronald Cron90915f22022-03-07 11:11:36 +01003810 md_alg = MBEDTLS_MD_SHA256;
3811 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256;
Paul Bakker577e0062013-08-28 11:57:20 +02003812 }
Ronald Cron90915f22022-03-07 11:11:36 +01003813 ssl->out_msg[5] = mbedtls_ssl_sig_from_pk( mbedtls_ssl_own_key( ssl ) );
3814
3815 /* Info from md_alg will be used instead */
3816 hashlen = 0;
3817 offset = 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003818
Gilles Peskineeccd8882020-03-10 12:19:08 +01003819#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003820 if( ssl->handshake->ecrs_enabled )
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003821 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003822#endif
3823
3824 if( ( ret = mbedtls_pk_sign_restartable( mbedtls_ssl_own_key( ssl ),
3825 md_alg, hash_start, hashlen,
Gilles Peskinef00f1522021-06-22 00:09:00 +02003826 ssl->out_msg + 6 + offset,
3827 out_buf_len - 6 - offset,
3828 &n,
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003829 ssl->conf->f_rng, ssl->conf->p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02003830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003831 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01003832#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003833 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3834 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
3835#endif
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003836 return( ret );
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02003837 }
Paul Bakker926af752012-11-23 13:38:07 +01003838
Joe Subbiani6dd73642021-07-19 11:56:54 +01003839 MBEDTLS_PUT_UINT16_BE( n, ssl->out_msg, offset + 4 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003840
Paul Bakker1ef83d62012-04-11 12:09:53 +00003841 ssl->out_msglen = 6 + n + offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003842 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3843 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003844
3845 ssl->state++;
3846
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003847 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003848 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003849 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003850 return( ret );
3851 }
3852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003853 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003854
Paul Bakkered27a042013-04-18 22:46:23 +02003855 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003856}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003857#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003859#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3860static int ssl_parse_new_session_ticket( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003861{
Janos Follath865b3eb2019-12-16 11:46:15 +00003862 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003863 uint32_t lifetime;
3864 size_t ticket_len;
3865 unsigned char *ticket;
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003866 const unsigned char *msg;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003868 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003869
Hanno Becker327c93b2018-08-15 13:56:18 +01003870 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003872 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003873 return( ret );
3874 }
3875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003876 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003877 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003878 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003879 mbedtls_ssl_send_alert_message(
3880 ssl,
3881 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3882 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003883 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003884 }
3885
3886 /*
3887 * struct {
3888 * uint32 ticket_lifetime_hint;
3889 * opaque ticket<0..2^16-1>;
3890 * } NewSessionTicket;
3891 *
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003892 * 0 . 3 ticket_lifetime_hint
3893 * 4 . 5 ticket_len (n)
3894 * 6 . 5+n ticket content
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003895 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003896 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET ||
3897 ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003899 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003900 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3901 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker241c1972021-06-24 09:44:26 +01003902 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003903 }
3904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003905 msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003906
Philippe Antoineb5b25432018-05-11 11:06:29 +02003907 lifetime = ( ((uint32_t) msg[0]) << 24 ) | ( msg[1] << 16 ) |
3908 ( msg[2] << 8 ) | ( msg[3] );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003909
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003910 ticket_len = ( msg[4] << 8 ) | ( msg[5] );
3911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003912 if( ticket_len + 6 + mbedtls_ssl_hs_hdr_len( ssl ) != ssl->in_hslen )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003913 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003914 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003915 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3916 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker241c1972021-06-24 09:44:26 +01003917 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003918 }
3919
Paul Elliottd48d5c62021-01-07 14:47:05 +00003920 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %" MBEDTLS_PRINTF_SIZET, ticket_len ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003921
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003922 /* We're not waiting for a NewSessionTicket message any more */
3923 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003924 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003925
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003926 /*
3927 * Zero-length ticket means the server changed his mind and doesn't want
3928 * to send a ticket after all, so just forget it
3929 */
Paul Bakker66d5d072014-06-17 16:39:18 +02003930 if( ticket_len == 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003931 return( 0 );
3932
Hanno Beckerb2964cb2019-01-30 14:46:35 +00003933 if( ssl->session != NULL && ssl->session->ticket != NULL )
3934 {
3935 mbedtls_platform_zeroize( ssl->session->ticket,
3936 ssl->session->ticket_len );
3937 mbedtls_free( ssl->session->ticket );
3938 ssl->session->ticket = NULL;
3939 ssl->session->ticket_len = 0;
3940 }
3941
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003942 mbedtls_platform_zeroize( ssl->session_negotiate->ticket,
3943 ssl->session_negotiate->ticket_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003944 mbedtls_free( ssl->session_negotiate->ticket );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003945 ssl->session_negotiate->ticket = NULL;
3946 ssl->session_negotiate->ticket_len = 0;
3947
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003948 if( ( ticket = mbedtls_calloc( 1, ticket_len ) ) == NULL )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003949 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003950 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ticket alloc failed" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003951 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3952 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003953 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003954 }
3955
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003956 memcpy( ticket, msg + 6, ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003957
3958 ssl->session_negotiate->ticket = ticket;
3959 ssl->session_negotiate->ticket_len = ticket_len;
3960 ssl->session_negotiate->ticket_lifetime = lifetime;
3961
3962 /*
3963 * RFC 5077 section 3.4:
3964 * "If the client receives a session ticket from the server, then it
3965 * discards any Session ID that was sent in the ServerHello."
3966 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003967 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02003968 ssl->session_negotiate->id_len = 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003970 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003971
3972 return( 0 );
3973}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003974#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003975
Paul Bakker5121ce52009-01-03 21:22:43 +00003976/*
Paul Bakker1961b702013-01-25 14:49:24 +01003977 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003978 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003979int mbedtls_ssl_handshake_client_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003980{
3981 int ret = 0;
3982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003983 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
Paul Bakker1961b702013-01-25 14:49:24 +01003984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003985 /* Change state now, so that it is right in mbedtls_ssl_read_record(), used
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003986 * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003987#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3988 if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC &&
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003989 ssl->handshake->new_session_ticket != 0 )
3990 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003991 ssl->state = MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET;
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003992 }
3993#endif
3994
Paul Bakker1961b702013-01-25 14:49:24 +01003995 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003996 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003997 case MBEDTLS_SSL_HELLO_REQUEST:
3998 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003999 break;
4000
Paul Bakker1961b702013-01-25 14:49:24 +01004001 /*
4002 * ==> ClientHello
4003 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004004 case MBEDTLS_SSL_CLIENT_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01004005 ret = ssl_write_client_hello( ssl );
4006 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004007
Paul Bakker1961b702013-01-25 14:49:24 +01004008 /*
4009 * <== ServerHello
4010 * Certificate
4011 * ( ServerKeyExchange )
4012 * ( CertificateRequest )
4013 * ServerHelloDone
4014 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004015 case MBEDTLS_SSL_SERVER_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01004016 ret = ssl_parse_server_hello( ssl );
4017 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004019 case MBEDTLS_SSL_SERVER_CERTIFICATE:
4020 ret = mbedtls_ssl_parse_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004021 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004023 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01004024 ret = ssl_parse_server_key_exchange( ssl );
4025 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004027 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Paul Bakker1961b702013-01-25 14:49:24 +01004028 ret = ssl_parse_certificate_request( ssl );
4029 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004031 case MBEDTLS_SSL_SERVER_HELLO_DONE:
Paul Bakker1961b702013-01-25 14:49:24 +01004032 ret = ssl_parse_server_hello_done( ssl );
4033 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004034
Paul Bakker1961b702013-01-25 14:49:24 +01004035 /*
4036 * ==> ( Certificate/Alert )
4037 * ClientKeyExchange
4038 * ( CertificateVerify )
4039 * ChangeCipherSpec
4040 * Finished
4041 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004042 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
4043 ret = mbedtls_ssl_write_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004044 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004046 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01004047 ret = ssl_write_client_key_exchange( ssl );
4048 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004050 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Paul Bakker1961b702013-01-25 14:49:24 +01004051 ret = ssl_write_certificate_verify( ssl );
4052 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004054 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
4055 ret = mbedtls_ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004056 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004058 case MBEDTLS_SSL_CLIENT_FINISHED:
4059 ret = mbedtls_ssl_write_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004060 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004061
Paul Bakker1961b702013-01-25 14:49:24 +01004062 /*
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02004063 * <== ( NewSessionTicket )
4064 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01004065 * Finished
4066 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004067#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4068 case MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET:
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02004069 ret = ssl_parse_new_session_ticket( ssl );
4070 break;
Paul Bakkera503a632013-08-14 13:48:06 +02004071#endif
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02004072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004073 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
4074 ret = mbedtls_ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004075 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004077 case MBEDTLS_SSL_SERVER_FINISHED:
4078 ret = mbedtls_ssl_parse_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004079 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004081 case MBEDTLS_SSL_FLUSH_BUFFERS:
4082 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
4083 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Paul Bakker1961b702013-01-25 14:49:24 +01004084 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004086 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
4087 mbedtls_ssl_handshake_wrapup( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004088 break;
Paul Bakker48916f92012-09-16 19:57:18 +00004089
Paul Bakker1961b702013-01-25 14:49:24 +01004090 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004091 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
4092 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1961b702013-01-25 14:49:24 +01004093 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004094
4095 return( ret );
4096}
Jerry Yuc5aef882021-12-23 20:15:02 +08004097
Jerry Yufb4b6472022-01-27 15:03:26 +08004098#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_2 */