|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | | -require 'ruby_saml/xml/crypto' |
| 3 | +require 'rexml/element' |
| 4 | +require 'openssl' |
| 5 | +require 'nokogiri' |
| 6 | +require 'digest/sha1' |
| 7 | +require 'digest/sha2' |
| 8 | + |
| 9 | +module RubySaml |
| 10 | + # Utility module for working with XML. |
| 11 | + module XML |
| 12 | + extend self |
| 13 | + |
| 14 | + # XML constants |
| 15 | + # |
| 16 | + # @api private |
| 17 | + C14N = 'http://www.w3.org/2001/10/xml-exc-c14n#' |
| 18 | + DSIG = 'http://www.w3.org/2000/09/xmldsig#' |
| 19 | + RSA_SHA1 = 'http://www.w3.org/2000/09/xmldsig#rsa-sha1' |
| 20 | + RSA_SHA224 = 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha224' |
| 21 | + RSA_SHA256 = 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha256' |
| 22 | + RSA_SHA384 = 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha384' |
| 23 | + RSA_SHA512 = 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha512' |
| 24 | + DSA_SHA1 = 'http://www.w3.org/2000/09/xmldsig#dsa-sha1' |
| 25 | + DSA_SHA256 = 'http://www.w3.org/2009/xmldsig11#dsa-sha256' |
| 26 | + ECDSA_SHA1 = 'http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha1' |
| 27 | + ECDSA_SHA224 = 'http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha224' |
| 28 | + ECDSA_SHA256 = 'http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha256' |
| 29 | + ECDSA_SHA384 = 'http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha384' |
| 30 | + ECDSA_SHA512 = 'http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha512' |
| 31 | + SHA1 = 'http://www.w3.org/2000/09/xmldsig#sha1' |
| 32 | + SHA224 = 'http://www.w3.org/2001/04/xmldsig-more#sha224' |
| 33 | + SHA256 = 'http://www.w3.org/2001/04/xmlenc#sha256' |
| 34 | + SHA384 = 'http://www.w3.org/2001/04/xmldsig-more#sha384' |
| 35 | + SHA512 = 'http://www.w3.org/2001/04/xmlenc#sha512' |
| 36 | + ENVELOPED_SIG = 'http://www.w3.org/2000/09/xmldsig#enveloped-signature' |
| 37 | + |
| 38 | + NOKOGIRI_OPTIONS = Nokogiri::XML::ParseOptions::STRICT | |
| 39 | + Nokogiri::XML::ParseOptions::NONET |
| 40 | + |
| 41 | + # Find XML canonicalization algorithm |
| 42 | + # |
| 43 | + # @api private |
| 44 | + def canon_algorithm(element, default: true) |
| 45 | + case get_algorithm_attr(element) |
| 46 | + when %r{\Ahttp://www\.w3\.org/TR/2001/REC-xml-c14n-20010315#?(?:WithComments)?\z}i |
| 47 | + Nokogiri::XML::XML_C14N_1_0 |
| 48 | + when %r{\Ahttp://www\.w3\.org/2006/12/xml-c14n11#?(?:WithComments)?\z}i |
| 49 | + Nokogiri::XML::XML_C14N_1_1 |
| 50 | + when %r{\Ahttp://www\.w3\.org/2001/10/xml-exc-c14n#?(?:WithComments)?\z}i |
| 51 | + Nokogiri::XML::XML_C14N_EXCLUSIVE_1_0 |
| 52 | + else |
| 53 | + Nokogiri::XML::XML_C14N_EXCLUSIVE_1_0 if default |
| 54 | + end |
| 55 | + end |
| 56 | + |
| 57 | + # Find XML signature algorithm |
| 58 | + # |
| 59 | + # @api private |
| 60 | + def signature_algorithm(element) |
| 61 | + alg = get_algorithm_attr(element) |
| 62 | + match_data = alg&.downcase&.match(/(?:\A|#)(rsa|dsa|ecdsa)-(sha\d+)\z/i) || {} |
| 63 | + key_alg = match_data[1] |
| 64 | + hash_alg = match_data[2] |
| 65 | + |
| 66 | + key = case key_alg |
| 67 | + when 'rsa' then OpenSSL::PKey::RSA |
| 68 | + when 'dsa' then OpenSSL::PKey::DSA |
| 69 | + when 'ecdsa' then OpenSSL::PKey::EC |
| 70 | + else # rubocop:disable Lint/DuplicateBranch |
| 71 | + # TODO: raise ArgumentError.new("Invalid key algorithm: #{alg}") |
| 72 | + OpenSSL::PKey::RSA |
| 73 | + end |
| 74 | + |
| 75 | + [key, hash_algorithm(hash_alg)] |
| 76 | + end |
| 77 | + |
| 78 | + # Find XML digest hashing algorithm |
| 79 | + # |
| 80 | + # @api private |
| 81 | + def hash_algorithm(element) |
| 82 | + alg = get_algorithm_attr(element) |
| 83 | + hash_alg = alg&.downcase&.match(/(?:\A|[#-])(sha\d+)\z/i)&.[](1) |
| 84 | + |
| 85 | + case hash_alg |
| 86 | + when 'sha1' then OpenSSL::Digest::SHA1 |
| 87 | + when 'sha224' then OpenSSL::Digest::SHA224 |
| 88 | + when 'sha256' then OpenSSL::Digest::SHA256 |
| 89 | + when 'sha384' then OpenSSL::Digest::SHA384 |
| 90 | + when 'sha512' then OpenSSL::Digest::SHA512 |
| 91 | + else # rubocop:disable Lint/DuplicateBranch |
| 92 | + # TODO: raise ArgumentError.new("Invalid hash algorithm: #{alg}") |
| 93 | + OpenSSL::Digest::SHA256 |
| 94 | + end |
| 95 | + end |
| 96 | + |
| 97 | + private |
| 98 | + |
| 99 | + def get_algorithm_attr(element) |
| 100 | + if element.is_a?(Nokogiri::XML::Element) |
| 101 | + element['Algorithm'] |
| 102 | + elsif element.is_a?(REXML::Element) |
| 103 | + element.attribute('Algorithm').value |
| 104 | + elsif element |
| 105 | + element |
| 106 | + end |
| 107 | + end |
| 108 | + end |
| 109 | +end |
| 110 | + |
4 | 111 | require 'ruby_saml/xml/base_document' |
5 | 112 | require 'ruby_saml/xml/document' |
6 | 113 | require 'ruby_saml/xml/signed_document' |
7 | 114 |
|
8 | 115 | # @deprecated This alias adds compatibility with v1.x and will be removed in v2.1.0 |
9 | | -XMLSecurity = RubySaml::XML |
| 116 | +XMLSecurity = RubySaml::XML unless defined?(XMLSecurity) |
0 commit comments