Merge pull request #2198 from BrzVlad/feature-concurrent-work
[mono.git] / mcs / class / System / Mono.Net.Security / MonoTlsProviderWrapper.cs
1 //
2 // MonoTlsProviderWrapper.cs
3 //
4 // Author:
5 //       Martin Baulig <martin.baulig@xamarin.com>
6 //
7 // Copyright (c) 2015 Xamarin, Inc.
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
29 #if MONO_X509_ALIAS
30 extern alias PrebuiltSystem;
31 #endif
32 #if MONO_SECURITY_ALIAS
33 extern alias MonoSecurity;
34 #endif
35
36 #if MONO_SECURITY_ALIAS
37 using MSI = MonoSecurity::Mono.Security.Interface;
38 #else
39 using MSI = Mono.Security.Interface;
40 #endif
41 #if MONO_X509_ALIAS
42 using XHttpWebRequest = PrebuiltSystem::System.Net.HttpWebRequest;
43 using XX509CertificateCollection = PrebuiltSystem::System.Security.Cryptography.X509Certificates.X509CertificateCollection;
44 #else
45 using XHttpWebRequest = System.Net.HttpWebRequest;
46 using XX509CertificateCollection = System.Security.Cryptography.X509Certificates.X509CertificateCollection;
47 #endif
48
49 using System;
50 using System.IO;
51 using System.Net;
52 using System.Net.Security;
53 using System.Security.Cryptography.X509Certificates;
54
55 namespace Mono.Net.Security.Private
56 {
57         /*
58          * Strictly private - do not use outside the Mono.Net.Security directory.
59          * 
60          * This is used by MonoTlsProviderFactory.InstallProvider() to wrap and masquerade
61          * a user-supplied @MonoTlsProvider as @IMonoTlsProvider.
62          */
63         class MonoTlsProviderWrapper : IMonoTlsProvider
64         {
65                 MSI.MonoTlsProvider provider;
66
67                 public MonoTlsProviderWrapper (MSI.MonoTlsProvider provider)
68                 {
69                         this.provider = provider;
70                 }
71
72                 public MSI.MonoTlsProvider Provider {
73                         get { return provider; }
74                 }
75
76                 public IMonoSslStream CreateSslStream (
77                         Stream innerStream, bool leaveInnerStreamOpen,
78                         MSI.MonoTlsSettings settings)
79                 {
80                         var sslStream = provider.CreateSslStream (innerStream, leaveInnerStreamOpen, settings);
81                         var monoSslStreamImpl = sslStream as MonoSslStreamImpl;
82                         if (monoSslStreamImpl != null)
83                                 return monoSslStreamImpl.Impl;
84                         return new MonoSslStreamWrapper (sslStream);
85                 }
86
87                 public MSI.IMonoTlsContext CreateTlsContext (
88                         string hostname, bool serverMode, MSI.TlsProtocols protocolFlags,
89                         X509Certificate serverCertificate, XX509CertificateCollection clientCertificates,
90                         bool remoteCertRequired, bool checkCertName, bool checkCertRevocationStatus,
91                         MSI.MonoEncryptionPolicy encryptionPolicy, MSI.MonoTlsSettings settings)
92                 {
93                         return provider.CreateTlsContext (
94                                 hostname, serverMode, protocolFlags,
95                                 serverCertificate, (XX509CertificateCollection)(object)clientCertificates,
96                                 remoteCertRequired, (MSI.MonoEncryptionPolicy)encryptionPolicy,
97                                 settings);
98                 }
99         }
100 }
101
102 #endif