From eb727481bb2740e4ac619803bd89ed119ee5649b Mon Sep 17 00:00:00 2001 From: Wenxing Hou Date: Thu, 1 Dec 2022 14:51:19 +0800 Subject: [PATCH] Fix session_id generate Fix the issue: #1424 Make session_id generate right. Signed-off-by: Wenxing Hou --- include/internal/libspdm_common_lib.h | 10 ++++++++++ .../libspdm_com_context_data_session.c | 15 +++++++++++++++ .../spdm_requester_lib/libspdm_req_key_exchange.c | 2 +- .../spdm_requester_lib/libspdm_req_psk_exchange.c | 2 +- .../spdm_responder_lib/libspdm_rsp_key_exchange.c | 2 +- .../spdm_responder_lib/libspdm_rsp_psk_exchange.c | 2 +- 6 files changed, 29 insertions(+), 4 deletions(-) diff --git a/include/internal/libspdm_common_lib.h b/include/internal/libspdm_common_lib.h index 2586a27eb64..4dc1732ace0 100644 --- a/include/internal/libspdm_common_lib.h +++ b/include/internal/libspdm_common_lib.h @@ -1029,6 +1029,16 @@ libspdm_return_t libspdm_append_message_f(void *context, void *spdm_session_info bool is_requester, const void *message, size_t message_size); +/** + * This function generate a new session ID by concatnating req_session_id and rsp_session_id. + * + * @param[in] req_session_id + * @param[in] rsp_session_id + * + * @return this new session ID. + **/ +uint32_t libspdm_generate_session_id(uint16_t req_session_id, uint16_t rsp_session_id); + /** * This function assigns a new session ID. * diff --git a/library/spdm_common_lib/libspdm_com_context_data_session.c b/library/spdm_common_lib/libspdm_com_context_data_session.c index 582f81a9f48..78180423cdf 100644 --- a/library/spdm_common_lib/libspdm_com_context_data_session.c +++ b/library/spdm_common_lib/libspdm_com_context_data_session.c @@ -169,6 +169,21 @@ void *libspdm_get_secured_message_context_via_session_info(void *spdm_session_in } } +/** + * This function generate a new session ID by concatnating req_session_id and rsp_session_id. + * + * @param[in] req_session_id + * @param[in] rsp_session_id + * + * @return this new session ID. + **/ +uint32_t libspdm_generate_session_id(uint16_t req_session_id, uint16_t rsp_session_id) +{ + uint32_t session_id; + session_id = (rsp_session_id << 16) | req_session_id; + return session_id; +} + /** * This function assigns a new session ID. * diff --git a/library/spdm_requester_lib/libspdm_req_key_exchange.c b/library/spdm_requester_lib/libspdm_req_key_exchange.c index 5d9548cf32f..6f1cc824d0c 100644 --- a/library/spdm_requester_lib/libspdm_req_key_exchange.c +++ b/library/spdm_requester_lib/libspdm_req_key_exchange.c @@ -505,7 +505,7 @@ static libspdm_return_t libspdm_try_send_receive_key_exchange( } rsp_session_id = spdm_response->rsp_session_id; - *session_id = (req_session_id << 16) | rsp_session_id; + *session_id = libspdm_generate_session_id(req_session_id, rsp_session_id); session_info = libspdm_assign_session_id(spdm_context, *session_id, false); /* session_info cannot be null as the check after libspdm_allocate_req_session_id assures diff --git a/library/spdm_requester_lib/libspdm_req_psk_exchange.c b/library/spdm_requester_lib/libspdm_req_psk_exchange.c index 904d13488e7..79f5446a44f 100644 --- a/library/spdm_requester_lib/libspdm_req_psk_exchange.c +++ b/library/spdm_requester_lib/libspdm_req_psk_exchange.c @@ -330,7 +330,7 @@ static libspdm_return_t libspdm_try_send_receive_psk_exchange( *heartbeat_period = spdm_response->header.param1; } rsp_session_id = spdm_response->rsp_session_id; - *session_id = (req_session_id << 16) | rsp_session_id; + *session_id = libspdm_generate_session_id(req_session_id, rsp_session_id); session_info = libspdm_assign_session_id(spdm_context, *session_id, true); if (session_info == NULL) { status = LIBSPDM_STATUS_SESSION_NUMBER_EXCEED; diff --git a/library/spdm_responder_lib/libspdm_rsp_key_exchange.c b/library/spdm_responder_lib/libspdm_rsp_key_exchange.c index 3d7b90d45db..a67942a9042 100644 --- a/library/spdm_responder_lib/libspdm_rsp_key_exchange.c +++ b/library/spdm_responder_lib/libspdm_rsp_key_exchange.c @@ -318,7 +318,7 @@ libspdm_return_t libspdm_get_response_key_exchange(void *context, spdm_context, SPDM_ERROR_CODE_SESSION_LIMIT_EXCEEDED, 0, response_size, response); } - session_id = (req_session_id << 16) | rsp_session_id; + session_id = libspdm_generate_session_id(req_session_id, rsp_session_id); session_info = libspdm_assign_session_id(spdm_context, session_id, false); if (session_info == NULL) { return libspdm_generate_error_response( diff --git a/library/spdm_responder_lib/libspdm_rsp_psk_exchange.c b/library/spdm_responder_lib/libspdm_rsp_psk_exchange.c index 0c70d278fd8..bf1e6651698 100644 --- a/library/spdm_responder_lib/libspdm_rsp_psk_exchange.c +++ b/library/spdm_responder_lib/libspdm_rsp_psk_exchange.c @@ -260,7 +260,7 @@ libspdm_return_t libspdm_get_response_psk_exchange(void *context, spdm_context, SPDM_ERROR_CODE_INVALID_REQUEST, 0, response_size, response); } - session_id = (req_session_id << 16) | rsp_session_id; + session_id = libspdm_generate_session_id(req_session_id, rsp_session_id); session_info = libspdm_assign_session_id(spdm_context, session_id, true); if (session_info == NULL) { return libspdm_generate_error_response(