#![doc = include_str!("../README.md")]
#![forbid(unsafe_code)]
pub mod cloud;
pub mod device;
pub mod roots;
pub use darkbio_crypto as crypto;
use darkbio_crypto::{cwt, xdsa};
use std::fmt;
use std::time::Duration;
pub const CRYPTO_DOMAIN_DEVICE_ATTESTATION: &[u8] = b"device-attestation-v1";
pub const CRYPTO_DOMAIN_CLOUD_ATTESTATION: &[u8] = b"cloud-attestation-v1";
pub const EMULATOR_ATTESTATION_MAX_VALIDITY: Duration = Duration::from_secs(3600 * 24 * 30);
pub const CLOUD_ATTESTATION_MAX_VALIDITY: Duration = Duration::from_secs(3600 * 24 * 90);
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Environment {
Release,
Staging,
Develop,
}
impl fmt::Display for Environment {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Environment::Release => "release",
Environment::Staging => "staging",
Environment::Develop => "develop",
})
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Realm {
Hardware,
Emulator,
}
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("attestation signed by {}, which is not among the trusted roots", describe_signer(.fingerprint, .root))]
UntrustedSigner {
fingerprint: xdsa::Fingerprint,
root: Option<roots::Root>,
},
#[error("attestation is not self-signed")]
NotSelfSigned,
#[error("invalid attestation validity; expected a positive duration of at most {} days", max.as_secs() / 86400)]
InvalidValidity {
max: Duration,
},
#[error("cwt: {0}")]
Cwt(#[from] cwt::Error),
}
fn check_validity(nbf: u64, exp: u64, max: Duration) -> Result<(), Error> {
if nbf >= exp || exp - nbf > max.as_secs() {
return Err(Error::InvalidValidity { max });
}
Ok(())
}
fn describe_signer(fingerprint: &xdsa::Fingerprint, root: &Option<roots::Root>) -> String {
match root {
Some(info) => format!("the {info} ({})", hex::encode(fingerprint.to_bytes())),
None => format!("unknown key {}", hex::encode(fingerprint.to_bytes())),
}
}
impl Error {
pub(crate) fn untrusted_signer(fingerprint: xdsa::Fingerprint) -> Self {
Self::UntrustedSigner {
root: roots::identify(&fingerprint),
fingerprint,
}
}
}