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 :
10 : #if LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP
11 :
12 : static uint8_t m_libspdm_local_certificate_chain[LIBSPDM_MAX_CERT_CHAIN_SIZE];
13 : static uint8_t temp_buf[LIBSPDM_RECEIVER_BUFFER_SIZE];
14 : static uint8_t temp_buff[LIBSPDM_MAX_SPDM_MSG_SIZE];
15 :
16 21 : static libspdm_return_t send_message(
17 : void *spdm_context, size_t request_size, const void *request, uint64_t timeout)
18 : {
19 : libspdm_test_context_t *spdm_test_context;
20 :
21 21 : spdm_test_context = libspdm_get_test_context();
22 : static uint8_t sub_index = 0;
23 : spdm_deliver_encapsulated_response_request_t *spdm_deliver_encapsulated_response_request;
24 : uint8_t send_temp_buf[LIBSPDM_SENDER_BUFFER_SIZE];
25 : size_t decode_message_size;
26 : spdm_error_response_t *spdm_response;
27 : libspdm_return_t status;
28 : uint32_t *message_session_id;
29 : bool is_message_app_message;
30 : uint8_t message_buffer[LIBSPDM_SENDER_BUFFER_SIZE];
31 :
32 21 : memcpy(message_buffer, request, request_size);
33 :
34 21 : switch (spdm_test_context->case_id)
35 : {
36 1 : case 0x1:
37 1 : return LIBSPDM_STATUS_SEND_FAIL;
38 2 : case 0x2:
39 2 : return LIBSPDM_STATUS_SUCCESS;
40 0 : case 0x3:
41 0 : return LIBSPDM_STATUS_SUCCESS;
42 1 : case 0x4:
43 1 : return LIBSPDM_STATUS_SUCCESS;
44 2 : case 0x5:
45 2 : return LIBSPDM_STATUS_SUCCESS;
46 0 : case 0x6:
47 0 : return LIBSPDM_STATUS_SUCCESS;
48 2 : case 0x7:
49 2 : return LIBSPDM_STATUS_SUCCESS;
50 2 : case 0x8:
51 2 : return LIBSPDM_STATUS_SUCCESS;
52 2 : case 0x9:
53 2 : return LIBSPDM_STATUS_SUCCESS;
54 2 : case 0xA:
55 : {
56 2 : if (sub_index == 0) {
57 1 : sub_index++;
58 1 : } else if (sub_index == 1) {
59 1 : sub_index = 0;
60 1 : message_session_id = NULL;
61 1 : is_message_app_message = false;
62 1 : spdm_deliver_encapsulated_response_request = (void*) send_temp_buf;
63 1 : decode_message_size = sizeof(spdm_deliver_encapsulated_response_request_t) +
64 : sizeof(spdm_error_response_t);
65 1 : status = libspdm_transport_test_decode_message(spdm_context, &message_session_id,
66 : &is_message_app_message, true,
67 : request_size, message_buffer,
68 : &decode_message_size,
69 : (void **)&spdm_deliver_encapsulated_response_request);
70 1 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
71 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR, "transport_decode_message - %xu\n",
72 : status));
73 : }
74 1 : spdm_response = (void*) (spdm_deliver_encapsulated_response_request + 1);
75 1 : if (spdm_response->header.request_response_code != SPDM_ERROR) {
76 0 : return LIBSPDM_STATUS_SEND_FAIL;
77 : }
78 1 : if (spdm_response->header.param1 != SPDM_ERROR_CODE_UNEXPECTED_REQUEST) { /* Here check ErrorCode should be UnexpectedRequestd */
79 0 : return LIBSPDM_STATUS_SEND_FAIL;
80 : }
81 1 : if (spdm_response->header.param2 != 0) {
82 0 : return LIBSPDM_STATUS_SEND_FAIL;
83 : }
84 : }
85 2 : return LIBSPDM_STATUS_SUCCESS;
86 : }
87 1 : case 0xb:
88 1 : return LIBSPDM_STATUS_SUCCESS;
89 1 : case 0xc:
90 1 : return LIBSPDM_STATUS_SUCCESS;
91 2 : case 0xD:
92 2 : return LIBSPDM_STATUS_SUCCESS;
93 1 : case 0xE:
94 1 : return LIBSPDM_STATUS_SUCCESS;
95 2 : case 0xF:
96 2 : return LIBSPDM_STATUS_SUCCESS;
97 0 : default:
98 0 : return LIBSPDM_STATUS_SEND_FAIL;
99 : }
100 : }
101 :
102 20 : static libspdm_return_t receive_message(
103 : void *spdm_context, size_t *response_size, void **response, uint64_t timeout)
104 : {
105 :
106 : libspdm_test_context_t *spdm_test_context;
107 : spdm_encapsulated_request_response_t *libspdm_encapsulated_request_response;
108 : uint8_t *digest;
109 : size_t temp_buf_size;
110 : uint8_t *temp_buf_ptr;
111 :
112 20 : spdm_test_context = libspdm_get_test_context();
113 : static uint8_t sub_index = 0;
114 20 : switch (spdm_test_context->case_id)
115 : {
116 0 : case 0x1:
117 0 : return LIBSPDM_STATUS_RECEIVE_FAIL;
118 :
119 2 : case 0x2:
120 : {
121 : spdm_digest_response_t *spdm_response;
122 :
123 : ((libspdm_context_t *)spdm_context)
124 2 : ->connection_info.algorithm.base_hash_algo =
125 : m_libspdm_use_hash_algo;
126 2 : if (sub_index == 0) {
127 1 : temp_buf_size = sizeof(spdm_digest_response_t) +
128 1 : libspdm_get_hash_size(m_libspdm_use_hash_algo) +
129 : sizeof(spdm_encapsulated_request_response_t);
130 1 : libspdm_zero_mem(temp_buf, LIBSPDM_RECEIVER_BUFFER_SIZE);
131 1 : temp_buf_ptr = temp_buf + sizeof(libspdm_test_message_header_t);
132 1 : libspdm_encapsulated_request_response = (void*) temp_buf_ptr;
133 1 : libspdm_encapsulated_request_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
134 1 : libspdm_encapsulated_request_response->header.request_response_code =
135 : SPDM_ENCAPSULATED_REQUEST;
136 1 : libspdm_encapsulated_request_response->header.param1 = 0;
137 1 : libspdm_encapsulated_request_response->header.param2 = 0;
138 :
139 1 : spdm_response = (void *)(temp_buf_ptr
140 : + sizeof(spdm_encapsulated_request_response_t));
141 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
142 1 : spdm_response->header.request_response_code = SPDM_GET_DIGESTS;
143 1 : spdm_response->header.param1 = 0;
144 1 : spdm_response->header.param2 = 0;
145 1 : libspdm_set_mem(m_libspdm_local_certificate_chain,
146 : sizeof(m_libspdm_local_certificate_chain),
147 : (uint8_t)(0xFF));
148 :
149 1 : digest = (void *)(spdm_response + 1);
150 1 : libspdm_hash_all(m_libspdm_use_hash_algo, m_libspdm_local_certificate_chain,
151 : sizeof(m_libspdm_local_certificate_chain), &digest[0]);
152 1 : spdm_response->header.param2 |= (0x01 << 0);
153 1 : sub_index++;
154 1 : } else if (sub_index == 1) {
155 : /*When the version is SPDM_MESSAGE_VERSION_12, use the following code*/
156 : spdm_message_header_t *spdm_encapsulated_response_ack_response;
157 1 : temp_buf_size = sizeof(spdm_message_header_t);
158 1 : libspdm_zero_mem(temp_buf, LIBSPDM_RECEIVER_BUFFER_SIZE);
159 1 : temp_buf_ptr = temp_buf + sizeof(libspdm_test_message_header_t);
160 1 : spdm_encapsulated_response_ack_response = (void*) temp_buf_ptr;
161 1 : spdm_encapsulated_response_ack_response->spdm_version = SPDM_MESSAGE_VERSION_11;
162 1 : spdm_encapsulated_response_ack_response->request_response_code =
163 : SPDM_ENCAPSULATED_RESPONSE_ACK;
164 1 : spdm_encapsulated_response_ack_response->param1 = 0;
165 1 : spdm_encapsulated_response_ack_response->param2 =
166 : SPDM_ENCAPSULATED_RESPONSE_ACK_RESPONSE_PAYLOAD_TYPE_ABSENT;
167 1 : sub_index = 0;
168 : } else {
169 0 : temp_buf_size = 0;
170 0 : temp_buf_ptr = NULL;
171 : }
172 2 : libspdm_transport_test_encode_message(spdm_context, NULL, false, false,
173 : temp_buf_size, temp_buf_ptr,
174 : response_size, response);
175 : }
176 2 : return LIBSPDM_STATUS_SUCCESS;
177 0 : case 0x3:
178 : {
179 : spdm_digest_response_t *spdm_response;
180 :
181 : ((libspdm_context_t *)spdm_context)
182 0 : ->connection_info.algorithm.base_hash_algo =
183 : m_libspdm_use_hash_algo;
184 0 : if (sub_index == 0) {
185 0 : temp_buf_size = sizeof(spdm_digest_response_t) +
186 0 : libspdm_get_hash_size(m_libspdm_use_hash_algo) +
187 : sizeof(spdm_encapsulated_request_response_t);
188 0 : libspdm_zero_mem(temp_buf, LIBSPDM_RECEIVER_BUFFER_SIZE);
189 0 : temp_buf_ptr = temp_buf + sizeof(libspdm_test_message_header_t);
190 0 : libspdm_encapsulated_request_response = (void *) temp_buf_ptr;
191 0 : libspdm_encapsulated_request_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
192 0 : libspdm_encapsulated_request_response->header.request_response_code =
193 : SPDM_ENCAPSULATED_REQUEST;
194 0 : libspdm_encapsulated_request_response->header.param1 = 0;
195 0 : libspdm_encapsulated_request_response->header.param2 = 0;
196 :
197 0 : spdm_response =
198 : (void *)(temp_buf_ptr + sizeof(spdm_encapsulated_request_response_t));
199 0 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
200 0 : spdm_response->header.request_response_code = SPDM_GET_DIGESTS;
201 0 : spdm_response->header.param1 = 0;
202 0 : spdm_response->header.param2 = 0;
203 0 : libspdm_set_mem(m_libspdm_local_certificate_chain,
204 : sizeof(m_libspdm_local_certificate_chain),
205 : (uint8_t)(0xFF));
206 :
207 0 : digest = (void *)(spdm_response + 1);
208 0 : libspdm_hash_all(m_libspdm_use_hash_algo, m_libspdm_local_certificate_chain,
209 : sizeof(m_libspdm_local_certificate_chain), &digest[0]);
210 0 : spdm_response->header.param2 |= (0x01 << 0);
211 0 : sub_index++;
212 0 : } else if (sub_index == 1) {
213 : /*When the version is SPDM_MESSAGE_VERSION_12, use the following code*/
214 : spdm_encapsulated_response_ack_response_t *spdm_encapsulated_response_ack_response;
215 0 : temp_buf_size = sizeof(spdm_encapsulated_response_ack_response_t);
216 0 : libspdm_zero_mem(temp_buf, LIBSPDM_RECEIVER_BUFFER_SIZE);
217 0 : temp_buf_ptr = temp_buf + sizeof(libspdm_test_message_header_t);
218 0 : spdm_encapsulated_response_ack_response = (void *) temp_buf_ptr;
219 0 : spdm_encapsulated_response_ack_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
220 0 : spdm_encapsulated_response_ack_response->header.request_response_code =
221 : SPDM_ENCAPSULATED_RESPONSE_ACK;
222 0 : spdm_encapsulated_response_ack_response->header.param1 = 0;
223 0 : spdm_encapsulated_response_ack_response->header.param2 =
224 : SPDM_ENCAPSULATED_RESPONSE_ACK_RESPONSE_PAYLOAD_TYPE_ABSENT;
225 0 : sub_index = 0;
226 : } else {
227 0 : temp_buf_size = 0;
228 0 : temp_buf_ptr = NULL;
229 : }
230 0 : libspdm_transport_test_encode_message(spdm_context, NULL, false, false,
231 : temp_buf_size, temp_buf_ptr,
232 : response_size, response);
233 : }
234 0 : return LIBSPDM_STATUS_SUCCESS;
235 1 : case 0x4:
236 : {
237 : spdm_digest_response_t *spdm_response;
238 :
239 : ((libspdm_context_t *)spdm_context)
240 1 : ->connection_info.algorithm.base_hash_algo =
241 : m_libspdm_use_hash_algo;
242 1 : temp_buf_size = sizeof(spdm_encapsulated_request_response_t);
243 1 : libspdm_zero_mem(temp_buf, LIBSPDM_RECEIVER_BUFFER_SIZE);
244 1 : temp_buf_ptr = temp_buf + sizeof(libspdm_test_message_header_t);
245 1 : spdm_response = (void *) temp_buf_ptr;
246 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
247 1 : spdm_response->header.request_response_code = SPDM_ENCAPSULATED_REQUEST;
248 1 : spdm_response->header.param1 = 0;
249 1 : spdm_response->header.param2 = 0;
250 :
251 1 : libspdm_transport_test_encode_message(spdm_context, NULL, false, false,
252 : temp_buf_size, temp_buf_ptr,
253 : response_size, response);
254 : }
255 1 : return LIBSPDM_STATUS_SUCCESS;
256 2 : case 0x5:
257 : {
258 : spdm_digest_response_t *spdm_response;
259 :
260 : ((libspdm_context_t *)spdm_context)
261 2 : ->connection_info.algorithm.base_hash_algo =
262 : m_libspdm_use_hash_algo;
263 2 : if (sub_index == 0) {
264 1 : temp_buf_size = sizeof(spdm_digest_response_t) +
265 1 : libspdm_get_hash_size(m_libspdm_use_hash_algo) +
266 : sizeof(spdm_encapsulated_request_response_t);
267 1 : libspdm_zero_mem(temp_buf, LIBSPDM_RECEIVER_BUFFER_SIZE);
268 1 : temp_buf_ptr = temp_buf + sizeof(libspdm_test_message_header_t);
269 1 : libspdm_encapsulated_request_response = (void *)temp_buf_ptr;
270 1 : libspdm_encapsulated_request_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
271 1 : libspdm_encapsulated_request_response->header.request_response_code =
272 : SPDM_ENCAPSULATED_REQUEST;
273 1 : libspdm_encapsulated_request_response->header.param1 = 0;
274 1 : libspdm_encapsulated_request_response->header.param2 = 0;
275 :
276 1 : spdm_response =
277 : (void *)(temp_buf_ptr + sizeof(spdm_encapsulated_request_response_t));
278 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
279 1 : spdm_response->header.request_response_code = SPDM_GET_DIGESTS;
280 1 : spdm_response->header.param1 = 0;
281 1 : spdm_response->header.param2 = 0;
282 1 : libspdm_set_mem(m_libspdm_local_certificate_chain,
283 : sizeof(m_libspdm_local_certificate_chain),
284 : (uint8_t)(0xFF));
285 :
286 1 : digest = (void *)(spdm_response + 1);
287 1 : libspdm_hash_all(m_libspdm_use_hash_algo, m_libspdm_local_certificate_chain,
288 : sizeof(m_libspdm_local_certificate_chain), &digest[0]);
289 1 : spdm_response->header.param2 |= (0x01 << 0);
290 :
291 1 : libspdm_transport_test_encode_message(spdm_context, NULL, false, false,
292 : temp_buf_size, temp_buf_ptr,
293 : response_size, response);
294 1 : sub_index++;
295 1 : } else if (sub_index == 1) {
296 : size_t temp_buff_size;
297 : uint8_t *temp_buff_ptr;
298 :
299 1 : temp_buff_size = sizeof(spdm_message_header_t);
300 1 : libspdm_zero_mem(temp_buff, LIBSPDM_MAX_SPDM_MSG_SIZE);
301 1 : temp_buff_ptr = temp_buff + sizeof(libspdm_test_message_header_t);
302 :
303 1 : sub_index = 0;
304 1 : libspdm_transport_test_encode_message(spdm_context, NULL, false, false,
305 : temp_buff_size, temp_buff_ptr,
306 : response_size, response);
307 : }
308 : }
309 2 : return LIBSPDM_STATUS_SUCCESS;
310 0 : case 0x6:
311 : {
312 : spdm_digest_response_t *spdm_response;
313 :
314 : ((libspdm_context_t *)spdm_context)
315 0 : ->connection_info.algorithm.base_hash_algo =
316 : m_libspdm_use_hash_algo;
317 0 : if (sub_index == 0) {
318 0 : temp_buf_size =
319 : sizeof(spdm_digest_response_t) +
320 0 : libspdm_get_hash_size(m_libspdm_use_hash_algo) +
321 : sizeof(spdm_encapsulated_request_response_t);
322 0 : libspdm_zero_mem(temp_buf, LIBSPDM_RECEIVER_BUFFER_SIZE);
323 0 : temp_buf_ptr = temp_buf + sizeof(libspdm_test_message_header_t);
324 :
325 0 : libspdm_encapsulated_request_response = (void*) temp_buf_ptr;
326 0 : libspdm_encapsulated_request_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
327 0 : libspdm_encapsulated_request_response->header.request_response_code =
328 : SPDM_ENCAPSULATED_REQUEST;
329 0 : libspdm_encapsulated_request_response->header.param1 = 0;
330 0 : libspdm_encapsulated_request_response->header.param2 = 0;
331 :
332 0 : spdm_response =
333 : (void*) (temp_buf_ptr + sizeof(spdm_encapsulated_request_response_t));
334 0 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
335 0 : spdm_response->header.request_response_code = SPDM_GET_DIGESTS;
336 0 : spdm_response->header.param1 = 0;
337 0 : spdm_response->header.param2 = 0;
338 0 : libspdm_set_mem(m_libspdm_local_certificate_chain,
339 : sizeof(m_libspdm_local_certificate_chain),
340 : (uint8_t) (0xFF));
341 :
342 0 : digest = (void*) (spdm_response + 1);
343 0 : libspdm_hash_all(m_libspdm_use_hash_algo, m_libspdm_local_certificate_chain,
344 : sizeof(m_libspdm_local_certificate_chain), &digest[0]);
345 0 : spdm_response->header.param2 |= (0x01 << 0);
346 0 : sub_index++;
347 0 : } else if (sub_index == 1) {
348 : spdm_encapsulated_response_ack_response_t* spdm_encapsulated_response_ack_response;
349 0 : temp_buf_size = sizeof(spdm_encapsulated_response_ack_response_t);
350 0 : libspdm_zero_mem(temp_buf, LIBSPDM_RECEIVER_BUFFER_SIZE);
351 0 : temp_buf_ptr = temp_buf + sizeof(libspdm_test_message_header_t);
352 0 : spdm_encapsulated_response_ack_response = (void*) temp_buf_ptr;
353 0 : spdm_encapsulated_response_ack_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
354 0 : spdm_encapsulated_response_ack_response->header.request_response_code =
355 : SPDM_ENCAPSULATED_RESPONSE_ACK;
356 0 : spdm_encapsulated_response_ack_response->header.param1 = 0;
357 0 : spdm_encapsulated_response_ack_response->header.param2 = 0;
358 0 : sub_index = 0;
359 : } else {
360 0 : temp_buf_size = 0;
361 0 : temp_buf_ptr = NULL;
362 : }
363 0 : libspdm_transport_test_encode_message(spdm_context, NULL, false, false,
364 : temp_buf_size, temp_buf_ptr,
365 : response_size, response);
366 : }
367 0 : return LIBSPDM_STATUS_SUCCESS;
368 2 : case 0x7:
369 : {
370 : spdm_digest_response_t *spdm_response;
371 :
372 : ((libspdm_context_t *)spdm_context)
373 2 : ->connection_info.algorithm.base_hash_algo =
374 : m_libspdm_use_hash_algo;
375 2 : if (sub_index == 0) {
376 1 : temp_buf_size = sizeof(spdm_digest_response_t) +
377 1 : libspdm_get_hash_size(m_libspdm_use_hash_algo) +
378 : sizeof(spdm_encapsulated_request_response_t);
379 1 : libspdm_zero_mem(temp_buf, LIBSPDM_RECEIVER_BUFFER_SIZE);
380 1 : temp_buf_ptr = temp_buf + sizeof(libspdm_test_message_header_t);
381 1 : libspdm_encapsulated_request_response = (void *)temp_buf_ptr;
382 1 : libspdm_encapsulated_request_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
383 1 : libspdm_encapsulated_request_response->header.request_response_code =
384 : SPDM_ENCAPSULATED_REQUEST;
385 1 : libspdm_encapsulated_request_response->header.param1 = 0;
386 1 : libspdm_encapsulated_request_response->header.param2 = 0;
387 :
388 1 : spdm_response =
389 : (void *)(temp_buf_ptr + sizeof(spdm_encapsulated_request_response_t));
390 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
391 1 : spdm_response->header.request_response_code = SPDM_GET_DIGESTS;
392 1 : spdm_response->header.param1 = 0;
393 1 : spdm_response->header.param2 = 0;
394 1 : libspdm_set_mem(m_libspdm_local_certificate_chain,
395 : sizeof(m_libspdm_local_certificate_chain),
396 : (uint8_t)(0xFF));
397 :
398 1 : digest = (void *)(spdm_response + 1);
399 1 : libspdm_hash_all(m_libspdm_use_hash_algo, m_libspdm_local_certificate_chain,
400 : sizeof(m_libspdm_local_certificate_chain), &digest[0]);
401 1 : spdm_response->header.param2 |= (0x01 << 0);
402 1 : sub_index++;
403 1 : } else if (sub_index == 1) {
404 : spdm_encapsulated_response_ack_response_t *spdm_encapsulated_response_ack_response;
405 1 : temp_buf_size = sizeof(spdm_encapsulated_response_ack_response_t);
406 1 : libspdm_zero_mem(temp_buf, LIBSPDM_RECEIVER_BUFFER_SIZE);
407 1 : temp_buf_ptr = temp_buf + sizeof(libspdm_test_message_header_t);
408 1 : spdm_encapsulated_response_ack_response = (void *) temp_buf_ptr;
409 1 : spdm_encapsulated_response_ack_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
410 1 : spdm_encapsulated_response_ack_response->header.request_response_code =
411 : SPDM_ENCAPSULATED_RESPONSE_ACK;
412 1 : spdm_encapsulated_response_ack_response->header.param1 = 0;
413 1 : spdm_encapsulated_response_ack_response->header.param2 =
414 : SPDM_ENCAPSULATED_RESPONSE_ACK_RESPONSE_PAYLOAD_TYPE_REQ_SLOT_NUMBER;
415 1 : sub_index = 0;
416 : } else {
417 0 : temp_buf_size = 0;
418 0 : temp_buf_ptr = NULL;
419 : }
420 2 : libspdm_transport_test_encode_message(spdm_context, NULL, false, false,
421 : temp_buf_size, temp_buf_ptr,
422 : response_size, response);
423 : }
424 2 : return LIBSPDM_STATUS_SUCCESS;
425 2 : case 0x8:
426 : {
427 : spdm_get_certificate_request_t *spdm_response;
428 :
429 : ((libspdm_context_t *)spdm_context)
430 2 : ->connection_info.algorithm.base_hash_algo =
431 : m_libspdm_use_hash_algo;
432 2 : if (sub_index == 0) {
433 1 : temp_buf_size = sizeof(spdm_get_certificate_request_t) +
434 : sizeof(spdm_encapsulated_request_response_t);
435 1 : libspdm_zero_mem(temp_buf, LIBSPDM_RECEIVER_BUFFER_SIZE);
436 1 : temp_buf_ptr = temp_buf + sizeof(libspdm_test_message_header_t);
437 1 : libspdm_encapsulated_request_response = (void *)temp_buf_ptr;
438 1 : libspdm_encapsulated_request_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
439 1 : libspdm_encapsulated_request_response->header.request_response_code =
440 : SPDM_ENCAPSULATED_REQUEST;
441 1 : libspdm_encapsulated_request_response->header.param1 = 0;
442 1 : libspdm_encapsulated_request_response->header.param2 = 0;
443 :
444 1 : spdm_response =
445 : (void *)(temp_buf_ptr + sizeof(spdm_encapsulated_request_response_t));
446 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
447 1 : spdm_response->header.request_response_code = SPDM_GET_CERTIFICATE;
448 1 : spdm_response->header.param1 = 0;
449 1 : spdm_response->header.param2 = 0;
450 1 : spdm_response->offset = 0;
451 1 : spdm_response->length = LIBSPDM_MAX_CERT_CHAIN_BLOCK_LEN;
452 1 : sub_index++;
453 1 : } else if (sub_index == 1) {
454 : /*When the version is SPDM_MESSAGE_VERSION_12, use the following code*/
455 : spdm_message_header_t *spdm_encapsulated_response_ack_response;
456 1 : temp_buf_size = sizeof(spdm_message_header_t);
457 1 : libspdm_zero_mem(temp_buf, LIBSPDM_RECEIVER_BUFFER_SIZE);
458 1 : temp_buf_ptr = temp_buf + sizeof(libspdm_test_message_header_t);
459 1 : spdm_encapsulated_response_ack_response = (void *)temp_buf_ptr;
460 1 : spdm_encapsulated_response_ack_response->spdm_version = SPDM_MESSAGE_VERSION_11;
461 1 : spdm_encapsulated_response_ack_response->request_response_code =
462 : SPDM_ENCAPSULATED_RESPONSE_ACK;
463 1 : spdm_encapsulated_response_ack_response->param1 = 0;
464 1 : spdm_encapsulated_response_ack_response->param2 =
465 : SPDM_ENCAPSULATED_RESPONSE_ACK_RESPONSE_PAYLOAD_TYPE_ABSENT;
466 1 : sub_index = 0;
467 : } else {
468 0 : temp_buf_size = 0;
469 0 : temp_buf_ptr = NULL;
470 : }
471 2 : libspdm_transport_test_encode_message(spdm_context, NULL, false, false,
472 : temp_buf_size, temp_buf_ptr,
473 : response_size, response);
474 : }
475 2 : return LIBSPDM_STATUS_SUCCESS;
476 :
477 2 : case 0x9:
478 : {
479 : spdm_key_update_request_t *spdm_response;
480 :
481 : ((libspdm_context_t *)spdm_context)
482 2 : ->connection_info.algorithm.base_hash_algo =
483 : m_libspdm_use_hash_algo;
484 2 : if (sub_index == 0) {
485 1 : temp_buf_size = sizeof(spdm_key_update_request_t) +
486 : sizeof(spdm_encapsulated_request_response_t);
487 1 : libspdm_zero_mem(temp_buf, LIBSPDM_RECEIVER_BUFFER_SIZE);
488 1 : temp_buf_ptr = temp_buf + sizeof(libspdm_test_message_header_t);
489 1 : libspdm_encapsulated_request_response = (void *)temp_buf_ptr;
490 1 : libspdm_encapsulated_request_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
491 1 : libspdm_encapsulated_request_response->header.request_response_code =
492 : SPDM_ENCAPSULATED_REQUEST;
493 1 : libspdm_encapsulated_request_response->header.param1 = 0;
494 1 : libspdm_encapsulated_request_response->header.param2 = 0;
495 :
496 1 : spdm_response =
497 : (void *)(temp_buf_ptr + sizeof(spdm_encapsulated_request_response_t));
498 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
499 1 : spdm_response->header.request_response_code = SPDM_KEY_UPDATE;
500 1 : spdm_response->header.param1 = SPDM_KEY_UPDATE_OPERATIONS_TABLE_UPDATE_KEY;
501 1 : spdm_response->header.param2 = 0x3;
502 :
503 1 : sub_index++;
504 1 : } else if (sub_index == 1) {
505 : /*When the version is SPDM_MESSAGE_VERSION_12, use the following code*/
506 : spdm_message_header_t *spdm_encapsulated_response_ack_response;
507 1 : temp_buf_size = sizeof(spdm_message_header_t);
508 1 : libspdm_zero_mem(temp_buf, LIBSPDM_RECEIVER_BUFFER_SIZE);
509 1 : temp_buf_ptr = temp_buf + sizeof(libspdm_test_message_header_t);
510 1 : spdm_encapsulated_response_ack_response = (void *)temp_buf_ptr;
511 1 : spdm_encapsulated_response_ack_response->spdm_version = SPDM_MESSAGE_VERSION_11;
512 1 : spdm_encapsulated_response_ack_response->request_response_code =
513 : SPDM_ENCAPSULATED_RESPONSE_ACK;
514 1 : spdm_encapsulated_response_ack_response->param1 = 0;
515 1 : spdm_encapsulated_response_ack_response->param2 =
516 : SPDM_ENCAPSULATED_RESPONSE_ACK_RESPONSE_PAYLOAD_TYPE_ABSENT;
517 1 : sub_index = 0;
518 : } else {
519 0 : temp_buf_size = 0;
520 0 : temp_buf_ptr = NULL;
521 : }
522 2 : libspdm_transport_test_encode_message(spdm_context, NULL, false, false,
523 : temp_buf_size, temp_buf_ptr,
524 : response_size, response);
525 : }
526 2 : return LIBSPDM_STATUS_SUCCESS;
527 :
528 2 : case 0xA:
529 : {
530 : spdm_get_encapsulated_request_request_t *spdm_response;
531 :
532 2 : if (sub_index == 0) {
533 1 : temp_buf_size = sizeof(spdm_encapsulated_request_response_t) +
534 : sizeof(spdm_get_encapsulated_request_request_t);
535 1 : libspdm_zero_mem(temp_buf, LIBSPDM_RECEIVER_BUFFER_SIZE);
536 1 : temp_buf_ptr = temp_buf + sizeof(libspdm_test_message_header_t);
537 :
538 : /* The following is ENCAPSULATED_REQUEST response message */
539 1 : libspdm_encapsulated_request_response = (void *) temp_buf_ptr;
540 1 : libspdm_encapsulated_request_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
541 1 : libspdm_encapsulated_request_response->header.request_response_code =
542 : SPDM_ENCAPSULATED_REQUEST;
543 1 : libspdm_encapsulated_request_response->header.param1 = 0;
544 1 : libspdm_encapsulated_request_response->header.param2 = 0;
545 :
546 : /* The following is EncapsulatedRequest Field of the above ENCAPSULATED_REQUEST response message*/
547 1 : spdm_response =
548 : (void *)(temp_buf_ptr + sizeof(spdm_get_encapsulated_request_request_t));
549 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
550 1 : spdm_response->header.request_response_code = SPDM_GET_ENCAPSULATED_REQUEST; /* Here invalid: GET_ENCAPSULATED_REQUEST is encapsulated */
551 1 : spdm_response->header.param1 = 0;
552 1 : spdm_response->header.param2 = 0;
553 :
554 1 : sub_index++;
555 1 : } else if (sub_index == 1) {
556 : spdm_message_header_t *spdm_encapsulated_response_ack_response;
557 1 : temp_buf_size = sizeof(spdm_message_header_t);
558 1 : libspdm_zero_mem(temp_buf, LIBSPDM_RECEIVER_BUFFER_SIZE);
559 1 : temp_buf_ptr = temp_buf + sizeof(libspdm_test_message_header_t);
560 1 : spdm_encapsulated_response_ack_response = (void *)temp_buf_ptr;
561 1 : spdm_encapsulated_response_ack_response->spdm_version = SPDM_MESSAGE_VERSION_11;
562 1 : spdm_encapsulated_response_ack_response->request_response_code =
563 : SPDM_ENCAPSULATED_RESPONSE_ACK;
564 1 : spdm_encapsulated_response_ack_response->param1 = 0;
565 1 : spdm_encapsulated_response_ack_response->param2 =
566 : SPDM_ENCAPSULATED_RESPONSE_ACK_RESPONSE_PAYLOAD_TYPE_ABSENT;
567 1 : sub_index = 0;
568 : } else {
569 0 : temp_buf_size = 0;
570 0 : temp_buf_ptr = NULL;
571 : }
572 2 : libspdm_transport_test_encode_message(spdm_context, NULL, false, false,
573 : temp_buf_size, temp_buf_ptr,
574 : response_size, response);
575 : }
576 2 : return LIBSPDM_STATUS_SUCCESS;
577 :
578 1 : case 0xB:
579 : {
580 : spdm_error_response_t *spdm_response;
581 :
582 1 : temp_buf_size = sizeof(spdm_error_response_t);
583 1 : libspdm_zero_mem(temp_buf, LIBSPDM_RECEIVER_BUFFER_SIZE);
584 1 : temp_buf_ptr = temp_buf + sizeof(libspdm_test_message_header_t);
585 1 : spdm_response = (void *) temp_buf_ptr;
586 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
587 1 : spdm_response->header.request_response_code = SPDM_ERROR;
588 1 : spdm_response->header.param1 = SPDM_ERROR_CODE_UNEXPECTED_REQUEST;
589 1 : spdm_response->header.param2 = 0;
590 :
591 1 : libspdm_transport_test_encode_message(spdm_context, NULL, false, false,
592 : temp_buf_size, temp_buf_ptr,
593 : response_size, response);
594 : }
595 1 : return LIBSPDM_STATUS_SUCCESS;
596 :
597 1 : case 0xC:
598 : {
599 : spdm_error_response_t *spdm_response;
600 :
601 1 : temp_buf_size = sizeof(spdm_error_response_t);
602 1 : libspdm_zero_mem(temp_buf, LIBSPDM_RECEIVER_BUFFER_SIZE);
603 1 : temp_buf_ptr = temp_buf + sizeof(libspdm_test_message_header_t);
604 1 : spdm_response = (void *) temp_buf_ptr;
605 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_13;
606 1 : spdm_response->header.request_response_code = SPDM_ERROR;
607 1 : spdm_response->header.param1 = SPDM_ERROR_CODE_NO_PENDING_REQUESTS;
608 1 : spdm_response->header.param2 = 0;
609 :
610 1 : libspdm_transport_test_encode_message(spdm_context, NULL, false, false,
611 : temp_buf_size, temp_buf_ptr,
612 : response_size, response);
613 : }
614 1 : return LIBSPDM_STATUS_SUCCESS;
615 :
616 2 : case 0xD:
617 : {
618 : spdm_get_endpoint_info_request_t *spdm_response;
619 : uint8_t *spdm_nonce;
620 :
621 : ((libspdm_context_t *)spdm_context)
622 2 : ->connection_info.algorithm.base_hash_algo =
623 : m_libspdm_use_hash_algo;
624 2 : if (sub_index == 0) {
625 1 : temp_buf_size = sizeof(spdm_encapsulated_request_response_t) +
626 : sizeof(spdm_get_endpoint_info_request_t) + SPDM_NONCE_SIZE;
627 1 : libspdm_zero_mem(temp_buf, LIBSPDM_RECEIVER_BUFFER_SIZE);
628 1 : temp_buf_ptr = temp_buf + sizeof(libspdm_test_message_header_t);
629 1 : libspdm_encapsulated_request_response = (void *)temp_buf_ptr;
630 1 : libspdm_encapsulated_request_response->header.spdm_version = SPDM_MESSAGE_VERSION_13;
631 1 : libspdm_encapsulated_request_response->header.request_response_code =
632 : SPDM_ENCAPSULATED_REQUEST;
633 1 : libspdm_encapsulated_request_response->header.param1 = 0;
634 1 : libspdm_encapsulated_request_response->header.param2 = 0;
635 :
636 1 : spdm_response =
637 : (void *)(temp_buf_ptr + sizeof(spdm_encapsulated_request_response_t));
638 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_13;
639 1 : spdm_response->header.request_response_code = SPDM_GET_ENDPOINT_INFO;
640 1 : spdm_response->header.param1 =
641 : SPDM_GET_ENDPOINT_INFO_REQUEST_SUBCODE_DEVICE_CLASS_IDENTIFIER;
642 1 : spdm_response->header.param2 = 0; /* slot_id */
643 1 : spdm_response->request_attributes =
644 : SPDM_GET_ENDPOINT_INFO_REQUEST_ATTRIBUTE_SIGNATURE_REQUESTED;
645 1 : libspdm_write_uint24(spdm_response->reserved, 0);
646 1 : spdm_nonce = (uint8_t *)(spdm_response + 1);
647 1 : libspdm_get_random_number(SPDM_NONCE_SIZE, spdm_nonce);
648 :
649 1 : sub_index++;
650 1 : } else if (sub_index == 1) {
651 : spdm_encapsulated_response_ack_response_t *spdm_encapsulated_response_ack_response;
652 1 : temp_buf_size = sizeof(spdm_encapsulated_response_ack_response_t);
653 1 : libspdm_zero_mem(temp_buf, LIBSPDM_RECEIVER_BUFFER_SIZE);
654 1 : temp_buf_ptr = temp_buf + sizeof(libspdm_test_message_header_t);
655 1 : spdm_encapsulated_response_ack_response = (void *)temp_buf_ptr;
656 1 : spdm_encapsulated_response_ack_response->header.spdm_version = SPDM_MESSAGE_VERSION_13;
657 1 : spdm_encapsulated_response_ack_response->header.request_response_code =
658 : SPDM_ENCAPSULATED_RESPONSE_ACK;
659 1 : spdm_encapsulated_response_ack_response->header.param1 = 0;
660 1 : spdm_encapsulated_response_ack_response->header.param2 =
661 : SPDM_ENCAPSULATED_RESPONSE_ACK_RESPONSE_PAYLOAD_TYPE_ABSENT;
662 1 : spdm_encapsulated_response_ack_response->ack_request_id = 0;
663 1 : libspdm_write_uint24(spdm_encapsulated_response_ack_response->reserved, 0);
664 :
665 1 : sub_index = 0;
666 : } else {
667 0 : temp_buf_size = 0;
668 0 : temp_buf_ptr = NULL;
669 : }
670 2 : libspdm_transport_test_encode_message(spdm_context, NULL, false, false,
671 : temp_buf_size, temp_buf_ptr,
672 : response_size, response);
673 : }
674 2 : return LIBSPDM_STATUS_SUCCESS;
675 1 : case 0xE:
676 : {
677 : spdm_error_response_t *spdm_response;
678 :
679 1 : temp_buf_size = sizeof(spdm_error_response_t);
680 1 : libspdm_zero_mem(temp_buf, LIBSPDM_RECEIVER_BUFFER_SIZE);
681 1 : temp_buf_ptr = temp_buf + sizeof(libspdm_test_message_header_t);
682 1 : spdm_response = (void *) temp_buf_ptr;
683 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_10;
684 1 : spdm_response->header.request_response_code = SPDM_ERROR;
685 1 : spdm_response->header.param1 = SPDM_ERROR_CODE_REQUEST_RESYNCH;
686 1 : spdm_response->header.param2 = 0;
687 1 : libspdm_transport_test_encode_message(spdm_context, NULL, false, false,
688 : temp_buf_size, temp_buf_ptr,
689 : response_size, response);
690 : }
691 1 : return LIBSPDM_STATUS_SUCCESS;
692 2 : case 0xF:
693 : {
694 : spdm_digest_response_t *spdm_response;
695 :
696 : ((libspdm_context_t *)spdm_context)
697 2 : ->connection_info.algorithm.base_hash_algo =
698 : m_libspdm_use_hash_algo;
699 2 : if (sub_index == 0) {
700 1 : temp_buf_size = sizeof(spdm_digest_response_t) +
701 1 : libspdm_get_hash_size(m_libspdm_use_hash_algo) +
702 : sizeof(spdm_encapsulated_request_response_t);
703 1 : libspdm_zero_mem(temp_buf, LIBSPDM_RECEIVER_BUFFER_SIZE);
704 1 : temp_buf_ptr = temp_buf + sizeof(libspdm_test_message_header_t);
705 1 : libspdm_encapsulated_request_response = (void*) temp_buf_ptr;
706 1 : libspdm_encapsulated_request_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
707 1 : libspdm_encapsulated_request_response->header.request_response_code =
708 : SPDM_ENCAPSULATED_REQUEST;
709 1 : libspdm_encapsulated_request_response->header.param1 = 0;
710 1 : libspdm_encapsulated_request_response->header.param2 = 0;
711 :
712 1 : spdm_response = (void *)(temp_buf_ptr
713 : + sizeof(spdm_encapsulated_request_response_t));
714 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
715 1 : spdm_response->header.request_response_code = SPDM_GET_DIGESTS;
716 1 : spdm_response->header.param1 = 0;
717 1 : spdm_response->header.param2 = 0;
718 1 : libspdm_set_mem(m_libspdm_local_certificate_chain,
719 : sizeof(m_libspdm_local_certificate_chain),
720 : (uint8_t)(0xFF));
721 :
722 1 : digest = (void *)(spdm_response + 1);
723 1 : libspdm_hash_all(m_libspdm_use_hash_algo, m_libspdm_local_certificate_chain,
724 : sizeof(m_libspdm_local_certificate_chain), &digest[0]);
725 1 : spdm_response->header.param2 |= (0x01 << 0);
726 1 : sub_index++;
727 1 : } else if (sub_index == 1) {
728 1 : temp_buf_size = sizeof(spdm_error_response_t);
729 1 : libspdm_zero_mem(temp_buf, LIBSPDM_RECEIVER_BUFFER_SIZE);
730 1 : temp_buf_ptr = temp_buf + sizeof(libspdm_test_message_header_t);
731 1 : spdm_response = (void *) temp_buf_ptr;
732 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_10;
733 1 : spdm_response->header.request_response_code = SPDM_ERROR;
734 1 : spdm_response->header.param1 = SPDM_ERROR_CODE_REQUEST_RESYNCH;
735 1 : spdm_response->header.param2 = 0;
736 1 : sub_index = 0;
737 : } else {
738 0 : temp_buf_size = 0;
739 0 : temp_buf_ptr = NULL;
740 : }
741 2 : libspdm_transport_test_encode_message(spdm_context, NULL, false, false,
742 : temp_buf_size, temp_buf_ptr,
743 : response_size, response);
744 : }
745 2 : return LIBSPDM_STATUS_SUCCESS;
746 0 : default:
747 0 : return LIBSPDM_STATUS_RECEIVE_FAIL;
748 : }
749 : }
750 :
751 : #if (LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP) && (LIBSPDM_ENABLE_CAPABILITY_CERT_CAP)
752 1 : static void req_get_encapsulated_request_case1(void **State)
753 : {
754 : libspdm_return_t status;
755 : libspdm_test_context_t *spdm_test_context;
756 : libspdm_context_t *spdm_context;
757 : void *data;
758 : size_t data_size;
759 : void *hash;
760 : size_t hash_size;
761 :
762 1 : spdm_test_context = *State;
763 1 : spdm_context = spdm_test_context->spdm_context;
764 1 : spdm_test_context->case_id = 0x1;
765 :
766 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
767 : SPDM_VERSION_NUMBER_SHIFT_BIT;
768 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
769 1 : spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PSK_CAP;
770 1 : spdm_context->connection_info.capability.flags |=
771 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
772 1 : spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
773 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PSK_CAP;
774 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
775 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
776 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCAP_CAP;
777 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_CERT_CAP;
778 :
779 1 : spdm_context->connection_info.capability.flags |=
780 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCAP_CAP;
781 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
782 : m_libspdm_use_asym_algo, &data,
783 : &data_size,
784 : &hash, &hash_size)) {
785 0 : assert(false);
786 : }
787 1 : libspdm_reset_message_a(spdm_context);
788 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
789 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
790 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
791 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
792 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
793 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
794 : data_size;
795 : libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
796 : sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
797 : data, data_size);
798 : #endif
799 :
800 1 : libspdm_register_get_encap_response_func(spdm_context, libspdm_get_encap_response_digest);
801 1 : status = libspdm_encapsulated_request(spdm_context, NULL, 0, NULL);
802 1 : assert_int_equal(status, LIBSPDM_STATUS_SEND_FAIL);
803 1 : free(data);
804 1 : }
805 :
806 1 : static void req_get_encapsulated_request_case2(void **State)
807 : {
808 : libspdm_return_t status;
809 : libspdm_test_context_t *spdm_test_context;
810 : libspdm_context_t *spdm_context;
811 : void *data;
812 : size_t data_size;
813 : void *hash;
814 : size_t hash_size;
815 :
816 1 : spdm_test_context = *State;
817 1 : spdm_context = spdm_test_context->spdm_context;
818 1 : spdm_test_context->case_id = 0x2;
819 :
820 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
821 : SPDM_VERSION_NUMBER_SHIFT_BIT;
822 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
823 1 : spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PSK_CAP;
824 1 : spdm_context->connection_info.capability.flags |=
825 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
826 1 : spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
827 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PSK_CAP;
828 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
829 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
830 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCAP_CAP;
831 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_CERT_CAP;
832 1 : spdm_context->connection_info.capability.flags |=
833 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCAP_CAP;
834 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
835 : m_libspdm_use_asym_algo, &data,
836 : &data_size,
837 : &hash, &hash_size)) {
838 0 : assert(false);
839 : }
840 1 : libspdm_reset_message_a(spdm_context);
841 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
842 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
843 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
844 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
845 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
846 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
847 : data_size;
848 : libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
849 : sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
850 : data, data_size);
851 : #endif
852 :
853 1 : libspdm_register_get_encap_response_func(spdm_context, libspdm_get_encap_response_digest);
854 1 : status = libspdm_encapsulated_request(spdm_context, NULL, 0, NULL);
855 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
856 1 : free(data);
857 1 : }
858 :
859 1 : static void req_get_encapsulated_request_case3(void **State)
860 : {
861 1 : }
862 :
863 1 : static void req_get_encapsulated_request_case4(void **State)
864 : {
865 : libspdm_return_t status;
866 : libspdm_test_context_t *spdm_test_context;
867 : libspdm_context_t *spdm_context;
868 : void *data;
869 : size_t data_size;
870 : void *hash;
871 : size_t hash_size;
872 :
873 1 : spdm_test_context = *State;
874 1 : spdm_context = spdm_test_context->spdm_context;
875 1 : spdm_test_context->case_id = 0x4;
876 :
877 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
878 : SPDM_VERSION_NUMBER_SHIFT_BIT;
879 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
880 1 : spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PSK_CAP;
881 1 : spdm_context->connection_info.capability.flags |=
882 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
883 1 : spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
884 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PSK_CAP;
885 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
886 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
887 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCAP_CAP;
888 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_CERT_CAP;
889 1 : spdm_context->connection_info.capability.flags |=
890 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCAP_CAP;
891 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
892 : m_libspdm_use_asym_algo, &data,
893 : &data_size,
894 : &hash, &hash_size)) {
895 0 : assert(false);
896 : }
897 1 : libspdm_reset_message_a(spdm_context);
898 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
899 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
900 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
901 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
902 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
903 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
904 : data_size;
905 : libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
906 : sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
907 : data, data_size);
908 : #endif
909 :
910 1 : libspdm_register_get_encap_response_func(spdm_context, libspdm_get_encap_response_digest);
911 1 : status = libspdm_encapsulated_request(spdm_context, NULL, 0, NULL);
912 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_SIZE);
913 1 : free(data);
914 1 : }
915 :
916 1 : static void req_get_encapsulated_request_case5(void **State)
917 : {
918 : libspdm_return_t status;
919 : libspdm_test_context_t *spdm_test_context;
920 : libspdm_context_t *spdm_context;
921 : void *data;
922 : size_t data_size;
923 : void *hash;
924 : size_t hash_size;
925 :
926 1 : spdm_test_context = *State;
927 1 : spdm_context = spdm_test_context->spdm_context;
928 1 : spdm_test_context->case_id = 0x5;
929 :
930 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
931 : SPDM_VERSION_NUMBER_SHIFT_BIT;
932 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
933 1 : spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PSK_CAP;
934 1 : spdm_context->connection_info.capability.flags |=
935 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
936 1 : spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
937 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PSK_CAP;
938 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
939 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
940 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCAP_CAP;
941 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_CERT_CAP;
942 1 : spdm_context->connection_info.capability.flags |=
943 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCAP_CAP;
944 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
945 : m_libspdm_use_asym_algo, &data,
946 : &data_size,
947 : &hash, &hash_size)) {
948 0 : assert(false);
949 : }
950 1 : libspdm_reset_message_a(spdm_context);
951 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
952 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
953 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
954 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
955 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
956 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
957 : data_size;
958 : libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
959 : sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
960 : data, data_size);
961 : #endif
962 :
963 1 : libspdm_register_get_encap_response_func(spdm_context, libspdm_get_encap_response_digest);
964 1 : status = libspdm_encapsulated_request(spdm_context, NULL, 0, NULL);
965 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
966 1 : free(data);
967 1 : }
968 :
969 1 : static void req_get_encapsulated_request_case6(void **State)
970 : {
971 1 : }
972 :
973 1 : static void req_get_encapsulated_request_case7(void **State)
974 : {
975 : libspdm_return_t status;
976 : libspdm_test_context_t *spdm_test_context;
977 : libspdm_context_t *spdm_context;
978 : void *data;
979 : size_t data_size;
980 : void *hash;
981 : size_t hash_size;
982 :
983 1 : spdm_test_context = *State;
984 1 : spdm_context = spdm_test_context->spdm_context;
985 1 : spdm_test_context->case_id = 0x7;
986 :
987 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
988 : SPDM_VERSION_NUMBER_SHIFT_BIT;
989 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
990 1 : spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PSK_CAP;
991 1 : spdm_context->connection_info.capability.flags |=
992 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
993 1 : spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
994 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PSK_CAP;
995 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
996 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
997 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCAP_CAP;
998 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_CERT_CAP;
999 1 : spdm_context->connection_info.capability.flags |=
1000 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCAP_CAP;
1001 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
1002 : m_libspdm_use_asym_algo, &data,
1003 : &data_size,
1004 : &hash, &hash_size)) {
1005 0 : assert(false);
1006 : }
1007 1 : libspdm_reset_message_a(spdm_context);
1008 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
1009 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
1010 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
1011 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
1012 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1013 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
1014 : data_size;
1015 : libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
1016 : sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
1017 : data, data_size);
1018 : #endif
1019 :
1020 1 : libspdm_register_get_encap_response_func(spdm_context, libspdm_get_encap_response_digest);
1021 1 : status = libspdm_encapsulated_request(spdm_context, NULL, 0, NULL);
1022 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
1023 1 : free(data);
1024 1 : }
1025 :
1026 1 : static void req_get_encapsulated_request_case8(void **State)
1027 : {
1028 : libspdm_return_t status;
1029 : libspdm_test_context_t *spdm_test_context;
1030 : libspdm_context_t *spdm_context;
1031 : void *data;
1032 : size_t data_size;
1033 : void *hash;
1034 : size_t hash_size;
1035 :
1036 1 : spdm_test_context = *State;
1037 1 : spdm_context = spdm_test_context->spdm_context;
1038 1 : spdm_test_context->case_id = 0x8;
1039 :
1040 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
1041 : SPDM_VERSION_NUMBER_SHIFT_BIT;
1042 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
1043 1 : spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PSK_CAP;
1044 1 : spdm_context->connection_info.capability.flags |=
1045 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
1046 1 : spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
1047 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PSK_CAP;
1048 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
1049 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
1050 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCAP_CAP;
1051 1 : spdm_context->local_context.capability.flags |=
1052 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP;
1053 1 : spdm_context->connection_info.capability.flags |=
1054 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCAP_CAP;
1055 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
1056 : m_libspdm_use_asym_algo, &data,
1057 : &data_size,
1058 : &hash, &hash_size)) {
1059 0 : assert(false);
1060 : }
1061 1 : spdm_context->local_context.local_cert_chain_provision[0] = data;
1062 1 : spdm_context->local_context.local_cert_chain_provision_size[0] = data_size;
1063 :
1064 1 : libspdm_reset_message_a(spdm_context);
1065 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
1066 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
1067 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
1068 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
1069 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1070 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
1071 : data_size;
1072 : libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
1073 : sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
1074 : data, data_size);
1075 : #endif
1076 :
1077 1 : libspdm_register_get_encap_response_func(spdm_context, libspdm_get_encap_response_certificate);
1078 1 : status = libspdm_encapsulated_request(spdm_context, NULL, 0, NULL);
1079 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
1080 1 : free(data);
1081 1 : }
1082 : #endif /* (LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP) && (LIBSPDM_ENABLE_CAPABILITY_CERT_CAP) */
1083 :
1084 : #if ((LIBSPDM_ENABLE_CAPABILITY_KEY_EX_CAP) || (LIBSPDM_ENABLE_CAPABILITY_PSK_CAP))
1085 1 : static void req_get_encapsulated_request_case9(void **State)
1086 : {
1087 : libspdm_return_t status;
1088 : libspdm_test_context_t *spdm_test_context;
1089 : libspdm_context_t *spdm_context;
1090 : void *data;
1091 : size_t data_size;
1092 : void *hash;
1093 : size_t hash_size;
1094 :
1095 1 : spdm_test_context = *State;
1096 1 : spdm_context = spdm_test_context->spdm_context;
1097 1 : spdm_test_context->case_id = 0x9;
1098 :
1099 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
1100 : SPDM_VERSION_NUMBER_SHIFT_BIT;
1101 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
1102 1 : spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PSK_CAP;
1103 1 : spdm_context->connection_info.capability.flags |=
1104 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
1105 1 : spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
1106 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PSK_CAP;
1107 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
1108 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
1109 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCAP_CAP;
1110 1 : spdm_context->connection_info.capability.flags |=
1111 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_UPD_CAP;
1112 1 : spdm_context->local_context.capability.flags |=
1113 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_UPD_CAP;
1114 1 : spdm_context->connection_info.capability.flags |=
1115 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCAP_CAP;
1116 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
1117 : m_libspdm_use_asym_algo, &data,
1118 : &data_size,
1119 : &hash, &hash_size)) {
1120 0 : assert(false);
1121 : }
1122 1 : spdm_context->local_context.local_cert_chain_provision[0] = data;
1123 1 : spdm_context->local_context.local_cert_chain_provision_size[0] = data_size;
1124 :
1125 1 : libspdm_reset_message_a(spdm_context);
1126 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
1127 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
1128 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
1129 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
1130 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1131 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
1132 : data_size;
1133 : libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
1134 : sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
1135 : data, data_size);
1136 : #endif
1137 :
1138 1 : libspdm_register_get_encap_response_func(spdm_context, libspdm_get_encap_response_key_update);
1139 1 : status = libspdm_encapsulated_request(spdm_context, NULL, 0, NULL);
1140 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
1141 1 : free(data);
1142 1 : }
1143 : #endif /* ((LIBSPDM_ENABLE_CAPABILITY_KEY_EX_CAP) || (LIBSPDM_ENABLE_CAPABILITY_PSK_CAP)) */
1144 :
1145 : /**
1146 : * Test 10: GET_ENCAPSULATED_REQUEST request message is encapsulated in ENCAPSULATED_REQUEST response message.
1147 : * Expected Behavior: the Requester should respond with ErrorCode=UnexpectedRequest.
1148 : **/
1149 1 : static void req_get_encapsulated_request_case10(void **State)
1150 : {
1151 : libspdm_return_t status;
1152 : libspdm_test_context_t *spdm_test_context;
1153 : libspdm_context_t *spdm_context;
1154 :
1155 1 : spdm_test_context = *State;
1156 1 : spdm_context = spdm_test_context->spdm_context;
1157 1 : spdm_test_context->case_id = 0xA;
1158 :
1159 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
1160 : SPDM_VERSION_NUMBER_SHIFT_BIT;
1161 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
1162 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCAP_CAP;
1163 1 : spdm_context->connection_info.capability.flags |=
1164 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCAP_CAP;
1165 :
1166 1 : libspdm_register_get_encap_response_func(spdm_context, NULL);
1167 1 : status = libspdm_encapsulated_request(spdm_context, NULL, 0, NULL);
1168 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
1169 1 : }
1170 :
1171 : /**
1172 : * Test 11: In a non-mutual-authentication context, Requester sends GET_ENCAPSULATED_REQUEST and
1173 : * Responder replies with UnexpectedRequest.
1174 : * Expected Behavior: return LIBSPDM_STATUS_SUCCESS as Responder did not have a pending request for
1175 : * the Requester.
1176 : **/
1177 1 : static void req_get_encapsulated_request_case11(void **State)
1178 : {
1179 : libspdm_return_t status;
1180 : libspdm_test_context_t *spdm_test_context;
1181 : libspdm_context_t *spdm_context;
1182 :
1183 1 : spdm_test_context = *State;
1184 1 : spdm_context = spdm_test_context->spdm_context;
1185 1 : spdm_test_context->case_id = 0xB;
1186 :
1187 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
1188 : SPDM_VERSION_NUMBER_SHIFT_BIT;
1189 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
1190 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCAP_CAP;
1191 1 : spdm_context->connection_info.capability.flags |=
1192 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCAP_CAP;
1193 :
1194 1 : libspdm_register_get_encap_response_func(spdm_context, NULL);
1195 1 : status = libspdm_encapsulated_request(spdm_context, NULL, 0, NULL);
1196 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
1197 1 : }
1198 :
1199 : /**
1200 : * Test 12: In a non-mutual-authentication context, Requester sends GET_ENCAPSULATED_REQUEST and
1201 : * Responder replies with NoPendingRequests.
1202 : * Expected Behavior: return LIBSPDM_STATUS_SUCCESS as Responder did not have a pending request for
1203 : * the Requester.
1204 : **/
1205 1 : static void req_get_encapsulated_request_case12(void **State)
1206 : {
1207 : libspdm_return_t status;
1208 : libspdm_test_context_t *spdm_test_context;
1209 : libspdm_context_t *spdm_context;
1210 :
1211 1 : spdm_test_context = *State;
1212 1 : spdm_context = spdm_test_context->spdm_context;
1213 1 : spdm_test_context->case_id = 0xC;
1214 :
1215 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 <<
1216 : SPDM_VERSION_NUMBER_SHIFT_BIT;
1217 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
1218 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCAP_CAP;
1219 1 : spdm_context->connection_info.capability.flags |=
1220 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCAP_CAP;
1221 :
1222 1 : libspdm_register_get_encap_response_func(spdm_context, NULL);
1223 1 : status = libspdm_encapsulated_request(spdm_context, NULL, 0, NULL);
1224 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
1225 1 : }
1226 :
1227 : #if LIBSPDM_ENABLE_CAPABILITY_ENDPOINT_INFO_CAP
1228 1 : static void req_get_encapsulated_request_case13(void **State)
1229 : {
1230 : libspdm_return_t status;
1231 : libspdm_test_context_t *spdm_test_context;
1232 : libspdm_context_t *spdm_context;
1233 : void *data;
1234 : size_t data_size;
1235 : void *hash;
1236 : size_t hash_size;
1237 :
1238 1 : spdm_test_context = *State;
1239 1 : spdm_context = spdm_test_context->spdm_context;
1240 1 : spdm_test_context->case_id = 0xD;
1241 :
1242 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 <<
1243 : SPDM_VERSION_NUMBER_SHIFT_BIT;
1244 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
1245 1 : spdm_context->connection_info.capability.flags = 0;
1246 1 : spdm_context->local_context.capability.flags = 0;
1247 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCAP_CAP;
1248 1 : spdm_context->connection_info.capability.flags |=
1249 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCAP_CAP;
1250 1 : spdm_context->local_context.capability.flags |=
1251 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_EP_INFO_CAP_SIG;
1252 1 : libspdm_read_requester_public_certificate_chain(m_libspdm_use_hash_algo,
1253 : m_libspdm_use_req_asym_algo, &data,
1254 : &data_size,
1255 : &hash, &hash_size);
1256 1 : spdm_context->local_context.local_cert_chain_provision[0] = data;
1257 1 : spdm_context->local_context.local_cert_chain_provision_size[0] = data_size;
1258 :
1259 1 : libspdm_reset_message_a(spdm_context);
1260 1 : libspdm_reset_message_encap_e(spdm_context, NULL);
1261 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
1262 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
1263 1 : spdm_context->connection_info.algorithm.req_base_asym_alg = m_libspdm_use_req_asym_algo;
1264 :
1265 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1266 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
1267 : data_size;
1268 : libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
1269 : sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
1270 : data, data_size);
1271 : #endif
1272 :
1273 1 : libspdm_register_get_encap_response_func(spdm_context,
1274 : libspdm_get_encap_response_endpoint_info);
1275 1 : status = libspdm_encapsulated_request(spdm_context, NULL, 0, NULL);
1276 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
1277 1 : free(data);
1278 1 : }
1279 : #endif /* LIBSPDM_ENABLE_CAPABILITY_ENDPOINT_INFO_CAP */
1280 :
1281 1 : static void req_get_encapsulated_request_case14(void **State)
1282 : {
1283 : libspdm_return_t status;
1284 : libspdm_test_context_t *spdm_test_context;
1285 : libspdm_context_t *spdm_context;
1286 : void *data;
1287 : size_t data_size;
1288 : void *hash;
1289 : size_t hash_size;
1290 :
1291 1 : spdm_test_context = *State;
1292 1 : spdm_context = spdm_test_context->spdm_context;
1293 1 : spdm_test_context->case_id = 0xE;
1294 :
1295 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
1296 : SPDM_VERSION_NUMBER_SHIFT_BIT;
1297 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
1298 1 : spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PSK_CAP;
1299 1 : spdm_context->connection_info.capability.flags |=
1300 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
1301 1 : spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
1302 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PSK_CAP;
1303 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
1304 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
1305 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCAP_CAP;
1306 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_CERT_CAP;
1307 1 : spdm_context->connection_info.capability.flags |=
1308 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCAP_CAP;
1309 1 : spdm_context->local_context.local_cert_chain_provision[0] = NULL;
1310 1 : spdm_context->local_context.local_cert_chain_provision_size[0] = 0;
1311 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
1312 : m_libspdm_use_asym_algo, &data,
1313 : &data_size,
1314 : &hash, &hash_size)) {
1315 0 : assert(false);
1316 : }
1317 1 : libspdm_reset_message_a(spdm_context);
1318 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
1319 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
1320 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
1321 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
1322 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1323 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
1324 : data_size;
1325 : libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
1326 : sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
1327 : data, data_size);
1328 : #endif
1329 :
1330 1 : libspdm_register_get_encap_response_func(spdm_context, libspdm_get_encap_response_digest);
1331 1 : status = libspdm_encapsulated_request(spdm_context, NULL, 0, NULL);
1332 1 : assert_int_equal(status, LIBSPDM_STATUS_RESYNCH_PEER);
1333 1 : assert_int_equal(spdm_context->connection_info.connection_state,
1334 : LIBSPDM_CONNECTION_STATE_NOT_STARTED);
1335 1 : free(data);
1336 1 : }
1337 :
1338 1 : static void req_get_encapsulated_request_case15(void **State)
1339 : {
1340 : libspdm_return_t status;
1341 : libspdm_test_context_t *spdm_test_context;
1342 : libspdm_context_t *spdm_context;
1343 : void *data;
1344 : size_t data_size;
1345 : void *hash;
1346 : size_t hash_size;
1347 :
1348 1 : spdm_test_context = *State;
1349 1 : spdm_context = spdm_test_context->spdm_context;
1350 1 : spdm_test_context->case_id = 0xF;
1351 :
1352 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
1353 : SPDM_VERSION_NUMBER_SHIFT_BIT;
1354 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
1355 1 : spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PSK_CAP;
1356 1 : spdm_context->connection_info.capability.flags |=
1357 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
1358 1 : spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
1359 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PSK_CAP;
1360 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
1361 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
1362 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCAP_CAP;
1363 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_CERT_CAP;
1364 1 : spdm_context->connection_info.capability.flags |=
1365 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCAP_CAP;
1366 1 : spdm_context->local_context.local_cert_chain_provision[0] = NULL;
1367 1 : spdm_context->local_context.local_cert_chain_provision_size[0] = 0;
1368 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
1369 : m_libspdm_use_asym_algo, &data,
1370 : &data_size,
1371 : &hash, &hash_size)) {
1372 0 : assert(false);
1373 : }
1374 1 : libspdm_reset_message_a(spdm_context);
1375 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
1376 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
1377 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
1378 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
1379 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1380 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
1381 : data_size;
1382 : libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
1383 : sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
1384 : data, data_size);
1385 : #endif
1386 :
1387 1 : libspdm_register_get_encap_response_func(spdm_context, libspdm_get_encap_response_digest);
1388 1 : status = libspdm_encapsulated_request(spdm_context, NULL, 0, NULL);
1389 1 : assert_int_equal(status, LIBSPDM_STATUS_RESYNCH_PEER);
1390 1 : assert_int_equal(spdm_context->connection_info.connection_state,
1391 : LIBSPDM_CONNECTION_STATE_NOT_STARTED);
1392 1 : free(data);
1393 1 : }
1394 :
1395 1 : int libspdm_req_get_encapsulated_request_test(void)
1396 : {
1397 1 : const struct CMUnitTest test_cases[] = {
1398 : /* SendRequest failed*/
1399 : #if (LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP) && (LIBSPDM_ENABLE_CAPABILITY_CERT_CAP)
1400 : cmocka_unit_test(req_get_encapsulated_request_case1),
1401 : /* Success Case ,func :libspdm_get_encap_response_digest*/
1402 : cmocka_unit_test(req_get_encapsulated_request_case2),
1403 : cmocka_unit_test(req_get_encapsulated_request_case3),
1404 : /* Error response:Receive message only SPDM ENCAPSULATED_REQUEST response*/
1405 : cmocka_unit_test(req_get_encapsulated_request_case4),
1406 : /* Error response: spdm_encapsulated_response_ack_response == NULL*/
1407 : cmocka_unit_test(req_get_encapsulated_request_case5),
1408 : cmocka_unit_test(req_get_encapsulated_request_case6),
1409 : /* response: param2 == SPDM_ENCAPSULATED_RESPONSE_ACK_RESPONSE_PAYLOAD_TYPE_REQ_SLOT_NUMBER*/
1410 : cmocka_unit_test(req_get_encapsulated_request_case7),
1411 : /*Success Case ,func :libspdm_get_encap_response_certificate */
1412 : cmocka_unit_test(req_get_encapsulated_request_case8),
1413 : #endif /* (LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP) && (..) */
1414 :
1415 : /*Success Case ,func :libspdm_get_encap_response_key_update */
1416 : cmocka_unit_test(req_get_encapsulated_request_case9),
1417 : /*Error response: GET_ENCAPSULATED_REQUEST message is encapsulated */
1418 : cmocka_unit_test(req_get_encapsulated_request_case10),
1419 : cmocka_unit_test(req_get_encapsulated_request_case11),
1420 : cmocka_unit_test(req_get_encapsulated_request_case12),
1421 : #if LIBSPDM_ENABLE_CAPABILITY_ENDPOINT_INFO_CAP
1422 : /*Success Case ,func :libspdm_get_encap_response_endpoint_info */
1423 : cmocka_unit_test(req_get_encapsulated_request_case13),
1424 : #endif /* LIBSPDM_ENABLE_CAPABILITY_ENDPOINT_INFO_CAP */
1425 : /* Error response: send SPDM_GET_ENCAPSULATED_REQUEST and receive request resync */
1426 : cmocka_unit_test(req_get_encapsulated_request_case14),
1427 : /* Error response: send SPDM_DELIVER_ENCAPSULATED_RESPONSE and receive request resync */
1428 : cmocka_unit_test(req_get_encapsulated_request_case15),
1429 : };
1430 :
1431 1 : libspdm_test_context_t test_context = {
1432 : LIBSPDM_TEST_CONTEXT_VERSION,
1433 : true,
1434 : send_message,
1435 : receive_message,
1436 : };
1437 :
1438 1 : libspdm_setup_test_context(&test_context);
1439 :
1440 1 : return cmocka_run_group_tests(test_cases,
1441 : libspdm_unit_test_group_setup,
1442 : libspdm_unit_test_group_teardown);
1443 : }
1444 :
1445 : #endif /* LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP */
|