Skip to content

Commit

Permalink
ssh-key: Add Certificate::decode_as and expose KeyData::decode_as (
Browse files Browse the repository at this point in the history
…#233)

* Expose `KeyData::decode_as`
* Add `Certificate::decode_as`

This additional function is needed for SSH Agent Protocol where, based on
the algorithm, we need to parse the `Certificate` or the `KeyData`.

Without `decode_as` the `decode` function will greedily consume additional
string from the reader.

See: wiktor-k/ssh-agent-lib#83

Signed-off-by: Wiktor Kwapisiewicz <wiktor@metacode.biz>
  • Loading branch information
wiktor-k authored Jul 10, 2024
1 parent 1d40179 commit bbf54df
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
18 changes: 11 additions & 7 deletions ssh-key/src/certificate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,14 +455,9 @@ impl Certificate {
self.reserved.encode(writer)?;
self.signature_key.encode_prefixed(writer)
}
}

impl Decode for Certificate {
type Error = Error;

fn decode(reader: &mut impl Reader) -> Result<Self> {
let algorithm = Algorithm::new_certificate(&String::decode(reader)?)?;

/// Decode [`Certificate`] for the specified algorithm.
pub fn decode_as(reader: &mut impl Reader, algorithm: Algorithm) -> Result<Self> {
Ok(Self {
nonce: Vec::decode(reader)?,
public_key: KeyData::decode_as(reader, algorithm)?,
Expand All @@ -482,6 +477,15 @@ impl Decode for Certificate {
}
}

impl Decode for Certificate {
type Error = Error;

fn decode(reader: &mut impl Reader) -> Result<Self> {
let algorithm = Algorithm::new_certificate(&String::decode(reader)?)?;
Self::decode_as(reader, algorithm)
}
}

impl Encode for Certificate {
fn encoded_len(&self) -> encoding::Result<usize> {
[
Expand Down
2 changes: 1 addition & 1 deletion ssh-key/src/public/key_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ impl KeyData {
}

/// Decode [`KeyData`] for the specified algorithm.
pub(crate) fn decode_as(reader: &mut impl Reader, algorithm: Algorithm) -> Result<Self> {
pub fn decode_as(reader: &mut impl Reader, algorithm: Algorithm) -> Result<Self> {
match algorithm {
#[cfg(feature = "alloc")]
Algorithm::Dsa => DsaPublicKey::decode(reader).map(Self::Dsa),
Expand Down

0 comments on commit bbf54df

Please sign in to comment.