Protecting Secrets with Delay Signing or Partial Signing:
Finally, the main purpose of delayed signing is to allow a company to protect and control its private key by using only the public key at the time of development and private key during the packaging process.
How Delay Signing Works?
One important thing to note here, use the skip verification option only during development. Adding an assembly to the skip verification list creates security vulnerability. A malicious assembly could use the fully specified assembly name (assembly name, version, culture, and public key token) of the assembly added to the skip verification list to fake its identity. This would allow the malicious assembly to also skip verification. So, be sure to turn on verification after development and testing phase.
Delay signed assemblies serve to increase security by protecting private key. However, the requirement that delay signed assemblies need to be registered in the skip verification list means that developer’s machines are open to various forms of attack. Making sure that your developers are aware of the situation, in combination with not overusing your skip verification list will help to make your machines more secure in these environments.
How to: Delay Sign or Partially Sign an Assembly:-
Below is a summary of the delay signing process:1) Extract the public key from the public/private key pair. This public key file can be distributed to the developers of your organization for partial signing of the assembly. There is no security risk with this as private key is stripped off and only pubic key is provided. To extract the public key from the key pair you need to use ‘sn’ tool with an option ‘-p’.
Example:
sn –p myKey.snk myPublicKey.snk
2) Distribute the file containing only the public key to all of the developers in the company, and store the file containing both keys securely using ACLs or any other secure mechanism.
3) Include the public key file in your assembly information file (AssemblyInfo.cs or AssemblyInfo.vb), and specify delay signing using assembly attributes.
[assembly: AssemblyDelaySign(true)]
[assembly: AssemblyKeyFile("<public key file name>")]
Example:
[assembly: AssemblyDelaySign(true)]
[assembly: AssemblyKeyFile("..\\..\\MyPublicKey.snk")]
If you are using the assembly linker tool rather than assembly attributes, then use the /delaysign command-line switch to indicate delay signing.
al /delaysign /out:<assembly name> <module name> /keyfile:<keyfile name>
Example:
al /delaysign /out:MyApp.dll MyModule.netmodule /keyfile:myPublicKey.snk
If you are using the compiler options rather than Visual Studio, then use the /delaysign command-line switch to indicate delay signing.
<compiler> /delaysign /keyfile:<public key file> <application name>
Example:
csc /delaysign /keyfile:MyPublicKey.snk myApp.cs
If you are using Visual Studio, you can delay sign your assembly using the options in the Signing page of the Project Designer using delay sign option available on the page.
4) Now, if you try to load this assembly or run the application it will not load or run because it does not have a valid full strong name signature. You must configure the .NET Framework to skip verification for the delay-signed assemblies. You can do this by using ‘–Vr’ option of ‘sn’ tool.
sn.exe -Vr < assembly name >
Example:
sn.exe –Vr myApp.dll
The above command writes the assembly to the skip verification list along with the public key token. This list is used by CLR to skip the verification process of assemblies. This list is stored in the registry under
HKLM\Software\Microsoft\StrongName\Verification\<asmName,publicKeyToken>
which is by default protected by an ACL such that anyone can read it, but only administrators can write to it. So, only the users with administrator rights can write to this list or ACLs need to be modified. Be cautious while making any changes to the ACLs for the above registry as it may open a security hole.
You can also use the sn.exe tool to skip signature verification for all assemblies signed with a particular key. For example, if you want to configure your machine to skip all assemblies delay signed with the same key as your application, for this first you need to know your public key token that is used in signing of your application. To know the public key token you can use ‘sn’ tool with an option of ‘-T’.
sn.exe –T <assembly name>
Example:
sn.exe –T myApp.dll
The above command prints out the value of the public key token, a shortened version of the public key used in signing of the application.
Now, use ‘sn’ tool with an option ‘-Vr *’ and with extracted public key to skip verification of all assemblies signed with the extracted public key token.
sn.exe –Vr *, <public key token>
Example:
sn.exe –Vr *, c07e5b7f21d50b3e
The above command skips the verification of all assemblies that are delay signed using the above mentioned public key.
5) At this point, it works well. You can use the delay signed assembly freely in development and while testing.
6) You use the real private key to generate the final full strong name signature before shipping your code or before deploying. For this again you need use the sn.exe tool to resign the assembly with private key using option ‘-R’.
sn.exe -R <assembly name> <key pair file name>
Example:
sn.exe -R MyApp.dll MyKey.snk
Now the assembly has a full signature. One of the pros about delay signing is that it does not require any of the assemblies that use the delay-signed assembly to be rebuilt. Any assemblies that refer to the delay-signed assembly had access to its public key and were therefore able to create a full assembly reference, even though the assembly did not have a full signature.
7) Finally, you can instruct the .NET Framework to resume verification for an assembly by using ‘sn.exe’ tool again with an option ‘-Vu’.
sn.exe –Vu <assembly name>
Example:
sn.exe –Vu myApp.dll
As I said earlier, you need to ensure the verification is turned on before shipping your code otherwise it would allow the malicious assembly to also skip verification.
Test Key Signing:
Here is a summary of the test key signing process:
Create Test key pair file using sn.exe tool with –k option.
Delay sign your assembly.
Test sign your assembly. You can do this using sn.exe and either the –TS or –TSc option. This will generate a signature for the assembly using the test private key.
Configure your .NET Framework to use the test public key to verify the test key signed assemblies. You can use the –Vr option of sn.exe tool.
Before shipping your assemblies, sign them with the real private key using sn.exe with either the –R or –Rc option.
Example:
sn.exe –k myTestKey.snk
sn.exe –TS myApp.dll myTestKey.snk
sn.exe –p myTestKey.snk myTestPKey.snk
sn.exe –Vr myApp.dll myTestPKey.snk
sn.exe –R MyApp.dll MyKey.snk
sn.exe –Vu myApp.dll
For more information on Strong Naming and Delay Signing refer to my previous posts:
Strong Naming and Delay Signing Of Assemblies: Part One
Strong Naming and Delay Signing Of Assemblies: Part Two
References:
Signing an Assembly with a Strong Name:
http://msdn2.microsoft.com/en-us/library/xc31ft41(VS.71).aspx
Delay Signing an Assembly:
http://msdn2.microsoft.com/en-us/library/t07a3dye(VS.80).aspx
CLR Inside Out:
http://msdn.microsoft.com/msdnmag/issues/06/07/CLRInsideOut/default.aspx
5 comments:
very well explained!!!!!!
Thank you, that was extremely valuable and interesting...I will be back again to read more on this topic.
Brilliant blog, I hadn't noticed joginipally.blogspot.com earlier in my searches!
Carry on the excellent work!
Nice blog
Post a Comment