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
*/
//! A keyring stored in a [Turso](https://github.com/tursodatabase/turso)
//! database whose file I/O passes through a custom encrypting [`IO`] backend
//! — the Turso counterpart of [`encrypted_vfs`](crate::encrypted_vfs).
//!
//! Every file Turso opens (the database and its WAL) is stored with the same
//! page layout as the encrypted VFS: `[ciphertext(4096) | nonce(24) | tag(16)]`,
//! and the database starts with the same keyring header.

use super::*;
use ::turso::Builder;
use ::turso::core::io::FileSyncType;
use ::turso::core::io::clock::DefaultClock;
use ::turso::core::{
    Buffer, Clock, Completion, File as TursoFileIo, IO, LimboError, MonotonicInstant, OpenFlags,
    WallClockInstant, io_error,
};
use std::fs::{self, File};
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, MutexGuard};

use super::file_common::{self, file_keyring, private_open_options};
use super::page_cipher::{self, ActiveCipher, PageFile};
use super::page_header::{HEADER_SIZE, open_or_create};
use super::page_store::PageStore;
use super::turso_common::TursoFile;

pub use super::cipher::PageCipher as CipherAlgorithm;

/// Connection parameters for [`TursoEncryptedIoKeyring`].
///
/// Accepted format:
/// ```text
/// password=mysecret [cipher=xchacha20poly1305|aes256gcm]
/// ```
///
/// The salt is generated and stored in the keyring file's header with the
/// cipher and costs, so reopening needs only the password (or `key=`).
pub type ConnectionParams = file_common::ConnectionParams<CipherAlgorithm>;

type TursoResult<T> = std::result::Result<T, LimboError>;

/// Turso IO backend that encrypts every page it writes and decrypts every
/// page it reads.
struct EncryptedIo {
    cipher: Arc<ActiveCipher>,
    /// The database file; only it starts with the keyring header.
    db_path: PathBuf,
}

impl IO for EncryptedIo {
    fn open_file(
        &self,
        path: &str,
        flags: OpenFlags,
        _direct: bool,
    ) -> TursoResult<Arc<dyn TursoFileIo>> {
        let mut opts = private_open_options();
        opts.read(true);
        if !flags.contains(OpenFlags::ReadOnly) {
            opts.write(true)
                .create(flags.contains(OpenFlags::Create))
                .truncate(false);
        }
        let file = opts.open(path).map_err(|e| io_error(e, "open"))?;
        let layout = if Path::new(path) == self.db_path {
            PageFile::main(HEADER_SIZE)
        } else if path.ends_with("-wal") {
            PageFile::JOURNAL
        } else {
            PageFile::TEMP
        };
        Ok(Arc::new(EncryptedFile {
            file: Mutex::new(file),
            layout,
            cipher: Arc::clone(&self.cipher),
        }))
    }

    fn remove_file(&self, path: &str) -> TursoResult<()> {
        fs::remove_file(path).map_err(|e| io_error(e, "remove_file"))
    }
}

impl Clock for EncryptedIo {
    fn current_time_monotonic(&self) -> MonotonicInstant {
        DefaultClock.current_time_monotonic()
    }

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

struct EncryptedFile {
    file: Mutex<File>,
    layout: PageFile,
    cipher: Arc<ActiveCipher>,
}

impl EncryptedFile {
    fn file(&self) -> TursoResult<MutexGuard<'_, File>> {
        self.file
            .lock()
            .map_err(|_| LimboError::InternalError("encrypted file lock poisoned".to_string()))
    }
}

fn to_usize(n: u64) -> TursoResult<usize> {
    usize::try_from(n).map_err(|_| LimboError::InternalError("offset overflows usize".into()))
}

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

impl TursoFileIo for EncryptedFile {
    // Exclusivity comes from the keyring's sidecar lock, as with the encrypted VFS.
    fn lock_file(&self, _exclusive: bool) -> TursoResult<()> {
        Ok(())
    }

    fn unlock_file(&self) -> TursoResult<()> {
        Ok(())
    }

