Skip to content
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
46 changes: 36 additions & 10 deletions nx_secure/src/nx_secure_generate_client_key_exchange.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* SPDX-License-Identifier: MIT
**************************************************************************/

// Some portions generated by Codex gpt-5.5.


/**************************************************************************/
/**************************************************************************/
Expand Down Expand Up @@ -124,15 +126,40 @@ NX_CRYPTO_EXTENDED_OUTPUT extended_output;
{
data_size = (UINT)(1 + tls_key_material -> nx_secure_tls_new_key_material_data[0]);

if ((data_size > sizeof(tls_key_material -> nx_secure_tls_new_key_material_data)) ||
(data_size > buffer_length))
#ifdef NX_SECURE_ENABLE_PSK_CIPHERSUITES
if (ciphersuite -> nx_secure_tls_public_auth -> nx_crypto_algorithm == NX_CRYPTO_KEY_EXCHANGE_PSK)
{
if ((tls_credentials -> nx_secure_tls_client_psk.nx_secure_tls_psk_id_size >
sizeof(tls_credentials -> nx_secure_tls_client_psk.nx_secure_tls_psk_id)) ||
(data_size > sizeof(tls_key_material -> nx_secure_tls_new_key_material_data)) ||
(((ULONG)data_size + 2u + tls_credentials -> nx_secure_tls_client_psk.nx_secure_tls_psk_id_size) > buffer_length))
{

/* Packet buffer too small. */
return(NX_SECURE_TLS_PACKET_BUFFER_TOO_SMALL);
/* Packet buffer too small. */
return(NX_SECURE_TLS_PACKET_BUFFER_TOO_SMALL);
}

data_buffer[0] = (UCHAR)((tls_credentials -> nx_secure_tls_client_psk.nx_secure_tls_psk_id_size & 0xFF00) >> 8);
data_buffer[1] = (UCHAR)(tls_credentials -> nx_secure_tls_client_psk.nx_secure_tls_psk_id_size & 0x00FF);
NX_SECURE_MEMCPY(&data_buffer[2], tls_credentials -> nx_secure_tls_client_psk.nx_secure_tls_psk_id,
tls_credentials -> nx_secure_tls_client_psk.nx_secure_tls_psk_id_size); /* Use case of memcpy is verified. */
NX_SECURE_MEMCPY(&data_buffer[2 + tls_credentials -> nx_secure_tls_client_psk.nx_secure_tls_psk_id_size],
tls_key_material -> nx_secure_tls_new_key_material_data, data_size); /* Use case of memcpy is verified. */
data_size += 2 + tls_credentials -> nx_secure_tls_client_psk.nx_secure_tls_psk_id_size;
}
else
#endif /* NX_SECURE_ENABLE_PSK_CIPHERSUITES */
{
if ((data_size > sizeof(tls_key_material -> nx_secure_tls_new_key_material_data)) ||
(data_size > buffer_length))
{

NX_SECURE_MEMCPY(data_buffer, tls_key_material -> nx_secure_tls_new_key_material_data, data_size); /* Use case of memcpy is verified. */
/* Packet buffer too small. */
return(NX_SECURE_TLS_PACKET_BUFFER_TOO_SMALL);
}

NX_SECURE_MEMCPY(data_buffer, tls_key_material -> nx_secure_tls_new_key_material_data, data_size); /* Use case of memcpy is verified. */
}
}
else
#endif /* NX_SECURE_ENABLE_ECC_CIPHERSUITE */
Expand All @@ -145,10 +172,10 @@ NX_CRYPTO_EXTENDED_OUTPUT extended_output;
/* Check for PSK ciphersuites. */
if (ciphersuite -> nx_secure_tls_public_auth -> nx_crypto_algorithm == NX_CRYPTO_KEY_EXCHANGE_PSK)
{
if ((tls_credentials -> nx_secure_tls_client_psk.nx_secure_tls_psk_id_hint_size >
sizeof(tls_credentials -> nx_secure_tls_client_psk.nx_secure_tls_psk_id_hint)) ||
(tls_credentials -> nx_secure_tls_client_psk.nx_secure_tls_psk_id_hint_size >
(buffer_length - 2)))
if ((tls_credentials -> nx_secure_tls_client_psk.nx_secure_tls_psk_id_size >
sizeof(tls_credentials -> nx_secure_tls_client_psk.nx_secure_tls_psk_id)) ||
((buffer_length < 2u) || (tls_credentials -> nx_secure_tls_client_psk.nx_secure_tls_psk_id_size >
(buffer_length - 2u))))
{

/* Packet buffer too small. */
Expand Down Expand Up @@ -327,4 +354,3 @@ NX_CRYPTO_EXTENDED_OUTPUT extended_output;

return(NX_SECURE_TLS_SUCCESS);
}

180 changes: 172 additions & 8 deletions nx_secure/src/nx_secure_generate_premaster_secret.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* SPDX-License-Identifier: MIT
**************************************************************************/

// Some portions generated by Codex gpt-5.5.


/**************************************************************************/
/**************************************************************************/
Expand All @@ -24,6 +26,141 @@

#include "nx_secure_tls.h"

#ifdef NX_SECURE_ENABLE_PSK_CIPHERSUITES
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _nx_secure_tls_psk_identity_lookup PORTABLE C */
/* 6.x */
/* AUTHOR */
/* */
/* Eclipse ThreadX Contributors */
/* */
/* DESCRIPTION */
/* */
/* This function searches the PSK store for an entry matching a peer */
/* PSK identity. */
/* */
/* INPUT */
/* */
/* tls_credentials TLS credentials */
/* psk_data Destination for PSK data */
/* psk_length Destination for PSK length */
/* psk_identity PSK identity data */
/* identity_length PSK identity length */
/* */
/* OUTPUT */
/* */
/* status Completion status */
/* */
/* CALLS */
/* */
/* tx_mutex_get Get TLS protection */
/* tx_mutex_put Release TLS protection */
/* */
/* CALLED BY */
/* */
/* _nx_secure_generate_premaster_secret Generate pre-master secret */
/* */
/**************************************************************************/
static UINT _nx_secure_tls_psk_identity_lookup(NX_SECURE_TLS_CREDENTIALS *tls_credentials, UCHAR **psk_data, UINT *psk_length,
UCHAR *psk_identity, UINT identity_length)
{
UINT psk_list_size;
UINT i;

/* Get the protection. */
tx_mutex_get(&_nx_secure_tls_protection, TX_WAIT_FOREVER);

psk_list_size = tls_credentials -> nx_secure_tls_psk_count;

/* Loop through all PSKs, looking for a matching identity string. */
for (i = 0; i < psk_list_size; ++i)
{
if (identity_length == tls_credentials -> nx_secure_tls_psk_store[i].nx_secure_tls_psk_id_size)
{
if (NX_SECURE_MEMCMP(tls_credentials -> nx_secure_tls_psk_store[i].nx_secure_tls_psk_id,
psk_identity, identity_length) == 0)
{
*psk_data = tls_credentials -> nx_secure_tls_psk_store[i].nx_secure_tls_psk_data;
*psk_length = tls_credentials -> nx_secure_tls_psk_store[i].nx_secure_tls_psk_data_size;

/* Release the protection. */
tx_mutex_put(&_nx_secure_tls_protection);

return(NX_SUCCESS);
}
}
}

/* Release the protection. */
tx_mutex_put(&_nx_secure_tls_protection);

return(NX_SECURE_TLS_NO_MATCHING_PSK);
}

/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _nx_secure_tls_client_psk_save PORTABLE C */
/* 6.x */
/* AUTHOR */
/* */
/* Eclipse ThreadX Contributors */
/* */
/* DESCRIPTION */
/* */
/* This function saves the PSK store entry selected by the server */
/* identity hint so the client sends the matching identity in */
/* ClientKeyExchange. */
/* */
/* INPUT */
/* */
/* tls_credentials TLS credentials */
/* psk_store_index Selected PSK store index */
/* */
/* OUTPUT */
/* */
/* status Completion status */
/* */
/* CALLS */
/* */
/* tx_mutex_get Get TLS protection */
/* tx_mutex_put Release TLS protection */
/* */
/* CALLED BY */
/* */
/* _nx_secure_generate_premaster_secret Generate pre-master secret */
/* */
/**************************************************************************/
static UINT _nx_secure_tls_client_psk_save(NX_SECURE_TLS_CREDENTIALS *tls_credentials, UINT psk_store_index)
{
UINT status;

/* Get the protection. */
tx_mutex_get(&_nx_secure_tls_protection, TX_WAIT_FOREVER);

if (psk_store_index < tls_credentials -> nx_secure_tls_psk_count)
{
NX_SECURE_MEMCPY(&tls_credentials -> nx_secure_tls_client_psk,
&tls_credentials -> nx_secure_tls_psk_store[psk_store_index],
sizeof(NX_SECURE_TLS_PSK_STORE)); /* Use case of memcpy is verified. */
status = NX_SUCCESS;
}
else
{
status = NX_SECURE_TLS_NO_MATCHING_PSK;
}

/* Release the protection. */
tx_mutex_put(&_nx_secure_tls_protection);

return(status);
}
#endif

/**************************************************************************/
/* */
/* FUNCTION RELEASE */
Expand Down Expand Up @@ -80,6 +217,7 @@ UINT status = NX_SECURE_TLS_SUCCESS;
UCHAR *psk_data;
UINT psk_length;
UINT index;
UINT psk_store_index;
#endif
#if defined(NX_SECURE_ENABLE_ECC_CIPHERSUITE) && !defined(NX_SECURE_DISABLE_X509)
NX_SECURE_X509_CERT *server_certificate;
Expand Down Expand Up @@ -125,9 +263,24 @@ UINT pre_master_secret_size;
containing the length of the PSK (in octets), and the PSK itself.
*/

/* Client has to search for the PSK based on the identity hint. */
status = _nx_secure_tls_psk_find(tls_credentials, &psk_data, &psk_length, tls_credentials -> nx_secure_tls_remote_psk_id,
tls_credentials -> nx_secure_tls_remote_psk_id_size, NX_NULL);
if (session_type == NX_SECURE_TLS_SESSION_TYPE_SERVER)
{
/* Server searches for the PSK based on the identity requested by the client. */
status = _nx_secure_tls_psk_identity_lookup(tls_credentials, &psk_data, &psk_length,
tls_credentials -> nx_secure_tls_remote_psk_id,
tls_credentials -> nx_secure_tls_remote_psk_id_size);
}
else
{
/* Client has to search for the PSK based on the identity hint. */
status = _nx_secure_tls_psk_find(tls_credentials, &psk_data, &psk_length,
tls_credentials -> nx_secure_tls_remote_psk_id,
tls_credentials -> nx_secure_tls_remote_psk_id_size, &psk_store_index);
if (status == NX_SUCCESS)
{
status = _nx_secure_tls_client_psk_save(tls_credentials, psk_store_index);
}
}

if (status != NX_SUCCESS)
{
Expand Down Expand Up @@ -280,20 +433,32 @@ UINT pre_master_secret_size;
/* Now, using the identity as a key, find the PSK in our PSK store. */
if (session_type == NX_SECURE_TLS_SESSION_TYPE_SERVER)
{
/* Server just uses its PSK. */
psk_data = tls_credentials -> nx_secure_tls_psk_store[0].nx_secure_tls_psk_data;
psk_length = tls_credentials -> nx_secure_tls_psk_store[0].nx_secure_tls_psk_data_size;
/* Server searches for the PSK based on the identity requested by the client. */
status = _nx_secure_tls_psk_identity_lookup(tls_credentials, &psk_data, &psk_length,
tls_credentials -> nx_secure_tls_remote_psk_id,
tls_credentials -> nx_secure_tls_remote_psk_id_size);

if (status != NX_SUCCESS)
{
return(status);
}
}
else
{
/* Client has to search for the PSK based on the identity hint. */
status = _nx_secure_tls_psk_find(tls_credentials, &psk_data, &psk_length, tls_credentials -> nx_secure_tls_remote_psk_id,
tls_credentials -> nx_secure_tls_remote_psk_id_size, NX_NULL);
tls_credentials -> nx_secure_tls_remote_psk_id_size, &psk_store_index);

if (status != NX_SUCCESS)
{
return(status);
}

status = _nx_secure_tls_client_psk_save(tls_credentials, psk_store_index);
if (status != NX_SUCCESS)
{
return(status);
}
}

/* From RFC 4279:
Expand Down Expand Up @@ -369,4 +534,3 @@ UINT pre_master_secret_size;

return(status);
}

Loading
Loading