Welcome to mirror list, hosted at ThFree Co, Russian Federation.

X509Store.cs « System.Security.Cryptography.X509Certificates « System « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f75e1388f2942faf53315feb342480d44f10d9fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
//
// System.Security.Cryptography.X509Certificates.X509Store class
//
// Author:
//	Sebastien Pouliot  <sebastien@ximian.com>
//
// (C) 2003 Motus Technologies Inc. (http://www.motus.com)
// Copyright (C) 2004-2006 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

#if NET_2_0 && SECURITY_DEP

using System.Security.Permissions;
using MX = Mono.Security.X509;

namespace System.Security.Cryptography.X509Certificates {

	public sealed class X509Store {

		private string _name;
		private StoreLocation _location;
		private X509Certificate2Collection list;
		private OpenFlags _flags;
		private MX.X509Store store;

		// constructors

		// BUG: MY when using this constructor - My when using StoreName.My
		public X509Store () 
			: this ("MY", StoreLocation.CurrentUser) 
		{
		}

		public X509Store (string storeName) 
			: this (storeName, StoreLocation.CurrentUser) 
		{
		}

		public X509Store (StoreName storeName) 
			: this (storeName, StoreLocation.CurrentUser)
		{
		}

		public X509Store (StoreLocation storeLocation) 
			: this ("MY", storeLocation)
		{
		}

		public X509Store (StoreName storeName, StoreLocation storeLocation)
		{
			if ((storeName < StoreName.AddressBook) || (storeName > StoreName.TrustedPublisher))
				throw new ArgumentException ("storeName");
			if ((storeLocation < StoreLocation.CurrentUser) || (storeLocation > StoreLocation.LocalMachine))
				throw new ArgumentException ("storeLocation");

			switch (storeName) {
			case StoreName.CertificateAuthority:
				_name = "CA";
				break;
			default:
				_name = storeName.ToString ();
				break;
			}
			_location = storeLocation;
		}

		[MonoTODO ("Mono's stores are fully managed. All handles are invalid.")]
		[SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode=true)]
		public X509Store (IntPtr storeHandle)
		{
			if (storeHandle == IntPtr.Zero)
				throw new ArgumentNullException ("storeHandle");
			throw new CryptographicException ("Invalid handle.");
		}

		public X509Store (string storeName, StoreLocation storeLocation)
		{
			if ((storeLocation < StoreLocation.CurrentUser) || (storeLocation > StoreLocation.LocalMachine))
				throw new ArgumentException ("storeLocation");

			_name = storeName;
			_location = storeLocation;
		}

		// properties

		public X509Certificate2Collection Certificates {
			get {
				if (list == null)
					list = new X509Certificate2Collection ();
				else if (store == null)
					list.Clear ();

				return list;
			}
		} 

		public StoreLocation Location {
			get { return _location; }
		}

		public string Name {
			get { return _name; }
		}

		private MX.X509Stores Factory {
			get {
				if (_location == StoreLocation.CurrentUser)
					return MX.X509StoreManager.CurrentUser;
				else
					return MX.X509StoreManager.LocalMachine;
			}
		}

		private bool IsOpen {
			get { return (store != null); }
		}

		private bool IsReadOnly {
			get { return ((_flags & OpenFlags.ReadWrite) == OpenFlags.ReadOnly); }
		}

		internal MX.X509Store Store {
			get { return store; }
		}

		[MonoTODO ("Mono's stores are fully managed. Always returns IntPtr.Zero.")]
		public IntPtr StoreHandle {
			get { return IntPtr.Zero; }
		}

		// methods

		public void Add (X509Certificate2 certificate)
		{
			if (certificate == null)
				throw new ArgumentNullException ("certificate");
			if (!IsOpen)
				throw new CryptographicException (Locale.GetText ("Store isn't opened."));
			if (IsReadOnly)
				throw new CryptographicException (Locale.GetText ("Store is read-only."));

			if (!Exists (certificate)) {
				try {
					store.Import (new MX.X509Certificate (certificate.RawData));
				}
				finally {
					Certificates.Add (certificate);
				}
			}
		}

		[MonoTODO ("Method isn't transactional (like documented)")]
		public void AddRange (X509Certificate2Collection certificates)
		{
			if (certificates == null)
				throw new ArgumentNullException ("certificates");

			if (certificates.Count == 0)
				return;

			if (!IsOpen)
				throw new CryptographicException (Locale.GetText ("Store isn't opened."));
			if (IsReadOnly)
				throw new CryptographicException (Locale.GetText ("Store is read-only."));

			foreach (X509Certificate2 certificate in certificates) {
				if (!Exists (certificate)) {
					try {
						store.Import (new MX.X509Certificate (certificate.RawData));
					}
					finally {
						Certificates.Add (certificate);
					}
				}
			}
		}

		public void Close () 
		{
			store = null;
			if (list != null)
				list.Clear ();
		}

		public void Open (OpenFlags flags)
		{
			if (String.IsNullOrEmpty (_name))
				throw new CryptographicException (Locale.GetText ("Invalid store name (null or empty)."));

			/* keep existing Mono installations (pre 2.0) compatible with new stuff */
			string name;
			switch (_name) {
			case "Root":
				name = "Trust";
				break;
			default:
				name = _name;
				break;
			}

			bool create = ((flags & OpenFlags.OpenExistingOnly) != OpenFlags.OpenExistingOnly);
			store = Factory.Open (name, create);
			if (store == null)
				throw new CryptographicException (Locale.GetText ("Store {0} doesn't exists.", _name));
			_flags = flags;

			foreach (MX.X509Certificate x in store.Certificates) {
				Certificates.Add (new X509Certificate2 (x.RawData));
			}
		}

		public void Remove (X509Certificate2 certificate) 
		{
			if (certificate == null)
				throw new ArgumentNullException ("certificate");
			if (!IsOpen)
				throw new CryptographicException (Locale.GetText ("Store isn't opened."));

			if (!Exists (certificate))
				return;

			if (IsReadOnly)
				throw new CryptographicException (Locale.GetText ("Store is read-only."));

			try {
				store.Remove (new MX.X509Certificate (certificate.RawData));
			}
			finally {
				Certificates.Remove (certificate);
			}
		}

		[MonoTODO ("Method isn't transactional (like documented)")]
		public void RemoveRange (X509Certificate2Collection certificates) 
		{
			if (certificates == null)
				throw new ArgumentNullException ("certificates");

			if (certificates.Count == 0)
				return;

			if (!IsOpen)
				throw new CryptographicException (Locale.GetText ("Store isn't opened."));

			bool delete = false;
			foreach (X509Certificate2 certificate in certificates) {
				if (Exists (certificate))
					delete = true;
			}
			if (!delete)
				return;

			if (IsReadOnly)
				throw new CryptographicException (Locale.GetText ("Store is read-only."));

			try {
				foreach (X509Certificate2 certificate in certificates)
					store.Remove (new MX.X509Certificate (certificate.RawData));
			}
			finally {
				Certificates.RemoveRange (certificates);
			}
		}

		private bool Exists (X509Certificate2 certificate)
		{
			if ((store == null) || (list == null) || (certificate == null))
				return false;

			foreach (X509Certificate2 c in list) {
				if (certificate.Equals (c)) {
					return true;
				}
			}
			return false;
		}
	}
}

#endif