diff --git a/card-functionality/src/tests.rs b/card-functionality/src/tests.rs index c9a5a2d..df16fab 100644 --- a/card-functionality/src/tests.rs +++ b/card-functionality/src/tests.rs @@ -2,6 +2,7 @@ // SPDX-License-Identifier: MIT OR Apache-2.0 use anyhow::Result; +use std::convert::TryFrom; use std::str::FromStr; use std::string::FromUtf8Error; use thiserror; @@ -229,7 +230,7 @@ pub fn test_keygen( // Generate all three subkeys on card let algo = param[0]; - let alg = AlgoSimple::from(algo); + let alg = AlgoSimple::try_from(algo)?; println!(" Generate subkey for Signing"); let (pkm, ts) = diff --git a/openpgp-card/src/algorithm.rs b/openpgp-card/src/algorithm.rs index e2f0c70..c50ef7c 100644 --- a/openpgp-card/src/algorithm.rs +++ b/openpgp-card/src/algorithm.rs @@ -36,11 +36,13 @@ pub enum AlgoSimple { Curve25519, } -impl From<&str> for AlgoSimple { - fn from(algo: &str) -> Self { +impl TryFrom<&str> for AlgoSimple { + type Error = anyhow::Error; + + fn try_from(algo: &str) -> Result { use AlgoSimple::*; - match algo { + Ok(match algo { "RSA1k/17" => RSA1k(17), "RSA1k/32" => RSA1k(32), "RSA2k/17" => RSA2k(17), @@ -53,8 +55,8 @@ impl From<&str> for AlgoSimple { "NIST384" => NIST384, "NIST521" => NIST521, "Curve25519" => Curve25519, - _ => panic!("unexpected algo {}", algo), - } + _ => return Err(anyhow!("unexpected algo {}", algo)), + }) } } diff --git a/openpgp-card/src/crypto_data.rs b/openpgp-card/src/crypto_data.rs index 95f78ac..f1efce7 100644 --- a/openpgp-card/src/crypto_data.rs +++ b/openpgp-card/src/crypto_data.rs @@ -22,6 +22,8 @@ pub enum Hash<'a> { } impl Hash<'_> { + /// This fn is currently only used in the context of creating a + /// digestinfo for SHA*. Other OIDs are not implemented. pub(crate) fn oid(&self) -> Option<&'static [u8]> { match self { Self::SHA256(_) => { @@ -33,8 +35,8 @@ impl Hash<'_> { Self::SHA512(_) => { Some(&[0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03]) } - Self::EdDSA(_) => panic!("This should not be called"), - Self::ECDSA(_) => panic!("This should not be called"), + Self::EdDSA(_) => panic!("OIDs for EdDSA are unimplemented"), + Self::ECDSA(_) => panic!("OIDs for ECDSA are unimplemented"), } } diff --git a/tools/src/bin/opgpcard/main.rs b/tools/src/bin/opgpcard/main.rs index c1f4082..ed8f3b4 100644 --- a/tools/src/bin/opgpcard/main.rs +++ b/tools/src/bin/opgpcard/main.rs @@ -464,7 +464,7 @@ fn generate_keys( Some("nistp384") => vec![AlgoSimple::NIST384], Some("nistp521") => vec![AlgoSimple::NIST521], Some("25519") => vec![AlgoSimple::Curve25519], - _ => unimplemented!("unexpected algorithm"), + _ => return Err(anyhow!("Unexpected algorithm")), }; log::info!(