Win32 API 日本語リファレンス
ホームSecurity.Cryptography › NCryptVerifyClaim

NCryptVerifyClaim

関数
キーに関するクレーム(証明)を検証する。
DLLncrypt.dll呼出規約winapi対応OSWindows 10 以降

シグネチャ

// ncrypt.dll
#include <windows.h>

HRESULT NCryptVerifyClaim(
    NCRYPT_KEY_HANDLE hSubjectKey,
    NCRYPT_KEY_HANDLE hAuthorityKey,   // optional
    DWORD dwClaimType,
    BCryptBufferDesc* pParameterList,   // optional
    BYTE* pbClaimBlob,
    DWORD cbClaimBlob,
    BCryptBufferDesc* pOutput,
    DWORD dwFlags
);

パラメーター

名前方向説明
hSubjectKeyNCRYPT_KEY_HANDLEinクレームの対象となるキーのハンドル。
hAuthorityKeyNCRYPT_KEY_HANDLEinoptionalクレームの検証時に使用する権限キーのハンドル。特定のクレームタイプでは権限キーが自己完結しているため、このパラメーターは省略可能です。
dwClaimTypeDWORDinクレームのタイプ。
pParameterListBCryptBufferDesc*inoptional省略可能なパラメーターリスト。
pbClaimBlobBYTE*in入力クレーム blob。
cbClaimBlobDWORDinpbClaimBlob バッファーのサイズ(バイト単位)。
pOutputBCryptBufferDesc*out出力 blob。
dwFlagsDWORDin

NCRYPT_VBS_RETURN_CLAIM_DETAILS_FLAG は、VBS が生成したクレームの検証時に設定する新しいフラグです。詳細については 備考 のセクションを参照してください。

現時点では他にフラグは定義されていません。それ以外のすべての検証タイプでは、dwFlags パラメーターに 0 を設定してください。

戻り値の型: HRESULT

公式ドキュメント

キー構成証明クレームを検証します。

戻り値

関数の成否を示すステータスコードを返します。

VBS キー保護構成証明クレームを検証する際に検証 API から返される可能性のあるエラーコードの一部を以下に示します。

戻り値コード 意味
NTE_BAD_TYPE クレームタイプの入力パラメーターが、入力クレーム blob のタイプと異なります。
STATUS_BAD_DATA 入力クレーム blob が無効です。
STATUS_NO_MEMORY クレーム検証に必要なメモリの割り当てに失敗しました。
STATUS_INVALID_PARAMETER 必須の入力パラメーターが欠落しているか、いずれかのパラメーターに不正な値が指定されています。
STATUS_FAIL_CHECK 入力 blob クレームのチェックに失敗しました。
NTE_BAD_VER クレーム blob のバージョンが検証実装と一致しません。
NTE_BAD_FLAGS dwFlags に指定されたフラグはサポートされていません。

解説(Remarks)

仮想化ベースのセキュリティ (VBS) を使用した秘密キーの保護/構成証明

メモ

VBS フラグに関する情報は、製品版リリース前のプレリリース製品に関するものであり、製品版リリースまでに大幅に変更される可能性があります。Microsoft は、ここで提供される情報に関して、明示または黙示を問わずいかなる保証も行いません。

この API は、VBS キー保護(VBS を使用して秘密キーを保護および構成証明するための Windows モジュール)に基づくセキュリティキーの高度な構成証明を可能にします。セキュリティキーの構成証明は、そのキーがアンカーキー(構成証明キーとも呼ばれます)に関連付けられていることを証明します。この機能により、コンテキスト外のキーの使用を制限することで、異なるエンティティ間の通信のセキュリティレベルを高めることができます。

この API は、VBS キー保護における構成証明キーに基づく構成証明クレームの作成および検証をサポートする新しいフラグを定義します。

この API で定義されている dwClaimType のタイプは以下のとおりです。

クレームタイプ 説明
NCRYPT_CLAIM_VBS_ROOT このタイプは、生成されたクレームが VBS ルートキーによって生成されたことを示します。
NCRYPT_CLAIM_VBS_IDENTITY このタイプは、生成されたクレームが VBS アイデンティティ/構成証明によって生成されたことを示します。これは、構成証明フラグ NCRYPT_ALLOW_KEY_ATTESTATION_FLAG で昇格された VBS キーによってクレームが生成されたことを意味します(詳細は後述)。

構成証明クレームを検証する際に pParameterList バッファーに設定するバッファータイプは以下のとおりです。

この例は、NCryptCreateClaim で生成された構成証明クレームを検証するための既存 API の使用方法を示しています。

検証 API は、ローカル(クレーム blob を生成したマシン上)またはリモートで呼び出すことができます。検証手順には、クレーム生成キーに対応する次の要素が必要です。

