blob: a365a9116e56b3dbc2d0fe9f191bd28d94808972 [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)
Ronald Crond491c2d2022-02-19 18:30:46 +010056int mbedtls_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 ) &&
Ronald Crond491c2d2022-02-19 18:30:46 +0100675 mbedtls_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 Cron4079abc2022-02-20 10:35:26 +0100684int mbedtls_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 )
Ronald Cron12dcdf02022-02-16 15:28:22 +0100689{
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 Cron4079abc2022-02-20 10:35:26 +01001091 ret = mbedtls_ssl_tls12_write_client_hello_exts( ssl, p + 2 + ext_len, end,
1092 uses_ec, &olen );
Ronald Cron12dcdf02022-02-16 15:28:22 +01001093 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 }
Gilles Peskine42459802019-12-19 13:31:53 +01002355 if( ecdh_bits > 0xffff )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002356 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Gilles Peskine42459802019-12-19 13:31:53 +01002357 handshake->ecdh_bits = (uint16_t) ecdh_bits;
Hanno Beckerbb89e272019-01-08 12:54:37 +00002358
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01002359 /* Keep a copy of the peer's public key */
Hanno Beckerbb89e272019-01-08 12:54:37 +00002360 ecpoint_len = *(*p)++;
2361 if( (size_t)( end - *p ) < ecpoint_len )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002362 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Hanno Beckerbb89e272019-01-08 12:54:37 +00002363
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01002364 if( ecpoint_len > sizeof( handshake->ecdh_psa_peerkey ) )
2365 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Hanno Beckerbb89e272019-01-08 12:54:37 +00002366
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01002367 memcpy( handshake->ecdh_psa_peerkey, *p, ecpoint_len );
2368 handshake->ecdh_psa_peerkey_len = ecpoint_len;
Hanno Beckerbb89e272019-01-08 12:54:37 +00002369 *p += ecpoint_len;
Manuel Pégourié-Gonnard4a0ac1f2022-01-18 12:30:40 +01002370
Hanno Beckerbb89e272019-01-08 12:54:37 +00002371 return( 0 );
2372}
2373#endif /* MBEDTLS_USE_PSA_CRYPTO &&
2374 ( MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2375 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ) */
2376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002377#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2378 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2379 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2380static int ssl_parse_server_ecdh_params( mbedtls_ssl_context *ssl,
Paul Bakker29e1f122013-04-16 13:07:56 +02002381 unsigned char **p,
2382 unsigned char *end )
2383{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002384 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker29e1f122013-04-16 13:07:56 +02002385
Paul Bakker29e1f122013-04-16 13:07:56 +02002386 /*
2387 * Ephemeral ECDH parameters:
2388 *
2389 * struct {
2390 * ECParameters curve_params;
2391 * ECPoint public;
2392 * } ServerECDHParams;
2393 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002394 if( ( ret = mbedtls_ecdh_read_params( &ssl->handshake->ecdh_ctx,
Paul Bakker29e1f122013-04-16 13:07:56 +02002395 (const unsigned char **) p, end ) ) != 0 )
2396 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002397 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_read_params" ), ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01002398#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard1c1c20e2018-09-12 10:34:43 +02002399 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2400 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02002401#endif
Paul Bakker29e1f122013-04-16 13:07:56 +02002402 return( ret );
2403 }
2404
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002405 if( ssl_check_server_ecdh_params( ssl ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02002406 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002407 MBEDTLS_SSL_DEBUG_MSG( 1,
2408 ( "bad server key exchange message (ECDHE curve)" ) );
Hanno Becker2fc9a652021-06-24 15:40:11 +01002409 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02002410 }
2411
Paul Bakker29e1f122013-04-16 13:07:56 +02002412 return( ret );
2413}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002414#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2415 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2416 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02002417
Gilles Peskineeccd8882020-03-10 12:19:08 +01002418#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002419static int ssl_parse_server_psk_hint( mbedtls_ssl_context *ssl,
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002420 unsigned char **p,
2421 unsigned char *end )
2422{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002423 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
irwir6527bd62019-09-21 18:51:25 +03002424 uint16_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02002425 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002426
2427 /*
2428 * PSK parameters:
2429 *
2430 * opaque psk_identity_hint<0..2^16-1>;
2431 */
Hanno Becker0c161d12018-10-08 13:40:50 +01002432 if( end - (*p) < 2 )
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01002433 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002434 MBEDTLS_SSL_DEBUG_MSG( 1,
2435 ( "bad server key exchange message (psk_identity_hint length)" ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002436 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiak740b2182018-03-13 11:31:14 +01002437 }
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02002438 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002439 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002440
irwir6527bd62019-09-21 18:51:25 +03002441 if( end - (*p) < len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002442 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002443 MBEDTLS_SSL_DEBUG_MSG( 1,
2444 ( "bad server key exchange message (psk_identity_hint length)" ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002445 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002446 }
2447
Manuel Pégourié-Gonnard9d624122016-02-22 11:10:14 +01002448 /*
2449 * Note: we currently ignore the PKS identity hint, as we only allow one
2450 * PSK to be provisionned on the client. This could be changed later if
2451 * someone needs that feature.
2452 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002453 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002454 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002455
2456 return( ret );
2457}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002458#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002460#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
2461 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002462/*
2463 * Generate a pre-master secret and encrypt it with the server's RSA key
2464 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002465static int ssl_write_encrypted_pms( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002466 size_t offset, size_t *olen,
2467 size_t pms_offset )
2468{
Janos Follath865b3eb2019-12-16 11:46:15 +00002469 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002470 size_t len_bytes = 2;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002471 unsigned char *p = ssl->handshake->premaster + pms_offset;
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002472 mbedtls_pk_context * peer_pk;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002473
Angus Grattond8213d02016-05-25 20:56:48 +10002474 if( offset + len_bytes > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002475 {
2476 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small for encrypted pms" ) );
2477 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2478 }
2479
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002480 /*
2481 * Generate (part of) the pre-master as
2482 * struct {
2483 * ProtocolVersion client_version;
2484 * opaque random[46];
2485 * } PreMasterSecret;
2486 */
Ronald Cron4e263fd2022-03-15 15:54:17 +01002487 mbedtls_ssl_write_version( MBEDTLS_SSL_MAJOR_VERSION_3,
2488 MBEDTLS_SSL_MINOR_VERSION_3,
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002489 ssl->conf->transport, p );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002490
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002491 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p + 2, 46 ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002493 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002494 return( ret );
2495 }
2496
2497 ssl->handshake->pmslen = 48;
2498
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002499#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2500 peer_pk = &ssl->handshake->peer_pubkey;
2501#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002502 if( ssl->session_negotiate->peer_cert == NULL )
2503 {
Hanno Becker8273df82019-02-06 17:37:32 +00002504 /* Should never happen */
Hanno Becker62d58ed2019-02-26 11:51:06 +00002505 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker8273df82019-02-06 17:37:32 +00002506 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002507 }
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002508 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2509#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002510
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002511 /*
2512 * Now write it out, encrypted
2513 */
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002514 if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_RSA ) )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002515 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002516 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
2517 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002518 }
2519
Hanno Beckerc7d7e292019-02-06 16:49:54 +00002520 if( ( ret = mbedtls_pk_encrypt( peer_pk,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002521 p, ssl->handshake->pmslen,
2522 ssl->out_msg + offset + len_bytes, olen,
Angus Grattond8213d02016-05-25 20:56:48 +10002523 MBEDTLS_SSL_OUT_CONTENT_LEN - offset - len_bytes,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002524 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002526 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_rsa_pkcs1_encrypt", ret );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002527 return( ret );
2528 }
2529
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002530 if( len_bytes == 2 )
2531 {
Joe Subbiani6dd73642021-07-19 11:56:54 +01002532 MBEDTLS_PUT_UINT16_BE( *olen, ssl->out_msg, offset );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002533 *olen += 2;
2534 }
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002535
Hanno Beckerae553dd2019-02-08 14:06:00 +00002536#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2537 /* We don't need the peer's public key anymore. Free it. */
2538 mbedtls_pk_free( peer_pk );
2539#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002540 return( 0 );
2541}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002542#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
2543 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02002544
Manuel Pégourié-Gonnard5c2a7ca2015-10-23 08:48:41 +02002545#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2546 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2547 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002548static int ssl_parse_signature_algorithm( mbedtls_ssl_context *ssl,
Paul Bakker29e1f122013-04-16 13:07:56 +02002549 unsigned char **p,
2550 unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002551 mbedtls_md_type_t *md_alg,
2552 mbedtls_pk_type_t *pk_alg )
Paul Bakker29e1f122013-04-16 13:07:56 +02002553{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002554 *md_alg = MBEDTLS_MD_NONE;
2555 *pk_alg = MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002556
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002557 if( (*p) + 2 > end )
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002558 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker29e1f122013-04-16 13:07:56 +02002559
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002560 /*
2561 * Get hash algorithm
2562 */
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002563 if( ( *md_alg = mbedtls_ssl_md_alg_from_hash( (*p)[0] ) )
2564 == MBEDTLS_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02002565 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002566 MBEDTLS_SSL_DEBUG_MSG( 1,
2567 ( "Server used unsupported HashAlgorithm %d", *(p)[0] ) );
Hanno Becker2fc9a652021-06-24 15:40:11 +01002568 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02002569 }
2570
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002571 /*
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002572 * Get signature algorithm
2573 */
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002574 if( ( *pk_alg = mbedtls_ssl_pk_alg_from_sig( (*p)[1] ) )
2575 == MBEDTLS_PK_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02002576 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002577 MBEDTLS_SSL_DEBUG_MSG( 1,
2578 ( "server used unsupported SignatureAlgorithm %d", (*p)[1] ) );
Dave Rodgman5f8c18b2021-06-28 11:58:00 +01002579 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Paul Bakker29e1f122013-04-16 13:07:56 +02002580 }
2581
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02002582 /*
Jerry Yu24811fb2022-01-19 18:02:15 +08002583 * Check if the signature algorithm is acceptable
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02002584 */
Jerry Yu24811fb2022-01-19 18:02:15 +08002585 if( !mbedtls_ssl_sig_alg_is_offered( ssl, MBEDTLS_GET_UINT16_BE( *p, 0 ) ) )
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02002586 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002587 MBEDTLS_SSL_DEBUG_MSG( 1,
2588 ( "server used HashAlgorithm %d that was not offered", *(p)[0] ) );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002589 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02002590 }
2591
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002592 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d",
2593 (*p)[1] ) );
2594 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d",
2595 (*p)[0] ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02002596 *p += 2;
2597
2598 return( 0 );
2599}
Manuel Pégourié-Gonnard5c2a7ca2015-10-23 08:48:41 +02002600#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2601 MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2602 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02002603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002604#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2605 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2606static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002607{
Janos Follath865b3eb2019-12-16 11:46:15 +00002608 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002609 const mbedtls_ecp_keypair *peer_key;
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002610 mbedtls_pk_context * peer_pk;
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002611
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002612#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2613 peer_pk = &ssl->handshake->peer_pubkey;
2614#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002615 if( ssl->session_negotiate->peer_cert == NULL )
2616 {
Hanno Becker8273df82019-02-06 17:37:32 +00002617 /* Should never happen */
Hanno Beckerbd5580a2019-02-26 12:36:01 +00002618 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker8273df82019-02-06 17:37:32 +00002619 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002620 }
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002621 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2622#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002623
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002624 if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_ECKEY ) )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002626 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2627 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002628 }
2629
Hanno Beckerbe7f5082019-02-06 17:44:07 +00002630 peer_key = mbedtls_pk_ec( *peer_pk );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002631
Przemek Stekielea4000f2022-03-16 09:49:33 +01002632#if defined(MBEDTLS_USE_PSA_CRYPTO)
2633 size_t ecdh_bits = 0;
Przemek Stekiel561a4232022-03-16 13:16:24 +01002634 size_t olen = 0;
2635
2636 if( mbedtls_ssl_check_curve( ssl, peer_key->grp.id ) != 0 )
2637 {
2638 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
2639 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
2640 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002641
2642 ssl->handshake->ecdh_psa_type =
2643 PSA_KEY_TYPE_ECC_KEY_PAIR( mbedtls_ecc_group_to_psa( peer_key->grp.id,
2644 &ecdh_bits ) );
2645
2646 if( ssl->handshake->ecdh_psa_type == 0 || ecdh_bits > 0xffff )
2647 {
2648 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Invalid ecc group conversion to psa." ) );
2649 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
2650 }
2651
2652 ssl->handshake->ecdh_bits = (uint16_t) ecdh_bits;
2653
Przemek Stekielea4000f2022-03-16 09:49:33 +01002654 /* Store peer's public key in psa format. */
Przemek Stekiel561a4232022-03-16 13:16:24 +01002655 ret = mbedtls_ecp_point_write_binary( &peer_key->grp, &peer_key->Q,
2656 MBEDTLS_ECP_PF_UNCOMPRESSED, &olen,
2657 ssl->handshake->ecdh_psa_peerkey,
2658 MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH );
Przemek Stekielea4000f2022-03-16 09:49:33 +01002659
Przemek Stekiel561a4232022-03-16 13:16:24 +01002660 if ( ret != 0 )
2661 {
2662 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecp_point_write_binary" ), ret );
2663 return( ret );
2664 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002665
Przemek Stekiel561a4232022-03-16 13:16:24 +01002666 ssl->handshake->ecdh_psa_peerkey_len = olen;
Przemek Stekielea4000f2022-03-16 09:49:33 +01002667#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002668 if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
2669 MBEDTLS_ECDH_THEIRS ) ) != 0 )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002671 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002672 return( ret );
2673 }
2674
2675 if( ssl_check_server_ecdh_params( ssl ) != 0 )
2676 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002677 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
Hanno Becker9ed1ba52021-06-24 11:03:13 +01002678 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002679 }
Przemek Stekielea4000f2022-03-16 09:49:33 +01002680#endif
Hanno Beckerae553dd2019-02-08 14:06:00 +00002681#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2682 /* We don't need the peer's public key anymore. Free it,
2683 * so that more RAM is available for upcoming expensive
2684 * operations like ECDHE. */
2685 mbedtls_pk_free( peer_pk );
2686#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2687
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002688 return( ret );
2689}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002690#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2691 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002693static int ssl_parse_server_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker41c83d32013-03-20 14:39:14 +01002694{
Janos Follath865b3eb2019-12-16 11:46:15 +00002695 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01002696 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002697 ssl->handshake->ciphersuite_info;
Andres Amaya Garcia53c77cc2017-06-27 16:15:06 +01002698 unsigned char *p = NULL, *end = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002700 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002702#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
2703 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002704 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002705 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002706 ssl->state++;
2707 return( 0 );
2708 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002709 ((void) p);
2710 ((void) end);
2711#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002713#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2714 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2715 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2716 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002717 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002718 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
2719 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002720 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002721 mbedtls_ssl_send_alert_message(
2722 ssl,
2723 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2724 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002725 return( ret );
2726 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002728 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002729 ssl->state++;
2730 return( 0 );
2731 }
2732 ((void) p);
2733 ((void) end);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002734#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2735 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002736
Gilles Peskineeccd8882020-03-10 12:19:08 +01002737#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002738 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002739 ssl->handshake->ecrs_state == ssl_ecrs_ske_start_processing )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002740 {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002741 goto start_processing;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02002742 }
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002743#endif
2744
Hanno Becker327c93b2018-08-15 13:56:18 +01002745 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002746 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002747 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002748 return( ret );
2749 }
2750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002751 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002752 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002753 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002754 mbedtls_ssl_send_alert_message(
2755 ssl,
2756 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2757 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002758 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002759 }
2760
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002761 /*
2762 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
2763 * doesn't use a psk_identity_hint
2764 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002765 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002767 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2768 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Paul Bakker188c8de2013-04-19 09:13:37 +02002769 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002770 /* Current message is probably either
2771 * CertificateRequest or ServerHelloDone */
2772 ssl->keep_current_message = 1;
Paul Bakker188c8de2013-04-19 09:13:37 +02002773 goto exit;
2774 }
2775
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002776 MBEDTLS_SSL_DEBUG_MSG( 1,
2777 ( "server key exchange message must not be skipped" ) );
2778 mbedtls_ssl_send_alert_message(
2779 ssl,
2780 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2781 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01002782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002783 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002784 }
2785
Gilles Peskineeccd8882020-03-10 12:19:08 +01002786#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002787 if( ssl->handshake->ecrs_enabled )
2788 ssl->handshake->ecrs_state = ssl_ecrs_ske_start_processing;
2789
2790start_processing:
2791#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002792 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002793 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002794 MBEDTLS_SSL_DEBUG_BUF( 3, "server key exchange", p, end - p );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01002795
Gilles Peskineeccd8882020-03-10 12:19:08 +01002796#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002797 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2798 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
2799 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
2800 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002801 {
2802 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
2803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002804 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002805 mbedtls_ssl_send_alert_message(
2806 ssl,
2807 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman8f127392021-06-28 12:02:21 +01002808 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002809 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002810 }
2811 } /* FALLTROUGH */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002812#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002814#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \
2815 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2816 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2817 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002818 ; /* nothing more to do */
2819 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002820#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED ||
2821 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2822#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2823 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2824 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ||
2825 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002826 {
Paul Bakker29e1f122013-04-16 13:07:56 +02002827 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002828 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002829 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002830 mbedtls_ssl_send_alert_message(
2831 ssl,
2832 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2833 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Dave Rodgmanbed89272021-06-29 12:06:32 +01002834 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002835 }
2836 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002837 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002838#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2839 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Hanno Beckerbb89e272019-01-08 12:54:37 +00002840#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
2841 ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2842 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) )
2843 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2844 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
2845 {
2846 if( ssl_parse_server_ecdh_params_psa( ssl, &p, end ) != 0 )
2847 {
2848 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002849 mbedtls_ssl_send_alert_message(
2850 ssl,
2851 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2852 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Dave Rodgmanc50b7172021-06-29 14:40:23 +01002853 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Hanno Beckerbb89e272019-01-08 12:54:37 +00002854 }
2855 }
2856 else
2857#endif /* MBEDTLS_USE_PSA_CRYPTO &&
2858 ( MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2859 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002860#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2861 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
2862 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2863 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2864 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
2865 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002866 {
2867 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
2868 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002869 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002870 mbedtls_ssl_send_alert_message(
2871 ssl,
2872 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2873 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Dave Rodgman39bd5a62021-06-29 15:25:21 +01002874 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker41c83d32013-03-20 14:39:14 +01002875 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002876 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002877 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002878#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2879 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
2880 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002881#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2882 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2883 {
2884 ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx,
2885 p, end - p );
2886 if( ret != 0 )
2887 {
2888 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002889 mbedtls_ssl_send_alert_message(
2890 ssl,
2891 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Becker77b4a652021-06-24 16:27:09 +01002892 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
2893 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02002894 }
2895 }
2896 else
2897#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002899 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2900 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002901 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002902
Gilles Peskineeccd8882020-03-10 12:19:08 +01002903#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01002904 if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002905 {
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002906 size_t sig_len, hashlen;
Ronald Cron69a63422021-10-18 09:47:58 +02002907#if defined(MBEDTLS_USE_PSA_CRYPTO)
2908 unsigned char hash[PSA_HASH_MAX_SIZE];
2909#else
2910 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2911#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002912 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
2913 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
2914 unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002915 size_t params_len = p - params;
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02002916 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002917
Hanno Beckera6899bb2019-02-06 18:26:03 +00002918 mbedtls_pk_context * peer_pk;
2919
Paul Bakker29e1f122013-04-16 13:07:56 +02002920 /*
2921 * Handle the digitally-signed structure
2922 */
Ronald Cron90915f22022-03-07 11:11:36 +01002923 if( ssl_parse_signature_algorithm( ssl, &p, end,
2924 &md_alg, &pk_alg ) != 0 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002925 {
Ronald Cron90915f22022-03-07 11:11:36 +01002926 MBEDTLS_SSL_DEBUG_MSG( 1,
2927 ( "bad server key exchange message" ) );
2928 mbedtls_ssl_send_alert_message(
2929 ssl,
2930 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2931 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
2932 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker29e1f122013-04-16 13:07:56 +02002933 }
Ronald Cron90915f22022-03-07 11:11:36 +01002934
2935 if( pk_alg !=
2936 mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
Paul Bakker9659dae2013-08-28 16:21:34 +02002937 {
Ronald Cron90915f22022-03-07 11:11:36 +01002938 MBEDTLS_SSL_DEBUG_MSG( 1,
2939 ( "bad server key exchange message" ) );
2940 mbedtls_ssl_send_alert_message(
2941 ssl,
2942 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2943 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
2944 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
Paul Bakker9659dae2013-08-28 16:21:34 +02002945 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002946
2947 /*
2948 * Read signature
2949 */
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002950
2951 if( p > end - 2 )
2952 {
2953 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002954 mbedtls_ssl_send_alert_message(
2955 ssl,
2956 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2957 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002958 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiaka1098f82018-03-13 11:28:49 +01002959 }
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002960 sig_len = ( p[0] << 8 ) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00002961 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002962
Krzysztof Stachowiak027f84c2018-03-13 11:29:24 +01002963 if( p != end - sig_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01002964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002965 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01002966 mbedtls_ssl_send_alert_message(
2967 ssl,
2968 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2969 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Beckercbc8f6f2021-06-24 10:32:31 +01002970 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker41c83d32013-03-20 14:39:14 +01002971 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002973 MBEDTLS_SSL_DEBUG_BUF( 3, "signature", p, sig_len );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002974
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002975 /*
2976 * Compute the hash that has been signed
2977 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002978 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02002979 {
Gilles Peskineca1d7422018-04-24 11:53:22 +02002980 ret = mbedtls_ssl_get_key_exchange_md_tls1_2( ssl, hash, &hashlen,
2981 params, params_len,
2982 md_alg );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01002983 if( ret != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02002984 return( ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02002985 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002986 else
Paul Bakker29e1f122013-04-16 13:07:56 +02002987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002988 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2989 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002990 }
Paul Bakker29e1f122013-04-16 13:07:56 +02002991
Gilles Peskineca1d7422018-04-24 11:53:22 +02002992 MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen );
Paul Bakker29e1f122013-04-16 13:07:56 +02002993
Hanno Beckera6899bb2019-02-06 18:26:03 +00002994#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2995 peer_pk = &ssl->handshake->peer_pubkey;
2996#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02002997 if( ssl->session_negotiate->peer_cert == NULL )
2998 {
Hanno Becker8273df82019-02-06 17:37:32 +00002999 /* Should never happen */
Hanno Beckerbd5580a2019-02-26 12:36:01 +00003000 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Hanno Becker8273df82019-02-06 17:37:32 +00003001 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02003002 }
Hanno Beckera6899bb2019-02-06 18:26:03 +00003003 peer_pk = &ssl->session_negotiate->peer_cert->pk;
3004#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard7f2f0622015-09-03 10:44:32 +02003005
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02003006 /*
3007 * Verify signature
3008 */
Hanno Beckera6899bb2019-02-06 18:26:03 +00003009 if( !mbedtls_pk_can_do( peer_pk, pk_alg ) )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02003010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003011 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003012 mbedtls_ssl_send_alert_message(
3013 ssl,
3014 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3015 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003016 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02003017 }
3018
Gilles Peskineeccd8882020-03-10 12:19:08 +01003019#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003020 if( ssl->handshake->ecrs_enabled )
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003021 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02003022#endif
3023
Hanno Beckera6899bb2019-02-06 18:26:03 +00003024 if( ( ret = mbedtls_pk_verify_restartable( peer_pk,
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02003025 md_alg, hash, hashlen, p, sig_len, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02003026 {
Gilles Peskineeccd8882020-03-10 12:19:08 +01003027#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard1f1f2a12017-05-18 11:27:06 +02003028 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3029#endif
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003030 mbedtls_ssl_send_alert_message(
3031 ssl,
3032 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3033 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003034 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01003035#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003036 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3037 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
3038#endif
Paul Bakkerc70b9822013-04-07 22:00:46 +02003039 return( ret );
Paul Bakkerc3f177a2012-04-11 16:11:49 +00003040 }
Hanno Beckerae553dd2019-02-08 14:06:00 +00003041
3042#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3043 /* We don't need the peer's public key anymore. Free it,
3044 * so that more RAM is available for upcoming expensive
3045 * operations like ECDHE. */
3046 mbedtls_pk_free( peer_pk );
3047#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakker5121ce52009-01-03 21:22:43 +00003048 }
Gilles Peskineeccd8882020-03-10 12:19:08 +01003049#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003050
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003051exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00003052 ssl->state++;
3053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003054 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003055
3056 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003057}
3058
Gilles Peskineeccd8882020-03-10 12:19:08 +01003059#if ! defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003060static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01003061{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003062 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003063 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01003064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003065 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01003066
Hanno Becker1aa267c2017-04-28 17:08:27 +01003067 if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01003068 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003069 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01003070 ssl->state++;
3071 return( 0 );
3072 }
3073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003074 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3075 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01003076}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003077#else /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003078static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003079{
Janos Follath865b3eb2019-12-16 11:46:15 +00003080 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00003081 unsigned char *buf;
3082 size_t n = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003083 size_t cert_type_len = 0, dn_len = 0;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003084 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003085 ssl->handshake->ciphersuite_info;
Ronald Cron90915f22022-03-07 11:11:36 +01003086 size_t sig_alg_len;
3087#if defined(MBEDTLS_DEBUG_C)
3088 unsigned char *sig_alg;
3089#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003091 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003092
Hanno Becker1aa267c2017-04-28 17:08:27 +01003093 if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01003094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003095 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01003096 ssl->state++;
3097 return( 0 );
3098 }
3099
Hanno Becker327c93b2018-08-15 13:56:18 +01003100 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003101 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003102 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
3103 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003104 }
3105
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003106 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
3107 {
3108 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003109 mbedtls_ssl_send_alert_message(
3110 ssl,
3111 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3112 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003113 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
3114 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003115
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003116 ssl->state++;
Jerry Yufb28b882022-01-28 11:05:58 +08003117 ssl->handshake->client_auth =
3118 ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST );
Paul Bakker5121ce52009-01-03 21:22:43 +00003119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003120 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
Jerry Yufb28b882022-01-28 11:05:58 +08003121 ssl->handshake->client_auth ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003122
Jerry Yufb28b882022-01-28 11:05:58 +08003123 if( ssl->handshake->client_auth == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003124 {
Johan Pascala89ca862020-08-25 10:03:19 +02003125 /* Current message is probably the ServerHelloDone */
3126 ssl->keep_current_message = 1;
Paul Bakker926af752012-11-23 13:38:07 +01003127 goto exit;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003128 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003129
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02003130 /*
3131 * struct {
3132 * ClientCertificateType certificate_types<1..2^8-1>;
3133 * SignatureAndHashAlgorithm
3134 * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
3135 * DistinguishedName certificate_authorities<0..2^16-1>;
3136 * } CertificateRequest;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00003137 *
3138 * Since we only support a single certificate on clients, let's just
3139 * ignore all the information that's supposed to help us pick a
3140 * certificate.
3141 *
3142 * We could check that our certificate matches the request, and bail out
3143 * if it doesn't, but it's simpler to just send the certificate anyway,
3144 * and give the server the opportunity to decide if it should terminate
3145 * the connection when it doesn't like our certificate.
3146 *
3147 * Same goes for the hash in TLS 1.2's signature_algorithms: at this
3148 * point we only have one hash available (see comments in
Simon Butcherc0957bd2016-03-01 13:16:57 +00003149 * write_certificate_verify), so let's just use what we have.
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00003150 *
3151 * However, we still minimally parse the message to check it is at least
3152 * superficially sane.
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02003153 */
Paul Bakker926af752012-11-23 13:38:07 +01003154 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003155
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00003156 /* certificate_types */
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02003157 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) )
3158 {
3159 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
3160 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3161 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01003162 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Krzysztof Stachowiak73b183c2018-04-05 10:20:09 +02003163 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003164 cert_type_len = buf[mbedtls_ssl_hs_hdr_len( ssl )];
Paul Bakker926af752012-11-23 13:38:07 +01003165 n = cert_type_len;
3166
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01003167 /*
Krzysztof Stachowiak94d49972018-04-05 14:48:55 +02003168 * In the subsequent code there are two paths that read from buf:
Krzysztof Stachowiakbc145f72018-03-20 11:19:50 +01003169 * * the length of the signature algorithms field (if minor version of
3170 * SSL is 3),
3171 * * distinguished name length otherwise.
3172 * Both reach at most the index:
3173 * ...hdr_len + 2 + n,
3174 * therefore the buffer length at this point must be greater than that
3175 * regardless of the actual code path.
3176 */
3177 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n )
Paul Bakker926af752012-11-23 13:38:07 +01003178 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003179 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003180 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3181 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01003182 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker926af752012-11-23 13:38:07 +01003183 }
3184
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00003185 /* supported_signature_algorithms */
Ronald Cron90915f22022-03-07 11:11:36 +01003186 sig_alg_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
3187 | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
3188
3189 /*
3190 * The furthest access in buf is in the loop few lines below:
3191 * sig_alg[i + 1],
3192 * where:
3193 * sig_alg = buf + ...hdr_len + 3 + n,
3194 * max(i) = sig_alg_len - 1.
3195 * Therefore the furthest access is:
3196 * buf[...hdr_len + 3 + n + sig_alg_len - 1 + 1],
3197 * which reduces to:
3198 * buf[...hdr_len + 3 + n + sig_alg_len],
3199 * which is one less than we need the buf to be.
3200 */
3201 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n + sig_alg_len )
Paul Bakker926af752012-11-23 13:38:07 +01003202 {
Ronald Cron90915f22022-03-07 11:11:36 +01003203 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
3204 mbedtls_ssl_send_alert_message(
3205 ssl,
3206 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3207 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
3208 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakkerf7abd422013-04-16 13:15:56 +02003209 }
Paul Bakker926af752012-11-23 13:38:07 +01003210
Ronald Cron90915f22022-03-07 11:11:36 +01003211#if defined(MBEDTLS_DEBUG_C)
3212 sig_alg = buf + mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n;
3213 for( size_t i = 0; i < sig_alg_len; i += 2 )
3214 {
3215 MBEDTLS_SSL_DEBUG_MSG( 3,
3216 ( "Supported Signature Algorithm found: %d,%d",
3217 sig_alg[i], sig_alg[i + 1] ) );
3218 }
3219#endif
3220
3221 n += 2 + sig_alg_len;
3222
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00003223 /* certificate_authorities */
3224 dn_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
3225 | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01003226
3227 n += dn_len;
Manuel Pégourié-Gonnardd1b7f2b2016-02-24 14:13:22 +00003228 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n )
Paul Bakker926af752012-11-23 13:38:07 +01003229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003230 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003231 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3232 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker5697af02021-06-24 10:33:51 +01003233 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker926af752012-11-23 13:38:07 +01003234 }
3235
3236exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003237 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003238
3239 return( 0 );
3240}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003241#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003243static int ssl_parse_server_hello_done( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003244{
Janos Follath865b3eb2019-12-16 11:46:15 +00003245 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003247 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003248
Hanno Becker327c93b2018-08-15 13:56:18 +01003249 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003250 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003251 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
3252 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003253 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003254
3255 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
3256 {
3257 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
3258 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
3259 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003261 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ||
3262 ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003265 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3266 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker029cc2f2021-06-24 10:09:50 +01003267 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +00003268 }
3269
3270 ssl->state++;
3271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003272#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003273 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003274 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003275#endif
3276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003277 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003278
3279 return( 0 );
3280}
3281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003282static int ssl_write_client_key_exchange( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003283{
Janos Follath865b3eb2019-12-16 11:46:15 +00003284 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003285
3286 size_t header_len;
3287 size_t content_len;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003288 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003289 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003291 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003293#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
3294 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003295 {
Paul Bakker5121ce52009-01-03 21:22:43 +00003296 /*
3297 * DHM key exchange -- send G^X mod P
3298 */
Gilles Peskine487bbf62021-05-27 22:17:07 +02003299 content_len = mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00003300
Joe Subbiani6dd73642021-07-19 11:56:54 +01003301 MBEDTLS_PUT_UINT16_BE( content_len, ssl->out_msg, 4 );
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003302 header_len = 6;
Paul Bakker5121ce52009-01-03 21:22:43 +00003303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003304 ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
Gilles Peskine487bbf62021-05-27 22:17:07 +02003305 (int) mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx ),
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003306 &ssl->out_msg[header_len], content_len,
3307 ssl->conf->f_rng, ssl->conf->p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +00003308 if( ret != 0 )
3309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003310 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003311 return( ret );
3312 }
3313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003314 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
3315 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker5121ce52009-01-03 21:22:43 +00003316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003317 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003318 ssl->handshake->premaster,
3319 MBEDTLS_PREMASTER_SIZE,
3320 &ssl->handshake->pmslen,
3321 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003323 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003324 return( ret );
3325 }
3326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003327 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker5121ce52009-01-03 21:22:43 +00003328 }
3329 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003330#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Hanno Becker4a63ed42019-01-08 11:39:35 +00003331#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
3332 ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Przemek Stekield905d332022-03-16 09:50:56 +01003333 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
3334 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3335 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) )
Hanno Becker4a63ed42019-01-08 11:39:35 +00003336 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Przemek Stekield905d332022-03-16 09:50:56 +01003337 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
3338 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
3339 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Hanno Becker4a63ed42019-01-08 11:39:35 +00003340 {
Andrzej Kureka0237f82022-02-24 13:24:52 -05003341 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3342 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
Janos Follath53b8ec22019-08-08 10:28:27 +01003343 psa_key_attributes_t key_attributes;
Hanno Becker4a63ed42019-01-08 11:39:35 +00003344
3345 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3346
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003347 header_len = 4;
Hanno Becker4a63ed42019-01-08 11:39:35 +00003348
Hanno Becker0a94a642019-01-11 14:35:30 +00003349 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
3350
Hanno Becker4a63ed42019-01-08 11:39:35 +00003351 /*
3352 * Generate EC private key for ECDHE exchange.
3353 */
3354
Hanno Becker4a63ed42019-01-08 11:39:35 +00003355 /* The master secret is obtained from the shared ECDH secret by
3356 * applying the TLS 1.2 PRF with a specific salt and label. While
3357 * the PSA Crypto API encourages combining key agreement schemes
3358 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
3359 * yet support the provisioning of salt + label to the KDF.
3360 * For the time being, we therefore need to split the computation
3361 * of the ECDH secret and the application of the TLS 1.2 PRF. */
Janos Follath53b8ec22019-08-08 10:28:27 +01003362 key_attributes = psa_key_attributes_init();
3363 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
Janos Follathdf3b0892019-08-08 11:12:24 +01003364 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
Gilles Peskine42459802019-12-19 13:31:53 +01003365 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
3366 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003367
3368 /* Generate ECDH private key. */
Janos Follath53b8ec22019-08-08 10:28:27 +01003369 status = psa_generate_key( &key_attributes,
Janos Follathdf3b0892019-08-08 11:12:24 +01003370 &handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003371 if( status != PSA_SUCCESS )
3372 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
3373
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01003374 /* Export the public part of the ECDH private key from PSA.
Manuel Pégourié-Gonnard5d6053f2022-02-08 10:26:19 +01003375 * The export format is an ECPoint structure as expected by TLS,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01003376 * but we just need to add a length byte before that. */
3377 unsigned char *own_pubkey = ssl->out_msg + header_len + 1;
3378 unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
3379 size_t own_pubkey_max_len = (size_t)( end - own_pubkey );
3380 size_t own_pubkey_len;
3381
Hanno Becker4a63ed42019-01-08 11:39:35 +00003382 status = psa_export_public_key( handshake->ecdh_psa_privkey,
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01003383 own_pubkey, own_pubkey_max_len,
Hanno Becker4a63ed42019-01-08 11:39:35 +00003384 &own_pubkey_len );
3385 if( status != PSA_SUCCESS )
Andrzej Kureka0237f82022-02-24 13:24:52 -05003386 {
3387 psa_destroy_key( handshake->ecdh_psa_privkey );
3388 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Becker4a63ed42019-01-08 11:39:35 +00003389 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kureka0237f82022-02-24 13:24:52 -05003390 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00003391
Manuel Pégourié-Gonnard58d23832022-01-18 12:17:15 +01003392 ssl->out_msg[header_len] = (unsigned char) own_pubkey_len;
3393 content_len = own_pubkey_len + 1;
Hanno Becker4a63ed42019-01-08 11:39:35 +00003394
Hanno Becker4a63ed42019-01-08 11:39:35 +00003395 /* The ECDH secret is the premaster secret used for key derivation. */
3396
Janos Follathdf3b0892019-08-08 11:12:24 +01003397 /* Compute ECDH shared secret. */
3398 status = psa_raw_key_agreement( PSA_ALG_ECDH,
3399 handshake->ecdh_psa_privkey,
3400 handshake->ecdh_psa_peerkey,
3401 handshake->ecdh_psa_peerkey_len,
3402 ssl->handshake->premaster,
3403 sizeof( ssl->handshake->premaster ),
3404 &ssl->handshake->pmslen );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003405
Andrzej Kureka0237f82022-02-24 13:24:52 -05003406 destruction_status = psa_destroy_key( handshake->ecdh_psa_privkey );
Ronald Croncf56a0a2020-08-04 09:51:30 +02003407 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Andrzej Kureka0237f82022-02-24 13:24:52 -05003408
3409 if( status != PSA_SUCCESS || destruction_status != PSA_SUCCESS )
3410 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003411 }
3412 else
3413#endif /* MBEDTLS_USE_PSA_CRYPTO &&
3414 ( MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Przemek Stekield905d332022-03-16 09:50:56 +01003415 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
3416 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3417 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003418#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
3419 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
3420 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3421 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
3422 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
3423 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
3424 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
3425 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01003426 {
3427 /*
3428 * ECDH key exchange -- send client public value
3429 */
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003430 header_len = 4;
Paul Bakker41c83d32013-03-20 14:39:14 +01003431
Gilles Peskineeccd8882020-03-10 12:19:08 +01003432#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003433 if( ssl->handshake->ecrs_enabled )
3434 {
Manuel Pégourié-Gonnardc37423f2018-10-16 10:28:17 +02003435 if( ssl->handshake->ecrs_state == ssl_ecrs_cke_ecdh_calc_secret )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003436 goto ecdh_calc_secret;
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +02003437
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003438 mbedtls_ecdh_enable_restart( &ssl->handshake->ecdh_ctx );
3439 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02003440#endif
3441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003442 ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx,
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003443 &content_len,
3444 &ssl->out_msg[header_len], 1000,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003445 ssl->conf->f_rng, ssl->conf->p_rng );
Paul Bakker41c83d32013-03-20 14:39:14 +01003446 if( ret != 0 )
3447 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003448 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01003449#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003450 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3451 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
3452#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01003453 return( ret );
3454 }
3455
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003456 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3457 MBEDTLS_DEBUG_ECDH_Q );
Paul Bakker41c83d32013-03-20 14:39:14 +01003458
Gilles Peskineeccd8882020-03-10 12:19:08 +01003459#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003460 if( ssl->handshake->ecrs_enabled )
3461 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003462 ssl->handshake->ecrs_n = content_len;
Manuel Pégourié-Gonnardc37423f2018-10-16 10:28:17 +02003463 ssl->handshake->ecrs_state = ssl_ecrs_cke_ecdh_calc_secret;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003464 }
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02003465
3466ecdh_calc_secret:
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003467 if( ssl->handshake->ecrs_enabled )
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003468 content_len = ssl->handshake->ecrs_n;
Manuel Pégourié-Gonnard2350b4e2017-05-16 09:26:48 +02003469#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003470 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003471 &ssl->handshake->pmslen,
3472 ssl->handshake->premaster,
3473 MBEDTLS_MPI_MAX_SIZE,
3474 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003475 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003476 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01003477#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003478 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3479 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
3480#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01003481 return( ret );
3482 }
3483
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003484 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3485 MBEDTLS_DEBUG_ECDH_Z );
Paul Bakker41c83d32013-03-20 14:39:14 +01003486 }
3487 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003488#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
3489 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
3490 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3491 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003492#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Becker1aa267c2017-04-28 17:08:27 +01003493 if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003494 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003495 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003496 * opaque psk_identity<0..2^16-1>;
3497 */
Ronald Crond491c2d2022-02-19 18:30:46 +01003498 if( mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003499 {
Hanno Becker2e4f6162018-10-23 11:54:44 +01003500 /* We don't offer PSK suites if we don't have a PSK,
3501 * and we check that the server's choice is among the
3502 * ciphersuites we offered, so this should never happen. */
3503 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003504 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003505
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003506 header_len = 4;
3507 content_len = ssl->conf->psk_identity_len;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003508
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003509 if( header_len + 2 + content_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003510 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003511 MBEDTLS_SSL_DEBUG_MSG( 1,
3512 ( "psk identity too long or SSL buffer too short" ) );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003513 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3514 }
3515
Joe Subbianifbeb6922021-07-16 14:27:50 +01003516 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1( content_len );
3517 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0( content_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003518
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003519 memcpy( ssl->out_msg + header_len,
3520 ssl->conf->psk_identity,
3521 ssl->conf->psk_identity_len );
3522 header_len += ssl->conf->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003524#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
3525 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003526 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003527 content_len = 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003528 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003529 else
3530#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003531#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
3532 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003533 {
Hanno Beckerdfab8e22018-10-23 11:59:34 +01003534#if defined(MBEDTLS_USE_PSA_CRYPTO)
3535 /* Opaque PSKs are currently only supported for PSK-only suites. */
Hanno Becker520224e2018-10-26 11:38:07 +01003536 if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
Hanno Beckerdfab8e22018-10-23 11:59:34 +01003537 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3538#endif /* MBEDTLS_USE_PSA_CRYPTO */
3539
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003540 if( ( ret = ssl_write_encrypted_pms( ssl, header_len,
3541 &content_len, 2 ) ) != 0 )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003542 return( ret );
3543 }
3544 else
3545#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003546#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3547 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003548 {
Hanno Beckerdfab8e22018-10-23 11:59:34 +01003549#if defined(MBEDTLS_USE_PSA_CRYPTO)
3550 /* Opaque PSKs are currently only supported for PSK-only suites. */
Hanno Becker520224e2018-10-26 11:38:07 +01003551 if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
Hanno Beckerdfab8e22018-10-23 11:59:34 +01003552 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3553#endif /* MBEDTLS_USE_PSA_CRYPTO */
3554
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003555 /*
3556 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
3557 */
Gilles Peskine487bbf62021-05-27 22:17:07 +02003558 content_len = mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003559
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003560 if( header_len + 2 + content_len >
3561 MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003562 {
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003563 MBEDTLS_SSL_DEBUG_MSG( 1,
3564 ( "psk identity or DHM size too long or SSL buffer too short" ) );
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02003565 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3566 }
3567
Joe Subbianifbeb6922021-07-16 14:27:50 +01003568 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1( content_len );
3569 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0( content_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003571 ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
Gilles Peskine487bbf62021-05-27 22:17:07 +02003572 (int) mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx ),
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003573 &ssl->out_msg[header_len], content_len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003574 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003575 if( ret != 0 )
3576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003577 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003578 return( ret );
3579 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003580 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003581 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003582#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
3583#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3584 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003585 {
Hanno Beckerdfab8e22018-10-23 11:59:34 +01003586#if defined(MBEDTLS_USE_PSA_CRYPTO)
3587 /* Opaque PSKs are currently only supported for PSK-only suites. */
Hanno Becker520224e2018-10-26 11:38:07 +01003588 if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
Hanno Beckerdfab8e22018-10-23 11:59:34 +01003589 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3590#endif /* MBEDTLS_USE_PSA_CRYPTO */
3591
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003592 /*
3593 * ClientECDiffieHellmanPublic public;
3594 */
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003595 ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx,
3596 &content_len,
3597 &ssl->out_msg[header_len],
3598 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01003599 ssl->conf->f_rng, ssl->conf->p_rng );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003600 if( ret != 0 )
3601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003602 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003603 return( ret );
3604 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003605
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003606 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3607 MBEDTLS_DEBUG_ECDH_Q );
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003608 }
3609 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003610#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02003611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003612 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3613 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003614 }
3615
Hanno Beckerafd311e2018-10-23 15:26:40 +01003616#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
3617 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
3618 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
Hanno Becker520224e2018-10-26 11:38:07 +01003619 ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
Hanno Beckerafd311e2018-10-23 15:26:40 +01003620 {
Ronald Cron5ee57072020-06-11 09:34:06 +02003621 MBEDTLS_SSL_DEBUG_MSG( 1,
3622 ( "skip PMS generation for opaque PSK" ) );
Hanno Beckerafd311e2018-10-23 15:26:40 +01003623 }
3624 else
3625#endif /* MBEDTLS_USE_PSA_CRYPTO &&
3626 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003627 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003628 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003629 {
Ronald Cron5ee57072020-06-11 09:34:06 +02003630 MBEDTLS_SSL_DEBUG_RET( 1,
3631 "mbedtls_ssl_psk_derive_premaster", ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003632 return( ret );
3633 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003634 }
3635 else
Gilles Peskineeccd8882020-03-10 12:19:08 +01003636#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003637#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
3638 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003639 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003640 header_len = 4;
3641 if( ( ret = ssl_write_encrypted_pms( ssl, header_len,
3642 &content_len, 0 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00003643 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003644 }
Paul Bakkered27a042013-04-18 22:46:23 +02003645 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003646#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003647#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3648 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
3649 {
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003650 header_len = 4;
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003651
3652 ret = mbedtls_ecjpake_write_round_two( &ssl->handshake->ecjpake_ctx,
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003653 ssl->out_msg + header_len,
3654 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
3655 &content_len,
Manuel Pégourié-Gonnard0f1660a2015-09-16 22:41:06 +02003656 ssl->conf->f_rng, ssl->conf->p_rng );
3657 if( ret != 0 )
3658 {
3659 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret );
3660 return( ret );
3661 }
3662
3663 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
3664 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
3665 ssl->conf->f_rng, ssl->conf->p_rng );
3666 if( ret != 0 )
3667 {
3668 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
3669 return( ret );
3670 }
3671 }
3672 else
3673#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02003674 {
3675 ((void) ciphersuite_info);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003676 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3677 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkered27a042013-04-18 22:46:23 +02003678 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003679
Hanno Beckerc14a3bb2019-01-14 09:41:16 +00003680 ssl->out_msglen = header_len + content_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003681 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3682 ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003683
3684 ssl->state++;
3685
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003686 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003687 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003688 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003689 return( ret );
3690 }
3691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003692 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003693
3694 return( 0 );
3695}
3696
Gilles Peskineeccd8882020-03-10 12:19:08 +01003697#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003698static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003699{
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003700 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003701 ssl->handshake->ciphersuite_info;
Janos Follath865b3eb2019-12-16 11:46:15 +00003702 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003704 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003706 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003707 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003708 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003709 return( ret );
3710 }
3711
Hanno Becker77adddc2019-02-07 12:32:43 +00003712 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakkered27a042013-04-18 22:46:23 +02003713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003714 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
Paul Bakkered27a042013-04-18 22:46:23 +02003715 ssl->state++;
3716 return( 0 );
3717 }
3718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003719 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3720 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003721}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003722#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003723static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003724{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003725 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Becker0d0cd4b2017-05-11 14:06:43 +01003726 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00003727 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003728 size_t n = 0, offset = 0;
3729 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003730 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003731 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02003732 size_t hashlen;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003733 void *rs_ctx = NULL;
Gilles Peskinef00f1522021-06-22 00:09:00 +02003734#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3735 size_t out_buf_len = ssl->out_buf_len - ( ssl->out_msg - ssl->out_buf );
3736#else
3737 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN - ( ssl->out_msg - ssl->out_buf );
3738#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003740 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003741
Gilles Peskineeccd8882020-03-10 12:19:08 +01003742#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003743 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003744 ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign )
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003745 {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003746 goto sign;
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003747 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003748#endif
3749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003750 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003752 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02003753 return( ret );
3754 }
3755
Hanno Becker77adddc2019-02-07 12:32:43 +00003756 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003758 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003759 ssl->state++;
3760 return( 0 );
3761 }
3762
Jerry Yufb28b882022-01-28 11:05:58 +08003763 if( ssl->handshake->client_auth == 0 ||
3764 mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003765 {
Johan Pascala89ca862020-08-25 10:03:19 +02003766 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
3767 ssl->state++;
3768 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003769 }
3770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003771 if( mbedtls_ssl_own_key( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003772 {
Manuel Pégourié-Gonnardb4b19f32015-07-07 11:41:21 +02003773 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key for certificate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003774 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003775 }
3776
3777 /*
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003778 * Make a signature of the handshake digests
Paul Bakker5121ce52009-01-03 21:22:43 +00003779 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01003780#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02003781 if( ssl->handshake->ecrs_enabled )
3782 ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign;
3783
3784sign:
3785#endif
3786
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02003787 ssl->handshake->calc_verify( ssl, hash, &hashlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003788
Ronald Cron90915f22022-03-07 11:11:36 +01003789 /*
3790 * digitally-signed struct {
3791 * opaque handshake_messages[handshake_messages_length];
3792 * };
3793 *
3794 * Taking shortcut here. We assume that the server always allows the
3795 * PRF Hash function and has sent it in the allowed signature
3796 * algorithms list received in the Certificate Request message.
3797 *
3798 * Until we encounter a server that does not, we will take this
3799 * shortcut.
3800 *
3801 * Reason: Otherwise we should have running hashes for SHA512 and
3802 * SHA224 in order to satisfy 'weird' needs from the server
3803 * side.
3804 */
3805 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01003806 {
Ronald Cron90915f22022-03-07 11:11:36 +01003807 md_alg = MBEDTLS_MD_SHA384;
3808 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003809 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003810 else
Paul Bakker577e0062013-08-28 11:57:20 +02003811 {
Ronald Cron90915f22022-03-07 11:11:36 +01003812 md_alg = MBEDTLS_MD_SHA256;
3813 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256;
Paul Bakker577e0062013-08-28 11:57:20 +02003814 }
Ronald Cron90915f22022-03-07 11:11:36 +01003815 ssl->out_msg[5] = mbedtls_ssl_sig_from_pk( mbedtls_ssl_own_key( ssl ) );
3816
3817 /* Info from md_alg will be used instead */
3818 hashlen = 0;
3819 offset = 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003820
Gilles Peskineeccd8882020-03-10 12:19:08 +01003821#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnardd27d1a52017-08-15 11:49:08 +02003822 if( ssl->handshake->ecrs_enabled )
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003823 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003824#endif
3825
3826 if( ( ret = mbedtls_pk_sign_restartable( mbedtls_ssl_own_key( ssl ),
3827 md_alg, hash_start, hashlen,
Gilles Peskinef00f1522021-06-22 00:09:00 +02003828 ssl->out_msg + 6 + offset,
3829 out_buf_len - 6 - offset,
3830 &n,
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003831 ssl->conf->f_rng, ssl->conf->p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02003832 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003833 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
Gilles Peskineeccd8882020-03-10 12:19:08 +01003834#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02003835 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3836 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
3837#endif
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003838 return( ret );
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02003839 }
Paul Bakker926af752012-11-23 13:38:07 +01003840
Joe Subbiani6dd73642021-07-19 11:56:54 +01003841 MBEDTLS_PUT_UINT16_BE( n, ssl->out_msg, offset + 4 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003842
Paul Bakker1ef83d62012-04-11 12:09:53 +00003843 ssl->out_msglen = 6 + n + offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003844 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3845 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003846
3847 ssl->state++;
3848
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003849 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003850 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003851 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003852 return( ret );
3853 }
3854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003855 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003856
Paul Bakkered27a042013-04-18 22:46:23 +02003857 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003858}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003859#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003861#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3862static int ssl_parse_new_session_ticket( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003863{
Janos Follath865b3eb2019-12-16 11:46:15 +00003864 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003865 uint32_t lifetime;
3866 size_t ticket_len;
3867 unsigned char *ticket;
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003868 const unsigned char *msg;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003870 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003871
Hanno Becker327c93b2018-08-15 13:56:18 +01003872 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003873 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003874 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003875 return( ret );
3876 }
3877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003878 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003879 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003880 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Hanno Beckerb2fff6d2017-05-08 11:06:19 +01003881 mbedtls_ssl_send_alert_message(
3882 ssl,
3883 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3884 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003885 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003886 }
3887
3888 /*
3889 * struct {
3890 * uint32 ticket_lifetime_hint;
3891 * opaque ticket<0..2^16-1>;
3892 * } NewSessionTicket;
3893 *
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003894 * 0 . 3 ticket_lifetime_hint
3895 * 4 . 5 ticket_len (n)
3896 * 6 . 5+n ticket content
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003897 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003898 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET ||
3899 ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003900 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003901 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003902 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3903 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker241c1972021-06-24 09:44:26 +01003904 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003905 }
3906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003907 msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003908
Philippe Antoineb5b25432018-05-11 11:06:29 +02003909 lifetime = ( ((uint32_t) msg[0]) << 24 ) | ( msg[1] << 16 ) |
3910 ( msg[2] << 8 ) | ( msg[3] );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003911
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003912 ticket_len = ( msg[4] << 8 ) | ( msg[5] );
3913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003914 if( ticket_len + 6 + mbedtls_ssl_hs_hdr_len( ssl ) != ssl->in_hslen )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003916 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003917 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3918 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Hanno Becker241c1972021-06-24 09:44:26 +01003919 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003920 }
3921
Paul Elliottd48d5c62021-01-07 14:47:05 +00003922 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %" MBEDTLS_PRINTF_SIZET, ticket_len ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003923
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003924 /* We're not waiting for a NewSessionTicket message any more */
3925 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003926 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003927
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003928 /*
3929 * Zero-length ticket means the server changed his mind and doesn't want
3930 * to send a ticket after all, so just forget it
3931 */
Paul Bakker66d5d072014-06-17 16:39:18 +02003932 if( ticket_len == 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003933 return( 0 );
3934
Hanno Beckerb2964cb2019-01-30 14:46:35 +00003935 if( ssl->session != NULL && ssl->session->ticket != NULL )
3936 {
3937 mbedtls_platform_zeroize( ssl->session->ticket,
3938 ssl->session->ticket_len );
3939 mbedtls_free( ssl->session->ticket );
3940 ssl->session->ticket = NULL;
3941 ssl->session->ticket_len = 0;
3942 }
3943
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003944 mbedtls_platform_zeroize( ssl->session_negotiate->ticket,
3945 ssl->session_negotiate->ticket_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003946 mbedtls_free( ssl->session_negotiate->ticket );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003947 ssl->session_negotiate->ticket = NULL;
3948 ssl->session_negotiate->ticket_len = 0;
3949
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003950 if( ( ticket = mbedtls_calloc( 1, ticket_len ) ) == NULL )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003951 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003952 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ticket alloc failed" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02003953 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3954 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003955 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003956 }
3957
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02003958 memcpy( ticket, msg + 6, ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003959
3960 ssl->session_negotiate->ticket = ticket;
3961 ssl->session_negotiate->ticket_len = ticket_len;
3962 ssl->session_negotiate->ticket_lifetime = lifetime;
3963
3964 /*
3965 * RFC 5077 section 3.4:
3966 * "If the client receives a session ticket from the server, then it
3967 * discards any Session ID that was sent in the ServerHello."
3968 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003969 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02003970 ssl->session_negotiate->id_len = 0;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003972 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003973
3974 return( 0 );
3975}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003976#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003977
Paul Bakker5121ce52009-01-03 21:22:43 +00003978/*
Paul Bakker1961b702013-01-25 14:49:24 +01003979 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003980 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003981int mbedtls_ssl_handshake_client_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003982{
3983 int ret = 0;
3984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003985 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
Paul Bakker1961b702013-01-25 14:49:24 +01003986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003987 /* Change state now, so that it is right in mbedtls_ssl_read_record(), used
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003988 * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003989#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3990 if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC &&
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003991 ssl->handshake->new_session_ticket != 0 )
3992 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003993 ssl->state = MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET;
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003994 }
3995#endif
3996
Paul Bakker1961b702013-01-25 14:49:24 +01003997 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003998 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003999 case MBEDTLS_SSL_HELLO_REQUEST:
4000 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00004001 break;
4002
Paul Bakker1961b702013-01-25 14:49:24 +01004003 /*
4004 * ==> ClientHello
4005 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004006 case MBEDTLS_SSL_CLIENT_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01004007 ret = ssl_write_client_hello( ssl );
4008 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004009
Paul Bakker1961b702013-01-25 14:49:24 +01004010 /*
4011 * <== ServerHello
4012 * Certificate
4013 * ( ServerKeyExchange )
4014 * ( CertificateRequest )
4015 * ServerHelloDone
4016 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004017 case MBEDTLS_SSL_SERVER_HELLO:
Paul Bakker1961b702013-01-25 14:49:24 +01004018 ret = ssl_parse_server_hello( ssl );
4019 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004021 case MBEDTLS_SSL_SERVER_CERTIFICATE:
4022 ret = mbedtls_ssl_parse_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004023 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004025 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01004026 ret = ssl_parse_server_key_exchange( ssl );
4027 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004029 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Paul Bakker1961b702013-01-25 14:49:24 +01004030 ret = ssl_parse_certificate_request( ssl );
4031 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004033 case MBEDTLS_SSL_SERVER_HELLO_DONE:
Paul Bakker1961b702013-01-25 14:49:24 +01004034 ret = ssl_parse_server_hello_done( ssl );
4035 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004036
Paul Bakker1961b702013-01-25 14:49:24 +01004037 /*
4038 * ==> ( Certificate/Alert )
4039 * ClientKeyExchange
4040 * ( CertificateVerify )
4041 * ChangeCipherSpec
4042 * Finished
4043 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004044 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
4045 ret = mbedtls_ssl_write_certificate( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004046 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004048 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
Paul Bakker1961b702013-01-25 14:49:24 +01004049 ret = ssl_write_client_key_exchange( ssl );
4050 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004052 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Paul Bakker1961b702013-01-25 14:49:24 +01004053 ret = ssl_write_certificate_verify( ssl );
4054 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004056 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
4057 ret = mbedtls_ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004058 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004060 case MBEDTLS_SSL_CLIENT_FINISHED:
4061 ret = mbedtls_ssl_write_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004062 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004063
Paul Bakker1961b702013-01-25 14:49:24 +01004064 /*
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02004065 * <== ( NewSessionTicket )
4066 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01004067 * Finished
4068 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004069#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4070 case MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET:
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02004071 ret = ssl_parse_new_session_ticket( ssl );
4072 break;
Paul Bakkera503a632013-08-14 13:48:06 +02004073#endif
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02004074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004075 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
4076 ret = mbedtls_ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004077 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004079 case MBEDTLS_SSL_SERVER_FINISHED:
4080 ret = mbedtls_ssl_parse_finished( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004081 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004083 case MBEDTLS_SSL_FLUSH_BUFFERS:
4084 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
4085 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Paul Bakker1961b702013-01-25 14:49:24 +01004086 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004088 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
4089 mbedtls_ssl_handshake_wrapup( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01004090 break;
Paul Bakker48916f92012-09-16 19:57:18 +00004091
Paul Bakker1961b702013-01-25 14:49:24 +01004092 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004093 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
4094 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1961b702013-01-25 14:49:24 +01004095 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004096
4097 return( ret );
4098}
Jerry Yuc5aef882021-12-23 20:15:02 +08004099
Jerry Yufb4b6472022-01-27 15:03:26 +08004100#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_2 */