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,106 @@
1
+ module PkiExpress
2
+
3
+ class Signer < BaseSigner
4
+
5
+ attr_accessor :output_file_path, :cert_thumb, :cert_password
6
+
7
+ def initialize(config=PkiExpressConfig.new)
8
+ super(config)
9
+ @output_file_path = nil
10
+ @pkcs12_path = nil
11
+ @cert_thumb = nil
12
+ @cert_password = nil
13
+ end
14
+
15
+ # region The "pkcs12" accessors
16
+
17
+ def pkcs12
18
+ _get_pkcs12
19
+ end
20
+
21
+ def _get_pkcs12
22
+ unless @pkcs12_path
23
+ return nil
24
+ end
25
+
26
+ File.read(@pkcs12_path)
27
+ end
28
+ private :_get_pkcs12
29
+
30
+ def pkcs12=(content_raw)
31
+ _set_pkcs12(content_raw)
32
+ end
33
+
34
+ def _set_pkcs12(content_raw)
35
+ unless content_raw
36
+ raise 'The provided "pkcs12" is not valid'
37
+ end
38
+
39
+ temp_file_path = self.create_temp_file
40
+ File.open(temp_file_path, 'wb') do |f|
41
+ f.write(content_raw)
42
+ end
43
+ @pkcs12_path = temp_file_path
44
+ end
45
+ private :_set_pkcs12
46
+
47
+ def pkcs12_base64
48
+ _get_pkcs12_base64
49
+ end
50
+
51
+ def _get_pkcs12_base64
52
+ unless @pkcs12_path
53
+ return nil
54
+ end
55
+
56
+ content = File.read(@pkcs12_path)
57
+ Base64.encode64(content)
58
+ end
59
+ private :_get_pkcs12_base64
60
+
61
+ def pkcs12_base64=(content_base64)
62
+ _set_pkcs12_base64(content_base64)
63
+ end
64
+
65
+ def _set_pkcs12_base64(content_base64)
66
+ unless content_base64
67
+ raise 'The provided "content_base64" is not valid'
68
+ end
69
+
70
+ begin
71
+ content_raw = Base64.decode64(content_base64)
72
+ rescue Error
73
+ raise 'The provided "content_base64" is not Base64-encoded'
74
+ end
75
+
76
+ _set_pkcs12(content_raw)
77
+ end
78
+ private :_set_pkcs12_base64
79
+
80
+ def pkcs12_path
81
+ @pkcs12_path
82
+ end
83
+
84
+ def pkcs12_path=(path)
85
+ unless path
86
+ raise 'The provided "content_path" is not valid'
87
+ end
88
+ unless File.exists?(path)
89
+ raise 'The provided "content_path" does not exist'
90
+ end
91
+
92
+ @pkcs12_path = path
93
+ end
94
+
95
+ # endregion
96
+
97
+ protected
98
+ def verify_and_add_common_options(args)
99
+ # Verify and add common option between signers and signature starters.
100
+ super(args)
101
+
102
+
103
+ end
104
+ end
105
+
106
+ end
@@ -0,0 +1,36 @@
1
+ module PkiExpress
2
+
3
+ class StandardSignaturePolicies
4
+ PKI_BRAZIL_CADES_ADR_BASICA = 'adrb'
5
+ PKI_BRAZIL_CADES_ADR_BASICA_WITH_REVOCATION_VALUE = 'adrb-rv'
6
+ PKI_BRAZIL_CADES_ADR_TEMPO = 'adrt'
7
+ PKI_BRAZIL_CADES_ADR_COMPLETA = 'adrc'
8
+ CADES_BES = 'cades'
9
+ CADES_BES_WITH_REVOCATION_VALUES = 'cades-rv'
10
+ CADES_T = 'cades-t'
11
+
12
+ PADES_BASIC = 'pades'
13
+ PADES_BASIC_WITH_LTV = 'pades-ltv'
14
+ PADES_T = 'pades-t'
15
+ PKI_BRAZIL_PADES_ADR_BASICA = 'pades-ltv'
16
+ PKI_BRAZIL_PADES_ADR_BASICA_WITH_LTV = 'adrb-ltv'
17
+ PKI_BRAZIL_PADES_ADR_TEMPO = 'adrt'
18
+
19
+ NFE_PADRAO_NACIONAL = 'nfe'
20
+ XADES_BES = 'xades'
21
+ XML_DSIG_BASIC = 'basic'
22
+ PKI_BRAZIL_XML_ADR_BASIC = 'adrb'
23
+ PKI_BRAZIL_XML_ADR_TEMPO = 'adrt'
24
+ COD_WITH_SHA1 = 'cod-sha1'
25
+ COD_WITH_SHA256 = 'cod-sha256'
26
+
27
+ def self.require_timestamp(policy)
28
+ if policy.nil?
29
+ return false
30
+ end
31
+
32
+ return policy == PKI_BRAZIL_CADES_ADR_TEMPO || policy == PKI_BRAZIL_CADES_ADR_COMPLETA || policy == CADES_T || policy == PADES_T || policy == PKI_BRAZIL_PADES_ADR_TEMPO || policy == PKI_BRAZIL_XML_ADR_TEMPO
33
+ end
34
+ end
35
+
36
+ end
@@ -0,0 +1,51 @@
1
+ module PkiExpress
2
+ class TimestampAuthority
3
+ attr_reader :url, :token, :ssl_thumbprint, :basic_auth, :auth_type
4
+
5
+ def initialize(url)
6
+ @url = url
7
+ @auth_type = :none
8
+ @token = nil
9
+ @ssl_thumbprint = nil
10
+ @basic_auth = nil
11
+ end
12
+
13
+ def set_oauth_token_authentication(token)
14
+ @token = token
15
+ @auth_type = :oauth_token
16
+ end
17
+
18
+ def set_basic_authentication(username, password)
19
+ @basic_auth = "#{username}:#{password}"
20
+ @auth_type = :basic_auth
21
+ end
22
+
23
+ def set_ssl_thumbprint(ssl_thumbprint)
24
+ @ssl_thumbprint = ssl_thumbprint
25
+ @auth_type = :ssl
26
+ end
27
+
28
+ def get_cmd_arguments
29
+ args = []
30
+ args.append('--tsa-url')
31
+ args.append(url)
32
+
33
+ case auth_type
34
+ when :none
35
+ when :basic_auth
36
+ args.append('--tsa-basic-auth')
37
+ args.append(@basic_auth)
38
+ when :ssl
39
+ args.append('--tsa-ssl-thumbprint')
40
+ args.append(@ssl_thumbprint)
41
+ when :oauth_token
42
+ args.append('--tsa-token')
43
+ args.append(token)
44
+ else
45
+ raise 'Unknown authentication type of the timestamp authority'
46
+ end
47
+
48
+ args
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,8 @@
1
+ module PkiExpress
2
+ class ValidationError < CommandError
3
+
4
+ def initialize(validation_results, inner_error=nil)
5
+ super(ErrorCodes::VALIDATION_FAILED, validation_results, inner_error)
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,43 @@
1
+ module PkiExpress
2
+
3
+ class ValidationItem
4
+ attr_accessor :type, :message, :detail, :inner_validation_results
5
+
6
+ def initialize(model)
7
+ @type = nil
8
+ @message = nil
9
+ @detail = nil
10
+ @inner_validation_results = nil
11
+
12
+ unless model.nil?
13
+ @type = model.fetch(:type)
14
+ @message = model.fetch(:message)
15
+ @detail = model.fetch(:detail)
16
+
17
+ inner_validation_results = model.fetch(:innerValidationResults)
18
+ unless inner_validation_results.nil?
19
+ @inner_validation_results = ValidationResults.new(inner_validation_results)
20
+ end
21
+ end
22
+ end
23
+
24
+ def to_str(indentation_level=0)
25
+ to_s(indentation_level)
26
+ end
27
+
28
+ def to_s(indentation_level=0)
29
+ text = @message
30
+ unless @detail.nil?
31
+ text += " (#{@detail})"
32
+ end
33
+
34
+ unless @inner_validation_results.nil?
35
+ text += '\n'
36
+ text += @inner_validation_results.to_s(indentation_level + 1)
37
+ end
38
+
39
+ text
40
+ end
41
+ end
42
+
43
+ end
@@ -0,0 +1,121 @@
1
+ module PkiExpress
2
+
3
+ class ValidationResults
4
+ attr_accessor :errors, :warnings, :passed_checks
5
+
6
+ def initialize(model)
7
+ @errors = []
8
+ @warnings = []
9
+ @passed_checks = []
10
+
11
+ unless model.nil?
12
+ errors = model.fetch(:errors)
13
+ unless errors.nil?
14
+ @errors = convert_items(errors)
15
+ end
16
+
17
+ warnings = model.fetch(:warnings)
18
+ unless warnings.nil?
19
+ @warnings = convert_items(warnings)
20
+ end
21
+
22
+ passed_checks = model.fetch(:passedChecks)
23
+ unless passed_checks.nil?
24
+ @passed_checks = convert_items(passed_checks)
25
+ end
26
+ end
27
+ end
28
+
29
+ def to_str(indentation_level = 0)
30
+ to_s(indentation_level)
31
+ end
32
+
33
+ def to_s(indentation_level = 0)
34
+ item_indent = '\t' * indentation_level
35
+ text = ''
36
+
37
+ text += get_summary(indentation_level)
38
+ if has_errors
39
+ text += "\n#{item_indent}Errors:\n"
40
+ text += join_items(@errors, indentation_level)
41
+ end
42
+
43
+ if has_warnings
44
+ text += "\n#{item_indent}Warnings:\n"
45
+ text += join_items(@warnings, indentation_level)
46
+ end
47
+
48
+ if not @passed_checks.nil? and @passed_checks.length > 0
49
+ text += "\n#{item_indent}Passed Checks:\n"
50
+ text += join_items(@passed_checks, indentation_level)
51
+ end
52
+
53
+ text
54
+ end
55
+
56
+ def is_valid
57
+ not has_errors
58
+ end
59
+
60
+ def checks_performed
61
+ @errors.length + @warnings.length + @passed_checks.length
62
+ end
63
+
64
+ def has_errors
65
+ @errors.length > 0
66
+ end
67
+
68
+ def has_warnings
69
+ @errors.length > 0
70
+ end
71
+
72
+ def get_summary(indentation_level=0)
73
+ item_indent = '\t' * indentation_level
74
+ text = "#{item_indent}Validation Results: "
75
+
76
+ if checks_performed == 0
77
+ text += 'no checks performed'
78
+ else
79
+ text += "#{checks_performed} checks performed"
80
+ if has_errors
81
+ text += ", #{@errors.length} errors"
82
+ end
83
+ if has_warnings
84
+ text += ", #{@warnings.length} warnings"
85
+ end
86
+ if not @passed_checks.nil? and @passed_checks.length
87
+ if not has_errors and not has_warnings
88
+ text += ', all passed'
89
+ else
90
+ text += ", #{@passed_checks.length} passed"
91
+ end
92
+ end
93
+ end
94
+
95
+ text
96
+ end
97
+
98
+ def convert_items(items)
99
+ items.map { |i| ValidationItem.new(i) }
100
+ end
101
+
102
+ def join_items(items, indentation_level=0)
103
+ text = ''
104
+ is_first = true
105
+ item_indent = '\t' * indentation_level
106
+
107
+ items.each do |i|
108
+ if is_first
109
+ is_first = false
110
+ else
111
+ text += '\n'
112
+ end
113
+ text += item_indent + '- '
114
+ text += i.to_s(indentation_level)
115
+ end
116
+
117
+ text
118
+ end
119
+ end
120
+
121
+ end
@@ -0,0 +1,3 @@
1
+ module PkiExpress
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,21 @@
1
+ module PkiExpress
2
+
3
+ class VersionManager
4
+ attr_reader :min_version
5
+
6
+ def initialize
7
+ @min_version = '0.0.0'
8
+ end
9
+
10
+ def require_version(candidate)
11
+ if Gem::Version.new(candidate) > Gem::Version.new(@min_version)
12
+ @min_version = candidate
13
+ end
14
+ end
15
+
16
+ def require_min_version_flag?
17
+ Gem::Version.new(@min_version) > Gem::Version.new('1.3')
18
+ end
19
+ end
20
+
21
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ require_relative 'lib/pki_express/version'
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = 'pki_express'
6
+ spec.version = PkiExpress::VERSION
7
+ spec.license = 'MIT'
8
+ spec.authors = ['Ismael Medeiros']
9
+ spec.email = %w{ismaelm@lacunasoftware.com}
10
+ spec.homepage = 'http://docs.lacunasoftware.com/en-us/articles/pki-express'
11
+ spec.summary = 'Gem for using PKI Express on Ruby'
12
+ spec.description = 'Classes to use Lacuna Software\'s PKI Express'
13
+ spec.files = `git ls-files -z`.split("\x0")
14
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
15
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
16
+ spec.require_paths = ['lib']
17
+
18
+ spec.metadata = {
19
+ 'bug_tracker_uri' => 'https://github.com/LacunaSoftware/PkiExpressRuby/issues',
20
+ 'changelog_uri' => 'https://github.com/LacunaSoftware/PkiExpressRuby/blob/develop/CHANGELOG.md',
21
+ 'documentation_uri' => 'http://docs.lacunasoftware.com/en-us/articles/pki-express',
22
+ 'source_code_uri' => 'https://github.com/LacunaSoftware/PkiExpressRuby'
23
+ }
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.7'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pki_express
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Ismael Medeiros
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-12-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: Classes to use Lacuna Software's PKI Express
42
+ email:
43
+ - ismaelm@lacunasoftware.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitattributes"
49
+ - ".gitignore"
50
+ - CHANGELOG.md
51
+ - Gemfile
52
+ - README.md
53
+ - Rakefile
54
+ - lib/pki_express.rb
55
+ - lib/pki_express/auth_complete_result.rb
56
+ - lib/pki_express/auth_start_result.rb
57
+ - lib/pki_express/authentication.rb
58
+ - lib/pki_express/base_signer.rb
59
+ - lib/pki_express/cades_signature_starter.rb
60
+ - lib/pki_express/command_error.rb
61
+ - lib/pki_express/commands.rb
62
+ - lib/pki_express/enum.rb
63
+ - lib/pki_express/error_codes.rb
64
+ - lib/pki_express/installation_not_found_error.rb
65
+ - lib/pki_express/name.rb
66
+ - lib/pki_express/pades_horizontal_align.rb
67
+ - lib/pki_express/pades_measurement_units.rb
68
+ - lib/pki_express/pades_page_optimization.rb
69
+ - lib/pki_express/pades_page_orientation.rb
70
+ - lib/pki_express/pades_paper_size.rb
71
+ - lib/pki_express/pades_signature_starter.rb
72
+ - lib/pki_express/pades_size.rb
73
+ - lib/pki_express/pades_text_horizontal_align.rb
74
+ - lib/pki_express/pades_vertical_align.rb
75
+ - lib/pki_express/pades_visual_auto_positioning.rb
76
+ - lib/pki_express/pades_visual_image.rb
77
+ - lib/pki_express/pades_visual_manual_positioning.rb
78
+ - lib/pki_express/pades_visual_positioning.rb
79
+ - lib/pki_express/pades_visual_rectangle.rb
80
+ - lib/pki_express/pades_visual_representation.rb
81
+ - lib/pki_express/pades_visual_text.rb
82
+ - lib/pki_express/pk_certificate.rb
83
+ - lib/pki_express/pki_brazil_certificate_fields.rb
84
+ - lib/pki_express/pki_brazil_certificate_types.rb
85
+ - lib/pki_express/pki_express_config.rb
86
+ - lib/pki_express/pki_express_operator.rb
87
+ - lib/pki_express/pki_italy_certificate_fields.rb
88
+ - lib/pki_express/pki_italy_certificate_types.rb
89
+ - lib/pki_express/signature_finisher.rb
90
+ - lib/pki_express/signature_start_result.rb
91
+ - lib/pki_express/signature_starter.rb
92
+ - lib/pki_express/signer.rb
93
+ - lib/pki_express/standard_signature_policies.rb
94
+ - lib/pki_express/timestamp_authority.rb
95
+ - lib/pki_express/validation_error.rb
96
+ - lib/pki_express/validation_item.rb
97
+ - lib/pki_express/validation_results.rb
98
+ - lib/pki_express/version.rb
99
+ - lib/pki_express/version_manager.rb
100
+ - pki_express.gemspec
101
+ homepage: http://docs.lacunasoftware.com/en-us/articles/pki-express
102
+ licenses:
103
+ - MIT
104
+ metadata:
105
+ bug_tracker_uri: https://github.com/LacunaSoftware/PkiExpressRuby/issues
106
+ changelog_uri: https://github.com/LacunaSoftware/PkiExpressRuby/blob/develop/CHANGELOG.md
107
+ documentation_uri: http://docs.lacunasoftware.com/en-us/articles/pki-express
108
+ source_code_uri: https://github.com/LacunaSoftware/PkiExpressRuby
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 2.7.6
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: Gem for using PKI Express on Ruby
129
+ test_files: []