mscorlib(4.0.0.0) API with additions
PublisherIdentityPermission.cs
1 using System.Collections;
4 using System.Security.Util;
5 
7 {
10  [ComVisible(true)]
11  public sealed class PublisherIdentityPermission : CodeAccessPermission, IBuiltInPermission
12  {
13  private bool m_unrestricted;
14 
15  private X509Certificate[] m_certs;
16 
23  {
24  get
25  {
26  if (m_certs == null || m_certs.Length < 1)
27  {
28  return null;
29  }
30  if (m_certs.Length > 1)
31  {
32  throw new NotSupportedException(Environment.GetResourceString("NotSupported_AmbiguousIdentity"));
33  }
34  if (m_certs[0] == null)
35  {
36  return null;
37  }
38  return new X509Certificate(m_certs[0]);
39  }
40  set
41  {
42  CheckCertificate(value);
43  m_unrestricted = false;
44  m_certs = new X509Certificate[1];
45  m_certs[0] = new X509Certificate(value);
46  }
47  }
48 
53  {
54  switch (state)
55  {
56  case PermissionState.Unrestricted:
57  m_unrestricted = true;
58  break;
59  case PermissionState.None:
60  m_unrestricted = false;
61  break;
62  default:
63  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidPermissionState"));
64  }
65  }
66 
72  {
73  Certificate = certificate;
74  }
75 
76  private static void CheckCertificate(X509Certificate certificate)
77  {
78  if (certificate == null)
79  {
80  throw new ArgumentNullException("certificate");
81  }
82  if (certificate.GetRawCertData() == null)
83  {
84  throw new ArgumentException(Environment.GetResourceString("Argument_UninitializedCertificate"));
85  }
86  }
87 
90  public override IPermission Copy()
91  {
92  PublisherIdentityPermission publisherIdentityPermission = new PublisherIdentityPermission(PermissionState.None);
93  publisherIdentityPermission.m_unrestricted = m_unrestricted;
94  if (m_certs != null)
95  {
96  publisherIdentityPermission.m_certs = new X509Certificate[m_certs.Length];
97  for (int i = 0; i < m_certs.Length; i++)
98  {
99  publisherIdentityPermission.m_certs[i] = ((m_certs[i] == null) ? null : new X509Certificate(m_certs[i]));
100  }
101  }
102  return publisherIdentityPermission;
103  }
104 
110  public override bool IsSubsetOf(IPermission target)
111  {
112  if (target == null)
113  {
114  if (m_unrestricted)
115  {
116  return false;
117  }
118  if (m_certs == null)
119  {
120  return true;
121  }
122  if (m_certs.Length == 0)
123  {
124  return true;
125  }
126  return false;
127  }
128  PublisherIdentityPermission publisherIdentityPermission = target as PublisherIdentityPermission;
129  if (publisherIdentityPermission == null)
130  {
131  throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", GetType().FullName));
132  }
133  if (publisherIdentityPermission.m_unrestricted)
134  {
135  return true;
136  }
137  if (m_unrestricted)
138  {
139  return false;
140  }
141  if (m_certs != null)
142  {
143  X509Certificate[] certs = m_certs;
144  foreach (X509Certificate x509Certificate in certs)
145  {
146  bool flag = false;
147  if (publisherIdentityPermission.m_certs != null)
148  {
149  X509Certificate[] certs2 = publisherIdentityPermission.m_certs;
150  foreach (X509Certificate other in certs2)
151  {
152  if (x509Certificate.Equals(other))
153  {
154  flag = true;
155  break;
156  }
157  }
158  }
159  if (!flag)
160  {
161  return false;
162  }
163  }
164  }
165  return true;
166  }
167 
172  public override IPermission Intersect(IPermission target)
173  {
174  if (target == null)
175  {
176  return null;
177  }
178  PublisherIdentityPermission publisherIdentityPermission = target as PublisherIdentityPermission;
179  if (publisherIdentityPermission == null)
180  {
181  throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", GetType().FullName));
182  }
183  if (m_unrestricted && publisherIdentityPermission.m_unrestricted)
184  {
185  PublisherIdentityPermission publisherIdentityPermission2 = new PublisherIdentityPermission(PermissionState.None);
186  publisherIdentityPermission2.m_unrestricted = true;
187  return publisherIdentityPermission2;
188  }
189  if (m_unrestricted)
190  {
191  return publisherIdentityPermission.Copy();
192  }
193  if (publisherIdentityPermission.m_unrestricted)
194  {
195  return Copy();
196  }
197  if (m_certs == null || publisherIdentityPermission.m_certs == null || m_certs.Length == 0 || publisherIdentityPermission.m_certs.Length == 0)
198  {
199  return null;
200  }
201  ArrayList arrayList = new ArrayList();
202  X509Certificate[] certs = m_certs;
203  foreach (X509Certificate x509Certificate in certs)
204  {
205  X509Certificate[] certs2 = publisherIdentityPermission.m_certs;
206  foreach (X509Certificate other in certs2)
207  {
208  if (x509Certificate.Equals(other))
209  {
210  arrayList.Add(new X509Certificate(x509Certificate));
211  }
212  }
213  }
214  if (arrayList.Count == 0)
215  {
216  return null;
217  }
218  PublisherIdentityPermission publisherIdentityPermission3 = new PublisherIdentityPermission(PermissionState.None);
219  publisherIdentityPermission3.m_certs = (X509Certificate[])arrayList.ToArray(typeof(X509Certificate));
220  return publisherIdentityPermission3;
221  }
222 
227  public override IPermission Union(IPermission target)
228  {
229  if (target == null)
230  {
231  if ((m_certs == null || m_certs.Length == 0) && !m_unrestricted)
232  {
233  return null;
234  }
235  return Copy();
236  }
237  PublisherIdentityPermission publisherIdentityPermission = target as PublisherIdentityPermission;
238  if (publisherIdentityPermission == null)
239  {
240  throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", GetType().FullName));
241  }
242  if (m_unrestricted || publisherIdentityPermission.m_unrestricted)
243  {
244  PublisherIdentityPermission publisherIdentityPermission2 = new PublisherIdentityPermission(PermissionState.None);
245  publisherIdentityPermission2.m_unrestricted = true;
246  return publisherIdentityPermission2;
247  }
248  if (m_certs == null || m_certs.Length == 0)
249  {
250  if (publisherIdentityPermission.m_certs == null || publisherIdentityPermission.m_certs.Length == 0)
251  {
252  return null;
253  }
254  return publisherIdentityPermission.Copy();
255  }
256  if (publisherIdentityPermission.m_certs == null || publisherIdentityPermission.m_certs.Length == 0)
257  {
258  return Copy();
259  }
260  ArrayList arrayList = new ArrayList();
261  X509Certificate[] certs = m_certs;
262  foreach (X509Certificate value in certs)
263  {
264  arrayList.Add(value);
265  }
266  X509Certificate[] certs2 = publisherIdentityPermission.m_certs;
267  foreach (X509Certificate x509Certificate in certs2)
268  {
269  bool flag = false;
270  foreach (X509Certificate item in arrayList)
271  {
272  if (x509Certificate.Equals(item))
273  {
274  flag = true;
275  break;
276  }
277  }
278  if (!flag)
279  {
280  arrayList.Add(x509Certificate);
281  }
282  }
283  PublisherIdentityPermission publisherIdentityPermission3 = new PublisherIdentityPermission(PermissionState.None);
284  publisherIdentityPermission3.m_certs = (X509Certificate[])arrayList.ToArray(typeof(X509Certificate));
285  return publisherIdentityPermission3;
286  }
287 
292  public override void FromXml(SecurityElement esd)
293  {
294  m_unrestricted = false;
295  m_certs = null;
296  CodeAccessPermission.ValidateElement(esd, this);
297  string text = esd.Attribute("Unrestricted");
298  if (text != null && string.Compare(text, "true", StringComparison.OrdinalIgnoreCase) == 0)
299  {
300  m_unrestricted = true;
301  return;
302  }
303  string text2 = esd.Attribute("X509v3Certificate");
304  ArrayList arrayList = new ArrayList();
305  if (text2 != null)
306  {
307  arrayList.Add(new X509Certificate(Hex.DecodeHexString(text2)));
308  }
309  ArrayList children = esd.Children;
310  if (children != null)
311  {
312  foreach (SecurityElement item in children)
313  {
314  text2 = item.Attribute("X509v3Certificate");
315  if (text2 != null)
316  {
317  arrayList.Add(new X509Certificate(Hex.DecodeHexString(text2)));
318  }
319  }
320  }
321  if (arrayList.Count != 0)
322  {
323  m_certs = (X509Certificate[])arrayList.ToArray(typeof(X509Certificate));
324  }
325  }
326 
329  public override SecurityElement ToXml()
330  {
331  SecurityElement securityElement = CodeAccessPermission.CreatePermissionElement(this, "System.Security.Permissions.PublisherIdentityPermission");
332  if (m_unrestricted)
333  {
334  securityElement.AddAttribute("Unrestricted", "true");
335  }
336  else if (m_certs != null)
337  {
338  if (m_certs.Length == 1)
339  {
340  securityElement.AddAttribute("X509v3Certificate", m_certs[0].GetRawCertDataString());
341  }
342  else
343  {
344  for (int i = 0; i < m_certs.Length; i++)
345  {
346  SecurityElement securityElement2 = new SecurityElement("Cert");
347  securityElement2.AddAttribute("X509v3Certificate", m_certs[i].GetRawCertDataString());
348  securityElement.AddChild(securityElement2);
349  }
350  }
351  }
352  return securityElement;
353  }
354 
355  int IBuiltInPermission.GetTokenIndex()
356  {
357  return GetTokenIndex();
358  }
359 
360  internal static int GetTokenIndex()
361  {
362  return 10;
363  }
364  }
365 }
PublisherIdentityPermission(PermissionState state)
Initializes a new instance of the T:System.Security.Permissions.PublisherIdentityPermission class wit...
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
StringComparison
Specifies the culture, case, and sort rules to be used by certain overloads of the M:System....
override SecurityElement ToXml()
Creates an XML encoding of the permission and its current state.
virtual int Count
Gets the number of elements actually contained in the T:System.Collections.ArrayList.
Definition: ArrayList.cs:2255
Definition: __Canon.cs:3
PublisherIdentityPermission(X509Certificate certificate)
Initializes a new instance of the T:System.Security.Permissions.PublisherIdentityPermission class wit...
override void FromXml(SecurityElement esd)
Reconstructs a permission with a specified state from an XML encoding.
void AddChild(SecurityElement child)
Adds a child element to the XML element.
override bool IsSubsetOf(IPermission target)
Determines whether the current permission is a subset of the specified permission.
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
Represents the XML object model for encoding security objects. This class cannot be inherited.
override IPermission Copy()
Creates and returns an identical copy of the current permission.
override IPermission Intersect(IPermission target)
Creates and returns a permission that is the intersection of the current permission and the specified...
Defines the underlying structure of all code access permissions.
Defines methods implemented by permission types.
Definition: IPermission.cs:7
virtual int Add(object value)
Adds an object to the end of the T:System.Collections.ArrayList.
Definition: ArrayList.cs:2381
Represents the identity of a software publisher. This class cannot be inherited.
The exception that is thrown when one of the arguments provided to a method is not valid.
X509Certificate Certificate
Gets or sets an Authenticode X.509v3 certificate that represents the identity of the software publish...
PermissionState
Specifies whether a permission should have all or no access to resources at creation.
virtual byte [] GetRawCertData()
Returns the raw data for the entire X.509v3 certificate as an array of bytes.
void AddAttribute(string name, string value)
Adds a name/value attribute to an XML element.
Specifies that the class can be serialized.
ArrayList Children
Gets or sets the array of child elements of the XML element.
override bool Equals(object obj)
Compares two T:System.Security.Cryptography.X509Certificates.X509Certificate objects for equality.
override IPermission Union(IPermission target)
Creates a permission that is the union of the current permission and the specified permission.
The exception that is thrown when an invoked method is not supported, or when there is an attempt to ...
Provides methods that help you use X.509 v.3 certificates.
virtual object [] ToArray()
Copies the elements of the T:System.Collections.ArrayList to a new T:System.Object array.
Definition: ArrayList.cs:3083
Implements the T:System.Collections.IList interface using an array whose size is dynamically increase...
Definition: ArrayList.cs:14