' ########################################################################################
' Microsoft Windows
' File: StrongName.inc
' Contents: Wrappers for signing and hashing functions needed to implement strong names
' Copyright (c) 2011 Jos? Roca
' Portions Copyright (c) Microsoft Corporation.
' All Rights Reserved.
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
' EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
' MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ########################################################################################
#IF NOT %DEF(PBX_StrongName_PBI)
#DEFINE PBX_StrongName_PBI
%EQUATE `STRONGNAME_INC = 1
#INCLUDE ONCE "Core/Windows.inc"
#INCLUDE ONCE "Legacy/Platform/WinCrypt.inc"
#INCLUDE ONCE "Legacy/COM/Ole2.inc"
#INCLUDE ONCE "Legacy/System/CorError.inc"
'// Public key blob binary format.
' // Size = 16 bytes
TYPE PublicKeyBlob DWORD
 SigAlgID AS DWORD ' unsigned int // (ALG_ID) signature algorithm used to create the signature
 HashAlgID AS DWORD ' unsigned int // (ALG_ID) hash algorithm used to create the signature
 cbPublicKey AS DWORD ' ULONG // length of the key in bytes
 PublicKey(0) AS BYTE ' BYTE // variable length byte array containing the key value in format output by CryptoAPI
END TYPE
'// Location in the registry (under HKLM) that strong name configuration info is
'// stored.
$SN_CONFIG_KEY = "Software\Microsoft\StrongName"
$SN_CONFIG_CSP = "CSP" ' // REG_SZ
$SN_CONFIG_MACHINE_KEYSET = "MachineKeyset" ' // REG_DWORD
$SN_CONFIG_KEYSPEC = "KeySpec" ' // REG_DWORD
$SN_CONFIG_HASH_ALG = "HashAlgorithm" ' // REG_DWORD
$SN_CONFIG_SIGN_ALG = "SignAlgorithm" ' // REG_DWORD
$SN_CONFIG_VERIFICATION = "Verification" ' // Registry subkey
$SN_CONFIG_USERLIST = "UserList" ' // REG_MULTI_SZ
$SN_CONFIG_CACHE_VERIFY = "CacheVerify" ' // REG_DWORD
$$SN_CONFIG_KEY_W = "Software\Microsoft\StrongName"$$
$$SN_CONFIG_CSP_W = "CSP"$$ ' // REG_SZ
$$SN_CONFIG_MACHINE_KEYSET_W = "MachineKeyset"$$ ' // REG_DWORD
$$SN_CONFIG_KEYSPEC_W = "KeySpec"$$ ' // REG_DWORD
$$SN_CONFIG_HASH_ALG_W = "HashAlgorithm"$$ ' // REG_DWORD
$$SN_CONFIG_SIGN_ALG_W = "SignAlgorithm"$$ ' // REG_DWORD
$$SN_CONFIG_VERIFICATION_W = "Verification"$$ ' // Registry subkey
$$SN_CONFIG_USERLIST_W = "UserList"$$ ' // REG_MULTI_SZ
$$SN_CONFIG_CACHE_VERIFY_W = "CacheVerify"$$ ' // REG_DWORD
'// VM related registry locations (Note, these values are under HKLM\Software\Microsoft\.NETFramework, rather
'// than SN_CONFIG_KEY
$SN_CONFIG_BYPASS_POLICY = "AllowStrongNameBypass" ' // REG_DWORD
$$SN_CONFIG_BYPASS_POLICY_W = "AllowStrongNameBypass"$$ ' // REG_DWORD
'#define SNAPI BOOLEAN __stdcall
'#define SNAPI_(_type) _type __stdcall
'// Return last error.
DECLARE FUNCTION StrongNameErrorInfo LIB "mscoree.dll" ALIAS "StrongNameErrorInfo" () AS QUAD
'// Free buffer allocated by routines below.
DECLARE SUB StrongNameFreeBuffer LIB "mscoree.dll" ALIAS "StrongNameFreeBuffer" ( BYVAL pbMemory AS BYTE PTR ) ' void
'// Generate a new key pair for strong name use.
DECLARE FUNCTION StrongNameKeyGen LIB "mscoree.dll" ALIAS "StrongNameKeyGen" ( BYREF wszKeyContainer AS WSTRINGZ , BYVAL dwFlags AS QUAD , BYREF ppbKeyBlob AS QUAD , BYREF pcbKeyBlob AS QUAD ) AS BYTE ' BOOLEAN
'// Generate a new key pair with the specified key size for strong name use.
DECLARE FUNCTION StrongNameKeyGenEx LIB "mscoree.dll" ALIAS "StrongNameKeyGenEx" ( BYREF wszKeyContainer AS WSTRINGZ , BYVAL dwFlags AS QUAD , BYVAL dwKeySize AS QUAD , BYREF ppbKeyBlob AS QUAD , BYREF pcbKeyBlob AS QUAD ) AS BYTE ' BOOLEAN
'// Flags for StrongNameKeyGen.
%EQUATE `SN_LEAVE_KEY = &H00000001 ' // Leave key pair registered with CSP
'// Import key pair into a key container.
DECLARE FUNCTION StrongNameKeyInstall LIB "mscoree.dll" ALIAS "StrongNameKeyInstall" ( BYREF wszKeyContainer AS WSTRINGZ , BYVAL pbKeyBlob AS BYTE PTR , BYVAL cbKeyBlob AS QUAD ) AS BYTE ' BOOLEAN
'// Delete a key pair.
DECLARE FUNCTION StrongNameKeyDelete LIB "mscoree.dll" ALIAS "StrongNameKeyDelete" ( BYREF wszKeyContainer AS WSTRINGZ ) AS BYTE ' BOOLEAN
'// Retrieve the public portion of a key pair.
DECLARE FUNCTION StrongNameGetPublicKey LIB "mscoree.dll" ALIAS "StrongNameGetPublicKey" ( BYREF wszKeyContainer AS WSTRINGZ , BYVAL pbKeyBlob AS BYTE PTR , BYVAL cbKeyBlob AS QUAD , BYREF ppbPublicKeyBlob AS QUAD , BYREF pcbPublicKeyBlob AS QUAD ) AS BYTE ' BOOLEAN
'// Hash and sign a manifest.
DECLARE FUNCTION StrongNameSignatureGeneration LIB "mscoree.dll" ALIAS "StrongNameSignatureGeneration" ( BYREF wszFilePath AS WSTRINGZ , BYREF wszKeyContainer AS WSTRINGZ , BYVAL pbKeyBlob AS BYTE PTR , BYVAL cbKeyBlob AS QUAD , BYREF ppbSignatureBlob AS QUAD , BYREF pcbSignatureBlob AS QUAD ) AS BYTE ' BOOLEAN
DECLARE FUNCTION StrongNameSignatureGenerationEx LIB "mscoree.dll" ALIAS "StrongNameSignatureGenerationEx" ( BYREF wszFilePath AS WSTRINGZ , BYREF wszKeyContainer AS WSTRINGZ , BYVAL pbKeyBlob AS BYTE PTR , BYVAL cbKeyBlob AS QUAD , BYREF ppbSignatureBlob AS QUAD , BYREF pcbSignatureBlob AS QUAD , BYVAL dwFlags AS QUAD ) AS BYTE ' BOOLEAN
#IF %DEF(%FEATURE_PLATFORM_ASSEMBLY_ALLOWLIST)
DECLARE FUNCTION StrongNameSetPlatformSignatureSequence LIB "mscoree.dll" ALIAS "StrongNameSetPlatformSignatureSequence" ( BYREF wszFilePath AS WSTRINGZ , BYVAL flags AS QUAD ) AS BYTE ' BOOLEAN
DECLARE FUNCTION StrongNameHasPlatformSignatureSequence LIB "mscoree.dll" ALIAS "StrongNameHasPlatformSignatureSequence" ( BYREF wszFilePath AS WSTRINGZ ) AS BYTE ' BOOLEAN
#ENDIF ' // FEATURE_PLATFORM_ASSEMBLY_ALLOWLIST
%EQUATE `SN_SIGN_ALL_FILES = &H00000001 ' // Rehash all linked modules as well as resigning the manifest
%EQUATE `SN_TEST_SIGN = &H00000002 ' // Test sign the assembly
'// Create a strong name token from an assembly file.
DECLARE FUNCTION StrongNameTokenFromAssembly LIB "mscoree.dll" ALIAS "StrongNameTokenFromAssembly" ( BYREF wszFilePath AS WSTRINGZ , BYREF ppbStrongNameToken AS QUAD , BYREF pcbStrongNameToken AS QUAD ) AS BYTE ' BOOLEAN
'// Create a strong name token from an assembly file and additionally return the full public key.
DECLARE FUNCTION StrongNameTokenFromAssemblyEx LIB "mscoree.dll" ALIAS "StrongNameTokenFromAssemblyEx" ( BYREF wszFilePath AS WSTRINGZ , BYREF ppbStrongNameToken AS QUAD , BYREF pcbStrongNameToken AS QUAD , BYREF ppbPublicKeyBlob AS QUAD , BYREF pcbPublicKeyBlob AS QUAD ) AS BYTE ' BOOLEAN
'// Create a strong name token from a public key blob.
DECLARE FUNCTION StrongNameTokenFromPublicKey LIB "mscoree.dll" ALIAS "StrongNameTokenFromPublicKey" ( BYVAL pbPublicKeyBlob AS BYTE PTR , BYVAL cbPublicKeyBlob AS QUAD , BYREF ppbStrongNameToken AS QUAD , BYREF pcbStrongNameToken AS QUAD ) AS BYTE ' BOOLEAN
'// Verify a strong name/manifest against a public key blob.
DECLARE FUNCTION StrongNameSignatureVerification LIB "mscoree.dll" ALIAS "StrongNameSignatureVerification" ( BYREF wszFilePath AS WSTRINGZ , BYVAL dwInFlags AS QUAD , BYREF pdwOutFlags AS QUAD ) AS BYTE ' BOOLEAN
'// Verify a strong name/manifest against a public key blob.
DECLARE FUNCTION StrongNameSignatureVerificationEx LIB "mscoree.dll" ALIAS "StrongNameSignatureVerificationEx" ( BYREF wszFilePath AS WSTRINGZ , BYVAL fForceVerification AS BYTE , BYREF pfWasVerified AS BYTE ) AS BYTE ' BOOLEAN
'// Verify a strong name/manifest against a public key blob.
DECLARE FUNCTION StrongNameSignatureVerificationEx2 LIB "mscoree.dll" ALIAS "StrongNameSignatureVerificationEx2" ( BYREF wszFilePath AS WSTRINGZ , BYVAL fForceVerification AS BYTE , BYREF pbEcmaPublicKey AS BYTE , BYVAL cbEcmaPublicKey AS QUAD , BYREF pfWasVerified AS BYTE ) AS BYTE ' BOOLEAN
'// Verify a strong name/manifest against a public key blob when the assembly is
'// already memory mapped.
DECLARE FUNCTION StrongNameSignatureVerificationFromImage LIB "mscoree.dll" ALIAS "StrongNameSignatureVerificationFromImage" ( BYVAL pbBase AS BYTE PTR , BYVAL dwLength AS QUAD , BYVAL dwInFlags AS QUAD , BYREF pdwOutFlags AS QUAD ) AS BYTE ' BOOLEAN
'// Flags for use with the verify routines.
%EQUATE `SN_INFLAG_FORCE_VER = &H00000001 ' // verify even if settings in the registry disable it
%EQUATE `SN_INFLAG_INSTALL = &H00000002 ' // verification is the first (on entry to the cache)
%EQUATE `SN_INFLAG_ADMIN_ACCESS = &H00000004 ' // cache protects assembly from all but admin access
%EQUATE `SN_INFLAG_USER_ACCESS = &H00000008 ' // cache protects user's assembly from other users
%EQUATE `SN_INFLAG_ALL_ACCESS = &H00000010 ' // cache provides no access restriction guarantees
#IF %DEF(%FEATURE_PLATFORM_ASSEMBLY_ALLOWLIST)
%EQUATE `SN_INFLAG_GAC_ASSEMBLY = &H00000020 ' // Flag that indicates the assembly is from the "GAC"
%EQUATE `SN_INFLAG_MAPPED_PEFILE = &H00000040 ' // Flag that indicates the assembly PE File is memory-mapped (not flat)
%EQUATE `SN_INFLAG_RELOCATED_PEFILE = &H00000080 ' // Flag that indicates the assembly PE File is memory-mapped (not flat) and relocated
#ENDIF ' // FEATURE_PLATFORM_ASSEMBLY_ALLOWLIST
%EQUATE `SN_INFLAG_RUNTIME = &H80000000 ' // internal debugging use only
%EQUATE `SN_OUTFLAG_WAS_VERIFIED = &H00000001 ' // set to false if verify succeeded due to registry settings
%EQUATE `SN_OUTFLAG_MICROSOFT_SIGNATURE = &H00000002 ' // set if the public key corresponds to SN_THE_KEY
#IF %DEF(%FEATURE_STRONGNAME_TESTKEY_ALLOWED)
%EQUATE `SN_OUTFLAG_TESTKEY_SIGNATURE = &H00000004 ' // set if the public key corresponds to SN_THE_TESTKEY
#ENDIF ' // FEATURE_STRONGNAME_TESTKEY_ALLOWED
%EQUATE `SN_OUTFLAG_SILVERLIGHT_PLATFORM_SIGNATURE = &H00000008 ' // set if the public key corresponds to the Silverlight Platform key
'// Verify that two assemblies differ only by signature blob.
DECLARE FUNCTION StrongNameCompareAssemblies LIB "mscoree.dll" ALIAS "StrongNameCompareAssemblies" ( BYREF wszAssembly1 AS WSTRINGZ , BYREF wszAssembly2 AS WSTRINGZ , BYREF pdwResult AS QUAD ) AS BYTE ' BOOLEAN
%EQUATE `SN_CMP_DIFFERENT = 0 ' // Assemblies contain different data
%EQUATE `SN_CMP_IDENTICAL = 1 ' // Assemblies are exactly the same, even signatures
%EQUATE `SN_CMP_SIGONLY = 2 ' // Assemblies differ only by signature (and checksum etc.)
'// Compute the size of buffer needed to hold a hash for a given hash algorithm.
DECLARE FUNCTION StrongNameHashSize LIB "mscoree.dll" ALIAS "StrongNameHashSize" ( BYVAL ulHashAlg AS QUAD , BYREF pcbSize AS QUAD ) AS BYTE ' BOOLEAN
'// Compute the size that needs to be allocated for a signature in an assembly.
DECLARE FUNCTION StrongNameSignatureSize LIB "mscoree.dll" ALIAS "StrongNameSignatureSize" ( BYVAL pbPublicKeyBlob AS BYTE PTR , BYVAL cbPublicKeyBlob AS QUAD , BYREF pcbSize AS QUAD ) AS BYTE ' BOOLEAN
DECLARE FUNCTION GetHashFromAssemblyFile LIB "mscoree.dll" ALIAS "GetHashFromAssemblyFile" ( BYREF szFilePath AS ASCIIZ , BYREF piHashAlg AS QUAD , BYREF pbHash AS QUAD , BYVAL cchHash AS QUAD , BYREF pchHash AS QUAD ) AS QUAD
DECLARE FUNCTION GetHashFromAssemblyFileW LIB "mscoree.dll" ALIAS "GetHashFromAssemblyFileW" ( BYREF wszFilePath AS WSTRINGZ
 , BYREF piHashAlg AS DWORD
 , BYREF pbHash AS DWORD
 , BYVAL cchHash AS DWORD
 , BYREF pchHash AS DWORD
 ) AS DWORD
