lib.rs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. use anyhow::{anyhow, Result};
  2. use lox_library::{
  3. bridge_table::{from_scalar, BridgeLine, BridgeTable, EncryptedBucket, MAX_BRIDGES_PER_BUCKET},
  4. cred,
  5. proto::{
  6. level_up::{LEVEL_INTERVAL, MAX_BLOCKAGES},
  7. *,
  8. },
  9. scalar_u32, IssuerPubKey, OPENINV_LENGTH,
  10. };
  11. use lox_utils::{EncBridgeTable, Invite};
  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. match scalar_u32(&cred.trust_level) {
  49. Some(v) => v == 0 && level_since + trust_promotion::UNTRUSTED_INTERVAL <= date,
  50. None => false,
  51. }
  52. }
  53. // Helper function to check if credential is eligible for
  54. // level up from level 1+
  55. pub async fn eligible_for_level_up(net: &dyn Networking, cred: &lox_library::cred::Lox) -> bool {
  56. let level_since: u32 = match scalar_u32(&cred.level_since) {
  57. Some(v) => v,
  58. None => return false,
  59. };
  60. let date = match get_today(net).await {
  61. Ok(v) => v,
  62. Err(e) => {
  63. eprintln!("Failed to get date, error: {}", e);
  64. return false;
  65. }
  66. };
  67. let trust_level = match scalar_u32(&cred.trust_level) {
  68. Some(v) => v,
  69. None => return false,
  70. };
  71. let blockages = match scalar_u32(&cred.blockages) {
  72. Some(v) => v,
  73. None => return false,
  74. };
  75. trust_level > 0
  76. && blockages <= MAX_BLOCKAGES[trust_level as usize]
  77. && level_since + LEVEL_INTERVAL[trust_level as usize] <= date
  78. }
  79. // Get current date from Lox Auth
  80. pub async fn get_today(net: &dyn Networking) -> Result<u32> {
  81. let resp = net.request("/today".to_string(), [].to_vec()).await?;
  82. let today: u32 = serde_json::from_slice(&resp)?;
  83. Ok(today)
  84. }
  85. // Download Lox Auth pubkeys
  86. pub async fn get_lox_auth_keys(net: &dyn Networking) -> Result<Vec<IssuerPubKey>> {
  87. let resp = net.request("/pubkeys".to_string(), [].to_vec()).await?;
  88. let lox_auth_pubkeys: Vec<IssuerPubKey> = serde_json::from_slice(&resp)?;
  89. Ok(lox_auth_pubkeys)
  90. }
  91. // Get encrypted bridge table
  92. pub async fn get_reachability_credential(
  93. net: &dyn Networking,
  94. ) -> Result<HashMap<u32, EncryptedBucket>> {
  95. let resp = net
  96. .request("/reachability".to_string(), [].to_vec())
  97. .await?;
  98. let reachability_cred: EncBridgeTable = serde_json::from_slice(&resp)?;
  99. Ok(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. ) -> Result<(
  106. [BridgeLine; MAX_BRIDGES_PER_BUCKET],
  107. Option<cred::BucketReachability>,
  108. )> {
  109. let encbuckets = get_reachability_credential(net).await?;
  110. let (id, key) = match from_scalar(lox_cred.bucket) {
  111. Ok((id, key)) => (id, key),
  112. Err(e) => {
  113. return Err(anyhow!(
  114. "aead error returned when trying to get id and key from bucket: {}",
  115. e
  116. ))
  117. }
  118. };
  119. let encbucket = match encbuckets.get(&id) {
  120. Some(v) => v,
  121. None => {
  122. return Err(anyhow!(
  123. "Unable to get encrypted bucket from encrypted bridge table"
  124. ))
  125. }
  126. };
  127. match BridgeTable::decrypt_bucket(id, &key, &encbucket) {
  128. Ok(v) => Ok(v),
  129. Err(e) => Err(anyhow!(
  130. "aead error returned when trying to decrypt bucket: {}",
  131. e
  132. )),
  133. }
  134. }
  135. // Get an open invitation
  136. pub async fn get_open_invitation(net: &dyn Networking) -> Result<[u8; OPENINV_LENGTH]> {
  137. let resp = net.request("/invite".to_string(), [].to_vec()).await?;
  138. let open_invite: [u8; OPENINV_LENGTH] = serde_json::from_slice::<Invite>(&resp)?.invite;
  139. Ok(open_invite)
  140. }
  141. // Get a Lox Credential from an open invitation
  142. pub async fn get_lox_credential(
  143. net: &dyn Networking,
  144. open_invite: &[u8; OPENINV_LENGTH],
  145. lox_pub: &IssuerPubKey,
  146. ) -> Result<(lox_library::cred::Lox, BridgeLine)> {
  147. let (req, state) = open_invite::request(&open_invite);
  148. let encoded_req: Vec<u8> = serde_json::to_vec(&req)?;
  149. let encoded_resp = net.request("/openreq".to_string(), encoded_req).await?;
  150. let decoded_resp: open_invite::Response = serde_json::from_slice(&encoded_resp)?;
  151. let (cred, bridgeline) = open_invite::handle_response(state, decoded_resp, &lox_pub)?;
  152. Ok((cred, bridgeline))
  153. }
  154. // Get a migration credential to migrate to higher trust
  155. pub async fn trust_promotion(
  156. net: &dyn Networking,
  157. lox_cred: &lox_library::cred::Lox,
  158. lox_pub: &IssuerPubKey,
  159. ) -> Result<lox_library::cred::Migration> {
  160. let (req, state) = trust_promotion::request(&lox_cred, &lox_pub, get_today(net).await?)?;
  161. let encoded_req: Vec<u8> = serde_json::to_vec(&req)?;
  162. let encoded_resp = net.request("/trustpromo".to_string(), encoded_req).await?;
  163. let decoded_resp: trust_promotion::Response = serde_json::from_slice(&encoded_resp)?;
  164. let migration_cred = trust_promotion::handle_response(state, decoded_resp)?;
  165. Ok(migration_cred)
  166. }
  167. // Promote from untrusted (trust level 0) to trusted (trust level 1)
  168. pub async fn trust_migration(
  169. net: &dyn Networking,
  170. lox_cred: &lox_library::cred::Lox,
  171. migration_cred: &lox_library::cred::Migration,
  172. lox_pub: &IssuerPubKey,
  173. migration_pub: &IssuerPubKey,
  174. ) -> Result<lox_library::cred::Lox> {
  175. let (req, state) = migration::request(lox_cred, migration_cred, lox_pub, migration_pub)?;
  176. let encoded_req: Vec<u8> = serde_json::to_vec(&req)?;
  177. let encoded_resp = net.request("/trustmig".to_string(), encoded_req).await?;
  178. let decoded_resp: migration::Response = serde_json::from_slice(&encoded_resp)?;
  179. let cred = migration::handle_response(state, decoded_resp, lox_pub)?;
  180. Ok(cred)
  181. }
  182. // Increase trust from at least level 1 to higher levels
  183. pub async fn level_up(
  184. net: &dyn Networking,
  185. lox_cred: &lox_library::cred::Lox,
  186. reachcred: &cred::BucketReachability,
  187. lox_pub: &IssuerPubKey,
  188. reachability_pub: &IssuerPubKey,
  189. ) -> Result<lox_library::cred::Lox> {
  190. let (req, state) = level_up::request(
  191. lox_cred,
  192. &reachcred,
  193. lox_pub,
  194. reachability_pub,
  195. get_today(net).await?,
  196. )?;
  197. let encoded_req: Vec<u8> = serde_json::to_vec(&req)?;
  198. let encoded_resp = net.request("/levelup".to_string(), encoded_req).await?;
  199. let decoded_resp: level_up::Response = serde_json::from_slice(&encoded_resp)?;
  200. let cred = level_up::handle_response(state, decoded_resp, lox_pub)?;
  201. Ok(cred)
  202. }
  203. // Request an Invitation credential to give to a friend
  204. pub async fn issue_invite(
  205. net: &dyn Networking,
  206. lox_cred: &lox_library::cred::Lox,
  207. encbuckets: &HashMap<u32, EncryptedBucket>,
  208. lox_pub: &IssuerPubKey,
  209. reachability_pub: &IssuerPubKey,
  210. invitation_pub: &IssuerPubKey,
  211. ) -> Result<(lox_library::cred::Lox, lox_library::cred::Invitation)> {
  212. // Read the bucket in the credential to get today's Bucket
  213. // Reachability credential
  214. let (id, key) = match from_scalar(lox_cred.bucket) {
  215. Ok((id, key)) => (id, key),
  216. Err(e) => {
  217. return Err(anyhow!(
  218. "aead error returned when trying to get id and key from bucket: {}",
  219. e
  220. ))
  221. }
  222. };
  223. let encbucket = match encbuckets.get(&id) {
  224. Some(v) => v,
  225. None => {
  226. return Err(anyhow!(
  227. "Unable to get encrypted bucket from encrypted bridge table"
  228. ))
  229. }
  230. };
  231. let bucket = match BridgeTable::decrypt_bucket(id, &key, encbucket) {
  232. Ok(v) => v,
  233. Err(e) => {
  234. return Err(anyhow!(
  235. "aead error returned when trying to decrypt bucket: {}",
  236. e
  237. ))
  238. }
  239. };
  240. let reachcred = match bucket.1 {
  241. Some(v) => v,
  242. None => {
  243. return Err(anyhow!(
  244. "Expected reachability credential but none was found"
  245. ))
  246. }
  247. };
  248. let (req, state) = issue_invite::request(
  249. lox_cred,
  250. &reachcred,
  251. lox_pub,
  252. reachability_pub,
  253. get_today(net).await?,
  254. )?;
  255. let encoded_req: Vec<u8> = serde_json::to_vec(&req)?;
  256. let encoded_resp = net.request("/issueinvite".to_string(), encoded_req).await?;
  257. let decoded_resp: issue_invite::Response = serde_json::from_slice(&encoded_resp)?;
  258. let (cred, invite) =
  259. issue_invite::handle_response(state, decoded_resp, lox_pub, invitation_pub)?;
  260. Ok((cred, invite))
  261. }
  262. // Redeem an Invitation credential to start at trust level 1
  263. pub async fn redeem_invite(
  264. net: &dyn Networking,
  265. invite: &lox_library::cred::Invitation,
  266. lox_pub: &IssuerPubKey,
  267. invitation_pub: &IssuerPubKey,
  268. ) -> Result<(lox_library::cred::Lox, [BridgeLine; MAX_BRIDGES_PER_BUCKET])> {
  269. let (req, state) = redeem_invite::request(invite, invitation_pub, get_today(net).await?)?;
  270. let encoded_req: Vec<u8> = serde_json::to_vec(&req)?;
  271. let encoded_resp = net.request("/redeem".to_string(), encoded_req).await?;
  272. let decoded_resp: redeem_invite::Response = serde_json::from_slice(&encoded_resp)?;
  273. let cred = redeem_invite::handle_response(state, decoded_resp, lox_pub)?;
  274. let bucket = get_bucket(net, &cred).await?.0;
  275. Ok((cred, bucket))
  276. }
  277. // Check for a migration credential to move to a new bucket
  278. pub async fn check_blockage(
  279. net: &dyn Networking,
  280. lox_cred: &lox_library::cred::Lox,
  281. lox_pub: &IssuerPubKey,
  282. ) -> Result<lox_library::cred::Migration> {
  283. let (req, state) = check_blockage::request(lox_cred, lox_pub)?;
  284. let encoded_req: Vec<u8> = serde_json::to_vec(&req)?;
  285. let encoded_resp = net
  286. .request("/checkblockage".to_string(), encoded_req)
  287. .await?;
  288. let decoded_resp: check_blockage::Response = serde_json::from_slice(&encoded_resp)?;
  289. let migcred = check_blockage::handle_response(state, decoded_resp)?;
  290. Ok(migcred)
  291. }
  292. // Migrate to a new bucket (must be level >= 3)
  293. pub async fn blockage_migration(
  294. net: &dyn Networking,
  295. lox_cred: &lox_library::cred::Lox,
  296. migcred: &lox_library::cred::Migration,
  297. lox_pub: &IssuerPubKey,
  298. migration_pub: &IssuerPubKey,
  299. ) -> Result<lox_library::cred::Lox> {
  300. let (req, state) = blockage_migration::request(lox_cred, migcred, lox_pub, migration_pub)?;
  301. let encoded_req: Vec<u8> = serde_json::to_vec(&req)?;
  302. let encoded_resp = net
  303. .request("/blockagemigration".to_string(), encoded_req)
  304. .await?;
  305. let decoded_resp: blockage_migration::Response = serde_json::from_slice(&encoded_resp)?;
  306. let cred = blockage_migration::handle_response(state, decoded_resp, lox_pub)?;
  307. Ok(cred)
  308. }
  309. #[cfg(test)]
  310. mod tests;