Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of key constraints #203

Merged
merged 1 commit into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading