-
Notifications
You must be signed in to change notification settings - Fork 376
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move signature logic to its own module #195
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
eab02ca
Move signing logic to its own module
EmilioCristalli 97bdf8b
Move signature verification logic to Sginature module
EmilioCristalli 8fc8592
Cleanup Signature module
EmilioCristalli 8ed32d7
Remove NAMED_CURVES from DefaultOptions
EmilioCristalli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
# frozen_string_literal: true | ||
require 'openssl' | ||
|
||
module JWT | ||
module Signature | ||
extend self | ||
|
||
HMAC_ALGORITHMS = %w(HS256 HS384 HS512).freeze | ||
RSA_ALGORITHMS = %w(RS256 RS384 RS512).freeze | ||
ECDSA_ALGORITHMS = %w(ES256 ES384 ES512).freeze | ||
|
||
NAMED_CURVES = { | ||
'prime256v1' => 'ES256', | ||
'secp384r1' => 'ES384', | ||
'secp521r1' => 'ES512' | ||
}.freeze | ||
|
||
def sign(algorithm, msg, key) | ||
if HMAC_ALGORITHMS.include?(algorithm) | ||
sign_hmac(algorithm, msg, key) | ||
elsif RSA_ALGORITHMS.include?(algorithm) | ||
sign_rsa(algorithm, msg, key) | ||
elsif ECDSA_ALGORITHMS.include?(algorithm) | ||
sign_ecdsa(algorithm, msg, key) | ||
else | ||
raise NotImplementedError, 'Unsupported signing method' | ||
end | ||
end | ||
|
||
def verify(algo, key, signing_input, signature) | ||
verified = if HMAC_ALGORITHMS.include?(algo) | ||
secure_compare(signature, sign_hmac(algo, signing_input, key)) | ||
elsif RSA_ALGORITHMS.include?(algo) | ||
verify_rsa(algo, key, signing_input, signature) | ||
elsif ECDSA_ALGORITHMS.include?(algo) | ||
verify_ecdsa(algo, key, signing_input, signature) | ||
else | ||
raise JWT::VerificationError, 'Algorithm not supported' | ||
end | ||
|
||
raise(JWT::VerificationError, 'Signature verification raised') unless verified | ||
rescue OpenSSL::PKey::PKeyError | ||
raise JWT::VerificationError, 'Signature verification raised' | ||
ensure | ||
OpenSSL.errors.clear | ||
end | ||
|
||
private | ||
|
||
def sign_rsa(algorithm, msg, private_key) | ||
raise EncodeError, "The given key is a #{private_key.class}. It has to be an OpenSSL::PKey::RSA instance." if private_key.class == String | ||
private_key.sign(OpenSSL::Digest.new(algorithm.sub('RS', 'sha')), msg) | ||
end | ||
|
||
def sign_ecdsa(algorithm, msg, private_key) | ||
key_algorithm = NAMED_CURVES[private_key.group.curve_name] | ||
if algorithm != key_algorithm | ||
raise IncorrectAlgorithm, "payload algorithm is #{algorithm} but #{key_algorithm} signing key was provided" | ||
end | ||
|
||
digest = OpenSSL::Digest.new(algorithm.sub('ES', 'sha')) | ||
asn1_to_raw(private_key.dsa_sign_asn1(digest.digest(msg)), private_key) | ||
end | ||
|
||
def sign_hmac(algorithm, msg, key) | ||
OpenSSL::HMAC.digest(OpenSSL::Digest.new(algorithm.sub('HS', 'sha')), key, msg) | ||
end | ||
|
||
def verify_rsa(algorithm, public_key, signing_input, signature) | ||
public_key.verify(OpenSSL::Digest.new(algorithm.sub('RS', 'sha')), signature, signing_input) | ||
end | ||
|
||
def verify_ecdsa(algorithm, public_key, signing_input, signature) | ||
key_algorithm = NAMED_CURVES[public_key.group.curve_name] | ||
if algorithm != key_algorithm | ||
raise IncorrectAlgorithm, "payload algorithm is #{algorithm} but #{key_algorithm} verification key was provided" | ||
end | ||
|
||
digest = OpenSSL::Digest.new(algorithm.sub('ES', 'sha')) | ||
public_key.dsa_verify_asn1(digest.digest(signing_input), raw_to_asn1(signature, public_key)) | ||
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 | ||
|
||
def raw_to_asn1(signature, private_key) | ||
byte_size = (private_key.group.degree + 7) / 8 | ||
r = signature[0..(byte_size - 1)] | ||
s = signature[byte_size..-1] || '' | ||
OpenSSL::ASN1::Sequence.new([r, s].map { |int| OpenSSL::ASN1::Integer.new(OpenSSL::BN.new(int, 2)) }).to_der | ||
end | ||
|
||
# From devise | ||
# constant-time comparison algorithm to prevent timing attacks | ||
def secure_compare(a, b) | ||
return false if a.nil? || b.nil? || a.empty? || b.empty? || a.bytesize != b.bytesize | ||
l = a.unpack "C#{a.bytesize}" | ||
res = 0 | ||
b.each_byte { |byte| res |= byte ^ l.shift } | ||
res.zero? | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use
module_function
instead ofextend self
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about
private
functions? I don't think it's possible to define private function withmodule_functions
.What are the benefits of
module_functions
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
module_function
is the preferred coding style.You're alway able to define a function as private by using this syntax:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think
module_function
respects the private visibility.