client_lib.rs 11 KB

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