Branch data Line data Source code
1 : : /**
2 : : * Copyright Notice:
3 : : * Copyright 2021-2026 DMTF. All rights reserved.
4 : : * License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/libspdm/blob/main/LICENSE.md
5 : : **/
6 : :
7 : : #include "internal/libspdm_requester_lib.h"
8 : :
9 : : #if LIBSPDM_RESPOND_IF_READY_SUPPORT
10 : : /**
11 : : * This function sends RESPOND_IF_READY and receives an expected SPDM response.
12 : : *
13 : : * @param spdm_context A pointer to the SPDM context.
14 : : * @param response_size The size of the response.
15 : : * On input, it means the size in bytes of response data buffer.
16 : : * On output, it means the size in bytes of copied response data
17 : : * buffer if LIBSPDM_STATUS_SUCCESS is returned.
18 : : * @param response The SPDM response message.
19 : : * @param expected_response_code Indicate the expected response code.
20 : : **/
21 : 70 : static libspdm_return_t libspdm_requester_respond_if_ready(libspdm_context_t *spdm_context,
22 : : const uint32_t *session_id,
23 : : size_t *response_size,
24 : : void **response,
25 : : uint8_t expected_response_code)
26 : : {
27 : : libspdm_return_t status;
28 : : spdm_response_if_ready_request_t *spdm_request;
29 : : size_t spdm_request_size;
30 : : spdm_message_header_t *spdm_response;
31 : : uint8_t *message;
32 : : size_t message_size;
33 : : size_t transport_header_size;
34 : :
35 : : /* the response might be in response buffer in normal SPDM message
36 : : * or it is in scratch buffer in case of secure SPDM message
37 : : * the response buffer is in acquired state, so release it*/
38 : 70 : libspdm_release_receiver_buffer (spdm_context);
39 : :
40 : : /* now acquire the sender buffer */
41 : 70 : transport_header_size = spdm_context->local_context.capability.transport_header_size;
42 : 70 : status = libspdm_acquire_sender_buffer (spdm_context, &message_size, (void **)&message);
43 [ - + ]: 70 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
44 : 0 : return status;
45 : : }
46 : 70 : LIBSPDM_ASSERT (message_size >= transport_header_size +
47 : : spdm_context->local_context.capability.transport_tail_size);
48 : 70 : spdm_request = (void *)(message + transport_header_size);
49 : 70 : spdm_request_size = message_size - transport_header_size -
50 : 70 : spdm_context->local_context.capability.transport_tail_size;
51 : :
52 : 70 : LIBSPDM_ASSERT (spdm_request_size >= sizeof(spdm_response_if_ready_request_t));
53 : 70 : spdm_request->header.spdm_version = libspdm_get_connection_version (spdm_context);
54 : 70 : spdm_request->header.request_response_code = SPDM_RESPOND_IF_READY;
55 : 70 : spdm_request->header.param1 = spdm_context->error_data.request_code;
56 : 70 : spdm_request->header.param2 = spdm_context->error_data.token;
57 : 70 : spdm_request_size = sizeof(spdm_response_if_ready_request_t);
58 : 70 : status = libspdm_send_spdm_request(spdm_context, session_id, spdm_request_size, spdm_request);
59 [ - + ]: 70 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
60 : 0 : libspdm_release_sender_buffer (spdm_context);
61 : : /* need acquire response buffer, so that the caller can release it */
62 : 0 : status = libspdm_acquire_receiver_buffer (spdm_context, response_size, response);
63 : 0 : return status;
64 : : }
65 : 70 : libspdm_release_sender_buffer (spdm_context);
66 : 70 : spdm_request = (void *)spdm_context->last_spdm_request;
67 : :
68 : : /* receive
69 : : * do not release response buffer in case of error, because caller will release it*/
70 : :
71 : 70 : status = libspdm_acquire_receiver_buffer (spdm_context, response_size, response);
72 [ - + ]: 70 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
73 : 0 : return status;
74 : : }
75 : 70 : LIBSPDM_ASSERT (*response_size >= transport_header_size);
76 : :
77 : 70 : status = libspdm_receive_spdm_response(spdm_context, session_id,
78 : : response_size, response);
79 [ - + ]: 70 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
80 : 0 : return status;
81 : : }
82 : 70 : spdm_response = (void *)(*response);
83 [ - + ]: 70 : if (*response_size < sizeof(spdm_message_header_t)) {
84 : 0 : return LIBSPDM_STATUS_INVALID_MSG_SIZE;
85 : : }
86 [ - + ]: 70 : if (spdm_response->spdm_version != spdm_request->header.spdm_version) {
87 : 0 : return LIBSPDM_STATUS_INVALID_MSG_FIELD;
88 : : }
89 [ + + ]: 70 : if (spdm_response->request_response_code == SPDM_ERROR) {
90 : 32 : status = libspdm_handle_simple_error_response(spdm_context, spdm_response->param1);
91 : 32 : return status;
92 : : }
93 [ - + ]: 38 : if (spdm_response->request_response_code != expected_response_code) {
94 : 0 : return LIBSPDM_STATUS_INVALID_MSG_FIELD;
95 : : }
96 : :
97 : 38 : return LIBSPDM_STATUS_SUCCESS;
98 : : }
99 : : #endif /* LIBSPDM_RESPOND_IF_READY_SUPPORT */
100 : :
101 : 976 : libspdm_return_t libspdm_handle_simple_error_response(libspdm_context_t *spdm_context,
102 : : uint8_t error_code)
103 : : {
104 : : spdm_set_certificate_request_t *last_spdm_request;
105 : :
106 [ + + ]: 976 : if (error_code == SPDM_ERROR_CODE_RESPONSE_NOT_READY) {
107 : 40 : return LIBSPDM_STATUS_NOT_READY_PEER;
108 : : }
109 : :
110 [ + + ]: 936 : if (error_code == SPDM_ERROR_CODE_BUSY) {
111 : 120 : return LIBSPDM_STATUS_BUSY_PEER;
112 : : }
113 : :
114 : 816 : last_spdm_request = (void *)spdm_context->last_spdm_request;
115 [ + + ]: 816 : if ((last_spdm_request->header.request_response_code == SPDM_SET_CERTIFICATE) ||
116 [ + + ]: 814 : (last_spdm_request->header.request_response_code == SPDM_GET_CSR)) {
117 : :
118 [ + - ]: 8 : if (error_code == SPDM_ERROR_CODE_RESET_REQUIRED) {
119 [ + + ]: 8 : if ((libspdm_get_connection_version(spdm_context) >= SPDM_MESSAGE_VERSION_13) &&
120 [ + + ]: 4 : !libspdm_is_capabilities_flag_supported(
121 : : spdm_context, true, 0,
122 : : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_INSTALL_RESET_CAP)) {
123 : 2 : return LIBSPDM_STATUS_ERROR_PEER;
124 : : }
125 : : /* CERT_INSTALL_RESET_CAP for a 1.2 Responder is not checked because it was not defined
126 : : * in SPDM 1.2.0. */
127 : 6 : return LIBSPDM_STATUS_RESET_REQUIRED_PEER;
128 : : }
129 : : }
130 : :
131 [ + + ]: 808 : if (last_spdm_request->header.request_response_code == SPDM_SET_KEY_PAIR_INFO) {
132 [ + - ]: 2 : if (error_code == SPDM_ERROR_CODE_RESET_REQUIRED) {
133 [ + - ]: 2 : if ((libspdm_get_connection_version(spdm_context) >= SPDM_MESSAGE_VERSION_14) &&
134 [ - + ]: 2 : !libspdm_is_capabilities_flag_supported(
135 : : spdm_context, true, 0,
136 : : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_SET_KEY_PAIR_RESET_CAP)) {
137 : 0 : return LIBSPDM_STATUS_ERROR_PEER;
138 : : }
139 : 2 : return LIBSPDM_STATUS_RESET_REQUIRED_PEER;
140 : : }
141 : : }
142 : :
143 [ + + ]: 806 : if (error_code == SPDM_ERROR_CODE_REQUEST_RESYNCH) {
144 : 52 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NOT_STARTED;
145 : 52 : return LIBSPDM_STATUS_RESYNCH_PEER;
146 : : }
147 : :
148 : 754 : return LIBSPDM_STATUS_ERROR_PEER;
149 : : }
150 : :
151 : : #if LIBSPDM_RESPOND_IF_READY_SUPPORT
152 : : /**
153 : : * This function handles RESPONSE_NOT_READY error code.
154 : : *
155 : : * @param spdm_context A pointer to the SPDM context.
156 : : * @param response_size The size of the response.
157 : : * On input, it means the size in bytes of response data buffer.
158 : : * On output, it means the size in bytes of copied response data
159 : : * buffer if LIBSPDM_STATUS_SUCCESS is returned.
160 : : * @param response The SPDM response message.
161 : : * @param original_request_code Indicate the original request code.
162 : : * @param expected_response_code Indicate the expected response code.
163 : : **/
164 : 70 : static libspdm_return_t libspdm_handle_response_not_ready(libspdm_context_t *spdm_context,
165 : : const uint32_t *session_id,
166 : : size_t *response_size,
167 : : void **response,
168 : : uint8_t original_request_code,
169 : : uint8_t expected_response_code)
170 : : {
171 : : spdm_error_response_t *spdm_response;
172 : : spdm_error_data_response_not_ready_t *extend_error_data;
173 : :
174 [ - + ]: 70 : if (*response_size < sizeof(spdm_error_response_t) +
175 : : sizeof(spdm_error_data_response_not_ready_t)) {
176 : 0 : return LIBSPDM_STATUS_INVALID_MSG_SIZE;
177 : : }
178 : :
179 : 70 : spdm_response = *response;
180 : 70 : extend_error_data = (spdm_error_data_response_not_ready_t *)(spdm_response + 1);
181 : 70 : LIBSPDM_ASSERT(spdm_response->header.request_response_code == SPDM_ERROR);
182 : 70 : LIBSPDM_ASSERT(spdm_response->header.param1 == SPDM_ERROR_CODE_RESPONSE_NOT_READY);
183 : :
184 [ - + ]: 70 : if (extend_error_data->request_code != original_request_code) {
185 : 0 : return LIBSPDM_STATUS_INVALID_MSG_FIELD;
186 : : }
187 [ - + ]: 70 : if (extend_error_data->rd_tm <= 1) {
188 : 0 : return LIBSPDM_STATUS_INVALID_MSG_FIELD;
189 : : }
190 [ - + ]: 70 : if (extend_error_data->rd_exponent > LIBSPDM_MAX_RDT_EXPONENT) {
191 : 0 : return LIBSPDM_STATUS_INVALID_MSG_FIELD;
192 : : }
193 : :
194 : 70 : spdm_context->error_data.rd_exponent = extend_error_data->rd_exponent;
195 : 70 : spdm_context->error_data.request_code = extend_error_data->request_code;
196 : 70 : spdm_context->error_data.token = extend_error_data->token;
197 : 70 : spdm_context->error_data.rd_tm = extend_error_data->rd_tm;
198 : :
199 : 70 : libspdm_sleep((uint64_t)1 << extend_error_data->rd_exponent);
200 : :
201 : 70 : return libspdm_requester_respond_if_ready(spdm_context, session_id,
202 : : response_size, response,
203 : : expected_response_code);
204 : : }
205 : : #endif /* LIBSPDM_RESPOND_IF_READY_SUPPORT */
206 : :
207 : : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
208 : 34 : libspdm_return_t libspdm_handle_error_large_response(
209 : : libspdm_context_t *spdm_context,
210 : : const uint32_t *session_id,
211 : : size_t *inout_response_size,
212 : : void *inout_response,
213 : : size_t response_capacity,
214 : : bool response_from_chunk)
215 : : {
216 : : libspdm_return_t status;
217 : : uint8_t chunk_handle;
218 : : spdm_error_response_t* error_response;
219 : : spdm_error_data_large_response_t* extend_error_data;
220 : :
221 : : spdm_chunk_get_request_t* spdm_request;
222 : : spdm_chunk_get_request_14_t* spdm_request_14;
223 : : size_t spdm_request_size;
224 : : spdm_chunk_response_response_t* spdm_response;
225 : : spdm_chunk_response_response_14_t* spdm_response_14;
226 : : uint8_t* message;
227 : : size_t message_size;
228 : : size_t transport_header_size;
229 : :
230 : : uint8_t* scratch_buffer;
231 : : size_t scratch_buffer_size;
232 : : uint32_t chunk_seq_no;
233 : : uint8_t* chunk_ptr;
234 : : uint8_t* large_response;
235 : : size_t large_response_capacity;
236 : : size_t large_response_size;
237 : : size_t large_response_size_so_far;
238 : : uint64_t max_chunk_data_transfer_size;
239 : : size_t min_large_response_size;
240 : : bool is_last_chunk;
241 : :
242 [ - + ]: 34 : if (libspdm_get_connection_version(spdm_context) < SPDM_MESSAGE_VERSION_12) {
243 : 0 : return LIBSPDM_STATUS_UNSUPPORTED_CAP;
244 : : }
245 : :
246 : : /* Fail if requester or responder does not support chunk cap. The Responder's negotiated
247 : : * capability flags are only known once the CAPABILITIES response has been processed
248 : : * (connection state AFTER_CAPABILITIES). A CAPABILITIES response that carries the Supported
249 : : * Algorithms Block can itself be chunked, in which case this handler runs while the state is
250 : : * still AFTER_VERSION; skip the check then, since the Responder returning
251 : : * ERROR_LARGE_RESPONSE already implies it supports chunking. */
252 [ + + ]: 34 : if (spdm_context->connection_info.connection_state >= LIBSPDM_CONNECTION_STATE_AFTER_CAPABILITIES) {
253 [ - + ]: 32 : if (!libspdm_is_capabilities_flag_supported(
254 : : spdm_context, true,
255 : : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_CHUNK_CAP,
256 : : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CHUNK_CAP)) {
257 : 0 : return LIBSPDM_STATUS_ERROR_PEER;
258 : : }
259 : : }
260 : :
261 [ - + ]: 34 : if (*inout_response_size < sizeof(spdm_error_response_t) +
262 : : sizeof(spdm_error_data_large_response_t)) {
263 : 0 : return LIBSPDM_STATUS_INVALID_MSG_SIZE;
264 : : }
265 : :
266 : 34 : error_response = inout_response;
267 : 34 : extend_error_data =
268 : : (spdm_error_data_large_response_t*)(error_response + 1);
269 : 34 : chunk_handle = extend_error_data->handle;
270 : :
271 : : /* now acquire the sender buffer */
272 : 34 : transport_header_size = spdm_context->local_context.capability.transport_header_size;
273 : :
274 : 34 : libspdm_get_scratch_buffer(spdm_context, (void**)&scratch_buffer, &scratch_buffer_size);
275 : :
276 : : /* The first section of the scratch
277 : : * buffer may be used for other purposes. Use only after that section. */
278 : 34 : large_response = scratch_buffer + libspdm_get_scratch_buffer_large_message_offset(spdm_context);
279 : 34 : large_response_capacity = libspdm_get_scratch_buffer_large_message_capacity(spdm_context);
280 : :
281 : : /* Temporary send/receive buffers for chunking are in the scratch space */
282 : 34 : message = scratch_buffer + libspdm_get_scratch_buffer_sender_receiver_offset(spdm_context);
283 : 34 : message_size = libspdm_get_scratch_buffer_sender_receiver_capacity(spdm_context);
284 : :
285 [ + + ]: 34 : if (libspdm_get_connection_version(spdm_context) < SPDM_MESSAGE_VERSION_14) {
286 : : /* chunk seq no wrap not considered in spdm 1.4+ */
287 : 28 : max_chunk_data_transfer_size =
288 : 28 : ((uint64_t) spdm_context->local_context.capability.data_transfer_size
289 : 28 : - sizeof(spdm_chunk_response_response_t)) * 65536 - sizeof(uint32_t);
290 : : }
291 : :
292 : 34 : libspdm_zero_mem(large_response, large_response_capacity);
293 : 34 : large_response_size = 0;
294 : 34 : large_response_size_so_far = 0;
295 : 34 : chunk_seq_no = 0;
296 : 34 : is_last_chunk = false;
297 : :
298 : : /* Mark that a CHUNK_GET transfer is in progress so that the response to a CHUNK_GET is not
299 : : * itself dispatched back into this handler by libspdm_receive_spdm_response. Per spec the
300 : : * response to CHUNK_GET must be CHUNK_RESPONSE; a nested ERROR_LARGE_RESPONSE would otherwise
301 : : * cause unbounded recursion. */
302 : 34 : spdm_context->chunk_context.get.chunk_in_use = true;
303 : :
304 : : do {
305 : 194 : LIBSPDM_ASSERT(message_size >= transport_header_size);
306 : :
307 : 194 : spdm_request = (spdm_chunk_get_request_t*)(void*) (message + transport_header_size);
308 : 194 : spdm_request_size = message_size - transport_header_size;
309 [ + + ]: 194 : if (libspdm_get_connection_version(spdm_context) < SPDM_MESSAGE_VERSION_14) {
310 : 170 : LIBSPDM_ASSERT(spdm_request_size >= sizeof(spdm_chunk_get_request_t));
311 : 170 : spdm_request->header.spdm_version = libspdm_get_connection_version(spdm_context);
312 : 170 : spdm_request->header.request_response_code = SPDM_CHUNK_GET;
313 : 170 : spdm_request->header.param1 = 0;
314 : 170 : spdm_request->header.param2 = chunk_handle;
315 : 170 : spdm_request->chunk_seq_no = (uint16_t) chunk_seq_no;
316 : 170 : spdm_request_size = sizeof(spdm_chunk_get_request_t);
317 : :
318 [ + + - + ]: 170 : if ((uint16_t) chunk_seq_no == 0 && large_response_size_so_far != 0) {
319 : : /* chunk_seq_no wrapped */
320 : 0 : status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
321 : 12 : break;
322 : : }
323 : : } else {
324 : 24 : LIBSPDM_ASSERT(spdm_request_size >= sizeof(spdm_chunk_get_request_14_t));
325 : 24 : spdm_request_14 = (spdm_chunk_get_request_14_t*)spdm_request;
326 : 24 : spdm_request_14->header.spdm_version = libspdm_get_connection_version(spdm_context);
327 : 24 : spdm_request_14->header.request_response_code = SPDM_CHUNK_GET;
328 : 24 : spdm_request_14->header.param1 = 0;
329 : 24 : spdm_request_14->header.param2 = chunk_handle;
330 : 24 : spdm_request_14->chunk_seq_no = chunk_seq_no;
331 : 24 : spdm_request_size = sizeof(spdm_chunk_get_request_14_t);
332 : : }
333 : :
334 : :
335 : 194 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO,
336 : : "CHUNK_GET Handle %d SeqNo %d\n", chunk_handle, chunk_seq_no));
337 : :
338 : 194 : status = libspdm_send_spdm_request(spdm_context, session_id,
339 : : spdm_request_size, spdm_request);
340 : :
341 [ - + ]: 194 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
342 : 0 : break;
343 : : }
344 : :
345 : 194 : libspdm_zero_mem(message, message_size);
346 : 194 : void* response = message;
347 : 194 : size_t response_size = message_size;
348 : :
349 : 194 : status = libspdm_receive_spdm_response(
350 : : spdm_context, session_id,
351 : : &response_size, &response);
352 : :
353 [ - + ]: 194 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
354 : 0 : break;
355 : : }
356 : 194 : spdm_response = (void*) (response);
357 [ + + ]: 194 : if (spdm_response->header.request_response_code == SPDM_ERROR) {
358 : 2 : status = libspdm_handle_simple_error_response(spdm_context,
359 : 2 : spdm_response->header.param1);
360 : 2 : break;
361 : : }
362 [ - + ]: 192 : if (spdm_response->header.spdm_version != libspdm_get_connection_version(spdm_context)) {
363 : 0 : status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
364 : 0 : break;
365 : : }
366 [ - + ]: 192 : if (spdm_response->header.request_response_code != SPDM_CHUNK_RESPONSE) {
367 : 0 : status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
368 : 0 : break;
369 : : }
370 [ + + ]: 192 : if (spdm_response->header.param2 != chunk_handle) {
371 : 4 : status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
372 : 4 : break;
373 : : }
374 : :
375 : : LIBSPDM_ASSERT(sizeof(spdm_chunk_response_response_t) == sizeof(spdm_chunk_response_response_14_t));
376 [ + + ]: 188 : if (chunk_seq_no == 0) {
377 : :
378 : 28 : if (response_size
379 [ - + ]: 28 : < (sizeof(spdm_chunk_response_response_t) + sizeof(uint32_t))) {
380 : 0 : status = LIBSPDM_STATUS_INVALID_MSG_SIZE;
381 : 0 : break;
382 : : }
383 [ - + ]: 28 : if (response_size < SPDM_MIN_DATA_TRANSFER_SIZE_VERSION_12) {
384 : 0 : status = LIBSPDM_STATUS_INVALID_MSG_SIZE;
385 : 0 : break;
386 : : }
387 : :
388 [ - + ]: 28 : if (spdm_response->chunk_size
389 : : < SPDM_MIN_DATA_TRANSFER_SIZE_VERSION_12
390 : : - sizeof(spdm_chunk_response_response_t)
391 : : - sizeof(uint32_t)) {
392 : 0 : status = LIBSPDM_STATUS_INVALID_MSG_SIZE;
393 : 0 : break;
394 : : }
395 : :
396 : 28 : large_response_size = libspdm_read_uint32((const uint8_t *)(spdm_response + 1));
397 : 28 : chunk_ptr = (uint8_t *)(spdm_response + 1) + sizeof(uint32_t);
398 : :
399 [ - + ]: 28 : if (spdm_response->chunk_size > large_response_size) {
400 : 0 : status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
401 : 0 : break;
402 : : }
403 [ - + ]: 28 : if (large_response_size > spdm_context->local_context.capability.max_spdm_msg_size) {
404 : 0 : status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
405 : 0 : break;
406 : : }
407 [ + + ]: 28 : if (libspdm_get_connection_version(spdm_context) < SPDM_MESSAGE_VERSION_14
408 [ + + ]: 24 : && large_response_size > max_chunk_data_transfer_size) {
409 : 2 : status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
410 : 2 : break;
411 : : }
412 [ + + ]: 26 : if (response_from_chunk) {
413 : : /* If the Responder's DataTransferSize is approximately equal to MinDataTransferSize
414 : : * then Responder may chunk a message whose size is less than MinDataTransferSize. */
415 [ + - ]: 2 : if (libspdm_get_connection_version(spdm_context) >= SPDM_MESSAGE_VERSION_14){
416 : 2 : min_large_response_size = SPDM_MIN_DATA_TRANSFER_SIZE_VERSION_12 -
417 : : sizeof(spdm_chunk_send_ack_response_14_t);
418 : : } else {
419 : 0 : min_large_response_size = SPDM_MIN_DATA_TRANSFER_SIZE_VERSION_12 -
420 : : sizeof(spdm_chunk_send_ack_response_t);
421 : : }
422 : : } else {
423 : 24 : min_large_response_size = SPDM_MIN_DATA_TRANSFER_SIZE_VERSION_12;
424 : : }
425 [ - + ]: 26 : if (large_response_size <= min_large_response_size) {
426 : 0 : status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
427 : 0 : break;
428 : : }
429 : : } else {
430 [ - + ]: 160 : if (response_size < sizeof(spdm_chunk_response_response_t)) {
431 : 0 : status = LIBSPDM_STATUS_INVALID_MSG_SIZE;
432 : 0 : break;
433 : : }
434 [ - + ]: 160 : if (spdm_response->chunk_size + large_response_size_so_far > large_response_size) {
435 : 0 : status = LIBSPDM_STATUS_INVALID_MSG_SIZE;
436 : 0 : break;
437 : : }
438 : :
439 [ + + ]: 160 : if (!(spdm_response->header.param1 & SPDM_CHUNK_GET_RESPONSE_ATTRIBUTE_LAST_CHUNK)) {
440 [ + + ]: 142 : if (response_size < SPDM_MIN_DATA_TRANSFER_SIZE_VERSION_12) {
441 : 2 : status = LIBSPDM_STATUS_INVALID_MSG_SIZE;
442 : 2 : break;
443 : : }
444 [ - + ]: 140 : if (spdm_response->chunk_size
445 : : < SPDM_MIN_DATA_TRANSFER_SIZE_VERSION_12
446 : : - sizeof(spdm_chunk_response_response_t)) {
447 : 0 : status = LIBSPDM_STATUS_INVALID_MSG_SIZE;
448 : 0 : break;
449 : : }
450 : : }
451 : :
452 : 158 : chunk_ptr = (uint8_t*) (spdm_response + 1);
453 : : }
454 [ + + ]: 184 : if (libspdm_get_connection_version(spdm_context) < SPDM_MESSAGE_VERSION_14) {
455 [ - + ]: 162 : if (spdm_response->chunk_seq_no != (uint16_t) chunk_seq_no) {
456 : 0 : status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
457 : 0 : break;
458 : : }
459 : : } else {
460 : 22 : spdm_response_14 = (spdm_chunk_response_response_14_t*)spdm_response;
461 [ - + ]: 22 : if (spdm_response_14->chunk_seq_no != chunk_seq_no) {
462 : 0 : status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
463 : 0 : break;
464 : : }
465 : : }
466 : :
467 : 184 : if (spdm_response->chunk_size >
468 [ + + ]: 184 : response_size - (size_t)(chunk_ptr - (uint8_t *)spdm_response)) {
469 : 2 : status = LIBSPDM_STATUS_INVALID_MSG_SIZE;
470 : 2 : break;
471 : : }
472 : :
473 : 182 : libspdm_copy_mem(large_response + large_response_size_so_far,
474 : : large_response_size - large_response_size_so_far,
475 : 182 : chunk_ptr, spdm_response->chunk_size);
476 : :
477 : 182 : large_response_size_so_far += spdm_response->chunk_size;
478 : 182 : is_last_chunk =
479 : 182 : (spdm_response->header.param1 & SPDM_CHUNK_GET_RESPONSE_ATTRIBUTE_LAST_CHUNK) != 0;
480 : :
481 : 182 : chunk_seq_no++;
482 : :
483 : 182 : } while (LIBSPDM_STATUS_IS_SUCCESS(status)
484 [ + + ]: 182 : && large_response_size_so_far < large_response_size
485 [ + - + + ]: 344 : && !is_last_chunk);
486 : :
487 : :
488 [ + + ]: 34 : if (LIBSPDM_STATUS_IS_SUCCESS(status)) {
489 [ + + + + ]: 22 : if (!is_last_chunk || (large_response_size_so_far != large_response_size)) {
490 : 6 : status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
491 [ + - ]: 16 : } else if (large_response_size <= response_capacity) {
492 : 16 : libspdm_copy_mem(inout_response, response_capacity,
493 : : large_response, large_response_size);
494 : 16 : *inout_response_size = large_response_size;
495 : :
496 : 16 : LIBSPDM_INTERNAL_DUMP_HEX(large_response, large_response_size);
497 : 16 : libspdm_zero_mem(large_response, large_response_size);
498 : : }
499 : : }
500 : :
501 : 34 : spdm_context->chunk_context.get.chunk_in_use = false;
502 : :
503 : 34 : return status;
504 : : }
505 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP */
506 : :
507 : 898 : libspdm_return_t libspdm_handle_error_response_main(
508 : : libspdm_context_t *spdm_context, const uint32_t *session_id,
509 : : size_t *response_size, void **response,
510 : : uint8_t original_request_code, uint8_t expected_response_code)
511 : : {
512 : : spdm_message_header_t *spdm_response;
513 : :
514 : 898 : spdm_response = *response;
515 : 898 : LIBSPDM_ASSERT(spdm_response->request_response_code == SPDM_ERROR);
516 : :
517 [ + + + + ]: 898 : if ((spdm_response->param1 == SPDM_ERROR_CODE_DECRYPT_ERROR) && (session_id != NULL)) {
518 : 10 : libspdm_free_session_id(spdm_context, *session_id);
519 : 10 : return LIBSPDM_STATUS_SESSION_MSG_ERROR;
520 [ + + ]: 888 : } else if (spdm_response->param1 == SPDM_ERROR_CODE_RESPONSE_NOT_READY) {
521 : : #if LIBSPDM_RESPOND_IF_READY_SUPPORT
522 : 70 : return libspdm_handle_response_not_ready(spdm_context, session_id,
523 : : response_size, response,
524 : : original_request_code,
525 : : expected_response_code);
526 : : #else
527 : : return LIBSPDM_STATUS_NOT_READY_PEER;
528 : : #endif /* LIBSPDM_RESPOND_IF_READY_SUPPORT */
529 : : } else {
530 : 818 : return libspdm_handle_simple_error_response(spdm_context, spdm_response->param1);
531 : : }
532 : : }
|