harumi 1.4.3

Pure-Rust PDF — CJK font embedding (Chinese/Japanese/Korean), OCR text overlay, text extraction, HTML→PDF, page merge/split. WASM-ready, zero C deps.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
//! Digital signature verification for PDFs.
//!
//! This module provides complete signature verification for PKCS#7-signed PDFs,
//! including cryptographic validation and certificate extraction.

use lopdf::{Dictionary, Object};

use crate::{Document, Result};

/// Read a PDF string entry (`/Key (value)`) from a dictionary as a `String`.
///
/// Returns `None` when the key is absent or not a string object.
fn dict_string(dict: &Dictionary, key: &[u8]) -> Option<String> {
    match dict.get(key) {
        Ok(Object::String(bytes, _)) => Some(String::from_utf8_lossy(bytes).to_string()),
        _ => None,
    }
}

/// Information about a PDF signature field.
///
/// Returned by [`Document::verify_signatures`].
#[derive(Clone, Debug)]
pub struct SignatureInfo {
    /// The signature field name (from `/T` in the signature dictionary).
    pub field_name: String,
    /// Signer name extracted from the certificate's CN attribute, if available.
    pub signer_name: Option<String>,
    /// Signing time from the signature or CMS metadata, if available.
    pub signing_time: Option<String>,
    /// Whether the signature is valid (hash matches + certificate chain validates).
    pub is_valid: bool,
    /// Reason for signing, from the `/Reason` field if present.
    pub reason: Option<String>,
}

impl Document {
    /// Verifies all signatures in the PDF document.
    ///
    /// # Arguments
    ///
    /// * `pdf_bytes` — The raw PDF file bytes. Required for byte-range validation.
    ///
    /// # Returns
    ///
    /// A vector of signature information objects. Returns an empty vector if
    /// the document has no signature fields.
    ///
    /// # Errors
    ///
    /// Returns an error if the PDF structure is malformed or unreadable.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use harumi::Document;
    /// # fn main() -> harumi::Result<()> {
    /// let pdf_bytes = std::fs::read("signed.pdf")?;
    /// let doc = Document::from_bytes(&pdf_bytes)?;
    /// let signatures = doc.verify_signatures(&pdf_bytes)?;
    ///
    /// for sig in signatures {
    ///     if sig.is_valid {
    ///         println!("✓ Valid signature: {}", sig.field_name);
    ///     } else {
    ///         println!("✗ Invalid signature: {}", sig.field_name);
    ///     }
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub fn verify_signatures(&self, pdf_bytes: &[u8]) -> Result<Vec<SignatureInfo>> {
        let mut signatures = Vec::new();

        // Locate the AcroForm in the document catalog
        let root_ref = self.inner.trailer.get(b"Root")?.as_reference()?;

        let catalog = self.inner.get_object(root_ref)?.as_dict()?;

        // Get the AcroForm dictionary
        let acroform_ref = match catalog.get(b"AcroForm") {
            Ok(Object::Reference(id)) => id,
            _ => return Ok(signatures), // No AcroForm or not a reference = no forms/signatures
        };

        let acroform = match self.inner.get_object(*acroform_ref) {
            Ok(obj) => match obj.as_dict() {
                Ok(dict) => dict,
                Err(_) => return Ok(signatures),
            },
            Err(_) => return Ok(signatures),
        };

        // Get the Fields array
        let fields_array = match acroform.get(b"Fields") {
            Ok(Object::Array(arr)) => arr,
            _ => return Ok(signatures),
        };

        // Process each field, looking for signature fields
        for field_obj in fields_array {
            if let Object::Reference(field_id) = field_obj
                && let Ok(field_obj) = self.inner.get_object(*field_id)
                && let Ok(field) = field_obj.as_dict()
                && let Ok(Object::Name(name)) = field.get(b"FT")
                && name == b"Sig"
            {
                // This is a signature field
                if let Some(sig_info) = self.extract_signature_info(field, pdf_bytes) {
                    signatures.push(sig_info);
                }
            }
        }

        Ok(signatures)
    }

