Branch data Line data Source code
1 : : /**
2 : : * Copyright Notice:
3 : : * Copyright 2021-2026 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 : 132 : libspdm_get_spdm_response_func libspdm_get_response_func_via_request_code(uint8_t request_code)
11 : : {
12 [ + + + + : 132 : switch (request_code) {
+ + + - +
+ + + - +
+ - - - -
- - - - +
- - - - +
+ ]
13 : 24 : case SPDM_GET_VERSION: return libspdm_get_response_version;
14 : 16 : case SPDM_GET_CAPABILITIES: return libspdm_get_response_capabilities;
15 : 8 : case SPDM_NEGOTIATE_ALGORITHMS: return libspdm_get_response_algorithms;
16 : :
17 : : #if LIBSPDM_ENABLE_CAPABILITY_CERT_CAP
18 : 8 : case SPDM_GET_DIGESTS: return libspdm_get_response_digests;
19 : 2 : case SPDM_GET_CERTIFICATE: return libspdm_get_response_certificate;
20 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_CERT_CAP */
21 : :
22 : : #if LIBSPDM_ENABLE_CAPABILITY_CHAL_CAP
23 : 2 : case SPDM_CHALLENGE: return libspdm_get_response_challenge_auth;
24 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_CHAL_CAP*/
25 : :
26 : : #if LIBSPDM_ENABLE_CAPABILITY_MEAS_CAP
27 : 6 : case SPDM_GET_MEASUREMENTS: return libspdm_get_response_measurements;
28 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_MEAS_CAP*/
29 : :
30 : : #if LIBSPDM_ENABLE_CAPABILITY_MEL_CAP
31 : 0 : case SPDM_GET_MEASUREMENT_EXTENSION_LOG: return libspdm_get_response_measurement_extension_log;
32 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_MEL_CAP */
33 : :
34 : : #if LIBSPDM_ENABLE_CAPABILITY_KEY_EX_CAP
35 : 2 : case SPDM_KEY_EXCHANGE: return libspdm_get_response_key_exchange;
36 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_KEY_EX_CAP*/
37 : :
38 : : #if LIBSPDM_ENABLE_CAPABILITY_PSK_CAP
39 : 2 : case SPDM_PSK_EXCHANGE: return libspdm_get_response_psk_exchange;
40 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_PSK_CAP*/
41 : :
42 : : #if LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP
43 : 14 : case SPDM_GET_ENCAPSULATED_REQUEST: return libspdm_get_response_encapsulated_request;
44 : 6 : case SPDM_DELIVER_ENCAPSULATED_RESPONSE: return libspdm_get_response_encapsulated_response_ack;
45 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP */
46 : :
47 : : #if LIBSPDM_RESPOND_IF_READY_SUPPORT
48 : 0 : case SPDM_RESPOND_IF_READY: return libspdm_get_response_respond_if_ready;
49 : : #endif /* LIBSPDM_RESPOND_IF_READY_SUPPORT */
50 : :
51 : : #if LIBSPDM_ENABLE_CAPABILITY_KEY_EX_CAP
52 : 16 : case SPDM_FINISH: return libspdm_get_response_finish;
53 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_KEY_EX_CAP*/
54 : :
55 : : #if LIBSPDM_ENABLE_CAPABILITY_PSK_CAP
56 : 2 : case SPDM_PSK_FINISH: return libspdm_get_response_psk_finish;
57 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_PSK_CAP*/
58 : :
59 : : #if (LIBSPDM_ENABLE_CAPABILITY_KEY_EX_CAP) || (LIBSPDM_ENABLE_CAPABILITY_PSK_CAP)
60 : 0 : case SPDM_END_SESSION: return libspdm_get_response_end_session;
61 : : #if LIBSPDM_ENABLE_CAPABILITY_HBEAT_CAP
62 : 0 : case SPDM_HEARTBEAT: return libspdm_get_response_heartbeat;
63 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_HBEAT_CAP */
64 : 0 : case SPDM_KEY_UPDATE: return libspdm_get_response_key_update;
65 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_KEY_EX_CAP || LIBSPDM_ENABLE_CAPABILITY_PSK_CAP*/
66 : :
67 : : #if LIBSPDM_ENABLE_CAPABILITY_ENDPOINT_INFO_CAP
68 : 0 : case SPDM_GET_ENDPOINT_INFO: return libspdm_get_response_endpoint_info;
69 : : #endif /*LIBSPDM_ENABLE_CAPABILITY_ENDPOINT_INFO_CAP*/
70 : :
71 : : #if LIBSPDM_ENABLE_CAPABILITY_CSR_CAP
72 : 0 : case SPDM_GET_CSR: return libspdm_get_response_csr;
73 : : #endif /*LIBSPDM_ENABLE_CAPABILITY_CSR_CAP*/
74 : :
75 : : #if LIBSPDM_ENABLE_CAPABILITY_SET_CERT_CAP
76 : 0 : case SPDM_SET_CERTIFICATE: return libspdm_get_response_set_certificate;
77 : : #endif /*LIBSPDM_ENABLE_CAPABILITY_SET_CERT_CAP*/
78 : :
79 : : #if LIBSPDM_ENABLE_CAPABILITY_GET_KEY_PAIR_INFO_CAP
80 : 0 : case SPDM_GET_KEY_PAIR_INFO: return libspdm_get_response_key_pair_info;
81 : : #endif /*LIBSPDM_ENABLE_CAPABILITY_GET_KEY_PAIR_INFO_CAP*/
82 : :
83 : : #if LIBSPDM_ENABLE_CAPABILITY_SET_KEY_PAIR_INFO_CAP
84 : 0 : case SPDM_SET_KEY_PAIR_INFO: return libspdm_get_response_set_key_pair_info_ack;
85 : : #endif /*LIBSPDM_ENABLE_CAPABILITY_SET_KEY_PAIR_INFO_CAP*/
86 : :
87 : : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
88 : 2 : case SPDM_CHUNK_GET: return libspdm_get_response_chunk_get;
89 : 0 : case SPDM_CHUNK_SEND: return libspdm_get_response_chunk_send;
90 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP */
91 : :
92 : : #if LIBSPDM_ENABLE_CAPABILITY_EVENT_CAP
93 : 0 : case SPDM_GET_SUPPORTED_EVENT_TYPES: return libspdm_get_response_supported_event_types;
94 : 0 : case SPDM_SUBSCRIBE_EVENT_TYPES: return libspdm_get_response_subscribe_event_types_ack;
95 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_EVENT_CAP */
96 : :
97 : : #if LIBSPDM_EVENT_RECIPIENT_SUPPORT
98 : 0 : case SPDM_SEND_EVENT: return libspdm_get_response_event_ack;
99 : : #endif /* LIBSPDM_EVENT_RECIPIENT_SUPPORT */
100 : :
101 : : #if LIBSPDM_ENABLE_VENDOR_DEFINED_MESSAGES
102 : 6 : case SPDM_VENDOR_DEFINED_REQUEST: return libspdm_get_vendor_defined_response;
103 : : #endif /*LIBSPDM_ENABLE_VENDOR_DEFINED_MESSAGES*/
104 : :
105 : 16 : default: return NULL;
106 : : }
107 : : }
108 : :
109 : : /**
110 : : * Return the GET_SPDM_RESPONSE function via last request.
111 : : *
112 : : * @param spdm_context The SPDM context for the device.
113 : : *
114 : : * @return GET_SPDM_RESPONSE function according to the last request.
115 : : **/
116 : 102 : static libspdm_get_spdm_response_func libspdm_get_response_func_via_last_request(
117 : : libspdm_context_t *spdm_context)
118 : : {
119 : : spdm_message_header_t *spdm_request;
120 : :
121 : 102 : spdm_request = (void *)spdm_context->last_spdm_request;
122 : 102 : return libspdm_get_response_func_via_request_code(spdm_request->request_response_code);
123 : : }
124 : :
125 : : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
126 : : /**
127 : : * Return whether a request would interrupt a chunk transfer that is in progress. Only the chunk
128 : : * messages of that transfer and GET_VERSION may be received during one.
129 : : *
130 : : * @param spdm_context The SPDM context for the device.
131 : : * @param get_response_func The GET_SPDM_RESPONSE function of the request.
132 : : **/
133 : 102 : static bool libspdm_request_interrupts_chunk_transfer(
134 : : const libspdm_context_t *spdm_context, libspdm_get_spdm_response_func get_response_func)
135 : : {
136 [ + + ]: 102 : if (get_response_func == libspdm_get_response_version) {
137 : 24 : return false;
138 : : }
139 [ + + + + ]: 78 : if (spdm_context->chunk_context.get.chunk_in_use &&
140 : : (get_response_func != libspdm_get_response_chunk_get)) {
141 : 2 : return true;
142 : : }
143 [ + + + - ]: 76 : if (spdm_context->chunk_context.send.chunk_in_use &&
144 : : (get_response_func != libspdm_get_response_chunk_send)) {
145 : 2 : return true;
146 : : }
147 : 74 : return false;
148 : : }
149 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP */
150 : :
151 : 12 : 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 : 12 : context = spdm_context;
168 : : size_t transport_header_size;
169 : : uint8_t *scratch_buffer;
170 : : size_t scratch_buffer_size;
171 : :
172 [ + + ]: 12 : if (request == NULL) {
173 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
174 : : }
175 [ + + ]: 10 : if (request_size == 0) {
176 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
177 : : }
178 : :
179 : 8 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "SpdmReceiveRequest[.] ...\n"));
180 : :
181 : 8 : message_session_id = NULL;
182 : 8 : context->last_spdm_request_session_id_valid = false;
183 : 8 : context->last_spdm_request_size =
184 : 8 : libspdm_get_scratch_buffer_last_spdm_request_capacity(context);
185 : :
186 : : /* always use scratch buffer for the request.
187 : : * if it is secured message, this scratch buffer will be used.
188 : : * if it is normal message, the request ptr will point to receiver buffer. */
189 : 8 : transport_header_size = context->local_context.capability.transport_header_size;
190 : 8 : libspdm_get_scratch_buffer (context, (void **)&scratch_buffer, &scratch_buffer_size);
191 : : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
192 : 8 : decoded_message_ptr = scratch_buffer +
193 : 8 : libspdm_get_scratch_buffer_secure_message_offset() +
194 : : transport_header_size;
195 : 8 : 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 : 8 : backup_decoded_message_ptr = decoded_message_ptr;
203 : 8 : backup_decoded_message_size = decoded_message_size;
204 : :
205 : 8 : 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 : 8 : reset_key_update = false;
211 : 8 : temp_session_context = NULL;
212 : :
213 [ - + ]: 8 : 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 libspdm 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, libspdm_read_uint32((const uint8_t *)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, libspdm_read_uint32((const uint8_t *)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 [ + + ]: 8 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
249 : 2 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "transport_decode_message : %x\n", status));
250 [ + - ]: 2 : if (context->last_spdm_error.error_code != 0) {
251 : : /* If the SPDM error code is Non-Zero, the Responder must send the error message back to the Requester.
252 : : * In this case, return SUCCESS and let caller invoke libspdm_build_response() to send an ERROR message.*/
253 : 2 : *session_id = &context->last_spdm_error.session_id;
254 : 2 : *is_app_message = false;
255 : 2 : 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 restore the environment by
264 : : * libspdm_create_update_session_data_key() again.*/
265 [ - + ]: 6 : if (reset_key_update) {
266 : : /* temp_session_context and message_session_id must necessarily
267 : : * be valid for the code 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, libspdm_read_uint32((const uint8_t *)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 : 6 : decoded_message_size = LIBSPDM_MIN(decoded_message_size,
287 : : context->local_context.capability.data_transfer_size);
288 : :
289 : 6 : context->last_spdm_request_size = decoded_message_size;
290 : 6 : libspdm_copy_mem (context->last_spdm_request,
291 : 6 : libspdm_get_scratch_buffer_last_spdm_request_capacity(context),
292 : : decoded_message_ptr,
293 : : decoded_message_size);
294 : 6 : libspdm_zero_mem (decoded_message_ptr, decoded_message_size);
295 : :
296 [ + + ]: 6 : if (!(*is_app_message)) {
297 : : /* Check for minimal SPDM message size. */
298 [ - + ]: 4 : if (context->last_spdm_request_size < sizeof(spdm_message_header_t)) {
299 : 0 : return LIBSPDM_STATUS_UNSUPPORTED_CAP;
300 : : }
301 : : }
302 : :
303 : 6 : *session_id = message_session_id;
304 : :
305 [ + + ]: 6 : if (message_session_id != NULL) {
306 : 2 : session_info = libspdm_get_session_info_via_session_id(context,
307 : : libspdm_read_uint32(
308 : : (const uint8_t *)message_session_id));
309 [ - + ]: 2 : if (session_info == NULL) {
310 : 0 : return LIBSPDM_STATUS_UNSUPPORTED_CAP;
311 : : }
312 : 2 : context->last_spdm_request_session_id = libspdm_read_uint32((const uint8_t *)message_session_id);
313 : 2 : context->last_spdm_request_session_id_valid = true;
314 : : /* message_session_id points into the received message and may be misaligned.
315 : : * Return the aligned copy instead. */
316 : 2 : *session_id = &context->last_spdm_request_session_id;
317 : : }
318 : :
319 [ + + ]: 6 : if (*is_app_message) {
320 [ + - ]: 2 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "SpdmReceiveRequest[%x] app msg, size (0x%zx): \n",
321 : : (message_session_id != NULL) ? libspdm_read_uint32((const uint8_t *)message_session_id) : 0,
322 : : context->last_spdm_request_size));
323 : : } else {
324 [ - + ]: 4 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "SpdmReceiveRequest[%x] msg %s(0x%x), size (0x%zx): \n",
325 : : (message_session_id != NULL) ? libspdm_read_uint32((const uint8_t *)message_session_id) : 0,
326 : : libspdm_get_code_str(((spdm_message_header_t *)context->last_spdm_request)->
327 : : request_response_code),
328 : : ((spdm_message_header_t *)context->last_spdm_request)->request_response_code,
329 : : context->last_spdm_request_size));
330 : : }
331 : 6 : LIBSPDM_INTERNAL_DUMP_HEX((uint8_t *)context->last_spdm_request,
332 : : context->last_spdm_request_size);
333 : :
334 : 6 : return LIBSPDM_STATUS_SUCCESS;
335 : : }
336 : :
337 : : /**
338 : : * Notify the session state to a session APP.
339 : : *
340 : : * @param spdm_context A pointer to the SPDM context.
341 : : * @param session_id The session_id of a session.
342 : : * @param session_state The state of a session.
343 : : **/
344 : 94 : static void libspdm_trigger_session_state_callback(libspdm_context_t *spdm_context,
345 : : uint32_t session_id,
346 : : libspdm_session_state_t session_state)
347 : : {
348 [ + + ]: 94 : if (spdm_context->spdm_session_state_callback != NULL) {
349 : 8 : ((libspdm_session_state_callback_func)
350 : 8 : spdm_context->spdm_session_state_callback)(spdm_context, session_id, session_state);
351 : : }
352 : 94 : }
353 : :
354 : 96 : void libspdm_set_session_state(libspdm_context_t *spdm_context,
355 : : uint32_t session_id,
356 : : libspdm_session_state_t session_state)
357 : : {
358 : : libspdm_session_info_t *session_info;
359 : : libspdm_session_state_t old_session_state;
360 : :
361 : 96 : session_info = libspdm_get_session_info_via_session_id(spdm_context, session_id);
362 [ - + ]: 96 : if (session_info == NULL) {
363 : 0 : LIBSPDM_ASSERT(false);
364 : 0 : return;
365 : : }
366 : :
367 : 96 : old_session_state = libspdm_secured_message_get_session_state(
368 : : session_info->secured_message_context);
369 [ + + ]: 96 : if (old_session_state != session_state) {
370 : 94 : libspdm_secured_message_set_session_state(
371 : : session_info->secured_message_context, session_state);
372 : 94 : libspdm_trigger_session_state_callback(
373 : : spdm_context, session_info->session_id, session_state);
374 : : }
375 : : }
376 : :
377 : 12 : libspdm_return_t libspdm_terminate_session(
378 : : void *spdm_context, uint32_t session_id)
379 : : {
380 : : libspdm_session_info_t *session_info;
381 : :
382 : 12 : session_info = libspdm_get_session_info_via_session_id(spdm_context, session_id);
383 [ + + ]: 12 : if (session_info == NULL) {
384 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
385 : : }
386 : :
387 : 10 : libspdm_set_session_state(spdm_context, session_id, LIBSPDM_SESSION_STATE_NOT_STARTED);
388 : 10 : libspdm_free_session_id(spdm_context, session_id);
389 : 10 : return LIBSPDM_STATUS_SUCCESS;
390 : : }
391 : :
392 : : /**
393 : : * Notify the connection state to an SPDM context register.
394 : : *
395 : : * @param spdm_context A pointer to the SPDM context.
396 : : * @param connection_state Indicate the SPDM connection state.
397 : : **/
398 : 248 : static void libspdm_trigger_connection_state_callback(libspdm_context_t *spdm_context,
399 : : libspdm_connection_state_t connection_state)
400 : : {
401 [ + + ]: 248 : if (spdm_context->spdm_connection_state_callback != NULL) {
402 : 2 : ((libspdm_connection_state_callback_func)
403 : 2 : spdm_context->spdm_connection_state_callback)(spdm_context, connection_state);
404 : : }
405 : 248 : }
406 : :
407 : 268 : void libspdm_set_connection_state(libspdm_context_t *spdm_context,
408 : : libspdm_connection_state_t connection_state)
409 : : {
410 [ + + ]: 268 : if (spdm_context->connection_info.connection_state != connection_state) {
411 : 248 : spdm_context->connection_info.connection_state = connection_state;
412 : 248 : libspdm_trigger_connection_state_callback(spdm_context, connection_state);
413 : : }
414 : 268 : }
415 : :
416 : 44 : void libspdm_trigger_key_update_callback(void *spdm_context, uint32_t session_id,
417 : : libspdm_key_update_operation_t key_update_op,
418 : : libspdm_key_update_action_t key_update_action)
419 : : {
420 : : libspdm_context_t *context;
421 : :
422 : 44 : context = spdm_context;
423 [ + + ]: 44 : if (context->spdm_key_update_callback != NULL) {
424 : 6 : ((libspdm_key_update_callback_func)
425 : 6 : context->spdm_key_update_callback)(context, session_id, key_update_op, key_update_action);
426 : : }
427 : 44 : }
428 : :
429 : 92 : bool libspdm_is_request_unexpected_for_mut_auth_encap(libspdm_context_t *spdm_context,
430 : : const uint32_t *session_id,
431 : : uint8_t request_code,
432 : : uint8_t *error_code)
433 : : {
434 : : #if LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP
435 : : /* During session-based mutual authentication, enforce that the Requester sends
436 : : * the next request required by the mut_auth_requested bits, before the flow
437 : : * advances (while response_state is still NORMAL).
438 : : *
439 : : * This keys off session_info->mut_auth_requested rather than the encapsulated
440 : : * context's flow_type. MUT_AUTH_REQUESTED (bit 0) has no encapsulated flow, so its
441 : : * flow_type is never set, and it is legal without ENCAP_CAP. */
442 : 92 : libspdm_session_info_t *mut_auth_session_info = NULL;
443 : :
444 [ + + ]: 92 : if (session_id != NULL) {
445 : 22 : mut_auth_session_info = libspdm_get_session_info_via_session_id(spdm_context, *session_id);
446 [ + + + - ]: 100 : } else if ((spdm_context->latest_session_id != INVALID_SESSION_ID) &&
447 : 30 : libspdm_is_capabilities_flag_supported(
448 : : spdm_context, false,
449 : : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP,
450 : : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP)) {
451 : : /* With handshake in the clear the session's handshake messages, including the
452 : : * encapsulated flow, are sent outside of a session, so the enforcement below
453 : : * applies to the channel outside of a session. */
454 : 30 : mut_auth_session_info = libspdm_get_session_info_via_session_id(
455 : : spdm_context, spdm_context->latest_session_id);
456 : : }
457 : :
458 [ + + ]: 92 : if ((mut_auth_session_info != NULL) &&
459 [ + - ]: 52 : (spdm_context->response_state == LIBSPDM_RESPONSE_STATE_NORMAL)) {
460 : : libspdm_session_state_t session_state;
461 : 52 : uint8_t expected_code = 0;
462 : 52 : uint8_t reject_error_code = SPDM_ERROR_CODE_UNEXPECTED_REQUEST;
463 : 52 : bool encap_flow_started = false;
464 : :
465 : : #if LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP
466 : : /* This only constrains the first request after KEY_EXCHANGE_RSP. Once the
467 : : * encapsulated flow has issued a request the messages that advance it are governed
468 : : * by the per-channel enforcement below. */
469 : 52 : encap_flow_started = mut_auth_session_info->encap_context.has_last_encap_request;
470 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP */
471 : :
472 : 52 : session_state = libspdm_secured_message_get_session_state(
473 : : mut_auth_session_info->secured_message_context);
474 [ + - + + ]: 52 : if ((session_state == LIBSPDM_SESSION_STATE_HANDSHAKING) && !encap_flow_started) {
475 [ + + + - ]: 36 : switch (mut_auth_session_info->mut_auth_requested) {
476 : 2 : case SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED:
477 : 2 : expected_code = SPDM_FINISH;
478 : 2 : break;
479 : 20 : case SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED_WITH_ENCAP_REQUEST:
480 : 20 : expected_code = SPDM_GET_ENCAPSULATED_REQUEST;
481 : 20 : break;
482 : 14 : case SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED_WITH_GET_DIGESTS:
483 : 14 : expected_code = SPDM_DELIVER_ENCAPSULATED_RESPONSE;
484 : : /* The optimized flow has already started, as the encapsulated request
485 : : * accompanied KEY_EXCHANGE_RSP, so any other request is in flight rather
486 : : * than unexpected. */
487 : 14 : reject_error_code = SPDM_ERROR_CODE_REQUEST_IN_FLIGHT;
488 : 14 : break;
489 : 0 : default:
490 : 0 : break;
491 : : }
492 : : }
493 : : /* Outside of a session GET_VERSION is also legal, as it resets the connection.
494 : : * The chunk transfer messages are also legal, as they deliver the response that
495 : : * started the flow. */
496 [ + + + + : 52 : if ((expected_code != 0) && (request_code != expected_code) &&
+ - ]
497 [ + - + + ]: 26 : (request_code != SPDM_CHUNK_GET) && (request_code != SPDM_CHUNK_SEND) &&
498 [ + + ]: 16 : ((session_id != NULL) || (request_code != SPDM_GET_VERSION))) {
499 : 22 : *error_code = reject_error_code;
500 : 22 : return true;
501 : : }
502 : : }
503 : :
504 : : #if LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP
505 : : /* During basic mutual authentication, once the Responder has signaled mutual
506 : : * authentication in its CHALLENGE_AUTH response, the next request from the Requester
507 : : * must be GET_ENCAPSULATED_REQUEST, until the flow has issued an encapsulated request.
508 : : * GET_VERSION is excluded because it resets the connection, and the chunk transfer
509 : : * messages are excluded because a large CHALLENGE_AUTH is delivered by CHUNK_GET. */
510 [ + + ]: 70 : if ((session_id == NULL) &&
511 [ + + ]: 58 : (spdm_context->encap_context.flow_type == LIBSPDM_ENCAP_FLOW_BASIC_MUT_AUTH) &&
512 [ + - + - ]: 6 : !spdm_context->encap_context.has_last_encap_request &&
513 [ + + + - ]: 6 : (request_code != SPDM_GET_ENCAPSULATED_REQUEST) && (request_code != SPDM_CHUNK_GET) &&
514 [ + + ]: 4 : (request_code != SPDM_CHUNK_SEND) && (request_code != SPDM_GET_VERSION)) {
515 : 2 : *error_code = SPDM_ERROR_CODE_UNEXPECTED_REQUEST;
516 : 2 : return true;
517 : : }
518 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP */
519 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP */
520 : :
521 : : #if LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP
522 : : /* An encapsulated flow is tracked per channel, so a flow in one secure session does
523 : : * not block requests in another session or outside of a session. While a flow is in
524 : : * progress on this channel only the messages that advance it, or that reset the
525 : : * connection, are legal. */
526 : : {
527 : : const libspdm_encap_context_t *channel_encap_context =
528 : 68 : libspdm_get_encap_context_via_last_request(spdm_context);
529 : :
530 [ + - ]: 68 : if ((channel_encap_context != NULL) &&
531 [ + + ]: 68 : (channel_encap_context->flow_type != LIBSPDM_ENCAP_FLOW_NONE)) {
532 [ + + ]: 28 : switch (request_code) {
533 : 20 : case SPDM_GET_ENCAPSULATED_REQUEST:
534 : : case SPDM_DELIVER_ENCAPSULATED_RESPONSE:
535 : : case SPDM_GET_VERSION:
536 : : case SPDM_CHUNK_GET:
537 : : case SPDM_CHUNK_SEND:
538 : 20 : break;
539 : 8 : default:
540 : 8 : *error_code = SPDM_ERROR_CODE_REQUEST_IN_FLIGHT;
541 : 8 : return true;
542 : : }
543 : : }
544 : : #if LIBSPDM_RESPOND_IF_READY_SUPPORT
545 [ + - + + ]: 40 : else if ((channel_encap_context != NULL) && channel_encap_context->response_not_ready) {
546 : : /* The flow was terminated by an encapsulated ERROR(ResponseNotReady), but the
547 : : * encapsulated request is still outstanding. The Requester must return to the
548 : : * flow so the Responder can reissue it with RESPOND_IF_READY. GET_VERSION is
549 : : * also allowed outside of a session, as it resets the connection. */
550 [ + + + + ]: 12 : if ((request_code != SPDM_GET_ENCAPSULATED_REQUEST) &&
551 [ + + ]: 4 : ((session_id != NULL) || (request_code != SPDM_GET_VERSION))) {
552 : 6 : *error_code = SPDM_ERROR_CODE_REQUEST_IN_FLIGHT;
553 : 6 : return true;
554 : : }
555 : : }
556 : : #endif /* LIBSPDM_RESPOND_IF_READY_SUPPORT */
557 : : }
558 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP */
559 : :
560 : 54 : return false;
561 : : }
562 : :
563 : 118 : libspdm_return_t libspdm_build_response(void *spdm_context, const uint32_t *session_id,
564 : : bool is_app_message,
565 : : size_t *response_size,
566 : : void **response)
567 : : {
568 : : libspdm_context_t *context;
569 : : uint8_t *my_response;
570 : : size_t my_response_size;
571 : : libspdm_return_t status;
572 : : libspdm_get_spdm_response_func get_response_func;
573 : : libspdm_session_info_t *session_info;
574 : : spdm_message_header_t *spdm_request;
575 : : spdm_message_header_t *spdm_response;
576 : : size_t transport_header_size;
577 : : uint8_t *scratch_buffer;
578 : : size_t scratch_buffer_size;
579 : : uint8_t request_response_code;
580 : : uint32_t actual_size;
581 : : bool is_large_response;
582 : :
583 : : #if LIBSPDM_ENABLE_CAPABILITY_HBEAT_CAP
584 : : bool result;
585 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_HBEAT_CAP */
586 : :
587 : : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
588 : : uint8_t *large_buffer;
589 : : size_t large_buffer_size;
590 : : libspdm_chunk_info_t *get_info;
591 : : spdm_chunk_response_response_t *chunk_rsp;
592 : : uint8_t *chunk_ptr;
593 : : size_t chunk_send_ack_response_header_size;
594 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP */
595 : :
596 : 118 : context = spdm_context;
597 : 118 : status = LIBSPDM_STATUS_UNSUPPORTED_CAP;
598 : :
599 : : /* For secure message, set up my_response to scratch buffer
600 : : * For non-secure message, set up my_response to sender buffer*/
601 : 118 : transport_header_size = context->local_context.capability.transport_header_size;
602 [ + + ]: 118 : if (session_id != NULL) {
603 : 34 : libspdm_get_scratch_buffer (context, (void **)&scratch_buffer, &scratch_buffer_size);
604 : : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
605 : 34 : my_response = scratch_buffer + libspdm_get_scratch_buffer_secure_message_offset() +
606 : : transport_header_size;
607 : 34 : my_response_size = libspdm_get_scratch_buffer_secure_message_capacity(context) -
608 : 34 : transport_header_size -
609 : 34 : context->local_context.capability.transport_tail_size;
610 : : #else
611 : : my_response = scratch_buffer + transport_header_size;
612 : : my_response_size = scratch_buffer_size - transport_header_size -
613 : : context->local_context.capability.transport_tail_size;
614 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP */
615 : : } else {
616 : 84 : my_response = (uint8_t *)*response + transport_header_size;
617 : 84 : my_response_size = *response_size - transport_header_size -
618 : 84 : context->local_context.capability.transport_tail_size;
619 : : }
620 : 118 : libspdm_zero_mem(my_response, my_response_size);
621 : :
622 : 118 : spdm_response = (void *)my_response;
623 : :
624 [ + + ]: 118 : if (context->last_spdm_error.error_code != 0) {
625 : : /* Error in libspdm_process_request(), so send the error message directly. */
626 [ + + - ]: 6 : switch (context->last_spdm_error.error_code) {
627 : 4 : case SPDM_ERROR_CODE_DECRYPT_ERROR:
628 : : /* session ID is valid. Use it to encrypt the error message.*/
629 [ + + ]: 4 : if ((context->handle_error_return_policy &
630 : : LIBSPDM_DATA_HANDLE_ERROR_RETURN_POLICY_DROP_ON_DECRYPT_ERROR) == 0) {
631 : 2 : status = libspdm_generate_error_response(
632 : : context, SPDM_ERROR_CODE_DECRYPT_ERROR, 0,
633 : : &my_response_size, my_response);
634 : : } else {
635 : : /**
636 : : * just ignore this message
637 : : * return UNSUPPORTED and clear response_size to continue the dispatch without send response
638 : : **/
639 : 2 : *response_size = 0;
640 : 2 : status = LIBSPDM_STATUS_UNSUPPORTED_CAP;
641 : : }
642 : 4 : break;
643 : 2 : case SPDM_ERROR_CODE_INVALID_SESSION:
644 : : /**
645 : : * don't use session ID, because libspdm doesn't know which session ID to use.
646 : : * just ignore this message
647 : : * return UNSUPPORTED and clear response_size to continue the dispatch without send response
648 : : **/
649 : 2 : *response_size = 0;
650 : 2 : status = LIBSPDM_STATUS_UNSUPPORTED_CAP;
651 : 2 : break;
652 : 0 : default:
653 : 0 : LIBSPDM_ASSERT(false);
654 : 0 : status = LIBSPDM_STATUS_UNSUPPORTED_CAP;
655 : : }
656 : :
657 [ + + ]: 6 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
658 [ + + ]: 4 : if ((session_id != NULL) &&
659 [ + - ]: 2 : (context->last_spdm_error.error_code == SPDM_ERROR_CODE_DECRYPT_ERROR)) {
660 : 2 : libspdm_free_session_id(context, *session_id);
661 : : }
662 : 4 : return status;
663 : : }
664 : :
665 [ + - ]: 2 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "SpdmSendResponse[%x]: msg %s(0x%x), size (0x%zx): \n",
666 : : (session_id != NULL) ? *session_id : 0,
667 : : libspdm_get_code_str(spdm_response->request_response_code),
668 : : spdm_response->request_response_code, my_response_size));
669 : 2 : LIBSPDM_INTERNAL_DUMP_HEX(my_response, my_response_size);
670 : :
671 : 2 : status = context->transport_encode_message(
672 : : context, session_id, false, false,
673 : : my_response_size, my_response, response_size, response);
674 [ - + ]: 2 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
675 [ # # # # ]: 0 : if ((session_id != NULL) &&
676 [ # # ]: 0 : ((status == LIBSPDM_STATUS_SEQUENCE_NUMBER_OVERFLOW) ||
677 : : (status == LIBSPDM_STATUS_CRYPTO_ERROR))) {
678 : 0 : libspdm_free_session_id(context, *session_id);
679 : : }
680 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "transport_encode_message : %x\n", status));
681 : 0 : return status;
682 : : }
683 : :
684 [ + - ]: 2 : if ((session_id != NULL) &&
685 [ + - ]: 2 : (context->last_spdm_error.error_code == SPDM_ERROR_CODE_DECRYPT_ERROR)) {
686 : 2 : libspdm_free_session_id(context, *session_id);
687 : : }
688 : :
689 : 2 : libspdm_zero_mem(&context->last_spdm_error, sizeof(context->last_spdm_error));
690 : 2 : return LIBSPDM_STATUS_SUCCESS;
691 : : }
692 : :
693 [ + + ]: 112 : if (session_id != NULL) {
694 : 30 : session_info = libspdm_get_session_info_via_session_id(context, *session_id);
695 [ - + ]: 30 : if (session_info == NULL) {
696 : 0 : LIBSPDM_ASSERT(false);
697 : 0 : return LIBSPDM_STATUS_UNSUPPORTED_CAP;
698 : : }
699 : : }
700 : :
701 [ + + ]: 112 : if (*response == NULL) {
702 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
703 : : }
704 [ + - + + ]: 110 : if ((response_size == NULL) || (*response_size == 0)) {
705 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
706 : : }
707 : :
708 [ + + ]: 108 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "SpdmSendResponse[%x] ...\n",
709 : : (session_id != NULL) ? *session_id : 0));
710 : :
711 : 108 : spdm_request = (void *)context->last_spdm_request;
712 [ + + ]: 108 : if (context->last_spdm_request_size == 0) {
713 : 2 : return LIBSPDM_STATUS_INVALID_STATE_LOCAL;
714 : : }
715 : :
716 : 106 : get_response_func = NULL;
717 [ + + ]: 106 : if (!is_app_message) {
718 : 102 : get_response_func = libspdm_get_response_func_via_last_request(context);
719 : :
720 : : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
721 : : /* Per DSP0274: The chunked transfer shall not be interrupted by any commands
722 : : * that are not part of the chunk transfer sequence, with the exception of
723 : : * GET_VERSION. The Responder shall return ErrorCode=UnexpectedRequest if an
724 : : * unexpected command is received during the chunked transfer. These error codes
725 : : * shall not interrupt the chunk transfer sequence. */
726 [ + + ]: 102 : if (libspdm_request_interrupts_chunk_transfer(context, get_response_func)) {
727 : 4 : status = libspdm_generate_error_response(
728 : : context, SPDM_ERROR_CODE_UNEXPECTED_REQUEST, 0,
729 : : &my_response_size, my_response);
730 : 4 : goto response_dispatched;
731 : : }
732 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP */
733 : :
734 [ + + ]: 98 : if (get_response_func != NULL) {
735 : 82 : uint8_t reject_error_code = 0;
736 : :
737 [ + + ]: 82 : if (libspdm_is_request_unexpected_for_mut_auth_encap(
738 : 82 : context, session_id, spdm_request->request_response_code, &reject_error_code)) {
739 : 36 : status = libspdm_generate_error_response(
740 : : context, reject_error_code, 0, &my_response_size, my_response);
741 : : } else {
742 : 46 : status = get_response_func(
743 : : context,
744 : : context->last_spdm_request_size,
745 : 46 : context->last_spdm_request,
746 : : &my_response_size, my_response);
747 : : }
748 : : }
749 : : }
750 [ + + + + ]: 102 : if (is_app_message || (get_response_func == NULL)) {
751 [ + + ]: 20 : if (context->get_response_func != NULL) {
752 : 18 : status = ((libspdm_get_response_func) context->get_response_func)(
753 : : context, session_id, is_app_message,
754 : : context->last_spdm_request_size,
755 : 18 : context->last_spdm_request,
756 : : &my_response_size, my_response);
757 : : } else {
758 : 2 : status = LIBSPDM_STATUS_UNSUPPORTED_CAP;
759 : : }
760 : : }
761 : :
762 : : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
763 : 82 : response_dispatched:
764 [ + - ]: 106 : if (libspdm_get_connection_version(context) < SPDM_MESSAGE_VERSION_14) {
765 : 106 : chunk_send_ack_response_header_size = sizeof(spdm_chunk_send_ack_response_t);
766 : : } else {
767 : 0 : chunk_send_ack_response_header_size = sizeof(spdm_chunk_send_ack_response_14_t);
768 : : }
769 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP */
770 : :
771 [ + + ]: 106 : if (status == LIBSPDM_STATUS_SUCCESS) {
772 : 104 : LIBSPDM_ASSERT (my_response_size <= context->local_context.capability.max_spdm_msg_size);
773 : : /* large SPDM message is the SPDM message whose size is greater than the DataTransferSize of the receiving
774 : : * SPDM endpoint or greater than the transmit buffer size of the sending SPDM endpoint */
775 : 104 : is_large_response =
776 : 134 : ((context->connection_info.capability.data_transfer_size != 0) &&
777 [ + + + + ]: 200 : (my_response_size > context->connection_info.capability.data_transfer_size)) ||
778 [ + - ]: 96 : ((context->local_context.capability.sender_data_transfer_size != 0) &&
779 [ + + ]: 96 : (my_response_size > context->local_context.capability.sender_data_transfer_size));
780 : :
781 : : /* The response is too large if it is larger than the Requester's MaxSPDMmsgSize, or if it
782 : : * is a large SPDM message and the endpoints cannot transfer it in chunks. */
783 [ + + ]: 104 : if (((context->connection_info.capability.max_spdm_msg_size != 0) &&
784 [ + + + + ]: 104 : (my_response_size > context->connection_info.capability.max_spdm_msg_size)) ||
785 : 18 : (is_large_response &&
786 [ + + ]: 18 : !libspdm_is_capabilities_flag_supported(
787 : : context, false, SPDM_GET_CAPABILITIES_REQUEST_FLAGS_CHUNK_CAP,
788 : : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CHUNK_CAP))) {
789 : 14 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR, "response size (0x%zx) is too large\n",
790 : : my_response_size));
791 [ + + ]: 28 : if (libspdm_get_connection_version(context) >= SPDM_MESSAGE_VERSION_12) {
792 : 12 : actual_size = (uint32_t)my_response_size;
793 : 12 : status = libspdm_generate_extended_error_response(
794 : : context, SPDM_ERROR_CODE_RESPONSE_TOO_LARGE, 0, sizeof(uint32_t),
795 : : (uint8_t *)&actual_size, &my_response_size, my_response);
796 : : } else {
797 : : /* ResponseTooLarge is not defined before SPDM 1.2. */
798 : 2 : status = libspdm_generate_error_response(
799 : : context, SPDM_ERROR_CODE_UNSPECIFIED, 0, &my_response_size, my_response);
800 : : }
801 [ + + ]: 90 : } else if (is_large_response) {
802 : : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
803 : :
804 : 8 : get_info = &context->chunk_context.get;
805 : :
806 : : /* Saving multiple large responses is not an expected use case.
807 : : * Therefore, if the requester did not perform chunk_get requests for
808 : : * previous large responses, they will be lost. */
809 [ - + ]: 8 : if (get_info->chunk_in_use) {
810 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR,
811 : : "Warning: Overwriting previous unrequested chunk_get info.\n"));
812 : : }
813 : :
814 : 8 : libspdm_get_scratch_buffer(context, (void **)&scratch_buffer, &scratch_buffer_size);
815 : :
816 : : /* The first section of the scratch
817 : : * buffer may be used for other purposes. Use only after that section. */
818 : 16 : large_buffer = (uint8_t *)scratch_buffer +
819 : 8 : libspdm_get_scratch_buffer_large_message_offset(spdm_context);
820 : 8 : large_buffer_size = libspdm_get_scratch_buffer_large_message_capacity(spdm_context);
821 : :
822 : 8 : get_info->chunk_in_use = true;
823 : 8 : get_info->session_id_valid = context->last_spdm_request_session_id_valid;
824 : 8 : get_info->session_id = context->last_spdm_request_session_id;
825 : : /* Increment chunk_handle here as opposed to end of chunk_get handler
826 : : * in case requester never issues chunk_get. */
827 : 8 : get_info->chunk_handle++;
828 : 8 : get_info->chunk_seq_no = 0;
829 : 8 : get_info->chunk_bytes_transferred = 0;
830 : 8 : get_info->large_message_capacity = large_buffer_size;
831 : :
832 : 8 : libspdm_zero_mem(large_buffer, large_buffer_size);
833 : :
834 : : /* It's possible that the large response that was to be sent to the requester was
835 : : * a CHUNK_SEND_ACK + non-chunk response. In this case, to prevent chunking within
836 : : * chunking, only send back the actual response, by saving only non-chunk portion
837 : : * in the scratch buffer, used to respond to the next CHUNK_GET request. */
838 : 8 : if (((spdm_message_header_t *)my_response)
839 [ - + ]: 8 : ->request_response_code == SPDM_CHUNK_SEND_ACK) {
840 : 0 : libspdm_copy_mem(large_buffer, large_buffer_size,
841 : 0 : my_response + chunk_send_ack_response_header_size,
842 : : my_response_size - chunk_send_ack_response_header_size);
843 : 0 : get_info->large_message = large_buffer;
844 : 0 : get_info->large_message_size =
845 : 0 : my_response_size - chunk_send_ack_response_header_size;
846 : : } else {
847 : 8 : libspdm_copy_mem(large_buffer, large_buffer_size, my_response, my_response_size);
848 : :
849 : 8 : get_info->large_message = large_buffer;
850 : 8 : get_info->large_message_size = my_response_size;
851 : : }
852 : :
853 : 8 : status = libspdm_generate_extended_error_response(context,
854 : : SPDM_ERROR_CODE_LARGE_RESPONSE, 0,
855 : : sizeof(uint8_t),
856 : 8 : &get_info->chunk_handle,
857 : : &my_response_size, my_response);
858 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP */
859 : :
860 [ - + ]: 8 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
861 : 0 : goto done;
862 : : }
863 : : }
864 : : }
865 : :
866 : : /* if return the status: Responder drop the response
867 : : * just ignore this message
868 : : * return UNSUPPORTED and clear response_size to continue the dispatch without send response.*/
869 [ - + - - ]: 106 : if ((my_response_size == 0) && (status == LIBSPDM_STATUS_UNSUPPORTED_CAP)) {
870 : 0 : *response_size = 0;
871 : 0 : status = LIBSPDM_STATUS_UNSUPPORTED_CAP;
872 : 0 : goto done;
873 : : }
874 : :
875 [ + + ]: 106 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
876 : 2 : status = libspdm_generate_error_response(
877 : : context, SPDM_ERROR_CODE_UNSUPPORTED_REQUEST,
878 : 2 : spdm_request->request_response_code, &my_response_size, my_response);
879 [ - + ]: 2 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
880 : 0 : goto done;
881 : : }
882 : : }
883 : :
884 [ + + ]: 106 : if (is_app_message) {
885 [ + - ]: 4 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "SpdmSendResponse[%x]: app msg, size (0x%zx): \n",
886 : : (session_id != NULL) ? *session_id : 0, my_response_size));
887 : : } else {
888 [ + + ]: 102 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "SpdmSendResponse[%x]: msg %s(0x%x), size (0x%zx): \n",
889 : : (session_id != NULL) ? *session_id : 0,
890 : : libspdm_get_code_str(spdm_response->request_response_code),
891 : : spdm_response->request_response_code,
892 : : my_response_size));
893 : : }
894 : 106 : LIBSPDM_INTERNAL_DUMP_HEX(my_response, my_response_size);
895 : :
896 : 106 : status = context->transport_encode_message(
897 : : context, session_id, is_app_message, false,
898 : : my_response_size, my_response, response_size, response);
899 [ - + ]: 106 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
900 [ # # # # ]: 0 : if ((session_id != NULL) &&
901 [ # # ]: 0 : ((status == LIBSPDM_STATUS_SEQUENCE_NUMBER_OVERFLOW) ||
902 : : (status == LIBSPDM_STATUS_CRYPTO_ERROR))) {
903 : 0 : libspdm_free_session_id(context, *session_id);
904 : : }
905 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "transport_encode_message : %x\n", status));
906 : 0 : goto done;
907 : : }
908 : :
909 [ + + ]: 106 : if (is_app_message) {
910 : : /* Application data has no request response code, and 0 matches none of the cases below. */
911 : 4 : request_response_code = 0;
912 : : } else {
913 : 102 : request_response_code = spdm_response->request_response_code;
914 : : }
915 : :
916 : : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
917 [ - + + ]: 106 : switch (request_response_code) {
918 : 0 : case SPDM_CHUNK_SEND_ACK:
919 [ # # ]: 0 : if (my_response_size > chunk_send_ack_response_header_size) {
920 : 0 : request_response_code =
921 : 0 : ((spdm_message_header_t *)(my_response + chunk_send_ack_response_header_size))
922 : : ->request_response_code;
923 : : }
924 : 0 : break;
925 : 2 : case SPDM_CHUNK_RESPONSE:
926 : 2 : chunk_rsp = (spdm_chunk_response_response_t *)my_response;
927 : 2 : chunk_ptr = (uint8_t *)(((uint32_t *)(chunk_rsp + 1)) + 1);
928 [ + - ]: 2 : if (chunk_rsp->chunk_seq_no == 0) {
929 : 2 : request_response_code = ((spdm_message_header_t *)chunk_ptr)->request_response_code;
930 : : }
931 : 2 : break;
932 : 104 : default:
933 : 104 : break;
934 : : }
935 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP */
936 : :
937 [ + + ]: 106 : if (session_id != NULL) {
938 [ - - - + ]: 26 : switch (request_response_code) {
939 : 0 : case SPDM_FINISH_RSP:
940 [ # # ]: 0 : if (!libspdm_is_capabilities_flag_supported(
941 : : context, false,
942 : : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP,
943 : : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP)) {
944 : 0 : libspdm_set_session_state(
945 : : context, *session_id,
946 : : LIBSPDM_SESSION_STATE_ESTABLISHED);
947 : : }
948 : 0 : break;
949 : 0 : case SPDM_PSK_FINISH_RSP:
950 : 0 : libspdm_set_session_state(context, *session_id, LIBSPDM_SESSION_STATE_ESTABLISHED);
951 : 0 : break;
952 : 0 : case SPDM_END_SESSION_ACK:
953 : : #if LIBSPDM_ENABLE_CAPABILITY_HBEAT_CAP
954 [ # # # # ]: 0 : if ((session_info->heartbeat_period != 0) &&
955 : 0 : libspdm_is_capabilities_flag_supported(
956 : : context, false,
957 : : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HBEAT_CAP,
958 : : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HBEAT_CAP)) {
959 : 0 : result = libspdm_stop_watchdog(spdm_context, *session_id);
960 [ # # ]: 0 : if (!result) {
961 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR, "libspdm_stop_watchdog error\n"));
962 : : /* No need to return error for internal watchdog error. */
963 : : }
964 : : }
965 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_HBEAT_CAP */
966 : 0 : libspdm_terminate_session(context, *session_id);
967 : 0 : break;
968 : 26 : default:
969 : : #if LIBSPDM_ENABLE_CAPABILITY_HBEAT_CAP
970 [ - + - - ]: 26 : if ((session_info->heartbeat_period != 0) &&
971 : 0 : libspdm_is_capabilities_flag_supported(
972 : : context, false,
973 : : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HBEAT_CAP,
974 : : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HBEAT_CAP)) {
975 : : /* reset watchdog in any session messages. */
976 : 0 : result = libspdm_reset_watchdog(spdm_context, *session_id);
977 [ # # ]: 0 : if (!result) {
978 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR, "libspdm_reset_watchdog error\n"));
979 : : /* No need to return error for internal watchdog error. */
980 : : }
981 : : }
982 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_HBEAT_CAP */
983 : 26 : break;
984 : : }
985 : : } else {
986 [ - + ]: 80 : switch (request_response_code) {
987 : 0 : case SPDM_FINISH_RSP:
988 [ # # ]: 0 : if (libspdm_is_capabilities_flag_supported(
989 : : context, false,
990 : : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP,
991 : : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP)) {
992 : 0 : libspdm_set_session_state(
993 : : context,
994 : : context->latest_session_id,
995 : : LIBSPDM_SESSION_STATE_ESTABLISHED);
996 : : }
997 : 0 : break;
998 : 80 : default:
999 : : /* No session state update needed */
1000 : 80 : break;
1001 : : }
1002 : : }
1003 : :
1004 : 106 : status = LIBSPDM_STATUS_SUCCESS;
1005 : 106 : done:
1006 [ + + ]: 106 : if (context->session_id_to_terminate != INVALID_SESSION_ID) {
1007 : : /* The response has been encoded, with the session's keys if it is in the session, so the
1008 : : * session can now end. */
1009 : 8 : libspdm_terminate_session(context, context->session_id_to_terminate);
1010 : 8 : context->session_id_to_terminate = INVALID_SESSION_ID;
1011 : : }
1012 [ + + ]: 106 : if (session_id != NULL) {
1013 : : /* clean plain text in scratch buffer */
1014 : 26 : libspdm_zero_mem (my_response, my_response_size);
1015 : : }
1016 : 106 : libspdm_zero_mem (context->last_spdm_request,
1017 : 106 : libspdm_get_scratch_buffer_last_spdm_request_capacity(context));
1018 : 106 : context->last_spdm_request_size = 0;
1019 : 106 : context->last_spdm_request_session_id_valid = false;
1020 : 106 : return status;
1021 : : }
1022 : :
1023 : 20 : void libspdm_register_get_response_func(void *context, libspdm_get_response_func get_response_func)
1024 : : {
1025 : : libspdm_context_t *spdm_context;
1026 : :
1027 : 20 : spdm_context = context;
1028 : 20 : spdm_context->get_response_func = (void *)get_response_func;
1029 : 20 : }
1030 : :
1031 : 10 : void libspdm_register_session_state_callback_func(
1032 : : void *spdm_context,
1033 : : libspdm_session_state_callback_func spdm_session_state_callback)
1034 : : {
1035 : : libspdm_context_t *context;
1036 : :
1037 : 10 : LIBSPDM_ASSERT(spdm_context != NULL);
1038 : :
1039 : 10 : context = spdm_context;
1040 : :
1041 : 10 : context->spdm_session_state_callback = (void *)spdm_session_state_callback;
1042 : 10 : }
1043 : :
1044 : 2 : void libspdm_register_connection_state_callback_func(
1045 : : void *spdm_context,
1046 : : libspdm_connection_state_callback_func spdm_connection_state_callback)
1047 : : {
1048 : : libspdm_context_t *context;
1049 : :
1050 : 2 : LIBSPDM_ASSERT(spdm_context != NULL);
1051 : :
1052 : 2 : context = spdm_context;
1053 : 2 : context->spdm_connection_state_callback = (void *)spdm_connection_state_callback;
1054 : 2 : }
1055 : :
1056 : 14 : void libspdm_register_key_update_callback_func(
1057 : : void *spdm_context, libspdm_key_update_callback_func spdm_key_update_callback)
1058 : : {
1059 : : libspdm_context_t *context;
1060 : :
1061 : 14 : LIBSPDM_ASSERT(spdm_context != NULL);
1062 : :
1063 : 14 : context = spdm_context;
1064 : 14 : context->spdm_key_update_callback = (void *)spdm_key_update_callback;
1065 : 14 : }
|