Class: HexaPDF::Type::Signature

Inherits:
Dictionary show all
Defined in:
lib/hexapdf/type/signature.rb,
lib/hexapdf/type/signature/handler.rb,
lib/hexapdf/type/signature/adbe_x509_rsa_sha1.rb,
lib/hexapdf/type/signature/adbe_pkcs7_detached.rb,
lib/hexapdf/type/signature/verification_result.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 one is able to also customize the signature verification.

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

Defined Under Namespace

Classes: AdbePkcs7Detached, AdbeX509RsaSha1, Handler, SignatureReference, TransformParams, VerificationResult

Constant Summary

Constants included from DictionaryFields

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

Instance Attribute Summary

Attributes inherited from Object

#data, #document, #must_be_indirect

Instance Method Summary collapse

Methods inherited from Dictionary

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

Methods inherited from Object

#<=>, #==, #cache, #cached?, #clear_cache, deep_copy, #deep_copy, #document?, #eql?, #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.



206
207
208
# File 'lib/hexapdf/type/signature.rb', line 206

def contents
  self[:Contents]
end

#signature_handlerObject

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



196
197
198
199
200
201
202
203
# File 'lib/hexapdf/type/signature.rb', line 196

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.



191
192
193
# File 'lib/hexapdf/type/signature.rb', line 191

def signature_type
  self[:SubFilter].to_s
end

#signed_dataObject

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



211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/hexapdf/type/signature.rb', line 211

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)
  end
  data
end

#signer_nameObject

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



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

def signer_name
  signature_handler.signer_name
end

#signing_locationObject

Returns the location of the signing.



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

def signing_location
  self[:Location]
end

#signing_reasonObject

Returns the reason for the signing.



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

def signing_reason
  self[:Reason]
end

#signing_timeObject

Returns the time of the signing.



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

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.



225
226
227
228
229
230
231
# File 'lib/hexapdf/type/signature.rb', line 225

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