Class: HexaPDF::DigitalSignature::Signature

Inherits:
HexaPDF::Dictionary show all
Defined in:
lib/hexapdf/digital_signature/signature.rb

Overview

Represents a digital signature that is used to authenticate a user and the contents of the document.

Signature Verification

Verification of signatures is a complex topic and what counts as completely verified may differ from use-case to use-case. Therefore HexaPDF provides as much diagnostic information as possible so that the user can decide whether a signature is valid.

By defining a custom signature handler based on BaseHandler or CMSHandler one is able to also customize the signature verification.

See: PDF2.0 s12.8.1, HexaPDF::Type::AcroForm::SignatureField

Defined Under Namespace

Classes: SignatureReference, TransformParams

Constant Summary

Constants included from HexaPDF::DictionaryFields

HexaPDF::DictionaryFields::Boolean, HexaPDF::DictionaryFields::PDFByteString, HexaPDF::DictionaryFields::PDFDate

Instance Attribute Summary

Attributes inherited from Object

#data, #document, #must_be_indirect

Instance Method Summary collapse

Methods inherited from HexaPDF::Dictionary

#[], #[]=, define_field, define_type, #delete, #each, each_field, #empty?, field, #key?, #to_hash, type, #type

Methods inherited from Object

#<=>, #==, #cache, #cached?, #clear_cache, deep_copy, #deep_copy, #document?, #eql?, field, #gen, #gen=, #hash, #indirect?, #initialize, #inspect, make_direct, #must_be_indirect?, #null?, #oid, #oid=, #type, #validate, #value, #value=

Constructor Details

This class inherits a constructor from HexaPDF::Object

Instance Method Details

#contentsObject

Returns the raw signature value.



201
202
203
# File 'lib/hexapdf/digital_signature/signature.rb', line 201

def contents
  self[:Contents]
end

#signature_handlerObject

Returns the signature handler for this signature based on the /SubFilter entry.



191
192
193
194
195
196
197
198
# File 'lib/hexapdf/digital_signature/signature.rb', line 191

def signature_handler
  cache(:signature_handler) do
    handler_class = document.config.constantize('signature.sub_filter_map', self[:SubFilter]) do
      raise HexaPDF::Error, "No or unknown signature handler set: #{self[:SubFilter]}"
    end
    handler_class.new(self)
  end
end

#signature_typeObject

Returns the signature type based on the /SubFilter.



186
187
188
# File 'lib/hexapdf/digital_signature/signature.rb', line 186

def signature_type
  self[:SubFilter].to_s
end

#signed_dataObject

Returns the signed data as indicated by the /ByteRange entry as binary string.



206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/hexapdf/digital_signature/signature.rb', line 206

def signed_data
  unless document.revisions.parser
    raise HexaPDF::Error, "Can't load signed data without existing PDF file"
  end
  io = document.revisions.parser.io
  data = ''.b
  self[:ByteRange]&.each_slice(2) do |offset, length|
    io.pos = offset
    data << io.read(length).to_s
  end
  data
end

#signer_nameObject

Returns the name of the person or authority that signed the document.



166
167
168
# File 'lib/hexapdf/digital_signature/signature.rb', line 166

def signer_name
  signature_handler.signer_name
end

#signing_locationObject

Returns the location of the signing.



181
182
183
# File 'lib/hexapdf/digital_signature/signature.rb', line 181

def signing_location
  self[:Location]
end

#signing_reasonObject

Returns the reason for the signing.



176
177
178
# File 'lib/hexapdf/digital_signature/signature.rb', line 176

def signing_reason
  self[:Reason]
end

#signing_timeObject

Returns the time of the signing.



171
172
173
# File 'lib/hexapdf/digital_signature/signature.rb', line 171

def signing_time
  signature_handler.signing_time
end

#verify(default_paths: true, trusted_certs: [], allow_self_signed: false) ⇒ Object

Returns a VerificationResult object with the verification information.



220
221
222
223
224
225
226
# File 'lib/hexapdf/digital_signature/signature.rb', line 220

def verify(default_paths: true, trusted_certs: [], allow_self_signed: false)
  store = OpenSSL::X509::Store.new
  store.set_default_paths if default_paths
  store.purpose = OpenSSL::X509::PURPOSE_SMIME_SIGN
  trusted_certs.each {|cert| store.add_cert(cert) }
  signature_handler.verify(store, allow_self_signed: allow_self_signed)
end