1

mmc.exe (サービス) 署名を検証しようとするアプリケーションがあります。(アプリケーションのコンテキストは無関係だと思います) WinVerifyTrust で両方とも失敗する winapi 関数を試しています。カタログからの検証で TRUST_E_BAD_DIGEST を取得し、ファイル情報からの検証で TRUST_E_NOSIGNATURE を取得します。私の関数はwin7、XPでは成功しますが、win8では失敗することに言及することは非常に重要です。

これは関数のコード スニペットです。

CATALOG_INFO InfoStruct = {0};
InfoStruct.cbStruct = sizeof(CATALOG_INFO);

WINTRUST_CATALOG_INFO WintrustCatalogStructure = {0};
WintrustCatalogStructure.cbStruct = sizeof(WINTRUST_CATALOG_INFO);

WINTRUST_FILE_INFO WintrustFileStructure = {0};
WintrustFileStructure.cbStruct = sizeof(WINTRUST_FILE_INFO);

GUID ActionGuid = WINTRUST_ACTION_GENERIC_VERIFY_V2;

//Get a context for signature verification.
HCATADMIN Context = NULL;
if(!::CryptCATAdminAcquireContext(&Context, NULL, 0) ){
    return false;
}

//Open file.

cx_handle hFile(::CreateFileW(filename_.c_str(), GENERIC_READ, 7, NULL, OPEN_EXISTING, 0, NULL));
if( INVALID_HANDLE_VALUE == (HANDLE)hFile )
{
    CryptCATAdminReleaseContext(Context, 0);
    return false;
}

//Get the size we need for our hash.
DWORD HashSize = 0;
::CryptCATAdminCalcHashFromFileHandle(hFile, &HashSize, NULL, 0);
if( HashSize == 0 )
{
    //0-sized has means error!
    ::CryptCATAdminReleaseContext(Context, 0);
    return false;
}

//Allocate memory.
buffer hashbuf(HashSize);

//Actually calculate the hash
if( !CryptCATAdminCalcHashFromFileHandle(hFile, &HashSize, hashbuf.data, 0) )
{
    CryptCATAdminReleaseContext(Context, 0);
    return false;
}

//Convert the hash to a string.
buffer MemberTag(((HashSize * 2) + 1) * sizeof(wchar_t));
for( unsigned int i = 0; i < HashSize; i++ ){
    swprintf(&((PWCHAR)MemberTag.data)[i * 2], L"%02X", hashbuf.data[i ]);
}

//Get catalog for our context.
HCATINFO CatalogContext = CryptCATAdminEnumCatalogFromHash(Context, hashbuf, HashSize, 0, NULL);
if ( CatalogContext )
{
    //If we couldn't get information
    if ( !CryptCATCatalogInfoFromContext(CatalogContext, &InfoStruct, 0) )
    {
        //Release the context and set the context to null so it gets picked up below.
        CryptCATAdminReleaseCatalogContext(Context, CatalogContext, 0);
        CatalogContext = NULL;
    }
}

//If we have a valid context, we got our info.  
//Otherwise, we attempt to verify the internal signature.

WINTRUST_DATA WintrustStructure = {0};
WintrustStructure.cbStruct = sizeof(WINTRUST_DATA);

if( !CatalogContext )
{
    load_signature_verification_from_file_info(WintrustFileStructure, WintrustStructure);
} 
else
{
    load_signature_verification_from_catalog(WintrustStructure, WintrustCatalogStructure, InfoStruct, MemberTag);
}

//Call our verification function.
long verification_res = ::WinVerifyTrust(0, &ActionGuid, &WintrustStructure);

//Check return.
bool is_success = SUCCEEDED(verification_res) ? true : false;

// if failed with CatalogContext, try with FILE_INFO
if(!is_success && CatalogContext && verification_res != TRUST_E_NOSIGNATURE)
{
    //warning2(L"Failed verification with Catalog Context: 0x%x %s ; Retrying with FILE_INFO.", verification_res, (const wchar_t*)format_last_error(verification_res));

    load_signature_verification_from_file_info(WintrustFileStructure, WintrustStructure);
    verification_res = ::WinVerifyTrust(0, &ActionGuid, &WintrustStructure);
    is_success = SUCCEEDED(verification_res) ? true : false;
}

if(perr && !is_success && verification_res != TRUST_E_NOSIGNATURE)
{
    perr->code = verification_res;
    perr->description = format_last_error(verification_res);
}

//Free context.
if( CatalogContext ){
    ::CryptCATAdminReleaseCatalogContext(Context, CatalogContext, 0);
}

//If we successfully verified, we need to free.
if( is_success )
{
    WintrustStructure.dwStateAction = WTD_STATEACTION_CLOSE;
    ::WinVerifyTrust(0, &ActionGuid, &WintrustStructure);
}

::CryptCATAdminReleaseContext(Context, 0);

return is_success;

この関数でwin7からwin 8に変更されたとは思わないので、何がうまくいかないのでしょうか?

アップデート

私の機能はwin 8のタスクマネージャーでは機能することに気付きましたが、mmcでは機能しません。

4

1 に答える 1

3

あなたの一般的なアプローチは正しく、機能自体は変わっていないようです。ただし、微妙な変更があります。つまり、操作対象のデータが変更されました。CryptCATAdminCalcHashFromFileHandleに関するコメントによると、Windows 8 でファイルに保存されるハッシュは、SHA-256 ハッシュを使用して計算されます。

SHA-256 ハッシュ アルゴリズムは CryptCATAdminCalcHashFromFileHandle ではサポートされていないため、Windows 8 でCryptCATAdminAcquireContext2CryptCATAdminCalcHashFromFileHandle2を使用するようにコードを更新する必要があります。HCATADMIN前者では指定されたハッシュ アルゴリズムでを取得でき、後者ではその を使用できますHCATADMIN

(興味深いことに、WINTRUST_CATALOG_INFOは、「 Windows 8 および Windows Server 2012: このメンバーのサポートが開始されますHCATADMIN hCatAdmin」として文書化されているそのメンバーでこの方向性も示しています。)

于 2014-10-06T15:14:42.853 に答える