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
26 #if SECURITY_DEP && MONO_FEATURE_BTLS
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;
49 static string machineTrustedRootPath;
50 static string machineIntermediateCAPath;
51 static string machineUntrustedPath;
52 static string userTrustedRootPath;
53 static string userIntermediateCAPath;
54 static string userUntrustedPath;
57 static void Initialize ()
64 } catch (Exception ex) {
65 Console.Error.WriteLine ("MonoBtlsX509StoreManager.Initialize() threw exception: {0}", ex);
71 static void DoInitialize ()
74 var userPath = MX.X509StoreManager.NewCurrentUserPath;
75 userTrustedRootPath = Path.Combine (userPath, MX.X509Stores.Names.TrustedRoot);
76 userIntermediateCAPath = Path.Combine (userPath, MX.X509Stores.Names.IntermediateCA);
77 userUntrustedPath = Path.Combine (userPath, MX.X509Stores.Names.Untrusted);
79 var machinePath = MX.X509StoreManager.NewLocalMachinePath;
80 machineTrustedRootPath = Path.Combine (userPath, MX.X509Stores.Names.TrustedRoot);
81 machineIntermediateCAPath = Path.Combine (userPath, MX.X509Stores.Names.IntermediateCA);
82 machineUntrustedPath = Path.Combine (userPath, MX.X509Stores.Names.Untrusted);
86 public static bool HasStore (MonoBtlsX509StoreType type)
91 var path = GetStorePath (type);
92 return path != null && Directory.Exists (path);
96 public static string GetStorePath (MonoBtlsX509StoreType type)
99 throw new NotSupportedException ();
103 case MonoBtlsX509StoreType.MachineTrustedRoots:
104 return machineTrustedRootPath;
105 case MonoBtlsX509StoreType.MachineIntermediateCA:
106 return machineIntermediateCAPath;
107 case MonoBtlsX509StoreType.MachineUntrusted:
108 return machineUntrustedPath;
109 case MonoBtlsX509StoreType.UserTrustedRoots:
110 return userTrustedRootPath;
111 case MonoBtlsX509StoreType.UserIntermediateCA:
112 return userIntermediateCAPath;
113 case MonoBtlsX509StoreType.UserUntrusted:
114 return userUntrustedPath;
116 throw new NotSupportedException ();