Line data Source code
1 : /**
2 : * Copyright Notice:
3 : * Copyright 2021-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_requester_lib.h"
9 : #include "internal/libspdm_responder_lib.h"
10 : #include "internal/libspdm_secured_message_lib.h"
11 :
12 : libspdm_return_t spdm_device_acquire_sender_buffer (
13 : void *context, void **msg_buf_ptr);
14 :
15 : void spdm_device_release_sender_buffer (void *context, const void *msg_buf_ptr);
16 :
17 : libspdm_return_t spdm_device_acquire_receiver_buffer (
18 : void *context, void **msg_buf_ptr);
19 :
20 : void spdm_device_release_receiver_buffer (void *context, const void *msg_buf_ptr);
21 :
22 : static uint32_t libspdm_opaque_data = 0xDEADBEEF;
23 :
24 : /**
25 : * This function verifies peer certificate chain buffer including spdm_cert_chain_t header.
26 : *
27 : * @param spdm_context A pointer to the SPDM context.
28 : * @param cert_chain_buffer Certificate chain buffer including spdm_cert_chain_t header.
29 : * @param cert_chain_buffer_size Size in bytes of the certificate chain buffer.
30 : * @param trust_anchor A buffer to hold the trust_anchor which is used to validate the
31 : * peer certificate, if not NULL.
32 : * @param trust_anchor_size A buffer to hold the trust_anchor_size, if not NULL.
33 : *
34 : * @retval true Peer certificate chain buffer verification passed.
35 : * @retval false Peer certificate chain buffer verification failed.
36 : **/
37 9 : static bool libspdm_verify_peer_cert_chain_buffer(void *spdm_context,
38 : const void *cert_chain_buffer,
39 : size_t cert_chain_buffer_size,
40 : const void **trust_anchor,
41 : size_t *trust_anchor_size)
42 : {
43 : bool result;
44 :
45 : /*verify peer cert chain integrity*/
46 9 : result = libspdm_verify_peer_cert_chain_buffer_integrity(spdm_context, cert_chain_buffer,
47 : cert_chain_buffer_size);
48 9 : if (!result) {
49 0 : return false;
50 : }
51 :
52 : /*verify peer cert chain authority*/
53 9 : result = libspdm_verify_peer_cert_chain_buffer_authority(spdm_context, cert_chain_buffer,
54 : cert_chain_buffer_size, trust_anchor,
55 : trust_anchor_size);
56 9 : if (!result) {
57 3 : return false;
58 : }
59 :
60 6 : return true;
61 : }
62 :
63 : /**
64 : * Return the size in bytes of multi element opaque data supported version.
65 : *
66 : * @param version_count Secure version count.
67 : *
68 : * @return the size in bytes of opaque data supported version.
69 : **/
70 38 : size_t libspdm_get_multi_element_opaque_data_supported_version_data_size(
71 : libspdm_context_t *spdm_context, uint8_t version_count, uint8_t element_num)
72 : {
73 : size_t size;
74 : uint8_t element_index;
75 :
76 38 : if (libspdm_get_connection_version (spdm_context) >= SPDM_MESSAGE_VERSION_12) {
77 19 : size = sizeof(spdm_general_opaque_data_table_header_t);
78 64 : for (element_index = 0; element_index < element_num; element_index++) {
79 45 : size += sizeof(secured_message_opaque_element_table_header_t) +
80 : sizeof(secured_message_opaque_element_supported_version_t) +
81 45 : sizeof(spdm_version_number_t) * version_count;
82 : /* Add Padding*/
83 45 : size = (size + 3) & ~3;
84 : }
85 : } else {
86 19 : size = sizeof(secured_message_general_opaque_data_table_header_t);
87 64 : for (element_index = 0; element_index < element_num; element_index++) {
88 45 : size += sizeof(secured_message_opaque_element_table_header_t) +
89 : sizeof(secured_message_opaque_element_supported_version_t) +
90 45 : sizeof(spdm_version_number_t) * version_count;
91 : /* Add Padding*/
92 45 : size = (size + 3) & ~3;
93 : }
94 : }
95 :
96 38 : return size;
97 : }
98 :
99 : /**
100 : * Build opaque data supported version test.
101 : *
102 : * @param data_out_size[in] size in bytes of the data_out.
103 : * On input, it means the size in bytes of data_out buffer.
104 : * On output, it means the size in bytes of copied data_out buffer if RETURN_SUCCESS is returned,
105 : * and means the size in bytes of desired data_out buffer if RETURN_BUFFER_TOO_SMALL is returned.
106 : * @param data_out[in] A pointer to the destination buffer to store the opaque data supported version.
107 : * @param element_num[in] in this test function, the element number < 9 is right. because element id is changed with element_index
108 : *
109 : * @retval RETURN_SUCCESS The opaque data supported version is built successfully.
110 : * @retval RETURN_BUFFER_TOO_SMALL The buffer is too small to hold the data.
111 : **/
112 : libspdm_return_t
113 4 : libspdm_build_multi_element_opaque_data_supported_version_test(libspdm_context_t *spdm_context,
114 : size_t *data_out_size,
115 : void *data_out,
116 : uint8_t element_num)
117 : {
118 : size_t final_data_size;
119 : secured_message_general_opaque_data_table_header_t
120 : *general_opaque_data_table_header;
121 : spdm_general_opaque_data_table_header_t
122 : *spdm_general_opaque_data_table_header;
123 : secured_message_opaque_element_table_header_t
124 : *opaque_element_table_header;
125 : secured_message_opaque_element_supported_version_t
126 : *opaque_element_support_version;
127 : spdm_version_number_t *versions_list;
128 : void *end;
129 : uint8_t element_index;
130 :
131 4 : if (spdm_context->local_context.secured_message_version
132 4 : .spdm_version_count == 0) {
133 0 : *data_out_size = 0;
134 0 : return LIBSPDM_STATUS_SUCCESS;
135 : }
136 :
137 : final_data_size =
138 4 : libspdm_get_multi_element_opaque_data_supported_version_data_size(
139 : spdm_context,
140 4 : spdm_context->local_context.secured_message_version.spdm_version_count,
141 : element_num);
142 4 : if (*data_out_size < final_data_size) {
143 0 : *data_out_size = final_data_size;
144 0 : return LIBSPDM_STATUS_BUFFER_TOO_SMALL;
145 : }
146 :
147 4 : if (libspdm_get_connection_version (spdm_context) >= SPDM_MESSAGE_VERSION_12) {
148 2 : spdm_general_opaque_data_table_header = data_out;
149 2 : spdm_general_opaque_data_table_header->total_elements = element_num;
150 2 : libspdm_write_uint24(spdm_general_opaque_data_table_header->reserved, 0);
151 2 : opaque_element_table_header =
152 : (void *)(spdm_general_opaque_data_table_header + 1);
153 : } else {
154 2 : general_opaque_data_table_header = data_out;
155 2 : general_opaque_data_table_header->spec_id =
156 : SECURED_MESSAGE_OPAQUE_DATA_SPEC_ID;
157 2 : general_opaque_data_table_header->opaque_version =
158 : SECURED_MESSAGE_OPAQUE_VERSION;
159 2 : general_opaque_data_table_header->total_elements = element_num;
160 2 : general_opaque_data_table_header->reserved = 0;
161 2 : opaque_element_table_header =
162 : (void *)(general_opaque_data_table_header + 1);
163 : }
164 :
165 34 : for (element_index = 0; element_index < element_num; element_index++) {
166 : /*id is changed with element_index*/
167 30 : opaque_element_table_header->id = element_index;
168 30 : opaque_element_table_header->vendor_len = 0;
169 30 : opaque_element_table_header->opaque_element_data_len =
170 30 : sizeof(secured_message_opaque_element_supported_version_t) +
171 30 : sizeof(spdm_version_number_t) *
172 30 : spdm_context->local_context.secured_message_version.spdm_version_count;
173 :
174 30 : opaque_element_support_version =
175 : (void *)(opaque_element_table_header + 1);
176 30 : opaque_element_support_version->sm_data_version =
177 : SECURED_MESSAGE_OPAQUE_ELEMENT_SMDATA_DATA_VERSION;
178 30 : opaque_element_support_version->sm_data_id =
179 : SECURED_MESSAGE_OPAQUE_ELEMENT_SMDATA_ID_SUPPORTED_VERSION;
180 30 : opaque_element_support_version->version_count =
181 30 : spdm_context->local_context.secured_message_version.spdm_version_count;
182 :
183 30 : versions_list = (void *)(opaque_element_support_version + 1);
184 :
185 30 : libspdm_copy_mem(versions_list,
186 30 : *data_out_size - ((uint8_t*)versions_list - (uint8_t*)data_out),
187 30 : spdm_context->local_context.secured_message_version.spdm_version,
188 30 : spdm_context->local_context.secured_message_version.spdm_version_count *
189 : sizeof(spdm_version_number_t));
190 :
191 : /*move to next element*/
192 30 : if (libspdm_get_connection_version (spdm_context) >= SPDM_MESSAGE_VERSION_12) {
193 15 : opaque_element_table_header =
194 : (secured_message_opaque_element_table_header_t *)(
195 : (uint8_t *)opaque_element_table_header +
196 15 : libspdm_get_multi_element_opaque_data_supported_version_data_size(
197 : spdm_context,
198 15 : spdm_context->local_context.secured_message_version.spdm_version_count,
199 15 : 1) -
200 : sizeof(spdm_general_opaque_data_table_header_t));
201 : } else {
202 15 : opaque_element_table_header =
203 : (secured_message_opaque_element_table_header_t *)(
204 : (uint8_t *)opaque_element_table_header +
205 15 : libspdm_get_multi_element_opaque_data_supported_version_data_size(
206 : spdm_context,
207 15 : spdm_context->local_context.secured_message_version.spdm_version_count,
208 15 : 1) -
209 : sizeof(secured_message_general_opaque_data_table_header_t));
210 : }
211 :
212 : /* Zero Padding. *data_out_size does not need to be changed, because data is 0 padded */
213 30 : end = versions_list +
214 30 : spdm_context->local_context.secured_message_version.spdm_version_count;
215 30 : libspdm_zero_mem(end, (size_t)data_out + final_data_size - (size_t)end);
216 : }
217 :
218 4 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO,
219 : "successful build multi element opaque data supported version! \n"));
220 4 : return LIBSPDM_STATUS_SUCCESS;
221 : }
222 :
223 : /**
224 : * Return the size in bytes of multi element opaque data selection version.
225 : *
226 : * @param version_count Secure version count.
227 : *
228 : * @return the size in bytes of opaque data selection version.
229 : **/
230 8 : size_t libspdm_get_multi_element_opaque_data_version_selection_data_size(
231 : const libspdm_context_t *spdm_context, uint8_t element_num)
232 : {
233 : size_t size;
234 : uint8_t element_index;
235 :
236 8 : if (spdm_context->local_context.secured_message_version
237 8 : .spdm_version_count == 0) {
238 0 : return 0;
239 : }
240 :
241 8 : if (libspdm_get_connection_version (spdm_context) >= SPDM_MESSAGE_VERSION_12) {
242 4 : size = sizeof(spdm_general_opaque_data_table_header_t);
243 34 : for (element_index = 0; element_index < element_num; element_index++) {
244 30 : size += sizeof(secured_message_opaque_element_table_header_t) +
245 : sizeof(secured_message_opaque_element_version_selection_t);
246 : /* Add Padding*/
247 30 : size = (size + 3) & ~3;
248 : }
249 : } else {
250 4 : size = sizeof(secured_message_general_opaque_data_table_header_t);
251 34 : for (element_index = 0; element_index < element_num; element_index++) {
252 30 : size += sizeof(secured_message_opaque_element_table_header_t) +
253 : sizeof(secured_message_opaque_element_version_selection_t);
254 : /* Add Padding*/
255 30 : size = (size + 3) & ~3;
256 : }
257 : }
258 :
259 8 : return size;
260 : }
261 :
262 : /**
263 : * Build opaque data selection version test.
264 : *
265 : * @param data_out_size[in] size in bytes of the data_out.
266 : * On input, it means the size in bytes of data_out buffer.
267 : * On output, it means the size in bytes of copied data_out buffer if RETURN_SUCCESS is returned,
268 : * and means the size in bytes of desired data_out buffer if RETURN_BUFFER_TOO_SMALL is returned.
269 : * @param data_out[in] A pointer to the destination buffer to store the opaque data selection version.
270 : * @param element_num[in] in this test function, the element number < 9 is right. because element id is changed with element_index
271 : *
272 : * @retval RETURN_SUCCESS The opaque data selection version is built successfully.
273 : * @retval RETURN_BUFFER_TOO_SMALL The buffer is too small to hold the data.
274 : **/
275 : libspdm_return_t
276 4 : libspdm_build_opaque_data_version_selection_data_test(const libspdm_context_t *spdm_context,
277 : size_t *data_out_size,
278 : void *data_out,
279 : uint8_t element_num)
280 : {
281 : size_t final_data_size;
282 : secured_message_general_opaque_data_table_header_t
283 : *general_opaque_data_table_header;
284 : spdm_general_opaque_data_table_header_t
285 : *spdm_general_opaque_data_table_header;
286 : secured_message_opaque_element_table_header_t
287 : *opaque_element_table_header;
288 : secured_message_opaque_element_version_selection_t
289 : *opaque_element_version_section;
290 : void *end;
291 : uint8_t element_index;
292 : size_t current_element_len;
293 :
294 4 : if (spdm_context->local_context.secured_message_version
295 4 : .spdm_version_count == 0) {
296 0 : *data_out_size = 0;
297 0 : return LIBSPDM_STATUS_SUCCESS;
298 : }
299 :
300 : final_data_size =
301 4 : libspdm_get_multi_element_opaque_data_version_selection_data_size(spdm_context,
302 : element_num);
303 4 : if (*data_out_size < final_data_size) {
304 0 : *data_out_size = final_data_size;
305 0 : return LIBSPDM_STATUS_BUFFER_TOO_SMALL;
306 : }
307 :
308 4 : if (libspdm_get_connection_version (spdm_context) >= SPDM_MESSAGE_VERSION_12) {
309 2 : spdm_general_opaque_data_table_header = data_out;
310 2 : spdm_general_opaque_data_table_header->total_elements = element_num;
311 2 : libspdm_write_uint24(spdm_general_opaque_data_table_header->reserved, 0);
312 :
313 2 : opaque_element_table_header =
314 : (void *)(spdm_general_opaque_data_table_header + 1);
315 : } else {
316 2 : general_opaque_data_table_header = data_out;
317 2 : general_opaque_data_table_header->spec_id =
318 : SECURED_MESSAGE_OPAQUE_DATA_SPEC_ID;
319 2 : general_opaque_data_table_header->opaque_version =
320 : SECURED_MESSAGE_OPAQUE_VERSION;
321 2 : general_opaque_data_table_header->total_elements = element_num;
322 2 : general_opaque_data_table_header->reserved = 0;
323 :
324 2 : opaque_element_table_header =
325 : (void *)(general_opaque_data_table_header + 1);
326 : }
327 :
328 34 : for (element_index = 0; element_index < element_num; element_index++) {
329 : /*id is changed with element_index*/
330 30 : opaque_element_table_header->id = element_index;
331 30 : opaque_element_table_header->vendor_len = 0;
332 30 : opaque_element_table_header->opaque_element_data_len =
333 : sizeof(secured_message_opaque_element_version_selection_t);
334 :
335 30 : opaque_element_version_section = (void *)(opaque_element_table_header + 1);
336 30 : opaque_element_version_section->sm_data_version =
337 : SECURED_MESSAGE_OPAQUE_ELEMENT_SMDATA_DATA_VERSION;
338 30 : opaque_element_version_section->sm_data_id =
339 : SECURED_MESSAGE_OPAQUE_ELEMENT_SMDATA_ID_VERSION_SELECTION;
340 30 : opaque_element_version_section->selected_version =
341 30 : spdm_context->connection_info.secured_message_version;
342 :
343 : /*move to next element*/
344 30 : current_element_len = sizeof(secured_message_opaque_element_table_header_t) +
345 30 : opaque_element_table_header->opaque_element_data_len;
346 : /* Add Padding*/
347 30 : current_element_len = (current_element_len + 3) & ~3;
348 :
349 30 : opaque_element_table_header =
350 : (secured_message_opaque_element_table_header_t *)(
351 : (uint8_t *)opaque_element_table_header + current_element_len);
352 : }
353 :
354 : /* Zero Padding*/
355 4 : end = opaque_element_version_section + 1;
356 4 : libspdm_zero_mem(end, (size_t)data_out + final_data_size - (size_t)end);
357 :
358 4 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO,
359 : "successful build multi element opaque data selection version! \n"));
360 :
361 4 : return LIBSPDM_STATUS_SUCCESS;
362 : }
363 :
364 :
365 : /**
366 : * Test 1: Basic test - tests happy path of setting and getting opaque data from
367 : * context successfully.
368 : **/
369 1 : static void libspdm_test_common_context_data_case1(void **state)
370 : {
371 : libspdm_return_t status;
372 : libspdm_test_context_t *spdm_test_context;
373 : libspdm_context_t *spdm_context;
374 1 : void *data = (void *)&libspdm_opaque_data;
375 1 : void *return_data = NULL;
376 1 : size_t data_return_size = 0;
377 :
378 1 : spdm_test_context = *state;
379 1 : spdm_context = spdm_test_context->spdm_context;
380 1 : spdm_test_context->case_id = 0x1;
381 :
382 1 : status = libspdm_set_data(spdm_context, LIBSPDM_DATA_APP_CONTEXT_DATA,
383 : NULL, &data, sizeof(data));
384 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
385 :
386 1 : data_return_size = sizeof(return_data);
387 1 : status = libspdm_get_data(spdm_context, LIBSPDM_DATA_APP_CONTEXT_DATA,
388 : NULL, &return_data, &data_return_size);
389 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
390 :
391 1 : assert_memory_equal(data, return_data, sizeof(data));
392 1 : assert_int_equal(data_return_size, sizeof(void*));
393 :
394 : /* check that nothing changed at the data location */
395 1 : assert_int_equal(libspdm_opaque_data, 0xDEADBEEF);
396 1 : }
397 :
398 : /**
399 : * Test 2: Test failure paths of setting opaque data in context. libspdm_set_data
400 : * should fail when an invalid size is passed.
401 : **/
402 1 : static void libspdm_test_common_context_data_case2(void **state)
403 : {
404 : libspdm_return_t status;
405 : libspdm_test_context_t *spdm_test_context;
406 : libspdm_context_t *spdm_context;
407 1 : void *data = (void *)&libspdm_opaque_data;
408 1 : void *return_data = NULL;
409 1 : void *current_return_data = NULL;
410 1 : size_t data_return_size = 0;
411 :
412 1 : spdm_test_context = *state;
413 1 : spdm_context = spdm_test_context->spdm_context;
414 1 : spdm_test_context->case_id = 0x2;
415 :
416 : /**
417 : * Get current opaque data in context. May have been set in previous
418 : * tests. This will be used to compare later to ensure the value hasn't
419 : * changed after a failed set data.
420 : */
421 1 : data_return_size = sizeof(current_return_data);
422 1 : status = libspdm_get_data(spdm_context, LIBSPDM_DATA_APP_CONTEXT_DATA,
423 : NULL, ¤t_return_data, &data_return_size);
424 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
425 1 : assert_int_equal(data_return_size, sizeof(void*));
426 :
427 : /* Ensure nothing has changed between subsequent calls to get data */
428 1 : assert_ptr_equal(current_return_data, &libspdm_opaque_data);
429 :
430 : /*
431 : * Set data with invalid size, it should fail. Read back to ensure that
432 : * no data was set.
433 : */
434 1 : status = libspdm_set_data(spdm_context, LIBSPDM_DATA_APP_CONTEXT_DATA,
435 : NULL, &data, 500);
436 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_PARAMETER);
437 :
438 1 : data_return_size = sizeof(return_data);
439 1 : status = libspdm_get_data(spdm_context, LIBSPDM_DATA_APP_CONTEXT_DATA,
440 : NULL, &return_data, &data_return_size);
441 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
442 1 : assert_ptr_equal(return_data, current_return_data);
443 1 : assert_int_equal(data_return_size, sizeof(void*));
444 :
445 : /* check that nothing changed at the data location */
446 1 : assert_int_equal(libspdm_opaque_data, 0xDEADBEEF);
447 1 : }
448 :
449 : /**
450 : * Test 3: Test failure paths of setting opaque data in context. libspdm_set_data
451 : * should fail when data contains NULL value.
452 : **/
453 1 : static void libspdm_test_common_context_data_case3(void **state)
454 : {
455 : libspdm_return_t status;
456 : libspdm_test_context_t *spdm_test_context;
457 : libspdm_context_t *spdm_context;
458 1 : void *data = NULL;
459 1 : void *return_data = NULL;
460 1 : void *current_return_data = NULL;
461 1 : size_t data_return_size = 0;
462 :
463 1 : spdm_test_context = *state;
464 1 : spdm_context = spdm_test_context->spdm_context;
465 1 : spdm_test_context->case_id = 0x3;
466 :
467 : /**
468 : * Get current opaque data in context. May have been set in previous
469 : * tests. This will be used to compare later to ensure the value hasn't
470 : * changed after a failed set data.
471 : */
472 1 : data_return_size = sizeof(current_return_data);
473 1 : status = libspdm_get_data(spdm_context, LIBSPDM_DATA_APP_CONTEXT_DATA,
474 : NULL, ¤t_return_data, &data_return_size);
475 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
476 1 : assert_int_equal(data_return_size, sizeof(void*));
477 :
478 : /* Ensure nothing has changed between subsequent calls to get data */
479 1 : assert_ptr_equal(current_return_data, &libspdm_opaque_data);
480 :
481 :
482 : /*
483 : * Set data with NULL data, it should fail. Read back to ensure that
484 : * no data was set.
485 : */
486 1 : status = libspdm_set_data(spdm_context, LIBSPDM_DATA_APP_CONTEXT_DATA,
487 : NULL, &data, sizeof(void *));
488 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_PARAMETER);
489 :
490 1 : data_return_size = sizeof(return_data);
491 1 : status = libspdm_get_data(spdm_context, LIBSPDM_DATA_APP_CONTEXT_DATA,
492 : NULL, &return_data, &data_return_size);
493 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
494 1 : assert_ptr_equal(return_data, current_return_data);
495 1 : assert_int_equal(data_return_size, sizeof(void*));
496 :
497 : /* check that nothing changed at the data location */
498 1 : assert_int_equal(libspdm_opaque_data, 0xDEADBEEF);
499 :
500 1 : }
501 :
502 : /**
503 : * Test 4: Test failure paths of getting opaque data in context. libspdm_get_data
504 : * should fail when the size of buffer to get is too small.
505 : **/
506 1 : static void libspdm_test_common_context_data_case4(void **state)
507 : {
508 : libspdm_return_t status;
509 : libspdm_test_context_t *spdm_test_context;
510 : libspdm_context_t *spdm_context;
511 1 : void *data = (void *)&libspdm_opaque_data;
512 1 : void *return_data = NULL;
513 1 : size_t data_return_size = 0;
514 :
515 1 : spdm_test_context = *state;
516 1 : spdm_context = spdm_test_context->spdm_context;
517 1 : spdm_test_context->case_id = 0x4;
518 :
519 : /*
520 : * Set data successfully.
521 : */
522 1 : status = libspdm_set_data(spdm_context, LIBSPDM_DATA_APP_CONTEXT_DATA,
523 : NULL, &data, sizeof(void *));
524 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
525 :
526 : /*
527 : * Fail get data due to insufficient buffer for return value. returned
528 : * data size must return required buffer size.
529 : */
530 1 : data_return_size = sizeof(void*) - 1;
531 1 : status = libspdm_get_data(spdm_context, LIBSPDM_DATA_APP_CONTEXT_DATA,
532 : NULL, &return_data, &data_return_size);
533 1 : assert_int_equal(status, LIBSPDM_STATUS_BUFFER_TOO_SMALL);
534 1 : assert_int_equal(data_return_size, sizeof(void*));
535 :
536 : /* check that nothing changed at the data location */
537 1 : assert_int_equal(libspdm_opaque_data, 0xDEADBEEF);
538 1 : }
539 :
540 : /**
541 : * Test 5: There is no root cert.
542 : * Expected Behavior: Return true result.
543 : **/
544 1 : void libspdm_test_verify_peer_cert_chain_buffer_case5(void **state)
545 : {
546 : libspdm_test_context_t *spdm_test_context;
547 : libspdm_context_t *spdm_context;
548 : void *data;
549 : size_t data_size;
550 : void *hash;
551 : size_t hash_size;
552 : const uint8_t *root_cert;
553 : size_t root_cert_size;
554 :
555 : const void *trust_anchor;
556 : size_t trust_anchor_size;
557 : bool result;
558 : uint8_t root_cert_index;
559 :
560 1 : spdm_test_context = *state;
561 1 : spdm_context = spdm_test_context->spdm_context;
562 1 : spdm_test_context->case_id = 0x5;
563 : /* Setting SPDM context as the first steps of the protocol has been accomplished*/
564 1 : spdm_context->connection_info.connection_state =
565 : LIBSPDM_CONNECTION_STATE_AFTER_DIGESTS;
566 1 : spdm_context->connection_info.capability.flags |=
567 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP;
568 : /* Loading Root certificate and saving its hash*/
569 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
570 : m_libspdm_use_asym_algo, &data,
571 : &data_size, &hash, &hash_size)) {
572 0 : assert(false);
573 : }
574 1 : if (!libspdm_x509_get_cert_from_cert_chain(
575 1 : (uint8_t *)data + sizeof(spdm_cert_chain_t) + hash_size,
576 1 : data_size - sizeof(spdm_cert_chain_t) - hash_size, 0, &root_cert, &root_cert_size)) {
577 0 : assert(false);
578 : }
579 :
580 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
581 1 : spdm_context->connection_info.algorithm.base_asym_algo= m_libspdm_use_asym_algo;
582 1 : spdm_context->local_context.is_requester = true;
583 :
584 : /*clear root cert array*/
585 11 : for (root_cert_index = 0; root_cert_index < LIBSPDM_MAX_ROOT_CERT_SUPPORT; root_cert_index++) {
586 10 : spdm_context->local_context.peer_root_cert_provision_size[root_cert_index] = 0;
587 10 : spdm_context->local_context.peer_root_cert_provision[root_cert_index] = NULL;
588 : }
589 1 : result = libspdm_verify_peer_cert_chain_buffer(spdm_context, data, data_size, &trust_anchor,
590 : &trust_anchor_size);
591 1 : assert_int_equal (result, true);
592 :
593 1 : free(data);
594 1 : }
595 :
596 : /**
597 : * Test 6: There is one root cert. And the root cert has two case: match root cert, mismatch root cert.
598 : *
599 : * case Expected Behavior
600 : * there is one match root cert; return false
601 : * there is one mismatch root cert; return true, and the return trust_anchor is root cert.
602 : **/
603 1 : void libspdm_test_verify_peer_cert_chain_buffer_case6(void **state)
604 : {
605 : libspdm_test_context_t *spdm_test_context;
606 : libspdm_context_t *spdm_context;
607 : void *data;
608 : size_t data_size;
609 : void *hash;
610 : size_t hash_size;
611 : const uint8_t *root_cert;
612 : size_t root_cert_size;
613 :
614 : void *data_test;
615 : size_t data_size_test;
616 : void *hash_test;
617 : size_t hash_size_test;
618 : const uint8_t *root_cert_test;
619 : size_t root_cert_size_test;
620 1 : uint32_t m_libspdm_use_asym_algo_test =SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSASSA_2048;
621 :
622 : const void *trust_anchor;
623 : size_t trust_anchor_size;
624 : bool result;
625 : uint8_t root_cert_index;
626 :
627 1 : spdm_test_context = *state;
628 1 : spdm_context = spdm_test_context->spdm_context;
629 1 : spdm_test_context->case_id = 0x6;
630 : /* Setting SPDM context as the first steps of the protocol has been accomplished*/
631 1 : spdm_context->connection_info.connection_state =
632 : LIBSPDM_CONNECTION_STATE_AFTER_DIGESTS;
633 1 : spdm_context->connection_info.capability.flags |=
634 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP;
635 1 : spdm_context->local_context.is_requester = true;
636 :
637 : /* Loading Root certificate and saving its hash*/
638 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
639 : m_libspdm_use_asym_algo, &data,
640 : &data_size, &hash, &hash_size)) {
641 0 : assert(false);
642 : }
643 1 : if (!libspdm_x509_get_cert_from_cert_chain(
644 1 : (uint8_t *)data + sizeof(spdm_cert_chain_t) + hash_size,
645 1 : data_size - sizeof(spdm_cert_chain_t) - hash_size, 0, &root_cert, &root_cert_size)) {
646 0 : assert(false);
647 : }
648 : /* Loading Other test Root certificate and saving its hash*/
649 1 : libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
650 : m_libspdm_use_asym_algo_test, &data_test,
651 : &data_size_test, &hash_test, &hash_size_test);
652 1 : libspdm_x509_get_cert_from_cert_chain(
653 1 : (uint8_t *)data_test + sizeof(spdm_cert_chain_t) + hash_size_test,
654 1 : data_size_test - sizeof(spdm_cert_chain_t) - hash_size_test, 0,
655 : &root_cert_test, &root_cert_size_test);
656 :
657 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
658 1 : spdm_context->connection_info.algorithm.base_asym_algo= m_libspdm_use_asym_algo;
659 :
660 : /*clear root cert array*/
661 11 : for (root_cert_index = 0; root_cert_index < LIBSPDM_MAX_ROOT_CERT_SUPPORT; root_cert_index++) {
662 10 : spdm_context->local_context.peer_root_cert_provision_size[root_cert_index] = 0;
663 10 : spdm_context->local_context.peer_root_cert_provision[root_cert_index] = NULL;
664 : }
665 :
666 : /*case: match root cert case*/
667 1 : spdm_context->local_context.peer_root_cert_provision_size[0] =root_cert_size_test;
668 1 : spdm_context->local_context.peer_root_cert_provision[0] = root_cert_test;
669 1 : result = libspdm_verify_peer_cert_chain_buffer(spdm_context, data, data_size, &trust_anchor,
670 : &trust_anchor_size);
671 1 : assert_int_equal (result, false);
672 :
673 : /*case: mismatch root cert case*/
674 1 : spdm_context->local_context.peer_root_cert_provision_size[0] =root_cert_size;
675 1 : spdm_context->local_context.peer_root_cert_provision[0] = root_cert;
676 1 : result = libspdm_verify_peer_cert_chain_buffer(spdm_context, data, data_size, &trust_anchor,
677 : &trust_anchor_size);
678 1 : assert_int_equal (result, true);
679 1 : assert_ptr_equal (trust_anchor, root_cert);
680 :
681 1 : free(data);
682 1 : free(data_test);
683 1 : }
684 :
685 : /**
686 : * Test 7: There are LIBSPDM_MAX_ROOT_CERT_SUPPORT/2 root cert.
687 : *
688 : * case Expected Behavior
689 : * there is no match root cert; return false
690 : * there is one match root cert in the end; return true, and the return trust_anchor is root cert.
691 : * there is one match root cert in the middle; return true, and the return trust_anchor is root cert.
692 : **/
693 1 : void libspdm_test_verify_peer_cert_chain_buffer_case7(void **state)
694 : {
695 : libspdm_test_context_t *spdm_test_context;
696 : libspdm_context_t *spdm_context;
697 : void *data;
698 : size_t data_size;
699 : void *hash;
700 : size_t hash_size;
701 : const uint8_t *root_cert;
702 : size_t root_cert_size;
703 :
704 : void *data_test;
705 : size_t data_size_test;
706 : void *hash_test;
707 : size_t hash_size_test;
708 : const uint8_t *root_cert_test;
709 : size_t root_cert_size_test;
710 1 : uint32_t m_libspdm_use_asym_algo_test =SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSASSA_2048;
711 :
712 : const void *trust_anchor;
713 : size_t trust_anchor_size;
714 : bool result;
715 : uint8_t root_cert_index;
716 :
717 1 : spdm_test_context = *state;
718 1 : spdm_context = spdm_test_context->spdm_context;
719 1 : spdm_test_context->case_id = 0x7;
720 : /* Setting SPDM context as the first steps of the protocol has been accomplished*/
721 1 : spdm_context->connection_info.connection_state =
722 : LIBSPDM_CONNECTION_STATE_AFTER_DIGESTS;
723 1 : spdm_context->connection_info.capability.flags |=
724 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP;
725 1 : spdm_context->local_context.is_requester = true;
726 : /* Loading Root certificate and saving its hash*/
727 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
728 : m_libspdm_use_asym_algo, &data,
729 : &data_size, &hash, &hash_size)) {
730 0 : assert(false);
731 : }
732 1 : if (!libspdm_x509_get_cert_from_cert_chain(
733 1 : (uint8_t *)data + sizeof(spdm_cert_chain_t) + hash_size,
734 1 : data_size - sizeof(spdm_cert_chain_t) - hash_size, 0, &root_cert, &root_cert_size)) {
735 0 : assert(false);
736 : }
737 : /* Loading Other test Root certificate and saving its hash*/
738 1 : libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
739 : m_libspdm_use_asym_algo_test, &data_test,
740 : &data_size_test, &hash_test, &hash_size_test);
741 1 : libspdm_x509_get_cert_from_cert_chain(
742 1 : (uint8_t *)data_test + sizeof(spdm_cert_chain_t) + hash_size_test,
743 1 : data_size_test - sizeof(spdm_cert_chain_t) - hash_size_test, 0,
744 : &root_cert_test, &root_cert_size_test);
745 :
746 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
747 1 : spdm_context->connection_info.algorithm.base_asym_algo= m_libspdm_use_asym_algo;
748 :
749 : /*clear root cert array*/
750 11 : for (root_cert_index = 0; root_cert_index < LIBSPDM_MAX_ROOT_CERT_SUPPORT; root_cert_index++) {
751 10 : spdm_context->local_context.peer_root_cert_provision_size[root_cert_index] = 0;
752 10 : spdm_context->local_context.peer_root_cert_provision[root_cert_index] = NULL;
753 : }
754 :
755 : /*case: there is no match root cert*/
756 6 : for (root_cert_index = 0; root_cert_index < (LIBSPDM_MAX_ROOT_CERT_SUPPORT / 2);
757 5 : root_cert_index++) {
758 5 : spdm_context->local_context.peer_root_cert_provision_size[root_cert_index] =
759 : root_cert_size_test;
760 5 : spdm_context->local_context.peer_root_cert_provision[root_cert_index] = root_cert_test;
761 : }
762 1 : result = libspdm_verify_peer_cert_chain_buffer(spdm_context, data, data_size, &trust_anchor,
763 : &trust_anchor_size);
764 1 : assert_int_equal (result, false);
765 :
766 : /*case: there is no match root cert in the end*/
767 : spdm_context->local_context.peer_root_cert_provision_size[LIBSPDM_MAX_ROOT_CERT_SUPPORT / 2 -
768 1 : 1] =root_cert_size;
769 : spdm_context->local_context.peer_root_cert_provision[LIBSPDM_MAX_ROOT_CERT_SUPPORT / 2 -
770 1 : 1] = root_cert;
771 1 : result = libspdm_verify_peer_cert_chain_buffer(spdm_context, data, data_size, &trust_anchor,
772 : &trust_anchor_size);
773 1 : assert_int_equal (result, true);
774 1 : assert_ptr_equal (trust_anchor, root_cert);
775 :
776 : /*case: there is no match root cert in the middle*/
777 : spdm_context->local_context.peer_root_cert_provision_size[LIBSPDM_MAX_ROOT_CERT_SUPPORT /
778 1 : 4] =root_cert_size;
779 : spdm_context->local_context.peer_root_cert_provision[LIBSPDM_MAX_ROOT_CERT_SUPPORT /
780 1 : 4] = root_cert;
781 1 : result = libspdm_verify_peer_cert_chain_buffer(spdm_context, data, data_size, &trust_anchor,
782 : &trust_anchor_size);
783 1 : assert_int_equal (result, true);
784 1 : assert_ptr_equal (trust_anchor, root_cert);
785 :
786 1 : free(data);
787 1 : free(data_test);
788 1 : }
789 :
790 :
791 : /**
792 : * Test 8: There are full(LIBSPDM_MAX_ROOT_CERT_SUPPORT - 1) root cert.
793 : *
794 : * case Expected Behavior
795 : * there is no match root cert; return false
796 : * there is one match root cert in the end; return true, and the return trust_anchor is root cert.
797 : * there is one match root cert in the middle; return true, and the return trust_anchor is root cert.
798 : **/
799 1 : void libspdm_test_verify_peer_cert_chain_buffer_case8(void **state)
800 : {
801 : libspdm_test_context_t *spdm_test_context;
802 : libspdm_context_t *spdm_context;
803 : void *data;
804 : size_t data_size;
805 : void *hash;
806 : size_t hash_size;
807 : const uint8_t *root_cert;
808 : size_t root_cert_size;
809 :
810 : void *data_test;
811 : size_t data_size_test;
812 : void *hash_test;
813 : size_t hash_size_test;
814 : const uint8_t *root_cert_test;
815 : size_t root_cert_size_test;
816 1 : uint32_t m_libspdm_use_asym_algo_test =SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSASSA_2048;
817 :
818 : const void *trust_anchor;
819 : size_t trust_anchor_size;
820 : bool result;
821 : uint8_t root_cert_index;
822 :
823 1 : spdm_test_context = *state;
824 1 : spdm_context = spdm_test_context->spdm_context;
825 1 : spdm_test_context->case_id = 0x8;
826 : /* Setting SPDM context as the first steps of the protocol has been accomplished*/
827 1 : spdm_context->connection_info.connection_state =
828 : LIBSPDM_CONNECTION_STATE_AFTER_DIGESTS;
829 1 : spdm_context->connection_info.capability.flags |=
830 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP;
831 1 : spdm_context->local_context.is_requester = true;
832 : /* Loading Root certificate and saving its hash*/
833 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
834 : m_libspdm_use_asym_algo, &data,
835 : &data_size, &hash, &hash_size)) {
836 0 : assert(false);
837 : }
838 1 : if (!libspdm_x509_get_cert_from_cert_chain(
839 1 : (uint8_t *)data + sizeof(spdm_cert_chain_t) + hash_size,
840 1 : data_size - sizeof(spdm_cert_chain_t) - hash_size, 0, &root_cert, &root_cert_size)) {
841 0 : assert(false);
842 : }
843 : /* Loading Other test Root certificate and saving its hash*/
844 1 : libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
845 : m_libspdm_use_asym_algo_test, &data_test,
846 : &data_size_test, &hash_test, &hash_size_test);
847 1 : libspdm_x509_get_cert_from_cert_chain(
848 1 : (uint8_t *)data_test + sizeof(spdm_cert_chain_t) + hash_size_test,
849 1 : data_size_test - sizeof(spdm_cert_chain_t) - hash_size_test, 0,
850 : &root_cert_test, &root_cert_size_test);
851 :
852 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
853 1 : spdm_context->connection_info.algorithm.base_asym_algo= m_libspdm_use_asym_algo;
854 :
855 : /*case: there is no match root cert*/
856 11 : for (root_cert_index = 0; root_cert_index < LIBSPDM_MAX_ROOT_CERT_SUPPORT; root_cert_index++) {
857 10 : spdm_context->local_context.peer_root_cert_provision_size[root_cert_index] =
858 : root_cert_size_test;
859 10 : spdm_context->local_context.peer_root_cert_provision[root_cert_index] = root_cert_test;
860 : }
861 1 : result = libspdm_verify_peer_cert_chain_buffer(spdm_context, data, data_size, &trust_anchor,
862 : &trust_anchor_size);
863 1 : assert_int_equal (result, false);
864 :
865 : /*case: there is no match root cert in the end*/
866 : spdm_context->local_context.peer_root_cert_provision_size[LIBSPDM_MAX_ROOT_CERT_SUPPORT -
867 1 : 1] =root_cert_size;
868 : spdm_context->local_context.peer_root_cert_provision[LIBSPDM_MAX_ROOT_CERT_SUPPORT -
869 1 : 1] = root_cert;
870 1 : result = libspdm_verify_peer_cert_chain_buffer(spdm_context, data, data_size, &trust_anchor,
871 : &trust_anchor_size);
872 1 : assert_int_equal (result, true);
873 1 : assert_ptr_equal (trust_anchor, root_cert);
874 :
875 : /*case: there is no match root cert in the middle*/
876 11 : for (root_cert_index = 0; root_cert_index < LIBSPDM_MAX_ROOT_CERT_SUPPORT; root_cert_index++) {
877 10 : spdm_context->local_context.peer_root_cert_provision_size[root_cert_index] =
878 : root_cert_size_test;
879 10 : spdm_context->local_context.peer_root_cert_provision[root_cert_index] = root_cert_test;
880 : }
881 : spdm_context->local_context.peer_root_cert_provision_size[LIBSPDM_MAX_ROOT_CERT_SUPPORT /
882 1 : 2] =root_cert_size;
883 : spdm_context->local_context.peer_root_cert_provision[LIBSPDM_MAX_ROOT_CERT_SUPPORT /
884 1 : 2] = root_cert;
885 1 : result = libspdm_verify_peer_cert_chain_buffer(spdm_context, data, data_size, &trust_anchor,
886 : &trust_anchor_size);
887 1 : assert_int_equal (result, true);
888 1 : assert_ptr_equal (trust_anchor, root_cert);
889 :
890 1 : free(data);
891 1 : free(data_test);
892 1 : }
893 :
894 : /**
895 : * Test 9: test set data for root cert.
896 : *
897 : * case Expected Behavior
898 : * there is null root cert; return RETURN_SUCCESS, and the root cert is set successfully.
899 : * there is full root cert; return RETURN_OUT_OF_RESOURCES.
900 : **/
901 1 : static void libspdm_test_set_data_case9(void **state)
902 : {
903 : libspdm_return_t status;
904 : libspdm_test_context_t *spdm_test_context;
905 : libspdm_context_t *spdm_context;
906 : libspdm_data_parameter_t parameter;
907 :
908 : void *data;
909 : size_t data_size;
910 : void *hash;
911 : size_t hash_size;
912 : const uint8_t *root_cert;
913 : uint8_t root_cert_buffer[LIBSPDM_MAX_CERT_CHAIN_SIZE];
914 : size_t root_cert_size;
915 :
916 : uint8_t root_cert_index;
917 :
918 1 : spdm_test_context = *state;
919 1 : spdm_context = spdm_test_context->spdm_context;
920 1 : spdm_test_context->case_id = 0x9;
921 :
922 : /* Loading Root certificate and saving its hash*/
923 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
924 : m_libspdm_use_asym_algo, &data,
925 : &data_size, &hash, &hash_size)) {
926 0 : assert(false);
927 : }
928 1 : if (!libspdm_x509_get_cert_from_cert_chain(
929 1 : (uint8_t *)data + sizeof(spdm_cert_chain_t) + hash_size,
930 1 : data_size - sizeof(spdm_cert_chain_t) - hash_size, 0, &root_cert, &root_cert_size)) {
931 0 : assert(false);
932 : }
933 1 : memcpy(root_cert_buffer, root_cert, root_cert_size);
934 :
935 : /*case: there is null root cert*/
936 11 : for (root_cert_index = 0; root_cert_index < LIBSPDM_MAX_ROOT_CERT_SUPPORT; root_cert_index++) {
937 10 : spdm_context->local_context.peer_root_cert_provision_size[root_cert_index] = 0;
938 10 : spdm_context->local_context.peer_root_cert_provision[root_cert_index] = NULL;
939 : }
940 1 : parameter.location = LIBSPDM_DATA_LOCATION_LOCAL;
941 1 : status = libspdm_set_data(spdm_context, LIBSPDM_DATA_PEER_PUBLIC_ROOT_CERT,
942 : ¶meter, root_cert_buffer, root_cert_size);
943 1 : assert_int_equal (status, LIBSPDM_STATUS_SUCCESS);
944 1 : assert_int_equal (spdm_context->local_context.peer_root_cert_provision_size[0], root_cert_size);
945 1 : assert_ptr_equal (spdm_context->local_context.peer_root_cert_provision[0], root_cert_buffer);
946 :
947 : /*case: there is full root cert*/
948 11 : for (root_cert_index = 0; root_cert_index < LIBSPDM_MAX_ROOT_CERT_SUPPORT; root_cert_index++) {
949 10 : spdm_context->local_context.peer_root_cert_provision_size[root_cert_index] = root_cert_size;
950 10 : spdm_context->local_context.peer_root_cert_provision[root_cert_index] = root_cert_buffer;
951 : }
952 1 : status = libspdm_set_data(spdm_context, LIBSPDM_DATA_PEER_PUBLIC_ROOT_CERT,
953 : ¶meter, root_cert_buffer, root_cert_size);
954 1 : assert_int_equal (status, LIBSPDM_STATUS_BUFFER_FULL);
955 :
956 1 : free(data);
957 1 : }
958 :
959 :
960 : /**
961 : * Test 10: There is no root cert.
962 : * Expected Behavior: Return true result.
963 : **/
964 1 : void libspdm_test_process_opaque_data_supported_version_data_case10(void **state)
965 : {
966 : libspdm_return_t status;
967 : libspdm_test_context_t *spdm_test_context;
968 : libspdm_context_t *spdm_context;
969 : size_t opaque_data_size;
970 : uint8_t element_num;
971 :
972 1 : spdm_test_context = *state;
973 1 : spdm_context = spdm_test_context->spdm_context;
974 1 : spdm_test_context->case_id = 0xA;
975 :
976 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
977 : SPDM_VERSION_NUMBER_SHIFT_BIT;
978 :
979 1 : spdm_context->local_context.secured_message_version.spdm_version_count = 1;
980 :
981 1 : element_num = 2;
982 1 : opaque_data_size =
983 1 : libspdm_get_multi_element_opaque_data_supported_version_data_size(
984 : spdm_context,
985 1 : spdm_context->local_context.secured_message_version.spdm_version_count,
986 : element_num);
987 :
988 : uint8_t *opaque_data_ptr;
989 1 : opaque_data_ptr = malloc(opaque_data_size);
990 :
991 1 : libspdm_build_multi_element_opaque_data_supported_version_test(
992 : spdm_context, &opaque_data_size, opaque_data_ptr, element_num);
993 :
994 1 : status = libspdm_process_opaque_data_supported_version_data(spdm_context,
995 : opaque_data_size,
996 : opaque_data_ptr);
997 :
998 1 : assert_int_equal (status, LIBSPDM_STATUS_SUCCESS);
999 :
1000 1 : free(opaque_data_ptr);
1001 1 : }
1002 :
1003 1 : void libspdm_test_process_opaque_data_supported_version_data_case11(void **state)
1004 : {
1005 : libspdm_return_t status;
1006 : libspdm_test_context_t *spdm_test_context;
1007 : libspdm_context_t *spdm_context;
1008 : size_t opaque_data_size;
1009 : uint8_t element_num;
1010 :
1011 1 : spdm_test_context = *state;
1012 1 : spdm_context = spdm_test_context->spdm_context;
1013 1 : spdm_test_context->case_id = 0xB;
1014 :
1015 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
1016 : SPDM_VERSION_NUMBER_SHIFT_BIT;
1017 :
1018 1 : spdm_context->local_context.secured_message_version.spdm_version_count = 1;
1019 :
1020 : /*make element id wrong*/
1021 1 : element_num = SPDM_REGISTRY_ID_MAX + 2;
1022 1 : opaque_data_size =
1023 1 : libspdm_get_multi_element_opaque_data_supported_version_data_size(
1024 : spdm_context,
1025 1 : spdm_context->local_context.secured_message_version.spdm_version_count,
1026 : element_num);
1027 :
1028 : uint8_t *opaque_data_ptr;
1029 1 : opaque_data_ptr = malloc(opaque_data_size);
1030 :
1031 1 : libspdm_build_multi_element_opaque_data_supported_version_test(
1032 : spdm_context, &opaque_data_size, opaque_data_ptr, element_num);
1033 :
1034 1 : status = libspdm_process_opaque_data_supported_version_data(spdm_context,
1035 : opaque_data_size,
1036 : opaque_data_ptr);
1037 :
1038 1 : assert_int_equal (status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
1039 :
1040 1 : free(opaque_data_ptr);
1041 1 : }
1042 :
1043 1 : void libspdm_test_process_opaque_data_supported_version_data_case12(void **state)
1044 : {
1045 : libspdm_return_t status;
1046 : libspdm_test_context_t *spdm_test_context;
1047 : libspdm_context_t *spdm_context;
1048 : size_t opaque_data_size;
1049 : uint8_t element_num;
1050 :
1051 1 : spdm_test_context = *state;
1052 1 : spdm_context = spdm_test_context->spdm_context;
1053 1 : spdm_test_context->case_id = 0xC;
1054 :
1055 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_12 <<
1056 : SPDM_VERSION_NUMBER_SHIFT_BIT;
1057 :
1058 1 : spdm_context->local_context.secured_message_version.spdm_version_count = 1;
1059 :
1060 1 : element_num = 2;
1061 1 : opaque_data_size =
1062 1 : libspdm_get_multi_element_opaque_data_supported_version_data_size(
1063 : spdm_context,
1064 1 : spdm_context->local_context.secured_message_version.spdm_version_count,
1065 : element_num);
1066 :
1067 : uint8_t *opaque_data_ptr;
1068 1 : opaque_data_ptr = malloc(opaque_data_size);
1069 :
1070 1 : libspdm_build_multi_element_opaque_data_supported_version_test(
1071 : spdm_context, &opaque_data_size, opaque_data_ptr, element_num);
1072 :
1073 1 : status = libspdm_process_opaque_data_supported_version_data(spdm_context,
1074 : opaque_data_size,
1075 : opaque_data_ptr);
1076 :
1077 1 : assert_int_equal (status, LIBSPDM_STATUS_SUCCESS);
1078 :
1079 1 : free(opaque_data_ptr);
1080 1 : }
1081 :
1082 1 : void libspdm_test_process_opaque_data_supported_version_data_case13(void **state)
1083 : {
1084 : libspdm_return_t status;
1085 : libspdm_test_context_t *spdm_test_context;
1086 : libspdm_context_t *spdm_context;
1087 : size_t opaque_data_size;
1088 : uint8_t element_num;
1089 :
1090 1 : spdm_test_context = *state;
1091 1 : spdm_context = spdm_test_context->spdm_context;
1092 1 : spdm_test_context->case_id = 0xD;
1093 :
1094 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_12 <<
1095 : SPDM_VERSION_NUMBER_SHIFT_BIT;
1096 :
1097 1 : spdm_context->local_context.secured_message_version.spdm_version_count = 1;
1098 :
1099 : /*make element id wrong*/
1100 1 : element_num = SPDM_REGISTRY_ID_MAX + 2;
1101 1 : opaque_data_size =
1102 1 : libspdm_get_multi_element_opaque_data_supported_version_data_size(
1103 : spdm_context,
1104 1 : spdm_context->local_context.secured_message_version.spdm_version_count,
1105 : element_num);
1106 :
1107 : uint8_t *opaque_data_ptr;
1108 1 : opaque_data_ptr = malloc(opaque_data_size);
1109 :
1110 1 : libspdm_build_multi_element_opaque_data_supported_version_test(
1111 : spdm_context, &opaque_data_size, opaque_data_ptr, element_num);
1112 :
1113 1 : status = libspdm_process_opaque_data_supported_version_data(spdm_context,
1114 : opaque_data_size,
1115 : opaque_data_ptr);
1116 :
1117 1 : assert_int_equal (status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
1118 :
1119 1 : free(opaque_data_ptr);
1120 1 : }
1121 :
1122 :
1123 1 : void libspdm_test_process_opaque_data_selection_version_data_case14(void **state)
1124 : {
1125 : libspdm_return_t status;
1126 : libspdm_test_context_t *spdm_test_context;
1127 : libspdm_context_t *spdm_context;
1128 : size_t opaque_data_size;
1129 : uint8_t element_num;
1130 :
1131 1 : spdm_test_context = *state;
1132 1 : spdm_context = spdm_test_context->spdm_context;
1133 1 : spdm_test_context->case_id = 0xE;
1134 :
1135 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
1136 : SPDM_VERSION_NUMBER_SHIFT_BIT;
1137 :
1138 1 : spdm_context->local_context.secured_message_version.spdm_version_count = 1;
1139 1 : spdm_context->connection_info.secured_message_version =
1140 : SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT;
1141 1 : spdm_context->local_context.secured_message_version.spdm_version[0] =
1142 : SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT;
1143 :
1144 1 : element_num = 2;
1145 1 : opaque_data_size =
1146 1 : libspdm_get_multi_element_opaque_data_version_selection_data_size(
1147 : spdm_context,
1148 : element_num);
1149 :
1150 : uint8_t *opaque_data_ptr;
1151 1 : opaque_data_ptr = malloc(opaque_data_size);
1152 :
1153 1 : libspdm_build_opaque_data_version_selection_data_test(
1154 : spdm_context, &opaque_data_size, opaque_data_ptr, element_num);
1155 :
1156 1 : status = libspdm_process_opaque_data_version_selection_data(spdm_context,
1157 : opaque_data_size,
1158 : opaque_data_ptr);
1159 :
1160 1 : assert_int_equal (status, LIBSPDM_STATUS_SUCCESS);
1161 :
1162 1 : free(opaque_data_ptr);
1163 1 : }
1164 :
1165 :
1166 1 : void libspdm_test_process_opaque_data_selection_version_data_case15(void **state)
1167 : {
1168 : libspdm_return_t status;
1169 : libspdm_test_context_t *spdm_test_context;
1170 : libspdm_context_t *spdm_context;
1171 : size_t opaque_data_size;
1172 : uint8_t element_num;
1173 :
1174 1 : spdm_test_context = *state;
1175 1 : spdm_context = spdm_test_context->spdm_context;
1176 1 : spdm_test_context->case_id = 0xF;
1177 :
1178 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
1179 : SPDM_VERSION_NUMBER_SHIFT_BIT;
1180 :
1181 1 : spdm_context->local_context.secured_message_version.spdm_version_count = 1;
1182 1 : spdm_context->connection_info.secured_message_version =
1183 : SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT;
1184 1 : spdm_context->local_context.secured_message_version.spdm_version[0] =
1185 : SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT;
1186 :
1187 : /*make element id wrong*/
1188 1 : element_num = SPDM_REGISTRY_ID_MAX + 2;
1189 1 : opaque_data_size =
1190 1 : libspdm_get_multi_element_opaque_data_version_selection_data_size(
1191 : spdm_context,
1192 : element_num);
1193 :
1194 : uint8_t *opaque_data_ptr;
1195 1 : opaque_data_ptr = malloc(opaque_data_size);
1196 :
1197 1 : libspdm_build_opaque_data_version_selection_data_test(
1198 : spdm_context, &opaque_data_size, opaque_data_ptr, element_num);
1199 :
1200 1 : status = libspdm_process_opaque_data_version_selection_data(spdm_context,
1201 : opaque_data_size,
1202 : opaque_data_ptr);
1203 :
1204 1 : assert_int_equal (status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
1205 :
1206 1 : free(opaque_data_ptr);
1207 1 : }
1208 :
1209 :
1210 1 : void libspdm_test_process_opaque_data_selection_version_data_case16(void **state)
1211 : {
1212 : libspdm_return_t status;
1213 : libspdm_test_context_t *spdm_test_context;
1214 : libspdm_context_t *spdm_context;
1215 : size_t opaque_data_size;
1216 : uint8_t element_num;
1217 :
1218 1 : spdm_test_context = *state;
1219 1 : spdm_context = spdm_test_context->spdm_context;
1220 1 : spdm_test_context->case_id = 0x10;
1221 :
1222 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_12 <<
1223 : SPDM_VERSION_NUMBER_SHIFT_BIT;
1224 :
1225 1 : spdm_context->local_context.secured_message_version.spdm_version_count = 1;
1226 1 : spdm_context->connection_info.secured_message_version =
1227 : SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT;
1228 1 : spdm_context->local_context.secured_message_version.spdm_version[0] =
1229 : SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT;
1230 :
1231 1 : element_num = 2;
1232 1 : opaque_data_size =
1233 1 : libspdm_get_multi_element_opaque_data_version_selection_data_size(
1234 : spdm_context,
1235 : element_num);
1236 :
1237 : uint8_t *opaque_data_ptr;
1238 1 : opaque_data_ptr = malloc(opaque_data_size);
1239 :
1240 1 : libspdm_build_opaque_data_version_selection_data_test(
1241 : spdm_context, &opaque_data_size, opaque_data_ptr, element_num);
1242 :
1243 1 : status = libspdm_process_opaque_data_version_selection_data(spdm_context,
1244 : opaque_data_size,
1245 : opaque_data_ptr);
1246 :
1247 1 : assert_int_equal (status, LIBSPDM_STATUS_SUCCESS);
1248 :
1249 1 : free(opaque_data_ptr);
1250 1 : }
1251 :
1252 1 : void libspdm_test_process_opaque_data_selection_version_data_case17(void **state)
1253 : {
1254 : libspdm_return_t status;
1255 : libspdm_test_context_t *spdm_test_context;
1256 : libspdm_context_t *spdm_context;
1257 : size_t opaque_data_size;
1258 : uint8_t element_num;
1259 :
1260 1 : spdm_test_context = *state;
1261 1 : spdm_context = spdm_test_context->spdm_context;
1262 1 : spdm_test_context->case_id = 0x11;
1263 :
1264 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_12 <<
1265 : SPDM_VERSION_NUMBER_SHIFT_BIT;
1266 :
1267 1 : spdm_context->local_context.secured_message_version.spdm_version_count = 1;
1268 1 : spdm_context->connection_info.secured_message_version =
1269 : SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT;
1270 1 : spdm_context->local_context.secured_message_version.spdm_version[0] =
1271 : SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT;
1272 :
1273 : /*make element id wrong*/
1274 1 : element_num = SPDM_REGISTRY_ID_MAX + 2;
1275 1 : opaque_data_size =
1276 1 : libspdm_get_multi_element_opaque_data_version_selection_data_size(
1277 : spdm_context,
1278 : element_num);
1279 :
1280 : uint8_t *opaque_data_ptr;
1281 1 : opaque_data_ptr = malloc(opaque_data_size);
1282 :
1283 1 : libspdm_build_opaque_data_version_selection_data_test(
1284 : spdm_context, &opaque_data_size, opaque_data_ptr, element_num);
1285 :
1286 1 : status = libspdm_process_opaque_data_version_selection_data(spdm_context,
1287 : opaque_data_size,
1288 : opaque_data_ptr);
1289 :
1290 1 : assert_int_equal (status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
1291 :
1292 1 : free(opaque_data_ptr);
1293 1 : }
1294 :
1295 1 : void libspdm_test_secured_message_context_location_selection_case18(void **state)
1296 : {
1297 : libspdm_return_t status;
1298 : libspdm_test_context_t *spdm_test_context;
1299 : libspdm_context_t *spdm_context;
1300 : void *secured_message_contexts[LIBSPDM_MAX_SESSION_COUNT];
1301 : size_t index;
1302 :
1303 1 : spdm_test_context = *state;
1304 1 : spdm_test_context->case_id = 0x12;
1305 :
1306 1 : spdm_context = (libspdm_context_t *)malloc(libspdm_get_context_size_without_secured_context());
1307 :
1308 5 : for (index = 0; index < LIBSPDM_MAX_SESSION_COUNT; index++)
1309 : {
1310 4 : secured_message_contexts[index] =
1311 4 : (void *)malloc(libspdm_secured_message_get_context_size());
1312 : }
1313 :
1314 1 : status = libspdm_init_context_with_secured_context(spdm_context, secured_message_contexts,
1315 : LIBSPDM_MAX_SESSION_COUNT);
1316 1 : assert_int_equal (status, LIBSPDM_STATUS_SUCCESS);
1317 :
1318 5 : for (index = 0; index < LIBSPDM_MAX_SESSION_COUNT; index++)
1319 : {
1320 : /* Ensure the SPDM context points to the specified memory. */
1321 4 : assert_ptr_equal(spdm_context->session_info[index].secured_message_context,
1322 : secured_message_contexts[index]);
1323 : }
1324 :
1325 1 : free(spdm_context);
1326 5 : for (index = 0; index < LIBSPDM_MAX_SESSION_COUNT; index++)
1327 : {
1328 4 : free(secured_message_contexts[index]);
1329 : }
1330 1 : }
1331 :
1332 1 : static void libspdm_test_export_master_secret_case19(void **state)
1333 : {
1334 : uint8_t target_buffer[LIBSPDM_MAX_HASH_SIZE];
1335 : bool result;
1336 : libspdm_secured_message_context_t secured_message_context;
1337 : size_t export_master_secret_size;
1338 :
1339 : /* Get the entire EMS when the reported size of the target buffer is larger than the size of the
1340 : * EMS. */
1341 65 : for (int index = 0; index < LIBSPDM_MAX_HASH_SIZE; index++) {
1342 64 : secured_message_context.export_master_secret[index] = (uint8_t)index;
1343 64 : target_buffer[index] = 0x00;
1344 : }
1345 :
1346 1 : secured_message_context.hash_size = LIBSPDM_MAX_HASH_SIZE;
1347 1 : export_master_secret_size = LIBSPDM_MAX_HASH_SIZE + 0x100;
1348 :
1349 1 : result = libspdm_secured_message_export_master_secret(&secured_message_context,
1350 : &target_buffer,
1351 : &export_master_secret_size);
1352 1 : assert_int_equal(result, true);
1353 :
1354 1 : libspdm_secured_message_clear_export_master_secret(&secured_message_context);
1355 :
1356 65 : for (int index = 0; index < LIBSPDM_MAX_HASH_SIZE; index++) {
1357 64 : assert_int_equal(target_buffer[index], index);
1358 64 : assert_int_equal(secured_message_context.export_master_secret[index], 0x00);
1359 : }
1360 1 : assert_int_equal(export_master_secret_size, LIBSPDM_MAX_HASH_SIZE);
1361 :
1362 : /* Get the entire EMS when the size of the target buffer is the same size as the EMS. */
1363 65 : for (int index = 0; index < LIBSPDM_MAX_HASH_SIZE; index++) {
1364 64 : secured_message_context.export_master_secret[index] = (uint8_t)index;
1365 64 : target_buffer[index] = 0x00;
1366 : }
1367 :
1368 1 : secured_message_context.hash_size = LIBSPDM_MAX_HASH_SIZE;
1369 1 : export_master_secret_size = LIBSPDM_MAX_HASH_SIZE;
1370 :
1371 1 : result = libspdm_secured_message_export_master_secret(&secured_message_context,
1372 : &target_buffer,
1373 : &export_master_secret_size);
1374 1 : assert_int_equal(result, true);
1375 :
1376 65 : for (int index = 0; index < LIBSPDM_MAX_HASH_SIZE; index++) {
1377 64 : assert_int_equal(target_buffer[index], index);
1378 : }
1379 1 : assert_int_equal(export_master_secret_size, LIBSPDM_MAX_HASH_SIZE);
1380 :
1381 : /* Get the truncated EMS when the size of the target buffer is less than the size of the EMS. */
1382 65 : for (int index = 0; index < LIBSPDM_MAX_HASH_SIZE; index++) {
1383 64 : secured_message_context.export_master_secret[index] = (uint8_t)index;
1384 64 : target_buffer[index] = 0x00;
1385 : }
1386 :
1387 1 : secured_message_context.hash_size = LIBSPDM_MAX_HASH_SIZE;
1388 1 : export_master_secret_size = LIBSPDM_MAX_HASH_SIZE - 4;
1389 :
1390 1 : result = libspdm_secured_message_export_master_secret(&secured_message_context,
1391 : &target_buffer,
1392 : &export_master_secret_size);
1393 1 : assert_int_equal(result, true);
1394 :
1395 65 : for (int index = 0; index < LIBSPDM_MAX_HASH_SIZE; index++) {
1396 64 : if (index < LIBSPDM_MAX_HASH_SIZE - 4) {
1397 60 : assert_int_equal(target_buffer[index], index);
1398 : } else {
1399 4 : assert_int_equal(target_buffer[index], 0x00);
1400 : }
1401 : }
1402 1 : assert_int_equal(export_master_secret_size, LIBSPDM_MAX_HASH_SIZE - 4);
1403 1 : }
1404 :
1405 1 : static void libspdm_test_check_context_case20(void **state)
1406 : {
1407 : void *context;
1408 : bool result;
1409 :
1410 1 : context = (void *)malloc (libspdm_get_context_size());
1411 :
1412 1 : libspdm_init_context (context);
1413 :
1414 1 : result = libspdm_check_context (context);
1415 1 : assert_int_equal(false, result);
1416 :
1417 1 : libspdm_register_transport_layer_func(context,
1418 : LIBSPDM_MAX_SPDM_MSG_SIZE,
1419 : LIBSPDM_TEST_TRANSPORT_HEADER_SIZE,
1420 : LIBSPDM_TEST_TRANSPORT_TAIL_SIZE,
1421 : libspdm_transport_test_encode_message,
1422 : libspdm_transport_test_decode_message);
1423 :
1424 1 : libspdm_register_device_buffer_func(context,
1425 : LIBSPDM_MAX_SENDER_RECEIVER_BUFFER_SIZE,
1426 : LIBSPDM_MAX_SENDER_RECEIVER_BUFFER_SIZE,
1427 : spdm_device_acquire_sender_buffer,
1428 : spdm_device_release_sender_buffer,
1429 : spdm_device_acquire_receiver_buffer,
1430 : spdm_device_release_receiver_buffer);
1431 :
1432 1 : result = libspdm_check_context (context);
1433 1 : assert_int_equal(true, result);
1434 :
1435 1 : libspdm_register_transport_layer_func(context,
1436 : SPDM_MIN_DATA_TRANSFER_SIZE_VERSION_12,
1437 : LIBSPDM_TEST_TRANSPORT_HEADER_SIZE,
1438 : LIBSPDM_TEST_TRANSPORT_TAIL_SIZE,
1439 : libspdm_transport_test_encode_message,
1440 : libspdm_transport_test_decode_message);
1441 :
1442 1 : result = libspdm_check_context (context);
1443 1 : assert_int_equal(false, result);
1444 1 : }
1445 :
1446 1 : static void libspdm_test_max_session_count_case21(void **state)
1447 : {
1448 : libspdm_context_t *spdm_context;
1449 : libspdm_data_parameter_t parameter;
1450 : size_t index;
1451 : size_t round;
1452 : uint16_t req_id;
1453 : uint16_t rsp_id;
1454 : uint32_t session_id;
1455 : void *session_info;
1456 : uint32_t dhe_session_count;
1457 : uint32_t psk_session_count;
1458 :
1459 7 : for (round = 0; round <= 5; round++) {
1460 : /* prepare parameter */
1461 6 : switch (round) {
1462 1 : case 0:
1463 1 : dhe_session_count = 1;
1464 1 : psk_session_count = 1;
1465 1 : break;
1466 1 : case 1:
1467 1 : dhe_session_count = LIBSPDM_MAX_SESSION_COUNT / 2;
1468 1 : psk_session_count = LIBSPDM_MAX_SESSION_COUNT - dhe_session_count;
1469 1 : break;
1470 1 : case 2:
1471 1 : dhe_session_count = 1;
1472 1 : psk_session_count = LIBSPDM_MAX_SESSION_COUNT - 1;
1473 1 : break;
1474 1 : case 3:
1475 1 : dhe_session_count = LIBSPDM_MAX_SESSION_COUNT - 1;
1476 1 : psk_session_count = 1;
1477 1 : break;
1478 1 : case 4:
1479 1 : dhe_session_count = 0;
1480 1 : psk_session_count = LIBSPDM_MAX_SESSION_COUNT;
1481 1 : break;
1482 1 : case 5:
1483 1 : dhe_session_count = LIBSPDM_MAX_SESSION_COUNT;
1484 1 : psk_session_count = 0;
1485 1 : break;
1486 0 : default:
1487 0 : dhe_session_count = 0;
1488 0 : psk_session_count = 0;
1489 0 : break;
1490 : }
1491 :
1492 : /* test */
1493 6 : spdm_context = (libspdm_context_t *)malloc(libspdm_get_context_size());
1494 6 : libspdm_init_context (spdm_context);
1495 6 : spdm_context->connection_info.capability.flags =
1496 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP |
1497 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
1498 6 : spdm_context->local_context.capability.flags =
1499 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP |
1500 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
1501 6 : spdm_context->connection_info.algorithm.base_hash_algo =
1502 : SPDM_ALGORITHMS_BASE_HASH_ALGO_TPM_ALG_SHA_256;
1503 6 : spdm_context->connection_info.algorithm.dhe_named_group =
1504 : SPDM_ALGORITHMS_DHE_NAMED_GROUP_SECP_256_R1;
1505 6 : spdm_context->connection_info.algorithm.aead_cipher_suite =
1506 : SPDM_ALGORITHMS_AEAD_CIPHER_SUITE_AES_256_GCM;
1507 6 : spdm_context->connection_info.algorithm.key_schedule =
1508 : SPDM_ALGORITHMS_KEY_SCHEDULE_SPDM;
1509 :
1510 6 : libspdm_zero_mem(¶meter, sizeof(parameter));
1511 6 : parameter.location = LIBSPDM_DATA_LOCATION_LOCAL;
1512 6 : if (dhe_session_count != 0) {
1513 5 : libspdm_set_data (spdm_context, LIBSPDM_DATA_MAX_DHE_SESSION_COUNT, ¶meter,
1514 : &dhe_session_count, sizeof(dhe_session_count));
1515 : }
1516 6 : if (psk_session_count != 0) {
1517 5 : libspdm_set_data (spdm_context, LIBSPDM_DATA_MAX_PSK_SESSION_COUNT, ¶meter,
1518 : &psk_session_count, sizeof(psk_session_count));
1519 : }
1520 :
1521 6 : if (dhe_session_count != 0) {
1522 16 : for (index = 0; index < dhe_session_count; index++)
1523 : {
1524 11 : req_id = libspdm_allocate_req_session_id (spdm_context, false);
1525 11 : assert_int_not_equal (req_id, INVALID_SESSION_ID & 0xFFFF);
1526 :
1527 11 : rsp_id = libspdm_allocate_rsp_session_id (spdm_context, false);
1528 11 : assert_int_not_equal (rsp_id, (INVALID_SESSION_ID & 0xFFFF0000) >> 16);
1529 :
1530 11 : session_id = libspdm_generate_session_id (req_id, rsp_id);
1531 11 : session_info = libspdm_assign_session_id (spdm_context, session_id, false);
1532 11 : assert_ptr_not_equal (session_info, NULL);
1533 : }
1534 5 : req_id = libspdm_allocate_req_session_id (spdm_context, false);
1535 5 : assert_int_equal (req_id, INVALID_SESSION_ID & 0xFFFF);
1536 :
1537 5 : rsp_id = libspdm_allocate_rsp_session_id (spdm_context, false);
1538 5 : assert_int_equal (rsp_id, (INVALID_SESSION_ID & 0xFFFF0000) >> 16);
1539 : }
1540 :
1541 6 : if (psk_session_count != 0) {
1542 16 : for (index = 0; index < psk_session_count; index++)
1543 : {
1544 11 : req_id = libspdm_allocate_req_session_id (spdm_context, true);
1545 11 : assert_int_not_equal (req_id, INVALID_SESSION_ID & 0xFFFF);
1546 :
1547 11 : rsp_id = libspdm_allocate_rsp_session_id (spdm_context, true);
1548 11 : assert_int_not_equal (rsp_id, (INVALID_SESSION_ID & 0xFFFF0000) >> 16);
1549 :
1550 11 : session_id = libspdm_generate_session_id (req_id, rsp_id);
1551 11 : session_info = libspdm_assign_session_id (spdm_context, session_id, true);
1552 11 : assert_ptr_not_equal (session_info, NULL);
1553 : }
1554 5 : req_id = libspdm_allocate_req_session_id (spdm_context, true);
1555 5 : assert_int_equal (req_id, INVALID_SESSION_ID & 0xFFFF);
1556 :
1557 5 : rsp_id = libspdm_allocate_rsp_session_id (spdm_context, true);
1558 5 : assert_int_equal (rsp_id, (INVALID_SESSION_ID & 0xFFFF0000) >> 16);
1559 : }
1560 :
1561 6 : free(spdm_context);
1562 : }
1563 1 : }
1564 :
1565 : #pragma pack(1)
1566 :
1567 : typedef struct {
1568 : spdm_general_opaque_data_table_header_t opaque_header;
1569 : spdm_svh_iana_cbor_header_t cbor_header;
1570 : uint8_t cbor_vendor_id[10];
1571 : uint16_t cbor_opaque_len;
1572 : uint8_t cbor_opaque[10];
1573 : /* uint8_t cbor_align[]; */
1574 : spdm_svh_vesa_header_t vesa_header;
1575 : uint16_t vesa_opaque_len;
1576 : uint8_t vesa_opaque[9];
1577 : uint8_t vesa_align[3];
1578 : spdm_svh_jedec_header_t jedec_header;
1579 : uint16_t jedec_opaque_len;
1580 : uint8_t jedec_opaque[8];
1581 : uint8_t jedec_align[2];
1582 : spdm_svh_cxl_header_t cxl_header;
1583 : uint16_t cxl_opaque_len;
1584 : uint8_t cxl_opaque[7];
1585 : uint8_t cxl_align[3];
1586 : spdm_svh_mipi_header_t mipi_header;
1587 : uint16_t mipi_opaque_len;
1588 : uint8_t mipi_opaque[6];
1589 : /* uint8_t mipi_align[0]; */
1590 : spdm_svh_hdbaset_header_t hdbaset_header;
1591 : uint16_t hdbaset_opaque_len;
1592 : uint8_t hdbaset_opaque[5];
1593 : uint8_t hdbaset_align[3];
1594 : spdm_svh_iana_header_t iana_header;
1595 : uint16_t iana_opaque_len;
1596 : uint8_t iana_opaque[4];
1597 : /* uint8_t iana_align[0]; */
1598 : spdm_svh_pcisig_header_t pcisig_header;
1599 : uint16_t pcisig_opaque_len;
1600 : uint8_t pcisig_opaque[3];
1601 : uint8_t pcisig_align[3];
1602 : spdm_svh_usb_header_t usb_header;
1603 : uint16_t usb_opaque_len;
1604 : uint8_t usb_opaque[2];
1605 : /* uint8_t usb_align[0]; */
1606 : spdm_svh_tcg_header_t tcg_header;
1607 : uint16_t tcg_opaque_len;
1608 : uint8_t tcg_opaque[1];
1609 : uint8_t tcg_align[1];
1610 : spdm_svh_dmtf_dsp_header_t dmtf_dsp_header;
1611 : uint16_t dmtf_dsp_opaque_len;
1612 : uint8_t dmtf_dsp_opaque[11];
1613 : uint8_t dmtf_dsp_align[3];
1614 : spdm_svh_dmtf_header_t dmtf_sm_ver_sel_header;
1615 : uint16_t dmtf_sm_ver_sel_opaque_len;
1616 : secured_message_opaque_element_version_selection_t dmtf_sm_ver_sel_opaque;
1617 : /* uint8_t dmtf_sm_ver_sel_align[0]; */
1618 : spdm_svh_dmtf_header_t dmtf_sm_sup_ver_header;
1619 : uint16_t dmtf_sm_sup_ver_opaque_len;
1620 : secured_message_opaque_element_supported_version_t dmtf_sm_sup_ver_opaque;
1621 : spdm_version_number_t dmtf_sm_sup_ver_versions_list[3];
1622 : uint8_t dmtf_sm_sup_ver_align[3];
1623 : } test_spdm12_opaque_data_table_t;
1624 :
1625 : #pragma pack()
1626 :
1627 1 : static void libspdm_test_process_opaque_data_case22(void **state)
1628 : {
1629 : libspdm_return_t status;
1630 : libspdm_test_context_t *spdm_test_context;
1631 : libspdm_context_t *spdm_context;
1632 : const void *get_element_ptr;
1633 : size_t get_element_len;
1634 : size_t opaque_data_size;
1635 : uint8_t *opaque_data_ptr;
1636 : test_spdm12_opaque_data_table_t opaque_data;
1637 :
1638 1 : spdm_test_context = *state;
1639 1 : spdm_context = spdm_test_context->spdm_context;
1640 1 : spdm_test_context->case_id = 0x16;
1641 :
1642 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_12 <<
1643 : SPDM_VERSION_NUMBER_SHIFT_BIT;
1644 :
1645 1 : spdm_context->local_context.secured_message_version.spdm_version_count = 1;
1646 :
1647 1 : libspdm_set_mem ((uint8_t *)&opaque_data, sizeof(opaque_data), 0xFF);
1648 1 : opaque_data.opaque_header.total_elements = SPDM_REGISTRY_ID_MAX + 2;
1649 1 : opaque_data.cbor_header.header.id = SPDM_REGISTRY_ID_IANA_CBOR;
1650 1 : opaque_data.cbor_header.header.vendor_id_len = sizeof(opaque_data.cbor_vendor_id);
1651 1 : opaque_data.cbor_opaque_len = sizeof(opaque_data.cbor_opaque);
1652 1 : opaque_data.vesa_header.header.id = SPDM_REGISTRY_ID_VESA;
1653 1 : opaque_data.vesa_header.header.vendor_id_len = 0;
1654 1 : opaque_data.vesa_opaque_len = sizeof(opaque_data.vesa_opaque);
1655 1 : opaque_data.jedec_header.header.id = SPDM_REGISTRY_ID_JEDEC;
1656 1 : opaque_data.jedec_header.header.vendor_id_len = sizeof(opaque_data.jedec_header.vendor_id);
1657 1 : opaque_data.jedec_opaque_len = sizeof(opaque_data.jedec_opaque);
1658 1 : opaque_data.cxl_header.header.id = SPDM_REGISTRY_ID_CXL;
1659 1 : opaque_data.cxl_header.header.vendor_id_len = sizeof(opaque_data.cxl_header.vendor_id);
1660 1 : opaque_data.cxl_opaque_len = sizeof(opaque_data.cxl_opaque);
1661 1 : opaque_data.mipi_header.header.id = SPDM_REGISTRY_ID_MIPI;
1662 1 : opaque_data.mipi_header.header.vendor_id_len = sizeof(opaque_data.mipi_header.vendor_id);
1663 1 : opaque_data.mipi_opaque_len = sizeof(opaque_data.mipi_opaque);
1664 1 : opaque_data.hdbaset_header.header.id = SPDM_REGISTRY_ID_HDBASET;
1665 1 : opaque_data.hdbaset_header.header.vendor_id_len = sizeof(opaque_data.hdbaset_header.vendor_id);
1666 1 : opaque_data.hdbaset_opaque_len = sizeof(opaque_data.hdbaset_opaque);
1667 1 : opaque_data.iana_header.header.id = SPDM_REGISTRY_ID_IANA;
1668 1 : opaque_data.iana_header.header.vendor_id_len = sizeof(opaque_data.iana_header.vendor_id);
1669 1 : opaque_data.iana_opaque_len = sizeof(opaque_data.iana_opaque);
1670 1 : opaque_data.pcisig_header.header.id = SPDM_REGISTRY_ID_PCISIG;
1671 1 : opaque_data.pcisig_header.header.vendor_id_len = sizeof(opaque_data.pcisig_header.vendor_id);
1672 1 : opaque_data.pcisig_opaque_len = sizeof(opaque_data.pcisig_opaque);
1673 1 : opaque_data.usb_header.header.id = SPDM_REGISTRY_ID_USB;
1674 1 : opaque_data.usb_header.header.vendor_id_len = sizeof(opaque_data.usb_header.vendor_id);
1675 1 : opaque_data.usb_opaque_len = sizeof(opaque_data.usb_opaque);
1676 1 : opaque_data.tcg_header.header.id = SPDM_REGISTRY_ID_TCG;
1677 1 : opaque_data.tcg_header.header.vendor_id_len = sizeof(opaque_data.tcg_header.vendor_id);
1678 1 : opaque_data.tcg_opaque_len = sizeof(opaque_data.tcg_opaque);
1679 1 : opaque_data.dmtf_dsp_header.header.id = SPDM_REGISTRY_ID_DMTF_DSP;
1680 1 : opaque_data.dmtf_dsp_header.header.vendor_id_len = sizeof(opaque_data.dmtf_dsp_header.vendor_id);
1681 1 : opaque_data.dmtf_dsp_opaque_len = sizeof(opaque_data.dmtf_dsp_opaque);
1682 1 : opaque_data.dmtf_sm_ver_sel_header.header.id = SPDM_REGISTRY_ID_DMTF;
1683 1 : opaque_data.dmtf_sm_ver_sel_header.header.vendor_id_len = 0;
1684 1 : opaque_data.dmtf_sm_ver_sel_opaque_len = sizeof(opaque_data.dmtf_sm_ver_sel_opaque);
1685 1 : opaque_data.dmtf_sm_ver_sel_opaque.sm_data_version =
1686 : SECURED_MESSAGE_OPAQUE_ELEMENT_SMDATA_DATA_VERSION;
1687 1 : opaque_data.dmtf_sm_ver_sel_opaque.sm_data_id =
1688 : SECURED_MESSAGE_OPAQUE_ELEMENT_SMDATA_ID_VERSION_SELECTION;
1689 1 : opaque_data.dmtf_sm_ver_sel_opaque.selected_version = SECURED_SPDM_VERSION_12 << 8;
1690 1 : opaque_data.dmtf_sm_sup_ver_header.header.id = SPDM_REGISTRY_ID_DMTF;
1691 1 : opaque_data.dmtf_sm_sup_ver_header.header.vendor_id_len = 0;
1692 1 : opaque_data.dmtf_sm_sup_ver_opaque_len = sizeof(opaque_data.dmtf_sm_sup_ver_opaque) +
1693 : sizeof(opaque_data.dmtf_sm_sup_ver_versions_list);
1694 1 : opaque_data.dmtf_sm_sup_ver_opaque.sm_data_version =
1695 : SECURED_MESSAGE_OPAQUE_ELEMENT_SMDATA_DATA_VERSION;
1696 1 : opaque_data.dmtf_sm_sup_ver_opaque.sm_data_id =
1697 : SECURED_MESSAGE_OPAQUE_ELEMENT_SMDATA_ID_SUPPORTED_VERSION;
1698 1 : opaque_data.dmtf_sm_sup_ver_opaque.version_count =
1699 : LIBSPDM_ARRAY_SIZE(opaque_data.dmtf_sm_sup_ver_versions_list);
1700 1 : opaque_data.dmtf_sm_sup_ver_versions_list[0] = SECURED_SPDM_VERSION_10 << 8;
1701 1 : opaque_data.dmtf_sm_sup_ver_versions_list[1] = SECURED_SPDM_VERSION_11 << 8;
1702 1 : opaque_data.dmtf_sm_sup_ver_versions_list[2] = SECURED_SPDM_VERSION_12 << 8;
1703 :
1704 1 : opaque_data_ptr = (uint8_t *)&opaque_data;
1705 1 : opaque_data_size = sizeof(opaque_data);
1706 1 : status = libspdm_get_element_from_opaque_data(spdm_context,
1707 : opaque_data_size, opaque_data_ptr,
1708 : SPDM_REGISTRY_ID_DMTF,
1709 : SECURED_MESSAGE_OPAQUE_ELEMENT_SMDATA_ID_VERSION_SELECTION,
1710 : &get_element_ptr, &get_element_len
1711 : );
1712 1 : assert_int_equal (status, true);
1713 1 : status = libspdm_get_element_from_opaque_data(spdm_context,
1714 : opaque_data_size, opaque_data_ptr,
1715 : SPDM_REGISTRY_ID_DMTF,
1716 : SECURED_MESSAGE_OPAQUE_ELEMENT_SMDATA_ID_SUPPORTED_VERSION,
1717 : &get_element_ptr, &get_element_len
1718 : );
1719 1 : assert_int_equal (status, true);
1720 1 : }
1721 :
1722 : static libspdm_test_context_t m_libspdm_common_context_data_test_context = {
1723 : LIBSPDM_TEST_CONTEXT_VERSION,
1724 : true,
1725 : NULL,
1726 : NULL,
1727 : };
1728 :
1729 1 : int libspdm_common_context_data_test_main(void)
1730 : {
1731 1 : const struct CMUnitTest spdm_common_context_data_tests[] = {
1732 : cmocka_unit_test(libspdm_test_common_context_data_case1),
1733 : cmocka_unit_test(libspdm_test_common_context_data_case2),
1734 : cmocka_unit_test(libspdm_test_common_context_data_case3),
1735 : cmocka_unit_test(libspdm_test_common_context_data_case4),
1736 :
1737 : cmocka_unit_test(libspdm_test_verify_peer_cert_chain_buffer_case5),
1738 : cmocka_unit_test(libspdm_test_verify_peer_cert_chain_buffer_case6),
1739 : cmocka_unit_test(libspdm_test_verify_peer_cert_chain_buffer_case7),
1740 : cmocka_unit_test(libspdm_test_verify_peer_cert_chain_buffer_case8),
1741 :
1742 : cmocka_unit_test(libspdm_test_set_data_case9),
1743 :
1744 : /* Successful response V1.1 for multi element opaque data supported version, element number is 2*/
1745 : cmocka_unit_test(libspdm_test_process_opaque_data_supported_version_data_case10),
1746 : /* Failed response V1.1 for multi element opaque data supported version, element id is wrong*/
1747 : cmocka_unit_test(libspdm_test_process_opaque_data_supported_version_data_case11),
1748 : /* Successful response V1.2 for multi element opaque data supported version, element number is 2*/
1749 : cmocka_unit_test(libspdm_test_process_opaque_data_supported_version_data_case12),
1750 : /* Failed response V1.2 for multi element opaque data supported version, element id is wrong*/
1751 : cmocka_unit_test(libspdm_test_process_opaque_data_supported_version_data_case13),
1752 : /* Successful response V1.1 for multi element opaque data selection version, element number is 2*/
1753 : cmocka_unit_test(libspdm_test_process_opaque_data_selection_version_data_case14),
1754 : /* Failed response V1.1 for multi element opaque data selection version, element number is wrong*/
1755 : cmocka_unit_test(libspdm_test_process_opaque_data_selection_version_data_case15),
1756 : /* Successful response V1.2 for multi element opaque data selection version, element number is 2*/
1757 : cmocka_unit_test(libspdm_test_process_opaque_data_selection_version_data_case16),
1758 : /* Failed response V1.2 for multi element opaque data selection version, element number is wrong*/
1759 : cmocka_unit_test(libspdm_test_process_opaque_data_selection_version_data_case17),
1760 :
1761 : /* Successful initialization and setting of secured message context location. */
1762 : cmocka_unit_test(libspdm_test_secured_message_context_location_selection_case18),
1763 :
1764 : /* Test that the Export Master Secret can be exported and cleared. */
1765 : cmocka_unit_test(libspdm_test_export_master_secret_case19),
1766 : cmocka_unit_test(libspdm_test_check_context_case20),
1767 :
1768 : /* Test the max DHE/PSK session count */
1769 : cmocka_unit_test(libspdm_test_max_session_count_case21),
1770 :
1771 : /* Successful response V1.2 for multi element */
1772 : cmocka_unit_test(libspdm_test_process_opaque_data_case22),
1773 : };
1774 :
1775 1 : libspdm_setup_test_context(&m_libspdm_common_context_data_test_context);
1776 :
1777 1 : return cmocka_run_group_tests(spdm_common_context_data_tests,
1778 : libspdm_unit_test_group_setup,
1779 : libspdm_unit_test_group_teardown);
1780 : }
|