Line data Source code
1 : /**
2 : * Copyright Notice:
3 : * Copyright 2024-2026 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_requester_lib.h"
9 : #include "internal/libspdm_secured_message_lib.h"
10 :
11 : #if LIBSPDM_ENABLE_CAPABILITY_SET_KEY_PAIR_INFO_CAP
12 :
13 4 : static libspdm_return_t send_message(
14 : void *spdm_context, size_t request_size, const void *request, uint64_t timeout)
15 : {
16 : libspdm_test_context_t *spdm_test_context;
17 :
18 4 : spdm_test_context = libspdm_get_test_context();
19 4 : switch (spdm_test_context->case_id) {
20 4 : case 0x1:
21 : case 0x2:
22 : case 0x3:
23 : case 0x4:
24 4 : return LIBSPDM_STATUS_SUCCESS;
25 0 : default:
26 0 : return LIBSPDM_STATUS_SEND_FAIL;
27 : }
28 : }
29 :
30 4 : static libspdm_return_t receive_message(
31 : void *spdm_context, size_t *response_size, void **response, uint64_t timeout)
32 : {
33 : libspdm_test_context_t *spdm_test_context;
34 :
35 4 : spdm_test_context = libspdm_get_test_context();
36 4 : switch (spdm_test_context->case_id) {
37 :
38 1 : case 0x1: {
39 : spdm_key_pair_info_response_t *spdm_response;
40 : size_t spdm_response_size;
41 : size_t transport_header_size;
42 :
43 1 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
44 1 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
45 1 : spdm_response_size = sizeof(spdm_key_pair_info_response_t);
46 :
47 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_13;
48 1 : spdm_response->header.request_response_code = SPDM_SET_KEY_PAIR_INFO_ACK;
49 1 : spdm_response->header.param1 = 0;
50 1 : spdm_response->header.param2 = 0;
51 :
52 1 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
53 : false, spdm_response_size,
54 : spdm_response, response_size,
55 : response);
56 : }
57 1 : return LIBSPDM_STATUS_SUCCESS;
58 1 : case 0x2: {
59 : spdm_key_pair_info_response_t *spdm_response;
60 : size_t spdm_response_size;
61 : size_t transport_header_size;
62 :
63 1 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
64 1 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
65 1 : spdm_response_size = sizeof(spdm_key_pair_info_response_t);
66 :
67 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_12;
68 1 : spdm_response->header.request_response_code = SPDM_SET_KEY_PAIR_INFO_ACK;
69 1 : spdm_response->header.param1 = 0;
70 1 : spdm_response->header.param2 = 0;
71 :
72 1 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
73 : false, spdm_response_size,
74 : spdm_response, response_size,
75 : response);
76 : }
77 1 : return LIBSPDM_STATUS_SUCCESS;
78 :
79 1 : case 0x3: {
80 : spdm_key_pair_info_response_t *spdm_response;
81 : size_t spdm_response_size;
82 : size_t transport_header_size;
83 :
84 1 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
85 1 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
86 1 : spdm_response_size = sizeof(spdm_key_pair_info_response_t);
87 :
88 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_13;
89 1 : spdm_response->header.request_response_code = SPDM_KEY_PAIR_INFO;
90 1 : spdm_response->header.param1 = 0;
91 1 : spdm_response->header.param2 = 0;
92 :
93 1 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
94 : false, spdm_response_size,
95 : spdm_response, response_size,
96 : response);
97 : }
98 1 : return LIBSPDM_STATUS_SUCCESS;
99 1 : case 0x4: {
100 : spdm_error_response_t *spdm_response;
101 : size_t spdm_response_size;
102 : size_t transport_header_size;
103 :
104 1 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
105 1 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
106 1 : spdm_response_size = sizeof(spdm_error_response_t);
107 :
108 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_14;
109 1 : spdm_response->header.request_response_code = SPDM_ERROR;
110 1 : spdm_response->header.param1 = SPDM_ERROR_CODE_RESET_REQUIRED;
111 1 : spdm_response->header.param2 = 0;
112 :
113 1 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
114 : false, spdm_response_size,
115 : spdm_response, response_size,
116 : response);
117 : }
118 1 : return LIBSPDM_STATUS_SUCCESS;
119 0 : default:
120 0 : return LIBSPDM_STATUS_RECEIVE_FAIL;
121 : }
122 : }
123 :
124 : /**
125 : * Test 1: Successful response to set key pair info
126 : * Expected Behavior: get a LIBSPDM_STATUS_SUCCESS return code
127 : **/
128 1 : static void req_set_key_pair_info_case1(void **state)
129 : {
130 : libspdm_return_t status;
131 : libspdm_test_context_t *spdm_test_context;
132 : libspdm_context_t *spdm_context;
133 :
134 : uint8_t key_pair_id;
135 : uint8_t operation;
136 : uint16_t desired_key_usage;
137 : uint32_t desired_asym_algo;
138 : uint8_t desired_assoc_cert_slot_mask;
139 :
140 1 : spdm_test_context = *state;
141 1 : spdm_context = spdm_test_context->spdm_context;
142 1 : spdm_test_context->case_id = 0x1;
143 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 <<
144 : SPDM_VERSION_NUMBER_SHIFT_BIT;
145 :
146 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
147 1 : spdm_context->connection_info.capability.flags |=
148 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_SET_KEY_PAIR_INFO_CAP;
149 :
150 1 : key_pair_id = 1;
151 1 : operation = SPDM_SET_KEY_PAIR_INFO_ERASE_OPERATION;
152 1 : desired_key_usage = 0;
153 1 : desired_asym_algo = 0;
154 1 : desired_assoc_cert_slot_mask = 0;
155 1 : status = libspdm_set_key_pair_info(spdm_context, NULL, key_pair_id,
156 : operation, desired_key_usage, desired_asym_algo,
157 : desired_assoc_cert_slot_mask);
158 :
159 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
160 :
161 : /*wrong desired_assoc_cert_slot_mask when operation is erase*/
162 1 : desired_assoc_cert_slot_mask = 1;
163 1 : status = libspdm_set_key_pair_info(spdm_context, NULL, key_pair_id,
164 : operation, desired_key_usage, desired_asym_algo,
165 : desired_assoc_cert_slot_mask);
166 :
167 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_PARAMETER);
168 1 : }
169 :
170 : /**
171 : * Test 2: Fail case, The response version is incorrect .
172 : * Expected Behavior: returns a status of LIBSPDM_STATUS_INVALID_MSG_FIELD.
173 : **/
174 1 : static void req_set_key_pair_info_case2(void **state)
175 : {
176 : libspdm_return_t status;
177 : libspdm_test_context_t *spdm_test_context;
178 : libspdm_context_t *spdm_context;
179 :
180 : uint8_t key_pair_id;
181 : uint8_t operation;
182 : uint16_t desired_key_usage;
183 : uint32_t desired_asym_algo;
184 : uint8_t desired_assoc_cert_slot_mask;
185 :
186 1 : spdm_test_context = *state;
187 1 : spdm_context = spdm_test_context->spdm_context;
188 1 : spdm_test_context->case_id = 0x2;
189 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 <<
190 : SPDM_VERSION_NUMBER_SHIFT_BIT;
191 :
192 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
193 1 : spdm_context->connection_info.capability.flags |=
194 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_SET_KEY_PAIR_INFO_CAP;
195 :
196 1 : key_pair_id = 1;
197 1 : operation = SPDM_SET_KEY_PAIR_INFO_ERASE_OPERATION;
198 1 : desired_key_usage = 0;
199 1 : desired_asym_algo = 0;
200 1 : desired_assoc_cert_slot_mask = 0;
201 1 : status = libspdm_set_key_pair_info(spdm_context, NULL, key_pair_id,
202 : operation, desired_key_usage, desired_asym_algo,
203 : desired_assoc_cert_slot_mask);
204 :
205 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
206 1 : }
207 :
208 : /**
209 : * Test 3: Fail case, The response code is incorrect
210 : * Expected Behavior: returns a status of LIBSPDM_STATUS_INVALID_MSG_FIELD.
211 : **/
212 1 : static void req_set_key_pair_info_case3(void **state)
213 : {
214 : libspdm_return_t status;
215 : libspdm_test_context_t *spdm_test_context;
216 : libspdm_context_t *spdm_context;
217 :
218 : uint8_t key_pair_id;
219 : uint8_t operation;
220 : uint16_t desired_key_usage;
221 : uint32_t desired_asym_algo;
222 : uint8_t desired_assoc_cert_slot_mask;
223 :
224 1 : spdm_test_context = *state;
225 1 : spdm_context = spdm_test_context->spdm_context;
226 1 : spdm_test_context->case_id = 0x3;
227 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 <<
228 : SPDM_VERSION_NUMBER_SHIFT_BIT;
229 :
230 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
231 1 : spdm_context->connection_info.capability.flags |=
232 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_SET_KEY_PAIR_INFO_CAP;
233 :
234 1 : key_pair_id = 1;
235 1 : operation = SPDM_SET_KEY_PAIR_INFO_ERASE_OPERATION;
236 1 : desired_key_usage = 0;
237 1 : desired_asym_algo = 0;
238 1 : desired_assoc_cert_slot_mask = 0;
239 1 : status = libspdm_set_key_pair_info(spdm_context, NULL, key_pair_id,
240 : operation, desired_key_usage, desired_asym_algo,
241 : desired_assoc_cert_slot_mask);
242 :
243 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
244 1 : }
245 :
246 : /**
247 : * Test 4: Successful reset required error code
248 : * Expected Behavior: get a RESET_REQURED_PEER return code
249 : **/
250 1 : static void req_set_key_pair_info_case4(void **state)
251 : {
252 : libspdm_return_t status;
253 : libspdm_test_context_t *spdm_test_context;
254 : libspdm_context_t *spdm_context;
255 :
256 : uint8_t key_pair_id;
257 : uint8_t operation;
258 : uint16_t desired_key_usage;
259 : uint32_t desired_asym_algo;
260 : uint8_t desired_assoc_cert_slot_mask;
261 :
262 1 : spdm_test_context = *state;
263 1 : spdm_context = spdm_test_context->spdm_context;
264 1 : spdm_test_context->case_id = 0x4;
265 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_14 <<
266 : SPDM_VERSION_NUMBER_SHIFT_BIT;
267 :
268 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
269 1 : spdm_context->connection_info.capability.flags |=
270 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_SET_KEY_PAIR_INFO_CAP;
271 1 : spdm_context->connection_info.capability.flags |=
272 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_SET_KEY_PAIR_RESET_CAP;
273 :
274 1 : key_pair_id = 1;
275 1 : operation = SPDM_SET_KEY_PAIR_INFO_ERASE_OPERATION;
276 1 : desired_key_usage = 0;
277 1 : desired_asym_algo = 0;
278 1 : desired_assoc_cert_slot_mask = 0;
279 1 : status = libspdm_set_key_pair_info(spdm_context, NULL, key_pair_id,
280 : operation, desired_key_usage, desired_asym_algo,
281 : desired_assoc_cert_slot_mask);
282 :
283 1 : assert_int_equal(status, LIBSPDM_STATUS_RESET_REQUIRED_PEER);
284 1 : }
285 :
286 1 : int libspdm_req_set_key_pair_info_test(void)
287 : {
288 1 : const struct CMUnitTest test_cases[] = {
289 : /* Successful response to set key pair info, key_pair_id is 1*/
290 : cmocka_unit_test(req_set_key_pair_info_case1),
291 : /* The response version is incorrect */
292 : cmocka_unit_test(req_set_key_pair_info_case2),
293 : /* The response code is incorrect */
294 : cmocka_unit_test(req_set_key_pair_info_case3),
295 : /* Successful response with reset required error code */
296 : cmocka_unit_test(req_set_key_pair_info_case4),
297 : };
298 :
299 1 : libspdm_test_context_t test_context = {
300 : LIBSPDM_TEST_CONTEXT_VERSION,
301 : true,
302 : send_message,
303 : receive_message,
304 : };
305 :
306 1 : libspdm_setup_test_context(&test_context);
307 :
308 1 : return cmocka_run_group_tests(test_cases,
309 : libspdm_unit_test_group_setup,
310 : libspdm_unit_test_group_teardown);
311 : }
312 :
313 : #endif /*LIBSPDM_ENABLE_CAPABILITY_SET_KEY_PAIR_INFO_CAP*/
|