Skip to content

Test mbedtls_ssl_conf_own_cert #10217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 32 commits into
base: development
Choose a base branch
from
Draft
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
353eb33
Use TEST_EQUAL(a,b) instead of TEST_ASSERT(a==b)
gilles-peskine-arm May 14, 2025
b6bb3fb
Flatten out mbedtls_test_ssl_endpoint_certificate structure
gilles-peskine-arm May 26, 2025
35a2d9b
Remove testing of mbedtls_ssl_conf_own_cert(NULL)
gilles-peskine-arm May 26, 2025
0677e02
Move timer into the endpoint structure
gilles-peskine-arm May 27, 2025
2744a43
Refactor set_ciphersuites to work on the endpoint structure
gilles-peskine-arm May 27, 2025
c4949d1
mbedtls_ssl_conf_alpn_protocols: declare list elements as const
gilles-peskine-arm May 27, 2025
9b99368
mbedtls_test_ssl_perform_handshake: declare options as const
gilles-peskine-arm May 27, 2025
2996959
Move DTLS context into the endpoint structure
gilles-peskine-arm May 27, 2025
b092e78
New auxiliary function mbedtls_test_ssl_dtls_join_endpoints
gilles-peskine-arm May 27, 2025
6c154e7
Move queue management into mbedtls_test_ssl_dtls_join_endpoints
gilles-peskine-arm May 27, 2025
ca8a9ac
Remove unused parameters to endpoint init/free
gilles-peskine-arm May 27, 2025
07432b9
Unify identical code
gilles-peskine-arm May 27, 2025
e30b5c7
mbedtls_test_ssl_perform_handshake: make client, server pointers
gilles-peskine-arm May 27, 2025
78df6ae
Move renegotiation testing into its own function
gilles-peskine-arm May 27, 2025
e23a6d1
Move serialization testing into its own function
gilles-peskine-arm May 27, 2025
bd95340
Unify SSL version checks between client and server
gilles-peskine-arm May 28, 2025
7a8fd46
Separate test function to perform an SSL connection
gilles-peskine-arm May 28, 2025
27586d8
Move more endpoint configuration into the setup function
gilles-peskine-arm May 28, 2025
fb2ce05
SSL tests: make client authentication more uniform, defaulting on
gilles-peskine-arm May 28, 2025
6e4d245
Move certificate and key parsing to auxiliary functions
gilles-peskine-arm May 27, 2025
a6e71f9
Don't change the configuration after mbedtls_ssl_setup
gilles-peskine-arm Jun 1, 2025
00eb072
mbedtls_test_ssl_endpoint_init: store user_data_n in the endpoint object
gilles-peskine-arm Jun 1, 2025
6edb76c
mbedtls_test_ssl_endpoint_init: split configuration and setup
gilles-peskine-arm Jun 1, 2025
42e8d42
Expand handshake_ciphersuite_select
gilles-peskine-arm Jun 2, 2025
e9c6c85
Simplify ownership of opaque key in SSL test endpoint
gilles-peskine-arm Jun 2, 2025
f697697
Break out key and certificate loading into separate functions
gilles-peskine-arm Jun 2, 2025
972f726
Allow endpoint init to skip loading a key and certificate
gilles-peskine-arm Jun 2, 2025
3c3001e
Automate debug logs in SSL tests more
gilles-peskine-arm Jun 11, 2025
fabb20e
Show debug logs in SSL tests based on a variable
gilles-peskine-arm Jun 11, 2025
df8d383
SSL test debug logs: show endpoint name
gilles-peskine-arm Jun 11, 2025
fb7cb97
Server-side tests for mbedtls_ssl_conf_own_cert
gilles-peskine-arm Jun 12, 2025
9fff313
Comment out known broken test cases
gilles-peskine-arm Jun 13, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Move more endpoint configuration into the setup function
Applying SSL configuration settings recorded in the
`mbedtls_test_handshake_test_options` structure to an
`mbedtls_test_ssl_endpoint` object was split between
`mbedtls_test_ssl_endpoint_init()` and `mbedtls_test_ssl_perform_handshake()`.
This was surprising, and made it harder to use
`mbedtls_test_ssl_endpoint_init()` for custom behavior. It also meant some
code duplication in `mbedtls_test_ssl_perform_handshake()`.

