为了使SAML正常运行,我需要了解什么有关XML签名的信息? [英] What do I need to know about XML Signatures to get SAML working?

查看:112
本文介绍了为了使SAML正常运行,我需要了解什么有关XML签名的信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在工作中,我们需要一个网络应用程序,我们需要使用经过SAML验证的Single Sign On与另一家公司的网络应用程序进行交互。我们的网络应用程序是用PHP编写的,与其他公司使用的语言选择显然无关紧要。尽管如此,我仍然需要编写一个简单的API,该另一家公司可以将该API与SAML请求一起发送给SOAP请求,并生成回SAML响应。我从头开始编写它的原因有三个: 1),即使我想要一个 2),似乎用PHP编写的SAML交互似乎没有太多选择 strong>可以限制添加其他第三方组件所需的开销,并且 3)从头开始创建事物通常会使我有了更好的理解,并使我更有能力适应事物



无论如何,我一般对SAML,SOAP和XML标准还是陌生的,所以我一直在自学走。我已经为我们的目的准备了非常完善的API,一个例外是另一家公司已指定要求我们的回复必须使用证书进行数字签名(同样,我们收到的请求也将进行数字签名)。因此,我一直在尝试找出如何处理/生成XML签名的方法,但是老实说,这有点令人困惑,因为W3C规范的阅读不尽如人意。



断言的第5.4.8节OASIS安全标记语言(SAML)V1.1 文档的协议和协议(我已经关闭了该文档,因为另一家公司表示他们将使用v1.1)包括一个签名响应的示例包含一个签名的断言,我将在这里包括以供参考:

 < Response IssueInstant = 2003-04- 17T00:46:02Z MajorVersion = 1 MinorVersion = 1 
