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