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 0 : context->last_spdm_request_size = decoded_message_size;
283 0 : libspdm_copy_mem (context->last_spdm_request,
284 0 : libspdm_get_scratch_buffer_last_spdm_request_capacity(context),
285 : decoded_message_ptr,
286 : decoded_message_size);
287 :
288 0 : if (!(*is_app_message)) {
289 : /* Check for minimal SPDM message size. */
290 0 : if (context->last_spdm_request_size < sizeof(spdm_message_header_t)) {
291 0 : return LIBSPDM_STATUS_UNSUPPORTED_CAP;
292 : }
293 : }
294 :
295 0 : *session_id = message_session_id;
296 :
297 0 : if (message_session_id != NULL) {
298 0 : session_info = libspdm_get_session_info_via_session_id(context, *message_session_id);
299 0 : if (session_info == NULL) {
300 0 : return LIBSPDM_STATUS_UNSUPPORTED_CAP;
301 : }
302 0 : context->last_spdm_request_session_id = *message_session_id;
303 0 : context->last_spdm_request_session_id_valid = true;
304 : }
305 :
306 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "SpdmReceiveRequest[%x] msg %s(0x%x), size (0x%zx): \n",
307 : (message_session_id != NULL) ? *message_session_id : 0,
308 : libspdm_get_code_str(((spdm_message_header_t *)context->last_spdm_request)->
309 : request_response_code),
310 : ((spdm_message_header_t *)context->last_spdm_request)->request_response_code,
311 : context->last_spdm_request_size));
312 0 : LIBSPDM_INTERNAL_DUMP_HEX((uint8_t *)context->last_spdm_request,
313 : context->last_spdm_request_size);
314 :
315 0 : return LIBSPDM_STATUS_SUCCESS;
316 : }
317 :
318 : /**
319 : * Notify the session state to a session APP.
320 : *
321 : * @param spdm_context A pointer to the SPDM context.
322 : * @param session_id The session_id of a session.
323 : * @param session_state The state of a session.
324 : **/
325 22 : static void libspdm_trigger_session_state_callback(libspdm_context_t *spdm_context,
326 : uint32_t session_id,
327 : libspdm_session_state_t session_state)
328 : {
329 22 : if (spdm_context->spdm_session_state_callback != NULL) {
330 0 : ((libspdm_session_state_callback_func)
331 0 : spdm_context->spdm_session_state_callback)(spdm_context, session_id, session_state);
332 : }
333 22 : }
334 :
335 : /**
336 : * Set session_state to an SPDM secured message context and trigger callback.
337 : *
338 : * @param spdm_context A pointer to the SPDM context.
339 : * @param session_id Indicate the SPDM session ID.
340 : * @param session_state Indicate the SPDM session state.
341 : */
342 22 : void libspdm_set_session_state(libspdm_context_t *spdm_context,
343 : uint32_t session_id,
344 : libspdm_session_state_t session_state)
345 : {
346 : libspdm_session_info_t *session_info;
347 : libspdm_session_state_t old_session_state;
348 :
349 22 : session_info = libspdm_get_session_info_via_session_id(spdm_context, session_id);
350 22 : if (session_info == NULL) {
351 0 : LIBSPDM_ASSERT(false);
352 0 : return;
353 : }
354 :
355 22 : old_session_state = libspdm_secured_message_get_session_state(
356 : session_info->secured_message_context);
357 22 : if (old_session_state != session_state) {
358 22 : libspdm_secured_message_set_session_state(
359 : session_info->secured_message_context, session_state);
360 22 : libspdm_trigger_session_state_callback(
361 : spdm_context, session_info->session_id, session_state);
362 : }
363 : }
364 :
365 : /**
366 : * This function allows the consumer to terminate a session.
367 : * For example, it can be used when the heartbeat period is over.
368 : *
369 : * @param spdm_context A pointer to the SPDM context.
370 : * @param session_id session_id of the session to be terminated.
371 : *
372 : * @retval LIBSPDM_STATUS_SUCCESS Success
373 : * @retval LIBSPDM_STATUS_INVALID_PARAMETER session_id is invalid.
374 : **/
375 0 : libspdm_return_t libspdm_terminate_session(
376 : void *spdm_context, uint32_t session_id)
377 : {
378 : libspdm_session_info_t *session_info;
379 :
380 0 : session_info = libspdm_get_session_info_via_session_id(spdm_context, session_id);
381 0 : if (session_info == NULL) {
382 0 : return LIBSPDM_STATUS_INVALID_PARAMETER;
383 : }
384 :
385 0 : libspdm_set_session_state(spdm_context, session_id, LIBSPDM_SESSION_STATE_NOT_STARTED);
386 0 : libspdm_free_session_id(spdm_context, session_id);
387 0 : return LIBSPDM_STATUS_SUCCESS;
388 : }
389 :
390 : /**
391 : * Notify the connection state to an SPDM context register.
392 : *
393 : * @param spdm_context A pointer to the SPDM context.
394 : * @param connection_state Indicate the SPDM connection state.
395 : **/
396 81 : static void libspdm_trigger_connection_state_callback(libspdm_context_t *spdm_context,
397 : const libspdm_connection_state_t
398 : connection_state)
399 : {
400 81 : if (spdm_context->spdm_connection_state_callback != NULL) {
401 0 : ((libspdm_connection_state_callback_func)
402 0 : spdm_context->spdm_connection_state_callback)(spdm_context, connection_state);
403 : }
404 81 : }
405 :
406 : /**
407 : * Set connection_state to an SPDM context and trigger callback.
408 : *
409 : * @param spdm_context A pointer to the SPDM context.
410 : * @param connection_state Indicate the SPDM connection state.
411 : */
412 83 : void libspdm_set_connection_state(libspdm_context_t *spdm_context,
413 : libspdm_connection_state_t connection_state)
414 : {
415 83 : if (spdm_context->connection_info.connection_state != connection_state) {
416 81 : spdm_context->connection_info.connection_state = connection_state;
417 81 : libspdm_trigger_connection_state_callback(spdm_context, connection_state);
418 : }
419 83 : }
420 :
421 15 : void libspdm_trigger_key_update_callback(void *spdm_context, uint32_t session_id,
422 : libspdm_key_update_operation_t key_update_op,
423 : libspdm_key_update_action_t key_update_action)
424 : {
425 : libspdm_context_t *context;
426 :
427 15 : context = spdm_context;
428 15 : if (context->spdm_key_update_callback != NULL) {
429 0 : ((libspdm_key_update_callback_func)
430 0 : context->spdm_key_update_callback)(context, session_id, key_update_op, key_update_action);
431 : }
432 15 : }
433 :
434 4 : libspdm_return_t libspdm_build_response(void *spdm_context, const uint32_t *session_id,
435 : bool is_app_message,
436 : size_t *response_size,
437 : void **response)
438 : {
439 : libspdm_context_t *context;
440 : uint8_t *my_response;
441 : size_t my_response_size;
442 : libspdm_return_t status;
443 : libspdm_get_spdm_response_func get_response_func;
444 : libspdm_session_info_t *session_info;
445 : spdm_message_header_t *spdm_request;
446 : spdm_message_header_t *spdm_response;
447 : size_t transport_header_size;
448 : uint8_t *scratch_buffer;
449 : size_t scratch_buffer_size;
450 : uint8_t request_response_code;
451 : uint32_t actual_size;
452 :
453 : #if LIBSPDM_ENABLE_CAPABILITY_HBEAT_CAP
454 : bool result;
455 : #endif /* LIBSPDM_ENABLE_CAPABILITY_HBEAT_CAP */
456 :
457 : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
458 : uint8_t *large_buffer;
459 : size_t large_buffer_size;
460 : libspdm_chunk_info_t *get_info;
461 : spdm_chunk_response_response_t *chunk_rsp;
462 : uint8_t *chunk_ptr;
463 : size_t chunk_send_ack_response_header_size;
464 : #endif /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP */
465 :
466 4 : context = spdm_context;
467 4 : status = LIBSPDM_STATUS_UNSUPPORTED_CAP;
468 :
469 : /* For secure message, setup my_response to scratch buffer
470 : * For non-secure message, setup my_response to sender buffer*/
471 4 : transport_header_size = context->local_context.capability.transport_header_size;
472 4 : if (session_id != NULL) {
473 0 : libspdm_get_scratch_buffer (context, (void **)&scratch_buffer, &scratch_buffer_size);
474 : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
475 0 : my_response = scratch_buffer + libspdm_get_scratch_buffer_secure_message_offset(context) +
476 : transport_header_size;
477 0 : my_response_size = libspdm_get_scratch_buffer_secure_message_capacity(context) -
478 0 : transport_header_size -
479 0 : context->local_context.capability.transport_tail_size;
480 : #else
481 : my_response = scratch_buffer + transport_header_size;
482 : my_response_size = scratch_buffer_size - transport_header_size -
483 : context->local_context.capability.transport_tail_size;
484 : #endif /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP */
485 : } else {
486 4 : my_response = (uint8_t *)*response + transport_header_size;
487 4 : my_response_size = *response_size - transport_header_size -
488 4 : context->local_context.capability.transport_tail_size;
489 : }
490 4 : libspdm_zero_mem(my_response, my_response_size);
491 :
492 4 : spdm_response = (void *)my_response;
493 :
494 4 : if (context->last_spdm_error.error_code != 0) {
495 : /* Error in libspdm_process_request(), and we need send error message directly. */
496 0 : switch (context->last_spdm_error.error_code) {
497 0 : case SPDM_ERROR_CODE_DECRYPT_ERROR:
498 : /* session ID is valid. Use it to encrypt the error message.*/
499 0 : if ((context->handle_error_return_policy &
500 : LIBSPDM_DATA_HANDLE_ERROR_RETURN_POLICY_DROP_ON_DECRYPT_ERROR) == 0) {
501 0 : status = libspdm_generate_error_response(
502 : context, SPDM_ERROR_CODE_DECRYPT_ERROR, 0,
503 : &my_response_size, my_response);
504 : } else {
505 : /**
506 : * just ignore this message
507 : * return UNSUPPORTED and clear response_size to continue the dispatch without send response
508 : **/
509 0 : *response_size = 0;
510 0 : status = LIBSPDM_STATUS_UNSUPPORTED_CAP;
511 : }
512 0 : break;
513 0 : case SPDM_ERROR_CODE_INVALID_SESSION:
514 : /**
515 : * don't use session ID, because we dont know which right session ID should be used.
516 : * just ignore this message
517 : * return UNSUPPORTED and clear response_size to continue the dispatch without send response
518 : **/
519 0 : *response_size = 0;
520 0 : status = LIBSPDM_STATUS_UNSUPPORTED_CAP;
521 0 : break;
522 0 : default:
523 0 : LIBSPDM_ASSERT(false);
524 0 : status = LIBSPDM_STATUS_UNSUPPORTED_CAP;
525 : }
526 :
527 0 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
528 0 : if ((session_id != NULL) &&
529 0 : (context->last_spdm_error.error_code == SPDM_ERROR_CODE_DECRYPT_ERROR)) {
530 0 : libspdm_free_session_id(context, *session_id);
531 : }
532 0 : return status;
533 : }
534 :
535 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "SpdmSendResponse[%x]: msg %s(0x%x), size (0x%zx): \n",
536 : (session_id != NULL) ? *session_id : 0,
537 : libspdm_get_code_str(spdm_response->request_response_code),
538 : spdm_response->request_response_code, my_response_size));
539 0 : LIBSPDM_INTERNAL_DUMP_HEX(my_response, my_response_size);
540 :
541 0 : status = context->transport_encode_message(
542 : context, session_id, false, false,
543 : my_response_size, my_response, response_size, response);
544 0 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
545 0 : if ((session_id != NULL) &&
546 0 : ((status == LIBSPDM_STATUS_SEQUENCE_NUMBER_OVERFLOW) ||
547 : (status == LIBSPDM_STATUS_CRYPTO_ERROR))) {
548 0 : libspdm_free_session_id(context, *session_id);
549 : }
550 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "transport_encode_message : %xu\n", status));
551 0 : return status;
552 : }
553 :
554 0 : if ((session_id != NULL) &&
555 0 : (context->last_spdm_error.error_code == SPDM_ERROR_CODE_DECRYPT_ERROR)) {
556 0 : libspdm_free_session_id(context, *session_id);
557 : }
558 :
559 0 : libspdm_zero_mem(&context->last_spdm_error, sizeof(context->last_spdm_error));
560 0 : return LIBSPDM_STATUS_SUCCESS;
561 : }
562 :
563 4 : if (session_id != NULL) {
564 0 : session_info = libspdm_get_session_info_via_session_id(context, *session_id);
565 0 : if (session_info == NULL) {
566 0 : LIBSPDM_ASSERT(false);
567 0 : return LIBSPDM_STATUS_UNSUPPORTED_CAP;
568 : }
569 : }
570 :
571 4 : if (*response == NULL) {
572 0 : return LIBSPDM_STATUS_INVALID_PARAMETER;
573 : }
574 4 : if ((response_size == NULL) || (*response_size == 0)) {
575 0 : return LIBSPDM_STATUS_INVALID_PARAMETER;
576 : }
577 :
578 4 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "SpdmSendResponse[%x] ...\n",
579 : (session_id != NULL) ? *session_id : 0));
580 :
581 4 : spdm_request = (void *)context->last_spdm_request;
582 4 : if (context->last_spdm_request_size == 0) {
583 0 : return LIBSPDM_STATUS_INVALID_STATE_LOCAL;
584 : }
585 :
586 4 : get_response_func = NULL;
587 4 : if (!is_app_message) {
588 4 : get_response_func = libspdm_get_response_func_via_last_request(context);
589 :
590 : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
591 : /* If responder is expecting chunk_get or chunk_send requests
592 : * and gets other requests instead, drop out of chunking mode */
593 4 : if (context->chunk_context.get.chunk_in_use
594 0 : && get_response_func != libspdm_get_response_chunk_get) {
595 :
596 0 : context->chunk_context.get.chunk_in_use = false;
597 0 : context->chunk_context.get.chunk_handle++; /* implicit wrap - around to 0. */
598 0 : context->chunk_context.get.chunk_seq_no = 0;
599 :
600 0 : context->chunk_context.get.large_message = NULL;
601 0 : context->chunk_context.get.large_message_size = 0;
602 0 : context->chunk_context.get.chunk_bytes_transferred = 0;
603 : }
604 4 : if (context->chunk_context.send.chunk_in_use
605 0 : && get_response_func != libspdm_get_response_chunk_send) {
606 :
607 0 : context->chunk_context.send.chunk_in_use = false;
608 0 : context->chunk_context.send.chunk_handle = 0;
609 0 : context->chunk_context.send.chunk_seq_no = 0;
610 :
611 0 : context->chunk_context.send.large_message = NULL;
612 0 : context->chunk_context.send.large_message_size = 0;
613 0 : context->chunk_context.send.chunk_bytes_transferred = 0;
614 : }
615 : #endif /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP */
616 :
617 4 : if (get_response_func != NULL) {
618 4 : status = get_response_func(
619 : context,
620 : context->last_spdm_request_size,
621 4 : context->last_spdm_request,
622 : &my_response_size, my_response);
623 : }
624 : }
625 4 : if (is_app_message || (get_response_func == NULL)) {
626 0 : if (context->get_response_func != NULL) {
627 0 : status = ((libspdm_get_response_func) context->get_response_func)(
628 : context, session_id, is_app_message,
629 : context->last_spdm_request_size,
630 0 : context->last_spdm_request,
631 : &my_response_size, my_response);
632 : } else {
633 0 : status = LIBSPDM_STATUS_UNSUPPORTED_CAP;
634 : }
635 : }
636 :
637 : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
638 4 : if (libspdm_get_connection_version(context) < SPDM_MESSAGE_VERSION_14) {
639 4 : chunk_send_ack_response_header_size = sizeof(spdm_chunk_send_ack_response_t);
640 : } else {
641 0 : chunk_send_ack_response_header_size = sizeof(spdm_chunk_send_ack_response_14_t);
642 : }
643 : #endif /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP */
644 :
645 4 : if (status == LIBSPDM_STATUS_SUCCESS) {
646 4 : LIBSPDM_ASSERT (my_response_size <= context->local_context.capability.max_spdm_msg_size);
647 : /* large SPDM message is the SPDM message whose size is greater than the DataTransferSize of the receiving
648 : * SPDM endpoint or greater than the transmit buffer size of the sending SPDM endpoint */
649 4 : if ((context->connection_info.capability.max_spdm_msg_size != 0) &&
650 4 : (my_response_size > context->connection_info.capability.max_spdm_msg_size)) {
651 1 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR, "my_response_size > req max_spdm_msg_size\n"));
652 1 : actual_size = (uint32_t)my_response_size;
653 1 : status = libspdm_generate_extended_error_response(context,
654 : SPDM_ERROR_CODE_RESPONSE_TOO_LARGE,
655 : 0,
656 : sizeof(uint32_t),
657 : (uint8_t *)&actual_size,
658 : &my_response_size, my_response);
659 3 : } else if ((((context->connection_info.capability.data_transfer_size != 0) &&
660 3 : (my_response_size > context->connection_info.capability.data_transfer_size)) ||
661 2 : ((context->local_context.capability.sender_data_transfer_size != 0) &&
662 2 : (my_response_size >
663 5 : context->local_context.capability.sender_data_transfer_size))) &&
664 3 : libspdm_is_capabilities_flag_supported(
665 : context, false, SPDM_GET_CAPABILITIES_REQUEST_FLAGS_CHUNK_CAP,
666 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CHUNK_CAP)) {
667 : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
668 :
669 3 : get_info = &context->chunk_context.get;
670 :
671 : /* Saving multiple large responses is not an expected use case.
672 : * Therefore, if the requester did not perform chunk_get requests for
673 : * previous large responses, they will be lost. */
674 3 : if (get_info->chunk_in_use) {
675 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR,
676 : "Warning: Overwriting previous unrequested chunk_get info.\n"));
677 : }
678 :
679 3 : libspdm_get_scratch_buffer(context, (void **)&scratch_buffer, &scratch_buffer_size);
680 :
681 : /* The first section of the scratch
682 : * buffer may be used for other purposes. Use only after that section. */
683 6 : large_buffer = (uint8_t *)scratch_buffer +
684 3 : libspdm_get_scratch_buffer_large_message_offset(spdm_context);
685 3 : large_buffer_size = libspdm_get_scratch_buffer_large_message_capacity(spdm_context);
686 :
687 3 : get_info->chunk_in_use = true;
688 : /* Increment chunk_handle here as opposed to end of chunk_get handler
689 : * in case requester never issues chunk_get. */
690 3 : get_info->chunk_handle++;
691 3 : get_info->chunk_seq_no = 0;
692 3 : get_info->chunk_bytes_transferred = 0;
693 :
694 3 : libspdm_zero_mem(large_buffer, large_buffer_size);
695 :
696 : /* It's possible that the large response that was to be sent to the requester was
697 : * a CHUNK_SEND_ACK + non-chunk response. In this case, to prevent chunking within
698 : * chunking, only send back the actual response, by saving only non-chunk portion
699 : * in the scratch buffer, used to respond to the next CHUNK_GET request. */
700 3 : if (((spdm_message_header_t *)my_response)
701 3 : ->request_response_code == SPDM_CHUNK_SEND_ACK) {
702 0 : libspdm_copy_mem(large_buffer, large_buffer_size,
703 0 : my_response + chunk_send_ack_response_header_size,
704 : my_response_size - chunk_send_ack_response_header_size);
705 0 : get_info->large_message = large_buffer;
706 0 : get_info->large_message_size =
707 0 : my_response_size - chunk_send_ack_response_header_size;
708 : } else {
709 3 : libspdm_copy_mem(large_buffer, large_buffer_size, my_response, my_response_size);
710 :
711 3 : get_info->large_message = large_buffer;
712 3 : get_info->large_message_size = my_response_size;
713 : }
714 :
715 3 : status = libspdm_generate_extended_error_response(context,
716 : SPDM_ERROR_CODE_LARGE_RESPONSE, 0,
717 : sizeof(uint8_t),
718 3 : &get_info->chunk_handle,
719 : &my_response_size, my_response);
720 : #else
721 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR,
722 : "Warning: Could not save chunk. Scratch buffer too small.\n"));
723 :
724 : status = libspdm_generate_extended_error_response(context,
725 : SPDM_ERROR_CODE_LARGE_RESPONSE,
726 : 0, 0, NULL,
727 : &my_response_size, my_response);
728 : #endif /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP */
729 :
730 3 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
731 0 : return status;
732 : }
733 : }
734 : }
735 :
736 : /* if return the status: Responder drop the response
737 : * just ignore this message
738 : * return UNSUPPORTED and clear response_size to continue the dispatch without send response.*/
739 4 : if ((my_response_size == 0) && (status == LIBSPDM_STATUS_UNSUPPORTED_CAP)) {
740 0 : *response_size = 0;
741 0 : return LIBSPDM_STATUS_UNSUPPORTED_CAP;
742 : }
743 :
744 4 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
745 0 : status = libspdm_generate_error_response(
746 : context, SPDM_ERROR_CODE_UNSUPPORTED_REQUEST,
747 0 : spdm_request->request_response_code, &my_response_size, my_response);
748 0 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
749 0 : return status;
750 : }
751 : }
752 :
753 4 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "SpdmSendResponse[%x]: msg %s(0x%x), size (0x%zx): \n",
754 : (session_id != NULL) ? *session_id : 0,
755 : libspdm_get_code_str(spdm_response->request_response_code),
756 : spdm_response->request_response_code,
757 : my_response_size));
758 4 : LIBSPDM_INTERNAL_DUMP_HEX(my_response, my_response_size);
759 :
760 4 : status = context->transport_encode_message(
761 : context, session_id, is_app_message, false,
762 : my_response_size, my_response, response_size, response);
763 4 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
764 0 : if ((session_id != NULL) &&
765 0 : ((status == LIBSPDM_STATUS_SEQUENCE_NUMBER_OVERFLOW) ||
766 : (status == LIBSPDM_STATUS_CRYPTO_ERROR))) {
767 0 : libspdm_free_session_id(context, *session_id);
768 : }
769 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "transport_encode_message : %xu\n", status));
770 0 : return status;
771 : }
772 :
773 4 : request_response_code = spdm_response->request_response_code;
774 : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
775 4 : switch (request_response_code) {
776 0 : case SPDM_CHUNK_SEND_ACK:
777 0 : if (my_response_size > chunk_send_ack_response_header_size) {
778 0 : request_response_code =
779 0 : ((spdm_message_header_t *)(my_response + chunk_send_ack_response_header_size))
780 : ->request_response_code;
781 : }
782 0 : break;
783 0 : case SPDM_CHUNK_RESPONSE:
784 0 : chunk_rsp = (spdm_chunk_response_response_t *)my_response;
785 0 : chunk_ptr = (uint8_t *)(((uint32_t *)(chunk_rsp + 1)) + 1);
786 0 : if (chunk_rsp->chunk_seq_no == 0) {
787 0 : request_response_code = ((spdm_message_header_t *)chunk_ptr)->request_response_code;
788 : }
789 0 : break;
790 4 : default:
791 4 : break;
792 : }
793 : #endif /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP */
794 :
795 4 : if (session_id != NULL) {
796 0 : switch (request_response_code) {
797 0 : case SPDM_FINISH_RSP:
798 0 : if (!libspdm_is_capabilities_flag_supported(
799 : context, false,
800 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP,
801 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP)) {
802 0 : libspdm_set_session_state(
803 : context, *session_id,
804 : LIBSPDM_SESSION_STATE_ESTABLISHED);
805 : }
806 0 : break;
807 0 : case SPDM_PSK_FINISH_RSP:
808 0 : libspdm_set_session_state(context, *session_id, LIBSPDM_SESSION_STATE_ESTABLISHED);
809 0 : break;
810 0 : case SPDM_END_SESSION_ACK:
811 : #if LIBSPDM_ENABLE_CAPABILITY_HBEAT_CAP
812 0 : if (libspdm_is_capabilities_flag_supported(
813 : context, false,
814 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HBEAT_CAP,
815 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HBEAT_CAP)) {
816 0 : result = libspdm_stop_watchdog(*session_id);
817 0 : if (!result) {
818 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR, "libspdm_stop_watchdog error\n"));
819 : /* No need to return error for internal watchdog error. */
820 : }
821 : }
822 : #endif /* LIBSPDM_ENABLE_CAPABILITY_HBEAT_CAP */
823 0 : libspdm_terminate_session(context, *session_id);
824 0 : break;
825 0 : default:
826 : #if LIBSPDM_ENABLE_CAPABILITY_HBEAT_CAP
827 0 : if (libspdm_is_capabilities_flag_supported(
828 : context, false,
829 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HBEAT_CAP,
830 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HBEAT_CAP)) {
831 : /* reset watchdog in any session messages. */
832 0 : result = libspdm_reset_watchdog(*session_id);
833 0 : if (!result) {
834 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR, "libspdm_reset_watchdog error\n"));
835 : /* No need to return error for internal watchdog error. */
836 : }
837 : }
838 : #endif /* LIBSPDM_ENABLE_CAPABILITY_HBEAT_CAP */
839 0 : break;
840 : }
841 : } else {
842 4 : switch (request_response_code) {
843 0 : case SPDM_FINISH_RSP:
844 0 : if (libspdm_is_capabilities_flag_supported(
845 : context, false,
846 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP,
847 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP)) {
848 0 : libspdm_set_session_state(
849 : context,
850 : context->latest_session_id,
851 : LIBSPDM_SESSION_STATE_ESTABLISHED);
852 : }
853 0 : break;
854 4 : default:
855 : /* No session state update needed */
856 4 : break;
857 : }
858 : }
859 :
860 4 : return LIBSPDM_STATUS_SUCCESS;
861 : }
862 :
863 0 : void libspdm_register_get_response_func(void *context, libspdm_get_response_func get_response_func)
864 : {
865 : libspdm_context_t *spdm_context;
866 :
867 0 : spdm_context = context;
868 0 : spdm_context->get_response_func = (void *)get_response_func;
869 0 : }
870 :
871 0 : void libspdm_register_session_state_callback_func(
872 : void *spdm_context,
873 : libspdm_session_state_callback_func spdm_session_state_callback)
874 : {
875 : libspdm_context_t *context;
876 :
877 0 : LIBSPDM_ASSERT(spdm_context != NULL);
878 :
879 0 : context = spdm_context;
880 :
881 0 : context->spdm_session_state_callback = (void *)spdm_session_state_callback;
882 0 : }
883 :
884 0 : void libspdm_register_connection_state_callback_func(
885 : void *spdm_context,
886 : libspdm_connection_state_callback_func spdm_connection_state_callback)
887 : {
888 : libspdm_context_t *context;
889 :
890 0 : LIBSPDM_ASSERT(spdm_context != NULL);
891 :
892 0 : context = spdm_context;
893 0 : context->spdm_connection_state_callback = (void *)spdm_connection_state_callback;
894 0 : }
895 :
896 0 : void libspdm_register_key_update_callback_func(
897 : void *spdm_context, libspdm_key_update_callback_func spdm_key_update_callback)
898 : {
899 : libspdm_context_t *context;
900 :
901 0 : LIBSPDM_ASSERT(spdm_context != NULL);
902 :
903 0 : context = spdm_context;
904 0 : context->spdm_key_update_callback = (void *)spdm_key_update_callback;
905 0 : }
906 :
907 : #if (LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP) && (LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP) && \
908 : (LIBSPDM_SEND_GET_CERTIFICATE_SUPPORT)
909 109 : void libspdm_register_cert_chain_buffer(
910 : void *spdm_context, void *cert_chain_buffer, size_t cert_chain_buffer_max_size)
911 : {
912 : libspdm_context_t *context;
913 :
914 109 : LIBSPDM_ASSERT(spdm_context != NULL);
915 :
916 109 : context = spdm_context;
917 109 : context->mut_auth_cert_chain_buffer = cert_chain_buffer;
918 109 : context->mut_auth_cert_chain_buffer_max_size = cert_chain_buffer_max_size;
919 109 : context->mut_auth_cert_chain_buffer_size = 0;
920 109 : }
921 : #endif
|