2 // MonoBtlsX509LookupMono.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;
34 abstract class MonoBtlsX509LookupMono : MonoBtlsObject
36 internal class BoringX509LookupMonoHandle : MonoBtlsHandle
38 public BoringX509LookupMonoHandle (IntPtr handle)
43 protected override bool ReleaseHandle ()
45 mono_btls_x509_lookup_mono_free (handle);
50 new internal BoringX509LookupMonoHandle Handle {
51 get { return (BoringX509LookupMonoHandle)base.Handle; }
54 [MethodImpl (MethodImplOptions.InternalCall)]
55 extern static IntPtr mono_btls_x509_lookup_mono_new ();
57 [MethodImpl (MethodImplOptions.InternalCall)]
58 extern static void mono_btls_x509_lookup_mono_init (
59 IntPtr handle, IntPtr instance, IntPtr by_subject_func);
61 [MethodImpl (MethodImplOptions.InternalCall)]
62 extern static int mono_btls_x509_lookup_mono_free (IntPtr handle);
64 delegate int BySubjectFunc (IntPtr instance, IntPtr name, out IntPtr x509_ptr);
68 BySubjectFunc bySubjectFunc;
69 IntPtr bySubjectFuncPtr;
71 internal MonoBtlsX509LookupMono ()
72 : base (new BoringX509LookupMonoHandle (mono_btls_x509_lookup_mono_new ()))
74 gch = GCHandle.Alloc (this);
75 instance = GCHandle.ToIntPtr (gch);
76 bySubjectFunc = OnGetBySubject;
77 bySubjectFuncPtr = Marshal.GetFunctionPointerForDelegate (bySubjectFunc);
78 mono_btls_x509_lookup_mono_init (Handle.DangerousGetHandle (), instance, bySubjectFuncPtr);
81 protected abstract MonoBtlsX509 OnGetBySubject (MonoBtlsX509Name name);
84 [MonoTouch.MonoPInvokeCallback (typeof (BySubjectFunc))]
86 static int OnGetBySubject (IntPtr instance, IntPtr name_ptr, out IntPtr x509_ptr)
89 MonoBtlsX509LookupMono obj;
90 MonoBtlsX509Name.BoringX509NameHandle name_handle = null;
92 obj = (MonoBtlsX509LookupMono)GCHandle.FromIntPtr (instance).Target;
93 name_handle = new MonoBtlsX509Name.BoringX509NameHandle (name_ptr, false);
94 MonoBtlsX509Name name_obj = new MonoBtlsX509Name (name_handle);
95 var x509 = obj.OnGetBySubject (name_obj);
97 x509_ptr = x509.Handle.StealHandle ();
100 x509_ptr = IntPtr.Zero;
104 if (name_handle != null)
105 name_handle.Dispose ();
107 } catch (Exception ex) {
108 Console.WriteLine ("LOOKUP METHOD - GET BY SUBJECT EX: {0}", ex);
109 x509_ptr = IntPtr.Zero;
114 protected override void Close ()
120 instance = IntPtr.Zero;
121 bySubjectFunc = null;
122 bySubjectFuncPtr = IntPtr.Zero;