Skip to content

Commit 67bd03d

Browse files
authored
Merge pull request #1311 from teo-tsirpanis/sn-fix
Avoid constructing `StrongNameKeyPair`.
2 parents 7fb7371 + 220fe89 commit 67bd03d

File tree

2 files changed

+14
-22
lines changed

2 files changed

+14
-22
lines changed

FodyIsolated/ModuleWriter.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public virtual void WriteModule()
99

1010
var parameters = new WriterParameters
1111
{
12-
StrongNameKeyPair = StrongNameKeyPair,
12+
StrongNameKeyBlob = StrongNameKeyBlob,
1313
WriteSymbols = hasSymbols
1414
};
1515

FodyIsolated/StrongNameKeyFinder.cs

+13-21
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1-
#if (NETSTANDARD)
2-
using StrongNameKeyPair = Mono.Cecil.StrongNameKeyPair;
3-
#else
4-
using StrongNameKeyPair = System.Reflection.StrongNameKeyPair;
5-
#endif
6-
71
public partial class InnerWeaver
82
{
9-
public StrongNameKeyPair? StrongNameKeyPair;
3+
public byte[]? StrongNameKeyBlob;
104
public byte[]? PublicKey;
115

126
public virtual void FindStrongNameKey()
@@ -27,30 +21,21 @@ public virtual void FindStrongNameKey()
2721

2822
if (!DelaySign)
2923
{
30-
try
24+
if (IsPrivateKeyFile(fileBytes))
3125
{
32-
Logger.LogDebug("Extract public key from key file for signing.");
33-
34-
StrongNameKeyPair = new(fileBytes);
35-
// Ensure that we can generate the public key from the key file. This requires the private key to
36-
// work. If we cannot generate the public key, an ArgumentException will be thrown. In this case,
37-
// the assembly is delay-signed with a public only key-file.
38-
// Note: The NETSTANDARD implementation of StrongNameKeyPair.PublicKey does never throw here.
39-
PublicKey = StrongNameKeyPair.PublicKey;
26+
StrongNameKeyBlob = fileBytes;
27+
// Cecil will update the assembly name's public key.
4028
return;
4129
}
42-
catch (ArgumentException)
43-
{
44-
Logger.LogWarning("Failed to extract public key from key file, fall back to delay-signing.");
45-
}
30+
Logger.LogWarning("Key file is not private key, fall back to delay-signing.");
4631
}
4732

4833
// Fall back to delay signing, this was the original behavior, however that does not work in NETSTANDARD (s.a.)
4934
Logger.LogDebug("Prepare public key for delay-signing.");
5035

5136
// We know that we cannot sign the assembly with this key-file. Let's assume that it is a public
5237
// only key-file and pass along all the bytes.
53-
StrongNameKeyPair = null;
38+
StrongNameKeyBlob = null;
5439
PublicKey = fileBytes;
5540
}
5641
}
@@ -78,4 +63,11 @@ public virtual void FindStrongNameKey()
7863
Logger.LogDebug("No strong name key found");
7964
return null;
8065
}
66+
67+
static bool IsPrivateKeyFile(byte[] blob) => blob.Length >= 12
68+
&& blob[0] == 0x07 // PRIVATEKEYBLOB (0x07)
69+
&& blob[1] == 0x02 // Version (0x02)
70+
&& blob[2] == 0x00 // Reserved (word)
71+
&& blob[3] == 0x00
72+
&& BitConverter.ToUInt32(blob, 8) == 0x32415352; // DWORD magic = RSA2
8173
}

0 commit comments

Comments
 (0)