cryptex 3.0.0

Store and retrieve secrets in OS keyrings, encrypted SQLite or Turso files, in-memory and browser keyrings, or under a YubiHSM 2 or Nitrokey 3
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
    Copyright Michael Lodder. All Rights Reserved.
    SPDX-License-Identifier: Apache-2.0
*/
//! Origin Private File System storage for a Turso keyring.
//!
//! OPFS is a real filesystem owned by the origin. Its synchronous access
//! handles are the only way to read and write files without awaiting every
//! call, and browsers only hand them out inside a worker — which is exactly
//! where a keyring belongs, since deriving a key with Argon2 would otherwise
//! freeze the page.
//!
//! ```no_run
//! # use cryptex::turso::TursoKeyring;
//! # use cryptex::KeyRing;
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let params = "password=mysecret salt=0okm9ijn8uhb7ygv".parse()?;
//! let mut keyring = TursoKeyring::open_opfs(&params, "settings").await?;
//! keyring.set_secret("api-token", b"secret")?;
//! # Ok(())
//! # }
//! ```
//!
//! Files live under `cryptex/` in the origin's private filesystem, as
//! `<name>.db3` plus the `<name>.db3-wal` Turso keeps next to it. Holding the
//! sync access handle is itself exclusive: a second tab or worker that tries
//! to open the same keyring gets an error from the browser instead of
//! corrupting the file, so no separate lock file is needed.
//!
//! Like every browser store, OPFS is readable by any script on the origin, so
//! the pages stay encrypted and the key never leaves the password.

use super::page_store::PageStore;
use super::web_time::WebClock;
use crate::Result;
use crate::error::KeyRingError;
use ::turso::core::io::FileId;
use ::turso::core::{
    Clock, File as TursoFileIo, LimboError, MonotonicInstant, WallClockInstant, io_error,
};
use std::cell::{Cell, RefCell};
use std::collections::HashMap;
use std::io;
use std::sync::{Arc, Mutex, MutexGuard};
use wasm_bindgen::JsCast;
use wasm_bindgen_futures::JsFuture;
use web_sys::{
    FileSystemDirectoryHandle, FileSystemGetDirectoryOptions, FileSystemGetFileOptions,
    FileSystemReadWriteOptions, FileSystemSyncAccessHandle,
};

/// Directory inside the origin's private filesystem.
const DIRECTORY: &str = "cryptex";

thread_local! {
    /// Handles are `!Send`, so they live here and the store keeps only an id.
    /// Wasm runs one thread, so a thread-local is the whole registry.
    static HANDLES: RefCell<HashMap<u32, FileSystemSyncAccessHandle>> =
        RefCell::new(HashMap::new());
    static NEXT_ID: Cell<u32> = const { Cell::new(1) };
}

/// A [`PageStore`] over one OPFS file's synchronous access handle.
pub(crate) struct OpfsPages {
    id: u32,
}

impl OpfsPages {
    /// Open (creating if needed) `name` inside the cryptex directory.
    async fn open(name: &str) -> Result<Self> {
        let directory = open_directory().await?;
        let options = FileSystemGetFileOptions::new();
        options.set_create(true);
        let handle: web_sys::FileSystemFileHandle =
            JsFuture::from(directory.get_file_handle_with_options(name, &options))
                .await
                .map_err(js_error)?
                .dyn_into()
                .map_err(js_error)?;

        let access: FileSystemSyncAccessHandle =
            JsFuture::from(handle.create_sync_access_handle())
                .await
                .map_err(|e| {
                    KeyRingError::AccessDenied {
                        msg: format!(
                            "unable to take exclusive access to {name} in OPFS (is another tab or worker using it?): {}",
                            js_message(e)
                        ),
                    }
                })?
                .dyn_into()
                .map_err(js_error)?;

        let id = NEXT_ID.with(|next| {
            let id = next.get();
            next.set(id.wrapping_add(1));
            id
        });
        HANDLES.with(|handles| handles.borrow_mut().insert(id, access));
        Ok(Self { id })
    }

