2 * Copyright 2007 Juan Lang
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/debug.h"
25 #include "wine/exception.h"
26 #include "crypt32_private.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
30 /* Called when a message's ref count reaches zero. Free any message-specific
33 typedef void (*CryptMsgCloseFunc)(HCRYPTMSG msg);
35 typedef BOOL (*CryptMsgGetParamFunc)(HCRYPTMSG hCryptMsg, DWORD dwParamType,
36 DWORD dwIndex, void *pvData, DWORD *pcbData);
38 typedef BOOL (*CryptMsgUpdateFunc)(HCRYPTMSG hCryptMsg, const BYTE *pbData,
39 DWORD cbData, BOOL fFinal);
41 typedef enum _CryptMsgState {
47 typedef struct _CryptMsgBase
52 CMSG_STREAM_INFO stream_info;
54 CryptMsgCloseFunc close;
55 CryptMsgUpdateFunc update;
56 CryptMsgGetParamFunc get_param;
59 static inline void CryptMsgBase_Init(CryptMsgBase *msg, DWORD dwFlags,
60 PCMSG_STREAM_INFO pStreamInfo, CryptMsgCloseFunc close,
61 CryptMsgGetParamFunc get_param, CryptMsgUpdateFunc update)
64 msg->open_flags = dwFlags;
68 memcpy(&msg->stream_info, pStreamInfo, sizeof(msg->stream_info));
72 msg->streamed = FALSE;
73 memset(&msg->stream_info, 0, sizeof(msg->stream_info));
76 msg->get_param = get_param;
78 msg->state = MsgStateInit;
81 typedef struct _CDataEncodeMsg
84 DWORD bare_content_len;
88 static const BYTE empty_data_content[] = { 0x04,0x00 };
90 static void CDataEncodeMsg_Close(HCRYPTMSG hCryptMsg)
92 CDataEncodeMsg *msg = (CDataEncodeMsg *)hCryptMsg;
94 if (msg->bare_content != empty_data_content)
95 LocalFree(msg->bare_content);
98 static WINAPI BOOL CRYPT_EncodeContentLength(DWORD dwCertEncodingType,
99 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
100 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
102 const CDataEncodeMsg *msg = (const CDataEncodeMsg *)pvStructInfo;
106 /* Trick: report bytes needed based on total message length, even though
107 * the message isn't available yet. The caller will use the length
108 * reported here to encode its length.
110 CRYPT_EncodeLen(msg->base.stream_info.cbContent, NULL, &lenBytes);
112 *pcbEncoded = 1 + lenBytes + msg->base.stream_info.cbContent;
115 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
116 pcbEncoded, 1 + lenBytes)))
118 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
119 pbEncoded = *(BYTE **)pbEncoded;
120 *pbEncoded++ = ASN_OCTETSTRING;
121 CRYPT_EncodeLen(msg->base.stream_info.cbContent, pbEncoded,
128 static BOOL CRYPT_EncodeDataContentInfoHeader(CDataEncodeMsg *msg,
129 CRYPT_DATA_BLOB *header)
133 if (msg->base.streamed && msg->base.stream_info.cbContent == 0xffffffff)
135 FIXME("unimplemented for indefinite-length encoding\n");
137 header->pbData = NULL;
142 struct AsnConstructedItem constructed = { 0, msg,
143 CRYPT_EncodeContentLength };
144 struct AsnEncodeSequenceItem items[2] = {
145 { szOID_RSA_data, CRYPT_AsnEncodeOid, 0 },
146 { &constructed, CRYPT_AsnEncodeConstructed, 0 },
149 ret = CRYPT_AsnEncodeSequence(X509_ASN_ENCODING, items,
150 sizeof(items) / sizeof(items[0]), CRYPT_ENCODE_ALLOC_FLAG, NULL,
151 (LPBYTE)&header->pbData, &header->cbData);
154 /* Trick: subtract the content length from the reported length,
155 * as the actual content hasn't come yet.
157 header->cbData -= msg->base.stream_info.cbContent;
163 static BOOL CDataEncodeMsg_Update(HCRYPTMSG hCryptMsg, const BYTE *pbData,
164 DWORD cbData, BOOL fFinal)
166 CDataEncodeMsg *msg = (CDataEncodeMsg *)hCryptMsg;
169 if (msg->base.streamed)
173 if (msg->base.state != MsgStateUpdated)
175 CRYPT_DATA_BLOB header;
177 ret = CRYPT_EncodeDataContentInfoHeader(msg, &header);
180 ret = msg->base.stream_info.pfnStreamOutput(
181 msg->base.stream_info.pvArg, header.pbData, header.cbData,
183 LocalFree(header.pbData);
187 ret = msg->base.stream_info.pfnStreamOutput(
188 msg->base.stream_info.pvArg, (BYTE *)pbData, cbData,
192 if (msg->base.stream_info.cbContent == 0xffffffff)
194 BYTE indefinite_trailer[6] = { 0 };
196 ret = msg->base.stream_info.pfnStreamOutput(
197 msg->base.stream_info.pvArg, (BYTE *)pbData, cbData,
200 ret = msg->base.stream_info.pfnStreamOutput(
201 msg->base.stream_info.pvArg, indefinite_trailer,
202 sizeof(indefinite_trailer), TRUE);
205 ret = msg->base.stream_info.pfnStreamOutput(
206 msg->base.stream_info.pvArg, (BYTE *)pbData, cbData, TRUE);
211 SetLastError(STATUS_ACCESS_VIOLATION);
219 if (msg->base.open_flags & CMSG_DETACHED_FLAG)
220 SetLastError(E_INVALIDARG);
222 SetLastError(CRYPT_E_MSG_ERROR);
227 SetLastError(E_INVALIDARG);
230 CRYPT_DATA_BLOB blob = { cbData, (LPBYTE)pbData };
232 /* non-streamed data messages don't allow non-final updates,
233 * don't bother checking whether data already exist, they can't.
235 ret = CryptEncodeObjectEx(X509_ASN_ENCODING, X509_OCTET_STRING,
236 &blob, CRYPT_ENCODE_ALLOC_FLAG, NULL, &msg->bare_content,
237 &msg->bare_content_len);
244 static BOOL CRYPT_CopyParam(void *pvData, DWORD *pcbData, const BYTE *src,
251 else if (*pcbData < len)
254 SetLastError(ERROR_MORE_DATA);
260 memcpy(pvData, src, len);
265 static BOOL CDataEncodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
266 DWORD dwIndex, void *pvData, DWORD *pcbData)
268 CDataEncodeMsg *msg = (CDataEncodeMsg *)hCryptMsg;
273 case CMSG_CONTENT_PARAM:
274 if (msg->base.streamed)
275 SetLastError(E_INVALIDARG);
278 CRYPT_CONTENT_INFO info;
279 char rsa_data[] = "1.2.840.113549.1.7.1";
281 info.pszObjId = rsa_data;
282 info.Content.cbData = msg->bare_content_len;
283 info.Content.pbData = msg->bare_content;
284 ret = CryptEncodeObject(X509_ASN_ENCODING, PKCS_CONTENT_INFO, &info,
288 case CMSG_BARE_CONTENT_PARAM:
289 if (msg->base.streamed)
290 SetLastError(E_INVALIDARG);
292 ret = CRYPT_CopyParam(pvData, pcbData, msg->bare_content,
293 msg->bare_content_len);
296 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
301 static HCRYPTMSG CDataEncodeMsg_Open(DWORD dwFlags, const void *pvMsgEncodeInfo,
302 LPSTR pszInnerContentObjID, PCMSG_STREAM_INFO pStreamInfo)
308 SetLastError(E_INVALIDARG);
311 msg = CryptMemAlloc(sizeof(CDataEncodeMsg));
314 CryptMsgBase_Init((CryptMsgBase *)msg, dwFlags, pStreamInfo,
315 CDataEncodeMsg_Close, CDataEncodeMsg_GetParam, CDataEncodeMsg_Update);
316 msg->bare_content_len = sizeof(empty_data_content);
317 msg->bare_content = (LPBYTE)empty_data_content;
319 return (HCRYPTMSG)msg;
322 typedef struct _CHashEncodeMsg
327 CRYPT_DATA_BLOB data;
330 static void CHashEncodeMsg_Close(HCRYPTMSG hCryptMsg)
332 CHashEncodeMsg *msg = (CHashEncodeMsg *)hCryptMsg;
334 CryptMemFree(msg->data.pbData);
335 CryptDestroyHash(msg->hash);
336 if (msg->base.open_flags & CMSG_CRYPT_RELEASE_CONTEXT_FLAG)
337 CryptReleaseContext(msg->prov, 0);
340 static BOOL CRYPT_EncodePKCSDigestedData(CHashEncodeMsg *msg, void *pvData,
345 DWORD size = sizeof(algID);
347 ret = CryptGetHashParam(msg->hash, HP_ALGID, (BYTE *)&algID, &size, 0);
350 CRYPT_DIGESTED_DATA digestedData = { 0 };
351 char oid_rsa_data[] = szOID_RSA_data;
353 digestedData.version = CMSG_HASHED_DATA_PKCS_1_5_VERSION;
354 digestedData.DigestAlgorithm.pszObjId = (LPSTR)CertAlgIdToOID(algID);
355 /* FIXME: what about digestedData.DigestAlgorithm.Parameters? */
356 /* Quirk: OID is only encoded messages if an update has happened */
357 if (msg->base.state != MsgStateInit)
358 digestedData.ContentInfo.pszObjId = oid_rsa_data;
359 if (!(msg->base.open_flags & CMSG_DETACHED_FLAG) && msg->data.cbData)
361 ret = CRYPT_AsnEncodeOctets(0, NULL, &msg->data,
362 CRYPT_ENCODE_ALLOC_FLAG, NULL,
363 (LPBYTE)&digestedData.ContentInfo.Content.pbData,
364 &digestedData.ContentInfo.Content.cbData);
366 if (msg->base.state == MsgStateFinalized)
368 size = sizeof(DWORD);
369 ret = CryptGetHashParam(msg->hash, HP_HASHSIZE,
370 (LPBYTE)&digestedData.hash.cbData, &size, 0);
373 digestedData.hash.pbData = CryptMemAlloc(
374 digestedData.hash.cbData);
375 ret = CryptGetHashParam(msg->hash, HP_HASHVAL,
376 digestedData.hash.pbData, &digestedData.hash.cbData, 0);
380 ret = CRYPT_AsnEncodePKCSDigestedData(&digestedData, pvData,
382 CryptMemFree(digestedData.hash.pbData);
383 LocalFree(digestedData.ContentInfo.Content.pbData);
388 static BOOL CHashEncodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
389 DWORD dwIndex, void *pvData, DWORD *pcbData)
391 CHashEncodeMsg *msg = (CHashEncodeMsg *)hCryptMsg;
394 TRACE("(%p, %d, %d, %p, %p)\n", hCryptMsg, dwParamType, dwIndex,
399 case CMSG_BARE_CONTENT_PARAM:
400 if (msg->base.streamed)
401 SetLastError(E_INVALIDARG);
403 ret = CRYPT_EncodePKCSDigestedData(msg, pvData, pcbData);
405 case CMSG_CONTENT_PARAM:
407 CRYPT_CONTENT_INFO info;
409 ret = CryptMsgGetParam(hCryptMsg, CMSG_BARE_CONTENT_PARAM, 0, NULL,
410 &info.Content.cbData);
413 info.Content.pbData = CryptMemAlloc(info.Content.cbData);
414 if (info.Content.pbData)
416 ret = CryptMsgGetParam(hCryptMsg, CMSG_BARE_CONTENT_PARAM, 0,
417 info.Content.pbData, &info.Content.cbData);
420 char oid_rsa_hashed[] = szOID_RSA_hashedData;
422 info.pszObjId = oid_rsa_hashed;
423 ret = CryptEncodeObjectEx(X509_ASN_ENCODING,
424 PKCS_CONTENT_INFO, &info, 0, NULL, pvData, pcbData);
426 CryptMemFree(info.Content.pbData);
433 case CMSG_COMPUTED_HASH_PARAM:
434 ret = CryptGetHashParam(msg->hash, HP_HASHVAL, (BYTE *)pvData, pcbData,
437 case CMSG_VERSION_PARAM:
438 if (msg->base.state != MsgStateFinalized)
439 SetLastError(CRYPT_E_MSG_ERROR);
442 DWORD version = CMSG_HASHED_DATA_PKCS_1_5_VERSION;
444 /* Since the data are always encoded as octets, the version is
445 * always 0 (see rfc3852, section 7)
447 ret = CRYPT_CopyParam(pvData, pcbData, (const BYTE *)&version,
457 static BOOL CHashEncodeMsg_Update(HCRYPTMSG hCryptMsg, const BYTE *pbData,
458 DWORD cbData, BOOL fFinal)
460 CHashEncodeMsg *msg = (CHashEncodeMsg *)hCryptMsg;
463 TRACE("(%p, %p, %d, %d)\n", hCryptMsg, pbData, cbData, fFinal);
465 if (msg->base.streamed || (msg->base.open_flags & CMSG_DETACHED_FLAG))
467 /* Doesn't do much, as stream output is never called, and you
468 * can't get the content.
470 ret = CryptHashData(msg->hash, pbData, cbData, 0);
475 SetLastError(CRYPT_E_MSG_ERROR);
478 ret = CryptHashData(msg->hash, pbData, cbData, 0);
481 msg->data.pbData = CryptMemAlloc(cbData);
482 if (msg->data.pbData)
484 memcpy(msg->data.pbData + msg->data.cbData, pbData, cbData);
485 msg->data.cbData += cbData;
495 static HCRYPTMSG CHashEncodeMsg_Open(DWORD dwFlags, const void *pvMsgEncodeInfo,
496 LPSTR pszInnerContentObjID, PCMSG_STREAM_INFO pStreamInfo)
499 const CMSG_HASHED_ENCODE_INFO *info =
500 (const CMSG_HASHED_ENCODE_INFO *)pvMsgEncodeInfo;
504 if (info->cbSize != sizeof(CMSG_HASHED_ENCODE_INFO))
506 SetLastError(E_INVALIDARG);
509 if (!(algID = CertOIDToAlgId(info->HashAlgorithm.pszObjId)))
511 SetLastError(CRYPT_E_UNKNOWN_ALGO);
514 if (info->hCryptProv)
515 prov = info->hCryptProv;
518 prov = CRYPT_GetDefaultProvider();
519 dwFlags &= ~CMSG_CRYPT_RELEASE_CONTEXT_FLAG;
521 msg = CryptMemAlloc(sizeof(CHashEncodeMsg));
524 CryptMsgBase_Init((CryptMsgBase *)msg, dwFlags, pStreamInfo,
525 CHashEncodeMsg_Close, CHashEncodeMsg_GetParam, CHashEncodeMsg_Update);
527 msg->data.cbData = 0;
528 msg->data.pbData = NULL;
529 if (!CryptCreateHash(prov, algID, 0, 0, &msg->hash))
535 return (HCRYPTMSG)msg;
538 static inline const char *MSG_TYPE_STR(DWORD type)
542 #define _x(x) case (x): return #x
546 _x(CMSG_SIGNED_AND_ENVELOPED);
551 return wine_dbg_sprintf("unknown (%d)", type);
555 HCRYPTMSG WINAPI CryptMsgOpenToEncode(DWORD dwMsgEncodingType, DWORD dwFlags,
556 DWORD dwMsgType, const void *pvMsgEncodeInfo, LPSTR pszInnerContentObjID,
557 PCMSG_STREAM_INFO pStreamInfo)
559 HCRYPTMSG msg = NULL;
561 TRACE("(%08x, %08x, %08x, %p, %s, %p)\n", dwMsgEncodingType, dwFlags,
562 dwMsgType, pvMsgEncodeInfo, debugstr_a(pszInnerContentObjID), pStreamInfo);
564 if (GET_CMSG_ENCODING_TYPE(dwMsgEncodingType) != PKCS_7_ASN_ENCODING)
566 SetLastError(E_INVALIDARG);
572 msg = CDataEncodeMsg_Open(dwFlags, pvMsgEncodeInfo,
573 pszInnerContentObjID, pStreamInfo);
576 msg = CHashEncodeMsg_Open(dwFlags, pvMsgEncodeInfo,
577 pszInnerContentObjID, pStreamInfo);
581 FIXME("unimplemented for type %s\n", MSG_TYPE_STR(dwMsgType));
583 case CMSG_SIGNED_AND_ENVELOPED:
585 /* defined but invalid, fall through */
587 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
592 typedef struct _CDecodeMsg
596 HCRYPTPROV crypt_prov;
597 CRYPT_DATA_BLOB msg_data;
598 PCONTEXT_PROPERTY_LIST properties;
601 static void CDecodeMsg_Close(HCRYPTMSG hCryptMsg)
603 CDecodeMsg *msg = (CDecodeMsg *)hCryptMsg;
605 if (msg->base.open_flags & CMSG_CRYPT_RELEASE_CONTEXT_FLAG)
606 CryptReleaseContext(msg->crypt_prov, 0);
607 CryptMemFree(msg->msg_data.pbData);
608 ContextPropertyList_Free(msg->properties);
611 static BOOL CDecodeMsg_CopyData(CDecodeMsg *msg, const BYTE *pbData,
618 if (msg->msg_data.cbData)
619 msg->msg_data.pbData = CryptMemRealloc(msg->msg_data.pbData,
620 msg->msg_data.cbData + cbData);
622 msg->msg_data.pbData = CryptMemAlloc(cbData);
623 if (msg->msg_data.pbData)
625 memcpy(msg->msg_data.pbData + msg->msg_data.cbData, pbData, cbData);
626 msg->msg_data.cbData += cbData;
634 static BOOL CDecodeMsg_DecodeDataContent(CDecodeMsg *msg, CRYPT_DER_BLOB *blob)
637 CRYPT_DATA_BLOB *data;
640 ret = CryptDecodeObjectEx(X509_ASN_ENCODING, X509_OCTET_STRING,
641 blob->pbData, blob->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, (LPBYTE)&data,
645 ret = ContextPropertyList_SetProperty(msg->properties,
646 CMSG_CONTENT_PARAM, data->pbData, data->cbData);
652 /* Decodes the content in blob as the type given, and updates the value
653 * (type, parameters, etc.) of msg based on what blob contains.
654 * It doesn't just use msg's type, to allow a recursive call from an implicitly
655 * typed message once the outer content info has been decoded.
657 static BOOL CDecodeMsg_DecodeContent(CDecodeMsg *msg, CRYPT_DER_BLOB *blob,
666 if ((ret = CDecodeMsg_DecodeDataContent(msg, blob)))
667 msg->type = CMSG_DATA;
671 CRYPT_DIGESTED_DATA *digestedData;
673 ret = CRYPT_AsnDecodePKCSDigestedData(blob->pbData, blob->cbData,
674 CRYPT_DECODE_ALLOC_FLAG, NULL, (CRYPT_DIGESTED_DATA *)&digestedData,
678 msg->type = CMSG_HASHED;
679 ContextPropertyList_SetProperty(msg->properties,
680 CMSG_VERSION_PARAM, (const BYTE *)&digestedData->version,
681 sizeof(digestedData->version));
682 ContextPropertyList_SetProperty(msg->properties,
683 CMSG_INNER_CONTENT_TYPE_PARAM,
684 (const BYTE *)digestedData->ContentInfo.pszObjId,
685 digestedData->ContentInfo.pszObjId ?
686 strlen(digestedData->ContentInfo.pszObjId) + 1 : 0);
687 if (digestedData->ContentInfo.Content.cbData)
688 CDecodeMsg_DecodeDataContent(msg,
689 &digestedData->ContentInfo.Content);
691 ContextPropertyList_SetProperty(msg->properties,
692 CMSG_CONTENT_PARAM, NULL, 0);
693 ContextPropertyList_SetProperty(msg->properties,
694 CMSG_HASH_DATA_PARAM, digestedData->hash.pbData,
695 digestedData->hash.cbData);
696 LocalFree(digestedData);
702 FIXME("unimplemented for type %s\n", MSG_TYPE_STR(type));
707 CRYPT_CONTENT_INFO *info;
709 ret = CryptDecodeObjectEx(X509_ASN_ENCODING, PKCS_CONTENT_INFO,
710 msg->msg_data.pbData, msg->msg_data.cbData, CRYPT_DECODE_ALLOC_FLAG,
711 NULL, (LPBYTE)&info, &size);
714 if (!strcmp(info->pszObjId, szOID_RSA_data))
715 ret = CDecodeMsg_DecodeContent(msg, &info->Content, CMSG_DATA);
716 else if (!strcmp(info->pszObjId, szOID_RSA_digestedData))
717 ret = CDecodeMsg_DecodeContent(msg, &info->Content,
719 else if (!strcmp(info->pszObjId, szOID_RSA_envelopedData))
720 ret = CDecodeMsg_DecodeContent(msg, &info->Content,
722 else if (!strcmp(info->pszObjId, szOID_RSA_signedData))
723 ret = CDecodeMsg_DecodeContent(msg, &info->Content,
727 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
737 static BOOL CDecodeMsg_Update(HCRYPTMSG hCryptMsg, const BYTE *pbData,
738 DWORD cbData, BOOL fFinal)
740 CDecodeMsg *msg = (CDecodeMsg *)hCryptMsg;
743 TRACE("(%p, %p, %d, %d)\n", hCryptMsg, pbData, cbData, fFinal);
745 if (msg->base.streamed)
747 ret = CDecodeMsg_CopyData(msg, pbData, cbData);
748 FIXME("(%p, %p, %d, %d): streamed update stub\n", hCryptMsg, pbData,
754 SetLastError(CRYPT_E_MSG_ERROR);
757 ret = CDecodeMsg_CopyData(msg, pbData, cbData);
759 ret = CDecodeMsg_DecodeContent(msg, &msg->msg_data, msg->type);
766 static BOOL CDecodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
767 DWORD dwIndex, void *pvData, DWORD *pcbData)
769 CDecodeMsg *msg = (CDecodeMsg *)hCryptMsg;
774 case CMSG_TYPE_PARAM:
775 ret = CRYPT_CopyParam(pvData, pcbData, (const BYTE *)&msg->type,
780 CRYPT_DATA_BLOB blob;
782 ret = ContextPropertyList_FindProperty(msg->properties, dwParamType,
785 ret = CRYPT_CopyParam(pvData, pcbData, blob.pbData, blob.cbData);
787 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
793 HCRYPTMSG WINAPI CryptMsgOpenToDecode(DWORD dwMsgEncodingType, DWORD dwFlags,
794 DWORD dwMsgType, HCRYPTPROV hCryptProv, PCERT_INFO pRecipientInfo,
795 PCMSG_STREAM_INFO pStreamInfo)
799 TRACE("(%08x, %08x, %08x, %08lx, %p, %p)\n", dwMsgEncodingType,
800 dwFlags, dwMsgType, hCryptProv, pRecipientInfo, pStreamInfo);
802 if (GET_CMSG_ENCODING_TYPE(dwMsgEncodingType) != PKCS_7_ASN_ENCODING)
804 SetLastError(E_INVALIDARG);
807 msg = CryptMemAlloc(sizeof(CDecodeMsg));
810 CryptMsgBase_Init((CryptMsgBase *)msg, dwFlags, pStreamInfo,
811 CDecodeMsg_Close, CDecodeMsg_GetParam, CDecodeMsg_Update);
812 msg->type = dwMsgType;
814 msg->crypt_prov = hCryptProv;
817 msg->crypt_prov = CRYPT_GetDefaultProvider();
818 msg->base.open_flags &= ~CMSG_CRYPT_RELEASE_CONTEXT_FLAG;
820 msg->msg_data.cbData = 0;
821 msg->msg_data.pbData = NULL;
822 msg->properties = ContextPropertyList_Create();
827 HCRYPTMSG WINAPI CryptMsgDuplicate(HCRYPTMSG hCryptMsg)
829 TRACE("(%p)\n", hCryptMsg);
833 CryptMsgBase *msg = (CryptMsgBase *)hCryptMsg;
835 InterlockedIncrement(&msg->ref);
840 BOOL WINAPI CryptMsgClose(HCRYPTMSG hCryptMsg)
842 TRACE("(%p)\n", hCryptMsg);
846 CryptMsgBase *msg = (CryptMsgBase *)hCryptMsg;
848 if (InterlockedDecrement(&msg->ref) == 0)
850 TRACE("freeing %p\n", msg);
859 BOOL WINAPI CryptMsgUpdate(HCRYPTMSG hCryptMsg, const BYTE *pbData,
860 DWORD cbData, BOOL fFinal)
862 CryptMsgBase *msg = (CryptMsgBase *)hCryptMsg;
865 TRACE("(%p, %p, %d, %d)\n", hCryptMsg, pbData, cbData, fFinal);
867 if (msg->state == MsgStateFinalized)
868 SetLastError(CRYPT_E_MSG_ERROR);
871 ret = msg->update(hCryptMsg, pbData, cbData, fFinal);
872 msg->state = MsgStateUpdated;
874 msg->state = MsgStateFinalized;
879 BOOL WINAPI CryptMsgGetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
880 DWORD dwIndex, void *pvData, DWORD *pcbData)
882 CryptMsgBase *msg = (CryptMsgBase *)hCryptMsg;
884 TRACE("(%p, %d, %d, %p, %p)\n", hCryptMsg, dwParamType, dwIndex,
886 return msg->get_param(hCryptMsg, dwParamType, dwIndex, pvData, pcbData);