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 Corporation. 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 e-mail
// 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 2000 Professional |
| Minimum supported server | Windows 2000 Server |
| Header | Wincrypt.h |
| Library | Crypt32.lib |
| DLL | Crypt32.dll |
See Also
- Certificate Functions
- CertAddSerializedElementToStore
Send comments about this topic to Microsoft
Build date: 11/16/2009