mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-14 06:22:24 +00:00
* Add craft-wordmodel: WordCraft's document model without its editing code WordCraft's crates/doc (wordcraft-doc, at 07f97737) moved as it is, so the DOCX, RTF, ODT and other text-format crates can read into and write from one model. Module paths, type names and serde attributes are unchanged: an app that re-exports the crate keeps its native file format. Left in the app: range editing (edit.rs), IME text and equation editing. Not model, and already in craft-libs: code pages (craft-rtf), the ZIP entry-count check (craft-opc), the compound-file guard (craft-cfb). The paragraph walks moved to walk.rs and the unit helpers of wordcraft-geom to geom.rs. * craft-wordmodel: math_tree, the arguments of an equation node and a walk over its runs The DOCX reader and writer walk an equation's runs. The walk and the child accessors it uses were in WordCraft's equation editor (math_edit.rs), which stays in the app; they are structure, not editing. * craft-wordmodel: keep the document operations (edit.rs) with the model The DOCX and RTF readers use them: alternative-format chunks are merged into a document with adopt_blocks, and a reader formats ranges with format_range and inserts objects. They operate on the document, not on a view, so they belong beside it. The tests that drove them come back with them; the paragraph walks stay in walk.rs and edit re-exports them. base64 0.23, as WordCraft and craft-offcrypto use. * craft-docx: WordCraft's crates/docx as it is at wordcraft 07f97737 The source, unchanged, so the next commit shows only what the move changes. This commit does not build on its own (it still names wordcraft-doc and wordcraft-geom, and is not a workspace member yet). * craft-docx: on craft-wordmodel, craft-opc, craft-xml and craft-offcrypto - The package (zip reading, part names, relationships, content types, zip writing) is craft-opc's; the module keeps WordCraft's bookkeeping (passthrough keys, the embedded-part manifest) and its own Rel/Rels shape. - Parts are parsed with craft-xml's canonical names (the Strict drawing names and mc:Choice/@Requires through its hooks), then given the tree shape the reader was written against. - Encryption and Restrict Editing hashes are craft-offcrypto's; this crate maps its errors and the w:documentProtection attributes. - wordcraft-doc is craft-wordmodel; wordcraft-geom's helpers are its geom. Tests that covered what now lives in craft-opc and craft-offcrypto are left to those crates; the DIFAT regression is craft-cfb's. Test-only zip use follows zip 9's fallible names. The attribute-cap test times the read only.
118 lines
5.4 KiB
Rust
118 lines
5.4 KiB
Rust
//! Password-protected Word documents and Restrict Editing passwords, through `craft-offcrypto`
|
|
//! ([MS-OFFCRYPTO]): this module maps its results onto [`DocxError`] and the
|
|
//! `w:documentProtection` attributes.
|
|
|
|
use craft_offcrypto::protection;
|
|
|
|
use crate::DocxError;
|
|
|
|
#[cfg(test)]
|
|
use craft_offcrypto::protection::legacy_key;
|
|
pub use craft_offcrypto::{DEFAULT_SPIN_COUNT, MAX_PASSWORD_CHARS, is_encrypted};
|
|
|
|
fn map(e: craft_offcrypto::Error) -> DocxError {
|
|
match e {
|
|
craft_offcrypto::Error::PasswordRequired => DocxError::PasswordRequired,
|
|
craft_offcrypto::Error::WrongPassword => DocxError::WrongPassword,
|
|
craft_offcrypto::Error::TooLarge(m) => DocxError::Limit(m),
|
|
other => DocxError::Encryption(other.to_string()),
|
|
}
|
|
}
|
|
|
|
/// The zip package inside an encrypted Office file. Without a password (`None`) this says
|
|
/// [`DocxError::PasswordRequired`] once it knows the file is one it can decrypt; a password that
|
|
/// doesn't fit gives [`DocxError::WrongPassword`].
|
|
pub fn decrypt(bytes: &[u8], password: Option<&str>) -> Result<Vec<u8>, DocxError> {
|
|
craft_offcrypto::decrypt(bytes, password).map_err(map)
|
|
}
|
|
|
|
/// Encrypt a zip package with `password` (agile encryption: AES-256-CBC, SHA-512, Word's spin
|
|
/// count), as Word's "Encrypt with Password" does.
|
|
pub fn encrypt(package: &[u8], password: &str) -> Result<Vec<u8>, DocxError> {
|
|
craft_offcrypto::encrypt(package, password).map_err(map)
|
|
}
|
|
|
|
/// [`encrypt`] with a chosen spin count (tests use a small one; files should use the default).
|
|
pub fn encrypt_with_spin_count(package: &[u8], password: &str, spin_count: u32) -> Result<Vec<u8>, DocxError> {
|
|
craft_offcrypto::encrypt_with_spin_count(package, password, spin_count).map_err(map)
|
|
}
|
|
|
|
/// A package encrypted with the *Standard* method (Word 2007), for tests: saves use the agile
|
|
/// method ([`encrypt`]). `key_bits` is 128, 192 or 256.
|
|
#[doc(hidden)]
|
|
pub fn encrypt_standard_for_tests(package: &[u8], password: &str, key_bits: u32) -> Result<Vec<u8>, DocxError> {
|
|
craft_offcrypto::encrypt_standard(package, password, key_bits).map_err(map)
|
|
}
|
|
|
|
/// Refuse a password Word wouldn't accept: empty, or longer than [`MAX_PASSWORD_CHARS`].
|
|
pub fn check_password(password: &str) -> Result<(), DocxError> {
|
|
craft_offcrypto::check_password(password).map_err(map)
|
|
}
|
|
|
|
/// Restrict Editing's password (`w:documentProtection`, ECMA-376 Part 1 §17.15.1.29): the
|
|
/// attributes that store its hash. Protection isn't encryption: the document stays readable,
|
|
/// the password only guards turning protection off.
|
|
pub fn protection_hash(password: &str, spin_count: u32) -> Result<Vec<(String, String)>, DocxError> {
|
|
let h = protection::hash_password(password, spin_count).map_err(map)?;
|
|
Ok(vec![
|
|
("w:algorithmName".into(), h.algorithm_name.into()),
|
|
("w:hashValue".into(), h.hash_value),
|
|
("w:saltValue".into(), h.salt_value),
|
|
("w:spinCount".into(), h.spin_count.to_string()),
|
|
])
|
|
}
|
|
|
|
/// What checking a Restrict Editing password found ([`check_protection_password`]).
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub enum PasswordCheck {
|
|
/// The protection has no password: anyone may stop it.
|
|
NoPassword,
|
|
/// The password matches the stored hash.
|
|
Correct,
|
|
/// The password doesn't match.
|
|
Incorrect,
|
|
/// The hash uses an algorithm or form that can't be checked, or is damaged. The protection
|
|
/// must stay on: guessing would let any password through.
|
|
Unsupported,
|
|
}
|
|
|
|
impl From<protection::PasswordCheck> for PasswordCheck {
|
|
fn from(c: protection::PasswordCheck) -> PasswordCheck {
|
|
match c {
|
|
protection::PasswordCheck::Correct => PasswordCheck::Correct,
|
|
protection::PasswordCheck::Incorrect => PasswordCheck::Incorrect,
|
|
protection::PasswordCheck::Unsupported => PasswordCheck::Unsupported,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Does `password` match a Restrict Editing hash (attributes as [`protection_hash`] makes them, or
|
|
/// as read from a file)? Two forms are checked:
|
|
///
|
|
/// - `w:algorithmName` / `w:hashValue` / `w:saltValue` / `w:spinCount`.
|
|
/// - `w:cryptAlgorithmSid` / `w:hash` / `w:salt` / `w:cryptSpinCount` (ECMA-376 Part 4 §14.8.1),
|
|
/// with `w:cryptAlgorithmClass` `hash`, `w:cryptAlgorithmType` `typeAny` and no `w:algIdExt`.
|
|
///
|
|
/// Anything else is [`PasswordCheck::Unsupported`], never a match.
|
|
pub fn check_protection_password(attrs: &[(String, String)], password: &str) -> PasswordCheck {
|
|
let get = |k: &str| attrs.iter().find(|(n, _)| n == k).map(|(_, v)| v.trim());
|
|
if let Some(hash) = get("w:hashValue") {
|
|
let Some(alg) = get("w:algorithmName") else { return PasswordCheck::Unsupported };
|
|
return protection::check_password_hash(alg, hash, get("w:saltValue"), get("w:spinCount"), password).into();
|
|
}
|
|
let Some(hash) = get("w:hash") else {
|
|
// §17.15.1.29: without a hash no password is associated with the protection.
|
|
return PasswordCheck::NoPassword;
|
|
};
|
|
let class_ok = get("w:cryptAlgorithmClass").is_none_or(|c| c == "hash");
|
|
let type_ok = get("w:cryptAlgorithmType").is_none_or(|t| t == "typeAny");
|
|
if !class_ok || !type_ok || get("w:algIdExt").is_some() {
|
|
return PasswordCheck::Unsupported;
|
|
}
|
|
let Some(sid) = get("w:cryptAlgorithmSid").and_then(|v| v.parse::<u32>().ok()) else { return PasswordCheck::Unsupported };
|
|
protection::check_legacy_password_hash(sid, hash, get("w:salt"), get("w:cryptSpinCount"), password).into()
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests;
|