**** Merged from MCS ****
[mono-project.git] / mcs / class / System.Security / System.Security.Cryptography.X509Certificates / X509Store.cs
blob945e94ff4d9e8a67aff559a33c3c3bb21b0d404b
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.
30 #if NET_2_0
32 using System;
34 using Mono.Security.X509;
36 namespace System.Security.Cryptography.X509Certificates {
38 public sealed class X509Store {
40 private string _name;
41 private StoreLocation _location;
42 private X509CertificateExCollection _certs;
43 private OpenFlags _flags;
45 // constructors
47 // BUG: MY when using this constructor - My when using StoreName.My
48 public X509Store ()
49 : this ("MY", StoreLocation.CurrentUser)
53 public X509Store (string storeName)
54 : this (storeName, StoreLocation.CurrentUser)
58 public X509Store (StoreName storeName)
59 : this (StoreNameToString (storeName), StoreLocation.CurrentUser)
63 public X509Store (StoreLocation storeLocation)
64 : this ("MY", storeLocation)
68 public X509Store (StoreName storeName, StoreLocation storeLocation)
69 : this (StoreNameToString (storeName), StoreLocation.CurrentUser)
73 [MonoTODO ("call Mono.Security.X509.X509Store*")]
74 public X509Store (string storeName, StoreLocation storeLocation)
76 if (storeName == null)
77 throw new ArgumentNullException ("storeName");
79 _name = storeName;
80 _location = storeLocation;
83 // properties
85 public X509CertificateExCollection Certificates {
86 get {
87 if (_certs == null)
88 _certs = new X509CertificateExCollection ();
89 return _certs;
93 public StoreLocation Location {
94 get { return _location; }
97 public string Name {
98 get { return _name; }
101 private bool ReadOnly {
102 get { return ((_flags & OpenFlags.ReadOnly) != OpenFlags.ReadOnly); }
105 // methods
107 private static string StoreNameToString (StoreName sn)
109 switch (sn) {
110 case StoreName.CertificateAuthority:
111 return "CA";
112 default:
113 return sn.ToString ();
117 [MonoTODO ("call Mono.Security.X509.X509Store*")]
118 public void Add (X509CertificateEx certificate)
120 if (certificate == null)
121 throw new ArgumentNullException ("certificate");
123 if (!ReadOnly) {
124 try {
125 Mono.Security.X509.X509Certificate x = new Mono.Security.X509.X509Certificate (certificate.RawData);
126 // TODO
128 catch {
129 throw new CryptographicException ("couldn't add certificate");
134 public void AddRange (X509CertificateExCollection certificates)
136 if (certificates == null)
137 throw new ArgumentNullException ("certificates");
139 if (!ReadOnly) {
140 foreach (X509CertificateEx certificate in certificates) {
141 Add (certificate);
146 [MonoTODO ("call Mono.Security.X509.X509Store*")]
147 public void Close ()
151 [MonoTODO ("call Mono.Security.X509.X509Store*")]
152 public void Open (OpenFlags flags)
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
161 [MonoTODO ("call Mono.Security.X509.X509Store*")]
162 public void Remove (X509CertificateEx certificate)
164 if (certificate == null)
165 throw new ArgumentNullException ("certificate");
167 if (!ReadOnly) {
168 try {
169 Mono.Security.X509.X509Certificate x = new Mono.Security.X509.X509Certificate (certificate.RawData);
170 // TODO
172 catch {
173 throw new CryptographicException ("couldn't remove certificate");
178 public void RemoveRange (X509CertificateExCollection certificates)
180 if (certificates == null)
181 throw new ArgumentNullException ("certificates");
183 if (!this.ReadOnly) {
184 foreach (X509CertificateEx certificate in certificates) {
185 Remove (certificate);
192 #endif