Skip to content

Commit

Permalink
Handle pubkey blob in sign call being SSH cert
Browse files Browse the repository at this point in the history
  • Loading branch information
timweri committed Nov 29, 2023
1 parent 2b41dc9 commit 8bd4025
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions rustica-agent/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ impl SshAgentHandler for Handler {
_flags: u32,
) -> Result<Response, AgentError> {
trace!("Sign call");

// Tri check to find how to sign the request. Since starting rustica with a file based
// key is the same process as keys added afterwards, we do this to prevent duplication
// of the private key based signing code.
Expand Down Expand Up @@ -333,13 +334,27 @@ impl SshAgentHandler for Handler {

return Ok(Response::SignResponse { signature });
} else if let Signatory::Direct(privkey) = &self.signatory {
// Extract the pubkey fingerprint from either the SSH pubkey or the SSH cert
let fingerprint = match (Certificate::from_bytes(&pubkey), PublicKey::from_bytes(&pubkey)) {
(Ok(cert), _) => cert.key.fingerprint(),
(_, Ok(pubkey)) => pubkey.fingerprint(),
_ => return Err(AgentError::from("Invalid key blob")),
};

// Don't sign requests if the requested key does not match the signatory
if privkey.pubkey.encode() != pubkey {
if privkey.pubkey.fingerprint() != fingerprint {
return Err(AgentError::from("No such key"));
}

Some(privkey)
} else if let Signatory::Yubikey(signer) = &mut self.signatory {
// Extract the pubkey fingerprint from either the SSH pubkey or the SSH cert
let fingerprint = match (Certificate::from_bytes(&pubkey), PublicKey::from_bytes(&pubkey)) {
(Ok(cert), _) => cert.key.fingerprint(),
(_, Ok(pubkey)) => pubkey.fingerprint(),
_ => return Err(AgentError::from("Invalid key blob")),
};

// Don't sign requests if the requested key does not match the signatory
if signer
.yk
Expand All @@ -348,11 +363,12 @@ impl SshAgentHandler for Handler {
println!("Yubikey Fetch Certificate Error: {e}");
AgentError::from("Yubikey fetch certificate error")
})?
.encode()
!= pubkey
.fingerprint()
!= fingerprint
{
return Err(AgentError::from("No such key"));
}

// Since we are using the Yubikey for a signing operation the only time they
// won't have to tap here is if they are using cached keys and this is right after
// a secure Rustica tap. In most cases, we'll need to send this, rarely, it'll be
Expand Down

0 comments on commit 8bd4025

Please sign in to comment.