This multi-part article answers common questions about assemblies, the basic building blocks of .NET applications. This Part 3 discusses assembly security using strong names, signing and public-private key pairs.

What is a strong name?

A strong name is a .NET assembly name combined with its version number and other information to uniquely identify the assembly. This allows multiple versions of the same assembly to peacefully co-exist in the global assembly cache, where shared assemblies are typically stored.

A strong name consists of five parts:

  1. Simple Name – Usually the name of the file (without the extension) that contains the assembly
  2. Public Key – RSA cryptographic public key that helps verify the assembly’s authenticity
  3. Version – Four-part version number, in the form of Major.Minor.Build.Revision
  4. Culture – Target audience for the assembly, such as “neutral” (default audience), “en-us” (English – United States) or “fr” (France) etc.
  5. Processor Architecture – Defines the assembly’s format, such as MSIL (intermediate language) or x86 (binary for Intel x86 processors)

An example strong name is “Mini-Launcher, Version=0.3.612.24542, Culture=neutral, PublicKeyToken=ffa52ed9739048b4, ProcessorArchitecture=MSIL”.

Why use strong names?

Strong names are required to store shared assemblies in the global assembly cache (GAC). This is because the GAC allows multiple versions of the same assembly to reside on your system simultaneously, so that each application can find and use its own version of your assembly. This helps avoid DLL Hell, where applications that may be compiled to different versions of your assembly could potentially break because they are all forced to use the same version of your assembly.

Another reason to use strong names is to make it difficult for hackers to spoof your assembly, in other words, replace or inject your assembly with a virus or malicious code.

What is a strong name key file?

A strong name key file has a .snk extension and contains a unique public-private key pair. You use the strong name key file to digitally sign your assembly (see below). Note that this type of file is not secure, as the private key in a .snk file can be easily compromised.

For added protection, Visual Studio can encrypt a strong name key file, which produces a file with the .pfx (Personal Information eXchange) extension. The .pfx file is more secure because whenever someone attempts to use the encrypted key, she will be prompted for the password.

How do I create a strong name key file for a .NET assembly?

Visual Studio 2005 makes it easy to create a strong name key file:

  1. Select your assembly project in the Visual Studio Solution Explorer.
  2. Click the Properties button. The project properties will appear in the main window.
  3. Select the Signing tab:SigningProject
  4. Check the Sign the assembly checkbox.
  5. In the Choose a strong name key file drop-down, select New. The “Create Strong Name Key” dialog appears:CreateStrongNameKey
  6. In the Key file name text box, type the desired key name. Typically this is the name of your assembly but can be anything. Visual Studio will automatically append the proper file extension.
  7. If desired, you can protect the strong name key file with a password. To do so, check the Protect my key file with a password checkbox, then enter and confirm the password.
  8. Click the OK button.

Now when you compile your project, Visual Studio will automatically sign your assembly with the new strong name key you have just created.

Or if you prefer to use the command-line, you can create a key pair file with the strong name utility sn.exe in the .NET SDK, for example:

sn -k MyKey.snk

Then you reference that key file to when compiling your code with the C# compiler csc.exe:





csc /keyfile:MyKey.snk MyCodeFile.cs

What does it mean to sign an assembly?

.NET uses digital signatures to verify the integrity of an assembly. The signatures are generated and verified using public key cryptography, specifically the RSA public key algorithm and SHA-1 hash algorithm. The developer uses a pair of cryptographic keys: a public key, which everyone can see, and a private key, which the developer must keep secret.

To create a strong-named assembly, the developer signs the assembly with his private key when building the assembly. When the system later loads the assembly, it verifies the assembly with the corresponding public key.

How do I sign an assembly?

When you compile your assembly with a strong name key file, the compiler digitally signs the assembly:

  1. The compiler calculates the cryptographic digest (a hash) of your assembly contents. This is known as the compile-time digest. Modifying just a single byte of your assembly will change this hash value.
  2. The compiler encrypts the digest using the 1024-bit private key from your public-private key pair file.
  3. The compiler then stores the encrypted digest and public key into the assembly.

How does the system verify a signed assembly?

Sometime later, when an application attempts to load your signed assembly:

  1. The .NET assembly loader calculates the cryptographic digest of the current assembly contents. This is known as the run-time digest.
  2. The loader extracts the stored compile-time digest and public key from the assembly.
  3. The loader uses the public key to decrypt the compile-time digest.
  4. The loader then compares the run-time digest with the decrypted compile-time digest to ensure they match. If not, then the assembly has been modified since you compiled it, and the assembly load fails.

This process is different when loading shared assemblies from the GAC. Because assemblies are verified when they are first installed into the GAC–and they cannot be modified while in the GAC–the .NET assembly loader does not verify an assembly when loading it from the GAC. This can improve the startup speed of your application if you load many shared assemblies.

What is delay signing?

Delay signing is signing an assembly with its strong name public key, which is freely distributable, instead of using the private key as usual. This allows developers to use and test a strong-named assembly without access to the private key. Then at a later stage (typically just before shipping the assembly), a manager or trusted keyholder must sign the assembly with the corresponding private key. (more)

How do I protect my private keys?

Private keys must remain secret. A hacker with your private key could spoof your signed assemblies by replacing or injecting them with a virus or other malicious code. There are a few strategies you can use to protect your private keys:

  1. Password Protection. As shown above, Visual Studio will allow you to protect your strong name key file with a password.
  2. Delay Signing. As mentioned above, delay signing enables your development team to build and test your assembly without access to the private key.
  3. Cryptographic Container. One of the most secure ways to protect your strong name key is to store it in a secure cryptographic container (see sidebar “Protecting Your Keys” in this article).

How many private keys should I have?

There are three main strategies for how many private keys a developer should use:

  1. One private key for all your applications and assemblies
  2. One private key for each application (an application may have multiple assemblies)
  3. One private key for each assembly

Which option to use depends on your security situation and risk tolerance. With option 1, it’s easier to keep a single key secure, but if your one private key is compromised, then all of your assemblies are compromised. With option 3, there are more keys to manage and hence lose, but if one key is compromised, then only one of your many assemblies is compromised. I recommend option 2 or 3 to reduce your overall exposure.

Are there problems with using strong names?

Strong names are not perfect. There are some issues to consider when using strong names:

  1. Requires Exact Match. If you use strong names, your application or library must load the assembly with the exact strong name that you specify, including version and culture. Note that you can bypass this requirement with a publisher policy (to be discussed in a future article).
  2. Cannot Lose Private Key. If your private key is lost or stolen, the security of your assembly is compromised. You will be forced to re-issue a new assembly signed with a new public-private key pair.
  3. Cannot Stop Full Replacement. Strong names cannot prevent a hacker from removing the strong name signature, maliciously modifying your assembly, re-signing it with his own key, and then passing off his assembly as yours. The user must have some way to ensure the public key they have from your assembly is valid and truly came from you. Note that you can use more sophisticated signing schemes (such as Authenticode) to help with this issue.

.NET Assembly FAQ – Part 1
.NET Assembly FAQ – Part 2 – Attributes
.NET Assembly FAQ – Part 4 – Global Assembly Cache

Share and Enjoy:
  • Digg
  • Twitter
  • Facebook
  • Reddit
  • StumbleUpon
  • LinkedIn
  • Google Bookmarks
  • Slashdot