2 // System.Security.Permissions.KeyContainerPermission.cs
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.
29 using System
.Globalization
;
30 using System
.Runtime
.InteropServices
;
32 namespace System
.Security
.Permissions
{
36 public sealed class KeyContainerPermission
: CodeAccessPermission
, IUnrestrictedPermission
, IBuiltInPermission
{
38 private KeyContainerPermissionAccessEntryCollection _accessEntries
;
39 private KeyContainerPermissionFlags _flags
;
41 private const int version
= 1;
45 public KeyContainerPermission (PermissionState state
)
47 if (CheckPermissionState (state
, true) == PermissionState
.Unrestricted
) {
48 _flags
= KeyContainerPermissionFlags
.AllFlags
;
52 public KeyContainerPermission (KeyContainerPermissionFlags flags
)
57 public KeyContainerPermission (KeyContainerPermissionFlags flags
, KeyContainerPermissionAccessEntry
[] accessList
)
60 if (accessList
!= null) {
61 foreach (KeyContainerPermissionAccessEntry kcpae
in accessList
) {
62 _accessEntries
.Add (kcpae
);
69 public KeyContainerPermissionAccessEntryCollection AccessEntries
{
70 get { return _accessEntries; }
73 public KeyContainerPermissionFlags Flags
{
74 get { return _flags; }
79 public override IPermission
Copy ()
81 if (_accessEntries
.Count
== 0)
82 return new KeyContainerPermission (_flags
);
84 KeyContainerPermissionAccessEntry
[] list
= new KeyContainerPermissionAccessEntry
[_accessEntries
.Count
];
85 _accessEntries
.CopyTo (list
, 0);
86 return new KeyContainerPermission (_flags
, list
);
89 [MonoTODO ("(2.0) missing support for AccessEntries")]
90 public override void FromXml (SecurityElement securityElement
)
92 // General validation in CodeAccessPermission
93 CheckSecurityElement (securityElement
, "securityElement", version
, version
);
94 // Note: we do not (yet) care about the return value
95 // as we only accept version 1 (min/max values)
97 if (IsUnrestricted (securityElement
)) {
98 _flags
= KeyContainerPermissionFlags
.AllFlags
;
102 _flags
= (KeyContainerPermissionFlags
) Enum
.Parse (
103 typeof (KeyContainerPermissionFlags
), securityElement
.Attribute ("Flags"));
108 public override IPermission
Intersect (IPermission target
)
114 public override bool IsSubsetOf (IPermission target
)
119 public bool IsUnrestricted ()
121 return (_flags
== KeyContainerPermissionFlags
.AllFlags
);
124 [MonoTODO ("(2.0) missing support for AccessEntries")]
125 public override SecurityElement
ToXml ()
127 SecurityElement e
= Element (version
);
128 if (IsUnrestricted ()) {
129 e
.AddAttribute ("Unrestricted", "true");
136 public override IPermission
Union (IPermission target
)
138 KeyContainerPermission kcp
= Cast (target
);
142 KeyContainerPermissionAccessEntryCollection kcpaec
= new KeyContainerPermissionAccessEntryCollection ();
144 foreach (KeyContainerPermissionAccessEntry kcpae
in _accessEntries
) {
147 // copy second group...
148 foreach (KeyContainerPermissionAccessEntry kcpae
in kcp
._accessEntries
) {
149 // ... but only if not present in first group
150 if (_accessEntries
.IndexOf (kcpae
) == -1)
154 if (kcpaec
.Count
== 0)
155 return new KeyContainerPermission ((_flags
| kcp
._flags
));
157 KeyContainerPermissionAccessEntry
[] list
= new KeyContainerPermissionAccessEntry
[kcpaec
.Count
];
158 kcpaec
.CopyTo (list
, 0);
159 return new KeyContainerPermission ((_flags
| kcp
._flags
), list
);
162 // IBuiltInPermission
163 int IBuiltInPermission
.GetTokenIndex ()
165 return (int) BuiltInToken
.KeyContainer
;
170 private void SetFlags (KeyContainerPermissionFlags flags
)
172 if ((flags
& KeyContainerPermissionFlags
.AllFlags
) != 0) {
173 string msg
= String
.Format (Locale
.GetText ("Invalid enum {0}"), flags
);
174 throw new ArgumentException (msg
, "KeyContainerPermissionFlags");
179 private KeyContainerPermission
Cast (IPermission target
)
184 KeyContainerPermission kcp
= (target
as KeyContainerPermission
);
186 ThrowInvalidPermission (target
, typeof (KeyContainerPermission
));