Class: XMLSecurity::SignedDocument
- Inherits:
-
BaseDocument
- Object
- REXML::Document
- BaseDocument
- XMLSecurity::SignedDocument
- Includes:
- OneLogin::RubySaml::ErrorHandling
- Defined in:
- lib/xml_security.rb
Constant Summary
Constants inherited from BaseDocument
BaseDocument::C14N, BaseDocument::DSIG, BaseDocument::NOKOGIRI_OPTIONS
Instance Attribute Summary collapse
Attributes included from OneLogin::RubySaml::ErrorHandling
Instance Method Summary collapse
- #cache_referenced_xml(soft, check_malformed_doc = true) ⇒ Object
-
#initialize(response, errors = []) ⇒ SignedDocument
constructor
A new instance of SignedDocument.
- #processed ⇒ Object
- #referenced_xml ⇒ Object
- #reset_elements ⇒ Object
-
#validate_document(idp_cert_fingerprint, soft = true, options = {}) ⇒ Object
Validates the referenced_xml, which is the signed part of the document.
- #validate_document_with_cert(idp_cert, soft = true) ⇒ Object
- #validate_signature(base64_cert, soft = true) ⇒ Object
Methods included from OneLogin::RubySaml::ErrorHandling
Methods inherited from BaseDocument
#algorithm, #canon_algorithm, safe_load_xml
Constructor Details
#initialize(response, errors = []) ⇒ SignedDocument
Returns a new instance of SignedDocument.
216 217 218 219 220 |
# File 'lib/xml_security.rb', line 216 def initialize(response, errors = []) super(response) @errors = errors reset_elements end |
Instance Attribute Details
#signed_element_id ⇒ Object
239 240 241 |
# File 'lib/xml_security.rb', line 239 def signed_element_id @signed_element_id ||= extract_signed_element_id end |
Instance Method Details
#cache_referenced_xml(soft, check_malformed_doc = true) ⇒ Object
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 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 |
# File 'lib/xml_security.rb', line 314 def cache_referenced_xml(soft, check_malformed_doc = true) reset_elements @processed = true begin nokogiri_document = XMLSecurity::BaseDocument.safe_load_xml(self, check_malformed_doc) rescue StandardError => error @errors << error. return false if soft raise ValidationError.new("XML load failed: #{error.}") 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} ) return if sig_element.nil? # 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} ) return if base64_signature.nil? base64_signature_text = OneLogin::RubySaml::Utils.element_text(base64_signature) @signature = base64_signature_text.nil? ? nil : Base64.decode64(base64_signature_text) # canonicalization method canon_algorithm = canon_algorithm REXML::XPath.first( sig_element, './ds:SignedInfo/ds:CanonicalizationMethod', 'ds' => DSIG ) noko_sig_element = nokogiri_document.at_xpath('//ds:Signature', 'ds' => DSIG) noko_signed_info_element = noko_sig_element.at_xpath('./ds:SignedInfo', 'ds' => DSIG) @cached_signed_info = noko_signed_info_element.canonicalize(canon_algorithm) ### Now get the @referenced_xml to use? rexml_signed_info = REXML::Document.new(@cached_signed_info.to_s).root noko_sig_element.remove # get inclusive namespaces inclusive_namespaces = extract_inclusive_namespaces # check digests @ref = REXML::XPath.first(rexml_signed_info, "./ds:Reference", {"ds"=>DSIG}) return if @ref.nil? reference_nodes = nokogiri_document.xpath("//*[@ID=$id]", nil, { 'id' => extract_signed_element_id }) hashed_element = reference_nodes[0] return if hashed_element.nil? canon_algorithm = canon_algorithm REXML::XPath.first( rexml_signed_info, './ds:CanonicalizationMethod', { "ds" => DSIG } ) canon_algorithm = process_transforms(@ref, canon_algorithm) @referenced_xml = hashed_element.canonicalize(canon_algorithm, inclusive_namespaces) end |
#processed ⇒ Object
231 232 233 |
# File 'lib/xml_security.rb', line 231 def processed @processed end |
#referenced_xml ⇒ Object
235 236 237 |
# File 'lib/xml_security.rb', line 235 def referenced_xml @referenced_xml end |
#reset_elements ⇒ Object
222 223 224 225 226 227 228 229 |
# File 'lib/xml_security.rb', line 222 def reset_elements @referenced_xml = nil @cached_signed_info = nil @signature = nil @signature_algorithm = nil @ref = nil @processed = false end |
#validate_document(idp_cert_fingerprint, soft = true, options = {}) ⇒ Object
Validates the referenced_xml, which is the signed part of the document
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 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 |
# File 'lib/xml_security.rb', line 244 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 append_error("Document Certificate Error", soft) end if [:fingerprint_alg] fingerprint_alg = XMLSecurity::BaseDocument.new.algorithm([:fingerprint_alg]).new else fingerprint_alg = OpenSSL::Digest.new('SHA1') 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 append_error("Fingerprint mismatch", soft) end base64_cert = Base64.encode64(cert.to_der) else if [:cert] base64_cert = Base64.encode64([:cert].to_pem) else if soft return false else return append_error("Certificate element missing in response (ds:X509Certificate) and not cert provided at settings", soft) end end end validate_signature(base64_cert, soft) end |
#validate_document_with_cert(idp_cert, soft = true) ⇒ Object
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 |
# File 'lib/xml_security.rb', line 287 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 append_error("Document Certificate Error", soft) end # check saml response cert matches provided idp cert if idp_cert.to_pem != cert.to_pem return append_error("Certificate of the Signature element does not match provided certificate", soft) end end encoded_idp_cert = Base64.encode64(idp_cert.to_pem) validate_signature(encoded_idp_cert, true) end |
#validate_signature(base64_cert, soft = true) ⇒ Object
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 |
# File 'lib/xml_security.rb', line 398 def validate_signature(base64_cert, soft = true) if !@processed cache_referenced_xml(soft) end return append_error("No Signature Algorithm Method found", soft) if @signature_algorithm.nil? return append_error("No Signature node found", soft) if @signature.nil? return append_error("No canonized SignedInfo ", soft) if @cached_signed_info.nil? return append_error("No Reference node found", soft) if @ref.nil? return append_error("No referenced XML", soft) if @referenced_xml.nil? # get certificate object cert_text = Base64.decode64(base64_cert) cert = OpenSSL::X509::Certificate.new(cert_text) digest_algorithm = algorithm(REXML::XPath.first( @ref, "./ds:DigestMethod", { "ds" => DSIG } )) hash = digest_algorithm.digest(@referenced_xml) encoded_digest_value = REXML::XPath.first( @ref, "./ds:DigestValue", { "ds" => DSIG } ) encoded_digest_value_text = OneLogin::RubySaml::Utils.element_text(encoded_digest_value) digest_value = encoded_digest_value_text.nil? ? nil : Base64.decode64(encoded_digest_value_text) # Compare the computed "hash" with the "signed" hash unless hash && hash == digest_value return append_error("Digest mismatch", soft) end # verify signature unless cert.public_key.verify(@signature_algorithm.new, @signature, @cached_signed_info) return append_error("Key validation error", soft) end return true end |