Merge pull request #2223 from lobrien/master
[mono.git] / mcs / class / System / Mono.Net.Security / MonoTlsProviderImpl.cs
1 //
2 // MonoTlsProviderImpl.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         abstract class MonoTlsProviderImpl : MSI.MonoTlsProvider, IMonoTlsProvider
61         {
62                 MSI.MonoTlsProvider IMonoTlsProvider.Provider {
63                         get { return this; }
64                 }
65
66                 IMonoSslStream IMonoTlsProvider.CreateSslStream (
67                         Stream innerStream, bool leaveInnerStreamOpen,
68                         MSI.MonoTlsSettings settings)
69                 {
70                         return CreateSslStreamImpl (innerStream, leaveInnerStreamOpen, settings);
71                 }
72
73                 protected abstract IMonoSslStream CreateSslStreamImpl (
74                         Stream innerStream, bool leaveInnerStreamOpen,
75                         MSI.MonoTlsSettings settings);
76
77                 public override MSI.IMonoSslStream CreateSslStream (
78                         Stream innerStream, bool leaveInnerStreamOpen,
79                         MSI.MonoTlsSettings settings = null)
80                 {
81                         var sslStream = CreateSslStreamImpl (innerStream, leaveInnerStreamOpen, settings);
82                         return new MonoSslStreamImpl (sslStream);
83                 }
84
85                 MSI.IMonoTlsContext IMonoTlsProvider.CreateTlsContext (
86                         string hostname, bool serverMode, MSI.TlsProtocols protocolFlags,
87                         X509Certificate serverCertificate, XX509CertificateCollection clientCertificates,
88                         bool remoteCertRequired, bool checkCertName, bool checkCertRevocationStatus,
89                         MSI.MonoEncryptionPolicy encryptionPolicy, MSI.MonoTlsSettings settings)
90                 {
91                         return CreateTlsContextImpl (
92                                 hostname, serverMode, protocolFlags,
93                                 serverCertificate, (X509CertificateCollection)(object)clientCertificates,
94                                 remoteCertRequired, encryptionPolicy, settings);
95                 }
96
97                 protected abstract MSI.IMonoTlsContext CreateTlsContextImpl (
98                         string hostname, bool serverMode, MSI.TlsProtocols protocolFlags,
99                         X509Certificate serverCertificate, X509CertificateCollection clientCertificates,
100                         bool remoteCertRequired, MSI.MonoEncryptionPolicy encryptionPolicy,
101                         MSI.MonoTlsSettings settings);
102
103                 public override MSI.IMonoTlsContext CreateTlsContext (
104                         string hostname, bool serverMode, MSI.TlsProtocols protocolFlags,
105                         X509Certificate serverCertificate, XX509CertificateCollection clientCertificates,
106                         bool remoteCertRequired, MSI.MonoEncryptionPolicy encryptionPolicy,
107                         MSI.MonoTlsSettings settings)
108                 {
109                         return CreateTlsContextImpl (
110                                 hostname, serverMode, (MSI.TlsProtocols)protocolFlags,
111                                 serverCertificate, (X509CertificateCollection)(object)clientCertificates,
112                                 remoteCertRequired, (MSI.MonoEncryptionPolicy)encryptionPolicy,
113                                 settings);
114                 }
115         }
116 }
117
118 #endif