Minor error handling/messaging cleanups.

This commit is contained in:
Heiko Schaefer 2021-12-01 22:23:22 +01:00
parent 576110ecce
commit 9739074b63
No known key found for this signature in database
GPG key ID: 4A849A1904CCBD7D
4 changed files with 14 additions and 9 deletions

View file

@ -2,6 +2,7 @@
// SPDX-License-Identifier: MIT OR Apache-2.0 // SPDX-License-Identifier: MIT OR Apache-2.0
use anyhow::Result; use anyhow::Result;
use std::convert::TryFrom;
use std::str::FromStr; use std::str::FromStr;
use std::string::FromUtf8Error; use std::string::FromUtf8Error;
use thiserror; use thiserror;
@ -229,7 +230,7 @@ pub fn test_keygen(
// Generate all three subkeys on card // Generate all three subkeys on card
let algo = param[0]; let algo = param[0];
let alg = AlgoSimple::from(algo); let alg = AlgoSimple::try_from(algo)?;
println!(" Generate subkey for Signing"); println!(" Generate subkey for Signing");
let (pkm, ts) = let (pkm, ts) =

View file

@ -36,11 +36,13 @@ pub enum AlgoSimple {
Curve25519, Curve25519,
} }
impl From<&str> for AlgoSimple { impl TryFrom<&str> for AlgoSimple {
fn from(algo: &str) -> Self { type Error = anyhow::Error;
fn try_from(algo: &str) -> Result<Self, anyhow::Error> {
use AlgoSimple::*; use AlgoSimple::*;
match algo { Ok(match algo {
"RSA1k/17" => RSA1k(17), "RSA1k/17" => RSA1k(17),
"RSA1k/32" => RSA1k(32), "RSA1k/32" => RSA1k(32),
"RSA2k/17" => RSA2k(17), "RSA2k/17" => RSA2k(17),
@ -53,8 +55,8 @@ impl From<&str> for AlgoSimple {
"NIST384" => NIST384, "NIST384" => NIST384,
"NIST521" => NIST521, "NIST521" => NIST521,
"Curve25519" => Curve25519, "Curve25519" => Curve25519,
_ => panic!("unexpected algo {}", algo), _ => return Err(anyhow!("unexpected algo {}", algo)),
} })
} }
} }

View file

@ -22,6 +22,8 @@ pub enum Hash<'a> {
} }
impl Hash<'_> { 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]> { pub(crate) fn oid(&self) -> Option<&'static [u8]> {
match self { match self {
Self::SHA256(_) => { Self::SHA256(_) => {
@ -33,8 +35,8 @@ impl Hash<'_> {
Self::SHA512(_) => { Self::SHA512(_) => {
Some(&[0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03]) Some(&[0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03])
} }
Self::EdDSA(_) => panic!("This should not be called"), Self::EdDSA(_) => panic!("OIDs for EdDSA are unimplemented"),
Self::ECDSA(_) => panic!("This should not be called"), Self::ECDSA(_) => panic!("OIDs for ECDSA are unimplemented"),
} }
} }

View file

@ -464,7 +464,7 @@ fn generate_keys(
Some("nistp384") => vec![AlgoSimple::NIST384], Some("nistp384") => vec![AlgoSimple::NIST384],
Some("nistp521") => vec![AlgoSimple::NIST521], Some("nistp521") => vec![AlgoSimple::NIST521],
Some("25519") => vec![AlgoSimple::Curve25519], Some("25519") => vec![AlgoSimple::Curve25519],
_ => unimplemented!("unexpected algorithm"), _ => return Err(anyhow!("Unexpected algorithm")),
}; };
log::info!( log::info!(