forked from confidential-containers/trustee
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmod.rs
200 lines (170 loc) · 6.42 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright (c) Microsoft Corporation.
//
// SPDX-License-Identifier: Apache-2.0
//
use super::az_snp_vtpm::{extend_claim, verify_init_data};
use super::tdx::claims::generate_parsed_claim;
use super::tdx::quote::{ecdsa_quote_verification, parse_tdx_quote, Quote as TdQuote};
use super::{TeeEvidenceParsedClaim, Verifier};
use crate::{InitDataHash, ReportData};
use anyhow::{bail, Context, Result};
use async_trait::async_trait;
use az_tdx_vtpm::hcl::HclReport;
use az_tdx_vtpm::vtpm::Quote as TpmQuote;
use log::debug;
use openssl::pkey::PKey;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
struct Evidence {
tpm_quote: TpmQuote,
hcl_report: Vec<u8>,
td_quote: Vec<u8>,
}
#[derive(Default)]
pub struct AzTdxVtpm;
#[async_trait]
impl Verifier for AzTdxVtpm {
/// The following verification steps are performed:
/// 1. TPM Quote has been signed by AK included in the HCL variable data
/// 2. Attestation nonce matches TPM Quote nonce
/// 3. TPM PCRs' digest matches the digest in the Quote
/// 4. TD Quote is genuine
/// 5. TD Report's report_data field matches hashed HCL variable data
/// 6. Init data hash matches TPM PCR[INITDATA_PCR]
async fn evaluate(
&self,
evidence: &[u8],
expected_report_data: &ReportData,
expected_init_data_hash: &InitDataHash,
) -> Result<TeeEvidenceParsedClaim> {
let ReportData::Value(expected_report_data) = expected_report_data else {
bail!("unexpected empty report data");
};
let evidence = serde_json::from_slice::<Evidence>(evidence)
.context("Failed to deserialize Azure vTPM TDX evidence")?;
let hcl_report = HclReport::new(evidence.hcl_report)?;
verify_tpm_signature(&evidence.tpm_quote, &hcl_report)?;
verify_tpm_nonce(&evidence.tpm_quote, expected_report_data)?;
verify_pcrs(&evidence.tpm_quote)?;
ecdsa_quote_verification(&evidence.td_quote).await?;
let td_quote = parse_tdx_quote(&evidence.td_quote)?;
verify_hcl_var_data(&hcl_report, &td_quote)?;
let pcrs: Vec<&[u8; 32]> = evidence.tpm_quote.pcrs_sha256().collect();
verify_init_data(expected_init_data_hash, &pcrs)?;
let mut claim = generate_parsed_claim(td_quote, None, None)?;
extend_claim(&mut claim, &evidence.tpm_quote)?;
Ok(claim)
}
}
fn verify_hcl_var_data(hcl_report: &HclReport, td_quote: &TdQuote) -> Result<()> {
let var_data_hash = hcl_report.var_data_sha256();
if var_data_hash != td_quote.report_data()[..32] {
bail!("TDX Quote report data mismatch");
}
debug!("Report data verification completed successfully.");
Ok(())
}
fn verify_tpm_signature(quote: &TpmQuote, hcl_report: &HclReport) -> Result<()> {
let ak_pub = hcl_report.ak_pub().context("Failed to get AKpub")?;
let der = ak_pub.key.try_to_der()?;
let ak_pub = PKey::public_key_from_der(&der).context("Failed to parse AKpub")?;
quote
.verify_signature(&ak_pub)
.context("Failed to verify vTPM quote")?;
Ok(())
}
fn verify_pcrs(quote: &TpmQuote) -> Result<()> {
quote
.verify_pcrs()
.context("Digest of PCRs does not match digest in Quote")?;
debug!("PCR verification completed successfully");
Ok(())
}
fn verify_tpm_nonce(quote: &TpmQuote, report_data: &[u8]) -> Result<()> {
let nonce = quote.nonce()?;
if nonce != report_data[..] {
bail!("TPM quote nonce doesn't match expected report_data");
}
debug!("TPM report_data verification completed successfully");
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use az_tdx_vtpm::vtpm::Quote;
use az_tdx_vtpm::vtpm::VerifyError;
const REPORT: &[u8; 2600] = include_bytes!("../../test_data/az-tdx-vtpm/hcl-report.bin");
const QUOTE: &[u8; 1170] = include_bytes!("../../test_data/az-tdx-vtpm/quote.bin");
const TD_QUOTE: &[u8; 5006] = include_bytes!("../../test_data/az-tdx-vtpm/td-quote.bin");
#[test]
fn test_verify_hcl_var_data() {
let hcl_report = HclReport::new(REPORT.to_vec()).unwrap();
let td_quote = parse_tdx_quote(TD_QUOTE).unwrap();
verify_hcl_var_data(&hcl_report, &td_quote).unwrap();
}
#[test]
fn test_verify_hcl_var_data_failure() {
let mut wrong_report = REPORT.clone();
wrong_report[0x0880] += 1;
let hcl_report = HclReport::new(wrong_report.to_vec()).unwrap();
let td_quote = parse_tdx_quote(TD_QUOTE).unwrap();
assert_eq!(
verify_hcl_var_data(&hcl_report, &td_quote)
.unwrap_err()
.to_string(),
"TDX Quote report data mismatch"
);
}
#[test]
fn test_verify_tpm_signature() {
let quote: Quote = bincode::deserialize(QUOTE).unwrap();
let hcl_report = HclReport::new(REPORT.to_vec()).unwrap();
verify_tpm_signature("e, &hcl_report).unwrap();
}
#[test]
fn test_verify_tpm_signature_failure() {
let mut quote = QUOTE.clone();
quote[0x020] = 0;
let wrong_quote: Quote = bincode::deserialize("e).unwrap();
let hcl_report = HclReport::new(REPORT.to_vec()).unwrap();
assert_eq!(
verify_tpm_signature(&wrong_quote, &hcl_report)
.unwrap_err()
.downcast_ref::<VerifyError>()
.unwrap()
.to_string(),
VerifyError::SignatureMismatch.to_string()
);
}
#[test]
fn test_verify_tpm_nonce() {
let quote: Quote = bincode::deserialize(QUOTE).unwrap();
let nonce = "challenge".as_bytes();
verify_tpm_nonce("e, nonce).unwrap();
}
#[test]
fn test_verify_tpm_nonce_failure() {
let quote: Quote = bincode::deserialize(QUOTE).unwrap();
let wrong_nonce = "wrong".as_bytes();
verify_tpm_nonce("e, wrong_nonce).unwrap_err();
}
#[test]
fn test_verify_pcrs() {
let quote: Quote = bincode::deserialize(QUOTE).unwrap();
verify_pcrs("e).unwrap();
}
#[test]
fn test_verify_pcrs_failure() {
let mut quote = QUOTE.clone();
quote[0x0169] = 0;
let wrong_quote: Quote = bincode::deserialize("e).unwrap();
assert_eq!(
verify_pcrs(&wrong_quote)
.unwrap_err()
.downcast_ref::<VerifyError>()
.unwrap()
.to_string(),
VerifyError::PcrMismatch.to_string()
);
}
}