use lgwks_deps::p256;
use lgwks_macros::Error;
use super::ensure;
mod app_attest;
pub mod apple;
pub mod der;
pub mod trustzone;
pub use app_attest::{
AppAttestAttestation, AppAttestError, AppAttestProductionError, app_attest_is_supported,
produce_app_attest_evidence,
};
#[derive(Clone, Debug, PartialEq, Eq, Error)]
#[non_exhaustive]
pub enum AttestError {
#[error("challenge must be 32 bytes")]
ChallengeMustBe32Bytes,
#[error("attested key must be compressed P-256 SEC1")]
AttestedKeyMustBeCompressedP256Sec1,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct BootChallengeReceipt {
issued_challenge: [u8; 32],
issued_at_epoch: u64,
expires_at_epoch: u64,
consumed_challenge: [u8; 32],
consumed_at_epoch: u64,
issue_mac: [u8; 32],
}
impl BootChallengeReceipt {
pub fn new(
issued_challenge: &[u8],
issued_at_epoch: u64,
expires_at_epoch: u64,
consumed_challenge: &[u8],
consumed_at_epoch: u64,
issue_mac: &[u8],
) -> Result<Self, AttestError> {
Ok(Self {
issued_challenge: require_32(issued_challenge)?,
issued_at_epoch,
expires_at_epoch,
consumed_challenge: require_32(consumed_challenge)?,
consumed_at_epoch,
issue_mac: require_32(issue_mac)?,
})
}
#[must_use]
pub const fn issued_challenge(&self) -> [u8; 32] {
self.issued_challenge
}
#[must_use]
pub const fn issued_at_epoch(&self) -> u64 {
self.issued_at_epoch
}
#[must_use]
pub const fn expires_at_epoch(&self) -> u64 {
self.expires_at_epoch
}
#[must_use]
pub const fn consumed_challenge(&self) -> [u8; 32] {
self.consumed_challenge
}
#[must_use]
pub const fn consumed_at_epoch(&self) -> u64 {
self.consumed_at_epoch
}
#[must_use]
pub const fn issue_mac(&self) -> [u8; 32] {
self.issue_mac
}
}
fn exact<const N: usize>(bytes: &[u8]) -> Option<[u8; N]> {
bytes
.first_chunk::<N>()
.copied()
.filter(|_| bytes.len() == N)
}
fn require_32(bytes: &[u8]) -> Result<[u8; 32], AttestError> {
exact(bytes).ok_or(AttestError::ChallengeMustBe32Bytes)
}
fn require_compressed_point(key: &[u8]) -> Result<[u8; 33], AttestError> {
let refusal = || AttestError::AttestedKeyMustBeCompressedP256Sec1;
let point = exact::<33>(key).ok_or_else(refusal)?;
ensure(matches!(point.first(), Some(0x02 | 0x03)), refusal)?;
ensure(
p256::ecdsa::VerifyingKey::from_sec1_bytes(&point).is_ok(),
refusal,
)?;
Ok(point)
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct AppleSecureEnclaveEvidencePackage {
key_cert_der: Vec<u8>,
intermediate_certs_der: Vec<Vec<u8>>,
challenge: [u8; 32],
attested_key_compressed_sec1: [u8; 33],
challenge_receipt: BootChallengeReceipt,
challenge_pop_signature_der: Vec<u8>,
}
impl AppleSecureEnclaveEvidencePackage {
#[must_use]
pub fn key_cert_der(&self) -> &[u8] {
&self.key_cert_der
}
#[must_use]
pub fn intermediate_certs_der(&self) -> &[Vec<u8>] {
&self.intermediate_certs_der
}
#[must_use]
pub const fn challenge(&self) -> [u8; 32] {
self.challenge
}
#[must_use]
pub const fn attested_key_compressed_sec1(&self) -> [u8; 33] {
self.attested_key_compressed_sec1
}
#[must_use]
pub const fn challenge_receipt(&self) -> &BootChallengeReceipt {
&self.challenge_receipt
}
#[must_use]
pub fn challenge_pop_signature_der(&self) -> &[u8] {
&self.challenge_pop_signature_der
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct ArmTrustZoneEvidencePackage {
quote_der: Vec<u8>,
cert_chain_der: Vec<Vec<u8>>,
challenge: [u8; 32],
attested_key_compressed_sec1: [u8; 33],
challenge_receipt: BootChallengeReceipt,
}
impl ArmTrustZoneEvidencePackage {
#[must_use]
pub fn quote_der(&self) -> &[u8] {
&self.quote_der
}
#[must_use]
pub const fn challenge_receipt(&self) -> &BootChallengeReceipt {
&self.challenge_receipt
}
#[must_use]
pub const fn challenge(&self) -> [u8; 32] {
self.challenge
}
#[must_use]
pub fn cert_chain_der(&self) -> &[Vec<u8>] {
&self.cert_chain_der
}
#[must_use]
pub const fn attested_key_compressed_sec1(&self) -> [u8; 33] {
self.attested_key_compressed_sec1
}
}