mscorlib(4.0.0.0) API with additions
ApplicationTrustCollection.cs
1 using System.Collections;
5 using System.Threading;
6 
8 {
10  [SecurityCritical]
11  [ComVisible(true)]
13  {
14  private const string ApplicationTrustProperty = "ApplicationTrust";
15 
16  private const string InstallerIdentifier = "{60051b8f-4f12-400a-8e50-dd05ebd438d1}";
17 
18  private static Guid ClrPropertySet = new Guid("c989bb7a-8385-4715-98cf-a741a8edb823");
19 
20  private static object s_installReference = null;
21 
22  private object m_appTrusts;
23 
24  private bool m_storeBounded;
25 
26  private Store m_pStore;
27 
28  private static StoreApplicationReference InstallReference
29  {
30  get
31  {
32  if (s_installReference == null)
33  {
34  Interlocked.CompareExchange(ref s_installReference, new StoreApplicationReference(IsolationInterop.GUID_SXS_INSTALL_REFERENCE_SCHEME_OPAQUESTRING, "{60051b8f-4f12-400a-8e50-dd05ebd438d1}", null), null);
35  }
36  return (StoreApplicationReference)s_installReference;
37  }
38  }
39 
40  private ArrayList AppTrusts
41  {
42  [SecurityCritical]
43  get
44  {
45  if (m_appTrusts == null)
46  {
47  ArrayList arrayList = new ArrayList();
48  if (m_storeBounded)
49  {
50  RefreshStorePointer();
51  StoreDeploymentMetadataEnumeration storeDeploymentMetadataEnumeration = m_pStore.EnumInstallerDeployments(IsolationInterop.GUID_SXS_INSTALL_REFERENCE_SCHEME_OPAQUESTRING, "{60051b8f-4f12-400a-8e50-dd05ebd438d1}", "ApplicationTrust", null);
52  foreach (IDefinitionAppId item in storeDeploymentMetadataEnumeration)
53  {
54  StoreDeploymentMetadataPropertyEnumeration storeDeploymentMetadataPropertyEnumeration = m_pStore.EnumInstallerDeploymentProperties(IsolationInterop.GUID_SXS_INSTALL_REFERENCE_SCHEME_OPAQUESTRING, "{60051b8f-4f12-400a-8e50-dd05ebd438d1}", "ApplicationTrust", item);
55  foreach (StoreOperationMetadataProperty item2 in storeDeploymentMetadataPropertyEnumeration)
56  {
57  string value = item2.Value;
58  if (value != null && value.Length > 0)
59  {
61  ApplicationTrust applicationTrust = new ApplicationTrust();
62  applicationTrust.FromXml(element);
63  arrayList.Add(applicationTrust);
64  }
65  }
66  }
67  }
68  Interlocked.CompareExchange(ref m_appTrusts, arrayList, null);
69  }
70  return m_appTrusts as ArrayList;
71  }
72  }
73 
76  public int Count
77  {
78  [SecuritySafeCritical]
79  get
80  {
81  return AppTrusts.Count;
82  }
83  }
84 
92  public ApplicationTrust this[int index]
93  {
94  [SecurityCritical]
95  get
96  {
97  return AppTrusts[index] as ApplicationTrust;
98  }
99  }
100 
104  public ApplicationTrust this[string appFullName]
105  {
106  [SecurityCritical]
107  get
108  {
109  ApplicationIdentity applicationIdentity = new ApplicationIdentity(appFullName);
110  ApplicationTrustCollection applicationTrustCollection = Find(applicationIdentity, ApplicationVersionMatch.MatchExactVersion);
111  if (applicationTrustCollection.Count > 0)
112  {
113  return applicationTrustCollection[0];
114  }
115  return null;
116  }
117  }
118 
122  public bool IsSynchronized
123  {
124  [SecuritySafeCritical]
125  get
126  {
127  return false;
128  }
129  }
130 
133  public object SyncRoot
134  {
135  [SecuritySafeCritical]
136  get
137  {
138  return this;
139  }
140  }
141 
142  [SecurityCritical]
143  internal ApplicationTrustCollection()
144  : this(storeBounded: false)
145  {
146  }
147 
148  internal ApplicationTrustCollection(bool storeBounded)
149  {
150  m_storeBounded = storeBounded;
151  }
152 
153  [SecurityCritical]
154  private void RefreshStorePointer()
155  {
156  if (m_pStore != null)
157  {
158  Marshal.ReleaseComObject(m_pStore.InternalStore);
159  }
160  m_pStore = IsolationInterop.GetUserStore();
161  }
162 
163  [SecurityCritical]
164  private void CommitApplicationTrust(ApplicationIdentity applicationIdentity, string trustXml)
165  {
166  StoreOperationMetadataProperty[] setProperties = new StoreOperationMetadataProperty[1]
167  {
168  new StoreOperationMetadataProperty(ClrPropertySet, "ApplicationTrust", trustXml)
169  };
170  IEnumDefinitionIdentity enumDefinitionIdentity = applicationIdentity.Identity.EnumAppPath();
171  IDefinitionIdentity[] array = new IDefinitionIdentity[1];
172  IDefinitionIdentity definitionIdentity = null;
173  if (enumDefinitionIdentity.Next(1u, array) == 1)
174  {
175  definitionIdentity = array[0];
176  }
177  IDefinitionAppId definitionAppId = IsolationInterop.AppIdAuthority.CreateDefinition();
178  definitionAppId.SetAppPath(1u, new IDefinitionIdentity[1]
179  {
180  definitionIdentity
181  });
182  definitionAppId.put_Codebase(applicationIdentity.CodeBase);
183  using (StoreTransaction storeTransaction = new StoreTransaction())
184  {
185  storeTransaction.Add(new StoreOperationSetDeploymentMetadata(definitionAppId, InstallReference, setProperties));
186  RefreshStorePointer();
187  m_pStore.Transact(storeTransaction.Operations);
188  }
189  m_appTrusts = null;
190  }
191 
198  [SecurityCritical]
199  public int Add(ApplicationTrust trust)
200  {
201  if (trust == null)
202  {
203  throw new ArgumentNullException("trust");
204  }
205  if (trust.ApplicationIdentity == null)
206  {
207  throw new ArgumentException(Environment.GetResourceString("Argument_ApplicationTrustShouldHaveIdentity"));
208  }
209  if (m_storeBounded)
210  {
211  CommitApplicationTrust(trust.ApplicationIdentity, trust.ToXml().ToString());
212  return -1;
213  }
214  return AppTrusts.Add(trust);
215  }
216 
222  [SecurityCritical]
223  public void AddRange(ApplicationTrust[] trusts)
224  {
225  if (trusts == null)
226  {
227  throw new ArgumentNullException("trusts");
228  }
229  int i = 0;
230  try
231  {
232  for (; i < trusts.Length; i++)
233  {
234  Add(trusts[i]);
235  }
236  }
237  catch
238  {
239  for (int j = 0; j < i; j++)
240  {
241  Remove(trusts[j]);
242  }
243  throw;
244  }
245  }
246 
252  [SecurityCritical]
254  {
255  if (trusts == null)
256  {
257  throw new ArgumentNullException("trusts");
258  }
259  int num = 0;
260  try
261  {
262  ApplicationTrustEnumerator enumerator = trusts.GetEnumerator();
263  while (enumerator.MoveNext())
264  {
265  ApplicationTrust current = enumerator.Current;
266  Add(current);
267  num++;
268  }
269  }
270  catch
271  {
272  for (int i = 0; i < num; i++)
273  {
274  Remove(trusts[i]);
275  }
276  throw;
277  }
278  }
279 
284  [SecurityCritical]
286  {
287  ApplicationTrustCollection applicationTrustCollection = new ApplicationTrustCollection(storeBounded: false);
289  while (enumerator.MoveNext())
290  {
291  ApplicationTrust current = enumerator.Current;
292  if (CmsUtils.CompareIdentities(current.ApplicationIdentity, applicationIdentity, versionMatch))
293  {
294  applicationTrustCollection.Add(current);
295  }
296  }
297  return applicationTrustCollection;
298  }
299 
303  [SecurityCritical]
304  public void Remove(ApplicationIdentity applicationIdentity, ApplicationVersionMatch versionMatch)
305  {
306  ApplicationTrustCollection trusts = Find(applicationIdentity, versionMatch);
307  RemoveRange(trusts);
308  }
309 
315  [SecurityCritical]
316  public void Remove(ApplicationTrust trust)
317  {
318  if (trust == null)
319  {
320  throw new ArgumentNullException("trust");
321  }
322  if (trust.ApplicationIdentity == null)
323  {
324  throw new ArgumentException(Environment.GetResourceString("Argument_ApplicationTrustShouldHaveIdentity"));
325  }
326  if (m_storeBounded)
327  {
328  CommitApplicationTrust(trust.ApplicationIdentity, null);
329  }
330  else
331  {
332  AppTrusts.Remove(trust);
333  }
334  }
335 
340  [SecurityCritical]
341  public void RemoveRange(ApplicationTrust[] trusts)
342  {
343  if (trusts == null)
344  {
345  throw new ArgumentNullException("trusts");
346  }
347  int i = 0;
348  try
349  {
350  for (; i < trusts.Length; i++)
351  {
352  Remove(trusts[i]);
353  }
354  }
355  catch
356  {
357  for (int j = 0; j < i; j++)
358  {
359  Add(trusts[j]);
360  }
361  throw;
362  }
363  }
364 
369  [SecurityCritical]
371  {
372  if (trusts == null)
373  {
374  throw new ArgumentNullException("trusts");
375  }
376  int num = 0;
377  try
378  {
379  ApplicationTrustEnumerator enumerator = trusts.GetEnumerator();
380  while (enumerator.MoveNext())
381  {
382  ApplicationTrust current = enumerator.Current;
383  Remove(current);
384  num++;
385  }
386  }
387  catch
388  {
389  for (int i = 0; i < num; i++)
390  {
391  Add(trusts[i]);
392  }
393  throw;
394  }
395  }
396 
399  [SecurityCritical]
400  public void Clear()
401  {
402  ArrayList appTrusts = AppTrusts;
403  if (m_storeBounded)
404  {
405  foreach (ApplicationTrust item in appTrusts)
406  {
407  if (item.ApplicationIdentity == null)
408  {
409  throw new ArgumentException(Environment.GetResourceString("Argument_ApplicationTrustShouldHaveIdentity"));
410  }
411  CommitApplicationTrust(item.ApplicationIdentity, null);
412  }
413  }
414  appTrusts.Clear();
415  }
416 
420  {
421  return new ApplicationTrustEnumerator(this);
422  }
423 
426  [SecuritySafeCritical]
428  {
429  return new ApplicationTrustEnumerator(this);
430  }
431 
441  [SecuritySafeCritical]
442  void ICollection.CopyTo(Array array, int index)
443  {
444  if (array == null)
445  {
446  throw new ArgumentNullException("array");
447  }
448  if (array.Rank != 1)
449  {
450  throw new ArgumentException(Environment.GetResourceString("Arg_RankMultiDimNotSupported"));
451  }
452  if (index < 0 || index >= array.Length)
453  {
454  throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index"));
455  }
456  if (array.Length - index < Count)
457  {
458  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
459  }
460  for (int i = 0; i < Count; i++)
461  {
462  array.SetValue(this[i], index++);
463  }
464  }
465 
475  public void CopyTo(ApplicationTrust[] array, int index)
476  {
477  ((ICollection)this).CopyTo((Array)array, index);
478  }
479  }
480 }
Represents the enumerator for T:System.Security.Policy.ApplicationTrust objects in the T:System....
void Clear()
Removes all the application trusts from the collection.
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
void FromXml(SecurityElement element)
Reconstructs an T:System.Security.Policy.ApplicationTrust object with a given state from an XML encod...
Encapsulates security decisions about an application. This class cannot be inherited.
bool IsSynchronized
Gets a value indicating whether access to the collection is synchronized (thread safe).
static int ReleaseComObject(object o)
Decrements the reference count of the Runtime Callable Wrapper (RCW) associated with the specified CO...
Definition: Marshal.cs:2179
int Add(ApplicationTrust trust)
Adds an element to the collection.
virtual int Count
Gets the number of elements actually contained in the T:System.Collections.ArrayList.
Definition: ArrayList.cs:2255
Definition: __Canon.cs:3
bool MoveNext()
Moves to the next element in the T:System.Security.Policy.ApplicationTrustCollection collection.
ApplicationTrust Current
Gets the current T:System.Security.Policy.ApplicationTrust object in the T:System....
void CopyTo(ApplicationTrust[] array, int index)
Copies the entire collection to a compatible one-dimensional array, starting at the specified index o...
virtual void Clear()
Removes all elements from the T:System.Collections.ArrayList.
Definition: ArrayList.cs:2461
void Remove(ApplicationTrust trust)
Removes the specified application trust from the collection.
void RemoveRange(ApplicationTrust[] trusts)
Removes the application trust objects in the specified array from the collection.
int Count
Gets the number of items contained in the collection.
Exposes an enumerator, which supports a simple iteration over a non-generic collection....
Definition: IEnumerable.cs:9
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
ApplicationTrustEnumerator GetEnumerator()
Returns an object that can be used to iterate over the collection.
Represents a globally unique identifier (GUID).To browse the .NET Framework source code for this type...
Definition: Guid.cs:14
ApplicationTrustCollection Find(ApplicationIdentity applicationIdentity, ApplicationVersionMatch versionMatch)
Gets the application trusts in the collection that match the specified application identity.
Represents the XML object model for encoding security objects. This class cannot be inherited.
static int CompareExchange(ref int location1, int value, int comparand)
Compares two 32-bit signed integers for equality and, if they are equal, replaces the first value.
void RemoveRange(ApplicationTrustCollection trusts)
Removes the application trust objects in the specified collection from the collection.
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition: Array.cs:17
Provides a collection of methods for allocating unmanaged memory, copying unmanaged memory blocks,...
Definition: Marshal.cs:15
ApplicationVersionMatch
Specifies how to match versions when locating application trusts in a collection.
IEnumerator GetEnumerator()
Returns an enumerator that iterates through a collection.
ApplicationIdentity ApplicationIdentity
Gets or sets the application identity for the application trust object.
SecurityElement ToXml()
Creates an XML encoding of the T:System.Security.Policy.ApplicationTrust object and its current state...
virtual int Add(object value)
Adds an object to the end of the T:System.Collections.ArrayList.
Definition: ArrayList.cs:2381
object SyncRoot
Gets an object that can be used to synchronize access to the collection.
The exception that is thrown when one of the arguments provided to a method is not valid.
static SecurityElement FromString(string xml)
Creates a security element from an XML-encoded string.
Represents a collection of T:System.Security.Policy.ApplicationTrust objects. This class cannot be in...
virtual void Remove(object obj)
Removes the first occurrence of a specific object from the T:System.Collections.ArrayList.
Definition: ArrayList.cs:2832
Provides the ability to uniquely identify a manifest-activated application. This class cannot be inhe...
void Remove(ApplicationIdentity applicationIdentity, ApplicationVersionMatch versionMatch)
Removes all application trust objects that match the specified criteria from the collection.
Defines size, enumerators, and synchronization methods for all nongeneric collections.
Definition: ICollection.cs:8
void CopyTo(Array array, int index)
Copies the elements of the T:System.Collections.ICollection to an T:System.Array, starting at a parti...
Provides atomic operations for variables that are shared by multiple threads.
Definition: Interlocked.cs:10
void AddRange(ApplicationTrustCollection trusts)
Copies the elements of the specified T:System.Security.Policy.ApplicationTrustCollection to the end o...
Supports a simple iteration over a non-generic collection.
Definition: IEnumerator.cs:9
override string ToString()
Produces a string representation of an XML element and its constituent attributes,...
void AddRange(ApplicationTrust[] trusts)
Copies the elements of the specified T:System.Security.Policy.ApplicationTrust array to the end of th...
Implements the T:System.Collections.IList interface using an array whose size is dynamically increase...
Definition: ArrayList.cs:14