30b2ee3876e4480e4c66d3499bae0158c800da06
[mono.git] / mcs / class / System / Mono.Btls / MonoBtlsX509LookupMono.cs
1 //
2 // MonoBtlsX509LookupMono.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:
15 //
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
18 //
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
27 using System;
28 using System.IO;
29 using System.Runtime.InteropServices;
30 using System.Runtime.CompilerServices;
31
32 namespace Mono.Btls
33 {
34         abstract class MonoBtlsX509LookupMono : MonoBtlsObject
35         {
36                 internal class BoringX509LookupMonoHandle : MonoBtlsHandle
37                 {
38                         public BoringX509LookupMonoHandle (IntPtr handle)
39                                 : base (handle, true)
40                         {
41                         }
42
43                         protected override bool ReleaseHandle ()
44                         {
45                                 mono_btls_x509_lookup_mono_free (handle);
46                                 return true;
47                         }
48                 }
49
50                 new internal BoringX509LookupMonoHandle Handle {
51                         get { return (BoringX509LookupMonoHandle)base.Handle; }
52                 }
53
54                 [MethodImpl (MethodImplOptions.InternalCall)]
55                 extern static IntPtr mono_btls_x509_lookup_mono_new ();
56
57                 [MethodImpl (MethodImplOptions.InternalCall)]
58                 extern static void mono_btls_x509_lookup_mono_init (
59                         IntPtr handle, IntPtr instance, IntPtr by_subject_func);
60
61                 [MethodImpl (MethodImplOptions.InternalCall)]
62                 extern static int mono_btls_x509_lookup_mono_free (IntPtr handle);
63
64                 delegate int BySubjectFunc (IntPtr instance, IntPtr name, out IntPtr x509_ptr);
65
66                 GCHandle gch;
67                 IntPtr instance;
68                 BySubjectFunc bySubjectFunc;
69                 IntPtr bySubjectFuncPtr;
70
71                 internal MonoBtlsX509LookupMono ()
72                         : base (new BoringX509LookupMonoHandle (mono_btls_x509_lookup_mono_new ()))
73                 {
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);
79                 }
80
81                 protected abstract MonoBtlsX509 OnGetBySubject (MonoBtlsX509Name name);
82
83 #if MONOTOUCH
84                 [MonoTouch.MonoPInvokeCallback (typeof (BySubjectFunc))]
85 #endif
86                 static int OnGetBySubject (IntPtr instance, IntPtr name_ptr, out IntPtr x509_ptr)
87                 {
88                         try {
89                                 MonoBtlsX509LookupMono obj;
90                                 MonoBtlsX509Name.BoringX509NameHandle name_handle = null;
91                                 try {
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);
96                                         if (x509 != null) {
97                                                 x509_ptr = x509.Handle.StealHandle ();
98                                                 return 1;
99                                         } else {
100                                                 x509_ptr = IntPtr.Zero;
101                                                 return 0;
102                                         }
103                                 } finally {
104                                         if (name_handle != null)
105                                                 name_handle.Dispose ();
106                                 }
107                         } catch (Exception ex) {
108                                 Console.WriteLine ("LOOKUP METHOD - GET BY SUBJECT EX: {0}", ex);
109                                 x509_ptr = IntPtr.Zero;
110                                 return 0;
111                         }
112                 }
113
114                 protected override void Close ()
115                 {
116                         try {
117                                 if (gch.IsAllocated)
118                                         gch.Free ();
119                         } finally {
120                                 instance = IntPtr.Zero;
121                                 bySubjectFunc = null;
122                                 bySubjectFuncPtr = IntPtr.Zero;
123                                 base.Close ();
124                         }
125                 }
126         }
127 }
128 #endif