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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/*
    Copyright Michael Lodder. All Rights Reserved.
    SPDX-License-Identifier: Apache-2.0
*/
//! JavaScript bindings, for use from a browser with `wasm-bindgen`.
//!
//! ```js
//! import init, { Keyring } from "./pkg/cryptex.js";
//! await init();
//!
//! const keyring = await Keyring.open(
//!   "password=mysecret salt=0okm9ijn8uhb7ygv",
//!   "settings",
//!   { backend: "turso", store: "indexeddb" },
//! );
//! await keyring.set("api-token", new TextEncoder().encode("secret"));
//! const token = keyring.get("api-token");
//! await keyring.close();
//! ```
//!
//! Backends:
//!
//! | `backend` | what it is |
//! | --- | --- |
//! | `turso` (default) | a Turso database, encrypted with the key from the connection string |
//! | `memory` | an encrypted image in memory, no database |
//!
//! Stores, in order of preference:
//!
//! | `store` | where the image lives | works in |
//! | --- | --- | --- |
//! | `indexeddb` (default) | IndexedDB, one image per keyring, guarded by a Web Lock | page and worker |
//! | `localstorage` | `localStorage`, one string per keyring | page only |
//! | `opfs` | origin private file system, one file per keyring | worker only, `backend: "turso"` only |
//! | `memory` | nowhere: the keyring is gone when the page unloads | page and worker |
//!
//! Every store keeps the image encrypted; the key comes from the password (or
//! raw key) in the connection string. What a store protects against is other
//! origins and other users, not scripts running on the same origin: any script
//! there can read IndexedDB, OPFS and `localStorage`.
//!
//! Deriving the key runs Argon2, which blocks the thread it runs on. Open the
//! keyring in a worker (or with default parameters) to keep the page
//! responsive.

use js_sys::Reflect;
use wasm_bindgen::prelude::*;
use zeroize::Zeroizing;

use crate::DynKeyRing;
use crate::error::KeyRingError;
use crate::indexeddb::LockHold;
use crate::indexeddb::SnapshotStore;
use crate::memory::MemoryKeyring;
use crate::turso::TursoKeyring;
use crate::web_storage::{self, LocalStorageImage};

/// A keyring, opened from JavaScript.
#[wasm_bindgen]
pub struct Keyring {
    inner: Inner,
}

enum Inner {
    /// A Turso keyring plus the store its image is written to.
    Turso {
        /// `None` once the keyring is closed: dropping it releases the
        /// database, and with it an OPFS handle or an IndexedDB lock.
        keyring: Option<TursoKeyring>,
        store: Store,
        dirty: bool,
    },
    Memory {
        /// `None` once the keyring is closed.
        keyring: Option<MemoryKeyring>,
        store: Store,
        dirty: bool,
    },
}

enum Store {
    /// The keyring persists itself, or is not persisted at all.
    None,
    /// The database is an OPFS file; it holds its own data.
    Opfs,
    IndexedDb(SnapshotStore),
    /// An image in `localStorage`, with the lock that keeps two tabs from
    /// overwriting each other's image.
    LocalStorage(LocalStorageImage, LockHold),
}

#[wasm_bindgen]
impl Keyring {
    /// Open the keyring called `name`, creating it on first use.
    ///
    /// `connection` is a connection string such as
    /// `password=mysecret salt=0okm9ijn8uhb7ygv`. `options` may name a
    /// `backend` and a `store`; both default as shown above. A stored keyring
    /// is refused with an `AccessDenied` error when the key is wrong.
    #[wasm_bindgen(js_name = "open")]
    pub async fn open(connection: &str, name: &str, options: JsValue) -> Result<Keyring, JsError> {
        let options = Options::parse(options)?;
        let name = if name.is_empty() { "default" } else { name };
        match options.backend {
            Backend::Turso => open_turso(connection, name, options.store).await,
            Backend::Memory => open_memory(connection, name, options.store).await,
        }
    }

    /// Reopen a keyring from an image returned by [`export`](Self::export).
    ///
    /// Only `backend` is consulted: the image is used as given, so nothing is
    /// read from storage and nothing is written until the first change.
    #[wasm_bindgen(js_name = "import")]
    pub async fn import(
        connection: &str,
        image: &[u8],
        options: JsValue,
    ) -> Result<Keyring, JsError> {
        let options = Options::parse(options)?;
        match options.backend {
            Backend::Turso => {
                let keyring =
                    TursoKeyring::from_bytes(image, &turso_params(connection)?).map_err(js)?;
                Ok(Keyring {
                    inner: Inner::Turso {
                        keyring: Some(keyring),
                        store: Store::None,
                        dirty: false,
                    },
                })
            }
            Backend::Memory => {
                let keyring = MemoryKeyring::import(image, memory_secret(connection)?.as_slice())
                    .map_err(js)?;
                Ok(Keyring {
                    inner: Inner::Memory {
                        keyring: Some(keyring),
                        store: Store::None,
                        dirty: false,
                    },
                })
            }
        }
    }