Recipient = www.opensaml.org ResponseID = _ c7055387-af61-4fce-8b98-e2927324b306
xmlns = urn :oasis:names:tc:SAML:1.0:protocol
xmlns:samlp = urn:oasis:names:tc:SAML:1.0:protocol
xmlns:xsd = http:// www .w3.org / 2001 / XMLSchema
xmlns:xsi = http://www.w3.org/2001/XMLSch ema-instance
< ds:Signature xmlns:ds = http://www.w3.org/2000/09/xmldsig#>
< ds:SignedInfo>
< ds:CanonicalizationMethod Algorithm = http://www.w3.org/2001/10/xml-exc-c14n# />
< ds:SignatureMethod Algorithm = http://www.w3.org/2000/09/xmldsig#rsa-sha1 />
< ds:参考URI = #_ c7055387-af61-4fce-8b98-e2927324b306>
< ds:Transforms>
< ds:Transform Algorithm = http://www.w3.org/2000/09/xmldsig#enveloped-signature />
< ds:Transform Algorithm = http://www.w3.org/2001/10/xml-exc-c14n#>
< InclusiveNamespaces PrefixList =#default saml samlp ds xsd xsi
xmlns = http://www.w3.org/2001/10/xml-exc-c14n# />
< / ds:Transform>
< / ds:Transforms>
< ds:DigestMethod Algorithm = http://www.w3.org/2000/09/xmldsig#sha1 />
< ds:DigestValue> TCDVSuG6grhyHbzhQFWFzGrxIPE =< / ds:DigestValue>
< / ds:Reference>
< / ds:SignedInfo>
将DS:SignatureValue所&X的催化剂/ GyPbzmFEe85pGD3c1aXG4Vspb9V9jGCjwcRCKrtwPS6vdVNCcY5rHaFPYWkf + 5EIYcPzx + pX1h43SmwviCqXRjRtMANWbHLhWAptaK1ywS7gFgsD01qjyen3CP + m3Dw6vKhaq1ed10BYyrIzb4KkHO4ahNyBVXbJwqv5pUaE4 = LT; / DS:SignatureValue所>
< ds:KeyInfo>
< ds:X509Data>
将DS:x509证书> MIICyjCCAjOgAwIBAgICAnUwDQYJKoZIhvcNAQEEBQAwgakxCzAJBgNVBAYTA1VT ... 8I3bsbmRAUg4UP9hH6ABVq4KQKMknxu1xQxLhpR1y1GPdiowMNTrEG8cCx3w / W ==< / DS:x509证书>
< / ds:X509Data>
< / ds:KeyInfo>
< / ds:Signature>
< Status>< StatusCode Value = samlp:Success />< / Status>
< Assertion AssertionID = _ a75adf55-01d7-40cc-929f-dbd8372ebdfc
IssueInstant = 2003-04-17T00:46:02Z Issuer = www.opensaml.org
MajorVersion = 1 MinorVersion = 1 xmlns = urn:oasis:names:tc:SAML:1.0:assertion
xmlns:xsd = http://www.w3.org/2001/XMLSchema
xmlns:xsi = http://www.w3.org/2001/XMLSchema-instance>
< Conditions NotBefore = 2003-04-17T00:46:02Z NotOnOrAfter = 2003-04-17T00:51:02Z>
< AudienceRestrictionCondition>
< Audience> http://www.opensaml.org< / Audience>
< / AudienceRestrictionCondition>
< /条件>
< AuthenticationStatement AuthenticationInstant = 2003-04-17T00:46:00Z
AuthenticationMethod = urn:oasis:names:tc:SAML:1.0:am:password>
< Subject>
< NameIdentifier Format = urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress> scott@example.org< / NameIdentifier>
< SubjectConfirmation>
< ConfirmationMethod> urn:oasis:names:tc:SAML:1.0:cm:bearer< / ConfirmationMethod>
< / SubjectConfirmation>
< / Subject>
< SubjectLocality IPAddress = 127.0.0.1 />
< / AuthenticationStatement>
< ds:Signature xmlns:ds = http://www.w3.org/2000/09/xmldsig#>
< ds:SignedInfo>
< ds:CanonicalizationMethod Algorithm = http://www.w3.org/2001/10/xml-exc-c14n# />
< ds:SignatureMethod Algorithm = http://www.w3.org/2000/09/xmldsig#rsa-sha1 />
< ds:参考URI = #_ a75adf55-01d7-40cc-929f-dbd8372ebdfc>
< ds:Transforms>
< ds:Transform Algorithm = http://www.w3.org/2000/09/xmldsig#enveloped-signature />
< ds:Transform Algorithm = http://www.w3.org/2001/10/xml-exc-c14n#>
< InclusiveNamespaces PrefixList =#default saml samlp ds xsd xsi
xmlns = http://www.w3.org/2001/10/xml-exc-c14n# />
< / ds:Transform>
< / ds:Transforms>
< ds:DigestMethod Algorithm = http://www.w3.org/2000/09/xmldsig#sha1 />
< ds:DigestValue> Kclet6XcaOgOWXM4gty6 / UNdviI =< / ds:DigestValue>
< / ds:Reference>
< / ds:SignedInfo>
将DS:SignatureValue所> hq4zk + ZknjggCQgZm7ea8fI79gJEsRy3E8LHDpYXWQIgZpkJN9CMLG8ENR4Nrw + n7iyzixBvKXX8P53BTCT4VghPBWhFTSt9tHWu / AtJfOTh6qaAsNdeCyG86jmtp3TDMWuL / cBUj2OtBZOQMFn7jQ9YB7k1Iz3RqVL + wNmeWI4 = LT; / DS:SignatureValue所>
< ds:KeyInfo>
< ds:X509Data>
< ds:X509
< / ds:X509Data>
< / ds:KeyInfo>
< / ds:Signature>
< / Assertion>
< / Response>

那么我如何生成这样的东西?如果我收到这样的信息,该如何验证?另外,谁能提供< ds:Signature> 标签在这里的基本概念概述?似乎有两个< ds:Signature> 标记,一个在主< Response> 中,另一个在< Assertion> ,每个都包含自己的< ds:DigestValue> < ; ds:SignatureValue> < ds:X509Certificate> (以及每个不同)。这些是如何产生的?您可以对此提出的任何建议将不胜感激。教程或代码示例将不胜感激!但是在这一点上,如果您能使我步入正轨,那便是我真正想要的。



