Skip to content
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

[MBEDTLS_PRIVATE] Add accessor for session and ciphersuite_id #8888

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 6 additions & 0 deletions ChangeLog.d/add_ssl_session_accessors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Features
* Add new accessors to expose the private session-id,
session-id length, and ciphersuite-id members of
`mbedtls_ssl_session` structure.
Add new accessor to expose the ciphersuite-id of
`mbedtls_ssl_ciphersuite_t` structure.Design ref: #8529
37 changes: 37 additions & 0 deletions include/mbedtls/ssl.h
Original file line number Diff line number Diff line change
Expand Up @@ -2717,6 +2717,43 @@ static inline int mbedtls_ssl_session_get_ticket_creation_time(
#endif /* MBEDTLS_HAVE_TIME */
#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_SRV_C */

/**
* \brief Get the session-id buffer.
*
* \param session SSL session.
*
* \return The address of the session-id buffer.
*/
static inline unsigned const char (*mbedtls_ssl_session_get_id(const mbedtls_ssl_session *
session))[32]
{
return &session->MBEDTLS_PRIVATE(id);
}

/**
* \brief Get the size of the session-id.
*
* \param session SSL session.
*
* \return size_t size of session-id buffer.
*/
static inline size_t mbedtls_ssl_session_get_id_len(const mbedtls_ssl_session *session)
{
return session->MBEDTLS_PRIVATE(id_len);
}

/**
* \brief Get the ciphersuite-id.
*
* \param session SSL session.
*
* \return int represetation for ciphersuite.
*/
static inline int mbedtls_ssl_session_get_ciphersuite_id(const mbedtls_ssl_session *session)
{
return session->MBEDTLS_PRIVATE(ciphersuite);
}

/**
* \brief Configure a key export callback.
* (Default: none.)
Expand Down
5 changes: 5 additions & 0 deletions include/mbedtls/ssl_ciphersuites.h
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,11 @@ static inline const char *mbedtls_ssl_ciphersuite_get_name(const mbedtls_ssl_cip
return info->MBEDTLS_PRIVATE(name);
}

static inline int mbedtls_ssl_ciphersuite_get_id(const mbedtls_ssl_ciphersuite_t *info)
{
return info->MBEDTLS_PRIVATE(id);
}

size_t mbedtls_ssl_ciphersuite_get_cipher_key_bitlen(const mbedtls_ssl_ciphersuite_t *info);

#ifdef __cplusplus
Expand Down
8 changes: 8 additions & 0 deletions tests/suites/test_suite_ssl.data
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,14 @@ TLS 1.3: SRV: session serialization: Wrong config
depends_on:MBEDTLS_SSL_PROTO_TLS1_3:MBEDTLS_SSL_SRV_C
ssl_session_serialize_version_check:0:0:0:1:MBEDTLS_SSL_IS_SERVER:MBEDTLS_SSL_VERSION_TLS1_3

Test Session id & Ciphersuite accessors TLS 1.2
depends_on:MBEDTLS_SSL_PROTO_TLS1_2
ssl_session_id_accessors_check:MBEDTLS_SSL_VERSION_TLS1_2

Test Session id & Ciphersuite accessors TLS 1.3
depends_on:MBEDTLS_SSL_PROTO_TLS1_3
ssl_session_id_accessors_check:MBEDTLS_SSL_VERSION_TLS1_3

Record crypt, AES-128-CBC, 1.2, SHA-384
depends_on:MBEDTLS_SSL_HAVE_AES:MBEDTLS_SSL_HAVE_CBC:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_MD_CAN_SHA384
ssl_crypt_record:MBEDTLS_CIPHER_AES_128_CBC:MBEDTLS_MD_SHA384:0:0:MBEDTLS_SSL_VERSION_TLS1_2:0:0
Expand Down
48 changes: 48 additions & 0 deletions tests/suites/test_suite_ssl.function
Original file line number Diff line number Diff line change
Expand Up @@ -2379,6 +2379,54 @@ exit:
}
/* END_CASE */

/* BEGIN_CASE */
void ssl_session_id_accessors_check(int tls_version)
{
mbedtls_ssl_session session;
int ciphersuite_id;
const mbedtls_ssl_ciphersuite_t *ciphersuite_info;

mbedtls_ssl_session_init(&session);
USE_PSA_INIT();

switch (tls_version) {
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
case MBEDTLS_SSL_VERSION_TLS1_3:
ciphersuite_id = MBEDTLS_TLS1_3_AES_128_GCM_SHA256;
TEST_ASSERT(mbedtls_test_ssl_tls13_populate_session(
&session, 0, MBEDTLS_SSL_IS_SERVER) == 0);
break;
#endif
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
case MBEDTLS_SSL_VERSION_TLS1_2:
ciphersuite_id = MBEDTLS_TLS_PSK_WITH_AES_128_GCM_SHA256;
TEST_ASSERT(mbedtls_test_ssl_tls12_populate_session(
&session, 0, MBEDTLS_SSL_IS_SERVER, NULL) == 0);

break;
#endif
default:
/* should never happen */
TEST_ASSERT(0);
break;
}
TEST_ASSERT(*mbedtls_ssl_session_get_id(&session) == session.id);
TEST_ASSERT(mbedtls_ssl_session_get_id_len(&session) == session.id_len);
/* mbedtls_test_ssl_tls1x_populate_session sets a mock suite-id of 0xabcd */
TEST_ASSERT(mbedtls_ssl_session_get_ciphersuite_id(&session) == 0xabcd);

/* Test setting a reference id for tls1.3 and tls1.2 */
ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite_id);
if (ciphersuite_info != NULL) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In certain configurations the ssl_ciphersuite_from_id will return NULL. Testing this function is outside of the scope so we only test the getter for the other cases where it will create a ciphersuite_info stucture.

TEST_ASSERT(mbedtls_ssl_ciphersuite_get_id(ciphersuite_info) == ciphersuite_id);
}

exit:
mbedtls_ssl_session_free(&session);
USE_PSA_DONE();
}
/* END_CASE */

/* BEGIN_CASE depends_on:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED:MBEDTLS_RSA_C:MBEDTLS_ECP_HAVE_SECP384R1:!MBEDTLS_SSL_PROTO_TLS1_3:MBEDTLS_PKCS1_V15:MBEDTLS_MD_CAN_SHA256 */
void mbedtls_endpoint_sanity(int endpoint_type)
{
Expand Down