Skip to content

Commit b32da31

Browse files
committed
WIP
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
1 parent 0521d64 commit b32da31

File tree

4 files changed

+107
-1
lines changed

4 files changed

+107
-1
lines changed

library/ssl_tls13_server.c

+3
Original file line numberDiff line numberDiff line change
@@ -1380,6 +1380,7 @@ static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
13801380
}
13811381

13821382
if (ret == 0) {
1383+
MBEDTLS_SSL_DEBUG_MSG(2, ("no supported_versions extension"));
13831384
return SSL_CLIENT_HELLO_TLS1_2;
13841385
}
13851386

@@ -1401,6 +1402,7 @@ static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
14011402
* the TLS version to negotiate.
14021403
*/
14031404
if (MBEDTLS_SSL_VERSION_TLS1_2 == ret) {
1405+
MBEDTLS_SSL_DEBUG_MSG(2, ("supported_versions without 1.3"));
14041406
return SSL_CLIENT_HELLO_TLS1_2;
14051407
}
14061408
}
@@ -1985,6 +1987,7 @@ static int ssl_tls13_process_client_hello(mbedtls_ssl_context *ssl)
19851987
}
19861988
ssl->keep_current_message = 1;
19871989
ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
1990+
MBEDTLS_SSL_DEBUG_MSG(1, ("non-1.3 ClientHello left for later processing"));
19881991
return 0;
19891992
}
19901993

tests/src/test_helpers/ssl_helpers.c