顺便说一句,如果有帮助,它在SAML 1.1规范的其他地方说SAML实现应使用仅限专有规范化方法(Excl-C14N),并且应仅使用包络变换。我仍然不太清楚这是什么意思。

解决方案

处理XML签名并不是很困难,如果您很熟悉的话使用XML,但是有很多细节必须绝对正确,否则事情将无法正常工作,因此在这种情况下,我可能不会尝试编写自己的实现(我只实现了部分实现,但这只是为了

无论如何,我对SAML知之甚少,但我确实对XML和XML有所了解。签名,所以也许我可以通过尝试回答您的问题来帮助您。



A Signature 元素在 SignedInfo 子元素中引用经过数字签名的XML文档的特定部分。该子元素的 Reference 子元素(我认为可以有许多 Reference 元素在形成要存储的字节时串联在一起)。签名,但我不记得了)通过 URI 属性指向内容。 Transform 元素描述了在对引用内容进行哈希处理之前对引用内容执行的转换;您将需要查看规格以了解如何定义转换算法。 DigestMethod 元素使哈希算法可以应用于这些转换算法的结果字节(请注意,其中之一始终是将XML转换为字节的规范化),并且 DigestValue 给出该摘要算法的结果。



实际签名在中SignatureValue 元素,它是通过应用 CanonicalizationMethod 元素的规范化来产生字节,然后使用 SignatureMethod对这些字节进行签名而产生的 KeyInfo 元素告诉您如何找到要使用的密钥。



规范化,在上面多次出现只是将XML文档转换为字节的一种方法,以便等效 XML文档产生相同的字节序列。这在数字签名中是必需的,因为算法适用于字节,并且XML可以通过许多中介,这些中介可能会破坏原始字节,但会保留等效性。并且在不同情况下需要使用不同的规范化方法:如果从文档中提取元素并将其放置在其他文档中,则您需要排他的规范化来去除不必要的名称空间定义,但是在其他情况下可能无法正常工作,因此您需要使用包含性的规范化,保留所有作用域内的名称空间。



这只是基础知识。如何产生XML签名有很多不同的选择,如果您想实现一个有效的验证器,则需要考虑所有这些。既然您是XML的新手,那么我将重复我的建议,即使用已有的东西。实施规范是一种有趣的学习经历,但是如果已经可以使用实施,通常会浪费时间。



有W3C的有关签名的文档


At work we have a web app that we'll need to interface with another company's web app using Single Sign On validated by SAML. Our web apps are written in PHP, and it's obviously irrelevant what language choice the other company is using. Nonetheless, I've needed to write a simple API that this other company can send SOAP requests to with SAML requests, and generate back a SAML response. I've been writing it from scratch for three reasons: 1) there don't really seem to be many options for SAML interactions written in PHP even if I wanted one, 2) it limits the overhead that would be involved with adding another third-party component, and 3) creating things from scratch usually leaves me with a significantly better understanding and makes me much more capable to adapt the thing in the future if needed.

Anyways, I'm fairly new to SAML, SOAP, and XML standards in general, so I've kind of been teaching myself as I go. I've got the API pretty much complete for our purposes, with the one exception that the other company has specified that our response will be required to be digitally signed with a certificate (and the request we receive will similarly be digitally signed). So I've been trying to figure out how to process/generate the XML signatures, but honestly it's all a bit confusing as W3C specs aren't exactly light reading.

Section 5.4.8 of the Assertions and Protocol for the OASIS Security Markup Language (SAML) V1.1 document (the document I've been going off, as the other company said they'll be using v1.1) includes an example of a signed response containing a signed assertion, which I'm going to include here to reference:

