mscorlib(4.0.0.0) API with additions
ApplicationSecurityManager.cs
2 using System.IO;
3 using System.Reflection;
6 using System.Security.Util;
7 
9 {
11  [ComVisible(true)]
12  public static class ApplicationSecurityManager
13  {
14  private static volatile IApplicationTrustManager m_appTrustManager;
15 
16  private static string s_machineConfigFile;
17 
21  {
22  [SecuritySafeCritical]
23  [SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlPolicy)]
24  get
25  {
26  return new ApplicationTrustCollection(storeBounded: true);
27  }
28  }
29 
34  {
35  [SecuritySafeCritical]
36  [SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlPolicy)]
37  get
38  {
39  if (m_appTrustManager == null)
40  {
41  m_appTrustManager = DecodeAppTrustManager();
42  if (m_appTrustManager == null)
43  {
44  throw new PolicyException(Environment.GetResourceString("Policy_NoTrustManager"));
45  }
46  }
47  return m_appTrustManager;
48  }
49  }
50 
51  [SecuritySafeCritical]
53  {
54  m_appTrustManager = null;
55  s_machineConfigFile = Config.MachineDirectory + "applicationtrust.config";
56  }
57 
64  [SecurityCritical]
65  [SecurityPermission(SecurityAction.Assert, Unrestricted = true)]
66  public static bool DetermineApplicationTrust(ActivationContext activationContext, TrustManagerContext context)
67  {
68  if (activationContext == null)
69  {
70  throw new ArgumentNullException("activationContext");
71  }
72  ApplicationTrust applicationTrust = null;
74  if (domainManager != null)
75  {
76  HostSecurityManager hostSecurityManager = domainManager.HostSecurityManager;
77  if (hostSecurityManager != null && (hostSecurityManager.Flags & HostSecurityManagerOptions.HostDetermineApplicationTrust) == HostSecurityManagerOptions.HostDetermineApplicationTrust)
78  {
79  return hostSecurityManager.DetermineApplicationTrust(CmsUtils.MergeApplicationEvidence(null, activationContext.Identity, activationContext, null), null, context)?.IsApplicationTrustedToRun ?? false;
80  }
81  }
82  return DetermineApplicationTrustInternal(activationContext, context)?.IsApplicationTrustedToRun ?? false;
83  }
84 
85  [SecurityCritical]
86  internal static ApplicationTrust DetermineApplicationTrustInternal(ActivationContext activationContext, TrustManagerContext context)
87  {
88  ApplicationTrust applicationTrust = null;
89  ApplicationTrustCollection applicationTrustCollection = new ApplicationTrustCollection(storeBounded: true);
90  if (context == null || !context.IgnorePersistedDecision)
91  {
92  applicationTrust = applicationTrustCollection[activationContext.Identity.FullName];
93  if (applicationTrust != null)
94  {
95  return applicationTrust;
96  }
97  }
98  applicationTrust = ApplicationTrustManager.DetermineApplicationTrust(activationContext, context);
99  if (applicationTrust == null)
100  {
101  applicationTrust = new ApplicationTrust(activationContext.Identity);
102  }
103  applicationTrust.ApplicationIdentity = activationContext.Identity;
104  if (applicationTrust.Persist)
105  {
106  applicationTrustCollection.Add(applicationTrust);
107  }
108  return applicationTrust;
109  }
110 
111  [SecurityCritical]
112  private static IApplicationTrustManager DecodeAppTrustManager()
113  {
114  if (File.InternalExists(s_machineConfigFile))
115  {
116  string xml;
117  using (FileStream stream = new FileStream(s_machineConfigFile, FileMode.Open, FileAccess.Read))
118  {
119  xml = new StreamReader(stream).ReadToEnd();
120  }
121  SecurityElement securityElement = SecurityElement.FromString(xml);
122  SecurityElement securityElement2 = securityElement.SearchForChildByTag("mscorlib");
123  if (securityElement2 != null)
124  {
125  SecurityElement securityElement3 = securityElement2.SearchForChildByTag("security");
126  if (securityElement3 != null)
127  {
128  SecurityElement securityElement4 = securityElement3.SearchForChildByTag("policy");
129  if (securityElement4 != null)
130  {
131  SecurityElement securityElement5 = securityElement4.SearchForChildByTag("ApplicationSecurityManager");
132  if (securityElement5 != null)
133  {
134  SecurityElement securityElement6 = securityElement5.SearchForChildByTag("IApplicationTrustManager");
135  if (securityElement6 != null)
136  {
137  IApplicationTrustManager applicationTrustManager = DecodeAppTrustManagerFromElement(securityElement6);
138  if (applicationTrustManager != null)
139  {
140  return applicationTrustManager;
141  }
142  }
143  }
144  }
145  }
146  }
147  }
148  return DecodeAppTrustManagerFromElement(CreateDefaultApplicationTrustManagerElement());
149  }
150 
151  [SecurityCritical]
152  private static SecurityElement CreateDefaultApplicationTrustManagerElement()
153  {
154  SecurityElement securityElement = new SecurityElement("IApplicationTrustManager");
155  securityElement.AddAttribute("class", "System.Security.Policy.TrustManager, System.Windows.Forms, Version=" + ((RuntimeAssembly)Assembly.GetExecutingAssembly()).GetVersion() + ", Culture=neutral, PublicKeyToken=b77a5c561934e089");
156  securityElement.AddAttribute("version", "1");
157  return securityElement;
158  }
159 
160  [SecurityCritical]
161  private static IApplicationTrustManager DecodeAppTrustManagerFromElement(SecurityElement elTrustManager)
162  {
164  string typeName = elTrustManager.Attribute("class");
165  Type type = Type.GetType(typeName, throwOnError: false, ignoreCase: false);
166  if (type == null)
167  {
168  return null;
169  }
170  IApplicationTrustManager applicationTrustManager = Activator.CreateInstance(type) as IApplicationTrustManager;
171  applicationTrustManager?.FromXml(elTrustManager);
172  return applicationTrustManager;
173  }
174  }
175 }
static Assembly GetExecutingAssembly()
Gets the assembly that contains the code that is currently executing.
Definition: Assembly.cs:799
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
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.
virtual ApplicationTrust DetermineApplicationTrust(Evidence applicationEvidence, Evidence activatorEvidence, TrustManagerContext context)
Determines whether an application should be executed.
ReflectionPermissionFlag
Specifies the permitted use of the N:System.Reflection and N:System.Reflection.Emit namespaces.
int Add(ApplicationTrust trust)
Adds an element to the collection.
virtual bool IgnorePersistedDecision
Gets or sets a value indicating whether the application security manager should ignore any persisted ...
Determines whether an application should be executed and which set of permissions should be granted t...
FileMode
Specifies how the operating system should open a file.
Definition: FileMode.cs:8
Identifies the activation context for the current application. This class cannot be inherited.
Definition: __Canon.cs:3
HostSecurityManagerOptions
Specifies the security policy components to be used by the host security manager.
override string ReadToEnd()
Reads all characters from the current position to the end of the stream.
Implements a T:System.IO.TextReader that reads characters from a byte stream in a particular encoding...
Definition: StreamReader.cs:13
ApplicationIdentity Identity
Gets the application identity for the current application.
Manages trust decisions for manifest-activated applications.
static AppDomain CurrentDomain
Gets the current application domain for the current T:System.Threading.Thread.
Definition: AppDomain.cs:274
Represents an application domain, which is an isolated environment where applications execute....
Definition: AppDomain.cs:33
Represents the context for the trust manager to consider when making the decision to run an applicati...
SecurityAction
Specifies the security actions that can be performed using declarative security.
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
ApplicationTrust DetermineApplicationTrust(ActivationContext activationContext, TrustManagerContext context)
Determines whether an application should be executed and which set of permissions should be granted t...
static bool DetermineApplicationTrust(ActivationContext activationContext, TrustManagerContext context)
Determines whether the user approves the specified application to execute with the requested permissi...
Provides a T:System.IO.Stream for a file, supporting both synchronous and asynchronous read and write...
Definition: FileStream.cs:15
virtual HostSecurityManager HostSecurityManager
Gets the host security manager that participates in security decisions for the application domain.
Represents an assembly, which is a reusable, versionable, and self-describing building block of a com...
Definition: Assembly.cs:22
static IApplicationTrustManager ApplicationTrustManager
Gets the current application trust manager.
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.
Provides a managed equivalent of an unmanaged host.
Represents a collection of T:System.Security.Policy.ApplicationTrust objects. This class cannot be in...
FileAccess
Defines constants for read, write, or read/write access to a file.
Definition: FileAccess.cs:9
AppDomainManager DomainManager
Gets the domain manager that was provided by the host when the application domain was initialized.
Definition: AppDomain.cs:245
virtual HostSecurityManagerOptions Flags
Gets the flag representing the security policy components of concern to the host.
Provides static methods for the creation, copying, deletion, moving, and opening of a single file,...
Definition: File.cs:14
void Assert()
Declares that the calling code can access the resource protected by a permission demand through the c...
SecurityPermissionFlag
Specifies access flags for the security permission object.
string FullName
Gets the full name of the application.
Controls access to non-public types and members through the N:System.Reflection APIs....
static ApplicationTrustCollection UserApplicationTrusts
Gets an application trust collection that contains the cached trust decisions for the user.
The exception that is thrown when policy forbids code to run.
Allows the control and customization of security behavior for application domains.