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

CryptSignCertificate

関数
被署名データに署名して証明書署名値を生成する。
DLLCRYPT32.dll呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

BOOL CryptSignCertificate(
    HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hCryptProvOrNCryptKey,   // optional
    DWORD dwKeySpec,   // optional
    CERT_QUERY_ENCODING_TYPE dwCertEncodingType,
    const BYTE* pbEncodedToBeSigned,
    DWORD cbEncodedToBeSigned,
    CRYPT_ALGORITHM_IDENTIFIER* pSignatureAlgorithm,
    const void* pvHashAuxInfo,   // optional
    BYTE* pbSignature,   // optional
    DWORD* pcbSignature
);

パラメーター

名前方向説明
hCryptProvOrNCryptKeyHCRYPTPROV_OR_NCRYPT_KEY_HANDLEinoptional署名に使うCSPまたはCNGキーのハンドル。秘密鍵を保持するプロバイダーを指す。
dwKeySpecDWORDinoptionalプロバイダーのコンテナーから使用する秘密キーを指定します。AT_KEYEXCHANGE または AT_SIGNATURE を指定できます。hCryptProvOrNCryptKey パラメーターに NCRYPT_KEY_HANDLE を使用する場合、このパラメーターは無視されます。
dwCertEncodingTypeCERT_QUERY_ENCODING_TYPEin

使用するエンコード種類を指定します。次の例のように、証明書とメッセージの両方のエンコード種類をビットごとの OR 演算で組み合わせて指定することは常に許容されます:

X509_ASN_ENCODING | PKCS_7_ASN_ENCODING

現在定義されているエンコード種類は次のとおりです:

pbEncodedToBeSignedBYTE*in署名対象となるエンコードされたコンテンツへのポインター。
cbEncodedToBeSignedDWORDinエンコードされたコンテンツ pbEncodedToBeSigned のサイズ (バイト単位)。
pSignatureAlgorithmCRYPT_ALGORITHM_IDENTIFIER*in

pszObjId メンバーが次のいずれかに設定された CRYPT_ALGORITHM_IDENTIFIER 構造体へのポインター:

  • szOID_RSA_MD5RSA
  • szOID_RSA_SHA1RSA
  • szOID_X957_SHA1DSA
  • szOID_RSA_SSA_PSS
  • szOID_ECDSA_SPECIFIED
署名アルゴリズムがハッシュアルゴリズムの場合、署名には暗号化されていないハッシュオクテットのみが含まれます。ハッシュの暗号化に秘密キーは使用されません。dwKeySpec は使用されず、ハッシュ処理に適切な既定の CSP を使用できる場合は hCryptProvOrNCryptKeyNULL にできます。
pvHashAuxInfovoid*inoptional現在使用されていません。NULL である必要があります。
pbSignatureBYTE*outoptional

コンテンツの署名済みハッシュを受け取るバッファーへのポインター。

このパラメーターは、メモリ割り当てのためにこの情報のサイズを設定する目的で NULL にできます。詳細については、Retrieving Data of Unknown Length を参照してください。

pcbSignatureDWORD*inout

pbSignature パラメーターが指すバッファーのサイズ (バイト単位) を格納する DWORD へのポインター。関数が戻ると、この DWORD にはバッファーに格納された、または格納されるバイト数が入ります。

バッファーで返されるデータを処理する際、アプリケーションは返されたデータの実際のサイズを使用する必要があります。実際のサイズは、入力時に指定したバッファーのサイズよりわずかに小さくなる場合があります。(入力時には通常、出力されうる最大データが収まるよう十分に大きいバッファーサイズを指定します。) 出力時には、このパラメーターが指す変数が、バッファーにコピーされたデータの実際のサイズを反映するよう更新されます。

戻り値の型: BOOL

公式ドキュメント

CryptSignCertificate 関数は、エンコードされた署名対象コンテンツ内の「署名対象 (to be signed)」情報に署名します。

戻り値

関数が成功した場合、戻り値は 0 以外 (TRUE) です。

関数が失敗した場合、戻り値は 0 (FALSE) です。拡張エラー情報を取得するには、GetLastError を呼び出します。

呼び出される関数 CryptCreateHashCryptSignHashCryptHashData からのエラーがこの関数に伝播される場合があります。
この関数には次のエラーコードがあります。
戻りコード 説明
ERROR_MORE_DATA
pbSignature パラメーターで指定したバッファーが返されるデータを保持するのに十分な大きさでない場合、関数は ERROR_MORE_DATA コードを設定し、必要なバッファーサイズ (バイト単位) を pcbSignature が指す変数に格納します。
NTE_BAD_ALGID
署名アルゴリズムのオブジェクト識別子 (OID) が、既知または対応しているハッシュアルゴリズムにマッピングされません。
出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

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

