13c92a29ef0560a9db89b0ae81a2b2af5e2e2e66
[mono.git] / mcs / class / System / Mono.Btls / MonoBtlsX509StoreManager.cs
1 //
2 // MonoBtlsX509StoreManager.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 #if MONO_SECURITY_ALIAS
28 extern alias MonoSecurity;
29 #endif
30
31 using System;
32 using System.IO;
33 using System.Security.Cryptography.X509Certificates;
34
35 #if MONO_SECURITY_ALIAS
36 using MonoSecurity::Mono.Security.Interface;
37 using MX = MonoSecurity::Mono.Security.X509;
38 #else
39 using Mono.Security.Interface;
40 using MX = Mono.Security.X509;
41 #endif
42
43 namespace Mono.Btls
44 {
45         static class MonoBtlsX509StoreManager
46         {
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;
54
55                 static void Initialize ()
56                 {
57                         if (initialized)
58                                 return;
59
60                         try {
61                                 DoInitialize ();
62                         } catch (Exception ex) {
63                                 Console.Error.WriteLine ("MonoBtlsX509StoreManager.Initialize() threw exception: {0}", ex);
64                         } finally {
65                                 initialized = true;
66                         }
67                 }
68
69                 static void DoInitialize ()
70                 {
71 #if !ANDROID
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);
76
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);
81 #endif
82                 }
83
84                 public static bool HasStore (MonoBtlsX509StoreType type)
85                 {
86 #if ANDROID
87                         return false;
88 #else
89                         var path = GetStorePath (type);
90                         return path != null && Directory.Exists (path);
91 #endif
92                 }
93
94                 public static string GetStorePath (MonoBtlsX509StoreType type)
95                 {
96 #if ANDROID
97                         throw new NotSupportedException ();
98 #else
99                         Initialize ();
100                         switch (type) {
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;
113                         default:
114                                 throw new NotSupportedException ();
115                         }
116 #endif
117                 }
118         }
119 }
120 #endif