CertCreateCertificateCoNtext 函式 (wincrypt.h)

CertCreateCertificateCoNtext函式會從編碼的憑證建立憑證內容。 建立的內容不會保存到憑證存放區。 函式會在建立的內容中建立編碼憑證的複本。

語法

PCCERT_CONTEXT CertCreateCertificateContext(
  [in] DWORD      dwCertEncodingType,
  [in] const BYTE *pbCertEncoded,
  [in] DWORD      cbCertEncoded
);

參數

[in] dwCertEncodingType

指定使用的編碼類型。 將憑證和 訊息編碼類型 與位OR 作業結合在一起,一律可以接受,如下列範例所示:

X509_ASN_ENCODING |PKCS_7_ASN_ENCODING目前定義的編碼類型如下:

  • X509_ASN_ENCODING
  • PKCS_7_ASN_ENCODING

[in] pbCertEncoded

緩衝區的指標,其中包含要從中建立內容的編碼憑證。

[in] cbCertEncoded

pbCertEncoded緩衝區的大小,以位元組為單位。

傳回值

如果函式成功,函式會傳回唯讀 CERT_CONTEXT的指標。 當您完成使用憑證內容時,請呼叫 CertFreeCertificateCoNtext 函式來釋放它。

如果函式無法解碼並建立 憑證內容,則會傳回 Null。 如需擴充錯誤資訊,請呼叫 GetLastError。 以下是一些可能的錯誤碼。

傳回碼 描述
E_INVALIDARG
指定不正確憑證編碼類型。 目前僅支援X509_ASN_ENCODING類型。
 

如果函式失敗, GetLastError 可能會傳回 抽象語法標記法一 (ASN.1) 編碼/解碼錯誤。 如需這些錯誤的相關資訊,請參閱 ASN.1 編碼/解碼傳回值

備註

CERT_CONTEXT必須透過呼叫CertFreeCertificateCoNtext來釋出。 您可以呼叫 CertDuplicateCertificateCoNtext 來建立重複專案。 您可以呼叫 CertSetCertificateCoNtextPropertyCertGetCertificateCoNtextProperty 來儲存和讀取憑證的屬性。

範例

下列範例示範如何從編碼憑證建立憑證內容。 建立的內容不會放在憑證存放區中。 如需使用此函式的另一個範例,請參閱 範例 C 程式:憑證存放區作業

#include <windows.h>
#include <stdio.h>
#include <Wincrypt.h>

#define MY_ENCODING_TYPE  (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)

void main()
{
	PCCERT_CONTEXT  pCertContext = NULL; 

	//------------------------------------------------------------------ 
	//  Create a new certificate from the encoded part of
	//  an available certificate. pDesiredCert is a previously
	//  assigned PCCERT_CONTEXT variable.
	if(pCertContext = CertCreateCertificateContext(
		MY_ENCODING_TYPE,              // The encoding type
		pDesiredCert->pbCertEncoded,   // The encoded data from
									   // the certificate retrieved
		pDesiredCert->cbCertEncoded))  // The length of the encoded data
	{
		printf("A new certificate has been created.\n");
	 
		// Use the certificate context as needed.
		// ...

		// When finished, free the certificate context.
		CertFreeCertificateContext(pCertContext);
	}
	else
	{
		printf("A new certificate could not be created.\n");
		exit(1);
	}
}

規格需求

   
最低支援的用戶端 Windows XP [傳統型應用程式 |UWP 應用程式]
最低支援的伺服器 Windows Server 2003 [傳統型應用程式 |UWP 應用程式]
目標平台 Windows
標頭 wincrypt.h
程式庫 Crypt32.lib
Dll Crypt32.dll

另請參閱

CERT_CONTEXT

CertCreateCRLCoNtext

CertCreateCTLCoNtext

CertDuplicateCertificateCoNtext

CertFreeCertificateCoNtext

CertGetCertificateCoNtextProperty

CertSetCertificateCoNtextProperty

憑證函數