use super::*;
use rusqlite::{Connection, OpenFlags};
use sqlite_plugin::flags::{AccessFlags, LockLevel, OpenKind, OpenOpts};
use sqlite_plugin::vfs::{RegisterOpts, Vfs, VfsHandle, VfsResult, register_static};
use std::ffi::CString;
use std::fs::{self, File};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::{Arc, Mutex, RwLock};
use zeroize::Zeroizing;
use super::file_common::{
acquire_single_opener_lock, file_keyring, get_keyring_file, private_open_options, sibling_path,
sync_parent_dir,
};
use super::page_cipher::{ActiveCipher, LOGICAL_PAGE_SIZE, PageFile, logical_size, truncate};
use super::page_header::{HEADER_SIZE, open_or_create};
use super::sqlite_common::{KEYRING_FILE, init_schema, lock_connection};
use crate::error::KeyRingError;
pub use super::cipher::PageCipher as CipherAlgorithm;
pub type ConnectionParams = file_common::ConnectionParams<CipherAlgorithm>;
static VFS_COUNTER: AtomicU32 = AtomicU32::new(0);
#[derive(Clone)]
pub(crate) struct EncryptedVfs {
cipher: Arc<RwLock<ActiveCipher>>,
}
impl EncryptedVfs {
fn new(cipher: ActiveCipher) -> Self {
Self {
cipher: Arc::new(RwLock::new(cipher)),
}
}
fn rotate(&self, cipher: ActiveCipher) -> Result<()> {
*self.cipher.write().map_err(|_| vfs_poisoned())? = cipher;
Ok(())
}
}
fn vfs_poisoned() -> KeyRingError {
KeyRingError::GeneralError {
msg: "VFS state lock poisoned".to_string(),
}
}
pub(crate) struct EncryptedHandle {
file: File,
layout: PageFile,
delete_on_close: bool,
path: PathBuf,
}
impl VfsHandle for EncryptedHandle {
fn readonly(&self) -> bool {
false
}
fn in_memory(&self) -> bool {
false
}
}
impl Vfs for EncryptedVfs {
type Handle = EncryptedHandle;
fn open(&self, path: Option<&str>, opts: OpenOpts) -> VfsResult<Self::Handle> {
let is_readonly = opts.mode().is_readonly();
let mut file_opts = private_open_options();
file_opts.read(true);
let path = match path {
Some(p) => {
file_opts
.write(!is_readonly)
.create(!is_readonly)
.truncate(false);
PathBuf::from(p)
}
None => {
let mut rand = [0u8; 16];
getrandom::fill(&mut rand).map_err(|_| sqlite_plugin::vars::SQLITE_CANTOPEN)?;
file_opts.write(true).create_new(true);
std::env::temp_dir().join(format!(
"cryptex-tmp-{}-{}-{}.db",
std::process::id(),
VFS_COUNTER.fetch_add(1, Ordering::Relaxed),
hex::encode(rand)
))
}
};
let file = file_opts
.open(&path)
.map_err(|_| sqlite_plugin::vars::SQLITE_CANTOPEN)?;
Ok(EncryptedHandle {
file,
layout: layout(opts.kind()),
delete_on_close: opts.delete_on_close(),
path,
})
}
fn close(&self, handle: Self::Handle) -> VfsResult<()> {
if handle.delete_on_close {
let _ = fs::remove_file(&handle.path);
}
Ok(())
}
fn delete(&self, path: &str) -> VfsResult<()> {
fs::remove_file(path).map_err(|_| sqlite_plugin::vars::SQLITE_IOERR_DELETE)
}
fn access(&self, path: &str, _flags: AccessFlags) -> VfsResult<bool> {
Ok(Path::new(path).exists())
}
fn file_size(&self, handle: &mut Self::Handle) -> VfsResult<usize> {
logical_size(&handle.file, handle.layout)
.map(|n| n as usize)
.map_err(|_| sqlite_plugin::vars::SQLITE_IOERR_FSTAT)
}
fn truncate(&self, handle: &mut Self::Handle, logical_size: usize) -> VfsResult<()> {
truncate(&mut handle.file, handle.layout, logical_size)
.map_err(|_| sqlite_plugin::vars::SQLITE_IOERR_TRUNCATE)
}
fn read(&self, handle: &mut Self::Handle, offset: usize, data: &mut [u8]) -> VfsResult<usize> {
self.cipher
.read()
.map_err(|_| sqlite_plugin::vars::SQLITE_IOERR_READ)?
.read_at(&mut handle.file, handle.layout, offset, data)
.map_err(|_| sqlite_plugin::vars::SQLITE_IOERR_READ)
}
fn write(&self, handle: &mut Self::Handle, offset: usize, data: &[u8]) -> VfsResult<usize> {
self.cipher
.read()
.map_err(|_| sqlite_plugin::vars::SQLITE_IOERR_WRITE)?
.write_at(&mut handle.file, handle.layout, offset, data)
.map_err(|_| sqlite_plugin::vars::SQLITE_IOERR_WRITE)
}
fn sync(&self, handle: &mut Self::Handle) -> VfsResult<()> {
handle
.file
.sync_all()
.map_err(|_| sqlite_plugin::vars::SQLITE_IOERR_FSYNC)
}
fn lock(&self, _handle: &mut Self::Handle, _level: LockLevel) -> VfsResult<()> {
Ok(())
}
fn unlock(&self, _handle: &mut Self::Handle, _level: LockLevel) -> VfsResult<()> {
Ok(())
}
fn check_reserved_lock(&self, _handle: &mut Self::Handle) -> VfsResult<bool> {
Ok(false)
}
}
fn layout(kind: OpenKind) -> PageFile {
match kind {
OpenKind::MainDb => PageFile::main(HEADER_SIZE),
OpenKind::MainJournal | OpenKind::Wal | OpenKind::SubJournal | OpenKind::SuperJournal => {
PageFile::JOURNAL
}
OpenKind::TempDb | OpenKind::TempJournal | OpenKind::TransientDb | OpenKind::Unknown => {
PageFile::TEMP
}
}
}
fn register_encrypted_vfs(vfs: EncryptedVfs) -> Result<String> {
let name = format!(
"cryptex-evfs-{}",
VFS_COUNTER.fetch_add(1, Ordering::Relaxed)
);
let cname = CString::new(name.as_str())
.map_err(|e| KeyRingError::GeneralError { msg: e.to_string() })?;
register_static(
cname,
vfs,
RegisterOpts {
make_default: false,
},
)
.map_err(|e| KeyRingError::GeneralError {
msg: format!("VFS registration failed (code {})", e),
})?;
Ok(name)
}
pub struct EncryptedVfsKeyring {
conn: Mutex<Connection>,
vfs: EncryptedVfs,
vfs_name: String,
db_path: PathBuf,
_lock: File,
}
file_keyring!(EncryptedVfsKeyring, conn);
impl EncryptedVfsKeyring {
pub fn with_params(connection: &ConnectionParams, path: Option<PathBuf>) -> Result<Self> {
let db_path = get_keyring_file(path, KEYRING_FILE)?;
let lock = acquire_single_opener_lock(&db_path)?;
let vfs = EncryptedVfs::new(open_or_create(&db_path, connection)?);
let vfs_name = register_encrypted_vfs(vfs.clone())?;
let conn = open_connection(&db_path, &vfs_name)?;
Ok(Self {
conn: Mutex::new(conn),
vfs,
vfs_name,
db_path,
_lock: lock,
})
}
pub fn rekey(&self, new_params: &ConnectionParams) -> Result<()> {
let mut conn = lock_connection(&self.conn)?;
let page_count: u32 = conn.pragma_query_value(None, "page_count", |row| row.get(0))?;
let tmp_path = self.db_path.with_extension("cryptex-rekey-tmp");
match fs::remove_file(&tmp_path) {
Err(e) if e.kind() != std::io::ErrorKind::NotFound => return Err(e.into()),
_ => {}
}
let new_cipher = open_or_create(&tmp_path, new_params)?;
if let Err(e) = self.reencrypt_into(&tmp_path, &new_cipher, page_count as usize) {
let _ = fs::remove_file(&tmp_path);
return Err(e);
}
*conn = Connection::open_in_memory()?;
if sibling_path(&self.db_path, "-journal").exists() {
let _ = fs::remove_file(&tmp_path);
*conn = open_connection(&self.db_path, &self.vfs_name)?;
return Err(KeyRingError::GeneralError {
msg: "the keyring has an unfinished write; reopen it and try the rekey again"
.to_string(),
});
}
if let Err(e) = fs::rename(&tmp_path, &self.db_path) {
let _ = fs::remove_file(&tmp_path);
*conn = open_connection(&self.db_path, &self.vfs_name)?;
return Err(KeyRingError::GeneralError {
msg: format!("unable to replace keyring file: {}", e),
});
}
sync_parent_dir(&self.db_path)?;
self.vfs.rotate(new_cipher)?;
*conn = open_connection(&self.db_path, &self.vfs_name)?;
Ok(())
}
fn reencrypt_into(
&self,
dst_path: &Path,
new_cipher: &ActiveCipher,
page_count: usize,
) -> Result<()> {
let main = PageFile::main(HEADER_SIZE);
let mut src = File::open(&self.db_path)?;
let mut dst = private_open_options().write(true).open(dst_path)?;
let cipher = self.vfs.cipher.read().map_err(|_| vfs_poisoned())?;
let mut page = Zeroizing::new([0u8; LOGICAL_PAGE_SIZE]);
for offset in (0..page_count).map(|n| n * LOGICAL_PAGE_SIZE) {
cipher.read_at(&mut src, main, offset, &mut *page)?;
new_cipher.write_at(&mut dst, main, offset, &*page)?;
}
dst.sync_all()?;
Ok(())
}
}
impl Drop for EncryptedVfsKeyring {
fn drop(&mut self) {
let Ok(conn) = self.conn.get_mut() else {
return;
};
let Ok(closed) = Connection::open_in_memory() else {
return;
};
*conn = closed;
let _ = self.vfs.rotate(ActiveCipher::from_key(
CipherAlgorithm::default(),
&[0u8; 32],
[0u8; 32],
));
}
}
fn open_connection(db_path: &Path, vfs_name: &str) -> Result<Connection> {
let conn = Connection::open_with_flags_and_vfs(
db_path,
OpenFlags::SQLITE_OPEN_READ_WRITE | OpenFlags::SQLITE_OPEN_CREATE,
vfs_name,
)?;
init_schema(&conn)?;
Ok(conn)
}
#[cfg(test)]
mod tests {
use super::{CipherAlgorithm, ConnectionParams, EncryptedVfs, EncryptedVfsKeyring};
use crate::KeyRing;
use crate::file_common::test_support::TestDir;
use std::fs::File;
fn params(s: &str) -> ConnectionParams {
s.parse().unwrap()
}
#[test]
fn works() {
let dir = TestDir::new(".test-evfs-works");
let p = params("password=works_test salt=1qaz2wsx3edc4rfv5tgb");
{
let res = EncryptedVfsKeyring::with_params(&p, dir.keyring_dir());
assert!(res.is_ok(), "open failed: {:?}", res.err());
let mut kr = res.unwrap();
assert!(kr.set_secret("k1", b"value1").is_ok());
let got = kr.get_secret("k1").unwrap();
assert_eq!(got.0, b"value1");
assert!(kr.delete_secret("k1").is_ok());
assert!(kr.get_secret("k1").is_err());
assert!(kr.set_secret("persist", b"persisted").is_ok());
}
{
let mut kr = EncryptedVfsKeyring::with_params(&p, dir.keyring_dir()).unwrap();
let got = kr.get_secret("persist").unwrap();
assert_eq!(got.0, b"persisted");
}
}
#[test]
fn drop_wipes_the_vfs_key() {
let dir = TestDir::new(".test-evfs-drop-wipe");
let p = params("password=drop_wipe_test salt=1qaz2wsx3edc4rfv5tgb");
let mut kr = EncryptedVfsKeyring::with_params(&p, dir.keyring_dir()).unwrap();
kr.set_secret("k", b"v").unwrap();
let vfs = kr.vfs.clone();
let db_path = kr.db_path.clone();
let reads_file = |vfs: &EncryptedVfs| {
let mut file = File::open(&db_path).unwrap();
let cipher = vfs.cipher.read().unwrap();
cipher
.read_at(
&mut file,
super::PageFile::main(super::HEADER_SIZE),
0,
&mut [0u8; 16],
)
.is_ok()
};
assert!(reads_file(&vfs));
drop(kr);
assert!(
!reads_file(&vfs),
"the dropped keyring's VFS still holds its key"
);
let mut kr = EncryptedVfsKeyring::with_params(&p, dir.keyring_dir()).unwrap();
assert_eq!(kr.get_secret("k").unwrap().0, b"v");
}
#[test]
fn header_decides_cipher_and_salt_on_reopen() {
let dir = TestDir::new(".test-evfs-header");
let created = params("password=header_test cipher=aes256gcm");
{
let mut kr = EncryptedVfsKeyring::with_params(&created, dir.keyring_dir()).unwrap();
kr.set_secret("k", b"header-secret").unwrap();
}
let raw = std::fs::read(dir.join(super::KEYRING_FILE)).unwrap();
assert_eq!(&raw[..8], b"CRYPTEXP");
assert!(!raw.windows(13).any(|w| w == b"header-secret"));
assert!(!raw.windows(13).any(|w| w == b"SQLite format"));
let mut kr =
EncryptedVfsKeyring::with_params(¶ms("password=header_test"), dir.keyring_dir())
.unwrap();
assert_eq!(kr.get_secret("k").unwrap().0, b"header-secret");
}
#[test]
fn edited_header_breaks_the_keyring() {
let dir = TestDir::new(".test-evfs-header-edit");
let p = params("password=edit_test");
{
let mut kr = EncryptedVfsKeyring::with_params(&p, dir.keyring_dir()).unwrap();
kr.set_secret("k", b"v").unwrap();
}
let path = dir.join(super::KEYRING_FILE);
let mut raw = std::fs::read(&path).unwrap();
raw[60] ^= 1;
std::fs::write(&path, raw).unwrap();
assert!(EncryptedVfsKeyring::with_params(&p, dir.keyring_dir()).is_err());
}
#[test]
fn raw_key_keyring_round_trips() {
let dir = TestDir::new(".test-evfs-raw-key");
let p = params(&format!("key={}", "5a".repeat(32)));
{
let mut kr = EncryptedVfsKeyring::with_params(&p, dir.keyring_dir()).unwrap();
kr.set_secret("k", b"v").unwrap();
}
let mut kr = EncryptedVfsKeyring::with_params(&p, dir.keyring_dir()).unwrap();
assert_eq!(kr.get_secret("k").unwrap().0, b"v");
drop(kr);
let pw = params("password=not_the_key");
assert!(EncryptedVfsKeyring::with_params(&pw, dir.keyring_dir()).is_err());
}
#[test]
fn rekey_refuses_to_run_over_a_leftover_journal() {
let dir = TestDir::new(".test-evfs-rekey-journal");
let p = params("password=journal_test");
let mut kr = EncryptedVfsKeyring::with_params(&p, dir.keyring_dir()).unwrap();
kr.set_secret("k", b"v").unwrap();
let journal = dir.join(format!("{}-journal", super::KEYRING_FILE));
File::create(&journal).unwrap();
let err = kr.rekey(¶ms("password=new_journal_test")).unwrap_err();
assert!(err.to_string().contains("unfinished write"), "{err}");
assert_eq!(kr.get_secret("k").unwrap().0, b"v");
std::fs::remove_file(&journal).unwrap();
kr.rekey(¶ms("password=new_journal_test")).unwrap();
assert_eq!(kr.get_secret("k").unwrap().0, b"v");
}
#[test]
fn concurrent_open_is_rejected() {
let dir = TestDir::new(".test-evfs-concurrent");
let p = params("password=concurrent_test salt=1qaz2wsx3edc4rfv5tgb");
let first = EncryptedVfsKeyring::with_params(&p, dir.keyring_dir()).unwrap();
let second = EncryptedVfsKeyring::with_params(&p, dir.keyring_dir());
assert!(
matches!(second, Err(crate::error::KeyRingError::AccessDenied { .. })),
"expected AccessDenied for concurrent open, got: {:?}",
second.err()
);
drop(first);
let reopened = EncryptedVfsKeyring::with_params(&p, dir.keyring_dir());
assert!(
reopened.is_ok(),
"reopen after drop failed: {:?}",
reopened.err()
);
}
#[test]
fn wrong_key_fails() {
let dir = TestDir::new(".test-evfs-wrongkey");
let correct = params("password=correct_password salt=1qaz2wsx3edc4rfv5tgb");
let wrong = params("password=wrong_password salt=1qaz2wsx3edc4rfv5tgb");
{
let mut kr = EncryptedVfsKeyring::with_params(&correct, dir.keyring_dir()).unwrap();
kr.set_secret("x", b"secret").unwrap();
}
{
let result = EncryptedVfsKeyring::with_params(&wrong, dir.keyring_dir());
assert!(result.is_err(), "expected error with wrong key");
}
}
#[test]
fn invalid_hex_key_is_an_error() {
let result = "key=not-hex".parse::<ConnectionParams>();
assert!(result.is_err());
}
#[test]
fn rekey_works() {
let dir = TestDir::new(".test-evfs-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_");
{
let mut kr = EncryptedVfsKeyring::with_params(&old_params, dir.keyring_dir()).unwrap();
kr.set_secret("data", b"important").unwrap();
kr.rekey(&new_params).unwrap();
}
{
let mut kr = EncryptedVfsKeyring::with_params(&new_params, dir.keyring_dir()).unwrap();
assert_eq!(kr.get_secret("data").unwrap().0, b"important");
}
{
let result = EncryptedVfsKeyring::with_params(&old_params, dir.keyring_dir());
assert!(result.is_err(), "old key should fail after rekey");
}
}
#[test]
fn rekey_allows_continued_use_on_same_connection() {
let dir = TestDir::new(".test-evfs-rekey-continue");
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 = EncryptedVfsKeyring::with_params(&old_params, dir.keyring_dir()).unwrap();
for i in 0..50 {
kr.set_secret(format!("before_{i}"), format!("value_{i}"))
.unwrap();
}
kr.rekey(&new_params).unwrap();
for i in 0..50 {
kr.set_secret(format!("after_{i}"), format!("value_{i}"))
.unwrap();
}
assert_eq!(kr.get_secret("before_7").unwrap().0, b"value_7");
kr.delete_secret("before_0").unwrap();
}
{
let mut kr = EncryptedVfsKeyring::with_params(&new_params, dir.keyring_dir()).unwrap();
assert!(kr.get_secret("before_0").is_err());
for i in 1..50 {
assert_eq!(
kr.get_secret(format!("before_{i}")).unwrap().0,
format!("value_{i}").as_bytes()
);
}
for i in 0..50 {
assert_eq!(
kr.get_secret(format!("after_{i}")).unwrap().0,
format!("value_{i}").as_bytes()
);
}
}
assert!(EncryptedVfsKeyring::with_params(&old_params, dir.keyring_dir()).is_err());
}
#[test]
fn rekey_through_shared_reference_on_another_thread() {
let dir = TestDir::new(".test-evfs-rekey-shared");
let old_params = params("password=old_pass salt=old_salt_value_here_");
let new_params = params("password=new_pass salt=new_salt_value_here_");
let mut kr = EncryptedVfsKeyring::with_params(&old_params, dir.keyring_dir()).unwrap();
kr.set_secret("shared", b"value").unwrap();
let kr = std::sync::Arc::new(kr);
let worker = std::sync::Arc::clone(&kr);
std::thread::spawn(move || worker.rekey(&new_params))
.join()
.unwrap()
.unwrap();
let mut kr = std::sync::Arc::try_unwrap(kr).ok().unwrap();
assert_eq!(kr.get_secret("shared").unwrap().0, b"value");
drop(kr);
assert!(EncryptedVfsKeyring::with_params(&old_params, dir.keyring_dir()).is_err());
}
#[test]
fn bulk_insert_and_retrieve() {
let dir = TestDir::new(".test-evfs-bulk");
let p = params("password=bulk_test salt=bulk_salt_value_here_");
let entries: Vec<(String, Vec<u8>)> = (0..50)
.map(|i| {
(
format!("key_{:02}", i),
format!("value_{:06}", i).into_bytes(),
)
})
.collect();
{
let mut kr = EncryptedVfsKeyring::with_params(&p, dir.keyring_dir()).unwrap();
for (k, v) in &entries {
kr.set_secret(k, v).unwrap();
}
}
{
let mut kr = EncryptedVfsKeyring::with_params(&p, dir.keyring_dir()).unwrap();
for (k, v) in &entries {
let got = kr.get_secret(k).unwrap();
assert_eq!(&got.0, v, "mismatch for {k}");
}
}
}
#[test]
fn aes_bulk_insert_and_retrieve() {
let dir = TestDir::new(".test-evfs-aes-bulk");
let p = params("password=aes_bulk_test salt=aes_bulk_salt_here_ cipher=aes256gcm");
assert_eq!(p.cipher, CipherAlgorithm::Aes256Gcm);
let entries: Vec<(String, Vec<u8>)> = (0..50)
.map(|i| {
(
format!("key_{:02}", i),
format!("value_{:06}", i).into_bytes(),
)
})
.collect();
{
let mut kr = EncryptedVfsKeyring::with_params(&p, dir.keyring_dir()).unwrap();
for (k, v) in &entries {
kr.set_secret(k, v).unwrap();
}
}
{
let mut kr = EncryptedVfsKeyring::with_params(&p, dir.keyring_dir()).unwrap();
for (k, v) in &entries {
let got = kr.get_secret(k).unwrap();
assert_eq!(&got.0, v, "mismatch for {k}");
}
}
}
}