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 구조체에 대한 포인터입니다. 발급자SerialNumber 멤버만 사용됩니다.

반환 값

함수가 성공하면 함수는 읽기 전용 CERT_CONTEXT 대한 포인터를 반환합니다. CERT_CONTEXT CertFreeCertificateContext를 호출하여 해제해야 합니다.

반환된 인증서가 잘못되었을 수 있습니다. 일반적으로 발급자 인증서를 받을 때 확인됩니다(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
라이브러리 Crypt32.lib
DLL Crypt32.dll

추가 정보

CertDuplicateCertificateContext

CertFreeCertificateContext

CertGetIssuerCertificateFromStore

인증서 함수