Branch data Line data Source code
1 : : /**
2 : : * Copyright Notice:
3 : : * Copyright 2025-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 "library/spdm_transport_storage_lib.h"
8 : : #include "industry_standard/spdm_storage_binding.h"
9 : : #include "hal/library/debuglib.h"
10 : : #include "hal/library/memlib.h"
11 : :
12 : : /**
13 : : * This function translates the negotiated secured_message_version to a DSP0277 version.
14 : : *
15 : : * @param secured_message_version The version specified in binding specification and
16 : : * negotiated in KEY_EXCHANGE/KEY_EXCHANGE_RSP.
17 : : *
18 : : * @return The DSP0277 version specified in binding specification,
19 : : * which is bound to secured_message_version.
20 : : */
21 : 22 : static spdm_version_number_t libspdm_storage_get_secured_spdm_version(
22 : : spdm_version_number_t secured_message_version)
23 : : {
24 : 22 : return secured_message_version;
25 : : }
26 : :
27 : : /**
28 : : * Return max random number count in an SPDM secure message.
29 : : *
30 : : * This value is transport layer specific.
31 : : *
32 : : * @return Max random number count in an SPDM secured message.
33 : : * 0 means no random number is required.
34 : : **/
35 : 8 : static uint32_t libspdm_storage_get_max_random_number_count(void)
36 : : {
37 : 8 : return LIBSPDM_STORAGE_MAX_RANDOM_NUMBER_COUNT;
38 : : }
39 : :
40 : : /**
41 : : * Get sequence number in an SPDM secure message.
42 : : *
43 : : * This value is transport layer specific.
44 : : *
45 : : * @param sequence_number The current sequence number used to encode or decode message.
46 : : * @param sequence_number_buffer A buffer to hold the sequence number output used in the secured message.
47 : : * The size in byte of the output buffer shall be 8.
48 : : *
49 : : * @return size in byte of the sequence_number_buffer.
50 : : * It shall be no greater than 8.
51 : : * 0 means no sequence number is required.
52 : : **/
53 : 22 : static uint8_t libspdm_storage_get_sequence_number(uint64_t sequence_number,
54 : : uint8_t *sequence_number_buffer)
55 : : {
56 : 22 : libspdm_copy_mem(sequence_number_buffer, sizeof(uint64_t),
57 : : &sequence_number, sizeof(uint64_t));
58 : :
59 : 22 : return SPDM_STORAGE_SEQUENCE_NUMBER_COUNT;
60 : : }
61 : :
62 : : /**
63 : : * Decode, from a decrypted SPDM Secured Storage message, the SPDM Secured Message Descriptor.
64 : : * This function must only be called after `libspdm_decode_secured_message()` has already
65 : : * processed the encrypted secured message.
66 : : *
67 : : * Limitations:
68 : : * - Implementation only supports parsing a single descriptor
69 : : * - Only supports processing of SPDM Message Descriptor
70 : : *
71 : : * Notes: Encapsulated Error Status shall be set in the response within the Secured descriptor
72 : : * message `status` field, if an erroneous or an unsupported message/field is detected.
73 : : * To accommodate this, keep track of the error state within the transport layer and append the error
74 : : * status when encoding the subsequent response. This is done in `libspdm_transport_storage_encode_message()`
75 : : *
76 : : * @param spdm_context A pointer to the SPDM context.
77 : : * @param session_id Secured Session ID
78 : : * @param message_size SPDM Message Size.
79 : : * On output, size of the real SPDM Message.
80 : : * @param message Decrypted SPDM Message, pointing to the `num descriptors` field of the message.
81 : : * On output, points the start of the real SPDM Message.
82 : : * @param is_request_message Indicates if it is a request message.
83 : : *
84 : : **/
85 : 12 : static libspdm_return_t libspdm_storage_secured_message_decode(
86 : : void *spdm_context, uint32_t session_id, size_t *message_size, void **message,
87 : : bool is_request_message)
88 : : {
89 : : uint8_t *spdm_storage_descriptor_start;
90 : : libspdm_error_struct_t spdm_error;
91 : : uint32_t spdm_msg_offset;
92 : : spdm_storage_secured_message_descriptor *descriptor;
93 : : uint8_t num_descriptors;
94 : :
95 : : /* The num descriptors byte and the single supported descriptor are read
96 : : * below, so the decrypted message must be at least that large before any
97 : : * field is dereferenced. */
98 [ + + ]: 12 : if (*message_size < LIBSPDM_STORAGE_SECURED_MESSAGE_DESCRIPTOR_MIN_SIZE) {
99 : 2 : return LIBSPDM_STATUS_INVALID_MSG_SIZE;
100 : : }
101 : :
102 : 10 : num_descriptors = ((uint8_t *)(*message))[0];
103 : :
104 : : /* Check for invalid message with no descriptors (DSP0286 Table 8) */
105 [ + + ]: 10 : if (num_descriptors == 0) {
106 : 2 : spdm_error.session_id = session_id;
107 : 2 : spdm_error.error_code = SPDM_STORAGE_SECURED_MSG_ENCAPSULATED_STATUS_INVALID_CMD;
108 : 2 : libspdm_set_last_spdm_error_struct(spdm_context, &spdm_error);
109 : 2 : return LIBSPDM_STATUS_INVALID_MSG_FIELD;
110 : : }
111 : :
112 : : /* Currently support handling only a single SPDM descriptor */
113 [ + + ]: 8 : if (num_descriptors > 1) {
114 : 2 : spdm_error.session_id = session_id;
115 : 2 : spdm_error.error_code = SPDM_STORAGE_SECURED_MSG_ENCAPSULATED_STATUS_INVALID_CMD;
116 : 2 : libspdm_set_last_spdm_error_struct(spdm_context, &spdm_error);
117 : 2 : return LIBSPDM_STATUS_UNSUPPORTED_CAP;
118 : : }
119 : :
120 : 6 : spdm_storage_descriptor_start = ((uint8_t *)(*message)) + (sizeof(uint8_t) * 3);
121 : 6 : descriptor = (void *)spdm_storage_descriptor_start;
122 : :
123 [ + + ]: 10 : for (int i = 0; i < num_descriptors; ++i) {
124 [ + - ]: 6 : switch(descriptor->desc_type) {
125 : 6 : case SPDM_STORAGE_SECURED_MSG_DESCRIPTOR_SPDM:
126 [ - + ]: 6 : if (descriptor->length > *message_size)
127 : 0 : return LIBSPDM_STATUS_INVALID_MSG_SIZE;
128 : :
129 : : /*
130 : : * Offset is calculated from the start of the Secured
131 : : * Message Data Buffer. `message` points to the start
132 : : * of `num descriptors` within the decoded Secured Message.
133 : : */
134 [ - + ]: 6 : if (LIBSPDM_STORAGE_SECURED_MESSAGE_NUM_DESCRIPTORS_OFFSET > descriptor->offset)
135 : 0 : return LIBSPDM_STATUS_INVALID_MSG_FIELD;
136 : :
137 : 6 : spdm_msg_offset = descriptor->offset -
138 : : LIBSPDM_STORAGE_SECURED_MESSAGE_NUM_DESCRIPTORS_OFFSET;
139 [ - + ]: 6 : if (spdm_msg_offset > *message_size)
140 : 0 : return LIBSPDM_STATUS_INVALID_MSG_SIZE;
141 : :
142 [ + + ]: 6 : if (!is_request_message &&
143 [ + + ]: 4 : descriptor->status != SPDM_STORAGE_SECURED_MSG_ENCAPSULATED_STATUS_SUCCESS)
144 : 2 : return LIBSPDM_STATUS_ERROR_PEER;
145 : :
146 : 4 : break;
147 : 0 : default:
148 : 0 : spdm_error.session_id = session_id;
149 : 0 : spdm_error.error_code = SPDM_STORAGE_SECURED_MSG_ENCAPSULATED_STATUS_INVALID_CMD;
150 : 0 : libspdm_set_last_spdm_error_struct(spdm_context, &spdm_error);
151 : 0 : return LIBSPDM_STATUS_UNSUPPORTED_CAP;
152 : : }
153 : :
154 : : /* Next descriptor */
155 : 4 : descriptor = (void *)(spdm_storage_descriptor_start +
156 : 4 : (LIBSPDM_STORAGE_SECURED_MESSAGE_DESCRIPTOR_MIN_SIZE * i));
157 : : }
158 : :
159 : 4 : *message = ((uint8_t *)*message) + spdm_msg_offset;
160 : 4 : *message_size -= spdm_msg_offset;
161 : :
162 : 4 : return LIBSPDM_STATUS_SUCCESS;
163 : : }
164 : :
165 : : /**
166 : : * Encode the SPDM Storage Secured Descriptor into an SPDM message. This shall be
167 : : * followed by a call to `libspdm_encode_secured_message()` to encrypt the
168 : : * attached command buffer. This appends exactly one descriptor with the
169 : : * SPDM message type, encapsulating the SPDM message data buffer.
170 : : *
171 : : * @param spdm_context A pointer to the SPDM context.
172 : : * @param message_size SPDM Message Size.
173 : : * @param message SPDM Message.
174 : : * @param secured_message_size On output, size in bytes of the secured message to be encrypted.
175 : : * @param secured_message On output, the start of an SPDM Storage secured message, pointing
176 : : * into the transport message.
177 : : * @param transport_message_size Size in bytes of the transport message buffer.
178 : : * @param transport_message A pointer to a destination buffer to store the transport message.
179 : : * @param is_request_message Indicates if it is a request message.
180 : : *
181 : : **/
182 : 8 : static libspdm_return_t libspdm_storage_secured_message_encode(
183 : : void *spdm_context, size_t *message_size, void **message,
184 : : size_t *secured_message_size, uint8_t **secured_message,
185 : : size_t *transport_message_size, void **transport_message,
186 : : bool is_request_message)
187 : : {
188 : : libspdm_error_struct_t spdm_error;
189 : 8 : size_t sec_trans_header_size = is_request_message ?
190 [ + + ]: 8 : sizeof(libspdm_storage_transport_virtual_header_t): 0;
191 : : uint8_t* secured_storage_desc_start;
192 : : uint8_t* secured_storage_desc_end;
193 : 8 : uint8_t num_descriptors = 1;
194 : : spdm_storage_secured_message_descriptor *descriptor;
195 : :
196 : : /* DSP0286 Specifies 4 Reserved bytes at the start of a secured message */
197 : 8 : sec_trans_header_size += sizeof(uint8_t) * 4;
198 : :
199 : 8 : libspdm_zero_mem(*transport_message, *transport_message_size);
200 : 8 : *secured_message = ((uint8_t *)(*transport_message)) + sec_trans_header_size;
201 : 8 : *secured_message_size = *transport_message_size - sec_trans_header_size;
202 : :
203 : 8 : if (*secured_message_size <
204 [ - + ]: 8 : LIBSPDM_STORAGE_SECURED_MESSAGE_DESCRIPTOR_MIN_SIZE + *message_size) {
205 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR,
206 : : "No space in transport message buffer to append storage descriptors"));
207 : 0 : return LIBSPDM_STATUS_INVALID_MSG_SIZE;
208 : : }
209 : :
210 : 8 : secured_storage_desc_start = *secured_message + (4 + 2 + 2 + 2 + 2);
211 : 8 : secured_storage_desc_end = secured_storage_desc_start +
212 : : LIBSPDM_STORAGE_SECURED_MESSAGE_DESCRIPTOR_MIN_SIZE;
213 : :
214 : : /*
215 : : * Move the secured message within the transport message to allow space for
216 : : * descriptors and zero the descriptor fields.
217 : : */
218 : 8 : libspdm_copy_mem(secured_storage_desc_end,
219 : 8 : *secured_message_size - LIBSPDM_STORAGE_SECURED_MESSAGE_DESCRIPTOR_MIN_SIZE,
220 : : *message, *message_size);
221 : 8 : libspdm_zero_mem(secured_storage_desc_start,
222 : : LIBSPDM_STORAGE_SECURED_MESSAGE_DESCRIPTOR_MIN_SIZE);
223 : :
224 : : /* Retrieve secured session encapsulated error status if any */
225 : 8 : libspdm_get_last_spdm_error_struct(spdm_context, &spdm_error);
226 : :
227 : : /* Encode Secured Message Storage Descriptor */
228 : 8 : secured_storage_desc_start[0] = num_descriptors;
229 : : /* 3 reserved bytes before the descriptor begins */
230 : 8 : descriptor = (void *)(secured_storage_desc_start + (sizeof(uint8_t) * 3));
231 : 8 : descriptor->desc_type = SPDM_STORAGE_SECURED_MSG_DESCRIPTOR_SPDM;
232 [ + + ]: 8 : descriptor->status =
233 : : is_request_message ? 0 : spdm_error.error_code;
234 : :
235 : : /* Length of the element this SPDM element */
236 : 8 : descriptor->length = (uint32_t)*message_size;
237 : :
238 : : /*
239 : : * The updated `message` means subsequent calls to `libspdm_encode_secured_message()`
240 : : * also encode this `descriptor` as part of the data buffer as specified
241 : : * by DSP0286: 158.
242 : : */
243 : 8 : *message = secured_storage_desc_start;
244 : 8 : *message_size += LIBSPDM_STORAGE_SECURED_MESSAGE_DESCRIPTOR_MIN_SIZE;
245 : :
246 : : /*
247 : : * Only a single descriptor is used, the offset into the secure message data
248 : : * element is calculated as per Table 7 of DSP0286. That is, from the start of
249 : : * the SPDM Storage Secured Message Header
250 : : */
251 : 8 : descriptor->offset = (4 + 4 + 2 + 2 + 2 + 2 +
252 : : LIBSPDM_STORAGE_SECURED_MESSAGE_DESCRIPTOR_MIN_SIZE);
253 : :
254 : 8 : return LIBSPDM_STATUS_SUCCESS;
255 : : }
256 : :
257 : 32 : libspdm_return_t libspdm_storage_decode_message(uint32_t **session_id,
258 : : uint8_t *connection_id,
259 : : size_t transport_message_size,
260 : : void *transport_message,
261 : : size_t *message_size,
262 : : void **message)
263 : : {
264 : : const libspdm_storage_transport_virtual_header_t *storage_header;
265 : : uint16_t security_protocol_specific;
266 : : uint8_t spsp0, spsp1, spdm_operation;
267 : :
268 : 32 : LIBSPDM_ASSERT(transport_message_size >= sizeof(libspdm_storage_transport_virtual_header_t));
269 [ + + ]: 32 : if (transport_message_size <= sizeof(libspdm_storage_transport_virtual_header_t)) {
270 : 2 : return LIBSPDM_STATUS_INVALID_MSG_SIZE;
271 : : }
272 : :
273 [ - + ]: 30 : if (transport_message_size == 0) {
274 : 0 : return LIBSPDM_STATUS_INVALID_MSG_SIZE;
275 : : }
276 : :
277 : 30 : storage_header = transport_message;
278 [ + + ]: 30 : if (storage_header->security_protocol != SPDM_STORAGE_SECURITY_PROTOCOL_DMTF) {
279 : 2 : return LIBSPDM_STATUS_INVALID_MSG_FIELD;
280 : : }
281 : :
282 : 28 : security_protocol_specific = storage_header->security_protocol_specific;
283 : 28 : spsp0 = security_protocol_specific & 0xFF;
284 : 28 : spsp1 = security_protocol_specific >> 8;
285 : :
286 [ + + ]: 28 : if (spsp1 != 0) {
287 : 2 : return LIBSPDM_STATUS_INVALID_MSG_FIELD;
288 : : }
289 : :
290 [ + + ]: 26 : if (connection_id) {
291 : 2 : *connection_id = spsp0 & 0x03;
292 : : }
293 : 26 : spdm_operation = (spsp0 & 0xFC) >> 2;
294 : :
295 [ + + + ]: 26 : switch (spdm_operation) {
296 : 10 : case SPDM_STORAGE_OPERATION_CODE_DISCOVERY:
297 : : case SPDM_STORAGE_OPERATION_CODE_PENDING_INFO:
298 : : case SPDM_STORAGE_OPERATION_CODE_MESSAGE:
299 [ + - ]: 10 : if (session_id != NULL) {
300 : 10 : *session_id = NULL;
301 : : }
302 : 10 : *message_size = transport_message_size - sizeof(libspdm_storage_transport_virtual_header_t);
303 : 10 : *message = (uint8_t *)transport_message + sizeof(libspdm_storage_transport_virtual_header_t);
304 : 10 : break;
305 : 14 : case SPDM_STORAGE_OPERATION_CODE_SECURED_MESSAGE:
306 : 14 : LIBSPDM_ASSERT(session_id != NULL);
307 [ - + ]: 14 : if (session_id == NULL) {
308 : 0 : return LIBSPDM_STATUS_INVALID_MSG_FIELD;
309 : : }
310 [ + + ]: 14 : if (transport_message_size <=
311 : : sizeof(libspdm_storage_transport_virtual_header_t) + sizeof(uint32_t)) {
312 : 2 : return LIBSPDM_STATUS_INVALID_MSG_SIZE;
313 : : }
314 : 12 : *session_id = (uint32_t *)((uint8_t *)transport_message +
315 : : sizeof(libspdm_storage_transport_virtual_header_t) +
316 : : LIBSPDM_STORAGE_SECURED_MESSAGE_HEADER_RESERVED_BYTES);
317 : 12 : *message_size = transport_message_size - sizeof(libspdm_storage_transport_virtual_header_t) -
318 : : LIBSPDM_STORAGE_SECURED_MESSAGE_HEADER_RESERVED_BYTES;
319 : 12 : *message = (uint8_t *)transport_message + sizeof(libspdm_storage_transport_virtual_header_t) +
320 : : LIBSPDM_STORAGE_SECURED_MESSAGE_HEADER_RESERVED_BYTES;
321 : 12 : break;
322 : 2 : default:
323 : 2 : return LIBSPDM_STATUS_UNSUPPORTED_CAP;
324 : : }
325 : :
326 : 22 : return LIBSPDM_STATUS_SUCCESS;
327 : : }
328 : :
329 : 36 : libspdm_return_t libspdm_transport_storage_decode_message(
330 : : void *spdm_context, uint32_t **session_id,
331 : : bool *is_app_message, bool is_request_message,
332 : : size_t transport_message_size, void *transport_message,
333 : : size_t *message_size, void **message)
334 : : {
335 : : libspdm_return_t status;
336 : : uint32_t *secured_message_session_id;
337 : : uint32_t received_session_id;
338 : : uint8_t *secured_message;
339 : : size_t secured_message_size;
340 : : libspdm_secured_message_callbacks_t spdm_secured_message_callbacks;
341 : : void *secured_message_context;
342 : : libspdm_error_struct_t spdm_error;
343 : :
344 : 36 : spdm_error.error_code = 0;
345 : 36 : spdm_error.session_id = 0;
346 : 36 : libspdm_set_last_spdm_error_struct(spdm_context, &spdm_error);
347 : :
348 : 36 : spdm_secured_message_callbacks.version =
349 : : LIBSPDM_SECURED_MESSAGE_CALLBACKS_VERSION;
350 : 36 : spdm_secured_message_callbacks.get_sequence_number =
351 : : libspdm_storage_get_sequence_number;
352 : 36 : spdm_secured_message_callbacks.get_max_random_number_count =
353 : : libspdm_storage_get_max_random_number_count;
354 : 36 : spdm_secured_message_callbacks.get_secured_spdm_version =
355 : : libspdm_storage_get_secured_spdm_version;
356 : :
357 [ + - - + ]: 36 : if ((session_id == NULL) || (is_app_message == NULL)) {
358 : 0 : return LIBSPDM_STATUS_UNSUPPORTED_CAP;
359 : : }
360 : 36 : *is_app_message = false;
361 : :
362 : 36 : secured_message_session_id = NULL;
363 : :
364 [ + + ]: 36 : if (!is_request_message) {
365 : : /*
366 : : * Storage response messages are not transport encoded, this is the SPDM
367 : : * message, and shall be processed as such. Which also means that there
368 : : * is no way from the response to determine if it's secure or not, instead
369 : : * use internal state to determine.
370 : : */
371 : : /* Expecting a secured message */
372 [ + + ]: 10 : if (*session_id != NULL) {
373 [ + + ]: 8 : if (transport_message_size <
374 : : LIBSPDM_STORAGE_SECURED_MESSAGE_HEADER_RESERVED_BYTES + sizeof(uint32_t)) {
375 : 2 : return LIBSPDM_STATUS_INVALID_MSG_SIZE;
376 : : }
377 : 6 : **session_id = libspdm_read_uint32(
378 : : (const uint8_t *)transport_message +
379 : : LIBSPDM_STORAGE_SECURED_MESSAGE_HEADER_RESERVED_BYTES);
380 : :
381 : : secured_message_context =
382 : 6 : libspdm_get_secured_message_context_via_session_id(
383 : 6 : spdm_context, **session_id);
384 [ + + ]: 6 : if (secured_message_context == NULL) {
385 : 2 : spdm_error.error_code = SPDM_ERROR_CODE_INVALID_SESSION;
386 : 2 : spdm_error.session_id = **session_id;
387 : 2 : libspdm_set_last_spdm_error_struct(spdm_context,
388 : : &spdm_error);
389 : 2 : return LIBSPDM_STATUS_UNSUPPORTED_CAP;
390 : : }
391 : :
392 : : /*
393 : : * Secured message to message
394 : : *
395 : : * DSP0286 specifies 4 reserved bytes at the start of a secured
396 : : * message. Skip that for decoding.
397 : : */
398 : 4 : status = libspdm_decode_secured_message(
399 : 4 : secured_message_context, **session_id,
400 : : is_request_message,
401 : : transport_message_size - LIBSPDM_STORAGE_SECURED_MESSAGE_HEADER_RESERVED_BYTES,
402 : : ((uint8_t *)transport_message) +
403 : : LIBSPDM_STORAGE_SECURED_MESSAGE_HEADER_RESERVED_BYTES,
404 : : message_size, message, &spdm_secured_message_callbacks);
405 : :
406 [ - + ]: 4 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
407 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR,
408 : : "libspdm_decode_secured_message - %x\n", status));
409 : 0 : libspdm_secured_message_get_last_spdm_error_struct(
410 : : secured_message_context, &spdm_error);
411 : 0 : libspdm_set_last_spdm_error_struct(spdm_context,
412 : : &spdm_error);
413 : 0 : return status;
414 : : }
415 : :
416 : 4 : status = libspdm_storage_secured_message_decode(spdm_context, **session_id, message_size,
417 : : message, is_request_message);
418 [ + + ]: 4 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
419 : 2 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR,
420 : : "libspdm_storage_secured_message_decode - %x\n", status));
421 : 2 : return status;
422 : : }
423 : :
424 : : } else {
425 : 2 : *message_size = transport_message_size;
426 : 2 : *message = transport_message;
427 : 2 : *session_id = NULL;
428 : : };
429 : 4 : return LIBSPDM_STATUS_SUCCESS;
430 : : } else {
431 : : /* Storage requests need to be transport encoded/decoded */
432 : 26 : status = libspdm_storage_decode_message(
433 : : &secured_message_session_id, 0, transport_message_size,
434 : : transport_message, &secured_message_size, (void **)&secured_message);
435 : :
436 [ + + ]: 26 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
437 : 10 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR, "transport_decode_message - %x\n", status));
438 : 10 : return status;
439 : : }
440 : :
441 [ + + ]: 16 : if (secured_message_session_id != NULL) {
442 : 12 : *session_id = secured_message_session_id;
443 : :
444 : : /* The session ID is located inside the received message, which may not be aligned. */
445 : 12 : received_session_id = libspdm_read_uint32((const uint8_t *)secured_message_session_id);
446 : :
447 : : secured_message_context =
448 : 12 : libspdm_get_secured_message_context_via_session_id(
449 : : spdm_context, received_session_id);
450 [ + + ]: 12 : if (secured_message_context == NULL) {
451 : 2 : spdm_error.error_code = SPDM_ERROR_CODE_INVALID_SESSION;
452 : 2 : spdm_error.session_id = received_session_id;
453 : 2 : libspdm_set_last_spdm_error_struct(spdm_context,
454 : : &spdm_error);
455 : 2 : return LIBSPDM_STATUS_UNSUPPORTED_CAP;
456 : : }
457 : :
458 : : /* Secured message to message*/
459 : 10 : status = libspdm_decode_secured_message(
460 : : secured_message_context, received_session_id,
461 : : is_request_message, secured_message_size,
462 : : (uint8_t *)secured_message,
463 : : message_size, message, &spdm_secured_message_callbacks);
464 [ + + ]: 10 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
465 : 2 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR,
466 : : "libspdm_decode_secured_message - %x\n", status));
467 : 2 : libspdm_secured_message_get_last_spdm_error_struct(
468 : : secured_message_context, &spdm_error);
469 : 2 : libspdm_set_last_spdm_error_struct(spdm_context,
470 : : &spdm_error);
471 : 2 : return status;
472 : : }
473 : :
474 : 8 : status = libspdm_storage_secured_message_decode(spdm_context, received_session_id, message_size,
475 : : message, is_request_message);
476 [ + + ]: 8 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
477 : 6 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR,
478 : : "libspdm_storage_secured_message_decode - %x\n", status));
479 : 6 : return status;
480 : : }
481 : 2 : return LIBSPDM_STATUS_SUCCESS;
482 : : } else {
483 : : /* get non-secured message*/
484 : 4 : status = libspdm_storage_decode_message(&secured_message_session_id,
485 : : 0,
486 : : transport_message_size,
487 : : transport_message,
488 : : message_size, message);
489 [ - + ]: 4 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
490 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR, "transport_decode_message - %x\n",
491 : : status));
492 : 0 : return status;
493 : : }
494 : 4 : LIBSPDM_ASSERT(secured_message_session_id == NULL);
495 : 4 : *session_id = NULL;
496 : 4 : return LIBSPDM_STATUS_SUCCESS;
497 : : }
498 : : }
499 : : }
500 : :
501 : 16 : libspdm_return_t libspdm_storage_encode_message(const uint32_t *session_id,
502 : : uint8_t connection_id,
503 : : size_t message_size, void *message,
504 : : size_t *transport_message_size,
505 : : void **transport_message)
506 : : {
507 : : uint32_t data32;
508 : : libspdm_storage_transport_virtual_header_t *storage_header;
509 : :
510 [ + - + - ]: 16 : if (!transport_message_size || *transport_message_size == 0
511 [ - + ]: 16 : || message_size == 0) {
512 : 0 : return LIBSPDM_STATUS_INVALID_MSG_SIZE;
513 : : }
514 : :
515 : 16 : LIBSPDM_ASSERT(*transport_message_size >= sizeof(libspdm_storage_transport_virtual_header_t));
516 : :
517 [ + + ]: 16 : if (*transport_message_size < message_size + sizeof(libspdm_storage_transport_virtual_header_t)) {
518 : 2 : *transport_message_size = message_size + sizeof(libspdm_storage_transport_virtual_header_t);
519 : 2 : return LIBSPDM_STATUS_BUFFER_TOO_SMALL;
520 : : }
521 : :
522 [ + - - + ]: 14 : if (!message || !transport_message) {
523 : 0 : return LIBSPDM_STATUS_INVALID_MSG_SIZE;
524 : : }
525 : :
526 [ + + ]: 14 : if (connection_id & ~0x03) {
527 : 2 : return LIBSPDM_STATUS_INVALID_MSG_FIELD;
528 : : }
529 : :
530 : 12 : *transport_message_size = message_size + sizeof(libspdm_storage_transport_virtual_header_t);
531 : 12 : *transport_message = (uint8_t *)message - sizeof(libspdm_storage_transport_virtual_header_t);
532 : 12 : storage_header = *transport_message;
533 : :
534 : 12 : storage_header->security_protocol = SPDM_STORAGE_SECURITY_PROTOCOL_DMTF;
535 : :
536 [ + + ]: 12 : if (session_id != NULL) {
537 : 4 : storage_header->security_protocol_specific = SPDM_STORAGE_OPERATION_CODE_SECURED_MESSAGE <<
538 : : 2;
539 : 4 : storage_header->security_protocol_specific |= connection_id &
540 : : SPDM_STORAGE_MAX_CONNECTION_ID_MASK;
541 : 4 : libspdm_zero_mem(message, LIBSPDM_STORAGE_SECURED_MESSAGE_HEADER_RESERVED_BYTES);
542 : 4 : data32 = libspdm_read_uint32(((const uint8_t *)message +
543 : : LIBSPDM_STORAGE_SECURED_MESSAGE_HEADER_RESERVED_BYTES));
544 : 4 : LIBSPDM_ASSERT(*session_id == data32);
545 [ - + ]: 4 : if (*session_id != data32) {
546 : 0 : return LIBSPDM_STATUS_INVALID_MSG_FIELD;
547 : : }
548 : : } else {
549 : 8 : storage_header->security_protocol_specific = SPDM_STORAGE_OPERATION_CODE_MESSAGE << 2;
550 : 8 : storage_header->security_protocol_specific |= connection_id &
551 : : SPDM_STORAGE_MAX_CONNECTION_ID_MASK;
552 : : }
553 : :
554 : 12 : return LIBSPDM_STATUS_SUCCESS;
555 : : }
556 : :
557 : 26 : libspdm_return_t libspdm_transport_storage_encode_message(
558 : : void *spdm_context, const uint32_t *session_id,
559 : : bool is_app_message,
560 : : bool is_request_message, size_t message_size, void *message,
561 : : size_t *transport_message_size, void **transport_message)
562 : : {
563 : : libspdm_return_t status;
564 : : uint8_t *secured_message;
565 : : size_t secured_message_size;
566 : : libspdm_secured_message_callbacks_t spdm_secured_message_callbacks;
567 : : void *secured_message_context;
568 : :
569 : 26 : spdm_secured_message_callbacks.version =
570 : : LIBSPDM_SECURED_MESSAGE_CALLBACKS_VERSION;
571 : 26 : spdm_secured_message_callbacks.get_sequence_number =
572 : : libspdm_storage_get_sequence_number;
573 : 26 : spdm_secured_message_callbacks.get_max_random_number_count =
574 : : libspdm_storage_get_max_random_number_count;
575 : 26 : spdm_secured_message_callbacks.get_secured_spdm_version =
576 : : libspdm_storage_get_secured_spdm_version;
577 : :
578 [ + + ]: 26 : if (is_app_message) {
579 : 2 : return LIBSPDM_STATUS_UNSUPPORTED_CAP;
580 : : }
581 : :
582 [ + - + - ]: 24 : if (!transport_message_size || !transport_message
583 [ + + ]: 24 : || *transport_message_size == 0) {
584 : 4 : return LIBSPDM_STATUS_INVALID_MSG_SIZE;
585 : : }
586 : :
587 [ + + ]: 20 : if (session_id != NULL) {
588 : : secured_message_context =
589 : 10 : libspdm_get_secured_message_context_via_session_id(
590 : : spdm_context, *session_id);
591 [ + + ]: 10 : if (secured_message_context == NULL) {
592 : 2 : return LIBSPDM_STATUS_UNSUPPORTED_CAP;
593 : : }
594 : :
595 : : /* Message to secured message*/
596 : 8 : status = libspdm_storage_secured_message_encode(
597 : : spdm_context, &message_size, &message, &secured_message_size, &secured_message,
598 : : transport_message_size, transport_message, is_request_message);
599 [ - + ]: 8 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
600 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR,
601 : : "libspdm_storage_secured_message_encode - %x\n", status));
602 : 0 : return status;
603 : : }
604 : :
605 : 8 : status = libspdm_encode_secured_message(
606 : : secured_message_context, *session_id, is_request_message,
607 : : message_size, message, &secured_message_size,
608 : : secured_message, &spdm_secured_message_callbacks);
609 [ - + ]: 8 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
610 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR,
611 : : "libspdm_encode_secured_message - %x\n", status));
612 : 0 : return status;
613 : : }
614 : :
615 [ + + ]: 8 : if (!is_request_message) {
616 : : /*
617 : : * Storage response messages are not transport encoded, instead it
618 : : * is just the SPDM message response.
619 : : */
620 : 4 : *transport_message_size = secured_message_size +
621 : : LIBSPDM_STORAGE_SECURED_MESSAGE_HEADER_RESERVED_BYTES;
622 : : /* Allow the 4 reserved bytes to be encapsulated */
623 : 4 : *transport_message = secured_message -
624 : : LIBSPDM_STORAGE_SECURED_MESSAGE_HEADER_RESERVED_BYTES;
625 : 4 : return LIBSPDM_STATUS_SUCCESS;
626 : : }
627 : :
628 : : /* secured message to secured storage message*/
629 : 4 : status = libspdm_storage_encode_message(
630 : : session_id, 0,
631 : : secured_message_size + LIBSPDM_STORAGE_SECURED_MESSAGE_HEADER_RESERVED_BYTES,
632 : 4 : secured_message - LIBSPDM_STORAGE_SECURED_MESSAGE_HEADER_RESERVED_BYTES,
633 : : transport_message_size, transport_message);
634 [ - + ]: 4 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
635 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR, "transport_encode_message - %x\n",
636 : : status));
637 : 0 : return status;
638 : : }
639 : : } else {
640 [ + + ]: 10 : if (!is_request_message) {
641 : : /*
642 : : * Storage response messages are not transport encoded, instead it
643 : : * is just the SPDM message response.
644 : : */
645 : 2 : *transport_message_size = message_size;
646 : 2 : *transport_message = message;
647 : 2 : return LIBSPDM_STATUS_SUCCESS;
648 : : }
649 : :
650 : : /* SPDM message to normal storage message*/
651 : 8 : status = libspdm_storage_encode_message(NULL, 0,
652 : : message_size, message,
653 : : transport_message_size,
654 : : transport_message);
655 [ + + ]: 8 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
656 : 2 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR, "transport_encode_message - %x\n",
657 : : status));
658 : 2 : return status;
659 : : }
660 : : }
661 : :
662 : 10 : return LIBSPDM_STATUS_SUCCESS;
663 : : }
664 : :
665 : 26 : libspdm_return_t libspdm_transport_storage_encode_management_cmd(
666 : : uint8_t cmd_direction, uint8_t transport_operation,
667 : : uint8_t connection_id, size_t *transport_message_size,
668 : : size_t *allocation_length, void *transport_message)
669 : : {
670 : : libspdm_storage_transport_virtual_header_t *storage_header;
671 : :
672 [ + - + - ]: 26 : if (!transport_message_size || !allocation_length
673 [ + + ]: 26 : || *transport_message_size == 0) {
674 : 2 : return LIBSPDM_STATUS_INVALID_MSG_SIZE;
675 : : }
676 : :
677 [ + + ]: 24 : if (*transport_message_size < sizeof(libspdm_storage_transport_virtual_header_t)) {
678 : 2 : return LIBSPDM_STATUS_BUFFER_TOO_SMALL;
679 : : }
680 : :
681 [ + + ]: 22 : if (connection_id & ~0x03) {
682 : 2 : return LIBSPDM_STATUS_INVALID_MSG_FIELD;
683 : : }
684 : :
685 [ + + + + ]: 20 : if (!(cmd_direction == LIBSPDM_STORAGE_CMD_DIRECTION_IF_SEND ||
686 : : cmd_direction == LIBSPDM_STORAGE_CMD_DIRECTION_IF_RECV)) {
687 : 4 : return LIBSPDM_STATUS_INVALID_MSG_FIELD;
688 : : }
689 : :
690 [ + + + ]: 16 : switch (transport_operation) {
691 : 8 : case SPDM_STORAGE_OPERATION_CODE_DISCOVERY:
692 [ + - ]: 8 : if (cmd_direction == LIBSPDM_STORAGE_CMD_DIRECTION_IF_RECV) {
693 : 8 : *allocation_length = sizeof(spdm_storage_discovery_response_t);
694 [ + + ]: 8 : if (*transport_message_size < sizeof(spdm_storage_discovery_response_t)) {
695 : 2 : return LIBSPDM_STATUS_BUFFER_TOO_SMALL;
696 : : }
697 : : }
698 : 6 : break;
699 : 4 : case SPDM_STORAGE_OPERATION_CODE_PENDING_INFO:
700 [ + + ]: 4 : if (cmd_direction == LIBSPDM_STORAGE_CMD_DIRECTION_IF_RECV) {
701 : 2 : *allocation_length = sizeof(spdm_storage_pending_info_response_t);
702 [ - + ]: 2 : if (*transport_message_size < sizeof(spdm_storage_pending_info_response_t)) {
703 : 0 : return LIBSPDM_STATUS_BUFFER_TOO_SMALL;
704 : : }
705 : : }
706 : 4 : break;
707 : 4 : default:
708 : 4 : return LIBSPDM_STATUS_INVALID_MSG_FIELD;
709 : : }
710 : :
711 : 10 : *transport_message_size = sizeof(libspdm_storage_transport_virtual_header_t);
712 : 10 : libspdm_zero_mem(transport_message, *transport_message_size);
713 : :
714 : 10 : storage_header = transport_message;
715 : 10 : storage_header->security_protocol = SPDM_STORAGE_SECURITY_PROTOCOL_DMTF;
716 : 10 : storage_header->security_protocol_specific = transport_operation << 2;
717 : 10 : storage_header->security_protocol_specific |= connection_id &
718 : : SPDM_STORAGE_MAX_CONNECTION_ID_MASK;
719 : :
720 : 10 : return LIBSPDM_STATUS_SUCCESS;
721 : : }
722 : :
723 : 8 : libspdm_return_t libspdm_transport_storage_encode_discovery_response(
724 : : size_t *transport_message_size,
725 : : void *transport_message)
726 : : {
727 : : spdm_storage_discovery_response_t *discovery_response;
728 : :
729 [ + - + - ]: 8 : if (!transport_message || !transport_message_size
730 [ - + ]: 8 : || *transport_message_size == 0) {
731 : 0 : return LIBSPDM_STATUS_INVALID_MSG_SIZE;
732 : : }
733 : :
734 [ + + ]: 8 : if (*transport_message_size < sizeof(spdm_storage_discovery_response_t)) {
735 : 4 : return LIBSPDM_STATUS_BUFFER_TOO_SMALL;
736 : : }
737 : :
738 : 4 : *transport_message_size = sizeof(spdm_storage_discovery_response_t);
739 : 4 : libspdm_zero_mem(transport_message, *transport_message_size);
740 : 4 : discovery_response = transport_message;
741 : :
742 : 4 : discovery_response->storage_response_headers.data_length = (uint16_t)*transport_message_size;
743 : 4 : discovery_response->storage_response_headers.storage_binding_version =
744 : : SPDM_STORAGE_SECURITY_BINDING_VERSION;
745 : : /* 1 supported connection (0's based) */
746 : 4 : discovery_response->conn_params = 0;
747 : 4 : discovery_response->supported_operations = (1 << SPDM_STORAGE_OPERATION_CODE_DISCOVERY)
748 : : | (1 << SPDM_STORAGE_OPERATION_CODE_PENDING_INFO)
749 : : | (1 << SPDM_STORAGE_OPERATION_CODE_MESSAGE)
750 : : | (1 <<
751 : : SPDM_STORAGE_OPERATION_CODE_SECURED_MESSAGE);
752 : :
753 : 4 : return LIBSPDM_STATUS_SUCCESS;
754 : : }
755 : :
756 : 14 : libspdm_return_t libspdm_transport_storage_encode_pending_info_response(
757 : : size_t *transport_message_size,
758 : : void *transport_message, bool response_pending,
759 : : uint32_t pending_response_length)
760 : : {
761 : : spdm_storage_pending_info_response_t *pending_info_response;
762 : :
763 [ + - + + ]: 14 : if (!transport_message || !transport_message_size
764 [ - + ]: 12 : || *transport_message_size == 0) {
765 : 2 : return LIBSPDM_STATUS_INVALID_MSG_SIZE;
766 : : }
767 : :
768 [ + + ]: 12 : if (*transport_message_size < sizeof(spdm_storage_pending_info_response_t)) {
769 : 4 : return LIBSPDM_STATUS_BUFFER_TOO_SMALL;
770 : : }
771 : :
772 : 8 : *transport_message_size = sizeof(spdm_storage_pending_info_response_t);
773 : 8 : libspdm_zero_mem(transport_message, *transport_message_size);
774 : 8 : pending_info_response = transport_message;
775 : :
776 : 8 : pending_info_response->storage_response_headers.data_length = (uint16_t)*transport_message_size;
777 : 8 : pending_info_response->storage_response_headers.storage_binding_version =
778 : : SPDM_STORAGE_SECURITY_BINDING_VERSION;
779 : :
780 [ + + ]: 8 : if (response_pending) {
781 : 4 : pending_info_response->pending_info_flag = (1 << 0);
782 : 4 : pending_info_response->response_length = pending_response_length;
783 : : }
784 : :
785 : 8 : return LIBSPDM_STATUS_SUCCESS;
786 : : }
787 : :
788 : 10 : libspdm_return_t libspdm_transport_storage_decode_management_cmd(
789 : : size_t transport_message_size,
790 : : const void *transport_message,
791 : : uint8_t *transport_command)
792 : : {
793 : : const libspdm_storage_transport_virtual_header_t *storage_header;
794 : : uint16_t security_protocol_specific;
795 : : uint8_t spsp0, spsp1, spdm_operation;
796 : :
797 [ + - - + ]: 10 : if (!transport_message || transport_message_size == 0) {
798 : 0 : return LIBSPDM_STATUS_INVALID_MSG_SIZE;
799 : : }
800 : :
801 : 10 : LIBSPDM_ASSERT(transport_message_size >= sizeof(libspdm_storage_transport_virtual_header_t));
802 : :
803 [ - + ]: 10 : if (transport_message_size < sizeof(libspdm_storage_transport_virtual_header_t)) {
804 : 0 : return LIBSPDM_STATUS_INVALID_MSG_SIZE;
805 : : }
806 : :
807 : 10 : storage_header = transport_message;
808 [ + + ]: 10 : if (storage_header->security_protocol != SPDM_STORAGE_SECURITY_PROTOCOL_DMTF) {
809 : 2 : return LIBSPDM_STATUS_INVALID_MSG_FIELD;
810 : : }
811 : :
812 : 8 : security_protocol_specific = storage_header->security_protocol_specific;
813 : 8 : spsp0 = security_protocol_specific & 0xFF;
814 : 8 : spsp1 = security_protocol_specific >> 8;
815 : :
816 [ + + ]: 8 : if (spsp1 != 0) {
817 : 2 : return LIBSPDM_STATUS_INVALID_MSG_FIELD;
818 : : }
819 : :
820 : 6 : spdm_operation = (spsp0 & 0xFC) >> 2;
821 : :
822 [ + + ]: 6 : switch (spdm_operation) {
823 : 4 : case SPDM_STORAGE_OPERATION_CODE_DISCOVERY:
824 : : case SPDM_STORAGE_OPERATION_CODE_PENDING_INFO:
825 : : case SPDM_STORAGE_OPERATION_CODE_MESSAGE:
826 : : case SPDM_STORAGE_OPERATION_CODE_SECURED_MESSAGE:
827 : 4 : *transport_command = spdm_operation;
828 : 4 : break;
829 : 2 : default:
830 : 2 : *transport_command = 0;
831 : 2 : return LIBSPDM_STATUS_UNSUPPORTED_CAP;
832 : : }
833 : :
834 : 4 : return LIBSPDM_STATUS_SUCCESS;
835 : : }
|