Funzione CertCreateCertificateContext (wincrypt.h)

La funzione CertCreateCertificateContext crea un contesto di certificato da un certificato codificato. Il contesto creato non viene salvato in modo permanente in un archivio certificati. La funzione crea una copia del certificato codificato all'interno del contesto creato.

Sintassi

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

Parametri

[in] dwCertEncodingType

Specifica il tipo di codifica utilizzata. È sempre accettabile specificare sia il certificato che i tipi di codifica dei messaggi combinandoli con un'operazione OR bit per bit, come illustrato nell'esempio seguente:

X509_ASN_ENCODING | PKCS_7_ASN_ENCODING I tipi di codifica attualmente definiti sono:

  • X509_ASN_ENCODING
  • PKCS_7_ASN_ENCODING

[in] pbCertEncoded

Puntatore a un buffer contenente il certificato codificato da cui creare il contesto.

[in] cbCertEncoded

Dimensione, in byte, del buffer pbCertEncoded .

Valore restituito

Se la funzione ha esito positivo, la funzione restituisce un puntatore a un CERT_CONTEXT di sola lettura. Al termine dell'uso del contesto del certificato, liberarlo chiamando la funzione CertFreeCertificateContext .

Se la funzione non è in grado di decodificare e creare il contesto del certificato, restituisce NULL. Per informazioni sugli errori estesi, chiamare GetLastError. Di seguito sono riportati alcuni possibili codici di errore.

Codice restituito Descrizione
E_INVALIDARG
È stato specificato un tipo di codifica del certificato non valido. Attualmente è supportato solo il tipo di X509_ASN_ENCODING.
 

Se la funzione ha esito negativo, GetLastError può restituire un errore di codifica/decodifica ASN.1 ( Abstract Syntax Notation One ). Per informazioni su questi errori, vedere Codifica ASN.1/Decodifica dei valori restituiti.

Commenti

Il CERT_CONTEXT deve essere liberato chiamando CertFreeCertificateContext. CertDuplicateCertificateContext può essere chiamato per creare un duplicato. È possibile chiamare CertSetCertificateContextProperty e CertGetCertificateContextProperty per archiviare e leggere le proprietà per il certificato.

Esempio

L'esempio seguente illustra la creazione di un contesto di certificato da un certificato codificato. Il contesto creato non viene inserito in un archivio certificati. Per un altro esempio che usa questa funzione, vedere Esempio di programma C: operazioni dell'archivio certificati.

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

Requisiti

   
Client minimo supportato Windows XP [app desktop | App UWP]
Server minimo supportato Windows Server 2003 [app desktop | App UWP]
Piattaforma di destinazione Windows
Intestazione wincrypt.h
Libreria Crypt32.lib
DLL Crypt32.dll

Vedi anche

CERT_CONTEXT

CertCreateCRLContext

CertCreateCTLContext

CertDuplicateCertificateContext

CertFreeCertificateContext

CertGetCertificateContextProperty

CertSetCertificateContextProperty

Funzioni per i certificati