    /// Read a secret. Throws `ItemNotFound` when there is none.
    pub fn get(&mut self, id: &str) -> Result<Vec<u8>, JsError> {
        let secret = match &mut self.inner {
            Inner::Turso { keyring, .. } => keyring.as_mut().ok_or_else(closed)?.get_secret(id),
            Inner::Memory { keyring, .. } => keyring.as_mut().ok_or_else(closed)?.get_secret(id),
        }
        .map_err(js)?;
        let value: Zeroizing<Vec<u8>> = Zeroizing::new(secret.0.clone());
        Ok(value.to_vec())
    }

    /// Store a secret, replacing any previous value.
    ///
    /// Resolves once the change has reached storage, so a resolved promise
    /// means the secret survives a reload.
    pub async fn set(&mut self, id: &str, secret: &[u8]) -> Result<(), JsError> {
        match &mut self.inner {
            Inner::Turso {
                keyring,
                store,
                dirty,
            } => {
                let keyring = keyring.as_mut().ok_or_else(closed)?;
                keyring.set_secret(id, secret).map_err(js)?;
                *dirty = true;
                flush(store, dirty, || keyring.export()).await
            }
            Inner::Memory {
                keyring,
                store,
                dirty,
            } => {
                let keyring = keyring.as_mut().ok_or_else(closed)?;
                keyring.set_secret(id, secret).map_err(js)?;
                *dirty = true;
                flush(store, dirty, || keyring.export()).await
            }
        }
    }

    /// Remove a secret. Resolves once storage has been updated.
    pub async fn delete(&mut self, id: &str) -> Result<(), JsError> {
        match &mut self.inner {
            Inner::Turso {
                keyring,
                store,
                dirty,
            } => {
                let keyring = keyring.as_mut().ok_or_else(closed)?;
                keyring.delete_secret(id).map_err(js)?;
                *dirty = true;
                flush(store, dirty, || keyring.export()).await
            }
            Inner::Memory {
                keyring,
                store,
                dirty,
            } => {
                let keyring = keyring.as_mut().ok_or_else(closed)?;
                keyring.delete_secret(id).map_err(js)?;
                *dirty = true;
                flush(store, dirty, || keyring.export()).await
            }
        }
    }

    /// Re-encrypt under a new connection string and store the result.
    ///
    /// Nothing changes if the new key cannot be applied. An OPFS keyring
    /// cannot be rekeyed in place: create a keyring with a new name and copy
    /// the secrets into it.
    pub async fn rekey(&mut self, connection: &str) -> Result<(), JsError> {
        match &mut self.inner {
            Inner::Turso {
                keyring,
                store,
                dirty,
            } => {
                if matches!(store, Store::Opfs) {
                    return Err(JsError::new(
                        "GeneralError: an OPFS keyring cannot be rekeyed in place; \
                         open a keyring with a new name and copy the secrets into it",
                    ));
                }
                let params = turso_params(connection)?;
                let keyring = keyring.as_mut().ok_or_else(closed)?;
                // Build the replacement first: if it fails, the keyring the
                // caller holds is untouched.
                let rekeyed = keyring.copy_under(&params).map_err(js)?;
                *keyring = rekeyed;
                *dirty = true;
                flush(store, dirty, || keyring.export()).await
            }
            Inner::Memory {
                keyring,
                store,
                dirty,
            } => {
                let keyring = keyring.as_mut().ok_or_else(closed)?;
                keyring.rekey(&memory_params(connection)?).map_err(js)?;
                *dirty = true;
                flush(store, dirty, || keyring.export()).await
            }
        }
    }

    /// Every id in this keyring, in order.
    pub fn keys(&self) -> Result<Vec<String>, JsError> {
        match &self.inner {
            Inner::Turso { keyring, .. } => keyring.as_ref().ok_or_else(closed)?.keys().map_err(js),
            Inner::Memory { keyring, .. } => Ok(keyring.as_ref().ok_or_else(closed)?.keys()),
        }
    }

    /// Copy the encrypted image out, e.g. to move it to another store.
    pub fn export(&self) -> Result<Vec<u8>, JsError> {
        match &self.inner {
            Inner::Turso { keyring, .. } => {
                keyring.as_ref().ok_or_else(closed)?.export().map_err(js)
            }
            Inner::Memory { keyring, .. } => {
                keyring.as_ref().ok_or_else(closed)?.export().map_err(js)
            }
        }
    }

