2024-02-20 17:12:34 -05:00
|
|
|
// For Lox-related code where points are uppercase and scalars are lowercase
|
|
|
|
#![allow(non_snake_case)]
|
2024-02-07 18:35:53 -05:00
|
|
|
|
2024-04-12 02:38:35 -04:00
|
|
|
use crate::{
|
|
|
|
bridge_verification_info::BridgeVerificationInfo, get_date, COUNTRY_CODES, MAX_BACKDATE,
|
|
|
|
};
|
2024-02-20 17:12:34 -05:00
|
|
|
|
2024-02-28 15:26:22 -05:00
|
|
|
use curve25519_dalek::ristretto::RistrettoBasepointTable;
|
|
|
|
use ed25519_dalek::{Signature, Signer, SigningKey, Verifier};
|
2024-02-28 16:06:58 -05:00
|
|
|
use lox_library::{cred::Lox, proto::positive_report as lox_pr, BridgeAuth, IssuerPubKey};
|
2024-02-07 18:35:53 -05:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use sha1::{Digest, Sha1};
|
|
|
|
use std::option::Option;
|
|
|
|
|
2024-03-15 13:24:00 -04:00
|
|
|
pub const REQUIRE_BRIDGE_TOKEN: bool = false;
|
|
|
|
|
2024-03-22 23:42:22 -04:00
|
|
|
#[derive(Debug, Serialize)]
|
2024-02-07 18:35:53 -05:00
|
|
|
pub enum PositiveReportError {
|
|
|
|
DateInFuture,
|
2024-04-12 02:38:35 -04:00
|
|
|
DateInPast, // report is more than MAX_BACKDATE days old
|
2024-04-16 01:12:39 -04:00
|
|
|
FailedToDeserialize, // couldn't deserialize to PositiveReport
|
2024-02-21 03:09:34 -05:00
|
|
|
InvalidBridgeToken,
|
2024-02-07 18:35:53 -05:00
|
|
|
InvalidCountryCode,
|
2024-02-26 18:00:43 -05:00
|
|
|
InvalidLoxProof,
|
2024-02-07 18:35:53 -05:00
|
|
|
MissingBridgeToken,
|
|
|
|
MissingCountryCode,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A report that the user was able to connect to the bridge
|
|
|
|
pub struct PositiveReport {
|
|
|
|
/// hashed fingerprint (SHA-1 hash of 20-byte bridge ID)
|
|
|
|
pub fingerprint: [u8; 20],
|
2024-02-25 17:42:30 -05:00
|
|
|
|
2024-02-07 18:35:53 -05:00
|
|
|
/// token from the bridge indicating it was reached
|
|
|
|
bridge_token: Option<BridgeToken>,
|
2024-02-25 17:42:30 -05:00
|
|
|
|
2024-02-07 18:35:53 -05:00
|
|
|
// proof of Lox cred with level >= 3 and this bridge
|
|
|
|
lox_proof: lox_pr::Request,
|
2024-02-25 17:42:30 -05:00
|
|
|
|
2024-02-07 18:35:53 -05:00
|
|
|
/// user's country code, may be an empty string
|
|
|
|
pub country: String,
|
2024-02-25 17:42:30 -05:00
|
|
|
|
2024-02-07 18:35:53 -05:00
|
|
|
/// today's Julian date
|
|
|
|
pub date: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PositiveReport {
|
|
|
|
pub fn new(
|
|
|
|
bridge_id: [u8; 20],
|
|
|
|
bridge_token: Option<BridgeToken>,
|
|
|
|
lox_proof: lox_pr::Request,
|
|
|
|
country: String,
|
|
|
|
) -> Self {
|
2024-03-15 13:24:00 -04:00
|
|
|
//if CONFIG.require_bridge_token && bridge_token.is_none() {
|
|
|
|
if REQUIRE_BRIDGE_TOKEN && bridge_token.is_none() {
|
2024-02-07 18:35:53 -05:00
|
|
|
panic!("Bridge tokens are required for positive reports.");
|
|
|
|
}
|
|
|
|
let mut hasher = Sha1::new();
|
|
|
|
hasher.update(bridge_id);
|
|
|
|
let fingerprint: [u8; 20] = hasher.finalize().into();
|
|
|
|
let date = get_date();
|
|
|
|
Self {
|
|
|
|
fingerprint,
|
|
|
|
bridge_token,
|
|
|
|
lox_proof,
|
|
|
|
country,
|
|
|
|
date,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_lox_credential(
|
|
|
|
bridge_id: [u8; 20],
|
|
|
|
bridge_token: Option<BridgeToken>,
|
|
|
|
lox_cred: &Lox,
|
|
|
|
lox_pub: &IssuerPubKey,
|
|
|
|
country: String,
|
2024-03-29 16:11:44 -04:00
|
|
|
) -> Result<Self, lox_library::proto::positive_report::requestproof::ProofError> {
|
|
|
|
let lox_proof = lox_pr::request(lox_cred, lox_pub)?;
|
2024-03-29 16:14:11 -04:00
|
|
|
Ok(PositiveReport::new(
|
|
|
|
bridge_id,
|
|
|
|
bridge_token,
|
|
|
|
lox_proof,
|
|
|
|
country,
|
|
|
|
))
|
2024-02-07 18:35:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Convert report to a serializable version
|
|
|
|
pub fn to_serializable_report(self) -> SerializablePositiveReport {
|
2024-02-21 03:09:34 -05:00
|
|
|
let bridge_token = if self.bridge_token.is_none() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(self.bridge_token.unwrap().to_serializable_bridge_token())
|
|
|
|
};
|
2024-02-07 18:35:53 -05:00
|
|
|
SerializablePositiveReport {
|
|
|
|
fingerprint: self.fingerprint,
|
2024-02-21 03:09:34 -05:00
|
|
|
bridge_token: bridge_token,
|
2024-02-07 18:35:53 -05:00
|
|
|
lox_proof: self.lox_proof,
|
|
|
|
country: self.country,
|
|
|
|
date: self.date,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Serializes the report, eliding the underlying process
|
|
|
|
pub fn to_json(self) -> String {
|
|
|
|
serde_json::to_string(&self.to_serializable_report()).unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Deserializes the report, eliding the underlying process
|
|
|
|
pub fn from_json(str: String) -> Result<Self, PositiveReportError> {
|
|
|
|
match serde_json::from_str::<SerializablePositiveReport>(&str) {
|
|
|
|
Ok(v) => v.to_report(),
|
|
|
|
Err(_) => Err(PositiveReportError::FailedToDeserialize),
|
|
|
|
}
|
|
|
|
}
|
2024-02-20 17:12:34 -05:00
|
|
|
|
2024-03-22 23:42:22 -04:00
|
|
|
/// Deserializes the report from slice, eliding the underlying process
|
|
|
|
pub fn from_slice(slice: &[u8]) -> Result<Self, PositiveReportError> {
|
|
|
|
match serde_json::from_slice::<SerializablePositiveReport>(&slice) {
|
|
|
|
Ok(v) => v.to_report(),
|
|
|
|
Err(_) => Err(PositiveReportError::FailedToDeserialize),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-28 16:06:58 -05:00
|
|
|
/// Verify report
|
|
|
|
pub fn verify(
|
2024-02-20 17:12:34 -05:00
|
|
|
self,
|
2024-02-28 16:06:58 -05:00
|
|
|
la: &mut BridgeAuth,
|
2024-03-15 11:32:17 -04:00
|
|
|
bridge_info: &BridgeVerificationInfo,
|
2024-02-28 11:57:48 -05:00
|
|
|
Htable: &RistrettoBasepointTable,
|
2024-02-20 17:12:34 -05:00
|
|
|
) -> bool {
|
|
|
|
// Verify bridge token
|
2024-03-15 13:24:00 -04:00
|
|
|
//if CONFIG.require_bridge_token {
|
|
|
|
if REQUIRE_BRIDGE_TOKEN {
|
2024-02-20 17:12:34 -05:00
|
|
|
let bridge_token = self.bridge_token.unwrap();
|
2024-02-28 15:26:22 -05:00
|
|
|
let bridge_key = bridge_info.pubkey;
|
2024-02-20 17:12:34 -05:00
|
|
|
if bridge_key.is_none() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if bridge_key
|
|
|
|
.unwrap()
|
|
|
|
.verify(
|
2024-02-21 03:09:34 -05:00
|
|
|
&bridge_token.unsigned_bridge_token.to_bincode(),
|
2024-02-20 17:12:34 -05:00
|
|
|
&bridge_token.sig,
|
|
|
|
)
|
|
|
|
.is_err()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Verify knowledge of bucket ID
|
2024-03-15 02:24:52 -04:00
|
|
|
let buckets = &bridge_info.buckets;
|
2024-02-20 17:12:34 -05:00
|
|
|
let BP = self.lox_proof.BP;
|
2024-03-15 02:24:52 -04:00
|
|
|
for bucket in buckets {
|
2024-03-29 15:25:42 -04:00
|
|
|
if bucket * Htable == BP {
|
2024-03-15 02:24:52 -04:00
|
|
|
return la.handle_positive_report(self.lox_proof, &Htable).is_ok();
|
|
|
|
}
|
2024-02-20 17:12:34 -05:00
|
|
|
}
|
2024-03-15 02:24:52 -04:00
|
|
|
false
|
2024-02-20 17:12:34 -05:00
|
|
|
}
|
2024-02-07 18:35:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// (De)serializable positive report object which must be consumed by the
|
|
|
|
/// checking function before it can be used
|
|
|
|
#[derive(Deserialize, Serialize)]
|
|
|
|
pub struct SerializablePositiveReport {
|
|
|
|
pub fingerprint: [u8; 20],
|
2024-02-21 03:09:34 -05:00
|
|
|
bridge_token: Option<SerializableBridgeToken>,
|
2024-02-07 18:35:53 -05:00
|
|
|
lox_proof: lox_pr::Request,
|
|
|
|
pub country: String,
|
|
|
|
pub date: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SerializablePositiveReport {
|
|
|
|
pub fn to_report(self) -> Result<PositiveReport, PositiveReportError> {
|
2024-02-21 03:09:34 -05:00
|
|
|
// Check that fields are valid
|
2024-03-15 13:24:00 -04:00
|
|
|
//if CONFIG.require_bridge_token && self.bridge_token.is_none() {
|
|
|
|
if REQUIRE_BRIDGE_TOKEN && self.bridge_token.is_none() {
|
2024-02-07 18:35:53 -05:00
|
|
|
return Err(PositiveReportError::MissingBridgeToken);
|
|
|
|
}
|
|
|
|
if self.country == "" {
|
|
|
|
return Err(PositiveReportError::MissingCountryCode);
|
|
|
|
}
|
|
|
|
if !COUNTRY_CODES.contains(self.country.as_str()) {
|
|
|
|
return Err(PositiveReportError::InvalidCountryCode);
|
|
|
|
}
|
2024-04-12 02:38:35 -04:00
|
|
|
let date: u32 = get_date();
|
2024-02-26 18:00:43 -05:00
|
|
|
if self.date > date {
|
2024-02-07 18:35:53 -05:00
|
|
|
return Err(PositiveReportError::DateInFuture);
|
|
|
|
}
|
2024-04-12 02:38:35 -04:00
|
|
|
if self.date < date - MAX_BACKDATE {
|
|
|
|
return Err(PositiveReportError::DateInPast);
|
|
|
|
}
|
2024-02-26 18:00:43 -05:00
|
|
|
if self.lox_proof.date != date {
|
|
|
|
return Err(PositiveReportError::InvalidLoxProof);
|
|
|
|
}
|
2024-02-21 03:09:34 -05:00
|
|
|
let bridge_token = if self.bridge_token.is_none() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
let bridge_token_unchecked = self.bridge_token.unwrap().to_bridge_token()?;
|
|
|
|
// Check that bridge token fields match report fields...
|
|
|
|
// The user may override the bridge's autodetected country code,
|
|
|
|
// so allow the country code to be different.
|
|
|
|
if self.fingerprint != bridge_token_unchecked.unsigned_bridge_token.fingerprint
|
|
|
|
|| self.date != bridge_token_unchecked.unsigned_bridge_token.date
|
|
|
|
{
|
|
|
|
return Err(PositiveReportError::InvalidBridgeToken);
|
|
|
|
}
|
|
|
|
Some(bridge_token_unchecked)
|
|
|
|
};
|
2024-02-07 18:35:53 -05:00
|
|
|
Ok(PositiveReport {
|
|
|
|
fingerprint: self.fingerprint,
|
2024-02-21 03:09:34 -05:00
|
|
|
bridge_token: bridge_token,
|
2024-02-07 18:35:53 -05:00
|
|
|
lox_proof: self.lox_proof,
|
|
|
|
country: self.country,
|
|
|
|
date: self.date,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// An unsigned token which indicates that the bridge was reached
|
|
|
|
pub struct UnsignedBridgeToken {
|
|
|
|
/// hashed fingerprint (SHA-1 hash of 20-byte bridge ID)
|
|
|
|
pub fingerprint: [u8; 20],
|
2024-02-25 17:42:30 -05:00
|
|
|
|
2024-02-07 18:35:53 -05:00
|
|
|
/// client's country code
|
|
|
|
pub country: String,
|
2024-02-25 17:42:30 -05:00
|
|
|
|
2024-02-07 18:35:53 -05:00
|
|
|
/// today's Julian date
|
|
|
|
pub date: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UnsignedBridgeToken {
|
|
|
|
pub fn new(bridge_id: [u8; 20], country: String) -> Self {
|
|
|
|
let mut hasher = Sha1::new();
|
|
|
|
hasher.update(bridge_id);
|
|
|
|
let fingerprint: [u8; 20] = hasher.finalize().into();
|
|
|
|
let date = get_date();
|
|
|
|
Self {
|
|
|
|
fingerprint,
|
|
|
|
country,
|
|
|
|
date,
|
|
|
|
}
|
|
|
|
}
|
2024-02-21 03:09:34 -05:00
|
|
|
|
|
|
|
pub fn to_serializable_unsigned_bridge_token(self) -> SerializableUnsignedBridgeToken {
|
|
|
|
SerializableUnsignedBridgeToken {
|
|
|
|
fingerprint: self.fingerprint,
|
|
|
|
country: self.country,
|
|
|
|
date: self.date,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Serializes the token, eliding the underlying process
|
|
|
|
pub fn to_bincode(self) -> Vec<u8> {
|
|
|
|
bincode::serialize(&self.to_serializable_unsigned_bridge_token()).unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Deserializes the token, eliding the underlying process
|
|
|
|
pub fn from_bincode(vec: Vec<u8>) -> Result<Self, PositiveReportError> {
|
|
|
|
match bincode::deserialize::<SerializableUnsignedBridgeToken>(&vec[..]) {
|
|
|
|
Ok(v) => v.to_unsigned_bridge_token(),
|
|
|
|
Err(_) => Err(PositiveReportError::FailedToDeserialize),
|
|
|
|
}
|
|
|
|
}
|
2024-02-07 18:35:53 -05:00
|
|
|
}
|
|
|
|
|
2024-02-21 03:09:34 -05:00
|
|
|
/// (De)serializable unsigned bridge token object which must be consumed by the
|
|
|
|
/// checking function before it can be used
|
2024-02-07 18:35:53 -05:00
|
|
|
#[derive(Serialize, Deserialize)]
|
2024-02-21 03:09:34 -05:00
|
|
|
pub struct SerializableUnsignedBridgeToken {
|
|
|
|
pub fingerprint: [u8; 20],
|
|
|
|
pub country: String,
|
|
|
|
pub date: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SerializableUnsignedBridgeToken {
|
|
|
|
pub fn to_unsigned_bridge_token(self) -> Result<UnsignedBridgeToken, PositiveReportError> {
|
|
|
|
if self.country == ""
|
|
|
|
|| !COUNTRY_CODES.contains(self.country.as_str())
|
|
|
|
|| self.date > get_date().into()
|
|
|
|
{
|
|
|
|
return Err(PositiveReportError::InvalidBridgeToken);
|
|
|
|
}
|
|
|
|
Ok(UnsignedBridgeToken {
|
|
|
|
fingerprint: self.fingerprint,
|
|
|
|
country: self.country,
|
|
|
|
date: self.date,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A signed token which indicates that the bridge was reached
|
2024-02-07 18:35:53 -05:00
|
|
|
pub struct BridgeToken {
|
|
|
|
/// the unsigned version of this token
|
|
|
|
pub unsigned_bridge_token: UnsignedBridgeToken,
|
2024-02-25 17:42:30 -05:00
|
|
|
|
2024-02-07 18:35:53 -05:00
|
|
|
/// signature from bridge's ed25519 key
|
2024-02-21 03:09:34 -05:00
|
|
|
sig: Signature,
|
2024-02-07 18:35:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl BridgeToken {
|
|
|
|
pub fn new(unsigned_bridge_token: UnsignedBridgeToken, keypair: SigningKey) -> Self {
|
2024-02-21 03:09:34 -05:00
|
|
|
let bin = unsigned_bridge_token.to_bincode();
|
|
|
|
let sig = keypair.sign(&bin);
|
|
|
|
let unsigned_bridge_token = UnsignedBridgeToken::from_bincode(bin).unwrap();
|
2024-02-07 18:35:53 -05:00
|
|
|
Self {
|
|
|
|
unsigned_bridge_token,
|
|
|
|
sig,
|
|
|
|
}
|
|
|
|
}
|
2024-02-21 03:09:34 -05:00
|
|
|
|
|
|
|
/// Convert bridge token to a serializable version
|
|
|
|
pub fn to_serializable_bridge_token(self) -> SerializableBridgeToken {
|
|
|
|
SerializableBridgeToken {
|
|
|
|
unsigned_bridge_token: self
|
|
|
|
.unsigned_bridge_token
|
|
|
|
.to_serializable_unsigned_bridge_token(),
|
|
|
|
sig: self.sig,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Serializes the bridge token, eliding the underlying process
|
|
|
|
pub fn to_json(self) -> String {
|
|
|
|
serde_json::to_string(&self.to_serializable_bridge_token()).unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Deserializes the bridge token, eliding the underlying process
|
|
|
|
pub fn from_json(str: String) -> Result<Self, PositiveReportError> {
|
|
|
|
match serde_json::from_str::<SerializableBridgeToken>(&str) {
|
|
|
|
Ok(v) => v.to_bridge_token(),
|
|
|
|
Err(_) => Err(PositiveReportError::InvalidBridgeToken),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// (De)serializable bridge token object which must be consumed by the
|
|
|
|
/// checking function before it can be used
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct SerializableBridgeToken {
|
|
|
|
pub unsigned_bridge_token: SerializableUnsignedBridgeToken,
|
|
|
|
sig: Signature,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SerializableBridgeToken {
|
|
|
|
pub fn to_bridge_token(self) -> Result<BridgeToken, PositiveReportError> {
|
|
|
|
let unsigned_bridge_token = self.unsigned_bridge_token.to_unsigned_bridge_token()?;
|
|
|
|
Ok(BridgeToken {
|
|
|
|
unsigned_bridge_token: unsigned_bridge_token,
|
|
|
|
sig: self.sig,
|
|
|
|
})
|
|
|
|
}
|
2024-02-07 18:35:53 -05:00
|
|
|
}
|