From ef91b59a1d73d462ceccf6a36963c1447d183815 Mon Sep 17 00:00:00 2001 From: tolak Date: Fri, 11 Oct 2024 11:19:01 +0800 Subject: [PATCH 01/13] build web and node target --- Cargo.toml | 5 +++++ Makefile | 29 +++++++++++++++++++++++++++++ src/collateral.rs | 1 + 3 files changed, 35 insertions(+) create mode 100644 Makefile diff --git a/Cargo.toml b/Cargo.toml index 5b76bab..c41d589 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -45,6 +45,7 @@ serde_json = { version = "1.0.108", optional = true, features = [ ] } tracing = { version = "0.1", optional = true } futures = { version = "0.3", optional = true } +getrandom = { version = "0.2", features = ["js"] } [dependencies.webpki] version = "0.102.7" @@ -56,6 +57,9 @@ features = ["alloc", "ring"] insta = "1" tokio = { version = "1", features = ["full"] } +[lib] +crate-type = ["cdylib", "rlib"] + [features] default = ["std", "report"] std = [ @@ -74,3 +78,4 @@ std = [ "urlencoding", ] report = ["std", "tracing", "futures"] +js = [] diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..9159f75 --- /dev/null +++ b/Makefile @@ -0,0 +1,29 @@ +WASM_PACK = wasm-pack +INSTALL_TOOL = cargo install wasm-pack +BUILD_WEB = $(WASM_PACK) build --release --target web --out-dir pkg/web --out-name dcap-qvl-web -- --features=js +BUILD_NODE = $(WASM_PACK) build --release --target nodejs --out-dir pkg/node --out-name dcap-qvl-node -- --features=js + +all: install_wasm_tool build_web_js_api build_node_js_api + +install_wasm_tool: + @echo "Installing wasm-pack if not already installed..." + @if ! command -v $(WASM_PACK) &> /dev/null; then \ + echo "wasm-pack not found, installing..."; \ + $(INSTALL_TOOL); \ + else \ + echo "wasm-pack is already installed."; \ + fi + +build_web_pkg: install_wasm_tool + @echo "Building for web browsers..." + $(BUILD_WEB) + +build_node_pkg: install_wasm_tool + @echo "Building for Node.js..." + $(BUILD_NODE) + +clean: + @echo "Cleaning up..." + rm -rf pkg + +.PHONY: all install_wasm_tool build_web_js_api build_node_js_api clean diff --git a/src/collateral.rs b/src/collateral.rs index db3b615..5e78d99 100644 --- a/src/collateral.rs +++ b/src/collateral.rs @@ -29,6 +29,7 @@ fn get_header(resposne: &reqwest::Response, name: &str) -> Result { /// /// * `Ok(QuoteCollateralV3)` - The quote collateral /// * `Err(Error)` - The error +#[cfg(not(feature = "js"))] pub async fn get_collateral( pccs_url: &str, mut quote: &[u8], From 30fa161df17b9017516a09aca4b1a0ce31a86798 Mon Sep 17 00:00:00 2001 From: tolak Date: Fri, 11 Oct 2024 15:14:27 +0800 Subject: [PATCH 02/13] fix Makefile target name --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 9159f75..1132ec6 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ INSTALL_TOOL = cargo install wasm-pack BUILD_WEB = $(WASM_PACK) build --release --target web --out-dir pkg/web --out-name dcap-qvl-web -- --features=js BUILD_NODE = $(WASM_PACK) build --release --target nodejs --out-dir pkg/node --out-name dcap-qvl-node -- --features=js -all: install_wasm_tool build_web_js_api build_node_js_api +all: install_wasm_tool build_web_pkg build_node_pkg install_wasm_tool: @echo "Installing wasm-pack if not already installed..." @@ -26,4 +26,4 @@ clean: @echo "Cleaning up..." rm -rf pkg -.PHONY: all install_wasm_tool build_web_js_api build_node_js_api clean +.PHONY: all install_wasm_tool build_web_pkg build_node_pkg clean From 6b15004ede1acb1cd3ea8051eb19ed4514e89c6c Mon Sep 17 00:00:00 2001 From: Shawn Tian Date: Tue, 15 Oct 2024 11:15:00 +0800 Subject: [PATCH 03/13] add wasm_bindgen trait to js api interface --- Cargo.toml | 1 + src/lib.rs | 3 +++ src/quote.rs | 3 +++ src/verify.rs | 3 +++ 4 files changed, 10 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index c41d589..68dcdc5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -46,6 +46,7 @@ serde_json = { version = "1.0.108", optional = true, features = [ tracing = { version = "0.1", optional = true } futures = { version = "0.3", optional = true } getrandom = { version = "0.2", features = ["js"] } +wasm-bindgen = "0.2.95" [dependencies.webpki] version = "0.102.7" diff --git a/src/lib.rs b/src/lib.rs index cabe483..e8d1ebf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,8 +40,10 @@ extern crate alloc; use scale::{Decode, Encode}; use scale_info::TypeInfo; +use wasm_bindgen::prelude::*; #[derive(Encode, Decode, TypeInfo, Debug, Clone, PartialEq, Eq)] +#[wasm_bindgen] pub enum Error { InvalidCertificate, InvalidSignature, @@ -76,6 +78,7 @@ pub enum Error { } #[derive(Encode, Decode, Clone, PartialEq, Eq, Debug)] +#[wasm_bindgen] pub struct QuoteCollateralV3 { pub pck_crl_issuer_chain: String, pub root_ca_crl: String, diff --git a/src/quote.rs b/src/quote.rs index 38f6f14..abdd026 100644 --- a/src/quote.rs +++ b/src/quote.rs @@ -3,6 +3,7 @@ use alloc::vec::Vec; use anyhow::Result; use scale::{Decode, Input}; +use wasm_bindgen::prelude::*; use crate::{constants::*, utils, Error}; @@ -41,6 +42,7 @@ pub struct Body { pub size: u32, } +#[wasm_bindgen] #[derive(Decode, Debug, Clone)] pub struct EnclaveReport { pub cpu_svn: [u8; 16], @@ -183,6 +185,7 @@ fn decode_auth_data(ver: u16, input: &mut &[u8]) -> Result, @@ -31,6 +33,7 @@ pub struct VerifiedReport { /// /// * `Ok(VerifiedReport)` - The verified report /// * `Err(Error)` - The error +#[wasm_bindgen] pub fn verify( raw_quote: &[u8], quote_collateral: &QuoteCollateralV3, From 6c62c0d3e66ff8b094c111f4cebd8f29fa17cb62 Mon Sep 17 00:00:00 2001 From: Shawn Tian Date: Tue, 15 Oct 2024 13:53:23 +0800 Subject: [PATCH 04/13] add serde-wasm-bindgen --- Cargo.toml | 1 + src/lib.rs | 8 +++----- src/quote.rs | 42 ++++++++++++++++++++---------------------- src/verify.rs | 4 ++-- 4 files changed, 26 insertions(+), 29 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 68dcdc5..7706c62 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -46,6 +46,7 @@ serde_json = { version = "1.0.108", optional = true, features = [ tracing = { version = "0.1", optional = true } futures = { version = "0.3", optional = true } getrandom = { version = "0.2", features = ["js"] } +serde-wasm-bindgen = "0.4" wasm-bindgen = "0.2.95" [dependencies.webpki] diff --git a/src/lib.rs b/src/lib.rs index e8d1ebf..5f35c53 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,10 +40,9 @@ extern crate alloc; use scale::{Decode, Encode}; use scale_info::TypeInfo; -use wasm_bindgen::prelude::*; +use serde::{Deserialize, Serialize}; -#[derive(Encode, Decode, TypeInfo, Debug, Clone, PartialEq, Eq)] -#[wasm_bindgen] +#[derive(Encode, Decode, TypeInfo, Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub enum Error { InvalidCertificate, InvalidSignature, @@ -77,8 +76,7 @@ pub enum Error { OidIsMissing, } -#[derive(Encode, Decode, Clone, PartialEq, Eq, Debug)] -#[wasm_bindgen] +#[derive(Encode, Decode, Clone, PartialEq, Eq, Debug, Serialize, Deserialize)] pub struct QuoteCollateralV3 { pub pck_crl_issuer_chain: String, pub root_ca_crl: String, diff --git a/src/quote.rs b/src/quote.rs index abdd026..4d4f20b 100644 --- a/src/quote.rs +++ b/src/quote.rs @@ -3,7 +3,7 @@ use alloc::vec::Vec; use anyhow::Result; use scale::{Decode, Input}; -use wasm_bindgen::prelude::*; +use serde::{Deserialize, Serialize}; use crate::{constants::*, utils, Error}; @@ -42,8 +42,7 @@ pub struct Body { pub size: u32, } -#[wasm_bindgen] -#[derive(Decode, Debug, Clone)] +#[derive(Serialize, Deserialize, Decode, Debug, Clone)] pub struct EnclaveReport { pub cpu_svn: [u8; 16], pub misc_select: u32, @@ -52,37 +51,37 @@ pub struct EnclaveReport { pub mr_enclave: [u8; 32], pub reserved2: [u8; 32], pub mr_signer: [u8; 32], - pub reserved3: [u8; 96], + pub reserved3: [u8; 32], pub isv_prod_id: u16, pub isv_svn: u16, - pub reserved4: [u8; 60], - pub report_data: [u8; 64], + pub reserved4: [u8; 32], + pub report_data: [u8; 32], } -#[derive(Decode, Debug, Clone)] +#[derive(Decode, Debug, Clone, Serialize, Deserialize)] pub struct TDReport10 { pub tee_tcb_svn: [u8; 16], - pub mr_seam: [u8; 48], - pub mr_signer_seam: [u8; 48], + pub mr_seam: [u8; 32], + pub mr_signer_seam: [u8; 32], pub seam_attributes: [u8; 8], pub td_attributes: [u8; 8], pub xfam: [u8; 8], - pub mr_td: [u8; 48], - pub mr_config_id: [u8; 48], - pub mr_owner: [u8; 48], - pub mr_owner_config: [u8; 48], - pub rt_mr0: [u8; 48], - pub rt_mr1: [u8; 48], - pub rt_mr2: [u8; 48], - pub rt_mr3: [u8; 48], - pub report_data: [u8; 64], + pub mr_td: [u8; 32], + pub mr_config_id: [u8; 32], + pub mr_owner: [u8; 32], + pub mr_owner_config: [u8; 32], + pub rt_mr0: [u8; 32], + pub rt_mr1: [u8; 32], + pub rt_mr2: [u8; 32], + pub rt_mr3: [u8; 32], + pub report_data: [u8; 32], } -#[derive(Decode, Debug, Clone)] +#[derive(Decode, Debug, Clone, Serialize, Deserialize)] pub struct TDReport15 { pub base: TDReport10, pub tee_tcb_svn2: [u8; 16], - pub mr_service_td: [u8; 48], + pub mr_service_td: [u8; 32], } #[derive(Decode)] @@ -185,8 +184,7 @@ fn decode_auth_data(ver: u16, input: &mut &[u8]) -> Result, From 1440fab6e640abd39c7690dacd94af95b078e246 Mon Sep 17 00:00:00 2001 From: tolak Date: Tue, 15 Oct 2024 14:45:50 +0800 Subject: [PATCH 05/13] add js_verify --- src/constants.rs | 1 - src/verify.rs | 21 ++++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/constants.rs b/src/constants.rs index 909e2da..c0afcb0 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -73,7 +73,6 @@ pub const ATTESTATION_KEY_LEN: usize = 64; pub const AUTHENTICATION_DATA_LEN: usize = 32; pub const QE_HASH_DATA_BYTE_LEN: usize = ATTESTATION_KEY_LEN + AUTHENTICATION_DATA_LEN; - pub const PCK_ID_PLAIN: u16 = 1; pub const PCK_ID_RSA_2048_OAEP: u16 = 2; pub const PCK_ID_RSA_3072_OAEP: u16 = 3; diff --git a/src/verify.rs b/src/verify.rs index 51e64fe..5c295f8 100644 --- a/src/verify.rs +++ b/src/verify.rs @@ -21,6 +21,26 @@ pub struct VerifiedReport { pub report: Report, } +#[wasm_bindgen] +pub fn js_verify( + raw_quote: JsValue, + quote_collateral: JsValue, + now: u64, +) -> Result { + let raw_quote: Vec = serde_wasm_bindgen::from_value(raw_quote) + .map_err(|_| JsValue::from_str("Failed to decode raw_quote"))?; + let quote_collateral: QuoteCollateralV3 = serde_wasm_bindgen::from_value(quote_collateral) + .map_err(|_| JsValue::from_str("Failed to decode quote_collateral"))?; + + let verified_report = verify(&raw_quote, "e_collateral, now).map_err(|e| { + serde_wasm_bindgen::to_value(&e) + .unwrap_or_else(|_| JsValue::from_str("Failed to encode Error")) + })?; + + serde_wasm_bindgen::to_value(&verified_report) + .map_err(|_| JsValue::from_str("Failed to encode verified_report")) +} + /// Verify a quote /// /// # Arguments @@ -33,7 +53,6 @@ pub struct VerifiedReport { /// /// * `Ok(VerifiedReport)` - The verified report /// * `Err(Error)` - The error -#[wasm_bindgen] pub fn verify( raw_quote: &[u8], quote_collateral: &QuoteCollateralV3, From 5f2f3ecd270eb1729073669d6d2ed8cb9915ea43 Mon Sep 17 00:00:00 2001 From: tolak Date: Tue, 15 Oct 2024 16:33:18 +0800 Subject: [PATCH 06/13] compile ring to wasm js to avoid env being imported in wasm --- Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7706c62..c1bd031 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,7 +31,7 @@ log = { version = "0.4.20", default-features = false } anyhow = { version = "1", optional = true } -ring = { version = "0.16.20", default-features = false, features = [ +ring = { version = "0.17", default-features = false, features = [ "alloc", ] } reqwest = { version = "0.11.27", optional = true, default-features = false, features = [ @@ -80,4 +80,4 @@ std = [ "urlencoding", ] report = ["std", "tracing", "futures"] -js = [] +js = ["ring/wasm32_unknown_unknown_js"] From 4712c43edd12e6fa1162882927868895fbb8e6a2 Mon Sep 17 00:00:00 2001 From: Shawn Tian Date: Tue, 15 Oct 2024 17:30:58 +0800 Subject: [PATCH 07/13] add serde_bytes to wasm exported struct --- Cargo.toml | 1 + src/quote.rs | 56 ++++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 42 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c1bd031..f8f2215 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -48,6 +48,7 @@ futures = { version = "0.3", optional = true } getrandom = { version = "0.2", features = ["js"] } serde-wasm-bindgen = "0.4" wasm-bindgen = "0.2.95" +serde_bytes = "0.11" [dependencies.webpki] version = "0.102.7" diff --git a/src/quote.rs b/src/quote.rs index 4d4f20b..02076f5 100644 --- a/src/quote.rs +++ b/src/quote.rs @@ -44,44 +44,70 @@ pub struct Body { #[derive(Serialize, Deserialize, Decode, Debug, Clone)] pub struct EnclaveReport { + #[serde(with = "serde_bytes")] pub cpu_svn: [u8; 16], pub misc_select: u32, + #[serde(with = "serde_bytes")] pub reserved1: [u8; 28], + #[serde(with = "serde_bytes")] pub attributes: [u8; 16], + #[serde(with = "serde_bytes")] pub mr_enclave: [u8; 32], + #[serde(with = "serde_bytes")] pub reserved2: [u8; 32], + #[serde(with = "serde_bytes")] pub mr_signer: [u8; 32], - pub reserved3: [u8; 32], + #[serde(with = "serde_bytes")] + pub reserved3: [u8; 96], pub isv_prod_id: u16, pub isv_svn: u16, - pub reserved4: [u8; 32], - pub report_data: [u8; 32], + #[serde(with = "serde_bytes")] + pub reserved4: [u8; 60], + #[serde(with = "serde_bytes")] + pub report_data: [u8; 64], } #[derive(Decode, Debug, Clone, Serialize, Deserialize)] pub struct TDReport10 { + #[serde(with = "serde_bytes")] pub tee_tcb_svn: [u8; 16], - pub mr_seam: [u8; 32], - pub mr_signer_seam: [u8; 32], + #[serde(with = "serde_bytes")] + pub mr_seam: [u8; 48], + #[serde(with = "serde_bytes")] + pub mr_signer_seam: [u8; 48], + #[serde(with = "serde_bytes")] pub seam_attributes: [u8; 8], + #[serde(with = "serde_bytes")] pub td_attributes: [u8; 8], + #[serde(with = "serde_bytes")] pub xfam: [u8; 8], - pub mr_td: [u8; 32], - pub mr_config_id: [u8; 32], - pub mr_owner: [u8; 32], - pub mr_owner_config: [u8; 32], - pub rt_mr0: [u8; 32], - pub rt_mr1: [u8; 32], - pub rt_mr2: [u8; 32], - pub rt_mr3: [u8; 32], - pub report_data: [u8; 32], + #[serde(with = "serde_bytes")] + pub mr_td: [u8; 48], + #[serde(with = "serde_bytes")] + pub mr_config_id: [u8; 48], + #[serde(with = "serde_bytes")] + pub mr_owner: [u8; 48], + #[serde(with = "serde_bytes")] + pub mr_owner_config: [u8; 48], + #[serde(with = "serde_bytes")] + pub rt_mr0: [u8; 48], + #[serde(with = "serde_bytes")] + pub rt_mr1: [u8; 48], + #[serde(with = "serde_bytes")] + pub rt_mr2: [u8; 48], + #[serde(with = "serde_bytes")] + pub rt_mr3: [u8; 48], + #[serde(with = "serde_bytes")] + pub report_data: [u8; 64], } #[derive(Decode, Debug, Clone, Serialize, Deserialize)] pub struct TDReport15 { pub base: TDReport10, + #[serde(with = "serde_bytes")] pub tee_tcb_svn2: [u8; 16], - pub mr_service_td: [u8; 32], + #[serde(with = "serde_bytes")] + pub mr_service_td: [u8; 48], } #[derive(Decode)] From fc0ab40e158d3a1fb0fe4e5e84c91ce88de65637 Mon Sep 17 00:00:00 2001 From: tolak Date: Tue, 15 Oct 2024 17:55:10 +0800 Subject: [PATCH 08/13] fix quote collateral decoding --- src/verify.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/verify.rs b/src/verify.rs index 5c295f8..4b2b25c 100644 --- a/src/verify.rs +++ b/src/verify.rs @@ -29,8 +29,10 @@ pub fn js_verify( ) -> Result { let raw_quote: Vec = serde_wasm_bindgen::from_value(raw_quote) .map_err(|_| JsValue::from_str("Failed to decode raw_quote"))?; - let quote_collateral: QuoteCollateralV3 = serde_wasm_bindgen::from_value(quote_collateral) + let quote_collateral_bytes: Vec = serde_wasm_bindgen::from_value(quote_collateral) .map_err(|_| JsValue::from_str("Failed to decode quote_collateral"))?; + let quote_collateral = QuoteCollateralV3::decode(&mut quote_collateral_bytes.as_slice()) + .map_err(|_| JsValue::from_str("Failed to decode quote_collateral_bytes"))?; let verified_report = verify(&raw_quote, "e_collateral, now).map_err(|e| { serde_wasm_bindgen::to_value(&e) From 9f87a7fa96ab2e6db29ac34c11dbf764bd148452 Mon Sep 17 00:00:00 2001 From: tolak Date: Tue, 15 Oct 2024 17:55:56 +0800 Subject: [PATCH 09/13] test js_verify api in node env --- tests/verify_quote.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 tests/verify_quote.js diff --git a/tests/verify_quote.js b/tests/verify_quote.js new file mode 100644 index 0000000..df9b4d9 --- /dev/null +++ b/tests/verify_quote.js @@ -0,0 +1,31 @@ +const fs = require('fs'); +const path = require('path'); +const { js_verify } = require('../pkg/node/dcap-qvl-node'); + +// Function to read a file as a Uint8Array +function readFileAsUint8Array(filePath) { + const data = fs.readFileSync(filePath); + return new Uint8Array(data); +} + +// Paths to your sample files +const rawQuotePath = path.join(__dirname, '../sample', 'tdx_quote'); +const quoteCollateralPath = path.join(__dirname, '../sample', 'tdx_quote_collateral'); + +// Read the files +const rawQuote = readFileAsUint8Array(rawQuotePath); +console.log("rawQuote: ", rawQuote); +const quoteCollateral = readFileAsUint8Array(quoteCollateralPath); + +// Current timestamp +// TCBInfoExpired when using current timestamp, pick the time from verify_quote.rs +// const now = BigInt(Math.floor(Date.now() / 1000)); +const now = BigInt(1725258675); + +try { + // Call the js_verify function + const result = js_verify(rawQuote, quoteCollateral, now); + console.log('Verification Result:', result); +} catch (error) { + console.error('Verification failed:', error); +} \ No newline at end of file From 24ecb2614d2ae8f8f4d8a1e5fcfdcd1b9e8d795a Mon Sep 17 00:00:00 2001 From: tolak Date: Tue, 15 Oct 2024 21:53:40 +0800 Subject: [PATCH 10/13] test js_verify api in web env --- tests/js/.gitignore | 2 ++ tests/js/README.md | 19 ++++++++++ tests/js/index.html | 12 +++++++ .../verify_quote_node.js} | 7 ++-- tests/js/verify_quote_web.js | 36 +++++++++++++++++++ 5 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 tests/js/.gitignore create mode 100644 tests/js/README.md create mode 100644 tests/js/index.html rename tests/{verify_quote.js => js/verify_quote_node.js} (76%) create mode 100644 tests/js/verify_quote_web.js diff --git a/tests/js/.gitignore b/tests/js/.gitignore new file mode 100644 index 0000000..242f99d --- /dev/null +++ b/tests/js/.gitignore @@ -0,0 +1,2 @@ +pkg +sample \ No newline at end of file diff --git a/tests/js/README.md b/tests/js/README.md new file mode 100644 index 0000000..9e94d0a --- /dev/null +++ b/tests/js/README.md @@ -0,0 +1,19 @@ +# Test the JS bindings + +## Verify Quote with Node + +``` +cd tests/js +node verify_quote.js +``` + +## Verify Quote with Web + +``` +cd tests/js +ln -sf ../../pkg pkg +ln -sf ../../sample sample +python3 -m http.server 8000 +``` + +Open http://localhost:8000/index.html in browser, and check the console for the result. \ No newline at end of file diff --git a/tests/js/index.html b/tests/js/index.html new file mode 100644 index 0000000..624422b --- /dev/null +++ b/tests/js/index.html @@ -0,0 +1,12 @@ + + + + + + Verify Quote + + +

Verify Quote

+ + + \ No newline at end of file diff --git a/tests/verify_quote.js b/tests/js/verify_quote_node.js similarity index 76% rename from tests/verify_quote.js rename to tests/js/verify_quote_node.js index df9b4d9..150ed9c 100644 --- a/tests/verify_quote.js +++ b/tests/js/verify_quote_node.js @@ -1,6 +1,6 @@ const fs = require('fs'); const path = require('path'); -const { js_verify } = require('../pkg/node/dcap-qvl-node'); +const { js_verify } = require('../../pkg/node/dcap-qvl-node'); // Function to read a file as a Uint8Array function readFileAsUint8Array(filePath) { @@ -9,12 +9,11 @@ function readFileAsUint8Array(filePath) { } // Paths to your sample files -const rawQuotePath = path.join(__dirname, '../sample', 'tdx_quote'); -const quoteCollateralPath = path.join(__dirname, '../sample', 'tdx_quote_collateral'); +const rawQuotePath = path.join(__dirname, '../../sample', 'tdx_quote'); +const quoteCollateralPath = path.join(__dirname, '../../sample', 'tdx_quote_collateral'); // Read the files const rawQuote = readFileAsUint8Array(rawQuotePath); -console.log("rawQuote: ", rawQuote); const quoteCollateral = readFileAsUint8Array(quoteCollateralPath); // Current timestamp diff --git a/tests/js/verify_quote_web.js b/tests/js/verify_quote_web.js new file mode 100644 index 0000000..3145428 --- /dev/null +++ b/tests/js/verify_quote_web.js @@ -0,0 +1,36 @@ +import init, { js_verify } from '/pkg/web/dcap-qvl-web.js'; + +// Function to fetch a file as a Uint8Array +async function fetchFileAsUint8Array(url) { + const response = await fetch(url); + const data = await response.arrayBuffer(); + return new Uint8Array(data); +} + +// URLs to your sample files +const rawQuoteUrl = '/sample/tdx_quote'; +const quoteCollateralUrl = '/sample/tdx_quote_collateral'; + +// Load the files +async function loadFilesAndVerify() { + try { + // Initialize the WASM module + await init('/pkg/web/dcap-qvl-web_bg.wasm'); + + const rawQuote = await fetchFileAsUint8Array(rawQuoteUrl); + const quoteCollateral = await fetchFileAsUint8Array(quoteCollateralUrl); + + // Current timestamp + const now = BigInt(1725258675); + + // Call the js_verify function + const result = js_verify(rawQuote, quoteCollateral, now); + console.log('Verification Result:', result); + } catch (error) { + console.error('Verification failed:', error); + } +} + +// Execute the verification +loadFilesAndVerify(); + From bc3903d03b7d987d36a2e11fd5af9030379e5aaa Mon Sep 17 00:00:00 2001 From: Shawn Tian Date: Tue, 15 Oct 2024 22:30:09 +0800 Subject: [PATCH 11/13] rename js filename in readme --- tests/js/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/js/README.md b/tests/js/README.md index 9e94d0a..a0ab1b0 100644 --- a/tests/js/README.md +++ b/tests/js/README.md @@ -4,7 +4,7 @@ ``` cd tests/js -node verify_quote.js +node verify_quote_node.js ``` ## Verify Quote with Web @@ -16,4 +16,4 @@ ln -sf ../../sample sample python3 -m http.server 8000 ``` -Open http://localhost:8000/index.html in browser, and check the console for the result. \ No newline at end of file +Open http://localhost:8000/index.html in browser, and check the console for the result. From d3f0e9decba1a1d0160848f2afe7477d74d5ac31 Mon Sep 17 00:00:00 2001 From: Shawn Tian Date: Thu, 17 Oct 2024 11:05:22 +0800 Subject: [PATCH 12/13] make wasm related denpendencies optional and add them to js feature --- Cargo.toml | 10 +++++----- src/verify.rs | 1 + 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 49e1f0f..d66bb74 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -45,10 +45,10 @@ serde_json = { version = "1.0.108", optional = true, features = [ ] } tracing = { version = "0.1", optional = true } futures = { version = "0.3", optional = true } -getrandom = { version = "0.2", features = ["js"] } -serde-wasm-bindgen = "0.4" -wasm-bindgen = "0.2.95" -serde_bytes = "0.11" +getrandom = { version = "0.2", optional = true, features = ["js"] } +serde-wasm-bindgen = { version = "0.4", optional = true} +wasm-bindgen = { version = "0.2.95", optional = true } +serde_bytes = { version = "0.11", optional = true } [dependencies.webpki] version = "0.102.8" @@ -81,4 +81,4 @@ std = [ "urlencoding", ] report = ["std", "tracing", "futures"] -js = ["ring/wasm32_unknown_unknown_js"] +js = ["ring/wasm32_unknown_unknown_js", "getrandom", "serde-wasm-bindgen", "wasm-bindgen", "serde_bytes"] diff --git a/src/verify.rs b/src/verify.rs index 4b2b25c..bca7761 100644 --- a/src/verify.rs +++ b/src/verify.rs @@ -21,6 +21,7 @@ pub struct VerifiedReport { pub report: Report, } +#[cfg(feature = "js")] #[wasm_bindgen] pub fn js_verify( raw_quote: JsValue, From b6f7c5e44ac58a4effbc4c20707272c0aafab765 Mon Sep 17 00:00:00 2001 From: Shawn Tian Date: Thu, 17 Oct 2024 11:15:25 +0800 Subject: [PATCH 13/13] fix cargo check errors --- Cargo.toml | 4 ++-- src/verify.rs | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d66bb74..afdd551 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -48,7 +48,7 @@ futures = { version = "0.3", optional = true } getrandom = { version = "0.2", optional = true, features = ["js"] } serde-wasm-bindgen = { version = "0.4", optional = true} wasm-bindgen = { version = "0.2.95", optional = true } -serde_bytes = { version = "0.11", optional = true } +serde_bytes = { version = "0.11" } [dependencies.webpki] version = "0.102.8" @@ -81,4 +81,4 @@ std = [ "urlencoding", ] report = ["std", "tracing", "futures"] -js = ["ring/wasm32_unknown_unknown_js", "getrandom", "serde-wasm-bindgen", "wasm-bindgen", "serde_bytes"] +js = ["ring/wasm32_unknown_unknown_js", "getrandom", "serde-wasm-bindgen", "wasm-bindgen"] diff --git a/src/verify.rs b/src/verify.rs index bca7761..237e5b7 100644 --- a/src/verify.rs +++ b/src/verify.rs @@ -12,6 +12,8 @@ use crate::{ }; use crate::{Error, QuoteCollateralV3}; use serde::{Deserialize, Serialize}; + +#[cfg(feature = "js")] use wasm_bindgen::prelude::*; #[derive(Debug, Clone, Deserialize, Serialize)]