Skip to content

Commit 50dbd3f

Browse files
THS-onansasaki
authored andcommittedOct 14, 2024·
tpm: check if EK certificate has valid ASN.1 DER encoding
Further this removes padding found on some TPMs in the NV indices. If this is not valid, we still use it, but output a warning. Signed-off-by: Thore Sommer <mail@thson.de>
1 parent f670c5a commit 50dbd3f

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed
 

‎keylime/src/tpm.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::algorithms::{
77
use base64::{engine::general_purpose, Engine as _};
88
use log::*;
99
use std::convert::{TryFrom, TryInto};
10+
use std::io::Read;
1011
use std::str::FromStr;
1112
use thiserror::Error;
1213

@@ -338,6 +339,10 @@ pub enum TpmError {
338339
#[error("Error finishing Hasher")]
339340
OpenSSLHasherFinish { source: openssl::error::ErrorStack },
340341

342+
/// Error when trying to decode the EK certificate
343+
#[error("EK certificate parsing error")]
344+
EKCertParsing(#[from] picky_asn1_der::Asn1DerError),
345+
341346
/// Number conversion error
342347
#[error("Error converting number")]
343348
TryFromInt(#[from] std::num::TryFromIntError),
@@ -490,6 +495,13 @@ impl Context {
490495
})
491496
}
492497

498+
// Tries to parse the EK certificate and re-encodes it to remove potential padding
499+
fn check_ek_cert(&mut self, cert: &[u8]) -> Result<Vec<u8>> {
500+
let parsed_cert: picky_asn1_der::Asn1RawDer =
501+
picky_asn1_der::from_bytes(cert)?;
502+
Ok(picky_asn1_der::to_vec(&parsed_cert)?)
503+
}
504+
493505
/// Creates an EK, returns the key handle and public certificate
494506
/// in `EKResult`.
495507
///
@@ -551,7 +563,13 @@ impl Context {
551563
};
552564
let cert = match ek::retrieve_ek_pubcert(&mut self.inner, alg.into())
553565
{
554-
Ok(v) => Some(v),
566+
Ok(cert) => match self.check_ek_cert(&cert) {
567+
Ok(cert_checked) => Some(cert_checked),
568+
Err(_) => {
569+
warn!("EK certificate in TPM NVRAM is not ASN.1 DER encoded");
570+
Some(cert)
571+
}
572+
},
555573
Err(_) => {
556574
warn!("No EK certificate found in TPM NVRAM");
557575
None

0 commit comments

Comments
 (0)
Please sign in to comment.