CryptMsgCalculateEncodedLength

The CryptMsgCalculateEncodedLength function calculates the required length for an encoded cryptographic message given the message type, encoding parameters, and total length of the data to be updated. Note that the result might not be the exact length. However, it will always be greater than or equal to the actual length.

#include <wincrypt.h>
DWORD WINAPI CryptMsgCalculateEncodedLength(
  DWORD dwMsgEncodingType,        // in
  DWORD dwFlags,                  // in
  DWORD dwMsgType,                // in
  const void *pvMsgEncodeInfo,    // in
  LPSTR pszInnerContentObjID,     // in/optional
  DWORD cbData                    // in
);
 

Parameters

dwMsgEncodingType
The type of message encoding used. Note that it is always acceptable to specify both the certificate and message encoding types, by combining them with a bitwise OR operation, as shown in the following example:
CRYPT_ASN_ENCODING | PKCS_7_ASN_ENCODING
 

However, it is required only to specify the message encoding here. Currently defined encoding types are shown in the following table.
Encoding type Value
CRYPT_ASN_ENCODING 0x00000001
PKCS_7_ASN_ENCODING 0x00010000

dwFlags
The flag values. Currently defined flags are shown in the following table:
Flag Value
CMSG_BARE_CONTENT_FLAG 0x00000001
CMSG_DETACHED_FLAG 0x00000004
CMSG_CONTENTS_OCTETS_FLAG 0x00000010

CMSG_BARE_CONTENT_FLAG can be specified for a streamed message to indicate that the streamed output will not have an outer ContentInfo wrapper (as defined by PKCS #7). This makes it suitable to be streamed into an enclosing message.

The CMSG_DETACHED_FLAG indicates that there is detached data being supplied for the subsequent calls to CryptMsgUpdate.

The CMSG_CONTENTS_OCTETS_FLAG should be used when you are calculating the size of a DER encoding of a message which is going to be nested inside an enveloped message. This is particularly useful when streaming is being performed.

dwMsgType
The message type. Currently defined types are shown in the following table. Descriptions of the defined types can be found in CryptMsgOpenToEncode.
Message type Associated structures Value
CMSG_DATA Raw data (no associated structure) 1
CMSG_SIGNED CMSG_SIGNER_ENCODE_INFO, CMSG_SIGNED_ENCODE_INFO 2
CMSG_ENVELOPED CMSG_ENVELOPED_ENCODE_INFO 3
CMSG_SIGNED_AND_
ENVELOPED
Not implemented. 4
CMSG_HASHED CMSG_HASHED_ENCODE_INFO 5
CMSG_ENCRYPTED Not implemented. 6

pvMsgEncodeInfo
A pointer to the message content. The type depends on the type of the message. For the related structures, see pvMsgEncodeInfo under CryptMsgOpenToEncode.
pszInnerContentObjID
See the description under CryptMsgOpenToEncode.
cbData
The size, in bytes, of the content.

Return Values

Returns the required length for an encoded cryptographic message. Returns a zero if the function fails.

To retrieve extended error information, use the GetLastError function.

The following table lists the error codes most commonly returned by the GetLastError function.

Error code Description
CRYPT_E_INVALID_MSG_TYPE The message type is invalid.
CRYPT_E_UNKNOWN_ALGO The cryptographic algorithm is unknown.
E_INVALIDARG One or more arguments are invalid.

Example

See Signed Message Example Code.

See Enveloped Message Example 1.

See Hashed Message Example Code.

QuickInfo

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

See Also

CryptMsgOpenToEncode