Canonicalizing XML in Ruby

一笑奈何 提交于 2019-12-04 02:28:55

Give these two gems a shot:

http://rubygems.org/gems/coupa-libxml-ruby

http://rubygems.org/gems/xmlsec-ruby

I wrote them for a SAML project. The first patches libxml-ruby to add a binding for the canonicalize function in the base C library.

The latter is ruby binding for xmlsec. Right now all that works is signature verification, which was all I needed for the project, but it sounds like it'd fit your needs too.

I'd recommend going with xmlsec because trying to write your own XML signature verification code is an exercise in futility. Just wait til you have to deal with multiple enveloped signatures, embedded certificates, gah. Let xmlsec handle that crap.

After looking around some more I've found that nokogiri has put c14n support on the todo list for the next release. Don't know more than that-- but it appears that no widely used XML library supports c14n as of June 2010. I'll close this out since nothing really popped up.

I have a ruby/rails Service Provider and a .NET (ComponentSoft) IDP

this worked for me ( I had no issues with the canonicalized version of the XML):

received_certificate = XPath.first(response_document,"//samlp:Response//Signature//KeyInfo//X509Data//X509Certificate").text

def self.verify_signature(received_certificate, idp_certificate_path)
  certificate ||= OpenSSL::X509::Certificate.new(File.read(idp_certificate_path))
  cert_decoded = Base64.decode64(received_certificate)
  cert = OpenSSL::X509::Certificate.new(cert_decoded)
  certificate.verify(cert.public_key)
end

The xmlcanonicalizer gem seems to be the most up-to-date ruby canonicaliser available:

https://github.com/andrewferk/xmlcanonicalizer

It does have a bug however, that makes it useless at canonicalising some XML trees. Some kind person has submitted a patch but it hasn't been applied yet:

https://github.com/andrewferk/xmlcanonicalizer/pull/1

This patched gem plus ruby-saml does the trick (plus more, if you're trying to implement SAML SSO:

https://github.com/onelogin/ruby-saml

Hope that helps someone save the 3 days I wasted trying to get things to work! :)

had problems with xmlcanonicalizer.

xmlstarlet worked for me:

`echo "#{xml_str}" | xmlstarlet c14n`
Chris Kimpton

Probably a little late and not really ideal, but this fork uses XMLStarlet via command line for canonicalization.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!