ホーム › Security.Cryptography › SslExportKeyingMaterial
SslExportKeyingMaterial
関数SSLマスター鍵から鍵生成材料をエクスポートする。
シグネチャ
// ncrypt.dll
#include <windows.h>
HRESULT SslExportKeyingMaterial(
NCRYPT_PROV_HANDLE hSslProvider,
NCRYPT_KEY_HANDLE hMasterKey,
LPSTR sLabel,
BYTE* pbRandoms, // optional
DWORD cbRandoms,
BYTE* pbContextValue, // optional
WORD cbContextValue,
BYTE* pbOutput,
DWORD cbOutput,
DWORD dwFlags
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| hSslProvider | NCRYPT_PROV_HANDLE | in | Schannel SSLプロバイダのハンドル。 |
| hMasterKey | NCRYPT_KEY_HANDLE | in | 鍵素材を導出する元のマスター鍵ハンドル。 |
| sLabel | LPSTR | in | RFC5705に基づくエクスポート用ラベル文字列(ANSI)。 |
| pbRandoms | BYTE* | inoptional | クライアント/サーバランダム値を連結したデータへのポインタ。 |
| cbRandoms | DWORD | in | pbRandomsのバイト数。 |
| pbContextValue | BYTE* | inoptional | オプションのコンテキスト値データへのポインタ。NULL可。 |
| cbContextValue | WORD | in | pbContextValueのバイト数(WORD)。 |
| pbOutput | BYTE* | out | 導出した鍵素材を書き込む出力バッファ。 |
| cbOutput | DWORD | in | pbOutputのバイト数。要求する鍵素材長を表す。 |
| dwFlags | DWORD | in | 動作を制御するフラグ。 |
戻り値の型: HRESULT
各言語での呼び出し定義
// ncrypt.dll
#include <windows.h>
HRESULT SslExportKeyingMaterial(
NCRYPT_PROV_HANDLE hSslProvider,
NCRYPT_KEY_HANDLE hMasterKey,
LPSTR sLabel,
BYTE* pbRandoms, // optional
DWORD cbRandoms,
BYTE* pbContextValue, // optional
WORD cbContextValue,
BYTE* pbOutput,
DWORD cbOutput,
DWORD dwFlags
);[DllImport("ncrypt.dll", ExactSpelling = true)]
static extern int SslExportKeyingMaterial(
UIntPtr hSslProvider, // NCRYPT_PROV_HANDLE
UIntPtr hMasterKey, // NCRYPT_KEY_HANDLE
[MarshalAs(UnmanagedType.LPStr)] string sLabel, // LPSTR
IntPtr pbRandoms, // BYTE* optional
uint cbRandoms, // DWORD
IntPtr pbContextValue, // BYTE* optional
ushort cbContextValue, // WORD
IntPtr pbOutput, // BYTE* out
uint cbOutput, // DWORD
uint dwFlags // DWORD
);<DllImport("ncrypt.dll", ExactSpelling:=True)>
Public Shared Function SslExportKeyingMaterial(
hSslProvider As UIntPtr, ' NCRYPT_PROV_HANDLE
hMasterKey As UIntPtr, ' NCRYPT_KEY_HANDLE
<MarshalAs(UnmanagedType.LPStr)> sLabel As String, ' LPSTR
pbRandoms As IntPtr, ' BYTE* optional
cbRandoms As UInteger, ' DWORD
pbContextValue As IntPtr, ' BYTE* optional
cbContextValue As UShort, ' WORD
pbOutput As IntPtr, ' BYTE* out
cbOutput As UInteger, ' DWORD
dwFlags As UInteger ' DWORD
) As Integer
End Function' hSslProvider : NCRYPT_PROV_HANDLE
' hMasterKey : NCRYPT_KEY_HANDLE
' sLabel : LPSTR
' pbRandoms : BYTE* optional
' cbRandoms : DWORD
' pbContextValue : BYTE* optional
' cbContextValue : WORD
' pbOutput : BYTE* out
' cbOutput : DWORD
' dwFlags : DWORD
Declare PtrSafe Function SslExportKeyingMaterial Lib "ncrypt" ( _
ByVal hSslProvider As LongPtr, _
ByVal hMasterKey As LongPtr, _
ByVal sLabel As String, _
ByVal pbRandoms As LongPtr, _
ByVal cbRandoms As Long, _
ByVal pbContextValue As LongPtr, _
ByVal cbContextValue As Integer, _
ByVal pbOutput As LongPtr, _
ByVal cbOutput 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
SslExportKeyingMaterial = ctypes.windll.ncrypt.SslExportKeyingMaterial
SslExportKeyingMaterial.restype = ctypes.c_int
SslExportKeyingMaterial.argtypes = [
ctypes.c_size_t, # hSslProvider : NCRYPT_PROV_HANDLE
ctypes.c_size_t, # hMasterKey : NCRYPT_KEY_HANDLE
wintypes.LPCSTR, # sLabel : LPSTR
ctypes.POINTER(ctypes.c_ubyte), # pbRandoms : BYTE* optional
wintypes.DWORD, # cbRandoms : DWORD
ctypes.POINTER(ctypes.c_ubyte), # pbContextValue : BYTE* optional
ctypes.c_ushort, # cbContextValue : WORD
ctypes.POINTER(ctypes.c_ubyte), # pbOutput : BYTE* out
wintypes.DWORD, # cbOutput : DWORD
wintypes.DWORD, # dwFlags : DWORD
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('ncrypt.dll')
SslExportKeyingMaterial = Fiddle::Function.new(
lib['SslExportKeyingMaterial'],
[
Fiddle::TYPE_UINTPTR_T, # hSslProvider : NCRYPT_PROV_HANDLE
Fiddle::TYPE_UINTPTR_T, # hMasterKey : NCRYPT_KEY_HANDLE
Fiddle::TYPE_VOIDP, # sLabel : LPSTR
Fiddle::TYPE_VOIDP, # pbRandoms : BYTE* optional
-Fiddle::TYPE_INT, # cbRandoms : DWORD
Fiddle::TYPE_VOIDP, # pbContextValue : BYTE* optional
-Fiddle::TYPE_SHORT, # cbContextValue : WORD
Fiddle::TYPE_VOIDP, # pbOutput : BYTE* out
-Fiddle::TYPE_INT, # cbOutput : DWORD
-Fiddle::TYPE_INT, # dwFlags : DWORD
],
Fiddle::TYPE_INT)#[link(name = "ncrypt")]
extern "system" {
fn SslExportKeyingMaterial(
hSslProvider: usize, // NCRYPT_PROV_HANDLE
hMasterKey: usize, // NCRYPT_KEY_HANDLE
sLabel: *mut u8, // LPSTR
pbRandoms: *mut u8, // BYTE* optional
cbRandoms: u32, // DWORD
pbContextValue: *mut u8, // BYTE* optional
cbContextValue: u16, // WORD
pbOutput: *mut u8, // BYTE* out
cbOutput: u32, // DWORD
dwFlags: u32 // DWORD
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("ncrypt.dll")]
public static extern int SslExportKeyingMaterial(UIntPtr hSslProvider, UIntPtr hMasterKey, [MarshalAs(UnmanagedType.LPStr)] string sLabel, IntPtr pbRandoms, uint cbRandoms, IntPtr pbContextValue, ushort cbContextValue, IntPtr pbOutput, uint cbOutput, uint dwFlags);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ncrypt_SslExportKeyingMaterial' -Namespace Win32 -PassThru
# $api::SslExportKeyingMaterial(hSslProvider, hMasterKey, sLabel, pbRandoms, cbRandoms, pbContextValue, cbContextValue, pbOutput, cbOutput, dwFlags)#uselib "ncrypt.dll"
#func global SslExportKeyingMaterial "SslExportKeyingMaterial" sptr, sptr, sptr, sptr, sptr, sptr, sptr, sptr, sptr, sptr
; SslExportKeyingMaterial hSslProvider, hMasterKey, sLabel, varptr(pbRandoms), cbRandoms, varptr(pbContextValue), cbContextValue, varptr(pbOutput), cbOutput, dwFlags ; 戻り値は stat
; hSslProvider : NCRYPT_PROV_HANDLE -> "sptr"
; hMasterKey : NCRYPT_KEY_HANDLE -> "sptr"
; sLabel : LPSTR -> "sptr"
; pbRandoms : BYTE* optional -> "sptr"
; cbRandoms : DWORD -> "sptr"
; pbContextValue : BYTE* optional -> "sptr"
; cbContextValue : WORD -> "sptr"
; pbOutput : BYTE* out -> "sptr"
; cbOutput : DWORD -> "sptr"
; dwFlags : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "ncrypt.dll" #cfunc global SslExportKeyingMaterial "SslExportKeyingMaterial" sptr, sptr, str, var, int, var, int, var, int, int ; res = SslExportKeyingMaterial(hSslProvider, hMasterKey, sLabel, pbRandoms, cbRandoms, pbContextValue, cbContextValue, pbOutput, cbOutput, dwFlags) ; hSslProvider : NCRYPT_PROV_HANDLE -> "sptr" ; hMasterKey : NCRYPT_KEY_HANDLE -> "sptr" ; sLabel : LPSTR -> "str" ; pbRandoms : BYTE* optional -> "var" ; cbRandoms : DWORD -> "int" ; pbContextValue : BYTE* optional -> "var" ; cbContextValue : WORD -> "int" ; pbOutput : BYTE* out -> "var" ; cbOutput : DWORD -> "int" ; dwFlags : DWORD -> "int" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "ncrypt.dll" #cfunc global SslExportKeyingMaterial "SslExportKeyingMaterial" sptr, sptr, str, sptr, int, sptr, int, sptr, int, int ; res = SslExportKeyingMaterial(hSslProvider, hMasterKey, sLabel, varptr(pbRandoms), cbRandoms, varptr(pbContextValue), cbContextValue, varptr(pbOutput), cbOutput, dwFlags) ; hSslProvider : NCRYPT_PROV_HANDLE -> "sptr" ; hMasterKey : NCRYPT_KEY_HANDLE -> "sptr" ; sLabel : LPSTR -> "str" ; pbRandoms : BYTE* optional -> "sptr" ; cbRandoms : DWORD -> "int" ; pbContextValue : BYTE* optional -> "sptr" ; cbContextValue : WORD -> "int" ; pbOutput : BYTE* out -> "sptr" ; cbOutput : DWORD -> "int" ; dwFlags : DWORD -> "int" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; HRESULT SslExportKeyingMaterial(NCRYPT_PROV_HANDLE hSslProvider, NCRYPT_KEY_HANDLE hMasterKey, LPSTR sLabel, BYTE* pbRandoms, DWORD cbRandoms, BYTE* pbContextValue, WORD cbContextValue, BYTE* pbOutput, DWORD cbOutput, DWORD dwFlags) #uselib "ncrypt.dll" #cfunc global SslExportKeyingMaterial "SslExportKeyingMaterial" intptr, intptr, str, var, int, var, int, var, int, int ; res = SslExportKeyingMaterial(hSslProvider, hMasterKey, sLabel, pbRandoms, cbRandoms, pbContextValue, cbContextValue, pbOutput, cbOutput, dwFlags) ; hSslProvider : NCRYPT_PROV_HANDLE -> "intptr" ; hMasterKey : NCRYPT_KEY_HANDLE -> "intptr" ; sLabel : LPSTR -> "str" ; pbRandoms : BYTE* optional -> "var" ; cbRandoms : DWORD -> "int" ; pbContextValue : BYTE* optional -> "var" ; cbContextValue : WORD -> "int" ; pbOutput : BYTE* out -> "var" ; cbOutput : DWORD -> "int" ; dwFlags : DWORD -> "int" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; HRESULT SslExportKeyingMaterial(NCRYPT_PROV_HANDLE hSslProvider, NCRYPT_KEY_HANDLE hMasterKey, LPSTR sLabel, BYTE* pbRandoms, DWORD cbRandoms, BYTE* pbContextValue, WORD cbContextValue, BYTE* pbOutput, DWORD cbOutput, DWORD dwFlags) #uselib "ncrypt.dll" #cfunc global SslExportKeyingMaterial "SslExportKeyingMaterial" intptr, intptr, str, intptr, int, intptr, int, intptr, int, int ; res = SslExportKeyingMaterial(hSslProvider, hMasterKey, sLabel, varptr(pbRandoms), cbRandoms, varptr(pbContextValue), cbContextValue, varptr(pbOutput), cbOutput, dwFlags) ; hSslProvider : NCRYPT_PROV_HANDLE -> "intptr" ; hMasterKey : NCRYPT_KEY_HANDLE -> "intptr" ; sLabel : LPSTR -> "str" ; pbRandoms : BYTE* optional -> "intptr" ; cbRandoms : DWORD -> "int" ; pbContextValue : BYTE* optional -> "intptr" ; cbContextValue : WORD -> "int" ; pbOutput : BYTE* out -> "intptr" ; cbOutput : DWORD -> "int" ; dwFlags : DWORD -> "int" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
ncrypt = windows.NewLazySystemDLL("ncrypt.dll")
procSslExportKeyingMaterial = ncrypt.NewProc("SslExportKeyingMaterial")
)
// hSslProvider (NCRYPT_PROV_HANDLE), hMasterKey (NCRYPT_KEY_HANDLE), sLabel (LPSTR), pbRandoms (BYTE* optional), cbRandoms (DWORD), pbContextValue (BYTE* optional), cbContextValue (WORD), pbOutput (BYTE* out), cbOutput (DWORD), dwFlags (DWORD)
r1, _, err := procSslExportKeyingMaterial.Call(
uintptr(hSslProvider),
uintptr(hMasterKey),
uintptr(unsafe.Pointer(windows.BytePtrFromString(sLabel))),
uintptr(pbRandoms),
uintptr(cbRandoms),
uintptr(pbContextValue),
uintptr(cbContextValue),
uintptr(pbOutput),
uintptr(cbOutput),
uintptr(dwFlags),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // HRESULTfunction SslExportKeyingMaterial(
hSslProvider: NativeUInt; // NCRYPT_PROV_HANDLE
hMasterKey: NativeUInt; // NCRYPT_KEY_HANDLE
sLabel: PAnsiChar; // LPSTR
pbRandoms: Pointer; // BYTE* optional
cbRandoms: DWORD; // DWORD
pbContextValue: Pointer; // BYTE* optional
cbContextValue: Word; // WORD
pbOutput: Pointer; // BYTE* out
cbOutput: DWORD; // DWORD
dwFlags: DWORD // DWORD
): Integer; stdcall;
external 'ncrypt.dll' name 'SslExportKeyingMaterial';result := DllCall("ncrypt\SslExportKeyingMaterial"
, "UPtr", hSslProvider ; NCRYPT_PROV_HANDLE
, "UPtr", hMasterKey ; NCRYPT_KEY_HANDLE
, "AStr", sLabel ; LPSTR
, "Ptr", pbRandoms ; BYTE* optional
, "UInt", cbRandoms ; DWORD
, "Ptr", pbContextValue ; BYTE* optional
, "UShort", cbContextValue ; WORD
, "Ptr", pbOutput ; BYTE* out
, "UInt", cbOutput ; DWORD
, "UInt", dwFlags ; DWORD
, "Int") ; return: HRESULT●SslExportKeyingMaterial(hSslProvider, hMasterKey, sLabel, pbRandoms, cbRandoms, pbContextValue, cbContextValue, pbOutput, cbOutput, dwFlags) = DLL("ncrypt.dll", "int SslExportKeyingMaterial(int, int, char*, void*, dword, void*, int, void*, dword, dword)")
# 呼び出し: SslExportKeyingMaterial(hSslProvider, hMasterKey, sLabel, pbRandoms, cbRandoms, pbContextValue, cbContextValue, pbOutput, cbOutput, dwFlags)
# hSslProvider : NCRYPT_PROV_HANDLE -> "int"
# hMasterKey : NCRYPT_KEY_HANDLE -> "int"
# sLabel : LPSTR -> "char*"
# pbRandoms : BYTE* optional -> "void*"
# cbRandoms : DWORD -> "dword"
# pbContextValue : BYTE* optional -> "void*"
# cbContextValue : WORD -> "int"
# pbOutput : BYTE* out -> "void*"
# cbOutput : DWORD -> "dword"
# dwFlags : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "ncrypt" fn SslExportKeyingMaterial(
hSslProvider: usize, // NCRYPT_PROV_HANDLE
hMasterKey: usize, // NCRYPT_KEY_HANDLE
sLabel: [*c]const u8, // LPSTR
pbRandoms: [*c]u8, // BYTE* optional
cbRandoms: u32, // DWORD
pbContextValue: [*c]u8, // BYTE* optional
cbContextValue: u16, // WORD
pbOutput: [*c]u8, // BYTE* out
cbOutput: u32, // DWORD
dwFlags: u32 // DWORD
) callconv(std.os.windows.WINAPI) i32;proc SslExportKeyingMaterial(
hSslProvider: uint, # NCRYPT_PROV_HANDLE
hMasterKey: uint, # NCRYPT_KEY_HANDLE
sLabel: cstring, # LPSTR
pbRandoms: ptr uint8, # BYTE* optional
cbRandoms: uint32, # DWORD
pbContextValue: ptr uint8, # BYTE* optional
cbContextValue: uint16, # WORD
pbOutput: ptr uint8, # BYTE* out
cbOutput: uint32, # DWORD
dwFlags: uint32 # DWORD
): int32 {.importc: "SslExportKeyingMaterial", stdcall, dynlib: "ncrypt.dll".}pragma(lib, "ncrypt");
extern(Windows)
int SslExportKeyingMaterial(
size_t hSslProvider, // NCRYPT_PROV_HANDLE
size_t hMasterKey, // NCRYPT_KEY_HANDLE
const(char)* sLabel, // LPSTR
ubyte* pbRandoms, // BYTE* optional
uint cbRandoms, // DWORD
ubyte* pbContextValue, // BYTE* optional
ushort cbContextValue, // WORD
ubyte* pbOutput, // BYTE* out
uint cbOutput, // DWORD
uint dwFlags // DWORD
);ccall((:SslExportKeyingMaterial, "ncrypt.dll"), stdcall, Int32,
(Csize_t, Csize_t, Cstring, Ptr{UInt8}, UInt32, Ptr{UInt8}, UInt16, Ptr{UInt8}, UInt32, UInt32),
hSslProvider, hMasterKey, sLabel, pbRandoms, cbRandoms, pbContextValue, cbContextValue, pbOutput, cbOutput, dwFlags)
# hSslProvider : NCRYPT_PROV_HANDLE -> Csize_t
# hMasterKey : NCRYPT_KEY_HANDLE -> Csize_t
# sLabel : LPSTR -> Cstring
# pbRandoms : BYTE* optional -> Ptr{UInt8}
# cbRandoms : DWORD -> UInt32
# pbContextValue : BYTE* optional -> Ptr{UInt8}
# cbContextValue : WORD -> UInt16
# pbOutput : BYTE* out -> Ptr{UInt8}
# cbOutput : DWORD -> UInt32
# dwFlags : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t SslExportKeyingMaterial(
uintptr_t hSslProvider,
uintptr_t hMasterKey,
const char* sLabel,
uint8_t* pbRandoms,
uint32_t cbRandoms,
uint8_t* pbContextValue,
uint16_t cbContextValue,
uint8_t* pbOutput,
uint32_t cbOutput,
uint32_t dwFlags);
]]
local ncrypt = ffi.load("ncrypt")
-- ncrypt.SslExportKeyingMaterial(hSslProvider, hMasterKey, sLabel, pbRandoms, cbRandoms, pbContextValue, cbContextValue, pbOutput, cbOutput, dwFlags)
-- hSslProvider : NCRYPT_PROV_HANDLE
-- hMasterKey : NCRYPT_KEY_HANDLE
-- sLabel : LPSTR
-- pbRandoms : BYTE* optional
-- cbRandoms : DWORD
-- pbContextValue : BYTE* optional
-- cbContextValue : WORD
-- pbOutput : BYTE* out
-- cbOutput : DWORD
-- dwFlags : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('ncrypt.dll');
const SslExportKeyingMaterial = lib.func('__stdcall', 'SslExportKeyingMaterial', 'int32_t', ['uintptr_t', 'uintptr_t', 'str', 'uint8_t *', 'uint32_t', 'uint8_t *', 'uint16_t', 'uint8_t *', 'uint32_t', 'uint32_t']);
// SslExportKeyingMaterial(hSslProvider, hMasterKey, sLabel, pbRandoms, cbRandoms, pbContextValue, cbContextValue, pbOutput, cbOutput, dwFlags)
// hSslProvider : NCRYPT_PROV_HANDLE -> 'uintptr_t'
// hMasterKey : NCRYPT_KEY_HANDLE -> 'uintptr_t'
// sLabel : LPSTR -> 'str'
// pbRandoms : BYTE* optional -> 'uint8_t *'
// cbRandoms : DWORD -> 'uint32_t'
// pbContextValue : BYTE* optional -> 'uint8_t *'
// cbContextValue : WORD -> 'uint16_t'
// pbOutput : BYTE* out -> 'uint8_t *'
// cbOutput : DWORD -> 'uint32_t'
// dwFlags : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("ncrypt.dll", {
SslExportKeyingMaterial: { parameters: ["usize", "usize", "buffer", "pointer", "u32", "pointer", "u16", "pointer", "u32", "u32"], result: "i32" },
});
// lib.symbols.SslExportKeyingMaterial(hSslProvider, hMasterKey, sLabel, pbRandoms, cbRandoms, pbContextValue, cbContextValue, pbOutput, cbOutput, dwFlags)
// hSslProvider : NCRYPT_PROV_HANDLE -> "usize"
// hMasterKey : NCRYPT_KEY_HANDLE -> "usize"
// sLabel : LPSTR -> "buffer"
// pbRandoms : BYTE* optional -> "pointer"
// cbRandoms : DWORD -> "u32"
// pbContextValue : BYTE* optional -> "pointer"
// cbContextValue : WORD -> "u16"
// pbOutput : BYTE* out -> "pointer"
// cbOutput : DWORD -> "u32"
// dwFlags : DWORD -> "u32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t SslExportKeyingMaterial(
size_t hSslProvider,
size_t hMasterKey,
const char* sLabel,
uint8_t* pbRandoms,
uint32_t cbRandoms,
uint8_t* pbContextValue,
uint16_t cbContextValue,
uint8_t* pbOutput,
uint32_t cbOutput,
uint32_t dwFlags);
C, "ncrypt.dll");
// $ffi->SslExportKeyingMaterial(hSslProvider, hMasterKey, sLabel, pbRandoms, cbRandoms, pbContextValue, cbContextValue, pbOutput, cbOutput, dwFlags);
// hSslProvider : NCRYPT_PROV_HANDLE
// hMasterKey : NCRYPT_KEY_HANDLE
// sLabel : LPSTR
// pbRandoms : BYTE* optional
// cbRandoms : DWORD
// pbContextValue : BYTE* optional
// cbContextValue : WORD
// pbOutput : BYTE* out
// cbOutput : DWORD
// 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 SslExportKeyingMaterial(
long hSslProvider, // NCRYPT_PROV_HANDLE
long hMasterKey, // NCRYPT_KEY_HANDLE
String sLabel, // LPSTR
byte[] pbRandoms, // BYTE* optional
int cbRandoms, // DWORD
byte[] pbContextValue, // BYTE* optional
short cbContextValue, // WORD
byte[] pbOutput, // BYTE* out
int cbOutput, // DWORD
int dwFlags // DWORD
);
}@[Link("ncrypt")]
lib Libncrypt
fun SslExportKeyingMaterial = SslExportKeyingMaterial(
hSslProvider : LibC::SizeT, # NCRYPT_PROV_HANDLE
hMasterKey : LibC::SizeT, # NCRYPT_KEY_HANDLE
sLabel : UInt8*, # LPSTR
pbRandoms : UInt8*, # BYTE* optional
cbRandoms : UInt32, # DWORD
pbContextValue : UInt8*, # BYTE* optional
cbContextValue : UInt16, # WORD
pbOutput : UInt8*, # BYTE* out
cbOutput : UInt32, # DWORD
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 SslExportKeyingMaterialNative = Int32 Function(UintPtr, UintPtr, Pointer<Utf8>, Pointer<Uint8>, Uint32, Pointer<Uint8>, Uint16, Pointer<Uint8>, Uint32, Uint32);
typedef SslExportKeyingMaterialDart = int Function(int, int, Pointer<Utf8>, Pointer<Uint8>, int, Pointer<Uint8>, int, Pointer<Uint8>, int, int);
final SslExportKeyingMaterial = DynamicLibrary.open('ncrypt.dll')
.lookupFunction<SslExportKeyingMaterialNative, SslExportKeyingMaterialDart>('SslExportKeyingMaterial');
// hSslProvider : NCRYPT_PROV_HANDLE -> UintPtr
// hMasterKey : NCRYPT_KEY_HANDLE -> UintPtr
// sLabel : LPSTR -> Pointer<Utf8>
// pbRandoms : BYTE* optional -> Pointer<Uint8>
// cbRandoms : DWORD -> Uint32
// pbContextValue : BYTE* optional -> Pointer<Uint8>
// cbContextValue : WORD -> Uint16
// pbOutput : BYTE* out -> Pointer<Uint8>
// cbOutput : DWORD -> Uint32
// dwFlags : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function SslExportKeyingMaterial(
hSslProvider: NativeUInt; // NCRYPT_PROV_HANDLE
hMasterKey: NativeUInt; // NCRYPT_KEY_HANDLE
sLabel: PAnsiChar; // LPSTR
pbRandoms: Pointer; // BYTE* optional
cbRandoms: DWORD; // DWORD
pbContextValue: Pointer; // BYTE* optional
cbContextValue: Word; // WORD
pbOutput: Pointer; // BYTE* out
cbOutput: DWORD; // DWORD
dwFlags: DWORD // DWORD
): Integer; stdcall;
external 'ncrypt.dll' name 'SslExportKeyingMaterial';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "SslExportKeyingMaterial"
c_SslExportKeyingMaterial :: CUIntPtr -> CUIntPtr -> CString -> Ptr Word8 -> Word32 -> Ptr Word8 -> Word16 -> Ptr Word8 -> Word32 -> Word32 -> IO Int32
-- hSslProvider : NCRYPT_PROV_HANDLE -> CUIntPtr
-- hMasterKey : NCRYPT_KEY_HANDLE -> CUIntPtr
-- sLabel : LPSTR -> CString
-- pbRandoms : BYTE* optional -> Ptr Word8
-- cbRandoms : DWORD -> Word32
-- pbContextValue : BYTE* optional -> Ptr Word8
-- cbContextValue : WORD -> Word16
-- pbOutput : BYTE* out -> Ptr Word8
-- cbOutput : DWORD -> Word32
-- dwFlags : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let sslexportkeyingmaterial =
foreign "SslExportKeyingMaterial"
(size_t @-> size_t @-> string @-> (ptr uint8_t) @-> uint32_t @-> (ptr uint8_t) @-> uint16_t @-> (ptr uint8_t) @-> uint32_t @-> uint32_t @-> returning int32_t)
(* hSslProvider : NCRYPT_PROV_HANDLE -> size_t *)
(* hMasterKey : NCRYPT_KEY_HANDLE -> size_t *)
(* sLabel : LPSTR -> string *)
(* pbRandoms : BYTE* optional -> (ptr uint8_t) *)
(* cbRandoms : DWORD -> uint32_t *)
(* pbContextValue : BYTE* optional -> (ptr uint8_t) *)
(* cbContextValue : WORD -> uint16_t *)
(* pbOutput : BYTE* out -> (ptr uint8_t) *)
(* cbOutput : DWORD -> 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 ("SslExportKeyingMaterial" ssl-export-keying-material :convention :stdcall) :int32
(h-ssl-provider :uint64) ; NCRYPT_PROV_HANDLE
(h-master-key :uint64) ; NCRYPT_KEY_HANDLE
(s-label :string) ; LPSTR
(pb-randoms :pointer) ; BYTE* optional
(cb-randoms :uint32) ; DWORD
(pb-context-value :pointer) ; BYTE* optional
(cb-context-value :uint16) ; WORD
(pb-output :pointer) ; BYTE* out
(cb-output :uint32) ; DWORD
(dw-flags :uint32)) ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $SslExportKeyingMaterial = Win32::API::More->new('ncrypt',
'int SslExportKeyingMaterial(WPARAM hSslProvider, WPARAM hMasterKey, LPCSTR sLabel, LPVOID pbRandoms, DWORD cbRandoms, LPVOID pbContextValue, WORD cbContextValue, LPVOID pbOutput, DWORD cbOutput, DWORD dwFlags)');
# my $ret = $SslExportKeyingMaterial->Call($hSslProvider, $hMasterKey, $sLabel, $pbRandoms, $cbRandoms, $pbContextValue, $cbContextValue, $pbOutput, $cbOutput, $dwFlags);
# hSslProvider : NCRYPT_PROV_HANDLE -> WPARAM
# hMasterKey : NCRYPT_KEY_HANDLE -> WPARAM
# sLabel : LPSTR -> LPCSTR
# pbRandoms : BYTE* optional -> LPVOID
# cbRandoms : DWORD -> DWORD
# pbContextValue : BYTE* optional -> LPVOID
# cbContextValue : WORD -> WORD
# pbOutput : BYTE* out -> LPVOID
# cbOutput : DWORD -> DWORD
# dwFlags : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。