signature_dfe 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +6 -23
- data/CHANGELOG +0 -2
- data/README.md +42 -42
- data/lib/signature_dfe.rb +1 -0
- data/lib/signature_dfe/config.rb +32 -0
- data/lib/signature_dfe/ssl.rb +12 -41
- data/lib/signature_dfe/version.rb +1 -1
- data/lib/signature_dfe_check.rb +2 -4
- data/lib/signature_dfe_xml.rb +11 -13
- data/signature_dfe.gemspec +3 -2
- metadata +11 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 92c8fd17fe3a88f60eee23a9e825452bde448b4e5645596441046a38a9651d8e
|
4
|
+
data.tar.gz: fa6c437efbc6796d427135a9fb605d55091ea4b1499399a6f487c32a1fc31a8f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b2174847030246e40692a53ddf57e0574f98d3e2e869d09c150afec33f778f7e15ece32fe258e880bdb985f1968b7c04b41d70a043501ccfcc8bd22cc60d0084
|
7
|
+
data.tar.gz: c6dec2aac9da0de279ce8febef467aa151ce0cbcffb1424182ed02ac4dc41ce61c17d4cb5e72ebbaf8b3bc717362aebfb75808ff457a8f0d513e9ff1b4f1af67
|
data/.rubocop.yml
CHANGED
@@ -1,27 +1,10 @@
|
|
1
1
|
AllCops:
|
2
|
-
|
3
|
-
|
4
|
-
Layout/LineLength:
|
5
|
-
Exclude:
|
6
|
-
|
7
|
-
Metrics/BlockLength:
|
8
|
-
Exclude:
|
9
|
-
- signature_dfe.gemspec
|
10
|
-
- spec/xml/signature_dfe_xml_spec.rb
|
11
|
-
- spec/signature_dfe_spec.rb
|
12
|
-
- spec/event/signature_dfe_event_pem_spec.rb
|
13
|
-
- spec/event/signature_dfe_event_pkcs_spec.rb
|
14
|
-
- spec/nfe/signature_dfe_nfe_pem_spec.rb
|
15
|
-
- spec/nfe/signature_dfe_nfe_pkcs_spec.rb
|
16
|
-
- spec/xml/event/signatire_dfe_event_spec.rb
|
17
|
-
|
18
|
-
Style/Documentation:
|
19
|
-
Enabled: false
|
20
|
-
|
2
|
+
NewCops: enable
|
21
3
|
Style/FrozenStringLiteralComment:
|
22
4
|
Enabled: false
|
23
|
-
|
24
|
-
|
25
|
-
|
5
|
+
Style/Documentation:
|
6
|
+
Enabled: false
|
7
|
+
Metrics/BlockLength:
|
26
8
|
Exclude:
|
27
|
-
|
9
|
+
- spec/**/*.rb
|
10
|
+
- signature_dfe.gemspec
|
data/CHANGELOG
CHANGED
data/README.md
CHANGED
@@ -68,7 +68,7 @@ A forma qual vc tem a xml da assinautra completo onde vc passa o seu xml contend
|
|
68
68
|
```ruby
|
69
69
|
inf_nfe = %{
|
70
70
|
<infNFe Id="NFe00000000000000000000000000000000000000000000" versao="3.10">
|
71
|
-
|
71
|
+
...
|
72
72
|
</infNFe>}
|
73
73
|
SignatureDfe::NFe.sign inf_nfe
|
74
74
|
```
|
@@ -77,24 +77,24 @@ Onde a resposta será
|
|
77
77
|
|
78
78
|
```xml
|
79
79
|
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
80
|
+
<SignedInfo>
|
81
|
+
<CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
|
82
|
+
<SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
|
83
|
+
<Reference URI="#NFe...">
|
84
|
+
<Transforms>
|
85
|
+
<Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
|
86
|
+
<Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
|
87
|
+
</Transforms>
|
88
|
+
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
|
89
|
+
<DigestValue>...</DigestValue>
|
90
|
+
</Reference>
|
91
|
+
</SignedInfo>
|
92
|
+
<SignatureValue>...</SignatureValue>
|
93
|
+
<KeyInfo>
|
94
|
+
<X509Data>
|
95
|
+
<X509Certificate>...</X509Certificate>
|
96
|
+
</X509Data>
|
97
|
+
</KeyInfo>
|
98
98
|
</Signature>
|
99
99
|
```
|
100
100
|
|
@@ -103,7 +103,7 @@ E a forma qual onde você pode obter os valores do <b>DigestValue</b>, <b>Signat
|
|
103
103
|
```ruby
|
104
104
|
inf_nfe = %{
|
105
105
|
<infNFe Id="NFe00000000000000000000000000000000000000000000" versao="3.10">
|
106
|
-
|
106
|
+
...
|
107
107
|
</infNFe>}
|
108
108
|
ch_nfe = "0000000000000000000000000000000000000000000"
|
109
109
|
digest_value = SignatureDfe::NFe.digest_value inf_nfe
|
@@ -122,7 +122,7 @@ A forma qual vc tem a xml da assinautra completo onde vc passa o seu xml contend
|
|
122
122
|
```ruby
|
123
123
|
inf_evento = %{
|
124
124
|
<infEvento Id="ID1101115515151515151515151515156546546546545646544701">
|
125
|
-
|
125
|
+
...
|
126
126
|
</infEvento>
|
127
127
|
}
|
128
128
|
SignatureDfe::NFe::Event.sign inf_evento
|
@@ -132,24 +132,24 @@ Onde a resposta será
|
|
132
132
|
|
133
133
|
```xml
|
134
134
|
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
135
|
+
<SignedInfo>
|
136
|
+
<CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
|
137
|
+
<SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
|
138
|
+
<Reference URI="#ID1...">
|
139
|
+
<Transforms>
|
140
|
+
<Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
|
141
|
+
<Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
|
142
|
+
</Transforms>
|
143
|
+
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
|
144
|
+
<DigestValue>...</DigestValue>
|
145
|
+
</Reference>
|
146
|
+
</SignedInfo>
|
147
|
+
<SignatureValue>...</SignatureValue>
|
148
|
+
<KeyInfo>
|
149
|
+
<X509Data>
|
150
|
+
<X509Certificate>...</X509Certificate>
|
151
|
+
</X509Data>
|
152
|
+
</KeyInfo>
|
153
153
|
</Signature>
|
154
154
|
```
|
155
155
|
|
@@ -158,7 +158,7 @@ E a forma qual onde você pode obter os valores do <b>DigestValue</b>, <b>Signat
|
|
158
158
|
```ruby
|
159
159
|
inf_evento = %{
|
160
160
|
<infEvento Id="ID1101115515151515151515151515156546546546545646544701">
|
161
|
-
|
161
|
+
...
|
162
162
|
</infEvento>}
|
163
163
|
event_id = "ID1101115515151515151515151515156546546546545646544701"
|
164
164
|
digest_value = SignatureDfe::NFe::Event.digest_value inf_evento
|
@@ -172,7 +172,7 @@ Segue-se exatamente como os documentos anteriores
|
|
172
172
|
|
173
173
|
Para assinar passo a passo
|
174
174
|
|
175
|
-
```
|
175
|
+
```ruby
|
176
176
|
inf_inut = %{
|
177
177
|
<infInut Id="ID06546541654654654654654654654654654654879">
|
178
178
|
...
|
@@ -186,7 +186,7 @@ x509certificate = SignatureDfe::SSL.cert
|
|
186
186
|
|
187
187
|
Ou, para gerar toda a assinatura em único passo
|
188
188
|
|
189
|
-
```
|
189
|
+
```ruby
|
190
190
|
inf_inut = %{
|
191
191
|
<infInut Id="ID06546541654654654654654654654654654654879">
|
192
192
|
...
|
data/lib/signature_dfe.rb
CHANGED
@@ -0,0 +1,32 @@
|
|
1
|
+
module SignatureDfe
|
2
|
+
class Config
|
3
|
+
include AbstractClass
|
4
|
+
|
5
|
+
attr_accessor :pkcs12, :pkey, :cert
|
6
|
+
|
7
|
+
attr_writer :password
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
clear
|
11
|
+
end
|
12
|
+
|
13
|
+
def clear
|
14
|
+
@pkcs12 = nil
|
15
|
+
@pkey = nil
|
16
|
+
@cert = nil
|
17
|
+
@password = nil
|
18
|
+
end
|
19
|
+
|
20
|
+
def inspect
|
21
|
+
super.gsub(/, @pass[\s\S]*?>/, '>')
|
22
|
+
end
|
23
|
+
|
24
|
+
def cert?
|
25
|
+
!(cert.nil? || cert.empty?)
|
26
|
+
end
|
27
|
+
|
28
|
+
def instance_variables
|
29
|
+
super - [:@password]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/lib/signature_dfe/ssl.rb
CHANGED
@@ -1,31 +1,4 @@
|
|
1
1
|
module SignatureDfe
|
2
|
-
class Config
|
3
|
-
include AbstractClass
|
4
|
-
|
5
|
-
attr_accessor :pkcs12, :pkey, :cert
|
6
|
-
|
7
|
-
attr_writer :password
|
8
|
-
|
9
|
-
def initialize
|
10
|
-
clear
|
11
|
-
end
|
12
|
-
|
13
|
-
def clear
|
14
|
-
@pkcs12 = nil
|
15
|
-
@pkey = nil
|
16
|
-
@cert = nil
|
17
|
-
@password = nil
|
18
|
-
end
|
19
|
-
|
20
|
-
def inspect
|
21
|
-
super.gsub(/\, \@pass[\s\S]*?\>/, '>')
|
22
|
-
end
|
23
|
-
|
24
|
-
def instance_variables
|
25
|
-
super - [:@password]
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
2
|
class SSL
|
30
3
|
include AbstractClass
|
31
4
|
|
@@ -35,17 +8,26 @@ module SignatureDfe
|
|
35
8
|
attr_reader :config
|
36
9
|
end
|
37
10
|
|
38
|
-
def self.sign(content, sign_method = OpenSSL::Digest
|
11
|
+
def self.sign(content, sign_method = OpenSSL::Digest.new('SHA1'))
|
39
12
|
self.test unless defined?(@pk)
|
40
13
|
@pk.sign sign_method, content
|
41
14
|
end
|
42
15
|
|
43
16
|
def self.cert
|
44
17
|
self.test unless defined?(@pk)
|
45
|
-
@cert.to_s.gsub(
|
18
|
+
@cert.to_s.gsub(/-----[A-Z]+ CERTIFICATE-----/, '').strip
|
46
19
|
end
|
47
20
|
|
48
21
|
class << self
|
22
|
+
def test
|
23
|
+
set_up
|
24
|
+
test_pkc12 if config.pkcs12
|
25
|
+
test_pem if config.pkey && !config.pkcs12
|
26
|
+
true
|
27
|
+
rescue OpenSSL::PKey::RSAError
|
28
|
+
error "Wrong password for '#{config.pkey}'"
|
29
|
+
end
|
30
|
+
|
49
31
|
private
|
50
32
|
|
51
33
|
def error(msg)
|
@@ -82,9 +64,7 @@ module SignatureDfe
|
|
82
64
|
end
|
83
65
|
|
84
66
|
def check_cert
|
85
|
-
|
86
|
-
error 'You must be set up the cert if you chose use pkey'
|
87
|
-
end
|
67
|
+
error 'You must be set up the cert if you chose use pkey' unless config.cert?
|
88
68
|
if File.exist? config.cert
|
89
69
|
@cert = OpenSSL::X509::Certificate.new(File.read(config.cert))
|
90
70
|
else
|
@@ -104,14 +84,5 @@ module SignatureDfe
|
|
104
84
|
error "Your cert '#{config.cert}' is not a valid file"
|
105
85
|
end
|
106
86
|
end
|
107
|
-
|
108
|
-
def self.test
|
109
|
-
set_up
|
110
|
-
test_pkc12 if config.pkcs12
|
111
|
-
test_pem if config.pkey && !config.pkcs12
|
112
|
-
true
|
113
|
-
rescue OpenSSL::PKey::RSAError
|
114
|
-
error "Wrong password for '#{config.pkey}'"
|
115
|
-
end
|
116
87
|
end
|
117
88
|
end
|
data/lib/signature_dfe_check.rb
CHANGED
@@ -20,11 +20,9 @@ module SignatureDfe
|
|
20
20
|
uri = Xml.namespace_value('URI', Xml.tag('Reference', xml)).gsub('#', '')
|
21
21
|
xmlns = Xml.namespace_value('xmlns', xml)
|
22
22
|
node_assigned = Xml.get_node_by_namespace_value(uri, xml)
|
23
|
-
node_assigned.gsub!(/>\s
|
23
|
+
node_assigned.gsub!(/>\s+</, '><')
|
24
24
|
node_name = Xml.node_name(node_assigned)
|
25
|
-
unless Xml.tag(node_name, xml).include?(xmlns)
|
26
|
-
node_assigned.gsub!(node_name, %(#{node_name} xmlns="#{xmlns}"))
|
27
|
-
end
|
25
|
+
node_assigned.gsub!(node_name, %(#{node_name} xmlns="#{xmlns}")) unless Xml.tag(node_name, xml).include?(xmlns)
|
28
26
|
dv = OpenSSL::Digest::SHA1.digest(Xml.canonize(node_assigned))
|
29
27
|
Base64.encode64(dv).strip == Xml.node_content('DigestValue', xml)
|
30
28
|
end
|
data/lib/signature_dfe_xml.rb
CHANGED
@@ -7,19 +7,17 @@ module SignatureDfe
|
|
7
7
|
end
|
8
8
|
|
9
9
|
def self.build_signature(options = {})
|
10
|
-
|
11
|
-
xml = File.read path
|
10
|
+
xml = File.read path('lib/signature_dfe/templates/signature.xml')
|
12
11
|
xml.gsub!(':id', options[:id])
|
13
12
|
xml.gsub!(':digest_value', options[:digest_value])
|
14
13
|
xml.gsub!(':signature_value', options[:signature_value])
|
15
14
|
cert = SignatureDfe::SSL.cert.to_s
|
16
|
-
cert.to_s.gsub(
|
17
|
-
xml.gsub!(':x509_certificate', cert.strip).gsub(
|
15
|
+
cert.to_s.gsub(/-----[A-Z]+ CERTIFICATE-----/, '').strip!
|
16
|
+
xml.gsub!(':x509_certificate', cert.strip).gsub(/>\s+</, '><')
|
18
17
|
end
|
19
18
|
|
20
19
|
def self.build_signed_info(id, digest_value_)
|
21
|
-
|
22
|
-
signed_info = File.read path
|
20
|
+
signed_info = File.read path('lib/signature_dfe/templates/signed_info.xml')
|
23
21
|
signed_info.gsub!(':id', id)
|
24
22
|
signed_info.gsub!(':digest_value', digest_value_)
|
25
23
|
signed_info_canonized = SignatureDfe::Xml.canonize signed_info
|
@@ -27,38 +25,38 @@ module SignatureDfe
|
|
27
25
|
end
|
28
26
|
|
29
27
|
def self.digest_method_algorithm(xml)
|
30
|
-
xml.scan(/(
|
28
|
+
xml.scan(/(<DigestMethod[\s\S]*?\#)([\s\S]*?)("|')/)[0][1]
|
31
29
|
end
|
32
30
|
|
33
31
|
def self.node(name, xml)
|
34
|
-
r = %r{
|
32
|
+
r = %r{<#{Regexp.escape(name)}[\s\S]*((/>)|(#{Regexp.escape(name)}>))}
|
35
33
|
xml.match(r)[0].gsub(/>\s+</, '><')
|
36
34
|
end
|
37
35
|
|
38
36
|
def self.node_content(name, xml)
|
39
|
-
full_node = xml.scan(%r{
|
37
|
+
full_node = xml.scan(%r{<#{name}.*?>([\s\S]*?)</#{name}>})
|
40
38
|
return nil unless full_node[0]
|
41
39
|
|
42
40
|
full_node[0][0]
|
43
41
|
end
|
44
42
|
|
45
43
|
def self.node_name(xml)
|
46
|
-
xml.scan(%r{
|
44
|
+
xml.scan(%r{<[^/\s>]*})[0].gsub('<', '')
|
47
45
|
end
|
48
46
|
|
49
47
|
def self.tag(name, xml)
|
50
|
-
xml.scan(%r{
|
48
|
+
xml.scan(%r{<#{name}[\S\s]*?[/>|>]})[0]
|
51
49
|
end
|
52
50
|
|
53
51
|
def self.namespace_value(namespace, xml)
|
54
|
-
matches = xml.match(/#{Regexp.escape(namespace)}
|
52
|
+
matches = xml.match(/#{Regexp.escape(namespace)}="([^"]*)/)
|
55
53
|
return nil unless matches
|
56
54
|
|
57
55
|
matches[1]
|
58
56
|
end
|
59
57
|
|
60
58
|
def self.get_node_by_namespace_value(value, xml)
|
61
|
-
a = xml.match(%r{
|
59
|
+
a = xml.match(%r{<[^<]*#{Regexp.escape(value)}[^>]*(>|/>)})[0]
|
62
60
|
node(node_name(a), xml)
|
63
61
|
end
|
64
62
|
|
data/signature_dfe.gemspec
CHANGED
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
20
20
|
spec.metadata['homepage_uri'] = spec.homepage
|
21
21
|
spec.metadata['source_code_uri'] = repo
|
22
|
-
spec.metadata['changelog_uri'] = repo
|
22
|
+
spec.metadata['changelog_uri'] = "#{repo}/blob/master/CHANGELOG"
|
23
23
|
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
24
24
|
rx = %r{^(test|spec|features)/}
|
25
25
|
`git ls-files -z`.split("\x0").reject { |f| f.match(rx) }
|
@@ -30,6 +30,7 @@ Gem::Specification.new do |spec|
|
|
30
30
|
spec.add_development_dependency 'bundler', '~> 2.1.2'
|
31
31
|
spec.add_development_dependency 'nokogiri', '~> 1.10.7'
|
32
32
|
spec.add_development_dependency 'openssl', '~> 2.1.2'
|
33
|
-
spec.add_development_dependency 'rake', '
|
33
|
+
spec.add_development_dependency 'rake', '>= 12.3.3'
|
34
34
|
spec.add_development_dependency 'rspec', '~> 3.0'
|
35
|
+
spec.required_ruby_version = '>= 2.4'
|
35
36
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: signature_dfe
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Thiago Feitosa
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-09-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -56,16 +56,16 @@ dependencies:
|
|
56
56
|
name: rake
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- - "
|
59
|
+
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
61
|
+
version: 12.3.3
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- - "
|
66
|
+
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version:
|
68
|
+
version: 12.3.3
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rspec
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -106,6 +106,7 @@ files:
|
|
106
106
|
- certs/gen.sh
|
107
107
|
- certs/key.pem
|
108
108
|
- lib/signature_dfe.rb
|
109
|
+
- lib/signature_dfe/config.rb
|
109
110
|
- lib/signature_dfe/disabling.rb
|
110
111
|
- lib/signature_dfe/evento_nfe.rb
|
111
112
|
- lib/signature_dfe/nfe.rb
|
@@ -124,7 +125,7 @@ metadata:
|
|
124
125
|
homepage_uri: https://github.com/thiaguerd/signature_dfe
|
125
126
|
source_code_uri: https://github.com/thiaguerd/signature_dfe
|
126
127
|
changelog_uri: https://github.com/thiaguerd/signature_dfe/blob/master/CHANGELOG
|
127
|
-
post_install_message:
|
128
|
+
post_install_message:
|
128
129
|
rdoc_options: []
|
129
130
|
require_paths:
|
130
131
|
- lib
|
@@ -132,7 +133,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
132
133
|
requirements:
|
133
134
|
- - ">="
|
134
135
|
- !ruby/object:Gem::Version
|
135
|
-
version: '
|
136
|
+
version: '2.4'
|
136
137
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
137
138
|
requirements:
|
138
139
|
- - ">="
|
@@ -140,7 +141,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
140
141
|
version: '0'
|
141
142
|
requirements: []
|
142
143
|
rubygems_version: 3.1.2
|
143
|
-
signing_key:
|
144
|
+
signing_key:
|
144
145
|
specification_version: 4
|
145
146
|
summary: Assinatura digital de documentos fiscais eletrônicos
|
146
147
|
test_files: []
|