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
33module internal FSharp.Compiler.AbstractIL.StrongNameSign
44
@@ -126,9 +126,11 @@ 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+
130+ member x.Offset
131+ with get () = x._ offset
132+ and set ( v ) = x._ offset <- v
133+
132134 member x.ReadInt32 () : int =
133135 let offset = x._ offset
134136 x._ offset <- offset + 4
@@ -144,34 +146,40 @@ type BlobReader =
144146 x._ offset <- x._ offset + length
145147 arr |> Array.rev
146148
147- let RSAParametersFromBlob blob keyType =
149+ /// Decodes an RSA functional blob into RSAParameters.
150+ /// Ref: https://learn.microsoft.com/en-us/windows/win32/api/wincrypt/ns-wincrypt-publickeystruc
151+ let RSAParametersFromBlob blob _keyType =
148152 let mutable reader = BlobReader blob
149153
150- let header = reader.ReadInt32()
151- if header <> 0x00000206 && header <> 0x00000207 && keyType = KeyType.KeyPair then
152- raise ( CryptographicException( getResourceString ( FSComp.SR.ilSignPrivateKeyExpected ())))
153-
154- reader.ReadInt32() |> ignore // ALG_ID
154+ // Skip PUBLICKEYSTRUC (8 bytes): bType(1), bVersion(1), reserved(2), aiKeyAlg(4)
155+ reader.ReadInt32() |> ignore
156+ reader.ReadInt32() |> ignore
155157
156- if reader.ReadInt32() <> RSA_ PRIV_ MAGIC then
157- raise ( CryptographicException( getResourceString ( FSComp.SR.ilSignRsaKeyExpected ()))) // 'RSA2'
158+ // Read RSAPUBKEY header (starts with magic)
159+ // Ref: https://learn.microsoft.com/en-us/windows/win32/api/wincrypt/ns-wincrypt-rsapubkey
160+ let magic = reader.ReadInt32()
158161
159- let byteLen , halfLen =
160- let bitLen = reader.ReadInt32 ( )
162+ if magic <> RSA _ PUB _ MAGIC && magic <> RSA _ PRIV _ MAGIC then
163+ raise ( CryptographicException ( getResourceString ( FSComp.SR.ilSignRsaKeyExpected ())) )
161164
162- match bitLen % 16 with
163- | 0 -> ( bitLen / 8 , bitLen / 16 )
164- | _ -> raise ( CryptographicException( getResourceString ( FSComp.SR.ilSignInvalidBitLen ())))
165+ let bitLen = reader.ReadInt32()
166+ let byteLen , halfLen = ( bitLen / 8 , bitLen / 16 )
165167
166168 let mutable key = RSAParameters()
167- key.Exponent <- reader.ReadBigInteger 4
169+ key.Exponent <- reader.ReadBigInteger 4 // pubexp (4 bytes)
168170 key.Modulus <- reader.ReadBigInteger byteLen
169- key.P <- reader.ReadBigInteger halfLen
170- key.Q <- reader.ReadBigInteger halfLen
171- key.DP <- reader.ReadBigInteger halfLen
172- key.DQ <- reader.ReadBigInteger halfLen
173- key.InverseQ <- reader.ReadBigInteger halfLen
174- key.D <- reader.ReadBigInteger byteLen
171+
172+ // IMPORTANT: Conditional reading based on Magic.
173+ // Private fields (P, Q, DP, DQ, InverseQ, D) follow the modulus ONLY in PrivateKeyBlobs (RSA2).
174+ // Ref: https://learn.microsoft.com/en-us/windows/win32/seccrypto/base-provider-key-blobs
175+ if magic = RSA_ PRIV_ MAGIC then
176+ key.P <- reader.ReadBigInteger halfLen
177+ key.Q <- reader.ReadBigInteger halfLen
178+ key.DP <- reader.ReadBigInteger halfLen
179+ key.DQ <- reader.ReadBigInteger halfLen
180+ key.InverseQ <- reader.ReadBigInteger halfLen
181+ key.D <- reader.ReadBigInteger byteLen
182+
175183 key
176184
177185let validateRSAField ( field : byte array MaybeNull ) expected ( name : string ) =
@@ -303,30 +311,44 @@ let signStream stream keyBlob =
303311 let signature = createSignature hash keyBlob KeyType.KeyPair
304312 patchSignature stream peReader signature
305313
306- let signatureSize ( pk : byte array ) =
307- if pk.Length < 20 then 0
314+ /// Calculates the required signature space in the PE header for an RSA key.
315+ ///
316+ /// Rationale:
317+ /// Following Roslyn's 'SigningUtilities', we distinguish between public keys and full key pairs.
318+ /// For full key pairs (SNKs), we reserve space matching the modulus size (pk.Length - 32 bytes of header).
319+ /// This ensures parity with 'mscorsn.dll' expectations on Windows Desktop during the signing process.
320+ let signatureSize ( pk : byte array ) ( isFullKeyPair : bool ) =
321+ if ( box pk |> isNull) || pk.Length < 16 then
322+ 0
308323 else
309- 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
320-
324+ let adjustedSize =
325+ if isFullKeyPair then
326+ // Calculate space based on modulus size (Blob length minus 32-byte header)
327+ // matching Roslyn's: (keySize < 160) ? 128 : keySize - 32
328+ Math.Max( pk.Length - 32 , 128 )
329+ else
330+ // For public keys/delay signing, strictly reserve the standard 128 bytes.
331+ 128
332+
333+ // PE/COFF Spec Section 5.5: The Certificate Table entry must be aligned on an 8-byte boundary.
334+ ( adjustedSize + 7 ) &&& ~~~ 7
335+
336+ // Returns a CLR Format Blob public key
321337let getPublicKeyForKeyPair keyBlob =
322338 use rsa = RSA.Create()
323339 rsa.ImportParameters( RSAParametersFromBlob keyBlob KeyType.KeyPair)
324340 let rsaParameters = rsa.ExportParameters false
325341 toCLRKeyBlob rsaParameters CALG_ RSA_ KEYX
326342
343+ // Key signing
344+ type keyContainerName = string
345+ type keyPair = byte array
346+ type pubkey = byte array
347+ type pubkeyOptions = byte array * bool
348+
327349let signerGetPublicKeyForKeyPair ( kp : keyPair ) : pubkey = getPublicKeyForKeyPair kp
328350
329- let signerSignatureSize ( pk : pubkey ) : int = signatureSize pk
351+ let signerSignatureSize ( pk : pubkey ) ( isFullKeyPair : bool ) : int = signatureSize pk isFullKeyPair
330352
331353let signerSignStreamWithKeyPair stream keyBlob = signStream stream keyBlob
332354
@@ -367,15 +389,15 @@ type ILStrongNameSigner =
367389 | KeyContainer _ -> failWithContainerSigningUnsupportedOnThisPlatform ()
368390
369391 member s.SignatureSize =
370- let pkSignatureSize pk =
371- signerSignatureSize pk
372-
373392 match s with
374- | PublicKeySigner pk -> pkSignatureSize pk
393+ | PublicKeySigner pk -> signerSignatureSize pk false
375394 | PublicKeyOptionsSigner pko ->
376395 let pk , _ = pko
377- pkSignatureSize pk
378- | KeyPair kp -> pkSignatureSize ( signerGetPublicKeyForKeyPair kp)
396+ signerSignatureSize pk false
397+ | KeyPair kp ->
398+ // For full key pairs, we calculate based on the public part
399+ // but pass true to trigger the legacy padding logic.
400+ signerSignatureSize ( signerGetPublicKeyForKeyPair kp) true
379401 | KeyContainer _ -> failWithContainerSigningUnsupportedOnThisPlatform ()
380402
381403 member s.SignStream stream =
0 commit comments