client_lib.rs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. use async_trait::async_trait;
  2. use curve25519_dalek::scalar::Scalar;
  3. use lox_library::bridge_table::BridgeLine;
  4. use lox_library::bridge_table::EncryptedBucket;
  5. use lox_library::proto::*;
  6. use lox_library::scalar_u32;
  7. use lox_library::IssuerPubKey;
  8. use lox_library::OPENINV_LENGTH;
  9. use lox_utils::EncBridgeTable;
  10. use serde::{Deserialize, Serialize};
  11. use serde_with::serde_as;
  12. use std::collections::HashMap;
  13. use std::time::Duration;
  14. // used for testing function
  15. use std::io::Write;
  16. // provides a generic way to make network requests
  17. #[async_trait]
  18. pub trait Networking {
  19. async fn request(&self, endpoint: String, body: Vec<u8>) -> Vec<u8>;
  20. }
  21. // From https://gitlab.torproject.org/onyinyang/lox-server/-/blob/main/src/main.rs
  22. // TODO: Move this to main Lox library?
  23. #[serde_as]
  24. #[derive(Serialize, Deserialize)]
  25. pub struct Invite {
  26. #[serde_as(as = "[_; OPENINV_LENGTH]")]
  27. invite: [u8; OPENINV_LENGTH],
  28. }
  29. // Helper functions to get public keys from vector
  30. pub fn get_lox_pub(lox_auth_pubkeys: &Vec<IssuerPubKey>) -> &IssuerPubKey {
  31. &lox_auth_pubkeys[0]
  32. }
  33. pub fn get_migration_pub(lox_auth_pubkeys: &Vec<IssuerPubKey>) -> &IssuerPubKey {
  34. &lox_auth_pubkeys[1]
  35. }
  36. pub fn get_migrationkey_pub(lox_auth_pubkeys: &Vec<IssuerPubKey>) -> &IssuerPubKey {
  37. &lox_auth_pubkeys[2]
  38. }
  39. pub fn get_reachability_pub(lox_auth_pubkeys: &Vec<IssuerPubKey>) -> &IssuerPubKey {
  40. &lox_auth_pubkeys[3]
  41. }
  42. pub fn get_invitation_pub(lox_auth_pubkeys: &Vec<IssuerPubKey>) -> &IssuerPubKey {
  43. &lox_auth_pubkeys[4]
  44. }
  45. // Helper function to get credential trust level as i8
  46. // (Note that this value should only be 0-4)
  47. pub fn get_cred_trust_level(cred: &lox_library::cred::Lox) -> i8 {
  48. let trust_levels: [Scalar; 5] = [
  49. Scalar::zero(),
  50. Scalar::one(),
  51. Scalar::from(2u64),
  52. Scalar::from(3u64),
  53. Scalar::from(4u8),
  54. ];
  55. for i in 0..trust_levels.len() {
  56. if cred.trust_level == trust_levels[i] {
  57. return i.try_into().unwrap();
  58. }
  59. }
  60. -1
  61. }
  62. // Helper function to check if credential is eligible for
  63. // promotion from level 0 to 1
  64. pub async fn eligible_for_trust_promotion(
  65. net: &dyn Networking,
  66. cred: &lox_library::cred::Lox,
  67. ) -> bool {
  68. let level_since: u32 = match scalar_u32(&cred.level_since) {
  69. Some(v) => v,
  70. None => return false,
  71. };
  72. get_cred_trust_level(cred) == 0
  73. && level_since + lox_library::proto::trust_promotion::UNTRUSTED_INTERVAL
  74. <= get_today(net).await
  75. }
  76. // Helper function to check if credential is eligible for
  77. // level up from level 1+
  78. pub async fn eligible_for_level_up(net: &dyn Networking, cred: &lox_library::cred::Lox) -> bool {
  79. let level_since: u32 = match scalar_u32(&cred.level_since) {
  80. Some(v) => v,
  81. None => return false,
  82. };
  83. let trust_level: usize = get_cred_trust_level(cred).try_into().unwrap();
  84. trust_level > 0
  85. && level_since + lox_library::proto::level_up::LEVEL_INTERVAL[trust_level]
  86. <= get_today(net).await
  87. }
  88. // Get current date from Lox Auth
  89. pub async fn get_today(net: &dyn Networking) -> u32 {
  90. let resp = net.request("/today".to_string(), [].to_vec()).await;
  91. let today: u32 = serde_json::from_slice(&resp).unwrap();
  92. today
  93. }
  94. // Download Lox Auth pubkeys
  95. pub async fn get_lox_auth_keys(net: &dyn Networking) -> Vec<IssuerPubKey> {
  96. let resp = net.request("/pubkeys".to_string(), [].to_vec()).await;
  97. let lox_auth_pubkeys: Vec<IssuerPubKey> = serde_json::from_slice(&resp).unwrap();
  98. lox_auth_pubkeys
  99. }
  100. // Get encrypted bridge table
  101. pub async fn get_reachability_credential(net: &dyn Networking) -> HashMap<u32, EncryptedBucket> {
  102. let resp = net.request("/reachability".to_string(), [].to_vec()).await;
  103. let reachability_cred: EncBridgeTable = serde_json::from_slice(&resp).unwrap();
  104. reachability_cred.etable
  105. }
  106. // Get an open invitation
  107. pub async fn get_open_invitation(net: &dyn Networking) -> [u8; OPENINV_LENGTH] {
  108. let resp = net.request("/invite".to_string(), [].to_vec()).await;
  109. let open_invite: [u8; OPENINV_LENGTH] = serde_json::from_slice::<Invite>(&resp).unwrap().invite;
  110. open_invite
  111. }
  112. // Get a Lox Credential from an open invitation
  113. pub async fn get_lox_credential(
  114. net: &dyn Networking,
  115. open_invite: &[u8; OPENINV_LENGTH],
  116. lox_pub: &IssuerPubKey,
  117. ) -> (lox_library::cred::Lox, BridgeLine) {
  118. let (req, state) = open_invite::request(&open_invite);
  119. let encoded_req: Vec<u8> = serde_json::to_vec(&req).unwrap();
  120. let encoded_resp = net.request("/openreq".to_string(), encoded_req).await;
  121. let decoded_resp: open_invite::Response = serde_json::from_slice(&encoded_resp).unwrap();
  122. let (cred, bridgeline) = open_invite::handle_response(state, decoded_resp, &lox_pub).unwrap();
  123. (cred, bridgeline)
  124. }
  125. // Get a migration credential to migrate to higher trust
  126. pub async fn trust_promotion(
  127. net: &dyn Networking,
  128. lox_cred: &lox_library::cred::Lox,
  129. lox_pub: &IssuerPubKey,
  130. ) -> lox_library::cred::Migration {
  131. let (req, state) = trust_promotion::request(&lox_cred, &lox_pub, get_today(net).await).unwrap();
  132. let encoded_req: Vec<u8> = serde_json::to_vec(&req).unwrap();
  133. let encoded_resp = net.request("/trustpromo".to_string(), 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. net: &dyn Networking,
  141. lox_cred: &lox_library::cred::Lox,
  142. migration_cred: &lox_library::cred::Migration,
  143. lox_pub: &IssuerPubKey,
  144. migration_pub: &IssuerPubKey,
  145. ) -> lox_library::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("/trustmig".to_string(), 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. net: &dyn Networking,
  157. lox_cred: &lox_library::cred::Lox,
  158. encbuckets: &HashMap<u32, EncryptedBucket>,
  159. lox_pub: &IssuerPubKey,
  160. reachability_pub: &IssuerPubKey,
  161. ) -> lox_library::cred::Lox {
  162. // Read the bucket in the credential to get today's Bucket
  163. // Reachability credential
  164. let (id, key) = lox_library::bridge_table::from_scalar(lox_cred.bucket).unwrap();
  165. let bucket = lox_library::bridge_table::BridgeTable::decrypt_bucket(
  166. id,
  167. &key,
  168. &encbuckets.get(&id).unwrap(),
  169. )
  170. .unwrap();
  171. let reachcred = bucket.1.unwrap();
  172. // Use the Bucket Reachability credential to advance to the next
  173. // level
  174. let (req, state) = level_up::request(
  175. lox_cred,
  176. &reachcred,
  177. lox_pub,
  178. reachability_pub,
  179. get_today(net).await,
  180. )
  181. .unwrap();
  182. let encoded_req: Vec<u8> = serde_json::to_vec(&req).unwrap();
  183. let encoded_resp = net.request("/levelup".to_string(), 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. net: &dyn Networking,
  191. lox_cred: &lox_library::cred::Lox,
  192. encbuckets: &HashMap<u32, EncryptedBucket>,
  193. lox_pub: &IssuerPubKey,
  194. reachability_pub: &IssuerPubKey,
  195. invitation_pub: &IssuerPubKey,
  196. ) -> (lox_library::cred::Lox, lox_library::cred::Invitation) {
  197. // Read the bucket in the credential to get today's Bucket
  198. // Reachability credential
  199. let (id, key) = lox_library::bridge_table::from_scalar(lox_cred.bucket).unwrap();
  200. let bucket = lox_library::bridge_table::BridgeTable::decrypt_bucket(
  201. id,
  202. &key,
  203. &encbuckets.get(&id).unwrap(),
  204. )
  205. .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. get_today(net).await,
  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_library::cred::Invitation,
  226. lox_pub: &IssuerPubKey,
  227. invitation_pub: &IssuerPubKey,
  228. ) -> lox_library::cred::Lox {
  229. let (req, state) =
  230. redeem_invite::request(invite, invitation_pub, get_today(net).await).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_library::cred::Lox,
  241. lox_pub: &IssuerPubKey,
  242. ) -> lox_library::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_library::cred::Lox,
  254. migcred: &lox_library::cred::Migration,
  255. lox_pub: &IssuerPubKey,
  256. migration_pub: &IssuerPubKey,
  257. ) -> lox_library::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. }
  268. // Advance days on server (ONLY FOR TESTING)
  269. pub async fn advance_days(net: &dyn Networking, days: u16) -> u32 {
  270. let resp = net
  271. .request(
  272. "/advancedays".to_string(),
  273. serde_json::to_vec(&days).unwrap(),
  274. )
  275. .await;
  276. let today: u32 = serde_json::from_slice(&resp).unwrap();
  277. today
  278. }