cycles-quartz/crates/utils/tcbinfo-updater/src/main.rs

111 lines
4.1 KiB
Rust
Raw Normal View History

2024-11-29 19:07:22 +00:00
use quoted_string::strip_dquotes;
2024-12-04 19:22:13 +00:00
use mc_attestation_verifier::SignedTcbInfo;
use p256::ecdsa::VerifyingKey;
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 19:22:13 +00:00
use x509_cert::Certificate;
use der::DecodePem;
2024-11-29 19:07:22 +00:00
use std::fs;
2024-11-29 19:07:22 +00:00
use cw_client::{CliClient, CwClient};
use reqwest::Url;
use quartz_tcbinfo_msgs::ExecuteMsg;
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 19:40:28 +00:00
println!("{body}");
2024-12-04 19:48:38 +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-03 09:13:37 +00:00
reqwest::get("https://api.trustedservices.intel.com/sgx/certification/v4/fmspcs").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-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-03 09:13:37 +00:00
async fn upsert_tcbinfo() -> Result<(), &'static str> {
2024-11-29 19:07:22 +00:00
let data = fs::read_to_string("./standard").expect("Unable to read file");
let mut store: HashMap<Fmspc, TcbInfo> = serde_json::from_str(&data).unwrap();
2024-12-04 19:22:13 +00:00
let certificate = TCB_SIGNER.to_string();
let parsed_certificate = Certificate::from_pem(certificate.clone()).expect("failed to parse PEM");
2024-12-03 09:38:50 +00:00
let fmspc_list = get_fmspc_list().await;
2024-12-04 19:22:13 +00:00
let key = VerifyingKey::from_sec1_bytes(
parsed_certificate
.tbs_certificate
.subject_public_key_info
.subject_public_key
.as_bytes()
.expect("Failed to parse public key"),
)
.expect("Failed to decode public key");
2024-11-29 19:07:22 +00:00
for fmspc in fmspc_list {
2024-12-03 09:38:50 +00:00
let tcbinfo = get_tcbinfo(fmspc.clone(), "standard".to_string()).await;
2024-12-04 19:35:48 +00:00
assert!(verify_signature(tcbinfo.clone(), key));
2024-11-29 19:07:22 +00:00
let store_entry = &store[&fmspc];
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 testnet = Url::parse("https://rpc-falcron.pion-1.ntrn.tech").expect("couldn't parse network URL");
2024-12-03 09:41:57 +00:00
let contract_address = "neutron1r4m59786vmxrx866585ze5ugjx9egcyja0nuxhn2y6d7ht6680sspa89zk".parse().expect("failed to parse contract address");
2024-11-29 19:07:22 +00:00
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-11-29 19:07:22 +00:00
let client = CliClient::neutrond(testnet);
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 19:22:13 +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-03 10:42:57 +00:00
let res =
2024-12-03 11:17:38 +00:00
client.tx_execute(&contract_address, &chain_id, 400000, &sender, json!(execute_msg), "11000untrn").await;
2024-12-03 10:42:57 +00:00
println!("done: {res:?}");
std::thread::sleep(std::time::Duration::from_secs(5));
2024-11-29 19:07:22 +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-11-29 19:07:22 +00:00
let serialized = serde_json::to_string(&store).unwrap();
fs::write("./standard", serialized).expect("Unable to write file");
Ok(())
2024-11-29 19:07:22 +00:00
// }
// }
}
2024-11-29 19:07:22 +00:00
2024-12-04 19:48:38 +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-04 19:35:48 +00:00
signed_tcbinfo.verify(Some(&key), None).expect("could not verify signature");
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
}