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

SslGetKeyProperty

関数
SSL鍵の指定プロパティを取得する。
DLLncrypt.dll呼出規約winapi

シグネチャ

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

HRESULT SslGetKeyProperty(
    NCRYPT_KEY_HANDLE hKey,
    LPCWSTR pszProperty,
    BYTE** ppbOutput,
    DWORD* pcbOutput,
    DWORD dwFlags
);

パラメーター

名前方向説明
hKeyNCRYPT_KEY_HANDLEinプロパティを取得する対象の鍵ハンドル。
pszPropertyLPCWSTRin取得するプロパティ名を示す文字列。
ppbOutputBYTE**out取得したプロパティ値を格納したバッファへのポインタを受け取る出力先。
pcbOutputDWORD*out出力されたプロパティ値のバイト数を受け取る出力先。
dwFlagsDWORDin動作を制御するフラグ。

戻り値の型: HRESULT

各言語での呼び出し定義

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

HRESULT SslGetKeyProperty(
    NCRYPT_KEY_HANDLE hKey,
    LPCWSTR pszProperty,
    BYTE** ppbOutput,
    DWORD* pcbOutput,
    DWORD dwFlags
);
[DllImport("ncrypt.dll", ExactSpelling = true)]
static extern int SslGetKeyProperty(
    UIntPtr hKey,   // NCRYPT_KEY_HANDLE
    [MarshalAs(UnmanagedType.LPWStr)] string pszProperty,   // LPCWSTR
    IntPtr ppbOutput,   // BYTE** out
    out uint pcbOutput,   // DWORD* out
    uint dwFlags   // DWORD
);
<DllImport("ncrypt.dll", ExactSpelling:=True)>
Public Shared Function SslGetKeyProperty(
    hKey As UIntPtr,   ' NCRYPT_KEY_HANDLE
    <MarshalAs(UnmanagedType.LPWStr)> pszProperty As String,   ' LPCWSTR
    ppbOutput As IntPtr,   ' BYTE** out
    <Out> ByRef pcbOutput As UInteger,   ' DWORD* out
    dwFlags As UInteger   ' DWORD
) As Integer
End Function
' hKey : NCRYPT_KEY_HANDLE
' pszProperty : LPCWSTR
' ppbOutput : BYTE** out
' pcbOutput : DWORD* out
' dwFlags : DWORD
Declare PtrSafe Function SslGetKeyProperty Lib "ncrypt" ( _
    ByVal hKey As LongPtr, _
    ByVal pszProperty As LongPtr, _
    ByVal ppbOutput As LongPtr, _
    ByRef pcbOutput As Long, _
    ByVal dwFlags As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

SslGetKeyProperty = ctypes.windll.ncrypt.SslGetKeyProperty
SslGetKeyProperty.restype = ctypes.c_int
SslGetKeyProperty.argtypes = [
    ctypes.c_size_t,  # hKey : NCRYPT_KEY_HANDLE
    wintypes.LPCWSTR,  # pszProperty : LPCWSTR
    ctypes.c_void_p,  # ppbOutput : BYTE** out
    ctypes.POINTER(wintypes.DWORD),  # pcbOutput : DWORD* out
    wintypes.DWORD,  # dwFlags : DWORD
]
require 'fiddle'
require 'fiddle/import'

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

var (
	ncrypt = windows.NewLazySystemDLL("ncrypt.dll")
	procSslGetKeyProperty = ncrypt.NewProc("SslGetKeyProperty")
)

// hKey (NCRYPT_KEY_HANDLE), pszProperty (LPCWSTR), ppbOutput (BYTE** out), pcbOutput (DWORD* out), dwFlags (DWORD)
r1, _, err := procSslGetKeyProperty.Call(
	uintptr(hKey),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pszProperty))),
	uintptr(ppbOutput),
	uintptr(pcbOutput),
	uintptr(dwFlags),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // HRESULT
function SslGetKeyProperty(
  hKey: NativeUInt;   // NCRYPT_KEY_HANDLE
  pszProperty: PWideChar;   // LPCWSTR
  ppbOutput: Pointer;   // BYTE** out
  pcbOutput: Pointer;   // DWORD* out
  dwFlags: DWORD   // DWORD
): Integer; stdcall;
  external 'ncrypt.dll' name 'SslGetKeyProperty';
