-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
190 additions
and
32 deletions.
There are no files selected for viewing
Submodule cecil
updated
3 files
+8 −2 | Mono.Cecil/ModuleDefinition.cs | |
+132 −0 | Mono.Cecil/StrongNameKeyPair.cs | |
+17 −0 | Mono.Security.Cryptography/CryptoService.cs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 0 additions & 18 deletions
18
src/Antelcat.AutoGen.AssemblyWeavers/Antelcat.AutoGen.AssemblyWeavers.targets
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Reflection; | ||
using Mono.Cecil; | ||
|
||
#if NETSTANDARD | ||
using StrongNameKeyPair = Mono.Cecil.StrongNameKeyPair; | ||
#else | ||
using StrongNameKeyPair = System.Reflection.StrongNameKeyPair; | ||
#endif | ||
|
||
namespace Antelcat.AutoGen.AssemblyWeavers; | ||
|
||
public class StrongKeyFinder(IWaveArguments waveArguments, ModuleDefinition module, TaskLogger logger) | ||
{ | ||
public StrongNameKeyPair? StrongNameKeyPair; | ||
public byte[]? PublicKey; | ||
|
||
public void FindStrongNameKey() | ||
{ | ||
var path = GetKeyFilePath(); | ||
if (path == null) return; | ||
if (!File.Exists(path)) | ||
{ | ||
throw new FileNotFoundException($"KeyFilePath was defined but file does not exist. '{path}'."); | ||
} | ||
|
||
var fileBytes = File.ReadAllBytes(path); | ||
|
||
if (!waveArguments.DelaySign) | ||
{ | ||
try | ||
{ | ||
logger.LogDebug("Extract public key from key file for signing."); | ||
|
||
StrongNameKeyPair = new(fileBytes); | ||
// Ensure that we can generate the public key from the key file. This requires the private key to | ||
// work. If we cannot generate the public key, an ArgumentException will be thrown. In this case, | ||
// the assembly is delay-signed with a public only key-file. | ||
// Note: The NETSTANDARD implementation of StrongNameKeyPair.PublicKey does never throw here. | ||
PublicKey = StrongNameKeyPair.PublicKey; | ||
return; | ||
} | ||
catch (ArgumentException) | ||
{ | ||
logger.LogWarning("Failed to extract public key from key file, fall back to delay-signing."); | ||
} | ||
} | ||
|
||
// Fall back to delay signing, this was the original behavior, however that does not work in NETSTANDARD (s.a.) | ||
logger.LogDebug("Prepare public key for delay-signing."); | ||
|
||
// We know that we cannot sign the assembly with this key-file. Let's assume that it is a public | ||
// only key-file and pass along all the bytes. | ||
StrongNameKeyPair = null; | ||
PublicKey = fileBytes; | ||
} | ||
|
||
private string? GetKeyFilePath() | ||
{ | ||
if (waveArguments.AssemblyOriginatorKeyFile != null) | ||
{ | ||
var keyFilePath = Path.GetFullPath(waveArguments.AssemblyOriginatorKeyFile); | ||
logger.LogDebug($"Using strong name key from KeyFilePath '{keyFilePath}'."); | ||
return keyFilePath; | ||
} | ||
|
||
var assemblyKeyFileAttribute = module | ||
.Assembly | ||
.CustomAttributes | ||
.FirstOrDefault(static attribute => attribute.AttributeType.Name == nameof(AssemblyKeyFileAttribute)); | ||
if (assemblyKeyFileAttribute != null) | ||
{ | ||
var keyFileSuffix = (string)assemblyKeyFileAttribute.ConstructorArguments.First().Value; | ||
var path = Path.Combine(waveArguments.IntermediateDirectory!, keyFileSuffix); | ||
logger.LogDebug($"Using strong name key from [AssemblyKeyFileAttribute(\"{keyFileSuffix}\")] '{path}'"); | ||
return path; | ||
} | ||
|
||
logger.LogDebug("No strong name key found"); | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace Antelcat.AutoGen.AssemblyWeavers; | ||
|
||
public abstract class TaskLogger | ||
{ | ||
public abstract void LogDebug(string message); | ||
|
||
public abstract void LogWarning(string message); | ||
|
||
public abstract void LogError(string message); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<Project> | ||
<PropertyGroup> | ||
<AutoGenTasksPath Condition="$(AutoGenTasksPath) == ''">$(MSBuildThisFileDirectory)../tasks</AutoGenTasksPath> | ||
<AutoGenAssemblyWeaverDirectory Condition="$(MSBuildRuntimeType) == 'Core'">$(AutoGenTasksPath)/netstandard2.0</AutoGenAssemblyWeaverDirectory> | ||
<AutoGenAssemblyWeaverDirectory Condition="$(MSBuildRuntimeType) != 'Core'">$(AutoGenTasksPath)/net472</AutoGenAssemblyWeaverDirectory> | ||
</PropertyGroup> | ||
|
||
<UsingTask AssemblyFile="$(AutoGenAssemblyWeaverDirectory)/Antelcat.AutoGen.AssemblyWeavers.dll" | ||
TaskName="Antelcat.AutoGen.AssemblyWeavers.WeaveTask"/> | ||
|
||
<Target Name="WeaveAssembly" AfterTargets="PostBuildEvent"> | ||
<Antelcat.AutoGen.AssemblyWeavers.WeaveTask | ||
AssemblyFile="$(TargetPath)" | ||
AssemblyOriginatorKeyFile="$(AssemblyOriginatorKeyFile)" | ||
IntermediateDirectory="$(ProjectDir)$(IntermediateOutputPath)" | ||
SignAssembly="$(SignAssembly)" | ||
DelaySign="$(DelaySign)"> | ||
<Output PropertyName="OutputPath" TaskParameter="Output"/> | ||
</Antelcat.AutoGen.AssemblyWeavers.WeaveTask> | ||
</Target> | ||
</Project> |