pki_express 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +7 -0
  2. data/.gitattributes +3 -0
  3. data/.gitignore +28 -0
  4. data/CHANGELOG.md +2 -0
  5. data/Gemfile +4 -0
  6. data/README.md +24 -0
  7. data/Rakefile +1 -0
  8. data/lib/pki_express.rb +48 -0
  9. data/lib/pki_express/auth_complete_result.rb +22 -0
  10. data/lib/pki_express/auth_start_result.rb +77 -0
  11. data/lib/pki_express/authentication.rb +285 -0
  12. data/lib/pki_express/base_signer.rb +55 -0
  13. data/lib/pki_express/cades_signature_starter.rb +242 -0
  14. data/lib/pki_express/command_error.rb +14 -0
  15. data/lib/pki_express/commands.rb +21 -0
  16. data/lib/pki_express/enum.rb +9 -0
  17. data/lib/pki_express/error_codes.rb +46 -0
  18. data/lib/pki_express/installation_not_found_error.rb +8 -0
  19. data/lib/pki_express/name.rb +48 -0
  20. data/lib/pki_express/pades_horizontal_align.rb +9 -0
  21. data/lib/pki_express/pades_measurement_units.rb +8 -0
  22. data/lib/pki_express/pades_page_optimization.rb +51 -0
  23. data/lib/pki_express/pades_page_orientation.rb +9 -0
  24. data/lib/pki_express/pades_paper_size.rb +21 -0
  25. data/lib/pki_express/pades_signature_starter.rb +232 -0
  26. data/lib/pki_express/pades_size.rb +17 -0
  27. data/lib/pki_express/pades_text_horizontal_align.rb +8 -0
  28. data/lib/pki_express/pades_vertical_align.rb +9 -0
  29. data/lib/pki_express/pades_visual_auto_positioning.rb +22 -0
  30. data/lib/pki_express/pades_visual_image.rb +52 -0
  31. data/lib/pki_express/pades_visual_manual_positioning.rb +17 -0
  32. data/lib/pki_express/pades_visual_positioning.rb +28 -0
  33. data/lib/pki_express/pades_visual_rectangle.rb +74 -0
  34. data/lib/pki_express/pades_visual_representation.rb +22 -0
  35. data/lib/pki_express/pades_visual_text.rb +35 -0
  36. data/lib/pki_express/pk_certificate.rb +62 -0
  37. data/lib/pki_express/pki_brazil_certificate_fields.rb +58 -0
  38. data/lib/pki_express/pki_brazil_certificate_types.rb +19 -0
  39. data/lib/pki_express/pki_express_config.rb +26 -0
  40. data/lib/pki_express/pki_express_operator.rb +216 -0
  41. data/lib/pki_express/pki_italy_certificate_fields.rb +16 -0
  42. data/lib/pki_express/pki_italy_certificate_types.rb +11 -0
  43. data/lib/pki_express/signature_finisher.rb +298 -0
  44. data/lib/pki_express/signature_start_result.rb +13 -0
  45. data/lib/pki_express/signature_starter.rb +115 -0
  46. data/lib/pki_express/signer.rb +106 -0
  47. data/lib/pki_express/standard_signature_policies.rb +36 -0
  48. data/lib/pki_express/timestamp_authority.rb +51 -0
  49. data/lib/pki_express/validation_error.rb +8 -0
  50. data/lib/pki_express/validation_item.rb +43 -0
  51. data/lib/pki_express/validation_results.rb +121 -0
  52. data/lib/pki_express/version.rb +3 -0
  53. data/lib/pki_express/version_manager.rb +21 -0
  54. data/pki_express.gemspec +27 -0
  55. metadata +129 -0
