Security Questions
In order to ensure the universality of the service, the security solution provided by WCF is based on the security standard of public standard, such as X.509 of ITU-T. Compliance with public security specifications can ensure the universality of WCF, but sometimes these general security methods are not suitable for us. In some simple scenarios, we just want to add a secret to the message, and it seems too heavy to use the X.509 Certificate.
If you want to get the certificate, the legal certificate is of course the best. However, it is costly to obtain a valid X.509 Certificate, and you must pay the certificate every year. Few customers will be happy. There are also a lot of problems with using an invalid X509 Certificate. First, you need to install the certificate on the server and find a proper way to distribute the certificate to the client, the installation of the certificate will bring about additional configuration work. Even if the installation and configuration are not a problem, using a false certificate in Silverlight will be very troublesome. net client, at least system. net. servicepointmanager. the servercertificatevalidationcallback attribute allows you to selectively trust your own fake certificates. However, Silverlight does not provide this mechanism. It is too secure and is a headache.
In order to avoid the certificate to implement the WCF message encryption available for Silverlight, it seems that you have to do it yourself. WCF provides a wide range of extended mechanisms (refer to: http://msdn.microsoft.com/zh-cn/library/ms733848) through custom message encoder (reference: http://msdn.microsoft.com/zh-cn/library/ms751486 ), you can add your own encryption and decryption operations to the message encoding and decoding process. Note that this practice will undermine the universality of services. You cannot publish such services to the public, because they do not know the message format of your services, but here, our services serve our own clients, but we expect this effect.
The next step is to design your own encryption and decryption.AlgorithmNow, it seems that this is not difficult to hear. In fact, it is not very simple. Because the processing target is a message transmitted over the network, the following things need to be considered:
1. Confidentiality. In addition to the sender and receiver, others should not understand the message.
2. Integrity. If a message has been tampered with during transmission, it must be recognized and rejected.
3. Anti-replay. If a message is recorded and resending as is, it must be recognized and rejected.
4. Identity Authentication. Who sent the transmitted message.
5. Anti-credit. Message senders cannot deny that they have sent a message.
6. out of order. The order of messages is rearranged by others or the message sending time is changed.
7. Behavior Analysis. Analyze features such as Message Size, time period, target, and frequency to determine and understand the meaning of the message.
8. DoS attacks. Sending a large number of malicious messages can overload the server or network, or directly damage the server or network facilities.
Because it is not an important scenario such as banking and military affairs, we do not consider Denial-of-Service, behavior analysis, and out-of-order issues. Identity Authentication and anti-denial can also be relaxed. However, confidentiality, integrity, and anti-replay must be ensured. If these three cannot be ensured, we can consider that this encryption mechanism is not available at all.
Another extra problem is that the custom encoder must be able to process excessive data through configuration. In actual applications, it is not uncommon for the WCF data to exceed the default size.
The encrypted content can solve the confidentiality problem. Here we use a common symmetric key for encryption and use the hash value of the System user password as the key, because only the user and the server know the hash value, therefore, you do not need to transmit keys over the network to reduce the risk of key leakage.
The content Digest can be used as a signature to solve the integrity problem. Here, the key hashing algorithm is used to calculate the Digest.ProgramTo ensure that the signature is not forged by other users.
A time window is created. messages that are not within the time window range are not processed. messages within the time window range are recorded, and duplicate messages that are checked based on records are not processed. This can solve the replay problem.
The specific algorithm flow is documented in the example in this article. Here we only talk about how to reuse the example in this article.
The examples in this article are somewhat different from those given by Microsoft. Microsoft's example encapsulates the encoding and decoding logic in a set of programs for reference by the server and the client. However, the client in this article is Silverlight, which cannot reference common code.. Net class library, so no DLL is encapsulated, which is written on the server and client respectively.Code. This method is more adaptive and can be used in non-Silverlight General scenarios.
Install a custom encoder on the server:
1. Copy customtextencoder. CS, customtextencoderbindingelement. CS, customtextencoderbindingelementsection. CS, and customtextencoderfactory. CS in the sample server to the program, and register the custom encoder in the program configuration file:
<System. servicemodel> <extensions> <bindingelementextensions> <Add name = "customtextcodeencoding" type = "silverlightwcfexample. web. customtextencoderbindingelementsection, silverlightwcfexample. web "/> </bindingelementextensions> </extensions> </system. servicemodel>
Change the namespace and set name according to the actual situation.
2. Create a custom binding:
<Custombinding> <binding name = "constraint" closetimeout = "00:10:00" opentimeout = "00:10:00" receivetimeout = "00:10:00" sendtimeout = "00:10:00"> <customtextcodeencoding maxreadpoolsize = "64000" maxwritepoolsize = ""16000"> <readerquotas maxarraylength = "16384000" maxbytesperread = "4096000" maxdepth = "32000" maxnametablecharcount = "16384000" maxstringcontentlength = "8192000"/> </strong>
The custom encoder in this article supports configuring readerquotas and other parameters to support big data and long-time services.
3. Modify the service configuration and set binding and bindingconfiguration as custom items.
<Service name = "silverlightwcfexample. web. services. sqldataservice "> <endpoint address =" "binding =" custombinding "bindingconfiguration =" customtextbinding_messagesecurityservice "Contract =" silverlightwcfexample. web. services. sqldataservice "/> <endpoint address =" mex "binding =" mexhttpbinding "Contract =" imetadataexchange "/> </service>
The server transformation has been completed.
Use a custom encoder on the client. For common. Net clients, refer to the Microsoft sample configuration. The Silverlight client is introduced here. Because Silverlight does not support some configurations, it may be used differently:
1. Copy the example clients customtextencoder. CS, customtextencoderbindingelement. CS, and customtextencoderfactory. CS to the program. You can find that Silverlight does not support customtextencoderbindingelementsection. CS.
2. reference the service on the server like a common service to generate code and configuration items. To improve service adaptability, you can increase the opentimeout equivalent:
<Custombinding> <binding name = "binding" closetimeout = "00:10:00" opentimeout = "00:10:00" receivetimeout = "00:10:00" sendtimeout = "00:10:00"> <textmessageencoding/>
3. In the configuration items generated by Silverlight, the encoding uses textmessageencoding. We cannot change it through the configuration. Therefore, we need to replace the Code with our own encoder when calling the service. Encapsulate the code for replacing the encoder in the following method:
Internal static class wcfservicehelper {internal static void replaceendpoint (serviceendpoint endpoint) {bindingelementcollection bindingcollection = endpoint. binding. createbindingelements (); bindingcollection [0] = new customtextencoderbindingelement (); binding = new custombinding (bindingcollection); endpoint. binding = binding ;}}
4. the reference of all services must be handled in the previous step before calling the service:
Sqldataserviceclient client = new sqldataserviceclient (); wcfservicehelper. replaceendpoint (client. endpoint); client. dosomethingasync ();
At this point, the SSL-free message encryption of the WCF for Silverlight has been completed. The above method is not elegant, but at least it is effective. If you have a better method, please advise. Thank you.
Example: http://download.csdn.net/detail/wantalcs/4494183