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_requester_lib.h"
8 : :
9 : : /**
10 : : * This function validates the Responder's capabilities.
11 : : *
12 : : * @param capabilities_flag The Responder's CAPABILITIES.Flags field.
13 : : * @param version The SPDM message version.
14 : : *
15 : : * @retval true The field is valid.
16 : : * @retval false The field is invalid.
17 : : **/
18 : 120 : static bool validate_responder_capability(uint32_t capabilities_flag, uint8_t version)
19 : : {
20 : : /*uint8_t cache_cap = (uint8_t)(capabilities_flag)&0x01;*/
21 : 120 : const uint8_t cert_cap = (uint8_t)(capabilities_flag >> 1) & 0x01;
22 : 120 : const uint8_t chal_cap = (uint8_t)(capabilities_flag >> 2) & 0x01;
23 : 120 : const uint8_t meas_cap = (uint8_t)(capabilities_flag >> 3) & 0x03;
24 : 120 : const uint8_t meas_fresh_cap = (uint8_t)(capabilities_flag >> 5) & 0x01;
25 : 120 : const uint8_t encrypt_cap = (uint8_t)(capabilities_flag >> 6) & 0x01;
26 : 120 : const uint8_t mac_cap = (uint8_t)(capabilities_flag >> 7) & 0x01;
27 : 120 : const uint8_t mut_auth_cap = (uint8_t)(capabilities_flag >> 8) & 0x01;
28 : 120 : const uint8_t key_ex_cap = (uint8_t)(capabilities_flag >> 9) & 0x01;
29 : 120 : const uint8_t psk_cap = (uint8_t)(capabilities_flag >> 10) & 0x03;
30 : 120 : const uint8_t encap_cap = (uint8_t)(capabilities_flag >> 12) & 0x01;
31 : 120 : const uint8_t hbeat_cap = (uint8_t)(capabilities_flag >> 13) & 0x01;
32 : 120 : const uint8_t key_upd_cap = (uint8_t)(capabilities_flag >> 14) & 0x01;
33 : 120 : const uint8_t handshake_in_the_clear_cap = (uint8_t)(capabilities_flag >> 15) & 0x01;
34 : 120 : const uint8_t pub_key_id_cap = (uint8_t)(capabilities_flag >> 16) & 0x01;
35 : : /* uint8_t chunk_cap = (uint8_t)(capabilities_flag >> 17) & 0x01; */
36 : 120 : const uint8_t alias_cert_cap = (uint8_t)(capabilities_flag >> 18) & 0x01;
37 : 120 : const uint8_t set_cert_cap = (uint8_t)(capabilities_flag >> 19) & 0x01;
38 : 120 : const uint8_t csr_cap = (uint8_t)(capabilities_flag >> 20) & 0x01;
39 : 120 : const uint8_t cert_install_reset_cap = (uint8_t)(capabilities_flag >> 21) & 0x01;
40 : 120 : const uint8_t ep_info_cap = (uint8_t)(capabilities_flag >> 22) & 0x03;
41 : : /* const uint8_t mel_cap = (uint8_t)(capabilities_flag >> 24) & 0x01; */
42 : 120 : const uint8_t event_cap = (uint8_t)(capabilities_flag >> 25) & 0x01;
43 : 120 : const uint8_t multi_key_cap = (uint8_t)(capabilities_flag >> 26) & 0x03;
44 : 120 : const uint8_t get_key_pair_info_cap = (uint8_t)(capabilities_flag >> 28) & 0x01;
45 : 120 : const uint8_t set_key_pair_info_cap = (uint8_t)(capabilities_flag >> 29) & 0x01;
46 : 120 : const uint8_t set_key_pair_reset_cap = (uint8_t)(capabilities_flag >> 30) & 0x01;
47 : : /* const uint8_t large_resp_cap = (uint8_t)(capabilities_flag >> 31) & 0x01; */
48 : :
49 : : /* Checks common to all SPDM versions. */
50 : :
51 : : /* Illegal to return reserved value. */
52 [ + + ]: 120 : if (meas_cap == 3) {
53 : 2 : return false;
54 : : }
55 : :
56 : : /* If MEAS_FRESH_CAP is set then MEAS_CAP must be set. */
57 [ + + + + ]: 118 : if ((meas_cap == 0) && (meas_fresh_cap == 1)) {
58 : 2 : return false;
59 : : }
60 : :
61 [ + + ]: 116 : if (version == SPDM_MESSAGE_VERSION_10) {
62 : : /* If measurements are not signed then CERT_CAP must equal CHAL_CAP.
63 : : * If measurements are signed then CERT_CAP must be set. */
64 [ + + + + ]: 18 : if ((meas_cap == 0) || (meas_cap == 1)) {
65 [ + + ]: 10 : if (cert_cap != chal_cap) {
66 : 2 : return false;
67 : : }
68 [ + - ]: 8 : } else if (meas_cap == 2) {
69 [ + + ]: 8 : if (cert_cap == 0) {
70 : 2 : return false;
71 : : }
72 : : }
73 : 14 : return true;
74 : : }
75 : :
76 : : /* Checks common to 1.1 and higher. */
77 [ + - ]: 98 : if (version >= SPDM_MESSAGE_VERSION_11) {
78 : : /* Illegal to return reserved values. */
79 [ + + ]: 98 : if (psk_cap == 3) {
80 : 2 : return false;
81 : : }
82 : :
83 : : /* Checks that originate from key exchange capabilities. */
84 [ + + + + ]: 96 : if ((key_ex_cap == 1) || (psk_cap != 0)) {
85 [ + + + + ]: 42 : if ((mac_cap == 0) && (encrypt_cap == 0)) {
86 : 10 : return false;
87 : : }
88 : : } else {
89 [ + + + + : 54 : if ((mac_cap == 1) || (encrypt_cap == 1) || (handshake_in_the_clear_cap == 1) ||
+ - + - ]
90 [ - + ]: 48 : (hbeat_cap == 1) || (key_upd_cap == 1)) {
91 : 6 : return false;
92 : : }
93 [ + + ]: 48 : if (version >= SPDM_MESSAGE_VERSION_13) {
94 [ + + ]: 22 : if (event_cap == 1) {
95 : 2 : return false;
96 : : }
97 : : }
98 : : }
99 [ + + + + ]: 78 : if ((key_ex_cap == 0) && (psk_cap != 0)) {
100 [ + - ]: 4 : if (handshake_in_the_clear_cap == 1) {
101 : 4 : return false;
102 : : }
103 : : }
104 : :
105 : : /* Checks that originate from certificate or public key capabilities. */
106 [ + + + + ]: 74 : if ((cert_cap == 1) || (pub_key_id_cap == 1)) {
107 : : /* Certificate capabilities and public key capabilities cannot both be set. */
108 [ + + + + ]: 60 : if ((cert_cap == 1) && (pub_key_id_cap == 1)) {
109 : 4 : return false;
110 : : }
111 : : /* If certificates or public keys are enabled then at least one of these capabilities
112 : : * must be enabled to use the key. */
113 [ + + + - : 56 : if ((chal_cap == 0) && (key_ex_cap == 0) && ((meas_cap == 0) || (meas_cap == 1))) {
- + - - ]
114 [ + + ]: 16 : if (version >= SPDM_MESSAGE_VERSION_13) {
115 [ + + - + ]: 14 : if ((ep_info_cap == 0) || (ep_info_cap == 1)) {
116 : 4 : return false;
117 : : }
118 : : } else {
119 : 2 : return false;
120 : : }
121 : : }
122 : : } else {
123 : : /* If certificates or public keys are not enabled then these capabilities
124 : : * cannot be enabled. */
125 [ + - + + : 14 : if ((chal_cap == 1) || (key_ex_cap == 1) || (meas_cap == 2) || (mut_auth_cap == 1)) {
+ - - + ]
126 : 2 : return false;
127 : : }
128 [ + + ]: 12 : if (version >= SPDM_MESSAGE_VERSION_13) {
129 [ + - ]: 2 : if (ep_info_cap == 2) {
130 : 2 : return false;
131 : : }
132 : : }
133 : : }
134 : :
135 : : /* Checks that originate from mutual authentication capabilities. */
136 [ + + ]: 60 : if (mut_auth_cap == 1) {
137 : : /* Mutual authentication with asymmetric keys can only occur through the basic mutual
138 : : * authentication flow (CHAL_CAP == 1) or the session-based mutual authentication flow
139 : : * (KEY_EX_CAP == 1). */
140 [ + + + - ]: 24 : if ((key_ex_cap == 0) && (chal_cap == 0)) {
141 : 2 : return false;
142 : : }
143 : : }
144 : : }
145 : :
146 : : /* Checks specific to 1.1. */
147 [ + + ]: 58 : if (version == SPDM_MESSAGE_VERSION_11) {
148 [ + - + + ]: 12 : if ((mut_auth_cap == 1) && (encap_cap == 0)) {
149 : 4 : return false;
150 : : }
151 : : }
152 : :
153 : : /* Checks common to 1.2 and higher. */
154 [ + + ]: 54 : if (version >= SPDM_MESSAGE_VERSION_12) {
155 [ + + + + : 46 : if ((cert_cap == 0) && ((alias_cert_cap == 1) || (set_cert_cap == 1))) {
- + ]
156 : 4 : return false;
157 : : }
158 [ + + + - ]: 42 : if ((csr_cap == 1) && (set_cert_cap == 0)) {
159 : 2 : return false;
160 : : }
161 [ + + + - : 40 : if ((cert_install_reset_cap == 1) && (csr_cap == 0) && (set_cert_cap == 0)) {
+ - ]
162 : 2 : return false;
163 : : }
164 : : }
165 : :
166 : : /* Checks specific to 1.3 and higher. */
167 [ + + ]: 46 : if (version >= SPDM_MESSAGE_VERSION_13) {
168 : : /* Illegal to return reserved values. */
169 [ + + - + ]: 22 : if ((ep_info_cap == 3) || (multi_key_cap == 3)) {
170 : 2 : return false;
171 : : }
172 [ + + + + : 20 : if ((multi_key_cap != 0) && ((get_key_pair_info_cap == 0) || (cert_cap == 0))) {
- + ]
173 : 4 : return false;
174 : : }
175 [ - + ]: 16 : if (pub_key_id_cap == 1) {
176 [ # # # # : 0 : if ((multi_key_cap != 0) || (get_key_pair_info_cap == 1) ||
# # ]
177 : : (set_key_pair_info_cap == 1)) {
178 : 0 : return false;
179 : : }
180 : : }
181 : : }
182 : :
183 : : /* Checks that are deferred to when a message is sent.
184 : : *
185 : : * If the Responder supports key exchange then MAC_CAP must be set. In addition, if the
186 : : * negotiated SPDM version is greater than 1.1 then the negotiated opaque data format must be
187 : : * OpaqueDataFmt1.
188 : : */
189 : :
190 : : /* Checks specific to 1.4 and higher. */
191 [ + + ]: 40 : if (version >= SPDM_MESSAGE_VERSION_14) {
192 [ + + + - ]: 10 : if ((set_key_pair_reset_cap == 1) && (set_key_pair_info_cap == 0)) {
193 : 2 : return false;
194 : : }
195 : : }
196 : :
197 : 38 : return true;
198 : : }
199 : :
200 : : /**
201 : : * This function sends GET_CAPABILITIES and receives CAPABILITIES.
202 : : *
203 : : * @param spdm_context A pointer to the SPDM context.
204 : : *
205 : : * @retval LIBSPDM_STATUS_SUCCESS
206 : : * GET_CAPABILITIES was sent and CAPABILITIES was received.
207 : : * @retval LIBSPDM_STATUS_INVALID_STATE_LOCAL
208 : : * Cannot send GET_CAPABILITIES due to Requester's state. Send GET_VERSION first.
209 : : * @retval LIBSPDM_STATUS_INVALID_MSG_SIZE
210 : : * The size of the CAPABILITIES response is invalid.
211 : : * @retval LIBSPDM_STATUS_INVALID_MSG_FIELD
212 : : * The CAPABILITIES response contains one or more invalid fields.
213 : : * @retval LIBSPDM_STATUS_ERROR_PEER
214 : : * The Responder returned an unexpected error.
215 : : * @retval LIBSPDM_STATUS_BUSY_PEER
216 : : * The Responder continually returned Busy error messages.
217 : : * @retval LIBSPDM_STATUS_RESYNCH_PEER
218 : : * The Responder returned a RequestResynch error message.
219 : : * @retval LIBSPDM_STATUS_BUFFER_FULL
220 : : * The buffer used to store transcripts is exhausted.
221 : : **/
222 : 266 : static libspdm_return_t libspdm_try_get_capabilities(libspdm_context_t *spdm_context,
223 : : size_t *supported_algs_length,
224 : : void *supported_algs)
225 : : {
226 : : libspdm_return_t status;
227 : : spdm_get_capabilities_request_t *spdm_request;
228 : : size_t spdm_request_size;
229 : : spdm_capabilities_response_t *spdm_response;
230 : : size_t spdm_response_size;
231 : : uint8_t *message;
232 : : size_t message_size;
233 : : size_t transport_header_size;
234 : :
235 : : /* -=[Verify State Phase]=- */
236 [ + + ]: 266 : if (spdm_context->connection_info.connection_state != LIBSPDM_CONNECTION_STATE_AFTER_VERSION) {
237 : 4 : return LIBSPDM_STATUS_INVALID_STATE_LOCAL;
238 : : }
239 : 262 : libspdm_reset_message_buffer_via_request_code(spdm_context, NULL, SPDM_GET_CAPABILITIES);
240 : :
241 : : /* -=[Construct Request Phase]=- */
242 : 262 : transport_header_size = spdm_context->local_context.capability.transport_header_size;
243 : 262 : status = libspdm_acquire_sender_buffer (spdm_context, &message_size, (void **)&message);
244 [ + + ]: 262 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
245 : 2 : return status;
246 : : }
247 : 260 : LIBSPDM_ASSERT (message_size >= transport_header_size +
248 : : spdm_context->local_context.capability.transport_tail_size);
249 : 260 : spdm_request = (void *)(message + transport_header_size);
250 : 260 : spdm_request_size = message_size - transport_header_size -
251 : 260 : spdm_context->local_context.capability.transport_tail_size;
252 : :
253 : 260 : LIBSPDM_ASSERT (spdm_request_size >= sizeof(spdm_request->header));
254 : :
255 : 260 : libspdm_zero_mem(spdm_request, spdm_request_size);
256 : 260 : spdm_request->header.spdm_version = libspdm_get_connection_version (spdm_context);
257 [ + + ]: 260 : if (spdm_request->header.spdm_version >= SPDM_MESSAGE_VERSION_12) {
258 : 74 : LIBSPDM_ASSERT (spdm_request_size >= sizeof(spdm_get_capabilities_request_t));
259 : 74 : spdm_request_size = sizeof(spdm_get_capabilities_request_t);
260 [ + + ]: 186 : } else if (spdm_request->header.spdm_version >= SPDM_MESSAGE_VERSION_11) {
261 : 122 : LIBSPDM_ASSERT (spdm_request_size >= sizeof(spdm_get_capabilities_request_t) -
262 : : sizeof(spdm_request->data_transfer_size) -
263 : : sizeof(spdm_request->max_spdm_msg_size));
264 : 122 : spdm_request_size = sizeof(spdm_get_capabilities_request_t) -
265 : : sizeof(spdm_request->data_transfer_size) -
266 : : sizeof(spdm_request->max_spdm_msg_size);
267 : : } else {
268 : 64 : spdm_request_size = sizeof(spdm_request->header);
269 : : }
270 : 260 : spdm_request->header.request_response_code = SPDM_GET_CAPABILITIES;
271 : 260 : spdm_request->header.param1 = 0;
272 : 260 : spdm_request->header.param2 = 0;
273 [ + + ]: 260 : if (spdm_request->header.spdm_version >= SPDM_MESSAGE_VERSION_11) {
274 : 196 : spdm_request->ct_exponent = spdm_context->local_context.capability.ct_exponent;
275 : 196 : spdm_request->flags =
276 : 196 : libspdm_mask_capability_flags(spdm_context, true,
277 : : spdm_context->local_context.capability.flags);
278 : : }
279 : :
280 [ + + ]: 260 : if (supported_algs != NULL) {
281 : 24 : LIBSPDM_ASSERT((spdm_request->header.spdm_version >= SPDM_MESSAGE_VERSION_13) &&
282 : : ((spdm_request->flags & SPDM_GET_CAPABILITIES_REQUEST_FLAGS_CHUNK_CAP) != 0));
283 : :
284 : 24 : spdm_request->header.param1 |= SPDM_GET_CAPABILITIES_REQUEST_PARAM1_SUPPORTED_ALGORITHMS;
285 : : }
286 : :
287 [ + + ]: 260 : if (spdm_request->header.spdm_version >= SPDM_MESSAGE_VERSION_12) {
288 : 74 : spdm_request->data_transfer_size =
289 : 74 : spdm_context->local_context.capability.data_transfer_size;
290 : 74 : spdm_request->max_spdm_msg_size =
291 : 74 : spdm_context->local_context.capability.max_spdm_msg_size;
292 : : }
293 [ + + ]: 260 : if (spdm_request->header.spdm_version >= SPDM_MESSAGE_VERSION_14) {
294 : 14 : spdm_request->ext_flags =
295 : 14 : libspdm_mask_capability_ext_flags(spdm_context, true,
296 : 14 : spdm_context->local_context.capability.ext_flags);
297 : : } else {
298 : 246 : spdm_request->ext_flags = 0;
299 : : }
300 : :
301 : : /* -=[Send Request Phase]=- */
302 : 260 : status = libspdm_send_spdm_request(spdm_context, NULL, spdm_request_size, spdm_request);
303 [ + + ]: 260 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
304 : 4 : libspdm_release_sender_buffer (spdm_context);
305 : 4 : return status;
306 : : }
307 : 256 : libspdm_release_sender_buffer (spdm_context);
308 : 256 : spdm_request = (void *)spdm_context->last_spdm_request;
309 : :
310 : : /* -=[Receive Response Phase]=- */
311 : 256 : status = libspdm_acquire_receiver_buffer (spdm_context, &message_size, (void **)&message);
312 [ + + ]: 256 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
313 : 2 : return status;
314 : : }
315 : 254 : LIBSPDM_ASSERT (message_size >= transport_header_size);
316 : 254 : spdm_response = (void *)(message);
317 : 254 : spdm_response_size = message_size;
318 : :
319 : 254 : status = libspdm_receive_spdm_response(spdm_context, NULL, &spdm_response_size,
320 : : (void **)&spdm_response);
321 [ + + ]: 254 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
322 : 16 : goto receive_done;
323 : : }
324 : :
325 : : /* -=[Validate Response Phase]=- */
326 [ - + ]: 238 : if (spdm_response_size < sizeof(spdm_message_header_t)) {
327 : 0 : status = LIBSPDM_STATUS_INVALID_MSG_SIZE;
328 : 0 : goto receive_done;
329 : : }
330 [ + + ]: 238 : if (spdm_response->header.request_response_code == SPDM_ERROR) {
331 : : /* SPDM 1.0 does not allow ResponseNotReady in response to GET_CAPABILITIES. */
332 [ + + ]: 92 : if (spdm_request->header.spdm_version >= SPDM_MESSAGE_VERSION_11) {
333 : 76 : status = libspdm_handle_error_response_main(
334 : : spdm_context, NULL, &spdm_response_size,
335 : : (void **)&spdm_response, SPDM_GET_CAPABILITIES, SPDM_CAPABILITIES);
336 : : } else {
337 : 16 : status = libspdm_handle_simple_error_response(
338 : 16 : spdm_context, spdm_response->header.param1);
339 : : }
340 [ + + ]: 92 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
341 : 90 : goto receive_done;
342 : : }
343 [ + + ]: 146 : } else if (spdm_response->header.request_response_code != SPDM_CAPABILITIES) {
344 : 4 : status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
345 : 4 : goto receive_done;
346 : : }
347 [ + + ]: 144 : if (spdm_response->header.spdm_version != spdm_request->header.spdm_version) {
348 : 10 : status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
349 : 10 : goto receive_done;
350 : : }
351 [ + + ]: 134 : if (spdm_response->header.spdm_version >= SPDM_MESSAGE_VERSION_12) {
352 [ + + ]: 66 : if (spdm_response_size < sizeof(spdm_capabilities_response_t)) {
353 : 2 : status = LIBSPDM_STATUS_INVALID_MSG_SIZE;
354 : 2 : goto receive_done;
355 : : }
356 : : } else {
357 [ + + ]: 68 : if (spdm_response_size < sizeof(spdm_capabilities_response_t) -
358 : : sizeof(spdm_response->data_transfer_size) - sizeof(spdm_response->max_spdm_msg_size)) {
359 : 4 : status = LIBSPDM_STATUS_INVALID_MSG_SIZE;
360 : 4 : goto receive_done;
361 : : }
362 : : }
363 : :
364 [ + + ]: 128 : if ((spdm_response->header.spdm_version >= SPDM_MESSAGE_VERSION_13) &&
365 [ + + ]: 40 : (spdm_request->header.param1 & SPDM_GET_CAPABILITIES_REQUEST_PARAM1_SUPPORTED_ALGORITHMS) &&
366 [ + + ]: 20 : (spdm_response->header.param1 & SPDM_CAPABILITIES_RESPONSE_PARAM1_SUPPORTED_ALGORITHMS)) {
367 : : spdm_supported_algorithms_block_t *supported_algorithms;
368 : : uint32_t expected_block_length;
369 : :
370 : : /* Minimum size guard: must fit the fixed block header before reading length/counts. */
371 [ - + ]: 18 : if (spdm_response_size < sizeof(spdm_capabilities_response_t) +
372 : : sizeof(spdm_supported_algorithms_block_t)) {
373 : 0 : status = LIBSPDM_STATUS_INVALID_MSG_SIZE;
374 : 0 : goto receive_done;
375 : : }
376 : :
377 : 18 : supported_algorithms = (spdm_supported_algorithms_block_t *)(
378 : 18 : (uint8_t *)spdm_response + sizeof(spdm_capabilities_response_t));
379 : :
380 : : /* Block length check: must cover at least the fixed part and not exceed response. */
381 [ + + ]: 18 : if (supported_algorithms->length < sizeof(spdm_supported_algorithms_block_t)) {
382 : 4 : status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
383 : 4 : goto receive_done;
384 : : }
385 : 14 : if (supported_algorithms->length >
386 [ - + ]: 14 : spdm_response_size - sizeof(spdm_capabilities_response_t)) {
387 : 0 : status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
388 : 0 : goto receive_done;
389 : : }
390 : :
391 : : /* Length/count coherence: length must equal fixed block + all dynamic sub-arrays. */
392 : 14 : expected_block_length =
393 : : (uint32_t)sizeof(spdm_supported_algorithms_block_t) +
394 : 14 : (uint32_t)supported_algorithms->ext_asym_count * sizeof(spdm_extended_algorithm_t) +
395 : 14 : (uint32_t)supported_algorithms->ext_hash_count * sizeof(spdm_extended_algorithm_t) +
396 : 14 : (uint32_t)supported_algorithms->param1 *
397 : : sizeof(spdm_negotiate_algorithms_common_struct_table_t);
398 [ + + ]: 14 : if (supported_algorithms->length != expected_block_length) {
399 : 4 : status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
400 : 4 : goto receive_done;
401 : : }
402 : :
403 : 10 : spdm_response_size = sizeof(spdm_capabilities_response_t) + supported_algorithms->length;
404 : :
405 [ + + ]: 110 : } else if ((spdm_response->header.spdm_version >= SPDM_MESSAGE_VERSION_13) &&
406 [ + + ]: 22 : (spdm_request->header.param1 &
407 : 2 : SPDM_GET_CAPABILITIES_REQUEST_PARAM1_SUPPORTED_ALGORITHMS) &&
408 [ + - ]: 2 : !(spdm_response->header.param1 &
409 : : SPDM_CAPABILITIES_RESPONSE_PARAM1_SUPPORTED_ALGORITHMS)) {
410 : : /* Requester requested SupportedAlgorithms but responder cleared the bit:
411 : : * optional responder support - accept and treat as base CAPABILITIES size. */
412 : 2 : spdm_response_size = sizeof(spdm_capabilities_response_t);
413 [ + + ]: 108 : } else if (spdm_request->header.spdm_version >= SPDM_MESSAGE_VERSION_12) {
414 : 44 : spdm_response_size = sizeof(spdm_capabilities_response_t);
415 : : } else {
416 : 64 : spdm_response_size = sizeof(spdm_capabilities_response_t) -
417 : : sizeof(spdm_response->data_transfer_size) -
418 : : sizeof(spdm_response->max_spdm_msg_size);
419 : : }
420 : :
421 [ + + ]: 120 : if (!validate_responder_capability(spdm_response->flags, spdm_response->header.spdm_version)) {
422 : 68 : status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
423 : 68 : goto receive_done;
424 : : }
425 [ + + ]: 52 : if (spdm_response->header.spdm_version >= SPDM_MESSAGE_VERSION_12) {
426 [ + + ]: 30 : if ((spdm_response->data_transfer_size < SPDM_MIN_DATA_TRANSFER_SIZE_VERSION_12) ||
427 [ + + ]: 26 : (spdm_response->data_transfer_size > spdm_response->max_spdm_msg_size)) {
428 : 6 : status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
429 : 6 : goto receive_done;
430 : : }
431 : :
432 [ + + ]: 24 : if (((spdm_response->flags & SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CHUNK_CAP) == 0) &&
433 [ + + ]: 8 : (spdm_response->data_transfer_size != spdm_response->max_spdm_msg_size)) {
434 : 2 : status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
435 : 2 : goto receive_done;
436 : : }
437 : : }
438 : :
439 [ + + ]: 44 : if (spdm_response->ct_exponent > LIBSPDM_MAX_CT_EXPONENT) {
440 : 2 : status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
441 : 2 : goto receive_done;
442 : : }
443 : :
444 : : /* -=[Process Response Phase]=- */
445 : 42 : status = libspdm_append_message_a(spdm_context, spdm_request, spdm_request_size);
446 [ + + ]: 42 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
447 : 2 : goto receive_done;
448 : : }
449 : :
450 : 40 : status = libspdm_append_message_a(spdm_context, spdm_response, spdm_response_size);
451 [ + + ]: 40 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
452 : 2 : goto receive_done;
453 : : }
454 : :
455 : 38 : spdm_context->connection_info.capability.ct_exponent = spdm_response->ct_exponent;
456 : 38 : spdm_context->connection_info.capability.flags =
457 : 38 : libspdm_mask_capability_flags(spdm_context, false, spdm_response->flags);
458 : :
459 [ + + ]: 38 : if (spdm_response->header.spdm_version >= SPDM_MESSAGE_VERSION_14) {
460 : 8 : spdm_context->connection_info.capability.ext_flags =
461 : 8 : libspdm_mask_capability_ext_flags(spdm_context, false, spdm_response->ext_flags);
462 : : } else {
463 : 30 : spdm_context->connection_info.capability.ext_flags = 0;
464 : : }
465 : :
466 [ + + ]: 38 : if (spdm_response->header.spdm_version >= SPDM_MESSAGE_VERSION_12) {
467 : 16 : spdm_context->connection_info.capability.data_transfer_size =
468 : 16 : spdm_response->data_transfer_size;
469 : 16 : spdm_context->connection_info.capability.max_spdm_msg_size =
470 : 16 : spdm_response->max_spdm_msg_size;
471 : : } else {
472 : 22 : spdm_context->connection_info.capability.data_transfer_size = 0;
473 : 22 : spdm_context->connection_info.capability.max_spdm_msg_size = 0;
474 : : }
475 : :
476 : : /* Copy algorithms if requested and received */
477 [ + + + - ]: 38 : if (supported_algs != NULL && supported_algs_length != NULL &&
478 [ + - ]: 12 : spdm_response->header.spdm_version >= SPDM_MESSAGE_VERSION_13 &&
479 [ + - ]: 12 : (spdm_request->header.param1 & SPDM_GET_CAPABILITIES_REQUEST_PARAM1_SUPPORTED_ALGORITHMS) &&
480 [ + + ]: 20 : (spdm_response->header.param1 & SPDM_CAPABILITIES_RESPONSE_PARAM1_SUPPORTED_ALGORITHMS)) {
481 : :
482 : 10 : spdm_supported_algorithms_block_t *supported_algorithms =
483 : 10 : (spdm_supported_algorithms_block_t*)((uint8_t*)spdm_response +
484 : : sizeof(spdm_capabilities_response_t));
485 : :
486 : 10 : size_t algorithm_data_size = spdm_response_size - sizeof(spdm_capabilities_response_t);
487 : :
488 [ + + ]: 10 : if (*supported_algs_length >= algorithm_data_size) {
489 : 8 : libspdm_copy_mem(supported_algs, *supported_algs_length,
490 : : supported_algorithms, algorithm_data_size);
491 : 8 : *supported_algs_length = algorithm_data_size;
492 : : } else {
493 : 2 : *supported_algs_length = algorithm_data_size;
494 : 2 : status = LIBSPDM_STATUS_BUFFER_TOO_SMALL;
495 : 2 : goto receive_done;
496 : : }
497 [ + + ]: 28 : } else if (supported_algs_length != NULL) {
498 : 2 : *supported_algs_length = 0;
499 : : }
500 : :
501 : : /* -=[Update State Phase]=- */
502 : 36 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_AFTER_CAPABILITIES;
503 : 36 : status = LIBSPDM_STATUS_SUCCESS;
504 : :
505 : : /* -=[Log Message Phase]=- */
506 : : #if LIBSPDM_ENABLE_MSG_LOG
507 : 36 : libspdm_append_msg_log(spdm_context, spdm_response, spdm_response_size);
508 : : #endif /* LIBSPDM_ENABLE_MSG_LOG */
509 : :
510 : 254 : receive_done:
511 : 254 : libspdm_release_receiver_buffer (spdm_context);
512 : 254 : return status;
513 : : }
514 : :
515 : 240 : libspdm_return_t libspdm_get_capabilities(libspdm_context_t *spdm_context)
516 : : {
517 : : size_t retry;
518 : : uint64_t retry_delay_time;
519 : : libspdm_return_t status;
520 : :
521 : 240 : spdm_context->crypto_request = false;
522 : 240 : retry = spdm_context->retry_times;
523 : 240 : retry_delay_time = spdm_context->retry_delay_time;
524 : : do {
525 : 242 : status = libspdm_try_get_capabilities(spdm_context, NULL, NULL);
526 [ + + ]: 242 : if (status != LIBSPDM_STATUS_BUSY_PEER) {
527 : 236 : return status;
528 : : }
529 : :
530 : 6 : libspdm_sleep(retry_delay_time);
531 [ + + ]: 6 : } while (retry-- != 0);
532 : :
533 : 4 : return status;
534 : : }
535 : :
536 : 22 : libspdm_return_t libspdm_get_capabilities_with_supported_algs(libspdm_context_t *spdm_context,
537 : : size_t *supported_algs_length,
538 : : void *supported_algs)
539 : : {
540 : : size_t retry;
541 : : uint64_t retry_delay_time;
542 : : libspdm_return_t status;
543 : :
544 : 22 : LIBSPDM_ASSERT((supported_algs == NULL) || (supported_algs_length != NULL));
545 : :
546 : 22 : spdm_context->crypto_request = false;
547 : 22 : retry = spdm_context->retry_times;
548 : 22 : retry_delay_time = spdm_context->retry_delay_time;
549 : : do {
550 : : status =
551 : 24 : libspdm_try_get_capabilities(spdm_context, supported_algs_length, supported_algs);
552 [ + + ]: 24 : if (status != LIBSPDM_STATUS_BUSY_PEER) {
553 : 22 : return status;
554 : : }
555 : :
556 : 2 : libspdm_sleep(retry_delay_time);
557 [ + - ]: 2 : } while (retry-- != 0);
558 : :
559 : 0 : return status;
560 : : }
|