STR-Transform 是如何工作的?

How does STR-Transform works?

使用 WS Security 时,STR-Transform 转换算法如何用于 XML 签名?我需要对用于 SOAP 消息签名的 SecurityTokenReference 进行签名,这是安全令牌所需的转换。我正在使用 x509 证书进行签名,所以安全令牌就是这个证书。但是,在消息中我只需要对证书指纹的引用。

这是我需要复制的签名结构,唯一缺少的是对 SecurityTokenReference 的签名引用:

<dsig:Signature xmlns:dsig="http://www.w3.org/2000/09/xmldsig#">
  <dsig:SignedInfo>
    <dsig:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
    <dsig:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
    <dsig:Reference URI="#Timestamp_C1Ih1AB1vpPT5uG2">
      <dsig:Transforms>
        <dsig:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
      </dsig:Transforms>
      <dsig:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
      <dsig:DigestValue>fVSyToUO8yS131cV8oT1h6fa69Jvtt+pKFeP4BFf1P4=</dsig:DigestValue>
    </dsig:Reference>
    <!-- Other signature references -->
    <dsig:Reference URI="#str_U1sjQ5j8JtKnObLk">
      <dsig:Transforms>
        <dsig:Transform Algorithm="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#STR-Transform">
          <wsse:TransformationParameters>
            <dsig:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
          </wsse:TransformationParameters>
        </dsig:Transform>
      </dsig:Transforms>
      <dsig:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
      <dsig:DigestValue>gRa3zakGn13XISoKpekB3zl0iDqb/LmNy7+aMDtzKIY=</dsig:DigestValue>
    </dsig:Reference>
  </dsig:SignedInfo>
  <dsig:SignatureValue>ptO...E9Q==</dsig:SignatureValue>
  <dsig:KeyInfo>
    <wsse:SecurityTokenReference
                    xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
                    xmlns:wsse11="http://docs.oasis-open.org/wss/oasis-wss-wssecurity-secext-1.1.xsd"
                    xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
                    wsse11:TokenType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3"
                    wsu:Id="str_U1sjQ5j8JtKnObLk">
      <wsse:KeyIdentifier
                        EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary"
                        ValueType="http://docs.oasis-open.org/wss/oasis-wss-soap-message-security-1.1#ThumbprintSHA1">h5...ow=</wsse:KeyIdentifier>
    </wsse:SecurityTokenReference>
  </dsig:KeyInfo>
</dsig:Signature>

谁能解释一下如何为这样的令牌签名?算法的逐步描述,或使用任何 language/library 的示例都很好。

In this document is the description of the transformation,从第 38 页开始,但我无法理解如何在实践中应用它。

好的,在检查了 Oracle 的 WebLogic 服务器调试和具有工作服务示例的详细日志文件并设置标志后 -Dweblogic.xml.crypto.dsig.debug=true -Dweblogic.xml.crypto.dsig.verbose=true -Dweblogic.xml.crypto.keyinfo.debug=true -Dweblogic.xml.crypto.keyinfo.verbose=true -Dweblogic.wsee.verbose=* -Dweblogic.wsee.debug=*(更多信息 here, here, here and here),谢天谢地,我对安全令牌是如何取消引用的。基本上,具有 SecurityTokenReference 的 x509 证书和 KeyIdentifier 以这种方式被取消引用为 BinarySecurityToken

<wsse:BinarySecurityToken xmlns="" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3">CertificateBase64String</wsse:BinarySecurityToken>

需要注意的一些重要事项是:

  • ValueType 以及 BinarySecurityToken 的内容由 SecurityTokenReferenceTokenType 定义。在这种情况下,BinarySecurityToken 的文本是由 KeyIdentifier 元素引用的 x509 证书,编码为 base64 字符串。
  • 根据规范,BinarySecurityToken 仅包含 ValueType 属性。因此它不应包含 EncodingType 属性,也不应包含 SecurityTokenReference 具有的 Id 属性。
  • 使用了与 SecurityTokenReference 相同的命名空间前缀。此外,此前缀的命名空间包含在标记中。
  • 默认命名空间属性设置为空:xmlns=""

所以基本上整个 SecurityTokenReference 元素被新的 BinarySecurityToken 替换,这是要规范化和散列的元素(以获得它的摘要值)。请注意,它是按原样规范化和消化的,因此如果通过删除空的 xmlns 命名空间或前缀命名空间,或通过更改命名空间前缀来简化 XML,操作可能会提供错误的结果。

示例 BinarySecurityToken 已经使用算法“http://www.w3.org/2001/10/xml-exc-c14n#", so in .NET, to get the DigestValue using the digest algorithm "http://www.w3.org/2001/04/xmlenc#sha256”规范化,足以做到这一点:

System.Security.Cryptography.SHA256 sha = System.Security.Cryptography.SHA256.Create();
byte[] hash = sha.ComputeHash(Encoding.UTF8.GetBytes("<wsse:BinarySecurityToken xmlns=\"\" xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\" ValueType=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3\">MIIF...2A8=</wsse:BinarySecurityToken>"));
string digestValue = Convert.ToBase64String(hash);