1
- #if ( NETSTANDARD )
2
- using StrongNameKeyPair = Mono . Cecil . StrongNameKeyPair ;
3
- #else
4
- using StrongNameKeyPair = System . Reflection . StrongNameKeyPair ;
5
- #endif
6
-
7
1
public partial class InnerWeaver
8
2
{
9
- public StrongNameKeyPair ? StrongNameKeyPair ;
3
+ public byte [ ] ? StrongNameKeyBlob ;
10
4
public byte [ ] ? PublicKey ;
11
5
12
6
public virtual void FindStrongNameKey ( )
@@ -27,30 +21,21 @@ public virtual void FindStrongNameKey()
27
21
28
22
if ( ! DelaySign )
29
23
{
30
- try
24
+ if ( IsPrivateKeyFile ( fileBytes ) )
31
25
{
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.
40
28
return ;
41
29
}
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." ) ;
46
31
}
47
32
48
33
// Fall back to delay signing, this was the original behavior, however that does not work in NETSTANDARD (s.a.)
49
34
Logger . LogDebug ( "Prepare public key for delay-signing." ) ;
50
35
51
36
// We know that we cannot sign the assembly with this key-file. Let's assume that it is a public
52
37
// only key-file and pass along all the bytes.
53
- StrongNameKeyPair = null ;
38
+ StrongNameKeyBlob = null ;
54
39
PublicKey = fileBytes ;
55
40
}
56
41
}
@@ -78,4 +63,11 @@ public virtual void FindStrongNameKey()
78
63
Logger . LogDebug ( "No strong name key found" ) ;
79
64
return null ;
80
65
}
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
81
73
}
0 commit comments