Skip to content

Commit b521296

Browse files
Xiaohanjllljyao1
Xiaohanjlll
authored andcommitted
Add unit test for SPDM 1.3 CERTIFICATE: request attributes and response attributes
Signed-off-by: Xiaohanjlll <[email protected]>
1 parent beb810a commit b521296

File tree

4 files changed

+481
-0
lines changed

4 files changed

+481
-0
lines changed

Diff for: unit_test/test_spdm_requester/encap_certificate.c

+70
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ spdm_get_certificate_request_t m_spdm_get_certificate_request3 = {
3232
size_t m_spdm_get_certificate_request3_size =
3333
sizeof(m_spdm_get_certificate_request3);
3434

35+
spdm_get_certificate_request_t m_spdm_get_certificate_request4 = {
36+
{SPDM_MESSAGE_VERSION_13, SPDM_GET_CERTIFICATE, 0, 0},
37+
0,
38+
LIBSPDM_MAX_CERT_CHAIN_BLOCK_LEN
39+
};
40+
size_t m_spdm_get_certificate_request4_size =
41+
sizeof(m_spdm_get_certificate_request4);
42+
3543
/**
3644
* Test 1: request the first LIBSPDM_MAX_CERT_CHAIN_BLOCK_LEN bytes of the
3745
* certificate chain Expected Behavior: generate a correctly formed Certficate
@@ -460,6 +468,66 @@ void libspdm_test_requester_encap_certificate_case6(void **state)
460468
free(data);
461469
}
462470

471+
/**
472+
* Test 7: check request attributes and response attributes , SlotSizeRequested=1b the Offset and Length fields in the
473+
* GET_CERTIFICATE request shall be ignored by the Responde
474+
* Expected Behavior: generate a correctly formed Certficate message, including its portion_length and remainder_length fields
475+
**/
476+
void libspdm_test_requester_encap_certificate_case7(void **state)
477+
{
478+
libspdm_return_t status;
479+
libspdm_test_context_t *spdm_test_context;
480+
libspdm_context_t *spdm_context;
481+
size_t response_size;
482+
uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
483+
spdm_certificate_response_t *spdm_response;
484+
void *data;
485+
size_t data_size;
486+
487+
spdm_test_context = *state;
488+
spdm_context = spdm_test_context->spdm_context;
489+
spdm_test_context->case_id = 0x7;
490+
spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13
491+
<< SPDM_VERSION_NUMBER_SHIFT_BIT;
492+
spdm_context->connection_info.connection_state =
493+
LIBSPDM_CONNECTION_STATE_AFTER_DIGESTS;
494+
spdm_context->local_context.capability.flags = 0;
495+
spdm_context->local_context.capability.flags |=
496+
SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP;
497+
spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
498+
libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
499+
m_libspdm_use_asym_algo,
500+
&data, &data_size, NULL, NULL);
501+
spdm_context->local_context.local_cert_chain_provision[0] = data;
502+
spdm_context->local_context.local_cert_chain_provision_size[0] = data_size;
503+
504+
#if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
505+
spdm_context->transcript.message_mut_b.buffer_size = 0;
506+
#endif
507+
508+
/* When SlotSizeRequested=1b , the Offset and Length fields in the GET_CERTIFICATE request shall be ignored by the Responder */
509+
m_spdm_get_certificate_request4.header.param2 =
510+
SPDM_GET_CERTIFICATE_REQUEST_ATTRIBUTES_SLOT_SIZE_REQUESTED;
511+
m_spdm_get_certificate_request4.length = LIBSPDM_MAX_CERT_CHAIN_BLOCK_LEN;
512+
m_spdm_get_certificate_request4.offset = 0xFF;
513+
514+
response_size = sizeof(response);
515+
status = libspdm_get_encap_response_certificate(
516+
spdm_context, m_spdm_get_certificate_request4_size,
517+
&m_spdm_get_certificate_request4, &response_size, response);
518+
519+
assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
520+
assert_int_equal(response_size, sizeof(spdm_certificate_response_t));
521+
spdm_response = (void *)response;
522+
assert_int_equal(spdm_response->header.request_response_code,
523+
SPDM_CERTIFICATE);
524+
assert_int_equal(spdm_response->header.param1, 0);
525+
assert_int_equal(spdm_response->portion_length,0);
526+
assert_int_equal(spdm_response->remainder_length, data_size);
527+
528+
free(data);
529+
}
530+
463531
libspdm_test_context_t m_libspdm_requester_encap_certificate_test_context = {
464532
LIBSPDM_TEST_CONTEXT_VERSION,
465533
false,
@@ -478,6 +546,8 @@ int libspdm_requester_encap_certificate_test_main(void)
478546
cmocka_unit_test(libspdm_test_requester_encap_certificate_case5),
479547
/* Requests byte by byte*/
480548
cmocka_unit_test(libspdm_test_requester_encap_certificate_case6),
549+
/* check request attributes and response attributes*/
550+
cmocka_unit_test(libspdm_test_requester_encap_certificate_case7),
481551
};
482552

483553
libspdm_setup_test_context(&m_libspdm_requester_encap_certificate_test_context);

Diff for: unit_test/test_spdm_requester/get_certificate.c

+204
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ static uint8_t m_libspdm_local_buffer[LIBSPDM_MAX_MESSAGE_M1M2_BUFFER_SIZE];
1818

1919
static bool m_get_cert;
2020

21+
static uint8_t m_cert_model;
22+
2123
/* Loading the target expiration certificate chain and saving root certificate hash
2224
* "rsa3072_Expiration/bundle_responder.certchain.der"*/
2325
bool libspdm_libspdm_read_responder_public_certificate_chain_expiration(
@@ -175,6 +177,7 @@ libspdm_return_t libspdm_requester_get_certificate_test_send_message(
175177
case 0x1C:
176178
return LIBSPDM_STATUS_SUCCESS;
177179
case 0x1D:
180+
case 0x1E:
178181
return LIBSPDM_STATUS_SUCCESS;
179182
default:
180183
return LIBSPDM_STATUS_SEND_FAIL;
@@ -2156,6 +2159,71 @@ libspdm_return_t libspdm_requester_get_certificate_test_receive_message(
21562159
spdm_response, response_size,
21572160
response);
21582161

2162+
calling_index++;
2163+
if (calling_index == count) {
2164+
calling_index = 0;
2165+
free(m_libspdm_local_certificate_chain);
2166+
m_libspdm_local_certificate_chain = NULL;
2167+
m_libspdm_local_certificate_chain_size = 0;
2168+
}
2169+
}
2170+
return LIBSPDM_STATUS_SUCCESS;
2171+
case 0x1E: {
2172+
spdm_certificate_response_t *spdm_response;
2173+
size_t spdm_response_size;
2174+
size_t transport_header_size;
2175+
uint16_t portion_length;
2176+
uint16_t remainder_length;
2177+
size_t count;
2178+
static size_t calling_index = 0;
2179+
2180+
if (m_libspdm_local_certificate_chain == NULL) {
2181+
libspdm_read_responder_public_certificate_chain(
2182+
m_libspdm_use_hash_algo, m_libspdm_use_asym_algo,
2183+
&m_libspdm_local_certificate_chain,
2184+
&m_libspdm_local_certificate_chain_size, NULL, NULL);
2185+
}
2186+
if (m_libspdm_local_certificate_chain == NULL) {
2187+
return LIBSPDM_STATUS_RECEIVE_FAIL;
2188+
}
2189+
count = (m_libspdm_local_certificate_chain_size +
2190+
LIBSPDM_MAX_CERT_CHAIN_BLOCK_LEN - 1) /
2191+
LIBSPDM_MAX_CERT_CHAIN_BLOCK_LEN;
2192+
if (calling_index != count - 1) {
2193+
portion_length = LIBSPDM_MAX_CERT_CHAIN_BLOCK_LEN;
2194+
remainder_length =
2195+
(uint16_t)(m_libspdm_local_certificate_chain_size -
2196+
LIBSPDM_MAX_CERT_CHAIN_BLOCK_LEN *
2197+
(calling_index + 1));
2198+
} else {
2199+
portion_length = (uint16_t)(
2200+
m_libspdm_local_certificate_chain_size -
2201+
LIBSPDM_MAX_CERT_CHAIN_BLOCK_LEN * (count - 1));
2202+
remainder_length = 0;
2203+
}
2204+
2205+
spdm_response_size =
2206+
sizeof(spdm_certificate_response_t) + portion_length;
2207+
transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
2208+
spdm_response = (void *)((uint8_t *)*response + transport_header_size);
2209+
2210+
spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_13;
2211+
spdm_response->header.request_response_code = SPDM_CERTIFICATE;
2212+
spdm_response->header.param1 = 0;
2213+
spdm_response->header.param2 = m_cert_model;
2214+
spdm_response->portion_length = portion_length;
2215+
spdm_response->remainder_length = remainder_length;
2216+
libspdm_copy_mem(spdm_response + 1,
2217+
(size_t)(*response) + *response_size - (size_t)(spdm_response + 1),
2218+
(uint8_t *)m_libspdm_local_certificate_chain +
2219+
LIBSPDM_MAX_CERT_CHAIN_BLOCK_LEN * calling_index,
2220+
portion_length);
2221+
2222+
libspdm_transport_test_encode_message(spdm_context, NULL, false,
2223+
false, spdm_response_size,
2224+
spdm_response, response_size,
2225+
response);
2226+
21592227
calling_index++;
21602228
if (calling_index == count) {
21612229
calling_index = 0;
@@ -4076,6 +4144,140 @@ void libspdm_test_requester_get_certificate_case29(void **state)
40764144
free(data);
40774145
}
40784146

4147+
/**
4148+
* Test 30: check request attributes and response attributes ,
4149+
* Set CertModel to determine whether it meets expectations
4150+
* Expected Behavior: requester returns the status LIBSPDM_STATUS_SUCCESS
4151+
**/
4152+
void libspdm_test_requester_get_certificate_case30(void **state)
4153+
{
4154+
libspdm_return_t status;
4155+
libspdm_test_context_t *spdm_test_context;
4156+
libspdm_context_t *spdm_context;
4157+
size_t cert_chain_size;
4158+
uint8_t cert_chain[LIBSPDM_MAX_CERT_CHAIN_SIZE];
4159+
void *data;
4160+
size_t data_size;
4161+
void *hash;
4162+
size_t hash_size;
4163+
const uint8_t *root_cert;
4164+
size_t root_cert_size;
4165+
libspdm_data_parameter_t parameter;
4166+
4167+
#if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
4168+
#else
4169+
uint8_t set_data_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
4170+
uint32_t set_data_buffer_hash_size;
4171+
#endif
4172+
4173+
spdm_test_context = *state;
4174+
spdm_context = spdm_test_context->spdm_context;
4175+
spdm_test_context->case_id = 0x1E;
4176+
spdm_context->retry_times = 1;
4177+
spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 <<
4178+
SPDM_VERSION_NUMBER_SHIFT_BIT;
4179+
spdm_context->connection_info.connection_state =
4180+
LIBSPDM_CONNECTION_STATE_AFTER_DIGESTS;
4181+
spdm_context->connection_info.capability.flags = 0;
4182+
spdm_context->connection_info.capability.flags |=
4183+
SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP;
4184+
spdm_context->local_context.is_requester = true;
4185+
libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
4186+
m_libspdm_use_asym_algo, &data,
4187+
&data_size, &hash, &hash_size);
4188+
libspdm_x509_get_cert_from_cert_chain((uint8_t *)data + sizeof(spdm_cert_chain_t) + hash_size,
4189+
data_size - sizeof(spdm_cert_chain_t) - hash_size, 0,
4190+
&root_cert, &root_cert_size);
4191+
LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "root cert data :\n"));
4192+
libspdm_dump_hex(
4193+
root_cert,
4194+
root_cert_size);
4195+
spdm_context->local_context.peer_root_cert_provision_size[0] =
4196+
root_cert_size;
4197+
spdm_context->local_context.peer_root_cert_provision[0] = root_cert;
4198+
libspdm_reset_message_b(spdm_context);
4199+
spdm_context->connection_info.algorithm.base_hash_algo =
4200+
m_libspdm_use_hash_algo;
4201+
spdm_context->connection_info.algorithm.base_asym_algo =
4202+
m_libspdm_use_asym_algo;
4203+
spdm_context->connection_info.algorithm.req_base_asym_alg =
4204+
m_libspdm_use_req_asym_algo;
4205+
4206+
libspdm_zero_mem(&parameter, sizeof(parameter));
4207+
parameter.location = LIBSPDM_DATA_LOCATION_CONNECTION;
4208+
parameter.additional_data[0] = 0;
4209+
libspdm_set_data(spdm_context, LIBSPDM_DATA_PEER_USED_CERT_CHAIN_BUFFER, &parameter,
4210+
data, data_size);
4211+
4212+
#if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
4213+
spdm_context->transcript.message_m.buffer_size =
4214+
spdm_context->transcript.message_m.max_buffer_size;
4215+
#else
4216+
set_data_buffer_hash_size =
4217+
spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size;
4218+
libspdm_copy_mem(set_data_buffer_hash, set_data_buffer_hash_size,
4219+
spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash,
4220+
set_data_buffer_hash_size);
4221+
#endif
4222+
4223+
/* Sub Case 1: CertModel Value of 1 , DeviceCert model*/
4224+
spdm_context->connection_info.multi_key_conn_rsp = true;
4225+
spdm_context->connection_info.peer_cert_info[0] = 0;
4226+
m_cert_model = SPDM_CERTIFICATE_INFO_CERT_MODEL_DEVICE_CERT;
4227+
libspdm_reset_message_b(spdm_context);
4228+
4229+
cert_chain_size = sizeof(cert_chain);
4230+
libspdm_zero_mem(cert_chain, sizeof(cert_chain));
4231+
status = libspdm_get_certificate(spdm_context, NULL, 0, &cert_chain_size,
4232+
cert_chain);
4233+
assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
4234+
assert_int_equal(spdm_context->connection_info.peer_cert_info[0], m_cert_model);
4235+
4236+
/* Sub Case 2: CertModel Value of 2 , AliasCert model*/
4237+
spdm_context->connection_info.multi_key_conn_rsp = true;
4238+
spdm_context->connection_info.peer_cert_info[0] = 0;
4239+
m_cert_model = SPDM_CERTIFICATE_INFO_CERT_MODEL_ALIAS_CERT;
4240+
libspdm_reset_message_b(spdm_context);
4241+
4242+
cert_chain_size = sizeof(cert_chain);
4243+
libspdm_zero_mem(cert_chain, sizeof(cert_chain));
4244+
status = libspdm_get_certificate(spdm_context, NULL, 0, &cert_chain_size,
4245+
cert_chain);
4246+
assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
4247+
assert_int_equal(spdm_context->connection_info.peer_cert_info[0], m_cert_model);
4248+
libspdm_reset_message_b(spdm_context);
4249+
4250+
/* Sub Case 3: CertModel Value of 3 , GenericCert model*/
4251+
spdm_context->connection_info.multi_key_conn_rsp = true;
4252+
spdm_context->connection_info.peer_cert_info[0] = 0;
4253+
m_cert_model = SPDM_CERTIFICATE_INFO_CERT_MODEL_GENERIC_CERT;
4254+
libspdm_reset_message_b(spdm_context);
4255+
4256+
cert_chain_size = sizeof(cert_chain);
4257+
libspdm_zero_mem(cert_chain, sizeof(cert_chain));
4258+
status = libspdm_get_certificate(spdm_context, NULL, 0, &cert_chain_size,
4259+
cert_chain);
4260+
assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
4261+
assert_int_equal(spdm_context->connection_info.peer_cert_info[0], m_cert_model);
4262+
4263+
/* Sub Case 4: CertModel Value of 0 */
4264+
/* Value of 0 indicates either that the certificate slot does not contain any certificates or that the corresponding
4265+
* MULTI_KEY_CONN_REQ or MULTI_KEY_CONN_RSP is false. */
4266+
spdm_context->connection_info.multi_key_conn_rsp = true;
4267+
spdm_context->connection_info.peer_cert_info[0] = 0;
4268+
m_cert_model = SPDM_CERTIFICATE_INFO_CERT_MODEL_NONE;
4269+
libspdm_reset_message_b(spdm_context);
4270+
4271+
cert_chain_size = sizeof(cert_chain);
4272+
libspdm_zero_mem(cert_chain, sizeof(cert_chain));
4273+
status = libspdm_get_certificate(spdm_context, NULL, 0, &cert_chain_size,
4274+
cert_chain);
4275+
assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
4276+
assert_int_equal(spdm_context->connection_info.peer_cert_info[0], m_cert_model);
4277+
4278+
free(data);
4279+
}
4280+
40794281
libspdm_test_context_t m_libspdm_requester_get_certificate_test_context = {
40804282
LIBSPDM_TEST_CONTEXT_VERSION,
40814283
true,
@@ -4147,6 +4349,8 @@ int libspdm_requester_get_certificate_test_main(void)
41474349
cmocka_unit_test(libspdm_test_requester_get_certificate_case28),
41484350
/*Fail response: get the partial alias_cert model cert_chain*/
41494351
cmocka_unit_test(libspdm_test_requester_get_certificate_case29),
4352+
/* check request attributes and response attributes*/
4353+
cmocka_unit_test(libspdm_test_requester_get_certificate_case30),
41504354
};
41514355

41524356
libspdm_setup_test_context(&m_libspdm_requester_get_certificate_test_context);

0 commit comments

Comments
 (0)