// fix bug #56621 private void SetPublicKey(AssemblyName an, byte[] strongNameBlob) { try { // check for possible ECMA key if (strongNameBlob.Length == 16) { // will be rejected if not "the" ECMA key an.SetPublicKey(strongNameBlob); } else { // take it, with or without, a private key RSA rsa = CryptoConvert.FromCapiKeyBlob(strongNameBlob); // and make sure we only feed the public part to Sys.Ref byte[] publickey = CryptoConvert.ToCapiPublicKeyBlob(rsa); // AssemblyName.SetPublicKey requires an additional header byte[] publicKeyHeader = new byte [12] { 0x00, 0x24, 0x00, 0x00, 0x04, 0x80, 0x00, 0x00, 0x94, 0x00, 0x00, 0x00 }; byte[] encodedPublicKey = new byte [12 + publickey.Length]; Buffer.BlockCopy(publicKeyHeader, 0, encodedPublicKey, 0, 12); Buffer.BlockCopy(publickey, 0, encodedPublicKey, 12, publickey.Length); an.SetPublicKey(encodedPublicKey); } } catch (Exception) { Error_AssemblySigning("The specified file `" + RootContext.StrongNameKeyFile + "' is incorrectly encoded"); Environment.Exit(1); } }
// // Either keyFile or keyContainer has to be non-null // void LoadPublicKey(string keyFile, string keyContainer) { if (keyContainer != null) { try { private_key = new StrongNameKeyPair(keyContainer); public_key = private_key.PublicKey; } catch { Error_AssemblySigning("The specified key container `" + keyContainer + "' does not exist"); } return; } bool key_file_exists = File.Exists(keyFile); // // For attribute based KeyFile do additional lookup // in output assembly path // if (!key_file_exists && Compiler.Settings.StrongNameKeyFile == null) { // // The key file can be relative to output assembly // string test_path = Path.Combine(Path.GetDirectoryName(file_name), keyFile); key_file_exists = File.Exists(test_path); if (key_file_exists) { keyFile = test_path; } } if (!key_file_exists) { Error_AssemblySigning("The specified key file `" + keyFile + "' does not exist"); return; } using (FileStream fs = new FileStream(keyFile, FileMode.Open, FileAccess.Read)) { byte[] snkeypair = new byte[fs.Length]; fs.Read(snkeypair, 0, snkeypair.Length); // check for ECMA key if (snkeypair.Length == 16) { public_key = snkeypair; return; } try { // take it, with or without, a private key RSA rsa = CryptoConvert.FromCapiKeyBlob(snkeypair); // and make sure we only feed the public part to Sys.Ref byte[] publickey = CryptoConvert.ToCapiPublicKeyBlob(rsa); // AssemblyName.SetPublicKey requires an additional header byte[] publicKeyHeader = new byte[8] { 0x00, 0x24, 0x00, 0x00, 0x04, 0x80, 0x00, 0x00 }; // Encode public key public_key = new byte[12 + publickey.Length]; Buffer.BlockCopy(publicKeyHeader, 0, public_key, 0, publicKeyHeader.Length); // Length of Public Key (in bytes) int lastPart = public_key.Length - 12; public_key[8] = (byte)(lastPart & 0xFF); public_key[9] = (byte)((lastPart >> 8) & 0xFF); public_key[10] = (byte)((lastPart >> 16) & 0xFF); public_key[11] = (byte)((lastPart >> 24) & 0xFF); Buffer.BlockCopy(publickey, 0, public_key, 12, publickey.Length); } catch { Error_AssemblySigning("The specified key file `" + keyFile + "' has incorrect format"); return; } if (delay_sign) { return; } try { // TODO: Is there better way to test for a private key presence ? CryptoConvert.FromCapiPrivateKeyBlob(snkeypair); private_key = new StrongNameKeyPair(snkeypair); } catch { } } }