|
1 | 1 | use super::{body::EnclaveReport, CertData, QeAuthData, QuoteHeader}; |
2 | | -use crate::Result; |
3 | | -use anyhow::anyhow; |
| 2 | +use crate::{Result, ENCLAVE_REPORT_LEN, QUOTE_HEADER_LEN}; |
| 3 | +use anyhow::{anyhow, bail}; |
| 4 | + |
| 5 | +const SIGNATURE_DATA_SIZE_OFFSET: usize = QUOTE_HEADER_LEN + ENCLAVE_REPORT_LEN; |
| 6 | +const SIGNATURE_DATA_SIZE_LEN: usize = 4; |
| 7 | +const SIGNATURE_DATA_OFFSET: usize = |
| 8 | + QUOTE_HEADER_LEN + ENCLAVE_REPORT_LEN + SIGNATURE_DATA_SIZE_LEN; |
4 | 9 |
|
5 | 10 | /// Quote structure for DCAP version 3. |
6 | 11 | /// The structure is defined in the Intel SGX ECDSA Quote Library Reference. |
@@ -29,30 +34,31 @@ pub struct QuoteV3 { |
29 | 34 | impl QuoteV3 { |
30 | 35 | /// Parse a QuoteV3 from a byte slice. |
31 | 36 | pub fn from_bytes(raw_bytes: &[u8]) -> Result<QuoteV3> { |
32 | | - if raw_bytes.len() < 436 { |
33 | | - return Err(anyhow!("QuoteV3 data is too short")); |
| 37 | + if raw_bytes.len() < SIGNATURE_DATA_OFFSET { |
| 38 | + bail!("QuoteV3 data is too short"); |
34 | 39 | } |
35 | | - let header = QuoteHeader::from_bytes(&raw_bytes[0..48])?; |
| 40 | + let header = QuoteHeader::from_bytes(&raw_bytes[..QUOTE_HEADER_LEN])?; |
36 | 41 | if header.version != 3 { |
37 | | - return Err(anyhow!("QuoteV3 version is not 3")); |
| 42 | + bail!("QuoteV3 version is not 3"); |
38 | 43 | } |
39 | | - let isv_enclave_report = EnclaveReport::from_bytes(&raw_bytes[48..432])?; |
| 44 | + let isv_enclave_report = |
| 45 | + EnclaveReport::from_bytes(&raw_bytes[QUOTE_HEADER_LEN..SIGNATURE_DATA_SIZE_OFFSET])?; |
40 | 46 | let signature_len = u32::from_le_bytes([ |
41 | | - raw_bytes[432], |
42 | | - raw_bytes[433], |
43 | | - raw_bytes[434], |
44 | | - raw_bytes[435], |
| 47 | + raw_bytes[SIGNATURE_DATA_SIZE_OFFSET], |
| 48 | + raw_bytes[SIGNATURE_DATA_SIZE_OFFSET + 1], |
| 49 | + raw_bytes[SIGNATURE_DATA_SIZE_OFFSET + 2], |
| 50 | + raw_bytes[SIGNATURE_DATA_SIZE_OFFSET + 3], |
45 | 51 | ]); |
46 | | - if raw_bytes.len() != 436 + signature_len as usize { |
47 | | - return Err(anyhow!( |
| 52 | + if raw_bytes.len() < SIGNATURE_DATA_OFFSET + signature_len as usize { |
| 53 | + bail!( |
48 | 54 | "QuoteV3 data is not the expected length: expected {}, got {}", |
49 | | - 436 + signature_len, |
| 55 | + SIGNATURE_DATA_OFFSET + signature_len as usize, |
50 | 56 | raw_bytes.len() |
51 | | - )); |
| 57 | + ); |
52 | 58 | } |
53 | | - // allocate and create a buffer for signature |
54 | | - let signature_slice = &raw_bytes[436..436 + signature_len as usize]; |
55 | | - let signature = QuoteSignatureDataV3::from_bytes(signature_slice)?; |
| 59 | + let signature = QuoteSignatureDataV3::from_bytes( |
| 60 | + &raw_bytes[SIGNATURE_DATA_OFFSET..SIGNATURE_DATA_OFFSET + signature_len as usize], |
| 61 | + )?; |
56 | 62 |
|
57 | 63 | Ok(QuoteV3 { |
58 | 64 | header, |
@@ -130,10 +136,13 @@ impl QuoteSignatureDataV3 { |
130 | 136 | #[cfg(test)] |
131 | 137 | mod tests { |
132 | 138 | use super::*; |
133 | | - use crate::quotes::{ |
134 | | - body::tests::enclave_report_strategy, |
135 | | - tests::{cert_data_strategy, qe_auth_data_strategy, quote_header_strategy}, |
136 | | - Quote, |
| 139 | + use crate::{ |
| 140 | + quotes::{ |
| 141 | + body::tests::enclave_report_strategy, |
| 142 | + tests::{cert_data_strategy, qe_auth_data_strategy, quote_header_strategy}, |
| 143 | + Quote, |
| 144 | + }, |
| 145 | + SGX_TEE_TYPE, |
137 | 146 | }; |
138 | 147 | use proptest::{collection::vec, prelude::*}; |
139 | 148 |
|
@@ -174,7 +183,7 @@ mod tests { |
174 | 183 |
|
175 | 184 | pub(crate) fn quote_v3_strategy() -> impl Strategy<Value = QuoteV3> { |
176 | 185 | ( |
177 | | - quote_header_strategy(Some(3)), |
| 186 | + quote_header_strategy(Some(3), Some(SGX_TEE_TYPE)), |
178 | 187 | enclave_report_strategy(), |
179 | 188 | quote_signature_data_v3_strategy(), |
180 | 189 | ) |
|
0 commit comments