    /// Release the handle, closing the file.
    pub(crate) fn close(&self) {
        if let Some(handle) = HANDLES.with(|handles| handles.borrow_mut().remove(&self.id)) {
            handle.close();
        }
    }

    fn with_handle<T>(
        &self,
        op: &str,
        f: impl FnOnce(&FileSystemSyncAccessHandle) -> std::result::Result<T, wasm_bindgen::JsValue>,
    ) -> io::Result<T> {
        HANDLES.with(|handles| {
            let handles = handles.borrow();
            let handle = handles.get(&self.id).ok_or_else(|| {
                io::Error::other(format!("the OPFS handle is closed, cannot {op}"))
            })?;
            f(handle).map_err(|e| io::Error::other(js_message(e)))
        })
    }
}

impl PageStore for OpfsPages {
    fn len(&self) -> io::Result<u64> {
        self.with_handle("size the file", |handle| handle.get_size())
            .map(|size| size as u64)
    }

    fn read_exact_at(&mut self, offset: u64, buf: &mut [u8]) -> io::Result<()> {
        let mut filled = 0;
        while filled < buf.len() {
            let options = FileSystemReadWriteOptions::new();
            options.set_at((offset as usize + filled) as f64);
            let read = self.with_handle("read", |handle| {
                handle.read_with_u8_array_and_options(&mut buf[filled..], &options)
            })? as usize;
            if read == 0 {
                // Past the end of the file: zero-fill, like a sparse file.
                buf[filled..].fill(0);
                return Ok(());
            }
            filled += read;
        }
        Ok(())
    }

    fn write_all_at(&mut self, offset: u64, buf: &[u8]) -> io::Result<()> {
        let mut written = 0;
        while written < buf.len() {
            let options = FileSystemReadWriteOptions::new();
            options.set_at((offset as usize + written) as f64);
            let n = self.with_handle("write", |handle| {
                handle.write_with_u8_array_and_options(&buf[written..], &options)
            })? as usize;
            if n == 0 {
                return Err(io::Error::other("OPFS accepted no bytes"));
            }
            written += n;
        }
        Ok(())
    }

    fn set_len(&mut self, len: u64) -> io::Result<()> {
        self.with_handle("resize the file", |handle| {
            handle.truncate_with_f64(len as f64)
        })
    }

    fn sync(&mut self) -> io::Result<()> {
        self.with_handle("flush the file", |handle| handle.flush())
    }
}

/// The Turso [`IO`](::turso::core::IO) backed by OPFS.
///
/// Both files Turso needs are opened up front, because a sync access handle is
/// obtained asynchronously: `open_file` itself cannot await.
pub(crate) struct OpfsIo {
    files: HashMap<String, Arc<OpfsFile>>,
    /// Distinguishes this IO in Turso's process-wide database registry, which
    /// is keyed by `file_id`; see `WebIo::instance`.
    instance: String,
}

impl OpfsIo {
    /// Open `<name>.db3` and its WAL under `cryptex/`.
    pub(crate) async fn open(name: &str) -> Result<Self> {
        let db = format!("{name}.db3");
        let wal = format!("{db}-wal");
        let mut files = HashMap::new();
        for path in [db, wal] {
            let pages = OpfsPages::open(&path).await?;
            files.insert(path, Arc::new(OpfsFile::new(pages)));
        }
        Ok(Self {
            files,
            instance: super::turso_web::new_instance(),
        })
    }

    /// Flush both files to disk and release their handles.
    pub(crate) fn close(&mut self) {
        for file in self.files.values() {
            file.close();
        }
        self.files.clear();
    }
}

impl Drop for OpfsIo {
    fn drop(&mut self) {
        self.close();
    }
}

impl Clock for OpfsIo {
    fn current_time_monotonic(&self) -> MonotonicInstant {
        WebClock.current_time_monotonic()
    }

