Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial implementation of SPDM over TCP #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,7 @@ if(ENABLE_CODEQL STREQUAL "ON")
ADD_SUBDIRECTORY(library/spdm_secured_message_lib)
ADD_SUBDIRECTORY(library/spdm_transport_mctp_lib)
ADD_SUBDIRECTORY(library/spdm_transport_pcidoe_lib)
ADD_SUBDIRECTORY(library/spdm_transport_tcp_lib)
ADD_SUBDIRECTORY(os_stub/memlib)
ADD_SUBDIRECTORY(os_stub/debuglib)
ADD_SUBDIRECTORY(os_stub/debuglib_null)
Expand Down Expand Up @@ -848,6 +849,7 @@ else()
ADD_SUBDIRECTORY(library/spdm_secured_message_lib)
ADD_SUBDIRECTORY(library/spdm_transport_mctp_lib)
ADD_SUBDIRECTORY(library/spdm_transport_pcidoe_lib)
ADD_SUBDIRECTORY(library/spdm_transport_tcp_lib)
ADD_SUBDIRECTORY(os_stub/memlib)
ADD_SUBDIRECTORY(os_stub/debuglib)
ADD_SUBDIRECTORY(os_stub/debuglib_null)
Expand Down
136 changes: 136 additions & 0 deletions include/library/spdm_transport_tcp_lib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/**
* Copyright Notice:
* Copyright 2022-2023 DMTF. All rights reserved.
* License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/libspdm/blob/main/LICENSE.md
**/

#ifndef SPDM_TCP_TRANSPORT_LIB_H
#define SPDM_TCP_TRANSPORT_LIB_H

#include "library/spdm_common_lib.h"

#pragma pack(1)

typedef struct {
uint16_t payload_length;
uint8_t reserved;
uint8_t message_type;
} tcp_spdm_binding_header_t;

#define TCP_MESSAGE_TYPE_OUT_OF_SESSION 0x01
#define TCP_MESSAGE_TYPE_IN_SESSION 0x02
#define TCP_MESSAGE_TYPE_HANDSHAKE_REQUEST 0x01

#pragma pack()

/**
* Encode an SPDM or APP message to a transport layer message.
*
* For normal SPDM message, it adds the transport layer wrapper.
* For secured SPDM message, it encrypts a secured message then adds the transport layer wrapper.
* For secured APP message, it encrypts a secured message then adds the transport layer wrapper.
*
* The APP message is encoded to a secured message directly in SPDM session.
* The APP message format is defined by the transport layer.
* Take TCP as example: APP message == TCP-SPDM-BINDING-HEADER + SPDM message
*
* @param spdm_context A pointer to the SPDM context.
* @param session_id Indicates if it is a secured message protected via SPDM session.
* If session_id is NULL, it is a normal message.
* If session_id is NOT NULL, it is a secured message.
* @param is_app_message Indicates if it is an APP message or SPDM message.
* @param is_requester Indicates if it is a requester message.
* @param message_size size in bytes of the message data buffer.
* @param message A pointer to a source buffer to store the message.
* For normal message, it shall point to the acquired sender buffer.
* For secured message, it shall point to the scratch buffer in spdm_context.
* @param transport_message_size size in bytes of the transport message data buffer.
* @param transport_message A pointer to a destination buffer to store the transport message.
* On input, it shall be msg_buf_ptr from sender buffer.
* On output, it will point to acquired sender buffer.
*
* @retval RETURN_SUCCESS The message is encoded successfully.
* @retval RETURN_INVALID_PARAMETER The message is NULL or the message_size is zero.
**/
libspdm_return_t libspdm_transport_tcp_encode_message(
void *spdm_context, const uint32_t *session_id, bool is_app_message,
bool is_requester, size_t message_size, void *message,
size_t *transport_message_size, void **transport_message);

/**
* Decode an SPDM or APP message from a transport layer message.
*
* For normal SPDM message, it removes the transport layer wrapper,
* For secured SPDM message, it removes the transport layer wrapper, then decrypts and verifies a secured message.
* For secured APP message, it removes the transport layer wrapper, then decrypts and verifies a secured message.
*
* The APP message is decoded from a secured message directly in SPDM session.
* The APP message format is defined by the transport layer.
* Take TCP as example: APP message == TCP-SPDM-BINDING-HEADER + SPDM message (For out-of-session message)
*
* @param spdm_context A pointer to the SPDM context.
* @param session_id Indicates if it is a secured message protected via SPDM session.
* If *session_id is NULL, it is a normal message.
* If *session_id is NOT NULL, it is a secured message.
* @param is_app_message Indicates if it is an APP message or SPDM message.
* @param is_requester Indicates if it is a requester message.
* @param transport_message_size size in bytes of the transport message data buffer.
* @param transport_message A pointer to a source buffer to store the transport message.
* For normal message or secured message, it shall point to acquired receiver buffer.
* @param message_size size in bytes of the message data buffer.
* @param message A pointer to a destination buffer to store the message.
* On input, it shall point to the scratch buffer in spdm_context.
* On output, for normal message, it will point to the original receiver buffer.
* On output, for secured message, it will point to the scratch buffer in spdm_context.
*
* @retval RETURN_SUCCESS The message is decoded successfully.
* @retval RETURN_INVALID_PARAMETER The message is NULL or the message_size is zero.
* @retval RETURN_UNSUPPORTED The transport_message is unsupported.
**/
libspdm_return_t libspdm_transport_tcp_decode_message(
void *spdm_context, uint32_t **session_id,
bool *is_app_message, bool is_requester,
size_t transport_message_size, void *transport_message,
size_t *message_size, void **message);

/**
* Return the maximum transport layer message header size.
* Transport Message Header Size + sizeof(spdm_secured_message_cipher_header_t))
*
* For TCP, Transport Message Header Size = sizeof(tcp_spdm_binding_header_t)
* For PCI_DOE, Transport Message Header Size = sizeof(pci_doe_data_object_header_t)
*
* @param spdm_context A pointer to the SPDM context.
*
* @return size of maximum transport layer message header size
**/
uint32_t libspdm_transport_tcp_get_header_size(
void *spdm_context);

/**
* Get sequence number in an SPDM secure message.
*
* This value is transport layer specific.
*
* @param sequence_number The current sequence number used to encode or decode message.
* @param sequence_number_buffer A buffer to hold the sequence number output used in the secured message.
* The size in byte of the output buffer shall be 8.
*
* @return size in byte of the sequence_number_buffer.
* It shall be no greater than 8.
* 0 means no sequence number is required.
**/
uint8_t libspdm_tcp_get_sequence_number(uint64_t sequence_number,
uint8_t *sequence_number_buffer);

/**
* Return max random number count in an SPDM secure message.
*
* This value is transport layer specific.
*
* @return Max random number count in an SPDM secured message.
* 0 means no randum number is required.
**/
uint32_t libspdm_tcp_get_max_random_number_count(void);

#endif /* SPDM_TCP_TRANSPORT_LIB_H */
10 changes: 10 additions & 0 deletions library/spdm_transport_tcp_lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 2.8.12)

INCLUDE_DIRECTORIES(${LIBSPDM_DIR}/include)

SET(src_spdm_transport_tcp_lib
libspdm_tcp_common.c
libspdm_tcp_tcp.c
)

ADD_LIBRARY(spdm_transport_tcp_lib STATIC ${src_spdm_transport_tcp_lib})
Loading