Merge pull request #2183 from joelmartinez/monodoc-ecmacref-fix
[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                 public override bool SupportsSslStream {
44                         get { return true; }
45                 }
46
47                 public override bool SupportsMonoExtensions {
48                         get { return false; }
49                 }
50
51                 public override bool SupportsTlsContext {
52                         get { return false; }
53                 }
54
55                 public override SslProtocols SupportedProtocols {
56                         get { return (SslProtocols)ServicePointManager.SecurityProtocol; }
57                 }
58
59                 public override MonoSslStream CreateSslStream (
60                         Stream innerStream, bool leaveInnerStreamOpen,
61                         MonoTlsSettings settings = null)
62                 {
63                         if (settings != null)
64                                 throw new NotSupportedException ("Mono-specific API Extensions not available.");
65
66                         RemoteCertificateValidationCallback validation_callback = null;
67                         LocalCertificateSelectionCallback selection_callback = null;
68
69                         if (settings != null) {
70                                 validation_callback = ConvertCallback (settings.RemoteCertificateValidationCallback);
71                                 selection_callback = ConvertCallback (settings.ClientCertificateSelectionCallback);
72                         }
73
74                         return new DotNetSslStreamImpl (innerStream, leaveInnerStreamOpen, validation_callback, selection_callback);
75                 }
76
77                 public override IMonoTlsContext CreateTlsContext (
78                         string hostname, bool serverMode, TlsProtocols protocolFlags,
79                         X509Certificate serverCertificate, X509CertificateCollection clientCertificates,
80                         bool remoteCertRequired, MonoEncryptionPolicy encryptionPolicy,
81                         MonoTlsSettings settings)
82                 {
83                         throw new NotSupportedException ();
84                 }
85
86                 internal static RemoteCertificateValidationCallback ConvertCallback (MonoRemoteCertificateValidationCallback callback)
87                 {
88                         if (callback == null)
89                                 return null;
90
91                         return (s, c, ch, e) => callback (null, c, ch, (MonoSslPolicyErrors)e);
92                 }
93
94                 internal static LocalCertificateSelectionCallback ConvertCallback (MonoLocalCertificateSelectionCallback callback)
95                 {
96                         if (callback == null)
97                                 return null;
98
99                         return (s, t, lc, rc, ai) => callback (t, lc, rc, ai);
100                 }
101
102         }
103 }
104