1 use std
::convert
::TryFrom
;
4 use anyhow
::{bail, format_err, Error}
;
6 use serde_json
::{json, Value}
;
7 use serde
::{Deserialize, Serialize}
;
9 use crate::{BackupDir, CryptMode, CryptConfig, Fingerprint}
;
11 pub const MANIFEST_BLOB_NAME
: &str = "index.json.blob";
12 pub const MANIFEST_LOCK_NAME
: &str = ".index.json.lck";
13 pub const CLIENT_LOG_BLOB_NAME
: &str = "client.log.blob";
14 pub const ENCRYPTED_KEY_BLOB_NAME
: &str = "rsa-encrypted.key.blob";
17 use serde
::{self, Deserialize, Serializer, Deserializer}
;
22 ) -> Result
<S
::Ok
, S
::Error
>
26 let s
= proxmox
::tools
::digest_to_hex(csum
);
27 serializer
.serialize_str(&s
)
30 pub fn deserialize
<'de
, D
>(
32 ) -> Result
<[u8; 32], D
::Error
>
36 let s
= String
::deserialize(deserializer
)?
;
37 proxmox
::tools
::hex_to_digest(&s
).map_err(serde
::de
::Error
::custom
)
41 fn crypt_mode_none() -> CryptMode { CryptMode::None }
42 fn empty_value() -> Value { json!({}
) }
44 #[derive(Serialize, Deserialize)]
45 #[serde(rename_all="kebab-case")]
48 #[serde(default="crypt_mode_none")] // to be compatible with < 0.8.0 backups
49 pub crypt_mode
: CryptMode
,
51 #[serde(with = "hex_csum")]
57 /// Return expected CryptMode of referenced chunks
59 /// Encrypted Indices should only reference encrypted chunks, while signed or plain indices
60 /// should only reference plain chunks.
61 pub fn chunk_crypt_mode (&self) -> CryptMode
{
62 match self.crypt_mode
{
63 CryptMode
::Encrypt
=> CryptMode
::Encrypt
,
64 CryptMode
::SignOnly
| CryptMode
::None
=> CryptMode
::None
,
69 #[derive(Serialize, Deserialize)]
70 #[serde(rename_all="kebab-case")]
71 pub struct BackupManifest
{
76 #[serde(default="empty_value")] // to be compatible with < 0.8.0 backups
77 pub unprotected
: Value
,
78 pub signature
: Option
<String
>,
82 pub enum ArchiveType
{
89 pub fn from_path(archive_name
: impl AsRef
<Path
>) -> Result
<Self, Error
> {
90 let archive_name
= archive_name
.as_ref();
91 let archive_type
= match archive_name
.extension().and_then(|ext
| ext
.to_str()) {
92 Some("didx") => ArchiveType
::DynamicIndex
,
93 Some("fidx") => ArchiveType
::FixedIndex
,
94 Some("blob") => ArchiveType
::Blob
,
95 _
=> bail
!("unknown archive type: {:?}", archive_name
),
101 //#[deprecated(note = "use ArchivType::from_path instead")] later...
102 pub fn archive_type
<P
: AsRef
<Path
>>(
104 ) -> Result
<ArchiveType
, Error
> {
105 ArchiveType
::from_path(archive_name
)
108 impl BackupManifest
{
110 pub fn new(snapshot
: BackupDir
) -> Self {
112 backup_type
: snapshot
.group().backup_type().into(),
113 backup_id
: snapshot
.group().backup_id().into(),
114 backup_time
: snapshot
.backup_time(),
116 unprotected
: json
!({}
),
121 pub fn add_file(&mut self, filename
: String
, size
: u64, csum
: [u8; 32], crypt_mode
: CryptMode
) -> Result
<(), Error
> {
122 let _archive_type
= ArchiveType
::from_path(&filename
)?
; // check type
123 self.files
.push(FileInfo { filename, size, csum, crypt_mode }
);
127 pub fn files(&self) -> &[FileInfo
] {
131 pub fn lookup_file_info(&self, name
: &str) -> Result
<&FileInfo
, Error
> {
133 let info
= self.files
.iter().find(|item
| item
.filename
== name
);
136 None
=> bail
!("manifest does not contain file '{}'", name
),
137 Some(info
) => Ok(info
),
141 pub fn verify_file(&self, name
: &str, csum
: &[u8; 32], size
: u64) -> Result
<(), Error
> {
143 let info
= self.lookup_file_info(name
)?
;
145 if size
!= info
.size
{
146 bail
!("wrong size for file '{}' ({} != {})", name
, info
.size
, size
);
149 if csum
!= &info
.csum
{
150 bail
!("wrong checksum for file '{}'", name
);
156 // Generate canonical json
157 fn to_canonical_json(value
: &Value
) -> Result
<Vec
<u8>, Error
> {
158 pbs_tools
::json
::to_canonical_json(value
)
161 /// Compute manifest signature
163 /// By generating a HMAC SHA256 over the canonical json
164 /// representation, The 'unpreotected' property is excluded.
165 pub fn signature(&self, crypt_config
: &CryptConfig
) -> Result
<[u8; 32], Error
> {
166 Self::json_signature(&serde_json
::to_value(&self)?
, crypt_config
)
169 fn json_signature(data
: &Value
, crypt_config
: &CryptConfig
) -> Result
<[u8; 32], Error
> {
171 let mut signed_data
= data
.clone();
173 signed_data
.as_object_mut().unwrap().remove("unprotected"); // exclude
174 signed_data
.as_object_mut().unwrap().remove("signature"); // exclude
176 let canonical
= Self::to_canonical_json(&signed_data
)?
;
178 let sig
= crypt_config
.compute_auth_tag(&canonical
);
183 /// Converts the Manifest into json string, and add a signature if there is a crypt_config.
184 pub fn to_string(&self, crypt_config
: Option
<&CryptConfig
>) -> Result
<String
, Error
> {
186 let mut manifest
= serde_json
::to_value(&self)?
;
188 if let Some(crypt_config
) = crypt_config
{
189 let sig
= self.signature(crypt_config
)?
;
190 manifest
["signature"] = proxmox
::tools
::digest_to_hex(&sig
).into();
191 let fingerprint
= &crypt_config
.fingerprint();
192 manifest
["unprotected"]["key-fingerprint"] = serde_json
::to_value(fingerprint
)?
;
195 let manifest
= serde_json
::to_string_pretty(&manifest
).unwrap();
199 pub fn fingerprint(&self) -> Result
<Option
<Fingerprint
>, Error
> {
200 match &self.unprotected
["key-fingerprint"] {
201 Value
::Null
=> Ok(None
),
202 value
=> Ok(Some(serde_json
::from_value(value
.clone())?
))
206 /// Checks if a BackupManifest and a CryptConfig share a valid fingerprint combination.
208 /// An unsigned manifest is valid with any or no CryptConfig.
209 /// A signed manifest is only valid with a matching CryptConfig.
210 pub fn check_fingerprint(&self, crypt_config
: Option
<&CryptConfig
>) -> Result
<(), Error
> {
211 if let Some(fingerprint
) = self.fingerprint()?
{
214 "missing key - manifest was created with key {}",
217 Some(crypt_config
) => {
218 let config_fp
= crypt_config
.fingerprint();
219 if config_fp
!= fingerprint
{
221 "wrong key - manifest's key {} does not match provided key {}",
233 /// Try to read the manifest. This verifies the signature if there is a crypt_config.
234 pub fn from_data(data
: &[u8], crypt_config
: Option
<&CryptConfig
>) -> Result
<BackupManifest
, Error
> {
235 let json
: Value
= serde_json
::from_slice(data
)?
;
236 let signature
= json
["signature"].as_str().map(String
::from
);
238 if let Some(ref crypt_config
) = crypt_config
{
239 if let Some(signature
) = signature
{
240 let expected_signature
= proxmox
::tools
::digest_to_hex(&Self::json_signature(&json
, crypt_config
)?
);
242 let fingerprint
= &json
["unprotected"]["key-fingerprint"];
243 if fingerprint
!= &Value
::Null
{
244 let fingerprint
= serde_json
::from_value(fingerprint
.clone())?
;
245 let config_fp
= crypt_config
.fingerprint();
246 if config_fp
!= fingerprint
{
248 "wrong key - unable to verify signature since manifest's key {} does not match provided key {}",
254 if signature
!= expected_signature
{
255 bail
!("wrong signature in manifest");
258 // not signed: warn/fail?
262 let manifest
: BackupManifest
= serde_json
::from_value(json
)?
;
268 impl TryFrom
<super::DataBlob
> for BackupManifest
{
271 fn try_from(blob
: super::DataBlob
) -> Result
<Self, Error
> {
272 // no expected digest available
273 let data
= blob
.decode(None
, None
)
274 .map_err(|err
| format_err
!("decode backup manifest blob failed - {}", err
))?
;
275 let json
: Value
= serde_json
::from_slice(&data
[..])
276 .map_err(|err
| format_err
!("unable to parse backup manifest json - {}", err
))?
;
277 let manifest
: BackupManifest
= serde_json
::from_value(json
)?
;
284 fn test_manifest_signature() -> Result
<(), Error
> {
286 use crate::{KeyDerivationConfig}
;
290 let kdf
= KeyDerivationConfig
::Scrypt
{
297 let testkey
= kdf
.derive_key(pw
)?
;
299 let crypt_config
= CryptConfig
::new(testkey
)?
;
301 let snapshot
: BackupDir
= "host/elsa/2020-06-26T13:56:05Z".parse()?
;
303 let mut manifest
= BackupManifest
::new(snapshot
);
305 manifest
.add_file("test1.img.fidx".into(), 200, [1u8; 32], CryptMode
::Encrypt
)?
;
306 manifest
.add_file("abc.blob".into(), 200, [2u8; 32], CryptMode
::None
)?
;
308 manifest
.unprotected
["note"] = "This is not protected by the signature.".into();
310 let text
= manifest
.to_string(Some(&crypt_config
))?
;
312 let manifest
: Value
= serde_json
::from_str(&text
)?
;
313 let signature
= manifest
["signature"].as_str().unwrap().to_string();
315 assert_eq
!(signature
, "d7b446fb7db081662081d4b40fedd858a1d6307a5aff4ecff7d5bf4fd35679e9");
317 let manifest
: BackupManifest
= serde_json
::from_value(manifest
)?
;
318 let expected_signature
= proxmox
::tools
::digest_to_hex(&manifest
.signature(&crypt_config
)?
);
320 assert_eq
!(signature
, expected_signature
);