2 // MonoBtlsX509StoreManager.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
27 #if MONO_SECURITY_ALIAS
28 extern alias MonoSecurity;
33 using System.Security.Cryptography.X509Certificates;
35 #if MONO_SECURITY_ALIAS
36 using MonoSecurity::Mono.Security.Interface;
37 using MX = MonoSecurity::Mono.Security.X509;
39 using Mono.Security.Interface;
40 using MX = Mono.Security.X509;
45 static class MonoBtlsX509StoreManager
47 static bool initialized;
48 static string machineTrustedRootPath;
49 static string machineIntermediateCAPath;
50 static string machineUntrustedPath;
51 static string userTrustedRootPath;
52 static string userIntermediateCAPath;
53 static string userUntrustedPath;
55 static void Initialize ()
62 } catch (Exception ex) {
63 Console.Error.WriteLine ("MonoBtlsX509StoreManager.Initialize() threw exception: {0}", ex);
69 static void DoInitialize ()
72 var userPath = MX.X509StoreManager.NewCurrentUserPath;
73 userTrustedRootPath = Path.Combine (userPath, MX.X509Stores.Names.TrustedRoot);
74 userIntermediateCAPath = Path.Combine (userPath, MX.X509Stores.Names.IntermediateCA);
75 userUntrustedPath = Path.Combine (userPath, MX.X509Stores.Names.Untrusted);
77 var machinePath = MX.X509StoreManager.NewLocalMachinePath;
78 machineTrustedRootPath = Path.Combine (userPath, MX.X509Stores.Names.TrustedRoot);
79 machineIntermediateCAPath = Path.Combine (userPath, MX.X509Stores.Names.IntermediateCA);
80 machineUntrustedPath = Path.Combine (userPath, MX.X509Stores.Names.Untrusted);
84 public static bool HasStore (MonoBtlsX509StoreType type)
89 var path = GetStorePath (type);
90 return path != null && Directory.Exists (path);
94 public static string GetStorePath (MonoBtlsX509StoreType type)
97 throw new NotSupportedException ();
101 case MonoBtlsX509StoreType.MachineTrustedRoots:
102 return machineTrustedRootPath;
103 case MonoBtlsX509StoreType.MachineIntermediateCA:
104 return machineIntermediateCAPath;
105 case MonoBtlsX509StoreType.MachineUntrusted:
106 return machineUntrustedPath;
107 case MonoBtlsX509StoreType.UserTrustedRoots:
108 return userTrustedRootPath;
109 case MonoBtlsX509StoreType.UserIntermediateCA:
110 return userIntermediateCAPath;
111 case MonoBtlsX509StoreType.UserUntrusted:
112 return userUntrustedPath;
114 throw new NotSupportedException ();