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

CertComparePublicKeyInfo

関数
2つの公開鍵情報が一致するかを比較する。
DLLCRYPT32.dll呼出規約winapi対応OSWindows XP 以降

シグネチャ

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

BOOL CertComparePublicKeyInfo(
    CERT_QUERY_ENCODING_TYPE dwCertEncodingType,
    CERT_PUBLIC_KEY_INFO* pPublicKey1,
    CERT_PUBLIC_KEY_INFO* pPublicKey2
);

パラメーター

名前方向説明
dwCertEncodingTypeCERT_QUERY_ENCODING_TYPEin

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

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

pPublicKey1CERT_PUBLIC_KEY_INFO*in比較対象となる 1 つ目の公開鍵の CERT_PUBLIC_KEY_INFO へのポインター。
pPublicKey2CERT_PUBLIC_KEY_INFO*in比較対象となる 2 つ目の公開鍵の CERT_PUBLIC_KEY_INFO へのポインター。

戻り値の型: BOOL

公式ドキュメント

CertComparePublicKeyInfo 関数は、エンコードされた 2 つの公開鍵を比較し、それらが同一かどうかを判定します。

戻り値

公開鍵が同一であり、関数が成功した場合、関数は 0 以外の値 (TRUE) を返します。

関数が失敗した場合は、0 (FALSE) を返します。

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

各言語での呼び出し定義

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

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

CertComparePublicKeyInfo = ctypes.windll.crypt32.CertComparePublicKeyInfo
CertComparePublicKeyInfo.restype = wintypes.BOOL
CertComparePublicKeyInfo.argtypes = [
    wintypes.DWORD,  # dwCertEncodingType : CERT_QUERY_ENCODING_TYPE
    ctypes.c_void_p,  # pPublicKey1 : CERT_PUBLIC_KEY_INFO*
    ctypes.c_void_p,  # pPublicKey2 : CERT_PUBLIC_KEY_INFO*
]
require 'fiddle'
require 'fiddle/import'

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

var (
	crypt32 = windows.NewLazySystemDLL("CRYPT32.dll")
	procCertComparePublicKeyInfo = crypt32.NewProc("CertComparePublicKeyInfo")
)

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

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

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

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

let certcomparepublickeyinfo =
  foreign "CertComparePublicKeyInfo"
    (uint32_t @-> (ptr void) @-> (ptr void) @-> returning int32_t)
(* dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> uint32_t *)
(* pPublicKey1 : CERT_PUBLIC_KEY_INFO* -> (ptr void) *)
(* pPublicKey2 : 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 ("CertComparePublicKeyInfo" cert-compare-public-key-info :convention :stdcall) :int32
  (dw-cert-encoding-type :uint32)   ; CERT_QUERY_ENCODING_TYPE
  (p-public-key1 :pointer)   ; CERT_PUBLIC_KEY_INFO*
  (p-public-key2 :pointer))   ; CERT_PUBLIC_KEY_INFO*
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $CertComparePublicKeyInfo = Win32::API::More->new('CRYPT32',
    'BOOL CertComparePublicKeyInfo(DWORD dwCertEncodingType, LPVOID pPublicKey1, LPVOID pPublicKey2)');
# my $ret = $CertComparePublicKeyInfo->Call($dwCertEncodingType, $pPublicKey1, $pPublicKey2);
# dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> DWORD
# pPublicKey1 : CERT_PUBLIC_KEY_INFO* -> LPVOID
# pPublicKey2 : CERT_PUBLIC_KEY_INFO* -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

使用する型