[btls] Convert BTLS icalls to pinvokes by invoking them using [DllImp… (#3799)
[mono.git] / mcs / class / System / Mono.Btls / MonoBtlsX509Store.cs
1 //
2 // MonoBtlsX509Store.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.Collections.Generic;
30 using System.Runtime.InteropServices;
31 using System.Runtime.CompilerServices;
32 using System.Security.Cryptography.X509Certificates;
33
34 namespace Mono.Btls
35 {
36         class MonoBtlsX509Store : MonoBtlsObject
37         {
38                 internal class BoringX509StoreHandle : MonoBtlsHandle
39                 {
40                         public BoringX509StoreHandle (IntPtr handle)
41                                 : base (handle, true)
42                         {
43                         }
44
45                         protected override bool ReleaseHandle ()
46                         {
47                                 mono_btls_x509_store_free (handle);
48                                 return true;
49                         }
50                 }
51
52                 new internal BoringX509StoreHandle Handle {
53                         get { return (BoringX509StoreHandle)base.Handle; }
54                 }
55
56                 [DllImport (BTLS_DYLIB)]
57                 extern static IntPtr mono_btls_x509_store_new ();
58
59                 [DllImport (BTLS_DYLIB)]
60                 extern static IntPtr mono_btls_x509_store_from_ctx (IntPtr ctx);
61
62                 [DllImport (BTLS_DYLIB)]
63                 extern static IntPtr mono_btls_x509_store_from_ssl_ctx (IntPtr handle);
64
65                 [DllImport (BTLS_DYLIB)]
66                 extern static int mono_btls_x509_store_load_locations (IntPtr handle, IntPtr file, IntPtr path);
67
68                 [DllImport (BTLS_DYLIB)]
69                 extern static int mono_btls_x509_store_set_default_paths (IntPtr handle);
70
71                 [DllImport (BTLS_DYLIB)]
72                 extern static int mono_btls_x509_store_add_cert (IntPtr handle, IntPtr x509);
73
74                 [DllImport (BTLS_DYLIB)]
75                 extern static int mono_btls_x509_store_get_count (IntPtr handle);
76
77                 [DllImport (BTLS_DYLIB)]
78                 extern static void mono_btls_x509_store_free (IntPtr handle);
79
80                 Dictionary<IntPtr,MonoBtlsX509Lookup> lookupHash;
81
82                 public void LoadLocations (string file, string path)
83                 {
84                         IntPtr filePtr = IntPtr.Zero;
85                         IntPtr pathPtr = IntPtr.Zero;
86                         try {
87                                 if (file != null)
88                                         filePtr = Marshal.StringToHGlobalAnsi (file);
89                                 if (path != null)
90                                         pathPtr = Marshal.StringToHGlobalAnsi (path);
91                                 var ret = mono_btls_x509_store_load_locations (
92                                         Handle.DangerousGetHandle (), filePtr, pathPtr);
93                                 CheckError (ret);
94                         } finally {
95                                 if (filePtr != IntPtr.Zero)
96                                         Marshal.FreeHGlobal (filePtr);
97                                 if (pathPtr != IntPtr.Zero)
98                                         Marshal.FreeHGlobal (pathPtr);
99                         }
100                 }
101
102                 public void SetDefaultPaths ()
103                 {
104                         var ret = mono_btls_x509_store_set_default_paths (Handle.DangerousGetHandle ());
105                         CheckError (ret);
106                 }
107
108                 static BoringX509StoreHandle Create_internal ()
109                 {
110                         var handle = mono_btls_x509_store_new ();
111                         if (handle == IntPtr.Zero)
112                                 throw new MonoBtlsException ();
113                         return new BoringX509StoreHandle (handle);
114                 }
115
116                 static BoringX509StoreHandle Create_internal (IntPtr store_ctx)
117                 {
118                         var handle = mono_btls_x509_store_from_ssl_ctx (store_ctx);
119                         if (handle == IntPtr.Zero)
120                                 throw new MonoBtlsException ();
121                         return new BoringX509StoreHandle (handle);
122                 }
123
124                 static BoringX509StoreHandle Create_internal (MonoBtlsSslCtx.BoringSslCtxHandle ctx)
125                 {
126                         var handle = mono_btls_x509_store_from_ssl_ctx (ctx.DangerousGetHandle ());
127                         if (handle == IntPtr.Zero)
128                                 throw new MonoBtlsException ();
129                         return new BoringX509StoreHandle (handle);
130                 }
131
132                 internal MonoBtlsX509Store ()
133                         : base (Create_internal ())
134                 {
135                 }
136
137                 internal MonoBtlsX509Store (IntPtr store_ctx)
138                         : base (Create_internal (store_ctx))
139                 {
140                 }
141
142                 internal MonoBtlsX509Store (MonoBtlsSslCtx.BoringSslCtxHandle ctx)
143                         : base (Create_internal (ctx))
144                 {
145                 }
146
147                 public void AddCertificate (MonoBtlsX509 x509)
148                 {
149                         var ret = mono_btls_x509_store_add_cert (
150                                 Handle.DangerousGetHandle (),
151                                 x509.Handle.DangerousGetHandle ());
152                         CheckError (ret);
153                 }
154
155                 public int GetCount ()
156                 {
157                         return mono_btls_x509_store_get_count (Handle.DangerousGetHandle ());
158                 }
159
160                 internal void AddTrustedRoots ()
161                 {
162                         var systemRoot = MonoBtlsProvider.GetSystemStoreLocation ();
163                         LoadLocations (null, systemRoot);
164                 }
165
166                 public MonoBtlsX509Lookup AddLookup (MonoBtlsX509LookupType type)
167                 {
168                         if (lookupHash == null)
169                                 lookupHash = new Dictionary<IntPtr,MonoBtlsX509Lookup> ();
170
171                         /*
172                          * X509_STORE_add_lookup() returns the same 'X509_LOOKUP *' for each
173                          * unique 'X509_LOOKUP_METHOD *' (which is supposed to be a static struct)
174                          * and we want to use the same managed object for each unique 'X509_LOOKUP *'.
175                         */
176                         var lookup = new MonoBtlsX509Lookup (this, type);
177                         var nativeLookup = lookup.GetNativeLookup ();
178                         if (lookupHash.ContainsKey (nativeLookup)) {
179                                 lookup.Dispose ();
180                                 lookup = lookupHash [nativeLookup];
181                         } else {
182                                 lookupHash.Add (nativeLookup, lookup);
183                         }
184
185                         return lookup;
186                 }
187
188                 public void AddDirectoryLookup (string dir, MonoBtlsX509FileType type)
189                 {
190                         var lookup = AddLookup (MonoBtlsX509LookupType.HASH_DIR);
191                         lookup.AddDirectory (dir, type);
192                 }
193
194                 public void AddFileLookup (string file, MonoBtlsX509FileType type)
195                 {
196                         var lookup = AddLookup (MonoBtlsX509LookupType.FILE);
197                         lookup.LoadFile (file, type);
198                 }
199
200                 public void AddCollection (X509CertificateCollection collection, MonoBtlsX509TrustKind trust)
201                 {
202                         var monoLookup = new MonoBtlsX509LookupMonoCollection (collection, trust);
203                         var lookup = new MonoBtlsX509Lookup (this, MonoBtlsX509LookupType.MONO);
204                         lookup.AddMono (monoLookup);
205                 }
206
207 #if MONODROID
208                 public void AddAndroidLookup ()
209                 {
210                         var androidLookup = new MonoBtlsX509LookupAndroid ();
211                         var lookup = new MonoBtlsX509Lookup (this, MonoBtlsX509LookupType.MONO);
212                         lookup.AddMono (androidLookup);
213                 }
214 #endif
215
216                 protected override void Close ()
217                 {
218                         try {
219                                 if (lookupHash != null) {
220                                         foreach (var lookup in lookupHash.Values)
221                                                 lookup.Dispose ();
222                                         lookupHash = null;
223                                 }
224                         } finally {
225                                 base.Close ();
226                         }
227                 }
228         }
229 }
230 #endif