mscorlib(4.0.0.0) API with additions
ApplicationTrust.cs
1 using System.Collections;
3 using System.IO;
7 using System.Security.Util;
8 
10 {
12  [Serializable]
13  [ComVisible(true)]
15  {
16  private ApplicationIdentity m_appId;
17 
18  private bool m_appTrustedToRun;
19 
20  private bool m_persist;
21 
22  private object m_extraInfo;
23 
24  private SecurityElement m_elExtraInfo;
25 
26  private PolicyStatement m_psDefaultGrant;
27 
28  private IList<StrongName> m_fullTrustAssemblies;
29 
30  [NonSerialized]
31  private int m_grantSetSpecialFlags;
32 
38  {
39  get
40  {
41  return m_appId;
42  }
43  set
44  {
45  if (value == null)
46  {
47  throw new ArgumentNullException(Environment.GetResourceString("Argument_InvalidAppId"));
48  }
49  m_appId = value;
50  }
51  }
52 
56  {
57  get
58  {
59  if (m_psDefaultGrant == null)
60  {
61  return new PolicyStatement(new PermissionSet(PermissionState.None));
62  }
63  return m_psDefaultGrant;
64  }
65  set
66  {
67  if (value == null)
68  {
69  m_psDefaultGrant = null;
70  m_grantSetSpecialFlags = 0;
71  }
72  else
73  {
74  m_psDefaultGrant = value;
75  m_grantSetSpecialFlags = SecurityManager.GetSpecialFlags(m_psDefaultGrant.PermissionSet, null);
76  }
77  }
78  }
79 
82  public IList<StrongName> FullTrustAssemblies => m_fullTrustAssemblies;
83 
87  public bool IsApplicationTrustedToRun
88  {
89  get
90  {
91  return m_appTrustedToRun;
92  }
93  set
94  {
95  m_appTrustedToRun = value;
96  }
97  }
98 
102  public bool Persist
103  {
104  get
105  {
106  return m_persist;
107  }
108  set
109  {
110  m_persist = value;
111  }
112  }
113 
116  public object ExtraInfo
117  {
118  get
119  {
120  if (m_elExtraInfo != null)
121  {
122  m_extraInfo = ObjectFromXml(m_elExtraInfo);
123  m_elExtraInfo = null;
124  }
125  return m_extraInfo;
126  }
127  set
128  {
129  m_elExtraInfo = null;
130  m_extraInfo = value;
131  }
132  }
133 
136  public ApplicationTrust(ApplicationIdentity applicationIdentity)
137  : this()
138  {
139  ApplicationIdentity = applicationIdentity;
140  }
141 
144  : this(new PermissionSet(PermissionState.None))
145  {
146  }
147 
148  internal ApplicationTrust(PermissionSet defaultGrantSet)
149  {
150  InitDefaultGrantSet(defaultGrantSet);
151  m_fullTrustAssemblies = new List<StrongName>().AsReadOnly();
152  }
153 
163  public ApplicationTrust(PermissionSet defaultGrantSet, IEnumerable<StrongName> fullTrustAssemblies)
164  {
165  if (fullTrustAssemblies == null)
166  {
167  throw new ArgumentNullException("fullTrustAssemblies");
168  }
169  InitDefaultGrantSet(defaultGrantSet);
170  List<StrongName> list = new List<StrongName>();
171  foreach (StrongName fullTrustAssembly in fullTrustAssemblies)
172  {
173  if (fullTrustAssembly == null)
174  {
175  throw new ArgumentException(Environment.GetResourceString("Argument_NullFullTrustAssembly"));
176  }
177  list.Add(new StrongName(fullTrustAssembly.PublicKey, fullTrustAssembly.Name, fullTrustAssembly.Version));
178  }
179  m_fullTrustAssemblies = list.AsReadOnly();
180  }
181 
182  private void InitDefaultGrantSet(PermissionSet defaultGrantSet)
183  {
184  if (defaultGrantSet == null)
185  {
186  throw new ArgumentNullException("defaultGrantSet");
187  }
188  DefaultGrantSet = new PolicyStatement(defaultGrantSet);
189  }
190 
194  {
195  SecurityElement securityElement = new SecurityElement("ApplicationTrust");
196  securityElement.AddAttribute("version", "1");
197  if (m_appId != null)
198  {
199  securityElement.AddAttribute("FullName", SecurityElement.Escape(m_appId.FullName));
200  }
201  if (m_appTrustedToRun)
202  {
203  securityElement.AddAttribute("TrustedToRun", "true");
204  }
205  if (m_persist)
206  {
207  securityElement.AddAttribute("Persist", "true");
208  }
209  if (m_psDefaultGrant != null)
210  {
211  SecurityElement securityElement2 = new SecurityElement("DefaultGrant");
212  securityElement2.AddChild(m_psDefaultGrant.ToXml());
213  securityElement.AddChild(securityElement2);
214  }
215  if (m_fullTrustAssemblies.Count > 0)
216  {
217  SecurityElement securityElement3 = new SecurityElement("FullTrustAssemblies");
218  foreach (StrongName fullTrustAssembly in m_fullTrustAssemblies)
219  {
220  securityElement3.AddChild(fullTrustAssembly.ToXml());
221  }
222  securityElement.AddChild(securityElement3);
223  }
224  if (ExtraInfo != null)
225  {
226  securityElement.AddChild(ObjectToXml("ExtraInfo", ExtraInfo));
227  }
228  return securityElement;
229  }
230 
236  public void FromXml(SecurityElement element)
237  {
238  if (element == null)
239  {
240  throw new ArgumentNullException("element");
241  }
242  if (string.Compare(element.Tag, "ApplicationTrust", StringComparison.Ordinal) != 0)
243  {
244  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidXML"));
245  }
246  m_appTrustedToRun = false;
247  string text = element.Attribute("TrustedToRun");
248  if (text != null && string.Compare(text, "true", StringComparison.Ordinal) == 0)
249  {
250  m_appTrustedToRun = true;
251  }
252  m_persist = false;
253  string text2 = element.Attribute("Persist");
254  if (text2 != null && string.Compare(text2, "true", StringComparison.Ordinal) == 0)
255  {
256  m_persist = true;
257  }
258  m_appId = null;
259  string text3 = element.Attribute("FullName");
260  if (text3 != null && text3.Length > 0)
261  {
262  m_appId = new ApplicationIdentity(text3);
263  }
264  m_psDefaultGrant = null;
265  m_grantSetSpecialFlags = 0;
266  SecurityElement securityElement = element.SearchForChildByTag("DefaultGrant");
267  if (securityElement != null)
268  {
269  SecurityElement securityElement2 = securityElement.SearchForChildByTag("PolicyStatement");
270  if (securityElement2 != null)
271  {
272  PolicyStatement policyStatement = new PolicyStatement(null);
273  policyStatement.FromXml(securityElement2);
274  m_psDefaultGrant = policyStatement;
275  m_grantSetSpecialFlags = SecurityManager.GetSpecialFlags(policyStatement.PermissionSet, null);
276  }
277  }
278  List<StrongName> list = new List<StrongName>();
279  SecurityElement securityElement3 = element.SearchForChildByTag("FullTrustAssemblies");
280  if (securityElement3 != null && securityElement3.InternalChildren != null)
281  {
282  IEnumerator enumerator = securityElement3.Children.GetEnumerator();
283  while (enumerator.MoveNext())
284  {
285  StrongName strongName = new StrongName();
286  strongName.FromXml(enumerator.Current as SecurityElement);
287  list.Add(strongName);
288  }
289  }
290  m_fullTrustAssemblies = list.AsReadOnly();
291  m_elExtraInfo = element.SearchForChildByTag("ExtraInfo");
292  }
293 
294  private static SecurityElement ObjectToXml(string tag, object obj)
295  {
296  ISecurityEncodable securityEncodable = obj as ISecurityEncodable;
297  SecurityElement securityElement;
298  if (securityEncodable != null)
299  {
300  securityElement = securityEncodable.ToXml();
301  if (!securityElement.Tag.Equals(tag))
302  {
303  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidXML"));
304  }
305  }
306  MemoryStream memoryStream = new MemoryStream();
307  BinaryFormatter binaryFormatter = new BinaryFormatter();
308  binaryFormatter.Serialize(memoryStream, obj);
309  byte[] sArray = memoryStream.ToArray();
310  securityElement = new SecurityElement(tag);
311  securityElement.AddAttribute("Data", Hex.EncodeHexString(sArray));
312  return securityElement;
313  }
314 
315  private static object ObjectFromXml(SecurityElement elObject)
316  {
317  if (elObject.Attribute("class") != null)
318  {
319  ISecurityEncodable securityEncodable = XMLUtil.CreateCodeGroup(elObject) as ISecurityEncodable;
320  if (securityEncodable != null)
321  {
322  securityEncodable.FromXml(elObject);
323  return securityEncodable;
324  }
325  }
326  string hexString = elObject.Attribute("Data");
327  MemoryStream serializationStream = new MemoryStream(Hex.DecodeHexString(hexString));
328  BinaryFormatter binaryFormatter = new BinaryFormatter();
329  return binaryFormatter.Deserialize(serializationStream);
330  }
331 
334  [SecuritySafeCritical]
335  [SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
336  public override EvidenceBase Clone()
337  {
338  return base.Clone();
339  }
340  }
341 }
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.
Describes a set of security permissions applied to code. This class cannot be inherited.
bool Persist
Gets or sets a value indicating whether application trust information is persisted.
IList< StrongName > FullTrustAssemblies
Gets the list of full-trust assemblies for this application trust.
bool MoveNext()
Advances the enumerator to the next element of the collection.
Provides the strong name of a code assembly as evidence for policy evaluation. This class cannot be i...
Definition: StrongName.cs:12
StringComparison
Specifies the culture, case, and sort rules to be used by certain overloads of the M:System....
PermissionSet PermissionSet
Gets or sets the T:System.Security.PermissionSet of the policy statement.
Represents a non-generic collection of objects that can be individually accessed by index.
Definition: IList.cs:8
ReadOnlyCollection< T > AsReadOnly()
Returns a read-only T:System.Collections.ObjectModel.ReadOnlyCollection`1 wrapper for the current col...
Definition: List.cs:553
void Serialize(Stream serializationStream, object graph)
Serializes the object, or graph of objects with the specified top (root), to the given stream.
Definition: __Canon.cs:3
StrongNamePublicKeyBlob PublicKey
Gets the T:System.Security.Permissions.StrongNamePublicKeyBlob of the current T:System....
Definition: StrongName.cs:28
SecurityElement SearchForChildByTag(string tag)
Finds a child by its tag name.
Version Version
Gets the T:System.Version of the current T:System.Security.Policy.StrongName.
Definition: StrongName.cs:36
ApplicationTrust(PermissionSet defaultGrantSet, IEnumerable< StrongName > fullTrustAssemblies)
Initializes a new instance of the T:System.Security.Policy.ApplicationTrust class using the provided ...
string Tag
Gets or sets the tag name of an XML element.
Provides a base class from which all objects to be used as evidence must derive.
Definition: EvidenceBase.cs:12
void FromXml(SecurityElement e)
Reconstructs a security object with a specified state from an XML encoding.
Represents the statement of a T:System.Security.Policy.CodeGroup describing the permissions and other...
Serializes and deserializes an object, or an entire graph of connected objects, in binary format.
static string Escape(string str)
Replaces invalid XML characters in a string with their valid XML equivalent.
void AddChild(SecurityElement child)
Adds a child element to the XML element.
SecurityAction
Specifies the security actions that can be performed using declarative security.
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
Creates a stream whose backing store is memory.To browse the .NET Framework source code for this type...
Definition: MemoryStream.cs:13
Represents a collection that can contain many different types of permissions.
virtual byte [] ToArray()
Writes the stream contents to a byte array, regardless of the P:System.IO.MemoryStream....
Represents the XML object model for encoding security objects. This class cannot be inherited.
Defines the methods that convert permission object state to and from XML element representation.
object Current
Gets the element in the collection at the current position of the enumerator.
Definition: IEnumerator.cs:15
override EvidenceBase Clone()
Creates a new object that is a complete copy of the current instance.
SecurityElement ToXml()
Creates an XML encoding of the security object and its current state.
bool IsApplicationTrustedToRun
Gets or sets a value indicating whether the application has the required permission grants and is tru...
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...
SecurityElement ToXml()
Creates an XML encoding of the security object and its current state.
The exception that is thrown when one of the arguments provided to a method is not valid.
ApplicationTrust(ApplicationIdentity applicationIdentity)
Initializes a new instance of the T:System.Security.Policy.ApplicationTrust class with an T:System....
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition: List.cs:14
PermissionState
Specifies whether a permission should have all or no access to resources at creation.
void AddAttribute(string name, string value)
Adds a name/value attribute to an XML element.
ApplicationTrust()
Initializes a new instance of the T:System.Security.Policy.ApplicationTrust class.
Provides the ability to uniquely identify a manifest-activated application. This class cannot be inhe...
Specifies that the class can be serialized.
ArrayList Children
Gets or sets the array of child elements of the XML element.
void FromXml(SecurityElement et)
Reconstructs a security object with a given state from an XML encoding.
virtual IEnumerator GetEnumerator()
Returns an enumerator for the entire T:System.Collections.ArrayList.
Definition: ArrayList.cs:2615
Provides the main access point for classes interacting with the security system. This class cannot be...
int Count
Gets the number of elements contained in the T:System.Collections.ICollection.
Definition: ICollection.cs:14
object ExtraInfo
Gets or sets extra security information about the application.
Supports a simple iteration over a non-generic collection.
Definition: IEnumerator.cs:9
virtual EvidenceBase Clone()
Creates a new object that is a complete copy of the current instance.
Definition: EvidenceBase.cs:29
string Name
Gets the simple name of the current T:System.Security.Policy.StrongName.
Definition: StrongName.cs:32
PolicyStatement DefaultGrantSet
Gets or sets the policy statement defining the default grant set.