Procedure for Encoding and Decoding Messages

The procedure for encoding a general message is as follows.

To encode a message

  1. Initialize the appropriate data structures for the desired data type.
  2. Call CryptMsgOpenToEncode, passing the necessary arguments. When calling CryptMsgOpenToEncode, if the data that is to be provided to CryptMsgUpdate has already been message-encoded, pass the appropriate object identifier in pszInnerContentObjID (for example, "1.2.840.113549.1.7.2" for szOID_RSA_signedData). If pszInnerContentObjID is NULL, the inner content type is assumed not to have been previously encoded and is processed appropriately.
  3. Call CryptMsgUpdate as many times as necessary to complete the message. On the last call, set the fFinal parameter to TRUE. (For details, see CryptMsgUpdate).
  4. Call CryptMsgGetParam to get a pointer to the desired parameters, such as the content. For encoding simple, general data, use CMSG_CONTENT_PARAM for the dwParamtype.
  5. Close the message by calling CryptMsgClose.

This procedure results in an encoded message of a type specified in the function calls.

The procedure for decoding a general message is as follows.

To decode a message

  1. Determine the length needed for the buffer to hold the encoded data using CryptMsgCalculateEncodedLength.
  2. Call CryptMsgOpenToDecode, passing the necessary arguments. To maintain compatibility with Internet Explorer version 3.0, the dwMsgType parameter is provided. Signed data created in Internet Explorer 3.0 does not contain header information. Therefore, if such a message is extracted from file signatures, the message type must be passed into the function. If zero is passed into the dwMsgType parameter, the function will read the message type from the header on the message. If the header is missing, the function call will fail. If successful, a handle to the opened message is returned.
  3. Call CryptMsgUpdate once. This causes the appropriate actions to be taken on the message, depending on the message type.
  4. For additional processing of the message, such as additional decryption or signature verification, call CryptMsgControl, passing the desired action in dwCtrlType.
  5. Call CryptMsgGetParam to get a pointer to the desired parameters, such as the content. To decode simple general data, use CMSG_CONTENT_PARAM for the dwParamtype parameter.
  6. Call CryptMsgClose to close the message.

For an example that implements these steps, see Example C Program: Encoding and Decoding Data. For procedures and an example demonstrating the process of encoding, decoding, and verifying the signature of a signed message, see Example C Program: Signing, Encoding, Decoding, and Verifying a Message.