判断程序签名是否有效

#include <Softpub.h>
#include <wincrypt.h>
#include <wintrust.h>
// Link with the Wintrust.lib file.
#pragma comment (lib, "wintrust")
BOOL VerifyEmbeddedSignature(LPCWSTR pwszSourceFile)
{
    LONG lStatus;
    DWORD dwLastError;

    // Initialize the WINTRUST_FILE_INFO structure.

    WINTRUST_FILE_INFO FileData;
    memset(&FileData, 0, sizeof(FileData));
    FileData.cbStruct = sizeof(WINTRUST_FILE_INFO);
    FileData.pcwszFilePath = pwszSourceFile;
    FileData.hFile = NULL;
    FileData.pgKnownSubject = NULL;

    /*
    WVTPolicyGUID specifies the policy to apply on the file
    WINTRUST_ACTION_GENERIC_VERIFY_V2 policy checks:
    
    1) The certificate used to sign the file chains up to a root 
    certificate located in the trusted root certificate store. This 
    implies that the identity of the publisher has been verified by 
    a certification authority.
    
    2) In cases where user interface is displayed (which this example
    does not do), WinVerifyTrust will check for whether the  
    end entity certificate is stored in the trusted publisher store,  
    implying that the user trusts content from this publisher.
    
    3) The end entity certificate has sufficient permission to sign 
    code, as indicated by the presence of a code signing EKU or no 
    EKU.
    */

    GUID WVTPolicyGUID = WINTRUST_ACTION_GENERIC_VERIFY_V2;
    WINTRUST_DATA WinTrustData;

    // Initialize the WinVerifyTrust input data structure.

    // Default all fields to 0.
    memset(&WinTrustData, 0, sizeof(WinTrustData));

    WinTrustData.cbStruct = sizeof(WinTrustData);
    
    // Use default code signing EKU.
    WinTrustData.pPolicyCallbackData = NULL;

    // No data to pass to SIP.
    WinTrustData.pSIPClientData = NULL;

    // Disable WVT UI.
    WinTrustData.dwUIChoice = WTD_UI_NONE;

    // No revocation checking.
    WinTrustData.fdwRevocationChecks = WTD_REVOKE_NONE; 

    // Verify an embedded signature on a file.
    WinTrustData.dwUnionChoice = WTD_CHOICE_FILE;

    // Default verification.
    WinTrustData.dwStateAction = 0;

    // Not applicable for default verification of embedded signature.
    WinTrustData.hWVTStateData = NULL;

    // Not used.
    WinTrustData.pwszURLReference = NULL;

    // This is not applicable if there is no UI because it changes 
    // the UI to accommodate running applications instead of 
    // installing applications.
    WinTrustData.dwUIContext = 0;

    // Set pFile.
    WinTrustData.pFile = &FileData;

    // WinVerifyTrust verifies signatures as specified by the GUID 
    // and Wintrust_Data.
    lStatus = WinVerifyTrust(
        NULL,
        &WVTPolicyGUID,
        &WinTrustData);

    switch (lStatus) 
    {
        case ERROR_SUCCESS:
            /*
            Signed file:
                - Hash that represents the subject is trusted.

                - Trusted publisher without any verification errors.

                - UI was disabled in dwUIChoice. No publisher or 
                    time stamp chain errors.

                - UI was enabled in dwUIChoice and the user clicked 
                    "Yes" when asked to install and run the signed 
                    subject.
            */
            wprintf_s(L"The file \"%s\" is signed and the signature "
                L"was verified.\n",
                pwszSourceFile);
            break;
        
        case TRUST_E_NOSIGNATURE:
            // The file was not signed or had a signature 
            // that was not valid.

            // Get the reason for no signature.
            dwLastError = GetLastError();
            if (TRUST_E_NOSIGNATURE == dwLastError ||
                    TRUST_E_SUBJECT_FORM_UNKNOWN == dwLastError ||
                    TRUST_E_PROVIDER_UNKNOWN == dwLastError) 
            {
                // The file was not signed.
                wprintf_s(L"The file \"%s\" is not signed.\n",
                    pwszSourceFile);
            } 
            else 
            {
                // The signature was not valid or there was an error 
                // opening the file.
                wprintf_s(L"An unknown error occurred trying to "
                    L"verify the signature of the \"%s\" file.\n",
                    pwszSourceFile);
            }

            break;

        case TRUST_E_EXPLICIT_DISTRUST:
            // The hash that represents the subject or the publisher 
            // is not allowed by the admin or user.
            wprintf_s(L"The signature is present, but specifically "
                L"disallowed.\n");
            break;

        case TRUST_E_SUBJECT_NOT_TRUSTED:
            // The user clicked "No" when asked to install and run.
            wprintf_s(L"The signature is present, but not "
                L"trusted.\n");
            break;

        case CRYPT_E_SECURITY_SETTINGS:
            /*
            The hash that represents the subject or the publisher 
            was not explicitly trusted by the admin and the 
            admin policy has disabled user trust. No signature, 
            publisher or time stamp errors.
            */
            wprintf_s(L"CRYPT_E_SECURITY_SETTINGS - The hash "
                L"representing the subject or the publisher wasn't "
                L"explicitly trusted by the admin and admin policy "
                L"has disabled user trust. No signature, publisher "
                L"or timestamp errors.\n");
            break;

        default:
            // The UI was disabled in dwUIChoice or the admin policy 
            // has disabled user trust. lStatus contains the 
            // publisher or time stamp chain error.
            wprintf_s(L"Error is: 0x%x.\n",
                lStatus);
            break;
    }

    return true;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Windows 10 数字签名是指微软在操作系统内核、设备驱动程序等关键组件上添加的一种安全机制。这个机制主要用于保护系统的完整性和防止恶意软件的入侵。 数字签名可以视为一种身份验证方式,它以数字形式附加到软件文件中,以证明该文件是由特定发布者签名并未修改过的。通过数字签名,用户可以确信软件的来源和完整性,从而减少恶意软件和病毒的入侵。 在Windows 10中,数字签名由微软的数字证书颁发机构颁发。当用户安装驱动程序或应用程序时,系统会检测文件的数字签名是否有效。如果签名有效,则系统会信任该文件;如果签名无效或缺失,系统会发出警告或拒绝执行文件。 数字签名的作用是确保文件的完整性和可信度。它可以防止恶意软件通过篡改系统文件或驱动程序来入侵系统。此外,数字签名还可以简化用户使用第三方软件和硬件设备的流程,因为系统会自动验证文件的签名,而不需要用户手动确认。 总的来说,Win10数字签名是一种安全机制,通过对操作系统和驱动程序进行数字签名,保护了系统的安全性和稳定性。用户可以根据数字签名判断文件的来源和完整性,从而确保系统的运行环境不受到恶意软件的侵害。 ### 回答2: Win10数字签名是指为Windows系统开发者提供的一种认证机制,目的是保证系统软件的安全性和可信度。数字签名可以验证软件的真实性和完整性,防止恶意软件和未经验证的软件进入系统。 Win10数字签名基于公钥基础设施(PKI)技术,使用公钥和私钥来进行加密和解密操作。开发者在开发完软件之后,会使用私钥对软件进行加密处理,生成一个唯一的数字签名。然后,用户在安装软件时,操作系统会自动验证该数字签名的可信度。 数字签名验证的过程是这样的:当用户打开一个软件时,操作系统会自动检查软件的数字签名,将公钥和数字签名解密得到软件的摘要,再与软件本身的摘要进行对比。如果两者一致,则意味着软件未被篡改,具有可信度,系统会继续运行软件;如果不一致,则意味着软件可能被篡改或者是未经验证的软件,系统会弹出警告提示用户潜在的风险。 Win10数字签名的使用有许多好处。首先,它可以为用户提供更高的安全性,防止恶意软件入侵用户系统。其次,数字签名可提高软件的可信度,用户可以更加放心地安装和使用来自可信来源的软件。此外,数字签名还可以帮助开发者保护自己的权益,防止他人篡改、复制或者重新分发他们的软件。 总之,Win10数字签名是一种保障Windows系统软件安全性和可信度的认证机制。它能够提高用户的安全性和信任度,同时也保护了开发者的权益。 ### 回答3: Win10数字签名是Windows 10操作系统中的一项安全功能,用于验证驱动程序和应用程序的完整性和可信性。数字签名通过使用公钥密码学技术,给文件或程序加上一个数字签名,以确保文件或程序在传输过程中没有被篡改或被恶意软件替换。 使用数字签名的目的是保护用户免受恶意软件的攻击,因为恶意软件通常会篡改系统文件或植入恶意代码,从而对用户的计算机造成损害。数字签名可以防止这些攻击,因为在系统中只有由受信任的软件供应商签名的文件才能被正常加载和执行。 在Win10中,数字签名的验证是由操作系统自动完成的。当用户尝试安装或运行一个软件或驱动程序时,系统会自动检查该文件的数字签名。如果文件没有数字签名签名无效,系统会发出警告,询问用户是否继续执行操作。这样,用户可以根据警告判断文件的可信度,并自行决定是否继续使用。 数字签名在Win10中的使用可以增强系统的安全性,避免恶意软件的入侵,并保护用户的隐私和数据安全。用户可以通过在系统设置中调整数字签名的验证级别,自定义安全级别,以适应自己的需求。 总之,Win10数字签名是操作系统提供的一项安全功能,通过验证文件或程序的完整性和可信性,保护用户免受恶意软件的攻击。通过数字签名,用户可以判断文件的来源和可信度,提高系统的安全性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值