client_lib.rs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. use async_trait::async_trait;
  2. use curve25519_dalek::scalar::Scalar;
  3. use lox_library::bridge_table::from_scalar;
  4. use lox_library::bridge_table::BridgeLine;
  5. use lox_library::bridge_table::BridgeTable;
  6. use lox_library::bridge_table::EncryptedBucket;
  7. use lox_library::bridge_table::MAX_BRIDGES_PER_BUCKET;
  8. use lox_library::proto::*;
  9. use lox_library::scalar_u32;
  10. use lox_library::IssuerPubKey;
  11. use lox_library::OPENINV_LENGTH;
  12. use lox_utils::EncBridgeTable;
  13. use lox_utils::Invite;
  14. use serde::{Deserialize, Serialize};
  15. use serde_with::serde_as;
  16. use std::collections::HashMap;
  17. use std::time::Duration;
  18. // used for testing function
  19. use std::io::Write;
  20. // provides a generic way to make network requests
  21. #[async_trait]
  22. pub trait Networking {
  23. async fn request(&self, endpoint: String, body: Vec<u8>) -> Vec<u8>;
  24. }
  25. // Helper functions to get public keys from vector
  26. pub fn get_lox_pub(lox_auth_pubkeys: &Vec<IssuerPubKey>) -> &IssuerPubKey {
  27. &lox_auth_pubkeys[0]
  28. }
  29. pub fn get_migration_pub(lox_auth_pubkeys: &Vec<IssuerPubKey>) -> &IssuerPubKey {
  30. &lox_auth_pubkeys[1]
  31. }
  32. pub fn get_migrationkey_pub(lox_auth_pubkeys: &Vec<IssuerPubKey>) -> &IssuerPubKey {
  33. &lox_auth_pubkeys[2]
  34. }
  35. pub fn get_reachability_pub(lox_auth_pubkeys: &Vec<IssuerPubKey>) -> &IssuerPubKey {
  36. &lox_auth_pubkeys[3]
  37. }
  38. pub fn get_invitation_pub(lox_auth_pubkeys: &Vec<IssuerPubKey>) -> &IssuerPubKey {
  39. &lox_auth_pubkeys[4]
  40. }
  41. // Helper function to check if credential is eligible for
  42. // promotion from level 0 to 1
  43. pub async fn eligible_for_trust_promotion(
  44. net: &dyn Networking,
  45. cred: &lox_library::cred::Lox,
  46. ) -> bool {
  47. let level_since: u32 = match scalar_u32(&cred.level_since) {
  48. Some(v) => v,
  49. None => return false,
  50. };
  51. scalar_u32(&cred.trust_level).unwrap() == 0
  52. && level_since + trust_promotion::UNTRUSTED_INTERVAL <= get_today(net).await
  53. }
  54. // Helper function to check if credential is eligible for
  55. // level up from level 1+
  56. pub async fn eligible_for_level_up(net: &dyn Networking, cred: &lox_library::cred::Lox) -> bool {
  57. let level_since: u32 = match scalar_u32(&cred.level_since) {
  58. Some(v) => v,
  59. None => return false,
  60. };
  61. let trust_level: usize = scalar_u32(&cred.trust_level).unwrap().try_into().unwrap();
  62. trust_level > 0
  63. && level_since + lox_library::proto::level_up::LEVEL_INTERVAL[trust_level]
  64. <= get_today(net).await
  65. }
  66. // Get current date from Lox Auth
  67. pub async fn get_today(net: &dyn Networking) -> u32 {
  68. let resp = net.request("/today".to_string(), [].to_vec()).await;
  69. let today: u32 = serde_json::from_slice(&resp).unwrap();
  70. today
  71. }
  72. // Download Lox Auth pubkeys
  73. pub async fn get_lox_auth_keys(net: &dyn Networking) -> Vec<IssuerPubKey> {
  74. let resp = net.request("/pubkeys".to_string(), [].to_vec()).await;
  75. let lox_auth_pubkeys: Vec<IssuerPubKey> = serde_json::from_slice(&resp).unwrap();
  76. lox_auth_pubkeys
  77. }
  78. // Get encrypted bridge table
  79. pub async fn get_reachability_credential(net: &dyn Networking) -> HashMap<u32, EncryptedBucket> {
  80. let resp = net.request("/reachability".to_string(), [].to_vec()).await;
  81. let reachability_cred: EncBridgeTable = serde_json::from_slice(&resp).unwrap();
  82. reachability_cred.etable
  83. }
  84. // Get encrypted bridge table from BridgeDB and decrypt our entry
  85. pub async fn get_bucket(
  86. net: &dyn Networking,
  87. lox_cred: &lox_library::cred::Lox,
  88. ) -> [BridgeLine; MAX_BRIDGES_PER_BUCKET] {
  89. let encbuckets = get_reachability_credential(net).await;
  90. let (id, key) = from_scalar(lox_cred.bucket).unwrap();
  91. let encbucket = match encbuckets.get(&id) {
  92. Some(encbucket) => encbucket,
  93. None => panic!("Provided ID not found"),
  94. };
  95. BridgeTable::decrypt_bucket(id, &key, &encbucket).unwrap().0
  96. }
  97. // Get an open invitation
  98. pub async fn get_open_invitation(net: &dyn Networking) -> [u8; OPENINV_LENGTH] {
  99. let resp = net.request("/invite".to_string(), [].to_vec()).await;
  100. let open_invite: [u8; OPENINV_LENGTH] = serde_json::from_slice::<Invite>(&resp).unwrap().invite;
  101. open_invite
  102. }
  103. // Get a Lox Credential from an open invitation
  104. pub async fn get_lox_credential(
  105. net: &dyn Networking,
  106. open_invite: &[u8; OPENINV_LENGTH],
  107. lox_pub: &IssuerPubKey,
  108. ) -> (lox_library::cred::Lox, BridgeLine) {
  109. let (req, state) = open_invite::request(&open_invite);
  110. let encoded_req: Vec<u8> = serde_json::to_vec(&req).unwrap();
  111. let encoded_resp = net.request("/openreq".to_string(), encoded_req).await;
  112. let decoded_resp: open_invite::Response = serde_json::from_slice(&encoded_resp).unwrap();
  113. let (cred, bridgeline) = open_invite::handle_response(state, decoded_resp, &lox_pub).unwrap();
  114. (cred, bridgeline)
  115. }
  116. // Get a migration credential to migrate to higher trust
  117. pub async fn trust_promotion(
  118. net: &dyn Networking,
  119. lox_cred: &lox_library::cred::Lox,
  120. lox_pub: &IssuerPubKey,
  121. ) -> lox_library::cred::Migration {
  122. let (req, state) = trust_promotion::request(&lox_cred, &lox_pub, get_today(net).await).unwrap();
  123. let encoded_req: Vec<u8> = serde_json::to_vec(&req).unwrap();
  124. let encoded_resp = net.request("/trustpromo".to_string(), encoded_req).await;
  125. let decoded_resp: trust_promotion::Response = serde_json::from_slice(&encoded_resp).unwrap();
  126. let migration_cred = trust_promotion::handle_response(state, decoded_resp).unwrap();
  127. migration_cred
  128. }
  129. // Promote from untrusted (trust level 0) to trusted (trust level 1)
  130. pub async fn trust_migration(
  131. net: &dyn Networking,
  132. lox_cred: &lox_library::cred::Lox,
  133. migration_cred: &lox_library::cred::Migration,
  134. lox_pub: &IssuerPubKey,
  135. migration_pub: &IssuerPubKey,
  136. ) -> lox_library::cred::Lox {
  137. let (req, state) =
  138. migration::request(lox_cred, migration_cred, lox_pub, migration_pub).unwrap();
  139. let encoded_req: Vec<u8> = serde_json::to_vec(&req).unwrap();
  140. let encoded_resp = net.request("/trustmig".to_string(), encoded_req).await;
  141. let decoded_resp: migration::Response = serde_json::from_slice(&encoded_resp).unwrap();
  142. let cred = migration::handle_response(state, decoded_resp, lox_pub).unwrap();
  143. cred
  144. }
  145. // Increase trust from at least level 1 to higher levels
  146. pub async fn level_up(
  147. net: &dyn Networking,
  148. lox_cred: &lox_library::cred::Lox,
  149. encbuckets: &HashMap<u32, EncryptedBucket>,
  150. lox_pub: &IssuerPubKey,
  151. reachability_pub: &IssuerPubKey,
  152. ) -> (lox_library::cred::Lox, [BridgeLine; MAX_BRIDGES_PER_BUCKET]) {
  153. // Read the bucket in the credential to get today's Bucket
  154. // Reachability credential
  155. let (id, key) = from_scalar(lox_cred.bucket).unwrap();
  156. let bucket = BridgeTable::decrypt_bucket(id, &key, &encbuckets.get(&id).unwrap()).unwrap();
  157. let reachcred = bucket.1.unwrap();
  158. // Use the Bucket Reachability credential to advance to the next
  159. // level
  160. let (req, state) = level_up::request(
  161. lox_cred,
  162. &reachcred,
  163. lox_pub,
  164. reachability_pub,
  165. get_today(net).await,
  166. )
  167. .unwrap();
  168. let encoded_req: Vec<u8> = serde_json::to_vec(&req).unwrap();
  169. let encoded_resp = net.request("/levelup".to_string(), encoded_req).await;
  170. let decoded_resp: level_up::Response = serde_json::from_slice(&encoded_resp).unwrap();
  171. let cred = level_up::handle_response(state, decoded_resp, lox_pub).unwrap();
  172. // Get bucket
  173. let (id, key) = from_scalar(lox_cred.bucket).unwrap();
  174. let bucket = BridgeTable::decrypt_bucket(id, &key, &encbuckets.get(&id).unwrap()).unwrap();
  175. (cred, bucket.0)
  176. }
  177. // Request an Invitation credential to give to a friend
  178. pub async fn issue_invite(
  179. net: &dyn Networking,
  180. lox_cred: &lox_library::cred::Lox,
  181. encbuckets: &HashMap<u32, EncryptedBucket>,
  182. lox_pub: &IssuerPubKey,
  183. reachability_pub: &IssuerPubKey,
  184. invitation_pub: &IssuerPubKey,
  185. ) -> (lox_library::cred::Lox, lox_library::cred::Invitation) {
  186. // Read the bucket in the credential to get today's Bucket
  187. // Reachability credential
  188. let (id, key) = from_scalar(lox_cred.bucket).unwrap();
  189. let bucket = BridgeTable::decrypt_bucket(id, &key, &encbuckets.get(&id).unwrap()).unwrap();
  190. let reachcred = bucket.1.unwrap();
  191. let (req, state) = issue_invite::request(
  192. lox_cred,
  193. &reachcred,
  194. lox_pub,
  195. reachability_pub,
  196. get_today(net).await,
  197. )
  198. .unwrap();
  199. let encoded_req: Vec<u8> = serde_json::to_vec(&req).unwrap();
  200. let encoded_resp = net.request("/issueinvite".to_string(), encoded_req).await;
  201. let decoded_resp: issue_invite::Response = serde_json::from_slice(&encoded_resp).unwrap();
  202. let (cred, invite) =
  203. issue_invite::handle_response(state, decoded_resp, lox_pub, invitation_pub).unwrap();
  204. (cred, invite)
  205. }
  206. // Redeem an Invitation credential to start at trust level 1
  207. pub async fn redeem_invite(
  208. net: &dyn Networking,
  209. invite: &lox_library::cred::Invitation,
  210. lox_pub: &IssuerPubKey,
  211. invitation_pub: &IssuerPubKey,
  212. ) -> (lox_library::cred::Lox, [BridgeLine; MAX_BRIDGES_PER_BUCKET]) {
  213. let (req, state) =
  214. redeem_invite::request(invite, invitation_pub, get_today(net).await).unwrap();
  215. let encoded_req: Vec<u8> = serde_json::to_vec(&req).unwrap();
  216. let encoded_resp = net.request("/redeem".to_string(), encoded_req).await;
  217. let decoded_resp: redeem_invite::Response = serde_json::from_slice(&encoded_resp).unwrap();
  218. let cred = redeem_invite::handle_response(state, decoded_resp, lox_pub).unwrap();
  219. let bucket = get_bucket(net, &cred).await;
  220. (cred, bucket)
  221. }
  222. // Check for a migration credential to move to a new bucket
  223. pub async fn check_blockage(
  224. net: &dyn Networking,
  225. lox_cred: &lox_library::cred::Lox,
  226. lox_pub: &IssuerPubKey,
  227. ) -> lox_library::cred::Migration {
  228. let (req, state) = check_blockage::request(lox_cred, lox_pub).unwrap();
  229. let encoded_req: Vec<u8> = serde_json::to_vec(&req).unwrap();
  230. let encoded_resp = net.request("/checkblockage".to_string(), encoded_req).await;
  231. let decoded_resp: check_blockage::Response = serde_json::from_slice(&encoded_resp).unwrap();
  232. let migcred = check_blockage::handle_response(state, decoded_resp).unwrap();
  233. migcred
  234. }
  235. // Migrate to a new bucket (must be level >= 3)
  236. pub async fn blockage_migration(
  237. net: &dyn Networking,
  238. lox_cred: &lox_library::cred::Lox,
  239. migcred: &lox_library::cred::Migration,
  240. lox_pub: &IssuerPubKey,
  241. migration_pub: &IssuerPubKey,
  242. ) -> lox_library::cred::Lox {
  243. let (req, state) =
  244. blockage_migration::request(lox_cred, migcred, lox_pub, migration_pub).unwrap();
  245. let encoded_req: Vec<u8> = serde_json::to_vec(&req).unwrap();
  246. let encoded_resp = net
  247. .request("/blockagemigration".to_string(), encoded_req)
  248. .await;
  249. let decoded_resp: blockage_migration::Response = serde_json::from_slice(&encoded_resp).unwrap();
  250. let cred = blockage_migration::handle_response(state, decoded_resp, lox_pub).unwrap();
  251. cred
  252. }
  253. #[cfg(test)]
  254. // Advance days on server
  255. pub async fn advance_days(net: &dyn Networking, days: u16) -> u32 {
  256. let resp = net
  257. .request(
  258. "/advancedays".to_string(),
  259. serde_json::to_vec(&days).unwrap(),
  260. )
  261. .await;
  262. let today: u32 = serde_json::from_slice(&resp).unwrap();
  263. today
  264. }