Class: XMLSecurity::SignedDocument
- Inherits:
-
BaseDocument
- Object
- REXML::Document
- BaseDocument
- XMLSecurity::SignedDocument
- Defined in:
- lib/xml_security.rb
Constant Summary
Constants inherited from BaseDocument
BaseDocument::C14N, BaseDocument::DSIG, BaseDocument::NOKOGIRI_OPTIONS
Instance Attribute Summary collapse
Instance Method Summary collapse
- #validate_document(idp_cert_fingerprint, soft = true, options = {}) ⇒ Object
- #validate_document_with_cert(idp_cert, soft = true) ⇒ Object
- #validate_signature(base64_cert, soft = true) ⇒ Object
Methods inherited from BaseDocument
Instance Attribute Details
#signed_element_id ⇒ Object
191 192 193 |
# File 'lib/xml_security.rb', line 191 def signed_element_id @signed_element_id ||= extract_signed_element_id end |
Instance Method Details
#validate_document(idp_cert_fingerprint, soft = true, options = {}) ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/xml_security.rb', line 195 def validate_document(idp_cert_fingerprint, soft = true, = {}) # get cert from response cert_element = REXML::XPath.first( self, "//ds:X509Certificate", { "ds"=>DSIG } ) if cert_element base64_cert = OneLogin::RubySaml::Utils.element_text(cert_element) cert_text = Base64.decode64(base64_cert) begin cert = OpenSSL::X509::Certificate.new(cert_text) rescue OpenSSL::X509::CertificateError => _e return soft ? false : (raise OneLogin::RubySaml::ValidationError.new("Certificate Error")) end if [:fingerprint_alg] fingerprint_alg = XMLSecurity::BaseDocument.new.algorithm([:fingerprint_alg]).new else fingerprint_alg = OpenSSL::Digest::SHA1.new end fingerprint = fingerprint_alg.hexdigest(cert.to_der) # check cert matches registered idp cert if fingerprint != idp_cert_fingerprint.gsub(/[^a-zA-Z0-9]/,"").downcase return soft ? false : (raise OneLogin::RubySaml::ValidationError.new("Fingerprint mismatch")) end else if [:cert] base64_cert = Base64.encode64([:cert].to_pem) else return soft ? false : (raise OneLogin::RubySaml::ValidationError.new("Certificate element missing in response (ds:X509Certificate) and not cert provided at settings")) end end validate_signature(base64_cert, soft) end |
#validate_document_with_cert(idp_cert, soft = true) ⇒ Object
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/xml_security.rb', line 233 def validate_document_with_cert(idp_cert, soft = true) # get cert from response cert_element = REXML::XPath.first( self, "//ds:X509Certificate", { "ds"=>DSIG } ) if cert_element base64_cert = OneLogin::RubySaml::Utils.element_text(cert_element) cert_text = Base64.decode64(base64_cert) begin cert = OpenSSL::X509::Certificate.new(cert_text) rescue OpenSSL::X509::CertificateError => _e return soft ? false : (raise OneLogin::RubySaml::ValidationError.new("Certificate Error")) end # check saml response cert matches provided idp cert if idp_cert.to_pem != cert.to_pem return false end else base64_cert = Base64.encode64(idp_cert.to_pem) end validate_signature(base64_cert, true) end |
#validate_signature(base64_cert, soft = true) ⇒ Object
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 |
# File 'lib/xml_security.rb', line 260 def validate_signature(base64_cert, soft = true) document = Nokogiri::XML(self.to_s) do |config| config. = XMLSecurity::BaseDocument::NOKOGIRI_OPTIONS end # create a rexml document @working_copy ||= REXML::Document.new(self.to_s).root # get signature node sig_element = REXML::XPath.first( @working_copy, "//ds:Signature", {"ds"=>DSIG} ) if sig_element.nil? return soft ? false : (raise OneLogin::RubySaml::ValidationError.new("No Signature Node")) end # signature method sig_alg_value = REXML::XPath.first( sig_element, "./ds:SignedInfo/ds:SignatureMethod", {"ds"=>DSIG} ) signature_algorithm = algorithm(sig_alg_value) # get signature base64_signature = REXML::XPath.first( sig_element, "./ds:SignatureValue", {"ds" => DSIG} ) if base64_signature.nil? return soft ? false : (raise OneLogin::RubySaml::ValidationError.new("SignatureValue not found")) end signature = Base64.decode64(OneLogin::RubySaml::Utils.element_text(base64_signature)) # canonicalization method canon_algorithm = canon_algorithm REXML::XPath.first( sig_element, './ds:SignedInfo/ds:CanonicalizationMethod', '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_string = noko_signed_info_element.canonicalize(canon_algorithm) noko_sig_element.remove # get inclusive namespaces inclusive_namespaces = extract_inclusive_namespaces # check digests ref = REXML::XPath.first(sig_element, "//ds:Reference", {"ds"=>DSIG}) hashed_element = document.at_xpath("//*[@ID=$id]", nil, { 'id' => extract_signed_element_id }) canon_algorithm = canon_algorithm REXML::XPath.first( ref, '//ds:CanonicalizationMethod', { "ds" => DSIG } ) canon_algorithm = process_transforms(ref, canon_algorithm) 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) encoded_digest_value = REXML::XPath.first( ref, "//ds:DigestValue", { "ds" => DSIG } ) digest_value = Base64.decode64(OneLogin::RubySaml::Utils.element_text(encoded_digest_value)) unless digests_match?(hash, digest_value) return soft ? false : (raise OneLogin::RubySaml::ValidationError.new("Digest mismatch")) end # get certificate object cert_text = Base64.decode64(base64_cert) cert = OpenSSL::X509::Certificate.new(cert_text) # verify signature unless cert.public_key.verify(signature_algorithm.new, signature, canon_string) return soft ? false : (raise OneLogin::RubySaml::ValidationError.new("Key validation error")) end return true end |