client_lib.rs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 get credential trust level as i8
  42. // (Note that this value should only be 0-4)
  43. pub fn get_cred_trust_level(cred: &lox_library::cred::Lox) -> i8 {
  44. let trust_levels: [Scalar; 5] = [
  45. Scalar::zero(),
  46. Scalar::one(),
  47. Scalar::from(2u64),
  48. Scalar::from(3u64),
  49. Scalar::from(4u8),
  50. ];
  51. for i in 0..trust_levels.len() {
  52. if cred.trust_level == trust_levels[i] {
  53. return i.try_into().unwrap();
  54. }
  55. }
  56. -1
  57. }
  58. // Helper function to check if credential is eligible for
  59. // promotion from level 0 to 1
  60. pub async fn eligible_for_trust_promotion(
  61. net: &dyn Networking,
  62. cred: &lox_library::cred::Lox,
  63. ) -> bool {
  64. let level_since: u32 = match scalar_u32(&cred.level_since) {
  65. Some(v) => v,
  66. None => return false,
  67. };
  68. get_cred_trust_level(cred) == 0
  69. && level_since + lox_library::proto::trust_promotion::UNTRUSTED_INTERVAL
  70. <= get_today(net).await
  71. }
  72. // Helper function to check if credential is eligible for
  73. // level up from level 1+
  74. pub async fn eligible_for_level_up(net: &dyn Networking, cred: &lox_library::cred::Lox) -> bool {
  75. let level_since: u32 = match scalar_u32(&cred.level_since) {
  76. Some(v) => v,
  77. None => return false,
  78. };
  79. let trust_level: usize = get_cred_trust_level(cred).try_into().unwrap();
  80. trust_level > 0
  81. && level_since + lox_library::proto::level_up::LEVEL_INTERVAL[trust_level]
  82. <= get_today(net).await
  83. }
  84. // Get current date from Lox Auth
  85. pub async fn get_today(net: &dyn Networking) -> u32 {
  86. let resp = net.request("/today".to_string(), [].to_vec()).await;
  87. let today: u32 = serde_json::from_slice(&resp).unwrap();
  88. today
  89. }
  90. // Download Lox Auth pubkeys
  91. pub async fn get_lox_auth_keys(net: &dyn Networking) -> Vec<IssuerPubKey> {
  92. let resp = net.request("/pubkeys".to_string(), [].to_vec()).await;
  93. let lox_auth_pubkeys: Vec<IssuerPubKey> = serde_json::from_slice(&resp).unwrap();
  94. lox_auth_pubkeys
  95. }
  96. // Get encrypted bridge table
  97. pub async fn get_reachability_credential(net: &dyn Networking) -> HashMap<u32, EncryptedBucket> {
  98. let resp = net.request("/reachability".to_string(), [].to_vec()).await;
  99. let reachability_cred: EncBridgeTable = serde_json::from_slice(&resp).unwrap();
  100. reachability_cred.etable
  101. }
  102. // Get encrypted bridge table from BridgeDB and decrypt our entry
  103. pub async fn get_bucket(
  104. net: &dyn Networking,
  105. lox_cred: &lox_library::cred::Lox,
  106. ) -> [BridgeLine; MAX_BRIDGES_PER_BUCKET] {
  107. let encbuckets = get_reachability_credential(net).await;
  108. let (id, key) = from_scalar(lox_cred.bucket).unwrap();
  109. let encbucket = match encbuckets.get(&id) {
  110. Some(encbucket) => encbucket,
  111. None => panic!("Provided ID not found"),
  112. };
  113. BridgeTable::decrypt_bucket(id, &key, &encbucket).unwrap().0
  114. }
  115. // Get an open invitation
  116. pub async fn get_open_invitation(net: &dyn Networking) -> [u8; OPENINV_LENGTH] {
  117. let resp = net.request("/invite".to_string(), [].to_vec()).await;
  118. let open_invite: [u8; OPENINV_LENGTH] = serde_json::from_slice::<Invite>(&resp).unwrap().invite;
  119. open_invite
  120. }
  121. // Get a Lox Credential from an open invitation
  122. pub async fn get_lox_credential(
  123. net: &dyn Networking,
  124. open_invite: &[u8; OPENINV_LENGTH],
  125. lox_pub: &IssuerPubKey,
  126. ) -> (lox_library::cred::Lox, BridgeLine) {
  127. let (req, state) = open_invite::request(&open_invite);
  128. let encoded_req: Vec<u8> = serde_json::to_vec(&req).unwrap();
  129. let encoded_resp = net.request("/openreq".to_string(), encoded_req).await;
  130. let decoded_resp: open_invite::Response = serde_json::from_slice(&encoded_resp).unwrap();
  131. let (cred, bridgeline) = open_invite::handle_response(state, decoded_resp, &lox_pub).unwrap();
  132. (cred, bridgeline)
  133. }
  134. // Get a migration credential to migrate to higher trust
  135. pub async fn trust_promotion(
  136. net: &dyn Networking,
  137. lox_cred: &lox_library::cred::Lox,
  138. lox_pub: &IssuerPubKey,
  139. ) -> lox_library::cred::Migration {
  140. let (req, state) = trust_promotion::request(&lox_cred, &lox_pub, get_today(net).await).unwrap();
  141. let encoded_req: Vec<u8> = serde_json::to_vec(&req).unwrap();
  142. let encoded_resp = net.request("/trustpromo".to_string(), encoded_req).await;
  143. let decoded_resp: trust_promotion::Response = serde_json::from_slice(&encoded_resp).unwrap();
  144. let migration_cred = trust_promotion::handle_response(state, decoded_resp).unwrap();
  145. migration_cred
  146. }
  147. // Promote from untrusted (trust level 0) to trusted (trust level 1)
  148. pub async fn trust_migration(
  149. net: &dyn Networking,
  150. lox_cred: &lox_library::cred::Lox,
  151. migration_cred: &lox_library::cred::Migration,
  152. lox_pub: &IssuerPubKey,
  153. migration_pub: &IssuerPubKey,
  154. ) -> lox_library::cred::Lox {
  155. let (req, state) =
  156. migration::request(lox_cred, migration_cred, lox_pub, migration_pub).unwrap();
  157. let encoded_req: Vec<u8> = serde_json::to_vec(&req).unwrap();
  158. let encoded_resp = net.request("/trustmig".to_string(), encoded_req).await;
  159. let decoded_resp: migration::Response = serde_json::from_slice(&encoded_resp).unwrap();
  160. let cred = migration::handle_response(state, decoded_resp, lox_pub).unwrap();
  161. cred
  162. }
  163. // Increase trust from at least level 1 to higher levels
  164. pub async fn level_up(
  165. net: &dyn Networking,
  166. lox_cred: &lox_library::cred::Lox,
  167. encbuckets: &HashMap<u32, EncryptedBucket>,
  168. lox_pub: &IssuerPubKey,
  169. reachability_pub: &IssuerPubKey,
  170. ) -> lox_library::cred::Lox {
  171. // Read the bucket in the credential to get today's Bucket
  172. // Reachability credential
  173. let (id, key) = lox_library::bridge_table::from_scalar(lox_cred.bucket).unwrap();
  174. let bucket = lox_library::bridge_table::BridgeTable::decrypt_bucket(
  175. id,
  176. &key,
  177. &encbuckets.get(&id).unwrap(),
  178. )
  179. .unwrap();
  180. let reachcred = bucket.1.unwrap();
  181. // Use the Bucket Reachability credential to advance to the next
  182. // level
  183. let (req, state) = level_up::request(
  184. lox_cred,
  185. &reachcred,
  186. lox_pub,
  187. reachability_pub,
  188. get_today(net).await,
  189. )
  190. .unwrap();
  191. let encoded_req: Vec<u8> = serde_json::to_vec(&req).unwrap();
  192. let encoded_resp = net.request("/levelup".to_string(), encoded_req).await;
  193. let decoded_resp: level_up::Response = serde_json::from_slice(&encoded_resp).unwrap();
  194. let cred = level_up::handle_response(state, decoded_resp, lox_pub).unwrap();
  195. cred
  196. }
  197. // Request an Invitation credential to give to a friend
  198. pub async fn issue_invite(
  199. net: &dyn Networking,
  200. lox_cred: &lox_library::cred::Lox,
  201. encbuckets: &HashMap<u32, EncryptedBucket>,
  202. lox_pub: &IssuerPubKey,
  203. reachability_pub: &IssuerPubKey,
  204. invitation_pub: &IssuerPubKey,
  205. ) -> (lox_library::cred::Lox, lox_library::cred::Invitation) {
  206. // Read the bucket in the credential to get today's Bucket
  207. // Reachability credential
  208. let (id, key) = lox_library::bridge_table::from_scalar(lox_cred.bucket).unwrap();
  209. let bucket = lox_library::bridge_table::BridgeTable::decrypt_bucket(
  210. id,
  211. &key,
  212. &encbuckets.get(&id).unwrap(),
  213. )
  214. .unwrap();
  215. let reachcred = bucket.1.unwrap();
  216. let (req, state) = issue_invite::request(
  217. lox_cred,
  218. &reachcred,
  219. lox_pub,
  220. reachability_pub,
  221. get_today(net).await,
  222. )
  223. .unwrap();
  224. let encoded_req: Vec<u8> = serde_json::to_vec(&req).unwrap();
  225. let encoded_resp = net.request("/issueinvite".to_string(), encoded_req).await;
  226. let decoded_resp: issue_invite::Response = serde_json::from_slice(&encoded_resp).unwrap();
  227. let (cred, invite) =
  228. issue_invite::handle_response(state, decoded_resp, lox_pub, invitation_pub).unwrap();
  229. (cred, invite)
  230. }
  231. // Redeem an Invitation credential to start at trust level 1
  232. pub async fn redeem_invite(
  233. net: &dyn Networking,
  234. invite: &lox_library::cred::Invitation,
  235. lox_pub: &IssuerPubKey,
  236. invitation_pub: &IssuerPubKey,
  237. ) -> lox_library::cred::Lox {
  238. let (req, state) =
  239. redeem_invite::request(invite, invitation_pub, get_today(net).await).unwrap();
  240. let encoded_req: Vec<u8> = serde_json::to_vec(&req).unwrap();
  241. let encoded_resp = net.request("/redeem".to_string(), encoded_req).await;
  242. let decoded_resp: redeem_invite::Response = serde_json::from_slice(&encoded_resp).unwrap();
  243. let cred = redeem_invite::handle_response(state, decoded_resp, lox_pub).unwrap();
  244. cred
  245. }
  246. // Check for a migration credential to move to a new bucket
  247. pub async fn check_blockage(
  248. net: &dyn Networking,
  249. lox_cred: &lox_library::cred::Lox,
  250. lox_pub: &IssuerPubKey,
  251. ) -> lox_library::cred::Migration {
  252. let (req, state) = check_blockage::request(lox_cred, lox_pub).unwrap();
  253. let encoded_req: Vec<u8> = serde_json::to_vec(&req).unwrap();
  254. let encoded_resp = net.request("/checkblockage".to_string(), encoded_req).await;
  255. let decoded_resp: check_blockage::Response = serde_json::from_slice(&encoded_resp).unwrap();
  256. let migcred = check_blockage::handle_response(state, decoded_resp).unwrap();
  257. migcred
  258. }
  259. // Migrate to a new bucket (must be level >= 3)
  260. pub async fn blockage_migration(
  261. net: &dyn Networking,
  262. lox_cred: &lox_library::cred::Lox,
  263. migcred: &lox_library::cred::Migration,
  264. lox_pub: &IssuerPubKey,
  265. migration_pub: &IssuerPubKey,
  266. ) -> lox_library::cred::Lox {
  267. let (req, state) =
  268. blockage_migration::request(lox_cred, migcred, lox_pub, migration_pub).unwrap();
  269. let encoded_req: Vec<u8> = serde_json::to_vec(&req).unwrap();
  270. let encoded_resp = net
  271. .request("/blockagemigration".to_string(), encoded_req)
  272. .await;
  273. let decoded_resp: blockage_migration::Response = serde_json::from_slice(&encoded_resp).unwrap();
  274. let cred = blockage_migration::handle_response(state, decoded_resp, lox_pub).unwrap();
  275. cred
  276. }
  277. // Advance days on server (ONLY FOR TESTING)
  278. pub async fn advance_days(net: &dyn Networking, days: u16) -> u32 {
  279. let resp = net
  280. .request(
  281. "/advancedays".to_string(),
  282. serde_json::to_vec(&days).unwrap(),
  283. )
  284. .await;
  285. let today: u32 = serde_json::from_slice(&resp).unwrap();
  286. today
  287. }