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