mscorlib(4.0.0.0) API with additions
KeyContainerPermission.cs
1 using System.Collections;
3 using System.Security.Util;
4 
6 {
9  [ComVisible(true)]
10  public sealed class KeyContainerPermission : CodeAccessPermission, IUnrestrictedPermission, IBuiltInPermission
11  {
12  private KeyContainerPermissionFlags m_flags;
13 
14  private KeyContainerPermissionAccessEntryCollection m_accessEntries;
15 
19 
23 
29  {
30  switch (state)
31  {
32  case PermissionState.Unrestricted:
33  m_flags = KeyContainerPermissionFlags.AllFlags;
34  break;
35  case PermissionState.None:
36  m_flags = KeyContainerPermissionFlags.NoFlags;
37  break;
38  default:
39  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidPermissionState"));
40  }
41  m_accessEntries = new KeyContainerPermissionAccessEntryCollection(m_flags);
42  }
43 
49  {
50  VerifyFlags(flags);
51  m_flags = flags;
52  m_accessEntries = new KeyContainerPermissionAccessEntryCollection(m_flags);
53  }
54 
63  {
64  if (accessList == null)
65  {
66  throw new ArgumentNullException("accessList");
67  }
68  VerifyFlags(flags);
69  m_flags = flags;
70  m_accessEntries = new KeyContainerPermissionAccessEntryCollection(m_flags);
71  for (int i = 0; i < accessList.Length; i++)
72  {
73  m_accessEntries.Add(accessList[i]);
74  }
75  }
76 
80  public bool IsUnrestricted()
81  {
82  if (m_flags != KeyContainerPermissionFlags.AllFlags)
83  {
84  return false;
85  }
87  while (enumerator.MoveNext())
88  {
89  KeyContainerPermissionAccessEntry current = enumerator.Current;
90  if ((current.Flags & KeyContainerPermissionFlags.AllFlags) != KeyContainerPermissionFlags.AllFlags)
91  {
92  return false;
93  }
94  }
95  return true;
96  }
97 
98  private bool IsEmpty()
99  {
100  if (Flags == KeyContainerPermissionFlags.NoFlags)
101  {
103  while (enumerator.MoveNext())
104  {
105  KeyContainerPermissionAccessEntry current = enumerator.Current;
106  if (current.Flags != 0)
107  {
108  return false;
109  }
110  }
111  return true;
112  }
113  return false;
114  }
115 
122  public override bool IsSubsetOf(IPermission target)
123  {
124  if (target == null)
125  {
126  return IsEmpty();
127  }
128  if (!VerifyType(target))
129  {
130  throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", GetType().FullName));
131  }
132  KeyContainerPermission keyContainerPermission = (KeyContainerPermission)target;
133  if ((m_flags & keyContainerPermission.m_flags) != m_flags)
134  {
135  return false;
136  }
138  while (enumerator.MoveNext())
139  {
140  KeyContainerPermissionAccessEntry current = enumerator.Current;
141  KeyContainerPermissionFlags applicableFlags = GetApplicableFlags(current, keyContainerPermission);
142  if ((current.Flags & applicableFlags) != current.Flags)
143  {
144  return false;
145  }
146  }
147  KeyContainerPermissionAccessEntryEnumerator enumerator2 = keyContainerPermission.AccessEntries.GetEnumerator();
148  while (enumerator2.MoveNext())
149  {
150  KeyContainerPermissionAccessEntry current2 = enumerator2.Current;
151  KeyContainerPermissionFlags applicableFlags2 = GetApplicableFlags(current2, this);
152  if ((applicableFlags2 & current2.Flags) != applicableFlags2)
153  {
154  return false;
155  }
156  }
157  return true;
158  }
159 
165  public override IPermission Intersect(IPermission target)
166  {
167  if (target == null)
168  {
169  return null;
170  }
171  if (!VerifyType(target))
172  {
173  throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", GetType().FullName));
174  }
175  KeyContainerPermission keyContainerPermission = (KeyContainerPermission)target;
176  if (IsEmpty() || keyContainerPermission.IsEmpty())
177  {
178  return null;
179  }
180  KeyContainerPermissionFlags flags = keyContainerPermission.m_flags & m_flags;
181  KeyContainerPermission keyContainerPermission2 = new KeyContainerPermission(flags);
183  while (enumerator.MoveNext())
184  {
185  KeyContainerPermissionAccessEntry current = enumerator.Current;
186  keyContainerPermission2.AddAccessEntryAndIntersect(current, keyContainerPermission);
187  }
188  KeyContainerPermissionAccessEntryEnumerator enumerator2 = keyContainerPermission.AccessEntries.GetEnumerator();
189  while (enumerator2.MoveNext())
190  {
191  KeyContainerPermissionAccessEntry current2 = enumerator2.Current;
192  keyContainerPermission2.AddAccessEntryAndIntersect(current2, this);
193  }
194  if (!keyContainerPermission2.IsEmpty())
195  {
196  return keyContainerPermission2;
197  }
198  return null;
199  }
200 
206  public override IPermission Union(IPermission target)
207  {
208  if (target == null)
209  {
210  return Copy();
211  }
212  if (!VerifyType(target))
213  {
214  throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", GetType().FullName));
215  }
216  KeyContainerPermission keyContainerPermission = (KeyContainerPermission)target;
217  if (IsUnrestricted() || keyContainerPermission.IsUnrestricted())
218  {
219  return new KeyContainerPermission(PermissionState.Unrestricted);
220  }
221  KeyContainerPermissionFlags flags = m_flags | keyContainerPermission.m_flags;
222  KeyContainerPermission keyContainerPermission2 = new KeyContainerPermission(flags);
224  while (enumerator.MoveNext())
225  {
226  KeyContainerPermissionAccessEntry current = enumerator.Current;
227  keyContainerPermission2.AddAccessEntryAndUnion(current, keyContainerPermission);
228  }
229  KeyContainerPermissionAccessEntryEnumerator enumerator2 = keyContainerPermission.AccessEntries.GetEnumerator();
230  while (enumerator2.MoveNext())
231  {
232  KeyContainerPermissionAccessEntry current2 = enumerator2.Current;
233  keyContainerPermission2.AddAccessEntryAndUnion(current2, this);
234  }
235  if (!keyContainerPermission2.IsEmpty())
236  {
237  return keyContainerPermission2;
238  }
239  return null;
240  }
241 
244  public override IPermission Copy()
245  {
246  if (IsEmpty())
247  {
248  return null;
249  }
250  KeyContainerPermission keyContainerPermission = new KeyContainerPermission(m_flags);
252  while (enumerator.MoveNext())
253  {
254  KeyContainerPermissionAccessEntry current = enumerator.Current;
255  keyContainerPermission.AccessEntries.Add(current);
256  }
257  return keyContainerPermission;
258  }
259 
262  public override SecurityElement ToXml()
263  {
264  SecurityElement securityElement = CodeAccessPermission.CreatePermissionElement(this, "System.Security.Permissions.KeyContainerPermission");
265  if (!IsUnrestricted())
266  {
267  securityElement.AddAttribute("Flags", m_flags.ToString());
268  if (AccessEntries.Count > 0)
269  {
270  SecurityElement securityElement2 = new SecurityElement("AccessList");
272  while (enumerator.MoveNext())
273  {
274  KeyContainerPermissionAccessEntry current = enumerator.Current;
275  SecurityElement securityElement3 = new SecurityElement("AccessEntry");
276  securityElement3.AddAttribute("KeyStore", current.KeyStore);
277  securityElement3.AddAttribute("ProviderName", current.ProviderName);
278  securityElement3.AddAttribute("ProviderType", current.ProviderType.ToString(null, null));
279  securityElement3.AddAttribute("KeyContainerName", current.KeyContainerName);
280  securityElement3.AddAttribute("KeySpec", current.KeySpec.ToString(null, null));
281  securityElement3.AddAttribute("Flags", current.Flags.ToString());
282  securityElement2.AddChild(securityElement3);
283  }
284  securityElement.AddChild(securityElement2);
285  }
286  }
287  else
288  {
289  securityElement.AddAttribute("Unrestricted", "true");
290  }
291  return securityElement;
292  }
293 
300  public override void FromXml(SecurityElement securityElement)
301  {
302  CodeAccessPermission.ValidateElement(securityElement, this);
303  if (XMLUtil.IsUnrestricted(securityElement))
304  {
305  m_flags = KeyContainerPermissionFlags.AllFlags;
306  m_accessEntries = new KeyContainerPermissionAccessEntryCollection(m_flags);
307  return;
308  }
309  m_flags = KeyContainerPermissionFlags.NoFlags;
310  string text = securityElement.Attribute("Flags");
311  if (text != null)
312  {
314  VerifyFlags(flags);
315  m_flags = flags;
316  }
317  m_accessEntries = new KeyContainerPermissionAccessEntryCollection(m_flags);
318  if (securityElement.InternalChildren == null || securityElement.InternalChildren.Count == 0)
319  {
320  return;
321  }
322  IEnumerator enumerator = securityElement.Children.GetEnumerator();
323  while (enumerator.MoveNext())
324  {
325  SecurityElement securityElement2 = (SecurityElement)enumerator.Current;
326  if (securityElement2 != null && string.Equals(securityElement2.Tag, "AccessList"))
327  {
328  AddAccessEntries(securityElement2);
329  }
330  }
331  }
332 
333  int IBuiltInPermission.GetTokenIndex()
334  {
335  return GetTokenIndex();
336  }
337 
338  private void AddAccessEntries(SecurityElement securityElement)
339  {
340  if (securityElement.InternalChildren == null || securityElement.InternalChildren.Count == 0)
341  {
342  return;
343  }
344  IEnumerator enumerator = securityElement.Children.GetEnumerator();
345  while (enumerator.MoveNext())
346  {
347  SecurityElement securityElement2 = (SecurityElement)enumerator.Current;
348  if (securityElement2 == null || !string.Equals(securityElement2.Tag, "AccessEntry"))
349  {
350  continue;
351  }
352  int count = securityElement2.m_lAttributes.Count;
353  string keyStore = null;
354  string providerName = null;
355  int providerType = -1;
356  string keyContainerName = null;
357  int keySpec = -1;
359  for (int i = 0; i < count; i += 2)
360  {
361  string a = (string)securityElement2.m_lAttributes[i];
362  string text = (string)securityElement2.m_lAttributes[i + 1];
363  if (string.Equals(a, "KeyStore"))
364  {
365  keyStore = text;
366  }
367  if (string.Equals(a, "ProviderName"))
368  {
369  providerName = text;
370  }
371  else if (string.Equals(a, "ProviderType"))
372  {
373  providerType = Convert.ToInt32(text, null);
374  }
375  else if (string.Equals(a, "KeyContainerName"))
376  {
377  keyContainerName = text;
378  }
379  else if (string.Equals(a, "KeySpec"))
380  {
381  keySpec = Convert.ToInt32(text, null);
382  }
383  else if (string.Equals(a, "Flags"))
384  {
385  flags = (KeyContainerPermissionFlags)Enum.Parse(typeof(KeyContainerPermissionFlags), text);
386  }
387  }
388  KeyContainerPermissionAccessEntry accessEntry = new KeyContainerPermissionAccessEntry(keyStore, providerName, providerType, keyContainerName, keySpec, flags);
389  AccessEntries.Add(accessEntry);
390  }
391  }
392 
393  private void AddAccessEntryAndUnion(KeyContainerPermissionAccessEntry accessEntry, KeyContainerPermission target)
394  {
395  KeyContainerPermissionAccessEntry keyContainerPermissionAccessEntry = new KeyContainerPermissionAccessEntry(accessEntry);
396  keyContainerPermissionAccessEntry.Flags |= GetApplicableFlags(accessEntry, target);
397  AccessEntries.Add(keyContainerPermissionAccessEntry);
398  }
399 
400  private void AddAccessEntryAndIntersect(KeyContainerPermissionAccessEntry accessEntry, KeyContainerPermission target)
401  {
402  KeyContainerPermissionAccessEntry keyContainerPermissionAccessEntry = new KeyContainerPermissionAccessEntry(accessEntry);
403  keyContainerPermissionAccessEntry.Flags &= GetApplicableFlags(accessEntry, target);
404  AccessEntries.Add(keyContainerPermissionAccessEntry);
405  }
406 
407  internal static void VerifyFlags(KeyContainerPermissionFlags flags)
408  {
410  {
411  throw new ArgumentException(Environment.GetResourceString("Arg_EnumIllegalVal", (int)flags));
412  }
413  }
414 
415  private static KeyContainerPermissionFlags GetApplicableFlags(KeyContainerPermissionAccessEntry accessEntry, KeyContainerPermission target)
416  {
417  KeyContainerPermissionFlags keyContainerPermissionFlags = KeyContainerPermissionFlags.NoFlags;
418  bool flag = true;
419  int num = target.AccessEntries.IndexOf(accessEntry);
420  if (num != -1)
421  {
422  return target.AccessEntries[num].Flags;
423  }
424  KeyContainerPermissionAccessEntryEnumerator enumerator = target.AccessEntries.GetEnumerator();
425  while (enumerator.MoveNext())
426  {
427  KeyContainerPermissionAccessEntry current = enumerator.Current;
428  if (accessEntry.IsSubsetOf(current))
429  {
430  if (!flag)
431  {
432  keyContainerPermissionFlags &= current.Flags;
433  continue;
434  }
435  keyContainerPermissionFlags = current.Flags;
436  flag = false;
437  }
438  }
439  if (flag)
440  {
441  keyContainerPermissionFlags = target.Flags;
442  }
443  return keyContainerPermissionFlags;
444  }
445 
446  private static int GetTokenIndex()
447  {
448  return 16;
449  }
450  }
451 }
Allows a permission to expose an unrestricted state.
override void FromXml(SecurityElement securityElement)
Reconstructs a permission with a specified state from an XML encoding.
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
bool MoveNext()
Moves to the next element in the collection.
bool MoveNext()
Advances the enumerator to the next element of the collection.
override IPermission Union(IPermission target)
Creates a permission that is the union of the current permission and the specified permission.
virtual int Count
Gets the number of elements actually contained in the T:System.Collections.ArrayList.
Definition: ArrayList.cs:2255
Definition: __Canon.cs:3
KeyContainerPermissionFlags Flags
Gets the key container permission flags that apply to all key containers associated with the permissi...
int KeySpec
Gets or sets the key specification.
Specifies access rights for specific key containers. This class cannot be inherited.
string ProviderName
Gets or sets the provider name.
Controls the ability to access key containers. This class cannot be inherited.
string Tag
Gets or sets the tag name of an XML element.
int ProviderType
Gets or sets the provider type.
static object Parse(Type enumType, string value)
Converts the string representation of the name or numeric value of one or more enumerated constants t...
Definition: Enum.cs:298
void AddChild(SecurityElement child)
Adds a child element to the XML element.
KeyContainerPermission(KeyContainerPermissionFlags flags, KeyContainerPermissionAccessEntry[] accessList)
Initializes a new instance of the T:System.Security.Permissions.KeyContainerPermission class with the...
KeyContainerPermissionAccessEntry Current
Gets the current entry in the collection.
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
bool IsUnrestricted()
Determines whether the current permission is unrestricted.
int Count
Gets the number of items in the collection.
KeyContainerPermission(PermissionState state)
Initializes a new instance of the T:System.Security.Permissions.KeyContainerPermission class with eit...
A cast or conversion operation, such as (SampleType)obj in C::or CType(obj, SampleType) in Visual Bas...
Provides the base class for enumerations.
Definition: Enum.cs:14
string KeyContainerName
Gets or sets the key container name.
Represents the XML object model for encoding security objects. This class cannot be inherited.
int Add(KeyContainerPermissionAccessEntry accessEntry)
Adds a T:System.Security.Permissions.KeyContainerPermissionAccessEntry object to the collection.
KeyContainerPermissionAccessEntryCollection AccessEntries
Gets the collection of T:System.Security.Permissions.KeyContainerPermissionAccessEntry objects associ...
object Current
Gets the element in the collection at the current position of the enumerator.
Definition: IEnumerator.cs:15
Defines the underlying structure of all code access permissions.
override bool Equals(object obj)
Determines whether the specified T:System.Security.CodeAccessPermission object is equal to the curren...
KeyContainerPermissionFlags
Specifies the type of key container access allowed.
Defines methods implemented by permission types.
Definition: IPermission.cs:7
The exception that is thrown when one of the arguments provided to a method is not valid.
KeyContainerPermission(KeyContainerPermissionFlags flags)
Initializes a new instance of the T:System.Security.Permissions.KeyContainerPermission class with the...
override SecurityElement ToXml()
Creates an XML encoding of the permission and its current state.
override IPermission Copy()
Creates and returns an identical copy of the current permission.
Attribute can be applied to an enumeration.
PermissionState
Specifies whether a permission should have all or no access to resources at creation.
KeyContainerPermissionAccessEntryEnumerator GetEnumerator()
Returns a T:System.Security.Permissions.KeyContainerPermissionAccessEntryEnumerator object that can b...
void AddAttribute(string name, string value)
Adds a name/value attribute to an XML element.
Specifies that the class can be serialized.
Represents a collection of T:System.Security.Permissions.KeyContainerPermissionAccessEntry objects....
ArrayList Children
Gets or sets the array of child elements of the XML element.
virtual IEnumerator GetEnumerator()
Returns an enumerator for the entire T:System.Collections.ArrayList.
Definition: ArrayList.cs:2615
override IPermission Intersect(IPermission target)
Creates and returns a permission that is the intersection of the current permission and the specified...
KeyContainerPermissionFlags Flags
Gets or sets the key container permissions.
override bool IsSubsetOf(IPermission target)
Determines whether the current permission is a subset of the specified permission.
Supports a simple iteration over a non-generic collection.
Definition: IEnumerator.cs:9
Represents the enumerator for T:System.Security.Permissions.KeyContainerPermissionAccessEntry objects...
string KeyStore
Gets or sets the name of the key store.