[This is preliminary documentation and subject to change.]
The CryptEncodeObjectEx function encodes a structure of type lpszStructType. CryptEncodeObjectEx offers a significant performance improvement over CryptEncodeObject by supporting the memory allocation with the CRYPT_ENCODE_ALLOC_FLAG.
#include <wincrypt.h>
BOOL WINAPI CryptEncodeObjectEx(
DWORD dwCertEncodingType, // in
LPCSTR lpszStructType, // in
const void *pvStructInfo, // in
DWORD dwFlags, // in
PCRYPT_ENCODE_PARA pEncodePara, // in, optional
void *pvEncoded, // out
DWORD *pcbEncoded // in, out
);
The following encoding types can be combined with a bitwise OR operation:
Certificate Encoding type | Value |
---|---|
CRYPT_ASN_ENCODING | 0x00000001 |
X509_ASN_ENCODING | 0x00000001 |
PKCS_7_ASN_ENCODING | 0x00010000 |
For more details, see the table in CryptEncodeObject/CryptDecodeObject Functions that relates object identifier strings and predefined constants to their corresponding data structures.
The following flags can be combined with a bitwise OR operation into CryptEncodeObjectEx dwFlags.
If pEncodePara or pEncodePara->pfnAlloc is NULL, then, LocalAlloc is called for the allocation and LocalFree must be called to free the memory.
If pEncodePara and pEncodePara->pfnAlloc are non NULL, then, pEncodePara->pfnAlloc is called for the allocation and the function pointed to by pEncodePara->pfnFree must be called to free the memory.
This parameter can be NULL to set the size of this information for memory allocation purposes. For more information, see Common In/Out Parameter Conventions.
When the CRYPT_ENCODE_ALLOC_FLAG is set, pvEncoded is the address of a pointer to the buffer that will be updated. Because memory is allocated and the pointer is stored at *pvEncoded, pvEncoded must always be non-NULL.
When the CRYPT_ENCODE_ALLOC_FLAG is set, pcbEncoded is the address of a pointer to the DWORD that will be updated.
Note When processing the data returned in the buffer, applications need to use the actual size of the data returned. The actual size may 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 will fit 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.
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_BAD_ENCODE | An error was encountered while encoding. |
CRYPT_E_OSS_ERROR | ASN.1 encoding error. Note, to get the OSS error subtract the value in CRYPT_E_OSS_ERROR from the returned error value and see asn1code.h for details on the error. |
ERROR_FILE_NOT_FOUND | An encoding function could not be found for the specified dwCertEncodingType and lpszStructType. |
ERROR_MORE_DATA | If the buffer specified by the pbEncoded parameter is not large enough to hold the returned data, the function sets the ERROR_MORE_DATA code, and stores the required buffer size, in bytes, in the variable pointed to by pcbEncoded. |
CryptEncodeObjectEx first looks for an installable Ex encode function. If no Ex encode function is found, then the old non-Ex installable function is located.
// EXAMPLE CODE FOR USING CryptEncodeObjectEx().
char *Cert_Sub_Name ="Elizabeth Jackson";
#define MY_ENCODING_TYPE (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)
// Initialize a single RDN struture.
CERT_RDN_ATTR rgNameAttr = {
"2.5.4.3", // pszObjId
CERT_RDN_PRINTABLE_STRING, // dwValueType
strlen(Cert_Sub_Name), // value.cbData
(BYTE *)Cert_Sub_Name}; // value.pbData
// Initialize a structure that includes an array of RDN structures.
CERT_RDN rgRDN[] = {
1, // rgRDN.cRDNAttr
&rgNameAttr}; // rgRDN.rgRDNAttr
// Initialize a CERT_NAME_INFO structure that includes a CERT_RND.
CERT_NAME_INFO CertName = {
1, // CertName. cRDN = number of elements
// in the CERT_RND's array.
rgRDN}; // CertName.rgRDN
VOID *pvEncoded;
DWORD cbEncoded;
BOOL fResult;
// CryptEncodeObjectEx function call.
fResult= CryptEncodeObjectEx(
MY_ENCODING_TYPE, // in- Encoding/decoding type.
X509_NAME, // in- Struct type
&CertName, // in- Address of CERT_NAME_INFO
// struct.
CRYPT_ENCODE_ALLOC_FLAG,
// in- dwFlags
NULL, // in, optional- EncodePara set to NULL.
// to use default memory allocation of
// LocalAlloc and LocalFree.
(void*) &pvEncoded,// out- address of a pointer to be
// updated.
&cbEncoded); // in, out address of a DWORD to be
// updated.
if (!fResult) { // FALSE returned- function failed.
printf("The call to CryptEncodeObjectEx failed.\n\n");
}
else { // TRUE returned- function successful.
printf("The call to CryptEncodeObjectEx succeeded.\n\n");
}
// Free the memory.
LocalFree(pvEncoded);
printf("The function ran to completion.\n\n");
Windows NT: Requires version 5.0 or later.
Windows: Unsupported.
Windows CE: Unsupported.
Header: Declared in wincrypt.h.
Import Library: Use crypt32.lib.
CryptEncodeObject, CryptDecodeObject, CryptDecodeObjectEx