result := DllCall("ncrypt\SslGetKeyProperty"
    , "UPtr", hKey   ; NCRYPT_KEY_HANDLE
    , "WStr", pszProperty   ; LPCWSTR
    , "Ptr", ppbOutput   ; BYTE** out
    , "Ptr", pcbOutput   ; DWORD* out
    , "UInt", dwFlags   ; DWORD
    , "Int")   ; return: HRESULT
●SslGetKeyProperty(hKey, pszProperty, ppbOutput, pcbOutput, dwFlags) = DLL("ncrypt.dll", "int SslGetKeyProperty(int, char*, void*, void*, dword)")
# 呼び出し: SslGetKeyProperty(hKey, pszProperty, ppbOutput, pcbOutput, dwFlags)
# hKey : NCRYPT_KEY_HANDLE -> "int"
# pszProperty : LPCWSTR -> "char*"
# ppbOutput : BYTE** out -> "void*"
# pcbOutput : DWORD* out -> "void*"
# dwFlags : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "ncrypt" fn SslGetKeyProperty(
    hKey: usize, // NCRYPT_KEY_HANDLE
    pszProperty: [*c]const u16, // LPCWSTR
    ppbOutput: [*c][*c]u8, // BYTE** out
    pcbOutput: [*c]u32, // DWORD* out
    dwFlags: u32 // DWORD
) callconv(std.os.windows.WINAPI) i32;
proc SslGetKeyProperty(
    hKey: uint,  # NCRYPT_KEY_HANDLE
    pszProperty: WideCString,  # LPCWSTR
    ppbOutput: ptr uint8,  # BYTE** out
    pcbOutput: ptr uint32,  # DWORD* out
    dwFlags: uint32  # DWORD
): int32 {.importc: "SslGetKeyProperty", stdcall, dynlib: "ncrypt.dll".}
pragma(lib, "ncrypt");
extern(Windows)
int SslGetKeyProperty(
    size_t hKey,   // NCRYPT_KEY_HANDLE
    const(wchar)* pszProperty,   // LPCWSTR
    ubyte** ppbOutput,   // BYTE** out
    uint* pcbOutput,   // DWORD* out
    uint dwFlags   // DWORD
);
ccall((:SslGetKeyProperty, "ncrypt.dll"), stdcall, Int32,
      (Csize_t, Cwstring, Ptr{UInt8}, Ptr{UInt32}, UInt32),
      hKey, pszProperty, ppbOutput, pcbOutput, dwFlags)
# hKey : NCRYPT_KEY_HANDLE -> Csize_t
# pszProperty : LPCWSTR -> Cwstring
# ppbOutput : BYTE** out -> Ptr{UInt8}
# pcbOutput : DWORD* out -> Ptr{UInt32}
# dwFlags : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t SslGetKeyProperty(
    uintptr_t hKey,
    const uint16_t* pszProperty,
    uint8_t** ppbOutput,
    uint32_t* pcbOutput,
    uint32_t dwFlags);
]]
local ncrypt = ffi.load("ncrypt")
-- ncrypt.SslGetKeyProperty(hKey, pszProperty, ppbOutput, pcbOutput, dwFlags)
-- hKey : NCRYPT_KEY_HANDLE
-- pszProperty : LPCWSTR
-- ppbOutput : BYTE** out
-- pcbOutput : DWORD* out
-- dwFlags : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('ncrypt.dll');
const SslGetKeyProperty = lib.func('__stdcall', 'SslGetKeyProperty', 'int32_t', ['uintptr_t', 'str16', 'uint8_t *', 'uint32_t *', 'uint32_t']);
// SslGetKeyProperty(hKey, pszProperty, ppbOutput, pcbOutput, dwFlags)
// hKey : NCRYPT_KEY_HANDLE -> 'uintptr_t'
// pszProperty : LPCWSTR -> 'str16'
// ppbOutput : BYTE** out -> 'uint8_t *'
// pcbOutput : DWORD* out -> 'uint32_t *'
// dwFlags : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("ncrypt.dll", {
  SslGetKeyProperty: { parameters: ["usize", "buffer", "pointer", "pointer", "u32"], result: "i32" },
});
// lib.symbols.SslGetKeyProperty(hKey, pszProperty, ppbOutput, pcbOutput, dwFlags)
// hKey : NCRYPT_KEY_HANDLE -> "usize"
// pszProperty : LPCWSTR -> "buffer"
// ppbOutput : BYTE** out -> "pointer"
// pcbOutput : DWORD* out -> "pointer"
// dwFlags : DWORD -> "u32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t SslGetKeyProperty(
    size_t hKey,
    const uint16_t* pszProperty,
    uint8_t** ppbOutput,
    uint32_t* pcbOutput,
    uint32_t dwFlags);
