This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mcs / class / System.Security / System.Security.Cryptography.X509Certificates / X509Store.cs
1 //
2 // X509Store.cs - System.Security.Cryptography.X509Certificates.X509Store
3 //
4 // Author:
5 //      Sebastien Pouliot (spouliot@motus.com)
6 //
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 //
9
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30
31 #if NET_2_0
32
33 using System;
34
35 using Mono.Security.X509;
36 using Mono.Security.X509.Stores;
37
38 namespace System.Security.Cryptography.X509Certificates {
39
40         // Note: Match the definition of framework version 1.2.3400.0 on http://longhorn.msdn.microsoft.com
41
42         public sealed class X509Store {
43
44                 private string _name;
45                 private StoreLocation _location;
46                 private X509CertificateExCollection _certs;
47                 private OpenFlags _flags;
48                 private ICertificateStore _store;
49
50                 // constructors
51
52                 // BUG: MY when using this constructor - My when using StoreName.My
53                 public X509Store () 
54                         : this ("MY", StoreLocation.CurrentUser) {}
55
56                 public X509Store (string storeName) 
57                         : this (storeName, StoreLocation.CurrentUser) {}
58
59                 public X509Store (StoreName storeName) 
60                         : this (StoreNameToString (storeName), StoreLocation.CurrentUser) {}
61
62                 public X509Store (StoreLocation storeLocation) 
63                         : this ("MY", storeLocation) {}
64
65                 public X509Store (StoreName storeName, StoreLocation storeLocation)
66                         : this (StoreNameToString (storeName), StoreLocation.CurrentUser) {}
67
68                 public X509Store (string storeName, StoreLocation storeLocation)
69                 {
70                         if (storeName == null)
71                                 throw new ArgumentNullException ("storeName");
72
73                         _name = storeName;
74                         _location = storeLocation;
75                         _store = new Mono.Security.X509.Stores.FileCertificateStore ();
76                 }
77
78                 // properties
79
80                 public X509CertificateExCollection Certificates {
81                         get { 
82                                 if (_certs == null)
83                                         _certs = new X509CertificateExCollection ();
84                                 return _certs; 
85                         }
86                 } 
87
88                 public StoreLocation Location {
89                         get { return _location; }
90                 }
91
92                 public string Name {
93                         get { return _name; }
94                 }
95
96                 private bool ReadOnly {
97                         get { return ((_flags & OpenFlags.ReadOnly) != OpenFlags.ReadOnly); }
98                 }
99
100                 // methods
101
102                 private static string StoreNameToString (StoreName sn) 
103                 {
104                         switch (sn) {
105                                 case StoreName.CertificateAuthority:
106                                         return "CA";
107                                 default:
108                                         return sn.ToString ();
109                         }
110                 }
111
112                 public void Add (X509CertificateEx certificate)
113                 {
114                         if (certificate == null)
115                                 throw new ArgumentNullException ("certificate");
116
117                         if ((!ReadOnly) && (_store != null)) {
118                                 try {
119                                         Mono.Security.X509.X509Certificate x = new Mono.Security.X509.X509Certificate (certificate.RawData);
120                                         _store.Add (x);
121                                 }
122                                 catch {
123                                         throw new CryptographicException ("couldn't add certificate");
124                                 }
125                         }
126                 }
127
128                 public void AddRange (X509CertificateExCollection certificates)
129                 {
130                         if (certificates == null)
131                                 throw new ArgumentNullException ("certificates");
132
133                         if (!ReadOnly) {
134                                 foreach (X509CertificateEx certificate in certificates) {
135                                         Add (certificate);
136                                 }
137                         }
138                 }
139
140                 public void Close () 
141                 {
142                         if (_store != null)
143                                 _store.Close ();
144                 }
145
146                 public void Open (OpenFlags flags)
147                 {
148                         _flags = 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);
153                 }
154
155                 public void Remove (X509CertificateEx certificate) 
156                 {
157                         if (certificate == null)
158                                 throw new ArgumentNullException ("certificate");
159
160                         if ((!ReadOnly) && (_store != null)) {
161                                 try {
162                                         Mono.Security.X509.X509Certificate x = new Mono.Security.X509.X509Certificate (certificate.RawData);
163                                         _store.Remove (x);
164                                 }
165                                 catch {
166                                         throw new CryptographicException ("couldn't remove certificate");
167                                 }
168                         }
169                 }
170
171                 public void RemoveRange (X509CertificateExCollection certificates) 
172                 {
173                         if (certificates == null)
174                                 throw new ArgumentNullException ("certificates");
175
176                         if (!this.ReadOnly) {
177                                 foreach (X509CertificateEx certificate in certificates) {
178                                         Remove (certificate);
179                                 }
180                         }
181                 }
182         }
183 }
184
185 #endif