Skip to main content

auths_crypto/
ssh.rs

1//! OpenSSH public key parsing for Ed25519 keys.
2
3use ssh_key::PublicKey;
4
5/// Errors from parsing an OpenSSH Ed25519 public key.
6#[derive(Debug, Clone, thiserror::Error, PartialEq, Eq)]
7pub enum SshKeyError {
8    #[error("Malformed or invalid OpenSSH public key: {0}")]
9    InvalidFormat(String),
10
11    #[error("Unsupported key type: expected ssh-ed25519")]
12    UnsupportedKeyType,
13}
14
15/// Parse an OpenSSH Ed25519 public key line and return the raw 32-byte public key.
16///
17/// Args:
18/// * `openssh_pub`: A full OpenSSH public key line, e.g. `"ssh-ed25519 AAAA... comment"`.
19///
20/// Usage:
21/// ```ignore
22/// let raw = openssh_pub_to_raw_ed25519("ssh-ed25519 AAAA...")?;
23/// assert_eq!(raw.len(), 32);
24/// ```
25pub fn openssh_pub_to_raw_ed25519(openssh_pub: &str) -> Result<[u8; 32], SshKeyError> {
26    let public_key = PublicKey::from_openssh(openssh_pub)
27        .map_err(|e| SshKeyError::InvalidFormat(e.to_string()))?;
28
29    let ed25519_key = public_key
30        .key_data()
31        .ed25519()
32        .ok_or(SshKeyError::UnsupportedKeyType)?;
33
34    Ok(ed25519_key.0)
35}