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
.Security
.Cryptography
;
30 using System
.Runtime
.InteropServices
;
31 using System
.Runtime
.CompilerServices
;
35 abstract class MonoBtlsObject
: IDisposable
37 internal const string BTLS_DYLIB
= "libmono-btls-shared";
39 internal MonoBtlsObject (MonoBtlsHandle handle
)
44 protected internal abstract class MonoBtlsHandle
: SafeHandle
46 internal MonoBtlsHandle ()
47 : base (IntPtr
.Zero
, true)
51 internal MonoBtlsHandle (IntPtr handle
, bool ownsHandle
)
52 : base (handle
, ownsHandle
)
56 public override bool IsInvalid
{
57 get { return handle == IntPtr.Zero; }
61 internal MonoBtlsHandle Handle
{
69 get { return handle != null && !handle.IsInvalid; }
72 MonoBtlsHandle handle
;
75 protected void CheckThrow ()
77 if (lastError
!= null)
79 if (handle
== null || handle
.IsInvalid
)
80 throw new ObjectDisposedException ("MonoBtlsSsl");
83 protected Exception
SetException (Exception ex
)
85 if (lastError
== null)
90 protected void CheckError (bool ok
, [CallerMemberName
] string callerName
= null)
93 if (callerName
!= null)
94 throw new CryptographicException ($"`{GetType ().Name}.{callerName}` failed.");
96 throw new CryptographicException ();
100 protected void CheckError (int ret
, [CallerMemberName
] string callerName
= null)
102 CheckError (ret
== 1, callerName
);
105 protected internal void CheckLastError ([CallerMemberName
] string callerName
= null)
107 var error
= Interlocked
.Exchange (ref lastError
, null);
112 if (callerName
!= null)
113 message
= $"Caught unhandled exception in `{GetType ().Name}.{callerName}`.";
115 message
= "Caught unhandled exception.";
116 throw new CryptographicException (message
, error
);
119 [DllImport (BTLS_DYLIB
)]
120 extern static void mono_btls_free (IntPtr data
);
122 protected void FreeDataPtr (IntPtr data
)
124 mono_btls_free (data
);
127 protected virtual void Close ()
131 protected void Dispose (bool disposing
)
135 if (handle
!= null) {
141 var disposedExc
= new ObjectDisposedException (GetType ().Name
);
142 Interlocked
.CompareExchange (ref lastError
, disposedExc
, null);
147 public void Dispose ()
150 GC
.SuppressFinalize (this);