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_responder_lib.h"
8 : #include "internal/libspdm_secured_message_lib.h"
9 :
10 : /**
11 : * Return the GET_SPDM_RESPONSE function via request code.
12 : *
13 : * @param request_code The SPDM request code.
14 : *
15 : * @return GET_SPDM_RESPONSE function according to the request code.
16 : **/
17 15 : libspdm_get_spdm_response_func libspdm_get_response_func_via_request_code(uint8_t request_code)
18 : {
19 : size_t index;
20 :
21 : typedef struct {
22 : uint8_t request_response_code;
23 : libspdm_get_spdm_response_func get_response_func;
24 : } libspdm_get_response_struct_t;
25 :
26 15 : libspdm_get_response_struct_t get_response_struct[] = {
27 : { SPDM_GET_VERSION, libspdm_get_response_version },
28 : { SPDM_GET_CAPABILITIES, libspdm_get_response_capabilities },
29 : { SPDM_NEGOTIATE_ALGORITHMS, libspdm_get_response_algorithms },
30 :
31 : #if LIBSPDM_ENABLE_CAPABILITY_CERT_CAP
32 : { SPDM_GET_DIGESTS, libspdm_get_response_digests },
33 : { SPDM_GET_CERTIFICATE, libspdm_get_response_certificate },
34 : #endif /* LIBSPDM_ENABLE_CAPABILITY_CERT_CAP */
35 :
36 : #if LIBSPDM_ENABLE_CAPABILITY_CHAL_CAP
37 : { SPDM_CHALLENGE, libspdm_get_response_challenge_auth },
38 : #endif /* LIBSPDM_ENABLE_CAPABILITY_CHAL_CAP*/
39 :
40 : #if LIBSPDM_ENABLE_CAPABILITY_MEAS_CAP
41 : { SPDM_GET_MEASUREMENTS, libspdm_get_response_measurements },
42 : #endif /* LIBSPDM_ENABLE_CAPABILITY_MEAS_CAP*/
43 :
44 : #if LIBSPDM_ENABLE_CAPABILITY_MEL_CAP
45 : { SPDM_GET_MEASUREMENT_EXTENSION_LOG, libspdm_get_response_measurement_extension_log },
46 : #endif /* LIBSPDM_ENABLE_CAPABILITY_MEL_CAP */
47 :
48 : #if LIBSPDM_ENABLE_CAPABILITY_KEY_EX_CAP
49 : { SPDM_KEY_EXCHANGE, libspdm_get_response_key_exchange },
50 : #endif /* LIBSPDM_ENABLE_CAPABILITY_KEY_EX_CAP*/
51 :
52 : #if LIBSPDM_ENABLE_CAPABILITY_PSK_CAP
53 : { SPDM_PSK_EXCHANGE, libspdm_get_response_psk_exchange },
54 : #endif /* LIBSPDM_ENABLE_CAPABILITY_PSK_CAP*/
55 :
56 : #if LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP
57 : { SPDM_GET_ENCAPSULATED_REQUEST, libspdm_get_response_encapsulated_request },
58 : { SPDM_DELIVER_ENCAPSULATED_RESPONSE, libspdm_get_response_encapsulated_response_ack },
59 : #endif /* LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP */
60 :
61 : #if LIBSPDM_RESPOND_IF_READY_SUPPORT
62 : { SPDM_RESPOND_IF_READY, libspdm_get_response_respond_if_ready },
63 : #endif /* LIBSPDM_RESPOND_IF_READY_SUPPORT */
64 :
65 : #if LIBSPDM_ENABLE_CAPABILITY_KEY_EX_CAP
66 : { SPDM_FINISH, libspdm_get_response_finish },
67 : #endif /* LIBSPDM_ENABLE_CAPABILITY_KEY_EX_CAP*/
68 :
69 : #if LIBSPDM_ENABLE_CAPABILITY_PSK_CAP
70 : { SPDM_PSK_FINISH, libspdm_get_response_psk_finish },
71 : #endif /* LIBSPDM_ENABLE_CAPABILITY_PSK_CAP*/
72 :
73 : #if (LIBSPDM_ENABLE_CAPABILITY_KEY_EX_CAP) || (LIBSPDM_ENABLE_CAPABILITY_PSK_CAP)
74 : { SPDM_END_SESSION, libspdm_get_response_end_session },
75 : { SPDM_HEARTBEAT, libspdm_get_response_heartbeat },
76 : { SPDM_KEY_UPDATE, libspdm_get_response_key_update },
77 : #endif /* LIBSPDM_ENABLE_CAPABILITY_KEY_EX_CAP || LIBSPDM_ENABLE_CAPABILITY_PSK_CAP*/
78 :
79 : #if LIBSPDM_ENABLE_CAPABILITY_ENDPOINT_INFO_CAP
80 : { SPDM_GET_ENDPOINT_INFO, libspdm_get_response_endpoint_info },
81 : #endif /*LIBSPDM_ENABLE_CAPABILITY_ENDPOINT_INFO_CAP*/
82 :
83 : #if LIBSPDM_ENABLE_CAPABILITY_CSR_CAP
84 : { SPDM_GET_CSR, libspdm_get_response_csr },
85 : #endif /*LIBSPDM_ENABLE_CAPABILITY_CSR_CAP*/
86 :
87 : #if LIBSPDM_ENABLE_CAPABILITY_SET_CERT_CAP
88 : { SPDM_SET_CERTIFICATE, libspdm_get_response_set_certificate },
89 : #endif /*LIBSPDM_ENABLE_CAPABILITY_SET_CERT_CAP*/
90 :
91 : #if LIBSPDM_ENABLE_CAPABILITY_GET_KEY_PAIR_INFO_CAP
92 : { SPDM_GET_KEY_PAIR_INFO, libspdm_get_response_key_pair_info },
93 : #endif /*LIBSPDM_ENABLE_CAPABILITY_GET_KEY_PAIR_INFO_CAP*/
94 :
95 : #if LIBSPDM_ENABLE_CAPABILITY_SET_KEY_PAIR_INFO_CAP
96 : { SPDM_SET_KEY_PAIR_INFO, libspdm_get_response_set_key_pair_info_ack },
97 : #endif /*LIBSPDM_ENABLE_CAPABILITY_SET_KEY_PAIR_INFO_CAP*/
98 :
99 : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
100 : { SPDM_CHUNK_GET, libspdm_get_response_chunk_get},
101 : { SPDM_CHUNK_SEND, libspdm_get_response_chunk_send},
102 : #endif /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP */
103 :
104 : #if LIBSPDM_ENABLE_CAPABILITY_EVENT_CAP
105 : { SPDM_GET_SUPPORTED_EVENT_TYPES, libspdm_get_response_supported_event_types },
106 : { SPDM_SUBSCRIBE_EVENT_TYPES, libspdm_get_response_subscribe_event_types_ack },
107 : #endif /* LIBSPDM_ENABLE_CAPABILITY_EVENT_CAP */
108 :
109 : #if LIBSPDM_ENABLE_VENDOR_DEFINED_MESSAGES
110 : { SPDM_VENDOR_DEFINED_REQUEST, libspdm_get_vendor_defined_response },
111 : #endif /*LIBSPDM_ENABLE_VENDOR_DEFINED_MESSAGES*/
112 : };
113 :
114 150 : for (index = 0; index < LIBSPDM_ARRAY_SIZE(get_response_struct); index++) {
115 150 : if (request_code == get_response_struct[index].request_response_code) {
116 15 : return get_response_struct[index].get_response_func;
117 : }
118 : }
119 0 : return NULL;
120 : }
121 :
122 : /**
123 : * Return the GET_SPDM_RESPONSE function via last request.
124 : *
125 : * @param spdm_context The SPDM context for the device.
126 : *
127 : * @return GET_SPDM_RESPONSE function according to the last request.
128 : **/
129 4 : static libspdm_get_spdm_response_func libspdm_get_response_func_via_last_request(
130 : libspdm_context_t *spdm_context)
131 : {
132 : spdm_message_header_t *spdm_request;
133 :
134 4 : spdm_request = (void *)spdm_context->last_spdm_request;
135 4 : return libspdm_get_response_func_via_request_code(spdm_request->request_response_code);
136 : }
137 :
138 : /**
139 : * Process a SPDM request from a device.
140 : *
141 : * @param spdm_context The SPDM context for the device.
142 : * @param session_id Indicate if the request is a secured message.
143 : * If session_id is NULL, it is a normal message.
144 : * If session_id is NOT NULL, it is a secured message.
145 : * @param is_app_message Indicates if it is an APP message or SPDM message.
146 : * @param request_size size in bytes of the request data buffer.
147 : * @param request A pointer to a destination buffer to store the request.
148 : * The caller is responsible for having
149 : * either implicit or explicit ownership of the buffer.
150 : **/
151 0 : libspdm_return_t libspdm_process_request(void *spdm_context, uint32_t **session_id,
152 : bool *is_app_message,
153 : size_t request_size, void *request)
154 : {
155 : libspdm_context_t *context;
156 : void *temp_session_context;
157 : libspdm_return_t status;
158 : libspdm_session_info_t *session_info;
159 : uint32_t *message_session_id;
160 : uint8_t *decoded_message_ptr;
161 : size_t decoded_message_size;
162 : uint8_t *backup_decoded_message_ptr;
163 : size_t backup_decoded_message_size;
164 : bool result;
165 : bool reset_key_update;
166 :
167 0 : context = spdm_context;
168 : size_t transport_header_size;
169 : uint8_t *scratch_buffer;
170 : size_t scratch_buffer_size;
171 :
172 0 : if (request == NULL) {
173 0 : return LIBSPDM_STATUS_INVALID_PARAMETER;
174 : }
175 0 : if (request_size == 0) {
176 0 : return LIBSPDM_STATUS_INVALID_PARAMETER;
177 : }
178 :
179 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "SpdmReceiveRequest[.] ...\n"));
180 :
181 0 : message_session_id = NULL;
182 0 : context->last_spdm_request_session_id_valid = false;
183 0 : context->last_spdm_request_size =
184 0 : libspdm_get_scratch_buffer_last_spdm_request_capacity(context);
185 :
186 : /* always use scratch buffer to response.
187 : * if it is secured message, this scratch buffer will be used.
188 : * if it is normal message, the response ptr will point to receiver buffer. */
189 0 : transport_header_size = context->local_context.capability.transport_header_size;
190 0 : libspdm_get_scratch_buffer (context, (void **)&scratch_buffer, &scratch_buffer_size);
191 : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
192 0 : decoded_message_ptr = scratch_buffer +
193 0 : libspdm_get_scratch_buffer_secure_message_offset(context) +
194 : transport_header_size;
195 0 : decoded_message_size = libspdm_get_scratch_buffer_secure_message_capacity(context) -
196 : transport_header_size;
197 : #else
198 : decoded_message_ptr = scratch_buffer + transport_header_size;
199 : decoded_message_size = scratch_buffer_size - transport_header_size;
200 : #endif /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP */
201 :
202 0 : backup_decoded_message_ptr = decoded_message_ptr;
203 0 : backup_decoded_message_size = decoded_message_size;
204 :
205 0 : status = context->transport_decode_message(
206 : context, &message_session_id, is_app_message, true,
207 : request_size, request, &decoded_message_size,
208 : (void **)&decoded_message_ptr);
209 :
210 0 : reset_key_update = false;
211 0 : temp_session_context = NULL;
212 :
213 0 : if (status == LIBSPDM_STATUS_SESSION_TRY_DISCARD_KEY_UPDATE) {
214 : /* Failed to decode, but have backup keys. Try rolling back before aborting.
215 : * message_session_id must be valid for us to have attempted decryption. */
216 0 : if (message_session_id == NULL) {
217 0 : return LIBSPDM_STATUS_INVALID_STATE_LOCAL;
218 : }
219 0 : temp_session_context = libspdm_get_secured_message_context_via_session_id(
220 : context, *message_session_id);
221 0 : if (temp_session_context == NULL) {
222 0 : return LIBSPDM_STATUS_INVALID_STATE_LOCAL;
223 : }
224 :
225 0 : result = libspdm_activate_update_session_data_key(
226 : temp_session_context, LIBSPDM_KEY_UPDATE_ACTION_REQUESTER, false);
227 0 : if (!result) {
228 0 : return LIBSPDM_STATUS_INVALID_STATE_LOCAL;
229 : }
230 0 : libspdm_trigger_key_update_callback(
231 : context, *message_session_id,
232 : LIBSPDM_KEY_UPDATE_OPERATION_DISCARD_UPDATE,
233 : LIBSPDM_KEY_UPDATE_ACTION_REQUESTER);
234 :
235 : /* Retry decoding message with backup Requester key.
236 : * Must reset some of the parameters in case they were modified */
237 0 : message_session_id = NULL;
238 0 : decoded_message_ptr = backup_decoded_message_ptr;
239 0 : decoded_message_size = backup_decoded_message_size;
240 0 : status = context->transport_decode_message(
241 : context, &message_session_id, is_app_message, true,
242 : request_size, request, &decoded_message_size,
243 : (void **)&decoded_message_ptr);
244 :
245 0 : reset_key_update = true;
246 : }
247 :
248 0 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
249 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "transport_decode_message : %xu\n", status));
250 0 : if (context->last_spdm_error.error_code != 0) {
251 : /* If the SPDM error code is Non-Zero, that means we need send the error message back to requester.
252 : * In this case, we need return SUCCESS and let caller invoke libspdm_build_response() to send an ERROR message.*/
253 0 : *session_id = &context->last_spdm_error.session_id;
254 0 : *is_app_message = false;
255 0 : return LIBSPDM_STATUS_SUCCESS;
256 : }
257 0 : return status;
258 : }
259 :
260 : /* Handle special case for bi-directional communication:
261 : * If the Requester returns RESPONSE_NOT_READY error to KEY_UPDATE, the Responder needs
262 : * to activate backup key to parse the error. Then later the Requester will return SUCCESS,
263 : * the Responder needs new key. So we need to restore the environment by
264 : * libspdm_create_update_session_data_key() again.*/
265 0 : if (reset_key_update) {
266 : /* temp_session_context and message_session_id must necessarily
267 : * be valid for us to reach here. */
268 0 : if (temp_session_context == NULL || message_session_id == NULL) {
269 0 : return LIBSPDM_STATUS_INVALID_STATE_LOCAL;
270 : }
271 0 : result = libspdm_create_update_session_data_key(
272 : temp_session_context, LIBSPDM_KEY_UPDATE_ACTION_REQUESTER);
273 0 : if (!result) {
274 0 : return LIBSPDM_STATUS_INVALID_STATE_LOCAL;
275 : }
276 0 : libspdm_trigger_key_update_callback(
277 : context, *message_session_id,
278 : LIBSPDM_KEY_UPDATE_OPERATION_CREATE_UPDATE,
279 : LIBSPDM_KEY_UPDATE_ACTION_REQUESTER);
280 : }
281 :
282 : /*
283 : * decoded_message may contain padding zeros due to transport layer alignment requirements.
284 : * trim the decoded_message size to the maximum data_transfer_size.
285 : */
286 0 : decoded_message_size = LIBSPDM_MIN(decoded_message_size,
287 : context->local_context.capability.data_transfer_size);
288 :
289 0 : context->last_spdm_request_size = decoded_message_size;
290 0 : libspdm_copy_mem (context->last_spdm_request,
291 0 : libspdm_get_scratch_buffer_last_spdm_request_capacity(context),
292 : decoded_message_ptr,
293 : decoded_message_size);
294 :
295 0 : if (!(*is_app_message)) {
296 : /* Check for minimal SPDM message size. */
297 0 : if (context->last_spdm_request_size < sizeof(spdm_message_header_t)) {
298 0 : return LIBSPDM_STATUS_UNSUPPORTED_CAP;
299 : }
300 : }
301 :
302 0 : *session_id = message_session_id;
303 :
304 0 : if (message_session_id != NULL) {
305 0 : session_info = libspdm_get_session_info_via_session_id(context, *message_session_id);
306 0 : if (session_info == NULL) {
307 0 : return LIBSPDM_STATUS_UNSUPPORTED_CAP;
308 : }
309 0 : context->last_spdm_request_session_id = *message_session_id;
310 0 : context->last_spdm_request_session_id_valid = true;
311 : }
312 :
313 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "SpdmReceiveRequest[%x] msg %s(0x%x), size (0x%zx): \n",
314 : (message_session_id != NULL) ? *message_session_id : 0,
315 : libspdm_get_code_str(((spdm_message_header_t *)context->last_spdm_request)->
316 : request_response_code),
317 : ((spdm_message_header_t *)context->last_spdm_request)->request_response_code,
318 : context->last_spdm_request_size));
319 0 : LIBSPDM_INTERNAL_DUMP_HEX((uint8_t *)context->last_spdm_request,
320 : context->last_spdm_request_size);
321 :
322 0 : return LIBSPDM_STATUS_SUCCESS;
323 : }
324 :
325 : /**
326 : * Notify the session state to a session APP.
327 : *
328 : * @param spdm_context A pointer to the SPDM context.
329 : * @param session_id The session_id of a session.
330 : * @param session_state The state of a session.
331 : **/
332 22 : static void libspdm_trigger_session_state_callback(libspdm_context_t *spdm_context,
333 : uint32_t session_id,
334 : libspdm_session_state_t session_state)
335 : {
336 22 : if (spdm_context->spdm_session_state_callback != NULL) {
337 0 : ((libspdm_session_state_callback_func)
338 0 : spdm_context->spdm_session_state_callback)(spdm_context, session_id, session_state);
339 : }
340 22 : }
341 :
342 : /**
343 : * Set session_state to an SPDM secured message context and trigger callback.
344 : *
345 : * @param spdm_context A pointer to the SPDM context.
346 : * @param session_id Indicate the SPDM session ID.
347 : * @param session_state Indicate the SPDM session state.
348 : */
349 22 : void libspdm_set_session_state(libspdm_context_t *spdm_context,
350 : uint32_t session_id,
351 : libspdm_session_state_t session_state)
352 : {
353 : libspdm_session_info_t *session_info;
354 : libspdm_session_state_t old_session_state;
355 :
356 22 : session_info = libspdm_get_session_info_via_session_id(spdm_context, session_id);
357 22 : if (session_info == NULL) {
358 0 : LIBSPDM_ASSERT(false);
359 0 : return;
360 : }
361 :
362 22 : old_session_state = libspdm_secured_message_get_session_state(
363 : session_info->secured_message_context);
364 22 : if (old_session_state != session_state) {
365 22 : libspdm_secured_message_set_session_state(
366 : session_info->secured_message_context, session_state);
367 22 : libspdm_trigger_session_state_callback(
368 : spdm_context, session_info->session_id, session_state);
369 : }
370 : }
371 :
372 : /**
373 : * This function allows the consumer to terminate a session.
374 : * For example, it can be used when the heartbeat period is over.
375 : *
376 : * @param spdm_context A pointer to the SPDM context.
377 : * @param session_id session_id of the session to be terminated.
378 : *
379 : * @retval LIBSPDM_STATUS_SUCCESS Success
380 : * @retval LIBSPDM_STATUS_INVALID_PARAMETER session_id is invalid.
381 : **/
382 0 : libspdm_return_t libspdm_terminate_session(
383 : void *spdm_context, uint32_t session_id)
384 : {
385 : libspdm_session_info_t *session_info;
386 :
387 0 : session_info = libspdm_get_session_info_via_session_id(spdm_context, session_id);
388 0 : if (session_info == NULL) {
389 0 : return LIBSPDM_STATUS_INVALID_PARAMETER;
390 : }
391 :
392 0 : libspdm_set_session_state(spdm_context, session_id, LIBSPDM_SESSION_STATE_NOT_STARTED);
393 0 : libspdm_free_session_id(spdm_context, session_id);
394 0 : return LIBSPDM_STATUS_SUCCESS;
395 : }
396 :
397 : /**
398 : * Notify the connection state to an SPDM context register.
399 : *
400 : * @param spdm_context A pointer to the SPDM context.
401 : * @param connection_state Indicate the SPDM connection state.
402 : **/
403 81 : static void libspdm_trigger_connection_state_callback(libspdm_context_t *spdm_context,
404 : const libspdm_connection_state_t
405 : connection_state)
406 : {
407 81 : if (spdm_context->spdm_connection_state_callback != NULL) {
408 0 : ((libspdm_connection_state_callback_func)
409 0 : spdm_context->spdm_connection_state_callback)(spdm_context, connection_state);
410 : }
411 81 : }
412 :
413 : /**
414 : * Set connection_state to an SPDM context and trigger callback.
415 : *
416 : * @param spdm_context A pointer to the SPDM context.
417 : * @param connection_state Indicate the SPDM connection state.
418 : */
419 83 : void libspdm_set_connection_state(libspdm_context_t *spdm_context,
420 : libspdm_connection_state_t connection_state)
421 : {
422 83 : if (spdm_context->connection_info.connection_state != connection_state) {
423 81 : spdm_context->connection_info.connection_state = connection_state;
424 81 : libspdm_trigger_connection_state_callback(spdm_context, connection_state);
425 : }
426 83 : }
427 :
428 15 : void libspdm_trigger_key_update_callback(void *spdm_context, uint32_t session_id,
429 : libspdm_key_update_operation_t key_update_op,
430 : libspdm_key_update_action_t key_update_action)
431 : {
432 : libspdm_context_t *context;
433 :
434 15 : context = spdm_context;
435 15 : if (context->spdm_key_update_callback != NULL) {
436 0 : ((libspdm_key_update_callback_func)
437 0 : context->spdm_key_update_callback)(context, session_id, key_update_op, key_update_action);
438 : }
439 15 : }
440 :
441 4 : libspdm_return_t libspdm_build_response(void *spdm_context, const uint32_t *session_id,
442 : bool is_app_message,
443 : size_t *response_size,
444 : void **response)
445 : {
446 : libspdm_context_t *context;
447 : uint8_t *my_response;
448 : size_t my_response_size;
449 : libspdm_return_t status;
450 : libspdm_get_spdm_response_func get_response_func;
451 : libspdm_session_info_t *session_info;
452 : spdm_message_header_t *spdm_request;
453 : spdm_message_header_t *spdm_response;
454 : size_t transport_header_size;
455 : uint8_t *scratch_buffer;
456 : size_t scratch_buffer_size;
457 : uint8_t request_response_code;
458 : uint32_t actual_size;
459 :
460 : #if LIBSPDM_ENABLE_CAPABILITY_HBEAT_CAP
461 : bool result;
462 : #endif /* LIBSPDM_ENABLE_CAPABILITY_HBEAT_CAP */
463 :
464 : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
465 : uint8_t *large_buffer;
466 : size_t large_buffer_size;
467 : libspdm_chunk_info_t *get_info;
468 : spdm_chunk_response_response_t *chunk_rsp;
469 : uint8_t *chunk_ptr;
470 : size_t chunk_send_ack_response_header_size;
471 : #endif /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP */
472 :
473 4 : context = spdm_context;
474 4 : status = LIBSPDM_STATUS_UNSUPPORTED_CAP;
475 :
476 : /* For secure message, setup my_response to scratch buffer
477 : * For non-secure message, setup my_response to sender buffer*/
478 4 : transport_header_size = context->local_context.capability.transport_header_size;
479 4 : if (session_id != NULL) {
480 0 : libspdm_get_scratch_buffer (context, (void **)&scratch_buffer, &scratch_buffer_size);
481 : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
482 0 : my_response = scratch_buffer + libspdm_get_scratch_buffer_secure_message_offset(context) +
483 : transport_header_size;
484 0 : my_response_size = libspdm_get_scratch_buffer_secure_message_capacity(context) -
485 0 : transport_header_size -
486 0 : context->local_context.capability.transport_tail_size;
487 : #else
488 : my_response = scratch_buffer + transport_header_size;
489 : my_response_size = scratch_buffer_size - transport_header_size -
490 : context->local_context.capability.transport_tail_size;
491 : #endif /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP */
492 : } else {
493 4 : my_response = (uint8_t *)*response + transport_header_size;
494 4 : my_response_size = *response_size - transport_header_size -
495 4 : context->local_context.capability.transport_tail_size;
496 : }
497 4 : libspdm_zero_mem(my_response, my_response_size);
498 :
499 4 : spdm_response = (void *)my_response;
500 :
501 4 : if (context->last_spdm_error.error_code != 0) {
502 : /* Error in libspdm_process_request(), and we need send error message directly. */
503 0 : switch (context->last_spdm_error.error_code) {
504 0 : case SPDM_ERROR_CODE_DECRYPT_ERROR:
505 : /* session ID is valid. Use it to encrypt the error message.*/
506 0 : if ((context->handle_error_return_policy &
507 : LIBSPDM_DATA_HANDLE_ERROR_RETURN_POLICY_DROP_ON_DECRYPT_ERROR) == 0) {
508 0 : status = libspdm_generate_error_response(
509 : context, SPDM_ERROR_CODE_DECRYPT_ERROR, 0,
510 : &my_response_size, my_response);
511 : } else {
512 : /**
513 : * just ignore this message
514 : * return UNSUPPORTED and clear response_size to continue the dispatch without send response
515 : **/
516 0 : *response_size = 0;
517 0 : status = LIBSPDM_STATUS_UNSUPPORTED_CAP;
518 : }
519 0 : break;
520 0 : case SPDM_ERROR_CODE_INVALID_SESSION:
521 : /**
522 : * don't use session ID, because we dont know which right session ID should be used.
523 : * just ignore this message
524 : * return UNSUPPORTED and clear response_size to continue the dispatch without send response
525 : **/
526 0 : *response_size = 0;
527 0 : status = LIBSPDM_STATUS_UNSUPPORTED_CAP;
528 0 : break;
529 0 : default:
530 0 : LIBSPDM_ASSERT(false);
531 0 : status = LIBSPDM_STATUS_UNSUPPORTED_CAP;
532 : }
533 :
534 0 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
535 0 : if ((session_id != NULL) &&
536 0 : (context->last_spdm_error.error_code == SPDM_ERROR_CODE_DECRYPT_ERROR)) {
537 0 : libspdm_free_session_id(context, *session_id);
538 : }
539 0 : return status;
540 : }
541 :
542 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "SpdmSendResponse[%x]: msg %s(0x%x), size (0x%zx): \n",
543 : (session_id != NULL) ? *session_id : 0,
544 : libspdm_get_code_str(spdm_response->request_response_code),
545 : spdm_response->request_response_code, my_response_size));
546 0 : LIBSPDM_INTERNAL_DUMP_HEX(my_response, my_response_size);
547 :
548 0 : status = context->transport_encode_message(
549 : context, session_id, false, false,
550 : my_response_size, my_response, response_size, response);
551 0 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
552 0 : if ((session_id != NULL) &&
553 0 : ((status == LIBSPDM_STATUS_SEQUENCE_NUMBER_OVERFLOW) ||
554 : (status == LIBSPDM_STATUS_CRYPTO_ERROR))) {
555 0 : libspdm_free_session_id(context, *session_id);
556 : }
557 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "transport_encode_message : %xu\n", status));
558 0 : return status;
559 : }
560 :
561 0 : if ((session_id != NULL) &&
562 0 : (context->last_spdm_error.error_code == SPDM_ERROR_CODE_DECRYPT_ERROR)) {
563 0 : libspdm_free_session_id(context, *session_id);
564 : }
565 :
566 0 : libspdm_zero_mem(&context->last_spdm_error, sizeof(context->last_spdm_error));
567 0 : return LIBSPDM_STATUS_SUCCESS;
568 : }
569 :
570 4 : if (session_id != NULL) {
571 0 : session_info = libspdm_get_session_info_via_session_id(context, *session_id);
572 0 : if (session_info == NULL) {
573 0 : LIBSPDM_ASSERT(false);
574 0 : return LIBSPDM_STATUS_UNSUPPORTED_CAP;
575 : }
576 : }
577 :
578 4 : if (*response == NULL) {
579 0 : return LIBSPDM_STATUS_INVALID_PARAMETER;
580 : }
581 4 : if ((response_size == NULL) || (*response_size == 0)) {
582 0 : return LIBSPDM_STATUS_INVALID_PARAMETER;
583 : }
584 :
585 4 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "SpdmSendResponse[%x] ...\n",
586 : (session_id != NULL) ? *session_id : 0));
587 :
588 4 : spdm_request = (void *)context->last_spdm_request;
589 4 : if (context->last_spdm_request_size == 0) {
590 0 : return LIBSPDM_STATUS_INVALID_STATE_LOCAL;
591 : }
592 :
593 4 : get_response_func = NULL;
594 4 : if (!is_app_message) {
595 4 : get_response_func = libspdm_get_response_func_via_last_request(context);
596 :
597 : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
598 : /* If responder is expecting chunk_get or chunk_send requests
599 : * and gets other requests instead, drop out of chunking mode */
600 4 : if (context->chunk_context.get.chunk_in_use
601 0 : && get_response_func != libspdm_get_response_chunk_get) {
602 :
603 0 : context->chunk_context.get.chunk_in_use = false;
604 0 : context->chunk_context.get.chunk_handle++; /* implicit wrap - around to 0. */
605 0 : context->chunk_context.get.chunk_seq_no = 0;
606 :
607 0 : context->chunk_context.get.large_message = NULL;
608 0 : context->chunk_context.get.large_message_size = 0;
609 0 : context->chunk_context.get.chunk_bytes_transferred = 0;
610 : }
611 4 : if (context->chunk_context.send.chunk_in_use
612 0 : && get_response_func != libspdm_get_response_chunk_send) {
613 :
614 0 : context->chunk_context.send.chunk_in_use = false;
615 0 : context->chunk_context.send.chunk_handle = 0;
616 0 : context->chunk_context.send.chunk_seq_no = 0;
617 :
618 0 : context->chunk_context.send.large_message = NULL;
619 0 : context->chunk_context.send.large_message_size = 0;
620 0 : context->chunk_context.send.chunk_bytes_transferred = 0;
621 : }
622 : #endif /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP */
623 :
624 4 : if (get_response_func != NULL) {
625 4 : status = get_response_func(
626 : context,
627 : context->last_spdm_request_size,
628 4 : context->last_spdm_request,
629 : &my_response_size, my_response);
630 : }
631 : }
632 4 : if (is_app_message || (get_response_func == NULL)) {
633 0 : if (context->get_response_func != NULL) {
634 0 : status = ((libspdm_get_response_func) context->get_response_func)(
635 : context, session_id, is_app_message,
636 : context->last_spdm_request_size,
637 0 : context->last_spdm_request,
638 : &my_response_size, my_response);
639 : } else {
640 0 : status = LIBSPDM_STATUS_UNSUPPORTED_CAP;
641 : }
642 : }
643 :
644 : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
645 4 : if (libspdm_get_connection_version(context) < SPDM_MESSAGE_VERSION_14) {
646 4 : chunk_send_ack_response_header_size = sizeof(spdm_chunk_send_ack_response_t);
647 : } else {
648 0 : chunk_send_ack_response_header_size = sizeof(spdm_chunk_send_ack_response_14_t);
649 : }
650 : #endif /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP */
651 :
652 4 : if (status == LIBSPDM_STATUS_SUCCESS) {
653 4 : LIBSPDM_ASSERT (my_response_size <= context->local_context.capability.max_spdm_msg_size);
654 : /* large SPDM message is the SPDM message whose size is greater than the DataTransferSize of the receiving
655 : * SPDM endpoint or greater than the transmit buffer size of the sending SPDM endpoint */
656 4 : if ((context->connection_info.capability.max_spdm_msg_size != 0) &&
657 4 : (my_response_size > context->connection_info.capability.max_spdm_msg_size)) {
658 1 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR, "my_response_size > req max_spdm_msg_size\n"));
659 1 : actual_size = (uint32_t)my_response_size;
660 1 : status = libspdm_generate_extended_error_response(context,
661 : SPDM_ERROR_CODE_RESPONSE_TOO_LARGE,
662 : 0,
663 : sizeof(uint32_t),
664 : (uint8_t *)&actual_size,
665 : &my_response_size, my_response);
666 3 : } else if ((((context->connection_info.capability.data_transfer_size != 0) &&
667 3 : (my_response_size > context->connection_info.capability.data_transfer_size)) ||
668 2 : ((context->local_context.capability.sender_data_transfer_size != 0) &&
669 2 : (my_response_size >
670 5 : context->local_context.capability.sender_data_transfer_size))) &&
671 3 : libspdm_is_capabilities_flag_supported(
672 : context, false, SPDM_GET_CAPABILITIES_REQUEST_FLAGS_CHUNK_CAP,
673 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CHUNK_CAP)) {
674 : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
675 :
676 3 : get_info = &context->chunk_context.get;
677 :
678 : /* Saving multiple large responses is not an expected use case.
679 : * Therefore, if the requester did not perform chunk_get requests for
680 : * previous large responses, they will be lost. */
681 3 : if (get_info->chunk_in_use) {
682 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR,
683 : "Warning: Overwriting previous unrequested chunk_get info.\n"));
684 : }
685 :
686 3 : libspdm_get_scratch_buffer(context, (void **)&scratch_buffer, &scratch_buffer_size);
687 :
688 : /* The first section of the scratch
689 : * buffer may be used for other purposes. Use only after that section. */
690 6 : large_buffer = (uint8_t *)scratch_buffer +
691 3 : libspdm_get_scratch_buffer_large_message_offset(spdm_context);
692 3 : large_buffer_size = libspdm_get_scratch_buffer_large_message_capacity(spdm_context);
693 :
694 3 : get_info->chunk_in_use = true;
695 : /* Increment chunk_handle here as opposed to end of chunk_get handler
696 : * in case requester never issues chunk_get. */
697 3 : get_info->chunk_handle++;
698 3 : get_info->chunk_seq_no = 0;
699 3 : get_info->chunk_bytes_transferred = 0;
700 :
701 3 : libspdm_zero_mem(large_buffer, large_buffer_size);
702 :
703 : /* It's possible that the large response that was to be sent to the requester was
704 : * a CHUNK_SEND_ACK + non-chunk response. In this case, to prevent chunking within
705 : * chunking, only send back the actual response, by saving only non-chunk portion
706 : * in the scratch buffer, used to respond to the next CHUNK_GET request. */
707 3 : if (((spdm_message_header_t *)my_response)
708 3 : ->request_response_code == SPDM_CHUNK_SEND_ACK) {
709 0 : libspdm_copy_mem(large_buffer, large_buffer_size,
710 0 : my_response + chunk_send_ack_response_header_size,
711 : my_response_size - chunk_send_ack_response_header_size);
712 0 : get_info->large_message = large_buffer;
713 0 : get_info->large_message_size =
714 0 : my_response_size - chunk_send_ack_response_header_size;
715 : } else {
716 3 : libspdm_copy_mem(large_buffer, large_buffer_size, my_response, my_response_size);
717 :
718 3 : get_info->large_message = large_buffer;
719 3 : get_info->large_message_size = my_response_size;
720 : }
721 :
722 3 : status = libspdm_generate_extended_error_response(context,
723 : SPDM_ERROR_CODE_LARGE_RESPONSE, 0,
724 : sizeof(uint8_t),
725 3 : &get_info->chunk_handle,
726 : &my_response_size, my_response);
727 : #else
728 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR,
729 : "Warning: Could not save chunk. Scratch buffer too small.\n"));
730 :
731 : status = libspdm_generate_extended_error_response(context,
732 : SPDM_ERROR_CODE_LARGE_RESPONSE,
733 : 0, 0, NULL,
734 : &my_response_size, my_response);
735 : #endif /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP */
736 :
737 3 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
738 0 : return status;
739 : }
740 : }
741 : }
742 :
743 : /* if return the status: Responder drop the response
744 : * just ignore this message
745 : * return UNSUPPORTED and clear response_size to continue the dispatch without send response.*/
746 4 : if ((my_response_size == 0) && (status == LIBSPDM_STATUS_UNSUPPORTED_CAP)) {
747 0 : *response_size = 0;
748 0 : return LIBSPDM_STATUS_UNSUPPORTED_CAP;
749 : }
750 :
751 4 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
752 0 : status = libspdm_generate_error_response(
753 : context, SPDM_ERROR_CODE_UNSUPPORTED_REQUEST,
754 0 : spdm_request->request_response_code, &my_response_size, my_response);
755 0 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
756 0 : return status;
757 : }
758 : }
759 :
760 4 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "SpdmSendResponse[%x]: msg %s(0x%x), size (0x%zx): \n",
761 : (session_id != NULL) ? *session_id : 0,
762 : libspdm_get_code_str(spdm_response->request_response_code),
763 : spdm_response->request_response_code,
764 : my_response_size));
765 4 : LIBSPDM_INTERNAL_DUMP_HEX(my_response, my_response_size);
766 :
767 4 : status = context->transport_encode_message(
768 : context, session_id, is_app_message, false,
769 : my_response_size, my_response, response_size, response);
770 4 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
771 0 : if ((session_id != NULL) &&
772 0 : ((status == LIBSPDM_STATUS_SEQUENCE_NUMBER_OVERFLOW) ||
773 : (status == LIBSPDM_STATUS_CRYPTO_ERROR))) {
774 0 : libspdm_free_session_id(context, *session_id);
775 : }
776 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "transport_encode_message : %xu\n", status));
777 0 : return status;
778 : }
779 :
780 4 : request_response_code = spdm_response->request_response_code;
781 : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
782 4 : switch (request_response_code) {
783 0 : case SPDM_CHUNK_SEND_ACK:
784 0 : if (my_response_size > chunk_send_ack_response_header_size) {
785 0 : request_response_code =
786 0 : ((spdm_message_header_t *)(my_response + chunk_send_ack_response_header_size))
787 : ->request_response_code;
788 : }
789 0 : break;
790 0 : case SPDM_CHUNK_RESPONSE:
791 0 : chunk_rsp = (spdm_chunk_response_response_t *)my_response;
792 0 : chunk_ptr = (uint8_t *)(((uint32_t *)(chunk_rsp + 1)) + 1);
793 0 : if (chunk_rsp->chunk_seq_no == 0) {
794 0 : request_response_code = ((spdm_message_header_t *)chunk_ptr)->request_response_code;
795 : }
796 0 : break;
797 4 : default:
798 4 : break;
799 : }
800 : #endif /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP */
801 :
802 4 : if (session_id != NULL) {
803 0 : switch (request_response_code) {
804 0 : case SPDM_FINISH_RSP:
805 0 : if (!libspdm_is_capabilities_flag_supported(
806 : context, false,
807 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP,
808 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP)) {
809 0 : libspdm_set_session_state(
810 : context, *session_id,
811 : LIBSPDM_SESSION_STATE_ESTABLISHED);
812 : }
813 0 : break;
814 0 : case SPDM_PSK_FINISH_RSP:
815 0 : libspdm_set_session_state(context, *session_id, LIBSPDM_SESSION_STATE_ESTABLISHED);
816 0 : break;
817 0 : case SPDM_END_SESSION_ACK:
818 : #if LIBSPDM_ENABLE_CAPABILITY_HBEAT_CAP
819 0 : if (libspdm_is_capabilities_flag_supported(
820 : context, false,
821 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HBEAT_CAP,
822 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HBEAT_CAP)) {
823 0 : result = libspdm_stop_watchdog(*session_id);
824 0 : if (!result) {
825 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR, "libspdm_stop_watchdog error\n"));
826 : /* No need to return error for internal watchdog error. */
827 : }
828 : }
829 : #endif /* LIBSPDM_ENABLE_CAPABILITY_HBEAT_CAP */
830 0 : libspdm_terminate_session(context, *session_id);
831 0 : break;
832 0 : default:
833 : #if LIBSPDM_ENABLE_CAPABILITY_HBEAT_CAP
834 0 : if (libspdm_is_capabilities_flag_supported(
835 : context, false,
836 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HBEAT_CAP,
837 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HBEAT_CAP)) {
838 : /* reset watchdog in any session messages. */
839 0 : result = libspdm_reset_watchdog(*session_id);
840 0 : if (!result) {
841 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR, "libspdm_reset_watchdog error\n"));
842 : /* No need to return error for internal watchdog error. */
843 : }
844 : }
845 : #endif /* LIBSPDM_ENABLE_CAPABILITY_HBEAT_CAP */
846 0 : break;
847 : }
848 : } else {
849 4 : switch (request_response_code) {
850 0 : case SPDM_FINISH_RSP:
851 0 : if (libspdm_is_capabilities_flag_supported(
852 : context, false,
853 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP,
854 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP)) {
855 0 : libspdm_set_session_state(
856 : context,
857 : context->latest_session_id,
858 : LIBSPDM_SESSION_STATE_ESTABLISHED);
859 : }
860 0 : break;
861 4 : default:
862 : /* No session state update needed */
863 4 : break;
864 : }
865 : }
866 :
867 4 : return LIBSPDM_STATUS_SUCCESS;
868 : }
869 :
870 0 : void libspdm_register_get_response_func(void *context, libspdm_get_response_func get_response_func)
871 : {
872 : libspdm_context_t *spdm_context;
873 :
874 0 : spdm_context = context;
875 0 : spdm_context->get_response_func = (void *)get_response_func;
876 0 : }
877 :
878 0 : void libspdm_register_session_state_callback_func(
879 : void *spdm_context,
880 : libspdm_session_state_callback_func spdm_session_state_callback)
881 : {
882 : libspdm_context_t *context;
883 :
884 0 : LIBSPDM_ASSERT(spdm_context != NULL);
885 :
886 0 : context = spdm_context;
887 :
888 0 : context->spdm_session_state_callback = (void *)spdm_session_state_callback;
889 0 : }
890 :
891 0 : void libspdm_register_connection_state_callback_func(
892 : void *spdm_context,
893 : libspdm_connection_state_callback_func spdm_connection_state_callback)
894 : {
895 : libspdm_context_t *context;
896 :
897 0 : LIBSPDM_ASSERT(spdm_context != NULL);
898 :
899 0 : context = spdm_context;
900 0 : context->spdm_connection_state_callback = (void *)spdm_connection_state_callback;
901 0 : }
902 :
903 0 : void libspdm_register_key_update_callback_func(
904 : void *spdm_context, libspdm_key_update_callback_func spdm_key_update_callback)
905 : {
906 : libspdm_context_t *context;
907 :
908 0 : LIBSPDM_ASSERT(spdm_context != NULL);
909 :
910 0 : context = spdm_context;
911 0 : context->spdm_key_update_callback = (void *)spdm_key_update_callback;
912 0 : }
913 :
914 : #if (LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP) && (LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP) && \
915 : (LIBSPDM_SEND_GET_CERTIFICATE_SUPPORT)
916 109 : void libspdm_register_cert_chain_buffer(
917 : void *spdm_context, void *cert_chain_buffer, size_t cert_chain_buffer_max_size)
918 : {
919 : libspdm_context_t *context;
920 :
921 109 : LIBSPDM_ASSERT(spdm_context != NULL);
922 :
923 109 : context = spdm_context;
924 109 : context->mut_auth_cert_chain_buffer = cert_chain_buffer;
925 109 : context->mut_auth_cert_chain_buffer_max_size = cert_chain_buffer_max_size;
926 109 : context->mut_auth_cert_chain_buffer_size = 0;
927 109 : }
928 : #endif
|