Line data Source code
1 : /**
2 : * Copyright Notice:
3 : * Copyright 2024-2025 DMTF. All rights reserved.
4 : * License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/libspdm/blob/main/LICENSE.md
5 : **/
6 :
7 : #include "spdm_unit_test.h"
8 : #include "internal/libspdm_responder_lib.h"
9 : #include "internal/libspdm_requester_lib.h"
10 :
11 : #if LIBSPDM_ENABLE_CAPABILITY_GET_KEY_PAIR_INFO_CAP
12 :
13 : spdm_get_key_pair_info_request_t m_libspdm_get_key_pair_info_request1 = {
14 : { SPDM_MESSAGE_VERSION_13, SPDM_GET_KEY_PAIR_INFO, 0, 0 },
15 : 4
16 : };
17 : size_t m_libspdm_get_key_pair_info_request1_size = sizeof(m_libspdm_get_key_pair_info_request1);
18 :
19 : /**
20 : * Test 1: Successful response to get key pair info with key pair id 4
21 : * Expected Behavior: get a LIBSPDM_STATUS_SUCCESS return code, and correct response message size and fields
22 : **/
23 1 : static void rsp_key_pair_info_case1(void **state)
24 : {
25 : libspdm_return_t status;
26 : libspdm_test_context_t *spdm_test_context;
27 : libspdm_context_t *spdm_context;
28 : size_t response_size;
29 : uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
30 : spdm_key_pair_info_response_t *spdm_response;
31 : uint8_t key_pair_id;
32 : uint16_t public_key_info_len;
33 1 : uint8_t public_key_info_ecp256[] = {0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D,
34 : 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D,
35 : 0x03, 0x01, 0x07};
36 :
37 1 : spdm_test_context = *state;
38 1 : spdm_context = spdm_test_context->spdm_context;
39 1 : spdm_test_context->case_id = 0x1;
40 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 <<
41 : SPDM_VERSION_NUMBER_SHIFT_BIT;
42 1 : spdm_context->connection_info.connection_state =
43 : LIBSPDM_CONNECTION_STATE_AUTHENTICATED;
44 1 : spdm_context->connection_info.algorithm.base_asym_algo =
45 : m_libspdm_use_asym_algo;
46 1 : spdm_context->local_context.capability.flags |=
47 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_GET_KEY_PAIR_INFO_CAP;
48 :
49 1 : key_pair_id = 4;
50 1 : public_key_info_len = sizeof(public_key_info_ecp256);
51 :
52 1 : response_size = sizeof(response);
53 :
54 1 : status = libspdm_get_response_key_pair_info(
55 : spdm_context, m_libspdm_get_key_pair_info_request1_size,
56 : &m_libspdm_get_key_pair_info_request1, &response_size, response);
57 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
58 1 : assert_int_equal(response_size,
59 : sizeof(spdm_key_pair_info_response_t) + public_key_info_len);
60 1 : spdm_response = (void *)response;
61 1 : assert_int_equal(spdm_response->header.request_response_code,
62 : SPDM_KEY_PAIR_INFO);
63 1 : assert_int_equal(spdm_response->key_pair_id,
64 : key_pair_id);
65 1 : }
66 :
67 : /**
68 : * Test 2:
69 : * An SPDM endpoint shall contain KeyPairID s starting from 1 to TotalKeyPairs inclusive and without gaps.
70 : * KeyPairID is set to 0.
71 : * Expected Behavior: Generate error response message
72 : **/
73 1 : static void rsp_key_pair_info_case2(void **state)
74 : {
75 : libspdm_return_t status;
76 : libspdm_test_context_t *spdm_test_context;
77 : libspdm_context_t *spdm_context;
78 : size_t response_size;
79 : uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
80 : spdm_key_pair_info_response_t *spdm_response;
81 : uint8_t key_pair_id;
82 :
83 1 : spdm_test_context = *state;
84 1 : spdm_context = spdm_test_context->spdm_context;
85 1 : spdm_test_context->case_id = 0x2;
86 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 <<
87 : SPDM_VERSION_NUMBER_SHIFT_BIT;
88 1 : spdm_context->connection_info.connection_state =
89 : LIBSPDM_CONNECTION_STATE_AUTHENTICATED;
90 1 : spdm_context->local_context.capability.flags |=
91 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_GET_KEY_PAIR_INFO_CAP;
92 :
93 : /* An SPDM endpoint shall contain KeyPairID s starting from 1 to TotalKeyPairs inclusive and without gaps.
94 : * KeyPairID is set to 0.*/
95 1 : key_pair_id = 0;
96 1 : m_libspdm_get_key_pair_info_request1.key_pair_id = key_pair_id;
97 :
98 1 : response_size = sizeof(response);
99 :
100 1 : status = libspdm_get_response_key_pair_info(
101 : spdm_context, m_libspdm_get_key_pair_info_request1_size,
102 : &m_libspdm_get_key_pair_info_request1, &response_size, response);
103 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
104 1 : spdm_response = (void *)response;
105 1 : assert_int_equal(spdm_response->header.request_response_code,
106 : SPDM_ERROR);
107 1 : assert_int_equal(spdm_response->header.param1, SPDM_ERROR_CODE_INVALID_REQUEST);
108 1 : assert_int_equal(spdm_response->header.param2, 0);
109 1 : }
110 :
111 : /**
112 : * Test 3: The key_pair_id is greater than the total key pairs
113 : * Expected Behavior: Generate error response message
114 : **/
115 1 : static void rsp_key_pair_info_case3(void **state)
116 : {
117 : libspdm_return_t status;
118 : libspdm_test_context_t *spdm_test_context;
119 : libspdm_context_t *spdm_context;
120 : size_t response_size;
121 : uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
122 : spdm_key_pair_info_response_t *spdm_response;
123 : uint8_t key_pair_id;
124 :
125 1 : spdm_test_context = *state;
126 1 : spdm_context = spdm_test_context->spdm_context;
127 1 : spdm_test_context->case_id = 0x3;
128 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 <<
129 : SPDM_VERSION_NUMBER_SHIFT_BIT;
130 1 : spdm_context->connection_info.connection_state =
131 : LIBSPDM_CONNECTION_STATE_AUTHENTICATED;
132 1 : spdm_context->local_context.capability.flags |=
133 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_GET_KEY_PAIR_INFO_CAP;
134 :
135 : /* key_pair_id > total_key_pairs*/
136 1 : key_pair_id = libspdm_read_total_key_pairs(spdm_context) + 1;
137 1 : m_libspdm_get_key_pair_info_request1.key_pair_id = key_pair_id;
138 :
139 1 : response_size = sizeof(response);
140 :
141 1 : status = libspdm_get_response_key_pair_info(
142 : spdm_context, m_libspdm_get_key_pair_info_request1_size,
143 : &m_libspdm_get_key_pair_info_request1, &response_size, response);
144 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
145 1 : spdm_response = (void *)response;
146 1 : assert_int_equal(spdm_response->header.request_response_code,
147 : SPDM_ERROR);
148 1 : assert_int_equal(spdm_response->header.param1, SPDM_ERROR_CODE_INVALID_REQUEST);
149 1 : assert_int_equal(spdm_response->header.param2, 0);
150 1 : }
151 :
152 : /**
153 : * Test 4: not set KEY_PAIR_INFO
154 : * Expected Behavior: Generate error response message
155 : **/
156 1 : static void rsp_key_pair_info_case4(void **state)
157 : {
158 : libspdm_return_t status;
159 : libspdm_test_context_t *spdm_test_context;
160 : libspdm_context_t *spdm_context;
161 : size_t response_size;
162 : uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
163 : spdm_key_pair_info_response_t *spdm_response;
164 : uint8_t key_pair_id;
165 :
166 1 : spdm_test_context = *state;
167 1 : spdm_context = spdm_test_context->spdm_context;
168 1 : spdm_test_context->case_id = 0x4;
169 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 <<
170 : SPDM_VERSION_NUMBER_SHIFT_BIT;
171 1 : spdm_context->connection_info.connection_state =
172 : LIBSPDM_CONNECTION_STATE_AUTHENTICATED;
173 : /* not set KEY_PAIR_INFO*/
174 1 : spdm_context->local_context.capability.flags &=
175 : ~SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_GET_KEY_PAIR_INFO_CAP;
176 :
177 1 : key_pair_id = 1;
178 1 : m_libspdm_get_key_pair_info_request1.key_pair_id = key_pair_id;
179 :
180 1 : response_size = sizeof(response);
181 :
182 1 : status = libspdm_get_response_key_pair_info(
183 : spdm_context, m_libspdm_get_key_pair_info_request1_size,
184 : &m_libspdm_get_key_pair_info_request1, &response_size, response);
185 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
186 1 : spdm_response = (void *)response;
187 1 : assert_int_equal(spdm_response->header.request_response_code,
188 : SPDM_ERROR);
189 1 : assert_int_equal(spdm_response->header.param1, SPDM_ERROR_CODE_UNSUPPORTED_REQUEST);
190 1 : assert_int_equal(spdm_response->header.param2, SPDM_GET_KEY_PAIR_INFO);
191 1 : }
192 :
193 1 : int libspdm_rsp_key_pair_info_test(void)
194 : {
195 1 : const struct CMUnitTest test_cases[] = {
196 : /* Success Case to get key pair info*/
197 : cmocka_unit_test(rsp_key_pair_info_case1),
198 : /* The KeyPairID is at least 1 , KeyPairID is set to 0.*/
199 : cmocka_unit_test(rsp_key_pair_info_case2),
200 : /* KeyPairID > total_key_pairs*/
201 : cmocka_unit_test(rsp_key_pair_info_case3),
202 : /* capability not set KEY_PAIR_INFO*/
203 : cmocka_unit_test(rsp_key_pair_info_case4),
204 : };
205 :
206 1 : libspdm_test_context_t test_context = {
207 : LIBSPDM_TEST_CONTEXT_VERSION,
208 : false,
209 : };
210 1 : libspdm_setup_test_context(&test_context);
211 :
212 1 : return cmocka_run_group_tests(test_cases,
213 : libspdm_unit_test_group_setup,
214 : libspdm_unit_test_group_teardown);
215 : }
216 :
217 : #endif /* LIBSPDM_ENABLE_CAPABILITY_GET_KEY_PAIR_INFO_CAP*/
|