client_lib.rs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. use async_trait::async_trait;
  2. use curve25519_dalek::scalar::Scalar;
  3. use lox::bridge_table::BridgeLine;
  4. use lox::bridge_table::ENC_BUCKET_BYTES;
  5. use lox::proto::*;
  6. use lox::IssuerPubKey;
  7. use lox::OPENINV_LENGTH;
  8. use serde::{Deserialize, Serialize};
  9. use serde_with::serde_as;
  10. use std::time::Duration;
  11. // used for testing function
  12. use std::io::Write;
  13. // provides a generic way to make network requests
  14. #[async_trait]
  15. pub trait Networking {
  16. async fn request(&self, endpoint: String, body: Vec<u8>) -> Vec<u8>;
  17. }
  18. // From https://gitlab.torproject.org/onyinyang/lox-server/-/blob/main/src/main.rs
  19. // TODO: Move this to main Lox library?
  20. #[serde_as]
  21. #[derive(Serialize, Deserialize)]
  22. pub struct Invite {
  23. #[serde_as(as = "[_; OPENINV_LENGTH]")]
  24. invite: [u8; OPENINV_LENGTH],
  25. }
  26. #[serde_as]
  27. #[derive(Serialize, Deserialize)]
  28. pub struct EncBridgeTable {
  29. #[serde_as(as = "Vec<[_; ENC_BUCKET_BYTES]>")]
  30. etable: Vec<[u8; ENC_BUCKET_BYTES]>,
  31. }
  32. // These two functions should only be used for testing.
  33. // This file should probably belong to the client, not the library.
  34. const TIME_OFFSET_FILENAME: &str = "time_offset.json";
  35. fn get_time_offset() -> u32 {
  36. if std::path::Path::new(&TIME_OFFSET_FILENAME).exists() {
  37. let infile = std::fs::File::open(&TIME_OFFSET_FILENAME).unwrap();
  38. serde_json::from_reader(infile).unwrap()
  39. } else {
  40. 0
  41. }
  42. }
  43. fn save_time_offset(secs: &u32) {
  44. let mut outfile =
  45. std::fs::File::create(&TIME_OFFSET_FILENAME).expect("Failed to create time_offset");
  46. write!(outfile, "{}", serde_json::to_string(&secs).unwrap())
  47. .expect("Failed to write to time_offset");
  48. }
  49. /// Get today's (real or simulated) date
  50. ///
  51. /// This function is modified from the lox lib.rs
  52. fn today(time_offset: Duration) -> u32 {
  53. // We will not encounter negative Julian dates (~6700 years ago)
  54. // or ones larger than 32 bits
  55. (time::OffsetDateTime::now_utc().date() + time_offset)
  56. .julian_day()
  57. .try_into()
  58. .unwrap()
  59. }
  60. // Helper functions to get public keys from vector
  61. pub fn get_lox_pub(lox_auth_pubkeys: &Vec<IssuerPubKey>) -> &IssuerPubKey {
  62. &lox_auth_pubkeys[0]
  63. }
  64. pub fn get_migration_pub(lox_auth_pubkeys: &Vec<IssuerPubKey>) -> &IssuerPubKey {
  65. &lox_auth_pubkeys[1]
  66. }
  67. pub fn get_migrationkey_pub(lox_auth_pubkeys: &Vec<IssuerPubKey>) -> &IssuerPubKey {
  68. &lox_auth_pubkeys[2]
  69. }
  70. pub fn get_reachability_pub(lox_auth_pubkeys: &Vec<IssuerPubKey>) -> &IssuerPubKey {
  71. &lox_auth_pubkeys[3]
  72. }
  73. pub fn get_invitation_pub(lox_auth_pubkeys: &Vec<IssuerPubKey>) -> &IssuerPubKey {
  74. &lox_auth_pubkeys[4]
  75. }
  76. // Helper function to get credential trust level as i8
  77. // (Note that this value should only be 0-4)
  78. pub fn get_cred_trust_level(cred: &lox::cred::Lox) -> i8 {
  79. let trust_levels: [Scalar; 5] = [
  80. Scalar::zero(),
  81. Scalar::one(),
  82. Scalar::from(2u64),
  83. Scalar::from(3u64),
  84. Scalar::from(4u8),
  85. ];
  86. for i in 0..trust_levels.len() {
  87. if cred.trust_level == trust_levels[i] {
  88. return i.try_into().unwrap();
  89. }
  90. }
  91. -1
  92. }
  93. // Download Lox Auth pubkeys
  94. pub async fn get_lox_auth_keys(net: &dyn Networking) -> Vec<IssuerPubKey> {
  95. let resp = net.request("/pubkeys".to_string(), [].to_vec()).await;
  96. let lox_auth_pubkeys: Vec<IssuerPubKey> = serde_json::from_slice(&resp).unwrap();
  97. lox_auth_pubkeys
  98. }
  99. // Get encrypted bridge table
  100. pub async fn get_reachability_credential(net: &dyn Networking) -> Vec<[u8; ENC_BUCKET_BYTES]> {
  101. let resp = net.request("/reachability".to_string(), [].to_vec()).await;
  102. let reachability_cred: EncBridgeTable = serde_json::from_slice(&resp).unwrap();
  103. reachability_cred.etable
  104. }
  105. // Get an open invitation
  106. pub async fn get_open_invitation(net: &dyn Networking) -> [u8; OPENINV_LENGTH] {
  107. let resp = net.request("/invite".to_string(), [].to_vec()).await;
  108. let open_invite: [u8; OPENINV_LENGTH] = serde_json::from_slice::<Invite>(&resp).unwrap().invite;
  109. open_invite
  110. }
  111. // Get a Lox Credential from an open invitation
  112. pub async fn get_lox_credential(
  113. net: &dyn Networking,
  114. open_invite: &[u8; OPENINV_LENGTH],
  115. lox_pub: &IssuerPubKey,
  116. ) -> (lox::cred::Lox, BridgeLine) {
  117. let (req, state) = open_invite::request(&open_invite);
  118. let encoded_req: Vec<u8> = serde_json::to_vec(&req).unwrap();
  119. let encoded_resp = net.request("/openreq".to_string(), encoded_req).await;
  120. let decoded_resp: open_invite::Response = serde_json::from_slice(&encoded_resp).unwrap();
  121. let (cred, bridgeline) = open_invite::handle_response(state, decoded_resp, &lox_pub).unwrap();
  122. (cred, bridgeline)
  123. }
  124. // Get a migration credential to migrate to higher trust
  125. pub async fn trust_promotion(
  126. net: &dyn Networking,
  127. lox_cred: &lox::cred::Lox,
  128. lox_pub: &IssuerPubKey,
  129. ) -> lox::cred::Migration {
  130. // FOR TESTING ONLY
  131. let time_offset = get_time_offset() + 60 * 60 * 24 * 31;
  132. save_time_offset(&time_offset);
  133. let (req, state) =
  134. // trust_promotion::request(&lox_cred, &lox_pub, today(Duration::ZERO)).unwrap();
  135. trust_promotion::request(&lox_cred, &lox_pub, today(Duration::from_secs(time_offset.into()))).unwrap(); // FOR TESTING ONLY
  136. let encoded_req: Vec<u8> = serde_json::to_vec(&req).unwrap();
  137. let encoded_resp = net.request("/trustpromo".to_string(), encoded_req).await;
  138. let decoded_resp: trust_promotion::Response = serde_json::from_slice(&encoded_resp).unwrap();
  139. let migration_cred = trust_promotion::handle_response(state, decoded_resp).unwrap();
  140. migration_cred
  141. }
  142. // Promote from untrusted (trust level 0) to trusted (trust level 1)
  143. pub async fn trust_migration(
  144. net: &dyn Networking,
  145. lox_cred: &lox::cred::Lox,
  146. migration_cred: &lox::cred::Migration,
  147. lox_pub: &IssuerPubKey,
  148. migration_pub: &IssuerPubKey,
  149. ) -> lox::cred::Lox {
  150. let (req, state) =
  151. migration::request(lox_cred, migration_cred, lox_pub, migration_pub).unwrap();
  152. let encoded_req: Vec<u8> = serde_json::to_vec(&req).unwrap();
  153. let encoded_resp = net.request("/trustmig".to_string(), encoded_req).await;
  154. let decoded_resp: migration::Response = serde_json::from_slice(&encoded_resp).unwrap();
  155. let cred = migration::handle_response(state, decoded_resp, lox_pub).unwrap();
  156. cred
  157. }
  158. // Increase trust from at least level 1 to higher levels
  159. pub async fn level_up(
  160. net: &dyn Networking,
  161. lox_cred: &lox::cred::Lox,
  162. encbuckets: &Vec<[u8; ENC_BUCKET_BYTES]>,
  163. lox_pub: &IssuerPubKey,
  164. reachability_pub: &IssuerPubKey,
  165. ) -> lox::cred::Lox {
  166. // Read the bucket in the credential to get today's Bucket
  167. // Reachability credential
  168. let (id, key) = lox::bridge_table::from_scalar(lox_cred.bucket).unwrap();
  169. let bucket =
  170. lox::bridge_table::BridgeTable::decrypt_bucket(id, &key, &encbuckets[id as usize]).unwrap();
  171. let reachcred = bucket.1.unwrap();
  172. // FOR TESTING ONLY
  173. let time_offset = get_time_offset() + 60 * 60 * 24 * 85;
  174. save_time_offset(&time_offset);
  175. // Use the Bucket Reachability credential to advance to the next
  176. // level
  177. let (req, state) = level_up::request(
  178. lox_cred,
  179. &reachcred,
  180. lox_pub,
  181. reachability_pub,
  182. //today(Duration::ZERO),
  183. today(Duration::from_secs(time_offset.into())), // FOR TESTING ONLY
  184. )
  185. .unwrap();
  186. let encoded_req: Vec<u8> = serde_json::to_vec(&req).unwrap();
  187. let encoded_resp = net.request("/levelup".to_string(), encoded_req).await;
  188. let decoded_resp: level_up::Response = serde_json::from_slice(&encoded_resp).unwrap();
  189. let cred = level_up::handle_response(state, decoded_resp, lox_pub).unwrap();
  190. cred
  191. }
  192. // Request an Invitation credential to give to a friend
  193. pub async fn issue_invite(
  194. net: &dyn Networking,
  195. lox_cred: &lox::cred::Lox,
  196. encbuckets: &Vec<[u8; ENC_BUCKET_BYTES]>,
  197. lox_pub: &IssuerPubKey,
  198. reachability_pub: &IssuerPubKey,
  199. invitation_pub: &IssuerPubKey,
  200. ) -> (lox::cred::Lox, lox::cred::Invitation) {
  201. // Read the bucket in the credential to get today's Bucket
  202. // Reachability credential
  203. let (id, key) = lox::bridge_table::from_scalar(lox_cred.bucket).unwrap();
  204. let bucket =
  205. lox::bridge_table::BridgeTable::decrypt_bucket(id, &key, &encbuckets[id as usize]).unwrap();
  206. let reachcred = bucket.1.unwrap();
  207. let (req, state) = issue_invite::request(
  208. lox_cred,
  209. &reachcred,
  210. lox_pub,
  211. reachability_pub,
  212. today(Duration::ZERO),
  213. )
  214. .unwrap();
  215. let encoded_req: Vec<u8> = serde_json::to_vec(&req).unwrap();
  216. let encoded_resp = net.request("/issueinvite".to_string(), encoded_req).await;
  217. let decoded_resp: issue_invite::Response = serde_json::from_slice(&encoded_resp).unwrap();
  218. let (cred, invite) =
  219. issue_invite::handle_response(state, decoded_resp, lox_pub, invitation_pub).unwrap();
  220. (cred, invite)
  221. }
  222. // Redeem an Invitation credential to start at trust level 1
  223. pub async fn redeem_invite(
  224. net: &dyn Networking,
  225. invite: &lox::cred::Invitation,
  226. lox_pub: &IssuerPubKey,
  227. invitation_pub: &IssuerPubKey,
  228. ) -> lox::cred::Lox {
  229. let (req, state) =
  230. redeem_invite::request(invite, invitation_pub, today(Duration::ZERO)).unwrap();
  231. let encoded_req: Vec<u8> = serde_json::to_vec(&req).unwrap();
  232. let encoded_resp = net.request("/redeem".to_string(), encoded_req).await;
  233. let decoded_resp: redeem_invite::Response = serde_json::from_slice(&encoded_resp).unwrap();
  234. let cred = redeem_invite::handle_response(state, decoded_resp, lox_pub).unwrap();
  235. cred
  236. }
  237. // Check for a migration credential to move to a new bucket
  238. pub async fn check_blockage(
  239. net: &dyn Networking,
  240. lox_cred: &lox::cred::Lox,
  241. lox_pub: &IssuerPubKey,
  242. ) -> lox::cred::Migration {
  243. let (req, state) = check_blockage::request(lox_cred, lox_pub).unwrap();
  244. let encoded_req: Vec<u8> = serde_json::to_vec(&req).unwrap();
  245. let encoded_resp = net.request("/checkblockage".to_string(), encoded_req).await;
  246. let decoded_resp: check_blockage::Response = serde_json::from_slice(&encoded_resp).unwrap();
  247. let migcred = check_blockage::handle_response(state, decoded_resp).unwrap();
  248. migcred
  249. }
  250. // Migrate to a new bucket (must be level >= 3)
  251. pub async fn blockage_migration(
  252. net: &dyn Networking,
  253. lox_cred: &lox::cred::Lox,
  254. migcred: &lox::cred::Migration,
  255. lox_pub: &IssuerPubKey,
  256. migration_pub: &IssuerPubKey,
  257. ) -> lox::cred::Lox {
  258. let (req, state) =
  259. blockage_migration::request(lox_cred, migcred, lox_pub, migration_pub).unwrap();
  260. let encoded_req: Vec<u8> = serde_json::to_vec(&req).unwrap();
  261. let encoded_resp = net
  262. .request("/blockagemigration".to_string(), encoded_req)
  263. .await;
  264. let decoded_resp: blockage_migration::Response = serde_json::from_slice(&encoded_resp).unwrap();
  265. let cred = blockage_migration::handle_response(state, decoded_resp, lox_pub).unwrap();
  266. cred
  267. }