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