Merge pull request #2274 from esdrubal/udpclientreceive
[mono.git] / mcs / class / System / System.Net / HttpListener.cs
index 82cf8bda9eb4b076db9e88955333c62dca950d95..29afc0fd84b78ceca4e33b0dfe9efa2b4f9767fd 100644 (file)
 //
 
 #if SECURITY_DEP
+#if MONO_SECURITY_ALIAS
+extern alias MonoSecurity;
+using MonoSecurity::Mono.Security.Authenticode;
+using MSI = MonoSecurity::Mono.Security.Interface;
+#else
+using Mono.Security.Authenticode;
+using MSI = Mono.Security.Interface;
+#endif
 
+using System.IO;
 using System.Collections;
 using System.Threading;
 using System.Threading.Tasks;
+using System.Security.Cryptography;
+using System.Security.Cryptography.X509Certificates;
+
+using Mono.Net.Security;
+
 
 //TODO: logging
 namespace System.Net {
@@ -46,6 +60,10 @@ namespace System.Net {
                bool listening;
                bool disposed;
 
+               IMonoTlsProvider tlsProvider;
+               MSI.MonoTlsSettings tlsSettings;
+               X509Certificate certificate;
+
                Hashtable registry;   // Dictionary<HttpListenerContext,HttpListenerContext> 
                ArrayList ctx_queue;  // List<HttpListenerContext> ctx_queue;
                ArrayList wait_queue; // List<ListenerAsyncResult> wait_queue;
@@ -61,6 +79,56 @@ namespace System.Net {
                        auth_schemes = AuthenticationSchemes.Anonymous;
                }
 
+               internal HttpListener (X509Certificate certificate, IMonoTlsProvider tlsProvider, MSI.MonoTlsSettings tlsSettings)
+                       : this ()
+               {
+                       this.certificate = certificate;
+                       this.tlsProvider = tlsProvider;
+                       this.tlsSettings = tlsSettings;
+               }
+
+               internal X509Certificate LoadCertificateAndKey (IPAddress addr, int port)
+               {
+                       lock (registry) {
+                               if (certificate != null)
+                                       return certificate;
+
+                               // Actually load the certificate
+                               try {
+                                       string dirname = Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData);
+                                       string path = Path.Combine (dirname, ".mono");
+                                       path = Path.Combine (path, "httplistener");
+                                       string cert_file = Path.Combine (path, String.Format ("{0}.cer", port));
+                                       if (!File.Exists (cert_file))
+                                               return null;
+                                       string pvk_file = Path.Combine (path, String.Format ("{0}.pvk", port));
+                                       if (!File.Exists (pvk_file))
+                                               return null;
+                                       var cert = new X509Certificate2 (cert_file);
+                                       cert.PrivateKey = PrivateKey.CreateFromFile (pvk_file).RSA;
+                                       certificate = cert;
+                                       return certificate;
+                               } catch {
+                                       // ignore errors
+                                       certificate = null;
+                                       return null;
+                               }
+                       }
+               }
+
+               internal IMonoSslStream CreateSslStream (Stream innerStream, bool ownsStream, MSI.MonoRemoteCertificateValidationCallback callback)
+               {
+                       lock (registry) {
+                               if (tlsProvider == null)
+                                       tlsProvider = MonoTlsProviderFactory.GetProviderInternal ();
+                               if (tlsSettings == null)
+                                       tlsSettings = MSI.MonoTlsSettings.CopyDefaultSettings ();
+                               if (tlsSettings.RemoteCertificateValidationCallback == null)
+                                       tlsSettings.RemoteCertificateValidationCallback = callback;
+                               return tlsProvider.CreateSslStream (innerStream, ownsStream, tlsSettings);
+                       }
+               }
+
                // TODO: Digest, NTLM and Negotiate require ControlPrincipal
                public AuthenticationSchemes AuthenticationSchemes {
                        get { return auth_schemes; }
@@ -349,5 +417,11 @@ namespace System.Net {
                }
        }
 }
+#else // SECURITY_DEP
+namespace System.Net
+{
+       public sealed class HttpListener
+       {
+       }
+}
 #endif
-