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 : 6152 : uint32_t libspdm_get_scratch_buffer_secure_message_offset(void) {
14 : 6152 : return 0;
15 : : }
16 : :
17 : 80666 : uint32_t libspdm_get_scratch_buffer_secure_message_capacity(libspdm_context_t *spdm_context) {
18 : 80666 : return spdm_context->local_context.capability.max_spdm_msg_size +
19 : 161332 : spdm_context->local_context.capability.transport_header_size +
20 : 80666 : spdm_context->local_context.capability.transport_tail_size;
21 : : }
22 : :
23 : : /* second section */
24 : 166 : uint32_t libspdm_get_scratch_buffer_large_message_offset(libspdm_context_t *spdm_context) {
25 : 166 : return libspdm_get_scratch_buffer_secure_message_capacity(spdm_context);
26 : : }
27 : :
28 : 74436 : uint32_t libspdm_get_scratch_buffer_large_message_capacity(libspdm_context_t *spdm_context) {
29 : 74436 : return spdm_context->local_context.capability.max_spdm_msg_size;
30 : : }
31 : : #endif
32 : :
33 : : /* third section */
34 : 12816 : uint32_t libspdm_get_scratch_buffer_sender_receiver_offset(libspdm_context_t *spdm_context) {
35 : 12816 : return 0 +
36 : : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
37 : : libspdm_get_scratch_buffer_secure_message_capacity(spdm_context) +
38 : 12816 : libspdm_get_scratch_buffer_large_message_capacity(spdm_context) +
39 : : #endif
40 : : 0;
41 : : }
42 : :
43 : 68066 : uint32_t libspdm_get_scratch_buffer_sender_receiver_capacity(libspdm_context_t *spdm_context) {
44 : 68066 : return spdm_context->local_context.capability.max_spdm_msg_size +
45 : 136132 : spdm_context->local_context.capability.transport_header_size +
46 : 68066 : spdm_context->local_context.capability.transport_tail_size;
47 : : }
48 : :
49 : : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
50 : : /* fourth section */
51 : 30306 : uint32_t libspdm_get_scratch_buffer_large_sender_receiver_offset(libspdm_context_t *spdm_context) {
52 : 30306 : return libspdm_get_scratch_buffer_secure_message_capacity(spdm_context) +
53 : 60612 : libspdm_get_scratch_buffer_large_message_capacity(spdm_context) +
54 : 30306 : libspdm_get_scratch_buffer_sender_receiver_capacity(spdm_context);
55 : : }
56 : :
57 : 55470 : uint32_t libspdm_get_scratch_buffer_large_sender_receiver_capacity(libspdm_context_t *spdm_context)
58 : : {
59 : 55470 : return spdm_context->local_context.capability.max_spdm_msg_size +
60 : 110940 : spdm_context->local_context.capability.transport_header_size +
61 : 55470 : spdm_context->local_context.capability.transport_tail_size;
62 : : }
63 : : #endif
64 : :
65 : : /* fifth section */
66 : 14490 : uint32_t libspdm_get_scratch_buffer_last_spdm_request_offset(libspdm_context_t *spdm_context) {
67 : 14490 : return 0 +
68 : : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
69 : 14490 : libspdm_get_scratch_buffer_secure_message_capacity(spdm_context) +
70 : 14490 : libspdm_get_scratch_buffer_large_message_capacity(spdm_context) +
71 : : #endif
72 : 14490 : libspdm_get_scratch_buffer_sender_receiver_capacity(spdm_context) +
73 : : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
74 : 14490 : libspdm_get_scratch_buffer_large_sender_receiver_capacity(spdm_context) +
75 : : #endif
76 : : 0;
77 : : }
78 : :
79 : 23030 : uint32_t libspdm_get_scratch_buffer_last_spdm_request_capacity(libspdm_context_t *spdm_context) {
80 : 23030 : return spdm_context->local_context.capability.max_spdm_msg_size;
81 : : }
82 : :
83 : : #if LIBSPDM_RESPOND_IF_READY_SUPPORT
84 : : /* sixth section */
85 : 1118 : uint32_t libspdm_get_scratch_buffer_cache_spdm_request_offset(libspdm_context_t *spdm_context) {
86 : 1118 : return 0 +
87 : : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
88 : 1118 : libspdm_get_scratch_buffer_secure_message_capacity(spdm_context) +
89 : 1118 : libspdm_get_scratch_buffer_large_message_capacity(spdm_context) +
90 : : #endif
91 : 1118 : libspdm_get_scratch_buffer_sender_receiver_capacity(spdm_context) +
92 : : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
93 : 1118 : libspdm_get_scratch_buffer_large_sender_receiver_capacity(spdm_context) +
94 : : #endif
95 : 1118 : libspdm_get_scratch_buffer_last_spdm_request_capacity(spdm_context) +
96 : : 0;
97 : : }
98 : :
99 : 15882 : uint32_t libspdm_get_scratch_buffer_cache_spdm_request_capacity(libspdm_context_t *spdm_context) {
100 : 15882 : return spdm_context->local_context.capability.max_spdm_msg_size;
101 : : }
102 : : #endif
103 : :
104 : : /* combination */
105 : 15606 : uint32_t libspdm_get_scratch_buffer_capacity(libspdm_context_t *spdm_context) {
106 : 15606 : return 0 +
107 : : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
108 : 15606 : libspdm_get_scratch_buffer_secure_message_capacity(spdm_context) +
109 : 15606 : libspdm_get_scratch_buffer_large_message_capacity(spdm_context) +
110 : : #endif
111 : 15606 : libspdm_get_scratch_buffer_sender_receiver_capacity(spdm_context) +
112 : : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
113 : 15606 : libspdm_get_scratch_buffer_large_sender_receiver_capacity(spdm_context) +
114 : : #endif
115 : 15606 : libspdm_get_scratch_buffer_last_spdm_request_capacity(spdm_context) +
116 : : #if LIBSPDM_RESPOND_IF_READY_SUPPORT
117 : 15606 : 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 : 1260 : static bool need_session_info_for_data(libspdm_data_type_t data_type)
131 : : {
132 [ + + ]: 1260 : 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 : 1202 : default:
146 : 1202 : return false;
147 : : }
148 : : }
149 : :
150 : 1018 : 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 [ + + + + ]: 1018 : if (spdm_context == NULL || data_type >= LIBSPDM_DATA_MAX) {
170 : 4 : return LIBSPDM_STATUS_INVALID_PARAMETER;
171 : : }
172 : :
173 : 1014 : context = spdm_context;
174 : :
175 [ + + ]: 1014 : 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 : 974 : session_info = NULL;
186 : : }
187 : :
188 [ + + + + : 1006 : 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 : 34 : case LIBSPDM_DATA_LOCAL_PUBLIC_CERT_CHAIN:
566 [ + + ]: 34 : if (parameter->location != LIBSPDM_DATA_LOCATION_LOCAL) {
567 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
568 : : }
569 : 32 : slot_id = parameter->additional_data[0];
570 [ + + ]: 32 : if (slot_id >= SPDM_MAX_SLOT_COUNT) {
571 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
572 : : }
573 : 30 : context->local_context.local_cert_chain_provision_size[slot_id] = data_size;
574 : 30 : context->local_context.local_cert_chain_provision[slot_id] = data;
575 : 30 : 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 : 588 : case LIBSPDM_DATA_IS_REQUESTER:
729 [ + + ]: 588 : if (data_size != sizeof(bool)) {
730 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
731 : : }
732 [ + + ]: 586 : if (parameter->location != LIBSPDM_DATA_LOCATION_LOCAL) {
733 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
734 : : }
735 : 584 : context->local_context.is_requester = *(const bool *)data;
736 : 584 : 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 : 784 : 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 : 3101 : void libspdm_reset_message_a(libspdm_context_t *spdm_context)
1302 : : {
1303 : 3101 : libspdm_reset_managed_buffer(&spdm_context->transcript.message_a);
1304 : 3101 : spdm_context->transcript.message_a_in_m1m2 = false;
1305 : 3101 : }
1306 : :
1307 : 1334 : void libspdm_reset_message_d(libspdm_context_t *spdm_context)
1308 : : {
1309 : 1334 : libspdm_reset_managed_buffer(&spdm_context->transcript.message_d);
1310 : : /* Restarting the DIGESTS transcript reopens the "immediately follows VCA" window. */
1311 : 1334 : spdm_context->connection_info.digest_transcript_window_closed = false;
1312 : 1334 : }
1313 : :
1314 : 3922 : void libspdm_reset_message_b(libspdm_context_t *spdm_context)
1315 : : {
1316 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1317 : : libspdm_reset_managed_buffer(&spdm_context->transcript.message_b);
1318 : : #else
1319 [ + + ]: 3922 : if (spdm_context->transcript.digest_context_m1m2 != NULL) {
1320 : 446 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1321 : : spdm_context->transcript.digest_context_m1m2);
1322 : 446 : spdm_context->transcript.digest_context_m1m2 = NULL;
1323 : : }
1324 : : #endif
1325 : 3922 : }
1326 : :
1327 : 2900 : void libspdm_reset_message_c(libspdm_context_t *spdm_context)
1328 : : {
1329 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1330 : : libspdm_reset_managed_buffer(&spdm_context->transcript.message_c);
1331 : : #else
1332 [ + + ]: 2900 : if (spdm_context->transcript.digest_context_m1m2 != NULL) {
1333 : 2 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1334 : : spdm_context->transcript.digest_context_m1m2);
1335 : 2 : spdm_context->transcript.digest_context_m1m2 = NULL;
1336 : : }
1337 : : #endif
1338 : 2900 : }
1339 : :
1340 : 2724 : void libspdm_reset_message_mut_b(libspdm_context_t *spdm_context)
1341 : : {
1342 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1343 : : libspdm_reset_managed_buffer(&spdm_context->transcript.message_mut_b);
1344 : : #else
1345 [ + + ]: 2724 : if (spdm_context->transcript.digest_context_mut_m1m2 != NULL) {
1346 : 128 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1347 : : spdm_context->transcript.digest_context_mut_m1m2);
1348 : 128 : spdm_context->transcript.digest_context_mut_m1m2 = NULL;
1349 : : }
1350 : : #endif
1351 : 2724 : }
1352 : :
1353 : 2658 : void libspdm_reset_message_mut_c(libspdm_context_t *spdm_context)
1354 : : {
1355 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1356 : : libspdm_reset_managed_buffer(&spdm_context->transcript.message_mut_c);
1357 : : #else
1358 [ + + ]: 2658 : if (spdm_context->transcript.digest_context_mut_m1m2 != NULL) {
1359 : 10 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1360 : : spdm_context->transcript.digest_context_mut_m1m2);
1361 : 10 : spdm_context->transcript.digest_context_mut_m1m2 = NULL;
1362 : : }
1363 : : #endif
1364 : 2658 : }
1365 : :
1366 : 15142 : void libspdm_reset_message_m(libspdm_context_t *spdm_context, void *session_info)
1367 : : {
1368 : : libspdm_session_info_t *spdm_session_info;
1369 : :
1370 : 15142 : spdm_session_info = session_info;
1371 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1372 : : if (spdm_session_info == NULL) {
1373 : : libspdm_reset_managed_buffer(&spdm_context->transcript.message_m);
1374 : : } else {
1375 : : libspdm_reset_managed_buffer(&spdm_session_info->session_transcript.message_m);
1376 : : }
1377 : : #else
1378 [ + + ]: 15142 : if (spdm_session_info == NULL) {
1379 [ + + ]: 10090 : if (spdm_context->transcript.digest_context_l1l2 != NULL) {
1380 : 138 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1381 : : spdm_context->transcript.digest_context_l1l2);
1382 : 138 : spdm_context->transcript.digest_context_l1l2 = NULL;
1383 : : }
1384 : : } else {
1385 [ + + ]: 5052 : if (spdm_session_info->session_transcript.digest_context_l1l2 != NULL) {
1386 : 10 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1387 : : spdm_session_info->session_transcript.digest_context_l1l2);
1388 : 10 : spdm_session_info->session_transcript.digest_context_l1l2 = NULL;
1389 : : }
1390 : : }
1391 : : #endif
1392 : : #if LIBSPDM_ENABLE_CAPABILITY_MEAS_CAP
1393 [ + + ]: 15142 : if (spdm_context->spdm_meas_log_reset_callback != NULL) {
1394 [ + + ]: 162 : spdm_context->spdm_meas_log_reset_callback(
1395 : : spdm_context, spdm_session_info == NULL ? NULL : &spdm_session_info->session_id);
1396 : : }
1397 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_MEAS_CAP */
1398 : 15142 : }
1399 : :
1400 : 4428 : void libspdm_reset_message_k(libspdm_context_t *spdm_context, void *session_info)
1401 : : {
1402 : : libspdm_session_info_t *spdm_session_info;
1403 : :
1404 : 4428 : spdm_session_info = session_info;
1405 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1406 : : libspdm_reset_managed_buffer(&spdm_session_info->session_transcript.message_k);
1407 : : #else
1408 : : {
1409 [ + + ]: 4428 : if (spdm_session_info->session_transcript.digest_context_th != NULL) {
1410 : 62 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1411 : : spdm_session_info->session_transcript.digest_context_th);
1412 : 62 : spdm_session_info->session_transcript.digest_context_th = NULL;
1413 : : }
1414 [ + + ]: 4428 : if (spdm_session_info->session_transcript.digest_context_th_backup != NULL) {
1415 : 38 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1416 : : spdm_session_info->session_transcript.digest_context_th_backup);
1417 : 38 : spdm_session_info->session_transcript.digest_context_th_backup = NULL;
1418 : : }
1419 : : }
1420 : : #endif
1421 : 4428 : }
1422 : :
1423 : 4424 : void libspdm_reset_message_encap_d(void *session_info)
1424 : : {
1425 : : libspdm_session_info_t *spdm_session_info;
1426 : :
1427 : 4424 : spdm_session_info = session_info;
1428 : 4424 : libspdm_reset_managed_buffer(&spdm_session_info->session_transcript.message_encap_d);
1429 : : /* Restarting the encapsulated DIGESTS transcript reopens the "first encapsulated response"
1430 : : * window. */
1431 : 4424 : spdm_session_info->session_transcript.encap_digest_window_closed = false;
1432 : 4424 : }
1433 : :
1434 : 4468 : void libspdm_reset_message_f(libspdm_context_t *spdm_context, void *session_info)
1435 : : {
1436 : : libspdm_session_info_t *spdm_session_info;
1437 : :
1438 : 4468 : spdm_session_info = session_info;
1439 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1440 : : libspdm_reset_managed_buffer(&spdm_session_info->session_transcript.message_f);
1441 : : #else
1442 : : {
1443 [ + + ]: 4468 : if (spdm_session_info->session_transcript.digest_context_th != NULL) {
1444 : 46 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1445 : : spdm_session_info->session_transcript.digest_context_th);
1446 : 46 : spdm_session_info->session_transcript.digest_context_th =
1447 : 46 : spdm_session_info->session_transcript.digest_context_th_backup;
1448 : 46 : spdm_session_info->session_transcript.digest_context_th_backup = NULL;
1449 : : }
1450 : 4468 : spdm_session_info->session_transcript.message_f_initialized = false;
1451 : : }
1452 : : #endif
1453 : 4468 : }
1454 : :
1455 : 6006 : void libspdm_reset_message_e(libspdm_context_t *spdm_context, void *session_info)
1456 : : {
1457 : : libspdm_session_info_t *spdm_session_info;
1458 : :
1459 : 6006 : spdm_session_info = session_info;
1460 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1461 : : if (spdm_session_info == NULL) {
1462 : : libspdm_reset_managed_buffer(&spdm_context->transcript.message_e);
1463 : : } else {
1464 : : libspdm_reset_managed_buffer(&spdm_session_info->session_transcript.message_e);
1465 : : }
1466 : : #else
1467 [ + + ]: 6006 : if (spdm_session_info == NULL) {
1468 [ + + ]: 1582 : if (spdm_context->transcript.digest_context_il1il2 != NULL) {
1469 : 28 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1470 : : spdm_context->transcript.digest_context_il1il2);
1471 : 28 : spdm_context->transcript.digest_context_il1il2 = NULL;
1472 : : }
1473 : : } else {
1474 [ + + ]: 4424 : if (spdm_session_info->session_transcript.digest_context_il1il2 != NULL) {
1475 : 6 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1476 : : spdm_session_info->session_transcript.digest_context_il1il2);
1477 : 6 : spdm_session_info->session_transcript.digest_context_il1il2 = NULL;
1478 : : }
1479 : : }
1480 : : #endif
1481 : 6006 : }
1482 : :
1483 : 5936 : void libspdm_reset_message_encap_e(libspdm_context_t *spdm_context, void *session_info)
1484 : : {
1485 : : libspdm_session_info_t *spdm_session_info;
1486 : :
1487 : 5936 : spdm_session_info = session_info;
1488 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1489 : : if (spdm_session_info == NULL) {
1490 : : libspdm_reset_managed_buffer(&spdm_context->transcript.message_encap_e);
1491 : : } else {
1492 : : libspdm_reset_managed_buffer(&spdm_session_info->session_transcript.message_encap_e);
1493 : : }
1494 : : #else
1495 [ + + ]: 5936 : if (spdm_session_info == NULL) {
1496 [ + + ]: 1512 : if (spdm_context->transcript.digest_context_encap_il1il2 != NULL) {
1497 : 28 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1498 : : spdm_context->transcript.digest_context_encap_il1il2);
1499 : 28 : spdm_context->transcript.digest_context_encap_il1il2 = NULL;
1500 : : }
1501 : : } else {
1502 [ + + ]: 4424 : if (spdm_session_info->session_transcript.digest_context_encap_il1il2 != NULL) {
1503 : 4 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1504 : : spdm_session_info->session_transcript.digest_context_encap_il1il2);
1505 : 4 : spdm_session_info->session_transcript.digest_context_encap_il1il2 = NULL;
1506 : : }
1507 : : }
1508 : : #endif
1509 : 5936 : }
1510 : :
1511 : 9632 : void libspdm_reset_message_buffer_via_request_code(void *context, void *session_info,
1512 : : uint8_t request_code)
1513 : : {
1514 : : libspdm_context_t *spdm_context;
1515 : :
1516 : 9632 : spdm_context = context;
1517 : : /* Close the DIGESTS transcript window once a non-GET_DIGESTS request is processed after
1518 : : * NEGOTIATED (the state check excludes the VCA messages). */
1519 [ + + ]: 9632 : if ((request_code != SPDM_GET_DIGESTS) &&
1520 [ + + ]: 9478 : (spdm_context->connection_info.connection_state >= LIBSPDM_CONNECTION_STATE_NEGOTIATED)) {
1521 : 8790 : spdm_context->connection_info.digest_transcript_window_closed = true;
1522 : : }
1523 : : /**
1524 : : * Any request other than SPDM_GET_MEASUREMENTS resets L1/L2
1525 : : */
1526 [ + + ]: 9632 : if (request_code != SPDM_GET_MEASUREMENTS) {
1527 : 8752 : libspdm_reset_message_m(spdm_context, session_info);
1528 : : }
1529 : : /**
1530 : : * If the Requester issued GET_MEASUREMENTS or KEY_EXCHANGE or FINISH or PSK_EXCHANGE
1531 : : * or PSK_FINISH or KEY_UPDATE or HEARTBEAT or GET_ENCAPSULATED_REQUEST or DELIVER_ENCAPSULATED_RESPONSE
1532 : : * or END_SESSION request(s) or SPDM_GET_MEASUREMENT_EXTENSION_LOG and skipped CHALLENGE completion, M1 and M2 are reset to null.
1533 : : */
1534 [ + + + + : 9632 : switch (request_code)
+ ]
1535 : : {
1536 : 2114 : case SPDM_KEY_EXCHANGE:
1537 : : case SPDM_GET_MEASUREMENTS:
1538 : : case SPDM_FINISH:
1539 : : case SPDM_PSK_EXCHANGE:
1540 : : case SPDM_PSK_FINISH:
1541 : : case SPDM_KEY_UPDATE:
1542 : : case SPDM_HEARTBEAT:
1543 : : case SPDM_GET_ENCAPSULATED_REQUEST:
1544 : : case SPDM_END_SESSION:
1545 : : case SPDM_GET_MEASUREMENT_EXTENSION_LOG:
1546 [ + + ]: 2114 : if (spdm_context->connection_info.connection_state <
1547 : : LIBSPDM_CONNECTION_STATE_AUTHENTICATED) {
1548 : 1192 : libspdm_reset_message_b(spdm_context);
1549 : 1192 : libspdm_reset_message_c(spdm_context);
1550 : 1192 : spdm_context->transcript.message_a_in_m1m2 = false;
1551 : 1192 : libspdm_reset_message_mut_b(spdm_context);
1552 : 1192 : libspdm_reset_message_mut_c(spdm_context);
1553 : : }
1554 : 2114 : break;
1555 : 96 : case SPDM_DELIVER_ENCAPSULATED_RESPONSE:
1556 [ + - ]: 96 : if (spdm_context->connection_info.connection_state <
1557 : : LIBSPDM_CONNECTION_STATE_AUTHENTICATED) {
1558 : 96 : libspdm_reset_message_b(spdm_context);
1559 : 96 : libspdm_reset_message_c(spdm_context);
1560 : 96 : spdm_context->transcript.message_a_in_m1m2 = false;
1561 : : }
1562 : 96 : break;
1563 : 154 : case SPDM_GET_DIGESTS:
1564 : 154 : libspdm_reset_message_b(spdm_context);
1565 : 154 : break;
1566 : 112 : case SPDM_GET_ENDPOINT_INFO:
1567 : 112 : libspdm_reset_message_e(spdm_context, session_info);
1568 : 112 : libspdm_reset_message_encap_e(spdm_context, session_info);
1569 : 112 : break;
1570 : 7156 : default:
1571 : 7156 : break;
1572 : : }
1573 : 9632 : }
1574 : :
1575 : 80 : void libspdm_reset_message_buffer_via_encap_request_code(void *context, void *session_info,
1576 : : uint8_t request_code)
1577 : : {
1578 : : libspdm_session_info_t *spdm_session_info;
1579 : :
1580 : : /* The encapsulated flow only affects the session transcript. */
1581 [ + + ]: 80 : if (session_info == NULL) {
1582 : 48 : return;
1583 : : }
1584 : 32 : spdm_session_info = session_info;
1585 : :
1586 : : /* Close the encapsulated DIGESTS window if the first encapsulated request is not GET_DIGESTS.
1587 : : * Enforced here (not from a fixed local sequence) so it holds for whatever order the peer drives,
1588 : : * for both the Requester and Responder encapsulated flows. */
1589 [ + + + - ]: 48 : if ((request_code != SPDM_GET_DIGESTS) &&
1590 : 16 : (libspdm_get_managed_buffer_size(
1591 : 16 : &spdm_session_info->session_transcript.message_encap_d) == 0)) {
1592 : 16 : spdm_session_info->session_transcript.encap_digest_window_closed = true;
1593 : : }
1594 : : }
1595 : :
1596 : 644 : libspdm_return_t libspdm_append_message_a(libspdm_context_t *spdm_context, const void *message,
1597 : : size_t message_size)
1598 : : {
1599 : 644 : spdm_context->transcript.message_a_in_m1m2 = true;
1600 : 644 : return libspdm_append_managed_buffer(&spdm_context->transcript.message_a,
1601 : : message, message_size);
1602 : : }
1603 : :
1604 : 20 : libspdm_return_t libspdm_append_message_d(libspdm_context_t *spdm_context, const void *message,
1605 : : size_t message_size)
1606 : : {
1607 : : /* Window closed: the GET_DIGESTS did not immediately follow the VCA, so no DIGESTS is added. */
1608 [ + + ]: 20 : if (spdm_context->connection_info.digest_transcript_window_closed) {
1609 : 2 : return LIBSPDM_STATUS_SUCCESS;
1610 : : }
1611 : : /* Only the first message D after VCA in connection counts */
1612 [ + + ]: 18 : if (libspdm_get_managed_buffer_size(&spdm_context->transcript.message_d) != 0) {
1613 : 2 : return LIBSPDM_STATUS_SUCCESS;
1614 : : }
1615 : 16 : return libspdm_append_managed_buffer(&spdm_context->transcript.message_d,
1616 : : message, message_size);
1617 : : }
1618 : :
1619 : 12506 : libspdm_return_t libspdm_append_message_b(libspdm_context_t *spdm_context, const void *message,
1620 : : size_t message_size)
1621 : : {
1622 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1623 : : return libspdm_append_managed_buffer(&spdm_context->transcript.message_b,
1624 : : message, message_size);
1625 : : #else
1626 : : {
1627 : : bool result;
1628 : :
1629 [ + + ]: 12506 : if (spdm_context->transcript.digest_context_m1m2 == NULL) {
1630 : 372 : spdm_context->transcript.digest_context_m1m2 = libspdm_hash_new (
1631 : : spdm_context->connection_info.algorithm.base_hash_algo);
1632 [ - + ]: 372 : if (spdm_context->transcript.digest_context_m1m2 == NULL) {
1633 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1634 : : }
1635 : 372 : result = libspdm_hash_init (spdm_context->connection_info.algorithm.base_hash_algo,
1636 : : spdm_context->transcript.digest_context_m1m2);
1637 [ - + ]: 372 : if (!result) {
1638 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1639 : : spdm_context->transcript.digest_context_m1m2);
1640 : 0 : spdm_context->transcript.digest_context_m1m2 = NULL;
1641 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1642 : : }
1643 [ + + ]: 372 : if (((spdm_context->connection_info.version >> SPDM_VERSION_NUMBER_SHIFT_BIT) >
1644 [ - + ]: 308 : SPDM_MESSAGE_VERSION_11) || spdm_context->transcript.message_a_in_m1m2) {
1645 : 64 : result = libspdm_hash_update (
1646 : : spdm_context->connection_info.algorithm.base_hash_algo,
1647 : : spdm_context->transcript.digest_context_m1m2,
1648 : 64 : libspdm_get_managed_buffer(&spdm_context->transcript.message_a),
1649 : 64 : libspdm_get_managed_buffer_size(&spdm_context->transcript.message_a));
1650 [ - + ]: 64 : if (!result) {
1651 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1652 : : spdm_context->transcript.digest_context_m1m2);
1653 : 0 : spdm_context->transcript.digest_context_m1m2 = NULL;
1654 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1655 : : }
1656 : : }
1657 : : }
1658 : :
1659 : 12506 : result = libspdm_hash_update (spdm_context->connection_info.algorithm.base_hash_algo,
1660 : : spdm_context->transcript.digest_context_m1m2, message,
1661 : : message_size);
1662 [ - + ]: 12506 : if (!result) {
1663 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1664 : : spdm_context->transcript.digest_context_m1m2);
1665 : 0 : spdm_context->transcript.digest_context_m1m2 = NULL;
1666 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1667 : : }
1668 : :
1669 : 12506 : return LIBSPDM_STATUS_SUCCESS;
1670 : : }
1671 : : #endif
1672 : : }
1673 : :
1674 : 156 : libspdm_return_t libspdm_append_message_c(libspdm_context_t *spdm_context, const void *message,
1675 : : size_t message_size)
1676 : : {
1677 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1678 : : return libspdm_append_managed_buffer(&spdm_context->transcript.message_c,
1679 : : message, message_size);
1680 : : #else
1681 : : {
1682 : : bool result;
1683 : :
1684 [ + + ]: 156 : if (spdm_context->transcript.digest_context_m1m2 == NULL) {
1685 : 76 : spdm_context->transcript.digest_context_m1m2 = libspdm_hash_new (
1686 : : spdm_context->connection_info.algorithm.base_hash_algo);
1687 [ - + ]: 76 : if (spdm_context->transcript.digest_context_m1m2 == NULL) {
1688 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1689 : : }
1690 : 76 : result = libspdm_hash_init (spdm_context->connection_info.algorithm.base_hash_algo,
1691 : : spdm_context->transcript.digest_context_m1m2);
1692 [ - + ]: 76 : if (!result) {
1693 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1694 : : spdm_context->transcript.digest_context_m1m2);
1695 : 0 : spdm_context->transcript.digest_context_m1m2 = NULL;
1696 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1697 : : }
1698 [ + + ]: 76 : if (((spdm_context->connection_info.version >> SPDM_VERSION_NUMBER_SHIFT_BIT) >
1699 [ + + ]: 60 : SPDM_MESSAGE_VERSION_11) || spdm_context->transcript.message_a_in_m1m2) {
1700 : 20 : result = libspdm_hash_update (
1701 : : spdm_context->connection_info.algorithm.base_hash_algo,
1702 : : spdm_context->transcript.digest_context_m1m2,
1703 : 20 : libspdm_get_managed_buffer(&spdm_context->transcript.message_a),
1704 : 20 : libspdm_get_managed_buffer_size(&spdm_context->transcript.message_a));
1705 [ - + ]: 20 : if (!result) {
1706 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1707 : : spdm_context->transcript.digest_context_m1m2);
1708 : 0 : spdm_context->transcript.digest_context_m1m2 = NULL;
1709 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1710 : : }
1711 : : }
1712 : : }
1713 : :
1714 : 156 : result = libspdm_hash_update (spdm_context->connection_info.algorithm.base_hash_algo,
1715 : : spdm_context->transcript.digest_context_m1m2, message,
1716 : : message_size);
1717 [ - + ]: 156 : if (!result) {
1718 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1719 : : spdm_context->transcript.digest_context_m1m2);
1720 : 0 : spdm_context->transcript.digest_context_m1m2 = NULL;
1721 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1722 : : }
1723 : :
1724 : 156 : return LIBSPDM_STATUS_SUCCESS;
1725 : : }
1726 : : #endif
1727 : : }
1728 : :
1729 : 6060 : libspdm_return_t libspdm_append_message_mut_b(libspdm_context_t *spdm_context, const void *message,
1730 : : size_t message_size)
1731 : : {
1732 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1733 : : return libspdm_append_managed_buffer(&spdm_context->transcript.message_mut_b,
1734 : : message, message_size);
1735 : : #else
1736 : : {
1737 : : bool result;
1738 : :
1739 [ + + ]: 6060 : if (spdm_context->transcript.digest_context_mut_m1m2 == NULL) {
1740 : 112 : spdm_context->transcript.digest_context_mut_m1m2 = libspdm_hash_new (
1741 : : spdm_context->connection_info.algorithm.base_hash_algo);
1742 [ - + ]: 112 : if (spdm_context->transcript.digest_context_mut_m1m2 == NULL) {
1743 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1744 : : }
1745 : 112 : result = libspdm_hash_init (spdm_context->connection_info.algorithm.base_hash_algo,
1746 : : spdm_context->transcript.digest_context_mut_m1m2);
1747 [ - + ]: 112 : if (!result) {
1748 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1749 : : spdm_context->transcript.digest_context_mut_m1m2);
1750 : 0 : spdm_context->transcript.digest_context_mut_m1m2 = NULL;
1751 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1752 : : }
1753 [ + + ]: 112 : if ((spdm_context->connection_info.version >> SPDM_VERSION_NUMBER_SHIFT_BIT) >
1754 : : SPDM_MESSAGE_VERSION_11) {
1755 : :
1756 : : /* Need append VCA since 1.2 script */
1757 : 18 : result = libspdm_hash_update (
1758 : : spdm_context->connection_info.algorithm.base_hash_algo,
1759 : : spdm_context->transcript.digest_context_mut_m1m2,
1760 : 18 : libspdm_get_managed_buffer(&spdm_context->transcript.message_a),
1761 : 18 : libspdm_get_managed_buffer_size(&spdm_context->transcript.
1762 : : message_a));
1763 [ - + ]: 18 : if (!result) {
1764 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1765 : : spdm_context->transcript.digest_context_mut_m1m2);
1766 : 0 : spdm_context->transcript.digest_context_mut_m1m2 = NULL;
1767 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1768 : : }
1769 : : }
1770 : : }
1771 : :
1772 : 6060 : result = libspdm_hash_update (spdm_context->connection_info.algorithm.base_hash_algo,
1773 : : spdm_context->transcript.digest_context_mut_m1m2, message,
1774 : : message_size);
1775 [ - + ]: 6060 : if (!result) {
1776 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1777 : : spdm_context->transcript.digest_context_mut_m1m2);
1778 : 0 : spdm_context->transcript.digest_context_mut_m1m2 = NULL;
1779 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1780 : : }
1781 : :
1782 : 6060 : return LIBSPDM_STATUS_SUCCESS;
1783 : : }
1784 : : #endif
1785 : : }
1786 : :
1787 : 34 : libspdm_return_t libspdm_append_message_mut_c(libspdm_context_t *spdm_context, const void *message,
1788 : : size_t message_size)
1789 : : {
1790 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1791 : : return libspdm_append_managed_buffer(&spdm_context->transcript.message_mut_c,
1792 : : message, message_size);
1793 : : #else
1794 : : {
1795 : : bool result;
1796 : :
1797 [ + + ]: 34 : if (spdm_context->transcript.digest_context_mut_m1m2 == NULL) {
1798 : 26 : spdm_context->transcript.digest_context_mut_m1m2 = libspdm_hash_new (
1799 : : spdm_context->connection_info.algorithm.base_hash_algo);
1800 [ - + ]: 26 : if (spdm_context->transcript.digest_context_mut_m1m2 == NULL) {
1801 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1802 : : }
1803 : 26 : result = libspdm_hash_init (spdm_context->connection_info.algorithm.base_hash_algo,
1804 : : spdm_context->transcript.digest_context_mut_m1m2);
1805 [ - + ]: 26 : if (!result) {
1806 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1807 : : spdm_context->transcript.digest_context_mut_m1m2);
1808 : 0 : spdm_context->transcript.digest_context_mut_m1m2 = NULL;
1809 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1810 : : }
1811 [ + + ]: 26 : if ((spdm_context->connection_info.version >> SPDM_VERSION_NUMBER_SHIFT_BIT) >
1812 : : SPDM_MESSAGE_VERSION_11) {
1813 : :
1814 : : /* Need append VCA since 1.2 script */
1815 : 12 : result = libspdm_hash_update (
1816 : : spdm_context->connection_info.algorithm.base_hash_algo,
1817 : : spdm_context->transcript.digest_context_mut_m1m2,
1818 : 12 : libspdm_get_managed_buffer(&spdm_context->transcript.message_a),
1819 : 12 : libspdm_get_managed_buffer_size(&spdm_context->transcript.
1820 : : message_a));
1821 [ - + ]: 12 : if (!result) {
1822 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1823 : : spdm_context->transcript.digest_context_mut_m1m2);
1824 : 0 : spdm_context->transcript.digest_context_mut_m1m2 = NULL;
1825 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1826 : : }
1827 : : }
1828 : : }
1829 : :
1830 : 34 : result = libspdm_hash_update (spdm_context->connection_info.algorithm.base_hash_algo,
1831 : : spdm_context->transcript.digest_context_mut_m1m2, message,
1832 : : message_size);
1833 [ - + ]: 34 : if (!result) {
1834 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1835 : : spdm_context->transcript.digest_context_mut_m1m2);
1836 : 0 : spdm_context->transcript.digest_context_mut_m1m2 = NULL;
1837 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1838 : : }
1839 : :
1840 : 34 : return LIBSPDM_STATUS_SUCCESS;
1841 : : }
1842 : : #endif
1843 : : }
1844 : :
1845 : 1478 : libspdm_return_t libspdm_append_message_m(libspdm_context_t *spdm_context, void *session_info,
1846 : : const void *message, size_t message_size)
1847 : : {
1848 : : libspdm_session_info_t *spdm_session_info;
1849 : :
1850 : 1478 : spdm_session_info = session_info;
1851 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1852 : : if (spdm_session_info == NULL) {
1853 : : return libspdm_append_managed_buffer(&spdm_context->transcript.message_m,
1854 : : message, message_size);
1855 : : } else {
1856 : : return libspdm_append_managed_buffer(&spdm_session_info->session_transcript.message_m,
1857 : : message, message_size);
1858 : : }
1859 : : #else
1860 : : {
1861 : : bool result;
1862 : :
1863 [ + + ]: 1478 : if (spdm_session_info == NULL) {
1864 [ + + ]: 1460 : if (spdm_context->transcript.digest_context_l1l2 == NULL) {
1865 : 136 : spdm_context->transcript.digest_context_l1l2 = libspdm_hash_new (
1866 : : spdm_context->connection_info.algorithm.base_hash_algo);
1867 [ - + ]: 136 : if (spdm_context->transcript.digest_context_l1l2 == NULL) {
1868 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1869 : : }
1870 : 136 : result = libspdm_hash_init (spdm_context->connection_info.algorithm.base_hash_algo,
1871 : : spdm_context->transcript.digest_context_l1l2);
1872 [ - + ]: 136 : if (!result) {
1873 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1874 : : spdm_context->transcript.digest_context_l1l2);
1875 : 0 : spdm_context->transcript.digest_context_l1l2 = NULL;
1876 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1877 : : }
1878 [ + + ]: 136 : if ((spdm_context->connection_info.version >> SPDM_VERSION_NUMBER_SHIFT_BIT) >
1879 : : SPDM_MESSAGE_VERSION_11) {
1880 : :
1881 : : /* Need append VCA since 1.2 script */
1882 : 28 : result = libspdm_hash_update (
1883 : : spdm_context->connection_info.algorithm.base_hash_algo,
1884 : : spdm_context->transcript.digest_context_l1l2,
1885 : 28 : libspdm_get_managed_buffer(
1886 : 28 : &spdm_context->transcript.message_a),
1887 : 28 : libspdm_get_managed_buffer_size(&spdm_context->transcript.
1888 : : message_a));
1889 [ - + ]: 28 : if (!result) {
1890 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1891 : : spdm_context->transcript.digest_context_l1l2);
1892 : 0 : spdm_context->transcript.digest_context_l1l2 = NULL;
1893 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1894 : : }
1895 : : }
1896 : : }
1897 : 1460 : result = libspdm_hash_update (spdm_context->connection_info.algorithm.base_hash_algo,
1898 : : spdm_context->transcript.digest_context_l1l2, message,
1899 : : message_size);
1900 [ - + ]: 1460 : if (!result) {
1901 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1902 : : spdm_context->transcript.digest_context_l1l2);
1903 : 0 : spdm_context->transcript.digest_context_l1l2 = NULL;
1904 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1905 : : }
1906 : : } else {
1907 [ + + ]: 18 : if (spdm_session_info->session_transcript.digest_context_l1l2 == NULL) {
1908 : 10 : spdm_session_info->session_transcript.digest_context_l1l2 = libspdm_hash_new (
1909 : : spdm_context->connection_info.algorithm.base_hash_algo);
1910 [ - + ]: 10 : if (spdm_session_info->session_transcript.digest_context_l1l2 == NULL) {
1911 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1912 : : }
1913 : 10 : result = libspdm_hash_init (spdm_context->connection_info.algorithm.base_hash_algo,
1914 : : spdm_session_info->session_transcript.digest_context_l1l2);
1915 [ - + ]: 10 : if (!result) {
1916 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1917 : : spdm_session_info->session_transcript.digest_context_l1l2);
1918 : 0 : spdm_session_info->session_transcript.digest_context_l1l2 = NULL;
1919 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1920 : : }
1921 [ + + ]: 10 : if ((spdm_context->connection_info.version >> SPDM_VERSION_NUMBER_SHIFT_BIT) >
1922 : : SPDM_MESSAGE_VERSION_11) {
1923 : :
1924 : : /* Need append VCA since 1.2 script*/
1925 : :
1926 : 2 : result = libspdm_hash_update (
1927 : : spdm_context->connection_info.algorithm.base_hash_algo,
1928 : : spdm_session_info->session_transcript.digest_context_l1l2,
1929 : 2 : libspdm_get_managed_buffer(
1930 : 2 : &spdm_context->transcript.message_a),
1931 : 2 : libspdm_get_managed_buffer_size(&spdm_context->transcript.
1932 : : message_a));
1933 [ - + ]: 2 : if (!result) {
1934 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1935 : : spdm_session_info->session_transcript.digest_context_l1l2);
1936 : 0 : spdm_session_info->session_transcript.digest_context_l1l2 = NULL;
1937 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1938 : : }
1939 : : }
1940 : : }
1941 : 18 : result = libspdm_hash_update (spdm_context->connection_info.algorithm.base_hash_algo,
1942 : : spdm_session_info->session_transcript.digest_context_l1l2,
1943 : : message, message_size);
1944 [ - + ]: 18 : if (!result) {
1945 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
1946 : : spdm_session_info->session_transcript.digest_context_l1l2);
1947 : 0 : spdm_session_info->session_transcript.digest_context_l1l2 = NULL;
1948 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1949 : : }
1950 : : }
1951 : :
1952 : 1478 : return LIBSPDM_STATUS_SUCCESS;
1953 : : }
1954 : : #endif
1955 : : }
1956 : :
1957 : 844 : libspdm_return_t libspdm_append_message_k(libspdm_context_t *spdm_context,
1958 : : void *session_info,
1959 : : bool is_requester, const void *message,
1960 : : size_t message_size)
1961 : : {
1962 : : libspdm_session_info_t *spdm_session_info;
1963 : :
1964 : 844 : spdm_session_info = session_info;
1965 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1966 : : return libspdm_append_managed_buffer(
1967 : : &spdm_session_info->session_transcript.message_k, message,
1968 : : message_size);
1969 : : #else
1970 : : {
1971 : : const uint8_t *cert_chain_buffer;
1972 : : size_t cert_chain_buffer_size;
1973 : : bool result;
1974 : : uint8_t cert_chain_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
1975 : : uint32_t hash_size;
1976 : : uint8_t slot_id;
1977 : :
1978 : 844 : hash_size = libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
1979 : :
1980 [ + + ]: 844 : if (spdm_session_info->session_transcript.digest_context_th == NULL) {
1981 [ + + ]: 422 : if (!spdm_session_info->use_psk) {
1982 [ + + ]: 246 : if (is_requester) {
1983 : 142 : slot_id = spdm_session_info->peer_used_cert_chain_slot_id;
1984 : 142 : LIBSPDM_ASSERT((slot_id < SPDM_MAX_SLOT_COUNT) || (slot_id == 0xFF));
1985 [ + + ]: 142 : if (slot_id == 0xFF) {
1986 : 8 : result = libspdm_get_peer_public_key_buffer(
1987 : : spdm_context, (const void **)&cert_chain_buffer,
1988 : : &cert_chain_buffer_size);
1989 [ + + ]: 8 : if (!result) {
1990 : 4 : return LIBSPDM_STATUS_INVALID_STATE_PEER;
1991 : : }
1992 : :
1993 : 4 : result = libspdm_hash_all(
1994 : : spdm_context->connection_info.algorithm.base_hash_algo,
1995 : : cert_chain_buffer, cert_chain_buffer_size,
1996 : : cert_chain_buffer_hash);
1997 [ - + ]: 4 : if (!result) {
1998 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
1999 : : }
2000 : : } else {
2001 : 134 : LIBSPDM_ASSERT(
2002 : : hash_size ==
2003 : : spdm_context->connection_info
2004 : : .peer_used_cert_chain[slot_id].buffer_hash_size);
2005 : :
2006 : 134 : libspdm_copy_mem(cert_chain_buffer_hash,
2007 : : sizeof(cert_chain_buffer_hash),
2008 : : spdm_context->connection_info
2009 : 134 : .peer_used_cert_chain[slot_id].buffer_hash,
2010 : : hash_size);
2011 : : }
2012 : : } else {
2013 : 104 : slot_id = spdm_session_info->local_used_cert_chain_slot_id;
2014 : 104 : LIBSPDM_ASSERT((slot_id < SPDM_MAX_SLOT_COUNT) || (slot_id == 0xFF));
2015 [ + + ]: 104 : if (slot_id == 0xFF) {
2016 : 8 : result = libspdm_get_local_public_key_buffer(
2017 : : spdm_context, (const void **)&cert_chain_buffer,
2018 : : &cert_chain_buffer_size);
2019 [ + + ]: 8 : if (!result) {
2020 : 2 : return LIBSPDM_STATUS_INVALID_STATE_LOCAL;
2021 : : }
2022 : : } else {
2023 : 96 : libspdm_get_local_cert_chain_buffer(
2024 : : spdm_context, slot_id, (const void **)&cert_chain_buffer,
2025 : : &cert_chain_buffer_size);
2026 : : }
2027 : :
2028 : 102 : result = libspdm_hash_all(
2029 : : spdm_context->connection_info.algorithm.base_hash_algo,
2030 : : cert_chain_buffer, cert_chain_buffer_size,
2031 : : cert_chain_buffer_hash);
2032 [ - + ]: 102 : if (!result) {
2033 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2034 : : }
2035 : : }
2036 : : }
2037 : : }
2038 : :
2039 : :
2040 : : /* prepare digest_context_th*/
2041 : :
2042 [ + + ]: 838 : if (spdm_session_info->session_transcript.digest_context_th == NULL) {
2043 : 416 : spdm_session_info->session_transcript.digest_context_th = libspdm_hash_new (
2044 : : spdm_context->connection_info.algorithm.base_hash_algo);
2045 [ - + ]: 416 : if (spdm_session_info->session_transcript.digest_context_th == NULL) {
2046 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2047 : : }
2048 : 416 : result = libspdm_hash_init (spdm_context->connection_info.algorithm.base_hash_algo,
2049 : : spdm_session_info->session_transcript.digest_context_th);
2050 [ - + ]: 416 : if (!result) {
2051 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
2052 : : spdm_session_info->session_transcript.digest_context_th);
2053 : 0 : spdm_session_info->session_transcript.digest_context_th = NULL;
2054 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2055 : : }
2056 : 416 : result = libspdm_hash_update (spdm_context->connection_info.algorithm.base_hash_algo,
2057 : : spdm_session_info->session_transcript.digest_context_th,
2058 : 416 : libspdm_get_managed_buffer(&spdm_context->transcript.
2059 : : message_a),
2060 : : libspdm_get_managed_buffer_size(
2061 : 416 : &spdm_context->transcript.message_a));
2062 [ - + ]: 416 : if (!result) {
2063 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
2064 : : spdm_session_info->session_transcript.digest_context_th);
2065 : 0 : spdm_session_info->session_transcript.digest_context_th = NULL;
2066 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2067 : : }
2068 [ + + ]: 416 : if (!spdm_session_info->use_psk) {
2069 [ + + ]: 240 : if (spdm_context->connection_info.multi_key_conn_rsp) {
2070 : 2 : result = libspdm_hash_update (
2071 : : spdm_context->connection_info.algorithm.base_hash_algo,
2072 : : spdm_session_info->session_transcript.digest_context_th,
2073 : 2 : libspdm_get_managed_buffer(&spdm_context->transcript.message_d),
2074 : 2 : libspdm_get_managed_buffer_size(&spdm_context->transcript.message_d));
2075 [ - + ]: 2 : if (!result) {
2076 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
2077 : : spdm_session_info->session_transcript.digest_context_th);
2078 : 0 : spdm_session_info->session_transcript.digest_context_th = NULL;
2079 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2080 : : }
2081 : : }
2082 : :
2083 : 240 : result = libspdm_hash_update (
2084 : : spdm_context->connection_info.algorithm.base_hash_algo,
2085 : : spdm_session_info->session_transcript.digest_context_th,
2086 : : cert_chain_buffer_hash, hash_size);
2087 [ - + ]: 240 : if (!result) {
2088 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
2089 : : spdm_session_info->session_transcript.digest_context_th);
2090 : 0 : spdm_session_info->session_transcript.digest_context_th = NULL;
2091 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2092 : : }
2093 : : }
2094 : : }
2095 : 838 : result = libspdm_hash_update (spdm_context->connection_info.algorithm.base_hash_algo,
2096 : : spdm_session_info->session_transcript.digest_context_th,
2097 : : message,
2098 : : message_size);
2099 [ - + ]: 838 : if (!result) {
2100 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
2101 : : spdm_session_info->session_transcript.digest_context_th);
2102 : 0 : spdm_session_info->session_transcript.digest_context_th = NULL;
2103 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2104 : : }
2105 : 838 : return LIBSPDM_STATUS_SUCCESS;
2106 : : }
2107 : : #endif
2108 : : }
2109 : :
2110 : 18 : libspdm_return_t libspdm_append_message_encap_d(void *session_info,
2111 : : const void *message,
2112 : : size_t message_size)
2113 : : {
2114 : : libspdm_session_info_t *spdm_session_info;
2115 : :
2116 : 18 : spdm_session_info = session_info;
2117 : : /* Window closed: the encapsulated DIGESTS is not the first encap response, so it is not added. */
2118 [ + + ]: 18 : if (spdm_session_info->session_transcript.encap_digest_window_closed) {
2119 : 2 : return LIBSPDM_STATUS_SUCCESS;
2120 : : }
2121 : : /* Only the first message EncapD in current session counts */
2122 [ + + ]: 16 : if (libspdm_get_managed_buffer_size(&spdm_session_info->session_transcript.message_encap_d) !=
2123 : : 0) {
2124 : 2 : return LIBSPDM_STATUS_SUCCESS;
2125 : : }
2126 : 14 : return libspdm_append_managed_buffer(
2127 : 14 : &spdm_session_info->session_transcript.message_encap_d, message,
2128 : : message_size);
2129 : : }
2130 : :
2131 : 664 : libspdm_return_t libspdm_append_message_f(libspdm_context_t *spdm_context,
2132 : : void *session_info,
2133 : : bool is_requester, const void *message,
2134 : : size_t message_size)
2135 : : {
2136 : : libspdm_session_info_t *spdm_session_info;
2137 : :
2138 : 664 : spdm_session_info = session_info;
2139 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
2140 : : return libspdm_append_managed_buffer(
2141 : : &spdm_session_info->session_transcript.message_f, message,
2142 : : message_size);
2143 : : #else
2144 : : {
2145 : : const uint8_t *mut_cert_chain_buffer;
2146 : : size_t mut_cert_chain_buffer_size;
2147 : : bool result;
2148 : : uint8_t mut_cert_chain_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
2149 : : uint32_t hash_size;
2150 : : libspdm_return_t status;
2151 : : uint8_t slot_id;
2152 : :
2153 : 664 : hash_size = libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
2154 : :
2155 [ + + ]: 664 : if (!spdm_session_info->session_transcript.message_f_initialized) {
2156 : : /* digest_context_th might be NULL in unit test, where message_k is hardcoded. */
2157 [ + + ]: 252 : if (spdm_session_info->session_transcript.digest_context_th == NULL) {
2158 : : status =
2159 : 244 : libspdm_append_message_k (spdm_context, session_info, is_requester, NULL, 0);
2160 [ + + ]: 244 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
2161 : 2 : return status;
2162 : : }
2163 : : }
2164 : :
2165 [ + + + + ]: 250 : if (!spdm_session_info->use_psk && (spdm_session_info->mut_auth_requested != 0)) {
2166 [ + + ]: 46 : if (is_requester) {
2167 : 18 : slot_id = spdm_session_info->local_used_cert_chain_slot_id;
2168 : 18 : LIBSPDM_ASSERT((slot_id < SPDM_MAX_SLOT_COUNT) || (slot_id == 0xFF));
2169 [ + + ]: 18 : if (slot_id == 0xFF) {
2170 : 2 : result = libspdm_get_local_public_key_buffer(
2171 : : spdm_context,
2172 : : (const void **)&mut_cert_chain_buffer,
2173 : : &mut_cert_chain_buffer_size);
2174 [ + - ]: 2 : if (!result) {
2175 : 2 : return LIBSPDM_STATUS_INVALID_STATE_LOCAL;
2176 : : }
2177 : : } else {
2178 : 16 : libspdm_get_local_cert_chain_buffer(
2179 : : spdm_context,
2180 : : slot_id,
2181 : : (const void **)&mut_cert_chain_buffer,
2182 : : &mut_cert_chain_buffer_size);
2183 : : }
2184 : :
2185 : 16 : result = libspdm_hash_all(
2186 : : spdm_context->connection_info.algorithm.base_hash_algo,
2187 : : mut_cert_chain_buffer, mut_cert_chain_buffer_size,
2188 : : mut_cert_chain_buffer_hash);
2189 [ - + ]: 16 : if (!result) {
2190 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2191 : : }
2192 : : } else {
2193 : 28 : slot_id = spdm_session_info->peer_used_cert_chain_slot_id;
2194 : 28 : LIBSPDM_ASSERT((slot_id < SPDM_MAX_SLOT_COUNT) || (slot_id == 0xFF));
2195 [ + + ]: 28 : if (slot_id == 0xFF) {
2196 : 4 : result = libspdm_get_peer_public_key_buffer(
2197 : : spdm_context,
2198 : : (const void **)&mut_cert_chain_buffer,
2199 : : &mut_cert_chain_buffer_size);
2200 [ + + ]: 4 : if (!result) {
2201 : 2 : return LIBSPDM_STATUS_INVALID_STATE_PEER;
2202 : : }
2203 : :
2204 : 2 : result = libspdm_hash_all(
2205 : : spdm_context->connection_info.algorithm.base_hash_algo,
2206 : : mut_cert_chain_buffer, mut_cert_chain_buffer_size,
2207 : : mut_cert_chain_buffer_hash);
2208 [ - + ]: 2 : if (!result) {
2209 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2210 : : }
2211 : : } else {
2212 : 24 : LIBSPDM_ASSERT(
2213 : : hash_size ==
2214 : : spdm_context->connection_info
2215 : : .peer_used_cert_chain[slot_id].buffer_hash_size);
2216 : :
2217 : 24 : libspdm_copy_mem(mut_cert_chain_buffer_hash,
2218 : : sizeof(mut_cert_chain_buffer_hash),
2219 : : spdm_context->connection_info
2220 : 24 : .peer_used_cert_chain[slot_id].buffer_hash,
2221 : : hash_size);
2222 : : }
2223 : : }
2224 : : }
2225 : :
2226 : : /* It is first time call, backup current message_k context
2227 : : * this backup will be used in reset_message_f.*/
2228 : :
2229 : 246 : LIBSPDM_ASSERT (spdm_session_info->session_transcript.digest_context_th != NULL);
2230 : 246 : spdm_session_info->session_transcript.digest_context_th_backup = libspdm_hash_new (
2231 : : spdm_context->connection_info.algorithm.base_hash_algo);
2232 [ - + ]: 246 : if (spdm_session_info->session_transcript.digest_context_th_backup == NULL) {
2233 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2234 : : }
2235 : 246 : result = libspdm_hash_duplicate (spdm_context->connection_info.algorithm.base_hash_algo,
2236 : 246 : spdm_session_info->session_transcript.digest_context_th,
2237 : : spdm_session_info->session_transcript.digest_context_th_backup);
2238 [ - + ]: 246 : if (!result) {
2239 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
2240 : : spdm_session_info->session_transcript.digest_context_th_backup);
2241 : 0 : spdm_session_info->session_transcript.digest_context_th_backup = NULL;
2242 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2243 : : }
2244 : : }
2245 : :
2246 : :
2247 : : /* prepare digest_context_th*/
2248 : :
2249 : 658 : LIBSPDM_ASSERT (spdm_session_info->session_transcript.digest_context_th != NULL);
2250 [ + + ]: 658 : if (!spdm_session_info->session_transcript.message_f_initialized) {
2251 [ + + + + ]: 246 : if (!spdm_session_info->use_psk && (spdm_session_info->mut_auth_requested != 0)) {
2252 [ + + ]: 42 : if (spdm_context->connection_info.multi_key_conn_req) {
2253 : 2 : result = libspdm_hash_update (
2254 : : spdm_context->connection_info.algorithm.base_hash_algo,
2255 : : spdm_session_info->session_transcript.digest_context_th,
2256 : 2 : libspdm_get_managed_buffer(&spdm_session_info->session_transcript.
2257 : : message_encap_d),
2258 : 2 : libspdm_get_managed_buffer_size(&spdm_session_info->session_transcript.
2259 : : message_encap_d));
2260 [ - + ]: 2 : if (!result) {
2261 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
2262 : : spdm_session_info->session_transcript.digest_context_th);
2263 : 0 : spdm_session_info->session_transcript.digest_context_th = NULL;
2264 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2265 : : }
2266 : : }
2267 : :
2268 : 42 : result = libspdm_hash_update (
2269 : : spdm_context->connection_info.algorithm.base_hash_algo,
2270 : : spdm_session_info->session_transcript.digest_context_th,
2271 : : mut_cert_chain_buffer_hash, hash_size);
2272 [ - + ]: 42 : if (!result) {
2273 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
2274 : : spdm_session_info->session_transcript.digest_context_th);
2275 : 0 : spdm_session_info->session_transcript.digest_context_th = NULL;
2276 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2277 : : }
2278 : : }
2279 : : }
2280 : 658 : result = libspdm_hash_update (spdm_context->connection_info.algorithm.base_hash_algo,
2281 : : spdm_session_info->session_transcript.digest_context_th,
2282 : : message,
2283 : : message_size);
2284 [ - + ]: 658 : if (!result) {
2285 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
2286 : : spdm_session_info->session_transcript.digest_context_th);
2287 : 0 : spdm_session_info->session_transcript.digest_context_th = NULL;
2288 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2289 : : }
2290 : :
2291 : 658 : spdm_session_info->session_transcript.message_f_initialized = true;
2292 : 658 : return LIBSPDM_STATUS_SUCCESS;
2293 : : }
2294 : : #endif
2295 : : }
2296 : :
2297 : 68 : libspdm_return_t libspdm_append_message_e(libspdm_context_t *spdm_context, void *session_info,
2298 : : const void *message, size_t message_size)
2299 : : {
2300 : : libspdm_session_info_t *spdm_session_info;
2301 : :
2302 : 68 : spdm_session_info = session_info;
2303 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
2304 : : if (spdm_session_info == NULL) {
2305 : : return libspdm_append_managed_buffer(&spdm_context->transcript.message_e,
2306 : : message, message_size);
2307 : : } else {
2308 : : return libspdm_append_managed_buffer(&spdm_session_info->session_transcript.message_e,
2309 : : message, message_size);
2310 : : }
2311 : : #else
2312 : : {
2313 : : bool result;
2314 : :
2315 [ + + ]: 68 : if (spdm_session_info == NULL) {
2316 [ + + ]: 56 : if (spdm_context->transcript.digest_context_il1il2 == NULL) {
2317 : 28 : spdm_context->transcript.digest_context_il1il2 = libspdm_hash_new (
2318 : : spdm_context->connection_info.algorithm.base_hash_algo);
2319 [ - + ]: 28 : if (spdm_context->transcript.digest_context_il1il2 == NULL) {
2320 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2321 : : }
2322 : 28 : result = libspdm_hash_init (spdm_context->connection_info.algorithm.base_hash_algo,
2323 : : spdm_context->transcript.digest_context_il1il2);
2324 [ - + ]: 28 : if (!result) {
2325 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
2326 : : spdm_context->transcript.digest_context_il1il2);
2327 : 0 : spdm_context->transcript.digest_context_il1il2 = NULL;
2328 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2329 : : }
2330 : :
2331 : 28 : result = libspdm_hash_update (
2332 : : spdm_context->connection_info.algorithm.base_hash_algo,
2333 : : spdm_context->transcript.digest_context_il1il2,
2334 : 28 : libspdm_get_managed_buffer(&spdm_context->transcript.message_a),
2335 : 28 : libspdm_get_managed_buffer_size(&spdm_context->transcript.message_a));
2336 : :
2337 [ - + ]: 28 : if (!result) {
2338 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
2339 : : spdm_context->transcript.digest_context_il1il2);
2340 : 0 : spdm_context->transcript.digest_context_il1il2 = NULL;
2341 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2342 : : }
2343 : : }
2344 : 56 : result = libspdm_hash_update (spdm_context->connection_info.algorithm.base_hash_algo,
2345 : : spdm_context->transcript.digest_context_il1il2, message,
2346 : : message_size);
2347 [ - + ]: 56 : if (!result) {
2348 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
2349 : : spdm_context->transcript.digest_context_il1il2);
2350 : 0 : spdm_context->transcript.digest_context_il1il2 = NULL;
2351 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2352 : : }
2353 : : } else {
2354 [ + + ]: 12 : if (spdm_session_info->session_transcript.digest_context_il1il2 == NULL) {
2355 : 6 : spdm_session_info->session_transcript.digest_context_il1il2 = libspdm_hash_new (
2356 : : spdm_context->connection_info.algorithm.base_hash_algo);
2357 [ - + ]: 6 : if (spdm_session_info->session_transcript.digest_context_il1il2 == NULL) {
2358 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2359 : : }
2360 : 6 : result = libspdm_hash_init (spdm_context->connection_info.algorithm.base_hash_algo,
2361 : : spdm_session_info->session_transcript.digest_context_il1il2);
2362 [ - + ]: 6 : if (!result) {
2363 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
2364 : : spdm_session_info->session_transcript.digest_context_il1il2);
2365 : 0 : spdm_session_info->session_transcript.digest_context_il1il2 = NULL;
2366 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2367 : : }
2368 : :
2369 : 6 : result = libspdm_hash_update (
2370 : : spdm_context->connection_info.algorithm.base_hash_algo,
2371 : : spdm_session_info->session_transcript.digest_context_il1il2,
2372 : 6 : libspdm_get_managed_buffer(&spdm_context->transcript.message_a),
2373 : 6 : libspdm_get_managed_buffer_size(&spdm_context->transcript.message_a));
2374 : :
2375 [ - + ]: 6 : if (!result) {
2376 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
2377 : : spdm_session_info->session_transcript.digest_context_il1il2);
2378 : 0 : spdm_session_info->session_transcript.digest_context_il1il2 = NULL;
2379 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2380 : : }
2381 : : }
2382 : 12 : result = libspdm_hash_update (spdm_context->connection_info.algorithm.base_hash_algo,
2383 : : spdm_session_info->session_transcript.digest_context_il1il2,
2384 : : message, message_size);
2385 [ - + ]: 12 : if (!result) {
2386 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
2387 : : spdm_session_info->session_transcript.digest_context_il1il2);
2388 : 0 : spdm_session_info->session_transcript.digest_context_il1il2 = NULL;
2389 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2390 : : }
2391 : : }
2392 : :
2393 : 68 : return LIBSPDM_STATUS_SUCCESS;
2394 : : }
2395 : : #endif
2396 : : }
2397 : :
2398 : 52 : libspdm_return_t libspdm_append_message_encap_e(libspdm_context_t *spdm_context, void *session_info,
2399 : : const void *message, size_t message_size)
2400 : : {
2401 : : libspdm_session_info_t *spdm_session_info;
2402 : :
2403 : 52 : spdm_session_info = session_info;
2404 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
2405 : : if (spdm_session_info == NULL) {
2406 : : return libspdm_append_managed_buffer(&spdm_context->transcript.message_encap_e,
2407 : : message, message_size);
2408 : : } else {
2409 : : return libspdm_append_managed_buffer(&spdm_session_info->session_transcript.message_encap_e,
2410 : : message, message_size);
2411 : : }
2412 : : #else
2413 : : {
2414 : : bool result;
2415 : :
2416 [ + + ]: 52 : if (spdm_session_info == NULL) {
2417 [ + + ]: 42 : if (spdm_context->transcript.digest_context_encap_il1il2 == NULL) {
2418 : 28 : spdm_context->transcript.digest_context_encap_il1il2 = libspdm_hash_new (
2419 : : spdm_context->connection_info.algorithm.base_hash_algo);
2420 [ - + ]: 28 : if (spdm_context->transcript.digest_context_encap_il1il2 == NULL) {
2421 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2422 : : }
2423 : 28 : result = libspdm_hash_init (spdm_context->connection_info.algorithm.base_hash_algo,
2424 : : spdm_context->transcript.digest_context_encap_il1il2);
2425 [ - + ]: 28 : if (!result) {
2426 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
2427 : : spdm_context->transcript.digest_context_encap_il1il2);
2428 : 0 : spdm_context->transcript.digest_context_encap_il1il2 = NULL;
2429 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2430 : : }
2431 : :
2432 : 28 : result = libspdm_hash_update (
2433 : : spdm_context->connection_info.algorithm.base_hash_algo,
2434 : : spdm_context->transcript.digest_context_encap_il1il2,
2435 : 28 : libspdm_get_managed_buffer(&spdm_context->transcript.message_a),
2436 : 28 : libspdm_get_managed_buffer_size(&spdm_context->transcript.message_a));
2437 : :
2438 [ - + ]: 28 : if (!result) {
2439 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
2440 : : spdm_context->transcript.digest_context_encap_il1il2);
2441 : 0 : spdm_context->transcript.digest_context_encap_il1il2 = NULL;
2442 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2443 : : }
2444 : : }
2445 : 42 : result = libspdm_hash_update (spdm_context->connection_info.algorithm.base_hash_algo,
2446 : : spdm_context->transcript.digest_context_encap_il1il2,
2447 : : message,
2448 : : message_size);
2449 [ - + ]: 42 : if (!result) {
2450 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
2451 : : spdm_context->transcript.digest_context_encap_il1il2);
2452 : 0 : spdm_context->transcript.digest_context_encap_il1il2 = NULL;
2453 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2454 : : }
2455 : : } else {
2456 [ + + ]: 10 : if (spdm_session_info->session_transcript.digest_context_encap_il1il2 == NULL) {
2457 : 6 : spdm_session_info->session_transcript.digest_context_encap_il1il2 =
2458 : 6 : libspdm_hash_new (spdm_context->connection_info.algorithm.base_hash_algo);
2459 [ - + ]: 6 : if (spdm_session_info->session_transcript.digest_context_encap_il1il2 == NULL) {
2460 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2461 : : }
2462 : 6 : result = libspdm_hash_init (spdm_context->connection_info.algorithm.base_hash_algo,
2463 : : spdm_session_info->session_transcript.digest_context_encap_il1il2);
2464 [ - + ]: 6 : if (!result) {
2465 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
2466 : : spdm_session_info->session_transcript.digest_context_encap_il1il2);
2467 : 0 : spdm_session_info->session_transcript.digest_context_encap_il1il2 = NULL;
2468 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2469 : : }
2470 : :
2471 : 6 : result = libspdm_hash_update (
2472 : : spdm_context->connection_info.algorithm.base_hash_algo,
2473 : : spdm_session_info->session_transcript.digest_context_encap_il1il2,
2474 : 6 : libspdm_get_managed_buffer(&spdm_context->transcript.message_a),
2475 : 6 : libspdm_get_managed_buffer_size(&spdm_context->transcript.message_a));
2476 : :
2477 [ - + ]: 6 : if (!result) {
2478 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
2479 : : spdm_session_info->session_transcript.digest_context_encap_il1il2);
2480 : 0 : spdm_session_info->session_transcript.digest_context_encap_il1il2 = NULL;
2481 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2482 : : }
2483 : : }
2484 : 10 : result = libspdm_hash_update (spdm_context->connection_info.algorithm.base_hash_algo,
2485 : : spdm_session_info->session_transcript.digest_context_encap_il1il2,
2486 : : message, message_size);
2487 [ - + ]: 10 : if (!result) {
2488 : 0 : libspdm_hash_free (spdm_context->connection_info.algorithm.base_hash_algo,
2489 : : spdm_session_info->session_transcript.digest_context_encap_il1il2);
2490 : 0 : spdm_session_info->session_transcript.digest_context_encap_il1il2 = NULL;
2491 : 0 : return LIBSPDM_STATUS_CRYPTO_ERROR;
2492 : : }
2493 : : }
2494 : :
2495 : 52 : return LIBSPDM_STATUS_SUCCESS;
2496 : : }
2497 : : #endif
2498 : : }
2499 : 4 : bool libspdm_is_version_supported(const libspdm_context_t *spdm_context, uint8_t version)
2500 : : {
2501 [ + + ]: 4 : if (version == (spdm_context->connection_info.version >> SPDM_VERSION_NUMBER_SHIFT_BIT)) {
2502 : 2 : return true;
2503 : : }
2504 : :
2505 : 2 : return false;
2506 : : }
2507 : :
2508 : 29980 : uint8_t libspdm_get_connection_version(const libspdm_context_t *spdm_context)
2509 : : {
2510 : 29980 : return (uint8_t)(spdm_context->connection_info.version >> SPDM_VERSION_NUMBER_SHIFT_BIT);
2511 : : }
2512 : :
2513 : 30932 : bool libspdm_is_capabilities_flag_supported(const libspdm_context_t *spdm_context,
2514 : : bool is_requester,
2515 : : uint32_t requester_capabilities_flag,
2516 : : uint32_t responder_capabilities_flag)
2517 : : {
2518 : : uint32_t negotiated_requester_capabilities_flag;
2519 : : uint32_t negotiated_responder_capabilities_flag;
2520 : :
2521 [ + + ]: 30932 : if (is_requester) {
2522 : 19294 : negotiated_requester_capabilities_flag = spdm_context->local_context.capability.flags;
2523 : 19294 : negotiated_responder_capabilities_flag = spdm_context->connection_info.capability.flags;
2524 : : } else {
2525 : 11638 : negotiated_requester_capabilities_flag = spdm_context->connection_info.capability.flags;
2526 : 11638 : negotiated_responder_capabilities_flag = spdm_context->local_context.capability.flags;
2527 : : }
2528 : :
2529 [ + + ]: 30932 : if (((requester_capabilities_flag == 0) ||
2530 [ + + ]: 21694 : ((negotiated_requester_capabilities_flag &
2531 [ + + ]: 15890 : requester_capabilities_flag) != 0)) &&
2532 : 12528 : ((responder_capabilities_flag == 0) ||
2533 [ + + ]: 12528 : ((negotiated_responder_capabilities_flag &
2534 : : responder_capabilities_flag) != 0))) {
2535 : 13064 : return true;
2536 : : } else {
2537 : 17868 : return false;
2538 : : }
2539 : : }
2540 : :
2541 : 8 : bool libspdm_is_capabilities_ext_flag_supported(const libspdm_context_t *spdm_context,
2542 : : bool is_requester,
2543 : : uint16_t requester_capabilities_ext_flag,
2544 : : uint16_t responder_capabilities_ext_flag)
2545 : : {
2546 : : uint16_t negotiated_requester_capabilities_ext_flag;
2547 : : uint16_t negotiated_responder_capabilities_ext_flag;
2548 : :
2549 [ + + ]: 8 : if (is_requester) {
2550 : 4 : negotiated_requester_capabilities_ext_flag = spdm_context->local_context.capability.ext_flags;
2551 : 4 : negotiated_responder_capabilities_ext_flag = spdm_context->connection_info.capability.ext_flags;
2552 : : } else {
2553 : 4 : negotiated_requester_capabilities_ext_flag = spdm_context->connection_info.capability.ext_flags;
2554 : 4 : negotiated_responder_capabilities_ext_flag = spdm_context->local_context.capability.ext_flags;
2555 : : }
2556 : :
2557 [ + + ]: 8 : if (((requester_capabilities_ext_flag == 0) ||
2558 : : ((negotiated_requester_capabilities_ext_flag &
2559 [ + + + - ]: 8 : requester_capabilities_ext_flag) != 0)) &&
2560 : 6 : ((responder_capabilities_ext_flag == 0) ||
2561 : : ((negotiated_responder_capabilities_ext_flag &
2562 [ + + ]: 6 : responder_capabilities_ext_flag) != 0))) {
2563 : 4 : return true;
2564 : : } else {
2565 : 4 : return false;
2566 : : }
2567 : : }
2568 : :
2569 : 314 : bool libspdm_is_encap_supported(const libspdm_context_t *spdm_context)
2570 : : {
2571 [ + + ]: 314 : if (libspdm_get_connection_version(spdm_context) == SPDM_MESSAGE_VERSION_10) {
2572 : 2 : return false;
2573 [ + + ]: 312 : } else if (libspdm_get_connection_version(spdm_context) == SPDM_MESSAGE_VERSION_12) {
2574 : : /* ENCAP_CAP was erroneously deprecated in SPDM 1.2.0 and 1.2.1, and MUT_AUTH_CAP
2575 : : * was used in its place. In SPDM 1.2.2 and later ENCAP_CAP is undeprecated. Since
2576 : : * UpdateVersionNumber must be ignored when checking interoperability libspdm will check
2577 : : * if ENCAP_CAP or MUT_AUTH_CAP is set. */
2578 : 48 : const bool is_req_encap_cap_supported = libspdm_is_capabilities_flag_supported(
2579 : 48 : spdm_context, spdm_context->local_context.is_requester,
2580 : : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCAP_CAP, 0);
2581 : 48 : const bool is_req_mut_auth_cap_supported = libspdm_is_capabilities_flag_supported(
2582 : 48 : spdm_context, spdm_context->local_context.is_requester,
2583 : : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MUT_AUTH_CAP, 0);
2584 : 48 : const bool is_rsp_encap_cap_supported = libspdm_is_capabilities_flag_supported(
2585 : 48 : spdm_context, spdm_context->local_context.is_requester,
2586 : : 0, SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCAP_CAP);
2587 : 48 : const bool is_rsp_mut_auth_cap_supported = libspdm_is_capabilities_flag_supported(
2588 : 48 : spdm_context, spdm_context->local_context.is_requester,
2589 : : 0, SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MUT_AUTH_CAP);
2590 : :
2591 [ + + + - : 50 : return ((is_req_encap_cap_supported || is_req_mut_auth_cap_supported) &&
+ + ]
2592 [ + - ]: 2 : (is_rsp_encap_cap_supported || is_rsp_mut_auth_cap_supported));
2593 : : } else {
2594 : : /* For SPDM 1.1 and 1.3 and later only check ENCAP_CAP. */
2595 : 264 : return libspdm_is_capabilities_flag_supported(
2596 : 264 : spdm_context, spdm_context->local_context.is_requester,
2597 : : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCAP_CAP,
2598 : : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCAP_CAP);
2599 : : }
2600 : : }
2601 : :
2602 : 1098 : void libspdm_register_device_io_func(
2603 : : void *spdm_context, libspdm_device_send_message_func send_message,
2604 : : libspdm_device_receive_message_func receive_message)
2605 : : {
2606 : : libspdm_context_t *context;
2607 : :
2608 : 1098 : context = spdm_context;
2609 : 1098 : context->send_message = send_message;
2610 : 1098 : context->receive_message = receive_message;
2611 : 1098 : }
2612 : :
2613 : 1104 : void libspdm_register_device_buffer_func(
2614 : : void *spdm_context,
2615 : : uint32_t sender_buffer_size,
2616 : : uint32_t receiver_buffer_size,
2617 : : libspdm_device_acquire_sender_buffer_func acquire_sender_buffer,
2618 : : libspdm_device_release_sender_buffer_func release_sender_buffer,
2619 : : libspdm_device_acquire_receiver_buffer_func acquire_receiver_buffer,
2620 : : libspdm_device_release_receiver_buffer_func release_receiver_buffer)
2621 : : {
2622 : : libspdm_context_t *context;
2623 : :
2624 : 1104 : context = spdm_context;
2625 : 1104 : context->sender_buffer_size = sender_buffer_size;
2626 : 1104 : context->receiver_buffer_size = receiver_buffer_size;
2627 : 1104 : context->acquire_sender_buffer = acquire_sender_buffer;
2628 : 1104 : context->release_sender_buffer = release_sender_buffer;
2629 : 1104 : context->acquire_receiver_buffer = acquire_receiver_buffer;
2630 : 1104 : context->release_receiver_buffer = release_receiver_buffer;
2631 : :
2632 : 1104 : LIBSPDM_ASSERT (sender_buffer_size >=
2633 : : context->local_context.capability.transport_header_size +
2634 : : context->local_context.capability.transport_tail_size);
2635 : 1104 : sender_buffer_size -= (context->local_context.capability.transport_header_size +
2636 : 1104 : context->local_context.capability.transport_tail_size);
2637 : 1104 : LIBSPDM_ASSERT (sender_buffer_size >= SPDM_MIN_DATA_TRANSFER_SIZE_VERSION_12);
2638 : 1104 : context->local_context.capability.sender_data_transfer_size = sender_buffer_size;
2639 : :
2640 : 1104 : LIBSPDM_ASSERT(receiver_buffer_size >=
2641 : : context->local_context.capability.transport_header_size +
2642 : : context->local_context.capability.transport_tail_size);
2643 : 1104 : receiver_buffer_size -= (context->local_context.capability.transport_header_size +
2644 : 1104 : context->local_context.capability.transport_tail_size);
2645 : 1104 : LIBSPDM_ASSERT (receiver_buffer_size >= SPDM_MIN_DATA_TRANSFER_SIZE_VERSION_12);
2646 : 1104 : context->local_context.capability.data_transfer_size = receiver_buffer_size;
2647 : 1104 : }
2648 : :
2649 : 1106 : void libspdm_register_transport_layer_func(
2650 : : void *spdm_context,
2651 : : uint32_t max_spdm_msg_size,
2652 : : uint32_t transport_header_size,
2653 : : uint32_t transport_tail_size,
2654 : : libspdm_transport_encode_message_func transport_encode_message,
2655 : : libspdm_transport_decode_message_func transport_decode_message)
2656 : : {
2657 : : libspdm_context_t *context;
2658 : :
2659 : 1106 : context = spdm_context;
2660 : :
2661 : : /* fix the data_transfer_size if it is set before */
2662 [ + + ]: 1106 : if ((context->local_context.capability.data_transfer_size != 0) &&
2663 : 4 : (context->local_context.capability.data_transfer_size ==
2664 [ + + ]: 4 : context->receiver_buffer_size)) {
2665 : 2 : context->local_context.capability.data_transfer_size =
2666 : 2 : (uint32_t)(context->receiver_buffer_size -
2667 : 2 : (transport_header_size + transport_tail_size));
2668 : : }
2669 [ + + ]: 1106 : if ((context->local_context.capability.sender_data_transfer_size != 0) &&
2670 : 4 : (context->local_context.capability.sender_data_transfer_size ==
2671 [ + + ]: 4 : context->sender_buffer_size)) {
2672 : 2 : context->local_context.capability.sender_data_transfer_size =
2673 : 2 : (uint32_t)(context->sender_buffer_size -
2674 : 2 : (transport_header_size + transport_tail_size));
2675 : : }
2676 : :
2677 : 1106 : context->local_context.capability.max_spdm_msg_size = max_spdm_msg_size;
2678 : 1106 : context->local_context.capability.transport_header_size = transport_header_size;
2679 : 1106 : context->local_context.capability.transport_tail_size = transport_tail_size;
2680 : 1106 : context->transport_encode_message = transport_encode_message;
2681 : 1106 : context->transport_decode_message = transport_decode_message;
2682 : 1106 : }
2683 : :
2684 : 12 : void libspdm_register_verify_spdm_cert_chain_func(
2685 : : void *spdm_context,
2686 : : const libspdm_verify_spdm_cert_chain_func verify_spdm_cert_chain)
2687 : : {
2688 : : libspdm_context_t *context;
2689 : :
2690 : 12 : context = spdm_context;
2691 : 12 : context->local_context.verify_peer_spdm_cert_chain = verify_spdm_cert_chain;
2692 : 12 : }
2693 : :
2694 : 1116 : size_t libspdm_get_sizeof_required_scratch_buffer (void *spdm_context)
2695 : : {
2696 : : libspdm_context_t *context;
2697 : : size_t scratch_buffer_size;
2698 : :
2699 : 1116 : context = spdm_context;
2700 : 1116 : LIBSPDM_ASSERT (context->local_context.capability.max_spdm_msg_size != 0);
2701 : :
2702 : 1116 : scratch_buffer_size = libspdm_get_scratch_buffer_capacity(context);
2703 : 1116 : return scratch_buffer_size;
2704 : : }
2705 : :
2706 : 1118 : void libspdm_set_scratch_buffer (
2707 : : void *spdm_context,
2708 : : void *scratch_buffer,
2709 : : size_t scratch_buffer_size)
2710 : : {
2711 : : libspdm_context_t *context;
2712 : :
2713 : 1118 : context = spdm_context;
2714 : 1118 : LIBSPDM_ASSERT (context->local_context.capability.max_spdm_msg_size != 0);
2715 : 1118 : LIBSPDM_ASSERT (scratch_buffer_size >= libspdm_get_scratch_buffer_capacity(spdm_context));
2716 : 1118 : context->scratch_buffer = scratch_buffer;
2717 : 1118 : context->scratch_buffer_size = scratch_buffer_size;
2718 : 1118 : context->last_spdm_request = (uint8_t *)scratch_buffer +
2719 : 1118 : libspdm_get_scratch_buffer_last_spdm_request_offset(spdm_context);
2720 : : #if LIBSPDM_RESPOND_IF_READY_SUPPORT
2721 : 1118 : context->cache_spdm_request = (uint8_t *)scratch_buffer +
2722 : 1118 : libspdm_get_scratch_buffer_cache_spdm_request_offset(spdm_context);
2723 : : #endif
2724 : 1118 : }
2725 : :
2726 : 13372 : void libspdm_get_scratch_buffer (
2727 : : void *spdm_context,
2728 : : void **scratch_buffer,
2729 : : size_t *scratch_buffer_size)
2730 : : {
2731 : : libspdm_context_t *context;
2732 : :
2733 : 13372 : context = spdm_context;
2734 : 13372 : LIBSPDM_ASSERT (context->scratch_buffer != NULL);
2735 : 13372 : LIBSPDM_ASSERT (context->scratch_buffer_size >=
2736 : : libspdm_get_scratch_buffer_capacity(spdm_context));
2737 : 13372 : *scratch_buffer = context->scratch_buffer;
2738 : : /* The sections for libspdm's internal state start at a fixed offset, so the returned space ends
2739 : : * there even if the scratch buffer is larger than required. */
2740 : 13372 : *scratch_buffer_size = libspdm_get_scratch_buffer_last_spdm_request_offset(spdm_context);
2741 : 13372 : }
2742 : :
2743 : 6202 : libspdm_return_t libspdm_acquire_sender_buffer (
2744 : : libspdm_context_t *spdm_context, size_t *max_msg_size, void **msg_buf_ptr)
2745 : : {
2746 : : libspdm_return_t status;
2747 : :
2748 : 6202 : LIBSPDM_ASSERT (spdm_context->sender_buffer == NULL);
2749 : 6202 : LIBSPDM_ASSERT (spdm_context->sender_buffer_size != 0);
2750 : 6202 : status = spdm_context->acquire_sender_buffer (spdm_context, msg_buf_ptr);
2751 [ + + ]: 6202 : if (status != LIBSPDM_STATUS_SUCCESS) {
2752 : 34 : return status;
2753 : : }
2754 : 6168 : spdm_context->sender_buffer = *msg_buf_ptr;
2755 : 6168 : *max_msg_size = spdm_context->sender_buffer_size;
2756 : : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
2757 : : /* it return scratch buffer, because the requester need build message there.*/
2758 : 12336 : *msg_buf_ptr = (uint8_t *)spdm_context->scratch_buffer +
2759 : 6168 : libspdm_get_scratch_buffer_large_sender_receiver_offset(spdm_context);
2760 : 6168 : *max_msg_size = libspdm_get_scratch_buffer_large_sender_receiver_capacity(spdm_context);
2761 : : #endif
2762 : 6168 : return LIBSPDM_STATUS_SUCCESS;
2763 : : }
2764 : :
2765 : 6168 : void libspdm_release_sender_buffer (libspdm_context_t *spdm_context)
2766 : : {
2767 : 6168 : LIBSPDM_ASSERT(spdm_context->sender_buffer != NULL);
2768 : 6168 : LIBSPDM_ASSERT(spdm_context->sender_buffer_size != 0);
2769 : :
2770 : 6168 : spdm_context->release_sender_buffer (spdm_context, spdm_context->sender_buffer);
2771 : 6168 : spdm_context->sender_buffer = NULL;
2772 : 6168 : }
2773 : :
2774 : 6270 : void libspdm_get_sender_buffer (
2775 : : libspdm_context_t *spdm_context,
2776 : : void **sender_buffer,
2777 : : size_t *sender_buffer_size)
2778 : : {
2779 : 6270 : *sender_buffer = spdm_context->sender_buffer;
2780 : 6270 : *sender_buffer_size = spdm_context->sender_buffer_size;
2781 : 6270 : }
2782 : :
2783 : 6022 : libspdm_return_t libspdm_acquire_receiver_buffer (
2784 : : libspdm_context_t *spdm_context, size_t *max_msg_size, void **msg_buf_ptr)
2785 : : {
2786 : : libspdm_return_t status;
2787 : :
2788 : 6022 : LIBSPDM_ASSERT (spdm_context->receiver_buffer == NULL);
2789 : 6022 : LIBSPDM_ASSERT (spdm_context->receiver_buffer_size != 0);
2790 : 6022 : status = spdm_context->acquire_receiver_buffer (spdm_context, msg_buf_ptr);
2791 [ + + ]: 6022 : if (status != LIBSPDM_STATUS_SUCCESS) {
2792 : 34 : return status;
2793 : : }
2794 : 5988 : spdm_context->receiver_buffer = *msg_buf_ptr;
2795 : 5988 : *max_msg_size = spdm_context->receiver_buffer_size;
2796 : : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
2797 : : /* it return scratch buffer, because the requester need build message there.*/
2798 : 11976 : *msg_buf_ptr = (uint8_t *)spdm_context->scratch_buffer +
2799 : 5988 : libspdm_get_scratch_buffer_large_sender_receiver_offset(spdm_context);
2800 : 5988 : *max_msg_size = libspdm_get_scratch_buffer_large_sender_receiver_capacity(spdm_context);
2801 : : #endif
2802 : 5988 : return LIBSPDM_STATUS_SUCCESS;
2803 : : }
2804 : :
2805 : 5988 : void libspdm_release_receiver_buffer (libspdm_context_t *spdm_context)
2806 : : {
2807 : 5988 : LIBSPDM_ASSERT(spdm_context->receiver_buffer != NULL);
2808 : 5988 : LIBSPDM_ASSERT(spdm_context->receiver_buffer_size != 0);
2809 : :
2810 : 5988 : spdm_context->release_receiver_buffer (spdm_context, spdm_context->receiver_buffer);
2811 : 5988 : spdm_context->receiver_buffer = NULL;
2812 : 5988 : }
2813 : :
2814 : 8 : void libspdm_get_receiver_buffer (
2815 : : libspdm_context_t *spdm_context,
2816 : : void **receiver_buffer,
2817 : : size_t *receiver_buffer_size)
2818 : : {
2819 : 8 : *receiver_buffer = spdm_context->receiver_buffer;
2820 : 8 : *receiver_buffer_size = spdm_context->receiver_buffer_size;
2821 : 8 : }
2822 : :
2823 : 32 : void libspdm_get_last_spdm_error_struct(void *spdm_context, libspdm_error_struct_t *last_spdm_error)
2824 : : {
2825 : : libspdm_context_t *context;
2826 : :
2827 : 32 : context = spdm_context;
2828 : 32 : libspdm_copy_mem(last_spdm_error, sizeof(libspdm_error_struct_t),
2829 : 32 : &context->last_spdm_error,sizeof(libspdm_error_struct_t));
2830 : 32 : }
2831 : :
2832 : 6696 : void libspdm_set_last_spdm_error_struct(void *spdm_context, libspdm_error_struct_t *last_spdm_error)
2833 : : {
2834 : : libspdm_context_t *context;
2835 : :
2836 : 6696 : context = spdm_context;
2837 : 6696 : libspdm_copy_mem(&context->last_spdm_error, sizeof(context->last_spdm_error),
2838 : : last_spdm_error, sizeof(libspdm_error_struct_t));
2839 : 6696 : }
2840 : :
2841 : : #if LIBSPDM_FIPS_MODE
2842 : 2 : libspdm_return_t libspdm_init_fips_selftest_context(void *fips_selftest_context,
2843 : : size_t buffer_size,
2844 : : void *buffer)
2845 : : {
2846 : : libspdm_fips_selftest_context_t *context;
2847 : 2 : LIBSPDM_ASSERT(fips_selftest_context != NULL);
2848 : 1 : LIBSPDM_ASSERT(buffer_size >= LIBSPDM_FIPS_REQUIRED_BUFFER_SIZE);
2849 [ + + ]: 2 : if (buffer_size > 0) {
2850 : 1 : LIBSPDM_ASSERT(buffer != NULL);
2851 : : }
2852 : :
2853 : 2 : context = fips_selftest_context;
2854 : :
2855 : : /*Not tested for every used algo*/
2856 : 2 : context->tested_algo = 0;
2857 : : /*self_test result is false for every used algo*/
2858 : 2 : context->self_test_result = 0;
2859 : : /*The buffer provided by integrator to hold large intermediate results*/
2860 : 2 : context->selftest_buffer_size = buffer_size;
2861 : 2 : context->selftest_buffer = buffer;
2862 : :
2863 : 2 : return LIBSPDM_STATUS_SUCCESS;
2864 : : }
2865 : :
2866 : 4 : size_t libspdm_get_fips_selftest_context_size(void)
2867 : : {
2868 : : size_t size;
2869 : :
2870 : 4 : size = sizeof(libspdm_fips_selftest_context_t);
2871 : 4 : return size;
2872 : : }
2873 : :
2874 : 4 : size_t libspdm_get_fips_selftest_buffer_size(void)
2875 : : {
2876 : 4 : return LIBSPDM_FIPS_REQUIRED_BUFFER_SIZE;
2877 : : }
2878 : :
2879 : 8 : bool libspdm_import_fips_selftest_context_to_spdm_context(void *spdm_context,
2880 : : void *fips_selftest_context,
2881 : : size_t fips_selftest_context_size)
2882 : : {
2883 : : libspdm_fips_selftest_context_t *libspdm_fips_selftest_context;
2884 : : libspdm_context_t *libspdm_context;
2885 : :
2886 : 8 : libspdm_context = spdm_context;
2887 : 8 : libspdm_fips_selftest_context = fips_selftest_context;
2888 : :
2889 [ + + + + ]: 8 : if ((libspdm_context == NULL) || (libspdm_fips_selftest_context == NULL)) {
2890 : 4 : return false;
2891 : : }
2892 [ + + ]: 4 : if (fips_selftest_context_size != sizeof(libspdm_fips_selftest_context_t)) {
2893 : 2 : return false;
2894 : : }
2895 : :
2896 : 2 : libspdm_copy_mem(&(libspdm_context->fips_selftest_context),
2897 : : sizeof(libspdm_fips_selftest_context_t),
2898 : : libspdm_fips_selftest_context, sizeof(libspdm_fips_selftest_context_t));
2899 : 2 : return true;
2900 : : }
2901 : :
2902 : 8 : bool libspdm_export_fips_selftest_context_from_spdm_context(void *spdm_context,
2903 : : void *fips_selftest_context,
2904 : : size_t fips_selftest_context_size)
2905 : : {
2906 : : libspdm_fips_selftest_context_t *libspdm_fips_selftest_context;
2907 : : libspdm_context_t *libspdm_context;
2908 : :
2909 : 8 : libspdm_context = spdm_context;
2910 : 8 : libspdm_fips_selftest_context = fips_selftest_context;
2911 : :
2912 [ + + + + ]: 8 : if ((libspdm_context == NULL) || (libspdm_fips_selftest_context == NULL)) {
2913 : 4 : return false;
2914 : : }
2915 [ + + ]: 4 : if (fips_selftest_context_size != sizeof(libspdm_fips_selftest_context_t)) {
2916 : 2 : return false;
2917 : : }
2918 : :
2919 : 2 : libspdm_copy_mem(libspdm_fips_selftest_context,
2920 : : sizeof(libspdm_fips_selftest_context_t),
2921 : 2 : &(libspdm_context->fips_selftest_context),
2922 : : sizeof(libspdm_fips_selftest_context_t));
2923 : 2 : return true;
2924 : : }
2925 : :
2926 : : #endif /* LIBSPDM_FIPS_MODE */
2927 : :
2928 : 1120 : libspdm_return_t libspdm_init_context_with_secured_context(void *spdm_context,
2929 : : void **secured_contexts,
2930 : : size_t num_secured_contexts)
2931 : : {
2932 : : libspdm_context_t *context;
2933 : : size_t index;
2934 : :
2935 : 1120 : LIBSPDM_ASSERT(spdm_context != NULL);
2936 : 1120 : LIBSPDM_ASSERT(secured_contexts != NULL);
2937 : 1120 : LIBSPDM_ASSERT(num_secured_contexts == LIBSPDM_MAX_SESSION_COUNT);
2938 : :
2939 : 1120 : context = spdm_context;
2940 : 1120 : libspdm_zero_mem(context, sizeof(libspdm_context_t));
2941 : 1120 : context->version = LIBSPDM_CONTEXT_STRUCT_VERSION;
2942 : 1120 : context->transcript.message_a.max_buffer_size =
2943 : : sizeof(context->transcript.message_a.buffer);
2944 : 1120 : context->transcript.message_d.max_buffer_size =
2945 : : sizeof(context->transcript.message_d.buffer);
2946 : : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
2947 : : context->transcript.message_b.max_buffer_size =
2948 : : sizeof(context->transcript.message_b.buffer);
2949 : : context->transcript.message_c.max_buffer_size =
2950 : : sizeof(context->transcript.message_c.buffer);
2951 : : context->transcript.message_mut_b.max_buffer_size =
2952 : : sizeof(context->transcript.message_mut_b.buffer);
2953 : : context->transcript.message_mut_c.max_buffer_size =
2954 : : sizeof(context->transcript.message_mut_c.buffer);
2955 : : context->transcript.message_m.max_buffer_size =
2956 : : sizeof(context->transcript.message_m.buffer);
2957 : : context->transcript.message_e.max_buffer_size =
2958 : : sizeof(context->transcript.message_e.buffer);
2959 : : context->transcript.message_encap_e.max_buffer_size =
2960 : : sizeof(context->transcript.message_encap_e.buffer);
2961 : : #endif
2962 : 1120 : context->response_state = LIBSPDM_RESPONSE_STATE_NORMAL;
2963 : 1120 : context->local_context.version.spdm_version_count = SPDM_MAX_VERSION_COUNT;
2964 : 1120 : context->local_context.version.spdm_version[0] = SPDM_MESSAGE_VERSION_10 <<
2965 : : SPDM_VERSION_NUMBER_SHIFT_BIT;
2966 : 1120 : context->local_context.version.spdm_version[1] = SPDM_MESSAGE_VERSION_11 <<
2967 : : SPDM_VERSION_NUMBER_SHIFT_BIT;
2968 : 1120 : context->local_context.version.spdm_version[2] = SPDM_MESSAGE_VERSION_12 <<
2969 : : SPDM_VERSION_NUMBER_SHIFT_BIT;
2970 : 1120 : context->local_context.version.spdm_version[3] = SPDM_MESSAGE_VERSION_13 <<
2971 : : SPDM_VERSION_NUMBER_SHIFT_BIT;
2972 : 1120 : context->local_context.version.spdm_version[4] = SPDM_MESSAGE_VERSION_14 <<
2973 : : SPDM_VERSION_NUMBER_SHIFT_BIT;
2974 : 1120 : context->local_context.secured_message_version.secured_message_version_count =
2975 : : SECURED_SPDM_MAX_VERSION_COUNT;
2976 : 1120 : context->local_context.secured_message_version.secured_message_version[0] =
2977 : : SECURED_SPDM_VERSION_10 << SPDM_VERSION_NUMBER_SHIFT_BIT;
2978 : 1120 : context->local_context.secured_message_version.secured_message_version[1] =
2979 : : SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT;
2980 : 1120 : context->local_context.secured_message_version.secured_message_version[2] =
2981 : : SECURED_SPDM_VERSION_12 << SPDM_VERSION_NUMBER_SHIFT_BIT;
2982 : 1120 : context->local_context.secured_message_version.secured_message_version[3] =
2983 : : SECURED_SPDM_VERSION_13 << SPDM_VERSION_NUMBER_SHIFT_BIT;
2984 : 1120 : context->local_context.capability.st1 = SPDM_ST1_VALUE_US;
2985 : :
2986 : : /* DSP0277 1.3 AEAD limit: max_spdm_session_sequence_number is the single source of truth for
2987 : : * the AEAD limit. The default 0xFFFFFFFFFFFFFFFF encodes the spec-default exponent of 64. */
2988 : 1120 : context->max_spdm_session_sequence_number = LIBSPDM_MAX_SPDM_SESSION_SEQUENCE_NUMBER;
2989 : :
2990 : 1120 : context->latest_session_id = INVALID_SESSION_ID;
2991 : 1120 : context->last_spdm_request_session_id = INVALID_SESSION_ID;
2992 : 1120 : context->last_spdm_request_session_id_valid = false;
2993 : 1120 : context->last_spdm_request_size = 0;
2994 : :
2995 : : /* To be updated in libspdm_register_device_buffer_func */
2996 : 1120 : context->local_context.capability.data_transfer_size = 0;
2997 : 1120 : context->local_context.capability.sender_data_transfer_size = 0;
2998 : 1120 : context->local_context.capability.max_spdm_msg_size = 0;
2999 : :
3000 [ + + ]: 5598 : for (index = 0; index < num_secured_contexts; index++) {
3001 [ + + ]: 4480 : if (secured_contexts[index] == NULL) {
3002 : 2 : return LIBSPDM_STATUS_INVALID_PARAMETER;
3003 : : }
3004 : :
3005 : 4478 : context->session_info[index].secured_message_context = secured_contexts[index];
3006 : 4478 : libspdm_secured_message_init_context(
3007 : : context->session_info[index].secured_message_context);
3008 : : }
3009 : :
3010 : 1118 : return LIBSPDM_STATUS_SUCCESS;
3011 : : }
3012 : :
3013 : 1116 : libspdm_return_t libspdm_init_context(void *spdm_context)
3014 : : {
3015 : : libspdm_context_t *context;
3016 : : void *secured_context;
3017 : : void *secured_contexts[LIBSPDM_MAX_SESSION_COUNT];
3018 : : size_t secured_context_size;
3019 : : size_t index;
3020 : :
3021 : 1116 : LIBSPDM_ASSERT(spdm_context != NULL);
3022 : :
3023 : : /* libspdm_get_context_size() allocates space for all secured message
3024 : : * contexts. They are appended to the general SPDM context. */
3025 : 1116 : context = spdm_context;
3026 : 1116 : secured_context = (void *)((size_t)(context + 1));
3027 : 1116 : secured_context_size = libspdm_secured_message_get_context_size();
3028 : :
3029 [ + + ]: 5580 : for (index = 0; index < LIBSPDM_MAX_SESSION_COUNT; index++)
3030 : : {
3031 : 4464 : secured_contexts[index] = (uint8_t *)secured_context + secured_context_size * index;
3032 : : }
3033 : :
3034 : 1116 : return libspdm_init_context_with_secured_context(spdm_context,
3035 : : secured_contexts,
3036 : : LIBSPDM_MAX_SESSION_COUNT);
3037 : : }
3038 : :
3039 : : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
3040 : 424 : void libspdm_reset_chunk_info(libspdm_chunk_info_t *chunk_info)
3041 : : {
3042 [ + + ]: 424 : if (chunk_info->large_message != NULL) {
3043 : 12 : libspdm_zero_mem(chunk_info->large_message, chunk_info->large_message_capacity);
3044 : : }
3045 : 424 : chunk_info->chunk_in_use = false;
3046 : 424 : chunk_info->session_id_valid = false;
3047 : 424 : chunk_info->session_id = 0;
3048 : 424 : chunk_info->chunk_seq_no = 0;
3049 : 424 : chunk_info->chunk_bytes_transferred = 0;
3050 : 424 : chunk_info->large_message = NULL;
3051 : 424 : chunk_info->large_message_size = 0;
3052 : 424 : chunk_info->large_message_capacity = 0;
3053 : 424 : }
3054 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP */
3055 : :
3056 : 210 : void libspdm_reset_context(void *spdm_context)
3057 : : {
3058 : : libspdm_context_t *context;
3059 : : size_t index;
3060 : : #if !(LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT)
3061 : : uint8_t slot_index;
3062 : : #endif
3063 : :
3064 : 210 : context = spdm_context;
3065 : :
3066 : : /* Clear all information about previous connection. Local context information is preserved. */
3067 : :
3068 : : /* Need to clear session information and message transcripts before negotiated algorithm
3069 : : * information is cleared. */
3070 [ + + ]: 1050 : for (index = 0; index < LIBSPDM_MAX_SESSION_COUNT; index++)
3071 : : {
3072 : 840 : libspdm_session_info_init(context,
3073 : : &context->session_info[index],
3074 : : INVALID_SESSION_ID,
3075 : : 0,
3076 : : false);
3077 : : }
3078 : :
3079 : 210 : libspdm_reset_message_a(spdm_context);
3080 : 210 : libspdm_reset_message_d(spdm_context);
3081 : 210 : libspdm_reset_message_b(spdm_context);
3082 : 210 : libspdm_reset_message_c(spdm_context);
3083 : 210 : libspdm_reset_message_mut_b(spdm_context);
3084 : 210 : libspdm_reset_message_mut_c(spdm_context);
3085 : 210 : libspdm_reset_message_m(spdm_context, NULL);
3086 : 210 : libspdm_reset_message_e(spdm_context, NULL);
3087 : 210 : libspdm_reset_message_encap_e(spdm_context, NULL);
3088 : :
3089 : : #if !(LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT)
3090 : : /* Free the peer leaf certificate public key contexts while the negotiated
3091 : : * algorithm they were parsed under is still known; that algorithm is cleared
3092 : : * below, and these contexts are otherwise only released in
3093 : : * libspdm_deinit_context, so they leak across every re-connection. */
3094 [ + + ]: 1890 : for (slot_index = 0; slot_index < SPDM_MAX_SLOT_COUNT; slot_index++) {
3095 : 1680 : libspdm_free_peer_leaf_cert_public_key(context, slot_index);
3096 : : }
3097 : 210 : libspdm_zero_mem(context->connection_info.peer_used_cert_chain,
3098 : : sizeof(context->connection_info.peer_used_cert_chain));
3099 : : #endif
3100 : :
3101 : 210 : context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NOT_STARTED;
3102 : 210 : libspdm_zero_mem(&context->connection_info.version, sizeof(spdm_version_number_t));
3103 : 210 : libspdm_zero_mem(&context->connection_info.capability,
3104 : : sizeof(libspdm_device_capability_t));
3105 : 210 : libspdm_zero_mem(&context->connection_info.algorithm, sizeof(libspdm_device_algorithm_t));
3106 : 210 : libspdm_zero_mem(&context->last_spdm_error, sizeof(libspdm_error_struct_t));
3107 : : #if LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP
3108 : 210 : libspdm_zero_mem(&context->encap_context, sizeof(libspdm_encap_context_t));
3109 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP */
3110 : : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
3111 : 210 : libspdm_reset_chunk_info(&context->chunk_context.get);
3112 : 210 : libspdm_reset_chunk_info(&context->chunk_context.send);
3113 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP */
3114 : 210 : context->connection_info.multi_key_conn_req = false;
3115 : 210 : context->connection_info.multi_key_conn_rsp = false;
3116 : 210 : context->connection_info.peer_provisioned_slot_mask = 0;
3117 : 210 : context->connection_info.peer_supported_slot_mask = 0;
3118 : 210 : libspdm_zero_mem(context->connection_info.peer_key_pair_id,
3119 : : sizeof(context->connection_info.peer_key_pair_id));
3120 : 210 : libspdm_zero_mem(context->connection_info.peer_cert_info,
3121 : : sizeof(context->connection_info.peer_cert_info));
3122 : 210 : libspdm_zero_mem(context->connection_info.peer_key_usage_bit_mask,
3123 : : sizeof(context->connection_info.peer_key_usage_bit_mask));
3124 : : #if LIBSPDM_RESPOND_IF_READY_SUPPORT
3125 : 210 : context->cache_spdm_request_size = 0;
3126 : 210 : context->cache_spdm_request_session_id_valid = false;
3127 : 210 : context->cache_spdm_request_session_id = INVALID_SESSION_ID;
3128 : : /* The cached request is plaintext even when the original request arrived in
3129 : : * a session, so erase it rather than leave it in the scratch buffer for the
3130 : : * next connection. */
3131 [ + - ]: 210 : if (context->cache_spdm_request != NULL) {
3132 : 210 : libspdm_zero_mem(context->cache_spdm_request,
3133 : 210 : libspdm_get_scratch_buffer_cache_spdm_request_capacity(context));
3134 : : }
3135 : : #endif
3136 : 210 : libspdm_zero_mem(&context->error_data, sizeof(context->error_data));
3137 : 210 : context->response_state = LIBSPDM_RESPONSE_STATE_NORMAL;
3138 : 210 : context->current_token = 0;
3139 : 210 : context->latest_session_id = INVALID_SESSION_ID;
3140 : 210 : context->last_spdm_request_session_id = INVALID_SESSION_ID;
3141 : 210 : context->last_spdm_request_session_id_valid = false;
3142 : 210 : context->last_spdm_request_size = 0;
3143 : : #if LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP
3144 : 210 : context->encap_context.payload_buffer_size = 0;
3145 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP */
3146 : 210 : context->current_dhe_session_count = 0;
3147 : 210 : context->current_psk_session_count = 0;
3148 : 210 : context->spdm_10_11_verify_signature_endian =
3149 : 210 : context->spdm_10_11_verify_signature_endian_setting;
3150 : 210 : }
3151 : :
3152 : 1102 : void libspdm_deinit_context(void *spdm_context)
3153 : : {
3154 : : uint32_t session_id;
3155 : : libspdm_context_t *context;
3156 : : libspdm_session_info_t *session_info;
3157 : : #if !(LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT)
3158 : : uint8_t slot_index;
3159 : : #endif
3160 : :
3161 : 1102 : context = spdm_context;
3162 : :
3163 : : #if !(LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT)
3164 [ + + ]: 9918 : for (slot_index = 0; slot_index < SPDM_MAX_SLOT_COUNT; slot_index++) {
3165 : 8816 : libspdm_free_peer_leaf_cert_public_key(context, slot_index);
3166 : : }
3167 : : #endif
3168 : :
3169 : 1102 : libspdm_reset_message_a(context);
3170 : 1102 : libspdm_reset_message_d(context);
3171 : 1102 : libspdm_reset_message_b(context);
3172 : 1102 : libspdm_reset_message_c(context);
3173 : 1102 : libspdm_reset_message_mut_b(context);
3174 : 1102 : libspdm_reset_message_mut_c(context);
3175 : 1102 : libspdm_reset_message_m(spdm_context, NULL);
3176 : 1102 : libspdm_reset_message_e(spdm_context, NULL);
3177 : 1102 : libspdm_reset_message_encap_e(spdm_context, NULL);
3178 [ + + ]: 5510 : for (session_id = 0; session_id < LIBSPDM_MAX_SESSION_COUNT; session_id++) {
3179 : 4408 : session_info = &context->session_info[session_id];
3180 : 4408 : libspdm_reset_message_m(context, session_info);
3181 : 4408 : libspdm_reset_message_e(context, session_info);
3182 : 4408 : libspdm_reset_message_encap_e(context, session_info);
3183 : 4408 : libspdm_reset_message_encap_d(session_info);
3184 : 4408 : libspdm_reset_message_k(context, session_info);
3185 : 4408 : libspdm_reset_message_f(context, session_info);
3186 : : }
3187 : 1102 : }
3188 : :
3189 : 1116 : size_t libspdm_get_context_size(void)
3190 : : {
3191 : : size_t size;
3192 : :
3193 : 1116 : size = sizeof(libspdm_context_t) +
3194 : 1116 : libspdm_secured_message_get_context_size() * LIBSPDM_MAX_SESSION_COUNT;
3195 : 1116 : LIBSPDM_ASSERT (size == LIBSPDM_CONTEXT_SIZE_ALL);
3196 : 1116 : return size;
3197 : : }
3198 : :
3199 : 4 : size_t libspdm_get_context_size_without_secured_context(void)
3200 : : {
3201 : : size_t size;
3202 : :
3203 : 4 : size = sizeof(libspdm_context_t);
3204 : 4 : LIBSPDM_ASSERT (size == LIBSPDM_CONTEXT_SIZE_WITHOUT_SECURED_CONTEXT);
3205 : 4 : return size;
3206 : : }
3207 : :
3208 : 3088 : uint8_t libspdm_get_version_from_version_number(spdm_version_number_t ver)
3209 : : {
3210 : 3088 : return (uint8_t)(ver >> SPDM_VERSION_NUMBER_SHIFT_BIT);
3211 : : }
3212 : :
3213 : 290 : void libspdm_version_number_sort(spdm_version_number_t *ver_set, size_t ver_num)
3214 : : {
3215 : : size_t index;
3216 : : size_t index_sort;
3217 : : size_t index_max;
3218 : : spdm_version_number_t version;
3219 : :
3220 : : /* Selection sort */
3221 [ + + ]: 290 : if (ver_num > 1) {
3222 [ + + ]: 578 : for (index_sort = 0; index_sort < ver_num; index_sort++) {
3223 : 462 : index_max = index_sort;
3224 [ + + ]: 1206 : for (index = index_sort + 1; index < ver_num; index++) {
3225 : : /* if ver_set[index] higher than ver_set[index_max] */
3226 [ + + ]: 744 : if (ver_set[index] > ver_set[index_max]) {
3227 : 440 : index_max = index;
3228 : : }
3229 : : }
3230 : : /* swap ver_set[index_max] and ver_set[index_sort] */
3231 : 462 : version = ver_set[index_sort];
3232 : 462 : ver_set[index_sort] = ver_set[index_max];
3233 : 462 : ver_set[index_max] = version;
3234 : : }
3235 : : }
3236 : 290 : }
3237 : :
3238 : 148 : bool libspdm_negotiate_connection_version(spdm_version_number_t *common_version,
3239 : : spdm_version_number_t *req_ver_set,
3240 : : size_t req_ver_num,
3241 : : const spdm_version_number_t *res_ver_set,
3242 : : size_t res_ver_num)
3243 : : {
3244 : : spdm_version_number_t req_version_list[LIBSPDM_MAX_VERSION_COUNT];
3245 : : spdm_version_number_t res_version_list[LIBSPDM_MAX_VERSION_COUNT];
3246 : : size_t req_index;
3247 : : size_t res_index;
3248 : :
3249 [ + + - + ]: 148 : if (req_ver_num > LIBSPDM_MAX_VERSION_COUNT || res_ver_num > LIBSPDM_MAX_VERSION_COUNT) {
3250 : 2 : return false;
3251 : : }
3252 : :
3253 [ + - + - : 146 : if (req_ver_set == NULL || req_ver_num == 0 || res_ver_set == NULL || res_ver_num == 0) {
+ + - + ]
3254 : 2 : return false;
3255 : : }
3256 : :
3257 : 144 : libspdm_zero_mem(req_version_list, sizeof(spdm_version_number_t) * LIBSPDM_MAX_VERSION_COUNT);
3258 : 144 : libspdm_zero_mem(res_version_list, sizeof(spdm_version_number_t) * LIBSPDM_MAX_VERSION_COUNT);
3259 : :
3260 : 144 : libspdm_copy_mem(req_version_list, sizeof(spdm_version_number_t) * LIBSPDM_MAX_VERSION_COUNT,
3261 : : req_ver_set, sizeof(spdm_version_number_t) * req_ver_num);
3262 : 144 : libspdm_copy_mem(res_version_list, sizeof(spdm_version_number_t) * LIBSPDM_MAX_VERSION_COUNT,
3263 : : res_ver_set, sizeof(spdm_version_number_t) * res_ver_num);
3264 : :
3265 : : /* Sort SPDMversion in descending order. */
3266 : 144 : libspdm_version_number_sort(req_version_list, req_ver_num);
3267 : 144 : libspdm_version_number_sort(res_version_list, res_ver_num);
3268 : :
3269 : : /**
3270 : : * Find highest same version and make req_index point to it.
3271 : : * If not found, return false.
3272 : : **/
3273 [ + + ]: 164 : for (res_index = 0; res_index < res_ver_num; res_index++) {
3274 [ + + ]: 324 : for (req_index = 0; req_index < req_ver_num; req_index++) {
3275 [ + + ]: 608 : if (libspdm_get_version_from_version_number(req_version_list[req_index]) ==
3276 : 304 : libspdm_get_version_from_version_number(res_version_list[res_index])) {
3277 : 140 : *common_version = req_version_list[req_index];
3278 : 140 : return true;
3279 : : }
3280 : : }
3281 : : }
3282 : 4 : return false;
3283 : : }
3284 : :
3285 : : #if LIBSPDM_EVENT_RECIPIENT_SUPPORT
3286 : 40 : void libspdm_register_event_callback(void *context,
3287 : : libspdm_process_event_func process_event_func)
3288 : : {
3289 : : libspdm_context_t *spdm_context;
3290 : :
3291 : 40 : spdm_context = context;
3292 : 40 : spdm_context->process_event = process_event_func;
3293 : 40 : }
3294 : : #endif /* LIBSPDM_EVENT_RECIPIENT_SUPPORT */
|