/* * Copyright (c) 2022 Winsider Seminars & Solutions, Inc. All rights reserved. * * This file is part of System Informer. * * Authors: * * dmex * */ namespace CustomBuildTool { using unsafe AuthenticodeDigestSignDelegate = delegate* unmanaged[Stdcall]; using unsafe AuthenticodeDigestSignExnDelegate = delegate* unmanaged[Stdcall]; [Flags] internal enum SignerSignEx3Flags : uint { NONE = 0x0, /// /// Exclude page hashes when creating SIP indirect data for the PE file. This flag takes precedence over the SPC_INC_PE_PAGE_HASHES_FLAG flag. /// SPC_EXC_PE_PAGE_HASHES_FLAG = 0x10, /// /// This value is not supported. /// SPC_INC_PE_IMPORT_ADDR_TABLE_FLAG = 0x20, /// /// This value is not supported. /// SPC_INC_PE_DEBUG_INFO_FLAG = 0x40, /// /// This value is not supported. /// SPC_INC_PE_RESOURCES_FLAG = 0x80, /// /// Include page hashes when creating SIP indirect data for the PE file. /// SPC_INC_PE_PAGE_HASHES_FLAG = 0x100, /// /// /// SPC_DIGEST_GENERATE_FLAG = 0x200, /// /// Digest signing will be done. /// SPC_DIGEST_SIGN_FLAG = 0x400, /// /// Define relaxed PE marker check semantic. Relaxed marker check flags, encoded as authenticated attribute /// SPC_RELAXED_PE_MARKER_CHECK_OBJID(1.3.6.1.4.1.311.2.6.1) of integer type. /// SPC_MARKER_CHECK_SKIP_SIP_INDIRECT_DATA_FLAG - If this flag is set, SIP_INDIRECT_DATA will be skipped for marker check. /// SPC_RELAXED_PE_MARKER_CHECK = 0x800, /// /// The signature will be nested. /// If you set this flag before any signature has been added, the generated signature will be added as the outer signature. /// If you do not set this flag, the generated signature replaces the outer signature, deleting all inner signatures. /// SIG_APPEND = 0x1000, /// /// /// SIG_SEAL = 0x2000, /// /// Digest signing will be done. The callers digest signing function will select and return the code signing certificate which performed the signing operation. /// SPC_DIGEST_SIGN_EX_FLAG = 0x4000 } [StructLayout(LayoutKind.Sequential)] internal unsafe struct SIGNER_SIGN_PARAMS_EX2 { internal SIGNER_SIGN_FLAGS Flags; internal SIGNER_SUBJECT_INFO* SubjectInfo; internal SIGNER_CERT* SignerCert; internal SIGNER_SIGNATURE_INFO* SignatureInfo; internal SIGNER_PROVIDER_INFO* ProviderInfo; internal SIGNER_TIMESTAMP_FLAGS TimestampFlags; internal byte* TimestampAlgorithmOid; internal char* TimestampURL; internal CRYPT_ATTRIBUTES* CryptAttrs; internal APPX_SIP_CLIENT_DATA* SipData; internal SIGNER_CONTEXT** SignerContext; internal CERT_STRONG_SIGN_PARA* CryptoPolicy; internal IntPtr Reserved; } [StructLayout(LayoutKind.Sequential)] internal unsafe struct SIGNER_SIGN_PARAMS_EX3 { internal SIGNER_SIGN_FLAGS Flags; internal SIGNER_SUBJECT_INFO* SubjectInfo; internal SIGNER_CERT* SignerCert; internal SIGNER_SIGNATURE_INFO* SignatureInfo; internal SIGNER_PROVIDER_INFO* ProviderInfo; internal SIGNER_TIMESTAMP_FLAGS TimestampFlags; internal byte* TimestampAlgorithmOid; internal char* TimestampURL; internal CRYPT_ATTRIBUTES* psRequest; internal SIGNER_DIGEST_SIGN_INFO_UNION* SignCallBack; internal SIGNER_CONTEXT** SignerContext; internal CERT_STRONG_SIGN_PARA* CryptoPolicy; internal IntPtr Reserved; } [StructLayout(LayoutKind.Sequential)] internal unsafe struct APPX_SIP_CLIENT_DATA { internal SIGNER_SIGN_EX_PARAMS* SignerParams; internal IntPtr AppxSipState; // IUnknown* } [StructLayout(LayoutKind.Explicit)] internal struct SIGNER_SIGN_EX_PARAMS { [FieldOffset(0)] internal SIGNER_SIGN_PARAMS_EX2 Ex2; [FieldOffset(0)] internal SIGNER_SIGN_PARAMS_EX3 Ex3; } /// /// The SIGNER_DIGEST_SIGN_INFO_V1 structure contains information about digest signing. /// It contains a pointer to a digest signing function implemented within the provided dll. /// https://learn.microsoft.com/en-us/windows/win32/seccrypto/signer-digest-sign-info-v1 /// [StructLayout(LayoutKind.Sequential)] internal unsafe struct SIGNER_DIGEST_SIGN_INFO_V1 { /// /// The size, in bytes, of the structure. /// internal uint Size; /// /// The pointer to a PFN_AUTHENTICODE_DIGEST_SIGN function. /// internal AuthenticodeDigestSignDelegate AuthenticodeDigestSign; /// /// Optional pointer to CRYPT_DATA_BLOB specifying metadata (opaque to SignerSignEx3). /// internal CRYPT_INTEGER_BLOB* MetadataBlob; } /// /// The SIGNER_DIGEST_SIGN_INFO_V2 structure contains information about digest signing. /// It contains a pointer to a digest signing function implemented within the provided dll, specified by the dwDigestSignChoice. /// https://learn.microsoft.com/en-us/windows/win32/seccrypto/signer-digest-sign-info-v2 /// [StructLayout(LayoutKind.Sequential)] internal unsafe struct SIGNER_DIGEST_SIGN_INFO_V2 { /// /// The size, in bytes, of the structure. /// internal uint Size; /// /// Pointer to the PFN_AUTHENTICODE_DIGEST_SIGN callback function. Required if the caller of SignerSignEx3 specifies SPC_DIGEST_SIGN_FLAG in the dwFlags parameter. /// internal AuthenticodeDigestSignDelegate AuthenticodeDigestSign; /// /// Pointer to the PFN_AUTHENTICODE_DIGEST_SIGN_EX callback function. Required if the caller of SignerSignEx3 specifies SPC_DIGEST_SIGN_EX_FLAG in the dwFlags parameter. /// internal AuthenticodeDigestSignExnDelegate AuthenticodeDigestSignEx; /// /// Optional pointer to CRYPT_DATA_BLOB specifying metadata. /// internal CRYPT_INTEGER_BLOB* MetadataBlob; } [StructLayout(LayoutKind.Explicit)] internal struct SIGNER_DIGEST_SIGN_INFO_UNION { [FieldOffset(0)] internal SIGNER_DIGEST_SIGN_INFO_V1 V1; [FieldOffset(0)] internal SIGNER_DIGEST_SIGN_INFO_V2 V2; } internal enum SIGNER_DIGEST_CHOICE : uint { DIGEST_SIGN = 1, // PFN_AUTHENTICODE_DIGEST_SIGN DIGEST_SIGN_WITHFILEHANDLE = 2, // PFN_AUTHENTICODE_DIGEST_SIGN_WITHFILEHANDLE DIGEST_SIGN_EX = 3, // PFN_AUTHENTICODE_DIGEST_SIGN_EX DIGEST_SIGN_EX_WITHFILEHANDLE = 4 // PFN_AUTHENTICODE_DIGEST_SIGN_EX_WITHFILEHANDLE } }