DECLARE FUNCTION GetHashFromFile LIB "mscoree.dll" ALIAS "GetHashFromFile" ( BYREF szFilePath AS ASCIIZ
 , BYREF piHashAlg AS DWORD
 , BYREF pbHash AS DWORD
 , BYVAL cchHash AS DWORD
 , BYREF pchHash AS DWORD
 ) AS DWORD
DECLARE FUNCTION GetHashFromFileW LIB "mscoree.dll" ALIAS "GetHashFromFileW" ( BYREF wszFilePath AS WSTRINGZ
 , BYREF piHashAlg AS DWORD
 , BYREF pbHash AS DWORD
 , BYVAL cchHash AS DWORD
 , BYREF pchHash AS DWORD
 ) AS DWORD
DECLARE FUNCTION GetHashFromHandle LIB "mscoree.dll" ALIAS "GetHashFromHandle" ( BYVAL hFile AS DWORD
 , BYREF piHashAlg AS DWORD
 , BYREF pbHash AS DWORD
 , BYVAL cchHash AS DWORD
 , BYREF pchHash AS DWORD
 ) AS DWORD
DECLARE FUNCTION GetHashFromBlob LIB "mscoree.dll" ALIAS "GetHashFromBlob" ( BYVAL pbBlob AS BYTE PTR
 , BYVAL cchBlob AS DWORD
 , BYREF piHashAlg AS DWORD
 , BYREF pbHash AS DWORD
 , BYVAL cchHash AS DWORD
 , BYREF pchHash AS DWORD
 ) AS DWORD
DECLARE FUNCTION StrongNameGetBlob LIB "mscoree.dll" ALIAS "StrongNameGetBlob" ( BYREF wszFilePath AS WSTRINGZ
 , BYVAL pbBlob AS BYTE PTR
 , BYREF pcbBlob AS DWORD
 ) AS BYTE ' BOOLEAN
DECLARE FUNCTION StrongNameGetBlobFromImage LIB "mscoree.dll" ALIAS "StrongNameGetBlobFromImage" ( BYVAL pbBase AS BYTE PTR , BYVAL dwLength AS QUAD , BYVAL pbBlob AS BYTE PTR , BYREF pcbBlob AS QUAD ) AS BYTE ' BOOLEAN
#ENDIF
