1
0
mirror of https://github.com/Mbed-TLS/mbedtls.git synced 2025-07-29 11:41:15 +03:00

Merge pull request #10197 from gilles-peskine-arm/ssl_helpers-split_perform_handshake-dev

Break down mbedtls_test_ssl_perform_handshake
This commit is contained in:
Manuel Pégourié-Gonnard
2025-06-30 09:39:29 +00:00
committed by GitHub
9 changed files with 1461 additions and 1321 deletions

View File

@ -0,0 +1,4 @@
API changes
* The list passed to mbedtls_ssl_conf_alpn_protocols() is now declared
as having const elements, reflecting the fact that the library will
not modify it

View File

@ -1559,7 +1559,7 @@ struct mbedtls_ssl_config {
#endif /* MBEDTLS_SSL_EARLY_DATA */
#if defined(MBEDTLS_SSL_ALPN)
const char **MBEDTLS_PRIVATE(alpn_list); /*!< ordered list of protocols */
const char *const *MBEDTLS_PRIVATE(alpn_list); /*!< ordered list of protocols */
#endif
#if defined(MBEDTLS_SSL_DTLS_SRTP)
@ -4001,7 +4001,8 @@ int mbedtls_ssl_set_hs_ecjpake_password_opaque(mbedtls_ssl_context *ssl,
*
* \return 0 on success, or MBEDTLS_ERR_SSL_BAD_INPUT_DATA.
*/
int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config *conf, const char **protos);
int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config *conf,
const char *const *protos);
/**
* \brief Get the name of the negotiated Application Layer Protocol.

View File

@ -141,7 +141,7 @@ static int ssl_write_alpn_ext(mbedtls_ssl_context *ssl,
* ProtocolName protocol_name_list<2..2^16-1>
* } ProtocolNameList;
*/
for (const char **cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
for (const char *const *cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
/*
* mbedtls_ssl_conf_set_alpn_protocols() checked that the length of
* protocol names is less than 255.

View File

@ -2534,10 +2534,11 @@ void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf,
#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
#if defined(MBEDTLS_SSL_ALPN)
int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config *conf, const char **protos)
int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config *conf,
const char *const *protos)
{
size_t cur_len, tot_len;
const char **p;
const char *const *p;
/*
* RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
@ -5111,7 +5112,7 @@ static int ssl_context_load(mbedtls_ssl_context *ssl,
#if defined(MBEDTLS_SSL_ALPN)
{
uint8_t alpn_len;
const char **cur;
const char *const *cur;
if ((size_t) (end - p) < 1) {
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
@ -8547,7 +8548,7 @@ int mbedtls_ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
}
/* Use our order of preference */
for (const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) {
for (const char *const *alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) {
size_t const alpn_len = strlen(*alpn);
p = protocol_name_list;
while (p < protocol_name_list_end) {

View File

@ -869,7 +869,7 @@ static int ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
const unsigned char *buf, size_t len)
{
size_t list_len, name_len;
const char **p;
const char *const *p;
/* If we didn't send it, the server shouldn't send it */
if (ssl->conf->alpn_list == NULL) {

View File

@ -158,7 +158,7 @@ static int ssl_tls13_parse_alpn_ext(mbedtls_ssl_context *ssl,
/* Check that the server chosen protocol was in our list and save it */
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end, protocol_name_len);
for (const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) {
for (const char *const *alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) {
if (protocol_name_len == strlen(*alpn) &&
memcmp(p, *alpn, protocol_name_len) == 0) {
ssl->alpn_chosen = *alpn;

View File

@ -186,15 +186,6 @@ typedef struct mbedtls_test_message_socket_context {
#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
/*
* Structure with endpoint's certificates for SSL communication tests.
*/
typedef struct mbedtls_test_ssl_endpoint_certificate {
mbedtls_x509_crt *ca_cert;
mbedtls_x509_crt *cert;
mbedtls_pk_context *pkey;
} mbedtls_test_ssl_endpoint_certificate;
/*
* Endpoint structure for SSL communication tests.
*/
@ -203,7 +194,22 @@ typedef struct mbedtls_test_ssl_endpoint {
mbedtls_ssl_context ssl;
mbedtls_ssl_config conf;
mbedtls_test_mock_socket socket;
mbedtls_test_ssl_endpoint_certificate cert;
uintptr_t user_data_cookie; /* A unique value associated with this endpoint */
/* Objects only used by DTLS.
* They should be guarded by MBEDTLS_SSL_PROTO_DTLS, but
* currently aren't because some code accesses them without guards. */
mbedtls_test_message_socket_context dtls_context;
#if defined(MBEDTLS_TIMING_C)
mbedtls_timing_delay_context timer;
#endif
/* Objects owned by the endpoint */
int *ciphersuites;
mbedtls_test_ssl_message_queue queue_input;
mbedtls_x509_crt *ca_chain;
mbedtls_x509_crt *cert;
mbedtls_pk_context *pkey;
} mbedtls_test_ssl_endpoint;
#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
@ -432,8 +438,7 @@ int mbedtls_test_mock_tcp_recv_msg(void *ctx,
#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
/*
* Initializes \p ep_cert structure and assigns it to endpoint
* represented by \p ep.
* Load default CA certificates and endpoint keys into \p ep.
*
* \retval 0 on success, otherwise error code.
*/
@ -442,34 +447,85 @@ int mbedtls_test_ssl_endpoint_certificate_init(mbedtls_test_ssl_endpoint *ep,
int opaque_alg, int opaque_alg2,
int opaque_usage);
/*
* Initializes \p ep structure. It is important to call
* `mbedtls_test_ssl_endpoint_free()` after calling this function
* even if it fails.
/** Initialize the configuration in an SSL endpoint structure.
*
* \p endpoint_type must be set as MBEDTLS_SSL_IS_SERVER or
* MBEDTLS_SSL_IS_CLIENT.
* \p pk_alg the algorithm to use, currently only MBEDTLS_PK_RSA and
* MBEDTLS_PK_ECDSA are supported.
* \p dtls_context - in case of DTLS - this is the context handling metadata.
* \p input_queue - used only in case of DTLS.
* \p output_queue - used only in case of DTLS.
* \note You must call `mbedtls_test_ssl_endpoint_free()` after
* calling this function, even if it fails. This is necessary to
* free data that may have been stored in the endpoint structure.
*
* \param[out] ep The endpoint structure to configure.
* \param endpoint_type #MBEDTLS_SSL_IS_SERVER or #MBEDTLS_SSL_IS_CLIENT.
* \param[in] options The options to use for configuring the endpoint
* structure.
*
* \retval 0 on success, otherwise error code.
*/
int mbedtls_test_ssl_endpoint_init_conf(
mbedtls_test_ssl_endpoint *ep, int endpoint_type,
const mbedtls_test_handshake_test_options *options);
/** Initialize the session context in an endpoint structure.
*
* \note The endpoint structure must have been set up with
* mbedtls_test_ssl_endpoint_init_conf() with the same \p options.
* Between calling mbedtls_test_ssl_endpoint_init_conf() and
* mbedtls_test_ssl_endpoint_init_ssl(), you may configure `ep->ssl`
* further if you know what you're doing.
*
* \note You must call `mbedtls_test_ssl_endpoint_free()` after
* calling this function, even if it fails. This is necessary to
* free data that may have been stored in the endpoint structure.
*
* \param[out] ep The endpoint structure to set up.
* \param[in] options The options used for configuring the endpoint
* structure.
*
* \retval 0 on success, otherwise error code.
*/
int mbedtls_test_ssl_endpoint_init_ssl(
mbedtls_test_ssl_endpoint *ep,
const mbedtls_test_handshake_test_options *options);
/** Initialize the configuration and a context in an SSL endpoint structure.
*
* This function is equivalent to calling
* mbedtls_test_ssl_endpoint_init_conf() followed by
* mbedtls_test_ssl_endpoint_init_ssl().
*
* \note You must call `mbedtls_test_ssl_endpoint_free()` after
* calling this function, even if it fails. This is necessary to
* free data that may have been stored in the endpoint structure.
*
* \param[out] ep The endpoint structure to configure.
* \param endpoint_type #MBEDTLS_SSL_IS_SERVER or #MBEDTLS_SSL_IS_CLIENT.
* \param[in] options The options to use for configuring the endpoint
* structure.
*
* \retval 0 on success, otherwise error code.
*/
int mbedtls_test_ssl_endpoint_init(
mbedtls_test_ssl_endpoint *ep, int endpoint_type,
mbedtls_test_handshake_test_options *options,
mbedtls_test_message_socket_context *dtls_context,
mbedtls_test_ssl_message_queue *input_queue,
mbedtls_test_ssl_message_queue *output_queue);
const mbedtls_test_handshake_test_options *options);
/*
* Deinitializes endpoint represented by \p ep.
*/
void mbedtls_test_ssl_endpoint_free(
mbedtls_test_ssl_endpoint *ep,
mbedtls_test_message_socket_context *context);
void mbedtls_test_ssl_endpoint_free(mbedtls_test_ssl_endpoint *ep);
/* Join a DTLS client with a DTLS server.
*
* You must call this function after setting up the endpoint objects
* and before starting a DTLS handshake.
*
* \param client The client. It must have been set up with
* mbedtls_test_ssl_endpoint_init().
* \param server The server. It must have been set up with
* mbedtls_test_ssl_endpoint_init().
*
* \retval 0 on success, otherwise error code.
*/
int mbedtls_test_ssl_dtls_join_endpoints(mbedtls_test_ssl_endpoint *client,
mbedtls_test_ssl_endpoint *server);
/*
* This function moves ssl handshake from \p ssl to prescribed \p state.
@ -610,8 +666,51 @@ int mbedtls_test_ssl_do_handshake_with_endpoints(
#endif /* defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) */
#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
/** Perform an SSL handshake and exchange data over the connection.
*
* This function also handles cases where the handshake is expected to fail.
*
* If the handshake succeeds as expected, this function validates that
* connection parameters are as expected, exchanges data over the
* connection, and exercises some optional protocol features if they
* are enabled. See the code to see what features are validated and exercised.
*
* The handshake is expected to fail in the following cases:
* - If `options->expected_handshake_result != 0`.
* - If `options->expected_negotiated_version == MBEDTLS_SSL_VERSION_UNKNOWN`.
*
* \param[in] options Options for the connection.
* \param client The client endpoint. It must have been set up with
* mbedtls_test_ssl_endpoint_init() with \p options
* and #MBEDTLS_SSL_IS_CLIENT.
* \param server The server endpoint. It must have been set up with
* mbedtls_test_ssl_endpoint_init() with \p options
* and #MBEDTLS_SSL_IS_CLIENT.
*
* \return 1 on success, 0 on failure. On failure, this function
* calls mbedtls_test_fail(), indicating the failure
* reason and location. The causes of failure are:
* - Inconsistent options or bad endpoint state.
* - Operational problem during the handshake.
* - The handshake was expected to pass, but failed.
* - The handshake was expected to fail, but passed or
* failed with a different result.
* - The handshake passed as expected, but some connection
* parameter (e.g. protocol version, cipher suite, ...)
* is not as expected.
* - The handshake passed as expected, but something
* went wrong when attempting to exchange data.
* - The handshake passed as expected, but something
* went wrong when exercising other features
* (e.g. renegotiation, serialization, ...).
*/
int mbedtls_test_ssl_perform_connection(
const mbedtls_test_handshake_test_options *options,
mbedtls_test_ssl_endpoint *client,
mbedtls_test_ssl_endpoint *server);
void mbedtls_test_ssl_perform_handshake(
mbedtls_test_handshake_test_options *options);
const mbedtls_test_handshake_test_options *options);
#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
#if defined(MBEDTLS_TEST_HOOKS)

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff