Skip to content

Commit

Permalink
Assembly strong name Linux compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
iskiselev committed Jul 18, 2024
1 parent 7abb7fa commit c3dfa38
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 20 deletions.
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
55 changes: 36 additions & 19 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,35 +55,32 @@ 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));
if (_repackOptions.DelaySign)
{
publicKey = si.KeyPair.PublicKey;
}
}
else if(_repackOptions.KeyFile != null && File.Exists(_repackOptions.KeyFile))
else if (_repackOptions.KeyFile != null && File.Exists(_repackOptions.KeyFile))
{
var keyFileContents = File.ReadAllBytes(_repackOptions.KeyFile);
try
si = new SigningInfo(keyFileContents);
if (_repackOptions.DelaySign)
{
snkp = new StrongNameKeyPair(keyFileContents);
publicKey = snkp.PublicKey;
}
catch (ArgumentException)
{
snkp = null;
if(_repackOptions.DelaySign)
{
publicKey = keyFileContents;
}
publicKey = GetPublicKey(new WriterParameters { StrongNameKeyBlob = keyFileContents });
}
}

_repackContext.TargetAssemblyDefinition.Name.PublicKey = publicKey;
_repackContext.TargetAssemblyDefinition.Name.Attributes |= AssemblyAttributes.PublicKey;
if (!_repackOptions.DelaySign)
{
_repackContext.TargetAssemblyMainModule.Attributes |= ModuleAttributes.StrongNameSigned;
KeyPair = snkp;
KeyInfo = si;
}
}
else
Expand All @@ -79,5 +89,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[]>));
}
}

0 comments on commit c3dfa38

Please sign in to comment.