Skip to main content

oxabl_cache_crypto/
identity.rs

1//! Member identities: one X25519 key to receive the root key and one Ed25519 key to sign.
2
3use age::secrecy::SecretString;
4use ed25519_dalek::{Signer, SigningKey};
5use oxabl_cache_format::{MemberDescriptor, PublicKey, Signature};
6use zeroize::{ZeroizeOnDrop, Zeroizing};
7
8use crate::error::{Error, Result};
9use crate::identity_string::{
10    IdentitySecrets, age_recipient_bytes, age_secret_string, decode_identity, encode_identity,
11};
12use crate::random;
13
14/// A member's secret identity. Zeroizes on drop; `Debug` shows only the public keys.
15pub struct Identity {
16    x25519_secret: Zeroizing<[u8; 32]>,
17    x25519: age::x25519::Identity,
18    signing: SigningKey,
19}
20
21impl Identity {
22    /// A new identity from the operating system's random generator.
23    pub fn generate() -> Result<Self> {
24        Self::from_secrets(IdentitySecrets {
25            x25519: Zeroizing::new(random::array()?),
26            ed25519_seed: Zeroizing::new(random::array()?),
27        })
28    }
29
30    pub(crate) fn from_secrets(secrets: IdentitySecrets) -> Result<Self> {
31        let age_secret = age_secret_string(&secrets.x25519)?;
32        let x25519 = age_secret
33            .parse::<age::x25519::Identity>()
34            .map_err(|_| Error::Identity("invalid X25519 secret"))?;
35        Ok(Self {
36            x25519,
37            signing: SigningKey::from_bytes(&secrets.ed25519_seed),
38            x25519_secret: secrets.x25519,
39        })
40    }
41
42    /// Parses an `OXABL-CACHE-IDENTITY-1…` string, in upper or lower case.
43    pub fn parse(s: &str) -> Result<Self> {
44        Self::from_secrets(decode_identity(s)?)
45    }
46
47    /// The identity string. Anyone who sees it holds the identity.
48    pub fn to_secret_string(&self) -> Result<SecretString> {
49        let encoded = encode_identity(&IdentitySecrets {
50            x25519: self.x25519_secret.clone(),
51            ed25519_seed: Zeroizing::new(self.signing.to_bytes()),
52        })?;
53        Ok(SecretString::from(encoded.to_string()))
54    }
55
56    /// The Ed25519 public key that signs this member's entries.
57    pub fn signing_public(&self) -> PublicKey {
58        self.signing.verifying_key().to_bytes()
59    }
60
61    /// The `age1…` recipient that receives the root key.
62    pub fn recipient(&self) -> String {
63        self.x25519.to_public().to_string()
64    }
65
66    /// The public halves, for `member add`.
67    pub fn descriptor(&self) -> Result<MemberDescriptor> {
68        Ok(MemberDescriptor {
69            ed25519_public: self.signing_public(),
70            x25519_public: age_recipient_bytes(&self.recipient())?,
71        })
72    }
73
74    /// The Bech32 string of [`Identity::descriptor`].
75    pub fn descriptor_string(&self) -> Result<String> {
76        Ok(self.descriptor()?.encode()?)
77    }
78
79    pub(crate) fn sign(&self, message: &[u8]) -> Signature {
80        self.signing.sign(message).to_bytes()
81    }
82
83    pub(crate) fn age_identity(&self) -> &age::x25519::Identity {
84        &self.x25519
85    }
86}
87
88/// Every secret field zeroizes on drop: the X25519 secret, the age identity and the signing key.
89impl ZeroizeOnDrop for Identity {}
90
91impl std::fmt::Debug for Identity {
92    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
93        f.debug_struct("Identity")
94            .field("signing_public", &self.signing_public())
95            .field("recipient", &self.recipient())
96            .finish_non_exhaustive()
97    }
98}