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

CryptImportPublicKeyInfo

関数
公開鍵情報をインポートして鍵ハンドルを取得する。
DLLCRYPT32.dll呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

BOOL CryptImportPublicKeyInfo(
    UINT_PTR hCryptProv,
    CERT_QUERY_ENCODING_TYPE dwCertEncodingType,
    CERT_PUBLIC_KEY_INFO* pInfo,
    UINT_PTR* phKey
);

パラメーター

名前方向説明
hCryptProvUINT_PTRin公開鍵のインポート時に使用する暗号化サービスプロバイダー (CSP) のハンドルです。このハンドルは、あらかじめ CryptAcquireContext を使用して作成しておく必要があります。
dwCertEncodingTypeCERT_QUERY_ENCODING_TYPEin

使用するエンコード方式を指定します。証明書とメッセージの両方のエンコード方式を、次の例に示すようにビットごとの OR 演算で組み合わせて指定しても常に問題ありません。

X509_ASN_ENCODING | PKCS_7_ASN_ENCODING

現在定義されているエンコード方式は次のとおりです。

pInfoCERT_PUBLIC_KEY_INFO*inプロバイダーにインポートする公開鍵を格納した CERT_PUBLIC_KEY_INFO 構造体のアドレスです。
phKeyUINT_PTR*outインポートされた公開鍵のハンドルを受け取る HCRYPTKEY 変数のアドレスです。公開鍵の使用が終わったら、CryptDestroyKey 関数を呼び出してハンドルを解放してください。

戻り値の型: BOOL

公式ドキュメント

公開鍵情報を変換してプロバイダーにインポートし、その公開鍵のハンドルを返します。

戻り値

関数が成功すると、戻り値は 0 以外 (TRUE) になります。

関数が失敗すると、戻り値は 0 (FALSE) になります。拡張エラー情報を取得するには、 GetLastError を呼び出します。

注意 呼び出される関数 CryptGetUserKey および CryptExportKey からのエラーが、この関数に伝播することがあります。この関数には次のエラーコードがあります。
戻り値 説明
ERROR_FILE_NOT_FOUND
指定された dwCertEncodingType および pInfo->Algorithm.pszObjId パラメーターに対して、インストールまたは登録できるインポート関数が見つかりませんでした。

関数が失敗した場合、GetLastErrorAbstract 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)

各言語での呼び出し定義

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

