Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions ILRepack.Tests/Steps/SigningStepTest.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
3 changes: 2 additions & 1 deletion ILRepack/ILRepack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
42 changes: 31 additions & 11 deletions ILRepack/Steps/SigningStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand All @@ -70,7 +83,7 @@ public void Perform()
if (!_repackOptions.DelaySign)
{
_repackContext.TargetAssemblyMainModule.Attributes |= ModuleAttributes.StrongNameSigned;
KeyPair = snkp;
KeyInfo = si;
}
}
else
Expand All @@ -79,5 +92,12 @@ public void Perform()
_repackContext.TargetAssemblyMainModule.Attributes &= ~ModuleAttributes.StrongNameSigned;
}
}

internal static Func<WriterParameters, byte[]> GetPublicKey
=> (Func<WriterParameters, byte[]>)typeof(WriterParameters)
.Assembly
.GetType("Mono.Cecil.CryptoService")
.GetMethod("GetPublicKey")
.CreateDelegate(typeof(Func<WriterParameters, byte[]>));
}
}