2 // MonoBtlsX509Lookup.cs
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
29 using System.Runtime.InteropServices;
30 using System.Runtime.CompilerServices;
31 using System.Collections.Generic;
35 class MonoBtlsX509Lookup : MonoBtlsObject
37 internal class BoringX509LookupHandle : MonoBtlsHandle
39 public BoringX509LookupHandle (IntPtr handle)
44 protected override bool ReleaseHandle ()
46 mono_btls_x509_lookup_free (handle);
51 new internal BoringX509LookupHandle Handle {
52 get { return (BoringX509LookupHandle)base.Handle; }
55 [DllImport (BTLS_DYLIB)]
56 extern static IntPtr mono_btls_x509_lookup_new (IntPtr store, MonoBtlsX509LookupType type);
58 [DllImport (BTLS_DYLIB)]
59 extern static int mono_btls_x509_lookup_load_file (IntPtr handle, IntPtr file, MonoBtlsX509FileType type);
61 [DllImport (BTLS_DYLIB)]
62 extern static int mono_btls_x509_lookup_add_dir (IntPtr handle, IntPtr dir, MonoBtlsX509FileType type);
64 [DllImport (BTLS_DYLIB)]
65 extern static int mono_btls_x509_lookup_add_mono (IntPtr handle, IntPtr monoLookup);
67 [DllImport (BTLS_DYLIB)]
68 extern static void mono_btls_x509_lookup_method_mono_init (
69 IntPtr handle, IntPtr instance, IntPtr by_subject_func);
71 [DllImport (BTLS_DYLIB)]
72 extern static int mono_btls_x509_lookup_init (IntPtr handle);
74 [DllImport (BTLS_DYLIB)]
75 extern static int mono_btls_x509_lookup_shutdown (IntPtr handle);
77 [DllImport (BTLS_DYLIB)]
78 extern static IntPtr mono_btls_x509_lookup_by_subject (IntPtr handle, IntPtr name);
80 [DllImport (BTLS_DYLIB)]
81 extern static IntPtr mono_btls_x509_lookup_by_fingerprint (IntPtr handle, IntPtr bytes, int len);
83 [DllImport (BTLS_DYLIB)]
84 extern static void mono_btls_x509_lookup_free (IntPtr handle);
86 [DllImport (BTLS_DYLIB)]
87 extern static IntPtr mono_btls_x509_lookup_peek_lookup (IntPtr handle);
89 MonoBtlsX509LookupType type;
90 List<MonoBtlsX509LookupMono> monoLookups;
94 internal MonoBtlsX509Lookup (BoringX509LookupHandle handle)
100 static BoringX509LookupHandle Create_internal (MonoBtlsX509Store store, MonoBtlsX509LookupType type)
102 var handle = mono_btls_x509_lookup_new (
103 store.Handle.DangerousGetHandle (), type);
104 if (handle == IntPtr.Zero)
105 throw new MonoBtlsException ();
106 return new BoringX509LookupHandle (handle);
109 internal MonoBtlsX509Lookup (MonoBtlsX509Store store, MonoBtlsX509LookupType type)
110 : base (Create_internal (store, type))
115 internal IntPtr GetNativeLookup ()
117 return mono_btls_x509_lookup_peek_lookup (Handle.DangerousGetHandle ());
120 public void LoadFile (string file, MonoBtlsX509FileType type)
122 IntPtr filePtr = IntPtr.Zero;
125 filePtr = Marshal.StringToHGlobalAnsi (file);
126 var ret = mono_btls_x509_lookup_load_file (
127 Handle.DangerousGetHandle (), filePtr, type);
130 if (filePtr != IntPtr.Zero)
131 Marshal.FreeHGlobal (filePtr);
135 public void AddDirectory (string dir, MonoBtlsX509FileType type)
137 IntPtr dirPtr = IntPtr.Zero;
140 dirPtr = Marshal.StringToHGlobalAnsi (dir);
141 var ret = mono_btls_x509_lookup_add_dir (
142 Handle.DangerousGetHandle (), dirPtr, type);
145 if (dirPtr != IntPtr.Zero)
146 Marshal.FreeHGlobal (dirPtr);
150 // Takes ownership of the 'monoLookup'.
151 internal void AddMono (MonoBtlsX509LookupMono monoLookup)
153 if (type != MonoBtlsX509LookupType.MONO)
154 throw new NotSupportedException ();
155 var ret = mono_btls_x509_lookup_add_mono (
156 Handle.DangerousGetHandle (), monoLookup.Handle.DangerousGetHandle ());
159 if (monoLookups == null)
160 monoLookups = new List<MonoBtlsX509LookupMono> ();
161 monoLookups.Add (monoLookup);
164 public void Initialize ()
166 var ret = mono_btls_x509_lookup_init (Handle.DangerousGetHandle ());
170 public void Shutdown ()
172 var ret = mono_btls_x509_lookup_shutdown (Handle.DangerousGetHandle ());
176 public MonoBtlsX509 LookupBySubject (MonoBtlsX509Name name)
178 var handle = mono_btls_x509_lookup_by_subject (
179 Handle.DangerousGetHandle (),
180 name.Handle.DangerousGetHandle ());
181 if (handle == IntPtr.Zero)
183 return new MonoBtlsX509 (new MonoBtlsX509.BoringX509Handle (handle));
186 public MonoBtlsX509 LookupByFingerPrint (byte[] fingerprint)
188 var bytes = Marshal.AllocHGlobal (fingerprint.Length);
190 Marshal.Copy (fingerprint, 0, bytes, fingerprint.Length);
191 var handle = mono_btls_x509_lookup_by_fingerprint (
192 Handle.DangerousGetHandle (),
193 bytes, fingerprint.Length);
194 if (handle == IntPtr.Zero)
196 return new MonoBtlsX509 (new MonoBtlsX509.BoringX509Handle (handle));
198 if (bytes != IntPtr.Zero)
199 Marshal.FreeHGlobal (bytes);
203 protected override void Close ()
206 if (monoLookups != null) {
207 foreach (var monoLookup in monoLookups)
208 monoLookup.Dispose ();