入力フラグ NCRYPT_VBS_RETURN_CLAIM_DETAILS_FLAG が設定されている場合、検証 API は出力情報構造体 NCRYPT_VBS_ROOT_KEY_ATTESTATION_CLAIM_DETAILS または NCRYPT_VBS_IDENTITY_KEY_ATTESTATION_CLAIM_DETAILS のいずれかを生成します。これらの構造体のメモリは、メモリリークを避けるためコードフローの最後で解放されます。

HRESULT VerifyClaim(
       BCRYPT_RSAKEY_BLOB *pAttestPublicKeyBlob,
       BCRYPT_RSAKEY_BLOB *pTokenPublicKeyBlob,
       PBYTE pRootClaim,
       DWORD rootClaimSize,
       PBYTE pIdentityClaim,
       DWORD identityClaimSize)
{

    HRESULT hr = S_OK;
    DWORD bytesWritten = 0;

    NCRYPT_PROV_HANDLE provider = NULL;

    if (FAILED(hr = NCryptOpenStorageProvider(&provider, MS_KEY_STORAGE_PROVIDER, 0)))
    {
        wprintf(L"Error opening storage provider in NCryptOpenStorageProvider: 0x%X\n", hr);
        goto cleanup;
    }

    NCRYPT_KEY_HANDLE attestPublicKey = NULL;

    if (FAILED(hr = NCryptImportKey(
                       provider,
                       NULL,
                       BCRYPT_RSAPUBLIC_BLOB,
                       NULL,
                       &attestPublicKey,
                       (PBYTE)pAttestPublicKeyBlob,
                       GetRsaPublicKeyBlobSize(pAttestPublicKeyBlob),
                       0)))
    {
        wprintf(L"Unable to create a key handle for attestation public key blob with NCryptImportKey(): 0x%X\n", hr);
        goto cleanup;
    }

    NCRYPT_KEY_HANDLE tokenPublicKey = NULL;

    if (FAILED(hr = NCryptImportKey(
                       provider,
                       NULL,
                       BCRYPT_RSAPUBLIC_BLOB,
                       NULL,
                       &tokenPublicKey,
                       (PBYTE)pTokenPublicKeyBlob,
                       GetRsaPublicKeyBlobSize(pTokenPublicKeyBlob),
                       0)))
    {
        wprintf(L"Unable to create a key handle for token public key blob with NCryptImportKey(): 0x%X\n", hr);
        goto cleanup;
    }

    NCryptBufferDesc rootOutput{};

    // Verify the VBS root claim using the attestation/identity public key
    if (FAILED(hr = NCryptVerifyClaim(
                       attestPublicKey,
                       NULL,
                       NCRYPT_CLAIM_VBS_ROOT, // Created claim by IDKS (VBS root signing key)
                       NULL, // parameters
                       pRootClaim,
                       rootClaimSize,
                       &rootOutput,
                       NCRYPT_VBS_RETURN_CLAIM_DETAILS_FLAG /*dwFlags*/)))
    {
        switch (hr)
        {
            case STATUS_OBJECT_TYPE_MISMATCH:
                wprintf(L"The dwClaimType parameter’s value is different than the claim’s type.\n-----\n", hr);
                break;
            case STATUS_BAD_DATA:
                wprintf(L"Something wrong in one of the data structures. E.g. Magic value mismatch\n-----\n", hr);
                break;
            case STATUS_NO_MEMORY:
                wprintf(L"Memory allocation failed\n-----\n", hr);
                break;
            case STATUS_INVALID_PARAMETER:
                wprintf(L"Missing mandatory parameter or one of the parameters has a bad value.\n-----\n", hr);
                break;
            case STATUS_FAIL_CHECK:
                wprintf(L"One of the claim checks has failed.\n-----\n", hr);
                break;
            default:
                wprintf(L"Unable to verify VBS root claim from NCryptVerifyClaim(): 0x%X\n-----\n", hr);
        }
        goto cleanup;
    }

    PNCryptBuffer pRootOutBuffer;
    DWORD count;

    // Look into the retrieved VBS root claim details
    for (count = 0; count < rootOutput.cBuffers; ++count)
    {
        pRootOutBuffer = rootOutput.pBuffers[count];
        if (pRootOutBuffer->BufferType == NCRYPTBUFFER_VBS_ATTESTATION_STATEMENT_ROOT_DETAILS)
        {
            PNCRYPT_VBS_ROOT_KEY_ATTESTATION_CLAIM_DETAILS pDetails =
            (PNCRYPT_VBS_ROOT_KEY_ATTESTATION_CLAIM_DETAILS) pRootOutBuffer->pvBuffer;
            wprintf(L"The claim trustlet id is: %lu\n-----\n", pDetails->ullTrustletId);
            wprintf(L"The claim trustlet Security Version number is: %llu\n-----\n", pDetails->ulTrustletSecurityVersion);
        }
    }

    NCryptBufferDesc identityOutput{};

    // Verify the identity claim using the attestation/identity and token public keys
    if (FAILED(hr = NCryptVerifyClaim(
                        tokenPublicKey,
                        attestPublicKey,
                        NCRYPT_CLAIM_VBS_IDENTITY, // Claim created by an attestation/identity key
                        NULL, // parameters
                        pIdentityClaim,
                        identityClaimSize,
                        &identityOutput,
                        NCRYPT_VBS_RETURN_CLAIM_DETAILS_FLAG /*dwFlags*/)))
    {
        wprintf(L"Unable to verify identity claim from NCryptVerifyClaim(): 0x%X\n-----\n", hr);
        goto cleanup;
    }

    PNCryptBuffer pIdentityOutBuffer;

    // Look into the retrieved identity claim details
    for (count = 0; count < identityOutput.cBuffers; ++count)
    {
        pIdentityOutBuffer = identityOutput.pBuffers[count];
        if (pIdentityOutBuffer->BufferType == NCRYPTBUFFER_VBS_ATTESTATION_STATEMENT_IDENTITY_DETAILS)
        {
            PNCRYPT_VBS_IDENTITY_KEY_ATTESTATION_CLAIM_DETAILS pDetails =
            (PNCRYPT_VBS_IDENTITY_KEY_ATTESTATION_CLAIM_DETAILS) pIdentityOutBuffer->pvBuffer;
            wprintf(L"The claim hash algorithm is: %S\n-----\n", pDetails-> pszSignatureHashAlg);
            wprintf(L"The claim padding scheme is: %lu\n-----\n", pDetails->ulPaddingScheme);
        }
    }

    wprintf(L"Verify claim for root and identity types passed successfully\n");

    cleanup:

    if (provider != NULL)
    {
        NCryptFreeObject(provider);
    }
    if (attestPublicKey != NULL)
    {
        CryptDestroyKey(attestPublicKey);
    }
    if (tokenPub != NULL)
    {
        CryptDestroyKey(tokenPublicKey);
    }
    if (rootOutput.pBuffers != NULL)
    {
        for (count = 0; count < rootOutput.cBuffers; ++count)
        {
            NCryptFreeBuffer(rootOutput.pBuffers[count].pvBuffer);
        }
        NCryptFreeBuffer(rootOutput.pBuffers);
    }

    if (identityOutput.pBuffers != NULL)
    {
        for (count = 0; count < identityOutput.cBuffers; ++count)
        {
            NCryptFreeBuffer(identityOutput.pBuffers[count].pvBuffer);
        }
        NCryptFreeBuffer(identityOutput.pBuffers);
    }

    return hr;
}

