fix(attestation): reject oversized challenges and rewrite cert DER encoding

DuckDetector flagged two issues:
1. Oversized challenge accepted — 256-byte attestation challenge should
   return INVALID_INPUT_LENGTH (-21) like real KeyMint. Added early check
   in handleGenerateKey before any path decision.
2. Issuer/subject chain mismatch — rcgen's HashMap loses DN attribute
   ordering and converts PrintableString to UTF8String, producing
   different DER bytes. Replaced rcgen with manual DER assembly that
   injects raw keybox issuer_dn_der bytes directly.

Verified on device: TX_ID 315 rejects 256-byte challenge, TX_ID 501
generates valid 4-cert chain with correct issuer linkage.
This commit is contained in:
Enginex0
2026-03-09 21:52:06 +01:00
parent c11465d660
commit ba0628c687
6 changed files with 1649 additions and 187 deletions
-7
View File
@@ -11,7 +11,6 @@ pub enum CertGenError {
KeyboxParseFailed(String),
AttestationBuildFailed(String),
DerError(der::Error),
RcgenError(rcgen::Error),
EmptyKeyboxChain,
ChallengeTooLong(usize),
InvalidParameter(String),
@@ -31,7 +30,6 @@ impl fmt::Display for CertGenError {
Self::KeyboxParseFailed(msg) => write!(f, "keybox parse failed: {}", msg),
Self::AttestationBuildFailed(msg) => write!(f, "attestation build failed: {}", msg),
Self::DerError(e) => write!(f, "DER error: {}", e),
Self::RcgenError(e) => write!(f, "rcgen error: {}", e),
Self::EmptyKeyboxChain => write!(f, "keybox certificate chain is empty"),
Self::ChallengeTooLong(len) => write!(f, "attestation challenge too long: {} bytes (max 128)", len),
Self::InvalidParameter(msg) => write!(f, "invalid parameter: {}", msg),
@@ -73,10 +71,5 @@ impl From<rsa::Error> for CertGenError {
}
}
impl From<rcgen::Error> for CertGenError {
fn from(e: rcgen::Error) -> Self {
Self::RcgenError(e)
}
}
pub type Result<T> = std::result::Result<T, CertGenError>;