Evidence is a critical concept in Code Access Security (CAS) in .NET, used to determine the identity of the code that is being executed. The role of evidence is to classify and categorize the code based on its origin, ensuring that only trusted code can access resources and perform sensitive operations. When an assembly is loaded into the .NET runtime, it presents evidence that helps the Common Language Runtime (CLR) decide which permissions should be granted to that code.
In the context of CAS, evidence is the information about an assembly or piece of code that is used to determine its trustworthiness. Evidence can include various properties, such as:
The .NET runtime evaluates this evidence to assign security permissions to the code, and the code group (a collection of similar code) that the code belongs to determines what permissions it has.
There are several types of evidence that can be used to identify and classify assemblies. Some of the key evidence types include:
http://www.example.com/assembly.dll could be classified differently from code loaded from the local file system (file://C:/ProgramFiles/assembly.dll).UrlMembershipCondition urlCondition = new UrlMembershipCondition("http://www.example.com/*");
StrongNameMembershipCondition strongNameCondition = new StrongNameMembershipCondition("PublicKey", "AssemblyName", "Version");
PublisherMembershipCondition publisherCondition = new PublisherMembershipCondition(new X509Certificate("certificate_path"));
SiteMembershipCondition siteCondition = new SiteMembershipCondition("www.example.com");
In CAS, evidence is associated with code groups and is used to determine what permissions an assembly should have. The Common Language Runtime (CLR) evaluates the evidence presented by an assembly and uses it to match the assembly to a code group that defines the permissions it should receive.
When the CLR loads an assembly, it performs the following steps:
Collect Evidence: The CLR collects the evidence from the assembly. This includes things like its URL, digital signature, publisher certificate, and strong name.
Match Against Code Groups: The CLR then checks the security policy to see if any code groups match the evidence of the assembly. Code groups define what evidence is required to belong to them, and the CLR checks whether the assembly's evidence satisfies the membership conditions for any code group.
Grant Permissions: Once the evidence matches a code group, the CLR grants the permissions defined in that code group's associated permission set. If there is no matching code group, the assembly is treated as potentially untrusted and is granted minimal permissions (or no permissions).
Permission Checking: The CLR checks whether the code is allowed to perform actions, like reading a file or making a network request, based on the permissions granted by the code group.
Here is an example that demonstrates how evidence can be used to grant permissions:
using System;
using System.Security;
using System.Security.Policy;
class EvidenceExample
{
public static void Main()
{
// Define evidence based on the URL from which the code is loaded
UrlMembershipCondition urlEvidence = new UrlMembershipCondition("file:///*");
// Define evidence based on the strong name of the assembly
StrongNameMembershipCondition strongNameEvidence = new StrongNameMembershipCondition(
new StrongNamePublicKeyBlob(new byte[] { /* public key bytes */ }),
"MyAssembly",
new Version("1.0.0.0"));
// Combine evidence for policy evaluation
Evidence evidence = new Evidence();
evidence.Add(urlEvidence);
evidence.Add(strongNameEvidence);
// Perform a security demand
try
{
new PermissionSet(PermissionState.None).Demand();
Console.WriteLine("Permission granted!");
}
catch (SecurityException ex)
{
Console.WriteLine("Security Exception: " + ex.Message);
}
}
}
In this example:
file:///*).Based on the evidence, the CLR will assign the appropriate permissions to the assembly.
The security policy files (like machine.config, user.config, or app.config) map evidence types to code groups, each of which defines a set of permissions for matching assemblies. The policy settings ensure that code can only access the resources that are allowed based on its evidence.
In the security policy files, you might have entries like:
<codeGroup class="UnionCodeGroup" version="1" PermissionSetName="FullTrust">
<membershipConditions>
<urlMembershipCondition url="file:///*" />
</membershipConditions>
</codeGroup>
<codeGroup class="UnionCodeGroup" version="1" PermissionSetName="ExecutionOnly">
<membershipConditions>
<strongNameMembershipCondition publicKey="publicKeyBytes" />
</membershipConditions>
</codeGroup>
file:///*).Evidence in .NET's Code Access Security (CAS) is essential for classifying code and determining its trustworthiness. The CLR uses evidence such as the location, strong name, digital signature, and other factors to determine what permissions should be granted to an assembly. By combining different types of evidence, developers can define granular security policies to control access to system resources. This security mechanism allows for fine-grained control over the permissions granted to assemblies based on where they came from and what their identity is.
Open this section to load past papers