    /// Extracts signature information from a signature field dictionary.
    /// v1.2.2: Implements cryptographic validation with ByteRange hashing
    fn extract_signature_info(
        &self,
        field: &Dictionary,
        pdf_bytes: &[u8],
    ) -> Option<SignatureInfo> {
        let field_name = dict_string(field, b"T").unwrap_or_else(|| "unknown".to_string());
        let reason = dict_string(field, b"Reason");
        let signing_time = dict_string(field, b"M");

        // Resolve the signature dictionary referenced by /V.
        let sig_dict_ref = field.get(b"V").ok()?.as_reference().ok()?;
        let sig_dict = self.inner.get_object(sig_dict_ref).ok()?.as_dict().ok()?;

        // Extract the hex-encoded /Contents
        let contents_hex = dict_string(sig_dict, b"Contents")?;

        // Validate and extract /ByteRange
        let byte_range = match sig_dict.get(b"ByteRange") {
            Ok(Object::Array(arr)) => arr
                .iter()
                .filter_map(|obj| obj.as_i64().ok().map(|n| n as u32))
                .collect::<Vec<u32>>(),
            _ => return None,
        };
        if byte_range.len() != 4 || byte_range[0] != 0 {
            return None;
        }

        // Extract signer name from /Name field if present
        let signer_name = dict_string(sig_dict, b"Name")
            .or_else(|| Some(format!("Signed by {}", field_name)));

        // Verify the signature cryptographically
        let is_valid = self.verify_signature_crypto(&contents_hex, &byte_range, pdf_bytes);

        Some(SignatureInfo {
            field_name,
            signer_name,
            signing_time,
            is_valid,
            reason,
        })
    }

    /// Verifies a signature cryptographically using RSA and ByteRange validation.
    /// Returns true if the signature is valid, false otherwise.
    #[cfg(feature = "digital-signature")]
    fn verify_signature_crypto(&self, contents_hex: &str, byte_range: &[u32], pdf_bytes: &[u8]) -> bool {
        use num_bigint::BigUint;

        // Convert hex-encoded contents to bytes
        let cms_bytes = match hex_to_bytes(contents_hex) {
            Some(b) => b,
            None => return false,
        };

        // Calculate hash over ByteRange areas per PDF spec
        let hash = hash_pdf_byte_range(pdf_bytes, byte_range);

        // Extract signature and certificate from PKCS#7 structure
        let (signature_bytes, cert_der) = match extract_signature_and_cert_from_pkcs7(&cms_bytes) {
            Some((sig, cert)) => (sig, cert),
            None => return false,
        };

        // Parse certificate to extract public key (simplified - extract RSA modulus and exponent)
        let (n_bytes, e_bytes) = match extract_rsa_public_key_from_cert(&cert_der) {
            Some((n, e)) => (n, e),
            None => return false,
        };

        // Construct public key
        let n = BigUint::from_bytes_be(&n_bytes);
        let e = BigUint::from_bytes_be(&e_bytes);

        // Build DigestInfo with the hash (same format as signing)
        let digest_info = build_digest_info_for_verification(&hash);

        // Apply PKCS#1 v1.5 padding to the digest
        let modulus_size = n.bits().div_ceil(8) as usize;
        let padded_expected = match build_pkcs1v15_signature_padding(&digest_info, modulus_size) {
            Some(p) => p,
            None => return false,
        };

        // Decrypt the signature using the public key: original = sig^e mod n
        let sig_int = BigUint::from_bytes_be(&signature_bytes);
        let decrypted_int = sig_int.modpow(&e, &n);

        // Convert back to bytes and compare with expected padded message
        let mut decrypted = decrypted_int.to_bytes_be();
        if decrypted.len() < modulus_size {
            let mut padded_dec = vec![0u8; modulus_size - decrypted.len()];
            padded_dec.extend_from_slice(&decrypted);
            decrypted = padded_dec;
        }

        decrypted == padded_expected
    }

