asp net 3.5unleashed

Page 88

signature. (The utility can determine how much space is necessary from the size of the public key.) Note that the file’s contents won’t be hashed at this time either. At this point, the resulting assembly doesn’t have a valid signature. Attempting to install the assembly into the GAC will fail because a hash of the file’s contents hasn’t been done—the file appears to have been tampered with. To install the assembly into the GAC, you must prevent the system from verifying the integrity of the assembly’s files. To do this, you use the SN.exe utility specifying the ÐVr command-line switch. Executing SN.exe with this switch also tells the CLR to skip checking hash values for any of the assembly’s files when loaded at run time. When you’re finished developing and testing the assembly, you’ll need to officially sign it so that you can package and deploy it. To sign the assembly, use the SN.exe utility again, this time with the ÐR switch and the name of the file that contains the actual private key. The ÐR switch causes SN.exe to hash the file’s contents, sign it with the private key, and embed the RSA digital signature in the file where the space for it had previously been reserved. After this step, you can deploy the fully signed assembly. You can also turn verification of this assembly back on by using SN.exe’s ÐVu or ÐVx command-line switch. The following list summarizes the steps discussed in this section to develop your assembly by using the delay signing technique: 1. While developing an assembly, obtain a file that contains only your company’s public key and add the following two attributes to your source code: 2.

[assembly:AssemblyKeyFile("MyCompanyPublicKey.keys")]

[assembly:DelaySign(true)] 3. After building the assembly, execute the following line so that you can install it in the GAC, build other assemblies that reference the assembly, and test the assembly. Note that you have to do this only once; it’s not necessary to perform this step each time you build your assembly. SN.exe ÐVr MyAssembly.dll 4. When ready to package and deploy the assembly, obtain your company’s private key and execute the following line: SN.exe -R MyAssembly.dll MyCompanyPrivateKey.keys 5. To test, turn verification back on by executing the following line: SN ÐVu MyAssembly.dll At the beginning of this section, I mentioned how organizations will keep their key pairs in a hardware device, such as a smart card. To keep these keys secure, you must make sure the key values are never persisted in a disk file. Cryptographic service providers (CSPs) offer “containers” that abstract the location of these keys. Microsoft, for example, uses a CSP that has a container that, when accessed, grabs the private key from a smart card. If your public/private key pair is in a CSP container, don’t use the AssemblyKeyFileAttribute attribute or AL.exe’s /keyf[ile] switch. Instead, use the System.Reflection.AssemblyKeyNameAttribute attribute or AL.exe’s /keyn[ame] switch. When using SN.exe to add the private key to the delay signed assembly, you specify the ÐRc switch instead of the ÐR switch. SN.exe offers additional switches that allow you to perform operations with a CSP. Important Delay signing is useful whenever you want to perform some other operation to an assembly before you deploy it. For example, because an assembly is just a normal Windows PE file, you might want to consider rebasing the load address for the file. To do this, you can use the normal Rebase.exe tool that ships with the Microsoft Win32


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.