BOOL CryptImportPublicKeyInfo(
    UINT_PTR hCryptProv,
    CERT_QUERY_ENCODING_TYPE dwCertEncodingType,
    CERT_PUBLIC_KEY_INFO* pInfo,
    UINT_PTR* phKey
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("CRYPT32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool CryptImportPublicKeyInfo(
    UIntPtr hCryptProv,   // UINT_PTR
    uint dwCertEncodingType,   // CERT_QUERY_ENCODING_TYPE
    IntPtr pInfo,   // CERT_PUBLIC_KEY_INFO*
    out UIntPtr phKey   // UINT_PTR* out
);
<DllImport("CRYPT32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function CryptImportPublicKeyInfo(
    hCryptProv As UIntPtr,   ' UINT_PTR
    dwCertEncodingType As UInteger,   ' CERT_QUERY_ENCODING_TYPE
    pInfo As IntPtr,   ' CERT_PUBLIC_KEY_INFO*
    <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*
' phKey : UINT_PTR* out
Declare PtrSafe Function CryptImportPublicKeyInfo Lib "crypt32" ( _
    ByVal hCryptProv As LongPtr, _
    ByVal dwCertEncodingType As Long, _
    ByVal pInfo 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

CryptImportPublicKeyInfo = ctypes.windll.crypt32.CryptImportPublicKeyInfo
CryptImportPublicKeyInfo.restype = wintypes.BOOL
CryptImportPublicKeyInfo.argtypes = [
    ctypes.c_size_t,  # hCryptProv : UINT_PTR
    wintypes.DWORD,  # dwCertEncodingType : CERT_QUERY_ENCODING_TYPE
    ctypes.c_void_p,  # pInfo : CERT_PUBLIC_KEY_INFO*
    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')
CryptImportPublicKeyInfo = Fiddle::Function.new(
  lib['CryptImportPublicKeyInfo'],
  [
    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_VOIDP,  # phKey : UINT_PTR* out
  ],
  Fiddle::TYPE_INT)
#[link(name = "crypt32")]
extern "system" {
    fn CryptImportPublicKeyInfo(
        hCryptProv: usize,  // UINT_PTR
        dwCertEncodingType: u32,  // CERT_QUERY_ENCODING_TYPE
        pInfo: *mut CERT_PUBLIC_KEY_INFO,  // CERT_PUBLIC_KEY_INFO*
        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 CryptImportPublicKeyInfo(UIntPtr hCryptProv, uint dwCertEncodingType, IntPtr pInfo, out UIntPtr phKey);
"@
$api = Add-Type -MemberDefinition $sig -Name 'CRYPT32_CryptImportPublicKeyInfo' -Namespace Win32 -PassThru
# $api::CryptImportPublicKeyInfo(hCryptProv, dwCertEncodingType, pInfo, phKey)
#uselib "CRYPT32.dll"
#func global CryptImportPublicKeyInfo "CryptImportPublicKeyInfo" sptr, sptr, sptr, sptr
; CryptImportPublicKeyInfo hCryptProv, dwCertEncodingType, varptr(pInfo), varptr(phKey)   ; 戻り値は stat
; hCryptProv : UINT_PTR -> "sptr"
; dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> "sptr"
; pInfo : CERT_PUBLIC_KEY_INFO* -> "sptr"
; phKey : UINT_PTR* out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "CRYPT32.dll"
#cfunc global CryptImportPublicKeyInfo "CryptImportPublicKeyInfo" sptr, int, var, var
; res = CryptImportPublicKeyInfo(hCryptProv, dwCertEncodingType, pInfo, phKey)
; hCryptProv : UINT_PTR -> "sptr"
; dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> "int"
; pInfo : CERT_PUBLIC_KEY_INFO* -> "var"
; phKey : UINT_PTR* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL CryptImportPublicKeyInfo(UINT_PTR hCryptProv, CERT_QUERY_ENCODING_TYPE dwCertEncodingType, CERT_PUBLIC_KEY_INFO* pInfo, UINT_PTR* phKey)
#uselib "CRYPT32.dll"
#cfunc global CryptImportPublicKeyInfo "CryptImportPublicKeyInfo" intptr, int, var, var
; res = CryptImportPublicKeyInfo(hCryptProv, dwCertEncodingType, pInfo, phKey)
; hCryptProv : UINT_PTR -> "intptr"
; dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> "int"
; pInfo : CERT_PUBLIC_KEY_INFO* -> "var"
; phKey : UINT_PTR* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	crypt32 = windows.NewLazySystemDLL("CRYPT32.dll")
	procCryptImportPublicKeyInfo = crypt32.NewProc("CryptImportPublicKeyInfo")
)

// hCryptProv (UINT_PTR), dwCertEncodingType (CERT_QUERY_ENCODING_TYPE), pInfo (CERT_PUBLIC_KEY_INFO*), phKey (UINT_PTR* out)
r1, _, err := procCryptImportPublicKeyInfo.Call(
	uintptr(hCryptProv),
	uintptr(dwCertEncodingType),
	uintptr(pInfo),
	uintptr(phKey),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function CryptImportPublicKeyInfo(
  hCryptProv: NativeUInt;   // UINT_PTR
  dwCertEncodingType: DWORD;   // CERT_QUERY_ENCODING_TYPE
  pInfo: Pointer;   // CERT_PUBLIC_KEY_INFO*
  phKey: Pointer   // UINT_PTR* out
): BOOL; stdcall;
  external 'CRYPT32.dll' name 'CryptImportPublicKeyInfo';
result := DllCall("CRYPT32\CryptImportPublicKeyInfo"
    , "UPtr", hCryptProv   ; UINT_PTR
    , "UInt", dwCertEncodingType   ; CERT_QUERY_ENCODING_TYPE
    , "Ptr", pInfo   ; CERT_PUBLIC_KEY_INFO*
    , "Ptr", phKey   ; UINT_PTR* out
    , "Int")   ; return: BOOL
●CryptImportPublicKeyInfo(hCryptProv, dwCertEncodingType, pInfo, phKey) = DLL("CRYPT32.dll", "bool CryptImportPublicKeyInfo(int, dword, void*, void*)")
# 呼び出し: CryptImportPublicKeyInfo(hCryptProv, dwCertEncodingType, pInfo, phKey)
# hCryptProv : UINT_PTR -> "int"
# dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> "dword"
# pInfo : CERT_PUBLIC_KEY_INFO* -> "void*"
# phKey : UINT_PTR* out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "crypt32" fn CryptImportPublicKeyInfo(
    hCryptProv: usize, // UINT_PTR
    dwCertEncodingType: u32, // CERT_QUERY_ENCODING_TYPE
    pInfo: [*c]CERT_PUBLIC_KEY_INFO, // CERT_PUBLIC_KEY_INFO*
    phKey: [*c]usize // UINT_PTR* out
) callconv(std.os.windows.WINAPI) i32;
proc CryptImportPublicKeyInfo(
    hCryptProv: uint,  # UINT_PTR
    dwCertEncodingType: uint32,  # CERT_QUERY_ENCODING_TYPE
    pInfo: ptr CERT_PUBLIC_KEY_INFO,  # CERT_PUBLIC_KEY_INFO*
    phKey: ptr uint  # UINT_PTR* out
): int32 {.importc: "CryptImportPublicKeyInfo", stdcall, dynlib: "CRYPT32.dll".}
pragma(lib, "crypt32");
extern(Windows)
int CryptImportPublicKeyInfo(
    size_t hCryptProv,   // UINT_PTR
    uint dwCertEncodingType,   // CERT_QUERY_ENCODING_TYPE
    CERT_PUBLIC_KEY_INFO* pInfo,   // CERT_PUBLIC_KEY_INFO*
    size_t* phKey   // UINT_PTR* out
);
ccall((:CryptImportPublicKeyInfo, "CRYPT32.dll"), stdcall, Int32,
      (Csize_t, UInt32, Ptr{CERT_PUBLIC_KEY_INFO}, Ptr{Csize_t}),
      hCryptProv, dwCertEncodingType, pInfo, phKey)
# hCryptProv : UINT_PTR -> Csize_t
# dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> UInt32
# pInfo : CERT_PUBLIC_KEY_INFO* -> Ptr{CERT_PUBLIC_KEY_INFO}
# phKey : UINT_PTR* out -> Ptr{Csize_t}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t CryptImportPublicKeyInfo(
    uintptr_t hCryptProv,
    uint32_t dwCertEncodingType,
    void* pInfo,
    uintptr_t* phKey);
]]
local crypt32 = ffi.load("crypt32")
-- crypt32.CryptImportPublicKeyInfo(hCryptProv, dwCertEncodingType, pInfo, phKey)
-- hCryptProv : UINT_PTR
-- dwCertEncodingType : CERT_QUERY_ENCODING_TYPE
-- pInfo : CERT_PUBLIC_KEY_INFO*
-- phKey : UINT_PTR* out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('CRYPT32.dll');
const CryptImportPublicKeyInfo = lib.func('__stdcall', 'CryptImportPublicKeyInfo', 'int32_t', ['uintptr_t', 'uint32_t', 'void *', 'uintptr_t *']);
// CryptImportPublicKeyInfo(hCryptProv, dwCertEncodingType, pInfo, phKey)
// hCryptProv : UINT_PTR -> 'uintptr_t'
// dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> 'uint32_t'
// pInfo : CERT_PUBLIC_KEY_INFO* -> 'void *'
// phKey : UINT_PTR* out -> 'uintptr_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("CRYPT32.dll", {
  CryptImportPublicKeyInfo: { parameters: ["usize", "u32", "pointer", "pointer"], result: "i32" },
});
// lib.symbols.CryptImportPublicKeyInfo(hCryptProv, dwCertEncodingType, pInfo, phKey)
// hCryptProv : UINT_PTR -> "usize"
// dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> "u32"
// pInfo : CERT_PUBLIC_KEY_INFO* -> "pointer"
// phKey : UINT_PTR* out -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t CryptImportPublicKeyInfo(
    size_t hCryptProv,
    uint32_t dwCertEncodingType,
    void* pInfo,
    size_t* phKey);
