Skip to content

Commit

Permalink
Fix handling of key constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
george-hopkins authored and Eugeny committed Nov 1, 2023
1 parent 6fdbb09 commit a904a08
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
18 changes: 13 additions & 5 deletions russh-keys/src/agent/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ impl<S: AsyncRead + AsyncWrite + Unpin> AgentClient<S> {
Ok(())
}

async fn read_success(&mut self) -> Result<(), Error> {
self.read_response().await?;
if self.buf.first() == Some(&msg::SUCCESS) {
Ok(())
} else {
Err(Error::AgentFailure)
}
}

/// Send a key to the agent, with a (possibly empty) slice of
/// constraints to apply when using the key to sign.
pub async fn add_identity(
Expand Down Expand Up @@ -131,12 +140,11 @@ impl<S: AsyncRead + AsyncWrite + Unpin> AgentClient<S> {
}
}
if !constraints.is_empty() {
self.buf.push_u32_be(constraints.len() as u32);
for cons in constraints {
match *cons {
Constraint::KeyLifetime { seconds } => {
self.buf.push(msg::CONSTRAIN_LIFETIME);
self.buf.push_u32_be(seconds)
self.buf.push_u32_be(seconds);
}
Constraint::Confirm => self.buf.push(msg::CONSTRAIN_CONFIRM),
Constraint::Extensions {
Expand All @@ -153,7 +161,7 @@ impl<S: AsyncRead + AsyncWrite + Unpin> AgentClient<S> {
let len = self.buf.len() - 4;
BigEndian::write_u32(&mut self.buf[..], len as u32);

self.read_response().await?;
self.read_success().await?;
Ok(())
}

Expand Down Expand Up @@ -467,8 +475,8 @@ impl<S: AsyncRead + AsyncWrite + Unpin> AgentClient<S> {
self.buf.clear();
self.buf.resize(4);
self.buf.push(msg::REMOVE_ALL_IDENTITIES);
BigEndian::write_u32(&mut self.buf[..], 5);
self.read_response().await?;
BigEndian::write_u32(&mut self.buf[..], 1);
self.read_success().await?;
Ok(())
}

Expand Down
3 changes: 2 additions & 1 deletion russh-keys/src/agent/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ pub const EXTENSION: u8 = 27;

pub const CONSTRAIN_LIFETIME: u8 = 1;
pub const CONSTRAIN_CONFIRM: u8 = 2;
pub const CONSTRAIN_EXTENSION: u8 = 3;
// pub const CONSTRAIN_MAXSIGN: u8 = 3;
pub const CONSTRAIN_EXTENSION: u8 = 255;
6 changes: 2 additions & 4 deletions russh-keys/src/agent/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,10 +325,8 @@ impl<S: AsyncRead + AsyncWrite + Send + Unpin + 'static, A: Agent + Send + Sync
let mut w = self.keys.0.write().or(Err(Error::AgentFailure))?;
let now = SystemTime::now();
if constrained {
let n = r.read_u32()?;
let mut c = Vec::new();
for _ in 0..n {
let t = r.read_byte()?;
while let Ok(t) = r.read_byte() {
if t == msg::CONSTRAIN_LIFETIME {
let seconds = r.read_u32()?;
c.push(Constraint::KeyLifetime { seconds });
Expand All @@ -353,7 +351,7 @@ impl<S: AsyncRead + AsyncWrite + Send + Unpin + 'static, A: Agent + Send + Sync
return Ok(false);
}
}
w.insert(blob, (Arc::new(key), now, Vec::new()));
w.insert(blob, (Arc::new(key), now, c));
} else {
w.insert(blob, (Arc::new(key), now, Vec::new()));
}
Expand Down

0 comments on commit a904a08

Please sign in to comment.