DWORD GetRsaPublicKeyBlobSize(BCRYPT_RSAKEY_BLOB* publicKeyBlob)
{
    return sizeof(BCRYPT_RSAKEY_BLOB) +
                publicKeyBlob->cbModulus +
                publicKeyBlob->cbPublicExp;
}
出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

// ncrypt.dll
#include <windows.h>

HRESULT NCryptVerifyClaim(
    NCRYPT_KEY_HANDLE hSubjectKey,
    NCRYPT_KEY_HANDLE hAuthorityKey,   // optional
    DWORD dwClaimType,
    BCryptBufferDesc* pParameterList,   // optional
    BYTE* pbClaimBlob,
    DWORD cbClaimBlob,
    BCryptBufferDesc* pOutput,
    DWORD dwFlags
);
[DllImport("ncrypt.dll", ExactSpelling = true)]
static extern int NCryptVerifyClaim(
    UIntPtr hSubjectKey,   // NCRYPT_KEY_HANDLE
    UIntPtr hAuthorityKey,   // NCRYPT_KEY_HANDLE optional
    uint dwClaimType,   // DWORD
    IntPtr pParameterList,   // BCryptBufferDesc* optional
    IntPtr pbClaimBlob,   // BYTE*
    uint cbClaimBlob,   // DWORD
    IntPtr pOutput,   // BCryptBufferDesc* out
    uint dwFlags   // DWORD
);
<DllImport("ncrypt.dll", ExactSpelling:=True)>
Public Shared Function NCryptVerifyClaim(
    hSubjectKey As UIntPtr,   ' NCRYPT_KEY_HANDLE
    hAuthorityKey As UIntPtr,   ' NCRYPT_KEY_HANDLE optional
    dwClaimType As UInteger,   ' DWORD
    pParameterList As IntPtr,   ' BCryptBufferDesc* optional
    pbClaimBlob As IntPtr,   ' BYTE*
    cbClaimBlob As UInteger,   ' DWORD
    pOutput As IntPtr,   ' BCryptBufferDesc* out
    dwFlags As UInteger   ' DWORD
) As Integer
End Function
' hSubjectKey : NCRYPT_KEY_HANDLE
' hAuthorityKey : NCRYPT_KEY_HANDLE optional
' dwClaimType : DWORD
' pParameterList : BCryptBufferDesc* optional
' pbClaimBlob : BYTE*
' cbClaimBlob : DWORD
' pOutput : BCryptBufferDesc* out
' dwFlags : DWORD
Declare PtrSafe Function NCryptVerifyClaim Lib "ncrypt" ( _
    ByVal hSubjectKey As LongPtr, _
    ByVal hAuthorityKey As LongPtr, _
    ByVal dwClaimType As Long, _
    ByVal pParameterList As LongPtr, _
    ByVal pbClaimBlob As LongPtr, _
    ByVal cbClaimBlob As Long, _
    ByVal pOutput As LongPtr, _
    ByVal dwFlags As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

NCryptVerifyClaim = ctypes.windll.ncrypt.NCryptVerifyClaim
NCryptVerifyClaim.restype = ctypes.c_int
NCryptVerifyClaim.argtypes = [
    ctypes.c_size_t,  # hSubjectKey : NCRYPT_KEY_HANDLE
    ctypes.c_size_t,  # hAuthorityKey : NCRYPT_KEY_HANDLE optional
    wintypes.DWORD,  # dwClaimType : DWORD
    ctypes.c_void_p,  # pParameterList : BCryptBufferDesc* optional
    ctypes.POINTER(ctypes.c_ubyte),  # pbClaimBlob : BYTE*
    wintypes.DWORD,  # cbClaimBlob : DWORD
    ctypes.c_void_p,  # pOutput : BCryptBufferDesc* out
    wintypes.DWORD,  # dwFlags : DWORD
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('ncrypt.dll')
NCryptVerifyClaim = Fiddle::Function.new(
  lib['NCryptVerifyClaim'],
  [
    Fiddle::TYPE_UINTPTR_T,  # hSubjectKey : NCRYPT_KEY_HANDLE
    Fiddle::TYPE_UINTPTR_T,  # hAuthorityKey : NCRYPT_KEY_HANDLE optional
    -Fiddle::TYPE_INT,  # dwClaimType : DWORD
    Fiddle::TYPE_VOIDP,  # pParameterList : BCryptBufferDesc* optional
    Fiddle::TYPE_VOIDP,  # pbClaimBlob : BYTE*
    -Fiddle::TYPE_INT,  # cbClaimBlob : DWORD
    Fiddle::TYPE_VOIDP,  # pOutput : BCryptBufferDesc* out
    -Fiddle::TYPE_INT,  # dwFlags : DWORD
  ],
  Fiddle::TYPE_INT)
#[link(name = "ncrypt")]
extern "system" {
    fn NCryptVerifyClaim(
        hSubjectKey: usize,  // NCRYPT_KEY_HANDLE
        hAuthorityKey: usize,  // NCRYPT_KEY_HANDLE optional
        dwClaimType: u32,  // DWORD
        pParameterList: *mut BCryptBufferDesc,  // BCryptBufferDesc* optional
        pbClaimBlob: *mut u8,  // BYTE*
        cbClaimBlob: u32,  // DWORD
        pOutput: *mut BCryptBufferDesc,  // BCryptBufferDesc* out
        dwFlags: u32  // DWORD
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("ncrypt.dll")]
public static extern int NCryptVerifyClaim(UIntPtr hSubjectKey, UIntPtr hAuthorityKey, uint dwClaimType, IntPtr pParameterList, IntPtr pbClaimBlob, uint cbClaimBlob, IntPtr pOutput, uint dwFlags);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ncrypt_NCryptVerifyClaim' -Namespace Win32 -PassThru
# $api::NCryptVerifyClaim(hSubjectKey, hAuthorityKey, dwClaimType, pParameterList, pbClaimBlob, cbClaimBlob, pOutput, dwFlags)
#uselib "ncrypt.dll"
#func global NCryptVerifyClaim "NCryptVerifyClaim" sptr, sptr, sptr, sptr, sptr, sptr, sptr, sptr
; NCryptVerifyClaim hSubjectKey, hAuthorityKey, dwClaimType, varptr(pParameterList), varptr(pbClaimBlob), cbClaimBlob, varptr(pOutput), dwFlags   ; 戻り値は stat
; hSubjectKey : NCRYPT_KEY_HANDLE -> "sptr"
; hAuthorityKey : NCRYPT_KEY_HANDLE optional -> "sptr"
; dwClaimType : DWORD -> "sptr"
; pParameterList : BCryptBufferDesc* optional -> "sptr"
; pbClaimBlob : BYTE* -> "sptr"
; cbClaimBlob : DWORD -> "sptr"
; pOutput : BCryptBufferDesc* out -> "sptr"
; dwFlags : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "ncrypt.dll"
#cfunc global NCryptVerifyClaim "NCryptVerifyClaim" sptr, sptr, int, var, var, int, var, int
; res = NCryptVerifyClaim(hSubjectKey, hAuthorityKey, dwClaimType, pParameterList, pbClaimBlob, cbClaimBlob, pOutput, dwFlags)
; hSubjectKey : NCRYPT_KEY_HANDLE -> "sptr"
; hAuthorityKey : NCRYPT_KEY_HANDLE optional -> "sptr"
; dwClaimType : DWORD -> "int"
; pParameterList : BCryptBufferDesc* optional -> "var"
; pbClaimBlob : BYTE* -> "var"
; cbClaimBlob : DWORD -> "int"
; pOutput : BCryptBufferDesc* out -> "var"
; dwFlags : DWORD -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; HRESULT NCryptVerifyClaim(NCRYPT_KEY_HANDLE hSubjectKey, NCRYPT_KEY_HANDLE hAuthorityKey, DWORD dwClaimType, BCryptBufferDesc* pParameterList, BYTE* pbClaimBlob, DWORD cbClaimBlob, BCryptBufferDesc* pOutput, DWORD dwFlags)
#uselib "ncrypt.dll"
#cfunc global NCryptVerifyClaim "NCryptVerifyClaim" intptr, intptr, int, var, var, int, var, int
; res = NCryptVerifyClaim(hSubjectKey, hAuthorityKey, dwClaimType, pParameterList, pbClaimBlob, cbClaimBlob, pOutput, dwFlags)
; hSubjectKey : NCRYPT_KEY_HANDLE -> "intptr"
; hAuthorityKey : NCRYPT_KEY_HANDLE optional -> "intptr"
; dwClaimType : DWORD -> "int"
; pParameterList : BCryptBufferDesc* optional -> "var"
; pbClaimBlob : BYTE* -> "var"
; cbClaimBlob : DWORD -> "int"
; pOutput : BCryptBufferDesc* out -> "var"
; dwFlags : DWORD -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	ncrypt = windows.NewLazySystemDLL("ncrypt.dll")
	procNCryptVerifyClaim = ncrypt.NewProc("NCryptVerifyClaim")
)

// hSubjectKey (NCRYPT_KEY_HANDLE), hAuthorityKey (NCRYPT_KEY_HANDLE optional), dwClaimType (DWORD), pParameterList (BCryptBufferDesc* optional), pbClaimBlob (BYTE*), cbClaimBlob (DWORD), pOutput (BCryptBufferDesc* out), dwFlags (DWORD)
r1, _, err := procNCryptVerifyClaim.Call(
	uintptr(hSubjectKey),
	uintptr(hAuthorityKey),
	uintptr(dwClaimType),
	uintptr(pParameterList),
	uintptr(pbClaimBlob),
	uintptr(cbClaimBlob),
	uintptr(pOutput),
	uintptr(dwFlags),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // HRESULT
function NCryptVerifyClaim(
  hSubjectKey: NativeUInt;   // NCRYPT_KEY_HANDLE
  hAuthorityKey: NativeUInt;   // NCRYPT_KEY_HANDLE optional
  dwClaimType: DWORD;   // DWORD
  pParameterList: Pointer;   // BCryptBufferDesc* optional
  pbClaimBlob: Pointer;   // BYTE*
  cbClaimBlob: DWORD;   // DWORD
  pOutput: Pointer;   // BCryptBufferDesc* out
  dwFlags: DWORD   // DWORD
): Integer; stdcall;
  external 'ncrypt.dll' name 'NCryptVerifyClaim';
result := DllCall("ncrypt\NCryptVerifyClaim"
    , "UPtr", hSubjectKey   ; NCRYPT_KEY_HANDLE
    , "UPtr", hAuthorityKey   ; NCRYPT_KEY_HANDLE optional
    , "UInt", dwClaimType   ; DWORD
    , "Ptr", pParameterList   ; BCryptBufferDesc* optional
    , "Ptr", pbClaimBlob   ; BYTE*
    , "UInt", cbClaimBlob   ; DWORD
    , "Ptr", pOutput   ; BCryptBufferDesc* out
    , "UInt", dwFlags   ; DWORD
    , "Int")   ; return: HRESULT
●NCryptVerifyClaim(hSubjectKey, hAuthorityKey, dwClaimType, pParameterList, pbClaimBlob, cbClaimBlob, pOutput, dwFlags) = DLL("ncrypt.dll", "int NCryptVerifyClaim(int, int, dword, void*, void*, dword, void*, dword)")
# 呼び出し: NCryptVerifyClaim(hSubjectKey, hAuthorityKey, dwClaimType, pParameterList, pbClaimBlob, cbClaimBlob, pOutput, dwFlags)
# hSubjectKey : NCRYPT_KEY_HANDLE -> "int"
# hAuthorityKey : NCRYPT_KEY_HANDLE optional -> "int"
# dwClaimType : DWORD -> "dword"
# pParameterList : BCryptBufferDesc* optional -> "void*"
# pbClaimBlob : BYTE* -> "void*"
# cbClaimBlob : DWORD -> "dword"
# pOutput : BCryptBufferDesc* out -> "void*"
# dwFlags : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "ncrypt" fn NCryptVerifyClaim(
    hSubjectKey: usize, // NCRYPT_KEY_HANDLE
    hAuthorityKey: usize, // NCRYPT_KEY_HANDLE optional
    dwClaimType: u32, // DWORD
    pParameterList: [*c]BCryptBufferDesc, // BCryptBufferDesc* optional
    pbClaimBlob: [*c]u8, // BYTE*
    cbClaimBlob: u32, // DWORD
    pOutput: [*c]BCryptBufferDesc, // BCryptBufferDesc* out
    dwFlags: u32 // DWORD
) callconv(std.os.windows.WINAPI) i32;
proc NCryptVerifyClaim(
    hSubjectKey: uint,  # NCRYPT_KEY_HANDLE
    hAuthorityKey: uint,  # NCRYPT_KEY_HANDLE optional
    dwClaimType: uint32,  # DWORD
    pParameterList: ptr BCryptBufferDesc,  # BCryptBufferDesc* optional
    pbClaimBlob: ptr uint8,  # BYTE*
    cbClaimBlob: uint32,  # DWORD
    pOutput: ptr BCryptBufferDesc,  # BCryptBufferDesc* out
    dwFlags: uint32  # DWORD
): int32 {.importc: "NCryptVerifyClaim", stdcall, dynlib: "ncrypt.dll".}
pragma(lib, "ncrypt");
extern(Windows)
int NCryptVerifyClaim(
    size_t hSubjectKey,   // NCRYPT_KEY_HANDLE
    size_t hAuthorityKey,   // NCRYPT_KEY_HANDLE optional
    uint dwClaimType,   // DWORD
    BCryptBufferDesc* pParameterList,   // BCryptBufferDesc* optional
    ubyte* pbClaimBlob,   // BYTE*
    uint cbClaimBlob,   // DWORD
    BCryptBufferDesc* pOutput,   // BCryptBufferDesc* out
    uint dwFlags   // DWORD
);
ccall((:NCryptVerifyClaim, "ncrypt.dll"), stdcall, Int32,
      (Csize_t, Csize_t, UInt32, Ptr{BCryptBufferDesc}, Ptr{UInt8}, UInt32, Ptr{BCryptBufferDesc}, UInt32),
      hSubjectKey, hAuthorityKey, dwClaimType, pParameterList, pbClaimBlob, cbClaimBlob, pOutput, dwFlags)
# hSubjectKey : NCRYPT_KEY_HANDLE -> Csize_t
# hAuthorityKey : NCRYPT_KEY_HANDLE optional -> Csize_t
# dwClaimType : DWORD -> UInt32
# pParameterList : BCryptBufferDesc* optional -> Ptr{BCryptBufferDesc}
# pbClaimBlob : BYTE* -> Ptr{UInt8}
# cbClaimBlob : DWORD -> UInt32
# pOutput : BCryptBufferDesc* out -> Ptr{BCryptBufferDesc}
# dwFlags : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t NCryptVerifyClaim(
    uintptr_t hSubjectKey,
    uintptr_t hAuthorityKey,
    uint32_t dwClaimType,
    void* pParameterList,
    uint8_t* pbClaimBlob,
    uint32_t cbClaimBlob,
    void* pOutput,
    uint32_t dwFlags);
]]
local ncrypt = ffi.load("ncrypt")
-- ncrypt.NCryptVerifyClaim(hSubjectKey, hAuthorityKey, dwClaimType, pParameterList, pbClaimBlob, cbClaimBlob, pOutput, dwFlags)
-- hSubjectKey : NCRYPT_KEY_HANDLE
-- hAuthorityKey : NCRYPT_KEY_HANDLE optional
-- dwClaimType : DWORD
-- pParameterList : BCryptBufferDesc* optional
-- pbClaimBlob : BYTE*
-- cbClaimBlob : DWORD
-- pOutput : BCryptBufferDesc* out
-- dwFlags : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('ncrypt.dll');
const NCryptVerifyClaim = lib.func('__stdcall', 'NCryptVerifyClaim', 'int32_t', ['uintptr_t', 'uintptr_t', 'uint32_t', 'void *', 'uint8_t *', 'uint32_t', 'void *', 'uint32_t']);
// NCryptVerifyClaim(hSubjectKey, hAuthorityKey, dwClaimType, pParameterList, pbClaimBlob, cbClaimBlob, pOutput, dwFlags)
// hSubjectKey : NCRYPT_KEY_HANDLE -> 'uintptr_t'
// hAuthorityKey : NCRYPT_KEY_HANDLE optional -> 'uintptr_t'
// dwClaimType : DWORD -> 'uint32_t'
// pParameterList : BCryptBufferDesc* optional -> 'void *'
// pbClaimBlob : BYTE* -> 'uint8_t *'
// cbClaimBlob : DWORD -> 'uint32_t'
// pOutput : BCryptBufferDesc* out -> 'void *'
// dwFlags : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("ncrypt.dll", {
  NCryptVerifyClaim: { parameters: ["usize", "usize", "u32", "pointer", "pointer", "u32", "pointer", "u32"], result: "i32" },
});
// lib.symbols.NCryptVerifyClaim(hSubjectKey, hAuthorityKey, dwClaimType, pParameterList, pbClaimBlob, cbClaimBlob, pOutput, dwFlags)
// hSubjectKey : NCRYPT_KEY_HANDLE -> "usize"
// hAuthorityKey : NCRYPT_KEY_HANDLE optional -> "usize"
// dwClaimType : DWORD -> "u32"
// pParameterList : BCryptBufferDesc* optional -> "pointer"
// pbClaimBlob : BYTE* -> "pointer"
// cbClaimBlob : DWORD -> "u32"
// pOutput : BCryptBufferDesc* out -> "pointer"
// dwFlags : DWORD -> "u32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t NCryptVerifyClaim(
    size_t hSubjectKey,
    size_t hAuthorityKey,
    uint32_t dwClaimType,
    void* pParameterList,
    uint8_t* pbClaimBlob,
    uint32_t cbClaimBlob,
    void* pOutput,
    uint32_t dwFlags);
