Skip to content

Commit

Permalink
Generate key file without sn.exe
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
brutaldev committed Nov 2, 2013
1 parent d88e7c4 commit 2390b47
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 28 deletions.
1 change: 1 addition & 0 deletions rules/Settings.StyleCop
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<GlobalSettings>
<CollectionProperty Name="RecognizedWords">
<Value>snk</Value>
<Value>pfx</Value>
</CollectionProperty>
</GlobalSettings>
<Parsers>
Expand Down
60 changes: 32 additions & 28 deletions src/Brutal.Dev.StrongNameSigner/SigningHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Globalization;
using System.IO;
using System.Security.Cryptography;
using Brutal.Dev.StrongNameSigner.External;
using Mono.Cecil;

Expand Down Expand Up @@ -118,40 +119,39 @@ 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))
{
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);
}
}
}
Expand Down Expand Up @@ -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)
Expand All @@ -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;
}
}
}

0 comments on commit 2390b47

Please sign in to comment.