2024-12-04 20:35:35 +00:00
|
|
|
use cw_client::{CliClient, CwClient};
|
|
|
|
use der::DecodePem;
|
2024-12-04 19:22:13 +00:00
|
|
|
use mc_attestation_verifier::SignedTcbInfo;
|
|
|
|
use p256::ecdsa::VerifyingKey;
|
2024-12-04 20:35:35 +00:00
|
|
|
use quartz_tcbinfo_msgs::ExecuteMsg;
|
|
|
|
use quoted_string::strip_dquotes;
|
|
|
|
use reqwest::Url;
|
2024-12-03 10:02:58 +00:00
|
|
|
use serde_json::{json, Value};
|
2024-11-29 19:07:22 +00:00
|
|
|
use std::collections::HashMap;
|
2024-12-04 20:35:35 +00:00
|
|
|
use std::{fs, path::Path};
|
2024-12-04 19:22:13 +00:00
|
|
|
use x509_cert::Certificate;
|
2024-12-04 21:00:18 +00:00
|
|
|
|
2024-12-04 19:48:38 +00:00
|
|
|
type TcbInfo = String;
|
2024-11-29 19:07:22 +00:00
|
|
|
type Fmspc = String;
|
|
|
|
type Update = String;
|
|
|
|
|
2024-11-29 19:07:22 +00:00
|
|
|
const TCB_SIGNER: &str = include_str!("../tcb_signer.pem");
|
|
|
|
|
2024-12-04 19:48:38 +00:00
|
|
|
async fn get_tcbinfo(fmspc: Fmspc, update: Update) -> String {
|
2024-11-29 19:07:22 +00:00
|
|
|
let url = format!("https://api.trustedservices.intel.com/sgx/certification/v4/tcb?fmspc={fmspc}&update={update}");
|
2024-12-03 09:13:37 +00:00
|
|
|
let body: String = reqwest::get(url)
|
|
|
|
.await
|
2024-11-29 19:07:22 +00:00
|
|
|
.expect("url retrieval failed")
|
|
|
|
.text()
|
2024-12-03 09:38:50 +00:00
|
|
|
.await
|
2024-11-29 19:07:22 +00:00
|
|
|
.expect("could not read https response");
|
2024-12-04 20:35:35 +00:00
|
|
|
body
|
2024-11-29 19:07:22 +00:00
|
|
|
}
|
|
|
|
|
2024-12-03 09:13:37 +00:00
|
|
|
async fn get_fmspc_list() -> Vec<Fmspc> {
|
2024-11-29 19:07:22 +00:00
|
|
|
let body: String =
|
2024-12-04 20:35:35 +00:00
|
|
|
reqwest::get("https://api.trustedservices.intel.com/sgx/certification/v4/fmspcs")
|
|
|
|
.await
|
|
|
|
.expect("url retrieval failed")
|
|
|
|
.text()
|
|
|
|
.await
|
|
|
|
.expect("could not read https response");
|
2024-11-29 19:07:22 +00:00
|
|
|
let fmspc_data: Vec<Value> = serde_json::from_str(&body).expect("could not convert to JSON");
|
|
|
|
let mut fmspc_list: Vec<Fmspc> = Vec::new();
|
|
|
|
for item in fmspc_data.iter() {
|
|
|
|
let fmspc: String = format!("{}", item["fmspc"]);
|
|
|
|
fmspc_list.push(strip_dquotes(&fmspc).unwrap().to_string());
|
|
|
|
}
|
|
|
|
println!("{:?}", fmspc_list);
|
|
|
|
fmspc_list
|
|
|
|
}
|
|
|
|
|
2024-12-04 20:35:35 +00:00
|
|
|
async fn upsert_tcbinfo() -> Result<(), &'static str> {
|
2024-12-05 12:03:39 +00:00
|
|
|
|
|
|
|
let testnet = Url::parse("https://rpc-falcron.pion-1.ntrn.tech").expect("couldn't parse network URL");
|
|
|
|
|
|
|
|
let client = CliClient::neutrond(testnet);
|
|
|
|
let fmspc_list = get_fmspc_list().await;
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-12-04 20:35:35 +00:00
|
|
|
let mut store: HashMap<Fmspc, TcbInfo> = if Path::new("./standard").exists() {
|
|
|
|
let data = fs::read_to_string("./standard").expect("Unable to read file");
|
2024-12-05 11:26:03 +00:00
|
|
|
serde_json::from_str(&data).unwrap()
|
|
|
|
} else {
|
2024-12-04 20:35:35 +00:00
|
|
|
fs::File::create("./standard").expect("couldn't create file");
|
2024-12-05 11:26:03 +00:00
|
|
|
HashMap::new()
|
2024-12-04 20:35:35 +00:00
|
|
|
};
|
2024-12-05 11:26:03 +00:00
|
|
|
|
2024-12-05 12:03:39 +00:00
|
|
|
|
|
|
|
|
2024-12-04 20:35:35 +00:00
|
|
|
let certificate = TCB_SIGNER.to_string();
|
|
|
|
let parsed_certificate =
|
|
|
|
Certificate::from_pem(certificate.clone()).expect("failed to parse PEM");
|
2024-12-05 12:03:39 +00:00
|
|
|
|
2024-12-04 20:35:35 +00:00
|
|
|
let key = VerifyingKey::from_sec1_bytes(
|
2024-12-04 19:22:13 +00:00
|
|
|
parsed_certificate
|
|
|
|
.tbs_certificate
|
|
|
|
.subject_public_key_info
|
|
|
|
.subject_public_key
|
|
|
|
.as_bytes()
|
|
|
|
.expect("Failed to parse public key"),
|
|
|
|
)
|
2024-12-05 11:26:03 +00:00
|
|
|
.expect("Failed to decode public key");
|
2024-12-04 20:35:35 +00:00
|
|
|
|
2024-11-29 19:07:22 +00:00
|
|
|
for fmspc in fmspc_list {
|
2024-12-05 12:03:39 +00:00
|
|
|
|
|
|
|
let tcbinfo_from_api = get_tcbinfo(fmspc.clone(), "standard".to_string()).await;
|
|
|
|
// assert!(verify_signature(tcbinfo.clone(), key));
|
|
|
|
let contract_address =
|
|
|
|
"neutron1r4m59786vmxrx866585ze5ugjx9egcyja0nuxhn2y6d7ht6680sspa89zk"
|
|
|
|
.parse()
|
|
|
|
.expect("failed to parse contract address");
|
|
|
|
let query_msg = format!("{\"get_tcb_info\": {\"fmspc\": \"{}\"}}", fmspc) ;
|
|
|
|
let tcbinfo_on_chain = client.query_smart(&contract_address, query_msg).await;
|
|
|
|
println!("{tcbinfo_on_chain}");
|
|
|
|
exit();
|
2024-12-05 11:26:03 +00:00
|
|
|
let store_entry = if store.contains_key(&fmspc) {
|
2024-12-04 20:41:48 +00:00
|
|
|
&store[&fmspc]
|
2024-12-05 11:26:03 +00:00
|
|
|
} else {
|
|
|
|
""
|
|
|
|
};
|
2024-11-29 19:07:22 +00:00
|
|
|
if *store_entry != tcbinfo {
|
2024-11-29 19:07:22 +00:00
|
|
|
println!("updating local TCBInfo for FMSPC: {fmspc}");
|
|
|
|
store.insert(fmspc.clone(), tcbinfo.clone());
|
|
|
|
println!("updating on-chain TCBInfo for FMSPC: {fmspc}");
|
|
|
|
let chain_id = tendermint::chain::id::Id::try_from("pion-1").expect("invalid chain id");
|
2024-11-29 19:07:22 +00:00
|
|
|
let sender = "ajinkya";
|
2024-12-05 12:03:39 +00:00
|
|
|
|
2024-12-03 09:58:52 +00:00
|
|
|
let execute_msg = ExecuteMsg {
|
2024-11-29 19:07:22 +00:00
|
|
|
tcb_info: tcbinfo.to_string(),
|
2024-12-04 20:35:35 +00:00
|
|
|
certificate: certificate.clone(),
|
2024-11-29 19:07:22 +00:00
|
|
|
time: None,
|
2024-12-03 10:01:17 +00:00
|
|
|
};
|
2024-12-04 20:35:35 +00:00
|
|
|
let res = client
|
|
|
|
.tx_execute(
|
|
|
|
&contract_address,
|
|
|
|
&chain_id,
|
2024-12-05 11:26:03 +00:00
|
|
|
1000000,
|
|
|
|
sender,
|
2024-12-04 20:35:35 +00:00
|
|
|
json!(execute_msg),
|
|
|
|
"11000untrn",
|
|
|
|
)
|
|
|
|
.await;
|
2024-12-05 12:03:39 +00:00
|
|
|
println!("done");
|
2024-12-03 10:42:57 +00:00
|
|
|
std::thread::sleep(std::time::Duration::from_secs(5));
|
2024-12-04 20:35:35 +00:00
|
|
|
} else {
|
2024-11-29 19:07:22 +00:00
|
|
|
println!("TCBInfo for FMSPC: {fmspc} up to date")
|
2024-11-29 19:07:22 +00:00
|
|
|
}
|
|
|
|
}
|
2024-12-04 20:35:35 +00:00
|
|
|
let serialized = serde_json::to_string(&store).unwrap();
|
|
|
|
fs::write("./standard", serialized).expect("Unable to write file");
|
2024-11-29 19:07:22 +00:00
|
|
|
Ok(())
|
2024-11-29 19:07:22 +00:00
|
|
|
}
|
2024-11-29 19:07:22 +00:00
|
|
|
|
2024-12-04 20:35:35 +00:00
|
|
|
fn verify_signature(tcbinfo: String, key: VerifyingKey) -> bool {
|
|
|
|
let signed_tcbinfo =
|
|
|
|
SignedTcbInfo::try_from(tcbinfo.as_ref()).expect("tcbinfo string parsing failed");
|
2024-12-05 11:26:03 +00:00
|
|
|
if signed_tcbinfo.verify(Some(&key), None).is_err() {
|
|
|
|
return false;
|
2024-12-04 21:00:18 +00:00
|
|
|
}
|
2024-12-04 20:35:35 +00:00
|
|
|
true
|
2024-12-04 19:22:13 +00:00
|
|
|
}
|
|
|
|
|
2024-12-03 09:13:37 +00:00
|
|
|
#[tokio::main]
|
|
|
|
pub async fn main() {
|
|
|
|
upsert_tcbinfo().await.expect("TCBInfo update failed");
|
2024-11-29 19:07:22 +00:00
|
|
|
}
|