C, "ncrypt.dll");
// $ffi->NCryptVerifyClaim(hSubjectKey, hAuthorityKey, dwClaimType, pParameterList, pbClaimBlob, cbClaimBlob, pOutput, dwFlags);
// hSubjectKey : NCRYPT_KEY_HANDLE
// hAuthorityKey : NCRYPT_KEY_HANDLE optional
// dwClaimType : DWORD
// pParameterList : BCryptBufferDesc* optional
// pbClaimBlob : BYTE*
// cbClaimBlob : DWORD
// pOutput : BCryptBufferDesc* out
// dwFlags : DWORD
// 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
// WINAPI(stdcall): x64 では呼出規約が統一されるため問題なし。x86 では __stdcall 対応のラッパが必要な場合あり。
import com.sun.jna.*;
import com.sun.jna.ptr.*;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;

public interface Ncrypt extends StdCallLibrary {
    Ncrypt INSTANCE = Native.load("ncrypt", Ncrypt.class);
    int NCryptVerifyClaim(
        long hSubjectKey,   // NCRYPT_KEY_HANDLE
        long hAuthorityKey,   // NCRYPT_KEY_HANDLE optional
        int dwClaimType,   // DWORD
        Pointer pParameterList,   // BCryptBufferDesc* optional
        byte[] pbClaimBlob,   // BYTE*
        int cbClaimBlob,   // DWORD
        Pointer pOutput,   // BCryptBufferDesc* out
        int dwFlags   // DWORD
    );
}
@[Link("ncrypt")]
lib Libncrypt
  fun NCryptVerifyClaim = NCryptVerifyClaim(
    hSubjectKey : LibC::SizeT,   # NCRYPT_KEY_HANDLE
    hAuthorityKey : LibC::SizeT,   # NCRYPT_KEY_HANDLE optional
    dwClaimType : UInt32,   # DWORD
    pParameterList : BCryptBufferDesc*,   # BCryptBufferDesc* optional
    pbClaimBlob : UInt8*,   # BYTE*
    cbClaimBlob : UInt32,   # DWORD
    pOutput : BCryptBufferDesc*,   # BCryptBufferDesc* out
    dwFlags : UInt32   # DWORD
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef NCryptVerifyClaimNative = Int32 Function(UintPtr, UintPtr, Uint32, Pointer<Void>, Pointer<Uint8>, Uint32, Pointer<Void>, Uint32);
typedef NCryptVerifyClaimDart = int Function(int, int, int, Pointer<Void>, Pointer<Uint8>, int, Pointer<Void>, int);
final NCryptVerifyClaim = DynamicLibrary.open('ncrypt.dll')
    .lookupFunction<NCryptVerifyClaimNative, NCryptVerifyClaimDart>('NCryptVerifyClaim');
// hSubjectKey : NCRYPT_KEY_HANDLE -> UintPtr
// hAuthorityKey : NCRYPT_KEY_HANDLE optional -> UintPtr
// dwClaimType : DWORD -> Uint32
// pParameterList : BCryptBufferDesc* optional -> Pointer<Void>
// pbClaimBlob : BYTE* -> Pointer<Uint8>
// cbClaimBlob : DWORD -> Uint32
// pOutput : BCryptBufferDesc* out -> Pointer<Void>
// dwFlags : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function NCryptVerifyClaim(
  hSubjectKey: NativeUInt;   // NCRYPT_KEY_HANDLE
  hAuthorityKey: NativeUInt;   // NCRYPT_KEY_HANDLE optional
  dwClaimType: DWORD;   // DWORD
  pParameterList: Pointer;   // BCryptBufferDesc* optional
  pbClaimBlob: Pointer;   // BYTE*
  cbClaimBlob: DWORD;   // DWORD
  pOutput: Pointer;   // BCryptBufferDesc* out
  dwFlags: DWORD   // DWORD
): Integer; stdcall;
  external 'ncrypt.dll' name 'NCryptVerifyClaim';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "NCryptVerifyClaim"
  c_NCryptVerifyClaim :: CUIntPtr -> CUIntPtr -> Word32 -> Ptr () -> Ptr Word8 -> Word32 -> Ptr () -> Word32 -> IO Int32
