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

CertGetPublicKeyLength

関数
公開鍵情報からビット単位の鍵長を取得する。
DLLCRYPT32.dll呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

DWORD CertGetPublicKeyLength(
    CERT_QUERY_ENCODING_TYPE dwCertEncodingType,
    CERT_PUBLIC_KEY_INFO* pPublicKey
);

パラメーター

名前方向説明
dwCertEncodingTypeCERT_QUERY_ENCODING_TYPEin

使用するエンコードの種類を指定します。次の例のように、証明書のエンコードの種類とメッセージのエンコードの種類の両方をビット単位の OR 演算で組み合わせて指定することも、常に可能です。

X509_ASN_ENCODING | PKCS_7_ASN_ENCODING 現在定義されているエンコードの種類は次のとおりです。

pPublicKeyCERT_PUBLIC_KEY_INFO*in長さを取得する対象のキーを含む公開キー BLOB へのポインターです。

戻り値の型: DWORD

公式ドキュメント

CertGetPublicKeyLength 関数は、公開キー BLOB から公開キー/秘密キーのビット長を取得します。

戻り値

公開キー/秘密キーの長さをビット単位で返します。キーの長さを特定できない場合は 0 を返します。

失敗の原因を確認するには、GetLastError を呼び出します。

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

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

DWORD CertGetPublicKeyLength(
    CERT_QUERY_ENCODING_TYPE dwCertEncodingType,
    CERT_PUBLIC_KEY_INFO* pPublicKey
);
[DllImport("CRYPT32.dll", SetLastError = true, ExactSpelling = true)]
static extern uint CertGetPublicKeyLength(
    uint dwCertEncodingType,   // CERT_QUERY_ENCODING_TYPE
    IntPtr pPublicKey   // CERT_PUBLIC_KEY_INFO*
);
<DllImport("CRYPT32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function CertGetPublicKeyLength(
    dwCertEncodingType As UInteger,   ' CERT_QUERY_ENCODING_TYPE
    pPublicKey As IntPtr   ' CERT_PUBLIC_KEY_INFO*
) As UInteger
End Function
' dwCertEncodingType : CERT_QUERY_ENCODING_TYPE
' pPublicKey : CERT_PUBLIC_KEY_INFO*
Declare PtrSafe Function CertGetPublicKeyLength Lib "crypt32" ( _
    ByVal dwCertEncodingType As Long, _
    ByVal pPublicKey As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

CertGetPublicKeyLength = ctypes.windll.crypt32.CertGetPublicKeyLength
CertGetPublicKeyLength.restype = wintypes.DWORD
CertGetPublicKeyLength.argtypes = [
    wintypes.DWORD,  # dwCertEncodingType : CERT_QUERY_ENCODING_TYPE
    ctypes.c_void_p,  # pPublicKey : CERT_PUBLIC_KEY_INFO*
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('CRYPT32.dll')
CertGetPublicKeyLength = Fiddle::Function.new(
  lib['CertGetPublicKeyLength'],
  [
    -Fiddle::TYPE_INT,  # dwCertEncodingType : CERT_QUERY_ENCODING_TYPE
    Fiddle::TYPE_VOIDP,  # pPublicKey : CERT_PUBLIC_KEY_INFO*
  ],
  -Fiddle::TYPE_INT)
#[link(name = "crypt32")]
extern "system" {
    fn CertGetPublicKeyLength(
        dwCertEncodingType: u32,  // CERT_QUERY_ENCODING_TYPE
        pPublicKey: *mut CERT_PUBLIC_KEY_INFO  // CERT_PUBLIC_KEY_INFO*
    ) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("CRYPT32.dll", SetLastError = true)]
public static extern uint CertGetPublicKeyLength(uint dwCertEncodingType, IntPtr pPublicKey);
"@
$api = Add-Type -MemberDefinition $sig -Name 'CRYPT32_CertGetPublicKeyLength' -Namespace Win32 -PassThru
# $api::CertGetPublicKeyLength(dwCertEncodingType, pPublicKey)
#uselib "CRYPT32.dll"
#func global CertGetPublicKeyLength "CertGetPublicKeyLength" sptr, sptr
; CertGetPublicKeyLength dwCertEncodingType, varptr(pPublicKey)   ; 戻り値は stat
; dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> "sptr"
; pPublicKey : CERT_PUBLIC_KEY_INFO* -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "CRYPT32.dll"
#cfunc global CertGetPublicKeyLength "CertGetPublicKeyLength" int, var
; res = CertGetPublicKeyLength(dwCertEncodingType, pPublicKey)
; dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> "int"
; pPublicKey : CERT_PUBLIC_KEY_INFO* -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; DWORD CertGetPublicKeyLength(CERT_QUERY_ENCODING_TYPE dwCertEncodingType, CERT_PUBLIC_KEY_INFO* pPublicKey)
#uselib "CRYPT32.dll"
#cfunc global CertGetPublicKeyLength "CertGetPublicKeyLength" int, var
; res = CertGetPublicKeyLength(dwCertEncodingType, pPublicKey)
; dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> "int"
; pPublicKey : CERT_PUBLIC_KEY_INFO* -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	crypt32 = windows.NewLazySystemDLL("CRYPT32.dll")
	procCertGetPublicKeyLength = crypt32.NewProc("CertGetPublicKeyLength")
)

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

extern "crypt32" fn CertGetPublicKeyLength(
    dwCertEncodingType: u32, // CERT_QUERY_ENCODING_TYPE
    pPublicKey: [*c]CERT_PUBLIC_KEY_INFO // CERT_PUBLIC_KEY_INFO*
) callconv(std.os.windows.WINAPI) u32;
proc CertGetPublicKeyLength(
    dwCertEncodingType: uint32,  # CERT_QUERY_ENCODING_TYPE
    pPublicKey: ptr CERT_PUBLIC_KEY_INFO  # CERT_PUBLIC_KEY_INFO*
): uint32 {.importc: "CertGetPublicKeyLength", stdcall, dynlib: "CRYPT32.dll".}
pragma(lib, "crypt32");
extern(Windows)
uint CertGetPublicKeyLength(
    uint dwCertEncodingType,   // CERT_QUERY_ENCODING_TYPE
    CERT_PUBLIC_KEY_INFO* pPublicKey   // CERT_PUBLIC_KEY_INFO*
);
ccall((:CertGetPublicKeyLength, "CRYPT32.dll"), stdcall, UInt32,
      (UInt32, Ptr{CERT_PUBLIC_KEY_INFO}),
      dwCertEncodingType, pPublicKey)
# dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> UInt32
# pPublicKey : CERT_PUBLIC_KEY_INFO* -> Ptr{CERT_PUBLIC_KEY_INFO}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
uint32_t CertGetPublicKeyLength(
    uint32_t dwCertEncodingType,
    void* pPublicKey);
]]
local crypt32 = ffi.load("crypt32")
-- crypt32.CertGetPublicKeyLength(dwCertEncodingType, pPublicKey)
-- dwCertEncodingType : CERT_QUERY_ENCODING_TYPE
-- pPublicKey : CERT_PUBLIC_KEY_INFO*
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('CRYPT32.dll');
const CertGetPublicKeyLength = lib.func('__stdcall', 'CertGetPublicKeyLength', 'uint32_t', ['uint32_t', 'void *']);
// CertGetPublicKeyLength(dwCertEncodingType, pPublicKey)
// dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> 'uint32_t'
// pPublicKey : CERT_PUBLIC_KEY_INFO* -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("CRYPT32.dll", {
  CertGetPublicKeyLength: { parameters: ["u32", "pointer"], result: "u32" },
});
// lib.symbols.CertGetPublicKeyLength(dwCertEncodingType, pPublicKey)
// dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> "u32"
// pPublicKey : CERT_PUBLIC_KEY_INFO* -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
uint32_t CertGetPublicKeyLength(
    uint32_t dwCertEncodingType,
    void* pPublicKey);
