2 // X509CertificateStore.cs: Handles certificate stores.
\r
5 // Sebastien Pouliot (spouliot@motus.com)
\r
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
\r
11 using System.Security.Cryptography;
\r
12 using System.Security.Cryptography.X509Certificates;
\r
15 namespace Microsoft.Web.Services.Security.X509 {
\r
17 public class X509CertificateStore {
\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
34 public enum StoreOpenFlags {
\r
39 OpenExisting = 16384,
\r
44 public enum StoreProvider {
\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
57 private const string storeAlreadyOpened = "store already opened";
\r
58 private const string storeNotOpened = "store not opened";
\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
66 public X509CertificateStore (StoreProvider provider, StoreLocation location, string storeName)
\r
68 storeProvider = provider;
\r
69 storeLocation = location;
\r
70 this.storeName = storeName;
\r
73 ~X509CertificateStore ()
\r
75 if (store != null) {
\r
81 public X509CertificateCollection Certificates {
\r
85 return store.GetCollection ();
\r
89 public IntPtr Handle {
\r
93 return store.Handle;
\r
97 public StoreLocation Location {
\r
98 get { return storeLocation; }
\r
101 public bool Open ()
\r
103 return InternalOpen (StoreOpenFlags.None);
\r
106 public bool OpenRead ()
\r
108 return InternalOpen (StoreOpenFlags.ReadOnly);
\r
111 internal bool InternalOpen (StoreOpenFlags flags)
\r
114 throw new InvalidOperationException (storeAlreadyOpened);
\r
116 storeOpenFlags = flags;
\r
117 switch (storeProvider) {
\r
118 case StoreProvider.Collection:
\r
121 case StoreProvider.File:
\r
124 case StoreProvider.Memory:
\r
125 store = new MemoryCertificateStore (storeLocation, storeName, flags);
\r
127 case StoreProvider.System:
\r
131 throw new NotSupportedException ("Unknown store provider");
\r
133 return (store != null);
\r
136 public void Close ()
\r
140 storeOpenFlags = StoreOpenFlags.None;
\r
143 internal bool Compare (byte[] array1, byte[] array2)
\r
145 if ((array1 == null) && (array2 == null))
\r
147 if ((array1 == null) || (array2 == null))
\r
149 if (array1.Length != array2.Length)
\r
151 for (int i=0; i < array1.Length; i++) {
\r
152 if (array1 [i] != array2 [i])
\r
158 public X509CertificateCollection FindCertificateByHash (byte[] certHash)
\r
160 if (certHash == null)
\r
161 throw new ArgumentNullException ("certHash");
\r
163 throw new InvalidOperationException (storeNotOpened);
\r
165 X509CertificateCollection results = new X509CertificateCollection ();
\r
166 if (store != null) {
\r
167 X509CertificateCollection certs = store.GetCollection ();
\r
169 foreach (X509Certificate c in certs) {
\r
170 if (Compare (c.GetCertHash (), certHash))
\r
177 public X509CertificateCollection FindCertificateByKeyIdentifier (byte[] keyIdentifier)
\r
179 if (keyIdentifier == null)
\r
180 throw new ArgumentNullException ("keyIdentifier");
\r
182 throw new InvalidOperationException (storeNotOpened);
\r
184 X509CertificateCollection results = new X509CertificateCollection ();
\r
185 if (store != null) {
\r
186 X509CertificateCollection certs = store.GetCollection ();
\r
188 foreach (X509Certificate c in certs) {
\r
189 if (Compare (c.GetKeyIdentifier (), keyIdentifier))
\r
196 public X509CertificateCollection FindCertificateBySubjectName (string subjectstring)
\r
198 if (subjectstring == null)
\r
199 throw new ArgumentNullException ("subjectstring");
\r
201 throw new InvalidOperationException (storeNotOpened);
\r
203 X509CertificateCollection results = new X509CertificateCollection ();
\r
204 if (store != null) {
\r
205 X509CertificateCollection certs = store.GetCollection ();
\r
207 foreach (X509Certificate c in certs) {
\r
208 if (c.GetName() != subjectstring)
\r
215 public X509CertificateCollection FindCertificateBySubjectString (string subjectsubstring)
\r
217 if (subjectsubstring == null)
\r
218 throw new ArgumentNullException ("subjectsubstring");
\r
220 throw new InvalidOperationException (storeNotOpened);
\r
222 X509CertificateCollection results = new X509CertificateCollection ();
\r
223 if (store != null) {
\r
224 X509CertificateCollection certs = store.GetCollection ();
\r
226 foreach (X509Certificate c in certs) {
\r
227 if (c.GetName ().IndexOf (subjectsubstring) > 0)
\r
234 public static X509CertificateStore CurrentUserStore (string storeName)
\r
236 return new X509CertificateStore (StoreProvider.System, StoreLocation.CurrentUser, storeName);
\r
239 public static X509CertificateStore LocalMachineStore (string storeName)
\r
241 return new X509CertificateStore (StoreProvider.System, StoreLocation.LocalMachine, storeName);
\r