lib.rs 9.8 KB

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