Merge pull request #2247 from ivmai/match-ext-libgc-api
[mono.git] / mcs / class / Mono.Security.Providers.DotNet / Mono.Security.Providers.DotNet / DotNetTlsProvider.cs
1 //
2 // MonoDefaultTlsProvider.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 using System;
27 using System.IO;
28 using System.Net;
29 using System.Net.Security;
30 using System.Security.Authentication;
31 using System.Security.Cryptography.X509Certificates;
32 using Mono.Security.Interface;
33
34 namespace Mono.Security.Providers.DotNet
35 {
36         /*
37          * This provider only uses the public .NET APIs from System.dll.
38          * 
39          * It is primarily intended for testing.
40          */
41         public class DotNetTlsProvider : MonoTlsProvider
42         {
43                 static readonly Guid id = new Guid ("3a7b3a26-0dbd-4572-a5b8-fdce766bf0dd");
44
45                 public override Guid ID {
46                         get { return id; }
47                 }
48
49                 public override string Name {
50                         get { return "dotnet"; }
51                 }
52
53                 public override bool SupportsSslStream {
54                         get { return true; }
55                 }
56
57                 public override bool SupportsMonoExtensions {
58                         get { return false; }
59                 }
60
61                 public override bool SupportsTlsContext {
62                         get { return false; }
63                 }
64
65                 public override SslProtocols SupportedProtocols {
66                         get { return (SslProtocols)ServicePointManager.SecurityProtocol; }
67                 }
68
69                 public override IMonoSslStream CreateSslStream (
70                         Stream innerStream, bool leaveInnerStreamOpen,
71                         MonoTlsSettings settings = null)
72                 {
73                         if (settings != null)
74                                 throw new NotSupportedException ("Mono-specific API Extensions not available.");
75
76                         RemoteCertificateValidationCallback validation_callback = null;
77                         LocalCertificateSelectionCallback selection_callback = null;
78
79                         if (settings != null) {
80                                 validation_callback = ConvertCallback (settings.RemoteCertificateValidationCallback);
81                                 selection_callback = ConvertCallback (settings.ClientCertificateSelectionCallback);
82                         }
83
84                         return new DotNetSslStreamImpl (innerStream, leaveInnerStreamOpen, validation_callback, selection_callback);
85                 }
86
87                 public override IMonoTlsContext CreateTlsContext (
88                         string hostname, bool serverMode, TlsProtocols protocolFlags,
89                         X509Certificate serverCertificate, X509CertificateCollection clientCertificates,
90                         bool remoteCertRequired, MonoEncryptionPolicy encryptionPolicy,
91                         MonoTlsSettings settings)
92                 {
93                         throw new NotSupportedException ();
94                 }
95
96                 internal static RemoteCertificateValidationCallback ConvertCallback (MonoRemoteCertificateValidationCallback callback)
97                 {
98                         if (callback == null)
99                                 return null;
100
101                         return (s, c, ch, e) => callback (null, c, ch, (MonoSslPolicyErrors)e);
102                 }
103
104                 internal static LocalCertificateSelectionCallback ConvertCallback (MonoLocalCertificateSelectionCallback callback)
105                 {
106                         if (callback == null)
107                                 return null;
108
109                         return (s, t, lc, rc, ai) => callback (t, lc, rc, ai);
110                 }
111
112         }
113 }
114