tilo_secrets/
device_proof.rs1#[cfg(feature = "host-keyring")]
10use std::os::unix::ffi::OsStringExt;
11use std::sync::Mutex;
12
13use tilo_seal::secret_service::CallerKey;
14
15#[cfg(feature = "host-keyring")]
16const SERVICE: &str = "org.freedesktop.secrets";
17#[cfg(feature = "host-keyring")]
18const MANAGEMENT_PATH: &str = "/so/tilo/Secrets1";
19#[cfg(feature = "host-keyring")]
20const MANAGEMENT: &str = "so.tilo.Secrets1";
21
22static ARMED: Mutex<Option<CallerKey>> = Mutex::new(None);
23
24pub fn arm(device_secret: &[u8]) {
27 let Ok(store) = CallerKey::derive(device_secret) else {
28 return;
29 };
30 if let Ok(mut armed) = ARMED.lock() {
31 *armed = Some(store);
32 }
33}
34
35#[cfg(feature = "host-keyring")]
36fn dumpable() -> bool {
37 unsafe { libc::prctl(libc::PR_GET_DUMPABLE, 0, 0, 0, 0) == 1 }
39}
40
41#[cfg(feature = "host-keyring")]
42fn key() -> Option<CallerKey> {
43 if dumpable() {
44 return None;
45 }
46 ARMED.lock().ok()?.clone()
47}
48
49#[cfg(feature = "host-keyring")]
53pub(crate) fn in_use() -> bool {
54 key().is_some()
55}
56
57#[cfg(feature = "host-keyring")]
58fn answer(key: &CallerKey, challenge: &[u8], caller: &str) -> Option<(Vec<u8>, Vec<u8>)> {
59 let program = std::fs::read_link("/proc/self/exe")
61 .ok()?
62 .into_os_string()
63 .into_vec();
64 let proof = key.prove(challenge, caller, &program).ok()?;
65 Some((program, proof.to_vec()))
66}
67
68#[cfg(feature = "host-keyring")]
74pub(crate) fn prove_blocking(connection: &zbus::blocking::Connection) -> zbus::Result<bool> {
75 let Some(key) = key() else {
76 return Ok(false);
77 };
78 let caller = connection
79 .unique_name()
80 .ok_or_else(|| zbus::Error::Failure("a bus connection has no unique name".into()))?
81 .to_string();
82 let store = zbus::blocking::Proxy::new(connection, SERVICE, MANAGEMENT_PATH, MANAGEMENT)?;
83 let challenge: Vec<u8> = store.call("CallerChallenge", &())?;
84 let Some((program, proof)) = answer(&key, &challenge, &caller) else {
85 return Ok(false);
86 };
87 let _identity: String = store.call("ProveCaller", &(program, proof))?;
88 Ok(true)
89}
90
91#[cfg(feature = "host-keyring")]
92pub async fn prove(connection: &zbus::Connection) -> zbus::Result<bool> {
93 let Some(key) = key() else {
94 return Ok(false);
95 };
96 let caller = connection
97 .unique_name()
98 .ok_or_else(|| zbus::Error::Failure("a bus connection has no unique name".into()))?
99 .to_string();
100 let store = zbus::Proxy::new(connection, SERVICE, MANAGEMENT_PATH, MANAGEMENT).await?;
101 let challenge: Vec<u8> = store.call("CallerChallenge", &()).await?;
102 let Some((program, proof)) = answer(&key, &challenge, &caller) else {
103 return Ok(false);
104 };
105 let _identity: String = store.call("ProveCaller", &(program, proof)).await?;
106 Ok(true)
107}