Skip to content

Commit

Permalink
cli_helpers: reduce panics
Browse files Browse the repository at this point in the history
Signed-off-by: Wilfred Mallawa <wilfred.mallawa@wdc.com>
  • Loading branch information
twilfredo authored and alistair23 committed Nov 15, 2024
1 parent 9a141e9 commit f2a206c
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 44 deletions.
98 changes: 61 additions & 37 deletions src/cli_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>) -> Option<u8> {
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
Expand Down Expand Up @@ -100,19 +89,33 @@ pub fn parse_aead_cipher_suite(aead_cipher_suites: Option<String>) -> Result<u16
match suite.as_str() {
"AES_128_GCM" => {
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);
Expand Down Expand Up @@ -160,32 +163,53 @@ pub fn parse_dhe_named_groups(dhe_groups: Option<String>) -> Result<u16, ()> {
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);
Expand Down
13 changes: 6 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit f2a206c

Please sign in to comment.