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_responder_lib.h"
8 : :
9 : : #if LIBSPDM_ENABLE_CAPABILITY_CHAL_CAP
10 : :
11 : 64 : libspdm_return_t libspdm_get_response_challenge_auth(libspdm_context_t *spdm_context,
12 : : size_t request_size,
13 : : const void *request,
14 : : size_t *response_size,
15 : : void *response)
16 : : {
17 : : const spdm_challenge_request_t *spdm_request;
18 : : size_t spdm_request_size;
19 : : spdm_challenge_auth_response_t *spdm_response;
20 : : bool result;
21 : : size_t signature_size;
22 : : uint8_t slot_id;
23 : : uint32_t hash_size;
24 : : uint32_t measurement_summary_hash_size;
25 : : uint8_t *ptr;
26 : : uint8_t auth_attribute;
27 : : libspdm_return_t status;
28 : : uint8_t slot_mask;
29 : : uint8_t *opaque_data;
30 : : size_t opaque_data_size;
31 : : size_t request_context_size;
32 : : const void *request_context;
33 : : size_t spdm_response_size;
34 : :
35 : 64 : spdm_request = request;
36 : :
37 : : /* -=[Check Parameters Phase]=- */
38 : 64 : LIBSPDM_ASSERT(spdm_request->header.request_response_code == SPDM_CHALLENGE);
39 : :
40 [ + + ]: 64 : if (spdm_request->header.spdm_version != libspdm_get_connection_version(spdm_context)) {
41 : 4 : return libspdm_generate_error_response(spdm_context,
42 : : SPDM_ERROR_CODE_VERSION_MISMATCH, 0,
43 : : response_size, response);
44 : : }
45 [ + + ]: 60 : if (spdm_context->response_state != LIBSPDM_RESPONSE_STATE_NORMAL) {
46 : 8 : return libspdm_responder_handle_response_state(
47 : : spdm_context,
48 : 8 : spdm_request->header.request_response_code,
49 : : response_size, response);
50 : : }
51 [ + + ]: 52 : if (spdm_context->last_spdm_request_session_id_valid) {
52 : 2 : return libspdm_generate_error_response(spdm_context,
53 : : SPDM_ERROR_CODE_UNEXPECTED_REQUEST, 0,
54 : : response_size, response);
55 : : }
56 [ + + ]: 50 : if (!libspdm_is_capabilities_flag_supported(
57 : : spdm_context, false, 0,
58 : : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CHAL_CAP)) {
59 : 4 : return libspdm_generate_error_response(
60 : : spdm_context, SPDM_ERROR_CODE_UNSUPPORTED_REQUEST,
61 : : SPDM_CHALLENGE, response_size, response);
62 : : }
63 [ + + ]: 46 : if (spdm_context->connection_info.connection_state < LIBSPDM_CONNECTION_STATE_NEGOTIATED) {
64 : 4 : return libspdm_generate_error_response(spdm_context,
65 : : SPDM_ERROR_CODE_UNEXPECTED_REQUEST,
66 : : 0, response_size, response);
67 : : }
68 : :
69 [ - + ]: 42 : if (request_size < sizeof(spdm_challenge_request_t)) {
70 : 0 : return libspdm_generate_error_response(spdm_context,
71 : : SPDM_ERROR_CODE_INVALID_REQUEST, 0,
72 : : response_size, response);
73 : : }
74 : 42 : spdm_request_size = sizeof(spdm_challenge_request_t);
75 [ + + ]: 42 : if (spdm_request->header.spdm_version >= SPDM_MESSAGE_VERSION_13) {
76 [ - + ]: 4 : if (request_size < sizeof(spdm_challenge_request_t) + SPDM_REQ_CONTEXT_SIZE) {
77 : 0 : return libspdm_generate_error_response(spdm_context,
78 : : SPDM_ERROR_CODE_INVALID_REQUEST, 0,
79 : : response_size, response);
80 : : }
81 : 4 : spdm_request_size += SPDM_REQ_CONTEXT_SIZE;
82 : : }
83 [ + + ]: 42 : if (spdm_request->header.param2 > 0) {
84 [ + + ]: 12 : if (!libspdm_is_capabilities_flag_supported(
85 : : spdm_context, false, 0,
86 : 6 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEAS_CAP) ||
87 [ + - ]: 6 : (spdm_context->connection_info.algorithm.measurement_spec == 0) ||
88 [ - + ]: 6 : (spdm_context->connection_info.algorithm.measurement_hash_algo == 0) ) {
89 : 6 : return libspdm_generate_error_response (spdm_context, SPDM_ERROR_CODE_INVALID_REQUEST,
90 : : 0, response_size, response);
91 : : }
92 : : }
93 : :
94 : 36 : slot_id = spdm_request->header.param1;
95 : :
96 [ + + + + ]: 36 : if ((slot_id != 0xFF) && (slot_id >= SPDM_MAX_SLOT_COUNT)) {
97 : 2 : return libspdm_generate_error_response(spdm_context,
98 : : SPDM_ERROR_CODE_INVALID_REQUEST, 0,
99 : : response_size, response);
100 : : }
101 : :
102 [ + + ]: 34 : if (slot_id != 0xFF) {
103 [ + + ]: 32 : if (spdm_context->local_context.local_cert_chain_provision[slot_id] == NULL) {
104 : 2 : return libspdm_generate_error_response(
105 : : spdm_context, SPDM_ERROR_CODE_INVALID_REQUEST,
106 : : 0, response_size, response);
107 : : }
108 : : } else {
109 [ - + ]: 2 : if (spdm_context->local_context.local_public_key_provision == NULL) {
110 : 0 : return libspdm_generate_error_response(
111 : : spdm_context, SPDM_ERROR_CODE_INVALID_REQUEST,
112 : : 0, response_size, response);
113 : : }
114 : : }
115 : :
116 [ + + ]: 32 : if ((spdm_request->header.spdm_version >= SPDM_MESSAGE_VERSION_13) &&
117 [ + + + - ]: 4 : spdm_context->connection_info.multi_key_conn_rsp &&
118 : : (slot_id != 0xFF)) {
119 [ + - ]: 2 : if ((spdm_context->local_context.local_key_usage_bit_mask[slot_id] &
120 : : SPDM_KEY_USAGE_BIT_MASK_CHALLENGE_USE) == 0) {
121 : 2 : return libspdm_generate_error_response(
122 : : spdm_context, SPDM_ERROR_CODE_INVALID_REQUEST,
123 : : 0, response_size, response);
124 : : }
125 : : }
126 : :
127 [ - + ]: 30 : if (spdm_context->connection_info.algorithm.pqc_asym_algo != 0) {
128 : 0 : signature_size = libspdm_get_pqc_asym_signature_size(
129 : : spdm_context->connection_info.algorithm.pqc_asym_algo);
130 : : } else {
131 : 30 : signature_size = libspdm_get_asym_signature_size(
132 : : spdm_context->connection_info.algorithm.base_asym_algo);
133 : : }
134 : 30 : hash_size = libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
135 : 30 : measurement_summary_hash_size = libspdm_get_measurement_summary_hash_size(
136 : 30 : spdm_context, false, spdm_request->header.param2);
137 [ + + ]: 30 : if ((measurement_summary_hash_size == 0) &&
138 [ - + ]: 24 : (spdm_request->header.param2 != SPDM_CHALLENGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH)) {
139 : 0 : return libspdm_generate_error_response(spdm_context,
140 : : SPDM_ERROR_CODE_INVALID_REQUEST,
141 : : 0, response_size, response);
142 : : }
143 : :
144 [ + + ]: 30 : if (spdm_request->header.spdm_version >= SPDM_MESSAGE_VERSION_13) {
145 : 2 : request_context_size = SPDM_REQ_CONTEXT_SIZE;
146 : 2 : request_context = spdm_request + 1;
147 : : } else {
148 : 28 : request_context_size = 0;
149 : 28 : request_context = NULL;
150 : : }
151 : :
152 : : /* response_size should be large enough to hold a challenge response without opaque data. */
153 : 30 : LIBSPDM_ASSERT(*response_size >= sizeof(spdm_challenge_auth_response_t) + hash_size +
154 : : SPDM_NONCE_SIZE + measurement_summary_hash_size + sizeof(uint16_t) +
155 : : SPDM_REQ_CONTEXT_SIZE + signature_size);
156 : :
157 : 30 : libspdm_zero_mem(response, *response_size);
158 : 30 : spdm_response = response;
159 : :
160 : 30 : libspdm_reset_message_buffer_via_request_code(spdm_context, NULL,
161 : 30 : spdm_request->header.request_response_code);
162 : :
163 : 30 : spdm_response->header.spdm_version = spdm_request->header.spdm_version;
164 : 30 : spdm_response->header.request_response_code = SPDM_CHALLENGE_AUTH;
165 : 30 : auth_attribute = (uint8_t)(slot_id & 0xF);
166 : :
167 : : #if (LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP) && (LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP) && \
168 : : (LIBSPDM_SEND_CHALLENGE_SUPPORT)
169 [ + - ]: 30 : if (spdm_request->header.spdm_version >= SPDM_MESSAGE_VERSION_11) {
170 [ + + ]: 30 : if (libspdm_is_capabilities_flag_supported(
171 : : spdm_context, false,
172 : : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MUT_AUTH_CAP,
173 [ + - ]: 2 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MUT_AUTH_CAP) &&
174 : 2 : libspdm_is_capabilities_flag_supported(
175 : : spdm_context, false,
176 [ - + ]: 2 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_CHAL_CAP, 0) &&
177 : 2 : (libspdm_is_capabilities_flag_supported(
178 : : spdm_context, false,
179 [ # # ]: 0 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_CERT_CAP, 0) ||
180 : 0 : libspdm_is_capabilities_flag_supported(
181 : : spdm_context, false,
182 : : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PUB_KEY_ID_CAP, 0))) {
183 [ + - ]: 2 : if (libspdm_challenge_start_mut_auth(spdm_context,
184 : 2 : spdm_context->connection_info.version,
185 : : slot_id,
186 : : request_context_size,
187 : : request_context)) {
188 : 2 : auth_attribute |= SPDM_CHALLENGE_AUTH_RESPONSE_ATTRIBUTE_BASIC_MUT_AUTH_REQ;
189 : 2 : libspdm_start_encap_flow(&spdm_context->encap_context,
190 : : LIBSPDM_ENCAP_FLOW_BASIC_MUT_AUTH);
191 : 2 : spdm_context->encap_context.payload_buffer_size = 0;
192 : 2 : libspdm_reset_message_mut_b(spdm_context);
193 : 2 : libspdm_reset_message_mut_c(spdm_context);
194 : 2 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO,
195 : : "Basic mutual authentication is a deprecated feature.\n"));
196 : : }
197 : : }
198 : : }
199 : : #endif /* (LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP) && (...) */
200 : :
201 : 30 : spdm_response->header.param1 = auth_attribute;
202 : :
203 [ + + ]: 30 : if (slot_id == 0xFF) {
204 : 2 : spdm_response->header.param2 = 0;
205 : : } else {
206 : 28 : slot_mask = libspdm_get_cert_slot_mask(spdm_context);
207 [ + - ]: 28 : if (slot_mask != 0) {
208 : 28 : spdm_response->header.param2 = slot_mask;
209 : : } else {
210 : 0 : return libspdm_generate_error_response(
211 : : spdm_context, SPDM_ERROR_CODE_UNSPECIFIED,
212 : : 0, response_size, response);
213 : : }
214 : : }
215 : :
216 : 30 : ptr = (void *)(spdm_response + 1);
217 [ + + ]: 30 : if (slot_id == 0xFF) {
218 : 2 : result = libspdm_generate_public_key_hash(spdm_context, ptr);
219 : : } else {
220 : 28 : result = libspdm_generate_cert_chain_hash(spdm_context, slot_id, ptr);
221 : : }
222 [ - + ]: 30 : if (!result) {
223 : 0 : return libspdm_generate_error_response(spdm_context,
224 : : SPDM_ERROR_CODE_UNSPECIFIED, 0,
225 : : response_size, response);
226 : : }
227 : 30 : ptr += hash_size;
228 : :
229 : 30 : result = libspdm_get_random_number(SPDM_NONCE_SIZE, ptr);
230 [ - + ]: 30 : if (!result) {
231 : 0 : return libspdm_generate_error_response(spdm_context,
232 : : SPDM_ERROR_CODE_UNSPECIFIED, 0,
233 : : response_size, response);
234 : : }
235 : 30 : ptr += SPDM_NONCE_SIZE;
236 : :
237 : : #if LIBSPDM_ENABLE_CAPABILITY_MEAS_CAP
238 [ + + ]: 30 : if (libspdm_is_capabilities_flag_supported(
239 : 6 : spdm_context, false, 0, SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEAS_CAP) &&
240 [ + + ]: 6 : ((spdm_request->header.param2 == SPDM_REQUEST_TCB_COMPONENT_MEASUREMENT_HASH) ||
241 [ + - ]: 4 : (spdm_request->header.param2 == SPDM_REQUEST_ALL_MEASUREMENTS_HASH))) {
242 : 6 : result = libspdm_generate_measurement_summary_hash(
243 : : spdm_context,
244 : 6 : spdm_context->connection_info.version,
245 : : spdm_context->connection_info.algorithm.base_hash_algo,
246 : 6 : spdm_context->connection_info.algorithm.measurement_spec,
247 : : spdm_context->connection_info.algorithm.measurement_hash_algo,
248 : 6 : spdm_request->header.param2,
249 : : ptr,
250 : : measurement_summary_hash_size);
251 : :
252 [ - + ]: 6 : if (!result) {
253 : 0 : return libspdm_generate_error_response(spdm_context,
254 : : SPDM_ERROR_CODE_UNSPECIFIED, 0,
255 : : response_size, response);
256 : : }
257 : : }
258 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_MEAS_CAP */
259 : :
260 : 30 : ptr += measurement_summary_hash_size;
261 : :
262 : 30 : opaque_data_size = *response_size - (sizeof(spdm_challenge_auth_response_t) + hash_size +
263 : 30 : SPDM_NONCE_SIZE + measurement_summary_hash_size +
264 : 30 : sizeof(uint16_t) + signature_size);
265 : 30 : opaque_data =
266 : 30 : (uint8_t*)response + sizeof(spdm_challenge_auth_response_t) + hash_size + SPDM_NONCE_SIZE +
267 : 30 : measurement_summary_hash_size + sizeof(uint16_t);
268 : :
269 [ + + ]: 30 : if ((libspdm_get_connection_version(spdm_context) >= SPDM_MESSAGE_VERSION_12) &&
270 [ + - ]: 6 : ((spdm_context->connection_info.algorithm.other_params_support &
271 : : SPDM_ALGORITHMS_OPAQUE_DATA_FORMAT_MASK) == SPDM_ALGORITHMS_OPAQUE_DATA_FORMAT_NONE)) {
272 : 6 : opaque_data_size = 0;
273 : : } else {
274 : 24 : result = libspdm_challenge_opaque_data(
275 : : spdm_context,
276 : 24 : spdm_context->connection_info.version,
277 : : slot_id,
278 : : request_context_size,
279 : : request_context,
280 : : opaque_data, &opaque_data_size);
281 [ - + ]: 24 : if (!result) {
282 : 0 : return libspdm_generate_error_response(
283 : : spdm_context, SPDM_ERROR_CODE_UNSPECIFIED,
284 : : 0, response_size, response);
285 : : }
286 : : }
287 : :
288 : : /*write opaque_data_size*/
289 : 30 : libspdm_write_uint16 (ptr, (uint16_t)opaque_data_size);
290 : 30 : ptr += sizeof(uint16_t);
291 : :
292 : : /*the opaque_data is stored by libspdm_challenge_opaque_data*/
293 : 30 : ptr += opaque_data_size;
294 : :
295 [ + + ]: 30 : if (spdm_request->header.spdm_version >= SPDM_MESSAGE_VERSION_13) {
296 : 2 : libspdm_copy_mem(ptr, SPDM_REQ_CONTEXT_SIZE,
297 : 2 : spdm_request + 1, SPDM_REQ_CONTEXT_SIZE);
298 : 2 : ptr += SPDM_REQ_CONTEXT_SIZE;
299 : : }
300 : :
301 : : /*get actual response size*/
302 : 30 : spdm_response_size =
303 : : sizeof(spdm_challenge_auth_response_t) + hash_size +
304 : 30 : SPDM_NONCE_SIZE + measurement_summary_hash_size +
305 : 30 : sizeof(uint16_t) + opaque_data_size + signature_size;
306 [ + + ]: 30 : if (spdm_request->header.spdm_version >= SPDM_MESSAGE_VERSION_13) {
307 : 2 : spdm_response_size += SPDM_REQ_CONTEXT_SIZE;
308 : : }
309 : :
310 : 30 : LIBSPDM_ASSERT(*response_size >= spdm_response_size);
311 : :
312 : 30 : *response_size = spdm_response_size;
313 : :
314 : : /* Calc Sign*/
315 : :
316 : 30 : status = libspdm_append_message_c(spdm_context, spdm_request, spdm_request_size);
317 [ - + ]: 30 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
318 : 0 : return libspdm_generate_error_response(spdm_context,
319 : : SPDM_ERROR_CODE_UNSPECIFIED, 0,
320 : : response_size, response);
321 : : }
322 : :
323 : 30 : status = libspdm_append_message_c(spdm_context, spdm_response,
324 : 30 : (size_t)ptr - (size_t)spdm_response);
325 [ - + ]: 30 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
326 : 0 : libspdm_reset_message_c(spdm_context);
327 : 0 : return libspdm_generate_error_response(spdm_context,
328 : : SPDM_ERROR_CODE_UNSPECIFIED, 0,
329 : : response_size, response);
330 : : }
331 : 30 : result = libspdm_generate_challenge_auth_signature(spdm_context, false, slot_id, ptr);
332 [ - + ]: 30 : if (!result) {
333 : 0 : libspdm_reset_message_c(spdm_context);
334 : 0 : return libspdm_generate_error_response(
335 : : spdm_context, SPDM_ERROR_CODE_UNSPECIFIED,
336 : : 0, response_size, response);
337 : : }
338 : 30 : ptr += signature_size;
339 : :
340 [ + + ]: 30 : if ((auth_attribute & SPDM_CHALLENGE_AUTH_RESPONSE_ATTRIBUTE_BASIC_MUT_AUTH_REQ) == 0) {
341 : 28 : libspdm_set_connection_state(spdm_context,
342 : : LIBSPDM_CONNECTION_STATE_AUTHENTICATED);
343 : : }
344 : :
345 : 30 : libspdm_reset_message_b(spdm_context);
346 : 30 : libspdm_reset_message_c(spdm_context);
347 : :
348 : 30 : return LIBSPDM_STATUS_SUCCESS;
349 : : }
350 : :
351 : : #endif /* LIBSPDM_ENABLE_CAPABILITY_CHAL_CAP */
|