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 : : #include "internal/libspdm_secured_message_lib.h"
9 : : #include "internal/libspdm_fips_lib.h"
10 : :
11 : : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
12 : : /* first section */
13 : 6134 : uint32_t libspdm_get_scratch_buffer_secure_message_offset(void) {
14 : 6134 : return 0;
15 : : }
16 : :
17 : 80390 : uint32_t libspdm_get_scratch_buffer_secure_message_capacity(libspdm_context_t *spdm_context) {
18 : 80390 : return spdm_context->local_context.capability.max_spdm_msg_size +
19 : 160780 : spdm_context->local_context.capability.transport_header_size +
20 : 80390 : spdm_context->local_context.capability.transport_tail_size;
21 : : }
22 : :
23 : : /* second section */
24 : 158 : uint32_t libspdm_get_scratch_buffer_large_message_offset(libspdm_context_t *spdm_context) {
25 : 158 : return libspdm_get_scratch_buffer_secure_message_capacity(spdm_context);
26 : : }
27 : :
28 : 74178 : uint32_t libspdm_get_scratch_buffer_large_message_capacity(libspdm_context_t *spdm_context) {
29 : 74178 : return spdm_context->local_context.capability.max_spdm_msg_size;
30 : : }
31 : : #endif
32 : :
33 : : /* third section */
34 : 12768 : uint32_t libspdm_get_scratch_buffer_sender_receiver_offset(libspdm_context_t *spdm_context) {
35 : 12768 : return 0 +
36 : : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
37 : : libspdm_get_scratch_buffer_secure_message_capacity(spdm_context) +
38 : 12768 : libspdm_get_scratch_buffer_large_message_capacity(spdm_context) +
39 : : #endif
40 : : 0;
41 : : }
42 : :
43 : 67834 : uint32_t libspdm_get_scratch_buffer_sender_receiver_capacity(libspdm_context_t *spdm_context) {
44 : 67834 : return spdm_context->local_context.capability.max_spdm_msg_size +
45 : 135668 : spdm_context->local_context.capability.transport_header_size +
46 : 67834 : spdm_context->local_context.capability.transport_tail_size;
47 : : }
48 : :
49 : : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
50 : : /* fourth section */
51 : 30256 : uint32_t libspdm_get_scratch_buffer_large_sender_receiver_offset(libspdm_context_t *spdm_context) {
52 : 30256 : return libspdm_get_scratch_buffer_secure_message_capacity(spdm_context) +
53 : 60512 : libspdm_get_scratch_buffer_large_message_capacity(spdm_context) +
54 : 30256 : libspdm_get_scratch_buffer_sender_receiver_capacity(spdm_context);
55 : : }
56 : :
57 : 55278 : uint32_t libspdm_get_scratch_buffer_large_sender_receiver_capacity(libspdm_context_t *spdm_context)
58 : : {
59 : 55278 : return spdm_context->local_context.capability.max_spdm_msg_size +
60 : 110556 : spdm_context->local_context.capability.transport_header_size +
61 : 55278 : spdm_context->local_context.capability.transport_tail_size;
62 : : }
63 : : #endif
64 : :
65 : : /* fifth section */
66 : 14430 : uint32_t libspdm_get_scratch_buffer_last_spdm_request_offset(libspdm_context_t *spdm_context) {
67 : 14430 : return 0 +
68 : : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
69 : 14430 : libspdm_get_scratch_buffer_secure_message_capacity(spdm_context) +
70 : 14430 : libspdm_get_scratch_buffer_large_message_capacity(spdm_context) +
71 : : #endif
72 : 14430 : libspdm_get_scratch_buffer_sender_receiver_capacity(spdm_context) +
73 : : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
74 : 14430 : libspdm_get_scratch_buffer_large_sender_receiver_capacity(spdm_context) +
75 : : #endif
76 : : 0;
77 : : }
78 : :
79 : 22926 : uint32_t libspdm_get_scratch_buffer_last_spdm_request_capacity(libspdm_context_t *spdm_context) {
80 : 22926 : return spdm_context->local_context.capability.max_spdm_msg_size;
81 : : }
82 : :
83 : : #if LIBSPDM_RESPOND_IF_READY_SUPPORT
84 : : /* sixth section */
85 : 1102 : uint32_t libspdm_get_scratch_buffer_cache_spdm_request_offset(libspdm_context_t *spdm_context) {
86 : 1102 : return 0 +
87 : : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
88 : 1102 : libspdm_get_scratch_buffer_secure_message_capacity(spdm_context) +
89 : 1102 : libspdm_get_scratch_buffer_large_message_capacity(spdm_context) +
90 : : #endif
91 : 1102 : libspdm_get_scratch_buffer_sender_receiver_capacity(spdm_context) +
92 : : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
93 : 1102 : libspdm_get_scratch_buffer_large_sender_receiver_capacity(spdm_context) +
94 : : #endif
95 : 1102 : libspdm_get_scratch_buffer_last_spdm_request_capacity(spdm_context) +
96 : : 0;
97 : : }
98 : :
99 : 15806 : uint32_t libspdm_get_scratch_buffer_cache_spdm_request_capacity(libspdm_context_t *spdm_context) {
100 : 15806 : return spdm_context->local_context.capability.max_spdm_msg_size;
101 : : }
102 : : #endif
103 : :
104 : : /* combination */
105 : 15530 : uint32_t libspdm_get_scratch_buffer_capacity(libspdm_context_t *spdm_context) {
106 : 15530 : return 0 +
107 : : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
108 : 15530 : libspdm_get_scratch_buffer_secure_message_capacity(spdm_context) +
109 : 15530 : libspdm_get_scratch_buffer_large_message_capacity(spdm_context) +
110 : : #endif
111 : 15530 : libspdm_get_scratch_buffer_sender_receiver_capacity(spdm_context) +
112 : : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
113 : 15530 : libspdm_get_scratch_buffer_large_sender_receiver_capacity(spdm_context) +
114 : : #endif
115 : 15530 : libspdm_get_scratch_buffer_last_spdm_request_capacity(spdm_context) +
116 : : #if LIBSPDM_RESPOND_IF_READY_SUPPORT
117 : 15530 : libspdm_get_scratch_buffer_cache_spdm_request_capacity(spdm_context) +
118 : : #endif
119 : : 0;
120 : : }
121 : :
122 : : /**
123 : : * Returns if an SPDM data_type requires session info.
124 : : *
125 : : * @param data_type SPDM data type.
126 : : *
127 : : * @retval true session info is required.
128 : : * @retval false session info is not required.
129 : : **/
130 : 1250 : static bool need_session_info_for_data(libspdm_data_type_t data_type)
131 : : {
132 [ + + ]: 1250 : switch (data_type) {
133 : 58 : case LIBSPDM_DATA_SESSION_SECURED_MESSAGE_VERSION:
134 : : case LIBSPDM_DATA_SESSION_USE_PSK:
135 : : case LIBSPDM_DATA_SESSION_MUT_AUTH_REQUESTED:
136 : : case LIBSPDM_DATA_SESSION_END_SESSION_ATTRIBUTES:
137 : : case LIBSPDM_DATA_SESSION_POLICY:
138 : : case LIBSPDM_DATA_SESSION_SEQUENCE_NUMBER_RSP_DIR:
139 : : case LIBSPDM_DATA_SESSION_SEQUENCE_NUMBER_REQ_DIR:
140 : : case LIBSPDM_DATA_SESSION_SEQUENCE_NUMBER_ENDIAN:
141 : : #if (LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP) && (LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP)
142 : : case LIBSPDM_DATA_SESSION_ENCAP_REQ_SLOT_ID:
143 : : #endif
144 : 58 : return true;
145 : 1192 : default:
146 : 1192 : return false;
147 : : }
148 : : }
149 : :
150 : 1008 : libspdm_return_t libspdm_set_data(void *spdm_context, libspdm_data_type_t data_type,
151 : : const libspdm_data_parameter_t *parameter, const void *data,
152 : : size_t data_size)
153 : : {
154 : : libspdm_context_t *context;
155 : : uint32_t session_id;
156 : : uint32_t data32;
157 : : libspdm_session_info_t *session_info;
158 : : uint8_t slot_id;
159 : : #if LIBSPDM_CERT_PARSE_SUPPORT
160 : : size_t root_cert_index;
161 : : #endif
162 : : uint16_t data16;
163 : : #if !(LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT) && LIBSPDM_CERT_PARSE_SUPPORT
164 : : bool status;
165 : : uint32_t peer_base_asym_algo;
166 : : uint32_t peer_pqc_asym_algo;
167 : : #endif
168 : :
169 [ + + + + ]: 1008 : if (spdm_context == NULL || data_type >= LIBSPDM_DATA_MAX) {
170 : 4 : return LIBSPDM_STATUS_INVALID_PARAMETER;
171 : : }
172 : :
173 : 1004 : context = spdm_context;
174 : :
175 [ + + ]: 1004 : if (need_session_info_for_data(data_type)) {
176 [ + + ]: 40 : if (parameter->location != LIBSPDM_DATA_LOCATION_SESSION) {
177 : 4 : return LIBSPDM_STATUS_INVALID_PARAMETER;
178 : : }
179 : 36 : session_id = libspdm_read_uint32(parameter->additional_data);
180 : 36 : session_info = libspdm_get_session_info_via_session_id(context, session_id);
181 [ + + ]: 36 : if (session_info == NULL) {
182 : 4 : return LIBSPDM_STATUS_INVALID_PARAMETER;
183 : : }
184 : : } else {
185 : 964 : session_info = NULL;
186 : : }
187 : :
188 [ + + + + : 996 : switch (data_type) {
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + ]
189 : 8 : case LIBSPDM_DATA_SPDM_VERSION:
190 : 8 : LIBSPDM_ASSERT (data_size <= sizeof(spdm_version_number_t) * SPDM_MAX_VERSION_COUNT);
191 [ + + ]: 8 : if (parameter->location == LIBSPDM_DATA_LOCATION_CONNECTION) {
192 : : /* Only have one connected version */
193 : 2 : LIBSPDM_ASSERT (data_size == sizeof(spdm_version_number_t));
194 : 2 : libspdm_copy_mem(&(context->connection_info.version),
195 : : sizeof(context->connection_info.version),
196 : : data,
197 : : sizeof(spdm_version_number_t));
198 [ + + ]: 6 : } else if (parameter->location == LIBSPDM_DATA_LOCATION_LOCAL) {
199 : 4 : context->local_context.version.spdm_version_count =
200 : 4 : (uint8_t)(data_size / sizeof(spdm_version_number_t));
201 : 4 : libspdm_copy_mem(context->local_context.version.spdm_version,
202 : : sizeof(context->local_context.version.spdm_version),
203 : : data,
204 : 4 : context->local_context.version.spdm_version_count *
205 : : sizeof(spdm_version_number_t));
206 : : } else {
207 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
208 : : }
209 : 6 : break;
210 : 4 : case LIBSPDM_DATA_SECURED_MESSAGE_VERSION:
211 : 4 : LIBSPDM_ASSERT (data_size <=
212 : : sizeof(spdm_version_number_t) * SECURED_SPDM_MAX_VERSION_COUNT);
213 [ + + ]: 4 : if (parameter->location == LIBSPDM_DATA_LOCATION_LOCAL) {
214 : 2 : context->local_context.secured_message_version.secured_message_version_count =
215 : 2 : (uint8_t)(data_size / sizeof(spdm_version_number_t));
216 : 2 : libspdm_copy_mem(context->local_context.secured_message_version.secured_message_version,
217 : : sizeof(context->local_context.secured_message_version.secured_message_version),
218 : : data,
219 : 2 : context->local_context.secured_message_version.
220 : : secured_message_version_count * sizeof(spdm_version_number_t));
221 : : } else {
222 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
223 : : }
224 : 2 : break;
225 : 10 : case LIBSPDM_DATA_CAPABILITY_FLAGS:
226 [ + + ]: 10 : if (data_size != sizeof(uint32_t)) {
227 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
228 : : }
229 : :
230 : 8 : data32 = libspdm_read_uint32((const uint8_t *)data);
231 : :
232 [ + + ]: 8 : if (parameter->location == LIBSPDM_DATA_LOCATION_LOCAL) {
233 : : #if !(LIBSPDM_ENABLE_CAPABILITY_CERT_CAP)
234 : : LIBSPDM_ASSERT((data32 & SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP) == 0);
235 : : #endif /* !LIBSPDM_ENABLE_CAPABILITY_CERT_CAP */
236 : :
237 : : #if !(LIBSPDM_ENABLE_CAPABILITY_CHAL_CAP)
238 : : LIBSPDM_ASSERT((data32 & SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CHAL_CAP) == 0);
239 : : #endif /* !LIBSPDM_ENABLE_CAPABILITY_CHAL_CAP */
240 : :
241 : : #if !(LIBSPDM_ENABLE_CAPABILITY_MEAS_CAP)
242 : : LIBSPDM_ASSERT((data32 & SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEAS_CAP) == 0);
243 : : #endif /* !LIBSPDM_ENABLE_CAPABILITY_MEAS_CAP */
244 : :
245 : : #if !(LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP)
246 : : LIBSPDM_ASSERT((data32 & SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MUT_AUTH_CAP) == 0);
247 : : #endif /* !LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP */
248 : :
249 : : #if !(LIBSPDM_ENABLE_CAPABILITY_KEY_EX_CAP)
250 : : LIBSPDM_ASSERT((data32 & SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP) == 0);
251 : : #endif /* !LIBSPDM_ENABLE_CAPABILITY_KEY_EX_CAP */
252 : :
253 : : #if !(LIBSPDM_ENABLE_CAPABILITY_PSK_CAP)
254 : : LIBSPDM_ASSERT((data32 & SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PSK_CAP) == 0);
255 : : #endif /* !LIBSPDM_ENABLE_CAPABILITY_PSK_CAP */
256 : :
257 : : #if !(LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP)
258 : : LIBSPDM_ASSERT((data32 & SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCAP_CAP) == 0);
259 : : #endif /* !LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP */
260 : :
261 : : #if !(LIBSPDM_ENABLE_CAPABILITY_HBEAT_CAP)
262 : : LIBSPDM_ASSERT((data32 & SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HBEAT_CAP) == 0);
263 : : #endif /* !LIBSPDM_ENABLE_CAPABILITY_HBEAT_CAP */
264 : :
265 : : #if !(LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP)
266 : : LIBSPDM_ASSERT((data32 & SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CHUNK_CAP) == 0);
267 : : #endif /* !LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP */
268 : :
269 : : #if !(LIBSPDM_ENABLE_CAPABILITY_SET_CERT_CAP)
270 : : LIBSPDM_ASSERT((data32 & SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_SET_CERT_CAP) == 0);
271 : : #endif /* !LIBSPDM_ENABLE_CAPABILITY_SET_CERT_CAP */
272 : :
273 : : #if !(LIBSPDM_ENABLE_CAPABILITY_CSR_CAP)
274 : : LIBSPDM_ASSERT((data32 & SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CSR_CAP) == 0);
275 : : #endif /* !LIBSPDM_ENABLE_CAPABILITY_CSR_CAP */
276 : :
277 : : #if !(LIBSPDM_ENABLE_CAPABILITY_ENDPOINT_INFO_CAP)
278 : : LIBSPDM_ASSERT((data32 & SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_EP_INFO_CAP) == 0);
279 : : #endif /* !LIBSPDM_ENABLE_CAPABILITY_ENDPOINT_INFO_CAP */
280 : :
281 : : #if !(LIBSPDM_ENABLE_CAPABILITY_MEL_CAP)
282 : : LIBSPDM_ASSERT((data32 & SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEL_CAP) == 0);
283 : : #endif /* !LIBSPDM_ENABLE_CAPABILITY_MEL_CAP */
284 : :
285 : : #if !(LIBSPDM_ENABLE_CAPABILITY_EVENT_CAP)
286 : : LIBSPDM_ASSERT((data32 & SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_EVENT_CAP) == 0);
287 : : #endif /* !LIBSPDM_ENABLE_CAPABILITY_EVENT_CAP */
288 : :
289 : : #if !(LIBSPDM_ENABLE_CAPABILITY_GET_KEY_PAIR_INFO_CAP)
290 : : LIBSPDM_ASSERT(
291 : : (data32 & SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_GET_KEY_PAIR_INFO_CAP) == 0);
292 : : #endif /* !LIBSPDM_ENABLE_CAPABILITY_GET_KEY_PAIR_INFO_CAP */
293 : :
294 : : #if !(LIBSPDM_ENABLE_CAPABILITY_SET_KEY_PAIR_INFO_CAP)
295 : : LIBSPDM_ASSERT(
296 : : (data32 & SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_SET_KEY_PAIR_INFO_CAP) == 0);
297 : : #endif /* !LIBSPDM_ENABLE_CAPABILITY_SET_KEY_PAIR_INFO_CAP */
298 : :
299 : 4 : context->local_context.capability.flags = data32;
300 [ + + ]: 4 : } else if (parameter->location == LIBSPDM_DATA_LOCATION_CONNECTION) {
301 : 2 : context->connection_info.capability.flags = data32;
302 : : } else {
303 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
304 : : }
305 : 6 : break;
306 : 8 : case LIBSPDM_DATA_CAPABILITY_EXT_FLAGS:
307 [ + + ]: 8 : if (data_size != sizeof(uint16_t)) {
308 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
309 : : }
310 : :
311 : 6 : data16 = libspdm_read_uint16((const uint8_t *)data);
312 : :
313 [ + + ]: 6 : if (parameter->location == LIBSPDM_DATA_LOCATION_LOCAL) {
314 : 2 : context->local_context.capability.ext_flags = data16;
315 [ + + ]: 4 : } else if (parameter->location == LIBSPDM_DATA_LOCATION_CONNECTION) {
316 : 2 : context->connection_info.capability.ext_flags = data16;
317 : : } else {
318 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
319 : : }
320 : 4 : break;
321 : 8 : case LIBSPDM_DATA_CAPABILITY_CT_EXPONENT:
322 [ + + ]: 8 : if (data_size != sizeof(uint8_t)) {
323 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
324 : : }
325 [ + + ]: 6 : if (parameter->location == LIBSPDM_DATA_LOCATION_CONNECTION) {
326 : 2 : context->connection_info.capability.ct_exponent = *(const uint8_t *)data;
327 [ + + ]: 4 : } else if (parameter->location == LIBSPDM_DATA_LOCATION_LOCAL) {
328 : 2 : context->local_context.capability.ct_exponent = *(const uint8_t *)data;
329 : : } else {
330 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
331 : : }
332 : 4 : break;
333 : 6 : case LIBSPDM_DATA_CAPABILITY_RTT_US:
334 [ + + ]: 6 : if (data_size != sizeof(uint64_t)) {
335 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
336 : : }
337 [ + + ]: 4 : if (parameter->location != LIBSPDM_DATA_LOCATION_LOCAL) {
338 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
339 : : }
340 : 2 : context->local_context.capability.rtt = libspdm_read_uint64((const uint8_t *)data);
341 : 2 : break;
342 : 6 : case LIBSPDM_DATA_CAPABILITY_MAX_SPDM_MSG_SIZE:
343 [ + + ]: 6 : if (data_size != sizeof(uint32_t)) {
344 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
345 : : }
346 : : /* The local max_spdm_msg_size is set by libspdm_register_transport_layer_func.
347 : : * Only the connection's max_spdm_msg_size is settable here. */
348 [ + + ]: 4 : if (parameter->location != LIBSPDM_DATA_LOCATION_CONNECTION) {
349 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
350 : : }
351 : 2 : data32 = libspdm_read_uint32((const uint8_t *)data);
352 : 2 : LIBSPDM_ASSERT (data32 >= SPDM_MIN_DATA_TRANSFER_SIZE_VERSION_12);
353 : 2 : context->connection_info.capability.max_spdm_msg_size = data32;
354 : 2 : break;
355 : 8 : case LIBSPDM_DATA_MEASUREMENT_SPEC:
356 [ + + ]: 8 : if (data_size != sizeof(uint8_t)) {
357 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
358 : : }
359 [ + + ]: 6 : if (parameter->location == LIBSPDM_DATA_LOCATION_CONNECTION) {
360 : 2 : context->connection_info.algorithm.measurement_spec = *(const uint8_t *)data;
361 [ + + ]: 4 : } else if (parameter->location == LIBSPDM_DATA_LOCATION_LOCAL) {
362 : 2 : context->local_context.algorithm.measurement_spec = *(const uint8_t *)data;
363 : : } else {
364 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
365 : : }
366 : 4 : break;
367 : 8 : case LIBSPDM_DATA_MEASUREMENT_HASH_ALGO:
368 [ + + ]: 8 : if (data_size != sizeof(uint32_t)) {
369 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
370 : : }
371 : 6 : data32 = libspdm_read_uint32((const uint8_t *)data);
372 [ + + ]: 6 : if (parameter->location == LIBSPDM_DATA_LOCATION_CONNECTION) {
373 : 2 : context->connection_info.algorithm.measurement_hash_algo = data32;
374 [ + + ]: 4 : } else if (parameter->location == LIBSPDM_DATA_LOCATION_LOCAL) {
375 : 2 : context->local_context.algorithm.measurement_hash_algo = data32;
376 : : } else {
377 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
378 : : }
379 : 4 : break;
380 : 8 : case LIBSPDM_DATA_BASE_ASYM_ALGO:
381 [ + + ]: 8 : if (data_size != sizeof(uint32_t)) {
382 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
383 : : }
384 : 6 : data32 = libspdm_read_uint32((const uint8_t *)data);
385 [ + + ]: 6 : if (parameter->location == LIBSPDM_DATA_LOCATION_CONNECTION) {
386 : 2 : context->connection_info.algorithm.base_asym_algo = data32;
387 [ + + ]: 4 : } else if (parameter->location == LIBSPDM_DATA_LOCATION_LOCAL) {
388 : 2 : context->local_context.algorithm.base_asym_algo = data32;
389 : : } else {
390 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
391 : : }
392 : 4 : break;
393 : 8 : case LIBSPDM_DATA_BASE_HASH_ALGO:
394 [ + + ]: 8 : if (data_size != sizeof(uint32_t)) {
395 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
396 : : }
397 : 6 : data32 = libspdm_read_uint32((const uint8_t *)data);
398 [ + + ]: 6 : if (parameter->location == LIBSPDM_DATA_LOCATION_CONNECTION) {
399 : 2 : context->connection_info.algorithm.base_hash_algo = data32;
400 [ + + ]: 4 : } else if (parameter->location == LIBSPDM_DATA_LOCATION_LOCAL) {
401 : 2 : context->local_context.algorithm.base_hash_algo = data32;
402 : : } else {
403 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
404 : : }
405 : 4 : break;
406 : 8 : case LIBSPDM_DATA_DHE_NAME_GROUP:
407 [ + + ]: 8 : if (data_size != sizeof(uint16_t)) {
408 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
409 : : }
410 : 6 : data16 = libspdm_read_uint16((const uint8_t *)data);
411 [ + + ]: 6 : if (parameter->location == LIBSPDM_DATA_LOCATION_CONNECTION) {
412 : 2 : context->connection_info.algorithm.dhe_named_group = data16;
413 [ + + ]: 4 : } else if (parameter->location == LIBSPDM_DATA_LOCATION_LOCAL) {
414 : 2 : context->local_context.algorithm.dhe_named_group = data16;
415 : : } else {
416 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
417 : : }
418 : 4 : break;
419 : 8 : case LIBSPDM_DATA_AEAD_CIPHER_SUITE:
420 [ + + ]: 8 : if (data_size != sizeof(uint16_t)) {
421 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
422 : : }
423 : 6 : data16 = libspdm_read_uint16((const uint8_t *)data);
424 [ + + ]: 6 : if (parameter->location == LIBSPDM_DATA_LOCATION_CONNECTION) {
425 : 2 : context->connection_info.algorithm.aead_cipher_suite = data16;
426 [ + + ]: 4 : } else if (parameter->location == LIBSPDM_DATA_LOCATION_LOCAL) {
427 : 2 : context->local_context.algorithm.aead_cipher_suite = data16;
428 : : } else {
429 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
430 : : }
431 : 4 : break;
432 : 8 : case LIBSPDM_DATA_REQ_BASE_ASYM_ALG:
433 [ + + ]: 8 : if (data_size != sizeof(uint16_t)) {
434 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
435 : : }
436 : 6 : data16 = libspdm_read_uint16((const uint8_t *)data);
437 [ + + ]: 6 : if (parameter->location == LIBSPDM_DATA_LOCATION_CONNECTION) {
438 : 2 : context->connection_info.algorithm.req_base_asym_alg = data16;
439 [ + + ]: 4 : } else if (parameter->location == LIBSPDM_DATA_LOCATION_LOCAL) {
440 : 2 : context->local_context.algorithm.req_base_asym_alg = data16;
441 : : } else {
442 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
443 : : }
444 : 4 : break;
445 : 8 : case LIBSPDM_DATA_KEY_SCHEDULE:
446 [ + + ]: 8 : if (data_size != sizeof(uint16_t)) {
447 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
448 : : }
449 : 6 : data16 = libspdm_read_uint16((const uint8_t *)data);
450 [ + + ]: 6 : if (parameter->location == LIBSPDM_DATA_LOCATION_CONNECTION) {
451 : 2 : context->connection_info.algorithm.key_schedule = data16;
452 [ + + ]: 4 : } else if (parameter->location == LIBSPDM_DATA_LOCATION_LOCAL) {
453 : 2 : context->local_context.algorithm.key_schedule = data16;
454 : : } else {
455 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
456 : : }
457 : 4 : break;
458 : 8 : case LIBSPDM_DATA_OTHER_PARAMS_SUPPORT:
459 [ + + ]: 8 : if (data_size != sizeof(uint8_t)) {
460 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
461 : : }
462 [ + + ]: 6 : if (parameter->location == LIBSPDM_DATA_LOCATION_CONNECTION) {
463 : 2 : context->connection_info.algorithm.other_params_support = *(const uint8_t *)data;
464 [ + + ]: 4 : } else if (parameter->location == LIBSPDM_DATA_LOCATION_LOCAL) {
465 : 2 : context->local_context.algorithm.other_params_support = *(const uint8_t *)data;
466 : : } else {
467 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
468 : : }
469 : 4 : break;
470 : 8 : case LIBSPDM_DATA_MEL_SPEC:
471 [ + + ]: 8 : if (data_size != sizeof(uint8_t)) {
472 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
473 : : }
474 [ + + ]: 6 : if (parameter->location == LIBSPDM_DATA_LOCATION_CONNECTION) {
475 : 2 : context->connection_info.algorithm.mel_spec = *(const uint8_t *)data;
476 [ + + ]: 4 : } else if (parameter->location == LIBSPDM_DATA_LOCATION_LOCAL) {
477 : 2 : context->local_context.algorithm.mel_spec = *(const uint8_t *)data;
478 : : } else {
479 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
480 : : }
481 : 4 : break;
482 : 8 : case LIBSPDM_DATA_PQC_ASYM_ALGO:
483 [ + + ]: 8 : if (data_size != sizeof(uint32_t)) {
484 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
485 : : }
486 : 6 : data32 = libspdm_read_uint32((const uint8_t *)data);
487 [ + + ]: 6 : if (parameter->location == LIBSPDM_DATA_LOCATION_CONNECTION) {
488 : 2 : context->connection_info.algorithm.pqc_asym_algo = data32;
489 [ + + ]: 4 : } else if (parameter->location == LIBSPDM_DATA_LOCATION_LOCAL) {
490 : 2 : context->local_context.algorithm.pqc_asym_algo = data32;
491 : : } else {
492 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
493 : : }
494 : 4 : break;
495 : 8 : case LIBSPDM_DATA_REQ_PQC_ASYM_ALG:
496 [ + + ]: 8 : if (data_size != sizeof(uint32_t)) {
497 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
498 : : }
499 : 6 : data32 = libspdm_read_uint32((const uint8_t *)data);
500 [ + + ]: 6 : if (parameter->location == LIBSPDM_DATA_LOCATION_CONNECTION) {
501 : 2 : context->connection_info.algorithm.req_pqc_asym_alg = data32;
502 [ + + ]: 4 : } else if (parameter->location == LIBSPDM_DATA_LOCATION_LOCAL) {
503 : 2 : context->local_context.algorithm.req_pqc_asym_alg = data32;
504 : : } else {
505 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
506 : : }
507 : 4 : break;
508 : 8 : case LIBSPDM_DATA_KEM_ALG:
509 [ + + ]: 8 : if (data_size != sizeof(uint32_t)) {
510 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
511 : : }
512 : 6 : data32 = libspdm_read_uint32((const uint8_t *)data);
513 [ + + ]: 6 : if (parameter->location == LIBSPDM_DATA_LOCATION_CONNECTION) {
514 : 2 : context->connection_info.algorithm.kem_alg = data32;
515 [ + + ]: 4 : } else if (parameter->location == LIBSPDM_DATA_LOCATION_LOCAL) {
516 : 2 : context->local_context.algorithm.kem_alg = data32;
517 : : } else {
518 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
519 : : }
520 : 4 : break;
521 : 6 : case LIBSPDM_DATA_ALGO_PRIORITY_PQC_FIRST:
522 [ + + ]: 6 : if (data_size != sizeof(bool)) {
523 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
524 : : }
525 [ + + ]: 4 : if (parameter->location == LIBSPDM_DATA_LOCATION_LOCAL) {
526 : 2 : context->local_context.algorithm.pqc_first = *(const bool *)data;
527 : : } else {
528 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
529 : : }
530 : 2 : break;
531 : 6 : case LIBSPDM_DATA_CONNECTION_STATE:
532 [ + + ]: 6 : if (data_size != sizeof(libspdm_connection_state_t)) {
533 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
534 : : }
535 [ + + ]: 4 : if (parameter->location != LIBSPDM_DATA_LOCATION_CONNECTION) {
536 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
537 : : }
538 : 2 : context->connection_info.connection_state = libspdm_read_uint32((const uint8_t *)data);
539 : 2 : break;
540 : 6 : case LIBSPDM_DATA_RESPONSE_STATE:
541 [ + + ]: 6 : if (data_size != sizeof(libspdm_response_state_t)) {
542 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
543 : : }
544 [ + + ]: 4 : if (libspdm_read_uint32((const uint8_t *)data) >= LIBSPDM_RESPONSE_STATE_MAX) {
545 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
546 : : }
547 : 2 : context->response_state = libspdm_read_uint32((const uint8_t *)data);
548 : 2 : break;
549 : : #if LIBSPDM_CERT_PARSE_SUPPORT
550 : 6 : case LIBSPDM_DATA_PEER_PUBLIC_ROOT_CERT:
551 [ + + ]: 6 : if (parameter->location != LIBSPDM_DATA_LOCATION_LOCAL) {
552 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
553 : : }
554 : 4 : root_cert_index = 0;
555 [ + + ]: 22 : while (context->local_context.peer_root_cert_provision[root_cert_index] != NULL) {
556 : 20 : root_cert_index++;
557 [ + + ]: 20 : if (root_cert_index >= LIBSPDM_MAX_ROOT_CERT_SUPPORT) {
558 : 2 : return LIBSPDM_STATUS_BUFFER_FULL;
559 : : }
560 : : }
561 : 2 : context->local_context.peer_root_cert_provision_size[root_cert_index] = data_size;
562 : 2 : context->local_context.peer_root_cert_provision[root_cert_index] = data;
563 : 2 : break;
564 : : #endif /* LIBSPDM_CERT_PARSE_SUPPORT */
565 : 32 : case LIBSPDM_DATA_LOCAL_PUBLIC_CERT_CHAIN:
566 [ + + ]: 32 : if (parameter->location != LIBSPDM_DATA_LOCATION_LOCAL) {
567 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
568 : : }
569 : 30 : slot_id = parameter->additional_data[0];
570 [ + + ]: 30 : if (slot_id >= SPDM_MAX_SLOT_COUNT) {
571 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
572 : : }
573 : 28 : context->local_context.local_cert_chain_provision_size[slot_id] = data_size;
574 : 28 : context->local_context.local_cert_chain_provision[slot_id] = data;
575 : 28 : break;
576 : 6 : case LIBSPDM_DATA_LOCAL_SUPPORTED_SLOT_MASK:
577 [ + + ]: 6 : if (parameter->location != LIBSPDM_DATA_LOCATION_LOCAL) {
578 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
579 : : }
580 [ + + ]: 4 : if (data_size != sizeof(uint8_t)) {
581 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
582 : : }
583 : 2 : context->local_context.local_supported_slot_mask = *(const uint8_t *)data;
584 : 2 : break;
585 : 8 : case LIBSPDM_DATA_LOCAL_KEY_PAIR_ID:
586 [ + + ]: 8 : if (parameter->location != LIBSPDM_DATA_LOCATION_LOCAL) {
587 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
588 : : }
589 : 6 : slot_id = parameter->additional_data[0];
590 [ + + ]: 6 : if (slot_id >= SPDM_MAX_SLOT_COUNT) {
591 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
592 : : }
593 [ + + ]: 4 : if (data_size != sizeof(spdm_key_pair_id_t)) {
594 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
595 : : }
596 : 2 : context->local_context.local_key_pair_id[slot_id] = *(const spdm_key_pair_id_t *)data;
597 : 2 : break;
598 : 8 : case LIBSPDM_DATA_LOCAL_CERT_INFO:
599 [ + + ]: 8 : if (parameter->location != LIBSPDM_DATA_LOCATION_LOCAL) {
600 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
601 : : }
602 : 6 : slot_id = parameter->additional_data[0];
603 [ + + ]: 6 : if (slot_id >= SPDM_MAX_SLOT_COUNT) {
604 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
605 : : }
606 [ + + ]: 4 : if (data_size != sizeof(spdm_certificate_info_t)) {
607 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
608 : : }
609 : 2 : context->local_context.local_cert_info[slot_id] = *(const spdm_certificate_info_t *)data;
610 : 2 : break;
611 : 8 : case LIBSPDM_DATA_LOCAL_KEY_USAGE_BIT_MASK:
612 [ + + ]: 8 : if (parameter->location != LIBSPDM_DATA_LOCATION_LOCAL) {
613 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
614 : : }
615 : 6 : slot_id = parameter->additional_data[0];
616 [ + + ]: 6 : if (slot_id >= SPDM_MAX_SLOT_COUNT) {
617 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
618 : : }
619 [ + + ]: 4 : if (data_size != sizeof(spdm_key_usage_bit_mask_t)) {
620 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
621 : : }
622 : 4 : context->local_context.local_key_usage_bit_mask[slot_id] =
623 : 2 : libspdm_read_uint16((const uint8_t *)data);
624 : 2 : break;
625 : 18 : case LIBSPDM_DATA_PEER_USED_CERT_CHAIN_BUFFER:
626 [ + + ]: 18 : if (parameter->location != LIBSPDM_DATA_LOCATION_CONNECTION) {
627 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
628 : : }
629 : 16 : slot_id = parameter->additional_data[0];
630 [ + + ]: 16 : if (slot_id >= SPDM_MAX_SLOT_COUNT) {
631 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
632 : : }
633 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
634 : : if (data_size > LIBSPDM_MAX_CERT_CHAIN_SIZE) {
635 : : return LIBSPDM_STATUS_BUFFER_TOO_SMALL;
636 : : }
637 : : context->connection_info.peer_used_cert_chain[slot_id].buffer_size = data_size;
638 : : libspdm_copy_mem(context->connection_info.peer_used_cert_chain[slot_id].buffer,
639 : : sizeof(context->connection_info.peer_used_cert_chain[slot_id].buffer),
640 : : data, data_size);
641 : : #else
642 : : #if LIBSPDM_CERT_PARSE_SUPPORT
643 : 14 : status = libspdm_hash_all(
644 : : context->connection_info.algorithm.base_hash_algo,
645 : : data, data_size,
646 : 14 : context->connection_info.peer_used_cert_chain[slot_id].buffer_hash);
647 [ - + ]: 14 : if (!status) {
648 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
649 : : }
650 : :
651 : 28 : context->connection_info.peer_used_cert_chain[slot_id].buffer_hash_size =
652 : 14 : libspdm_get_hash_size(context->connection_info.algorithm.base_hash_algo);
653 : :
654 : 14 : libspdm_free_peer_leaf_cert_public_key(context, slot_id);
655 [ + + ]: 14 : if (context->local_context.is_requester) {
656 : 12 : peer_base_asym_algo = context->connection_info.algorithm.base_asym_algo;
657 : 12 : peer_pqc_asym_algo = context->connection_info.algorithm.pqc_asym_algo;
658 : : } else {
659 : 2 : peer_base_asym_algo = context->connection_info.algorithm.req_base_asym_alg;
660 : 2 : peer_pqc_asym_algo = context->connection_info.algorithm.req_pqc_asym_alg;
661 : : }
662 : :
663 [ + + ]: 14 : if (peer_pqc_asym_algo != 0) {
664 : 2 : status = libspdm_get_pqc_leaf_cert_public_key_from_cert_chain(
665 : : context->connection_info.algorithm.base_hash_algo,
666 : : peer_pqc_asym_algo,
667 : : (uint8_t *)(size_t)data, data_size,
668 : 2 : &context->connection_info.peer_used_cert_chain[slot_id].leaf_cert_public_key);
669 : : } else {
670 : 12 : status = libspdm_get_leaf_cert_public_key_from_cert_chain(
671 : : context->connection_info.algorithm.base_hash_algo,
672 : : peer_base_asym_algo,
673 : : (uint8_t *)(size_t)data, data_size,
674 : 12 : &context->connection_info.peer_used_cert_chain[slot_id].leaf_cert_public_key);
675 : : }
676 [ + + ]: 14 : if (!status) {
677 : 4 : return LIBSPDM_STATUS_INVALID_CERT;
678 : : }
679 : : #else
680 : : LIBSPDM_ASSERT (false);
681 : : #endif /* LIBSPDM_CERT_PARSE_SUPPORT */
682 : : #endif /* LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT */
683 : 10 : break;
684 : 4 : case LIBSPDM_DATA_PEER_PUBLIC_KEY:
685 [ + + ]: 4 : if (parameter->location != LIBSPDM_DATA_LOCATION_LOCAL) {
686 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
687 : : }
688 : 2 : context->local_context.peer_public_key_provision_size = data_size;
689 : 2 : context->local_context.peer_public_key_provision = data;
690 : 2 : break;
691 : 4 : case LIBSPDM_DATA_LOCAL_PUBLIC_KEY:
692 [ + + ]: 4 : if (parameter->location != LIBSPDM_DATA_LOCATION_LOCAL) {
693 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
694 : : }
695 : 2 : context->local_context.local_public_key_provision_size = data_size;
696 : 2 : context->local_context.local_public_key_provision = data;
697 : 2 : break;
698 : 6 : case LIBSPDM_DATA_HEARTBEAT_PERIOD:
699 [ + + ]: 6 : if (data_size != sizeof(uint8_t)) {
700 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
701 : : }
702 [ + + ]: 4 : if (parameter->location != LIBSPDM_DATA_LOCATION_LOCAL) {
703 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
704 : : }
705 : 2 : context->local_context.heartbeat_period = *(const uint8_t *)data;
706 : 2 : break;
707 : 14 : case LIBSPDM_DATA_APP_CONTEXT_DATA:
708 [ + + + + ]: 14 : if (data_size != sizeof(void *) || *(void *const *)data == NULL) {
709 : 8 : return LIBSPDM_STATUS_INVALID_PARAMETER;
710 : : }
711 : 6 : context->app_context_data_ptr = *(void *const *)data;
712 : 6 : break;
713 : 4 : case LIBSPDM_DATA_HANDLE_ERROR_RETURN_POLICY:
714 [ + + ]: 4 : if (data_size != sizeof(uint8_t)) {
715 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
716 : : }
717 : 2 : context->handle_error_return_policy = *(const uint8_t *)data;
718 : 2 : break;
719 : 4 : case LIBSPDM_DATA_VCA_CACHE:
720 [ + + ]: 4 : if (data_size > sizeof(context->transcript.message_a.buffer)) {
721 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
722 : : }
723 : 2 : context->transcript.message_a.buffer_size = data_size;
724 : 2 : libspdm_copy_mem(context->transcript.message_a.buffer,
725 : : sizeof(context->transcript.message_a.buffer),
726 : : data, data_size);
727 : 2 : break;
728 : 580 : case LIBSPDM_DATA_IS_REQUESTER:
729 [ + + ]: 580 : if (data_size != sizeof(bool)) {
730 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
731 : : }
732 [ + + ]: 578 : if (parameter->location != LIBSPDM_DATA_LOCATION_LOCAL) {
733 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
734 : : }
735 : 576 : context->local_context.is_requester = *(const bool *)data;
736 : 576 : break;
737 : 4 : case LIBSPDM_DATA_REQUEST_RETRY_TIMES:
738 [ + + ]: 4 : if (data_size != sizeof(uint8_t)) {
739 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
740 : : }
741 : 2 : context->retry_times = *(const uint8_t *)data;
742 : 2 : break;
743 : 4 : case LIBSPDM_DATA_REQUEST_RETRY_DELAY_TIME:
744 [ + + ]: 4 : if (data_size != sizeof(uint64_t)) {
745 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
746 : : }
747 : 2 : context->retry_delay_time = *(const uint64_t *)data;
748 : 2 : break;
749 : 16 : case LIBSPDM_DATA_MAX_DHE_SESSION_COUNT:
750 [ + + ]: 16 : if (data_size != sizeof(uint32_t)) {
751 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
752 : : }
753 [ + + ]: 14 : if (*(const uint32_t *)data > LIBSPDM_MAX_SESSION_COUNT - context->max_psk_session_count) {
754 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
755 : : }
756 : 12 : context->max_dhe_session_count = *(const uint32_t *)data;
757 : 12 : break;
758 : 16 : case LIBSPDM_DATA_MAX_PSK_SESSION_COUNT:
759 [ + + ]: 16 : if (data_size != sizeof(uint32_t)) {
760 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
761 : : }
762 [ + + ]: 14 : if (*(const uint32_t *)data > LIBSPDM_MAX_SESSION_COUNT - context->max_dhe_session_count) {
763 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
764 : : }
765 : 12 : context->max_psk_session_count = *(const uint32_t *)data;
766 : 12 : break;
767 : 10 : case LIBSPDM_DATA_MAX_SPDM_SESSION_SEQUENCE_NUMBER:
768 [ + + ]: 10 : if (data_size != sizeof(uint64_t)) {
769 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
770 : : }
771 : : /* This is a context-level integrator cap only. A per-session value is not settable: a
772 : : * session's effective cap is derived once at establishment from min(this cap, negotiated
773 : : * DSP0277 1.3 AEAD limit) and must not be overridden afterwards. The per-session value is
774 : : * read-only via get_data(LIBSPDM_DATA_LOCATION_SESSION). */
775 [ + + ]: 8 : if (parameter->location == LIBSPDM_DATA_LOCATION_SESSION) {
776 : 4 : return LIBSPDM_STATUS_INVALID_PARAMETER;
777 : : }
778 : 4 : context->max_spdm_session_sequence_number = *(const uint64_t *)data;
779 [ + + ]: 4 : if (context->max_spdm_session_sequence_number == 0) {
780 : 2 : context->max_spdm_session_sequence_number = LIBSPDM_MAX_SPDM_SESSION_SEQUENCE_NUMBER;
781 : : }
782 : 4 : break;
783 : 8 : case LIBSPDM_DATA_SPDM_VERSION_10_11_VERIFY_SIGNATURE_ENDIAN:
784 [ + + ]: 8 : if (data_size != sizeof(uint8_t)) {
785 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
786 : : }
787 [ + - ]: 6 : if (*(const uint8_t*)data != LIBSPDM_SPDM_10_11_VERIFY_SIGNATURE_ENDIAN_BIG_ONLY &&
788 [ + + ]: 6 : *(const uint8_t*)data != LIBSPDM_SPDM_10_11_VERIFY_SIGNATURE_ENDIAN_LITTLE_ONLY &&
789 [ + + ]: 4 : *(const uint8_t*)data != LIBSPDM_SPDM_10_11_VERIFY_SIGNATURE_ENDIAN_BIG_OR_LITTLE) {
790 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
791 : : }
792 : 4 : context->spdm_10_11_verify_signature_endian = *(const uint8_t*)data;
793 : 4 : context->spdm_10_11_verify_signature_endian_setting = *(const uint8_t*)data;
794 : 4 : break;
795 : 4 : case LIBSPDM_DATA_SEQUENCE_NUMBER_ENDIAN:
796 [ + + ]: 4 : if (data_size != sizeof(uint8_t)) {
797 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
798 : : }
799 : 2 : context->sequence_number_endian = *(const uint8_t *)data;
800 : 2 : break;
801 : 6 : case LIBSPDM_DATA_MULTI_KEY_CONN_REQ:
802 [ + + ]: 6 : if (parameter->location != LIBSPDM_DATA_LOCATION_CONNECTION) {
803 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
804 : : }
805 [ + + ]: 4 : if (data_size != sizeof(bool)) {
806 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
807 : : }
808 : 2 : context->connection_info.multi_key_conn_req = *(const bool *)data;
809 : 2 : break;
810 : 6 : case LIBSPDM_DATA_MULTI_KEY_CONN_RSP:
811 [ + + ]: 6 : if (parameter->location != LIBSPDM_DATA_LOCATION_CONNECTION) {
812 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
813 : : }
814 [ + + ]: 4 : if (data_size != sizeof(bool)) {
815 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
816 : : }
817 : 2 : context->connection_info.multi_key_conn_rsp = *(const bool *)data;
818 : 2 : break;
819 : : #if (LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP) && (LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP)
820 : 16 : case LIBSPDM_DATA_SESSION_ENCAP_REQ_SLOT_ID:
821 [ + + ]: 16 : if (data_size != sizeof(uint8_t)) {
822 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
823 : : }
824 [ + + ]: 14 : if (*(const uint8_t *)data >= SPDM_MAX_SLOT_COUNT) {
825 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
826 : : }
827 : : /* The slot is only meaningful while session-based mutual authentication is in progress. */
828 [ + + ]: 12 : if (session_info->encap_context.flow_type != LIBSPDM_ENCAP_FLOW_SESS_MUT_AUTH) {
829 : 4 : return LIBSPDM_STATUS_INVALID_STATE_LOCAL;
830 : : }
831 : : /* The encapsulated context conveys the slot to the Requester in the final
832 : : * ENCAPSULATED_RESPONSE_ACK, and the session's peer slot is what FINISH verification
833 : : * reads. The two must agree, as the Requester signs FINISH with the designated slot. */
834 : 8 : session_info->encap_context.mut_auth_req_slot_id = *(const uint8_t *)data;
835 : 8 : session_info->peer_used_cert_chain_slot_id = *(const uint8_t *)data;
836 : 8 : break;
837 : : #endif /* (LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP) && (LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP) */
838 : 32 : default:
839 : 32 : return LIBSPDM_STATUS_UNSUPPORTED_CAP;
840 : : break;
841 : : }
842 : :
843 : 774 : return LIBSPDM_STATUS_SUCCESS;
844 : : }
845 : :
846 : 260 : libspdm_return_t libspdm_get_data(void *spdm_context, libspdm_data_type_t data_type,
847 : : const libspdm_data_parameter_t *parameter,
848 : : void *data, size_t *data_size)
849 : : {
850 : : libspdm_context_t *context;
851 : 260 : libspdm_secured_message_context_t *secured_context = NULL;
852 : : size_t target_data_size;
853 : : void *target_data;
854 : : uint32_t session_id;
855 : : libspdm_session_info_t *session_info;
856 : : uint8_t slot_id;
857 : :
858 [ + + + + : 260 : if (spdm_context == NULL || data == NULL || data_size == NULL ||
+ + + + ]
859 : : data_type >= LIBSPDM_DATA_MAX) {
860 : 8 : return LIBSPDM_STATUS_INVALID_PARAMETER;
861 : : }
862 : :
863 : 252 : context = spdm_context;
864 : :
865 [ + + ]: 252 : if (data_type == LIBSPDM_DATA_SESSION_END_SESSION_ATTRIBUTES) {
866 : : /* end_session_attributes is present in both a session context as well as an
867 : : * spdm context. */
868 : 6 : session_id = libspdm_read_uint32(parameter->additional_data);
869 : 6 : session_info = libspdm_get_session_info_via_session_id(context, session_id);
870 [ + + ]: 246 : } else if (need_session_info_for_data(data_type)) {
871 [ + + ]: 18 : if (parameter->location != LIBSPDM_DATA_LOCATION_SESSION) {
872 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
873 : : }
874 : 16 : session_id = libspdm_read_uint32(parameter->additional_data);
875 : 16 : session_info = libspdm_get_session_info_via_session_id(context, session_id);
876 [ + + ]: 16 : if (session_info == NULL) {
877 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
878 : : }
879 : 14 : secured_context = session_info->secured_message_context;
880 : : } else {
881 : 228 : session_info = NULL;
882 : : }
883 : :
884 [ + + + + : 248 : switch (data_type) {
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + ]
885 : 4 : case LIBSPDM_DATA_SPDM_VERSION:
886 [ + + ]: 4 : if (parameter->location != LIBSPDM_DATA_LOCATION_CONNECTION) {
887 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
888 : : }
889 : 2 : target_data_size = sizeof(spdm_version_number_t);
890 : 2 : target_data = &(context->connection_info.version);
891 : 2 : break;
892 : 2 : case LIBSPDM_DATA_SESSION_SECURED_MESSAGE_VERSION:
893 : 2 : target_data_size = sizeof(spdm_version_number_t);
894 : 2 : target_data = &(secured_context->secured_message_version);
895 : 2 : break;
896 : 6 : case LIBSPDM_DATA_CAPABILITY_FLAGS:
897 : 6 : target_data_size = sizeof(uint32_t);
898 [ + + ]: 6 : if (parameter->location == LIBSPDM_DATA_LOCATION_CONNECTION) {
899 : 2 : target_data = &context->connection_info.capability.flags;
900 [ + + ]: 4 : } else if (parameter->location == LIBSPDM_DATA_LOCATION_LOCAL) {
901 : 2 : target_data = &context->local_context.capability.flags;
902 : : } else {
903 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
904 : : }
905 : 4 : break;
906 : 6 : case LIBSPDM_DATA_CAPABILITY_EXT_FLAGS:
907 : 6 : target_data_size = sizeof(uint16_t);
908 [ + + ]: 6 : if (parameter->location == LIBSPDM_DATA_LOCATION_CONNECTION) {
909 : 2 : target_data = &context->connection_info.capability.ext_flags;
910 [ + + ]: 4 : } else if (parameter->location == LIBSPDM_DATA_LOCATION_LOCAL) {
911 : 2 : target_data = &context->local_context.capability.ext_flags;
912 : : } else {
913 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
914 : : }
915 : 4 : break;
916 : 6 : case LIBSPDM_DATA_CAPABILITY_CT_EXPONENT:
917 : 6 : target_data_size = sizeof(uint8_t);
918 [ + + ]: 6 : if (parameter->location == LIBSPDM_DATA_LOCATION_CONNECTION) {
919 : 2 : target_data = &context->connection_info.capability.ct_exponent;
920 [ + + ]: 4 : } else if (parameter->location == LIBSPDM_DATA_LOCATION_LOCAL) {
921 : 2 : target_data = &context->local_context.capability.ct_exponent;
922 : : } else {
923 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
924 : : }
925 : 4 : break;
926 : 6 : case LIBSPDM_DATA_CAPABILITY_DATA_TRANSFER_SIZE:
927 : 6 : target_data_size = sizeof(uint32_t);
928 [ + + ]: 6 : if (parameter->location == LIBSPDM_DATA_LOCATION_CONNECTION) {
929 : 2 : target_data = &context->connection_info.capability.data_transfer_size;
930 [ + + ]: 4 : } else if (parameter->location == LIBSPDM_DATA_LOCATION_LOCAL) {
931 : 2 : target_data = &context->local_context.capability.data_transfer_size;
932 : : } else {
933 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
934 : : }
935 : 4 : break;
936 : 6 : case LIBSPDM_DATA_CAPABILITY_MAX_SPDM_MSG_SIZE:
937 : 6 : target_data_size = sizeof(uint32_t);
938 [ + + ]: 6 : if (parameter->location == LIBSPDM_DATA_LOCATION_CONNECTION) {
939 : 2 : target_data = &context->connection_info.capability.max_spdm_msg_size;
940 [ + + ]: 4 : } else if (parameter->location == LIBSPDM_DATA_LOCATION_LOCAL) {
941 : 2 : target_data = &context->local_context.capability.max_spdm_msg_size;
942 : : } else {
943 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
944 : : }
945 : 4 : break;
946 : 6 : case LIBSPDM_DATA_CAPABILITY_SENDER_DATA_TRANSFER_SIZE:
947 : 6 : target_data_size = sizeof(uint32_t);
948 [ + + ]: 6 : if (parameter->location == LIBSPDM_DATA_LOCATION_CONNECTION) {
949 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
950 [ + + ]: 4 : } else if (parameter->location == LIBSPDM_DATA_LOCATION_LOCAL) {
951 : 2 : target_data = &context->local_context.capability.sender_data_transfer_size;
952 : : } else {
953 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
954 : : }
955 : 2 : break;
956 : 4 : case LIBSPDM_DATA_MEASUREMENT_SPEC:
957 [ + + ]: 4 : if (parameter->location != LIBSPDM_DATA_LOCATION_CONNECTION) {
958 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
959 : : }
960 : 2 : target_data_size = sizeof(uint8_t);
961 : 2 : target_data = &context->connection_info.algorithm.measurement_spec;
962 : 2 : break;
963 : 4 : case LIBSPDM_DATA_MEASUREMENT_HASH_ALGO:
964 [ + + ]: 4 : if (parameter->location != LIBSPDM_DATA_LOCATION_CONNECTION) {
965 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
966 : : }
967 : 2 : target_data_size = sizeof(uint32_t);
968 : 2 : target_data = &context->connection_info.algorithm.measurement_hash_algo;
969 : 2 : break;
970 : 4 : case LIBSPDM_DATA_BASE_ASYM_ALGO:
971 [ + + ]: 4 : if (parameter->location != LIBSPDM_DATA_LOCATION_CONNECTION) {
972 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
973 : : }
974 : 2 : target_data_size = sizeof(uint32_t);
975 : 2 : target_data = &context->connection_info.algorithm.base_asym_algo;
976 : 2 : break;
977 : 4 : case LIBSPDM_DATA_BASE_HASH_ALGO:
978 [ + + ]: 4 : if (parameter->location != LIBSPDM_DATA_LOCATION_CONNECTION) {
979 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
980 : : }
981 : 2 : target_data_size = sizeof(uint32_t);
982 : 2 : target_data = &context->connection_info.algorithm.base_hash_algo;
983 : 2 : break;
984 : 4 : case LIBSPDM_DATA_DHE_NAME_GROUP:
985 [ + + ]: 4 : if (parameter->location != LIBSPDM_DATA_LOCATION_CONNECTION) {
986 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
987 : : }
988 : 2 : target_data_size = sizeof(uint16_t);
989 : 2 : target_data = &context->connection_info.algorithm.dhe_named_group;
990 : 2 : break;
991 : 4 : case LIBSPDM_DATA_AEAD_CIPHER_SUITE:
992 [ + + ]: 4 : if (parameter->location != LIBSPDM_DATA_LOCATION_CONNECTION) {
993 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
994 : : }
995 : 2 : target_data_size = sizeof(uint16_t);
996 : 2 : target_data = &context->connection_info.algorithm.aead_cipher_suite;
997 : 2 : break;
998 : 4 : case LIBSPDM_DATA_REQ_BASE_ASYM_ALG:
999 [ + + ]: 4 : if (parameter->location != LIBSPDM_DATA_LOCATION_CONNECTION) {
1000 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
1001 : : }
1002 : 2 : target_data_size = sizeof(uint16_t);
1003 : 2 : target_data = &context->connection_info.algorithm.req_base_asym_alg;
1004 : 2 : break;
1005 : 4 : case LIBSPDM_DATA_KEY_SCHEDULE:
1006 [ + + ]: 4 : if (parameter->location != LIBSPDM_DATA_LOCATION_CONNECTION) {
1007 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
1008 : : }
1009 : 2 : target_data_size = sizeof(uint16_t);
1010 : 2 : target_data = &context->connection_info.algorithm.key_schedule;
1011 : 2 : break;
1012 : 4 : case LIBSPDM_DATA_OTHER_PARAMS_SUPPORT:
1013 [ + + ]: 4 : if (parameter->location != LIBSPDM_DATA_LOCATION_CONNECTION) {
1014 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
1015 : : }
1016 : 2 : target_data_size = sizeof(uint8_t);
1017 : 2 : target_data = &context->connection_info.algorithm.other_params_support;
1018 : 2 : break;
1019 : 4 : case LIBSPDM_DATA_MEL_SPEC:
1020 [ + + ]: 4 : if (parameter->location != LIBSPDM_DATA_LOCATION_CONNECTION) {
1021 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
1022 : : }
1023 : 2 : target_data_size = sizeof(uint8_t);
1024 : 2 : target_data = &context->connection_info.algorithm.mel_spec;
1025 : 2 : break;
1026 : 4 : case LIBSPDM_DATA_PQC_ASYM_ALGO:
1027 [ + + ]: 4 : if (parameter->location != LIBSPDM_DATA_LOCATION_CONNECTION) {
1028 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
1029 : : }
1030 : 2 : target_data_size = sizeof(uint32_t);
1031 : 2 : target_data = &context->connection_info.algorithm.pqc_asym_algo;
1032 : 2 : break;
1033 : 4 : case LIBSPDM_DATA_REQ_PQC_ASYM_ALG:
1034 [ + + ]: 4 : if (parameter->location != LIBSPDM_DATA_LOCATION_CONNECTION) {
1035 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
1036 : : }
1037 : 2 : target_data_size = sizeof(uint32_t);
1038 : 2 : target_data = &context->connection_info.algorithm.req_pqc_asym_alg;
1039 : 2 : break;
1040 : 4 : case LIBSPDM_DATA_KEM_ALG:
1041 [ + + ]: 4 : if (parameter->location != LIBSPDM_DATA_LOCATION_CONNECTION) {
1042 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
1043 : : }
1044 : 2 : target_data_size = sizeof(uint32_t);
1045 : 2 : target_data = &context->connection_info.algorithm.kem_alg;
1046 : 2 : break;
1047 : 4 : case LIBSPDM_DATA_CONNECTION_STATE:
1048 [ + + ]: 4 : if (parameter->location != LIBSPDM_DATA_LOCATION_CONNECTION) {
1049 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
1050 : : }
1051 : 2 : target_data_size = sizeof(libspdm_connection_state_t);
1052 : 2 : target_data = &context->connection_info.connection_state;
1053 : 2 : break;
1054 : 2 : case LIBSPDM_DATA_RESPONSE_STATE:
1055 : 2 : target_data_size = sizeof(libspdm_response_state_t);
1056 : 2 : target_data = &context->response_state;
1057 : 2 : break;
1058 : 16 : case LIBSPDM_DATA_PEER_PROVISIONED_SLOT_MASK:
1059 [ + + ]: 16 : if (parameter->location != LIBSPDM_DATA_LOCATION_CONNECTION) {
1060 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
1061 : : }
1062 : 14 : target_data_size = sizeof(uint8_t);
1063 : 14 : target_data = &context->connection_info.peer_provisioned_slot_mask;
1064 : 14 : break;
1065 : 4 : case LIBSPDM_DATA_PEER_SUPPORTED_SLOT_MASK:
1066 [ + + ]: 4 : if (parameter->location != LIBSPDM_DATA_LOCATION_CONNECTION) {
1067 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
1068 : : }
1069 : 2 : target_data_size = sizeof(uint8_t);
1070 : 2 : target_data = &context->connection_info.peer_supported_slot_mask;
1071 : 2 : break;
1072 : 6 : case LIBSPDM_DATA_PEER_KEY_PAIR_ID:
1073 [ + + ]: 6 : if (parameter->location != LIBSPDM_DATA_LOCATION_CONNECTION) {
1074 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
1075 : : }
1076 : 4 : slot_id = parameter->additional_data[0];
1077 [ + + ]: 4 : if (slot_id >= SPDM_MAX_SLOT_COUNT) {
1078 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
1079 : : }
1080 : 2 : target_data_size = sizeof(spdm_key_pair_id_t);
1081 : 2 : target_data = &context->connection_info.peer_key_pair_id[slot_id];
1082 : 2 : break;
1083 : 6 : case LIBSPDM_DATA_PEER_CERT_INFO:
1084 [ + + ]: 6 : if (parameter->location != LIBSPDM_DATA_LOCATION_CONNECTION) {
1085 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
1086 : : }
1087 : 4 : slot_id = parameter->additional_data[0];
1088 [ + + ]: 4 : if (slot_id >= SPDM_MAX_SLOT_COUNT) {
1089 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
1090 : : }
1091 : 2 : target_data_size = sizeof(spdm_certificate_info_t);
1092 : 2 : target_data = &context->connection_info.peer_cert_info[slot_id];
1093 : 2 : break;
1094 : 6 : case LIBSPDM_DATA_PEER_KEY_USAGE_BIT_MASK:
1095 [ + + ]: 6 : if (parameter->location != LIBSPDM_DATA_LOCATION_CONNECTION) {
1096 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
1097 : : }
1098 : 4 : slot_id = parameter->additional_data[0];
1099 [ + + ]: 4 : if (slot_id >= SPDM_MAX_SLOT_COUNT) {
1100 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
1101 : : }
1102 : 2 : target_data_size = sizeof(spdm_key_usage_bit_mask_t);
1103 : 2 : target_data = &context->connection_info.peer_key_usage_bit_mask[slot_id];
1104 : 2 : break;
1105 : 2 : case LIBSPDM_DATA_SESSION_USE_PSK:
1106 : 2 : target_data_size = sizeof(bool);
1107 : 2 : target_data = &session_info->use_psk;
1108 : 2 : break;
1109 : 2 : case LIBSPDM_DATA_SESSION_MUT_AUTH_REQUESTED:
1110 : 2 : target_data_size = sizeof(uint8_t);
1111 : 2 : target_data = &session_info->mut_auth_requested;
1112 : 2 : break;
1113 : 6 : case LIBSPDM_DATA_SESSION_END_SESSION_ATTRIBUTES:
1114 [ + + ]: 6 : if (parameter->location != LIBSPDM_DATA_LOCATION_CONNECTION) {
1115 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
1116 : : }
1117 : 4 : target_data_size = sizeof(uint8_t);
1118 [ + + ]: 4 : if (session_info == NULL) {
1119 : 2 : target_data = &context->connection_info.end_session_attributes;
1120 : : } else {
1121 : 2 : target_data = &session_info->end_session_attributes;
1122 : : }
1123 : 4 : break;
1124 : 2 : case LIBSPDM_DATA_SESSION_POLICY:
1125 : 2 : target_data_size = sizeof(uint8_t);
1126 : 2 : target_data = &session_info->session_policy;
1127 : 2 : break;
1128 : 14 : case LIBSPDM_DATA_APP_CONTEXT_DATA:
1129 : 14 : target_data_size = sizeof(void *);
1130 : 14 : target_data = &context->app_context_data_ptr;
1131 : 14 : break;
1132 : 2 : case LIBSPDM_DATA_HANDLE_ERROR_RETURN_POLICY:
1133 : 2 : target_data_size = sizeof(uint8_t);
1134 : 2 : target_data = &context->handle_error_return_policy;
1135 : 2 : break;
1136 : 2 : case LIBSPDM_DATA_MAX_DHE_SESSION_COUNT:
1137 : 2 : target_data_size = sizeof(uint32_t);
1138 : 2 : target_data = &context->max_dhe_session_count;
1139 : 2 : break;
1140 : 2 : case LIBSPDM_DATA_MAX_PSK_SESSION_COUNT:
1141 : 2 : target_data_size = sizeof(uint32_t);
1142 : 2 : target_data = &context->max_psk_session_count;
1143 : 2 : break;
1144 : 2 : case LIBSPDM_DATA_SESSION_SEQUENCE_NUMBER_REQ_DIR:
1145 : 2 : target_data_size = sizeof(uint64_t);
1146 : 2 : target_data = &secured_context->application_secret.request_data_sequence_number;
1147 : 2 : break;
1148 : 2 : case LIBSPDM_DATA_SESSION_SEQUENCE_NUMBER_RSP_DIR:
1149 : 2 : target_data_size = sizeof(uint64_t);
1150 : 2 : target_data = &secured_context->application_secret.response_data_sequence_number;
1151 : 2 : break;
1152 : 24 : case LIBSPDM_DATA_MAX_SPDM_SESSION_SEQUENCE_NUMBER:
1153 : 24 : target_data_size = sizeof(uint64_t);
1154 : : /* When a session is addressed (LIBSPDM_DATA_LOCATION_SESSION), return that session's
1155 : : * effective maximum sequence number, i.e. the value negotiated via the DSP0277 1.3 AEAD
1156 : : * limit (min of the integrator-configured cap and the negotiated AEAD limit). Otherwise
1157 : : * return the context-level integrator-configured value. */
1158 [ + + ]: 24 : if (parameter->location == LIBSPDM_DATA_LOCATION_SESSION) {
1159 : 22 : session_id = libspdm_read_uint32(parameter->additional_data);
1160 : 22 : session_info = libspdm_get_session_info_via_session_id(context, session_id);
1161 [ + + ]: 22 : if (session_info == NULL) {
1162 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
1163 : : }
1164 : 20 : secured_context = session_info->secured_message_context;
1165 : 20 : target_data = &secured_context->max_spdm_session_sequence_number;
1166 : : } else {
1167 : 2 : target_data = &context->max_spdm_session_sequence_number;
1168 : : }
1169 : 22 : break;
1170 : 2 : case LIBSPDM_DATA_VCA_CACHE:
1171 : 2 : target_data_size = context->transcript.message_a.buffer_size;
1172 : 2 : target_data = context->transcript.message_a.buffer;
1173 : 2 : break;
1174 : 2 : case LIBSPDM_DATA_REQUEST_AND_SIZE:
1175 : 2 : target_data_size = context->last_spdm_request_size;
1176 : 2 : target_data = context->last_spdm_request;
1177 : 2 : break;
1178 : 2 : case LIBSPDM_DATA_SPDM_VERSION_10_11_VERIFY_SIGNATURE_ENDIAN:
1179 : 2 : target_data_size = sizeof(uint8_t);
1180 : 2 : target_data = &context->spdm_10_11_verify_signature_endian;
1181 : 2 : break;
1182 : 2 : case LIBSPDM_DATA_SEQUENCE_NUMBER_ENDIAN:
1183 : 2 : target_data_size = sizeof(uint8_t);
1184 : 2 : target_data = &context->sequence_number_endian;
1185 : 2 : break;
1186 : 2 : case LIBSPDM_DATA_SESSION_SEQUENCE_NUMBER_ENDIAN:
1187 : 2 : target_data_size = sizeof(uint8_t);
1188 : 2 : target_data = &secured_context->sequence_number_endian;
1189 : 2 : break;
1190 : 4 : case LIBSPDM_DATA_MULTI_KEY_CONN_REQ:
1191 [ + + ]: 4 : if (parameter->location != LIBSPDM_DATA_LOCATION_CONNECTION) {
1192 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
1193 : : }
1194 : 2 : target_data_size = sizeof(bool);
1195 : 2 : target_data = &context->connection_info.multi_key_conn_req;
1196 : 2 : break;
1197 : 4 : case LIBSPDM_DATA_MULTI_KEY_CONN_RSP:
1198 [ + + ]: 4 : if (parameter->location != LIBSPDM_DATA_LOCATION_CONNECTION) {
1199 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
1200 : : }
1201 : 2 : target_data_size = sizeof(bool);
1202 : 2 : target_data = &context->connection_info.multi_key_conn_rsp;
1203 : 2 : break;
1204 : 32 : default:
1205 : 32 : return LIBSPDM_STATUS_UNSUPPORTED_CAP;
1206 : : break;
1207 : : }
1208 : :
1209 [ + + ]: 148 : if (*data_size < target_data_size) {
1210 : 2 : *data_size = target_data_size;
1211 : 2 : return LIBSPDM_STATUS_BUFFER_TOO_SMALL;
1212 : : }
1213 : 146 : libspdm_copy_mem(data, *data_size, target_data, target_data_size);
1214 : 146 : *data_size = target_data_size;
1215 : :
1216 : 146 : return LIBSPDM_STATUS_SUCCESS;
1217 : : }
1218 : :
1219 : : #if LIBSPDM_CHECK_SPDM_CONTEXT
1220 : 18 : bool libspdm_check_context (void *spdm_context)
1221 : : {
1222 : : libspdm_context_t *context;
1223 : : size_t index;
1224 : :
1225 : 18 : context = spdm_context;
1226 : :
1227 [ + + ]: 18 : if (context->local_context.capability.data_transfer_size <
1228 : : SPDM_MIN_DATA_TRANSFER_SIZE_VERSION_12) {
1229 : 2 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR,
1230 : : "data_transfer_size must be greater than or equal "
1231 : : "to SPDM_MIN_DATA_TRANSFER_SIZE_VERSION_12 (%d).\n",
1232 : : SPDM_MIN_DATA_TRANSFER_SIZE_VERSION_12));
1233 : 2 : return false;
1234 : : }
1235 : :
1236 : 16 : if (context->local_context.capability.max_spdm_msg_size <
1237 [ + + ]: 16 : context->local_context.capability.data_transfer_size) {
1238 : 2 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR,
1239 : : "max_spdm_msg_size (%d) must be greater than or "
1240 : : "equal to data_transfer_size (%d).\n",
1241 : : context->local_context.capability.max_spdm_msg_size,
1242 : : context->local_context.capability.data_transfer_size));
1243 : 2 : return false;
1244 : : }
1245 : :
1246 [ + + ]: 14 : if (context->local_context.capability.sender_data_transfer_size <
1247 : : SPDM_MIN_DATA_TRANSFER_SIZE_VERSION_12) {
1248 : 2 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR,
1249 : : "sender_data_transfer_size must be greater than or equal "
1250 : : "to %d.\n", SPDM_MIN_DATA_TRANSFER_SIZE_VERSION_12));
1251 : 2 : return false;
1252 : : }
1253 : :
1254 : 12 : if (context->local_context.capability.max_spdm_msg_size <
1255 [ + + ]: 12 : context->local_context.capability.sender_data_transfer_size) {
1256 : 2 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR,
1257 : : "max_spdm_msg_size (%d) must be greater than or "
1258 : : "equal to sender_data_transfer_size (%d).\n",
1259 : : context->local_context.capability.max_spdm_msg_size,
1260 : : context->local_context.capability.sender_data_transfer_size));
1261 : 2 : return false;
1262 : : }
1263 : :
1264 [ + + ]: 10 : if (((context->local_context.capability.flags &
1265 : 2 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CHUNK_CAP) != 0) &&
1266 [ + - ]: 2 : (context->local_context.capability.max_spdm_msg_size != 0)) {
1267 [ + - ]: 4 : for (index = 0; index < SPDM_MAX_SLOT_COUNT; index++) {
1268 [ + + ]: 4 : if ((context->local_context.local_cert_chain_provision_size[index] != 0) &&
1269 : 2 : (context->local_context.local_cert_chain_provision_size[index] +
1270 : : sizeof(spdm_certificate_response_t) >
1271 [ + - ]: 2 : context->local_context.capability.max_spdm_msg_size)) {
1272 : 2 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR,
1273 : : "max_spdm_msg_size (%d) must be greater than or "
1274 : : "equal to local_cert_chain_provision_size[%zu] (%zu).\n",
1275 : : context->local_context.capability.max_spdm_msg_size, index,
1276 : : context->local_context.local_cert_chain_provision_size[index]));
1277 : 2 : return false;
1278 : : }
1279 : : }
1280 : : }
1281 : :
1282 : : /* SPDM 1.0 has no provisioned public keys. */
1283 [ + + ]: 8 : if (!context->local_context.is_requester &&
1284 [ + + ]: 6 : ((context->local_context.capability.flags &
1285 : : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PUB_KEY_ID_CAP) != 0)) {
1286 [ + + ]: 8 : for (index = 0; index < context->local_context.version.spdm_version_count; index++) {
1287 [ + + ]: 6 : if (libspdm_get_version_from_version_number(
1288 : 6 : context->local_context.version.spdm_version[index]) ==
1289 : : SPDM_MESSAGE_VERSION_10) {
1290 : 2 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR,
1291 : : "A Responder with PUB_KEY_ID_CAP cannot support SPDM 1.0.\n"));
1292 : 2 : return false;
1293 : : }
1294 : : }
1295 : : }
1296 : :
1297 : 6 : return true;
1298 : : }
1299 : : #endif /* LIBSPDM_CHECK_SPDM_CONTEXT */
1300 : :
1301 : 3081 : void libspdm_reset_message_a(libspdm_context_t *spdm_context)
1302 : : {
1303 : 3081 : libspdm_reset_managed_buffer(&spdm_context->transcript.message_a);
1304 : 3081 : }
1305 : :
1306 : 1326 : void libspdm_reset_message_d(libspdm_context_t *spdm_context)
1307 : : {
1308 : 1326 : libspdm_reset_managed_buffer(&spdm_context->transcript.message_d);
1309 : : /* Restarting the DIGESTS transcript reopens the "immediately follows VCA" window. */
1310 : 1326 : spdm_context->connection_info.digest_transcript_window_closed = false;
1311 : 1326 : }
1312 : :
1313 : 3876 : void libspdm_reset_message_b(libspdm_context_t *spdm_context)
1314 : : {
1315 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1316 : : libspdm_reset_managed_buffer(&spdm_context->transcript.message_b);
1317 : : #else
1318 [ + + ]: 3876 : if (spdm_context->transcript.digest_context_m1m2 != NULL) {
1319 : 428 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1320 : : spdm_context->transcript.digest_context_m1m2);
1321 : 428 : spdm_context->transcript.digest_context_m1m2 = NULL;
1322 : : }
1323 : : #endif
1324 : 3876 : }
1325 : :
1326 : 2854 : void libspdm_reset_message_c(libspdm_context_t *spdm_context)
1327 : : {
1328 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1329 : : libspdm_reset_managed_buffer(&spdm_context->transcript.message_c);
1330 : : #else
1331 [ + + ]: 2854 : if (spdm_context->transcript.digest_context_m1m2 != NULL) {
1332 : 2 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1333 : : spdm_context->transcript.digest_context_m1m2);
1334 : 2 : spdm_context->transcript.digest_context_m1m2 = NULL;
1335 : : }
1336 : : #endif
1337 : 2854 : }
1338 : :
1339 : 2714 : void libspdm_reset_message_mut_b(libspdm_context_t *spdm_context)
1340 : : {
1341 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1342 : : libspdm_reset_managed_buffer(&spdm_context->transcript.message_mut_b);
1343 : : #else
1344 [ + + ]: 2714 : if (spdm_context->transcript.digest_context_mut_m1m2 != NULL) {
1345 : 128 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1346 : : spdm_context->transcript.digest_context_mut_m1m2);
1347 : 128 : spdm_context->transcript.digest_context_mut_m1m2 = NULL;
1348 : : }
1349 : : #endif
1350 : 2714 : }
1351 : :
1352 : 2648 : void libspdm_reset_message_mut_c(libspdm_context_t *spdm_context)
1353 : : {
1354 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1355 : : libspdm_reset_managed_buffer(&spdm_context->transcript.message_mut_c);
1356 : : #else
1357 [ + + ]: 2648 : if (spdm_context->transcript.digest_context_mut_m1m2 != NULL) {
1358 : 10 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1359 : : spdm_context->transcript.digest_context_mut_m1m2);
1360 : 10 : spdm_context->transcript.digest_context_mut_m1m2 = NULL;
1361 : : }
1362 : : #endif
1363 : 2648 : }
1364 : :
1365 : 15084 : void libspdm_reset_message_m(libspdm_context_t *spdm_context, void *session_info)
1366 : : {
1367 : : libspdm_session_info_t *spdm_session_info;
1368 : :
1369 : 15084 : spdm_session_info = session_info;
1370 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1371 : : if (spdm_session_info == NULL) {
1372 : : libspdm_reset_managed_buffer(&spdm_context->transcript.message_m);
1373 : : } else {
1374 : : libspdm_reset_managed_buffer(&spdm_session_info->session_transcript.message_m);
1375 : : }
1376 : : #else
1377 [ + + ]: 15084 : if (spdm_session_info == NULL) {
1378 [ + + ]: 10064 : if (spdm_context->transcript.digest_context_l1l2 != NULL) {
1379 : 138 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1380 : : spdm_context->transcript.digest_context_l1l2);
1381 : 138 : spdm_context->transcript.digest_context_l1l2 = NULL;
1382 : : }
1383 : : } else {
1384 [ + + ]: 5020 : if (spdm_session_info->session_transcript.digest_context_l1l2 != NULL) {
1385 : 10 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1386 : : spdm_session_info->session_transcript.digest_context_l1l2);
1387 : 10 : spdm_session_info->session_transcript.digest_context_l1l2 = NULL;
1388 : : }
1389 : : }
1390 : : #endif
1391 : : #if LIBSPDM_ENABLE_CAPABILITY_MEAS_CAP
1392 [ + + ]: 15084 : if (spdm_context->spdm_meas_log_reset_callback != NULL) {
1393 [ + + ]: 162 : spdm_context->spdm_meas_log_reset_callback(
1394 : : spdm_context, spdm_session_info == NULL ? NULL : &spdm_session_info->session_id);
1395 : : }
1396 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_MEAS_CAP */
1397 : 15084 : }
1398 : :
1399 : 4396 : void libspdm_reset_message_k(libspdm_context_t *spdm_context, void *session_info)
1400 : : {
1401 : : libspdm_session_info_t *spdm_session_info;
1402 : :
1403 : 4396 : spdm_session_info = session_info;
1404 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1405 : : libspdm_reset_managed_buffer(&spdm_session_info->session_transcript.message_k);
1406 : : #else
1407 : : {
1408 [ + + ]: 4396 : if (spdm_session_info->session_transcript.digest_context_th != NULL) {
1409 : 62 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1410 : : spdm_session_info->session_transcript.digest_context_th);
1411 : 62 : spdm_session_info->session_transcript.digest_context_th = NULL;
1412 : : }
1413 [ + + ]: 4396 : if (spdm_session_info->session_transcript.digest_context_th_backup != NULL) {
1414 : 38 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1415 : : spdm_session_info->session_transcript.digest_context_th_backup);
1416 : 38 : spdm_session_info->session_transcript.digest_context_th_backup = NULL;
1417 : : }
1418 : : }
1419 : : #endif
1420 : 4396 : }
1421 : :
1422 : 4392 : void libspdm_reset_message_encap_d(void *session_info)
1423 : : {
1424 : : libspdm_session_info_t *spdm_session_info;
1425 : :
1426 : 4392 : spdm_session_info = session_info;
1427 : 4392 : libspdm_reset_managed_buffer(&spdm_session_info->session_transcript.message_encap_d);
1428 : : /* Restarting the encapsulated DIGESTS transcript reopens the "first encapsulated response"
1429 : : * window. */
1430 : 4392 : spdm_session_info->session_transcript.encap_digest_window_closed = false;
1431 : 4392 : }
1432 : :
1433 : 4436 : void libspdm_reset_message_f(libspdm_context_t *spdm_context, void *session_info)
1434 : : {
1435 : : libspdm_session_info_t *spdm_session_info;
1436 : :
1437 : 4436 : spdm_session_info = session_info;
1438 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1439 : : libspdm_reset_managed_buffer(&spdm_session_info->session_transcript.message_f);
1440 : : #else
1441 : : {
1442 [ + + ]: 4436 : if (spdm_session_info->session_transcript.digest_context_th != NULL) {
1443 : 46 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1444 : : spdm_session_info->session_transcript.digest_context_th);
1445 : 46 : spdm_session_info->session_transcript.digest_context_th =
1446 : 46 : spdm_session_info->session_transcript.digest_context_th_backup;
1447 : 46 : spdm_session_info->session_transcript.digest_context_th_backup = NULL;
1448 : : }
1449 : 4436 : spdm_session_info->session_transcript.message_f_initialized = false;
1450 : : }
1451 : : #endif
1452 : 4436 : }
1453 : :
1454 : 5966 : void libspdm_reset_message_e(libspdm_context_t *spdm_context, void *session_info)
1455 : : {
1456 : : libspdm_session_info_t *spdm_session_info;
1457 : :
1458 : 5966 : spdm_session_info = session_info;
1459 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1460 : : if (spdm_session_info == NULL) {
1461 : : libspdm_reset_managed_buffer(&spdm_context->transcript.message_e);
1462 : : } else {
1463 : : libspdm_reset_managed_buffer(&spdm_session_info->session_transcript.message_e);
1464 : : }
1465 : : #else
1466 [ + + ]: 5966 : if (spdm_session_info == NULL) {
1467 [ + + ]: 1574 : if (spdm_context->transcript.digest_context_il1il2 != NULL) {
1468 : 28 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1469 : : spdm_context->transcript.digest_context_il1il2);
1470 : 28 : spdm_context->transcript.digest_context_il1il2 = NULL;
1471 : : }
1472 : : } else {
1473 [ + + ]: 4392 : if (spdm_session_info->session_transcript.digest_context_il1il2 != NULL) {
1474 : 6 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1475 : : spdm_session_info->session_transcript.digest_context_il1il2);
1476 : 6 : spdm_session_info->session_transcript.digest_context_il1il2 = NULL;
1477 : : }
1478 : : }
1479 : : #endif
1480 : 5966 : }
1481 : :
1482 : 5896 : void libspdm_reset_message_encap_e(libspdm_context_t *spdm_context, void *session_info)
1483 : : {
1484 : : libspdm_session_info_t *spdm_session_info;
1485 : :
1486 : 5896 : spdm_session_info = session_info;
1487 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1488 : : if (spdm_session_info == NULL) {
1489 : : libspdm_reset_managed_buffer(&spdm_context->transcript.message_encap_e);
1490 : : } else {
1491 : : libspdm_reset_managed_buffer(&spdm_session_info->session_transcript.message_encap_e);
1492 : : }
1493 : : #else
1494 [ + + ]: 5896 : if (spdm_session_info == NULL) {
1495 [ + + ]: 1504 : if (spdm_context->transcript.digest_context_encap_il1il2 != NULL) {
1496 : 28 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1497 : : spdm_context->transcript.digest_context_encap_il1il2);
1498 : 28 : spdm_context->transcript.digest_context_encap_il1il2 = NULL;
1499 : : }
1500 : : } else {
1501 [ + + ]: 4392 : if (spdm_session_info->session_transcript.digest_context_encap_il1il2 != NULL) {
1502 : 4 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1503 : : spdm_session_info->session_transcript.digest_context_encap_il1il2);
1504 : 4 : spdm_session_info->session_transcript.digest_context_encap_il1il2 = NULL;
1505 : : }
1506 : : }
1507 : : #endif
1508 : 5896 : }
1509 : :
1510 : 9612 : void libspdm_reset_message_buffer_via_request_code(void *context, void *session_info,
1511 : : uint8_t request_code)
1512 : : {
1513 : : libspdm_context_t *spdm_context;
1514 : :
1515 : 9612 : spdm_context = context;
1516 : : /* Close the DIGESTS transcript window once a non-GET_DIGESTS request is processed after
1517 : : * NEGOTIATED (the state check excludes the VCA messages). */
1518 [ + + ]: 9612 : if ((request_code != SPDM_GET_DIGESTS) &&
1519 [ + + ]: 9458 : (spdm_context->connection_info.connection_state >= LIBSPDM_CONNECTION_STATE_NEGOTIATED)) {
1520 : 8770 : spdm_context->connection_info.digest_transcript_window_closed = true;
1521 : : }
1522 : : /**
1523 : : * Any request other than SPDM_GET_MEASUREMENTS resets L1/L2
1524 : : */
1525 [ + + ]: 9612 : if (request_code != SPDM_GET_MEASUREMENTS) {
1526 : 8734 : libspdm_reset_message_m(spdm_context, session_info);
1527 : : }
1528 : : /**
1529 : : * If the Requester issued GET_MEASUREMENTS or KEY_EXCHANGE or FINISH or PSK_EXCHANGE
1530 : : * or PSK_FINISH or KEY_UPDATE or HEARTBEAT or GET_ENCAPSULATED_REQUEST or DELIVER_ENCAPSULATED_RESPONSE
1531 : : * or END_SESSION request(s) or SPDM_GET_MEASUREMENT_EXTENSION_LOG and skipped CHALLENGE completion, M1 and M2 are reset to null.
1532 : : */
1533 [ + + + + : 9612 : switch (request_code)
+ ]
1534 : : {
1535 : 2112 : case SPDM_KEY_EXCHANGE:
1536 : : case SPDM_GET_MEASUREMENTS:
1537 : : case SPDM_FINISH:
1538 : : case SPDM_PSK_EXCHANGE:
1539 : : case SPDM_PSK_FINISH:
1540 : : case SPDM_KEY_UPDATE:
1541 : : case SPDM_HEARTBEAT:
1542 : : case SPDM_GET_ENCAPSULATED_REQUEST:
1543 : : case SPDM_END_SESSION:
1544 : : case SPDM_GET_MEASUREMENT_EXTENSION_LOG:
1545 [ + + ]: 2112 : if (spdm_context->connection_info.connection_state <
1546 : : LIBSPDM_CONNECTION_STATE_AUTHENTICATED) {
1547 : 1190 : libspdm_reset_message_b(spdm_context);
1548 : 1190 : libspdm_reset_message_c(spdm_context);
1549 : 1190 : libspdm_reset_message_mut_b(spdm_context);
1550 : 1190 : libspdm_reset_message_mut_c(spdm_context);
1551 : : }
1552 : 2112 : break;
1553 : 96 : case SPDM_DELIVER_ENCAPSULATED_RESPONSE:
1554 [ + - ]: 96 : if (spdm_context->connection_info.connection_state <
1555 : : LIBSPDM_CONNECTION_STATE_AUTHENTICATED) {
1556 : 96 : libspdm_reset_message_b(spdm_context);
1557 : 96 : libspdm_reset_message_c(spdm_context);
1558 : : }
1559 : 96 : break;
1560 : 154 : case SPDM_GET_DIGESTS:
1561 : 154 : libspdm_reset_message_b(spdm_context);
1562 : 154 : break;
1563 : 112 : case SPDM_GET_ENDPOINT_INFO:
1564 : 112 : libspdm_reset_message_e(spdm_context, session_info);
1565 : 112 : libspdm_reset_message_encap_e(spdm_context, session_info);
1566 : 112 : break;
1567 : 7138 : default:
1568 : 7138 : break;
1569 : : }
1570 : 9612 : }
1571 : :
1572 : 80 : void libspdm_reset_message_buffer_via_encap_request_code(void *context, void *session_info,
1573 : : uint8_t request_code)
1574 : : {
1575 : : libspdm_session_info_t *spdm_session_info;
1576 : :
1577 : : /* The encapsulated flow only affects the session transcript. */
1578 [ + + ]: 80 : if (session_info == NULL) {
1579 : 48 : return;
1580 : : }
1581 : 32 : spdm_session_info = session_info;
1582 : :
1583 : : /* Close the encapsulated DIGESTS window if the first encapsulated request is not GET_DIGESTS.
1584 : : * Enforced here (not from a fixed local sequence) so it holds for whatever order the peer drives,
1585 : : * for both the Requester and Responder encapsulated flows. */
1586 [ + + + - ]: 48 : if ((request_code != SPDM_GET_DIGESTS) &&
1587 : 16 : (libspdm_get_managed_buffer_size(
1588 : 16 : &spdm_session_info->session_transcript.message_encap_d) == 0)) {
1589 : 16 : spdm_session_info->session_transcript.encap_digest_window_closed = true;
1590 : : }
1591 : : }
1592 : :
1593 : 634 : libspdm_return_t libspdm_append_message_a(libspdm_context_t *spdm_context, const void *message,
1594 : : size_t message_size)
1595 : : {
1596 : 634 : return libspdm_append_managed_buffer(&spdm_context->transcript.message_a,
1597 : : message, message_size);
1598 : : }
1599 : :
1600 : 20 : libspdm_return_t libspdm_append_message_d(libspdm_context_t *spdm_context, const void *message,
1601 : : size_t message_size)
1602 : : {
1603 : : /* Window closed: the GET_DIGESTS did not immediately follow the VCA, so no DIGESTS is added. */
1604 [ + + ]: 20 : if (spdm_context->connection_info.digest_transcript_window_closed) {
1605 : 2 : return LIBSPDM_STATUS_SUCCESS;
1606 : : }
1607 : : /* Only the first message D after VCA in connection counts */
1608 [ + + ]: 18 : if (libspdm_get_managed_buffer_size(&spdm_context->transcript.message_d) != 0) {
1609 : 2 : return LIBSPDM_STATUS_SUCCESS;
1610 : : }
1611 : 16 : return libspdm_append_managed_buffer(&spdm_context->transcript.message_d,
1612 : : message, message_size);
1613 : : }
1614 : :
1615 : 12506 : libspdm_return_t libspdm_append_message_b(libspdm_context_t *spdm_context, const void *message,
1616 : : size_t message_size)
1617 : : {
1618 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1619 : : return libspdm_append_managed_buffer(&spdm_context->transcript.message_b,
1620 : : message, message_size);
1621 : : #else
1622 : : {
1623 : : bool result;
1624 : :
1625 [ + + ]: 12506 : if (spdm_context->transcript.digest_context_m1m2 == NULL) {
1626 : 372 : spdm_context->transcript.digest_context_m1m2 = libspdm_hash_new (
1627 : : spdm_context->connection_info.algorithm.base_hash_algo);
1628 [ - + ]: 372 : if (spdm_context->transcript.digest_context_m1m2 == NULL) {
1629 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1630 : : }
1631 : 372 : result = libspdm_hash_init (spdm_context->connection_info.algorithm.base_hash_algo,
1632 : : spdm_context->transcript.digest_context_m1m2);
1633 [ - + ]: 372 : if (!result) {
1634 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1635 : : spdm_context->transcript.digest_context_m1m2);
1636 : 0 : spdm_context->transcript.digest_context_m1m2 = NULL;
1637 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1638 : : }
1639 : 372 : result = libspdm_hash_update (spdm_context->connection_info.algorithm.base_hash_algo,
1640 : : spdm_context->transcript.digest_context_m1m2,
1641 : 372 : libspdm_get_managed_buffer(&spdm_context->transcript.
1642 : : message_a),
1643 : 372 : libspdm_get_managed_buffer_size(&spdm_context->transcript.
1644 : : message_a));
1645 [ - + ]: 372 : if (!result) {
1646 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1647 : : spdm_context->transcript.digest_context_m1m2);
1648 : 0 : spdm_context->transcript.digest_context_m1m2 = NULL;
1649 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1650 : : }
1651 : : }
1652 : :
1653 : 12506 : result = libspdm_hash_update (spdm_context->connection_info.algorithm.base_hash_algo,
1654 : : spdm_context->transcript.digest_context_m1m2, message,
1655 : : message_size);
1656 [ - + ]: 12506 : if (!result) {
1657 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1658 : : spdm_context->transcript.digest_context_m1m2);
1659 : 0 : spdm_context->transcript.digest_context_m1m2 = NULL;
1660 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1661 : : }
1662 : :
1663 : 12506 : return LIBSPDM_STATUS_SUCCESS;
1664 : : }
1665 : : #endif
1666 : : }
1667 : :
1668 : 120 : libspdm_return_t libspdm_append_message_c(libspdm_context_t *spdm_context, const void *message,
1669 : : size_t message_size)
1670 : : {
1671 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1672 : : return libspdm_append_managed_buffer(&spdm_context->transcript.message_c,
1673 : : message, message_size);
1674 : : #else
1675 : : {
1676 : : bool result;
1677 : :
1678 [ + + ]: 120 : if (spdm_context->transcript.digest_context_m1m2 == NULL) {
1679 : 58 : spdm_context->transcript.digest_context_m1m2 = libspdm_hash_new (
1680 : : spdm_context->connection_info.algorithm.base_hash_algo);
1681 [ - + ]: 58 : if (spdm_context->transcript.digest_context_m1m2 == NULL) {
1682 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1683 : : }
1684 : 58 : result = libspdm_hash_init (spdm_context->connection_info.algorithm.base_hash_algo,
1685 : : spdm_context->transcript.digest_context_m1m2);
1686 [ - + ]: 58 : if (!result) {
1687 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1688 : : spdm_context->transcript.digest_context_m1m2);
1689 : 0 : spdm_context->transcript.digest_context_m1m2 = NULL;
1690 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1691 : : }
1692 : 58 : result = libspdm_hash_update (spdm_context->connection_info.algorithm.base_hash_algo,
1693 : : spdm_context->transcript.digest_context_m1m2,
1694 : 58 : libspdm_get_managed_buffer(&spdm_context->transcript.
1695 : : message_a),
1696 : 58 : libspdm_get_managed_buffer_size(&spdm_context->transcript.
1697 : : message_a));
1698 [ - + ]: 58 : if (!result) {
1699 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1700 : : spdm_context->transcript.digest_context_m1m2);
1701 : 0 : spdm_context->transcript.digest_context_m1m2 = NULL;
1702 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1703 : : }
1704 : : }
1705 : :
1706 : 120 : result = libspdm_hash_update (spdm_context->connection_info.algorithm.base_hash_algo,
1707 : : spdm_context->transcript.digest_context_m1m2, message,
1708 : : message_size);
1709 [ - + ]: 120 : if (!result) {
1710 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1711 : : spdm_context->transcript.digest_context_m1m2);
1712 : 0 : spdm_context->transcript.digest_context_m1m2 = NULL;
1713 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1714 : : }
1715 : :
1716 : 120 : return LIBSPDM_STATUS_SUCCESS;
1717 : : }
1718 : : #endif
1719 : : }
1720 : :
1721 : 6060 : libspdm_return_t libspdm_append_message_mut_b(libspdm_context_t *spdm_context, const void *message,
1722 : : size_t message_size)
1723 : : {
1724 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1725 : : return libspdm_append_managed_buffer(&spdm_context->transcript.message_mut_b,
1726 : : message, message_size);
1727 : : #else
1728 : : {
1729 : : bool result;
1730 : :
1731 [ + + ]: 6060 : if (spdm_context->transcript.digest_context_mut_m1m2 == NULL) {
1732 : 112 : spdm_context->transcript.digest_context_mut_m1m2 = libspdm_hash_new (
1733 : : spdm_context->connection_info.algorithm.base_hash_algo);
1734 [ - + ]: 112 : if (spdm_context->transcript.digest_context_mut_m1m2 == NULL) {
1735 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1736 : : }
1737 : 112 : result = libspdm_hash_init (spdm_context->connection_info.algorithm.base_hash_algo,
1738 : : spdm_context->transcript.digest_context_mut_m1m2);
1739 [ - + ]: 112 : if (!result) {
1740 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1741 : : spdm_context->transcript.digest_context_mut_m1m2);
1742 : 0 : spdm_context->transcript.digest_context_mut_m1m2 = NULL;
1743 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1744 : : }
1745 [ + + ]: 112 : if ((spdm_context->connection_info.version >> SPDM_VERSION_NUMBER_SHIFT_BIT) >
1746 : : SPDM_MESSAGE_VERSION_11) {
1747 : :
1748 : : /* Need append VCA since 1.2 script */
1749 : 18 : result = libspdm_hash_update (
1750 : : spdm_context->connection_info.algorithm.base_hash_algo,
1751 : : spdm_context->transcript.digest_context_mut_m1m2,
1752 : 18 : libspdm_get_managed_buffer(&spdm_context->transcript.message_a),
1753 : 18 : libspdm_get_managed_buffer_size(&spdm_context->transcript.
1754 : : message_a));
1755 [ - + ]: 18 : if (!result) {
1756 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1757 : : spdm_context->transcript.digest_context_mut_m1m2);
1758 : 0 : spdm_context->transcript.digest_context_mut_m1m2 = NULL;
1759 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1760 : : }
1761 : : }
1762 : : }
1763 : :
1764 : 6060 : result = libspdm_hash_update (spdm_context->connection_info.algorithm.base_hash_algo,
1765 : : spdm_context->transcript.digest_context_mut_m1m2, message,
1766 : : message_size);
1767 [ - + ]: 6060 : if (!result) {
1768 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1769 : : spdm_context->transcript.digest_context_mut_m1m2);
1770 : 0 : spdm_context->transcript.digest_context_mut_m1m2 = NULL;
1771 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1772 : : }
1773 : :
1774 : 6060 : return LIBSPDM_STATUS_SUCCESS;
1775 : : }
1776 : : #endif
1777 : : }
1778 : :
1779 : 34 : libspdm_return_t libspdm_append_message_mut_c(libspdm_context_t *spdm_context, const void *message,
1780 : : size_t message_size)
1781 : : {
1782 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1783 : : return libspdm_append_managed_buffer(&spdm_context->transcript.message_mut_c,
1784 : : message, message_size);
1785 : : #else
1786 : : {
1787 : : bool result;
1788 : :
1789 [ + + ]: 34 : if (spdm_context->transcript.digest_context_mut_m1m2 == NULL) {
1790 : 26 : spdm_context->transcript.digest_context_mut_m1m2 = libspdm_hash_new (
1791 : : spdm_context->connection_info.algorithm.base_hash_algo);
1792 [ - + ]: 26 : if (spdm_context->transcript.digest_context_mut_m1m2 == NULL) {
1793 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1794 : : }
1795 : 26 : result = libspdm_hash_init (spdm_context->connection_info.algorithm.base_hash_algo,
1796 : : spdm_context->transcript.digest_context_mut_m1m2);
1797 [ - + ]: 26 : if (!result) {
1798 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1799 : : spdm_context->transcript.digest_context_mut_m1m2);
1800 : 0 : spdm_context->transcript.digest_context_mut_m1m2 = NULL;
1801 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1802 : : }
1803 [ + + ]: 26 : if ((spdm_context->connection_info.version >> SPDM_VERSION_NUMBER_SHIFT_BIT) >
1804 : : SPDM_MESSAGE_VERSION_11) {
1805 : :
1806 : : /* Need append VCA since 1.2 script */
1807 : 12 : result = libspdm_hash_update (
1808 : : spdm_context->connection_info.algorithm.base_hash_algo,
1809 : : spdm_context->transcript.digest_context_mut_m1m2,
1810 : 12 : libspdm_get_managed_buffer(&spdm_context->transcript.message_a),
1811 : 12 : libspdm_get_managed_buffer_size(&spdm_context->transcript.
1812 : : message_a));
1813 [ - + ]: 12 : if (!result) {
1814 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1815 : : spdm_context->transcript.digest_context_mut_m1m2);
1816 : 0 : spdm_context->transcript.digest_context_mut_m1m2 = NULL;
1817 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1818 : : }
1819 : : }
1820 : : }
1821 : :
1822 : 34 : result = libspdm_hash_update (spdm_context->connection_info.algorithm.base_hash_algo,
1823 : : spdm_context->transcript.digest_context_mut_m1m2, message,
1824 : : message_size);
1825 [ - + ]: 34 : if (!result) {
1826 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1827 : : spdm_context->transcript.digest_context_mut_m1m2);
1828 : 0 : spdm_context->transcript.digest_context_mut_m1m2 = NULL;
1829 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1830 : : }
1831 : :
1832 : 34 : return LIBSPDM_STATUS_SUCCESS;
1833 : : }
1834 : : #endif
1835 : : }
1836 : :
1837 : 1478 : libspdm_return_t libspdm_append_message_m(libspdm_context_t *spdm_context, void *session_info,
1838 : : const void *message, size_t message_size)
1839 : : {
1840 : : libspdm_session_info_t *spdm_session_info;
1841 : :
1842 : 1478 : spdm_session_info = session_info;
1843 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1844 : : if (spdm_session_info == NULL) {
1845 : : return libspdm_append_managed_buffer(&spdm_context->transcript.message_m,
1846 : : message, message_size);
1847 : : } else {
1848 : : return libspdm_append_managed_buffer(&spdm_session_info->session_transcript.message_m,
1849 : : message, message_size);
1850 : : }
1851 : : #else
1852 : : {
1853 : : bool result;
1854 : :
1855 [ + + ]: 1478 : if (spdm_session_info == NULL) {
1856 [ + + ]: 1460 : if (spdm_context->transcript.digest_context_l1l2 == NULL) {
1857 : 136 : spdm_context->transcript.digest_context_l1l2 = libspdm_hash_new (
1858 : : spdm_context->connection_info.algorithm.base_hash_algo);
1859 [ - + ]: 136 : if (spdm_context->transcript.digest_context_l1l2 == NULL) {
1860 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1861 : : }
1862 : 136 : result = libspdm_hash_init (spdm_context->connection_info.algorithm.base_hash_algo,
1863 : : spdm_context->transcript.digest_context_l1l2);
1864 [ - + ]: 136 : if (!result) {
1865 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1866 : : spdm_context->transcript.digest_context_l1l2);
1867 : 0 : spdm_context->transcript.digest_context_l1l2 = NULL;
1868 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1869 : : }
1870 [ + + ]: 136 : if ((spdm_context->connection_info.version >> SPDM_VERSION_NUMBER_SHIFT_BIT) >
1871 : : SPDM_MESSAGE_VERSION_11) {
1872 : :
1873 : : /* Need append VCA since 1.2 script */
1874 : 28 : result = libspdm_hash_update (
1875 : : spdm_context->connection_info.algorithm.base_hash_algo,
1876 : : spdm_context->transcript.digest_context_l1l2,
1877 : 28 : libspdm_get_managed_buffer(
1878 : 28 : &spdm_context->transcript.message_a),
1879 : 28 : libspdm_get_managed_buffer_size(&spdm_context->transcript.
1880 : : message_a));
1881 [ - + ]: 28 : if (!result) {
1882 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1883 : : spdm_context->transcript.digest_context_l1l2);
1884 : 0 : spdm_context->transcript.digest_context_l1l2 = NULL;
1885 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1886 : : }
1887 : : }
1888 : : }
1889 : 1460 : result = libspdm_hash_update (spdm_context->connection_info.algorithm.base_hash_algo,
1890 : : spdm_context->transcript.digest_context_l1l2, message,
1891 : : message_size);
1892 [ - + ]: 1460 : if (!result) {
1893 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1894 : : spdm_context->transcript.digest_context_l1l2);
1895 : 0 : spdm_context->transcript.digest_context_l1l2 = NULL;
1896 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1897 : : }
1898 : : } else {
1899 [ + + ]: 18 : if (spdm_session_info->session_transcript.digest_context_l1l2 == NULL) {
1900 : 10 : spdm_session_info->session_transcript.digest_context_l1l2 = libspdm_hash_new (
1901 : : spdm_context->connection_info.algorithm.base_hash_algo);
1902 [ - + ]: 10 : if (spdm_session_info->session_transcript.digest_context_l1l2 == NULL) {
1903 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1904 : : }
1905 : 10 : result = libspdm_hash_init (spdm_context->connection_info.algorithm.base_hash_algo,
1906 : : spdm_session_info->session_transcript.digest_context_l1l2);
1907 [ - + ]: 10 : if (!result) {
1908 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1909 : : spdm_session_info->session_transcript.digest_context_l1l2);
1910 : 0 : spdm_session_info->session_transcript.digest_context_l1l2 = NULL;
1911 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1912 : : }
1913 [ + + ]: 10 : if ((spdm_context->connection_info.version >> SPDM_VERSION_NUMBER_SHIFT_BIT) >
1914 : : SPDM_MESSAGE_VERSION_11) {
1915 : :
1916 : : /* Need append VCA since 1.2 script*/
1917 : :
1918 : 2 : result = libspdm_hash_update (
1919 : : spdm_context->connection_info.algorithm.base_hash_algo,
1920 : : spdm_session_info->session_transcript.digest_context_l1l2,
1921 : 2 : libspdm_get_managed_buffer(
1922 : 2 : &spdm_context->transcript.message_a),
1923 : 2 : libspdm_get_managed_buffer_size(&spdm_context->transcript.
1924 : : message_a));
1925 [ - + ]: 2 : if (!result) {
1926 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1927 : : spdm_session_info->session_transcript.digest_context_l1l2);
1928 : 0 : spdm_session_info->session_transcript.digest_context_l1l2 = NULL;
1929 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1930 : : }
1931 : : }
1932 : : }
1933 : 18 : result = libspdm_hash_update (spdm_context->connection_info.algorithm.base_hash_algo,
1934 : : spdm_session_info->session_transcript.digest_context_l1l2,
1935 : : message, message_size);
1936 [ - + ]: 18 : if (!result) {
1937 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1938 : : spdm_session_info->session_transcript.digest_context_l1l2);
1939 : 0 : spdm_session_info->session_transcript.digest_context_l1l2 = NULL;
1940 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1941 : : }
1942 : : }
1943 : :
1944 : 1478 : return LIBSPDM_STATUS_SUCCESS;
1945 : : }
1946 : : #endif
1947 : : }
1948 : :
1949 : 844 : libspdm_return_t libspdm_append_message_k(libspdm_context_t *spdm_context,
1950 : : void *session_info,
1951 : : bool is_requester, const void *message,
1952 : : size_t message_size)
1953 : : {
1954 : : libspdm_session_info_t *spdm_session_info;
1955 : :
1956 : 844 : spdm_session_info = session_info;
1957 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1958 : : return libspdm_append_managed_buffer(
1959 : : &spdm_session_info->session_transcript.message_k, message,
1960 : : message_size);
1961 : : #else
1962 : : {
1963 : : const uint8_t *cert_chain_buffer;
1964 : : size_t cert_chain_buffer_size;
1965 : : bool result;
1966 : : uint8_t cert_chain_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
1967 : : uint32_t hash_size;
1968 : : uint8_t slot_id;
1969 : :
1970 : 844 : hash_size = libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
1971 : :
1972 [ + + ]: 844 : if (spdm_session_info->session_transcript.digest_context_th == NULL) {
1973 [ + + ]: 422 : if (!spdm_session_info->use_psk) {
1974 [ + + ]: 246 : if (is_requester) {
1975 : 142 : slot_id = spdm_session_info->peer_used_cert_chain_slot_id;
1976 : 142 : LIBSPDM_ASSERT((slot_id < SPDM_MAX_SLOT_COUNT) || (slot_id == 0xFF));
1977 [ + + ]: 142 : if (slot_id == 0xFF) {
1978 : 8 : result = libspdm_get_peer_public_key_buffer(
1979 : : spdm_context, (const void **)&cert_chain_buffer,
1980 : : &cert_chain_buffer_size);
1981 [ + + ]: 8 : if (!result) {
1982 : 4 : return LIBSPDM_STATUS_INVALID_STATE_PEER;
1983 : : }
1984 : :
1985 : 4 : result = libspdm_hash_all(
1986 : : spdm_context->connection_info.algorithm.base_hash_algo,
1987 : : cert_chain_buffer, cert_chain_buffer_size,
1988 : : cert_chain_buffer_hash);
1989 [ - + ]: 4 : if (!result) {
1990 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1991 : : }
1992 : : } else {
1993 : 134 : LIBSPDM_ASSERT(
1994 : : hash_size ==
1995 : : spdm_context->connection_info
1996 : : .peer_used_cert_chain[slot_id].buffer_hash_size);
1997 : :
1998 : 134 : libspdm_copy_mem(cert_chain_buffer_hash,
1999 : : sizeof(cert_chain_buffer_hash),
2000 : : spdm_context->connection_info
2001 : 134 : .peer_used_cert_chain[slot_id].buffer_hash,
2002 : : hash_size);
2003 : : }
2004 : : } else {
2005 : 104 : slot_id = spdm_session_info->local_used_cert_chain_slot_id;
2006 : 104 : LIBSPDM_ASSERT((slot_id < SPDM_MAX_SLOT_COUNT) || (slot_id == 0xFF));
2007 [ + + ]: 104 : if (slot_id == 0xFF) {
2008 : 8 : result = libspdm_get_local_public_key_buffer(
2009 : : spdm_context, (const void **)&cert_chain_buffer,
2010 : : &cert_chain_buffer_size);
2011 [ + + ]: 8 : if (!result) {
2012 : 2 : return LIBSPDM_STATUS_INVALID_STATE_LOCAL;
2013 : : }
2014 : : } else {
2015 : 96 : libspdm_get_local_cert_chain_buffer(
2016 : : spdm_context, slot_id, (const void **)&cert_chain_buffer,
2017 : : &cert_chain_buffer_size);
2018 : : }
2019 : :
2020 : 102 : result = libspdm_hash_all(
2021 : : spdm_context->connection_info.algorithm.base_hash_algo,
2022 : : cert_chain_buffer, cert_chain_buffer_size,
2023 : : cert_chain_buffer_hash);
2024 [ - + ]: 102 : if (!result) {
2025 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2026 : : }
2027 : : }
2028 : : }
2029 : : }
2030 : :
2031 : :
2032 : : /* prepare digest_context_th*/
2033 : :
2034 [ + + ]: 838 : if (spdm_session_info->session_transcript.digest_context_th == NULL) {
2035 : 416 : spdm_session_info->session_transcript.digest_context_th = libspdm_hash_new (
2036 : : spdm_context->connection_info.algorithm.base_hash_algo);
2037 [ - + ]: 416 : if (spdm_session_info->session_transcript.digest_context_th == NULL) {
2038 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2039 : : }
2040 : 416 : result = libspdm_hash_init (spdm_context->connection_info.algorithm.base_hash_algo,
2041 : : spdm_session_info->session_transcript.digest_context_th);
2042 [ - + ]: 416 : if (!result) {
2043 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
2044 : : spdm_session_info->session_transcript.digest_context_th);
2045 : 0 : spdm_session_info->session_transcript.digest_context_th = NULL;
2046 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2047 : : }
2048 : 416 : result = libspdm_hash_update (spdm_context->connection_info.algorithm.base_hash_algo,
2049 : : spdm_session_info->session_transcript.digest_context_th,
2050 : 416 : libspdm_get_managed_buffer(&spdm_context->transcript.
2051 : : message_a),
2052 : : libspdm_get_managed_buffer_size(
2053 : 416 : &spdm_context->transcript.message_a));
2054 [ - + ]: 416 : if (!result) {
2055 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
2056 : : spdm_session_info->session_transcript.digest_context_th);
2057 : 0 : spdm_session_info->session_transcript.digest_context_th = NULL;
2058 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2059 : : }
2060 [ + + ]: 416 : if (!spdm_session_info->use_psk) {
2061 [ + + ]: 240 : if (spdm_context->connection_info.multi_key_conn_rsp) {
2062 : 2 : result = libspdm_hash_update (
2063 : : spdm_context->connection_info.algorithm.base_hash_algo,
2064 : : spdm_session_info->session_transcript.digest_context_th,
2065 : 2 : libspdm_get_managed_buffer(&spdm_context->transcript.message_d),
2066 : 2 : libspdm_get_managed_buffer_size(&spdm_context->transcript.message_d));
2067 [ - + ]: 2 : if (!result) {
2068 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
2069 : : spdm_session_info->session_transcript.digest_context_th);
2070 : 0 : spdm_session_info->session_transcript.digest_context_th = NULL;
2071 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2072 : : }
2073 : : }
2074 : :
2075 : 240 : result = libspdm_hash_update (
2076 : : spdm_context->connection_info.algorithm.base_hash_algo,
2077 : : spdm_session_info->session_transcript.digest_context_th,
2078 : : cert_chain_buffer_hash, hash_size);
2079 [ - + ]: 240 : if (!result) {
2080 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
2081 : : spdm_session_info->session_transcript.digest_context_th);
2082 : 0 : spdm_session_info->session_transcript.digest_context_th = NULL;
2083 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2084 : : }
2085 : : }
2086 : : }
2087 : 838 : result = libspdm_hash_update (spdm_context->connection_info.algorithm.base_hash_algo,
2088 : : spdm_session_info->session_transcript.digest_context_th,
2089 : : message,
2090 : : message_size);
2091 [ - + ]: 838 : if (!result) {
2092 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
2093 : : spdm_session_info->session_transcript.digest_context_th);
2094 : 0 : spdm_session_info->session_transcript.digest_context_th = NULL;
2095 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2096 : : }
2097 : 838 : return LIBSPDM_STATUS_SUCCESS;
2098 : : }
2099 : : #endif
2100 : : }
2101 : :
2102 : 18 : libspdm_return_t libspdm_append_message_encap_d(void *session_info,
2103 : : const void *message,
2104 : : size_t message_size)
2105 : : {
2106 : : libspdm_session_info_t *spdm_session_info;
2107 : :
2108 : 18 : spdm_session_info = session_info;
2109 : : /* Window closed: the encapsulated DIGESTS is not the first encap response, so it is not added. */
2110 [ + + ]: 18 : if (spdm_session_info->session_transcript.encap_digest_window_closed) {
2111 : 2 : return LIBSPDM_STATUS_SUCCESS;
2112 : : }
2113 : : /* Only the first message EncapD in current session counts */
2114 [ + + ]: 16 : if (libspdm_get_managed_buffer_size(&spdm_session_info->session_transcript.message_encap_d) !=
2115 : : 0) {
2116 : 2 : return LIBSPDM_STATUS_SUCCESS;
2117 : : }
2118 : 14 : return libspdm_append_managed_buffer(
2119 : 14 : &spdm_session_info->session_transcript.message_encap_d, message,
2120 : : message_size);
2121 : : }
2122 : :
2123 : 664 : libspdm_return_t libspdm_append_message_f(libspdm_context_t *spdm_context,
2124 : : void *session_info,
2125 : : bool is_requester, const void *message,
2126 : : size_t message_size)
2127 : : {
2128 : : libspdm_session_info_t *spdm_session_info;
2129 : :
2130 : 664 : spdm_session_info = session_info;
2131 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
2132 : : return libspdm_append_managed_buffer(
2133 : : &spdm_session_info->session_transcript.message_f, message,
2134 : : message_size);
2135 : : #else
2136 : : {
2137 : : const uint8_t *mut_cert_chain_buffer;
2138 : : size_t mut_cert_chain_buffer_size;
2139 : : bool result;
2140 : : uint8_t mut_cert_chain_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
2141 : : uint32_t hash_size;
2142 : : libspdm_return_t status;
2143 : : uint8_t slot_id;
2144 : :
2145 : 664 : hash_size = libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
2146 : :
2147 [ + + ]: 664 : if (!spdm_session_info->session_transcript.message_f_initialized) {
2148 : : /* digest_context_th might be NULL in unit test, where message_k is hardcoded. */
2149 [ + + ]: 252 : if (spdm_session_info->session_transcript.digest_context_th == NULL) {
2150 : : status =
2151 : 244 : libspdm_append_message_k (spdm_context, session_info, is_requester, NULL, 0);
2152 [ + + ]: 244 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
2153 : 2 : return status;
2154 : : }
2155 : : }
2156 : :
2157 [ + + + + ]: 250 : if (!spdm_session_info->use_psk && (spdm_session_info->mut_auth_requested != 0)) {
2158 [ + + ]: 46 : if (is_requester) {
2159 : 18 : slot_id = spdm_session_info->local_used_cert_chain_slot_id;
2160 : 18 : LIBSPDM_ASSERT((slot_id < SPDM_MAX_SLOT_COUNT) || (slot_id == 0xFF));
2161 [ + + ]: 18 : if (slot_id == 0xFF) {
2162 : 2 : result = libspdm_get_local_public_key_buffer(
2163 : : spdm_context,
2164 : : (const void **)&mut_cert_chain_buffer,
2165 : : &mut_cert_chain_buffer_size);
2166 [ + - ]: 2 : if (!result) {
2167 : 2 : return LIBSPDM_STATUS_INVALID_STATE_LOCAL;
2168 : : }
2169 : : } else {
2170 : 16 : libspdm_get_local_cert_chain_buffer(
2171 : : spdm_context,
2172 : : slot_id,
2173 : : (const void **)&mut_cert_chain_buffer,
2174 : : &mut_cert_chain_buffer_size);
2175 : : }
2176 : :
2177 : 16 : result = libspdm_hash_all(
2178 : : spdm_context->connection_info.algorithm.base_hash_algo,
2179 : : mut_cert_chain_buffer, mut_cert_chain_buffer_size,
2180 : : mut_cert_chain_buffer_hash);
2181 [ - + ]: 16 : if (!result) {
2182 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2183 : : }
2184 : : } else {
2185 : 28 : slot_id = spdm_session_info->peer_used_cert_chain_slot_id;
2186 : 28 : LIBSPDM_ASSERT((slot_id < SPDM_MAX_SLOT_COUNT) || (slot_id == 0xFF));
2187 [ + + ]: 28 : if (slot_id == 0xFF) {
2188 : 4 : result = libspdm_get_peer_public_key_buffer(
2189 : : spdm_context,
2190 : : (const void **)&mut_cert_chain_buffer,
2191 : : &mut_cert_chain_buffer_size);
2192 [ + + ]: 4 : if (!result) {
2193 : 2 : return LIBSPDM_STATUS_INVALID_STATE_PEER;
2194 : : }
2195 : :
2196 : 2 : result = libspdm_hash_all(
2197 : : spdm_context->connection_info.algorithm.base_hash_algo,
2198 : : mut_cert_chain_buffer, mut_cert_chain_buffer_size,
2199 : : mut_cert_chain_buffer_hash);
2200 [ - + ]: 2 : if (!result) {
2201 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2202 : : }
2203 : : } else {
2204 : 24 : LIBSPDM_ASSERT(
2205 : : hash_size ==
2206 : : spdm_context->connection_info
2207 : : .peer_used_cert_chain[slot_id].buffer_hash_size);
2208 : :
2209 : 24 : libspdm_copy_mem(mut_cert_chain_buffer_hash,
2210 : : sizeof(mut_cert_chain_buffer_hash),
2211 : : spdm_context->connection_info
2212 : 24 : .peer_used_cert_chain[slot_id].buffer_hash,
2213 : : hash_size);
2214 : : }
2215 : : }
2216 : : }
2217 : :
2218 : : /* It is first time call, backup current message_k context
2219 : : * this backup will be used in reset_message_f.*/
2220 : :
2221 : 246 : LIBSPDM_ASSERT (spdm_session_info->session_transcript.digest_context_th != NULL);
2222 : 246 : spdm_session_info->session_transcript.digest_context_th_backup = libspdm_hash_new (
2223 : : spdm_context->connection_info.algorithm.base_hash_algo);
2224 [ - + ]: 246 : if (spdm_session_info->session_transcript.digest_context_th_backup == NULL) {
2225 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2226 : : }
2227 : 246 : result = libspdm_hash_duplicate (spdm_context->connection_info.algorithm.base_hash_algo,
2228 : 246 : spdm_session_info->session_transcript.digest_context_th,
2229 : : spdm_session_info->session_transcript.digest_context_th_backup);
2230 [ - + ]: 246 : if (!result) {
2231 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
2232 : : spdm_session_info->session_transcript.digest_context_th_backup);
2233 : 0 : spdm_session_info->session_transcript.digest_context_th_backup = NULL;
2234 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2235 : : }
2236 : : }
2237 : :
2238 : :
2239 : : /* prepare digest_context_th*/
2240 : :
2241 : 658 : LIBSPDM_ASSERT (spdm_session_info->session_transcript.digest_context_th != NULL);
2242 [ + + ]: 658 : if (!spdm_session_info->session_transcript.message_f_initialized) {
2243 [ + + + + ]: 246 : if (!spdm_session_info->use_psk && (spdm_session_info->mut_auth_requested != 0)) {
2244 [ + + ]: 42 : if (spdm_context->connection_info.multi_key_conn_req) {
2245 : 2 : result = libspdm_hash_update (
2246 : : spdm_context->connection_info.algorithm.base_hash_algo,
2247 : : spdm_session_info->session_transcript.digest_context_th,
2248 : 2 : libspdm_get_managed_buffer(&spdm_session_info->session_transcript.
2249 : : message_encap_d),
2250 : 2 : libspdm_get_managed_buffer_size(&spdm_session_info->session_transcript.
2251 : : message_encap_d));
2252 [ - + ]: 2 : if (!result) {
2253 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
2254 : : spdm_session_info->session_transcript.digest_context_th);
2255 : 0 : spdm_session_info->session_transcript.digest_context_th = NULL;
2256 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2257 : : }
2258 : : }
2259 : :
2260 : 42 : result = libspdm_hash_update (
2261 : : spdm_context->connection_info.algorithm.base_hash_algo,
2262 : : spdm_session_info->session_transcript.digest_context_th,
2263 : : mut_cert_chain_buffer_hash, hash_size);
2264 [ - + ]: 42 : if (!result) {
2265 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
2266 : : spdm_session_info->session_transcript.digest_context_th);
2267 : 0 : spdm_session_info->session_transcript.digest_context_th = NULL;
2268 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2269 : : }
2270 : : }
2271 : : }
2272 : 658 : result = libspdm_hash_update (spdm_context->connection_info.algorithm.base_hash_algo,
2273 : : spdm_session_info->session_transcript.digest_context_th,
2274 : : message,
2275 : : message_size);
2276 [ - + ]: 658 : if (!result) {
2277 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
2278 : : spdm_session_info->session_transcript.digest_context_th);
2279 : 0 : spdm_session_info->session_transcript.digest_context_th = NULL;
2280 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2281 : : }
2282 : :
2283 : 658 : spdm_session_info->session_transcript.message_f_initialized = true;
2284 : 658 : return LIBSPDM_STATUS_SUCCESS;
2285 : : }
2286 : : #endif
2287 : : }
2288 : :
2289 : 68 : libspdm_return_t libspdm_append_message_e(libspdm_context_t *spdm_context, void *session_info,
2290 : : const void *message, size_t message_size)
2291 : : {
2292 : : libspdm_session_info_t *spdm_session_info;
2293 : :
2294 : 68 : spdm_session_info = session_info;
2295 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
2296 : : if (spdm_session_info == NULL) {
2297 : : return libspdm_append_managed_buffer(&spdm_context->transcript.message_e,
2298 : : message, message_size);
2299 : : } else {
2300 : : return libspdm_append_managed_buffer(&spdm_session_info->session_transcript.message_e,
2301 : : message, message_size);
2302 : : }
2303 : : #else
2304 : : {
2305 : : bool result;
2306 : :
2307 [ + + ]: 68 : if (spdm_session_info == NULL) {
2308 [ + + ]: 56 : if (spdm_context->transcript.digest_context_il1il2 == NULL) {
2309 : 28 : spdm_context->transcript.digest_context_il1il2 = libspdm_hash_new (
2310 : : spdm_context->connection_info.algorithm.base_hash_algo);
2311 [ - + ]: 28 : if (spdm_context->transcript.digest_context_il1il2 == NULL) {
2312 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2313 : : }
2314 : 28 : result = libspdm_hash_init (spdm_context->connection_info.algorithm.base_hash_algo,
2315 : : spdm_context->transcript.digest_context_il1il2);
2316 [ - + ]: 28 : if (!result) {
2317 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
2318 : : spdm_context->transcript.digest_context_il1il2);
2319 : 0 : spdm_context->transcript.digest_context_il1il2 = NULL;
2320 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2321 : : }
2322 : :
2323 : 28 : result = libspdm_hash_update (
2324 : : spdm_context->connection_info.algorithm.base_hash_algo,
2325 : : spdm_context->transcript.digest_context_il1il2,
2326 : 28 : libspdm_get_managed_buffer(&spdm_context->transcript.message_a),
2327 : 28 : libspdm_get_managed_buffer_size(&spdm_context->transcript.message_a));
2328 : :
2329 [ - + ]: 28 : if (!result) {
2330 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
2331 : : spdm_context->transcript.digest_context_il1il2);
2332 : 0 : spdm_context->transcript.digest_context_il1il2 = NULL;
2333 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2334 : : }
2335 : : }
2336 : 56 : result = libspdm_hash_update (spdm_context->connection_info.algorithm.base_hash_algo,
2337 : : spdm_context->transcript.digest_context_il1il2, message,
2338 : : message_size);
2339 [ - + ]: 56 : if (!result) {
2340 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
2341 : : spdm_context->transcript.digest_context_il1il2);
2342 : 0 : spdm_context->transcript.digest_context_il1il2 = NULL;
2343 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2344 : : }
2345 : : } else {
2346 [ + + ]: 12 : if (spdm_session_info->session_transcript.digest_context_il1il2 == NULL) {
2347 : 6 : spdm_session_info->session_transcript.digest_context_il1il2 = libspdm_hash_new (
2348 : : spdm_context->connection_info.algorithm.base_hash_algo);
2349 [ - + ]: 6 : if (spdm_session_info->session_transcript.digest_context_il1il2 == NULL) {
2350 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2351 : : }
2352 : 6 : result = libspdm_hash_init (spdm_context->connection_info.algorithm.base_hash_algo,
2353 : : spdm_session_info->session_transcript.digest_context_il1il2);
2354 [ - + ]: 6 : if (!result) {
2355 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
2356 : : spdm_session_info->session_transcript.digest_context_il1il2);
2357 : 0 : spdm_session_info->session_transcript.digest_context_il1il2 = NULL;
2358 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2359 : : }
2360 : :
2361 : 6 : result = libspdm_hash_update (
2362 : : spdm_context->connection_info.algorithm.base_hash_algo,
2363 : : spdm_session_info->session_transcript.digest_context_il1il2,
2364 : 6 : libspdm_get_managed_buffer(&spdm_context->transcript.message_a),
2365 : 6 : libspdm_get_managed_buffer_size(&spdm_context->transcript.message_a));
2366 : :
2367 [ - + ]: 6 : if (!result) {
2368 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
2369 : : spdm_session_info->session_transcript.digest_context_il1il2);
2370 : 0 : spdm_session_info->session_transcript.digest_context_il1il2 = NULL;
2371 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2372 : : }
2373 : : }
2374 : 12 : result = libspdm_hash_update (spdm_context->connection_info.algorithm.base_hash_algo,
2375 : : spdm_session_info->session_transcript.digest_context_il1il2,
2376 : : message, message_size);
2377 [ - + ]: 12 : if (!result) {
2378 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
2379 : : spdm_session_info->session_transcript.digest_context_il1il2);
2380 : 0 : spdm_session_info->session_transcript.digest_context_il1il2 = NULL;
2381 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2382 : : }
2383 : : }
2384 : :
2385 : 68 : return LIBSPDM_STATUS_SUCCESS;
2386 : : }
2387 : : #endif
2388 : : }
2389 : :
2390 : 52 : libspdm_return_t libspdm_append_message_encap_e(libspdm_context_t *spdm_context, void *session_info,
2391 : : const void *message, size_t message_size)
2392 : : {
2393 : : libspdm_session_info_t *spdm_session_info;
2394 : :
2395 : 52 : spdm_session_info = session_info;
2396 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
2397 : : if (spdm_session_info == NULL) {
2398 : : return libspdm_append_managed_buffer(&spdm_context->transcript.message_encap_e,
2399 : : message, message_size);
2400 : : } else {
2401 : : return libspdm_append_managed_buffer(&spdm_session_info->session_transcript.message_encap_e,
2402 : : message, message_size);
2403 : : }
2404 : : #else
2405 : : {
2406 : : bool result;
2407 : :
2408 [ + + ]: 52 : if (spdm_session_info == NULL) {
2409 [ + + ]: 42 : if (spdm_context->transcript.digest_context_encap_il1il2 == NULL) {
2410 : 28 : spdm_context->transcript.digest_context_encap_il1il2 = libspdm_hash_new (
2411 : : spdm_context->connection_info.algorithm.base_hash_algo);
2412 [ - + ]: 28 : if (spdm_context->transcript.digest_context_encap_il1il2 == NULL) {
2413 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2414 : : }
2415 : 28 : result = libspdm_hash_init (spdm_context->connection_info.algorithm.base_hash_algo,
2416 : : spdm_context->transcript.digest_context_encap_il1il2);
2417 [ - + ]: 28 : if (!result) {
2418 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
2419 : : spdm_context->transcript.digest_context_encap_il1il2);
2420 : 0 : spdm_context->transcript.digest_context_encap_il1il2 = NULL;
2421 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2422 : : }
2423 : :
2424 : 28 : result = libspdm_hash_update (
2425 : : spdm_context->connection_info.algorithm.base_hash_algo,
2426 : : spdm_context->transcript.digest_context_encap_il1il2,
2427 : 28 : libspdm_get_managed_buffer(&spdm_context->transcript.message_a),
2428 : 28 : libspdm_get_managed_buffer_size(&spdm_context->transcript.message_a));
2429 : :
2430 [ - + ]: 28 : if (!result) {
2431 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
2432 : : spdm_context->transcript.digest_context_encap_il1il2);
2433 : 0 : spdm_context->transcript.digest_context_encap_il1il2 = NULL;
2434 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2435 : : }
2436 : : }
2437 : 42 : result = libspdm_hash_update (spdm_context->connection_info.algorithm.base_hash_algo,
2438 : : spdm_context->transcript.digest_context_encap_il1il2,
2439 : : message,
2440 : : message_size);
2441 [ - + ]: 42 : if (!result) {
2442 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
2443 : : spdm_context->transcript.digest_context_encap_il1il2);
2444 : 0 : spdm_context->transcript.digest_context_encap_il1il2 = NULL;
2445 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2446 : : }
2447 : : } else {
2448 [ + + ]: 10 : if (spdm_session_info->session_transcript.digest_context_encap_il1il2 == NULL) {
2449 : 6 : spdm_session_info->session_transcript.digest_context_encap_il1il2 =
2450 : 6 : libspdm_hash_new (spdm_context->connection_info.algorithm.base_hash_algo);
2451 [ - + ]: 6 : if (spdm_session_info->session_transcript.digest_context_encap_il1il2 == NULL) {
2452 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2453 : : }
2454 : 6 : result = libspdm_hash_init (spdm_context->connection_info.algorithm.base_hash_algo,
2455 : : spdm_session_info->session_transcript.digest_context_encap_il1il2);
2456 [ - + ]: 6 : if (!result) {
2457 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
2458 : : spdm_session_info->session_transcript.digest_context_encap_il1il2);
2459 : 0 : spdm_session_info->session_transcript.digest_context_encap_il1il2 = NULL;
2460 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2461 : : }
2462 : :
2463 : 6 : result = libspdm_hash_update (
2464 : : spdm_context->connection_info.algorithm.base_hash_algo,
2465 : : spdm_session_info->session_transcript.digest_context_encap_il1il2,
2466 : 6 : libspdm_get_managed_buffer(&spdm_context->transcript.message_a),
2467 : 6 : libspdm_get_managed_buffer_size(&spdm_context->transcript.message_a));
2468 : :
2469 [ - + ]: 6 : if (!result) {
2470 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
2471 : : spdm_session_info->session_transcript.digest_context_encap_il1il2);
2472 : 0 : spdm_session_info->session_transcript.digest_context_encap_il1il2 = NULL;
2473 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2474 : : }
2475 : : }
2476 : 10 : result = libspdm_hash_update (spdm_context->connection_info.algorithm.base_hash_algo,
2477 : : spdm_session_info->session_transcript.digest_context_encap_il1il2,
2478 : : message, message_size);
2479 [ - + ]: 10 : if (!result) {
2480 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
2481 : : spdm_session_info->session_transcript.digest_context_encap_il1il2);
2482 : 0 : spdm_session_info->session_transcript.digest_context_encap_il1il2 = NULL;
2483 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2484 : : }
2485 : : }
2486 : :
2487 : 52 : return LIBSPDM_STATUS_SUCCESS;
2488 : : }
2489 : : #endif
2490 : : }
2491 : 4 : bool libspdm_is_version_supported(const libspdm_context_t *spdm_context, uint8_t version)
2492 : : {
2493 [ + + ]: 4 : if (version == (spdm_context->connection_info.version >> SPDM_VERSION_NUMBER_SHIFT_BIT)) {
2494 : 2 : return true;
2495 : : }
2496 : :
2497 : 2 : return false;
2498 : : }
2499 : :
2500 : 29872 : uint8_t libspdm_get_connection_version(const libspdm_context_t *spdm_context)
2501 : : {
2502 : 29872 : return (uint8_t)(spdm_context->connection_info.version >> SPDM_VERSION_NUMBER_SHIFT_BIT);
2503 : : }
2504 : :
2505 : 30854 : bool libspdm_is_capabilities_flag_supported(const libspdm_context_t *spdm_context,
2506 : : bool is_requester,
2507 : : uint32_t requester_capabilities_flag,
2508 : : uint32_t responder_capabilities_flag)
2509 : : {
2510 : : uint32_t negotiated_requester_capabilities_flag;
2511 : : uint32_t negotiated_responder_capabilities_flag;
2512 : :
2513 [ + + ]: 30854 : if (is_requester) {
2514 : 19254 : negotiated_requester_capabilities_flag = spdm_context->local_context.capability.flags;
2515 : 19254 : negotiated_responder_capabilities_flag = spdm_context->connection_info.capability.flags;
2516 : : } else {
2517 : 11600 : negotiated_requester_capabilities_flag = spdm_context->connection_info.capability.flags;
2518 : 11600 : negotiated_responder_capabilities_flag = spdm_context->local_context.capability.flags;
2519 : : }
2520 : :
2521 [ + + ]: 30854 : if (((requester_capabilities_flag == 0) ||
2522 [ + + ]: 21660 : ((negotiated_requester_capabilities_flag &
2523 [ + + ]: 15830 : requester_capabilities_flag) != 0)) &&
2524 : 12468 : ((responder_capabilities_flag == 0) ||
2525 [ + + ]: 12468 : ((negotiated_responder_capabilities_flag &
2526 : : responder_capabilities_flag) != 0))) {
2527 : 13030 : return true;
2528 : : } else {
2529 : 17824 : return false;
2530 : : }
2531 : : }
2532 : :
2533 : 8 : bool libspdm_is_capabilities_ext_flag_supported(const libspdm_context_t *spdm_context,
2534 : : bool is_requester,
2535 : : uint16_t requester_capabilities_ext_flag,
2536 : : uint16_t responder_capabilities_ext_flag)
2537 : : {
2538 : : uint16_t negotiated_requester_capabilities_ext_flag;
2539 : : uint16_t negotiated_responder_capabilities_ext_flag;
2540 : :
2541 [ + + ]: 8 : if (is_requester) {
2542 : 4 : negotiated_requester_capabilities_ext_flag = spdm_context->local_context.capability.ext_flags;
2543 : 4 : negotiated_responder_capabilities_ext_flag = spdm_context->connection_info.capability.ext_flags;
2544 : : } else {
2545 : 4 : negotiated_requester_capabilities_ext_flag = spdm_context->connection_info.capability.ext_flags;
2546 : 4 : negotiated_responder_capabilities_ext_flag = spdm_context->local_context.capability.ext_flags;
2547 : : }
2548 : :
2549 [ + + ]: 8 : if (((requester_capabilities_ext_flag == 0) ||
2550 : : ((negotiated_requester_capabilities_ext_flag &
2551 [ + + + - ]: 8 : requester_capabilities_ext_flag) != 0)) &&
2552 : 6 : ((responder_capabilities_ext_flag == 0) ||
2553 : : ((negotiated_responder_capabilities_ext_flag &
2554 [ + + ]: 6 : responder_capabilities_ext_flag) != 0))) {
2555 : 4 : return true;
2556 : : } else {
2557 : 4 : return false;
2558 : : }
2559 : : }
2560 : :
2561 : 314 : bool libspdm_is_encap_supported(const libspdm_context_t *spdm_context)
2562 : : {
2563 [ + + ]: 314 : if (libspdm_get_connection_version(spdm_context) == SPDM_MESSAGE_VERSION_10) {
2564 : 2 : return false;
2565 [ + + ]: 312 : } else if (libspdm_get_connection_version(spdm_context) == SPDM_MESSAGE_VERSION_12) {
2566 : : /* ENCAP_CAP was erroneously deprecated in SPDM 1.2.0 and 1.2.1, and MUT_AUTH_CAP
2567 : : * was used in its place. In SPDM 1.2.2 and later ENCAP_CAP is undeprecated. Since
2568 : : * UpdateVersionNumber must be ignored when checking interoperability libspdm will check
2569 : : * if ENCAP_CAP or MUT_AUTH_CAP is set. */
2570 : 48 : const bool is_req_encap_cap_supported = libspdm_is_capabilities_flag_supported(
2571 : 48 : spdm_context, spdm_context->local_context.is_requester,
2572 : : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCAP_CAP, 0);
2573 : 48 : const bool is_req_mut_auth_cap_supported = libspdm_is_capabilities_flag_supported(
2574 : 48 : spdm_context, spdm_context->local_context.is_requester,
2575 : : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MUT_AUTH_CAP, 0);
2576 : 48 : const bool is_rsp_encap_cap_supported = libspdm_is_capabilities_flag_supported(
2577 : 48 : spdm_context, spdm_context->local_context.is_requester,
2578 : : 0, SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCAP_CAP);
2579 : 48 : const bool is_rsp_mut_auth_cap_supported = libspdm_is_capabilities_flag_supported(
2580 : 48 : spdm_context, spdm_context->local_context.is_requester,
2581 : : 0, SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MUT_AUTH_CAP);
2582 : :
2583 [ + + + - : 50 : return ((is_req_encap_cap_supported || is_req_mut_auth_cap_supported) &&
+ + ]
2584 [ + - ]: 2 : (is_rsp_encap_cap_supported || is_rsp_mut_auth_cap_supported));
2585 : : } else {
2586 : : /* For SPDM 1.1 and 1.3 and later only check ENCAP_CAP. */
2587 : 264 : return libspdm_is_capabilities_flag_supported(
2588 : 264 : spdm_context, spdm_context->local_context.is_requester,
2589 : : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCAP_CAP,
2590 : : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCAP_CAP);
2591 : : }
2592 : : }
2593 : :
2594 : 1090 : void libspdm_register_device_io_func(
2595 : : void *spdm_context, libspdm_device_send_message_func send_message,
2596 : : libspdm_device_receive_message_func receive_message)
2597 : : {
2598 : : libspdm_context_t *context;
2599 : :
2600 : 1090 : context = spdm_context;
2601 : 1090 : context->send_message = send_message;
2602 : 1090 : context->receive_message = receive_message;
2603 : 1090 : }
2604 : :
2605 : 1096 : void libspdm_register_device_buffer_func(
2606 : : void *spdm_context,
2607 : : uint32_t sender_buffer_size,
2608 : : uint32_t receiver_buffer_size,
2609 : : libspdm_device_acquire_sender_buffer_func acquire_sender_buffer,
2610 : : libspdm_device_release_sender_buffer_func release_sender_buffer,
2611 : : libspdm_device_acquire_receiver_buffer_func acquire_receiver_buffer,
2612 : : libspdm_device_release_receiver_buffer_func release_receiver_buffer)
2613 : : {
2614 : : libspdm_context_t *context;
2615 : :
2616 : 1096 : context = spdm_context;
2617 : 1096 : context->sender_buffer_size = sender_buffer_size;
2618 : 1096 : context->receiver_buffer_size = receiver_buffer_size;
2619 : 1096 : context->acquire_sender_buffer = acquire_sender_buffer;
2620 : 1096 : context->release_sender_buffer = release_sender_buffer;
2621 : 1096 : context->acquire_receiver_buffer = acquire_receiver_buffer;
2622 : 1096 : context->release_receiver_buffer = release_receiver_buffer;
2623 : :
2624 : 1096 : LIBSPDM_ASSERT (sender_buffer_size >=
2625 : : context->local_context.capability.transport_header_size +
2626 : : context->local_context.capability.transport_tail_size);
2627 : 1096 : sender_buffer_size -= (context->local_context.capability.transport_header_size +
2628 : 1096 : context->local_context.capability.transport_tail_size);
2629 : 1096 : LIBSPDM_ASSERT (sender_buffer_size >= SPDM_MIN_DATA_TRANSFER_SIZE_VERSION_12);
2630 : 1096 : context->local_context.capability.sender_data_transfer_size = sender_buffer_size;
2631 : :
2632 : 1096 : LIBSPDM_ASSERT(receiver_buffer_size >=
2633 : : context->local_context.capability.transport_header_size +
2634 : : context->local_context.capability.transport_tail_size);
2635 : 1096 : receiver_buffer_size -= (context->local_context.capability.transport_header_size +
2636 : 1096 : context->local_context.capability.transport_tail_size);
2637 : 1096 : LIBSPDM_ASSERT (receiver_buffer_size >= SPDM_MIN_DATA_TRANSFER_SIZE_VERSION_12);
2638 : 1096 : context->local_context.capability.data_transfer_size = receiver_buffer_size;
2639 : 1096 : }
2640 : :
2641 : 1098 : void libspdm_register_transport_layer_func(
2642 : : void *spdm_context,
2643 : : uint32_t max_spdm_msg_size,
2644 : : uint32_t transport_header_size,
2645 : : uint32_t transport_tail_size,
2646 : : libspdm_transport_encode_message_func transport_encode_message,
2647 : : libspdm_transport_decode_message_func transport_decode_message)
2648 : : {
2649 : : libspdm_context_t *context;
2650 : :
2651 : 1098 : context = spdm_context;
2652 : :
2653 : : /* fix the data_transfer_size if it is set before */
2654 [ + + ]: 1098 : if ((context->local_context.capability.data_transfer_size != 0) &&
2655 : 4 : (context->local_context.capability.data_transfer_size ==
2656 [ + + ]: 4 : context->receiver_buffer_size)) {
2657 : 2 : context->local_context.capability.data_transfer_size =
2658 : 2 : (uint32_t)(context->receiver_buffer_size -
2659 : 2 : (transport_header_size + transport_tail_size));
2660 : : }
2661 [ + + ]: 1098 : if ((context->local_context.capability.sender_data_transfer_size != 0) &&
2662 : 4 : (context->local_context.capability.sender_data_transfer_size ==
2663 [ + + ]: 4 : context->sender_buffer_size)) {
2664 : 2 : context->local_context.capability.sender_data_transfer_size =
2665 : 2 : (uint32_t)(context->sender_buffer_size -
2666 : 2 : (transport_header_size + transport_tail_size));
2667 : : }
2668 : :
2669 : 1098 : context->local_context.capability.max_spdm_msg_size = max_spdm_msg_size;
2670 : 1098 : context->local_context.capability.transport_header_size = transport_header_size;
2671 : 1098 : context->local_context.capability.transport_tail_size = transport_tail_size;
2672 : 1098 : context->transport_encode_message = transport_encode_message;
2673 : 1098 : context->transport_decode_message = transport_decode_message;
2674 : 1098 : }
2675 : :
2676 : 12 : void libspdm_register_verify_spdm_cert_chain_func(
2677 : : void *spdm_context,
2678 : : const libspdm_verify_spdm_cert_chain_func verify_spdm_cert_chain)
2679 : : {
2680 : : libspdm_context_t *context;
2681 : :
2682 : 12 : context = spdm_context;
2683 : 12 : context->local_context.verify_peer_spdm_cert_chain = verify_spdm_cert_chain;
2684 : 12 : }
2685 : :
2686 : 1100 : size_t libspdm_get_sizeof_required_scratch_buffer (void *spdm_context)
2687 : : {
2688 : : libspdm_context_t *context;
2689 : : size_t scratch_buffer_size;
2690 : :
2691 : 1100 : context = spdm_context;
2692 : 1100 : LIBSPDM_ASSERT (context->local_context.capability.max_spdm_msg_size != 0);
2693 : :
2694 : 1100 : scratch_buffer_size = libspdm_get_scratch_buffer_capacity(context);
2695 : 1100 : return scratch_buffer_size;
2696 : : }
2697 : :
2698 : 1102 : void libspdm_set_scratch_buffer (
2699 : : void *spdm_context,
2700 : : void *scratch_buffer,
2701 : : size_t scratch_buffer_size)
2702 : : {
2703 : : libspdm_context_t *context;
2704 : :
2705 : 1102 : context = spdm_context;
2706 : 1102 : LIBSPDM_ASSERT (context->local_context.capability.max_spdm_msg_size != 0);
2707 : 1102 : LIBSPDM_ASSERT (scratch_buffer_size >= libspdm_get_scratch_buffer_capacity(spdm_context));
2708 : 1102 : context->scratch_buffer = scratch_buffer;
2709 : 1102 : context->scratch_buffer_size = scratch_buffer_size;
2710 : 1102 : context->last_spdm_request = (uint8_t *)scratch_buffer +
2711 : 1102 : libspdm_get_scratch_buffer_last_spdm_request_offset(spdm_context);
2712 : : #if LIBSPDM_RESPOND_IF_READY_SUPPORT
2713 : 1102 : context->cache_spdm_request = (uint8_t *)scratch_buffer +
2714 : 1102 : libspdm_get_scratch_buffer_cache_spdm_request_offset(spdm_context);
2715 : : #endif
2716 : 1102 : }
2717 : :
2718 : 13328 : void libspdm_get_scratch_buffer (
2719 : : void *spdm_context,
2720 : : void **scratch_buffer,
2721 : : size_t *scratch_buffer_size)
2722 : : {
2723 : : libspdm_context_t *context;
2724 : :
2725 : 13328 : context = spdm_context;
2726 : 13328 : LIBSPDM_ASSERT (context->scratch_buffer != NULL);
2727 : 13328 : LIBSPDM_ASSERT (context->scratch_buffer_size >=
2728 : : libspdm_get_scratch_buffer_capacity(spdm_context));
2729 : 13328 : *scratch_buffer = context->scratch_buffer;
2730 : : /* The sections for libspdm's internal state start at a fixed offset, so the returned space ends
2731 : : * there even if the scratch buffer is larger than required. */
2732 : 13328 : *scratch_buffer_size = libspdm_get_scratch_buffer_last_spdm_request_offset(spdm_context);
2733 : 13328 : }
2734 : :
2735 : 6192 : libspdm_return_t libspdm_acquire_sender_buffer (
2736 : : libspdm_context_t *spdm_context, size_t *max_msg_size, void **msg_buf_ptr)
2737 : : {
2738 : : libspdm_return_t status;
2739 : :
2740 : 6192 : LIBSPDM_ASSERT (spdm_context->sender_buffer == NULL);
2741 : 6192 : LIBSPDM_ASSERT (spdm_context->sender_buffer_size != 0);
2742 : 6192 : status = spdm_context->acquire_sender_buffer (spdm_context, msg_buf_ptr);
2743 [ + + ]: 6192 : if (status != LIBSPDM_STATUS_SUCCESS) {
2744 : 34 : return status;
2745 : : }
2746 : 6158 : spdm_context->sender_buffer = *msg_buf_ptr;
2747 : 6158 : *max_msg_size = spdm_context->sender_buffer_size;
2748 : : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
2749 : : /* it return scratch buffer, because the requester need build message there.*/
2750 : 12316 : *msg_buf_ptr = (uint8_t *)spdm_context->scratch_buffer +
2751 : 6158 : libspdm_get_scratch_buffer_large_sender_receiver_offset(spdm_context);
2752 : 6158 : *max_msg_size = libspdm_get_scratch_buffer_large_sender_receiver_capacity(spdm_context);
2753 : : #endif
2754 : 6158 : return LIBSPDM_STATUS_SUCCESS;
2755 : : }
2756 : :
2757 : 6158 : void libspdm_release_sender_buffer (libspdm_context_t *spdm_context)
2758 : : {
2759 : 6158 : LIBSPDM_ASSERT(spdm_context->sender_buffer != NULL);
2760 : 6158 : LIBSPDM_ASSERT(spdm_context->sender_buffer_size != 0);
2761 : :
2762 : 6158 : spdm_context->release_sender_buffer (spdm_context, spdm_context->sender_buffer);
2763 : 6158 : spdm_context->sender_buffer = NULL;
2764 : 6158 : }
2765 : :
2766 : 6252 : void libspdm_get_sender_buffer (
2767 : : libspdm_context_t *spdm_context,
2768 : : void **sender_buffer,
2769 : : size_t *sender_buffer_size)
2770 : : {
2771 : 6252 : *sender_buffer = spdm_context->sender_buffer;
2772 : 6252 : *sender_buffer_size = spdm_context->sender_buffer_size;
2773 : 6252 : }
2774 : :
2775 : 6012 : libspdm_return_t libspdm_acquire_receiver_buffer (
2776 : : libspdm_context_t *spdm_context, size_t *max_msg_size, void **msg_buf_ptr)
2777 : : {
2778 : : libspdm_return_t status;
2779 : :
2780 : 6012 : LIBSPDM_ASSERT (spdm_context->receiver_buffer == NULL);
2781 : 6012 : LIBSPDM_ASSERT (spdm_context->receiver_buffer_size != 0);
2782 : 6012 : status = spdm_context->acquire_receiver_buffer (spdm_context, msg_buf_ptr);
2783 [ + + ]: 6012 : if (status != LIBSPDM_STATUS_SUCCESS) {
2784 : 34 : return status;
2785 : : }
2786 : 5978 : spdm_context->receiver_buffer = *msg_buf_ptr;
2787 : 5978 : *max_msg_size = spdm_context->receiver_buffer_size;
2788 : : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
2789 : : /* it return scratch buffer, because the requester need build message there.*/
2790 : 11956 : *msg_buf_ptr = (uint8_t *)spdm_context->scratch_buffer +
2791 : 5978 : libspdm_get_scratch_buffer_large_sender_receiver_offset(spdm_context);
2792 : 5978 : *max_msg_size = libspdm_get_scratch_buffer_large_sender_receiver_capacity(spdm_context);
2793 : : #endif
2794 : 5978 : return LIBSPDM_STATUS_SUCCESS;
2795 : : }
2796 : :
2797 : 5978 : void libspdm_release_receiver_buffer (libspdm_context_t *spdm_context)
2798 : : {
2799 : 5978 : LIBSPDM_ASSERT(spdm_context->receiver_buffer != NULL);
2800 : 5978 : LIBSPDM_ASSERT(spdm_context->receiver_buffer_size != 0);
2801 : :
2802 : 5978 : spdm_context->release_receiver_buffer (spdm_context, spdm_context->receiver_buffer);
2803 : 5978 : spdm_context->receiver_buffer = NULL;
2804 : 5978 : }
2805 : :
2806 : 8 : void libspdm_get_receiver_buffer (
2807 : : libspdm_context_t *spdm_context,
2808 : : void **receiver_buffer,
2809 : : size_t *receiver_buffer_size)
2810 : : {
2811 : 8 : *receiver_buffer = spdm_context->receiver_buffer;
2812 : 8 : *receiver_buffer_size = spdm_context->receiver_buffer_size;
2813 : 8 : }
2814 : :
2815 : 32 : void libspdm_get_last_spdm_error_struct(void *spdm_context, libspdm_error_struct_t *last_spdm_error)
2816 : : {
2817 : : libspdm_context_t *context;
2818 : :
2819 : 32 : context = spdm_context;
2820 : 32 : libspdm_copy_mem(last_spdm_error, sizeof(libspdm_error_struct_t),
2821 : 32 : &context->last_spdm_error,sizeof(libspdm_error_struct_t));
2822 : 32 : }
2823 : :
2824 : 6672 : void libspdm_set_last_spdm_error_struct(void *spdm_context, libspdm_error_struct_t *last_spdm_error)
2825 : : {
2826 : : libspdm_context_t *context;
2827 : :
2828 : 6672 : context = spdm_context;
2829 : 6672 : libspdm_copy_mem(&context->last_spdm_error, sizeof(context->last_spdm_error),
2830 : : last_spdm_error, sizeof(libspdm_error_struct_t));
2831 : 6672 : }
2832 : :
2833 : : #if LIBSPDM_FIPS_MODE
2834 : 2 : libspdm_return_t libspdm_init_fips_selftest_context(void *fips_selftest_context,
2835 : : size_t buffer_size,
2836 : : void *buffer)
2837 : : {
2838 : : libspdm_fips_selftest_context_t *context;
2839 : 2 : LIBSPDM_ASSERT(fips_selftest_context != NULL);
2840 : 1 : LIBSPDM_ASSERT(buffer_size >= LIBSPDM_FIPS_REQUIRED_BUFFER_SIZE);
2841 [ + + ]: 2 : if (buffer_size > 0) {
2842 : 1 : LIBSPDM_ASSERT(buffer != NULL);
2843 : : }
2844 : :
2845 : 2 : context = fips_selftest_context;
2846 : :
2847 : : /*Not tested for every used algo*/
2848 : 2 : context->tested_algo = 0;
2849 : : /*self_test result is false for every used algo*/
2850 : 2 : context->self_test_result = 0;
2851 : : /*The buffer provided by integrator to hold large intermediate results*/
2852 : 2 : context->selftest_buffer_size = buffer_size;
2853 : 2 : context->selftest_buffer = buffer;
2854 : :
2855 : 2 : return LIBSPDM_STATUS_SUCCESS;
2856 : : }
2857 : :
2858 : 4 : size_t libspdm_get_fips_selftest_context_size(void)
2859 : : {
2860 : : size_t size;
2861 : :
2862 : 4 : size = sizeof(libspdm_fips_selftest_context_t);
2863 : 4 : return size;
2864 : : }
2865 : :
2866 : 4 : size_t libspdm_get_fips_selftest_buffer_size(void)
2867 : : {
2868 : 4 : return LIBSPDM_FIPS_REQUIRED_BUFFER_SIZE;
2869 : : }
2870 : :
2871 : 8 : bool libspdm_import_fips_selftest_context_to_spdm_context(void *spdm_context,
2872 : : void *fips_selftest_context,
2873 : : size_t fips_selftest_context_size)
2874 : : {
2875 : : libspdm_fips_selftest_context_t *libspdm_fips_selftest_context;
2876 : : libspdm_context_t *libspdm_context;
2877 : :
2878 : 8 : libspdm_context = spdm_context;
2879 : 8 : libspdm_fips_selftest_context = fips_selftest_context;
2880 : :
2881 [ + + + + ]: 8 : if ((libspdm_context == NULL) || (libspdm_fips_selftest_context == NULL)) {
2882 : 4 : return false;
2883 : : }
2884 [ + + ]: 4 : if (fips_selftest_context_size != sizeof(libspdm_fips_selftest_context_t)) {
2885 : 2 : return false;
2886 : : }
2887 : :
2888 : 2 : libspdm_copy_mem(&(libspdm_context->fips_selftest_context),
2889 : : sizeof(libspdm_fips_selftest_context_t),
2890 : : libspdm_fips_selftest_context, sizeof(libspdm_fips_selftest_context_t));
2891 : 2 : return true;
2892 : : }
2893 : :
2894 : 8 : bool libspdm_export_fips_selftest_context_from_spdm_context(void *spdm_context,
2895 : : void *fips_selftest_context,
2896 : : size_t fips_selftest_context_size)
2897 : : {
2898 : : libspdm_fips_selftest_context_t *libspdm_fips_selftest_context;
2899 : : libspdm_context_t *libspdm_context;
2900 : :
2901 : 8 : libspdm_context = spdm_context;
2902 : 8 : libspdm_fips_selftest_context = fips_selftest_context;
2903 : :
2904 [ + + + + ]: 8 : if ((libspdm_context == NULL) || (libspdm_fips_selftest_context == NULL)) {
2905 : 4 : return false;
2906 : : }
2907 [ + + ]: 4 : if (fips_selftest_context_size != sizeof(libspdm_fips_selftest_context_t)) {
2908 : 2 : return false;
2909 : : }
2910 : :
2911 : 2 : libspdm_copy_mem(libspdm_fips_selftest_context,
2912 : : sizeof(libspdm_fips_selftest_context_t),
2913 : 2 : &(libspdm_context->fips_selftest_context),
2914 : : sizeof(libspdm_fips_selftest_context_t));
2915 : 2 : return true;
2916 : : }
2917 : :
2918 : : #endif /* LIBSPDM_FIPS_MODE */
2919 : :
2920 : 1112 : libspdm_return_t libspdm_init_context_with_secured_context(void *spdm_context,
2921 : : void **secured_contexts,
2922 : : size_t num_secured_contexts)
2923 : : {
2924 : : libspdm_context_t *context;
2925 : : size_t index;
2926 : :
2927 : 1112 : LIBSPDM_ASSERT(spdm_context != NULL);
2928 : 1112 : LIBSPDM_ASSERT(secured_contexts != NULL);
2929 : 1112 : LIBSPDM_ASSERT(num_secured_contexts == LIBSPDM_MAX_SESSION_COUNT);
2930 : :
2931 : 1112 : context = spdm_context;
2932 : 1112 : libspdm_zero_mem(context, sizeof(libspdm_context_t));
2933 : 1112 : context->version = LIBSPDM_CONTEXT_STRUCT_VERSION;
2934 : 1112 : context->transcript.message_a.max_buffer_size =
2935 : : sizeof(context->transcript.message_a.buffer);
2936 : 1112 : context->transcript.message_d.max_buffer_size =
2937 : : sizeof(context->transcript.message_d.buffer);
2938 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
2939 : : context->transcript.message_b.max_buffer_size =
2940 : : sizeof(context->transcript.message_b.buffer);
2941 : : context->transcript.message_c.max_buffer_size =
2942 : : sizeof(context->transcript.message_c.buffer);
2943 : : context->transcript.message_mut_b.max_buffer_size =
2944 : : sizeof(context->transcript.message_mut_b.buffer);
2945 : : context->transcript.message_mut_c.max_buffer_size =
2946 : : sizeof(context->transcript.message_mut_c.buffer);
2947 : : context->transcript.message_m.max_buffer_size =
2948 : : sizeof(context->transcript.message_m.buffer);
2949 : : context->transcript.message_e.max_buffer_size =
2950 : : sizeof(context->transcript.message_e.buffer);
2951 : : context->transcript.message_encap_e.max_buffer_size =
2952 : : sizeof(context->transcript.message_encap_e.buffer);
2953 : : #endif
2954 : 1112 : context->response_state = LIBSPDM_RESPONSE_STATE_NORMAL;
2955 : 1112 : context->local_context.version.spdm_version_count = SPDM_MAX_VERSION_COUNT;
2956 : 1112 : context->local_context.version.spdm_version[0] = SPDM_MESSAGE_VERSION_10 <<
2957 : : SPDM_VERSION_NUMBER_SHIFT_BIT;
2958 : 1112 : context->local_context.version.spdm_version[1] = SPDM_MESSAGE_VERSION_11 <<
2959 : : SPDM_VERSION_NUMBER_SHIFT_BIT;
2960 : 1112 : context->local_context.version.spdm_version[2] = SPDM_MESSAGE_VERSION_12 <<
2961 : : SPDM_VERSION_NUMBER_SHIFT_BIT;
2962 : 1112 : context->local_context.version.spdm_version[3] = SPDM_MESSAGE_VERSION_13 <<
2963 : : SPDM_VERSION_NUMBER_SHIFT_BIT;
2964 : 1112 : context->local_context.version.spdm_version[4] = SPDM_MESSAGE_VERSION_14 <<
2965 : : SPDM_VERSION_NUMBER_SHIFT_BIT;
2966 : 1112 : context->local_context.secured_message_version.secured_message_version_count =
2967 : : SECURED_SPDM_MAX_VERSION_COUNT;
2968 : 1112 : context->local_context.secured_message_version.secured_message_version[0] =
2969 : : SECURED_SPDM_VERSION_10 << SPDM_VERSION_NUMBER_SHIFT_BIT;
2970 : 1112 : context->local_context.secured_message_version.secured_message_version[1] =
2971 : : SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT;
2972 : 1112 : context->local_context.secured_message_version.secured_message_version[2] =
2973 : : SECURED_SPDM_VERSION_12 << SPDM_VERSION_NUMBER_SHIFT_BIT;
2974 : 1112 : context->local_context.secured_message_version.secured_message_version[3] =
2975 : : SECURED_SPDM_VERSION_13 << SPDM_VERSION_NUMBER_SHIFT_BIT;
2976 : 1112 : context->local_context.capability.st1 = SPDM_ST1_VALUE_US;
2977 : :
2978 : : /* DSP0277 1.3 AEAD limit: max_spdm_session_sequence_number is the single source of truth for
2979 : : * the AEAD limit. The default 0xFFFFFFFFFFFFFFFF encodes the spec-default exponent of 64. */
2980 : 1112 : context->max_spdm_session_sequence_number = LIBSPDM_MAX_SPDM_SESSION_SEQUENCE_NUMBER;
2981 : :
2982 : 1112 : context->latest_session_id = INVALID_SESSION_ID;
2983 : 1112 : context->last_spdm_request_session_id = INVALID_SESSION_ID;
2984 : 1112 : context->last_spdm_request_session_id_valid = false;
2985 : 1112 : context->last_spdm_request_size = 0;
2986 : :
2987 : : /* To be updated in libspdm_register_device_buffer_func */
2988 : 1112 : context->local_context.capability.data_transfer_size = 0;
2989 : 1112 : context->local_context.capability.sender_data_transfer_size = 0;
2990 : 1112 : context->local_context.capability.max_spdm_msg_size = 0;
2991 : :
2992 [ + + ]: 5558 : for (index = 0; index < num_secured_contexts; index++) {
2993 [ + + ]: 4448 : if (secured_contexts[index] == NULL) {
2994 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
2995 : : }
2996 : :
2997 : 4446 : context->session_info[index].secured_message_context = secured_contexts[index];
2998 : 4446 : libspdm_secured_message_init_context(
2999 : : context->session_info[index].secured_message_context);
3000 : : }
3001 : :
3002 : 1110 : return LIBSPDM_STATUS_SUCCESS;
3003 : : }
3004 : :
3005 : 1108 : libspdm_return_t libspdm_init_context(void *spdm_context)
3006 : : {
3007 : : libspdm_context_t *context;
3008 : : void *secured_context;
3009 : : void *secured_contexts[LIBSPDM_MAX_SESSION_COUNT];
3010 : : size_t secured_context_size;
3011 : : size_t index;
3012 : :
3013 : 1108 : LIBSPDM_ASSERT(spdm_context != NULL);
3014 : :
3015 : : /* libspdm_get_context_size() allocates space for all secured message
3016 : : * contexts. They are appended to the general SPDM context. */
3017 : 1108 : context = spdm_context;
3018 : 1108 : secured_context = (void *)((size_t)(context + 1));
3019 : 1108 : secured_context_size = libspdm_secured_message_get_context_size();
3020 : :
3021 [ + + ]: 5540 : for (index = 0; index < LIBSPDM_MAX_SESSION_COUNT; index++)
3022 : : {
3023 : 4432 : secured_contexts[index] = (uint8_t *)secured_context + secured_context_size * index;
3024 : : }
3025 : :
3026 : 1108 : return libspdm_init_context_with_secured_context(spdm_context,
3027 : : secured_contexts,
3028 : : LIBSPDM_MAX_SESSION_COUNT);
3029 : : }
3030 : :
3031 : : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
3032 : 424 : void libspdm_reset_chunk_info(libspdm_chunk_info_t *chunk_info)
3033 : : {
3034 [ + + ]: 424 : if (chunk_info->large_message != NULL) {
3035 : 12 : libspdm_zero_mem(chunk_info->large_message, chunk_info->large_message_capacity);
3036 : : }
3037 : 424 : chunk_info->chunk_in_use = false;
3038 : 424 : chunk_info->session_id_valid = false;
3039 : 424 : chunk_info->session_id = 0;
3040 : 424 : chunk_info->chunk_seq_no = 0;
3041 : 424 : chunk_info->chunk_bytes_transferred = 0;
3042 : 424 : chunk_info->large_message = NULL;
3043 : 424 : chunk_info->large_message_size = 0;
3044 : 424 : chunk_info->large_message_capacity = 0;
3045 : 424 : }
3046 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP */
3047 : :
3048 : 210 : void libspdm_reset_context(void *spdm_context)
3049 : : {
3050 : : libspdm_context_t *context;
3051 : : size_t index;
3052 : : #if !(LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT)
3053 : : uint8_t slot_index;
3054 : : #endif
3055 : :
3056 : 210 : context = spdm_context;
3057 : :
3058 : : /* Clear all information about previous connection. Local context information is preserved. */
3059 : :
3060 : : /* Need to clear session information and message transcripts before negotiated algorithm
3061 : : * information is cleared. */
3062 [ + + ]: 1050 : for (index = 0; index < LIBSPDM_MAX_SESSION_COUNT; index++)
3063 : : {
3064 : 840 : libspdm_session_info_init(context,
3065 : : &context->session_info[index],
3066 : : INVALID_SESSION_ID,
3067 : : 0,
3068 : : false);
3069 : : }
3070 : :
3071 : 210 : libspdm_reset_message_a(spdm_context);
3072 : 210 : libspdm_reset_message_d(spdm_context);
3073 : 210 : libspdm_reset_message_b(spdm_context);
3074 : 210 : libspdm_reset_message_c(spdm_context);
3075 : 210 : libspdm_reset_message_mut_b(spdm_context);
3076 : 210 : libspdm_reset_message_mut_c(spdm_context);
3077 : 210 : libspdm_reset_message_m(spdm_context, NULL);
3078 : 210 : libspdm_reset_message_e(spdm_context, NULL);
3079 : 210 : libspdm_reset_message_encap_e(spdm_context, NULL);
3080 : :
3081 : : #if !(LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT)
3082 : : /* Free the peer leaf certificate public key contexts while the negotiated
3083 : : * algorithm they were parsed under is still known; that algorithm is cleared
3084 : : * below, and these contexts are otherwise only released in
3085 : : * libspdm_deinit_context, so they leak across every re-connection. */
3086 [ + + ]: 1890 : for (slot_index = 0; slot_index < SPDM_MAX_SLOT_COUNT; slot_index++) {
3087 : 1680 : libspdm_free_peer_leaf_cert_public_key(context, slot_index);
3088 : : }
3089 : 210 : libspdm_zero_mem(context->connection_info.peer_used_cert_chain,
3090 : : sizeof(context->connection_info.peer_used_cert_chain));
3091 : : #endif
3092 : :
3093 : 210 : context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NOT_STARTED;
3094 : 210 : libspdm_zero_mem(&context->connection_info.version, sizeof(spdm_version_number_t));
3095 : 210 : libspdm_zero_mem(&context->connection_info.capability,
3096 : : sizeof(libspdm_device_capability_t));
3097 : 210 : libspdm_zero_mem(&context->connection_info.algorithm, sizeof(libspdm_device_algorithm_t));
3098 : 210 : libspdm_zero_mem(&context->last_spdm_error, sizeof(libspdm_error_struct_t));
3099 : : #if LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP
3100 : 210 : libspdm_zero_mem(&context->encap_context, sizeof(libspdm_encap_context_t));
3101 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP */
3102 : : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
3103 : 210 : libspdm_reset_chunk_info(&context->chunk_context.get);
3104 : 210 : libspdm_reset_chunk_info(&context->chunk_context.send);
3105 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP */
3106 : 210 : context->connection_info.multi_key_conn_req = false;
3107 : 210 : context->connection_info.multi_key_conn_rsp = false;
3108 : 210 : context->connection_info.peer_provisioned_slot_mask = 0;
3109 : 210 : context->connection_info.peer_supported_slot_mask = 0;
3110 : 210 : libspdm_zero_mem(context->connection_info.peer_key_pair_id,
3111 : : sizeof(context->connection_info.peer_key_pair_id));
3112 : 210 : libspdm_zero_mem(context->connection_info.peer_cert_info,
3113 : : sizeof(context->connection_info.peer_cert_info));
3114 : 210 : libspdm_zero_mem(context->connection_info.peer_key_usage_bit_mask,
3115 : : sizeof(context->connection_info.peer_key_usage_bit_mask));
3116 : : #if LIBSPDM_RESPOND_IF_READY_SUPPORT
3117 : 210 : context->cache_spdm_request_size = 0;
3118 : 210 : context->cache_spdm_request_session_id_valid = false;
3119 : 210 : context->cache_spdm_request_session_id = INVALID_SESSION_ID;
3120 : : /* The cached request is plaintext even when the original request arrived in
3121 : : * a session, so erase it rather than leave it in the scratch buffer for the
3122 : : * next connection. */
3123 [ + - ]: 210 : if (context->cache_spdm_request != NULL) {
3124 : 210 : libspdm_zero_mem(context->cache_spdm_request,
3125 : 210 : libspdm_get_scratch_buffer_cache_spdm_request_capacity(context));
3126 : : }
3127 : : #endif
3128 : 210 : libspdm_zero_mem(&context->error_data, sizeof(context->error_data));
3129 : 210 : context->response_state = LIBSPDM_RESPONSE_STATE_NORMAL;
3130 : 210 : context->current_token = 0;
3131 : 210 : context->latest_session_id = INVALID_SESSION_ID;
3132 : 210 : context->last_spdm_request_session_id = INVALID_SESSION_ID;
3133 : 210 : context->last_spdm_request_session_id_valid = false;
3134 : 210 : context->last_spdm_request_size = 0;
3135 : : #if LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP
3136 : 210 : context->encap_context.payload_buffer_size = 0;
3137 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP */
3138 : 210 : context->current_dhe_session_count = 0;
3139 : 210 : context->current_psk_session_count = 0;
3140 : 210 : context->spdm_10_11_verify_signature_endian =
3141 : 210 : context->spdm_10_11_verify_signature_endian_setting;
3142 : 210 : }
3143 : :
3144 : 1094 : void libspdm_deinit_context(void *spdm_context)
3145 : : {
3146 : : uint32_t session_id;
3147 : : libspdm_context_t *context;
3148 : : libspdm_session_info_t *session_info;
3149 : : #if !(LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT)
3150 : : uint8_t slot_index;
3151 : : #endif
3152 : :
3153 : 1094 : context = spdm_context;
3154 : :
3155 : : #if !(LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT)
3156 [ + + ]: 9846 : for (slot_index = 0; slot_index < SPDM_MAX_SLOT_COUNT; slot_index++) {
3157 : 8752 : libspdm_free_peer_leaf_cert_public_key(context, slot_index);
3158 : : }
3159 : : #endif
3160 : :
3161 : 1094 : libspdm_reset_message_a(context);
3162 : 1094 : libspdm_reset_message_d(context);
3163 : 1094 : libspdm_reset_message_b(context);
3164 : 1094 : libspdm_reset_message_c(context);
3165 : 1094 : libspdm_reset_message_mut_b(context);
3166 : 1094 : libspdm_reset_message_mut_c(context);
3167 : 1094 : libspdm_reset_message_m(spdm_context, NULL);
3168 : 1094 : libspdm_reset_message_e(spdm_context, NULL);
3169 : 1094 : libspdm_reset_message_encap_e(spdm_context, NULL);
3170 [ + + ]: 5470 : for (session_id = 0; session_id < LIBSPDM_MAX_SESSION_COUNT; session_id++) {
3171 : 4376 : session_info = &context->session_info[session_id];
3172 : 4376 : libspdm_reset_message_m(context, session_info);
3173 : 4376 : libspdm_reset_message_e(context, session_info);
3174 : 4376 : libspdm_reset_message_encap_e(context, session_info);
3175 : 4376 : libspdm_reset_message_encap_d(session_info);
3176 : 4376 : libspdm_reset_message_k(context, session_info);
3177 : 4376 : libspdm_reset_message_f(context, session_info);
3178 : : }
3179 : 1094 : }
3180 : :
3181 : 1108 : size_t libspdm_get_context_size(void)
3182 : : {
3183 : : size_t size;
3184 : :
3185 : 1108 : size = sizeof(libspdm_context_t) +
3186 : 1108 : libspdm_secured_message_get_context_size() * LIBSPDM_MAX_SESSION_COUNT;
3187 : 1108 : LIBSPDM_ASSERT (size == LIBSPDM_CONTEXT_SIZE_ALL);
3188 : 1108 : return size;
3189 : : }
3190 : :
3191 : 4 : size_t libspdm_get_context_size_without_secured_context(void)
3192 : : {
3193 : : size_t size;
3194 : :
3195 : 4 : size = sizeof(libspdm_context_t);
3196 : 4 : LIBSPDM_ASSERT (size == LIBSPDM_CONTEXT_SIZE_WITHOUT_SECURED_CONTEXT);
3197 : 4 : return size;
3198 : : }
3199 : :
3200 : 3088 : uint8_t libspdm_get_version_from_version_number(spdm_version_number_t ver)
3201 : : {
3202 : 3088 : return (uint8_t)(ver >> SPDM_VERSION_NUMBER_SHIFT_BIT);
3203 : : }
3204 : :
3205 : 290 : void libspdm_version_number_sort(spdm_version_number_t *ver_set, size_t ver_num)
3206 : : {
3207 : : size_t index;
3208 : : size_t index_sort;
3209 : : size_t index_max;
3210 : : spdm_version_number_t version;
3211 : :
3212 : : /* Selection sort */
3213 [ + + ]: 290 : if (ver_num > 1) {
3214 [ + + ]: 578 : for (index_sort = 0; index_sort < ver_num; index_sort++) {
3215 : 462 : index_max = index_sort;
3216 [ + + ]: 1206 : for (index = index_sort + 1; index < ver_num; index++) {
3217 : : /* if ver_set[index] higher than ver_set[index_max] */
3218 [ + + ]: 744 : if (ver_set[index] > ver_set[index_max]) {
3219 : 440 : index_max = index;
3220 : : }
3221 : : }
3222 : : /* swap ver_set[index_max] and ver_set[index_sort] */
3223 : 462 : version = ver_set[index_sort];
3224 : 462 : ver_set[index_sort] = ver_set[index_max];
3225 : 462 : ver_set[index_max] = version;
3226 : : }
3227 : : }
3228 : 290 : }
3229 : :
3230 : 148 : bool libspdm_negotiate_connection_version(spdm_version_number_t *common_version,
3231 : : spdm_version_number_t *req_ver_set,
3232 : : size_t req_ver_num,
3233 : : const spdm_version_number_t *res_ver_set,
3234 : : size_t res_ver_num)
3235 : : {
3236 : : spdm_version_number_t req_version_list[LIBSPDM_MAX_VERSION_COUNT];
3237 : : spdm_version_number_t res_version_list[LIBSPDM_MAX_VERSION_COUNT];
3238 : : size_t req_index;
3239 : : size_t res_index;
3240 : :
3241 [ + + - + ]: 148 : if (req_ver_num > LIBSPDM_MAX_VERSION_COUNT || res_ver_num > LIBSPDM_MAX_VERSION_COUNT) {
3242 : 2 : return false;
3243 : : }
3244 : :
3245 [ + - + - : 146 : if (req_ver_set == NULL || req_ver_num == 0 || res_ver_set == NULL || res_ver_num == 0) {
+ + - + ]
3246 : 2 : return false;
3247 : : }
3248 : :
3249 : 144 : libspdm_zero_mem(req_version_list, sizeof(spdm_version_number_t) * LIBSPDM_MAX_VERSION_COUNT);
3250 : 144 : libspdm_zero_mem(res_version_list, sizeof(spdm_version_number_t) * LIBSPDM_MAX_VERSION_COUNT);
3251 : :
3252 : 144 : libspdm_copy_mem(req_version_list, sizeof(spdm_version_number_t) * LIBSPDM_MAX_VERSION_COUNT,
3253 : : req_ver_set, sizeof(spdm_version_number_t) * req_ver_num);
3254 : 144 : libspdm_copy_mem(res_version_list, sizeof(spdm_version_number_t) * LIBSPDM_MAX_VERSION_COUNT,
3255 : : res_ver_set, sizeof(spdm_version_number_t) * res_ver_num);
3256 : :
3257 : : /* Sort SPDMversion in descending order. */
3258 : 144 : libspdm_version_number_sort(req_version_list, req_ver_num);
3259 : 144 : libspdm_version_number_sort(res_version_list, res_ver_num);
3260 : :
3261 : : /**
3262 : : * Find highest same version and make req_index point to it.
3263 : : * If not found, return false.
3264 : : **/
3265 [ + + ]: 164 : for (res_index = 0; res_index < res_ver_num; res_index++) {
3266 [ + + ]: 324 : for (req_index = 0; req_index < req_ver_num; req_index++) {
3267 [ + + ]: 608 : if (libspdm_get_version_from_version_number(req_version_list[req_index]) ==
3268 : 304 : libspdm_get_version_from_version_number(res_version_list[res_index])) {
3269 : 140 : *common_version = req_version_list[req_index];
3270 : 140 : return true;
3271 : : }
3272 : : }
3273 : : }
3274 : 4 : return false;
3275 : : }
3276 : :
3277 : : #if LIBSPDM_EVENT_RECIPIENT_SUPPORT
3278 : 40 : void libspdm_register_event_callback(void *context,
3279 : : libspdm_process_event_func process_event_func)
3280 : : {
3281 : : libspdm_context_t *spdm_context;
3282 : :
3283 : 40 : spdm_context = context;
3284 : 40 : spdm_context->process_event = process_event_func;
3285 : 40 : }
3286 : : #endif /* LIBSPDM_EVENT_RECIPIENT_SUPPORT */
|