BOOL CryptSignCertificate(
    HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hCryptProvOrNCryptKey,   // optional
    DWORD dwKeySpec,   // optional
    CERT_QUERY_ENCODING_TYPE dwCertEncodingType,
    const BYTE* pbEncodedToBeSigned,
    DWORD cbEncodedToBeSigned,
    CRYPT_ALGORITHM_IDENTIFIER* pSignatureAlgorithm,
    const void* pvHashAuxInfo,   // optional
    BYTE* pbSignature,   // optional
    DWORD* pcbSignature
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("CRYPT32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool CryptSignCertificate(
    UIntPtr hCryptProvOrNCryptKey,   // HCRYPTPROV_OR_NCRYPT_KEY_HANDLE optional
    uint dwKeySpec,   // DWORD optional
    uint dwCertEncodingType,   // CERT_QUERY_ENCODING_TYPE
    IntPtr pbEncodedToBeSigned,   // BYTE*
    uint cbEncodedToBeSigned,   // DWORD
    IntPtr pSignatureAlgorithm,   // CRYPT_ALGORITHM_IDENTIFIER*
    IntPtr pvHashAuxInfo,   // void* optional
    IntPtr pbSignature,   // BYTE* optional, out
    ref uint pcbSignature   // DWORD* in/out
);
<DllImport("CRYPT32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function CryptSignCertificate(
    hCryptProvOrNCryptKey As UIntPtr,   ' HCRYPTPROV_OR_NCRYPT_KEY_HANDLE optional
    dwKeySpec As UInteger,   ' DWORD optional
    dwCertEncodingType As UInteger,   ' CERT_QUERY_ENCODING_TYPE
    pbEncodedToBeSigned As IntPtr,   ' BYTE*
    cbEncodedToBeSigned As UInteger,   ' DWORD
    pSignatureAlgorithm As IntPtr,   ' CRYPT_ALGORITHM_IDENTIFIER*
    pvHashAuxInfo As IntPtr,   ' void* optional
    pbSignature As IntPtr,   ' BYTE* optional, out
    ByRef pcbSignature As UInteger   ' DWORD* in/out
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' hCryptProvOrNCryptKey : HCRYPTPROV_OR_NCRYPT_KEY_HANDLE optional
' dwKeySpec : DWORD optional
' dwCertEncodingType : CERT_QUERY_ENCODING_TYPE
' pbEncodedToBeSigned : BYTE*
' cbEncodedToBeSigned : DWORD
' pSignatureAlgorithm : CRYPT_ALGORITHM_IDENTIFIER*
' pvHashAuxInfo : void* optional
' pbSignature : BYTE* optional, out
' pcbSignature : DWORD* in/out
Declare PtrSafe Function CryptSignCertificate Lib "crypt32" ( _
    ByVal hCryptProvOrNCryptKey As LongPtr, _
    ByVal dwKeySpec As Long, _
    ByVal dwCertEncodingType As Long, _
    ByVal pbEncodedToBeSigned As LongPtr, _
    ByVal cbEncodedToBeSigned As Long, _
    ByVal pSignatureAlgorithm As LongPtr, _
    ByVal pvHashAuxInfo As LongPtr, _
    ByVal pbSignature As LongPtr, _
    ByRef pcbSignature As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

CryptSignCertificate = ctypes.windll.crypt32.CryptSignCertificate
CryptSignCertificate.restype = wintypes.BOOL
CryptSignCertificate.argtypes = [
    ctypes.c_size_t,  # hCryptProvOrNCryptKey : HCRYPTPROV_OR_NCRYPT_KEY_HANDLE optional
    wintypes.DWORD,  # dwKeySpec : DWORD optional
    wintypes.DWORD,  # dwCertEncodingType : CERT_QUERY_ENCODING_TYPE
    ctypes.POINTER(ctypes.c_ubyte),  # pbEncodedToBeSigned : BYTE*
    wintypes.DWORD,  # cbEncodedToBeSigned : DWORD
    ctypes.c_void_p,  # pSignatureAlgorithm : CRYPT_ALGORITHM_IDENTIFIER*
    ctypes.POINTER(None),  # pvHashAuxInfo : void* optional
    ctypes.POINTER(ctypes.c_ubyte),  # pbSignature : BYTE* optional, out
    ctypes.POINTER(wintypes.DWORD),  # pcbSignature : DWORD* in/out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('CRYPT32.dll')
CryptSignCertificate = Fiddle::Function.new(
  lib['CryptSignCertificate'],
  [
    Fiddle::TYPE_UINTPTR_T,  # hCryptProvOrNCryptKey : HCRYPTPROV_OR_NCRYPT_KEY_HANDLE optional
    -Fiddle::TYPE_INT,  # dwKeySpec : DWORD optional
    -Fiddle::TYPE_INT,  # dwCertEncodingType : CERT_QUERY_ENCODING_TYPE
    Fiddle::TYPE_VOIDP,  # pbEncodedToBeSigned : BYTE*
    -Fiddle::TYPE_INT,  # cbEncodedToBeSigned : DWORD
    Fiddle::TYPE_VOIDP,  # pSignatureAlgorithm : CRYPT_ALGORITHM_IDENTIFIER*
    Fiddle::TYPE_VOIDP,  # pvHashAuxInfo : void* optional
    Fiddle::TYPE_VOIDP,  # pbSignature : BYTE* optional, out
    Fiddle::TYPE_VOIDP,  # pcbSignature : DWORD* in/out
  ],
  Fiddle::TYPE_INT)
#[link(name = "crypt32")]
extern "system" {
    fn CryptSignCertificate(
        hCryptProvOrNCryptKey: usize,  // HCRYPTPROV_OR_NCRYPT_KEY_HANDLE optional
        dwKeySpec: u32,  // DWORD optional
        dwCertEncodingType: u32,  // CERT_QUERY_ENCODING_TYPE
        pbEncodedToBeSigned: *const u8,  // BYTE*
        cbEncodedToBeSigned: u32,  // DWORD
        pSignatureAlgorithm: *mut CRYPT_ALGORITHM_IDENTIFIER,  // CRYPT_ALGORITHM_IDENTIFIER*
        pvHashAuxInfo: *const (),  // void* optional
        pbSignature: *mut u8,  // BYTE* optional, out
        pcbSignature: *mut u32  // DWORD* in/out
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("CRYPT32.dll", SetLastError = true)]
public static extern bool CryptSignCertificate(UIntPtr hCryptProvOrNCryptKey, uint dwKeySpec, uint dwCertEncodingType, IntPtr pbEncodedToBeSigned, uint cbEncodedToBeSigned, IntPtr pSignatureAlgorithm, IntPtr pvHashAuxInfo, IntPtr pbSignature, ref uint pcbSignature);
"@
$api = Add-Type -MemberDefinition $sig -Name 'CRYPT32_CryptSignCertificate' -Namespace Win32 -PassThru
# $api::CryptSignCertificate(hCryptProvOrNCryptKey, dwKeySpec, dwCertEncodingType, pbEncodedToBeSigned, cbEncodedToBeSigned, pSignatureAlgorithm, pvHashAuxInfo, pbSignature, pcbSignature)
#uselib "CRYPT32.dll"
#func global CryptSignCertificate "CryptSignCertificate" sptr, sptr, sptr, sptr, sptr, sptr, sptr, sptr, sptr
; CryptSignCertificate hCryptProvOrNCryptKey, dwKeySpec, dwCertEncodingType, varptr(pbEncodedToBeSigned), cbEncodedToBeSigned, varptr(pSignatureAlgorithm), pvHashAuxInfo, varptr(pbSignature), varptr(pcbSignature)   ; 戻り値は stat
; hCryptProvOrNCryptKey : HCRYPTPROV_OR_NCRYPT_KEY_HANDLE optional -> "sptr"
; dwKeySpec : DWORD optional -> "sptr"
; dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> "sptr"
; pbEncodedToBeSigned : BYTE* -> "sptr"
; cbEncodedToBeSigned : DWORD -> "sptr"
; pSignatureAlgorithm : CRYPT_ALGORITHM_IDENTIFIER* -> "sptr"
; pvHashAuxInfo : void* optional -> "sptr"
; pbSignature : BYTE* optional, out -> "sptr"
; pcbSignature : DWORD* in/out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "CRYPT32.dll"
#cfunc global CryptSignCertificate "CryptSignCertificate" sptr, int, int, var, int, var, sptr, var, var
; res = CryptSignCertificate(hCryptProvOrNCryptKey, dwKeySpec, dwCertEncodingType, pbEncodedToBeSigned, cbEncodedToBeSigned, pSignatureAlgorithm, pvHashAuxInfo, pbSignature, pcbSignature)
; hCryptProvOrNCryptKey : HCRYPTPROV_OR_NCRYPT_KEY_HANDLE optional -> "sptr"
; dwKeySpec : DWORD optional -> "int"
; dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> "int"
; pbEncodedToBeSigned : BYTE* -> "var"
; cbEncodedToBeSigned : DWORD -> "int"
; pSignatureAlgorithm : CRYPT_ALGORITHM_IDENTIFIER* -> "var"
; pvHashAuxInfo : void* optional -> "sptr"
; pbSignature : BYTE* optional, out -> "var"
; pcbSignature : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL CryptSignCertificate(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hCryptProvOrNCryptKey, DWORD dwKeySpec, CERT_QUERY_ENCODING_TYPE dwCertEncodingType, BYTE* pbEncodedToBeSigned, DWORD cbEncodedToBeSigned, CRYPT_ALGORITHM_IDENTIFIER* pSignatureAlgorithm, void* pvHashAuxInfo, BYTE* pbSignature, DWORD* pcbSignature)
#uselib "CRYPT32.dll"
#cfunc global CryptSignCertificate "CryptSignCertificate" intptr, int, int, var, int, var, intptr, var, var
; res = CryptSignCertificate(hCryptProvOrNCryptKey, dwKeySpec, dwCertEncodingType, pbEncodedToBeSigned, cbEncodedToBeSigned, pSignatureAlgorithm, pvHashAuxInfo, pbSignature, pcbSignature)
; hCryptProvOrNCryptKey : HCRYPTPROV_OR_NCRYPT_KEY_HANDLE optional -> "intptr"
; dwKeySpec : DWORD optional -> "int"
; dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> "int"
; pbEncodedToBeSigned : BYTE* -> "var"
; cbEncodedToBeSigned : DWORD -> "int"
; pSignatureAlgorithm : CRYPT_ALGORITHM_IDENTIFIER* -> "var"
; pvHashAuxInfo : void* optional -> "intptr"
; pbSignature : BYTE* optional, out -> "var"
; pcbSignature : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	crypt32 = windows.NewLazySystemDLL("CRYPT32.dll")
	procCryptSignCertificate = crypt32.NewProc("CryptSignCertificate")
)

// hCryptProvOrNCryptKey (HCRYPTPROV_OR_NCRYPT_KEY_HANDLE optional), dwKeySpec (DWORD optional), dwCertEncodingType (CERT_QUERY_ENCODING_TYPE), pbEncodedToBeSigned (BYTE*), cbEncodedToBeSigned (DWORD), pSignatureAlgorithm (CRYPT_ALGORITHM_IDENTIFIER*), pvHashAuxInfo (void* optional), pbSignature (BYTE* optional, out), pcbSignature (DWORD* in/out)
r1, _, err := procCryptSignCertificate.Call(
	uintptr(hCryptProvOrNCryptKey),
	uintptr(dwKeySpec),
	uintptr(dwCertEncodingType),
	uintptr(pbEncodedToBeSigned),
	uintptr(cbEncodedToBeSigned),
	uintptr(pSignatureAlgorithm),
	uintptr(pvHashAuxInfo),
	uintptr(pbSignature),
	uintptr(pcbSignature),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function CryptSignCertificate(
  hCryptProvOrNCryptKey: NativeUInt;   // HCRYPTPROV_OR_NCRYPT_KEY_HANDLE optional
  dwKeySpec: DWORD;   // DWORD optional
  dwCertEncodingType: DWORD;   // CERT_QUERY_ENCODING_TYPE
  pbEncodedToBeSigned: Pointer;   // BYTE*
  cbEncodedToBeSigned: DWORD;   // DWORD
  pSignatureAlgorithm: Pointer;   // CRYPT_ALGORITHM_IDENTIFIER*
  pvHashAuxInfo: Pointer;   // void* optional
  pbSignature: Pointer;   // BYTE* optional, out
  pcbSignature: Pointer   // DWORD* in/out
): BOOL; stdcall;
  external 'CRYPT32.dll' name 'CryptSignCertificate';
result := DllCall("CRYPT32\CryptSignCertificate"
    , "UPtr", hCryptProvOrNCryptKey   ; HCRYPTPROV_OR_NCRYPT_KEY_HANDLE optional
    , "UInt", dwKeySpec   ; DWORD optional
    , "UInt", dwCertEncodingType   ; CERT_QUERY_ENCODING_TYPE
    , "Ptr", pbEncodedToBeSigned   ; BYTE*
    , "UInt", cbEncodedToBeSigned   ; DWORD
    , "Ptr", pSignatureAlgorithm   ; CRYPT_ALGORITHM_IDENTIFIER*
    , "Ptr", pvHashAuxInfo   ; void* optional
    , "Ptr", pbSignature   ; BYTE* optional, out
    , "Ptr", pcbSignature   ; DWORD* in/out
    , "Int")   ; return: BOOL
●CryptSignCertificate(hCryptProvOrNCryptKey, dwKeySpec, dwCertEncodingType, pbEncodedToBeSigned, cbEncodedToBeSigned, pSignatureAlgorithm, pvHashAuxInfo, pbSignature, pcbSignature) = DLL("CRYPT32.dll", "bool CryptSignCertificate(int, dword, dword, void*, dword, void*, void*, void*, void*)")
# 呼び出し: CryptSignCertificate(hCryptProvOrNCryptKey, dwKeySpec, dwCertEncodingType, pbEncodedToBeSigned, cbEncodedToBeSigned, pSignatureAlgorithm, pvHashAuxInfo, pbSignature, pcbSignature)
# hCryptProvOrNCryptKey : HCRYPTPROV_OR_NCRYPT_KEY_HANDLE optional -> "int"
# dwKeySpec : DWORD optional -> "dword"
# dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> "dword"
# pbEncodedToBeSigned : BYTE* -> "void*"
# cbEncodedToBeSigned : DWORD -> "dword"
# pSignatureAlgorithm : CRYPT_ALGORITHM_IDENTIFIER* -> "void*"
# pvHashAuxInfo : void* optional -> "void*"
# pbSignature : BYTE* optional, out -> "void*"
# pcbSignature : DWORD* in/out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "crypt32" fn CryptSignCertificate(
    hCryptProvOrNCryptKey: usize, // HCRYPTPROV_OR_NCRYPT_KEY_HANDLE optional
    dwKeySpec: u32, // DWORD optional
    dwCertEncodingType: u32, // CERT_QUERY_ENCODING_TYPE
    pbEncodedToBeSigned: [*c]u8, // BYTE*
    cbEncodedToBeSigned: u32, // DWORD
    pSignatureAlgorithm: [*c]CRYPT_ALGORITHM_IDENTIFIER, // CRYPT_ALGORITHM_IDENTIFIER*
    pvHashAuxInfo: ?*anyopaque, // void* optional
    pbSignature: [*c]u8, // BYTE* optional, out
    pcbSignature: [*c]u32 // DWORD* in/out
) callconv(std.os.windows.WINAPI) i32;
proc CryptSignCertificate(
    hCryptProvOrNCryptKey: uint,  # HCRYPTPROV_OR_NCRYPT_KEY_HANDLE optional
    dwKeySpec: uint32,  # DWORD optional
    dwCertEncodingType: uint32,  # CERT_QUERY_ENCODING_TYPE
    pbEncodedToBeSigned: ptr uint8,  # BYTE*
    cbEncodedToBeSigned: uint32,  # DWORD
    pSignatureAlgorithm: ptr CRYPT_ALGORITHM_IDENTIFIER,  # CRYPT_ALGORITHM_IDENTIFIER*
    pvHashAuxInfo: pointer,  # void* optional
    pbSignature: ptr uint8,  # BYTE* optional, out
    pcbSignature: ptr uint32  # DWORD* in/out
): int32 {.importc: "CryptSignCertificate", stdcall, dynlib: "CRYPT32.dll".}
pragma(lib, "crypt32");
extern(Windows)
int CryptSignCertificate(
    size_t hCryptProvOrNCryptKey,   // HCRYPTPROV_OR_NCRYPT_KEY_HANDLE optional
    uint dwKeySpec,   // DWORD optional
    uint dwCertEncodingType,   // CERT_QUERY_ENCODING_TYPE
    ubyte* pbEncodedToBeSigned,   // BYTE*
    uint cbEncodedToBeSigned,   // DWORD
    CRYPT_ALGORITHM_IDENTIFIER* pSignatureAlgorithm,   // CRYPT_ALGORITHM_IDENTIFIER*
    void* pvHashAuxInfo,   // void* optional
    ubyte* pbSignature,   // BYTE* optional, out
    uint* pcbSignature   // DWORD* in/out
);
ccall((:CryptSignCertificate, "CRYPT32.dll"), stdcall, Int32,
      (Csize_t, UInt32, UInt32, Ptr{UInt8}, UInt32, Ptr{CRYPT_ALGORITHM_IDENTIFIER}, Ptr{Cvoid}, Ptr{UInt8}, Ptr{UInt32}),
      hCryptProvOrNCryptKey, dwKeySpec, dwCertEncodingType, pbEncodedToBeSigned, cbEncodedToBeSigned, pSignatureAlgorithm, pvHashAuxInfo, pbSignature, pcbSignature)
# hCryptProvOrNCryptKey : HCRYPTPROV_OR_NCRYPT_KEY_HANDLE optional -> Csize_t
# dwKeySpec : DWORD optional -> UInt32
# dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> UInt32
# pbEncodedToBeSigned : BYTE* -> Ptr{UInt8}
# cbEncodedToBeSigned : DWORD -> UInt32
# pSignatureAlgorithm : CRYPT_ALGORITHM_IDENTIFIER* -> Ptr{CRYPT_ALGORITHM_IDENTIFIER}
# pvHashAuxInfo : void* optional -> Ptr{Cvoid}
# pbSignature : BYTE* optional, out -> Ptr{UInt8}
# pcbSignature : DWORD* in/out -> Ptr{UInt32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t CryptSignCertificate(
    uintptr_t hCryptProvOrNCryptKey,
    uint32_t dwKeySpec,
    uint32_t dwCertEncodingType,
    uint8_t* pbEncodedToBeSigned,
    uint32_t cbEncodedToBeSigned,
    void* pSignatureAlgorithm,
    void* pvHashAuxInfo,
    uint8_t* pbSignature,
    uint32_t* pcbSignature);
]]
local crypt32 = ffi.load("crypt32")
-- crypt32.CryptSignCertificate(hCryptProvOrNCryptKey, dwKeySpec, dwCertEncodingType, pbEncodedToBeSigned, cbEncodedToBeSigned, pSignatureAlgorithm, pvHashAuxInfo, pbSignature, pcbSignature)
-- hCryptProvOrNCryptKey : HCRYPTPROV_OR_NCRYPT_KEY_HANDLE optional
-- dwKeySpec : DWORD optional
-- dwCertEncodingType : CERT_QUERY_ENCODING_TYPE
-- pbEncodedToBeSigned : BYTE*
-- cbEncodedToBeSigned : DWORD
-- pSignatureAlgorithm : CRYPT_ALGORITHM_IDENTIFIER*
-- pvHashAuxInfo : void* optional
-- pbSignature : BYTE* optional, out
-- pcbSignature : DWORD* in/out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('CRYPT32.dll');
const CryptSignCertificate = lib.func('__stdcall', 'CryptSignCertificate', 'int32_t', ['uintptr_t', 'uint32_t', 'uint32_t', 'uint8_t *', 'uint32_t', 'void *', 'void *', 'uint8_t *', 'uint32_t *']);
// CryptSignCertificate(hCryptProvOrNCryptKey, dwKeySpec, dwCertEncodingType, pbEncodedToBeSigned, cbEncodedToBeSigned, pSignatureAlgorithm, pvHashAuxInfo, pbSignature, pcbSignature)
// hCryptProvOrNCryptKey : HCRYPTPROV_OR_NCRYPT_KEY_HANDLE optional -> 'uintptr_t'
// dwKeySpec : DWORD optional -> 'uint32_t'
// dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> 'uint32_t'
// pbEncodedToBeSigned : BYTE* -> 'uint8_t *'
// cbEncodedToBeSigned : DWORD -> 'uint32_t'
// pSignatureAlgorithm : CRYPT_ALGORITHM_IDENTIFIER* -> 'void *'
// pvHashAuxInfo : void* optional -> 'void *'
// pbSignature : BYTE* optional, out -> 'uint8_t *'
// pcbSignature : DWORD* in/out -> 'uint32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("CRYPT32.dll", {
  CryptSignCertificate: { parameters: ["usize", "u32", "u32", "pointer", "u32", "pointer", "pointer", "pointer", "pointer"], result: "i32" },
});
// lib.symbols.CryptSignCertificate(hCryptProvOrNCryptKey, dwKeySpec, dwCertEncodingType, pbEncodedToBeSigned, cbEncodedToBeSigned, pSignatureAlgorithm, pvHashAuxInfo, pbSignature, pcbSignature)
// hCryptProvOrNCryptKey : HCRYPTPROV_OR_NCRYPT_KEY_HANDLE optional -> "usize"
// dwKeySpec : DWORD optional -> "u32"
// dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> "u32"
// pbEncodedToBeSigned : BYTE* -> "pointer"
// cbEncodedToBeSigned : DWORD -> "u32"
// pSignatureAlgorithm : CRYPT_ALGORITHM_IDENTIFIER* -> "pointer"
// pvHashAuxInfo : void* optional -> "pointer"
// pbSignature : BYTE* optional, out -> "pointer"
// pcbSignature : DWORD* in/out -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t CryptSignCertificate(
    size_t hCryptProvOrNCryptKey,
    uint32_t dwKeySpec,
    uint32_t dwCertEncodingType,
    uint8_t* pbEncodedToBeSigned,
    uint32_t cbEncodedToBeSigned,
    void* pSignatureAlgorithm,
    void* pvHashAuxInfo,
    uint8_t* pbSignature,
    uint32_t* pcbSignature);
