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 : :
9 : : #if LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP
10 : :
11 : 310 : void libspdm_register_encap_flow_handler(void *spdm_context,
12 : : libspdm_encap_flow_handler_func encap_flow_handler)
13 : : {
14 : : libspdm_context_t *context;
15 : :
16 : 310 : context = spdm_context;
17 : :
18 : 310 : context->encap_flow_handler_callback = (void *)encap_flow_handler;
19 : 310 : }
20 : :
21 : 16 : libspdm_return_t libspdm_get_encap_payload_size(void *spdm_context,
22 : : const uint32_t *session_id,
23 : : size_t *payload_size)
24 : : {
25 : : libspdm_encap_context_t *encap_context;
26 : :
27 : 16 : encap_context = libspdm_get_encap_context(spdm_context, session_id);
28 [ + + ]: 16 : if (encap_context == NULL) {
29 : : /* session_id does not refer to an existing session. */
30 : 2 : return LIBSPDM_STATUS_INVALID_STATE_LOCAL;
31 : : }
32 : :
33 : 14 : *payload_size = encap_context->payload_buffer_size;
34 : :
35 : 14 : return LIBSPDM_STATUS_SUCCESS;
36 : : }
37 : :
38 : : /**
39 : : * Process the encapsulated response received from the Requester. Dispatches to the correct
40 : : * response processing function based on the last request code.
41 : : **/
42 : 202 : static libspdm_return_t libspdm_dispatch_process_encap_response(
43 : : libspdm_context_t *spdm_context, uint8_t last_request_code,
44 : : size_t encap_response_size, const void *encap_response, bool *need_continue)
45 : : {
46 [ + + + + : 202 : switch (last_request_code) {
+ + - ]
47 : : #if LIBSPDM_SEND_GET_CERTIFICATE_SUPPORT
48 : 64 : case SPDM_GET_DIGESTS:
49 : 64 : return libspdm_process_encap_response_digest(
50 : : spdm_context, encap_response_size, encap_response, need_continue);
51 : 110 : case SPDM_GET_CERTIFICATE:
52 : 110 : return libspdm_process_encap_response_certificate(
53 : : spdm_context, encap_response_size, encap_response, need_continue);
54 : : #endif /* LIBSPDM_SEND_GET_CERTIFICATE_SUPPORT */
55 : : #if (LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP) && (LIBSPDM_SEND_CHALLENGE_SUPPORT)
56 : 6 : case SPDM_CHALLENGE:
57 : 6 : return libspdm_process_encap_response_challenge_auth(
58 : : spdm_context, encap_response_size, encap_response, need_continue);
59 : : #endif
60 : 14 : case SPDM_KEY_UPDATE:
61 : 14 : return libspdm_process_encap_response_key_update(
62 : : spdm_context, encap_response_size, encap_response, need_continue);
63 : : #if LIBSPDM_SEND_GET_ENDPOINT_INFO_SUPPORT
64 : 4 : case SPDM_GET_ENDPOINT_INFO:
65 : 4 : return libspdm_process_encap_response_endpoint_info(
66 : : spdm_context, encap_response_size, encap_response, need_continue);
67 : : #endif /* LIBSPDM_SEND_GET_ENDPOINT_INFO_SUPPORT */
68 : : #if LIBSPDM_ENABLE_CAPABILITY_EVENT_CAP
69 : 4 : case SPDM_SEND_EVENT:
70 : 4 : return libspdm_process_encap_response_event_ack(
71 : : spdm_context, encap_response_size, encap_response, need_continue);
72 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_EVENT_CAP */
73 : 0 : default:
74 : 0 : LIBSPDM_ASSERT(false);
75 : 0 : return LIBSPDM_STATUS_INVALID_PARAMETER;
76 : : }
77 : : }
78 : :
79 : : /**
80 : : * Determine whether processing the encapsulated response failed because of the Responder's own
81 : : * state or resources rather than because of anything the Requester sent.
82 : : *
83 : : * @param status The status that libspdm_dispatch_process_encap_response returned.
84 : : *
85 : : * @retval true The failure is local, so the Requester is not at fault.
86 : : * @retval false The Requester's encapsulated response could not be processed.
87 : : **/
88 : 38 : static bool libspdm_is_local_process_failure(libspdm_return_t status)
89 : : {
90 [ + - ]: 34 : return (status == LIBSPDM_STATUS_BUFFER_TOO_SMALL) ||
91 [ + + ]: 34 : (status == LIBSPDM_STATUS_BUFFER_FULL) ||
92 [ + - ]: 20 : (status == LIBSPDM_STATUS_INVALID_STATE_LOCAL) ||
93 [ + + - + ]: 72 : (status == LIBSPDM_STATUS_UNSUPPORTED_CAP) ||
94 : : (status == LIBSPDM_STATUS_CRYPTO_ERROR);
95 : : }
96 : :
97 : : /**
98 : : * Tells the Integrator's handler that the encapsulated flow has ended without completing, because
99 : : * of the Requester's encapsulated response or because libspdm could not continue the flow after
100 : : * processing it. The handler cannot continue the flow, so its result and outputs are ignored.
101 : : *
102 : : * @param spdm_context A pointer to the SPDM context.
103 : : * @param session_id The session that the flow belonged to, or NULL.
104 : : * @param flow_type The encapsulated flow that has ended.
105 : : * @param last_request_code The encapsulated request that the Requester responded to.
106 : : * @param encap_response_status The error that ended the flow.
107 : : * @param error_code The ErrorCode of the Requester's encapsulated ERROR, or 0x00.
108 : : * @param encap_request_size The size, in bytes, of encap_request.
109 : : * @param encap_request A buffer that the handler may write to.
110 : : **/
111 : 56 : static void libspdm_report_encap_flow_failure(libspdm_context_t *spdm_context,
112 : : const uint32_t *session_id,
113 : : libspdm_encap_flow_type_t flow_type,
114 : : uint8_t last_request_code,
115 : : libspdm_return_t encap_response_status,
116 : : uint8_t error_code,
117 : : size_t encap_request_size,
118 : : void *encap_request)
119 : : {
120 : : bool terminate_flow;
121 : :
122 : 56 : LIBSPDM_ASSERT(LIBSPDM_STATUS_IS_ERROR(encap_response_status));
123 : :
124 : 56 : terminate_flow = false;
125 : 56 : ((libspdm_encap_flow_handler_func)spdm_context->encap_flow_handler_callback)(
126 : : spdm_context, session_id, flow_type, last_request_code, encap_response_status, error_code,
127 : : &terminate_flow, &encap_request_size, encap_request);
128 : 56 : }
129 : :
130 : : /**
131 : : * Ends an encapsulated flow that did not complete. If the flow is mutual authentication then the
132 : : * Requester is not authenticated, so the certificate chains that the flow recorded are discarded.
133 : : * Session-based mutual authentication also cannot complete, so the session is terminated once the
134 : : * response to the current request has been encoded.
135 : : *
136 : : * @param spdm_context A pointer to the SPDM context.
137 : : * @param session_id The session that the flow belongs to, or NULL.
138 : : * @param encap_context The encapsulated context of the flow.
139 : : **/
140 : 152 : static void libspdm_fail_encap_flow(libspdm_context_t *spdm_context, const uint32_t *session_id,
141 : : libspdm_encap_context_t *encap_context)
142 : : {
143 : : uint8_t slot_id;
144 : :
145 [ + + ]: 152 : if ((encap_context->flow_type == LIBSPDM_ENCAP_FLOW_BASIC_MUT_AUTH) ||
146 [ + + ]: 104 : (encap_context->flow_type == LIBSPDM_ENCAP_FLOW_SESS_MUT_AUTH)) {
147 [ + + ]: 792 : for (slot_id = 0; slot_id < SPDM_MAX_SLOT_COUNT; slot_id++) {
148 [ + + ]: 704 : if ((encap_context->recorded_cert_chain_slot_mask & (1 << slot_id)) == 0) {
149 : 682 : continue;
150 : : }
151 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
152 : : spdm_context->connection_info.peer_used_cert_chain[slot_id].buffer_size = 0;
153 : : #else
154 : 22 : libspdm_free_peer_leaf_cert_public_key(spdm_context, slot_id);
155 : 22 : spdm_context->connection_info.peer_used_cert_chain[slot_id].buffer_hash_size = 0;
156 : : #endif /* LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT */
157 : : }
158 : 88 : encap_context->recorded_cert_chain_slot_mask = 0;
159 : :
160 [ + + + + ]: 88 : if ((encap_context->flow_type == LIBSPDM_ENCAP_FLOW_SESS_MUT_AUTH) &&
161 : : (session_id != NULL)) {
162 : 38 : spdm_context->session_id_to_terminate = *session_id;
163 : : }
164 : : }
165 : :
166 : 152 : encap_context->flow_type = LIBSPDM_ENCAP_FLOW_NONE;
167 : : #if LIBSPDM_RESPOND_IF_READY_SUPPORT
168 : : /* A request that the Requester deferred with ResponseNotReady is not reissued either. */
169 : 152 : encap_context->response_not_ready = false;
170 : : #endif /* LIBSPDM_RESPOND_IF_READY_SUPPORT */
171 : 152 : }
172 : :
173 : : /**
174 : : * When a multi-message operation (GET_CERTIFICATE or KEY_UPDATE) requires a follow-up request,
175 : : * build the next request without calling the Integrator's handler.
176 : : **/
177 : 56 : static libspdm_return_t libspdm_dispatch_encap_need_continue(
178 : : libspdm_context_t *spdm_context, const uint32_t *session_id, uint8_t last_request_code,
179 : : size_t *encap_request_size, void *encap_request)
180 : : {
181 [ + + - ]: 56 : switch (last_request_code) {
182 : : #if LIBSPDM_SEND_GET_CERTIFICATE_SUPPORT
183 : 52 : case SPDM_GET_CERTIFICATE: {
184 : : libspdm_encap_context_t *encap_context;
185 : :
186 : 52 : encap_context = libspdm_get_encap_context(spdm_context, session_id);
187 : :
188 : : /* session_id comes from the request that libspdm is responding to, so it is necessarily
189 : : * valid. */
190 : 52 : LIBSPDM_ASSERT(encap_context != NULL);
191 : :
192 : 52 : return libspdm_get_encap_request_get_certificate_continue(
193 : 52 : spdm_context, session_id, encap_context->req_slot_id,
194 : : encap_request_size, encap_request);
195 : : }
196 : : #endif /* LIBSPDM_SEND_GET_CERTIFICATE_SUPPORT */
197 : 4 : case SPDM_KEY_UPDATE:
198 : 4 : return libspdm_get_encap_request_key_update(
199 : : spdm_context, *session_id, SPDM_KEY_UPDATE_OPERATIONS_VERIFY_NEW_KEY,
200 : : encap_request_size, encap_request);
201 : 0 : default:
202 : 0 : LIBSPDM_ASSERT(false);
203 : 0 : return LIBSPDM_STATUS_INVALID_PARAMETER;
204 : : }
205 : : }
206 : :
207 : : /**
208 : : * Determine whether an encapsulated request is legal for the flow it would be sent in.
209 : : *
210 : : * Encapsulated requests are limited by message type and by whether the flow belongs to a session.
211 : : *
212 : : * @param flow_type The encapsulated flow that is in progress.
213 : : * @param in_session True if the flow belongs to a session. Note that this is not the same as
214 : : * the messages being sent within that session, as session-based mutual
215 : : * authentication is conducted outside of a session when both endpoints have
216 : : * set HANDSHAKE_IN_THE_CLEAR_CAP.
217 : : * @param request_code The request code that the Integrator's handler produced.
218 : : *
219 : : * @retval true The request is legal for this flow.
220 : : * @retval false The request is not legal and must not be sent.
221 : : **/
222 : 120 : static bool libspdm_is_encap_request_legal(libspdm_encap_flow_type_t flow_type,
223 : : bool in_session,
224 : : uint8_t request_code)
225 : : {
226 [ + + + - ]: 120 : switch (flow_type) {
227 : 26 : case LIBSPDM_ENCAP_FLOW_BASIC_MUT_AUTH:
228 : : /* All messages are sent outside of a session. */
229 [ + + ]: 26 : if (in_session) {
230 : 2 : return false;
231 : : }
232 [ + + + + : 24 : return (request_code == SPDM_CHALLENGE) || (request_code == SPDM_GET_DIGESTS) ||
+ + ]
233 : : (request_code == SPDM_GET_CERTIFICATE);
234 : 34 : case LIBSPDM_ENCAP_FLOW_SESS_MUT_AUTH:
235 : : /* All messages belong to the same session, and are only to retrieve the Requester's
236 : : * certificate chain. */
237 [ + + ]: 34 : if (!in_session) {
238 : 2 : return false;
239 : : }
240 [ + + + + ]: 32 : return (request_code == SPDM_GET_DIGESTS) || (request_code == SPDM_GET_CERTIFICATE);
241 : 60 : case LIBSPDM_ENCAP_FLOW_GENERAL:
242 [ + + + ]: 60 : switch (request_code) {
243 : 42 : case SPDM_GET_DIGESTS:
244 : : case SPDM_GET_CERTIFICATE:
245 : : case SPDM_GET_ENDPOINT_INFO:
246 : 42 : return true;
247 : 14 : case SPDM_SEND_EVENT:
248 : : case SPDM_KEY_UPDATE:
249 : : /* Only legal within a session. */
250 : 14 : return in_session;
251 : 4 : default:
252 : 4 : return false;
253 : : }
254 : 0 : default:
255 : 0 : return false;
256 : : }
257 : : }
258 : :
259 : :
260 : 30 : void libspdm_reset_all_encap_state(libspdm_context_t *spdm_context)
261 : : {
262 : : size_t index;
263 : :
264 : 30 : spdm_context->encap_context.flow_type = LIBSPDM_ENCAP_FLOW_NONE;
265 : : #if LIBSPDM_RESPOND_IF_READY_SUPPORT
266 : 30 : spdm_context->encap_context.response_not_ready = false;
267 : : #endif /* LIBSPDM_RESPOND_IF_READY_SUPPORT */
268 : :
269 [ + + ]: 150 : for (index = 0; index < LIBSPDM_MAX_SESSION_COUNT; index++) {
270 : 120 : spdm_context->session_info[index].encap_context.flow_type = LIBSPDM_ENCAP_FLOW_NONE;
271 : : #if LIBSPDM_RESPOND_IF_READY_SUPPORT
272 : 120 : spdm_context->session_info[index].encap_context.response_not_ready = false;
273 : : #endif /* LIBSPDM_RESPOND_IF_READY_SUPPORT */
274 : : }
275 : 30 : }
276 : :
277 : : #define MAX_ERROR_MSG_SIZE 36
278 : :
279 : : /**
280 : : * Propagate an ERROR response that the Integrator's handler produced in place of an encapsulated
281 : : * request. The ERROR replaces the ENCAPSULATED_REQUEST or ENCAPSULATED_RESPONSE_ACK that would
282 : : * otherwise have been sent.
283 : : *
284 : : * @param response The response buffer. It overlaps encap_error.
285 : : * @param response_size On input the size of the response buffer, on output the size of the
286 : : * ERROR response. Unchanged if this function returns an error.
287 : : * @param encap_error The ERROR message that the handler produced.
288 : : * @param encap_error_size Size, in bytes, of encap_error.
289 : : *
290 : : * @retval LIBSPDM_STATUS_SUCCESS The ERROR response was placed in the response buffer.
291 : : * @retval LIBSPDM_STATUS_INVALID_MSG_SIZE encap_error_size is not a plausible ERROR size.
292 : : **/
293 : 12 : static libspdm_return_t libspdm_propagate_encap_error(void *response, size_t *response_size,
294 : : const void *encap_error,
295 : : size_t encap_error_size)
296 : : {
297 : : uint8_t error_response_buffer[MAX_ERROR_MSG_SIZE];
298 : :
299 : : /* The handler supplies encap_error_size, so it is bounded here before it reaches
300 : : * libspdm_copy_mem, which does not clamp. */
301 [ + + + + ]: 12 : if ((encap_error_size < sizeof(spdm_error_response_t)) ||
302 : 8 : (encap_error_size > sizeof(error_response_buffer)) ||
303 [ - + ]: 8 : (encap_error_size > *response_size)) {
304 : 4 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR,
305 : : "encapsulated ERROR size 0x%zx is not valid\n", encap_error_size));
306 : 4 : return LIBSPDM_STATUS_INVALID_MSG_SIZE;
307 : : }
308 : :
309 : : /* Copy through a temporary buffer since the source and destination overlap and memmove is
310 : : * not available. */
311 : 8 : libspdm_copy_mem(error_response_buffer, sizeof(error_response_buffer),
312 : : encap_error, encap_error_size);
313 : 8 : libspdm_copy_mem(response, *response_size, error_response_buffer, encap_error_size);
314 : 8 : *response_size = encap_error_size;
315 : :
316 : 8 : return LIBSPDM_STATUS_SUCCESS;
317 : : }
318 : :
319 : : /* Tracks the session's encapsulated DIGESTS window, so it does nothing outside a session.
320 : : * libspdm_reset_message_buffer_via_request_code() is not used here, as an encapsulated
321 : : * request the Responder sends must not reset its own transcripts. */
322 : 98 : static void libspdm_reset_transcript_via_encap_request(libspdm_context_t *spdm_context,
323 : : const uint32_t *session_id,
324 : : const void *encap_request)
325 : : {
326 [ + + ]: 98 : if (session_id == NULL) {
327 : 50 : return;
328 : : }
329 : :
330 : 48 : libspdm_reset_message_buffer_via_encap_request_code(
331 : : spdm_context,
332 : : libspdm_get_session_info_via_session_id(spdm_context, *session_id),
333 : 48 : ((const spdm_message_header_t *)encap_request)->request_response_code);
334 : : }
335 : :
336 : 174 : libspdm_return_t libspdm_get_response_encapsulated_request(
337 : : libspdm_context_t *spdm_context, size_t request_size, const void *request,
338 : : size_t *response_size, void *response)
339 : : {
340 : : spdm_encapsulated_request_response_t *spdm_response;
341 : : void *encap_request;
342 : : size_t encap_request_size;
343 : : libspdm_return_t status;
344 : : const spdm_get_encapsulated_request_request_t *spdm_request;
345 : : spdm_error_response_t *error_response;
346 : : bool terminate_flow;
347 : : bool recovering;
348 : : const uint32_t *session_id;
349 : : libspdm_encap_context_t *encap_context;
350 : :
351 : 174 : spdm_request = request;
352 : 174 : encap_context = libspdm_get_encap_context_via_last_request(spdm_context);
353 : 174 : recovering = false;
354 : :
355 : : /* LIBSPDM_ASSERT(spdm_request->header.request_response_code == SPDM_GET_ENCAPSULATED_REQUEST);
356 : : */
357 : :
358 [ + + ]: 174 : if (libspdm_get_connection_version(spdm_context) < SPDM_MESSAGE_VERSION_11) {
359 : 18 : return libspdm_generate_error_response(spdm_context,
360 : : SPDM_ERROR_CODE_UNSUPPORTED_REQUEST,
361 : : SPDM_GET_ENCAPSULATED_REQUEST,
362 : : response_size, response);
363 : : }
364 : :
365 [ - + ]: 156 : if (!libspdm_is_encap_supported(spdm_context)) {
366 : 0 : return libspdm_generate_error_response(
367 : : spdm_context, SPDM_ERROR_CODE_UNSUPPORTED_REQUEST,
368 : : SPDM_GET_ENCAPSULATED_REQUEST, response_size, response);
369 : : }
370 : :
371 [ + + ]: 156 : if (spdm_context->encap_flow_handler_callback == NULL) {
372 : : /* If ENCAP_CAP is set then the handler must also be registered. */
373 : 2 : return libspdm_generate_error_response(
374 : : spdm_context, SPDM_ERROR_CODE_UNSPECIFIED, 0, response_size, response);
375 : : }
376 : :
377 [ - + ]: 154 : if (request_size < sizeof(spdm_get_encapsulated_request_request_t)) {
378 : 0 : return libspdm_generate_error_response(spdm_context,
379 : : SPDM_ERROR_CODE_INVALID_REQUEST, 0,
380 : : response_size, response);
381 : : }
382 [ + + ]: 154 : if (spdm_request->header.spdm_version != libspdm_get_connection_version(spdm_context)) {
383 : 2 : return libspdm_generate_error_response(spdm_context,
384 : : SPDM_ERROR_CODE_VERSION_MISMATCH, 0,
385 : : response_size, response);
386 : : }
387 : :
388 [ + + ]: 152 : if (spdm_context->response_state != LIBSPDM_RESPONSE_STATE_NORMAL) {
389 : 2 : return libspdm_responder_handle_response_state(
390 : : spdm_context,
391 : 2 : spdm_request->header.request_response_code,
392 : : response_size, response);
393 : : }
394 : :
395 [ + + ]: 150 : if (encap_context->flow_type == LIBSPDM_ENCAP_FLOW_NONE) {
396 : : #if LIBSPDM_RESPOND_IF_READY_SUPPORT
397 [ + + ]: 50 : if (encap_context->response_not_ready) {
398 : : /* Resume the flow that an encapsulated ERROR(ResponseNotReady) terminated. The
399 : : * outstanding request is left in last_encap_request_header so that the response, when
400 : : * it eventually arrives, is dispatched to the request that asked for it. Entering the
401 : : * flow consumes the ResponseNotReady. */
402 : 8 : encap_context->flow_type = encap_context->response_not_ready_flow_type;
403 : 8 : encap_context->response_not_ready = false;
404 : 8 : recovering = true;
405 : : }
406 : : #endif /* LIBSPDM_RESPOND_IF_READY_SUPPORT */
407 : :
408 [ + + ]: 50 : if (!recovering) {
409 : : /* General encap flow; initialize the encap context. The mutual
410 : : * authentication flows have already set flow_type in CHALLENGE_AUTH or
411 : : * KEY_EXCHANGE_RSP. */
412 : 42 : libspdm_start_encap_flow(encap_context, LIBSPDM_ENCAP_FLOW_GENERAL);
413 : : }
414 : : }
415 : :
416 : 150 : libspdm_reset_message_buffer_via_request_code(spdm_context, NULL,
417 : 150 : spdm_request->header.request_response_code);
418 : :
419 : 150 : LIBSPDM_ASSERT(*response_size > sizeof(spdm_encapsulated_request_response_t));
420 : 150 : libspdm_zero_mem(response, *response_size);
421 : :
422 : 150 : spdm_response = response;
423 : 150 : spdm_response->header.spdm_version = spdm_request->header.spdm_version;
424 : 150 : spdm_response->header.request_response_code = SPDM_ENCAPSULATED_REQUEST;
425 : 150 : spdm_response->header.param1 = encap_context->request_id;
426 : 150 : spdm_response->header.param2 = 0;
427 : :
428 : 150 : encap_request_size = *response_size - sizeof(spdm_encapsulated_request_response_t);
429 : 150 : encap_request = spdm_response + 1;
430 : 150 : terminate_flow = false;
431 : :
432 : : /* This is the session the flow belongs to, which is not necessarily the session the message
433 : : * arrived on. See libspdm_get_encap_session_id_via_last_request. */
434 : 150 : session_id = libspdm_get_encap_session_id_via_last_request(spdm_context);
435 : :
436 : : #if LIBSPDM_RESPOND_IF_READY_SUPPORT
437 [ + + ]: 150 : if (recovering) {
438 : : /* The Requester asked for more time rather than declining the request, so libspdm reissues
439 : : * the outstanding request itself. The Integrator's handler is not consulted, as the flow
440 : : * continues with the request it already has. */
441 : 8 : status = libspdm_get_encap_request_respond_if_ready(
442 : : spdm_context, session_id, &encap_request_size, encap_request);
443 : :
444 [ + + ]: 8 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
445 : : /* The failure is local to the Responder; nothing was wrong with the Requester's
446 : : * message. */
447 : 2 : libspdm_fail_encap_flow(spdm_context, session_id, encap_context);
448 : 2 : return libspdm_generate_error_response(
449 : : spdm_context, SPDM_ERROR_CODE_UNSPECIFIED, 0, response_size, response);
450 : : }
451 : :
452 : : /* The Request ID is unchanged, since this reissues the request that already carries it. */
453 : 6 : *response_size = sizeof(spdm_encapsulated_request_response_t) + encap_request_size;
454 : :
455 : 6 : return LIBSPDM_STATUS_SUCCESS;
456 : : }
457 : : #endif /* LIBSPDM_RESPOND_IF_READY_SUPPORT */
458 : :
459 : 142 : LIBSPDM_ASSERT(encap_context->flow_type != LIBSPDM_ENCAP_FLOW_NONE);
460 : :
461 : 142 : status = ((libspdm_encap_flow_handler_func)spdm_context->encap_flow_handler_callback)(
462 : : spdm_context, session_id, encap_context->flow_type, 0, LIBSPDM_STATUS_SUCCESS, 0,
463 : : &terminate_flow, &encap_request_size, encap_request);
464 : :
465 [ + + ]: 142 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
466 : : /* The failure is local to the Responder; nothing was wrong with the Requester's
467 : : * message. */
468 : 10 : libspdm_fail_encap_flow(spdm_context, session_id, encap_context);
469 : 10 : return libspdm_generate_error_response(
470 : : spdm_context, SPDM_ERROR_CODE_UNSPECIFIED, 0,
471 : : response_size, response);
472 : : }
473 : :
474 : 132 : error_response = (spdm_error_response_t *)encap_request;
475 : :
476 [ + + ]: 132 : if (error_response->header.request_response_code == SPDM_ERROR) {
477 : : /* Handler generated an error response; propagate it directly. The ERROR ends the flow. */
478 : 4 : status = libspdm_propagate_encap_error(response, response_size,
479 : : encap_request, encap_request_size);
480 : 4 : libspdm_fail_encap_flow(spdm_context, session_id, encap_context);
481 [ + + ]: 4 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
482 : 2 : return libspdm_generate_error_response(
483 : : spdm_context, SPDM_ERROR_CODE_UNSPECIFIED, 0, response_size, response);
484 : : }
485 : 2 : return LIBSPDM_STATUS_SUCCESS;
486 : : }
487 : :
488 [ + + + + ]: 128 : if ((terminate_flow || (encap_request_size == 0)) &&
489 [ + + ]: 24 : (encap_context->flow_type != LIBSPDM_ENCAP_FLOW_GENERAL)) {
490 : : /* The Responder asked for this flow in CHALLENGE_AUTH or KEY_EXCHANGE_RSP, so it must
491 : : * issue a request rather than report that it has none pending. */
492 : 12 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR,
493 : : "encapsulated flow type %d cannot terminate without a request\n",
494 : : encap_context->flow_type));
495 : 12 : libspdm_fail_encap_flow(spdm_context, session_id, encap_context);
496 : 12 : return libspdm_generate_error_response(
497 : : spdm_context, SPDM_ERROR_CODE_UNSPECIFIED, 0, response_size, response);
498 : : }
499 : :
500 [ + + ]: 116 : if (terminate_flow) {
501 : : /* The Requester polled and the Responder has nothing to send, so no flow is in progress. */
502 : 10 : encap_context->flow_type = LIBSPDM_ENCAP_FLOW_NONE;
503 : :
504 [ + + ]: 10 : if (libspdm_get_connection_version(spdm_context) >= SPDM_MESSAGE_VERSION_13) {
505 : 2 : return libspdm_generate_error_response(
506 : : spdm_context,
507 : : SPDM_ERROR_CODE_NO_PENDING_REQUESTS, 0,
508 : : response_size, response);
509 : : } else {
510 : 8 : return libspdm_generate_error_response(
511 : : spdm_context,
512 : : SPDM_ERROR_CODE_UNEXPECTED_REQUEST, 0,
513 : : response_size, response);
514 : : }
515 : : } else {
516 [ + + ]: 106 : if ((encap_request_size != 0) &&
517 [ + + ]: 104 : !libspdm_is_encap_request_legal(encap_context->flow_type,
518 : : session_id != NULL,
519 : 104 : error_response->header.request_response_code)) {
520 : : /* The Integrator produced a request that is not permitted in this flow. */
521 : 18 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR,
522 : : "encapsulated request 0x%x is not legal in flow type %d\n",
523 : : error_response->header.request_response_code,
524 : : encap_context->flow_type));
525 : 18 : libspdm_fail_encap_flow(spdm_context, session_id, encap_context);
526 : 18 : return libspdm_generate_error_response(
527 : : spdm_context, SPDM_ERROR_CODE_UNSPECIFIED, 0, response_size, response);
528 : : }
529 [ + + ]: 88 : if (encap_request_size != 0) {
530 : 86 : libspdm_reset_transcript_via_encap_request(spdm_context, session_id, encap_request);
531 : : }
532 : 88 : *response_size = sizeof(spdm_encapsulated_request_response_t) + encap_request_size;
533 : : }
534 : :
535 [ + + ]: 88 : if (encap_request_size == 0) {
536 : 2 : encap_context->flow_type = LIBSPDM_ENCAP_FLOW_NONE;
537 : : }
538 : :
539 : 88 : return LIBSPDM_STATUS_SUCCESS;
540 : : }
541 : :
542 : 254 : libspdm_return_t libspdm_get_response_encapsulated_response_ack(
543 : : libspdm_context_t *spdm_context, size_t request_size, const void *request,
544 : : size_t *response_size, void *response)
545 : : {
546 : : const spdm_deliver_encapsulated_response_request_t *spdm_request;
547 : : size_t spdm_request_size;
548 : : spdm_encapsulated_response_ack_response_t *spdm_response;
549 : : const void *encap_response;
550 : : size_t encap_response_size;
551 : : void *encap_request;
552 : : size_t encap_request_size;
553 : : libspdm_return_t status;
554 : : size_t ack_header_size;
555 : : bool terminate_flow;
556 : : bool need_continue;
557 : : bool response_not_ready;
558 : : bool flow_suspended;
559 : : bool encap_error_received;
560 : : uint8_t last_request_code;
561 : : uint8_t error_code;
562 : : libspdm_return_t encap_response_status;
563 : : libspdm_encap_flow_type_t flow_type;
564 : : const uint32_t *session_id;
565 : : libspdm_encap_context_t *encap_context;
566 : :
567 : 254 : spdm_request = request;
568 : 254 : encap_context = libspdm_get_encap_context_via_last_request(spdm_context);
569 : :
570 : : /* LIBSPDM_ASSERT(spdm_request->header.request_response_code ==
571 : : * SPDM_DELIVER_ENCAPSULATED_RESPONSE); */
572 : :
573 [ + + ]: 254 : if (libspdm_get_connection_version(spdm_context) < SPDM_MESSAGE_VERSION_11) {
574 : 12 : return libspdm_generate_error_response(spdm_context,
575 : : SPDM_ERROR_CODE_UNSUPPORTED_REQUEST,
576 : : SPDM_DELIVER_ENCAPSULATED_RESPONSE,
577 : : response_size, response);
578 : : }
579 : :
580 [ - + ]: 242 : if (!libspdm_is_encap_supported(spdm_context)) {
581 : 0 : return libspdm_generate_error_response(
582 : : spdm_context, SPDM_ERROR_CODE_UNSUPPORTED_REQUEST,
583 : : SPDM_DELIVER_ENCAPSULATED_RESPONSE, response_size, response);
584 : : }
585 : :
586 [ + + ]: 242 : if (spdm_context->encap_flow_handler_callback == NULL) {
587 : : /* If ENCAP_CAP is set then the handler must also be registered. */
588 : 2 : return libspdm_generate_error_response(
589 : : spdm_context, SPDM_ERROR_CODE_UNSPECIFIED, 0, response_size, response);
590 : : }
591 : :
592 [ + + ]: 240 : if (spdm_context->response_state != LIBSPDM_RESPONSE_STATE_NORMAL) {
593 : 2 : return libspdm_responder_handle_response_state(
594 : : spdm_context,
595 : 2 : spdm_request->header.request_response_code,
596 : : response_size, response);
597 : : }
598 : :
599 [ + + ]: 238 : if (encap_context->flow_type == LIBSPDM_ENCAP_FLOW_NONE) {
600 : : /* No encapsulated flow is in progress on this channel. Note that the first
601 : : * DELIVER_ENCAPSULATED_RESPONSE after KEY_EXCHANGE_RSP with bit 2 set is legal, as
602 : : * flow_type was set to LIBSPDM_ENCAP_FLOW_SESS_MUT_AUTH at that time. */
603 : 2 : return libspdm_generate_error_response(
604 : : spdm_context,
605 : : SPDM_ERROR_CODE_UNEXPECTED_REQUEST, 0,
606 : : response_size, response);
607 : : }
608 : :
609 [ + + ]: 236 : if (request_size <= sizeof(spdm_deliver_encapsulated_response_request_t)) {
610 : 12 : return libspdm_generate_error_response(spdm_context,
611 : : SPDM_ERROR_CODE_INVALID_REQUEST, 0,
612 : : response_size, response);
613 : : }
614 [ + + ]: 224 : if (spdm_request->header.spdm_version != libspdm_get_connection_version(spdm_context)) {
615 : 6 : return libspdm_generate_error_response(spdm_context,
616 : : SPDM_ERROR_CODE_VERSION_MISMATCH, 0,
617 : : response_size, response);
618 : : }
619 : :
620 : 218 : spdm_request_size = request_size;
621 : :
622 [ + + ]: 218 : if (spdm_request->header.param1 != encap_context->request_id) {
623 : 4 : return libspdm_generate_error_response(spdm_context,
624 : : SPDM_ERROR_CODE_INVALID_REQUEST, 0,
625 : : response_size, response);
626 : : }
627 : :
628 : 214 : encap_response = spdm_request + 1;
629 : 214 : encap_response_size = spdm_request_size - sizeof(spdm_deliver_encapsulated_response_request_t);
630 : :
631 [ + + ]: 214 : if (spdm_request->header.spdm_version >= SPDM_MESSAGE_VERSION_12) {
632 : 62 : ack_header_size = sizeof(spdm_encapsulated_response_ack_response_t);
633 : : } else {
634 : 152 : ack_header_size = sizeof(spdm_message_header_t);
635 : : }
636 : :
637 : 214 : LIBSPDM_ASSERT(*response_size > ack_header_size);
638 : 214 : libspdm_zero_mem(response, *response_size);
639 : :
640 : 214 : spdm_response = response;
641 : 214 : spdm_response->header.spdm_version = spdm_request->header.spdm_version;
642 : 214 : spdm_response->header.request_response_code = SPDM_ENCAPSULATED_RESPONSE_ACK;
643 : 214 : spdm_response->header.param1 = 0;
644 : 214 : spdm_response->header.param2 = SPDM_ENCAPSULATED_RESPONSE_ACK_RESPONSE_PAYLOAD_TYPE_PRESENT;
645 : :
646 : 214 : encap_request_size = *response_size - ack_header_size;
647 : 214 : encap_request = (uint8_t *)spdm_response + ack_header_size;
648 [ - + ]: 214 : if (encap_response_size < sizeof(spdm_message_header_t)) {
649 : 0 : return libspdm_generate_error_response(spdm_context,
650 : : SPDM_ERROR_CODE_INVALID_REQUEST, 0,
651 : : response_size, response);
652 : : }
653 : :
654 : 214 : libspdm_reset_message_buffer_via_request_code(spdm_context, NULL,
655 : 214 : spdm_request->header.request_response_code);
656 : :
657 : 214 : terminate_flow = false;
658 : 214 : need_continue = false;
659 : 214 : response_not_ready = false;
660 : 214 : flow_suspended = false;
661 : 214 : encap_error_received = false;
662 : 214 : error_code = 0;
663 : 214 : encap_response_status = LIBSPDM_STATUS_SUCCESS;
664 : 214 : flow_type = encap_context->flow_type;
665 : 214 : last_request_code = encap_context->last_encap_request_header.request_response_code;
666 : : /* This is the session the flow belongs to, which is not necessarily the session the message
667 : : * arrived on. See libspdm_get_encap_session_id_via_last_request. */
668 : 214 : session_id = libspdm_get_encap_session_id_via_last_request(spdm_context);
669 : :
670 [ + + ]: 214 : if (last_request_code != 0) {
671 : 202 : const spdm_error_response_t *encap_error = encap_response;
672 : :
673 [ + + + + ]: 202 : if ((flow_type == LIBSPDM_ENCAP_FLOW_SESS_MUT_AUTH) &&
674 [ + + ]: 18 : (last_request_code == SPDM_GET_DIGESTS) && !encap_context->has_last_encap_request) {
675 : : /* This answers the GET_DIGESTS that KEY_EXCHANGE_RSP implies, which counts as
676 : : * issued from now on. The flow has then moved past its first message. */
677 : 14 : encap_context->has_last_encap_request = true;
678 : : }
679 : :
680 : : /* Process the encapsulated response from the Requester before calling the handler. */
681 : 202 : status = libspdm_dispatch_process_encap_response(
682 : : spdm_context, last_request_code,
683 : : encap_response_size, encap_response, &need_continue);
684 : 202 : encap_response_status = status;
685 : :
686 [ + + + + ]: 278 : if ((session_id != NULL) &&
687 : 76 : (libspdm_get_session_info_via_session_id(spdm_context, *session_id) == NULL)) {
688 : : /* Processing the encapsulated response ended the session that the flow belongs to,
689 : : * which also discarded encap_context. There is no flow left to continue. */
690 [ + - ]: 2 : if (encap_error->header.request_response_code == SPDM_ERROR) {
691 : 2 : error_code = encap_error->header.param1;
692 : : }
693 : 2 : libspdm_report_encap_flow_failure(spdm_context, session_id, flow_type,
694 : : last_request_code, encap_response_status,
695 : : error_code, encap_request_size, encap_request);
696 : 2 : return libspdm_generate_error_response(
697 : : spdm_context, SPDM_ERROR_CODE_UNSPECIFIED, 0, response_size, response);
698 : : }
699 : :
700 [ + + ]: 200 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
701 : : #if LIBSPDM_RESPOND_IF_READY_SUPPORT
702 : : const spdm_error_data_response_not_ready_t *not_ready_data;
703 : : #endif /* LIBSPDM_RESPOND_IF_READY_SUPPORT */
704 : :
705 [ + - ]: 84 : if ((encap_response_size < sizeof(spdm_error_response_t)) ||
706 [ + + + + ]: 132 : (encap_error->header.request_response_code != SPDM_ERROR) ||
707 : 48 : (encap_error->header.spdm_version !=
708 : 48 : libspdm_get_connection_version(spdm_context))) {
709 : : /* The encapsulated response was not an ERROR of the negotiated version, so
710 : : * processing it genuinely failed and there is no ErrorCode to report to the
711 : : * handler. A failure of the Responder's own state or resources, such as a payload
712 : : * larger than the buffer the Integrator supplied, is not reported as though the
713 : : * Requester's response were at fault. */
714 : : uint8_t process_error_code;
715 : :
716 [ + + ]: 38 : if (libspdm_is_local_process_failure(status)) {
717 : 18 : process_error_code = SPDM_ERROR_CODE_UNSPECIFIED;
718 : : } else {
719 : 20 : process_error_code = SPDM_ERROR_CODE_INVALID_RESPONSE_CODE;
720 : : }
721 : :
722 : 38 : libspdm_fail_encap_flow(spdm_context, session_id, encap_context);
723 : 38 : libspdm_report_encap_flow_failure(spdm_context, session_id, flow_type,
724 : : last_request_code, encap_response_status, 0,
725 : : encap_request_size, encap_request);
726 : 38 : return libspdm_generate_error_response(
727 : : spdm_context, process_error_code, 0, response_size, response);
728 : : }
729 : :
730 [ + + ]: 46 : if (encap_error->header.param1 == 0) {
731 : : /* ErrorCode 0x00 is reserved, so this ERROR is malformed. It is reported to the
732 : : * handler as a malformed response rather than as an encapsulated ERROR, as an
733 : : * error_code of 0 is how the absence of an encapsulated ERROR is conveyed. */
734 : 6 : libspdm_fail_encap_flow(spdm_context, session_id, encap_context);
735 : 6 : libspdm_report_encap_flow_failure(spdm_context, session_id, flow_type,
736 : : last_request_code,
737 : : LIBSPDM_STATUS_INVALID_MSG_FIELD, 0,
738 : : encap_request_size, encap_request);
739 : 6 : return libspdm_generate_error_response(
740 : : spdm_context, SPDM_ERROR_CODE_INVALID_RESPONSE_CODE, 0,
741 : : response_size, response);
742 : : }
743 : :
744 : : /* The Requester delivered an encapsulated ERROR. Report its code to the handler so
745 : : * that the Integrator learns why the flow ended. The status is derived from the
746 : : * ErrorCode, so that it does not depend on which request was answered, except that
747 : : * ResponseNotReady is reported as such only when the flow resumes. */
748 : 40 : encap_error_received = true;
749 : 40 : error_code = encap_error->header.param1;
750 : 40 : encap_response_status = libspdm_handle_encap_error_response_main(error_code);
751 : :
752 [ + + ]: 40 : if (status == LIBSPDM_STATUS_NOT_READY_PEER) {
753 : 20 : response_not_ready = true;
754 : :
755 : : #if LIBSPDM_RESPOND_IF_READY_SUPPORT
756 : : /* The encapsulated request is still outstanding, so retain the flow it belongs to
757 : : * and the fields needed to reissue it with RESPOND_IF_READY once the Requester
758 : : * returns with GET_ENCAPSULATED_REQUEST. */
759 [ + + ]: 20 : if (encap_response_size <
760 : : (sizeof(spdm_error_response_t) +
761 : : sizeof(spdm_error_data_response_not_ready_t))) {
762 : 2 : libspdm_fail_encap_flow(spdm_context, session_id, encap_context);
763 : 2 : libspdm_report_encap_flow_failure(spdm_context, session_id, flow_type,
764 : : last_request_code,
765 : : LIBSPDM_STATUS_INVALID_MSG_SIZE, error_code,
766 : : encap_request_size, encap_request);
767 : 2 : return libspdm_generate_error_response(
768 : : spdm_context, SPDM_ERROR_CODE_INVALID_RESPONSE_CODE, 0,
769 : : response_size, response);
770 : : }
771 : 18 : not_ready_data = (const void *)((const uint8_t *)encap_response +
772 : : sizeof(spdm_error_response_t));
773 : :
774 : : /* The Requester echoes the request it is deferring, and the Responder knows which
775 : : * request that is. A mismatch, or a retry interval the Responder cannot honour,
776 : : * means the extended data cannot be used to reissue the request. */
777 [ + + ]: 18 : if ((not_ready_data->request_code != last_request_code) ||
778 [ + + ]: 16 : (not_ready_data->rd_tm <= 1) ||
779 [ + + ]: 14 : (not_ready_data->rd_exponent > LIBSPDM_MAX_RDT_EXPONENT)) {
780 : 6 : libspdm_fail_encap_flow(spdm_context, session_id, encap_context);
781 : 6 : libspdm_report_encap_flow_failure(spdm_context, session_id, flow_type,
782 : : last_request_code,
783 : : LIBSPDM_STATUS_INVALID_MSG_FIELD, error_code,
784 : : encap_request_size, encap_request);
785 : 6 : return libspdm_generate_error_response(
786 : : spdm_context, SPDM_ERROR_CODE_INVALID_RESPONSE_CODE, 0,
787 : : response_size, response);
788 : : }
789 : :
790 : : /* Session-based mutual authentication allows no encapsulated request but
791 : : * GET_DIGESTS and GET_CERTIFICATE, so it cannot be resumed with
792 : : * RESPOND_IF_READY. */
793 [ + + ]: 12 : if (flow_type != LIBSPDM_ENCAP_FLOW_SESS_MUT_AUTH) {
794 : 6 : libspdm_copy_mem(&encap_context->response_not_ready_data,
795 : : sizeof(encap_context->response_not_ready_data),
796 : : not_ready_data,
797 : : sizeof(spdm_error_data_response_not_ready_t));
798 : 6 : encap_context->response_not_ready_flow_type = encap_context->flow_type;
799 : 6 : encap_context->response_not_ready = true;
800 : 6 : flow_suspended = true;
801 : : }
802 : : #endif /* LIBSPDM_RESPOND_IF_READY_SUPPORT */
803 : : }
804 : :
805 [ + + + + ]: 32 : if ((encap_response_status == LIBSPDM_STATUS_NOT_READY_PEER) && !flow_suspended) {
806 : : /* The flow does not resume, so ResponseNotReady ends it as any other ErrorCode
807 : : * does. */
808 : 8 : encap_response_status = LIBSPDM_STATUS_ERROR_PEER;
809 : : }
810 : : }
811 : :
812 [ + + ]: 148 : if (need_continue) {
813 : : /* Build the follow-up request (next GET_CERTIFICATE chunk or VerifyNewKey)
814 : : * without invoking the handler. */
815 : 56 : status = libspdm_dispatch_encap_need_continue(
816 : : spdm_context, session_id, last_request_code,
817 : : &encap_request_size, encap_request);
818 [ + + ]: 56 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
819 : : /* The failure is local to the Responder; nothing was wrong with the Requester's
820 : : * encapsulated response. */
821 : 2 : libspdm_fail_encap_flow(spdm_context, session_id, encap_context);
822 : 2 : libspdm_report_encap_flow_failure(spdm_context, session_id, flow_type,
823 : : last_request_code, status, 0,
824 : : encap_request_size, encap_request);
825 : 2 : return libspdm_generate_error_response(
826 : : spdm_context, SPDM_ERROR_CODE_UNSPECIFIED, 0,
827 : : response_size, response);
828 : : }
829 : 54 : goto set_ack_fields;
830 : : }
831 : :
832 : : #if (LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP) && (LIBSPDM_SEND_CHALLENGE_SUPPORT)
833 [ + + + + ]: 92 : if (!encap_error_received && (last_request_code == SPDM_CHALLENGE)) {
834 : : /* Basic mutual authentication concludes with the encapsulated CHALLENGE_AUTH
835 : : * response, whose signature has been verified, so the Requester is authenticated.
836 : : * The Responder must then terminate the encapsulated flow by clearing
837 : : * ENCAPSULATED_RESPONSE_ACK.Param2. The Integrator's handler is told that the
838 : : * Requester is authenticated, but it cannot continue the flow, so its result and
839 : : * outputs are ignored. */
840 : : size_t ignored_request_size;
841 : :
842 : 2 : ignored_request_size = encap_request_size;
843 : 2 : ((libspdm_encap_flow_handler_func)spdm_context->encap_flow_handler_callback)(
844 : : spdm_context, session_id, flow_type, last_request_code, encap_response_status, 0,
845 : : &terminate_flow, &ignored_request_size, encap_request);
846 : 2 : terminate_flow = true;
847 : 2 : goto set_ack_fields;
848 : : }
849 : : #endif /* (LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP) && (LIBSPDM_SEND_CHALLENGE_SUPPORT) */
850 : : }
851 : :
852 : 102 : LIBSPDM_ASSERT(encap_context->flow_type != LIBSPDM_ENCAP_FLOW_NONE);
853 : :
854 : : /* All response data processed; ask the Integrator what to do next. */
855 : 102 : status = ((libspdm_encap_flow_handler_func)spdm_context->encap_flow_handler_callback)(
856 : : spdm_context, session_id, encap_context->flow_type, last_request_code,
857 : : encap_response_status, error_code, &terminate_flow, &encap_request_size, encap_request);
858 : :
859 [ + + ]: 102 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
860 : : /* The failure is local to the Responder; nothing was wrong with the Requester's
861 : : * encapsulated response. */
862 : 8 : libspdm_fail_encap_flow(spdm_context, session_id, encap_context);
863 : 8 : return libspdm_generate_error_response(
864 : : spdm_context, SPDM_ERROR_CODE_UNSPECIFIED, 0, response_size, response);
865 : : }
866 : :
867 [ + + ]: 94 : if (encap_error_received) {
868 : : /* An encapsulated ERROR ends the flow, so whatever the handler produced is discarded. */
869 [ + + ]: 28 : if (!flow_suspended) {
870 : 26 : libspdm_fail_encap_flow(spdm_context, session_id, encap_context);
871 [ + + + + ]: 26 : if ((flow_type == LIBSPDM_ENCAP_FLOW_SESS_MUT_AUTH) && !response_not_ready) {
872 : : /* The Requester has not completed mutual authentication, so session
873 : : * establishment halts instead of the final ENCAPSULATED_RESPONSE_ACK designating
874 : : * its certificate slot. After ResponseNotReady that ENCAPSULATED_RESPONSE_ACK
875 : : * must clear Param2 instead, designating no slot, and the session is still
876 : : * terminated. */
877 : 2 : return libspdm_generate_error_response(
878 : : spdm_context, SPDM_ERROR_CODE_UNSPECIFIED, 0, response_size, response);
879 : : }
880 : : }
881 : 26 : terminate_flow = true;
882 : 26 : encap_request_size = 0;
883 : 26 : goto set_ack_fields;
884 : : }
885 : :
886 [ + - ]: 66 : if ((encap_request_size != 0) &&
887 [ + + ]: 66 : (((const spdm_message_header_t *)encap_request)->request_response_code == SPDM_ERROR)) {
888 : : /* Handler generated an error response instead of an encapsulated request; propagate it
889 : : * directly. This is checked before the legality test below, as SPDM_ERROR is not a
890 : : * request code and would otherwise be rejected there. The ERROR ends the flow. */
891 : 8 : status = libspdm_propagate_encap_error(response, response_size,
892 : : encap_request, encap_request_size);
893 : 8 : libspdm_fail_encap_flow(spdm_context, session_id, encap_context);
894 [ + + ]: 8 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
895 : 2 : return libspdm_generate_error_response(
896 : : spdm_context, SPDM_ERROR_CODE_UNSPECIFIED, 0, response_size, response);
897 : : }
898 : 6 : return LIBSPDM_STATUS_SUCCESS;
899 : : }
900 : :
901 [ + + - + ]: 58 : if ((terminate_flow || (encap_request_size == 0)) &&
902 [ + + ]: 42 : (encap_context->flow_type == LIBSPDM_ENCAP_FLOW_BASIC_MUT_AUTH)) {
903 : : /* Basic mutual authentication succeeds only once the CHALLENGE_AUTH signature has been
904 : : * verified, so the flow fails if it ends before then. */
905 : 6 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR,
906 : : "encapsulated flow type %d cannot terminate before CHALLENGE_AUTH\n",
907 : : encap_context->flow_type));
908 : 6 : libspdm_fail_encap_flow(spdm_context, session_id, encap_context);
909 : 6 : return libspdm_generate_error_response(
910 : : spdm_context, SPDM_ERROR_CODE_UNSPECIFIED, 0, response_size, response);
911 : : }
912 : :
913 [ + + + - ]: 52 : if (!terminate_flow && (encap_request_size != 0) &&
914 [ + + ]: 16 : !libspdm_is_encap_request_legal(
915 : : encap_context->flow_type, session_id != NULL,
916 : 16 : ((const spdm_message_header_t *)encap_request)->request_response_code)) {
917 : : /* The Integrator produced a request that is not permitted in this flow. */
918 : 4 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR,
919 : : "encapsulated request 0x%x is not legal in flow type %d\n",
920 : : ((const spdm_message_header_t *)encap_request)->request_response_code,
921 : : encap_context->flow_type));
922 : 4 : libspdm_fail_encap_flow(spdm_context, session_id, encap_context);
923 : 4 : return libspdm_generate_error_response(
924 : : spdm_context, SPDM_ERROR_CODE_UNSPECIFIED, 0, response_size, response);
925 : : }
926 : :
927 [ + + - + ]: 48 : if (!terminate_flow && (encap_request_size != 0)) {
928 : 12 : libspdm_reset_transcript_via_encap_request(spdm_context, session_id, encap_request);
929 : : }
930 : :
931 : 36 : set_ack_fields:
932 : 130 : *response_size = ack_header_size + encap_request_size;
933 : :
934 [ + + ]: 130 : if (spdm_request->header.spdm_version >= SPDM_MESSAGE_VERSION_12) {
935 : 28 : spdm_response->ack_request_id = spdm_request->header.param1;
936 : : }
937 : :
938 [ + + + - ]: 130 : if (!terminate_flow && (encap_request_size != 0)) {
939 [ + + ]: 66 : if (encap_context->request_id == UINT8_MAX) {
940 : 2 : encap_context->request_id = 1;
941 : : } else {
942 : 64 : encap_context->request_id++;
943 : : }
944 : 66 : spdm_response->header.param1 = encap_context->request_id;
945 : : } else {
946 : : /* No further encapsulated request, so this is the final message of the flow. */
947 : 64 : spdm_response->header.param1 = 0;
948 : 64 : spdm_response->header.param2 = SPDM_ENCAPSULATED_RESPONSE_ACK_RESPONSE_PAYLOAD_TYPE_ABSENT;
949 : 64 : *response_size = ack_header_size;
950 : :
951 [ + + + - ]: 64 : if ((encap_context->flow_type == LIBSPDM_ENCAP_FLOW_SESS_MUT_AUTH) && !response_not_ready) {
952 : : /* When MutAuthRequested bit 1 or bit 2 is set, the Responder must designate the
953 : : * Requester's certificate slot in the final ENCAPSULATED_RESPONSE_ACK, as
954 : : * KEY_EXCHANGE_RSP.ReqSlotID could not convey it. */
955 : 8 : spdm_response->header.param2 =
956 : : SPDM_ENCAPSULATED_RESPONSE_ACK_RESPONSE_PAYLOAD_TYPE_REQ_SLOT_NUMBER;
957 : 8 : *response_size = ack_header_size + 1;
958 : 8 : *((uint8_t *)spdm_response + ack_header_size) = encap_context->mut_auth_req_slot_id;
959 : : }
960 : :
961 : 64 : encap_context->flow_type = LIBSPDM_ENCAP_FLOW_NONE;
962 : : }
963 : :
964 : 130 : return LIBSPDM_STATUS_SUCCESS;
965 : : }
966 : :
967 : 90 : libspdm_return_t libspdm_handle_encap_error_response_main(uint8_t error_code)
968 : : {
969 [ + + + + ]: 90 : switch (error_code) {
970 : 42 : case SPDM_ERROR_CODE_RESPONSE_NOT_READY:
971 : 42 : return LIBSPDM_STATUS_NOT_READY_PEER;
972 : 6 : case SPDM_ERROR_CODE_BUSY:
973 : 6 : return LIBSPDM_STATUS_BUSY_PEER;
974 : 4 : case SPDM_ERROR_CODE_DECRYPT_ERROR:
975 : 4 : return LIBSPDM_STATUS_SESSION_MSG_ERROR;
976 : 38 : default:
977 : 38 : return LIBSPDM_STATUS_ERROR_PEER;
978 : : }
979 : : }
980 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP */
|