From 1724a099be7e9b6cdd9d300ce3dbc39751954f5c Mon Sep 17 00:00:00 2001 From: Wilfred Mallawa Date: Tue, 12 Nov 2024 10:20:56 +1000 Subject: [PATCH 1/2] main.rs: reduce panic points Signed-off-by: Wilfred Mallawa --- src/main.rs | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index 13b9077..1684475 100644 --- a/src/main.rs +++ b/src/main.rs @@ -342,7 +342,8 @@ impl std::str::FromStr for RequestCode { // options, or does not require any arguments. let (request_code, subargs) = if s.find('[').is_some() && s.find(']').is_some() { // This request has sub-arguments specified - let (req_code, subarg) = s.split_at(s.find('[').unwrap()); + let (req_code, subarg) = + s.split_at(s.find('[').ok_or(format!("Invalid argument: {}", s))?); let subarg_len = subarg.len(); (req_code, Some(&subarg[1..subarg_len - 1])) } else { @@ -737,12 +738,11 @@ async fn main() -> Result<(), ()> { request::setup_capabilities( cntx_ptr, slot_id, - cli_helpers::parse_asym_algos(asym_algos).unwrap(), - cli_helpers::parse_hash_algos(hash_algos).unwrap(), - cli_helpers::parse_dhe_named_groups(dhe_named_groups).unwrap(), - cli_helpers::parse_aead_cipher_suite(aead_cipher_suites).unwrap(), - ) - .unwrap(); + cli_helpers::parse_asym_algos(asym_algos)?, + cli_helpers::parse_hash_algos(hash_algos)?, + cli_helpers::parse_dhe_named_groups(dhe_named_groups)?, + cli_helpers::parse_aead_cipher_suite(aead_cipher_suites)?, + )?; let mut session_info = if cli.no_session { spdm::SpdmSessionInfo { @@ -756,13 +756,25 @@ async fn main() -> Result<(), ()> { } } else { unsafe { - spdm::initialise_connection(cntx_ptr, slot_id).unwrap(); - spdm::start_session(cntx_ptr, slot_id, use_psk_exchange).unwrap() + spdm::initialise_connection(cntx_ptr, slot_id).map_err(|e| { + error!("Failed to initialise an SPDM connection: 0x{:x}", e); + () + })?; + spdm::start_session(cntx_ptr, slot_id, use_psk_exchange).map_err(|e| { + error!("Failed to start session: 0x{:x}", e); + () + })? } }; + // Print out the negotiated algorithms if !cli.no_session { - unsafe { spdm::get_negotiated_algos(cntx_ptr, slot_id).unwrap() } + unsafe { + spdm::get_negotiated_algos(cntx_ptr, slot_id).map_err(|e| { + error!("Failed to negotiate algorithms: 0x{:x}", e); + () + })? + } }; // Process one or more requests specified From 9a280558db918be513dc1f0bff6f143c7a13e119 Mon Sep 17 00:00:00 2001 From: Wilfred Mallawa Date: Tue, 12 Nov 2024 11:12:57 +1000 Subject: [PATCH 2/2] cli_helpers: reduce panics Signed-off-by: Wilfred Mallawa --- src/cli_helpers.rs | 98 +++++++++++++++++++++++++++++----------------- src/main.rs | 13 +++--- 2 files changed, 67 insertions(+), 44 deletions(-) diff --git a/src/cli_helpers.rs b/src/cli_helpers.rs index 468ab03..2599539 100644 --- a/src/cli_helpers.rs +++ b/src/cli_helpers.rs @@ -50,24 +50,13 @@ pub fn parse_pcie_identifiers(vid: String, dev_id: String) -> Result<(u16, u16), /// /// The corresponding libspdm value for the version, None if not found. pub fn parse_spdm_responder_version(spdm_ver: Option) -> Option { - if let Some(ver) = spdm_ver { - match ver.as_str() { - "1.0" => { - return Some(u8::try_from(libspdm::libspdm_rs::SPDM_MESSAGE_VERSION_10).unwrap()) - } - "1.1" => { - return Some(u8::try_from(libspdm::libspdm_rs::SPDM_MESSAGE_VERSION_11).unwrap()) - } - "1.2" => { - return Some(u8::try_from(libspdm::libspdm_rs::SPDM_MESSAGE_VERSION_12).unwrap()) - } - "1.3" => { - return Some(u8::try_from(libspdm::libspdm_rs::SPDM_MESSAGE_VERSION_13).unwrap()) - } - _ => return None, - } - } - None + spdm_ver.and_then(|ver| match ver.as_str() { + "1.0" => u8::try_from(libspdm::libspdm_rs::SPDM_MESSAGE_VERSION_10).ok(), + "1.1" => u8::try_from(libspdm::libspdm_rs::SPDM_MESSAGE_VERSION_11).ok(), + "1.2" => u8::try_from(libspdm::libspdm_rs::SPDM_MESSAGE_VERSION_12).ok(), + "1.3" => u8::try_from(libspdm::libspdm_rs::SPDM_MESSAGE_VERSION_13).ok(), + _ => None, + }) } /// # Summary @@ -100,19 +89,33 @@ pub fn parse_aead_cipher_suite(aead_cipher_suites: Option) -> Result { libspdm_aead_cipher_suites |= - u16::try_from(SPDM_ALGORITHMS_AEAD_CIPHER_SUITE_AES_128_GCM).unwrap(); + u16::try_from(SPDM_ALGORITHMS_AEAD_CIPHER_SUITE_AES_128_GCM).map_err(|e| { + error!("AEAD type conversion error: {e}"); + () + })?; } "AES_256_GCM" => { libspdm_aead_cipher_suites |= - u16::try_from(SPDM_ALGORITHMS_AEAD_CIPHER_SUITE_AES_256_GCM).unwrap(); + u16::try_from(SPDM_ALGORITHMS_AEAD_CIPHER_SUITE_AES_256_GCM).map_err(|e| { + error!("AEAD type conversion error: {e}"); + () + })?; } "CHACHA20_POLY1305" => { - libspdm_aead_cipher_suites |= - u16::try_from(SPDM_ALGORITHMS_AEAD_CIPHER_SUITE_CHACHA20_POLY1305).unwrap(); + libspdm_aead_cipher_suites |= u16::try_from( + SPDM_ALGORITHMS_AEAD_CIPHER_SUITE_CHACHA20_POLY1305, + ) + .map_err(|e| { + error!("AEAD type conversion error: {e}"); + () + })?; } "AEAD_SM4_GCM" => { libspdm_aead_cipher_suites |= - u16::try_from(SPDM_ALGORITHMS_AEAD_CIPHER_SUITE_AEAD_SM4_GCM).unwrap(); + u16::try_from(SPDM_ALGORITHMS_AEAD_CIPHER_SUITE_AEAD_SM4_GCM).map_err(|e| { + error!("AEAD type conversion error: {e}"); + () + })?; } _ => { error!("Unsupported AEAD Cipher Suite ({})", suite); @@ -160,32 +163,53 @@ pub fn parse_dhe_named_groups(dhe_groups: Option) -> Result { for group in groups { match group.as_str() { "FFDHE_2048" => { - libspdm_dhe_groups |= - u16::try_from(SPDM_ALGORITHMS_DHE_NAMED_GROUP_FFDHE_2048).unwrap(); + libspdm_dhe_groups |= u16::try_from(SPDM_ALGORITHMS_DHE_NAMED_GROUP_FFDHE_2048) + .map_err(|e| { + error!("DHE type conversion error: {e}"); + () + })?; } "FFDHE_3072" => { - libspdm_dhe_groups |= - u16::try_from(SPDM_ALGORITHMS_DHE_NAMED_GROUP_FFDHE_3072).unwrap(); + libspdm_dhe_groups |= u16::try_from(SPDM_ALGORITHMS_DHE_NAMED_GROUP_FFDHE_3072) + .map_err(|e| { + error!("DHE type conversion error: {e}"); + () + })?; } "FFDHE_4096" => { - libspdm_dhe_groups |= - u16::try_from(SPDM_ALGORITHMS_DHE_NAMED_GROUP_FFDHE_4096).unwrap(); + libspdm_dhe_groups |= u16::try_from(SPDM_ALGORITHMS_DHE_NAMED_GROUP_FFDHE_4096) + .map_err(|e| { + error!("DHE type conversion error: {e}"); + () + })?; } "SECP_256_R1" => { - libspdm_dhe_groups |= - u16::try_from(SPDM_ALGORITHMS_DHE_NAMED_GROUP_SECP_256_R1).unwrap(); + libspdm_dhe_groups |= u16::try_from(SPDM_ALGORITHMS_DHE_NAMED_GROUP_SECP_256_R1) + .map_err(|e| { + error!("DHE type conversion error: {e}"); + () + })?; } "SECP_384_R1" => { - libspdm_dhe_groups |= - u16::try_from(SPDM_ALGORITHMS_DHE_NAMED_GROUP_SECP_384_R1).unwrap(); + libspdm_dhe_groups |= u16::try_from(SPDM_ALGORITHMS_DHE_NAMED_GROUP_SECP_384_R1) + .map_err(|e| { + error!("DHE type conversion error: {e}"); + () + })?; } "SECP_521_R1" => { - libspdm_dhe_groups |= - u16::try_from(SPDM_ALGORITHMS_DHE_NAMED_GROUP_SECP_521_R1).unwrap(); + libspdm_dhe_groups |= u16::try_from(SPDM_ALGORITHMS_DHE_NAMED_GROUP_SECP_521_R1) + .map_err(|e| { + error!("DHE type conversion error: {e}"); + () + })?; } "SM2_P256" => { - libspdm_dhe_groups |= - u16::try_from(SPDM_ALGORITHMS_DHE_NAMED_GROUP_SM2_P256).unwrap(); + libspdm_dhe_groups |= u16::try_from(SPDM_ALGORITHMS_DHE_NAMED_GROUP_SM2_P256) + .map_err(|e| { + error!("DHE type conversion error: {e}"); + () + })?; } _ => { error!("Unsupported DHE group ({})", group); diff --git a/src/main.rs b/src/main.rs index 1684475..64763b6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -850,13 +850,12 @@ async fn main() -> Result<(), ()> { } } // Check if version was specified - let ver = cli_helpers::parse_spdm_responder_version(spdm_ver); - if ver.is_none() { - // spdm_ver has a default value set, if None was returned, it means - // the user argument was invalid. - error!("Unsupported libspdm data spdm version"); - return Err(()); - } + let ver = Some( + cli_helpers::parse_spdm_responder_version(spdm_ver).ok_or_else(|| { + error!("Unsupported/Invalid SPDM version"); + () + })?, + ); responder::setup_capabilities( cntx_ptr, 0,