Skip to content

Commit

Permalink
ssh-key: Add Certificate::decode_as
Browse files Browse the repository at this point in the history
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 committed Jul 9, 2024
1 parent e43e0a0 commit 9cbe34a
Showing 1 changed file with 11 additions and 7 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

0 comments on commit 9cbe34a

Please sign in to comment.