shex 2.0.3

An OPAQUE-authenticated, end-to-end encrypted Redis remote shell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
use std::{path::Path, sync::LazyLock};

#[cfg(target_os = "linux")]
use std::{fs, io::Write, path::PathBuf};

#[cfg(target_os = "linux")]
use anyhow::bail;
use anyhow::{Context, Result, anyhow};
#[cfg(target_os = "linux")]
use chacha20poly1305::{
    ChaCha20Poly1305, KeyInit,
    aead::{Aead, Payload},
};
use keyring_core::Entry as SystemEntry;
#[cfg(target_os = "linux")]
use serde::{Deserialize, Serialize};
#[cfg(target_os = "linux")]
use sha2::{Digest, Sha256};

#[cfg(target_os = "linux")]
const FILE_STORE_VERSION: u8 = 1;
#[cfg(target_os = "linux")]
const FILE_STORE_DIRECTORY: &str = "credentials";
#[cfg(target_os = "linux")]
const FILE_STORE_KEY: &str = "credential.key";

#[derive(Clone, Copy)]
enum Backend {
    System,
    #[cfg(target_os = "linux")]
    LinuxKernel,
    #[cfg(target_os = "linux")]
    EncryptedFile,
}

static BACKEND: LazyLock<std::result::Result<Backend, String>> = LazyLock::new(configure_backend);

pub enum Entry {
    System(SystemEntry),
    #[cfg(target_os = "linux")]
    EncryptedFile(FileEntry),
}

impl Entry {
    pub fn set_secret(&self, secret: &[u8]) -> Result<()> {
        match self {
            Self::System(entry) => entry
                .set_secret(secret)
                .context("could not write to the OS credential store"),
            #[cfg(target_os = "linux")]
            Self::EncryptedFile(entry) => entry.set_secret(secret),
        }
    }

    pub fn get_secret(&self) -> Result<Vec<u8>> {
        match self {
            Self::System(entry) => entry
                .get_secret()
                .context("could not read from the OS credential store"),
            #[cfg(target_os = "linux")]
            Self::EncryptedFile(entry) => entry.get_secret(),
        }
    }

    pub fn delete_credential(&self) -> Result<()> {
        match self {
            Self::System(entry) => entry
                .delete_credential()
                .context("could not delete from the OS credential store"),
            #[cfg(target_os = "linux")]
            Self::EncryptedFile(entry) => entry.delete_credential(),
        }
    }
}

pub fn entry(store_dir: &Path, service: &str, user: &str) -> Result<Entry> {
    #[cfg(not(target_os = "linux"))]
    let _ = store_dir;
    let backend = *BACKEND
        .as_ref()
        .map_err(|message| anyhow!("secure credential storage is unavailable: {message}"))?;
    match backend {
        Backend::System => SystemEntry::new(service, user)
            .map(Entry::System)
            .context("OS credential store is unavailable"),
        #[cfg(target_os = "linux")]
        Backend::LinuxKernel => SystemEntry::new(service, user)
            .map(Entry::System)
            .context("Linux kernel credential store is unavailable"),
        #[cfg(target_os = "linux")]
        Backend::EncryptedFile => Ok(Entry::EncryptedFile(FileEntry::new(
            store_dir, service, user,
        ))),
    }
}

pub fn fallback_notice() -> Option<&'static str> {
    match BACKEND.as_ref() {
        #[cfg(target_os = "linux")]
        Ok(Backend::LinuxKernel) => Some(
            "credential store: Linux kernel keyring (secure and memory-backed; re-add credentials after reboot)",
        ),
        #[cfg(target_os = "linux")]
        Ok(Backend::EncryptedFile) => Some(
            "credential store: encrypted local shex vault protected by private OS file permissions",
        ),
        _ => None,
    }
}

#[cfg(target_os = "macos")]
fn configure_backend() -> std::result::Result<Backend, String> {
    let store = apple_native_keyring_store::keychain::Store::new()
        .map_err(|error| format!("could not open macOS Keychain: {error}"))?;
    keyring_core::set_default_store(store);
    Ok(Backend::System)
}

#[cfg(target_os = "windows")]
fn configure_backend() -> std::result::Result<Backend, String> {
    let store = windows_native_keyring_store::Store::new()
        .map_err(|error| format!("could not open Windows Credential Manager: {error}"))?;
    keyring_core::set_default_store(store);
    Ok(Backend::System)
}

