提交 c54860f8 编写于 作者: A Andy Gocke 提交者: GitHub

Remove restrictions on signing key length (#13910)

Roslyn uses the CLR APIs to sign an assembly, so as long as the CLR
supports the key length and it is a valid key structure there's no
reason we should limit the length of the key. In addition, the previous
limit was wrong -- it didn't include padding space for the key header.

Fixes #12822
上级 177f717e
......@@ -1015,6 +1015,68 @@ public class C
#region Signing
[Fact]
public void MaxSizeKey()
{
var pubKey = TestResources.General.snMaxSizePublicKeyString;
string pubKeyToken = "1540923db30520b2";
var pubKeyTokenBytes = new byte[] { 0x15, 0x40, 0x92, 0x3d, 0xb3, 0x05, 0x20, 0xb2 };
var comp = CreateCompilationWithMscorlib($@"
using System;
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo(""MaxSizeComp2, PublicKey={pubKey}, PublicKeyToken={pubKeyToken}"")]
internal class C
{{
public static void M()
{{
Console.WriteLine(""Called M"");
}}
}}",
options: TestOptions.ReleaseDll
.WithCryptoKeyFile(SigningTestHelpers.MaxSizeKeyFile)
.WithStrongNameProvider(s_defaultProvider));
comp.VerifyEmitDiagnostics();
Assert.True(comp.IsRealSigned);
VerifySignedBitSetAfterEmit(comp);
Assert.Equal(TestResources.General.snMaxSizePublicKey, comp.Assembly.Identity.PublicKey);
Assert.Equal<byte>(pubKeyTokenBytes, comp.Assembly.Identity.PublicKeyToken);
var src = @"
class D
{
public static void Main()
{
C.M();
}
}";
var comp2 = CreateCompilationWithMscorlib(src,
references: new[] { comp.ToMetadataReference() },
assemblyName: "MaxSizeComp2",
options: TestOptions.ReleaseExe
.WithCryptoKeyFile(SigningTestHelpers.MaxSizeKeyFile)
.WithStrongNameProvider(s_defaultProvider));
CompileAndVerify(comp2, expectedOutput: "Called M");
Assert.Equal(TestResources.General.snMaxSizePublicKey, comp2.Assembly.Identity.PublicKey);
Assert.Equal<byte>(pubKeyTokenBytes, comp2.Assembly.Identity.PublicKeyToken);
var comp3 = CreateCompilationWithMscorlib(src,
references: new[] { comp.EmitToImageReference() },
assemblyName: "MaxSizeComp2",
options: TestOptions.ReleaseExe
.WithCryptoKeyFile(SigningTestHelpers.MaxSizeKeyFile)
.WithStrongNameProvider(s_defaultProvider));
CompileAndVerify(comp3, expectedOutput: "Called M");
Assert.Equal(TestResources.General.snMaxSizePublicKey, comp3.Assembly.Identity.PublicKey);
Assert.Equal<byte>(pubKeyTokenBytes, comp3.Assembly.Identity.PublicKeyToken);
}
[Fact]
public void SignIt()
{
......
......@@ -619,24 +619,15 @@ internal static bool TryParseVersion(string str, out ulong result, out AssemblyI
}
}
private const int MaxPublicKeyBytes = 2048;
private static bool TryParsePublicKey(string value, out ImmutableArray<byte> key)
{
ImmutableArray<byte> result;
if (value.Length > (MaxPublicKeyBytes * 2) || !TryParseHexBytes(value, out result))
{
key = default(ImmutableArray<byte>);
return false;
}
if (!MetadataHelpers.IsValidPublicKey(result))
if (!TryParseHexBytes(value, out key) ||
!MetadataHelpers.IsValidPublicKey(key))
{
key = default(ImmutableArray<byte>);
return false;
}
key = result;
return true;
}
......
......@@ -346,6 +346,8 @@
<EmbeddedResource Include="SymbolsTests\snKey2.snk" />
<EmbeddedResource Include="SymbolsTests\snPublicKey.snk" />
<EmbeddedResource Include="SymbolsTests\snPublicKey2.snk" />
<EmbeddedResource Include="SymbolsTests\snMaxSizeKey.snk" />
<EmbeddedResource Include="SymbolsTests\snMaxSizePublicKey.snk" />
<EmbeddedResource Include="SymbolsTests\TypeForwarders\Forwarded.netmodule" />
<EmbeddedResource Include="SymbolsTests\TypeForwarders\TypeForwarder.dll" />
<Content Include="SymbolsTests\TypeForwarders\TypeForwarder.vb" />
......
......@@ -253,6 +253,67 @@ public static class General
private static byte[] s_snPublicKey2;
public static byte[] snPublicKey2 => ResourceLoader.GetOrCreateResource(ref s_snPublicKey2, "SymbolsTests.snPublicKey2.snk");
private static byte[] s_snMaxSizeKey;
public static byte[] snMaxSizeKey => ResourceLoader.GetOrCreateResource(ref s_snMaxSizeKey, "SymbolsTests.snMaxSizeKey.snk");
private static byte[] s_snMaxSizePublicKey;
public static byte[] snMaxSizePublicKey => ResourceLoader.GetOrCreateResource(ref s_snMaxSizePublicKey, "SymbolsTests.snMaxSizePublicKey.snk");
public static string snMaxSizePublicKeyString => "002400000480000014080000060200000024000052534131004000000100010079bb5332224912" +
"5411d2b44dd63b137e1b452899a7e7f626917328ff9e25c728e3e3b503ba34deab31d1f1ae1558" +
"8c4bda69eccea5b13e4a4e10b39fc2fd9f05d1ba728beb8365bad6b6da9adc653836d3ff12b9a6" +
"98900c3f593cf088b2504ec949489b6f837e76fe84ddd30ccedce1d836e5b8fb149b8e9e0b8b8f" +
"bc2cdaee0e76eb549270c4df104accb72530113f431d88982ae69ed75e09530d6951722b60342e" +
"b1f5dd5babacdb365dd71597680c50fe85bce823ee902ab3377e7eef8f96168f8c8a1e8264ba94" +
"481f5208e4c21208ea312bc1a34bd0e615b39ce8948c4a4d2c0a48b0bc901dfc0519afc378f859" +
"5a77375e6c265e1c38bdc7dbf7c4d07d36b67ac94464fe5c26aed915f1c035756d0f3363fce751" +
"0f12459060f417ab5df610ffca60e6dd739dc750189f23a47716c75a7a8e3363b198f05093d2a0" +
"c9debafbfca3d682c5ea3ed578118d9dc7d0f8828cad1c03ede009d774585b9665e0c8d7325805" +
"faba80796f668f79c92b9a195bc7530bb8ecaaba07a7cfdb70c46b96ca613102b1a674bfc742fa" +
"9562704edb78063db818c0675c9bd8c18d203fc4d5bc2685003bc6c136caf07a202578cb85480d" +
"50f6187b88fb733a2f4ce200bbda68c4ef47483a3530ae8403cb38253a06e2e9385b6d3ae9a718" +
"2ba7a23f03499cec1c92ae06dde6b304c025d23466ebbbac9e06b5d7eb932fc009bc1803d03571" +
"0ec7bce4a6176b407ffdc9a5b55a3ff444609172a146bf76ae40759634e8224ba2882371808f44" +
"59a37f8e69115424947818f19ff6609a715f550e33de0307195fe1e526c57efc7212d6cb561dd8" +
"33cb8c28ae9dc32a4bc0f775887001a5ec36cf63e5b2aa9989d3fa29ebf57e4fa89a206a32e75b" +
"cac3c2f26c3267ec1b7433d4a3b90bc01563ddbffffe586ccfb8ee59af34e3127ebf99036427e0" +
"9c107d47c1e885a032065dce6dd646305bf84fb9123392c89794318e2fdffd5eaa62d1e52d29b9" +
"4e484f2fb73fea0487bbdaa1790e79fc0e09372c6187c742c8a3f160d09818f51dc58f71ff1a1e" +
"d955d9b373bfe92e09eac22241c2b96ce0213aa266f21aae95489921269bffdf5c0a0794716daf" +
"8b5daa3a496004297b3a25c6472027f4b6f9fd82d4e297546faa6ac31579a30b3da1d6c6f04125" +
"667868b536b9d9ebd767e4d1cabbaeb977ec0738dab3b599fe63ea0ec622261d90c3c0c1ebc1ab" +
"631b2162284a9659e961c541aab1658853a9a6501e73f11c9c27b8e9bf41f03187dab5909d8433" +
"499f9dfaec2a2c907e39bf1683c75c882e469e79aba05d597a24db33479fac758f5bdc4cbd79df" +
"03ec1e403f231bfb81ff9db7ee4cfe084f5c187729cc9f072d7a710651ea15f0e43f330e321723" +
"21554d7bf9fd784d18a80a13509818286616d4a7251e2c57f1c257aa0c57bd75da0b0e01532ad5" +
"17de450733f8379a9db8c9f12ac77b65215d44b40eebb513ec9ddc9537f7811eb5283386422d90" +
"6d26077608a1f506e966426d40cc5e61e2d7e888586c85050ec29eff79116c42c9714ad6672441" +
"03a9e79af9b330825ff186b19a791b60eca8776539ca2759f9dbbd87d07f3dac38b814ae9707e4" +
"73ee52e10b3e8d8344bc06287a9c6c58ab36658b4a6ab48ae2e6d08d748b35868c5207aab08311" +
"91f451595d0104968050ae1c13e0d619fa766cd90821732b9fbcc429815606704633515cbe5ad5" +
"e33a28690534748e15413c65d9a370b12946a36796aa4d8e5b471675a3471439e133476981e21e" +
"9a4dfeae52f657a5fe3ab6cd6ad8aabc09bd5d9af77226c6cfbe01fb38546b5c0b8b825e03bda1" +
"3d85403765bd5a6cbed19fd09674fd691d732328948f5ab07e03d7f919eee0ac23f6de7d49ae44" +
"f15f8459683ab792270945ee2807158a5e6898cb912cbe3b0b6820565045a41699d0a5e3b89319" +
"fb921008e18bb1c28557600c33cf2c299a79213834cb9ec72ba6402699c381060cfebaa3faf52d" +
"9b2f1b68c3cc0db79ff47b293853b80ec4198c7fe099077f876f2d6c26305cab1c9de8bb8daae4" +
"22e1ef7c5c76949c8d27fde90281781eef364cc001d0916108d6c0ace740521ec549d912fbaf71" +
"68bd37f790b46282684030dcdc2d52cb41d4b763adfc701a1d392166d4b3269ab30fb83a4fd183" +
"4771e0ea24680c09f55413750b082787e4bb301e107c34cfab1cc88b7d68489602cb8e46bd73c9" +
"6c8de8af5285f919e93cc6251df057443460a15d432e130510f8adbaa8d28c574db7d9ef6fb947" +
"b70e274d93cfaa47d00f3318643a08815c10975722324037504d7f0e3902393d5327bc0467ea5b" +
"d555ba0671ca3873486038abeccc6d48a11c6e3ffb2acca285a53641a02233bb7e7c76ab38acf6" +
"759b985e22b18da77932c0c04217798d1473ebf41061d8c006c9479b34745fbea8a1761000d16f" +
"414a544a7dc4a5a346871981d1ed3fe4dfcb8494e95643b8bae2e13bbfcb5a432c2dfd481e1d61" +
"bab2bcc0d7140fe9b472d25112b2e241c3026a7468560ce3ed582d6872b041680bff3998d51afc" +
"a45094e3e1982510fe8573ac2d3ab596d9d0c6b43a5f72c6046f24c2ac457fd440d6f8d4dd0b71" +
"399d0c1aa366e7a86c57ba5235d327da1245b5ecdf0b3e0e81a0418a5743f3fe98ef6c9236dce0" +
"2463c798af2b239f6ddf2e5a5ffa198151c2ffbf932b7357e80e858c9ddb81fe8223897af61cae" +
"c44ae4f07e686b1d721fa78b39c7934179786592472f8739fb90fd5ae41e118fafbb30bd7b02c3" +
"cf3def669d830f4dcdf863919c1ee6c3b68a4d66a74af3088592a4055b54738804034d134c5a92" +
"e47395955d222b04472da50de86f931084653e4b0f91ffccef2c777c80d92683f8f87b6b60733d" +
"73b0035501dd2adba2bbdf6697";
private static byte[] s_CSharpErrors;
public static byte[] CSharpErrors => ResourceLoader.GetOrCreateResource(ref s_CSharpErrors, "SymbolsTests.UseSiteErrors.CSharpErrors.dll");
......
......@@ -31,6 +31,18 @@ Partial Public Class InternalsVisibleToAndStrongNameTests
Return New SigningTestHelpers.VirtualizedStrongNameProvider(ImmutableArray.Create(keyFilePath))
End Function
Private Shared Sub VerifySigned(comp As Compilation)
Using outStream = comp.EmitToStream()
outStream.Position = 0
Dim headers = New PEHeaders(outStream)
Dim flags = headers.CorHeader.Flags
Assert.True(flags.HasFlag(CorFlags.StrongNameSigned))
End Using
End Sub
#End Region
#Region "Naming Tests"
......@@ -913,6 +925,63 @@ BC31535: Friend assembly reference 'WantsIVTAccess' is invalid. Strong-name sign
#End Region
#Region "Signing"
<Fact>
Public Sub MaxSizeKey()
Dim pubKey = TestResources.General.snMaxSizePublicKeyString
Const pubKeyToken = "1540923db30520b2"
Dim pubKeyTokenBytes As Byte() = {&H15, &H40, &H92, &H3D, &HB3, &H5, &H20, &HB2}
Dim comp = CreateCompilationWithMscorlib(
<compilation>
<file name="c.vb">
Imports System
Imports System.Runtime.CompilerServices
&lt;Assembly:InternalsVisibleTo("MaxSizeComp2, PublicKey=<%= pubKey %>, PublicKeyToken=<%= pubKeyToken %>")&gt;
Friend Class C
Public Shared Sub M()
Console.WriteLine("Called M")
End Sub
End Class
</file>
</compilation>,
options:=TestOptions.ReleaseDll.WithCryptoKeyFile(SigningTestHelpers.MaxSizeKeyFile).WithStrongNameProvider(s_defaultProvider))
comp.VerifyEmitDiagnostics()
Assert.True(comp.IsRealSigned)
VerifySigned(comp)
Assert.Equal(TestResources.General.snMaxSizePublicKey, comp.Assembly.Identity.PublicKey)
Assert.Equal(Of Byte)(pubKeyTokenBytes, comp.Assembly.Identity.PublicKeyToken)
Dim src =
<compilation name="MaxSizeComp2">
<file name="c.vb">
Class D
Public Shared Sub Main()
C.M()
End Sub
End Class
</file>
</compilation>
Dim comp2 = CreateCompilationWithMscorlib(src, references:={comp.ToMetadataReference()},
options:=TestOptions.ReleaseExe.WithCryptoKeyFile(SigningTestHelpers.MaxSizeKeyFile).WithStrongNameProvider(s_defaultProvider))
CompileAndVerify(comp2, expectedOutput:="Called M")
Assert.Equal(TestResources.General.snMaxSizePublicKey, comp2.Assembly.Identity.PublicKey)
Assert.Equal(Of Byte)(pubKeyTokenBytes, comp2.Assembly.Identity.PublicKeyToken)
Dim comp3 = CreateCompilationWithMscorlib(src, references:={comp.EmitToImageReference()},
options:=TestOptions.ReleaseExe.WithCryptoKeyFile(SigningTestHelpers.MaxSizeKeyFile).WithStrongNameProvider(s_defaultProvider))
CompileAndVerify(comp3, expectedOutput:="Called M")
Assert.Equal(TestResources.General.snMaxSizePublicKey, comp3.Assembly.Identity.PublicKey)
Assert.Equal(Of Byte)(pubKeyTokenBytes, comp3.Assembly.Identity.PublicKeyToken)
End Sub
<Fact>
Public Sub SignIt()
Dim other As VisualBasicCompilation = CreateCompilationWithMscorlib(
......
......@@ -19,6 +19,8 @@ internal static class SigningTestHelpers
internal static string KeyPairFile2 = @"R:\__Test__\KeyPair2_" + Guid.NewGuid() + ".snk";
internal static string PublicKeyFile2 = @"R:\__Test__\PublicKey2_" + Guid.NewGuid() + ".snk";
internal static string MaxSizeKeyFile = @"R:\__Test__\MaxSizeKey_" + Guid.NewGuid() + ".snk";
private static bool s_keyInstalled;
internal const string TestContainerName = "RoslynTestContainer";
......@@ -88,6 +90,10 @@ internal override byte[] ReadAllBytes(string fullPath)
{
return TestResources.General.snPublicKey2;
}
else if (PathEquals(fullPath, MaxSizeKeyFile))
{
return TestResources.General.snMaxSizeKey;
}
throw new FileNotFoundException("File not found", fullPath);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册