C, "CRYPT32.dll");
// $ffi->CryptImportPublicKeyInfo(hCryptProv, dwCertEncodingType, pInfo, phKey);
// hCryptProv : UINT_PTR
// dwCertEncodingType : CERT_QUERY_ENCODING_TYPE
// pInfo : CERT_PUBLIC_KEY_INFO*
// 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 CryptImportPublicKeyInfo(
        long hCryptProv,   // UINT_PTR
        int dwCertEncodingType,   // CERT_QUERY_ENCODING_TYPE
        Pointer pInfo,   // CERT_PUBLIC_KEY_INFO*
        LongByReference phKey   // UINT_PTR* out
    );
}
@[Link("crypt32")]
lib LibCRYPT32
  fun CryptImportPublicKeyInfo = CryptImportPublicKeyInfo(
    hCryptProv : LibC::SizeT,   # UINT_PTR
    dwCertEncodingType : UInt32,   # CERT_QUERY_ENCODING_TYPE
    pInfo : CERT_PUBLIC_KEY_INFO*,   # CERT_PUBLIC_KEY_INFO*
    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 CryptImportPublicKeyInfoNative = Int32 Function(UintPtr, Uint32, Pointer<Void>, Pointer<UintPtr>);
typedef CryptImportPublicKeyInfoDart = int Function(int, int, Pointer<Void>, Pointer<UintPtr>);
final CryptImportPublicKeyInfo = DynamicLibrary.open('CRYPT32.dll')
    .lookupFunction<CryptImportPublicKeyInfoNative, CryptImportPublicKeyInfoDart>('CryptImportPublicKeyInfo');
// hCryptProv : UINT_PTR -> UintPtr
// dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> Uint32
// pInfo : CERT_PUBLIC_KEY_INFO* -> Pointer<Void>
// phKey : UINT_PTR* out -> Pointer<UintPtr>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function CryptImportPublicKeyInfo(
  hCryptProv: NativeUInt;   // UINT_PTR
  dwCertEncodingType: DWORD;   // CERT_QUERY_ENCODING_TYPE
  pInfo: Pointer;   // CERT_PUBLIC_KEY_INFO*
  phKey: Pointer   // UINT_PTR* out
): BOOL; stdcall;
  external 'CRYPT32.dll' name 'CryptImportPublicKeyInfo';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "CryptImportPublicKeyInfo"
  c_CryptImportPublicKeyInfo :: CUIntPtr -> Word32 -> Ptr () -> Ptr CUIntPtr -> IO CInt
-- hCryptProv : UINT_PTR -> CUIntPtr
-- dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> Word32
-- pInfo : CERT_PUBLIC_KEY_INFO* -> Ptr ()
-- phKey : UINT_PTR* out -> Ptr CUIntPtr
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let cryptimportpublickeyinfo =
  foreign "CryptImportPublicKeyInfo"
    (size_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) *)
(* 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 ("CryptImportPublicKeyInfo" crypt-import-public-key-info :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*
  (ph-key :pointer))   ; UINT_PTR* out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $CryptImportPublicKeyInfo = Win32::API::More->new('CRYPT32',
    'BOOL CryptImportPublicKeyInfo(WPARAM hCryptProv, DWORD dwCertEncodingType, LPVOID pInfo, LPVOID phKey)');
# my $ret = $CryptImportPublicKeyInfo->Call($hCryptProv, $dwCertEncodingType, $pInfo, $phKey);
# hCryptProv : UINT_PTR -> WPARAM
# dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> DWORD
# pInfo : CERT_PUBLIC_KEY_INFO* -> LPVOID
# phKey : UINT_PTR* out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

類似 API
公式の関連項目
使用する型