Skip to content

Commit

Permalink
ssh-key: impl decode_as for KeypairData (#211)
Browse files Browse the repository at this point in the history
  • Loading branch information
baloo committed Apr 12, 2024
1 parent cb2706f commit bed4f1c
Showing 1 changed file with 30 additions and 24 deletions.
54 changes: 30 additions & 24 deletions ssh-key/src/private/keypair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,34 @@ impl KeypairData {

n
}

/// Decode [`KeypairData`] for the specified algorithm.
pub fn decode_as(reader: &mut impl Reader, algorithm: Algorithm) -> Result<Self> {
match algorithm {
#[cfg(feature = "alloc")]
Algorithm::Dsa => DsaKeypair::decode(reader).map(Self::Dsa),
#[cfg(feature = "ecdsa")]
Algorithm::Ecdsa { curve } => match EcdsaKeypair::decode(reader)? {
keypair if keypair.curve() == curve => Ok(Self::Ecdsa(keypair)),
_ => Err(Error::AlgorithmUnknown),
},
Algorithm::Ed25519 => Ed25519Keypair::decode(reader).map(Self::Ed25519),
#[cfg(feature = "alloc")]
Algorithm::Rsa { .. } => RsaKeypair::decode(reader).map(Self::Rsa),
#[cfg(all(feature = "alloc", feature = "ecdsa"))]
Algorithm::SkEcdsaSha2NistP256 => {
SkEcdsaSha2NistP256::decode(reader).map(Self::SkEcdsaSha2NistP256)
}
#[cfg(feature = "alloc")]
Algorithm::SkEd25519 => SkEd25519::decode(reader).map(Self::SkEd25519),
#[cfg(feature = "alloc")]
algorithm @ Algorithm::Other(_) => {
OpaqueKeypair::decode_as(reader, algorithm).map(Self::Other)
}
#[allow(unreachable_patterns)]
_ => Err(Error::AlgorithmUnknown),
}
}
}

impl ConstantTimeEq for KeypairData {
Expand Down Expand Up @@ -286,30 +314,8 @@ impl Decode for KeypairData {
type Error = Error;

fn decode(reader: &mut impl Reader) -> Result<Self> {
match Algorithm::decode(reader)? {
#[cfg(feature = "alloc")]
Algorithm::Dsa => DsaKeypair::decode(reader).map(Self::Dsa),
#[cfg(feature = "ecdsa")]
Algorithm::Ecdsa { curve } => match EcdsaKeypair::decode(reader)? {
keypair if keypair.curve() == curve => Ok(Self::Ecdsa(keypair)),
_ => Err(Error::AlgorithmUnknown),
},
Algorithm::Ed25519 => Ed25519Keypair::decode(reader).map(Self::Ed25519),
#[cfg(feature = "alloc")]
Algorithm::Rsa { .. } => RsaKeypair::decode(reader).map(Self::Rsa),
#[cfg(all(feature = "alloc", feature = "ecdsa"))]
Algorithm::SkEcdsaSha2NistP256 => {
SkEcdsaSha2NistP256::decode(reader).map(Self::SkEcdsaSha2NistP256)
}
#[cfg(feature = "alloc")]
Algorithm::SkEd25519 => SkEd25519::decode(reader).map(Self::SkEd25519),
#[cfg(feature = "alloc")]
algorithm @ Algorithm::Other(_) => {
OpaqueKeypair::decode_as(reader, algorithm).map(Self::Other)
}
#[allow(unreachable_patterns)]
_ => Err(Error::AlgorithmUnknown),
}
let algorithm = Algorithm::decode(reader)?;
Self::decode_as(reader, algorithm)
}
}

Expand Down

0 comments on commit bed4f1c

Please sign in to comment.