CertSaveStore function
The CertSaveStore function saves the certificate store to a file or to a memory BLOB.
Syntax
BOOL WINAPI CertSaveStore(
_In_ HCERTSTORE hCertStore,
_In_ DWORD dwMsgAndCertEncodingType,
_In_ DWORD dwSaveAs,
_In_ DWORD dwSaveTo,
_Inout_ void *pvSaveToPara,
_In_ DWORD dwFlags
);
Parameters
- hCertStore [in]
-
The handle of the certificate store to be saved.
- dwMsgAndCertEncodingType [in]
-
Specifies the certificate encoding type and message encoding type. Encoding is used only when dwSaveAs contains CERT_STORE_SAVE_AS_PKCS7. Otherwise, the dwMsgAndCertEncodingType parameter is not used.
This parameter can be a combination of one or more of the following values.
Value Meaning - PKCS_7_ASN_ENCODING
- 65536 (0x10000)
Specifies PKCS 7 message encoding.
- X509_ASN_ENCODING
- 1 (0x1)
Specifies X.509 certificate encoding.
- dwSaveAs [in]
-
Specifies how to save the certificate store.
This parameter can be one of the following values.
Value Meaning - CERT_STORE_SAVE_AS_PKCS7
- 2
The certificate store can be saved as a PKCS #7 signed message that does not include additional properties. The dwEncodingType parameter specifies the message encoding type.
- CERT_STORE_SAVE_AS_STORE
- 1
The certificate store can be saved as a serialized store containing properties in addition to encoded certificates, certificate revocation lists (CRLs), and certificate trust lists (CTLs). The dwEncodingType parameter is ignored.
Note The CERT_KEY_CONTEXT_PROP_ID property and the related CERT_KEY_PROV_HANDLE_PROP_ID and CERT_KEY_SPEC_PROP_ID values are not saved to a serialized store. - dwSaveTo [in]
-
Specifies where and how to save the certificate store. The contents of this parameter determines the format of the pvSaveToPara parameter.
This parameter can be one of the following values.
Value Meaning - CERT_STORE_SAVE_TO_FILE
- 1
The function saves the certificate store to a file. The pvSaveToPara parameter contains a handle to a file previously obtained by using the CreateFile function. The file must be opened with write permission. After a successful save operation, the file pointer is positioned after the last write operation.
- CERT_STORE_SAVE_TO_FILENAME
- 4
The function saves the certificate store to a file. The pvSaveToPara parameter contains a pointer to a null-terminated Unicode string that contains the path and file name of the file to save to. The function opens the file, saves to it, and closes it.
- CERT_STORE_SAVE_TO_FILENAME_A
- 3
The function saves the certificate store to a file. The pvSaveToPara parameter contains a pointer to a null-terminated ANSI string that contains the path and file name of the file to save to. The function opens the file, saves to it, and closes it.
- CERT_STORE_SAVE_TO_FILENAME_W
- 4
The function saves the certificate store to a file. The pvSaveToPara parameter contains a pointer to a null-terminated Unicode string that contains the path and file name of the file to save to. The function opens the file, saves to it, and closes it.
- CERT_STORE_SAVE_TO_MEMORY
- 2
The function saves the certificate store to a memory BLOB. The pvSaveToPara parameter contains a pointer to a CERT_BLOB structure. Before use, the CERT_BLOB's pbData and cbData members must be initialized. Upon return, cbData is updated with the actual length. For a length-only calculation, pbData must be set to NULL. If pbData is non-NULL and cbData is not large enough, the function returns zero with a last error code of ERROR_MORE_DATA.
- pvSaveToPara [in, out]
-
A pointer that represents where the store should be saved to. The contents of this parameter depends on the value of the dwSaveTo parameter.
- dwFlags [in]
-
This parameter is reserved for future use and must be set to zero.
Return value
If the function succeeds, the function returns nonzero.
If the function fails, it returns zero. For extended error information, call GetLastError.
Note that CreateFile or WriteFile errors can be propagated to this function. One possible error code is CRYPT_E_FILE_ERROR which indicates that an error occurred while writing to the file.
Examples
The following example shows saving a certificate store and its contents to an open file. The example uses the CreateMyDACL example function, defined in the Creating a DACL topic, to ensure the open file is created with a proper DACL.
For other examples that use this function, see Example C Program: Collection and Sibling Certificate Store Operations, Example C Program: Certificate Store Operations, and Example C Program: Setting and Getting Certificate Store Properties.
//-------------------------------------------------------------------- // Declare and initialize variables. HCERTSTORE hMemoryStore = NULL; // A memory store handle HANDLE hStoreFileHandle = NULL; // Output file handle LPCSTR pszFileName = "TestStor.sto"; // Output file name SECURITY_ATTRIBUTES sa; // For DACL sa.nLength = sizeof(SECURITY_ATTRIBUTES); sa.bInheritHandle = FALSE; // Call function to set the DACL. The DACL // is set in the SECURITY_ATTRIBUTES // lpSecurityDescriptor member. if (!CreateMyDACL(&sa)) { // Error encountered; generate message and exit. printf("Failed CreateMyDACL\n"); exit(1); } //------------------------------------------------------------------- // Open the output file. After the store is open, // output can be written to the file before a certificate // store is saved to it. if(hStoreFileHandle = CreateFile( pszFileName, // File path GENERIC_WRITE, // Access mode 0, // Share mode &sa, // Security CREATE_ALWAYS, // How to create the file FILE_ATTRIBUTE_NORMAL, // File attributes NULL)) // File template { printf("Created a new file on disk. \n"); } else { printf("Could not create a file on disk.\n"); exit(1); } // Free the memory allocated for the SECURITY_DESCRIPTOR. if (NULL != LocalFree(sa.lpSecurityDescriptor)) { // Error encountered; generate message and exit. printf("Failed LocalFree\n"); exit(1); } //------------------------------------------------------------------- // Open a memory certificate store. After the store is open, // certificates can be added to it. if(hMemoryStore = CertOpenStore( CERT_STORE_PROV_MEMORY, // A memory store 0, // Encoding type // Not used with a memory store NULL, // Use the default provider 0, // No flags NULL)) // Not needed { printf("Opened a memory store. \n"); } else { printf( "Error opening a memory store.\n"); exit(1); } //------------------------------------------------------------------- // Save the memory store and its certificates to the output file. if( CertSaveStore( hMemoryStore, // Store handle 0, // Encoding type not needed here CERT_STORE_SAVE_AS_STORE, CERT_STORE_SAVE_TO_FILE, hStoreFileHandle, // The handle of an open disk file 0)) // dwFlags: No flags are needed here. { printf("Saved the memory store to disk. \n"); } else { printf("Could not save the memory store to disk.\n"); exit(1); } //------------------------------------------------------------------- // When the memory store and the file are no longer needed, // close them. if(hMemoryStore) { if (!CertCloseStore( hMemoryStore, CERT_CLOSE_STORE_CHECK_FLAG)) printf("Failed call to CertCloseStore.\n"); } if(hStoreFileHandle) { if (!CloseHandle(hStoreFileHandle)) printf("Failed call to CloseHandle.\n"); }
Requirements
|
Minimum supported client |
Windows XP [desktop apps | Windows Store apps] |
|---|---|
|
Minimum supported server |
Windows Server 2003 [desktop apps | Windows Store apps] |
|
Header |
|
|
Library |
|
|
DLL |
|
See also