Merge pull request #3800 from madewokherd/mingwbuild
[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 && MONO_FEATURE_BTLS
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 #if !ANDROID
49                 static string machineTrustedRootPath;
50                 static string machineIntermediateCAPath;
51                 static string machineUntrustedPath;
52                 static string userTrustedRootPath;
53                 static string userIntermediateCAPath;
54                 static string userUntrustedPath;
55 #endif
56
57                 static void Initialize ()
58                 {
59                         if (initialized)
60                                 return;
61
62                         try {
63                                 DoInitialize ();
64                         } catch (Exception ex) {
65                                 Console.Error.WriteLine ("MonoBtlsX509StoreManager.Initialize() threw exception: {0}", ex);
66                         } finally {
67                                 initialized = true;
68                         }
69                 }
70
71                 static void DoInitialize ()
72                 {
73 #if !ANDROID
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);
78
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);
83 #endif
84                 }
85
86                 public static bool HasStore (MonoBtlsX509StoreType type)
87                 {
88 #if ANDROID
89                         return false;
90 #else
91                         var path = GetStorePath (type);
92                         return path != null && Directory.Exists (path);
93 #endif
94                 }
95
96                 public static string GetStorePath (MonoBtlsX509StoreType type)
97                 {
98 #if ANDROID
99                         throw new NotSupportedException ();
100 #else
101                         Initialize ();
102                         switch (type) {
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;
115                         default:
116                                 throw new NotSupportedException ();
117                         }
118 #endif
119                 }
120         }
121 }
122 #endif