Add SQLCipher storage primitive
This commit is contained in:
23
Cargo.lock
generated
23
Cargo.lock
generated
@@ -799,6 +799,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2e99fb7a497b1e3339bc746195567ed8d3e24945ecd636e3619d20b9de9e9149"
|
||||
dependencies = [
|
||||
"cc",
|
||||
"openssl-sys",
|
||||
"pkg-config",
|
||||
"vcpkg",
|
||||
]
|
||||
@@ -860,6 +861,28 @@ version = "1.21.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"
|
||||
|
||||
[[package]]
|
||||
name = "openssl-src"
|
||||
version = "300.6.1+3.6.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "46eb8fb9fb3b61ce1c0f8a026c4c1a0714d3a9e138e7fbde78753ce2babc3846"
|
||||
dependencies = [
|
||||
"cc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "openssl-sys"
|
||||
version = "0.9.117"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b47e7e6bb2c38cd930d25a23b40fa52e068c10e85f3e03a7f5ba5aaca5713695"
|
||||
dependencies = [
|
||||
"cc",
|
||||
"libc",
|
||||
"openssl-src",
|
||||
"pkg-config",
|
||||
"vcpkg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "option-ext"
|
||||
version = "0.2.0"
|
||||
|
||||
@@ -14,7 +14,7 @@ gui = ["x11", "dep:gtk4", "dep:gdk4-x11", "dep:glib", "dep:ksni"]
|
||||
[dependencies]
|
||||
anyhow = "1.0"
|
||||
dirs = "6.0"
|
||||
rusqlite = { version = "0.32", features = ["bundled"] }
|
||||
rusqlite = { version = "0.32", features = ["bundled-sqlcipher-vendored-openssl"] }
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
sha2 = "0.10"
|
||||
thiserror = "2.0"
|
||||
|
||||
@@ -24,6 +24,18 @@ impl Database {
|
||||
}
|
||||
|
||||
pub fn open(path: impl AsRef<Path>) -> Result<Self> {
|
||||
Self::open_internal(path, None)
|
||||
}
|
||||
|
||||
pub fn open_encrypted(path: impl AsRef<Path>, passphrase: &str) -> Result<Self> {
|
||||
anyhow::ensure!(
|
||||
!passphrase.is_empty(),
|
||||
"database passphrase must not be empty"
|
||||
);
|
||||
Self::open_internal(path, Some(passphrase))
|
||||
}
|
||||
|
||||
fn open_internal(path: impl AsRef<Path>, passphrase: Option<&str>) -> Result<Self> {
|
||||
let path = path.as_ref().to_path_buf();
|
||||
if let Ok(metadata) = std::fs::symlink_metadata(&path) {
|
||||
anyhow::ensure!(
|
||||
@@ -45,6 +57,14 @@ impl Database {
|
||||
}
|
||||
let connection = Connection::open(&path)
|
||||
.with_context(|| format!("failed to open database {}", path.display()))?;
|
||||
if let Some(passphrase) = passphrase {
|
||||
connection
|
||||
.pragma_update(None, "key", passphrase)
|
||||
.context("failed to apply database encryption key")?;
|
||||
connection
|
||||
.query_row("SELECT count(*) FROM sqlite_master", [], |_row| Ok(()))
|
||||
.context("incorrect database passphrase or corrupt encrypted database")?;
|
||||
}
|
||||
if path.exists() {
|
||||
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600)).with_context(
|
||||
|| format!("failed to restrict database permissions {}", path.display()),
|
||||
@@ -294,4 +314,29 @@ mod tests {
|
||||
assert!(db.delete(404).is_err());
|
||||
assert!(db.toggle_pinned(404, 1).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encrypted_database_requires_the_correct_passphrase() {
|
||||
let path = std::env::temp_dir().join(format!(
|
||||
"cliphoard-encrypted-{}-{}.db",
|
||||
std::process::id(),
|
||||
std::thread::current().name().unwrap_or("test")
|
||||
));
|
||||
let _ = std::fs::remove_file(&path);
|
||||
let db = Database::open_encrypted(&path, "correct horse battery staple").unwrap();
|
||||
db.upsert_content("sensitive test payload", 1).unwrap();
|
||||
drop(db);
|
||||
|
||||
let header = std::fs::read(&path).unwrap();
|
||||
assert_ne!(&header[..16], b"SQLite format 3\0");
|
||||
assert!(Database::open_encrypted(&path, "wrong passphrase").is_err());
|
||||
let db = Database::open_encrypted(&path, "correct horse battery staple").unwrap();
|
||||
assert_eq!(
|
||||
db.load_entries().unwrap()[0].content,
|
||||
"sensitive test payload"
|
||||
);
|
||||
|
||||
drop(db);
|
||||
let _ = std::fs::remove_file(path);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user