    fn current_time_wall_clock(&self) -> WallClockInstant {
        WebClock.current_time_wall_clock()
    }
}

impl ::turso::core::IO for OpfsIo {
    fn open_file(
        &self,
        path: &str,
        _flags: ::turso::core::OpenFlags,
        _direct: bool,
    ) -> std::result::Result<Arc<dyn TursoFileIo>, LimboError> {
        // Only the database and its WAL are ever opened: the handle for each
        // had to exist before Turso asked, so an unknown path is a bug in the
        // caller's configuration, not a file to create.
        match self.files.get(path) {
            Some(file) => {
                let file: Arc<OpfsFile> = Arc::clone(file);
                Ok(file)
            }
            None => Err(LimboError::InternalError(format!(
                "the OPFS keyring did not prepare '{path}'; it opens only the database and its WAL"
            ))),
        }
    }

    fn remove_file(&self, _path: &str) -> std::result::Result<(), LimboError> {
        Err(LimboError::InternalError(
            "an OPFS keyring is removed through the file system, not Turso".to_string(),
        ))
    }

    fn is_memory(&self) -> bool {
        false
    }

    fn file_id(&self, path: &str) -> std::result::Result<FileId, LimboError> {
        Ok(FileId::from_path_hash(&format!(
            "{}/{}",
            self.instance, path
        )))
    }
}

/// The Turso file view of one prepared OPFS file.
struct OpfsFile {
    store: Mutex<OpfsPages>,
}

impl OpfsFile {
    fn new(pages: OpfsPages) -> Self {
        Self {
            store: Mutex::new(pages),
        }
    }

    /// Flush and close the handle. The store stays usable but fails every call.
    fn close(&self) {
        if let Ok(mut store) = self.store.lock() {
            let _ = store.sync();
            store.close();
        }
    }

    fn store(&self) -> std::result::Result<MutexGuard<'_, OpfsPages>, LimboError> {
        self.store
            .lock()
            .map_err(|_| LimboError::InternalError("OPFS file lock poisoned".to_string()))
    }
}

fn to_i32(n: usize) -> std::result::Result<i32, LimboError> {
    i32::try_from(n).map_err(|_| LimboError::InternalError("I/O length overflows i32".into()))
}

impl TursoFileIo for OpfsFile {
    // The sync access handle is already exclusive.
    fn lock_file(&self, _exclusive: bool) -> std::result::Result<(), LimboError> {
        Ok(())
    }

    fn unlock_file(&self) -> std::result::Result<(), LimboError> {
        Ok(())
    }

    fn pread(
        &self,
        pos: u64,
        c: ::turso::core::Completion,
    ) -> std::result::Result<::turso::core::Completion, LimboError> {
        let mut store = self.store()?;
        let size = store.len().map_err(|e| io_error(e, "pread"))?;
        let buf = c.as_read().buf().as_mut_slice();
        let n = (buf.len() as u64).min(size.saturating_sub(pos)) as usize;
        store
            .read_exact_at(pos, &mut buf[..n])
            .map_err(|e| io_error(e, "pread"))?;
        c.complete(to_i32(n)?);
        Ok(c)
    }

    fn pwrite(
        &self,
        pos: u64,
        buffer: Arc<::turso::core::Buffer>,
        c: ::turso::core::Completion,
    ) -> std::result::Result<::turso::core::Completion, LimboError> {
        let mut store = self.store()?;
        store
            .write_all_at(pos, buffer.as_slice())
            .map_err(|e| io_error(e, "pwrite"))?;
        c.complete(to_i32(buffer.as_slice().len())?);
        Ok(c)
    }

    fn sync(
        &self,
        c: ::turso::core::Completion,
        _sync_type: ::turso::core::io::FileSyncType,
    ) -> std::result::Result<::turso::core::Completion, LimboError> {
        self.store()?.sync().map_err(|e| io_error(e, "sync"))?;
        c.complete(0);
        Ok(c)
    }

    fn size(&self) -> std::result::Result<u64, LimboError> {
        self.store()?.len().map_err(|e| io_error(e, "size"))
    }

