【问题标题】:How do I use RSA-SHA512 as signing algorithm in defaultAlgorithmSuite in custom bindings?如何在自定义绑定的 defaultAlgorithmSuite 中使用 RSA-SHA512 作为签名算法?
【发布时间】:2018-10-29 13:17:41
【问题描述】:

我按照以下方式签署 XML: 签名算法:http://www.w3.org/2001/04/xmldsig-more#rsa-sha512摘要算法:http://www.w3.org/2001/04/xmlenc#sha512规范化算法:http://www.w3.org/2001/10/xml-exc-c14n#

但我未能在 WCF 中找到对 SHA512 的支持。有解决办法吗?

下面是我的代码:

<customBinding>
    <binding name="McBinding">
         <textMessageEncoding messageVersion="Soap11" />
        <security authenticationMode="MutualCertificate" includeTimestamp="false" defaultAlgorithmSuite="??"
            messageSecurityVersion="WSSecurity10WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10">
            <secureConversationBootstrap />
        </security>
        <httpsTransport />
    </binding>

【问题讨论】:

    标签: c# wcf soap


    【解决方案1】:

    我遇到的问题是不支持 RSA-SHA512 签名算法。它可以通过使用自定义签名算法来实现。然后将其添加到自定义绑定中的默认签名算法。

     public class MyCustomAlgorithmSuite : SecurityAlgorithmSuite { }
    

    还为此创建签名:

    public class RsaPkCs1Sha512SignatureDescription : SignatureDescription
    {}
    

    然后将其添加到:

    CryptoConfig.AddAlgorithm(typeof(RsaPkCs1Sha512SignatureDescription),
                "http://www.w3.org/2001/04/xmldsig-more#rsa-sha512");
    

    但是,我们在签署 BinarySecurity 令牌时遇到了问题。可以通过添加来签名:

    assymetricKey.EndpointSupportingTokenParameters.Signed.Add(new X509SecurityTokenParameters());
    

    但是,这会创建一个新的签名二进制安全令牌,您最终会得到两个 BST。解决方法是:

                assymetricKey.InitiatorTokenParameters = new System.ServiceModel.Security.Tokens.X509SecurityTokenParameters { InclusionMode = SecurityTokenInclusionMode.Never};
            assymetricKey.RecipientTokenParameters = new System.ServiceModel.Security.Tokens.X509SecurityTokenParameters { InclusionMode = SecurityTokenInclusionMode.Never};
    

    这会扰乱您的 SignedInfo 以失去对 BST 的引用,我们陷入困境。

    无论如何,我们最终没有使用 WCF 功能,而是使用了:

    SignedXML class.
    

    这里是如何使用它的参考链接: https://gist.github.com/luizvaz/43ccbd85b16b6802218b50b6d34c26de

    另外,这里要注意的是,如果您从证书实现签名算法 RSA-SHA512,那么您将需要使用扩展方法(确保使用 .net 4.6.2 或更高版本)。对于旧版本:

    SignedXml Compute Signature with SHA256

    signedXml.SigningKey = RSACertificateExtensions.GetRSAPrivateKey(cert);
    

    另外,如果您要为签名添加前缀为ds,那么您需要从SignedInfo 中删除引用,重新计算签名并将其添加回来。

    Generate Digital Signature but with a Specific Namespace Prefix ("ds:")

    我希望这有助于解决类似问题。

    【讨论】:

    • 很棒的回复。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-12
    • 2011-06-01
    • 1970-01-01
    相关资源
    最近更新 更多