Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/release-notes/.FSharp.Compiler.Service/10.0.200.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,5 @@
* Removed `#light` and `#indent` directives (they are now a no-op; combined with `off` they give an error). ([PR #19143](https://github.com/dotnet/fsharp/pull/19143))
* Removed `--light`, `--indentation-syntax`, `--no-indendation-syntax`, `--ml-keywords` and `--mlcompatibility` compiler/fsi flags. ([PR #19143](https://github.com/dotnet/fsharp/pull/19143))
* Removed parsing support for long-deprecated ML (non-light) constructs. ([PR #19143](https://github.com/dotnet/fsharp/pull/19143))

Fix strong name signature size to align with Roslyn for public signing (#11887)
72 changes: 41 additions & 31 deletions src/Compiler/AbstractIL/ilsign.fs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.

module internal FSharp.Compiler.AbstractIL.StrongNameSign

Expand Down Expand Up @@ -127,6 +127,10 @@ type BlobReader =
val mutable _offset: int
new(blob: byte array) = { _blob = blob; _offset = 0 }

member x.Offset
with get () = x._offset
and set (v) = x._offset <- v

member x.ReadInt32() : int =
let offset = x._offset
x._offset <- offset + 4
Expand All @@ -145,13 +149,13 @@ type BlobReader =
let RSAParametersFromBlob blob keyType =
let mutable reader = BlobReader blob

if reader.ReadInt32() <> 0x00000207 && keyType = KeyType.KeyPair then
raise (CryptographicException(getResourceString (FSComp.SR.ilSignPrivateKeyExpected ())))
reader.ReadInt32() |> ignore
reader.ReadInt32() |> ignore

reader.ReadInt32() |> ignore // ALG_ID
let magic = reader.ReadInt32()

if reader.ReadInt32() <> RSA_PRIV_MAGIC then
raise (CryptographicException(getResourceString (FSComp.SR.ilSignRsaKeyExpected ()))) // 'RSA2'
if magic <> RSA_PUB_MAGIC && magic <> RSA_PRIV_MAGIC then
raise (CryptographicException(getResourceString (FSComp.SR.ilSignRsaKeyExpected ())))

let byteLen, halfLen =
let bitLen = reader.ReadInt32()
Expand All @@ -160,15 +164,20 @@ let RSAParametersFromBlob blob keyType =
| 0 -> (bitLen / 8, bitLen / 16)
| _ -> raise (CryptographicException(getResourceString (FSComp.SR.ilSignInvalidBitLen ())))

ignore keyType

let mutable key = RSAParameters()
key.Exponent <- reader.ReadBigInteger 4
key.Modulus <- reader.ReadBigInteger byteLen
key.P <- reader.ReadBigInteger halfLen
key.Q <- reader.ReadBigInteger halfLen
key.DP <- reader.ReadBigInteger halfLen
key.DQ <- reader.ReadBigInteger halfLen
key.InverseQ <- reader.ReadBigInteger halfLen
key.D <- reader.ReadBigInteger byteLen

if magic = RSA_PRIV_MAGIC then
key.P <- reader.ReadBigInteger halfLen
key.Q <- reader.ReadBigInteger halfLen
key.DP <- reader.ReadBigInteger halfLen
key.DQ <- reader.ReadBigInteger halfLen
key.InverseQ <- reader.ReadBigInteger halfLen
key.D <- reader.ReadBigInteger byteLen

key

let validateRSAField (field: byte array | null) expected (name: string) =
Expand Down Expand Up @@ -301,19 +310,25 @@ let signStream stream keyBlob =
patchSignature stream peReader signature

let signatureSize (pk: byte array) =
if pk.Length < 25 then
raise (CryptographicException(getResourceString (FSComp.SR.ilSignInvalidPKBlob ())))

let mutable reader = BlobReader pk
reader.ReadBigInteger 12 |> ignore // Skip CLRHeader
reader.ReadBigInteger 8 |> ignore // Skip BlobHeader
let magic = reader.ReadInt32() // Read magic

if not (magic = RSA_PRIV_MAGIC || magic = RSA_PUB_MAGIC) then // RSAPubKey.magic
raise (CryptographicException(getResourceString (FSComp.SR.ilSignInvalidPKBlob ())))

let x = reader.ReadInt32() / 8
x
if pk.Length < 20 then
0
else
let reader = BlobReader pk
reader.ReadInt32() |> ignore // aiKeyAlg
reader.ReadInt32() |> ignore // aiHashAlg
reader.ReadInt32() |> ignore // keyLen
reader.ReadInt32() |> ignore // bType, bVersion, reserved
let magic = reader.ReadInt32()

if magic = RSA_PUB_MAGIC || magic = RSA_PRIV_MAGIC then
let bitLen = reader.ReadInt32()
let keySize = bitLen / 8

if keySize < (128 + 32) then 128 else keySize - 32
else if pk.Length < 160 then
128
else
pk.Length - 32

// Returns a CLR Format Blob public key
let getPublicKeyForKeyPair keyBlob =
Expand Down Expand Up @@ -371,12 +386,7 @@ type ILStrongNameSigner =
| KeyContainer _ -> failWithContainerSigningUnsupportedOnThisPlatform ()

member s.SignatureSize =
let pkSignatureSize pk =
try
signerSignatureSize pk
with exn ->
failwith ("A call to StrongNameSignatureSize failed (" + exn.Message + ")")
0x80
let pkSignatureSize pk = signerSignatureSize pk

match s with
| PublicKeySigner pk -> pkSignatureSize pk
Expand Down
Loading