pq_crypto-jwt 0.2.2 → 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 +29 -0
- data/README.md +9 -1
- 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 -214
- 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 +47 -42
- data/lib/pq_crypto/jwt/jwk.rb +100 -100
- 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 +52 -118
- 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
data/lib/pq_crypto/jwt/jwa.rb
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
require_relative "jwa/streaming"
|
|
4
|
+
require_relative "jwa/detached_signing_input_io"
|
|
5
|
+
require_relative "jwa/ml_dsa"
|
|
5
6
|
|
|
6
7
|
module PQCrypto
|
|
7
8
|
module JWT
|
|
@@ -12,53 +13,7 @@ module PQCrypto
|
|
|
12
13
|
::JWT::DecodeError, ::JWT::VerificationError, ::JWT::JWKError,
|
|
13
14
|
ArgumentError, TypeError, EncodingError
|
|
14
15
|
].freeze
|
|
15
|
-
|
|
16
|
-
module MLDSA
|
|
17
|
-
def valid_alg?(alg_to_validate) = alg_to_validate == alg
|
|
18
|
-
def pq_crypto_algorithm = self::PQ_CRYPTO_ALGORITHM
|
|
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 PQCrypto::JWT::KeyTypeError, ArgumentError
|
|
27
|
-
raise
|
|
28
|
-
rescue StandardError => e
|
|
29
|
-
raise ::JWT::EncodeError, e.message
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
def verify(data:, signature:, verification_key:)
|
|
33
|
-
return false unless verification_key.is_a?(PQCrypto::Signature::PublicKey)
|
|
34
|
-
return false unless verification_key.algorithm == pq_crypto_algorithm
|
|
35
|
-
return false unless data.is_a?(String) && signature.is_a?(String)
|
|
36
|
-
return false unless signature_length_valid?(signature)
|
|
37
|
-
|
|
38
|
-
verification_key.verify(data.b, signature.b, context: EMPTY_CONTEXT)
|
|
39
|
-
rescue *VERIFY_ERRORS
|
|
40
|
-
false
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
private
|
|
44
|
-
|
|
45
|
-
def ensure_key!(key, klass, role)
|
|
46
|
-
unless key.is_a?(klass)
|
|
47
|
-
raise PQCrypto::JWT::KeyTypeError, "#{alg} #{role} requires #{klass}"
|
|
48
|
-
end
|
|
49
|
-
return if key.algorithm == pq_crypto_algorithm
|
|
50
|
-
|
|
51
|
-
raise PQCrypto::JWT::KeyTypeError,
|
|
52
|
-
"#{alg} #{role} requires #{pq_crypto_algorithm.inspect} key, got #{key.algorithm.inspect}"
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
def signature_length_valid?(signature)
|
|
56
|
-
expected = PQCrypto::Signature.details(pq_crypto_algorithm)[:signature_bytes]
|
|
57
|
-
expected.nil? || signature.bytesize == expected
|
|
58
|
-
rescue *VERIFY_ERRORS
|
|
59
|
-
false
|
|
60
|
-
end
|
|
61
|
-
end
|
|
16
|
+
MLDSAStreaming = Streaming
|
|
62
17
|
end
|
|
63
18
|
end
|
|
64
19
|
end
|
|
@@ -5,9 +5,10 @@ require "jwt/jwk/key_base"
|
|
|
5
5
|
module JWT
|
|
6
6
|
module JWK
|
|
7
7
|
class AKP < KeyBase
|
|
8
|
-
KTY = "AKP"
|
|
8
|
+
KTY = "AKP"
|
|
9
9
|
KTYS = [KTY, PQCrypto::Signature::PublicKey, JWT::JWK::AKP].freeze
|
|
10
10
|
AKP_KEY_ELEMENTS = %i[kty alg pub priv].freeze
|
|
11
|
+
AKP_PUBLIC_KEY_ELEMENTS = %i[alg kty pub].freeze
|
|
11
12
|
PRIVATE_EXPORT_OPTIONS = %i[private include_private].freeze
|
|
12
13
|
|
|
13
14
|
class NullKidGenerator
|
|
@@ -15,38 +16,34 @@ module JWT
|
|
|
15
16
|
def generate = nil
|
|
16
17
|
end
|
|
17
18
|
|
|
19
|
+
attr_reader :public_key
|
|
20
|
+
|
|
18
21
|
def initialize(key, params = nil, options = {})
|
|
19
|
-
params =
|
|
20
|
-
params = params.transform_keys(&:to_sym)
|
|
21
|
-
key_params = extract_key_params(key)
|
|
22
|
-
@
|
|
23
|
-
super({ kid_generator: NullKidGenerator
|
|
22
|
+
params = { kid: params } if params.is_a?(String)
|
|
23
|
+
params = (params || {}).transform_keys(&:to_sym)
|
|
24
|
+
key_params = snapshot_key_params(extract_key_params(key))
|
|
25
|
+
@public_key = validate_key_params!(key_params, params)
|
|
26
|
+
super({ kid_generator: NullKidGenerator, **(options || {}) }, key_params.merge(params))
|
|
24
27
|
end
|
|
25
28
|
|
|
26
|
-
def private? =
|
|
29
|
+
def private? = !self[:priv].nil?
|
|
27
30
|
def verify_key = public_key
|
|
28
|
-
|
|
29
|
-
def public_key
|
|
30
|
-
@public_key ||= @checked_public_key || PQCrypto::JWT::JWK.public_key_from_jwk(string_export)
|
|
31
|
-
end
|
|
32
|
-
|
|
33
31
|
def signing_key = private? ? secret_key : public_key
|
|
34
32
|
|
|
35
33
|
def secret_key
|
|
36
34
|
raise JWT::JWKError, "AKP JWK does not contain private material" unless private?
|
|
37
35
|
|
|
38
|
-
|
|
36
|
+
self.cached_secret_key ||= PQCrypto::JWT::JWK.secret_key_from_jwk(string_export(include_private: true))
|
|
39
37
|
end
|
|
40
38
|
|
|
41
39
|
def export(options = {})
|
|
42
|
-
include_private =
|
|
43
|
-
parameters.
|
|
40
|
+
include_private = (options || {}).values_at(*PRIVATE_EXPORT_OPTIONS).any?
|
|
41
|
+
include_private ? parameters.dup : parameters.except(:priv)
|
|
44
42
|
end
|
|
45
43
|
|
|
46
|
-
def members
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
end
|
|
44
|
+
def members = parameters.slice(*AKP_PUBLIC_KEY_ELEMENTS)
|
|
45
|
+
|
|
46
|
+
def parameters = super.dup.freeze
|
|
50
47
|
|
|
51
48
|
def key_digest = PQCrypto::JWT::JWK.thumbprint(string_export)
|
|
52
49
|
def jwa = PQCrypto::JWT.algorithm_for(self[:alg]) || super
|
|
@@ -59,16 +56,22 @@ module JWT
|
|
|
59
56
|
|
|
60
57
|
private
|
|
61
58
|
|
|
62
|
-
|
|
63
|
-
|
|
59
|
+
attr_accessor :cached_secret_key
|
|
60
|
+
|
|
61
|
+
def snapshot_key_params(key_params)
|
|
62
|
+
key_params.to_h do |name, value|
|
|
63
|
+
copied = AKP_KEY_ELEMENTS.include?(name) && value.is_a?(String) ? value.dup.freeze : value
|
|
64
|
+
[name, copied]
|
|
65
|
+
end
|
|
64
66
|
end
|
|
65
67
|
|
|
68
|
+
def string_export(include_private: false) = export(include_private:).transform_keys(&:to_s)
|
|
69
|
+
|
|
66
70
|
def extract_key_params(key)
|
|
67
71
|
case key
|
|
68
72
|
when JWT::JWK::AKP then key.export(include_private: key.private?)
|
|
69
73
|
when Hash then key.transform_keys(&:to_sym)
|
|
70
|
-
when PQCrypto::Signature::PublicKey
|
|
71
|
-
PQCrypto::JWT::JWK.from_public_key(key).transform_keys(&:to_sym)
|
|
74
|
+
when PQCrypto::Signature::PublicKey then PQCrypto::JWT::JWK.from_public_key(key).transform_keys(&:to_sym)
|
|
72
75
|
when PQCrypto::Signature::Keypair, PQCrypto::Signature::SecretKey
|
|
73
76
|
raise JWT::JWKError,
|
|
74
77
|
"AKP private JWK export from SecretKey requires public seed-export APIs in pq_crypto; " \
|
|
@@ -76,30 +79,32 @@ module JWT
|
|
|
76
79
|
else
|
|
77
80
|
raise ArgumentError, "key must be an AKP JWK Hash or PQCrypto::Signature::PublicKey"
|
|
78
81
|
end
|
|
79
|
-
rescue PQCrypto::JWT::Error =>
|
|
80
|
-
raise JWT::JWKError,
|
|
82
|
+
rescue PQCrypto::JWT::Error => error
|
|
83
|
+
raise JWT::JWKError, error.message
|
|
81
84
|
end
|
|
82
85
|
|
|
83
|
-
def
|
|
84
|
-
|
|
85
|
-
return if seed.bytesize == PQCrypto::JWT::JWK::SEED_BYTES
|
|
86
|
+
def validate_key_params!(key_params, params)
|
|
87
|
+
raise ArgumentError, "cannot overwrite cryptographic key attributes" if AKP_KEY_ELEMENTS.intersect?(params.keys)
|
|
86
88
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
+
kty, alg, pub = key_params.values_at(:kty, :alg, :pub)
|
|
90
|
+
raise JWT::JWKError, "Incorrect 'kty' value: #{kty}, expected #{KTY}" unless kty == KTY
|
|
91
|
+
raise JWT::JWKError, "AKP JWK alg is required" unless alg
|
|
92
|
+
raise JWT::JWKError, "AKP JWK pub is required" unless pub
|
|
93
|
+
raise JWT::JWKError, "Unsupported AKP JWK alg: #{alg.inspect}" unless PQCrypto::JWT.algorithm_for(alg)
|
|
94
|
+
|
|
95
|
+
validate_seed_length!(key_params) if key_params.key?(:priv)
|
|
96
|
+
PQCrypto::JWT::JWK.public_key_from_jwk(key_params)
|
|
97
|
+
rescue PQCrypto::JWT::Error => error
|
|
98
|
+
raise JWT::JWKError, error.message
|
|
89
99
|
end
|
|
90
100
|
|
|
91
|
-
def
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
raise JWT::JWKError,
|
|
97
|
-
|
|
98
|
-
parsed = key_params.transform_keys(&:to_s)
|
|
99
|
-
validate_private_seed_metadata!(parsed) if key_params.key?(:priv)
|
|
100
|
-
PQCrypto::JWT::JWK.public_key_from_jwk(parsed)
|
|
101
|
-
rescue PQCrypto::JWT::Error => e
|
|
102
|
-
raise JWT::JWKError, e.message
|
|
101
|
+
def validate_seed_length!(key_params)
|
|
102
|
+
actual = PQCrypto::JWT::JWK.base64url_decode(key_params[:priv]).bytesize
|
|
103
|
+
expected = PQCrypto::JWT::JWK::SEED_BYTES
|
|
104
|
+
return if actual == expected
|
|
105
|
+
|
|
106
|
+
raise JWT::JWKError,
|
|
107
|
+
"Invalid priv seed length for #{key_params[:alg].inspect}: expected #{expected}, got #{actual}"
|
|
103
108
|
end
|
|
104
109
|
end
|
|
105
110
|
end
|
data/lib/pq_crypto/jwt/jwk.rb
CHANGED
|
@@ -7,36 +7,36 @@ require "json"
|
|
|
7
7
|
module PQCrypto
|
|
8
8
|
module JWT
|
|
9
9
|
module JWK
|
|
10
|
-
|
|
10
|
+
extend self
|
|
11
11
|
|
|
12
|
-
KTY = "AKP"
|
|
12
|
+
KTY = "AKP"
|
|
13
13
|
SEED_BYTES = defined?(PQCrypto::PKCS8::ML_DSA_SEED_BYTES) ? PQCrypto::PKCS8::ML_DSA_SEED_BYTES : 32
|
|
14
|
-
THUMBPRINT_URI_PREFIX = "urn:ietf:params:oauth:jwk-thumbprint:sha-256:"
|
|
14
|
+
THUMBPRINT_URI_PREFIX = "urn:ietf:params:oauth:jwk-thumbprint:sha-256:"
|
|
15
15
|
|
|
16
16
|
def from_public_key(public_key, kid: nil, use: nil, key_ops: nil)
|
|
17
17
|
validate_key!(public_key, PQCrypto::Signature::PublicKey)
|
|
18
|
-
public_jwk(public_key
|
|
18
|
+
public_jwk(public_key, kid:, use:, key_ops:).freeze
|
|
19
19
|
end
|
|
20
20
|
|
|
21
21
|
def from_seed(seed, alg:, kid: nil, public_key: nil, use: nil, key_ops: nil, verify_public: false)
|
|
22
22
|
algorithm = pq_algorithm_from_jose!(alg)
|
|
23
|
-
|
|
24
|
-
derived = derive_public_key(algorithm,
|
|
23
|
+
seed = validate_seed!(seed, algorithm)
|
|
24
|
+
derived = derive_public_key(algorithm, seed) if public_key.nil? || verify_public
|
|
25
25
|
public_key ||= derived
|
|
26
26
|
unless public_key
|
|
27
|
-
raise
|
|
27
|
+
raise UnsupportedFeature,
|
|
28
28
|
"AKP JWK export from seed requires pq_crypto public_key_from_seed/keypair_from_seed or public_key:"
|
|
29
29
|
end
|
|
30
30
|
|
|
31
31
|
validate_key!(public_key, PQCrypto::Signature::PublicKey)
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
actual = public_key.algorithm
|
|
33
|
+
unless actual == algorithm
|
|
34
|
+
raise KeyTypeError,
|
|
35
|
+
"public_key algorithm mismatch: expected #{algorithm.inspect}, got #{actual.inspect}"
|
|
34
36
|
end
|
|
35
|
-
check_seed_matches_public!(public_key, derived) if verify_public
|
|
37
|
+
check_seed_matches_public!(public_key.to_bytes, derived) if verify_public
|
|
36
38
|
|
|
37
|
-
public_jwk(
|
|
38
|
-
.merge!("priv" => base64url(seed_bytes))
|
|
39
|
-
.freeze
|
|
39
|
+
public_jwk(public_key, kid:, use:, key_ops:).merge("priv" => base64url(seed).freeze).freeze
|
|
40
40
|
end
|
|
41
41
|
|
|
42
42
|
def from_secret_key(secret_key, kid: nil, public_key: nil, use: nil, key_ops: nil)
|
|
@@ -46,156 +46,156 @@ module PQCrypto
|
|
|
46
46
|
end
|
|
47
47
|
validate_key!(secret_key, PQCrypto::Signature::SecretKey)
|
|
48
48
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
kid: kid, public_key: public_key, use: use, key_ops: key_ops)
|
|
49
|
+
seed = seed_from_secret_key!(secret_key)
|
|
50
|
+
from_seed(seed, alg: jose_alg_for!(secret_key.algorithm), kid:, public_key:, use:, key_ops:)
|
|
52
51
|
end
|
|
53
52
|
|
|
54
53
|
def public_key_from_jwk(hash)
|
|
55
|
-
|
|
56
|
-
algorithm
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
raise PQCrypto::JWT::Error, e.message
|
|
54
|
+
_jwk, algorithm, public_bytes = parse_public!(hash)
|
|
55
|
+
PQCrypto::Signature.public_key_from_bytes(algorithm, public_bytes)
|
|
56
|
+
rescue ArgumentError, PQCrypto::Error => error
|
|
57
|
+
raise Error, error.message
|
|
60
58
|
end
|
|
61
59
|
|
|
62
60
|
def secret_key_from_jwk(hash, verify_public: false)
|
|
63
|
-
jwk =
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
seed = validate_seed!(base64url_decode(jwk.fetch("priv") { raise PQCrypto::JWT::Error, "JWK priv is required" }), algorithm)
|
|
67
|
-
check_seed_matches_public!(public_key_from_jwk(jwk), derive_public_key(algorithm, seed)) if verify_public
|
|
61
|
+
jwk, algorithm, public_bytes = parse_public!(hash)
|
|
62
|
+
seed = validate_seed!(base64url_decode(jwk.fetch("priv") { raise Error, "JWK priv is required" }), algorithm)
|
|
63
|
+
check_seed_matches_public!(public_bytes, derive_public_key(algorithm, seed)) if verify_public
|
|
68
64
|
|
|
69
65
|
PQCrypto::Signature.secret_key_from_seed(algorithm, seed)
|
|
70
|
-
rescue ArgumentError, PQCrypto::Error =>
|
|
71
|
-
raise
|
|
66
|
+
rescue ArgumentError, PQCrypto::Error => error
|
|
67
|
+
raise Error, error.message
|
|
72
68
|
end
|
|
73
69
|
|
|
74
70
|
def thumbprint(jwk_hash)
|
|
75
|
-
jwk =
|
|
76
|
-
|
|
77
|
-
decode_field!(jwk, "pub", algorithm)
|
|
78
|
-
|
|
79
|
-
canonical = JSON.generate("alg" => jwk.fetch("alg"), "kty" => KTY, "pub" => jwk.fetch("pub"))
|
|
80
|
-
base64url(Digest::SHA256.digest(canonical.b))
|
|
71
|
+
jwk = parse_public!(jwk_hash).first
|
|
72
|
+
base64url(Digest::SHA256.digest(JSON.generate("alg" => jwk["alg"], "kty" => KTY, "pub" => jwk["pub"])))
|
|
81
73
|
end
|
|
82
74
|
|
|
83
|
-
def thumbprint_uri(jwk_hash)
|
|
84
|
-
"#{THUMBPRINT_URI_PREFIX}#{thumbprint(jwk_hash)}"
|
|
85
|
-
end
|
|
75
|
+
def thumbprint_uri(jwk_hash) = "#{THUMBPRINT_URI_PREFIX}#{thumbprint(jwk_hash)}"
|
|
86
76
|
|
|
87
|
-
def base64url(bytes)
|
|
88
|
-
Base64.urlsafe_encode64(String(bytes).b, padding: false)
|
|
89
|
-
end
|
|
77
|
+
def base64url(bytes) = Base64URL.encode(bytes)
|
|
90
78
|
|
|
91
79
|
def base64url_decode(value)
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
raise PQCrypto::JWT::Error, "Invalid base64url value: #{e.message}"
|
|
80
|
+
Base64URL.decode(value)
|
|
81
|
+
rescue ArgumentError => error
|
|
82
|
+
raise Error, "Invalid base64url value: #{error.message}"
|
|
96
83
|
end
|
|
97
84
|
|
|
98
85
|
def normalize_hash!(hash)
|
|
99
|
-
raise
|
|
86
|
+
raise Error, "JWK must be a Hash-like object" unless hash.respond_to?(:to_hash)
|
|
87
|
+
|
|
88
|
+
converted = hash.to_hash
|
|
89
|
+
raise Error, "JWK #to_hash must return a Hash" unless converted.is_a?(Hash)
|
|
100
90
|
|
|
101
|
-
|
|
91
|
+
converted.transform_keys { |key| String(key) }
|
|
102
92
|
end
|
|
103
93
|
|
|
104
94
|
def algorithm_from_jwk!(jwk)
|
|
105
|
-
|
|
95
|
+
kty = jwk["kty"]
|
|
96
|
+
raise Error, "Unsupported JWK kty: #{kty.inspect}" unless kty == KTY
|
|
106
97
|
|
|
107
98
|
pq_algorithm_from_jose!(jwk["alg"])
|
|
108
99
|
end
|
|
109
100
|
|
|
110
|
-
|
|
111
|
-
algorithm = PQCrypto::JWT.algorithm_for(alg)
|
|
112
|
-
raise PQCrypto::JWT::UnsupportedAlgorithm, "Unsupported JWK alg: #{alg.inspect}" unless algorithm&.key_kind == :signature
|
|
101
|
+
private
|
|
113
102
|
|
|
114
|
-
|
|
103
|
+
def parse_public!(hash)
|
|
104
|
+
jwk = normalize_hash!(hash)
|
|
105
|
+
algorithm = algorithm_from_jwk!(jwk)
|
|
106
|
+
[jwk, algorithm, decode_public_bytes!(jwk, algorithm)]
|
|
115
107
|
end
|
|
116
|
-
private_class_method :pq_algorithm_from_jose!
|
|
117
108
|
|
|
118
|
-
def
|
|
119
|
-
|
|
120
|
-
raise PQCrypto::JWT::UnsupportedAlgorithm, "Unsupported pq_crypto signature algorithm: #{pq_algorithm.inspect}" unless match
|
|
109
|
+
def decode_public_bytes!(jwk, algorithm)
|
|
110
|
+
raise Error, "JWK pub is required" unless jwk.key?("pub")
|
|
121
111
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
112
|
+
bytes = base64url_decode(jwk["pub"])
|
|
113
|
+
expected = PQCrypto::Signature.details(algorithm).fetch(:public_key_bytes)
|
|
114
|
+
actual = bytes.bytesize
|
|
115
|
+
return bytes if actual == expected
|
|
125
116
|
|
|
126
|
-
|
|
127
|
-
jwk = { "kty" => KTY, "alg" => jose_alg_for!(pq_algorithm), "pub" => base64url(public_bytes) }
|
|
128
|
-
jwk["kid"] = String(kid) unless kid.nil?
|
|
129
|
-
jwk["use"] = String(use) unless use.nil?
|
|
130
|
-
jwk["key_ops"] = Array(key_ops).map(&:to_s) unless key_ops.nil?
|
|
131
|
-
jwk
|
|
117
|
+
raise Error, "Invalid pub length for #{algorithm.inspect}: expected #{expected}, got #{actual}"
|
|
132
118
|
end
|
|
133
|
-
private_class_method :public_jwk
|
|
134
119
|
|
|
135
|
-
def
|
|
136
|
-
|
|
120
|
+
def public_jwk(public_key, kid:, use:, key_ops:)
|
|
121
|
+
{
|
|
122
|
+
"kty" => KTY,
|
|
123
|
+
"alg" => jose_alg_for!(public_key.algorithm),
|
|
124
|
+
"pub" => base64url(public_key.to_bytes).freeze,
|
|
125
|
+
"kid" => metadata_string(kid),
|
|
126
|
+
"use" => metadata_string(use),
|
|
127
|
+
"key_ops" => (Array(key_ops).map { |operation| operation.to_s.dup.freeze }.freeze unless key_ops.nil?)
|
|
128
|
+
}.compact
|
|
129
|
+
end
|
|
137
130
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
131
|
+
def metadata_string(value)
|
|
132
|
+
String(value).dup.freeze unless value.nil?
|
|
133
|
+
end
|
|
141
134
|
|
|
142
|
-
|
|
135
|
+
def pq_algorithm_from_jose!(alg)
|
|
136
|
+
PQCrypto::JWT.algorithm_for(alg)&.pq_crypto_algorithm ||
|
|
137
|
+
raise(UnsupportedAlgorithm, "Unsupported JWK alg: #{alg.inspect}")
|
|
143
138
|
end
|
|
144
|
-
private_class_method :decode_field!
|
|
145
139
|
|
|
146
|
-
def
|
|
147
|
-
|
|
148
|
-
|
|
140
|
+
def jose_alg_for!(pq_algorithm)
|
|
141
|
+
signing_algorithm_for(pq_algorithm)&.alg ||
|
|
142
|
+
raise(UnsupportedAlgorithm, "Unsupported pq_crypto signature algorithm: #{pq_algorithm.inspect}")
|
|
143
|
+
end
|
|
149
144
|
|
|
150
|
-
|
|
145
|
+
def signing_algorithm_for(pq_algorithm)
|
|
146
|
+
PQCrypto::JWT.signing_algorithms.find { |algorithm| algorithm.pq_crypto_algorithm == pq_algorithm }
|
|
151
147
|
end
|
|
152
|
-
private_class_method :validate_seed!
|
|
153
148
|
|
|
154
149
|
def validate_key!(key, klass)
|
|
155
|
-
raise
|
|
156
|
-
return if PQCrypto::JWT.signing_algorithms.any? { |c| c.pq_crypto_algorithm == key.algorithm }
|
|
150
|
+
raise KeyTypeError, "Expected #{klass}" unless key.is_a?(klass)
|
|
157
151
|
|
|
158
|
-
|
|
152
|
+
actual = key.algorithm
|
|
153
|
+
return if signing_algorithm_for(actual)
|
|
154
|
+
|
|
155
|
+
raise KeyTypeError, "Unsupported signature algorithm: #{actual.inspect}"
|
|
159
156
|
end
|
|
160
|
-
private_class_method :validate_key!
|
|
161
157
|
|
|
162
|
-
def
|
|
163
|
-
|
|
164
|
-
|
|
158
|
+
def validate_seed!(seed, algorithm)
|
|
159
|
+
bytes = String(seed).b
|
|
160
|
+
actual = bytes.bytesize
|
|
161
|
+
return bytes if actual == SEED_BYTES
|
|
162
|
+
|
|
163
|
+
raise Error, "Invalid priv seed length for #{algorithm.inspect}: expected #{SEED_BYTES}, got #{actual}"
|
|
164
|
+
end
|
|
165
165
|
|
|
166
|
-
|
|
166
|
+
def derive_public_key(algorithm, seed)
|
|
167
|
+
if PQCrypto::Signature.respond_to?(:public_key_from_seed)
|
|
168
|
+
PQCrypto::Signature.public_key_from_seed(algorithm, seed)
|
|
169
|
+
elsif PQCrypto::Signature.respond_to?(:keypair_from_seed)
|
|
170
|
+
PQCrypto::Signature.keypair_from_seed(algorithm, seed).public_key
|
|
171
|
+
end
|
|
167
172
|
end
|
|
168
|
-
private_class_method :derive_public_key
|
|
169
173
|
|
|
170
|
-
def check_seed_matches_public!(
|
|
174
|
+
def check_seed_matches_public!(public_bytes, derived)
|
|
171
175
|
unless derived
|
|
172
|
-
raise
|
|
176
|
+
raise UnsupportedFeature,
|
|
173
177
|
"AKP JWK pub/priv consistency verification requires pq_crypto public_key_from_seed/keypair_from_seed"
|
|
174
178
|
end
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
raise PQCrypto::JWT::Error, "AKP JWK public_key does not match priv seed"
|
|
179
|
+
raise Error, "AKP JWK public_key does not match priv seed" unless public_bytes == derived.to_bytes
|
|
178
180
|
end
|
|
179
|
-
private_class_method :check_seed_matches_public!
|
|
180
181
|
|
|
181
182
|
def seed_from_secret_key!(secret_key)
|
|
182
183
|
if secret_key.respond_to?(:seed?) && !secret_key.seed?
|
|
183
|
-
raise
|
|
184
|
-
"SecretKey was not created from a seed; cannot export RFC 9964 AKP JWK"
|
|
184
|
+
raise UnsupportedFeature, "SecretKey was not created from a seed; cannot export RFC 9964 AKP JWK"
|
|
185
185
|
end
|
|
186
|
+
|
|
186
187
|
unless secret_key.respond_to?(:to_seed)
|
|
187
|
-
raise
|
|
188
|
-
|
|
188
|
+
raise UnsupportedFeature, "SecretKey seed export requires a public pq_crypto #to_seed API; " \
|
|
189
|
+
"use from_seed(..., public_key:) instead"
|
|
189
190
|
end
|
|
190
191
|
|
|
191
192
|
seed = secret_key.to_seed
|
|
192
|
-
raise
|
|
193
|
+
raise UnsupportedFeature, "SecretKey #to_seed returned nil" if seed.nil?
|
|
193
194
|
|
|
194
195
|
validate_seed!(seed, secret_key.algorithm)
|
|
195
|
-
rescue PQCrypto::Error =>
|
|
196
|
-
raise
|
|
196
|
+
rescue PQCrypto::Error => error
|
|
197
|
+
raise UnsupportedFeature, "SecretKey seed export failed: #{error.message}"
|
|
197
198
|
end
|
|
198
|
-
private_class_method :seed_from_secret_key!
|
|
199
199
|
end
|
|
200
200
|
end
|
|
201
201
|
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PQCrypto
|
|
4
|
+
module JWT
|
|
5
|
+
module JWKS
|
|
6
|
+
class Loader
|
|
7
|
+
def initialize(source)
|
|
8
|
+
@fetch = source.respond_to?(:call) ? source : proc { source }
|
|
9
|
+
@cached = CACHE_EMPTY
|
|
10
|
+
@mutex = Mutex.new
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def call(options = {})
|
|
14
|
+
options ||= {}
|
|
15
|
+
invalidate = options[:invalidate]
|
|
16
|
+
current = cached
|
|
17
|
+
return current unless invalidate || current.equal?(CACHE_EMPTY)
|
|
18
|
+
|
|
19
|
+
mutex.synchronize { refresh(options, invalidate) }
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
private
|
|
23
|
+
|
|
24
|
+
attr_reader :fetch, :mutex
|
|
25
|
+
attr_accessor :cached
|
|
26
|
+
|
|
27
|
+
def refresh(options, invalidate)
|
|
28
|
+
self.cached = CACHE_EMPTY if invalidate
|
|
29
|
+
self.cached = fetch.call(options) if cached.equal?(CACHE_EMPTY)
|
|
30
|
+
cached
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PQCrypto
|
|
4
|
+
module JWT
|
|
5
|
+
module JWKS
|
|
6
|
+
class ValidationCache
|
|
7
|
+
MISSING = Object.new.freeze
|
|
8
|
+
|
|
9
|
+
def initialize(limit)
|
|
10
|
+
@limit = limit
|
|
11
|
+
@entries = {}
|
|
12
|
+
@mutex = Mutex.new
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def fetch(key)
|
|
16
|
+
cached = mutex.synchronize { entries.fetch(key, MISSING) }
|
|
17
|
+
return cached unless cached.equal?(MISSING)
|
|
18
|
+
|
|
19
|
+
result = yield
|
|
20
|
+
mutex.synchronize { store(key, result) }
|
|
21
|
+
result
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def clear
|
|
25
|
+
mutex.synchronize { entries.clear }
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
private
|
|
29
|
+
|
|
30
|
+
attr_reader :limit, :entries, :mutex
|
|
31
|
+
|
|
32
|
+
def store(key, result)
|
|
33
|
+
entries.shift if entries.size >= limit && !entries.key?(key)
|
|
34
|
+
entries[key] = result
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|