Skip to content

Commit a904a08

Browse files
george-hopkinsEugeny
authored andcommitted
Fix handling of key constraints
1 parent 6fdbb09 commit a904a08

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

russh-keys/src/agent/client.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ impl<S: AsyncRead + AsyncWrite + Unpin> AgentClient<S> {
8787
Ok(())
8888
}
8989

90+
async fn read_success(&mut self) -> Result<(), Error> {
91+
self.read_response().await?;
92+
if self.buf.first() == Some(&msg::SUCCESS) {
93+
Ok(())
94+
} else {
95+
Err(Error::AgentFailure)
96+
}
97+
}
98+
9099
/// Send a key to the agent, with a (possibly empty) slice of
91100
/// constraints to apply when using the key to sign.
92101
pub async fn add_identity(
@@ -131,12 +140,11 @@ impl<S: AsyncRead + AsyncWrite + Unpin> AgentClient<S> {
131140
}
132141
}
133142
if !constraints.is_empty() {
134-
self.buf.push_u32_be(constraints.len() as u32);
135143
for cons in constraints {
136144
match *cons {
137145
Constraint::KeyLifetime { seconds } => {
138146
self.buf.push(msg::CONSTRAIN_LIFETIME);
139-
self.buf.push_u32_be(seconds)
147+
self.buf.push_u32_be(seconds);
140148
}
141149
Constraint::Confirm => self.buf.push(msg::CONSTRAIN_CONFIRM),
142150
Constraint::Extensions {
@@ -153,7 +161,7 @@ impl<S: AsyncRead + AsyncWrite + Unpin> AgentClient<S> {
153161
let len = self.buf.len() - 4;
154162
BigEndian::write_u32(&mut self.buf[..], len as u32);
155163

156-
self.read_response().await?;
164+
self.read_success().await?;
157165
Ok(())
158166
}
159167

@@ -467,8 +475,8 @@ impl<S: AsyncRead + AsyncWrite + Unpin> AgentClient<S> {
467475
self.buf.clear();
468476
self.buf.resize(4);
469477
self.buf.push(msg::REMOVE_ALL_IDENTITIES);
470-
BigEndian::write_u32(&mut self.buf[..], 5);
471-
self.read_response().await?;
478+
BigEndian::write_u32(&mut self.buf[..], 1);
479+
self.read_success().await?;
472480
Ok(())
473481
}
474482

russh-keys/src/agent/msg.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ pub const EXTENSION: u8 = 27;
1919

2020
pub const CONSTRAIN_LIFETIME: u8 = 1;
2121
pub const CONSTRAIN_CONFIRM: u8 = 2;
22-
pub const CONSTRAIN_EXTENSION: u8 = 3;
22+
// pub const CONSTRAIN_MAXSIGN: u8 = 3;
23+
pub const CONSTRAIN_EXTENSION: u8 = 255;

russh-keys/src/agent/server.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,10 +325,8 @@ impl<S: AsyncRead + AsyncWrite + Send + Unpin + 'static, A: Agent + Send + Sync
325325
let mut w = self.keys.0.write().or(Err(Error::AgentFailure))?;
326326
let now = SystemTime::now();
327327
if constrained {
328-
let n = r.read_u32()?;
329328
let mut c = Vec::new();
330-
for _ in 0..n {
331-
let t = r.read_byte()?;
329+
while let Ok(t) = r.read_byte() {
332330
if t == msg::CONSTRAIN_LIFETIME {
333331
let seconds = r.read_u32()?;
334332
c.push(Constraint::KeyLifetime { seconds });
@@ -353,7 +351,7 @@ impl<S: AsyncRead + AsyncWrite + Send + Unpin + 'static, A: Agent + Send + Sync
353351
return Ok(false);
354352
}
355353
}
356-
w.insert(blob, (Arc::new(key), now, Vec::new()));
354+
w.insert(blob, (Arc::new(key), now, c));
357355
} else {
358356
w.insert(blob, (Arc::new(key), now, Vec::new()));
359357
}

0 commit comments

Comments
 (0)