Skip to content

Commit

Permalink
Add set_key_pair_info responder unit_test
Browse files Browse the repository at this point in the history
Signed-off-by: Wenxing Hou <[email protected]>
  • Loading branch information
Wenxing-hou committed Aug 21, 2024
1 parent eada6aa commit 40febd1
Show file tree
Hide file tree
Showing 3 changed files with 319 additions and 0 deletions.
1 change: 1 addition & 0 deletions unit_test/test_spdm_responder/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ target_sources(test_spdm_responder
chunk_get.c
chunk_send_ack.c
key_pair_info.c
set_key_pair_info_ack.c
${LIBSPDM_DIR}/unit_test/spdm_unit_test_common/common.c
${LIBSPDM_DIR}/unit_test/spdm_unit_test_common/algo.c
${LIBSPDM_DIR}/unit_test/spdm_unit_test_common/support.c
Expand Down
308 changes: 308 additions & 0 deletions unit_test/test_spdm_responder/set_key_pair_info_ack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,308 @@
/**
* Copyright Notice:
* Copyright 2024 DMTF. All rights reserved.
* License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/libspdm/blob/main/LICENSE.md
**/

#include "spdm_unit_test.h"
#include "internal/libspdm_responder_lib.h"
#include "internal/libspdm_requester_lib.h"

#if LIBSPDM_ENABLE_CAPABILITY_SET_KEY_PAIR_INFO_CAP

/**
* Test 1: Successful response to set key pair info with key pair id 4
* Expected Behavior: get a RETURN_SUCCESS return code, and correct response message size and fields
**/
void libspdm_test_responder_set_key_pair_info_ack_case1(void **state)
{
libspdm_return_t status;
libspdm_test_context_t *spdm_test_context;
libspdm_context_t *spdm_context;
size_t response_size;
uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
spdm_set_key_pair_info_ack_response_t *spdm_response;

uint8_t key_pair_id;
size_t set_key_pair_info_request_size;
spdm_set_key_pair_info_request_t *set_key_pair_info_request;
uint8_t *ptr;
uint16_t desired_key_usage;
uint32_t desired_asym_algo;
uint8_t desired_assoc_cert_slot_mask;

set_key_pair_info_request = malloc(sizeof(spdm_set_key_pair_info_request_t) +
sizeof(uint8_t) + sizeof(uint16_t) + sizeof(uint32_t) +
sizeof(uint8_t));

spdm_test_context = *state;
spdm_context = spdm_test_context->spdm_context;
spdm_test_context->case_id = 0x1;
spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 <<
SPDM_VERSION_NUMBER_SHIFT_BIT;
spdm_context->connection_info.connection_state =
LIBSPDM_CONNECTION_STATE_AUTHENTICATED;
spdm_context->connection_info.algorithm.base_asym_algo =
m_libspdm_use_asym_algo;
spdm_context->local_context.capability.flags |=
SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_SET_KEY_PAIR_INFO_CAP;
spdm_context->local_context.total_key_pairs = 16;
key_pair_id = 4;

response_size = sizeof(response);

/*change: remove an association with slot*/
set_key_pair_info_request_size =
sizeof(spdm_set_key_pair_info_request_t) +
sizeof(uint8_t) + sizeof(uint16_t) + sizeof(uint32_t) + sizeof(uint8_t);

libspdm_zero_mem(set_key_pair_info_request, set_key_pair_info_request_size);
set_key_pair_info_request->header.spdm_version = SPDM_MESSAGE_VERSION_13;
set_key_pair_info_request->header.request_response_code = SPDM_SET_KEY_PAIR_INFO;
set_key_pair_info_request->header.param1 = SPDM_SET_KEY_PAIR_INFO_CHANGE_OPERATION;
set_key_pair_info_request->header.param2 = 0;
set_key_pair_info_request->key_pair_id = key_pair_id;

status = libspdm_get_response_set_key_pair_info_ack(
spdm_context, set_key_pair_info_request_size,
set_key_pair_info_request, &response_size, response);
assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
assert_int_equal(response_size,
sizeof(spdm_set_key_pair_info_ack_response_t));
spdm_response = (void *)response;
assert_int_equal(spdm_response->header.request_response_code,
SPDM_SET_KEY_PAIR_INFO_ACK);

/*erase: erase the keyusage and asymalgo*/
set_key_pair_info_request->header.param1 = SPDM_SET_KEY_PAIR_INFO_ERASE_OPERATION;
set_key_pair_info_request_size =
sizeof(spdm_set_key_pair_info_request_t);
status = libspdm_get_response_set_key_pair_info_ack(
spdm_context, set_key_pair_info_request_size,
set_key_pair_info_request, &response_size, response);
assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
assert_int_equal(response_size,
sizeof(spdm_set_key_pair_info_ack_response_t));
spdm_response = (void *)response;
assert_int_equal(spdm_response->header.request_response_code,
SPDM_SET_KEY_PAIR_INFO_ACK);

/*generate: generate a new key pair*/
desired_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
desired_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_ECC256;
desired_assoc_cert_slot_mask = 0x08;
set_key_pair_info_request_size =
sizeof(spdm_set_key_pair_info_request_t) +
sizeof(uint8_t) + sizeof(uint16_t) + sizeof(uint32_t) + sizeof(uint8_t);

libspdm_zero_mem(set_key_pair_info_request, set_key_pair_info_request_size);
set_key_pair_info_request->header.spdm_version = SPDM_MESSAGE_VERSION_13;
set_key_pair_info_request->header.request_response_code = SPDM_SET_KEY_PAIR_INFO;
set_key_pair_info_request->header.param1 = SPDM_SET_KEY_PAIR_INFO_CHANGE_OPERATION;
set_key_pair_info_request->header.param2 = 0;
set_key_pair_info_request->key_pair_id = key_pair_id;

ptr = (uint8_t*)(set_key_pair_info_request + 1);
ptr += sizeof(uint8_t);

libspdm_write_uint16(ptr, desired_key_usage);
ptr += sizeof(uint16_t);

libspdm_write_uint32(ptr, desired_asym_algo);
ptr += sizeof(uint32_t);

*ptr = desired_assoc_cert_slot_mask;

status = libspdm_get_response_set_key_pair_info_ack(
spdm_context, set_key_pair_info_request_size,
set_key_pair_info_request, &response_size, response);
assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
assert_int_equal(response_size,
sizeof(spdm_set_key_pair_info_ack_response_t));
spdm_response = (void *)response;
assert_int_equal(spdm_response->header.request_response_code,
SPDM_SET_KEY_PAIR_INFO_ACK);
free(set_key_pair_info_request);
}

/**
* Test 2: Successful response to set key pair info with key pair id 4: need reset
* Expected Behavior: get a RETURN_SUCCESS return code, and correct response message size and fields
**/
void libspdm_test_responder_set_key_pair_info_ack_case2(void **state)
{
libspdm_return_t status;
libspdm_test_context_t *spdm_test_context;
libspdm_context_t *spdm_context;
size_t response_size;
uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
spdm_set_key_pair_info_ack_response_t *spdm_response;

uint8_t key_pair_id;
size_t set_key_pair_info_request_size;
spdm_set_key_pair_info_request_t *set_key_pair_info_request;
uint8_t *ptr;
uint16_t desired_key_usage;
uint32_t desired_asym_algo;
uint8_t desired_assoc_cert_slot_mask;

set_key_pair_info_request = malloc(sizeof(spdm_set_key_pair_info_request_t) +
sizeof(uint8_t) + sizeof(uint16_t) + sizeof(uint32_t) +
sizeof(uint8_t));

spdm_test_context = *state;
spdm_context = spdm_test_context->spdm_context;
spdm_test_context->case_id = 0x2;
spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 <<
SPDM_VERSION_NUMBER_SHIFT_BIT;
spdm_context->connection_info.connection_state =
LIBSPDM_CONNECTION_STATE_AUTHENTICATED;
spdm_context->connection_info.algorithm.base_asym_algo =
m_libspdm_use_asym_algo;
spdm_context->local_context.capability.flags |=
SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_SET_KEY_PAIR_INFO_CAP;
spdm_context->local_context.total_key_pairs = 16;
key_pair_id = 4;

/*set responder need reset*/
spdm_context->local_context.capability.flags |=
SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_INSTALL_RESET_CAP;

response_size = sizeof(response);

/*Before reset, change: remove an association with slot*/
set_key_pair_info_request_size =
sizeof(spdm_set_key_pair_info_request_t) +
sizeof(uint8_t) + sizeof(uint16_t) + sizeof(uint32_t) + sizeof(uint8_t);

libspdm_zero_mem(set_key_pair_info_request, set_key_pair_info_request_size);
set_key_pair_info_request->header.spdm_version = SPDM_MESSAGE_VERSION_13;
set_key_pair_info_request->header.request_response_code = SPDM_SET_KEY_PAIR_INFO;
set_key_pair_info_request->header.param1 = SPDM_SET_KEY_PAIR_INFO_CHANGE_OPERATION;
set_key_pair_info_request->header.param2 = 0;
set_key_pair_info_request->key_pair_id = key_pair_id;

status = libspdm_get_response_set_key_pair_info_ack(
spdm_context, set_key_pair_info_request_size,
set_key_pair_info_request, &response_size, response);
assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
assert_int_equal(response_size, sizeof(spdm_error_response_t));
spdm_response = (void *)response;
assert_int_equal(spdm_response->header.request_response_code,
SPDM_ERROR);
assert_int_equal(spdm_response->header.param1,
SPDM_ERROR_CODE_RESET_REQUIRED);
assert_int_equal(spdm_response->header.param2, 0);

/*After reset, change: remove an association with slot*/
status = libspdm_get_response_set_key_pair_info_ack(
spdm_context, set_key_pair_info_request_size,
set_key_pair_info_request, &response_size, response);
assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
assert_int_equal(response_size,
sizeof(spdm_set_key_pair_info_ack_response_t));
spdm_response = (void *)response;
assert_int_equal(spdm_response->header.request_response_code,
SPDM_SET_KEY_PAIR_INFO_ACK);

/*Before reset, erase: erase the keyusage and asymalgo*/
set_key_pair_info_request->header.param1 = SPDM_SET_KEY_PAIR_INFO_ERASE_OPERATION;
set_key_pair_info_request_size =
sizeof(spdm_set_key_pair_info_request_t);
status = libspdm_get_response_set_key_pair_info_ack(
spdm_context, set_key_pair_info_request_size,
set_key_pair_info_request, &response_size, response);
assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
assert_int_equal(response_size, sizeof(spdm_error_response_t));
spdm_response = (void *)response;
assert_int_equal(spdm_response->header.request_response_code,
SPDM_ERROR);
assert_int_equal(spdm_response->header.param1,
SPDM_ERROR_CODE_RESET_REQUIRED);
assert_int_equal(spdm_response->header.param2, 0);

/*After reset, erase: erase the keyusage and asymalgo*/
status = libspdm_get_response_set_key_pair_info_ack(
spdm_context, set_key_pair_info_request_size,
set_key_pair_info_request, &response_size, response);
assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
assert_int_equal(response_size,
sizeof(spdm_set_key_pair_info_ack_response_t));
spdm_response = (void *)response;
assert_int_equal(spdm_response->header.request_response_code,
SPDM_SET_KEY_PAIR_INFO_ACK);


/*Before reset, generate: generate a new key pair*/
desired_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
desired_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_ECC256;
desired_assoc_cert_slot_mask = 0x08;
set_key_pair_info_request_size =
sizeof(spdm_set_key_pair_info_request_t) +
sizeof(uint8_t) + sizeof(uint16_t) + sizeof(uint32_t) + sizeof(uint8_t);

libspdm_zero_mem(set_key_pair_info_request, set_key_pair_info_request_size);
set_key_pair_info_request->header.spdm_version = SPDM_MESSAGE_VERSION_13;
set_key_pair_info_request->header.request_response_code = SPDM_SET_KEY_PAIR_INFO;
set_key_pair_info_request->header.param1 = SPDM_SET_KEY_PAIR_INFO_CHANGE_OPERATION;
set_key_pair_info_request->header.param2 = 0;
set_key_pair_info_request->key_pair_id = key_pair_id;

ptr = (uint8_t*)(set_key_pair_info_request + 1);
ptr += sizeof(uint8_t);

libspdm_write_uint16(ptr, desired_key_usage);
ptr += sizeof(uint16_t);

libspdm_write_uint32(ptr, desired_asym_algo);
ptr += sizeof(uint32_t);

*ptr = desired_assoc_cert_slot_mask;

status = libspdm_get_response_set_key_pair_info_ack(
spdm_context, set_key_pair_info_request_size,
set_key_pair_info_request, &response_size, response);
assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
assert_int_equal(response_size, sizeof(spdm_error_response_t));
spdm_response = (void *)response;
assert_int_equal(spdm_response->header.request_response_code,
SPDM_ERROR);
assert_int_equal(spdm_response->header.param1,
SPDM_ERROR_CODE_RESET_REQUIRED);
assert_int_equal(spdm_response->header.param2, 0);

/*After reset, generate: generate a new key pair*/
status = libspdm_get_response_set_key_pair_info_ack(
spdm_context, set_key_pair_info_request_size,
set_key_pair_info_request, &response_size, response);
assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
assert_int_equal(response_size,
sizeof(spdm_set_key_pair_info_ack_response_t));
spdm_response = (void *)response;
assert_int_equal(spdm_response->header.request_response_code,
SPDM_SET_KEY_PAIR_INFO_ACK);
free(set_key_pair_info_request);
}

libspdm_test_context_t m_libspdm_responder_set_key_pair_info_ack_test_context = {
LIBSPDM_TEST_CONTEXT_VERSION,
false,
};

int libspdm_responder_set_key_pair_info_ack_test_main(void)
{
const struct CMUnitTest spdm_responder_set_key_pair_info_ack_tests[] = {
/* Success Case to set key pair info*/
cmocka_unit_test(libspdm_test_responder_set_key_pair_info_ack_case1),
/* Success Case to set key pair info with reset*/
cmocka_unit_test(libspdm_test_responder_set_key_pair_info_ack_case2),
};

libspdm_setup_test_context(&m_libspdm_responder_set_key_pair_info_ack_test_context);

return cmocka_run_group_tests(spdm_responder_set_key_pair_info_ack_tests,
libspdm_unit_test_group_setup,
libspdm_unit_test_group_teardown);
}

#endif /* LIBSPDM_ENABLE_CAPABILITY_SET_KEY_PAIR_INFO_CAP*/
10 changes: 10 additions & 0 deletions unit_test/test_spdm_responder/test_spdm_responder.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ int libspdm_responder_measurement_extension_log_test_main(void);
int libspdm_responder_key_pair_info_test_main(void);
#endif /* LIBSPDM_ENABLE_CAPABILITY_GET_KEY_PAIR_INFO_CAP*/

#if LIBSPDM_ENABLE_CAPABILITY_SET_KEY_PAIR_INFO_CAP
int libspdm_responder_set_key_pair_info_ack_test_main(void);
#endif /* LIBSPDM_ENABLE_CAPABILITY_SET_KEY_PAIR_INFO_CAP*/

#if LIBSPDM_RESPOND_IF_READY_SUPPORT
#if (LIBSPDM_ENABLE_CAPABILITY_CERT_CAP || LIBSPDM_ENABLE_CAPABILITY_CHAL_CAP || \
LIBSPDM_ENABLE_CAPABILITY_MEAS_CAP || LIBSPDM_ENABLE_CAPABILITY_KEY_EX_CAP || \
Expand Down Expand Up @@ -139,6 +143,12 @@ int main(void)
}
#endif /* LIBSPDM_ENABLE_CAPABILITY_GET_KEY_PAIR_INFO_CAP*/

#if LIBSPDM_ENABLE_CAPABILITY_SET_KEY_PAIR_INFO_CAP
if (libspdm_responder_set_key_pair_info_ack_test_main() != 0) {
return_value = 1;
}
#endif /* LIBSPDM_ENABLE_CAPABILITY_SET_KEY_PAIR_INFO_CAP*/

#if LIBSPDM_RESPOND_IF_READY_SUPPORT
#if (LIBSPDM_ENABLE_CAPABILITY_CERT_CAP || LIBSPDM_ENABLE_CAPABILITY_CHAL_CAP || \
LIBSPDM_ENABLE_CAPABILITY_MEAS_CAP || LIBSPDM_ENABLE_CAPABILITY_KEY_EX_CAP || \
Expand Down

0 comments on commit 40febd1

Please sign in to comment.