Skip to content

Commit

Permalink
Move rescueing OpenSSL::PKey::PKeyError closer to the verification ha…
Browse files Browse the repository at this point in the history
…ppens using a pkey
  • Loading branch information
anakinj committed Feb 3, 2023
1 parent d0978c1 commit e5f5711
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 4 deletions.
4 changes: 0 additions & 4 deletions lib/jwt/algos/algo_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ def sign(data:, signing_key:)

def verify(data:, signature:, verification_key:)
cls.verify(alg, verification_key, data, signature)
rescue OpenSSL::PKey::PKeyError # These should be moved to the algorithms that actually need this, but left here to ensure nothing will break.
raise JWT::VerificationError, 'Signature verification raised'
ensure
OpenSSL.errors.clear
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/jwt/algos/ecdsa.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ def verify(algorithm, public_key, signing_input, signature)

digest = OpenSSL::Digest.new(curve_definition[:digest])
public_key.dsa_verify_asn1(digest.digest(signing_input), raw_to_asn1(signature, public_key))
rescue OpenSSL::PKey::PKeyError
raise JWT::VerificationError, 'Signature verification raised'
end

def curve_by_name(name)
Expand Down
2 changes: 2 additions & 0 deletions lib/jwt/algos/ps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ def verify(algorithm, public_key, signing_input, signature)
require_openssl!
translated_algorithm = algorithm.sub('PS', 'sha')
public_key.verify_pss(translated_algorithm, signature, signing_input, salt_length: :auto, mgf1_hash: translated_algorithm)
rescue OpenSSL::PKey::PKeyError
raise JWT::VerificationError, 'Signature verification raised'
end

def require_openssl!
Expand Down
2 changes: 2 additions & 0 deletions lib/jwt/algos/rsa.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ def sign(algorithm, msg, key)

def verify(algorithm, public_key, signing_input, signature)
public_key.verify(OpenSSL::Digest.new(algorithm.sub('RS', 'sha')), signature, signing_input)
rescue OpenSSL::PKey::PKeyError
raise JWT::VerificationError, 'Signature verification raised'
end
end
end
Expand Down

0 comments on commit e5f5711

Please sign in to comment.