2 // HttpListener.Mono.cs
5 // Martin Baulig <mabaul@microsoft.com>
7 // Copyright (c) 2017 Xamarin Inc. (http://www.xamarin.com)
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:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
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
28 #if MONO_SECURITY_ALIAS
29 extern alias MonoSecurity;
30 using MonoSecurity::Mono.Security.Authenticode;
31 using MonoSecurity::Mono.Security.Interface;
33 using Mono.Security.Authenticode;
34 using Mono.Security.Interface;
36 using MNS = Mono.Net.Security;
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;
48 namespace System.Net {
49 partial class HttpListener {
51 MonoTlsProvider tlsProvider;
52 MonoTlsSettings tlsSettings;
53 X509Certificate certificate;
55 internal HttpListener (X509Certificate certificate, MonoTlsProvider tlsProvider, MonoTlsSettings tlsSettings)
58 this.certificate = certificate;
59 this.tlsProvider = tlsProvider;
60 this.tlsSettings = tlsSettings;
64 internal X509Certificate LoadCertificateAndKey (IPAddress addr, int port)
67 lock (_internalLock) {
68 if (certificate != null)
71 // Actually load the certificate
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))
79 string pvk_file = Path.Combine (path, String.Format ("{0}.pvk", port));
80 if (!File.Exists (pvk_file))
82 var cert = new X509Certificate2 (cert_file);
83 cert.PrivateKey = PrivateKey.CreateFromFile (pvk_file).RSA;
93 throw new PlatformNotSupportedException ();
97 internal SslStream CreateSslStream (Stream innerStream, bool ownsStream, RemoteCertificateValidationCallback callback)
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;
111 throw new PlatformNotSupportedException ();