1 | //#define USE_MSG_BINDING
|
---|
2 | using System;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using System.Linq;
|
---|
5 | using System.Text;
|
---|
6 | using System.ServiceModel.Channels;
|
---|
7 | using System.ServiceModel;
|
---|
8 | using System.Security.Cryptography.X509Certificates;
|
---|
9 | using System.Net;
|
---|
10 |
|
---|
11 | namespace HeuristicLab.Hive.Contracts {
|
---|
12 | public static class WcfSettings {
|
---|
13 |
|
---|
14 | public const string SERVERCERT = "HIVE-Server";
|
---|
15 | public const int DEFAULTPORT = 9000;
|
---|
16 |
|
---|
17 | /// <summary>
|
---|
18 | /// Gets a pre-defined binding using TCP for secure transport.
|
---|
19 | /// </summary>
|
---|
20 | /// <returns>A binding type of <see cref="NetTcpBinding"/></returns>
|
---|
21 | public static Binding GetBinding() {
|
---|
22 | #if USE_MSG_BINDING
|
---|
23 | NetTcpBinding binding = new NetTcpBinding(SecurityMode.Message);
|
---|
24 | #else
|
---|
25 | NetTcpBinding binding = new NetTcpBinding();
|
---|
26 | binding.MaxBufferSize = int.MaxValue;
|
---|
27 | binding.MaxReceivedMessageSize = int.MaxValue;
|
---|
28 | binding.ReaderQuotas.MaxArrayLength = int.MaxValue;
|
---|
29 | binding.ReaderQuotas.MaxStringContentLength = int.MaxValue;
|
---|
30 | #endif
|
---|
31 | return binding;
|
---|
32 | }
|
---|
33 |
|
---|
34 | /// <summary>
|
---|
35 | /// Defines the used certificate for authentification located in a certification store.
|
---|
36 | /// </summary>
|
---|
37 | /// <param name="svchost">A service for which this certificate is applicable.</param>
|
---|
38 | public static void SetServiceCertificate(ServiceHost svchost) {
|
---|
39 | #if USE_MSG_BINDING
|
---|
40 | svchost.Credentials.ServiceCertificate.SetCertificate(
|
---|
41 | StoreLocation.LocalMachine,
|
---|
42 | StoreName.My,
|
---|
43 | X509FindType.FindBySubjectName,
|
---|
44 | SERVERCERT);
|
---|
45 | #endif
|
---|
46 | }
|
---|
47 |
|
---|
48 | /// <summary>
|
---|
49 | /// Gets the currently active IP address.
|
---|
50 | /// <remarks>If more than one IP connections is active, the first one will be used.</remarks>
|
---|
51 | /// </summary>
|
---|
52 | /// <returns></returns>
|
---|
53 | public static string GetActiveIP() {
|
---|
54 | return System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpConnections()[0].LocalEndPoint.Address.ToString();
|
---|
55 | }
|
---|
56 |
|
---|
57 | /// <summary>
|
---|
58 | /// Gets the default port used for HIVE services.
|
---|
59 | /// </summary>
|
---|
60 | /// <returns></returns>
|
---|
61 | public static int GetDefaultPort() {
|
---|
62 | return DEFAULTPORT;
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | /// <summary>
|
---|
67 | /// This class verifies the certificate defined by <see cref="SetServerCertificate"></see> method. Normally,
|
---|
68 | /// the verification process is managed by the underlying operating system.
|
---|
69 | /// </summary>
|
---|
70 | /// <remarks>
|
---|
71 | /// WARNUNG: Dieser Code wird nur für Testzertifikate benötigt, wie sie beispielsweise von makecert erstellt werden.
|
---|
72 | /// Sie sollten diesen Code nicht in einer Produktionsumgebung verwenden.
|
---|
73 | /// </remarks>
|
---|
74 | public class PermissiveCertificatePolicy {
|
---|
75 | string subjectName;
|
---|
76 | static PermissiveCertificatePolicy currentPolicy;
|
---|
77 | PermissiveCertificatePolicy(string subjectName) {
|
---|
78 | this.subjectName = subjectName;
|
---|
79 | ServicePointManager.ServerCertificateValidationCallback +=
|
---|
80 | new System.Net.Security.RemoteCertificateValidationCallback(RemoteCertValidate);
|
---|
81 | }
|
---|
82 |
|
---|
83 | public static void Enact(string subjectName) {
|
---|
84 | currentPolicy = new PermissiveCertificatePolicy(subjectName);
|
---|
85 | }
|
---|
86 |
|
---|
87 | bool RemoteCertValidate(object sender, X509Certificate cert, X509Chain chain, System.Net.Security.SslPolicyErrors error) {
|
---|
88 | if (cert.Subject == subjectName) {
|
---|
89 | return true;
|
---|
90 | }
|
---|
91 | return false;
|
---|
92 | }
|
---|
93 | }
|
---|
94 | }
|
---|