Throw `CryptographicException` instead of `MonoBtlsException`. (#9504)
[mono-project.git] / mcs / class / System / Mono.Btls / MonoBtlsObject.cs
blobf1b4fd76855aa05d56279d60d5bdd8f92272446c
1 //
2 // MonoBtlsObject.cs
3 //
4 // Author:
5 // Martin Baulig <martin.baulig@xamarin.com>
6 //
7 // Copyright (c) 2016 Xamarin Inc. (http://www.xamarin.com)
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:
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
25 // THE SOFTWARE.
26 #if SECURITY_DEP && MONO_FEATURE_BTLS
27 using System;
28 using System.Threading;
29 using System.Security.Cryptography;
30 using System.Runtime.InteropServices;
31 using System.Runtime.CompilerServices;
33 namespace Mono.Btls
35 abstract class MonoBtlsObject : IDisposable
37 internal const string BTLS_DYLIB = "libmono-btls-shared";
39 internal MonoBtlsObject (MonoBtlsHandle handle)
41 this.handle = 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 {
62 get {
63 CheckThrow ();
64 return handle;
68 public bool IsValid {
69 get { return handle != null && !handle.IsInvalid; }
72 MonoBtlsHandle handle;
73 Exception lastError;
75 protected void CheckThrow ()
77 if (lastError != null)
78 throw lastError;
79 if (handle == null || handle.IsInvalid)
80 throw new ObjectDisposedException ("MonoBtlsSsl");
83 protected Exception SetException (Exception ex)
85 if (lastError == null)
86 lastError = ex;
87 return ex;
90 protected void CheckError (bool ok, [CallerMemberName] string callerName = null)
92 if (!ok) {
93 if (callerName != null)
94 throw new CryptographicException ($"`{GetType ().Name}.{callerName}` failed.");
95 else
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);
108 if (error == null)
109 return;
111 string message;
112 if (callerName != null)
113 message = $"Caught unhandled exception in `{GetType ().Name}.{callerName}`.";
114 else
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)
133 if (disposing) {
134 try {
135 if (handle != null) {
136 Close ();
137 handle.Dispose ();
138 handle = null;
140 } finally {
141 var disposedExc = new ObjectDisposedException (GetType ().Name);
142 Interlocked.CompareExchange (ref lastError, disposedExc, null);
147 public void Dispose ()
149 Dispose (true);
150 GC.SuppressFinalize (this);
153 ~MonoBtlsObject ()
155 Dispose (false);
159 #endif