pub mod cd;
pub mod chain;
pub mod factory;
pub mod nocsr;
pub mod x509;
use crate::crypto::{
HASH_LEN_BYTES, KeyHandle, KeyStore, PublicKey, Signature, SymmetricKey, verify,
};
use crate::error::{Error, ErrorCode, Result, bail};
use crate::tlv::{ContainerKind, Tag, TlvReader, TlvWriter, Value, set_once};
pub use cd::{CertificationElements, CertificationType, SignedData};
pub use chain::{DacChain, check_declaration_against_chain, verify_dac_chain};
pub use nocsr::{Csr, NocsrElements, build_csr, sign_nocsr, verify_nocsr};
pub use x509::{VidPid, X509Certificate};
pub const ATTESTATION_NONCE_LEN: usize = 32;
pub const CSR_NONCE_LEN: usize = 32;
pub const MAX_ATTESTATION_ELEMENTS: usize = cd::MAX_CD_LEN + 512;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AttestationElements<'a> {
pub certification_declaration: &'a [u8],
pub attestation_nonce: [u8; ATTESTATION_NONCE_LEN],
pub timestamp: u32,
pub firmware_information: Option<&'a [u8]>,
}
impl<'a> AttestationElements<'a> {
pub fn encode<'b>(&self, buf: &'b mut [u8]) -> Result<&'b [u8]> {
let mut w = TlvWriter::new(buf);
w.start_structure(Tag::Anonymous)?;
w.octets(Tag::Context(1), self.certification_declaration)?;
w.octets(Tag::Context(2), &self.attestation_nonce)?;
w.unsigned(Tag::Context(3), u64::from(self.timestamp))?;
if let Some(firmware) = self.firmware_information {
w.octets(Tag::Context(4), firmware)?;
}
w.end_container()?;
w.finish()
}
pub fn decode(buf: &'a [u8]) -> Result<Self> {
let mut reader = TlvReader::new(buf);
let Some(head) = reader.next_element()? else {
bail!(TlvTruncated)
};
if head.value.container() != Some(ContainerKind::Structure) || !head.tag.is_anonymous() {
bail!(TlvWrongType)
}
let mut certification_declaration = None;
let mut attestation_nonce = None;
let mut timestamp = None;
let mut firmware_information = None;
loop {
let Some(element) = reader.next_element()? else {
bail!(TlvTruncated)
};
if element.value == Value::EndOfContainer {
break;
}
match element.tag.context() {
Some(1) => set_once(&mut certification_declaration, element.octets()?)?,
Some(2) => {
let value = <[u8; ATTESTATION_NONCE_LEN]>::try_from(element.octets()?)
.map_err(|_| Error::new(ErrorCode::TlvOutOfRange))?;
set_once(&mut attestation_nonce, value)?;
}
Some(3) => {
let value = u32::try_from(element.unsigned()?)
.map_err(|_| Error::new(ErrorCode::TlvOutOfRange))?;
set_once(&mut timestamp, value)?;
}
Some(4) => set_once(&mut firmware_information, element.octets()?)?,
_ => reader.skip_value(&element)?,
}
}
reader.finish()?;
Ok(Self {
certification_declaration: certification_declaration
.ok_or(Error::new(ErrorCode::TlvNotFound))?,
attestation_nonce: attestation_nonce.ok_or(Error::new(ErrorCode::TlvNotFound))?,
timestamp: timestamp.ok_or(Error::new(ErrorCode::TlvNotFound))?,
firmware_information,
})
}
}
pub fn sign_attestation<K: KeyStore>(
keys: &K,
dac_key: KeyHandle,
elements: &[u8],
challenge: &SymmetricKey,
) -> Result<Signature> {
with_tbs(elements, challenge, |tbs| keys.sign(dac_key, tbs))
}
pub fn verify_attestation(
dac_public_key: &PublicKey,
elements: &[u8],
challenge: &SymmetricKey,
signature: &Signature,
) -> Result<bool> {
with_tbs(elements, challenge, |tbs| {
verify(dac_public_key, tbs, signature)
})
}
pub fn attestation_tbs_hash(
elements: &[u8],
challenge: &SymmetricKey,
) -> Result<[u8; HASH_LEN_BYTES]> {
with_tbs(elements, challenge, |tbs| Ok(crate::crypto::hash(tbs)))
}
pub(crate) fn with_tbs<T>(
elements: &[u8],
challenge: &SymmetricKey,
f: impl FnOnce(&[u8]) -> Result<T>,
) -> Result<T> {
let key = challenge.as_bytes();
let total = elements
.len()
.checked_add(key.len())
.ok_or(Error::new(ErrorCode::BufferTooSmall))?;
let mut buf = [0u8; MAX_ATTESTATION_ELEMENTS + crate::crypto::SYMMETRIC_KEY_LENGTH_BYTES];
let Some(slot) = buf.get_mut(..total) else {
bail!(BufferTooSmall)
};
let (head, tail) = slot.split_at_mut(elements.len());
head.copy_from_slice(elements);
tail.copy_from_slice(key);
f(slot)
}