Skip to content

Commit

Permalink
Error::UnsupportedKeyType holds a String
Browse files Browse the repository at this point in the history
  • Loading branch information
rukai authored and Eugeny committed Aug 2, 2023
1 parent b3576aa commit 531fe30
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
5 changes: 4 additions & 1 deletion russh-keys/src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ pub fn decode_secret_key(secret: &str, password: Option<&str>) -> Result<key::Ke
} else if l == "-----BEGIN RSA PRIVATE KEY-----" {
#[cfg(not(feature = "openssl"))]
{
return Err(Error::UnsupportedKeyType("rsa".as_bytes().to_vec()));
return Err(Error::UnsupportedKeyType {
key_type_string: "rsa".to_owned(),
key_type_raw: "rsa".as_bytes().to_vec(),
});
}
#[cfg(feature = "openssl")]
{
Expand Down
10 changes: 7 additions & 3 deletions russh-keys/src/format/openssh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn decode_openssh(secret: &[u8], password: Option<&str>) -> Result<key::KeyP
let _check1 = position.read_u32()?;
#[allow(clippy::never_loop)]
for _ in 0..nkeys {
// TODO check: never really loops beyong the first key
// TODO check: never really loops beyond the first key
let key_type = position.read_string()?;
if key_type == KEYTYPE_ED25519 {
let pubkey = position.read_string()?;
Expand Down Expand Up @@ -82,7 +82,11 @@ pub fn decode_openssh(secret: &[u8], password: Option<&str>) -> Result<key::KeyP
});
}
} else {
return Err(Error::UnsupportedKeyType(key_type.to_vec()));
return Err(Error::UnsupportedKeyType {
key_type_string: String::from_utf8(key_type.to_vec())
.unwrap_or_else(|_| format!("{key_type:?}")),
key_type_raw: key_type.to_vec(),
});
}
}
Err(Error::CouldNotReadKey)
Expand Down Expand Up @@ -122,7 +126,7 @@ fn decrypt_secret_key(
#[allow(clippy::indexing_slicing)] // output length is static
match bcrypt_pbkdf::bcrypt_pbkdf(password, salt, rounds, &mut key[..n]) {
Err(bcrypt_pbkdf::Error::InvalidParamLen) => return Err(Error::KeyIsEncrypted),
e => e.unwrap()
e => e.unwrap(),
}
}
_kdfname => {
Expand Down
9 changes: 6 additions & 3 deletions russh-keys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ use aes::cipher::block_padding::UnpadError;
use aes::cipher::inout::PadError;
use byteorder::{BigEndian, WriteBytesExt};
use data_encoding::BASE64_MIME;
use thiserror::Error;
use log::debug;
use thiserror::Error;

pub mod encoding;
pub mod key;
Expand All @@ -91,8 +91,11 @@ pub enum Error {
#[error("Could not read key")]
CouldNotReadKey,
/// The type of the key is unsupported
#[error("Unsupported key type")]
UnsupportedKeyType(Vec<u8>),
#[error("Unsupported key type {}", key_type_string)]
UnsupportedKeyType {
key_type_string: String,
key_type_raw: Vec<u8>,
},
/// The type of the key is unsupported
#[error("Invalid Ed25519 key data")]
Ed25519KeyError(#[from] ed25519_dalek::SignatureError),
Expand Down

0 comments on commit 531fe30

Please sign in to comment.