Move most configuration setup from `mbedtls_test_ssl_perform_handshake()` to
`mbedtls_test_ssl_endpoint_init()`. This changes the behavior in two ways:

* `mbedtls_test_ssl_endpoint_init()` now takes some options into account
  that it previously ignored. This is ok because we don't set these options
  in any of the existing tests.
* When calling `mbedtls_test_ssl_perform_handshake()`, some SSL configuration
  settings are now set (calls to `mbedtls_ssl_conf_xxx()`) before the call
  to `mbedtls_ssl_setup()` instead of after. This should be ok since it is
  forbidden to change the configuration after `mbedtls_ssl_setup()`,
  although the previous test code was getting away with it. This commit does
  not move all configuration before `mbedtls_ssl_setup()`, that would be out
  of scope of the current series of patches.

Thus there are some internal behavior changes, but they should not affect
any relevant aspect of the tests' behavior.

Signed-off-by: Gilles Peskine <[email protected]>
  • Loading branch information
gilles-peskine-arm committed May 28, 2025
commit 27586d83f016f539dcc27faaae125943533c16af
192 changes: 92 additions & 100 deletions tests/src/test_helpers/ssl_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -572,8 +572,60 @@ int mbedtls_test_mock_tcp_recv_msg(void *ctx,
return (msg_len > INT_MAX) ? INT_MAX : (int) msg_len;
}


#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) && \
defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) && \
defined(MBEDTLS_SSL_SRV_C)
static int psk_dummy_callback(void *p_info, mbedtls_ssl_context *ssl,
const unsigned char *name, size_t name_len)
{
(void) p_info;
(void) ssl;
(void) name;
(void) name_len;

return 0;
}
#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED &&
MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED &&
MBEDTLS_SSL_SRV_C */

#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)

static int set_ciphersuite(mbedtls_test_ssl_endpoint *ep,
const char *cipher)
{
if (cipher == NULL || cipher[0] == 0) {
return 1;
}

int ok = 0;

TEST_CALLOC(ep->ciphersuites, 2);
ep->ciphersuites[0] = mbedtls_ssl_get_ciphersuite_id(cipher);
ep->ciphersuites[1] = 0;

const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
mbedtls_ssl_ciphersuite_from_id(ep->ciphersuites[0]);

TEST_ASSERT(ciphersuite_info != NULL);
TEST_ASSERT(ciphersuite_info->min_tls_version <= ep->conf.max_tls_version);
TEST_ASSERT(ciphersuite_info->max_tls_version >= ep->conf.min_tls_version);

if (ep->conf.max_tls_version > ciphersuite_info->max_tls_version) {
ep->conf.max_tls_version = (mbedtls_ssl_protocol_version) ciphersuite_info->max_tls_version;
}
if (ep->conf.min_tls_version < ciphersuite_info->min_tls_version) {
ep->conf.min_tls_version = (mbedtls_ssl_protocol_version) ciphersuite_info->min_tls_version;
}

mbedtls_ssl_conf_ciphersuites(&ep->conf, ep->ciphersuites);
ok = 1;

exit:
return ok;
}

