use crate::json_inspect::decode_pdf_text_string;
use crate::{
Dictionary, Error, FormFieldObjectHelper, Object, ObjectRef, Pdf, Result, WriteOptions,
DEFAULT_MAX_ACROFORM_DEPTH,
};
use std::collections::BTreeSet;
use std::io::{Read, Seek};
pub const DEFAULT_MAX_SIGNATURE_FIELD_DEPTH: usize = crate::pages::DEFAULT_MAX_PAGE_TREE_DEPTH;
pub const SIG_FLAGS_SIGNATURES_EXIST: u32 = 1;
pub const SIG_FLAGS_APPEND_ONLY: u32 = 2;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SignatureInfo {
pub field_ref: ObjectRef,
pub signature_ref: Option<ObjectRef>,
pub field_name: String,
pub byte_range: [u64; 4],
pub sub_filter: Option<String>,
pub signer_name: Option<String>,
pub signing_time: Option<String>,
pub reason: Option<String>,
pub location: Option<String>,
pub contact_info: Option<String>,
pub certificate: Option<Vec<u8>>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SignatureWriteMode {
Incremental,
FullRewrite,
}
impl SignatureWriteMode {
fn from_options(options: &WriteOptions) -> Self {
if options.full_rewrite {
Self::FullRewrite
} else {
Self::Incremental
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SignatureRewriteReason {
NoSignatures,
FullRewrite,
IncrementalTouchesAcroForm,
IncrementalTouchesSignedObject,
IncrementalPreservesSignedByteRanges,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SignatureRewriteImpact {
pub has_signatures: bool,
pub invalidates_signatures: bool,
pub reason: SignatureRewriteReason,
pub first_invalidating_ref: Option<ObjectRef>,
pub signed_object_refs: Vec<ObjectRef>,
pub touched_object_refs: Vec<ObjectRef>,
pub acroform_ref: Option<ObjectRef>,
pub sig_flags: Option<u32>,
}
impl SignatureRewriteImpact {
pub fn signatures_exist(&self) -> bool {
self.sig_flags
.is_some_and(|flags| flags & SIG_FLAGS_SIGNATURES_EXIST != 0)
}
pub fn append_only(&self) -> bool {
self.sig_flags
.is_some_and(|flags| flags & SIG_FLAGS_APPEND_ONLY != 0)
}
}
pub fn signatures<R: Read + Seek>(pdf: &mut Pdf<R>) -> Result<Vec<SignatureInfo>> {
signatures_with_max_depth(pdf, DEFAULT_MAX_SIGNATURE_FIELD_DEPTH)
}
pub fn signatures_with_max_depth<R: Read + Seek>(
pdf: &mut Pdf<R>,
max_depth: usize,
) -> Result<Vec<SignatureInfo>> {
let Some(catalog_ref) = pdf.root_ref() else {
return Ok(Vec::new());
};
let catalog_obj = pdf.resolve_borrowed(catalog_ref)?;
let Object::Dictionary(catalog_dict) = catalog_obj else {
return Ok(Vec::new());
};
let Some(acroform_val) = catalog_dict.get("AcroForm").cloned() else {
return Ok(Vec::new());
};
let Some(acroform_dict) = resolve_dictionary(pdf, acroform_val)? else {
return Ok(Vec::new());
};
let Some(fields_obj) = acroform_dict.get("Fields").cloned() else {
return Ok(Vec::new());
};
let fields = resolve_array(pdf, fields_obj)?;
let mut output = Vec::new();
let mut seen = BTreeSet::new();
for field in fields {
if let Object::Reference(field_ref) = field {
walk_signature_field(pdf, field_ref, "", &mut output, &mut seen, 0, max_depth)?;
}
}
Ok(output)
}
pub fn would_rewrite_invalidate_signatures<R: Read + Seek>(
pdf: &mut Pdf<R>,
options: &WriteOptions,
) -> Result<bool> {
Ok(
signature_rewrite_impact(pdf, SignatureWriteMode::from_options(options))?
.invalidates_signatures,
)
}
pub fn acroform_sig_flags<R: Read + Seek>(pdf: &mut Pdf<R>) -> Result<Option<u32>> {
Ok(resolve_catalog_acroform(pdf)?
.and_then(|(_, acroform)| sig_flags_from_acroform_dict(&acroform)))
}
pub fn clear_sig_flags<R: Read + Seek>(pdf: &mut Pdf<R>) -> Result<bool> {
let Some((home, mut acroform)) = resolve_catalog_acroform(pdf)? else {
return Ok(false);
};
if !clear_sig_flags_in_dict(&mut acroform) {
return Ok(false);
}
write_back_acroform(pdf, home, acroform);
Ok(true)
}
pub fn remove_security_restrictions<R: Read + Seek>(pdf: &mut Pdf<R>) -> Result<bool> {
let Some(root_ref) = pdf.root_ref() else {
return Ok(false);
};
let Object::Dictionary(mut catalog) = pdf.resolve(root_ref)? else {
return Ok(false);
};
let mut changed = false;
if catalog.remove("Perms").is_some() {
pdf.set_object(root_ref, Object::Dictionary(catalog));
changed = true;
}
if let Some((home, mut acroform)) = resolve_catalog_acroform(pdf)? {
if acroform.get("SigFlags").is_some() && sig_flags_from_acroform_dict(&acroform) != Some(0)
{
acroform.insert("SigFlags", Object::Integer(0));
write_back_acroform(pdf, home, acroform);
changed = true;
}
}
Ok(changed)
}
pub fn disable_digital_signatures<R: Read + Seek>(pdf: &mut Pdf<R>) -> Result<bool> {
let mut changed = remove_security_restrictions(pdf)?;
let form_fields = collect_signature_form_field_refs(pdf)?;
let mut to_remove: Vec<ObjectRef> = Vec::new();
for field_ref in form_fields {
let field_type = FormFieldObjectHelper::new(field_ref, pdf).field_type()?;
if field_type.as_deref() != Some(b"Sig") {
continue;
}
to_remove.push(field_ref);
let Object::Dictionary(mut dict) = pdf.resolve(field_ref)? else {
continue; };
let mut field_changed = false;
for key in ["FT", "V", "SV", "Lock"] {
if dict.remove(key).is_some() {
field_changed = true;
}
}
if field_changed {
pdf.set_object(field_ref, Object::Dictionary(dict));
changed = true;
}
}
let Some((home, mut acroform)) = resolve_catalog_acroform(pdf)? else {
return Ok(changed);
};
let Some(fields_obj) = acroform.get("Fields").cloned() else {
return Ok(changed);
};
let fields_ref = fields_obj.as_ref_id();
let fields = resolve_array(pdf, fields_obj)?;
let original_len = fields.len();
let new_fields: Vec<Object> = fields
.into_iter()
.filter(|f| !matches!(f, Object::Reference(r) if to_remove.contains(r)))
.collect();
if new_fields.len() != original_len {
match fields_ref {
Some(fields_ref) => pdf.set_object(fields_ref, Object::Array(new_fields)),
None => {
acroform.insert("Fields", Object::Array(new_fields));
write_back_acroform(pdf, home, acroform);
}
}
changed = true;
}
Ok(changed)
}
fn collect_signature_form_field_refs<R: Read + Seek>(
pdf: &mut Pdf<R>,
) -> Result<BTreeSet<ObjectRef>> {
let mut form_field_keys: BTreeSet<ObjectRef> = BTreeSet::new();
let mut annotations_seen: BTreeSet<ObjectRef> = BTreeSet::new();
let Some((_, acroform)) = resolve_catalog_acroform(pdf)? else {
return Ok(form_field_keys);
};
let Some(fields_obj) = acroform.get("Fields").cloned() else {
return Ok(form_field_keys);
};
let fields = resolve_array(pdf, fields_obj)?;
let mut visited: BTreeSet<ObjectRef> = BTreeSet::new();
for field in fields {
if let Object::Reference(field_ref) = field {
traverse_field(
pdf,
field_ref,
None,
0,
&mut visited,
&mut form_field_keys,
&mut annotations_seen,
)?;
}
}
for page_ref in crate::pages::page_refs(pdf)? {
for annot_ref in page_widget_annotation_refs(pdf, page_ref)? {
if annotations_seen.insert(annot_ref) {
form_field_keys.insert(annot_ref);
}
}
}
Ok(form_field_keys)
}
fn traverse_field<R: Read + Seek>(
pdf: &mut Pdf<R>,
field_ref: ObjectRef,
parent_ref: Option<ObjectRef>,
depth: usize,
visited: &mut BTreeSet<ObjectRef>,
form_field_keys: &mut BTreeSet<ObjectRef>,
annotations_seen: &mut BTreeSet<ObjectRef>,
) -> Result<()> {
if depth > DEFAULT_MAX_ACROFORM_DEPTH {
return Err(Error::Unsupported(format!(
"AcroForm field tree depth exceeds maximum of {DEFAULT_MAX_ACROFORM_DEPTH} at {field_ref}"
)));
}
let Object::Dictionary(dict) = pdf.resolve(field_ref)? else {
return Ok(());
};
if !visited.insert(field_ref) {
return Ok(());
}
let mut is_annotation = false;
let mut is_field = depth == 0;
if let Some(kids) = resolve_kids_array(pdf, &dict)? {
is_field = true;
for kid in kids {
if let Object::Reference(kid_ref) = kid {
traverse_field(
pdf,
kid_ref,
Some(field_ref),
depth + 1,
visited,
form_field_keys,
annotations_seen,
)?;
}
}
} else {
if dict.get("Parent").is_some() {
is_field = true;
}
if dict.get("Subtype").is_some() || dict.get("Rect").is_some() || dict.get("AP").is_some() {
is_annotation = true;
}
}
if is_annotation {
let our_field = if is_field {
field_ref
} else {
parent_ref.unwrap_or(field_ref)
};
form_field_keys.insert(our_field);
annotations_seen.insert(field_ref);
}
Ok(())
}
fn resolve_kids_array<R: Read + Seek>(
pdf: &mut Pdf<R>,
dict: &Dictionary,
) -> Result<Option<Vec<Object>>> {
match dict.get("Kids").cloned() {
Some(Object::Array(items)) => Ok(Some(items)),
Some(Object::Reference(kids_ref)) => match pdf.resolve(kids_ref)? {
Object::Array(items) => Ok(Some(items)),
_ => Ok(None),
},
_ => Ok(None),
}
}
fn page_widget_annotation_refs<R: Read + Seek>(
pdf: &mut Pdf<R>,
page_ref: ObjectRef,
) -> Result<Vec<ObjectRef>> {
let annots_obj = match pdf.resolve(page_ref)? {
Object::Dictionary(page) => page.get("Annots").cloned(),
_ => None,
};
let Some(annots_obj) = annots_obj else {
return Ok(Vec::new());
};
let mut widgets = Vec::new();
for annot in resolve_array(pdf, annots_obj)? {
let Object::Reference(annot_ref) = annot else {
continue;
};
if let Object::Dictionary(annot_dict) = pdf.resolve(annot_ref)? {
if matches!(annot_dict.get("Subtype"), Some(Object::Name(name)) if name.as_slice() == b"Widget")
{
widgets.push(annot_ref);
}
}
}
Ok(widgets)
}
fn write_back_acroform<R: Read + Seek>(pdf: &mut Pdf<R>, home: AcroformHome, acroform: Dictionary) {
match home {
AcroformHome::Object(acroform_ref) => {
pdf.set_object(acroform_ref, Object::Dictionary(acroform));
}
AcroformHome::Inline {
root_ref,
mut catalog,
} => {
catalog.insert("AcroForm", Object::Dictionary(acroform));
pdf.set_object(root_ref, Object::Dictionary(catalog));
}
}
}
pub fn strip_signature_values<R: Read + Seek>(pdf: &mut Pdf<R>) -> Result<bool> {
let Some((_, mut acroform)) = resolve_catalog_acroform(pdf)? else {
return Ok(false);
};
let Some(fields_obj) = acroform.remove("Fields") else {
return Ok(false);
};
let mut changed = false;
let mut seen = BTreeSet::new();
for field in resolve_array(pdf, fields_obj)? {
let Object::Reference(field_ref) = field else {
continue;
};
strip_signature_values_from_field(pdf, field_ref, None, 0, &mut seen, &mut changed)?;
}
Ok(changed)
}
enum AcroformHome {
Object(ObjectRef),
Inline {
root_ref: ObjectRef,
catalog: Dictionary,
},
}
fn resolve_catalog_acroform<R: Read + Seek>(
pdf: &mut Pdf<R>,
) -> Result<Option<(AcroformHome, Dictionary)>> {
let Some(root_ref) = pdf.root_ref() else {
return Ok(None);
};
let Object::Dictionary(catalog) = pdf.resolve(root_ref)? else {
return Ok(None);
};
let Some(acroform) = catalog.get("AcroForm").cloned() else {
return Ok(None);
};
match acroform {
Object::Reference(acroform_ref) => match pdf.resolve(acroform_ref)? {
Object::Dictionary(dict) => Ok(Some((AcroformHome::Object(acroform_ref), dict))),
_ => Ok(None),
},
Object::Dictionary(dict) => Ok(Some((AcroformHome::Inline { root_ref, catalog }, dict))),
_ => Ok(None),
}
}
fn sig_flags_from_acroform_dict(acroform: &Dictionary) -> Option<u32> {
acroform
.get("SigFlags")
.and_then(Object::as_integer)
.and_then(|n| u32::try_from(n).ok())
}
fn clear_sig_flags_in_dict(acroform: &mut Dictionary) -> bool {
let Some(flags) = sig_flags_from_acroform_dict(acroform) else {
return false;
};
let cleared = flags & !(SIG_FLAGS_SIGNATURES_EXIST | SIG_FLAGS_APPEND_ONLY);
if cleared == flags {
return false;
}
acroform.insert("SigFlags", Object::Integer(i64::from(cleared)));
true
}
pub fn signature_rewrite_impact<R: Read + Seek>(
pdf: &mut Pdf<R>,
mode: SignatureWriteMode,
) -> Result<SignatureRewriteImpact> {
let touched_object_refs = pdf.dirty_object_refs();
let touched: BTreeSet<ObjectRef> = touched_object_refs.iter().copied().collect();
let rewrite_info = collect_signature_rewrite_info(pdf)?;
let has_signatures = !rewrite_info.signed_object_refs.is_empty();
let mut signed_object_refs: Vec<ObjectRef> =
rewrite_info.signed_object_refs.iter().copied().collect();
signed_object_refs.sort();
if !has_signatures {
return Ok(SignatureRewriteImpact {
has_signatures: false,
invalidates_signatures: false,
reason: SignatureRewriteReason::NoSignatures,
first_invalidating_ref: None,
signed_object_refs,
touched_object_refs,
acroform_ref: rewrite_info.acroform_ref,
sig_flags: rewrite_info.sig_flags,
});
}
if mode == SignatureWriteMode::FullRewrite {
return Ok(SignatureRewriteImpact {
has_signatures: true,
invalidates_signatures: true,
reason: SignatureRewriteReason::FullRewrite,
first_invalidating_ref: None,
signed_object_refs,
touched_object_refs,
acroform_ref: rewrite_info.acroform_ref,
sig_flags: rewrite_info.sig_flags,
});
}
if let Some(acroform_ref) = rewrite_info.acroform_ref {
if touched.contains(&acroform_ref) {
return Ok(SignatureRewriteImpact {
has_signatures: true,
invalidates_signatures: true,
reason: SignatureRewriteReason::IncrementalTouchesAcroForm,
first_invalidating_ref: Some(acroform_ref),
signed_object_refs,
touched_object_refs,
acroform_ref: Some(acroform_ref),
sig_flags: rewrite_info.sig_flags,
});
}
}
if let Some(object_ref) = signed_object_refs
.iter()
.copied()
.find(|object_ref| touched.contains(object_ref))
{
return Ok(SignatureRewriteImpact {
has_signatures: true,
invalidates_signatures: true,
reason: SignatureRewriteReason::IncrementalTouchesSignedObject,
first_invalidating_ref: Some(object_ref),
signed_object_refs,
touched_object_refs,
acroform_ref: rewrite_info.acroform_ref,
sig_flags: rewrite_info.sig_flags,
});
}
Ok(SignatureRewriteImpact {
has_signatures: true,
invalidates_signatures: false,
reason: SignatureRewriteReason::IncrementalPreservesSignedByteRanges,
first_invalidating_ref: None,
signed_object_refs,
touched_object_refs,
acroform_ref: rewrite_info.acroform_ref,
sig_flags: rewrite_info.sig_flags,
})
}
#[derive(Debug, Default)]
struct SignatureRewriteInfo {
acroform_ref: Option<ObjectRef>,
signed_object_refs: BTreeSet<ObjectRef>,
sig_flags: Option<u32>,
}
fn collect_signature_rewrite_info<R: Read + Seek>(
pdf: &mut Pdf<R>,
) -> Result<SignatureRewriteInfo> {
let mut info = SignatureRewriteInfo::default();
if let Some(root_ref) = pdf.root_ref() {
let root = pdf.resolve(root_ref)?;
if let Object::Dictionary(catalog) = root {
if let Some(acroform) = catalog.get("AcroForm").cloned() {
let (acroform_ref, acroform_dict) = resolve_acroform(pdf, root_ref, acroform)?;
info.acroform_ref = Some(acroform_ref);
info.sig_flags = sig_flags_from_acroform_dict(&acroform_dict);
collect_acroform_signatures(pdf, &acroform_dict, &mut info)?;
}
for key in ["Perms", "DSS"] {
if let Some(value) = catalog.get(key).cloned() {
collect_known_signature_value(pdf, value, &mut info, 0)?;
}
}
}
}
Ok(info)
}
fn resolve_acroform<R: Read + Seek>(
pdf: &mut Pdf<R>,
root_ref: ObjectRef,
acroform: Object,
) -> Result<(ObjectRef, Dictionary)> {
match acroform {
Object::Reference(acroform_ref) => {
let object = pdf.resolve(acroform_ref)?;
match object {
Object::Dictionary(dict) => Ok((acroform_ref, dict)),
_ => Ok((acroform_ref, Dictionary::new())),
}
}
Object::Dictionary(dict) => Ok((root_ref, dict)),
_ => Ok((root_ref, Dictionary::new())),
}
}
fn collect_acroform_signatures<R: Read + Seek>(
pdf: &mut Pdf<R>,
acroform: &Dictionary,
info: &mut SignatureRewriteInfo,
) -> Result<()> {
let Some(fields) = acroform.get("Fields").cloned() else {
return Ok(());
};
let mut seen: BTreeSet<(ObjectRef, bool)> = BTreeSet::new();
for field in resolve_array(pdf, fields)? {
if let Object::Reference(field_ref) = field {
walk_signature_rewrite_field(pdf, field_ref, None, 0, info, &mut seen)?;
}
}
Ok(())
}
fn strip_signature_values_from_field<R: Read + Seek>(
pdf: &mut Pdf<R>,
field_ref: ObjectRef,
inherited_type: Option<Vec<u8>>,
depth: usize,
seen: &mut BTreeSet<ObjectRef>,
changed: &mut bool,
) -> Result<()> {
if depth > DEFAULT_MAX_SIGNATURE_FIELD_DEPTH || !seen.insert(field_ref) {
return Ok(());
}
let Object::Dictionary(mut dict) = pdf.resolve(field_ref)? else {
return Ok(());
};
let field_type = inherited_name(pdf, &dict, "FT")?.or(inherited_type);
let kids_obj = dict.get("Kids").cloned();
let signature_value_ref = dict.get("V").and_then(Object::as_ref_id);
if field_type.as_deref() == Some(b"Sig") && dict.remove("V").is_some() {
pdf.set_object(field_ref, Object::Dictionary(dict));
if let Some(signature_ref) = signature_value_ref {
pdf.delete_object(signature_ref);
}
*changed = true;
if depth == DEFAULT_MAX_SIGNATURE_FIELD_DEPTH {
return Ok(());
}
let Some(kids_obj) = kids_obj else {
return Ok(());
};
return strip_signature_values_from_kids(pdf, kids_obj, field_type, depth, seen, changed);
}
if depth == DEFAULT_MAX_SIGNATURE_FIELD_DEPTH {
return Ok(());
}
let Some(kids_obj) = kids_obj else {
return Ok(());
};
strip_signature_values_from_kids(pdf, kids_obj, field_type, depth, seen, changed)
}
fn strip_signature_values_from_kids<R: Read + Seek>(
pdf: &mut Pdf<R>,
kids_obj: Object,
field_type: Option<Vec<u8>>,
depth: usize,
seen: &mut BTreeSet<ObjectRef>,
changed: &mut bool,
) -> Result<()> {
for kid in resolve_array(pdf, kids_obj)? {
let Object::Reference(kid_ref) = kid else {
continue;
};
let kid_obj = pdf.resolve_borrowed(kid_ref)?;
let Object::Dictionary(kid_dict) = kid_obj else {
continue;
};
if is_pure_widget(kid_dict) {
continue;
}
strip_signature_values_from_field(
pdf,
kid_ref,
field_type.clone(),
depth + 1,
seen,
changed,
)?;
}
Ok(())
}
fn walk_signature_rewrite_field<R: Read + Seek>(
pdf: &mut Pdf<R>,
field_ref: ObjectRef,
inherited_ft: Option<Vec<u8>>,
depth: usize,
info: &mut SignatureRewriteInfo,
seen: &mut BTreeSet<(ObjectRef, bool)>,
) -> Result<()> {
let inherited_is_sig = inherited_ft.as_deref() == Some(b"Sig");
if !seen.insert((field_ref, inherited_is_sig)) {
return Ok(());
}
if depth > DEFAULT_MAX_SIGNATURE_FIELD_DEPTH {
return Err(crate::Error::Unsupported(format!(
"signature field-tree depth limit {DEFAULT_MAX_SIGNATURE_FIELD_DEPTH} exceeded at {field_ref}"
)));
}
let Object::Dictionary(dict) = pdf.resolve(field_ref)? else {
return Ok(());
};
let field_type = inherited_name(pdf, &dict, "FT")?.or(inherited_ft);
if field_type.as_deref() == Some(b"Sig") {
info.signed_object_refs.insert(field_ref);
if let Some(value) = dict.get("V").cloned() {
collect_signature_value(pdf, value, info)?;
}
}
if dict.get("ByteRange").is_some() {
info.signed_object_refs.insert(field_ref);
}
if let Some(kids) = dict.get("Kids").cloned() {
for kid in resolve_array(pdf, kids)? {
if let Object::Reference(kid_ref) = kid {
walk_signature_rewrite_field(
pdf,
kid_ref,
field_type.clone(),
depth + 1,
info,
seen,
)?;
}
}
}
Ok(())
}
fn collect_known_signature_value<R: Read + Seek>(
pdf: &mut Pdf<R>,
value: Object,
info: &mut SignatureRewriteInfo,
depth: usize,
) -> Result<()> {
if depth > DEFAULT_MAX_SIGNATURE_FIELD_DEPTH {
return Err(crate::Error::Unsupported(format!(
"signature known-container depth limit {DEFAULT_MAX_SIGNATURE_FIELD_DEPTH} exceeded"
)));
}
match value {
Object::Reference(object_ref) => {
let object = pdf.resolve(object_ref)?;
if object_has_byte_range(&object) {
info.signed_object_refs.insert(object_ref);
}
collect_known_signature_value(pdf, object, info, depth + 1)?;
}
Object::Dictionary(dict) => {
for (_, value) in dict.iter() {
collect_known_signature_value(pdf, value.clone(), info, depth + 1)?;
}
}
Object::Array(items) => {
for item in items {
collect_known_signature_value(pdf, item, info, depth + 1)?;
}
}
Object::Stream(stream) if stream.dict.get("ByteRange").is_some() => {
for (_, value) in stream.dict.iter() {
collect_known_signature_value(pdf, value.clone(), info, depth + 1)?;
}
}
_ => {}
}
Ok(())
}
fn collect_signature_value<R: Read + Seek>(
pdf: &mut Pdf<R>,
value: Object,
info: &mut SignatureRewriteInfo,
) -> Result<()> {
match value {
Object::Reference(sig_ref) => {
let object = pdf.resolve(sig_ref)?;
if object_has_byte_range(&object) {
info.signed_object_refs.insert(sig_ref);
}
}
object if object_has_byte_range(&object) => {}
_ => {}
}
Ok(())
}
fn walk_signature_field<R: Read + Seek>(
pdf: &mut Pdf<R>,
field_ref: ObjectRef,
parent_name: &str,
output: &mut Vec<SignatureInfo>,
seen: &mut BTreeSet<ObjectRef>,
depth: usize,
max_depth: usize,
) -> Result<()> {
if depth > max_depth || !seen.insert(field_ref) {
return Ok(());
}
let field_obj = pdf.resolve_borrowed(field_ref)?;
let Object::Dictionary(field_dict) = field_obj else {
return Ok(());
};
let field_dict = field_dict.clone();
let field_name = join_field_name(parent_name, text_entry(pdf, &field_dict, "T")?);
if inherited_name(pdf, &field_dict, "FT")?.as_deref() == Some(b"Sig") {
if let Some(info) = signature_info_for_field(pdf, field_ref, &field_name, &field_dict)? {
output.push(info);
}
}
if depth == max_depth {
return Ok(());
}
let Some(kids_obj) = field_dict.get("Kids").cloned() else {
return Ok(());
};
for kid in resolve_array(pdf, kids_obj)? {
let Object::Reference(kid_ref) = kid else {
continue;
};
let kid_obj = pdf.resolve_borrowed(kid_ref)?;
let Object::Dictionary(kid_dict) = kid_obj else {
continue;
};
if is_pure_widget(kid_dict) {
continue;
}
walk_signature_field(
pdf,
kid_ref,
&field_name,
output,
seen,
depth + 1,
max_depth,
)?;
}
Ok(())
}
fn signature_info_for_field<R: Read + Seek>(
pdf: &mut Pdf<R>,
field_ref: ObjectRef,
field_name: &str,
field_dict: &Dictionary,
) -> Result<Option<SignatureInfo>> {
let Some(value) = inherited_field_value(pdf, field_dict, "V")? else {
return Ok(None);
};
let signature_ref = value.as_ref_id();
let Some(signature_dict) = resolve_dictionary(pdf, value)? else {
return Ok(None);
};
let Some(byte_range_obj) = signature_dict.get("ByteRange").cloned() else {
return Ok(None);
};
let byte_range = parse_byte_range(pdf, byte_range_obj)?;
Ok(Some(SignatureInfo {
field_ref,
signature_ref,
field_name: field_name.to_string(),
byte_range,
sub_filter: name_entry(pdf, &signature_dict, "SubFilter")?,
signer_name: text_entry(pdf, &signature_dict, "Name")?,
signing_time: text_entry(pdf, &signature_dict, "M")?,
reason: text_entry(pdf, &signature_dict, "Reason")?,
location: text_entry(pdf, &signature_dict, "Location")?,
contact_info: text_entry(pdf, &signature_dict, "ContactInfo")?,
certificate: certificate_entry(pdf, &signature_dict)?,
}))
}
fn resolve_dictionary<R: Read + Seek>(
pdf: &mut Pdf<R>,
value: Object,
) -> Result<Option<Dictionary>> {
match value {
Object::Dictionary(dict) => Ok(Some(dict)),
Object::Reference(object_ref) => match pdf.resolve_borrowed(object_ref)? {
Object::Dictionary(dict) => Ok(Some(dict.clone())),
_ => Ok(None),
},
_ => Ok(None),
}
}
fn resolve_array<R: Read + Seek>(pdf: &mut Pdf<R>, value: Object) -> Result<Vec<Object>> {
match value {
Object::Array(values) => Ok(values),
Object::Reference(object_ref) => match pdf.resolve(object_ref)? {
Object::Array(values) => Ok(values),
_ => Ok(Vec::new()),
},
_ => Ok(Vec::new()),
}
}
fn parse_byte_range<R: Read + Seek>(pdf: &mut Pdf<R>, value: Object) -> Result<[u64; 4]> {
let values = match value {
Object::Array(values) => values,
Object::Reference(object_ref) => match pdf.resolve_borrowed(object_ref)? {
Object::Array(values) => values.clone(),
_ => return Err(invalid_byte_range("must be an array")),
},
_ => return Err(invalid_byte_range("must be an array")),
};
if values.len() != 4 {
return Err(invalid_byte_range("must contain exactly four integers"));
}
let mut out = [0; 4];
for (idx, value) in values.iter().enumerate() {
let Some(n) = value.as_integer() else {
return Err(invalid_byte_range("must contain only integers"));
};
out[idx] = u64::try_from(n)
.map_err(|_| invalid_byte_range("must contain non-negative integers"))?;
}
Ok(out)
}
fn invalid_byte_range(message: &'static str) -> Error {
Error::parse(0, format!("invalid signature /ByteRange: {message}"))
}
fn inherited_name<R: Read + Seek>(
pdf: &mut Pdf<R>,
field_dict: &Dictionary,
key: &str,
) -> Result<Option<Vec<u8>>> {
match inherited_field_value(pdf, field_dict, key)? {
Some(Object::Name(name)) => Ok(Some(name)),
Some(Object::Reference(object_ref)) => match pdf.resolve_borrowed(object_ref)? {
Object::Name(name) => Ok(Some(name.clone())),
_ => Ok(None),
},
_ => Ok(None),
}
}
fn inherited_field_value<R: Read + Seek>(
pdf: &mut Pdf<R>,
field_dict: &Dictionary,
key: &str,
) -> Result<Option<Object>> {
if let Some(local) = field_dict.get(key).cloned() {
return Ok(Some(local));
}
let mut parent = field_dict.get("Parent").cloned();
let mut seen = BTreeSet::new();
let mut depth: usize = 0;
while let Some(Object::Reference(parent_ref)) = parent {
if depth > DEFAULT_MAX_SIGNATURE_FIELD_DEPTH {
return Err(Error::Unsupported(format!(
"signature field-tree depth limit {DEFAULT_MAX_SIGNATURE_FIELD_DEPTH} exceeded at {parent_ref}"
)));
}
if !seen.insert(parent_ref) {
break;
}
match pdf.resolve_borrowed(parent_ref)? {
Object::Dictionary(parent_dict) => {
if let Some(value) = parent_dict.get(key).cloned() {
return Ok(Some(value));
}
parent = parent_dict.get("Parent").cloned();
}
_ => break,
}
depth += 1;
}
Ok(None)
}
fn join_field_name(parent_name: &str, local_name: Option<String>) -> String {
let local_name = local_name.unwrap_or_default();
if parent_name.is_empty() {
local_name
} else if local_name.is_empty() {
parent_name.to_string()
} else {
format!("{parent_name}.{local_name}")
}
}
fn is_pure_widget(dict: &Dictionary) -> bool {
let is_widget = matches!(
dict.get("Subtype"),
Some(Object::Name(name)) if name.as_slice() == b"Widget"
);
let has_field_entries = dict.get("T").is_some()
|| dict.get("FT").is_some()
|| dict.get("Kids").is_some()
|| dict.get("V").is_some()
|| dict.get("DV").is_some()
|| dict.get("Ff").is_some()
|| dict.get("TU").is_some()
|| dict.get("TM").is_some();
is_widget && !has_field_entries
}
fn resolve_entry<R: Read + Seek>(
pdf: &mut Pdf<R>,
dict: &Dictionary,
key: &str,
) -> Result<Option<Object>> {
match dict.get(key) {
Some(Object::Reference(object_ref)) => Ok(Some(pdf.resolve(*object_ref)?)),
Some(object) => Ok(Some(object.clone())),
None => Ok(None),
}
}
fn name_entry<R: Read + Seek>(
pdf: &mut Pdf<R>,
dict: &Dictionary,
key: &str,
) -> Result<Option<String>> {
match resolve_entry(pdf, dict, key)? {
Some(Object::Name(name)) => Ok(Some(String::from_utf8_lossy(&name).into_owned())),
_ => Ok(None),
}
}
fn text_entry<R: Read + Seek>(
pdf: &mut Pdf<R>,
dict: &Dictionary,
key: &str,
) -> Result<Option<String>> {
match resolve_entry(pdf, dict, key)? {
Some(Object::String(bytes)) => Ok(Some(
decode_pdf_text_string(&bytes)
.unwrap_or_else(|| String::from_utf8_lossy(&bytes).into()),
)),
_ => Ok(None),
}
}
fn certificate_entry<R: Read + Seek>(
pdf: &mut Pdf<R>,
dict: &Dictionary,
) -> Result<Option<Vec<u8>>> {
match resolve_entry(pdf, dict, "Cert")? {
Some(Object::String(bytes)) => Ok(Some(bytes)),
Some(Object::Array(values)) => {
for value in values {
match value {
Object::String(bytes) => return Ok(Some(bytes)),
Object::Reference(object_ref) => {
if let Object::String(bytes) = pdf.resolve(object_ref)? {
return Ok(Some(bytes));
}
}
_ => {}
}
}
Ok(None)
}
_ => Ok(None),
}
}
fn object_has_byte_range(object: &Object) -> bool {
match object {
Object::Dictionary(dict) => dict.get("ByteRange").is_some(),
Object::Stream(stream) => stream.dict.get("ByteRange").is_some(),
_ => false,
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Cursor;
fn empty_pdf() -> Pdf<Cursor<Vec<u8>>> {
let mut bytes = Vec::new();
bytes.extend_from_slice(b"%PDF-1.4\n");
let off1 = bytes.len() as u64;
bytes.extend_from_slice(b"1 0 obj\n<< /Type /Catalog >>\nendobj\n");
let xref = bytes.len() as u64;
bytes.extend_from_slice(
format!(
"xref\n0 2\n0000000000 65535 f \n{off1:010} 00000 n \ntrailer\n<< /Size 2 /Root 1 0 R >>\nstartxref\n{xref}\n%%EOF\n"
)
.as_bytes(),
);
Pdf::open(Cursor::new(bytes)).expect("open")
}
fn parent_chain(pdf: &mut Pdf<Cursor<Vec<u8>>>, start: u32, len: u32, key: &str) -> Dictionary {
for i in 0..len {
let num = start + i;
let mut d = Dictionary::new();
if i + 1 < len {
d.insert("Parent", Object::Reference(ObjectRef::new(num + 1, 0)));
} else {
d.insert(key, Object::Integer(42));
}
pdf.set_object(ObjectRef::new(num, 0), Object::Dictionary(d));
}
let mut start_dict = Dictionary::new();
start_dict.insert("Parent", Object::Reference(ObjectRef::new(start, 0)));
start_dict
}
#[test]
fn inherited_field_value_errors_on_excessive_parent_depth() {
let mut pdf = empty_pdf();
let start_dict = parent_chain(
&mut pdf,
2,
(DEFAULT_MAX_SIGNATURE_FIELD_DEPTH as u32) + 5,
"V",
);
let err = inherited_field_value(&mut pdf, &start_dict, "V");
assert!(matches!(err, Err(Error::Unsupported(_))));
}
#[test]
fn inherited_field_value_resolves_within_limit() {
let mut pdf = empty_pdf();
let start_dict = parent_chain(&mut pdf, 2, 4, "V");
let got = inherited_field_value(&mut pdf, &start_dict, "V").unwrap();
assert_eq!(got, Some(Object::Integer(42)));
}
fn dict_value(pairs: Vec<(&str, Object)>) -> Dictionary {
let mut d = Dictionary::new();
for (k, v) in pairs {
d.insert(k, v);
}
d
}
fn dict(pairs: Vec<(&str, Object)>) -> Object {
Object::Dictionary(dict_value(pairs))
}
fn refs_vec(pairs: &[u32]) -> Vec<Object> {
pairs
.iter()
.map(|n| Object::Reference(ObjectRef::new(*n, 0)))
.collect()
}
fn refs(pairs: &[u32]) -> Object {
Object::Array(refs_vec(pairs))
}
#[test]
fn traverse_field_visited_guard_breaks_kids_cycle() {
let mut pdf = empty_pdf();
pdf.set_object(ObjectRef::new(5, 0), dict(vec![("Kids", refs(&[6]))]));
pdf.set_object(ObjectRef::new(6, 0), dict(vec![("Kids", refs(&[5]))]));
let mut visited = BTreeSet::new();
let mut keys = BTreeSet::new();
let mut annots = BTreeSet::new();
traverse_field(
&mut pdf,
ObjectRef::new(5, 0),
None,
0,
&mut visited,
&mut keys,
&mut annots,
)
.unwrap();
assert!(visited.contains(&ObjectRef::new(5, 0)));
assert!(visited.contains(&ObjectRef::new(6, 0)));
assert!(keys.is_empty());
}
#[test]
fn traverse_field_ignores_non_dictionary_node() {
let mut pdf = empty_pdf();
pdf.set_object(ObjectRef::new(5, 0), Object::Integer(7));
let mut visited = BTreeSet::new();
let mut keys = BTreeSet::new();
let mut annots = BTreeSet::new();
traverse_field(
&mut pdf,
ObjectRef::new(5, 0),
None,
0,
&mut visited,
&mut keys,
&mut annots,
)
.unwrap();
assert!(keys.is_empty());
assert!(!visited.contains(&ObjectRef::new(5, 0)));
}
#[test]
fn traverse_field_errs_past_depth_limit() {
let mut pdf = empty_pdf();
pdf.set_object(ObjectRef::new(5, 0), dict(vec![]));
let mut visited = BTreeSet::new();
let mut keys = BTreeSet::new();
let mut annots = BTreeSet::new();
let err = traverse_field(
&mut pdf,
ObjectRef::new(5, 0),
None,
DEFAULT_MAX_ACROFORM_DEPTH + 1,
&mut visited,
&mut keys,
&mut annots,
);
assert!(matches!(err, Err(Error::Unsupported(_))));
}
#[test]
fn resolve_kids_array_resolves_indirect_and_rejects_non_array() {
let mut pdf = empty_pdf();
pdf.set_object(ObjectRef::new(9, 0), refs(&[6, 7]));
pdf.set_object(ObjectRef::new(10, 0), Object::Integer(1));
let direct = dict_value(vec![("Kids", refs(&[6]))]);
assert_eq!(
resolve_kids_array(&mut pdf, &direct).unwrap(),
Some(refs_vec(&[6]))
);
let indirect = dict_value(vec![("Kids", Object::Reference(ObjectRef::new(9, 0)))]);
assert_eq!(
resolve_kids_array(&mut pdf, &indirect).unwrap(),
Some(refs_vec(&[6, 7]))
);
let non_array = dict_value(vec![("Kids", Object::Reference(ObjectRef::new(10, 0)))]);
assert_eq!(resolve_kids_array(&mut pdf, &non_array).unwrap(), None);
assert_eq!(
resolve_kids_array(&mut pdf, &dict_value(vec![])).unwrap(),
None
);
let name_kids = dict_value(vec![("Kids", Object::Name(b"x".to_vec()))]);
assert_eq!(resolve_kids_array(&mut pdf, &name_kids).unwrap(), None);
}
#[test]
fn page_widget_annotation_refs_filters_widgets_and_edge_entries() {
let mut pdf = empty_pdf();
pdf.set_object(
ObjectRef::new(3, 0),
dict(vec![(
"Annots",
Object::Array(vec![
Object::Reference(ObjectRef::new(5, 0)),
Object::Reference(ObjectRef::new(6, 0)),
Object::Reference(ObjectRef::new(7, 0)),
Object::Integer(99),
]),
)]),
);
pdf.set_object(
ObjectRef::new(5, 0),
dict(vec![("Subtype", Object::Name(b"Widget".to_vec()))]),
);
pdf.set_object(
ObjectRef::new(6, 0),
dict(vec![("Subtype", Object::Name(b"Link".to_vec()))]),
);
pdf.set_object(ObjectRef::new(7, 0), Object::Integer(0));
let widgets = page_widget_annotation_refs(&mut pdf, ObjectRef::new(3, 0)).unwrap();
assert_eq!(widgets, vec![ObjectRef::new(5, 0)]);
}
#[test]
fn page_widget_annotation_refs_handles_non_dict_page_and_missing_annots() {
let mut pdf = empty_pdf();
pdf.set_object(ObjectRef::new(3, 0), Object::Integer(1));
assert!(page_widget_annotation_refs(&mut pdf, ObjectRef::new(3, 0))
.unwrap()
.is_empty());
pdf.set_object(
ObjectRef::new(3, 0),
dict(vec![("Type", Object::Name(b"Page".to_vec()))]),
);
assert!(page_widget_annotation_refs(&mut pdf, ObjectRef::new(3, 0))
.unwrap()
.is_empty());
}
}