Skip to content

Commit dfec15d

Browse files
steven-bellockjyao1
authored andcommitted
Replace mut_auth_requested with HAL call
Signed-off-by: Steven Bellock <[email protected]>
1 parent f0208e0 commit dfec15d

File tree

19 files changed

+232
-161
lines changed

19 files changed

+232
-161
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* Copyright Notice:
3+
* Copyright 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+
#ifndef RESPONDER_KEYEXLIB_H
8+
#define RESPONDER_KEYEXLIB_H
9+
10+
#include "hal/base.h"
11+
#include "internal/libspdm_lib_config.h"
12+
#include "industry_standard/spdm.h"
13+
14+
#if (LIBSPDM_ENABLE_CAPABILITY_KEY_EX_CAP) && (LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP)
15+
/**
16+
* Queries whether session-based mutual authentication should be initiated or not.
17+
*
18+
* @param spdm_context A pointer to the SPDM context.
19+
* @param session_id Secure session identifier.
20+
* @param spdm_version Indicates the negotiated version.
21+
* @param slot_id The certificate slot within the KEY_EXCHANGE request.
22+
* @param req_slot_id The certificate slot within the KEY_EXCHANGE_RSP response.
23+
* This value can be non-zero only when
24+
* SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED is returned.
25+
*
26+
* @param session_policy Policy for the session. A bitmask whose values are
27+
* SPDM_KEY_EXCHANGE_REQUEST_SESSION_POLICY_*.
28+
29+
* @param opaque_data_length Size, in bytes, of opaque_data.
30+
* @param opaque_data The KEY_EXCHANGE OpaqueData field. Its value is NULL if value of
31+
* opaque_data_length is 0.
32+
* @param mandatory_mut_auth If true, then mutual authentication must be completed, and libspdm
33+
* will return an error to the Requester if the Requester does not
34+
* support mutual authentication. If false, and Requester does not
35+
* support mutual authentication, then the session will still be
36+
* established.
37+
*
38+
* @retval 0 Do not initiate the session-based mutual authentication flow.
39+
* @retval SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED
40+
* @retval SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED_WITH_ENCAP_REQUEST
41+
* @retval SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED_WITH_GET_DIGESTS
42+
*/
43+
extern uint8_t libspdm_key_exchange_start_mut_auth(
44+
void *spdm_context,
45+
uint32_t session_id,
46+
spdm_version_number_t spdm_version,
47+
uint8_t slot_id,
48+
uint8_t *req_slot_id,
49+
uint8_t session_policy,
50+
size_t opaque_data_length,
51+
const void *opaque_data,
52+
bool *mandatory_mut_auth
53+
);
54+
#endif /* (LIBSPDM_ENABLE_CAPABILITY_KEY_EX_CAP) && (LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP) */
55+
56+
#endif /* RESPONDER_KEYEXLIB_H */

