Skip to content
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

From instead of TryFrom for proto to prost #3592

Merged
merged 1 commit into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions api/src/convert/collateral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

//! Convert to/from external::Collateral

use crate::{convert::encode_to_protobuf_vec, external, ConversionError};
use crate::{external, ConversionError};
use mc_attest_verifier_types::prost;
use mc_sgx_dcap_types::Collateral;
use mc_util_serial::Message;
Expand All @@ -24,12 +24,25 @@ impl TryFrom<&Collateral> for external::Collateral {
impl TryFrom<&external::Collateral> for Collateral {
type Error = ConversionError;
fn try_from(src: &external::Collateral) -> Result<Self, Self::Error> {
let bytes = encode_to_protobuf_vec(src)?;
let prost = prost::Collateral::decode(bytes.as_slice())?;
let prost = prost::Collateral::from(src);
Ok((&prost).try_into()?)
}
}

impl From<&external::Collateral> for prost::Collateral {
fn from(src: &external::Collateral) -> Self {
Self {
nick-mobilecoin marked this conversation as resolved.
Show resolved Hide resolved
pck_crl_issuer_chain: src.pck_crl_issuer_chain.clone().into_vec(),
root_ca_crl: src.root_ca_crl.clone(),
pck_crl: src.pck_crl.clone(),
tcb_info_issuer_chain: src.tcb_info_issuer_chain.clone().into_vec(),
tcb_info: src.tcb_info.clone(),
qe_identity_issuer_chain: src.qe_identity_issuer_chain.clone().into_vec(),
qe_identity: src.qe_identity.clone(),
}
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
14 changes: 8 additions & 6 deletions api/src/convert/dcap_evidence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

//! Convert to/from external::DcapEvidence

use crate::{convert::encode_to_protobuf_vec, external, ConversionError};
use crate::{external, ConversionError};
use mc_attest_verifier_types::{prost, DcapEvidence};
use mc_util_serial::Message;
use protobuf::Message as ProtoMessage;
Expand Down Expand Up @@ -34,11 +34,13 @@ impl From<&prost::DcapEvidence> for external::DcapEvidence {
}
}

impl TryFrom<&external::DcapEvidence> for prost::DcapEvidence {
type Error = ConversionError;
fn try_from(src: &external::DcapEvidence) -> Result<Self, Self::Error> {
let bytes = encode_to_protobuf_vec(src)?;
Ok(prost::DcapEvidence::decode(bytes.as_slice())?)
impl From<&external::DcapEvidence> for prost::DcapEvidence {
fn from(src: &external::DcapEvidence) -> Self {
Self {
quote: src.quote.as_ref().map(|q| q.into()),
collateral: src.collateral.as_ref().map(|c| c.into()),
report_data: src.report_data.as_ref().map(|r| r.into()),
}
}
}

Expand Down
15 changes: 12 additions & 3 deletions api/src/convert/enclave_report_data_contents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

//! Convert to/from external::EnclaveReportDataContents

use crate::{convert::encode_to_protobuf_vec, external, ConversionError};
use crate::{external, ConversionError};
use mc_attest_verifier_types::{prost, EnclaveReportDataContents};
use mc_util_serial::Message;
use protobuf::Message as ProtoMessage;
Expand All @@ -22,12 +22,21 @@ impl From<&EnclaveReportDataContents> for external::EnclaveReportDataContents {
impl TryFrom<&external::EnclaveReportDataContents> for EnclaveReportDataContents {
type Error = ConversionError;
fn try_from(src: &external::EnclaveReportDataContents) -> Result<Self, Self::Error> {
let bytes = encode_to_protobuf_vec(src)?;
let prost = prost::EnclaveReportDataContents::decode(bytes.as_slice())?;
let prost = prost::EnclaveReportDataContents::from(src);
Ok((&prost).try_into()?)
}
}

impl From<&external::EnclaveReportDataContents> for prost::EnclaveReportDataContents {
fn from(value: &external::EnclaveReportDataContents) -> Self {
Self {
nonce: value.nonce.clone(),
key: value.key.clone(),
custom_identity: value.custom_identity.clone(),
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
20 changes: 0 additions & 20 deletions api/src/convert/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ mod error;
pub use error::ConversionError;

use mc_blockchain_types::BlockIndex;
use protobuf::Message as ProtoMessage;
use std::path::PathBuf;

/// Helper method for getting the suggested path/filename for a given block
Expand Down Expand Up @@ -94,25 +93,6 @@ pub fn merged_block_num_to_s3block_path(
path
}

/// Encode a protobuf type to the protobuf representation.
///
/// This makes it easy to convert from a protobuf to a rust type by way of a
/// prost implementation. While this requires converting to a protobuf stream
/// and back again, this allows for placing most of the complex logic in the
/// `prost` implementation and keeping the local `try_from` implementations
/// simple.
///
/// For example:
/// ```ignore
/// let bytes = encode_to_protobuf_vec(proto_type)?;
/// let prost = prost::TYPENAME::decode(bytes.as_slice())?;
/// let rust_type = TYPENAME::try_from(prost)?;
/// ```
pub(crate) fn encode_to_protobuf_vec<T: ProtoMessage>(msg: &T) -> Result<Vec<u8>, ConversionError> {
let bytes = msg.write_to_bytes().map_err(|_| ConversionError::Other)?;
Ok(bytes)
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
21 changes: 15 additions & 6 deletions api/src/convert/quote3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//! Convert to/from external::Quote3

use crate::{external, ConversionError};
use mc_attest_verifier_types::prost;
use mc_sgx_dcap_types::Quote3;

impl<T: AsRef<[u8]>> From<&Quote3<T>> for external::Quote3 {
Expand All @@ -14,10 +15,18 @@ impl<T: AsRef<[u8]>> From<&Quote3<T>> for external::Quote3 {
}
}

impl TryFrom<external::Quote3> for Quote3<Vec<u8>> {
impl TryFrom<&external::Quote3> for Quote3<Vec<u8>> {
type Error = ConversionError;
fn try_from(src: external::Quote3) -> Result<Self, Self::Error> {
Ok(Quote3::try_from(src.data)?)
fn try_from(src: &external::Quote3) -> Result<Self, Self::Error> {
Ok(Quote3::try_from(&prost::Quote3::from(src))?)
}
}

impl From<&external::Quote3> for prost::Quote3 {
fn from(value: &external::Quote3) -> Self {
Self {
data: value.data.clone(),
}
}
}

Expand All @@ -33,18 +42,18 @@ mod test {
let report = Report::default();
let quote = DcapQuotingEnclave::quote_report(&report).expect("Failed to create quote");
let proto_quote = external::Quote3::from(&quote);
let new_quote = Quote3::try_from(proto_quote).expect("failed to decode proto quote");
let new_quote = Quote3::try_from(&proto_quote).expect("failed to decode proto quote");
assert_eq!(quote, new_quote);
}

#[test]
fn try_from_prost_quote_fails_on_bad_bytes() {
fn try_from_proto_quote_fails_on_bad_bytes() {
let report = Report::default();
let quote = DcapQuotingEnclave::quote_report(&report).expect("Failed to create quote");
let mut proto_quote = external::Quote3::from(&quote);
// Corrupting the quote type
proto_quote.data[1] += 1;
let error = Quote3::try_from(proto_quote);
let error = Quote3::try_from(&proto_quote);

assert_matches!(error, Err(ConversionError::InvalidContents));
}
Expand Down
Loading