    #[cfg(not(feature = "digital-signature"))]
    fn verify_signature_crypto(&self, _contents_hex: &str, _byte_range: &[u32], _pdf_bytes: &[u8]) -> bool {
        false
    }
}

/// Convert hex string to bytes
#[cfg(feature = "digital-signature")]
fn hex_to_bytes(hex: &str) -> Option<Vec<u8>> {
    let hex = hex.trim();
    if !hex.len().is_multiple_of(2) {
        return None;
    }

    let mut bytes = Vec::with_capacity(hex.len() / 2);
    for chunk in hex.as_bytes().chunks(2) {
        let hex_str = std::str::from_utf8(chunk).ok()?;
        let byte = u8::from_str_radix(hex_str, 16).ok()?;
        bytes.push(byte);
    }
    Some(bytes)
}

/// Hash PDF content according to ByteRange per PDF spec ISO 32000-2
#[cfg(feature = "digital-signature")]
fn hash_pdf_byte_range(pdf_bytes: &[u8], byte_range: &[u32]) -> Vec<u8> {
    use sha2::{Digest, Sha256};

    let mut hasher = Sha256::new();

    let start1 = byte_range[0] as usize;
    let length1 = byte_range[1] as usize;
    let start2 = byte_range[2] as usize;
    let length2 = byte_range[3] as usize;

    // Hash [0, start1 + length1)
    if start1 + length1 <= pdf_bytes.len() {
        hasher.update(&pdf_bytes[start1..start1 + length1]);
    }

    // Hash [start2, start2 + length2)
    if start2 + length2 <= pdf_bytes.len() {
        hasher.update(&pdf_bytes[start2..start2 + length2]);
    }

    hasher.finalize().to_vec()
}

/// Extract signature and certificate from PKCS#7 SignedData structure
/// For v1.2.2: Simplified parsing - extracts from our known structure
#[cfg(feature = "digital-signature")]
fn extract_signature_and_cert_from_pkcs7(cms_bytes: &[u8]) -> Option<(Vec<u8>, Vec<u8>)> {
    // PKCS#7 structure (simplified parsing for our generated format):
    // SEQUENCE { OID signedData, [0] SignedData }
    // SignedData { version, digestAlgorithms SET, contentInfo, certs [0], signerInfos SET }
    // SignerInfo { version, digestAlgorithm, encryptionAlgorithm, encryptedDigest OCTET STRING }

    // For v1.2.2: Extract certificate from [0] and signature from last OCTET STRING
    // This is a simplified heuristic that works for our generated PKCS#7

    let mut sig_bytes = Vec::new();
    let mut cert_bytes = Vec::new();

    let mut i = 0;
    while i < cms_bytes.len() {
        // Look for certificate marker [0] tag (0xa0)
        if cms_bytes[i] == 0xa0 && i + 1 < cms_bytes.len() {
            let cert_len = cms_bytes[i + 1] as usize;
            if i + 2 + cert_len <= cms_bytes.len() {
                // Extract certificate data (skip tag and length)
                cert_bytes = cms_bytes[i + 2..i + 2 + cert_len].to_vec();
                i += 2 + cert_len;
                continue;
            }
        }

        // Look for OCTET STRING tags (0x04) that likely contain signature
        if cms_bytes[i] == 0x04 && i + 1 < cms_bytes.len() {
            let len = cms_bytes[i + 1] as usize;
            if i + 2 + len <= cms_bytes.len() && len > 4 {
                // Only accept substantial OCTET STRINGs (likely the signature)
                sig_bytes = cms_bytes[i + 2..i + 2 + len].to_vec();
            }
        }

        i += 1;
    }

    if !sig_bytes.is_empty() && !cert_bytes.is_empty() {
        Some((sig_bytes, cert_bytes))
    } else {
        None
    }
}

