<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/atom.xsl" media="screen"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-US"><title type="html">.NET Security Blog</title><subtitle type="html" /><id>http://blogs.msdn.com/shawnfa/atom.xml</id><link rel="alternate" type="text/html" href="http://blogs.msdn.com/shawnfa/default.aspx" /><link rel="self" type="application/atom+xml" href="http://blogs.msdn.com/shawnfa/atom.xml" /><generator uri="http://communityserver.org" version="2.1.61025.2">Community Server</generator><updated>2008-12-02T14:26:23Z</updated><entry><title>Bridging the Gap Between Transparent and Critical Code</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/shawnfa/archive/2009/11/05/bridging-the-gap-between-transparent-and-critical-code.aspx" /><id>http://blogs.msdn.com/shawnfa/archive/2009/11/05/bridging-the-gap-between-transparent-and-critical-code.aspx</id><published>2009-11-05T17:59:29Z</published><updated>2009-11-05T17:59:29Z</updated><content type="html">&lt;p&gt;Last time we looked at &lt;a href="http://blogs.msdn.com/shawnfa/archive/2009/11/03/transparency-101-basic-transparency-rules.aspx"&gt;the set of operations that can only be performed by security critical code&lt;/a&gt;. One interesting observation is that just because you are doing one of these operations does not mean that your method in and of itself is security sensitive. For instance, you might implement a method with unverifiable IL as a performance optimization - however that optimization is done in an inherently safe way. &lt;p&gt;Another example of a safe operation that uses security critical constructs is the Isolated Storage example from that post. Although Isolated Storage performs a security assert, which is security sensitive and requires it to be critical, it makes this assert safe by several techniques including issuing a demand for IsolatedStoragePermission. &lt;p&gt;Similarly, the file classes might use P/Invokes in order to implement their functionality. However, they ensure that this is safe by issuing a demand for FileIOPermission in order to ensure that they are only used in sandboxes which explicitly decided to allow access to the file system. &lt;p&gt;In these cases, you might want transparent code to be able to call into your method, since the fact that it is doing something critical is more of an implementation detail than anything else. These methods form the boundary between the security sensitive portions of your code and the security transparent portions, and are marked as Security Safe Critical. &lt;p&gt;A security safe critical method can do everything that a security critical method can do, however it does not require that its caller (or overriding methods) be security critical themselves. Instead, it takes on the responsibility of validating that all of its operations are safe. This includes (but is not limited to): &lt;ol&gt; &lt;li&gt;&lt;em&gt;Verifying that the core operations it is performing are safe&lt;/em&gt;. A method that formats the hard disk would never be security safe critical for instance.&lt;/li&gt; &lt;li&gt;&lt;em&gt;Verifying that the inputs that the method uses make sense&lt;/em&gt;. For example, Isolated Storage only allows access to paths within the Isolated Storage root and rejects attempts to use its APIs to open arbitrary files on the machine.&lt;/li&gt; &lt;li&gt;&lt;em&gt;Verifying that the outputs are also safe&lt;/em&gt;. This includes the obvious: return values, output and reference parameters. However, non-obvious results of operations are also included here. For example, exceptions thrown or even state transitions of objects need to be safe as well. If outside code can observe a change based upon using a safe critical method, then that safe critical method is responsible for ensuring that exposing this change is something safe to do.&lt;/li&gt;&lt;/ol&gt; &lt;p&gt;With this in mind, another interesting observation can be made. Since the security safe critical layer of code is the bridge between transparent code and security critical code, it really forms the attack surface of a library. &lt;p&gt;This means that upon adopting the security transparency model, an APTCA library's audit burden really falls mostly upon the security safe critical surface of the library. If any of the safe critical code in the assembly is not correctly verifying operations, inputs, or outputs, then that safe critical method is a security hole that needs be closed. &lt;p&gt;Conversely, since transparent (and therefore, in .NET 4, partially trusted) code cannot directly call through to security critical code the audit burden on this code is significantly reduced. Similarly, since transparent code cannot be doing any security sensitive operations it also has a significantly reduced security audit burden. &lt;p&gt;By using security transparency, therefore, it becomes very easy to identify the sections of code that need to be focused on in security review in order to make sure that the shipping assembly is secure. &lt;p&gt;(Our internal security reviewers sometimes jokingly refer to the SecuritySafeCriticalAttribute as the BigRedFlagAttribute)&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9918103" width="1" height="1"&gt;</content><author><name>shawnfa</name><uri>http://blogs.msdn.com/members/shawnfa.aspx</uri></author><category term="Security" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Security/default.aspx" /><category term="CLR v4" scheme="http://blogs.msdn.com/shawnfa/archive/tags/CLR+v4/default.aspx" /><category term="Transparency" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Transparency/default.aspx" /></entry><entry><title>Transparency 101: Basic Transparency Rules</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/shawnfa/archive/2009/11/03/transparency-101-basic-transparency-rules.aspx" /><id>http://blogs.msdn.com/shawnfa/archive/2009/11/03/transparency-101-basic-transparency-rules.aspx</id><published>2009-11-03T17:38:27Z</published><updated>2009-11-03T17:38:27Z</updated><content type="html">&lt;p&gt;One of the biggest changes in the .NET 4 security model is a move toward security transparency as a primary security enforcement mechanism of the platform. As you'll recall, we &lt;a href="http://blogs.msdn.com/shawnfa/archive/2005/08/31/458641.aspx"&gt;introduced security transparency in the v2 release of .NET&lt;/a&gt; as more of an audit mechanism in order to help make the surface area of APTCA libraries as safe as possible. In Silverlight, we evolved transparency into &lt;a href="http://blogs.msdn.com/shawnfa/archive/2007/05/14/silverlight-security-cheat-sheet.aspx"&gt;the security model&lt;/a&gt; that the entire managed platform was built on top of.&amp;nbsp; With .NET 4 we continue that evolution, making security transparency now the consistent way to enforce security both on Silverlight and on the desktop CLR.  &lt;p&gt;Before we dive deep into what all this means, let's take a quick refresher over the basic concepts of transparency.  &lt;p&gt;The fundamental idea of security transparency is to separate code which may potentially do dangerous or security sensitive things from code which is benign from a security perspective. The security sensitive code is called security critical, and the code which does not perform security sensitive operations is called security transparent.  &lt;p&gt;With that in mind, let's figure out what operations are security sensitive, and therefore require the code performing them to be security critical.  &lt;p&gt;Imagine for a minute that the CLR shipped exactly as-is, but without the ability to do two important operations:  &lt;ul&gt; &lt;li&gt;Call native code, either via COM Interop or P/Invoke.  &lt;li&gt;Execute unverifiable code&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Without either of these operations, all the possible code that could run on the CLR would be entirely safe - there's no possible thing that it could do that could be dangerous. On the flip side, there's also not very much interesting it could do (taking into account that the BCL is managed code, and would have to abide by these rules as well).  &lt;p&gt;For example, you could write a calculator application or an XML parser library with the operations available to you in verifiable IL, however the utility of that code would be severely limited by the fact that you could not receive any input from the user of your application (which would require either your app itself or the BCL interop with native code in order to read from a file or standard input); similarly you couldn't display the results of your calculations without talking to native code either.  &lt;p&gt;Obviously the CLR wouldn't be a very interesting platform for writing code on if these restrictions were in place, so we need to make them available. However, since they both allow taking full control of the process, we need to restrict them to trusted code only. Therefore, calling native code and having unverifiable code are our first set of operations that are security critical.  &lt;p&gt;(Note that &lt;i&gt;containing&lt;/i&gt; unverifiable code and &lt;i&gt;calling&lt;/i&gt; native code are the operations here - there's no inherent problem with calling an unverifiable method and the fact that a method contains unverifiable code does not in and of itself mean that it is dangerous to use).  &lt;p&gt;We've now determined that code needs to be security critical in order to work with native code or unverifiable code - easy enough; this gives us our first set of security critical methods. However, since these methods are performing security sensitive operations using them may also be a security sensitive operation. That leads us to our third transparency rule - you must be critical if you:  &lt;ul&gt; &lt;li value="3"&gt;Call critical code&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Some code, such as the File classes, are security sensitive but mitigate their security risk by demanding permission to use them. In the case of the File classes, if the sandbox they are running in is granted the appropriate FileIOPermission then they are safe to use; otherwise they are not.  &lt;p&gt;If trusted code wants to use the File classes in a sandbox that does not support them, it can assert away the file IO demands. For instance, IsolatedStorage does exactly this to allow access to a safe isolated storage file store in sandboxes that do not allow unrestricted access to the user's hard drive.  &lt;p&gt;By doing this, however, the trusted code has removed the mitigation that the original security critical code put in place - the permission demand - and asserted that the demand is not necessary anymore for some reason. (In the case of isolated storage because the file paths are well controlled, a quota is being enforced, and an IsolatedStoragePermission demand will be issued).  &lt;p&gt;Since permission asserts remove security checks, performing an assert is security sensitive.&amp;nbsp; This means we've now got the fourth operation which requires code to be security critical:  &lt;ul&gt; &lt;li&gt;Perform a security assert &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Some code which performs a security sensitive operation will protect itself with a LinkDemand, which rather than requiring that it only run in a specific sandbox instead says that the operation is viable in any sandbox - as long as the code executing the operation is trusted. For example, the Marshal class falls into this category.  &lt;p&gt;Marshaling data back and forth between native and managed code makes sense in every sandbox - it's a generally useful operation. However, you certainly don't want the sandboxed code using methods like ReadByte and WriteByte to start manipulating memory. Therefore, the Marshal class protects itself with a LinkDemand for a full trust equivalent permission.  &lt;p&gt;Since this LinkDemand is Marshal's way of calling out that any use of these methods are security sensitive, our fifth transparency rule is easily derived. Code must be security critical if it attempts to:  &lt;ul&gt; &lt;li&gt;Satisfy a link demand &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Security transparency and inheritance have an interesting interaction, which is sometimes rather subtle. However, understanding it will lead us to a few more operations that require code be security critical.  &lt;p&gt;Let's start with security critical types - when a type, such as SafeHandle, declares itself to be security critical it's saying that any use of that type is potentially security sensitive. This includes not only direct uses, such as creating instances and calling methods on the type, but also more subtle uses - such as deriving from the type. Therefore, a type must be security critical if it wants to:  &lt;ul&gt; &lt;li&gt;Derive from a non-transparent type or implement a non-transparent interface. &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;If a base type has security critical virtual methods, it's interesting to think about what requirements we might want to place on overrides of those virtuals. At first glance there doesn't appear to be any security requirements for overriding these methods - after all, once you've overridden a method none of its code is going to execute, so the fact that it is security critical doesn't matter.  &lt;p&gt;However, from the perspective of the caller of the security critical virtual method, it is actually rather important that any override of a critical virtual remain security critical.  &lt;p&gt;To see why, let's take an example. X509Certificate provides an Import method which is security critical in the v4 release of the CLR. This method takes both the raw bytes of the certificate and the password necessary to gain access to the private key of that certificate.  &lt;p&gt;Since the code on the other end of the virtual function call is going to be receiving sensitive information, such as a password and a certificate that may have a private key, it is by definition security sensitive.&amp;nbsp; The code which calls the Import virtual is passing this sensitive information through the call under the assumption that the method which will ultimately execute is itself trustworthy.&amp;nbsp; Therefore, it methods are security critical if they:  &lt;ul&gt; &lt;li&gt;Override a security critical virtual or implement a security critical interface method &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;This is the final core transparency rule - the core set of things that are security sensitive and therefore require the code doing them to be security critical.  &lt;p&gt;It's interesting to note that this list of critical operations:  &lt;ol&gt; &lt;li&gt;Call native code &lt;/li&gt; &lt;li&gt;Contain unverifiable code &lt;/li&gt; &lt;li&gt;Call critical code &lt;/li&gt; &lt;li&gt;Perform security asserts &lt;/li&gt; &lt;li&gt;Satisfy link demands &lt;/li&gt; &lt;li&gt;Derive from non-transparent types &lt;/li&gt; &lt;li&gt;Override security critical virtuals &lt;/li&gt;&lt;/ol&gt; &lt;p&gt;Could also read as a list of operations that partial trust code cannot perform. In fact, in the v4 CLR we now force all partial trust code to be entirely transparent. Or, put another way, only full trust code can be security critical. This is very similar to the way that Silverlight requires that all user assemblies are entirely transparent, and only Silverlight platform assemblies can contain security critical code. This is one of the basic steps that allowed us to use security transparency as a security enforcement mechanism in Silverlight and the v4 desktop framework.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9916865" width="1" height="1"&gt;</content><author><name>shawnfa</name><uri>http://blogs.msdn.com/members/shawnfa.aspx</uri></author><category term="Security" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Security/default.aspx" /><category term="CLR v4" scheme="http://blogs.msdn.com/shawnfa/archive/tags/CLR+v4/default.aspx" /><category term="Transparency" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Transparency/default.aspx" /></entry><entry><title>CLR v4 Security Policy Roundup</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/shawnfa/archive/2009/06/12/clr-v4-security-policy-roundup.aspx" /><id>http://blogs.msdn.com/shawnfa/archive/2009/06/12/clr-v4-security-policy-roundup.aspx</id><published>2009-06-12T21:33:00Z</published><updated>2009-06-12T21:33:00Z</updated><content type="html">&lt;P&gt;Over the last few weeks we’ve been taking a look at the updates to the CLR security policy system in the v4 release of the .NET Framework.&amp;nbsp; Here’s a quick index of those topics:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Overview&lt;/LI&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://blogs.msdn.com/shawnfa/archive/2009/05/21/security-policy-in-the-v4-clr.aspx" mce_href="http://blogs.msdn.com/shawnfa/archive/2009/05/21/security-policy-in-the-v4-clr.aspx"&gt;Security Policy in the v4 CLR&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://blogs.msdn.com/shawnfa/archive/2009/05/22/sandboxing-in-net-4-0.aspx" mce_href="http://blogs.msdn.com/shawnfa/archive/2009/05/22/sandboxing-in-net-4-0.aspx"&gt;Sandboxing in .NET 4.0&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;LI&gt;Updating code to work with the new model&lt;/LI&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://blogs.msdn.com/shawnfa/archive/2009/05/27/coding-with-security-policy-in-net-4-0-implicit-uses-of-cas-policy.aspx" mce_href="http://blogs.msdn.com/shawnfa/archive/2009/05/27/coding-with-security-policy-in-net-4-0-implicit-uses-of-cas-policy.aspx"&gt;Implicit uses of CAS policy part 1 (Assembly.Load with Evidence, AppDomain creation with Evidence)&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://blogs.msdn.com/shawnfa/archive/2009/06/08/more-implicit-uses-of-cas-policy-loadfromremotesources.aspx" mce_href="http://blogs.msdn.com/shawnfa/archive/2009/06/08/more-implicit-uses-of-cas-policy-loadfromremotesources.aspx"&gt;Implicit uses of CAS policy part 2 (loading assemblies from remote sources)&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://blogs.msdn.com/shawnfa/archive/2009/06/09/coding-with-security-policy-in-net-4-part-2-explicit-uses-of-cas-policy.aspx" mce_href="http://blogs.msdn.com/shawnfa/archive/2009/06/09/coding-with-security-policy-in-net-4-part-2-explicit-uses-of-cas-policy.aspx"&gt;Explicit uses of CAS policy (APIs such as SecurityManager which directly use CAS policy)&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;LI&gt;Temporarily re-enabling CAS policy during migration&lt;/LI&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://blogs.msdn.com/shawnfa/archive/2009/06/12/temporarily-re-enabling-cas-policy-during-migration.aspx" mce_href="http://blogs.msdn.com/shawnfa/archive/2009/06/12/temporarily-re-enabling-cas-policy-during-migration.aspx"&gt;The NetFx40_LegacySecurityPolicy config file switch&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;/UL&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9737125" width="1" height="1"&gt;</content><author><name>shawnfa</name><uri>http://blogs.msdn.com/members/shawnfa.aspx</uri></author><category term="Security" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Security/default.aspx" /><category term="ClickOnce" scheme="http://blogs.msdn.com/shawnfa/archive/tags/ClickOnce/default.aspx" /><category term="CAS" scheme="http://blogs.msdn.com/shawnfa/archive/tags/CAS/default.aspx" /><category term="Policy" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Policy/default.aspx" /></entry><entry><title>Temporarily re-enabling CAS policy during migration</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/shawnfa/archive/2009/06/12/temporarily-re-enabling-cas-policy-during-migration.aspx" /><id>http://blogs.msdn.com/shawnfa/archive/2009/06/12/temporarily-re-enabling-cas-policy-during-migration.aspx</id><published>2009-06-12T21:27:13Z</published><updated>2009-06-12T21:27:13Z</updated><content type="html">&lt;p&gt;Over the last few weeks we’ve been looking at the &lt;a href="http://blogs.msdn.com/shawnfa/archive/2009/05/21/security-policy-in-the-v4-clr.aspx"&gt;changes to security policy in .NET 4,&lt;/a&gt; namely that security policy is now in the hands of the host and the operating system.&lt;/p&gt;  &lt;p&gt;While we’ve looked at how to update code that &lt;a href="http://blogs.msdn.com/shawnfa/archive/2009/05/27/coding-with-security-policy-in-net-4-0-implicit-uses-of-cas-policy.aspx"&gt;implicitly uses CAS policy&lt;/a&gt;, &lt;a href="http://blogs.msdn.com/shawnfa/archive/2009/06/08/more-implicit-uses-of-cas-policy-loadfromremotesources.aspx"&gt;loads assemblies from remote sources&lt;/a&gt;, and &lt;a href="http://blogs.msdn.com/shawnfa/archive/2009/06/09/coding-with-security-policy-in-net-4-part-2-explicit-uses-of-cas-policy.aspx"&gt;explicitly uses CAS policy,&lt;/a&gt; in applications of larger size it may not be practical to update all the code at once.&amp;#160; Similarly, you might be able to update the code in your application, but may rely on a third party assembly that is not yet updated for the changes in CAS policy.&lt;/p&gt;  &lt;p&gt;If you do find yourself needing to re-enable CAS policy temporarily, in order to move a large code base to the new v4 security APIs bit by bit rather than all at once, or to use an assembly that you don’t control, there is a configuration switch that you can set in order to flip your process back into legacy CAS policy mode.&lt;/p&gt;  &lt;p&gt;In order to temporarily enable legacy CAS policy in your process, you’ll need an .exe.config file for your application with the legacy security policy switch set in its runtime section.&amp;#160; So, if your application’s entry point is YourApp.exe, you’ll have next to it a YourApp.exe.config file.&amp;#160; (You can also use the app.config feature in your Visual Studio project).&amp;#160; The file should look like this for any release of the .NET Framework v4 after beta 1:&lt;/p&gt;  &lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;   &lt;pre style="margin: 0px"&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;configuration&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;runtime&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;NetFx40_LegacySecurityPolicy&lt;/span&gt;&lt;span style="color: blue"&gt; &lt;/span&gt;&lt;span style="color: red"&gt;enabled&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;true&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt; /&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;#160; &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;runtime&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;configuration&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;In .NET 4 Beta 1, the switch has a slightly different name:&lt;/p&gt;

&lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;
  &lt;pre style="margin: 0px"&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;configuration&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;runtime&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;legacyCasPolicy&lt;/span&gt;&lt;span style="color: blue"&gt; &lt;/span&gt;&lt;span style="color: red"&gt;enabled&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;true&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt; /&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;#160; &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;runtime&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;configuration&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;One thing to note is that this switch must be set on the process-level.&amp;#160; So, if you’re using a third party control that uses CAS policy, you may well need to set the switch for both Visual Studio in devenv.exe.config and for your application itself.&amp;#160; That way the control will work both in the Visual Studio process during your development, as well as in your process at runtime.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9737038" width="1" height="1"&gt;</content><author><name>shawnfa</name><uri>http://blogs.msdn.com/members/shawnfa.aspx</uri></author><category term="Security" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Security/default.aspx" /><category term="CAS" scheme="http://blogs.msdn.com/shawnfa/archive/tags/CAS/default.aspx" /><category term="Policy" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Policy/default.aspx" /><category term="CLR v4" scheme="http://blogs.msdn.com/shawnfa/archive/tags/CLR+v4/default.aspx" /></entry><entry><title>Coding with Security Policy in .NET 4 part 2 – Explicit uses of CAS policy</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/shawnfa/archive/2009/06/09/coding-with-security-policy-in-net-4-part-2-explicit-uses-of-cas-policy.aspx" /><id>http://blogs.msdn.com/shawnfa/archive/2009/06/09/coding-with-security-policy-in-net-4-part-2-explicit-uses-of-cas-policy.aspx</id><published>2009-06-09T22:14:31Z</published><updated>2009-06-09T22:14:31Z</updated><content type="html">&lt;p&gt;Over the last few posts, I’ve been looking at how the update to the CLR v4 &lt;a href="http://blogs.msdn.com/shawnfa/archive/2009/05/21/security-policy-in-the-v4-clr.aspx"&gt;security policy&lt;/a&gt; interacts with how you write managed code against the v4 .NET Framework.&amp;#160; So far we’ve looked at the implicit uses of CAS policy, such as &lt;a href="http://blogs.msdn.com/shawnfa/archive/2009/05/21/security-policy-in-the-v4-clr.aspx"&gt;loading assemblies and creating AppDomains with Evidence&lt;/a&gt; and &lt;a href="http://blogs.msdn.com/shawnfa/archive/2009/06/08/more-implicit-uses-of-cas-policy-loadfromremotesources.aspx"&gt;loading assemblies from remote sources&lt;/a&gt;.&amp;#160; Now let’s look at how to work with code which was written to work with CAS policy explicitly.&lt;/p&gt;  &lt;p&gt;The good news is that explicit use of CAS policy is frequently very easy to spot, as opposed to implicit uses which can be somewhat more subtle.&amp;#160; APIs that directly manipulate policy (such as SecurityManager.ResolvePolicy) as well as those that require CAS policy to sandbox (such as AppDomain.SetAppDomainPolicy) fall into this category.&amp;#160; Other APIs that explicitly use CAS policy are:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;AppDomain.SetAppDomainPolicy &lt;/li&gt;    &lt;li&gt;HostSecurityManager.DomainPolicy &lt;/li&gt;    &lt;li&gt;PolicyLevel.CreateAppDomainLevel &lt;/li&gt;    &lt;li&gt;SecurityManager.LoadPolicyLevelFromString &lt;/li&gt;    &lt;li&gt;SecurityManager.LoadPolicyLevelFromFile &lt;/li&gt;    &lt;li&gt;SecurityManager.ResolvePolicy &lt;/li&gt;    &lt;li&gt;SecurityManager.ResolveSystemPolicy &lt;/li&gt;    &lt;li&gt;SecurityManager.ResolvePolicyGroups &lt;/li&gt;    &lt;li&gt;SecurityManager.PolicyHierarchy &lt;/li&gt;    &lt;li&gt;SecurityManager.SavePolicy &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;As with the implicit CAS policy uses, the explicit APIs also are obsolete in .NET 4, and will throw NotSupportedExceptions by default:&lt;/p&gt;  &lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;System.NotSupportedException: This method uses CAS policy, which has been obsoleted by the .NET Framework. In order to enable CAS policy for compatibility reasons, please use the NetFx40_LegacySecurityPolicy configuration switch. Please see &lt;a href="http://go.microsoft.com/fwlink/?LinkId=131738"&gt;http://go.microsoft.com/fwlink/?LinkId=131738&lt;/a&gt; for more information.&lt;/div&gt;  &lt;p&gt;Let’s take a look at how code which used these APIs in the past might get updated with new v4 APIs.&lt;/p&gt;  &lt;p&gt;Generally, there are three reasons that the explicit policy APIs are being used:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;The code wants to figure out what the grant set of an assembly or AppDomain is &lt;/li&gt;    &lt;li&gt;The code wants to create a sandbox &lt;/li&gt;    &lt;li&gt;The code wants to figure out what a safe sandbox is to setup &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;The correct way way to update the code calling an explicit policy API in v4 depends upon what it was trying to do by calling the API in the first place.&amp;#160; Let’s take a look at each of the reasons for using an explicit policy API in turn and figure out what the replacement code should look like.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Figuring out what the grant set of an assembly or AppDomain is&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Sometimes an application or library wants to figure out what the grant set of a particular assembly or domain was and would do so with code similar to:&lt;/p&gt;  &lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;   &lt;pre style="margin: 0px"&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: #2b91af"&gt;PermissionSet&lt;/span&gt; GetAssemblyGrantSet(&lt;span style="color: #2b91af"&gt;Assembly&lt;/span&gt; assembly)&lt;/p&gt;&lt;p style="margin: 0px"&gt;{&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Evidence&lt;/span&gt; assemblyEvidence = assembly.Evidence;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: #2b91af"&gt;SecurityManager&lt;/span&gt;.ResolvePolicy(assemblyEvidence);&lt;/p&gt;&lt;p style="margin: 0px"&gt;}&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;bool&lt;/span&gt; IsFullyTrusted(&lt;span style="color: #2b91af"&gt;Assembly&lt;/span&gt; assembly)&lt;/p&gt;&lt;p style="margin: 0px"&gt;{&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;PermissionSet&lt;/span&gt; grant = GetAssemblyGrantSet(assembly);&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; grant.IsUnrestricted();&lt;/p&gt;&lt;p style="margin: 0px"&gt;}&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: #2b91af"&gt;PermissionSet&lt;/span&gt; GetAppDomainGrantSet(&lt;span style="color: #2b91af"&gt;AppDomain&lt;/span&gt; domain)&lt;/p&gt;&lt;p style="margin: 0px"&gt;{&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Evidence&lt;/span&gt; appDomain = domain.Evidence;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: #2b91af"&gt;SecurityManager&lt;/span&gt;.ResolvePolicy(appDomain);&lt;/p&gt;&lt;p style="margin: 0px"&gt;}&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;bool&lt;/span&gt; IsFullyTrusted(&lt;span style="color: #2b91af"&gt;AppDomain&lt;/span&gt; domain)&lt;/p&gt;&lt;p style="margin: 0px"&gt;{&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;PermissionSet&lt;/span&gt; grant = GetAppDomainGrantSet(domain);&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; grant.IsUnrestricted();&lt;/p&gt;&lt;p style="margin: 0px"&gt;}&lt;/p&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This code worked by resolving the assembly or AppDomain’s evidence through CAS policy to determine what would be granted to that particular evidence.&amp;#160; There are a few problems here – for instance, the code doesn’t take into account simple sandbox domains, hosted AppDomains, dynamic assemblies, or assemblies loaded from byte arrays.&amp;#160; (Take a look at AssemblyExtensionMethods.GetPermissionSet() on &lt;a href="http://clrsecurity.codeplex.com"&gt;http://clrsecurity.codeplex.com&lt;/a&gt; for code that does take most of the other considerations into account).&amp;#160;&amp;#160; These methods also cause a full CAS policy resolution to occur, which is not a cheap operation.&amp;#160; &lt;/p&gt;

&lt;p&gt;Instead of requiring people to manually jump through hoops in order to recreate the CLR’s security policy system in v4, we’ve directly exposed the grant sets of assemblies and AppDomains as properties of the objects themselves.&amp;#160; The above code can be replaced with:&lt;/p&gt;

&lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;
  &lt;pre style="margin: 0px"&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: #2b91af"&gt;PermissionSet&lt;/span&gt; GetAssemblyGrantSet(&lt;span style="color: #2b91af"&gt;Assembly&lt;/span&gt; assembly)&lt;/p&gt;&lt;p style="margin: 0px"&gt;{&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; assembly.PermissionSet;&lt;/p&gt;&lt;p style="margin: 0px"&gt;}&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;bool&lt;/span&gt; IsFullyTrusted(&lt;span style="color: #2b91af"&gt;Assembly&lt;/span&gt; assembly)&lt;/p&gt;&lt;p style="margin: 0px"&gt;{&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; assembly.IsFullyTrusted;&lt;/p&gt;&lt;p style="margin: 0px"&gt;}&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: #2b91af"&gt;PermissionSet&lt;/span&gt; GetAppDomainGrantSet(&lt;span style="color: #2b91af"&gt;AppDomain&lt;/span&gt; domain)&lt;/p&gt;&lt;p style="margin: 0px"&gt;{&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; domain.PermissionSet;&lt;/p&gt;&lt;p style="margin: 0px"&gt;}&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;bool&lt;/span&gt; IsFullyTrusted(&lt;span style="color: #2b91af"&gt;AppDomain&lt;/span&gt; domain)&lt;/p&gt;&lt;p style="margin: 0px"&gt;{&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; domain.IsFullyTrusted;&lt;/p&gt;&lt;p style="margin: 0px"&gt;}&lt;/p&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Which has the dual benefit of being more accurate (these properties read the real grant set that the CLR is using, no matter how it was determined), and also being faster than a full policy resolution.&lt;/p&gt;

&lt;p&gt;Accessing the PermissionSet property of an AppDomain or an Assembly does require that the accessing code be fully trusted.&amp;#160; The reason is that the permission sets themselves can contain sensitive data.&amp;#160; (For instance, FileIOPermission can contain full path information about the local machine in it).&amp;#160;&amp;#160; Partial trust code, however, can use the IsFullyTrusted property.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Creating a Sandbox&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I suspect many people who have read this blog already know what I’m going to say here :-)&amp;#160; Instead of using SetAppDomainPolicy to create a sandbox, which suffers from many problems, the replacement API is the &lt;a href="http://blogs.msdn.com/shawnfa/archive/2006/04/19/579066.aspx"&gt;simple sandboxing API&lt;/a&gt;.&amp;#160; I’ve already covered most of the reasoning for this change when &lt;a href="http://blogs.msdn.com/shawnfa/archive/2009/05/22/sandboxing-in-net-4-0.aspx"&gt;I talked about sandboxing in CLR v4&lt;/a&gt;, so let’s look at the final reason that code may have been using CAS policy APIs&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Figuring out what a safe grant set is to provide a sandbox&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Sometimes a host needs to figure out what is a reasonable set of permissions to assign to a sandbox.&amp;#160; For instance, even though ClickOnce does not use CAS policy, it still needs to figure out if the permission set that the ClickOnce application is requesting is a reasonable set of permissions for it to have.&amp;#160;&amp;#160; (For instance, if it’s requesting only the permission to execute, that’s going to be fine, while if an application from the Internet is requesting permission to read and write all of the files on your disk, that’s not such a good idea).&lt;/p&gt;

&lt;p&gt;In order to solve this problem in v2, code might look like this:&lt;/p&gt;

&lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;
  &lt;pre style="margin: 0px"&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;bool&lt;/span&gt; IsSafeGrantSet(&lt;span style="color: #2b91af"&gt;PermissionSet&lt;/span&gt; grantSet, &lt;span style="color: #2b91af"&gt;Evidence&lt;/span&gt; sandboxEvidence)&lt;/p&gt;&lt;p style="margin: 0px"&gt;{&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: green"&gt;// Figure out what the CLR's policy system says is safe to give a sandbox&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: green"&gt;// with this evidence&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;PermissionSet&lt;/span&gt; systemGrantSet = &lt;span style="color: #2b91af"&gt;SecurityManager&lt;/span&gt;.ResolveSystemPolicy(sandboxEvidence);&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: green"&gt;// We'll consider this safe only if we're requesting a subset of the safe&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: green"&gt;// sandbox set.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; grantSet.IsSubsetOf(systemGrantSet);&lt;/p&gt;&lt;p style="margin: 0px"&gt;}&lt;/p&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Since system wide CAS policy (which this code depends upon to determine safety) is deprecated in v4, we need to find a new way to accomplish this goal.&lt;/p&gt;

&lt;p&gt;The answer is with a new API called GetStandardSandbox.&amp;#160;&amp;#160; GetStandardSandbox is used to have the CLR provide what it considers a safe sandbox grant set for an AppDomain that will host code with the specified evidence.&amp;#160; It’s the CLR’s way of providing suggestions to hosts who are making trust decisions.&amp;#160;&amp;#160; One thing that is very important to note is what GetStandardSandbox is not however.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;u&gt;GetStandardSandbox is not a policy API&lt;/u&gt;&lt;/em&gt;.&amp;#160; This isn’t the CLR applying CAS to evidence in order to modify grant set, and the CLR does not take any external factors such as CAS policy into account when returning its grant set.&amp;#160; Instead, GetStandardSandbox is simply a helper API for hosts which are trying to setup sandboxes.&lt;/p&gt;

&lt;p&gt;With that in mind, the way the above code would be written in CLR v4 is:&lt;/p&gt;

&lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;
  &lt;pre style="margin: 0px"&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;bool&lt;/span&gt; IsSafeGrantSet(&lt;span style="color: #2b91af"&gt;PermissionSet&lt;/span&gt; grantSet, &lt;span style="color: #2b91af"&gt;Evidence&lt;/span&gt; sandboxEvidence)&lt;/p&gt;&lt;p style="margin: 0px"&gt;{&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: green"&gt;// Figure out what the CLR considers a safe grant set&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;PermissionSet&lt;/span&gt; clrSandbox = &lt;span style="color: #2b91af"&gt;SecurityManager&lt;/span&gt;.GetStandardSandbox(sandboxEvidence);&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: green"&gt;// We'll consider this safe only if we're requesting a subset of the safe&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: green"&gt;// sandbox set.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; grantSet.IsSubsetOf(clrSandbox);&lt;/p&gt;&lt;p style="margin: 0px"&gt;}&lt;/p&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Similarly, if you are a host trying to setup an AppDomain to sandbox assemblies that are coming from the Internet, you might do so this way:&lt;/p&gt;

&lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;
  &lt;pre style="margin: 0px"&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// Find a safe sandbox set to give to assemblies downloaded&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// from the internet&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;Evidence&lt;/span&gt; internetEvidence = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Evidence&lt;/span&gt;();&lt;/p&gt;&lt;p style="margin: 0px"&gt;internetEvidence.AddHostEvidence(&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Zone&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;SecurityZone&lt;/span&gt;.Internet));&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;PermissionSet&lt;/span&gt; clrSandbox = &lt;span style="color: #2b91af"&gt;SecurityManager&lt;/span&gt;.GetStandardSandbox(internetEvidence);&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// Create a sandboxed AppDomain to hold them&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;AppDomainSetup&lt;/span&gt; sandboxSetup = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;AppDomainSetup&lt;/span&gt;();&lt;/p&gt;&lt;p style="margin: 0px"&gt;sandboxSetup.ApplicationBase = DownloadDirectory;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;AppDomain&lt;/span&gt; sandbox = &lt;span style="color: #2b91af"&gt;AppDomain&lt;/span&gt;.CreateDomain(&lt;span style="color: #a31515"&gt;&amp;quot;Internet sandbox&amp;quot;&lt;/span&gt;,&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; internetEvidence,&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; sandboxSetup,&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; clrSandbox);&lt;/p&gt;&lt;/pre&gt;
&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9717560" width="1" height="1"&gt;</content><author><name>shawnfa</name><uri>http://blogs.msdn.com/members/shawnfa.aspx</uri></author><category term="Security" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Security/default.aspx" /><category term="CAS" scheme="http://blogs.msdn.com/shawnfa/archive/tags/CAS/default.aspx" /><category term="Policy" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Policy/default.aspx" /><category term="CLR v4" scheme="http://blogs.msdn.com/shawnfa/archive/tags/CLR+v4/default.aspx" /></entry><entry><title>More Implicit Uses of CAS Policy: loadFromRemoteSources</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/shawnfa/archive/2009/06/08/more-implicit-uses-of-cas-policy-loadfromremotesources.aspx" /><id>http://blogs.msdn.com/shawnfa/archive/2009/06/08/more-implicit-uses-of-cas-policy-loadfromremotesources.aspx</id><published>2009-06-08T21:59:07Z</published><updated>2009-06-08T21:59:07Z</updated><content type="html">&lt;p&gt;In my last post about changes to the CLR v4 security policy model, &lt;a href="http://blogs.msdn.com/shawnfa/archive/2009/05/27/coding-with-security-policy-in-net-4-0-implicit-uses-of-cas-policy.aspx"&gt;I looked at APIs which implicitly use CAS policy in their operation&lt;/a&gt; (such as Assembly.Load overloads that take an Evidence parameter), and how to migrate code that was using those APIs.&amp;#160;&amp;#160; There are another set of assembly loads which cause implicit use of CAS policy, which I’ll look at today – these are loads from remote sources.&lt;/p&gt;  &lt;p&gt;For example, in .NET 3.5 the following code:&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;   &lt;pre style="margin: 0px"&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;Assembly&lt;/span&gt; internetAssembly = &lt;span style="color: #2b91af"&gt;Assembly&lt;/span&gt;.LoadFrom(&lt;span style="color: #a31515"&gt;@&amp;quot;http://www.microsoft.com/assembly.dll&amp;quot;&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;Assembly&lt;/span&gt; intranetAssembly = &lt;span style="color: #2b91af"&gt;Assembly&lt;/span&gt;.LoadFrom(&lt;span style="color: #a31515"&gt;@&amp;quot;\\server\share\assembly.dll&amp;quot;&lt;/span&gt;);&lt;/p&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;Will by default load internetAssembly with the Internet permission set and intranetAssembly with the LocalIntranet permission set.&amp;#160;&amp;#160; That was because the CLR would internally gather evidence for both assemblies and run that evidence though CAS policy in order to find the permission set to grant that assembly. &lt;/p&gt;

&lt;p&gt;Now that &lt;a href="http://blogs.msdn.com/shawnfa/archive/2009/05/22/sandboxing-in-net-4-0.aspx"&gt;the sandboxing model has changed in the v4 CLR&lt;/a&gt;, there is no more CAS policy to apply the assembly’s evidence to by default, and&amp;#160; therefore default behavior of both of these loads would be to load the assemblies with a grant set of full trust.&lt;/p&gt;

&lt;p&gt;That creates a problem for code which was written before .NET 4 shipped – this code may quite reasonably be expecting that the above assembly loads are safe because the CLR will automatically apply a restricted grant set to the assemblies if they are coming from a remote location.&amp;#160;&amp;#160; Now when the code runs in the v4 CLR, the assemblies are elevated to full trust, which amounts to a silent elevation of privilege bug against the .NET 2.0 code which was expecting that these assemblies be sandboxed.&amp;#160; Obviously that’s not a good thing.&lt;/p&gt;

&lt;p&gt;Instead of silently granting these assemblies full trust, the v4 CLR will actually take the opposite approach.&amp;#160; We’ll detect that these assemblies are being loaded in such a way that&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;They would have been sandboxed by the v2 CLR &lt;em&gt;and&lt;/em&gt; &lt;/li&gt;

  &lt;li&gt;Are going to be given full trust by the v4 CLR &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Once we detect an assembly load where both of the above conditions are true, the CLR will refuse to load the assembly with the following message:&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;
  &lt;p&gt;System.IO.FileLoadException: Could not load file or assembly '&lt;em&gt;&amp;lt;assemblyPath&amp;gt;&lt;/em&gt;' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515 (COR_E_NOTSUPPORTED)) ---&amp;gt;&lt;/p&gt;

  &lt;p&gt;System.NotSupportedException: An attempt was made to load an assembly from a network location which would have caused the assembly to be sandboxed in previous versions of the .NET Framework. This release of the .NET Framework does not enable CAS policy by default, so this load may be dangerous. If this load is not intended to sandbox the assembly, please enable the loadFromRemoteSources switch. See http://go.microsoft.com/fwlink/?LinkId=131738 for more information.&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;This exception is saying “The v4 CLR is not going to sandbox the assembly that you’re trying to load, however the v2 CLR would have.&amp;#160; We don’t know if that’s safe in your application or not, so we’re going to fail the assembly load to ensure that your application is secure by default.&amp;#160; However, if this is a safe assembly load, go ahead and enable loading from remote sources for this process.”&lt;/p&gt;

&lt;p&gt;That leads to the next question -- how do you know if it is safe to enable loadFromRemoteSources in your application?&amp;#160; This decision generally comes down to applying these tests:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Do you trust the string that you’re passing to Assembly.LoadFrom? &lt;/li&gt;

  &lt;li&gt;Do you trust the assembly that you’re loading? &lt;/li&gt;

  &lt;li&gt;Do you trust the server hosting the assembly (and the network path from the server back to your application)? &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you answered yes to all three questions then your application is a good candidate for enabling the loadFromRemoteSources switch.&amp;#160; If you answered no to any of the three questions, then you may need to take further action before enabling the switch and loading the assembly.&amp;#160;&amp;#160; (For instance, you may have some application logic to ensure that the string being passed to LoadFrom is going to a server you trust, or your application might download the assembly first and verify it has an Authenticode signature that it trusts).&lt;/p&gt;

&lt;p&gt;Let’s look at some examples:&lt;/p&gt;

&lt;p&gt;The most straight-forward reason that you would want to enable this is in the case that you know what the assemblies you are loading are, you trust them, and you trust the server that they are hosted on.&amp;#160; For example, if your application is hosted on a share on your company’s intranet, and happens to need to load other assemblies from other shares on the network, you probably want to enable the switch.&amp;#160;&amp;#160; (In many cases, this category of applications used to have to fight with CAS policy to get things loaded the way they wanted, now with loadFromRemoteSources set things should just work.)&lt;/p&gt;

&lt;p&gt;On the other hand, if you are an application that takes as untrusted input a string which then is passed through to Assembly.LoadFrom, you probably don’t want to enable this switch, as you might be opening yourself up to an elevation of privilege attack via that untrusted input.&lt;/p&gt;

&lt;p&gt;Similarly, if your application takes as input an assembly name to LoadFrom, however you trust that input.&amp;#160; (Maybe it comes directly from your application’s user, and there is no trust boundary between the user and your app – for instance, the user is pointing you at a plugin they trust and wish to load in the app), you may also want to enable this switch.&lt;/p&gt;

&lt;p&gt;Another consideration to take into account when considering loadFromRemoteSources is that this is a process-wide configuration switch.&amp;#160; This means that it applies to all places in your code which loads assemblies, not just a single LoadFrom call.&amp;#160; If you only trust the inputs to some of your assembly loads, then you may wish to consider not using the loadFromRemoteSources switch and instead take a different approach.&lt;/p&gt;

&lt;p&gt;Since the first condition for the NotSupportedException that blocks remote assembly loads is that the load would have been sandboxed by the v2 CLR, one alternate way to enable these loads without setting loadFromRemoteSources for the entire process is to load the assemblies into a domain that you create &lt;a href="http://blogs.msdn.com/shawnfa/archive/2005/08/08/449050.aspx"&gt;with the simple sandboxing API&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This will work because even in v2.0, simple sandbox domains never apply CAS policy, and therefore any remote loads in simple sandbox domains would not have required CAS policy to sandbox them.&amp;#160; Since the assemblies would not have used CAS policy in v2, the loads are considered safe to use in v4 as well, and will succeed without the NotSupportedException being thrown. &lt;/p&gt;

&lt;p&gt;For example, if you want to enable only a subset of LoadFroms to load assemblies in full trust, if you create a fully trusted simple sandbox, then any assemblies loaded into that sandbox would have the same full trust grant set in v2 as in v4.&amp;#160;&amp;#160; (The full trust grant set of the domain applies to all assemblies loaded into it).&amp;#160;&amp;#160; This will cause the CLR to allow the loads to proceed in full trust in v4 without having to throw the switch.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;
  &lt;pre style="margin: 0px"&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// Since this application only trusts a handful of LoadFrom operations,&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// we'll put them all into the same AppDomain which is a simple sandbox&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// with a full trust grant set.&amp;#160; The application itself will not enable&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// loadFromRemoteSources, but instead channel all of the trusted loads&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// into this domain.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;PermissionSet&lt;/span&gt; trustedLoadFromRemoteSourceGrantSet&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;PermissionSet&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;PermissionState&lt;/span&gt;.Unrestricted);&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;AppDomainSetup&lt;/span&gt; trustedLoadFromRemoteSourcesSetup = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;AppDomainSetup&lt;/span&gt;();&lt;/p&gt;&lt;p style="margin: 0px"&gt;trustedLoadFromRemoteSourcesSetup.ApplicationBase =&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;AppDomain&lt;/span&gt;.CurrentDomain.SetupInformation.ApplicationBase;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;AppDomain&lt;/span&gt; trustedRemoteLoadDomain =&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;AppDomain&lt;/span&gt;.CreateDomain(&lt;span style="color: #a31515"&gt;&amp;quot;Trusted LoadFromRemoteSources Domain&amp;quot;&lt;/span&gt;,&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;null&lt;/span&gt;,&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; trustedLoadFromRemoteSourcesSetup,&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; trustedLoadFromRemoteSourcesGrantSet);&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// Now all trusted remote LoadFroms can be done in the trustedRemoteLoadDomain,&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// and communicated with via a MarshalByRefObject.&lt;/span&gt;&lt;/p&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;As an example in the opposite direction, maybe your application has mostly loads which are safe to have remote targets, however there are a small handful of places that do need to be sandboxed.&amp;#160; By creating a simple sandboxed AppDomain for those loads, you can then safely set the loadFromRemoteSources switch for the rest of your process.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;
  &lt;pre style="margin: 0px"&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// Since this application trusts almost all of its assembly loads, it&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// is going to enable the process-wide loadFromRemoteSources switch.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// However, the loads that it does not trust still need to be sandboxed.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// First figure out a grant set that the CLR considers safe to apply&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// to code from the Internet.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;Evidence&lt;/span&gt; sandboxEvidence = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Evidence&lt;/span&gt;();&lt;/p&gt;&lt;p style="margin: 0px"&gt;sandboxEvidence.AddHostEvidence(&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Zone&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;SecurityZone&lt;/span&gt;.Internet));&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;PermissionSet&lt;/span&gt; remoteLoadGrantSet = &lt;span style="color: #2b91af"&gt;SecurityManager&lt;/span&gt;.GetStandardSandbox(sandboxEvidence);&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;AppDomainSetup&lt;/span&gt; remoteLoadSetup = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;AppDomainSetup&lt;/span&gt;();&lt;/p&gt;&lt;p style="margin: 0px"&gt;trustedLoadFromRemoteSourcesSetup.ApplicationBase = GetSandboxRoot();&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;AppDomain&lt;/span&gt; remoteLoadSandbox =&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;AppDomain&lt;/span&gt;.CreateDomain(&lt;span style="color: #a31515"&gt;&amp;quot;Remote Load Sandbox&amp;quot;&lt;/span&gt;,&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; sandboxEvidence,&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; remoteLoadSetup,&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; remoteLoadGrantSet);&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// Now all trusted remote LoadFroms can be done in the default domain&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// with loadFromRemoteSources set, and untrusted loads can be done&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// in the sandbox that we just setup.&lt;/span&gt;&lt;/p&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;(Similarly, if the process is in legacy CAS policy mode, the v4 CLR will have the same behavior as the v2 CLR, and there will be no exception).&lt;/p&gt;

&lt;p&gt;Let’s say that you’ve considered the security implications and your application is a good candidate to enable loadFromRemoteSources, how do you go about doing so?&amp;#160;&amp;#160; Basically, you just provide a .exe.config file for your application with a loadFromRemoteSources runtime switch enabled.&amp;#160;&amp;#160; So, if your application’s entry point is YourApp.exe, you’ll want to make a YourApp.exe.config.&amp;#160;&amp;#160; (Or use the app.config file in your Visual Studio project).&amp;#160;&amp;#160; This configuration file will need to contain runtime section such as:&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;
  &lt;pre style="margin: 0px"&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;configuration&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;runtime&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;loadFromRemoteSources&lt;/span&gt;&lt;span style="color: blue"&gt; &lt;/span&gt;&lt;span style="color: red"&gt;enabled&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;true&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt; /&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;#160; &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;runtime&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;configuration&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This setting will cause the CLR to notice that even though it is going to load an assembly that would have been sandboxed in the v2 runtime, your application has explicitly stated that this is a safe thing to do.&amp;#160;&amp;#160; Since your application has said that it understands the security impact of loading from remote locations and it is safe in the context of this application, the CLR will then allow these loads to succeed without throwing a NotSupportedException to block them.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9709114" width="1" height="1"&gt;</content><author><name>shawnfa</name><uri>http://blogs.msdn.com/members/shawnfa.aspx</uri></author><category term="Security" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Security/default.aspx" /><category term="CAS" scheme="http://blogs.msdn.com/shawnfa/archive/tags/CAS/default.aspx" /><category term="Policy" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Policy/default.aspx" /><category term="CLR v4" scheme="http://blogs.msdn.com/shawnfa/archive/tags/CLR+v4/default.aspx" /></entry><entry><title>CLR 4 Security on Channel 9</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/shawnfa/archive/2009/05/28/clr-4-security-on-channel-9.aspx" /><id>http://blogs.msdn.com/shawnfa/archive/2009/05/28/clr-4-security-on-channel-9.aspx</id><published>2009-05-28T23:30:00Z</published><updated>2009-05-28T23:30:00Z</updated><content type="html">&lt;p&gt;A while back I did an interview with &lt;a href="http://channel9.msdn.com/Niners/Charles/"&gt;Charles Torre&lt;/a&gt;&amp;#160; about the changes to security in CLR v4, and he posted it to the Channel 9 videos site yesterday.&lt;/p&gt;  &lt;p&gt;I start out talking about the security policy changes I've been covering here over the last week, and then transition into an overview of some of the transparency changes that I'll be talking about once I finish with the policy changes.&lt;/p&gt; &lt;object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="320" height="240"&gt; &lt;param name="source" value="http://channel9.msdn.com/App_Themes/default/vp09_04_23.xap" /&gt; &lt;param name="initParams" value="m=mms://mschnlnine.wmod.llnwd.net/a1809/d1/ch9/6/7/9/8/6/4/CLR4SecurityModel_s_ch9.wmv,autostart=false,autohide=true,showembed=true, thumbnail=http://mschnlnine.vo.llnwd.net/d1/ch9/6/7/9/8/6/4/CLR4SecurityModel_large_ch9.png, postid=468976" /&gt; &lt;param name="background" value="#00FFFFFF" /&gt; &lt;a href="http://go.microsoft.com/fwlink/?LinkID=124807" style="text-decoration: none;"&gt; &lt;img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none" /&gt; &lt;/a&gt; &lt;/object&gt;  &lt;p&gt;(The full video is also available here: &lt;a href="http://channel9.msdn.com/posts/Charles/Shawn-Farkas-CLR-4-Inside-the-new-Managed-Security-Model/" mce_href="http://channel9.msdn.com/posts/Charles/Shawn-Farkas-CLR-4-Inside-the-new-Managed-Security-Model/"&gt;http://channel9.msdn.com/posts/Charles/Shawn-Farkas-CLR-4-Inside-the-new-Managed-Security-Model/&lt;/a&gt;)&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9648833" width="1" height="1"&gt;</content><author><name>shawnfa</name><uri>http://blogs.msdn.com/members/shawnfa.aspx</uri></author><category term="Security" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Security/default.aspx" /><category term="CAS" scheme="http://blogs.msdn.com/shawnfa/archive/tags/CAS/default.aspx" /><category term="Policy" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Policy/default.aspx" /><category term="CLR v4" scheme="http://blogs.msdn.com/shawnfa/archive/tags/CLR+v4/default.aspx" /><category term="Transparency" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Transparency/default.aspx" /></entry><entry><title>Visual Studio 10 Security Tab Changes</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/shawnfa/archive/2009/05/28/visual-studio-10-security-tab-changes.aspx" /><id>http://blogs.msdn.com/shawnfa/archive/2009/05/28/visual-studio-10-security-tab-changes.aspx</id><published>2009-05-28T17:00:00Z</published><updated>2009-05-28T17:00:00Z</updated><content type="html">&lt;p&gt;Kris Makey, who works on the Visual Studio team, has written up a good blog post about the &lt;a href="http://blogs.msdn.com/krimakey/archive/2009/05/20/where-did-my-permission-set-controls-go.aspx"&gt;changes you’ll see on the security tab in Visual Studio 10 when it comes to editing permission sets&lt;/a&gt;.&amp;#160; He covers what the changes are, and some of the reasons why we worked with the Visual Studio team to make those changes.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9644742" width="1" height="1"&gt;</content><author><name>shawnfa</name><uri>http://blogs.msdn.com/members/shawnfa.aspx</uri></author><category term="Security" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Security/default.aspx" /><category term="CAS" scheme="http://blogs.msdn.com/shawnfa/archive/tags/CAS/default.aspx" /><category term="Visual Studio" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Visual+Studio/default.aspx" /><category term="CLR v4" scheme="http://blogs.msdn.com/shawnfa/archive/tags/CLR+v4/default.aspx" /></entry><entry><title>Coding with Security Policy in .NET 4.0 – Implicit uses of CAS policy</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/shawnfa/archive/2009/05/27/coding-with-security-policy-in-net-4-0-implicit-uses-of-cas-policy.aspx" /><id>http://blogs.msdn.com/shawnfa/archive/2009/05/27/coding-with-security-policy-in-net-4-0-implicit-uses-of-cas-policy.aspx</id><published>2009-05-27T20:46:59Z</published><updated>2009-05-27T20:46:59Z</updated><content type="html">&lt;p&gt;Last week we looked at &lt;a href="http://blogs.msdn.com/shawnfa/archive/2009/05/22/sandboxing-in-net-4-0.aspx"&gt;sandboxing and the v4 CLR&lt;/a&gt; – with the key change being that the CLR now defers exclusively to the host application when setting up sandboxed domains by moving away from the old CAS policy model, and moving instead to simple sandboxed AppDomains.&lt;/p&gt;  &lt;p&gt;This leads to an interesting situation when your program calls APIs that assume the presence of CAS policy, either implicitly [for example, &lt;a href="http://msdn.microsoft.com/en-us/library/0wcskf6d.aspx"&gt;Assembly.Load(string, Evidence)]&lt;/a&gt; or explicitly [for example &lt;a href="http://msdn.microsoft.com/en-us/library/system.security.securitymanager.policyhierarchy.aspx"&gt;SecurityManager.PolicyHierarchy&lt;/a&gt;].&amp;#160;&amp;#160; These APIs require CAS policy in order to return correct results, however by default there is no longer CAS policy to apply behind the scenes anymore.&lt;/p&gt;  &lt;p&gt;Let’s take a look at what happens if these APIs are called, and what should be done to update your code to take into account the new security policy model.&lt;/p&gt;  &lt;p&gt;(In addition to this blog post, the CLR security test team is preparing a set of blog posts about how they moved our test code base forward to deal with these and other v4 security changes – those posts will provide additional advice about how to replace uses of obsolete APIs based upon the real world examples they’ve seen).&lt;/p&gt;  &lt;p&gt;In general, APIs that assume the presence of CAS policy have been marked obsolete, and will give a compiler warning when you build against them:&lt;/p&gt;  &lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;Microsoft (R) Visual C# 2010 Compiler version 4.0.20506    &lt;br /&gt;Copyright (C) Microsoft Corporation. All rights reserved.     &lt;br /&gt;    &lt;br /&gt;obsolete.cs(32,1): warning CS0618: '&amp;lt;&lt;i&gt;API Name&amp;gt;&lt;/i&gt;' is     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; obsolete: 'This method is obsolete and will be removed in a future     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; release of the .NET Framework. Please use &lt;i&gt;&amp;lt;suggested alternate API&amp;gt;&lt;/i&gt;. See     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;a href="http://go.microsoft.com/fwlink/?LinkId=131738"&gt;http://go.microsoft.com/fwlink/?LinkId=131738&lt;/a&gt; for more information.'     &lt;br /&gt;&lt;/div&gt;  &lt;p&gt;Additionally, these APIs will throw a NotSupportedException if they are called at runtime:&lt;/p&gt;  &lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;Unhandled Exception: System.NotSupportedException: This method uses CAS policy, which has been obsoleted by the .NET Framework. In order to enable CAS policy for compatibility reasons, please use the NetFx40_LegacySecurityPolicy configuration switch. Please see &lt;a href="http://go.microsoft.com/fwlink/?LinkId=131738"&gt;http://go.microsoft.com/fwlink/?LinkId=131738&lt;/a&gt; for more information.&lt;/div&gt;  &lt;p&gt;(In the beta 1 release, this message is slightly different:)&lt;/p&gt;  &lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;Unhandled Exception: System.NotSupportedException: This method uses CAS policy, which has been obsoleted by the .NET Framework. In order to enable CAS policy for compatibility reasons, please use the legacyCasPolicy configuration switch. Please see &lt;a href="http://go.microsoft.com/fwlink/?LinkId=131738"&gt;http://go2.microsoft.com/fwlink/?LinkId=131738&lt;/a&gt; for more information.&lt;/div&gt;  &lt;p&gt;Let’s take a look at the set of APIs which make implicit use of CAS policy first, and then see what they might be replaced with in a v4.0 application.&lt;/p&gt;  &lt;p&gt;The general way to recognize an API which is implicitly using CAS policy is that they tend to take an Evidence parameter which was used to resolve against CAS policy and provide a grant set for an assembly.&amp;#160; For instance:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Activator.CreateInstance and Activator.CreateInstanceFrom overloads which take an Evidence parameter &lt;/li&gt;    &lt;li&gt;AppDomain.CreateInstance, AppDomain.CreateInstanceFrom, AppDomain.CreateInstanceAndUnwrap, and AppDomain.CreateInstanceAndUnwrap overloads which take an Evidence parameter &lt;/li&gt;    &lt;li&gt;AppDomain.DefineDynamicAssembly overloads which take an Evidence parameter &lt;/li&gt;    &lt;li&gt;AppDomain.ExecuteAssembly and AppDomain.ExecuteAssemblyByName overloads which take an Evidence parameter &lt;/li&gt;    &lt;li&gt;AppDomain.Load and AppDomain.LoadFrom overloads which take an Evidence parameter &lt;/li&gt;    &lt;li&gt;Assembly.Load and Assembly.LoadFrom overloads which take an Evidence parameter &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;It’s important to note that although these APIs all take Evidence parameters, the concept of Evidence itself is not deprecated and continues to exist (and even enhanced in v4.0 – but that’s another show).&amp;#160; Evidence itself is still a useful tool for hosts to use when figuring out what grant sets they want to give assemblies.&amp;#160; The common thread with these APIs is that they used the Evidence to resolve against CAS policy – and it’s the CAS policy portion that’s been deprecated in v4.&lt;/p&gt;  &lt;p&gt;Let’s say that your application is using one of the Evidence-taking overloads of these APIs, and thus had an implicit dependency on CAS policy.&amp;#160; Figuring out what to replace the API call with depends upon what your application was trying to accomplish with the API call.&lt;/p&gt;  &lt;p&gt;We’ve found that commonly the goal of calling one of these APIs was not to sandbox the assembly being loaded, but rather to access other parameters on the overload which may not be available without also providing Evidence.&amp;#160; In these cases, you can go ahead and just drop the Evidence parameter from the API.&amp;#160; We’ve ensured that all of the above APIs now have overloads that provide the full set of parameters without requiring an Evidence parameter.&lt;/p&gt;  &lt;p&gt;Additionally, in many cases we’ve found that code passes in Assembly.GetExecutingAssembly().Evidence or simply null to the Evidence parameter.&amp;#160; In both of those cases, it’s safe to simply call an overload of the API which does not require an Evidence parameter as well.&lt;/p&gt;  &lt;p&gt;The other reason to provide Evidence when calling these APIs is to sandbox the assembly in question.&amp;#160; The correct way to do this in v4 (and the best way to do this in v2.0 and higher of the .NET Framework) is to simply load the assembly &lt;a href="http://blogs.msdn.com/shawnfa/archive/2006/04/19/579066.aspx"&gt;into a simple sandboxed AppDomain&lt;/a&gt;.&amp;#160; The assembly will then be sandboxed by virtue of the fact that it’s loaded in the sandboxed domain, and you will no longer need to load the assembly with an Evidence parameter to restrict its grant set.&lt;/p&gt;  &lt;p&gt;I’ve listed the benefits of using simple sandboxed domains before, and they continue to apply in this scenario.&amp;#160; For example, using a simple sandbox rather than an Evidence resolve to sandbox assemblies allows your application:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;To be in charge of its own sandbox.&amp;#160; The load-with-Evidence route took a dependency on what the grant set that the CLR would give the assembly was.&amp;#160; That grant set could change from version to version of the CLR (&lt;a href="http://blogs.msdn.com/shawnfa/archive/2006/07/11/661769.aspx"&gt;since each version has independent CAS policies&lt;/a&gt;), and even from user to user.&amp;#160; This makes supporting your application more difficult than it needs to be – with simple sandboxing there are no external dependencies for grant set resolution – your application is in charge of its own sandboxes &lt;/li&gt;    &lt;li&gt;To setup real isolation boundaries – hosting multiple levels of partial trust code within a single AppDomain turns out to be incredibly difficult to do correctly.&amp;#160; Further, hosting partial trust code in a domain wtih full trust code that does not expect to be run along with partial trust code also turns out to be problematic from a security perspective.&amp;#160; By isolating the partial trust code in its own sandboxed domain, a real isolation boundary is setup for the code and your application is kept much more secure by default. &lt;/li&gt;    &lt;li&gt;To have version and bitness independence – I touched on this in the first point, but to reiterate it, your application is no longer dependent upon each version of the CLR’s security policy to be setup in the same way, as well as each bitness of the policy within a single version. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;So, to summarize, if you’re using one of the Evidence taking APIs which would have resolved an assembly’s grant set against CAS policy in the past:&lt;/p&gt;  &lt;table border="1" cellspacing="0" cellpadding="2"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top"&gt;&lt;b&gt;Use&lt;/b&gt;&lt;/td&gt;        &lt;td valign="top"&gt;&lt;b&gt;Replacement&lt;/b&gt;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top"&gt;Passing null, Assembly.GetExecutingAssembly().Evidence, or AppDomain.CurrentDomain.Evidence&lt;/td&gt;        &lt;td valign="top"&gt;Call an overload which does not require an Evidence parameter.&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top"&gt;Using a parameter of the API which was only available on an overload taking an Evidence parameter as well.&lt;/td&gt;        &lt;td valign="top"&gt;Call one of the newly added overloads which provides access to your parameter without requiring Evidence.&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top"&gt;Sandboxing the assembly being loaded.&lt;/td&gt;        &lt;td valign="top"&gt;Load the assembly into a sandboxed AppDomain, and let the domain do the sandboxing.&amp;#160; This will remove the need for the Evidence parameter.&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;Next time, I’ll look at the explicit uses of CAS policy, and what their replacements should be.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9644730" width="1" height="1"&gt;</content><author><name>shawnfa</name><uri>http://blogs.msdn.com/members/shawnfa.aspx</uri></author><category term="Security" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Security/default.aspx" /><category term="CAS" scheme="http://blogs.msdn.com/shawnfa/archive/tags/CAS/default.aspx" /><category term="Policy" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Policy/default.aspx" /><category term="CLR v4" scheme="http://blogs.msdn.com/shawnfa/archive/tags/CLR+v4/default.aspx" /></entry><entry><title>Sandboxing in .NET 4.0</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/shawnfa/archive/2009/05/22/sandboxing-in-net-4-0.aspx" /><id>http://blogs.msdn.com/shawnfa/archive/2009/05/22/sandboxing-in-net-4-0.aspx</id><published>2009-05-22T20:54:00Z</published><updated>2009-05-22T20:54:00Z</updated><content type="html">&lt;P&gt;Yesterday I talked about the &lt;A href="http://blogs.msdn.com/shawnfa/archive/2009/05/21/security-policy-in-the-v4-clr.aspx" mce_href="http://blogs.msdn.com/shawnfa/archive/2009/05/21/security-policy-in-the-v4-clr.aspx"&gt;changes in security policy for managed applications&lt;/A&gt;, namely that managed applications will run with full trust - the same as native applications - when you execute them directly.&lt;/P&gt;
&lt;P&gt;That change doesn’t mean that managed code can no longer be sandboxed however - far from it.&amp;nbsp; Hosts such as ASP.NET and ClickOnce continue to use the CLR to sandbox untrusted code.&amp;nbsp; Additionally, any application can continue to create AppDomains to sandbox code in.&lt;/P&gt;
&lt;P&gt;As part of our overhaul of security policy in v4, we made some interesting changes to how that sandboxing should be accomplished as well.&amp;nbsp; In previous releases, the CLR provided a variety of ways to sandbox code – but many of them were problematic to use correctly.&amp;nbsp; In the v4 framework, we made it a goal to simplify and standardize how sandboxing should be done in managed code.&lt;/P&gt;
&lt;P&gt;One of the key observations we made about sandboxing is that there really isn’t a good reason for the CLR to be involved in the decision as to what grant set should be given to partial trust code.&amp;nbsp;&amp;nbsp; If your application says “I want to run this code with ReflectionPermission/RestrictedMemberAccess and SecurityPermission/Execution”, that’s exactly the set of permissions that the code should run with.&amp;nbsp;&amp;nbsp; After all, your application knows much better than the CLR what operations the sandboxed code can be safely allowed to undertake.&lt;/P&gt;
&lt;P&gt;The problem is, sandboxing by providing an AppDomain policy level doesn’t provide total control to the application doing the sandboxing.&amp;nbsp; For instance, imagine you wanted to provide the sandbox grant set of RestrictedMemberAccess + Execution permission.&amp;nbsp; You might setup a policy level that grants AllCode this grant set and assign it to the AppDomain.&amp;nbsp;&amp;nbsp; However, if the code you place in that AppDomain has evidence that says it came from the Internet, the CLR will instead produce a grant set that doesn’t include RestrictedMemberAccess for the sandbox.&amp;nbsp; Rather than allowing safe partial trust reflection as you wanted, your sandbox just became execute-only.&lt;/P&gt;
&lt;P&gt;This really doesn’t make sense – what right does the CLR have to tell your application what should and should not be allowed in its sandboxes?&amp;nbsp; In the v1.x release of the runtime, developers had to go to great lengths in order to ensure they got the grant set they wanted.&amp;nbsp; (&lt;A href="http://blogs.msdn.com/ericlippert/" mce_href="http://blogs.msdn.com/ericlippert/"&gt;Eric Lippert’s&lt;/A&gt; CAS policy acrobatics to get VSTO working correctly is the stuff of legends around the security team – fabulous adventures in coding indeed!).&lt;/P&gt;
&lt;P&gt;As many a frustrated application developer found out, intersecting with the application supplied grant set was only the tip of the iceburg when it came to the difficulties of coding with CAS policy.&amp;nbsp; You would also run into a slew of other problems – such as each version of the CLR having an entirely independent security policy to deal with.&lt;/P&gt;
&lt;P&gt;In v2.0, we introduced the &lt;A href="http://blogs.msdn.com/shawnfa/archive/2006/04/19/579066.aspx" mce_href="http://blogs.msdn.com/shawnfa/archive/2006/04/19/579066.aspx"&gt;simple sandboxing API&lt;/A&gt; as a way for applications to say “This is the grant set I want my application to have.&amp;nbsp; Please don’t mess with it.”.&amp;nbsp; This went a long way toward making writing an application which sandboxes code an easier task.&lt;/P&gt;
&lt;P&gt;Beginning with v4.0, the CLR is getting out of the policy business completely.&amp;nbsp; By default, the CLR is not going to supply a CAS policy level that interferes with the wishes of the application that is trying to do sandboxing.&lt;/P&gt;
&lt;P&gt;Effectively, we’ve simplified the multiple ways that you could have sandboxed code in v3.5 into one easy option – all sandboxes in v4 must be setup with the simple sandboxing API.&lt;/P&gt;
&lt;P&gt;This means that the days of wrangling with complicated policy trees with arbitrary decision nodes in them are thankfully a thing of the past.&amp;nbsp; All that’s needed from here on out is a simple statement: “here is my sandboxed grant set, and here are the assemblies I wish to trust.”&lt;/P&gt;
&lt;P&gt;Next time, I’ll look at the implications of this on code that interacts with policy, looking at what you used to write, and what it would be replaced with in v4.0 of the CLR.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9635605" width="1" height="1"&gt;</content><author><name>shawnfa</name><uri>http://blogs.msdn.com/members/shawnfa.aspx</uri></author><category term="Security" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Security/default.aspx" /><category term="CAS" scheme="http://blogs.msdn.com/shawnfa/archive/tags/CAS/default.aspx" /><category term="Policy" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Policy/default.aspx" /><category term="CLR v4" scheme="http://blogs.msdn.com/shawnfa/archive/tags/CLR+v4/default.aspx" /></entry><entry><title>Security Policy in the v4 CLR</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/shawnfa/archive/2009/05/21/security-policy-in-the-v4-clr.aspx" /><id>http://blogs.msdn.com/shawnfa/archive/2009/05/21/security-policy-in-the-v4-clr.aspx</id><published>2009-05-21T22:03:38Z</published><updated>2009-05-21T22:03:38Z</updated><content type="html">&lt;p&gt;One of the first changes that you might see to security in the v4 CLR is that we’ve overhauled the security policy system.&amp;#160; In previous releases of the .NET Framework, CAS policy applied to all assemblies loaded into an application (except for in &lt;a href="http://blogs.msdn.com/shawnfa/archive/2006/04/19/579066.aspx"&gt;simple sandbox domains&lt;/a&gt;). &lt;/p&gt;  &lt;p&gt;That lead to a lot of interesting problems.&amp;#160; For instance, one of the more common issues people ran into was that they would develop an application on their local machine that they wanted to share with other people on the network.&amp;#160;&amp;#160; Once the application was working on their machine, they would share it out, but nobody could run it over the network because CAS policy provided a lower grant set to assemblies loaded from the intranet than it does to assemblies loaded from the local machine.&amp;#160;&amp;#160; The usual result was unexpected and unhandled SecurityExceptions when trying to use the application.&lt;/p&gt;  &lt;p&gt;Generally, the only solution to this problem was to either manually update the CAS policy on each machine that wanted to run the application, deploy the application some other way (for instance via ClickOnce), or use native code.&lt;/p&gt;  &lt;p&gt;One of the worst things about this problem was that the additional pain of not being able to just share a managed app over the network wasn’t actually buying any security.&amp;#160; If an application wanted to attack your machine, it could bypass the sandbox that the CLR was setting up simply by being written in native code.&lt;/p&gt;  &lt;p&gt;Effectively, running an executable &lt;em&gt;is&lt;/em&gt; a trust decision – you’re saying that you trust the application that you’re running enough to execute with the privileges your Windows account has.&lt;/p&gt;  &lt;p&gt;That leads to an interesting observation – the CLR isn’t the correct place to be setting permission restrictions for applications that are being launched directly (either from the command prompt, or from Windows explorer for instance).&amp;#160; Instead, that should be done through Windows itself using mechanisms like &lt;a href="http://technet.microsoft.com/en-us/library/cc779607(WS.10).aspx"&gt;SRP&lt;/a&gt;, which apply equally to both managed and native applications.&lt;/p&gt;  &lt;p&gt;In the v3.5 SP1 release, these observations (writing managed code to use on the network was harder than it needed to be, and it wasn’t even buying any extra security) &lt;a href="http://blogs.msdn.com/shawnfa/archive/2008/05/12/fulltrust-on-the-localintranet.aspx"&gt;led us to relax CAS policy for LocalIntranet applications slightly&lt;/a&gt;.&amp;#160;&amp;#160; We enabled applications that were run directly from an intranet share (and any assemblies loaded from immediately next to that application) to be fully trusted by pretending that it had MyComputer zone evidence instead of LocalIntranet.&lt;/p&gt;  &lt;p&gt;In the v4.0 release of the runtime, the CLR has taken that a step further.&amp;#160; &lt;strong&gt;&lt;em&gt;By default, unhosted applications are not subject to managed security policy when run under v4.0.&lt;/em&gt;&lt;/strong&gt;&amp;#160;&amp;#160; Effectively, this means any managed application that you launch from the command prompt or by double clicking the .exe in Windows Explorer will run fully trusted, as will all of the assemblies that it loads (including assemblies that it loads from a location other than the the directory where the executable lives).&lt;/p&gt;  &lt;p&gt;For applications run from the local machine, there really should be no observable change.&amp;#160; However, for applications that are shared out over a network, this means that everything should just work – just as if you had run the application from your computer while you were developing it.&lt;/p&gt;  &lt;p&gt;One very important point about this change is that it specifically applies only to unhosted code.&amp;#160; In my next post, we’ll look at what v4.0 security policy means for CLR hosts.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9634177" width="1" height="1"&gt;</content><author><name>shawnfa</name><uri>http://blogs.msdn.com/members/shawnfa.aspx</uri></author><category term="CAS" scheme="http://blogs.msdn.com/shawnfa/archive/tags/CAS/default.aspx" /><category term="Policy" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Policy/default.aspx" /><category term="CLR v4" scheme="http://blogs.msdn.com/shawnfa/archive/tags/CLR+v4/default.aspx" /></entry><entry><title>.NET 4.0 Security</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/shawnfa/archive/2009/05/20/net-4-0-security.aspx" /><id>http://blogs.msdn.com/shawnfa/archive/2009/05/20/net-4-0-security.aspx</id><published>2009-05-21T01:58:00Z</published><updated>2009-05-21T01:58:00Z</updated><content type="html">&lt;P&gt;The first beta of the &lt;A href="http://blogs.msdn.com/somasegar/archive/2009/05/18/visual-studio-2010-and-net-fx-4-beta-1-ships.aspx" mce_href="http://blogs.msdn.com/somasegar/archive/2009/05/18/visual-studio-2010-and-net-fx-4-beta-1-ships.aspx"&gt;v4.0 .NET Framework is now available&lt;/A&gt;, and with it comes a lot of changes to the CLR's security system.&amp;nbsp; We've updated both the policy and enforcement portions of the runtime in a lot of ways that I'm pretty excited to finally see available.&amp;nbsp; Since there are a lot of security changes, I'll spend the next month or so taking a deeper look at each of them.&amp;nbsp; At a high level, the major areas that are seeing updates with the v4 CLR are:&amp;nbsp; &lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://blogs.msdn.com/shawnfa/archive/2009/06/12/clr-v4-security-policy-roundup.aspx" mce_href="http://blogs.msdn.com/shawnfa/archive/2009/06/12/clr-v4-security-policy-roundup.aspx"&gt;Security policy&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;Security transparency&lt;/LI&gt;
&lt;LI&gt;APTCA&lt;/LI&gt;
&lt;LI&gt;Evidence&lt;/LI&gt;
&lt;LI&gt;AppDomain Managers&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;Like I did when we shipped the v2.0 CLR, I'll come back and update this post with links to the details about each of the features we added as I write more detailed blog posts about each of them.&lt;/P&gt;
&lt;P&gt;Tomorrow, I'll start by looking at probably the most visible change of the group - the update to the CLR's security policy system.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9633014" width="1" height="1"&gt;</content><author><name>shawnfa</name><uri>http://blogs.msdn.com/members/shawnfa.aspx</uri></author><category term="Security" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Security/default.aspx" /><category term="CAS" scheme="http://blogs.msdn.com/shawnfa/archive/tags/CAS/default.aspx" /><category term="CLR v4" scheme="http://blogs.msdn.com/shawnfa/archive/tags/CLR+v4/default.aspx" /></entry><entry><title>Authenticated Symmetric Encryption in .NET</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/shawnfa/archive/2009/03/17/authenticated-symmetric-encryption-in-net.aspx" /><id>http://blogs.msdn.com/shawnfa/archive/2009/03/17/authenticated-symmetric-encryption-in-net.aspx</id><published>2009-03-17T23:48:20Z</published><updated>2009-03-17T23:48:20Z</updated><content type="html">&lt;p&gt;Over the last week, we've made a couple of updates to our Codeplex projects to add authenticated symmetric encryption to the managed cryptography surface area for the first time.&amp;nbsp; Since we've never supported authenticated symmetric algorithms in managed code before, I thought I'd run though some basics about what they are and how to use them.&lt;/p&gt; &lt;p&gt;For starters, in order to use the authenticated symmetric encryption classes, you'll need a few prerequisites:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Windows Vista SP 1, Windows Server 2008, or higher  &lt;li&gt;.NET framework v3.5 SP 1 or higher  &lt;li&gt;Security.Cryptography.dll 1.3 or higher (from &lt;a title="http://codeplex.com/clrsecurity" href="http://codeplex.com/clrsecurity"&gt;http://codeplex.com/clrsecurity&lt;/a&gt;)&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Authenticated symmetric cryptography differs from the symmetric cryptography that the .NET framework has traditionally supported in that it produces an authentication tag in addition to ciphertext when encrypting data.&amp;nbsp; This authentication tag can be used to verify that the ciphertext has not been tampered with between when it was encrypted and decrypted.&lt;/p&gt; &lt;p&gt;For example, imagine you encrypted a message using the AES class:&lt;/p&gt; &lt;p&gt; &lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;using&lt;/span&gt; (&lt;span style="color: #2b91af"&gt;Aes&lt;/span&gt; aes = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;AesCng&lt;/span&gt;())&lt;/p&gt; &lt;p style="margin: 0px"&gt;{&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; aes.Key = key;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; aes.IV = iv;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;using&lt;/span&gt; (&lt;span style="color: #2b91af"&gt;MemoryStream&lt;/span&gt; ms = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MemoryStream&lt;/span&gt;())&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;using&lt;/span&gt; (&lt;span style="color: #2b91af"&gt;CryptoStream&lt;/span&gt; cs = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;CryptoStream&lt;/span&gt;(ms, aes.CreateEncryptor(), &lt;span style="color: #2b91af"&gt;CryptoStreamMode&lt;/span&gt;.Write))&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;byte&lt;/span&gt;[] plaintext = &lt;span style="color: #2b91af"&gt;Encoding&lt;/span&gt;.UTF8.GetBytes(&lt;span style="color: #a31515"&gt;"Secret data to be encrypted."&lt;/span&gt;);&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cs.Write(plaintext, 0, plaintext.Length);&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cs.FlushFinalBlock();&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; ms.ToArray();&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt; &lt;p style="margin: 0px"&gt;}&lt;/p&gt;&lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;While an attacker cannot read the data encrypted by this operation without knowing the encryption key, they can modify the ciphertext bytes themselves which will result in corruption of the decrypted message on the receiving end:&lt;/p&gt; &lt;p&gt; &lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// The ciphertext is protected from being decrypted without knowledge of the key, however it&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// is not protected from being tampered with:&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;ciphertext[5] = 0x21;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;using&lt;/span&gt; (&lt;span style="color: #2b91af"&gt;Aes&lt;/span&gt; aes = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;AesCng&lt;/span&gt;())&lt;/p&gt; &lt;p style="margin: 0px"&gt;{&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; aes.Key = key;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; aes.IV = iv;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;using&lt;/span&gt; (&lt;span style="color: #2b91af"&gt;MemoryStream&lt;/span&gt; ms = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MemoryStream&lt;/span&gt;())&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;using&lt;/span&gt; (&lt;span style="color: #2b91af"&gt;CryptoStream&lt;/span&gt; cs = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;CryptoStream&lt;/span&gt;(ms, aes.CreateDecryptor(), &lt;span style="color: #2b91af"&gt;CryptoStreamMode&lt;/span&gt;.Write))&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cs.Write(ciphertext, 0, ciphertext.Length);&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cs.FlushFinalBlock();&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;byte&lt;/span&gt;[] decrypted = ms.ToArray();&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;string&lt;/span&gt; message = &lt;span style="color: #2b91af"&gt;Encoding&lt;/span&gt;.UTF8.GetString(decrypted);&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(message);&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt; &lt;p style="margin: 0px"&gt;}&lt;/p&gt;&lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;Which produces output such as:&lt;/p&gt; &lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;? ]\??e enc?ypted.&lt;/div&gt; &lt;p&gt;This can be solved by signing the ciphertext, and having the receiving party verify the signature before decrypting the secret message.&lt;/p&gt; &lt;p&gt;Authenticated symmetric algorithms solve this problem by creating an authentication tag which can be used to verify that the ciphertext has not been tampered with since it was generated.&lt;/p&gt; &lt;p&gt;In addition to verifying that the ciphertext has not been modified, the authenticated symmetric algorithms that we have in managed code also take additional authenticated data as an input.&amp;nbsp; This data is not included in the ciphertext itself - so when decrypting a message that was created with additional authenticated data, the output will not contain the authenticated data.&amp;nbsp; Instead, the authenticated data is only used in generating the authentication tag.&amp;nbsp; This means that much like the key and IV, both the encrypting and decrypting parties need to know what the additional authenticated data is otherwise the authentication tag will not verify.&lt;/p&gt; &lt;h2&gt;&lt;/h2&gt; &lt;h2&gt;Authenticated Symmetric Algorithm Type Hierarchy&lt;/h2&gt; &lt;p&gt;This functionality is exposed in managed code via the AuthenticatedSymmetricAlgorithm base class.&amp;nbsp; Much like the SymmetricAlgorithm base class, AuthenticatedSymmetricAlgorithm is an abstract class for actual authenticated symmetric algorithms to derive from.&lt;/p&gt; &lt;p&gt;Currently, the only authenticated symmetric algorithm is an authenticated version of AES, which is represented by the AuthenticatedAes abstract base class.&amp;nbsp; Again, mirroring the symmetric algorithm type hierarchy, AuthenticatedAes is an abstract base class that concrete authenticated AES implementations derive from.&amp;nbsp; (Much like Aes serves as the base class for AesManaged, AesCryptoServiceProvider and AesCng).&lt;/p&gt; &lt;p&gt;The concrete implementation of AuthenticatedAesCng which is in Security.Cryptography.dll 1.3 is built on top of&amp;nbsp; CNG, and follows our traditional naming scheme: AuthenticatedAesCng.&lt;/p&gt; &lt;h2&gt;Setting up an Authenticated AES Encryptor&lt;/h2&gt; &lt;p&gt;The authentication tag is generated by an authenticated chaining algorithm, which is used in place of the standard chaining modes that AES can use (such as CBC or ECB).&amp;nbsp; Currently CNG supports two algorithms for generating an authentication tag with AES:&lt;/p&gt; &lt;ol&gt; &lt;li&gt;Galois/Counter Mode - this is the default, and is represented by CngChainingMode.Gcm.&amp;nbsp; (&lt;a title="http://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/gcm/gcm-spec.pdf" href="http://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/gcm/gcm-spec.pdf"&gt;http://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/gcm/gcm-spec.pdf&lt;/a&gt;)  &lt;li&gt;Coutner with CBC-MAC - this is selected by using CngChainingMode.Ccm.&amp;nbsp; (&lt;a title="http://www.ietf.org/rfc/rfc3610.txt" href="http://www.ietf.org/rfc/rfc3610.txt"&gt;http://www.ietf.org/rfc/rfc3610.txt&lt;/a&gt;)&lt;/li&gt;&lt;/ol&gt; &lt;p&gt;Since neither of these chaining modes are supported by the standard CipherMode enumeration, AuthenticatedAesCng adds a new property called CngMode which allows you to specify a CngChainingMode rather than a standard CipherMode.&amp;nbsp; Trying to use the traditional Mode property when the CngChainingMode is set to one of these new values will result in an exception.&lt;/p&gt; &lt;p&gt;The IV property is used to setup what the two algorithm specifications above refer to as a nonce.&amp;nbsp; Unlike traditional AES, the IV size does not match the block size, but is instead specified by the chaining mode.&amp;nbsp; For instance, both GCM and CCM can work with an IV of 12 bytes.&lt;/p&gt; &lt;p&gt;AuthenticatedSymmetricAlgorithms also have an AuthenticatedData byte array property which is used to setup the additional authentication data being used in the tag generation.&amp;nbsp; This property is optional - leaving the value null means that the authentication tag is generated only from the plaintext.&lt;/p&gt; &lt;p&gt;The last interesting property on the encryption side is the TagSize property.&amp;nbsp; This property specifies the size (in bits) of the authentication tag to generate.&amp;nbsp; The LegalTagSizes property contains information about which sizes are valid for the current chaining mode (and the ValidTagSize method allows you to quickly test to see if a tag size is valid).&lt;/p&gt; &lt;p&gt;Once the AuthenticatedAesCng object is setup, we'll need to create an encryptor to do the actual encryption operation.&amp;nbsp; This can be done by calling the CreateAuthenticatedEncryptor method.&amp;nbsp; CreateAuthenticatedEncryptor returns an IAuthenticatdCryptoTransform rather than an ICryptoTransform since IAuthenticatedCryptoTransform allows us access to the authentication tag after the encryption is done.&amp;nbsp; The CreateEncryptor overloads also return IAuthenticatedCryptoTransforms, however they are typed as ICryptoTransform because they're defined on the SymmetricAlgorithm base type.&amp;nbsp; If you call one of the these methods, then you'll have to manually cast to IAuthenticatedCryptoTransform.&lt;/p&gt; &lt;p&gt;Putting this all together, code to encrypt and generate an authentication tag using AuthenticatedAesCng would look something like this:&lt;/p&gt; &lt;p&gt; &lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;using&lt;/span&gt; (&lt;span style="color: #2b91af"&gt;AuthenticatedAesCng&lt;/span&gt; aes = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;AuthenticatedAesCng&lt;/span&gt;())&lt;/p&gt; &lt;p style="margin: 0px"&gt;{&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// Setup an authenticated chaining mode - The two current CNG options are&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// CngChainingMode.Gcm and CngChainingMode.Ccm.&amp;nbsp; This should be done before setting up&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// the other properties, since changing the chaining mode can update things such as the&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// valid and current tag sizes.&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; aes.CngMode = &lt;span style="color: #2b91af"&gt;CngChainingMode&lt;/span&gt;.Ccm;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// Keys work the same as standard AES&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; aes.Key = key;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// The IV (called the nonce in many of the authenticated algorithm specs) is not sized for&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// the input block size. Instead its size depends upon the algorithm.&amp;nbsp; 12 bytes works&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// for both GCM and CCM. Generate a random 12 byte nonce here.&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; nonce = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: blue"&gt;byte&lt;/span&gt;[12];&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; rng.GetBytes(nonce);&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; aes.IV = nonce;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// Authenticated data becomes part of the authentication tag that is generated during&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// encryption, however it is not part of the ciphertext.&amp;nbsp; That is, when decrypting the&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// ciphertext the authenticated data will not be produced.&amp;nbsp; However, if the&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// authenticated data does not match at encryption and decryption time, the&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// authentication tag will not validate.&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; aes.AuthenticatedData = &lt;span style="color: #2b91af"&gt;Encoding&lt;/span&gt;.UTF8.GetBytes(&lt;span style="color: #a31515"&gt;"Additional authenticated data"&lt;/span&gt;);&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// Perform the encryption - this works nearly the same as standard symmetric encryption,&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// however instead of using an ICryptoTransform we use an IAuthenticatedCryptoTrasform&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// which provides access to the authentication tag.&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;using&lt;/span&gt; (&lt;span style="color: #2b91af"&gt;MemoryStream&lt;/span&gt; ms = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MemoryStream&lt;/span&gt;())&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;using&lt;/span&gt; (&lt;span style="color: #2b91af"&gt;IAuthenticatedCryptoTransform&lt;/span&gt; encryptor = aes.CreateAuthenticatedEncryptor())&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;using&lt;/span&gt; (&lt;span style="color: #2b91af"&gt;CryptoStream&lt;/span&gt; cs = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;CryptoStream&lt;/span&gt;(ms, encryptor, &lt;span style="color: #2b91af"&gt;CryptoStreamMode&lt;/span&gt;.Write))&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// Encrypt the secret message&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;byte&lt;/span&gt;[] plaintext = &lt;span style="color: #2b91af"&gt;Encoding&lt;/span&gt;.UTF8.GetBytes(&lt;span style="color: #a31515"&gt;"Secret data to be encrypted and authenticated."&lt;/span&gt;);&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cs.Write(plaintext, 0, plaintext.Length);&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// Finish the encryption and get the output authentication tag and ciphertext&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cs.FlushFinalBlock();&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; tag = encryptor.GetTag();&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ciphertext = ms.ToArray();&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt; &lt;p style="margin: 0px"&gt;}&lt;/p&gt;&lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;Notice how the tag is accessed by calling GetTag on the IAuthenticatedCryptoTransform after we've completed all encryption and flushed the final block.&amp;nbsp; If GetTag is called before this, it will throw an InvalidOperaitonException as AuthenticatedAesCng does not support generating partial tags for partially encrypted data.&amp;nbsp; Also, encryption will notably not set the Tag property of the AuthenticatedAesCng object which was used to create the encryptor.&amp;nbsp; (The tag is an output, not an input, and therefore does not get propagated back to the AuthenticatedAesCng object which acts like a crypto transform factory).&lt;/p&gt; &lt;h2&gt;Setting up an Authenticated AES Decryptor&lt;/h2&gt; &lt;p&gt;Setting up an authenticated AES object to do decryption is very similar to setting one up to do encryption.&amp;nbsp; The Key, IV, AuthenticationData, and CngMode all need to be setup to match the parameters in place when the ciphertext being decrypted was encrypted.&amp;nbsp; The only additional property that needs to be set is the Tag property.&amp;nbsp; Unsurprisingly, this should be set to be the output of the GetTag call on the encryptor.&lt;/p&gt; &lt;p&gt;We'll end up with decryption code along the lines of:&lt;/p&gt; &lt;p&gt; &lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// To decrypt, we need to know the nonce, key, additional authenticated data, and&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// authentication tag.&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;using&lt;/span&gt; (&lt;span style="color: #2b91af"&gt;AuthenticatedAesCng&lt;/span&gt; aes = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;AuthenticatedAesCng&lt;/span&gt;())&lt;/p&gt; &lt;p style="margin: 0px"&gt;{&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// Chaining modes, keys, and IVs must match between encryption and decryption&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; aes.CngMode = &lt;span style="color: #2b91af"&gt;CngChainingMode&lt;/span&gt;.Ccm;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; aes.Key = key;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; aes.IV = nonce;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// If the authenticated data does not match between encryption and decryption, then the&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// authentication tag will not match either, and the decryption operation will fail.&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; aes.AuthenticatedData = &lt;span style="color: #2b91af"&gt;Encoding&lt;/span&gt;.UTF8.GetBytes(&lt;span style="color: #a31515"&gt;"Additional authenticated data"&lt;/span&gt;);&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// The tag that was generated during encryption gets set here as input to the decryption&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// operation.&amp;nbsp; This is in contrast to the encryption code path which does not use the&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// Tag property (since it is an output from encryption).&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; aes.Tag = tag;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// Decryption works the same as standard symmetric encryption&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;using&lt;/span&gt; (&lt;span style="color: #2b91af"&gt;MemoryStream&lt;/span&gt; ms = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MemoryStream&lt;/span&gt;())&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;using&lt;/span&gt; (&lt;span style="color: #2b91af"&gt;CryptoStream&lt;/span&gt; cs = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;CryptoStream&lt;/span&gt;(ms, aes.CreateDecryptor(), &lt;span style="color: #2b91af"&gt;CryptoStreamMode&lt;/span&gt;.Write))&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cs.Write(ciphertext, 0, ciphertext.Length);&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// If the authentication tag does not match, we'll fail here with a&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// CryptographicException, and the ciphertext will not be decrypted.&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cs.FlushFinalBlock();&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;byte&lt;/span&gt;[] plaintext = ms.ToArray();&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;"Decrypted and authenticated message: {0}"&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;Encoding&lt;/span&gt;.UTF8.GetString(plaintext));&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt; &lt;p style="margin: 0px"&gt;}&lt;/p&gt;&lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;If the authentication tag generated while decrypting the ciphertext (taking into account any optional authenticated data provided), then an exception will be thrown when decryption is completed.&amp;nbsp; The decryptor will also not produce any plaintext until the authentication tag is verified - this way partial plaintext cannoot be used if the authentication tag does not match.&lt;/p&gt; &lt;p&gt;Now that we're using AuthenticatedAesCng to do our encryption, the scenario where someone tampers the ciphertext no longer works.&amp;nbsp; While we won't be able to access the corrupted plaintext anymore, we also will be unable to mistake it for valid plaintext.&amp;nbsp; If the authentication tag does not match while decrypting (most commonly because the ciphertext was tampered with or the authenticated data was not correct), the following exception is thrown:&lt;/p&gt; &lt;p&gt; &lt;div style="border-bottom: black thin inset; border-left: black thin inset; padding-bottom: 1em; margin: 1em 1em 1em 2em; padding-left: 1em; padding-right: 1em; font-family: monospace; background: lightgrey; color: black; font-size: 10pt; border-top: black thin inset; border-right: black thin inset; padding-top: 1em"&gt;Unhandled Exception: System.Security.Cryptography.CryptographicException: The computed authentication tag did not match the input authentication tag.&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; at Security.Cryptography.BCryptNative.SymmetricDecrypt(SafeBCryptKeyHandle key, Byte[] input, Byte[] chainData, BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO&amp;amp; authenticationInfo)&lt;br&gt;&amp;nbsp;&amp;nbsp; at Security.Cryptography.BCryptAuthenticatedSymmetricCryptoTransform.CngTransform(Byte[] input, Int32 inputOffset, Int32 inputCount)&lt;br&gt;&amp;nbsp;&amp;nbsp; at Security.Cryptography.BCryptAuthenticatedSymmetricCryptoTransform.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount)&lt;br&gt;&amp;nbsp;&amp;nbsp; at System.Security.Cryptography.CryptoStream.FlushFinalBlock()&lt;/div&gt; &lt;h2&gt;Security.Cryptography.Debug Support&lt;/h2&gt; &lt;p&gt;The Security.Cryptography.Debug library has also been updated on the Codeplex in order to support the same type of debugging of AuthenicatedSymmetricAlgorithm objects that was already supported for SymmetricAlgorithm objects.&amp;nbsp; This support is enabled in the v1.1 release and higher of Security.Cryptography.Debug.dll - but since it requires a dependency on the Security.Cryptography.dll library, it is not enabled in the FxOnly builds.&amp;nbsp; Instead, you'll need to download the full binary package or build the sources manually to get the AuthenticatedSymmetricAlgorithm debugging support.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9484740" width="1" height="1"&gt;</content><author><name>shawnfa</name><uri>http://blogs.msdn.com/members/shawnfa.aspx</uri></author><category term="Cryptography" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Cryptography/default.aspx" /><category term="CNG" scheme="http://blogs.msdn.com/shawnfa/archive/tags/CNG/default.aspx" /></entry><entry><title>MD5 on Silverlight</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/shawnfa/archive/2008/12/09/md5-on-silverlight.aspx" /><id>http://blogs.msdn.com/shawnfa/archive/2008/12/09/md5-on-silverlight.aspx</id><published>2008-12-10T04:39:03Z</published><updated>2008-12-10T04:39:03Z</updated><content type="html">&lt;p&gt;Reid Borsuk, an SDE/T on the CLR security team, has released &lt;a href="http://code.msdn.microsoft.com/SilverlightMD5"&gt;a fully transparent implementation of the MD5 hash algorithm to the MSDN Code Gallery&lt;/a&gt;.&amp;#160; Since the code is entirely transparent, it can be used as part of a Silverlight application that needs to compute MD5 hashes in order to interop with existing code or file formats that requires MD5.&lt;/p&gt;  &lt;p&gt;He's also released an MD5Managed type to wrap around his implementation, in case you want to plug the algorithm into the standard .NET hash object model.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9188680" width="1" height="1"&gt;</content><author><name>shawnfa</name><uri>http://blogs.msdn.com/members/shawnfa.aspx</uri></author><category term="Cryptography" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Cryptography/default.aspx" /><category term="Silverlight" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Silverlight/default.aspx" /></entry><entry><title>CryptoConfig</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/shawnfa/archive/2008/12/02/cryptoconfig.aspx" /><id>http://blogs.msdn.com/shawnfa/archive/2008/12/02/cryptoconfig.aspx</id><published>2008-12-03T01:26:23Z</published><updated>2008-12-03T01:26:23Z</updated><content type="html">&lt;p&gt;The crypto config schema has been a bit of a hot topic around here lately, specifically around how to modify the CLR's machine.config to get custom crypto types registered with CryptoConfig.&lt;/p&gt; &lt;p&gt;Let's take a quick look at what CryptoConfig is first, and then we'll see how to customize its behavior.&amp;nbsp; &lt;a href="http://msdn.microsoft.com/en-us/library/system.security.cryptography.cryptoconfig.aspx"&gt;CryptoConfig&lt;/a&gt; is a type in mscorlib which allows cryptography classes to be created from a string rather than using a hard coded type.&amp;nbsp; For instance, you can say:&lt;/p&gt; &lt;div style="border-right: black thin inset; padding-right: 1em; border-top: black thin inset; padding-left: 1em; font-size: 10pt; background: lightgrey; padding-bottom: 1em; margin: 1em 1em 1em 2em; border-left: black thin inset; color: black; padding-top: 1em; border-bottom: black thin inset; font-family: monospace"&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;HashAlgorithm&lt;/span&gt; hashAlgorithm = &lt;span style="color: #2b91af"&gt;CryptoConfig&lt;/span&gt;.CreateFromName(&lt;span style="color: #a31515"&gt;"SHA256Managed"&lt;/span&gt;) &lt;span style="color: blue"&gt;as&lt;/span&gt; &lt;span style="color: #2b91af"&gt;HashAlgorithm&lt;/span&gt;;&lt;/p&gt;&lt;/div&gt; &lt;p&gt;to create instances of crypto types. This means that rather than having to hard code algorithms and implementations into your assembly itself, you can accept the name of the algorithm as a configuration parameter to achieve some measure of crypto agility.&amp;nbsp; CryptoConfig is also the underlying mechanism that allows the algorithm factory methods to work.&amp;nbsp; For instance, the above snippet is more commonly written as:&lt;/p&gt; &lt;div style="border-right: black thin inset; padding-right: 1em; border-top: black thin inset; padding-left: 1em; font-size: 10pt; background: lightgrey; padding-bottom: 1em; margin: 1em 1em 1em 2em; border-left: black thin inset; color: black; padding-top: 1em; border-bottom: black thin inset; font-family: monospace"&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;HashAlgorithm&lt;/span&gt; hashAlgorithm = &lt;span style="color: #2b91af"&gt;HashAlgorithm&lt;/span&gt;.Create(&lt;span style="color: #a31515"&gt;"SHA256Managed"&lt;/span&gt;);&lt;/p&gt;&lt;/div&gt; &lt;p&gt;CryptoConfig comes built in with names of algorithms that ship with the .NET Framework (with the exception of the &lt;a href="http://blogs.msdn.com/shawnfa/archive/2007/01/17/new-crypto-algorithms-in-orcas.aspx"&gt;new algorithms introduced in .NET 3.5&lt;/a&gt; due to red bits / green bits restrictions).&amp;nbsp; You can also extend the names that CryptoConfig understands if you have your own algorithms that you would like to be createable by name.&amp;nbsp; In fact, you can even do this to get the .NET 3.5 algorithms registered.&lt;/p&gt; &lt;p&gt;The customizable algorithm name mappings are setup in the machine.config file in the config subdirectory of the CLR installation directory.&amp;nbsp; For instance, for the 32 bit .NET 2.0, 3.0, and 3.5 releases you would register your types in the %WINDIR%\Microsoft.NET\Framework\v2.0.50727\config\machine.config file.&amp;nbsp; (For the 64 bit versions, you would also need to modify the equivalent file in Framework64).&lt;/p&gt; &lt;p&gt;CryptoConfig looks for information in the configuration/mscorlib/cryptographySettings element of machine.config. If there are multiple mscorlib sections, then crypto config prefers one with a version attribute that matches the current runtime -- however, in general there is only one mscorlib element.&lt;/p&gt; &lt;div style="border-right: black thin inset; padding-right: 1em; border-top: black thin inset; padding-left: 1em; font-size: 10pt; background: lightgrey; padding-bottom: 1em; margin: 1em 1em 1em 2em; border-left: black thin inset; color: black; padding-top: 1em; border-bottom: black thin inset; font-family: monospace"&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;configuration&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;mscorlib&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;cryptographySettings&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;cryptoNameMapping&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;!--&lt;/span&gt;&lt;span style="color: green"&gt; name mappings &lt;/span&gt;&lt;span style="color: blue"&gt;--&amp;gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;cryptoNameMapping&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;oidMap&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;!--&lt;/span&gt;&lt;span style="color: green"&gt; OID mappings &lt;/span&gt;&lt;span style="color: blue"&gt;--&amp;gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;oidMap&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;cryptographySettings&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;mscorlib&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;configuration&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;/div&gt; &lt;p&gt;Name mappings are created in a nameMappings element under cryptographySettings.&amp;nbsp; In order to setup a name mapping, two steps are required:&lt;/p&gt; &lt;ol&gt; &lt;li&gt;Map the implementation type to an alias by registering it as a crypto class  &lt;li&gt;Map the alias to the name that you wish to use in CryptoConfig to create an instance of the class.&lt;/li&gt;&lt;/ol&gt; &lt;p&gt;For example, imagine that you want to register the SHA256CryptoServiceProvider type that shipped in .NET 3.5 to be able to be created with the strings "SHA256", "SHA256CryptoServiceProvider", and "System.Security.Cryptography.SHA256ServiceProvider".&amp;nbsp; The first step is to register SHA256CryptoServiceProvider as a crypto class.&amp;nbsp; We can do this by creating a cryptoClasses node within the cryptoNameMapping element:&lt;/p&gt; &lt;div style="border-right: black thin inset; padding-right: 1em; border-top: black thin inset; padding-left: 1em; font-size: 10pt; background: lightgrey; padding-bottom: 1em; margin: 1em 1em 1em 2em; border-left: black thin inset; color: black; padding-top: 1em; border-bottom: black thin inset; font-family: monospace"&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;cryptoClasses&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;cryptoClass&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: red"&gt;SHA256CSP&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;System.Security.Cryptography.SHA256CryptoServiceProvider, System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089&lt;/span&gt;"&lt;span style="color: blue"&gt; /&amp;gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;cryptoClasses&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;/div&gt; &lt;p&gt;This creates an alias of SHA256CSP that refers to the SHA256CryptoServiceProvier type in System.Core.dll.&amp;nbsp; Note that the assemblies used in CryptoConfig must reside in the GAC; in this case System.Core.dll is in the GAC so registering SHA256CryptoServiceProvider is valid.&lt;/p&gt; &lt;p&gt;Now that we have created an alias we need to setup some names to create it with at runtime.&amp;nbsp; These names are created using nameEntry elements in the cryptoNameMapping element:&lt;/p&gt; &lt;div style="border-right: black thin inset; padding-right: 1em; border-top: black thin inset; padding-left: 1em; font-size: 10pt; background: lightgrey; padding-bottom: 1em; margin: 1em 1em 1em 2em; border-left: black thin inset; color: black; padding-top: 1em; border-bottom: black thin inset; font-family: monospace"&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;nameEntry&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: red"&gt;name&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;SHA256&lt;/span&gt;"&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: red"&gt;class&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;SHA256CSP&lt;/span&gt;"&lt;span style="color: blue"&gt; /&amp;gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;nameEntry&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: red"&gt;name&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;SHA256CryptoServiceProvider&lt;/span&gt;"&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: red"&gt;class&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;SHA256CSP&lt;/span&gt;"&lt;span style="color: blue"&gt; /&amp;gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;nameEntry&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: red"&gt;name&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;System.Security.Cryptography.SHA256CryptoServiceProvider&lt;/span&gt;"&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: red"&gt;class&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;SHA256CSP&lt;/span&gt;"&lt;span style="color: blue"&gt; /&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;/div&gt; &lt;p&gt;Each nameEntry element maps a string which CryptoConfig will accept to the alias for the type that it will create.&amp;nbsp; Here, we've setup entries that allow CryptoConfig to create a SHA256CryptoServiceProvier object via the names "SHA256", "SHA256CryptoServiceProvider", and "System.Security.Cryptography.SHA256CryptoServiceProvider".&lt;/p&gt; &lt;p&gt;If you're paying close attention, you'll notice that I mapped SHA256 to the .NET 3.5 SHA256CryptoServiceProvider class, even though the CLR already has a built in mapping for SHA256 to the SHA256Managed class.&amp;nbsp; In the case of a collision like this, machine.config entries take precedence over the built in mappings so these entries have the effect of changing the result of HashAlgorithm.Create("SHA256") from being a SHA256Managed object to being a SHA256CryptoServiceProvider object.&lt;/p&gt; &lt;p&gt;The final XML we ended up with for this example looks like this:&lt;/p&gt; &lt;div style="border-right: black thin inset; padding-right: 1em; border-top: black thin inset; padding-left: 1em; font-size: 10pt; background: lightgrey; padding-bottom: 1em; margin: 1em 1em 1em 2em; border-left: black thin inset; color: black; padding-top: 1em; border-bottom: black thin inset; font-family: monospace"&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;configuration&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;!--&lt;/span&gt;&lt;span style="color: green"&gt; ... other configuration data ... &lt;/span&gt;&lt;span style="color: blue"&gt;--&amp;gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;mscorlib&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;!--&lt;/span&gt;&lt;span style="color: green"&gt; ... other configuration data ... &lt;/span&gt;&lt;span style="color: blue"&gt;--&amp;gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;cryptographySettings&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;cryptoNameMapping&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;cryptoClasses&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;cryptoClass&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: red"&gt;SHA256CSP&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;System.Security.Cryptography.SHA256CryptoServiceProvider, System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089&lt;/span&gt;"&lt;span style="color: blue"&gt; /&amp;gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;cryptoClasses&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;nameEntry&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: red"&gt;name&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;SHA256&lt;/span&gt;"&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: red"&gt;class&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;SHA256CSP&lt;/span&gt;"&lt;span style="color: blue"&gt; /&amp;gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;nameEntry&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: red"&gt;name&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;SHA256CryptoServiceProvider&lt;/span&gt;"&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: red"&gt;class&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;SHA256CSP&lt;/span&gt;"&lt;span style="color: blue"&gt; /&amp;gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;nameEntry&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: red"&gt;name&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;System.Security.Cryptography.SHA256CryptoServiceProvider&lt;/span&gt;"&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: red"&gt;class&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;SHA256CSP&lt;/span&gt;"&lt;span style="color: blue"&gt; /&amp;gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;cryptoNameMapping&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;cryptographySettings&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;mscorlib&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;configuration&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;/div&gt; &lt;p&gt;A couple of other small crypto config notes:&lt;/p&gt; &lt;p&gt;Above I mentioned that there is the ability to setup OID mappings - this allows you to add entries to the results returned from the &lt;a href="http://msdn.microsoft.com/en-us/library/system.security.cryptography.cryptoconfig.mapnametooid.aspx"&gt;CryptoConfig.MapNameToOid&lt;/a&gt; API.&amp;nbsp; These entries go in the oidMap element and are simple name -&amp;gt; OID pairs.&amp;nbsp; Like name map entries above, machine.config values take precedence over built-in values:&lt;/p&gt; &lt;div style="border-right: black thin inset; padding-right: 1em; border-top: black thin inset; padding-left: 1em; font-size: 10pt; background: lightgrey; padding-bottom: 1em; margin: 1em 1em 1em 2em; border-left: black thin inset; color: black; padding-top: 1em; border-bottom: black thin inset; font-family: monospace"&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;oidMap&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;oidEntry&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: red"&gt;OID&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;2.16.840.1.101.3.4.2.1&lt;/span&gt;"&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: red"&gt;name&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;SHA256&lt;/span&gt;"&lt;span style="color: blue"&gt; /&amp;gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;oidMap&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;/div&gt; &lt;p&gt;Generally this is much less useful than the name mappings, since it really only allows you to access your custom OIDs via MapNameToOid -- however it's there if you do want to add custom name-&amp;gt;OID pairs.&lt;/p&gt; &lt;p&gt;A final note is that although CryptoConfig itself doesn't have an API to modify the mappings at runtime, it is sometimes a lot more convenient to programmatically add crypto name mappings for your application at runtime than to worry about getting all the machine.config XML correctly added at installation time.&amp;nbsp; This can be done with the &lt;a href="http://www.codeplex.com/clrsecurity/Wiki/View.aspx?title=Security.Cryptography.CryptoConfig2"&gt;CryptoConfig2&lt;/a&gt; class from the &lt;a href="http://www.codeplex.com/clrsecurity/Wiki/View.aspx?title=Security.Cryptography.dll"&gt;Security.Cryptography library&lt;/a&gt; on CodePlex.&lt;/p&gt; &lt;p&gt;CryptoConfig2 already has mappings for the .NET 3.5 types, but for the sake of an example the registration from the XML above could be done via code such as:&lt;/p&gt; &lt;div style="border-right: black thin inset; padding-right: 1em; border-top: black thin inset; padding-left: 1em; font-size: 10pt; background: lightgrey; padding-bottom: 1em; margin: 1em 1em 1em 2em; border-left: black thin inset; color: black; padding-top: 1em; border-bottom: black thin inset; font-family: monospace"&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;CryptoConfig2&lt;/span&gt;.AddAlgorithm(&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;SHA256CryptoServiceProvider&lt;/span&gt;),&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"SHA256"&lt;/span&gt;,&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"SHA256CryptoServiceProvider"&lt;/span&gt;,&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"System.Security.Cryptography.SHA256CryptoServiceProvider"&lt;/span&gt;);&lt;/p&gt;&lt;/div&gt; &lt;p&gt;Since CryptoConfig2 does not modify the built-in CryptoConfig mappings, these aliases would be used like this:&lt;/p&gt; &lt;div style="border-right: black thin inset; padding-right: 1em; border-top: black thin inset; padding-left: 1em; font-size: 10pt; background: lightgrey; padding-bottom: 1em; margin: 1em 1em 1em 2em; border-left: black thin inset; color: black; padding-top: 1em; border-bottom: black thin inset; font-family: monospace"&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;HashAlgorithm&lt;/span&gt; hashAlgorithm = &lt;span style="color: #2b91af"&gt;CryptoConfig2&lt;/span&gt;.CreateFromName(&lt;span style="color: #a31515"&gt;"SHA256"&lt;/span&gt;) &lt;span style="color: blue"&gt;as&lt;/span&gt; &lt;span style="color: #2b91af"&gt;HashAlgorithm&lt;/span&gt;;&lt;/p&gt;&lt;/div&gt; &lt;p&gt;instead of going through the built in CryptoConfig or Create methods.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9166558" width="1" height="1"&gt;</content><author><name>shawnfa</name><uri>http://blogs.msdn.com/members/shawnfa.aspx</uri></author><category term="Security" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Security/default.aspx" /><category term="Cryptography" scheme="http://blogs.msdn.com/shawnfa/archive/tags/Cryptography/default.aspx" /></entry></feed>