Skip to content

Commit 54ab01c

Browse files
committed
fix functions and docs
Signed-off-by: Jun Kimura <jun.kimura@datachain.jp>
1 parent e6fd9cf commit 54ab01c

File tree

9 files changed

+64
-62
lines changed

9 files changed

+64
-62
lines changed

crates/quote-verifier/src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ mod quote_verifier_tests {
2424
use crate::cert::verify_crl_signature;
2525
use crate::collaterals::IntelCollateral;
2626
use crate::crypto::keccak256sum;
27-
use crate::quotes::{version_3::verify_quote_dcapv3, version_4::verify_quote_dcapv4};
27+
use crate::quotes::{version_3::verify_quote_v3, version_4::verify_quote_v4};
2828
use crate::tcbinfo::validate_tcbinfov3;
29-
use crate::verifier::VerifiedOutput;
29+
use crate::verifier::QuoteVerificationOutput;
3030
use dcap_types::quotes::{version_3::QuoteV3, version_4::QuoteV4};
3131
use dcap_types::tcbinfo::TcbInfoV3;
3232
use dcap_types::utils::{parse_crl_der, parse_pem, parse_x509_der, pem_to_der};
@@ -96,7 +96,7 @@ mod quote_verifier_tests {
9696
let res = QuoteV3::from_bytes(&dcap_quote_bytes);
9797
assert!(res.is_ok(), "failed to parse quotev3: {:?}", res.err());
9898
let dcap_quote = res.unwrap();
99-
let res = verify_quote_dcapv3(&dcap_quote, &collaterals, 1737458686);
99+
let res = verify_quote_v3(&dcap_quote, &collaterals, 1737458686);
100100
assert!(res.is_ok(), "verification failed: {:?}", res.err());
101101
let verified_output = res.unwrap();
102102
assert_eq!(verified_output.quote_version, 3);
@@ -124,7 +124,7 @@ mod quote_verifier_tests {
124124
"invalid `not_after_min`"
125125
);
126126
let bz = verified_output.to_bytes();
127-
let res = VerifiedOutput::from_bytes(&bz);
127+
let res = QuoteVerificationOutput::from_bytes(&bz);
128128
assert!(
129129
res.is_ok(),
130130
"failed to parse verified output: {:?}",
@@ -154,12 +154,12 @@ mod quote_verifier_tests {
154154
assert!(res.is_ok(), "failed to parse quotev4: {:?}", res.err());
155155
let dcap_quote = res.unwrap();
156156

157-
let res = verify_quote_dcapv4(&dcap_quote, &collaterals, 1737467060);
157+
let res = verify_quote_v4(&dcap_quote, &collaterals, 1737467060);
158158
assert!(res.is_ok(), "verification failed: {:?}", res.err());
159159
let verified_output = res.unwrap();
160160
assert_eq!(verified_output.tcb_status, Status::TcbOutOfDate);
161161
let bz = verified_output.to_bytes();
162-
let res = VerifiedOutput::from_bytes(&bz);
162+
let res = QuoteVerificationOutput::from_bytes(&bz);
163163
assert!(
164164
res.is_ok(),
165165
"failed to parse verified output: {:?}",

crates/quote-verifier/src/quotes/mod.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ use x509_parser::certificate::X509Certificate;
2929

3030
/// The TCB info of the QE
3131
#[derive(Debug, Clone, PartialEq, Eq)]
32-
pub struct QETCB {
32+
pub struct QeTcb {
3333
pub tcb_evaluation_data_number: u32,
3434
pub tcb_status: EnclaveIdentityV2TcbStatus,
3535
pub advisory_ids: Vec<String>,
3636
}
3737

38-
/// common_verify_and_fetch_tcb is a common function that verifies the quote and fetches the TCB info
38+
/// Verify the quote and return the TCB info of the QE, SGX extensions from the PCK leaf certificate, TCB info of the platform, and the validity intersection of all collaterals
3939
///
4040
/// # Arguments
4141
///
@@ -53,12 +53,12 @@ pub struct QETCB {
5353
/// # Returns
5454
///
5555
/// * A tuple containing:
56-
/// * The TCB status of the QE
56+
/// * The TCB info of the QE
5757
/// * The SGX extensions from the PCK leaf certificate
58-
/// * The TCB info
58+
/// * The TCB info of the platform
5959
/// * The validity intersection of all collaterals
6060
#[allow(clippy::too_many_arguments)]
61-
fn common_verify_and_fetch_tcb(
61+
fn verify_quote_common(
6262
quote_header: &QuoteHeader,
6363
quote_body: &QuoteBody,
6464
ecdsa_attestation_signature: &[u8; 64],
@@ -69,7 +69,7 @@ fn common_verify_and_fetch_tcb(
6969
qe_cert_data: &CertData,
7070
collaterals: &IntelCollateral,
7171
current_time: u64,
72-
) -> Result<(QETCB, SgxExtensions, TcbInfo, ValidityIntersection)> {
72+
) -> Result<(QeTcb, SgxExtensions, TcbInfo, ValidityIntersection)> {
7373
// get the certchain embedded in the ecda quote signature data
7474
// this can be one of 5 types, and we only support type 5
7575
// https://github.com/intel/SGXDataCenterAttestationPrimitives/blob/aa239d25a437a28f3f4de92c38f5b6809faac842/QuoteGeneration/quote_wrapper/common/inc/sgx_quote_3.h#L63C4-L63C112
@@ -163,6 +163,12 @@ fn common_verify_and_fetch_tcb(
163163

164164
if !validity.validate() {
165165
bail!("Validity intersection provided from collaterals is invalid");
166+
} else if !validity.validate_time(current_time) {
167+
bail!(
168+
"certificates are expired: validity={} current_time={}",
169+
validity,
170+
current_time
171+
);
166172
}
167173

168174
Ok((qe_tcb, pck_cert_sgx_extensions, tcb_info, validity))
@@ -194,7 +200,7 @@ fn verify_qe_report(
194200
qeidentityv2: &EnclaveIdentityV2,
195201
pck_leaf_cert: &X509Certificate,
196202
qe_report_signature: &[u8; 64],
197-
) -> Result<QETCB> {
203+
) -> Result<QeTcb> {
198204
// validate QEReport then get TCB Status
199205
if !validate_qe_report_data(
200206
&qe_report.report_data,
@@ -219,7 +225,7 @@ fn verify_qe_report(
219225
let (tcb_status, advisory_ids) =
220226
get_qe_tcbstatus(qe_report.isv_svn, &qeidentityv2.enclave_identity.tcb_levels)?;
221227

222-
Ok(QETCB {
228+
Ok(QeTcb {
223229
tcb_evaluation_data_number: qeidentityv2.enclave_identity.tcb_evaluation_data_number,
224230
tcb_status,
225231
advisory_ids,
@@ -234,8 +240,7 @@ fn verify_quote_attestation(
234240
ecdsa_attestation_signature: &[u8; 64],
235241
) -> Result<()> {
236242
// verify the signature for attestation body
237-
let mut data = Vec::new();
238-
data.extend_from_slice(&quote_header.to_bytes());
243+
let mut data = quote_header.to_bytes().to_vec();
239244
match quote_body {
240245
QuoteBody::SGXQuoteBody(body) => data.extend_from_slice(&body.to_bytes()),
241246
QuoteBody::TD10QuoteBody(body) => data.extend_from_slice(&body.to_bytes()),

crates/quote-verifier/src/quotes/version_3.rs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
1-
use super::{
2-
check_quote_header, common_verify_and_fetch_tcb, converge_tcb_status_with_qe_tcb, Result,
3-
};
1+
use super::{check_quote_header, converge_tcb_status_with_qe_tcb, verify_quote_common, Result};
42
use crate::{
53
cert::{get_sgx_tdx_fmspc_tcbstatus_v3, merge_advisory_ids},
64
collaterals::IntelCollateral,
75
crypto::keccak256sum,
8-
verifier::VerifiedOutput,
6+
verifier::QuoteVerificationOutput,
97
VERIFIER_VERSION,
108
};
11-
use anyhow::{bail, Context};
9+
use anyhow::Context;
1210
use core::cmp::min;
1311
use dcap_types::{
1412
quotes::{body::QuoteBody, version_3::QuoteV3},
1513
tcbinfo::TcbInfo,
1614
};
1715

18-
pub fn verify_quote_dcapv3(
16+
/// Verify the given DCAP quote v3 and return the verification output.
17+
///
18+
/// # Arguments
19+
/// - `quote`: The quote to be verified
20+
/// - `collateral`: The collateral data to be used for verification
21+
/// - `current_time`: The current time in seconds since the Unix epoch
22+
pub fn verify_quote_v3(
1923
quote: &QuoteV3,
20-
collaterals: &IntelCollateral,
24+
collateral: &IntelCollateral,
2125
current_time: u64,
22-
) -> Result<VerifiedOutput> {
26+
) -> Result<QuoteVerificationOutput> {
2327
check_quote_header(&quote.header, 3).context("invalid quote header")?;
2428

2529
let quote_body = QuoteBody::SGXQuoteBody(quote.isv_enclave_report);
26-
let (qe_status, sgx_extensions, tcb_info, validity) = common_verify_and_fetch_tcb(
30+
let (qe_status, sgx_extensions, tcb_info, validity) = verify_quote_common(
2731
&quote.header,
2832
&quote_body,
2933
&quote.signature.isv_enclave_report_signature,
@@ -32,21 +36,14 @@ pub fn verify_quote_dcapv3(
3236
&quote.signature.qe_report_signature,
3337
&quote.signature.qe_auth_data.data,
3438
&quote.signature.qe_cert_data,
35-
collaterals,
39+
collateral,
3640
current_time,
3741
)?;
38-
if !validity.validate_time(current_time) {
39-
bail!(
40-
"certificates are expired: validity={} current_time={}",
41-
validity,
42-
current_time
43-
);
44-
}
4542
let TcbInfo::V3(tcb_info_v3) = tcb_info;
4643
let (tcb_status, _, tcb_advisory_ids) =
4744
get_sgx_tdx_fmspc_tcbstatus_v3(quote.header.tee_type, None, &sgx_extensions, &tcb_info_v3)?;
4845

49-
Ok(VerifiedOutput {
46+
Ok(QuoteVerificationOutput {
5047
version: VERIFIER_VERSION,
5148
quote_version: quote.header.version,
5249
tee_type: quote.header.tee_type,
@@ -56,7 +53,7 @@ pub fn verify_quote_dcapv3(
5653
tcb_info_v3.tcb_info.tcb_evaluation_data_number,
5754
),
5855
fmspc: sgx_extensions.fmspc,
59-
sgx_intel_root_ca_hash: keccak256sum(collaterals.sgx_intel_root_ca_der.as_ref()),
56+
sgx_intel_root_ca_hash: keccak256sum(collateral.sgx_intel_root_ca_der.as_ref()),
6057
validity,
6158
quote_body,
6259
advisory_ids: merge_advisory_ids(tcb_advisory_ids, qe_status.advisory_ids),
@@ -176,7 +173,7 @@ mod tests {
176173
};
177174

178175
let current_time = 1730000000;
179-
let res = verify_quote_dcapv3(&quote, &collateral, current_time);
176+
let res = verify_quote_v3(&quote, &collateral, current_time);
180177
assert!(res.is_ok(), "{:?}", res);
181178
let output = res.unwrap();
182179
assert_eq!(output.min_tcb_evaluation_data_number, 1);

crates/quote-verifier/src/quotes/version_4.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
use super::{
2-
check_quote_header, common_verify_and_fetch_tcb, converge_tcb_status_with_qe_tcb, Result,
3-
};
1+
use super::{check_quote_header, converge_tcb_status_with_qe_tcb, verify_quote_common, Result};
42
use crate::{
53
cert::{get_sgx_tdx_fmspc_tcbstatus_v3, merge_advisory_ids},
64
collaterals::IntelCollateral,
75
crypto::sha256sum,
86
tdx_module::{converge_tcb_status_with_tdx_module_tcb, get_tdx_module_identity_and_tcb},
9-
verifier::VerifiedOutput,
7+
verifier::QuoteVerificationOutput,
108
VERIFIER_VERSION,
119
};
1210
use anyhow::{bail, Context};
@@ -17,11 +15,17 @@ use dcap_types::{
1715
TcbInfoV3TcbStatus, TdxModuleTcbValidationStatus, SGX_TEE_TYPE,
1816
};
1917

20-
pub fn verify_quote_dcapv4(
18+
/// Verify the given DCAP quote v4 and return the verification output.
19+
///
20+
/// # Arguments
21+
/// - `quote`: The quote to be verified
22+
/// - `collaterals`: The collateral data to be used for verification
23+
/// - `current_time`: The current time in seconds since the Unix epoch
24+
pub fn verify_quote_v4(
2125
quote: &QuoteV4,
2226
collaterals: &IntelCollateral,
2327
current_time: u64,
24-
) -> Result<VerifiedOutput> {
28+
) -> Result<QuoteVerificationOutput> {
2529
check_quote_header(&quote.header, 4).context("invalid quote header")?;
2630

2731
// we'll now proceed to verify the qe
@@ -39,7 +43,7 @@ pub fn verify_quote_dcapv4(
3943
);
4044
};
4145

42-
let (qe_tcb, sgx_extensions, tcb_info, validity) = common_verify_and_fetch_tcb(
46+
let (qe_tcb, sgx_extensions, tcb_info, validity) = verify_quote_common(
4347
&quote.header,
4448
&quote.quote_body,
4549
&quote.signature.quote_signature,
@@ -52,13 +56,6 @@ pub fn verify_quote_dcapv4(
5256
current_time,
5357
)?;
5458

55-
if !validity.validate_time(current_time) {
56-
bail!(
57-
"certificates are expired: validity={} current_time={}",
58-
validity,
59-
current_time
60-
);
61-
}
6259
let TcbInfo::V3(tcb_info_v3) = tcb_info;
6360
let (quote_tdx_body, tee_tcb_svn) = if let QuoteBody::TD10QuoteBody(body) = &quote.quote_body {
6461
(Some(body), body.tee_tcb_svn)
@@ -112,7 +109,7 @@ pub fn verify_quote_dcapv4(
112109
advisory_ids = merge_advisory_ids(advisory_ids, tdx_module_advisory_ids);
113110
}
114111

115-
Ok(VerifiedOutput {
112+
Ok(QuoteVerificationOutput {
116113
version: VERIFIER_VERSION,
117114
quote_version: quote.header.version,
118115
tee_type: quote.header.tee_type,

crates/quote-verifier/src/verifier.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ impl TryFrom<&Validity> for ValidityIntersection {
101101
}
102102
}
103103

104-
/// VerifiedOutput is the output of the dcap quote verifier.
104+
/// QuoteVerificationOutput is the output of the quote verification process.
105105
#[derive(Debug, Clone, PartialEq, Eq)]
106-
pub struct VerifiedOutput {
106+
pub struct QuoteVerificationOutput {
107107
/// verifier version
108108
/// length: 2 bytes
109109
pub version: u16,
@@ -136,11 +136,13 @@ pub struct VerifiedOutput {
136136
pub advisory_ids: Vec<String>,
137137
}
138138

139-
impl VerifiedOutput {
139+
impl QuoteVerificationOutput {
140+
/// Calculate the hash of the verification output.
140141
pub fn hash(&self) -> [u8; 32] {
141142
keccak256sum(&self.to_bytes())
142143
}
143144

145+
/// Serialize the verification output to bytes.
144146
pub fn to_bytes(&self) -> Vec<u8> {
145147
let mut output_vec = Vec::new();
146148

@@ -168,7 +170,8 @@ impl VerifiedOutput {
168170
output_vec
169171
}
170172

171-
pub fn from_bytes(slice: &[u8]) -> Result<VerifiedOutput> {
173+
/// Deserialize the verification output from bytes.
174+
pub fn from_bytes(slice: &[u8]) -> Result<QuoteVerificationOutput> {
172175
let mut version = [0; 2];
173176
version.copy_from_slice(&slice[0..2]);
174177
let version = u16::from_be_bytes(version);
@@ -215,7 +218,7 @@ impl VerifiedOutput {
215218

216219
let advisory_ids = <Vec<String>>::abi_decode(&slice[advisory_ids_offset..], true)?;
217220

218-
Ok(VerifiedOutput {
221+
Ok(QuoteVerificationOutput {
219222
version,
220223
quote_version: u16::from_be_bytes(quote_version),
221224
tee_type: u32::from_be_bytes(tee_type),
1.18 KB
Binary file not shown.

zkvm/risc0/guest/src/bin/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use dcap_quote_verifier::collaterals::IntelCollateral;
22
use dcap_quote_verifier::types::quotes::version_3::QuoteV3;
3-
use dcap_quote_verifier::quotes::version_3::verify_quote_dcapv3;
3+
use dcap_quote_verifier::quotes::version_3::verify_quote_v3;
44
use risc0_zkvm::guest::env;
55

66
fn main() {
@@ -10,7 +10,7 @@ fn main() {
1010
let collaterals = IntelCollateral::from_bytes(&collaterals).unwrap();
1111

1212
env::commit_slice(
13-
verify_quote_dcapv3(&quote, &collaterals, current_time)
13+
verify_quote_v3(&quote, &collaterals, current_time)
1414
.unwrap()
1515
.to_bytes()
1616
.as_slice(),

zkvm/risc0/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub use methods::*;
66
#[cfg(test)]
77
mod tests {
88
use super::*;
9-
use dcap_quote_verifier::verifier::VerifiedOutput;
9+
use dcap_quote_verifier::verifier::QuoteVerificationOutput;
1010
use risc0_zkvm::{ExecutorEnv, LocalProver, Prover, ProverOpts, VerifierContext};
1111

1212
#[test]
@@ -33,7 +33,7 @@ mod tests {
3333
let res = res.unwrap();
3434
println!("proving stats: {:?}", res.stats);
3535

36-
let res = VerifiedOutput::from_bytes(&res.receipt.journal.bytes);
36+
let res = QuoteVerificationOutput::from_bytes(&res.receipt.journal.bytes);
3737
assert!(res.is_ok(), "Verifier failed: {:?}", res.err().unwrap());
3838
println!("verifier output: {:?}", res.unwrap());
3939
}

zkvm/risc0/src/methods.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11

2-
pub const DCAP_QUOTE_VERIFIER_ID: [u32; 8] = [927428042, 1559589155, 2728204900, 3879470470, 2705950934, 683452413, 3440644992, 2443300394];
3-
pub const DCAP_QUOTE_VERIFIER_ID_STR: &str = "ca6d47372371f55c641a9da286053ce7d68849a1fda7bc28801314cd2acea191";
2+
pub const DCAP_QUOTE_VERIFIER_ID: [u32; 8] = [1431900809, 3770780031, 3053255185, 442616749, 2525666630, 3278113115, 2914535527, 4088667128];
3+
pub const DCAP_QUOTE_VERIFIER_ID_STR: &str = "891259557f89c1e011fafcb5adcb611a469d8a965b0964c36748b8adf81bb4f3";
44
pub const DCAP_QUOTE_VERIFIER_ELF: &[u8] = include_bytes!("../artifacts/dcap-quote-verifier");

0 commit comments

Comments
 (0)