Schannel の証明書の取得
次の例は、証明書を含む CERT_CONTEXT 構造を取得する手順を示しています。アプリケーションに適した証明書と証明書ストアを選択する必要があります。
この例では、証明書ストアを開き、SCHANNEL_CRED構造を使用して AcquireCredentialsHandle 関数に渡される証明書を検索する方法を示します。
#include <windows.h>
#include <stdio.h>
#pragma comment(lib, "crypt32.lib")
#define MACHINE_NAME "example server"
PCCERT_CONTEXT getServerCertificate ()
{
HCERTSTORE hMyCertStore = NULL;
PCCERT_CONTEXT aCertContext = NULL;
//-------------------------------------------------------
// Open the My store, also called the personal store.
// This call to CertOpenStore opens the Local_Machine My
// store as opposed to the Current_User's My store.
hMyCertStore = CertOpenStore(CERT_STORE_PROV_SYSTEM,
X509_ASN_ENCODING,
0,
CERT_SYSTEM_STORE_LOCAL_MACHINE,
L"MY");
if (hMyCertStore == NULL)
{
printf("Error opening MY store for server.\n");
goto cleanup;
}
//-------------------------------------------------------
// Search for a certificate with some specified
// string in it. This example attempts to find
// a certificate with the string "example server" in
// its subject string. Substitute an appropriate string
// to find a certificate for a specific user.
aCertContext = CertFindCertificateInStore(hMyCertStore,
X509_ASN_ENCODING,
0,
CERT_FIND_SUBJECT_STR_A,
MACHINE_NAME, // use appropriate subject name
NULL
);
if (aCertContext == NULL)
{
printf("Error retrieving server certificate.");
goto cleanup;
}
cleanup:
if(hMyCertStore)
{
CertCloseStore(hMyCertStore,0);
}
return aCertContext;
}