본문 바로가기

프로그래밍/코드 조각

파일 코드사인(디지털서명) 여부 확인

//-------------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
// Example of verifying the embedded signature of a PE file by using 
// the WinVerifyTrust function.
 
#include <tchar.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <Softpub.h>
#include <wincrypt.h>
#include <wintrust.h>
 
// Link with the Wintrust.lib file.
#pragma comment (lib, "wintrust")
 
bool VerifyEmbeddedSignature(LPCTSTR pszSourceFile)
{
    // Initialize the WINTRUST_FILE_INFO structure.
    WINTRUST_FILE_INFO FileData;
    memset(&FileData, 0, sizeof(FileData));
    FileData.cbStruct = sizeof(WINTRUST_FILE_INFO);
    FileData.pcwszFilePath = CComBSTR(pszSourceFile);
    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;
 
    // Default.
    WinTrustData.dwProvFlags = WTD_SAFER_FLAG;
 
    // 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.
    LONG lStatus = WinVerifyTrust(NULL, &WVTPolicyGUID, &WinTrustData);
    return (lStatus == ERROR_SUCCESS);
}

Reference : http://msdn.microsoft.com/en-us/library/aa382384(VS.85).aspx