2 // X509CertificateStore.cs: Handles certificate stores.
5 // Sebastien Pouliot (spouliot@motus.com)
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
11 using System
.Security
.Cryptography
;
12 using System
.Security
.Cryptography
.X509Certificates
;
15 namespace Microsoft
.Web
.Services
.Security
.X509
{
17 public class X509CertificateStore
{
20 public enum StoreLocation
{
21 CurrentService
= 262144,
23 CurrentUserGroupPolicy
= 458752,
24 LocalMachine
= 131072,
25 LocalMachineEnterprise
= 589824,
26 LocalMachineGroupPolicy
= 524288,
34 public enum StoreOpenFlags
{
44 public enum StoreProvider
{
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 ()
81 public X509CertificateCollection Certificates
{
85 return store
.GetCollection ();
89 public IntPtr Handle
{
97 public StoreLocation Location
{
98 get { return storeLocation; }
103 return InternalOpen (StoreOpenFlags
.None
);
106 public bool OpenRead ()
108 return InternalOpen (StoreOpenFlags
.ReadOnly
);
111 internal bool InternalOpen (StoreOpenFlags flags
)
114 throw new InvalidOperationException (storeAlreadyOpened
);
116 storeOpenFlags
= flags
;
117 switch (storeProvider
) {
118 case StoreProvider
.Collection
:
121 case StoreProvider
.File
:
124 case StoreProvider
.Memory
:
125 store
= new MemoryCertificateStore (storeLocation
, storeName
, flags
);
127 case StoreProvider
.System
:
131 throw new NotSupportedException ("Unknown store provider");
133 return (store
!= null);
140 storeOpenFlags
= StoreOpenFlags
.None
;
143 internal bool Compare (byte[] array1
, byte[] array2
)
145 if ((array1
== null) && (array2
== null))
147 if ((array1
== null) || (array2
== null))
149 if (array1
.Length
!= array2
.Length
)
151 for (int i
=0; i
< array1
.Length
; i
++) {
152 if (array1
[i
] != array2
[i
])
158 public X509CertificateCollection
FindCertificateByHash (byte[] certHash
)
160 if (certHash
== null)
161 throw new ArgumentNullException ("certHash");
163 throw new InvalidOperationException (storeNotOpened
);
165 X509CertificateCollection results
= new X509CertificateCollection ();
167 X509CertificateCollection certs
= store
.GetCollection ();
169 foreach (X509Certificate c
in certs
) {
170 if (Compare (c
.GetCertHash (), certHash
))
177 public X509CertificateCollection
FindCertificateByKeyIdentifier (byte[] keyIdentifier
)
179 if (keyIdentifier
== null)
180 throw new ArgumentNullException ("keyIdentifier");
182 throw new InvalidOperationException (storeNotOpened
);
184 X509CertificateCollection results
= new X509CertificateCollection ();
186 X509CertificateCollection certs
= store
.GetCollection ();
188 foreach (X509Certificate c
in certs
) {
189 if (Compare (c
.GetKeyIdentifier (), keyIdentifier
))
196 public X509CertificateCollection
FindCertificateBySubjectName (string subjectstring
)
198 if (subjectstring
== null)
199 throw new ArgumentNullException ("subjectstring");
201 throw new InvalidOperationException (storeNotOpened
);
203 X509CertificateCollection results
= new X509CertificateCollection ();
205 X509CertificateCollection certs
= store
.GetCollection ();
207 foreach (X509Certificate c
in certs
) {
208 if (c
.GetName() != subjectstring
)
215 public X509CertificateCollection
FindCertificateBySubjectString (string subjectsubstring
)
217 if (subjectsubstring
== null)
218 throw new ArgumentNullException ("subjectsubstring");
220 throw new InvalidOperationException (storeNotOpened
);
222 X509CertificateCollection results
= new X509CertificateCollection ();
224 X509CertificateCollection certs
= store
.GetCollection ();
226 foreach (X509Certificate c
in certs
) {
227 if (c
.GetName ().IndexOf (subjectsubstring
) > 0)
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
);