lib.rs 10 KB

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