    fn truncate(
        &self,
        len: u64,
        c: ::turso::core::Completion,
    ) -> std::result::Result<::turso::core::Completion, LimboError> {
        self.store()?
            .set_len(len)
            .map_err(|e| io_error(e, "truncate"))?;
        c.complete(0);
        Ok(c)
    }
}

async fn open_directory() -> Result<FileSystemDirectoryHandle> {
    let navigator = js_sys::Reflect::get(
        &js_sys::global(),
        &wasm_bindgen::JsValue::from_str("navigator"),
    )
    .map_err(js_error)?;
    let storage = js_sys::Reflect::get(&navigator, &wasm_bindgen::JsValue::from_str("storage"))
        .map_err(js_error)?;
    if storage.is_undefined() {
        return Err(unsupported(
            "this browser has no Storage Manager API, so OPFS is unavailable",
        ));
    }
    let get_directory =
        js_sys::Reflect::get(&storage, &wasm_bindgen::JsValue::from_str("getDirectory"))
            .map_err(js_error)?
            .dyn_into::<js_sys::Function>()
            .map_err(js_error)?;
    let root: FileSystemDirectoryHandle = JsFuture::from(
        get_directory
            .call0(&storage)
            .map_err(js_error)?
            .dyn_into::<js_sys::Promise>()
            .map_err(js_error)?,
    )
    .await
    .map_err(js_error)?
    .dyn_into()
    .map_err(js_error)?;

    let options = FileSystemGetDirectoryOptions::new();
    options.set_create(true);
    let directory = JsFuture::from(root.get_directory_handle_with_options(DIRECTORY, &options))
        .await
        .map_err(js_error)?
        .dyn_into()
        .map_err(js_error)?;
    Ok(directory)
}

/// Names of every keyring stored under `cryptex/` in the origin's OPFS.
///
/// Reads the directory listing only, so it works from a page even though
/// opening a keyring needs a worker (sync access handles are worker-only).
pub async fn list_names() -> Result<Vec<String>> {
    use wasm_bindgen::JsValue;

    let directory = open_directory().await?;
    let keys: js_sys::Function = js_sys::Reflect::get(&directory, &JsValue::from_str("keys"))
        .map_err(js_error)?
        .dyn_into()
        .map_err(|_| unsupported("this browser cannot list OPFS files"))?;
    let iterator = keys.call0(&directory).map_err(js_error)?;
    let next: js_sys::Function = js_sys::Reflect::get(&iterator, &JsValue::from_str("next"))
        .map_err(js_error)?
        .dyn_into()
        .map_err(js_error)?;

    let mut names = Vec::new();
    loop {
        // `keys()` is an async iterator: each step is a promise for
        // `{ value, done }`.
        let step = JsFuture::from(
            next.call0(&iterator)
                .map_err(js_error)?
                .dyn_into::<js_sys::Promise>()
                .map_err(js_error)?,
        )
        .await
        .map_err(js_error)?;
        let done = js_sys::Reflect::get(&step, &JsValue::from_str("done"))
            .map_err(js_error)?
            .as_bool()
            .unwrap_or(true);
        if done {
            break;
        }
        let value = js_sys::Reflect::get(&step, &JsValue::from_str("value")).map_err(js_error)?;
        // Every keyring is `<name>.db3`; the WAL next to it is not a keyring.
        if let Some(name) = value
            .as_string()
            .and_then(|name| name.strip_suffix(".db3").map(str::to_string))
        {
            names.push(name);
        }
    }
    names.sort();
    Ok(names)
}

fn js_error(value: wasm_bindgen::JsValue) -> KeyRingError {
    KeyRingError::from(value)
}

fn js_message(value: wasm_bindgen::JsValue) -> String {
    KeyRingError::from(value).as_str()
}

fn unsupported(msg: &str) -> KeyRingError {
    KeyRingError::GeneralError {
        msg: msg.to_string(),
    }
}