Skip to content

Commit 813510c

Browse files
author
Xiaohanjlll
committed
Add unit test for SPDM 1.3 CHALLENGE: context field.
Signed-off-by: Xiaohanjlll <[email protected]>
1 parent ca2fde8 commit 813510c

File tree

4 files changed

+632
-0
lines changed

4 files changed

+632
-0
lines changed

unit_test/test_spdm_requester/challenge.c

+327
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ static uint8_t m_libspdm_local_buffer[LIBSPDM_MAX_MESSAGE_M1M2_BUFFER_SIZE];
1515
static size_t m_libspdm_opaque_data_size;
1616
static uint8_t m_libspdm_opaque_data[SPDM_MAX_OPAQUE_DATA_SIZE];
1717

18+
static uint8_t m_requester_context[SPDM_REQ_CONTEXT_SIZE];
19+
1820
libspdm_return_t libspdm_requester_challenge_test_send_message(void *spdm_context,
1921
size_t request_size,
2022
const void *request,
@@ -130,6 +132,13 @@ libspdm_return_t libspdm_requester_challenge_test_send_message(void *spdm_contex
130132
request_size - 1);
131133
m_libspdm_local_buffer_size += (request_size - 1);
132134
return LIBSPDM_STATUS_SUCCESS;
135+
case 0x1B:
136+
case 0x1C:
137+
m_libspdm_local_buffer_size = 0;
138+
libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer), &ptr[1],
139+
request_size - 1);
140+
m_libspdm_local_buffer_size += (request_size - 1);
141+
return LIBSPDM_STATUS_SUCCESS;
133142
default:
134143
return LIBSPDM_STATUS_SEND_FAIL;
135144
}
@@ -1770,6 +1779,175 @@ libspdm_return_t libspdm_requester_challenge_test_receive_message(
17701779
spdm_response, response_size, response);
17711780
}
17721781
return LIBSPDM_STATUS_SUCCESS;
1782+
case 0x1B: {
1783+
spdm_challenge_auth_response_t *spdm_response;
1784+
void *data;
1785+
size_t data_size;
1786+
uint8_t *ptr;
1787+
uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
1788+
size_t sig_size;
1789+
size_t spdm_response_size;
1790+
size_t transport_header_size;
1791+
uint8_t slot_id = 0;
1792+
1793+
libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
1794+
m_libspdm_use_asym_algo, &data,
1795+
&data_size, NULL, NULL);
1796+
((libspdm_context_t *)spdm_context)
1797+
->local_context.local_cert_chain_provision_size[0] =
1798+
data_size;
1799+
((libspdm_context_t *)spdm_context)
1800+
->local_context.local_cert_chain_provision[0] = data;
1801+
((libspdm_context_t *)spdm_context)
1802+
->connection_info.algorithm.base_asym_algo =
1803+
m_libspdm_use_asym_algo;
1804+
((libspdm_context_t *)spdm_context)
1805+
->connection_info.algorithm.base_hash_algo =
1806+
m_libspdm_use_hash_algo;
1807+
spdm_response_size = sizeof(spdm_challenge_auth_response_t) +
1808+
libspdm_get_hash_size(m_libspdm_use_hash_algo) +
1809+
SPDM_NONCE_SIZE + 0 + sizeof(uint16_t) + 0 +
1810+
libspdm_get_asym_signature_size(m_libspdm_use_asym_algo) +
1811+
SPDM_REQ_CONTEXT_SIZE;
1812+
transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
1813+
spdm_response = (void *)((uint8_t *)*response + transport_header_size);
1814+
1815+
spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_13;
1816+
spdm_response->header.request_response_code =
1817+
SPDM_CHALLENGE_AUTH;
1818+
spdm_response->header.param1 = slot_id;
1819+
spdm_response->header.param2 = (1 << slot_id);
1820+
ptr = (void *)(spdm_response + 1);
1821+
libspdm_hash_all(
1822+
m_libspdm_use_hash_algo,
1823+
((libspdm_context_t *)spdm_context)
1824+
->local_context.local_cert_chain_provision[slot_id],
1825+
((libspdm_context_t *)spdm_context)
1826+
->local_context
1827+
.local_cert_chain_provision_size[slot_id],
1828+
ptr);
1829+
free(data);
1830+
ptr += libspdm_get_hash_size(m_libspdm_use_hash_algo);
1831+
libspdm_get_random_number(SPDM_NONCE_SIZE, ptr);
1832+
ptr += SPDM_NONCE_SIZE;
1833+
*(uint16_t *)ptr = 0;
1834+
ptr += sizeof(uint16_t);
1835+
libspdm_copy_mem(ptr, SPDM_REQ_CONTEXT_SIZE, m_requester_context, SPDM_REQ_CONTEXT_SIZE);
1836+
ptr += SPDM_REQ_CONTEXT_SIZE;
1837+
libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
1838+
sizeof(m_libspdm_local_buffer) -
1839+
(&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
1840+
m_libspdm_local_buffer),
1841+
spdm_response, (size_t)ptr - (size_t)spdm_response);
1842+
m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
1843+
LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%x):\n",
1844+
m_libspdm_local_buffer_size));
1845+
libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
1846+
libspdm_hash_all(m_libspdm_use_hash_algo, m_libspdm_local_buffer,
1847+
m_libspdm_local_buffer_size, hash_data);
1848+
LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "HashDataSize (0x%x):\n",
1849+
libspdm_get_hash_size(m_libspdm_use_hash_algo)));
1850+
libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
1851+
sig_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
1852+
libspdm_responder_data_sign(
1853+
spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
1854+
SPDM_CHALLENGE_AUTH,
1855+
m_libspdm_use_asym_algo, m_libspdm_use_hash_algo,
1856+
false, m_libspdm_local_buffer, m_libspdm_local_buffer_size,
1857+
ptr, &sig_size);
1858+
ptr += sig_size;
1859+
1860+
libspdm_transport_test_encode_message(spdm_context, NULL, false,
1861+
false, spdm_response_size,
1862+
spdm_response, response_size,
1863+
response);
1864+
}
1865+
return LIBSPDM_STATUS_SUCCESS;
1866+
case 0x1C: {
1867+
spdm_challenge_auth_response_t *spdm_response;
1868+
void *data;
1869+
size_t data_size;
1870+
uint8_t *ptr;
1871+
uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
1872+
size_t sig_size;
1873+
size_t spdm_response_size;
1874+
size_t transport_header_size;
1875+
uint8_t slot_id = 0;
1876+
1877+
libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
1878+
m_libspdm_use_asym_algo, &data,
1879+
&data_size, NULL, NULL);
1880+
((libspdm_context_t *)spdm_context)
1881+
->local_context.local_cert_chain_provision_size[0] =
1882+
data_size;
1883+
((libspdm_context_t *)spdm_context)
1884+
->local_context.local_cert_chain_provision[0] = data;
1885+
((libspdm_context_t *)spdm_context)
1886+
->connection_info.algorithm.base_asym_algo =
1887+
m_libspdm_use_asym_algo;
1888+
((libspdm_context_t *)spdm_context)
1889+
->connection_info.algorithm.base_hash_algo =
1890+
m_libspdm_use_hash_algo;
1891+
spdm_response_size = sizeof(spdm_challenge_auth_response_t) +
1892+
libspdm_get_hash_size(m_libspdm_use_hash_algo) +
1893+
SPDM_NONCE_SIZE + 0 + sizeof(uint16_t) + 0 +
1894+
libspdm_get_asym_signature_size(m_libspdm_use_asym_algo) +
1895+
SPDM_REQ_CONTEXT_SIZE;
1896+
transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
1897+
spdm_response = (void *)((uint8_t *)*response + transport_header_size);
1898+
1899+
spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_13;
1900+
spdm_response->header.request_response_code =
1901+
SPDM_CHALLENGE_AUTH;
1902+
spdm_response->header.param1 = slot_id;
1903+
spdm_response->header.param2 = (1 << slot_id);
1904+
ptr = (void *)(spdm_response + 1);
1905+
libspdm_hash_all(
1906+
m_libspdm_use_hash_algo,
1907+
((libspdm_context_t *)spdm_context)
1908+
->local_context.local_cert_chain_provision[slot_id],
1909+
((libspdm_context_t *)spdm_context)
1910+
->local_context
1911+
.local_cert_chain_provision_size[slot_id],
1912+
ptr);
1913+
free(data);
1914+
ptr += libspdm_get_hash_size(m_libspdm_use_hash_algo);
1915+
libspdm_get_random_number(SPDM_NONCE_SIZE, ptr);
1916+
ptr += SPDM_NONCE_SIZE;
1917+
*(uint16_t *)ptr = 0;
1918+
ptr += sizeof(uint16_t);
1919+
libspdm_get_random_number(SPDM_REQ_CONTEXT_SIZE,ptr);
1920+
ptr += SPDM_REQ_CONTEXT_SIZE;
1921+
1922+
libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
1923+
sizeof(m_libspdm_local_buffer) -
1924+
(&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
1925+
m_libspdm_local_buffer),
1926+
spdm_response, (size_t)ptr - (size_t)spdm_response);
1927+
m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
1928+
LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%x):\n",
1929+
m_libspdm_local_buffer_size));
1930+
libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
1931+
libspdm_hash_all(m_libspdm_use_hash_algo, m_libspdm_local_buffer,
1932+
m_libspdm_local_buffer_size, hash_data);
1933+
LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "HashDataSize (0x%x):\n",
1934+
libspdm_get_hash_size(m_libspdm_use_hash_algo)));
1935+
libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
1936+
sig_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
1937+
libspdm_responder_data_sign(
1938+
spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
1939+
SPDM_CHALLENGE_AUTH,
1940+
m_libspdm_use_asym_algo, m_libspdm_use_hash_algo,
1941+
false, m_libspdm_local_buffer, m_libspdm_local_buffer_size,
1942+
ptr, &sig_size);
1943+
ptr += sig_size;
1944+
1945+
libspdm_transport_test_encode_message(spdm_context, NULL, false,
1946+
false, spdm_response_size,
1947+
spdm_response, response_size,
1948+
response);
1949+
}
1950+
return LIBSPDM_STATUS_SUCCESS;
17731951
default:
17741952
return LIBSPDM_STATUS_RECEIVE_FAIL;
17751953
}
@@ -3566,6 +3744,151 @@ void libspdm_test_requester_challenge_case26(void **state) {
35663744
free(data);
35673745
}
35683746

