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

CryptSignAndEncodeCertificate

関数
証明書構造をエンコードして署名し署名済みデータを生成する。
DLLCRYPT32.dll呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

BOOL CryptSignAndEncodeCertificate(
    HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hCryptProvOrNCryptKey,   // optional
    CERT_KEY_SPEC dwKeySpec,   // optional
    CERT_QUERY_ENCODING_TYPE dwCertEncodingType,
    LPCSTR lpszStructType,
    const void* pvStructInfo,
    CRYPT_ALGORITHM_IDENTIFIER* pSignatureAlgorithm,
    const void* pvHashAuxInfo,   // optional
    BYTE* pbEncoded,   // optional
    DWORD* pcbEncoded
);

パラメーター

名前方向
hCryptProvOrNCryptKeyHCRYPTPROV_OR_NCRYPT_KEY_HANDLEinoptional
dwKeySpecCERT_KEY_SPECinoptional
dwCertEncodingTypeCERT_QUERY_ENCODING_TYPEin
lpszStructTypeLPCSTRin
pvStructInfovoid*in
pSignatureAlgorithmCRYPT_ALGORITHM_IDENTIFIER*in
pvHashAuxInfovoid*inoptional
pbEncodedBYTE*outoptional
pcbEncodedDWORD*inout

戻り値の型: BOOL

各言語での呼び出し定義

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

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

