Expand Minimize
EN
Deze inhoud is niet beschikbaar in uw taal, maar wel in het Engels.

CertSerializeCertificateStoreElement function

The CertSerializeCertificateStoreElement function serializes a certificate context's encoded certificate and its encoded properties. The result can be persisted to storage so that the certificate and properties can be retrieved at a later time.

Syntax


BOOL WINAPI CertSerializeCertificateStoreElement(
  _In_     PCCERT_CONTEXT pCertContext,
  _In_     DWORD dwFlags,
  _Out_    BYTE *pbElement,
  _Inout_  DWORD *pcbElement
);

Parameters

pCertContext [in]

A pointer to the CERT_CONTEXT to be serialized.

dwFlags [in]

Reserved for future use and must be zero.

pbElement [out]

A pointer to a buffer that receives the serialized output, including the encoded certificate and possibly its properties.

This parameter can be NULL to set the size of this information for memory allocation purposes. For more information, see Retrieving Data of Unknown Length.

pcbElement [in, out]

A pointer to a DWORD value specifying the size, in bytes, of the buffer pointed to by the pbElement parameter. When the function returns, DWORD value contains the number of bytes stored in the buffer.

Note  When processing the data returned in the buffer, applications must use the actual size of the data returned. The actual size can be slightly smaller than the size of the buffer specified on input. (On input, buffer sizes are usually specified large enough to ensure that the largest possible output data fits in the buffer.) On output, the variable pointed to by this parameter is updated to reflect the actual size of the data copied to the buffer.

Return value

If the function succeeds, the return value is TRUE.

If the function fails, the return value is FALSE. For extended error information, call GetLastError.

Examples

The following example serializes a certificate context's encoded certificate and the encoded representation of the certificate's properties.



#include <stdio.h>
#include <windows.h>
#include <Wincrypt.h>
#pragma comment(lib, "crypt32.lib")

void main()
{
    //---------------------------------------------------------------
    // Copyright (C) Microsoft.  All rights reserved.
    // Declare and initialize variables.
    HCERTSTORE         hCertStore = NULL;
    PCCERT_CONTEXT     pCertContext = NULL;
    BYTE*              pbElement;
    DWORD              cbElement;

    //---------------------------------------------------------------
    // Open a system certificate store.
    if(hCertStore = CertOpenSystemStore(
        0,
        "CA"))
    {
      printf("The CA system store is open. Continue.\n");
    }
    else
    {
      printf("The system store did not open.\n");
      exit(1);
    }

    //----------------------------------------------------------------
    // Retrieve a certificate from the store.
    // CertFindCertificateInStore could be used here to find
    // a certificate with a specific property.
    if(pCertContext=CertEnumCertificatesInStore(
        hCertStore,
        pCertContext))
    {
         printf("A certificate is available. Continue.\n");
    }
    else
    {
         printf("No certificate available. The store may "
			 "be empty.\n");
         CertCloseStore(hCertStore,0);
         exit(1);
    }

    //---------------------------------------------------------------
    // Find out how much memory to allocate for the serialized 
	// element.
    if(CertSerializeCertificateStoreElement(
        pCertContext,      // The existing certificate.
        0,                 // Accept default for dwFlags, 
        NULL,              // NULL for the first function call.
        &cbElement))       // Address where the length of the 
                           // serialized element will be placed.
    {
         printf("The length of the serialized string is %d.\n",
			 cbElement);
    }
    else
    {
         printf("Finding the length of the serialized element "
			 "failed.\n");
         CertCloseStore(hCertStore,0);
         exit(1);
    }

    //---------------------------------------------------------------
    // Allocate memory for the serialized element.
    if(pbElement = (BYTE*)malloc(cbElement))
    {
         printf("Memory has been allocated. Continue.\n");
    }
    else
    {
         printf("The allocation of memory failed.\n");
         CertCloseStore(hCertStore,0);
         exit(1);
    }

    //---------------------------------------------------------------
    // Create the serialized element from the certificate context.
    if(CertSerializeCertificateStoreElement(
        pCertContext,        // The certificate context source for  
                             // the serialized element.
        0,                   // dwFlags. Accept the default.
        pbElement,           // A pointer to where the new element 
                             // will be stored.
        &cbElement))         // The length of the serialized element,
    {
         printf("The encoded element has been serialized. \n");
    }
    else
    {
         printf("The element could not be serialized.\n");
         CertCloseStore(hCertStore,0);
         exit(1);
    }

    //---------------------------------------------------------------
    // pbElement could be written to a file or be sent by email
    // to another user (not shown). 
    // ...

    //---------------------------------------------------------------
    //  When all processing is done, clean up.
    free(pbElement);
    CertCloseStore(hCertStore,0);
}


For another example that uses this function, see Example C Program: Serializing Certificates.

Requirements

Minimum supported client

Windows XP [desktop apps only]

Minimum supported server

Windows Server 2003 [desktop apps only]

Header

Wincrypt.h

Library

Crypt32.lib

DLL

Crypt32.dll

See also

Certificate Functions
CertAddSerializedElementToStore

 

 

Community-inhoud

Toevoegen
Weergeven:
© 2014 Microsoft