Skip to content

Commit

Permalink
ita: added processing event logs in SGX & TDX context
Browse files Browse the repository at this point in the history
Added processing base64 encoded CC event logs from tee evidence and map them to ITA json payload format

Signed-off-by: Pawel Proskurnicki <[email protected]>
  • Loading branch information
pawelpros committed Jan 17, 2025
1 parent 1b3b41c commit 94c86f0
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 3 deletions.
18 changes: 17 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions kbs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ clap = { workspace = true, features = ["derive", "env"] }
config.workspace = true
cryptoki = { version = "0.7.0", optional = true }
env_logger.workspace = true
#eventlog-rs = "0.1.3" # NOT YET RELEASED
eventlog-rs = { git = "https://github.com/pawelpros/eventlog-rs.git", rev = "0c74ee6a732a69811929219485c9747bb1ee0c59" }
hex = "0.4.3"
jsonwebtoken = { workspace = true, default-features = false }
jwt-simple.workspace = true
kbs-types.workspace = true
Expand Down
108 changes: 108 additions & 0 deletions kbs/src/attestation/intel_trust_authority/ita_event_log_parser.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
use anyhow::Context;
use anyhow::Result;
use base64::{engine::general_purpose::STANDARD, Engine};
use eventlog_rs::Eventlog;
use eventlog_rs::EventlogEntry;
use serde::Serialize;
use std::collections::HashMap;

#[derive(Serialize, Debug)]
pub struct Rtmr {
index: u32,
bank: String,
}

#[derive(Serialize, Debug)]
pub struct RtmrEvent {
type_id: String,
type_name: String,
#[serde(skip_serializing_if = "Vec::is_empty")]
tags: Vec<String>,
measurement: String,
}

#[derive(Serialize, Debug)]
pub struct RtmrLog {
rtmr: Rtmr,
rtmr_events: Vec<RtmrEvent>,
}

#[derive(Clone, Hash, Eq, PartialEq)]
pub struct MrIndex {
pub index: u32,
pub sha: String,
}

pub struct CcEventLogParser;

impl CcEventLogParser {
pub fn handle(encoded_event_log: String) -> Result<String> {
let decoded_event_log = STANDARD
.decode(encoded_event_log)
.context("Failed to decode base64 encoded event log")?;

let event_log = Eventlog::try_from(decoded_event_log)
.context("Failed to decode event log from given data.")?;

let json = serde_json::to_string(&Self::parse(&event_log.log))
.context("Failed to serialize parsed event log in ITA format.")?;

Ok(STANDARD.encode(json))
}

pub fn parse(data: &Vec<EventlogEntry>) -> Vec<RtmrLog> {
let mut event_logs_by_mr_index: HashMap<MrIndex, Vec<EventlogEntry>> = HashMap::new();

for log_entry in data.iter() {
let index = MrIndex {
index: log_entry.target_measurement_registry,
sha: log_entry.digests[0]
.algorithm
.clone()
.replace("TPM_ALG_", ""),
};
match event_logs_by_mr_index.get_mut(&index) {
Some(logs) => logs.push(log_entry.clone()),
None => {
event_logs_by_mr_index.insert(index, vec![log_entry.clone()]);
}
}
}

let mut out_logs = Vec::new();

for (mr_index, log_set) in event_logs_by_mr_index.iter() {
let index = mr_index.index;
let bank = mr_index.sha.clone();

let mut events = Vec::new();

for event_entry in log_set.clone() {
let type_name = event_entry.event_type;
let mut tags = Vec::new();

if let Ok(valid_utf8) = String::from_utf8(event_entry.event_desc.clone()) {
if !valid_utf8.contains('\u{0000}') {
tags.push(valid_utf8);
}
}

let type_id = format!("0x{:08X}", event_entry.event_type_id);
let measurement = hex::encode(event_entry.digests[0].digest.clone());
events.push(RtmrEvent {
type_id,
type_name,
tags,
measurement,
});
}

out_logs.push(RtmrLog {
rtmr: Rtmr { index, bank },
rtmr_events: events,
});
}

out_logs
}
}
14 changes: 12 additions & 2 deletions kbs/src/attestation/intel_trust_authority/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0

mod ita_event_log_parser;

use crate::{
attestation::backend::{generic_generate_challenge, make_nonce, Attest},
attestation::intel_trust_authority::ita_event_log_parser::CcEventLogParser,
token::{jwk::JwkAttestationTokenVerifier, AttestationTokenVerifierConfig},
};
use anyhow::*;
Expand Down Expand Up @@ -44,8 +47,8 @@ pub enum HashAlgorithm {

#[derive(Deserialize, Debug)]
struct ItaTeeEvidence {
#[serde(skip)]
_cc_eventlog: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
cc_eventlog: Option<String>,
quote: String,
}

Expand All @@ -63,6 +66,8 @@ struct AttestReqData {
user_data: Option<String>,
policy_ids: Vec<String>,
policy_must_match: bool,
#[serde(skip_serializing_if = "Option::is_none")]
event_log: Option<String>,
}

#[derive(Deserialize, Debug)]
Expand Down Expand Up @@ -127,6 +132,7 @@ impl Attest for IntelTrustAuthority {
user_data: Some(STANDARD.encode(runtime_data)),
policy_ids,
policy_must_match,
event_log: None,
};

(req_data, att_url)
Expand All @@ -143,6 +149,10 @@ impl Attest for IntelTrustAuthority {
user_data: None,
policy_ids,
policy_must_match,
event_log: evidence
.cc_eventlog
.map(CcEventLogParser::handle)
.transpose()?,
};

(req_data, att_url)
Expand Down

0 comments on commit 94c86f0

Please sign in to comment.