+13
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,22 @@ void mbedtls_test_ssl_log_analyzer(void *ctx, int level,
2828
{
2929
mbedtls_test_ssl_log_pattern *p = (mbedtls_test_ssl_log_pattern *) ctx;
3030

31+
/* Change 0 to 1 for debugging of test cases that use this function. */
32+
#if 0
33+
const char *q, *basename;
34+
/* Extract basename from file */
35+
for (q = basename = file; *q != '\0'; q++) {
36+
if (*q == '/' || *q == '\\') {
37+
basename = q + 1;
38+
}
39+
}
40+
printf("%s:%04d: |%d| %s",
41+
basename, line, level, str);
42+
#else
3143
(void) level;
3244
(void) line;
3345
(void) file;
46+
#endif
3447

3548
if (NULL != p &&
3649
NULL != p->pattern &&

tests/suites/test_suite_ssl.data

+29
Original file line numberDiff line numberDiff line change
@@ -3445,3 +3445,32 @@ tls13_srv_max_early_data_size:TEST_EARLY_DATA_HRR:3:3
34453445

34463446
TLS 1.3 srv, max early data size, HRR, 98, wsz=49
34473447
tls13_srv_max_early_data_size:TEST_EARLY_DATA_HRR:97:0
3448+
3449+
# 1.2 minimal ClientHello breakdown:
3450+
# 160303rlrl - record header, 2-byte record contents len
3451+
# 01hlhlhl - handshake header, 3-byte handshake message len
3452+
# 0303 - protocol version: 1.2
3453+
# 0123456789abcdef (repeated, 4 times total) - 32-byte "random"
3454+
# 00 - session ID (empty)
3455+
# 0002cvcv - ciphersuite list: 2-byte len + list of 2-byte values (see below)
3456+
# 0100 - compression methods: 1-byte len then "null" (only legal value now)
3457+
# [then end, or extensions]
3458+
# elel - 2-byte extensions length
3459+
# ...
3460+
#
3461+
# Note: currently our TLS "1.3 or 1.2" code requires extension length to be
3462+
# present even it it's 0. This is not strictly compliant but doesn't matter
3463+
# much in practice as these days everyone want to use signature_algorithms
3464+
# (for hashes better than SHA-1), secure_renego (even if you have renego
3465+
# disabled), and most people want either ECC or PSK related extensions.
3466+
#
3467+
# Note: cccc is currently not assigned, so can be used get a consistent
3468+
# "no matching ciphersuite" behaviour regardless of the configuration.
3469+
# 002f is the mandatory-to-implement TLS 1.2 ciphersuite, but removed in 4.0.
3470+
Inject ClientHello - TLS 1.2 good (for reference)
3471+
depends_on:MBEDTLS_SSL_PROTO_TLS1_2
3472+
inject_cleartext_handshake_msg:MBEDTLS_SSL_CLIENT_HELLO:"160303002f0100002b03030123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef000002002f01000000":"<= parse client hello":0
3473+
3474+
Inject ClientHello - TLS 1.2 unknown ciphersuite (for reference)
3475+
depends_on:MBEDTLS_SSL_PROTO_TLS1_2
3476+
inject_cleartext_handshake_msg:MBEDTLS_SSL_CLIENT_HELLO:"160303002f0100002b03030123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef000002cccc01000000":"got no ciphersuites in common":MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE

tests/suites/test_suite_ssl.function

+62-1
Original file line numberDiff line numberDiff line change
@@ -2784,7 +2784,7 @@ void handshake_fragmentation(int mfl,
27842784
options.srv_log_obj = &srv_pattern;
27852785
options.cli_log_obj = &cli_pattern;
27862786
options.srv_log_fun = mbedtls_test_ssl_log_analyzer;
2787-
options.cli_log_fun = mbedtls_test_ssl_log_analyzer;
2787+
options.cli_log_fun = mbdtls_test_ssl_log_analyzer;
27882788

27892789
mbedtls_test_ssl_perform_handshake(&options);
27902790

@@ -5037,3 +5037,64 @@ exit:
50375037
PSA_DONE();
50385038
}
50395039
/* END_CASE */
5040+
5041+
/* BEGIN_CASE */
5042+
void inject_cleartext_handshake_msg(int state, data_t *hello, char *log_pattern, int expected_ret)
5043+
{
5044+
/* This function allows us to inject crafted records at a specific point
5045+
* in the handshake, as if we were an active network attacker.
5046+
*/
5047+
enum { BUFFSIZE = 16384 };
5048+
mbedtls_test_ssl_endpoint server, client;
5049+
mbedtls_platform_zeroize(&server, sizeof(server));
5050+
mbedtls_platform_zeroize(&client, sizeof(client));
5051+
mbedtls_test_handshake_test_options options;
5052+
mbedtls_test_init_handshake_options(&options);
5053+
mbedtls_test_ssl_log_pattern srv_pattern = { 0 };
5054+
int ret = -1;
5055+
5056+
PSA_INIT();
5057+
5058+
srv_pattern.pattern = log_pattern;
5059+
options.srv_log_obj = &srv_pattern;
5060+
options.srv_log_fun = mbedtls_test_ssl_log_analyzer;
5061+
mbedtls_debug_set_threshold(3);
5062+
5063+
ret = mbedtls_test_ssl_endpoint_init(&server, MBEDTLS_SSL_IS_SERVER,
5064+
&options, NULL, NULL, NULL);
5065+
TEST_EQUAL(ret, 0);
5066+
5067+
ret = mbedtls_test_ssl_endpoint_init(&client, MBEDTLS_SSL_IS_CLIENT,
5068+
&options, NULL, NULL, NULL);
5069+
TEST_EQUAL(ret, 0);
5070+
5071+
ret = mbedtls_test_mock_socket_connect(&server.socket, &client.socket,
5072+
BUFFSIZE);
5073+
TEST_EQUAL(ret, 0);
5074+
5075+
/* Make the server move to the required state */
5076+
ret = mbedtls_test_move_handshake_to_state(&client.ssl, &server.ssl, state);
5077+
TEST_EQUAL(ret, 0);
5078+
5079+
/* Send the crafted message */
5080+
ret = mbedtls_test_mock_tcp_send_b(&client.socket, hello->x, hello->len);
5081+
TEST_ASSERT(ret >= 0 && (size_t) ret == hello->len);
5082+
5083+
/* Have the server process it.
5084+
* Need the loop because a server that support 1.3 and 1.2
5085+
* will process a 1.2 ClientHello in two steps.
5086+
*/
5087+
do {
5088+
ret = mbedtls_ssl_handshake_step(&server.ssl);
5089+
} while (ret == 0 && server.ssl.state == state);
5090+
TEST_EQUAL(ret, expected_ret);
5091+
TEST_EQUAL(srv_pattern.counter, 1);
5092+
5093+
exit:
5094+
mbedtls_test_free_handshake_options(&options);
5095+
mbedtls_test_ssl_endpoint_free(&server, NULL);
5096+
mbedtls_test_ssl_endpoint_free(&client, NULL);
5097+
mbedtls_debug_set_threshold(0);
5098+
PSA_DONE();
5099+
}
5100+
/* END_CASE */

0 commit comments

Comments
 (0)