[runtime] Fix the managed name of the ReRegisterForFinalize () icall.
[mono.git] / mcs / class / Mono.Security.Providers.NewTls / Mono.Security.Providers.NewTls / MonoNewTlsStreamFactory.cs
1 //
2 // MonoNewTlsStreamFactory.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 extern alias NewSystemSource;
28
29 using XEncryptionPolicy = NewSystemSource::System.Net.Security.EncryptionPolicy;
30 using XSslPolicyErrors = NewSystemSource::System.Net.Security.SslPolicyErrors;
31 using XLocalCertificateSelectionCallback = NewSystemSource::System.Net.Security.LocalCertificateSelectionCallback;
32 using XRemoteCertificateValidationCallback = NewSystemSource::System.Net.Security.RemoteCertificateValidationCallback;
33
34 using System;
35 using System.IO;
36 using System.Net.Security;
37 using System.Security.Authentication;
38
39 using Mono.Security.Interface;
40
41 using PSSCX = System.Security.Cryptography.X509Certificates;
42 using SSCX = System.Security.Cryptography.X509Certificates;
43
44 namespace Mono.Security.Providers.NewTls
45 {
46         public static class MonoNewTlsStreamFactory
47         {
48                 internal static IMonoSslStream CreateSslStream (
49                         Stream innerStream, bool leaveInnerStreamOpen,
50                         MonoTlsProvider provider, MonoTlsSettings settings = null)
51                 {
52                         return new MonoNewTlsStream (innerStream, leaveInnerStreamOpen, provider, settings);
53                 }
54
55                 public static MonoNewTlsStream CreateServer (
56                         Stream innerStream, bool leaveOpen, MonoTlsProvider provider, MonoTlsSettings settings,
57                         SSCX.X509Certificate serverCertificate, bool clientCertificateRequired,
58                         SslProtocols enabledSslProtocols, bool checkCertificateRevocation)
59                 {
60                         var stream = new MonoNewTlsStream (innerStream, leaveOpen, provider, settings);
61
62                         try {
63                                 stream.AuthenticateAsServer (serverCertificate, clientCertificateRequired, enabledSslProtocols, checkCertificateRevocation);
64                         } catch (Exception ex) {
65                                 var tlsEx = stream.LastError;
66                                 if (tlsEx != null)
67                                         throw new AggregateException (ex, tlsEx);
68                                 throw;
69                         }
70
71                         return stream;
72                 }
73
74                 public static MonoNewTlsStream CreateClient (
75                         Stream innerStream, bool leaveOpen, MonoTlsProvider provider, MonoTlsSettings settings,
76                         string targetHost, PSSCX.X509CertificateCollection clientCertificates, SslProtocols enabledSslProtocols, bool checkCertificateRevocation)
77                 {
78                         var stream = new MonoNewTlsStream (innerStream, leaveOpen, provider, settings);
79
80                         try {
81                                 stream.AuthenticateAsClient (targetHost, clientCertificates, enabledSslProtocols, checkCertificateRevocation);
82                         } catch (Exception ex) {
83                                 var tlsEx = stream.LastError;
84                                 if (tlsEx != null)
85                                         throw new AggregateException (ex, tlsEx);
86                                 throw;
87                         }
88                         return stream;
89                 }
90         }
91 }