hexapdf 0.28.0 → 0.31.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +86 -10
  3. data/examples/024-digital-signatures.rb +23 -0
  4. data/lib/hexapdf/cli/command.rb +16 -1
  5. data/lib/hexapdf/cli/info.rb +9 -1
  6. data/lib/hexapdf/cli/inspect.rb +2 -2
  7. data/lib/hexapdf/composer.rb +76 -28
  8. data/lib/hexapdf/configuration.rb +29 -16
  9. data/lib/hexapdf/dictionary_fields.rb +13 -4
  10. data/lib/hexapdf/digital_signature/cms_handler.rb +137 -0
  11. data/lib/hexapdf/digital_signature/handler.rb +138 -0
  12. data/lib/hexapdf/digital_signature/pkcs1_handler.rb +96 -0
  13. data/lib/hexapdf/{type → digital_signature}/signature.rb +3 -8
  14. data/lib/hexapdf/digital_signature/signatures.rb +210 -0
  15. data/lib/hexapdf/digital_signature/signing/default_handler.rb +317 -0
  16. data/lib/hexapdf/digital_signature/signing/signed_data_creator.rb +308 -0
  17. data/lib/hexapdf/digital_signature/signing/timestamp_handler.rb +148 -0
  18. data/lib/hexapdf/digital_signature/signing.rb +101 -0
  19. data/lib/hexapdf/{type/signature → digital_signature}/verification_result.rb +37 -41
  20. data/lib/hexapdf/digital_signature.rb +56 -0
  21. data/lib/hexapdf/document/pages.rb +31 -18
  22. data/lib/hexapdf/document.rb +29 -15
  23. data/lib/hexapdf/encryption/standard_security_handler.rb +4 -3
  24. data/lib/hexapdf/filter/flate_decode.rb +20 -8
  25. data/lib/hexapdf/layout/page_style.rb +144 -0
  26. data/lib/hexapdf/layout.rb +1 -0
  27. data/lib/hexapdf/task/optimize.rb +8 -6
  28. data/lib/hexapdf/type/font_simple.rb +14 -2
  29. data/lib/hexapdf/type/object_stream.rb +7 -2
  30. data/lib/hexapdf/type/outline.rb +1 -1
  31. data/lib/hexapdf/type/outline_item.rb +1 -1
  32. data/lib/hexapdf/type/page.rb +29 -8
  33. data/lib/hexapdf/type/xref_stream.rb +11 -4
  34. data/lib/hexapdf/type.rb +0 -1
  35. data/lib/hexapdf/version.rb +1 -1
  36. data/lib/hexapdf/writer.rb +1 -1
  37. data/test/hexapdf/{type/signature → digital_signature}/common.rb +31 -3
  38. data/test/hexapdf/digital_signature/signing/test_default_handler.rb +162 -0
  39. data/test/hexapdf/digital_signature/signing/test_signed_data_creator.rb +225 -0
  40. data/test/hexapdf/digital_signature/signing/test_timestamp_handler.rb +88 -0
  41. data/test/hexapdf/{type/signature/test_adbe_pkcs7_detached.rb → digital_signature/test_cms_handler.rb} +7 -7
  42. data/test/hexapdf/{type/signature → digital_signature}/test_handler.rb +4 -4
  43. data/test/hexapdf/{type/signature/test_adbe_x509_rsa_sha1.rb → digital_signature/test_pkcs1_handler.rb} +3 -3
  44. data/test/hexapdf/{type → digital_signature}/test_signature.rb +7 -7
  45. data/test/hexapdf/digital_signature/test_signatures.rb +137 -0
  46. data/test/hexapdf/digital_signature/test_signing.rb +53 -0
  47. data/test/hexapdf/{type/signature → digital_signature}/test_verification_result.rb +7 -7
  48. data/test/hexapdf/document/test_pages.rb +25 -0
  49. data/test/hexapdf/encryption/test_standard_security_handler.rb +2 -2
  50. data/test/hexapdf/filter/test_flate_decode.rb +19 -5
  51. data/test/hexapdf/layout/test_page_style.rb +70 -0
  52. data/test/hexapdf/task/test_optimize.rb +11 -9
  53. data/test/hexapdf/test_composer.rb +35 -10
  54. data/test/hexapdf/test_dictionary_fields.rb +9 -3
  55. data/test/hexapdf/test_document.rb +1 -1
  56. data/test/hexapdf/test_writer.rb +8 -8
  57. data/test/hexapdf/type/test_font_simple.rb +18 -6
  58. data/test/hexapdf/type/test_object_stream.rb +16 -7
  59. data/test/hexapdf/type/test_outline.rb +3 -1
  60. data/test/hexapdf/type/test_outline_item.rb +3 -1
  61. data/test/hexapdf/type/test_page.rb +42 -11
  62. data/test/hexapdf/type/test_xref_stream.rb +6 -1
  63. metadata +27 -15
  64. data/lib/hexapdf/document/signatures.rb +0 -546
  65. data/lib/hexapdf/type/signature/adbe_pkcs7_detached.rb +0 -135
  66. data/lib/hexapdf/type/signature/adbe_x509_rsa_sha1.rb +0 -95
  67. data/lib/hexapdf/type/signature/handler.rb +0 -140
  68. data/test/hexapdf/document/test_signatures.rb +0 -352
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 874e09b094ea4e793d1d123cfbaded6d1cc5ba93af3b57587e9faf402786a30f
4
- data.tar.gz: c1eed6a778936cd360b4f1878a18abc3e5727c1f36b5eab4838a9a72817dff7b
3
+ metadata.gz: 7a32d8f7558ea14ae5dc40849c690b79f5ed4a797cffb85253e7fd6d00c54736
4
+ data.tar.gz: 92713168ee64efc59f86ff1d4f8989054ab02653ccaf84795aed3fea02a4811d
5
5
  SHA512:
