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