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_responder_lib.h"
8 : #include "internal/libspdm_secured_message_lib.h"
9 :
10 : #if LIBSPDM_ENABLE_CAPABILITY_PSK_CAP
11 :
12 6 : bool libspdm_verify_psk_finish_req_hmac(libspdm_context_t *spdm_context,
13 : libspdm_session_info_t *session_info,
14 : const uint8_t *hmac, size_t hmac_size)
15 : {
16 : uint8_t hmac_data[LIBSPDM_MAX_HASH_SIZE];
17 : size_t hash_size;
18 : bool result;
19 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
20 : uint8_t *th_curr_data;
21 : size_t th_curr_data_size;
22 : libspdm_th_managed_buffer_t th_curr;
23 : uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
24 : #endif
25 :
26 6 : hash_size = libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
27 6 : LIBSPDM_ASSERT(hmac_size == hash_size);
28 :
29 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
30 : result = libspdm_calculate_th_for_finish(spdm_context, session_info, NULL,
31 : 0, NULL, 0, &th_curr);
32 : if (!result) {
33 : return false;
34 : }
35 : th_curr_data = libspdm_get_managed_buffer(&th_curr);
36 : th_curr_data_size = libspdm_get_managed_buffer_size(&th_curr);
37 :
38 : result = libspdm_hash_all (spdm_context->connection_info.algorithm.base_hash_algo,
39 : th_curr_data, th_curr_data_size, hash_data);
40 : if (!result) {
41 : return false;
42 : }
43 :
44 : result = libspdm_hmac_all_with_request_finished_key(
45 : session_info->secured_message_context, hash_data,
46 : hash_size, hmac_data);
47 : if (!result) {
48 : return false;
49 : }
50 : #else
51 6 : result = libspdm_calculate_th_hmac_for_finish_req(
52 : spdm_context, session_info, &hash_size, hmac_data);
53 6 : if (!result) {
54 0 : return false;
55 : }
56 : #endif
57 6 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "Calc th_curr hmac - "));
58 6 : LIBSPDM_INTERNAL_DUMP_DATA(hmac_data, hash_size);
59 6 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "\n"));
60 :
61 6 : if (!libspdm_consttime_is_mem_equal(hmac, hmac_data, hash_size)) {
62 2 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR, "!!! verify_psk_finish_req_hmac - FAIL !!!\n"));
63 2 : return false;
64 : }
65 4 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "!!! verify_psk_finish_req_hmac - PASS !!!\n"));
66 4 : return true;
67 : }
68 :
69 15 : libspdm_return_t libspdm_get_response_psk_finish(libspdm_context_t *spdm_context,
70 : size_t request_size,
71 : const void *request,
72 : size_t *response_size,
73 : void *response)
74 : {
75 : uint32_t session_id;
76 : bool result;
77 : uint32_t hmac_size;
78 : spdm_psk_finish_response_t *spdm_response;
79 : libspdm_session_info_t *session_info;
80 : uint8_t th2_hash_data[LIBSPDM_MAX_HASH_SIZE];
81 : const spdm_psk_finish_request_t *spdm_request;
82 : libspdm_return_t status;
83 : libspdm_session_state_t session_state;
84 :
85 15 : spdm_request = request;
86 :
87 : /* -=[Check Parameters Phase]=- */
88 15 : LIBSPDM_ASSERT(spdm_request->header.request_response_code == SPDM_PSK_FINISH);
89 :
90 15 : if (libspdm_get_connection_version(spdm_context) < SPDM_MESSAGE_VERSION_11) {
91 0 : return libspdm_generate_error_response(spdm_context,
92 : SPDM_ERROR_CODE_UNSUPPORTED_REQUEST,
93 : SPDM_PSK_FINISH,
94 : response_size, response);
95 : }
96 :
97 15 : if (spdm_request->header.spdm_version != libspdm_get_connection_version(spdm_context)) {
98 0 : return libspdm_generate_error_response(spdm_context,
99 : SPDM_ERROR_CODE_VERSION_MISMATCH, 0,
100 : response_size, response);
101 : }
102 15 : if (spdm_context->response_state != LIBSPDM_RESPONSE_STATE_NORMAL) {
103 3 : return libspdm_responder_handle_response_state(
104 : spdm_context,
105 3 : spdm_request->header.request_response_code,
106 : response_size, response);
107 : }
108 12 : if (!libspdm_is_capabilities_flag_supported(
109 : spdm_context, false,
110 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PSK_CAP,
111 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PSK_CAP_RESPONDER_WITH_CONTEXT)) {
112 1 : return libspdm_generate_error_response(
113 : spdm_context, SPDM_ERROR_CODE_UNSUPPORTED_REQUEST,
114 : SPDM_PSK_FINISH, response_size, response);
115 : }
116 11 : if (spdm_context->connection_info.connection_state < LIBSPDM_CONNECTION_STATE_NEGOTIATED) {
117 1 : return libspdm_generate_error_response(spdm_context,
118 : SPDM_ERROR_CODE_UNEXPECTED_REQUEST,
119 : 0, response_size, response);
120 : }
121 :
122 10 : if (!spdm_context->last_spdm_request_session_id_valid) {
123 0 : if (libspdm_get_connection_version(spdm_context) >= SPDM_MESSAGE_VERSION_12) {
124 0 : return libspdm_generate_error_response(spdm_context,
125 : SPDM_ERROR_CODE_SESSION_REQUIRED, 0,
126 : response_size, response);
127 : } else {
128 0 : return libspdm_generate_error_response(spdm_context,
129 : SPDM_ERROR_CODE_UNSPECIFIED, 0,
130 : response_size, response);
131 : }
132 : }
133 10 : session_id = spdm_context->last_spdm_request_session_id;
134 : session_info =
135 10 : libspdm_get_session_info_via_session_id(spdm_context, session_id);
136 10 : if (session_info == NULL) {
137 0 : if (libspdm_get_connection_version(spdm_context) >= SPDM_MESSAGE_VERSION_12) {
138 0 : return libspdm_generate_error_response(spdm_context,
139 : SPDM_ERROR_CODE_SESSION_REQUIRED, 0,
140 : response_size, response);
141 : } else {
142 0 : return libspdm_generate_error_response(spdm_context,
143 : SPDM_ERROR_CODE_UNSPECIFIED, 0,
144 : response_size, response);
145 : }
146 : }
147 10 : if (!session_info->use_psk) {
148 0 : return libspdm_generate_error_response(spdm_context,
149 : SPDM_ERROR_CODE_UNEXPECTED_REQUEST, 0,
150 : response_size, response);
151 : }
152 10 : session_state = libspdm_secured_message_get_session_state(
153 : session_info->secured_message_context);
154 10 : if (session_state != LIBSPDM_SESSION_STATE_HANDSHAKING) {
155 1 : return libspdm_generate_error_response(spdm_context,
156 : SPDM_ERROR_CODE_UNEXPECTED_REQUEST, 0,
157 : response_size, response);
158 : }
159 :
160 : /* remove HMAC*/
161 9 : hmac_size = libspdm_get_hash_size(
162 : spdm_context->connection_info.algorithm.base_hash_algo);
163 :
164 : /* this message can only be in secured session
165 : * thus don't need to consider transport layer padding, just check its exact size */
166 9 : if (request_size != sizeof(spdm_psk_finish_request_t) + hmac_size) {
167 3 : return libspdm_generate_error_response(spdm_context,
168 : SPDM_ERROR_CODE_INVALID_REQUEST, 0,
169 : response_size, response);
170 : }
171 :
172 6 : libspdm_reset_message_buffer_via_request_code(spdm_context, session_info,
173 6 : spdm_request->header.request_response_code);
174 :
175 6 : status = libspdm_append_message_f(spdm_context, session_info, false, request,
176 : request_size - hmac_size);
177 6 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
178 0 : return libspdm_generate_error_response(spdm_context,
179 : SPDM_ERROR_CODE_UNSPECIFIED, 0,
180 : response_size, response);
181 : }
182 :
183 6 : LIBSPDM_ASSERT(*response_size >= sizeof(spdm_psk_finish_response_t));
184 6 : *response_size = sizeof(spdm_psk_finish_response_t);
185 6 : libspdm_zero_mem(response, *response_size);
186 6 : spdm_response = response;
187 :
188 6 : spdm_response->header.spdm_version = spdm_request->header.spdm_version;
189 6 : spdm_response->header.request_response_code = SPDM_PSK_FINISH_RSP;
190 6 : spdm_response->header.param1 = 0;
191 6 : spdm_response->header.param2 = 0;
192 :
193 6 : result = libspdm_verify_psk_finish_req_hmac(
194 : spdm_context, session_info,
195 : (const uint8_t *)request + sizeof(spdm_psk_finish_request_t),
196 : hmac_size);
197 6 : if (!result) {
198 2 : if((spdm_context->handle_error_return_policy &
199 : LIBSPDM_DATA_HANDLE_ERROR_RETURN_POLICY_DROP_ON_DECRYPT_ERROR) == 0) {
200 2 : return libspdm_generate_error_response(
201 : spdm_context, SPDM_ERROR_CODE_DECRYPT_ERROR, 0,
202 : response_size, response);
203 : } else {
204 : /**
205 : * just ignore this message
206 : * return UNSUPPORTED and clear response_size to continue the dispatch without send response
207 : **/
208 0 : *response_size = 0;
209 0 : return LIBSPDM_STATUS_UNSUPPORTED_CAP;
210 : }
211 : }
212 4 : status = libspdm_append_message_f(
213 : spdm_context, session_info, false,
214 4 : (const uint8_t *)request + request_size - hmac_size,
215 : hmac_size);
216 4 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
217 0 : return libspdm_generate_error_response(spdm_context,
218 : SPDM_ERROR_CODE_UNSPECIFIED, 0,
219 : response_size, response);
220 : }
221 :
222 4 : status = libspdm_append_message_f(spdm_context, session_info, false, spdm_response,
223 : *response_size);
224 4 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
225 0 : return libspdm_generate_error_response(spdm_context,
226 : SPDM_ERROR_CODE_UNSPECIFIED, 0,
227 : response_size, response);
228 : }
229 :
230 4 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "libspdm_generate_session_data_key[%x]\n", session_id));
231 4 : result = libspdm_calculate_th2_hash(spdm_context, session_info, false,
232 : th2_hash_data);
233 4 : if (!result) {
234 0 : return libspdm_generate_error_response(spdm_context,
235 : SPDM_ERROR_CODE_UNSPECIFIED, 0,
236 : response_size, response);
237 : }
238 4 : result = libspdm_generate_session_data_key(
239 : session_info->secured_message_context, th2_hash_data);
240 4 : if (!result) {
241 0 : return libspdm_generate_error_response(spdm_context,
242 : SPDM_ERROR_CODE_UNSPECIFIED, 0,
243 : response_size, response);
244 : }
245 :
246 : #if LIBSPDM_ENABLE_CAPABILITY_HBEAT_CAP
247 4 : if (libspdm_is_capabilities_flag_supported(
248 : spdm_context, false,
249 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HBEAT_CAP,
250 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HBEAT_CAP)) {
251 0 : result = libspdm_start_watchdog(
252 0 : session_id, spdm_context->local_context.heartbeat_period * 2);
253 0 : if (!result) {
254 0 : return libspdm_generate_error_response(spdm_context,
255 : SPDM_ERROR_CODE_UNSPECIFIED, 0,
256 : response_size, response);
257 : }
258 : }
259 : #endif /* LIBSPDM_ENABLE_CAPABILITY_HBEAT_CAP */
260 :
261 4 : return LIBSPDM_STATUS_SUCCESS;
262 : }
263 :
264 : #endif /* LIBSPDM_ENABLE_CAPABILITY_PSK_CAP */
|