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 "internal/libspdm_requester_lib.h"
8 : #include "internal/libspdm_secured_message_lib.h"
9 :
10 68177 : libspdm_return_t libspdm_send_request(void *spdm_context, const uint32_t *session_id,
11 : bool is_app_message,
12 : size_t request_size, void *request)
13 : {
14 : libspdm_context_t *context;
15 : libspdm_return_t status;
16 : uint8_t *message;
17 : size_t message_size;
18 : uint64_t timeout;
19 : uint8_t *scratch_buffer;
20 : size_t scratch_buffer_size;
21 : size_t transport_header_size;
22 : uint8_t *sender_buffer;
23 : size_t sender_buffer_size;
24 :
25 68177 : context = spdm_context;
26 :
27 68177 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO,
28 : "libspdm_send_spdm_request[%x] msg %s(0x%x), size (0x%zx): \n",
29 : (session_id != NULL) ? *session_id : 0x0,
30 : libspdm_get_code_str(((spdm_message_header_t *)request)->request_response_code),
31 : ((spdm_message_header_t *)request)->request_response_code, request_size));
32 68177 : LIBSPDM_INTERNAL_DUMP_HEX(request, request_size);
33 :
34 68177 : transport_header_size = context->local_context.capability.transport_header_size;
35 68177 : libspdm_get_scratch_buffer(context, (void **)&scratch_buffer, &scratch_buffer_size);
36 68177 : libspdm_get_sender_buffer(context, (void **)&sender_buffer, &sender_buffer_size);
37 :
38 : /* This is a problem because original code assumes request is in the sender buffer,
39 : * when it can really be using the scratch space for chunking.
40 : * Did not want to modify all request handlers to pass this information,
41 : * so just making the determination here by examining scratch/sender buffers.
42 : * This may be something that should be refactored in the future. */
43 : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
44 68177 : if ((uint8_t *)request >= sender_buffer &&
45 68177 : (uint8_t *)request < sender_buffer + sender_buffer_size) {
46 0 : message = sender_buffer;
47 0 : message_size = sender_buffer_size;
48 : } else {
49 68177 : if ((uint8_t *)request >=
50 68177 : scratch_buffer + libspdm_get_scratch_buffer_sender_receiver_offset(spdm_context)
51 68177 : && (uint8_t *)request <
52 136354 : scratch_buffer + libspdm_get_scratch_buffer_sender_receiver_offset(spdm_context)
53 68177 : + libspdm_get_scratch_buffer_sender_receiver_capacity(spdm_context)) {
54 131238 : message = scratch_buffer +
55 65619 : libspdm_get_scratch_buffer_sender_receiver_offset(spdm_context);
56 65619 : message_size = libspdm_get_scratch_buffer_sender_receiver_capacity(spdm_context);
57 2558 : } else if ((uint8_t *)request >=
58 5116 : scratch_buffer +
59 2558 : libspdm_get_scratch_buffer_large_sender_receiver_offset(spdm_context)
60 2558 : && (uint8_t *)request <
61 : scratch_buffer +
62 2558 : libspdm_get_scratch_buffer_large_sender_receiver_offset(spdm_context) +
63 2558 : libspdm_get_scratch_buffer_large_sender_receiver_capacity(spdm_context)) {
64 5116 : message = scratch_buffer +
65 2558 : libspdm_get_scratch_buffer_large_sender_receiver_offset(spdm_context);
66 2558 : message_size = libspdm_get_scratch_buffer_large_sender_receiver_capacity(spdm_context);
67 : }
68 : }
69 : #else /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP */
70 : message = sender_buffer;
71 : message_size = sender_buffer_size;
72 : #endif /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP */
73 :
74 68177 : if (session_id != NULL) {
75 : /* For secure message, message is in sender buffer, we need copy it to scratch buffer.
76 : * transport_message is always in sender buffer. */
77 :
78 237 : libspdm_copy_mem (scratch_buffer + transport_header_size,
79 : scratch_buffer_size - transport_header_size,
80 : request, request_size);
81 237 : request = scratch_buffer + transport_header_size;
82 : }
83 :
84 : /* backup it to last_spdm_request, because the caller wants to compare it with response */
85 68177 : if (((const spdm_message_header_t *)request)->request_response_code != SPDM_RESPOND_IF_READY
86 68148 : && ((const spdm_message_header_t *)request)->request_response_code != SPDM_CHUNK_GET
87 2543 : && ((const spdm_message_header_t *)request)->request_response_code != SPDM_CHUNK_SEND) {
88 2529 : libspdm_copy_mem (context->last_spdm_request,
89 2529 : libspdm_get_scratch_buffer_last_spdm_request_capacity(context),
90 : request,
91 : request_size);
92 2529 : context->last_spdm_request_size = request_size;
93 : }
94 :
95 68177 : status = context->transport_encode_message(
96 : context, session_id, is_app_message, true, request_size,
97 : request, &message_size, (void **)&message);
98 68177 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
99 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "transport_encode_message status - %xu\n", status));
100 0 : if ((session_id != NULL) &&
101 0 : ((status == LIBSPDM_STATUS_SEQUENCE_NUMBER_OVERFLOW) ||
102 : (status == LIBSPDM_STATUS_CRYPTO_ERROR))) {
103 0 : libspdm_free_session_id(context, *session_id);
104 : }
105 0 : return status;
106 : }
107 :
108 68177 : timeout = context->local_context.capability.rtt;
109 68177 : status = context->send_message(context, message_size, message, timeout);
110 :
111 68177 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
112 24 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "libspdm_send_spdm_request[%x] status - %xu\n",
113 : (session_id != NULL) ? *session_id : 0x0, status));
114 : }
115 :
116 68177 : return status;
117 : }
118 :
119 68147 : libspdm_return_t libspdm_receive_response(void *spdm_context, const uint32_t *session_id,
120 : bool is_app_message,
121 : size_t *response_size,
122 : void **response)
123 : {
124 : libspdm_context_t *context;
125 : void *temp_session_context;
126 : libspdm_return_t status;
127 : uint8_t *message;
128 : size_t message_size;
129 : uint32_t *message_session_id;
130 : uint32_t message_id;
131 : bool is_message_app_message;
132 : uint64_t timeout;
133 : size_t transport_header_size;
134 : uint8_t *scratch_buffer;
135 : size_t scratch_buffer_size;
136 : void *backup_response;
137 : size_t backup_response_size;
138 : bool reset_key_update;
139 : bool result;
140 :
141 68147 : context = spdm_context;
142 :
143 68147 : if (context->crypto_request) {
144 67963 : timeout = context->local_context.capability.rtt +
145 67963 : ((uint64_t)1 << context->connection_info.capability.ct_exponent);
146 : } else {
147 184 : timeout = context->local_context.capability.rtt + context->local_context.capability.st1;
148 : }
149 :
150 68147 : message = *response;
151 68147 : message_size = *response_size;
152 68147 : status = context->receive_message(context, &message_size, (void **)&message, timeout);
153 68147 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
154 9 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO,
155 : "libspdm_receive_spdm_response[%x] status - %xu\n",
156 : (session_id != NULL) ? *session_id : 0x0, status));
157 9 : return status;
158 : }
159 :
160 : /*
161 : * The storage transport encoding, defined by DSP0286, does not indicate
162 : * if we are/are not in a secure session in the transport data. This is
163 : * different to most other transport encodings, which includes session
164 : * information in the encoding.
165 : *
166 : * As such if we are in a secure session, session_id != NULL, we set
167 : * message_session_id to be non-NULL to indicate to the lower layer
168 : * that we are in a secure session.
169 : */
170 68138 : if (session_id != NULL) {
171 233 : message_session_id = &message_id;
172 233 : message_id = *session_id;
173 : } else {
174 67905 : message_session_id = NULL;
175 : }
176 68138 : is_message_app_message = false;
177 :
178 : /* always use scratch buffer to response.
179 : * if it is secured message, this scratch buffer will be used.
180 : * if it is normal message, the response ptr will point to receiver buffer. */
181 68138 : transport_header_size = context->local_context.capability.transport_header_size;
182 68138 : libspdm_get_scratch_buffer (context, (void **)&scratch_buffer, &scratch_buffer_size);
183 : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
184 68138 : *response = scratch_buffer + libspdm_get_scratch_buffer_secure_message_offset(context) +
185 : transport_header_size;
186 68138 : *response_size = libspdm_get_scratch_buffer_secure_message_capacity(context) -
187 : transport_header_size;
188 : #else
189 : *response = scratch_buffer + transport_header_size;
190 : *response_size = scratch_buffer_size - transport_header_size;
191 : #endif
192 :
193 68138 : backup_response = *response;
194 68138 : backup_response_size = *response_size;
195 :
196 68138 : status = context->transport_decode_message(
197 : context, &message_session_id, &is_message_app_message,
198 : false, message_size, message, response_size, response);
199 :
200 68138 : reset_key_update = false;
201 68138 : temp_session_context = NULL;
202 :
203 68138 : if (status == LIBSPDM_STATUS_SESSION_TRY_DISCARD_KEY_UPDATE) {
204 : /* Failed to decode, but have backup keys. Try rolling back before aborting.
205 : * message_session_id must be valid for us to have attempted decryption. */
206 27 : if (message_session_id == NULL) {
207 0 : return LIBSPDM_STATUS_INVALID_STATE_LOCAL;
208 : }
209 27 : temp_session_context = libspdm_get_secured_message_context_via_session_id(
210 : context, *message_session_id);
211 27 : if (temp_session_context == NULL) {
212 0 : return LIBSPDM_STATUS_INVALID_STATE_LOCAL;
213 : }
214 :
215 27 : result = libspdm_activate_update_session_data_key(
216 : temp_session_context, LIBSPDM_KEY_UPDATE_ACTION_RESPONDER, false);
217 27 : if (!result) {
218 0 : return LIBSPDM_STATUS_INVALID_STATE_LOCAL;
219 : }
220 :
221 : /* Retry decoding message with backup Requester key.
222 : * Must reset some of the parameters in case they were modified */
223 27 : if (session_id != NULL) {
224 27 : *message_session_id = *session_id;
225 : } else {
226 0 : message_session_id = NULL;
227 : }
228 27 : is_message_app_message = false;
229 27 : *response = backup_response;
230 27 : *response_size = backup_response_size;
231 27 : status = context->transport_decode_message(
232 : context, &message_session_id, &is_message_app_message,
233 : false, message_size, message, response_size, response);
234 :
235 27 : reset_key_update = true;
236 : }
237 :
238 68138 : if (session_id != NULL) {
239 233 : if (message_session_id == NULL) {
240 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO,
241 : "libspdm_receive_spdm_response[%x] GetSessionId - NULL\n",
242 : (session_id != NULL) ? *session_id : 0x0));
243 0 : goto error;
244 : }
245 233 : if (*message_session_id != *session_id) {
246 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO,
247 : "libspdm_receive_spdm_response[%x] GetSessionId - %x\n",
248 : (session_id != NULL) ? *session_id : 0x0, *message_session_id));
249 0 : goto error;
250 : }
251 : } else {
252 67905 : if (message_session_id != NULL) {
253 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO,
254 : "libspdm_receive_spdm_response[%x] GetSessionId - %x\n",
255 : (session_id != NULL) ? *session_id : 0x0, *message_session_id));
256 0 : goto error;
257 : }
258 : }
259 :
260 68138 : if ((is_app_message && !is_message_app_message) ||
261 68138 : (!is_app_message && is_message_app_message)) {
262 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO,
263 : "libspdm_receive_spdm_response[%x] app_message mismatch\n",
264 : (session_id != NULL) ? *session_id : 0x0));
265 0 : goto error;
266 : }
267 :
268 68138 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
269 0 : if ((session_id != NULL) &&
270 0 : (context->last_spdm_error.error_code == SPDM_ERROR_CODE_DECRYPT_ERROR)) {
271 0 : libspdm_free_session_id(context, *session_id);
272 : }
273 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO,
274 : "libspdm_receive_spdm_response[%x] status - %xu\n",
275 : (session_id != NULL) ? *session_id : 0x0, status));
276 : } else {
277 68138 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO,
278 : "libspdm_receive_spdm_response[%x] msg %s(0x%x), size (0x%zx): \n",
279 : (session_id != NULL) ? *session_id : 0x0,
280 : libspdm_get_code_str(((spdm_message_header_t *)*response)->
281 : request_response_code),
282 : ((spdm_message_header_t *)*response)->request_response_code,
283 : *response_size));
284 68138 : LIBSPDM_INTERNAL_DUMP_HEX(*response, *response_size);
285 : }
286 :
287 : /* Handle special case:
288 : * If the Responder returns RESPONSE_NOT_READY error to KEY_UPDATE, the Requester needs
289 : * to activate backup key to parse the error. Then later the Responder will return SUCCESS,
290 : * the Requester needs new key. So we need to restore the environment by
291 : * libspdm_create_update_session_data_key() again.*/
292 68138 : if (reset_key_update) {
293 : /* temp_session_context and message_session_id must necessarily
294 : * be valid for us to reach here. */
295 27 : if (temp_session_context == NULL || message_session_id == NULL) {
296 0 : return LIBSPDM_STATUS_INVALID_STATE_LOCAL;
297 : }
298 27 : result = libspdm_create_update_session_data_key(
299 : temp_session_context, LIBSPDM_KEY_UPDATE_ACTION_RESPONDER);
300 27 : if (!result) {
301 0 : return LIBSPDM_STATUS_INVALID_STATE_LOCAL;
302 : }
303 : }
304 :
305 68138 : return status;
306 :
307 0 : error:
308 0 : if (context->last_spdm_error.error_code == SPDM_ERROR_CODE_DECRYPT_ERROR) {
309 0 : return LIBSPDM_STATUS_SESSION_MSG_ERROR;
310 : } else {
311 0 : return LIBSPDM_STATUS_RECEIVE_FAIL;
312 : }
313 : }
314 :
315 : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
316 13 : libspdm_return_t libspdm_handle_large_request(
317 : libspdm_context_t *spdm_context,
318 : const uint32_t *session_id,
319 : size_t request_size, void *request)
320 : {
321 : libspdm_return_t status;
322 :
323 : spdm_chunk_send_request_t *spdm_request;
324 : size_t spdm_request_size;
325 : spdm_chunk_send_ack_response_t *spdm_response;
326 : uint8_t *message;
327 : size_t message_size;
328 : void *response;
329 : size_t response_size;
330 : size_t transport_header_size;
331 :
332 : uint8_t *scratch_buffer;
333 : size_t scratch_buffer_size;
334 :
335 : uint8_t *chunk_ptr;
336 : size_t copy_size;
337 : libspdm_chunk_info_t *send_info;
338 : uint32_t min_data_transfer_size;
339 : uint64_t max_chunk_data_transfer_size;
340 : spdm_error_response_t *spdm_error;
341 :
342 13 : if (libspdm_get_connection_version(spdm_context) < SPDM_MESSAGE_VERSION_12) {
343 0 : return LIBSPDM_STATUS_UNSUPPORTED_CAP;
344 : }
345 :
346 : /* Fail if requester or responder does not support chunk cap */
347 13 : if (!libspdm_is_capabilities_flag_supported(
348 : spdm_context, true,
349 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_CHUNK_CAP,
350 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CHUNK_CAP)) {
351 0 : return LIBSPDM_STATUS_ERROR_PEER;
352 : }
353 :
354 : /* Fail if exceed max chunks */
355 13 : min_data_transfer_size = LIBSPDM_MIN(
356 : spdm_context->connection_info.capability.data_transfer_size,
357 : spdm_context->local_context.capability.sender_data_transfer_size);
358 :
359 13 : max_chunk_data_transfer_size =
360 13 : ((size_t) min_data_transfer_size - sizeof(spdm_chunk_send_request_t)) * 65536 -
361 : sizeof(uint32_t);
362 : /* max_spdm_msg_size is already checked in caller */
363 :
364 13 : if (request_size > max_chunk_data_transfer_size) {
365 1 : return LIBSPDM_STATUS_BUFFER_TOO_SMALL;
366 : }
367 :
368 : /* now we can get sender buffer */
369 12 : transport_header_size = spdm_context->local_context.capability.transport_header_size;
370 :
371 12 : libspdm_get_scratch_buffer(spdm_context, (void **)&scratch_buffer, &scratch_buffer_size);
372 :
373 : /* Temporary send/receive buffers for chunking are in the scratch space */
374 12 : message = scratch_buffer + libspdm_get_scratch_buffer_sender_receiver_offset(spdm_context);
375 12 : message_size = libspdm_get_scratch_buffer_sender_receiver_capacity(spdm_context);
376 :
377 12 : send_info = &spdm_context->chunk_context.send;
378 12 : send_info->chunk_in_use = true;
379 :
380 : /* The first section of the scratch
381 : * buffer may be used for other purposes. Use only after that section. */
382 24 : send_info->large_message = scratch_buffer +
383 12 : libspdm_get_scratch_buffer_large_message_offset(spdm_context);
384 12 : send_info->large_message_capacity =
385 12 : libspdm_get_scratch_buffer_large_message_capacity(spdm_context);
386 :
387 12 : libspdm_zero_mem(send_info->large_message, send_info->large_message_capacity);
388 12 : libspdm_copy_mem(send_info->large_message, send_info->large_message_capacity,
389 : request, request_size);
390 :
391 12 : send_info->large_message_size = request_size;
392 12 : send_info->chunk_bytes_transferred = 0;
393 12 : send_info->chunk_seq_no = 0;
394 12 : request = NULL; /* Invalidate to prevent accidental use. */
395 12 : request_size = 0;
396 :
397 : do {
398 14 : LIBSPDM_ASSERT(send_info->large_message_capacity >= transport_header_size);
399 14 : spdm_request = (spdm_chunk_send_request_t *)((uint8_t *)message + transport_header_size);
400 14 : spdm_request_size = message_size - transport_header_size;
401 :
402 14 : spdm_request->header.spdm_version = libspdm_get_connection_version(spdm_context);
403 14 : spdm_request->header.request_response_code = SPDM_CHUNK_SEND;
404 14 : spdm_request->header.param1 = 0;
405 14 : spdm_request->header.param2 = send_info->chunk_handle;
406 14 : spdm_request->chunk_seq_no = send_info->chunk_seq_no;
407 14 : spdm_request->reserved = 0;
408 14 : chunk_ptr = (uint8_t *)(spdm_request + 1);
409 :
410 14 : if ((min_data_transfer_size - sizeof(spdm_chunk_send_request_t)) <
411 14 : (send_info->large_message_size - send_info->chunk_bytes_transferred)) {
412 12 : copy_size = min_data_transfer_size - sizeof(spdm_chunk_send_request_t);
413 : } else {
414 2 : copy_size = (send_info->large_message_size - send_info->chunk_bytes_transferred);
415 : }
416 :
417 14 : if (send_info->chunk_seq_no == 0) {
418 12 : *(uint32_t *)(spdm_request + 1) = (uint32_t)send_info->large_message_size;
419 12 : chunk_ptr += sizeof(uint32_t);
420 12 : copy_size -= sizeof(uint32_t);
421 : }
422 :
423 14 : spdm_request->chunk_size = (uint32_t)copy_size;
424 :
425 14 : libspdm_copy_mem(
426 14 : chunk_ptr, spdm_request_size - ((uint8_t *)spdm_request - (uint8_t *)message),
427 14 : (uint8_t *)send_info->large_message + send_info->chunk_bytes_transferred, copy_size);
428 :
429 14 : send_info->chunk_bytes_transferred += copy_size;
430 14 : if (send_info->chunk_bytes_transferred >= send_info->large_message_size) {
431 2 : spdm_request->header.param1 |= SPDM_CHUNK_SEND_REQUEST_ATTRIBUTE_LAST_CHUNK;
432 : }
433 :
434 14 : spdm_request_size = (chunk_ptr + copy_size) - (uint8_t *)spdm_request;
435 14 : status = libspdm_send_request(
436 : spdm_context, session_id, false,
437 : spdm_request_size, spdm_request);
438 :
439 14 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
440 1 : break;
441 : }
442 :
443 13 : response = message;
444 13 : response_size = message_size;
445 :
446 13 : libspdm_zero_mem(response, response_size);
447 :
448 13 : status = libspdm_receive_response(
449 : spdm_context, session_id, false,
450 : &response_size, &response);
451 :
452 13 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
453 1 : break;
454 : }
455 12 : spdm_response = (void *)(response);
456 :
457 12 : if (response_size < sizeof(spdm_message_header_t)) {
458 0 : status = LIBSPDM_STATUS_INVALID_MSG_SIZE;
459 0 : break;
460 : }
461 :
462 12 : if (spdm_response->header.request_response_code == SPDM_ERROR
463 2 : && spdm_response->header.param1 != SPDM_ERROR_CODE_LARGE_RESPONSE) {
464 2 : status = libspdm_handle_simple_error_response(spdm_context,
465 2 : spdm_response->header.param1);
466 2 : break;
467 : }
468 :
469 10 : if (spdm_response->header.spdm_version != libspdm_get_connection_version(spdm_context)) {
470 1 : status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
471 1 : break;
472 : }
473 :
474 9 : if (spdm_response->header.request_response_code == SPDM_ERROR
475 0 : && spdm_response->header.param1 == SPDM_ERROR_CODE_LARGE_RESPONSE) {
476 :
477 : /* It is possible that the CHUNK_SEND_ACK + chunk response is larger
478 : * than the DATA_TRANSFER_SIZE. In this case an ERROR_LARGE_RESPONSE
479 : * is returned directly in the response buffer rather than part of
480 : * the CHUNK_SEND_ACK. Store this error response in scratch buffer
481 : * to be handled when reading response. Also note that in this case
482 : * of large response, the CHUNK_SEND_ACK portion is not sent.
483 : * Only the response portion that requires the CHUNK_GET is sent */
484 0 : if (response_size < send_info->large_message_capacity) {
485 0 : libspdm_copy_mem(
486 : send_info->large_message, send_info->large_message_capacity,
487 : spdm_response, response_size);
488 0 : send_info->large_message_size = response_size;
489 0 : break;
490 : } else {
491 0 : status = LIBSPDM_STATUS_INVALID_MSG_SIZE;
492 0 : break;
493 : }
494 : } else {
495 9 : if (spdm_response->header.request_response_code != SPDM_CHUNK_SEND_ACK) {
496 0 : status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
497 0 : break;
498 : }
499 :
500 9 : if (response_size < sizeof(spdm_chunk_send_ack_response_t)) {
501 1 : status = LIBSPDM_STATUS_INVALID_MSG_SIZE;
502 1 : break;
503 : }
504 8 : if (spdm_response->header.param1
505 8 : & SPDM_CHUNK_SEND_ACK_RESPONSE_ATTRIBUTE_EARLY_ERROR_DETECTED) {
506 :
507 2 : spdm_error = (spdm_error_response_t *)(spdm_response + 1);
508 2 : if (response_size < (sizeof(spdm_chunk_send_ack_response_t) +
509 : sizeof(spdm_error_response_t))) {
510 0 : status = LIBSPDM_STATUS_INVALID_MSG_SIZE;
511 0 : break;
512 : }
513 2 : if ((spdm_error->header.spdm_version !=
514 2 : libspdm_get_connection_version(spdm_context)) ||
515 2 : (spdm_error->header.request_response_code != SPDM_ERROR)) {
516 0 : status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
517 0 : break;
518 : }
519 2 : if (spdm_error->header.param1 == SPDM_ERROR_CODE_LARGE_RESPONSE) {
520 1 : status = LIBSPDM_STATUS_ERROR_PEER;
521 1 : break;
522 : }
523 :
524 : /* Store the error response in scratch buffer to be read by
525 : * libspdm_receive_spdm_response and returned to its caller
526 : * and handled in the error response handling flow */
527 1 : libspdm_copy_mem(
528 : send_info->large_message,
529 : send_info->large_message_capacity,
530 1 : (uint8_t *)(spdm_response + 1),
531 : response_size - sizeof(spdm_chunk_send_ack_response_t));
532 :
533 1 : send_info->large_message_size =
534 1 : (response_size - sizeof(spdm_chunk_send_ack_response_t));
535 :
536 1 : status = LIBSPDM_STATUS_SUCCESS;
537 1 : break;
538 : }
539 6 : if (spdm_response->header.param2 != send_info->chunk_handle) {
540 1 : status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
541 1 : break;
542 : }
543 5 : if (send_info->chunk_seq_no != spdm_response->chunk_seq_no) {
544 1 : status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
545 1 : break;
546 : }
547 :
548 4 : chunk_ptr = (uint8_t *)(spdm_response + 1);
549 4 : send_info->chunk_seq_no++;
550 :
551 4 : if (send_info->chunk_bytes_transferred >= send_info->large_message_size) {
552 : /* All bytes have been transferred. Store response in scratch buffer
553 : * to be read by libspdm_receive_spdm_response */
554 2 : libspdm_copy_mem(
555 : send_info->large_message, send_info->large_message_capacity,
556 : chunk_ptr, response_size - sizeof(spdm_chunk_send_ack_response_t));
557 2 : send_info->large_message_size =
558 2 : (response_size - sizeof(spdm_chunk_send_ack_response_t));
559 2 : break;
560 : }
561 : }
562 :
563 2 : } while (LIBSPDM_STATUS_IS_SUCCESS(status)
564 2 : && send_info->chunk_bytes_transferred < send_info->large_message_size);
565 :
566 12 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
567 9 : send_info->chunk_in_use = false;
568 9 : send_info->chunk_handle++; /* Implicit wrap-around*/
569 9 : send_info->chunk_seq_no = 0;
570 9 : send_info->chunk_bytes_transferred = 0;
571 9 : send_info->large_message = NULL;
572 9 : send_info->large_message_size = 0;
573 : }
574 :
575 12 : return status;
576 : }
577 : #endif /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP */
578 :
579 68177 : libspdm_return_t libspdm_send_spdm_request(libspdm_context_t *spdm_context,
580 : const uint32_t *session_id,
581 : size_t request_size, void *request)
582 : {
583 : libspdm_session_info_t *session_info;
584 : libspdm_session_state_t session_state;
585 : libspdm_return_t status;
586 : #if LIBSPDM_ENABLE_MSG_LOG
587 : size_t msg_log_size;
588 : #endif /* LIBSPDM_ENABLE_MSG_LOG */
589 :
590 : /* If chunking is not supported then message must fit in both the send buffer and the receive
591 : * buffer. */
592 68177 : if (!libspdm_is_capabilities_flag_supported(
593 : spdm_context, true,
594 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_CHUNK_CAP,
595 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CHUNK_CAP)) {
596 2549 : if ((spdm_context->connection_info.capability.data_transfer_size != 0) &&
597 0 : (request_size > spdm_context->connection_info.capability.data_transfer_size)) {
598 0 : return LIBSPDM_STATUS_PEER_BUFFER_TOO_SMALL;
599 : }
600 2549 : if ((spdm_context->local_context.capability.sender_data_transfer_size != 0) &&
601 2549 : (request_size > spdm_context->local_context.capability.sender_data_transfer_size)) {
602 0 : return LIBSPDM_STATUS_BUFFER_TOO_SMALL;
603 : }
604 : }
605 :
606 68452 : if ((session_id != NULL) &&
607 275 : libspdm_is_capabilities_flag_supported(
608 : spdm_context, true,
609 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP,
610 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP)) {
611 38 : session_info = libspdm_get_session_info_via_session_id(spdm_context, *session_id);
612 38 : LIBSPDM_ASSERT(session_info != NULL);
613 38 : if (session_info == NULL) {
614 0 : return LIBSPDM_STATUS_INVALID_STATE_LOCAL;
615 : }
616 38 : session_state = libspdm_secured_message_get_session_state(
617 : session_info->secured_message_context);
618 38 : if ((session_state == LIBSPDM_SESSION_STATE_HANDSHAKING) && !session_info->use_psk) {
619 38 : session_id = NULL;
620 : }
621 : }
622 :
623 68177 : if ((spdm_context->connection_info.capability.max_spdm_msg_size != 0) &&
624 65555 : (request_size > spdm_context->connection_info.capability.max_spdm_msg_size)) {
625 1 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR, "request_size > rsp max_spdm_msg_size\n"));
626 1 : return LIBSPDM_STATUS_PEER_BUFFER_TOO_SMALL;
627 : }
628 68176 : LIBSPDM_ASSERT (request_size <= spdm_context->local_context.capability.max_spdm_msg_size);
629 :
630 : #if LIBSPDM_ENABLE_MSG_LOG
631 : /* First save the size of the message log buffer. If there is an error it will be reverted. */
632 68176 : msg_log_size = libspdm_get_msg_log_size(spdm_context);
633 68176 : libspdm_append_msg_log(spdm_context, request, request_size);
634 : #endif /* LIBSPDM_ENABLE_MSG_LOG */
635 :
636 : /* large SPDM message is the SPDM message whose size is greater than the DataTransferSize of the receiving
637 : * SPDM endpoint or greater than the transmit buffer size of the sending SPDM endpoint */
638 68176 : if (((const spdm_message_header_t *)request)->request_response_code != SPDM_GET_VERSION
639 68138 : && ((const spdm_message_header_t *)request)->request_response_code != SPDM_GET_CAPABILITIES
640 68072 : && ((spdm_context->connection_info.capability.data_transfer_size != 0 &&
641 65553 : request_size > spdm_context->connection_info.capability.data_transfer_size) ||
642 68060 : (spdm_context->local_context.capability.sender_data_transfer_size != 0 &&
643 68060 : request_size > spdm_context->local_context.capability.sender_data_transfer_size))) {
644 :
645 : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
646 : /* libspdm_send_request is not called with the original request in this flow.
647 : * This leads to the last_spdm_request field not having the original request value.
648 : * The caller assumes the request has been copied to last_spdm_request,
649 : * so that it can compare last_spdm_request's fields with response fields
650 : * Therefore the request must be copied to last_spdm_request here. */
651 :
652 13 : if (((const spdm_message_header_t *)request)->request_response_code != SPDM_RESPOND_IF_READY
653 13 : && ((const spdm_message_header_t *)request)->request_response_code != SPDM_CHUNK_GET
654 13 : && ((const spdm_message_header_t *)request)->request_response_code != SPDM_CHUNK_SEND) {
655 13 : libspdm_copy_mem(
656 : spdm_context->last_spdm_request,
657 13 : libspdm_get_scratch_buffer_last_spdm_request_capacity(spdm_context),
658 : request, request_size);
659 13 : spdm_context->last_spdm_request_size = request_size;
660 : }
661 :
662 13 : status = libspdm_handle_large_request(
663 : spdm_context, session_id, request_size, request);
664 : #else /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP*/
665 : status = LIBSPDM_STATUS_BUFFER_TOO_SMALL;
666 : #endif /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP*/
667 : } else {
668 68163 : status = libspdm_send_request(spdm_context, session_id, false, request_size, request);
669 : }
670 :
671 : #if LIBSPDM_ENABLE_MSG_LOG
672 : /* If there is an error in sending the request then revert the request in the message log. */
673 68176 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
674 33 : spdm_context->msg_log.buffer_size = msg_log_size;
675 : }
676 : #endif /* LIBSPDM_ENABLE_MSG_LOG */
677 :
678 68176 : return status;
679 : }
680 :
681 68137 : libspdm_return_t libspdm_receive_spdm_response(libspdm_context_t *spdm_context,
682 : const uint32_t *session_id,
683 : size_t *response_size,
684 : void **response)
685 : {
686 : libspdm_return_t status;
687 : libspdm_session_info_t *session_info;
688 : libspdm_session_state_t session_state;
689 :
690 : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
691 : spdm_message_header_t *spdm_response;
692 : size_t response_capacity;
693 : libspdm_chunk_info_t *send_info;
694 : #endif /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP */
695 :
696 68407 : if ((session_id != NULL) &&
697 270 : libspdm_is_capabilities_flag_supported(
698 : spdm_context, true,
699 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP,
700 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP)) {
701 37 : session_info = libspdm_get_session_info_via_session_id(spdm_context, *session_id);
702 37 : LIBSPDM_ASSERT(session_info != NULL);
703 37 : if (session_info == NULL) {
704 0 : return LIBSPDM_STATUS_INVALID_STATE_LOCAL;
705 : }
706 37 : session_state = libspdm_secured_message_get_session_state(
707 : session_info->secured_message_context);
708 37 : if ((session_state == LIBSPDM_SESSION_STATE_HANDSHAKING) && !session_info->use_psk) {
709 37 : session_id = NULL;
710 : }
711 : }
712 :
713 : #if !(LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP)
714 : status = libspdm_receive_response(spdm_context, session_id, false, response_size, response);
715 : #else /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP */
716 68137 : send_info = &spdm_context->chunk_context.send;
717 68137 : if (send_info->chunk_in_use) {
718 3 : libspdm_copy_mem(*response, *response_size,
719 3 : send_info->large_message, send_info->large_message_size);
720 3 : *response_size = send_info->large_message_size;
721 3 : response_capacity = send_info->large_message_capacity;
722 :
723 : /* This response may either be an actual response or ERROR_LARGE_RESPONSE,
724 : * the latter which should be handled in the large response handler. */
725 3 : send_info->chunk_in_use = false;
726 3 : send_info->chunk_handle++; /* Implicit wrap-around*/
727 3 : send_info->chunk_seq_no = 0;
728 3 : send_info->chunk_bytes_transferred = 0;
729 3 : send_info->large_message = NULL;
730 3 : send_info->large_message_size = 0;
731 3 : send_info->large_message_capacity = 0;
732 3 : status = LIBSPDM_STATUS_SUCCESS;
733 : } else {
734 68134 : response_capacity = *response_size;
735 68134 : status = libspdm_receive_response(spdm_context, session_id, false, response_size, response);
736 68134 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
737 8 : goto receive_done;
738 : }
739 : }
740 :
741 68129 : spdm_response = (spdm_message_header_t *)(*response);
742 :
743 68129 : if (*response_size < sizeof(spdm_message_header_t)) {
744 0 : status = LIBSPDM_STATUS_INVALID_MSG_SIZE;
745 0 : goto receive_done;
746 : }
747 :
748 68129 : if (spdm_response->request_response_code == SPDM_ERROR
749 473 : && spdm_response->param1 == SPDM_ERROR_CODE_LARGE_RESPONSE) {
750 7 : status = libspdm_handle_error_large_response(
751 : spdm_context, session_id,
752 : response_size, (void *)spdm_response, response_capacity);
753 :
754 7 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
755 2 : goto receive_done;
756 : }
757 :
758 5 : if (*response_size < sizeof(spdm_message_header_t)) {
759 0 : status = LIBSPDM_STATUS_INVALID_MSG_SIZE;
760 0 : goto receive_done;
761 : }
762 :
763 : /* Per the spec, SPDM_VERSION and SPDM_CAPABILITIES shall not be chunked
764 : * and should be an unexpected error. */
765 5 : if (spdm_response->request_response_code == SPDM_VERSION ||
766 5 : spdm_response->request_response_code == SPDM_CAPABILITIES) {
767 0 : status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
768 0 : goto receive_done;
769 : }
770 : }
771 :
772 68127 : receive_done:
773 : #endif /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP */
774 :
775 68137 : return status;
776 : }
|