2003-10-26 Todd Berman <tberman@gentoo.org>
[mono.git] / mcs / class / Microsoft.Web.Services / Microsoft.Web.Services.Security.X509 / X509CertificateStore.cs
1 //\r
2 // X509CertificateStore.cs: Handles certificate stores.\r
3 //\r
4 // Author:\r
5 //      Sebastien Pouliot (spouliot@motus.com)\r
6 //\r
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)\r
8 //\r
9 \r
10 using System;\r
11 using System.Security.Cryptography;\r
12 using System.Security.Cryptography.X509Certificates;\r
13 using System.Text;\r
14 \r
15 namespace Microsoft.Web.Services.Security.X509 {\r
16 \r
17         public class X509CertificateStore {\r
18 \r
19                 [Serializable]\r
20                 public enum StoreLocation {\r
21                         CurrentService = 262144,\r
22                         CurrentUser = 65536,\r
23                         CurrentUserGroupPolicy = 458752,\r
24                         LocalMachine = 131072,\r
25                         LocalMachineEnterprise = 589824,\r
26                         LocalMachineGroupPolicy = 524288,\r
27                         Services = 327680,\r
28                         Unknown = 0,\r
29                         Users = 393216\r
30                 }\r
31 \r
32                 [Flags]\r
33                 [Serializable]\r
34                 public enum StoreOpenFlags {\r
35                         CreateNew = 8192,\r
36                         DeferClose = 4,\r
37                         Delete = 16,\r
38                         None = 0,\r
39                         OpenExisting = 16384,\r
40                         ReadOnly = 32768\r
41                 }\r
42 \r
43                 [Serializable]\r
44                 public enum StoreProvider {\r
45                         Collection = 11,\r
46                         File = 3,\r
47                         Memory = 1,\r
48                         System = 10\r
49                 } \r
50 \r
51                 public const string CAStore = "CA";\r
52                 public const string MyStore = "My";\r
53                 public const string RootStore = "Root";\r
54                 public const string TrustStore = "Trust";\r
55                 public const string UnTrustedStore = "Disallowed";\r
56 \r
57                 private const string storeAlreadyOpened = "store already opened";\r
58                 private const string storeNotOpened = "store not opened";\r
59 \r
60                 private StoreOpenFlags storeOpenFlags;\r
61                 private StoreProvider storeProvider;\r
62                 private StoreLocation storeLocation;\r
63                 private string storeName;\r
64                 private ICertificateStore store;\r
65 \r
66                 public X509CertificateStore (StoreProvider provider, StoreLocation location, string storeName)\r
67                 {\r
68                         storeProvider = provider;\r
69                         storeLocation = location;\r
70                         this.storeName = storeName;\r
71                 }\r
72 \r
73                 ~X509CertificateStore () \r
74                 {\r
75                         if (store != null) {\r
76                                 store.Close ();\r
77                                 store = null;\r
78                         }\r
79                 }\r
80 \r
81                 public X509CertificateCollection Certificates {\r
82                         get { \r
83                                 if (store == null)\r
84                                         return null;\r
85                                 return store.GetCollection (); \r
86                         }\r
87                 }\r
88 \r
89                 public IntPtr Handle {\r
90                         get { \r
91                                 if (store == null)\r
92                                         return (IntPtr) 0;\r
93                                 return store.Handle; \r
94                         }\r
95                 }\r
96 \r
97                 public StoreLocation Location {\r
98                         get { return storeLocation; }\r
99                 }\r
100 \r
101                 public bool Open () \r
102                 {\r
103                         return InternalOpen (StoreOpenFlags.None);\r
104                 }\r
105 \r
106                 public bool OpenRead () \r
107                 {\r
108                         return InternalOpen (StoreOpenFlags.ReadOnly);\r
109                 }\r
110 \r
111                 internal bool InternalOpen (StoreOpenFlags flags) \r
112                 {\r
113                         if (store != null)\r
114                                 throw new InvalidOperationException (storeAlreadyOpened);\r
115 \r
116                         storeOpenFlags = flags;\r
117                         switch (storeProvider) {\r
118                                 case StoreProvider.Collection:\r
119                                         store = null;\r
120                                         break;\r
121                                 case StoreProvider.File:\r
122                                         store = null;\r
123                                         break;\r
124                                 case StoreProvider.Memory:\r
125                                         store = new MemoryCertificateStore (storeLocation, storeName, flags);\r
126                                         break;\r
127                                 case StoreProvider.System:\r
128                                         store = null;\r
129                                         break;\r
130                                 default:\r
131                                         throw new NotSupportedException ("Unknown store provider");\r
132                         }\r
133                         return (store != null);\r
134                 }\r
135 \r
136                 public void Close ()\r
137                 {\r
138                         store.Close ();\r
139                         store = null;\r
140                         storeOpenFlags = StoreOpenFlags.None;\r
141                 }\r
142 \r
143                 internal bool Compare (byte[] array1, byte[] array2) \r
144                 {\r
145                         if ((array1 == null) && (array2 == null))\r
146                                 return true;\r
147                         if ((array1 == null) || (array2 == null))\r
148                                 return false;\r
149                         if (array1.Length != array2.Length)\r
150                                 return false;\r
151                         for (int i=0; i < array1.Length; i++) {\r
152                                 if (array1 [i] != array2 [i])\r
153                                         return false;\r
154                         }\r
155                         return true;\r
156                 }\r
157 \r
158                 public X509CertificateCollection FindCertificateByHash (byte[] certHash)\r
159                 {\r
160                         if (certHash == null)\r
161                                 throw new ArgumentNullException ("certHash");\r
162                         if (store != null)\r
163                                 throw new InvalidOperationException (storeNotOpened);\r
164  \r
165                         X509CertificateCollection results = new X509CertificateCollection ();\r
166                         if (store != null) {\r
167                                 X509CertificateCollection certs = store.GetCollection ();\r
168                                 // apply filter\r
169                                 foreach (X509Certificate c in certs) {\r
170                                         if (Compare (c.GetCertHash (), certHash))\r
171                                                 results.Add (c);\r
172                                 }\r
173                         }\r
174                         return results;\r
175                 }\r
176 \r
177                 public X509CertificateCollection FindCertificateByKeyIdentifier (byte[] keyIdentifier)\r
178                 {\r
179                         if (keyIdentifier == null)\r
180                                 throw new ArgumentNullException ("keyIdentifier");\r
181                         if (store != null)\r
182                                 throw new InvalidOperationException (storeNotOpened);\r
183  \r
184                         X509CertificateCollection results = new X509CertificateCollection ();\r
185                         if (store != null) {\r
186                                 X509CertificateCollection certs = store.GetCollection ();\r
187                                 // apply filter\r
188                                 foreach (X509Certificate c in certs) {\r
189                                         if (Compare (c.GetKeyIdentifier (), keyIdentifier))\r
190                                                 results.Add (c);\r
191                                 }\r
192                         }\r
193                         return results;\r
194                 }\r
195 \r
196                 public X509CertificateCollection FindCertificateBySubjectName (string subjectstring)\r
197                 {\r
198                         if (subjectstring == null)\r
199                                 throw new ArgumentNullException ("subjectstring");\r
200                         if (store != null)\r
201                                 throw new InvalidOperationException (storeNotOpened);\r
202 \r
203                         X509CertificateCollection results = new X509CertificateCollection ();\r
204                         if (store != null) {\r
205                                 X509CertificateCollection certs = store.GetCollection ();\r
206                                 // apply filter\r
207                                 foreach (X509Certificate c in certs) {\r
208                                         if (c.GetName() != subjectstring)\r
209                                                 results.Add (c);\r
210                                 }\r
211                         }\r
212                         return results;\r
213                 }\r
214 \r
215                 public X509CertificateCollection FindCertificateBySubjectString (string subjectsubstring)\r
216                 {\r
217                         if (subjectsubstring == null)\r
218                                 throw new ArgumentNullException ("subjectsubstring");\r
219                         if (store != null)\r
220                                 throw new InvalidOperationException (storeNotOpened);\r
221 \r
222                         X509CertificateCollection results = new X509CertificateCollection ();\r
223                         if (store != null) {\r
224                                 X509CertificateCollection certs = store.GetCollection ();\r
225                                 // apply filter\r
226                                 foreach (X509Certificate c in certs) {\r
227                                         if (c.GetName ().IndexOf (subjectsubstring) > 0)\r
228                                                 results.Add (c);\r
229                                 }\r
230                         }\r
231                         return results;\r
232                 }\r
233 \r
234                 public static X509CertificateStore CurrentUserStore (string storeName) \r
235                 {\r
236                         return new X509CertificateStore (StoreProvider.System, StoreLocation.CurrentUser, storeName);\r
237                 }\r
238 \r
239                 public static X509CertificateStore LocalMachineStore (string storeName) \r
240                 {\r
241                         return new X509CertificateStore (StoreProvider.System, StoreLocation.LocalMachine, storeName);\r
242                 }\r
243         }\r
244 }\r