    fn pread(&self, pos: u64, c: Completion) -> TursoResult<Completion> {
        let mut file = self.file()?;
        let available = page_cipher::logical_size(&*file, self.layout)
            .map_err(|e| io_error(e, "pread"))?
            .saturating_sub(pos);
        let read = {
            let buf = c.as_read().buf().as_mut_slice();
            let n = buf.len().min(to_usize(available)?);
            self.cipher
                .read_at(&mut *file, self.layout, to_usize(pos)?, &mut buf[..n])
                .map_err(|e| io_error(e, "pread"))?
        };
        c.complete(to_i32(read)?);
        Ok(c)
    }

    fn pwrite(&self, pos: u64, buffer: Arc<Buffer>, c: Completion) -> TursoResult<Completion> {
        let written = self
            .cipher
            .write_at(
                &mut *self.file()?,
                self.layout,
                to_usize(pos)?,
                buffer.as_slice(),
            )
            .map_err(|e| io_error(e, "pwrite"))?;
        c.complete(to_i32(written)?);
        Ok(c)
    }

    fn sync(&self, c: Completion, _sync_type: FileSyncType) -> TursoResult<Completion> {
        self.file()?.sync().map_err(|e| io_error(e, "sync"))?;
        c.complete(0);
        Ok(c)
    }

    fn size(&self) -> TursoResult<u64> {
        page_cipher::logical_size(&*self.file()?, self.layout).map_err(|e| io_error(e, "size"))
    }

    fn truncate(&self, len: u64, c: Completion) -> TursoResult<Completion> {
        page_cipher::truncate(&mut *self.file()?, self.layout, to_usize(len)?)
            .map_err(|e| io_error(e, "truncate"))?;
        c.complete(0);
        Ok(c)
    }
}

/// A keyring stored in a Turso database whose pages are encrypted by
/// cryptex's own XChaCha20-Poly1305 / AES-256-GCM IO layer rather than Turso's
/// built-in encryption. The default file is `~/.cryptex/keyring-turso.db3`.
pub struct TursoEncryptedIoKeyring {
    file: TursoFile,
}

file_keyring!(TursoEncryptedIoKeyring, file);

impl TursoEncryptedIoKeyring {
    /// Open (or create) the keyring in `path` (default `~/.cryptex`).
    pub fn with_params(connection: &ConnectionParams, path: Option<PathBuf>) -> Result<Self> {
        Ok(Self {
            file: TursoFile::open(path, builder(connection))?,
        })
    }

    /// Re-encrypt the keyring with a new key (and optionally a different cipher).
    ///
    /// Consumes the keyring and returns it reopened under `new_params`. If
    /// rekeying fails the original file is left intact and can be reopened
    /// with the old parameters.
    pub fn rekey(self, new_params: &ConnectionParams) -> Result<Self> {
        Ok(Self {
            file: self.file.rekey(builder(new_params))?,
        })
    }
}

/// A closure building a Turso [`Builder`] for a database path, routing all
/// file I/O through an [`EncryptedIo`]. The key comes from the header at the
/// start of that database, written first if the file is new.
fn builder(params: &ConnectionParams) -> impl Fn(&str) -> Result<Builder> + '_ {
    move |path: &str| {
        let io: Arc<dyn IO> = Arc::new(EncryptedIo {
            cipher: Arc::new(open_or_create(Path::new(path), params)?),
            db_path: PathBuf::from(path),
        });
        Ok(Builder::new_local(path).with_io_impl(io))
    }
}

#[cfg(test)]
mod tests {
    use super::{CipherAlgorithm, ConnectionParams, TursoEncryptedIoKeyring};
    use crate::KeyRing;
    use crate::error::KeyRingError;
    use crate::file_common::test_support::TestDir;
    use std::fs;

    fn params(s: &str) -> ConnectionParams {
        s.parse().unwrap()
    }