/*
* Deinitializes certificates from endpoint represented by \p ep.
*/
Expand Down Expand Up @@ -740,6 +792,9 @@ int mbedtls_test_ssl_endpoint_init(
{
int ret = -1;
uintptr_t user_data_n;
#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
const char *psk_identity = "foo";
#endif

if (ep == NULL) {
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Expand Down Expand Up @@ -813,6 +868,10 @@ int mbedtls_test_ssl_endpoint_init(
}
}

if (MBEDTLS_SSL_IS_CLIENT == endpoint_type) {
TEST_ASSERT(set_ciphersuite(ep, options->cipher));
}

if (options->group_list != NULL) {
mbedtls_ssl_conf_groups(&(ep->conf), options->group_list);
}
Expand All @@ -828,6 +887,7 @@ int mbedtls_test_ssl_endpoint_init(
options->max_early_data_size);
}
#endif

#if defined(MBEDTLS_SSL_ALPN)
/* check that alpn_list contains at least one valid entry */
if (options->alpn_list[0] != NULL) {
Expand All @@ -836,6 +896,15 @@ int mbedtls_test_ssl_endpoint_init(
#endif
#endif

#if defined(MBEDTLS_SSL_RENEGOTIATION)
if (options->renegotiate) {
mbedtls_ssl_conf_renegotiation(&ep->conf,
MBEDTLS_SSL_RENEGOTIATION_ENABLED);
mbedtls_ssl_conf_legacy_renegotiation(&ep->conf,
options->legacy_renegotiation);
}
#endif /* MBEDTLS_SSL_RENEGOTIATION */

#if defined(MBEDTLS_SSL_CACHE_C) && defined(MBEDTLS_SSL_SRV_C)
if (endpoint_type == MBEDTLS_SSL_IS_SERVER && options->cache != NULL) {
mbedtls_ssl_conf_session_cache(&(ep->conf), options->cache,
Expand All @@ -844,6 +913,14 @@ int mbedtls_test_ssl_endpoint_init(
}
#endif

#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
TEST_EQUAL(mbedtls_ssl_conf_max_frag_len(&ep->conf,
(unsigned char) options->mfl),
0);
#else
TEST_EQUAL(MBEDTLS_SSL_MAX_FRAG_LEN_NONE, options->mfl);
#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */

ret = mbedtls_ssl_setup(&(ep->ssl), &(ep->conf));
TEST_EQUAL(ret, 0);

Expand Down Expand Up @@ -881,6 +958,21 @@ int mbedtls_test_ssl_endpoint_init(
options->opaque_usage);
TEST_EQUAL(ret, 0);

#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
if (options->psk_str != NULL && options->psk_str->len > 0) {
TEST_EQUAL(mbedtls_ssl_conf_psk(
&ep->conf, options->psk_str->x,
options->psk_str->len,
(const unsigned char *) psk_identity,
strlen(psk_identity)), 0);
#if defined(MBEDTLS_SSL_SRV_C)
if (MBEDTLS_SSL_IS_SERVER == endpoint_type) {
mbedtls_ssl_conf_psk_cb(&ep->conf, psk_dummy_callback, NULL);
}
#endif
}
#endif

TEST_EQUAL(mbedtls_ssl_conf_get_user_data_n(&ep->conf), user_data_n);
mbedtls_ssl_conf_set_user_data_p(&ep->conf, ep);
TEST_EQUAL(mbedtls_ssl_get_user_data_n(&ep->ssl), user_data_n);
Expand Down Expand Up @@ -1060,59 +1152,6 @@ static int mbedtls_ssl_read_fragment(mbedtls_ssl_context *ssl,
return -1;
}

#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
static int set_ciphersuite(mbedtls_test_ssl_endpoint *ep,
const char *cipher)
{
if (cipher == NULL || cipher[0] == 0) {
return 1;
}

int ok = 0;

TEST_CALLOC(ep->ciphersuites, 2);
ep->ciphersuites[0] = mbedtls_ssl_get_ciphersuite_id(cipher);
ep->ciphersuites[1] = 0;

const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
mbedtls_ssl_ciphersuite_from_id(ep->ciphersuites[0]);

TEST_ASSERT(ciphersuite_info != NULL);
TEST_ASSERT(ciphersuite_info->min_tls_version <= ep->conf.max_tls_version);
TEST_ASSERT(ciphersuite_info->max_tls_version >= ep->conf.min_tls_version);

if (ep->conf.max_tls_version > ciphersuite_info->max_tls_version) {
ep->conf.max_tls_version = (mbedtls_ssl_protocol_version) ciphersuite_info->max_tls_version;
}
if (ep->conf.min_tls_version < ciphersuite_info->min_tls_version) {
ep->conf.min_tls_version = (mbedtls_ssl_protocol_version) ciphersuite_info->min_tls_version;
}

mbedtls_ssl_conf_ciphersuites(&ep->conf, ep->ciphersuites);
ok = 1;

exit:
return ok;
}
#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */

#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) && \
defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) && \
defined(MBEDTLS_SSL_SRV_C)
static int psk_dummy_callback(void *p_info, mbedtls_ssl_context *ssl,
const unsigned char *name, size_t name_len)
{
(void) p_info;
(void) ssl;
(void) name;
(void) name_len;

return 0;
}
#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED &&
MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED &&
MBEDTLS_SSL_SRV_C */

#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
defined(PSA_WANT_ALG_CBC_NO_PADDING) && defined(PSA_WANT_KEY_TYPE_AES)
int mbedtls_test_psa_cipher_encrypt_helper(mbedtls_ssl_transform *transform,
Expand Down Expand Up @@ -2383,9 +2422,6 @@ void mbedtls_test_ssl_perform_handshake(
mbedtls_test_ssl_endpoint server_struct;
memset(&server_struct, 0, sizeof(server_struct));
mbedtls_test_ssl_endpoint *const server = &server_struct;
#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
const char *psk_identity = "foo";
#endif

MD_OR_USE_PSA_INIT();

Expand All @@ -2399,7 +2435,6 @@ void mbedtls_test_ssl_perform_handshake(
TEST_EQUAL(mbedtls_test_ssl_endpoint_init(client,
MBEDTLS_SSL_IS_CLIENT,
options), 0);
TEST_ASSERT(set_ciphersuite(client, options->cipher));

/* Server side */
TEST_EQUAL(mbedtls_test_ssl_endpoint_init(server,
Expand All @@ -2411,49 +2446,6 @@ void mbedtls_test_ssl_perform_handshake(
TEST_EQUAL(mbedtls_test_ssl_dtls_join_endpoints(client, server), 0);
}

#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
TEST_EQUAL(mbedtls_ssl_conf_max_frag_len(&(server->conf),
(unsigned char) options->mfl),
0);
TEST_EQUAL(mbedtls_ssl_conf_max_frag_len(&(client->conf),
(unsigned char) options->mfl),
0);
#else
TEST_EQUAL(MBEDTLS_SSL_MAX_FRAG_LEN_NONE, options->mfl);
#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */

#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
if (options->psk_str != NULL && options->psk_str->len > 0) {
TEST_EQUAL(mbedtls_ssl_conf_psk(
&client->conf, options->psk_str->x,
options->psk_str->len,
(const unsigned char *) psk_identity,
strlen(psk_identity)), 0);

TEST_EQUAL(mbedtls_ssl_conf_psk(
&server->conf, options->psk_str->x,
options->psk_str->len,
(const unsigned char *) psk_identity,
strlen(psk_identity)), 0);
#if defined(MBEDTLS_SSL_SRV_C)
mbedtls_ssl_conf_psk_cb(&server->conf, psk_dummy_callback, NULL);
#endif
}
#endif
#if defined(MBEDTLS_SSL_RENEGOTIATION)
if (options->renegotiate) {
mbedtls_ssl_conf_renegotiation(&(server->conf),
MBEDTLS_SSL_RENEGOTIATION_ENABLED);
mbedtls_ssl_conf_renegotiation(&(client->conf),
MBEDTLS_SSL_RENEGOTIATION_ENABLED);

mbedtls_ssl_conf_legacy_renegotiation(&(server->conf),
options->legacy_renegotiation);
mbedtls_ssl_conf_legacy_renegotiation(&(client->conf),
options->legacy_renegotiation);
}
#endif /* MBEDTLS_SSL_RENEGOTIATION */

TEST_ASSERT(mbedtls_test_ssl_perform_connection(options, client, server));

TEST_ASSERT(mbedtls_ssl_conf_get_user_data_p(&client->conf) == client);
Expand Down