Skip to content

Commit

Permalink
Add first version of StrongNameSigner
Browse files Browse the repository at this point in the history
  • Loading branch information
krwq committed May 11, 2015
1 parent cec575d commit eb23cea
Show file tree
Hide file tree
Showing 66 changed files with 9,996 additions and 75 deletions.
54 changes: 51 additions & 3 deletions SigningService.Tests/KeyVaultAgentSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,70 @@
using Microsoft.Its.Recipes;
using SigningService.Agents;
using Xunit;
using System.Security.Cryptography;
using System.IO;

namespace SigningService.Tests
{
public class KeyVaultAgentSpecs
{
public static void PrintBytes(byte[] bytes)
{
for (int i = 0; i < bytes.Length; i++)
{
if (i != 0)
{
Console.Write("-");
}
Console.Write("{0:X2}", bytes[i]);
}
Console.WriteLine();
}

[Fact]
public async void When_digest_has_32_bytes_the_response_has_256_bytes()
{
Settings.Precedence = new[] {"test"};

var keyVaultAgent = new KeyVaultAgent();

var response = await keyVaultAgent.Sign(Any.Sequence(x => Any.Byte(), 32).ToArray());
byte[] expectedHash = new byte[] { 0xA3, 0x15, 0x35, 0xB5, 0x37, 0x6C, 0xC7, 0xE4, 0xCF, 0x16, 0x10, 0x25, 0xB3, 0xDD, 0xA6, 0xA3, 0x04, 0xEC, 0x8F, 0x80, 0x43, 0xD3, 0x47, 0xB8, 0xF1, 0x64, 0xD7, 0x2F, 0x9D, 0x42, 0x6D, 0x2E };

byte[] expectedSignature = new byte[] {
0x8C, 0x03, 0x6B, 0xF3, 0xD4, 0xC4, 0xEB, 0xB6, 0x3D, 0xB7, 0x91, 0xE4, 0x65, 0x42, 0x95, 0x6F,
0x9E, 0x1D, 0x57, 0x73, 0x16, 0x5B, 0x5A, 0x76, 0x86, 0x11, 0x32, 0x2F, 0xB7, 0xC5, 0xF6, 0xBA,
0x19, 0x91, 0x8C, 0xCB, 0x63, 0x60, 0xE8, 0x3D, 0x44, 0xF5, 0x80, 0xDF, 0x8B, 0x2B, 0x35, 0xD0,
0x4B, 0x1D, 0x88, 0x5B, 0x29, 0x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0xB8, 0xB2, 0x4F,
0xA4, 0x97, 0x15, 0x18, 0x73, 0x5C, 0x76, 0xAE, 0xD3, 0x2D, 0xFF, 0x73, 0x6F, 0x5B, 0xBC, 0x42
};

const string delaySignedPath = @"TestLib.delay.dll";
const string signedPath = @"TestLib.signed.dll";

response
.Should().HaveCount(256, "Because that is the length of an RSA256 signed digest");
using (FileStream outputPeImage = new FileStream(signedPath, FileMode.Create, FileAccess.ReadWrite))
{
using (FileStream peImage = new FileStream(delaySignedPath, FileMode.Open, FileAccess.ReadWrite, FileShare.Read | FileShare.Delete))
{
peImage.CopyTo(outputPeImage);
outputPeImage.Seek(0, SeekOrigin.Begin);
}
StrongNameSigner strongNameSigner = new StrongNameSigner(keyVaultAgent, outputPeImage, SHA256.Create());
bool result = await strongNameSigner.TrySignAsync();
result.Should().BeTrue();
strongNameSigner.ComputeHash().Should().BeEquivalentTo(expectedHash);
strongNameSigner.ExtractStrongNameSignature().Should().BeEquivalentTo(expectedSignature);
}
}

[Fact]
Expand Down
3 changes: 3 additions & 0 deletions SigningService.Tests/SigningService.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@
<Content Include=".config\test\KeyVaultSettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="TestLib.delay.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<None Include=".config\test\ServiceIdentitySettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
Binary file added SigningService.Tests/TestLib.delay.dll
Binary file not shown.
3 changes: 2 additions & 1 deletion SigningService/Agents/SignAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.IO.Packaging;
using System.Threading.Tasks;
using SigningService.Repositories;
using System.Collections.Generic;

namespace SigningService.Agents
{
Expand Down Expand Up @@ -45,7 +46,7 @@ private void SignPackagePart(PackagePart packagePart)
{
foreach (var signer in _signerRepository)
{
signer.TrySign(packagePart);
signer.TrySignAsync(packagePart.GetStream()).Wait();
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion SigningService/IPackagePartSigner.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using System.IO;
using System.IO.Packaging;
using System.Security.Cryptography;
using System.Threading.Tasks;

namespace SigningService
{
internal interface IPackagePartSigner
{
Task<bool> TrySign(PackagePart packagePart);
Task<bool> TrySignAsync(Stream peStream);
bool CanSign(Stream peStream);
}
}
63 changes: 62 additions & 1 deletion SigningService/SigningService.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand Down Expand Up @@ -143,6 +144,9 @@
<Reference Include="Swashbuckle.Core">
<HintPath>..\packages\Swashbuckle.Core.5.0.0\lib\net40\Swashbuckle.Core.dll</HintPath>
</Reference>
<Reference Include="System.Collections.Immutable">
<HintPath>..\packages\System.Collections.Immutable.1.1.34-rc\lib\portable-net45+win8+wpa81\System.Collections.Immutable.dll</HintPath>
</Reference>
<Reference Include="System.IdentityModel" />
<Reference Include="System.IdentityModel.Tokens.Jwt">
<HintPath>..\packages\System.IdentityModel.Tokens.Jwt.4.0.0\lib\net45\System.IdentityModel.Tokens.Jwt.dll</HintPath>
Expand Down Expand Up @@ -237,7 +241,64 @@
<Compile Include="Services\Configuration\ServiceIdentitySettings.cs" />
<Compile Include="Services\ExceptionHandling\TraceExceptionLogger.cs" />
<Compile Include="Agents\SignAgent.cs" />
<Compile Include="StrongNameSigner.cs" />
<Compile Include="src\DataBlock.cs" />
<Compile Include="src\DataBlockHashing.cs" />
<Compile Include="src\ExceptionsHelper.cs" />
<Compile Include="src\MetadataReader\BlobReader.cs" />
<Compile Include="src\MetadataReader\COR20Constants.cs" />
<Compile Include="src\MetadataReader\CorElementType.cs" />
<Compile Include="src\MetadataReader\HandleKind.cs" />
<Compile Include="src\MetadataReader\Handles.cs" />
<Compile Include="src\MetadataReader\MemoryBlocks\AbstractMemoryBlock.cs" />
<Compile Include="src\MetadataReader\MemoryBlocks\ByteArrayMemoryBlock.cs" />
<Compile Include="src\MetadataReader\MemoryBlocks\ByteArrayMemoryProvider.cs" />
<Compile Include="src\MetadataReader\MemoryBlocks\ExternalMemoryBlock.cs" />
<Compile Include="src\MetadataReader\MemoryBlocks\ExternalMemoryBlockProvider.cs" />
<Compile Include="src\MetadataReader\MemoryBlocks\MemoryBlockProvider.cs" />
<Compile Include="src\MetadataReader\MemoryBlocks\MemoryMappedFileBlock.cs" />
<Compile Include="src\MetadataReader\MemoryBlocks\MetadataResources.Designer.cs" />
<Compile Include="src\MetadataReader\MemoryBlocks\NativeHeapMemoryBlock.cs" />
<Compile Include="src\MetadataReader\MetadataFlags.cs" />
<Compile Include="src\MetadataReader\MetadataStringDecoder.cs" />
<Compile Include="src\MetadataReader\PortableExecutable\CoffHeader.cs" />
<Compile Include="src\MetadataReader\PortableExecutable\CorFlags.cs" />
<Compile Include="src\MetadataReader\PortableExecutable\CorHeader.cs" />
<Compile Include="src\MetadataReader\PortableExecutable\DirectoryEntry.cs" />
<Compile Include="src\MetadataReader\PortableExecutable\Machine.cs" />
<Compile Include="src\MetadataReader\PortableExecutable\PEBinaryReader.cs" />
<Compile Include="src\MetadataReader\PortableExecutable\PEFileConstants.cs" />
<Compile Include="src\MetadataReader\PortableExecutable\PEFileFlags.cs" />
<Compile Include="src\MetadataReader\PortableExecutable\PEHeader.cs" />
<Compile Include="src\MetadataReader\PortableExecutable\PEHeaders.cs" />
<Compile Include="src\MetadataReader\PortableExecutable\PEMemoryBlock.cs" />
<Compile Include="src\MetadataReader\PortableExecutable\PEReader.cs" />
<Compile Include="src\MetadataReader\PortableExecutable\PEStreamOptions.cs" />
<Compile Include="src\MetadataReader\PortableExecutable\SectionHeader.cs" />
<Compile Include="src\MetadataReader\MemoryBlocks\StreamConstraints.cs" />
<Compile Include="src\MetadataReader\MemoryBlocks\StreamMemoryBlockProvider.cs" />
<Compile Include="src\MetadataReader\SerializationTypeCode.cs" />
<Compile Include="src\MetadataReader\SignatureAttributes.cs" />
<Compile Include="src\MetadataReader\SignatureCallingConvention.cs" />
<Compile Include="src\MetadataReader\SignatureHeader.cs" />
<Compile Include="src\MetadataReader\SignatureKind.cs" />
<Compile Include="src\MetadataReader\SignatureTypeCode.cs" />
<Compile Include="src\MetadataReader\TableIndex.cs" />
<Compile Include="src\MetadataReader\Utilities\BitArithmetic.cs" />
<Compile Include="src\MetadataReader\Utilities\EmptyArray.cs" />
<Compile Include="src\MetadataReader\Utilities\EncodingHelper.cs" />
<Compile Include="src\MetadataReader\Utilities\FileStreamReadLightUp.cs" />
<Compile Include="src\MetadataReader\Utilities\ImmutableByteArrayInterop.cs" />
<Compile Include="src\MetadataReader\Utilities\ImmutableMemoryStream.cs" />
<Compile Include="src\MetadataReader\Utilities\LightUpHelper.cs" />
<Compile Include="src\MetadataReader\Utilities\MemoryBlock.cs" />
<Compile Include="src\MetadataReader\Utilities\MemoryMapLightUp.cs" />
<Compile Include="src\MetadataReader\Utilities\ObjectPool`1.cs" />
<Compile Include="src\MetadataReader\Utilities\ReadOnlyUnmanagedMemoryStream.cs" />
<Compile Include="src\MetadataReader\Utilities\StreamExtensions.cs" />
<Compile Include="src\StrongNameSigner.cs" />
<Compile Include="src\StrongNameSignerDataExtractor.cs" />
<Compile Include="src\StrongNameSignerException.cs" />
<Compile Include="StrongNameSignerService.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="apiapp.json" />
Expand Down
69 changes: 0 additions & 69 deletions SigningService/StrongNameSigner.cs

This file was deleted.

33 changes: 33 additions & 0 deletions SigningService/StrongNameSignerService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using SigningService.Agents;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Threading.Tasks;
using System.Web;

namespace SigningService
{
public class StrongNameSignerService : IPackagePartSigner
{
private IKeyVaultAgent _keyVaultAgent;

public StrongNameSignerService(IKeyVaultAgent keyVaultAgent)
{
_keyVaultAgent = keyVaultAgent;
}

public Task<bool> TrySignAsync(Stream peStream)
{
StrongNameSigner strongNameSigner = new StrongNameSigner(_keyVaultAgent, peStream, SHA256.Create());
return strongNameSigner.TrySignAsync();
}

public bool CanSign(Stream peStream)
{
StrongNameSigner strongNameSigner = new StrongNameSigner(_keyVaultAgent, peStream, SHA256.Create());
return strongNameSigner.CanSign();
}
}
}
1 change: 1 addition & 0 deletions SigningService/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<package id="Owin" version="1.0" targetFramework="net45" userInstalled="true" />
<package id="Swashbuckle" version="5.0.0" targetFramework="net45" userInstalled="true" />
<package id="Swashbuckle.Core" version="5.0.0" targetFramework="net45" userInstalled="true" />
<package id="System.Collections.Immutable" version="1.1.34-rc" targetFramework="net45" />
<package id="System.IdentityModel.Tokens.Jwt" version="4.0.0" targetFramework="net45" userInstalled="true" />
<package id="Unity" version="3.5.1404.0" targetFramework="net45" userInstalled="true" />
<package id="Unity.WebAPI" version="5.1" targetFramework="net45" userInstalled="true" />
Expand Down
25 changes: 25 additions & 0 deletions SigningService/src/DataBlock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;

namespace SigningService
{
public struct DataBlock : IComparable<DataBlock>
{
public DataBlock(DataBlockHashing hashing, string name, int offset, int size)
{
Hashing = hashing;
Name = name;
Offset = offset;
Size = size;
}

public DataBlockHashing Hashing;
public string Name;
public int Offset;
public int Size;

public int CompareTo(DataBlock other)
{
return Offset.CompareTo(other.Offset);
}
}
}
9 changes: 9 additions & 0 deletions SigningService/src/DataBlockHashing.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace SigningService
{
public enum DataBlockHashing
{
Hash,
Skip,
HashZeros
}
}
Loading

0 comments on commit eb23cea

Please sign in to comment.