1 use anyhow
::{bail, format_err, Error}
;
2 use std
::convert
::TryFrom
;
5 use serde_json
::{json, Value}
;
6 use ::serde
::{Deserialize, Serialize}
;
8 use crate::backup
::{BackupDir, CryptMode, CryptConfig}
;
10 pub const MANIFEST_BLOB_NAME
: &str = "index.json.blob";
11 pub const CLIENT_LOG_BLOB_NAME
: &str = "client.log.blob";
14 use serde
::{self, Deserialize, Serializer, Deserializer}
;
19 ) -> Result
<S
::Ok
, S
::Error
>
23 let s
= proxmox
::tools
::digest_to_hex(csum
);
24 serializer
.serialize_str(&s
)
27 pub fn deserialize
<'de
, D
>(
29 ) -> Result
<[u8; 32], D
::Error
>
33 let s
= String
::deserialize(deserializer
)?
;
34 proxmox
::tools
::hex_to_digest(&s
).map_err(serde
::de
::Error
::custom
)
38 fn crypt_mode_none() -> CryptMode { CryptMode::None }
39 fn empty_value() -> Value { json!({}
) }
41 #[derive(Serialize, Deserialize)]
42 #[serde(rename_all="kebab-case")]
45 #[serde(default="crypt_mode_none")] // to be compatible with < 0.8.0 backups
46 pub crypt_mode
: CryptMode
,
48 #[serde(with = "hex_csum")]
54 /// Return expected CryptMode of referenced chunks
56 /// Encrypted Indices should only reference encrypted chunks, while signed or plain indices
57 /// should only reference plain chunks.
58 pub fn chunk_crypt_mode (&self) -> CryptMode
{
59 match self.crypt_mode
{
60 CryptMode
::Encrypt
=> CryptMode
::Encrypt
,
61 CryptMode
::SignOnly
| CryptMode
::None
=> CryptMode
::None
,
66 #[derive(Serialize, Deserialize)]
67 #[serde(rename_all="kebab-case")]
68 pub struct BackupManifest
{
73 #[serde(default="empty_value")] // to be compatible with < 0.8.0 backups
74 pub unprotected
: Value
,
78 pub enum ArchiveType
{
84 pub fn archive_type
<P
: AsRef
<Path
>>(
86 ) -> Result
<ArchiveType
, Error
> {
88 let archive_name
= archive_name
.as_ref();
89 let archive_type
= match archive_name
.extension().and_then(|ext
| ext
.to_str()) {
90 Some("didx") => ArchiveType
::DynamicIndex
,
91 Some("fidx") => ArchiveType
::FixedIndex
,
92 Some("blob") => ArchiveType
::Blob
,
93 _
=> bail
!("unknown archive type: {:?}", archive_name
),
101 pub fn new(snapshot
: BackupDir
) -> Self {
103 backup_type
: snapshot
.group().backup_type().into(),
104 backup_id
: snapshot
.group().backup_id().into(),
105 backup_time
: snapshot
.backup_time().timestamp(),
107 unprotected
: json
!({}
),
111 pub fn add_file(&mut self, filename
: String
, size
: u64, csum
: [u8; 32], crypt_mode
: CryptMode
) -> Result
<(), Error
> {
112 let _archive_type
= archive_type(&filename
)?
; // check type
113 self.files
.push(FileInfo { filename, size, csum, crypt_mode }
);
117 pub fn files(&self) -> &[FileInfo
] {
121 pub fn lookup_file_info(&self, name
: &str) -> Result
<&FileInfo
, Error
> {
123 let info
= self.files
.iter().find(|item
| item
.filename
== name
);
126 None
=> bail
!("manifest does not contain file '{}'", name
),
127 Some(info
) => Ok(info
),
131 pub fn verify_file(&self, name
: &str, csum
: &[u8; 32], size
: u64) -> Result
<(), Error
> {
133 let info
= self.lookup_file_info(name
)?
;
135 if size
!= info
.size
{
136 bail
!("wrong size for file '{}' ({} != {})", name
, info
.size
, size
);
139 if csum
!= &info
.csum
{
140 bail
!("wrong checksum for file '{}'", name
);
146 // Generate cannonical json
147 fn to_canonical_json(value
: &Value
) -> Result
<Vec
<u8>, Error
> {
148 let mut data
= Vec
::new();
149 Self::write_canonical_json(value
, &mut data
)?
;
153 fn write_canonical_json(value
: &Value
, output
: &mut Vec
<u8>) -> Result
<(), Error
> {
155 Value
::Null
=> bail
!("got unexpected null value"),
156 Value
::String(_
) | Value
::Number(_
) | Value
::Bool(_
) => {
157 serde_json
::to_writer(output
, &value
)?
;
159 Value
::Array(list
) => {
161 let mut iter
= list
.iter();
162 if let Some(item
) = iter
.next() {
163 Self::write_canonical_json(item
, output
)?
;
166 Self::write_canonical_json(item
, output
)?
;
171 Value
::Object(map
) => {
173 let mut keys
: Vec
<&str> = map
.keys().map(String
::as_str
).collect();
175 let mut iter
= keys
.into_iter();
176 if let Some(key
) = iter
.next() {
177 serde_json
::to_writer(&mut *output
, &key
)?
;
179 Self::write_canonical_json(&map
[key
], output
)?
;
182 serde_json
::to_writer(&mut *output
, &key
)?
;
184 Self::write_canonical_json(&map
[key
], output
)?
;
193 /// Compute manifest signature
195 /// By generating a HMAC SHA256 over the canonical json
196 /// representation, The 'unpreotected' property is excluded.
197 pub fn signature(&self, crypt_config
: &CryptConfig
) -> Result
<[u8; 32], Error
> {
198 Self::json_signature(&serde_json
::to_value(&self)?
, crypt_config
)
201 fn json_signature(data
: &Value
, crypt_config
: &CryptConfig
) -> Result
<[u8; 32], Error
> {
203 let mut signed_data
= data
.clone();
205 signed_data
.as_object_mut().unwrap().remove("unprotected"); // exclude
206 signed_data
.as_object_mut().unwrap().remove("signature"); // exclude
208 let canonical
= Self::to_canonical_json(&signed_data
)?
;
210 let sig
= crypt_config
.compute_auth_tag(&canonical
);
215 /// Converts the Manifest into json string, and add a signature if there is a crypt_config.
216 pub fn to_string(&self, crypt_config
: Option
<&CryptConfig
>) -> Result
<String
, Error
> {
218 let mut manifest
= serde_json
::to_value(&self)?
;
220 if let Some(crypt_config
) = crypt_config
{
221 let sig
= self.signature(crypt_config
)?
;
222 manifest
["signature"] = proxmox
::tools
::digest_to_hex(&sig
).into();
225 let manifest
= serde_json
::to_string_pretty(&manifest
).unwrap().into();
229 /// Try to read the manifest. This verifies the signature if there is a crypt_config.
230 pub fn from_data(data
: &[u8], crypt_config
: Option
<&CryptConfig
>) -> Result
<BackupManifest
, Error
> {
231 let json
: Value
= serde_json
::from_slice(data
)?
;
232 let signature
= json
["signature"].as_str().map(String
::from
);
234 if let Some(ref crypt_config
) = crypt_config
{
235 if let Some(signature
) = signature
{
236 let expected_signature
= proxmox
::tools
::digest_to_hex(&Self::json_signature(&json
, crypt_config
)?
);
237 if signature
!= expected_signature
{
238 bail
!("wrong signature in manifest");
241 // not signed: warn/fail?
245 let manifest
: BackupManifest
= serde_json
::from_value(json
)?
;
251 impl TryFrom
<super::DataBlob
> for BackupManifest
{
254 fn try_from(blob
: super::DataBlob
) -> Result
<Self, Error
> {
255 // no expected digest available
256 let data
= blob
.decode(None
, None
)
257 .map_err(|err
| format_err
!("decode backup manifest blob failed - {}", err
))?
;
258 let json
: Value
= serde_json
::from_slice(&data
[..])
259 .map_err(|err
| format_err
!("unable to parse backup manifest json - {}", err
))?
;
260 let manifest
: BackupManifest
= serde_json
::from_value(json
)?
;
267 fn test_manifest_signature() -> Result
<(), Error
> {
269 use crate::backup
::{KeyDerivationConfig}
;
273 let kdf
= KeyDerivationConfig
::Scrypt
{
280 let testkey
= kdf
.derive_key(pw
)?
;
282 let crypt_config
= CryptConfig
::new(testkey
)?
;
284 let snapshot
: BackupDir
= "host/elsa/2020-06-26T13:56:05Z".parse()?
;
286 let mut manifest
= BackupManifest
::new(snapshot
);
288 manifest
.add_file("test1.img.fidx".into(), 200, [1u8; 32], CryptMode
::Encrypt
)?
;
289 manifest
.add_file("abc.blob".into(), 200, [2u8; 32], CryptMode
::None
)?
;
291 manifest
.unprotected
["note"] = "This is not protected by the signature.".into();
293 let text
= manifest
.to_string(Some(&crypt_config
))?
;
295 let manifest
: Value
= serde_json
::from_str(&text
)?
;
296 let signature
= manifest
["signature"].as_str().unwrap().to_string();
298 assert_eq
!(signature
, "d7b446fb7db081662081d4b40fedd858a1d6307a5aff4ecff7d5bf4fd35679e9");
300 let manifest
: BackupManifest
= serde_json
::from_value(manifest
)?
;
301 let expected_signature
= proxmox
::tools
::digest_to_hex(&manifest
.signature(&crypt_config
)?
);
303 assert_eq
!(signature
, expected_signature
);