New run-jenkins tags to allow a checked build job and fix partial checked build suppo...
[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 SupportsConnectionInfo {
58                         get { return false; }
59                 }
60
61                 public override bool SupportsMonoExtensions {
62                         get { return false; }
63                 }
64
65                 internal override bool SupportsTlsContext {
66                         get { return false; }
67                 }
68
69                 public override SslProtocols SupportedProtocols {
70                         get { return (SslProtocols)ServicePointManager.SecurityProtocol; }
71                 }
72
73                 public override IMonoSslStream CreateSslStream (
74                         Stream innerStream, bool leaveInnerStreamOpen,
75                         MonoTlsSettings settings = null)
76                 {
77                         if (settings != null)
78                                 throw new NotSupportedException ("Mono-specific API Extensions not available.");
79
80                         RemoteCertificateValidationCallback validation_callback = null;
81                         LocalCertificateSelectionCallback selection_callback = null;
82
83                         if (settings != null) {
84                                 validation_callback = ConvertCallback (settings.RemoteCertificateValidationCallback);
85                                 selection_callback = ConvertCallback (settings.ClientCertificateSelectionCallback);
86                         }
87
88                         return new DotNetSslStreamImpl (innerStream, leaveInnerStreamOpen, this, validation_callback, selection_callback);
89                 }
90
91                 internal override IMonoTlsContext CreateTlsContext (
92                         string hostname, bool serverMode, TlsProtocols protocolFlags,
93                         X509Certificate serverCertificate, X509CertificateCollection clientCertificates,
94                         bool remoteCertRequired, MonoEncryptionPolicy encryptionPolicy,
95                         MonoTlsSettings settings)
96                 {
97                         throw new NotSupportedException ();
98                 }
99
100                 internal static RemoteCertificateValidationCallback ConvertCallback (MonoRemoteCertificateValidationCallback callback)
101                 {
102                         if (callback == null)
103                                 return null;
104
105                         return (s, c, ch, e) => callback (null, c, ch, (MonoSslPolicyErrors)e);
106                 }
107
108                 internal static LocalCertificateSelectionCallback ConvertCallback (MonoLocalCertificateSelectionCallback callback)
109                 {
110                         if (callback == null)
111                                 return null;
112
113                         return (s, t, lc, rc, ai) => callback (t, lc, rc, ai);
114                 }
115
116         }
117 }
118