ホーム › Security.Cryptography › CryptImportPublicKeyInfoEx
CryptImportPublicKeyInfoEx
関数鍵アルゴリズムを指定して公開鍵情報を拡張インポートする。
シグネチャ
// CRYPT32.dll
#include <windows.h>
BOOL CryptImportPublicKeyInfoEx(
UINT_PTR hCryptProv,
CERT_QUERY_ENCODING_TYPE dwCertEncodingType,
CERT_PUBLIC_KEY_INFO* pInfo,
ALG_ID aiKeyAlg,
DWORD dwFlags,
void* pvAuxInfo, // optional
UINT_PTR* phKey
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| hCryptProv | UINT_PTR | in | インポートされる公開キーを受け取る CSP のハンドル。このハンドルは、あらかじめ CryptAcquireContext を使用して作成しておく必要があります。 |
| dwCertEncodingType | CERT_QUERY_ENCODING_TYPE | in | 使用するエンコード タイプを指定します。次の例に示すように、証明書とメッセージ エンコード タイプの両方をビットごとの OR 演算で組み合わせて指定することは常に有効です。 X509_ASN_ENCODING | PKCS_7_ASN_ENCODING 現在定義されているエンコード タイプは次のとおりです。 |
| pInfo | CERT_PUBLIC_KEY_INFO* | in | プロバイダーにインポートする公開キーを格納する CERT_PUBLIC_KEY_INFO 構造体のアドレス。 メモ pInfo パラメーターが指す Algorithm メンバーの pzObjId メンバーと dwCertEncodingType パラメーターによって、インストール可能な CRYPT_OID_IMPORT_PUBLIC_KEY_INFO_FUNC コールバック関数が決定されます。インストール可能な関数が見つからない場合は、キーを RSA 公開キー (szOID_RSA_RSA) としてインポートしようとします。
|
| aiKeyAlg | ALG_ID | in | 既定のアルゴリズム CALG_RSA_KEYX をオーバーライドする CSP 固有のアルゴリズムを格納する ALG_ID 構造体。 |
| dwFlags | DWORD | in | 将来の使用のために予約されており、0 にする必要があります。 |
| pvAuxInfo | void* | inoptional | 将来の使用のために予約されており、NULL にする必要があります。 |
| phKey | UINT_PTR* | out | インポートされた公開キーのハンドルを受け取る HCRYPTKEY 変数のアドレス。公開キーの使用が終わったら、CryptDestroyKey 関数を呼び出してハンドルを解放してください。 |
戻り値の型: BOOL
公式ドキュメント
重要 この API は非推奨です。(CryptImportPublicKeyInfoEx)
戻り値
関数が成功すると、0 以外の値 (TRUE) を返します。
関数が失敗すると、0 (FALSE) を返します。拡張エラー情報を取得するには、 GetLastError を呼び出します。
メモ 呼び出される関数
CryptGetUserKey および
CryptExportKey からのエラーが、この関数に伝播されることがあります。この関数には次のエラー コードがあります。
| 値 | 説明 |
|---|---|
| 指定された dwCertEncodingType および pInfo パラメーターに対して、インストールまたは登録できるインポート関数が見つかりませんでした。 |
関数が失敗した場合、GetLastError が Abstract Syntax Notation One (ASN.1) のエンコード/デコード エラーを返すことがあります。これらのエラーについては、 ASN.1 Encoding/Decoding Return Values を参照してください。
解説(Remarks)
この関数は通常、証明書から公開キーを取得するために使用します。これは、次の擬似コードに示すように、入力済みの証明書構造体から CERT_PUBLIC_KEY_INFO 構造体を渡すことで行います。
PCCERT_CONTEXT pCertContext
// 証明書から証明書コンテキスト構造体を取得します。
pCertContext = CertCreateCertificateContext(...)
if(pCertContext)
{
HCRYPTKEY hCertPubKey
// 証明書の公開キー情報を取得します。
CryptImportPublicKeyInfo(
hCryptProv,
X509_ASN_ENCODING,
&pCertContext->pCertInfo->SubjectPublicKeyInfo,
&hCertPubKey)
CertFreeCertificateContext(pCertContext)
}
出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// CRYPT32.dll
#include <windows.h>
BOOL CryptImportPublicKeyInfoEx(
UINT_PTR hCryptProv,
CERT_QUERY_ENCODING_TYPE dwCertEncodingType,
CERT_PUBLIC_KEY_INFO* pInfo,
ALG_ID aiKeyAlg,
DWORD dwFlags,
void* pvAuxInfo, // optional
UINT_PTR* phKey
);[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("CRYPT32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool CryptImportPublicKeyInfoEx(
UIntPtr hCryptProv, // UINT_PTR
uint dwCertEncodingType, // CERT_QUERY_ENCODING_TYPE
IntPtr pInfo, // CERT_PUBLIC_KEY_INFO*
uint aiKeyAlg, // ALG_ID
uint dwFlags, // DWORD
IntPtr pvAuxInfo, // void* optional
out UIntPtr phKey // UINT_PTR* out
);<DllImport("CRYPT32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function CryptImportPublicKeyInfoEx(
hCryptProv As UIntPtr, ' UINT_PTR
dwCertEncodingType As UInteger, ' CERT_QUERY_ENCODING_TYPE
pInfo As IntPtr, ' CERT_PUBLIC_KEY_INFO*
aiKeyAlg As UInteger, ' ALG_ID
dwFlags As UInteger, ' DWORD
pvAuxInfo As IntPtr, ' void* optional
<Out> ByRef phKey As UIntPtr ' UINT_PTR* out
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function' hCryptProv : UINT_PTR
' dwCertEncodingType : CERT_QUERY_ENCODING_TYPE
' pInfo : CERT_PUBLIC_KEY_INFO*
' aiKeyAlg : ALG_ID
' dwFlags : DWORD
' pvAuxInfo : void* optional
' phKey : UINT_PTR* out
Declare PtrSafe Function CryptImportPublicKeyInfoEx Lib "crypt32" ( _
ByVal hCryptProv As LongPtr, _
ByVal dwCertEncodingType As Long, _
ByVal pInfo As LongPtr, _
ByVal aiKeyAlg As Long, _
ByVal dwFlags As Long, _
ByVal pvAuxInfo As LongPtr, _
ByRef phKey As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
CryptImportPublicKeyInfoEx = ctypes.windll.crypt32.CryptImportPublicKeyInfoEx
CryptImportPublicKeyInfoEx.restype = wintypes.BOOL
CryptImportPublicKeyInfoEx.argtypes = [
ctypes.c_size_t, # hCryptProv : UINT_PTR
wintypes.DWORD, # dwCertEncodingType : CERT_QUERY_ENCODING_TYPE
ctypes.c_void_p, # pInfo : CERT_PUBLIC_KEY_INFO*
wintypes.DWORD, # aiKeyAlg : ALG_ID
wintypes.DWORD, # dwFlags : DWORD
ctypes.POINTER(None), # pvAuxInfo : void* optional
ctypes.POINTER(ctypes.c_size_t), # phKey : UINT_PTR* out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('CRYPT32.dll')
CryptImportPublicKeyInfoEx = Fiddle::Function.new(
lib['CryptImportPublicKeyInfoEx'],
[
Fiddle::TYPE_UINTPTR_T, # hCryptProv : UINT_PTR
-Fiddle::TYPE_INT, # dwCertEncodingType : CERT_QUERY_ENCODING_TYPE
Fiddle::TYPE_VOIDP, # pInfo : CERT_PUBLIC_KEY_INFO*
-Fiddle::TYPE_INT, # aiKeyAlg : ALG_ID
-Fiddle::TYPE_INT, # dwFlags : DWORD
Fiddle::TYPE_VOIDP, # pvAuxInfo : void* optional
Fiddle::TYPE_VOIDP, # phKey : UINT_PTR* out
],
Fiddle::TYPE_INT)#[link(name = "crypt32")]
extern "system" {
fn CryptImportPublicKeyInfoEx(
hCryptProv: usize, // UINT_PTR
dwCertEncodingType: u32, // CERT_QUERY_ENCODING_TYPE
pInfo: *mut CERT_PUBLIC_KEY_INFO, // CERT_PUBLIC_KEY_INFO*
aiKeyAlg: u32, // ALG_ID
dwFlags: u32, // DWORD
pvAuxInfo: *mut (), // void* optional
phKey: *mut usize // UINT_PTR* 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 CryptImportPublicKeyInfoEx(UIntPtr hCryptProv, uint dwCertEncodingType, IntPtr pInfo, uint aiKeyAlg, uint dwFlags, IntPtr pvAuxInfo, out UIntPtr phKey);
"@
$api = Add-Type -MemberDefinition $sig -Name 'CRYPT32_CryptImportPublicKeyInfoEx' -Namespace Win32 -PassThru
# $api::CryptImportPublicKeyInfoEx(hCryptProv, dwCertEncodingType, pInfo, aiKeyAlg, dwFlags, pvAuxInfo, phKey)#uselib "CRYPT32.dll"
#func global CryptImportPublicKeyInfoEx "CryptImportPublicKeyInfoEx" sptr, sptr, sptr, sptr, sptr, sptr, sptr
; CryptImportPublicKeyInfoEx hCryptProv, dwCertEncodingType, varptr(pInfo), aiKeyAlg, dwFlags, pvAuxInfo, varptr(phKey) ; 戻り値は stat
; hCryptProv : UINT_PTR -> "sptr"
; dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> "sptr"
; pInfo : CERT_PUBLIC_KEY_INFO* -> "sptr"
; aiKeyAlg : ALG_ID -> "sptr"
; dwFlags : DWORD -> "sptr"
; pvAuxInfo : void* optional -> "sptr"
; phKey : UINT_PTR* out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "CRYPT32.dll" #cfunc global CryptImportPublicKeyInfoEx "CryptImportPublicKeyInfoEx" sptr, int, var, int, int, sptr, var ; res = CryptImportPublicKeyInfoEx(hCryptProv, dwCertEncodingType, pInfo, aiKeyAlg, dwFlags, pvAuxInfo, phKey) ; hCryptProv : UINT_PTR -> "sptr" ; dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> "int" ; pInfo : CERT_PUBLIC_KEY_INFO* -> "var" ; aiKeyAlg : ALG_ID -> "int" ; dwFlags : DWORD -> "int" ; pvAuxInfo : void* optional -> "sptr" ; phKey : UINT_PTR* out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "CRYPT32.dll" #cfunc global CryptImportPublicKeyInfoEx "CryptImportPublicKeyInfoEx" sptr, int, sptr, int, int, sptr, sptr ; res = CryptImportPublicKeyInfoEx(hCryptProv, dwCertEncodingType, varptr(pInfo), aiKeyAlg, dwFlags, pvAuxInfo, varptr(phKey)) ; hCryptProv : UINT_PTR -> "sptr" ; dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> "int" ; pInfo : CERT_PUBLIC_KEY_INFO* -> "sptr" ; aiKeyAlg : ALG_ID -> "int" ; dwFlags : DWORD -> "int" ; pvAuxInfo : void* optional -> "sptr" ; phKey : UINT_PTR* out -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; BOOL CryptImportPublicKeyInfoEx(UINT_PTR hCryptProv, CERT_QUERY_ENCODING_TYPE dwCertEncodingType, CERT_PUBLIC_KEY_INFO* pInfo, ALG_ID aiKeyAlg, DWORD dwFlags, void* pvAuxInfo, UINT_PTR* phKey) #uselib "CRYPT32.dll" #cfunc global CryptImportPublicKeyInfoEx "CryptImportPublicKeyInfoEx" intptr, int, var, int, int, intptr, var ; res = CryptImportPublicKeyInfoEx(hCryptProv, dwCertEncodingType, pInfo, aiKeyAlg, dwFlags, pvAuxInfo, phKey) ; hCryptProv : UINT_PTR -> "intptr" ; dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> "int" ; pInfo : CERT_PUBLIC_KEY_INFO* -> "var" ; aiKeyAlg : ALG_ID -> "int" ; dwFlags : DWORD -> "int" ; pvAuxInfo : void* optional -> "intptr" ; phKey : UINT_PTR* out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; BOOL CryptImportPublicKeyInfoEx(UINT_PTR hCryptProv, CERT_QUERY_ENCODING_TYPE dwCertEncodingType, CERT_PUBLIC_KEY_INFO* pInfo, ALG_ID aiKeyAlg, DWORD dwFlags, void* pvAuxInfo, UINT_PTR* phKey) #uselib "CRYPT32.dll" #cfunc global CryptImportPublicKeyInfoEx "CryptImportPublicKeyInfoEx" intptr, int, intptr, int, int, intptr, intptr ; res = CryptImportPublicKeyInfoEx(hCryptProv, dwCertEncodingType, varptr(pInfo), aiKeyAlg, dwFlags, pvAuxInfo, varptr(phKey)) ; hCryptProv : UINT_PTR -> "intptr" ; dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> "int" ; pInfo : CERT_PUBLIC_KEY_INFO* -> "intptr" ; aiKeyAlg : ALG_ID -> "int" ; dwFlags : DWORD -> "int" ; pvAuxInfo : void* optional -> "intptr" ; phKey : UINT_PTR* out -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
crypt32 = windows.NewLazySystemDLL("CRYPT32.dll")
procCryptImportPublicKeyInfoEx = crypt32.NewProc("CryptImportPublicKeyInfoEx")
)
// hCryptProv (UINT_PTR), dwCertEncodingType (CERT_QUERY_ENCODING_TYPE), pInfo (CERT_PUBLIC_KEY_INFO*), aiKeyAlg (ALG_ID), dwFlags (DWORD), pvAuxInfo (void* optional), phKey (UINT_PTR* out)
r1, _, err := procCryptImportPublicKeyInfoEx.Call(
uintptr(hCryptProv),
uintptr(dwCertEncodingType),
uintptr(pInfo),
uintptr(aiKeyAlg),
uintptr(dwFlags),
uintptr(pvAuxInfo),
uintptr(phKey),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // BOOLfunction CryptImportPublicKeyInfoEx(
hCryptProv: NativeUInt; // UINT_PTR
dwCertEncodingType: DWORD; // CERT_QUERY_ENCODING_TYPE
pInfo: Pointer; // CERT_PUBLIC_KEY_INFO*
aiKeyAlg: DWORD; // ALG_ID
dwFlags: DWORD; // DWORD
pvAuxInfo: Pointer; // void* optional
phKey: Pointer // UINT_PTR* out
): BOOL; stdcall;
external 'CRYPT32.dll' name 'CryptImportPublicKeyInfoEx';result := DllCall("CRYPT32\CryptImportPublicKeyInfoEx"
, "UPtr", hCryptProv ; UINT_PTR
, "UInt", dwCertEncodingType ; CERT_QUERY_ENCODING_TYPE
, "Ptr", pInfo ; CERT_PUBLIC_KEY_INFO*
, "UInt", aiKeyAlg ; ALG_ID
, "UInt", dwFlags ; DWORD
, "Ptr", pvAuxInfo ; void* optional
, "Ptr", phKey ; UINT_PTR* out
, "Int") ; return: BOOL●CryptImportPublicKeyInfoEx(hCryptProv, dwCertEncodingType, pInfo, aiKeyAlg, dwFlags, pvAuxInfo, phKey) = DLL("CRYPT32.dll", "bool CryptImportPublicKeyInfoEx(int, dword, void*, dword, dword, void*, void*)")
# 呼び出し: CryptImportPublicKeyInfoEx(hCryptProv, dwCertEncodingType, pInfo, aiKeyAlg, dwFlags, pvAuxInfo, phKey)
# hCryptProv : UINT_PTR -> "int"
# dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> "dword"
# pInfo : CERT_PUBLIC_KEY_INFO* -> "void*"
# aiKeyAlg : ALG_ID -> "dword"
# dwFlags : DWORD -> "dword"
# pvAuxInfo : void* optional -> "void*"
# phKey : UINT_PTR* out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "crypt32" fn CryptImportPublicKeyInfoEx(
hCryptProv: usize, // UINT_PTR
dwCertEncodingType: u32, // CERT_QUERY_ENCODING_TYPE
pInfo: [*c]CERT_PUBLIC_KEY_INFO, // CERT_PUBLIC_KEY_INFO*
aiKeyAlg: u32, // ALG_ID
dwFlags: u32, // DWORD
pvAuxInfo: ?*anyopaque, // void* optional
phKey: [*c]usize // UINT_PTR* out
) callconv(std.os.windows.WINAPI) i32;proc CryptImportPublicKeyInfoEx(
hCryptProv: uint, # UINT_PTR
dwCertEncodingType: uint32, # CERT_QUERY_ENCODING_TYPE
pInfo: ptr CERT_PUBLIC_KEY_INFO, # CERT_PUBLIC_KEY_INFO*
aiKeyAlg: uint32, # ALG_ID
dwFlags: uint32, # DWORD
pvAuxInfo: pointer, # void* optional
phKey: ptr uint # UINT_PTR* out
): int32 {.importc: "CryptImportPublicKeyInfoEx", stdcall, dynlib: "CRYPT32.dll".}pragma(lib, "crypt32");
extern(Windows)
int CryptImportPublicKeyInfoEx(
size_t hCryptProv, // UINT_PTR
uint dwCertEncodingType, // CERT_QUERY_ENCODING_TYPE
CERT_PUBLIC_KEY_INFO* pInfo, // CERT_PUBLIC_KEY_INFO*
uint aiKeyAlg, // ALG_ID
uint dwFlags, // DWORD
void* pvAuxInfo, // void* optional
size_t* phKey // UINT_PTR* out
);ccall((:CryptImportPublicKeyInfoEx, "CRYPT32.dll"), stdcall, Int32,
(Csize_t, UInt32, Ptr{CERT_PUBLIC_KEY_INFO}, UInt32, UInt32, Ptr{Cvoid}, Ptr{Csize_t}),
hCryptProv, dwCertEncodingType, pInfo, aiKeyAlg, dwFlags, pvAuxInfo, phKey)
# hCryptProv : UINT_PTR -> Csize_t
# dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> UInt32
# pInfo : CERT_PUBLIC_KEY_INFO* -> Ptr{CERT_PUBLIC_KEY_INFO}
# aiKeyAlg : ALG_ID -> UInt32
# dwFlags : DWORD -> UInt32
# pvAuxInfo : void* optional -> Ptr{Cvoid}
# phKey : UINT_PTR* out -> Ptr{Csize_t}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t CryptImportPublicKeyInfoEx(
uintptr_t hCryptProv,
uint32_t dwCertEncodingType,
void* pInfo,
uint32_t aiKeyAlg,
uint32_t dwFlags,
void* pvAuxInfo,
uintptr_t* phKey);
]]
local crypt32 = ffi.load("crypt32")
-- crypt32.CryptImportPublicKeyInfoEx(hCryptProv, dwCertEncodingType, pInfo, aiKeyAlg, dwFlags, pvAuxInfo, phKey)
-- hCryptProv : UINT_PTR
-- dwCertEncodingType : CERT_QUERY_ENCODING_TYPE
-- pInfo : CERT_PUBLIC_KEY_INFO*
-- aiKeyAlg : ALG_ID
-- dwFlags : DWORD
-- pvAuxInfo : void* optional
-- phKey : UINT_PTR* out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('CRYPT32.dll');
const CryptImportPublicKeyInfoEx = lib.func('__stdcall', 'CryptImportPublicKeyInfoEx', 'int32_t', ['uintptr_t', 'uint32_t', 'void *', 'uint32_t', 'uint32_t', 'void *', 'uintptr_t *']);
// CryptImportPublicKeyInfoEx(hCryptProv, dwCertEncodingType, pInfo, aiKeyAlg, dwFlags, pvAuxInfo, phKey)
// hCryptProv : UINT_PTR -> 'uintptr_t'
// dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> 'uint32_t'
// pInfo : CERT_PUBLIC_KEY_INFO* -> 'void *'
// aiKeyAlg : ALG_ID -> 'uint32_t'
// dwFlags : DWORD -> 'uint32_t'
// pvAuxInfo : void* optional -> 'void *'
// phKey : UINT_PTR* out -> 'uintptr_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("CRYPT32.dll", {
CryptImportPublicKeyInfoEx: { parameters: ["usize", "u32", "pointer", "u32", "u32", "pointer", "pointer"], result: "i32" },
});
// lib.symbols.CryptImportPublicKeyInfoEx(hCryptProv, dwCertEncodingType, pInfo, aiKeyAlg, dwFlags, pvAuxInfo, phKey)
// hCryptProv : UINT_PTR -> "usize"
// dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> "u32"
// pInfo : CERT_PUBLIC_KEY_INFO* -> "pointer"
// aiKeyAlg : ALG_ID -> "u32"
// dwFlags : DWORD -> "u32"
// pvAuxInfo : void* optional -> "pointer"
// phKey : UINT_PTR* out -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t CryptImportPublicKeyInfoEx(
size_t hCryptProv,
uint32_t dwCertEncodingType,
void* pInfo,
uint32_t aiKeyAlg,
uint32_t dwFlags,
void* pvAuxInfo,
size_t* phKey);
C, "CRYPT32.dll");
// $ffi->CryptImportPublicKeyInfoEx(hCryptProv, dwCertEncodingType, pInfo, aiKeyAlg, dwFlags, pvAuxInfo, phKey);
// hCryptProv : UINT_PTR
// dwCertEncodingType : CERT_QUERY_ENCODING_TYPE
// pInfo : CERT_PUBLIC_KEY_INFO*
// aiKeyAlg : ALG_ID
// dwFlags : DWORD
// pvAuxInfo : void* optional
// phKey : UINT_PTR* 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 CryptImportPublicKeyInfoEx(
long hCryptProv, // UINT_PTR
int dwCertEncodingType, // CERT_QUERY_ENCODING_TYPE
Pointer pInfo, // CERT_PUBLIC_KEY_INFO*
int aiKeyAlg, // ALG_ID
int dwFlags, // DWORD
Pointer pvAuxInfo, // void* optional
LongByReference phKey // UINT_PTR* out
);
}@[Link("crypt32")]
lib LibCRYPT32
fun CryptImportPublicKeyInfoEx = CryptImportPublicKeyInfoEx(
hCryptProv : LibC::SizeT, # UINT_PTR
dwCertEncodingType : UInt32, # CERT_QUERY_ENCODING_TYPE
pInfo : CERT_PUBLIC_KEY_INFO*, # CERT_PUBLIC_KEY_INFO*
aiKeyAlg : UInt32, # ALG_ID
dwFlags : UInt32, # DWORD
pvAuxInfo : Void*, # void* optional
phKey : LibC::SizeT* # UINT_PTR* out
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef CryptImportPublicKeyInfoExNative = Int32 Function(UintPtr, Uint32, Pointer<Void>, Uint32, Uint32, Pointer<Void>, Pointer<UintPtr>);
typedef CryptImportPublicKeyInfoExDart = int Function(int, int, Pointer<Void>, int, int, Pointer<Void>, Pointer<UintPtr>);
final CryptImportPublicKeyInfoEx = DynamicLibrary.open('CRYPT32.dll')
.lookupFunction<CryptImportPublicKeyInfoExNative, CryptImportPublicKeyInfoExDart>('CryptImportPublicKeyInfoEx');
// hCryptProv : UINT_PTR -> UintPtr
// dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> Uint32
// pInfo : CERT_PUBLIC_KEY_INFO* -> Pointer<Void>
// aiKeyAlg : ALG_ID -> Uint32
// dwFlags : DWORD -> Uint32
// pvAuxInfo : void* optional -> Pointer<Void>
// phKey : UINT_PTR* out -> Pointer<UintPtr>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function CryptImportPublicKeyInfoEx(
hCryptProv: NativeUInt; // UINT_PTR
dwCertEncodingType: DWORD; // CERT_QUERY_ENCODING_TYPE
pInfo: Pointer; // CERT_PUBLIC_KEY_INFO*
aiKeyAlg: DWORD; // ALG_ID
dwFlags: DWORD; // DWORD
pvAuxInfo: Pointer; // void* optional
phKey: Pointer // UINT_PTR* out
): BOOL; stdcall;
external 'CRYPT32.dll' name 'CryptImportPublicKeyInfoEx';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "CryptImportPublicKeyInfoEx"
c_CryptImportPublicKeyInfoEx :: CUIntPtr -> Word32 -> Ptr () -> Word32 -> Word32 -> Ptr () -> Ptr CUIntPtr -> IO CInt
-- hCryptProv : UINT_PTR -> CUIntPtr
-- dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> Word32
-- pInfo : CERT_PUBLIC_KEY_INFO* -> Ptr ()
-- aiKeyAlg : ALG_ID -> Word32
-- dwFlags : DWORD -> Word32
-- pvAuxInfo : void* optional -> Ptr ()
-- phKey : UINT_PTR* out -> Ptr CUIntPtr
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let cryptimportpublickeyinfoex =
foreign "CryptImportPublicKeyInfoEx"
(size_t @-> uint32_t @-> (ptr void) @-> uint32_t @-> uint32_t @-> (ptr void) @-> (ptr size_t) @-> returning int32_t)
(* hCryptProv : UINT_PTR -> size_t *)
(* dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> uint32_t *)
(* pInfo : CERT_PUBLIC_KEY_INFO* -> (ptr void) *)
(* aiKeyAlg : ALG_ID -> uint32_t *)
(* dwFlags : DWORD -> uint32_t *)
(* pvAuxInfo : void* optional -> (ptr void) *)
(* phKey : UINT_PTR* out -> (ptr size_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library crypt32 (t "CRYPT32.dll"))
(cffi:use-foreign-library crypt32)
(cffi:defcfun ("CryptImportPublicKeyInfoEx" crypt-import-public-key-info-ex :convention :stdcall) :int32
(h-crypt-prov :uint64) ; UINT_PTR
(dw-cert-encoding-type :uint32) ; CERT_QUERY_ENCODING_TYPE
(p-info :pointer) ; CERT_PUBLIC_KEY_INFO*
(ai-key-alg :uint32) ; ALG_ID
(dw-flags :uint32) ; DWORD
(pv-aux-info :pointer) ; void* optional
(ph-key :pointer)) ; UINT_PTR* out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $CryptImportPublicKeyInfoEx = Win32::API::More->new('CRYPT32',
'BOOL CryptImportPublicKeyInfoEx(WPARAM hCryptProv, DWORD dwCertEncodingType, LPVOID pInfo, DWORD aiKeyAlg, DWORD dwFlags, LPVOID pvAuxInfo, LPVOID phKey)');
# my $ret = $CryptImportPublicKeyInfoEx->Call($hCryptProv, $dwCertEncodingType, $pInfo, $aiKeyAlg, $dwFlags, $pvAuxInfo, $phKey);
# hCryptProv : UINT_PTR -> WPARAM
# dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> DWORD
# pInfo : CERT_PUBLIC_KEY_INFO* -> LPVOID
# aiKeyAlg : ALG_ID -> DWORD
# dwFlags : DWORD -> DWORD
# pvAuxInfo : void* optional -> LPVOID
# phKey : UINT_PTR* out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
類似 API
- f CryptImportPublicKeyInfo — 公開鍵情報をインポートして鍵ハンドルを取得する。
- f CryptImportPublicKeyInfoEx2 — 公開鍵情報をBCrypt鍵ハンドルとしてインポートする。
公式の関連項目
- f CryptExportPublicKeyInfoEx — オブジェクトIDを指定して公開鍵情報を拡張エクスポートする。