xades 0.1.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.
- checksums.yaml +7 -0
- data/CHANGELOG.md +5 -0
- data/LICENSE.txt +21 -0
- data/NOTICE +12 -0
- data/README.md +114 -0
- data/Rakefile +12 -0
- data/lib/xades/algorithms.rb +17 -0
- data/lib/xades/bes.rb +23 -0
- data/lib/xades/builder/key_info.rb +14 -0
- data/lib/xades/builder/qualifying_properties.rb +50 -0
- data/lib/xades/builder/signed_info.rb +16 -0
- data/lib/xades/c14n.rb +13 -0
- data/lib/xades/certificate.rb +76 -0
- data/lib/xades/digest.rb +10 -0
- data/lib/xades/ecdsa_signature.rb +39 -0
- data/lib/xades/errors.rb +11 -0
- data/lib/xades/signer.rb +117 -0
- data/lib/xades/util.rb +17 -0
- data/lib/xades/verifier.rb +159 -0
- data/lib/xades/version.rb +5 -0
- data/lib/xades.rb +21 -0
- data/sig/xades.rbs +4 -0
- metadata +97 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 73fcbd3237d890cc28a31a94cff79c09f8fe7eadb7a0f453f8a45c78791a8d52
|
|
4
|
+
data.tar.gz: dae7a067ede1c4d6567efab5654a40bbf9d7675a4c8174cfc23f6ddfaf16231b
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 4fbf97d128b02319f277d72a99e7a7891443f2f371cb62ab63d5d5f424657b72bc91c55125a936edd8686d636395fcd676a4b573a608da94079b897c23cfcade
|
|
7
|
+
data.tar.gz: 63ccb7b27ea142234fb3e8fc889ff5194d7a2f9504921c817f998c2dd26f1222218c59bc6d882286f676ab3f384e5ee9ed2577e874211c53ba4276a02ae16298
|
data/CHANGELOG.md
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Chris Hasinski
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/NOTICE
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
This gem's own source code is MIT-licensed (see LICENSE.txt).
|
|
2
|
+
|
|
3
|
+
The test fixtures under spec/fixtures/interop/ are vendored, unmodified, from:
|
|
4
|
+
|
|
5
|
+
signxml (https://github.com/XML-Security/signxml)
|
|
6
|
+
Copyright signxml contributors
|
|
7
|
+
Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
|
|
8
|
+
|
|
9
|
+
They are test data only (sample signed XML documents used to exercise this gem's verifier) and are
|
|
10
|
+
not compiled or linked into the gem's runtime code. See spec/fixtures/interop/SOURCES.md for the
|
|
11
|
+
provenance and purpose of each individual file, including further upstream lineage to the esig/dss
|
|
12
|
+
project (LGPL-2.1) and the ETSI XAdES plugtests corpus.
|
data/README.md
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# Xades
|
|
2
|
+
|
|
3
|
+
[](https://github.com/khasinski/xades/actions/workflows/main.yml)
|
|
4
|
+
|
|
5
|
+
XAdES-BES / BASELINE-B (ETSI EN 319 132) XML signing and verification for pure Ruby (MRI). No
|
|
6
|
+
`xmlsec1` binary, no JVM -- just [Nokogiri](https://nokogiri.org/) (XML + Exclusive C14N) and
|
|
7
|
+
Ruby's built-in `openssl` (RSA / ECDSA).
|
|
8
|
+
|
|
9
|
+
This exists because Ruby has never had a maintained, general-purpose XAdES gem, while several
|
|
10
|
+
eIDAS-adjacent flows require one -- most concretely, **Poland's KSeF 2.0** e-invoicing system,
|
|
11
|
+
where authenticating with a certificate means signing an `AuthTokenRequest` document with
|
|
12
|
+
XAdES-BES (see [CIRFMF/ksef-docs](https://github.com/CIRFMF/ksef-docs)).
|
|
13
|
+
|
|
14
|
+
## What this is (and isn't)
|
|
15
|
+
|
|
16
|
+
Implements **XAdES-BES / BASELINE-B** only:
|
|
17
|
+
|
|
18
|
+
- Enveloped signatures (the signature is inserted as the last child of the signed document's root)
|
|
19
|
+
- Exclusive C14N (`http://www.w3.org/2001/10/xml-exc-c14n#`)
|
|
20
|
+
- RSA-SHA256 and ECDSA-SHA256 (P-256 / P-384 / P-521), with ECDSA `SignatureValue` encoded as raw
|
|
21
|
+
`R || S` per RFC 4050/6931 -- not the DER form OpenSSL produces internally
|
|
22
|
+
- `SigningCertificate` (V1) or `SigningCertificateV2` (default; RFC 5035 `IssuerSerialV2`)
|
|
23
|
+
- PEM and PKCS#12 key/certificate loading
|
|
24
|
+
|
|
25
|
+
**Not implemented** (out of scope for this MVP): XAdES-T/-LT/-LTA (timestamps, revocation data,
|
|
26
|
+
archival), CAdES/PAdES, enveloping or detached signature formats, HSM integration, and certificate
|
|
27
|
+
trust/chain/revocation validation. `Xades::Bes.verify` checks that the signature is *structurally
|
|
28
|
+
and cryptographically correct against the certificate embedded in the document* -- it does not
|
|
29
|
+
tell you whether that certificate should be trusted.
|
|
30
|
+
|
|
31
|
+
**Status**: pre-1.0, early. The test suite includes real-world interop fixtures from other
|
|
32
|
+
projects (see `spec/fixtures/interop/SOURCES.md`) and a golden fixture matching KSeF's published
|
|
33
|
+
`AuthTokenRequest` schema, but this gem has not yet been independently cross-checked against a
|
|
34
|
+
third-party validator (`xmlsec1`, esig/dss) or a live KSeF test environment. Please verify against
|
|
35
|
+
your target system before relying on it for anything regulated, and open an issue if you do.
|
|
36
|
+
|
|
37
|
+
## Installation
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
bundle add xades
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Usage
|
|
44
|
+
|
|
45
|
+
```ruby
|
|
46
|
+
require "xades"
|
|
47
|
+
|
|
48
|
+
certificate = Xades::Certificate.from_pem(
|
|
49
|
+
cert: File.read("cert.pem"),
|
|
50
|
+
key: File.read("key.pem")
|
|
51
|
+
)
|
|
52
|
+
# or: Xades::Certificate.from_pkcs12(File.read("cert.p12"), "password")
|
|
53
|
+
|
|
54
|
+
signed_xml = Xades::Bes.sign(xml, certificate: certificate)
|
|
55
|
+
|
|
56
|
+
result = Xades::Bes.verify(signed_xml)
|
|
57
|
+
result.valid? # => true
|
|
58
|
+
result.errors # => []
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
`Xades::Bes.sign` accepts:
|
|
62
|
+
|
|
63
|
+
| option | default | |
|
|
64
|
+
|---|---|---|
|
|
65
|
+
| `certificate:` | *(required)* | an `Xades::Certificate` |
|
|
66
|
+
| `signing_time:` | `Time.now.utc` | embedded as `xades:SigningTime` |
|
|
67
|
+
| `signing_certificate_version:` | `:v2` | `:v2` (`SigningCertificateV2`/`IssuerSerialV2`) or `:v1` (`SigningCertificate`/`IssuerSerial`) |
|
|
68
|
+
|
|
69
|
+
The certificate's key type (RSA or EC) determines the signature algorithm automatically.
|
|
70
|
+
|
|
71
|
+
### KSeF example
|
|
72
|
+
|
|
73
|
+
```ruby
|
|
74
|
+
auth_token_request = <<~XML
|
|
75
|
+
<AuthTokenRequest xmlns="http://ksef.mf.gov.pl/auth/token/2.1">
|
|
76
|
+
<Challenge>#{challenge}</Challenge>
|
|
77
|
+
<ContextIdentifier><Nip>#{nip}</Nip></ContextIdentifier>
|
|
78
|
+
<SubjectIdentifierType>certificateSubject</SubjectIdentifierType>
|
|
79
|
+
</AuthTokenRequest>
|
|
80
|
+
XML
|
|
81
|
+
|
|
82
|
+
signed = Xades::Bes.sign(auth_token_request, certificate: certificate)
|
|
83
|
+
# POST `signed` to /api/v2/auth/xades-signature
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Development
|
|
87
|
+
|
|
88
|
+
```bash
|
|
89
|
+
bin/setup
|
|
90
|
+
bundle exec rspec
|
|
91
|
+
bundle exec rubocop
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
The test suite (`spec/`) combines three kinds of coverage, modeled on how other languages'
|
|
95
|
+
XAdES libraries test themselves (`signxml` in Python, `xadesjs` in Node, `esig/dss` in Java --
|
|
96
|
+
see `spec/fixtures/interop/SOURCES.md`):
|
|
97
|
+
|
|
98
|
+
- **Unit specs** (`spec/xades/`) for each internal component
|
|
99
|
+
- **Conformance specs** (`spec/conformance/`), asserting the exact BASELINE-B structural
|
|
100
|
+
requirements a signature must (and must not) satisfy, mirroring `esig/dss`'s
|
|
101
|
+
`XAdESBaselineBTest` pattern
|
|
102
|
+
- **Interop specs** (`spec/interop/`), verifying real-world XAdES documents vendored from other
|
|
103
|
+
projects' test suites, plus a golden fixture built against KSeF's published `AuthTokenRequest`
|
|
104
|
+
schema and XAdES parameter constraints
|
|
105
|
+
|
|
106
|
+
## Contributing
|
|
107
|
+
|
|
108
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/khasinski/xades.
|
|
109
|
+
|
|
110
|
+
## License
|
|
111
|
+
|
|
112
|
+
The gem's own code is available as open source under the [MIT License](LICENSE.txt). Some test
|
|
113
|
+
fixtures under `spec/fixtures/interop/` are vendored from other projects under their own licenses
|
|
114
|
+
-- see `NOTICE` and `spec/fixtures/interop/SOURCES.md`.
|
data/Rakefile
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Xades
|
|
4
|
+
module Algorithms
|
|
5
|
+
DS_NAMESPACE = "http://www.w3.org/2000/09/xmldsig#"
|
|
6
|
+
XADES_NAMESPACE = "http://uri.etsi.org/01903/v1.3.2#"
|
|
7
|
+
XADES_SIGNED_PROPERTIES_TYPE = "http://uri.etsi.org/01903#SignedProperties"
|
|
8
|
+
|
|
9
|
+
C14N_EXCLUSIVE = "http://www.w3.org/2001/10/xml-exc-c14n#"
|
|
10
|
+
ENVELOPED_SIGNATURE = "http://www.w3.org/2000/09/xmldsig#enveloped-signature"
|
|
11
|
+
|
|
12
|
+
DIGEST_SHA256 = "http://www.w3.org/2001/04/xmlenc#sha256"
|
|
13
|
+
|
|
14
|
+
SIGNATURE_RSA_SHA256 = "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"
|
|
15
|
+
SIGNATURE_ECDSA_SHA256 = "http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha256"
|
|
16
|
+
end
|
|
17
|
+
end
|
data/lib/xades/bes.rb
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Xades
|
|
4
|
+
# Public entry point: XAdES-BES / BASELINE-B signing and verification.
|
|
5
|
+
#
|
|
6
|
+
# certificate = Xades::Certificate.from_pem(cert: File.read("cert.pem"), key: File.read("key.pem"))
|
|
7
|
+
# signed_xml = Xades::Bes.sign(xml, certificate: certificate)
|
|
8
|
+
# result = Xades::Bes.verify(signed_xml)
|
|
9
|
+
# result.valid? # => true
|
|
10
|
+
module Bes
|
|
11
|
+
def self.sign(xml, certificate:, signing_time: Time.now.utc, signing_certificate_version: :v2)
|
|
12
|
+
Signer.new(
|
|
13
|
+
certificate: certificate,
|
|
14
|
+
signing_time: signing_time,
|
|
15
|
+
signing_certificate_version: signing_certificate_version
|
|
16
|
+
).sign(xml)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def self.verify(xml)
|
|
20
|
+
Verifier.verify(xml)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Xades
|
|
4
|
+
module Builder
|
|
5
|
+
# Builds <ds:KeyInfo><ds:X509Data><ds:X509Certificate>.
|
|
6
|
+
module KeyInfo
|
|
7
|
+
def self.build(certificate:, id:)
|
|
8
|
+
<<~XML.strip
|
|
9
|
+
<ds:KeyInfo xmlns:ds="#{Algorithms::DS_NAMESPACE}" Id="#{id}"><ds:X509Data><ds:X509Certificate>#{certificate.base64_der}</ds:X509Certificate></ds:X509Data></ds:KeyInfo>
|
|
10
|
+
XML
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Xades
|
|
4
|
+
module Builder
|
|
5
|
+
# Builds the <xades:QualifyingProperties>/<xades:SignedProperties> fragment: SigningTime +
|
|
6
|
+
# SigningCertificate (V1, ds:X509IssuerName/X509SerialNumber) or V2 (xades:IssuerSerialV2, RFC 5035).
|
|
7
|
+
module QualifyingProperties
|
|
8
|
+
def self.build(certificate:, signature_id:, signed_properties_id:, signing_time:, version: :v2)
|
|
9
|
+
<<~XML.strip
|
|
10
|
+
<xades:QualifyingProperties xmlns:xades="#{Algorithms::XADES_NAMESPACE}" xmlns:ds="#{Algorithms::DS_NAMESPACE}" Target="##{signature_id}"><xades:SignedProperties Id="#{signed_properties_id}"><xades:SignedSignatureProperties><xades:SigningTime>#{format_time(signing_time)}</xades:SigningTime>#{signing_certificate(certificate, version)}</xades:SignedSignatureProperties></xades:SignedProperties></xades:QualifyingProperties>
|
|
11
|
+
XML
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def self.format_time(time)
|
|
15
|
+
time.utc.strftime("%Y-%m-%dT%H:%M:%SZ")
|
|
16
|
+
end
|
|
17
|
+
private_class_method :format_time
|
|
18
|
+
|
|
19
|
+
def self.signing_certificate(certificate, version)
|
|
20
|
+
case version
|
|
21
|
+
when :v2 then signing_certificate_v2(certificate)
|
|
22
|
+
when :v1 then signing_certificate_v1(certificate)
|
|
23
|
+
else raise ArgumentError, "signing_certificate_version must be :v1 or :v2, got #{version.inspect}"
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
private_class_method :signing_certificate
|
|
27
|
+
|
|
28
|
+
def self.cert_digest(certificate)
|
|
29
|
+
<<~XML.strip
|
|
30
|
+
<ds:DigestMethod Algorithm="#{Algorithms::DIGEST_SHA256}"/><ds:DigestValue>#{certificate.sha256_digest_base64}</ds:DigestValue>
|
|
31
|
+
XML
|
|
32
|
+
end
|
|
33
|
+
private_class_method :cert_digest
|
|
34
|
+
|
|
35
|
+
def self.signing_certificate_v2(certificate)
|
|
36
|
+
<<~XML.strip
|
|
37
|
+
<xades:SigningCertificateV2><xades:Cert><xades:CertDigest>#{cert_digest(certificate)}</xades:CertDigest><xades:IssuerSerialV2>#{certificate.issuer_serial_v2_base64}</xades:IssuerSerialV2></xades:Cert></xades:SigningCertificateV2>
|
|
38
|
+
XML
|
|
39
|
+
end
|
|
40
|
+
private_class_method :signing_certificate_v2
|
|
41
|
+
|
|
42
|
+
def self.signing_certificate_v1(certificate)
|
|
43
|
+
<<~XML.strip
|
|
44
|
+
<xades:SigningCertificate><xades:Cert><xades:CertDigest>#{cert_digest(certificate)}</xades:CertDigest><xades:IssuerSerial><ds:X509IssuerName>#{Util.escape_xml_text(certificate.issuer_name)}</ds:X509IssuerName><ds:X509SerialNumber>#{certificate.serial}</ds:X509SerialNumber></xades:IssuerSerial></xades:Cert></xades:SigningCertificate>
|
|
45
|
+
XML
|
|
46
|
+
end
|
|
47
|
+
private_class_method :signing_certificate_v1
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Xades
|
|
4
|
+
module Builder
|
|
5
|
+
# Builds <ds:SignedInfo>: the two References required by XAdES-BASELINE-B (the enveloped
|
|
6
|
+
# document, and the SignedProperties) plus the canonicalization/signature method declarations.
|
|
7
|
+
module SignedInfo
|
|
8
|
+
def self.build(signature_method:, document_digest:, document_reference_id:, signed_properties_id:,
|
|
9
|
+
signed_properties_digest:)
|
|
10
|
+
<<~XML.strip
|
|
11
|
+
<ds:SignedInfo xmlns:ds="#{Algorithms::DS_NAMESPACE}"><ds:CanonicalizationMethod Algorithm="#{Algorithms::C14N_EXCLUSIVE}"/><ds:SignatureMethod Algorithm="#{signature_method}"/><ds:Reference Id="#{document_reference_id}" URI=""><ds:Transforms><ds:Transform Algorithm="#{Algorithms::ENVELOPED_SIGNATURE}"/><ds:Transform Algorithm="#{Algorithms::C14N_EXCLUSIVE}"/></ds:Transforms><ds:DigestMethod Algorithm="#{Algorithms::DIGEST_SHA256}"/><ds:DigestValue>#{document_digest}</ds:DigestValue></ds:Reference><ds:Reference Type="#{Algorithms::XADES_SIGNED_PROPERTIES_TYPE}" URI="##{signed_properties_id}"><ds:Transforms><ds:Transform Algorithm="#{Algorithms::C14N_EXCLUSIVE}"/></ds:Transforms><ds:DigestMethod Algorithm="#{Algorithms::DIGEST_SHA256}"/><ds:DigestValue>#{signed_properties_digest}</ds:DigestValue></ds:Reference></ds:SignedInfo>
|
|
12
|
+
XML
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
data/lib/xades/c14n.rb
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Xades
|
|
4
|
+
# Wraps Nokogiri's native Exclusive XML Canonicalization (C14N 1.0, no comments),
|
|
5
|
+
# the only canonicalization method KSeF / ETSI XAdES-BASELINE-B require.
|
|
6
|
+
module C14N
|
|
7
|
+
MODE = Nokogiri::XML::XML_C14N_EXCLUSIVE_1_0
|
|
8
|
+
|
|
9
|
+
def self.canonicalize(node)
|
|
10
|
+
node.canonicalize(MODE)
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Xades
|
|
4
|
+
# Wraps an X.509 certificate together with its private key, loaded from PEM or PKCS#12.
|
|
5
|
+
class Certificate
|
|
6
|
+
attr_reader :x509, :key
|
|
7
|
+
|
|
8
|
+
def initialize(x509:, key:)
|
|
9
|
+
unless key.is_a?(OpenSSL::PKey::RSA) || key.is_a?(OpenSSL::PKey::EC)
|
|
10
|
+
raise UnsupportedKeyError,
|
|
11
|
+
"key must be an RSA or EC private key"
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
@x509 = x509
|
|
15
|
+
@key = key
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def self.from_pem(cert:, key:, password: nil)
|
|
19
|
+
new(x509: OpenSSL::X509::Certificate.new(cert), key: OpenSSL::PKey.read(key, password))
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# +pkcs12+ may be raw PKCS#12 bytes, an IO-like object, or a path to a .p12/.pfx file.
|
|
23
|
+
def self.from_pkcs12(pkcs12, password)
|
|
24
|
+
data = if pkcs12.respond_to?(:read)
|
|
25
|
+
pkcs12.read
|
|
26
|
+
elsif pkcs12.is_a?(String) && !pkcs12.include?("\x00") && File.exist?(pkcs12)
|
|
27
|
+
File.binread(pkcs12)
|
|
28
|
+
else
|
|
29
|
+
pkcs12
|
|
30
|
+
end
|
|
31
|
+
p12 = OpenSSL::PKCS12.new(data, password)
|
|
32
|
+
new(x509: p12.certificate, key: p12.key)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def rsa?
|
|
36
|
+
key.is_a?(OpenSSL::PKey::RSA)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def ec?
|
|
40
|
+
key.is_a?(OpenSSL::PKey::EC)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def base64_der
|
|
44
|
+
Base64.strict_encode64(x509.to_der)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def sha256_digest_base64
|
|
48
|
+
Xades::Digest.sha256_base64(x509.to_der)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# RFC 2253 string form of the issuer DN, as used by ds:X509IssuerName (SigningCertificate V1).
|
|
52
|
+
def issuer_name
|
|
53
|
+
x509.issuer.to_s(OpenSSL::X509::Name::RFC2253).force_encoding(Encoding::UTF_8)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def serial
|
|
57
|
+
x509.serial.to_s
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# DER-encoded IssuerSerial (RFC 5035 IssuerSerial: SEQUENCE { GeneralNames, CertificateSerialNumber }),
|
|
61
|
+
# base64-encoded, as used by xades:IssuerSerialV2 (SigningCertificate V2). Verified byte-for-byte against
|
|
62
|
+
# a real DSS-generated fixture during development.
|
|
63
|
+
def issuer_serial_v2_base64
|
|
64
|
+
self.class.issuer_serial_v2_base64_for(x509)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def self.issuer_serial_v2_base64_for(x509)
|
|
68
|
+
issuer_asn1 = OpenSSL::ASN1.decode(x509.issuer.to_der)
|
|
69
|
+
general_name = OpenSSL::ASN1::ASN1Data.new([issuer_asn1], 4, :CONTEXT_SPECIFIC)
|
|
70
|
+
general_names = OpenSSL::ASN1::Sequence.new([general_name])
|
|
71
|
+
serial_asn1 = OpenSSL::ASN1::Integer.new(x509.serial)
|
|
72
|
+
issuer_serial = OpenSSL::ASN1::Sequence.new([general_names, serial_asn1])
|
|
73
|
+
Base64.strict_encode64(issuer_serial.to_der)
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
data/lib/xades/digest.rb
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Xades
|
|
4
|
+
# XMLDSig (RFC 6931) requires ECDSA SignatureValue as the raw concatenation r || s, each
|
|
5
|
+
# padded to the curve's field width -- not the DER SEQUENCE{r,s} that OpenSSL produces/expects.
|
|
6
|
+
# Conversion verified against 200+ generated signatures during development (see scratchpad).
|
|
7
|
+
module EcdsaSignature
|
|
8
|
+
FIELD_BYTES = {
|
|
9
|
+
"prime256v1" => 32,
|
|
10
|
+
"secp384r1" => 48,
|
|
11
|
+
"secp521r1" => 66
|
|
12
|
+
}.freeze
|
|
13
|
+
|
|
14
|
+
def self.field_bytes_for(ec_key)
|
|
15
|
+
FIELD_BYTES.fetch(ec_key.group.curve_name) do
|
|
16
|
+
raise UnsupportedKeyError,
|
|
17
|
+
"Unsupported EC curve: #{ec_key.group.curve_name} (supported: #{FIELD_BYTES.keys.join(", ")})"
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def self.der_to_raw(der, byte_len)
|
|
22
|
+
r, s = OpenSSL::ASN1.decode(der).value
|
|
23
|
+
pad(r.value, byte_len) + pad(s.value, byte_len)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def self.raw_to_der(raw, byte_len)
|
|
27
|
+
raise MalformedDocumentError, "ECDSA SignatureValue has wrong length" unless raw.bytesize == byte_len * 2
|
|
28
|
+
|
|
29
|
+
r = OpenSSL::BN.new(raw[0, byte_len], 2)
|
|
30
|
+
s = OpenSSL::BN.new(raw[byte_len, byte_len], 2)
|
|
31
|
+
OpenSSL::ASN1::Sequence.new([OpenSSL::ASN1::Integer.new(r), OpenSSL::ASN1::Integer.new(s)]).to_der
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def self.pad(integer, byte_len)
|
|
35
|
+
integer.to_s(2).rjust(byte_len, "\x00")
|
|
36
|
+
end
|
|
37
|
+
private_class_method :pad
|
|
38
|
+
end
|
|
39
|
+
end
|
data/lib/xades/errors.rb
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Xades
|
|
4
|
+
class Error < StandardError; end
|
|
5
|
+
|
|
6
|
+
# Raised when a key type or curve isn't supported (only RSA and EC P-256/P-384/P-521 are).
|
|
7
|
+
class UnsupportedKeyError < Error; end
|
|
8
|
+
|
|
9
|
+
# Raised when the input XML cannot be parsed or is missing an expected signature structure.
|
|
10
|
+
class MalformedDocumentError < Error; end
|
|
11
|
+
end
|
data/lib/xades/signer.rb
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Xades
|
|
4
|
+
# Produces an XAdES-BES / BASELINE-B enveloped signature on an XML document.
|
|
5
|
+
#
|
|
6
|
+
# Build order (each step's correctness was verified empirically against Nokogiri/OpenSSL
|
|
7
|
+
# behavior and a real DSS-generated fixture before this was written -- see scratchpad notes):
|
|
8
|
+
# 1. Digest the original document (no Signature present yet == enveloped transform is a no-op).
|
|
9
|
+
# 2. Build the whole <ds:Signature> subtree as a self-contained fragment (its own xmlns:ds /
|
|
10
|
+
# xmlns:xades declarations) with a placeholder SignedProperties digest and SignatureValue,
|
|
11
|
+
# and graft it onto the document root.
|
|
12
|
+
# 3. Now that SignedProperties is embedded with real ancestor context, canonicalize it and
|
|
13
|
+
# patch its real digest into the (still embedded) SignedInfo/Reference.
|
|
14
|
+
# 4. Canonicalize the now-final SignedInfo node -- that's the exact byte sequence signed.
|
|
15
|
+
class Signer
|
|
16
|
+
def initialize(certificate:, signing_time: Time.now.utc, signing_certificate_version: :v2, id_prefix: "xades")
|
|
17
|
+
@certificate = certificate
|
|
18
|
+
@signing_time = signing_time
|
|
19
|
+
@signing_certificate_version = signing_certificate_version
|
|
20
|
+
@id_prefix = id_prefix
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def sign(xml)
|
|
24
|
+
doc = Nokogiri::XML(xml.to_s)
|
|
25
|
+
raise MalformedDocumentError, "document has no root element" unless doc.root
|
|
26
|
+
|
|
27
|
+
document_digest = Xades::Digest.sha256_base64(Xades::C14N.canonicalize(doc.root))
|
|
28
|
+
signature_node = doc.root.add_child(doc.fragment(build_skeleton(document_digest)))
|
|
29
|
+
|
|
30
|
+
patch_signed_properties_digest!(signature_node)
|
|
31
|
+
patch_signature_value!(signature_node)
|
|
32
|
+
|
|
33
|
+
# Nokogiri's default to_xml applies FORMAT (pretty-printing), which inserts new whitespace
|
|
34
|
+
# text nodes on reparse -- and canonicalization treats whitespace as significant, so that
|
|
35
|
+
# would silently invalidate every digest we just computed. AS_XML alone preserves exactly
|
|
36
|
+
# the tree we signed.
|
|
37
|
+
doc.to_xml(save_with: Nokogiri::XML::Node::SaveOptions::AS_XML)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
private
|
|
41
|
+
|
|
42
|
+
Ids = Struct.new(:signature, :document_reference, :signed_properties, :key_info, :signature_value)
|
|
43
|
+
|
|
44
|
+
def generate_ids
|
|
45
|
+
Ids.new(*%w[signature ref signedprops keyinfo sigvalue].map { |name| Util.random_id("#{@id_prefix}-#{name}") })
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# SignedProperties' real digest isn't known yet at this point (it depends on the certificate
|
|
49
|
+
# and signing time, not the placeholder digest below) -- it's patched in once this skeleton is
|
|
50
|
+
# embedded in the document, see #patch_signed_properties_digest!.
|
|
51
|
+
def build_skeleton(document_digest)
|
|
52
|
+
ids = generate_ids
|
|
53
|
+
parts = {
|
|
54
|
+
signed_info: build_signed_info(ids, document_digest),
|
|
55
|
+
key_info: Builder::KeyInfo.build(certificate: @certificate, id: ids.key_info),
|
|
56
|
+
qualifying_properties: build_qualifying_properties(ids)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
assemble_signature_xml(ids, **parts)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def build_signed_info(ids, document_digest)
|
|
63
|
+
Builder::SignedInfo.build(
|
|
64
|
+
signature_method: @certificate.ec? ? Algorithms::SIGNATURE_ECDSA_SHA256 : Algorithms::SIGNATURE_RSA_SHA256,
|
|
65
|
+
document_digest: document_digest,
|
|
66
|
+
document_reference_id: ids.document_reference,
|
|
67
|
+
signed_properties_id: ids.signed_properties,
|
|
68
|
+
signed_properties_digest: ""
|
|
69
|
+
)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def build_qualifying_properties(ids)
|
|
73
|
+
Builder::QualifyingProperties.build(
|
|
74
|
+
certificate: @certificate,
|
|
75
|
+
signature_id: ids.signature,
|
|
76
|
+
signed_properties_id: ids.signed_properties,
|
|
77
|
+
signing_time: @signing_time,
|
|
78
|
+
version: @signing_certificate_version
|
|
79
|
+
)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def assemble_signature_xml(ids, signed_info:, key_info:, qualifying_properties:)
|
|
83
|
+
<<~XML.strip
|
|
84
|
+
<ds:Signature xmlns:ds="#{Algorithms::DS_NAMESPACE}" Id="#{ids.signature}">#{signed_info}<ds:SignatureValue Id="#{ids.signature_value}"></ds:SignatureValue>#{key_info}<ds:Object>#{qualifying_properties}</ds:Object></ds:Signature>
|
|
85
|
+
XML
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def patch_signed_properties_digest!(signature_node)
|
|
89
|
+
signed_properties_node = signature_node.at_xpath(".//*[local-name()='SignedProperties']")
|
|
90
|
+
digest = Xades::Digest.sha256_base64(Xades::C14N.canonicalize(signed_properties_node))
|
|
91
|
+
|
|
92
|
+
digest_value_node = signature_node.at_xpath(
|
|
93
|
+
".//*[local-name()='Reference'][@Type='#{Algorithms::XADES_SIGNED_PROPERTIES_TYPE}']/*[local-name()='DigestValue']"
|
|
94
|
+
)
|
|
95
|
+
digest_value_node.content = digest
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def patch_signature_value!(signature_node)
|
|
99
|
+
signed_info_node = signature_node.at_xpath("./*[local-name()='SignedInfo']")
|
|
100
|
+
bytes_to_sign = Xades::C14N.canonicalize(signed_info_node)
|
|
101
|
+
|
|
102
|
+
signature_value_node = signature_node.at_xpath("./*[local-name()='SignatureValue']")
|
|
103
|
+
signature_value_node.content = compute_signature(bytes_to_sign)
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def compute_signature(bytes)
|
|
107
|
+
digest = OpenSSL::Digest.new("SHA256")
|
|
108
|
+
if @certificate.ec?
|
|
109
|
+
der = @certificate.key.sign(digest, bytes)
|
|
110
|
+
raw = EcdsaSignature.der_to_raw(der, EcdsaSignature.field_bytes_for(@certificate.key))
|
|
111
|
+
Base64.strict_encode64(raw)
|
|
112
|
+
else
|
|
113
|
+
Base64.strict_encode64(@certificate.key.sign(digest, bytes))
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
end
|
data/lib/xades/util.rb
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Xades
|
|
4
|
+
# @api private
|
|
5
|
+
module Util
|
|
6
|
+
module_function
|
|
7
|
+
|
|
8
|
+
# Escapes text for use inside XML element content (not attribute values).
|
|
9
|
+
def escape_xml_text(str)
|
|
10
|
+
str.to_s.gsub("&", "&").gsub("<", "<").gsub(">", ">")
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def random_id(prefix)
|
|
14
|
+
"#{prefix}-#{SecureRandom.hex(16)}"
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Xades
|
|
4
|
+
# Verifies the structural and cryptographic correctness of an XAdES-BES signature: that the
|
|
5
|
+
# document/SignedProperties digests match and that SignatureValue matches SignedInfo under the
|
|
6
|
+
# embedded certificate's public key.
|
|
7
|
+
#
|
|
8
|
+
# This does NOT validate certificate trust, chain, revocation or validity period -- that is a
|
|
9
|
+
# separate concern (deliberately out of scope for BES-level verification; see the gem's README).
|
|
10
|
+
class Verifier
|
|
11
|
+
Result = Struct.new(:valid, :errors) do
|
|
12
|
+
def valid?
|
|
13
|
+
valid
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def self.verify(xml)
|
|
18
|
+
new(xml).verify
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def initialize(xml)
|
|
22
|
+
@doc = Nokogiri::XML(xml.to_s)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def verify
|
|
26
|
+
errors = []
|
|
27
|
+
|
|
28
|
+
signature_node = @doc.at_xpath("//*[local-name()='Signature']")
|
|
29
|
+
return failure("no Signature element found") unless signature_node
|
|
30
|
+
|
|
31
|
+
signed_info_node = signature_node.at_xpath("./*[local-name()='SignedInfo']")
|
|
32
|
+
return failure("no SignedInfo element found") unless signed_info_node
|
|
33
|
+
|
|
34
|
+
verify_document_digest(signature_node, signed_info_node, errors)
|
|
35
|
+
verify_signed_properties_digest(signature_node, signed_info_node, errors)
|
|
36
|
+
verify_signature_value(signature_node, signed_info_node, errors)
|
|
37
|
+
verify_signing_certificate_binding(signature_node, errors)
|
|
38
|
+
|
|
39
|
+
Result.new(errors.empty?, errors)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
private
|
|
43
|
+
|
|
44
|
+
def failure(message)
|
|
45
|
+
Result.new(false, [message])
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def verify_document_digest(_signature_node, signed_info_node, errors)
|
|
49
|
+
reference = signed_info_node.at_xpath(".//*[local-name()='Reference'][not(@Type)]")
|
|
50
|
+
return errors << "missing document Reference in SignedInfo" unless reference
|
|
51
|
+
|
|
52
|
+
expected = reference.at_xpath("./*[local-name()='DigestValue']")&.text
|
|
53
|
+
clone = @doc.dup(1)
|
|
54
|
+
clone.at_xpath("//*[local-name()='Signature']").unlink
|
|
55
|
+
actual = Xades::Digest.sha256_base64(Xades::C14N.canonicalize(clone.root))
|
|
56
|
+
|
|
57
|
+
errors << "document digest mismatch" unless expected == actual
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def verify_signed_properties_digest(signature_node, signed_info_node, errors)
|
|
61
|
+
reference = signed_info_node.at_xpath(
|
|
62
|
+
".//*[local-name()='Reference'][@Type='#{Algorithms::XADES_SIGNED_PROPERTIES_TYPE}']"
|
|
63
|
+
)
|
|
64
|
+
return errors << "missing SignedProperties Reference in SignedInfo" unless reference
|
|
65
|
+
|
|
66
|
+
signed_properties_node = signature_node.at_xpath(".//*[local-name()='SignedProperties']")
|
|
67
|
+
return errors << "missing SignedProperties element" unless signed_properties_node
|
|
68
|
+
|
|
69
|
+
expected = reference.at_xpath("./*[local-name()='DigestValue']")&.text
|
|
70
|
+
actual = Xades::Digest.sha256_base64(Xades::C14N.canonicalize(signed_properties_node))
|
|
71
|
+
|
|
72
|
+
errors << "SignedProperties digest mismatch" unless expected == actual
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
KNOWN_SIGNATURE_METHODS = [Algorithms::SIGNATURE_RSA_SHA256, Algorithms::SIGNATURE_ECDSA_SHA256].freeze
|
|
76
|
+
|
|
77
|
+
def verify_signature_value(signature_node, signed_info_node, errors)
|
|
78
|
+
signature_value_node = signature_node.at_xpath("./*[local-name()='SignatureValue']")
|
|
79
|
+
cert_text = signature_node.at_xpath(".//*[local-name()='X509Certificate']")&.text
|
|
80
|
+
return errors << "missing SignatureValue or X509Certificate" unless signature_value_node && cert_text
|
|
81
|
+
|
|
82
|
+
signature_method = signed_info_node.at_xpath("./*[local-name()='SignatureMethod']")&.[]("Algorithm")
|
|
83
|
+
unless KNOWN_SIGNATURE_METHODS.include?(signature_method)
|
|
84
|
+
return errors << "unsupported SignatureMethod: #{signature_method.inspect}"
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
cert = OpenSSL::X509::Certificate.new(Base64.decode64(cert_text))
|
|
88
|
+
raw_signature = Base64.decode64(signature_value_node.text)
|
|
89
|
+
bytes = Xades::C14N.canonicalize(signed_info_node)
|
|
90
|
+
|
|
91
|
+
errors << "signature does not match certificate" unless signature_valid?(cert, signature_method, raw_signature,
|
|
92
|
+
bytes)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def signature_valid?(cert, signature_method, raw_signature, bytes)
|
|
96
|
+
der = to_der_signature(signature_method, raw_signature)
|
|
97
|
+
cert.public_key.verify(OpenSSL::Digest.new("SHA256"), der, bytes)
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def to_der_signature(signature_method, raw_signature)
|
|
101
|
+
return raw_signature unless signature_method == Algorithms::SIGNATURE_ECDSA_SHA256
|
|
102
|
+
|
|
103
|
+
EcdsaSignature.raw_to_der(raw_signature, raw_signature.bytesize / 2)
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
DIGEST_ALGORITHMS = {
|
|
107
|
+
"http://www.w3.org/2001/04/xmlenc#sha256" => OpenSSL::Digest::SHA256,
|
|
108
|
+
"http://www.w3.org/2001/04/xmlenc#sha512" => OpenSSL::Digest::SHA512,
|
|
109
|
+
"http://www.w3.org/2000/09/xmldsig#sha1" => OpenSSL::Digest::SHA1
|
|
110
|
+
}.freeze
|
|
111
|
+
|
|
112
|
+
CERT_DIGEST_XPATH = ".//*[local-name()='SigningCertificate' or local-name()='SigningCertificateV2']" \
|
|
113
|
+
"//*[local-name()='CertDigest']"
|
|
114
|
+
|
|
115
|
+
# xades:SigningCertificate(V2)/.../CertDigest is a signed *claim* about which certificate was
|
|
116
|
+
# used. It's covered by the signature (it's inside SignedProperties), so it can't be tampered
|
|
117
|
+
# with in isolation -- but nothing else stops it from simply being wrong/stale from the start
|
|
118
|
+
# (a "certificate substitution" nonconformance some real-world XAdES validators, incl. DSS,
|
|
119
|
+
# reject). We bind the claim to the certificate actually present in KeyInfo.
|
|
120
|
+
def verify_signing_certificate_binding(signature_node, errors)
|
|
121
|
+
cert_digest_node = signature_node.at_xpath(CERT_DIGEST_XPATH)
|
|
122
|
+
return unless cert_digest_node # SigningCertificate is optional at the schema level; nothing to bind
|
|
123
|
+
|
|
124
|
+
cert_text = signature_node.at_xpath(".//*[local-name()='X509Certificate']")&.text
|
|
125
|
+
return errors << "SigningCertificate present but no X509Certificate to bind it to" unless cert_text
|
|
126
|
+
|
|
127
|
+
verify_cert_digest(cert_digest_node, cert_text, errors)
|
|
128
|
+
verify_issuer_serial_v2_binding(signature_node, cert_text, errors)
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def verify_cert_digest(cert_digest_node, cert_text, errors)
|
|
132
|
+
algorithm_uri = cert_digest_node.at_xpath("./*[local-name()='DigestMethod']")&.[]("Algorithm")
|
|
133
|
+
digest_class = DIGEST_ALGORITHMS[algorithm_uri]
|
|
134
|
+
return errors << "unsupported CertDigest algorithm: #{algorithm_uri.inspect}" unless digest_class
|
|
135
|
+
|
|
136
|
+
expected = cert_digest_node.at_xpath("./*[local-name()='DigestValue']")&.text
|
|
137
|
+
actual = Base64.strict_encode64(digest_class.digest(Base64.decode64(cert_text)))
|
|
138
|
+
|
|
139
|
+
errors << "SigningCertificate digest does not match the embedded certificate" unless expected == actual
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# IssuerSerialV2 is DER (RFC 5035), so a byte comparison against a freshly-recomputed value is
|
|
143
|
+
# exact and implementation-independent (verified against a real DSS fixture during development).
|
|
144
|
+
# We deliberately do NOT attempt the same for the V1 xades:IssuerSerial form: it's a
|
|
145
|
+
# ds:X509IssuerName *string*, and RFC 2253 rendering differs enough across implementations
|
|
146
|
+
# (whitespace, escaping, attribute case) that comparing it byte-for-byte would produce false
|
|
147
|
+
# mismatches on legitimately-valid third-party documents.
|
|
148
|
+
def verify_issuer_serial_v2_binding(signature_node, cert_text, errors)
|
|
149
|
+
issuer_serial_v2_node = signature_node.at_xpath(".//*[local-name()='IssuerSerialV2']")
|
|
150
|
+
return unless issuer_serial_v2_node
|
|
151
|
+
|
|
152
|
+
cert = OpenSSL::X509::Certificate.new(Base64.decode64(cert_text))
|
|
153
|
+
expected = issuer_serial_v2_node.text.strip
|
|
154
|
+
actual = Xades::Certificate.issuer_serial_v2_base64_for(cert)
|
|
155
|
+
|
|
156
|
+
errors << "IssuerSerialV2 does not match the embedded certificate's issuer/serial" unless expected == actual
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
end
|
data/lib/xades.rb
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "openssl"
|
|
4
|
+
require "base64"
|
|
5
|
+
require "securerandom"
|
|
6
|
+
require "nokogiri"
|
|
7
|
+
|
|
8
|
+
require_relative "xades/version"
|
|
9
|
+
require_relative "xades/errors"
|
|
10
|
+
require_relative "xades/algorithms"
|
|
11
|
+
require_relative "xades/util"
|
|
12
|
+
require_relative "xades/c14n"
|
|
13
|
+
require_relative "xades/digest"
|
|
14
|
+
require_relative "xades/certificate"
|
|
15
|
+
require_relative "xades/ecdsa_signature"
|
|
16
|
+
require_relative "xades/builder/key_info"
|
|
17
|
+
require_relative "xades/builder/signed_info"
|
|
18
|
+
require_relative "xades/builder/qualifying_properties"
|
|
19
|
+
require_relative "xades/signer"
|
|
20
|
+
require_relative "xades/verifier"
|
|
21
|
+
require_relative "xades/bes"
|
data/sig/xades.rbs
ADDED
metadata
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: xades
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Chris Hasinski
|
|
8
|
+
bindir: exe
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: base64
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '0'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '0'
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: nokogiri
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - "~>"
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '1.15'
|
|
33
|
+
type: :runtime
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - "~>"
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '1.15'
|
|
40
|
+
description: 'Sign and verify XML documents with XAdES-BES / BASELINE-B advanced electronic
|
|
41
|
+
signatures (ETSI EN 319 132), the profile used for eIDAS-adjacent flows such as
|
|
42
|
+
Poland''s KSeF e-invoicing authentication. Pure MRI: Nokogiri for XML/C14N, OpenSSL
|
|
43
|
+
for RSA/ECDSA.'
|
|
44
|
+
email:
|
|
45
|
+
- krzysztof.hasinski@gmail.com
|
|
46
|
+
executables: []
|
|
47
|
+
extensions: []
|
|
48
|
+
extra_rdoc_files: []
|
|
49
|
+
files:
|
|
50
|
+
- CHANGELOG.md
|
|
51
|
+
- LICENSE.txt
|
|
52
|
+
- NOTICE
|
|
53
|
+
- README.md
|
|
54
|
+
- Rakefile
|
|
55
|
+
- lib/xades.rb
|
|
56
|
+
- lib/xades/algorithms.rb
|
|
57
|
+
- lib/xades/bes.rb
|
|
58
|
+
- lib/xades/builder/key_info.rb
|
|
59
|
+
- lib/xades/builder/qualifying_properties.rb
|
|
60
|
+
- lib/xades/builder/signed_info.rb
|
|
61
|
+
- lib/xades/c14n.rb
|
|
62
|
+
- lib/xades/certificate.rb
|
|
63
|
+
- lib/xades/digest.rb
|
|
64
|
+
- lib/xades/ecdsa_signature.rb
|
|
65
|
+
- lib/xades/errors.rb
|
|
66
|
+
- lib/xades/signer.rb
|
|
67
|
+
- lib/xades/util.rb
|
|
68
|
+
- lib/xades/verifier.rb
|
|
69
|
+
- lib/xades/version.rb
|
|
70
|
+
- sig/xades.rbs
|
|
71
|
+
homepage: https://github.com/khasinski/xades
|
|
72
|
+
licenses:
|
|
73
|
+
- MIT
|
|
74
|
+
metadata:
|
|
75
|
+
homepage_uri: https://github.com/khasinski/xades
|
|
76
|
+
source_code_uri: https://github.com/khasinski/xades
|
|
77
|
+
changelog_uri: https://github.com/khasinski/xades/blob/main/CHANGELOG.md
|
|
78
|
+
rubygems_mfa_required: 'true'
|
|
79
|
+
rdoc_options: []
|
|
80
|
+
require_paths:
|
|
81
|
+
- lib
|
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
83
|
+
requirements:
|
|
84
|
+
- - ">="
|
|
85
|
+
- !ruby/object:Gem::Version
|
|
86
|
+
version: 3.2.0
|
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
88
|
+
requirements:
|
|
89
|
+
- - ">="
|
|
90
|
+
- !ruby/object:Gem::Version
|
|
91
|
+
version: '0'
|
|
92
|
+
requirements: []
|
|
93
|
+
rubygems_version: 4.0.20
|
|
94
|
+
specification_version: 4
|
|
95
|
+
summary: XAdES-BES/BASELINE-B XML signing and verification (ETSI EN 319 132) for pure
|
|
96
|
+
Ruby.
|
|
97
|
+
test_files: []
|