update readme (#21797)
[mono-project.git] / mcs / class / System / Mono.Btls / MonoBtlsKey.cs
blobfcbe0026c89afa417ca81ef82994a0c68d746df1
1 //
2 // MonoBtlsKey.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 #if MONO_SECURITY_ALIAS
28 extern alias MonoSecurity;
29 using MX = MonoSecurity::Mono.Security.Cryptography;
30 #else
31 using MX = Mono.Security.Cryptography;
32 #endif
33 using System;
34 using System.IO;
35 using System.Text;
36 using System.Runtime.InteropServices;
37 using System.Runtime.CompilerServices;
39 namespace Mono.Btls
41 class MonoBtlsKey : MonoBtlsObject
43 internal class BoringKeyHandle : MonoBtlsHandle
45 internal BoringKeyHandle (IntPtr handle)
46 : base (handle, true)
50 protected override bool ReleaseHandle ()
52 mono_btls_key_free (handle);
53 return true;
58 [DllImport (BTLS_DYLIB)]
59 extern static IntPtr mono_btls_key_new ();
61 [DllImport (BTLS_DYLIB)]
62 extern static void mono_btls_key_free (IntPtr handle);
64 [DllImport (BTLS_DYLIB)]
65 extern static IntPtr mono_btls_key_up_ref (IntPtr handle);
67 [DllImport (BTLS_DYLIB)]
68 extern static int mono_btls_key_get_bytes (IntPtr handle, out IntPtr data, out int size, int include_private_bits);
70 [DllImport (BTLS_DYLIB)]
71 extern static int mono_btls_key_get_bits (IntPtr handle);
73 [DllImport (BTLS_DYLIB)]
74 extern static int mono_btls_key_is_rsa (IntPtr handle);
76 [DllImport (BTLS_DYLIB)]
77 extern static int mono_btls_key_assign_rsa_private_key (IntPtr handle, byte[] der, int der_length);
79 new internal BoringKeyHandle Handle {
80 get { return (BoringKeyHandle)base.Handle; }
83 internal MonoBtlsKey (BoringKeyHandle handle)
84 : base (handle)
88 public byte[] GetBytes (bool include_private_bits)
90 int size;
91 IntPtr data;
93 var ret = mono_btls_key_get_bytes (Handle.DangerousGetHandle (), out data, out size, include_private_bits ? 1 : 0);
94 CheckError (ret);
96 var buffer = new byte [size];
97 Marshal.Copy (data, buffer, 0, size);
98 FreeDataPtr (data);
99 return buffer;
102 public bool IsRsa {
103 get {
104 return mono_btls_key_is_rsa (Handle.DangerousGetHandle ()) != 0;
108 public MonoBtlsKey Copy ()
110 CheckThrow ();
111 var copy = mono_btls_key_up_ref (Handle.DangerousGetHandle ());
112 CheckError (copy != IntPtr.Zero);
113 return new MonoBtlsKey (new BoringKeyHandle (copy));
116 public static MonoBtlsKey CreateFromRSAPrivateKey (System.Security.Cryptography.RSA privateKey)
118 var keyData = MX.PKCS8.PrivateKeyInfo.Encode (privateKey);
119 var key = new MonoBtlsKey (new BoringKeyHandle (mono_btls_key_new ()));
121 var ret = mono_btls_key_assign_rsa_private_key (key.Handle.DangerousGetHandle (), keyData, keyData.Length);
122 if (ret == 0)
123 throw new MonoBtlsException ("Assigning private key failed.");
125 return key;
129 #endif