negative_report.rs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. use crate::{
  2. bridge_verification_info::BridgeVerificationInfo, crypto::EciesCiphertext, get_date,
  3. BridgeDistributor, COUNTRY_CODES, MAX_BACKDATE,
  4. };
  5. use curve25519_dalek::scalar::Scalar;
  6. use lox_library::{bridge_table::BridgeLine, cred::Lox};
  7. use rand::RngCore;
  8. use serde::{Deserialize, Serialize};
  9. use sha1::{Digest, Sha1};
  10. use sha3::Sha3_256;
  11. use x25519_dalek::{PublicKey, StaticSecret};
  12. #[derive(Debug, Serialize)]
  13. pub enum NegativeReportError {
  14. DateInFuture,
  15. DateInPast, // report is more than MAX_BACKDATE days old
  16. FailedToDecrypt, // couldn't decrypt to SerializableNegativeReport
  17. FailedToDeserialize, // couldn't deserialize to NegativeReport
  18. InvalidCountryCode,
  19. MissingCountryCode,
  20. }
  21. /// A report that the user was unable to connect to the bridge
  22. #[derive(Debug, Eq, PartialEq, Ord, PartialOrd)]
  23. pub struct NegativeReport {
  24. /// hashed fingerprint (SHA-1 hash of 20-byte bridge ID)
  25. pub fingerprint: [u8; 20],
  26. /// some way to prove knowledge of bridge
  27. bridge_pok: ProofOfBridgeKnowledge,
  28. /// user's country code
  29. pub country: String,
  30. /// today's Julian date
  31. pub date: u32,
  32. /// a random nonce used in the bridge_pok
  33. pub nonce: [u8; 32],
  34. /// the bridge distributor, e.g., Lox, Https, or Moat
  35. pub distributor: BridgeDistributor,
  36. }
  37. impl NegativeReport {
  38. pub fn new(
  39. bridge_id: [u8; 20],
  40. bridge_pok: ProofOfBridgeKnowledge,
  41. country: String,
  42. date: u32,
  43. nonce: [u8; 32],
  44. distributor: BridgeDistributor,
  45. ) -> Self {
  46. let mut hasher = Sha1::new();
  47. hasher.update(bridge_id);
  48. let fingerprint: [u8; 20] = hasher.finalize().into();
  49. Self {
  50. fingerprint,
  51. bridge_pok,
  52. country,
  53. date,
  54. nonce,
  55. distributor,
  56. }
  57. }
  58. pub fn from_bridgeline(
  59. bridgeline: BridgeLine,
  60. country: String,
  61. distributor: BridgeDistributor,
  62. ) -> Self {
  63. let date = get_date();
  64. let mut rng = rand::thread_rng();
  65. let mut nonce = [0; 32];
  66. rng.fill_bytes(&mut nonce);
  67. let bridge_pok = ProofOfBridgeKnowledge::HashOfBridgeLine(HashOfBridgeLine::new(
  68. &bridgeline,
  69. date,
  70. nonce,
  71. ));
  72. Self::new(
  73. bridgeline.fingerprint,
  74. bridge_pok,
  75. country,
  76. date,
  77. nonce,
  78. distributor,
  79. )
  80. }
  81. pub fn from_lox_bucket(bridge_id: [u8; 20], bucket: Scalar, country: String) -> Self {
  82. let date = get_date();
  83. let mut rng = rand::thread_rng();
  84. let mut nonce = [0; 32];
  85. rng.fill_bytes(&mut nonce);
  86. let bridge_pok =
  87. ProofOfBridgeKnowledge::HashOfBucket(HashOfBucket::new(&bucket, date, nonce));
  88. Self::new(
  89. bridge_id,
  90. bridge_pok,
  91. country,
  92. date,
  93. nonce,
  94. BridgeDistributor::Lox,
  95. )
  96. }
  97. pub fn from_lox_credential(bridge_id: [u8; 20], cred: &Lox, country: String) -> Self {
  98. NegativeReport::from_lox_bucket(bridge_id, cred.bucket, country)
  99. }
  100. pub fn encrypt(self, server_pub: &PublicKey) -> EncryptedNegativeReport {
  101. EncryptedNegativeReport {
  102. date: self.date,
  103. ciphertext: EciesCiphertext::encrypt(
  104. &bincode::serialize(&self.to_serializable_report()).unwrap(),
  105. server_pub,
  106. )
  107. .unwrap(),
  108. }
  109. }
  110. /// Convert report to a serializable version
  111. pub fn to_serializable_report(self) -> SerializableNegativeReport {
  112. SerializableNegativeReport {
  113. fingerprint: self.fingerprint,
  114. bridge_pok: self.bridge_pok,
  115. country: self.country,
  116. date: self.date,
  117. nonce: self.nonce,
  118. distributor: self.distributor,
  119. }
  120. }
  121. /// Serializes the report, eliding the underlying process
  122. pub fn to_json(self) -> String {
  123. serde_json::to_string(&self.to_serializable_report()).unwrap()
  124. }
  125. /// Deserializes the report, eliding the underlying process
  126. pub fn from_json(str: String) -> Result<Self, NegativeReportError> {
  127. match serde_json::from_str::<SerializableNegativeReport>(&str) {
  128. Ok(v) => v.to_report(),
  129. Err(_) => Err(NegativeReportError::FailedToDeserialize),
  130. }
  131. }
  132. /// Deserializes the report from slice, eliding the underlying process
  133. pub fn from_slice(slice: &[u8]) -> Result<Self, NegativeReportError> {
  134. match serde_json::from_slice::<SerializableNegativeReport>(&slice) {
  135. Ok(v) => v.to_report(),
  136. Err(_) => Err(NegativeReportError::FailedToDeserialize),
  137. }
  138. }
  139. /// Verify the report
  140. pub fn verify(self, bridge_info: &BridgeVerificationInfo) -> bool {
  141. match self.bridge_pok {
  142. ProofOfBridgeKnowledge::HashOfBridgeLine(pok) => {
  143. let hash = HashOfBridgeLine::new(&bridge_info.bridge_line, self.date, self.nonce);
  144. hash == pok
  145. }
  146. ProofOfBridgeKnowledge::HashOfBucket(pok) => {
  147. for b in &bridge_info.buckets {
  148. let hash = HashOfBucket::new(&b, self.date, self.nonce);
  149. if hash == pok {
  150. return true;
  151. }
  152. }
  153. false
  154. }
  155. }
  156. }
  157. }
  158. /// (De)serializable negative report object which must be consumed by the
  159. /// checking function before it can be used
  160. #[derive(Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize)]
  161. pub struct SerializableNegativeReport {
  162. pub fingerprint: [u8; 20],
  163. bridge_pok: ProofOfBridgeKnowledge,
  164. pub country: String,
  165. pub date: u32,
  166. pub nonce: [u8; 32],
  167. pub distributor: BridgeDistributor,
  168. }
  169. impl SerializableNegativeReport {
  170. pub fn to_report(self) -> Result<NegativeReport, NegativeReportError> {
  171. if self.country == "" {
  172. return Err(NegativeReportError::MissingCountryCode);
  173. }
  174. if !COUNTRY_CODES.contains(self.country.as_str()) {
  175. return Err(NegativeReportError::InvalidCountryCode);
  176. }
  177. let date = get_date();
  178. if self.date > date {
  179. return Err(NegativeReportError::DateInFuture);
  180. }
  181. if self.date < date - MAX_BACKDATE {
  182. return Err(NegativeReportError::DateInPast);
  183. }
  184. Ok(NegativeReport {
  185. fingerprint: self.fingerprint,
  186. bridge_pok: self.bridge_pok,
  187. country: self.country.to_string(),
  188. date: self.date.try_into().unwrap(),
  189. nonce: self.nonce,
  190. distributor: self.distributor,
  191. })
  192. }
  193. }
  194. /// Negative reports should be sent encrypted. This struct provides an
  195. /// encrypted serializable negative report.
  196. #[derive(Serialize, Deserialize)]
  197. pub struct EncryptedNegativeReport {
  198. /// The date field in the report. This is used to determine which key to use
  199. /// to decrypt the report.
  200. pub date: u32,
  201. ciphertext: EciesCiphertext,
  202. }
  203. impl EncryptedNegativeReport {
  204. pub fn decrypt(self, secret: &StaticSecret) -> Result<NegativeReport, NegativeReportError> {
  205. match self.ciphertext.decrypt(&secret) {
  206. Ok(m) => match bincode::deserialize::<SerializableNegativeReport>(&m) {
  207. Ok(ser_report) => ser_report.to_report(),
  208. Err(_) => Err(NegativeReportError::FailedToDeserialize),
  209. },
  210. Err(_) => Err(NegativeReportError::FailedToDecrypt),
  211. }
  212. }
  213. }
  214. /// Proof that the user knows (and should be able to access) a given bridge
  215. #[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize)]
  216. pub enum ProofOfBridgeKnowledge {
  217. /// Hash of bridge line as proof of knowledge of bridge line
  218. HashOfBridgeLine(HashOfBridgeLine),
  219. /// Hash of bucket ID for Lox user
  220. HashOfBucket(HashOfBucket),
  221. }
  222. /// Hash of bridge line to prove knowledge of that bridge
  223. #[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize)]
  224. pub struct HashOfBridgeLine {
  225. hash: [u8; 32],
  226. }
  227. impl HashOfBridgeLine {
  228. pub fn new(bl: &BridgeLine, date: u32, nonce: [u8; 32]) -> Self {
  229. let mut hasher = Sha3_256::new();
  230. hasher.update(date.to_le_bytes());
  231. hasher.update(nonce);
  232. hasher.update(bincode::serialize(&bl).unwrap());
  233. let hash: [u8; 32] = hasher.finalize().into();
  234. Self { hash }
  235. }
  236. }
  237. /// Hash of bucket ID to prove knowledge of bridges in that bucket
  238. #[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize)]
  239. pub struct HashOfBucket {
  240. hash: [u8; 32],
  241. }
  242. impl HashOfBucket {
  243. pub fn new(bucket: &Scalar, date: u32, nonce: [u8; 32]) -> Self {
  244. let mut hasher = Sha3_256::new();
  245. hasher.update(date.to_le_bytes());
  246. hasher.update(nonce);
  247. hasher.update(bucket.to_bytes());
  248. let hash: [u8; 32] = hasher.finalize().into();
  249. Self { hash }
  250. }
  251. }