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
dwKeySpecDWORDinoptional
dwCertEncodingTypeCERT_QUERY_ENCODING_TYPEin
pbEncodedToBeSignedBYTE*in
cbEncodedToBeSignedDWORDin
pSignatureAlgorithmCRYPT_ALGORITHM_IDENTIFIER*in
pvHashAuxInfovoid*inoptional
pbSignatureBYTE*outoptional
pcbSignatureDWORD*inout

戻り値の型: BOOL

各言語での呼び出し定義

// 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 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)。