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_secured_message_lib.h"
10 :
11 : #if LIBSPDM_SEND_GET_CERTIFICATE_SUPPORT
12 :
13 : static uint8_t m_libspdm_local_certificate_chain[LIBSPDM_MAX_CERT_CHAIN_SIZE];
14 :
15 : static void *m_libspdm_local_certificate_chain_test_cert;
16 : static size_t m_libspdm_local_certificate_chain_size;
17 :
18 : static bool m_get_digest;
19 :
20 31 : static libspdm_return_t libspdm_requester_get_digests_test_send_message(
21 : void *spdm_context, size_t request_size, const void *request,
22 : uint64_t timeout)
23 : {
24 : libspdm_test_context_t *spdm_test_context;
25 :
26 31 : spdm_test_context = libspdm_get_test_context();
27 31 : switch (spdm_test_context->case_id) {
28 1 : case 0x1:
29 1 : return LIBSPDM_STATUS_SEND_FAIL;
30 1 : case 0x2:
31 1 : return LIBSPDM_STATUS_SUCCESS;
32 0 : case 0x3:
33 0 : return LIBSPDM_STATUS_SUCCESS;
34 1 : case 0x4:
35 1 : return LIBSPDM_STATUS_SUCCESS;
36 1 : case 0x5:
37 1 : return LIBSPDM_STATUS_SUCCESS;
38 0 : case 0x6:
39 0 : return LIBSPDM_STATUS_SUCCESS;
40 1 : case 0x7:
41 1 : return LIBSPDM_STATUS_SUCCESS;
42 2 : case 0x8:
43 2 : return LIBSPDM_STATUS_SUCCESS;
44 1 : case 0x9:
45 1 : return LIBSPDM_STATUS_SUCCESS;
46 0 : case 0xA:
47 0 : return LIBSPDM_STATUS_SUCCESS;
48 1 : case 0xB:
49 1 : return LIBSPDM_STATUS_SUCCESS;
50 0 : case 0xC:
51 0 : return LIBSPDM_STATUS_SUCCESS;
52 1 : case 0xD:
53 1 : return LIBSPDM_STATUS_SUCCESS;
54 1 : case 0xE:
55 1 : return LIBSPDM_STATUS_SUCCESS;
56 0 : case 0xF:
57 0 : return LIBSPDM_STATUS_SUCCESS;
58 0 : case 0x10:
59 0 : return LIBSPDM_STATUS_SUCCESS;
60 0 : case 0x11:
61 0 : return LIBSPDM_STATUS_SUCCESS;
62 1 : case 0x12:
63 1 : return LIBSPDM_STATUS_SUCCESS;
64 0 : case 0x13:
65 0 : return LIBSPDM_STATUS_SUCCESS;
66 0 : case 0x14:
67 0 : return LIBSPDM_STATUS_SUCCESS;
68 0 : case 0x15:
69 0 : return LIBSPDM_STATUS_SUCCESS;
70 18 : case 0x16:
71 18 : return LIBSPDM_STATUS_SUCCESS;
72 1 : case 0x17:
73 1 : return LIBSPDM_STATUS_SUCCESS;
74 0 : case 0x18:
75 0 : return LIBSPDM_STATUS_SUCCESS;
76 0 : case 0x19:
77 0 : return LIBSPDM_STATUS_SUCCESS;
78 0 : default:
79 0 : return LIBSPDM_STATUS_SEND_FAIL;
80 : }
81 : }
82 :
83 29 : static libspdm_return_t libspdm_requester_get_digests_test_receive_message(
84 : void *spdm_context, size_t *response_size,
85 : void **response, uint64_t timeout)
86 : {
87 : libspdm_test_context_t *spdm_test_context;
88 :
89 29 : spdm_test_context = libspdm_get_test_context();
90 29 : switch (spdm_test_context->case_id) {
91 0 : case 0x1:
92 0 : return LIBSPDM_STATUS_RECEIVE_FAIL;
93 :
94 0 : case 0x2: {
95 : spdm_digest_response_t *spdm_response;
96 : uint8_t *digest;
97 : size_t spdm_response_size;
98 : size_t transport_header_size;
99 :
100 : ((libspdm_context_t *)spdm_context)
101 0 : ->connection_info.algorithm.base_hash_algo =
102 : m_libspdm_use_hash_algo;
103 0 : spdm_response_size = sizeof(spdm_digest_response_t) +
104 0 : libspdm_get_hash_size(m_libspdm_use_hash_algo);
105 0 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
106 0 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
107 :
108 0 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_10;
109 0 : spdm_response->header.param1 = 0;
110 0 : spdm_response->header.request_response_code = SPDM_DIGESTS;
111 0 : spdm_response->header.param2 = 0;
112 0 : libspdm_set_mem(m_libspdm_local_certificate_chain,
113 : sizeof(m_libspdm_local_certificate_chain),
114 : (uint8_t)(0xFF));
115 :
116 0 : digest = (void *)(spdm_response + 1);
117 0 : libspdm_zero_mem (digest,
118 0 : libspdm_get_hash_size(m_libspdm_use_hash_algo) *
119 : (SPDM_MAX_SLOT_COUNT - 1));
120 0 : libspdm_hash_all(m_libspdm_use_hash_algo, m_libspdm_local_certificate_chain,
121 : sizeof(m_libspdm_local_certificate_chain), &digest[0]);
122 0 : spdm_response->header.param2 |= (0x01 << 0);
123 :
124 0 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
125 : false, spdm_response_size,
126 : spdm_response, response_size,
127 : response);
128 : }
129 0 : return LIBSPDM_STATUS_SUCCESS;
130 :
131 0 : case 0x3: {
132 : spdm_digest_response_t *spdm_response;
133 : uint8_t *digest;
134 : size_t spdm_response_size;
135 : size_t transport_header_size;
136 :
137 : ((libspdm_context_t *)spdm_context)
138 0 : ->connection_info.algorithm.base_hash_algo =
139 : m_libspdm_use_hash_algo;
140 0 : spdm_response_size = sizeof(spdm_digest_response_t) +
141 0 : libspdm_get_hash_size(m_libspdm_use_hash_algo);
142 0 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
143 0 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
144 :
145 0 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_10;
146 0 : spdm_response->header.param1 = 0;
147 0 : spdm_response->header.request_response_code = SPDM_DIGESTS;
148 0 : spdm_response->header.param2 = 0;
149 0 : libspdm_set_mem(m_libspdm_local_certificate_chain,
150 : sizeof(m_libspdm_local_certificate_chain),
151 : (uint8_t)(0xFF));
152 :
153 0 : digest = (void *)(spdm_response + 1);
154 0 : libspdm_hash_all(m_libspdm_use_hash_algo, m_libspdm_local_certificate_chain,
155 : sizeof(m_libspdm_local_certificate_chain), &digest[0]);
156 0 : spdm_response->header.param2 |= (1 << 0);
157 :
158 0 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
159 : false, spdm_response_size,
160 : spdm_response, response_size,
161 : response);
162 : }
163 0 : return LIBSPDM_STATUS_SUCCESS;
164 :
165 1 : case 0x4: {
166 : spdm_error_response_t *spdm_response;
167 : size_t spdm_response_size;
168 : size_t transport_header_size;
169 :
170 1 : spdm_response_size = sizeof(spdm_error_response_t);
171 1 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
172 1 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
173 :
174 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_10;
175 1 : spdm_response->header.request_response_code = SPDM_ERROR;
176 1 : spdm_response->header.param1 = SPDM_ERROR_CODE_INVALID_REQUEST;
177 1 : spdm_response->header.param2 = 0;
178 :
179 1 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
180 : false, spdm_response_size,
181 : spdm_response,
182 : response_size, response);
183 : }
184 1 : return LIBSPDM_STATUS_SUCCESS;
185 :
186 1 : case 0x5: {
187 : spdm_error_response_t *spdm_response;
188 : size_t spdm_response_size;
189 : size_t transport_header_size;
190 :
191 1 : spdm_response_size = sizeof(spdm_error_response_t);
192 1 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
193 1 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
194 :
195 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_10;
196 1 : spdm_response->header.request_response_code = SPDM_ERROR;
197 1 : spdm_response->header.param1 = SPDM_ERROR_CODE_BUSY;
198 1 : spdm_response->header.param2 = 0;
199 :
200 1 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
201 : false, spdm_response_size,
202 : spdm_response,
203 : response_size, response);
204 : }
205 1 : return LIBSPDM_STATUS_SUCCESS;
206 :
207 0 : case 0x6: {
208 : static size_t sub_index1 = 0;
209 0 : if (sub_index1 == 0) {
210 : spdm_error_response_t *spdm_response;
211 : size_t spdm_response_size;
212 : size_t transport_header_size;
213 :
214 0 : spdm_response_size = sizeof(spdm_error_response_t);
215 0 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
216 0 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
217 :
218 0 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_10;
219 0 : spdm_response->header.request_response_code = SPDM_ERROR;
220 0 : spdm_response->header.param1 = SPDM_ERROR_CODE_BUSY;
221 0 : spdm_response->header.param2 = 0;
222 :
223 0 : libspdm_transport_test_encode_message(
224 : spdm_context, NULL, false, false,
225 : spdm_response_size, spdm_response,
226 : response_size, response);
227 0 : } else if (sub_index1 == 1) {
228 : spdm_digest_response_t *spdm_response;
229 : uint8_t *digest;
230 : size_t spdm_response_size;
231 : size_t transport_header_size;
232 :
233 : ((libspdm_context_t *)spdm_context)
234 0 : ->connection_info.algorithm.base_hash_algo =
235 : m_libspdm_use_hash_algo;
236 0 : spdm_response_size = sizeof(spdm_digest_response_t) +
237 0 : libspdm_get_hash_size(m_libspdm_use_hash_algo);
238 0 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
239 0 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
240 :
241 0 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_10;
242 0 : spdm_response->header.param1 = 0;
243 0 : spdm_response->header.request_response_code = SPDM_DIGESTS;
244 0 : spdm_response->header.param2 = 0;
245 0 : libspdm_set_mem(m_libspdm_local_certificate_chain,
246 : sizeof(m_libspdm_local_certificate_chain), (uint8_t)(0xFF));
247 :
248 0 : digest = (void *)(spdm_response + 1);
249 0 : libspdm_hash_all(m_libspdm_use_hash_algo,
250 : m_libspdm_local_certificate_chain,
251 : sizeof(m_libspdm_local_certificate_chain), &digest[0]);
252 0 : spdm_response->header.param2 |= (1 << 0);
253 :
254 0 : libspdm_transport_test_encode_message(
255 : spdm_context, NULL, false, false, spdm_response_size,
256 : spdm_response, response_size, response);
257 : }
258 0 : sub_index1++;
259 : }
260 0 : return LIBSPDM_STATUS_SUCCESS;
261 :
262 1 : case 0x7: {
263 : spdm_error_response_t *spdm_response;
264 : size_t spdm_response_size;
265 : size_t transport_header_size;
266 :
267 1 : spdm_response_size = sizeof(spdm_error_response_t);
268 1 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
269 1 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
270 :
271 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_10;
272 1 : spdm_response->header.request_response_code = SPDM_ERROR;
273 1 : spdm_response->header.param1 = SPDM_ERROR_CODE_REQUEST_RESYNCH;
274 1 : spdm_response->header.param2 = 0;
275 :
276 1 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
277 : false, spdm_response_size,
278 : spdm_response,
279 : response_size, response);
280 : }
281 1 : return LIBSPDM_STATUS_SUCCESS;
282 :
283 2 : case 0x8: {
284 : spdm_error_response_data_response_not_ready_t *spdm_response;
285 : size_t spdm_response_size;
286 : size_t transport_header_size;
287 :
288 2 : spdm_response_size = sizeof(spdm_error_response_data_response_not_ready_t);
289 2 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
290 2 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
291 :
292 2 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_10;
293 2 : spdm_response->header.request_response_code = SPDM_ERROR;
294 2 : spdm_response->header.param1 = SPDM_ERROR_CODE_RESPONSE_NOT_READY;
295 2 : spdm_response->header.param2 = 0;
296 2 : spdm_response->extend_error_data.rd_exponent = 1;
297 2 : spdm_response->extend_error_data.rd_tm = 2;
298 2 : spdm_response->extend_error_data.request_code = SPDM_GET_DIGESTS;
299 2 : spdm_response->extend_error_data.token = 0;
300 :
301 2 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
302 : false, spdm_response_size,
303 : spdm_response,
304 : response_size, response);
305 : }
306 2 : return LIBSPDM_STATUS_SUCCESS;
307 :
308 1 : case 0x9: {
309 : spdm_digest_response_t *spdm_response;
310 : uint8_t *digest;
311 : size_t spdm_response_size;
312 : size_t transport_header_size;
313 :
314 1 : ((libspdm_context_t *)spdm_context)->connection_info.algorithm.base_hash_algo =
315 : m_libspdm_use_hash_algo;
316 1 : spdm_response_size = sizeof(spdm_digest_response_t) +
317 1 : libspdm_get_hash_size(m_libspdm_use_hash_algo);
318 1 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
319 1 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
320 :
321 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
322 1 : spdm_response->header.param1 = 0;
323 1 : spdm_response->header.request_response_code = SPDM_DIGESTS;
324 1 : spdm_response->header.param2 = 0;
325 1 : libspdm_set_mem(m_libspdm_local_certificate_chain,
326 : sizeof(m_libspdm_local_certificate_chain),
327 : (uint8_t)(0xFF));
328 :
329 1 : digest = (void *)(spdm_response + 1);
330 1 : libspdm_zero_mem (digest,
331 1 : libspdm_get_hash_size(m_libspdm_use_hash_algo) * SPDM_MAX_SLOT_COUNT);
332 1 : libspdm_hash_all(m_libspdm_use_hash_algo, m_libspdm_local_certificate_chain,
333 : sizeof(m_libspdm_local_certificate_chain), &digest[0]);
334 1 : spdm_response->header.param2 |= (0x01 << 0);
335 :
336 1 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
337 : false, spdm_response_size,
338 : spdm_response, response_size,
339 : response);
340 : }
341 1 : return LIBSPDM_STATUS_SUCCESS;
342 :
343 0 : case 0xA:
344 0 : return LIBSPDM_STATUS_SUCCESS;
345 :
346 1 : case 0xB:
347 1 : return LIBSPDM_STATUS_RECEIVE_FAIL;
348 :
349 0 : case 0xC: {
350 : spdm_digest_response_t *spdm_response;
351 : size_t spdm_response_size;
352 : size_t transport_header_size;
353 :
354 : ((libspdm_context_t *)spdm_context)
355 0 : ->connection_info.algorithm.base_hash_algo =
356 : m_libspdm_use_hash_algo;
357 0 : spdm_response_size = 2;
358 0 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
359 0 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
360 :
361 0 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_10;
362 0 : spdm_response->header.param1 = 0;
363 0 : spdm_response->header.request_response_code = SPDM_DIGESTS;
364 0 : spdm_response->header.param2 = 0;
365 :
366 0 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
367 : false, spdm_response_size,
368 : spdm_response, response_size,
369 : response);
370 : }
371 0 : return LIBSPDM_STATUS_SUCCESS;
372 :
373 1 : case 0xD: {
374 : spdm_digest_response_t *spdm_response;
375 : uint8_t *digest;
376 : size_t spdm_response_size;
377 : size_t transport_header_size;
378 :
379 : ((libspdm_context_t *)spdm_context)
380 1 : ->connection_info.algorithm.base_hash_algo =
381 : m_libspdm_use_hash_algo;
382 1 : spdm_response_size = sizeof(spdm_digest_response_t) +
383 1 : libspdm_get_hash_size(m_libspdm_use_hash_algo);
384 1 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
385 1 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
386 :
387 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_10;
388 1 : spdm_response->header.param1 = 0;
389 1 : spdm_response->header.request_response_code = SPDM_CERTIFICATE;
390 1 : spdm_response->header.param2 = 0;
391 1 : libspdm_set_mem(m_libspdm_local_certificate_chain,
392 : sizeof(m_libspdm_local_certificate_chain),
393 : (uint8_t)(0xFF));
394 :
395 1 : digest = (void *)(spdm_response + 1);
396 1 : libspdm_hash_all(m_libspdm_use_hash_algo, m_libspdm_local_certificate_chain,
397 : sizeof(m_libspdm_local_certificate_chain), &digest[0]);
398 1 : spdm_response->header.param2 |= (1 << 0);
399 :
400 1 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
401 : false, spdm_response_size,
402 : spdm_response, response_size,
403 : response);
404 : }
405 1 : return LIBSPDM_STATUS_SUCCESS;
406 :
407 1 : case 0xE: {
408 : spdm_digest_response_t *spdm_response;
409 : size_t spdm_response_size;
410 : size_t transport_header_size;
411 :
412 1 : spdm_response_size = sizeof(spdm_digest_response_t);
413 1 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
414 1 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
415 :
416 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_10;
417 1 : spdm_response->header.param1 = 0;
418 1 : spdm_response->header.request_response_code = SPDM_DIGESTS;
419 1 : spdm_response->header.param2 = 0;
420 :
421 1 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
422 : false, spdm_response_size,
423 : spdm_response,
424 : response_size, response);
425 : }
426 1 : return LIBSPDM_STATUS_SUCCESS;
427 :
428 0 : case 0xF:
429 0 : return LIBSPDM_STATUS_SUCCESS;
430 :
431 0 : case 0x10: {
432 : spdm_digest_response_t *spdm_response;
433 : uint8_t *digest;
434 : size_t spdm_response_size;
435 : size_t transport_header_size;
436 :
437 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
438 : ((libspdm_context_t *)spdm_context)->transcript.message_b.buffer_size =
439 : ((libspdm_context_t *)spdm_context)->transcript.message_b.max_buffer_size -
440 : (sizeof(spdm_digest_response_t));
441 : #endif
442 :
443 : ((libspdm_context_t *)spdm_context)
444 0 : ->connection_info.algorithm.base_hash_algo =
445 : m_libspdm_use_hash_algo;
446 0 : spdm_response_size = sizeof(spdm_digest_response_t) +
447 0 : libspdm_get_hash_size(m_libspdm_use_hash_algo);
448 0 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
449 0 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
450 :
451 0 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_10;
452 0 : spdm_response->header.param1 = 0;
453 0 : spdm_response->header.request_response_code = SPDM_DIGESTS;
454 0 : spdm_response->header.param2 = 0;
455 0 : libspdm_set_mem(m_libspdm_local_certificate_chain,
456 : sizeof(m_libspdm_local_certificate_chain),
457 : (uint8_t)(0xFF));
458 :
459 0 : digest = (void *)(spdm_response + 1);
460 0 : libspdm_hash_all(m_libspdm_use_hash_algo, m_libspdm_local_certificate_chain,
461 : sizeof(m_libspdm_local_certificate_chain), &digest[0]);
462 0 : spdm_response->header.param2 |= (1 << 0);
463 :
464 0 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
465 : false, spdm_response_size,
466 : spdm_response, response_size,
467 : response);
468 : }
469 0 : return LIBSPDM_STATUS_SUCCESS;
470 :
471 0 : case 0x11:
472 0 : return LIBSPDM_STATUS_SUCCESS;
473 :
474 1 : case 0x12: {
475 : spdm_digest_response_t *spdm_response;
476 : uint8_t *digest;
477 : size_t digest_count;
478 : size_t spdm_response_size;
479 : size_t transport_header_size;
480 : size_t index;
481 :
482 : ((libspdm_context_t *)spdm_context)
483 1 : ->connection_info.algorithm.base_hash_algo =
484 : m_libspdm_use_hash_algo;
485 1 : digest_count = 4;
486 1 : spdm_response_size = sizeof(spdm_digest_response_t) +
487 1 : libspdm_get_hash_size(m_libspdm_use_hash_algo);
488 1 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
489 1 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
490 :
491 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_10;
492 1 : spdm_response->header.param1 = 0;
493 1 : spdm_response->header.request_response_code = SPDM_DIGESTS;
494 1 : spdm_response->header.param2 = 0;
495 1 : libspdm_set_mem(m_libspdm_local_certificate_chain,
496 : sizeof(m_libspdm_local_certificate_chain),
497 : (uint8_t)(0xFF));
498 :
499 1 : digest = (void *)(spdm_response + 1);
500 :
501 1 : libspdm_hash_all(m_libspdm_use_hash_algo, m_libspdm_local_certificate_chain,
502 : sizeof(m_libspdm_local_certificate_chain), &digest[0]);
503 5 : for (index = 0; index < digest_count; index++) {
504 4 : spdm_response->header.param2 |= (1 << index);
505 : }
506 :
507 1 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
508 : false, spdm_response_size,
509 : spdm_response, response_size,
510 : response);
511 : }
512 1 : return LIBSPDM_STATUS_SUCCESS;
513 :
514 0 : case 0x13: {
515 : spdm_digest_response_t *spdm_response;
516 : uint8_t *digest;
517 : size_t digest_count;
518 : size_t spdm_response_size;
519 : size_t transport_header_size;
520 : size_t index;
521 :
522 : ((libspdm_context_t *)spdm_context)
523 0 : ->connection_info.algorithm.base_hash_algo =
524 : m_libspdm_use_hash_algo;
525 0 : digest_count = 4;
526 0 : spdm_response_size =
527 : sizeof(spdm_digest_response_t) +
528 0 : digest_count * libspdm_get_hash_size(m_libspdm_use_hash_algo);
529 0 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
530 0 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
531 :
532 0 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_10;
533 0 : spdm_response->header.param1 = 0;
534 0 : spdm_response->header.request_response_code = SPDM_DIGESTS;
535 0 : spdm_response->header.param2 = 0;
536 0 : libspdm_set_mem(m_libspdm_local_certificate_chain,
537 : sizeof(m_libspdm_local_certificate_chain),
538 : (uint8_t)(0xFF));
539 :
540 0 : digest = (void *)(spdm_response + 1);
541 :
542 0 : for (index = 0; index < digest_count; index++) {
543 0 : libspdm_hash_all(
544 : m_libspdm_use_hash_algo, m_libspdm_local_certificate_chain,
545 : sizeof(m_libspdm_local_certificate_chain),
546 0 : &digest[index *
547 0 : libspdm_get_hash_size(m_libspdm_use_hash_algo)]);
548 0 : spdm_response->header.param2 |= (1 << index);
549 0 : if (index == 0) {
550 0 : continue;
551 : }
552 0 : digest[(index + 1) * libspdm_get_hash_size(m_libspdm_use_hash_algo) -
553 0 : 1] = 0;
554 : }
555 :
556 0 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
557 : false, spdm_response_size,
558 : spdm_response, response_size,
559 : response);
560 : }
561 0 : return LIBSPDM_STATUS_SUCCESS;
562 :
563 0 : case 0x14: {
564 : spdm_digest_response_t *spdm_response;
565 : size_t spdm_response_size;
566 : size_t transport_header_size;
567 :
568 0 : ((libspdm_context_t *)spdm_context)->connection_info.algorithm.base_hash_algo =
569 : m_libspdm_use_hash_algo;
570 0 : spdm_response_size = 5;
571 0 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
572 0 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
573 :
574 0 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_10;
575 0 : spdm_response->header.param1 = 0;
576 0 : spdm_response->header.request_response_code = SPDM_DIGESTS;
577 0 : spdm_response->header.param2 = 0;
578 :
579 0 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
580 : false, spdm_response_size,
581 : spdm_response, response_size,
582 : response);
583 : }
584 0 : return LIBSPDM_STATUS_SUCCESS;
585 :
586 0 : case 0x15: {
587 : spdm_digest_response_t *spdm_response;
588 : uint8_t *digest;
589 : size_t spdm_response_size;
590 : size_t transport_header_size;
591 :
592 : ((libspdm_context_t *)spdm_context)
593 0 : ->connection_info.algorithm.base_hash_algo =
594 : m_libspdm_use_hash_algo;
595 0 : spdm_response_size = sizeof(spdm_message_header_t) +
596 : LIBSPDM_MAX_HASH_SIZE * SPDM_MAX_SLOT_COUNT + 1;
597 0 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
598 0 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
599 :
600 0 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_10;
601 0 : spdm_response->header.param1 = 0;
602 0 : spdm_response->header.request_response_code = SPDM_DIGESTS;
603 0 : spdm_response->header.param2 = 0;
604 0 : libspdm_set_mem(m_libspdm_local_certificate_chain,
605 : sizeof(m_libspdm_local_certificate_chain),
606 : (uint8_t)(0xFF));
607 :
608 0 : digest = (void *)(spdm_response + 1);
609 0 : libspdm_hash_all(m_libspdm_use_hash_algo, m_libspdm_local_certificate_chain,
610 : sizeof(m_libspdm_local_certificate_chain), &digest[0]);
611 0 : spdm_response->header.param2 |= (1 << 0);
612 :
613 0 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
614 : false, spdm_response_size,
615 : spdm_response, response_size,
616 : response);
617 : }
618 0 : return LIBSPDM_STATUS_SUCCESS;
619 :
620 18 : case 0x16:
621 : {
622 : static uint16_t error_code = LIBSPDM_ERROR_CODE_RESERVED_00;
623 :
624 : spdm_error_response_t *spdm_response;
625 : size_t spdm_response_size;
626 : size_t transport_header_size;
627 :
628 18 : spdm_response_size = sizeof(spdm_error_response_t);
629 18 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
630 18 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
631 :
632 18 : if(error_code <= 0xff) {
633 18 : libspdm_zero_mem (spdm_response, spdm_response_size);
634 18 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
635 18 : spdm_response->header.request_response_code = SPDM_ERROR;
636 18 : spdm_response->header.param1 = (uint8_t) error_code;
637 18 : spdm_response->header.param2 = 0;
638 :
639 18 : libspdm_transport_test_encode_message (spdm_context, NULL, false, false,
640 : spdm_response_size, spdm_response,
641 : response_size, response);
642 : }
643 :
644 18 : error_code++;
645 18 : if(error_code == SPDM_ERROR_CODE_BUSY) { /*busy is treated in cases 5 and 6*/
646 1 : error_code = SPDM_ERROR_CODE_UNEXPECTED_REQUEST;
647 : }
648 18 : if(error_code == LIBSPDM_ERROR_CODE_RESERVED_0D) { /*skip some reserved error codes (0d to 3e)*/
649 1 : error_code = LIBSPDM_ERROR_CODE_RESERVED_3F;
650 : }
651 18 : if(error_code == SPDM_ERROR_CODE_RESPONSE_NOT_READY) { /*skip response not ready, request resync, and some reserved codes (44 to fc)*/
652 1 : error_code = LIBSPDM_ERROR_CODE_RESERVED_FD;
653 : }
654 : }
655 18 : return LIBSPDM_STATUS_SUCCESS;
656 :
657 1 : case 0x17: {
658 : spdm_digest_response_t *spdm_response;
659 : uint8_t *digest;
660 : size_t spdm_response_size;
661 : size_t transport_header_size;
662 : uint32_t hash_size;
663 : uint8_t slot_count;
664 :
665 1 : slot_count = 2;
666 :
667 1 : hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
668 :
669 1 : spdm_response_size = sizeof(spdm_digest_response_t) + (hash_size * slot_count);
670 1 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
671 1 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
672 :
673 1 : spdm_response->header.request_response_code = SPDM_DIGESTS;
674 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_13;
675 1 : spdm_response->header.param1 = 0x01;
676 : /* Illegal value. Bit cannot be set unless also set in param1. */
677 1 : spdm_response->header.param2 = 0x03;
678 :
679 1 : libspdm_set_mem(m_libspdm_local_certificate_chain,
680 : sizeof(m_libspdm_local_certificate_chain),
681 : (uint8_t)(0xFF));
682 :
683 1 : digest = (void *)(spdm_response + 1);
684 1 : libspdm_zero_mem (digest, hash_size * slot_count);
685 :
686 1 : libspdm_hash_all(m_libspdm_use_hash_algo, m_libspdm_local_certificate_chain,
687 : sizeof(m_libspdm_local_certificate_chain), &digest[hash_size * 0]);
688 1 : libspdm_hash_all(m_libspdm_use_hash_algo, m_libspdm_local_certificate_chain,
689 1 : sizeof(m_libspdm_local_certificate_chain), &digest[hash_size * 1]);
690 :
691 1 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
692 : false, spdm_response_size,
693 : spdm_response, response_size,
694 : response);
695 : }
696 1 : return LIBSPDM_STATUS_SUCCESS;
697 :
698 0 : case 0x18: {
699 0 : if (m_get_digest) {
700 : spdm_digest_response_t *spdm_response;
701 : uint8_t *digest;
702 : size_t spdm_response_size;
703 : size_t transport_header_size;
704 :
705 : ((libspdm_context_t *)spdm_context)
706 0 : ->connection_info.algorithm.base_hash_algo =
707 : m_libspdm_use_hash_algo;
708 0 : spdm_response_size = sizeof(spdm_digest_response_t) +
709 0 : libspdm_get_hash_size(m_libspdm_use_hash_algo);
710 0 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
711 0 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
712 :
713 0 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_10;
714 0 : spdm_response->header.param1 = 0;
715 0 : spdm_response->header.request_response_code = SPDM_DIGESTS;
716 0 : spdm_response->header.param2 = 0;
717 0 : libspdm_set_mem(m_libspdm_local_certificate_chain,
718 : sizeof(m_libspdm_local_certificate_chain),
719 : (uint8_t)(0xFF));
720 :
721 0 : digest = (void *)(spdm_response + 1);
722 0 : libspdm_zero_mem (digest,
723 0 : libspdm_get_hash_size(m_libspdm_use_hash_algo) * SPDM_MAX_SLOT_COUNT);
724 0 : libspdm_hash_all(m_libspdm_use_hash_algo, m_libspdm_local_certificate_chain,
725 : sizeof(m_libspdm_local_certificate_chain), &digest[0]);
726 0 : spdm_response->header.param2 |= (0x01 << 0);
727 :
728 0 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
729 : false, spdm_response_size,
730 : spdm_response, response_size,
731 : response);
732 : } else {
733 : spdm_certificate_response_t *spdm_response;
734 : size_t spdm_response_size;
735 : size_t transport_header_size;
736 : uint16_t portion_length;
737 : uint16_t remainder_length;
738 : size_t count;
739 : static size_t calling_index = 0;
740 :
741 0 : if (m_libspdm_local_certificate_chain_test_cert == NULL) {
742 0 : libspdm_read_responder_public_certificate_chain(
743 : m_libspdm_use_hash_algo, m_libspdm_use_asym_algo,
744 : &m_libspdm_local_certificate_chain_test_cert,
745 : &m_libspdm_local_certificate_chain_size, NULL, NULL);
746 : }
747 0 : if (m_libspdm_local_certificate_chain_test_cert == NULL) {
748 0 : return LIBSPDM_STATUS_RECEIVE_FAIL;
749 : }
750 0 : count = (m_libspdm_local_certificate_chain_size +
751 0 : LIBSPDM_MAX_CERT_CHAIN_BLOCK_LEN - 1) /
752 : LIBSPDM_MAX_CERT_CHAIN_BLOCK_LEN;
753 0 : if (calling_index != count - 1) {
754 0 : portion_length = LIBSPDM_MAX_CERT_CHAIN_BLOCK_LEN;
755 0 : remainder_length =
756 0 : (uint16_t)(m_libspdm_local_certificate_chain_size -
757 : LIBSPDM_MAX_CERT_CHAIN_BLOCK_LEN *
758 0 : (calling_index + 1));
759 : } else {
760 0 : portion_length = (uint16_t)(
761 : m_libspdm_local_certificate_chain_size -
762 0 : LIBSPDM_MAX_CERT_CHAIN_BLOCK_LEN * (count - 1));
763 0 : remainder_length = 0;
764 : }
765 :
766 0 : spdm_response_size =
767 0 : sizeof(spdm_certificate_response_t) + portion_length;
768 0 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
769 0 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
770 :
771 0 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_10;
772 0 : spdm_response->header.request_response_code = SPDM_CERTIFICATE;
773 0 : spdm_response->header.param1 = 0;
774 0 : spdm_response->header.param2 = 0;
775 0 : spdm_response->portion_length = portion_length;
776 0 : spdm_response->remainder_length = remainder_length;
777 0 : libspdm_copy_mem(spdm_response + 1,
778 0 : (size_t)(*response) + *response_size - (size_t)(spdm_response + 1),
779 0 : (uint8_t *)m_libspdm_local_certificate_chain_test_cert +
780 0 : LIBSPDM_MAX_CERT_CHAIN_BLOCK_LEN * calling_index,
781 : portion_length);
782 :
783 0 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
784 : false, spdm_response_size,
785 : spdm_response, response_size,
786 : response);
787 :
788 0 : calling_index++;
789 0 : if (calling_index == count) {
790 0 : calling_index = 0;
791 0 : free(m_libspdm_local_certificate_chain_test_cert);
792 0 : m_libspdm_local_certificate_chain_test_cert = NULL;
793 0 : m_libspdm_local_certificate_chain_size = 0;
794 : }
795 : }
796 : }
797 0 : return LIBSPDM_STATUS_SUCCESS;
798 :
799 0 : case 0x19: {
800 : spdm_digest_response_t *spdm_response;
801 : uint8_t *digest;
802 : size_t spdm_response_size;
803 : size_t transport_header_size;
804 : uint32_t session_id;
805 : libspdm_session_info_t *session_info;
806 : uint8_t *scratch_buffer;
807 : size_t scratch_buffer_size;
808 :
809 0 : session_id = 0xFFFFFFFF;
810 :
811 : ((libspdm_context_t *)spdm_context)
812 0 : ->connection_info.algorithm.base_hash_algo =
813 : m_libspdm_use_hash_algo;
814 0 : spdm_response_size = sizeof(spdm_digest_response_t) +
815 0 : libspdm_get_hash_size(m_libspdm_use_hash_algo);
816 0 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
817 0 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
818 :
819 0 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_10;
820 0 : spdm_response->header.param1 = 0;
821 0 : spdm_response->header.request_response_code = SPDM_DIGESTS;
822 0 : spdm_response->header.param2 = 0;
823 :
824 : /* For secure message, message is in sender buffer, we need copy it to scratch buffer.
825 : * transport_message is always in sender buffer. */
826 0 : libspdm_get_scratch_buffer (spdm_context, (void **)&scratch_buffer, &scratch_buffer_size);
827 :
828 0 : libspdm_set_mem(m_libspdm_local_certificate_chain,
829 : sizeof(m_libspdm_local_certificate_chain),
830 : (uint8_t)(0xFF));
831 :
832 0 : digest = (void *)(spdm_response + 1);
833 0 : libspdm_zero_mem (digest,
834 0 : libspdm_get_hash_size(m_libspdm_use_hash_algo) * SPDM_MAX_SLOT_COUNT);
835 0 : libspdm_hash_all(m_libspdm_use_hash_algo, m_libspdm_local_certificate_chain,
836 : sizeof(m_libspdm_local_certificate_chain), &digest[0]);
837 0 : spdm_response->header.param2 |= (0x01 << 0);
838 :
839 0 : libspdm_copy_mem (scratch_buffer + transport_header_size,
840 : scratch_buffer_size - transport_header_size,
841 : spdm_response, spdm_response_size);
842 0 : spdm_response = (void *)(scratch_buffer + transport_header_size);
843 0 : libspdm_transport_test_encode_message(spdm_context, &session_id, false,
844 : false, spdm_response_size,
845 : spdm_response, response_size,
846 : response);
847 :
848 0 : session_info = libspdm_get_session_info_via_session_id(
849 : spdm_context, session_id);
850 0 : if (session_info == NULL) {
851 0 : return LIBSPDM_STATUS_RECEIVE_FAIL;
852 : }
853 : /* WALKAROUND: If just use single context to encode message and then decode message */
854 : ((libspdm_secured_message_context_t
855 0 : *)(session_info->secured_message_context))
856 0 : ->application_secret.response_data_sequence_number--;
857 :
858 : }
859 0 : return LIBSPDM_STATUS_SUCCESS;
860 :
861 0 : default:
862 0 : return LIBSPDM_STATUS_RECEIVE_FAIL;
863 : }
864 : }
865 :
866 : /**
867 : * Test 1: a failure occurs during the sending of the request message
868 : * Expected Behavior: requester returns the status LIBSPDM_STATUS_SEND_FAIL, with no DIGESTS message received
869 : **/
870 1 : static void libspdm_test_requester_get_digests_err_case1(void **state)
871 : {
872 : libspdm_return_t status;
873 : libspdm_test_context_t *spdm_test_context;
874 : libspdm_context_t *spdm_context;
875 : uint8_t slot_mask;
876 : uint8_t total_digest_buffer[LIBSPDM_MAX_HASH_SIZE * SPDM_MAX_SLOT_COUNT];
877 :
878 1 : spdm_test_context = *state;
879 1 : spdm_context = spdm_test_context->spdm_context;
880 1 : spdm_test_context->case_id = 0x1;
881 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_10 <<
882 : SPDM_VERSION_NUMBER_SHIFT_BIT;
883 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
884 1 : spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP;
885 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
886 1 : libspdm_set_mem(m_libspdm_local_certificate_chain,
887 : sizeof(m_libspdm_local_certificate_chain),
888 : (uint8_t)(0xFF));
889 1 : libspdm_reset_message_b(spdm_context);
890 :
891 1 : libspdm_zero_mem(total_digest_buffer, sizeof(total_digest_buffer));
892 1 : status = libspdm_get_digest(spdm_context, NULL, &slot_mask, &total_digest_buffer);
893 1 : assert_int_equal(status, LIBSPDM_STATUS_SEND_FAIL);
894 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
895 : assert_int_equal(spdm_context->transcript.message_b.buffer_size, 0);
896 : #endif
897 1 : }
898 :
899 : /**
900 : * Test 2: Requester is unable to acquire the sender buffer.
901 : * Expected Behavior: returns with error LIBSPDM_STATUS_ACQUIRE_FAIL.
902 : **/
903 1 : static void libspdm_test_requester_get_digests_err_case2(void **state)
904 : {
905 : libspdm_return_t status;
906 : libspdm_test_context_t *spdm_test_context;
907 : libspdm_context_t *spdm_context;
908 : uint8_t slot_mask;
909 : uint8_t total_digest_buffer[LIBSPDM_MAX_HASH_SIZE * SPDM_MAX_SLOT_COUNT];
910 :
911 1 : spdm_test_context = *state;
912 1 : spdm_context = spdm_test_context->spdm_context;
913 1 : spdm_test_context->case_id = 0x2;
914 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_10 <<
915 : SPDM_VERSION_NUMBER_SHIFT_BIT;
916 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
917 1 : spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP;
918 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
919 1 : libspdm_set_mem(m_libspdm_local_certificate_chain,
920 : sizeof(m_libspdm_local_certificate_chain),
921 : (uint8_t)(0xFF));
922 1 : libspdm_reset_message_b(spdm_context);
923 :
924 1 : libspdm_zero_mem(total_digest_buffer, sizeof(total_digest_buffer));
925 :
926 1 : libspdm_force_error(LIBSPDM_ERR_ACQUIRE_SENDER_BUFFER);
927 1 : status = libspdm_get_digest(spdm_context, NULL, &slot_mask, &total_digest_buffer);
928 1 : libspdm_release_error(LIBSPDM_ERR_ACQUIRE_SENDER_BUFFER);
929 :
930 1 : assert_int_equal(status, LIBSPDM_STATUS_ACQUIRE_FAIL);
931 1 : }
932 :
933 : /**
934 : * Test 3: connection_state equals to zero and makes the check fail, meaning that steps
935 : * GET_CAPABILITIES-CAPABILITIES and NEGOTIATE_ALGORITHMS-ALGORITHMS of the protocol were not previously completed
936 : * Expected Behavior: requester returns the status LIBSPDM_STATUS_INVALID_STATE_LOCAL, with no DIGESTS message received
937 : **/
938 1 : static void libspdm_test_requester_get_digests_err_case3(void **state)
939 : {
940 : libspdm_return_t status;
941 : libspdm_test_context_t *spdm_test_context;
942 : libspdm_context_t *spdm_context;
943 : uint8_t slot_mask;
944 : uint8_t total_digest_buffer[LIBSPDM_MAX_HASH_SIZE * SPDM_MAX_SLOT_COUNT];
945 :
946 1 : spdm_test_context = *state;
947 1 : spdm_context = spdm_test_context->spdm_context;
948 1 : spdm_test_context->case_id = 0x3;
949 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_10 <<
950 : SPDM_VERSION_NUMBER_SHIFT_BIT;
951 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NOT_STARTED;
952 1 : spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP;
953 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
954 1 : libspdm_set_mem(m_libspdm_local_certificate_chain,
955 : sizeof(m_libspdm_local_certificate_chain),
956 : (uint8_t)(0xFF));
957 1 : libspdm_reset_message_b(spdm_context);
958 :
959 1 : libspdm_zero_mem(total_digest_buffer, sizeof(total_digest_buffer));
960 1 : status = libspdm_get_digest(spdm_context, NULL, &slot_mask, &total_digest_buffer);
961 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_STATE_LOCAL);
962 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
963 : assert_int_equal(spdm_context->transcript.message_b.buffer_size, 0);
964 : #endif
965 1 : }
966 :
967 : /**
968 : * Test 4: a request message is successfully sent and an ERROR response message with error code = InvalidRequest is received
969 : * Expected Behavior: requester returns the status LIBSPDM_STATUS_ERROR_PEER, with no DIGESTS message received
970 : **/
971 1 : static void libspdm_test_requester_get_digests_err_case4(void **state)
972 : {
973 : libspdm_return_t status;
974 : libspdm_test_context_t *spdm_test_context;
975 : libspdm_context_t *spdm_context;
976 : uint8_t slot_mask;
977 : uint8_t total_digest_buffer[LIBSPDM_MAX_HASH_SIZE * SPDM_MAX_SLOT_COUNT];
978 :
979 1 : spdm_test_context = *state;
980 1 : spdm_context = spdm_test_context->spdm_context;
981 1 : spdm_test_context->case_id = 0x4;
982 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_10 <<
983 : SPDM_VERSION_NUMBER_SHIFT_BIT;
984 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
985 1 : spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP;
986 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
987 1 : libspdm_set_mem(m_libspdm_local_certificate_chain,
988 : sizeof(m_libspdm_local_certificate_chain),
989 : (uint8_t)(0xFF));
990 1 : libspdm_reset_message_b(spdm_context);
991 :
992 1 : libspdm_zero_mem(total_digest_buffer, sizeof(total_digest_buffer));
993 1 : status = libspdm_get_digest(spdm_context, NULL, &slot_mask, &total_digest_buffer);
994 1 : assert_int_equal(status, LIBSPDM_STATUS_ERROR_PEER);
995 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
996 : assert_int_equal(spdm_context->transcript.message_b.buffer_size, 0);
997 : #endif
998 1 : }
999 :
1000 : /**
1001 : * Test 5: request messages are successfully sent and ERROR response messages with error code = Busy are received in all attempts
1002 : * Expected Behavior: requester returns the status LIBSPDM_STATUS_BUSY_PEER, with no DIGESTS message received
1003 : **/
1004 1 : static void libspdm_test_requester_get_digests_err_case5(void **state)
1005 : {
1006 : libspdm_return_t status;
1007 : libspdm_test_context_t *spdm_test_context;
1008 : libspdm_context_t *spdm_context;
1009 : uint8_t slot_mask;
1010 : uint8_t total_digest_buffer[LIBSPDM_MAX_HASH_SIZE * SPDM_MAX_SLOT_COUNT];
1011 :
1012 1 : spdm_test_context = *state;
1013 1 : spdm_context = spdm_test_context->spdm_context;
1014 1 : spdm_test_context->case_id = 0x5;
1015 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_10 <<
1016 : SPDM_VERSION_NUMBER_SHIFT_BIT;
1017 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
1018 1 : spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP;
1019 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
1020 1 : libspdm_set_mem(m_libspdm_local_certificate_chain,
1021 : sizeof(m_libspdm_local_certificate_chain),
1022 : (uint8_t)(0xFF));
1023 1 : libspdm_reset_message_b(spdm_context);
1024 :
1025 1 : libspdm_zero_mem(total_digest_buffer, sizeof(total_digest_buffer));
1026 1 : status = libspdm_get_digest(spdm_context, NULL, &slot_mask, &total_digest_buffer);
1027 1 : assert_int_equal(status, LIBSPDM_STATUS_BUSY_PEER);
1028 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1029 : assert_int_equal(spdm_context->transcript.message_b.buffer_size, 0);
1030 : #endif
1031 1 : }
1032 :
1033 : /**
1034 : * Test 6: Requester is unable to acquire the receiver buffer.
1035 : * Expected Behavior: returns with error LIBSPDM_STATUS_ACQUIRE_FAIL.
1036 : **/
1037 1 : static void libspdm_test_requester_get_digests_err_case6(void **state)
1038 : {
1039 : libspdm_return_t status;
1040 : libspdm_test_context_t *spdm_test_context;
1041 : libspdm_context_t *spdm_context;
1042 : uint8_t slot_mask;
1043 : uint8_t total_digest_buffer[LIBSPDM_MAX_HASH_SIZE * SPDM_MAX_SLOT_COUNT];
1044 :
1045 1 : spdm_test_context = *state;
1046 1 : spdm_context = spdm_test_context->spdm_context;
1047 1 : spdm_test_context->case_id = 0x2;
1048 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_10 <<
1049 : SPDM_VERSION_NUMBER_SHIFT_BIT;
1050 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
1051 1 : spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP;
1052 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
1053 1 : libspdm_set_mem(m_libspdm_local_certificate_chain,
1054 : sizeof(m_libspdm_local_certificate_chain),
1055 : (uint8_t)(0xFF));
1056 1 : libspdm_reset_message_b(spdm_context);
1057 :
1058 1 : libspdm_zero_mem(total_digest_buffer, sizeof(total_digest_buffer));
1059 :
1060 1 : libspdm_force_error(LIBSPDM_ERR_ACQUIRE_RECEIVER_BUFFER);
1061 1 : status = libspdm_get_digest(spdm_context, NULL, &slot_mask, &total_digest_buffer);
1062 1 : libspdm_release_error(LIBSPDM_ERR_ACQUIRE_RECEIVER_BUFFER);
1063 :
1064 1 : assert_int_equal(status, LIBSPDM_STATUS_ACQUIRE_FAIL);
1065 1 : }
1066 :
1067 : /**
1068 : * Test 7: a request message is successfully sent and an ERROR response message with error code = RequestResynch
1069 : * (Meaning Responder is requesting Requester to reissue GET_VERSION to resynchronize) is received
1070 : * Expected Behavior: requester returns the status RETURN_DEVICE_ERROR, with no DIGESTS message received
1071 : **/
1072 1 : static void libspdm_test_requester_get_digests_err_case7(void **state)
1073 : {
1074 : libspdm_return_t status;
1075 : libspdm_test_context_t *spdm_test_context;
1076 : libspdm_context_t *spdm_context;
1077 : uint8_t slot_mask;
1078 : uint8_t total_digest_buffer[LIBSPDM_MAX_HASH_SIZE * SPDM_MAX_SLOT_COUNT];
1079 :
1080 1 : spdm_test_context = *state;
1081 1 : spdm_context = spdm_test_context->spdm_context;
1082 1 : spdm_test_context->case_id = 0x7;
1083 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_10 <<
1084 : SPDM_VERSION_NUMBER_SHIFT_BIT;
1085 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
1086 1 : spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP;
1087 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
1088 1 : libspdm_set_mem(m_libspdm_local_certificate_chain,
1089 : sizeof(m_libspdm_local_certificate_chain),
1090 : (uint8_t)(0xFF));
1091 1 : libspdm_reset_message_b(spdm_context);
1092 :
1093 1 : libspdm_zero_mem(total_digest_buffer, sizeof(total_digest_buffer));
1094 1 : status = libspdm_get_digest(spdm_context, NULL, &slot_mask, &total_digest_buffer);
1095 1 : assert_int_equal(status, LIBSPDM_STATUS_RESYNCH_PEER);
1096 1 : assert_int_equal(spdm_context->connection_info.connection_state,
1097 : LIBSPDM_CONNECTION_STATE_NOT_STARTED);
1098 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1099 : assert_int_equal(spdm_context->transcript.message_b.buffer_size, 0);
1100 : #endif
1101 1 : }
1102 :
1103 : /**
1104 : * Test 8: request messages are successfully sent and ERROR response messages with error code = ResponseNotReady
1105 : * are received in all attempts
1106 : * Expected Behavior: requester returns the status LIBSPDM_STATUS_ERROR_PEER
1107 : **/
1108 1 : static void libspdm_test_requester_get_digests_err_case8(void **state)
1109 : {
1110 : libspdm_return_t status;
1111 : libspdm_test_context_t *spdm_test_context;
1112 : libspdm_context_t *spdm_context;
1113 : uint8_t slot_mask;
1114 : uint8_t total_digest_buffer[LIBSPDM_MAX_HASH_SIZE * SPDM_MAX_SLOT_COUNT];
1115 :
1116 1 : spdm_test_context = *state;
1117 1 : spdm_context = spdm_test_context->spdm_context;
1118 1 : spdm_test_context->case_id = 0x8;
1119 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_10 <<
1120 : SPDM_VERSION_NUMBER_SHIFT_BIT;
1121 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
1122 1 : spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP;
1123 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
1124 1 : libspdm_set_mem(m_libspdm_local_certificate_chain,
1125 : sizeof(m_libspdm_local_certificate_chain),
1126 : (uint8_t)(0xFF));
1127 1 : libspdm_reset_message_b(spdm_context);
1128 :
1129 1 : libspdm_zero_mem(total_digest_buffer, sizeof(total_digest_buffer));
1130 1 : status = libspdm_get_digest(spdm_context, NULL, &slot_mask, &total_digest_buffer);
1131 1 : assert_int_equal(status, LIBSPDM_STATUS_NOT_READY_PEER);
1132 1 : }
1133 :
1134 : /**
1135 : * Test 9: Invalid SPDM version in the DIGESTS response.
1136 : * Expected Behavior: returns with LIBSPDM_STATUS_INVALID_MSG_FIELD.
1137 : **/
1138 1 : static void libspdm_test_requester_get_digests_err_case9(void **state)
1139 : {
1140 : libspdm_return_t status;
1141 : libspdm_test_context_t *spdm_test_context;
1142 : libspdm_context_t *spdm_context;
1143 : uint8_t slot_mask;
1144 : uint8_t total_digest_buffer[LIBSPDM_MAX_HASH_SIZE * SPDM_MAX_SLOT_COUNT];
1145 :
1146 1 : spdm_test_context = *state;
1147 1 : spdm_context = spdm_test_context->spdm_context;
1148 1 : spdm_test_context->case_id = 0x9;
1149 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_10 <<
1150 : SPDM_VERSION_NUMBER_SHIFT_BIT;
1151 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
1152 1 : spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP;
1153 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
1154 1 : libspdm_set_mem(m_libspdm_local_certificate_chain,
1155 : sizeof(m_libspdm_local_certificate_chain),
1156 : (uint8_t)(0xFF));
1157 1 : libspdm_reset_message_b(spdm_context);
1158 :
1159 1 : libspdm_zero_mem(total_digest_buffer, sizeof(total_digest_buffer));
1160 1 : status = libspdm_get_digest(spdm_context, NULL, &slot_mask, &total_digest_buffer);
1161 :
1162 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
1163 1 : }
1164 :
1165 : /**
1166 : * Test 10: flag cert_cap from CAPABILITIES is not set meaning the Requester does not support DIGESTS and
1167 : * CERTIFICATE response messages
1168 : * Expected Behavior: requester returns the status LIBSPDM_STATUS_UNSUPPORTED_CAP, with no DIGESTS message received
1169 : **/
1170 1 : static void libspdm_test_requester_get_digests_err_case10(void **state)
1171 : {
1172 : libspdm_return_t status;
1173 : libspdm_test_context_t *spdm_test_context;
1174 : libspdm_context_t *spdm_context;
1175 : uint8_t slot_mask;
1176 : uint8_t total_digest_buffer[LIBSPDM_MAX_HASH_SIZE * SPDM_MAX_SLOT_COUNT];
1177 :
1178 1 : spdm_test_context = *state;
1179 1 : spdm_context = spdm_test_context->spdm_context;
1180 1 : spdm_test_context->case_id = 0xA;
1181 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_10 <<
1182 : SPDM_VERSION_NUMBER_SHIFT_BIT;
1183 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
1184 1 : spdm_context->connection_info.capability.flags = 0;
1185 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
1186 1 : libspdm_set_mem(m_libspdm_local_certificate_chain,
1187 : sizeof(m_libspdm_local_certificate_chain),
1188 : (uint8_t)(0xFF));
1189 1 : libspdm_reset_message_b(spdm_context);
1190 :
1191 1 : libspdm_zero_mem(total_digest_buffer, sizeof(total_digest_buffer));
1192 1 : status = libspdm_get_digest(spdm_context, NULL, &slot_mask, &total_digest_buffer);
1193 1 : assert_int_equal(status, LIBSPDM_STATUS_UNSUPPORTED_CAP);
1194 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1195 : assert_int_equal(spdm_context->transcript.message_b.buffer_size, 0);
1196 : #endif
1197 1 : }
1198 :
1199 : /**
1200 : * Test 11: a request message is successfully sent but a failure occurs during the receiving of the response message
1201 : * Expected Behavior: requester returns the status LIBSPDM_STATUS_RECEIVE_FAIL, with no DIGESTS message received
1202 : **/
1203 1 : static void libspdm_test_requester_get_digests_err_case11(void **state)
1204 : {
1205 : libspdm_return_t status;
1206 : libspdm_test_context_t *spdm_test_context;
1207 : libspdm_context_t *spdm_context;
1208 : uint8_t slot_mask;
1209 : uint8_t total_digest_buffer[LIBSPDM_MAX_HASH_SIZE * SPDM_MAX_SLOT_COUNT];
1210 :
1211 1 : spdm_test_context = *state;
1212 1 : spdm_context = spdm_test_context->spdm_context;
1213 1 : spdm_test_context->case_id = 0xB;
1214 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_10 <<
1215 : SPDM_VERSION_NUMBER_SHIFT_BIT;
1216 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
1217 1 : spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP;
1218 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
1219 1 : libspdm_set_mem(m_libspdm_local_certificate_chain,
1220 : sizeof(m_libspdm_local_certificate_chain),
1221 : (uint8_t)(0xFF));
1222 1 : libspdm_reset_message_b(spdm_context);
1223 :
1224 1 : libspdm_zero_mem(total_digest_buffer, sizeof(total_digest_buffer));
1225 1 : status = libspdm_get_digest(spdm_context, NULL, &slot_mask, &total_digest_buffer);
1226 1 : assert_int_equal(status, LIBSPDM_STATUS_RECEIVE_FAIL);
1227 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1228 : assert_int_equal(spdm_context->transcript.message_b.buffer_size,
1229 : 0);
1230 : #endif
1231 1 : }
1232 :
1233 : /**
1234 : * Test 12:
1235 : * Expected Behavior:
1236 : **/
1237 1 : static void libspdm_test_requester_get_digests_err_case12(void **state)
1238 : {
1239 1 : }
1240 :
1241 : /**
1242 : * Test 13: a request message is successfully sent but the request_response_code from the response message is different than the code of SPDM_DIGESTS
1243 : * Expected Behavior: requester returns the status LIBSPDM_STATUS_INVALID_MSG_FIELD, with no DIGESTS message received
1244 : **/
1245 1 : static void libspdm_test_requester_get_digests_err_case13(void **state)
1246 : {
1247 : libspdm_return_t status;
1248 : libspdm_test_context_t *spdm_test_context;
1249 : libspdm_context_t *spdm_context;
1250 : uint8_t slot_mask;
1251 : uint8_t total_digest_buffer[LIBSPDM_MAX_HASH_SIZE * SPDM_MAX_SLOT_COUNT];
1252 :
1253 1 : spdm_test_context = *state;
1254 1 : spdm_context = spdm_test_context->spdm_context;
1255 1 : spdm_test_context->case_id = 0xD;
1256 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_10 <<
1257 : SPDM_VERSION_NUMBER_SHIFT_BIT;
1258 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
1259 1 : spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP;
1260 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
1261 1 : libspdm_set_mem(m_libspdm_local_certificate_chain,
1262 : sizeof(m_libspdm_local_certificate_chain),
1263 : (uint8_t)(0xFF));
1264 1 : libspdm_reset_message_b(spdm_context);
1265 :
1266 1 : libspdm_zero_mem(total_digest_buffer, sizeof(total_digest_buffer));
1267 1 : status = libspdm_get_digest(spdm_context, NULL, &slot_mask, &total_digest_buffer);
1268 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
1269 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1270 : assert_int_equal(spdm_context->transcript.message_b.buffer_size, 0);
1271 : #endif
1272 1 : }
1273 :
1274 : /**
1275 : * Test 14: a request message is successfully sent but the number of digests in the response message is equal to zero
1276 : * Expected Behavior: requester returns the status LIBSPDM_STATUS_INVALID_MSG_FIELD, with no successful DIGESTS message received
1277 : **/
1278 1 : static void libspdm_test_requester_get_digests_err_case14(void **state)
1279 : {
1280 : libspdm_return_t status;
1281 : libspdm_test_context_t *spdm_test_context;
1282 : libspdm_context_t *spdm_context;
1283 : uint8_t slot_mask;
1284 : uint8_t total_digest_buffer[LIBSPDM_MAX_HASH_SIZE * SPDM_MAX_SLOT_COUNT];
1285 :
1286 1 : spdm_test_context = *state;
1287 1 : spdm_context = spdm_test_context->spdm_context;
1288 1 : spdm_test_context->case_id = 0xE;
1289 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_10 <<
1290 : SPDM_VERSION_NUMBER_SHIFT_BIT;
1291 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
1292 1 : spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP;
1293 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
1294 1 : libspdm_set_mem(m_libspdm_local_certificate_chain,
1295 : sizeof(m_libspdm_local_certificate_chain),
1296 : (uint8_t)(0xFF));
1297 1 : libspdm_reset_message_b(spdm_context);
1298 :
1299 1 : libspdm_zero_mem(total_digest_buffer, sizeof(total_digest_buffer));
1300 1 : status = libspdm_get_digest(spdm_context, NULL, &slot_mask, &total_digest_buffer);
1301 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
1302 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1303 : assert_int_equal(spdm_context->transcript.message_b.buffer_size,
1304 : 0);
1305 : #endif
1306 1 : }
1307 :
1308 : /**
1309 : * Test 15:
1310 : * Expected Behavior:
1311 : **/
1312 1 : static void libspdm_test_requester_get_digests_err_case15(void **state)
1313 : {
1314 1 : }
1315 :
1316 : /**
1317 : * Test 16: a request message is successfully sent but the response message cannot be appended to the internal cache since the internal cache is full
1318 : * Expected Behavior: requester returns the status RETURN_SECURITY_VIOLATION
1319 : **/
1320 1 : static void libspdm_test_requester_get_digests_err_case16(void **state)
1321 : {
1322 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1323 : libspdm_return_t status;
1324 : uint8_t slot_mask;
1325 : #endif
1326 : libspdm_test_context_t *spdm_test_context;
1327 : libspdm_context_t *spdm_context;
1328 : uint8_t total_digest_buffer[LIBSPDM_MAX_HASH_SIZE * SPDM_MAX_SLOT_COUNT];
1329 :
1330 1 : spdm_test_context = *state;
1331 1 : spdm_context = spdm_test_context->spdm_context;
1332 1 : spdm_test_context->case_id = 0x10;
1333 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_10 <<
1334 : SPDM_VERSION_NUMBER_SHIFT_BIT;
1335 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
1336 1 : spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP;
1337 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
1338 1 : libspdm_set_mem(m_libspdm_local_certificate_chain,
1339 : sizeof(m_libspdm_local_certificate_chain),
1340 : (uint8_t)(0xFF));
1341 :
1342 1 : libspdm_zero_mem(total_digest_buffer, sizeof(total_digest_buffer));
1343 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1344 : status = libspdm_get_digest(spdm_context, NULL, &slot_mask, &total_digest_buffer);
1345 : assert_int_equal(status, LIBSPDM_STATUS_BUFFER_FULL);
1346 : #endif
1347 1 : }
1348 :
1349 : /**
1350 : * Test 17:
1351 : * Expected Behavior:
1352 : **/
1353 1 : static void libspdm_test_requester_get_digests_err_case17(void **state)
1354 : {
1355 1 : }
1356 :
1357 : /**
1358 : * Test 18: a request message is successfully sent but the number of digests received in the response message is different than
1359 : * the number of bits set in param2 - Slot mask
1360 : * Expected Behavior: requester returns the status RETURN_DEVICE_ERROR, with no successful DIGESTS message received (managed buffer is not shrunk)
1361 : **/
1362 1 : static void libspdm_test_requester_get_digests_err_case18(void **state)
1363 : {
1364 : libspdm_return_t status;
1365 : libspdm_test_context_t *spdm_test_context;
1366 : libspdm_context_t *spdm_context;
1367 : uint8_t slot_mask;
1368 : uint8_t total_digest_buffer[LIBSPDM_MAX_HASH_SIZE * SPDM_MAX_SLOT_COUNT];
1369 :
1370 1 : spdm_test_context = *state;
1371 1 : spdm_context = spdm_test_context->spdm_context;
1372 1 : spdm_test_context->case_id = 0x12;
1373 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_10 <<
1374 : SPDM_VERSION_NUMBER_SHIFT_BIT;
1375 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
1376 1 : spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP;
1377 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
1378 1 : libspdm_set_mem(m_libspdm_local_certificate_chain,
1379 : sizeof(m_libspdm_local_certificate_chain),
1380 : (uint8_t)(0xFF));
1381 1 : libspdm_reset_message_b(spdm_context);
1382 :
1383 1 : libspdm_zero_mem(total_digest_buffer, sizeof(total_digest_buffer));
1384 1 : status = libspdm_get_digest(spdm_context, NULL, &slot_mask, &total_digest_buffer);
1385 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_SIZE);
1386 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1387 : assert_int_equal(spdm_context->transcript.message_b.buffer_size,
1388 : 0);
1389 : #endif
1390 1 : }
1391 :
1392 : #if 0
1393 : /**
1394 : * Test 19: a request message is successfully sent but several digests (except the first) received in the response message are invalid
1395 : * Expected Behavior: requester returns the status RETURN_SECURITY_VIOLATION, with error state LIBSPDM_STATUS_ERROR_CERTIFICATE_FAILURE
1396 : **/
1397 : static void libspdm_test_requester_get_digests_err_case19(void **state)
1398 : {
1399 : libspdm_return_t status;
1400 : libspdm_test_context_t *spdm_test_context;
1401 : libspdm_context_t *spdm_context;
1402 : uint8_t slot_mask;
1403 : uint8_t total_digest_buffer[LIBSPDM_MAX_HASH_SIZE * SPDM_MAX_SLOT_COUNT];
1404 :
1405 : spdm_test_context = *state;
1406 : spdm_context = spdm_test_context->spdm_context;
1407 : spdm_test_context->case_id = 0x13;
1408 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_10 <<
1409 : SPDM_VERSION_NUMBER_SHIFT_BIT;
1410 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
1411 : spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP;
1412 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
1413 : libspdm_set_mem(m_libspdm_local_certificate_chain,
1414 : sizeof(m_libspdm_local_certificate_chain),
1415 : (uint8_t)(0xFF));
1416 : libspdm_reset_message_b(spdm_context);
1417 :
1418 : libspdm_zero_mem(total_digest_buffer, sizeof(total_digest_buffer));
1419 : status = libspdm_get_digest(spdm_context, NULL, &slot_mask, &total_digest_buffer);
1420 : assert_int_equal(status, LIBSPDM_STATUS_VERIF_FAIL);
1421 : }
1422 :
1423 : /**
1424 : * Test 20: a request message is successfully sent but the size of the response message is smaller than the minimum size of a SPDM DIGESTS response,
1425 : * meaning it is an invalid response message.
1426 : * Expected Behavior: requester returns the status RETURN_DEVICE_ERROR, with no successful DIGESTS message received (managed buffer is not shrunk)
1427 : **/
1428 : static void libspdm_test_requester_get_digests_err_case20(void **state)
1429 : {
1430 : libspdm_return_t status;
1431 : libspdm_test_context_t *spdm_test_context;
1432 : libspdm_context_t *spdm_context;
1433 : uint8_t slot_mask;
1434 : uint8_t total_digest_buffer[LIBSPDM_MAX_HASH_SIZE * SPDM_MAX_SLOT_COUNT];
1435 :
1436 : spdm_test_context = *state;
1437 : spdm_context = spdm_test_context->spdm_context;
1438 : spdm_test_context->case_id = 0x14;
1439 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_10 <<
1440 : SPDM_VERSION_NUMBER_SHIFT_BIT;
1441 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
1442 : spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP;
1443 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
1444 : libspdm_set_mem(m_libspdm_local_certificate_chain,
1445 : sizeof(m_libspdm_local_certificate_chain),
1446 : (uint8_t)(0xFF));
1447 : libspdm_reset_message_b(spdm_context);
1448 :
1449 : libspdm_zero_mem(total_digest_buffer, sizeof(total_digest_buffer));
1450 : status = libspdm_get_digest(spdm_context, NULL, &slot_mask, &total_digest_buffer);
1451 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_SIZE);
1452 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1453 : assert_int_equal(spdm_context->transcript.message_b.buffer_size,
1454 : sizeof(spdm_get_digest_request_t));
1455 : #endif
1456 : }
1457 :
1458 : /**
1459 : * Test 21: a request message is successfully sent but the size of the response message is bigger than the maximum size of a SPDM DIGESTS response,
1460 : * meaning it is an invalid response message.
1461 : * Expected Behavior: requester returns the status RETURN_DEVICE_ERROR, with no successful DIGESTS message received (managed buffer is not shrunk)
1462 : **/
1463 : static void libspdm_test_requester_get_digests_err_case21(void **state)
1464 : {
1465 : libspdm_return_t status;
1466 : libspdm_test_context_t *spdm_test_context;
1467 : libspdm_context_t *spdm_context;
1468 : uint8_t slot_mask;
1469 : uint8_t total_digest_buffer[LIBSPDM_MAX_HASH_SIZE * SPDM_MAX_SLOT_COUNT];
1470 :
1471 : spdm_test_context = *state;
1472 : spdm_context = spdm_test_context->spdm_context;
1473 : spdm_test_context->case_id = 0x15;
1474 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_10 <<
1475 : SPDM_VERSION_NUMBER_SHIFT_BIT;
1476 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
1477 : spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP;
1478 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
1479 : libspdm_set_mem(m_libspdm_local_certificate_chain,
1480 : sizeof(m_libspdm_local_certificate_chain),
1481 : (uint8_t)(0xFF));
1482 : libspdm_reset_message_b(spdm_context);
1483 :
1484 : libspdm_zero_mem(total_digest_buffer, sizeof(total_digest_buffer));
1485 : status = libspdm_get_digest(spdm_context, NULL, &slot_mask, &total_digest_buffer);
1486 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_SIZE);
1487 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1488 : assert_int_equal(spdm_context->transcript.message_b.buffer_size,
1489 : sizeof(spdm_get_digest_request_t));
1490 : #endif
1491 : }
1492 : #endif
1493 :
1494 : /**
1495 : * Test 22: receiving an unexpected ERROR message from the responder.
1496 : * There are tests for all named codes, including some reserved ones
1497 : * (namely, 0x00, 0x0b, 0x0c, 0x3f, 0xfd, 0xfe).
1498 : * However, for having specific test cases, it is excluded from this case:
1499 : * Busy (0x03), ResponseNotReady (0x42), and RequestResync (0x43).
1500 : * Expected behavior: client returns a status of RETURN_DEVICE_ERROR.
1501 : **/
1502 1 : static void libspdm_test_requester_get_digests_err_case22(void **state) {
1503 : libspdm_return_t status;
1504 : libspdm_test_context_t *spdm_test_context;
1505 : libspdm_context_t *spdm_context;
1506 : uint8_t slot_mask;
1507 : uint8_t total_digest_buffer[LIBSPDM_MAX_HASH_SIZE * SPDM_MAX_SLOT_COUNT];
1508 : uint16_t error_code;
1509 :
1510 1 : spdm_test_context = *state;
1511 1 : spdm_context = spdm_test_context->spdm_context;
1512 1 : spdm_test_context->case_id = 0x16;
1513 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
1514 : SPDM_VERSION_NUMBER_SHIFT_BIT;
1515 1 : spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP;
1516 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
1517 1 : libspdm_set_mem (m_libspdm_local_certificate_chain,
1518 : sizeof(m_libspdm_local_certificate_chain),
1519 : (uint8_t)(0xFF));
1520 :
1521 1 : error_code = LIBSPDM_ERROR_CODE_RESERVED_00;
1522 19 : while(error_code <= 0xff) {
1523 18 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
1524 18 : libspdm_reset_message_b(spdm_context);
1525 :
1526 18 : libspdm_zero_mem (total_digest_buffer, sizeof(total_digest_buffer));
1527 18 : status = libspdm_get_digest (spdm_context, NULL, &slot_mask, &total_digest_buffer);
1528 18 : LIBSPDM_ASSERT_INT_EQUAL_CASE (status, LIBSPDM_STATUS_ERROR_PEER, error_code);
1529 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1530 : LIBSPDM_ASSERT_INT_EQUAL_CASE (spdm_context->transcript.message_b.buffer_size, 0,
1531 : error_code);
1532 : #endif
1533 :
1534 18 : error_code++;
1535 18 : if(error_code == SPDM_ERROR_CODE_BUSY) { /*busy is treated in cases 5 and 6*/
1536 1 : error_code = SPDM_ERROR_CODE_UNEXPECTED_REQUEST;
1537 : }
1538 18 : if(error_code == LIBSPDM_ERROR_CODE_RESERVED_0D) { /*skip some reserved error codes (0d to 3e)*/
1539 1 : error_code = LIBSPDM_ERROR_CODE_RESERVED_3F;
1540 : }
1541 18 : if(error_code == SPDM_ERROR_CODE_RESPONSE_NOT_READY) { /*skip response not ready, request resync, and some reserved codes (44 to fc)*/
1542 1 : error_code = LIBSPDM_ERROR_CODE_RESERVED_FD;
1543 : }
1544 : }
1545 1 : }
1546 :
1547 : /**
1548 : * Test 23: Responder returns invalid combination of SupportedSlotMask and ProvisionedSlotMask.
1549 : * Expected Behavior: returns LIBSPDM_STATUS_INVALID_MSG_FIELD.
1550 : **/
1551 1 : static void libspdm_test_requester_get_digests_err_case23(void **state)
1552 : {
1553 : libspdm_return_t status;
1554 : libspdm_test_context_t *spdm_test_context;
1555 : libspdm_context_t *spdm_context;
1556 : uint8_t slot_mask;
1557 : uint8_t total_digest_buffer[LIBSPDM_MAX_HASH_SIZE * SPDM_MAX_SLOT_COUNT];
1558 :
1559 1 : spdm_test_context = *state;
1560 1 : spdm_context = spdm_test_context->spdm_context;
1561 1 : spdm_test_context->case_id = 0x17;
1562 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 <<
1563 : SPDM_VERSION_NUMBER_SHIFT_BIT;
1564 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
1565 1 : spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP;
1566 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
1567 1 : libspdm_set_mem(m_libspdm_local_certificate_chain,
1568 : sizeof(m_libspdm_local_certificate_chain),
1569 : (uint8_t)(0xFF));
1570 1 : libspdm_reset_message_b(spdm_context);
1571 :
1572 1 : libspdm_zero_mem(total_digest_buffer, sizeof(total_digest_buffer));
1573 1 : status = libspdm_get_digest(spdm_context, NULL, &slot_mask, &total_digest_buffer);
1574 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
1575 :
1576 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1577 : assert_int_equal(spdm_context->transcript.message_b.buffer_size, 0);
1578 : #endif
1579 1 : }
1580 :
1581 : /**
1582 : * Test 24:
1583 : * Expected Behavior:
1584 : **/
1585 1 : static void libspdm_test_requester_get_digests_err_case24(void **state)
1586 : {
1587 1 : }
1588 :
1589 : /**
1590 : * Test 25:
1591 : * Expected Behavior:
1592 : **/
1593 1 : static void libspdm_test_requester_get_digests_err_case25(void **state)
1594 : {
1595 1 : }
1596 :
1597 1 : int libspdm_requester_get_digests_error_test_main(void)
1598 : {
1599 1 : const struct CMUnitTest spdm_requester_get_digests_tests[] = {
1600 : cmocka_unit_test(libspdm_test_requester_get_digests_err_case1),
1601 : cmocka_unit_test(libspdm_test_requester_get_digests_err_case2),
1602 : cmocka_unit_test(libspdm_test_requester_get_digests_err_case3),
1603 : cmocka_unit_test(libspdm_test_requester_get_digests_err_case4),
1604 : cmocka_unit_test(libspdm_test_requester_get_digests_err_case5),
1605 : cmocka_unit_test(libspdm_test_requester_get_digests_err_case6),
1606 : cmocka_unit_test(libspdm_test_requester_get_digests_err_case7),
1607 : cmocka_unit_test(libspdm_test_requester_get_digests_err_case8),
1608 : cmocka_unit_test(libspdm_test_requester_get_digests_err_case9),
1609 : cmocka_unit_test(libspdm_test_requester_get_digests_err_case10),
1610 : cmocka_unit_test(libspdm_test_requester_get_digests_err_case11),
1611 : cmocka_unit_test(libspdm_test_requester_get_digests_err_case12),
1612 : cmocka_unit_test(libspdm_test_requester_get_digests_err_case13),
1613 : cmocka_unit_test(libspdm_test_requester_get_digests_err_case14),
1614 : cmocka_unit_test(libspdm_test_requester_get_digests_err_case15),
1615 : cmocka_unit_test(libspdm_test_requester_get_digests_err_case16),
1616 : cmocka_unit_test(libspdm_test_requester_get_digests_err_case17),
1617 : cmocka_unit_test(libspdm_test_requester_get_digests_err_case18),
1618 : /* cmocka_unit_test(libspdm_test_requester_get_digests_err_case19),
1619 : * cmocka_unit_test(libspdm_test_requester_get_digests_err_case20),
1620 : * cmocka_unit_test(libspdm_test_requester_get_digests_err_case21), */
1621 : cmocka_unit_test(libspdm_test_requester_get_digests_err_case22),
1622 : cmocka_unit_test(libspdm_test_requester_get_digests_err_case23),
1623 : cmocka_unit_test(libspdm_test_requester_get_digests_err_case24),
1624 : cmocka_unit_test(libspdm_test_requester_get_digests_err_case25),
1625 : };
1626 :
1627 1 : libspdm_test_context_t test_context = {
1628 : LIBSPDM_TEST_CONTEXT_VERSION,
1629 : true,
1630 : libspdm_requester_get_digests_test_send_message,
1631 : libspdm_requester_get_digests_test_receive_message,
1632 : };
1633 :
1634 1 : libspdm_setup_test_context(&test_context);
1635 :
1636 1 : return cmocka_run_group_tests(spdm_requester_get_digests_tests,
1637 : libspdm_unit_test_group_setup,
1638 : libspdm_unit_test_group_teardown);
1639 : }
1640 :
1641 : #endif /* LIBSPDM_SEND_GET_CERTIFICATE_SUPPORT */
|