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 MonoBtlsX509Name : MonoBtlsObject
37 internal class BoringX509NameHandle : MonoBtlsHandle
41 internal BoringX509NameHandle (IntPtr handle, bool ownsHandle)
42 : base (handle, ownsHandle)
44 this.dontFree = !ownsHandle;
47 protected override bool ReleaseHandle ()
50 mono_btls_x509_name_free (handle);
55 [DllImport (BTLS_DYLIB)]
56 extern static int mono_btls_x509_name_print_bio (IntPtr handle, IntPtr bio);
58 [DllImport (BTLS_DYLIB)]
59 extern static int mono_btls_x509_name_print_string (IntPtr handle, IntPtr buffer, int size);
61 [DllImport (BTLS_DYLIB)]
62 extern static int mono_btls_x509_name_get_raw_data (IntPtr handle, out IntPtr buffer, int use_canon_enc);
64 [DllImport (BTLS_DYLIB)]
65 extern static long mono_btls_x509_name_hash (IntPtr handle);
67 [DllImport (BTLS_DYLIB)]
68 extern static long mono_btls_x509_name_hash_old (IntPtr handle);
70 [DllImport (BTLS_DYLIB)]
71 extern static int mono_btls_x509_name_get_entry_count (IntPtr handle);
73 [DllImport (BTLS_DYLIB)]
74 extern static MonoBtlsX509NameEntryType mono_btls_x509_name_get_entry_type (IntPtr name, int index);
76 [DllImport (BTLS_DYLIB)]
77 extern static int mono_btls_x509_name_get_entry_oid (IntPtr name, int index, IntPtr buffer, int size);
79 [DllImport (BTLS_DYLIB)]
80 extern static int mono_btls_x509_name_get_entry_oid_data (IntPtr name, int index, out IntPtr data);
82 [DllImport (BTLS_DYLIB)]
83 extern static int mono_btls_x509_name_get_entry_value (IntPtr name, int index, out int tag, out IntPtr str);
85 [DllImport (BTLS_DYLIB)]
86 extern unsafe static IntPtr mono_btls_x509_name_from_data (void* data, int len, int use_canon_enc);
88 [DllImport (BTLS_DYLIB)]
89 extern static void mono_btls_x509_name_free (IntPtr handle);
91 new internal BoringX509NameHandle Handle {
92 get { return (BoringX509NameHandle)base.Handle; }
95 internal MonoBtlsX509Name (BoringX509NameHandle handle)
100 public string GetString ()
102 const int size = 4096;
103 var data = Marshal.AllocHGlobal (size);
105 var ret = mono_btls_x509_name_print_string (
106 Handle.DangerousGetHandle (), data, size);
108 return Marshal.PtrToStringAnsi (data);
110 Marshal.FreeHGlobal (data);
114 public void PrintBio (MonoBtlsBio bio)
116 var ret = mono_btls_x509_name_print_bio (
117 Handle.DangerousGetHandle (),
118 bio.Handle.DangerousGetHandle ());
122 public byte[] GetRawData (bool use_canon_enc)
125 var ret = mono_btls_x509_name_get_raw_data (
126 Handle.DangerousGetHandle (),
127 out data, use_canon_enc ? 1 : 0);
128 CheckError (ret > 0);
129 var buffer = new byte [ret];
130 Marshal.Copy (data, buffer, 0, ret);
135 public long GetHash ()
137 return mono_btls_x509_name_hash (Handle.DangerousGetHandle ());
140 public long GetHashOld ()
142 return mono_btls_x509_name_hash_old (Handle.DangerousGetHandle ());
145 public int GetEntryCount ()
147 return mono_btls_x509_name_get_entry_count (Handle.DangerousGetHandle ());
150 public MonoBtlsX509NameEntryType GetEntryType (int index)
152 if (index >= GetEntryCount ())
153 throw new ArgumentOutOfRangeException ();
154 return mono_btls_x509_name_get_entry_type (
155 Handle.DangerousGetHandle (), index);
158 public string GetEntryOid (int index)
160 if (index >= GetEntryCount ())
161 throw new ArgumentOutOfRangeException ();
163 const int size = 4096;
164 var data = Marshal.AllocHGlobal (size);
166 var ret = mono_btls_x509_name_get_entry_oid (
167 Handle.DangerousGetHandle (),
169 CheckError (ret > 0);
170 return Marshal.PtrToStringAnsi (data);
172 Marshal.FreeHGlobal (data);
176 public byte[] GetEntryOidData (int index)
179 var ret = mono_btls_x509_name_get_entry_oid_data (
180 Handle.DangerousGetHandle (), index, out data);
181 CheckError (ret > 0);
183 var bytes = new byte[ret];
184 Marshal.Copy (data, bytes, 0, ret);
188 public unsafe string GetEntryValue (int index, out int tag)
190 if (index >= GetEntryCount ())
191 throw new ArgumentOutOfRangeException ();
193 var ret = mono_btls_x509_name_get_entry_value (
194 Handle.DangerousGetHandle (), index, out tag, out data);
198 return new UTF8Encoding ().GetString ((byte*)data, ret);
200 if (data != IntPtr.Zero)
205 public static unsafe MonoBtlsX509Name CreateFromData (byte[] data, bool use_canon_enc)
207 fixed (void *ptr = data) {
208 var handle = mono_btls_x509_name_from_data (ptr, data.Length, use_canon_enc ? 1 : 0);
209 if (handle == IntPtr.Zero)
210 throw new MonoBtlsException ("mono_btls_x509_name_from_data() failed.");
211 return new MonoBtlsX509Name (new BoringX509NameHandle (handle, false));