Skip to main content
 首页 » 编程设计

c#之WCF之实现需要加密一个 soap :body element, 的客户端如何

2024年02月23日14birdshome

我一直在寻找这个,到目前为止它看起来并不乐观,试图编写一个使用第 3 方服务的客户端应用程序(下面的示例请求,来自 java 客户端)。一个陷阱是它期望正文中的一个元素被加密。

理想情况下,这将是一个 WCF 解决方案,尽管我已经阅读了一些关于使用 WSE1/2/3 来接近这个的地方(并不是真正想要的,因为 WSE 停滞不前),有没有人必须解决这类问题?

<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://editedURL"> 
  <soap:Header> 
    <wsse:Security soap:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"> 
      <wsu:Timestamp xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"  
            wsu:Id="Timestamp-2d960151-fc35-4522-a8cd-b463025209d8"> 
        <wsu:Created>2010-11-23T16:17:36Z</wsu:Created> 
      </wsu:Timestamp> 
      <wsse:BinarySecurityToken ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3"  
            EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary"  
            xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"  
            wsu:Id="SecurityToken-68e70781-8ff5-4e08-b066-8b9c6badef37"><!--token was here --></wsse:BinarySecurityToken> 
      <xenc:EncryptedKey xmlns:xenc="http://www.w3.org/2001/04/xmlenc#"> 
        <xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5"/> 
        <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#"> 
          <wsse:SecurityTokenReference> 
            <wsse:Reference URI="#SecurityToken-68e70781-8ff5-4e08-b066-8b9c6badef37"  
            ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3"/> 
          </wsse:SecurityTokenReference> 
        </KeyInfo> 
        <xenc:CipherData> 
          <xenc:CipherValue><!--cypher was here --></xenc:CipherValue> 
        </xenc:CipherData> 
        <xenc:ReferenceList> 
          <xenc:DataReference URI="#Enc-5293b4f0-9aa5-4c37-ad1c-7c55c7d9ded1"/> 
        </xenc:ReferenceList> 
      </xenc:EncryptedKey> 
    </wsse:Security> 
  </soap:Header> 
  <soap:Body> 
    <tns:Authenticate> 
      <authIn> 
        <authID><!-- user id was here plaintext --></authID> 
        <authPwd wsu:Id="Id-8bac7fa6-fac9-4ce4-8a5e-3b221c64ca76"  
                xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"> 
          <xenc:EncryptedData Id="Enc-5293b4f0-9aa5-4c37-ad1c-7c55c7d9ded1" Type="http://www.w3.org/2001/04/xmlenc#Content"  
                                mlns:xenc="http://www.w3.org/2001/04/xmlenc#"> 
            <xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes128-cbc"/> 
            <xenc:CipherData> 
              <xenc:CipherValue><!-- encrypted password was here --></xenc:CipherValue> 
            </xenc:CipherData> 
          </xenc:EncryptedData> 
        </authPwd> 
        <authServer><!-- domain was here plaintext --></authServer> 
      </authIn> 
    </tns:Authenticate> 
  </soap:Body> 
</soap:Envelope> 

痛点是 soap:Body 中的 authPwd 元素,感谢您的任何见解

请您参考如下方法:

有个坏消息要告诉你。开箱即用的安全实现 is not able to do this并且我不确定如何扩展 WCF 以支持它,除非您从头开始编写所有 WS-Security 内容。 WCF 是非常可扩展的,除非您想扩展已经实现的协议(protocol)——这些类大多是密封的/内部的,不能重用。

如果这些元素是 SOAP header 而不是正文元素,那么整个问题就会完全不同。

编辑:

我再次检查了您提供的整个 SOAP 消息,我发现了另外两个问题。

  • 我怀疑你们服务提供的安全级别。它不显示明文密码,但可以是全部。隐藏密码不仅是您需要的安全措施,因为如果没有正确实现安全措施,攻击者可以简单地获取您的整个消息(按原样加密)并再次从他的计算机发送。如果服务配置不正确,它将像处理任何其他请求一样处理该请求。这称为重放攻击,针对这种攻击的主要防御措施是时间戳,但由于您的消息未签名,攻击者可以根据需要更改时间戳值。 EncryptedKey 元素中可以有额外的防御机制,但从消息本身很难说。
  • 即使服务对整个正文进行加密,WCF 也很可能无法处理该消息。开箱即用的安全性需要签名——时间戳必须签名(由于上述原因),消息部分可以签名或加密并签名,但不仅要加密。签署加密部分将确保攻击者不会用另一个替换发布的数据。