C, "CRYPT32.dll");
// $ffi->CryptSignCertificate(hCryptProvOrNCryptKey, dwKeySpec, dwCertEncodingType, pbEncodedToBeSigned, cbEncodedToBeSigned, pSignatureAlgorithm, pvHashAuxInfo, pbSignature, pcbSignature);
// hCryptProvOrNCryptKey : HCRYPTPROV_OR_NCRYPT_KEY_HANDLE optional
// dwKeySpec : DWORD optional
// dwCertEncodingType : CERT_QUERY_ENCODING_TYPE
// pbEncodedToBeSigned : BYTE*
// cbEncodedToBeSigned : DWORD
// pSignatureAlgorithm : CRYPT_ALGORITHM_IDENTIFIER*
// pvHashAuxInfo : void* optional
// pbSignature : BYTE* optional, out
// pcbSignature : DWORD* in/out
// 構造体/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 Crypt32 extends StdCallLibrary {
    Crypt32 INSTANCE = Native.load("crypt32", Crypt32.class);
    boolean CryptSignCertificate(
        long hCryptProvOrNCryptKey,   // HCRYPTPROV_OR_NCRYPT_KEY_HANDLE optional
        int dwKeySpec,   // DWORD optional
        int dwCertEncodingType,   // CERT_QUERY_ENCODING_TYPE
        byte[] pbEncodedToBeSigned,   // BYTE*
        int cbEncodedToBeSigned,   // DWORD
        Pointer pSignatureAlgorithm,   // CRYPT_ALGORITHM_IDENTIFIER*
        Pointer pvHashAuxInfo,   // void* optional
        byte[] pbSignature,   // BYTE* optional, out
        IntByReference pcbSignature   // DWORD* in/out
    );
}
@[Link("crypt32")]
lib LibCRYPT32
  fun CryptSignCertificate = CryptSignCertificate(
    hCryptProvOrNCryptKey : LibC::SizeT,   # HCRYPTPROV_OR_NCRYPT_KEY_HANDLE optional
    dwKeySpec : UInt32,   # DWORD optional
    dwCertEncodingType : UInt32,   # CERT_QUERY_ENCODING_TYPE
    pbEncodedToBeSigned : UInt8*,   # BYTE*
    cbEncodedToBeSigned : UInt32,   # DWORD
    pSignatureAlgorithm : CRYPT_ALGORITHM_IDENTIFIER*,   # CRYPT_ALGORITHM_IDENTIFIER*
    pvHashAuxInfo : Void*,   # void* optional
    pbSignature : UInt8*,   # BYTE* optional, out
    pcbSignature : UInt32*   # DWORD* in/out
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef CryptSignCertificateNative = Int32 Function(UintPtr, Uint32, Uint32, Pointer<Uint8>, Uint32, Pointer<Void>, Pointer<Void>, Pointer<Uint8>, Pointer<Uint32>);
typedef CryptSignCertificateDart = int Function(int, int, int, Pointer<Uint8>, int, Pointer<Void>, Pointer<Void>, Pointer<Uint8>, Pointer<Uint32>);
final CryptSignCertificate = DynamicLibrary.open('CRYPT32.dll')
    .lookupFunction<CryptSignCertificateNative, CryptSignCertificateDart>('CryptSignCertificate');
// hCryptProvOrNCryptKey : HCRYPTPROV_OR_NCRYPT_KEY_HANDLE optional -> UintPtr
// dwKeySpec : DWORD optional -> Uint32
// dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> Uint32
// pbEncodedToBeSigned : BYTE* -> Pointer<Uint8>
// cbEncodedToBeSigned : DWORD -> Uint32
// pSignatureAlgorithm : CRYPT_ALGORITHM_IDENTIFIER* -> Pointer<Void>
// pvHashAuxInfo : void* optional -> Pointer<Void>
// pbSignature : BYTE* optional, out -> Pointer<Uint8>
// pcbSignature : DWORD* in/out -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function CryptSignCertificate(
  hCryptProvOrNCryptKey: NativeUInt;   // HCRYPTPROV_OR_NCRYPT_KEY_HANDLE optional
  dwKeySpec: DWORD;   // DWORD optional
  dwCertEncodingType: DWORD;   // CERT_QUERY_ENCODING_TYPE
  pbEncodedToBeSigned: Pointer;   // BYTE*
  cbEncodedToBeSigned: DWORD;   // DWORD
  pSignatureAlgorithm: Pointer;   // CRYPT_ALGORITHM_IDENTIFIER*
  pvHashAuxInfo: Pointer;   // void* optional
  pbSignature: Pointer;   // BYTE* optional, out
  pcbSignature: Pointer   // DWORD* in/out
): BOOL; stdcall;
  external 'CRYPT32.dll' name 'CryptSignCertificate';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "CryptSignCertificate"
  c_CryptSignCertificate :: CUIntPtr -> Word32 -> Word32 -> Ptr Word8 -> Word32 -> Ptr () -> Ptr () -> Ptr Word8 -> Ptr Word32 -> IO CInt
