Class: OmniAuth::Strategies::SAML::XMLSecurity::SignedDocument

Inherits:
REXML::Document
  • Object
show all
Defined in:
lib/omniauth/strategies/saml/xml_security.rb

Constant Summary collapse

DSIG =
"http://www.w3.org/2000/09/xmldsig#"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ SignedDocument

Returns a new instance of SignedDocument.



43
44
45
46
# File 'lib/omniauth/strategies/saml/xml_security.rb', line 43

def initialize(response)
  super(response)
  extract_signed_element_id
end

Instance Attribute Details

#signed_element_idObject

Returns the value of attribute signed_element_id.



41
42
43
# File 'lib/omniauth/strategies/saml/xml_security.rb', line 41

def signed_element_id
  @signed_element_id
end

Instance Method Details

#validate(idp_cert_fingerprint, soft = true) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/omniauth/strategies/saml/xml_security.rb', line 48

def validate(idp_cert_fingerprint, soft = true)
  # get cert from response
  base64_cert = self.elements["//ds:X509Certificate"].text
  cert_text   = Base64.decode64(base64_cert)
  cert        = OpenSSL::X509::Certificate.new(cert_text)

  # check cert matches registered idp cert
  fingerprint = Digest::SHA1.hexdigest(cert.to_der)

  if fingerprint != idp_cert_fingerprint.gsub(/[^a-zA-Z0-9]/,"").downcase
    return soft ? false : (raise OmniAuth::Strategies::SAML::ValidationError.new("Fingerprint mismatch"))
  end

  validate_doc(base64_cert, soft)
end

#validate_doc(base64_cert, soft = true) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/omniauth/strategies/saml/xml_security.rb', line 64

def validate_doc(base64_cert, soft = true)
  # validate references

  # check for inclusive namespaces

  inclusive_namespaces            = []
  inclusive_namespace_element     = REXML::XPath.first(self, "//ec:InclusiveNamespaces")

  if inclusive_namespace_element
    prefix_list                   = inclusive_namespace_element.attributes.get_attribute('PrefixList').value
    inclusive_namespaces          = prefix_list.split(" ")
  end

  # remove signature node
  sig_element = REXML::XPath.first(self, "//ds:Signature", {"ds"=>"http://www.w3.org/2000/09/xmldsig#"})
  sig_element.remove

  # check digests
  REXML::XPath.each(sig_element, "//ds:Reference", {"ds"=>"http://www.w3.org/2000/09/xmldsig#"}) do |ref|
    uri                           = ref.attributes.get_attribute("URI").value
    hashed_element                = REXML::XPath.first(self, "//[@ID='#{uri[1,uri.size]}']")
    canoner                       = XML::Util::XmlCanonicalizer.new(false, true)
    canoner.inclusive_namespaces  = inclusive_namespaces if canoner.respond_to?(:inclusive_namespaces) && !inclusive_namespaces.empty?
    canon_hashed_element          = canoner.canonicalize(hashed_element)
    hash                          = Base64.encode64(Digest::SHA1.digest(canon_hashed_element)).chomp
    digest_value                  = REXML::XPath.first(ref, "//ds:DigestValue", {"ds"=>"http://www.w3.org/2000/09/xmldsig#"}).text

    if hash != digest_value
      return soft ? false : (raise OmniAuth::Strategies::SAML::ValidationError.new("Digest mismatch"))
    end
  end

  # verify signature
  canoner                 = XML::Util::XmlCanonicalizer.new(false, true)
  signed_info_element     = REXML::XPath.first(sig_element, "//ds:SignedInfo", {"ds"=>"http://www.w3.org/2000/09/xmldsig#"})
  canon_string            = canoner.canonicalize(signed_info_element)

  base64_signature        = REXML::XPath.first(sig_element, "//ds:SignatureValue", {"ds"=>"http://www.w3.org/2000/09/xmldsig#"}).text
  signature               = Base64.decode64(base64_signature)

  # get certificate object
  cert_text               = Base64.decode64(base64_cert)
  cert                    = OpenSSL::X509::Certificate.new(cert_text)

  if !cert.public_key.verify(OpenSSL::Digest::SHA1.new, signature, canon_string)
    return soft ? false : (raise OmniAuth::Strategies::SAML::ValidationError.new("Key validation error"))
  end

  return true
end