如何在C#中为SOAP消息配置符合WS-Security要求的SHA2签名
问题描述
第三方对接方提出的强制安全要求如下:
涉及个人信息及其他敏感数据的交易需采用传输层安全保护。Web服务消息通过HTTPS(基于SSL的HTTP)传输,且必须符合Web服务安全(WS-Security)v1.1标准。服务消息的WS-Security部分必须满足以下条件:
- 使用2048位密钥长度的x.509证书进行签名
- 采用SHA2结合RSA算法完成加密
- 使用C14N规范化
现有实现逻辑
目前已写出初步的消息签名代码,可正常和服务完成基础连通,但存在签名算法不匹配的问题,核心实现代码如下:
someServiceRef.widjetClient client = null; try { X509Certificate2 signingCert = GetSigningCert(); var bindings = new BasicHttpsBinding(); bindings.Security.Mode = BasicHttpsSecurityMode.TransportWithMessageCredential; bindings.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.Certificate; client = new someServiceRef.widjetClient( bindings, new EndpointAddress(@"<URL OF SERVICE>")); client.ClientCredentials.ClientCertificate.Certificate = signingCert; client.ClientCredentials.ServiceCertificate.DefaultCertificate = signingCert; client.Open(); var request = BuildRequest(); var response = client.SayHello(request); Console.WriteLine(response); } finally { if (client != null) { if (client.State == System.ServiceModel.CommunicationState.Faulted) client.Abort(); else client.Close(); } }
待解决问题
当前代码实际生成的消息签名使用SHA1算法,不符合要求的SHA2标准。公开可查的实现方案大多需要先生成完整SOAP消息,再通过XML解析手动修改、新增节点,这类实现复杂且维护成本高。期望能通过纯框架配置的方式,让WCF自动生成符合第三方要求的签名格式,无需手动操作XML节点。
对比第三方提供的标准签名样例可以发现,默认使用的BasicHttpsBinding没有暴露修改签名算法的配置项,无法直接调整算法参数到要求的标准。
第三方提供的合法请求签名结构参考:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> <SOAP-ENV:Header> <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" SOAP-ENV:mustUnderstand="1"> <wsse:BinarySecurityToken xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" 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/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3" wsu:Id="XWSSGID-12324774331131695995061">XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</wsse:BinarySecurityToken> <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-sha2" /> <ds:Reference URI="#XWSSGID-1232477437326-1352495766"> <ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha2" /> <ds:DigestValue>XXXXXXXXXXXXXXXXXXXXXX</ds:DigestValue> </ds:Reference> <ds:Reference URI="#XWSSGID-1232477437326-823787906"> <ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha2" /> <ds:DigestValue>XXXXXXXXXXXXXXXXXXXXX</ds:DigestValue> </ds:Reference> </ds:SignedInfo> <ds:SignatureValue>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</ds:SignatureValue> <ds:KeyInfo> <wsse:SecurityTokenReference xmlns:wsse="http://www.w3.org/2000/09/xmldsig#" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsse:Id="XWSSGID-1232477437311698965010"> <wsse:Reference URI="#XWSSGID-12324774331131695995061" /> <ds:X509Data> <ds:X509IssuerName>XXXXXXXXXXXXXXXXXXXXXXXXX</ds:X509IssuerName> <ds:X509SerialNumber>XXXXXXXXXXXXXXXXXXX</ds:X509SerialNumber> </ds:X509Data> </wsse:SecurityTokenReference> </ds:KeyInfo> </ds:Signature> <wsu:Timestamp xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="XWSSGID-1232477437326-823787906"> <wsu:Created>2009-01-20T18:50:37.233Z</wsu:Created> <wsu:Expires>2009-01-20T18:50:42.233Z</wsu:Expires> </wsu:Timestamp> </wsse:Security> </SOAP-ENV:Header> <SOAP-ENV:Body xmlns:SOAP-ENV="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="XWSSGID-1232477437326-1352495766"> BODY OF MESSAGE GOES HERE </SOAP-ENV:Body> </SOAP-ENV:Envelope>
配置实现方案
无需手动解析修改SOAP XML,通过WCF自定义绑定调整安全算法套件,即可让框架自动生成符合要求的签名内容。问题核心在于BasicHttpsBinding默认使用的算法套件固定采用SHA1,需要替换为支持SHA2的算法配置,具体调整方式:
- 替换原有默认
BasicHttpsBinding的初始化逻辑为自定义绑定,明确指定SHA2相关的加密参数
// 替换原有BasicHttpsBinding的初始化代码 var customBinding = new CustomBinding(); // 配置HTTPS传输层安全 var httpsTransport = new HttpsTransportBindingElement(); customBinding.Elements.Add(httpsTransport); // 配置消息层WS-Security参数,适配第三方要求 var securityElement = SecurityBindingElement.CreateCertificateOverTransportBindingElement(); // 指定WS-Security 1.1版本,匹配对接方标准 securityElement.MessageSecurityVersion = MessageSecurityVersion.WSSecurity11WSTrust13WSSecureConversation13WSSecurityPolicy12BasicSecurityProfile10; // 核心配置:替换默认SHA1套件为支持SHA256的算法套件 securityElement.DefaultAlgorithmSuite = SecurityAlgorithmSuite.Basic256Sha256; customBinding.Elements.Add(securityElement);
- 其余客户端初始化、证书配置的逻辑保持原有代码不变,框架自动生成的签名内容会完全匹配第三方要求:
- 自动基于2048位X.509证书生成BinarySecurityToken
- 签名算法自动使用RSA+SHA2组合,对应标准样例中定义的
rsa-sha2算法URI - 自动启用兼容C14N的规范化输出,匹配
xml-exc-c14n#的格式要求 - 自动在SOAP头中生成结构完整的Security节点,包含时间戳、签名值、证书引用等内容,与第三方提供的样例结构完全一致
内容的提问来源于stack exchange,提问作者scottDeveloper
相关产品推荐
相关产品推荐

