Skip to content

Commit 81b92c4

Browse files
Split off mbedtls_test_ssl_endpoint_make_key_opaque
Split the opaque key handling out of `mbedtls_test_ssl_endpoint_certificate_init()` and into a separate function `mbedtls_test_ssl_endpoint_make_key_opaque()`. This will allow more flexibility in what `mbedtls_test_ssl_endpoint_certificate_init()` can do, since it no longer has to care about the opaque case. There is only one test function that calls ``mbedtls_test_ssl_endpoint_certificate_init()` to set up an opaque key. Make it call `mbedtls_test_ssl_endpoint_make_key_opaque()` (only on the server side, since the function doesn't do client authentication anyway). Thus there is no behavior change in the test suite. Signed-off-by: Gilles Peskine <[email protected]>
1 parent ceadecc commit 81b92c4

File tree

3 files changed

+92
-43
lines changed

3 files changed

+92
-43
lines changed

tests/include/test/ssl_helpers.h

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,6 @@ typedef struct mbedtls_test_handshake_test_options {
103103
int expected_handshake_result;
104104
int expected_ciphersuite;
105105
int pk_alg;
106-
int opaque_alg;
107-
int opaque_alg2;
108-
int opaque_usage;
109106
data_t *psk_str;
110107
int dtls;
111108
int srv_auth_mode;
@@ -442,9 +439,27 @@ int mbedtls_test_mock_tcp_recv_msg(void *ctx,
442439
* \retval 0 on success, otherwise error code.
443440
*/
444441
int mbedtls_test_ssl_endpoint_certificate_init(mbedtls_test_ssl_endpoint *ep,
445-
int pk_alg,
446-
int opaque_alg, int opaque_alg2,
447-
int opaque_usage);
442+
int pk_alg);
443+
444+
/** Make the endpoint's private key opaque.
445+
*
446+
* Call this function after mbedtls_test_ssl_endpoint_certificate_init()
447+
* (including indirect calls by mbedtls_test_ssl_endpoint_init()) to
448+
* replace the endpoint key by an opaque key with the same material.
449+
*
450+
* \param ep The endpoint to set up. It must already have a key
451+
* and certificate set.
452+
* \param opaque_alg The algorithm to declare in the PSA key policy.
453+
* \param opaque_alg2 The enrollment algorithm to declare in the PSA
454+
* key policy.
455+
* \param opaque_usage The usage flags to declare in the PSA key policy.
456+
*
457+
* \return 0 on success. On error, this function marks the test as failed
458+
* and returns a negative error code.
459+
*/
460+
int mbedtls_test_ssl_endpoint_make_key_opaque(mbedtls_test_ssl_endpoint *ep,
461+
int opaque_alg, int opaque_alg2,
462+
int opaque_usage);
448463

449464
/*
450465
* Initializes \p ep structure. It is important to call

tests/src/test_helpers/ssl_helpers.c

Lines changed: 48 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -716,16 +716,11 @@ static int load_endpoint_ecc(mbedtls_test_ssl_endpoint *ep)
716716
}
717717

718718
int mbedtls_test_ssl_endpoint_certificate_init(mbedtls_test_ssl_endpoint *ep,
719-
int pk_alg,
720-
int opaque_alg, int opaque_alg2,
721-
int opaque_usage)
719+
int pk_alg)
722720
{
723721
int i = 0;
724722
int ret = -1;
725723
int ok = 0;
726-
#if defined(MBEDTLS_USE_PSA_CRYPTO)
727-
mbedtls_svc_key_id_t key_slot = MBEDTLS_SVC_KEY_ID_INIT;
728-
#endif
729724

730725
if (ep == NULL) {
731726
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
@@ -757,29 +752,6 @@ int mbedtls_test_ssl_endpoint_certificate_init(mbedtls_test_ssl_endpoint *ep,
757752
TEST_EQUAL(load_endpoint_ecc(ep), 0);
758753
}
759754

760-
#if defined(MBEDTLS_USE_PSA_CRYPTO)
761-
if (opaque_alg != 0) {
762-
psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
763-
/* Use a fake key usage to get a successful initial guess for the PSA attributes. */
764-
TEST_EQUAL(mbedtls_pk_get_psa_attributes(ep->pkey, PSA_KEY_USAGE_SIGN_HASH,
765-
&key_attr), 0);
766-
/* Then manually usage, alg and alg2 as requested by the test. */
767-
psa_set_key_usage_flags(&key_attr, opaque_usage);
768-
psa_set_key_algorithm(&key_attr, opaque_alg);
769-
if (opaque_alg2 != PSA_ALG_NONE) {
770-
psa_set_key_enrollment_algorithm(&key_attr, opaque_alg2);
771-
}
772-
TEST_EQUAL(mbedtls_pk_import_into_psa(ep->pkey, &key_attr, &key_slot), 0);
773-
mbedtls_pk_free(ep->pkey);
774-
mbedtls_pk_init(ep->pkey);
775-
TEST_EQUAL(mbedtls_pk_setup_opaque(ep->pkey, key_slot), 0);
776-
}
777-
#else
778-
(void) opaque_alg;
779-
(void) opaque_alg2;
780-
(void) opaque_usage;
781-
#endif
782-
783755
mbedtls_ssl_conf_ca_chain(&(ep->conf), ep->ca_chain, NULL);
784756

785757
ret = mbedtls_ssl_conf_own_cert(&(ep->conf), ep->cert,
@@ -800,6 +772,52 @@ int mbedtls_test_ssl_endpoint_certificate_init(mbedtls_test_ssl_endpoint *ep,
800772
return ret;
801773
}
802774

775+
int mbedtls_test_ssl_endpoint_make_key_opaque(mbedtls_test_ssl_endpoint *ep,
776+
int opaque_alg, int opaque_alg2,
777+
int opaque_usage)
778+
{
779+
psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
780+
#if defined(MBEDTLS_USE_PSA_CRYPTO)
781+
mbedtls_svc_key_id_t key_slot = MBEDTLS_SVC_KEY_ID_INIT;
782+
#endif
783+
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
784+
int ok = 0;
785+
786+
/* Use a fake key usage to get a successful initial guess for the PSA attributes. */
787+
TEST_EQUAL(mbedtls_pk_get_psa_attributes(ep->pkey, PSA_KEY_USAGE_SIGN_HASH,
788+
&key_attr), 0);
789+
/* Then manually usage, alg and alg2 as requested by the test. */
790+
psa_set_key_usage_flags(&key_attr, opaque_usage);
791+
psa_set_key_algorithm(&key_attr, opaque_alg);
792+
if (opaque_alg2 != PSA_ALG_NONE) {
793+
psa_set_key_enrollment_algorithm(&key_attr, opaque_alg2);
794+
}
795+
TEST_EQUAL(mbedtls_pk_import_into_psa(ep->pkey, &key_attr, &key_slot), 0);
796+
mbedtls_pk_free(ep->pkey);
797+
mbedtls_pk_init(ep->pkey);
798+
TEST_EQUAL(mbedtls_pk_setup_opaque(ep->pkey, key_slot), 0);
799+
800+
/* Reset (key, certificate) pair(s) in the SSL configuration, so that
801+
* the configuration will only contain what we put explicitly in
802+
* this function. */
803+
ret = mbedtls_ssl_conf_own_cert(&(ep->conf), NULL, NULL);
804+
TEST_EQUAL(ret, 0);
805+
806+
/* Only put the opaque key, with the same certificate as before. */
807+
ret = mbedtls_ssl_conf_own_cert(&(ep->conf), ep->cert, ep->pkey);
808+
TEST_EQUAL(ret, 0);
809+
810+
ok = 1;
811+
812+
exit:
813+
if (ret == 0 && !ok) {
814+
/* Exiting due to a test assertion that isn't ret == 0 */
815+
ret = -1;
816+
}
817+
818+
return ret;
819+
}
820+
803821
int mbedtls_test_ssl_endpoint_init(
804822
mbedtls_test_ssl_endpoint *ep, int endpoint_type,
805823
const mbedtls_test_handshake_test_options *options)
@@ -970,10 +988,7 @@ int mbedtls_test_ssl_endpoint_init(
970988
#endif
971989
#endif /* MBEDTLS_DEBUG_C */
972990

973-
ret = mbedtls_test_ssl_endpoint_certificate_init(ep, options->pk_alg,
974-
options->opaque_alg,
975-
options->opaque_alg2,
976-
options->opaque_usage);
991+
ret = mbedtls_test_ssl_endpoint_certificate_init(ep, options->pk_alg);
977992
TEST_EQUAL(ret, 0);
978993

979994
#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)

tests/suites/test_suite_ssl.function

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3033,13 +3033,14 @@ void handshake_ciphersuite_select(char *cipher, int pk_alg, data_t *psk_str,
30333033
{
30343034
mbedtls_test_handshake_test_options options;
30353035
mbedtls_test_init_handshake_options(&options);
3036+
mbedtls_test_ssl_endpoint client;
3037+
memset(&client, 0, sizeof(client));
3038+
mbedtls_test_ssl_endpoint server;
3039+
memset(&server, 0, sizeof(server));
30363040

30373041
options.cipher = cipher;
30383042
options.psk_str = psk_str;
30393043
options.pk_alg = pk_alg;
3040-
options.opaque_alg = psa_alg;
3041-
options.opaque_alg2 = psa_alg2;
3042-
options.opaque_usage = psa_usage;
30433044
options.srv_auth_mode = MBEDTLS_SSL_VERIFY_NONE;
30443045
options.expected_handshake_result = expected_handshake_result;
30453046
options.expected_ciphersuite = expected_ciphersuite;
@@ -3048,13 +3049,31 @@ void handshake_ciphersuite_select(char *cipher, int pk_alg, data_t *psk_str,
30483049
options.server_max_version = MBEDTLS_SSL_VERSION_TLS1_2;
30493050
options.expected_negotiated_version = MBEDTLS_SSL_VERSION_TLS1_2;
30503051

3051-
mbedtls_test_ssl_perform_handshake(&options);
3052+
MD_OR_USE_PSA_INIT();
3053+
3054+
TEST_EQUAL(mbedtls_test_ssl_endpoint_init(&client,
3055+
MBEDTLS_SSL_IS_CLIENT,
3056+
&options), 0);
3057+
3058+
TEST_EQUAL(mbedtls_test_ssl_endpoint_init(&server,
3059+
MBEDTLS_SSL_IS_SERVER,
3060+
&options), 0);
3061+
if (psa_alg != 0) {
3062+
TEST_EQUAL(mbedtls_test_ssl_endpoint_make_key_opaque(&server,
3063+
psa_alg, psa_alg2,
3064+
psa_usage), 0);
3065+
}
3066+
3067+
TEST_ASSERT(mbedtls_test_ssl_perform_connection(&options, &client, &server));
30523068

30533069
/* The goto below is used to avoid an "unused label" warning.*/
30543070
goto exit;
30553071

30563072
exit:
3073+
mbedtls_test_ssl_endpoint_free(&client);
3074+
mbedtls_test_ssl_endpoint_free(&server);
30573075
mbedtls_test_free_handshake_options(&options);
3076+
MD_OR_USE_PSA_DONE();
30583077
}
30593078
/* END_CASE */
30603079

0 commit comments

Comments
 (0)