Skip to content

Commit

Permalink
Merge main to release/dev16.8 (#10120)
Browse files Browse the repository at this point in the history
* Bloody mutable fields (#10116)

* Fix 9150 --- #i nuget on desktop witout net48 ref assemblies fails. (#10114)

* signing (#10108)

Co-authored-by: Kevin Ransom (msft) <codecutter@hotmail.com>
  • Loading branch information
dotnet-bot and KevinRansom authored Sep 14, 2020
1 parent 71b8192 commit 038fd2f
Show file tree
Hide file tree
Showing 14 changed files with 383 additions and 344 deletions.
2 changes: 1 addition & 1 deletion src/absil/il.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ type ILScopeRef =
/// A reference to the type in the current module
| Local
/// A reference to a type in a module in the same assembly
| Module of ILModuleRef
| Module of ILModuleRef
/// A reference to a type in another assembly
| Assembly of ILAssemblyRef
/// A reference to a type in the primary assembly
Expand Down
335 changes: 328 additions & 7 deletions src/absil/ilsign.fs

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions src/absil/ilsign.fsi
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.

/// Functions associated with signing il assemblies which
/// vary between supported implementations of the CLI Common Language
/// Runtime, e.g. between the SSCLI, Mono and the Microsoft CLR.
///
module internal FSharp.Compiler.AbstractIL.Internal.StrongNameSign

//---------------------------------------------------------------------
// Strong name signing
//---------------------------------------------------------------------
[<Sealed>]
type ILStrongNameSigner =
member PublicKey: byte[]
static member OpenPublicKeyOptions: string -> bool -> ILStrongNameSigner
static member OpenPublicKey: byte[] -> ILStrongNameSigner
static member OpenKeyPairFile: string -> ILStrongNameSigner
static member OpenKeyContainer: string -> ILStrongNameSigner
member Close: unit -> unit
member IsFullySigned: bool
member PublicKey: byte[]
member SignatureSize: int
member SignFile: string -> unit
232 changes: 0 additions & 232 deletions src/absil/ilsupp.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1118,235 +1118,3 @@ let pdbVariableGetSignature (variable: PdbVariable) : byte[] =
let pdbVariableGetAddressAttributes (variable: PdbVariable) : (int32 * int32) =
(int32 variable.symVariable.AddressKind, variable.symVariable.AddressField1)
#endif

// Key signing
type keyContainerName = string
type keyPair = byte[]
type pubkey = byte[]
type pubkeyOptions = byte[] * bool

#if FX_NO_CORHOST_SIGNER

let signerOpenPublicKeyFile filePath = FileSystem.ReadAllBytesShim filePath

let signerOpenKeyPairFile filePath = FileSystem.ReadAllBytesShim filePath

let signerGetPublicKeyForKeyPair (kp: keyPair) : pubkey =
let reply = (StrongNameSign.getPublicKeyForKeyPair kp)
reply

let signerGetPublicKeyForKeyContainer (_kcName: keyContainerName) : pubkey =
raise (NotImplementedException("signerGetPublicKeyForKeyContainer is not yet implemented"))

let signerCloseKeyContainer (_kc: keyContainerName) : unit =
raise (NotImplementedException("signerCloseKeyContainer is not yet implemented"))

let signerSignatureSize (pk: pubkey) : int =
(StrongNameSign.signatureSize pk)

let signerSignFileWithKeyPair (fileName: string) (kp: keyPair) : unit =
(StrongNameSign.signFile fileName kp)

let signerSignFileWithKeyContainer (_fileName: string) (_kcName: keyContainerName) : unit =
raise (NotImplementedException("signerSignFileWithKeyContainer is not yet implemented"))

#else
// New mscoree functionality
// This type represents methods that we don't currently need, so I'm leaving unimplemented
type UnusedCOMMethod = unit -> unit
[<System.Security.SecurityCritical; Interface>]
[<ComImport; InterfaceType(ComInterfaceType.InterfaceIsIUnknown); Guid("D332DB9E-B9B3-4125-8207-A14884F53216")>]
type ICLRMetaHost =
[<MethodImpl(MethodImplOptions.InternalCall, MethodCodeType=MethodCodeType.Runtime)>]
abstract GetRuntime:
[<In; MarshalAs(UnmanagedType.LPWStr)>] version: string *
[<In; MarshalAs(UnmanagedType.LPStruct)>] interfaceId: System.Guid -> [<MarshalAs(UnmanagedType.Interface)>] System.Object

// Methods that we don't need are stubbed out for now...
abstract GetVersionFromFile: UnusedCOMMethod
abstract EnumerateInstalledRuntimes: UnusedCOMMethod
abstract EnumerateLoadedRuntimes: UnusedCOMMethod
abstract Reserved01: UnusedCOMMethod

// We don't currently support ComConversionLoss
[<System.Security.SecurityCritical; Interface>]
[<ComImport; ComConversionLoss; InterfaceType(ComInterfaceType.InterfaceIsIUnknown); Guid("9FD93CCF-3280-4391-B3A9-96E1CDE77C8D")>]
type ICLRStrongName =
// Methods that we don't need are stubbed out for now...
abstract GetHashFromAssemblyFile: UnusedCOMMethod
abstract GetHashFromAssemblyFileW: UnusedCOMMethod
abstract GetHashFromBlob: UnusedCOMMethod
abstract GetHashFromFile: UnusedCOMMethod
abstract GetHashFromFileW: UnusedCOMMethod
abstract GetHashFromHandle: UnusedCOMMethod
abstract StrongNameCompareAssemblies: UnusedCOMMethod

[<MethodImpl(MethodImplOptions.InternalCall, MethodCodeType=MethodCodeType.Runtime)>]
abstract StrongNameFreeBuffer: [<In>] pbMemory: nativeint -> unit

abstract StrongNameGetBlob: UnusedCOMMethod
abstract StrongNameGetBlobFromImage: UnusedCOMMethod

[<MethodImpl(MethodImplOptions.InternalCall, MethodCodeType=MethodCodeType.Runtime)>]
abstract StrongNameGetPublicKey :
[<In; MarshalAs(UnmanagedType.LPWStr)>] pwzKeyContainer: string *
[<In; MarshalAs(UnmanagedType.LPArray, SizeParamIndex=2s)>] pbKeyBlob: byte[] *
[<In; MarshalAs(UnmanagedType.U4)>] cbKeyBlob: uint32 *
[<Out>] ppbPublicKeyBlob: nativeint byref *
[<Out; MarshalAs(UnmanagedType.U4)>] pcbPublicKeyBlob: uint32 byref -> unit

abstract StrongNameHashSize: UnusedCOMMethod

[<MethodImpl(MethodImplOptions.InternalCall, MethodCodeType=MethodCodeType.Runtime)>]
abstract StrongNameKeyDelete: [<In; MarshalAs(UnmanagedType.LPWStr)>] pwzKeyContainer: string -> unit

abstract StrongNameKeyGen: UnusedCOMMethod
abstract StrongNameKeyGenEx: UnusedCOMMethod
abstract StrongNameKeyInstall: UnusedCOMMethod

[<MethodImpl(MethodImplOptions.InternalCall, MethodCodeType=MethodCodeType.Runtime)>]
abstract StrongNameSignatureGeneration :
[<In; MarshalAs(UnmanagedType.LPWStr)>] pwzFilePath: string *
[<In; MarshalAs(UnmanagedType.LPWStr)>] pwzKeyContainer: string *
[<In; MarshalAs(UnmanagedType.LPArray, SizeParamIndex=3s)>] pbKeyBlob: byte [] *
[<In; MarshalAs(UnmanagedType.U4)>] cbKeyBlob: uint32 *
[<Out>] ppbSignatureBlob: nativeint *
[<MarshalAs(UnmanagedType.U4)>] pcbSignatureBlob: uint32 byref -> unit

abstract StrongNameSignatureGenerationEx: UnusedCOMMethod

[<MethodImpl(MethodImplOptions.InternalCall, MethodCodeType=MethodCodeType.Runtime)>]
abstract StrongNameSignatureSize :
[<In; MarshalAs(UnmanagedType.LPArray, SizeParamIndex=1s)>] pbPublicKeyBlob: byte[] *
[<In; MarshalAs(UnmanagedType.U4)>] cbPublicKeyBlob: uint32 *
[<Out; MarshalAs(UnmanagedType.U4)>] pcbSize: uint32 byref -> unit

abstract StrongNameSignatureVerification: UnusedCOMMethod

[<MethodImpl(MethodImplOptions.InternalCall, MethodCodeType=MethodCodeType.Runtime)>]
abstract StrongNameSignatureVerificationEx :
[<In; MarshalAs(UnmanagedType.LPWStr)>] pwzFilePath: string *
[<In; MarshalAs(UnmanagedType.I1)>] fForceVerification: bool *
[<In; MarshalAs(UnmanagedType.I1)>] pfWasVerified: bool byref -> [<MarshalAs(UnmanagedType.I1)>] bool

abstract StrongNameSignatureVerificationFromImage: UnusedCOMMethod
abstract StrongNameTokenFromAssembly: UnusedCOMMethod
abstract StrongNameTokenFromAssemblyEx: UnusedCOMMethod
abstract StrongNameTokenFromPublicKey: UnusedCOMMethod


[<System.Security.SecurityCritical; Interface>]
[<ComImport; InterfaceType(ComInterfaceType.InterfaceIsIUnknown); Guid("BD39D1D2-BA2F-486A-89B0-B4B0CB466891")>]
type ICLRRuntimeInfo =
// REVIEW: Methods that we don't need will be stubbed out for now...
abstract GetVersionString: unit -> unit
abstract GetRuntimeDirectory: unit -> unit
abstract IsLoaded: unit -> unit
abstract LoadErrorString: unit -> unit
abstract LoadLibrary: unit -> unit
abstract GetProcAddress: unit -> unit

[<MethodImpl(MethodImplOptions.InternalCall, MethodCodeType=MethodCodeType.Runtime)>]
abstract GetInterface :
[<In; MarshalAs(UnmanagedType.LPStruct)>] coClassId: System.Guid *
[<In; MarshalAs(UnmanagedType.LPStruct)>] interfaceId: System.Guid -> [<MarshalAs(UnmanagedType.Interface)>]System.Object

[<System.Security.SecurityCritical>]
[<DllImport("mscoree.dll", SetLastError = true, PreserveSig=false, EntryPoint="CreateInterface")>]
let CreateInterface (
([<MarshalAs(UnmanagedType.LPStruct)>] _clsidguid: System.Guid),
([<MarshalAs(UnmanagedType.LPStruct)>] _guid: System.Guid),
([<MarshalAs(UnmanagedType.Interface)>] _metaHost :
ICLRMetaHost byref)) : unit = failwith "CreateInterface"

let signerOpenPublicKeyFile filePath = FileSystem.ReadAllBytesShim filePath

let signerOpenKeyPairFile filePath = FileSystem.ReadAllBytesShim filePath

let mutable iclrsn: ICLRStrongName option = None
let getICLRStrongName () =
match iclrsn with
| None ->
let CLSID_CLRStrongName = System.Guid(0xB79B0ACDu, 0xF5CDus, 0x409bus, 0xB5uy, 0xA5uy, 0xA1uy, 0x62uy, 0x44uy, 0x61uy, 0x0Buy, 0x92uy)
let IID_ICLRStrongName = System.Guid(0x9FD93CCFu, 0x3280us, 0x4391us, 0xB3uy, 0xA9uy, 0x96uy, 0xE1uy, 0xCDuy, 0xE7uy, 0x7Cuy, 0x8Duy)
let CLSID_CLRMetaHost = System.Guid(0x9280188Du, 0x0E8Eus, 0x4867us, 0xB3uy, 0x0Cuy, 0x7Fuy, 0xA8uy, 0x38uy, 0x84uy, 0xE8uy, 0xDEuy)
let IID_ICLRMetaHost = System.Guid(0xD332DB9Eu, 0xB9B3us, 0x4125us, 0x82uy, 0x07uy, 0xA1uy, 0x48uy, 0x84uy, 0xF5uy, 0x32uy, 0x16uy)
let clrRuntimeInfoGuid = System.Guid(0xBD39D1D2u, 0xBA2Fus, 0x486aus, 0x89uy, 0xB0uy, 0xB4uy, 0xB0uy, 0xCBuy, 0x46uy, 0x68uy, 0x91uy)

let runtimeVer = System.Runtime.InteropServices.RuntimeEnvironment.GetSystemVersion()
let mutable metaHost = Unchecked.defaultof<ICLRMetaHost>
CreateInterface(CLSID_CLRMetaHost, IID_ICLRMetaHost, &metaHost)
if Unchecked.defaultof<ICLRMetaHost> = metaHost then
failwith "Unable to obtain ICLRMetaHost object - check freshness of mscoree.dll"
let runtimeInfo = metaHost.GetRuntime(runtimeVer, clrRuntimeInfoGuid) :?> ICLRRuntimeInfo
let sn = runtimeInfo.GetInterface(CLSID_CLRStrongName, IID_ICLRStrongName) :?> ICLRStrongName
if Unchecked.defaultof<ICLRStrongName> = sn then
failwith "Unable to obtain ICLRStrongName object"
iclrsn <- Some sn
sn
| Some sn -> sn

let signerGetPublicKeyForKeyPair kp =
if runningOnMono then
let snt = System.Type.GetType("Mono.Security.StrongName")
let sn = System.Activator.CreateInstance(snt, [| box kp |])
snt.InvokeMember("PublicKey", (BindingFlags.GetProperty ||| BindingFlags.Instance ||| BindingFlags.Public), null, sn, [| |], Globalization.CultureInfo.InvariantCulture) :?> byte[]
else
let mutable pSize = 0u
let mutable pBuffer: nativeint = (nativeint)0
let iclrSN = getICLRStrongName()

iclrSN.StrongNameGetPublicKey(Unchecked.defaultof<string>, kp, (uint32) kp.Length, &pBuffer, &pSize) |> ignore
let mutable keybuffer: byte [] = Bytes.zeroCreate (int pSize)
// Copy the marshalled data over - we'll have to free this ourselves
Marshal.Copy(pBuffer, keybuffer, 0, int pSize)
iclrSN.StrongNameFreeBuffer pBuffer |> ignore
keybuffer

let signerGetPublicKeyForKeyContainer kc =
let mutable pSize = 0u
let mutable pBuffer: nativeint = (nativeint)0
let iclrSN = getICLRStrongName()
iclrSN.StrongNameGetPublicKey(kc, Unchecked.defaultof<byte[]>, 0u, &pBuffer, &pSize) |> ignore
let mutable keybuffer: byte [] = Bytes.zeroCreate (int pSize)
// Copy the marshalled data over - we'll have to free this ourselves later
Marshal.Copy(pBuffer, keybuffer, 0, int pSize)
iclrSN.StrongNameFreeBuffer pBuffer |> ignore
keybuffer

let signerCloseKeyContainer kc =
let iclrSN = getICLRStrongName()
iclrSN.StrongNameKeyDelete kc |> ignore

let signerSignatureSize (pk: byte[]) =
if runningOnMono then
if pk.Length > 32 then pk.Length - 32 else 128
else
let mutable pSize = 0u
let iclrSN = getICLRStrongName()
iclrSN.StrongNameSignatureSize(pk, uint32 pk.Length, &pSize) |> ignore
int pSize

let signerSignFileWithKeyPair fileName kp =
if runningOnMono then
let snt = System.Type.GetType("Mono.Security.StrongName")
let sn = System.Activator.CreateInstance(snt, [| box kp |])
let conv (x: obj) = if (unbox x: bool) then 0 else -1
snt.InvokeMember("Sign", (BindingFlags.InvokeMethod ||| BindingFlags.Instance ||| BindingFlags.Public), null, sn, [| box fileName |], Globalization.CultureInfo.InvariantCulture) |> conv |> check "Sign"
snt.InvokeMember("Verify", (BindingFlags.InvokeMethod ||| BindingFlags.Instance ||| BindingFlags.Public), null, sn, [| box fileName |], Globalization.CultureInfo.InvariantCulture) |> conv |> check "Verify"
else
let mutable pcb = 0u
let mutable ppb = (nativeint)0
let mutable ok = false
let iclrSN = getICLRStrongName()
iclrSN.StrongNameSignatureGeneration(fileName, Unchecked.defaultof<string>, kp, uint32 kp.Length, ppb, &pcb) |> ignore
iclrSN.StrongNameSignatureVerificationEx(fileName, true, &ok) |> ignore

let signerSignFileWithKeyContainer fileName kcName =
let mutable pcb = 0u
let mutable ppb = (nativeint)0
let mutable ok = false
let iclrSN = getICLRStrongName()
iclrSN.StrongNameSignatureGeneration(fileName, kcName, Unchecked.defaultof<byte[]>, 0u, ppb, &pcb) |> ignore
iclrSN.StrongNameSignatureVerificationEx(fileName, true, &ok) |> ignore
#endif
19 changes: 1 addition & 18 deletions src/absil/ilsupp.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ open System.Runtime.InteropServices
#else
open System.Diagnostics.SymbolStore
#endif

open Internal.Utilities
open FSharp.Compiler.AbstractIL
open FSharp.Compiler.AbstractIL.Internal
Expand Down Expand Up @@ -109,21 +110,3 @@ val pdbSetMethodRange: PdbWriter -> PdbDocumentWriter -> int -> int -> PdbDocume
val pdbDefineSequencePoints: PdbWriter -> PdbDocumentWriter -> (int * int * int * int * int) array -> unit
val pdbWriteDebugInfo: PdbWriter -> idd
#endif

//---------------------------------------------------------------------
// Strong name signing
//---------------------------------------------------------------------

type keyContainerName = string
type keyPair = byte[]
type pubkey = byte[]
type pubkeyOptions = byte[] * bool

val signerOpenPublicKeyFile: string -> pubkey
val signerOpenKeyPairFile: string -> keyPair
val signerSignatureSize: pubkey -> int
val signerGetPublicKeyForKeyPair: keyPair -> pubkey
val signerGetPublicKeyForKeyContainer: string -> pubkey
val signerCloseKeyContainer: keyContainerName -> unit
val signerSignFileWithKeyPair: string -> keyPair -> unit
val signerSignFileWithKeyContainer: string -> keyContainerName -> unit
65 changes: 1 addition & 64 deletions src/absil/ilwrite.fs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,10 @@ open FSharp.Compiler.AbstractIL.Internal.BinaryConstants
open FSharp.Compiler.AbstractIL.Internal.Support
open FSharp.Compiler.AbstractIL.Internal.Library
open FSharp.Compiler.AbstractIL.Internal.Utils
open FSharp.Compiler.AbstractIL.Internal.StrongNameSign
open FSharp.Compiler.AbstractIL.ILPdbWriter
open FSharp.Compiler.ErrorLogger
open FSharp.Compiler.Range
#if FX_NO_CORHOST_SIGNER
open FSharp.Compiler.AbstractIL.Internal.StrongNameSign
#endif


#if DEBUG
let showEntryLookups = false
Expand Down Expand Up @@ -146,66 +143,6 @@ let applyFixup32 (data: byte[]) offset v =
data.[offset+2] <- b2 v
data.[offset+3] <- b3 v

//---------------------------------------------------------------------
// Strong name signing
//---------------------------------------------------------------------

type ILStrongNameSigner =
| PublicKeySigner of Support.pubkey
| PublicKeyOptionsSigner of Support.pubkeyOptions
| KeyPair of Support.keyPair
| KeyContainer of Support.keyContainerName

static member OpenPublicKeyOptions s p = PublicKeyOptionsSigner((Support.signerOpenPublicKeyFile s), p)

static member OpenPublicKey pubkey = PublicKeySigner pubkey

static member OpenKeyPairFile s = KeyPair(Support.signerOpenKeyPairFile s)

static member OpenKeyContainer s = KeyContainer s

member s.Close() =
match s with
| PublicKeySigner _
| PublicKeyOptionsSigner _
| KeyPair _ -> ()
| KeyContainer containerName -> Support.signerCloseKeyContainer containerName

member s.IsFullySigned =
match s with
| PublicKeySigner _ -> false
| PublicKeyOptionsSigner pko -> let _, usePublicSign = pko
usePublicSign
| KeyPair _ | KeyContainer _ -> true

member s.PublicKey =
match s with
| PublicKeySigner pk -> pk
| PublicKeyOptionsSigner pko -> let pk, _ = pko
pk
| KeyPair kp -> Support.signerGetPublicKeyForKeyPair kp
| KeyContainer kn -> Support.signerGetPublicKeyForKeyContainer kn

member s.SignatureSize =
let pkSignatureSize pk =
try Support.signerSignatureSize pk
with e ->
failwith ("A call to StrongNameSignatureSize failed ("+e.Message+")")
0x80
match s with
| PublicKeySigner pk -> pkSignatureSize pk
| PublicKeyOptionsSigner pko -> let pk, _ = pko
pkSignatureSize pk
| KeyPair kp -> pkSignatureSize (Support.signerGetPublicKeyForKeyPair kp)
| KeyContainer kn -> pkSignatureSize (Support.signerGetPublicKeyForKeyContainer kn)

member s.SignFile file =
match s with
| PublicKeySigner _ -> ()
| PublicKeyOptionsSigner _ -> ()
| KeyPair kp -> Support.signerSignFileWithKeyPair file kp
| KeyContainer kn -> Support.signerSignFileWithKeyContainer file kn

//---------------------------------------------------------------------
// TYPES FOR TABLES
//---------------------------------------------------------------------
Expand Down
9 changes: 1 addition & 8 deletions src/absil/ilwrite.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,7 @@ open FSharp.Compiler.AbstractIL
open FSharp.Compiler.AbstractIL.Internal
open FSharp.Compiler.AbstractIL.IL
open FSharp.Compiler.AbstractIL.ILPdbWriter

[<Sealed>]
type ILStrongNameSigner =
member PublicKey: byte[]
static member OpenPublicKeyOptions: string -> bool -> ILStrongNameSigner
static member OpenPublicKey: byte[] -> ILStrongNameSigner
static member OpenKeyPairFile: string -> ILStrongNameSigner
static member OpenKeyContainer: string -> ILStrongNameSigner
open FSharp.Compiler.AbstractIL.Internal.StrongNameSign

type options =
{ ilg: ILGlobals
Expand Down
Loading

0 comments on commit 038fd2f

Please sign in to comment.