    /// Flush any pending change and release the store.
    ///
    /// A keyring holding a Web Lock releases it here, letting another tab
    /// open the same name. Every other method then throws, so a caller cannot
    /// keep writing to a keyring whose changes have nowhere to go.
    pub async fn close(&mut self) -> Result<(), JsError> {
        match &mut self.inner {
            Inner::Turso {
                keyring,
                store,
                dirty,
            } => {
                if let Some(keyring) = keyring.as_ref() {
                    flush(store, dirty, || keyring.export()).await?;
                }
                *keyring = None;
                release(store).await;
            }
            Inner::Memory {
                keyring,
                store,
                dirty,
            } => {
                if let Some(keyring) = keyring.as_ref() {
                    flush(store, dirty, || keyring.export()).await?;
                }
                *keyring = None;
                release(store).await;
            }
        }
        Ok(())
    }
}

/// Ask the browser not to evict this origin's storage.
///
/// Browsers may grant it silently, prompt, or refuse. Call it while the user
/// is interacting with the page; without it a browser under storage pressure
/// can delete the keyring.
#[wasm_bindgen(js_name = "requestPersistentStorage")]
pub async fn request_persistent_storage() -> Result<bool, JsError> {
    web_storage::request_persistent_storage().await.map_err(js)
}

/// The keyrings this origin already has in one store, in name order.
///
/// `storedKeyrings({ store })` reads `indexeddb` (the default), `localstorage`
/// or `opfs` without opening anything, so a page can show what is stored
/// before the user picks a keyring. Nothing is listed for `memory`, which
/// keeps no store of its own.
#[wasm_bindgen(js_name = "storedKeyrings")]
pub async fn stored_keyrings(options: JsValue) -> Result<Vec<String>, JsError> {
    match Options::parse(options)?.store {
        StoreKind::IndexedDb => SnapshotStore::list_names().await.map_err(js),
        StoreKind::LocalStorage => web_storage::list_names().map_err(js),
        StoreKind::Opfs => crate::opfs::list_names().await.map_err(js),
        StoreKind::Memory => Ok(Vec::new()),
    }
}

async fn open_turso(connection: &str, name: &str, store: StoreKind) -> Result<Keyring, JsError> {
    let params = turso_params(connection)?;
    let (keyring, store) = match store {
        StoreKind::IndexedDb => {
            let snapshot = SnapshotStore::open(name).await.map_err(js)?;
            let keyring = match snapshot.load().await.map_err(js)? {
                Some(image) => TursoKeyring::from_bytes(&image, &params).map_err(js)?,
                None => TursoKeyring::in_memory(&params).map_err(js)?,
            };
            (keyring, Store::IndexedDb(snapshot))
        }
        StoreKind::LocalStorage => {
            let hold = LockHold::acquire(&format!("cryptex:{name}"))
                .await
                .map_err(js)?;
            let image = LocalStorageImage::new(Some(name))?;
            let keyring = match image.load()? {
                Some(bytes) => TursoKeyring::from_bytes(&bytes, &params).map_err(js)?,
                None => TursoKeyring::in_memory(&params).map_err(js)?,
            };
            (keyring, Store::LocalStorage(image, hold))
        }
        StoreKind::Opfs => {
            let keyring = TursoKeyring::open_opfs(&params, name).await.map_err(js)?;
            (keyring, Store::Opfs)
        }
        StoreKind::Memory => (TursoKeyring::in_memory(&params).map_err(js)?, Store::None),
    };
    Ok(Keyring {
        inner: Inner::Turso {
            keyring: Some(keyring),
            store,
            dirty: false,
        },
    })
}

async fn open_memory(connection: &str, name: &str, store: StoreKind) -> Result<Keyring, JsError> {
    let params = memory_params(connection)?;
    let secret = memory_secret(connection)?;
    let (keyring, store) = match store {
        StoreKind::IndexedDb => {
            let snapshot = SnapshotStore::open(name).await.map_err(js)?;
            let keyring = match snapshot.load().await.map_err(js)? {
                Some(image) => MemoryKeyring::import(&image, secret.as_slice()).map_err(js)?,
                None => MemoryKeyring::with_params(&params).map_err(js)?,
            };
            (keyring, Store::IndexedDb(snapshot))
        }
        StoreKind::LocalStorage => {
            let hold = LockHold::acquire(&format!("cryptex:{name}"))
                .await
                .map_err(js)?;
            let image = LocalStorageImage::new(Some(name))?;
            let keyring = match image.load()? {
                Some(bytes) => MemoryKeyring::import(&bytes, secret.as_slice()).map_err(js)?,
                None => MemoryKeyring::with_params(&params).map_err(js)?,
            };
            (keyring, Store::LocalStorage(image, hold))
        }
        StoreKind::Memory => (
            MemoryKeyring::with_params(&params).map_err(js)?,
            Store::None,
        ),
        // Refused when the options are parsed; OPFS stores database files.
        StoreKind::Opfs => {
            return Err(JsError::new(
                "TypeError: the opfs store needs backend: \"turso\"",
            ));
        }
    };
    Ok(Keyring {
        inner: Inner::Memory {
            keyring: Some(keyring),
            store,
            dirty: false,
        },
    })
}

