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 : : #if LIBSPDM_ENABLE_CAPABILITY_KEY_EX_CAP
11 : :
12 : 40 : bool libspdm_generate_key_exchange_rsp_hmac(libspdm_context_t *spdm_context,
13 : : libspdm_session_info_t *session_info,
14 : : uint8_t *hmac)
15 : : {
16 : : uint8_t hmac_data[LIBSPDM_MAX_HASH_SIZE];
17 : : size_t hash_size;
18 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
19 : : uint8_t slot_id;
20 : : const uint8_t *cert_chain_buffer;
21 : : size_t cert_chain_buffer_size;
22 : : uint8_t *th_curr_data;
23 : : size_t th_curr_data_size;
24 : : libspdm_th_managed_buffer_t th_curr;
25 : : uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
26 : : #endif /* LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT */
27 : : bool result;
28 : :
29 : 40 : hash_size = libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
30 : :
31 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
32 : : slot_id = session_info->local_used_cert_chain_slot_id;
33 : : LIBSPDM_ASSERT((slot_id < SPDM_MAX_SLOT_COUNT) || (slot_id == 0xFF));
34 : : if (slot_id == 0xFF) {
35 : : result = libspdm_get_local_public_key_buffer(
36 : : spdm_context, (const void **)&cert_chain_buffer, &cert_chain_buffer_size);
37 : : if (!result) {
38 : : return false;
39 : : }
40 : : } else {
41 : : libspdm_get_local_cert_chain_buffer(
42 : : spdm_context, slot_id, (const void **)&cert_chain_buffer, &cert_chain_buffer_size);
43 : : }
44 : :
45 : : result = libspdm_calculate_th_for_exchange(
46 : : spdm_context, session_info, cert_chain_buffer, cert_chain_buffer_size, &th_curr);
47 : : if (!result) {
48 : : return false;
49 : : }
50 : : th_curr_data = libspdm_get_managed_buffer(&th_curr);
51 : : th_curr_data_size = libspdm_get_managed_buffer_size(&th_curr);
52 : :
53 : : result = libspdm_hash_all (spdm_context->connection_info.algorithm.base_hash_algo,
54 : : th_curr_data, th_curr_data_size, hash_data);
55 : : if (!result) {
56 : : return false;
57 : : }
58 : :
59 : : result = libspdm_hmac_all_with_response_finished_key(
60 : : session_info->secured_message_context, hash_data, hash_size, hmac_data);
61 : : if (!result) {
62 : : return false;
63 : : }
64 : : #else
65 : 40 : result = libspdm_calculate_th_hmac_for_exchange_rsp(
66 : : spdm_context, session_info, &hash_size, hmac_data);
67 [ - + ]: 40 : if (!result) {
68 : 0 : return false;
69 : : }
70 : : #endif /* LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT */
71 : 40 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "th_curr hmac - "));
72 : 40 : LIBSPDM_INTERNAL_DUMP_DATA(hmac_data, hash_size);
73 : 40 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "\n"));
74 : 40 : libspdm_copy_mem(hmac, hash_size, hmac_data, hash_size);
75 : :
76 : 40 : return true;
77 : : }
78 : :
79 : 48 : bool libspdm_generate_key_exchange_rsp_signature(libspdm_context_t *spdm_context,
80 : : libspdm_session_info_t *session_info,
81 : : uint8_t slot_id,
82 : : uint8_t *signature)
83 : : {
84 : : bool result;
85 : : size_t signature_size;
86 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
87 : : uint8_t *th_curr_data;
88 : : size_t th_curr_data_size;
89 : : libspdm_th_managed_buffer_t th_curr;
90 : : const uint8_t *cert_chain_buffer;
91 : : size_t cert_chain_buffer_size;
92 : : #endif /* LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT */
93 : : #if ((LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT) && (LIBSPDM_DEBUG_BLOCK_ENABLE)) || \
94 : : !(LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT)
95 : : uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
96 : : #endif
97 : : #if !(LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT) || (LIBSPDM_DEBUG_PRINT_ENABLE)
98 : : size_t hash_size;
99 : :
100 : 48 : hash_size = libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
101 : : #endif
102 : :
103 [ - + ]: 48 : if (spdm_context->connection_info.algorithm.pqc_asym_algo != 0) {
104 : 0 : signature_size = libspdm_get_pqc_asym_signature_size(
105 : : spdm_context->connection_info.algorithm.pqc_asym_algo);
106 : : } else {
107 : 48 : signature_size = libspdm_get_asym_signature_size(
108 : : spdm_context->connection_info.algorithm.base_asym_algo);
109 : : }
110 : :
111 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
112 : : LIBSPDM_ASSERT((slot_id < SPDM_MAX_SLOT_COUNT) || (slot_id == 0xFF));
113 : : if (slot_id == 0xFF) {
114 : : result = libspdm_get_local_public_key_buffer(
115 : : spdm_context, (const void **)&cert_chain_buffer, &cert_chain_buffer_size);
116 : : if (!result) {
117 : : return false;
118 : : }
119 : : } else {
120 : : libspdm_get_local_cert_chain_buffer(
121 : : spdm_context, slot_id, (const void **)&cert_chain_buffer, &cert_chain_buffer_size);
122 : : }
123 : :
124 : : result = libspdm_calculate_th_for_exchange(
125 : : spdm_context, session_info, cert_chain_buffer, cert_chain_buffer_size, &th_curr);
126 : : if (!result) {
127 : : return false;
128 : : }
129 : : th_curr_data = libspdm_get_managed_buffer(&th_curr);
130 : : th_curr_data_size = libspdm_get_managed_buffer_size(&th_curr);
131 : :
132 : : /* Debug code only - required for debug print of th_curr hash below*/
133 : : LIBSPDM_DEBUG_CODE(
134 : : if (!libspdm_hash_all(
135 : : spdm_context->connection_info.algorithm.base_hash_algo,
136 : : th_curr_data, th_curr_data_size, hash_data)) {
137 : : return false;
138 : : }
139 : : );
140 : : #else
141 : 48 : result = libspdm_calculate_th_hash_for_exchange(
142 : : spdm_context, session_info, &hash_size, hash_data);
143 [ - + ]: 48 : if (!result) {
144 : 0 : return false;
145 : : }
146 : : #endif /* LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT */
147 : 48 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "th_curr hash - "));
148 : 48 : LIBSPDM_INTERNAL_DUMP_DATA(hash_data, hash_size);
149 : 48 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "\n"));
150 : :
151 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
152 : : result = libspdm_responder_data_sign(
153 : : spdm_context,
154 : : spdm_context->connection_info.version,
155 : : libspdm_slot_id_to_key_pair_id(spdm_context, slot_id, false),
156 : : SPDM_KEY_EXCHANGE_RSP,
157 : : spdm_context->connection_info.algorithm.base_asym_algo,
158 : : spdm_context->connection_info.algorithm.pqc_asym_algo,
159 : : spdm_context->connection_info.algorithm.base_hash_algo,
160 : : false, th_curr_data, th_curr_data_size, signature, &signature_size);
161 : : #else
162 : 96 : result = libspdm_responder_data_sign(
163 : : spdm_context,
164 : 48 : spdm_context->connection_info.version,
165 : 48 : libspdm_slot_id_to_key_pair_id(spdm_context, slot_id, false),
166 : : SPDM_KEY_EXCHANGE_RSP,
167 : : spdm_context->connection_info.algorithm.base_asym_algo,
168 : : spdm_context->connection_info.algorithm.pqc_asym_algo,
169 : : spdm_context->connection_info.algorithm.base_hash_algo,
170 : : true, hash_data, hash_size, signature, &signature_size);
171 : : #endif /* LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT */
172 [ + - ]: 48 : if (result) {
173 : 48 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "signature - "));
174 : 48 : LIBSPDM_INTERNAL_DUMP_DATA(signature, signature_size);
175 : 48 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "\n"));
176 : : }
177 : 48 : return result;
178 : : }
179 : :
180 : : #if (LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP) && (LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP)
181 : : /**
182 : : * Initialize the encapsulated state for session-based mutual authentication.
183 : : *
184 : : * @param spdm_context A pointer to the SPDM context.
185 : : * @param session_info The session that mutual authentication occurs within.
186 : : * @param mut_auth_requested The MutAuthRequested value returned in KEY_EXCHANGE_RSP.
187 : : * @param req_slot_id The Requester's certificate slot.
188 : : **/
189 : 6 : static void init_encap_state(libspdm_context_t *spdm_context,
190 : : libspdm_session_info_t *session_info,
191 : : uint8_t mut_auth_requested,
192 : : uint8_t req_slot_id)
193 : : {
194 : : libspdm_encap_context_t *encap_context;
195 : :
196 : 6 : LIBSPDM_ASSERT(
197 : : (mut_auth_requested == SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED_WITH_ENCAP_REQUEST) ||
198 : : (mut_auth_requested == SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED_WITH_GET_DIGESTS));
199 : :
200 : 6 : LIBSPDM_ASSERT(req_slot_id < SPDM_MAX_SLOT_COUNT);
201 : :
202 : 6 : encap_context = &session_info->encap_context;
203 : 6 : libspdm_start_encap_flow(encap_context, LIBSPDM_ENCAP_FLOW_SESS_MUT_AUTH);
204 : 6 : encap_context->req_slot_id = req_slot_id;
205 : 6 : encap_context->mut_auth_req_slot_id = req_slot_id;
206 : 6 : encap_context->payload_buffer_size = 0;
207 : :
208 : : /* Clear cache. */
209 : 6 : libspdm_reset_message_mut_b(spdm_context);
210 : 6 : libspdm_reset_message_mut_c(spdm_context);
211 : :
212 [ + + - ]: 6 : switch (mut_auth_requested) {
213 : 4 : case SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED_WITH_ENCAP_REQUEST:
214 : 4 : break;
215 : 2 : case SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED_WITH_GET_DIGESTS:
216 : : /* GET_DIGESTS was embedded in KEY_EXCHANGE_RSP; prime the ACK handler to treat the
217 : : * first DELIVER_ENCAPSULATED_RESPONSE as a DIGESTS reply. */
218 : 2 : encap_context->last_encap_request_header.request_response_code = SPDM_GET_DIGESTS;
219 : 2 : break;
220 : 0 : default:
221 : 0 : LIBSPDM_ASSERT(false);
222 : 0 : break;
223 : : }
224 : 6 : }
225 : : #endif /* (LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP) && (LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP) */
226 : :
227 : 116 : libspdm_return_t libspdm_get_response_key_exchange(libspdm_context_t *spdm_context,
228 : : size_t request_size,
229 : : const void *request,
230 : : size_t *response_size,
231 : : void *response)
232 : : {
233 : : const spdm_key_exchange_request_t *spdm_request;
234 : : spdm_key_exchange_response_t *spdm_response;
235 : : size_t dhe_key_size;
236 : : size_t kem_encap_key_size;
237 : : size_t kem_cipher_text_size;
238 : : size_t req_key_exchange_size;
239 : : size_t rsp_key_exchange_size;
240 : : uint32_t measurement_summary_hash_size;
241 : : uint32_t signature_size;
242 : : uint32_t hmac_size;
243 : : uint8_t *ptr;
244 : : const uint8_t *req_opaque_data;
245 : : uint8_t *rsp_opaque_data;
246 : : uint16_t opaque_data_length;
247 : : bool result;
248 : : uint8_t slot_id;
249 : : uint32_t session_id;
250 : : void *dhe_context;
251 : : void *kem_context;
252 : : libspdm_session_info_t *session_info;
253 : : size_t total_size;
254 : : uint16_t req_session_id;
255 : : uint16_t rsp_session_id;
256 : : libspdm_return_t status;
257 : : size_t opaque_key_exchange_rsp_size;
258 : : bool use_default_opaque_data;
259 : : uint8_t th1_hash_data[LIBSPDM_MAX_HASH_SIZE];
260 : : spdm_version_number_t secured_message_version;
261 : 116 : uint8_t peer_aead_limit_exponent = SECURED_MESSAGE_AEAD_LIMIT_EXPONENT_DEFAULT;
262 : : #if LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP
263 : : uint8_t req_slot_id;
264 : : uint8_t mut_auth_requested;
265 : : bool mandatory_mut_auth;
266 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP */
267 : :
268 : 116 : spdm_request = request;
269 : :
270 : : /* -=[Check Parameters Phase]=- */
271 : 116 : LIBSPDM_ASSERT(spdm_request->header.request_response_code == SPDM_KEY_EXCHANGE);
272 : :
273 [ - + ]: 116 : if (libspdm_get_connection_version(spdm_context) < SPDM_MESSAGE_VERSION_11) {
274 : 0 : return libspdm_generate_error_response(spdm_context,
275 : : SPDM_ERROR_CODE_UNSUPPORTED_REQUEST,
276 : : SPDM_KEY_EXCHANGE,
277 : : response_size, response);
278 : : }
279 : :
280 [ - + ]: 116 : if (spdm_request->header.spdm_version != libspdm_get_connection_version(spdm_context)) {
281 : 0 : return libspdm_generate_error_response(spdm_context,
282 : : SPDM_ERROR_CODE_VERSION_MISMATCH, 0,
283 : : response_size, response);
284 : : }
285 [ + + ]: 116 : if (spdm_context->response_state != LIBSPDM_RESPONSE_STATE_NORMAL) {
286 : 8 : return libspdm_responder_handle_response_state(
287 : : spdm_context,
288 : 8 : spdm_request->header.request_response_code,
289 : : response_size, response);
290 : : }
291 [ - + ]: 108 : if (!libspdm_is_capabilities_flag_supported(
292 : : spdm_context, false,
293 : : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP,
294 : : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP)) {
295 : 0 : return libspdm_generate_error_response(
296 : : spdm_context, SPDM_ERROR_CODE_UNSUPPORTED_REQUEST,
297 : : SPDM_KEY_EXCHANGE, response_size, response);
298 : : }
299 : :
300 : : /* While clearing MAC_CAP and setting ENCRYPT_CAP is legal according to DSP0274, libspdm
301 : : * also implements DSP0277 secure messages, which requires at least MAC_CAP to be set.
302 : : */
303 [ + + ]: 108 : if (!libspdm_is_capabilities_flag_supported(
304 : : spdm_context, false,
305 : : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP,
306 : : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP)) {
307 : 12 : return libspdm_generate_error_response(
308 : : spdm_context, SPDM_ERROR_CODE_INVALID_REQUEST,
309 : : SPDM_KEY_EXCHANGE, response_size, response);
310 : : }
311 : :
312 [ + + ]: 96 : if (spdm_context->connection_info.connection_state < LIBSPDM_CONNECTION_STATE_NEGOTIATED) {
313 : 2 : return libspdm_generate_error_response(spdm_context,
314 : : SPDM_ERROR_CODE_UNEXPECTED_REQUEST,
315 : : 0, response_size, response);
316 : : }
317 [ + + ]: 94 : if (libspdm_get_connection_version(spdm_context) >= SPDM_MESSAGE_VERSION_12) {
318 [ - + ]: 14 : if ((spdm_context->connection_info.algorithm.other_params_support &
319 : : SPDM_ALGORITHMS_OPAQUE_DATA_FORMAT_MASK) != SPDM_ALGORITHMS_OPAQUE_DATA_FORMAT_1) {
320 : 0 : return libspdm_generate_error_response(
321 : : spdm_context, SPDM_ERROR_CODE_INVALID_REQUEST,
322 : : 0, response_size, response);
323 : : }
324 : : }
325 [ - + ]: 94 : if (spdm_context->last_spdm_request_session_id_valid) {
326 : 0 : return libspdm_generate_error_response(spdm_context,
327 : : SPDM_ERROR_CODE_UNEXPECTED_REQUEST,
328 : : 0, response_size, response);
329 : : }
330 : :
331 [ + + ]: 94 : if (spdm_request->header.param1 > 0) {
332 [ + + ]: 10 : if (!libspdm_is_capabilities_flag_supported(
333 : : spdm_context, false,
334 : 8 : 0, SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEAS_CAP) ||
335 [ + - ]: 8 : (spdm_context->connection_info.algorithm.measurement_spec == 0) ||
336 [ - + ]: 8 : (spdm_context->connection_info.algorithm.measurement_hash_algo == 0) ) {
337 : 2 : return libspdm_generate_error_response(
338 : : spdm_context, SPDM_ERROR_CODE_INVALID_REQUEST,
339 : : 0, response_size, response);
340 : : }
341 : : }
342 : :
343 : 92 : slot_id = spdm_request->header.param2;
344 : :
345 [ + + ]: 92 : if (libspdm_is_capabilities_flag_supported(
346 : : spdm_context, false,
347 : : 0, SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP)) {
348 [ + + ]: 88 : if (slot_id >= SPDM_MAX_SLOT_COUNT) {
349 : 2 : return libspdm_generate_error_response(spdm_context,
350 : : SPDM_ERROR_CODE_INVALID_REQUEST, 0,
351 : : response_size, response);
352 : : }
353 [ - + ]: 86 : if (spdm_context->local_context.local_cert_chain_provision[slot_id] == NULL) {
354 : 0 : return libspdm_generate_error_response(spdm_context,
355 : : SPDM_ERROR_CODE_INVALID_REQUEST, 0,
356 : : response_size, response);
357 : : }
358 : : } else {
359 [ + + ]: 4 : if (slot_id != 0xff) {
360 : 2 : return libspdm_generate_error_response(spdm_context,
361 : : SPDM_ERROR_CODE_INVALID_REQUEST, 0,
362 : : response_size, response);
363 : : }
364 [ - + ]: 2 : if (spdm_context->local_context.local_public_key_provision == NULL) {
365 : 0 : return libspdm_generate_error_response(spdm_context,
366 : : SPDM_ERROR_CODE_INVALID_REQUEST, 0,
367 : : response_size, response);
368 : : }
369 : : }
370 : :
371 [ + + ]: 88 : if (spdm_request->header.spdm_version >= SPDM_MESSAGE_VERSION_13) {
372 [ + + + - ]: 6 : if (spdm_context->connection_info.multi_key_conn_rsp && (slot_id != 0xff)) {
373 [ + - ]: 2 : if ((spdm_context->local_context.local_key_usage_bit_mask[slot_id] &
374 : : SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE) == 0) {
375 : 2 : return libspdm_generate_error_response(
376 : : spdm_context, SPDM_ERROR_CODE_INVALID_REQUEST, 0, response_size, response);
377 : : }
378 : : }
379 : :
380 [ + + ]: 4 : if ((spdm_request->session_policy &
381 : : SPDM_KEY_EXCHANGE_REQUEST_SESSION_POLICY_EVENT_ALL_POLICY) != 0) {
382 [ - + ]: 2 : if (!libspdm_is_capabilities_flag_supported(
383 : : spdm_context, false, 0, SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_EVENT_CAP)) {
384 : 0 : return libspdm_generate_error_response(spdm_context,
385 : : SPDM_ERROR_CODE_INVALID_REQUEST, 0,
386 : : response_size, response);
387 : : }
388 : : }
389 : : }
390 : :
391 [ - + ]: 86 : if (spdm_context->connection_info.algorithm.pqc_asym_algo != 0) {
392 : 0 : signature_size = libspdm_get_pqc_asym_signature_size(
393 : : spdm_context->connection_info.algorithm.pqc_asym_algo);
394 : : } else {
395 : 86 : signature_size = libspdm_get_asym_signature_size(
396 : : spdm_context->connection_info.algorithm.base_asym_algo);
397 : : }
398 : 86 : hmac_size = libspdm_get_hash_size(
399 : : spdm_context->connection_info.algorithm.base_hash_algo);
400 [ - + ]: 86 : if (spdm_context->connection_info.algorithm.kem_alg != 0) {
401 : 0 : kem_encap_key_size = libspdm_get_kem_encap_key_size(
402 : : spdm_context->connection_info.algorithm.kem_alg);
403 : 0 : kem_cipher_text_size = libspdm_get_kem_cipher_text_size(
404 : : spdm_context->connection_info.algorithm.kem_alg);
405 : 0 : req_key_exchange_size = kem_encap_key_size;
406 : 0 : rsp_key_exchange_size = kem_cipher_text_size;
407 : : } else {
408 : 172 : dhe_key_size = libspdm_get_dhe_pub_key_size(
409 : 86 : spdm_context->connection_info.algorithm.dhe_named_group);
410 : 86 : req_key_exchange_size = dhe_key_size;
411 : 86 : rsp_key_exchange_size = dhe_key_size;
412 : : }
413 : 86 : measurement_summary_hash_size = libspdm_get_measurement_summary_hash_size(
414 : 86 : spdm_context, false, spdm_request->header.param1);
415 : :
416 [ + + ]: 86 : if ((measurement_summary_hash_size == 0) &&
417 [ + + ]: 80 : (spdm_request->header.param1 != SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH)) {
418 : 2 : return libspdm_generate_error_response(spdm_context,
419 : : SPDM_ERROR_CODE_INVALID_REQUEST,
420 : : 0, response_size, response);
421 : : }
422 [ + + ]: 84 : if (request_size < sizeof(spdm_key_exchange_request_t) + req_key_exchange_size +
423 : : sizeof(uint16_t)) {
424 : 2 : return libspdm_generate_error_response(spdm_context,
425 : : SPDM_ERROR_CODE_INVALID_REQUEST, 0,
426 : : response_size, response);
427 : : }
428 : 82 : opaque_data_length = libspdm_read_uint16((const uint8_t *)request +
429 : 82 : sizeof(spdm_key_exchange_request_t) +
430 : : req_key_exchange_size);
431 [ + + ]: 82 : if (opaque_data_length > SPDM_MAX_OPAQUE_DATA_SIZE) {
432 : 2 : return libspdm_generate_error_response(spdm_context,
433 : : SPDM_ERROR_CODE_INVALID_REQUEST, 0,
434 : : response_size, response);
435 : : }
436 : 80 : if (request_size < sizeof(spdm_key_exchange_request_t) + req_key_exchange_size +
437 [ - + ]: 80 : sizeof(uint16_t) + opaque_data_length) {
438 : 0 : return libspdm_generate_error_response(spdm_context,
439 : : SPDM_ERROR_CODE_INVALID_REQUEST, 0,
440 : : response_size, response);
441 : : }
442 : 80 : request_size = sizeof(spdm_key_exchange_request_t) + req_key_exchange_size +
443 : 80 : sizeof(uint16_t) + opaque_data_length;
444 : :
445 [ + - ]: 80 : if (opaque_data_length != 0) {
446 : 80 : req_opaque_data = (const uint8_t *)request + sizeof(spdm_key_exchange_request_t) +
447 : 80 : req_key_exchange_size + sizeof(uint16_t);
448 : :
449 : : /*
450 : : * Here allows integrator generate own opaque data for Key Exchange Response.
451 : : * If libspdm_key_exchange_rsp_opaque_data() returns false,
452 : : * libspdm will generate version selection opaque data.
453 : : */
454 : 80 : opaque_key_exchange_rsp_size = *response_size - sizeof(spdm_key_exchange_response_t) -
455 : 80 : rsp_key_exchange_size - measurement_summary_hash_size -
456 : 80 : sizeof(uint16_t) - signature_size - hmac_size;
457 : :
458 : 80 : use_default_opaque_data = false;
459 : 80 : result = libspdm_key_exchange_rsp_opaque_data(
460 : 80 : spdm_context, spdm_request->header.spdm_version,
461 : 80 : spdm_request->header.param1, slot_id, spdm_request->session_policy,
462 : : req_opaque_data, opaque_data_length, NULL,
463 : : &opaque_key_exchange_rsp_size);
464 [ + + ]: 80 : if (!result) {
465 : 78 : use_default_opaque_data = true;
466 : 78 : opaque_key_exchange_rsp_size =
467 : 78 : libspdm_get_opaque_data_version_selection_data_size(spdm_context);
468 : : }
469 : :
470 [ + + ]: 80 : if (use_default_opaque_data) {
471 : 78 : result = libspdm_process_general_opaque_data_check(spdm_context, opaque_data_length,
472 : : req_opaque_data);
473 [ - + ]: 78 : if (!result) {
474 : 0 : return libspdm_generate_error_response(spdm_context,
475 : : SPDM_ERROR_CODE_INVALID_REQUEST, 0,
476 : : response_size, response);
477 : : }
478 : 78 : status = libspdm_process_opaque_data_supported_version_data(
479 : : spdm_context, opaque_data_length, req_opaque_data, &secured_message_version);
480 [ - + ]: 78 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
481 : 0 : return libspdm_generate_error_response(spdm_context,
482 : : SPDM_ERROR_CODE_INVALID_REQUEST, 0,
483 : : response_size, response);
484 : : }
485 : : /* DSP0277 1.3: reserve room for this Responder's AEADlimitOE. The size helper returns 0
486 : : * unless the negotiated secured message version is 1.3 or later. */
487 : 78 : opaque_key_exchange_rsp_size +=
488 : 78 : libspdm_get_opaque_data_aead_limit_element_size(spdm_context,
489 : : secured_message_version);
490 : : } else {
491 : : /* use response buffer to temporarily store opaque data */
492 : 2 : rsp_opaque_data = (uint8_t *)response;
493 : 2 : result = libspdm_key_exchange_rsp_opaque_data(
494 : 2 : spdm_context, spdm_request->header.spdm_version,
495 : 2 : spdm_request->header.param1, slot_id, spdm_request->session_policy,
496 : : req_opaque_data, opaque_data_length, rsp_opaque_data,
497 : : &opaque_key_exchange_rsp_size);
498 [ - + ]: 2 : if (!result) {
499 : 0 : return libspdm_generate_error_response(spdm_context,
500 : : SPDM_ERROR_CODE_UNSPECIFIED, 0,
501 : : response_size, response);
502 : : }
503 : : /*
504 : : * parse responder opaque data from integrator
505 : : * to get secured_message_version.
506 : : */
507 : 2 : status = libspdm_process_opaque_data_version_selection_data(
508 : : spdm_context, opaque_key_exchange_rsp_size,
509 : : rsp_opaque_data, &secured_message_version);
510 [ - + ]: 2 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
511 : 0 : return libspdm_generate_error_response(spdm_context,
512 : : SPDM_ERROR_CODE_UNSPECIFIED, 0,
513 : : response_size, response);
514 : : }
515 : : }
516 : : /* DSP0277 1.3: read the Requester's AEAD limit from the request (absent -> default 64).
517 : : * This is independent of whether the Responder builds default or custom response opaque
518 : : * data, so it runs for both paths. */
519 : 80 : status = libspdm_process_opaque_data_aead_limit(
520 : : spdm_context, secured_message_version, opaque_data_length, req_opaque_data,
521 : : &peer_aead_limit_exponent);
522 [ - + ]: 80 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
523 : 0 : return libspdm_generate_error_response(spdm_context,
524 : : SPDM_ERROR_CODE_INVALID_REQUEST, 0,
525 : : response_size, response);
526 : : }
527 : : } else {
528 : 0 : secured_message_version = 0;
529 : 0 : opaque_key_exchange_rsp_size = 0;
530 : 0 : req_opaque_data = NULL;
531 : : }
532 : :
533 : 80 : libspdm_reset_message_buffer_via_request_code(spdm_context, NULL,
534 : 80 : spdm_request->header.request_response_code);
535 : :
536 [ + + ]: 80 : if (libspdm_is_capabilities_flag_supported(
537 : : spdm_context, false,
538 : : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP,
539 : : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP)) {
540 : 8 : hmac_size = 0;
541 : : }
542 : :
543 : 80 : req_session_id = spdm_request->req_session_id;
544 : 80 : rsp_session_id = libspdm_allocate_rsp_session_id(spdm_context, false);
545 [ - + ]: 80 : if (rsp_session_id == ((INVALID_SESSION_ID & 0xFFFF0000) >> 16)) {
546 : 0 : return libspdm_generate_error_response(
547 : : spdm_context, SPDM_ERROR_CODE_SESSION_LIMIT_EXCEEDED, 0,
548 : : response_size, response);
549 : : }
550 : 80 : session_id = libspdm_generate_session_id(req_session_id, rsp_session_id);
551 : 80 : session_info = libspdm_assign_session_id(spdm_context, session_id, secured_message_version,
552 : : false);
553 [ - + ]: 80 : if (session_info == NULL) {
554 : 0 : return libspdm_generate_error_response(
555 : : spdm_context, SPDM_ERROR_CODE_SESSION_LIMIT_EXCEEDED, 0,
556 : : response_size, response);
557 : : }
558 : :
559 : : /* DSP0277 1.3: program the session's AEAD limit (min of local and peer) when secured message
560 : : * version 1.3 was negotiated. */
561 [ + + ]: 80 : if (libspdm_get_version_from_version_number(secured_message_version) >=
562 : : SECURED_SPDM_VERSION_13) {
563 : 4 : libspdm_apply_aead_limit_to_session(spdm_context, session_info,
564 : : peer_aead_limit_exponent);
565 : : }
566 : :
567 : 80 : total_size = sizeof(spdm_key_exchange_response_t) + rsp_key_exchange_size +
568 : 80 : measurement_summary_hash_size + sizeof(uint16_t) +
569 : 80 : opaque_key_exchange_rsp_size + signature_size + hmac_size;
570 : :
571 : 80 : LIBSPDM_ASSERT(*response_size >= total_size);
572 : 80 : *response_size = total_size;
573 : 80 : libspdm_zero_mem(response, *response_size);
574 : 80 : spdm_response = response;
575 : :
576 : 80 : spdm_response->header.spdm_version = spdm_request->header.spdm_version;
577 : 80 : spdm_response->header.request_response_code = SPDM_KEY_EXCHANGE_RSP;
578 : :
579 : : #if LIBSPDM_ENABLE_CAPABILITY_HBEAT_CAP
580 [ + + ]: 80 : if (libspdm_is_capabilities_flag_supported(
581 : : spdm_context, false,
582 : : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HBEAT_CAP,
583 : : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HBEAT_CAP)) {
584 : 4 : spdm_response->header.param1 = spdm_context->local_context.heartbeat_period;
585 : : } else
586 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_HBEAT_CAP */
587 : : {
588 : 76 : spdm_response->header.param1 = 0x00;
589 : : }
590 : :
591 : 80 : session_info->local_used_cert_chain_slot_id = slot_id;
592 : :
593 : : #if LIBSPDM_ENABLE_CAPABILITY_HBEAT_CAP
594 [ + + ]: 80 : if (libspdm_is_capabilities_flag_supported(
595 : : spdm_context, false,
596 : : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HBEAT_CAP,
597 : : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HBEAT_CAP)) {
598 : 4 : session_info->heartbeat_period = spdm_context->local_context.heartbeat_period;
599 : : } else
600 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_HBEAT_CAP */
601 : : {
602 : 76 : session_info->heartbeat_period = 0x00;
603 : : }
604 : :
605 : 80 : spdm_response->rsp_session_id = rsp_session_id;
606 : 80 : spdm_response->mut_auth_requested = 0;
607 : 80 : spdm_response->req_slot_id_param = 0;
608 : :
609 : : #if LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP
610 [ + + ]: 80 : if (libspdm_is_capabilities_flag_supported(
611 : : spdm_context, false, 0, SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MUT_AUTH_CAP)) {
612 : 56 : req_slot_id = 0;
613 : :
614 : : mut_auth_requested =
615 : 56 : libspdm_key_exchange_start_mut_auth(spdm_context,
616 : : session_id,
617 : 56 : spdm_context->connection_info.version,
618 : : slot_id,
619 : : &req_slot_id,
620 : 56 : spdm_request->session_policy,
621 : : opaque_data_length,
622 : : req_opaque_data,
623 : : &mandatory_mut_auth);
624 [ + + ]: 56 : if (mut_auth_requested != 0) {
625 : 52 : const bool req_pub_key_id_cap = libspdm_is_capabilities_flag_supported(
626 : : spdm_context, false, SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PUB_KEY_ID_CAP, 0);
627 : 52 : const bool req_mut_auth_cap = libspdm_is_capabilities_flag_supported(
628 : : spdm_context, false, SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MUT_AUTH_CAP, 0);
629 : 52 : const bool req_encap_cap = libspdm_is_capabilities_flag_supported(
630 : : spdm_context, false, SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCAP_CAP, 0);
631 : 52 : const bool need_encap =
632 : : (mut_auth_requested ==
633 [ + + + + ]: 52 : SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED_WITH_ENCAP_REQUEST) ||
634 : : (mut_auth_requested ==
635 : : SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED_WITH_GET_DIGESTS);
636 : :
637 [ + + ]: 52 : switch (mut_auth_requested) {
638 : 36 : case SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED:
639 : : case SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED_WITH_ENCAP_REQUEST:
640 : : case SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED_WITH_GET_DIGESTS:
641 : 36 : break;
642 : 16 : default:
643 : 16 : libspdm_free_session_id(spdm_context, session_id);
644 : 16 : return libspdm_generate_error_response(spdm_context,
645 : : SPDM_ERROR_CODE_UNSPECIFIED, 0,
646 : : response_size, response);
647 : : }
648 : :
649 [ + + + + ]: 36 : if (req_pub_key_id_cap &&
650 : : (mut_auth_requested != SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED)) {
651 : 2 : libspdm_free_session_id(spdm_context, session_id);
652 : 2 : return libspdm_generate_error_response(spdm_context,
653 : : SPDM_ERROR_CODE_UNSPECIFIED, 0,
654 : : response_size, response);
655 : : }
656 [ + + ]: 34 : if (!need_encap) {
657 [ + + + + ]: 20 : if (req_pub_key_id_cap ?
658 : : (req_slot_id != 0xf) : (req_slot_id >= SPDM_MAX_SLOT_COUNT)) {
659 : 6 : libspdm_free_session_id(spdm_context, session_id);
660 : 6 : return libspdm_generate_error_response(spdm_context,
661 : : SPDM_ERROR_CODE_UNSPECIFIED, 0,
662 : : response_size, response);
663 : : }
664 [ + + ]: 14 : } else if (req_slot_id != 0) {
665 : 4 : libspdm_free_session_id(spdm_context, session_id);
666 : 4 : return libspdm_generate_error_response(spdm_context,
667 : : SPDM_ERROR_CODE_UNSPECIFIED, 0,
668 : : response_size, response);
669 : : }
670 : :
671 : : /* If Integrator requires mutual authentication but Requester does not support mutual
672 : : * authentication, or Integrator requires the encapsulated mutual authentication flow
673 : : * and Requester does not support encapsulated messages, then return an error to
674 : : * Requester. If it is not required then the session is established without mutual
675 : : * authentication. */
676 [ + + - + : 24 : if (mandatory_mut_auth && (!req_mut_auth_cap || (need_encap && !req_encap_cap))) {
- - - - ]
677 [ + + ]: 4 : if (libspdm_get_connection_version(spdm_context) >= SPDM_MESSAGE_VERSION_13) {
678 : 2 : libspdm_free_session_id(spdm_context, session_id);
679 : 2 : return libspdm_generate_error_response(spdm_context,
680 : : SPDM_ERROR_CODE_INVALID_POLICY, 0,
681 : : response_size, response);
682 : : } else {
683 : 2 : libspdm_free_session_id(spdm_context, session_id);
684 : 2 : return libspdm_generate_error_response(spdm_context,
685 : : SPDM_ERROR_CODE_UNSPECIFIED, 0,
686 : : response_size, response);
687 : : }
688 : : }
689 : :
690 [ + + + + ]: 20 : if (req_mut_auth_cap && !need_encap) {
691 : 8 : spdm_response->mut_auth_requested = mut_auth_requested;
692 : 8 : spdm_response->req_slot_id_param = req_slot_id;
693 : : /* There is no encapsulated flow to retrieve the Requester's certificate chain,
694 : : * so the Responder already possesses it. Record the slot that the Requester is
695 : : * being told to sign FINISH with, as FINISH verification reads it.
696 : : * SlotIDParam carries 0xF for a provisioned public key, which libspdm
697 : : * records as 0xFF. */
698 [ + + ]: 8 : session_info->peer_used_cert_chain_slot_id =
699 : : req_pub_key_id_cap ? 0xFF : req_slot_id;
700 : : }
701 : : #if LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP
702 [ + + + - : 12 : else if (req_mut_auth_cap && need_encap && req_encap_cap) {
+ - ]
703 : 6 : spdm_response->mut_auth_requested = mut_auth_requested;
704 : 6 : session_info->peer_used_cert_chain_slot_id = req_slot_id;
705 : 6 : init_encap_state(spdm_context, session_info, mut_auth_requested, req_slot_id);
706 : : }
707 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP */
708 : : }
709 : : }
710 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP */
711 : :
712 [ - + ]: 48 : if (!libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, spdm_response->random_data)) {
713 : 0 : libspdm_free_session_id(spdm_context, session_id);
714 : 0 : return libspdm_generate_error_response(spdm_context,
715 : : SPDM_ERROR_CODE_UNSPECIFIED, 0,
716 : : response_size, response);
717 : : }
718 : :
719 : 48 : ptr = (void *)(spdm_response + 1);
720 [ - + ]: 48 : if (spdm_context->connection_info.algorithm.kem_alg != 0) {
721 : 0 : kem_context = libspdm_secured_message_kem_new(
722 : 0 : spdm_context->connection_info.version,
723 : : spdm_context->connection_info.algorithm.kem_alg, false);
724 [ # # ]: 0 : if (kem_context == NULL) {
725 : 0 : libspdm_free_session_id(spdm_context, session_id);
726 : 0 : return libspdm_generate_error_response(spdm_context,
727 : : SPDM_ERROR_CODE_UNSPECIFIED, 0,
728 : : response_size, response);
729 : : }
730 : :
731 : 0 : result = libspdm_secured_message_kem_encapsulate(
732 : : spdm_context->connection_info.algorithm.kem_alg,
733 : : kem_context,
734 : : (const uint8_t *)request + sizeof(spdm_key_exchange_request_t),
735 : : kem_encap_key_size, ptr, &kem_cipher_text_size, session_info->secured_message_context);
736 : 0 : libspdm_secured_message_kem_free(
737 : : spdm_context->connection_info.algorithm.kem_alg,
738 : : kem_context);
739 [ # # ]: 0 : if (!result) {
740 : 0 : libspdm_free_session_id(spdm_context, session_id);
741 : 0 : return libspdm_generate_error_response(spdm_context,
742 : : SPDM_ERROR_CODE_UNSPECIFIED, 0,
743 : : response_size, response);
744 : : }
745 : :
746 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "Calc Self cipher_text (0x%zx):\n", kem_cipher_text_size));
747 : 0 : LIBSPDM_INTERNAL_DUMP_HEX(ptr, kem_cipher_text_size);
748 : :
749 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "Calc peer encap_key (0x%zx):\n", kem_encap_key_size));
750 : 0 : LIBSPDM_INTERNAL_DUMP_HEX((const uint8_t *)request +
751 : : sizeof(spdm_key_exchange_request_t),
752 : : kem_encap_key_size);
753 : :
754 : 0 : ptr += kem_cipher_text_size;
755 : : } else {
756 : 48 : dhe_context = libspdm_secured_message_dhe_new(
757 : 48 : spdm_context->connection_info.version,
758 : 48 : spdm_context->connection_info.algorithm.dhe_named_group, false);
759 [ - + ]: 48 : if (dhe_context == NULL) {
760 : 0 : libspdm_free_session_id(spdm_context, session_id);
761 : 0 : return libspdm_generate_error_response(spdm_context,
762 : : SPDM_ERROR_CODE_UNSPECIFIED, 0,
763 : : response_size, response);
764 : : }
765 : :
766 : 48 : result = libspdm_secured_message_dhe_generate_key(
767 : 48 : spdm_context->connection_info.algorithm.dhe_named_group,
768 : : dhe_context, ptr, &dhe_key_size);
769 [ - + ]: 48 : if (!result) {
770 : 0 : libspdm_secured_message_dhe_free(
771 : 0 : spdm_context->connection_info.algorithm.dhe_named_group,
772 : : dhe_context);
773 : 0 : libspdm_free_session_id(spdm_context, session_id);
774 : 0 : return libspdm_generate_error_response(spdm_context,
775 : : SPDM_ERROR_CODE_UNSPECIFIED, 0,
776 : : response_size, response);
777 : : }
778 : :
779 : 48 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "Calc SelfKey (0x%zx):\n", dhe_key_size));
780 : 48 : LIBSPDM_INTERNAL_DUMP_HEX(ptr, dhe_key_size);
781 : :
782 : 48 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "Calc peer_key (0x%zx):\n", dhe_key_size));
783 : 48 : LIBSPDM_INTERNAL_DUMP_HEX((const uint8_t *)request +
784 : : sizeof(spdm_key_exchange_request_t),
785 : : dhe_key_size);
786 : :
787 : 48 : result = libspdm_secured_message_dhe_compute_key(
788 : 48 : spdm_context->connection_info.algorithm.dhe_named_group,
789 : : dhe_context,
790 : : (const uint8_t *)request + sizeof(spdm_key_exchange_request_t),
791 : : dhe_key_size, session_info->secured_message_context);
792 : 48 : libspdm_secured_message_dhe_free(
793 : 48 : spdm_context->connection_info.algorithm.dhe_named_group, dhe_context);
794 [ - + ]: 48 : if (!result) {
795 : 0 : libspdm_free_session_id(spdm_context, session_id);
796 : 0 : return libspdm_generate_error_response(spdm_context,
797 : : SPDM_ERROR_CODE_UNSPECIFIED, 0,
798 : : response_size, response);
799 : : }
800 : :
801 : 48 : ptr += dhe_key_size;
802 : : }
803 : :
804 : : #if LIBSPDM_ENABLE_CAPABILITY_MEAS_CAP
805 [ + + ]: 48 : if (libspdm_is_capabilities_flag_supported(
806 : 6 : spdm_context, false, 0, SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEAS_CAP) &&
807 [ + + ]: 6 : ((spdm_request->header.param1 == SPDM_REQUEST_TCB_COMPONENT_MEASUREMENT_HASH) ||
808 [ + - ]: 4 : (spdm_request->header.param1 == SPDM_REQUEST_ALL_MEASUREMENTS_HASH))) {
809 : 6 : result = libspdm_generate_measurement_summary_hash(
810 : : spdm_context,
811 : 6 : spdm_context->connection_info.version,
812 : : spdm_context->connection_info.algorithm.base_hash_algo,
813 : 6 : spdm_context->connection_info.algorithm.measurement_spec,
814 : : spdm_context->connection_info.algorithm.measurement_hash_algo,
815 : 6 : spdm_request->header.param1,
816 : : ptr,
817 : : measurement_summary_hash_size);
818 : :
819 [ - + ]: 6 : if (!result) {
820 : 0 : libspdm_free_session_id(spdm_context, session_id);
821 : 0 : return libspdm_generate_error_response(spdm_context,
822 : : SPDM_ERROR_CODE_UNSPECIFIED, 0,
823 : : response_size, response);
824 : : }
825 : : }
826 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_MEAS_CAP */
827 : :
828 : 48 : ptr += measurement_summary_hash_size;
829 : :
830 : 48 : libspdm_write_uint16(ptr, (uint16_t)opaque_key_exchange_rsp_size);
831 : 48 : ptr += sizeof(uint16_t);
832 : :
833 [ + - ]: 48 : if (opaque_key_exchange_rsp_size != 0) {
834 [ + + ]: 48 : if (use_default_opaque_data) {
835 : 46 : size_t version_selection_size =
836 : 46 : libspdm_get_opaque_data_version_selection_data_size(spdm_context);
837 : 46 : libspdm_build_opaque_data_version_selection_data(
838 : : spdm_context, secured_message_version, &version_selection_size, ptr);
839 : : /* DSP0277 1.3: advertise this Responder's own AEAD limit. opaque_key_exchange_rsp_size
840 : : * is the reserved opaque data capacity (version selection + AEAD limit); the append is a
841 : : * no-op unless the negotiated secured message version is 1.3 or later. */
842 : 46 : libspdm_build_opaque_data_aead_limit_element(
843 : : spdm_context, secured_message_version, &opaque_key_exchange_rsp_size, ptr);
844 : : } else {
845 : 2 : result = libspdm_key_exchange_rsp_opaque_data(
846 : 2 : spdm_context, spdm_request->header.spdm_version,
847 : 2 : spdm_request->header.param1, slot_id, spdm_request->session_policy,
848 : : req_opaque_data, opaque_data_length, ptr,
849 : : &opaque_key_exchange_rsp_size);
850 [ - + ]: 2 : if (!result) {
851 : 0 : libspdm_free_session_id(spdm_context, session_id);
852 : 0 : return libspdm_generate_error_response(spdm_context,
853 : : SPDM_ERROR_CODE_UNSPECIFIED, 0,
854 : : response_size, response);
855 : : }
856 : : }
857 : 48 : ptr += opaque_key_exchange_rsp_size;
858 : : }
859 : :
860 : 48 : status = libspdm_append_message_k(spdm_context, session_info, false, request, request_size);
861 [ - + ]: 48 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
862 : 0 : libspdm_free_session_id(spdm_context, session_id);
863 : 0 : return libspdm_generate_error_response(spdm_context,
864 : : SPDM_ERROR_CODE_UNSPECIFIED, 0,
865 : : response_size, response);
866 : : }
867 : :
868 : 48 : status = libspdm_append_message_k(spdm_context, session_info, false, spdm_response,
869 : 48 : (size_t)ptr - (size_t)spdm_response);
870 [ - + ]: 48 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
871 : 0 : libspdm_free_session_id(spdm_context, session_id);
872 : 0 : return libspdm_generate_error_response(spdm_context,
873 : : SPDM_ERROR_CODE_UNSPECIFIED, 0,
874 : : response_size, response);
875 : : }
876 : 48 : result = libspdm_generate_key_exchange_rsp_signature(
877 : : spdm_context, session_info, slot_id, ptr);
878 [ - + ]: 48 : if (!result) {
879 : 0 : libspdm_free_session_id(spdm_context, session_id);
880 : 0 : return libspdm_generate_error_response(
881 : : spdm_context, SPDM_ERROR_CODE_UNSPECIFIED,
882 : : 0, response_size, response);
883 : : }
884 : :
885 : 48 : status = libspdm_append_message_k(spdm_context, session_info, false, ptr, signature_size);
886 [ - + ]: 48 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
887 : 0 : libspdm_free_session_id(spdm_context, session_id);
888 : 0 : return libspdm_generate_error_response(spdm_context,
889 : : SPDM_ERROR_CODE_UNSPECIFIED, 0,
890 : : response_size, response);
891 : : }
892 : :
893 : 48 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "libspdm_generate_session_handshake_key[%x]\n", session_id));
894 : 48 : result = libspdm_calculate_th1_hash(spdm_context, session_info, false, th1_hash_data);
895 [ - + ]: 48 : if (!result) {
896 : 0 : libspdm_free_session_id(spdm_context, session_id);
897 : 0 : return libspdm_generate_error_response(spdm_context,
898 : : SPDM_ERROR_CODE_UNSPECIFIED, 0,
899 : : response_size, response);
900 : : }
901 : 48 : result = libspdm_generate_session_handshake_key(
902 : : session_info->secured_message_context, th1_hash_data);
903 [ - + ]: 48 : if (!result) {
904 : 0 : libspdm_free_session_id(spdm_context, session_id);
905 : 0 : return libspdm_generate_error_response(spdm_context,
906 : : SPDM_ERROR_CODE_UNSPECIFIED, 0,
907 : : response_size, response);
908 : : }
909 : :
910 : 48 : ptr += signature_size;
911 : :
912 [ + + ]: 48 : if (!libspdm_is_capabilities_flag_supported(
913 : : spdm_context, false,
914 : : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP,
915 : : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP)) {
916 : 40 : result = libspdm_generate_key_exchange_rsp_hmac(spdm_context, session_info, ptr);
917 [ - + ]: 40 : if (!result) {
918 : 0 : libspdm_free_session_id(spdm_context, session_id);
919 : 0 : return libspdm_generate_error_response(
920 : : spdm_context,
921 : : SPDM_ERROR_CODE_UNSPECIFIED,
922 : : 0, response_size, response);
923 : : }
924 : 40 : status = libspdm_append_message_k(spdm_context, session_info, false, ptr, hmac_size);
925 [ - + ]: 40 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
926 : 0 : libspdm_free_session_id(spdm_context, session_id);
927 : 0 : return libspdm_generate_error_response(
928 : : spdm_context, SPDM_ERROR_CODE_UNSPECIFIED,
929 : : 0, response_size, response);
930 : : }
931 : :
932 : 40 : ptr += hmac_size;
933 : : }
934 : :
935 : : #if LIBSPDM_ENABLE_CAPABILITY_EVENT_CAP
936 [ + + ]: 48 : if (spdm_request->header.spdm_version >= SPDM_MESSAGE_VERSION_13) {
937 [ + - ]: 2 : if ((spdm_request->session_policy &
938 : : SPDM_KEY_EXCHANGE_REQUEST_SESSION_POLICY_EVENT_ALL_POLICY) != 0) {
939 [ - + ]: 2 : if (!libspdm_event_subscribe(spdm_context, spdm_context->connection_info.version,
940 : : session_id, LIBSPDM_EVENT_SUBSCRIBE_ALL, 0, 0, NULL)) {
941 : 0 : libspdm_free_session_id(spdm_context, session_id);
942 : 0 : return libspdm_generate_error_response(spdm_context,
943 : : SPDM_ERROR_CODE_UNSPECIFIED, 0,
944 : : response_size, response);
945 : : }
946 : : }
947 : : }
948 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_EVENT_CAP */
949 : :
950 : 48 : session_info->mut_auth_requested = spdm_response->mut_auth_requested;
951 [ + + ]: 48 : if (spdm_request->header.spdm_version >= SPDM_MESSAGE_VERSION_12) {
952 : 8 : session_info->session_policy = spdm_request->session_policy;
953 : : }
954 : 48 : libspdm_set_session_state(spdm_context, session_id, LIBSPDM_SESSION_STATE_HANDSHAKING);
955 : :
956 : 48 : return LIBSPDM_STATUS_SUCCESS;
957 : : }
958 : :
959 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_KEY_EX_CAP */
|