#[cfg(target_os = "linux")]
fn configure_backend() -> std::result::Result<Backend, String> {
    if std::env::var_os("DBUS_SESSION_BUS_ADDRESS").is_some() {
        if let Ok(store) = zbus_secret_service_keyring_store::Store::new() {
            keyring_core::set_default_store(store);
            if probe_current_store().is_ok() {
                return Ok(Backend::System);
            }
        }
    }

    if let Ok(store) = linux_keyutils_keyring_store::Store::new() {
        keyring_core::set_default_store(store);
        if probe_current_store().is_ok() {
            return Ok(Backend::LinuxKernel);
        }
    }

    Ok(Backend::EncryptedFile)
}

#[cfg(target_os = "linux")]
fn probe_current_store() -> std::result::Result<(), String> {
    let user = format!("probe-{:032x}", rand::random::<u128>());
    let entry = SystemEntry::new("shex-probe", &user).map_err(|error| error.to_string())?;
    let secret: [u8; 32] = rand::random();
    entry
        .set_secret(&secret)
        .map_err(|error| error.to_string())?;
    let result = entry
        .get_secret()
        .map_err(|error| error.to_string())
        .and_then(|stored| {
            if stored == secret {
                Ok(())
            } else {
                Err("credential probe returned different bytes".to_owned())
            }
        });
    let _ = entry.delete_credential();
    result
}

#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
fn configure_backend() -> std::result::Result<Backend, String> {
    Err("this operating system has no configured shex credential backend".to_owned())
}

#[cfg(target_os = "linux")]
#[derive(Serialize, Deserialize)]
struct FileEnvelope {
    version: u8,
    nonce: [u8; 12],
    ciphertext: Vec<u8>,
}

#[cfg(target_os = "linux")]
pub struct FileEntry {
    store_dir: PathBuf,
    path: PathBuf,
    aad: Vec<u8>,
}

#[cfg(target_os = "linux")]
impl FileEntry {
    fn new(store_dir: &Path, service: &str, user: &str) -> Self {
        let mut identity = service.as_bytes().to_vec();
        identity.push(0);
        identity.extend_from_slice(user.as_bytes());
        let name = hex_digest(&identity);
        Self {
            store_dir: store_dir.to_owned(),
            path: store_dir
                .join(FILE_STORE_DIRECTORY)
                .join(format!("{name}.secret")),
            aad: identity,
        }
    }

    fn set_secret(&self, secret: &[u8]) -> Result<()> {
        let key = load_or_create_file_key(&self.store_dir)?;
        let nonce: [u8; 12] = rand::random();
        let cipher = ChaCha20Poly1305::new_from_slice(&key)
            .map_err(|_| anyhow!("invalid local credential-store key"))?;
        let ciphertext = cipher
            .encrypt(
                (&nonce).into(),
                Payload {
                    msg: secret,
                    aad: &self.aad,
                },
            )
            .map_err(|_| anyhow!("could not encrypt the local credential"))?;
        let bytes = serde_json::to_vec(&FileEnvelope {
            version: FILE_STORE_VERSION,
            nonce,
            ciphertext,
        })?;
        write_private_replace(&self.path, &bytes)
    }

    fn get_secret(&self) -> Result<Vec<u8>> {
        let key = load_file_key(&self.store_dir)?;
        check_private_permissions(&self.path)?;
        let envelope: FileEnvelope = serde_json::from_slice(
            &fs::read(&self.path)
                .with_context(|| format!("could not read credential {}", self.path.display()))?,
        )
        .context("invalid encrypted credential")?;
        if envelope.version != FILE_STORE_VERSION {
            bail!("unsupported encrypted credential version");
        }
        let cipher = ChaCha20Poly1305::new_from_slice(&key)
            .map_err(|_| anyhow!("invalid local credential-store key"))?;
        cipher
            .decrypt(
                (&envelope.nonce).into(),
                Payload {
                    msg: &envelope.ciphertext,
                    aad: &self.aad,
                },
            )
            .map_err(|_| anyhow!("local credential authentication failed"))
    }

    fn delete_credential(&self) -> Result<()> {
        fs::remove_file(&self.path)
            .with_context(|| format!("could not delete credential {}", self.path.display()))
    }
}

