blob: 8ab53b5e8d260599ac8dd58be79ee3c8f7226ecb [file] [log] [blame]
Ronald Cron3d580bf2022-02-18 17:24:56 +01001/*
2 * TLS 1.2 and 1.3 client-side functions
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS ( https://tls.mbed.org )
20 */
21
22#include "common.h"
23
24#if defined(MBEDTLS_SSL_CLI_C)
25#if defined(MBEDTLS_SSL_PROTO_TLS1_3) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
26
27#include <string.h>
28
29#include "mbedtls/debug.h"
30#include "mbedtls/error.h"
31#include "mbedtls/platform.h"
32
33#include "ssl_client.h"
34#include "ssl_misc.h"
35#include "ecdh_misc.h"
36#include "ssl_tls13_keys.h"
37#include "ssl_debug_helpers.h"
38
39#if defined(MBEDTLS_SSL_ALPN)
40/*
Ronald Cron71c23322022-02-18 17:29:39 +010041 * ssl_write_alpn_ext()
Ronald Cron3d580bf2022-02-18 17:24:56 +010042 *
43 * Structure of the application_layer_protocol_negotiation extension in
44 * ClientHello:
45 *
46 * opaque ProtocolName<1..2^8-1>;
47 *
48 * struct {
49 * ProtocolName protocol_name_list<2..2^16-1>
50 * } ProtocolNameList;
51 *
52 */
Ronald Cron71c23322022-02-18 17:29:39 +010053static int ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
54 unsigned char *buf,
55 const unsigned char *end,
56 size_t *out_len )
Ronald Cron3d580bf2022-02-18 17:24:56 +010057{
58 unsigned char *p = buf;
59
60 *out_len = 0;
61
62 if( ssl->conf->alpn_list == NULL )
63 return( 0 );
64
65 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) );
66
67
68 /* Check we have enough space for the extension type (2 bytes), the
69 * extension length (2 bytes) and the protocol_name_list length (2 bytes).
70 */
71 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
72 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
73 /* Skip writing extension and list length for now */
74 p += 6;
75
76 /*
77 * opaque ProtocolName<1..2^8-1>;
78 *
79 * struct {
80 * ProtocolName protocol_name_list<2..2^16-1>
81 * } ProtocolNameList;
82 */
83 for( const char **cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
84 {
85 /*
86 * mbedtls_ssl_conf_set_alpn_protocols() checked that the length of
87 * protocol names is less than 255.
88 */
89 size_t protocol_name_len = strlen( *cur );
90
91 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 + protocol_name_len );
92 *p++ = (unsigned char)protocol_name_len;
93 memcpy( p, *cur, protocol_name_len );
94 p += protocol_name_len;
95 }
96
97 *out_len = p - buf;
98
99 /* List length = *out_len - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
100 MBEDTLS_PUT_UINT16_BE( *out_len - 6, buf, 4 );
101
102 /* Extension length = *out_len - 2 (ext_type) - 2 (ext_len) */
103 MBEDTLS_PUT_UINT16_BE( *out_len - 4, buf, 2 );
104
105 return( 0 );
106}
107#endif /* MBEDTLS_SSL_ALPN */
108
109/* Write cipher_suites
110 * CipherSuite cipher_suites<2..2^16-2>;
111 */
Ronald Cron71c23322022-02-18 17:29:39 +0100112static int ssl_write_client_hello_cipher_suites(
Ronald Cron3d580bf2022-02-18 17:24:56 +0100113 mbedtls_ssl_context *ssl,
114 unsigned char *buf,
115 unsigned char *end,
116 size_t *out_len )
117{
118 unsigned char *p = buf;
119 const int *ciphersuite_list;
120 unsigned char *cipher_suites; /* Start of the cipher_suites list */
121 size_t cipher_suites_len;
122
123 *out_len = 0 ;
124
125 /*
126 * Ciphersuite list
127 *
128 * This is a list of the symmetric cipher options supported by
129 * the client, specifically the record protection algorithm
130 * ( including secret key length ) and a hash to be used with
131 * HKDF, in descending order of client preference.
132 */
133 ciphersuite_list = ssl->conf->ciphersuite_list;
134
135 /* Check there is space for the cipher suite list length (2 bytes). */
136 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
137 p += 2;
138
139 /* Write cipher_suites */
140 cipher_suites = p;
141 for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
142 {
143 int cipher_suite = ciphersuite_list[i];
144 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
145
146 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
147 if( ciphersuite_info == NULL )
148 continue;
149 if( !( MBEDTLS_SSL_MINOR_VERSION_4 >= ciphersuite_info->min_minor_ver &&
150 MBEDTLS_SSL_MINOR_VERSION_4 <= ciphersuite_info->max_minor_ver ) )
151 continue;
152
153 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
154 (unsigned int) cipher_suite,
155 ciphersuite_info->name ) );
156
157 /* Check there is space for the cipher suite identifier (2 bytes). */
158 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
159 MBEDTLS_PUT_UINT16_BE( cipher_suite, p, 0 );
160 p += 2;
161 }
162
163 /* Write the cipher_suites length in number of bytes */
164 cipher_suites_len = p - cipher_suites;
165 MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 );
166 MBEDTLS_SSL_DEBUG_MSG( 3,
167 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
168 cipher_suites_len/2 ) );
169
170 /* Output the total length of cipher_suites field. */
171 *out_len = p - buf;
172
173 return( 0 );
174}
175
176/*
Ronald Cron5456a7f2022-02-18 17:38:42 +0100177 * Structure of the TLS 1.3 ClientHello message:
Ronald Cron3d580bf2022-02-18 17:24:56 +0100178 *
179 * struct {
180 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
181 * Random random;
182 * opaque legacy_session_id<0..32>;
183 * CipherSuite cipher_suites<2..2^16-2>;
184 * opaque legacy_compression_methods<1..2^8-1>;
185 * Extension extensions<8..2^16-1>;
186 * } ClientHello;
Ronald Cron5456a7f2022-02-18 17:38:42 +0100187 *
188 * Structure of the (D)TLS 1.2 ClientHello message:
189 *
190 * struct {
191 * ProtocolVersion client_version;
192 * Random random;
193 * SessionID session_id;
194 * opaque cookie<0..2^8-1>; // DTLS 1.2 ONLY
195 * CipherSuite cipher_suites<2..2^16-2>;
196 * CompressionMethod compression_methods<1..2^8-1>;
197 * select (extensions_present) {
198 * case false:
199 * struct {};
200 * case true:
201 * Extension extensions<0..2^16-1>;
202 * };
203 * } ClientHello;
Ronald Cron3d580bf2022-02-18 17:24:56 +0100204 */
Ronald Cron71c23322022-02-18 17:29:39 +0100205static int ssl_write_client_hello_body( mbedtls_ssl_context *ssl,
206 unsigned char *buf,
207 unsigned char *end,
208 size_t *out_len )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100209{
210
211 int ret;
212 unsigned char *p_extensions_len; /* Pointer to extensions length */
213 size_t output_len; /* Length of buffer used by function */
214 size_t extensions_len; /* Length of the list of extensions*/
215
216 /* Buffer management */
217 unsigned char *p = buf;
218
219 *out_len = 0;
220
Ronald Cron3d580bf2022-02-18 17:24:56 +0100221 /*
222 * Write legacy_version
223 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
224 *
225 * For TLS 1.3 we use the legacy version number {0x03, 0x03}
226 * instead of the true version number.
227 */
228 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
229 MBEDTLS_PUT_UINT16_BE( 0x0303, p, 0 );
230 p += 2;
231
232 /* Write the random bytes ( random ).*/
233 MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
234 memcpy( p, ssl->handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
235 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
236 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
237 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
238
239 /*
240 * Write legacy_session_id
241 *
242 * Versions of TLS before TLS 1.3 supported a "session resumption" feature
243 * which has been merged with pre-shared keys in this version. A client
244 * which has a cached session ID set by a pre-TLS 1.3 server SHOULD set
245 * this field to that value. In compatibility mode, this field MUST be
246 * non-empty, so a client not offering a pre-TLS 1.3 session MUST generate
247 * a new 32-byte value. This value need not be random but SHOULD be
248 * unpredictable to avoid implementations fixating on a specific value
249 * ( also known as ossification ). Otherwise, it MUST be set as a zero-length
250 * vector ( i.e., a zero-valued single byte length field ).
251 */
252#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
253 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ssl->session_negotiate->id_len + 1 );
254 *p++ = (unsigned char)ssl->session_negotiate->id_len;
255 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
256 p += ssl->session_negotiate->id_len;
257
258 MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id,
259 ssl->session_negotiate->id_len );
260#else
261 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 );
262 *p++ = 0; /* session id length set to zero */
263#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
264
265 /* Write cipher_suites */
Ronald Cron71c23322022-02-18 17:29:39 +0100266 ret = ssl_write_client_hello_cipher_suites( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100267 if( ret != 0 )
268 return( ret );
269 p += output_len;
270
271 /* Write legacy_compression_methods
272 *
273 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
274 * one byte set to zero, which corresponds to the 'null' compression
275 * method in prior versions of TLS.
276 */
277 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
278 *p++ = 1;
279 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
280
281 /* Write extensions */
282
283#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
284 /* Keeping track of the included extensions */
285 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
286#endif
287
288 /* First write extensions, then the total length */
289 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
290 p_extensions_len = p;
291 p += 2;
292
293#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
294 ret = mbedtls_ssl_tls13_write_client_hello_exts( ssl, p, end, &output_len );
295 if( ret != 0 )
296 return( ret );
297 p += output_len;
298#endif
299
300#if defined(MBEDTLS_SSL_ALPN)
Ronald Cron71c23322022-02-18 17:29:39 +0100301 ret = ssl_write_alpn_ext( ssl, p, end, &output_len );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100302 if( ret != 0 )
303 return( ret );
304 p += output_len;
305#endif /* MBEDTLS_SSL_ALPN */
306
307#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
308#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
309 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
310 {
311 ret = mbedtls_ssl_write_supported_groups_ext( ssl, p, end, &output_len );
312 if( ret != 0 )
313 return( ret );
314 p += output_len;
315 }
316
317 if( mbedtls_ssl_conf_tls13_ephemeral_enabled( ssl ) )
318 {
319 ret = mbedtls_ssl_write_sig_alg_ext( ssl, p, end, &output_len );
320 if( ret != 0 )
321 return( ret );
322 p += output_len;
323 }
324#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
325#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
326
327#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
328 /* Write server name extension */
329 ret = mbedtls_ssl_write_hostname_ext( ssl, p, end, &output_len );
330 if( ret != 0 )
331 return( ret );
332 p += output_len;
333#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
334
335 /* Add more extensions here */
336
337 /* Write the length of the list of extensions. */
338 extensions_len = p - p_extensions_len - 2;
339 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
340 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET ,
341 extensions_len ) );
342 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p_extensions_len, extensions_len );
343
344 *out_len = p - buf;
345 return( 0 );
346}
347
Ronald Cron71c23322022-02-18 17:29:39 +0100348static int ssl_prepare_client_hello( mbedtls_ssl_context *ssl )
Ronald Cron3d580bf2022-02-18 17:24:56 +0100349{
350 int ret;
351
352 if( ssl->conf->f_rng == NULL )
353 {
354 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
355 return( MBEDTLS_ERR_SSL_NO_RNG );
356 }
357
Ronald Cron86a477f2022-02-18 17:45:10 +0100358 /* Bet on the highest configured version if we are not in a TLS 1.2
359 * renegotiation or session resumption.
360 */
361#if defined(MBEDTLS_SSL_RENEGOTIATION)
362 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
363 ssl->handshake->min_minor_ver = ssl->minor_ver;
364 else
365#endif
366 {
367 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
368
369 if( ssl->handshake->resume )
370 {
371 ssl->minor_ver = ssl->session_negotiate->minor_ver;
372 ssl->handshake->min_minor_ver = ssl->minor_ver;
373 }
374 else
375 {
376 ssl->minor_ver = ssl->conf->max_minor_ver;
377 ssl->handshake->min_minor_ver = ssl->conf->min_minor_ver;
378 }
379 }
380
Ronald Cron3d580bf2022-02-18 17:24:56 +0100381 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
382 ssl->handshake->randbytes,
383 MBEDTLS_CLIENT_HELLO_RANDOM_LEN ) ) != 0 )
384 {
385 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
386 return( ret );
387 }
388
389#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
390 /*
391 * Create a session identifier for the purpose of middlebox compatibility
392 * only if one has not been created already.
393 */
394 if( ssl->session_negotiate->id_len == 0 )
395 {
396 /* Creating a session id with 32 byte length */
397 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
398 ssl->session_negotiate->id, 32 ) ) != 0 )
399 {
400 MBEDTLS_SSL_DEBUG_RET( 1, "creating session id failed", ret );
401 return( ret );
402 }
403 ssl->session_negotiate->id_len = 32;
404 }
405#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
406
407 return( 0 );
408}
409
410/*
411 * Write ClientHello handshake message.
412 * Handler for MBEDTLS_SSL_CLIENT_HELLO
413 */
414int mbedtls_ssl_write_client_hello( mbedtls_ssl_context *ssl )
415{
416 int ret = 0;
417 unsigned char *buf;
418 size_t buf_len, msg_len;
419
420 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
421
Ronald Cron71c23322022-02-18 17:29:39 +0100422 MBEDTLS_SSL_PROC_CHK( ssl_prepare_client_hello( ssl ) );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100423
424 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg(
425 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
426 &buf, &buf_len ) );
427
Ronald Cron71c23322022-02-18 17:29:39 +0100428 MBEDTLS_SSL_PROC_CHK( ssl_write_client_hello_body( ssl, buf,
429 buf + buf_len,
430 &msg_len ) );
Ronald Cron3d580bf2022-02-18 17:24:56 +0100431
432 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
433 buf, msg_len );
434
435 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg( ssl,
436 buf_len,
437 msg_len ) );
438
439 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
440
441cleanup:
442
443 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
444 return ret;
445}
446
447#endif /* MBEDTLS_SSL_PROTO_TLS1_3 || MBEDTLS_SSL_PROTO_TLS1_2 */
448#endif /* MBEDTLS_SSL_CLI_C */