C, "CRYPT32.dll");
// $ffi->CertGetPublicKeyLength(dwCertEncodingType, pPublicKey);
// dwCertEncodingType : CERT_QUERY_ENCODING_TYPE
// pPublicKey : CERT_PUBLIC_KEY_INFO*
// 構造体/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);
    int CertGetPublicKeyLength(
        int dwCertEncodingType,   // CERT_QUERY_ENCODING_TYPE
        Pointer pPublicKey   // CERT_PUBLIC_KEY_INFO*
    );
}
@[Link("crypt32")]
lib LibCRYPT32
  fun CertGetPublicKeyLength = CertGetPublicKeyLength(
    dwCertEncodingType : UInt32,   # CERT_QUERY_ENCODING_TYPE
    pPublicKey : CERT_PUBLIC_KEY_INFO*   # CERT_PUBLIC_KEY_INFO*
  ) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef CertGetPublicKeyLengthNative = Uint32 Function(Uint32, Pointer<Void>);
typedef CertGetPublicKeyLengthDart = int Function(int, Pointer<Void>);
final CertGetPublicKeyLength = DynamicLibrary.open('CRYPT32.dll')
    .lookupFunction<CertGetPublicKeyLengthNative, CertGetPublicKeyLengthDart>('CertGetPublicKeyLength');
// dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> Uint32
// pPublicKey : CERT_PUBLIC_KEY_INFO* -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function CertGetPublicKeyLength(
  dwCertEncodingType: DWORD;   // CERT_QUERY_ENCODING_TYPE
  pPublicKey: Pointer   // CERT_PUBLIC_KEY_INFO*
): DWORD; stdcall;
  external 'CRYPT32.dll' name 'CertGetPublicKeyLength';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "CertGetPublicKeyLength"
  c_CertGetPublicKeyLength :: Word32 -> Ptr () -> IO Word32
-- dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> Word32
-- pPublicKey : CERT_PUBLIC_KEY_INFO* -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let certgetpublickeylength =
  foreign "CertGetPublicKeyLength"
    (uint32_t @-> (ptr void) @-> returning uint32_t)
(* dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> uint32_t *)
(* pPublicKey : CERT_PUBLIC_KEY_INFO* -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library crypt32 (t "CRYPT32.dll"))
(cffi:use-foreign-library crypt32)

(cffi:defcfun ("CertGetPublicKeyLength" cert-get-public-key-length :convention :stdcall) :uint32
  (dw-cert-encoding-type :uint32)   ; CERT_QUERY_ENCODING_TYPE
  (p-public-key :pointer))   ; CERT_PUBLIC_KEY_INFO*
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $CertGetPublicKeyLength = Win32::API::More->new('CRYPT32',
    'DWORD CertGetPublicKeyLength(DWORD dwCertEncodingType, LPVOID pPublicKey)');
# my $ret = $CertGetPublicKeyLength->Call($dwCertEncodingType, $pPublicKey);
# dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> DWORD
# pPublicKey : CERT_PUBLIC_KEY_INFO* -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

使用する型