CryptSignAndEncodeCertificate = ctypes.windll.crypt32.CryptSignAndEncodeCertificate
CryptSignAndEncodeCertificate.restype = wintypes.BOOL
CryptSignAndEncodeCertificate.argtypes = [
    ctypes.c_size_t,  # hCryptProvOrNCryptKey : HCRYPTPROV_OR_NCRYPT_KEY_HANDLE optional
    wintypes.DWORD,  # dwKeySpec : CERT_KEY_SPEC optional
    wintypes.DWORD,  # dwCertEncodingType : CERT_QUERY_ENCODING_TYPE
    wintypes.LPCSTR,  # lpszStructType : LPCSTR
    ctypes.POINTER(None),  # pvStructInfo : void*
    ctypes.c_void_p,  # pSignatureAlgorithm : CRYPT_ALGORITHM_IDENTIFIER*
    ctypes.POINTER(None),  # pvHashAuxInfo : void* optional
    ctypes.POINTER(ctypes.c_ubyte),  # pbEncoded : BYTE* optional, out
    ctypes.POINTER(wintypes.DWORD),  # pcbEncoded : DWORD* in/out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('CRYPT32.dll')
CryptSignAndEncodeCertificate = Fiddle::Function.new(
  lib['CryptSignAndEncodeCertificate'],
  [
    Fiddle::TYPE_UINTPTR_T,  # hCryptProvOrNCryptKey : HCRYPTPROV_OR_NCRYPT_KEY_HANDLE optional
    -Fiddle::TYPE_INT,  # dwKeySpec : CERT_KEY_SPEC optional
    -Fiddle::TYPE_INT,  # dwCertEncodingType : CERT_QUERY_ENCODING_TYPE
    Fiddle::TYPE_VOIDP,  # lpszStructType : LPCSTR
    Fiddle::TYPE_VOIDP,  # pvStructInfo : void*
    Fiddle::TYPE_VOIDP,  # pSignatureAlgorithm : CRYPT_ALGORITHM_IDENTIFIER*
    Fiddle::TYPE_VOIDP,  # pvHashAuxInfo : void* optional
    Fiddle::TYPE_VOIDP,  # pbEncoded : BYTE* optional, out
    Fiddle::TYPE_VOIDP,  # pcbEncoded : DWORD* in/out
  ],
  Fiddle::TYPE_INT)
#[link(name = "crypt32")]
extern "system" {
    fn CryptSignAndEncodeCertificate(
        hCryptProvOrNCryptKey: usize,  // HCRYPTPROV_OR_NCRYPT_KEY_HANDLE optional
        dwKeySpec: u32,  // CERT_KEY_SPEC optional
        dwCertEncodingType: u32,  // CERT_QUERY_ENCODING_TYPE
        lpszStructType: *const u8,  // LPCSTR
        pvStructInfo: *const (),  // void*
        pSignatureAlgorithm: *mut CRYPT_ALGORITHM_IDENTIFIER,  // CRYPT_ALGORITHM_IDENTIFIER*
        pvHashAuxInfo: *const (),  // void* optional
        pbEncoded: *mut u8,  // BYTE* optional, out
        pcbEncoded: *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 CryptSignAndEncodeCertificate(UIntPtr hCryptProvOrNCryptKey, uint dwKeySpec, uint dwCertEncodingType, [MarshalAs(UnmanagedType.LPStr)] string lpszStructType, IntPtr pvStructInfo, IntPtr pSignatureAlgorithm, IntPtr pvHashAuxInfo, IntPtr pbEncoded, ref uint pcbEncoded);
"@
$api = Add-Type -MemberDefinition $sig -Name 'CRYPT32_CryptSignAndEncodeCertificate' -Namespace Win32 -PassThru
# $api::CryptSignAndEncodeCertificate(hCryptProvOrNCryptKey, dwKeySpec, dwCertEncodingType, lpszStructType, pvStructInfo, pSignatureAlgorithm, pvHashAuxInfo, pbEncoded, pcbEncoded)
#uselib "CRYPT32.dll"
#func global CryptSignAndEncodeCertificate "CryptSignAndEncodeCertificate" sptr, sptr, sptr, sptr, sptr, sptr, sptr, sptr, sptr
; CryptSignAndEncodeCertificate hCryptProvOrNCryptKey, dwKeySpec, dwCertEncodingType, lpszStructType, pvStructInfo, varptr(pSignatureAlgorithm), pvHashAuxInfo, varptr(pbEncoded), varptr(pcbEncoded)   ; 戻り値は stat
; hCryptProvOrNCryptKey : HCRYPTPROV_OR_NCRYPT_KEY_HANDLE optional -> "sptr"
; dwKeySpec : CERT_KEY_SPEC optional -> "sptr"
; dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> "sptr"
; lpszStructType : LPCSTR -> "sptr"
; pvStructInfo : void* -> "sptr"
; pSignatureAlgorithm : CRYPT_ALGORITHM_IDENTIFIER* -> "sptr"
; pvHashAuxInfo : void* optional -> "sptr"
; pbEncoded : BYTE* optional, out -> "sptr"
; pcbEncoded : DWORD* in/out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "CRYPT32.dll"
#cfunc global CryptSignAndEncodeCertificate "CryptSignAndEncodeCertificate" sptr, int, int, str, sptr, var, sptr, var, var
; res = CryptSignAndEncodeCertificate(hCryptProvOrNCryptKey, dwKeySpec, dwCertEncodingType, lpszStructType, pvStructInfo, pSignatureAlgorithm, pvHashAuxInfo, pbEncoded, pcbEncoded)
; hCryptProvOrNCryptKey : HCRYPTPROV_OR_NCRYPT_KEY_HANDLE optional -> "sptr"
; dwKeySpec : CERT_KEY_SPEC optional -> "int"
; dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> "int"
; lpszStructType : LPCSTR -> "str"
; pvStructInfo : void* -> "sptr"
; pSignatureAlgorithm : CRYPT_ALGORITHM_IDENTIFIER* -> "var"
; pvHashAuxInfo : void* optional -> "sptr"
; pbEncoded : BYTE* optional, out -> "var"
; pcbEncoded : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL CryptSignAndEncodeCertificate(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hCryptProvOrNCryptKey, CERT_KEY_SPEC dwKeySpec, CERT_QUERY_ENCODING_TYPE dwCertEncodingType, LPCSTR lpszStructType, void* pvStructInfo, CRYPT_ALGORITHM_IDENTIFIER* pSignatureAlgorithm, void* pvHashAuxInfo, BYTE* pbEncoded, DWORD* pcbEncoded)
#uselib "CRYPT32.dll"
#cfunc global CryptSignAndEncodeCertificate "CryptSignAndEncodeCertificate" intptr, int, int, str, intptr, var, intptr, var, var
; res = CryptSignAndEncodeCertificate(hCryptProvOrNCryptKey, dwKeySpec, dwCertEncodingType, lpszStructType, pvStructInfo, pSignatureAlgorithm, pvHashAuxInfo, pbEncoded, pcbEncoded)
; hCryptProvOrNCryptKey : HCRYPTPROV_OR_NCRYPT_KEY_HANDLE optional -> "intptr"
; dwKeySpec : CERT_KEY_SPEC optional -> "int"
; dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> "int"
; lpszStructType : LPCSTR -> "str"
; pvStructInfo : void* -> "intptr"
; pSignatureAlgorithm : CRYPT_ALGORITHM_IDENTIFIER* -> "var"
; pvHashAuxInfo : void* optional -> "intptr"
; pbEncoded : BYTE* optional, out -> "var"
; pcbEncoded : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	crypt32 = windows.NewLazySystemDLL("CRYPT32.dll")
	procCryptSignAndEncodeCertificate = crypt32.NewProc("CryptSignAndEncodeCertificate")
)

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