Skip to content

Commit 531fe30

Browse files
rukaiEugeny
authored andcommitted
Error::UnsupportedKeyType holds a String
1 parent b3576aa commit 531fe30

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

russh-keys/src/format/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@ pub fn decode_secret_key(secret: &str, password: Option<&str>) -> Result<key::Ke
7676
} else if l == "-----BEGIN RSA PRIVATE KEY-----" {
7777
#[cfg(not(feature = "openssl"))]
7878
{
79-
return Err(Error::UnsupportedKeyType("rsa".as_bytes().to_vec()));
79+
return Err(Error::UnsupportedKeyType {
80+
key_type_string: "rsa".to_owned(),
81+
key_type_raw: "rsa".as_bytes().to_vec(),
82+
});
8083
}
8184
#[cfg(feature = "openssl")]
8285
{

russh-keys/src/format/openssh.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub fn decode_openssh(secret: &[u8], password: Option<&str>) -> Result<key::KeyP
3333
let _check1 = position.read_u32()?;
3434
#[allow(clippy::never_loop)]
3535
for _ in 0..nkeys {
36-
// TODO check: never really loops beyong the first key
36+
// TODO check: never really loops beyond the first key
3737
let key_type = position.read_string()?;
3838
if key_type == KEYTYPE_ED25519 {
3939
let pubkey = position.read_string()?;
@@ -82,7 +82,11 @@ pub fn decode_openssh(secret: &[u8], password: Option<&str>) -> Result<key::KeyP
8282
});
8383
}
8484
} else {
85-
return Err(Error::UnsupportedKeyType(key_type.to_vec()));
85+
return Err(Error::UnsupportedKeyType {
86+
key_type_string: String::from_utf8(key_type.to_vec())
87+
.unwrap_or_else(|_| format!("{key_type:?}")),
88+
key_type_raw: key_type.to_vec(),
89+
});
8690
}
8791
}
8892
Err(Error::CouldNotReadKey)
@@ -122,7 +126,7 @@ fn decrypt_secret_key(
122126
#[allow(clippy::indexing_slicing)] // output length is static
123127
match bcrypt_pbkdf::bcrypt_pbkdf(password, salt, rounds, &mut key[..n]) {
124128
Err(bcrypt_pbkdf::Error::InvalidParamLen) => return Err(Error::KeyIsEncrypted),
125-
e => e.unwrap()
129+
e => e.unwrap(),
126130
}
127131
}
128132
_kdfname => {

russh-keys/src/lib.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ use aes::cipher::block_padding::UnpadError;
7272
use aes::cipher::inout::PadError;
7373
use byteorder::{BigEndian, WriteBytesExt};
7474
use data_encoding::BASE64_MIME;
75-
use thiserror::Error;
7675
use log::debug;
76+
use thiserror::Error;
7777

7878
pub mod encoding;
7979
pub mod key;
@@ -91,8 +91,11 @@ pub enum Error {
9191
#[error("Could not read key")]
9292
CouldNotReadKey,
9393
/// The type of the key is unsupported
94-
#[error("Unsupported key type")]
95-
UnsupportedKeyType(Vec<u8>),
94+
#[error("Unsupported key type {}", key_type_string)]
95+
UnsupportedKeyType {
96+
key_type_string: String,
97+
key_type_raw: Vec<u8>,
98+
},
9699
/// The type of the key is unsupported
97100
#[error("Invalid Ed25519 key data")]
98101
Ed25519KeyError(#[from] ed25519_dalek::SignatureError),

0 commit comments

Comments
 (0)