* Mono.Posix.dll.sources: Rename Mono.Posix to Mono.Unix.
[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  <sebastien@ximian.com>
6 //
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004 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
31
32 using System;
33
34 using Mono.Security.X509;
35
36 namespace System.Security.Cryptography.X509Certificates {
37
38         public sealed class X509Store {
39
40                 private string _name;
41                 private StoreLocation _location;
42                 private X509CertificateExCollection _certs;
43                 private OpenFlags _flags;
44
45                 // constructors
46
47                 // BUG: MY when using this constructor - My when using StoreName.My
48                 public X509Store () 
49                         : this ("MY", StoreLocation.CurrentUser) 
50                 {
51                 }
52
53                 public X509Store (string storeName) 
54                         : this (storeName, StoreLocation.CurrentUser) 
55                 {
56                 }
57
58                 public X509Store (StoreName storeName) 
59                         : this (StoreNameToString (storeName), StoreLocation.CurrentUser)
60                 {
61                 }
62
63                 public X509Store (StoreLocation storeLocation) 
64                         : this ("MY", storeLocation)
65                 {
66                 }
67
68                 public X509Store (StoreName storeName, StoreLocation storeLocation)
69                         : this (StoreNameToString (storeName), StoreLocation.CurrentUser)
70                 {
71                 }
72
73                 [MonoTODO ("call Mono.Security.X509.X509Store*")]
74                 public X509Store (string storeName, StoreLocation storeLocation)
75                 {
76                         if (storeName == null)
77                                 throw new ArgumentNullException ("storeName");
78
79                         _name = storeName;
80                         _location = storeLocation;
81                 }
82
83                 // properties
84
85                 public X509CertificateExCollection Certificates {
86                         get { 
87                                 if (_certs == null)
88                                         _certs = new X509CertificateExCollection ();
89                                 return _certs; 
90                         }
91                 } 
92
93                 public StoreLocation Location {
94                         get { return _location; }
95                 }
96
97                 public string Name {
98                         get { return _name; }
99                 }
100
101                 private bool ReadOnly {
102                         get { return ((_flags & OpenFlags.ReadOnly) != OpenFlags.ReadOnly); }
103                 }
104
105                 // methods
106
107                 private static string StoreNameToString (StoreName sn) 
108                 {
109                         switch (sn) {
110                                 case StoreName.CertificateAuthority:
111                                         return "CA";
112                                 default:
113                                         return sn.ToString ();
114                         }
115                 }
116
117                 [MonoTODO ("call Mono.Security.X509.X509Store*")]
118                 public void Add (X509CertificateEx certificate)
119                 {
120                         if (certificate == null)
121                                 throw new ArgumentNullException ("certificate");
122
123                         if (!ReadOnly) {
124                                 try {
125                                         Mono.Security.X509.X509Certificate x = new Mono.Security.X509.X509Certificate (certificate.RawData);
126                                         // TODO
127                                 }
128                                 catch {
129                                         throw new CryptographicException ("couldn't add certificate");
130                                 }
131                         }
132                 }
133
134                 public void AddRange (X509CertificateExCollection certificates)
135                 {
136                         if (certificates == null)
137                                 throw new ArgumentNullException ("certificates");
138
139                         if (!ReadOnly) {
140                                 foreach (X509CertificateEx certificate in certificates) {
141                                         Add (certificate);
142                                 }
143                         }
144                 }
145
146                 [MonoTODO ("call Mono.Security.X509.X509Store*")]
147                 public void Close () 
148                 {
149                 }
150
151                 [MonoTODO ("call Mono.Security.X509.X509Store*")]
152                 public void Open (OpenFlags flags)
153                 {
154                         _flags = flags;
155                         bool readOnly = ((flags & OpenFlags.ReadOnly) == OpenFlags.ReadOnly);
156                         bool create = !((flags & OpenFlags.OpenExistingOnly) == OpenFlags.OpenExistingOnly);
157                         bool archive = ((flags & OpenFlags.IncludeArchived) == OpenFlags.IncludeArchived);
158                         // TODO
159                 }
160
161                 [MonoTODO ("call Mono.Security.X509.X509Store*")]
162                 public void Remove (X509CertificateEx certificate) 
163                 {
164                         if (certificate == null)
165                                 throw new ArgumentNullException ("certificate");
166
167                         if (!ReadOnly) {
168                                 try {
169                                         Mono.Security.X509.X509Certificate x = new Mono.Security.X509.X509Certificate (certificate.RawData);
170                                         // TODO
171                                 }
172                                 catch {
173                                         throw new CryptographicException ("couldn't remove certificate");
174                                 }
175                         }
176                 }
177
178                 public void RemoveRange (X509CertificateExCollection certificates) 
179                 {
180                         if (certificates == null)
181                                 throw new ArgumentNullException ("certificates");
182
183                         if (!this.ReadOnly) {
184                                 foreach (X509CertificateEx certificate in certificates) {
185                                         Remove (certificate);
186                                 }
187                         }
188                 }
189         }
190 }
191
192 #endif