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