Procedure for Encoding and Decoding Messages
The procedure for encoding a general message is as follows.
To encode a message
- Initialize the appropriate data structures for the desired data type.
- 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.1135220.127.116.11" for szOID_RSA_signedData). If pszInnerContentObjID is NULL, the inner content type is assumed not to have been previously encoded and is processed appropriately.
- 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).
- 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.
- 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
- Determine the length needed for the buffer to hold the encoded data using CryptMsgCalculateEncodedLength.
- 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.
- Call CryptMsgUpdate once. This causes the appropriate actions to be taken on the message, depending on the message type.
- For additional processing of the message, such as additional decryption or signature verification, call CryptMsgControl, passing the desired action in dwCtrlType.
- 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.
- 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.