Merge pull request #2433 from alexrp/thread-volatile-semantics
[mono.git] / mcs / class / Mono.Security / Mono.Security.X509 / X509StoreManager.cs
1 //
2 // X509StoreManager.cs: X.509 store manager.
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // (C) 2004 Novell (http://www.novell.com)
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.Collections;
33 using System.IO;
34
35 using Mono.Security.X509.Extensions;
36
37 namespace Mono.Security.X509 {
38
39 #if INSIDE_CORLIB
40         internal
41 #else
42         public 
43 #endif
44         sealed class X509StoreManager {
45
46                 static private string _userPath;
47                 static private string _localMachinePath;
48                 static private X509Stores _userStore;
49                 static private X509Stores _machineStore;
50
51                 private X509StoreManager ()
52                 {
53                 }
54
55                 internal static string CurrentUserPath {
56                         get {
57                                 if (_userPath == null) {
58                                         _userPath = Path.Combine(
59                                                         Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
60                                                         ".mono");
61                                         _userPath = Path.Combine(_userPath, "certs");
62                                 }
63                                 return _userPath;
64                         }
65                 }
66
67                 internal static string LocalMachinePath {
68                         get {
69                                 if (_localMachinePath == null) {
70                                         _localMachinePath = Path.Combine (
71                                                 Environment.GetFolderPath (Environment.SpecialFolder.CommonApplicationData),
72                                                 ".mono");
73                                         _localMachinePath = Path.Combine (_localMachinePath, "certs");
74                                 }
75                                 return _localMachinePath;
76                         }
77                 }
78
79                 static public X509Stores CurrentUser {
80                         get { 
81                                 if (_userStore == null)
82                                         _userStore = new X509Stores(CurrentUserPath);
83                                 
84                                 return _userStore;
85                         }
86                 }
87
88                 static public X509Stores LocalMachine {
89                         get {
90                                 if (_machineStore == null) 
91                                         _machineStore = new X509Stores (LocalMachinePath);
92
93                                 return _machineStore;
94                         }
95                 }
96
97                 // Merged stores collections
98                 // we need to look at both the user and the machine (entreprise)
99                 // certificates/CRLs when building/validating a chain
100
101                 static public X509CertificateCollection IntermediateCACertificates {
102                         get { 
103                                 X509CertificateCollection intermediateCerts = new X509CertificateCollection ();
104                                 intermediateCerts.AddRange (CurrentUser.IntermediateCA.Certificates);
105                                 intermediateCerts.AddRange (LocalMachine.IntermediateCA.Certificates);
106                                 return intermediateCerts; 
107                         }
108                 }
109
110                 static public ArrayList IntermediateCACrls {
111                         get { 
112                                 ArrayList intermediateCRLs = new ArrayList ();
113                                 intermediateCRLs.AddRange (CurrentUser.IntermediateCA.Crls);
114                                 intermediateCRLs.AddRange (LocalMachine.IntermediateCA.Crls);
115                                 return intermediateCRLs; 
116                         }
117                 }
118
119                 static public X509CertificateCollection TrustedRootCertificates {
120                         get { 
121                                 X509CertificateCollection trustedCerts = new X509CertificateCollection ();
122                                 trustedCerts.AddRange (CurrentUser.TrustedRoot.Certificates);
123                                 trustedCerts.AddRange (LocalMachine.TrustedRoot.Certificates);
124                                 return trustedCerts; 
125                         }
126                 }
127
128                 static public ArrayList TrustedRootCACrls {
129                         get { 
130                                 ArrayList trustedCRLs = new ArrayList ();
131                                 trustedCRLs.AddRange (CurrentUser.TrustedRoot.Crls);
132                                 trustedCRLs.AddRange (LocalMachine.TrustedRoot.Crls);
133                                 return trustedCRLs; 
134                         }
135                 }
136
137                 static public X509CertificateCollection UntrustedCertificates {
138                         get { 
139                                 X509CertificateCollection untrustedCerts = new X509CertificateCollection ();
140                                 untrustedCerts.AddRange (CurrentUser.Untrusted.Certificates);
141                                 untrustedCerts.AddRange (LocalMachine.Untrusted.Certificates);
142                                 return untrustedCerts; 
143                         }
144                 }
145         }
146 }