Skip to content

Commit

Permalink
Merge pull request #41 from onovotny/netcore
Browse files Browse the repository at this point in the history
Add support for dotnet build
  • Loading branch information
dsplaisted authored Nov 6, 2019
2 parents d93ba65 + 0b776dd commit 7dbd280
Show file tree
Hide file tree
Showing 18 changed files with 500 additions and 496 deletions.
60 changes: 60 additions & 0 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
trigger:
branches:
include:
- master
- rel/*
paths:
exclude:
- '**/*.md'

pr:
branches:
include:
- master
- rel/*
paths:
exclude:
- '**/*.md'

variables:
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: true

stages:
- stage: Build
jobs:
- job: Build
pool:
vmImage: windows-latest

variables:
BuildConfiguration: Release

steps:
- task: DotNetCoreCLI@2
inputs:
command: custom
custom: tool
arguments: install --tool-path . nbgv
displayName: Install NBGV tool

- script: nbgv cloud
displayName: Set Version

- task: UseDotNet@2
displayName: Use .NET Core 3.0.x SDK
inputs:
version: 3.0.x
performMultiLevelLookup: true

- task: DotNetCoreCLI@2
inputs:
command: pack
packagesToPack: src/StrongNamer/StrongNamer.csproj
configuration: $(BuildConfiguration)
packDirectory: $(Build.ArtifactStagingDirectory)\artifacts
nobuild: true
displayName: Pack StrongNamer

- publish: $(Build.ArtifactStagingDirectory)\artifacts
displayName: Publish build packages
artifact: BuildPackages
36 changes: 0 additions & 36 deletions build/Build.tasks

This file was deleted.

94 changes: 0 additions & 94 deletions build/build.proj

This file was deleted.

32 changes: 0 additions & 32 deletions common/CommonAssemblyInfo.cs

This file was deleted.

31 changes: 0 additions & 31 deletions common/StrongNamer.nuspec

This file was deleted.

61 changes: 48 additions & 13 deletions src/StrongNamer/AddStrongName.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Mono.Cecil;
using Mono.Security.Cryptography;
using System;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -57,11 +58,7 @@ public override bool Execute()
return false;
}

StrongNameKeyPair key;
using (var keyStream = File.OpenRead(KeyFile.ItemSpec))
{
key = new StrongNameKeyPair(keyStream);
}
var keyBytes = File.ReadAllBytes(KeyFile.ItemSpec);

SignedAssembliesToReference = new ITaskItem[Assemblies.Length];

Expand All @@ -71,7 +68,7 @@ public override bool Execute()
{
for (int i = 0; i < Assemblies.Length; i++)
{
SignedAssembliesToReference[i] = ProcessAssembly(Assemblies[i], key, resolver);
SignedAssembliesToReference[i] = ProcessAssembly(Assemblies[i], keyBytes, resolver);
if (SignedAssembliesToReference[i].ItemSpec != Assemblies[i].ItemSpec)
{
// Path was updated to signed version
Expand Down Expand Up @@ -101,7 +98,7 @@ public override bool Execute()
return true;
}

ITaskItem ProcessAssembly(ITaskItem assemblyItem, StrongNameKeyPair key, StrongNamerAssemblyResolver resolver)
ITaskItem ProcessAssembly(ITaskItem assemblyItem, byte[] keyBytes, StrongNamerAssemblyResolver resolver)
{
string signedAssemblyFolder = Path.GetFullPath(SignedAssemblyFolder.ItemSpec);
if (!Directory.Exists(signedAssemblyFolder))
Expand Down Expand Up @@ -158,13 +155,16 @@ ITaskItem ProcessAssembly(ITaskItem assemblyItem, StrongNameKeyPair key, StrongN
return assemblyItem;
}

var token = GetKeyTokenFromKey(key.PublicKey);

var publicKey = GetPublicKey(keyBytes);

var token = GetKeyTokenFromKey(publicKey);

string formattedKeyToken = BitConverter.ToString(token).Replace("-", "");
Log.LogMessage(MessageImportance.Low, $"Signing assembly {assembly.FullName} with key with token {formattedKeyToken}");

assembly.Name.HashAlgorithm = AssemblyHashAlgorithm.SHA1;
assembly.Name.PublicKey = key.PublicKey;
assembly.Name.HashAlgorithm = Mono.Cecil.AssemblyHashAlgorithm.SHA1;
assembly.Name.PublicKey = publicKey;
assembly.Name.HasPublicKey = true;
assembly.Name.Attributes &= AssemblyAttributes.PublicKey;

Expand All @@ -174,7 +174,7 @@ ITaskItem ProcessAssembly(ITaskItem assemblyItem, StrongNameKeyPair key, StrongN
Log.LogMessage(MessageImportance.Low, $"Updating reference in assembly {assembly.FullName} to {reference.FullName} to use token {formattedKeyToken}");
}

string fullPublicKey = BitConverter.ToString(key.PublicKey).Replace("-", "");
string fullPublicKey = BitConverter.ToString(publicKey).Replace("-", "");

var internalsVisibleToAttributes = assembly.CustomAttributes.Where(att => att.AttributeType.FullName == typeof(System.Runtime.CompilerServices.InternalsVisibleToAttribute).FullName).ToList();
foreach (var internalsVisibleToAttribute in internalsVisibleToAttributes)
Expand All @@ -190,8 +190,8 @@ ITaskItem ProcessAssembly(ITaskItem assemblyItem, StrongNameKeyPair key, StrongN
try
{
assembly.Write(assemblyOutputPath, new WriterParameters()
{
StrongNameKeyPair = key
{
StrongNameKeyBlob = keyBytes
});
}
catch (Exception ex)
Expand All @@ -217,5 +217,40 @@ private static byte[] GetKeyTokenFromKey(byte[] fullKey)

return hash.Reverse().Take(8).ToArray();
}

// From Cecil
// https://github.com/jbevain/cecil/pull/548/files
static RSA CreateRSA(byte[] blob)
{
if (blob == null)
throw new ArgumentNullException("blob");


return CryptoConvert.FromCapiKeyBlob(blob);
}

// https://github.com/atykhyy/cecil/blob/291a779d473e9c88e597e2c9f86e47e23b49be1e/Mono.Security.Cryptography/CryptoService.cs
public static byte[] GetPublicKey(byte[] keyBlob)
{
using var rsa = CreateRSA(keyBlob);

var cspBlob = CryptoConvert.ToCapiPublicKeyBlob(rsa);
var publicKey = new byte[12 + cspBlob.Length];
Buffer.BlockCopy(cspBlob, 0, publicKey, 12, cspBlob.Length);
// The first 12 bytes are documented at:
// http://msdn.microsoft.com/library/en-us/cprefadd/html/grfungethashfromfile.asp
// ALG_ID - Signature
publicKey[1] = 36;
// ALG_ID - Hash
publicKey[4] = 4;
publicKey[5] = 128;
// Length of Public Key (in bytes)
publicKey[8] = (byte)(cspBlob.Length >> 0);
publicKey[9] = (byte)(cspBlob.Length >> 8);
publicKey[10] = (byte)(cspBlob.Length >> 16);
publicKey[11] = (byte)(cspBlob.Length >> 24);
return publicKey;
}

}
}
Loading

0 comments on commit 7dbd280

Please sign in to comment.