C, "ncrypt.dll");
// $ffi->SslGetKeyProperty(hKey, pszProperty, ppbOutput, pcbOutput, dwFlags);
// hKey : NCRYPT_KEY_HANDLE
// pszProperty : LPCWSTR
// ppbOutput : BYTE** out
// pcbOutput : DWORD* 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 SslGetKeyProperty(
        long hKey,   // NCRYPT_KEY_HANDLE
        WString pszProperty,   // LPCWSTR
        Pointer ppbOutput,   // BYTE** out
        IntByReference pcbOutput,   // DWORD* out
        int dwFlags   // DWORD
    );
}
@[Link("ncrypt")]
lib Libncrypt
  fun SslGetKeyProperty = SslGetKeyProperty(
    hKey : LibC::SizeT,   # NCRYPT_KEY_HANDLE
    pszProperty : UInt16*,   # LPCWSTR
    ppbOutput : UInt8**,   # BYTE** out
    pcbOutput : UInt32*,   # DWORD* 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 SslGetKeyPropertyNative = Int32 Function(UintPtr, Pointer<Utf16>, Pointer<Uint8>, Pointer<Uint32>, Uint32);
typedef SslGetKeyPropertyDart = int Function(int, Pointer<Utf16>, Pointer<Uint8>, Pointer<Uint32>, int);
final SslGetKeyProperty = DynamicLibrary.open('ncrypt.dll')
    .lookupFunction<SslGetKeyPropertyNative, SslGetKeyPropertyDart>('SslGetKeyProperty');
// hKey : NCRYPT_KEY_HANDLE -> UintPtr
// pszProperty : LPCWSTR -> Pointer<Utf16>
// ppbOutput : BYTE** out -> Pointer<Uint8>
// pcbOutput : DWORD* out -> Pointer<Uint32>
// dwFlags : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function SslGetKeyProperty(
  hKey: NativeUInt;   // NCRYPT_KEY_HANDLE
  pszProperty: PWideChar;   // LPCWSTR
  ppbOutput: Pointer;   // BYTE** out
  pcbOutput: Pointer;   // DWORD* out
  dwFlags: DWORD   // DWORD
): Integer; stdcall;
  external 'ncrypt.dll' name 'SslGetKeyProperty';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "SslGetKeyProperty"
  c_SslGetKeyProperty :: CUIntPtr -> CWString -> Ptr Word8 -> Ptr Word32 -> Word32 -> IO Int32
-- hKey : NCRYPT_KEY_HANDLE -> CUIntPtr
-- pszProperty : LPCWSTR -> CWString
-- ppbOutput : BYTE** out -> Ptr Word8
-- pcbOutput : DWORD* out -> Ptr Word32
-- dwFlags : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let sslgetkeyproperty =
  foreign "SslGetKeyProperty"
    (size_t @-> (ptr uint16_t) @-> (ptr uint8_t) @-> (ptr uint32_t) @-> uint32_t @-> returning int32_t)
(* hKey : NCRYPT_KEY_HANDLE -> size_t *)
(* pszProperty : LPCWSTR -> (ptr uint16_t) *)
(* ppbOutput : BYTE** out -> (ptr uint8_t) *)
(* pcbOutput : DWORD* out -> (ptr uint32_t) *)
(* 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 ("SslGetKeyProperty" ssl-get-key-property :convention :stdcall) :int32
  (h-key :uint64)   ; NCRYPT_KEY_HANDLE
  (psz-property (:string :encoding :utf-16le))   ; LPCWSTR
  (ppb-output :pointer)   ; BYTE** out
  (pcb-output :pointer)   ; DWORD* out
  (dw-flags :uint32))   ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $SslGetKeyProperty = Win32::API::More->new('ncrypt',
    'int SslGetKeyProperty(WPARAM hKey, LPCWSTR pszProperty, LPVOID ppbOutput, LPVOID pcbOutput, DWORD dwFlags)');
# my $ret = $SslGetKeyProperty->Call($hKey, $pszProperty, $ppbOutput, $pcbOutput, $dwFlags);
# hKey : NCRYPT_KEY_HANDLE -> WPARAM
# pszProperty : LPCWSTR -> LPCWSTR
# ppbOutput : BYTE** out -> LPVOID
# pcbOutput : DWORD* out -> LPVOID
# dwFlags : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。