Class: SamlIdp::XMLSecurity::SignedDocument

Inherits:
REXML::Document
  • Object
show all
Defined in:
lib/saml_idp/xml_security.rb

Constant Summary collapse

ValidationError =
Class.new(StandardError)
C14N =
"http://www.w3.org/2001/10/xml-exc-c14n#"
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.



41
42
43
44
# File 'lib/saml_idp/xml_security.rb', line 41

def initialize(response)
  super(response)
  extract_signed_element_id
end

Instance Attribute Details

#signed_element_idObject

Returns the value of attribute signed_element_id.



39
40
41
# File 'lib/saml_idp/xml_security.rb', line 39

def signed_element_id
  @signed_element_id
end

Instance Method Details

#fingerprint_cert(cert) ⇒ Object



71
72
73
74
75
76
# File 'lib/saml_idp/xml_security.rb', line 71

def fingerprint_cert(cert)
  # pick algorithm based on the doc's digest algorithm
  ref_elem = REXML::XPath.first(self, "//ds:Reference", {"ds"=>DSIG})
  digest_algorithm = algorithm(REXML::XPath.first(ref_elem, "//ds:DigestMethod"))
  digest_algorithm.hexdigest(cert.to_der)
end

#fingerprint_cert_sha1(cert) ⇒ Object



78
79
80
# File 'lib/saml_idp/xml_security.rb', line 78

def fingerprint_cert_sha1(cert)
  OpenSSL::Digest::SHA1.hexdigest(cert.to_der)
end

#validate(idp_base64_cert, idp_cert_fingerprint, soft = true) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/saml_idp/xml_security.rb', line 46

def validate(idp_base64_cert, idp_cert_fingerprint, soft = true)
  # get cert from response
  cert_element = REXML::XPath.first(self, "//ds:X509Certificate", { "ds"=>DSIG })
  if cert_element
    idp_base64_cert = cert_element.text
    cert_text    = Base64.decode64(idp_base64_cert)
    cert         = OpenSSL::X509::Certificate.new(cert_text)

    # check cert matches registered idp cert
    fingerprint = fingerprint_cert(cert)
    sha1_fingerprint = fingerprint_cert_sha1(cert)
    plain_idp_cert_fingerprint = idp_cert_fingerprint.gsub(/[^a-zA-Z0-9]/,"").downcase

    if fingerprint != plain_idp_cert_fingerprint && sha1_fingerprint != plain_idp_cert_fingerprint
      return soft ? false : (raise ValidationError.new("Fingerprint mismatch"))
    end
  end

  if idp_base64_cert.nil? || idp_base64_cert.empty?
    raise ValidationError.new("Certificate validation is required, but it doesn't exist.")
  end

  validate_doc(idp_base64_cert, soft)
end

#validate_doc(base64_cert, soft = true) ⇒ Object



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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/saml_idp/xml_security.rb', line 82

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

  # check for inclusive namespaces
  inclusive_namespaces = extract_inclusive_namespaces

  document = Nokogiri.parse(self.to_s)

  # create a working copy so we don't modify the original
  @working_copy ||= REXML::Document.new(self.to_s).root

  # store and remove signature node
  @sig_element ||= begin
                     element = REXML::XPath.first(@working_copy, "//ds:Signature", {"ds"=>DSIG})
                     element.remove
                   end


  # verify signature
  signed_info_element     = REXML::XPath.first(@sig_element, "//ds:SignedInfo", {"ds"=>DSIG})
  noko_sig_element = document.at_xpath('//ds:Signature', 'ds' => DSIG)
  noko_signed_info_element = noko_sig_element.at_xpath('./ds:SignedInfo', 'ds' => DSIG)
  canon_algorithm = canon_algorithm REXML::XPath.first(@sig_element, '//ds:CanonicalizationMethod', 'ds' => DSIG)
  canon_string = noko_signed_info_element.canonicalize(canon_algorithm)
  noko_sig_element.remove

  # check digests
  REXML::XPath.each(@sig_element, "//ds:Reference", {"ds"=>DSIG}) do |ref|
    uri                           = ref.attributes.get_attribute("URI").value

    hashed_element                = document.at_xpath("//*[@ID='#{uri[1..-1]}']")
    canon_algorithm               = canon_algorithm REXML::XPath.first(ref, '//ds:CanonicalizationMethod', 'ds' => DSIG)
    canon_hashed_element          = hashed_element.canonicalize(canon_algorithm, inclusive_namespaces)

    digest_algorithm              = algorithm(REXML::XPath.first(ref, "//ds:DigestMethod", {'ds' => DSIG}))

    hash                          = digest_algorithm.digest(canon_hashed_element)
    digest_value                  = Base64.decode64(REXML::XPath.first(ref, "//ds:DigestValue", {"ds"=>DSIG}).text)

    unless digests_match?(hash, digest_value)
      return soft ? false : (raise ValidationError.new("Digest mismatch"))
    end
  end

  base64_signature        = REXML::XPath.first(@sig_element, "//ds:SignatureValue", {"ds"=>DSIG}).text
  signature               = Base64.decode64(base64_signature)

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

  # signature method
  signature_algorithm     = algorithm(REXML::XPath.first(signed_info_element, "//ds:SignatureMethod", {"ds"=>DSIG}))

  unless cert.public_key.verify(signature_algorithm.new, signature, canon_string)
    return soft ? false : (raise ValidationError.new("Key validation error"))
  end

  return true
end