(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / Microsoft.Web.Services / Microsoft.Web.Services.Security.X509 / X509CertificateStore.cs
blob3d4274bed4f180172e58b7f2baf9f28e3235da9d
1 //
2 // X509CertificateStore.cs: Handles certificate stores.
3 //
4 // Author:
5 // Sebastien Pouliot (spouliot@motus.com)
6 //
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 //
10 using System;
11 using System.Security.Cryptography;
12 using System.Security.Cryptography.X509Certificates;
13 using System.Text;
15 namespace Microsoft.Web.Services.Security.X509 {
17 public class X509CertificateStore {
19 [Serializable]
20 public enum StoreLocation {
21 CurrentService = 262144,
22 CurrentUser = 65536,
23 CurrentUserGroupPolicy = 458752,
24 LocalMachine = 131072,
25 LocalMachineEnterprise = 589824,
26 LocalMachineGroupPolicy = 524288,
27 Services = 327680,
28 Unknown = 0,
29 Users = 393216
32 [Flags]
33 [Serializable]
34 public enum StoreOpenFlags {
35 CreateNew = 8192,
36 DeferClose = 4,
37 Delete = 16,
38 None = 0,
39 OpenExisting = 16384,
40 ReadOnly = 32768
43 [Serializable]
44 public enum StoreProvider {
45 Collection = 11,
46 File = 3,
47 Memory = 1,
48 System = 10
51 public const string CAStore = "CA";
52 public const string MyStore = "My";
53 public const string RootStore = "Root";
54 public const string TrustStore = "Trust";
55 public const string UnTrustedStore = "Disallowed";
57 private const string storeAlreadyOpened = "store already opened";
58 private const string storeNotOpened = "store not opened";
60 private StoreOpenFlags storeOpenFlags;
61 private StoreProvider storeProvider;
62 private StoreLocation storeLocation;
63 private string storeName;
64 private ICertificateStore store;
66 public X509CertificateStore (StoreProvider provider, StoreLocation location, string storeName)
68 storeProvider = provider;
69 storeLocation = location;
70 this.storeName = storeName;
73 ~X509CertificateStore ()
75 if (store != null) {
76 store.Close ();
77 store = null;
81 public X509CertificateCollection Certificates {
82 get {
83 if (store == null)
84 return null;
85 return store.GetCollection ();
89 public IntPtr Handle {
90 get {
91 if (store == null)
92 return (IntPtr) 0;
93 return store.Handle;
97 public StoreLocation Location {
98 get { return storeLocation; }
101 public bool Open ()
103 return InternalOpen (StoreOpenFlags.None);
106 public bool OpenRead ()
108 return InternalOpen (StoreOpenFlags.ReadOnly);
111 internal bool InternalOpen (StoreOpenFlags flags)
113 if (store != null)
114 throw new InvalidOperationException (storeAlreadyOpened);
116 storeOpenFlags = flags;
117 switch (storeProvider) {
118 case StoreProvider.Collection:
119 store = null;
120 break;
121 case StoreProvider.File:
122 store = null;
123 break;
124 case StoreProvider.Memory:
125 store = new MemoryCertificateStore (storeLocation, storeName, flags);
126 break;
127 case StoreProvider.System:
128 store = null;
129 break;
130 default:
131 throw new NotSupportedException ("Unknown store provider");
133 return (store != null);
136 public void Close ()
138 store.Close ();
139 store = null;
140 storeOpenFlags = StoreOpenFlags.None;
143 internal bool Compare (byte[] array1, byte[] array2)
145 if ((array1 == null) && (array2 == null))
146 return true;
147 if ((array1 == null) || (array2 == null))
148 return false;
149 if (array1.Length != array2.Length)
150 return false;
151 for (int i=0; i < array1.Length; i++) {
152 if (array1 [i] != array2 [i])
153 return false;
155 return true;
158 public X509CertificateCollection FindCertificateByHash (byte[] certHash)
160 if (certHash == null)
161 throw new ArgumentNullException ("certHash");
162 if (store != null)
163 throw new InvalidOperationException (storeNotOpened);
165 X509CertificateCollection results = new X509CertificateCollection ();
166 if (store != null) {
167 X509CertificateCollection certs = store.GetCollection ();
168 // apply filter
169 foreach (X509Certificate c in certs) {
170 if (Compare (c.GetCertHash (), certHash))
171 results.Add (c);
174 return results;
177 public X509CertificateCollection FindCertificateByKeyIdentifier (byte[] keyIdentifier)
179 if (keyIdentifier == null)
180 throw new ArgumentNullException ("keyIdentifier");
181 if (store != null)
182 throw new InvalidOperationException (storeNotOpened);
184 X509CertificateCollection results = new X509CertificateCollection ();
185 if (store != null) {
186 X509CertificateCollection certs = store.GetCollection ();
187 // apply filter
188 foreach (X509Certificate c in certs) {
189 if (Compare (c.GetKeyIdentifier (), keyIdentifier))
190 results.Add (c);
193 return results;
196 public X509CertificateCollection FindCertificateBySubjectName (string subjectstring)
198 if (subjectstring == null)
199 throw new ArgumentNullException ("subjectstring");
200 if (store != null)
201 throw new InvalidOperationException (storeNotOpened);
203 X509CertificateCollection results = new X509CertificateCollection ();
204 if (store != null) {
205 X509CertificateCollection certs = store.GetCollection ();
206 // apply filter
207 foreach (X509Certificate c in certs) {
208 if (c.GetName() != subjectstring)
209 results.Add (c);
212 return results;
215 public X509CertificateCollection FindCertificateBySubjectString (string subjectsubstring)
217 if (subjectsubstring == null)
218 throw new ArgumentNullException ("subjectsubstring");
219 if (store != null)
220 throw new InvalidOperationException (storeNotOpened);
222 X509CertificateCollection results = new X509CertificateCollection ();
223 if (store != null) {
224 X509CertificateCollection certs = store.GetCollection ();
225 // apply filter
226 foreach (X509Certificate c in certs) {
227 if (c.GetName ().IndexOf (subjectsubstring) > 0)
228 results.Add (c);
231 return results;
234 public static X509CertificateStore CurrentUserStore (string storeName)
236 return new X509CertificateStore (StoreProvider.System, StoreLocation.CurrentUser, storeName);
239 public static X509CertificateStore LocalMachineStore (string storeName)
241 return new X509CertificateStore (StoreProvider.System, StoreLocation.LocalMachine, storeName);