use crate::ObjectHandle;
pub(crate) mod crypt_filters;
pub(crate) mod keys;
pub(crate) mod password;
pub mod permissions;
pub(crate) mod primitives;
pub(crate) mod rc4;
pub(crate) mod standard;
pub(crate) mod state;
pub use keys::ObjectKeyAlg;
pub use password::PasswordMode;
pub use permissions::{Permissions, PermissionsConfig, PrintPermission};
pub use state::EncryptionInfo;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EncryptMethod {
V4Aes128,
V5R6Aes256,
V5R5Aes256,
V1Rc440,
V2Rc4128,
V4Rc4128,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EncryptParams {
pub method: EncryptMethod,
pub user_password: Vec<u8>,
pub owner_password: Vec<u8>,
pub permissions: PermissionsConfig,
pub encrypt_metadata: bool,
}
impl EncryptParams {
pub fn v4_aes128(
user_password: impl Into<Vec<u8>>,
owner_password: impl Into<Vec<u8>>,
) -> Self {
Self {
method: EncryptMethod::V4Aes128,
user_password: user_password.into(),
owner_password: owner_password.into(),
permissions: PermissionsConfig::default(),
encrypt_metadata: true,
}
}
pub fn v5_r6(user_password: impl Into<Vec<u8>>, owner_password: impl Into<Vec<u8>>) -> Self {
Self {
method: EncryptMethod::V5R6Aes256,
user_password: user_password.into(),
owner_password: owner_password.into(),
permissions: PermissionsConfig::default(),
encrypt_metadata: true,
}
}
pub fn v5_r5(user_password: impl Into<Vec<u8>>, owner_password: impl Into<Vec<u8>>) -> Self {
Self {
method: EncryptMethod::V5R5Aes256,
user_password: user_password.into(),
owner_password: owner_password.into(),
permissions: PermissionsConfig::default(),
encrypt_metadata: true,
}
}
pub fn rc4(
method: EncryptMethod,
user_password: impl Into<Vec<u8>>,
owner_password: impl Into<Vec<u8>>,
) -> Self {
debug_assert!(
matches!(
method,
EncryptMethod::V1Rc440 | EncryptMethod::V2Rc4128 | EncryptMethod::V4Rc4128
),
"EncryptParams::rc4 requires an RC4 method"
);
Self {
method,
user_password: user_password.into(),
owner_password: owner_password.into(),
permissions: PermissionsConfig::default(),
encrypt_metadata: true,
}
}
pub fn is_weak_rc4(&self) -> bool {
matches!(
self.method,
EncryptMethod::V1Rc440 | EncryptMethod::V2Rc4128 | EncryptMethod::V4Rc4128
)
}
pub fn is_deprecated_r5(&self) -> bool {
matches!(self.method, EncryptMethod::V5R5Aes256)
}
}
#[derive(Debug, Clone)]
pub struct CopyEncryptionSource {
pub encrypt_dict: ObjectHandle,
pub file_key: Vec<u8>,
pub id0: Vec<u8>,
pub object_key_alg: ObjectKeyAlg,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn is_deprecated_r5_only_true_for_v5_r5() {
assert!(EncryptParams::v5_r5(b"u".to_vec(), b"o".to_vec()).is_deprecated_r5());
assert!(!EncryptParams::v5_r6(b"u".to_vec(), b"o".to_vec()).is_deprecated_r5());
assert!(!EncryptParams::v4_aes128(b"u".to_vec(), b"o".to_vec()).is_deprecated_r5());
assert!(
!EncryptParams::rc4(EncryptMethod::V1Rc440, b"u".to_vec(), b"o".to_vec())
.is_deprecated_r5()
);
}
#[test]
fn weak_rc4_and_deprecated_r5_classify_disjoint_methods() {
let r5 = EncryptParams::v5_r5(b"u".to_vec(), b"o".to_vec());
assert!(r5.is_deprecated_r5() && !r5.is_weak_rc4());
let rc4 = EncryptParams::rc4(EncryptMethod::V2Rc4128, b"u".to_vec(), b"o".to_vec());
assert!(rc4.is_weak_rc4() && !rc4.is_deprecated_r5());
let r6 = EncryptParams::v5_r6(b"u".to_vec(), b"o".to_vec());
assert!(!r6.is_deprecated_r5() && !r6.is_weak_rc4());
}
}