client_lib.rs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. mod client_net;
  2. use client_net::net_request;
  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. // From https://gitlab.torproject.org/onyinyang/lox-server/-/blob/main/src/main.rs
  12. // TODO: Move this to main Lox library?
  13. #[serde_as]
  14. #[derive(Serialize, Deserialize)]
  15. pub struct Invite {
  16. #[serde_as(as = "[_; OPENINV_LENGTH]")]
  17. invite: [u8; OPENINV_LENGTH],
  18. }
  19. #[serde_as]
  20. #[derive(Serialize, Deserialize)]
  21. pub struct EncBridgeTable {
  22. #[serde_as(as = "Vec<[_; ENC_BUCKET_BYTES]>")]
  23. etable: Vec<[u8; ENC_BUCKET_BYTES]>,
  24. }
  25. /// Get today's (real or simulated) date
  26. ///
  27. /// This function is modified from the lox lib.rs
  28. fn today(time_offset: Duration) -> u32 {
  29. // We will not encounter negative Julian dates (~6700 years ago)
  30. // or ones larger than 32 bits
  31. (time::OffsetDateTime::now_utc().date() + time_offset)
  32. .julian_day()
  33. .try_into()
  34. .unwrap()
  35. }
  36. // Helper functions to get public keys from vector
  37. pub fn get_lox_pub(lox_auth_pubkeys: &Vec<IssuerPubKey>) -> &IssuerPubKey {
  38. &lox_auth_pubkeys[0]
  39. }
  40. pub fn get_migration_pub(lox_auth_pubkeys: &Vec<IssuerPubKey>) -> &IssuerPubKey {
  41. &lox_auth_pubkeys[1]
  42. }
  43. pub fn get_migrationkey_pub(lox_auth_pubkeys: &Vec<IssuerPubKey>) -> &IssuerPubKey {
  44. &lox_auth_pubkeys[2]
  45. }
  46. pub fn get_reachability_pub(lox_auth_pubkeys: &Vec<IssuerPubKey>) -> &IssuerPubKey {
  47. &lox_auth_pubkeys[3]
  48. }
  49. pub fn get_invitation_pub(lox_auth_pubkeys: &Vec<IssuerPubKey>) -> &IssuerPubKey {
  50. &lox_auth_pubkeys[4]
  51. }
  52. // Download Lox Auth pubkeys
  53. pub async fn get_lox_auth_keys(server_addr: &str) -> Vec<IssuerPubKey> {
  54. let resp = net_request(server_addr.to_string() + "/pubkeys", [].to_vec()).await;
  55. let lox_auth_pubkeys: Vec<IssuerPubKey> = serde_json::from_slice(&resp).unwrap();
  56. lox_auth_pubkeys
  57. }
  58. // Get encrypted bridge table
  59. pub async fn get_reachability_credential(server_addr: &str) -> Vec<[u8; ENC_BUCKET_BYTES]> {
  60. let resp = net_request(server_addr.to_string() + "/reachability", [].to_vec()).await;
  61. let reachability_cred: EncBridgeTable = serde_json::from_slice(&resp).unwrap();
  62. reachability_cred.etable
  63. }
  64. // Get an open invitation
  65. pub async fn get_open_invitation(server_addr: &str) -> [u8; OPENINV_LENGTH] {
  66. let resp = net_request(server_addr.to_string() + "/invite", [].to_vec()).await;
  67. let open_invite: [u8; OPENINV_LENGTH] = serde_json::from_slice::<Invite>(&resp).unwrap().invite;
  68. open_invite
  69. }
  70. // Get a Lox Credential from an open invitation
  71. pub async fn get_lox_credential(
  72. server_addr: &str,
  73. open_invite: &[u8; OPENINV_LENGTH],
  74. lox_pub: &IssuerPubKey,
  75. ) -> (lox::cred::Lox, BridgeLine) {
  76. let (req, state) = open_invite::request(&open_invite);
  77. let encoded_req: Vec<u8> = serde_json::to_vec(&req).unwrap();
  78. let encoded_resp = net_request(server_addr.to_string() + "/openreq", encoded_req).await;
  79. let decoded_resp: open_invite::Response = serde_json::from_slice(&encoded_resp).unwrap();
  80. let (cred, bridgeline) = open_invite::handle_response(state, decoded_resp, &lox_pub).unwrap();
  81. (cred, bridgeline)
  82. }
  83. // Get a migration credential to migrate to higher trust
  84. pub async fn trust_promotion(
  85. server_addr: &str,
  86. lox_cred: &lox::cred::Lox,
  87. lox_pub: &IssuerPubKey,
  88. ) -> lox::cred::Migration {
  89. let (req, state) =
  90. // trust_promotion::request(&lox_cred, &lox_pub, today(Duration::ZERO)).unwrap();
  91. trust_promotion::request(&lox_cred, &lox_pub, today(Duration::from_secs(60 * 60 * 24 * 31))).unwrap(); // FOR TESTING ONLY
  92. let encoded_req: Vec<u8> = serde_json::to_vec(&req).unwrap();
  93. let encoded_resp = net_request(server_addr.to_string() + "/trustpromo", encoded_req).await;
  94. let decoded_resp: trust_promotion::Response = serde_json::from_slice(&encoded_resp).unwrap();
  95. let migration_cred = trust_promotion::handle_response(state, decoded_resp).unwrap();
  96. migration_cred
  97. }
  98. // Promote from untrusted (trust level 0) to trusted (trust level 1)
  99. pub async fn trust_migration(
  100. server_addr: &str,
  101. lox_cred: &lox::cred::Lox,
  102. migration_cred: &lox::cred::Migration,
  103. lox_pub: &IssuerPubKey,
  104. migration_pub: &IssuerPubKey,
  105. ) -> lox::cred::Lox {
  106. let (req, state) =
  107. migration::request(lox_cred, migration_cred, lox_pub, migration_pub).unwrap();
  108. let encoded_req: Vec<u8> = serde_json::to_vec(&req).unwrap();
  109. let encoded_resp = net_request(server_addr.to_string() + "/trustmig", encoded_req).await;
  110. let decoded_resp: migration::Response = serde_json::from_slice(&encoded_resp).unwrap();
  111. let cred = migration::handle_response(state, decoded_resp, lox_pub).unwrap();
  112. cred
  113. }
  114. // Increase trust from at least level 1 to higher levels
  115. pub async fn level_up(
  116. server_addr: &str,
  117. lox_cred: &lox::cred::Lox,
  118. encbuckets: &Vec<[u8; ENC_BUCKET_BYTES]>,
  119. lox_pub: &IssuerPubKey,
  120. reachability_pub: &IssuerPubKey,
  121. ) -> lox::cred::Lox {
  122. // Read the bucket in the credential to get today's Bucket
  123. // Reachability credential
  124. let (id, key) = lox::bridge_table::from_scalar(lox_cred.bucket).unwrap();
  125. let bucket =
  126. lox::bridge_table::BridgeTable::decrypt_bucket(id, &key, &encbuckets[id as usize]).unwrap();
  127. let reachcred = bucket.1.unwrap();
  128. // Use the Bucket Reachability credential to advance to the next
  129. // level
  130. let (req, state) = level_up::request(
  131. lox_cred,
  132. &reachcred,
  133. lox_pub,
  134. reachability_pub,
  135. //today(Duration::ZERO),
  136. // The following line adds 31 days (from initial trust migration)
  137. // plus 85 days for first level up.
  138. // The server's testing code keeps track of time added as the
  139. // program runs. This should be adapted to do that as well.
  140. today(Duration::from_secs(60 * 60 * 24 * (31 + 85))), // FOR TESTING ONLY
  141. )
  142. .unwrap();
  143. let encoded_req: Vec<u8> = serde_json::to_vec(&req).unwrap();
  144. let encoded_resp = net_request(server_addr.to_string() + "/levelup", encoded_req).await;
  145. let decoded_resp: level_up::Response = serde_json::from_slice(&encoded_resp).unwrap();
  146. let cred = level_up::handle_response(state, decoded_resp, lox_pub).unwrap();
  147. cred
  148. }
  149. // Request an Invitation credential to give to a friend
  150. pub async fn issue_invite(
  151. server_addr: &str,
  152. lox_cred: &lox::cred::Lox,
  153. encbuckets: &Vec<[u8; ENC_BUCKET_BYTES]>,
  154. lox_pub: &IssuerPubKey,
  155. reachability_pub: &IssuerPubKey,
  156. invitation_pub: &IssuerPubKey,
  157. ) -> (lox::cred::Lox, lox::cred::Invitation) {
  158. // Read the bucket in the credential to get today's Bucket
  159. // Reachability credential
  160. let (id, key) = lox::bridge_table::from_scalar(lox_cred.bucket).unwrap();
  161. let bucket =
  162. lox::bridge_table::BridgeTable::decrypt_bucket(id, &key, &encbuckets[id as usize]).unwrap();
  163. let reachcred = bucket.1.unwrap();
  164. let (req, state) = issue_invite::request(
  165. lox_cred,
  166. &reachcred,
  167. lox_pub,
  168. reachability_pub,
  169. today(Duration::ZERO),
  170. )
  171. .unwrap();
  172. let encoded_req: Vec<u8> = serde_json::to_vec(&req).unwrap();
  173. let encoded_resp = net_request(server_addr.to_string() + "/issueinvite", encoded_req).await;
  174. let decoded_resp: issue_invite::Response = serde_json::from_slice(&encoded_resp).unwrap();
  175. let (cred, invite) =
  176. issue_invite::handle_response(state, decoded_resp, lox_pub, invitation_pub).unwrap();
  177. (cred, invite)
  178. }
  179. // Redeem an Invitation credential to start at trust level 1
  180. pub async fn redeem_invite(
  181. server_addr: &str,
  182. invite: &lox::cred::Invitation,
  183. lox_pub: &IssuerPubKey,
  184. invitation_pub: &IssuerPubKey,
  185. ) -> lox::cred::Lox {
  186. let (req, state) =
  187. redeem_invite::request(invite, invitation_pub, today(Duration::ZERO)).unwrap();
  188. let encoded_req: Vec<u8> = serde_json::to_vec(&req).unwrap();
  189. let encoded_resp = net_request(server_addr.to_string() + "/redeem", encoded_req).await;
  190. let decoded_resp: redeem_invite::Response = serde_json::from_slice(&encoded_resp).unwrap();
  191. let cred = redeem_invite::handle_response(state, decoded_resp, lox_pub).unwrap();
  192. cred
  193. }
  194. // Check for a migration credential to move to a new bucket
  195. pub async fn check_blockage(
  196. server_addr: &str,
  197. lox_cred: &lox::cred::Lox,
  198. lox_pub: &IssuerPubKey,
  199. ) -> lox::cred::Migration {
  200. let (req, state) = check_blockage::request(lox_cred, lox_pub).unwrap();
  201. let encoded_req: Vec<u8> = serde_json::to_vec(&req).unwrap();
  202. let encoded_resp = net_request(server_addr.to_string() + "/checkblockage", encoded_req).await;
  203. let decoded_resp: check_blockage::Response = serde_json::from_slice(&encoded_resp).unwrap();
  204. let migcred = check_blockage::handle_response(state, decoded_resp).unwrap();
  205. migcred
  206. }
  207. // Migrate to a new bucket (must be level >= 3)
  208. pub async fn blockage_migration(
  209. server_addr: &str,
  210. lox_cred: &lox::cred::Lox,
  211. migcred: &lox::cred::Migration,
  212. lox_pub: &IssuerPubKey,
  213. migration_pub: &IssuerPubKey,
  214. ) -> lox::cred::Lox {
  215. let (req, state) =
  216. blockage_migration::request(lox_cred, migcred, lox_pub, migration_pub).unwrap();
  217. let encoded_req: Vec<u8> = serde_json::to_vec(&req).unwrap();
  218. let encoded_resp =
  219. net_request(server_addr.to_string() + "/blockagemigration", encoded_req).await;
  220. let decoded_resp: blockage_migration::Response = serde_json::from_slice(&encoded_resp).unwrap();
  221. let cred = blockage_migration::handle_response(state, decoded_resp, lox_pub).unwrap();
  222. cred
  223. }