2 // System.Security.Permissions.KeyContainerPermission class
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Globalization;
32 using System.Runtime.InteropServices;
34 namespace System.Security.Permissions {
38 public sealed class KeyContainerPermission : CodeAccessPermission, IUnrestrictedPermission, IBuiltInPermission {
40 private KeyContainerPermissionAccessEntryCollection _accessEntries;
41 private KeyContainerPermissionFlags _flags;
43 private const int version = 1;
47 public KeyContainerPermission (PermissionState state)
49 if (CheckPermissionState (state, true) == PermissionState.Unrestricted) {
50 _flags = KeyContainerPermissionFlags.AllFlags;
54 public KeyContainerPermission (KeyContainerPermissionFlags flags)
59 public KeyContainerPermission (KeyContainerPermissionFlags flags, KeyContainerPermissionAccessEntry[] accessList)
62 if (accessList != null) {
63 foreach (KeyContainerPermissionAccessEntry kcpae in accessList) {
64 _accessEntries.Add (kcpae);
71 public KeyContainerPermissionAccessEntryCollection AccessEntries {
72 get { return _accessEntries; }
75 public KeyContainerPermissionFlags Flags {
76 get { return _flags; }
81 public override IPermission Copy ()
83 if (_accessEntries.Count == 0)
84 return new KeyContainerPermission (_flags);
86 KeyContainerPermissionAccessEntry[] list = new KeyContainerPermissionAccessEntry [_accessEntries.Count];
87 _accessEntries.CopyTo (list, 0);
88 return new KeyContainerPermission (_flags, list);
91 [MonoTODO ("(2.0) missing support for AccessEntries")]
92 public override void FromXml (SecurityElement esd)
94 // General validation in CodeAccessPermission
95 CheckSecurityElement (esd, "esd", version, version);
96 // Note: we do not (yet) care about the return value
97 // as we only accept version 1 (min/max values)
99 if (IsUnrestricted (esd)) {
100 _flags = KeyContainerPermissionFlags.AllFlags;
104 _flags = (KeyContainerPermissionFlags) Enum.Parse (
105 typeof (KeyContainerPermissionFlags), esd.Attribute ("Flags"));
110 public override IPermission Intersect (IPermission target)
116 public override bool IsSubsetOf (IPermission target)
121 public bool IsUnrestricted ()
123 return (_flags == KeyContainerPermissionFlags.AllFlags);
126 [MonoTODO ("(2.0) missing support for AccessEntries")]
127 public override SecurityElement ToXml ()
129 SecurityElement e = Element (version);
130 if (IsUnrestricted ()) {
131 e.AddAttribute ("Unrestricted", "true");
138 public override IPermission Union (IPermission target)
140 KeyContainerPermission kcp = Cast (target);
144 KeyContainerPermissionAccessEntryCollection kcpaec = new KeyContainerPermissionAccessEntryCollection ();
146 foreach (KeyContainerPermissionAccessEntry kcpae in _accessEntries) {
149 // copy second group...
150 foreach (KeyContainerPermissionAccessEntry kcpae in kcp._accessEntries) {
151 // ... but only if not present in first group
152 if (_accessEntries.IndexOf (kcpae) == -1)
156 if (kcpaec.Count == 0)
157 return new KeyContainerPermission ((_flags | kcp._flags));
159 KeyContainerPermissionAccessEntry[] list = new KeyContainerPermissionAccessEntry [kcpaec.Count];
160 kcpaec.CopyTo (list, 0);
161 return new KeyContainerPermission ((_flags | kcp._flags), list);
164 // IBuiltInPermission
165 int IBuiltInPermission.GetTokenIndex ()
167 return (int) BuiltInToken.KeyContainer;
172 private void SetFlags (KeyContainerPermissionFlags flags)
174 if ((flags & KeyContainerPermissionFlags.AllFlags) != 0) {
175 string msg = String.Format (Locale.GetText ("Invalid enum {0}"), flags);
176 throw new ArgumentException (msg, "KeyContainerPermissionFlags");
181 private KeyContainerPermission Cast (IPermission target)
186 KeyContainerPermission kcp = (target as KeyContainerPermission);
188 ThrowInvalidPermission (target, typeof (KeyContainerPermission));