Skip to content

Commit 8cb5441

Browse files
AhmedAhmed
authored andcommitted
Fix cross-platform strong name signing by manually parsing RSA key blobs
1 parent f8427ae commit 8cb5441

File tree

1 file changed

+37
-18
lines changed

1 file changed

+37
-18
lines changed

src/Compiler/AbstractIL/ilsign.fs

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
1+
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
22

33
module internal FSharp.Compiler.AbstractIL.StrongNameSign
44

@@ -126,9 +126,7 @@ type BlobReader =
126126
val mutable _blob: byte array
127127
val mutable _offset: int
128128
new(blob: byte array) = { _blob = blob; _offset = 0 }
129-
130-
member x.Offset with get() = x._offset and set(v) = x._offset <- v
131-
129+
132130
member x.ReadInt32() : int =
133131
let offset = x._offset
134132
x._offset <- offset + 4
@@ -147,8 +145,7 @@ type BlobReader =
147145
let RSAParametersFromBlob blob keyType =
148146
let mutable reader = BlobReader blob
149147

150-
let header = reader.ReadInt32()
151-
if header <> 0x00000206 && header <> 0x00000207 && keyType = KeyType.KeyPair then
148+
if reader.ReadInt32() <> 0x00000207 && keyType = KeyType.KeyPair then
152149
raise (CryptographicException(getResourceString (FSComp.SR.ilSignPrivateKeyExpected ())))
153150

154151
reader.ReadInt32() |> ignore // ALG_ID
@@ -304,26 +301,44 @@ let signStream stream keyBlob =
304301
patchSignature stream peReader signature
305302

306303
let signatureSize (pk: byte array) =
307-
if pk.Length < 20 then 0
304+
if isNull (box pk) || pk.Length < 16 then
305+
0
308306
else
309307
let reader = BlobReader pk
310-
reader.Offset <- 12
311-
let bitLen = reader.ReadInt32()
312-
let modulusLength = bitLen / 8
313-
314-
if modulusLength < 160 then 128 else modulusLength - 32
315-
// Key signing
316-
type keyContainerName = string
317-
type keyPair = byte array
318-
type pubkey = byte array
319-
type pubkeyOptions = byte array * bool
320308

309+
let tryReadBitLen (offset: int) =
310+
if pk.Length >= offset + 12 then
311+
reader._offset <- offset
312+
let magic = reader.ReadInt32()
313+
314+
if magic = RSA_PUB_MAGIC || magic = RSA_PRIV_MAGIC then
315+
let bitLen = reader.ReadInt32()
316+
Some bitLen
317+
else
318+
None
319+
else
320+
None
321+
322+
match tryReadBitLen 8 with
323+
| Some bitLen -> (bitLen / 8 + 7) &&& ~~~7
324+
| None ->
325+
match tryReadBitLen 20 with
326+
| Some bitLen -> (bitLen / 8 + 7) &&& ~~~7
327+
| None -> 128
328+
329+
// Returns a CLR Format Blob public key
321330
let getPublicKeyForKeyPair keyBlob =
322331
use rsa = RSA.Create()
323332
rsa.ImportParameters(RSAParametersFromBlob keyBlob KeyType.KeyPair)
324333
let rsaParameters = rsa.ExportParameters false
325334
toCLRKeyBlob rsaParameters CALG_RSA_KEYX
326335

336+
// Key signing
337+
type keyContainerName = string
338+
type keyPair = byte array
339+
type pubkey = byte array
340+
type pubkeyOptions = byte array * bool
341+
327342
let signerGetPublicKeyForKeyPair (kp: keyPair) : pubkey = getPublicKeyForKeyPair kp
328343

329344
let signerSignatureSize (pk: pubkey) : int = signatureSize pk
@@ -368,7 +383,11 @@ type ILStrongNameSigner =
368383

369384
member s.SignatureSize =
370385
let pkSignatureSize pk =
371-
signerSignatureSize pk
386+
try
387+
signerSignatureSize pk
388+
with exn ->
389+
failwith ("A call to StrongNameSignatureSize failed (" + exn.Message + ")")
390+
0x80
372391

373392
match s with
374393
| PublicKeySigner pk -> pkSignatureSize pk

0 commit comments

Comments
 (0)