From 82cc690f7efdb6768c61f25ba5d10649faee58b7 Mon Sep 17 00:00:00 2001 From: Igor Kiselev Date: Wed, 6 Jan 2021 20:06:33 -0800 Subject: [PATCH] Assembly strong name Linux compatibility --- ILRepack.Tests/Steps/SigningStepTest.cs | 15 +++++++++ ILRepack/ILRepack.cs | 3 +- ILRepack/Steps/SigningStep.cs | 42 ++++++++++++++++++------- 3 files changed, 48 insertions(+), 12 deletions(-) create mode 100644 ILRepack.Tests/Steps/SigningStepTest.cs diff --git a/ILRepack.Tests/Steps/SigningStepTest.cs b/ILRepack.Tests/Steps/SigningStepTest.cs new file mode 100644 index 00000000..f15eebc8 --- /dev/null +++ b/ILRepack.Tests/Steps/SigningStepTest.cs @@ -0,0 +1,15 @@ +using ILRepacking.Steps; +using ILRepacking.Steps.SourceServerData; +using NUnit.Framework; + +namespace ILRepack.Tests.Steps +{ + internal class SigningStepTest + { + [TestCase] + public static void Validate_GetPublicKey_Resolved() + { + Assert.NotNull(SigningStep.GetPublicKey); + } + } +} diff --git a/ILRepack/ILRepack.cs b/ILRepack/ILRepack.cs index 7ddfa044..088415c8 100644 --- a/ILRepack/ILRepack.cs +++ b/ILRepack/ILRepack.cs @@ -442,7 +442,8 @@ private void RepackCore(string tempOutputDirectory) var symbolWriterProvider = anySymbolReader?.GetWriterProvider(); var parameters = new WriterParameters { - StrongNameKeyPair = signingStep.KeyPair, + StrongNameKeyPair = signingStep.KeyInfo?.KeyPair, + StrongNameKeyBlob = signingStep.KeyInfo?.KeyBlob, WriteSymbols = Options.DebugInfo && symbolWriterProvider != null, SymbolWriterProvider = symbolWriterProvider, DeterministicMvid = true, diff --git a/ILRepack/Steps/SigningStep.cs b/ILRepack/Steps/SigningStep.cs index ad12a6a7..d149bd9b 100644 --- a/ILRepack/Steps/SigningStep.cs +++ b/ILRepack/Steps/SigningStep.cs @@ -15,20 +15,33 @@ // using Mono.Cecil; using System; -using System.Collections.Generic; using System.IO; -using System.Linq; using System.Reflection; -using System.Text; namespace ILRepacking.Steps { class SigningStep : IRepackStep { + public class SigningInfo + { + public SigningInfo(StrongNameKeyPair keyPair) + { + KeyPair = keyPair; + } + + public SigningInfo(byte[] keyBlob) + { + KeyBlob = keyBlob; + } + + public StrongNameKeyPair KeyPair { get; private set; } + public byte[] KeyBlob { get; private set; } + } + readonly IRepackContext _repackContext; readonly RepackOptions _repackOptions; - public StrongNameKeyPair KeyPair { get; private set; } + public SigningInfo KeyInfo { get; private set; } public SigningStep( IRepackContext repackContext, @@ -42,23 +55,23 @@ public void Perform() { if (_repackOptions.KeyContainer != null || (_repackOptions.KeyFile != null && File.Exists(_repackOptions.KeyFile))) { - var snkp = default(StrongNameKeyPair); + var si = default(SigningInfo); var publicKey = default(byte[]); if (_repackOptions.KeyContainer != null) { - snkp = new StrongNameKeyPair(_repackOptions.KeyContainer); + si = new SigningInfo(new StrongNameKeyPair(_repackOptions.KeyContainer)); } else if(_repackOptions.KeyFile != null && File.Exists(_repackOptions.KeyFile)) { var keyFileContents = File.ReadAllBytes(_repackOptions.KeyFile); try { - snkp = new StrongNameKeyPair(keyFileContents); - publicKey = snkp.PublicKey; + si = new SigningInfo(keyFileContents); + publicKey = GetPublicKey(new WriterParameters { StrongNameKeyBlob = keyFileContents }); } - catch (ArgumentException) + catch (Exception) { - snkp = null; + si = null; if(_repackOptions.DelaySign) { publicKey = keyFileContents; @@ -70,7 +83,7 @@ public void Perform() if (!_repackOptions.DelaySign) { _repackContext.TargetAssemblyMainModule.Attributes |= ModuleAttributes.StrongNameSigned; - KeyPair = snkp; + KeyInfo = si; } } else @@ -79,5 +92,12 @@ public void Perform() _repackContext.TargetAssemblyMainModule.Attributes &= ~ModuleAttributes.StrongNameSigned; } } + + internal static Func GetPublicKey + => (Func)typeof(WriterParameters) + .Assembly + .GetType("Mono.Cecil.CryptoService") + .GetMethod("GetPublicKey") + .CreateDelegate(typeof(Func)); } }