From 01a6cb37e1693cf3a7bf2e6aa39463964e9b6c00 Mon Sep 17 00:00:00 2001 From: onyinyang Date: Thu, 23 Feb 2023 13:18:11 -0500 Subject: [PATCH] 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()));