CertGetSubjectCertificateFromStore 函数 (wincrypt.h)

CertGetSubjectCertificateFromStore 函数从证书存储返回由其颁发者和序列号唯一标识的使用者证书上下文。

语法

PCCERT_CONTEXT CertGetSubjectCertificateFromStore(
  [in] HCERTSTORE hCertStore,
  [in] DWORD      dwCertEncodingType,
  [in] PCERT_INFO pCertId
);

参数

[in] hCertStore

证书存储的句柄。

[in] dwCertEncodingType

使用的编码类型。 始终可以接受将证书和 消息编码类型 与按位 OR 操作组合在一起,如以下示例所示:

X509_ASN_ENCODING |PKCS_7_ASN_ENCODING当前定义的编码类型为:

  • X509_ASN_ENCODING
  • PKCS_7_ASN_ENCODING

[in] pCertId

指向 CERT_INFO 结构的指针。 仅使用 IssuerSerialNumber 成员。

返回值

如果函数成功,该函数将返回指向只读 CERT_CONTEXT的指针。 必须通过调用 CertFreeCertificateContext 来释放CERT_CONTEXT

返回的证书可能无效。 通常,在将颁发者证书 (CertGetIssuerCertificateFromStore) 进行验证。

有关扩展的错误信息,请调用 GetLastError。 下面是一个可能的错误代码。

返回代码 说明
CRYPT_E_NOT_FOUND
在存储中找不到使用者证书。

注解

可以调用 CertDuplicateCertificateContext 来生成重复的证书。

示例

以下示例演示如何从证书存储中检索使用者的证书上下文,该上下文由其颁发者和序列号唯一标识。 有关包含此示例的完整上下文的示例,请参阅 示例 C 程序:签名、编码、解码和验证消息


#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);

}

要求

要求
最低受支持的客户端 Windows XP [桌面应用 | UWP 应用]
最低受支持的服务器 Windows Server 2003 [桌面应用 | UWP 应用]
目标平台 Windows
标头 wincrypt.h
Library Crypt32.lib
DLL Crypt32.dll

另请参阅

CertDuplicateCertificateContext

CertFreeCertificateContext

CertGetIssuerCertificateFromStore

证书函数