Skip to content

Commit

Permalink
re-orginze API trait implementation code
Browse files Browse the repository at this point in the history
  • Loading branch information
hairongchen committed Jan 15, 2024
1 parent d747fb6 commit 4fd24c0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 25 deletions.
25 changes: 0 additions & 25 deletions common/rust/cctrusted_base/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ use crate::api_data::Algorithm;
use crate::api_data::*;
use crate::eventlog::TcgEventLog;
use crate::tcg::TcgDigest;
use crate::tdx::quote::TdxQuote;
use crate::tpm::quote::TpmQuote;
use anyhow::*;
use core::mem;
use core::result::Result;
use core::result::Result::Ok;

pub trait CCTrustedApi {
/***
Expand Down Expand Up @@ -117,23 +112,3 @@ pub trait CCTrustedApi {
pub trait ParseCcReport<T> {
fn parse_cc_report(_report: Vec<u8>) -> Result<T, anyhow::Error>;
}

// API function parses raw cc report to TdxQuote struct
impl ParseCcReport<TdxQuote> for CcReport {
fn parse_cc_report(report: Vec<u8>) -> Result<TdxQuote, anyhow::Error> {
match TdxQuote::parse_tdx_quote(report) {
Ok(tdx_quote) => unsafe {
let report: &TdxQuote = mem::transmute(&tdx_quote);
Ok(report.clone())
},
Err(e) => Err(anyhow!("[parse_cc_report] error parse tdx quote: {:?}", e)),
}
}
}

// API function parses raw cc report to TpmQuote struct
impl ParseCcReport<TpmQuote> for CcReport {
fn parse_cc_report(_report: Vec<u8>) -> Result<TpmQuote, anyhow::Error> {
todo!()
}
}
16 changes: 16 additions & 0 deletions common/rust/cctrusted_base/src/tdx/quote.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#![allow(non_camel_case_types)]
use anyhow::anyhow;
use core::mem;
use core::mem::transmute;
use core::result::Result;
use core::result::Result::Ok;
use log::*;

use crate::api::ParseCcReport;
use crate::api_data::CcReport;
use crate::tdx::common::*;

#[repr(C)]
Expand Down Expand Up @@ -564,3 +567,16 @@ impl TdxQuote {
}
}
}

// API function parses raw cc report to TdxQuote struct
impl ParseCcReport<TdxQuote> for CcReport {
fn parse_cc_report(report: Vec<u8>) -> Result<TdxQuote, anyhow::Error> {
match TdxQuote::parse_tdx_quote(report) {
Ok(tdx_quote) => unsafe {
let report: &TdxQuote = mem::transmute(&tdx_quote);
Ok(report.clone())
},
Err(e) => Err(anyhow!("[parse_cc_report] error parse tdx quote: {:?}", e)),
}
}
}
10 changes: 10 additions & 0 deletions common/rust/cctrusted_base/src/tpm/quote.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use crate::api::ParseCcReport;
use crate::api_data::CcReport;

// return of API parse_cc_report()
pub struct TpmQuote {}

Expand All @@ -6,3 +9,10 @@ impl TpmQuote {
todo!()
}
}

// API function parses raw cc report to TpmQuote struct
impl ParseCcReport<TpmQuote> for CcReport {
fn parse_cc_report(_report: Vec<u8>) -> Result<TpmQuote, anyhow::Error> {
todo!()
}
}

0 comments on commit 4fd24c0

Please sign in to comment.