ssh_agent_client_rs/
lib.rs

1//! # ssh-agent-client-rs
2//!
3//! An ssh-agent client implementation in rust, aiming to provide a robust,
4//! well tested and easy to use synchronous API to interact with an ssh-agent.
5//!
6//! # Examples
7//! ```no_run
8//! use ssh_agent_client_rs::Client;
9//! # use std::env;
10//! # use std::path::Path;
11//! # use ssh_agent_client_rs::{Identity, Error};
12//! use ssh_key::PublicKey;
13//!
14//! # let env = env::var("SSH_AUTH_SOCK").unwrap();
15//! # let path_to_ssh_auth_socket = Path::new(env.as_str());
16//! let mut client = Client::connect(path_to_ssh_auth_socket).expect("failed to connect");
17//!
18//! // List the identities that the connected ssh-agent makes available
19//! let identities: Vec<Identity> = client.list_all_identities().expect("failed to list identities");
20//! ```
21
22use crate::codec::{read_message, write_message, ReadMessage, WriteMessage};
23#[cfg(target_family = "windows")]
24use interprocess::os::windows::named_pipe::{pipe_mode, DuplexPipeStream};
25use ssh_key::public::KeyData;
26use ssh_key::{Certificate, PrivateKey, PublicKey, Signature};
27use std::borrow::Cow;
28use std::io::{Read, Write};
29#[cfg(target_family = "unix")]
30use std::os::unix::net::UnixStream;
31use std::path::Path;
32
33mod codec;
34mod error;
35
36pub use self::error::Error;
37pub use self::error::Result;
38
39/// A combination of the std::io::Read and std::io::Write traits.
40pub trait ReadWrite: Read + Write {}
41
42/// A Client instance is an object that can be used to interact with an ssh-agent,
43/// typically using a Unix socket
44pub struct Client {
45    socket: Box<dyn ReadWrite>,
46}
47
48#[derive(Debug, PartialEq, Clone)]
49pub enum Identity<'a> {
50    PublicKey(Box<Cow<'a, PublicKey>>),
51    Certificate(Box<Cow<'a, Certificate>>),
52}
53
54impl<'a> From<PublicKey> for Identity<'a> {
55    fn from(value: PublicKey) -> Self {
56        Identity::PublicKey(Box::new(Cow::Owned(value)))
57    }
58}
59
60impl<'a> From<&'a PublicKey> for Identity<'a> {
61    fn from(value: &'a PublicKey) -> Self {
62        Identity::PublicKey(Box::new(Cow::Borrowed(value)))
63    }
64}
65
66impl<'a> From<Certificate> for Identity<'a> {
67    fn from(value: Certificate) -> Self {
68        Identity::Certificate(Box::new(Cow::Owned(value)))
69    }
70}
71
72impl<'a> From<&'a Certificate> for Identity<'a> {
73    fn from(value: &'a Certificate) -> Self {
74        Identity::Certificate(Box::new(Cow::Borrowed(value)))
75    }
76}
77
78impl<'a> From<&'a Identity<'a>> for &'a KeyData {
79    fn from(value: &'a Identity) -> Self {
80        match value {
81            Identity::PublicKey(pk) => pk.key_data(),
82            Identity::Certificate(cert) => cert.public_key(),
83        }
84    }
85}
86
87impl<T> ReadWrite for T where T: Read + Write {}
88
89impl<'a> Client {
90    /// Constructs a Client connected to a unix socket referenced by path.
91    #[cfg(target_family = "unix")]
92    pub fn connect(path: &Path) -> Result<Client> {
93        let socket = Box::new(UnixStream::connect(path)?);
94        Ok(Client { socket })
95    }
96
97    // If you want to communicate with the ssh-agent shipped with windows you probably want to pass
98    // Path::new(r"\\.\pipe\openssh-ssh-agent")
99    #[cfg(target_family = "windows")]
100    pub fn connect(path: &Path) -> Result<Client> {
101        let pipe = DuplexPipeStream::<pipe_mode::Bytes>::connect_by_path(path)?;
102        Ok(Client {
103            socket: Box::new(pipe),
104        })
105    }
106
107    /// Construct a Client backed by an implementation of ReadWrite, mainly useful for
108    /// testing.
109    pub fn with_read_write(read_write: Box<dyn ReadWrite>) -> Client {
110        Client { socket: read_write }
111    }
112
113    /// List the identities that has been added to the connected ssh-agent. Identities that
114    /// are not ssh public keys, particularly identities that corresponds to certs, are ignored
115    #[deprecated(note = "Use list_all_identities() instead")]
116    pub fn list_identities(&mut self) -> Result<Vec<PublicKey>> {
117        self.list_all_identities().map(|identities| {
118            identities
119                .into_iter()
120                .filter_map(|i| match i {
121                    Identity::PublicKey(pk) => Some(pk.into_owned()),
122                    _ => None,
123                })
124                .collect()
125        })
126    }
127    /// List the identities that have been added to the connected ssh-agent including certs.
128    pub fn list_all_identities(&mut self) -> Result<Vec<Identity>> {
129        write_message(&mut self.socket, WriteMessage::RequestIdentities)?;
130        match read_message(&mut self.socket)? {
131            ReadMessage::Identities(identities) => Ok(identities),
132            m => Err(unexpected_response(m)),
133        }
134    }
135
136    /// Add an identity to the connected ssh-agent.
137    pub fn add_identity(&mut self, key: &PrivateKey) -> Result<()> {
138        write_message(&mut self.socket, WriteMessage::AddIdentity(key))?;
139        self.expect_success()
140    }
141
142    /// Remove an identity from the connected ssh-agent.
143    pub fn remove_identity(&mut self, key: &PrivateKey) -> Result<()> {
144        write_message(&mut self.socket, WriteMessage::RemoveIdentity(key))?;
145        self.expect_success()
146    }
147
148    /// Remove all identities from the connected ssh-agent.
149    pub fn remove_all_identities(&mut self) -> Result<()> {
150        write_message(&mut self.socket, WriteMessage::RemoveAllIdentities)?;
151        self.expect_success()
152    }
153
154    /// Instruct the connected ssh-agent to sign data with the private key associated with the
155    /// provided public key. For now, sign requests with RSA keys are hard coded to use the
156    /// SHA-512 hashing algorithm.
157    pub fn sign(&mut self, key: impl Into<Identity<'a>>, data: &[u8]) -> Result<Signature> {
158        self.sign_with_ref(&key.into(), data)
159    }
160    pub fn sign_with_ref(&mut self, identity: &Identity, data: &[u8]) -> Result<Signature> {
161        write_message(&mut self.socket, WriteMessage::Sign(identity, data))?;
162        match read_message(&mut self.socket)? {
163            ReadMessage::Signature(sig) => Ok(sig),
164            ReadMessage::Failure => Err(Error::RemoteFailure),
165            m => Err(unexpected_response(m)),
166        }
167    }
168
169    fn expect_success(&mut self) -> Result<()> {
170        let response = read_message(&mut self.socket)?;
171        match response {
172            ReadMessage::Success => Ok(()),
173            ReadMessage::Failure => Err(Error::RemoteFailure),
174            _ => Err(Error::InvalidMessage("Unexpected response".to_string())),
175        }
176    }
177}
178
179fn unexpected_response(message: ReadMessage) -> Error {
180    let error = format!("Agent responded with unexpected message '{message:?}'");
181    Error::InvalidMessage(error)
182}