385002ebd99b2cfe6540b5b228c3780da0dfb1ea
[mono.git] / mcs / class / System / System.Security.Cryptography.X509Certificates / X509Store.cs
1 //
2 // System.Security.Cryptography.X509Certificates.X509Store class
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 #if NET_2_0 && SECURITY_DEP
31
32 using Mono.Security.X509;
33
34 namespace System.Security.Cryptography.X509Certificates {
35
36         public sealed class X509Store {
37
38                 private string _name;
39                 private StoreLocation _location;
40                 private X509Certificate2Collection _certs;
41                 private OpenFlags _flags;
42
43                 // constructors
44
45                 // BUG: MY when using this constructor - My when using StoreName.My
46                 public X509Store () 
47                         : this ("MY", StoreLocation.CurrentUser) 
48                 {
49                 }
50
51                 public X509Store (string storeName) 
52                         : this (storeName, StoreLocation.CurrentUser) 
53                 {
54                 }
55
56                 public X509Store (StoreName storeName) 
57                         : this (StoreNameToString (storeName), StoreLocation.CurrentUser)
58                 {
59                 }
60
61                 public X509Store (StoreLocation storeLocation) 
62                         : this ("MY", storeLocation)
63                 {
64                 }
65
66                 public X509Store (StoreName storeName, StoreLocation storeLocation)
67                         : this (StoreNameToString (storeName), StoreLocation.CurrentUser)
68                 {
69                 }
70
71                 public X509Store (IntPtr storeHandle)
72                 {
73                         // CryptoAPI compatibility (unmanaged handle)
74                         throw new NotSupportedException ();
75                 }
76
77                 [MonoTODO ("call Mono.Security.X509.X509Store*")]
78                 public X509Store (string storeName, StoreLocation storeLocation)
79                 {
80                         if (storeName == null)
81                                 throw new ArgumentNullException ("storeName");
82
83                         _name = storeName;
84                         _location = storeLocation;
85                 }
86
87                 // properties
88
89                 public X509Certificate2Collection Certificates {
90                         get { 
91                                 if (_certs == null)
92                                         _certs = new X509Certificate2Collection ();
93                                 return _certs; 
94                         }
95                 } 
96
97                 public StoreLocation Location {
98                         get { return _location; }
99                 }
100
101                 public string Name {
102                         get { return _name; }
103                 }
104
105                 private bool ReadOnly {
106                         get { return ((_flags & OpenFlags.ReadOnly) != OpenFlags.ReadOnly); }
107                 }
108
109                 public IntPtr StoreHandle {
110                         get { return IntPtr.Zero; }
111                 }
112
113                 // methods
114
115                 private static string StoreNameToString (StoreName sn) 
116                 {
117                         switch (sn) {
118                                 case StoreName.CertificateAuthority:
119                                         return "CA";
120                                 default:
121                                         return sn.ToString ();
122                         }
123                 }
124
125                 [MonoTODO ("call Mono.Security.X509.X509Store*")]
126                 public void Add (X509Certificate2 certificate)
127                 {
128                         if (certificate == null)
129                                 throw new ArgumentNullException ("certificate");
130
131                         if (!ReadOnly) {
132                                 try {
133                                         new Mono.Security.X509.X509Certificate (certificate.RawData);
134                                         // Mono.Security.X509.X509Certificate x = new Mono.Security.X509.X509Certificate (certificate.RawData);
135                                         // TODO
136                                 }
137                                 catch {
138                                         throw new CryptographicException ("couldn't add certificate");
139                                 }
140                         }
141                 }
142
143                 public void AddRange (X509Certificate2Collection certificates)
144                 {
145                         if (certificates == null)
146                                 throw new ArgumentNullException ("certificates");
147
148                         if (!ReadOnly) {
149                                 foreach (X509Certificate2 certificate in certificates) {
150                                         Add (certificate);
151                                 }
152                         }
153                 }
154
155                 [MonoTODO ("call Mono.Security.X509.X509Store*")]
156                 public void Close () 
157                 {
158                 }
159
160                 [MonoTODO ("call Mono.Security.X509.X509Store*")]
161                 public void Open (OpenFlags flags)
162                 {
163                         _flags = flags;
164                         /*bool readOnly = ((flags & OpenFlags.ReadOnly) == OpenFlags.ReadOnly);
165                         bool create = !((flags & OpenFlags.OpenExistingOnly) == OpenFlags.OpenExistingOnly);
166                         bool archive = ((flags & OpenFlags.IncludeArchived) == OpenFlags.IncludeArchived);*/
167                         // TODO
168                 }
169
170                 [MonoTODO ("call Mono.Security.X509.X509Store*")]
171                 public void Remove (X509Certificate2 certificate) 
172                 {
173                         if (certificate == null)
174                                 throw new ArgumentNullException ("certificate");
175
176                         if (!ReadOnly) {
177                                 try {
178                                         //Mono.Security.X509.X509Certificate x = new Mono.Security.X509.X509Certificate (certificate.RawData);
179                                         // TODO
180                                 }
181                                 catch {
182                                         throw new CryptographicException ("couldn't remove certificate");
183                                 }
184                         }
185                 }
186
187                 public void RemoveRange (X509Certificate2Collection certificates) 
188                 {
189                         if (certificates == null)
190                                 throw new ArgumentNullException ("certificates");
191
192                         if (!this.ReadOnly) {
193                                 foreach (X509Certificate2 certificate in certificates) {
194                                         Remove (certificate);
195                                 }
196                         }
197                 }
198         }
199 }
200
201 #endif