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
30 using System.Runtime.InteropServices;
31 using System.Runtime.CompilerServices;
35 class MonoBtlsKey : MonoBtlsObject
37 internal class BoringKeyHandle : MonoBtlsHandle
39 internal BoringKeyHandle (IntPtr handle)
44 protected override bool ReleaseHandle ()
46 mono_btls_key_free (handle);
51 [DllImport (BTLS_DYLIB)]
52 extern static void mono_btls_key_free (IntPtr handle);
54 [DllImport (BTLS_DYLIB)]
55 extern static IntPtr mono_btls_key_up_ref (IntPtr handle);
57 [DllImport (BTLS_DYLIB)]
58 extern static int mono_btls_key_get_bytes (IntPtr handle, out IntPtr data, out int size, int include_private_bits);
60 [DllImport (BTLS_DYLIB)]
61 extern static int mono_btls_key_get_bits (IntPtr handle);
63 [DllImport (BTLS_DYLIB)]
64 extern static int mono_btls_key_is_rsa (IntPtr handle);
66 new internal BoringKeyHandle Handle {
67 get { return (BoringKeyHandle)base.Handle; }
70 internal MonoBtlsKey (BoringKeyHandle handle)
75 public byte[] GetBytes (bool include_private_bits)
80 var ret = mono_btls_key_get_bytes (Handle.DangerousGetHandle (), out data, out size, include_private_bits ? 1 : 0);
83 var buffer = new byte [size];
84 Marshal.Copy (data, buffer, 0, size);
91 return mono_btls_key_is_rsa (Handle.DangerousGetHandle ()) != 0;
95 public MonoBtlsKey Copy ()
98 var copy = mono_btls_key_up_ref (Handle.DangerousGetHandle ());
99 CheckError (copy != IntPtr.Zero);
100 return new MonoBtlsKey (new BoringKeyHandle (copy));