[System]: Remove 'SECURITY_DEP' conditional from HttpListener and related classes...
[mono.git] / mcs / class / System / System.Net / HttpListener.Mono.cs
1 //
2 // HttpListener.Mono.cs
3 //
4 // Author:
5 //       Martin Baulig <mabaul@microsoft.com>
6 //
7 // Copyright (c) 2017 Xamarin Inc. (http://www.xamarin.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
15 //
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
18 //
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 // THE SOFTWARE.
26 //
27 #if SECURITY_DEP
28 #if MONO_SECURITY_ALIAS
29 extern alias MonoSecurity;
30 using MonoSecurity::Mono.Security.Authenticode;
31 using MonoSecurity::Mono.Security.Interface;
32 #else
33 using Mono.Security.Authenticode;
34 using Mono.Security.Interface;
35 #endif
36 using MNS = Mono.Net.Security;
37 #endif
38
39 using System.IO;
40 using System.Collections;
41 using System.Threading;
42 using System.Threading.Tasks;
43 using System.Net.Security;
44 using System.Security.Authentication.ExtendedProtection;
45 using System.Security.Cryptography;
46 using System.Security.Cryptography.X509Certificates;
47
48 namespace System.Net {
49         partial class HttpListener {
50 #if SECURITY_DEP
51                 MonoTlsProvider tlsProvider;
52                 MonoTlsSettings tlsSettings;
53                 X509Certificate certificate;
54
55                 internal HttpListener (X509Certificate certificate, MonoTlsProvider tlsProvider, MonoTlsSettings tlsSettings)
56                         : this ()
57                 {
58                         this.certificate = certificate;
59                         this.tlsProvider = tlsProvider;
60                         this.tlsSettings = tlsSettings;
61                 }
62 #endif
63
64                 internal X509Certificate LoadCertificateAndKey (IPAddress addr, int port)
65                 {
66 #if SECURITY_DEP
67                         lock (_internalLock) {
68                                 if (certificate != null)
69                                         return certificate;
70
71                                 // Actually load the certificate
72                                 try {
73                                         string dirname = Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData);
74                                         string path = Path.Combine (dirname, ".mono");
75                                         path = Path.Combine (path, "httplistener");
76                                         string cert_file = Path.Combine (path, String.Format ("{0}.cer", port));
77                                         if (!File.Exists (cert_file))
78                                                 return null;
79                                         string pvk_file = Path.Combine (path, String.Format ("{0}.pvk", port));
80                                         if (!File.Exists (pvk_file))
81                                                 return null;
82                                         var cert = new X509Certificate2 (cert_file);
83                                         cert.PrivateKey = PrivateKey.CreateFromFile (pvk_file).RSA;
84                                         certificate = cert;
85                                         return certificate;
86                                 } catch {
87                                         // ignore errors
88                                         certificate = null;
89                                         return null;
90                                 }
91                         }
92 #else
93                         throw new PlatformNotSupportedException ();
94 #endif
95                 }
96
97                 internal SslStream CreateSslStream (Stream innerStream, bool ownsStream, RemoteCertificateValidationCallback callback)
98                 {
99 #if SECURITY_DEP
100                         lock (_internalLock) {
101                                 if (tlsProvider == null)
102                                         tlsProvider = MonoTlsProviderFactory.GetProvider ();
103                                 if (tlsSettings == null)
104                                         tlsSettings = MonoTlsSettings.CopyDefaultSettings ();
105                                 if (tlsSettings.RemoteCertificateValidationCallback == null)
106                                         tlsSettings.RemoteCertificateValidationCallback = MNS.Private.CallbackHelpers.PublicToMono (callback);
107                                 var sslStream = tlsProvider.CreateSslStream (innerStream, ownsStream, tlsSettings);
108                                 return sslStream.SslStream;
109                         }
110 #else
111                         throw new PlatformNotSupportedException ();
112 #endif
113                 }
114         }
115 }