mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-11-17 00:04:18 +00:00
fix(crypt): write RC4 fixtures through the legacy entry points
Pinning craft-pdf to the #1852 merge SHA also picked up d74e60b2, which makes create() and set_encryption() refuse RC4-40 and RC4-128. Tests, helpers and the protect example that still write those schemes now use create_legacy and set_encryption_legacy. Edit::Protect does the same when the chosen algorithm is legacy, so a file that must stay readable by a reader without AES can still be written.
This commit is contained in:
1 parent
99ef606491
commit
3d43364928
15 files changed
+67
-57
No files matched your search
@@ -46,7 +46,7 @@ fn protected(algorithm: Algorithm) -> Vec<u8> {
|
||||
|
||||
fn protected_with(algorithm: Algorithm, content: &str) -> Vec<u8> {
|
||||
let mut doc = pdfcraft_cos::Document::open(Arc::new(fixture(content))).unwrap();
|
||||
doc.set_encryption(&NewEncryption {
|
||||
doc.set_encryption_legacy(&NewEncryption {
|
||||
algorithm,
|
||||
user_password: "user",
|
||||
owner_password: "owner",
|
||||
|
||||
@@ -22,7 +22,8 @@ fn main() {
|
||||
_ => pdfcraft_cos::Algorithm::Aes256,
|
||||
};
|
||||
let mut doc = pdfcraft_cos::Document::open(std::sync::Arc::new(std::fs::read(input).unwrap())).unwrap();
|
||||
doc.set_encryption(&pdfcraft_cos::NewEncryption {
|
||||
// `rc4-40` and `rc4-128` are the legacy schemes, which `set_encryption` refuses.
|
||||
doc.set_encryption_legacy(&pdfcraft_cos::NewEncryption {
|
||||
algorithm,
|
||||
user_password: user,
|
||||
owner_password: owner,
|
||||
|
||||
@@ -122,7 +122,7 @@ fn caller_entropy_makes_the_new_identifier_reproducible() {
|
||||
fn an_encrypted_rewrite_keeps_the_first_identifier_string_its_key_depends_on() {
|
||||
for algorithm in [Algorithm::Rc4_128, Algorithm::Aes128, Algorithm::Aes256] {
|
||||
let mut doc = reopen(source(true));
|
||||
doc.set_encryption(&NewEncryption {
|
||||
doc.set_encryption_legacy(&NewEncryption {
|
||||
algorithm,
|
||||
user_password: "user",
|
||||
owner_password: "owner",
|
||||
@@ -160,7 +160,7 @@ fn unusable_ids() -> Vec<(&'static str, Option<Object>)> {
|
||||
/// An encrypted file whose key is derived from the empty string, with its trailer `/ID` replaced by `id`.
|
||||
fn protected_with_id(algorithm: Algorithm, id: Option<Object>) -> Vec<u8> {
|
||||
let mut doc = reopen(source_with(Some("/ID [<> <>]")));
|
||||
doc.set_encryption(&NewEncryption {
|
||||
doc.set_encryption_legacy(&NewEncryption {
|
||||
algorithm,
|
||||
user_password: "user",
|
||||
owner_password: "owner",
|
||||
|
||||
@@ -139,7 +139,7 @@ fn full_rewrites_preserve_graph_across_multiple_object_streams_and_evictions() {
|
||||
}
|
||||
|
||||
fn protect(doc: &mut Document, algorithm: Algorithm) {
|
||||
doc.set_encryption(&NewEncryption {
|
||||
doc.set_encryption_legacy(&NewEncryption {
|
||||
algorithm,
|
||||
user_password: "user",
|
||||
owner_password: "owner",
|
||||
|
||||
@@ -41,7 +41,7 @@ fn fixture_with(extra: &str) -> Vec<u8> {
|
||||
|
||||
fn protect(alg: Algorithm, user: &str, owner: &str, permissions: i32) -> Vec<u8> {
|
||||
let mut doc = Document::open(Arc::new(fixture())).unwrap();
|
||||
doc.set_encryption(&NewEncryption {
|
||||
doc.set_encryption_legacy(&NewEncryption {
|
||||
algorithm: alg,
|
||||
user_password: user,
|
||||
owner_password: owner,
|
||||
@@ -160,7 +160,7 @@ fn changing_and_removing_passwords() {
|
||||
let bytes = protect(Algorithm::Rc4_128, "old", "old-owner", -1);
|
||||
// Change: decrypt with the old handler, encrypt with the new one.
|
||||
let mut doc = Document::open_with_password(Arc::new(bytes), Some("old-owner")).unwrap();
|
||||
doc.set_encryption(&NewEncryption {
|
||||
doc.set_encryption_legacy(&NewEncryption {
|
||||
algorithm: Algorithm::Aes256,
|
||||
user_password: "new",
|
||||
owner_password: "new-owner",
|
||||
@@ -188,7 +188,7 @@ fn changing_and_removing_passwords() {
|
||||
#[test]
|
||||
fn unencrypted_metadata_is_left_readable() {
|
||||
let mut doc = Document::open(Arc::new(fixture())).unwrap();
|
||||
doc.set_encryption(&NewEncryption {
|
||||
doc.set_encryption_legacy(&NewEncryption {
|
||||
algorithm: Algorithm::Aes256,
|
||||
user_password: "",
|
||||
owner_password: "o",
|
||||
@@ -287,7 +287,7 @@ fn signature_contents_stay_unencrypted() {
|
||||
c.set(b"Test".to_vec(), Object::Array(vec![Object::Ref(sig), Object::Ref(untyped), Object::Ref(note)]))
|
||||
})
|
||||
.unwrap();
|
||||
doc.set_encryption(&NewEncryption {
|
||||
doc.set_encryption_legacy(&NewEncryption {
|
||||
algorithm: alg,
|
||||
user_password: "",
|
||||
owner_password: "owner",
|
||||
@@ -328,7 +328,7 @@ fn a_byte_range_does_not_exempt_an_annotation() {
|
||||
note.set(b"ByteRange".to_vec(), Object::Array(Vec::new()));
|
||||
let note = doc.add(Object::Dict(note));
|
||||
doc.update_dict(doc.root().unwrap(), |c| c.set(b"Test".to_vec(), Object::Ref(note))).unwrap();
|
||||
doc.set_encryption(&NewEncryption {
|
||||
doc.set_encryption_legacy(&NewEncryption {
|
||||
algorithm: Algorithm::Aes256,
|
||||
user_password: "",
|
||||
owner_password: "owner",
|
||||
@@ -353,7 +353,7 @@ fn protecting_drops_inherited_crypt_filter_selectors() {
|
||||
for selector in [" /Filter /Crypt /DecodeParms << /Name /MyCF >>", " /Filter [/Crypt] /DecodeParms [<< /Name /Identity >>]", " /Filter /Crypt"] {
|
||||
let mut doc = Document::open(Arc::new(fixture_with(selector))).unwrap();
|
||||
assert_eq!(content(&doc), SECRET, "{selector}");
|
||||
doc.set_encryption(&NewEncryption {
|
||||
doc.set_encryption_legacy(&NewEncryption {
|
||||
algorithm: Algorithm::Aes256,
|
||||
user_password: "",
|
||||
owner_password: "owner",
|
||||
@@ -423,7 +423,7 @@ fn an_earlier_revision_opens_with_the_documents_own_credentials() {
|
||||
// Another file, encrypted with other keys: the document's key is no use, and it says so.
|
||||
let other = {
|
||||
let mut doc = Document::open(Arc::new(fixture())).unwrap();
|
||||
doc.set_encryption(&NewEncryption {
|
||||
doc.set_encryption_legacy(&NewEncryption {
|
||||
algorithm: alg,
|
||||
user_password: "user",
|
||||
owner_password: "owner",
|
||||
@@ -444,7 +444,7 @@ fn an_earlier_revision_opens_with_the_documents_own_credentials() {
|
||||
/// instead of as an indirect object.
|
||||
fn protect_inline(alg: Algorithm, user: &str, owner: &str) -> Vec<u8> {
|
||||
let mut doc = Document::open(Arc::new(fixture())).unwrap();
|
||||
doc.set_encryption(&NewEncryption {
|
||||
doc.set_encryption_legacy(&NewEncryption {
|
||||
algorithm: alg,
|
||||
user_password: user,
|
||||
owner_password: owner,
|
||||
@@ -482,7 +482,7 @@ fn removing_an_inline_encrypt_dictionary_leaves_an_unencrypted_file() {
|
||||
#[test]
|
||||
fn a_stream_labelled_xref_is_encrypted_when_saved() {
|
||||
let mut doc = Document::open(Arc::new(fixture_with(" /Type /XRef"))).unwrap();
|
||||
doc.set_encryption(&NewEncryption {
|
||||
doc.set_encryption_legacy(&NewEncryption {
|
||||
algorithm: Algorithm::Aes256,
|
||||
user_password: "u",
|
||||
owner_password: "o",
|
||||
@@ -566,7 +566,7 @@ fn holds(bytes: &[u8], text: &str) -> bool {
|
||||
#[test]
|
||||
fn an_edited_object_that_encrypt_names_is_encrypted() {
|
||||
let mut doc = Document::open(Arc::new(fixture())).unwrap();
|
||||
doc.set_encryption(&NewEncryption {
|
||||
doc.set_encryption_legacy(&NewEncryption {
|
||||
algorithm: Algorithm::Aes128,
|
||||
user_password: "",
|
||||
owner_password: "owner",
|
||||
@@ -686,7 +686,7 @@ fn an_identity_crypt_filter_selected_by_reference_leaves_a_stream_alone() {
|
||||
#[test]
|
||||
fn protecting_drops_a_crypt_filter_selected_by_reference() {
|
||||
let protected = |doc: &mut Document| {
|
||||
doc.set_encryption(&NewEncryption {
|
||||
doc.set_encryption_legacy(&NewEncryption {
|
||||
algorithm: Algorithm::Aes256,
|
||||
user_password: "",
|
||||
owner_password: "owner",
|
||||
@@ -761,7 +761,7 @@ fn protecting_copies_no_large_object_into_every_stream() {
|
||||
streams.push(Object::Ref(doc.add(Object::Stream(pdfcraft_cos::Stream::from_raw(by_filter, b"41>".to_vec())))));
|
||||
}
|
||||
doc.update_dict(doc.root().unwrap(), |c| c.set(b"Test".to_vec(), Object::Array(streams))).unwrap();
|
||||
doc.set_encryption(&NewEncryption {
|
||||
doc.set_encryption_legacy(&NewEncryption {
|
||||
algorithm: Algorithm::Aes256,
|
||||
user_password: "",
|
||||
owner_password: "owner",
|
||||
@@ -779,7 +779,7 @@ fn protecting_copies_no_large_object_into_every_stream() {
|
||||
#[test]
|
||||
fn a_file_identifier_of_many_references_is_two_strings() {
|
||||
let mut doc = Document::open(Arc::new(fixture())).unwrap();
|
||||
doc.set_encryption(&NewEncryption {
|
||||
doc.set_encryption_legacy(&NewEncryption {
|
||||
algorithm: Algorithm::Aes128,
|
||||
user_password: "",
|
||||
owner_password: "owner",
|
||||
@@ -857,7 +857,7 @@ fn protecting_keeps_the_parameters_of_the_filter_before_a_crypt_filter() {
|
||||
let mut parms = pdfcraft_cos::Dict::new();
|
||||
parms.set(b"Name".to_vec(), Object::name("MyCF"));
|
||||
select(&mut doc, (Object::Array(filters), Object::Dict(parms)));
|
||||
doc.set_encryption(&NewEncryption {
|
||||
doc.set_encryption_legacy(&NewEncryption {
|
||||
algorithm: Algorithm::Aes256,
|
||||
user_password: "",
|
||||
owner_password: "owner",
|
||||
@@ -883,7 +883,7 @@ fn protecting_leaves_a_parameter_array_too_short_for_the_crypt_filter() {
|
||||
let short = Object::Array(vec![Object::Null]);
|
||||
let parms = if by_reference { Object::Ref(doc.add(short)) } else { short };
|
||||
select(&mut doc, (Object::Array(vec![Object::name("ASCIIHexDecode"), Object::name("Crypt")]), parms));
|
||||
doc.set_encryption(&NewEncryption {
|
||||
doc.set_encryption_legacy(&NewEncryption {
|
||||
algorithm: Algorithm::Aes256,
|
||||
user_password: "",
|
||||
owner_password: "owner",
|
||||
@@ -930,7 +930,7 @@ fn a_stream_type_given_by_reference_picks_its_crypt_filter() {
|
||||
let Object::Stream(mut s) = doc.get(meta).as_ref().clone() else { panic!("no metadata stream") };
|
||||
s.dict.set(b"Type".to_vec(), Object::Ref(ty));
|
||||
doc.set(meta, Object::Stream(s));
|
||||
doc.set_encryption(&NewEncryption {
|
||||
doc.set_encryption_legacy(&NewEncryption {
|
||||
algorithm: Algorithm::Aes256,
|
||||
user_password: "",
|
||||
owner_password: "o",
|
||||
@@ -992,7 +992,7 @@ fn a_signature_dictionary_is_known_in_every_form() {
|
||||
other.set(b"Contents".to_vec(), Object::String(PdfString { bytes: vec![0xAB, 0xCD], hex: true }));
|
||||
signatures.push(Object::Ref(doc.add(Object::Dict(other))));
|
||||
doc.update_dict(doc.root().unwrap(), |c| c.set(b"Test".to_vec(), Object::Array(signatures))).unwrap();
|
||||
doc.set_encryption(&NewEncryption {
|
||||
doc.set_encryption_legacy(&NewEncryption {
|
||||
algorithm: Algorithm::Aes256,
|
||||
user_password: "",
|
||||
owner_password: "owner",
|
||||
@@ -1021,7 +1021,7 @@ struct Classic {
|
||||
|
||||
fn classic_file() -> Classic {
|
||||
let mut doc = Document::open(Arc::new(fixture())).unwrap();
|
||||
doc.set_encryption(&NewEncryption {
|
||||
doc.set_encryption_legacy(&NewEncryption {
|
||||
algorithm: Algorithm::Aes128,
|
||||
user_password: "",
|
||||
owner_password: "owner",
|
||||
@@ -1256,7 +1256,7 @@ fn a_file_identifier_given_by_reference_is_read() {
|
||||
fn a_direct_encrypt_dictionary_keeps_what_it_refers_to() {
|
||||
for object_streams in [false, true] {
|
||||
let mut doc = Document::open(Arc::new(fixture())).unwrap();
|
||||
doc.set_encryption(&NewEncryption {
|
||||
doc.set_encryption_legacy(&NewEncryption {
|
||||
algorithm: Algorithm::Aes128,
|
||||
user_password: "",
|
||||
owner_password: "owner",
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
//!
|
||||
//! Bit n has the mask `1 << (n - 1)`. Readers use bits 3 to 6 and 9 to 12 and ignore the rest.
|
||||
|
||||
use pdfcraft_crypt::{Algorithm, Auth, NewEncryption, Permissions, SecurityHandler, create};
|
||||
use pdfcraft_crypt::{Algorithm, Auth, NewEncryption, Permissions, SecurityHandler, create_legacy};
|
||||
|
||||
const ID: &[u8] = b"0123456789abcdef";
|
||||
const T: bool = true;
|
||||
@@ -18,7 +18,7 @@ fn cells(p: &Permissions) -> [bool; 8] {
|
||||
/// Creates a file with `flags` at revision `revision`, opens it with the user password, and
|
||||
/// returns the user's permissions.
|
||||
fn user_view(alg: Algorithm, revision: i64, flags: i32) -> Permissions {
|
||||
let made = create(
|
||||
let made = create_legacy(
|
||||
&NewEncryption { algorithm: alg, user_password: "user", owner_password: "owner", permissions: flags, encrypt_metadata: true, seed: [42; 32] },
|
||||
ID,
|
||||
)
|
||||
@@ -56,7 +56,7 @@ fn revision_three_rows_hold_through_a_created_and_reopened_file() {
|
||||
|
||||
#[test]
|
||||
fn owner_access_gives_every_flag() {
|
||||
let made = create(
|
||||
let made = create_legacy(
|
||||
&NewEncryption {
|
||||
algorithm: Algorithm::Rc4_128,
|
||||
user_password: "user",
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
|
||||
use pdfcraft_crypt::{
|
||||
Algorithm, Auth, CryptError, Method, NewEncryption, PADDING, SecurityHandler, StreamKind, aes_cbc_decrypt, aes_cbc_encrypt, create,
|
||||
effective_password, pdfdoc_byte, rc4,
|
||||
create_legacy, effective_password, pdfdoc_byte, rc4,
|
||||
};
|
||||
use proptest::prelude::*;
|
||||
|
||||
@@ -92,8 +92,11 @@ fn expected_auth(algorithm: Algorithm, user_target: &[u8], owner_target: &[u8],
|
||||
}
|
||||
|
||||
fn make(algorithm: Algorithm, user: &str, owner: &str) -> SecurityHandler {
|
||||
create(&NewEncryption { algorithm, user_password: user, owner_password: owner, permissions: -1, encrypt_metadata: true, seed: [0x5e; 32] }, ID)
|
||||
.unwrap()
|
||||
create_legacy(
|
||||
&NewEncryption { algorithm, user_password: user, owner_password: owner, permissions: -1, encrypt_metadata: true, seed: [0x5e; 32] },
|
||||
ID,
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
proptest! {
|
||||
@@ -457,7 +460,7 @@ proptest! {
|
||||
}
|
||||
|
||||
// Revisions below 4 always encrypt metadata, whatever the caller asked for.
|
||||
let legacy = create(&NewEncryption { algorithm: Algorithm::Rc4_128, user_password: "user", owner_password: "owner", permissions: -1, encrypt_metadata: false, seed: [0x5e; 32] }, ID).unwrap();
|
||||
let legacy = create_legacy(&NewEncryption { algorithm: Algorithm::Rc4_128, user_password: "user", owner_password: "owner", permissions: -1, encrypt_metadata: false, seed: [0x5e; 32] }, ID).unwrap();
|
||||
prop_assert!(legacy.dict().encrypt_metadata && legacy.encrypts_metadata(), "V<4 always encrypts metadata, as create() documents");
|
||||
}
|
||||
|
||||
|
||||
@@ -1125,7 +1125,13 @@ pub(crate) fn run_edit(doc: &mut pdfcraft_cos::Document, edit: &Edit, cx: &mut E
|
||||
encrypt_metadata: p.encrypt_metadata,
|
||||
seed,
|
||||
};
|
||||
doc.set_encryption(¶ms).map_err(|e| EditError::Protection(e.to_string()))?;
|
||||
// RC4-40 and RC4-128 are refused by `set_encryption` (RFC 7465); the legacy
|
||||
// entry point is only for a file that must stay readable by a reader without AES.
|
||||
if params.algorithm.is_legacy() {
|
||||
doc.set_encryption_legacy(¶ms).map_err(|e| EditError::Protection(e.to_string()))?;
|
||||
} else {
|
||||
doc.set_encryption(¶ms).map_err(|e| EditError::Protection(e.to_string()))?;
|
||||
}
|
||||
}
|
||||
Edit::RemoveProtection => {
|
||||
if doc.output_handler().is_none() {
|
||||
|
||||
+16
-16
@@ -1608,7 +1608,7 @@ fn files_for_combine_are_made_into_pdfs_and_pdfs_pass_through() {
|
||||
|
||||
fn protected(user: &str, owner: &str, permissions: i32) -> Arc<Vec<u8>> {
|
||||
let mut doc = pdfcraft_cos::Document::open(Arc::new(fixture(2))).unwrap();
|
||||
doc.set_encryption(&pdfcraft_cos::NewEncryption {
|
||||
doc.set_encryption_legacy(&pdfcraft_cos::NewEncryption {
|
||||
algorithm: pdfcraft_cos::Algorithm::Aes256,
|
||||
user_password: user,
|
||||
owner_password: owner,
|
||||
@@ -1631,7 +1631,7 @@ fn protected_with_changes(algorithm: pdfcraft_cos::Algorithm, changes: Changes)
|
||||
fn protected_pages(pages: usize, algorithm: pdfcraft_cos::Algorithm, changes: Changes, seed: u8) -> Arc<Vec<u8>> {
|
||||
let mut doc = pdfcraft_cos::Document::open(Arc::new(fixture(pages))).unwrap();
|
||||
let protection = Protection { permissions_password: Some("boss".into()), changes, algorithm, ..Protection::default() };
|
||||
doc.set_encryption(&pdfcraft_cos::NewEncryption {
|
||||
doc.set_encryption_legacy(&pdfcraft_cos::NewEncryption {
|
||||
algorithm,
|
||||
user_password: "",
|
||||
owner_password: "boss",
|
||||
@@ -1796,7 +1796,7 @@ fn pages_under_the_same_protection_are_inserted_without_page_extraction() {
|
||||
fn page_extraction_needs_the_copy_permission_on_every_revision() {
|
||||
for algorithm in [pdfcraft_cos::Algorithm::Rc4_40, pdfcraft_cos::Algorithm::Aes256] {
|
||||
let mut doc = pdfcraft_cos::Document::open(Arc::new(fixture(2))).unwrap();
|
||||
doc.set_encryption(&pdfcraft_cos::NewEncryption {
|
||||
doc.set_encryption_legacy(&pdfcraft_cos::NewEncryption {
|
||||
algorithm,
|
||||
user_password: "",
|
||||
owner_password: "boss",
|
||||
@@ -1992,7 +1992,7 @@ fn print_content_pdf() -> Vec<u8> {
|
||||
#[test]
|
||||
fn low_resolution_printing_prints_only_the_chosen_content() {
|
||||
let mut cos = pdfcraft_cos::Document::open(Arc::new(print_content_pdf())).unwrap();
|
||||
cos.set_encryption(&pdfcraft_cos::NewEncryption {
|
||||
cos.set_encryption_legacy(&pdfcraft_cos::NewEncryption {
|
||||
algorithm: pdfcraft_cos::Algorithm::Aes256,
|
||||
user_password: "",
|
||||
owner_password: "owner",
|
||||
@@ -2148,7 +2148,7 @@ fn combining_protected_files_is_refused_clearly() {
|
||||
fn owner_password_of_older_revisions_opens_the_viewer_too() {
|
||||
for alg in [pdfcraft_cos::Algorithm::Rc4_128, pdfcraft_cos::Algorithm::Aes128] {
|
||||
let mut doc = pdfcraft_cos::Document::open(Arc::new(fixture(1))).unwrap();
|
||||
doc.set_encryption(&pdfcraft_cos::NewEncryption {
|
||||
doc.set_encryption_legacy(&pdfcraft_cos::NewEncryption {
|
||||
algorithm: alg,
|
||||
user_password: "u",
|
||||
owner_password: "o",
|
||||
@@ -2172,7 +2172,7 @@ fn owner_password_of_older_revisions_opens_the_viewer_too() {
|
||||
fn render_password_for_recovers_the_user_password_of_older_revisions() {
|
||||
for alg in [pdfcraft_cos::Algorithm::Rc4_40, pdfcraft_cos::Algorithm::Rc4_128, pdfcraft_cos::Algorithm::Aes128, pdfcraft_cos::Algorithm::Aes256] {
|
||||
let mut doc = pdfcraft_cos::Document::open(Arc::new(fixture(1))).unwrap();
|
||||
doc.set_encryption(&pdfcraft_cos::NewEncryption {
|
||||
doc.set_encryption_legacy(&pdfcraft_cos::NewEncryption {
|
||||
algorithm: alg,
|
||||
user_password: "u",
|
||||
owner_password: "o",
|
||||
@@ -2431,7 +2431,7 @@ fn bookmarks_from_text_styles_refuse_one_style_for_two_levels() {
|
||||
#[test]
|
||||
fn bookmarks_from_text_styles_need_copying_permission() {
|
||||
let mut doc = pdfcraft_cos::Document::open(Arc::new(styled_headings_fixture())).unwrap();
|
||||
doc.set_encryption(&pdfcraft_cos::NewEncryption {
|
||||
doc.set_encryption_legacy(&pdfcraft_cos::NewEncryption {
|
||||
algorithm: pdfcraft_cos::Algorithm::Aes256,
|
||||
user_password: "",
|
||||
owner_password: "owner",
|
||||
@@ -2586,7 +2586,7 @@ fn converting_colours_of_comments_needs_the_comment_permission() {
|
||||
encrypt_metadata: true,
|
||||
seed: [7; 32],
|
||||
};
|
||||
cos.set_encryption(¶ms).unwrap();
|
||||
cos.set_encryption_legacy(¶ms).unwrap();
|
||||
let bytes = pdfcraft_cos::write_full(&cos, &pdfcraft_cos::SaveOptions::default()).unwrap();
|
||||
let mut s = Session::new();
|
||||
let id = s.open("locked.pdf", None, Arc::new(bytes), None).unwrap();
|
||||
@@ -2608,7 +2608,7 @@ fn comment_permission_is_enforced() {
|
||||
encrypt_metadata: true,
|
||||
seed: [7; 32],
|
||||
};
|
||||
cos.set_encryption(¶ms).unwrap();
|
||||
cos.set_encryption_legacy(¶ms).unwrap();
|
||||
let bytes = pdfcraft_cos::write_full(&cos, &pdfcraft_cos::SaveOptions::default()).unwrap();
|
||||
let mut s = Session::new();
|
||||
let id = s.open("locked.pdf", None, Arc::new(bytes), None).unwrap();
|
||||
@@ -5636,7 +5636,7 @@ fn signed_documents_round_trip_behind_a_user_password() {
|
||||
let digital_id = sign::pkcs12::open(&p12, "test").unwrap();
|
||||
// After the test certificate's start date (2026-10-02).
|
||||
let mut doc = pdfcraft_cos::Document::open(Arc::new(fixture(1))).unwrap();
|
||||
doc.set_encryption(&pdfcraft_cos::NewEncryption {
|
||||
doc.set_encryption_legacy(&pdfcraft_cos::NewEncryption {
|
||||
algorithm: pdfcraft_cos::Algorithm::Aes128,
|
||||
user_password: "user",
|
||||
owner_password: "owner",
|
||||
@@ -5960,7 +5960,7 @@ fn real_layered_signatures_round_trip_behind_a_user_password() {
|
||||
let anchor = id.chain.first().cloned().unwrap_or_else(|| id.certificate.clone());
|
||||
let date = "D:20261002120000+01'00'";
|
||||
let mut doc = pdfcraft_cos::Document::open(Arc::new(layered_signable_fixture())).unwrap();
|
||||
doc.set_encryption(&pdfcraft_cos::NewEncryption {
|
||||
doc.set_encryption_legacy(&pdfcraft_cos::NewEncryption {
|
||||
algorithm: pdfcraft_cos::Algorithm::Aes128,
|
||||
user_password: "user",
|
||||
owner_password: "owner",
|
||||
@@ -6123,7 +6123,7 @@ fn named_layer_signatures_show_their_state_through_trust_save_and_reopen() {
|
||||
fn named_layer_signatures_round_trip_behind_a_user_password() {
|
||||
let (id, anchor) = test_identity();
|
||||
let mut doc = pdfcraft_cos::Document::open(Arc::new(named_layer_signable_fixture())).unwrap();
|
||||
doc.set_encryption(&pdfcraft_cos::NewEncryption {
|
||||
doc.set_encryption_legacy(&pdfcraft_cos::NewEncryption {
|
||||
algorithm: pdfcraft_cos::Algorithm::Aes128,
|
||||
user_password: "user",
|
||||
owner_password: "owner",
|
||||
@@ -6914,7 +6914,7 @@ fn javascript_invalid_field_colour_array_is_refused_without_partial_changes() {
|
||||
fn javascript_field_colours_respect_document_fill_permissions() {
|
||||
let mut plain = pdfcraft_cos::Document::open(Arc::new(scripted_form())).unwrap();
|
||||
plain
|
||||
.set_encryption(&pdfcraft_cos::NewEncryption {
|
||||
.set_encryption_legacy(&pdfcraft_cos::NewEncryption {
|
||||
algorithm: pdfcraft_cos::Algorithm::Aes256,
|
||||
user_password: "",
|
||||
owner_password: "owner",
|
||||
@@ -10408,7 +10408,7 @@ fn legacy_passwords_with_turkish_characters_open() {
|
||||
encrypt_metadata: true,
|
||||
seed: [7; 32],
|
||||
};
|
||||
cos.set_encryption(¶ms).unwrap();
|
||||
cos.set_encryption_legacy(¶ms).unwrap();
|
||||
let bytes = Arc::new(pdfcraft_cos::write_full(&cos, &pdfcraft_cos::SaveOptions::default()).unwrap());
|
||||
let mut s = Session::new();
|
||||
// The renderer used to try only the UTF-8 bytes, and the owner fallback handed it the
|
||||
@@ -10472,7 +10472,7 @@ fn aes256_files_whose_passwords_saslprep_changes_open_as_typed() {
|
||||
encrypt_metadata: true,
|
||||
seed: [7; 32],
|
||||
};
|
||||
cos.set_encryption(¶ms).unwrap();
|
||||
cos.set_encryption_legacy(¶ms).unwrap();
|
||||
let bytes = Arc::new(pdfcraft_cos::write_full(&cos, &pdfcraft_cos::SaveOptions::default()).unwrap());
|
||||
let mut s = Session::new();
|
||||
// pdfcraft-cos accepts the password as typed (it applies SASLprep); the renderer used to
|
||||
@@ -13197,7 +13197,7 @@ fn importing_a_pdf_copies_its_comments() {
|
||||
// A PDF that needs a password is refused with that reason, with or without bytes before its header.
|
||||
let mut locked = pdfcraft_cos::Document::open(Arc::new(review)).unwrap();
|
||||
locked
|
||||
.set_encryption(&pdfcraft_cos::NewEncryption {
|
||||
.set_encryption_legacy(&pdfcraft_cos::NewEncryption {
|
||||
algorithm: pdfcraft_cos::Algorithm::Aes256,
|
||||
user_password: "u",
|
||||
owner_password: "o",
|
||||
|
||||
@@ -1309,7 +1309,7 @@ fn a_nonempty_user_password_document_is_proven_with_those_credentials() {
|
||||
let mut doc = marked(Spec::default());
|
||||
let mut snap = Snapshot::capture(&doc, None).unwrap();
|
||||
applied(&mut doc, &mut snap).unwrap();
|
||||
doc.set_encryption(&NewEncryption {
|
||||
doc.set_encryption_legacy(&NewEncryption {
|
||||
algorithm: alg,
|
||||
user_password: "pw-secret",
|
||||
owner_password: "owner-secret",
|
||||
|
||||
@@ -11481,7 +11481,7 @@ fn a_filters_parameters_are_read_in_either_form() {
|
||||
|
||||
let plain = format!("4 0 obj << /Length {} >> stream\n{}\nendstream endobj", content.len(), String::from_utf8_lossy(content));
|
||||
let mut doc = Document::open(Arc::new(page(plain.as_bytes()))).unwrap();
|
||||
doc.set_encryption(&NewEncryption {
|
||||
doc.set_encryption_legacy(&NewEncryption {
|
||||
algorithm: Algorithm::Aes128,
|
||||
user_password: "",
|
||||
owner_password: "owner",
|
||||
@@ -11558,7 +11558,7 @@ fn a_stream_with_a_crypt_filter_of_its_own_is_drawn() {
|
||||
};
|
||||
for (algorithm, filters) in [(Algorithm::Rc4_128, false), (Algorithm::Aes128, true), (Algorithm::Aes256, true)] {
|
||||
let mut doc = Document::open(Arc::new(page.clone().into_bytes())).unwrap();
|
||||
doc.set_encryption(&NewEncryption {
|
||||
doc.set_encryption_legacy(&NewEncryption {
|
||||
algorithm,
|
||||
user_password: "",
|
||||
owner_password: "owner",
|
||||
@@ -11691,7 +11691,7 @@ endobj
|
||||
fn an_encrypted_file_with_a_cut_off_update_still_needs_its_password() {
|
||||
use pdfcraft_cos::{Algorithm, Document, NewEncryption, SaveOptions, write_full};
|
||||
let mut doc = Document::open(Arc::new(one_page_file())).unwrap();
|
||||
doc.set_encryption(&NewEncryption {
|
||||
doc.set_encryption_legacy(&NewEncryption {
|
||||
algorithm: Algorithm::Aes128,
|
||||
user_password: "user",
|
||||
owner_password: "owner",
|
||||
@@ -11730,7 +11730,7 @@ trailer << /Root 1 0 R >>
|
||||
%%EOF"
|
||||
.to_vec();
|
||||
let mut doc = Document::open(Arc::new(plain)).unwrap();
|
||||
doc.set_encryption(&NewEncryption {
|
||||
doc.set_encryption_legacy(&NewEncryption {
|
||||
algorithm: Algorithm::Aes128,
|
||||
user_password: "user",
|
||||
owner_password: "owner",
|
||||
|
||||
@@ -71,7 +71,7 @@ fn protected(alg: pdfcraft_cos::Algorithm, permissions: i32) -> Vec<u8> {
|
||||
/// `fixture()` protected with the user password `user` and an owner password, then saved.
|
||||
fn protected_with(alg: pdfcraft_cos::Algorithm, user: &str, permissions: i32) -> Vec<u8> {
|
||||
let mut doc = open(&fixture());
|
||||
doc.set_encryption(&pdfcraft_cos::NewEncryption {
|
||||
doc.set_encryption_legacy(&pdfcraft_cos::NewEncryption {
|
||||
algorithm: alg,
|
||||
user_password: user,
|
||||
owner_password: "owner",
|
||||
|
||||
@@ -2677,7 +2677,7 @@ fn protected(alg: pdfcraft_cos::Algorithm, permissions: i32) -> Vec<u8> {
|
||||
/// `fixture()` protected with the user password `user` and an owner password, then saved.
|
||||
fn protected_with(alg: pdfcraft_cos::Algorithm, user: &str, permissions: i32) -> Vec<u8> {
|
||||
let mut doc = open(&fixture());
|
||||
doc.set_encryption(&pdfcraft_cos::NewEncryption {
|
||||
doc.set_encryption_legacy(&pdfcraft_cos::NewEncryption {
|
||||
algorithm: alg,
|
||||
user_password: user,
|
||||
owner_password: "owner",
|
||||
|
||||
@@ -75,7 +75,7 @@ fn protected(user: &str, owner: &str) -> Vec<u8> {
|
||||
|
||||
fn protected_with(algorithm: pdfcraft_cos::Algorithm, user: &str, owner: &str) -> Vec<u8> {
|
||||
let mut doc = pdfcraft_cos::Document::open(std::sync::Arc::new(fixture(2))).unwrap();
|
||||
doc.set_encryption(&pdfcraft_cos::NewEncryption {
|
||||
doc.set_encryption_legacy(&pdfcraft_cos::NewEncryption {
|
||||
algorithm,
|
||||
user_password: user,
|
||||
owner_password: owner,
|
||||
|
||||
@@ -2592,7 +2592,7 @@ fn protected_with(algorithm: pdfcraft_cos::Algorithm, user: &str, owner: &str, p
|
||||
/// `bytes` rewritten encrypted.
|
||||
pub(crate) fn encrypted(bytes: Vec<u8>, algorithm: pdfcraft_cos::Algorithm, user: &str, owner: &str, permissions: i32) -> Vec<u8> {
|
||||
let mut doc = pdfcraft_cos::Document::open(std::sync::Arc::new(bytes)).unwrap();
|
||||
doc.set_encryption(&pdfcraft_cos::NewEncryption {
|
||||
doc.set_encryption_legacy(&pdfcraft_cos::NewEncryption {
|
||||
algorithm,
|
||||
user_password: user,
|
||||
owner_password: owner,
|
||||
|
||||
Reference in new issue
Block a user