@@ -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 );
0 commit comments