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
/*
    Copyright Michael Lodder. All Rights Reserved.
    SPDX-License-Identifier: Apache-2.0
*/
//! Store and fetch secrets in secure keyrings behind one small API.
//!
//! Every backend implements [`KeyRing`] (or [`DynKeyRing`] for trait
//! objects): `get_secret`, `set_secret` and `delete_secret` by id. The
//! backends, each behind a cargo feature:
//!
//! - OS keyrings: macOS Keychain (`macos-keychain`), Windows Credential
//!   Manager (`windows-credentials`) and the Linux Secret Service
//!   (`linux-secret-service`). These are the default features.
//! - File keyrings: `sqlcipher` (`file`), `encrypted_vfs`
//!   (`encrypted-vfs`), `turso` (`turso`) and `turso_encrypted_io`
//!   (`turso-encrypted-io`), opened with a connection string such as
//!   `password=... salt=...`; see `file_common::ConnectionParams`.
//! - In-memory and browser keyrings: `memory`, `web-storage`, `indexeddb`,
//!   `turso-opfs` and the `js` bindings.
//! - Hardware: YubiHSM 2 (`yubihsm-usb`, `yubihsm-http`) and Nitrokey 3 or
//!   any FIDO2 key with `hmac-secret` (`nitrokey`).
//!
//! The `fips` feature limits every keyring to FIPS-approved algorithms
//! (AES-256-GCM and PBKDF2-HMAC-SHA256). `migrate` reads 2.x encrypted VFS
//! and Turso encrypted IO files.
//!
//! ```no_run
//! # #[cfg(feature = "encrypted-vfs")]
//! # {
//! use cryptex::KeyRing;
//! use cryptex::encrypted_vfs::EncryptedVfsKeyring;
//!
//! let params = "password='correct horse'".parse().unwrap();
//! let mut keyring = EncryptedVfsKeyring::with_params(&params, None).unwrap();
//! keyring.set_secret("aws", b"AKIA...").unwrap();
//! assert_eq!(keyring.get_secret("aws").unwrap().as_slice(), b"AKIA...");
//! # }
//! ```
#![cfg_attr(docsrs, feature(doc_cfg))]
#![deny(
    missing_docs,
    warnings,
    unused_import_braces,
    unused_qualifications,
    trivial_casts,
    trivial_numeric_casts
)]

/// The error type every keyring returns.
pub mod error;
mod keyring;

#[cfg(feature = "encrypted-vfs")]
#[cfg_attr(docsrs, doc(cfg(feature = "encrypted-vfs")))]
pub use keyring::encrypted_vfs;
#[cfg(any(
    feature = "file",
    feature = "encrypted-vfs",
    feature = "memory",
    feature = "turso",
    feature = "turso-encrypted-io"
))]
#[cfg_attr(
    docsrs,
    doc(cfg(any(
        feature = "file",
        feature = "encrypted-vfs",
        feature = "turso",
        feature = "turso-encrypted-io"
    )))
)]
pub use keyring::file_common;
#[cfg(feature = "memory")]
#[cfg_attr(docsrs, doc(cfg(feature = "memory")))]
#[cfg(feature = "indexeddb")]
pub use keyring::indexeddb;
#[cfg(any(
    feature = "yubihsm-usb",
    feature = "yubihsm-http",
    feature = "nitrokey"
))]
#[cfg_attr(
    docsrs,
    doc(cfg(any(
        feature = "yubihsm-usb",
        feature = "yubihsm-http",
        feature = "nitrokey"
    )))
)]
pub use keyring::kms;
#[cfg(all(target_os = "linux", feature = "linux-secret-service"))]
#[cfg_attr(
    docsrs,
    doc(cfg(all(target_os = "linux", feature = "linux-secret-service")))
)]
pub use keyring::linux;
#[cfg(all(target_os = "macos", feature = "macos-keychain"))]
#[cfg_attr(docsrs, doc(cfg(all(target_os = "macos", feature = "macos-keychain"))))]
pub use keyring::macos;
#[cfg(feature = "memory")]
pub use keyring::memory;
#[cfg(all(feature = "encrypted-vfs", not(feature = "fips")))]
#[cfg_attr(
    docsrs,
    doc(cfg(all(feature = "encrypted-vfs", not(feature = "fips"))))
)]
pub use keyring::migrate;
#[cfg(feature = "nitrokey")]
#[cfg_attr(docsrs, doc(cfg(feature = "nitrokey")))]
pub use keyring::nitrokey;
#[cfg(feature = "web-storage")]
pub use keyring::web_storage;

#[cfg(feature = "js")]
mod wasm;
#[cfg(feature = "file")]
#[cfg_attr(docsrs, doc(cfg(feature = "file")))]
pub use keyring::sqlcipher;
#[cfg(feature = "turso")]
#[cfg_attr(docsrs, doc(cfg(feature = "turso")))]
pub use keyring::turso;
#[cfg(feature = "turso-encrypted-io")]
#[cfg_attr(docsrs, doc(cfg(feature = "turso-encrypted-io")))]
pub use keyring::turso_encrypted_io;
#[cfg(all(target_os = "windows", feature = "windows-credentials"))]
#[cfg_attr(
    docsrs,
    doc(cfg(all(target_os = "windows", feature = "windows-credentials")))
)]
pub use keyring::windows;
#[cfg(any(feature = "yubihsm-usb", feature = "yubihsm-http"))]
#[cfg_attr(
    docsrs,
    doc(cfg(any(feature = "yubihsm-usb", feature = "yubihsm-http")))
)]
pub use keyring::yubihsm;
pub use keyring::*;
#[cfg(feature = "js")]
#[cfg_attr(docsrs, doc(cfg(feature = "js")))]
pub use wasm::{Keyring, request_persistent_storage, stored_keyrings};

#[cfg(any(
    all(target_os = "macos", feature = "macos-keychain"),
    all(target_os = "linux", feature = "linux-secret-service"),
))]
#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn parse_peek_criteria_test() {
        for pair in &[
            ("", 0),
            ("kind=generic", 1),
            ("kind=internet,account=aws", 2),
            ("account=aws,service=lox", 2),
            // Malformed input must be skipped, not panic (no '=' delimiter).
            ("no_delimiter_here", 0),
            ("valid=1,broken", 1),
            ("kind=internet,,account=aws", 2),
        ] {
            let criteria = parse_peek_criteria(pair.0);
            assert_eq!(criteria.len(), pair.1, "input: {:?}", pair.0);
        }
    }
}