@@ -0,0 +1,16 @@
1
+ class PkiItalyCertificateFields
2
+
3
+ attr_accessor :certificate_type, :codice_fiscale, :id_carta
4
+
5
+ def initialize(model)
6
+ @certificate_type = nil
7
+ @codice_fiscale = nil
8
+ @id_carta = nil
9
+
10
+ unless model.nil?
11
+ @certificate_type = model.fetch(:certificateType)
12
+ @codice_fiscale = model.fetch(:codiceFiscale)
13
+ @id_carta = model.fetch(:idCarta)
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,11 @@
1
+ module PkiExpress
2
+ class PkiItalyCertificateTypes < Enum
3
+ UNDEFINED = 'Undefined'
4
+ CNS = 'Cns'
5
+ DIGITAL_SIGNATURE = 'DigitalSignature'
6
+
7
+ VALUES = [
8
+ UNDEFINED, CNS, DIGITAL_SIGNATURE
9
+ ]
10
+ end
11
+ end
@@ -0,0 +1,298 @@
1
+ module PkiExpress
2
+ class SignatureFinisher < PkiExpressOperator
3
+ attr_accessor :output_file_path, :transfer_file_id
4
+
5
+ def initialize(config=PkiExpressConfig.new)
6
+ super(config)
7
+ @file_to_sign_path = nil
8
+ @transfer_file_id = nil
9
+ @data_file_path = nil
10
+ @output_file_path = nil
11
+ @signature = nil
12
+ end
13
+
14
+ # region The "file_to_sign" accessors
15
+
16
+ def file_to_sign
17
+ _get_file_to_sign
18
+ end
19
+
20
+ def _get_file_to_sign
21
+ unless @file_to_sign_path
22
+ return nil
23
+ end
24
+
25
+ File.read(@file_to_sign_path)
26
+ end
27
+ private :_get_file_to_sign
28
+
29
+ def file_to_sign=(content_raw)
30
+ _set_file_to_sign(content_raw)
31
+ end
32
+
33
+ def _set_file_to_sign(content_raw)
34
+ unless content_raw
35
+ raise 'The provided "file_to_sign" is not valid'
36
+ end
37
+
38
+ temp_file_path = self.create_temp_file
39
+ File.open(temp_file_path, 'wb') do |f|
40
+ f.write(content_raw)
41
+ end
42
+ @file_to_sign_path = temp_file_path
43
+ end
44
+ private :_set_file_to_sign
45
+
46
+ def file_to_sign_base64
47
+ _get_file_to_sign_base64
48
+ end
49
+
50
+ def _get_file_to_sign_base64
51
+ unless @file_to_sign_path
52
+ return nil
53
+ end
54
+
55
+ content = File.read(@file_to_sign_path)
56
+ Base64.encode64(content)
57
+ end
58
+ private :_get_file_to_sign_base64
59
+
60
+ def file_to_sign_base64=(content_base64)
61
+ _set_file_to_sign_base64(content_base64)
62
+ end
63
+
64
+ def _set_file_to_sign_base64(content_base64)
65
+ unless content_base64
66
+ raise 'The provided "file_to_sign_base64" is not valid'
67
+ end
68
+
69
+ begin
70
+ content_raw = Base64.decode64(content_base64)
71
+ rescue Error
72
+ raise 'The provided "file_to_sign_base64" is not Base64-encoded'
73
+ end
74
+
75
+ _set_file_to_sign(content_raw)
76
+ end
77
+ private :_set_file_to_sign_base64
78
+
79
+ def file_to_sign_path
80
+ _get_file_to_sign_path
81
+ end
82
+
83
+ def _get_file_to_sign_path
84
+ @file_to_sign_path
85
+ end
86
+ private :_get_file_to_sign_path
87
+
88
+ def file_to_sign_path=(path)
89
+ _set_file_to_sign_path(path)
90
+ end
91
+
92
+ def _set_file_to_sign_path(path)
93
+ unless path
94
+ raise 'The provided "file_to_sign_path" is not valid'
95
+ end
96
+ unless File.exists?(path)
97
+ raise 'The provided "file_to_sign_path" does not exist'
98
+ end
99
+
100
+ @file_to_sign_path = path
101
+ end
102
+ private :_set_file_to_sign_path
103
+
104
+ # endregion
105
+
106
+ # region The "data_file" accessors
107
+
108
+ def data_file
109
+ _get_data_file
110
+ end
111
+
112
+ def _get_data_file
113
+ unless @data_file_path
114
+ return nil
115
+ end
116
+
117
+ File.read(@data_file_path)
118
+ end
119
+ private :_get_data_file
120
+
121
+ def data_file=(content_raw)
122
+ _set_data_file(content_raw)
123
+ end
124
+
125
+ def _set_data_file(content_raw)
126
+ unless content_raw
127
+ raise 'The provided "data_file" is not valid'
128
+ end
129
+
130
+ temp_file_path = self.create_temp_file
131
+ File.open(temp_file_path, 'wb') do |f|
132
+ f.write(content_raw)
133
+ end
134
+ @data_file_path = temp_file_path
135
+ end
136
+ private :_set_data_file
137
+
138
+ def data_file_base64
139
+ _get_data_file_base64
140
+ end
141
+
142
+ def _get_data_file_base64
143
+ unless @data_file_path
144
+ return nil
145
+ end
146
+
147
+ content = File.read(@data_file_path)
148
+ Base64.encode64(content)
149
+ end
150
+ private :_get_data_file_base64
151
+
152
+ def data_file_base64=(content_base64)
153
+ _set_data_file_base64(content_base64)
154
+ end
155
+
156
+ def _set_data_file_base64(content_base64)
157
+ unless content_base64
158
+ raise 'The provided "data_file_base64" is not valid'
159
+ end
160
+
161
+ begin
162
+ content_raw = Base64.decode64(content_base64)
163
+ rescue Error
164
+ raise 'The provided "data_file_base64" is not Base64-encoded'
165
+ end
166
+
167
+ _set_data_file(content_raw)
168
+ end
169
+ private :_set_data_file_base64
170
+
171
+ def data_file_path
172
+ _get_data_file_path
173
+ end
174
+
175
+ def _get_data_file_path
176
+ @data_file_path
177
+ end
178
+ private :_get_data_file_path
179
+
180
+ def data_file_path=(path)
181
+ _set_data_file_path(path)
182
+ end
183
+
184
+ def _set_data_file_path(path)
185
+ unless path
186
+ raise 'The provided "data_file_path" is not valid'
187
+ end
188
+ unless File.exists?(path)
189
+ raise 'The provided "data_file_path" does not exist'
190
+ end
191
+
192
+ @data_file_path = path
193
+ end
194
+ private :_set_data_file_path
195
+
196
+ # endregion
197
+
198
+ # region The "signature" accessors
199
+
200
+ def signature
201
+ _get_signature
202
+ end
203
+
204
+ def _get_signature
205
+ @signature
206
+ end
207
+ private :_get_signature
208
+
209
+ def signature=(content_raw)
210
+ _set_signature(content_raw)
211
+ end
212
+
213
+ def _set_signature(content_raw)
214
+ unless content_raw
215
+ raise 'The provided "signature" is not valid'
216
+ end
217
+ @signature = content_raw
218
+ end
219
+ private :_set_signature
220
+
221
+ def signature_base64
222
+ _get_signature_base64
223
+ end
224
+
225
+ def _get_signature_base64
226
+ unless @signature
227
+ return nil
228
+ end
229
+ Base64.encode64(@signature)
230
+ end
231
+ private :_get_signature_base64
232
+
233
+ def signature_base64=(content_base64)
234
+ _set_signature_base64(content_base64)
235
+ end
236
+
237
+ def _set_signature_base64(content_base64)
238
+ unless content_base64
239
+ raise 'The provided "signature_base64" is not valid'
240
+ end
241
+
242
+ begin
243
+ content_raw = Base64.decode64(content_base64)
244
+ rescue Error
245
+ raise 'The provided "signature_base64" is not Base64-encoded'
246
+ end
247
+
248
+ _set_signature(content_raw)
249
+ end
250
+ private :_set_signature_base64
251
+
252
+ # endregion
253
+
254
+ def complete(get_cert=true)
255
+ unless @file_to_sign_path
256
+ raise 'The file to be signed was not set'
257
+ end
258
+ unless @transfer_file_id
259
+ raise 'The transfer data file was not set'
260
+ end
261
+ unless @signature
262
+ raise 'The signature was not set'
263
+ end
264
+ unless @output_file_path
265
+ raise 'The output destination was not set'
266
+ end
267
+
268
+ args = [
269
+ @file_to_sign_path,
270
+ File.expand_path(@transfer_file_id, @config.transfer_data_folder),
271
+ @signature,
272
+ @output_file_path,
273
+ ]
274
+
275
+ if @data_file_path
276
+ args.append('--data-file')
277
+ args.append(@data_file_path)
278
+ end
279
+
280
+ if get_cert
281
+ # This operation can only be used on version greater than 1.8 of the
282
+ # PKI Express.
283
+ @version_manager.require_version('1.8')
284
+
285
+ # Invoke command.
286
+ result = invoke(Commands::COMPLETE_SIG, args)
287
+
288
+ # Parse output and return model.
289
+ model = parse_output(result)
290
+ return PKCertificate.new(model.fetch(:signer))
291
+ end
292
+
293
+ # Invoke command.
294
+ invoke(Commands::COMPLETE_SIG, args)
295
+ end
296
+
297
+ end
298
+ end
@@ -0,0 +1,13 @@
1
+ module PkiExpress
2
+ class SignatureStartResult
3
+ attr_accessor :to_sign_hash, :digest_algorithm_name, :digest_algorithm_oid,
4
+ :transfer_file_id
5
+
6
+ def initialize(model, transfer_file_id)
7
+ @to_sign_hash = model.fetch(:toSignHash)
8
+ @digest_algorithm_name = model.fetch(:digestAlgorithmName)
9
+ @digest_algorithm_oid = model.fetch(:digestAlgorithmOid)
10
+ @transfer_file_id = transfer_file_id
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,115 @@
1
+ module PkiExpress
2
+
3
+ class SignatureStarter < BaseSigner
4
+
5
+ def initialize(config=PkiExpressConfig.new)
6
+ super(config)
7
+ @certificate_path = nil
8
+ end
9
+
10
+ # region The "certificate" accessors
11
+
12
+ def certificate
13
+ _get_certificate
14
+ end
15
+
16
+ def _get_certificate
17
+ unless @certificate_path
18
+ return nil
19
+ end
20
+
21
+ File.read(@certificate_path)
22
+ end
23
+ private :_get_certificate
24
+
25
+ def certificate=(content_raw)
26
+ _set_certificate(content_raw)
27
+ end
28
+
29
+ def _set_certificate(content_raw)
30
+ unless content_raw
31
+ raise 'The provided "certificate" is not valid'
32
+ end
33
+
34
+ temp_file_path = self.create_temp_file
35
+ File.open(temp_file_path, 'wb') do |f|
36
+ f.write(content_raw)
37
+ end
38
+ @certificate_path = temp_file_path
39
+ end
40
+ private :_set_certificate
41
+
42
+ def certificate_base64
43
+ _get_certificate_base64
44
+ end
45
+
46
+ def _get_certificate_base64
47
+ unless @certificate_path
48
+ return nil
49
+ end
50
+
51
+ content = File.read(@certificate_path)
52
+ Base64.encode64(content)
53
+ end
54
+ private :_get_certificate_base64
55
+
56
+ def certificate_base64=(content_base64)
57
+ _set_certificate_base64(content_base64)
58
+ end
59
+
60
+ def _set_certificate_base64(content_base64)
61
+ unless content_base64
62
+ raise 'The provided "certificate_base64" is not valid'
63
+ end
64
+
65
+ begin
66
+ content_raw = Base64.decode64(content_base64)
67
+ rescue Error
68
+ raise 'The provided "certificate_base64" is not Base64-encoded'
69
+ end
70
+
71
+ _set_certificate(content_raw)
72
+ end
73
+
74
+ def certificate_path
75
+ _get_certificate_path
76
+ end
77
+
78
+ def _get_certificate_path
79
+ @certificate_path
80
+ end
81
+ private :_get_certificate_path
82
+
83
+ def certificate_path=(path)
84
+ _set_certificate_path(path)
85
+ end
86
+
87
+ def _set_certificate_path(path)
88
+ unless path
89
+ raise 'The provided "certificate_path" is not valid'
90
+ end
91
+ unless File.exists?(path)
92
+ raise 'The provided "certificate_path" does not exist'
93
+ end
94
+
95
+ @certificate_path = path
96
+ end
97
+ private :_set_certificate_path
98
+
99
+ # endregion
100
+
101
+ def self.get_result(response, transfer_file)
102
+ return {
103
+ toSignHash: response[0],
104
+ digestAlgorithmName: response[1],
105
+ digestAlgorithmOid: response[2],
106
+ transferFile: transfer_file
107
+ }
108
+ end
109
+
110
+ def start
111
+ raise NotImplementedError.new('This method is not implemented')
112
+ end
113
+ end
114
+
115
+ end