client_lib.rs 11 KB

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