Skip to content

Commit 2f232d9

Browse files
committed
Refactor proto::message::Identity into module
Signed-off-by: Ross Williams <ross@ross-williams.net>
1 parent 4931eb3 commit 2f232d9

File tree

2 files changed

+64
-56
lines changed

2 files changed

+64
-56
lines changed

src/proto/message.rs

Lines changed: 2 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Agent protocol message structures.
22
3+
pub mod identity;
34
pub mod unparsed;
45

56
use core::str::FromStr;
@@ -9,69 +10,14 @@ use ssh_key::{
910
certificate::Certificate, private::KeypairData, public::KeyData, Algorithm, Error, Signature,
1011
};
1112

12-
pub use self::unparsed::*;
13+
pub use self::{identity::*, unparsed::*};
1314
use super::{
1415
extension::{KeyConstraintExtension, MessageExtension},
1516
PrivateKeyData, ProtoError,
1617
};
1718

1819
type Result<T> = core::result::Result<T, ProtoError>;
1920

20-
/// Data returned to the client when listing keys.
21-
///
22-
/// A list of these structures are sent in a [`Response::IdentitiesAnswer`] (`SSH_AGENT_IDENTITIES_ANSWER`) message body.
23-
///
24-
/// Described in [draft-miller-ssh-agent-14 § 3.5](https://www.ietf.org/archive/id/draft-miller-ssh-agent-14.html#section-3.5)
25-
#[derive(Clone, PartialEq, Debug)]
26-
pub struct Identity {
27-
/// A standard public-key encoding of an underlying key.
28-
pub pubkey: KeyData,
29-
30-
/// A human-readable comment
31-
pub comment: String,
32-
}
33-
34-
impl Identity {
35-
fn decode_vec(reader: &mut impl Reader) -> Result<Vec<Self>> {
36-
let len = u32::decode(reader)?;
37-
let mut identities = vec![];
38-
39-
for _ in 0..len {
40-
identities.push(Self::decode(reader)?);
41-
}
42-
43-
Ok(identities)
44-
}
45-
}
46-
47-
impl Decode for Identity {
48-
type Error = ProtoError;
49-
50-
fn decode(reader: &mut impl Reader) -> Result<Self> {
51-
let pubkey = reader.read_prefixed(KeyData::decode)?;
52-
let comment = String::decode(reader)?;
53-
54-
Ok(Self { pubkey, comment })
55-
}
56-
}
57-
58-
impl Encode for Identity {
59-
fn encoded_len(&self) -> ssh_encoding::Result<usize> {
60-
[
61-
self.pubkey.encoded_len_prefixed()?,
62-
self.comment.encoded_len()?,
63-
]
64-
.checked_sum()
65-
}
66-
67-
fn encode(&self, writer: &mut impl Writer) -> ssh_encoding::Result<()> {
68-
self.pubkey.encode_prefixed(writer)?;
69-
self.comment.encode(writer)?;
70-
71-
Ok(())
72-
}
73-
}
74-
7521
/// Signature request with data to be signed with a key in an agent.
7622
///
7723
/// This structure is sent in a [`Request::SignRequest`] (`SSH_AGENTC_SIGN_REQUEST`) message.

src/proto/message/identity.rs

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

0 commit comments

Comments
 (0)