CertGetSubjectCertificateFromStore (wincrypt.h)
Fungsi CertGetSubjectCertificateFromStore kembali dari sertifikat menyimpan konteks sertifikat subjek yang diidentifikasi secara unik oleh penerbit dan nomor serinya.
Sintaks
PCCERT_CONTEXT CertGetSubjectCertificateFromStore(
[in] HCERTSTORE hCertStore,
[in] DWORD dwCertEncodingType,
[in] PCERT_INFO pCertId
);
Parameter
[in] hCertStore
Handel penyimpanan sertifikat.
[in] dwCertEncodingType
Jenis pengodean yang digunakan. Selalu dapat diterima untuk menentukan jenis pengodean sertifikat dan pesan dengan menggabungkannya dengan operasi bitwise-OR seperti yang ditunjukkan dalam contoh berikut:
X509_ASN_ENCODING | PKCS_7_ASN_ENCODING Jenis pengodean yang ditentukan saat ini adalah:
- X509_ASN_ENCODING
- PKCS_7_ASN_ENCODING
[in] pCertId
Penunjuk ke struktur CERT_INFO . Hanya anggota Penerbit dan SerialNumber yang digunakan.
Mengembalikan nilai
Jika fungsi berhasil, fungsi mengembalikan penunjuk ke CERT_CONTEXT baca-saja. CERT_CONTEXT harus dibebaskan dengan memanggil CertFreeCertificateContext.
Sertifikat yang dikembalikan mungkin tidak valid. Biasanya, sertifikat diverifikasi saat mendapatkan sertifikat penerbitnya (CertGetIssuerCertificateFromStore).
Untuk informasi kesalahan yang diperluas, hubungi GetLastError. Salah satu kemungkinan kode kesalahan adalah sebagai berikut.
Menampilkan kode | Deskripsi |
---|---|
|
Sertifikat subjek tidak ditemukan di penyimpanan. |
Keterangan
CertDuplicateCertificateContext dapat dipanggil untuk membuat sertifikat duplikat.
Contoh
Contoh berikut menunjukkan pengambilan konteks sertifikat subjek, yang diidentifikasi secara unik oleh penerbit dan nomor serinya, dari penyimpanan sertifikat. Untuk contoh yang menyertakan konteks lengkap untuk contoh ini, lihat Contoh Program C: Penandatanganan, Pengodean, Pendekodean, dan Verifikasi Pesan.
#include <windows.h>
#include <stdio.h>
#include <Wincrypt.h>
#define MY_ENCODING_TYPE (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)
void main()
{
//-------------------------------------------------------------------
// Declare and initialize variables.
HCERTSTORE hStoreHandle; // certificate store handle
HCRYPTMSG hMsg;
PCCERT_CONTEXT pSignerCertContext;
DWORD cbSignerCertInfo;
PCERT_INFO pSignerCertInfo;
//-------------------------------------------------------------------
// This code is based on hMsg being a signed, encoded message
// read from a file or otherwise acquired. For detailed code, see
// "Example C Program: Signing, Encoding, Decoding, and Verifying a
// Message."
//-------------------------------------------------------------------
// Retrieve the signer CERT_INFO from the message by first getting
// the size of the memory required for the certificate.
if(CryptMsgGetParam(
hMsg, // handle to the message
CMSG_SIGNER_CERT_INFO_PARAM, // parameter type
0, // index
NULL,
&cbSignerCertInfo)) // size of the returned information
{
printf("%d bytes needed for the buffer.\n", cbSignerCertInfo);
}
else
{
printf("Verify SIGNER_CERT_INFO #1 failed\n");
exit(1);
}
//-------------------------------------------------------------------
// Allocate memory.
pSignerCertInfo = (PCERT_INFO) malloc(cbSignerCertInfo);
if (!pSignerCertInfo)
{
printf("Verify memory allocation failed.\n");
exit(1);
}
//-------------------------------------------------------------------
// Get the message certificate information (CERT_INFO
// structure).
if(!(CryptMsgGetParam(
hMsg, // handle to the message
CMSG_SIGNER_CERT_INFO_PARAM, // parameter type
0, // index
pSignerCertInfo, // address for returned information
&cbSignerCertInfo))) // size of the returned information
{
printf("Verify SIGNER_CERT_INFO #2 failed.\n");
exit(1);
}
//-------------------------------------------------------------------
// Open a certificate store in memory using CERT_STORE_PROV_MSG,
// which initializes it with the certificates from the message.
hStoreHandle = CertOpenStore(
CERT_STORE_PROV_MSG, // store provider type
MY_ENCODING_TYPE, // encoding type
NULL, // cryptographic provider
// use NULL for the default
0, // flags
hMsg)); // handle to the message
if (hStoreHandle)
{
printf("The certificate store to be used for message\n ");
printf(" verification has been opened. \n");
}
else
{
printf("Verify open store failed.\n");
exit(1);
}
//-------------------------------------------------------------------
// Find the signer's certificate in the store.
pSignerCertContext = CertGetSubjectCertificateFromStore(
hStoreHandle, // handle to store
MY_ENCODING_TYPE, // encoding type
pSignerCertInfo)); // pointer to retrieved CERT_CONTEXT
if(pSignerCertContext)
{
//-------------------------------------------------------------------
// Use the certificate retrieved from the message. For some
// processing that can be done with the certificate, see the full
// program.
}
//-------------------------------------------------------------------
// Clean up.
if(pSignerCertInfo)
free(pSignerCertInfo);
if(pSignerCertContext)
CertFreeCertificateContext(pSignerCertContext);
if(hStoreHandle)
CertCloseStore(hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG);
if(hMsg)
CryptMsgClose(hMsg);
}
Persyaratan
Persyaratan | Nilai |
---|---|
Klien minimum yang didukung | Windows XP [aplikasi desktop | Aplikasi UWP] |
Server minimum yang didukung | Windows Server 2003 [aplikasi desktop | Aplikasi UWP] |
Target Platform | Windows |
Header | wincrypt.h |
Pustaka | Crypt32.lib |
DLL | Crypt32.dll |
Lihat juga
CertDuplicateCertificateContext
Saran dan Komentar
https://aka.ms/ContentUserFeedback.
Segera hadir: Sepanjang tahun 2024 kami akan menghentikan penggunaan GitHub Issues sebagai mekanisme umpan balik untuk konten dan menggantinya dengan sistem umpan balik baru. Untuk mengetahui informasi selengkapnya, lihat:Kirim dan lihat umpan balik untuk