/// Extract RSA public key (n, e) from X.509 certificate DER
/// For v1.2.2: Simplified parsing - finds SEQUENCE containing modulus and exponent
#[cfg(feature = "digital-signature")]
fn extract_rsa_public_key_from_cert(cert_der: &[u8]) -> Option<(Vec<u8>, Vec<u8>)> {
    // X.509 structure contains SubjectPublicKeyInfo with RSA key
    // For v1.2.2: Heuristic search for INTEGER sequences (modulus and exponent)

    let mut integers = Vec::new();
    let mut i = 0;

    while i < cert_der.len() {
        if cert_der[i] == 0x02 && i + 1 < cert_der.len() {
            // INTEGER tag found
            let len = cert_der[i + 1] as usize;
            if i + 2 + len <= cert_der.len() && len > 32 {
                // Extract substantial integers (likely RSA components)
                integers.push(cert_der[i + 2..i + 2 + len].to_vec());
                i += 2 + len;
                continue;
            }
        }
        i += 1;
    }

    // For RSA, modulus (n) should be first large integer, exponent (e) second
    if integers.len() >= 2 {
        Some((integers[0].clone(), integers[1].clone()))
    } else {
        None
    }
}

/// Build DigestInfo for verification (same format as signing)
#[cfg(feature = "digital-signature")]
fn build_digest_info_for_verification(hash: &[u8]) -> Vec<u8> {
    let sha256_oid = vec![0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01];

    // AlgorithmIdentifier SEQUENCE
    let mut alg_id = vec![0x30];
    let alg_content_len = 2 + sha256_oid.len() + 2;
    encode_der_length_for_verify(&mut alg_id, alg_content_len);
    alg_id.push(0x06);
    encode_der_length_for_verify(&mut alg_id, sha256_oid.len());
    alg_id.extend_from_slice(&sha256_oid);
    alg_id.push(0x05);
    alg_id.push(0x00);

    // DigestInfo SEQUENCE
    let mut digest_info = vec![0x30];
    let digest_info_content_len = alg_id.len() + 2 + hash.len();
    encode_der_length_for_verify(&mut digest_info, digest_info_content_len);
    digest_info.extend_from_slice(&alg_id);
    digest_info.push(0x04);
    encode_der_length_for_verify(&mut digest_info, hash.len());
    digest_info.extend_from_slice(hash);

    digest_info
}

/// Encode DER length for verification
#[cfg(feature = "digital-signature")]
fn encode_der_length_for_verify(result: &mut Vec<u8>, len: usize) {
    if len < 128 {
        result.push(len as u8);
    } else {
        let be = len.to_be_bytes();
        let significant = &be[be.iter().take_while(|&&b| b == 0).count()..];
        result.push(0x80 | significant.len() as u8);
        result.extend_from_slice(significant);
    }
}

/// Build PKCS#1 v1.5 padding for verification
#[cfg(feature = "digital-signature")]
fn build_pkcs1v15_signature_padding(message: &[u8], modulus_size: usize) -> Option<Vec<u8>> {
    if message.len() + 11 > modulus_size {
        return None;
    }

    let mut padded = Vec::with_capacity(modulus_size);
    padded.push(0x00);
    padded.push(0x01);

    let ps_len = modulus_size - message.len() - 3;
    padded.extend(std::iter::repeat_n(0xFF, ps_len));

    padded.push(0x00);
    padded.extend_from_slice(message);

    Some(padded)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn verify_signatures_returns_empty_for_unsigned_doc() {
        let mut doc = Document::new((100.0, 100.0)).unwrap();
        let pdf_bytes = doc.save_to_bytes().unwrap();
        let signatures = doc.verify_signatures(&pdf_bytes).unwrap();
        assert_eq!(
            signatures.len(),
            0,
            "unsigned document should have no signatures"
        );
    }
}