ssh_agent_lib/proto/message/
identity.rs

1//! Data returned to the client when listing keys.
2
3use ssh_encoding::{self, CheckedSum, Decode, Encode, Reader, Writer};
4use ssh_key::public::KeyData;
5
6use crate::proto::{Error, Result};
7
8/// Data returned to the client when listing keys.
9///
10/// A list of these structures are sent in a [`Response::IdentitiesAnswer`](super::Response::IdentitiesAnswer) (`SSH_AGENT_IDENTITIES_ANSWER`) message body.
11///
12/// Described in [draft-miller-ssh-agent-14 ยง 3.5](https://www.ietf.org/archive/id/draft-miller-ssh-agent-14.html#section-3.5)
13#[derive(Clone, PartialEq, Debug)]
14pub struct Identity {
15    /// A standard public-key encoding of an underlying key.
16    pub pubkey: KeyData,
17
18    /// A human-readable comment
19    pub comment: String,
20}
21
22impl Identity {
23    pub(crate) fn decode_vec(reader: &mut impl Reader) -> Result<Vec<Self>> {
24        let len = u32::decode(reader)?;
25        let mut identities = vec![];
26
27        for _ in 0..len {
28            identities.push(Self::decode(reader)?);
29        }
30
31        Ok(identities)
32    }
33}
34
35impl Decode for Identity {
36    type Error = Error;
37
38    fn decode(reader: &mut impl Reader) -> Result<Self> {
39        let pubkey = reader.read_prefixed(KeyData::decode)?;
40        let comment = String::decode(reader)?;
41
42        Ok(Self { pubkey, comment })
43    }
44}
45
46impl Encode for Identity {
47    fn encoded_len(&self) -> ssh_encoding::Result<usize> {
48        [
49            self.pubkey.encoded_len_prefixed()?,
50            self.comment.encoded_len()?,
51        ]
52        .checked_sum()
53    }
54
55    fn encode(&self, writer: &mut impl Writer) -> ssh_encoding::Result<()> {
56        self.pubkey.encode_prefixed(writer)?;
57        self.comment.encode(writer)?;
58
59        Ok(())
60    }
61}