Merge pull request #3749 from BrzVlad/fix-mips-fix
[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 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;
54
55                 private X509StoreManager ()
56                 {
57                 }
58
59                 internal static string CurrentUserPath {
60                         get {
61                                 if (_userPath == null) {
62                                         _userPath = Path.Combine (
63                                                         Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData),
64                                                         ".mono");
65                                         _userPath = Path.Combine (_userPath, "certs");
66                                 }
67                                 return _userPath;
68                         }
69                 }
70
71                 internal static string LocalMachinePath {
72                         get {
73                                 if (_localMachinePath == null) {
74                                         _localMachinePath = Path.Combine (
75                                                 Environment.GetFolderPath (Environment.SpecialFolder.CommonApplicationData),
76                                                 ".mono");
77                                         _localMachinePath = Path.Combine (_localMachinePath, "certs");
78                                 }
79                                 return _localMachinePath;
80                         }
81                 }
82
83                 internal static string NewCurrentUserPath {
84                         get {
85                                 if (_newUserPath == null) {
86                                         _newUserPath = Path.Combine (
87                                                         Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData),
88                                                         ".mono");
89                                         _newUserPath = Path.Combine (_newUserPath, "new-certs");
90                                 }
91                                 return _newUserPath;
92                         }
93                 }
94
95                 internal static string NewLocalMachinePath {
96                         get {
97                                 if (_newLocalMachinePath == null) {
98                                         _newLocalMachinePath = Path.Combine (
99                                                 Environment.GetFolderPath (Environment.SpecialFolder.CommonApplicationData),
100                                                 ".mono");
101                                         _newLocalMachinePath = Path.Combine (_newLocalMachinePath, "new-certs");
102                                 }
103                                 return _newLocalMachinePath;
104                         }
105                 }
106
107                 static public X509Stores CurrentUser {
108                         get { 
109                                 if (_userStore == null)
110                                         _userStore = new X509Stores (CurrentUserPath, false);
111                                 
112                                 return _userStore;
113                         }
114                 }
115
116                 static public X509Stores LocalMachine {
117                         get {
118                                 if (_machineStore == null) 
119                                         _machineStore = new X509Stores (LocalMachinePath, false);
120
121                                 return _machineStore;
122                         }
123                 }
124
125                 static public X509Stores NewCurrentUser {
126                         get {
127                                 if (_newUserStore == null)
128                                         _newUserStore = new X509Stores (NewCurrentUserPath, true);
129
130                                 return _newUserStore;
131                         }
132                 }
133
134                 static public X509Stores NewLocalMachine {
135                         get {
136                                 if (_newMachineStore == null)
137                                         _newMachineStore = new X509Stores (NewLocalMachinePath, true);
138
139                                 return _newMachineStore;
140                         }
141                 }
142
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
146
147                 static public X509CertificateCollection IntermediateCACertificates {
148                         get { 
149                                 X509CertificateCollection intermediateCerts = new X509CertificateCollection ();
150                                 intermediateCerts.AddRange (CurrentUser.IntermediateCA.Certificates);
151                                 intermediateCerts.AddRange (LocalMachine.IntermediateCA.Certificates);
152                                 return intermediateCerts; 
153                         }
154                 }
155
156                 static public ArrayList IntermediateCACrls {
157                         get { 
158                                 ArrayList intermediateCRLs = new ArrayList ();
159                                 intermediateCRLs.AddRange (CurrentUser.IntermediateCA.Crls);
160                                 intermediateCRLs.AddRange (LocalMachine.IntermediateCA.Crls);
161                                 return intermediateCRLs; 
162                         }
163                 }
164
165                 static public X509CertificateCollection TrustedRootCertificates {
166                         get { 
167                                 X509CertificateCollection trustedCerts = new X509CertificateCollection ();
168                                 trustedCerts.AddRange (CurrentUser.TrustedRoot.Certificates);
169                                 trustedCerts.AddRange (LocalMachine.TrustedRoot.Certificates);
170                                 return trustedCerts; 
171                         }
172                 }
173
174                 static public ArrayList TrustedRootCACrls {
175                         get { 
176                                 ArrayList trustedCRLs = new ArrayList ();
177                                 trustedCRLs.AddRange (CurrentUser.TrustedRoot.Crls);
178                                 trustedCRLs.AddRange (LocalMachine.TrustedRoot.Crls);
179                                 return trustedCRLs; 
180                         }
181                 }
182
183                 static public X509CertificateCollection UntrustedCertificates {
184                         get { 
185                                 X509CertificateCollection untrustedCerts = new X509CertificateCollection ();
186                                 untrustedCerts.AddRange (CurrentUser.Untrusted.Certificates);
187                                 untrustedCerts.AddRange (LocalMachine.Untrusted.Certificates);
188                                 return untrustedCerts; 
189                         }
190                 }
191         }
192 }