2 // TlsContextWrapper.cs
5 // Martin Baulig <martin.baulig@xamarin.com>
7 // Copyright (c) 2015 Xamarin, Inc.
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:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
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
27 extern alias NewSystemSource;
30 using System.Security.Cryptography;
32 using SSCX = System.Security.Cryptography.X509Certificates;
33 using PSSCX = System.Security.Cryptography.X509Certificates;
35 using MSI = Mono.Security.Interface;
36 using MX = Mono.Security.X509;
38 namespace Mono.Security.Providers.NewTls
40 class TlsContextWrapper : IDisposable, MSI.IMonoTlsContext
42 ITlsConfiguration config;
46 public TlsContextWrapper (ITlsConfiguration config, bool serverMode)
49 this.serverMode = serverMode;
52 public bool IsServer {
53 get { return serverMode; }
57 get { return context != null && context.IsValid; }
60 public void Initialize (MSI.IMonoTlsEventSink eventSink)
63 throw new InvalidOperationException ();
64 context = TlsProviderFactory.CreateTlsContext (config, serverMode, eventSink);
69 if (context != null) {
75 public ITlsConfiguration Configuration {
78 throw new ObjectDisposedException ("TlsConfiguration");
83 public ITlsContext Context {
86 throw new ObjectDisposedException ("TlsContext");
91 public bool HasCredentials {
92 get { return Configuration.HasCredentials; }
95 public void SetCertificate (SSCX.X509Certificate certificate, AsymmetricAlgorithm privateKey)
97 var monoCert = new MX.X509Certificate (certificate.GetRawCertData ());
98 Configuration.SetCertificate (monoCert, privateKey);
101 public int GenerateNextToken (MSI.IBufferOffsetSize incoming, out MSI.IBufferOffsetSize outgoing)
103 var input = incoming != null ? new MSI.TlsBuffer (BOSWrapper.Wrap (incoming)) : null;
104 var output = new MSI.TlsMultiBuffer ();
105 var retval = Context.GenerateNextToken (input, output);
108 outgoing = BOSWrapper.Wrap (output.StealBuffer ());
112 public int EncryptMessage (ref MSI.IBufferOffsetSize incoming)
114 var buffer = new MSI.TlsBuffer (BOSWrapper.Wrap (incoming));
115 var retval = Context.EncryptMessage (ref buffer);
116 incoming = BOSWrapper.Wrap (buffer.GetRemaining ());
120 public int DecryptMessage (ref MSI.IBufferOffsetSize incoming)
122 var buffer = new MSI.TlsBuffer (BOSWrapper.Wrap (incoming));
123 var retval = Context.DecryptMessage (ref buffer);
124 incoming = buffer != null ? BOSWrapper.Wrap (buffer.GetRemaining ()) : null;
128 class BOSWrapper : MSI.IBufferOffsetSize
130 public byte[] Buffer {
145 BOSWrapper (byte[] buffer, int offset, int size)
152 public static BOSWrapper Wrap (MSI.IBufferOffsetSize bos)
154 return bos != null ? new BOSWrapper (bos.Buffer, bos.Offset, bos.Size) : null;
158 public byte[] CreateCloseNotify ()
160 return Context.CreateAlert (new MSI.Alert (MSI.AlertLevel.Warning, MSI.AlertDescription.CloseNotify));
163 public byte[] CreateHelloRequest ()
165 return Context.CreateHelloRequest ();
168 public SSCX.X509Certificate GetRemoteCertificate (out PSSCX.X509CertificateCollection remoteCertificateStore)
170 MX.X509CertificateCollection monoCollection;
171 var remoteCert = Context.GetRemoteCertificate (out monoCollection);
172 if (remoteCert == null) {
173 remoteCertificateStore = null;
177 remoteCertificateStore = new PSSCX.X509CertificateCollection ();
178 foreach (var cert in monoCollection) {
179 remoteCertificateStore.Add (new PSSCX.X509Certificate2 (cert.RawData));
181 return new PSSCX.X509Certificate2 (remoteCert.RawData);
185 public bool VerifyRemoteCertificate ()
187 return Context.VerifyRemoteCertificate ();
190 public Exception LastError {
193 return context.LastError;
198 public bool ReceivedCloseNotify {
200 return Context.ReceivedCloseNotify;
204 public MSI.MonoTlsConnectionInfo GetConnectionInfo ()
206 return Context.ConnectionInfo;
209 public void Dispose ()
212 GC.SuppressFinalize (this);
215 void Dispose (bool disposing)