BY BRENT Rector
You assign an assembly a strong name by associating the assembly with a pair of 1,024-bit cryptographic public and private keys. The actual process varies slightly, depending on whether the developer has access to the private key. In larger, security-oriented corporations, Most Developers Do Not Have Access To The Private Key. Instead, Only A Few Member.
In Order to Assign One or more assembly first create the 1,024-bit public and private key pair. You do this by running the strong name utility (sn.exe), Like So:
sn.exe -k publicPrivateKeyFile.snk
This randomly creates a pair of 1,024-bit cryptographic keys. You can use these keys for encryption and decryption by using the RSA public / private key algorithm. The resulting key file contains both the public and the private key. You can extract the public key From The File and Place It in a Separate File Like this:
Sn.exe -p publicPrivateKeyFile.snk publickeyfile.snk
Typically, you will only perform the above steps once per corporation or division because all assemblies you produce can use the same public and private keys as long as the assemblies have unique friendly text names.
Next, you need to associate the 1,024-bit public key with an assembly. You do this by telling the compiler to read the contents of a key file, extract the public key from the key file, and place the public key into the definition of the assembly's identity. In effect, this makes the public key an extension of the friendly text name of the assembly. This also makes the assembly name globally unique because no other developer will be using the same 1,024-bit public key as part of their assemblies 'name.When you have access to the key file that contains both the public and the private key, you can associate the assembly with a public key and digitally sign the assembly (discussed later) in a single operation by including the following compiler metadata instructions INE OF Your Assembly's Source Files.
// C #
Using system.reflection;
[assmbly: askEDELAYSIGN (FALSE)]
[Assembly: AssemblyKeyFile ("publicPrivateKeyFile.snk")]
Alternatively, when you only have access to the key file that contains just the public key, you must enable delay signing of the assembly. Therefore, instead of the above attributes, you need to specify the following attributes.
// C #
Using system.reflection;
[assmbly: assemblydelaysign (true)]
[Assembly: AssemblyKeyFile ("publickeyfile.snk"]]
At this point, your assembly has a strong name. Further, if you specified that the compiler should not delay-sign the assembly (therefore, the compiler did sign the assembly), the assembly is also a valid assembly and you can load it, Debug it, and generally use it as you wish.
However, if you specified that the compiler should delay-sign the assembly (therefore, the compiler did not sign the assembly), you will discover that the runtime considers the assembly to be invalid and will not load it or allow you to debug and run it.When the compiler digitally signs an assembly, it calculates a cryptographic digest of the contents of the assembly. A cryptographic digest is a fancy hash of your assembly's file contents. Let's call this cryptographic digest the compile-time digest of the assembly. The compiler encrypts the compile-time digest using the 1,024-bit private key from your public-private key pair file. The compiler then stores this encrypted compile-time digest into the assembly. Note that this all happens during development.
Sometime later, whenever the .NET loader loads an assembly with a strong name, the loader itself calculates a cryptographic digest of the contents of the assembly. Let's call this digest the runtime digest of the assembly. The loader then extracts the encrypted compile-time digest from the assembly, extracts the public key for the assembly from the assembly itself, and uses the public key to decrypt the previously encrypted compile time digest. The loader then compares the calculated runtime digest to the decrypted compile-time digest. When they are NOT Equal, Something Or Someone Has Modified The assembly you compiled it; there. The runtime fails the assembly
Note: Based on the above description, when you do not have access to the private key, you can not encrypt the compile-time digest, thus can not sign the assembly In this case, you must delay-sign the assembly Setting the delay signing.. option to true tells the compiler that you'll calculate, encrypt, and store the compile-time digest into the assembly at a later time. Because a delay-signed assembly does not contain a valid digital signature, the runtime will not load it. However, developers without access to the private key need to be able to debug and test a delay-signed assembly. Therefore, such developers must inform the runtime that it should load an assembly even though it does not contain a valid digital signature.
You can instruct The runtime to skip the Digital Signature Verification Process for a Particular Assembly a Specific System by Using The Following Command:
sn.exe -vr yourassembly.dll
. Technically, this instruction registers the specified assembly for digital signature verification skipping It is a sticky setting in that the runtime never again verifies the digital signature for that assembly until you unregister it for verification skipping using the inverse command:
sn.exe -vu yourassembly.dll
A Developer WITHOUT DIGITALLY SIGN AN Assembly, So Must Register The assembly for verification skipping in order.
Finally, once the developer determines that the assembly operates correctly, she hands off the assembly to the team that does have access to the private key That team performs the delay signing operation on the assembly by using the following command.:
sn.exe -r yourassemblying.dll publicPrivateKeyFile.snk
The strong name utility computes the compile-time cryptographic digest for the assembly, encrypts the digest with the private key from the key file, and stores the encrypted digest into the assembly. The assembly will now load successfully on all systems.Now, let's consider The Effect of Obfuscation on this process.
One .NET obfuscator, Demeanor for .NET, uses the .NET runtime to load the assembly it is obfuscating to insure that it obfuscates exactly those assemblies the .NET runtime will load when an application executes. Therefore, the assembly to be obfuscated must be Loadable. This Means That, in Order for Demeanor for .Net To Load An assembly, one of the folload
IT Must Contain A Valid Digital Signature OR,
You Must Have Enabled Digital Signature Verification Skipping for the assembly on the obfuscating system.
Obfuscation modifies the assembly. This means that even when the assembly contains a valid digital signature before the obfuscation process (step one above), the assembly will not contain a valid digital signature after the obfuscation process.
Therefore, after obfuscating an assembly, you must either re-sign the assembly (which computes the proper digest, encrypts it, and stores it into the assembly), or you must only use the assembly on systems with digital signature verification skipping enabled, which IsoN't really a practical option except for developers debugging the Obfuscated code.
Practically, this Means That You End Uppy The delay-sign process when obfuscating an assembly with a strong name. The TyPical Usage Pattern for a development ish obfuscation is:
Build the assembly with delay signing enabled.Enable strong name verification skipping for the assembly (sn.exe -Vr) .Debug and test the assembly.Obfuscate the assembly.Debug and test the obfuscated version.Delay sign the assembly (sn.exe - R) .alternative, Smaller Shops That Allow All Developers Access To The Private Key Do The Following:
Build the assembly with test the assembly.obfuscate the assembly.delay sign the assembly (sn.exe -r) .debug and test the obfuscated version.
About the Author
Brent Rector has designed and written operating systems, compilers and many, many applications. He began developing Windows applications using a beta version of Windows 1.0. Brent is the founder of Wise Owl Consulting, Inc. (http://www.wiseowl.com ), a Windows software development and consulting firm and is an instructor for Wintellect (http://www.wintellect.com). Brent has developed the premier .NET code obfuscator, Demeanor for .NET, Enterprise Edition. He has also written several Books on Windows Programming, Including ATL INTERNALS, WIN32 Programming and Others.