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

Attesteer RPC additions #1201

Merged
merged 22 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
378652e
rpc: add attesteer_forward_dcap_quote call
OverOrion Mar 2, 2023
5862ee0
rpc: add attesteer_forward_ias_attestation_report
OverOrion Mar 2, 2023
3865f59
remove unused variables
OverOrion Mar 2, 2023
296ce9c
enclave-runtime/attestation: remove duplicate code
OverOrion Mar 2, 2023
ad76638
enclave-runtime/attestation: extract extrinsic sending
OverOrion Mar 2, 2023
3b21f0a
enclave-runtime/attestaion: refactor generate_dcap_ra_extrinsic_inter…
OverOrion Mar 2, 2023
5f1c56f
attesteer: add attesteer RPC calls to the CLI
OverOrion Mar 9, 2023
6e4a5c1
compiling
OverOrion Mar 13, 2023
9a11f6b
cli: attesteer dcap quote verification takes a filename (to a hex enc…
OverOrion Mar 13, 2023
3c184f4
cli: attesteer ias attestation report takes a filename (to a hex enco…
OverOrion Mar 13, 2023
74c7597
clippy: fix needless borrow
OverOrion Mar 13, 2023
376dc0b
rename: match substrate convention for RPC method names
OverOrion Mar 13, 2023
ac5fcc5
rename: SendDCAPQuoteCmd -> SendDcapQuoteCmd
OverOrion Mar 13, 2023
43aa983
rename: SendIASAttestationReportCmd -> SendIasAttestationReportCmd
OverOrion Mar 13, 2023
554eea9
rename: attesteer_callForwardIASAttestationReport -> attesteer_Forwar…
OverOrion Mar 13, 2023
a7e5f80
rename: attesteer_callForwardDCAPQuote -> attesteer_ForwardDcapQuote
OverOrion Mar 13, 2023
e6cdacc
cli: refactor attesteer comamnds to use a neat match expressions
OverOrion Mar 14, 2023
6875baa
cli: attesteer commands use let-else pattern
OverOrion Mar 14, 2023
c920561
cli: attesteer commands send to chain
OverOrion Mar 14, 2023
5b94a6c
rename: attesteer_Forward -> attesteer_forward
OverOrion Mar 16, 2023
2cb4162
Merge branch 'master' into szp/attesteer-rpc
OverOrion Mar 30, 2023
8d75580
Merge branch 'master' into szp/attesteer-rpc
OverOrion Apr 3, 2023
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
2 changes: 1 addition & 1 deletion cli/src/attesteer/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ mod send_dcap_quote;
mod send_ias_attestation;

pub use self::{
send_dcap_quote::SendDCAPQuoteCmd, send_ias_attestation::SendIASAttestationReportCmd,
send_dcap_quote::SendDcapQuoteCmd, send_ias_attestation::SendIasAttestationReportCmd,
};
29 changes: 14 additions & 15 deletions cli/src/attesteer/commands/send_dcap_quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,33 @@ use std::fs::read_to_string;

/// Forward DCAP quote for verification.
#[derive(Debug, Clone, Parser)]
pub struct SendDCAPQuoteCmd {
pub struct SendDcapQuoteCmd {
/// Hex encoded DCAP quote filename.
quote: String,
}

impl SendDCAPQuoteCmd {
impl SendDcapQuoteCmd {
pub fn run(&self, cli: &Cli) {
let direct_api = get_worker_api_direct(cli);
let hex_encoded_quote = read_to_string(&self.quote)
.map_err(|e| error!("Opening hex encoded DCAP quote file failed: {:#?}", e))
.unwrap();
let hex_encoded_quote = match read_to_string(&self.quote) {
Ok(hex_encoded_quote) => hex_encoded_quote,
Err(e) => panic!("Opening hex encoded DCAP quote file failed: {:#?}", e),
};

let rpc_method = "attesteer_callForwardDCAPQuote".to_owned();
let rpc_method = "attesteer_ForwardDcapQuote".to_owned();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small 'f' please :)

let jsonrpc_call: String =
RpcRequest::compose_jsonrpc_call(rpc_method, vec![hex_encoded_quote]).unwrap();

let rpc_response_str = direct_api.get(&jsonrpc_call).unwrap();

// Decode RPC response.
let rpc_response: RpcResponse = serde_json::from_str(&rpc_response_str).ok().unwrap();
let rpc_return_value = RpcReturnValue::from_hex(&rpc_response.result)
// Replace with `inspect_err` once it's stable.
.map_err(|e| {
error!("Failed to decode RpcReturnValue: {:?}", e);
e
})
.ok()
.unwrap();
let Ok(rpc_response) = serde_json::from_str::<RpcResponse>(&rpc_response_str) else {
panic!("Can't parse RPC response: '{rpc_response_str}'");
};
clangenb marked this conversation as resolved.
Show resolved Hide resolved
let rpc_return_value = match RpcReturnValue::from_hex(&rpc_response.result) {
Ok(rpc_return_value) => rpc_return_value,
Err(e) => panic!("Failed to decode RpcReturnValue: {:?}", e),
};

match rpc_return_value.status {
DirectRequestStatus::Ok => println!("DCAP quote verification succeded."),
Expand Down
31 changes: 15 additions & 16 deletions cli/src/attesteer/commands/send_ias_attestation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,43 +19,42 @@ use itc_rpc_client::direct_client::DirectApi;
use itp_rpc::{RpcRequest, RpcResponse, RpcReturnValue};
use itp_types::DirectRequestStatus;
use itp_utils::FromHexPrefixed;
use log::error;
use log::*;
use std::fs::read_to_string;

use crate::{command_utils::get_worker_api_direct, Cli};

/// Forward IAS attestation report for verification.
#[derive(Debug, Clone, Parser)]
pub struct SendIASAttestationReportCmd {
pub struct SendIasAttestationReportCmd {
/// Hex encoded IAS attestation report filename.
report: String,
}

impl SendIASAttestationReportCmd {
impl SendIasAttestationReportCmd {
pub fn run(&self, cli: &Cli) {
let direct_api = get_worker_api_direct(cli);
let hex_encoded_report = read_to_string(&self.report)
.map_err(|e| error!("Opening hex encoded IAS attestation report file failed: {:#?}", e))
.unwrap();
let hex_encoded_report = match read_to_string(&self.report) {
Ok(hex_encoded_report) => hex_encoded_report,
Err(e) => panic!("Opening hex encoded IAS attestation report file failed: {:#?}", e),
};

//let request = Request { shard, cyphertext: hex_encoded_quote.to_vec() };

let rpc_method = "attesteer_callForwardIASAttestationReport".to_owned();
let rpc_method = "attesteer_ForwardIasAttestationReport".to_owned();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same, small 'f' please :)

let jsonrpc_call: String =
RpcRequest::compose_jsonrpc_call(rpc_method, vec![hex_encoded_report]).unwrap();

let rpc_response_str = direct_api.get(&jsonrpc_call).unwrap();

// Decode RPC response.
let rpc_response: RpcResponse = serde_json::from_str(&rpc_response_str).ok().unwrap();
let rpc_return_value = RpcReturnValue::from_hex(&rpc_response.result)
// Replace with `inspect_err` once it's stable.
.map_err(|e| {
error!("Failed to decode RpcReturnValue: {:?}", e);
e
})
.ok()
.unwrap();
let Ok(rpc_response) = serde_json::from_str::<RpcResponse>(&rpc_response_str) else {
panic!("Can't parse RPC response: '{rpc_response_str}'");
};
let rpc_return_value = match RpcReturnValue::from_hex(&rpc_response.result) {
Ok(rpc_return_value) => rpc_return_value,
Err(e) => panic!("Failed to decode RpcReturnValue: {:?}", e),
};

match rpc_return_value.status {
DirectRequestStatus::Ok => println!("IAS attestation report verification succeded."),
Expand Down
6 changes: 3 additions & 3 deletions cli/src/attesteer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@

use crate::Cli;

use self::commands::{SendDCAPQuoteCmd, SendIASAttestationReportCmd};
use self::commands::{SendDcapQuoteCmd, SendIasAttestationReportCmd};

mod commands;

/// Attesteer subcommands for the CLI.
#[derive(Debug, clap::Subcommand)]
pub enum AttesteerCommand {
/// Forward DCAP quote for verification.
SendDCAPQuote(SendDCAPQuoteCmd),
SendDCAPQuote(SendDcapQuoteCmd),

/// Forward IAS attestation report for verification.
SendIASAttestationReport(SendIASAttestationReportCmd),
SendIASAttestationReport(SendIasAttestationReportCmd),
}

impl AttesteerCommand {
Expand Down
49 changes: 37 additions & 12 deletions enclave-runtime/src/rpc/worker_api_direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@

*/

use crate::attestation::{
generate_dcap_ra_extrinsic_from_quote_internal,
generate_ias_ra_extrinsic_from_der_cert_internal,
use crate::{
attestation::{
generate_dcap_ra_extrinsic_from_quote_internal,
generate_ias_ra_extrinsic_from_der_cert_internal,
},
utils::get_validator_accessor_from_solo_or_parachain,
};
use codec::Encode;
use core::result::Result;
use ita_sgx_runtime::Runtime;
use itc_parentchain::light_client::{concurrent_access::ValidatorAccess, ExtrinsicSender};
use itp_primitives_cache::{GetPrimitives, GLOBAL_PRIMITIVES_CACHE};
use itp_rpc::RpcReturnValue;
use itp_sgx_crypto::Rsa3072Seal;
Expand Down Expand Up @@ -149,7 +153,7 @@ where
});

// attesteer_forward_dcap_quote
let attesteer_forward_dcap_quote: &str = "attesteer_callForwardDCAPQuote";
let attesteer_forward_dcap_quote: &str = "attesteer_ForwardDcapQuote";
clangenb marked this conversation as resolved.
Show resolved Hide resolved
io.add_sync_method(attesteer_forward_dcap_quote, move |params: Params| {
let json_value = match forward_dcap_quote_inner(params) {
Ok(val) => RpcReturnValue {
Expand All @@ -165,8 +169,7 @@ where
});

// attesteer_forward_ias_attestation_report
let attesteer_forward_ias_attestation_report: &str =
"attesteer_callForwardIASAttestationReport";
let attesteer_forward_ias_attestation_report: &str = "attesteer_ForwardIasAttestationReport";
clangenb marked this conversation as resolved.
Show resolved Hide resolved
io.add_sync_method(attesteer_forward_ias_attestation_report, move |params: Params| {
let json_value = match attesteer_forward_ias_attestation_report_inner(params) {
Ok(val) => RpcReturnValue {
Expand Down Expand Up @@ -233,15 +236,26 @@ fn execute_getter_inner<G: ExecuteGetter>(
fn forward_dcap_quote_inner(params: Params) -> Result<OpaqueExtrinsic, String> {
let hex_encoded_params = params.parse::<Vec<String>>().map_err(|e| format!("{:?}", e))?;

let request =
Request::from_hex(&hex_encoded_params[0].clone()).map_err(|e| format!("{:?}", e))?;
if hex_encoded_params.len() != 1 {
return Err(format!(
"Wrong number of arguments for IAS attestation report forwarding: {}, expected: {}",
hex_encoded_params.len(),
1
))
}

let encoded_quote_to_forward: Vec<u8> = request.cyphertext;
let encoded_quote_to_forward: Vec<u8> =
itp_utils::hex::decode_hex(&hex_encoded_params[0]).map_err(|e| format!("{:?}", e))?;

let url = String::new();
let ext = generate_dcap_ra_extrinsic_from_quote_internal(url, &encoded_quote_to_forward)
.map_err(|e| format!("{:?}", e))?;

let validator_access = get_validator_accessor_from_solo_or_parachain().unwrap();
validator_access
.execute_mut_on_validator(|v| v.send_extrinsics(vec![ext.clone()]))
.unwrap();

clangenb marked this conversation as resolved.
Show resolved Hide resolved
Ok(ext)
}

Expand All @@ -250,15 +264,26 @@ fn attesteer_forward_ias_attestation_report_inner(
) -> Result<OpaqueExtrinsic, String> {
let hex_encoded_params = params.parse::<Vec<String>>().map_err(|e| format!("{:?}", e))?;

let request =
Request::from_hex(&hex_encoded_params[0].clone()).map_err(|e| format!("{:?}", e))?;
if hex_encoded_params.len() != 1 {
return Err(format!(
"Wrong number of arguments for IAS attestation report forwarding: {}, expected: {}",
hex_encoded_params.len(),
1
))
}

let ias_attestation_report: Vec<u8> = request.cyphertext;
let ias_attestation_report =
itp_utils::hex::decode_hex(&hex_encoded_params[0]).map_err(|e| format!("{:?}", e))?;

let url = String::new();
let ext = generate_ias_ra_extrinsic_from_der_cert_internal(url, &ias_attestation_report)
.map_err(|e| format!("{:?}", e))?;

let validator_access = get_validator_accessor_from_solo_or_parachain().unwrap();
validator_access
.execute_mut_on_validator(|v| v.send_extrinsics(vec![ext.clone()]))
.unwrap();

Ok(ext)
}

Expand Down