#[cfg(target_os = "linux")]
fn load_or_create_file_key(store_dir: &Path) -> Result<[u8; 32]> {
    use std::os::unix::fs::OpenOptionsExt;

    ensure_private_directory(store_dir)?;
    ensure_private_directory(&store_dir.join(FILE_STORE_DIRECTORY))?;
    let path = store_dir.join(FILE_STORE_KEY);
    if path.exists() {
        return load_file_key(store_dir);
    }

    let key: [u8; 32] = rand::random();
    let temp = store_dir.join(format!(
        ".credential-key-{:032x}.tmp",
        rand::random::<u128>()
    ));
    let mut file = fs::OpenOptions::new()
        .write(true)
        .create_new(true)
        .mode(0o600)
        .open(&temp)
        .with_context(|| format!("could not create {}", temp.display()))?;
    file.write_all(&key)?;
    file.sync_all()?;
    drop(file);

    let result = match fs::hard_link(&temp, &path) {
        Ok(()) => Ok(key),
        Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => load_file_key(store_dir),
        Err(error) => Err(error).with_context(|| format!("could not create {}", path.display())),
    };
    let _ = fs::remove_file(temp);
    result
}

#[cfg(target_os = "linux")]
fn load_file_key(store_dir: &Path) -> Result<[u8; 32]> {
    let path = store_dir.join(FILE_STORE_KEY);
    check_private_permissions(&path)?;
    let bytes = fs::read(&path).with_context(|| format!("could not read {}", path.display()))?;
    bytes
        .try_into()
        .map_err(|_| anyhow!("local credential-store key has an invalid length"))
}

#[cfg(target_os = "linux")]
fn ensure_private_directory(path: &Path) -> Result<()> {
    use std::os::unix::fs::PermissionsExt;
    fs::create_dir_all(path).with_context(|| format!("could not create {}", path.display()))?;
    fs::set_permissions(path, fs::Permissions::from_mode(0o700))
        .with_context(|| format!("could not secure {}", path.display()))
}

#[cfg(target_os = "linux")]
fn check_private_permissions(path: &Path) -> Result<()> {
    use std::os::unix::fs::PermissionsExt;
    let mode = fs::metadata(path)
        .with_context(|| format!("could not inspect {}", path.display()))?
        .permissions()
        .mode();
    if mode & 0o077 != 0 {
        bail!(
            "credential file {} is not private; run `chmod 600 {}`",
            path.display(),
            path.display()
        );
    }
    Ok(())
}

#[cfg(target_os = "linux")]
fn write_private_replace(path: &Path, bytes: &[u8]) -> Result<()> {
    use std::os::unix::fs::OpenOptionsExt;

    let parent = path
        .parent()
        .ok_or_else(|| anyhow!("credential path has no parent"))?;
    ensure_private_directory(parent)?;
    let temp = parent.join(format!(".credential-{:032x}.tmp", rand::random::<u128>()));
    let result: Result<()> = (|| -> Result<()> {
        let mut file = fs::OpenOptions::new()
            .write(true)
            .create_new(true)
            .mode(0o600)
            .open(&temp)?;
        file.write_all(bytes)?;
        file.sync_all()?;
        fs::rename(&temp, path)?;
        Ok(())
    })();
    if result.is_err() {
        let _ = fs::remove_file(&temp);
    }
    result.with_context(|| format!("could not write credential {}", path.display()))
}

#[cfg(target_os = "linux")]
fn hex_digest(bytes: &[u8]) -> String {
    Sha256::digest(bytes)
        .iter()
        .map(|byte| format!("{byte:02x}"))
        .collect()
}

#[cfg(all(test, target_os = "linux"))]
mod tests {
    use std::os::unix::fs::PermissionsExt;

    use super::{FileEntry, entry};

    #[test]
    fn headless_linux_store_round_trips_a_secret() {
        let root = std::env::temp_dir().join(format!("shex-store-{:032x}", rand::random::<u128>()));
        let user = format!("test-{:016x}", rand::random::<u64>());
        let entry = entry(&root, "shex-test", &user).expect("credential store should initialize");
        entry
            .set_secret(b"not-a-real-credential")
            .expect("secret should be stored");
        assert_eq!(
            entry.get_secret().expect("secret should be retrieved"),
            b"not-a-real-credential"
        );
        entry
            .delete_credential()
            .expect("test credential should be deleted");
        let _ = std::fs::remove_dir_all(root);
    }

    #[test]
    fn encrypted_file_store_never_writes_plaintext() {
        let root = std::env::temp_dir().join(format!("shex-vault-{:032x}", rand::random::<u128>()));
        let secret = b"redis://user:password@example.invalid/";
        let entry = FileEntry::new(&root, "shex-test", "local");
        entry.set_secret(secret).unwrap();

        let bytes = std::fs::read(&entry.path).unwrap();
        assert!(!bytes.windows(secret.len()).any(|part| part == secret));
        assert_eq!(
            std::fs::metadata(&entry.path).unwrap().permissions().mode() & 0o777,
            0o600
        );
        assert_eq!(entry.get_secret().unwrap(), secret);

        let _ = std::fs::remove_dir_all(root);
    }
}