2 // X509Store.cs - System.Security.Cryptography.X509Certificates.X509Store
5 // Sebastien Pouliot (spouliot@motus.com)
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
35 using Mono.Security.X509;
36 using Mono.Security.X509.Stores;
38 namespace System.Security.Cryptography.X509Certificates {
40 // Note: Match the definition of framework version 1.2.3400.0 on http://longhorn.msdn.microsoft.com
42 public sealed class X509Store {
45 private StoreLocation _location;
46 private X509CertificateExCollection _certs;
47 private OpenFlags _flags;
48 private ICertificateStore _store;
52 // BUG: MY when using this constructor - My when using StoreName.My
54 : this ("MY", StoreLocation.CurrentUser) {}
56 public X509Store (string storeName)
57 : this (storeName, StoreLocation.CurrentUser) {}
59 public X509Store (StoreName storeName)
60 : this (StoreNameToString (storeName), StoreLocation.CurrentUser) {}
62 public X509Store (StoreLocation storeLocation)
63 : this ("MY", storeLocation) {}
65 public X509Store (StoreName storeName, StoreLocation storeLocation)
66 : this (StoreNameToString (storeName), StoreLocation.CurrentUser) {}
68 public X509Store (string storeName, StoreLocation storeLocation)
70 if (storeName == null)
71 throw new ArgumentNullException ("storeName");
74 _location = storeLocation;
75 _store = new Mono.Security.X509.Stores.FileCertificateStore ();
80 public X509CertificateExCollection Certificates {
83 _certs = new X509CertificateExCollection ();
88 public StoreLocation Location {
89 get { return _location; }
96 private bool ReadOnly {
97 get { return ((_flags & OpenFlags.ReadOnly) != OpenFlags.ReadOnly); }
102 private static string StoreNameToString (StoreName sn)
105 case StoreName.CertificateAuthority:
108 return sn.ToString ();
112 public void Add (X509CertificateEx certificate)
114 if (certificate == null)
115 throw new ArgumentNullException ("certificate");
117 if ((!ReadOnly) && (_store != null)) {
119 Mono.Security.X509.X509Certificate x = new Mono.Security.X509.X509Certificate (certificate.RawData);
123 throw new CryptographicException ("couldn't add certificate");
128 public void AddRange (X509CertificateExCollection certificates)
130 if (certificates == null)
131 throw new ArgumentNullException ("certificates");
134 foreach (X509CertificateEx certificate in certificates) {
146 public void Open (OpenFlags flags)
149 bool readOnly = ((flags & OpenFlags.ReadOnly) == OpenFlags.ReadOnly);
150 bool create = !((flags & OpenFlags.OpenExistingOnly) == OpenFlags.OpenExistingOnly);
151 bool archive = ((flags & OpenFlags.IncludeArchived) == OpenFlags.IncludeArchived);
152 _store.Open (_name, _location.ToString (), readOnly, create, archive);
155 public void Remove (X509CertificateEx certificate)
157 if (certificate == null)
158 throw new ArgumentNullException ("certificate");
160 if ((!ReadOnly) && (_store != null)) {
162 Mono.Security.X509.X509Certificate x = new Mono.Security.X509.X509Certificate (certificate.RawData);
166 throw new CryptographicException ("couldn't remove certificate");
171 public void RemoveRange (X509CertificateExCollection certificates)
173 if (certificates == null)
174 throw new ArgumentNullException ("certificates");
176 if (!this.ReadOnly) {
177 foreach (X509CertificateEx certificate in certificates) {
178 Remove (certificate);