Implement mono_gc_alloc_fixed on Boehm to be uncollectable. This matches SGen behavio...
[mono.git] / mcs / class / System / Mono.Btls / MonoBtlsX509Lookup.cs
1 //
2 // MonoBtlsX509Lookup.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 && MONO_FEATURE_BTLS
27 using System;
28 using System.IO;
29 using System.Runtime.InteropServices;
30 using System.Runtime.CompilerServices;
31 using System.Collections.Generic;
32
33 namespace Mono.Btls
34 {
35         class MonoBtlsX509Lookup : MonoBtlsObject
36         {
37                 internal class BoringX509LookupHandle : MonoBtlsHandle
38                 {
39                         public BoringX509LookupHandle (IntPtr handle)
40                                 : base (handle, true)
41                         {
42                         }
43
44                         protected override bool ReleaseHandle ()
45                         {
46                                 mono_btls_x509_lookup_free (handle);
47                                 return true;
48                         }
49                 }
50
51                 new internal BoringX509LookupHandle Handle {
52                         get { return (BoringX509LookupHandle)base.Handle; }
53                 }
54
55                 [DllImport (BTLS_DYLIB)]
56                 extern static IntPtr mono_btls_x509_lookup_new (IntPtr store, MonoBtlsX509LookupType type);
57
58                 [DllImport (BTLS_DYLIB)]
59                 extern static int mono_btls_x509_lookup_load_file (IntPtr handle, IntPtr file, MonoBtlsX509FileType type);
60
61                 [DllImport (BTLS_DYLIB)]
62                 extern static int mono_btls_x509_lookup_add_dir (IntPtr handle, IntPtr dir, MonoBtlsX509FileType type);
63
64                 [DllImport (BTLS_DYLIB)]
65                 extern static int mono_btls_x509_lookup_add_mono (IntPtr handle, IntPtr monoLookup);
66
67                 [DllImport (BTLS_DYLIB)]
68                 extern static int mono_btls_x509_lookup_init (IntPtr handle);
69
70                 [DllImport (BTLS_DYLIB)]
71                 extern static int mono_btls_x509_lookup_shutdown (IntPtr handle);
72
73                 [DllImport (BTLS_DYLIB)]
74                 extern static IntPtr mono_btls_x509_lookup_by_subject (IntPtr handle, IntPtr name);
75
76                 [DllImport (BTLS_DYLIB)]
77                 extern static IntPtr mono_btls_x509_lookup_by_fingerprint (IntPtr handle, IntPtr bytes, int len);
78
79                 [DllImport (BTLS_DYLIB)]
80                 extern static void mono_btls_x509_lookup_free (IntPtr handle);
81
82                 [DllImport (BTLS_DYLIB)]
83                 extern static IntPtr mono_btls_x509_lookup_peek_lookup (IntPtr handle);
84
85                 MonoBtlsX509LookupType type;
86                 List<MonoBtlsX509LookupMono> monoLookups;
87
88 #if FIXME
89                 // Do we need this?
90                 internal MonoBtlsX509Lookup (BoringX509LookupHandle handle)
91                         : base (handle)
92                 {
93                 }
94 #endif
95
96                 static BoringX509LookupHandle Create_internal (MonoBtlsX509Store store, MonoBtlsX509LookupType type)
97                 {
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);
103                 }
104
105                 internal MonoBtlsX509Lookup (MonoBtlsX509Store store, MonoBtlsX509LookupType type)
106                         : base (Create_internal (store, type))
107                 {
108                         this.type = type;
109                 }
110
111                 internal IntPtr GetNativeLookup ()
112                 {
113                         return mono_btls_x509_lookup_peek_lookup (Handle.DangerousGetHandle ());
114                 }
115
116                 public void LoadFile (string file, MonoBtlsX509FileType type)
117                 {
118                         IntPtr filePtr = IntPtr.Zero;
119                         try {
120                                 if (file != null)
121                                         filePtr = Marshal.StringToHGlobalAnsi (file);
122                                 var ret = mono_btls_x509_lookup_load_file (
123                                         Handle.DangerousGetHandle (), filePtr, type);
124                                 CheckError (ret);
125                         } finally {
126                                 if (filePtr != IntPtr.Zero)
127                                         Marshal.FreeHGlobal (filePtr);
128                         }
129                 }
130
131                 public void AddDirectory (string dir, MonoBtlsX509FileType type)
132                 {
133                         IntPtr dirPtr = IntPtr.Zero;
134                         try {
135                                 if (dir != null)
136                                         dirPtr = Marshal.StringToHGlobalAnsi (dir);
137                                 var ret = mono_btls_x509_lookup_add_dir (
138                                         Handle.DangerousGetHandle (), dirPtr, type);
139                                 CheckError (ret);
140                         } finally {
141                                 if (dirPtr != IntPtr.Zero)
142                                         Marshal.FreeHGlobal (dirPtr);
143                         }
144                 }
145
146                 // Takes ownership of the 'monoLookup'.
147                 internal void AddMono (MonoBtlsX509LookupMono monoLookup)
148                 {
149                         if (type != MonoBtlsX509LookupType.MONO)
150                                 throw new NotSupportedException ();
151                         var ret = mono_btls_x509_lookup_add_mono (
152                                 Handle.DangerousGetHandle (), monoLookup.Handle.DangerousGetHandle ());
153                         CheckError (ret);
154
155                         if (monoLookups == null)
156                                 monoLookups = new List<MonoBtlsX509LookupMono> ();
157                         monoLookups.Add (monoLookup);
158                 }
159
160                 public void Initialize ()
161                 {
162                         var ret = mono_btls_x509_lookup_init (Handle.DangerousGetHandle ());
163                         CheckError (ret);
164                 }
165
166                 public void Shutdown ()
167                 {
168                         var ret = mono_btls_x509_lookup_shutdown (Handle.DangerousGetHandle ());
169                         CheckError (ret);
170                 }
171
172                 public MonoBtlsX509 LookupBySubject (MonoBtlsX509Name name)
173                 {
174                         var handle = mono_btls_x509_lookup_by_subject (
175                                 Handle.DangerousGetHandle (),
176                                 name.Handle.DangerousGetHandle ());
177                         if (handle == IntPtr.Zero)
178                                 return null;
179                         return new MonoBtlsX509 (new MonoBtlsX509.BoringX509Handle (handle));
180                 }
181
182                 public MonoBtlsX509 LookupByFingerPrint (byte[] fingerprint)
183                 {
184                         var bytes = Marshal.AllocHGlobal (fingerprint.Length);
185                         try {
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)
191                                         return null;
192                                 return new MonoBtlsX509 (new MonoBtlsX509.BoringX509Handle (handle));
193                         } finally {
194                                 if (bytes != IntPtr.Zero)
195                                         Marshal.FreeHGlobal (bytes);
196                         }
197                 }
198
199                 protected override void Close ()
200                 {
201                         try {
202                                 if (monoLookups != null) {
203                                         foreach (var monoLookup in monoLookups)
204                                                 monoLookup.Dispose ();
205                                 monoLookups = null;
206                                 }
207                         } finally {
208                                 base.Close ();
209                         }
210                 }
211         }
212 }
213 #endif