Skip to content

Commit b6ad19b

Browse files
authored
Merge pull request #9976 from mpg/defragment-ext-test-3.6
Defragment ext test 3.6
2 parents d3ca688 + 43a04e7 commit b6ad19b

File tree

4 files changed

+422
-1
lines changed

4 files changed

+422
-1
lines changed

library/ssl_tls13_server.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ static void ssl_tls13_select_ciphersuite(
9292
return;
9393
}
9494

95-
MBEDTLS_SSL_DEBUG_MSG(2, ("No matched ciphersuite, psk_ciphersuite_id=%x, psk_hash_alg=%lx",
95+
MBEDTLS_SSL_DEBUG_MSG(1, ("No matched ciphersuite, psk_ciphersuite_id=%x, psk_hash_alg=%lx",
9696
(unsigned) psk_ciphersuite_id,
9797
(unsigned long) psk_hash_alg));
9898
}
@@ -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

Lines changed: 13 additions & 0 deletions
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.function

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5037,3 +5037,193 @@ exit:
50375037
PSA_DONE();
50385038
}
50395039
/* END_CASE */
5040+
5041+
/* BEGIN_CASE depends_on:MBEDTLS_DEBUG_C:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
5042+
void inject_client_content_on_the_wire(int pk_alg,
5043+
int state, data_t *data,
5044+
char *log_pattern, int expected_ret)
5045+
{
5046+
/* This function allows us to inject content at a specific state
5047+
* in the handshake, or when it's completed. The content is injected
5048+
* on the mock TCP socket, as if we were an active network attacker.
5049+
*
5050+
* This function is suitable to inject:
5051+
* - crafted records, at any point;
5052+
* - valid records that contain crafted handshake messages, but only
5053+
* when the traffic is still unprotected (for TLS 1.2 that's most of the
5054+
* handshake, for TLS 1.3 that's only the Hello messages);
5055+
* - handshake messages that are fragmented in a specific way,
5056+
* under the same conditions as above.
5057+
*/
5058+
enum { BUFFSIZE = 16384 };
5059+
mbedtls_test_ssl_endpoint server, client;
5060+
mbedtls_platform_zeroize(&server, sizeof(server));
5061+
mbedtls_platform_zeroize(&client, sizeof(client));
5062+
mbedtls_test_handshake_test_options options;
5063+
mbedtls_test_init_handshake_options(&options);
5064+
mbedtls_test_ssl_log_pattern srv_pattern;
5065+
memset(&srv_pattern, 0, sizeof(srv_pattern));
5066+
int ret = -1;
5067+
5068+
PSA_INIT();
5069+
5070+
srv_pattern.pattern = log_pattern;
5071+
options.srv_log_obj = &srv_pattern;
5072+
options.srv_log_fun = mbedtls_test_ssl_log_analyzer;
5073+
mbedtls_debug_set_threshold(3);
5074+
5075+
options.pk_alg = pk_alg;
5076+
5077+
ret = mbedtls_test_ssl_endpoint_init(&server, MBEDTLS_SSL_IS_SERVER,
5078+
&options, NULL, NULL, NULL);
5079+
TEST_EQUAL(ret, 0);
5080+
5081+
ret = mbedtls_test_ssl_endpoint_init(&client, MBEDTLS_SSL_IS_CLIENT,
5082+
&options, NULL, NULL, NULL);
5083+
TEST_EQUAL(ret, 0);
5084+
5085+
ret = mbedtls_test_mock_socket_connect(&server.socket, &client.socket,
5086+
BUFFSIZE);
5087+
TEST_EQUAL(ret, 0);
5088+
5089+
/* Make the server move to the required state */
5090+
ret = mbedtls_test_move_handshake_to_state(&client.ssl, &server.ssl, state);
5091+
TEST_EQUAL(ret, 0);
5092+
5093+
/* Send the crafted message */
5094+
ret = mbedtls_test_mock_tcp_send_b(&client.socket, data->x, data->len);
5095+
TEST_EQUAL(ret, (int) data->len);
5096+
5097+
/* Have the server process it.
5098+
* Need the loop because a server that support 1.3 and 1.2
5099+
* will process a 1.2 ClientHello in two steps.
5100+
*/
5101+
do {
5102+
ret = mbedtls_ssl_handshake_step(&server.ssl);
5103+
} while (ret == 0 && server.ssl.state == state);
5104+
TEST_EQUAL(ret, expected_ret);
5105+
TEST_ASSERT(srv_pattern.counter >= 1);
5106+
5107+
exit:
5108+
mbedtls_test_free_handshake_options(&options);
5109+
mbedtls_test_ssl_endpoint_free(&server, NULL);
5110+
mbedtls_test_ssl_endpoint_free(&client, NULL);
5111+
mbedtls_debug_set_threshold(0);
5112+
PSA_DONE();
5113+
}
5114+
/* END_CASE */
5115+
5116+
/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3:MBEDTLS_DEBUG_C:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED:MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED:MBEDTLS_MD_CAN_SHA256:MBEDTLS_ECP_HAVE_SECP256R1:MBEDTLS_ECP_HAVE_SECP384R1:MBEDTLS_PK_CAN_ECDSA_SIGN:MBEDTLS_PK_CAN_ECDSA_VERIFY */
5117+
void send_large_fragmented_hello(int hs_len_int, int first_frag_content_len_int,
5118+
char *log_pattern, int expected_ret)
5119+
{
5120+
/* This function sends a long message (claiming to be a ClientHello)
5121+
* fragmented in 1-byte fragments (except the initial fragment).
5122+
* The purpose is to test how the stack reacts when receiving:
5123+
* - a message larger than our buffer;
5124+
* - a message smaller than our buffer, but where the intermediate size of
5125+
* holding all the fragments (including overhead) is larger than our
5126+
* buffer.
5127+
*/
5128+
enum { BUFFSIZE = 16384 };
5129+
mbedtls_test_ssl_endpoint server, client;
5130+
mbedtls_platform_zeroize(&server, sizeof(server));
5131+
mbedtls_platform_zeroize(&client, sizeof(client));
5132+
5133+
mbedtls_test_handshake_test_options options;
5134+
mbedtls_test_init_handshake_options(&options);
5135+
5136+
mbedtls_test_ssl_log_pattern srv_pattern;
5137+
memset(&srv_pattern, 0, sizeof(srv_pattern));
5138+
5139+
unsigned char *first_frag = NULL;
5140+
int ret = -1;
5141+
5142+
size_t hs_len = (size_t) hs_len_int;
5143+
size_t first_frag_content_len = (size_t) first_frag_content_len_int;
5144+
5145+
PSA_INIT();
5146+
5147+
srv_pattern.pattern = log_pattern;
5148+
options.srv_log_obj = &srv_pattern;
5149+
options.srv_log_fun = mbedtls_test_ssl_log_analyzer;
5150+
mbedtls_debug_set_threshold(1);
5151+
5152+
// Does't really matter but we want to know to declare dependencies.
5153+
options.pk_alg = MBEDTLS_PK_ECDSA;
5154+
5155+
ret = mbedtls_test_ssl_endpoint_init(&server, MBEDTLS_SSL_IS_SERVER,
5156+
&options, NULL, NULL, NULL);
5157+
TEST_EQUAL(ret, 0);
5158+
5159+
ret = mbedtls_test_ssl_endpoint_init(&client, MBEDTLS_SSL_IS_CLIENT,
5160+
&options, NULL, NULL, NULL);
5161+
TEST_EQUAL(ret, 0);
5162+
5163+
ret = mbedtls_test_mock_socket_connect(&server.socket, &client.socket,
5164+
BUFFSIZE);
5165+
TEST_EQUAL(ret, 0);
5166+
5167+
/* Make the server move past the initial dummy state */
5168+
ret = mbedtls_test_move_handshake_to_state(&client.ssl, &server.ssl,
5169+
MBEDTLS_SSL_CLIENT_HELLO);
5170+
TEST_EQUAL(ret, 0);
5171+
5172+
/* Prepare initial fragment */
5173+
const size_t first_len = 5 // record header, see below
5174+
+ 4 // handshake header, see balow
5175+
+ first_frag_content_len;
5176+
TEST_CALLOC(first_frag, first_len);
5177+
unsigned char *p = first_frag;
5178+
// record header
5179+
// record type: handshake
5180+
*p++ = 0x16,
5181+
// record version (actually common to TLS 1.2 and TLS 1.3)
5182+
*p++ = 0x03,
5183+
*p++ = 0x03,
5184+
// record length: two bytes
5185+
*p++ = (unsigned char) (((4 + first_frag_content_len) >> 8) & 0xff);
5186+
*p++ = (unsigned char) (((4 + first_frag_content_len) >> 0) & 0xff);
5187+
// handshake header
5188+
// handshake type: ClientHello
5189+
*p++ = 0x01,
5190+
// handshake length: three bytes
5191+
*p++ = (unsigned char) ((hs_len >> 16) & 0xff);
5192+
*p++ = (unsigned char) ((hs_len >> 8) & 0xff);
5193+
*p++ = (unsigned char) ((hs_len >> 0) & 0xff);
5194+
// handshake content: dummy value
5195+
memset(p, 0x2a, first_frag_content_len);
5196+
5197+
/* Send initial fragment and have the server process it. */
5198+
ret = mbedtls_test_mock_tcp_send_b(&client.socket, first_frag, first_len);
5199+
TEST_ASSERT(ret >= 0 && (size_t) ret == first_len);
5200+
5201+
ret = mbedtls_ssl_handshake_step(&server.ssl);
5202+
TEST_EQUAL(ret, MBEDTLS_ERR_SSL_WANT_READ);
5203+
5204+
/* Dummy 1-byte fragment to repeatedly send next */
5205+
const unsigned char next[] = {
5206+
0x16, 0x03, 0x03, 0x00, 0x01, // record header (see above)
5207+
0x2a, // Dummy handshake message content
5208+
};
5209+
for (size_t left = hs_len - first_frag_content_len; left != 0; left--) {
5210+
ret = mbedtls_test_mock_tcp_send_b(&client.socket, next, sizeof(next));
5211+
TEST_ASSERT(ret >= 0 && (size_t) ret == sizeof(next));
5212+
5213+
ret = mbedtls_ssl_handshake_step(&server.ssl);
5214+
if (ret != MBEDTLS_ERR_SSL_WANT_READ) {
5215+
break;
5216+
}
5217+
}
5218+
TEST_EQUAL(ret, expected_ret);
5219+
TEST_EQUAL(srv_pattern.counter, 1);
5220+
5221+
exit:
5222+
mbedtls_test_free_handshake_options(&options);
5223+
mbedtls_test_ssl_endpoint_free(&server, NULL);
5224+
mbedtls_test_ssl_endpoint_free(&client, NULL);
5225+
mbedtls_debug_set_threshold(0);
5226+
mbedtls_free(first_frag);
5227+
PSA_DONE();
5228+
}
5229+
/* END_CASE */

0 commit comments

Comments
 (0)