<Response IssueInstant="2003-04-17T00:46:02Z" MajorVersion="1" MinorVersion="1"
 Recipient="www.opensaml.org" ResponseID="_c7055387-af61-4fce-8b98-e2927324b306"
 xmlns="urn:oasis:names:tc:SAML:1.0:protocol"
 xmlns:samlp="urn:oasis:names:tc:SAML:1.0:protocol"
 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
  <ds:SignedInfo>
   <ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
   <ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
   <ds:Reference URI="#_c7055387-af61-4fce-8b98-e2927324b306">
    <ds:Transforms>
     <ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
     <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#">
      <InclusiveNamespaces PrefixList="#default saml samlp ds xsd xsi"
       xmlns="http://www.w3.org/2001/10/xml-exc-c14n#"/>
     </ds:Transform>
    </ds:Transforms>
    <ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
    <ds:DigestValue>TCDVSuG6grhyHbzhQFWFzGrxIPE=</ds:DigestValue>
   </ds:Reference>
  </ds:SignedInfo>
  <ds:SignatureValue>x/GyPbzmFEe85pGD3c1aXG4Vspb9V9jGCjwcRCKrtwPS6vdVNCcY5rHaFPYWkf+5EIYcPzx+pX1h43SmwviCqXRjRtMANWbHLhWAptaK1ywS7gFgsD01qjyen3CP+m3Dw6vKhaq1ed10BYyrIzb4KkHO4ahNyBVXbJwqv5pUaE4=</ds:SignatureValue>
  <ds:KeyInfo>
   <ds:X509Data>
    <ds:X509Certificate>MIICyjCCAjOgAwIBAgICAnUwDQYJKoZIhvcNAQEEBQAwgakxCzAJBgNVBAYTA1VT ... 8I3bsbmRAUg4UP9hH6ABVq4KQKMknxu1xQxLhpR1y1GPdiowMNTrEG8cCx3w/w==</ds:X509Certificate>
   </ds:X509Data>
  </ds:KeyInfo>
 </ds:Signature>
 <Status><StatusCode Value="samlp:Success"/></Status>
 <Assertion AssertionID="_a75adf55-01d7-40cc-929f-dbd8372ebdfc"
  IssueInstant="2003-04-17T00:46:02Z" Issuer="www.opensaml.org"
  MajorVersion="1" MinorVersion="1" xmlns="urn:oasis:names:tc:SAML:1.0:assertion"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Conditions NotBefore="2003-04-17T00:46:02Z" NotOnOrAfter="2003-04-17T00:51:02Z">
   <AudienceRestrictionCondition>
    <Audience>http://www.opensaml.org</Audience>
   </AudienceRestrictionCondition>
  </Conditions>
  <AuthenticationStatement AuthenticationInstant="2003-04-17T00:46:00Z"
   AuthenticationMethod="urn:oasis:names:tc:SAML:1.0:am:password">
   <Subject>
    <NameIdentifier Format="urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress">scott@example.org</NameIdentifier>
    <SubjectConfirmation>
     <ConfirmationMethod>urn:oasis:names:tc:SAML:1.0:cm:bearer</ConfirmationMethod>
    </SubjectConfirmation>
   </Subject>
   <SubjectLocality IPAddress="127.0.0.1"/>
  </AuthenticationStatement>
  <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
   <ds:SignedInfo>
    <ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
    <ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
    <ds:Reference URI="#_a75adf55-01d7-40cc-929f-dbd8372ebdfc">
     <ds:Transforms>
      <ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
      <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#">
       <InclusiveNamespaces PrefixList="#default saml samlp ds xsd xsi"
        xmlns="http://www.w3.org/2001/10/xml-exc-c14n#"/>
      </ds:Transform>
     </ds:Transforms>
     <ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
     <ds:DigestValue>Kclet6XcaOgOWXM4gty6/UNdviI=</ds:DigestValue>
    </ds:Reference>
   </ds:SignedInfo>
   <ds:SignatureValue>hq4zk+ZknjggCQgZm7ea8fI79gJEsRy3E8LHDpYXWQIgZpkJN9CMLG8ENR4Nrw+n7iyzixBvKXX8P53BTCT4VghPBWhFTSt9tHWu/AtJfOTh6qaAsNdeCyG86jmtp3TDMWuL/cBUj2OtBZOQMFn7jQ9YB7k1Iz3RqVL+wNmeWI4=</ds:SignatureValue>
   <ds:KeyInfo>
    <ds:X509Data>
     <ds:X509Certificate>MIICyjCCAjOgAwIBAgICAnUwDQYJKoZIhvcNAQEEBQAwgakxCzAJBgNVBAYTA1VT ... 8I3bsbmRAUg4UP9hH6ABVq4KQKMknxu1xQxLhpR1y1GPdiowMNTrEG8cCx3w/w==</ds:X509Certificate>
    </ds:X509Data>
   </ds:KeyInfo>
  </ds:Signature>
 </Assertion>
