crypto: RefCell instead of Mutex in Agent
This commit is contained in:
parent
62d000f7cb
commit
3b5fac178e
|
|
@ -39,7 +39,7 @@ impl SignerError {
|
|||
}
|
||||
}
|
||||
|
||||
pub trait Signer: Send + Sync {
|
||||
pub trait Signer: Send {
|
||||
/// Return this signer's public/verification key.
|
||||
fn public_key(&self) -> &PublicKey;
|
||||
/// Sign a message and return the signature.
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
use std::ops::{Deref, DerefMut};
|
||||
use std::sync::Mutex;
|
||||
use std::cell::RefCell;
|
||||
|
||||
pub use radicle_ssh::agent::client::AgentClient;
|
||||
pub use radicle_ssh::agent::client::Error;
|
||||
|
|
@ -27,41 +26,47 @@ impl Agent {
|
|||
self.client.add_identity(key, &[])
|
||||
}
|
||||
|
||||
pub fn unregister(&mut self, key: &PublicKey) -> Result<(), ssh::Error> {
|
||||
self.client.remove_identity(key)
|
||||
}
|
||||
|
||||
pub fn unregister_all(&mut self) -> Result<(), ssh::Error> {
|
||||
self.client.remove_all_identities()
|
||||
}
|
||||
|
||||
pub fn sign(&mut self, key: &PublicKey, data: &[u8]) -> Result<[u8; 64], ssh::Error> {
|
||||
self.client.sign(key, data)
|
||||
}
|
||||
|
||||
/// Get a signer from this agent, given the public key.
|
||||
pub fn signer(self, key: PublicKey) -> AgentSigner {
|
||||
AgentSigner::new(self, key)
|
||||
}
|
||||
|
||||
pub fn pid(&self) -> Option<u32> {
|
||||
self.client.pid()
|
||||
}
|
||||
|
||||
impl Deref for Agent {
|
||||
type Target = AgentClient<Stream>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.client
|
||||
}
|
||||
}
|
||||
|
||||
impl DerefMut for Agent {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.client
|
||||
pub fn request_identities(&mut self) -> Result<Vec<PublicKey>, ssh::agent::client::Error> {
|
||||
self.client.request_identities()
|
||||
}
|
||||
}
|
||||
|
||||
/// A [`Signer`] that uses `ssh-agent`.
|
||||
pub struct AgentSigner {
|
||||
agent: Mutex<Agent>,
|
||||
agent: RefCell<Agent>,
|
||||
public: PublicKey,
|
||||
}
|
||||
|
||||
impl AgentSigner {
|
||||
pub fn new(agent: Agent, public: PublicKey) -> Self {
|
||||
let agent = Mutex::new(agent);
|
||||
let agent = RefCell::new(agent);
|
||||
|
||||
Self { agent, public }
|
||||
}
|
||||
|
||||
pub fn is_ready(&self) -> Result<bool, Error> {
|
||||
let ids = self.agent.lock().unwrap().request_identities()?;
|
||||
let ids = self.agent.borrow_mut().request_identities()?;
|
||||
|
||||
Ok(ids.contains(&self.public))
|
||||
}
|
||||
|
|
@ -84,9 +89,7 @@ impl Signer for AgentSigner {
|
|||
fn try_sign(&self, msg: &[u8]) -> Result<Signature, SignerError> {
|
||||
let sig = self
|
||||
.agent
|
||||
.lock()
|
||||
// We'll take our chances here; the worse that can happen is the agent returns an error.
|
||||
.unwrap_or_else(|e| e.into_inner())
|
||||
.borrow_mut()
|
||||
.sign(&self.public, msg)
|
||||
.map_err(SignerError::new)?;
|
||||
|
||||
|
|
|
|||
|
|
@ -28,11 +28,11 @@ fn main() -> anyhow::Result<()> {
|
|||
println!("ok");
|
||||
}
|
||||
Some("remove") => {
|
||||
agent.remove_identity(profile.id())?;
|
||||
agent.unregister(profile.id())?;
|
||||
println!("ok");
|
||||
}
|
||||
Some("remove-all") => {
|
||||
agent.remove_all_identities()?;
|
||||
agent.unregister_all()?;
|
||||
println!("ok");
|
||||
}
|
||||
Some("sign") => {
|
||||
|
|
|
|||
Loading…
Reference in New Issue