include/internal/libspdm_common_lib.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "hal/library/responder/asymsignlib.h"
1919
#include "hal/library/responder/csrlib.h"
2020
#include "hal/library/responder/measlib.h"
21+
#include "hal/library/responder/keyexlib.h"
2122
#include "hal/library/responder/key_pair_info.h"
2223
#include "hal/library/responder/psklib.h"
2324
#include "hal/library/responder/setcertlib.h"
@@ -121,8 +122,6 @@ typedef struct {
121122

122123
/* Responder policy*/
123124
bool basic_mut_auth_requested;
124-
uint8_t mut_auth_requested;
125-
bool mandatory_mut_auth;
126125
uint8_t heartbeat_period;
127126

128127
/*The device role*/

include/library/spdm_common_lib.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ typedef enum {
7878
LIBSPDM_DATA_LOCAL_KEY_USAGE_BIT_MASK,
7979

8080
LIBSPDM_DATA_MUT_AUTH_REQUESTED,
81-
LIBSPDM_DATA_MANDATORY_MUT_AUTH,
8281
LIBSPDM_DATA_HEARTBEAT_PERIOD,
8382

8483
/* Negotiated result */

library/spdm_common_lib/libspdm_com_context_data.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -687,23 +687,13 @@ libspdm_return_t libspdm_set_data(void *spdm_context, libspdm_data_type_t data_t
687687
SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED_WITH_GET_DIGESTS))) {
688688
return LIBSPDM_STATUS_INVALID_PARAMETER;
689689
}
690-
context->local_context.mut_auth_requested = mut_auth_requested;
691690
context->encap_context.request_id = 0;
692691
slot_id = parameter->additional_data[0];
693692
if ((slot_id >= SPDM_MAX_SLOT_COUNT) && (slot_id != 0xFF)) {
694693
return LIBSPDM_STATUS_INVALID_PARAMETER;
695694
}
696695
context->encap_context.req_slot_id = slot_id;
697696
break;
698-
case LIBSPDM_DATA_MANDATORY_MUT_AUTH:
699-
if (data_size != sizeof(bool)) {
700-
return LIBSPDM_STATUS_INVALID_PARAMETER;
701-
}
702-
if (parameter->location != LIBSPDM_DATA_LOCATION_LOCAL) {
703-
return LIBSPDM_STATUS_INVALID_PARAMETER;
704-
}
705-
context->local_context.mandatory_mut_auth = *(const bool *)data;
706-
break;
707697
case LIBSPDM_DATA_HEARTBEAT_PERIOD:
708698
if (data_size != sizeof(uint8_t)) {
709699
return LIBSPDM_STATUS_INVALID_PARAMETER;

library/spdm_responder_lib/libspdm_rsp_key_exchange.c

Lines changed: 64 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,8 @@ libspdm_return_t libspdm_get_response_key_exchange(libspdm_context_t *spdm_conte
193193
uint32_t measurement_summary_hash_size;
194194
uint32_t signature_size;
195195
uint32_t hmac_size;
196-
const uint8_t *cptr;
197196
uint8_t *ptr;
197+
const uint8_t *req_opaque_data;
198198
uint16_t opaque_data_length;
199199
bool result;
200200
uint8_t slot_id;
@@ -209,6 +209,11 @@ libspdm_return_t libspdm_get_response_key_exchange(libspdm_context_t *spdm_conte
209209
size_t opaque_key_exchange_rsp_size;
210210
uint8_t th1_hash_data[LIBSPDM_MAX_HASH_SIZE];
211211
spdm_version_number_t secured_message_version;
212+
#if LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP
213+
uint8_t req_slot_id;
214+
uint8_t mut_auth_requested;
215+
bool mandatory_mut_auth;
216+
#endif /* LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP */
212217

213218
spdm_request = request;
214219

@@ -384,21 +389,24 @@ libspdm_return_t libspdm_get_response_key_exchange(libspdm_context_t *spdm_conte
384389
sizeof(uint16_t) + opaque_data_length;
385390

386391
if (opaque_data_length != 0) {
387-
cptr = (const uint8_t *)request + sizeof(spdm_key_exchange_request_t) +
392+
req_opaque_data = (const uint8_t *)request + sizeof(spdm_key_exchange_request_t) +
388393
req_key_exchange_size + sizeof(uint16_t);
389-
result = libspdm_process_general_opaque_data_check(spdm_context, opaque_data_length, cptr);
394+
result = libspdm_process_general_opaque_data_check(spdm_context, opaque_data_length,
395+
req_opaque_data);
390396
if (!result) {
391397
return libspdm_generate_error_response(spdm_context,
392398
SPDM_ERROR_CODE_INVALID_REQUEST, 0,
393399
response_size, response);
394400
}
395401
status = libspdm_process_opaque_data_supported_version_data(
396-
spdm_context, opaque_data_length, cptr, &secured_message_version);
402+
spdm_context, opaque_data_length, req_opaque_data, &secured_message_version);
397403
if (LIBSPDM_STATUS_IS_ERROR(status)) {
398404
return libspdm_generate_error_response(spdm_context,
399405
SPDM_ERROR_CODE_INVALID_REQUEST, 0,
400406
response_size, response);
401407
}
408+
} else {
409+
req_opaque_data = NULL;
402410
}
403411

404412
opaque_key_exchange_rsp_size =
@@ -462,48 +470,63 @@ libspdm_return_t libspdm_get_response_key_exchange(libspdm_context_t *spdm_conte
462470

463471
spdm_response->rsp_session_id = rsp_session_id;
464472
spdm_response->mut_auth_requested = 0;
473+
spdm_response->req_slot_id_param = 0;
465474

475+
#if LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP
466476
if (libspdm_is_capabilities_flag_supported(
467-
spdm_context, false,
468-
SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MUT_AUTH_CAP,
469-
SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MUT_AUTH_CAP)) {
470-
spdm_response->mut_auth_requested =
471-
spdm_context->local_context.mut_auth_requested;
472-
} else if (spdm_context->local_context.mandatory_mut_auth) {
473-
LIBSPDM_ASSERT(spdm_context->local_context.capability.flags &
474-
SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MUT_AUTH_CAP);
475-
if (libspdm_get_connection_version(spdm_context) >= SPDM_MESSAGE_VERSION_13) {
476-
libspdm_free_session_id(spdm_context, session_id);
477-
return libspdm_generate_error_response(spdm_context,
478-
SPDM_ERROR_CODE_INVALID_POLICY, 0,
479-
response_size, response);
480-
} else {
481-
libspdm_free_session_id(spdm_context, session_id);
482-
return libspdm_generate_error_response(spdm_context,
483-
SPDM_ERROR_CODE_UNSPECIFIED, 0,
484-
response_size, response);
485-
}
486-
}
477+
spdm_context, false, 0, SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MUT_AUTH_CAP)) {
478+
req_slot_id = 0;
479+
480+
mut_auth_requested =
481+
libspdm_key_exchange_start_mut_auth(spdm_context,
482+
session_id,
483+
spdm_context->connection_info.version,
484+
slot_id,
485+
&req_slot_id,
486+
spdm_request->session_policy,
487+
opaque_data_length,
488+
req_opaque_data,
489+
&mandatory_mut_auth);
490+
if (mut_auth_requested != 0) {
491+
const bool req_mut_auth_cap = libspdm_is_capabilities_flag_supported(
492+
spdm_context, false, SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MUT_AUTH_CAP, 0);
493+
const bool req_encap_cap = libspdm_is_capabilities_flag_supported(
494+
spdm_context, false, SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCAP_CAP, 0);
495+
const bool need_encap =
496+
(mut_auth_requested ==
497+
SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED_WITH_ENCAP_REQUEST) ||
498+
(mut_auth_requested ==
499+
SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED_WITH_GET_DIGESTS);
500+
501+
/* If Integrator requires mutual authentication but Requester does not support mutual
502+
* authentication, or Integrator requires the encapsulated mutual authentication flow
503+
* and Requester does not support encapsulated messages, then return an error to
504+
* Requester. */
505+
if (mandatory_mut_auth && (!req_mut_auth_cap || (need_encap && !req_encap_cap))) {
506+
if (libspdm_get_connection_version(spdm_context) >= SPDM_MESSAGE_VERSION_13) {
507+
libspdm_free_session_id(spdm_context, session_id);
508+
return libspdm_generate_error_response(spdm_context,
509+
SPDM_ERROR_CODE_INVALID_POLICY, 0,
510+
response_size, response);
511+
} else {
512+
libspdm_free_session_id(spdm_context, session_id);
513+
return libspdm_generate_error_response(spdm_context,
514+
SPDM_ERROR_CODE_UNSPECIFIED, 0,
515+
response_size, response);
516+
}
517+
}
487518

488-
if (spdm_response->mut_auth_requested != 0) {
489-
#if LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP
490-
spdm_context->connection_info.peer_used_cert_chain_slot_id =
491-
spdm_context->encap_context.req_slot_id;
492-
libspdm_init_mut_auth_encap_state(spdm_context, spdm_response->mut_auth_requested);
493-
if (spdm_response->mut_auth_requested == SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED) {
494-
/* no need to libspdm_init_mut_auth_encap_state() because of no ENCAP message */
495-
spdm_response->req_slot_id_param = spdm_context->encap_context.req_slot_id & 0xF;
496-
} else {
497-
/* req_slot_id is always 0 if ENCAP message is needed */
498-
spdm_response->req_slot_id_param = 0;
519+
if (!need_encap) {
520+
spdm_response->mut_auth_requested = mut_auth_requested;
521+
spdm_response->req_slot_id_param = req_slot_id;
522+
} else if (need_encap && req_encap_cap) {
523+
spdm_response->mut_auth_requested = mut_auth_requested;
524+
spdm_context->connection_info.peer_used_cert_chain_slot_id = req_slot_id;
525+
libspdm_init_mut_auth_encap_state(spdm_context, mut_auth_requested);
526+
}
499527
}
500-
#else
501-
spdm_response->mut_auth_requested = 0;
502-
spdm_response->req_slot_id_param = 0;
503-
#endif /* LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP */
504-
} else {
505-
spdm_response->req_slot_id_param = 0;
506528
}
529+
#endif /* LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP */
507530

508531
if (!libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, spdm_response->random_data)) {
509532
libspdm_free_session_id(spdm_context, session_id);

os_stub/spdm_device_secret_lib_null/lib.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,23 @@ bool libspdm_measurement_extension_log_collection(
117117
}
118118
#endif /* LIBSPDM_ENABLE_CAPABILITY_MEL_CAP */
119119

120+
#if (LIBSPDM_ENABLE_CAPABILITY_KEY_EX_CAP) && (LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP)
121+
extern uint8_t libspdm_key_exchange_start_mut_auth(
122+
void *spdm_context,
123+
uint32_t session_id,
124+
spdm_version_number_t spdm_version,
125+
uint8_t slot_id,
126+
uint8_t *req_slot_id,
127+
uint8_t session_policy,
128+
size_t opaque_data_length,
129+
const void *opaque_data,
130+
bool *mandatory_mut_auth
131+
)
132+
{
133+
return false;
134+
}
135+
#endif /* (LIBSPDM_ENABLE_CAPABILITY_KEY_EX_CAP) && (LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP) */
136+
120137
#if (LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP) || (LIBSPDM_ENABLE_CAPABILITY_ENDPOINT_INFO_CAP)
121138
bool libspdm_requester_data_sign(
122139
void *spdm_context,

os_stub/spdm_device_secret_lib_sample/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ target_sources(spdm_device_secret_lib_sample
1616
csr.c
1717
endpointinfo.c
1818
event.c
19+
key_ex.c
1920
key_pair.c
2021
meas.c
2122
psk.c
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Copyright Notice:
3+
* Copyright 2024-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 "hal/base.h"
8+
#include "internal/libspdm_common_lib.h"
9+
10+
uint8_t g_key_exchange_start_mut_auth = 0;
11+
bool g_mandatory_mut_auth = false;
12+
13+
#if (LIBSPDM_ENABLE_CAPABILITY_KEY_EX_CAP) && (LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP)
14+
extern uint8_t libspdm_key_exchange_start_mut_auth(
15+
void *spdm_context,
16+
uint32_t session_id,
17+
spdm_version_number_t spdm_version,
18+
uint8_t slot_id,
19+
uint8_t *req_slot_id,
20+
uint8_t session_policy,
21+
size_t opaque_data_length,
22+
const void *opaque_data,
23+
bool *mandatory_mut_auth
24+
)
25+
{
26+
*req_slot_id = 0;
27+
*mandatory_mut_auth = g_mandatory_mut_auth;
28+
29+
return g_key_exchange_start_mut_auth;
30+
}
31+
#endif /* (LIBSPDM_ENABLE_CAPABILITY_KEY_EX_CAP) && (LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP) */

unit_test/fuzzing/test_responder/test_spdm_responder_end_session/end_session.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ void libspdm_test_responder_end_session(void **State)
6464
data_size;
6565

6666
libspdm_reset_message_a(spdm_context);
67-
spdm_context->local_context.mut_auth_requested = 0;
6867

6968
session_id = 0xFFFFFFFF;
7069
spdm_context->latest_session_id = session_id;

unit_test/fuzzing/test_responder/test_spdm_responder_finish_rsp/finish_rsp.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#if LIBSPDM_ENABLE_CAPABILITY_KEY_EX_CAP
1313

1414
static libspdm_th_managed_buffer_t th_curr;
15+
extern uint8_t g_key_exchange_start_mut_auth;
1516

1617
size_t libspdm_get_max_buffer_size(void)
1718
{
@@ -98,7 +99,6 @@ void libspdm_test_responder_finish_case1(void **State)
9899
spdm_context->connection_info.local_used_cert_chain_buffer_size = data_size1;
99100

100101
libspdm_reset_message_a(spdm_context);
101-
spdm_context->local_context.mut_auth_requested = 0;
102102

103103
session_id = 0xFFFFFFFF;
104104
spdm_context->latest_session_id = session_id;
@@ -319,7 +319,6 @@ void libspdm_test_responder_finish_case7(void **State)
319319
spdm_context->connection_info.local_used_cert_chain_buffer_size = data_size1;
320320

321321
libspdm_reset_message_a(spdm_context);
322-
spdm_context->local_context.mut_auth_requested = 0;
323322

324323
session_id = 0xFFFFFFFF;
325324
spdm_context->latest_session_id = session_id;
@@ -402,7 +401,7 @@ void libspdm_test_responder_finish_case8(void **State)
402401
spdm_context->connection_info.local_used_cert_chain_buffer_size = data_size1;
403402

404403
libspdm_reset_message_a(spdm_context);
405-
spdm_context->local_context.mut_auth_requested = 1;
404+
g_key_exchange_start_mut_auth = SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED;
406405
libspdm_read_requester_public_certificate_chain(m_libspdm_use_hash_algo,
407406
m_libspdm_use_req_asym_algo,
408407
&data2,
@@ -504,6 +503,7 @@ void libspdm_test_responder_finish_case8(void **State)
504503
spdm_context->connection_info.algorithm.req_base_asym_alg,
505504
spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
506505
#endif
506+
g_key_exchange_start_mut_auth = 0;
507507
}
508508

509509
void libspdm_run_test_harness(void *test_buffer, size_t test_buffer_size)

0 commit comments

Comments
 (0)