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_common_lib.h"
8 : :
9 : 154 : uint8_t libspdm_slot_id_to_key_pair_id (
10 : : void *spdm_context,
11 : : uint8_t slot_id,
12 : : bool is_requester)
13 : : {
14 : : libspdm_context_t *context;
15 : :
16 : 154 : context = spdm_context;
17 [ + + + + ]: 154 : if (slot_id == 0xFF || slot_id == 0xF) {
18 : 12 : return 0;
19 : : }
20 [ + + ]: 142 : if (is_requester) {
21 [ + - ]: 28 : if (!context->connection_info.multi_key_conn_req) {
22 : 28 : return 0;
23 : : }
24 : : } else {
25 [ + + ]: 114 : if (!context->connection_info.multi_key_conn_rsp) {
26 : 110 : return 0;
27 : : }
28 : : }
29 : 4 : LIBSPDM_ASSERT(slot_id < SPDM_MAX_SLOT_COUNT);
30 : 4 : return context->local_context.local_key_pair_id[slot_id];
31 : : }
32 : :
33 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
34 : : void libspdm_get_peer_cert_chain_buffer(void *spdm_context,
35 : : uint8_t slot_id,
36 : : const void **cert_chain_buffer,
37 : : size_t *cert_chain_buffer_size)
38 : : {
39 : :
40 : : libspdm_context_t *context;
41 : :
42 : : context = spdm_context;
43 : :
44 : : LIBSPDM_ASSERT(slot_id < SPDM_MAX_SLOT_COUNT);
45 : :
46 : : *cert_chain_buffer = context->connection_info.peer_used_cert_chain[slot_id].buffer;
47 : : *cert_chain_buffer_size = context->connection_info.peer_used_cert_chain[slot_id].buffer_size;
48 : : }
49 : :
50 : : void libspdm_get_peer_cert_chain_data(void *spdm_context,
51 : : uint8_t slot_id,
52 : : const void **cert_chain_data,
53 : : size_t *cert_chain_data_size)
54 : : {
55 : : libspdm_context_t *context;
56 : : size_t hash_size;
57 : :
58 : : context = spdm_context;
59 : : hash_size = libspdm_get_hash_size(context->connection_info.algorithm.base_hash_algo);
60 : :
61 : : libspdm_get_peer_cert_chain_buffer(context, slot_id, cert_chain_data, cert_chain_data_size);
62 : : if (*cert_chain_data_size < sizeof(spdm_cert_chain_t) + hash_size) {
63 : : /* No certificate chain has been recorded for the slot. The buffer may still hold the
64 : : * bytes of a chain that was discarded, so none of it is returned. */
65 : : *cert_chain_data_size = 0;
66 : : return;
67 : : }
68 : : *cert_chain_data = (const uint8_t *)*cert_chain_data + sizeof(spdm_cert_chain_t) + hash_size;
69 : : *cert_chain_data_size = *cert_chain_data_size - (sizeof(spdm_cert_chain_t) + hash_size);
70 : : }
71 : : #endif /* LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT */
72 : :
73 : 118 : void libspdm_get_local_cert_chain_buffer(void *spdm_context,
74 : : uint8_t slot_id,
75 : : const void **cert_chain_buffer,
76 : : size_t *cert_chain_buffer_size)
77 : : {
78 : : libspdm_context_t *context;
79 : :
80 : 118 : context = spdm_context;
81 : :
82 : 118 : LIBSPDM_ASSERT(context->local_context.local_cert_chain_provision[slot_id] != NULL);
83 : 118 : LIBSPDM_ASSERT(context->local_context.local_cert_chain_provision_size[slot_id] != 0);
84 : :
85 : 118 : *cert_chain_buffer = context->local_context.local_cert_chain_provision[slot_id];
86 : 118 : *cert_chain_buffer_size = context->local_context.local_cert_chain_provision_size[slot_id];
87 : 118 : }
88 : :
89 : 2 : bool libspdm_get_local_cert_chain_data(void *spdm_context,
90 : : uint8_t slot_id,
91 : : const void **cert_chain_data,
92 : : size_t *cert_chain_data_size)
93 : : {
94 : : libspdm_context_t *context;
95 : : size_t hash_size;
96 : :
97 : 2 : context = spdm_context;
98 : :
99 : 2 : libspdm_get_local_cert_chain_buffer(context, slot_id, cert_chain_data, cert_chain_data_size);
100 : :
101 : 2 : hash_size = libspdm_get_hash_size(context->connection_info.algorithm.base_hash_algo);
102 : :
103 : 2 : *cert_chain_data = (const uint8_t *)*cert_chain_data + sizeof(spdm_cert_chain_t) + hash_size;
104 : 2 : *cert_chain_data_size = *cert_chain_data_size - (sizeof(spdm_cert_chain_t) + hash_size);
105 : :
106 : 2 : return true;
107 : : }
108 : :
109 : 12 : bool libspdm_get_peer_public_key_buffer(void *spdm_context,
110 : : const void **peer_public_key_buffer,
111 : : size_t *peer_public_key_buffer_size)
112 : : {
113 : : libspdm_context_t *context;
114 : :
115 : 12 : context = spdm_context;
116 [ + + ]: 12 : if (context->local_context.peer_public_key_provision_size != 0) {
117 : 6 : *peer_public_key_buffer = context->local_context.peer_public_key_provision;
118 : 6 : *peer_public_key_buffer_size = context->local_context.peer_public_key_provision_size;
119 : 6 : return true;
120 : : }
121 : 6 : return false;
122 : : }
123 : :
124 : 10 : bool libspdm_get_local_public_key_buffer(void *spdm_context,
125 : : const void **local_public_key_buffer,
126 : : size_t *local_public_key_buffer_size)
127 : : {
128 : : libspdm_context_t *context;
129 : :
130 : 10 : context = spdm_context;
131 [ + + ]: 10 : if (context->local_context.local_public_key_provision_size != 0) {
132 : 6 : *local_public_key_buffer = context->local_context.local_public_key_provision;
133 : 6 : *local_public_key_buffer_size = context->local_context.local_public_key_provision_size;
134 : 6 : return true;
135 : : }
136 : 4 : return false;
137 : : }
138 : :
139 : : #if !(LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT)
140 : 11110 : void libspdm_free_peer_leaf_cert_public_key(libspdm_context_t *context, uint8_t slot_id)
141 : : {
142 : : void *pubkey_context;
143 : :
144 : 11110 : pubkey_context = context->connection_info.peer_used_cert_chain[slot_id].leaf_cert_public_key;
145 [ + + ]: 11110 : if (pubkey_context == NULL) {
146 : 10822 : return;
147 : : }
148 : :
149 [ + + ]: 288 : if (context->local_context.is_requester) {
150 [ - + ]: 188 : if (context->connection_info.algorithm.pqc_asym_algo != 0) {
151 : 0 : libspdm_pqc_asym_free(context->connection_info.algorithm.pqc_asym_algo,
152 : : pubkey_context);
153 : : } else {
154 : 188 : libspdm_asym_free(context->connection_info.algorithm.base_asym_algo, pubkey_context);
155 : : }
156 : : } else {
157 [ - + ]: 100 : if (context->connection_info.algorithm.req_pqc_asym_alg != 0) {
158 : 0 : libspdm_req_pqc_asym_free(context->connection_info.algorithm.req_pqc_asym_alg,
159 : : pubkey_context);
160 : : } else {
161 : 100 : libspdm_req_asym_free(context->connection_info.algorithm.req_base_asym_alg,
162 : : pubkey_context);
163 : : }
164 : : }
165 : 288 : context->connection_info.peer_used_cert_chain[slot_id].leaf_cert_public_key = NULL;
166 : : }
167 : : #endif /* !(LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT) */
168 : :
169 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
170 : : bool libspdm_calculate_l1l2(libspdm_context_t *spdm_context,
171 : : void *session_info,
172 : : libspdm_l1l2_managed_buffer_t *l1l2)
173 : : {
174 : : libspdm_return_t status;
175 : : libspdm_session_info_t *spdm_session_info;
176 : :
177 : : spdm_session_info = session_info;
178 : :
179 : : libspdm_init_managed_buffer(l1l2, sizeof(l1l2->buffer));
180 : :
181 : : if ((spdm_context->connection_info.version >> SPDM_VERSION_NUMBER_SHIFT_BIT) >
182 : : SPDM_MESSAGE_VERSION_11) {
183 : :
184 : : /* Need append VCA since 1.2 script*/
185 : :
186 : : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "message_a data :\n"));
187 : : LIBSPDM_INTERNAL_DUMP_HEX(
188 : : libspdm_get_managed_buffer(&spdm_context->transcript.message_a),
189 : : libspdm_get_managed_buffer_size(&spdm_context->transcript.message_a));
190 : : status = libspdm_append_managed_buffer(
191 : : l1l2,
192 : : libspdm_get_managed_buffer(&spdm_context->transcript.message_a),
193 : : libspdm_get_managed_buffer_size(&spdm_context->transcript.message_a));
194 : : if (LIBSPDM_STATUS_IS_ERROR(status)) {
195 : : return false;
196 : : }
197 : : }
198 : :
199 : : if (spdm_session_info == NULL) {
200 : : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "message_m data :\n"));
201 : : LIBSPDM_INTERNAL_DUMP_HEX(
202 : : libspdm_get_managed_buffer(&spdm_context->transcript.message_m),
203 : : libspdm_get_managed_buffer_size(&spdm_context->transcript.message_m));
204 : : status = libspdm_append_managed_buffer(
205 : : l1l2,
206 : : libspdm_get_managed_buffer(&spdm_context->transcript.message_m),
207 : : libspdm_get_managed_buffer_size(&spdm_context->transcript.message_m));
208 : : } else {
209 : : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "use message_m in session :\n"));
210 : : LIBSPDM_INTERNAL_DUMP_HEX(
211 : : libspdm_get_managed_buffer(&spdm_session_info->session_transcript.message_m),
212 : : libspdm_get_managed_buffer_size(&spdm_session_info->session_transcript.message_m));
213 : : status = libspdm_append_managed_buffer(
214 : : l1l2,
215 : : libspdm_get_managed_buffer(&spdm_session_info->session_transcript.message_m),
216 : : libspdm_get_managed_buffer_size(&spdm_session_info->session_transcript.message_m));
217 : : }
218 : : if (LIBSPDM_STATUS_IS_ERROR(status)) {
219 : : return false;
220 : : }
221 : :
222 : : /* Debug code only - calculate and print value of l1l2 hash*/
223 : : LIBSPDM_DEBUG_CODE(
224 : : uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
225 : : uint32_t hash_size = libspdm_get_hash_size(
226 : : spdm_context->connection_info.algorithm.base_hash_algo);
227 : : if (!libspdm_hash_all(
228 : : spdm_context->connection_info.algorithm.base_hash_algo,
229 : : libspdm_get_managed_buffer(l1l2),
230 : : libspdm_get_managed_buffer_size(l1l2), hash_data)) {
231 : : return false;
232 : : }
233 : : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "l1l2 hash - "));
234 : : LIBSPDM_INTERNAL_DUMP_DATA(hash_data, hash_size);
235 : : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "\n"));
236 : : );
237 : :
238 : : return true;
239 : : }
240 : : #else
241 : 84 : bool libspdm_calculate_l1l2_hash(libspdm_context_t *spdm_context,
242 : : void *session_info,
243 : : size_t *l1l2_hash_size, void *l1l2_hash)
244 : : {
245 : : libspdm_session_info_t *spdm_session_info;
246 : : bool result;
247 : :
248 : : uint32_t hash_size;
249 : :
250 : 84 : spdm_session_info = session_info;
251 : :
252 : 84 : hash_size = libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
253 : :
254 [ + + ]: 84 : if (spdm_session_info == NULL) {
255 : 78 : result = libspdm_hash_final (spdm_context->connection_info.algorithm.base_hash_algo,
256 : : spdm_context->transcript.digest_context_l1l2, l1l2_hash);
257 : : } else {
258 : 6 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "use message_m in session :\n"));
259 : 6 : result = libspdm_hash_final (spdm_context->connection_info.algorithm.base_hash_algo,
260 : : spdm_session_info->session_transcript.digest_context_l1l2,
261 : : l1l2_hash);
262 : : }
263 [ - + ]: 84 : if (!result) {
264 : 0 : return false;
265 : : }
266 : 84 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "l1l2 hash - "));
267 : 84 : LIBSPDM_INTERNAL_DUMP_DATA(l1l2_hash, hash_size);
268 : 84 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "\n"));
269 : :
270 : 84 : *l1l2_hash_size = hash_size;
271 : :
272 : 84 : return true;
273 : : }
274 : : #endif /* LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT */
275 : :
276 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
277 : : /*
278 : : * This function calculates m1m2.
279 : : *
280 : : * @param context A pointer to the SPDM context.
281 : : * @param is_mut Indicate if this is from mutual authentication.
282 : : * @param m1m2 The buffer to store the m1m2
283 : : */
284 : : static bool libspdm_calculate_m1m2(void *context, bool is_mut,
285 : : libspdm_m1m2_managed_buffer_t *m1m2)
286 : : {
287 : : libspdm_context_t *spdm_context;
288 : : libspdm_return_t status;
289 : :
290 : : spdm_context = context;
291 : :
292 : : libspdm_init_managed_buffer(m1m2, sizeof(m1m2->buffer));
293 : :
294 : : if (is_mut) {
295 : : if ((spdm_context->connection_info.version >> SPDM_VERSION_NUMBER_SHIFT_BIT) >
296 : : SPDM_MESSAGE_VERSION_11) {
297 : : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "message_a data :\n"));
298 : : LIBSPDM_INTERNAL_DUMP_HEX(
299 : : libspdm_get_managed_buffer(&spdm_context->transcript.message_a),
300 : : libspdm_get_managed_buffer_size(&spdm_context->transcript.message_a));
301 : : status = libspdm_append_managed_buffer(
302 : : m1m2,
303 : : libspdm_get_managed_buffer(&spdm_context->transcript.message_a),
304 : : libspdm_get_managed_buffer_size(&spdm_context->transcript.message_a));
305 : : if (LIBSPDM_STATUS_IS_ERROR(status)) {
306 : : return false;
307 : : }
308 : : }
309 : :
310 : : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "message_mut_b data :\n"));
311 : : LIBSPDM_INTERNAL_DUMP_HEX(
312 : : libspdm_get_managed_buffer(&spdm_context->transcript.message_mut_b),
313 : : libspdm_get_managed_buffer_size(&spdm_context->transcript.message_mut_b));
314 : : status = libspdm_append_managed_buffer(
315 : : m1m2,
316 : : libspdm_get_managed_buffer(&spdm_context->transcript.message_mut_b),
317 : : libspdm_get_managed_buffer_size(&spdm_context->transcript.message_mut_b));
318 : : if (LIBSPDM_STATUS_IS_ERROR(status)) {
319 : : return false;
320 : : }
321 : :
322 : : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "message_mut_c data :\n"));
323 : : LIBSPDM_INTERNAL_DUMP_HEX(
324 : : libspdm_get_managed_buffer(&spdm_context->transcript.message_mut_c),
325 : : libspdm_get_managed_buffer_size(&spdm_context->transcript.message_mut_c));
326 : : status = libspdm_append_managed_buffer(
327 : : m1m2,
328 : : libspdm_get_managed_buffer(&spdm_context->transcript.message_mut_c),
329 : : libspdm_get_managed_buffer_size(&spdm_context->transcript.message_mut_c));
330 : : if (LIBSPDM_STATUS_IS_ERROR(status)) {
331 : : return false;
332 : : }
333 : :
334 : : /* Debug code only - calculate and print value of m1m2 mut hash*/
335 : : LIBSPDM_DEBUG_CODE(
336 : : uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
337 : : uint32_t hash_size = libspdm_get_hash_size(
338 : : spdm_context->connection_info.algorithm.base_hash_algo);
339 : : if (!libspdm_hash_all(
340 : : spdm_context->connection_info.algorithm.base_hash_algo,
341 : : libspdm_get_managed_buffer(m1m2),
342 : : libspdm_get_managed_buffer_size(m1m2), hash_data)) {
343 : : return false;
344 : : }
345 : : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m1m2 Mut hash - "));
346 : : LIBSPDM_INTERNAL_DUMP_DATA(hash_data, hash_size);
347 : : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "\n"));
348 : : );
349 : :
350 : : } else {
351 : : if (((spdm_context->connection_info.version >> SPDM_VERSION_NUMBER_SHIFT_BIT) >
352 : : SPDM_MESSAGE_VERSION_11) || spdm_context->transcript.message_a_in_m1m2) {
353 : : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "message_a data :\n"));
354 : : LIBSPDM_INTERNAL_DUMP_HEX(
355 : : libspdm_get_managed_buffer(&spdm_context->transcript.message_a),
356 : : libspdm_get_managed_buffer_size(&spdm_context->transcript.message_a));
357 : : status = libspdm_append_managed_buffer(
358 : : m1m2,
359 : : libspdm_get_managed_buffer(&spdm_context->transcript.message_a),
360 : : libspdm_get_managed_buffer_size(&spdm_context->transcript.message_a));
361 : : if (LIBSPDM_STATUS_IS_ERROR(status)) {
362 : : return false;
363 : : }
364 : : }
365 : :
366 : : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "message_b data :\n"));
367 : : LIBSPDM_INTERNAL_DUMP_HEX(
368 : : libspdm_get_managed_buffer(&spdm_context->transcript.message_b),
369 : : libspdm_get_managed_buffer_size(&spdm_context->transcript.message_b));
370 : : status = libspdm_append_managed_buffer(
371 : : m1m2,
372 : : libspdm_get_managed_buffer(&spdm_context->transcript.message_b),
373 : : libspdm_get_managed_buffer_size(&spdm_context->transcript.message_b));
374 : : if (LIBSPDM_STATUS_IS_ERROR(status)) {
375 : : return false;
376 : : }
377 : :
378 : : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "message_c data :\n"));
379 : : LIBSPDM_INTERNAL_DUMP_HEX(
380 : : libspdm_get_managed_buffer(&spdm_context->transcript.message_c),
381 : : libspdm_get_managed_buffer_size(&spdm_context->transcript.message_c));
382 : : status = libspdm_append_managed_buffer(
383 : : m1m2,
384 : : libspdm_get_managed_buffer(&spdm_context->transcript.message_c),
385 : : libspdm_get_managed_buffer_size(&spdm_context->transcript.message_c));
386 : : if (LIBSPDM_STATUS_IS_ERROR(status)) {
387 : : return false;
388 : : }
389 : :
390 : : /* Debug code only - calculate and print value of m1m2 hash*/
391 : : LIBSPDM_DEBUG_CODE(
392 : : uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
393 : : uint32_t hash_size = libspdm_get_hash_size(
394 : : spdm_context->connection_info.algorithm.base_hash_algo);
395 : : if (!libspdm_hash_all(
396 : : spdm_context->connection_info.algorithm.base_hash_algo,
397 : : libspdm_get_managed_buffer(m1m2),
398 : : libspdm_get_managed_buffer_size(m1m2), hash_data)) {
399 : : return false;
400 : : }
401 : : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m1m2 hash - "));
402 : : LIBSPDM_INTERNAL_DUMP_DATA(hash_data, hash_size);
403 : : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "\n"));
404 : : );
405 : : }
406 : :
407 : : return true;
408 : : }
409 : : #else
410 : : /*
411 : : * This function calculates m1m2 hash.
412 : : *
413 : : * @param context A pointer to the SPDM context.
414 : : * @param is_mut Indicate if this is from mutual authentication.
415 : : * @param m1m2_hash_size size in bytes of the m1m2 hash
416 : : * @param m1m2_hash The buffer to store the m1m2 hash
417 : : */
418 : 98 : static bool libspdm_calculate_m1m2_hash(void *context, bool is_mut,
419 : : size_t *m1m2_hash_size,
420 : : void *m1m2_hash)
421 : : {
422 : : libspdm_context_t *spdm_context;
423 : : uint32_t hash_size;
424 : : bool result;
425 : :
426 : 98 : spdm_context = context;
427 : :
428 : 98 : hash_size = libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
429 : :
430 [ + + ]: 98 : if (is_mut) {
431 : 20 : result = libspdm_hash_final (spdm_context->connection_info.algorithm.base_hash_algo,
432 : : spdm_context->transcript.digest_context_mut_m1m2, m1m2_hash);
433 [ - + ]: 20 : if (!result) {
434 : 0 : return false;
435 : : }
436 : 20 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m1m2 Mut hash - "));
437 : 20 : LIBSPDM_INTERNAL_DUMP_DATA(m1m2_hash, hash_size);
438 : 20 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "\n"));
439 : :
440 : : } else {
441 : 78 : result = libspdm_hash_final (spdm_context->connection_info.algorithm.base_hash_algo,
442 : : spdm_context->transcript.digest_context_m1m2, m1m2_hash);
443 [ - + ]: 78 : if (!result) {
444 : 0 : return false;
445 : : }
446 : 78 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m1m2 hash - "));
447 : 78 : LIBSPDM_INTERNAL_DUMP_DATA(m1m2_hash, hash_size);
448 : 78 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "\n"));
449 : : }
450 : :
451 : 98 : *m1m2_hash_size = hash_size;
452 : :
453 : 98 : return true;
454 : : }
455 : : #endif
456 : :
457 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
458 : : static bool libspdm_calculate_il1il2(libspdm_context_t *spdm_context,
459 : : void *session_info,
460 : : bool is_mut,
461 : : libspdm_il1il2_managed_buffer_t *il1il2)
462 : : {
463 : : libspdm_return_t status;
464 : : libspdm_session_info_t *spdm_session_info;
465 : :
466 : : spdm_session_info = session_info;
467 : :
468 : : libspdm_init_managed_buffer(il1il2, sizeof(il1il2->buffer));
469 : :
470 : :
471 : : if (is_mut) {
472 : : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "message_a data :\n"));
473 : : LIBSPDM_INTERNAL_DUMP_HEX(
474 : : libspdm_get_managed_buffer(&spdm_context->transcript.message_a),
475 : : libspdm_get_managed_buffer_size(&spdm_context->transcript.message_a));
476 : : status = libspdm_append_managed_buffer(
477 : : il1il2,
478 : : libspdm_get_managed_buffer(&spdm_context->transcript.message_a),
479 : : libspdm_get_managed_buffer_size(&spdm_context->transcript.message_a));
480 : : if (LIBSPDM_STATUS_IS_ERROR(status)) {
481 : : return false;
482 : : }
483 : :
484 : : if (spdm_session_info == NULL) {
485 : : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "message_encap_e data :\n"));
486 : : LIBSPDM_INTERNAL_DUMP_HEX(
487 : : libspdm_get_managed_buffer(&spdm_context->transcript.message_encap_e),
488 : : libspdm_get_managed_buffer_size(&spdm_context->transcript.message_encap_e));
489 : : status = libspdm_append_managed_buffer(
490 : : il1il2,
491 : : libspdm_get_managed_buffer(&spdm_context->transcript.message_encap_e),
492 : : libspdm_get_managed_buffer_size(&spdm_context->transcript.message_encap_e));
493 : : } else {
494 : : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "use message_encap_e in session :\n"));
495 : : LIBSPDM_INTERNAL_DUMP_HEX(
496 : : libspdm_get_managed_buffer(&spdm_session_info->session_transcript.message_encap_e),
497 : : libspdm_get_managed_buffer_size(
498 : : &spdm_session_info->session_transcript.message_encap_e));
499 : : status = libspdm_append_managed_buffer(
500 : : il1il2,
501 : : libspdm_get_managed_buffer(&spdm_session_info->session_transcript.message_encap_e),
502 : : libspdm_get_managed_buffer_size(
503 : : &spdm_session_info->session_transcript.message_encap_e));
504 : : }
505 : : if (LIBSPDM_STATUS_IS_ERROR(status)) {
506 : : return false;
507 : : }
508 : :
509 : : /* Debug code only - calculate and print value of il1il2 hash*/
510 : : LIBSPDM_DEBUG_CODE(
511 : : uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
512 : : uint32_t hash_size = libspdm_get_hash_size(
513 : : spdm_context->connection_info.algorithm.base_hash_algo);
514 : : if (!libspdm_hash_all(
515 : : spdm_context->connection_info.algorithm.base_hash_algo,
516 : : libspdm_get_managed_buffer(il1il2),
517 : : libspdm_get_managed_buffer_size(il1il2), hash_data)) {
518 : : return false;
519 : : }
520 : : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "il1il2 mut hash - "));
521 : : LIBSPDM_INTERNAL_DUMP_DATA(hash_data, hash_size);
522 : : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "\n"));
523 : : );
524 : : } else {
525 : : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "message_a data :\n"));
526 : : LIBSPDM_INTERNAL_DUMP_HEX(
527 : : libspdm_get_managed_buffer(&spdm_context->transcript.message_a),
528 : : libspdm_get_managed_buffer_size(&spdm_context->transcript.message_a));
529 : : status = libspdm_append_managed_buffer(
530 : : il1il2,
531 : : libspdm_get_managed_buffer(&spdm_context->transcript.message_a),
532 : : libspdm_get_managed_buffer_size(&spdm_context->transcript.message_a));
533 : : if (LIBSPDM_STATUS_IS_ERROR(status)) {
534 : : return false;
535 : : }
536 : :
537 : : if (spdm_session_info == NULL) {
538 : : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "message_e data :\n"));
539 : : LIBSPDM_INTERNAL_DUMP_HEX(
540 : : libspdm_get_managed_buffer(&spdm_context->transcript.message_e),
541 : : libspdm_get_managed_buffer_size(&spdm_context->transcript.message_e));
542 : : status = libspdm_append_managed_buffer(
543 : : il1il2,
544 : : libspdm_get_managed_buffer(&spdm_context->transcript.message_e),
545 : : libspdm_get_managed_buffer_size(&spdm_context->transcript.message_e));
546 : : } else {
547 : : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "use message_e in session :\n"));
548 : : LIBSPDM_INTERNAL_DUMP_HEX(
549 : : libspdm_get_managed_buffer(&spdm_session_info->session_transcript.message_e),
550 : : libspdm_get_managed_buffer_size(&spdm_session_info->session_transcript.message_e));
551 : : status = libspdm_append_managed_buffer(
552 : : il1il2,
553 : : libspdm_get_managed_buffer(&spdm_session_info->session_transcript.message_e),
554 : : libspdm_get_managed_buffer_size(&spdm_session_info->session_transcript.message_e));
555 : : }
556 : : if (LIBSPDM_STATUS_IS_ERROR(status)) {
557 : : return false;
558 : : }
559 : :
560 : : /* Debug code only - calculate and print value of il1il2 hash*/
561 : : LIBSPDM_DEBUG_CODE(
562 : : uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
563 : : uint32_t hash_size = libspdm_get_hash_size(
564 : : spdm_context->connection_info.algorithm.base_hash_algo);
565 : : if (!libspdm_hash_all(
566 : : spdm_context->connection_info.algorithm.base_hash_algo,
567 : : libspdm_get_managed_buffer(il1il2),
568 : : libspdm_get_managed_buffer_size(il1il2), hash_data)) {
569 : : return false;
570 : : }
571 : : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "il1il2 hash - "));
572 : : LIBSPDM_INTERNAL_DUMP_DATA(hash_data, hash_size);
573 : : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "\n"));
574 : : );
575 : : }
576 : :
577 : : return true;
578 : : }
579 : : #else
580 : 64 : static bool libspdm_calculate_il1il2_hash(libspdm_context_t *spdm_context,
581 : : void *session_info, bool is_encap,
582 : : size_t *il1il2_hash_size, void *il1il2_hash)
583 : : {
584 : : libspdm_session_info_t *spdm_session_info;
585 : : bool result;
586 : :
587 : : uint32_t hash_size;
588 : :
589 : 64 : spdm_session_info = session_info;
590 : :
591 : 64 : hash_size = libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
592 : :
593 [ + + ]: 64 : if (spdm_session_info == NULL) {
594 [ + + ]: 54 : if (is_encap) {
595 : 26 : result = libspdm_hash_final (spdm_context->connection_info.algorithm.base_hash_algo,
596 : : spdm_context->transcript.digest_context_encap_il1il2,
597 : : il1il2_hash);
598 : : } else {
599 : 28 : result = libspdm_hash_final (spdm_context->connection_info.algorithm.base_hash_algo,
600 : : spdm_context->transcript.digest_context_il1il2,
601 : : il1il2_hash);
602 : : }
603 : : } else {
604 [ + + ]: 10 : if (is_encap) {
605 : 4 : result = libspdm_hash_final (spdm_context->connection_info.algorithm.base_hash_algo,
606 : : spdm_session_info->session_transcript.digest_context_encap_il1il2,
607 : : il1il2_hash);
608 : : } else {
609 : 6 : result = libspdm_hash_final (spdm_context->connection_info.algorithm.base_hash_algo,
610 : : spdm_session_info->session_transcript.digest_context_il1il2,
611 : : il1il2_hash);
612 : : }
613 : : }
614 [ - + ]: 64 : if (!result) {
615 : 0 : return false;
616 : : }
617 : 64 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "il1il2 hash - "));
618 : 64 : LIBSPDM_INTERNAL_DUMP_DATA(il1il2_hash, hash_size);
619 : 64 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "\n"));
620 : :
621 : 64 : *il1il2_hash_size = hash_size;
622 : :
623 : 64 : return true;
624 : : }
625 : : #endif /* LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT */
626 : :
627 : 102 : bool libspdm_generate_cert_chain_hash(libspdm_context_t *spdm_context,
628 : : size_t slot_id, uint8_t *hash)
629 : : {
630 : 102 : LIBSPDM_ASSERT(slot_id < SPDM_MAX_SLOT_COUNT);
631 : 102 : return libspdm_hash_all(
632 : : spdm_context->connection_info.algorithm.base_hash_algo,
633 : : spdm_context->local_context.local_cert_chain_provision[slot_id],
634 : : spdm_context->local_context.local_cert_chain_provision_size[slot_id], hash);
635 : : }
636 : :
637 : 4 : bool libspdm_generate_public_key_hash(libspdm_context_t *spdm_context,
638 : : uint8_t *hash)
639 : : {
640 : 4 : return libspdm_hash_all(
641 : : spdm_context->connection_info.algorithm.base_hash_algo,
642 : : spdm_context->local_context.local_public_key_provision,
643 : : spdm_context->local_context.local_public_key_provision_size, hash);
644 : : }
645 : :
646 : 34 : uint8_t libspdm_get_cert_slot_mask(libspdm_context_t *spdm_context)
647 : : {
648 : : size_t index;
649 : : uint8_t slot_mask;
650 : :
651 : 34 : slot_mask = 0;
652 [ + + ]: 306 : for (index = 0; index < SPDM_MAX_SLOT_COUNT; index++) {
653 [ + + ]: 272 : if (spdm_context->local_context.local_cert_chain_provision[index] != NULL) {
654 : 36 : slot_mask |= (1 << index);
655 : : }
656 : : }
657 : :
658 : 34 : return slot_mask;
659 : : }
660 : :
661 : 50 : uint8_t libspdm_get_cert_slot_count(libspdm_context_t *spdm_context)
662 : : {
663 : : size_t index;
664 : : uint8_t slot_count;
665 : :
666 : 50 : slot_count = 0;
667 [ + + ]: 450 : for (index = 0; index < SPDM_MAX_SLOT_COUNT; index++) {
668 [ + + ]: 400 : if (spdm_context->local_context.local_cert_chain_provision[index] != NULL) {
669 : 68 : slot_count++;
670 : : }
671 : : }
672 : :
673 : 50 : return slot_count;
674 : : }
675 : :
676 : : #if LIBSPDM_CERT_PARSE_SUPPORT
677 : 116 : bool libspdm_verify_peer_cert_chain_buffer_integrity(libspdm_context_t *spdm_context,
678 : : const void *cert_chain_buffer,
679 : : size_t cert_chain_buffer_size)
680 : : {
681 : : bool result;
682 : : uint8_t cert_model;
683 : : bool is_requester;
684 : :
685 : 116 : is_requester = spdm_context->local_context.is_requester;
686 : :
687 : 116 : cert_model = SPDM_CERTIFICATE_INFO_CERT_MODEL_ALIAS_CERT;
688 : : /* Responder does not determine Requester's certificate model */
689 [ + + ]: 116 : if (is_requester) {
690 [ + + ]: 78 : if ((spdm_context->connection_info.capability.flags &
691 : : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ALIAS_CERT_CAP) == 0) {
692 : 72 : cert_model = SPDM_CERTIFICATE_INFO_CERT_MODEL_DEVICE_CERT;
693 : : }
694 : : }
695 : :
696 [ + + ]: 116 : if (is_requester) {
697 : 78 : result = libspdm_verify_certificate_chain_buffer(
698 : 78 : libspdm_get_connection_version(spdm_context),
699 : : spdm_context->connection_info.algorithm.base_hash_algo,
700 : : spdm_context->connection_info.algorithm.base_asym_algo,
701 : : spdm_context->connection_info.algorithm.pqc_asym_algo,
702 : : cert_chain_buffer, cert_chain_buffer_size,
703 : : false, cert_model);
704 : : } else {
705 : 38 : result = libspdm_verify_certificate_chain_buffer(
706 : 38 : libspdm_get_connection_version(spdm_context),
707 : : spdm_context->connection_info.algorithm.base_hash_algo,
708 : 38 : spdm_context->connection_info.algorithm.req_base_asym_alg,
709 : : spdm_context->connection_info.algorithm.req_pqc_asym_alg,
710 : : cert_chain_buffer, cert_chain_buffer_size,
711 : : true, cert_model);
712 : : }
713 : :
714 : 116 : return result;
715 : : }
716 : :
717 : 94 : bool libspdm_verify_peer_cert_chain_buffer_authority(libspdm_context_t *spdm_context,
718 : : const void *cert_chain_buffer,
719 : : size_t cert_chain_buffer_size,
720 : : const void **trust_anchor,
721 : : size_t *trust_anchor_size)
722 : : {
723 : : const uint8_t *root_cert;
724 : : size_t root_cert_size;
725 : : size_t root_cert_index;
726 : : size_t root_cert_hash_size;
727 : : uint8_t root_cert_hash[LIBSPDM_MAX_HASH_SIZE];
728 : : const uint8_t *received_root_cert;
729 : : size_t received_root_cert_size;
730 : : bool result;
731 : :
732 : 94 : root_cert_index = 0;
733 : 94 : root_cert = spdm_context->local_context.peer_root_cert_provision[root_cert_index];
734 : 94 : root_cert_size = spdm_context->local_context.peer_root_cert_provision_size[root_cert_index];
735 : :
736 : 94 : root_cert_hash_size = libspdm_get_hash_size(
737 : : spdm_context->connection_info.algorithm.base_hash_algo);
738 : :
739 [ + + + - ]: 94 : if ((root_cert != NULL) && (root_cert_size != 0)) {
740 [ + - + - ]: 154 : while ((root_cert != NULL) && (root_cert_size != 0)) {
741 : 154 : result = libspdm_hash_all(
742 : : spdm_context->connection_info.algorithm.base_hash_algo,
743 : : root_cert, root_cert_size, root_cert_hash);
744 [ - + ]: 154 : if (!result) {
745 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR,
746 : : "!!! verify_peer_cert_chain_buffer - FAIL (hash calculation) !!!\n"));
747 : 0 : return false;
748 : : }
749 : :
750 [ + + ]: 154 : if (libspdm_consttime_is_mem_equal((const uint8_t *)cert_chain_buffer +
751 : : sizeof(spdm_cert_chain_t),
752 : : root_cert_hash, root_cert_hash_size)) {
753 : 74 : break;
754 : : }
755 : :
756 : : #if (LIBSPDM_MAX_ROOT_CERT_SUPPORT) > 1
757 [ + + ]: 80 : if ((root_cert_index < ((LIBSPDM_MAX_ROOT_CERT_SUPPORT) -1)) &&
758 [ + + ]: 78 : (spdm_context->local_context.peer_root_cert_provision[root_cert_index + 1] !=
759 : : NULL)) {
760 : 68 : root_cert_index++;
761 : 68 : root_cert = spdm_context->local_context.peer_root_cert_provision[root_cert_index];
762 : 68 : root_cert_size =
763 : : spdm_context->local_context.peer_root_cert_provision_size[root_cert_index];
764 : : } else
765 : : #endif /* LIBSPDM_MAX_ROOT_CERT_SUPPORT */
766 : : {
767 : 12 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR,
768 : : "!!! verify_peer_cert_chain_buffer - "
769 : : "FAIL (all root cert hash mismatch) !!!\n"));
770 : 12 : return false;
771 : : }
772 : : }
773 : :
774 : 74 : result = libspdm_x509_get_cert_from_cert_chain(
775 : 74 : (const uint8_t *)cert_chain_buffer + sizeof(spdm_cert_chain_t) + root_cert_hash_size,
776 : 74 : cert_chain_buffer_size - sizeof(spdm_cert_chain_t) - root_cert_hash_size,
777 : : 0, &received_root_cert, &received_root_cert_size);
778 [ - + ]: 74 : if (!result) {
779 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR,
780 : : "!!! verify_peer_cert_chain_buffer - FAIL (cert retrieval fail) !!!\n"));
781 : 0 : return false;
782 : : }
783 [ + + ]: 74 : if (libspdm_is_root_certificate(received_root_cert, received_root_cert_size)) {
784 [ + - ]: 72 : if ((root_cert != NULL) &&
785 [ - + ]: 72 : !libspdm_consttime_is_mem_equal(received_root_cert, root_cert, root_cert_size)) {
786 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR,
787 : : "!!! verify_peer_cert_chain_buffer - "
788 : : "FAIL (root cert mismatch) !!!\n"));
789 : 0 : return false;
790 : : }
791 : : } else {
792 [ - + ]: 2 : if (!libspdm_x509_verify_cert(received_root_cert, received_root_cert_size,
793 : : root_cert, root_cert_size)) {
794 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR,
795 : : "!!! verify_peer_cert_chain_buffer - "
796 : : "FAIL (received root cert verify failed)!!!\n"));
797 : 0 : return false;
798 : : }
799 : : }
800 [ + + ]: 74 : if (trust_anchor != NULL) {
801 : 10 : *trust_anchor = root_cert;
802 : : }
803 [ + + ]: 74 : if (trust_anchor_size != NULL) {
804 : 10 : *trust_anchor_size = root_cert_size;
805 : : }
806 : : } else {
807 : 8 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR,
808 : : "!!! verify_peer_cert_chain_buffer - "
809 : : "FAIL (no root cert provisioned) !!!\n"));
810 : 8 : return false;
811 : : }
812 : 74 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "!!! verify_peer_cert_chain_buffer - PASS !!!\n"));
813 : :
814 : 74 : return true;
815 : : }
816 : : #endif
817 : :
818 : 38 : bool libspdm_generate_challenge_auth_signature(libspdm_context_t *spdm_context,
819 : : bool is_requester,
820 : : uint8_t slot_id,
821 : : uint8_t *signature)
822 : : {
823 : : bool result;
824 : : size_t signature_size;
825 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
826 : : libspdm_m1m2_managed_buffer_t m1m2;
827 : : uint8_t *m1m2_buffer;
828 : : size_t m1m2_buffer_size;
829 : : #else
830 : : uint8_t m1m2_hash[LIBSPDM_MAX_HASH_SIZE];
831 : : size_t m1m2_hash_size;
832 : : #endif
833 : :
834 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
835 : : result = libspdm_calculate_m1m2(spdm_context, is_requester, &m1m2);
836 : : m1m2_buffer = libspdm_get_managed_buffer(&m1m2);
837 : : m1m2_buffer_size = libspdm_get_managed_buffer_size(&m1m2);
838 : : #else
839 : 38 : m1m2_hash_size = sizeof(m1m2_hash);
840 : 38 : result = libspdm_calculate_m1m2_hash(spdm_context, is_requester, &m1m2_hash_size, &m1m2_hash);
841 : : #endif
842 [ + + ]: 38 : if (is_requester) {
843 : 8 : libspdm_reset_message_mut_b(spdm_context);
844 : 8 : libspdm_reset_message_mut_c(spdm_context);
845 : : } else {
846 : 30 : libspdm_reset_message_b(spdm_context);
847 : 30 : libspdm_reset_message_c(spdm_context);
848 : 30 : spdm_context->transcript.message_a_in_m1m2 = false;
849 : : }
850 [ - + ]: 38 : if (!result) {
851 : 0 : return false;
852 : : }
853 : :
854 [ + + ]: 38 : if (is_requester) {
855 : : #if LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP
856 [ - + ]: 8 : if (spdm_context->connection_info.algorithm.req_pqc_asym_alg != 0) {
857 : 0 : signature_size = libspdm_get_req_pqc_asym_signature_size(
858 : : spdm_context->connection_info.algorithm.req_pqc_asym_alg);
859 : : } else {
860 : 8 : signature_size = libspdm_get_req_asym_signature_size(
861 : 8 : spdm_context->connection_info.algorithm.req_base_asym_alg);
862 : : }
863 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
864 : : result = libspdm_requester_data_sign(
865 : : spdm_context,
866 : : spdm_context->connection_info.version,
867 : : libspdm_slot_id_to_key_pair_id(spdm_context, slot_id, true),
868 : : SPDM_CHALLENGE_AUTH,
869 : : spdm_context->connection_info.algorithm.req_base_asym_alg,
870 : : spdm_context->connection_info.algorithm.req_pqc_asym_alg,
871 : : spdm_context->connection_info.algorithm.base_hash_algo,
872 : : false, m1m2_buffer, m1m2_buffer_size, signature, &signature_size);
873 : : #else
874 : 16 : result = libspdm_requester_data_sign(
875 : : spdm_context,
876 : 8 : spdm_context->connection_info.version,
877 : 8 : libspdm_slot_id_to_key_pair_id(spdm_context, slot_id, true),
878 : : SPDM_CHALLENGE_AUTH,
879 : 8 : spdm_context->connection_info.algorithm.req_base_asym_alg,
880 : : spdm_context->connection_info.algorithm.req_pqc_asym_alg,
881 : : spdm_context->connection_info.algorithm.base_hash_algo,
882 : : true, m1m2_hash, m1m2_hash_size, signature, &signature_size);
883 : : #endif
884 : : #else /* LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP */
885 : : result = false;
886 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP */
887 : : } else {
888 [ - + ]: 30 : if (spdm_context->connection_info.algorithm.pqc_asym_algo != 0) {
889 : 0 : signature_size = libspdm_get_pqc_asym_signature_size(
890 : : spdm_context->connection_info.algorithm.pqc_asym_algo);
891 : : } else {
892 : 30 : signature_size = libspdm_get_asym_signature_size(
893 : : spdm_context->connection_info.algorithm.base_asym_algo);
894 : : }
895 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
896 : : result = libspdm_responder_data_sign(
897 : : spdm_context,
898 : : spdm_context->connection_info.version,
899 : : libspdm_slot_id_to_key_pair_id(spdm_context, slot_id, false),
900 : : SPDM_CHALLENGE_AUTH,
901 : : spdm_context->connection_info.algorithm.base_asym_algo,
902 : : spdm_context->connection_info.algorithm.pqc_asym_algo,
903 : : spdm_context->connection_info.algorithm.base_hash_algo,
904 : : false, m1m2_buffer, m1m2_buffer_size, signature,
905 : : &signature_size);
906 : : #else
907 : 60 : result = libspdm_responder_data_sign(
908 : : spdm_context,
909 : 30 : spdm_context->connection_info.version,
910 : 30 : libspdm_slot_id_to_key_pair_id(spdm_context, slot_id, false),
911 : : SPDM_CHALLENGE_AUTH,
912 : : spdm_context->connection_info.algorithm.base_asym_algo,
913 : : spdm_context->connection_info.algorithm.pqc_asym_algo,
914 : : spdm_context->connection_info.algorithm.base_hash_algo,
915 : : true, m1m2_hash, m1m2_hash_size, signature,
916 : : &signature_size);
917 : : #endif
918 : : }
919 : :
920 : 38 : return result;
921 : : }
922 : :
923 : 64 : bool libspdm_verify_certificate_chain_hash(libspdm_context_t *spdm_context,
924 : : uint8_t slot_id,
925 : : const void *certificate_chain_hash,
926 : : size_t certificate_chain_hash_size)
927 : : {
928 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
929 : : size_t hash_size;
930 : : uint8_t cert_chain_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
931 : : const uint8_t *cert_chain_buffer;
932 : : size_t cert_chain_buffer_size;
933 : : bool result;
934 : :
935 : : libspdm_get_peer_cert_chain_buffer(spdm_context,
936 : : slot_id,
937 : : (const void **)&cert_chain_buffer,
938 : : &cert_chain_buffer_size);
939 : :
940 : : hash_size = libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
941 : :
942 : : result = libspdm_hash_all(spdm_context->connection_info.algorithm.base_hash_algo,
943 : : cert_chain_buffer, cert_chain_buffer_size,
944 : : cert_chain_buffer_hash);
945 : : if (!result) {
946 : : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR,
947 : : "!!! verify_certificate_chain_hash - FAIL (hash calculation) !!!\n"));
948 : : return false;
949 : : }
950 : :
951 : : if (hash_size != certificate_chain_hash_size) {
952 : : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR, "!!! verify_certificate_chain_hash - FAIL !!!\n"));
953 : : return false;
954 : : }
955 : : if (!libspdm_consttime_is_mem_equal(certificate_chain_hash, cert_chain_buffer_hash,
956 : : certificate_chain_hash_size)) {
957 : : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR, "!!! verify_certificate_chain_hash - FAIL !!!\n"));
958 : : return false;
959 : : }
960 : : #else
961 : : /* This also fails if no certificate chain has been recorded for the slot. */
962 [ + + ]: 64 : if (spdm_context->connection_info.peer_used_cert_chain[slot_id].buffer_hash_size !=
963 : : certificate_chain_hash_size) {
964 : 2 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR, "!!! verify_certificate_chain_hash - FAIL !!!\n"));
965 : 2 : return false;
966 : : }
967 : :
968 [ - + ]: 62 : if (!libspdm_consttime_is_mem_equal(certificate_chain_hash,
969 : 62 : spdm_context->connection_info.peer_used_cert_chain[slot_id].
970 : : buffer_hash, certificate_chain_hash_size)) {
971 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR, "!!! verify_certificate_chain_hash - FAIL !!!\n"));
972 : 0 : return false;
973 : : }
974 : : #endif
975 : 62 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "!!! verify_certificate_chain_hash - PASS !!!\n"));
976 : 62 : return true;
977 : : }
978 : :
979 : 4 : bool libspdm_verify_public_key_hash(libspdm_context_t *spdm_context,
980 : : const void *public_key_hash,
981 : : size_t public_key_hash_size)
982 : : {
983 : : size_t hash_size;
984 : : uint8_t public_key_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
985 : : bool result;
986 : :
987 : 4 : hash_size = libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
988 : :
989 : 4 : result = libspdm_hash_all(spdm_context->connection_info.algorithm.base_hash_algo,
990 : : spdm_context->local_context.peer_public_key_provision,
991 : : spdm_context->local_context.peer_public_key_provision_size,
992 : : public_key_buffer_hash);
993 [ - + ]: 4 : if (!result) {
994 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR,
995 : : "!!! verify_public_key_hash - FAIL (hash calculation) !!!\n"));
996 : 0 : return false;
997 : : }
998 : :
999 [ - + ]: 4 : if (hash_size != public_key_hash_size) {
1000 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR, "!!! verify_public_key_hash - FAIL !!!\n"));
1001 : 0 : return false;
1002 : : }
1003 [ - + ]: 4 : if (!libspdm_consttime_is_mem_equal(public_key_hash, public_key_buffer_hash,
1004 : : public_key_hash_size)) {
1005 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR, "!!! verify_public_key_hash - FAIL !!!\n"));
1006 : 0 : return false;
1007 : : }
1008 : :
1009 : 4 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "!!! verify_public_key_hash - PASS !!!\n"));
1010 : 4 : return true;
1011 : : }
1012 : :
1013 : 60 : bool libspdm_verify_challenge_auth_signature(libspdm_context_t *spdm_context,
1014 : : bool is_requester,
1015 : : uint8_t slot_id,
1016 : : const void *sign_data,
1017 : : size_t sign_data_size)
1018 : : {
1019 : : bool result;
1020 : : void *context;
1021 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1022 : : libspdm_m1m2_managed_buffer_t m1m2;
1023 : : uint8_t *m1m2_buffer;
1024 : : size_t m1m2_buffer_size;
1025 : : #if LIBSPDM_CERT_PARSE_SUPPORT
1026 : : const uint8_t *cert_buffer;
1027 : : size_t cert_buffer_size;
1028 : : const uint8_t *cert_chain_data;
1029 : : size_t cert_chain_data_size;
1030 : : #endif /* LIBSPDM_CERT_PARSE_SUPPORT */
1031 : : #else
1032 : : uint8_t m1m2_hash[LIBSPDM_MAX_HASH_SIZE];
1033 : : size_t m1m2_hash_size;
1034 : : #endif
1035 : :
1036 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1037 : : result = libspdm_calculate_m1m2(spdm_context, !is_requester, &m1m2);
1038 : : m1m2_buffer = libspdm_get_managed_buffer(&m1m2);
1039 : : m1m2_buffer_size = libspdm_get_managed_buffer_size(&m1m2);
1040 : : #else
1041 : 60 : m1m2_hash_size = sizeof(m1m2_hash);
1042 : 60 : result = libspdm_calculate_m1m2_hash(spdm_context, !is_requester, &m1m2_hash_size, &m1m2_hash);
1043 : : #endif
1044 [ + + ]: 60 : if (is_requester) {
1045 : 48 : libspdm_reset_message_b(spdm_context);
1046 : 48 : libspdm_reset_message_c(spdm_context);
1047 : 48 : spdm_context->transcript.message_a_in_m1m2 = false;
1048 : : } else {
1049 : 12 : libspdm_reset_message_mut_b(spdm_context);
1050 : 12 : libspdm_reset_message_mut_c(spdm_context);
1051 : : }
1052 [ - + ]: 60 : if (!result) {
1053 : 0 : return false;
1054 : : }
1055 : :
1056 [ + + ]: 60 : if (slot_id == 0xFF) {
1057 [ + + ]: 4 : if (is_requester) {
1058 [ - + ]: 2 : if (spdm_context->connection_info.algorithm.pqc_asym_algo != 0) {
1059 : 0 : result = libspdm_pqc_asym_get_public_key_from_der(
1060 : : spdm_context->connection_info.algorithm.pqc_asym_algo,
1061 : 0 : spdm_context->local_context.peer_public_key_provision,
1062 : : spdm_context->local_context.peer_public_key_provision_size,
1063 : : &context);
1064 : : } else {
1065 : 2 : result = libspdm_asym_get_public_key_from_der(
1066 : : spdm_context->connection_info.algorithm.base_asym_algo,
1067 : 2 : spdm_context->local_context.peer_public_key_provision,
1068 : : spdm_context->local_context.peer_public_key_provision_size,
1069 : : &context);
1070 : : }
1071 : : } else {
1072 [ - + ]: 2 : if (spdm_context->connection_info.algorithm.req_pqc_asym_alg != 0) {
1073 : 0 : result = libspdm_req_pqc_asym_get_public_key_from_der(
1074 : : spdm_context->connection_info.algorithm.req_pqc_asym_alg,
1075 : 0 : spdm_context->local_context.peer_public_key_provision,
1076 : : spdm_context->local_context.peer_public_key_provision_size,
1077 : : &context);
1078 : : } else {
1079 : 2 : result = libspdm_req_asym_get_public_key_from_der(
1080 : 2 : spdm_context->connection_info.algorithm.req_base_asym_alg,
1081 : 2 : spdm_context->local_context.peer_public_key_provision,
1082 : : spdm_context->local_context.peer_public_key_provision_size,
1083 : : &context);
1084 : : }
1085 : : }
1086 [ - + ]: 4 : if (!result) {
1087 : 0 : return false;
1088 : : }
1089 : : } else {
1090 : : #if (LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT) && (LIBSPDM_CERT_PARSE_SUPPORT)
1091 : : libspdm_get_peer_cert_chain_data(
1092 : : spdm_context, slot_id, (const void **)&cert_chain_data, &cert_chain_data_size);
1093 : :
1094 : : /* Get leaf cert from cert chain*/
1095 : : result = libspdm_x509_get_cert_from_cert_chain(
1096 : : cert_chain_data, cert_chain_data_size, -1, &cert_buffer, &cert_buffer_size);
1097 : : if (!result) {
1098 : : return false;
1099 : : }
1100 : :
1101 : : if (is_requester) {
1102 : : if (spdm_context->connection_info.algorithm.pqc_asym_algo != 0) {
1103 : : result = libspdm_pqc_asym_get_public_key_from_x509(
1104 : : spdm_context->connection_info.algorithm.pqc_asym_algo,
1105 : : cert_buffer, cert_buffer_size, &context);
1106 : : } else {
1107 : : result = libspdm_asym_get_public_key_from_x509(
1108 : : spdm_context->connection_info.algorithm.base_asym_algo,
1109 : : cert_buffer, cert_buffer_size, &context);
1110 : : }
1111 : : } else {
1112 : : if (spdm_context->connection_info.algorithm.req_pqc_asym_alg != 0) {
1113 : : result = libspdm_req_pqc_asym_get_public_key_from_x509(
1114 : : spdm_context->connection_info.algorithm.req_pqc_asym_alg,
1115 : : cert_buffer, cert_buffer_size, &context);
1116 : : } else {
1117 : : result = libspdm_req_asym_get_public_key_from_x509(
1118 : : spdm_context->connection_info.algorithm.req_base_asym_alg,
1119 : : cert_buffer, cert_buffer_size, &context);
1120 : : }
1121 : : }
1122 : : if (!result) {
1123 : : return false;
1124 : : }
1125 : : #elif LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1126 : : return false;
1127 : : #else
1128 : 56 : context = spdm_context->connection_info.peer_used_cert_chain[slot_id].leaf_cert_public_key;
1129 [ + + ]: 56 : if (context == NULL) {
1130 : : /* No certificate chain has been recorded for the slot. */
1131 : 2 : return false;
1132 : : }
1133 : : #endif
1134 : : }
1135 : :
1136 [ + + ]: 58 : if (is_requester) {
1137 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1138 : : if (spdm_context->connection_info.algorithm.pqc_asym_algo != 0) {
1139 : : result = libspdm_pqc_asym_verify(
1140 : : spdm_context->connection_info.version, SPDM_CHALLENGE_AUTH,
1141 : : spdm_context->connection_info.algorithm.pqc_asym_algo,
1142 : : spdm_context->connection_info.algorithm.base_hash_algo,
1143 : : context, m1m2_buffer, m1m2_buffer_size, sign_data, sign_data_size);
1144 : : libspdm_pqc_asym_free(
1145 : : spdm_context->connection_info.algorithm.pqc_asym_algo, context);
1146 : : } else {
1147 : : result = libspdm_asym_verify_ex(
1148 : : spdm_context->connection_info.version, SPDM_CHALLENGE_AUTH,
1149 : : spdm_context->connection_info.algorithm.base_asym_algo,
1150 : : spdm_context->connection_info.algorithm.base_hash_algo,
1151 : : context, m1m2_buffer, m1m2_buffer_size, sign_data, sign_data_size,
1152 : : &spdm_context->spdm_10_11_verify_signature_endian);
1153 : : libspdm_asym_free(
1154 : : spdm_context->connection_info.algorithm.base_asym_algo, context);
1155 : : }
1156 : : #else
1157 [ - + ]: 48 : if (spdm_context->connection_info.algorithm.pqc_asym_algo != 0) {
1158 : 0 : result = libspdm_pqc_asym_verify_hash(
1159 : 0 : spdm_context->connection_info.version, SPDM_CHALLENGE_AUTH,
1160 : : spdm_context->connection_info.algorithm.pqc_asym_algo,
1161 : : spdm_context->connection_info.algorithm.base_hash_algo,
1162 : : context, m1m2_hash, m1m2_hash_size, sign_data, sign_data_size);
1163 [ # # ]: 0 : if (slot_id == 0xFF) {
1164 : 0 : libspdm_pqc_asym_free(
1165 : : spdm_context->connection_info.algorithm.pqc_asym_algo, context);
1166 : : }
1167 : : } else {
1168 : 48 : result = libspdm_asym_verify_hash_ex(
1169 : 48 : spdm_context->connection_info.version, SPDM_CHALLENGE_AUTH,
1170 : : spdm_context->connection_info.algorithm.base_asym_algo,
1171 : : spdm_context->connection_info.algorithm.base_hash_algo,
1172 : : context, m1m2_hash, m1m2_hash_size, sign_data, sign_data_size,
1173 : : &spdm_context->spdm_10_11_verify_signature_endian);
1174 [ + + ]: 48 : if (slot_id == 0xFF) {
1175 : 2 : libspdm_asym_free(
1176 : : spdm_context->connection_info.algorithm.base_asym_algo, context);
1177 : : }
1178 : : }
1179 : : #endif
1180 : : } else {
1181 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1182 : : if (spdm_context->connection_info.algorithm.req_pqc_asym_alg != 0) {
1183 : : result = libspdm_req_pqc_asym_verify(
1184 : : spdm_context->connection_info.version, SPDM_CHALLENGE_AUTH,
1185 : : spdm_context->connection_info.algorithm.req_pqc_asym_alg,
1186 : : spdm_context->connection_info.algorithm.base_hash_algo,
1187 : : context, m1m2_buffer, m1m2_buffer_size, sign_data, sign_data_size);
1188 : : libspdm_req_pqc_asym_free(
1189 : : spdm_context->connection_info.algorithm.req_pqc_asym_alg, context);
1190 : : } else {
1191 : : result = libspdm_req_asym_verify_ex(
1192 : : spdm_context->connection_info.version, SPDM_CHALLENGE_AUTH,
1193 : : spdm_context->connection_info.algorithm.req_base_asym_alg,
1194 : : spdm_context->connection_info.algorithm.base_hash_algo,
1195 : : context, m1m2_buffer, m1m2_buffer_size, sign_data, sign_data_size,
1196 : : &spdm_context->spdm_10_11_verify_signature_endian);
1197 : : libspdm_req_asym_free(
1198 : : spdm_context->connection_info.algorithm.req_base_asym_alg, context);
1199 : : }
1200 : : #else
1201 [ - + ]: 10 : if (spdm_context->connection_info.algorithm.req_pqc_asym_alg != 0) {
1202 : 0 : result = libspdm_req_pqc_asym_verify_hash(
1203 : 0 : spdm_context->connection_info.version, SPDM_CHALLENGE_AUTH,
1204 : : spdm_context->connection_info.algorithm.req_pqc_asym_alg,
1205 : : spdm_context->connection_info.algorithm.base_hash_algo,
1206 : : context, m1m2_hash, m1m2_hash_size, sign_data, sign_data_size);
1207 [ # # ]: 0 : if (slot_id == 0xFF) {
1208 : 0 : libspdm_req_pqc_asym_free(
1209 : : spdm_context->connection_info.algorithm.req_pqc_asym_alg, context);
1210 : : }
1211 : : } else {
1212 : 10 : result = libspdm_req_asym_verify_hash_ex(
1213 : 10 : spdm_context->connection_info.version, SPDM_CHALLENGE_AUTH,
1214 : 10 : spdm_context->connection_info.algorithm.req_base_asym_alg,
1215 : : spdm_context->connection_info.algorithm.base_hash_algo,
1216 : : context, m1m2_hash, m1m2_hash_size, sign_data, sign_data_size,
1217 : : &spdm_context->spdm_10_11_verify_signature_endian);
1218 [ + + ]: 10 : if (slot_id == 0xFF) {
1219 : 2 : libspdm_req_asym_free(
1220 : 2 : spdm_context->connection_info.algorithm.req_base_asym_alg, context);
1221 : : }
1222 : : }
1223 : : #endif
1224 : : }
1225 [ + + ]: 58 : if (!result) {
1226 : 4 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR,
1227 : : "!!! verify_challenge_signature - FAIL !!!\n"));
1228 : 4 : return false;
1229 : : }
1230 : :
1231 : 54 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "!!! verify_challenge_signature - PASS !!!\n"));
1232 : :
1233 : 54 : return true;
1234 : : }
1235 : :
1236 : : uint32_t
1237 : 336 : libspdm_get_measurement_summary_hash_size(libspdm_context_t *spdm_context,
1238 : : bool is_requester,
1239 : : uint8_t measurement_summary_hash_type)
1240 : : {
1241 [ + + ]: 336 : if (!libspdm_is_capabilities_flag_supported(
1242 : : spdm_context, is_requester, 0,
1243 : : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEAS_CAP)) {
1244 : 224 : return 0;
1245 : : }
1246 : :
1247 [ + + + ]: 112 : switch (measurement_summary_hash_type) {
1248 : 56 : case SPDM_REQUEST_NO_MEASUREMENT_SUMMARY_HASH:
1249 : 56 : return 0;
1250 : : break;
1251 : :
1252 : 52 : case SPDM_REQUEST_TCB_COMPONENT_MEASUREMENT_HASH:
1253 : : case SPDM_REQUEST_ALL_MEASUREMENTS_HASH:
1254 : 52 : return libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
1255 : : break;
1256 : 4 : default:
1257 : 4 : return 0;
1258 : : break;
1259 : : }
1260 : : }
1261 : :
1262 : : #if LIBSPDM_ENABLE_CAPABILITY_ENDPOINT_INFO_CAP
1263 : 18 : bool libspdm_generate_endpoint_info_signature(libspdm_context_t *spdm_context,
1264 : : libspdm_session_info_t *session_info,
1265 : : bool is_requester,
1266 : : uint8_t slot_id,
1267 : : uint8_t *signature)
1268 : : {
1269 : : bool result;
1270 : : size_t signature_size;
1271 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1272 : : libspdm_il1il2_managed_buffer_t il1il2;
1273 : : uint8_t *il1il2_buffer;
1274 : : size_t il1il2_buffer_size;
1275 : : #else
1276 : : uint8_t il1il2_hash[LIBSPDM_MAX_HASH_SIZE];
1277 : : size_t il1il2_hash_size;
1278 : : #endif
1279 : :
1280 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1281 : : result = libspdm_calculate_il1il2(spdm_context, session_info, is_requester, &il1il2);
1282 : : il1il2_buffer = libspdm_get_managed_buffer(&il1il2);
1283 : : il1il2_buffer_size = libspdm_get_managed_buffer_size(&il1il2);
1284 : : #else
1285 : 18 : il1il2_hash_size = sizeof(il1il2_hash);
1286 : 18 : result = libspdm_calculate_il1il2_hash(spdm_context, session_info, is_requester,
1287 : : &il1il2_hash_size, &il1il2_hash);
1288 : : #endif
1289 [ + + ]: 18 : if (is_requester) {
1290 : 10 : libspdm_reset_message_encap_e(spdm_context, session_info);
1291 : : } else {
1292 : 8 : libspdm_reset_message_e(spdm_context, session_info);
1293 : : }
1294 [ - + ]: 18 : if (!result) {
1295 : 0 : return false;
1296 : : }
1297 : :
1298 [ + + ]: 18 : if (is_requester) {
1299 [ - + ]: 10 : if (spdm_context->connection_info.algorithm.req_pqc_asym_alg != 0) {
1300 : 0 : signature_size = libspdm_get_req_pqc_asym_signature_size(
1301 : : spdm_context->connection_info.algorithm.req_pqc_asym_alg);
1302 : : } else {
1303 : 10 : signature_size = libspdm_get_req_asym_signature_size(
1304 : 10 : spdm_context->connection_info.algorithm.req_base_asym_alg);
1305 : : }
1306 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1307 : : result = libspdm_requester_data_sign(
1308 : : spdm_context,
1309 : : spdm_context->connection_info.version,
1310 : : libspdm_slot_id_to_key_pair_id(spdm_context, slot_id, true),
1311 : : SPDM_ENDPOINT_INFO,
1312 : : spdm_context->connection_info.algorithm.req_base_asym_alg,
1313 : : spdm_context->connection_info.algorithm.req_pqc_asym_alg,
1314 : : spdm_context->connection_info.algorithm.base_hash_algo,
1315 : : false, il1il2_buffer, il1il2_buffer_size, signature, &signature_size);
1316 : : #else
1317 : 20 : result = libspdm_requester_data_sign(
1318 : : spdm_context,
1319 : 10 : spdm_context->connection_info.version,
1320 : 10 : libspdm_slot_id_to_key_pair_id(spdm_context, slot_id, true),
1321 : : SPDM_ENDPOINT_INFO,
1322 : 10 : spdm_context->connection_info.algorithm.req_base_asym_alg,
1323 : : spdm_context->connection_info.algorithm.req_pqc_asym_alg,
1324 : : spdm_context->connection_info.algorithm.base_hash_algo,
1325 : : true, il1il2_hash, il1il2_hash_size, signature, &signature_size);
1326 : : #endif
1327 : : } else {
1328 [ - + ]: 8 : if (spdm_context->connection_info.algorithm.pqc_asym_algo != 0) {
1329 : 0 : signature_size = libspdm_get_pqc_asym_signature_size(
1330 : : spdm_context->connection_info.algorithm.pqc_asym_algo);
1331 : : } else {
1332 : 8 : signature_size = libspdm_get_asym_signature_size(
1333 : : spdm_context->connection_info.algorithm.base_asym_algo);
1334 : : }
1335 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1336 : : result = libspdm_responder_data_sign(
1337 : : spdm_context,
1338 : : spdm_context->connection_info.version,
1339 : : libspdm_slot_id_to_key_pair_id(spdm_context, slot_id, false),
1340 : : SPDM_ENDPOINT_INFO,
1341 : : spdm_context->connection_info.algorithm.base_asym_algo,
1342 : : spdm_context->connection_info.algorithm.pqc_asym_algo,
1343 : : spdm_context->connection_info.algorithm.base_hash_algo,
1344 : : false, il1il2_buffer, il1il2_buffer_size, signature,
1345 : : &signature_size);
1346 : : #else
1347 : 16 : result = libspdm_responder_data_sign(
1348 : : spdm_context,
1349 : 8 : spdm_context->connection_info.version,
1350 : 8 : libspdm_slot_id_to_key_pair_id(spdm_context, slot_id, false),
1351 : : SPDM_ENDPOINT_INFO,
1352 : : spdm_context->connection_info.algorithm.base_asym_algo,
1353 : : spdm_context->connection_info.algorithm.pqc_asym_algo,
1354 : : spdm_context->connection_info.algorithm.base_hash_algo,
1355 : : true, il1il2_hash, il1il2_hash_size, signature,
1356 : : &signature_size);
1357 : : #endif
1358 : : }
1359 : :
1360 : 18 : return result;
1361 : : }
1362 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_ENDPOINT_INFO_CAP */
1363 : :
1364 : 46 : bool libspdm_verify_endpoint_info_signature(libspdm_context_t *spdm_context,
1365 : : libspdm_session_info_t *session_info,
1366 : : bool is_requester,
1367 : : uint8_t slot_id,
1368 : : const void *sign_data,
1369 : : size_t sign_data_size)
1370 : : {
1371 : : bool result;
1372 : : void *context;
1373 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1374 : : libspdm_il1il2_managed_buffer_t il1il2;
1375 : : uint8_t *il1il2_buffer;
1376 : : size_t il1il2_buffer_size;
1377 : : #if LIBSPDM_CERT_PARSE_SUPPORT
1378 : : const uint8_t *cert_chain_data;
1379 : : size_t cert_chain_data_size;
1380 : : const uint8_t *cert_buffer;
1381 : : size_t cert_buffer_size;
1382 : : #endif /* LIBSPDM_CERT_PARSE_SUPPORT */
1383 : : #else
1384 : : uint8_t il1il2_hash[LIBSPDM_MAX_HASH_SIZE];
1385 : : size_t il1il2_hash_size;
1386 : : #endif
1387 : :
1388 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1389 : : result = libspdm_calculate_il1il2(spdm_context, session_info,!is_requester, &il1il2);
1390 : : il1il2_buffer = libspdm_get_managed_buffer(&il1il2);
1391 : : il1il2_buffer_size = libspdm_get_managed_buffer_size(&il1il2);
1392 : : #else
1393 : 46 : il1il2_hash_size = sizeof(il1il2_hash);
1394 : 46 : result = libspdm_calculate_il1il2_hash(spdm_context, session_info, !is_requester,
1395 : 46 : &il1il2_hash_size, il1il2_hash);
1396 : : #endif
1397 [ + + ]: 46 : if (is_requester) {
1398 : 26 : libspdm_reset_message_e(spdm_context, session_info);
1399 : : } else {
1400 : 20 : libspdm_reset_message_encap_e(spdm_context, session_info);
1401 : : }
1402 [ - + ]: 46 : if (!result) {
1403 : 0 : return false;
1404 : : }
1405 : :
1406 [ + + ]: 46 : if (slot_id == 0xF) {
1407 [ + + ]: 8 : if (is_requester) {
1408 [ + - ]: 4 : if (spdm_context->connection_info.algorithm.base_asym_algo != 0) {
1409 : 4 : result = libspdm_asym_get_public_key_from_der(
1410 : : spdm_context->connection_info.algorithm.base_asym_algo,
1411 : 4 : spdm_context->local_context.peer_public_key_provision,
1412 : : spdm_context->local_context.peer_public_key_provision_size,
1413 : : &context);
1414 : : }
1415 [ - + ]: 4 : if (spdm_context->connection_info.algorithm.pqc_asym_algo != 0) {
1416 : 0 : result = libspdm_pqc_asym_get_public_key_from_der(
1417 : : spdm_context->connection_info.algorithm.pqc_asym_algo,
1418 : 0 : spdm_context->local_context.peer_public_key_provision,
1419 : : spdm_context->local_context.peer_public_key_provision_size,
1420 : : &context);
1421 : : }
1422 : : } else {
1423 [ + - ]: 4 : if (spdm_context->connection_info.algorithm.req_base_asym_alg != 0) {
1424 : 4 : result = libspdm_req_asym_get_public_key_from_der(
1425 : 4 : spdm_context->connection_info.algorithm.req_base_asym_alg,
1426 : 4 : spdm_context->local_context.peer_public_key_provision,
1427 : : spdm_context->local_context.peer_public_key_provision_size,
1428 : : &context);
1429 : : }
1430 [ - + ]: 4 : if (spdm_context->connection_info.algorithm.req_pqc_asym_alg != 0) {
1431 : 0 : result = libspdm_req_pqc_asym_get_public_key_from_der(
1432 : : spdm_context->connection_info.algorithm.req_pqc_asym_alg,
1433 : 0 : spdm_context->local_context.peer_public_key_provision,
1434 : : spdm_context->local_context.peer_public_key_provision_size,
1435 : : &context);
1436 : : }
1437 : : }
1438 [ - + ]: 8 : if (!result) {
1439 : 0 : return false;
1440 : : }
1441 : : } else {
1442 : : #if (LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT) && (LIBSPDM_CERT_PARSE_SUPPORT)
1443 : : libspdm_get_peer_cert_chain_data(
1444 : : spdm_context, slot_id, (const void **)&cert_chain_data, &cert_chain_data_size);
1445 : :
1446 : : /* Get leaf cert from cert chain*/
1447 : : result = libspdm_x509_get_cert_from_cert_chain(cert_chain_data,
1448 : : cert_chain_data_size, -1,
1449 : : &cert_buffer, &cert_buffer_size);
1450 : : if (!result) {
1451 : : return false;
1452 : : }
1453 : :
1454 : : if (is_requester) {
1455 : : result = libspdm_asym_get_public_key_from_x509(
1456 : : spdm_context->connection_info.algorithm.base_asym_algo,
1457 : : cert_buffer, cert_buffer_size, &context);
1458 : : } else {
1459 : : result = libspdm_req_asym_get_public_key_from_x509(
1460 : : spdm_context->connection_info.algorithm.req_base_asym_alg,
1461 : : cert_buffer, cert_buffer_size, &context);
1462 : : }
1463 : : if (!result) {
1464 : : return false;
1465 : : }
1466 : : #elif LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1467 : : return false;
1468 : : #else
1469 : 38 : context = spdm_context->connection_info.peer_used_cert_chain[slot_id].leaf_cert_public_key;
1470 [ + + ]: 38 : if (context == NULL) {
1471 : : /* No certificate chain has been recorded for the slot. */
1472 : 2 : return false;
1473 : : }
1474 : : #endif
1475 : : }
1476 : :
1477 [ + + ]: 44 : if (is_requester) {
1478 [ + - ]: 26 : if (spdm_context->connection_info.algorithm.base_asym_algo != 0) {
1479 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1480 : : result = libspdm_asym_verify_ex(
1481 : : spdm_context->connection_info.version, SPDM_ENDPOINT_INFO,
1482 : : spdm_context->connection_info.algorithm.base_asym_algo,
1483 : : spdm_context->connection_info.algorithm.base_hash_algo,
1484 : : context, il1il2_buffer, il1il2_buffer_size, sign_data, sign_data_size,
1485 : : &spdm_context->spdm_10_11_verify_signature_endian);
1486 : : libspdm_asym_free(
1487 : : spdm_context->connection_info.algorithm.base_asym_algo, context);
1488 : : #else
1489 : 26 : result = libspdm_asym_verify_hash_ex(
1490 : 26 : spdm_context->connection_info.version, SPDM_ENDPOINT_INFO,
1491 : : spdm_context->connection_info.algorithm.base_asym_algo,
1492 : : spdm_context->connection_info.algorithm.base_hash_algo,
1493 : : context, il1il2_hash, il1il2_hash_size, sign_data, sign_data_size,
1494 : : &spdm_context->spdm_10_11_verify_signature_endian);
1495 [ + + ]: 26 : if (slot_id == 0xF) {
1496 : 4 : libspdm_asym_free(
1497 : : spdm_context->connection_info.algorithm.base_asym_algo, context);
1498 : : }
1499 : : #endif
1500 : : }
1501 [ - + ]: 26 : if (spdm_context->connection_info.algorithm.pqc_asym_algo != 0) {
1502 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1503 : : result = libspdm_pqc_asym_verify(
1504 : : spdm_context->connection_info.version, SPDM_ENDPOINT_INFO,
1505 : : spdm_context->connection_info.algorithm.pqc_asym_algo,
1506 : : spdm_context->connection_info.algorithm.base_hash_algo,
1507 : : context, il1il2_buffer, il1il2_buffer_size, sign_data, sign_data_size);
1508 : : libspdm_pqc_asym_free(
1509 : : spdm_context->connection_info.algorithm.pqc_asym_algo, context);
1510 : : #else
1511 : 0 : result = libspdm_pqc_asym_verify_hash(
1512 : 0 : spdm_context->connection_info.version, SPDM_ENDPOINT_INFO,
1513 : : spdm_context->connection_info.algorithm.pqc_asym_algo,
1514 : : spdm_context->connection_info.algorithm.base_hash_algo,
1515 : : context, il1il2_hash, il1il2_hash_size, sign_data, sign_data_size);
1516 [ # # ]: 0 : if (slot_id == 0xF) {
1517 : 0 : libspdm_pqc_asym_free(
1518 : : spdm_context->connection_info.algorithm.pqc_asym_algo, context);
1519 : : }
1520 : : #endif
1521 : : }
1522 : : } else {
1523 [ + - ]: 18 : if (spdm_context->connection_info.algorithm.req_base_asym_alg != 0) {
1524 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1525 : : result = libspdm_req_asym_verify_ex(
1526 : : spdm_context->connection_info.version, SPDM_ENDPOINT_INFO,
1527 : : spdm_context->connection_info.algorithm.req_base_asym_alg,
1528 : : spdm_context->connection_info.algorithm.base_hash_algo,
1529 : : context, il1il2_buffer, il1il2_buffer_size, sign_data, sign_data_size,
1530 : : &spdm_context->spdm_10_11_verify_signature_endian);
1531 : : libspdm_req_asym_free(
1532 : : spdm_context->connection_info.algorithm.req_base_asym_alg, context);
1533 : : #else
1534 : 18 : result = libspdm_req_asym_verify_hash_ex(
1535 : 18 : spdm_context->connection_info.version, SPDM_ENDPOINT_INFO,
1536 : 18 : spdm_context->connection_info.algorithm.req_base_asym_alg,
1537 : : spdm_context->connection_info.algorithm.base_hash_algo,
1538 : : context, il1il2_hash, il1il2_hash_size, sign_data, sign_data_size,
1539 : : &spdm_context->spdm_10_11_verify_signature_endian);
1540 [ + + ]: 18 : if (slot_id == 0xF) {
1541 : 4 : libspdm_req_asym_free(
1542 : 4 : spdm_context->connection_info.algorithm.req_base_asym_alg, context);
1543 : : }
1544 : : #endif
1545 : : }
1546 [ - + ]: 18 : if (spdm_context->connection_info.algorithm.req_pqc_asym_alg != 0) {
1547 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1548 : : result = libspdm_req_pqc_asym_verify(
1549 : : spdm_context->connection_info.version, SPDM_ENDPOINT_INFO,
1550 : : spdm_context->connection_info.algorithm.req_pqc_asym_alg,
1551 : : spdm_context->connection_info.algorithm.base_hash_algo,
1552 : : context, il1il2_buffer, il1il2_buffer_size, sign_data, sign_data_size);
1553 : : libspdm_req_pqc_asym_free(
1554 : : spdm_context->connection_info.algorithm.req_pqc_asym_alg, context);
1555 : : #else
1556 : 0 : result = libspdm_req_pqc_asym_verify_hash(
1557 : 0 : spdm_context->connection_info.version, SPDM_ENDPOINT_INFO,
1558 : : spdm_context->connection_info.algorithm.req_pqc_asym_alg,
1559 : : spdm_context->connection_info.algorithm.base_hash_algo,
1560 : : context, il1il2_hash, il1il2_hash_size, sign_data, sign_data_size);
1561 [ # # ]: 0 : if (slot_id == 0xF) {
1562 : 0 : libspdm_req_pqc_asym_free(
1563 : : spdm_context->connection_info.algorithm.req_pqc_asym_alg, context);
1564 : : }
1565 : : #endif
1566 : : }
1567 : : }
1568 [ + + ]: 44 : if (!result) {
1569 : 6 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR, "!!! verify_endpoint_info_signature - FAIL !!!\n"));
1570 : 6 : return false;
1571 : : }
1572 : :
1573 : 38 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "!!! verify_endpoint_info_signature - PASS !!!\n"));
1574 : 38 : return true;
1575 : : }
|