Line data Source code
1 : /**
2 : * Copyright Notice:
3 : * Copyright 2021-2022 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 36 : 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 36 : if (libspdm_get_connection_version (spdm_context) >= SPDM_MESSAGE_VERSION_12) {
77 18 : size = sizeof(spdm_general_opaque_data_table_header_t);
78 60 : for (element_index = 0; element_index < element_num; element_index++) {
79 42 : size += sizeof(secured_message_opaque_element_table_header_t) +
80 : sizeof(secured_message_opaque_element_supported_version_t) +
81 42 : sizeof(spdm_version_number_t) * version_count;
82 : /* Add Padding*/
83 42 : size = (size + 3) & ~3;
84 : }
85 : } else {
86 18 : size = sizeof(secured_message_general_opaque_data_table_header_t);
87 60 : for (element_index = 0; element_index < element_num; element_index++) {
88 42 : size += sizeof(secured_message_opaque_element_table_header_t) +
89 : sizeof(secured_message_opaque_element_supported_version_t) +
90 42 : sizeof(spdm_version_number_t) * version_count;
91 : /* Add Padding*/
92 42 : size = (size + 3) & ~3;
93 : }
94 : }
95 :
96 36 : 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 32 : for (element_index = 0; element_index < element_num; element_index++) {
166 : /*id is changed with element_index*/
167 28 : opaque_element_table_header->id = element_index;
168 28 : opaque_element_table_header->vendor_len = 0;
169 28 : opaque_element_table_header->opaque_element_data_len =
170 28 : sizeof(secured_message_opaque_element_supported_version_t) +
171 28 : sizeof(spdm_version_number_t) *
172 28 : spdm_context->local_context.secured_message_version.spdm_version_count;
173 :
174 28 : opaque_element_support_version =
175 : (void *)(opaque_element_table_header + 1);
176 28 : opaque_element_support_version->sm_data_version =
177 : SECURED_MESSAGE_OPAQUE_ELEMENT_SMDATA_DATA_VERSION;
178 28 : opaque_element_support_version->sm_data_id =
179 : SECURED_MESSAGE_OPAQUE_ELEMENT_SMDATA_ID_SUPPORTED_VERSION;
180 28 : opaque_element_support_version->version_count =
181 28 : spdm_context->local_context.secured_message_version.spdm_version_count;
182 :
183 28 : versions_list = (void *)(opaque_element_support_version + 1);
184 :
185 28 : libspdm_copy_mem(versions_list,
186 28 : *data_out_size - ((uint8_t*)versions_list - (uint8_t*)data_out),
187 28 : spdm_context->local_context.secured_message_version.spdm_version,
188 28 : spdm_context->local_context.secured_message_version.spdm_version_count *
189 : sizeof(spdm_version_number_t));
190 :
191 : /*move to next element*/
192 28 : if (libspdm_get_connection_version (spdm_context) >= SPDM_MESSAGE_VERSION_12) {
193 14 : opaque_element_table_header =
194 : (secured_message_opaque_element_table_header_t *)(
195 : (uint8_t *)opaque_element_table_header +
196 14 : libspdm_get_multi_element_opaque_data_supported_version_data_size(
197 : spdm_context,
198 14 : spdm_context->local_context.secured_message_version.spdm_version_count,
199 14 : 1) -
200 : sizeof(spdm_general_opaque_data_table_header_t));
201 : } else {
202 14 : opaque_element_table_header =
203 : (secured_message_opaque_element_table_header_t *)(
204 : (uint8_t *)opaque_element_table_header +
205 14 : libspdm_get_multi_element_opaque_data_supported_version_data_size(
206 : spdm_context,
207 14 : spdm_context->local_context.secured_message_version.spdm_version_count,
208 14 : 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 28 : end = versions_list +
214 28 : spdm_context->local_context.secured_message_version.spdm_version_count;
215 28 : 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 32 : for (element_index = 0; element_index < element_num; element_index++) {
244 28 : size += sizeof(secured_message_opaque_element_table_header_t) +
245 : sizeof(secured_message_opaque_element_version_selection_t);
246 : /* Add Padding*/
247 28 : size = (size + 3) & ~3;
248 : }
249 : } else {
250 4 : size = sizeof(secured_message_general_opaque_data_table_header_t);
251 32 : for (element_index = 0; element_index < element_num; element_index++) {
252 28 : size += sizeof(secured_message_opaque_element_table_header_t) +
253 : sizeof(secured_message_opaque_element_version_selection_t);
254 : /* Add Padding*/
255 28 : 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 32 : for (element_index = 0; element_index < element_num; element_index++) {
329 : /*id is changed with element_index*/
330 28 : opaque_element_table_header->id = element_index;
331 28 : opaque_element_table_header->vendor_len = 0;
332 28 : opaque_element_table_header->opaque_element_data_len =
333 : sizeof(secured_message_opaque_element_version_selection_t);
334 :
335 28 : opaque_element_version_section = (void *)(opaque_element_table_header + 1);
336 28 : opaque_element_version_section->sm_data_version =
337 : SECURED_MESSAGE_OPAQUE_ELEMENT_SMDATA_DATA_VERSION;
338 28 : opaque_element_version_section->sm_data_id =
339 : SECURED_MESSAGE_OPAQUE_ELEMENT_SMDATA_ID_VERSION_SELECTION;
340 28 : opaque_element_version_section->selected_version =
341 28 : spdm_context->connection_info.secured_message_version;
342 :
343 : /*move to next element*/
344 28 : current_element_len = sizeof(secured_message_opaque_element_table_header_t) +
345 28 : opaque_element_table_header->opaque_element_data_len;
346 : /* Add Padding*/
347 28 : current_element_len = (current_element_len + 3) & ~3;
348 :
349 28 : 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 : 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 1 : libspdm_x509_get_cert_from_cert_chain((uint8_t *)data + sizeof(spdm_cert_chain_t) + hash_size,
573 1 : data_size - sizeof(spdm_cert_chain_t) - hash_size, 0,
574 : &root_cert, &root_cert_size);
575 :
576 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
577 1 : spdm_context->connection_info.algorithm.base_asym_algo= m_libspdm_use_asym_algo;
578 1 : spdm_context->local_context.is_requester = true;
579 :
580 : /*clear root cert array*/
581 11 : for (root_cert_index = 0; root_cert_index < LIBSPDM_MAX_ROOT_CERT_SUPPORT; root_cert_index++) {
582 10 : spdm_context->local_context.peer_root_cert_provision_size[root_cert_index] = 0;
583 10 : spdm_context->local_context.peer_root_cert_provision[root_cert_index] = NULL;
584 : }
585 1 : result = libspdm_verify_peer_cert_chain_buffer(spdm_context, data, data_size, &trust_anchor,
586 : &trust_anchor_size);
587 1 : assert_int_equal (result, true);
588 :
589 1 : free(data);
590 1 : }
591 :
592 : /**
593 : * Test 6: There is one root cert. And the root cert has two case: match root cert, mismatch root cert.
594 : *
595 : * case Expected Behavior
596 : * there is one match root cert; return false
597 : * there is one mismatch root cert; return true, and the return trust_anchor is root cert.
598 : **/
599 1 : void libspdm_test_verify_peer_cert_chain_buffer_case6(void **state)
600 : {
601 : libspdm_test_context_t *spdm_test_context;
602 : libspdm_context_t *spdm_context;
603 : void *data;
604 : size_t data_size;
605 : void *hash;
606 : size_t hash_size;
607 : const uint8_t *root_cert;
608 : size_t root_cert_size;
609 :
610 : void *data_test;
611 : size_t data_size_test;
612 : void *hash_test;
613 : size_t hash_size_test;
614 : const uint8_t *root_cert_test;
615 : size_t root_cert_size_test;
616 1 : uint32_t m_libspdm_use_asym_algo_test =SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSASSA_2048;
617 :
618 : const void *trust_anchor;
619 : size_t trust_anchor_size;
620 : bool result;
621 : uint8_t root_cert_index;
622 :
623 1 : spdm_test_context = *state;
624 1 : spdm_context = spdm_test_context->spdm_context;
625 1 : spdm_test_context->case_id = 0x6;
626 : /* Setting SPDM context as the first steps of the protocol has been accomplished*/
627 1 : spdm_context->connection_info.connection_state =
628 : LIBSPDM_CONNECTION_STATE_AFTER_DIGESTS;
629 1 : spdm_context->connection_info.capability.flags |=
630 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP;
631 1 : spdm_context->local_context.is_requester = true;
632 :
633 : /* Loading Root certificate and saving its hash*/
634 1 : libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
635 : m_libspdm_use_asym_algo, &data,
636 : &data_size, &hash, &hash_size);
637 1 : libspdm_x509_get_cert_from_cert_chain((uint8_t *)data + sizeof(spdm_cert_chain_t) + hash_size,
638 1 : data_size - sizeof(spdm_cert_chain_t) - hash_size, 0,
639 : &root_cert, &root_cert_size);
640 : /* Loading Other test Root certificate and saving its hash*/
641 1 : libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
642 : m_libspdm_use_asym_algo_test, &data_test,
643 : &data_size_test, &hash_test, &hash_size_test);
644 1 : libspdm_x509_get_cert_from_cert_chain(
645 1 : (uint8_t *)data_test + sizeof(spdm_cert_chain_t) + hash_size_test,
646 1 : data_size_test - sizeof(spdm_cert_chain_t) - hash_size_test, 0,
647 : &root_cert_test, &root_cert_size_test);
648 :
649 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
650 1 : spdm_context->connection_info.algorithm.base_asym_algo= m_libspdm_use_asym_algo;
651 :
652 : /*clear root cert array*/
653 11 : for (root_cert_index = 0; root_cert_index < LIBSPDM_MAX_ROOT_CERT_SUPPORT; root_cert_index++) {
654 10 : spdm_context->local_context.peer_root_cert_provision_size[root_cert_index] = 0;
655 10 : spdm_context->local_context.peer_root_cert_provision[root_cert_index] = NULL;
656 : }
657 :
658 : /*case: match root cert case*/
659 1 : spdm_context->local_context.peer_root_cert_provision_size[0] =root_cert_size_test;
660 1 : spdm_context->local_context.peer_root_cert_provision[0] = root_cert_test;
661 1 : result = libspdm_verify_peer_cert_chain_buffer(spdm_context, data, data_size, &trust_anchor,
662 : &trust_anchor_size);
663 1 : assert_int_equal (result, false);
664 :
665 : /*case: mismatch root cert case*/
666 1 : spdm_context->local_context.peer_root_cert_provision_size[0] =root_cert_size;
667 1 : spdm_context->local_context.peer_root_cert_provision[0] = root_cert;
668 1 : result = libspdm_verify_peer_cert_chain_buffer(spdm_context, data, data_size, &trust_anchor,
669 : &trust_anchor_size);
670 1 : assert_int_equal (result, true);
671 1 : assert_ptr_equal (trust_anchor, root_cert);
672 :
673 1 : free(data);
674 1 : free(data_test);
675 1 : }
676 :
677 : /**
678 : * Test 7: There are LIBSPDM_MAX_ROOT_CERT_SUPPORT/2 root cert.
679 : *
680 : * case Expected Behavior
681 : * there is no match root cert; return false
682 : * there is one match root cert in the end; return true, and the return trust_anchor is root cert.
683 : * there is one match root cert in the middle; return true, and the return trust_anchor is root cert.
684 : **/
685 1 : void libspdm_test_verify_peer_cert_chain_buffer_case7(void **state)
686 : {
687 : libspdm_test_context_t *spdm_test_context;
688 : libspdm_context_t *spdm_context;
689 : void *data;
690 : size_t data_size;
691 : void *hash;
692 : size_t hash_size;
693 : const uint8_t *root_cert;
694 : size_t root_cert_size;
695 :
696 : void *data_test;
697 : size_t data_size_test;
698 : void *hash_test;
699 : size_t hash_size_test;
700 : const uint8_t *root_cert_test;
701 : size_t root_cert_size_test;
702 1 : uint32_t m_libspdm_use_asym_algo_test =SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSASSA_2048;
703 :
704 : const void *trust_anchor;
705 : size_t trust_anchor_size;
706 : bool result;
707 : uint8_t root_cert_index;
708 :
709 1 : spdm_test_context = *state;
710 1 : spdm_context = spdm_test_context->spdm_context;
711 1 : spdm_test_context->case_id = 0x7;
712 : /* Setting SPDM context as the first steps of the protocol has been accomplished*/
713 1 : spdm_context->connection_info.connection_state =
714 : LIBSPDM_CONNECTION_STATE_AFTER_DIGESTS;
715 1 : spdm_context->connection_info.capability.flags |=
716 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP;
717 1 : spdm_context->local_context.is_requester = true;
718 : /* Loading Root certificate and saving its hash*/
719 1 : libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
720 : m_libspdm_use_asym_algo, &data,
721 : &data_size, &hash, &hash_size);
722 1 : libspdm_x509_get_cert_from_cert_chain((uint8_t *)data + sizeof(spdm_cert_chain_t) + hash_size,
723 1 : data_size - sizeof(spdm_cert_chain_t) - hash_size, 0,
724 : &root_cert, &root_cert_size);
725 : /* Loading Other test Root certificate and saving its hash*/
726 1 : libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
727 : m_libspdm_use_asym_algo_test, &data_test,
728 : &data_size_test, &hash_test, &hash_size_test);
729 1 : libspdm_x509_get_cert_from_cert_chain(
730 1 : (uint8_t *)data_test + sizeof(spdm_cert_chain_t) + hash_size_test,
731 1 : data_size_test - sizeof(spdm_cert_chain_t) - hash_size_test, 0,
732 : &root_cert_test, &root_cert_size_test);
733 :
734 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
735 1 : spdm_context->connection_info.algorithm.base_asym_algo= m_libspdm_use_asym_algo;
736 :
737 : /*clear root cert array*/
738 11 : for (root_cert_index = 0; root_cert_index < LIBSPDM_MAX_ROOT_CERT_SUPPORT; root_cert_index++) {
739 10 : spdm_context->local_context.peer_root_cert_provision_size[root_cert_index] = 0;
740 10 : spdm_context->local_context.peer_root_cert_provision[root_cert_index] = NULL;
741 : }
742 :
743 : /*case: there is no match root cert*/
744 6 : for (root_cert_index = 0; root_cert_index < (LIBSPDM_MAX_ROOT_CERT_SUPPORT / 2);
745 5 : root_cert_index++) {
746 5 : spdm_context->local_context.peer_root_cert_provision_size[root_cert_index] =
747 : root_cert_size_test;
748 5 : spdm_context->local_context.peer_root_cert_provision[root_cert_index] = root_cert_test;
749 : }
750 1 : result = libspdm_verify_peer_cert_chain_buffer(spdm_context, data, data_size, &trust_anchor,
751 : &trust_anchor_size);
752 1 : assert_int_equal (result, false);
753 :
754 : /*case: there is no match root cert in the end*/
755 : spdm_context->local_context.peer_root_cert_provision_size[LIBSPDM_MAX_ROOT_CERT_SUPPORT / 2 -
756 1 : 1] =root_cert_size;
757 : spdm_context->local_context.peer_root_cert_provision[LIBSPDM_MAX_ROOT_CERT_SUPPORT / 2 -
758 1 : 1] = root_cert;
759 1 : result = libspdm_verify_peer_cert_chain_buffer(spdm_context, data, data_size, &trust_anchor,
760 : &trust_anchor_size);
761 1 : assert_int_equal (result, true);
762 1 : assert_ptr_equal (trust_anchor, root_cert);
763 :
764 : /*case: there is no match root cert in the middle*/
765 : spdm_context->local_context.peer_root_cert_provision_size[LIBSPDM_MAX_ROOT_CERT_SUPPORT /
766 1 : 4] =root_cert_size;
767 : spdm_context->local_context.peer_root_cert_provision[LIBSPDM_MAX_ROOT_CERT_SUPPORT /
768 1 : 4] = root_cert;
769 1 : result = libspdm_verify_peer_cert_chain_buffer(spdm_context, data, data_size, &trust_anchor,
770 : &trust_anchor_size);
771 1 : assert_int_equal (result, true);
772 1 : assert_ptr_equal (trust_anchor, root_cert);
773 :
774 1 : free(data);
775 1 : free(data_test);
776 1 : }
777 :
778 :
779 : /**
780 : * Test 8: There are full(LIBSPDM_MAX_ROOT_CERT_SUPPORT - 1) root cert.
781 : *
782 : * case Expected Behavior
783 : * there is no match root cert; return false
784 : * there is one match root cert in the end; return true, and the return trust_anchor is root cert.
785 : * there is one match root cert in the middle; return true, and the return trust_anchor is root cert.
786 : **/
787 1 : void libspdm_test_verify_peer_cert_chain_buffer_case8(void **state)
788 : {
789 : libspdm_test_context_t *spdm_test_context;
790 : libspdm_context_t *spdm_context;
791 : void *data;
792 : size_t data_size;
793 : void *hash;
794 : size_t hash_size;
795 : const uint8_t *root_cert;
796 : size_t root_cert_size;
797 :
798 : void *data_test;
799 : size_t data_size_test;
800 : void *hash_test;
801 : size_t hash_size_test;
802 : const uint8_t *root_cert_test;
803 : size_t root_cert_size_test;
804 1 : uint32_t m_libspdm_use_asym_algo_test =SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSASSA_2048;
805 :
806 : const void *trust_anchor;
807 : size_t trust_anchor_size;
808 : bool result;
809 : uint8_t root_cert_index;
810 :
811 1 : spdm_test_context = *state;
812 1 : spdm_context = spdm_test_context->spdm_context;
813 1 : spdm_test_context->case_id = 0x8;
814 : /* Setting SPDM context as the first steps of the protocol has been accomplished*/
815 1 : spdm_context->connection_info.connection_state =
816 : LIBSPDM_CONNECTION_STATE_AFTER_DIGESTS;
817 1 : spdm_context->connection_info.capability.flags |=
818 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP;
819 1 : spdm_context->local_context.is_requester = true;
820 : /* Loading Root certificate and saving its hash*/
821 1 : libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
822 : m_libspdm_use_asym_algo, &data,
823 : &data_size, &hash, &hash_size);
824 1 : libspdm_x509_get_cert_from_cert_chain((uint8_t *)data + sizeof(spdm_cert_chain_t) + hash_size,
825 1 : data_size - sizeof(spdm_cert_chain_t) - hash_size, 0,
826 : &root_cert, &root_cert_size);
827 : /* Loading Other test Root certificate and saving its hash*/
828 1 : libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
829 : m_libspdm_use_asym_algo_test, &data_test,
830 : &data_size_test, &hash_test, &hash_size_test);
831 1 : libspdm_x509_get_cert_from_cert_chain(
832 1 : (uint8_t *)data_test + sizeof(spdm_cert_chain_t) + hash_size_test,
833 1 : data_size_test - sizeof(spdm_cert_chain_t) - hash_size_test, 0,
834 : &root_cert_test, &root_cert_size_test);
835 :
836 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
837 1 : spdm_context->connection_info.algorithm.base_asym_algo= m_libspdm_use_asym_algo;
838 :
839 : /*case: there is no match root cert*/
840 11 : for (root_cert_index = 0; root_cert_index < LIBSPDM_MAX_ROOT_CERT_SUPPORT; root_cert_index++) {
841 10 : spdm_context->local_context.peer_root_cert_provision_size[root_cert_index] =
842 : root_cert_size_test;
843 10 : spdm_context->local_context.peer_root_cert_provision[root_cert_index] = root_cert_test;
844 : }
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, false);
848 :
849 : /*case: there is no match root cert in the end*/
850 : spdm_context->local_context.peer_root_cert_provision_size[LIBSPDM_MAX_ROOT_CERT_SUPPORT -
851 1 : 1] =root_cert_size;
852 : spdm_context->local_context.peer_root_cert_provision[LIBSPDM_MAX_ROOT_CERT_SUPPORT -
853 1 : 1] = root_cert;
854 1 : result = libspdm_verify_peer_cert_chain_buffer(spdm_context, data, data_size, &trust_anchor,
855 : &trust_anchor_size);
856 1 : assert_int_equal (result, true);
857 1 : assert_ptr_equal (trust_anchor, root_cert);
858 :
859 : /*case: there is no match root cert in the middle*/
860 11 : for (root_cert_index = 0; root_cert_index < LIBSPDM_MAX_ROOT_CERT_SUPPORT; root_cert_index++) {
861 10 : spdm_context->local_context.peer_root_cert_provision_size[root_cert_index] =
862 : root_cert_size_test;
863 10 : spdm_context->local_context.peer_root_cert_provision[root_cert_index] = root_cert_test;
864 : }
865 : spdm_context->local_context.peer_root_cert_provision_size[LIBSPDM_MAX_ROOT_CERT_SUPPORT /
866 1 : 2] =root_cert_size;
867 : spdm_context->local_context.peer_root_cert_provision[LIBSPDM_MAX_ROOT_CERT_SUPPORT /
868 1 : 2] = root_cert;
869 1 : result = libspdm_verify_peer_cert_chain_buffer(spdm_context, data, data_size, &trust_anchor,
870 : &trust_anchor_size);
871 1 : assert_int_equal (result, true);
872 1 : assert_ptr_equal (trust_anchor, root_cert);
873 :
874 1 : free(data);
875 1 : free(data_test);
876 1 : }
877 :
878 : /**
879 : * Test 9: test set data for root cert.
880 : *
881 : * case Expected Behavior
882 : * there is null root cert; return RETURN_SUCCESS, and the root cert is set successfully.
883 : * there is full root cert; return RETURN_OUT_OF_RESOURCES.
884 : **/
885 1 : static void libspdm_test_set_data_case9(void **state)
886 : {
887 : libspdm_return_t status;
888 : libspdm_test_context_t *spdm_test_context;
889 : libspdm_context_t *spdm_context;
890 : libspdm_data_parameter_t parameter;
891 :
892 : void *data;
893 : size_t data_size;
894 : void *hash;
895 : size_t hash_size;
896 : const uint8_t *root_cert;
897 : uint8_t root_cert_buffer[LIBSPDM_MAX_CERT_CHAIN_SIZE];
898 : size_t root_cert_size;
899 :
900 : uint8_t root_cert_index;
901 :
902 1 : spdm_test_context = *state;
903 1 : spdm_context = spdm_test_context->spdm_context;
904 1 : spdm_test_context->case_id = 0x9;
905 :
906 : /* Loading Root certificate and saving its hash*/
907 1 : libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
908 : m_libspdm_use_asym_algo, &data,
909 : &data_size, &hash, &hash_size);
910 1 : libspdm_x509_get_cert_from_cert_chain((uint8_t *)data + sizeof(spdm_cert_chain_t) + hash_size,
911 1 : data_size - sizeof(spdm_cert_chain_t) - hash_size, 0,
912 : &root_cert, &root_cert_size);
913 1 : memcpy(root_cert_buffer, root_cert, root_cert_size);
914 :
915 : /*case: there is null root cert*/
916 11 : for (root_cert_index = 0; root_cert_index < LIBSPDM_MAX_ROOT_CERT_SUPPORT; root_cert_index++) {
917 10 : spdm_context->local_context.peer_root_cert_provision_size[root_cert_index] = 0;
918 10 : spdm_context->local_context.peer_root_cert_provision[root_cert_index] = NULL;
919 : }
920 1 : parameter.location = LIBSPDM_DATA_LOCATION_LOCAL;
921 1 : status = libspdm_set_data(spdm_context, LIBSPDM_DATA_PEER_PUBLIC_ROOT_CERT,
922 : ¶meter, root_cert_buffer, root_cert_size);
923 1 : assert_int_equal (status, LIBSPDM_STATUS_SUCCESS);
924 1 : assert_int_equal (spdm_context->local_context.peer_root_cert_provision_size[0], root_cert_size);
925 1 : assert_ptr_equal (spdm_context->local_context.peer_root_cert_provision[0], root_cert_buffer);
926 :
927 : /*case: there is full root cert*/
928 11 : for (root_cert_index = 0; root_cert_index < LIBSPDM_MAX_ROOT_CERT_SUPPORT; root_cert_index++) {
929 10 : spdm_context->local_context.peer_root_cert_provision_size[root_cert_index] = root_cert_size;
930 10 : spdm_context->local_context.peer_root_cert_provision[root_cert_index] = root_cert_buffer;
931 : }
932 1 : status = libspdm_set_data(spdm_context, LIBSPDM_DATA_PEER_PUBLIC_ROOT_CERT,
933 : ¶meter, root_cert_buffer, root_cert_size);
934 1 : assert_int_equal (status, LIBSPDM_STATUS_BUFFER_FULL);
935 :
936 1 : free(data);
937 1 : }
938 :
939 :
940 : /**
941 : * Test 10: There is no root cert.
942 : * Expected Behavior: Return true result.
943 : **/
944 1 : void libspdm_test_process_opaque_data_supported_version_data_case10(void **state)
945 : {
946 : libspdm_return_t status;
947 : libspdm_test_context_t *spdm_test_context;
948 : libspdm_context_t *spdm_context;
949 : size_t opaque_data_size;
950 : uint8_t element_num;
951 :
952 1 : spdm_test_context = *state;
953 1 : spdm_context = spdm_test_context->spdm_context;
954 1 : spdm_test_context->case_id = 0xA;
955 :
956 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
957 : SPDM_VERSION_NUMBER_SHIFT_BIT;
958 :
959 1 : spdm_context->local_context.secured_message_version.spdm_version_count = 1;
960 :
961 1 : element_num = 2;
962 1 : opaque_data_size =
963 1 : libspdm_get_multi_element_opaque_data_supported_version_data_size(
964 : spdm_context,
965 1 : spdm_context->local_context.secured_message_version.spdm_version_count,
966 : element_num);
967 :
968 : uint8_t *opaque_data_ptr;
969 1 : opaque_data_ptr = malloc(opaque_data_size);
970 :
971 1 : libspdm_build_multi_element_opaque_data_supported_version_test(
972 : spdm_context, &opaque_data_size, opaque_data_ptr, element_num);
973 :
974 1 : status = libspdm_process_opaque_data_supported_version_data(spdm_context,
975 : opaque_data_size,
976 : opaque_data_ptr);
977 :
978 1 : assert_int_equal (status, LIBSPDM_STATUS_SUCCESS);
979 :
980 1 : free(opaque_data_ptr);
981 1 : }
982 :
983 1 : void libspdm_test_process_opaque_data_supported_version_data_case11(void **state)
984 : {
985 : libspdm_return_t status;
986 : libspdm_test_context_t *spdm_test_context;
987 : libspdm_context_t *spdm_context;
988 : size_t opaque_data_size;
989 : uint8_t element_num;
990 :
991 1 : spdm_test_context = *state;
992 1 : spdm_context = spdm_test_context->spdm_context;
993 1 : spdm_test_context->case_id = 0xB;
994 :
995 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
996 : SPDM_VERSION_NUMBER_SHIFT_BIT;
997 :
998 1 : spdm_context->local_context.secured_message_version.spdm_version_count = 1;
999 :
1000 : /*make element id wrong*/
1001 1 : element_num = SPDM_REGISTRY_ID_MAX + 2;
1002 1 : opaque_data_size =
1003 1 : libspdm_get_multi_element_opaque_data_supported_version_data_size(
1004 : spdm_context,
1005 1 : spdm_context->local_context.secured_message_version.spdm_version_count,
1006 : element_num);
1007 :
1008 : uint8_t *opaque_data_ptr;
1009 1 : opaque_data_ptr = malloc(opaque_data_size);
1010 :
1011 1 : libspdm_build_multi_element_opaque_data_supported_version_test(
1012 : spdm_context, &opaque_data_size, opaque_data_ptr, element_num);
1013 :
1014 1 : status = libspdm_process_opaque_data_supported_version_data(spdm_context,
1015 : opaque_data_size,
1016 : opaque_data_ptr);
1017 :
1018 1 : assert_int_equal (status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
1019 :
1020 1 : free(opaque_data_ptr);
1021 1 : }
1022 :
1023 1 : void libspdm_test_process_opaque_data_supported_version_data_case12(void **state)
1024 : {
1025 : libspdm_return_t status;
1026 : libspdm_test_context_t *spdm_test_context;
1027 : libspdm_context_t *spdm_context;
1028 : size_t opaque_data_size;
1029 : uint8_t element_num;
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 :
1057 1 : assert_int_equal (status, LIBSPDM_STATUS_SUCCESS);
1058 :
1059 1 : free(opaque_data_ptr);
1060 1 : }
1061 :
1062 1 : void libspdm_test_process_opaque_data_supported_version_data_case13(void **state)
1063 : {
1064 : libspdm_return_t status;
1065 : libspdm_test_context_t *spdm_test_context;
1066 : libspdm_context_t *spdm_context;
1067 : size_t opaque_data_size;
1068 : uint8_t element_num;
1069 :
1070 1 : spdm_test_context = *state;
1071 1 : spdm_context = spdm_test_context->spdm_context;
1072 1 : spdm_test_context->case_id = 0xD;
1073 :
1074 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_12 <<
1075 : SPDM_VERSION_NUMBER_SHIFT_BIT;
1076 :
1077 1 : spdm_context->local_context.secured_message_version.spdm_version_count = 1;
1078 :
1079 : /*make element id wrong*/
1080 1 : element_num = SPDM_REGISTRY_ID_MAX + 2;
1081 1 : opaque_data_size =
1082 1 : libspdm_get_multi_element_opaque_data_supported_version_data_size(
1083 : spdm_context,
1084 1 : spdm_context->local_context.secured_message_version.spdm_version_count,
1085 : element_num);
1086 :
1087 : uint8_t *opaque_data_ptr;
1088 1 : opaque_data_ptr = malloc(opaque_data_size);
1089 :
1090 1 : libspdm_build_multi_element_opaque_data_supported_version_test(
1091 : spdm_context, &opaque_data_size, opaque_data_ptr, element_num);
1092 :
1093 1 : status = libspdm_process_opaque_data_supported_version_data(spdm_context,
1094 : opaque_data_size,
1095 : opaque_data_ptr);
1096 :
1097 1 : assert_int_equal (status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
1098 :
1099 1 : free(opaque_data_ptr);
1100 1 : }
1101 :
1102 :
1103 1 : void libspdm_test_process_opaque_data_selection_version_data_case14(void **state)
1104 : {
1105 : libspdm_return_t status;
1106 : libspdm_test_context_t *spdm_test_context;
1107 : libspdm_context_t *spdm_context;
1108 : size_t opaque_data_size;
1109 : uint8_t element_num;
1110 :
1111 1 : spdm_test_context = *state;
1112 1 : spdm_context = spdm_test_context->spdm_context;
1113 1 : spdm_test_context->case_id = 0xE;
1114 :
1115 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
1116 : SPDM_VERSION_NUMBER_SHIFT_BIT;
1117 :
1118 1 : spdm_context->local_context.secured_message_version.spdm_version_count = 1;
1119 1 : spdm_context->connection_info.secured_message_version =
1120 : SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT;
1121 1 : spdm_context->local_context.secured_message_version.spdm_version[0] =
1122 : SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT;
1123 :
1124 1 : element_num = 2;
1125 1 : opaque_data_size =
1126 1 : libspdm_get_multi_element_opaque_data_version_selection_data_size(
1127 : spdm_context,
1128 : element_num);
1129 :
1130 : uint8_t *opaque_data_ptr;
1131 1 : opaque_data_ptr = malloc(opaque_data_size);
1132 :
1133 1 : libspdm_build_opaque_data_version_selection_data_test(
1134 : spdm_context, &opaque_data_size, opaque_data_ptr, element_num);
1135 :
1136 1 : status = libspdm_process_opaque_data_version_selection_data(spdm_context,
1137 : opaque_data_size,
1138 : opaque_data_ptr);
1139 :
1140 1 : assert_int_equal (status, LIBSPDM_STATUS_SUCCESS);
1141 :
1142 1 : free(opaque_data_ptr);
1143 1 : }
1144 :
1145 :
1146 1 : void libspdm_test_process_opaque_data_selection_version_data_case15(void **state)
1147 : {
1148 : libspdm_return_t status;
1149 : libspdm_test_context_t *spdm_test_context;
1150 : libspdm_context_t *spdm_context;
1151 : size_t opaque_data_size;
1152 : uint8_t element_num;
1153 :
1154 1 : spdm_test_context = *state;
1155 1 : spdm_context = spdm_test_context->spdm_context;
1156 1 : spdm_test_context->case_id = 0xF;
1157 :
1158 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
1159 : SPDM_VERSION_NUMBER_SHIFT_BIT;
1160 :
1161 1 : spdm_context->local_context.secured_message_version.spdm_version_count = 1;
1162 1 : spdm_context->connection_info.secured_message_version =
1163 : SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT;
1164 1 : spdm_context->local_context.secured_message_version.spdm_version[0] =
1165 : SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT;
1166 :
1167 : /*make element id wrong*/
1168 1 : element_num = SPDM_REGISTRY_ID_MAX + 2;
1169 1 : opaque_data_size =
1170 1 : libspdm_get_multi_element_opaque_data_version_selection_data_size(
1171 : spdm_context,
1172 : element_num);
1173 :
1174 : uint8_t *opaque_data_ptr;
1175 1 : opaque_data_ptr = malloc(opaque_data_size);
1176 :
1177 1 : libspdm_build_opaque_data_version_selection_data_test(
1178 : spdm_context, &opaque_data_size, opaque_data_ptr, element_num);
1179 :
1180 1 : status = libspdm_process_opaque_data_version_selection_data(spdm_context,
1181 : opaque_data_size,
1182 : opaque_data_ptr);
1183 :
1184 1 : assert_int_equal (status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
1185 :
1186 1 : free(opaque_data_ptr);
1187 1 : }
1188 :
1189 :
1190 1 : void libspdm_test_process_opaque_data_selection_version_data_case16(void **state)
1191 : {
1192 : libspdm_return_t status;
1193 : libspdm_test_context_t *spdm_test_context;
1194 : libspdm_context_t *spdm_context;
1195 : size_t opaque_data_size;
1196 : uint8_t element_num;
1197 :
1198 1 : spdm_test_context = *state;
1199 1 : spdm_context = spdm_test_context->spdm_context;
1200 1 : spdm_test_context->case_id = 0x10;
1201 :
1202 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_12 <<
1203 : SPDM_VERSION_NUMBER_SHIFT_BIT;
1204 :
1205 1 : spdm_context->local_context.secured_message_version.spdm_version_count = 1;
1206 1 : spdm_context->connection_info.secured_message_version =
1207 : SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT;
1208 1 : spdm_context->local_context.secured_message_version.spdm_version[0] =
1209 : SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT;
1210 :
1211 1 : element_num = 2;
1212 1 : opaque_data_size =
1213 1 : libspdm_get_multi_element_opaque_data_version_selection_data_size(
1214 : spdm_context,
1215 : element_num);
1216 :
1217 : uint8_t *opaque_data_ptr;
1218 1 : opaque_data_ptr = malloc(opaque_data_size);
1219 :
1220 1 : libspdm_build_opaque_data_version_selection_data_test(
1221 : spdm_context, &opaque_data_size, opaque_data_ptr, element_num);
1222 :
1223 1 : status = libspdm_process_opaque_data_version_selection_data(spdm_context,
1224 : opaque_data_size,
1225 : opaque_data_ptr);
1226 :
1227 1 : assert_int_equal (status, LIBSPDM_STATUS_SUCCESS);
1228 :
1229 1 : free(opaque_data_ptr);
1230 1 : }
1231 :
1232 1 : void libspdm_test_process_opaque_data_selection_version_data_case17(void **state)
1233 : {
1234 : libspdm_return_t status;
1235 : libspdm_test_context_t *spdm_test_context;
1236 : libspdm_context_t *spdm_context;
1237 : size_t opaque_data_size;
1238 : uint8_t element_num;
1239 :
1240 1 : spdm_test_context = *state;
1241 1 : spdm_context = spdm_test_context->spdm_context;
1242 1 : spdm_test_context->case_id = 0x11;
1243 :
1244 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_12 <<
1245 : SPDM_VERSION_NUMBER_SHIFT_BIT;
1246 :
1247 1 : spdm_context->local_context.secured_message_version.spdm_version_count = 1;
1248 1 : spdm_context->connection_info.secured_message_version =
1249 : SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT;
1250 1 : spdm_context->local_context.secured_message_version.spdm_version[0] =
1251 : SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT;
1252 :
1253 : /*make element id wrong*/
1254 1 : element_num = SPDM_REGISTRY_ID_MAX + 2;
1255 1 : opaque_data_size =
1256 1 : libspdm_get_multi_element_opaque_data_version_selection_data_size(
1257 : spdm_context,
1258 : element_num);
1259 :
1260 : uint8_t *opaque_data_ptr;
1261 1 : opaque_data_ptr = malloc(opaque_data_size);
1262 :
1263 1 : libspdm_build_opaque_data_version_selection_data_test(
1264 : spdm_context, &opaque_data_size, opaque_data_ptr, element_num);
1265 :
1266 1 : status = libspdm_process_opaque_data_version_selection_data(spdm_context,
1267 : opaque_data_size,
1268 : opaque_data_ptr);
1269 :
1270 1 : assert_int_equal (status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
1271 :
1272 1 : free(opaque_data_ptr);
1273 1 : }
1274 :
1275 1 : void libspdm_test_secured_message_context_location_selection_case18(void **state)
1276 : {
1277 : libspdm_return_t status;
1278 : libspdm_test_context_t *spdm_test_context;
1279 : libspdm_context_t *spdm_context;
1280 : void *secured_message_contexts[LIBSPDM_MAX_SESSION_COUNT];
1281 : size_t index;
1282 :
1283 1 : spdm_test_context = *state;
1284 1 : spdm_test_context->case_id = 0x12;
1285 :
1286 1 : spdm_context = (libspdm_context_t *)malloc(libspdm_get_context_size_without_secured_context());
1287 :
1288 5 : for (index = 0; index < LIBSPDM_MAX_SESSION_COUNT; index++)
1289 : {
1290 4 : secured_message_contexts[index] =
1291 4 : (void *)malloc(libspdm_secured_message_get_context_size());
1292 : }
1293 :
1294 1 : status = libspdm_init_context_with_secured_context(spdm_context, secured_message_contexts,
1295 : LIBSPDM_MAX_SESSION_COUNT);
1296 1 : assert_int_equal (status, LIBSPDM_STATUS_SUCCESS);
1297 :
1298 5 : for (index = 0; index < LIBSPDM_MAX_SESSION_COUNT; index++)
1299 : {
1300 : /* Ensure the SPDM context points to the specified memory. */
1301 4 : assert_ptr_equal(spdm_context->session_info[index].secured_message_context,
1302 : secured_message_contexts[index]);
1303 : }
1304 :
1305 1 : free(spdm_context);
1306 5 : for (index = 0; index < LIBSPDM_MAX_SESSION_COUNT; index++)
1307 : {
1308 4 : free(secured_message_contexts[index]);
1309 : }
1310 1 : }
1311 :
1312 1 : static void libspdm_test_export_master_secret_case19(void **state)
1313 : {
1314 : uint8_t target_buffer[LIBSPDM_MAX_HASH_SIZE];
1315 : bool result;
1316 : libspdm_secured_message_context_t secured_message_context;
1317 : size_t export_master_secret_size;
1318 :
1319 : /* Get the entire EMS when the reported size of the target buffer is larger than the size of the
1320 : * EMS. */
1321 65 : for (int index = 0; index < LIBSPDM_MAX_HASH_SIZE; index++) {
1322 64 : secured_message_context.export_master_secret[index] = (uint8_t)index;
1323 64 : target_buffer[index] = 0x00;
1324 : }
1325 :
1326 1 : secured_message_context.hash_size = LIBSPDM_MAX_HASH_SIZE;
1327 1 : export_master_secret_size = LIBSPDM_MAX_HASH_SIZE + 0x100;
1328 :
1329 1 : result = libspdm_secured_message_export_master_secret(&secured_message_context,
1330 : &target_buffer,
1331 : &export_master_secret_size);
1332 1 : assert_int_equal(result, true);
1333 :
1334 1 : libspdm_secured_message_clear_export_master_secret(&secured_message_context);
1335 :
1336 65 : for (int index = 0; index < LIBSPDM_MAX_HASH_SIZE; index++) {
1337 64 : assert_int_equal(target_buffer[index], index);
1338 64 : assert_int_equal(secured_message_context.export_master_secret[index], 0x00);
1339 : }
1340 1 : assert_int_equal(export_master_secret_size, LIBSPDM_MAX_HASH_SIZE);
1341 :
1342 : /* Get the entire EMS when the size of the target buffer is the same size as the EMS. */
1343 65 : for (int index = 0; index < LIBSPDM_MAX_HASH_SIZE; index++) {
1344 64 : secured_message_context.export_master_secret[index] = (uint8_t)index;
1345 64 : target_buffer[index] = 0x00;
1346 : }
1347 :
1348 1 : secured_message_context.hash_size = LIBSPDM_MAX_HASH_SIZE;
1349 1 : export_master_secret_size = LIBSPDM_MAX_HASH_SIZE;
1350 :
1351 1 : result = libspdm_secured_message_export_master_secret(&secured_message_context,
1352 : &target_buffer,
1353 : &export_master_secret_size);
1354 1 : assert_int_equal(result, true);
1355 :
1356 65 : for (int index = 0; index < LIBSPDM_MAX_HASH_SIZE; index++) {
1357 64 : assert_int_equal(target_buffer[index], index);
1358 : }
1359 1 : assert_int_equal(export_master_secret_size, LIBSPDM_MAX_HASH_SIZE);
1360 :
1361 : /* Get the truncated EMS when the size of the target buffer is less than the size of the EMS. */
1362 65 : for (int index = 0; index < LIBSPDM_MAX_HASH_SIZE; index++) {
1363 64 : secured_message_context.export_master_secret[index] = (uint8_t)index;
1364 64 : target_buffer[index] = 0x00;
1365 : }
1366 :
1367 1 : secured_message_context.hash_size = LIBSPDM_MAX_HASH_SIZE;
1368 1 : export_master_secret_size = LIBSPDM_MAX_HASH_SIZE - 4;
1369 :
1370 1 : result = libspdm_secured_message_export_master_secret(&secured_message_context,
1371 : &target_buffer,
1372 : &export_master_secret_size);
1373 1 : assert_int_equal(result, true);
1374 :
1375 65 : for (int index = 0; index < LIBSPDM_MAX_HASH_SIZE; index++) {
1376 64 : if (index < LIBSPDM_MAX_HASH_SIZE - 4) {
1377 60 : assert_int_equal(target_buffer[index], index);
1378 : } else {
1379 4 : assert_int_equal(target_buffer[index], 0x00);
1380 : }
1381 : }
1382 1 : assert_int_equal(export_master_secret_size, LIBSPDM_MAX_HASH_SIZE - 4);
1383 1 : }
1384 :
1385 1 : static void libspdm_test_check_context_case20(void **state)
1386 : {
1387 : void *context;
1388 : bool result;
1389 :
1390 1 : context = (void *)malloc (libspdm_get_context_size());
1391 :
1392 1 : libspdm_init_context (context);
1393 :
1394 1 : result = libspdm_check_context (context);
1395 1 : assert_int_equal(false, result);
1396 :
1397 1 : libspdm_register_transport_layer_func(context,
1398 : LIBSPDM_MAX_SPDM_MSG_SIZE,
1399 : LIBSPDM_TEST_TRANSPORT_HEADER_SIZE,
1400 : LIBSPDM_TEST_TRANSPORT_TAIL_SIZE,
1401 : libspdm_transport_test_encode_message,
1402 : libspdm_transport_test_decode_message);
1403 :
1404 1 : libspdm_register_device_buffer_func(context,
1405 : LIBSPDM_MAX_SENDER_RECEIVER_BUFFER_SIZE,
1406 : LIBSPDM_MAX_SENDER_RECEIVER_BUFFER_SIZE,
1407 : spdm_device_acquire_sender_buffer,
1408 : spdm_device_release_sender_buffer,
1409 : spdm_device_acquire_receiver_buffer,
1410 : spdm_device_release_receiver_buffer);
1411 :
1412 1 : result = libspdm_check_context (context);
1413 1 : assert_int_equal(true, result);
1414 :
1415 1 : libspdm_register_transport_layer_func(context,
1416 : SPDM_MIN_DATA_TRANSFER_SIZE_VERSION_12,
1417 : LIBSPDM_TEST_TRANSPORT_HEADER_SIZE,
1418 : LIBSPDM_TEST_TRANSPORT_TAIL_SIZE,
1419 : libspdm_transport_test_encode_message,
1420 : libspdm_transport_test_decode_message);
1421 :
1422 1 : result = libspdm_check_context (context);
1423 1 : assert_int_equal(false, result);
1424 1 : }
1425 :
1426 1 : static void libspdm_test_max_session_count_case21(void **state)
1427 : {
1428 : libspdm_context_t *spdm_context;
1429 : libspdm_data_parameter_t parameter;
1430 : size_t index;
1431 : size_t round;
1432 : uint16_t req_id;
1433 : uint16_t rsp_id;
1434 : uint32_t session_id;
1435 : void *session_info;
1436 : uint32_t dhe_session_count;
1437 : uint32_t psk_session_count;
1438 :
1439 7 : for (round = 0; round <= 5; round++) {
1440 : /* prepare parameter */
1441 6 : switch (round) {
1442 1 : case 0:
1443 1 : dhe_session_count = 1;
1444 1 : psk_session_count = 1;
1445 1 : break;
1446 1 : case 1:
1447 1 : dhe_session_count = LIBSPDM_MAX_SESSION_COUNT / 2;
1448 1 : psk_session_count = LIBSPDM_MAX_SESSION_COUNT - dhe_session_count;
1449 1 : break;
1450 1 : case 2:
1451 1 : dhe_session_count = 1;
1452 1 : psk_session_count = LIBSPDM_MAX_SESSION_COUNT - 1;
1453 1 : break;
1454 1 : case 3:
1455 1 : dhe_session_count = LIBSPDM_MAX_SESSION_COUNT - 1;
1456 1 : psk_session_count = 1;
1457 1 : break;
1458 1 : case 4:
1459 1 : dhe_session_count = 0;
1460 1 : psk_session_count = LIBSPDM_MAX_SESSION_COUNT;
1461 1 : break;
1462 1 : case 5:
1463 1 : dhe_session_count = LIBSPDM_MAX_SESSION_COUNT;
1464 1 : psk_session_count = 0;
1465 1 : break;
1466 0 : default:
1467 0 : dhe_session_count = 0;
1468 0 : psk_session_count = 0;
1469 0 : break;
1470 : }
1471 :
1472 : /* test */
1473 6 : spdm_context = (libspdm_context_t *)malloc(libspdm_get_context_size());
1474 6 : libspdm_init_context (spdm_context);
1475 6 : spdm_context->connection_info.capability.flags =
1476 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP |
1477 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
1478 6 : spdm_context->local_context.capability.flags =
1479 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP |
1480 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
1481 6 : spdm_context->connection_info.algorithm.base_hash_algo =
1482 : SPDM_ALGORITHMS_BASE_HASH_ALGO_TPM_ALG_SHA_256;
1483 6 : spdm_context->connection_info.algorithm.dhe_named_group =
1484 : SPDM_ALGORITHMS_DHE_NAMED_GROUP_SECP_256_R1;
1485 6 : spdm_context->connection_info.algorithm.aead_cipher_suite =
1486 : SPDM_ALGORITHMS_AEAD_CIPHER_SUITE_AES_256_GCM;
1487 6 : spdm_context->connection_info.algorithm.key_schedule =
1488 : SPDM_ALGORITHMS_KEY_SCHEDULE_SPDM;
1489 :
1490 6 : libspdm_zero_mem(¶meter, sizeof(parameter));
1491 6 : parameter.location = LIBSPDM_DATA_LOCATION_LOCAL;
1492 6 : if (dhe_session_count != 0) {
1493 5 : libspdm_set_data (spdm_context, LIBSPDM_DATA_MAX_DHE_SESSION_COUNT, ¶meter,
1494 : &dhe_session_count, sizeof(dhe_session_count));
1495 : }
1496 6 : if (psk_session_count != 0) {
1497 5 : libspdm_set_data (spdm_context, LIBSPDM_DATA_MAX_PSK_SESSION_COUNT, ¶meter,
1498 : &psk_session_count, sizeof(psk_session_count));
1499 : }
1500 :
1501 6 : if (dhe_session_count != 0) {
1502 16 : for (index = 0; index < dhe_session_count; index++)
1503 : {
1504 11 : req_id = libspdm_allocate_req_session_id (spdm_context, false);
1505 11 : assert_int_not_equal (req_id, INVALID_SESSION_ID & 0xFFFF);
1506 :
1507 11 : rsp_id = libspdm_allocate_rsp_session_id (spdm_context, false);
1508 11 : assert_int_not_equal (rsp_id, (INVALID_SESSION_ID & 0xFFFF0000) >> 16);
1509 :
1510 11 : session_id = libspdm_generate_session_id (req_id, rsp_id);
1511 11 : session_info = libspdm_assign_session_id (spdm_context, session_id, false);
1512 11 : assert_ptr_not_equal (session_info, NULL);
1513 : }
1514 5 : req_id = libspdm_allocate_req_session_id (spdm_context, false);
1515 5 : assert_int_equal (req_id, INVALID_SESSION_ID & 0xFFFF);
1516 :
1517 5 : rsp_id = libspdm_allocate_rsp_session_id (spdm_context, false);
1518 5 : assert_int_equal (rsp_id, (INVALID_SESSION_ID & 0xFFFF0000) >> 16);
1519 : }
1520 :
1521 6 : if (psk_session_count != 0) {
1522 16 : for (index = 0; index < psk_session_count; index++)
1523 : {
1524 11 : req_id = libspdm_allocate_req_session_id (spdm_context, true);
1525 11 : assert_int_not_equal (req_id, INVALID_SESSION_ID & 0xFFFF);
1526 :
1527 11 : rsp_id = libspdm_allocate_rsp_session_id (spdm_context, true);
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, true);
1532 11 : assert_ptr_not_equal (session_info, NULL);
1533 : }
1534 5 : req_id = libspdm_allocate_req_session_id (spdm_context, true);
1535 5 : assert_int_equal (req_id, INVALID_SESSION_ID & 0xFFFF);
1536 :
1537 5 : rsp_id = libspdm_allocate_rsp_session_id (spdm_context, true);
1538 5 : assert_int_equal (rsp_id, (INVALID_SESSION_ID & 0xFFFF0000) >> 16);
1539 : }
1540 :
1541 6 : free(spdm_context);
1542 : }
1543 1 : }
1544 :
1545 : #pragma pack(1)
1546 :
1547 : typedef struct {
1548 : spdm_general_opaque_data_table_header_t opaque_header;
1549 : spdm_svh_iana_cbor_header_t cbor_header;
1550 : uint8_t cbor_vendor_id[10];
1551 : uint16_t cbor_opaque_len;
1552 : uint8_t cbor_opaque[10];
1553 : /* uint8_t cbor_align[]; */
1554 : spdm_svh_vesa_header_t vesa_header;
1555 : uint16_t vesa_opaque_len;
1556 : uint8_t vesa_opaque[9];
1557 : uint8_t vesa_align[3];
1558 : spdm_svh_jedec_header_t jedec_header;
1559 : uint16_t jedec_opaque_len;
1560 : uint8_t jedec_opaque[8];
1561 : uint8_t jedec_align[2];
1562 : spdm_svh_cxl_header_t cxl_header;
1563 : uint16_t cxl_opaque_len;
1564 : uint8_t cxl_opaque[7];
1565 : uint8_t cxl_align[3];
1566 : spdm_svh_mipi_header_t mipi_header;
1567 : uint16_t mipi_opaque_len;
1568 : uint8_t mipi_opaque[6];
1569 : /* uint8_t mipi_align[0]; */
1570 : spdm_svh_hdbaset_header_t hdbaset_header;
1571 : uint16_t hdbaset_opaque_len;
1572 : uint8_t hdbaset_opaque[5];
1573 : uint8_t hdbaset_align[3];
1574 : spdm_svh_iana_header_t iana_header;
1575 : uint16_t iana_opaque_len;
1576 : uint8_t iana_opaque[4];
1577 : /* uint8_t iana_align[0]; */
1578 : spdm_svh_pcisig_header_t pcisig_header;
1579 : uint16_t pcisig_opaque_len;
1580 : uint8_t pcisig_opaque[3];
1581 : uint8_t pcisig_align[3];
1582 : spdm_svh_usb_header_t usb_header;
1583 : uint16_t usb_opaque_len;
1584 : uint8_t usb_opaque[2];
1585 : /* uint8_t usb_align[0]; */
1586 : spdm_svh_tcg_header_t tcg_header;
1587 : uint16_t tcg_opaque_len;
1588 : uint8_t tcg_opaque[1];
1589 : uint8_t tcg_align[1];
1590 : spdm_svh_dmtf_header_t dmtf_sm_ver_sel_header;
1591 : uint16_t dmtf_sm_ver_sel_opaque_len;
1592 : secured_message_opaque_element_version_selection_t dmtf_sm_ver_sel_opaque;
1593 : /* uint8_t dmtf_sm_ver_sel_align[0]; */
1594 : spdm_svh_dmtf_header_t dmtf_sm_sup_ver_header;
1595 : uint16_t dmtf_sm_sup_ver_opaque_len;
1596 : secured_message_opaque_element_supported_version_t dmtf_sm_sup_ver_opaque;
1597 : spdm_version_number_t dmtf_sm_sup_ver_versions_list[3];
1598 : uint8_t dmtf_sm_sup_ver_align[3];
1599 : } test_spdm12_opaque_data_table_t;
1600 :
1601 : #pragma pack()
1602 :
1603 1 : static void libspdm_test_process_opaque_data_case22(void **state)
1604 : {
1605 : libspdm_return_t status;
1606 : libspdm_test_context_t *spdm_test_context;
1607 : libspdm_context_t *spdm_context;
1608 : const void *get_element_ptr;
1609 : size_t get_element_len;
1610 : size_t opaque_data_size;
1611 : uint8_t *opaque_data_ptr;
1612 : test_spdm12_opaque_data_table_t opaque_data;
1613 :
1614 1 : spdm_test_context = *state;
1615 1 : spdm_context = spdm_test_context->spdm_context;
1616 1 : spdm_test_context->case_id = 0x16;
1617 :
1618 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_12 <<
1619 : SPDM_VERSION_NUMBER_SHIFT_BIT;
1620 :
1621 1 : spdm_context->local_context.secured_message_version.spdm_version_count = 1;
1622 :
1623 1 : libspdm_set_mem ((uint8_t *)&opaque_data, sizeof(opaque_data), 0xFF);
1624 1 : opaque_data.opaque_header.total_elements = SPDM_REGISTRY_ID_MAX + 2;
1625 1 : opaque_data.cbor_header.header.id = SPDM_REGISTRY_ID_IANA_CBOR;
1626 1 : opaque_data.cbor_header.header.vendor_id_len = sizeof(opaque_data.cbor_vendor_id);
1627 1 : opaque_data.cbor_opaque_len = sizeof(opaque_data.cbor_opaque);
1628 1 : opaque_data.vesa_header.header.id = SPDM_REGISTRY_ID_VESA;
1629 1 : opaque_data.vesa_header.header.vendor_id_len = 0;
1630 1 : opaque_data.vesa_opaque_len = sizeof(opaque_data.vesa_opaque);
1631 1 : opaque_data.jedec_header.header.id = SPDM_REGISTRY_ID_JEDEC;
1632 1 : opaque_data.jedec_header.header.vendor_id_len = sizeof(opaque_data.jedec_header.vendor_id);
1633 1 : opaque_data.jedec_opaque_len = sizeof(opaque_data.jedec_opaque);
1634 1 : opaque_data.cxl_header.header.id = SPDM_REGISTRY_ID_CXL;
1635 1 : opaque_data.cxl_header.header.vendor_id_len = sizeof(opaque_data.cxl_header.vendor_id);
1636 1 : opaque_data.cxl_opaque_len = sizeof(opaque_data.cxl_opaque);
1637 1 : opaque_data.mipi_header.header.id = SPDM_REGISTRY_ID_MIPI;
1638 1 : opaque_data.mipi_header.header.vendor_id_len = sizeof(opaque_data.mipi_header.vendor_id);
1639 1 : opaque_data.mipi_opaque_len = sizeof(opaque_data.mipi_opaque);
1640 1 : opaque_data.hdbaset_header.header.id = SPDM_REGISTRY_ID_HDBASET;
1641 1 : opaque_data.hdbaset_header.header.vendor_id_len = sizeof(opaque_data.hdbaset_header.vendor_id);
1642 1 : opaque_data.hdbaset_opaque_len = sizeof(opaque_data.hdbaset_opaque);
1643 1 : opaque_data.iana_header.header.id = SPDM_REGISTRY_ID_IANA;
1644 1 : opaque_data.iana_header.header.vendor_id_len = sizeof(opaque_data.iana_header.vendor_id);
1645 1 : opaque_data.iana_opaque_len = sizeof(opaque_data.iana_opaque);
1646 1 : opaque_data.pcisig_header.header.id = SPDM_REGISTRY_ID_PCISIG;
1647 1 : opaque_data.pcisig_header.header.vendor_id_len = sizeof(opaque_data.pcisig_header.vendor_id);
1648 1 : opaque_data.pcisig_opaque_len = sizeof(opaque_data.pcisig_opaque);
1649 1 : opaque_data.usb_header.header.id = SPDM_REGISTRY_ID_USB;
1650 1 : opaque_data.usb_header.header.vendor_id_len = sizeof(opaque_data.usb_header.vendor_id);
1651 1 : opaque_data.usb_opaque_len = sizeof(opaque_data.usb_opaque);
1652 1 : opaque_data.tcg_header.header.id = SPDM_REGISTRY_ID_TCG;
1653 1 : opaque_data.tcg_header.header.vendor_id_len = sizeof(opaque_data.tcg_header.vendor_id);
1654 1 : opaque_data.tcg_opaque_len = sizeof(opaque_data.tcg_opaque);
1655 1 : opaque_data.dmtf_sm_ver_sel_header.header.id = SPDM_REGISTRY_ID_DMTF;
1656 1 : opaque_data.dmtf_sm_ver_sel_header.header.vendor_id_len = 0;
1657 1 : opaque_data.dmtf_sm_ver_sel_opaque_len = sizeof(opaque_data.dmtf_sm_ver_sel_opaque);
1658 1 : opaque_data.dmtf_sm_ver_sel_opaque.sm_data_version =
1659 : SECURED_MESSAGE_OPAQUE_ELEMENT_SMDATA_DATA_VERSION;
1660 1 : opaque_data.dmtf_sm_ver_sel_opaque.sm_data_id =
1661 : SECURED_MESSAGE_OPAQUE_ELEMENT_SMDATA_ID_VERSION_SELECTION;
1662 1 : opaque_data.dmtf_sm_ver_sel_opaque.selected_version = SECURED_SPDM_VERSION_12 << 8;
1663 1 : opaque_data.dmtf_sm_sup_ver_header.header.id = SPDM_REGISTRY_ID_DMTF;
1664 1 : opaque_data.dmtf_sm_sup_ver_header.header.vendor_id_len = 0;
1665 1 : opaque_data.dmtf_sm_sup_ver_opaque_len = sizeof(opaque_data.dmtf_sm_sup_ver_opaque) +
1666 : sizeof(opaque_data.dmtf_sm_sup_ver_versions_list);
1667 1 : opaque_data.dmtf_sm_sup_ver_opaque.sm_data_version =
1668 : SECURED_MESSAGE_OPAQUE_ELEMENT_SMDATA_DATA_VERSION;
1669 1 : opaque_data.dmtf_sm_sup_ver_opaque.sm_data_id =
1670 : SECURED_MESSAGE_OPAQUE_ELEMENT_SMDATA_ID_SUPPORTED_VERSION;
1671 1 : opaque_data.dmtf_sm_sup_ver_opaque.version_count =
1672 : LIBSPDM_ARRAY_SIZE(opaque_data.dmtf_sm_sup_ver_versions_list);
1673 1 : opaque_data.dmtf_sm_sup_ver_versions_list[0] = SECURED_SPDM_VERSION_10 << 8;
1674 1 : opaque_data.dmtf_sm_sup_ver_versions_list[1] = SECURED_SPDM_VERSION_11 << 8;
1675 1 : opaque_data.dmtf_sm_sup_ver_versions_list[2] = SECURED_SPDM_VERSION_12 << 8;
1676 :
1677 1 : opaque_data_ptr = (uint8_t *)&opaque_data;
1678 1 : opaque_data_size = sizeof(opaque_data);
1679 1 : status = libspdm_get_element_from_opaque_data(spdm_context,
1680 : opaque_data_size, opaque_data_ptr,
1681 : SPDM_REGISTRY_ID_DMTF,
1682 : SECURED_MESSAGE_OPAQUE_ELEMENT_SMDATA_ID_VERSION_SELECTION,
1683 : &get_element_ptr, &get_element_len
1684 : );
1685 1 : assert_int_equal (status, true);
1686 1 : status = libspdm_get_element_from_opaque_data(spdm_context,
1687 : opaque_data_size, opaque_data_ptr,
1688 : SPDM_REGISTRY_ID_DMTF,
1689 : SECURED_MESSAGE_OPAQUE_ELEMENT_SMDATA_ID_SUPPORTED_VERSION,
1690 : &get_element_ptr, &get_element_len
1691 : );
1692 1 : assert_int_equal (status, true);
1693 1 : }
1694 :
1695 : static libspdm_test_context_t m_libspdm_common_context_data_test_context = {
1696 : LIBSPDM_TEST_CONTEXT_VERSION,
1697 : true,
1698 : NULL,
1699 : NULL,
1700 : };
1701 :
1702 1 : int libspdm_common_context_data_test_main(void)
1703 : {
1704 1 : const struct CMUnitTest spdm_common_context_data_tests[] = {
1705 : cmocka_unit_test(libspdm_test_common_context_data_case1),
1706 : cmocka_unit_test(libspdm_test_common_context_data_case2),
1707 : cmocka_unit_test(libspdm_test_common_context_data_case3),
1708 : cmocka_unit_test(libspdm_test_common_context_data_case4),
1709 :
1710 : cmocka_unit_test(libspdm_test_verify_peer_cert_chain_buffer_case5),
1711 : cmocka_unit_test(libspdm_test_verify_peer_cert_chain_buffer_case6),
1712 : cmocka_unit_test(libspdm_test_verify_peer_cert_chain_buffer_case7),
1713 : cmocka_unit_test(libspdm_test_verify_peer_cert_chain_buffer_case8),
1714 :
1715 : cmocka_unit_test(libspdm_test_set_data_case9),
1716 :
1717 : /* Successful response V1.1 for multi element opaque data supported version, element number is 2*/
1718 : cmocka_unit_test(libspdm_test_process_opaque_data_supported_version_data_case10),
1719 : /* Failed response V1.1 for multi element opaque data supported version, element id is wrong*/
1720 : cmocka_unit_test(libspdm_test_process_opaque_data_supported_version_data_case11),
1721 : /* Successful response V1.2 for multi element opaque data supported version, element number is 2*/
1722 : cmocka_unit_test(libspdm_test_process_opaque_data_supported_version_data_case12),
1723 : /* Failed response V1.2 for multi element opaque data supported version, element id is wrong*/
1724 : cmocka_unit_test(libspdm_test_process_opaque_data_supported_version_data_case13),
1725 : /* Successful response V1.1 for multi element opaque data selection version, element number is 2*/
1726 : cmocka_unit_test(libspdm_test_process_opaque_data_selection_version_data_case14),
1727 : /* Failed response V1.1 for multi element opaque data selection version, element number is wrong*/
1728 : cmocka_unit_test(libspdm_test_process_opaque_data_selection_version_data_case15),
1729 : /* Successful response V1.2 for multi element opaque data selection version, element number is 2*/
1730 : cmocka_unit_test(libspdm_test_process_opaque_data_selection_version_data_case16),
1731 : /* Failed response V1.2 for multi element opaque data selection version, element number is wrong*/
1732 : cmocka_unit_test(libspdm_test_process_opaque_data_selection_version_data_case17),
1733 :
1734 : /* Successful initialization and setting of secured message context location. */
1735 : cmocka_unit_test(libspdm_test_secured_message_context_location_selection_case18),
1736 :
1737 : /* Test that the Export Master Secret can be exported and cleared. */
1738 : cmocka_unit_test(libspdm_test_export_master_secret_case19),
1739 : cmocka_unit_test(libspdm_test_check_context_case20),
1740 :
1741 : /* Test the max DHE/PSK session count */
1742 : cmocka_unit_test(libspdm_test_max_session_count_case21),
1743 :
1744 : /* Successful response V1.2 for multi element */
1745 : cmocka_unit_test(libspdm_test_process_opaque_data_case22),
1746 : };
1747 :
1748 1 : libspdm_setup_test_context(&m_libspdm_common_context_data_test_context);
1749 :
1750 1 : return cmocka_run_group_tests(spdm_common_context_data_tests,
1751 : libspdm_unit_test_group_setup,
1752 : libspdm_unit_test_group_teardown);
1753 : }
|