/// Drop the store, releasing a Web Lock only once the browser has done so.
async fn release(store: &mut Store) {
    match store {
        Store::IndexedDb(snapshot) => snapshot.release().await,
        Store::LocalStorage(_, hold) => hold.release_async().await,
        Store::None | Store::Opfs => {}
    }
    *store = Store::None;
}

/// Write the keyring's current image to its store.
///
/// Nothing to do for a keyring that keeps its own data (OPFS) or has no store:
/// `image` is only called when a store actually needs a copy.
async fn flush(
    store: &Store,
    dirty: &mut bool,
    image: impl FnOnce() -> Result<Vec<u8>, KeyRingError>,
) -> Result<(), JsError> {
    if !*dirty {
        return Ok(());
    }
    match store {
        Store::None | Store::Opfs => {}
        Store::IndexedDb(snapshot) => snapshot.save(&image().map_err(js)?).await.map_err(js)?,
        Store::LocalStorage(local, _) => local.save(&image().map_err(js)?)?,
    }
    *dirty = false;
    Ok(())
}

#[derive(Clone, Copy)]
enum Backend {
    Turso,
    Memory,
}

#[derive(Clone, Copy)]
enum StoreKind {
    IndexedDb,
    LocalStorage,
    Opfs,
    Memory,
}

struct Options {
    backend: Backend,
    store: StoreKind,
}

impl Options {
    fn parse(options: JsValue) -> Result<Self, JsError> {
        let mut parsed = Self {
            backend: Backend::Turso,
            store: StoreKind::IndexedDb,
        };
        if options.is_undefined() || options.is_null() {
            return Ok(parsed);
        }
        if let Some(backend) = string_property(&options, "backend")? {
            parsed.backend = match backend.as_str() {
                "turso" => Backend::Turso,
                "memory" => Backend::Memory,
                other => return Err(unknown("backend", other)),
            };
        }
        if let Some(store) = string_property(&options, "store")? {
            parsed.store = match store.as_str() {
                "indexeddb" => StoreKind::IndexedDb,
                "localstorage" => StoreKind::LocalStorage,
                "opfs" => StoreKind::Opfs,
                "memory" => StoreKind::Memory,
                other => return Err(unknown("store", other)),
            };
        }
        // OPFS holds database files, so it needs a database backend.
        if matches!(parsed.store, StoreKind::Opfs) && matches!(parsed.backend, Backend::Memory) {
            return Err(JsError::new(
                "TypeError: the opfs store needs backend: \"turso\"",
            ));
        }
        Ok(parsed)
    }
}

fn string_property(value: &JsValue, name: &str) -> Result<Option<String>, JsError> {
    let property =
        Reflect::get(value, &JsValue::from_str(name)).map_err(|e| js(KeyRingError::from(e)))?;
    if property.is_undefined() || property.is_null() {
        return Ok(None);
    }
    property
        .as_string()
        .map(Some)
        .ok_or_else(|| JsError::new(&format!("TypeError: options.{name} must be a string")))
}

fn unknown(field: &str, value: &str) -> JsError {
    JsError::new(&format!(
        "TypeError: unknown {field} '{value}'; expected one of the documented values"
    ))
}

fn turso_params(connection: &str) -> Result<crate::turso::ConnectionParams, JsError> {
    connection.parse().map_err(js)
}

fn memory_params(connection: &str) -> Result<crate::memory::ConnectionParams, JsError> {
    connection.parse().map_err(js)
}

/// The password or raw key from a connection string, for a memory keyring.
/// The password or raw key, wrapped so its copy is wiped on the way out.
fn memory_secret(connection: &str) -> Result<Zeroizing<Vec<u8>>, JsError> {
    let params = memory_params(connection)?;
    Ok(Zeroizing::new(if params.key.is_empty() {
        params.password.clone()
    } else {
        params.key.clone()
    }))
}

fn closed() -> JsError {
    JsError::new("GeneralError: the keyring is closed")
}

fn js(error: KeyRingError) -> JsError {
    // Keep the kind as the first word so JavaScript can switch on it.
    let kind = match &error {
        KeyRingError::ItemNotFound => "ItemNotFound",
        KeyRingError::AccessDenied { .. } => "AccessDenied",
        KeyRingError::GeneralError { .. } => "GeneralError",
    };
    JsError::new(&format!("{kind}: {error}"))
}