From 5d60807a42d6052ec3e5e9f5dd37539d73abac21 Mon Sep 17 00:00:00 2001 From: onyinyang Date: Wed, 25 Jan 2023 15:02:23 -0500 Subject: [PATCH 01/22] Updates open_invite to return request and state --- crates/lox-wasm/Cargo.toml | 3 +- crates/lox-wasm/index.js | 25 ++++++++++-- crates/lox-wasm/src/lib.rs | 83 +++++++++++++++++++++++++++++++------- 3 files changed, 93 insertions(+), 18 deletions(-) diff --git a/crates/lox-wasm/Cargo.toml b/crates/lox-wasm/Cargo.toml index 58336b8..c54e2e7 100644 --- a/crates/lox-wasm/Cargo.toml +++ b/crates/lox-wasm/Cargo.toml @@ -10,9 +10,10 @@ license = "MIT" crate-type = ["cdylib"] [dependencies] -lox = { git = "https://git-crysp.uwaterloo.ca/iang/lox.git" } +lox = { git = "https://gitlab.torproject.org/onyinyang/lox.git" } wasm-bindgen = "0.2" serde_json = "1.0.87" +serde-wasm-bindgen = "0.4.5" console_error_panic_hook = "0.1.7" diff --git a/crates/lox-wasm/index.js b/crates/lox-wasm/index.js index abf80e2..269d1b4 100644 --- a/crates/lox-wasm/index.js +++ b/crates/lox-wasm/index.js @@ -1,9 +1,12 @@ import init, { open_invite, set_panic_hook } from "./pkg/lox_wasm.js"; init().then(() => { set_panic_hook(); - request_open_invite().then((token) => { +requested = request_open_invite().then((token) => { open_invite(token); }); +// lox_credential = request_new_lox_credential(request_cred[0]).then((lox_cred) => { +// handle_new_lox_credential(request_cred[1], response, pubkey); +// }) }); function request_open_invite() { @@ -21,7 +24,7 @@ function request_open_invite() { function loxServerRequest(data) { return new Promise((fulfill, reject) => { - const xhr = new XMLHttpRequest(); + const xhr = new XMLHttpRequest(data); xhr.onreadystatechange = function() { if (xhr.DONE !== xhr.readyState) { return; @@ -37,12 +40,28 @@ function loxServerRequest(data) { return; }; try { + if (data != ""){ xhr.open('GET', "http://localhost:8001"); + } + else { + xhr.open("Post", "http://localhost:8001") + xhr.setRequestHeader("Content-Type", "application/json"); + var data = JSON.stringify({}) + } } catch (err) { console.log("Error connecting to lox bridge db"); reject(); return; } - xhr.send(); + xhr.send(data); }); } + +// The correct key should be matched against a public commit to the key to +// verify that the key issuer is in fact the correct Bridge Authority +function loxKeyRequest(key_type) { + return new Promise((fulfull, reject) => { + + + }) +} diff --git a/crates/lox-wasm/src/lib.rs b/crates/lox-wasm/src/lib.rs index 6a50392..6971ffd 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -1,12 +1,15 @@ -use wasm_bindgen::prelude::*; -use lox::OPENINV_LENGTH; +use lox::bridge_table::BridgeLine; +use lox::cred; use lox::proto::open_invite; +use lox::{IssuerPubKey, OPENINV_LENGTH}; use serde_json; +use serde_wasm_bindgen; use std::array::TryFromSliceError; use std::panic; +use wasm_bindgen::prelude::*; #[wasm_bindgen] -extern { +extern "C" { #[wasm_bindgen(js_namespace = console)] pub fn log(s: &str); } @@ -17,20 +20,72 @@ pub fn set_panic_hook() { } #[wasm_bindgen] -pub fn open_invite(invite: &[u8]) { - log(&format!("Using invite: {:?}", invite)); - match validate(invite) { - Ok(token) => { - let (request, _state) = open_invite::request(&token); - let serialized_request = serde_json::to_string(&request).unwrap(); - log(&format!("Formatted open invite request: {}", serialized_request)); - }, - Err(e) => { - log(&format!("{:?}", e)); - }, +pub fn open_invite(invite: &[u8]) -> Result { + unsafe { + log(&format!("Using invite: {:?}", invite)); } + let token = match validate(invite) { + Ok(token) => token, + Err(e) => return Err(JsValue::from(e.to_string())), + }; + let (request, state) = open_invite::request(&token); + let serialized_request = serde_json::to_string(&request).unwrap(); + let serialized_state = serde_json::to_string(&state).unwrap(); + unsafe { + log(&format!( + "Formatted open invite request: {}", + serialized_request + )); + } + let open_lox_result = concat_string(serialized_request, serialized_state); + Ok(open_lox_result) } +fn concat_string(request: String, state: String) -> String { + let mut new_string: String = "Request:".to_owned(); + new_string.push_str(&request); + new_string.push_str(", State:"); + new_string.push_str(&state); + return new_string; +} + +/* +#[wasm_bindgen] +pub fn handle_new_lox_credential(state: String, response: String) -> Result { + unsafe { + log(&format!("Using server response: {:?}", response)); + } + let deserialized_state = serde_json::from_str(&state).unwrap(); + let deserialized_response = serde_json::from_str(&response).unwrap(); + let lox_cred = match open_invite::handle_response(deserialized_state, deserialized_response, lox_pub) { + Ok(lox_cred) => lox_cred, + Err(e) => return Err(JsValue::from(e.to_string())), + }; + let serialized_credential = serde_json::to_string(&lox_cred.0).unwrap(); + let serialized_bridgeline= serde_json::to_string(&lox_cred.1).unwrap(); + unsafe { + log(&format!("Got new Lox Credential: {}", serialized_credential)); + log(&format!("Got new bridgeline: {}", serialized_bridgeline)); + } + Ok((serialized_credential, serialized_bridgeline)) +} */ + +/* Somehow get pubkeys and return to function +#[wasm_bindgen] +pub async fn get_pubkey(key_type: String) -> Result { + let mut url = "http://localhost:8001/".to_owned() + &key_type; + let res = reqwest::Client::new() + .get(url) + .send() + .await?; + + let text = res.text().await?; + let pub_key: IssuerPubKey = serde_json::from_str(&text).unwrap(); + + Ok(pub_key) +} */ + +// This should also check the pubkey fn validate(invite: &[u8]) -> Result<[u8; OPENINV_LENGTH], TryFromSliceError> { invite.try_into() } From d473b537458bab59b99cd79ecd1a34519d25be19 Mon Sep 17 00:00:00 2001 From: onyinyang Date: Wed, 25 Jan 2023 15:02:39 -0500 Subject: [PATCH 02/22] Add wasm_bindgen for handling new lox credential --- crates/lox-wasm/Cargo.toml | 2 +- crates/lox-wasm/src/lib.rs | 15 ++++++++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/crates/lox-wasm/Cargo.toml b/crates/lox-wasm/Cargo.toml index c54e2e7..1954a57 100644 --- a/crates/lox-wasm/Cargo.toml +++ b/crates/lox-wasm/Cargo.toml @@ -10,7 +10,7 @@ license = "MIT" crate-type = ["cdylib"] [dependencies] -lox = { git = "https://gitlab.torproject.org/onyinyang/lox.git" } +lox = { git = "https://gitlab.torproject.org/onyinyang/lox.git", branch = "master" } wasm-bindgen = "0.2" serde_json = "1.0.87" serde-wasm-bindgen = "0.4.5" diff --git a/crates/lox-wasm/src/lib.rs b/crates/lox-wasm/src/lib.rs index 6971ffd..56d23e8 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -1,9 +1,8 @@ use lox::bridge_table::BridgeLine; -use lox::cred; use lox::proto::open_invite; use lox::{IssuerPubKey, OPENINV_LENGTH}; use serde_json; -use serde_wasm_bindgen; +//use serde_wasm_bindgen; use std::array::TryFromSliceError; use std::panic; use wasm_bindgen::prelude::*; @@ -49,15 +48,16 @@ fn concat_string(request: String, state: String) -> String { return new_string; } -/* + #[wasm_bindgen] -pub fn handle_new_lox_credential(state: String, response: String) -> Result { +pub fn handle_new_lox_credential(state: String, response: String, lox_pub: String) -> Result { unsafe { log(&format!("Using server response: {:?}", response)); } let deserialized_state = serde_json::from_str(&state).unwrap(); let deserialized_response = serde_json::from_str(&response).unwrap(); - let lox_cred = match open_invite::handle_response(deserialized_state, deserialized_response, lox_pub) { + let deserialized_pubkey = serde_json::from_str(&lox_pub).unwrap(); + let lox_cred = match open_invite::handle_response(deserialized_state, deserialized_response, &deserialized_pubkey) { Ok(lox_cred) => lox_cred, Err(e) => return Err(JsValue::from(e.to_string())), }; @@ -67,8 +67,9 @@ pub fn handle_new_lox_credential(state: String, response: String) -> Result Date: Wed, 25 Jan 2023 15:02:53 -0500 Subject: [PATCH 03/22] Change calls to match server --- crates/lox-wasm/index.js | 34 ++++++++++++++++++++++++---------- crates/lox-wasm/src/lib.rs | 11 +++++++++-- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/crates/lox-wasm/index.js b/crates/lox-wasm/index.js index 269d1b4..6b62307 100644 --- a/crates/lox-wasm/index.js +++ b/crates/lox-wasm/index.js @@ -1,9 +1,14 @@ import init, { open_invite, set_panic_hook } from "./pkg/lox_wasm.js"; +var pubkeys = request_pubkeys().then((pubkeys) => { + pubkeys +}); +console.log(pubkeys) init().then(() => { set_panic_hook(); requested = request_open_invite().then((token) => { open_invite(token); }); +// unsigned_open_lox_credential = handle_new_lox_credential(requested, pubkeys); // lox_credential = request_new_lox_credential(request_cred[0]).then((lox_cred) => { // handle_new_lox_credential(request_cred[1], response, pubkey); // }) @@ -11,7 +16,7 @@ requested = request_open_invite().then((token) => { function request_open_invite() { return new Promise((fulfill, reject) => { - loxServerRequest("").then((response) => { + loxServerRequest("/invite").then((response) => { console.log("Got invitation token: " + response.invite); fulfill(response.invite); return; @@ -22,6 +27,19 @@ function request_open_invite() { }); } +function request_pubkeys() { + return new Promise((fulfill, reject) => { + loxServerRequest("/pubkeys").then((response) => { + console.log("Got pubkeys: " + JSON.parse(response)); + fulfill(JSON.parse(response)); + return; + }).catch(() => { + console.log("Error requesting open invite from Lox server"); + reject(); + }); + }); +} + function loxServerRequest(data) { return new Promise((fulfill, reject) => { const xhr = new XMLHttpRequest(data); @@ -40,20 +58,16 @@ function loxServerRequest(data) { return; }; try { - if (data != ""){ - xhr.open('GET', "http://localhost:8001"); - } - else { - xhr.open("Post", "http://localhost:8001") - xhr.setRequestHeader("Content-Type", "application/json"); - var data = JSON.stringify({}) - } + xhr.open('GET', "http://localhost:8001"+data); + //xhr.open("Post", "http://localhost:8001"+data) + //xhr.setRequestHeader("Content-Type", "application/json"); + //var data = JSON.stringify({}) } catch (err) { console.log("Error connecting to lox bridge db"); reject(); return; } - xhr.send(data); + xhr.send(); }); } diff --git a/crates/lox-wasm/src/lib.rs b/crates/lox-wasm/src/lib.rs index 56d23e8..b5fa0ba 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -48,12 +48,19 @@ fn concat_string(request: String, state: String) -> String { return new_string; } +fn deconcat_string(concatString: String) -> (String, String) { + + ("hello".to_owned(), "world".to_owned()) + +} + #[wasm_bindgen] -pub fn handle_new_lox_credential(state: String, response: String, lox_pub: String) -> Result { +pub fn handle_new_lox_credential(open_lox_result: String, lox_pub: String) -> Result { unsafe { - log(&format!("Using server response: {:?}", response)); + log(&format!("Using server response: {:?}", open_lox_result)); } + let (state, response) = deconcat_string(open_lox_result); let deserialized_state = serde_json::from_str(&state).unwrap(); let deserialized_response = serde_json::from_str(&response).unwrap(); let deserialized_pubkey = serde_json::from_str(&lox_pub).unwrap(); From dd1b97191426dac37c55c13889a70a16aa33f6f9 Mon Sep 17 00:00:00 2001 From: onyinyang Date: Thu, 26 Jan 2023 00:17:14 -0500 Subject: [PATCH 04/22] Fix javascript to handle values properly --- crates/lox-wasm/index.js | 16 +++++++++------- crates/lox-wasm/src/lib.rs | 2 +- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/crates/lox-wasm/index.js b/crates/lox-wasm/index.js index 6b62307..8aa6a2a 100644 --- a/crates/lox-wasm/index.js +++ b/crates/lox-wasm/index.js @@ -1,18 +1,20 @@ import init, { open_invite, set_panic_hook } from "./pkg/lox_wasm.js"; -var pubkeys = request_pubkeys().then((pubkeys) => { - pubkeys -}); -console.log(pubkeys) -init().then(() => { +let pubkeys = await request_pubkeys(); +console.log(pubkeys); + + +let requested = await init().then(() => { set_panic_hook(); -requested = request_open_invite().then((token) => { - open_invite(token); + let requested = request_open_invite().then((token) => { + return open_invite(token); }); + return requested; // unsigned_open_lox_credential = handle_new_lox_credential(requested, pubkeys); // lox_credential = request_new_lox_credential(request_cred[0]).then((lox_cred) => { // handle_new_lox_credential(request_cred[1], response, pubkey); // }) }); +console.log("Got request and state "+requested); function request_open_invite() { return new Promise((fulfill, reject) => { diff --git a/crates/lox-wasm/src/lib.rs b/crates/lox-wasm/src/lib.rs index b5fa0ba..10eb93d 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -56,7 +56,7 @@ fn deconcat_string(concatString: String) -> (String, String) { #[wasm_bindgen] -pub fn handle_new_lox_credential(open_lox_result: String, lox_pub: String) -> Result { +pub fn handle_new_lox_credential(open_lox_result: String, open_lox_response: String, lox_pub: String) -> Result { unsafe { log(&format!("Using server response: {:?}", open_lox_result)); } From 03008449faba4c355b14645edad13f56d1c1f5d8 Mon Sep 17 00:00:00 2001 From: onyinyang Date: Thu, 26 Jan 2023 14:16:41 -0500 Subject: [PATCH 05/22] Handle request and state more reasonably --- crates/lox-wasm/Cargo.toml | 1 + crates/lox-wasm/src/lib.rs | 18 +++++++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/crates/lox-wasm/Cargo.toml b/crates/lox-wasm/Cargo.toml index 1954a57..5af794d 100644 --- a/crates/lox-wasm/Cargo.toml +++ b/crates/lox-wasm/Cargo.toml @@ -13,6 +13,7 @@ crate-type = ["cdylib"] lox = { git = "https://gitlab.torproject.org/onyinyang/lox.git", branch = "master" } wasm-bindgen = "0.2" serde_json = "1.0.87" +serde = "1" serde-wasm-bindgen = "0.4.5" console_error_panic_hook = "0.1.7" diff --git a/crates/lox-wasm/src/lib.rs b/crates/lox-wasm/src/lib.rs index 10eb93d..6d821ff 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -2,11 +2,18 @@ use lox::bridge_table::BridgeLine; use lox::proto::open_invite; use lox::{IssuerPubKey, OPENINV_LENGTH}; use serde_json; +use serde::{Deserialize,Serialize}; //use serde_wasm_bindgen; use std::array::TryFromSliceError; use std::panic; use wasm_bindgen::prelude::*; +#[derive(Debug,Deserialize, Serialize)] +struct ReqState { + request: String, + state: String, +} + #[wasm_bindgen] extern "C" { #[wasm_bindgen(js_namespace = console)] @@ -28,16 +35,17 @@ pub fn open_invite(invite: &[u8]) -> Result { Err(e) => return Err(JsValue::from(e.to_string())), }; let (request, state) = open_invite::request(&token); - let serialized_request = serde_json::to_string(&request).unwrap(); - let serialized_state = serde_json::to_string(&state).unwrap(); + let req_state = ReqState { + request: serde_json::to_string(&request).unwrap(), + state: serde_json::to_string(&state).unwrap(), + }; unsafe { log(&format!( "Formatted open invite request: {}", - serialized_request + serde_json::to_string(&req_state).unwrap() )); } - let open_lox_result = concat_string(serialized_request, serialized_state); - Ok(open_lox_result) + Ok(serde_json::to_string(&req_state).unwrap()) } fn concat_string(request: String, state: String) -> String { From 832a252318db4d764c51fabf3f430eb0eed96329 Mon Sep 17 00:00:00 2001 From: onyinyang Date: Mon, 30 Jan 2023 16:03:12 -0500 Subject: [PATCH 06/22] Fix serialization of request/state --- crates/lox-wasm/src/lib.rs | 66 +++++++++++++------------------------- 1 file changed, 22 insertions(+), 44 deletions(-) diff --git a/crates/lox-wasm/src/lib.rs b/crates/lox-wasm/src/lib.rs index 6d821ff..6233666 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -8,10 +8,16 @@ use std::array::TryFromSliceError; use std::panic; use wasm_bindgen::prelude::*; -#[derive(Debug,Deserialize, Serialize)] +#[derive(Deserialize, Serialize)] struct ReqState { - request: String, - state: String, + request: lox::proto::open_invite::Request, + state: lox::proto::open_invite::State, +} + +#[derive(Debug,Deserialize, Serialize)] +struct Credential { + lox_credential: String, + bridgeline: String, } #[wasm_bindgen] @@ -36,9 +42,9 @@ pub fn open_invite(invite: &[u8]) -> Result { }; let (request, state) = open_invite::request(&token); let req_state = ReqState { - request: serde_json::to_string(&request).unwrap(), - state: serde_json::to_string(&state).unwrap(), - }; + request: request, + state: state, + }; unsafe { log(&format!( "Formatted open invite request: {}", @@ -48,59 +54,31 @@ pub fn open_invite(invite: &[u8]) -> Result { Ok(serde_json::to_string(&req_state).unwrap()) } -fn concat_string(request: String, state: String) -> String { - let mut new_string: String = "Request:".to_owned(); - new_string.push_str(&request); - new_string.push_str(", State:"); - new_string.push_str(&state); - return new_string; -} - -fn deconcat_string(concatString: String) -> (String, String) { - - ("hello".to_owned(), "world".to_owned()) - -} - #[wasm_bindgen] pub fn handle_new_lox_credential(open_lox_result: String, open_lox_response: String, lox_pub: String) -> Result { unsafe { log(&format!("Using server response: {:?}", open_lox_result)); } - let (state, response) = deconcat_string(open_lox_result); - let deserialized_state = serde_json::from_str(&state).unwrap(); - let deserialized_response = serde_json::from_str(&response).unwrap(); + let req_state: ReqState = serde_json::from_str(&open_lox_result).unwrap(); + let deserialized_state = req_state.state; + let deserialized_response = serde_json::from_str(&open_lox_response).unwrap(); let deserialized_pubkey = serde_json::from_str(&lox_pub).unwrap(); let lox_cred = match open_invite::handle_response(deserialized_state, deserialized_response, &deserialized_pubkey) { Ok(lox_cred) => lox_cred, Err(e) => return Err(JsValue::from(e.to_string())), }; - let serialized_credential = serde_json::to_string(&lox_cred.0).unwrap(); - let serialized_bridgeline= serde_json::to_string(&lox_cred.1).unwrap(); + let lox_cred = Credential { + lox_credential: serde_json::to_string(&lox_cred.0).unwrap(), + bridgeline: serde_json::to_string(&lox_cred.1).unwrap(), + }; unsafe { - log(&format!("Got new Lox Credential: {}", serialized_credential)); - log(&format!("Got new bridgeline: {}", serialized_bridgeline)); + log(&format!("Got new Lox Credential: {}", lox_cred.lox_credential)); + log(&format!("Got new bridgeline: {}", lox_cred.bridgeline)); } - let open_lox_response = concat_string(serialized_credential, serialized_bridgeline); - Ok(open_lox_response) + Ok(serde_json::to_string(&lox_cred).unwrap()) } -/* Somehow get pubkeys and return to function -#[wasm_bindgen] -pub async fn get_pubkey(key_type: String) -> Result { - let mut url = "http://localhost:8001/".to_owned() + &key_type; - let res = reqwest::Client::new() - .get(url) - .send() - .await?; - - let text = res.text().await?; - let pub_key: IssuerPubKey = serde_json::from_str(&text).unwrap(); - - Ok(pub_key) -} */ - // This should also check the pubkey fn validate(invite: &[u8]) -> Result<[u8; OPENINV_LENGTH], TryFromSliceError> { invite.try_into() From dfa2ae4c6f926bdcbf1dc1db230d7715f5e06ae4 Mon Sep 17 00:00:00 2001 From: onyinyang Date: Mon, 30 Jan 2023 18:13:17 -0500 Subject: [PATCH 07/22] Handle open invite credential --- crates/lox-wasm/index.js | 73 +++++++++++++++++++++++++++++++------- crates/lox-wasm/src/lib.rs | 2 +- 2 files changed, 61 insertions(+), 14 deletions(-) diff --git a/crates/lox-wasm/index.js b/crates/lox-wasm/index.js index 8aa6a2a..0110e33 100644 --- a/crates/lox-wasm/index.js +++ b/crates/lox-wasm/index.js @@ -1,4 +1,4 @@ -import init, { open_invite, set_panic_hook } from "./pkg/lox_wasm.js"; +import init, { open_invite, handle_new_lox_credential, set_panic_hook } from "./pkg/lox_wasm.js"; let pubkeys = await request_pubkeys(); console.log(pubkeys); @@ -9,16 +9,36 @@ let requested = await init().then(() => { return open_invite(token); }); return requested; -// unsigned_open_lox_credential = handle_new_lox_credential(requested, pubkeys); -// lox_credential = request_new_lox_credential(request_cred[0]).then((lox_cred) => { -// handle_new_lox_credential(request_cred[1], response, pubkey); -// }) }); console.log("Got request and state "+requested); +let open_lox_cred = await init().then(() => { + set_panic_hook(); + let cred = request_new_lox_credential(requested).then((response) => { + return handle_new_lox_credential(requested, response, pubkeys); + }); + return cred; +}); +console.log("Got request and state "+requested); + +function request_new_lox_credential(requested) { + let req = JSON.parse(requested); + console.log("Request? "+req.request); + return new Promise((fulfill, reject) => { + loxServerPostRequest("/openreq", req.request).then((response) => { + console.log("Got new Lox Credential: " + response); + fulfill(response); + return; + }).catch(() => { + console.log("Error requesting new Lox credential from server"); + reject(); + }); + }); +} + function request_open_invite() { return new Promise((fulfill, reject) => { - loxServerRequest("/invite").then((response) => { + loxServerGetRequest("/invite").then((response) => { console.log("Got invitation token: " + response.invite); fulfill(response.invite); return; @@ -31,8 +51,8 @@ function request_open_invite() { function request_pubkeys() { return new Promise((fulfill, reject) => { - loxServerRequest("/pubkeys").then((response) => { - console.log("Got pubkeys: " + JSON.parse(response)); + loxServerGetRequest("/pubkeys").then((response) => { + console.log("Got pubkeys: " + response); fulfill(JSON.parse(response)); return; }).catch(() => { @@ -42,9 +62,9 @@ function request_pubkeys() { }); } -function loxServerRequest(data) { +function loxServerGetRequest(data) { return new Promise((fulfill, reject) => { - const xhr = new XMLHttpRequest(data); + const xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.DONE !== xhr.readyState) { return; @@ -61,9 +81,6 @@ function loxServerRequest(data) { }; try { xhr.open('GET', "http://localhost:8001"+data); - //xhr.open("Post", "http://localhost:8001"+data) - //xhr.setRequestHeader("Content-Type", "application/json"); - //var data = JSON.stringify({}) } catch (err) { console.log("Error connecting to lox bridge db"); reject(); @@ -73,6 +90,36 @@ function loxServerRequest(data) { }); } +function loxServerPostRequest(data, payload) { + return new Promise((fulfill, reject) => { + const xhr = new XMLHttpRequest(); + xhr.onreadystatechange = function() { + if (xhr.DONE !== xhr.readyState) { + return; + } + if (xhr.status !== 200) { + console.log("Error. Status code: "+xhr.status); + console.log(xhr); + reject(); + return; + } + const response = JSON.parse(xhr.responseText); + console.log("Received: "+response); + fulfill(response); + return; + }; + try { + xhr.open('POST', "http://localhost:8001"+data, true) + xhr.setRequestHeader("Content-Type", "application/json"); + } catch (err) { + console.log("Error connecting to lox bridge db"); + reject(); + return; + } + xhr.send(JSON.stringify(payload)); + }); +} + // The correct key should be matched against a public commit to the key to // verify that the key issuer is in fact the correct Bridge Authority function loxKeyRequest(key_type) { diff --git a/crates/lox-wasm/src/lib.rs b/crates/lox-wasm/src/lib.rs index 6233666..a3df463 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -58,7 +58,7 @@ pub fn open_invite(invite: &[u8]) -> Result { #[wasm_bindgen] pub fn handle_new_lox_credential(open_lox_result: String, open_lox_response: String, lox_pub: String) -> Result { unsafe { - log(&format!("Using server response: {:?}", open_lox_result)); + log(&format!("Using server response: {:?}", open_lox_result)); } let req_state: ReqState = serde_json::from_str(&open_lox_result).unwrap(); let deserialized_state = req_state.state; From 8cc9d3d5d36de6388178c6c9e4c51f7c60aeff02 Mon Sep 17 00:00:00 2001 From: onyinyang Date: Wed, 1 Feb 2023 00:42:49 -0500 Subject: [PATCH 08/22] Change pubkey passing, handling credentials still broken --- crates/lox-wasm/index.js | 8 +++----- crates/lox-wasm/src/lib.rs | 23 ++++++++++++++++++++--- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/crates/lox-wasm/index.js b/crates/lox-wasm/index.js index 0110e33..bfcdb44 100644 --- a/crates/lox-wasm/index.js +++ b/crates/lox-wasm/index.js @@ -2,7 +2,6 @@ import init, { open_invite, handle_new_lox_credential, set_panic_hook } from "./ let pubkeys = await request_pubkeys(); console.log(pubkeys); - let requested = await init().then(() => { set_panic_hook(); let requested = request_open_invite().then((token) => { @@ -22,9 +21,9 @@ let open_lox_cred = await init().then(() => { console.log("Got request and state "+requested); function request_new_lox_credential(requested) { + return new Promise((fulfill, reject) => { let req = JSON.parse(requested); console.log("Request? "+req.request); - return new Promise((fulfill, reject) => { loxServerPostRequest("/openreq", req.request).then((response) => { console.log("Got new Lox Credential: " + response); fulfill(response); @@ -52,8 +51,8 @@ function request_open_invite() { function request_pubkeys() { return new Promise((fulfill, reject) => { loxServerGetRequest("/pubkeys").then((response) => { - console.log("Got pubkeys: " + response); - fulfill(JSON.parse(response)); + console.log("Got pubkeys: " + JSON.stringify(response)); + fulfill(JSON.stringify(response)); return; }).catch(() => { console.log("Error requesting open invite from Lox server"); @@ -104,7 +103,6 @@ function loxServerPostRequest(data, payload) { return; } const response = JSON.parse(xhr.responseText); - console.log("Received: "+response); fulfill(response); return; }; diff --git a/crates/lox-wasm/src/lib.rs b/crates/lox-wasm/src/lib.rs index a3df463..57916d0 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -14,6 +14,15 @@ struct ReqState { state: lox::proto::open_invite::State, } +#[derive(Debug, Deserialize,Serialize)] +struct PubKeys { + lox_pub: IssuerPubKey, + migration_pub: IssuerPubKey, + migrationkey_pub: IssuerPubKey, + reachability_pub: IssuerPubKey, + invitation_pub: IssuerPubKey, +} + #[derive(Debug,Deserialize, Serialize)] struct Credential { lox_credential: String, @@ -63,11 +72,18 @@ pub fn handle_new_lox_credential(open_lox_result: String, open_lox_response: Str let req_state: ReqState = serde_json::from_str(&open_lox_result).unwrap(); let deserialized_state = req_state.state; let deserialized_response = serde_json::from_str(&open_lox_response).unwrap(); - let deserialized_pubkey = serde_json::from_str(&lox_pub).unwrap(); - let lox_cred = match open_invite::handle_response(deserialized_state, deserialized_response, &deserialized_pubkey) { + let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap(); + unsafe { + log(&format!("pubkeys: {:?}", pubkeys.lox_pub)); + } + let lox_cred = match open_invite::handle_response(deserialized_state, deserialized_response, &pubkeys.lox_pub) { Ok(lox_cred) => lox_cred, - Err(e) => return Err(JsValue::from(e.to_string())), + Err(e) => { + log(&format!("Error: {:?}", e.to_string())); + return Err(JsValue::from(e.to_string())); + } }; + log(&format!("Did this actually work?: {:?}", lox_cred)); let lox_cred = Credential { lox_credential: serde_json::to_string(&lox_cred.0).unwrap(), bridgeline: serde_json::to_string(&lox_cred.1).unwrap(), @@ -79,6 +95,7 @@ pub fn handle_new_lox_credential(open_lox_result: String, open_lox_response: Str Ok(serde_json::to_string(&lox_cred).unwrap()) } + // This should also check the pubkey fn validate(invite: &[u8]) -> Result<[u8; OPENINV_LENGTH], TryFromSliceError> { invite.try_into() From 745b6673cb1f29cf586345a6b80002cb064dfd41 Mon Sep 17 00:00:00 2001 From: onyinyang Date: Mon, 6 Feb 2023 13:58:24 -0500 Subject: [PATCH 09/22] Clean up messages, full first credential handling --- crates/lox-wasm/index.js | 8 ++++---- crates/lox-wasm/src/lib.rs | 35 ++++++++++++++++++----------------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/crates/lox-wasm/index.js b/crates/lox-wasm/index.js index bfcdb44..f04ad5b 100644 --- a/crates/lox-wasm/index.js +++ b/crates/lox-wasm/index.js @@ -18,15 +18,15 @@ let open_lox_cred = await init().then(() => { }); return cred; }); -console.log("Got request and state "+requested); +console.log("Got Lox Credential and BridgeLine "+open_lox_cred); function request_new_lox_credential(requested) { return new Promise((fulfill, reject) => { let req = JSON.parse(requested); - console.log("Request? "+req.request); + console.log("Request? "+JSON.stringify(req.request)); loxServerPostRequest("/openreq", req.request).then((response) => { - console.log("Got new Lox Credential: " + response); - fulfill(response); + console.log("Got new Lox Credential: " + JSON.stringify(response)); + fulfill(JSON.stringify(response)); return; }).catch(() => { console.log("Error requesting new Lox credential from server"); diff --git a/crates/lox-wasm/src/lib.rs b/crates/lox-wasm/src/lib.rs index 57916d0..6f7f55a 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -1,8 +1,7 @@ -use lox::bridge_table::BridgeLine; use lox::proto::open_invite; use lox::{IssuerPubKey, OPENINV_LENGTH}; +use serde::{Deserialize, Serialize}; use serde_json; -use serde::{Deserialize,Serialize}; //use serde_wasm_bindgen; use std::array::TryFromSliceError; use std::panic; @@ -14,7 +13,7 @@ struct ReqState { state: lox::proto::open_invite::State, } -#[derive(Debug, Deserialize,Serialize)] +#[derive(Debug, Deserialize, Serialize)] struct PubKeys { lox_pub: IssuerPubKey, migration_pub: IssuerPubKey, @@ -23,7 +22,7 @@ struct PubKeys { invitation_pub: IssuerPubKey, } -#[derive(Debug,Deserialize, Serialize)] +#[derive(Debug, Deserialize, Serialize)] struct Credential { lox_credential: String, bridgeline: String, @@ -53,7 +52,7 @@ pub fn open_invite(invite: &[u8]) -> Result { let req_state = ReqState { request: request, state: state, - }; + }; unsafe { log(&format!( "Formatted open invite request: {}", @@ -63,39 +62,41 @@ pub fn open_invite(invite: &[u8]) -> Result { Ok(serde_json::to_string(&req_state).unwrap()) } - #[wasm_bindgen] -pub fn handle_new_lox_credential(open_lox_result: String, open_lox_response: String, lox_pub: String) -> Result { - unsafe { - log(&format!("Using server response: {:?}", open_lox_result)); - } +pub fn handle_new_lox_credential( + open_lox_result: String, + open_lox_response: String, + lox_pub: String, +) -> Result { let req_state: ReqState = serde_json::from_str(&open_lox_result).unwrap(); let deserialized_state = req_state.state; let deserialized_response = serde_json::from_str(&open_lox_response).unwrap(); let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap(); - unsafe { - log(&format!("pubkeys: {:?}", pubkeys.lox_pub)); - } - let lox_cred = match open_invite::handle_response(deserialized_state, deserialized_response, &pubkeys.lox_pub) { + let lox_cred = match open_invite::handle_response( + deserialized_state, + deserialized_response, + &pubkeys.lox_pub, + ) { Ok(lox_cred) => lox_cred, Err(e) => { log(&format!("Error: {:?}", e.to_string())); return Err(JsValue::from(e.to_string())); } }; - log(&format!("Did this actually work?: {:?}", lox_cred)); let lox_cred = Credential { lox_credential: serde_json::to_string(&lox_cred.0).unwrap(), bridgeline: serde_json::to_string(&lox_cred.1).unwrap(), }; unsafe { - log(&format!("Got new Lox Credential: {}", lox_cred.lox_credential)); + log(&format!( + "Got new Lox Credential: {}", + lox_cred.lox_credential + )); log(&format!("Got new bridgeline: {}", lox_cred.bridgeline)); } Ok(serde_json::to_string(&lox_cred).unwrap()) } - // This should also check the pubkey fn validate(invite: &[u8]) -> Result<[u8; OPENINV_LENGTH], TryFromSliceError> { invite.try_into() From 080d5879b15cab0b29549541137e19514951c505 Mon Sep 17 00:00:00 2001 From: onyinyang Date: Tue, 7 Feb 2023 00:45:54 -0500 Subject: [PATCH 10/22] Adds trust promo logic, time issue --- crates/lox-wasm/Cargo.toml | 2 +- crates/lox-wasm/index.js | 62 ++++++++++++++++++++------ crates/lox-wasm/src/lib.rs | 91 +++++++++++++++++++++++++++++++++----- 3 files changed, 129 insertions(+), 26 deletions(-) diff --git a/crates/lox-wasm/Cargo.toml b/crates/lox-wasm/Cargo.toml index 5af794d..7cf14be 100644 --- a/crates/lox-wasm/Cargo.toml +++ b/crates/lox-wasm/Cargo.toml @@ -17,5 +17,5 @@ serde = "1" serde-wasm-bindgen = "0.4.5" console_error_panic_hook = "0.1.7" - +time = "0.2" rand = { version = "0.7", features = ["wasm-bindgen"] } diff --git a/crates/lox-wasm/index.js b/crates/lox-wasm/index.js index f04ad5b..3c70961 100644 --- a/crates/lox-wasm/index.js +++ b/crates/lox-wasm/index.js @@ -1,31 +1,54 @@ -import init, { open_invite, handle_new_lox_credential, set_panic_hook } from "./pkg/lox_wasm.js"; +import init, { open_invite, handle_new_lox_credential, trust_promotion, handle_trust_promotion, set_panic_hook } from "./pkg/lox_wasm.js"; let pubkeys = await request_pubkeys(); -console.log(pubkeys); -let requested = await init().then(() => { +let requested_invite = await init().then(() => { set_panic_hook(); - let requested = request_open_invite().then((token) => { + let requested_invite = request_open_invite().then((token) => { return open_invite(token); }); - return requested; + return requested_invite; }); -console.log("Got request and state "+requested); +console.log("Got request and state: "+requested_invite); let open_lox_cred = await init().then(() => { set_panic_hook(); - let cred = request_new_lox_credential(requested).then((response) => { - return handle_new_lox_credential(requested, response, pubkeys); + let cred = request_new_lox_credential(requested_invite).then((response) => { + return handle_new_lox_credential(requested_invite, response, pubkeys); }); return cred; }); -console.log("Got Lox Credential and BridgeLine "+open_lox_cred); -function request_new_lox_credential(requested) { +let requested_trust_promo = trust_promotion(open_lox_cred, pubkeys); + +let trust_promo_cred = await init().then(() => { + set_panic_hook(); + let cred = request_trust_promo_cred(requested_trust_promo).then((response)=> { + return handle_trust_promotion(requested_trust_promo, response); + }); + return cred; + }); + + let reachability_cred = await request_reachability(); + +function request_new_lox_credential(requested_invite) { + return new Promise((fulfill, reject) => { + let req = JSON.parse(requested_invite); + loxServerPostRequest("/openreq", req.request).then((response) => { + console.log("Got new Open Invite Lox Credential: " + JSON.stringify(response)); + fulfill(JSON.stringify(response)); + return; + }).catch(() => { + console.log("Error requesting new Lox credential from server"); + reject(); + }); + }); +} + +function request_trust_promo_cred(requested) { return new Promise((fulfill, reject) => { let req = JSON.parse(requested); - console.log("Request? "+JSON.stringify(req.request)); - loxServerPostRequest("/openreq", req.request).then((response) => { - console.log("Got new Lox Credential: " + JSON.stringify(response)); + loxServerPostRequest("/trustpromo", req.request).then((response) => { + console.log("Got new Trust Promotion Lox Credential: " + JSON.stringify(response)); fulfill(JSON.stringify(response)); return; }).catch(() => { @@ -61,6 +84,19 @@ function request_pubkeys() { }); } +function request_reachability() { + return new Promise((fulfill, reject) => { + loxServerGetRequest("/reachability").then((response) => { + console.log("Got reachability Cred: " + JSON.stringify(response)); + fulfill(JSON.stringify(response)); + return; + }).catch(() => { + console.log("Error requesting bucket reachability credential from Lox server"); + reject(); + }); + }); +} + function loxServerGetRequest(data) { return new Promise((fulfill, reject) => { const xhr = new XMLHttpRequest(); diff --git a/crates/lox-wasm/src/lib.rs b/crates/lox-wasm/src/lib.rs index 6f7f55a..41f0dbe 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -1,4 +1,6 @@ -use lox::proto::open_invite; +use lox::cred::Lox; +use lox::bridge_table::BridgeLine; +use lox::proto::{open_invite, trust_promotion}; use lox::{IssuerPubKey, OPENINV_LENGTH}; use serde::{Deserialize, Serialize}; use serde_json; @@ -8,9 +10,15 @@ use std::panic; use wasm_bindgen::prelude::*; #[derive(Deserialize, Serialize)] -struct ReqState { - request: lox::proto::open_invite::Request, - state: lox::proto::open_invite::State, +struct OpenReqState { + request: open_invite::Request, + state: open_invite::State, +} + +#[derive(Deserialize, Serialize)] +struct TrustReqState { + request: trust_promotion::Request, + state: trust_promotion::State, } #[derive(Debug, Deserialize, Serialize)] @@ -24,8 +32,8 @@ struct PubKeys { #[derive(Debug, Deserialize, Serialize)] struct Credential { - lox_credential: String, - bridgeline: String, + lox_credential: Lox, + bridgeline: BridgeLine, } #[wasm_bindgen] @@ -49,7 +57,7 @@ pub fn open_invite(invite: &[u8]) -> Result { Err(e) => return Err(JsValue::from(e.to_string())), }; let (request, state) = open_invite::request(&token); - let req_state = ReqState { + let req_state = OpenReqState { request: request, state: state, }; @@ -68,7 +76,7 @@ pub fn handle_new_lox_credential( open_lox_response: String, lox_pub: String, ) -> Result { - let req_state: ReqState = serde_json::from_str(&open_lox_result).unwrap(); + let req_state: OpenReqState = serde_json::from_str(&open_lox_result).unwrap(); let deserialized_state = req_state.state; let deserialized_response = serde_json::from_str(&open_lox_response).unwrap(); let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap(); @@ -84,20 +92,79 @@ pub fn handle_new_lox_credential( } }; let lox_cred = Credential { - lox_credential: serde_json::to_string(&lox_cred.0).unwrap(), - bridgeline: serde_json::to_string(&lox_cred.1).unwrap(), + lox_credential: lox_cred.0, + bridgeline: lox_cred.1, }; unsafe { log(&format!( "Got new Lox Credential: {}", - lox_cred.lox_credential + serde_json::to_string(&lox_cred.lox_credential).unwrap() )); - log(&format!("Got new bridgeline: {}", lox_cred.bridgeline)); + log(&format!("Got new bridgeline: {}", serde_json::to_string(&lox_cred.bridgeline).unwrap())); } Ok(serde_json::to_string(&lox_cred).unwrap()) } + +#[wasm_bindgen] +pub fn trust_promotion(open_lox_cred: String, lox_pub: String) -> String { + let lox_cred: Credential = serde_json::from_str(&open_lox_cred).unwrap(); + let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap(); + let (request, state) = trust_promotion::request(&lox_cred.lox_credential, &pubkeys.lox_pub, today()).unwrap(); + let req_state = TrustReqState { + request: request, + state: state, + }; + unsafe { + log(&format!( + "Formatted open invite request: {}", + serde_json::to_string(&req_state).unwrap() + )); + } + serde_json::to_string(&req_state).unwrap() +} + + +#[wasm_bindgen] +pub fn handle_trust_promotion( + trust_promo_request: String, + trust_promo_response: String, +) -> Result { + let req_state: TrustReqState = serde_json::from_str(&trust_promo_request).unwrap(); + let deserialized_state = req_state.state; + let deserialized_response = serde_json::from_str(&trust_promo_response).unwrap(); + let migration_cred = match trust_promotion::handle_response( + deserialized_state, + deserialized_response, + ) { + Ok(migration_cred) => migration_cred, + Err(e) => { + log(&format!("Error: {:?}", e.to_string())); + return Err(JsValue::from(e.to_string())); + } + }; + unsafe { + log(&format!( + "Got new Migration Credential: {}", + serde_json::to_string(&migration_cred).unwrap() + )); + } + Ok(serde_json::to_string(&migration_cred).unwrap()) +} + + + // This should also check the pubkey fn validate(invite: &[u8]) -> Result<[u8; OPENINV_LENGTH], TryFromSliceError> { invite.try_into() } + +/// Get today's (real or simulated) date +fn today() -> u32 { + // We will not encounter negative Julian dates (~6700 years ago) + // or ones larger than 32 bits + (time::OffsetDateTime::now_utc().date()) + .julian_day() + .try_into() + .unwrap() + } \ No newline at end of file From 0a29265c1ff885bd82e40902ce11c7484f079d2b Mon Sep 17 00:00:00 2001 From: onyinyang Date: Fri, 10 Feb 2023 16:18:54 -0500 Subject: [PATCH 11/22] Solve time issue, add time for testing --- crates/lox-wasm/Cargo.toml | 8 +++- crates/lox-wasm/src/lib.rs | 80 +++++++++++++++++++++++--------------- 2 files changed, 55 insertions(+), 33 deletions(-) diff --git a/crates/lox-wasm/Cargo.toml b/crates/lox-wasm/Cargo.toml index 7cf14be..c1d24a0 100644 --- a/crates/lox-wasm/Cargo.toml +++ b/crates/lox-wasm/Cargo.toml @@ -10,12 +10,18 @@ license = "MIT" crate-type = ["cdylib"] [dependencies] +julianday = "1.2.0" lox = { git = "https://gitlab.torproject.org/onyinyang/lox.git", branch = "master" } wasm-bindgen = "0.2" +time = "0.2" serde_json = "1.0.87" serde = "1" serde-wasm-bindgen = "0.4.5" console_error_panic_hook = "0.1.7" -time = "0.2" +js-sys = "0.3.61" rand = { version = "0.7", features = ["wasm-bindgen"] } + +[dependencies.chrono] +version = "0.4.19" +features = ["serde", "wasmbind"] diff --git a/crates/lox-wasm/src/lib.rs b/crates/lox-wasm/src/lib.rs index 41f0dbe..e7be319 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -1,12 +1,14 @@ -use lox::cred::Lox; +use chrono::{Duration, Utc}; +use julianday::JulianDay; use lox::bridge_table::BridgeLine; +use lox::cred::Lox; use lox::proto::{open_invite, trust_promotion}; use lox::{IssuerPubKey, OPENINV_LENGTH}; use serde::{Deserialize, Serialize}; use serde_json; //use serde_wasm_bindgen; use std::array::TryFromSliceError; -use std::panic; +use std::{panic}; use wasm_bindgen::prelude::*; #[derive(Deserialize, Serialize)] @@ -42,6 +44,9 @@ extern "C" { pub fn log(s: &str); } +// Time has to be implemented with wasmbind feature as +// explained here: https://stackoverflow.com/questions/63210984/chrono-kills-my-rust-webassembly-function + #[wasm_bindgen] pub fn set_panic_hook() { panic::set_hook(Box::new(console_error_panic_hook::hook)); @@ -70,6 +75,16 @@ pub fn open_invite(invite: &[u8]) -> Result { Ok(serde_json::to_string(&req_state).unwrap()) } +fn today() -> u32 { + let naive_now = Utc::now().date_naive(); + JulianDay::from(naive_now).inner().try_into().unwrap() +} + +fn add_today(sum: i64) -> u32 { + let naive_now_plus = (Utc::now() + Duration::days(sum)).date_naive(); + JulianDay::from(naive_now_plus).inner().try_into().unwrap() +} + #[wasm_bindgen] pub fn handle_new_lox_credential( open_lox_result: String, @@ -100,20 +115,36 @@ pub fn handle_new_lox_credential( "Got new Lox Credential: {}", serde_json::to_string(&lox_cred.lox_credential).unwrap() )); - log(&format!("Got new bridgeline: {}", serde_json::to_string(&lox_cred.bridgeline).unwrap())); + log(&format!( + "Got new bridgeline: {}", + serde_json::to_string(&lox_cred.bridgeline).unwrap() + )); } Ok(serde_json::to_string(&lox_cred).unwrap()) } - #[wasm_bindgen] -pub fn trust_promotion(open_lox_cred: String, lox_pub: String) -> String { +pub fn trust_promotion(open_lox_cred: String, lox_pub: String) -> Result { let lox_cred: Credential = serde_json::from_str(&open_lox_cred).unwrap(); let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap(); - let (request, state) = trust_promotion::request(&lox_cred.lox_credential, &pubkeys.lox_pub, today()).unwrap(); + // To test creation of the credential we need to advance the day to 30 + // in production this should just use the today() function + log(&format!( + "TEST: Add 30 days to today's date: {}", + add_today(30) + )); + let tp_result = + match trust_promotion::request(&lox_cred.lox_credential, &pubkeys.lox_pub, add_today(30)) { + //CHANGE add_today() to today() + Ok(tp_result) => tp_result, + Err(e) => { + log(&format!("Error: {:?}", e.to_string())); + return Err(JsValue::from(e.to_string())); + } + }; let req_state = TrustReqState { - request: request, - state: state, + request: tp_result.0, + state: tp_result.1, }; unsafe { log(&format!( @@ -121,10 +152,9 @@ pub fn trust_promotion(open_lox_cred: String, lox_pub: String) -> String { serde_json::to_string(&req_state).unwrap() )); } - serde_json::to_string(&req_state).unwrap() + Ok(serde_json::to_string(&req_state).unwrap()) } - #[wasm_bindgen] pub fn handle_trust_promotion( trust_promo_request: String, @@ -133,16 +163,14 @@ pub fn handle_trust_promotion( let req_state: TrustReqState = serde_json::from_str(&trust_promo_request).unwrap(); let deserialized_state = req_state.state; let deserialized_response = serde_json::from_str(&trust_promo_response).unwrap(); - let migration_cred = match trust_promotion::handle_response( - deserialized_state, - deserialized_response, - ) { - Ok(migration_cred) => migration_cred, - Err(e) => { - log(&format!("Error: {:?}", e.to_string())); - return Err(JsValue::from(e.to_string())); - } - }; + let migration_cred = + match trust_promotion::handle_response(deserialized_state, deserialized_response) { + Ok(migration_cred) => migration_cred, + Err(e) => { + log(&format!("Error: {:?}", e.to_string())); + return Err(JsValue::from(e.to_string())); + } + }; unsafe { log(&format!( "Got new Migration Credential: {}", @@ -152,19 +180,7 @@ pub fn handle_trust_promotion( Ok(serde_json::to_string(&migration_cred).unwrap()) } - - // This should also check the pubkey fn validate(invite: &[u8]) -> Result<[u8; OPENINV_LENGTH], TryFromSliceError> { invite.try_into() } - -/// Get today's (real or simulated) date -fn today() -> u32 { - // We will not encounter negative Julian dates (~6700 years ago) - // or ones larger than 32 bits - (time::OffsetDateTime::now_utc().date()) - .julian_day() - .try_into() - .unwrap() - } \ No newline at end of file From 56bc148db15aabc52080d21aaab6a6a4147ada58 Mon Sep 17 00:00:00 2001 From: onyinyang Date: Mon, 13 Feb 2023 16:29:34 -0500 Subject: [PATCH 12/22] Update date to 1 past threshold for upgrade --- crates/lox-wasm/src/lib.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/lox-wasm/src/lib.rs b/crates/lox-wasm/src/lib.rs index e7be319..2d78f70 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -130,12 +130,12 @@ pub fn trust_promotion(open_lox_cred: String, lox_pub: String) -> Result tp_result, Err(e) => { log(&format!("Error: {:?}", e.to_string())); From 944c3634ca516b0c6f3da88dc823f7c7831cb907 Mon Sep 17 00:00:00 2001 From: onyinyang Date: Mon, 13 Feb 2023 23:58:07 -0500 Subject: [PATCH 13/22] Add Trust migration protocol --- crates/lox-wasm/index.js | 41 ++++++++-------- crates/lox-wasm/src/lib.rs | 95 ++++++++++++++++++++++++++++++++------ 2 files changed, 102 insertions(+), 34 deletions(-) diff --git a/crates/lox-wasm/index.js b/crates/lox-wasm/index.js index 3c70961..fa003d9 100644 --- a/crates/lox-wasm/index.js +++ b/crates/lox-wasm/index.js @@ -1,4 +1,11 @@ -import init, { open_invite, handle_new_lox_credential, trust_promotion, handle_trust_promotion, set_panic_hook } from "./pkg/lox_wasm.js"; +import init, { + open_invite, + handle_new_lox_credential, + trust_promotion, + handle_trust_promotion, + trust_migration, + handle_trust_migration, + set_panic_hook } from "./pkg/lox_wasm.js"; let pubkeys = await request_pubkeys(); let requested_invite = await init().then(() => { @@ -12,7 +19,7 @@ console.log("Got request and state: "+requested_invite); let open_lox_cred = await init().then(() => { set_panic_hook(); - let cred = request_new_lox_credential(requested_invite).then((response) => { + let cred = requested_cred("/openreq", requested_invite).then((response) => { return handle_new_lox_credential(requested_invite, response, pubkeys); }); return cred; @@ -22,33 +29,29 @@ let requested_trust_promo = trust_promotion(open_lox_cred, pubkeys); let trust_promo_cred = await init().then(() => { set_panic_hook(); - let cred = request_trust_promo_cred(requested_trust_promo).then((response)=> { + let cred = requested_cred("/trustpromo", requested_trust_promo).then((response)=> { return handle_trust_promotion(requested_trust_promo, response); }); return cred; }); - let reachability_cred = await request_reachability(); +let reachability_cred = await request_reachability(); -function request_new_lox_credential(requested_invite) { - return new Promise((fulfill, reject) => { - let req = JSON.parse(requested_invite); - loxServerPostRequest("/openreq", req.request).then((response) => { - console.log("Got new Open Invite Lox Credential: " + JSON.stringify(response)); - fulfill(JSON.stringify(response)); - return; - }).catch(() => { - console.log("Error requesting new Lox credential from server"); - reject(); - }); +let requested_trust_migration = trust_migration(open_lox_cred, trust_promo_cred, pubkeys); + +let level_1_cred = await init().then(() => { + set_panic_hook(); + let cred = requested_cred("/trustmig", requested_trust_migration).then((response)=> { + return handle_trust_migration(requested_trust_migration, response, pubkeys); + }); + return cred; }); -} -function request_trust_promo_cred(requested) { +function requested_cred(command, requested) { return new Promise((fulfill, reject) => { let req = JSON.parse(requested); - loxServerPostRequest("/trustpromo", req.request).then((response) => { - console.log("Got new Trust Promotion Lox Credential: " + JSON.stringify(response)); + loxServerPostRequest(command, req.request).then((response) => { + console.log("Got new Trust Migration Lox Credential: " + JSON.stringify(response)); fulfill(JSON.stringify(response)); return; }).catch(() => { diff --git a/crates/lox-wasm/src/lib.rs b/crates/lox-wasm/src/lib.rs index 2d78f70..7308a1f 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -2,7 +2,8 @@ use chrono::{Duration, Utc}; use julianday::JulianDay; use lox::bridge_table::BridgeLine; use lox::cred::Lox; -use lox::proto::{open_invite, trust_promotion}; +use lox::proto::{open_invite, trust_promotion, migration, level_up, + issue_invite, redeem_invite, check_blockage, blockage_migration}; use lox::{IssuerPubKey, OPENINV_LENGTH}; use serde::{Deserialize, Serialize}; use serde_json; @@ -23,6 +24,13 @@ struct TrustReqState { state: trust_promotion::State, } +#[derive(Deserialize, Serialize)] +struct MigReqState { + request: migration::Request, + state: migration::State, +} + + #[derive(Debug, Deserialize, Serialize)] struct PubKeys { lox_pub: IssuerPubKey, @@ -38,14 +46,24 @@ struct Credential { bridgeline: BridgeLine, } + +fn today() -> u32 { + let naive_now = Utc::now().date_naive(); + JulianDay::from(naive_now).inner().try_into().unwrap() +} + +// This should only be used for testing, use today in production +fn add_today(sum: i64) -> u32 { + let naive_now_plus = (Utc::now() + Duration::days(sum)).date_naive(); + JulianDay::from(naive_now_plus).inner().try_into().unwrap() +} + #[wasm_bindgen] extern "C" { #[wasm_bindgen(js_namespace = console)] pub fn log(s: &str); } -// Time has to be implemented with wasmbind feature as -// explained here: https://stackoverflow.com/questions/63210984/chrono-kills-my-rust-webassembly-function #[wasm_bindgen] pub fn set_panic_hook() { @@ -75,16 +93,6 @@ pub fn open_invite(invite: &[u8]) -> Result { Ok(serde_json::to_string(&req_state).unwrap()) } -fn today() -> u32 { - let naive_now = Utc::now().date_naive(); - JulianDay::from(naive_now).inner().try_into().unwrap() -} - -fn add_today(sum: i64) -> u32 { - let naive_now_plus = (Utc::now() + Duration::days(sum)).date_naive(); - JulianDay::from(naive_now_plus).inner().try_into().unwrap() -} - #[wasm_bindgen] pub fn handle_new_lox_credential( open_lox_result: String, @@ -134,7 +142,7 @@ pub fn trust_promotion(open_lox_cred: String, lox_pub: String) -> Result tp_result, Err(e) => { @@ -148,7 +156,7 @@ pub fn trust_promotion(open_lox_cred: String, lox_pub: String) -> Result Result { + let lox_cred: Credential = serde_json::from_str(&open_lox_cred).unwrap(); + let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap(); + let mig_cred = serde_json::from_str(&trust_promo_cred).unwrap(); + // To test creation of the credential we need to advance the day to 30 + // in production this should just use the today() function + let tm_result = + //CHANGE add_today(31) to today() for production + match migration::request(&lox_cred.lox_credential, &mig_cred, &pubkeys.lox_pub, &pubkeys.migration_pub) { + Ok(tm_result) => tm_result, + Err(e) => { + log(&format!("Error: {:?}", e.to_string())); + return Err(JsValue::from(e.to_string())); + } + }; + let req_state = MigReqState { + request: tm_result.0, + state: tm_result.1, + }; + unsafe { + log(&format!( + "Formatted Trust Migration request: {}", + serde_json::to_string(&req_state).unwrap() + )); + } + Ok(serde_json::to_string(&req_state).unwrap()) +} + +#[wasm_bindgen] +pub fn handle_trust_migration( + trust_migration_request: String, + trust_migration_response: String, + lox_pub: String +) -> Result { + let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap(); + let req_state: MigReqState = serde_json::from_str(&trust_migration_request).unwrap(); + let deserialized_state = req_state.state; + let deserialized_response = serde_json::from_str(&trust_migration_response).unwrap(); + let level_1_cred = + match migration::handle_response(deserialized_state, deserialized_response, &pubkeys.lox_pub) { + Ok(level_1_cred) => level_1_cred, + Err(e) => { + log(&format!("Error: {:?}", e.to_string())); + return Err(JsValue::from(e.to_string())); + } + }; + unsafe { + log(&format!( + "Got new Level 1 Credential: {}", + serde_json::to_string(&level_1_cred).unwrap() + )); + } + Ok(serde_json::to_string(&level_1_cred).unwrap()) +} + + // This should also check the pubkey fn validate(invite: &[u8]) -> Result<[u8; OPENINV_LENGTH], TryFromSliceError> { invite.try_into() From f44ac0ae9f4219f99ad3d55d4b43ec9656e362be Mon Sep 17 00:00:00 2001 From: onyinyang Date: Tue, 14 Feb 2023 14:45:52 -0500 Subject: [PATCH 14/22] Refactor js to remove duplicate code --- crates/lox-wasm/index.js | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/crates/lox-wasm/index.js b/crates/lox-wasm/index.js index fa003d9..7a5e8a1 100644 --- a/crates/lox-wasm/index.js +++ b/crates/lox-wasm/index.js @@ -6,7 +6,8 @@ import init, { trust_migration, handle_trust_migration, set_panic_hook } from "./pkg/lox_wasm.js"; -let pubkeys = await request_pubkeys(); +let pubkeys = await simple_request("/pubkeys"); +console.log("Got pubkeys: " + pubkeys); let requested_invite = await init().then(() => { set_panic_hook(); @@ -20,6 +21,7 @@ console.log("Got request and state: "+requested_invite); let open_lox_cred = await init().then(() => { set_panic_hook(); let cred = requested_cred("/openreq", requested_invite).then((response) => { + console.log("Got new Level 0 Lox Credential: " + response); return handle_new_lox_credential(requested_invite, response, pubkeys); }); return cred; @@ -30,18 +32,21 @@ let requested_trust_promo = trust_promotion(open_lox_cred, pubkeys); let trust_promo_cred = await init().then(() => { set_panic_hook(); let cred = requested_cred("/trustpromo", requested_trust_promo).then((response)=> { + console.log("Got Migration Credential for Trust Promotion: " + response); return handle_trust_promotion(requested_trust_promo, response); }); return cred; }); -let reachability_cred = await request_reachability(); +let reachability_cred = await simple_request("/reachability"); +console.log("Got reachability credential: " + reachability_cred); let requested_trust_migration = trust_migration(open_lox_cred, trust_promo_cred, pubkeys); let level_1_cred = await init().then(() => { set_panic_hook(); let cred = requested_cred("/trustmig", requested_trust_migration).then((response)=> { + console.log("Got new Level 1 Lox Credential: " + response); return handle_trust_migration(requested_trust_migration, response, pubkeys); }); return cred; @@ -51,7 +56,6 @@ function requested_cred(command, requested) { return new Promise((fulfill, reject) => { let req = JSON.parse(requested); loxServerPostRequest(command, req.request).then((response) => { - console.log("Got new Trust Migration Lox Credential: " + JSON.stringify(response)); fulfill(JSON.stringify(response)); return; }).catch(() => { @@ -74,27 +78,13 @@ function request_open_invite() { }); } -function request_pubkeys() { +function simple_request(requested) { return new Promise((fulfill, reject) => { - loxServerGetRequest("/pubkeys").then((response) => { - console.log("Got pubkeys: " + JSON.stringify(response)); + loxServerGetRequest(requested).then((response) => { fulfill(JSON.stringify(response)); return; }).catch(() => { - console.log("Error requesting open invite from Lox server"); - reject(); - }); - }); -} - -function request_reachability() { - return new Promise((fulfill, reject) => { - loxServerGetRequest("/reachability").then((response) => { - console.log("Got reachability Cred: " + JSON.stringify(response)); - fulfill(JSON.stringify(response)); - return; - }).catch(() => { - console.log("Error requesting bucket reachability credential from Lox server"); + console.log("Error making simple request: " + requested); reject(); }); }); From d8cdf3ccab4cfc44069d2ba0a9db2ef3061c0ad0 Mon Sep 17 00:00:00 2001 From: onyinyang Date: Tue, 14 Feb 2023 17:08:09 -0500 Subject: [PATCH 15/22] Logic for level up, reachability credential unimplemented --- crates/lox-wasm/index.js | 17 +++++++- crates/lox-wasm/src/lib.rs | 79 ++++++++++++++++++++++++++++++++++---- 2 files changed, 86 insertions(+), 10 deletions(-) diff --git a/crates/lox-wasm/index.js b/crates/lox-wasm/index.js index 7a5e8a1..423271b 100644 --- a/crates/lox-wasm/index.js +++ b/crates/lox-wasm/index.js @@ -5,6 +5,8 @@ import init, { handle_trust_promotion, trust_migration, handle_trust_migration, + level_up, + handle_level_up, set_panic_hook } from "./pkg/lox_wasm.js"; let pubkeys = await simple_request("/pubkeys"); console.log("Got pubkeys: " + pubkeys); @@ -38,12 +40,11 @@ let trust_promo_cred = await init().then(() => { return cred; }); -let reachability_cred = await simple_request("/reachability"); console.log("Got reachability credential: " + reachability_cred); let requested_trust_migration = trust_migration(open_lox_cred, trust_promo_cred, pubkeys); -let level_1_cred = await init().then(() => { +let level_one_cred = await init().then(() => { set_panic_hook(); let cred = requested_cred("/trustmig", requested_trust_migration).then((response)=> { console.log("Got new Level 1 Lox Credential: " + response); @@ -52,6 +53,18 @@ let level_1_cred = await init().then(() => { return cred; }); +let reachability_cred = await simple_request("/reachability"); +let requested_level_two = level_up(level_one_cred, reachability_cred, pubkeys); + +let level_two_cred = await init().then(() => { + set_panic_hook(); + let cred = requested_cred("/levelup", requested_level_two).then((response)=> { + console.log("Got new Level 2 Lox Credential: " + response); + return handle_level_up(requested_level_two, response, pubkeys); + }); + return cred; + }); + function requested_cred(command, requested) { return new Promise((fulfill, reject) => { let req = JSON.parse(requested); diff --git a/crates/lox-wasm/src/lib.rs b/crates/lox-wasm/src/lib.rs index 7308a1f..be96107 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -1,7 +1,7 @@ use chrono::{Duration, Utc}; use julianday::JulianDay; use lox::bridge_table::BridgeLine; -use lox::cred::Lox; +use lox::cred::{BucketReachability, Lox, Migration}; use lox::proto::{open_invite, trust_promotion, migration, level_up, issue_invite, redeem_invite, check_blockage, blockage_migration}; use lox::{IssuerPubKey, OPENINV_LENGTH}; @@ -30,6 +30,11 @@ struct MigReqState { state: migration::State, } +#[derive(Deserialize, Serialize)] +struct LevelupReqState { + request: level_up::Request, + state: level_up::State, +} #[derive(Debug, Deserialize, Serialize)] struct PubKeys { @@ -192,11 +197,8 @@ pub fn handle_trust_promotion( pub fn trust_migration(open_lox_cred: String, trust_promo_cred: String, lox_pub: String) -> Result { let lox_cred: Credential = serde_json::from_str(&open_lox_cred).unwrap(); let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap(); - let mig_cred = serde_json::from_str(&trust_promo_cred).unwrap(); - // To test creation of the credential we need to advance the day to 30 - // in production this should just use the today() function + let mig_cred: Migration = serde_json::from_str(&trust_promo_cred).unwrap(); let tm_result = - //CHANGE add_today(31) to today() for production match migration::request(&lox_cred.lox_credential, &mig_cred, &pubkeys.lox_pub, &pubkeys.migration_pub) { Ok(tm_result) => tm_result, Err(e) => { @@ -227,7 +229,7 @@ pub fn handle_trust_migration( let req_state: MigReqState = serde_json::from_str(&trust_migration_request).unwrap(); let deserialized_state = req_state.state; let deserialized_response = serde_json::from_str(&trust_migration_response).unwrap(); - let level_1_cred = + let level_one_cred = match migration::handle_response(deserialized_state, deserialized_response, &pubkeys.lox_pub) { Ok(level_1_cred) => level_1_cred, Err(e) => { @@ -238,10 +240,71 @@ pub fn handle_trust_migration( unsafe { log(&format!( "Got new Level 1 Credential: {}", - serde_json::to_string(&level_1_cred).unwrap() + serde_json::to_string(&level_one_cred).unwrap() )); } - Ok(serde_json::to_string(&level_1_cred).unwrap()) + Ok(serde_json::to_string(&level_one_cred).unwrap()) +} + +#[wasm_bindgen] +pub fn level_up(level_one_cred: String, reachability_cred: String, lox_pub: String) -> Result { + let lox_cred: Lox = serde_json::from_str(&level_one_cred).unwrap(); + let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap(); + let reach_cred: BucketReachability = serde_json::from_str(&reachability_cred).unwrap(); + // To test level up of the credential we need to advance the day to the correct interval + // In this case, the maximum of 85 can be used to test all level ups + // in production this should just use the today() function + log(&format!( + "TEST ONLY: Add 85 days to today's date: {}", + add_today(85) + )); + let lu_result = + //CHANGE add_today(31) to today() for production + match level_up::request(&lox_cred, &reach_cred, &pubkeys.lox_pub, &pubkeys.reachability_pub, add_today(85)) { + Ok(lu_result) => lu_result, + Err(e) => { + log(&format!("Error: {:?}", e.to_string())); + return Err(JsValue::from(e.to_string())); + } + }; + let req_state = LevelupReqState { + request: lu_result.0, + state: lu_result.1, + }; + unsafe { + log(&format!( + "Formatted Level Up request: {}", + serde_json::to_string(&req_state).unwrap() + )); + } + Ok(serde_json::to_string(&req_state).unwrap()) +} + +#[wasm_bindgen] +pub fn handle_level_up( + levelup_request: String, + levelup_response: String, + lox_pub: String +) -> Result { + let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap(); + let req_state: LevelupReqState = serde_json::from_str(&levelup_request).unwrap(); + let deserialized_state = req_state.state; + let deserialized_response = serde_json::from_str(&levelup_response).unwrap(); + let level_up_cred = + match level_up::handle_response(deserialized_state, deserialized_response, &pubkeys.lox_pub) { + Ok(level_up_cred) => level_up_cred, + Err(e) => { + log(&format!("Error: {:?}", e.to_string())); + return Err(JsValue::from(e.to_string())); + } + }; + unsafe { + log(&format!( + "Got new Level Up Credential: {}", + serde_json::to_string(&level_up_cred).unwrap() + )); + } + Ok(serde_json::to_string(&level_up_cred).unwrap()) } From 01a6cb37e1693cf3a7bf2e6aa39463964e9b6c00 Mon Sep 17 00:00:00 2001 From: onyinyang Date: Thu, 23 Feb 2023 13:18:11 -0500 Subject: [PATCH 16/22] Add working reachability cred, test credential aging needs work --- crates/lox-wasm/Cargo.toml | 2 ++ crates/lox-wasm/index.js | 6 +++--- crates/lox-wasm/src/lib.rs | 37 ++++++++++++++++++++++++++----------- 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/crates/lox-wasm/Cargo.toml b/crates/lox-wasm/Cargo.toml index c1d24a0..93c7d3c 100644 --- a/crates/lox-wasm/Cargo.toml +++ b/crates/lox-wasm/Cargo.toml @@ -11,11 +11,13 @@ crate-type = ["cdylib"] [dependencies] julianday = "1.2.0" +lazy_static = "1.4.0" lox = { git = "https://gitlab.torproject.org/onyinyang/lox.git", branch = "master" } wasm-bindgen = "0.2" time = "0.2" serde_json = "1.0.87" serde = "1" +serde_with = "1.9.1" serde-wasm-bindgen = "0.4.5" console_error_panic_hook = "0.1.7" diff --git a/crates/lox-wasm/index.js b/crates/lox-wasm/index.js index 423271b..5755e3b 100644 --- a/crates/lox-wasm/index.js +++ b/crates/lox-wasm/index.js @@ -40,7 +40,6 @@ let trust_promo_cred = await init().then(() => { return cred; }); -console.log("Got reachability credential: " + reachability_cred); let requested_trust_migration = trust_migration(open_lox_cred, trust_promo_cred, pubkeys); @@ -53,8 +52,9 @@ let level_one_cred = await init().then(() => { return cred; }); -let reachability_cred = await simple_request("/reachability"); -let requested_level_two = level_up(level_one_cred, reachability_cred, pubkeys); +let encrypted_table = await simple_request("/reachability"); +console.log("Got Encrypted Table: " + encrypted_table); +let requested_level_two = level_up(level_one_cred, encrypted_table, pubkeys); let level_two_cred = await init().then(() => { set_panic_hook(); diff --git a/crates/lox-wasm/src/lib.rs b/crates/lox-wasm/src/lib.rs index be96107..0bd94a3 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -1,17 +1,21 @@ -use chrono::{Duration, Utc}; +use chrono::{Duration, Utc, NaiveDate}; +use std::sync::atomic::{AtomicI64, Ordering}; use julianday::JulianDay; -use lox::bridge_table::BridgeLine; +use lazy_static::lazy_static; +use lox::bridge_table::{BridgeLine,from_scalar,BridgeTable, ENC_BUCKET_BYTES}; use lox::cred::{BucketReachability, Lox, Migration}; use lox::proto::{open_invite, trust_promotion, migration, level_up, issue_invite, redeem_invite, check_blockage, blockage_migration}; use lox::{IssuerPubKey, OPENINV_LENGTH}; use serde::{Deserialize, Serialize}; +use serde_with::{serde_as}; use serde_json; //use serde_wasm_bindgen; use std::array::TryFromSliceError; use std::{panic}; use wasm_bindgen::prelude::*; + #[derive(Deserialize, Serialize)] struct OpenReqState { request: open_invite::Request, @@ -45,6 +49,13 @@ struct PubKeys { invitation_pub: IssuerPubKey, } +#[serde_as] +#[derive(Serialize, Deserialize)] +pub struct EncBridgeTable { + #[serde_as(as = "Vec<[_; ENC_BUCKET_BYTES]>")] + pub etable: Vec<[u8; ENC_BUCKET_BYTES]>, +} + #[derive(Debug, Deserialize, Serialize)] struct Credential { lox_credential: Lox, @@ -58,8 +69,8 @@ fn today() -> u32 { } // This should only be used for testing, use today in production -fn add_today(sum: i64) -> u32 { - let naive_now_plus = (Utc::now() + Duration::days(sum)).date_naive(); +fn test_today(days: i64) -> u32 { + let naive_now_plus = (Utc::now() + Duration::days(days)).date_naive(); JulianDay::from(naive_now_plus).inner().try_into().unwrap() } @@ -144,11 +155,11 @@ pub fn trust_promotion(open_lox_cred: String, lox_pub: String) -> Result tp_result, Err(e) => { log(&format!("Error: {:?}", e.to_string())); @@ -247,20 +258,24 @@ pub fn handle_trust_migration( } #[wasm_bindgen] -pub fn level_up(level_one_cred: String, reachability_cred: String, lox_pub: String) -> Result { +pub fn level_up(level_one_cred: String, encrypted_table: String, lox_pub: String) -> Result { let lox_cred: Lox = serde_json::from_str(&level_one_cred).unwrap(); let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap(); - let reach_cred: BucketReachability = serde_json::from_str(&reachability_cred).unwrap(); + let (id, key) = from_scalar(lox_cred.bucket).unwrap(); + let enc_buckets: EncBridgeTable = serde_json::from_str(&encrypted_table).unwrap(); + let bucket = BridgeTable::decrypt_bucket(id, &key, &enc_buckets.etable[id as usize]).unwrap(); + let reach_cred = bucket.1.unwrap(); // To test level up of the credential we need to advance the day to the correct interval // In this case, the maximum of 85 can be used to test all level ups // in production this should just use the today() function + // decrypt trust level and use to calculate the correct date for now log(&format!( - "TEST ONLY: Add 85 days to today's date: {}", - add_today(85) + "TEST ONLY: Add 31 (open invitation) + x*85 days to today's date: {}", + test_today(31+85) )); let lu_result = //CHANGE add_today(31) to today() for production - match level_up::request(&lox_cred, &reach_cred, &pubkeys.lox_pub, &pubkeys.reachability_pub, add_today(85)) { + match level_up::request(&lox_cred, &reach_cred, &pubkeys.lox_pub, &pubkeys.reachability_pub, test_today(31+85)) { Ok(lu_result) => lu_result, Err(e) => { log(&format!("Error: {:?}", e.to_string())); From 66f7ae4c0dc914595953e8879ce81e9ba4cb20c9 Mon Sep 17 00:00:00 2001 From: onyinyang Date: Thu, 23 Feb 2023 22:16:45 -0500 Subject: [PATCH 17/22] Add up to level up 4 --- crates/lox-wasm/Cargo.toml | 1 + crates/lox-wasm/index.js | 29 +++++++++++++++++++++++++++++ crates/lox-wasm/src/lib.rs | 36 ++++++++++++++++++++++++++++++++---- 3 files changed, 62 insertions(+), 4 deletions(-) diff --git a/crates/lox-wasm/Cargo.toml b/crates/lox-wasm/Cargo.toml index 93c7d3c..e5712e5 100644 --- a/crates/lox-wasm/Cargo.toml +++ b/crates/lox-wasm/Cargo.toml @@ -23,6 +23,7 @@ serde-wasm-bindgen = "0.4.5" console_error_panic_hook = "0.1.7" js-sys = "0.3.61" rand = { version = "0.7", features = ["wasm-bindgen"] } +zkp = "0.8.0" [dependencies.chrono] version = "0.4.19" diff --git a/crates/lox-wasm/index.js b/crates/lox-wasm/index.js index 5755e3b..bfde485 100644 --- a/crates/lox-wasm/index.js +++ b/crates/lox-wasm/index.js @@ -65,6 +65,35 @@ let level_two_cred = await init().then(() => { return cred; }); +//Update reachability cred + encrypted_table = await simple_request("/reachability"); + console.log("Got Encrypted Table: " + encrypted_table); + let requested_level_three = level_up(level_two_cred, encrypted_table, pubkeys); + + let level_three_cred = await init().then(() => { + set_panic_hook(); + let cred = requested_cred("/levelup", requested_level_three).then((response)=> { + console.log("Got new Level 3 Lox Credential: " + response); + return handle_level_up(requested_level_three, response, pubkeys); + }); + return cred; + }); + + +//Update reachability cred +encrypted_table = await simple_request("/reachability"); +console.log("Got Encrypted Table: " + encrypted_table); +let requested_level_four = level_up(level_three_cred, encrypted_table, pubkeys); + +let level_four_cred = await init().then(() => { + set_panic_hook(); + let cred = requested_cred("/levelup", requested_level_four).then((response)=> { + console.log("Got new Level 4 Lox Credential: " + response); + return handle_level_up(requested_level_four, response, pubkeys); + }); + return cred; + }); + function requested_cred(command, requested) { return new Promise((fulfill, reject) => { let req = JSON.parse(requested); diff --git a/crates/lox-wasm/src/lib.rs b/crates/lox-wasm/src/lib.rs index 0bd94a3..4701b70 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -6,7 +6,7 @@ use lox::bridge_table::{BridgeLine,from_scalar,BridgeTable, ENC_BUCKET_BYTES}; use lox::cred::{BucketReachability, Lox, Migration}; use lox::proto::{open_invite, trust_promotion, migration, level_up, issue_invite, redeem_invite, check_blockage, blockage_migration}; -use lox::{IssuerPubKey, OPENINV_LENGTH}; +use lox::{IssuerPubKey, OPENINV_LENGTH, scalar_u32}; use serde::{Deserialize, Serialize}; use serde_with::{serde_as}; use serde_json; @@ -14,6 +14,7 @@ use serde_json; use std::array::TryFromSliceError; use std::{panic}; use wasm_bindgen::prelude::*; +use zkp::ProofError; #[derive(Deserialize, Serialize)] @@ -74,6 +75,23 @@ fn test_today(days: i64) -> u32 { JulianDay::from(naive_now_plus).inner().try_into().unwrap() } + +//pub const MAX_LEVEL: usize = 4; +//pub const LEVEL_INTERVAL: [u32; MAX_LEVEL + 1] = [0, 14, 28, 56, 84]; +fn calc_test_days(lox_cred: &Lox) -> Result { + let trust_level: i64 = match scalar_u32(&lox_cred.trust_level) { + Some(v) => v as i64, + None => return Err(ProofError::VerificationFailure), + }; + let mut total = 31; + // for level in 0..trust_level { + // let level_interval: u32 = LEVEL_INTERVAL[trust_level as usize]; + // total += level_interval; + total += trust_level*85; + // } + Ok(total) + } + #[wasm_bindgen] extern "C" { #[wasm_bindgen(js_namespace = console)] @@ -269,13 +287,22 @@ pub fn level_up(level_one_cred: String, encrypted_table: String, lox_pub: String // In this case, the maximum of 85 can be used to test all level ups // in production this should just use the today() function // decrypt trust level and use to calculate the correct date for now + // The trust level has to be at least 1 + + let test_cumulative_days = match calc_test_days(&lox_cred) { + Ok(v) => v, + Err(e) => { + log(&format!("Error: {:?}", e.to_string())); + return Err(JsValue::from(e.to_string())); + } + }; + log(&format!( - "TEST ONLY: Add 31 (open invitation) + x*85 days to today's date: {}", - test_today(31+85) + "TEST ONLY: Add 31 (open invitation) + Trust Level*85 days to today's date: {}", test_today(test_cumulative_days) )); let lu_result = //CHANGE add_today(31) to today() for production - match level_up::request(&lox_cred, &reach_cred, &pubkeys.lox_pub, &pubkeys.reachability_pub, test_today(31+85)) { + match level_up::request(&lox_cred, &reach_cred, &pubkeys.lox_pub, &pubkeys.reachability_pub, test_today(test_cumulative_days)) { Ok(lu_result) => lu_result, Err(e) => { log(&format!("Error: {:?}", e.to_string())); @@ -295,6 +322,7 @@ pub fn level_up(level_one_cred: String, encrypted_table: String, lox_pub: String Ok(serde_json::to_string(&req_state).unwrap()) } + #[wasm_bindgen] pub fn handle_level_up( levelup_request: String, From 944cbb77e44d5358b86a0f2f752cd52d0fe2c0b5 Mon Sep 17 00:00:00 2001 From: onyinyang Date: Fri, 24 Feb 2023 19:11:44 -0500 Subject: [PATCH 18/22] Add issue invitation --- crates/lox-wasm/index.js | 30 ++++++++++++++-- crates/lox-wasm/src/lib.rs | 74 +++++++++++++++++++++++++++++++++++--- 2 files changed, 97 insertions(+), 7 deletions(-) diff --git a/crates/lox-wasm/index.js b/crates/lox-wasm/index.js index bfde485..99ac848 100644 --- a/crates/lox-wasm/index.js +++ b/crates/lox-wasm/index.js @@ -7,10 +7,13 @@ import init, { handle_trust_migration, level_up, handle_level_up, + issue_invite, + handle_issue_invite, set_panic_hook } from "./pkg/lox_wasm.js"; let pubkeys = await simple_request("/pubkeys"); console.log("Got pubkeys: " + pubkeys); +// Get Lox Invitation let requested_invite = await init().then(() => { set_panic_hook(); let requested_invite = request_open_invite().then((token) => { @@ -20,6 +23,8 @@ let requested_invite = await init().then(() => { }); console.log("Got request and state: "+requested_invite); +// Redeem Lox Invitation for an Open Invitation Lox Credential +// Trust Level 0 let open_lox_cred = await init().then(() => { set_panic_hook(); let cred = requested_cred("/openreq", requested_invite).then((response) => { @@ -31,6 +36,7 @@ let open_lox_cred = await init().then(() => { let requested_trust_promo = trust_promotion(open_lox_cred, pubkeys); +// Get Migration credential for Trust Promotion from Trust Level 0 -> 1 let trust_promo_cred = await init().then(() => { set_panic_hook(); let cred = requested_cred("/trustpromo", requested_trust_promo).then((response)=> { @@ -43,6 +49,7 @@ let trust_promo_cred = await init().then(() => { let requested_trust_migration = trust_migration(open_lox_cred, trust_promo_cred, pubkeys); +// Trust Promotion from Trust Level 0 -> 1 let level_one_cred = await init().then(() => { set_panic_hook(); let cred = requested_cred("/trustmig", requested_trust_migration).then((response)=> { @@ -56,6 +63,7 @@ let encrypted_table = await simple_request("/reachability"); console.log("Got Encrypted Table: " + encrypted_table); let requested_level_two = level_up(level_one_cred, encrypted_table, pubkeys); +// Level Up to Trust Level 2 let level_two_cred = await init().then(() => { set_panic_hook(); let cred = requested_cred("/levelup", requested_level_two).then((response)=> { @@ -65,11 +73,12 @@ let level_two_cred = await init().then(() => { return cred; }); -//Update reachability cred +// Update reachability cred encrypted_table = await simple_request("/reachability"); console.log("Got Encrypted Table: " + encrypted_table); let requested_level_three = level_up(level_two_cred, encrypted_table, pubkeys); +// Level Up to Trust Level 3 let level_three_cred = await init().then(() => { set_panic_hook(); let cred = requested_cred("/levelup", requested_level_three).then((response)=> { @@ -80,11 +89,12 @@ let level_two_cred = await init().then(() => { }); -//Update reachability cred +// Update reachability cred encrypted_table = await simple_request("/reachability"); console.log("Got Encrypted Table: " + encrypted_table); let requested_level_four = level_up(level_three_cred, encrypted_table, pubkeys); +// Level Up to Trust Level 4 let level_four_cred = await init().then(() => { set_panic_hook(); let cred = requested_cred("/levelup", requested_level_four).then((response)=> { @@ -94,6 +104,22 @@ let level_four_cred = await init().then(() => { return cred; }); +// Update reachability cred +encrypted_table = await simple_request("/reachability"); +console.log("Got Encrypted Table: " + encrypted_table); +let requested_issue_invitation = issue_invite(level_four_cred, encrypted_table, pubkeys); + +// Issue an Invitation for a friend +let issue_invite_cred = await init().then(() => { + set_panic_hook(); + let cred = requested_cred("/issueinvite", requested_issue_invitation).then((response)=> { + console.log("Got new Invite and Lox Credential: " + response); + return handle_issue_invite(requested_issue_invitation, response, pubkeys); + }); + return cred; + }); + + function requested_cred(command, requested) { return new Promise((fulfill, reject) => { let req = JSON.parse(requested); diff --git a/crates/lox-wasm/src/lib.rs b/crates/lox-wasm/src/lib.rs index 4701b70..4dfeeba 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -41,6 +41,12 @@ struct LevelupReqState { state: level_up::State, } +#[derive(Deserialize, Serialize)] +struct IssueInviteReqState { + request: issue_invite::Request, + state: issue_invite::State, +} + #[derive(Debug, Deserialize, Serialize)] struct PubKeys { lox_pub: IssuerPubKey, @@ -275,20 +281,24 @@ pub fn handle_trust_migration( Ok(serde_json::to_string(&level_one_cred).unwrap()) } +fn generate_reachability_cred(lox_cred: &Lox, encrypted_table: String) -> BucketReachability { + let (id, key) = from_scalar(lox_cred.bucket).unwrap(); + let enc_buckets: EncBridgeTable = serde_json::from_str(&encrypted_table).unwrap(); + let bucket = BridgeTable::decrypt_bucket(id, &key, &enc_buckets.etable[id as usize]).unwrap(); + return bucket.1.unwrap() +} + #[wasm_bindgen] pub fn level_up(level_one_cred: String, encrypted_table: String, lox_pub: String) -> Result { let lox_cred: Lox = serde_json::from_str(&level_one_cred).unwrap(); let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap(); - let (id, key) = from_scalar(lox_cred.bucket).unwrap(); - let enc_buckets: EncBridgeTable = serde_json::from_str(&encrypted_table).unwrap(); - let bucket = BridgeTable::decrypt_bucket(id, &key, &enc_buckets.etable[id as usize]).unwrap(); - let reach_cred = bucket.1.unwrap(); + let reach_cred = generate_reachability_cred(&lox_cred, encrypted_table); + // To test level up of the credential we need to advance the day to the correct interval // In this case, the maximum of 85 can be used to test all level ups // in production this should just use the today() function // decrypt trust level and use to calculate the correct date for now // The trust level has to be at least 1 - let test_cumulative_days = match calc_test_days(&lox_cred) { Ok(v) => v, Err(e) => { @@ -350,6 +360,60 @@ pub fn handle_level_up( Ok(serde_json::to_string(&level_up_cred).unwrap()) } +#[wasm_bindgen] +pub fn issue_invite(trusted_cred: String, encrypted_table: String, lox_pub: String) -> Result { + let lox_cred: Lox = serde_json::from_str(&trusted_cred).unwrap(); + let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap(); + let reach_cred = generate_reachability_cred(&lox_cred, encrypted_table); + + let issue_result = + match issue_invite::request(&lox_cred, &reach_cred, &pubkeys.lox_pub, &pubkeys.reachability_pub, test_today(371)) { + Ok(issue_result) => issue_result, + Err(e) => { + log(&format!("Error: {:?}", e.to_string())); + return Err(JsValue::from(e.to_string())); + } + }; + let req_state = IssueInviteReqState { + request: issue_result.0, + state: issue_result.1, + }; + unsafe { + log(&format!( + "Formatted Issue Invite request: {}", + serde_json::to_string(&req_state).unwrap() + )); + } + Ok(serde_json::to_string(&req_state).unwrap()) +} + + +#[wasm_bindgen] +pub fn handle_issue_invite( + issue_invite_request: String, + issue_invite_response: String, + lox_pub: String +) -> Result { + let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap(); + let req_state: IssueInviteReqState = serde_json::from_str(&issue_invite_request).unwrap(); + let deserialized_state = req_state.state; + let deserialized_response = serde_json::from_str(&issue_invite_response).unwrap(); + let issue_invite_cred = + match issue_invite::handle_response(deserialized_state, deserialized_response, &pubkeys.lox_pub, &pubkeys.invitation_pub) { + Ok(issue_invite_cred) => issue_invite_cred, + Err(e) => { + log(&format!("Error: {:?}", e.to_string())); + return Err(JsValue::from(e.to_string())); + } + }; + unsafe { + log(&format!( + "Got new Invitation Credential and Lox Credential: {}", + serde_json::to_string(&issue_invite_cred).unwrap() + )); + } + Ok(serde_json::to_string(&issue_invite_cred).unwrap()) +} // This should also check the pubkey fn validate(invite: &[u8]) -> Result<[u8; OPENINV_LENGTH], TryFromSliceError> { From 8faf4fc2736e2c39aa6d28aaeb25e9f8bcfd8485 Mon Sep 17 00:00:00 2001 From: onyinyang Date: Mon, 27 Feb 2023 14:17:26 -0500 Subject: [PATCH 19/22] Add redeem invite --- crates/lox-wasm/index.js | 18 +++++++- crates/lox-wasm/src/lib.rs | 95 +++++++++++++++++++++++++++++++++++--- 2 files changed, 105 insertions(+), 8 deletions(-) diff --git a/crates/lox-wasm/index.js b/crates/lox-wasm/index.js index 99ac848..e380140 100644 --- a/crates/lox-wasm/index.js +++ b/crates/lox-wasm/index.js @@ -9,6 +9,9 @@ import init, { handle_level_up, issue_invite, handle_issue_invite, + prepare_invite, + redeem_invite, + handle_redeem_invite, set_panic_hook } from "./pkg/lox_wasm.js"; let pubkeys = await simple_request("/pubkeys"); console.log("Got pubkeys: " + pubkeys); @@ -109,7 +112,7 @@ encrypted_table = await simple_request("/reachability"); console.log("Got Encrypted Table: " + encrypted_table); let requested_issue_invitation = issue_invite(level_four_cred, encrypted_table, pubkeys); -// Issue an Invitation for a friend +// Issue an Invitation cred let issue_invite_cred = await init().then(() => { set_panic_hook(); let cred = requested_cred("/issueinvite", requested_issue_invitation).then((response)=> { @@ -119,6 +122,19 @@ let issue_invite_cred = await init().then(() => { return cred; }); +let prepare_invitation = prepare_invite(issue_invite_cred); +// Trusted Invitation Request +let requested_invitation = redeem_invite(prepare_invitation, pubkeys); +// Issue an Invitation cred +let lox_cred_from_invite = await init().then(() => { + set_panic_hook(); + let cred = requested_cred("/redeem", requested_invitation).then((response)=> { + console.log("Got new Invite and Lox Credential: " + response); + return handle_redeem_invite(requested_invitation, response, pubkeys); + }); + return cred; + }); + function requested_cred(command, requested) { return new Promise((fulfill, reject) => { diff --git a/crates/lox-wasm/src/lib.rs b/crates/lox-wasm/src/lib.rs index 4dfeeba..e838538 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -3,7 +3,7 @@ use std::sync::atomic::{AtomicI64, Ordering}; use julianday::JulianDay; use lazy_static::lazy_static; use lox::bridge_table::{BridgeLine,from_scalar,BridgeTable, ENC_BUCKET_BYTES}; -use lox::cred::{BucketReachability, Lox, Migration}; +use lox::cred::{BucketReachability, Invitation, Lox, Migration}; use lox::proto::{open_invite, trust_promotion, migration, level_up, issue_invite, redeem_invite, check_blockage, blockage_migration}; use lox::{IssuerPubKey, OPENINV_LENGTH, scalar_u32}; @@ -47,6 +47,12 @@ struct IssueInviteReqState { state: issue_invite::State, } +#[derive(Deserialize, Serialize)] +struct RedeemReqState { + request: redeem_invite::Request, + state: redeem_invite::State, +} + #[derive(Debug, Deserialize, Serialize)] struct PubKeys { lox_pub: IssuerPubKey, @@ -64,11 +70,16 @@ pub struct EncBridgeTable { } #[derive(Debug, Deserialize, Serialize)] -struct Credential { +struct OpenCredential { lox_credential: Lox, bridgeline: BridgeLine, } +#[derive(Debug, Deserialize, Serialize)] +struct InviteCredential { + lox_credential: Lox, + invitation: Invitation, +} fn today() -> u32 { let naive_now = Utc::now().date_naive(); @@ -154,7 +165,7 @@ pub fn handle_new_lox_credential( return Err(JsValue::from(e.to_string())); } }; - let lox_cred = Credential { + let lox_cred = OpenCredential { lox_credential: lox_cred.0, bridgeline: lox_cred.1, }; @@ -173,7 +184,7 @@ pub fn handle_new_lox_credential( #[wasm_bindgen] pub fn trust_promotion(open_lox_cred: String, lox_pub: String) -> Result { - let lox_cred: Credential = serde_json::from_str(&open_lox_cred).unwrap(); + let lox_cred: OpenCredential = serde_json::from_str(&open_lox_cred).unwrap(); let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap(); // To test creation of the credential we need to advance the day to 30 // in production this should just use the today() function @@ -230,7 +241,7 @@ pub fn handle_trust_promotion( #[wasm_bindgen] pub fn trust_migration(open_lox_cred: String, trust_promo_cred: String, lox_pub: String) -> Result { - let lox_cred: Credential = serde_json::from_str(&open_lox_cred).unwrap(); + let lox_cred: OpenCredential = serde_json::from_str(&open_lox_cred).unwrap(); let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap(); let mig_cred: Migration = serde_json::from_str(&trust_promo_cred).unwrap(); let tm_result = @@ -406,15 +417,85 @@ pub fn handle_issue_invite( return Err(JsValue::from(e.to_string())); } }; + let invitation_cred = InviteCredential { + lox_credential: issue_invite_cred.0, + invitation: issue_invite_cred.1, + }; + unsafe { log(&format!( "Got new Invitation Credential and Lox Credential: {}", - serde_json::to_string(&issue_invite_cred).unwrap() + serde_json::to_string(&invitation_cred).unwrap() )); } - Ok(serde_json::to_string(&issue_invite_cred).unwrap()) + Ok(serde_json::to_string(&invitation_cred).unwrap()) } +// Separate Trusted Invite from credential prior to passing it to friend +#[wasm_bindgen] +pub fn prepare_invite(invitation_cred: String) -> String { + let cred: InviteCredential = serde_json::from_str(&invitation_cred).unwrap(); + log(&format!( + "Prepared Invitation: {}", + serde_json::to_string(&cred.invitation).unwrap() + )); + serde_json::to_string(&cred.invitation).unwrap() +} + +// +#[wasm_bindgen] +pub fn redeem_invite(invitation: String, lox_pub: String) -> Result { + let invitation_cred: Invitation = serde_json::from_str(&invitation).unwrap(); + let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap(); + let redeem_result = + match redeem_invite::request(&invitation_cred, &pubkeys.invitation_pub, test_today(371)) { + Ok(redeem_result) => redeem_result, + Err(e) => { + log(&format!("Error: {:?}", e.to_string())); + return Err(JsValue::from(e.to_string())); + } + }; + let req_state = RedeemReqState { + request: redeem_result.0, + state: redeem_result.1, + }; + unsafe { + log(&format!( + "Formatted Redeem Invite request: {}", + serde_json::to_string(&req_state).unwrap() + )); + } + Ok(serde_json::to_string(&req_state).unwrap()) +} + +#[wasm_bindgen] +pub fn handle_redeem_invite( + redeem_invite_request: String, + redeem_invite_response: String, + lox_pub: String +) -> Result { + let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap(); + let req_state: RedeemReqState = serde_json::from_str(&redeem_invite_request).unwrap(); + let deserialized_state = req_state.state; + let deserialized_response = serde_json::from_str(&redeem_invite_response).unwrap(); + let redeem_invite_cred = + match redeem_invite::handle_response(deserialized_state, deserialized_response, &pubkeys.lox_pub) { + Ok(issue_invite_cred) => issue_invite_cred, + Err(e) => { + log(&format!("Error: {:?}", e.to_string())); + return Err(JsValue::from(e.to_string())); + } + }; + unsafe { + log(&format!( + "Got new Trusted Lox Credential from Invitation: {}", + serde_json::to_string(&redeem_invite_cred).unwrap() + )); + } + Ok(serde_json::to_string(&redeem_invite_cred).unwrap()) +} + + // This should also check the pubkey fn validate(invite: &[u8]) -> Result<[u8; OPENINV_LENGTH], TryFromSliceError> { invite.try_into() From 1bac11f1d0c86f6a1fda4db0a4c7afd4f441bd1f Mon Sep 17 00:00:00 2001 From: onyinyang Date: Mon, 27 Feb 2023 16:46:01 -0500 Subject: [PATCH 20/22] Refactor Lox Credential, Add check blockage --- crates/lox-wasm/index.js | 38 ++++++++---- crates/lox-wasm/src/lib.rs | 116 +++++++++++++++++++++++++++++-------- 2 files changed, 117 insertions(+), 37 deletions(-) diff --git a/crates/lox-wasm/index.js b/crates/lox-wasm/index.js index e380140..73f9f4a 100644 --- a/crates/lox-wasm/index.js +++ b/crates/lox-wasm/index.js @@ -12,6 +12,8 @@ import init, { prepare_invite, redeem_invite, handle_redeem_invite, + check_blockage, + handle_check_blockage, set_panic_hook } from "./pkg/lox_wasm.js"; let pubkeys = await simple_request("/pubkeys"); console.log("Got pubkeys: " + pubkeys); @@ -53,7 +55,7 @@ let trust_promo_cred = await init().then(() => { let requested_trust_migration = trust_migration(open_lox_cred, trust_promo_cred, pubkeys); // Trust Promotion from Trust Level 0 -> 1 -let level_one_cred = await init().then(() => { +let lox_cred = await init().then(() => { set_panic_hook(); let cred = requested_cred("/trustmig", requested_trust_migration).then((response)=> { console.log("Got new Level 1 Lox Credential: " + response); @@ -64,10 +66,10 @@ let level_one_cred = await init().then(() => { let encrypted_table = await simple_request("/reachability"); console.log("Got Encrypted Table: " + encrypted_table); -let requested_level_two = level_up(level_one_cred, encrypted_table, pubkeys); +let requested_level_two = level_up(lox_cred, encrypted_table, pubkeys); // Level Up to Trust Level 2 -let level_two_cred = await init().then(() => { +lox_cred = await init().then(() => { set_panic_hook(); let cred = requested_cred("/levelup", requested_level_two).then((response)=> { console.log("Got new Level 2 Lox Credential: " + response); @@ -79,10 +81,10 @@ let level_two_cred = await init().then(() => { // Update reachability cred encrypted_table = await simple_request("/reachability"); console.log("Got Encrypted Table: " + encrypted_table); - let requested_level_three = level_up(level_two_cred, encrypted_table, pubkeys); + let requested_level_three = level_up(lox_cred, encrypted_table, pubkeys); // Level Up to Trust Level 3 - let level_three_cred = await init().then(() => { + lox_cred = await init().then(() => { set_panic_hook(); let cred = requested_cred("/levelup", requested_level_three).then((response)=> { console.log("Got new Level 3 Lox Credential: " + response); @@ -95,10 +97,10 @@ let level_two_cred = await init().then(() => { // Update reachability cred encrypted_table = await simple_request("/reachability"); console.log("Got Encrypted Table: " + encrypted_table); -let requested_level_four = level_up(level_three_cred, encrypted_table, pubkeys); +let requested_level_four = level_up(lox_cred, encrypted_table, pubkeys); // Level Up to Trust Level 4 -let level_four_cred = await init().then(() => { +lox_cred = await init().then(() => { set_panic_hook(); let cred = requested_cred("/levelup", requested_level_four).then((response)=> { console.log("Got new Level 4 Lox Credential: " + response); @@ -110,10 +112,10 @@ let level_four_cred = await init().then(() => { // Update reachability cred encrypted_table = await simple_request("/reachability"); console.log("Got Encrypted Table: " + encrypted_table); -let requested_issue_invitation = issue_invite(level_four_cred, encrypted_table, pubkeys); +let requested_issue_invitation = issue_invite(lox_cred, encrypted_table, pubkeys); // Issue an Invitation cred -let issue_invite_cred = await init().then(() => { +lox_cred = await init().then(() => { set_panic_hook(); let cred = requested_cred("/issueinvite", requested_issue_invitation).then((response)=> { console.log("Got new Invite and Lox Credential: " + response); @@ -122,19 +124,31 @@ let issue_invite_cred = await init().then(() => { return cred; }); -let prepare_invitation = prepare_invite(issue_invite_cred); +let prepared_invitation = prepare_invite(lox_cred); // Trusted Invitation Request -let requested_invitation = redeem_invite(prepare_invitation, pubkeys); +let requested_invitation = redeem_invite(prepared_invitation, pubkeys); // Issue an Invitation cred let lox_cred_from_invite = await init().then(() => { set_panic_hook(); let cred = requested_cred("/redeem", requested_invitation).then((response)=> { - console.log("Got new Invite and Lox Credential: " + response); + console.log("Got new Trusted Lox Credential Invite: " + response); return handle_redeem_invite(requested_invitation, response, pubkeys); }); return cred; }); + let requested_check_blockage = check_blockage(lox_cred, pubkeys); + + // Check whether or not a bucket is blocked + let check_cred = await init().then(() => { + set_panic_hook(); + let cred = requested_cred("/checkblockage", requested_check_blockage).then((response)=> { + console.log("Got check blockage Migration Credential: " + response); + return handle_check_blockage(requested_check_blockage, response); + }); + return cred; + }); + function requested_cred(command, requested) { return new Promise((fulfill, reject) => { diff --git a/crates/lox-wasm/src/lib.rs b/crates/lox-wasm/src/lib.rs index e838538..a75b428 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -53,6 +53,13 @@ struct RedeemReqState { state: redeem_invite::State, } +#[derive(Deserialize, Serialize)] +struct CheckBlockageReqState { + request: check_blockage::Request, + state: check_blockage::State, +} + + #[derive(Debug, Deserialize, Serialize)] struct PubKeys { lox_pub: IssuerPubKey, @@ -70,15 +77,10 @@ pub struct EncBridgeTable { } #[derive(Debug, Deserialize, Serialize)] -struct OpenCredential { +struct LoxCredential { lox_credential: Lox, - bridgeline: BridgeLine, -} - -#[derive(Debug, Deserialize, Serialize)] -struct InviteCredential { - lox_credential: Lox, - invitation: Invitation, + bridgeline: Option, + invitation: Option, } fn today() -> u32 { @@ -165,9 +167,10 @@ pub fn handle_new_lox_credential( return Err(JsValue::from(e.to_string())); } }; - let lox_cred = OpenCredential { + let lox_cred = LoxCredential { lox_credential: lox_cred.0, - bridgeline: lox_cred.1, + bridgeline: Some(lox_cred.1), + invitation: None, }; unsafe { log(&format!( @@ -184,7 +187,7 @@ pub fn handle_new_lox_credential( #[wasm_bindgen] pub fn trust_promotion(open_lox_cred: String, lox_pub: String) -> Result { - let lox_cred: OpenCredential = serde_json::from_str(&open_lox_cred).unwrap(); + let lox_cred: LoxCredential = serde_json::from_str(&open_lox_cred).unwrap(); let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap(); // To test creation of the credential we need to advance the day to 30 // in production this should just use the today() function @@ -241,7 +244,7 @@ pub fn handle_trust_promotion( #[wasm_bindgen] pub fn trust_migration(open_lox_cred: String, trust_promo_cred: String, lox_pub: String) -> Result { - let lox_cred: OpenCredential = serde_json::from_str(&open_lox_cred).unwrap(); + let lox_cred: LoxCredential = serde_json::from_str(&open_lox_cred).unwrap(); let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap(); let mig_cred: Migration = serde_json::from_str(&trust_promo_cred).unwrap(); let tm_result = @@ -277,7 +280,11 @@ pub fn handle_trust_migration( let deserialized_response = serde_json::from_str(&trust_migration_response).unwrap(); let level_one_cred = match migration::handle_response(deserialized_state, deserialized_response, &pubkeys.lox_pub) { - Ok(level_1_cred) => level_1_cred, + Ok(level_1_cred) => LoxCredential { + lox_credential: level_1_cred, + bridgeline: None, + invitation: None, + }, Err(e) => { log(&format!("Error: {:?}", e.to_string())); return Err(JsValue::from(e.to_string())); @@ -301,16 +308,16 @@ fn generate_reachability_cred(lox_cred: &Lox, encrypted_table: String) -> Bucket #[wasm_bindgen] pub fn level_up(level_one_cred: String, encrypted_table: String, lox_pub: String) -> Result { - let lox_cred: Lox = serde_json::from_str(&level_one_cred).unwrap(); + let lox_cred: LoxCredential = serde_json::from_str(&level_one_cred).unwrap(); let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap(); - let reach_cred = generate_reachability_cred(&lox_cred, encrypted_table); + let reach_cred = generate_reachability_cred(&lox_cred.lox_credential, encrypted_table); // To test level up of the credential we need to advance the day to the correct interval // In this case, the maximum of 85 can be used to test all level ups // in production this should just use the today() function // decrypt trust level and use to calculate the correct date for now // The trust level has to be at least 1 - let test_cumulative_days = match calc_test_days(&lox_cred) { + let test_cumulative_days = match calc_test_days(&lox_cred.lox_credential) { Ok(v) => v, Err(e) => { log(&format!("Error: {:?}", e.to_string())); @@ -323,7 +330,7 @@ pub fn level_up(level_one_cred: String, encrypted_table: String, lox_pub: String )); let lu_result = //CHANGE add_today(31) to today() for production - match level_up::request(&lox_cred, &reach_cred, &pubkeys.lox_pub, &pubkeys.reachability_pub, test_today(test_cumulative_days)) { + match level_up::request(&lox_cred.lox_credential, &reach_cred, &pubkeys.lox_pub, &pubkeys.reachability_pub, test_today(test_cumulative_days)) { Ok(lu_result) => lu_result, Err(e) => { log(&format!("Error: {:?}", e.to_string())); @@ -356,7 +363,11 @@ pub fn handle_level_up( let deserialized_response = serde_json::from_str(&levelup_response).unwrap(); let level_up_cred = match level_up::handle_response(deserialized_state, deserialized_response, &pubkeys.lox_pub) { - Ok(level_up_cred) => level_up_cred, + Ok(level_up_cred) => LoxCredential { + lox_credential: level_up_cred, + bridgeline: None, + invitation: None, + }, Err(e) => { log(&format!("Error: {:?}", e.to_string())); return Err(JsValue::from(e.to_string())); @@ -373,12 +384,12 @@ pub fn handle_level_up( #[wasm_bindgen] pub fn issue_invite(trusted_cred: String, encrypted_table: String, lox_pub: String) -> Result { - let lox_cred: Lox = serde_json::from_str(&trusted_cred).unwrap(); + let lox_cred: LoxCredential = serde_json::from_str(&trusted_cred).unwrap(); let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap(); - let reach_cred = generate_reachability_cred(&lox_cred, encrypted_table); + let reach_cred = generate_reachability_cred(&lox_cred.lox_credential, encrypted_table); let issue_result = - match issue_invite::request(&lox_cred, &reach_cred, &pubkeys.lox_pub, &pubkeys.reachability_pub, test_today(371)) { + match issue_invite::request(&lox_cred.lox_credential, &reach_cred, &pubkeys.lox_pub, &pubkeys.reachability_pub, test_today(371)) { Ok(issue_result) => issue_result, Err(e) => { log(&format!("Error: {:?}", e.to_string())); @@ -417,9 +428,10 @@ pub fn handle_issue_invite( return Err(JsValue::from(e.to_string())); } }; - let invitation_cred = InviteCredential { + let invitation_cred = LoxCredential { lox_credential: issue_invite_cred.0, - invitation: issue_invite_cred.1, + bridgeline: None, + invitation: Some(issue_invite_cred.1), }; unsafe { @@ -434,7 +446,7 @@ pub fn handle_issue_invite( // Separate Trusted Invite from credential prior to passing it to friend #[wasm_bindgen] pub fn prepare_invite(invitation_cred: String) -> String { - let cred: InviteCredential = serde_json::from_str(&invitation_cred).unwrap(); + let cred: LoxCredential = serde_json::from_str(&invitation_cred).unwrap(); log(&format!( "Prepared Invitation: {}", serde_json::to_string(&cred.invitation).unwrap() @@ -480,7 +492,11 @@ pub fn handle_redeem_invite( let deserialized_response = serde_json::from_str(&redeem_invite_response).unwrap(); let redeem_invite_cred = match redeem_invite::handle_response(deserialized_state, deserialized_response, &pubkeys.lox_pub) { - Ok(issue_invite_cred) => issue_invite_cred, + Ok(issue_invite_cred) => LoxCredential { + lox_credential: issue_invite_cred, + bridgeline: None, + invitation: None, + }, Err(e) => { log(&format!("Error: {:?}", e.to_string())); return Err(JsValue::from(e.to_string())); @@ -496,6 +512,56 @@ pub fn handle_redeem_invite( } +#[wasm_bindgen] +pub fn check_blockage(lox_cred: String, lox_pub: String) -> Result { + let lox: LoxCredential = serde_json::from_str(&lox_cred).unwrap(); + let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap(); + let cb_result = + match check_blockage::request(&lox.lox_credential, &pubkeys.lox_pub) { + Ok(cb_result) => cb_result, + Err(e) => { + log(&format!("Error: {:?}", e.to_string())); + return Err(JsValue::from(e.to_string())); + } + }; + let req_state = CheckBlockageReqState { + request: cb_result.0, + state: cb_result.1, + }; + unsafe { + log(&format!( + "Formatted Trust Promotion request: {}", + serde_json::to_string(&req_state).unwrap() + )); + } + Ok(serde_json::to_string(&req_state).unwrap()) +} + +#[wasm_bindgen] +pub fn handle_check_blockage( + check_blockage_request: String, + check_blockage_response: String, +) -> Result { + let req_state: CheckBlockageReqState = serde_json::from_str(&check_blockage_request).unwrap(); + let deserialized_state = req_state.state; + let deserialized_response = serde_json::from_str(&check_blockage_response).unwrap(); + let migration_cred = + match check_blockage::handle_response(deserialized_state, deserialized_response) { + Ok(migration_cred) => migration_cred, + Err(e) => { + log(&format!("Error: {:?}", e.to_string())); + return Err(JsValue::from(e.to_string())); + } + }; + unsafe { + log(&format!( + "Got new Migration Credential: {}", + serde_json::to_string(&migration_cred).unwrap() + )); + } + Ok(serde_json::to_string(&migration_cred).unwrap()) +} + // This should also check the pubkey fn validate(invite: &[u8]) -> Result<[u8; OPENINV_LENGTH], TryFromSliceError> { invite.try_into() From dd00f99958d80f12b05cf18474d2c54aa264d557 Mon Sep 17 00:00:00 2001 From: onyinyang Date: Mon, 27 Feb 2023 16:57:23 -0500 Subject: [PATCH 21/22] Add blockage migration (untested) --- crates/lox-wasm/index.js | 16 ++++++++++- crates/lox-wasm/src/lib.rs | 58 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/crates/lox-wasm/index.js b/crates/lox-wasm/index.js index 73f9f4a..ab3045f 100644 --- a/crates/lox-wasm/index.js +++ b/crates/lox-wasm/index.js @@ -14,6 +14,8 @@ import init, { handle_redeem_invite, check_blockage, handle_check_blockage, + blockage_migration, + handle_blockage_migration, set_panic_hook } from "./pkg/lox_wasm.js"; let pubkeys = await simple_request("/pubkeys"); console.log("Got pubkeys: " + pubkeys); @@ -140,7 +142,7 @@ let lox_cred_from_invite = await init().then(() => { let requested_check_blockage = check_blockage(lox_cred, pubkeys); // Check whether or not a bucket is blocked - let check_cred = await init().then(() => { + let check_migration_cred = await init().then(() => { set_panic_hook(); let cred = requested_cred("/checkblockage", requested_check_blockage).then((response)=> { console.log("Got check blockage Migration Credential: " + response); @@ -149,6 +151,18 @@ let lox_cred_from_invite = await init().then(() => { return cred; }); +let requested_blockage_migration = blockage_migration(lox_cred, check_migration_cred, pubkeys); + + // Migrate to a new unblocked bridge + lox_cred = await init().then(() => { + set_panic_hook(); + let cred = requested_cred("/blockagemigration", requested_blockage_migration).then((response)=> { + console.log("Got Lox Credential for new bucket: " + response); + return handle_blockage_migration(requested_check_blockage, response); + }); + return cred; + }); + function requested_cred(command, requested) { return new Promise((fulfill, reject) => { diff --git a/crates/lox-wasm/src/lib.rs b/crates/lox-wasm/src/lib.rs index a75b428..e7547d8 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -562,6 +562,64 @@ pub fn handle_check_blockage( Ok(serde_json::to_string(&migration_cred).unwrap()) } + +#[wasm_bindgen] +pub fn blockage_migration(lox_cred: String, check_migration_cred: String, lox_pub: String) -> Result { + let lox_cred: LoxCredential = serde_json::from_str(&lox_cred).unwrap(); + let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap(); + let mig_cred: Migration = serde_json::from_str(&check_migration_cred).unwrap(); + let bm_result = + match migration::request(&lox_cred.lox_credential, &mig_cred, &pubkeys.lox_pub, &pubkeys.migration_pub) { + Ok(tm_result) => tm_result, + Err(e) => { + log(&format!("Error: {:?}", e.to_string())); + return Err(JsValue::from(e.to_string())); + } + }; + let req_state = MigReqState { + request: bm_result.0, + state: bm_result.1, + }; + unsafe { + log(&format!( + "Formatted Trust Migration request: {}", + serde_json::to_string(&req_state).unwrap() + )); + } + Ok(serde_json::to_string(&req_state).unwrap()) +} + +#[wasm_bindgen] +pub fn handle_blockage_migration( + blockage_migration_request: String, + blockage_migration_response: String, + lox_pub: String +) -> Result { + let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap(); + let req_state: MigReqState = serde_json::from_str(&blockage_migration_request).unwrap(); + let deserialized_state = req_state.state; + let deserialized_response = serde_json::from_str(&blockage_migration_response).unwrap(); + let lox_cred = + match migration::handle_response(deserialized_state, deserialized_response, &pubkeys.lox_pub) { + Ok(lox_cred) => LoxCredential { + lox_credential: lox_cred, + bridgeline: None, + invitation: None, + }, + Err(e) => { + log(&format!("Error: {:?}", e.to_string())); + return Err(JsValue::from(e.to_string())); + } + }; + unsafe { + log(&format!( + "Got new Level 1 Credential: {}", + serde_json::to_string(&lox_cred).unwrap() + )); + } + Ok(serde_json::to_string(&lox_cred).unwrap()) +} + // This should also check the pubkey fn validate(invite: &[u8]) -> Result<[u8; OPENINV_LENGTH], TryFromSliceError> { invite.try_into() From a9de52a266d8c6b3cea085bd8030d9e5b2b80fbe Mon Sep 17 00:00:00 2001 From: onyinyang Date: Tue, 28 Feb 2023 12:57:57 -0500 Subject: [PATCH 22/22] Blockage Migration tested and fixed up --- crates/lox-wasm/index.js | 4 ++-- crates/lox-wasm/src/lib.rs | 25 ++++++++++++++++--------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/crates/lox-wasm/index.js b/crates/lox-wasm/index.js index ab3045f..89e83ef 100644 --- a/crates/lox-wasm/index.js +++ b/crates/lox-wasm/index.js @@ -129,7 +129,7 @@ lox_cred = await init().then(() => { let prepared_invitation = prepare_invite(lox_cred); // Trusted Invitation Request let requested_invitation = redeem_invite(prepared_invitation, pubkeys); -// Issue an Invitation cred +// Redeem an Invitation cred let lox_cred_from_invite = await init().then(() => { set_panic_hook(); let cred = requested_cred("/redeem", requested_invitation).then((response)=> { @@ -158,7 +158,7 @@ let requested_blockage_migration = blockage_migration(lox_cred, check_migration_ set_panic_hook(); let cred = requested_cred("/blockagemigration", requested_blockage_migration).then((response)=> { console.log("Got Lox Credential for new bucket: " + response); - return handle_blockage_migration(requested_check_blockage, response); + return handle_blockage_migration(requested_blockage_migration, response, pubkeys); }); return cred; }); diff --git a/crates/lox-wasm/src/lib.rs b/crates/lox-wasm/src/lib.rs index e7547d8..74bd687 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -59,6 +59,13 @@ struct CheckBlockageReqState { state: check_blockage::State, } +#[derive(Deserialize, Serialize)] +struct BlockageMigReqState { + request: blockage_migration::Request, + state: blockage_migration::State, +} + + #[derive(Debug, Deserialize, Serialize)] struct PubKeys { @@ -530,7 +537,7 @@ pub fn check_blockage(lox_cred: String, lox_pub: String) -> Result tm_result, + match blockage_migration::request(&lox_cred.lox_credential, &mig_cred, &pubkeys.lox_pub, &pubkeys.migration_pub) { + Ok(bm_result) => bm_result, Err(e) => { log(&format!("Error: {:?}", e.to_string())); return Err(JsValue::from(e.to_string())); } }; - let req_state = MigReqState { + let req_state = BlockageMigReqState { request: bm_result.0, state: bm_result.1, }; unsafe { log(&format!( - "Formatted Trust Migration request: {}", + "Formatted Blockage Migration request: {}", serde_json::to_string(&req_state).unwrap() )); } @@ -596,11 +603,11 @@ pub fn handle_blockage_migration( lox_pub: String ) -> Result { let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap(); - let req_state: MigReqState = serde_json::from_str(&blockage_migration_request).unwrap(); + let req_state: BlockageMigReqState = serde_json::from_str(&blockage_migration_request).unwrap(); let deserialized_state = req_state.state; let deserialized_response = serde_json::from_str(&blockage_migration_response).unwrap(); let lox_cred = - match migration::handle_response(deserialized_state, deserialized_response, &pubkeys.lox_pub) { + match blockage_migration::handle_response(deserialized_state, deserialized_response, &pubkeys.lox_pub) { Ok(lox_cred) => LoxCredential { lox_credential: lox_cred, bridgeline: None, @@ -613,7 +620,7 @@ pub fn handle_blockage_migration( }; unsafe { log(&format!( - "Got new Level 1 Credential: {}", + "Got new Lox Credential after Migration: {}", serde_json::to_string(&lox_cred).unwrap() )); }