Skip to content

Commit

Permalink
Require RSA keys of minimum 2048 bits
Browse files Browse the repository at this point in the history
  • Loading branch information
anakinj committed Dec 28, 2024
1 parent 9617ef8 commit c073c98
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
- Remove deprecated claim verification methods [#654](https://github.com/jwt/ruby-jwt/pull/654) ([@anakinj](https://github.com/anakinj))
- Remove dependency to rbnacl [#655](https://github.com/jwt/ruby-jwt/pull/655) ([@anakinj](https://github.com/anakinj))
- Support only stricter base64 decoding (RFC 4648) [#658](https://github.com/jwt/ruby-jwt/pull/658) ([@anakinj](https://github.com/anakinj))
- Custom algorithms are required to include `JWT::JWA::SigningAlgorithm` [#660](https://github.com/jwt/ruby-jwt/pull/560) ([@anakinj](https://github.com/anakinj))
- Custom algorithms are required to include `JWT::JWA::SigningAlgorithm` [#660](https://github.com/jwt/ruby-jwt/pull/660) ([@anakinj](https://github.com/anakinj))
- Require RSA keys to be at least 2048 bits [#661](https://github.com/jwt/ruby-jwt/pull/661) ([@anakinj](https://github.com/anakinj))

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

Expand Down
1 change: 1 addition & 0 deletions lib/jwt/jwa/ps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def initialize(alg)

def sign(data:, signing_key:)
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)
raise_sign_error!('The key length must be greater than or equal to 2048 bits') if signing_key.n.num_bits < 2048

signing_key.sign_pss(digest_algorithm, data, salt_length: :digest, mgf1_hash: digest_algorithm)
end
Expand Down
1 change: 1 addition & 0 deletions lib/jwt/jwa/rsa.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def initialize(alg)

def sign(data:, signing_key:)
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)
raise_sign_error!('The key length must be greater than or equal to 2048 bits') if signing_key.n.num_bits < 2048

signing_key.sign(digest, data)
end
Expand Down
10 changes: 10 additions & 0 deletions spec/jwt/jwa/ps_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@
end.to raise_error(JWT::EncodeError, /The given key is a String. It has to be an OpenSSL::PKey::RSA instance./)
end
end

context 'with a key length less than 2048 bits' do
let(:rsa_key) { OpenSSL::PKey::RSA.generate(1024) }

it 'raises an error' do
expect do
ps256_instance.sign(data: data, signing_key: rsa_key)
end.to raise_error(JWT::EncodeError, 'The key length must be greater than or equal to 2048 bits')
end
end
end

describe '#verify' do
Expand Down
10 changes: 10 additions & 0 deletions spec/jwt/jwa/rsa_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@
end
end

context 'with a key length less than 2048 bits' do
let(:rsa_key) { OpenSSL::PKey::RSA.generate(1024) }

it 'raises an error' do
expect do
rsa_instance.sign(data: data, signing_key: rsa_key)
end.to raise_error(JWT::EncodeError, 'The key length must be greater than or equal to 2048 bits')
end
end

context 'with an invalid key' do
it 'raises an error' do
expect do
Expand Down

0 comments on commit c073c98

Please sign in to comment.