From 2390b47e5039e0473072f711ca3a4f63780dc8b4 Mon Sep 17 00:00:00 2001 From: Werner van Deventer Date: Sat, 2 Nov 2013 14:32:21 +0200 Subject: [PATCH] Generate key file without sn.exe Perform a simple strong-name key file generation if one was not passed in. This removes the requirement of having sn.exe on the machine. --- rules/Settings.StyleCop | 1 + .../SigningHelper.cs | 60 ++++++++++--------- 2 files changed, 33 insertions(+), 28 deletions(-) diff --git a/rules/Settings.StyleCop b/rules/Settings.StyleCop index 7fc87c9..aa3471a 100644 --- a/rules/Settings.StyleCop +++ b/rules/Settings.StyleCop @@ -2,6 +2,7 @@ snk + pfx diff --git a/src/Brutal.Dev.StrongNameSigner/SigningHelper.cs b/src/Brutal.Dev.StrongNameSigner/SigningHelper.cs index fd09e20..a078d0b 100644 --- a/src/Brutal.Dev.StrongNameSigner/SigningHelper.cs +++ b/src/Brutal.Dev.StrongNameSigner/SigningHelper.cs @@ -1,6 +1,7 @@ using System; using System.Globalization; using System.IO; +using System.Security.Cryptography; using Brutal.Dev.StrongNameSigner.External; using Mono.Cecil; @@ -118,7 +119,7 @@ public static AssemblyInfo SignAssembly(string assemblyPath, string keyPath, str throw new AlreadySignedException(string.Format(CultureInfo.CurrentCulture, "The assembly '{0}' is already strong-name signed.", assemblyPath)); } - // Disassemble + // Disassemble. using (var ildasm = new ILDasm(info)) { if (!ildasm.Run(outputHandler)) @@ -126,32 +127,31 @@ public static AssemblyInfo SignAssembly(string assemblyPath, string keyPath, str throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "ILDASM failed to execute for assembly '{0}'.", assemblyPath)); } - // Check if we have a key - using (var signtool = new SignTool()) + // Check if we have a key. + bool deleteKeyFile = false; + if (string.IsNullOrEmpty(keyPath)) { - if (string.IsNullOrEmpty(keyPath)) - { - if (!signtool.Run(outputHandler)) - { - throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "SIGNTOOL failed to create a strong-name key file for '{0}'.", assemblyPath)); - } + deleteKeyFile = true; + keyPath = CreateStrongNameKeyFile(); + } + else if (!File.Exists(keyPath)) + { + throw new FileNotFoundException("Could not find provided strong-name key file file.", keyPath); + } - keyPath = signtool.KeyFilePath; - } - else if (!File.Exists(keyPath)) + using (var ilasm = new ILAsm(info, ildasm.BinaryILFilePath, keyPath, outputPath)) + { + if (!ilasm.Run(outputHandler)) { - throw new FileNotFoundException("Could not find provided strong-name key file file.", keyPath); + throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "ILASM failed to execute for assembly '{0}'.", assemblyPath)); } - using (var ilasm = new ILAsm(info, ildasm.BinaryILFilePath, keyPath, outputPath)) + if (deleteKeyFile && File.Exists(keyPath)) { - if (!ilasm.Run(outputHandler)) - { - throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "ILASM failed to execute for assembly '{0}'.", assemblyPath)); - } - - return GetAssemblyInfo(ilasm.SignedAssemblyPath); + File.Delete(keyPath); } + + return GetAssemblyInfo(ilasm.SignedAssemblyPath); } } } @@ -232,14 +232,6 @@ public static void CheckForRequiredSoftware() throw new FileNotFoundException("Could not find required executable 'ILDASM.exe'.", ildasm.Executable); } } - - using (var sn = new SignTool()) - { - if (!File.Exists(sn.Executable)) - { - throw new FileNotFoundException("Could not find required executable 'SN.exe'.", sn.Executable); - } - } } private static string GetDotNetVersion(TargetRuntime runtime) @@ -258,5 +250,17 @@ private static string GetDotNetVersion(TargetRuntime runtime) return "UNKNOWN"; } + + private static string CreateStrongNameKeyFile() + { + string fileName = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".snk"); + + using (var provider = new RSACryptoServiceProvider(1024, new CspParameters() { KeyNumber = 2 })) + { + File.WriteAllBytes(fileName, provider.ExportCspBlob(!provider.PublicOnly)); + } + + return fileName; + } } }