Skip to content
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

Add SHA256 support #9

Merged
merged 2 commits into from
Jun 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/omniauth/strategies/latvija.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'time'
require 'openssl'
require 'digest/sha1'
require 'digest/sha2'
require 'xmlenc'
require 'nokogiri'
require 'omniauth/strategies/latvija/response'
Expand Down
2 changes: 1 addition & 1 deletion lib/omniauth/strategies/latvija/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def attributes

def fingerprint
cert = OpenSSL::X509::Certificate.new(options[:certificate])
Digest::SHA1.hexdigest(cert.to_der).upcase.scan(/../).join(':')
Digest::SHA256.hexdigest(cert.to_der).upcase.scan(/../).join(':')
end

def conditions_tag
Expand Down
16 changes: 13 additions & 3 deletions lib/omniauth/strategies/latvija/signed_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,18 @@ def certificate
end
end

def digest_method_class(reference)
value = reference.xpath('.//xmlns:DigestMethod', xmlns: DSIG).attribute('Algorithm').value
value == "#{DSIG}sha1" ? Digest::SHA1 : Digest::SHA256
end

def signature_method_class(sig_element)
value = sig_element.xpath('.//xmlns:SignatureMethod', xmlns: DSIG).attribute('Algorithm').value
value == "#{DSIG}rsa-sha1" ? OpenSSL::Digest::SHA1 : OpenSSL::Digest::SHA256
end

def validate_fingerprint!(idp_cert_fingerprint)
fingerprint = Digest::SHA1.hexdigest(certificate.to_der)
fingerprint = Digest::SHA256.hexdigest(certificate.to_der)
if fingerprint != idp_cert_fingerprint.gsub(/[^a-zA-Z0-9]/, '').downcase
raise ValidationError, 'Fingerprint mismatch'
end
Expand All @@ -80,7 +90,7 @@ def validate_digest!(sig_element)
hashed_element = response_without_signature.
at_xpath("//*[@AssertionID='#{uri[1, uri.size]}']").
canonicalize(CANON_MODE)
hash = Base64.encode64(Digest::SHA1.digest(hashed_element)).chomp
hash = Base64.encode64(digest_method_class(ref).digest(hashed_element)).chomp
digest_value = ref.xpath('.//xmlns:DigestValue', xmlns: DSIG).text

raise ValidationError, 'Digest mismatch' if hash != digest_value
Expand All @@ -94,7 +104,7 @@ def validate_signature!(sig_element)
base64_signature = sig_element.xpath('.//xmlns:SignatureValue', xmlns: DSIG).text
signature = Base64.decode64(base64_signature)

unless certificate.public_key.verify(OpenSSL::Digest::SHA1.new, signature, signed_info_element)
unless certificate.public_key.verify(signature_method_class(sig_element).new, signature, signed_info_element)
raise ValidationError, 'Key validation error'
end
end
Expand Down