2 // MonoBtlsX509Store.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.Collections.Generic;
30 using System.Runtime.InteropServices;
31 using System.Runtime.CompilerServices;
32 using System.Security.Cryptography.X509Certificates;
36 class MonoBtlsX509Store : MonoBtlsObject
38 internal class BoringX509StoreHandle : MonoBtlsHandle
40 public BoringX509StoreHandle (IntPtr handle)
45 protected override bool ReleaseHandle ()
47 mono_btls_x509_store_free (handle);
52 new internal BoringX509StoreHandle Handle {
53 get { return (BoringX509StoreHandle)base.Handle; }
56 [DllImport (BTLS_DYLIB)]
57 extern static IntPtr mono_btls_x509_store_new ();
59 [DllImport (BTLS_DYLIB)]
60 extern static IntPtr mono_btls_x509_store_from_ctx (IntPtr ctx);
62 [DllImport (BTLS_DYLIB)]
63 extern static IntPtr mono_btls_x509_store_from_ssl_ctx (IntPtr handle);
65 [DllImport (BTLS_DYLIB)]
66 extern static int mono_btls_x509_store_load_locations (IntPtr handle, IntPtr file, IntPtr path);
68 [DllImport (BTLS_DYLIB)]
69 extern static int mono_btls_x509_store_set_default_paths (IntPtr handle);
71 [DllImport (BTLS_DYLIB)]
72 extern static int mono_btls_x509_store_add_cert (IntPtr handle, IntPtr x509);
74 [DllImport (BTLS_DYLIB)]
75 extern static int mono_btls_x509_store_get_count (IntPtr handle);
77 [DllImport (BTLS_DYLIB)]
78 extern static void mono_btls_x509_store_free (IntPtr handle);
80 Dictionary<IntPtr,MonoBtlsX509Lookup> lookupHash;
82 public void LoadLocations (string file, string path)
84 IntPtr filePtr = IntPtr.Zero;
85 IntPtr pathPtr = IntPtr.Zero;
88 filePtr = Marshal.StringToHGlobalAnsi (file);
90 pathPtr = Marshal.StringToHGlobalAnsi (path);
91 var ret = mono_btls_x509_store_load_locations (
92 Handle.DangerousGetHandle (), filePtr, pathPtr);
95 if (filePtr != IntPtr.Zero)
96 Marshal.FreeHGlobal (filePtr);
97 if (pathPtr != IntPtr.Zero)
98 Marshal.FreeHGlobal (pathPtr);
102 public void SetDefaultPaths ()
104 var ret = mono_btls_x509_store_set_default_paths (Handle.DangerousGetHandle ());
108 static BoringX509StoreHandle Create_internal ()
110 var handle = mono_btls_x509_store_new ();
111 if (handle == IntPtr.Zero)
112 throw new MonoBtlsException ();
113 return new BoringX509StoreHandle (handle);
116 static BoringX509StoreHandle Create_internal (IntPtr store_ctx)
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);
124 static BoringX509StoreHandle Create_internal (MonoBtlsSslCtx.BoringSslCtxHandle ctx)
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);
132 internal MonoBtlsX509Store ()
133 : base (Create_internal ())
137 internal MonoBtlsX509Store (IntPtr store_ctx)
138 : base (Create_internal (store_ctx))
142 internal MonoBtlsX509Store (MonoBtlsSslCtx.BoringSslCtxHandle ctx)
143 : base (Create_internal (ctx))
147 public void AddCertificate (MonoBtlsX509 x509)
149 var ret = mono_btls_x509_store_add_cert (
150 Handle.DangerousGetHandle (),
151 x509.Handle.DangerousGetHandle ());
155 public int GetCount ()
157 return mono_btls_x509_store_get_count (Handle.DangerousGetHandle ());
160 internal void AddTrustedRoots ()
162 MonoBtlsProvider.SetupCertificateStore (this);
165 public MonoBtlsX509Lookup AddLookup (MonoBtlsX509LookupType type)
167 if (lookupHash == null)
168 lookupHash = new Dictionary<IntPtr,MonoBtlsX509Lookup> ();
171 * X509_STORE_add_lookup() returns the same 'X509_LOOKUP *' for each
172 * unique 'X509_LOOKUP_METHOD *' (which is supposed to be a static struct)
173 * and we want to use the same managed object for each unique 'X509_LOOKUP *'.
175 var lookup = new MonoBtlsX509Lookup (this, type);
176 var nativeLookup = lookup.GetNativeLookup ();
177 if (lookupHash.ContainsKey (nativeLookup)) {
179 lookup = lookupHash [nativeLookup];
181 lookupHash.Add (nativeLookup, lookup);
187 public void AddDirectoryLookup (string dir, MonoBtlsX509FileType type)
189 var lookup = AddLookup (MonoBtlsX509LookupType.HASH_DIR);
190 lookup.AddDirectory (dir, type);
193 public void AddFileLookup (string file, MonoBtlsX509FileType type)
195 var lookup = AddLookup (MonoBtlsX509LookupType.FILE);
196 lookup.LoadFile (file, type);
199 public void AddCollection (X509CertificateCollection collection, MonoBtlsX509TrustKind trust)
201 var monoLookup = new MonoBtlsX509LookupMonoCollection (collection, trust);
202 var lookup = new MonoBtlsX509Lookup (this, MonoBtlsX509LookupType.MONO);
203 lookup.AddMono (monoLookup);
207 public void AddAndroidLookup ()
209 var androidLookup = new MonoBtlsX509LookupAndroid ();
210 var lookup = new MonoBtlsX509Lookup (this, MonoBtlsX509LookupType.MONO);
211 lookup.AddMono (androidLookup);
215 protected override void Close ()
218 if (lookupHash != null) {
219 foreach (var lookup in lookupHash.Values)