use rand::rng;
use ssh_encoding::Decode;
use ssh_key::public::KeyData;
use ssh_key::{Algorithm, EcdsaCurve, PublicKey};
use crate::keys::Error;
pub trait PublicKeyExt {
fn decode(bytes: &[u8]) -> Result<PublicKey, Error>;
}
impl PublicKeyExt for PublicKey {
fn decode(mut bytes: &[u8]) -> Result<PublicKey, Error> {
let key = KeyData::decode(&mut bytes)?;
Ok(PublicKey::new(key, ""))
}
}
#[doc(hidden)]
pub trait Verify {
fn verify_client_auth(&self, buffer: &[u8], sig: &[u8]) -> bool;
fn verify_server_auth(&self, buffer: &[u8], sig: &[u8]) -> bool;
}
pub fn parse_public_key(mut p: &[u8]) -> Result<PublicKey, Error> {
Ok(ssh_key::public::KeyData::decode(&mut p)?.into())
}
pub fn safe_rng() -> impl rand::CryptoRng {
rng()
}
mod private_key_with_hash_alg {
use std::ops::Deref;
use std::sync::Arc;
use ssh_key::Algorithm;
use crate::helpers::AlgorithmExt;
#[derive(Clone, Debug)]
pub struct PrivateKeyWithHashAlg {
key: Arc<crate::keys::PrivateKey>,
hash_alg: Option<crate::keys::HashAlg>,
}
impl PrivateKeyWithHashAlg {
pub fn new(
key: Arc<crate::keys::PrivateKey>,
mut hash_alg: Option<crate::keys::HashAlg>,
) -> Self {
if !key.algorithm().is_rsa() {
hash_alg = None;
}
Self { key, hash_alg }
}
pub fn algorithm(&self) -> Algorithm {
self.key.algorithm().with_hash_alg(self.hash_alg)
}
pub fn hash_alg(&self) -> Option<crate::keys::HashAlg> {
self.hash_alg
}
}
impl Deref for PrivateKeyWithHashAlg {
type Target = crate::keys::PrivateKey;
fn deref(&self) -> &Self::Target {
&self.key
}
}
}
pub use private_key_with_hash_alg::PrivateKeyWithHashAlg;
pub const ALL_KEY_TYPES: &[Algorithm] = &[
Algorithm::Dsa,
Algorithm::Ecdsa {
curve: EcdsaCurve::NistP256,
},
Algorithm::Ecdsa {
curve: EcdsaCurve::NistP384,
},
Algorithm::Ecdsa {
curve: EcdsaCurve::NistP521,
},
Algorithm::Ed25519,
#[cfg(feature = "rsa")]
Algorithm::Rsa { hash: None },
#[cfg(feature = "rsa")]
Algorithm::Rsa {
hash: Some(ssh_key::HashAlg::Sha256),
},
#[cfg(feature = "rsa")]
Algorithm::Rsa {
hash: Some(ssh_key::HashAlg::Sha512),
},
Algorithm::SkEcdsaSha2NistP256,
Algorithm::SkEd25519,
];