5 // Martin Baulig <martin.baulig@xamarin.com>
7 // Copyright (c) 2016 Xamarin Inc. (http://www.xamarin.com)
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
26 #if SECURITY_DEP && MONO_FEATURE_BTLS
28 using System.Threading;
29 using System.Runtime.InteropServices;
30 using System.Runtime.CompilerServices;
34 abstract class MonoBtlsObject : IDisposable
36 internal const string BTLS_DYLIB = "libmono-btls-shared";
38 internal MonoBtlsObject (MonoBtlsHandle handle)
43 protected internal abstract class MonoBtlsHandle : SafeHandle
45 internal MonoBtlsHandle ()
46 : base (IntPtr.Zero, true)
50 internal MonoBtlsHandle (IntPtr handle, bool ownsHandle)
51 : base (handle, ownsHandle)
55 public override bool IsInvalid {
56 get { return handle == IntPtr.Zero; }
60 internal MonoBtlsHandle Handle {
68 get { return handle != null && !handle.IsInvalid; }
71 MonoBtlsHandle handle;
74 protected void CheckThrow ()
76 if (lastError != null)
78 if (handle == null || handle.IsInvalid)
79 throw new ObjectDisposedException ("MonoBtlsSsl");
82 protected Exception SetException (Exception ex)
84 if (lastError == null)
89 protected void CheckError (bool ok, [CallerMemberName] string callerName = null)
92 if (callerName != null)
93 throw new MonoBtlsException ("{0}.{1} failed.", GetType ().Name, callerName);
95 throw new MonoBtlsException ();
100 protected void CheckError (int ret, [CallerMemberName] string callerName = null)
102 CheckError (ret == 1, callerName);
105 [DllImport (BTLS_DYLIB)]
106 extern static void mono_btls_free (IntPtr data);
108 protected void FreeDataPtr (IntPtr data)
110 mono_btls_free (data);
113 protected virtual void Close ()
117 protected void Dispose (bool disposing)
121 if (handle != null) {
127 var disposedExc = new ObjectDisposedException (GetType ().Name);
128 Interlocked.CompareExchange (ref lastError, disposedExc, null);
133 public void Dispose ()
136 GC.SuppressFinalize (this);