-- hSubjectKey : NCRYPT_KEY_HANDLE -> CUIntPtr
-- hAuthorityKey : NCRYPT_KEY_HANDLE optional -> CUIntPtr
-- dwClaimType : DWORD -> Word32
-- pParameterList : BCryptBufferDesc* optional -> Ptr ()
-- pbClaimBlob : BYTE* -> Ptr Word8
-- cbClaimBlob : DWORD -> Word32
-- pOutput : BCryptBufferDesc* out -> Ptr ()
-- dwFlags : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let ncryptverifyclaim =
  foreign "NCryptVerifyClaim"
    (size_t @-> size_t @-> uint32_t @-> (ptr void) @-> (ptr uint8_t) @-> uint32_t @-> (ptr void) @-> uint32_t @-> returning int32_t)
(* hSubjectKey : NCRYPT_KEY_HANDLE -> size_t *)
(* hAuthorityKey : NCRYPT_KEY_HANDLE optional -> size_t *)
(* dwClaimType : DWORD -> uint32_t *)
(* pParameterList : BCryptBufferDesc* optional -> (ptr void) *)
(* pbClaimBlob : BYTE* -> (ptr uint8_t) *)
(* cbClaimBlob : DWORD -> uint32_t *)
(* pOutput : BCryptBufferDesc* out -> (ptr void) *)
(* dwFlags : DWORD -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library ncrypt (t "ncrypt.dll"))
(cffi:use-foreign-library ncrypt)

