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 : 42 : bool libspdm_verify_finish_req_hmac(libspdm_context_t *spdm_context,
13 : : libspdm_session_info_t *session_info,
14 : : const uint8_t *hmac, size_t hmac_size)
15 : : {
16 : : uint8_t hmac_data[LIBSPDM_MAX_HASH_SIZE];
17 : : size_t hash_size;
18 : : bool result;
19 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
20 : : uint8_t slot_id;
21 : : const uint8_t *cert_chain_buffer;
22 : : size_t cert_chain_buffer_size;
23 : : const uint8_t *mut_cert_chain_buffer;
24 : : size_t mut_cert_chain_buffer_size;
25 : : uint8_t *th_curr_data;
26 : : size_t th_curr_data_size;
27 : : libspdm_th_managed_buffer_t th_curr;
28 : : uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
29 : : #endif /* LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT */
30 : :
31 : 42 : hash_size = libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
32 : 42 : LIBSPDM_ASSERT(hmac_size == hash_size);
33 : :
34 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
35 : : slot_id = session_info->local_used_cert_chain_slot_id;
36 : : LIBSPDM_ASSERT((slot_id < SPDM_MAX_SLOT_COUNT) || (slot_id == 0xFF));
37 : : if (slot_id == 0xFF) {
38 : : result = libspdm_get_local_public_key_buffer(
39 : : spdm_context, (const void **)&cert_chain_buffer, &cert_chain_buffer_size);
40 : : if (!result) {
41 : : return false;
42 : : }
43 : : } else {
44 : : libspdm_get_local_cert_chain_buffer(
45 : : spdm_context, slot_id, (const void **)&cert_chain_buffer, &cert_chain_buffer_size);
46 : : }
47 : :
48 : : if (session_info->mut_auth_requested != 0) {
49 : : slot_id = session_info->peer_used_cert_chain_slot_id;
50 : : LIBSPDM_ASSERT((slot_id < SPDM_MAX_SLOT_COUNT) || (slot_id == 0xFF));
51 : : if (slot_id == 0xFF) {
52 : : result = libspdm_get_peer_public_key_buffer(
53 : : spdm_context, (const void **)&mut_cert_chain_buffer, &mut_cert_chain_buffer_size);
54 : : if (!result) {
55 : : return false;
56 : : }
57 : : } else {
58 : : libspdm_get_peer_cert_chain_buffer(
59 : : spdm_context, slot_id, (const void **)&mut_cert_chain_buffer,
60 : : &mut_cert_chain_buffer_size);
61 : : }
62 : : } else {
63 : : mut_cert_chain_buffer = NULL;
64 : : mut_cert_chain_buffer_size = 0;
65 : : }
66 : :
67 : : result = libspdm_calculate_th_for_finish(
68 : : spdm_context, session_info, cert_chain_buffer,
69 : : cert_chain_buffer_size, mut_cert_chain_buffer,
70 : : mut_cert_chain_buffer_size, &th_curr);
71 : : if (!result) {
72 : : return false;
73 : : }
74 : : th_curr_data = libspdm_get_managed_buffer(&th_curr);
75 : : th_curr_data_size = libspdm_get_managed_buffer_size(&th_curr);
76 : :
77 : : result = libspdm_hash_all (spdm_context->connection_info.algorithm.base_hash_algo,
78 : : th_curr_data, th_curr_data_size, hash_data);
79 : : if (!result) {
80 : : return false;
81 : : }
82 : :
83 : : result = libspdm_hmac_all_with_request_finished_key(
84 : : session_info->secured_message_context, hash_data, hash_size, hmac_data);
85 : : if (!result) {
86 : : return false;
87 : : }
88 : : #else
89 : 42 : result = libspdm_calculate_th_hmac_for_finish_req(
90 : : spdm_context, session_info, &hash_size, hmac_data);
91 [ - + ]: 42 : if (!result) {
92 : 0 : return false;
93 : : }
94 : : #endif /* LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT */
95 : 42 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "th_curr hmac - "));
96 : 42 : LIBSPDM_INTERNAL_DUMP_DATA(hmac_data, hash_size);
97 : 42 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "\n"));
98 : :
99 [ + + ]: 42 : if (!libspdm_consttime_is_mem_equal(hmac, hmac_data, hash_size)) {
100 : 4 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR, "!!! verify_finish_req_hmac - FAIL !!!\n"));
101 : 4 : return false;
102 : : }
103 : 38 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "!!! verify_finish_req_hmac - PASS !!!\n"));
104 : 38 : return true;
105 : : }
106 : :
107 : : #if LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP
108 : 28 : bool libspdm_verify_finish_req_signature(libspdm_context_t *spdm_context,
109 : : libspdm_session_info_t *session_info,
110 : : const void *sign_data,
111 : : size_t sign_data_size)
112 : : {
113 : : bool result;
114 : : void *context;
115 : : uint8_t slot_id;
116 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
117 : : const uint8_t *cert_chain_buffer;
118 : : size_t cert_chain_buffer_size;
119 : : const uint8_t *mut_cert_chain_buffer;
120 : : size_t mut_cert_chain_buffer_size;
121 : : uint8_t *th_curr_data;
122 : : size_t th_curr_data_size;
123 : : libspdm_th_managed_buffer_t th_curr;
124 : : #if LIBSPDM_CERT_PARSE_SUPPORT
125 : : const uint8_t *mut_cert_chain_data;
126 : : size_t mut_cert_chain_data_size;
127 : : const uint8_t *mut_cert_buffer;
128 : : size_t mut_cert_buffer_size;
129 : : #endif /* LIBSPDM_CERT_PARSE_SUPPORT */
130 : : #endif
131 : : #if ((LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT) && (LIBSPDM_DEBUG_BLOCK_ENABLE)) || \
132 : : !(LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT)
133 : : uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
134 : : #endif
135 : : #if !(LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT) || (LIBSPDM_DEBUG_PRINT_ENABLE)
136 : : size_t hash_size;
137 : :
138 : 28 : hash_size = libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
139 : : #endif
140 : :
141 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
142 : : slot_id = session_info->local_used_cert_chain_slot_id;
143 : : LIBSPDM_ASSERT((slot_id < SPDM_MAX_SLOT_COUNT) || (slot_id == 0xFF));
144 : : if (slot_id == 0xFF) {
145 : : result = libspdm_get_local_public_key_buffer(
146 : : spdm_context, (const void **)&cert_chain_buffer, &cert_chain_buffer_size);
147 : : if (!result) {
148 : : return false;
149 : : }
150 : : } else {
151 : : libspdm_get_local_cert_chain_buffer(
152 : : spdm_context, slot_id, (const void **)&cert_chain_buffer, &cert_chain_buffer_size);
153 : : }
154 : :
155 : : slot_id = session_info->peer_used_cert_chain_slot_id;
156 : : LIBSPDM_ASSERT((slot_id < SPDM_MAX_SLOT_COUNT) || (slot_id == 0xFF));
157 : : if (slot_id == 0xFF) {
158 : : result = libspdm_get_peer_public_key_buffer(
159 : : spdm_context, (const void **)&mut_cert_chain_buffer, &mut_cert_chain_buffer_size);
160 : : if (!result) {
161 : : return false;
162 : : }
163 : : } else {
164 : : libspdm_get_peer_cert_chain_buffer(
165 : : spdm_context, slot_id, (const void **)&mut_cert_chain_buffer,
166 : : &mut_cert_chain_buffer_size);
167 : : }
168 : :
169 : : result = libspdm_calculate_th_for_finish(
170 : : spdm_context, session_info, cert_chain_buffer,
171 : : cert_chain_buffer_size, mut_cert_chain_buffer,
172 : : mut_cert_chain_buffer_size, &th_curr);
173 : : if (!result) {
174 : : return false;
175 : : }
176 : : th_curr_data = libspdm_get_managed_buffer(&th_curr);
177 : : th_curr_data_size = libspdm_get_managed_buffer_size(&th_curr);
178 : :
179 : : /* Debug code only - required for debug print of th_curr below*/
180 : : LIBSPDM_DEBUG_CODE(
181 : : if (!libspdm_hash_all(
182 : : spdm_context->connection_info.algorithm.base_hash_algo,
183 : : th_curr_data, th_curr_data_size, hash_data)) {
184 : : return false;
185 : : }
186 : : );
187 : : #else
188 : 28 : result = libspdm_calculate_th_hash_for_finish(
189 : : spdm_context, session_info, &hash_size, hash_data);
190 [ - + ]: 28 : if (!result) {
191 : 0 : return false;
192 : : }
193 : : #endif /* LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT */
194 : 28 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "th_curr hash - "));
195 : 28 : LIBSPDM_INTERNAL_DUMP_DATA(hash_data, hash_size);
196 : 28 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "\n"));
197 : :
198 : 28 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "signature - "));
199 : 28 : LIBSPDM_INTERNAL_DUMP_DATA(sign_data, sign_data_size);
200 : 28 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "\n"));
201 : :
202 : 28 : slot_id = session_info->peer_used_cert_chain_slot_id;
203 : 28 : LIBSPDM_ASSERT((slot_id < SPDM_MAX_SLOT_COUNT) || (slot_id == 0xFF));
204 : :
205 [ + + ]: 28 : if (slot_id == 0xFF) {
206 [ - + ]: 2 : if (spdm_context->connection_info.algorithm.req_pqc_asym_alg != 0) {
207 : 0 : result = libspdm_req_pqc_asym_get_public_key_from_der(
208 : : spdm_context->connection_info.algorithm.req_pqc_asym_alg,
209 : 0 : spdm_context->local_context.peer_public_key_provision,
210 : : spdm_context->local_context.peer_public_key_provision_size,
211 : : &context);
212 : : } else {
213 : 2 : result = libspdm_req_asym_get_public_key_from_der(
214 : 2 : spdm_context->connection_info.algorithm.req_base_asym_alg,
215 : 2 : spdm_context->local_context.peer_public_key_provision,
216 : : spdm_context->local_context.peer_public_key_provision_size,
217 : : &context);
218 : : }
219 [ - + ]: 2 : if (!result) {
220 : 0 : return false;
221 : : }
222 : : } else {
223 : : #if (LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT) && (LIBSPDM_CERT_PARSE_SUPPORT)
224 : : /* Get leaf cert from cert chain*/
225 : : libspdm_get_peer_cert_chain_data(spdm_context,
226 : : slot_id,
227 : : (const void **)&mut_cert_chain_data,
228 : : &mut_cert_chain_data_size);
229 : :
230 : : result = libspdm_x509_get_cert_from_cert_chain(mut_cert_chain_data,
231 : : mut_cert_chain_data_size, -1,
232 : : &mut_cert_buffer,
233 : : &mut_cert_buffer_size);
234 : : if (!result) {
235 : : return false;
236 : : }
237 : :
238 : : if (spdm_context->connection_info.algorithm.req_pqc_asym_alg != 0) {
239 : : result = libspdm_req_pqc_asym_get_public_key_from_x509(
240 : : spdm_context->connection_info.algorithm.req_pqc_asym_alg,
241 : : mut_cert_buffer, mut_cert_buffer_size, &context);
242 : : } else {
243 : : result = libspdm_req_asym_get_public_key_from_x509(
244 : : spdm_context->connection_info.algorithm.req_base_asym_alg,
245 : : mut_cert_buffer, mut_cert_buffer_size, &context);
246 : : }
247 : : if (!result) {
248 : : return false;
249 : : }
250 : : #elif LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
251 : : return false;
252 : : #else
253 : 26 : context = spdm_context->connection_info.peer_used_cert_chain[slot_id].leaf_cert_public_key;
254 [ + + ]: 26 : if (context == NULL) {
255 : : /* No certificate chain has been recorded for the slot. */
256 : 2 : return false;
257 : : }
258 : : #endif
259 : : }
260 : :
261 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
262 : : if (spdm_context->connection_info.algorithm.req_pqc_asym_alg != 0) {
263 : : result = libspdm_req_pqc_asym_verify(
264 : : spdm_context->connection_info.version, SPDM_FINISH,
265 : : spdm_context->connection_info.algorithm.req_pqc_asym_alg,
266 : : spdm_context->connection_info.algorithm.base_hash_algo,
267 : : context, th_curr_data, th_curr_data_size, sign_data, sign_data_size);
268 : : libspdm_req_pqc_asym_free(spdm_context->connection_info.algorithm.req_pqc_asym_alg, context);
269 : : } else {
270 : : result = libspdm_req_asym_verify_ex(
271 : : spdm_context->connection_info.version, SPDM_FINISH,
272 : : spdm_context->connection_info.algorithm.req_base_asym_alg,
273 : : spdm_context->connection_info.algorithm.base_hash_algo,
274 : : context, th_curr_data, th_curr_data_size, sign_data, sign_data_size,
275 : : &spdm_context->spdm_10_11_verify_signature_endian);
276 : : libspdm_req_asym_free(spdm_context->connection_info.algorithm.req_base_asym_alg, context);
277 : : }
278 : : #else
279 [ - + ]: 26 : if (spdm_context->connection_info.algorithm.req_pqc_asym_alg != 0) {
280 : 0 : result = libspdm_req_pqc_asym_verify_hash(
281 : 0 : spdm_context->connection_info.version, SPDM_FINISH,
282 : : spdm_context->connection_info.algorithm.req_pqc_asym_alg,
283 : : spdm_context->connection_info.algorithm.base_hash_algo,
284 : : context, hash_data, hash_size, sign_data, sign_data_size);
285 [ # # ]: 0 : if (slot_id == 0xFF) {
286 : 0 : libspdm_req_pqc_asym_free(spdm_context->connection_info.algorithm.req_pqc_asym_alg, context);
287 : : }
288 : : } else {
289 : 26 : result = libspdm_req_asym_verify_hash_ex(
290 : 26 : spdm_context->connection_info.version, SPDM_FINISH,
291 : 26 : spdm_context->connection_info.algorithm.req_base_asym_alg,
292 : : spdm_context->connection_info.algorithm.base_hash_algo,
293 : : context, hash_data, hash_size, sign_data, sign_data_size,
294 : : &spdm_context->spdm_10_11_verify_signature_endian);
295 [ + + ]: 26 : if (slot_id == 0xFF) {
296 : 2 : libspdm_req_asym_free(spdm_context->connection_info.algorithm.req_base_asym_alg, context);
297 : : }
298 : : }
299 : : #endif /* LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT */
300 : :
301 [ + + ]: 26 : if (!result) {
302 : 10 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR, "!!! VerifyFinishSignature - FAIL !!!\n"));
303 : 10 : return false;
304 : : }
305 : 16 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "!!! VerifyFinishSignature - PASS !!!\n"));
306 : :
307 : 16 : return true;
308 : : }
309 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP */
310 : :
311 : 30 : bool libspdm_generate_finish_rsp_hmac(libspdm_context_t *spdm_context,
312 : : libspdm_session_info_t *session_info,
313 : : uint8_t *hmac)
314 : : {
315 : : uint8_t hmac_data[LIBSPDM_MAX_HASH_SIZE];
316 : : size_t hash_size;
317 : : bool result;
318 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
319 : : uint8_t slot_id;
320 : : const uint8_t *cert_chain_buffer;
321 : : size_t cert_chain_buffer_size;
322 : : const uint8_t *mut_cert_chain_buffer;
323 : : size_t mut_cert_chain_buffer_size;
324 : : uint8_t *th_curr_data;
325 : : size_t th_curr_data_size;
326 : : libspdm_th_managed_buffer_t th_curr;
327 : : uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
328 : : #endif
329 : :
330 : 30 : hash_size = libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
331 : :
332 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
333 : : slot_id = session_info->local_used_cert_chain_slot_id;
334 : : LIBSPDM_ASSERT((slot_id < SPDM_MAX_SLOT_COUNT) || (slot_id == 0xFF));
335 : : if (slot_id == 0xFF) {
336 : : result = libspdm_get_local_public_key_buffer(
337 : : spdm_context, (const void **)&cert_chain_buffer, &cert_chain_buffer_size);
338 : : if (!result) {
339 : : return false;
340 : : }
341 : : } else {
342 : : libspdm_get_local_cert_chain_buffer(
343 : : spdm_context, slot_id, (const void **)&cert_chain_buffer, &cert_chain_buffer_size);
344 : : }
345 : :
346 : : if (session_info->mut_auth_requested != 0) {
347 : : slot_id = session_info->peer_used_cert_chain_slot_id;
348 : : LIBSPDM_ASSERT((slot_id < SPDM_MAX_SLOT_COUNT) || (slot_id == 0xFF));
349 : : if (slot_id == 0xFF) {
350 : : result = libspdm_get_peer_public_key_buffer(
351 : : spdm_context, (const void **)&mut_cert_chain_buffer, &mut_cert_chain_buffer_size);
352 : : if (!result) {
353 : : return false;
354 : : }
355 : : } else {
356 : : libspdm_get_peer_cert_chain_buffer(
357 : : spdm_context, slot_id, (const void **)&mut_cert_chain_buffer,
358 : : &mut_cert_chain_buffer_size);
359 : : }
360 : : } else {
361 : : mut_cert_chain_buffer = NULL;
362 : : mut_cert_chain_buffer_size = 0;
363 : : }
364 : :
365 : : result = libspdm_calculate_th_for_finish(
366 : : spdm_context, session_info, cert_chain_buffer,
367 : : cert_chain_buffer_size, mut_cert_chain_buffer,
368 : : mut_cert_chain_buffer_size, &th_curr);
369 : : if (!result) {
370 : : return false;
371 : : }
372 : : th_curr_data = libspdm_get_managed_buffer(&th_curr);
373 : : th_curr_data_size = libspdm_get_managed_buffer_size(&th_curr);
374 : :
375 : : result = libspdm_hash_all (spdm_context->connection_info.algorithm.base_hash_algo,
376 : : th_curr_data, th_curr_data_size, hash_data);
377 : : if (!result) {
378 : : return false;
379 : : }
380 : :
381 : : result = libspdm_hmac_all_with_response_finished_key(
382 : : session_info->secured_message_context, hash_data, hash_size, hmac_data);
383 : : if (!result) {
384 : : return false;
385 : : }
386 : : #else
387 : 30 : result = libspdm_calculate_th_hmac_for_finish_rsp(
388 : : spdm_context, session_info, &hash_size, hmac_data);
389 [ - + ]: 30 : if (!result) {
390 : 0 : return false;
391 : : }
392 : : #endif
393 : 30 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "th_curr hmac - "));
394 : 30 : LIBSPDM_INTERNAL_DUMP_DATA(hmac_data, hash_size);
395 : 30 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "\n"));
396 : :
397 : 30 : libspdm_copy_mem(hmac, hash_size, hmac_data, hash_size);
398 : :
399 : 30 : return true;
400 : : }
401 : :
402 : 88 : libspdm_return_t libspdm_get_response_finish(libspdm_context_t *spdm_context, size_t request_size,
403 : : const void *request,
404 : : size_t *response_size,
405 : : void *response)
406 : : {
407 : : uint32_t session_id;
408 : : bool result;
409 : : uint32_t hmac_size;
410 : : uint32_t signature_size;
411 : : uint8_t req_slot_id;
412 : : const spdm_finish_request_t *spdm_request;
413 : : spdm_finish_response_t *spdm_response;
414 : : libspdm_session_info_t *session_info;
415 : : uint8_t th2_hash_data[LIBSPDM_MAX_HASH_SIZE];
416 : : libspdm_return_t status;
417 : : libspdm_session_state_t session_state;
418 : : uint8_t *ptr;
419 : : size_t opaque_data_entry_size;
420 : : const uint8_t *req_opaque_data;
421 : : size_t req_opaque_data_size;
422 : : uint8_t *opaque_data;
423 : : size_t opaque_data_size;
424 : :
425 : 88 : spdm_request = request;
426 : :
427 : : /* -=[Check Parameters Phase]=- */
428 : 88 : LIBSPDM_ASSERT(spdm_request->header.request_response_code == SPDM_FINISH);
429 : :
430 [ - + ]: 88 : if (libspdm_get_connection_version(spdm_context) < SPDM_MESSAGE_VERSION_11) {
431 : 0 : return libspdm_generate_error_response(spdm_context,
432 : : SPDM_ERROR_CODE_UNSUPPORTED_REQUEST,
433 : : SPDM_FINISH,
434 : : response_size, response);
435 : : }
436 : :
437 [ - + ]: 88 : if (spdm_request->header.spdm_version != libspdm_get_connection_version(spdm_context)) {
438 : 0 : return libspdm_generate_error_response(spdm_context,
439 : : SPDM_ERROR_CODE_VERSION_MISMATCH, 0,
440 : : response_size, response);
441 : : }
442 [ + + ]: 88 : if (spdm_context->response_state != LIBSPDM_RESPONSE_STATE_NORMAL) {
443 : 8 : return libspdm_responder_handle_response_state(
444 : : spdm_context,
445 : 8 : spdm_request->header.request_response_code,
446 : : response_size, response);
447 : : }
448 [ + + ]: 80 : if (!libspdm_is_capabilities_flag_supported(
449 : : spdm_context, false,
450 : : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP,
451 : : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP)) {
452 : 4 : return libspdm_generate_error_response(
453 : : spdm_context, SPDM_ERROR_CODE_UNSUPPORTED_REQUEST,
454 : : SPDM_FINISH, response_size, response);
455 : : }
456 [ + + ]: 76 : if (spdm_context->connection_info.connection_state < LIBSPDM_CONNECTION_STATE_NEGOTIATED) {
457 : 4 : return libspdm_generate_error_response(spdm_context,
458 : : SPDM_ERROR_CODE_UNEXPECTED_REQUEST,
459 : : 0, response_size, response);
460 : : }
461 [ + + ]: 72 : if (!libspdm_is_capabilities_flag_supported(
462 : : spdm_context, false,
463 : : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP,
464 : : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP)) {
465 : : /* No handshake in clear, then it must be in a session.*/
466 [ + + ]: 10 : if (!spdm_context->last_spdm_request_session_id_valid) {
467 [ - + ]: 2 : if (libspdm_get_connection_version(spdm_context) >= SPDM_MESSAGE_VERSION_12) {
468 : 0 : return libspdm_generate_error_response(
469 : : spdm_context, SPDM_ERROR_CODE_SESSION_REQUIRED, 0, response_size, response);
470 : : } else {
471 : 2 : return libspdm_generate_error_response(
472 : : spdm_context, SPDM_ERROR_CODE_UNSPECIFIED, 0, response_size, response);
473 : : }
474 : : }
475 : : } else {
476 : : /* handshake in clear, then it must not be in a session.*/
477 [ + + ]: 62 : if (spdm_context->last_spdm_request_session_id_valid) {
478 [ - + ]: 2 : if (libspdm_get_connection_version(spdm_context) >= SPDM_MESSAGE_VERSION_12) {
479 : 0 : return libspdm_generate_error_response(
480 : : spdm_context, SPDM_ERROR_CODE_SESSION_REQUIRED, 0, response_size, response);
481 : : } else {
482 : 2 : return libspdm_generate_error_response(
483 : : spdm_context, SPDM_ERROR_CODE_UNSPECIFIED, 0, response_size, response);
484 : : }
485 : : }
486 : : }
487 [ + + ]: 68 : if (spdm_context->last_spdm_request_session_id_valid) {
488 : 8 : session_id = spdm_context->last_spdm_request_session_id;
489 : : } else {
490 : 60 : session_id = spdm_context->latest_session_id;
491 : : }
492 : 68 : session_info = libspdm_get_session_info_via_session_id(spdm_context, session_id);
493 [ + + ]: 68 : if (session_info == NULL) {
494 [ + - ]: 2 : if (libspdm_get_connection_version(spdm_context) >= SPDM_MESSAGE_VERSION_12) {
495 : 2 : return libspdm_generate_error_response(spdm_context,
496 : : SPDM_ERROR_CODE_SESSION_REQUIRED, 0,
497 : : response_size, response);
498 : : } else {
499 : 0 : return libspdm_generate_error_response(spdm_context,
500 : : SPDM_ERROR_CODE_UNSPECIFIED, 0,
501 : : response_size, response);
502 : : }
503 : : }
504 [ + + ]: 66 : if (session_info->use_psk) {
505 : 2 : return libspdm_generate_error_response(spdm_context,
506 : : SPDM_ERROR_CODE_UNEXPECTED_REQUEST, 0,
507 : : response_size, response);
508 : : }
509 : 64 : session_state = libspdm_secured_message_get_session_state(
510 : : session_info->secured_message_context);
511 [ + + ]: 64 : if (session_state != LIBSPDM_SESSION_STATE_HANDSHAKING) {
512 : 2 : return libspdm_generate_error_response(spdm_context,
513 : : SPDM_ERROR_CODE_UNEXPECTED_REQUEST, 0,
514 : : response_size, response);
515 : : }
516 : :
517 [ + + ]: 62 : if (((session_info->mut_auth_requested == 0) &&
518 [ + - ]: 28 : ((spdm_request->header.param1 & SPDM_FINISH_REQUEST_ATTRIBUTES_SIGNATURE_INCLUDED) !=
519 : 62 : 0)) ||
520 [ + + ]: 62 : ((session_info->mut_auth_requested != 0) &&
521 [ - + ]: 34 : ((spdm_request->header.param1 & SPDM_FINISH_REQUEST_ATTRIBUTES_SIGNATURE_INCLUDED) ==
522 : : 0))) {
523 : 0 : return libspdm_generate_error_response(spdm_context,
524 : : SPDM_ERROR_CODE_INVALID_REQUEST, 0,
525 : : response_size, response);
526 : : }
527 : :
528 : 62 : hmac_size = libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
529 : 62 : signature_size = 0;
530 : : #if LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP
531 [ + + ]: 62 : if (session_info->mut_auth_requested != 0) {
532 [ - + ]: 34 : if (spdm_context->connection_info.algorithm.req_pqc_asym_alg != 0) {
533 : 0 : signature_size = libspdm_get_req_pqc_asym_signature_size(
534 : : spdm_context->connection_info.algorithm.req_pqc_asym_alg);
535 : : } else {
536 : 34 : signature_size = libspdm_get_req_asym_signature_size(
537 : 34 : spdm_context->connection_info.algorithm.req_base_asym_alg);
538 : : }
539 : : }
540 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP */
541 : :
542 : 62 : ptr = (uint8_t *)(size_t)spdm_request + sizeof(spdm_finish_request_t);
543 [ + + ]: 62 : if (libspdm_get_connection_version(spdm_context) >= SPDM_MESSAGE_VERSION_14) {
544 [ - + ]: 6 : if (request_size < sizeof(spdm_finish_request_t) + sizeof(uint16_t)) {
545 : 0 : return libspdm_generate_error_response(spdm_context,
546 : : SPDM_ERROR_CODE_INVALID_REQUEST, 0,
547 : : response_size, response);
548 : : }
549 : 6 : req_opaque_data_size = libspdm_read_uint16((const uint8_t *)request +
550 : : sizeof(spdm_finish_request_t));
551 : 6 : ptr += sizeof(uint16_t);
552 [ - + ]: 6 : if (req_opaque_data_size > SPDM_MAX_OPAQUE_DATA_SIZE) {
553 : 0 : return libspdm_generate_error_response(spdm_context,
554 : : SPDM_ERROR_CODE_INVALID_REQUEST, 0,
555 : : response_size, response);
556 : : }
557 : 6 : if (request_size < sizeof(spdm_finish_request_t) +
558 [ - + ]: 6 : sizeof(uint16_t) + req_opaque_data_size) {
559 : 0 : return libspdm_generate_error_response(spdm_context,
560 : : SPDM_ERROR_CODE_INVALID_REQUEST, 0,
561 : : response_size, response);
562 : : }
563 : 6 : req_opaque_data = ptr;
564 : 6 : ptr += req_opaque_data_size;
565 : 6 : opaque_data_entry_size = sizeof(uint16_t) + req_opaque_data_size;
566 : : } else {
567 : 56 : opaque_data_entry_size = 0;
568 : : }
569 : :
570 : 62 : if (request_size < sizeof(spdm_finish_request_t) + opaque_data_entry_size +
571 [ + + ]: 62 : signature_size + hmac_size) {
572 : 2 : return libspdm_generate_error_response(spdm_context,
573 : : SPDM_ERROR_CODE_INVALID_REQUEST, 0,
574 : : response_size, response);
575 : : }
576 : 60 : request_size = sizeof(spdm_finish_request_t) + opaque_data_entry_size +
577 : 60 : signature_size + hmac_size;
578 : :
579 : 60 : req_slot_id = 0;
580 [ + + ]: 60 : if ((spdm_request->header.param1 & SPDM_FINISH_REQUEST_ATTRIBUTES_SIGNATURE_INCLUDED) != 0) {
581 : 34 : req_slot_id = spdm_request->header.param2;
582 [ + + + + ]: 34 : if ((req_slot_id != 0xFF) && (req_slot_id >= SPDM_MAX_SLOT_COUNT)) {
583 : 2 : return libspdm_generate_error_response(spdm_context,
584 : : SPDM_ERROR_CODE_INVALID_REQUEST, 0,
585 : : response_size, response);
586 : : }
587 : :
588 : : #if LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP
589 : : /* Both of the mut_auth_requested values checked below require the encapsulated flow. */
590 [ + - ]: 32 : if (libspdm_is_capabilities_flag_supported(
591 : : spdm_context, false,
592 : : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP,
593 : : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP)) {
594 [ + + ]: 32 : if (((session_info->mut_auth_requested ==
595 : 26 : SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED_WITH_ENCAP_REQUEST) ||
596 [ - + ]: 26 : (session_info->mut_auth_requested ==
597 : 6 : SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED_WITH_GET_DIGESTS)) &&
598 [ + + ]: 6 : (req_slot_id != session_info->encap_context.mut_auth_req_slot_id)) {
599 : 2 : return libspdm_generate_error_response(spdm_context,
600 : : SPDM_ERROR_CODE_INVALID_REQUEST, 0,
601 : : response_size, response);
602 : : }
603 : : }
604 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP */
605 : : }
606 : :
607 : 56 : libspdm_reset_message_buffer_via_request_code(spdm_context, session_info,
608 : 56 : spdm_request->header.request_response_code);
609 : :
610 : 56 : status = libspdm_append_message_f(spdm_context, session_info, false, request,
611 : 56 : request_size - (signature_size + hmac_size));
612 [ + + ]: 56 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
613 : 2 : return libspdm_generate_error_response(spdm_context,
614 : : SPDM_ERROR_CODE_UNSPECIFIED, 0,
615 : : response_size, response);
616 : : }
617 : : #if LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP
618 [ + + ]: 54 : if (session_info->mut_auth_requested != 0) {
619 : 28 : result = libspdm_verify_finish_req_signature(
620 : : spdm_context, session_info,
621 : : ptr, signature_size);
622 [ + + ]: 28 : if (!result) {
623 [ + - ]: 12 : if ((spdm_context->handle_error_return_policy &
624 : : LIBSPDM_DATA_HANDLE_ERROR_RETURN_POLICY_DROP_ON_DECRYPT_ERROR) == 0) {
625 : 12 : return libspdm_generate_error_response(
626 : : spdm_context, SPDM_ERROR_CODE_DECRYPT_ERROR, 0,
627 : : response_size, response);
628 : : } else {
629 : : /**
630 : : * just ignore this message
631 : : * return UNSUPPORTED and clear response_size to continue the dispatch without send response.
632 : : **/
633 : 0 : *response_size = 0;
634 : 0 : return LIBSPDM_STATUS_UNSUPPORTED_CAP;
635 : : }
636 : : }
637 : 16 : status = libspdm_append_message_f(
638 : : spdm_context, session_info, false,
639 : : ptr, signature_size);
640 [ - + ]: 16 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
641 : 0 : return libspdm_generate_error_response(
642 : : spdm_context, SPDM_ERROR_CODE_UNSPECIFIED,
643 : : 0, response_size, response);
644 : : }
645 : : }
646 : 42 : ptr += signature_size;
647 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP */
648 : :
649 : 42 : result = libspdm_verify_finish_req_hmac(
650 : : spdm_context, session_info, ptr, hmac_size);
651 [ + + ]: 42 : if (!result) {
652 [ + - ]: 4 : if ((spdm_context->handle_error_return_policy &
653 : : LIBSPDM_DATA_HANDLE_ERROR_RETURN_POLICY_DROP_ON_DECRYPT_ERROR) == 0) {
654 : 4 : return libspdm_generate_error_response(
655 : : spdm_context, SPDM_ERROR_CODE_DECRYPT_ERROR, 0,
656 : : response_size, response);
657 : : } else {
658 : : /**
659 : : * just ignore this message
660 : : * return UNSUPPORTED and clear response_size to continue the dispatch without send response
661 : : **/
662 : 0 : *response_size = 0;
663 : 0 : return LIBSPDM_STATUS_UNSUPPORTED_CAP;
664 : : }
665 : : }
666 : :
667 : 38 : status = libspdm_append_message_f(spdm_context, session_info, false,
668 : : ptr, hmac_size);
669 [ - + ]: 38 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
670 : 0 : return libspdm_generate_error_response(spdm_context,
671 : : SPDM_ERROR_CODE_UNSPECIFIED, 0,
672 : : response_size, response);
673 : : }
674 : :
675 [ + + ]: 38 : if (!libspdm_is_capabilities_flag_supported(
676 : : spdm_context, false,
677 : : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP,
678 : : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP)) {
679 : 8 : hmac_size = 0;
680 : : }
681 : :
682 [ + + ]: 38 : if (libspdm_get_connection_version(spdm_context) >= SPDM_MESSAGE_VERSION_14) {
683 : 6 : opaque_data_entry_size = sizeof(uint16_t);
684 : : } else {
685 : 32 : opaque_data_entry_size = 0;
686 : : }
687 : :
688 : : /* response_size should be large enough to hold a finish response without opaque data. */
689 : 38 : LIBSPDM_ASSERT(*response_size >= sizeof(spdm_finish_response_t) +
690 : : opaque_data_entry_size + hmac_size);
691 : 38 : libspdm_zero_mem(response, *response_size);
692 : 38 : spdm_response = response;
693 : :
694 : 38 : spdm_response->header.spdm_version = spdm_request->header.spdm_version;
695 : 38 : spdm_response->header.request_response_code = SPDM_FINISH_RSP;
696 : 38 : spdm_response->header.param1 = 0;
697 : 38 : spdm_response->header.param2 = 0;
698 : :
699 : 38 : ptr = (uint8_t *)spdm_response + sizeof(spdm_finish_response_t);
700 [ + + ]: 38 : if (libspdm_get_connection_version(spdm_context) >= SPDM_MESSAGE_VERSION_14) {
701 : 6 : opaque_data_size = *response_size - sizeof(spdm_finish_response_t) -
702 : 6 : hmac_size - opaque_data_entry_size;
703 : 6 : opaque_data = ptr + opaque_data_entry_size;
704 : :
705 [ + + ]: 6 : if ((spdm_context->connection_info.algorithm.other_params_support &
706 : : SPDM_ALGORITHMS_OPAQUE_DATA_FORMAT_MASK) == SPDM_ALGORITHMS_OPAQUE_DATA_FORMAT_NONE) {
707 : 2 : opaque_data_size = 0;
708 : : } else {
709 : 4 : result = libspdm_finish_rsp_opaque_data(
710 : 4 : spdm_context, session_id, spdm_request->header.spdm_version,
711 : : req_slot_id, req_opaque_data, req_opaque_data_size,
712 : : opaque_data, &opaque_data_size);
713 [ - + ]: 4 : if (!result){
714 : 0 : return libspdm_generate_error_response(spdm_context,
715 : : SPDM_ERROR_CODE_UNSPECIFIED, 0,
716 : : response_size, response);
717 : : }
718 : : }
719 : :
720 : 6 : libspdm_write_uint16(ptr, (uint16_t)opaque_data_size);
721 : 6 : opaque_data_entry_size = sizeof(uint16_t) + opaque_data_size;
722 : 6 : ptr += opaque_data_entry_size;
723 : : }
724 : :
725 : 38 : *response_size = sizeof(spdm_finish_response_t) + opaque_data_entry_size + hmac_size;
726 : :
727 : 38 : status = libspdm_append_message_f(spdm_context, session_info, false, spdm_response,
728 : 38 : (size_t)ptr - (size_t)spdm_response);
729 [ - + ]: 38 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
730 : 0 : return libspdm_generate_error_response(spdm_context,
731 : : SPDM_ERROR_CODE_UNSPECIFIED, 0,
732 : : response_size, response);
733 : : }
734 : :
735 [ + + ]: 38 : if (libspdm_is_capabilities_flag_supported(
736 : : spdm_context, false,
737 : : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP,
738 : : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP)) {
739 : 30 : result = libspdm_generate_finish_rsp_hmac(
740 : : spdm_context, session_info, ptr);
741 [ - + ]: 30 : if (!result) {
742 : 0 : return libspdm_generate_error_response(
743 : : spdm_context,
744 : : SPDM_ERROR_CODE_UNSPECIFIED,
745 : : 0, response_size, response);
746 : : }
747 : :
748 : 30 : status = libspdm_append_message_f(
749 : : spdm_context, session_info, false,
750 : : ptr, hmac_size);
751 [ - + ]: 30 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
752 : 0 : return libspdm_generate_error_response(
753 : : spdm_context, SPDM_ERROR_CODE_UNSPECIFIED,
754 : : 0, response_size, response);
755 : : }
756 : : }
757 : :
758 : 38 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "libspdm_generate_session_data_key[%x]\n", session_id));
759 : 38 : result = libspdm_calculate_th2_hash(spdm_context, session_info, false, th2_hash_data);
760 [ - + ]: 38 : if (!result) {
761 : 0 : return libspdm_generate_error_response(spdm_context,
762 : : SPDM_ERROR_CODE_UNSPECIFIED, 0,
763 : : response_size, response);
764 : : }
765 : 38 : result = libspdm_generate_session_data_key(
766 : : session_info->secured_message_context, th2_hash_data);
767 [ - + ]: 38 : if (!result) {
768 : 0 : return libspdm_generate_error_response(spdm_context,
769 : : SPDM_ERROR_CODE_UNSPECIFIED, 0,
770 : : response_size, response);
771 : : }
772 : :
773 : : #if LIBSPDM_ENABLE_CAPABILITY_HBEAT_CAP
774 [ + + + - ]: 40 : if ((spdm_context->local_context.heartbeat_period != 0) &&
775 : 2 : libspdm_is_capabilities_flag_supported(
776 : : spdm_context, false,
777 : : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HBEAT_CAP,
778 : : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HBEAT_CAP)) {
779 : 2 : result = libspdm_start_watchdog(
780 : 2 : spdm_context, session_id, spdm_context->local_context.heartbeat_period * 2);
781 [ - + ]: 2 : if (!result) {
782 : 0 : return libspdm_generate_error_response(spdm_context,
783 : : SPDM_ERROR_CODE_UNSPECIFIED, 0,
784 : : response_size, response);
785 : : }
786 : : }
787 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_HBEAT_CAP */
788 : :
789 : 38 : return LIBSPDM_STATUS_SUCCESS;
790 : : }
791 : :
792 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_KEY_EX_CAP */
|