6
- metadata.gz: b66a7587a239acbeb9ebbb20f851b2fa7738c5a4c2f8a95ae7ae3d7419b84ae37b5d1fb48a6b7023bff0df3e1728c395b5351c9c42c05707fabd5a1722e2b88a
7
- data.tar.gz: 62ac7d070bb8ae3426685af497a047096dd32dc16a102baa961512652222b388198561776fc1227be69bdd0713183d91fa25e411262eec34a29227aae9723d5c
6
+ metadata.gz: 43b05643d9a59e9656a464eda221fa28e04bb4fe8ba1f97d0e31e434fbce64ffb9ed551689ce68957c8b4a7b82fbc85ecd91c1eac6d177b0109d4b9b5d275a6c
7
+ data.tar.gz: 780e7051327b0463e800ca4ea3b9f1ac1c3723d960154a2ac27c9e9bed867d0a257c48c20762cd0947f8638dc1f72b598a33ff76e1f0d1d2b0f1ff843497eb29
data/CHANGELOG.md CHANGED
@@ -1,3 +1,79 @@
1
+ ## 0.31.0 - 2023-02-22
2
+
3
+ ### Added
4
+
5
+ * [HexaPDF::Layout::PageStyle] for collecting all styling information for pages
6
+ * [HexaPDF::Composer#page_style] for configuring different page styles
7
+
8
+ ### Changed
9
+
10
+ * **Breaking change**: [HexaPDF::Composer] uses page styles underneath
11
+ * **Breaking change**: Configuration options `filter.flate_compression` and
12
+ `filter.flate_memory` are changed to `filter.flate.compression` and
13
+ `filter.flate.memory`
14
+ * **Breaking change**: [HexaPDF::Document#wrap] handles cross-reference and
15
+ object stream specially to avoid problems with invalid PDFs
16
+ * [HexaPDF::Composer::new] to allow skipping the initial page creation
17
+ * CLI command `hexapdf info --check` to process streams to reveal stream errors
18
+ * CLI commands to output the name of created PDF files in verbose mode
19
+
20
+ ### Fixed
21
+
22
+ * Validation of document outline items in case the first or last item got
23
+ deleted
24
+ * `HexaPDF::Type::Page#perform_validation` to set a /MediaBox for invalid pages
25
+ that don't have one
26
+
27
+
28
+ ## 0.30.0 - 2023-02-13
29
+
30
+ ### Added
31
+
32
+ * [HexaPDF::Document::Pages#create] for creating a page object without adding it
33
+ to the page tree
34
+
35
+ ### Changed
36
+
37
+ * `HexaPDF::Type::FontSimple#perform_validation` to correct /Widths fields in
38
+ case it has an invalid number of entries
39
+
40
+ ### Fixed
41
+
42
+ * [HexaPDF::DictionaryFields::DateConverter] to handle invalid months, day,
43
+ hour, minute and second values
44
+
45
+
46
+ ## 0.29.0 - 2023-01-30
47
+
48
+ ### Added
49
+
50
+ * [HexaPDF::DigitalSignature::Signing::SignedDataCreator] for creating custom
51
+ CMS signed data objects
52
+
53
+ ### Changed
54
+
55
+ * **Breaking change**: Refactored digital signature support and moved all
56
+ related code under the [HexaPDF::DigitalSignature] module
57
+ * **Breaking change**: New external signing mode without the need for creating
58
+ the PKCS#7/CMS signed data object for
59
+ [HexaPDF::DigitalSignature::Signing::DefaultHandler]
60
+ * **Breaking change**: Use value :pades instead of :etsi for
61
+ [HexaPDF::DigitalSignature::Signing::DefaultHandler#signature_type]
62
+ * [HexaPDF::DigitalSignature::Signing::DefaultHandler] to allow creating PAdES
63
+ level B-B and B-T signatures
64
+ * [HexaPDF::DigitalSignature::Signing::DefaultHandler] to allow specifying the
65
+ used digest algorithm
66
+ * [HexaPDF::DigitalSignature::Signing::DefaultHandler] to allow specifying a
67
+ timestamp handler for including a timestamp token in the signature
68
+ * Moved setting of signature entries /Filter, /SubFilter and /M fields to the
69
+ signing handlers
70
+
71
+ ### Fixed
72
+
73
+ * [HexaPDF::DictionaryFields::DateConverter] to handle invalid timezone hour and
74
+ minute values
75
+
76
+
1
77
  ## 0.28.0 - 2022-12-30
2
78
 
3
79
  ### Added
@@ -61,30 +137,30 @@
61
137
  ### Added
62
138
 
63
139
  * Support for timestamp signatures through the
64
- [HexaPDF::Document::Signatures::TimestampHandler]
140
+ `HexaPDF::Document::Signatures::TimestampHandler`
65
141
  * [HexaPDF::Document::Destinations#resolve] for resolving destination values
66
142
  * [HexaPDF::Document::Destinations::Destination#value] to return the destination
67
143
  array
68
144
  * Support for verifying document timestamp signatures
69
- * [HexaPDF::Document::Signatures::DefaultHandler#signature_size] to support
145
+ * `HexaPDF::Document::Signatures::DefaultHandler#signature_size` to support
70
146
  setting custom signature sizes
71
- * [HexaPDF::Document::Signatures::DefaultHandler#external_signing] to support
147
+ * `HexaPDF::Document::Signatures::DefaultHandler#external_signing` to support
72
148
  signing via custom mechanisms
73
- * [HexaPDF::Document::Signatures::embed_signature] to enable asynchronous
149
+ * `HexaPDF::Document::Signatures::embed_signature` to enable asynchronous
74
150
  external signing
75
151
 
76
152
  ### Changed
77
153
 
78
154
  * **Breaking change**: The crop box is now used instead of the media box in most
79
155
  cases to be in line with the specification
80
- * [HexaPDF::Document::Signatures::DefaultHandler] to allow setting the used
156
+ * `HexaPDF::Document::Signatures::DefaultHandler` to allow setting the used
81
157
  signature method
82
- * **Breaking change**: [HexaPDF::Document::Signatures::DefaultHandler#sign]
158
+ * **Breaking change**: `HexaPDF::Document::Signatures::DefaultHandler#sign`
83
159
  needs to accept the IO object and the byte range instead of just the data
84
160
  * **Breaking change**: Enhanced support for outline items with new methods
85
161
  `#level` and `#destination_page` as well as changes to `#add` and `#each_item`
86
162
  * **Breaking change**: Removed `#filter_name` and `#sub_filter_name` from
87
- [HexaPDF::Document::Signatures::DefaultHandler]
163
+ `HexaPDF::Document::Signatures::DefaultHandler`
88
164
  * `HexaPDF::Type::Resources#perform_validation` to not add a default procedure
89
165
  set since this feature is deprecated
90
166
 
@@ -101,7 +177,7 @@
101
177
  * [HexaPDF::Type::OutlineItem] to always be an indirect object
102
178
  * `HexaPDF::Tokenizer#parse_number` to handle references correctly in all cases
103
179
  * [HexaPDF::Type::Page#rotate] to correctly flatten all page boxes
104
- * [HexaPDF::Document::Signatures#add] to raise an error if the reserved space
180
+ * `HexaPDF::Document::Signatures#add` to raise an error if the reserved space
105
181
  for the signature is not enough
106
182
  * `HexaPDF::Type::AcroForm::Form#perform_validation` to fix broken /Parent
107
183
  entries and to remove invalid objects from the field hierarchy
@@ -276,7 +352,7 @@
276
352
  moved node doesn't change
277
353
  * [HexaPDF::Type::PageTreeNode#move_page] to use the correct target position
278
354
  when the moved node is before the target position
279
- * [HexaPDF::Document::Signatures#add] to work in case the signature object is
355
+ * `HexaPDF::Document::Signatures#add` to work in case the signature object is
280
356
  the last object written
281
357
  * CLI command `hexapdf inspect` to show correct byte range of the last revision
282
358
  * [HexaPDF::Writer#write_incremental] to only use a cross-reference stream if a
@@ -285,7 +361,7 @@
285
361
  disabled
286
362
  * [HexaPDF::Font::Encoding::GlyphList] to use binary reading to avoid problems
287
363
  on Windows
288
- * [HexaPDF::Document::Signatures#add] to use binary writing to avoid problems on
364
+ * `HexaPDF::Document::Signatures#add` to use binary writing to avoid problems on
289
365
  Windows
290
366
 
291
367
 
@@ -0,0 +1,23 @@
1
+ # # Images
2
+ #
3
+ # This example shows how to embed images into a PDF document, directly on a
4
+ # page's canvas and through the high-level [HexaPDF::Composer].
5
+ #
6
+ # Usage:
7
+ # : `ruby digital-signatures.rb`
8
+ #
9
+
10
+ require 'hexapdf'
11
+ require HexaPDF.data_dir + '/cert/demo_cert.rb'
12
+
13
+ doc = if ARGV[0]
14
+ HexaPDF::Document.open(ARGV[0])
15
+ else
16
+ HexaPDF::Document.new.pages.add.document
17
+ end
18
+ doc.sign("digital-signatures.pdf",
19
+ reason: 'Some reason',
20
+ certificate: HexaPDF.demo_cert.cert,
21
+ key: HexaPDF.demo_cert.key,
22
+ certificate_chain: [HexaPDF.demo_cert.sub_ca,
23
+ HexaPDF.demo_cert.root_ca])
@@ -101,6 +101,17 @@ module HexaPDF
101
101
  def pdf_options(password)
102
102
  hash = {decryption_opts: {password: password}, config: {}}
103
103
  HexaPDF::GlobalConfiguration['filter.predictor.strict'] = command_parser.strict
104
+ HexaPDF::GlobalConfiguration['filter.flate.on_error'] =
105
+ if command_parser.strict
106
+ proc { true }
107
+ else
108
+ proc do |_, error|
109
+ if command_parser.verbosity_info?
110
+ $stderr.puts "Ignoring error in flate encoded stream: #{error}"
111
+ end
112
+ false
113
+ end
114
+ end
104
115
  hash[:config]['parser.try_xref_reconstruction'] = !command_parser.strict
105
116
  hash[:config]['parser.on_correctable_error'] =
106
117
  if command_parser.strict
@@ -120,6 +131,7 @@ module HexaPDF
120
131
  # Writes the document to the given file or does nothing if +out_file+ is +nil+.
121
132
  def write_document(doc, out_file, incremental: false)
122
133
  if out_file
134
+ doc.trailer.update_id
123
135
  doc.validate(auto_correct: true) do |msg, correctable, object|
124
136
  if command_parser.strict && !correctable
125
137
  raise "Validation error for object (#{object.oid},#{object.gen}): #{msg}"
@@ -128,6 +140,9 @@ module HexaPDF
128
140
  "for object (#{object.oid},#{object.gen}): #{msg}"
129
141
  end
130
142
  end
143
+ if command_parser.verbosity_info?
144
+ puts "Creating output document #{out_file}"
145
+ end
131
146
  doc.write(out_file, validate: false, incremental: incremental)
132
147
  end
133
148
  end
@@ -331,7 +346,7 @@ module HexaPDF
331
346
  rotation = ROTATE_MAP[$4]
332
347
  start_nr.step(to: end_nr, by: step) {|n| arr << [n, rotation] }
333
348
  else
334
- raise OptionParser::InvalidArgument, "invalid page range format: #{str}"
349
+ raise OptionParser::InvalidArgument, "invalid page range format: #{str.inspect}"
335
350
  end
336
351
  end
337
352
  end
@@ -106,6 +106,13 @@ module HexaPDF
106
106
  doc.each(only_loaded: false) do |obj|
107
107
  indirect_object = obj
108
108
  obj.validate(auto_correct: true, &validation_block)
109
+ if obj.data.stream
110
+ begin
111
+ obj.stream
112
+ rescue StandardError
113
+ puts "ERROR: Stream of object (#{obj.oid},#{obj.gen}) invalid: #{$!.message}"
114
+ end
115
+ end
109
116
  end
110
117
  end
111
118
 
@@ -177,7 +184,8 @@ module HexaPDF
177
184
  def pdf_options(password)
178
185
  if @check_file
179
186
  options = {decryption_opts: {password: password}, config: {}}
180
- HexaPDF::GlobalConfiguration['filter.predictor.strict'] = false
187
+ HexaPDF::GlobalConfiguration['filter.predictor.strict'] = true
188
+ HexaPDF::GlobalConfiguration['filter.flate.on_error'] = proc { true }
181
189
  options[:config]['parser.try_xref_reconstruction'] = true
182
190
  options[:config]['parser.on_correctable_error'] = lambda do |_, msg, pos|
183
191
  puts "WARNING: Parse error at position #{pos}: #{msg}"
@@ -166,8 +166,8 @@ module HexaPDF
166
166
  when 'p', 'pages'
167
167
  begin
168
168
  pages = parse_pages_specification(data.shift || '1-e', @doc.pages.count)
169
- rescue StandardError
170
- $stderr.puts("Error: Invalid page range argument")
169
+ rescue StandardError => e
170
+ $stderr.puts("Error: #{e}")
171
171
  next
172
172
  end
173
173
  page_list = @doc.pages.to_a
@@ -106,6 +106,14 @@ module HexaPDF
106
106
 
107
107
  # Creates a new Composer object and optionally yields it to the given block.
108
108
  #
109
+ # skip_page_creation::
110
+ # If this argument is +true+ (the default), the arguments +page_size+, +page_orientation+
111
+ # and +margin+ are used to create a page style with the name :default and an initial page is
112
+ # created as well.
113
+ #
114
+ # Otherwise, i.e. when this argument is +false+, no initial page or default page style is
115
+ # created. This has to be done manually using the #page_style and #new_page methods.
116
+ #
109
117
  # page_size::
110
118
  # Can be any valid predefined page size (see Type::Page::PAPER_SIZE) or an array [llx, lly,
111
119
  # urx, ury] specifying a custom page size.
@@ -120,36 +128,53 @@ module HexaPDF
120
128
  # Example:
121
129
  #
122
130
  # composer = HexaPDF::Composer.new # uses the default values
131
+ #
123
132
  # HexaPDF::Composer.new(page_size: :Letter, margin: 72) do |composer|
124
133
  # #...
125
134
  # end
126
- def initialize(page_size: :A4, page_orientation: :portrait, margin: 36) #:yields: composer
135
+ #
136
+ # HexaPDF::Composer.new(skip_page_creation: true) do |composer|
137
+ # page_template = lambda {|canvas, style| style.create_frame(canvas.context, 36) }
138
+ # page_style(:default, template: page_template)
139
+ # new_page
140
+ # # ...
141
+ # end
142
+ def initialize(skip_page_creation: false, page_size: :A4, page_orientation: :portrait,
143
+ margin: 36) #:yields: composer
127
144
  @document = HexaPDF::Document.new
128
- @page_size = page_size
129
- @page_orientation = page_orientation
130
- @margin = Layout::Style::Quad.new(margin)
131
-
132
- new_page
145
+ @page_styles = {}
146
+ @page_style = :default
147
+ unless skip_page_creation
148
+ page_style(:default, page_size: page_size, orientation: page_orientation) do |canvas, style|
149
+ style.frame = style.create_frame(canvas.context, margin)
150
+ end
151
+ new_page
152
+ end
133
153
  yield(self) if block_given?
134
154
  end
135
155
 
136
156
  # Creates a new page, making it the current one.
137
157
  #
138
- # If any of +page_size+, +page_orientation+ or +margin+ are set, they will be used instead of
139
- # the default values and will become the default values.
158
+ # The page style to use for the new page can be set via the +style+ argument. If not provided,
159
+ # the currently set page style is used.
160
+ #
161
+ # The used page style determines the page style that should be used for the following new pages.
162
+ # If this information is not provided, the used page style is used again.
140
163
  #
141
164
  # Examples:
142
165
  #
143
- # composer.new_page # uses the default values
144
- # composer.new_page(page_size: :A5, margin: [72, 36])
145
- def new_page(page_size: nil, page_orientation: nil, margin: nil)
146
- @page_size = page_size if page_size
147
- @page_orientation = page_orientation if page_orientation
148
- @margin = Layout::Style::Quad.new(margin) if margin
149
-
150
- @page = @document.pages.add(@page_size, orientation: @page_orientation)
166
+ # composer.page_style(:cover, page_size: :A4).next_style = :content
167
+ # composer.page_style(:content, page_size: :A4)
168
+ # composer.new_page(:cover) # uses the :cover style, set next style to :content
169
+ # composer.new_page # uses the :content style, next style again :content
170
+ def new_page(style = @page_style)
171
+ page_style = @page_styles.fetch(style) do |key|
172
+ raise ArgumentError, "Page style #{key} has not been defined"
173
+ end
174
+ @page = @document.pages.add(page_style.create_page(@document))
151
175
  @canvas = @page.canvas
152
- create_frame
176
+ @frame = page_style.frame
177
+ @page_style = page_style.next_style || style
153
178
  end
154
179
 
155
180
  # The x-position of the cursor inside the current frame.
@@ -190,6 +215,40 @@ module HexaPDF
190
215
  @document.layout.style(name, base: base, **properties)
191
216
  end
192
217
 
218
+ # :call-seq:
219
+ # composer.page_style(name) -> page_style
220
+ # composer.page_style(name, **attributes, &template_block) -> page_style
221
+ #
222
+ # Creates and/or returns the page style +name+.
223
+ #
224
+ # If no attributes are given, the page style +name+ is returned. In case it does not exist,
225
+ # +nil+ is returned.
226
+ #
227
+ # If one or more page style attributes are given, a new HexaPDF::Layout::PageStyle object with
228
+ # those attribute values is created, stored under +name+ and returned. If a block is provided,
229
+ # it is used to define the page template.
230
+ #
231
+ # Example:
232
+ #
233
+ # composer.page_style(:default)
234
+ # composer.page_style(:cover, page_size: :A4) do |canvas, style|
235
+ # page_box = canvas.context.box
236
+ # canvas.fill_color("fd0") do
237
+ # canvas.rectangle(0, 0, page_box.width, page_box.height).
238
+ # fill
239
+ # end
240
+ # style.frame = style.create_frame(canvas.context, 36)
241
+ # end
242
+ #
243
+ # See: HexaPDF::Layout::PageStyle
244
+ def page_style(name, **attributes, &block)
245
+ if attributes.empty?
246
+ @page_styles[name]
247
+ else
248
+ @page_styles[name] = HexaPDF::Layout::PageStyle.new(**attributes, &block)
249
+ end
250
+ end
251
+
193
252
  # Draws the given text at the current position into the current frame.
194
253
  #
195
254
  # The text will be positioned at the current position if possible. Otherwise the next best
@@ -325,17 +384,6 @@ module HexaPDF
325
384
  stamp
326
385
  end
327
386
 
328
- private
329
-
330
- # Creates the frame into which boxes are layed out when a new page is created.
331
- def create_frame
332
- media_box = @page.box
333
- @frame = Layout::Frame.new(media_box.left + @margin.left,
334
- media_box.bottom + @margin.bottom,
335
- media_box.width - @margin.left - @margin.right,
336
- media_box.height - @margin.bottom - @margin.top)
337
- end
338
-
339
387
  end
340
388
 
341
389
  end
@@ -350,6 +350,9 @@ module HexaPDF
350
350
  # The media box that is used for new pages that don't define a media box. Default value is
351
351
  # A4. See HexaPDF::Type::Page::PAPER_SIZE for a list of predefined paper sizes.
352
352
  #
353
+ # This configuration option (together with 'page.default_media_orientation') is also used when
354
+ # validating pages and a page without a media box is found.
355
+ #
353
356
  # The value can either be a rectangle defining the paper size or a Symbol referencing one of
354
357
  # the predefined paper sizes.
355
358
  #
@@ -393,8 +396,8 @@ module HexaPDF
393
396
  #
394
397
  # signature.sub_filter_map::
395
398
  # A mapping from a PDF name (a Symbol) to a signature handler class (see
396
- # HexaPDF::Type::Signature::Handler). If the value is a String, it should contain the name of a
397
- # constant to such a class.
399
+ # HexaPDF::DigitalSignature::Handler). If the value is a String, it should contain the name of
400
+ # a constant to such a class.
398
401
  #
399
402
  # The sub filter map is used for mapping specific signature algorithms to handler classes. The
400
403
  # filter value of a signature dictionary is ignored since we only support the standard
@@ -486,14 +489,14 @@ module HexaPDF
486
489
  link: 'HexaPDF::Layout::Style::LinkLayer',
487
490
  },
488
491
  'signature.signing_handler' => {
489
- default: 'HexaPDF::Document::Signatures::DefaultHandler',
490
- timestamp: 'HexaPDF::Document::Signatures::TimestampHandler',
492
+ default: 'HexaPDF::DigitalSignature::Signing::DefaultHandler',
493
+ timestamp: 'HexaPDF::DigitalSignature::Signing::TimestampHandler',
491
494
  },
492
495
  'signature.sub_filter_map' => {
493
- 'adbe.x509.rsa_sha1': 'HexaPDF::Type::Signature::AdbeX509RsaSha1',
494
- 'adbe.pkcs7.detached': 'HexaPDF::Type::Signature::AdbePkcs7Detached',
495
- 'ETSI.CAdES.detached': 'HexaPDF::Type::Signature::AdbePkcs7Detached',
496
- 'ETSI.RFC3161': 'HexaPDF::Type::Signature::AdbePkcs7Detached',
496
+ 'adbe.x509.rsa_sha1': 'HexaPDF::DigitalSignature::PKCS1Handler',
497
+ 'adbe.pkcs7.detached': 'HexaPDF::DigitalSignature::CMSHandler',
498
+ 'ETSI.CAdES.detached': 'HexaPDF::DigitalSignature::CMSHandler',
499
+ 'ETSI.RFC3161': 'HexaPDF::DigitalSignature::CMSHandler',
497
500
  },
498
501
  'task.map' => {
499
502
  optimize: 'HexaPDF::Task::Optimize',
@@ -511,11 +514,20 @@ module HexaPDF
511
514
  #
512
515
  # See PDF1.7 s8.6
513
516
  #
514
- # filter.flate_compression::
517
+ # filter.flate.compression::
515
518
  # Specifies the compression level that should be used with the FlateDecode filter. The level
516
519
  # can range from 0 (no compression), 1 (best speed) to 9 (best compression, default).
517
520
  #
518
- # filter.flate_memory::
521
+ # filter.flate.on_error::
522
+ # Callback hook when a potentially recoverable Zlib error occurs in the FlateDecode filter.
523
+ #
524
+ # The value needs to be an object that responds to \#call(stream, error) where stream is the
525
+ # Zlib stream object and error is the thrown error. The method needs to return +true+ if an
526
+ # error should be raised.
527
+ #
528
+ # The default implementation prevents errors from being raised.
529
+ #
530
+ # filter.flate.memory::
519
531
  # Specifies the memory level that should be used with the FlateDecode filter. The level can
520
532
  # range from 1 (minimum memory usage; slow, reduces compression) to 9 (maximum memory usage).
521
533
  #
@@ -543,8 +555,9 @@ module HexaPDF
543
555
  # This mapping is used to provide automatic wrapping of objects in the HexaPDF::Document#wrap
544
556
  # method.
545
557
  GlobalConfiguration =
546
- Configuration.new('filter.flate_compression' => 9,
547
- 'filter.flate_memory' => 6,
558
+ Configuration.new('filter.flate.compression' => 9,
559
+ 'filter.flate.on_error' => proc { false },
560
+ 'filter.flate.memory' => 6,
548
561
  'filter.predictor.strict' => false,
549
562
  'color_space.map' => {
550
563
  DeviceRGB: 'HexaPDF::Content::ColorSpace::DeviceRGB',
@@ -583,10 +596,10 @@ module HexaPDF
583
596
  SigFieldLock: 'HexaPDF::Type::AcroForm::SignatureField::LockDictionary',
584
597
  SV: 'HexaPDF::Type::AcroForm::SignatureField::SeedValueDictionary',
585
598
  SVCert: 'HexaPDF::Type::AcroForm::SignatureField::CertificateSeedValueDictionary',
586
- Sig: 'HexaPDF::Type::Signature',
587
- DocTimeStamp: 'HexaPDF::Type::Signature',
588
- SigRef: 'HexaPDF::Type::Signature::SignatureReference',
589
- TransformParams: 'HexaPDF::Type::Signature::TransformParams',
599
+ Sig: 'HexaPDF::DigitalSignature::Signature',
600
+ DocTimeStamp: 'HexaPDF::DigitalSignature::Signature',
601
+ SigRef: 'HexaPDF::DigitalSignature::Signature::SignatureReference',
602
+ TransformParams: 'HexaPDF::DigitalSignature::Signature::TransformParams',
590
603
  Outlines: 'HexaPDF::Type::Outline',
591
604
  XXOutlineItem: 'HexaPDF::Type::OutlineItem',
592
605
  PageLabel: 'HexaPDF::Type::PageLabel',
@@ -293,16 +293,25 @@ module HexaPDF
293
293
  end
294
294
 
295
295
  # :nodoc:
296
- DATE_RE = /\AD:(\d{4})(\d\d)?(\d\d)?(\d\d)?(\d\d)?(\d\d)?([Z+-])?(?:(\d\d)(?:'|'([0-5]\d)'?|\z)?)?\z/n
296
+ DATE_RE = /\AD:(\d{4})(\d\d)?(\d\d)?(\d\d)?(\d\d)?(\d\d)?([Z+-])?(?:(\d+)(?:'|'(\d+)'?|\z)?)?\z/n
297
297
 
298
298
  # Checks if the given object is a string and converts into a Time object if possible.
299
299
  # Otherwise returns +nil+.
300
300
  def self.convert(str, _type, _document)
301
301
  return unless str.kind_of?(String) && (m = str.match(DATE_RE))
302
302
 
303
- utc_offset = (m[7].nil? || m[7] == 'Z' ? 0 : "#{m[7]}#{m[8]}:#{m[9] || '00'}")
304
- Time.new(m[1].to_i, (m[2] ? m[2].to_i : 1), (m[3] ? m[3].to_i : 1),
305
- m[4].to_i, m[5].to_i, m[6].to_i, utc_offset)
303
+ utc_offset = if m[7].nil? || m[7] == 'Z'
304
+ 0
305
+ else
306
+ (m[7] == '-' ? -1 : 1) * (m[8].to_i * 3600 + m[9].to_i * 60).clamp(0, 86399)
307
+ end
308
+ begin
309
+ Time.new(m[1].to_i, (m[2] ? m[2].to_i : 1), (m[3] ? m[3].to_i : 1),
310
+ m[4].to_i, m[5].to_i, m[6].to_i, utc_offset)
311
+ rescue ArgumentError
312
+ Time.new(m[1].to_i, m[2].to_i.clamp(1, 12), m[3].to_i.clamp(1, 31),
313
+ m[4].to_i.clamp(0, 23), m[5].to_i.clamp(0, 59), m[6].to_i.clamp(0, 59), utc_offset)
314
+ end
306
315
  end
307
316
 
308
317
  end
@@ -0,0 +1,137 @@
1
+ # -*- encoding: utf-8; frozen_string_literal: true -*-
2
+ #
3
+ #--
4
+ # This file is part of HexaPDF.
5
+ #
6
+ # HexaPDF - A Versatile PDF Creation and Manipulation Library For Ruby
7
+ # Copyright (C) 2014-2022 Thomas Leitner
8
+ #
9
+ # HexaPDF is free software: you can redistribute it and/or modify it
10
+ # under the terms of the GNU Affero General Public License version 3 as
11
+ # published by the Free Software Foundation with the addition of the
12
+ # following permission added to Section 15 as permitted in Section 7(a):
13
+ # FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
14
+ # THOMAS LEITNER, THOMAS LEITNER DISCLAIMS THE WARRANTY OF NON
15
+ # INFRINGEMENT OF THIRD PARTY RIGHTS.
16
+ #
17
+ # HexaPDF is distributed in the hope that it will be useful, but WITHOUT
18
+ # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19
+ # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
20
+ # License for more details.
21
+ #
22
+ # You should have received a copy of the GNU Affero General Public License
23
+ # along with HexaPDF. If not, see <http://www.gnu.org/licenses/>.
24
+ #
25
+ # The interactive user interfaces in modified source and object code
26
+ # versions of HexaPDF must display Appropriate Legal Notices, as required
27
+ # under Section 5 of the GNU Affero General Public License version 3.
28
+ #
29
+ # In accordance with Section 7(b) of the GNU Affero General Public
30
+ # License, a covered work must retain the producer line in every PDF that
31
+ # is created or manipulated using HexaPDF.
32
+ #
33
+ # If the GNU Affero General Public License doesn't fit your need,
34
+ # commercial licenses are available at <https://gettalong.at/hexapdf/>.
35
+ #++
36
+
37
+ require 'openssl'
38
+ require 'hexapdf/digital_signature/handler'
39
+
40
+ module HexaPDF
41
+ module DigitalSignature
42
+
43
+ # The signature handler for PKCS#7 a.k.a. CMS signatures. Those include, for example, the
44
+ # adbe.pkcs7.detached sub-filter.
45
+ #
46
+ # See: PDF1.7/2.0 s12.8.3.3
47
+ class CMSHandler < Handler
48
+
49
+ # Creates a new signature handler for the given signature dictionary.
50
+ def initialize(signature_dict)
51
+ super
52
+ @pkcs7 = OpenSSL::PKCS7.new(signature_dict.contents)
53
+ end
54
+
55
+ # Returns the common name of the signer.
56
+ def signer_name
57
+ signer_certificate.subject.to_a.assoc("CN")&.[](1) || super
58
+ end
59
+
60
+ # Returns the time of signing.
61
+ def signing_time
62
+ signer_info.signed_time rescue super
63
+ end
64
+
65
+ # Returns the certificate chain.
66
+ def certificate_chain
67
+ @pkcs7.certificates
68
+ end
69
+
70
+ # Returns the signer certificate (an instance of OpenSSL::X509::Certificate).
71
+ def signer_certificate
72
+ info = signer_info
73
+ certificate_chain.find {|cert| cert.issuer == info.issuer && cert.serial == info.serial }
74
+ end
75
+
76
+ # Returns the signer information object (an instance of OpenSSL::PKCS7::SignerInfo).
77
+ def signer_info
78
+ @pkcs7.signers.first
79
+ end
80
+
81
+ # Verifies the signature using the provided OpenSSL::X509::Store object.
82
+ def verify(store, allow_self_signed: false)
83
+ result = super
84
+
85
+ signer_info = self.signer_info
86
+ signer_certificate = self.signer_certificate
87
+ certificate_chain = self.certificate_chain
88
+
89
+ if certificate_chain.empty?
90
+ result.log(:error, "No certificates found in signature")
91
+ return result
92
+ end
93
+
94
+ if @pkcs7.signers.size != 1
95
+ result.log(:error, "Exactly one signer needed, found #{@pkcs7.signers.size}")
96
+ end
97
+
98
+ unless signer_certificate
99
+ result.log(:error, "Signer serial=#{signer_info.serial} issuer=#{signer_info.issuer} " \
100
+ "not found in certificates stored in PKCS7 object")
101
+ return result
102
+ end
103
+
104
+ key_usage = signer_certificate.extensions.find {|ext| ext.oid == 'keyUsage' }
105
+ unless key_usage && key_usage.value.split(', ').include?("Digital Signature")
106
+ result.log(:error, "Certificate key usage is missing 'Digital Signature'")
107
+ end
108
+
109
+ if signature_dict.signature_type == 'ETSI.RFC3161'
110
+ # Getting the needed values is not directly supported by Ruby OpenSSL
111
+ p7 = OpenSSL::ASN1.decode(signature_dict.contents.sub(/\x00*\z/, ''))
112
+ signed_data = p7.value[1].value[0]
113
+ content_info = signed_data.value[2]
114
+ content = OpenSSL::ASN1.decode(content_info.value[1].value[0].value)
115
+ digest_algorithm = content.value[2].value[0].value[0].value
116
+ original_hash = content.value[2].value[1].value
117
+ recomputed_hash = OpenSSL::Digest.digest(digest_algorithm, signature_dict.signed_data)
118
+ hash_valid = (original_hash == recomputed_hash)
119
+ else
120
+ data = signature_dict.signed_data
121
+ hash_valid = true # hash will be checked by @pkcs7.verify
122
+ end
123
+ if hash_valid && @pkcs7.verify(certificate_chain, store, data,
124
+ OpenSSL::PKCS7::DETACHED | OpenSSL::PKCS7::BINARY)
125
+ result.log(:info, "Signature valid")
126
+ else
127
+ result.log(:error, "Signature verification failed")
128
+ end
129
+
130
+ result
131
+ end
132
+
133
+ end
134
+
135
+ end
136
+ end
137
+