(cffi:defcfun ("NCryptVerifyClaim" ncrypt-verify-claim :convention :stdcall) :int32
  (h-subject-key :uint64)   ; NCRYPT_KEY_HANDLE
  (h-authority-key :uint64)   ; NCRYPT_KEY_HANDLE optional
  (dw-claim-type :uint32)   ; DWORD
  (p-parameter-list :pointer)   ; BCryptBufferDesc* optional
  (pb-claim-blob :pointer)   ; BYTE*
  (cb-claim-blob :uint32)   ; DWORD
  (p-output :pointer)   ; BCryptBufferDesc* out
  (dw-flags :uint32))   ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $NCryptVerifyClaim = Win32::API::More->new('ncrypt',
    'int NCryptVerifyClaim(WPARAM hSubjectKey, WPARAM hAuthorityKey, DWORD dwClaimType, LPVOID pParameterList, LPVOID pbClaimBlob, DWORD cbClaimBlob, LPVOID pOutput, DWORD dwFlags)');
# my $ret = $NCryptVerifyClaim->Call($hSubjectKey, $hAuthorityKey, $dwClaimType, $pParameterList, $pbClaimBlob, $cbClaimBlob, $pOutput, $dwFlags);
# hSubjectKey : NCRYPT_KEY_HANDLE -> WPARAM
# hAuthorityKey : NCRYPT_KEY_HANDLE optional -> WPARAM
# dwClaimType : DWORD -> DWORD
# pParameterList : BCryptBufferDesc* optional -> LPVOID
# pbClaimBlob : BYTE* -> LPVOID
# cbClaimBlob : DWORD -> DWORD
# pOutput : BCryptBufferDesc* out -> LPVOID
# dwFlags : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

使用する型