src/backup/data_blob_reader.rs: avoid unwrap() - return error instead

This commit is contained in:
Dietmar Maurer 2020-07-10 11:28:19 +02:00
parent d257c2ecbd
commit f58233a73a
1 changed files with 5 additions and 3 deletions

View File

@ -1,4 +1,4 @@
use anyhow::{bail, Error}; use anyhow::{bail, format_err, Error};
use std::sync::Arc; use std::sync::Arc;
use std::io::{Read, BufReader}; use std::io::{Read, BufReader};
use proxmox::tools::io::ReadExt; use proxmox::tools::io::ReadExt;
@ -40,23 +40,25 @@ impl <R: Read> DataBlobReader<R> {
Ok(Self { state: BlobReaderState::Compressed { expected_crc, decompr }}) Ok(Self { state: BlobReaderState::Compressed { expected_crc, decompr }})
} }
ENCRYPTED_BLOB_MAGIC_1_0 => { ENCRYPTED_BLOB_MAGIC_1_0 => {
let config = config.ok_or_else(|| format_err!("unable to read encrypted blob without key"))?;
let expected_crc = u32::from_le_bytes(head.crc); let expected_crc = u32::from_le_bytes(head.crc);
let mut iv = [0u8; 16]; let mut iv = [0u8; 16];
let mut expected_tag = [0u8; 16]; let mut expected_tag = [0u8; 16];
reader.read_exact(&mut iv)?; reader.read_exact(&mut iv)?;
reader.read_exact(&mut expected_tag)?; reader.read_exact(&mut expected_tag)?;
let csum_reader = ChecksumReader::new(reader, None); let csum_reader = ChecksumReader::new(reader, None);
let decrypt_reader = CryptReader::new(BufReader::with_capacity(64*1024, csum_reader), iv, expected_tag, config.unwrap())?; let decrypt_reader = CryptReader::new(BufReader::with_capacity(64*1024, csum_reader), iv, expected_tag, config)?;
Ok(Self { state: BlobReaderState::Encrypted { expected_crc, decrypt_reader }}) Ok(Self { state: BlobReaderState::Encrypted { expected_crc, decrypt_reader }})
} }
ENCR_COMPR_BLOB_MAGIC_1_0 => { ENCR_COMPR_BLOB_MAGIC_1_0 => {
let config = config.ok_or_else(|| format_err!("unable to read encrypted blob without key"))?;
let expected_crc = u32::from_le_bytes(head.crc); let expected_crc = u32::from_le_bytes(head.crc);
let mut iv = [0u8; 16]; let mut iv = [0u8; 16];
let mut expected_tag = [0u8; 16]; let mut expected_tag = [0u8; 16];
reader.read_exact(&mut iv)?; reader.read_exact(&mut iv)?;
reader.read_exact(&mut expected_tag)?; reader.read_exact(&mut expected_tag)?;
let csum_reader = ChecksumReader::new(reader, None); let csum_reader = ChecksumReader::new(reader, None);
let decrypt_reader = CryptReader::new(BufReader::with_capacity(64*1024, csum_reader), iv, expected_tag, config.unwrap())?; let decrypt_reader = CryptReader::new(BufReader::with_capacity(64*1024, csum_reader), iv, expected_tag, config)?;
let decompr = zstd::stream::read::Decoder::new(decrypt_reader)?; let decompr = zstd::stream::read::Decoder::new(decrypt_reader)?;
Ok(Self { state: BlobReaderState::EncryptedCompressed { expected_crc, decompr }}) Ok(Self { state: BlobReaderState::EncryptedCompressed { expected_crc, decompr }})
} }