-
Notifications
You must be signed in to change notification settings - Fork 101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dcap-ql: Fix length validation logic for quote signatures #677
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,7 @@ use serde::{Deserialize, Serialize}; | |
#[cfg(feature = "verify")] | ||
use sgx_isa::Report; | ||
use std::borrow::Cow; | ||
use std::mem; | ||
use std::{cmp, mem}; | ||
use anyhow::bail; | ||
|
||
// ==================================================== | ||
|
@@ -159,7 +159,9 @@ impl<'a> TakePrefix for Cow<'a, str> { | |
} | ||
|
||
pub trait Quote3Signature<'a>: Sized { | ||
fn parse(type_: Quote3AttestationKeyType, data: Cow<'a, [u8]>) -> Result<Self>; | ||
/// Parse the signature encoded in data and return the parsed value along | ||
/// with any trailing data that was not part of the signature. | ||
fn parse(type_: Quote3AttestationKeyType, data: Cow<'a, [u8]>) -> Result<(Self, Cow<'a, [u8]>)>; | ||
} | ||
|
||
pub trait Qe3CertData<'a>: Sized { | ||
|
@@ -231,19 +233,27 @@ fn get_ecdsa_sig_der(sig: &[u8]) -> Result<Vec<u8>> { | |
} | ||
|
||
impl<'a> Quote3Signature<'a> for Quote3SignatureEcdsaP256<'a> { | ||
fn parse(type_: Quote3AttestationKeyType, mut data: Cow<'a, [u8]>) -> Result<Self> { | ||
fn parse(type_: Quote3AttestationKeyType, mut data: Cow<'a, [u8]>) -> Result<(Self, Cow<'a, [u8]>)> { | ||
if type_ != Quote3AttestationKeyType::EcdsaP256 { | ||
bail!("Invalid attestation key type: {:?}", type_) | ||
} | ||
|
||
let sig_len = data.take_prefix(mem::size_of::<u32>()).map(|v| LE::read_u32(&v))?; | ||
if sig_len as usize != data.len() { | ||
let sig_len = data.take_prefix(mem::size_of::<u32>()).map(|v| LE::read_u32(&v))? as usize; | ||
if sig_len > data.len() { | ||
bail!( | ||
"Invalid signature length. Got {}, expected {}", | ||
"Invalid signature length. Got {}, expected >= {}", | ||
data.len(), | ||
sig_len | ||
); | ||
} | ||
// NOTE: data may contain trailing zeros due to `get_quote_size` and | ||
// `get_quote` C APIs allowing larger than necessary buffer to be | ||
// allocated to hold the quote. | ||
let (mut data, trailing) = { | ||
let prefix = data.take_prefix(cmp::min(data.len(), sig_len))?; | ||
(prefix, data) | ||
}; | ||
|
||
let signature = data.take_prefix(ECDSA_P256_SIGNATURE_LEN)?; | ||
let attestation_public_key = data.take_prefix(ECDSA_P256_PUBLIC_KEY_LEN)?; | ||
let qe3_report = data.take_prefix(REPORT_BODY_LEN)?; | ||
|
@@ -262,15 +272,15 @@ impl<'a> Quote3Signature<'a> for Quote3SignatureEcdsaP256<'a> { | |
); | ||
} | ||
|
||
Ok(Quote3SignatureEcdsaP256 { | ||
Ok((Quote3SignatureEcdsaP256 { | ||
signature, | ||
attestation_public_key, | ||
qe3_report, | ||
qe3_signature, | ||
authentication_data, | ||
certification_data_type, | ||
certification_data: data, | ||
}) | ||
}, trailing)) | ||
} | ||
} | ||
|
||
|
@@ -357,7 +367,9 @@ impl<'a> Quote<'a> { | |
&self.report_body | ||
} | ||
|
||
pub fn signature<T: Quote3Signature<'a>>(&self) -> Result<T> { | ||
/// Parse the signature encoded in `self.signature` and return the parsed | ||
/// value along with any trailing data that was not part of the signature. | ||
pub fn signature<T: Quote3Signature<'a>>(&self) -> Result<(T, Cow<'a, [u8]>)> { | ||
let QuoteHeader::V3 { | ||
attestation_key_type, | ||
.. | ||
|
@@ -538,11 +550,15 @@ impl<'a> Quote3SignatureVerify<'a> for Quote3SignatureEcdsaP256<'a> { | |
|
||
impl<'a> Quote<'a> { | ||
#[cfg(feature = "verify")] | ||
pub fn verify<T: Quote3SignatureVerify<'a>>(quote: &'a [u8], root_of_trust: T::TrustRoot) -> Result<Self> { | ||
/// Parses the `Quote` encoded in `quote`, verifies the signature within the | ||
/// quote, and returns the parsed quote along with any trailing data that | ||
/// was not part of the signature. It's up to the caller to decide what to | ||
/// do with the trailing bytes. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This voluntary behavior is not acceptable - the API should such that it's difficult to misuse. |
||
pub fn verify<T: Quote3SignatureVerify<'a>>(quote: &'a [u8], root_of_trust: T::TrustRoot) -> Result<(Self, Cow<'a, [u8]>)> { | ||
let parsed_quote = Self::parse(quote)?; | ||
let sig = parsed_quote.signature::<T>()?; | ||
let (sig, trailing) = parsed_quote.signature::<T>()?; | ||
Quote3SignatureVerify::verify(&sig, quote, root_of_trust)?; | ||
Ok(parsed_quote) | ||
Ok((parsed_quote, trailing)) | ||
} | ||
} | ||
|
||
|
@@ -641,8 +657,10 @@ mod tests { | |
assert_eq!(user_data, &ud); | ||
|
||
assert_eq!(attestation_key_type, Quote3AttestationKeyType::EcdsaP256); | ||
let sig = quote.signature::<Quote3SignatureEcdsaP256>().unwrap(); | ||
let (sig, trailing) = quote.signature::<Quote3SignatureEcdsaP256>().unwrap(); | ||
|
||
let expected_trailing: &[u8] = &[]; | ||
assert_eq!(&*trailing, expected_trailing); | ||
assert_eq!( | ||
sig.certification_data_type(), | ||
CertificationDataType::PpidEncryptedRsa3072 | ||
|
@@ -722,8 +740,11 @@ mod tests { | |
// TODO: Update the example quote with a matching qe3_identity.json file | ||
qe3_identity: include_str!("../tests/corrupt_qe3_identity.json").to_string(), | ||
}; | ||
#[cfg(feature = "verify")] | ||
assert!(Quote::verify::<Quote3SignatureEcdsaP256>(TEST_QUOTE, &mut verifier).is_ok()) | ||
#[cfg(feature = "verify")] { | ||
let (_, trailing) = Quote::verify::<Quote3SignatureEcdsaP256>(TEST_QUOTE, &mut verifier).unwrap(); | ||
let expected_trailing: &[u8] = &[]; | ||
assert_eq!(&*trailing, expected_trailing); | ||
} | ||
} | ||
|
||
#[test] | ||
|
@@ -774,4 +795,64 @@ mod tests { | |
#[cfg(feature = "verify")] | ||
assert!(Quote::verify::<Quote3SignatureEcdsaP256>(TEST_QUOTE, &mut verifier).is_err()); | ||
} | ||
|
||
#[test] | ||
fn test_quote_with_trailing_zeros() { | ||
let expected_trailing: &[u8] = &[42, 75, 92]; | ||
let with_trailing_data = { | ||
const TEST_QUOTE: &[u8] = &*include_bytes!("../tests/quote_pck_cert_chain.bin"); | ||
let n = TEST_QUOTE.len(); | ||
let t = expected_trailing.len(); | ||
let mut with_trailing_data = vec![0u8; n + t]; | ||
with_trailing_data[..n].copy_from_slice(TEST_QUOTE); | ||
with_trailing_data[n..].copy_from_slice(expected_trailing); | ||
with_trailing_data | ||
}; | ||
|
||
let quote = Quote::parse(&with_trailing_data).unwrap(); | ||
let &QuoteHeader::V3 { | ||
attestation_key_type, | ||
.. | ||
} = quote.header(); | ||
|
||
assert_eq!(attestation_key_type, Quote3AttestationKeyType::EcdsaP256); | ||
let (sig, trailing) = quote.signature::<Quote3SignatureEcdsaP256>().unwrap(); | ||
|
||
assert_eq!(&*trailing, expected_trailing); | ||
|
||
#[cfg(feature = "verify")] | ||
let mut verifier = MyVerifier { | ||
// See the note in `fn test_quote_verification`. | ||
// TODO: Update the example quote with a matching qe3_identity.json file | ||
qe3_identity: include_str!("../tests/corrupt_qe3_identity.json").to_string(), | ||
}; | ||
#[cfg(feature = "verify")] { | ||
let (_, trailing) = Quote::verify::<Quote3SignatureEcdsaP256>(&with_trailing_data, &mut verifier).unwrap(); | ||
assert_eq!(&*trailing, expected_trailing); | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_quote_too_short() { | ||
let too_short = { | ||
const TEST_QUOTE: &[u8] = &*include_bytes!("../tests/quote_pck_cert_chain.bin"); | ||
let mut too_short = vec![0u8; TEST_QUOTE.len() - 5]; | ||
let n = too_short.len(); | ||
too_short.copy_from_slice(&TEST_QUOTE[..n]); | ||
too_short | ||
}; | ||
|
||
// We won't detect the issue until we try to parse the signature | ||
let quote = Quote::parse(&too_short).unwrap(); | ||
let &QuoteHeader::V3 { | ||
attestation_key_type, | ||
.. | ||
} = quote.header(); | ||
|
||
assert_eq!(attestation_key_type, Quote3AttestationKeyType::EcdsaP256); | ||
match quote.signature::<Quote3SignatureEcdsaP256>() { | ||
Ok(_) => panic!("unexpected Ok result"), | ||
Err(e) => assert_eq!(e.to_string(), "Invalid signature length. Got 4137, expected >= 4142"), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably can have a warning log here if
data.len() > sig_len
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a better idea, see the new commit.