</Response>

So how do I generate something like this? And if I receive something like this, how do I validate it? Also, can anyone offer just a basic conceptual overview of what the <ds:Signature> tags are here? It seems there are two <ds:Signature> tags, one in the main <Response> and one in the <Assertion>, each containing their own <ds:DigestValue>, <ds:SignatureValue>, and <ds:X509Certificate> (and each distinct). How are these generated? Any light you can shed on this will be much appreciated. Tutorials or code examples would be even more appreciated! But at this point, if you can just get me on the right track, that's all I'm really asking for. Right now it all still seems like a big black box to me.

By the way, if this helps, it says elsewhere in the SAML 1.1 spec that SAML implementations should use the "Exclusive Canonicalization" method only (Excl-C14N) and should use the "enveloped transform" only. I'm still not completely sure what that means.

解决方案

Processing XML signatures is not really too difficult, if you are very familiar with XML, but there are a lot of details that have to be absolutely right or things don't work, so I probably wouldn't try writing my own implementation in this situation (I did implement it partially once, but that was for a different and special purpose, and anyway it wasn't a complete implementation).

Anyway, I don't know much about SAML, but I do know about XML and XML Signatures, so maybe I can get you some of the way by trying to answer your questions.

A Signature element refers to a specific piece of an XML document that has been digitally signed, in its SignedInfo child element. The Reference child element of that (I think there can be many Reference elements that get concatenated when forming the bytes to be signed but I don't remember for sure anymore) points to the content through the URI attribute. The Transform elements describe transformations performed on the referred-to content prior to hashing it; you will need to look at the specifications to figure out how the transformation algorithms are defined. The DigestMethod element gives the hash algorithm to apply to the bytes that are the result of these transformation algorithms (note that one of them is always canonicalization that converts XML into bytes), and the DigestValue gives the result of that digest algorithm.

The actual signature is in the SignatureValue element, and is produced by applying the CanonicalizationMethod element's canonicalization to produce the bytes and then signing these bytes with the SignatureMethod. The KeyInfo element tells you how to find the key to use.

Canonicalization, which appears a couple of times above, is simply a way to convert an XML document into bytes so that "equivalent" XML documents produce the same sequence of bytes. This is required in a digital signature because the algorithms work on bytes and XML can pass through a number of intermediaries that probably will disrupt the original bytes but will retain the equivalence. And different canonicalization methods are needed for different situations: if elements are extracted from documents and placed into others, you need exclusive canonicalization that strips away unneeded namespace definitions, but in other cases that might not work correctly, so you need inclusive canonicalization instead, which preserves all in-scope namespaces.

This is just the basics. There are a number of different options in how to produce an XML signature, and if you want to implement a working verifier, you need to consider all of them. Since you are new to XML in general, I'll just repeat my advice of using something that already exists. It's an interesting learning experience to implement a specification, but often it's a waste of time if implementations are already available.

There is the W3C's documentation about signatures.

这篇关于为了使SAML正常运行,我需要了解什么有关XML签名的信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