Skip to content

Commit 347a4aa

Browse files
authored
Enable stream based strong signing (#14471)
* Enable stream based strong signing * moare * fantomas * readwrite
1 parent 5e5344c commit 347a4aa

File tree

3 files changed

+27
-46
lines changed

3 files changed

+27
-46
lines changed

src/Compiler/AbstractIL/ilsign.fs

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -297,12 +297,6 @@ let signStream stream keyBlob =
297297
let signature = createSignature hash keyBlob KeyType.KeyPair
298298
patchSignature stream peReader signature
299299

300-
let signFile fileName keyBlob =
301-
use fs =
302-
FileSystem.OpenFileForWriteShim(fileName, FileMode.Open, FileAccess.ReadWrite)
303-
304-
signStream fs keyBlob
305-
306300
let signatureSize (pk: byte[]) =
307301
if pk.Length < 25 then
308302
raise (CryptographicException(getResourceString (FSComp.SR.ilSignInvalidPKBlob ())))
@@ -339,18 +333,9 @@ let signerOpenKeyPairFile filePath =
339333

340334
let signerGetPublicKeyForKeyPair (kp: keyPair) : pubkey = getPublicKeyForKeyPair kp
341335

342-
let signerGetPublicKeyForKeyContainer (_kcName: keyContainerName) : pubkey =
343-
raise (NotImplementedException("signerGetPublicKeyForKeyContainer is not yet implemented"))
344-
345-
let signerCloseKeyContainer (_kc: keyContainerName) : unit =
346-
raise (NotImplementedException("signerCloseKeyContainer is not yet implemented"))
347-
348336
let signerSignatureSize (pk: pubkey) : int = signatureSize pk
349337

350-
let signerSignFileWithKeyPair (fileName: string) (kp: keyPair) : unit = signFile fileName kp
351-
352-
let signerSignFileWithKeyContainer (_fileName: string) (_kcName: keyContainerName) : unit =
353-
raise (NotImplementedException("signerSignFileWithKeyContainer is not yet implemented"))
338+
let signerSignStreamWithKeyPair stream keyBlob = signStream stream keyBlob
354339

355340
let failWithContainerSigningUnsupportedOnThisPlatform () =
356341
failwith (FSComp.SR.containerSigningUnsupportedOnThisPlatform () |> snd)
@@ -371,13 +356,6 @@ type ILStrongNameSigner =
371356
static member OpenKeyPairFile s = KeyPair(signerOpenKeyPairFile s)
372357
static member OpenKeyContainer s = KeyContainer s
373358

374-
member s.Close() =
375-
match s with
376-
| PublicKeySigner _
377-
| PublicKeyOptionsSigner _
378-
| KeyPair _ -> ()
379-
| KeyContainer _ -> failWithContainerSigningUnsupportedOnThisPlatform ()
380-
381359
member s.IsFullySigned =
382360
match s with
383361
| PublicKeySigner _ -> false
@@ -412,9 +390,9 @@ type ILStrongNameSigner =
412390
| KeyPair kp -> pkSignatureSize (signerGetPublicKeyForKeyPair kp)
413391
| KeyContainer _ -> failWithContainerSigningUnsupportedOnThisPlatform ()
414392

415-
member s.SignFile file =
393+
member s.SignStream stream =
416394
match s with
417395
| PublicKeySigner _ -> ()
418396
| PublicKeyOptionsSigner _ -> ()
419-
| KeyPair kp -> signerSignFileWithKeyPair file kp
397+
| KeyPair kp -> signerSignStreamWithKeyPair stream kp
420398
| KeyContainer _ -> failWithContainerSigningUnsupportedOnThisPlatform ()

src/Compiler/AbstractIL/ilsign.fsi

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
88
module internal FSharp.Compiler.AbstractIL.StrongNameSign
99

10+
open System
11+
open System.IO
12+
1013
//---------------------------------------------------------------------
1114
// Strong name signing
1215
//---------------------------------------------------------------------
@@ -17,8 +20,7 @@ type ILStrongNameSigner =
1720
static member OpenPublicKey: byte[] -> ILStrongNameSigner
1821
static member OpenKeyPairFile: string -> ILStrongNameSigner
1922
static member OpenKeyContainer: string -> ILStrongNameSigner
20-
member Close: unit -> unit
2123
member IsFullySigned: bool
2224
member PublicKey: byte[]
2325
member SignatureSize: int
24-
member SignFile: string -> unit
26+
member SignStream: Stream -> unit

src/Compiler/AbstractIL/ilwrite.fs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3711,9 +3711,22 @@ let writePdb (
37113711
// Used to capture the pdb file bytes in the case we're generating in-memory
37123712
let mutable pdbBytes = None
37133713

3714+
let signImage () =
3715+
// Sign the binary. No further changes to binary allowed past this point!
3716+
match signer with
3717+
| None -> ()
3718+
| Some s ->
3719+
use fs = reopenOutput()
3720+
try
3721+
s.SignStream fs
3722+
with exn ->
3723+
failwith ($"Warning: A call to SignFile failed ({exn.Message})")
3724+
reportTime showTimes "Signing Image"
3725+
37143726
// Now we've done the bulk of the binary, do the PDB file and fixup the binary.
37153727
match pdbfile with
3716-
| None -> ()
3728+
| None -> signImage ()
3729+
37173730
| Some pdbfile ->
37183731
let idd =
37193732
match pdbInfoOpt with
@@ -3763,28 +3776,14 @@ let writePdb (
37633776
os2.BaseStream.Seek (int64 (textV2P i.iddChunk.addr), SeekOrigin.Begin) |> ignore
37643777
if i.iddChunk.size < i.iddData.Length then failwith "Debug data area is not big enough. Debug info may not be usable"
37653778
writeBytes os2 i.iddData
3779+
reportTime showTimes "Finalize PDB"
3780+
signImage ()
37663781
os2.Dispose()
37673782
with exn ->
37683783
failwith ("Error while writing debug directory entry: " + exn.Message)
37693784
(try os2.Dispose(); FileSystem.FileDeleteShim outfile with _ -> ())
37703785
reraise()
37713786

3772-
reportTime showTimes "Finalize PDB"
3773-
3774-
// Sign the binary. No further changes to binary allowed past this point!
3775-
match signer with
3776-
| None -> ()
3777-
| Some s ->
3778-
try
3779-
s.SignFile outfile
3780-
s.Close()
3781-
with exn ->
3782-
failwith ("Warning: A call to SignFile failed ("+exn.Message+")")
3783-
(try s.Close() with _ -> ())
3784-
(try FileSystem.FileDeleteShim outfile with _ -> ())
3785-
()
3786-
3787-
reportTime showTimes "Signing Image"
37883787
pdbBytes
37893788

37903789
type options =
@@ -4528,7 +4527,7 @@ let writeBinaryFiles (options: options, modul, normalizeAssemblyRefs) =
45284527
reraise()
45294528

45304529
let reopenOutput () =
4531-
FileSystem.OpenFileForWriteShim(options.outfile, FileMode.Open, FileAccess.Write, FileShare.Read)
4530+
FileSystem.OpenFileForWriteShim(options.outfile, FileMode.Open, FileAccess.ReadWrite, FileShare.Read)
45324531

45334532
writePdb (options.dumpDebugInfo,
45344533
options.showTimes,
@@ -4558,7 +4557,9 @@ let writeBinaryInMemory (options: options, modul, normalizeAssemblyRefs) =
45584557
let pdbData, pdbInfoOpt, debugDirectoryChunk, debugDataChunk, debugChecksumPdbChunk, debugEmbeddedPdbChunk, debugDeterministicPdbChunk, textV2P, _mappings =
45594558
writeBinaryAux(stream, options, modul, normalizeAssemblyRefs)
45604559

4561-
let reopenOutput () = stream
4560+
let reopenOutput () =
4561+
stream.Seek(0, SeekOrigin.Begin) |> ignore
4562+
stream
45624563

45634564
let pdbBytes =
45644565
writePdb (options.dumpDebugInfo,

0 commit comments

Comments
 (0)