当前位置:编程学习 > 网站相关 >>

[WCF安全系列]通过绑定元素看各种绑定对消息保护的实现

对消息进行签名和加密分别解决了消息的一致性和机密性问题。而最终是仅仅采用签名还是签名与加密共用取决于契约中对消息保护级别的设置。但是具体的签名和加密在整个WCF框架体系中如何实现?是采用对称加密还是非对称加密?密钥如何而来?相信这些问题在本篇文章中你会找到答案。

目录
一、BasicHttpBinding
二、WSHttpBinding、WS2007HttpBinding和WSDualHttpBinding
三、NetTcpBinding和NetNamedPipeBinding
四、NetMsmqBinding
五、总结
在本系列中我不断在强调这么一个要点:整个安全传输的实施最终是在信道层实现的。而信道层是根绝终结点绑定创建的,而绑定从结构上是一系列绑定元素的有序集合。当绑定的安全开启的时候,决定最终安全传输实现方式的必然是某一个或者多个绑定元素。了解相关绑定元素可以帮助读者从本质上理解安全传输实现原理。

为了演示方便,我写了如下一个针对Binding类型的扩展方法ListAllBindingElements,该方法会将绑定所有的绑定元素的类型打印出来。接下来,我们就利用这个扩展方法应用了那些常见的绑定,看看最终决定安全传输的是哪些绑定元素。

   1: public static class BindingExtension  

 2: {  

3:     public static void ListAllBindingElements(this Binding binding)  

4:     {  

5:         int i = 0; 

  6:         foreach (var bindingElement in binding.CreateBindingElements())   7:         {   8:             Console.WriteLine(" {0}.{1}", ++i, bindingElement.GetType().FullName);   9:         }  10:     }  11: }


一、BasicHttpBinding
我们先来看看对于三种典型安全模式(Transport、Message和Mixed)下的BasicHttpBinding具体由哪些绑定元素构成,为了我编写了如下的程序。

   1: BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);   2: Console.WriteLine("Transport:");   3: binding.ListAllBindingElements();   4:     5: binding = new BasicHttpBinding(BasicHttpSecurityMode.Message);   6: binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.Certificate;   7: Console.WriteLine("Message:");   8: binding.ListAllBindingElements();   9:    10: binding = new BasicHttpBinding(BasicHttpSecurityMode.TransportWithMessageCredential);  11: Console.WriteLine("Mixed:");  12: binding.ListAllBindingElements();
输出结果:


   1: Transport:   2:         1.System.ServiceModel.Channels.TextMessageEncodingBindingElement   3:         2.System.ServiceModel.Channels.HttpsTransportBindingElement   4: Message:   5:         1.System.ServiceModel.Channels.AsymmetricSecurityBindingElement   6:         2.System.ServiceModel.Channels.TextMessageEncodingBindingElement   7:         3.System.ServiceModel.Channels.HttpTransportBindingElement   8: Mixed:   9:         1.System.ServiceModel.Channels.TransportSecurityBindingElement  10:         2.System.ServiceModel.Channels.TextMessageEncodingBindingElement  11:         3.System.ServiceModel.Channels.HttpsTransportBindingElement
我们来具体分析一下最终在不同安全模式下输出的绑定元素列表。对于Mixe安全模式下对服务的验证、消息签名和加密都是基于Transport安全,Message安全仅仅用于对客户端的认证。所以对于Transport和Mixed模式,消息保护都是通过HttpsTransportBindingElement来实现。从名称就可以看出来,这是一个基于HTTPS的传输绑定元素,这也再次印证了BasicHttpBinding通过HTTPS实现Transport安全模式的说法。

对于Message安全模式的三个绑定元素中,很明显和安全传输相关的是AsymmetricSecurityBindingElement。从名称我们就知道,该绑定元素通过非对称加密的方式提供签名和加密的实现。具体来说,对于请求消息来说,发送方使用自己的私钥对消息进行签名,使用接收方的公钥对消息进行加密。接收方采用发送方的公钥验证签名,用自己的私钥对消息进行解密。这也是为什么在选择了Message安全模式的情况下,基于用户名/密码的客户端凭证不被支持的真正原因。

二、WSHttpBinding、WS2007HttpBinding和WSDualHttpBinding
我们按照相同的方式来分析基于WS的绑定,由于WSHttpBinding和WS2007HttpBinding仅仅在实现WS-*协议上面有所不同之外,整个设计基本相同。所以我们仅仅分析WS2007HttpBinding和WSDualHttpBinding。首先来分析WS2007HttpBinding,我们对前面的分析程序略加修改,将绑定类型替换成WS2007HttpBinding。

   1: WS2007HttpBinding binding = new WS2007HttpBinding(SecurityMode.Transport);   2: Console.WriteLine("Transport:");   3: binding.ListAllBindingElements();   4:     5: binding = new WS2007HttpBinding(SecurityMode.Message);   6: Console.WriteLine("Message:");   7: binding.ListAllBindingElements();   8:     9: binding = new WS2007HttpBinding(SecurityMode.TransportWithMessageCredential);  10: Console.WriteLine("Mixed:");  11: binding.ListAllBindingElements();
输出结果:


   1: Transport:   2:         1.System.ServiceModel.Channels.TransactionFlowBindingElement   3:         2.System.ServiceModel.Channels.TextMessageEncodingBindingElement   4:         3.System.ServiceModel.Channels.HttpsTransportBindingElement   5: Message:   6:         1.System.ServiceModel.Channels.TransactionFlowBindingElement   7:         2.System.ServiceModel.Channels.SymmetricSecurityBindingElement   8:         3.System.ServiceModel.Channels.TextMessageEncodingBindingElement   9:         4.System.ServiceModel.Channels.HttpTransportBindingElement  10: Mixed:  11:         1.System.ServiceModel.Channels.TransactionFlowBindingElement  12:         2.System.ServiceModel.Channels.TransportSecurityBindingElement  13:         3.System.ServiceModel.Channels.TextMessageEncodingBindingElement  14:         4.System.ServiceModel.Channels.HttpsTransportBindingElement
WS2007HttpBinding和BasicHttpBinding实现Transport安全都是基于HTTPS,所以在对于Transport和Mixed安全模式,HttpsTransportBindingElement实现了对消息的签名和加密。而对于Message安全模式下包含的四个绑定元素,我们可以看出最终和安全传输相关的是一个叫做SymmetricSecurityBindingElement。从名称我们都可以猜出,SymmetricSecurityBindingElement采用了对称加密的实现了对消息的签名和加密。这就意味着,客户端和服务在进行正式的功能性消息交换之前,会相互协商生成一个仅限于双方知道的密钥。

接着来看用户双向通信的WSDualHttpBinding。通过前面的接收,我们已经知道了该邦绑定仅仅支持Message安全模式。我们同样调用ListAllBindingElements扩展方法列出WSDualHttpBinding在Message安全模式下的所有绑定元素。

   1: WSDualHttpBinding binding = new WSDualHttpBinding(WSDualHttpSecurityMode.Message);   2: Console.WriteLine("Message:");   3: binding.ListAllBindingElements();
输出结果:

   1: Message:   2:         1.System.ServiceModel.Channels.TransactionFlowBindingElement   3:     &nbs

补充:综合编程 , 安全编程 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,