2024-03-15 11:32:17 -04:00
|
|
|
use crate::{
|
2024-04-16 01:11:47 -04:00
|
|
|
bridge_verification_info::BridgeVerificationInfo, crypto::EciesCiphertext, get_date,
|
|
|
|
BridgeDistributor, COUNTRY_CODES, MAX_BACKDATE,
|
2024-03-15 11:32:17 -04:00
|
|
|
};
|
2024-02-07 18:35:53 -05:00
|
|
|
|
|
|
|
use curve25519_dalek::scalar::Scalar;
|
|
|
|
use lox_library::{bridge_table::BridgeLine, cred::Lox};
|
2024-04-12 02:38:35 -04:00
|
|
|
use rand::RngCore;
|
2024-02-07 18:35:53 -05:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use sha1::{Digest, Sha1};
|
|
|
|
use sha3::Sha3_256;
|
2024-04-16 01:11:47 -04:00
|
|
|
use x25519_dalek::{PublicKey, StaticSecret};
|
2024-02-07 18:35:53 -05:00
|
|
|
|
2024-03-22 23:42:22 -04:00
|
|
|
#[derive(Debug, Serialize)]
|
2024-02-07 18:35:53 -05:00
|
|
|
pub enum NegativeReportError {
|
|
|
|
DateInFuture,
|
2024-04-12 02:38:35 -04:00
|
|
|
DateInPast, // report is more than MAX_BACKDATE days old
|
2024-04-16 01:11:47 -04:00
|
|
|
FailedToDecrypt, // couldn't decrypt to SerializableNegativeReport
|
|
|
|
FailedToDeserialize, // couldn't deserialize to NegativeReport
|
2024-02-07 18:35:53 -05:00
|
|
|
InvalidCountryCode,
|
2024-02-21 15:34:29 -05:00
|
|
|
MissingCountryCode,
|
2024-02-07 18:35:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A report that the user was unable to connect to the bridge
|
2024-04-12 12:26:36 -04:00
|
|
|
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd)]
|
2024-02-07 18:35:53 -05:00
|
|
|
pub struct NegativeReport {
|
|
|
|
/// 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
|
|
|
/// some way to prove knowledge of bridge
|
|
|
|
bridge_pok: ProofOfBridgeKnowledge,
|
2024-02-25 17:42:30 -05:00
|
|
|
|
2024-02-21 15:34:29 -05:00
|
|
|
/// user's country code
|
2024-02-07 18:35:53 -05:00
|
|
|
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,
|
2024-03-14 18:09:24 -04:00
|
|
|
|
2024-04-12 02:38:35 -04:00
|
|
|
/// a random nonce used in the bridge_pok
|
|
|
|
pub nonce: [u8; 32],
|
|
|
|
|
2024-03-14 18:09:24 -04:00
|
|
|
/// the bridge distributor, e.g., Lox, Https, or Moat
|
|
|
|
pub distributor: BridgeDistributor,
|
2024-02-07 18:35:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl NegativeReport {
|
2024-03-14 18:09:24 -04:00
|
|
|
pub fn new(
|
|
|
|
bridge_id: [u8; 20],
|
|
|
|
bridge_pok: ProofOfBridgeKnowledge,
|
|
|
|
country: String,
|
2024-04-04 22:55:07 -04:00
|
|
|
date: u32,
|
2024-04-12 02:38:35 -04:00
|
|
|
nonce: [u8; 32],
|
2024-03-14 18:09:24 -04:00
|
|
|
distributor: BridgeDistributor,
|
|
|
|
) -> Self {
|
2024-02-07 18:35:53 -05:00
|
|
|
let mut hasher = Sha1::new();
|
|
|
|
hasher.update(bridge_id);
|
|
|
|
let fingerprint: [u8; 20] = hasher.finalize().into();
|
|
|
|
Self {
|
|
|
|
fingerprint,
|
|
|
|
bridge_pok,
|
|
|
|
country,
|
|
|
|
date,
|
2024-04-12 02:38:35 -04:00
|
|
|
nonce,
|
2024-03-14 18:09:24 -04:00
|
|
|
distributor,
|
2024-02-07 18:35:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-14 18:09:24 -04:00
|
|
|
pub fn from_bridgeline(
|
|
|
|
bridgeline: BridgeLine,
|
|
|
|
country: String,
|
|
|
|
distributor: BridgeDistributor,
|
|
|
|
) -> Self {
|
2024-04-04 22:55:07 -04:00
|
|
|
let date = get_date();
|
2024-04-12 02:38:35 -04:00
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
let mut nonce = [0; 32];
|
|
|
|
rng.fill_bytes(&mut nonce);
|
|
|
|
let bridge_pok = ProofOfBridgeKnowledge::HashOfBridgeLine(HashOfBridgeLine::new(
|
|
|
|
&bridgeline,
|
|
|
|
date,
|
|
|
|
nonce,
|
|
|
|
));
|
|
|
|
Self::new(
|
2024-04-04 22:55:07 -04:00
|
|
|
bridgeline.fingerprint,
|
|
|
|
bridge_pok,
|
|
|
|
country,
|
|
|
|
date,
|
2024-04-12 02:38:35 -04:00
|
|
|
nonce,
|
2024-04-04 22:55:07 -04:00
|
|
|
distributor,
|
|
|
|
)
|
2024-02-07 18:35:53 -05:00
|
|
|
}
|
|
|
|
|
2024-03-14 18:09:24 -04:00
|
|
|
pub fn from_lox_bucket(bridge_id: [u8; 20], bucket: Scalar, country: String) -> Self {
|
2024-04-04 22:55:07 -04:00
|
|
|
let date = get_date();
|
2024-04-12 02:38:35 -04:00
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
let mut nonce = [0; 32];
|
|
|
|
rng.fill_bytes(&mut nonce);
|
|
|
|
let bridge_pok =
|
|
|
|
ProofOfBridgeKnowledge::HashOfBucket(HashOfBucket::new(&bucket, date, nonce));
|
|
|
|
Self::new(
|
|
|
|
bridge_id,
|
|
|
|
bridge_pok,
|
|
|
|
country,
|
|
|
|
date,
|
|
|
|
nonce,
|
|
|
|
BridgeDistributor::Lox,
|
|
|
|
)
|
2024-02-07 18:35:53 -05:00
|
|
|
}
|
|
|
|
|
2024-04-12 02:38:35 -04:00
|
|
|
pub fn from_lox_credential(bridge_id: [u8; 20], cred: &Lox, country: String) -> Self {
|
2024-03-14 18:09:24 -04:00
|
|
|
NegativeReport::from_lox_bucket(bridge_id, cred.bucket, country)
|
2024-02-07 18:35:53 -05:00
|
|
|
}
|
|
|
|
|
2024-04-16 01:11:47 -04:00
|
|
|
pub fn encrypt(self, server_pub: &PublicKey) -> EncryptedNegativeReport {
|
|
|
|
EncryptedNegativeReport {
|
|
|
|
date: self.date,
|
|
|
|
ciphertext: EciesCiphertext::encrypt(
|
|
|
|
&bincode::serialize(&self.to_serializable_report()).unwrap(),
|
|
|
|
server_pub,
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-07 18:35:53 -05:00
|
|
|
/// Convert report to a serializable version
|
|
|
|
pub fn to_serializable_report(self) -> SerializableNegativeReport {
|
|
|
|
SerializableNegativeReport {
|
|
|
|
fingerprint: self.fingerprint,
|
|
|
|
bridge_pok: self.bridge_pok,
|
|
|
|
country: self.country,
|
|
|
|
date: self.date,
|
2024-04-12 02:38:35 -04:00
|
|
|
nonce: self.nonce,
|
2024-03-14 18:09:24 -04:00
|
|
|
distributor: self.distributor,
|
2024-02-07 18:35:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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, NegativeReportError> {
|
|
|
|
match serde_json::from_str::<SerializableNegativeReport>(&str) {
|
|
|
|
Ok(v) => v.to_report(),
|
2024-03-22 23:42:22 -04:00
|
|
|
Err(_) => Err(NegativeReportError::FailedToDeserialize),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Deserializes the report from slice, eliding the underlying process
|
|
|
|
pub fn from_slice(slice: &[u8]) -> Result<Self, NegativeReportError> {
|
2024-05-29 13:20:56 -04:00
|
|
|
match serde_json::from_slice::<SerializableNegativeReport>(slice) {
|
2024-03-22 23:42:22 -04:00
|
|
|
Ok(v) => v.to_report(),
|
2024-02-07 18:35:53 -05:00
|
|
|
Err(_) => Err(NegativeReportError::FailedToDeserialize),
|
|
|
|
}
|
|
|
|
}
|
2024-02-21 03:38:55 -05:00
|
|
|
|
2024-02-28 16:06:58 -05:00
|
|
|
/// Verify the report
|
2024-03-15 11:32:17 -04:00
|
|
|
pub fn verify(self, bridge_info: &BridgeVerificationInfo) -> bool {
|
2024-02-21 03:38:55 -05:00
|
|
|
match self.bridge_pok {
|
2024-02-28 15:26:22 -05:00
|
|
|
ProofOfBridgeKnowledge::HashOfBridgeLine(pok) => {
|
2024-04-12 02:38:35 -04:00
|
|
|
let hash = HashOfBridgeLine::new(&bridge_info.bridge_line, self.date, self.nonce);
|
2024-02-28 15:26:22 -05:00
|
|
|
hash == pok
|
|
|
|
}
|
2024-03-15 02:24:52 -04:00
|
|
|
ProofOfBridgeKnowledge::HashOfBucket(pok) => {
|
|
|
|
for b in &bridge_info.buckets {
|
2024-05-29 13:20:56 -04:00
|
|
|
let hash = HashOfBucket::new(b, self.date, self.nonce);
|
2024-03-15 02:24:52 -04:00
|
|
|
if hash == pok {
|
|
|
|
return true;
|
|
|
|
}
|
2024-02-28 14:10:45 -05:00
|
|
|
}
|
2024-03-15 02:24:52 -04:00
|
|
|
false
|
|
|
|
}
|
2024-02-21 03:38:55 -05:00
|
|
|
}
|
|
|
|
}
|
2024-02-07 18:35:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// (De)serializable negative report object which must be consumed by the
|
|
|
|
/// checking function before it can be used
|
2024-02-21 15:15:39 -05:00
|
|
|
#[derive(Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize)]
|
2024-02-07 18:35:53 -05:00
|
|
|
pub struct SerializableNegativeReport {
|
|
|
|
pub fingerprint: [u8; 20],
|
|
|
|
bridge_pok: ProofOfBridgeKnowledge,
|
|
|
|
pub country: String,
|
|
|
|
pub date: u32,
|
2024-04-12 02:38:35 -04:00
|
|
|
pub nonce: [u8; 32],
|
2024-03-14 18:09:24 -04:00
|
|
|
pub distributor: BridgeDistributor,
|
2024-02-07 18:35:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SerializableNegativeReport {
|
|
|
|
pub fn to_report(self) -> Result<NegativeReport, NegativeReportError> {
|
2024-05-29 13:20:56 -04:00
|
|
|
if self.country.is_empty() {
|
2024-02-21 15:34:29 -05:00
|
|
|
return Err(NegativeReportError::MissingCountryCode);
|
|
|
|
}
|
|
|
|
if !COUNTRY_CODES.contains(self.country.as_str()) {
|
2024-02-07 18:35:53 -05:00
|
|
|
return Err(NegativeReportError::InvalidCountryCode);
|
|
|
|
}
|
2024-04-12 02:38:35 -04:00
|
|
|
let date = get_date();
|
|
|
|
if self.date > date {
|
2024-02-07 18:35:53 -05:00
|
|
|
return Err(NegativeReportError::DateInFuture);
|
|
|
|
}
|
2024-04-12 02:38:35 -04:00
|
|
|
if self.date < date - MAX_BACKDATE {
|
|
|
|
return Err(NegativeReportError::DateInPast);
|
|
|
|
}
|
2024-02-07 18:35:53 -05:00
|
|
|
Ok(NegativeReport {
|
|
|
|
fingerprint: self.fingerprint,
|
|
|
|
bridge_pok: self.bridge_pok,
|
|
|
|
country: self.country.to_string(),
|
2024-05-29 13:20:56 -04:00
|
|
|
date: self.date,
|
2024-04-12 02:38:35 -04:00
|
|
|
nonce: self.nonce,
|
2024-03-14 18:09:24 -04:00
|
|
|
distributor: self.distributor,
|
2024-02-07 18:35:53 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-16 01:11:47 -04:00
|
|
|
/// Negative reports should be sent encrypted. This struct provides an
|
|
|
|
/// encrypted serializable negative report.
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct EncryptedNegativeReport {
|
|
|
|
/// The date field in the report. This is used to determine which key to use
|
|
|
|
/// to decrypt the report.
|
|
|
|
pub date: u32,
|
|
|
|
ciphertext: EciesCiphertext,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EncryptedNegativeReport {
|
|
|
|
pub fn decrypt(self, secret: &StaticSecret) -> Result<NegativeReport, NegativeReportError> {
|
2024-05-29 13:20:56 -04:00
|
|
|
match self.ciphertext.decrypt(secret) {
|
2024-04-16 01:11:47 -04:00
|
|
|
Ok(m) => match bincode::deserialize::<SerializableNegativeReport>(&m) {
|
|
|
|
Ok(ser_report) => ser_report.to_report(),
|
|
|
|
Err(_) => Err(NegativeReportError::FailedToDeserialize),
|
|
|
|
},
|
|
|
|
Err(_) => Err(NegativeReportError::FailedToDecrypt),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-07 18:35:53 -05:00
|
|
|
/// Proof that the user knows (and should be able to access) a given bridge
|
2024-04-12 12:26:36 -04:00
|
|
|
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize)]
|
2024-02-07 18:35:53 -05:00
|
|
|
pub enum ProofOfBridgeKnowledge {
|
|
|
|
/// Hash of bridge line as proof of knowledge of bridge line
|
|
|
|
HashOfBridgeLine(HashOfBridgeLine),
|
2024-02-25 17:42:30 -05:00
|
|
|
|
2024-02-07 18:35:53 -05:00
|
|
|
/// Hash of bucket ID for Lox user
|
|
|
|
HashOfBucket(HashOfBucket),
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Hash of bridge line to prove knowledge of that bridge
|
2024-04-12 12:26:36 -04:00
|
|
|
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize)]
|
2024-02-07 18:35:53 -05:00
|
|
|
pub struct HashOfBridgeLine {
|
|
|
|
hash: [u8; 32],
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HashOfBridgeLine {
|
2024-04-12 02:38:35 -04:00
|
|
|
pub fn new(bl: &BridgeLine, date: u32, nonce: [u8; 32]) -> Self {
|
2024-02-07 18:35:53 -05:00
|
|
|
let mut hasher = Sha3_256::new();
|
2024-04-04 22:55:07 -04:00
|
|
|
hasher.update(date.to_le_bytes());
|
2024-04-12 02:38:35 -04:00
|
|
|
hasher.update(nonce);
|
2024-02-07 18:35:53 -05:00
|
|
|
hasher.update(bincode::serialize(&bl).unwrap());
|
|
|
|
let hash: [u8; 32] = hasher.finalize().into();
|
|
|
|
Self { hash }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Hash of bucket ID to prove knowledge of bridges in that bucket
|
2024-04-12 12:26:36 -04:00
|
|
|
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize)]
|
2024-02-07 18:35:53 -05:00
|
|
|
pub struct HashOfBucket {
|
|
|
|
hash: [u8; 32],
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HashOfBucket {
|
2024-04-12 02:38:35 -04:00
|
|
|
pub fn new(bucket: &Scalar, date: u32, nonce: [u8; 32]) -> Self {
|
2024-02-07 18:35:53 -05:00
|
|
|
let mut hasher = Sha3_256::new();
|
2024-04-04 22:55:07 -04:00
|
|
|
hasher.update(date.to_le_bytes());
|
2024-04-12 02:38:35 -04:00
|
|
|
hasher.update(nonce);
|
2024-02-07 18:35:53 -05:00
|
|
|
hasher.update(bucket.to_bytes());
|
|
|
|
let hash: [u8; 32] = hasher.finalize().into();
|
|
|
|
Self { hash }
|
|
|
|
}
|
|
|
|
}
|