@@ -4471,3 +4471,153 @@ exit:
44714471 PSA_DONE();
44724472}
44734473/* END_CASE */
4474+
4475+ /* BEGIN_CASE depends_on:MBEDTLS_SSL_EARLY_DATA:MBEDTLS_SSL_CLI_C:MBEDTLS_SSL_SRV_C:MBEDTLS_DEBUG_C:MBEDTLS_TEST_AT_LEAST_ONE_TLS1_3_CIPHERSUITE:MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED:MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED:MBEDTLS_MD_CAN_SHA256:MBEDTLS_ECP_HAVE_SECP256R1:MBEDTLS_ECP_HAVE_SECP384R1:MBEDTLS_PK_CAN_ECDSA_VERIFY:MBEDTLS_SSL_SESSION_TICKETS */
4476+ void tls13_cli_max_early_data_size(int max_early_data_size_arg)
4477+ {
4478+ int ret = -1;
4479+ mbedtls_test_ssl_endpoint client_ep, server_ep;
4480+ mbedtls_test_handshake_test_options client_options;
4481+ mbedtls_test_handshake_test_options server_options;
4482+ mbedtls_ssl_session saved_session;
4483+ unsigned char buf[64];
4484+ uint32_t max_early_data_size;
4485+ uint32_t written_early_data_size = 0;
4486+ uint32_t read_early_data_size = 0;
4487+
4488+ mbedtls_platform_zeroize(&client_ep, sizeof(client_ep));
4489+ mbedtls_platform_zeroize(&server_ep, sizeof(server_ep));
4490+ mbedtls_test_init_handshake_options(&client_options);
4491+ mbedtls_test_init_handshake_options(&server_options);
4492+ mbedtls_ssl_session_init(&saved_session);
4493+
4494+ PSA_INIT();
4495+
4496+ /*
4497+ * Run first handshake to get a ticket from the server.
4498+ */
4499+
4500+ client_options.pk_alg = MBEDTLS_PK_ECDSA;
4501+ client_options.early_data = MBEDTLS_SSL_EARLY_DATA_ENABLED;
4502+ server_options.pk_alg = MBEDTLS_PK_ECDSA;
4503+ server_options.early_data = MBEDTLS_SSL_EARLY_DATA_ENABLED;
4504+ server_options.max_early_data_size = max_early_data_size_arg;
4505+
4506+ ret = mbedtls_test_get_tls13_ticket(&client_options, &server_options,
4507+ &saved_session);
4508+ TEST_EQUAL(ret, 0);
4509+
4510+ /*
4511+ * Prepare for handshake with the ticket.
4512+ */
4513+ ret = mbedtls_test_ssl_endpoint_init(&client_ep, MBEDTLS_SSL_IS_CLIENT,
4514+ &client_options, NULL, NULL, NULL);
4515+ TEST_EQUAL(ret, 0);
4516+
4517+ ret = mbedtls_test_ssl_endpoint_init(&server_ep, MBEDTLS_SSL_IS_SERVER,
4518+ &server_options, NULL, NULL, NULL);
4519+ TEST_EQUAL(ret, 0);
4520+
4521+ mbedtls_ssl_conf_session_tickets_cb(&server_ep.conf,
4522+ mbedtls_test_ticket_write,
4523+ mbedtls_test_ticket_parse,
4524+ NULL);
4525+
4526+ max_early_data_size = saved_session.max_early_data_size;
4527+ /*
4528+ * (max_early_data_size + 1024) for the size of the socket buffers for the
4529+ * server one to be able to contain the maximum number of early data bytes
4530+ * plus the first flight client messages. Needed because we cannot initiate
4531+ * the handshake on server side before doing all the calls to
4532+ * mbedtls_ssl_write_early_data() we want to test. See below for more
4533+ * information.
4534+ */
4535+ ret = mbedtls_test_mock_socket_connect(&(client_ep.socket),
4536+ &(server_ep.socket),
4537+ max_early_data_size + 1024);
4538+ TEST_EQUAL(ret, 0);
4539+
4540+ /* If our server is configured with max_early_data_size equal to zero, it
4541+ * does not set the MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_EARLY_DATA flag for
4542+ * the tickets it creates. To be able to test early data with a ticket
4543+ * allowing early data in its flags but with max_early_data_size equal to
4544+ * zero (case supported by our client) tweak the ticket flags here.
4545+ */
4546+ if (max_early_data_size == 0) {
4547+ saved_session.ticket_flags |= MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_EARLY_DATA;
4548+ }
4549+
4550+ ret = mbedtls_ssl_set_session(&(client_ep.ssl), &saved_session);
4551+ TEST_EQUAL(ret, 0);
4552+
4553+ while (written_early_data_size < max_early_data_size) {
4554+ size_t early_data_len = sizeof(buf);
4555+ uint32_t remaining = max_early_data_size - written_early_data_size;
4556+
4557+ for (size_t i = 0; i < early_data_len; i++) {
4558+ buf[i] = (unsigned char) (written_early_data_size + i);
4559+ }
4560+
4561+ ret = mbedtls_ssl_write_early_data(&(client_ep.ssl),
4562+ buf,
4563+ early_data_len);
4564+
4565+ if (early_data_len <= remaining) {
4566+ TEST_EQUAL(ret, early_data_len);
4567+ } else {
4568+ TEST_EQUAL(ret, remaining);
4569+ }
4570+ written_early_data_size += early_data_len;
4571+ }
4572+
4573+ /* In case we reached exactly the limit in the loop above, do another one
4574+ * byte early data write.
4575+ */
4576+ ret = mbedtls_ssl_write_early_data(&(client_ep.ssl), buf, 1);
4577+ TEST_EQUAL(ret, MBEDTLS_ERR_SSL_CANNOT_WRITE_EARLY_DATA);
4578+ TEST_EQUAL(client_ep.ssl.early_data_count, max_early_data_size);
4579+ TEST_EQUAL(client_ep.ssl.early_data_status,
4580+ MBEDTLS_SSL_EARLY_DATA_STATUS_CAN_WRITE);
4581+
4582+ /*
4583+ * Now, check data on server side. It is not done in the previous loop as
4584+ * in the first call to mbedtls_ssl_handshake(), the server ends up sending
4585+ * its Finished message and then in the following call to
4586+ * mbedtls_ssl_write_early_data() we go past the early data writing window
4587+ * and we cannot test multiple calls to the API is this writing window.
4588+ */
4589+ while (read_early_data_size < max_early_data_size) {
4590+ ret = mbedtls_ssl_handshake(&(server_ep.ssl));
4591+ TEST_EQUAL(ret, MBEDTLS_ERR_SSL_RECEIVED_EARLY_DATA);
4592+
4593+ ret = mbedtls_ssl_read_early_data(&(server_ep.ssl),
4594+ buf,
4595+ sizeof(buf));
4596+ TEST_ASSERT(ret > 0);
4597+
4598+ for (size_t i = 0; i < (size_t) ret; i++) {
4599+ TEST_EQUAL(buf[i], (unsigned char) (read_early_data_size + i));
4600+ }
4601+
4602+ read_early_data_size += ret;
4603+ }
4604+ TEST_EQUAL(read_early_data_size, max_early_data_size);
4605+
4606+ ret = mbedtls_ssl_handshake(&(server_ep.ssl));
4607+ TEST_EQUAL(ret, MBEDTLS_ERR_SSL_WANT_READ);
4608+
4609+ ret = mbedtls_ssl_write_early_data(&(client_ep.ssl), buf, 1);
4610+ TEST_EQUAL(ret, MBEDTLS_ERR_SSL_CANNOT_WRITE_EARLY_DATA);
4611+ TEST_EQUAL(client_ep.ssl.early_data_count, max_early_data_size);
4612+ TEST_EQUAL(client_ep.ssl.early_data_status,
4613+ MBEDTLS_SSL_EARLY_DATA_STATUS_CAN_WRITE);
4614+
4615+ exit:
4616+ mbedtls_test_ssl_endpoint_free(&client_ep, NULL);
4617+ mbedtls_test_ssl_endpoint_free(&server_ep, NULL);
4618+ mbedtls_test_free_handshake_options(&client_options);
4619+ mbedtls_test_free_handshake_options(&server_options);
4620+ mbedtls_ssl_session_free(&saved_session);
4621+ PSA_DONE();
4622+ }
4623+ /* END_CASE */
0 commit comments