diff --git a/CMakeLists.txt b/CMakeLists.txt index 5a88b173ab9..ec6519b617f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1019,6 +1019,8 @@ else() add_subdirectory(unit_test/fuzzing/test_responder/test_spdm_responder_vendor_cmds) add_subdirectory(unit_test/fuzzing/test_requester/test_spdm_requester_get_measurement_extension_log) add_subdirectory(unit_test/fuzzing/test_responder/test_spdm_responder_measurement_extension_log) + add_subdirectory(unit_test/fuzzing/test_requester/test_spdm_requester_get_key_pair_info) + add_subdirectory(unit_test/fuzzing/test_responder/test_spdm_responder_key_pair_info) endif() add_subdirectory(os_stub/cryptlib_null) diff --git a/unit_test/fuzzing/fuzzing_AFL.sh b/unit_test/fuzzing/fuzzing_AFL.sh index 8eaa54c082a..86b30cc2dde 100755 --- a/unit_test/fuzzing/fuzzing_AFL.sh +++ b/unit_test/fuzzing/fuzzing_AFL.sh @@ -161,6 +161,8 @@ test_spdm_requester_vendor_cmds test_spdm_responder_vendor_cmds test_spdm_responder_measurement_extension_log test_spdm_requester_get_measurement_extension_log +test_spdm_requester_get_key_pair_info +test_spdm_responder_key_pair_info ) for ((i=0;i<${#cmds[*]};i++)) do diff --git a/unit_test/fuzzing/fuzzing_AFLTurbo.sh b/unit_test/fuzzing/fuzzing_AFLTurbo.sh index a3ce8bde364..19a02d6e662 100755 --- a/unit_test/fuzzing/fuzzing_AFLTurbo.sh +++ b/unit_test/fuzzing/fuzzing_AFLTurbo.sh @@ -162,6 +162,8 @@ test_spdm_requester_vendor_cmds test_spdm_responder_vendor_cmds test_spdm_responder_measurement_extension_log test_spdm_requester_get_measurement_extension_log +test_spdm_requester_get_key_pair_info +test_spdm_responder_key_pair_info ) export FUZZ_START_TIME=`date +%Y-%m-%d_%H:%M:%S` diff --git a/unit_test/fuzzing/fuzzing_AFLplusplus.sh b/unit_test/fuzzing/fuzzing_AFLplusplus.sh index f954eabf036..f48f1493d7b 100644 --- a/unit_test/fuzzing/fuzzing_AFLplusplus.sh +++ b/unit_test/fuzzing/fuzzing_AFLplusplus.sh @@ -170,6 +170,8 @@ test_spdm_requester_vendor_cmds test_spdm_responder_vendor_cmds test_spdm_responder_measurement_extension_log test_spdm_requester_get_measurement_extension_log +test_spdm_requester_get_key_pair_info +test_spdm_responder_key_pair_info ) export FUZZ_START_TIME=`date +%Y-%m-%d_%H:%M:%S` diff --git a/unit_test/fuzzing/fuzzing_LibFuzzer.sh b/unit_test/fuzzing/fuzzing_LibFuzzer.sh index a48e82742c5..1be6e30c3b3 100755 --- a/unit_test/fuzzing/fuzzing_LibFuzzer.sh +++ b/unit_test/fuzzing/fuzzing_LibFuzzer.sh @@ -132,6 +132,8 @@ test_spdm_requester_vendor_cmds test_spdm_responder_vendor_cmds test_spdm_responder_measurement_extension_log test_spdm_requester_get_measurement_extension_log +test_spdm_requester_get_key_pair_info +test_spdm_responder_key_pair_info ) object_parameters=() cp -r $fuzzing_seeds ./ diff --git a/unit_test/fuzzing/oss_fuzz.sh b/unit_test/fuzzing/oss_fuzz.sh index b93264cf3d2..3ace18548b9 100755 --- a/unit_test/fuzzing/oss_fuzz.sh +++ b/unit_test/fuzzing/oss_fuzz.sh @@ -116,6 +116,8 @@ test_spdm_requester_vendor_cmds test_spdm_responder_vendor_cmds test_spdm_responder_measurement_extension_log test_spdm_requester_get_measurement_extension_log +test_spdm_requester_get_key_pair_info +test_spdm_responder_key_pair_info ) for ((i=0;i<${#cmds[*]};i++)) diff --git a/unit_test/fuzzing/run_initial_seed.sh b/unit_test/fuzzing/run_initial_seed.sh index 9f3926414a6..8c69b5497ac 100755 --- a/unit_test/fuzzing/run_initial_seed.sh +++ b/unit_test/fuzzing/run_initial_seed.sh @@ -53,6 +53,8 @@ test_spdm_requester_vendor_cmds test_spdm_responder_vendor_cmds test_spdm_responder_measurement_extension_log test_spdm_requester_get_measurement_extension_log +test_spdm_requester_get_key_pair_info +test_spdm_responder_key_pair_info ) flag=0 diff --git a/unit_test/fuzzing/seeds/test_spdm_requester_get_key_pair_info/key_pair_info.raw b/unit_test/fuzzing/seeds/test_spdm_requester_get_key_pair_info/key_pair_info.raw new file mode 100644 index 00000000000..a3af05932a5 Binary files /dev/null and b/unit_test/fuzzing/seeds/test_spdm_requester_get_key_pair_info/key_pair_info.raw differ diff --git a/unit_test/fuzzing/seeds/test_spdm_responder_key_pair_info/key_pair_info.raw b/unit_test/fuzzing/seeds/test_spdm_responder_key_pair_info/key_pair_info.raw new file mode 100644 index 00000000000..a3af05932a5 Binary files /dev/null and b/unit_test/fuzzing/seeds/test_spdm_responder_key_pair_info/key_pair_info.raw differ diff --git a/unit_test/fuzzing/test_requester/test_spdm_requester_get_key_pair_info/CMakeLists.txt b/unit_test/fuzzing/test_requester/test_spdm_requester_get_key_pair_info/CMakeLists.txt new file mode 100644 index 00000000000..cc825987946 --- /dev/null +++ b/unit_test/fuzzing/test_requester/test_spdm_requester_get_key_pair_info/CMakeLists.txt @@ -0,0 +1,65 @@ +cmake_minimum_required(VERSION 3.5) + +add_executable(test_spdm_requester_get_key_pair_info) + +target_include_directories(test_spdm_requester_get_key_pair_info + PRIVATE + ${LIBSPDM_DIR}/unit_test/fuzzing/test_requester/test_spdm_requester_get_key_pair_info + ${LIBSPDM_DIR}/include + ${LIBSPDM_DIR}/unit_test/include + ${LIBSPDM_DIR}/unit_test/fuzzing/spdm_unit_fuzzing_common + ${LIBSPDM_DIR}/os_stub/include + ${LIBSPDM_DIR}/os_stub +) + +if(TOOLCHAIN STREQUAL "KLEE") + target_include_directories(test_spdm_requester_get_key_pair_info + PRIVATE + $ENV{KLEE_SRC_PATH}/include + ) +endif() + +target_sources(test_spdm_requester_get_key_pair_info + PRIVATE + get_key_pair_info.c + ${PROJECT_SOURCE_DIR}/unit_test/fuzzing/spdm_unit_fuzzing_common/common.c + ${PROJECT_SOURCE_DIR}/unit_test/fuzzing/spdm_unit_fuzzing_common/toolchain_harness.c + ${PROJECT_SOURCE_DIR}/unit_test/fuzzing/spdm_unit_fuzzing_common/algo.c +) + +if((TOOLCHAIN STREQUAL "KLEE") OR (TOOLCHAIN STREQUAL "CBMC")) + target_link_libraries(test_spdm_requester_get_key_pair_info + PRIVATE + $ + $ + $ + $ + $ + $ + $ + $ + $ + $ + $ + $ + $ + ) +else() + target_link_libraries(test_spdm_requester_get_key_pair_info + PRIVATE + memlib + debuglib + spdm_requester_lib + spdm_common_lib + ${CRYPTO_LIB_PATHS} + rnglib + platform_lib_null + cryptlib_${CRYPTO} + malloclib + spdm_crypt_lib + spdm_crypt_ext_lib + spdm_secured_message_lib + spdm_transport_test_lib + spdm_device_secret_lib_null + ) +endif() diff --git a/unit_test/fuzzing/test_requester/test_spdm_requester_get_key_pair_info/get_key_pair_info.c b/unit_test/fuzzing/test_requester/test_spdm_requester_get_key_pair_info/get_key_pair_info.c new file mode 100644 index 00000000000..73045cc4dc5 --- /dev/null +++ b/unit_test/fuzzing/test_requester/test_spdm_requester_get_key_pair_info/get_key_pair_info.c @@ -0,0 +1,138 @@ +/** + * 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_fuzzing.h" +#include "toolchain_harness.h" +#include "internal/libspdm_requester_lib.h" + +#if LIBSPDM_ENABLE_CAPABILITY_GET_KEY_PAIR_INFO_CAP + +#define LIBSPDM_MAX_key_pair_info_SIZE 0x1000 + +uint8_t temp_buf[LIBSPDM_MAX_key_pair_info_SIZE]; + +size_t libspdm_get_max_buffer_size(void) +{ + return LIBSPDM_MAX_SPDM_MSG_SIZE; +} + +libspdm_return_t libspdm_device_send_message(void *spdm_context, + size_t request_size, const void *request, + uint64_t timeout) +{ + return LIBSPDM_STATUS_SUCCESS; +} + +libspdm_return_t libspdm_device_receive_message(void *spdm_context, + size_t *response_size, + void **response, + uint64_t timeout) +{ + libspdm_test_context_t *spdm_test_context; + uint8_t *spdm_response; + size_t spdm_response_size; + size_t test_message_header_size; + + spdm_test_context = libspdm_get_test_context(); + test_message_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE; + libspdm_zero_mem(temp_buf, sizeof(temp_buf)); + spdm_response = (void *)((uint8_t *)temp_buf + test_message_header_size); + spdm_response_size = spdm_test_context->test_buffer_size; + if (spdm_response_size > sizeof(temp_buf) - test_message_header_size - LIBSPDM_TEST_ALIGNMENT) { + spdm_response_size = sizeof(temp_buf) - test_message_header_size - LIBSPDM_TEST_ALIGNMENT; + } + libspdm_copy_mem((uint8_t *)temp_buf + test_message_header_size, + sizeof(temp_buf) - test_message_header_size, + spdm_test_context->test_buffer, + spdm_response_size); + + libspdm_transport_test_encode_message(spdm_context, NULL, false, false, + spdm_response_size, + spdm_response, response_size, response); + + return LIBSPDM_STATUS_SUCCESS; +} + +void libspdm_test_requester_get_key_pair_info(void **State) +{ + libspdm_test_context_t *spdm_test_context; + libspdm_context_t *spdm_context; + + uint8_t key_pair_id; + uint8_t associated_slot_id; + uint8_t total_key_pairs; + uint16_t capabilities; + uint16_t key_usage_capabilities; + uint16_t current_key_usage; + uint32_t asym_algo_capabilities; + uint32_t current_asym_algo; + uint16_t public_key_info_len; + uint8_t assoc_cert_slot_mask; + uint8_t public_key_info[SPDM_MAX_PUBLIC_KEY_INFO_LEN]; + + spdm_test_context = *State; + spdm_context = spdm_test_context->spdm_context; + spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 << + SPDM_VERSION_NUMBER_SHIFT_BIT; + + spdm_context->connection_info.connection_state = + LIBSPDM_CONNECTION_STATE_NEGOTIATED; + spdm_context->connection_info.capability.flags |= + SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_GET_KEY_PAIR_INFO_CAP; + + public_key_info_len = SPDM_MAX_PUBLIC_KEY_INFO_LEN; + key_pair_id = 1; + spdm_key_pair_info_response_t *spdm_response; + + /* In order to improve the fuzz efficiency, make the key_pair_id equal to spdm_response->key_pair_id.*/ + if(spdm_test_context->test_buffer_size > sizeof(spdm_key_pair_info_response_t)) { + spdm_response = (spdm_key_pair_info_response_t *)spdm_test_context->test_buffer; + + key_pair_id = spdm_response->key_pair_id; + } + + associated_slot_id = 1; + spdm_context->connection_info.peer_key_pair_id[associated_slot_id] = key_pair_id; + + libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs, + &capabilities, &key_usage_capabilities, ¤t_key_usage, + &asym_algo_capabilities, ¤t_asym_algo, + &assoc_cert_slot_mask, &public_key_info_len, + public_key_info); +} + +libspdm_test_context_t m_libspdm_requester_get_key_pair_info_test_context = { + LIBSPDM_TEST_CONTEXT_VERSION, + true, + libspdm_device_send_message, + libspdm_device_receive_message, +}; + + +void libspdm_run_test_harness(void *test_buffer, size_t test_buffer_size) +{ + void *State; + + libspdm_setup_test_context(&m_libspdm_requester_get_key_pair_info_test_context); + + m_libspdm_requester_get_key_pair_info_test_context.test_buffer = test_buffer; + m_libspdm_requester_get_key_pair_info_test_context.test_buffer_size = + test_buffer_size; + + libspdm_unit_test_group_setup(&State); + libspdm_test_requester_get_key_pair_info(&State); + libspdm_unit_test_group_teardown(&State); +} +#else +size_t libspdm_get_max_buffer_size(void) +{ + return 0; +} + +void libspdm_run_test_harness(void *test_buffer, size_t test_buffer_size) { + +} +#endif /* LIBSPDM_ENABLE_CAPABILITY_GET_KEY_PAIR_INFO_CAP*/ diff --git a/unit_test/fuzzing/test_responder/test_spdm_responder_key_pair_info/CMakeLists.txt b/unit_test/fuzzing/test_responder/test_spdm_responder_key_pair_info/CMakeLists.txt new file mode 100644 index 00000000000..27ce4cf4ebc --- /dev/null +++ b/unit_test/fuzzing/test_responder/test_spdm_responder_key_pair_info/CMakeLists.txt @@ -0,0 +1,66 @@ +cmake_minimum_required(VERSION 3.5) + +add_executable(test_spdm_responder_key_pair_info) + +target_include_directories(test_spdm_responder_key_pair_info + PRIVATE + ${LIBSPDM_DIR}/unit_test/fuzzing/test_responder/test_spdm_responder_key_pair_info + ${LIBSPDM_DIR}/include + ${LIBSPDM_DIR}/unit_test/include + ${LIBSPDM_DIR}/unit_test/fuzzing/spdm_unit_fuzzing_common + ${LIBSPDM_DIR}/os_stub/spdm_device_secret_lib_sample + ${LIBSPDM_DIR}/os_stub/include + ${LIBSPDM_DIR}/os_stub +) + +if(TOOLCHAIN STREQUAL "KLEE") + target_include_directories(test_spdm_responder_key_pair_info + PRIVATE + $ENV{KLEE_SRC_PATH}/include + ) +endif() + +target_sources(test_spdm_responder_key_pair_info + PRIVATE + key_pair_info.c + ${PROJECT_SOURCE_DIR}/unit_test/fuzzing/spdm_unit_fuzzing_common/common.c + ${PROJECT_SOURCE_DIR}/unit_test/fuzzing/spdm_unit_fuzzing_common/toolchain_harness.c + ${PROJECT_SOURCE_DIR}/unit_test/fuzzing/spdm_unit_fuzzing_common/algo.c + ) + +if((TOOLCHAIN STREQUAL "KLEE") OR (TOOLCHAIN STREQUAL "CBMC")) + target_link_libraries(test_spdm_responder_key_pair_info + PRIVATE + $ + $ + $ + $ + $ + $ + $ + $ + $ + $ + $ + $ + $ + ) +else() + target_link_libraries(test_spdm_responder_key_pair_info + PRIVATE + memlib + debuglib + spdm_responder_lib + spdm_common_lib + ${CRYPTO_LIB_PATHS} + rnglib + cryptlib_${CRYPTO} + malloclib + spdm_crypt_lib + spdm_crypt_ext_lib + spdm_secured_message_lib + spdm_transport_test_lib + platform_lib_null + spdm_device_secret_lib_sample + ) +endif() diff --git a/unit_test/fuzzing/test_responder/test_spdm_responder_key_pair_info/key_pair_info.c b/unit_test/fuzzing/test_responder/test_spdm_responder_key_pair_info/key_pair_info.c new file mode 100644 index 00000000000..7a2fdcceeb7 --- /dev/null +++ b/unit_test/fuzzing/test_responder/test_spdm_responder_key_pair_info/key_pair_info.c @@ -0,0 +1,77 @@ +/** + * 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_fuzzing.h" +#include "toolchain_harness.h" +#include "internal/libspdm_responder_lib.h" + +#if LIBSPDM_ENABLE_CAPABILITY_GET_KEY_PAIR_INFO_CAP + +size_t libspdm_get_max_buffer_size(void) +{ + return LIBSPDM_MAX_SPDM_MSG_SIZE; +} + +void libspdm_test_responder_key_pair_info(void **State) +{ + 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_test_context = *State; + spdm_context = spdm_test_context->spdm_context; + + 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->local_context.capability.flags |= + SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_GET_KEY_PAIR_INFO_CAP; + + response_size = sizeof(response); + libspdm_get_response_key_pair_info(spdm_context, + spdm_test_context->test_buffer_size, + spdm_test_context->test_buffer, + &response_size, response); +} + +libspdm_test_context_t m_libspdm_responder_key_pair_info_test_context = { + LIBSPDM_TEST_CONTEXT_VERSION, + false, +}; + +void libspdm_run_test_harness(void *test_buffer, size_t test_buffer_size) +{ + void *State; + spdm_message_header_t *spdm_request_header; + libspdm_setup_test_context(&m_libspdm_responder_key_pair_info_test_context); + + spdm_request_header = (spdm_message_header_t*)test_buffer; + + if (spdm_request_header->request_response_code != SPDM_GET_KEY_PAIR_INFO) { + spdm_request_header->request_response_code = SPDM_GET_KEY_PAIR_INFO; + } + + m_libspdm_responder_key_pair_info_test_context.test_buffer = test_buffer; + m_libspdm_responder_key_pair_info_test_context.test_buffer_size = + test_buffer_size; + + /* Success Case*/ + libspdm_unit_test_group_setup(&State); + libspdm_test_responder_key_pair_info(&State); + libspdm_unit_test_group_teardown(&State); +} +#else +size_t libspdm_get_max_buffer_size(void) +{ + return 0; +} + +void libspdm_run_test_harness(void *test_buffer, size_t test_buffer_size) { + +} +#endif /* LIBSPDM_ENABLE_CAPABILITY_GET_KEY_PAIR_INFO_CAP*/ diff --git a/unit_test/test_spdm_requester/get_key_pair_info.c b/unit_test/test_spdm_requester/get_key_pair_info.c index 8ef37a569a5..df0f4dfb887 100644 --- a/unit_test/test_spdm_requester/get_key_pair_info.c +++ b/unit_test/test_spdm_requester/get_key_pair_info.c @@ -10,6 +10,15 @@ #if LIBSPDM_ENABLE_CAPABILITY_GET_KEY_PAIR_INFO_CAP +uint8_t m_response_total_key_pairs; +uint8_t m_response_key_pair_id; +uint16_t m_response_capabilities; +uint16_t m_response_key_usage_capabilities; +uint16_t m_response_current_key_usage; +uint32_t m_response_asym_algo_capabilities; +uint32_t m_response_current_asym_algo; +uint8_t m_response_assoc_cert_slot_mask; + libspdm_return_t libspdm_requester_get_key_pair_info_test_send_message( void *spdm_context, size_t request_size, const void *request, uint64_t timeout) @@ -19,6 +28,7 @@ libspdm_return_t libspdm_requester_get_key_pair_info_test_send_message( spdm_test_context = libspdm_get_test_context(); switch (spdm_test_context->case_id) { case 0x1: + case 0x2: return LIBSPDM_STATUS_SUCCESS; default: return LIBSPDM_STATUS_SEND_FAIL; @@ -91,6 +101,46 @@ libspdm_return_t libspdm_requester_get_key_pair_info_test_receive_message( response); } return LIBSPDM_STATUS_SUCCESS; + + case 0x2: { + spdm_key_pair_info_response_t *spdm_response; + size_t spdm_response_size; + size_t transport_header_size; + + uint16_t public_key_info_len; + uint8_t public_key_info_rsa2048[] = {0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, + 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00}; + + public_key_info_len = (uint16_t)sizeof(public_key_info_rsa2048); + + transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE; + spdm_response = (void *)((uint8_t *)*response + transport_header_size); + spdm_response_size = sizeof(spdm_key_pair_info_response_t) + public_key_info_len; + + spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_13; + spdm_response->header.request_response_code = SPDM_KEY_PAIR_INFO; + spdm_response->header.param1 = 0; + spdm_response->header.param2 = 0; + spdm_response->total_key_pairs = m_response_total_key_pairs; + spdm_response->key_pair_id = m_response_key_pair_id; + spdm_response->capabilities = m_response_capabilities; + spdm_response->key_usage_capabilities = m_response_key_usage_capabilities; + spdm_response->current_key_usage = m_response_current_key_usage; + spdm_response->asym_algo_capabilities = m_response_asym_algo_capabilities; + spdm_response->current_asym_algo = m_response_current_asym_algo; + spdm_response->public_key_info_len = public_key_info_len; + spdm_response->assoc_cert_slot_mask = m_response_assoc_cert_slot_mask; + + + libspdm_copy_mem((void*)(spdm_response + 1), public_key_info_len, + public_key_info_rsa2048, public_key_info_len); + libspdm_transport_test_encode_message(spdm_context, NULL, false, + false, spdm_response_size, + spdm_response, response_size, + response); + } + return LIBSPDM_STATUS_SUCCESS; + default: return LIBSPDM_STATUS_RECEIVE_FAIL; } @@ -149,12 +199,384 @@ void libspdm_test_requester_get_key_pair_info_case1(void **state) assert_int_equal(current_asym_algo, SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048); } -libspdm_test_context_t m_libspdm_requester_get_key_pair_info_test_context = { - LIBSPDM_TEST_CONTEXT_VERSION, - true, - libspdm_requester_get_key_pair_info_test_send_message, - libspdm_requester_get_key_pair_info_test_receive_message, -}; +/** + * Test 2: The collection of multiple sub-cases. + **/ +void libspdm_test_requester_get_key_pair_info_case3(void **state) +{ + libspdm_return_t status; + libspdm_test_context_t *spdm_test_context; + libspdm_context_t *spdm_context; + + uint8_t key_pair_id; + uint8_t associated_slot_id; + uint8_t total_key_pairs; + uint16_t capabilities; + uint16_t key_usage_capabilities; + uint16_t current_key_usage; + uint32_t asym_algo_capabilities; + uint32_t current_asym_algo; + uint16_t public_key_info_len; + uint8_t assoc_cert_slot_mask; + uint8_t public_key_info[SPDM_MAX_PUBLIC_KEY_INFO_LEN]; + + spdm_test_context = *state; + spdm_context = spdm_test_context->spdm_context; + spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 << + SPDM_VERSION_NUMBER_SHIFT_BIT; + + spdm_context->connection_info.connection_state = + LIBSPDM_CONNECTION_STATE_NEGOTIATED; + spdm_context->connection_info.capability.flags |= + SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_GET_KEY_PAIR_INFO_CAP; + + spdm_test_context->case_id = 0x2; + public_key_info_len = SPDM_MAX_PUBLIC_KEY_INFO_LEN; + + /* Sub Case 1: key_pair_id > total_key_pairs*/ + key_pair_id = 2; + associated_slot_id = 1; + spdm_context->connection_info.peer_key_pair_id[associated_slot_id] = key_pair_id; + + m_response_total_key_pairs = 1; + m_response_key_pair_id = 2; + m_response_capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP; + m_response_key_usage_capabilities =SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE; + m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE; + m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048; + m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048; + /* association with slot 1*/ + m_response_assoc_cert_slot_mask = 0x2; + + status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs, + &capabilities, &key_usage_capabilities, ¤t_key_usage, + &asym_algo_capabilities, ¤t_asym_algo, + &assoc_cert_slot_mask, &public_key_info_len, + public_key_info); + + assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD); + + /* Sub Case 2: responder and requester, the KeyPairID are not consistent.*/ + key_pair_id = 0xA0; + associated_slot_id = 1; + spdm_context->connection_info.peer_key_pair_id[associated_slot_id] = key_pair_id; + + m_response_total_key_pairs = 1; + m_response_key_pair_id = 1; + m_response_capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP; + m_response_key_usage_capabilities =SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE; + m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE; + m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048; + m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048; + /* association with slot 1*/ + m_response_assoc_cert_slot_mask = 0x2; + + status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs, + &capabilities, &key_usage_capabilities, ¤t_key_usage, + &asym_algo_capabilities, ¤t_asym_algo, + &assoc_cert_slot_mask, &public_key_info_len, + public_key_info); + + assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD); + + /* Sub Case 3: not set capabilities.*/ + key_pair_id = 1; + associated_slot_id = 1; + spdm_context->connection_info.peer_key_pair_id[associated_slot_id] = key_pair_id; + + m_response_total_key_pairs = 1; + m_response_key_pair_id = 1; + m_response_capabilities = 0; + m_response_key_usage_capabilities =SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE; + m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE; + m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048; + m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048; + /* association with slot 1*/ + m_response_assoc_cert_slot_mask = 0x2; + + status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs, + &capabilities, &key_usage_capabilities, ¤t_key_usage, + &asym_algo_capabilities, ¤t_asym_algo, + &assoc_cert_slot_mask, &public_key_info_len, + public_key_info); + + assert_int_equal(status, LIBSPDM_STATUS_SUCCESS); + assert_int_equal(capabilities, 0); + assert_int_equal(key_usage_capabilities, SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE); + assert_int_equal(current_key_usage, SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE); + assert_int_equal(asym_algo_capabilities, SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048); + assert_int_equal(current_asym_algo, SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048); + + /* Sub Case 4: This bit shall not be set if CertAssocCap is not set. + * Set the ShareableCap bit and not set the CertAssocCap bit.*/ + key_pair_id = 1; + associated_slot_id = 1; + spdm_context->connection_info.peer_key_pair_id[associated_slot_id] = key_pair_id; + + m_response_total_key_pairs = 1; + m_response_key_pair_id = 1; + + m_response_capabilities = 0; + m_response_capabilities |= SPDM_KEY_PAIR_CAP_SHAREABLE_CAP; + m_response_capabilities &= ~SPDM_KEY_PAIR_CAP_CERT_ASSOC_CAP; + + m_response_key_usage_capabilities =SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE; + m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE; + m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048; + m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048; + /* association with slot 1*/ + m_response_assoc_cert_slot_mask = 0x2; + + status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs, + &capabilities, &key_usage_capabilities, ¤t_key_usage, + &asym_algo_capabilities, ¤t_asym_algo, + &assoc_cert_slot_mask, &public_key_info_len, + public_key_info); + + assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD); + + /* Sub Case 5: KeyUsageCapabilities at least one bit shall be set, not set KeyUsageCapabilities.*/ + m_response_total_key_pairs = 1; + m_response_key_pair_id = 1; + m_response_capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP; + m_response_key_usage_capabilities =0; + m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE; + m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048; + m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048; + /* association with slot 1*/ + m_response_assoc_cert_slot_mask = 0x2; + + status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs, + &capabilities, &key_usage_capabilities, ¤t_key_usage, + &asym_algo_capabilities, ¤t_asym_algo, + &assoc_cert_slot_mask, &public_key_info_len, + public_key_info); + + assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD); + + /* Sub Case 6: KeyUsageCapabilities Set multiple bits.*/ + m_response_total_key_pairs = 1; + m_response_key_pair_id = 1; + m_response_capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP; + m_response_key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE | + SPDM_KEY_USAGE_BIT_MASK_CHALLENGE_USE | + SPDM_KEY_USAGE_BIT_MASK_MEASUREMENT_USE | + SPDM_KEY_USAGE_BIT_MASK_ENDPOINT_INFO_USE | + SPDM_KEY_USAGE_BIT_MASK_STANDARDS_KEY_USE | + SPDM_KEY_USAGE_BIT_MASK_VENDOR_KEY_USE; + m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE; + m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048; + m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048; + /* association with slot 1*/ + m_response_assoc_cert_slot_mask = 0x2; + + status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs, + &capabilities, &key_usage_capabilities, ¤t_key_usage, + &asym_algo_capabilities, ¤t_asym_algo, + &assoc_cert_slot_mask, &public_key_info_len, + public_key_info); + + assert_int_equal(status, LIBSPDM_STATUS_SUCCESS); + assert_int_equal(capabilities, SPDM_KEY_PAIR_CAP_GEN_KEY_CAP); + assert_int_equal(key_usage_capabilities, SPDM_KEY_USAGE_BIT_MASK); + assert_int_equal(current_key_usage, SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE); + assert_int_equal(asym_algo_capabilities, SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048); + assert_int_equal(current_asym_algo, SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048); + + /* Sub Case 7: not set CurrentKeyUsage*/ + m_response_total_key_pairs = 1; + m_response_key_pair_id = 1; + m_response_capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP; + m_response_key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE; + m_response_current_key_usage = 0; + m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048; + m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048; + /* association with slot 1*/ + m_response_assoc_cert_slot_mask = 0x2; + + status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs, + &capabilities, &key_usage_capabilities, ¤t_key_usage, + &asym_algo_capabilities, ¤t_asym_algo, + &assoc_cert_slot_mask, &public_key_info_len, + public_key_info); + assert_int_equal(status, LIBSPDM_STATUS_SUCCESS); + assert_int_equal(capabilities, SPDM_KEY_PAIR_CAP_GEN_KEY_CAP); + assert_int_equal(key_usage_capabilities, SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE); + assert_int_equal(current_key_usage, 0); + assert_int_equal(asym_algo_capabilities, SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048); + assert_int_equal(current_asym_algo, SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048); + + /* Sub Case 8: CurrentKeyUsage Set multiple bits.*/ + m_response_total_key_pairs = 1; + m_response_key_pair_id = 1; + m_response_capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP; + m_response_key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE; + m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE | + SPDM_KEY_USAGE_BIT_MASK_CHALLENGE_USE | + SPDM_KEY_USAGE_BIT_MASK_MEASUREMENT_USE | + SPDM_KEY_USAGE_BIT_MASK_ENDPOINT_INFO_USE | + SPDM_KEY_USAGE_BIT_MASK_STANDARDS_KEY_USE | + SPDM_KEY_USAGE_BIT_MASK_VENDOR_KEY_USE; + m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048; + m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048; + /* association with slot 1*/ + m_response_assoc_cert_slot_mask = 0x2; + + status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs, + &capabilities, &key_usage_capabilities, ¤t_key_usage, + &asym_algo_capabilities, ¤t_asym_algo, + &assoc_cert_slot_mask, &public_key_info_len, + public_key_info); + assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD); + + /* Sub Case 9: CurrentKeyUsage and KeyUsageCapabilities, Set multiple bits.*/ + m_response_total_key_pairs = 1; + m_response_key_pair_id = 1; + m_response_capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP; + m_response_key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE | + SPDM_KEY_USAGE_BIT_MASK_CHALLENGE_USE; + m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE | + SPDM_KEY_USAGE_BIT_MASK_CHALLENGE_USE; + m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048; + m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048; + /* association with slot 1*/ + m_response_assoc_cert_slot_mask = 0x2; + + status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs, + &capabilities, &key_usage_capabilities, ¤t_key_usage, + &asym_algo_capabilities, ¤t_asym_algo, + &assoc_cert_slot_mask, &public_key_info_len, + public_key_info); + assert_int_equal(status, LIBSPDM_STATUS_SUCCESS); + assert_int_equal(capabilities, SPDM_KEY_PAIR_CAP_GEN_KEY_CAP); + assert_int_equal(key_usage_capabilities, SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE | + SPDM_KEY_USAGE_BIT_MASK_CHALLENGE_USE); + assert_int_equal(current_key_usage, SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE | + SPDM_KEY_USAGE_BIT_MASK_CHALLENGE_USE); + assert_int_equal(asym_algo_capabilities, SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048); + assert_int_equal(current_asym_algo, SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048); + + /* Sub Case 10: CurrentKeyUsage and KeyUsageCapabilities are not consistent.*/ + m_response_total_key_pairs = 1; + m_response_key_pair_id = 1; + m_response_capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP; + m_response_key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_CHALLENGE_USE; + m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE; + m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048; + m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048; + /* association with slot 1*/ + m_response_assoc_cert_slot_mask = 0x2; + + status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs, + &capabilities, &key_usage_capabilities, ¤t_key_usage, + &asym_algo_capabilities, ¤t_asym_algo, + &assoc_cert_slot_mask, &public_key_info_len, + public_key_info); + assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD); + + /* Sub Case 11: AsymAlgoCapabilities, at least one bit shall be set.not set AsymAlgoCapabilities*/ + m_response_total_key_pairs = 1; + m_response_key_pair_id = 1; + m_response_capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP; + m_response_key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE; + m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE; + m_response_asym_algo_capabilities = 0; + m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048; + /* association with slot 1*/ + m_response_assoc_cert_slot_mask = 0x2; + + status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs, + &capabilities, &key_usage_capabilities, ¤t_key_usage, + &asym_algo_capabilities, ¤t_asym_algo, + &assoc_cert_slot_mask, &public_key_info_len, + public_key_info); + + assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD); + + /* Sub Case 12: AsymAlgoCapabilities Set multiple bits.*/ + m_response_total_key_pairs = 1; + m_response_key_pair_id = 1; + m_response_capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP; + m_response_key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE; + m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE; + m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048 | + SPDM_KEY_PAIR_ASYM_ALGO_CAP_ED448; + m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048; + /* association with slot 1*/ + m_response_assoc_cert_slot_mask = 0x2; + + status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs, + &capabilities, &key_usage_capabilities, ¤t_key_usage, + &asym_algo_capabilities, ¤t_asym_algo, + &assoc_cert_slot_mask, &public_key_info_len, + public_key_info); + + assert_int_equal(status, LIBSPDM_STATUS_SUCCESS); + assert_int_equal(capabilities, SPDM_KEY_PAIR_CAP_GEN_KEY_CAP); + assert_int_equal(key_usage_capabilities, SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE); + assert_int_equal(current_key_usage, SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE); + assert_int_equal(asym_algo_capabilities, SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048 | + SPDM_KEY_PAIR_ASYM_ALGO_CAP_ED448); + assert_int_equal(current_asym_algo, SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048); + + /* Sub Case 13: CurrentAsymAlgo Set bits more than one bit.*/ + m_response_total_key_pairs = 1; + m_response_key_pair_id = 1; + m_response_capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP; + m_response_key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE; + m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE; + m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048; + m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048 | + SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA3072; + /* association with slot 1*/ + m_response_assoc_cert_slot_mask = 0x2; + + status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs, + &capabilities, &key_usage_capabilities, ¤t_key_usage, + &asym_algo_capabilities, ¤t_asym_algo, + &assoc_cert_slot_mask, &public_key_info_len, + public_key_info); + + assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD); + + /* Sub Case 14: AsymAlgoCapabilities and AsymAlgoCapabilities are not consistent.*/ + m_response_total_key_pairs = 1; + m_response_key_pair_id = 1; + m_response_capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP; + m_response_key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE; + m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE; + m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA3072; + m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048; + /* association with slot 1*/ + m_response_assoc_cert_slot_mask = 0x2; + + status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs, + &capabilities, &key_usage_capabilities, ¤t_key_usage, + &asym_algo_capabilities, ¤t_asym_algo, + &assoc_cert_slot_mask, &public_key_info_len, + public_key_info); + + assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD); + + /* Sub Case 15: AssocCertSlotMask set more than one bit, but ShareableCap is not set.*/ + m_response_total_key_pairs = 1; + m_response_key_pair_id = 1; + m_response_capabilities = 0; + m_response_key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE; + m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE; + m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048; + m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048; + /* association with slot 1*/ + m_response_assoc_cert_slot_mask = 0xFF; + + status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs, + &capabilities, &key_usage_capabilities, ¤t_key_usage, + &asym_algo_capabilities, ¤t_asym_algo, + &assoc_cert_slot_mask, &public_key_info_len, + public_key_info); + + assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD); +} int libspdm_requester_get_key_pair_info_test_main(void) { @@ -163,8 +585,14 @@ int libspdm_requester_get_key_pair_info_test_main(void) cmocka_unit_test(libspdm_test_requester_get_key_pair_info_case1), }; - libspdm_setup_test_context( - &m_libspdm_requester_get_key_pair_info_test_context); + libspdm_test_context_t test_context = { + LIBSPDM_TEST_CONTEXT_VERSION, + true, + libspdm_requester_get_key_pair_info_test_send_message, + libspdm_requester_get_key_pair_info_test_receive_message, + }; + + libspdm_setup_test_context(&test_context); return cmocka_run_group_tests(spdm_requester_get_key_pair_info_tests, libspdm_unit_test_group_setup, diff --git a/unit_test/test_spdm_responder/key_pair_info.c b/unit_test/test_spdm_responder/key_pair_info.c index 9a54264f012..ddbfdb2a0ef 100644 --- a/unit_test/test_spdm_responder/key_pair_info.c +++ b/unit_test/test_spdm_responder/key_pair_info.c @@ -65,19 +65,155 @@ void libspdm_test_responder_key_pair_info_case1(void **state) key_pair_id); } -libspdm_test_context_t m_libspdm_responder_key_pair_info_test_context = { - LIBSPDM_TEST_CONTEXT_VERSION, - false, -}; +/** + * Test 2: + * An SPDM endpoint shall contain KeyPairID s starting from 1 to TotalKeyPairs inclusive and without gaps. + * KeyPairID is set to 0. + * Expected Behavior: Generate error response message + **/ +void libspdm_test_responder_key_pair_info_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_key_pair_info_response_t *spdm_response; + uint8_t key_pair_id; + + 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->local_context.capability.flags |= + SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_GET_KEY_PAIR_INFO_CAP; + + /* An SPDM endpoint shall contain KeyPairID s starting from 1 to TotalKeyPairs inclusive and without gaps. + * KeyPairID is set to 0.*/ + key_pair_id = 0; + m_libspdm_get_key_pair_info_request1.key_pair_id = key_pair_id; + + spdm_context->local_context.total_key_pairs = 1; + + response_size = sizeof(response); + + status = libspdm_get_response_key_pair_info( + spdm_context, m_libspdm_get_key_pair_info_request1_size, + &m_libspdm_get_key_pair_info_request1, &response_size, response); + assert_int_equal(status, LIBSPDM_STATUS_SUCCESS); + 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_INVALID_REQUEST); + assert_int_equal(spdm_response->header.param2, 0); +} + +/** + * Test 3: The key_pair_id is greater than the total key pairs + * Expected Behavior: Generate error response message + **/ +void libspdm_test_responder_key_pair_info_case3(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_key_pair_info_response_t *spdm_response; + uint8_t key_pair_id; + + spdm_test_context = *state; + spdm_context = spdm_test_context->spdm_context; + spdm_test_context->case_id = 0x3; + 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->local_context.capability.flags |= + SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_GET_KEY_PAIR_INFO_CAP; + + /* key_pair_id > total_key_pairs*/ + key_pair_id = 2; + m_libspdm_get_key_pair_info_request1.key_pair_id = key_pair_id; + spdm_context->local_context.total_key_pairs = 1; + + response_size = sizeof(response); + + status = libspdm_get_response_key_pair_info( + spdm_context, m_libspdm_get_key_pair_info_request1_size, + &m_libspdm_get_key_pair_info_request1, &response_size, response); + assert_int_equal(status, LIBSPDM_STATUS_SUCCESS); + 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_INVALID_REQUEST); + assert_int_equal(spdm_response->header.param2, 0); +} + +/** + * Test 4: not set KEY_PAIR_INFO + * Expected Behavior: Generate error response message + **/ +void libspdm_test_responder_key_pair_info_case4(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_key_pair_info_response_t *spdm_response; + uint8_t key_pair_id; + + spdm_test_context = *state; + spdm_context = spdm_test_context->spdm_context; + spdm_test_context->case_id = 0x4; + spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 << + SPDM_VERSION_NUMBER_SHIFT_BIT; + spdm_context->connection_info.connection_state = + LIBSPDM_CONNECTION_STATE_AUTHENTICATED; + /* not set KEY_PAIR_INFO*/ + spdm_context->local_context.capability.flags &= + ~SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_GET_KEY_PAIR_INFO_CAP; + + key_pair_id = 1; + m_libspdm_get_key_pair_info_request1.key_pair_id = key_pair_id; + + spdm_context->local_context.total_key_pairs = key_pair_id; + + response_size = sizeof(response); + + status = libspdm_get_response_key_pair_info( + spdm_context, m_libspdm_get_key_pair_info_request1_size, + &m_libspdm_get_key_pair_info_request1, &response_size, response); + assert_int_equal(status, LIBSPDM_STATUS_SUCCESS); + 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_UNSUPPORTED_REQUEST); + assert_int_equal(spdm_response->header.param2, SPDM_GET_KEY_PAIR_INFO); +} int libspdm_responder_key_pair_info_test_main(void) { const struct CMUnitTest spdm_responder_key_pair_info_tests[] = { /* Success Case to get key pair info*/ cmocka_unit_test(libspdm_test_responder_key_pair_info_case1), + /* The KeyPairID is at least 1 , KeyPairID is set to 0.*/ + cmocka_unit_test(libspdm_test_responder_key_pair_info_case2), + /* KeyPairID > total_key_pairs*/ + cmocka_unit_test(libspdm_test_responder_key_pair_info_case3), + /* capability not set KEY_PAIR_INFO*/ + cmocka_unit_test(libspdm_test_responder_key_pair_info_case4), }; - libspdm_setup_test_context(&m_libspdm_responder_key_pair_info_test_context); + libspdm_test_context_t test_context = { + LIBSPDM_TEST_CONTEXT_VERSION, + false, + }; + libspdm_setup_test_context(&test_context); return cmocka_run_group_tests(spdm_responder_key_pair_info_tests, libspdm_unit_test_group_setup,