use lgwks_macros::Error;
use super::super::cbor::{CborValue, decode_attestation};
use super::super::ensure;
use super::{AppleSecureEnclaveEvidencePackage, BootChallengeReceipt};
#[derive(Clone, Debug, PartialEq, Eq, Error)]
#[non_exhaustive]
pub enum AppAttestError {
#[error("attestation truncated")]
Truncated,
#[error("unsupported CBOR major type: {0}")]
UnsupportedMajorType(u8),
#[error("unsupported CBOR additional info: {0}")]
UnsupportedAdditionalInfo(u8),
#[error("CBOR integer overflow")]
IntegerOverflow(#[from] core::num::TryFromIntError),
#[error("CBOR text is not valid UTF-8")]
InvalidUtf8(#[from] std::string::FromUtf8Error),
#[error("attestation is not a map")]
NotAMap,
#[error("attestation missing key: {0}")]
MissingKey(String),
#[error("attestation unexpected type: {0}")]
UnexpectedType(String),
#[error("attestation has an empty certificate chain")]
EmptyCertChain,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct AppAttestAttestation {
x5c: Vec<Vec<u8>>,
auth_data: Vec<u8>,
receipt: Option<Vec<u8>>,
}
impl AppAttestAttestation {
pub fn parse(cbor: &[u8]) -> Result<Self, AppAttestError> {
let root = decode_attestation(cbor)?;
let missing = |key: &str| AppAttestError::MissingKey(String::from(key));
let top = root.map_value().ok_or(AppAttestError::NotAMap)?;
let att_stmt = top
.get("attStmt")
.and_then(CborValue::map_value)
.ok_or_else(|| missing("attStmt"))?;
let chain_items = att_stmt
.get("x5c")
.and_then(CborValue::array_value)
.ok_or_else(|| missing("x5c"))?;
let chain = chain_items
.iter()
.map(|item| {
item.bytes_value()
.cloned()
.ok_or_else(|| AppAttestError::UnexpectedType(String::from("x5c entry")))
})
.collect::<Result<Vec<_>, _>>()?;
ensure(!chain.is_empty(), || AppAttestError::EmptyCertChain)?;
let auth_data = top
.get("authData")
.and_then(CborValue::bytes_value)
.ok_or_else(|| missing("authData"))?;
let receipt = att_stmt
.get("receipt")
.map(|value| {
value
.bytes_value()
.cloned()
.ok_or_else(|| AppAttestError::UnexpectedType(String::from("receipt")))
})
.transpose()?;
Ok(Self {
x5c: chain,
auth_data: auth_data.clone(),
receipt,
})
}
#[must_use]
pub fn leaf_cert_der(&self) -> &[u8] {
match self.x5c.first() {
Some(leaf) => leaf,
None => &[],
}
}
#[must_use]
pub fn intermediate_certs_der(&self) -> &[Vec<u8>] {
match self.x5c.get(1..) {
Some(rest) => rest,
None => &[],
}
}
#[must_use]
pub fn chain(&self) -> &[Vec<u8>] {
&self.x5c
}
#[must_use]
pub fn auth_data(&self) -> &[u8] {
&self.auth_data
}
#[must_use]
pub const fn receipt(&self) -> Option<&Vec<u8>> {
self.receipt.as_ref()
}
}
#[derive(Clone, Debug, PartialEq, Eq, Error)]
#[non_exhaustive]
pub enum AppAttestProductionError {
#[error("App Attest unavailable")]
AppAttestUnavailable,
#[error("App Attest production path deferred to kernel #569")]
ProductionPathNotYetImplemented,
}
#[must_use]
pub const fn app_attest_is_supported() -> bool {
false
}
pub fn produce_app_attest_evidence(
_receipt: &BootChallengeReceipt,
) -> Result<AppleSecureEnclaveEvidencePackage, AppAttestProductionError> {
ensure(app_attest_is_supported(), || {
AppAttestProductionError::AppAttestUnavailable
})?;
Err(AppAttestProductionError::ProductionPathNotYetImplemented)
}