CertAddSerializedElementToStore

The CertAddSerializedElementToStore function adds the serialized certificate, CRL, or CTL element to the store. The serialized element contains the encoded certificate, CRL, or CTL and its properties, such as, CERT_KEY_PROV_INFO_PROP_ID.

#include <wincrypt.h>
BOOL WINAPI CertAddSerializedElementToStore(
  HCERTSTORE hCertStore,             // in
  const BYTE *pbElement,             // in
  DWORD cbElement,                   // in
  DWORD dwAddDisposition,            // in
  DWORD dwFlags,                     // in
  DWORD dwContextTypeFlags,          // in
  DWORD *pdwContextType,             // out, optional
  const void **ppvContext            // out, optional
);
 

Parameters

hCertStore
Handle to the certificate store. If hCertStore is NULL, creates a certificate, CRL, or CTL context not residing in any store.
pbElement
Pointer to the certificate, CRL, or CTL element to be added to the certificate store.
cbElement
Size, in bytes, of the element to be added to the certificate store.
dwAddDisposition
Specifies the action to take if the certificate, CRL, or CTL already exists in the store. Currently defined disposition values are shown in the following table:
Disposition Value
CERT_STORE_ADD_NEW 0x00000001
CERT_STORE_ADD_USE_EXISTING 0x00000002
CERT_STORE_ADD_REPLACE_EXISTING 0x00000003
CERT_STORE_ADD_ALWAYS 0x00000004

For dwAddDisposition == CERT_STORE_ADD_NEW the operation fails if the certificate, CRL, or CTL already exists in the store. LastError is set to CRYPT_E_EXISTS.

For dwAddDisposition == CERT_STORE_ADD_USE_EXISTING, if the certificate, CRL, or CTL already exists, then it is used and if ppvContext is not NULL, the existing context is duplicated. It only copies the properties that don't already exist. The SHA1 and MD5 hash propoerties aren't copied.

For dwAddDisposition == CERT_STORE_ADD_REPLACE_EXISTING, if the certificate, CRL, or CTL already exists, then the existing certificate, CRL, or CTL context is deleted before creating and adding the new context.

For dwAddDisposition == CERT_STORE_ADD_ALWAYS, no check is made to see if the certificate, CRL, or CTL already exists. A new certificate, CRL, or CTL context is always created. This may lead to duplicates in the store.

CertGetCRLFromStore, or CertGetSubjectCertificateFromStore is called to determine if the element already exists in the store, if that check is necessary.

dwFlags
This parameter is not currently used. Zero should be passed for its value when this function is called.
dwContextTypeFlags
Specifies the set of allowable contexts. For example, to add either a certificate, CRL, or CTL, set dwContextTypeFlags to: CERT_STORE_CERTIFICATE_CONTEXT_FLAG | CERT_STORE_CRL_CONTEXT_FLAG.

Currently defined context type flags are shown in the following table:
Context type flag Value
CERT_STORE_ALL_CONTEXT_FLAG 0xFFFFFFFF
CERT_STORE_CERTIFICATE_CONTEXT_FLAG 0x00000001
CERT_STORE_CRL_CONTEXT_FLAG 0x00000002
CERT_STORE_CTL_CONTEXT_FLAG 0x00000004

pdwContextType
Pointer to the context type of the added serialized element. This is an optional parameter, and can be NULL, indicating that the caller does not want the context type.

Currently defined context types are shown in the following table:
Context type Value
CERT_STORE_CERTIFICATE_CONTEXT 0x00000001
CERT_STORE_CRL_CONTEXT 0x00000002
CERT_STORE_CTL_CONTEXT 0x00000003

ppvContext
Pointer to a pointer to the decoded certificate, CRL, or CTL context. This is an optional parameter, and can be NULL, indicating that the caller does not want the context of the added or existing certificate, CRL, or CTL. When finished, the context must be freed by using CertFreeCertificateContext for a certificate and CertFreeCRLContext for a CRL.

Return Values

TRUE if the function succeeded. FALSE if the function failed.

Call GetLastError to see the reason for any failures. This function has the following error codes:

Error code Description
CRYPT_E_EXISTS For a dwAddDisposition of CERT_STORE_ADD_NEW, the certificate, CRL, or CTL already exists in the store.
CRYPT_E_OSS_ERROR ASN.1 decoding error. Note, to get the OSS error subtract CRYPT_E_OSS_ERROR from the returned error and see asn1code.h for details on the error.
E_INVALIDARG Invalid add disposition specified by the dwAddDisposition argument.

Example

See CertSerializeCertificateStoreElement.

QuickInfo

  Windows NT: Requires version 4.0 SP3 or later. Available also in IE 3.02 and later.
  Windows: Requires Windows 95 OSR2 or later.
  Windows CE: Unsupported.
  Header: Declared in wincrypt.h.
  Import Library: Use crypt32.lib.

See Also

CertSerializeCertificateStoreElement, CertSerializeCRLStoreElement