Skip to content

Commit c073c98

Browse files
committed
Require RSA keys of minimum 2048 bits
1 parent 9617ef8 commit c073c98

File tree

5 files changed

+24
-1
lines changed

5 files changed

+24
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
- Remove deprecated claim verification methods [#654](https://github.com/jwt/ruby-jwt/pull/654) ([@anakinj](https://github.com/anakinj))
1111
- Remove dependency to rbnacl [#655](https://github.com/jwt/ruby-jwt/pull/655) ([@anakinj](https://github.com/anakinj))
1212
- Support only stricter base64 decoding (RFC 4648) [#658](https://github.com/jwt/ruby-jwt/pull/658) ([@anakinj](https://github.com/anakinj))
13-
- Custom algorithms are required to include `JWT::JWA::SigningAlgorithm` [#660](https://github.com/jwt/ruby-jwt/pull/560) ([@anakinj](https://github.com/anakinj))
13+
- Custom algorithms are required to include `JWT::JWA::SigningAlgorithm` [#660](https://github.com/jwt/ruby-jwt/pull/660) ([@anakinj](https://github.com/anakinj))
14+
- Require RSA keys to be at least 2048 bits [#661](https://github.com/jwt/ruby-jwt/pull/661) ([@anakinj](https://github.com/anakinj))
1415

1516
Take a look at the [upgrade guide](UPGRADING.md) for more details.
1617

lib/jwt/jwa/ps.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ def initialize(alg)
1313

1414
def sign(data:, signing_key:)
1515
raise_sign_error!("The given key is a #{signing_key.class}. It has to be an OpenSSL::PKey::RSA instance.") unless signing_key.is_a?(::OpenSSL::PKey::RSA)
16+
raise_sign_error!('The key length must be greater than or equal to 2048 bits') if signing_key.n.num_bits < 2048
1617

1718
signing_key.sign_pss(digest_algorithm, data, salt_length: :digest, mgf1_hash: digest_algorithm)
1819
end

lib/jwt/jwa/rsa.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ def initialize(alg)
1313

1414
def sign(data:, signing_key:)
1515
raise_sign_error!("The given key is a #{signing_key.class}. It has to be an OpenSSL::PKey::RSA instance") unless signing_key.is_a?(OpenSSL::PKey::RSA)
16+
raise_sign_error!('The key length must be greater than or equal to 2048 bits') if signing_key.n.num_bits < 2048
1617

1718
signing_key.sign(digest, data)
1819
end

spec/jwt/jwa/ps_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@
4242
end.to raise_error(JWT::EncodeError, /The given key is a String. It has to be an OpenSSL::PKey::RSA instance./)
4343
end
4444
end
45+
46+
context 'with a key length less than 2048 bits' do
47+
let(:rsa_key) { OpenSSL::PKey::RSA.generate(1024) }
48+
49+
it 'raises an error' do
50+
expect do
51+
ps256_instance.sign(data: data, signing_key: rsa_key)
52+
end.to raise_error(JWT::EncodeError, 'The key length must be greater than or equal to 2048 bits')
53+
end
54+
end
4555
end
4656

4757
describe '#verify' do

spec/jwt/jwa/rsa_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@
2020
end
2121
end
2222

23+
context 'with a key length less than 2048 bits' do
24+
let(:rsa_key) { OpenSSL::PKey::RSA.generate(1024) }
25+
26+
it 'raises an error' do
27+
expect do
28+
rsa_instance.sign(data: data, signing_key: rsa_key)
29+
end.to raise_error(JWT::EncodeError, 'The key length must be greater than or equal to 2048 bits')
30+
end
31+
end
32+
2333
context 'with an invalid key' do
2434
it 'raises an error' do
2535
expect do

0 commit comments

Comments
 (0)