Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

general: reduce potential for user induced panics #122

Merged
merged 2 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
45 changes: 28 additions & 17 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -838,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
Loading