    #[test]
    fn works_with_both_ciphers() {
        for (name, cipher) in [
            #[cfg(not(feature = "fips"))]
            ("xchacha20poly1305", CipherAlgorithm::XChaCha20Poly1305),
            ("aes256gcm", CipherAlgorithm::Aes256Gcm),
        ] {
            let dir = TestDir::new(&format!(".test-turso-io-works-{name}"));
            let p = params(&format!(
                "password=works_test salt=1qaz2wsx3edc4rfv5tgb cipher={name}"
            ));
            assert_eq!(p.cipher, cipher);

            {
                let mut kr = TursoEncryptedIoKeyring::with_params(&p, dir.keyring_dir()).unwrap();
                for i in 0..50 {
                    kr.set_secret(format!("key_{i}"), format!("value_{i}"))
                        .unwrap();
                }
                kr.delete_secret("key_0").unwrap();
                assert!(matches!(
                    kr.get_secret("key_0"),
                    Err(KeyRingError::ItemNotFound)
                ));
            }

            {
                let mut kr = TursoEncryptedIoKeyring::with_params(&p, dir.keyring_dir()).unwrap();
                for i in 1..50 {
                    assert_eq!(
                        kr.get_secret(format!("key_{i}")).unwrap().0,
                        format!("value_{i}").as_bytes()
                    );
                }
            }

            for entry in fs::read_dir(&*dir).unwrap() {
                let raw = fs::read(entry.unwrap().path()).unwrap();
                assert!(!raw.windows(8).any(|w| w == b"value_42"));
                assert!(!raw.windows(13).any(|w| w == b"SQLite format"));
            }
        }
    }

    #[test]
    fn database_starts_with_header_and_reopens_with_password_only() {
        let dir = TestDir::new(".test-turso-io-header");
        {
            let created = params("password=header_test cipher=aes256gcm");
            let mut kr = TursoEncryptedIoKeyring::with_params(&created, dir.keyring_dir()).unwrap();
            kr.set_secret("k", b"v").unwrap();
        }
        let raw = fs::read(dir.join(super::super::turso_common::KEYRING_FILE)).unwrap();
        assert_eq!(&raw[..8], b"CRYPTEXP");
        let mut kr = TursoEncryptedIoKeyring::with_params(
            &params("password=header_test"),
            dir.keyring_dir(),
        )
        .unwrap();
        assert_eq!(kr.get_secret("k").unwrap().0, b"v");
    }

    #[test]
    fn wrong_key_and_concurrent_open_fail() {
        let dir = TestDir::new(".test-turso-io-wrongkey");
        let correct = params("password=correct_password salt=1qaz2wsx3edc4rfv5tgb");
        let wrong = params("password=wrong_password salt=1qaz2wsx3edc4rfv5tgb");

        {
            let mut kr = TursoEncryptedIoKeyring::with_params(&correct, dir.keyring_dir()).unwrap();
            kr.set_secret("x", b"secret").unwrap();
            assert!(matches!(
                TursoEncryptedIoKeyring::with_params(&correct, dir.keyring_dir()),
                Err(KeyRingError::AccessDenied { .. })
            ));
        }

        assert!(TursoEncryptedIoKeyring::with_params(&wrong, dir.keyring_dir()).is_err());
    }

    #[test]
    fn rekey_works() {
        let dir = TestDir::new(".test-turso-io-rekey");
        let old_params = params("password=old_pass salt=old_salt_value_here_");
        let new_params = params("password=new_pass salt=new_salt_value_here_ cipher=aes256gcm");

        {
            let mut kr =
                TursoEncryptedIoKeyring::with_params(&old_params, dir.keyring_dir()).unwrap();
            kr.set_secret("data", b"important").unwrap();
            let mut kr = kr.rekey(&new_params).unwrap();
            kr.set_secret("after", b"after_rekey").unwrap();
            assert_eq!(kr.get_secret("data").unwrap().0, b"important");
        }

        {
            let mut kr =
                TursoEncryptedIoKeyring::with_params(&new_params, dir.keyring_dir()).unwrap();
            assert_eq!(kr.get_secret("data").unwrap().0, b"important");
            assert_eq!(kr.get_secret("after").unwrap().0, b"after_rekey");
        }

        assert!(TursoEncryptedIoKeyring::with_params(&old_params, dir.keyring_dir()).is_err());
    }
}