Skip to main content

crypto/
lib.rs

1// SPDX-License-Identifier: Apache-2.0
2//! Cryptographic signing for Heddle states.
3
4mod ed25519;
5mod error;
6mod p256;
7mod pem_loader;
8mod rsa;
9mod state_signature;
10mod state_signing;
11
12use std::path::Path;
13
14pub use ed25519::Ed25519Signer;
15pub use error::SignerError;
16use objects::object::ContentHash;
17pub use objects::object::SignatureStatus;
18pub use p256::P256Signer;
19pub use pem_loader::{PemKind, classify_pem};
20pub use rsa::RsaSigner;
21pub use state_signature::{
22    StateSignatureError, public_key_bytes, signature_bytes, state_signature_from_signer,
23    verify_state_signature_bytes,
24};
25pub use state_signing::StateSigningExt;
26
27/// Trait for cryptographic signers.
28pub trait Signer: Send + Sync {
29    fn algorithm(&self) -> &'static str;
30    fn public_key(&self) -> &[u8];
31    fn sign(&self, data: &[u8]) -> Result<Vec<u8>, SignerError>;
32    fn verify(&self, data: &[u8], signature: &[u8]) -> Result<(), SignerError>;
33}
34
35/// Load a signer from a key file. When `algorithm` is `None`, the PEM
36/// header (or raw-seed shape) selects the backend via
37/// [`pem_loader::load_signer_from_pem`].
38pub fn load_signer(path: &Path, algorithm: Option<&str>) -> Result<Box<dyn Signer>, SignerError> {
39    reject_group_or_world_readable_key(path)?;
40    let key_data = std::fs::read(path)?;
41    let pem_content = String::from_utf8_lossy(&key_data);
42
43    if let Some(algo) = algorithm {
44        return match algo.to_lowercase().as_str() {
45            "ed25519" => {
46                Ed25519Signer::from_pem(&pem_content).map(|s| Box::new(s) as Box<dyn Signer>)
47            }
48            "rsa" => RsaSigner::from_pem(&pem_content).map(|s| Box::new(s) as Box<dyn Signer>),
49            "p256" | "ecdsa-p256" => {
50                P256Signer::from_pem(&pem_content).map(|s| Box::new(s) as Box<dyn Signer>)
51            }
52            _ => Err(SignerError::UnsupportedAlgorithm(algo.to_string())),
53        };
54    }
55
56    pem_loader::load_signer_from_pem(&pem_content)
57}
58
59/// Reject a private-key file whose permissions expose it to group/world
60/// readers. The single source of the `0600`-or-stricter rule: the key-file
61/// signer loader ([`load_signer`]) and the auto-signing identity loader
62/// (`repo::identity`) both call this so the threshold lives in one place. On
63/// unix, errors with [`SignerError::InsecureKeyPermissions`] when any of the
64/// group/world bits (`0o077`) are set; a no-op on platforms without a unix
65/// permission model. Propagates I/O errors (e.g. `NotFound`) from the stat.
66#[cfg(unix)]
67pub fn reject_group_or_world_readable_key(path: &Path) -> Result<(), SignerError> {
68    use std::os::unix::fs::PermissionsExt;
69
70    let mode = std::fs::metadata(path)?.permissions().mode() & 0o777;
71    if mode & 0o077 != 0 {
72        return Err(SignerError::InsecureKeyPermissions {
73            path: path.to_path_buf(),
74            mode,
75        });
76    }
77    Ok(())
78}
79
80/// Non-unix stub: no permission model to enforce. See the unix variant.
81#[cfg(not(unix))]
82pub fn reject_group_or_world_readable_key(_path: &Path) -> Result<(), SignerError> {
83    Ok(())
84}
85
86/// Verify a state's signature.
87pub fn verify_state_signature(
88    content_hash: &ContentHash,
89    algorithm: &str,
90    public_key: &[u8],
91    signature: &[u8],
92) -> Result<(), SignerError> {
93    verify_payload_signature(content_hash.as_bytes(), algorithm, public_key, signature)
94}
95
96/// Verify a detached signature over an arbitrary payload. Used by
97/// non-state-signature flows (e.g. `ReviewSignature`) that already have a
98/// canonical byte payload built upstream.
99pub fn verify_payload_signature(
100    payload: &[u8],
101    algorithm: &str,
102    public_key: &[u8],
103    signature: &[u8],
104) -> Result<(), SignerError> {
105    match algorithm.to_lowercase().as_str() {
106        "ed25519" => Ed25519Signer::verify_with_public_key(payload, public_key, signature),
107        "rsa" => RsaSigner::verify_with_public_key(payload, public_key, signature),
108        "p256" | "ecdsa-p256" => P256Signer::verify_with_public_key(payload, public_key, signature),
109        _ => Err(SignerError::UnsupportedAlgorithm(algorithm.to_string())),
110    }
111}
112
113#[cfg(test)]
114mod tests {
115    #[cfg(unix)]
116    use std::os::unix::fs::PermissionsExt;
117
118    use objects::fs_atomic::write_file_atomic_secret;
119    use tempfile::TempDir;
120
121    use super::*;
122
123    #[test]
124    fn test_ed25519_sign_verify_roundtrip() {
125        let signer = Ed25519Signer::generate().expect("generate key");
126        let data = b"test data for signing";
127
128        let signature = signer.sign(data).expect("sign data");
129        signer.verify(data, &signature).expect("verify signature");
130    }
131
132    #[test]
133    fn test_ed25519_sign_verify_invalid_signature_fails_explicitly() {
134        let signer = Ed25519Signer::generate().expect("generate key");
135        let data = b"test data for signing";
136
137        let signature = signer.sign(data).expect("sign data");
138        let error = signer
139            .verify(b"wrong data", &signature)
140            .expect_err("verify should fail");
141
142        assert!(matches!(error, SignerError::VerificationFailed));
143    }
144
145    #[test]
146    fn test_load_signer_ed25519() {
147        let temp = TempDir::new().expect("create temp dir");
148        let key_path = temp.path().join("test_ed25519.pem");
149
150        let signer = Ed25519Signer::generate().expect("generate key");
151        let pem = signer.to_pem().expect("export to PEM");
152        write_file_atomic_secret(&key_path, pem.as_bytes()).expect("write key file");
153
154        let loaded = load_signer(&key_path, Some("ed25519")).expect("load signer");
155        assert_eq!(loaded.algorithm(), "ed25519");
156        assert_eq!(loaded.public_key(), signer.public_key());
157    }
158
159    #[cfg(unix)]
160    #[test]
161    fn load_signer_refuses_group_or_world_readable_private_key() {
162        let temp = TempDir::new().expect("create temp dir");
163        let key_path = temp.path().join("test_ed25519.pem");
164
165        let signer = Ed25519Signer::generate().expect("generate key");
166        let pem = signer.to_pem().expect("export to PEM");
167        write_file_atomic_secret(&key_path, pem.as_bytes()).expect("write key file");
168        std::fs::set_permissions(&key_path, std::fs::Permissions::from_mode(0o644))
169            .expect("make key insecure");
170
171        let err = match load_signer(&key_path, Some("ed25519")) {
172            Ok(_) => panic!("insecure key must fail"),
173            Err(err) => err,
174        };
175        assert!(matches!(
176            err,
177            SignerError::InsecureKeyPermissions { mode: 0o644, .. }
178        ));
179        // The refusal must be actionable: name the offending path, the
180        // observed + required modes, and the exact chmod to run.
181        let msg = err.to_string();
182        assert!(msg.contains(&key_path.display().to_string()), "{msg}");
183        assert!(msg.contains("0644"), "{msg}");
184        assert!(msg.contains("0600"), "{msg}");
185        assert!(msg.contains("chmod 600"), "{msg}");
186    }
187
188    #[cfg(unix)]
189    #[test]
190    fn load_signer_accepts_owner_only_private_key() {
191        let temp = TempDir::new().expect("create temp dir");
192        let key_path = temp.path().join("test_ed25519.pem");
193
194        let signer = Ed25519Signer::generate().expect("generate key");
195        let pem = signer.to_pem().expect("export to PEM");
196        write_file_atomic_secret(&key_path, pem.as_bytes()).expect("write key file");
197        std::fs::set_permissions(&key_path, std::fs::Permissions::from_mode(0o600))
198            .expect("set owner-only mode");
199
200        let loaded = load_signer(&key_path, Some("ed25519")).expect("0600 key must load");
201        assert_eq!(loaded.public_key(), signer.public_key());
202    }
203}