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