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
26 #if SECURITY_DEP && MONO_FEATURE_BTLS
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 int mono_btls_x509_lookup_init (IntPtr handle);
70 [DllImport (BTLS_DYLIB)]
71 extern static int mono_btls_x509_lookup_shutdown (IntPtr handle);
73 [DllImport (BTLS_DYLIB)]
74 extern static IntPtr mono_btls_x509_lookup_by_subject (IntPtr handle, IntPtr name);
76 [DllImport (BTLS_DYLIB)]
77 extern static IntPtr mono_btls_x509_lookup_by_fingerprint (IntPtr handle, IntPtr bytes, int len);
79 [DllImport (BTLS_DYLIB)]
80 extern static void mono_btls_x509_lookup_free (IntPtr handle);
82 [DllImport (BTLS_DYLIB)]
83 extern static IntPtr mono_btls_x509_lookup_peek_lookup (IntPtr handle);
85 MonoBtlsX509LookupType type;
86 List<MonoBtlsX509LookupMono> monoLookups;
90 internal MonoBtlsX509Lookup (BoringX509LookupHandle handle)
96 static BoringX509LookupHandle Create_internal (MonoBtlsX509Store store, MonoBtlsX509LookupType type)
98 var handle = mono_btls_x509_lookup_new (
99 store.Handle.DangerousGetHandle (), type);
100 if (handle == IntPtr.Zero)
101 throw new MonoBtlsException ();
102 return new BoringX509LookupHandle (handle);
105 internal MonoBtlsX509Lookup (MonoBtlsX509Store store, MonoBtlsX509LookupType type)
106 : base (Create_internal (store, type))
111 internal IntPtr GetNativeLookup ()
113 return mono_btls_x509_lookup_peek_lookup (Handle.DangerousGetHandle ());
116 public void LoadFile (string file, MonoBtlsX509FileType type)
118 IntPtr filePtr = IntPtr.Zero;
121 filePtr = Marshal.StringToHGlobalAnsi (file);
122 var ret = mono_btls_x509_lookup_load_file (
123 Handle.DangerousGetHandle (), filePtr, type);
126 if (filePtr != IntPtr.Zero)
127 Marshal.FreeHGlobal (filePtr);
131 public void AddDirectory (string dir, MonoBtlsX509FileType type)
133 IntPtr dirPtr = IntPtr.Zero;
136 dirPtr = Marshal.StringToHGlobalAnsi (dir);
137 var ret = mono_btls_x509_lookup_add_dir (
138 Handle.DangerousGetHandle (), dirPtr, type);
141 if (dirPtr != IntPtr.Zero)
142 Marshal.FreeHGlobal (dirPtr);
146 // Takes ownership of the 'monoLookup'.
147 internal void AddMono (MonoBtlsX509LookupMono monoLookup)
149 if (type != MonoBtlsX509LookupType.MONO)
150 throw new NotSupportedException ();
151 var ret = mono_btls_x509_lookup_add_mono (
152 Handle.DangerousGetHandle (), monoLookup.Handle.DangerousGetHandle ());
155 if (monoLookups == null)
156 monoLookups = new List<MonoBtlsX509LookupMono> ();
157 monoLookups.Add (monoLookup);
160 public void Initialize ()
162 var ret = mono_btls_x509_lookup_init (Handle.DangerousGetHandle ());
166 public void Shutdown ()
168 var ret = mono_btls_x509_lookup_shutdown (Handle.DangerousGetHandle ());
172 public MonoBtlsX509 LookupBySubject (MonoBtlsX509Name name)
174 var handle = mono_btls_x509_lookup_by_subject (
175 Handle.DangerousGetHandle (),
176 name.Handle.DangerousGetHandle ());
177 if (handle == IntPtr.Zero)
179 return new MonoBtlsX509 (new MonoBtlsX509.BoringX509Handle (handle));
182 public MonoBtlsX509 LookupByFingerPrint (byte[] fingerprint)
184 var bytes = Marshal.AllocHGlobal (fingerprint.Length);
186 Marshal.Copy (fingerprint, 0, bytes, fingerprint.Length);
187 var handle = mono_btls_x509_lookup_by_fingerprint (
188 Handle.DangerousGetHandle (),
189 bytes, fingerprint.Length);
190 if (handle == IntPtr.Zero)
192 return new MonoBtlsX509 (new MonoBtlsX509.BoringX509Handle (handle));
194 if (bytes != IntPtr.Zero)
195 Marshal.FreeHGlobal (bytes);
199 protected override void Close ()
202 if (monoLookups != null) {
203 foreach (var monoLookup in monoLookups)
204 monoLookup.Dispose ();