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