Skip to content

Commit

Permalink
Move SecurityUtils methods closer to the usage
Browse files Browse the repository at this point in the history
  • Loading branch information
anakinj committed Feb 3, 2023
1 parent 324941b commit d0978c1
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 41 deletions.
1 change: 0 additions & 1 deletion lib/jwt/algos.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
end
require 'openssl'

require 'jwt/security_utils'
require 'jwt/algos/hmac'
require 'jwt/algos/eddsa'
require 'jwt/algos/ecdsa'
Expand Down
16 changes: 14 additions & 2 deletions lib/jwt/algos/ecdsa.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def sign(algorithm, msg, key)
end

digest = OpenSSL::Digest.new(curve_definition[:digest])
SecurityUtils.asn1_to_raw(key.dsa_sign_asn1(digest.digest(msg)), key)
asn1_to_raw(key.dsa_sign_asn1(digest.digest(msg)), key)
end

def verify(algorithm, public_key, signing_input, signature)
Expand All @@ -49,14 +49,26 @@ def verify(algorithm, public_key, signing_input, signature)
end

digest = OpenSSL::Digest.new(curve_definition[:digest])
public_key.dsa_verify_asn1(digest.digest(signing_input), SecurityUtils.raw_to_asn1(signature, public_key))
public_key.dsa_verify_asn1(digest.digest(signing_input), raw_to_asn1(signature, public_key))
end

def curve_by_name(name)
NAMED_CURVES.fetch(name) do
raise UnsupportedEcdsaCurve, "The ECDSA curve '#{name}' is not supported"
end
end

def raw_to_asn1(signature, private_key)
byte_size = (private_key.group.degree + 7) / 8
sig_bytes = signature[0..(byte_size - 1)]
sig_char = signature[byte_size..-1] || ''
OpenSSL::ASN1::Sequence.new([sig_bytes, sig_char].map { |int| OpenSSL::ASN1::Integer.new(OpenSSL::BN.new(int, 2)) }).to_der
end

def asn1_to_raw(signature, public_key)
byte_size = (public_key.group.degree + 7) / 8
OpenSSL::ASN1.decode(signature).value.map { |value| value.value.to_s(2).rjust(byte_size, "\x00") }.join
end
end
end
end
8 changes: 3 additions & 5 deletions lib/jwt/algos/ps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ module Ps
def sign(algorithm, msg, key)
require_openssl!

key_class = key.class

raise EncodeError, "The given key is a #{key_class}. It has to be an OpenSSL::PKey::RSA instance." if key_class == String
raise EncodeError, "The given key is a #{key_class}. It has to be an OpenSSL::PKey::RSA instance." if key.is_a?(String)

translated_algorithm = algorithm.sub('PS', 'sha')

Expand All @@ -23,8 +21,8 @@ def sign(algorithm, msg, key)

def verify(algorithm, public_key, signing_input, signature)
require_openssl!

SecurityUtils.verify_ps(algorithm, public_key, signing_input, signature)
translated_algorithm = algorithm.sub('PS', 'sha')
public_key.verify_pss(translated_algorithm, signature, signing_input, salt_length: :auto, mgf1_hash: translated_algorithm)
end

def require_openssl!
Expand Down
2 changes: 1 addition & 1 deletion lib/jwt/algos/rsa.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def sign(algorithm, msg, key)
end

def verify(algorithm, public_key, signing_input, signature)
SecurityUtils.verify_rsa(algorithm, public_key, signing_input, signature)
public_key.verify(OpenSSL::Digest.new(algorithm.sub('RS', 'sha')), signature, signing_input)
end
end
end
Expand Down
32 changes: 0 additions & 32 deletions lib/jwt/security_utils.rb

This file was deleted.

0 comments on commit d0978c1

Please sign in to comment.