use crate::cert::CertPair;
use crate::{Error, Result};
use serde::Serialize;
use serde::de::DeserializeOwned;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::Mutex;
pub trait CertStore: Send + Sync {
fn get(&self, key: &str) -> Result<Option<Vec<u8>>>;
fn put(&self, key: &str, value: &[u8]) -> Result<()>;
fn delete(&self, key: &str) -> Result<()>;
}
pub fn check_key(key: &str) -> Result<()> {
let ok = !key.is_empty()
&& key.len() <= 200
&& !key.starts_with('/')
&& key.split('/').all(|p| !p.is_empty() && p != "." && p != "..")
&& key.bytes().all(|b| matches!(b, b'a'..=b'z' | b'0'..=b'9' | b'.' | b'_' | b'-' | b'/'));
if ok { Ok(()) } else { Err(Error::Store(format!("bad store key {key:?}"))) }
}
pub fn get_json<T: DeserializeOwned>(s: &dyn CertStore, key: &str) -> Result<Option<T>> {
match s.get(key)? {
Some(b) => Ok(Some(serde_json::from_slice(&b)?)),
None => Ok(None),
}
}
pub fn put_json<T: Serialize>(s: &dyn CertStore, key: &str, value: &T) -> Result<()> {
s.put(key, &serde_json::to_vec_pretty(value)?)
}
pub fn cert_key(set: &str) -> String {
format!("cert/{set}.json")
}
pub fn state_key(set: &str) -> String {
format!("state/{set}.json")
}
pub fn account_key(dir: &str) -> String {
use sha2::Digest;
let tag: String = sha2::Sha256::digest(dir.as_bytes())[..8].iter().map(|b| format!("{b:02x}")).collect();
format!("account/{tag}.json")
}
pub fn load_cert(s: &dyn CertStore, set: &str) -> Result<Option<CertPair>> {
get_json(s, &cert_key(set))
}
pub fn save_cert(s: &dyn CertStore, set: &str, pair: &CertPair) -> Result<()> {
put_json(s, &cert_key(set), pair)
}
#[derive(Default)]
pub struct MemoryStore(Mutex<HashMap<String, Vec<u8>>>);
impl MemoryStore {
pub fn new() -> Self {
Self::default()
}
pub fn keys(&self) -> Vec<String> {
let mut k: Vec<String> = self.0.lock().unwrap().keys().cloned().collect();
k.sort();
k
}
}
impl CertStore for MemoryStore {
fn get(&self, key: &str) -> Result<Option<Vec<u8>>> {
check_key(key)?;
Ok(self.0.lock().unwrap().get(key).cloned())
}
fn put(&self, key: &str, value: &[u8]) -> Result<()> {
check_key(key)?;
self.0.lock().unwrap().insert(key.to_string(), value.to_vec());
Ok(())
}
fn delete(&self, key: &str) -> Result<()> {
check_key(key)?;
self.0.lock().unwrap().remove(key);
Ok(())
}
}
#[derive(Debug, Clone)]
pub struct FileStore {
root: PathBuf,
}
impl FileStore {
pub fn new(root: impl Into<PathBuf>) -> Self {
Self { root: root.into() }
}
pub fn root(&self) -> &Path {
&self.root
}
fn path(&self, key: &str) -> Result<PathBuf> {
check_key(key)?;
Ok(self.root.join(key))
}
}
fn private_dir(dir: &Path) -> Result<()> {
let mut b = std::fs::DirBuilder::new();
b.recursive(true);
#[cfg(unix)]
{
use std::os::unix::fs::DirBuilderExt;
b.mode(0o700);
}
b.create(dir)?;
Ok(())
}
pub fn write_private(path: &Path, data: &[u8]) -> Result<()> {
use std::io::Write;
let dir = path.parent().ok_or_else(|| Error::Store("no parent directory".into()))?;
private_dir(dir)?;
let name = path.file_name().and_then(|n| n.to_str()).unwrap_or("x");
let tmp = dir.join(format!(".{name}.{}.tmp", std::process::id()));
let mut o = std::fs::OpenOptions::new();
o.write(true).create(true).truncate(true);
#[cfg(unix)]
{
use std::os::unix::fs::OpenOptionsExt;
o.mode(0o600);
}
let res = (|| {
let mut f = o.open(&tmp)?;
f.write_all(data)?;
f.sync_all()?;
std::fs::rename(&tmp, path)
})();
if res.is_err() {
let _ = std::fs::remove_file(&tmp);
}
Ok(res?)
}
impl CertStore for FileStore {
fn get(&self, key: &str) -> Result<Option<Vec<u8>>> {
match std::fs::read(self.path(key)?) {
Ok(b) => Ok(Some(b)),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
Err(e) => Err(e.into()),
}
}
fn put(&self, key: &str, value: &[u8]) -> Result<()> {
write_private(&self.path(key)?, value)
}
fn delete(&self, key: &str) -> Result<()> {
match std::fs::remove_file(self.path(key)?) {
Err(e) if e.kind() != std::io::ErrorKind::NotFound => Err(e.into()),
_ => Ok(()),
}
}
}
#[cfg(feature = "storage")]
pub use sql::SqlStore;
#[cfg(feature = "storage")]
mod sql {
use super::{CertStore, check_key};
use crate::{Error, Result};
use std::sync::{Arc, Mutex};
use weirdnet_storage::rusqlite::{self, OptionalExtension};
pub struct SqlStore {
conn: Arc<Mutex<rusqlite::Connection>>,
table: String,
}
fn db(e: rusqlite::Error) -> Error {
Error::Store(e.to_string())
}
impl SqlStore {
pub fn new(conn: Arc<Mutex<rusqlite::Connection>>) -> Result<Self> {
Self::with_table(conn, "weirdnet_tls")
}
pub fn with_table(conn: Arc<Mutex<rusqlite::Connection>>, table: &str) -> Result<Self> {
let table = weirdnet_storage::quote_ident(table);
conn.lock()
.unwrap()
.execute_batch(&format!("CREATE TABLE IF NOT EXISTS {table} (k TEXT PRIMARY KEY NOT NULL, v BLOB NOT NULL)"))
.map_err(db)?;
Ok(Self { conn, table })
}
}
impl CertStore for SqlStore {
fn get(&self, key: &str) -> Result<Option<Vec<u8>>> {
check_key(key)?;
let c = self.conn.lock().unwrap();
c.query_row(&format!("SELECT v FROM {} WHERE k = ?1", self.table), [key], |r| r.get(0)).optional().map_err(db)
}
fn put(&self, key: &str, value: &[u8]) -> Result<()> {
check_key(key)?;
let c = self.conn.lock().unwrap();
c.execute(
&format!("INSERT INTO {} (k, v) VALUES (?1, ?2) ON CONFLICT(k) DO UPDATE SET v = excluded.v", self.table),
rusqlite::params![key, value],
)
.map_err(db)?;
Ok(())
}
fn delete(&self, key: &str) -> Result<()> {
check_key(key)?;
let c = self.conn.lock().unwrap();
c.execute(&format!("DELETE FROM {} WHERE k = ?1", self.table), [key]).map_err(db)?;
Ok(())
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn keys_are_checked() {
for bad in ["", "/a", "a/../b", "a//b", "A", "a b", "./a"] {
assert!(check_key(bad).is_err(), "{bad:?}");
}
check_key("cert/main.json").unwrap();
assert!(account_key("https://acme-v02.api.letsencrypt.org/directory").starts_with("account/"));
}
#[test]
fn file_store_is_private_and_atomic() {
let d = tempfile::tempdir().unwrap();
let s = FileStore::new(d.path().join("tls"));
assert_eq!(s.get("cert/a.json").unwrap(), None);
s.put("cert/a.json", b"one").unwrap();
s.put("cert/a.json", b"two").unwrap();
assert_eq!(s.get("cert/a.json").unwrap().as_deref(), Some(&b"two"[..]));
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let m = std::fs::metadata(d.path().join("tls/cert/a.json")).unwrap().permissions().mode();
assert_eq!(m & 0o777, 0o600);
let m = std::fs::metadata(d.path().join("tls/cert")).unwrap().permissions().mode();
assert_eq!(m & 0o777, 0o700);
}
assert_eq!(std::fs::read_dir(d.path().join("tls/cert")).unwrap().count(), 1, "no temp files left");
s.delete("cert/a.json").unwrap();
s.delete("cert/a.json").unwrap();
assert_eq!(s.get("cert/a.json").unwrap(), None);
}
#[cfg(feature = "storage")]
#[test]
fn sql_store_round_trip() {
use std::sync::{Arc, Mutex};
let conn = weirdnet_storage::rusqlite::Connection::open_in_memory().unwrap();
let s = SqlStore::new(Arc::new(Mutex::new(conn))).unwrap();
assert_eq!(s.get("cert/a.json").unwrap(), None);
s.put("cert/a.json", b"one").unwrap();
s.put("cert/a.json", b"two").unwrap();
assert_eq!(s.get("cert/a.json").unwrap().as_deref(), Some(&b"two"[..]));
s.delete("cert/a.json").unwrap();
assert_eq!(s.get("cert/a.json").unwrap(), None);
assert!(s.put("../x", b"").is_err());
}
#[test]
fn json_round_trip() {
let s = MemoryStore::new();
let p = CertPair { chain_pem: "c".into(), key_pem: "k".into() };
save_cert(&s, "main", &p).unwrap();
assert_eq!(load_cert(&s, "main").unwrap(), Some(p));
assert_eq!(s.keys(), vec!["cert/main.json"]);
}
}