client_lib.rs 11 KB

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