3747+
/**
3748+
* Test 27: Successful case , With the correct challenge context field
3749+
* Expected Behavior: client returns a status of RETURN_SUCCESS.
3750+
**/
3751+
void libspdm_test_requester_challenge_case27(void **state)
3752+
{
3753+
libspdm_return_t status;
3754+
libspdm_test_context_t *spdm_test_context;
3755+
libspdm_context_t *spdm_context;
3756+
uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
3757+
void *data;
3758+
size_t data_size;
3759+
void *hash;
3760+
size_t hash_size;
3761+
3762+
spdm_test_context = *state;
3763+
spdm_context = spdm_test_context->spdm_context;
3764+
spdm_test_context->case_id = 0x1B;
3765+
spdm_context->connection_info.connection_state =
3766+
LIBSPDM_CONNECTION_STATE_NEGOTIATED;
3767+
spdm_context->connection_info.capability.flags = 0;
3768+
spdm_context->connection_info.capability.flags |=
3769+
SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CHAL_CAP;
3770+
libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
3771+
m_libspdm_use_asym_algo, &data,
3772+
&data_size, &hash, &hash_size);
3773+
libspdm_reset_message_a(spdm_context);
3774+
libspdm_reset_message_b(spdm_context);
3775+
libspdm_reset_message_c(spdm_context);
3776+
spdm_context->connection_info.algorithm.base_hash_algo =
3777+
m_libspdm_use_hash_algo;
3778+
spdm_context->connection_info.algorithm.base_asym_algo =
3779+
m_libspdm_use_asym_algo;
3780+
3781+
spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 <<
3782+
SPDM_VERSION_NUMBER_SHIFT_BIT;
3783+
3784+
#if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
3785+
spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
3786+
data_size;
3787+
libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
3788+
sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
3789+
data, data_size);
3790+
#else
3791+
libspdm_hash_all(
3792+
spdm_context->connection_info.algorithm.base_hash_algo,
3793+
data, data_size,
3794+
spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
3795+
spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
3796+
libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
3797+
libspdm_get_leaf_cert_public_key_from_cert_chain(
3798+
spdm_context->connection_info.algorithm.base_hash_algo,
3799+
spdm_context->connection_info.algorithm.base_asym_algo,
3800+
data, data_size,
3801+
&spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
3802+
#endif
3803+
3804+
libspdm_set_mem(m_requester_context, SPDM_REQ_CONTEXT_SIZE, 0xAA);
3805+
3806+
libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
3807+
3808+
status = libspdm_challenge_ex2(
3809+
spdm_context, NULL, 0, m_requester_context,
3810+
SPDM_CHALLENGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH,
3811+
measurement_hash, NULL, NULL, NULL, NULL, NULL, NULL);
3812+
assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
3813+
3814+
#if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
3815+
assert_int_equal(spdm_context->transcript.message_c.buffer_size, 0);
3816+
#else
3817+
assert_null(spdm_context->transcript.digest_context_m1m2);
3818+
#endif
3819+
free(data);
3820+
}
3821+
3822+
/**
3823+
* Test 28: Error case , challenge context fields are inconsistent
3824+
* Expected Behavior: get a LIBSPDM_STATUS_INVALID_MSG_FIELD return code
3825+
**/
3826+
void libspdm_test_requester_challenge_case28(void **state)
3827+
{
3828+
libspdm_return_t status;
3829+
libspdm_test_context_t *spdm_test_context;
3830+
libspdm_context_t *spdm_context;
3831+
uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
3832+
void *data;
3833+
size_t data_size;
3834+
void *hash;
3835+
size_t hash_size;
3836+
3837+
spdm_test_context = *state;
3838+
spdm_context = spdm_test_context->spdm_context;
3839+
spdm_test_context->case_id = 0x1C;
3840+
spdm_context->connection_info.connection_state =
3841+
LIBSPDM_CONNECTION_STATE_NEGOTIATED;
3842+
spdm_context->connection_info.capability.flags = 0;
3843+
spdm_context->connection_info.capability.flags |=
3844+
SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CHAL_CAP;
3845+
libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
3846+
m_libspdm_use_asym_algo, &data,
3847+
&data_size, &hash, &hash_size);
3848+
libspdm_reset_message_a(spdm_context);
3849+
libspdm_reset_message_b(spdm_context);
3850+
libspdm_reset_message_c(spdm_context);
3851+
spdm_context->connection_info.algorithm.base_hash_algo =
3852+
m_libspdm_use_hash_algo;
3853+
spdm_context->connection_info.algorithm.base_asym_algo =
3854+
m_libspdm_use_asym_algo;
3855+
3856+
spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 <<
3857+
SPDM_VERSION_NUMBER_SHIFT_BIT;
3858+
3859+
#if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
3860+
spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
3861+
data_size;
3862+
libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
3863+
sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
3864+
data, data_size);
3865+
#else
3866+
libspdm_hash_all(
3867+
spdm_context->connection_info.algorithm.base_hash_algo,
3868+
data, data_size,
3869+
spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
3870+
spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
3871+
libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
3872+
libspdm_get_leaf_cert_public_key_from_cert_chain(
3873+
spdm_context->connection_info.algorithm.base_hash_algo,
3874+
spdm_context->connection_info.algorithm.base_asym_algo,
3875+
data, data_size,
3876+
&spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
3877+
#endif
3878+
3879+
libspdm_set_mem(m_requester_context, SPDM_REQ_CONTEXT_SIZE, 0xAA);
3880+
3881+
libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
3882+
3883+
status = libspdm_challenge_ex2(
3884+
spdm_context, NULL, 0, m_requester_context,
3885+
SPDM_CHALLENGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH,
3886+
measurement_hash, NULL, NULL, NULL, NULL, NULL, NULL);
3887+
assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
3888+
3889+
free(data);
3890+
}
3891+
35693892
libspdm_test_context_t m_libspdm_requester_challenge_test_context = {
35703893
LIBSPDM_TEST_CONTEXT_VERSION,
35713894
true,
@@ -3622,6 +3945,10 @@ int libspdm_requester_challenge_test_main(void)
36223945
cmocka_unit_test(libspdm_test_requester_challenge_case25),
36233946
/* the OpaqueDataFmt1 bit is selected in OtherParamsSelection of ALGORITHMS*/
36243947
cmocka_unit_test(libspdm_test_requester_challenge_case26),
3948+
/* Successful response, With the correct challenge context field*/
3949+
cmocka_unit_test(libspdm_test_requester_challenge_case27),
3950+
/* Error response: challenge context fields are inconsistent*/
3951+
cmocka_unit_test(libspdm_test_requester_challenge_case28),
36253952
};
36263953

36273954
libspdm_setup_test_context(&m_libspdm_requester_challenge_test_context);

0 commit comments

Comments
 (0)