-- hCryptProvOrNCryptKey : HCRYPTPROV_OR_NCRYPT_KEY_HANDLE optional -> CUIntPtr
-- dwKeySpec : DWORD optional -> Word32
-- dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> Word32
-- pbEncodedToBeSigned : BYTE* -> Ptr Word8
-- cbEncodedToBeSigned : DWORD -> Word32
-- pSignatureAlgorithm : CRYPT_ALGORITHM_IDENTIFIER* -> Ptr ()
-- pvHashAuxInfo : void* optional -> Ptr ()
-- pbSignature : BYTE* optional, out -> Ptr Word8
-- pcbSignature : DWORD* in/out -> Ptr Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let cryptsigncertificate =
  foreign "CryptSignCertificate"
    (size_t @-> uint32_t @-> uint32_t @-> (ptr uint8_t) @-> uint32_t @-> (ptr void) @-> (ptr void) @-> (ptr uint8_t) @-> (ptr uint32_t) @-> returning int32_t)
(* hCryptProvOrNCryptKey : HCRYPTPROV_OR_NCRYPT_KEY_HANDLE optional -> size_t *)
(* dwKeySpec : DWORD optional -> uint32_t *)
(* dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> uint32_t *)
(* pbEncodedToBeSigned : BYTE* -> (ptr uint8_t) *)
(* cbEncodedToBeSigned : DWORD -> uint32_t *)
(* pSignatureAlgorithm : CRYPT_ALGORITHM_IDENTIFIER* -> (ptr void) *)
(* pvHashAuxInfo : void* optional -> (ptr void) *)
(* pbSignature : BYTE* optional, out -> (ptr uint8_t) *)
(* pcbSignature : DWORD* in/out -> (ptr uint32_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library crypt32 (t "CRYPT32.dll"))
(cffi:use-foreign-library crypt32)

(cffi:defcfun ("CryptSignCertificate" crypt-sign-certificate :convention :stdcall) :int32
  (h-crypt-prov-or-ncrypt-key :uint64)   ; HCRYPTPROV_OR_NCRYPT_KEY_HANDLE optional
  (dw-key-spec :uint32)   ; DWORD optional
  (dw-cert-encoding-type :uint32)   ; CERT_QUERY_ENCODING_TYPE
  (pb-encoded-to-be-signed :pointer)   ; BYTE*
  (cb-encoded-to-be-signed :uint32)   ; DWORD
  (p-signature-algorithm :pointer)   ; CRYPT_ALGORITHM_IDENTIFIER*
  (pv-hash-aux-info :pointer)   ; void* optional
  (pb-signature :pointer)   ; BYTE* optional, out
  (pcb-signature :pointer))   ; DWORD* in/out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $CryptSignCertificate = Win32::API::More->new('CRYPT32',
    'BOOL CryptSignCertificate(WPARAM hCryptProvOrNCryptKey, DWORD dwKeySpec, DWORD dwCertEncodingType, LPVOID pbEncodedToBeSigned, DWORD cbEncodedToBeSigned, LPVOID pSignatureAlgorithm, LPVOID pvHashAuxInfo, LPVOID pbSignature, LPVOID pcbSignature)');
# my $ret = $CryptSignCertificate->Call($hCryptProvOrNCryptKey, $dwKeySpec, $dwCertEncodingType, $pbEncodedToBeSigned, $cbEncodedToBeSigned, $pSignatureAlgorithm, $pvHashAuxInfo, $pbSignature, $pcbSignature);
# hCryptProvOrNCryptKey : HCRYPTPROV_OR_NCRYPT_KEY_HANDLE optional -> WPARAM
# dwKeySpec : DWORD optional -> DWORD
# dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> DWORD
# pbEncodedToBeSigned : BYTE* -> LPVOID
# cbEncodedToBeSigned : DWORD -> DWORD
# pSignatureAlgorithm : CRYPT_ALGORITHM_IDENTIFIER* -> LPVOID
# pvHashAuxInfo : void* optional -> LPVOID
# pbSignature : BYTE* optional, out -> LPVOID
# pcbSignature : DWORD* in/out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

公式の関連項目
使用する型