Module: Pdfrb::DigitalSignature::Verification
- Defined in:
- lib/pdfrb/digital_signature/verification.rb
Class Method Summary collapse
- .find_signature(pdf_bytes, start_offset) ⇒ Object
- .verify(pdf_bytes, trusted_certs: []) ⇒ Object
- .verify_signature(pdf_bytes, sig_info, trusted_certs) ⇒ Object
Class Method Details
.find_signature(pdf_bytes, start_offset) ⇒ Object
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 |
# File 'lib/pdfrb/digital_signature/verification.rb', line 26 def find_signature(pdf_bytes, start_offset) contents_marker = "/Contents <" ck_start = pdf_bytes.index(contents_marker, start_offset) return nil unless ck_start hex_start = ck_start + contents_marker.bytesize hex_end = pdf_bytes.index(">", hex_start) return nil unless hex_end br_start = pdf_bytes.rindex("/ByteRange [", ck_start) return nil unless br_start br_str_start = br_start + "/ByteRange [".bytesize br_end = pdf_bytes.index("]", br_str_start) return nil unless br_end br_str = pdf_bytes[br_str_start...br_end] byte_range = br_str.split.map(&:to_i) { byte_range: byte_range, contents_hex_start: hex_start, contents_hex_end: hex_end, contents_end: hex_end, } end |
.verify(pdf_bytes, trusted_certs: []) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/pdfrb/digital_signature/verification.rb', line 11 def verify(pdf_bytes, trusted_certs: []) results = [] offset = 0 loop do sig_info = find_signature(pdf_bytes, offset) break unless sig_info results << verify_signature(pdf_bytes, sig_info, trusted_certs) offset = sig_info[:contents_end] + 1 end results end |
.verify_signature(pdf_bytes, sig_info, trusted_certs) ⇒ Object
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 |
# File 'lib/pdfrb/digital_signature/verification.rb', line 53 def verify_signature(pdf_bytes, sig_info, trusted_certs) byte_range = sig_info[:byte_range] if byte_range.nil? || byte_range.length != 4 return VerificationResult.new(valid?: false, byte_range_ok?: false, cert_chain: [], error: "invalid ByteRange") end _start1, len1, start2, len2 = byte_range part1 = pdf_bytes.bytes[0, len1] || [] part2 = pdf_bytes.bytes[start2, len2] || [] signed_data = part1.pack("C*") + part2.pack("C*") hex_start = sig_info[:contents_hex_start] hex_end = sig_info[:contents_hex_end] contents_hex = pdf_bytes[hex_start...hex_end] contents_hex = contents_hex.sub(/0+$/, "") begin der = [contents_hex].pack("H*") pkcs7 = OpenSSL::PKCS7.new(der) store = OpenSSL::X509::Store.new trusted_certs.each { |c| store.add_cert(c) } valid = pkcs7.verify(nil, store, signed_data, OpenSSL::PKCS7::DETACHED | OpenSSL::PKCS7::BINARY) VerificationResult.new( signer: pkcs7.signers.first&.issuer&.to_s, valid?: valid, byte_range_ok?: true, cert_chain: pkcs7.certificates || [], trusted?: !trusted_certs.empty? && valid, error: valid ? nil : "signature verification failed", ) rescue OpenSSL::PKCS7::PKCS7Error => e VerificationResult.new(valid?: false, byte_range_ok?: true, cert_chain: [], error: e.) end end |