【问题标题】:Remove seconds from message timestamp to when sending signed request从消息时间戳到发送签名请求时删除秒数
【发布时间】:2014-02-03 20:23:58
【问题描述】:

我有一个使用 TransportSecurityBindingElement 安全元素的 WCF 自定义绑定,但客户端和(第三方)服务器上的时间准确性一直存在问题。

如何删除秒以使时间戳仅精确到分钟(我被告知服务器会接受这一点)。

我的替代想法是在每次请求之前更新系统时间,但是这假设(错误地)服务器时间是准确的。我也尝试完全删除时间戳(可能不需要),但我得到一个 System.InvalidOperationExceptionSigning without primary signature requires timestamp.

.Net 代码构建安全元素:

    Dim msgSecVer As System.ServiceModel.MessageSecurityVersion = ServiceModel.MessageSecurityVersion.WSSecurity10WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10

    Dim tsbe As TransportSecurityBindingElement = SecurityBindingElement.CreateCertificateOverTransportBindingElement(msgSecVer)
    tsbe.EnableUnsecuredResponse = True
    tsbe.SetKeyDerivation(False)
    tsbe.AllowInsecureTransport = True
    tsbe.IncludeTimestamp = True

    'adding clock skew doesn't seem to make any difference?
    Dim clockSkew As TimeSpan = TimeSpan.FromMinutes(1)
    tsbe.LocalClientSettings.MaxClockSkew = clockSkew
    tsbe.LocalServiceSettings.MaxClockSkew = clockSkew

    Return tsbe

消息头,注意(可能超出)时间戳的准确性:

POST http://wwwqa.xxxx.com/services/
Content-Type: text/xml; charset=utf-8
VsDebuggerCausalityData: xxxx
SOAPAction: ""
Host: wwwqa.xxxx.com
Content-Length: 2400
Expect: 100-continue
Connection: Keep-Alive

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
    <s:Header>
        <o:Security s:mustUnderstand="1" xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
            <u:Timestamp u:Id="_0">
                <u:Created>2013-12-04T10:53:13.568Z</u:Created>
                <u:Expires>2013-12-04T10:58:13.568Z</u:Expires>
            </u:Timestamp>
            <o:BinarySecurityToken u:Id="uuid-bc441202-xxxx-xxxx-a176-02f2a61a6002-1" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3">....xxxx....</o:BinarySecurityToken>
            <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
                <SignedInfo>
                    <CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
                    <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
                    <Reference URI="#_0">
                        <Transforms>
                            <Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
                        </Transforms>
                        <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
                        <DigestValue>xxxx</DigestValue>
                    </Reference>
                </SignedInfo>
                <SignatureValue>xxxx</SignatureValue>
                <KeyInfo>
                    <o:SecurityTokenReference><o:Reference URI="#uuid-bc441202-xxxx-xxxx-a176-02f2a61a6002-1"/></o:SecurityTokenReference>
                </KeyInfo>
            </Signature>
        </o:Security>
    </s:Header>

【问题讨论】:

  • 我没有,但现在我添加了时钟 Skew,但它似乎对发送的消息时间戳没有任何影响。 (我看不出有什么不同,我做错了什么吗?(​​有问题的更新代码)。
  • 时间不正确时在哪里失败,删除时间戳时你的InvalidOperationException在哪里抛出?
  • 如果服务是第 3 方并且您无法控制它,时钟偏差将不起作用。客户端和服务器都必须就一个共同的偏差时钟达成一致。 BTW 默认值为 5 分钟。因此,服务器必须故意将其设置为较低的值,以防止重放攻击。
  • @Zache,调用服务时失败(对于时间戳),服务有错误,并且当我去调用服务时引发了 InvalidOp(发送消息之前的.net 错误) .

标签: .net wcf httprequest wcf-client custom-binding


【解决方案1】:

如果您使用 IIS 运行 WCF 主机,您可以添加一个自定义编写的模块 - 一个 .NET DLL - 在 IIS 中拦截消息,读取、解析并删除秒数,然后将消息传递给您的WCF 服务。

我记得,模块程序会拾取并读取 HTTPContext,让您可以随意更改消息,甚至可以在标题和正文部分添加全新的字段。

但这种方法只有在您使用 IIS 来管理或管理您的 Web 服务时才有效。

这是一个链接:http://www.iis.net/learn/get-started/introduction-to-iis/iis-modules-overview#Querying

【讨论】:

  • 这是第三方服务,所以我无法控制该服务。
【解决方案2】:

我认为完成它的唯一方法是在您的客户端中移动到仅传输安全,然后在custom encoder 中自己添加安全标头。生成安全头包括:

  1. 生成时间戳并将其推送到 SOAP(简单)。
  2. 生成签名(硬)。请参阅here,GetSignature 评论了我们的方法here。然后将其推送到 SOAP。

要操作 SOAP,请实现编码器,并在可以访问消息的 WriteMessage 方法中将其加载到 XmlDocument 并对其进行操作(例如,添加标头)。

【讨论】:

  • 谢谢亚龙,我会试试看的。对不起,我一直在度假,所以错过了赏金。
猜你喜欢
  • 2017-12-21
  • 1970-01-01
  • 2017-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-18
相关资源
最近更新 更多