2 // X509StoreManager.cs: X.509 store manager.
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // (C) 2004 Novell (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
32 using System
.Collections
;
35 using Mono
.Security
.X509
.Extensions
;
37 namespace Mono
.Security
.X509
{
39 #if INSIDE_CORLIB || INSIDE_SYSTEM
44 sealed class X509StoreManager
{
46 static private string _userPath
;
47 static private string _localMachinePath
;
48 static private string _newUserPath
;
49 static private string _newLocalMachinePath
;
50 static private X509Stores _userStore
;
51 static private X509Stores _machineStore
;
52 static private X509Stores _newUserStore
;
53 static private X509Stores _newMachineStore
;
55 private X509StoreManager ()
59 internal static string CurrentUserPath
{
61 if (_userPath
== null) {
62 _userPath
= Path
.Combine (
63 Environment
.GetFolderPath (Environment
.SpecialFolder
.ApplicationData
),
65 _userPath
= Path
.Combine (_userPath
, "certs");
71 internal static string LocalMachinePath
{
73 if (_localMachinePath
== null) {
74 _localMachinePath
= Path
.Combine (
75 Environment
.GetFolderPath (Environment
.SpecialFolder
.CommonApplicationData
),
77 _localMachinePath
= Path
.Combine (_localMachinePath
, "certs");
79 return _localMachinePath
;
83 internal static string NewCurrentUserPath
{
85 if (_newUserPath
== null) {
86 _newUserPath
= Path
.Combine (
87 Environment
.GetFolderPath (Environment
.SpecialFolder
.ApplicationData
),
89 _newUserPath
= Path
.Combine (_newUserPath
, "new-certs");
95 internal static string NewLocalMachinePath
{
97 if (_newLocalMachinePath
== null) {
98 _newLocalMachinePath
= Path
.Combine (
99 Environment
.GetFolderPath (Environment
.SpecialFolder
.CommonApplicationData
),
101 _newLocalMachinePath
= Path
.Combine (_newLocalMachinePath
, "new-certs");
103 return _newLocalMachinePath
;
107 static public X509Stores CurrentUser
{
109 if (_userStore
== null)
110 _userStore
= new X509Stores (CurrentUserPath
, false);
116 static public X509Stores LocalMachine
{
118 if (_machineStore
== null)
119 _machineStore
= new X509Stores (LocalMachinePath
, false);
121 return _machineStore
;
125 static public X509Stores NewCurrentUser
{
127 if (_newUserStore
== null)
128 _newUserStore
= new X509Stores (NewCurrentUserPath
, true);
130 return _newUserStore
;
134 static public X509Stores NewLocalMachine
{
136 if (_newMachineStore
== null)
137 _newMachineStore
= new X509Stores (NewLocalMachinePath
, true);
139 return _newMachineStore
;
143 // Merged stores collections
144 // we need to look at both the user and the machine (entreprise)
145 // certificates/CRLs when building/validating a chain
147 static public X509CertificateCollection IntermediateCACertificates
{
149 X509CertificateCollection intermediateCerts
= new X509CertificateCollection ();
150 intermediateCerts
.AddRange (CurrentUser
.IntermediateCA
.Certificates
);
151 intermediateCerts
.AddRange (LocalMachine
.IntermediateCA
.Certificates
);
152 return intermediateCerts
;
156 static public ArrayList IntermediateCACrls
{
158 ArrayList intermediateCRLs
= new ArrayList ();
159 intermediateCRLs
.AddRange (CurrentUser
.IntermediateCA
.Crls
);
160 intermediateCRLs
.AddRange (LocalMachine
.IntermediateCA
.Crls
);
161 return intermediateCRLs
;
165 static public X509CertificateCollection TrustedRootCertificates
{
167 X509CertificateCollection trustedCerts
= new X509CertificateCollection ();
168 trustedCerts
.AddRange (CurrentUser
.TrustedRoot
.Certificates
);
169 trustedCerts
.AddRange (LocalMachine
.TrustedRoot
.Certificates
);
174 static public ArrayList TrustedRootCACrls
{
176 ArrayList trustedCRLs
= new ArrayList ();
177 trustedCRLs
.AddRange (CurrentUser
.TrustedRoot
.Crls
);
178 trustedCRLs
.AddRange (LocalMachine
.TrustedRoot
.Crls
);
183 static public X509CertificateCollection UntrustedCertificates
{
185 X509CertificateCollection untrustedCerts
= new X509CertificateCollection ();
186 untrustedCerts
.AddRange (CurrentUser
.Untrusted
.Certificates
);
187 untrustedCerts
.AddRange (LocalMachine
.Untrusted
.Certificates
);
188 return untrustedCerts
;