blob: a6a0abefae7c47231c436748bd3f9f25fd8fc0b3 [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/*
41 * ssl_tls13_write_alpn_ext()
42 *
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 */
53static int ssl_tls13_write_alpn_ext( mbedtls_ssl_context *ssl,
54 unsigned char *buf,
55 const unsigned char *end,
56 size_t *out_len )
57{
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 */
112static int ssl_tls13_write_client_hello_cipher_suites(
113 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/*
177 * Structure of ClientHello message:
178 *
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;
187 */
188static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl,
189 unsigned char *buf,
190 unsigned char *end,
191 size_t *out_len )
192{
193
194 int ret;
195 unsigned char *p_extensions_len; /* Pointer to extensions length */
196 size_t output_len; /* Length of buffer used by function */
197 size_t extensions_len; /* Length of the list of extensions*/
198
199 /* Buffer management */
200 unsigned char *p = buf;
201
202 *out_len = 0;
203
204 /* No validation needed here. It has been done by ssl_conf_check() */
205 ssl->major_ver = ssl->conf->min_major_ver;
206 ssl->minor_ver = ssl->conf->min_minor_ver;
207
208 /*
209 * Write legacy_version
210 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
211 *
212 * For TLS 1.3 we use the legacy version number {0x03, 0x03}
213 * instead of the true version number.
214 */
215 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
216 MBEDTLS_PUT_UINT16_BE( 0x0303, p, 0 );
217 p += 2;
218
219 /* Write the random bytes ( random ).*/
220 MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
221 memcpy( p, ssl->handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
222 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
223 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
224 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
225
226 /*
227 * Write legacy_session_id
228 *
229 * Versions of TLS before TLS 1.3 supported a "session resumption" feature
230 * which has been merged with pre-shared keys in this version. A client
231 * which has a cached session ID set by a pre-TLS 1.3 server SHOULD set
232 * this field to that value. In compatibility mode, this field MUST be
233 * non-empty, so a client not offering a pre-TLS 1.3 session MUST generate
234 * a new 32-byte value. This value need not be random but SHOULD be
235 * unpredictable to avoid implementations fixating on a specific value
236 * ( also known as ossification ). Otherwise, it MUST be set as a zero-length
237 * vector ( i.e., a zero-valued single byte length field ).
238 */
239#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
240 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ssl->session_negotiate->id_len + 1 );
241 *p++ = (unsigned char)ssl->session_negotiate->id_len;
242 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
243 p += ssl->session_negotiate->id_len;
244
245 MBEDTLS_SSL_DEBUG_BUF( 3, "session id", ssl->session_negotiate->id,
246 ssl->session_negotiate->id_len );
247#else
248 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 );
249 *p++ = 0; /* session id length set to zero */
250#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
251
252 /* Write cipher_suites */
253 ret = ssl_tls13_write_client_hello_cipher_suites( ssl, p, end, &output_len );
254 if( ret != 0 )
255 return( ret );
256 p += output_len;
257
258 /* Write legacy_compression_methods
259 *
260 * For every TLS 1.3 ClientHello, this vector MUST contain exactly
261 * one byte set to zero, which corresponds to the 'null' compression
262 * method in prior versions of TLS.
263 */
264 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
265 *p++ = 1;
266 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
267
268 /* Write extensions */
269
270#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
271 /* Keeping track of the included extensions */
272 ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
273#endif
274
275 /* First write extensions, then the total length */
276 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
277 p_extensions_len = p;
278 p += 2;
279
280#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
281 ret = mbedtls_ssl_tls13_write_client_hello_exts( ssl, p, end, &output_len );
282 if( ret != 0 )
283 return( ret );
284 p += output_len;
285#endif
286
287#if defined(MBEDTLS_SSL_ALPN)
288 ret = ssl_tls13_write_alpn_ext( ssl, p, end, &output_len );
289 if( ret != 0 )
290 return( ret );
291 p += output_len;
292#endif /* MBEDTLS_SSL_ALPN */
293
294#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
295#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
296 if( mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
297 {
298 ret = mbedtls_ssl_write_supported_groups_ext( ssl, p, end, &output_len );
299 if( ret != 0 )
300 return( ret );
301 p += output_len;
302 }
303
304 if( mbedtls_ssl_conf_tls13_ephemeral_enabled( ssl ) )
305 {
306 ret = mbedtls_ssl_write_sig_alg_ext( ssl, p, end, &output_len );
307 if( ret != 0 )
308 return( ret );
309 p += output_len;
310 }
311#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
312#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
313
314#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
315 /* Write server name extension */
316 ret = mbedtls_ssl_write_hostname_ext( ssl, p, end, &output_len );
317 if( ret != 0 )
318 return( ret );
319 p += output_len;
320#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
321
322 /* Add more extensions here */
323
324 /* Write the length of the list of extensions. */
325 extensions_len = p - p_extensions_len - 2;
326 MBEDTLS_PUT_UINT16_BE( extensions_len, p_extensions_len, 0 );
327 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET ,
328 extensions_len ) );
329 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", p_extensions_len, extensions_len );
330
331 *out_len = p - buf;
332 return( 0 );
333}
334
335static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl )
336{
337 int ret;
338
339 if( ssl->conf->f_rng == NULL )
340 {
341 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
342 return( MBEDTLS_ERR_SSL_NO_RNG );
343 }
344
345 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
346 ssl->handshake->randbytes,
347 MBEDTLS_CLIENT_HELLO_RANDOM_LEN ) ) != 0 )
348 {
349 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
350 return( ret );
351 }
352
353#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
354 /*
355 * Create a session identifier for the purpose of middlebox compatibility
356 * only if one has not been created already.
357 */
358 if( ssl->session_negotiate->id_len == 0 )
359 {
360 /* Creating a session id with 32 byte length */
361 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
362 ssl->session_negotiate->id, 32 ) ) != 0 )
363 {
364 MBEDTLS_SSL_DEBUG_RET( 1, "creating session id failed", ret );
365 return( ret );
366 }
367 ssl->session_negotiate->id_len = 32;
368 }
369#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
370
371 return( 0 );
372}
373
374/*
375 * Write ClientHello handshake message.
376 * Handler for MBEDTLS_SSL_CLIENT_HELLO
377 */
378int mbedtls_ssl_write_client_hello( mbedtls_ssl_context *ssl )
379{
380 int ret = 0;
381 unsigned char *buf;
382 size_t buf_len, msg_len;
383
384 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
385
386 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_client_hello( ssl ) );
387
388 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg(
389 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
390 &buf, &buf_len ) );
391
392 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_client_hello_body( ssl, buf,
393 buf + buf_len,
394 &msg_len ) );
395
396 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
397 buf, msg_len );
398
399 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg( ssl,
400 buf_len,
401 msg_len ) );
402
403 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
404
405cleanup:
406
407 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
408 return ret;
409}
410
411#endif /* MBEDTLS_SSL_PROTO_TLS1_3 || MBEDTLS_SSL_PROTO_TLS1_2 */
412#endif /* MBEDTLS_SSL_CLI_C */