2020-04-17 12:11:25 +00:00
|
|
|
use anyhow::{bail, format_err, Error};
|
2019-10-12 15:58:08 +00:00
|
|
|
use std::convert::TryFrom;
|
2019-12-31 14:23:41 +00:00
|
|
|
use std::path::Path;
|
2019-10-12 15:58:08 +00:00
|
|
|
|
|
|
|
use serde_json::{json, Value};
|
2020-07-09 07:20:49 +00:00
|
|
|
use ::serde::{Deserialize, Serialize};
|
2019-10-12 15:58:08 +00:00
|
|
|
|
2020-07-08 14:07:14 +00:00
|
|
|
use crate::backup::{BackupDir, CryptMode, CryptConfig};
|
2019-10-12 15:58:08 +00:00
|
|
|
|
|
|
|
pub const MANIFEST_BLOB_NAME: &str = "index.json.blob";
|
2020-05-30 12:04:15 +00:00
|
|
|
pub const CLIENT_LOG_BLOB_NAME: &str = "client.log.blob";
|
2019-10-12 15:58:08 +00:00
|
|
|
|
2020-07-09 07:20:49 +00:00
|
|
|
mod hex_csum {
|
|
|
|
use serde::{self, Deserialize, Serializer, Deserializer};
|
|
|
|
|
|
|
|
pub fn serialize<S>(
|
|
|
|
csum: &[u8; 32],
|
|
|
|
serializer: S,
|
|
|
|
) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: Serializer,
|
|
|
|
{
|
|
|
|
let s = proxmox::tools::digest_to_hex(csum);
|
|
|
|
serializer.serialize_str(&s)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn deserialize<'de, D>(
|
|
|
|
deserializer: D,
|
|
|
|
) -> Result<[u8; 32], D::Error>
|
|
|
|
where
|
|
|
|
D: Deserializer<'de>,
|
|
|
|
{
|
|
|
|
let s = String::deserialize(deserializer)?;
|
|
|
|
proxmox::tools::hex_to_digest(&s).map_err(serde::de::Error::custom)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-09 11:25:38 +00:00
|
|
|
fn crypt_mode_none() -> CryptMode { CryptMode::None }
|
|
|
|
fn empty_value() -> Value { json!({}) }
|
|
|
|
|
2020-07-09 07:20:49 +00:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
#[serde(rename_all="kebab-case")]
|
2019-12-31 14:23:41 +00:00
|
|
|
pub struct FileInfo {
|
|
|
|
pub filename: String,
|
2020-07-09 11:25:38 +00:00
|
|
|
#[serde(default="crypt_mode_none")] // to be compatible with < 0.8.0 backups
|
2020-07-07 13:20:20 +00:00
|
|
|
pub crypt_mode: CryptMode,
|
2019-12-31 14:23:41 +00:00
|
|
|
pub size: u64,
|
2020-07-09 07:20:49 +00:00
|
|
|
#[serde(with = "hex_csum")]
|
2019-12-31 14:23:41 +00:00
|
|
|
pub csum: [u8; 32],
|
2019-10-12 15:58:08 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 07:20:49 +00:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
#[serde(rename_all="kebab-case")]
|
2019-10-12 15:58:08 +00:00
|
|
|
pub struct BackupManifest {
|
2020-07-09 07:20:49 +00:00
|
|
|
backup_type: String,
|
|
|
|
backup_id: String,
|
|
|
|
backup_time: i64,
|
2019-10-12 15:58:08 +00:00
|
|
|
files: Vec<FileInfo>,
|
2020-07-09 11:25:38 +00:00
|
|
|
#[serde(default="empty_value")] // to be compatible with < 0.8.0 backups
|
2020-07-09 07:20:49 +00:00
|
|
|
pub unprotected: Value,
|
2019-10-12 15:58:08 +00:00
|
|
|
}
|
|
|
|
|
2019-12-31 14:23:41 +00:00
|
|
|
#[derive(PartialEq)]
|
|
|
|
pub enum ArchiveType {
|
|
|
|
FixedIndex,
|
|
|
|
DynamicIndex,
|
|
|
|
Blob,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn archive_type<P: AsRef<Path>>(
|
|
|
|
archive_name: P,
|
|
|
|
) -> Result<ArchiveType, Error> {
|
|
|
|
|
|
|
|
let archive_name = archive_name.as_ref();
|
|
|
|
let archive_type = match archive_name.extension().and_then(|ext| ext.to_str()) {
|
|
|
|
Some("didx") => ArchiveType::DynamicIndex,
|
|
|
|
Some("fidx") => ArchiveType::FixedIndex,
|
|
|
|
Some("blob") => ArchiveType::Blob,
|
|
|
|
_ => bail!("unknown archive type: {:?}", archive_name),
|
|
|
|
};
|
|
|
|
Ok(archive_type)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-12 15:58:08 +00:00
|
|
|
impl BackupManifest {
|
|
|
|
|
|
|
|
pub fn new(snapshot: BackupDir) -> Self {
|
2020-07-09 07:20:49 +00:00
|
|
|
Self {
|
|
|
|
backup_type: snapshot.group().backup_type().into(),
|
|
|
|
backup_id: snapshot.group().backup_id().into(),
|
|
|
|
backup_time: snapshot.backup_time().timestamp(),
|
|
|
|
files: Vec::new(),
|
|
|
|
unprotected: json!({}),
|
|
|
|
}
|
2019-10-12 15:58:08 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 13:20:20 +00:00
|
|
|
pub fn add_file(&mut self, filename: String, size: u64, csum: [u8; 32], crypt_mode: CryptMode) -> Result<(), Error> {
|
2019-12-31 14:23:41 +00:00
|
|
|
let _archive_type = archive_type(&filename)?; // check type
|
2020-07-07 13:20:20 +00:00
|
|
|
self.files.push(FileInfo { filename, size, csum, crypt_mode });
|
2019-12-31 14:23:41 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn files(&self) -> &[FileInfo] {
|
|
|
|
&self.files[..]
|
2019-10-12 15:58:08 +00:00
|
|
|
}
|
|
|
|
|
2019-10-13 08:09:12 +00:00
|
|
|
fn lookup_file_info(&self, name: &str) -> Result<&FileInfo, Error> {
|
|
|
|
|
|
|
|
let info = self.files.iter().find(|item| item.filename == name);
|
|
|
|
|
|
|
|
match info {
|
|
|
|
None => bail!("manifest does not contain file '{}'", name),
|
|
|
|
Some(info) => Ok(info),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn verify_file(&self, name: &str, csum: &[u8; 32], size: u64) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let info = self.lookup_file_info(name)?;
|
|
|
|
|
|
|
|
if size != info.size {
|
2020-06-03 17:09:58 +00:00
|
|
|
bail!("wrong size for file '{}' ({} != {})", name, info.size, size);
|
2019-10-13 08:09:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if csum != &info.csum {
|
|
|
|
bail!("wrong checksum for file '{}'", name);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-07-09 07:20:49 +00:00
|
|
|
// Generate cannonical json
|
2020-07-09 10:08:00 +00:00
|
|
|
fn to_canonical_json(value: &Value) -> Result<Vec<u8>, Error> {
|
|
|
|
let mut data = Vec::new();
|
|
|
|
Self::write_canonical_json(value, &mut data)?;
|
|
|
|
Ok(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_canonical_json(value: &Value, output: &mut Vec<u8>) -> Result<(), Error> {
|
2020-07-09 07:20:49 +00:00
|
|
|
match value {
|
|
|
|
Value::Null => bail!("got unexpected null value"),
|
2020-07-09 10:08:00 +00:00
|
|
|
Value::String(_) | Value::Number(_) | Value::Bool(_) => {
|
|
|
|
serde_json::to_writer(output, &value)?;
|
2020-07-09 07:20:49 +00:00
|
|
|
}
|
|
|
|
Value::Array(list) => {
|
2020-07-09 10:08:00 +00:00
|
|
|
output.push(b'[');
|
|
|
|
let mut iter = list.iter();
|
|
|
|
if let Some(item) = iter.next() {
|
|
|
|
Self::write_canonical_json(item, output)?;
|
|
|
|
for item in iter {
|
|
|
|
output.push(b',');
|
|
|
|
Self::write_canonical_json(item, output)?;
|
|
|
|
}
|
2020-07-09 07:20:49 +00:00
|
|
|
}
|
2020-07-09 10:08:00 +00:00
|
|
|
output.push(b']');
|
2020-07-09 07:20:49 +00:00
|
|
|
}
|
|
|
|
Value::Object(map) => {
|
2020-07-09 10:08:00 +00:00
|
|
|
output.push(b'{');
|
|
|
|
let mut keys: Vec<&str> = map.keys().map(String::as_str).collect();
|
2020-07-09 07:20:49 +00:00
|
|
|
keys.sort();
|
2020-07-09 10:08:00 +00:00
|
|
|
let mut iter = keys.into_iter();
|
|
|
|
if let Some(key) = iter.next() {
|
|
|
|
output.extend(key.as_bytes());
|
|
|
|
output.push(b':');
|
|
|
|
Self::write_canonical_json(&map[key], output)?;
|
|
|
|
for key in iter {
|
|
|
|
output.push(b',');
|
|
|
|
output.extend(key.as_bytes());
|
|
|
|
output.push(b':');
|
|
|
|
Self::write_canonical_json(&map[key], output)?;
|
|
|
|
}
|
2020-07-09 07:20:49 +00:00
|
|
|
}
|
2020-07-09 10:08:00 +00:00
|
|
|
output.push(b'}');
|
2020-07-09 07:20:49 +00:00
|
|
|
}
|
2020-07-08 14:07:14 +00:00
|
|
|
}
|
2020-07-09 07:20:49 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Compute manifest signature
|
|
|
|
///
|
|
|
|
/// By generating a HMAC SHA256 over the canonical json
|
|
|
|
/// representation, The 'unpreotected' property is excluded.
|
|
|
|
pub fn signature(&self, crypt_config: &CryptConfig) -> Result<[u8; 32], Error> {
|
2020-07-09 07:48:30 +00:00
|
|
|
Self::json_signature(&serde_json::to_value(&self)?, crypt_config)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn json_signature(data: &Value, crypt_config: &CryptConfig) -> Result<[u8; 32], Error> {
|
2020-07-09 07:20:49 +00:00
|
|
|
|
2020-07-09 07:48:30 +00:00
|
|
|
let mut signed_data = data.clone();
|
2020-07-09 07:20:49 +00:00
|
|
|
|
|
|
|
signed_data.as_object_mut().unwrap().remove("unprotected"); // exclude
|
2020-07-10 08:31:42 +00:00
|
|
|
signed_data.as_object_mut().unwrap().remove("signature"); // exclude
|
2020-07-09 07:20:49 +00:00
|
|
|
|
2020-07-09 10:08:00 +00:00
|
|
|
let canonical = Self::to_canonical_json(&signed_data)?;
|
2020-07-08 14:07:14 +00:00
|
|
|
|
2020-07-09 10:08:00 +00:00
|
|
|
let sig = crypt_config.compute_auth_tag(&canonical);
|
2020-07-09 07:20:49 +00:00
|
|
|
|
|
|
|
Ok(sig)
|
2020-07-08 14:07:14 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 07:20:49 +00:00
|
|
|
/// Converts the Manifest into json string, and add a signature if there is a crypt_config.
|
2020-07-09 09:28:05 +00:00
|
|
|
pub fn to_string(&self, crypt_config: Option<&CryptConfig>) -> Result<String, Error> {
|
2020-07-09 07:20:49 +00:00
|
|
|
|
|
|
|
let mut manifest = serde_json::to_value(&self)?;
|
2020-07-08 14:07:14 +00:00
|
|
|
|
|
|
|
if let Some(crypt_config) = crypt_config {
|
2020-07-09 07:20:49 +00:00
|
|
|
let sig = self.signature(crypt_config)?;
|
2020-07-08 14:07:14 +00:00
|
|
|
manifest["signature"] = proxmox::tools::digest_to_hex(&sig).into();
|
|
|
|
}
|
|
|
|
|
2020-07-09 07:20:49 +00:00
|
|
|
let manifest = serde_json::to_string_pretty(&manifest).unwrap().into();
|
|
|
|
Ok(manifest)
|
2019-10-12 15:58:08 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 07:20:49 +00:00
|
|
|
/// Try to read the manifest. This verifies the signature if there is a crypt_config.
|
|
|
|
pub fn from_data(data: &[u8], crypt_config: Option<&CryptConfig>) -> Result<BackupManifest, Error> {
|
|
|
|
let json: Value = serde_json::from_slice(data)?;
|
|
|
|
let signature = json["signature"].as_str().map(String::from);
|
|
|
|
|
|
|
|
if let Some(ref crypt_config) = crypt_config {
|
|
|
|
if let Some(signature) = signature {
|
2020-07-09 07:48:30 +00:00
|
|
|
let expected_signature = proxmox::tools::digest_to_hex(&Self::json_signature(&json, crypt_config)?);
|
2020-07-09 07:20:49 +00:00
|
|
|
if signature != expected_signature {
|
|
|
|
bail!("wrong signature in manifest");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// not signed: warn/fail?
|
|
|
|
}
|
|
|
|
}
|
2020-07-09 07:48:30 +00:00
|
|
|
|
|
|
|
let manifest: BackupManifest = serde_json::from_value(json)?;
|
2020-07-09 07:20:49 +00:00
|
|
|
Ok(manifest)
|
|
|
|
}
|
2019-10-12 15:58:08 +00:00
|
|
|
}
|
2020-07-09 07:20:49 +00:00
|
|
|
|
2020-07-09 07:48:30 +00:00
|
|
|
|
2020-01-05 15:20:26 +00:00
|
|
|
impl TryFrom<super::DataBlob> for BackupManifest {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn try_from(blob: super::DataBlob) -> Result<Self, Error> {
|
|
|
|
let data = blob.decode(None)
|
|
|
|
.map_err(|err| format_err!("decode backup manifest blob failed - {}", err))?;
|
|
|
|
let json: Value = serde_json::from_slice(&data[..])
|
|
|
|
.map_err(|err| format_err!("unable to parse backup manifest json - {}", err))?;
|
2020-07-09 07:48:30 +00:00
|
|
|
let manifest: BackupManifest = serde_json::from_value(json)?;
|
|
|
|
Ok(manifest)
|
2020-01-05 15:20:26 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-12 15:58:08 +00:00
|
|
|
|
2020-07-09 07:20:49 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_manifest_signature() -> Result<(), Error> {
|
|
|
|
|
|
|
|
use crate::backup::{KeyDerivationConfig};
|
|
|
|
|
|
|
|
let pw = b"test";
|
|
|
|
|
|
|
|
let kdf = KeyDerivationConfig::Scrypt {
|
|
|
|
n: 65536,
|
|
|
|
r: 8,
|
|
|
|
p: 1,
|
|
|
|
salt: Vec::new(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let testkey = kdf.derive_key(pw)?;
|
|
|
|
|
|
|
|
let crypt_config = CryptConfig::new(testkey)?;
|
|
|
|
|
|
|
|
let snapshot: BackupDir = "host/elsa/2020-06-26T13:56:05Z".parse()?;
|
|
|
|
|
|
|
|
let mut manifest = BackupManifest::new(snapshot);
|
|
|
|
|
|
|
|
manifest.add_file("test1.img.fidx".into(), 200, [1u8; 32], CryptMode::Encrypt)?;
|
|
|
|
manifest.add_file("abc.blob".into(), 200, [2u8; 32], CryptMode::None)?;
|
|
|
|
|
|
|
|
manifest.unprotected["note"] = "This is not protected by the signature.".into();
|
|
|
|
|
2020-07-09 09:28:05 +00:00
|
|
|
let text = manifest.to_string(Some(&crypt_config))?;
|
2020-07-09 07:20:49 +00:00
|
|
|
|
|
|
|
let manifest: Value = serde_json::from_str(&text)?;
|
|
|
|
let signature = manifest["signature"].as_str().unwrap().to_string();
|
|
|
|
|
|
|
|
assert_eq!(signature, "d7b446fb7db081662081d4b40fedd858a1d6307a5aff4ecff7d5bf4fd35679e9");
|
|
|
|
|
2020-07-09 07:48:30 +00:00
|
|
|
let manifest: BackupManifest = serde_json::from_value(manifest)?;
|
2020-07-09 07:20:49 +00:00
|
|
|
let expected_signature = proxmox::tools::digest_to_hex(&manifest.signature(&crypt_config)?);
|
|
|
|
|
|
|
|
assert_eq!(signature, expected_signature);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|