LCOV - code coverage report
Current view: top level - library/spdm_requester_lib - libspdm_req_end_session.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 80.8 % 78 63
Test Date: 2025-10-12 08:10:56 Functions: 100.0 % 2 2

            Line data    Source code
       1              : /**
       2              :  *  Copyright Notice:
       3              :  *  Copyright 2021-2025 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              : #include "internal/libspdm_secured_message_lib.h"
       9              : 
      10              : #if (LIBSPDM_ENABLE_CAPABILITY_KEY_EX_CAP) || (LIBSPDM_ENABLE_CAPABILITY_PSK_CAP)
      11              : 
      12              : #pragma pack(1)
      13              : typedef struct {
      14              :     spdm_message_header_t header;
      15              :     uint8_t dummy_data[sizeof(spdm_error_data_response_not_ready_t)];
      16              : } libspdm_end_session_response_mine_t;
      17              : #pragma pack()
      18              : 
      19              : /**
      20              :  * This function sends END_SESSION and receives END_SESSION_ACK for SPDM session end.
      21              :  *
      22              :  * @param  spdm_context                  A pointer to the SPDM context.
      23              :  * @param  session_id                    session_id to the END_SESSION request.
      24              :  * @param  end_session_attributes         end_session_attributes to the END_SESSION_ACK request.
      25              :  **/
      26           30 : static libspdm_return_t libspdm_try_send_receive_end_session(libspdm_context_t *spdm_context,
      27              :                                                              uint32_t session_id,
      28              :                                                              uint8_t end_session_attributes)
      29              : {
      30              :     libspdm_return_t status;
      31              :     spdm_end_session_request_t *spdm_request;
      32              :     size_t spdm_request_size;
      33              :     libspdm_end_session_response_mine_t *spdm_response;
      34              :     size_t spdm_response_size;
      35              :     libspdm_session_info_t *session_info;
      36              :     libspdm_session_state_t session_state;
      37              :     uint8_t *message;
      38              :     size_t message_size;
      39              :     size_t transport_header_size;
      40              : 
      41           30 :     if (libspdm_get_connection_version(spdm_context) < SPDM_MESSAGE_VERSION_11) {
      42            0 :         return LIBSPDM_STATUS_UNSUPPORTED_CAP;
      43              :     }
      44              : 
      45           30 :     if (spdm_context->connection_info.connection_state <
      46              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED) {
      47            1 :         return LIBSPDM_STATUS_INVALID_STATE_LOCAL;
      48              :     }
      49           29 :     session_info = libspdm_get_session_info_via_session_id(spdm_context, session_id);
      50           29 :     if (session_info == NULL) {
      51            0 :         LIBSPDM_ASSERT(false);
      52            0 :         return LIBSPDM_STATUS_INVALID_STATE_LOCAL;
      53              :     }
      54           29 :     session_state = libspdm_secured_message_get_session_state(
      55              :         session_info->secured_message_context);
      56           29 :     if (session_state != LIBSPDM_SESSION_STATE_ESTABLISHED) {
      57            0 :         return LIBSPDM_STATUS_INVALID_STATE_LOCAL;
      58              :     }
      59              : 
      60           29 :     if (!libspdm_is_capabilities_flag_supported(
      61              :             spdm_context, true, 0,
      62              :             SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CACHE_CAP)) {
      63           29 :         end_session_attributes = 0;
      64              :     }
      65              : 
      66           29 :     transport_header_size = spdm_context->local_context.capability.transport_header_size;
      67           29 :     status = libspdm_acquire_sender_buffer (spdm_context, &message_size, (void **)&message);
      68           29 :     if (LIBSPDM_STATUS_IS_ERROR(status)) {
      69            0 :         return status;
      70              :     }
      71           29 :     LIBSPDM_ASSERT (message_size >= transport_header_size +
      72              :                     spdm_context->local_context.capability.transport_tail_size);
      73           29 :     spdm_request = (void *)(message + transport_header_size);
      74           29 :     spdm_request_size = message_size - transport_header_size -
      75           29 :                         spdm_context->local_context.capability.transport_tail_size;
      76              : 
      77           29 :     LIBSPDM_ASSERT (spdm_request_size >= sizeof(spdm_end_session_request_t));
      78           29 :     spdm_request->header.spdm_version = libspdm_get_connection_version (spdm_context);
      79           29 :     spdm_request->header.request_response_code = SPDM_END_SESSION;
      80           29 :     spdm_request->header.param1 = end_session_attributes;
      81           29 :     spdm_request->header.param2 = 0;
      82              : 
      83           29 :     spdm_request_size = sizeof(spdm_end_session_request_t);
      84           29 :     status = libspdm_send_spdm_request(spdm_context, &session_id, spdm_request_size, spdm_request);
      85           29 :     if (LIBSPDM_STATUS_IS_ERROR(status)) {
      86            1 :         libspdm_release_sender_buffer (spdm_context);
      87            1 :         return status;
      88              :     }
      89              : 
      90           28 :     libspdm_reset_message_buffer_via_request_code(spdm_context, session_info, SPDM_END_SESSION);
      91              : 
      92           28 :     libspdm_release_sender_buffer (spdm_context);
      93           28 :     spdm_request = (void *)spdm_context->last_spdm_request;
      94              : 
      95              :     /* receive */
      96              : 
      97           28 :     status = libspdm_acquire_receiver_buffer (spdm_context, &message_size, (void **)&message);
      98           28 :     if (LIBSPDM_STATUS_IS_ERROR(status)) {
      99            0 :         return status;
     100              :     }
     101           28 :     LIBSPDM_ASSERT (message_size >= transport_header_size);
     102           28 :     spdm_response = (void *)(message);
     103           28 :     spdm_response_size = message_size;
     104              : 
     105           28 :     status = libspdm_receive_spdm_response(
     106              :         spdm_context, &session_id, &spdm_response_size, (void **)&spdm_response);
     107           28 :     if (LIBSPDM_STATUS_IS_ERROR(status)) {
     108            0 :         goto receive_done;
     109              :     }
     110           28 :     if (spdm_response_size < sizeof(spdm_message_header_t)) {
     111            0 :         status = LIBSPDM_STATUS_INVALID_MSG_SIZE;
     112            0 :         goto receive_done;
     113              :     }
     114           28 :     if (spdm_response->header.request_response_code == SPDM_ERROR) {
     115           25 :         status = libspdm_handle_error_response_main(
     116              :             spdm_context, &session_id, &spdm_response_size,
     117              :             (void **)&spdm_response, SPDM_END_SESSION, SPDM_END_SESSION_ACK);
     118           25 :         if (LIBSPDM_STATUS_IS_ERROR(status)) {
     119           24 :             goto receive_done;
     120              :         }
     121            3 :     } else if (spdm_response->header.request_response_code != SPDM_END_SESSION_ACK) {
     122            0 :         status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
     123            0 :         goto receive_done;
     124              :     }
     125            4 :     if (spdm_response->header.spdm_version != spdm_request->header.spdm_version) {
     126            0 :         status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
     127            0 :         goto receive_done;
     128              :     }
     129              :     /* this message can only be in secured session
     130              :      * thus don't need to consider transport layer padding, just check its exact size */
     131            4 :     if (spdm_response_size != sizeof(spdm_end_session_response_t)) {
     132            0 :         status = LIBSPDM_STATUS_INVALID_MSG_SIZE;
     133            0 :         goto receive_done;
     134              :     }
     135              : 
     136            4 :     session_info->end_session_attributes = end_session_attributes;
     137              : 
     138            4 :     libspdm_secured_message_set_session_state(
     139              :         session_info->secured_message_context,
     140              :         LIBSPDM_SESSION_STATE_NOT_STARTED);
     141            4 :     libspdm_free_session_id(spdm_context, session_id);
     142              : 
     143            4 :     status = LIBSPDM_STATUS_SUCCESS;
     144              : 
     145              :     /* -=[Log Message Phase]=- */
     146              :     #if LIBSPDM_ENABLE_MSG_LOG
     147            4 :     libspdm_append_msg_log(spdm_context, spdm_response, spdm_response_size);
     148              :     #endif /* LIBSPDM_ENABLE_MSG_LOG */
     149              : 
     150           28 : receive_done:
     151           28 :     libspdm_release_receiver_buffer (spdm_context);
     152           28 :     return status;
     153              : }
     154              : 
     155           29 : libspdm_return_t libspdm_send_receive_end_session(libspdm_context_t *spdm_context,
     156              :                                                   uint32_t session_id,
     157              :                                                   uint8_t end_session_attributes)
     158              : {
     159              :     size_t retry;
     160              :     uint64_t retry_delay_time;
     161              :     libspdm_return_t status;
     162              : 
     163           29 :     spdm_context->crypto_request = true;
     164           29 :     retry = spdm_context->retry_times;
     165           29 :     retry_delay_time = spdm_context->retry_delay_time;
     166              :     do {
     167           30 :         status = libspdm_try_send_receive_end_session(
     168              :             spdm_context, session_id, end_session_attributes);
     169           30 :         if (status != LIBSPDM_STATUS_BUSY_PEER) {
     170           28 :             return status;
     171              :         }
     172              : 
     173            2 :         libspdm_sleep(retry_delay_time);
     174            2 :     } while (retry-- != 0);
     175              : 
     176            1 :     return status;
     177              : }
     178              : 
     179              : #endif /* (LIBSPDM_ENABLE_CAPABILITY_KEY_EX_CAP) || (LIBSPDM_ENABLE_CAPABILITY_PSK_CAP) */
        

Generated by: LCOV version 2.0-1