Skip to content

Commit 4f497e3

Browse files
committed
Optionally log quotes to file when verifying
1 parent 28665f2 commit 4f497e3

File tree

4 files changed

+38
-6
lines changed

4 files changed

+38
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/target
22
remote-cert.crt
3+
/quotes
34

src/attestation/mod.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize};
66
use std::{
77
fmt::{self, Display, Formatter},
88
sync::Arc,
9-
time::SystemTimeError,
9+
time::{SystemTime, SystemTimeError, UNIX_EPOCH},
1010
};
1111

1212
use configfs_tsm::QuoteGenerationError;
@@ -148,6 +148,8 @@ pub struct AttestationVerifier {
148148
pub accepted_measurements: Vec<MeasurementRecord>,
149149
/// A PCCS service to use - defaults to Intel PCS
150150
pub pccs_url: Option<String>,
151+
/// Whether to log quotes to a file
152+
pub log_dcap_quote: bool,
151153
}
152154

153155
impl AttestationVerifier {
@@ -156,6 +158,7 @@ impl AttestationVerifier {
156158
Self {
157159
accepted_measurements: Vec::new(),
158160
pccs_url: None,
161+
log_dcap_quote: false,
159162
}
160163
}
161164

@@ -179,6 +182,7 @@ impl AttestationVerifier {
179182
},
180183
}],
181184
pccs_url: None,
185+
log_dcap_quote: false,
182186
}
183187
}
184188

@@ -190,6 +194,11 @@ impl AttestationVerifier {
190194
exporter: [u8; 32],
191195
) -> Result<Option<Measurements>, AttestationError> {
192196
let attestation_type = attestation_exchange_message.attestation_type;
197+
tracing::debug!("Verifing {attestation_type} attestation");
198+
199+
if self.log_dcap_quote {
200+
log_attestation(&attestation_exchange_message).await;
201+
}
193202

194203
let measurements = match attestation_type {
195204
AttestationType::DcapTdx => {
@@ -222,6 +231,7 @@ impl AttestationVerifier {
222231
.find(|a| a.attestation_type == attestation_type && a.measurements == measurements)
223232
.ok_or(AttestationError::MeasurementsNotAccepted)?;
224233

234+
tracing::debug!("Verification successful");
225235
Ok(Some(measurements))
226236
}
227237

@@ -384,6 +394,21 @@ fn get_pki_hash_from_certificate_chain(
384394
Ok(hasher.finalize().into())
385395
}
386396

397+
/// Write attestation data to a log file
398+
async fn log_attestation(attestation: &AttestationExchangeMessage) {
399+
if attestation.attestation_type != AttestationType::None {
400+
let timestamp = SystemTime::now()
401+
.duration_since(UNIX_EPOCH)
402+
.expect("Time went backwards")
403+
.as_nanos();
404+
405+
let filename = format!("quotes/{}-{}", attestation.attestation_type, timestamp);
406+
if let Err(err) = tokio::fs::write(&filename, attestation.attestation.clone()).await {
407+
tracing::warn!("Failed to write {filename}: {err}");
408+
}
409+
}
410+
}
411+
387412
/// An error when generating or verifying an attestation
388413
#[derive(Error, Debug)]
389414
pub enum AttestationError {

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,6 +1208,7 @@ mod tests {
12081208
},
12091209
}],
12101210
pccs_url: None,
1211+
log_dcap_quote: false,
12111212
};
12121213

12131214
let proxy_client_result = ProxyClient::new_with_tls_config(

src/main.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,9 @@ struct Cli {
2020
/// Log in JSON format
2121
#[arg(long, global = true)]
2222
log_json: bool,
23-
// TODO still missing
24-
// Name: "log-dcap-quote",
25-
// EnvVars: []string{"LOG_DCAP_QUOTE"},
26-
// Value: false,
27-
// Usage: "log dcap quotes to folder quotes/",
23+
/// Log DCAP quotes to folder `quotes/`
24+
#[arg(long, global = true)]
25+
log_dcap_quote: bool,
2826
}
2927

3028
#[derive(Subcommand, Debug, Clone)]
@@ -132,6 +130,10 @@ async fn main() -> anyhow::Result<()> {
132130
subscriber.pretty().init();
133131
}
134132

133+
if cli.log_dcap_quote {
134+
tokio::fs::create_dir_all("quotes").await?;
135+
}
136+
135137
match cli.command {
136138
CliCommand::Client {
137139
listen_addr,
@@ -166,6 +168,7 @@ async fn main() -> anyhow::Result<()> {
166168
Some(server_measurements) => AttestationVerifier {
167169
accepted_measurements: get_measurements_from_file(server_measurements).await?,
168170
pccs_url,
171+
log_dcap_quote: cli.log_dcap_quote,
169172
},
170173
None => AttestationVerifier::do_not_verify(),
171174
};
@@ -225,6 +228,7 @@ async fn main() -> anyhow::Result<()> {
225228
Some(client_measurements) => AttestationVerifier {
226229
accepted_measurements: get_measurements_from_file(client_measurements).await?,
227230
pccs_url,
231+
log_dcap_quote: cli.log_dcap_quote,
228232
},
229233
None => AttestationVerifier::do_not_verify(),
230234
};
@@ -254,6 +258,7 @@ async fn main() -> anyhow::Result<()> {
254258
Some(server_measurements) => AttestationVerifier {
255259
accepted_measurements: get_measurements_from_file(server_measurements).await?,
256260
pccs_url,
261+
log_dcap_quote: cli.log_dcap_quote,
257262
},
258263
None => AttestationVerifier::do_not_verify(),
259264
};

0 commit comments

Comments
 (0)