pq_crypto-jwt 0.2.1 → 0.2.3
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 +4 -4
- data/CHANGELOG.md +53 -3
- data/README.md +39 -2
- data/lib/pq_crypto/jwt/base64url.rb +27 -0
- data/lib/pq_crypto/jwt/jwa/detached_signing_input_io.rb +71 -0
- data/lib/pq_crypto/jwt/jwa/ml_dsa.rb +66 -0
- data/lib/pq_crypto/jwt/jwa/ml_dsa_44.rb +1 -19
- data/lib/pq_crypto/jwt/jwa/ml_dsa_65.rb +1 -19
- data/lib/pq_crypto/jwt/jwa/ml_dsa_87.rb +1 -19
- data/lib/pq_crypto/jwt/jwa/ml_dsa_streaming.rb +1 -186
- data/lib/pq_crypto/jwt/jwa/streaming.rb +129 -0
- data/lib/pq_crypto/jwt/jwa.rb +4 -49
- data/lib/pq_crypto/jwt/jwk/akp.rb +50 -39
- data/lib/pq_crypto/jwt/jwk.rb +101 -96
- data/lib/pq_crypto/jwt/jwks/loader.rb +35 -0
- data/lib/pq_crypto/jwt/jwks/validation_cache.rb +39 -0
- data/lib/pq_crypto/jwt/jwks.rb +77 -27
- data/lib/pq_crypto/jwt/keys.rb +30 -45
- data/lib/pq_crypto/jwt/version.rb +1 -1
- data/lib/pq_crypto/jwt.rb +11 -9
- metadata +10 -7
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a1ef792000099bbeefd1b4f1ad2a3b4e5e1e249bbb82f019c82d0fddffce4141
|
|
4
|
+
data.tar.gz: 69d4cf5300f40df6aac37963dcf08ef26e38c40052d404f0a959b6d205ae9253
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 4f760084e0fa96d10cba90bd11f6eb0508620146c029a04510f89101b31c70fb239873176a6e9974b143eff930f4f54646b7c41cef724da2c20c797b4ee52d5b
|
|
7
|
+
data.tar.gz: f8ff2a669c988f448d2e11d9773ea8121b61a670bbcd7b9e9df35327027116ce188138ca87cf80f609a09b7450e20bd13baecf520bdaa7fa0e585d24da40ed55
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,56 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.2.3
|
|
4
|
+
|
|
5
|
+
### Breaking
|
|
6
|
+
|
|
7
|
+
- JWK `pub` / `priv` members and detached JWS segments must be unpadded base64url strings (RFC 7515, RFC 7517).
|
|
8
|
+
Padded, standard-alphabet and non-String values are rejected, so a key or signature has exactly one accepted
|
|
9
|
+
encoding. `PQCrypto::JWT::JWK.base64url_decode` raises `PQCrypto::JWT::Error` for them.
|
|
10
|
+
- `PQCrypto::JWT::MLDSA44/65/87` are frozen instances of `PQCrypto::JWT::JWA::MLDSA`, which includes
|
|
11
|
+
`JWT::JWA::SigningAlgorithm` like ruby-jwt's built-in algorithms. The `::ALG` / `::PQ_CRYPTO_ALGORITHM`
|
|
12
|
+
constants are replaced by `#alg` / `#pq_crypto_algorithm`.
|
|
13
|
+
- `JWT::JWK::AKP#parameters` returns a frozen snapshot and the cryptographic members are frozen copies. Metadata
|
|
14
|
+
still changes through `[]=`. JWK hashes returned by `JWK.from_public_key` / `JWK.from_seed` freeze their values.
|
|
15
|
+
|
|
16
|
+
### Changed
|
|
17
|
+
|
|
18
|
+
- Streaming lives in the `PQCrypto::JWT::JWA::Streaming` mixin; `MLDSAStreaming` remains as an alias and the
|
|
19
|
+
per-algorithm require paths (`pq_crypto/jwt/jwa/ml_dsa_65` etc.) still load the gem.
|
|
20
|
+
- `JWK`, `JWKS` and `Keys` use `extend self` with a `private` section instead of per-method `private_class_method`.
|
|
21
|
+
- CI adds `jwt ~> 3.3.0` to the matrix and a RuboCop lint job.
|
|
22
|
+
|
|
23
|
+
### Fixed
|
|
24
|
+
|
|
25
|
+
- `JWT::JWK::AKP#members` no longer includes `priv`. ruby-jwt's `JWT::JWK::Thumbprint` kid generator hashed the
|
|
26
|
+
private seed for private AKP JWKs, so they got a different `kid` than their public counterpart.
|
|
27
|
+
- `JWT::JWK::AKP` copies its cryptographic members on import, so mutating the caller's strings afterwards can no
|
|
28
|
+
longer desynchronize the exported `pub` from the already parsed public key.
|
|
29
|
+
- `JWKS.find` / `JWKS.find_all` return `nil` / `[]` for non-Hash containers instead of raising `TypeError`, and skip
|
|
30
|
+
entries whose `#to_hash` does not return a Hash.
|
|
31
|
+
|
|
32
|
+
## 0.2.2
|
|
33
|
+
|
|
34
|
+
### Fixed
|
|
35
|
+
|
|
36
|
+
- CI workflow now matches the documented release matrix for Ruby `3.1`, `3.2`, `3.3`, `3.4`, `4.0` crossed with `jwt ~> 3.1.0` and `jwt ~> 3.2.0`.
|
|
37
|
+
- Renamed the synthetic RFC 9964 regression test file to `test_ml_dsa_sizes.rb`; it now describes size and canonical-thumbprint checks without implying full Appendix-vector coverage.
|
|
38
|
+
- `JWT::JWK::AKP` no longer expands and retains private seed material during initialization; private key materialization is lazy and happens only when `#secret_key` / `#signing_key` is requested.
|
|
39
|
+
- `JWKS.from_keys(kids:)` now raises `ArgumentError` when the number of kids does not match the number of public keys.
|
|
40
|
+
- JWKS lookup now ignores structurally invalid AKP JWK entries instead of returning malformed keys from weak filters.
|
|
41
|
+
- JWKS lookup now prefilters by `kid`/`alg` before reconstructing public keys and uses a bounded immutable validation cache for repeated lookups.
|
|
42
|
+
- `JWKS.from_keys(kids:)` now normalizes Array-compatible `kids` objects through `to_ary` before indexing.
|
|
43
|
+
- `JWK.thumbprint` now validates the AKP `pub` length for the declared ML-DSA algorithm before computing the digest.
|
|
44
|
+
- `DetachedSigningInputIO` now implements `#readpartial`, `#binmode`, `#binmode?`, and `#close` for better IO compatibility.
|
|
45
|
+
|
|
46
|
+
### Added
|
|
47
|
+
|
|
48
|
+
- `PQCrypto::JWT::JWK.thumbprint_uri(jwk)` for RFC 9278-style JWK thumbprint URIs.
|
|
49
|
+
- `PQCrypto::JWT::JWKS.from_keys(..., kid_strategy: :thumbprint)` and `kid_strategy: :thumbprint_uri` for automatic key IDs.
|
|
50
|
+
- Optional `strict: true` mode for streaming detached JWS verification, raising `JWT::DecodeError` for structural/token errors and `JWT::VerificationError` for cryptographic verification failures.
|
|
51
|
+
- Thread-safe cache invalidation for `JWKS.loader`.
|
|
52
|
+
- Optional Node `jose` cross-implementation interop test for `ML-DSA-65`, wired as a dedicated CI job.
|
|
53
|
+
|
|
3
54
|
## 0.2.1
|
|
4
55
|
|
|
5
56
|
### Fixed
|
|
@@ -7,12 +58,11 @@
|
|
|
7
58
|
- Streaming detached JWS signing now rejects protected `b64` and `crit` headers instead of allowing semantic mismatch with the base64url-encoded signing input.
|
|
8
59
|
- Streaming detached JWS verification now fails closed for critical headers and `b64: false`.
|
|
9
60
|
- Streaming `chunk_size` is now validated as a positive integer before signing, verification, or detached signing-input reads.
|
|
10
|
-
- CI workflow now matches the documented release matrix for Ruby `3.1`, `3.2`, `3.3`, `3.4`, `4.0` crossed with `jwt ~> 3.1.0` and `jwt ~> 3.2.0`.
|
|
11
61
|
|
|
12
62
|
### Added
|
|
13
63
|
|
|
14
64
|
- Negative streaming tests for `crit`, `b64`, malformed base64url headers, and non-positive chunk sizes.
|
|
15
|
-
-
|
|
65
|
+
- Regression tests for ML-DSA public key/signature sizes and AKP JWK thumbprint canonical members.
|
|
16
66
|
- Explicit tests and documentation for trusted private AKP JWK import without `verify_public: true`.
|
|
17
67
|
|
|
18
68
|
## 0.2.0
|
|
@@ -26,7 +76,7 @@
|
|
|
26
76
|
- PEM/DER loading now delegates to `PQCrypto::Key.from_pem` / `PQCrypto::Key.from_der` for auto-dispatch instead of manually parsing ASN.1 OIDs in the JWT adapter.
|
|
27
77
|
- `signature_length_valid?` now fails closed when signature metadata cannot be resolved.
|
|
28
78
|
- `JWT::JWK.classes` is no longer mutated at require-time; AKP registration happens through `PQCrypto::JWT.register!`.
|
|
29
|
-
-
|
|
79
|
+
- Documented the intended Ruby `3.1`, `3.2`, `3.3`, `3.4`, `4.0` and `jwt >= 3.1, < 4.0` compatibility target.
|
|
30
80
|
|
|
31
81
|
### Added
|
|
32
82
|
|
data/README.md
CHANGED
|
@@ -18,6 +18,8 @@ ML-KEM/JWE is **not included**. Full JWE support needs a separate standards-comp
|
|
|
18
18
|
- `pq_crypto` `~> 0.6.1`
|
|
19
19
|
- `jwt` `>= 3.1`, `< 4.0`
|
|
20
20
|
|
|
21
|
+
The CI release matrix covers Ruby `3.1`, `3.2`, `3.3`, `3.4`, `4.0` crossed with `jwt ~> 3.1.0`, `jwt ~> 3.2.0` and `jwt ~> 3.3.0` on Linux and macOS. A dedicated Node `jose` interop job runs on Node 24 with `JOSE_INTEROP=1`.
|
|
22
|
+
|
|
21
23
|
`pq_crypto-jwt` is Ruby-only and does not ship its own native extension. Native ML-DSA work, seed-aware keys, PKCS#8/SPKI parsing, and streaming signing/verification are delegated to `pq_crypto`.
|
|
22
24
|
|
|
23
25
|
## Install
|
|
@@ -26,6 +28,12 @@ ML-KEM/JWE is **not included**. Full JWE support needs a separate standards-comp
|
|
|
26
28
|
gem "pq_crypto-jwt", "~> 0.2"
|
|
27
29
|
```
|
|
28
30
|
|
|
31
|
+
## Upgrading from 0.2.x
|
|
32
|
+
|
|
33
|
+
- JWK `pub` / `priv` members and detached JWS segments must be unpadded base64url strings (RFC 7515, RFC 7517). Padded or standard-alphabet Base64 values, which 0.2.x tolerated, are now rejected; re-encode them before import.
|
|
34
|
+
- `JWT::JWK::AKP#parameters` returns a frozen snapshot, and the `kty`, `alg`, `pub` and `priv` values are frozen. Change metadata through `akp[:kid] = "..."`.
|
|
35
|
+
- `PQCrypto::JWT::MLDSA44`, `MLDSA65` and `MLDSA87` are frozen algorithm objects instead of modules. Use `.alg` and `.pq_crypto_algorithm` instead of the internal `::ALG` and `::PQ_CRYPTO_ALGORITHM` constants. `require "pq_crypto/jwt/jwa/ml_dsa_65"` and the other per-algorithm paths still work.
|
|
36
|
+
|
|
29
37
|
## Register the algorithms
|
|
30
38
|
|
|
31
39
|
`pq_crypto-jwt` does not register algorithms implicitly. Register once during boot:
|
|
@@ -107,6 +115,8 @@ jwk = PQCrypto::JWT::JWK.from_public_key(
|
|
|
107
115
|
)
|
|
108
116
|
```
|
|
109
117
|
|
|
118
|
+
`pub` and `priv` must be unpadded base64url strings; padded or standard Base64 values are rejected.
|
|
119
|
+
|
|
110
120
|
Private AKP JWK uses RFC 9964 seed format: `priv` is the 32 raw-byte ML-DSA seed encoded as base64url. It is not the expanded ML-DSA secret key, and helper APIs expect raw seed bytes, not hex or text encodings.
|
|
111
121
|
|
|
112
122
|
```ruby
|
|
@@ -129,6 +139,15 @@ jwk = PQCrypto::JWT::JWK.from_seed(seed_32_bytes, alg: "ML-DSA-65", public_key:
|
|
|
129
139
|
|
|
130
140
|
`from_secret_key` is reserved for a future parent release that exposes a public seed accessor. Expanded-only keys, and current `pq_crypto 0.6.1` keys, raise `UnsupportedFeature` instead of reading parent private state.
|
|
131
141
|
|
|
142
|
+
JWK thumbprints are available as raw RFC 7638 thumbprints or RFC 9278-style URIs:
|
|
143
|
+
|
|
144
|
+
```ruby
|
|
145
|
+
thumbprint = PQCrypto::JWT::JWK.thumbprint(jwk)
|
|
146
|
+
thumbprint_uri = PQCrypto::JWT::JWK.thumbprint_uri(jwk)
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
`thumbprint` validates that `pub` has the expected ML-DSA public-key length for the declared `alg` before hashing the canonical AKP members.
|
|
150
|
+
|
|
132
151
|
JWKS lookup with `ruby-jwt`:
|
|
133
152
|
|
|
134
153
|
```ruby
|
|
@@ -140,7 +159,14 @@ token = JWT.encode({ "sub" => "alice" }, keypair.secret_key, "ML-DSA-65", kid: "
|
|
|
140
159
|
payload, header = JWT.decode(token, nil, true, algorithms: ["ML-DSA-65"], jwks: jwks)
|
|
141
160
|
```
|
|
142
161
|
|
|
143
|
-
|
|
162
|
+
`JWKS.from_keys(kids:)` requires one `kid` per public key. For automatic key IDs, derive them from the AKP thumbprint:
|
|
163
|
+
|
|
164
|
+
```ruby
|
|
165
|
+
jwks = PQCrypto::JWT::JWKS.from_keys([keypair.public_key], kid_strategy: :thumbprint)
|
|
166
|
+
jwks = PQCrypto::JWT::JWKS.from_keys([keypair.public_key], kid_strategy: :thumbprint_uri)
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
For rotation, pass `PQCrypto::JWT::JWKS.loader(callable_or_hash)` as the `jwks:` value. The loader is thread-safe and refreshes when `ruby-jwt` calls it with `invalidate: true`.
|
|
144
170
|
|
|
145
171
|
## Streaming detached JWS
|
|
146
172
|
|
|
@@ -173,6 +199,17 @@ PQCrypto::JWT::JWA::MLDSA87
|
|
|
173
199
|
|
|
174
200
|
`verify_io` returns `[payload_position, header]` on success. `payload_position` is `nil` for non-seekable streams that do not respond to `#pos`.
|
|
175
201
|
|
|
202
|
+
By default, streaming verification fails closed and returns `false` for malformed tokens or failed signatures. Use `strict: true` when callers need error classification: structural/token errors raise `JWT::DecodeError`, and cryptographic verification failures raise `JWT::VerificationError`.
|
|
203
|
+
|
|
204
|
+
```ruby
|
|
205
|
+
PQCrypto::JWT::JWA::MLDSA65.verify_io(
|
|
206
|
+
verification_key: keypair.public_key,
|
|
207
|
+
token: token,
|
|
208
|
+
payload_io: payload_io,
|
|
209
|
+
strict: true
|
|
210
|
+
)
|
|
211
|
+
```
|
|
212
|
+
|
|
176
213
|
The streaming helper signs the regular compact JWS signing input with a base64url-encoded payload. It does not implement RFC 7797 unencoded payload mode. `sign_io` rejects protected `b64` and `crit` header fields, and `verify_io` fails closed for critical headers or `b64: false`. `chunk_size` must be a positive integer.
|
|
177
214
|
|
|
178
215
|
## Non-goals
|
|
@@ -196,7 +233,7 @@ JWK seed encoding; backed by pq_crypto, which should also be reviewed before
|
|
|
196
233
|
production use.
|
|
197
234
|
```
|
|
198
235
|
|
|
199
|
-
Use in production only after your own security review and interoperability testing.
|
|
236
|
+
Use in production only after your own security review and interoperability testing. The repository includes an optional Node `jose` interop test (`JOSE_INTEROP=1 bundle exec ruby test/test_jose_interop.rb`) for the ML-DSA-65 JWS/JWK path.
|
|
200
237
|
|
|
201
238
|
## License
|
|
202
239
|
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "base64"
|
|
4
|
+
|
|
5
|
+
module PQCrypto
|
|
6
|
+
module JWT
|
|
7
|
+
module Base64URL
|
|
8
|
+
module_function
|
|
9
|
+
|
|
10
|
+
def encode(bytes)
|
|
11
|
+
Base64.urlsafe_encode64(String(bytes), padding: false)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def decode(value)
|
|
15
|
+
raise ArgumentError, "base64url value must be a String" unless value.is_a?(String)
|
|
16
|
+
|
|
17
|
+
bytes = value.b
|
|
18
|
+
unless /\A[A-Za-z0-9_-]*\z/n.match?(bytes)
|
|
19
|
+
raise ArgumentError, "base64url must use the unpadded URL-safe alphabet"
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
Base64.urlsafe_decode64(bytes)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
private_constant :Base64URL
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "base64"
|
|
4
|
+
|
|
5
|
+
module PQCrypto
|
|
6
|
+
module JWT
|
|
7
|
+
module JWA
|
|
8
|
+
class DetachedSigningInputIO
|
|
9
|
+
def initialize(encoded_header, payload_io, chunk_size: Streaming::DEFAULT_CHUNK_SIZE)
|
|
10
|
+
@chunk_size = Streaming.validate_chunk_size!(chunk_size)
|
|
11
|
+
@payload_io = payload_io
|
|
12
|
+
@buffer = "#{encoded_header}.".b
|
|
13
|
+
@carry = "".b
|
|
14
|
+
@eof = false
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def read(length = nil, outbuf = nil)
|
|
18
|
+
raise ArgumentError, "negative length #{length} given" if length&.negative?
|
|
19
|
+
return emit(+"", outbuf) if length&.zero?
|
|
20
|
+
|
|
21
|
+
fill(length)
|
|
22
|
+
emit(take(length || buffer.bytesize), outbuf) unless buffer.empty?
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def readpartial(maxlen, outbuf = nil)
|
|
26
|
+
result = read(maxlen, outbuf)
|
|
27
|
+
raise EOFError if result.nil? || result.empty?
|
|
28
|
+
|
|
29
|
+
result
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def binmode = self
|
|
33
|
+
def binmode? = true
|
|
34
|
+
def close = nil
|
|
35
|
+
|
|
36
|
+
private
|
|
37
|
+
|
|
38
|
+
attr_reader :chunk_size, :payload_io
|
|
39
|
+
attr_accessor :buffer, :carry, :eof
|
|
40
|
+
|
|
41
|
+
def emit(result, outbuf) = outbuf ? outbuf.replace(result) : result
|
|
42
|
+
|
|
43
|
+
def take(length)
|
|
44
|
+
head = buffer.byteslice(0, length)
|
|
45
|
+
self.buffer = buffer.byteslice(head.bytesize..)
|
|
46
|
+
head
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def fill(length)
|
|
50
|
+
feed until eof || (length && buffer.bytesize >= length)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def feed
|
|
54
|
+
chunk = payload_io.read(chunk_size)
|
|
55
|
+
if chunk.nil? || chunk.empty?
|
|
56
|
+
buffer << encode(carry)
|
|
57
|
+
self.eof = true
|
|
58
|
+
else
|
|
59
|
+
bytes = carry + chunk.b
|
|
60
|
+
size = bytes.bytesize
|
|
61
|
+
aligned = size - (size % 3)
|
|
62
|
+
buffer << encode(bytes.byteslice(0, aligned))
|
|
63
|
+
self.carry = bytes.byteslice(aligned..)
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def encode(bytes) = Base64.urlsafe_encode64(bytes, padding: false)
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PQCrypto
|
|
4
|
+
module JWT
|
|
5
|
+
module JWA
|
|
6
|
+
class MLDSA
|
|
7
|
+
include ::JWT::JWA::SigningAlgorithm
|
|
8
|
+
include Streaming
|
|
9
|
+
|
|
10
|
+
attr_reader :alg, :pq_crypto_algorithm
|
|
11
|
+
|
|
12
|
+
def initialize(alg, pq_crypto_algorithm)
|
|
13
|
+
@alg = String(alg).dup.freeze
|
|
14
|
+
@pq_crypto_algorithm = pq_crypto_algorithm
|
|
15
|
+
freeze
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def valid_alg?(alg_to_validate) = alg_to_validate == alg
|
|
19
|
+
def key_kind = :signature
|
|
20
|
+
|
|
21
|
+
def sign(data:, signing_key:)
|
|
22
|
+
ensure_key!(signing_key, PQCrypto::Signature::SecretKey, "signing")
|
|
23
|
+
raise ArgumentError, "data must be a String" unless data.is_a?(String)
|
|
24
|
+
|
|
25
|
+
signing_key.sign(data.b, context: EMPTY_CONTEXT)
|
|
26
|
+
rescue KeyTypeError, ArgumentError
|
|
27
|
+
raise
|
|
28
|
+
rescue StandardError => error
|
|
29
|
+
raise ::JWT::EncodeError, error.message
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def verify(data:, signature:, verification_key:)
|
|
33
|
+
verification_key.is_a?(PQCrypto::Signature::PublicKey) &&
|
|
34
|
+
verification_key.algorithm == pq_crypto_algorithm &&
|
|
35
|
+
data.is_a?(String) && signature.is_a?(String) &&
|
|
36
|
+
signature_length_valid?(signature) &&
|
|
37
|
+
verification_key.verify(data.b, signature.b, context: EMPTY_CONTEXT)
|
|
38
|
+
rescue *VERIFY_ERRORS
|
|
39
|
+
false
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
private
|
|
43
|
+
|
|
44
|
+
def ensure_key!(key, klass, role)
|
|
45
|
+
raise KeyTypeError, "#{alg} #{role} requires #{klass}" unless key.is_a?(klass)
|
|
46
|
+
|
|
47
|
+
actual = key.algorithm
|
|
48
|
+
return if actual == pq_crypto_algorithm
|
|
49
|
+
|
|
50
|
+
raise KeyTypeError, "#{alg} #{role} requires #{pq_crypto_algorithm.inspect} key, got #{actual.inspect}"
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def signature_length_valid?(signature)
|
|
54
|
+
expected = PQCrypto::Signature.details(pq_crypto_algorithm)[:signature_bytes]
|
|
55
|
+
expected.nil? || signature.bytesize == expected
|
|
56
|
+
rescue *VERIFY_ERRORS
|
|
57
|
+
false
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
MLDSA44 = MLDSA.new("ML-DSA-44", :ml_dsa_44)
|
|
62
|
+
MLDSA65 = MLDSA.new("ML-DSA-65", :ml_dsa_65)
|
|
63
|
+
MLDSA87 = MLDSA.new("ML-DSA-87", :ml_dsa_87)
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
@@ -1,21 +1,3 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require_relative "
|
|
4
|
-
require_relative "ml_dsa_streaming"
|
|
5
|
-
|
|
6
|
-
module PQCrypto
|
|
7
|
-
module JWT
|
|
8
|
-
module JWA
|
|
9
|
-
module MLDSA44
|
|
10
|
-
extend ::JWT::JWA::SigningAlgorithm
|
|
11
|
-
extend PQCrypto::JWT::JWA::MLDSA
|
|
12
|
-
extend PQCrypto::JWT::JWA::MLDSAStreaming
|
|
13
|
-
|
|
14
|
-
ALG = "ML-DSA-44".freeze
|
|
15
|
-
PQ_CRYPTO_ALGORITHM = :ml_dsa_44
|
|
16
|
-
|
|
17
|
-
def self.alg = ALG
|
|
18
|
-
end
|
|
19
|
-
end
|
|
20
|
-
end
|
|
21
|
-
end
|
|
3
|
+
require_relative "../../jwt"
|
|
@@ -1,21 +1,3 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require_relative "
|
|
4
|
-
require_relative "ml_dsa_streaming"
|
|
5
|
-
|
|
6
|
-
module PQCrypto
|
|
7
|
-
module JWT
|
|
8
|
-
module JWA
|
|
9
|
-
module MLDSA65
|
|
10
|
-
extend ::JWT::JWA::SigningAlgorithm
|
|
11
|
-
extend PQCrypto::JWT::JWA::MLDSA
|
|
12
|
-
extend PQCrypto::JWT::JWA::MLDSAStreaming
|
|
13
|
-
|
|
14
|
-
ALG = "ML-DSA-65".freeze
|
|
15
|
-
PQ_CRYPTO_ALGORITHM = :ml_dsa_65
|
|
16
|
-
|
|
17
|
-
def self.alg = ALG
|
|
18
|
-
end
|
|
19
|
-
end
|
|
20
|
-
end
|
|
21
|
-
end
|
|
3
|
+
require_relative "../../jwt"
|
|
@@ -1,21 +1,3 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require_relative "
|
|
4
|
-
require_relative "ml_dsa_streaming"
|
|
5
|
-
|
|
6
|
-
module PQCrypto
|
|
7
|
-
module JWT
|
|
8
|
-
module JWA
|
|
9
|
-
module MLDSA87
|
|
10
|
-
extend ::JWT::JWA::SigningAlgorithm
|
|
11
|
-
extend PQCrypto::JWT::JWA::MLDSA
|
|
12
|
-
extend PQCrypto::JWT::JWA::MLDSAStreaming
|
|
13
|
-
|
|
14
|
-
ALG = "ML-DSA-87".freeze
|
|
15
|
-
PQ_CRYPTO_ALGORITHM = :ml_dsa_87
|
|
16
|
-
|
|
17
|
-
def self.alg = ALG
|
|
18
|
-
end
|
|
19
|
-
end
|
|
20
|
-
end
|
|
21
|
-
end
|
|
3
|
+
require_relative "../../jwt"
|
|
@@ -1,188 +1,3 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
require "json"
|
|
5
|
-
|
|
6
|
-
module PQCrypto
|
|
7
|
-
module JWT
|
|
8
|
-
module JWA
|
|
9
|
-
module MLDSAStreaming
|
|
10
|
-
DEFAULT_CHUNK_SIZE = 1 << 20
|
|
11
|
-
STREAMING_ERRORS = [JSON::ParserError, ArgumentError, PQCrypto::Error, PQCrypto::JWT::Error, EncodingError].freeze
|
|
12
|
-
|
|
13
|
-
def streaming_supported?
|
|
14
|
-
PQCrypto::Signature.supported.include?(pq_crypto_algorithm)
|
|
15
|
-
rescue PQCrypto::Error
|
|
16
|
-
false
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
def sign_io(signing_key:, payload_io: nil, io: nil, header_fields: {}, chunk_size: DEFAULT_CHUNK_SIZE)
|
|
20
|
-
ensure_streaming!
|
|
21
|
-
ensure_key!(signing_key, PQCrypto::Signature::SecretKey, "signing")
|
|
22
|
-
validate_chunk_size!(chunk_size)
|
|
23
|
-
source = payload_io || io
|
|
24
|
-
raise ArgumentError, "payload_io must respond to #read" unless source.respond_to?(:read)
|
|
25
|
-
|
|
26
|
-
header = normalize_signing_header!(header_fields).merge("alg" => alg)
|
|
27
|
-
encoded_header = base64url(JSON.generate(header))
|
|
28
|
-
input = DetachedSigningInputIO.new(encoded_header, source, chunk_size: chunk_size)
|
|
29
|
-
signature = signing_key.sign_io(input, chunk_size: chunk_size, context: EMPTY_CONTEXT)
|
|
30
|
-
"#{encoded_header}..#{base64url(signature)}"
|
|
31
|
-
rescue PQCrypto::JWT::Error, ArgumentError
|
|
32
|
-
raise
|
|
33
|
-
rescue StandardError => e
|
|
34
|
-
raise ::JWT::EncodeError, e.message
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
def verify_io(verification_key:, token:, payload_io:, chunk_size: DEFAULT_CHUNK_SIZE)
|
|
38
|
-
ensure_streaming!
|
|
39
|
-
ensure_key!(verification_key, PQCrypto::Signature::PublicKey, "verification")
|
|
40
|
-
validate_chunk_size!(chunk_size)
|
|
41
|
-
raise ArgumentError, "token must be a String" unless token.is_a?(String)
|
|
42
|
-
raise ArgumentError, "payload_io must respond to #read" unless payload_io.respond_to?(:read)
|
|
43
|
-
|
|
44
|
-
encoded_header, encoded_payload, encoded_signature, extra = token.split(".", -1)
|
|
45
|
-
return false unless extra.nil? && encoded_payload == "" && encoded_signature
|
|
46
|
-
|
|
47
|
-
header = JSON.parse(Base64.urlsafe_decode64(encoded_header))
|
|
48
|
-
return false unless supported_streaming_header?(header)
|
|
49
|
-
|
|
50
|
-
signature = Base64.urlsafe_decode64(encoded_signature)
|
|
51
|
-
return false unless signature_length_valid?(signature)
|
|
52
|
-
|
|
53
|
-
input = DetachedSigningInputIO.new(encoded_header, payload_io, chunk_size: chunk_size)
|
|
54
|
-
return false unless verification_key.verify_io(input, signature, chunk_size: chunk_size, context: EMPTY_CONTEXT)
|
|
55
|
-
|
|
56
|
-
[payload_io.respond_to?(:pos) ? payload_io.pos : nil, header]
|
|
57
|
-
rescue *STREAMING_ERRORS
|
|
58
|
-
false
|
|
59
|
-
end
|
|
60
|
-
|
|
61
|
-
def verify_io!(**kwargs)
|
|
62
|
-
verify_io(**kwargs) || raise(::JWT::VerificationError, "Streaming JWS verification failed")
|
|
63
|
-
true
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
private
|
|
67
|
-
|
|
68
|
-
def ensure_streaming!
|
|
69
|
-
return if streaming_supported?
|
|
70
|
-
|
|
71
|
-
raise PQCrypto::JWT::UnsupportedFeature, "#{alg} does not support streaming JWS"
|
|
72
|
-
end
|
|
73
|
-
|
|
74
|
-
def base64url(bytes)
|
|
75
|
-
Base64.urlsafe_encode64(String(bytes).b, padding: false)
|
|
76
|
-
end
|
|
77
|
-
|
|
78
|
-
def normalize_signing_header!(header_fields)
|
|
79
|
-
unless header_fields.respond_to?(:to_hash)
|
|
80
|
-
raise ArgumentError, "header_fields must be a Hash-like object"
|
|
81
|
-
end
|
|
82
|
-
|
|
83
|
-
header = header_fields.to_hash.each_with_object({}) { |(key, value), out| out[String(key)] = value }
|
|
84
|
-
unsupported = %w[b64 crit] & header.keys
|
|
85
|
-
unless unsupported.empty?
|
|
86
|
-
raise ArgumentError, "unsupported protected header#{unsupported.size == 1 ? '' : 's'}: #{unsupported.join(', ')}"
|
|
87
|
-
end
|
|
88
|
-
|
|
89
|
-
header
|
|
90
|
-
end
|
|
91
|
-
|
|
92
|
-
def supported_streaming_header?(header)
|
|
93
|
-
return false unless header.is_a?(Hash) && header["alg"] == alg
|
|
94
|
-
return false if header.key?("crit")
|
|
95
|
-
return false if header.key?("b64") && header["b64"] != true
|
|
96
|
-
|
|
97
|
-
true
|
|
98
|
-
end
|
|
99
|
-
|
|
100
|
-
def validate_chunk_size!(chunk_size)
|
|
101
|
-
return if chunk_size.is_a?(Integer) && chunk_size.positive?
|
|
102
|
-
|
|
103
|
-
raise ArgumentError, "chunk_size must be a positive Integer"
|
|
104
|
-
end
|
|
105
|
-
end
|
|
106
|
-
|
|
107
|
-
class DetachedSigningInputIO
|
|
108
|
-
def initialize(encoded_header, payload_io, chunk_size: MLDSAStreaming::DEFAULT_CHUNK_SIZE)
|
|
109
|
-
unless chunk_size.is_a?(Integer) && chunk_size.positive?
|
|
110
|
-
raise ArgumentError, "chunk_size must be a positive Integer"
|
|
111
|
-
end
|
|
112
|
-
|
|
113
|
-
@prefix = "#{encoded_header}.".b
|
|
114
|
-
@payload_io = payload_io
|
|
115
|
-
@chunk_size = chunk_size
|
|
116
|
-
@buffer = +""
|
|
117
|
-
@carry = +""
|
|
118
|
-
@prefix_done = false
|
|
119
|
-
@payload_done = false
|
|
120
|
-
end
|
|
121
|
-
|
|
122
|
-
def read(length = nil, outbuf = nil)
|
|
123
|
-
if length.nil?
|
|
124
|
-
drain_until_eof
|
|
125
|
-
return nil if @buffer.empty?
|
|
126
|
-
|
|
127
|
-
result = @buffer
|
|
128
|
-
@buffer = +""
|
|
129
|
-
else
|
|
130
|
-
raise ArgumentError, "negative length #{length} given" if length.negative?
|
|
131
|
-
return replace_outbuf(outbuf, +"") if length.zero?
|
|
132
|
-
|
|
133
|
-
fill(length)
|
|
134
|
-
return nil if @buffer.empty?
|
|
135
|
-
|
|
136
|
-
result = @buffer.byteslice(0, length)
|
|
137
|
-
@buffer = @buffer.byteslice(result.bytesize..-1) || +""
|
|
138
|
-
end
|
|
139
|
-
|
|
140
|
-
replace_outbuf(outbuf, result)
|
|
141
|
-
end
|
|
142
|
-
|
|
143
|
-
private
|
|
144
|
-
|
|
145
|
-
def replace_outbuf(outbuf, result)
|
|
146
|
-
outbuf&.replace(result)
|
|
147
|
-
outbuf || result
|
|
148
|
-
end
|
|
149
|
-
|
|
150
|
-
def drain_until_eof
|
|
151
|
-
consume_prefix
|
|
152
|
-
feed_one_chunk until @payload_done
|
|
153
|
-
end
|
|
154
|
-
|
|
155
|
-
def fill(length)
|
|
156
|
-
consume_prefix
|
|
157
|
-
feed_one_chunk while @buffer.bytesize < length && !@payload_done
|
|
158
|
-
end
|
|
159
|
-
|
|
160
|
-
def consume_prefix
|
|
161
|
-
return if @prefix_done
|
|
162
|
-
|
|
163
|
-
@buffer << @prefix
|
|
164
|
-
@prefix_done = true
|
|
165
|
-
end
|
|
166
|
-
|
|
167
|
-
def feed_one_chunk
|
|
168
|
-
chunk = @payload_io.read(@chunk_size)
|
|
169
|
-
if chunk.nil? || chunk.empty?
|
|
170
|
-
@buffer << Base64.urlsafe_encode64(@carry, padding: false) unless @carry.empty?
|
|
171
|
-
@carry = +""
|
|
172
|
-
@payload_done = true
|
|
173
|
-
return
|
|
174
|
-
end
|
|
175
|
-
|
|
176
|
-
bytes = @carry + chunk.b
|
|
177
|
-
aligned = bytes.bytesize - (bytes.bytesize % 3)
|
|
178
|
-
if aligned.positive?
|
|
179
|
-
@buffer << Base64.urlsafe_encode64(bytes.byteslice(0, aligned), padding: false)
|
|
180
|
-
@carry = bytes.byteslice(aligned..-1) || +""
|
|
181
|
-
else
|
|
182
|
-
@carry = bytes
|
|
183
|
-
end
|
|
184
|
-
end
|
|
185
|
-
end
|
|
186
|
-
end
|
|
187
|
-
end
|
|
188
|
-
end
|
|
3
|
+
require_relative "../../jwt"
|