crypt32: Implement getting the hash for each signer of a signed encoded message.
[wine/testsucceed.git] / dlls / crypt32 / msg.c
blob226c04bd0f1d98e8f479799486a53464fb13bafc
1 /*
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
18 #include <stdarg.h>
19 #include "windef.h"
20 #include "winbase.h"
21 #include "wincrypt.h"
22 #include "snmp.h"
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
31 * data here.
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 {
42 MsgStateInit,
43 MsgStateUpdated,
44 MsgStateFinalized
45 } CryptMsgState;
47 typedef struct _CryptMsgBase
49 LONG ref;
50 DWORD open_flags;
51 BOOL streamed;
52 CMSG_STREAM_INFO stream_info;
53 CryptMsgState state;
54 CryptMsgCloseFunc close;
55 CryptMsgUpdateFunc update;
56 CryptMsgGetParamFunc get_param;
57 } CryptMsgBase;
59 static inline void CryptMsgBase_Init(CryptMsgBase *msg, DWORD dwFlags,
60 PCMSG_STREAM_INFO pStreamInfo, CryptMsgCloseFunc close,
61 CryptMsgGetParamFunc get_param, CryptMsgUpdateFunc update)
63 msg->ref = 1;
64 msg->open_flags = dwFlags;
65 if (pStreamInfo)
67 msg->streamed = TRUE;
68 memcpy(&msg->stream_info, pStreamInfo, sizeof(msg->stream_info));
70 else
72 msg->streamed = FALSE;
73 memset(&msg->stream_info, 0, sizeof(msg->stream_info));
75 msg->close = close;
76 msg->get_param = get_param;
77 msg->update = update;
78 msg->state = MsgStateInit;
81 typedef struct _CDataEncodeMsg
83 CryptMsgBase base;
84 DWORD bare_content_len;
85 LPBYTE bare_content;
86 } CDataEncodeMsg;
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;
103 DWORD lenBytes;
104 BOOL ret = TRUE;
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);
111 if (!pbEncoded)
112 *pcbEncoded = 1 + lenBytes + msg->base.stream_info.cbContent;
113 else
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,
122 &lenBytes);
125 return ret;
128 static BOOL CRYPT_EncodeDataContentInfoHeader(CDataEncodeMsg *msg,
129 CRYPT_DATA_BLOB *header)
131 BOOL ret;
133 if (msg->base.streamed && msg->base.stream_info.cbContent == 0xffffffff)
135 FIXME("unimplemented for indefinite-length encoding\n");
136 header->cbData = 0;
137 header->pbData = NULL;
138 ret = TRUE;
140 else
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);
152 if (ret)
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;
160 return ret;
163 static BOOL CDataEncodeMsg_Update(HCRYPTMSG hCryptMsg, const BYTE *pbData,
164 DWORD cbData, BOOL fFinal)
166 CDataEncodeMsg *msg = (CDataEncodeMsg *)hCryptMsg;
167 BOOL ret = FALSE;
169 if (msg->base.streamed)
171 __TRY
173 if (msg->base.state != MsgStateUpdated)
175 CRYPT_DATA_BLOB header;
177 ret = CRYPT_EncodeDataContentInfoHeader(msg, &header);
178 if (ret)
180 ret = msg->base.stream_info.pfnStreamOutput(
181 msg->base.stream_info.pvArg, header.pbData, header.cbData,
182 FALSE);
183 LocalFree(header.pbData);
186 if (!fFinal)
187 ret = msg->base.stream_info.pfnStreamOutput(
188 msg->base.stream_info.pvArg, (BYTE *)pbData, cbData,
189 FALSE);
190 else
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,
198 FALSE);
199 if (ret)
200 ret = msg->base.stream_info.pfnStreamOutput(
201 msg->base.stream_info.pvArg, indefinite_trailer,
202 sizeof(indefinite_trailer), TRUE);
204 else
205 ret = msg->base.stream_info.pfnStreamOutput(
206 msg->base.stream_info.pvArg, (BYTE *)pbData, cbData, TRUE);
209 __EXCEPT_PAGE_FAULT
211 SetLastError(STATUS_ACCESS_VIOLATION);
213 __ENDTRY;
215 else
217 if (!fFinal)
219 if (msg->base.open_flags & CMSG_DETACHED_FLAG)
220 SetLastError(E_INVALIDARG);
221 else
222 SetLastError(CRYPT_E_MSG_ERROR);
224 else
226 if (!cbData)
227 SetLastError(E_INVALIDARG);
228 else
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);
241 return ret;
244 static BOOL CRYPT_CopyParam(void *pvData, DWORD *pcbData, const BYTE *src,
245 DWORD len)
247 BOOL ret = TRUE;
249 if (!pvData)
250 *pcbData = len;
251 else if (*pcbData < len)
253 *pcbData = len;
254 SetLastError(ERROR_MORE_DATA);
255 ret = FALSE;
257 else
259 *pcbData = len;
260 memcpy(pvData, src, len);
262 return ret;
265 static BOOL CDataEncodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
266 DWORD dwIndex, void *pvData, DWORD *pcbData)
268 CDataEncodeMsg *msg = (CDataEncodeMsg *)hCryptMsg;
269 BOOL ret = FALSE;
271 switch (dwParamType)
273 case CMSG_CONTENT_PARAM:
274 if (msg->base.streamed)
275 SetLastError(E_INVALIDARG);
276 else
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,
285 pvData, pcbData);
287 break;
288 case CMSG_BARE_CONTENT_PARAM:
289 if (msg->base.streamed)
290 SetLastError(E_INVALIDARG);
291 else
292 ret = CRYPT_CopyParam(pvData, pcbData, msg->bare_content,
293 msg->bare_content_len);
294 break;
295 default:
296 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
298 return ret;
301 static HCRYPTMSG CDataEncodeMsg_Open(DWORD dwFlags, const void *pvMsgEncodeInfo,
302 LPSTR pszInnerContentObjID, PCMSG_STREAM_INFO pStreamInfo)
304 CDataEncodeMsg *msg;
306 if (pvMsgEncodeInfo)
308 SetLastError(E_INVALIDARG);
309 return NULL;
311 msg = CryptMemAlloc(sizeof(CDataEncodeMsg));
312 if (msg)
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
324 CryptMsgBase base;
325 HCRYPTPROV prov;
326 HCRYPTHASH hash;
327 CRYPT_DATA_BLOB data;
328 } CHashEncodeMsg;
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,
341 DWORD *pcbData)
343 BOOL ret;
344 ALG_ID algID;
345 DWORD size = sizeof(algID);
347 ret = CryptGetHashParam(msg->hash, HP_ALGID, (BYTE *)&algID, &size, 0);
348 if (ret)
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);
371 if (ret)
373 digestedData.hash.pbData = CryptMemAlloc(
374 digestedData.hash.cbData);
375 ret = CryptGetHashParam(msg->hash, HP_HASHVAL,
376 digestedData.hash.pbData, &digestedData.hash.cbData, 0);
379 if (ret)
380 ret = CRYPT_AsnEncodePKCSDigestedData(&digestedData, pvData,
381 pcbData);
382 CryptMemFree(digestedData.hash.pbData);
383 LocalFree(digestedData.ContentInfo.Content.pbData);
385 return ret;
388 static BOOL CHashEncodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
389 DWORD dwIndex, void *pvData, DWORD *pcbData)
391 CHashEncodeMsg *msg = (CHashEncodeMsg *)hCryptMsg;
392 BOOL ret = FALSE;
394 TRACE("(%p, %d, %d, %p, %p)\n", hCryptMsg, dwParamType, dwIndex,
395 pvData, pcbData);
397 switch (dwParamType)
399 case CMSG_BARE_CONTENT_PARAM:
400 if (msg->base.streamed)
401 SetLastError(E_INVALIDARG);
402 else
403 ret = CRYPT_EncodePKCSDigestedData(msg, pvData, pcbData);
404 break;
405 case CMSG_CONTENT_PARAM:
407 CRYPT_CONTENT_INFO info;
409 ret = CryptMsgGetParam(hCryptMsg, CMSG_BARE_CONTENT_PARAM, 0, NULL,
410 &info.Content.cbData);
411 if (ret)
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);
418 if (ret)
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);
428 else
429 ret = FALSE;
431 break;
433 case CMSG_COMPUTED_HASH_PARAM:
434 ret = CryptGetHashParam(msg->hash, HP_HASHVAL, (BYTE *)pvData, pcbData,
436 break;
437 case CMSG_VERSION_PARAM:
438 if (msg->base.state != MsgStateFinalized)
439 SetLastError(CRYPT_E_MSG_ERROR);
440 else
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,
448 sizeof(version));
450 break;
451 default:
452 ret = FALSE;
454 return ret;
457 static BOOL CHashEncodeMsg_Update(HCRYPTMSG hCryptMsg, const BYTE *pbData,
458 DWORD cbData, BOOL fFinal)
460 CHashEncodeMsg *msg = (CHashEncodeMsg *)hCryptMsg;
461 BOOL ret = FALSE;
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);
472 else
474 if (!fFinal)
475 SetLastError(CRYPT_E_MSG_ERROR);
476 else
478 ret = CryptHashData(msg->hash, pbData, cbData, 0);
479 if (ret)
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;
487 else
488 ret = FALSE;
492 return ret;
495 static HCRYPTMSG CHashEncodeMsg_Open(DWORD dwFlags, const void *pvMsgEncodeInfo,
496 LPSTR pszInnerContentObjID, PCMSG_STREAM_INFO pStreamInfo)
498 CHashEncodeMsg *msg;
499 const CMSG_HASHED_ENCODE_INFO *info =
500 (const CMSG_HASHED_ENCODE_INFO *)pvMsgEncodeInfo;
501 HCRYPTPROV prov;
502 ALG_ID algID;
504 if (info->cbSize != sizeof(CMSG_HASHED_ENCODE_INFO))
506 SetLastError(E_INVALIDARG);
507 return NULL;
509 if (!(algID = CertOIDToAlgId(info->HashAlgorithm.pszObjId)))
511 SetLastError(CRYPT_E_UNKNOWN_ALGO);
512 return NULL;
514 if (info->hCryptProv)
515 prov = info->hCryptProv;
516 else
518 prov = CRYPT_GetDefaultProvider();
519 dwFlags &= ~CMSG_CRYPT_RELEASE_CONTEXT_FLAG;
521 msg = CryptMemAlloc(sizeof(CHashEncodeMsg));
522 if (msg)
524 CryptMsgBase_Init((CryptMsgBase *)msg, dwFlags, pStreamInfo,
525 CHashEncodeMsg_Close, CHashEncodeMsg_GetParam, CHashEncodeMsg_Update);
526 msg->prov = prov;
527 msg->data.cbData = 0;
528 msg->data.pbData = NULL;
529 if (!CryptCreateHash(prov, algID, 0, 0, &msg->hash))
531 CryptMsgClose(msg);
532 msg = NULL;
535 return (HCRYPTMSG)msg;
538 typedef struct _CMSG_SIGNER_ENCODE_INFO_WITH_CMS
540 DWORD cbSize;
541 PCERT_INFO pCertInfo;
542 HCRYPTPROV hCryptProv;
543 DWORD dwKeySpec;
544 CRYPT_ALGORITHM_IDENTIFIER HashAlgorithm;
545 void *pvHashAuxInfo;
546 DWORD cAuthAttr;
547 PCRYPT_ATTRIBUTE rgAuthAttr;
548 DWORD cUnauthAttr;
549 PCRYPT_ATTRIBUTE rgUnauthAttr;
550 CERT_ID SignerId;
551 CRYPT_ALGORITHM_IDENTIFIER HashEncryptionAlgorithm;
552 void *pvHashEncryptionAuxInfo;
553 } CMSG_SIGNER_ENCODE_INFO_WITH_CMS, *PCMSG_SIGNER_ENCODE_INFO_WITH_CMS;
555 typedef struct _CMSG_SIGNED_ENCODE_INFO_WITH_CMS
557 DWORD cbSize;
558 DWORD cSigners;
559 PCMSG_SIGNER_ENCODE_INFO_WITH_CMS rgSigners;
560 DWORD cCertEncoded;
561 PCERT_BLOB rgCertEncoded;
562 DWORD cCrlEncoded;
563 PCRL_BLOB rgCrlEncoded;
564 DWORD cAttrCertEncoded;
565 PCERT_BLOB rgAttrCertEncoded;
566 } CMSG_SIGNED_ENCODE_INFO_WITH_CMS, *PCMSG_SIGNED_ENCODE_INFO_WITH_CMS;
568 static BOOL CRYPT_IsValidSigner(CMSG_SIGNER_ENCODE_INFO_WITH_CMS *signer)
570 if (signer->cbSize != sizeof(CMSG_SIGNER_ENCODE_INFO) &&
571 signer->cbSize != sizeof(CMSG_SIGNER_ENCODE_INFO_WITH_CMS))
573 SetLastError(E_INVALIDARG);
574 return FALSE;
576 if (signer->cbSize == sizeof(CMSG_SIGNER_ENCODE_INFO_WITH_CMS))
578 FIXME("CMSG_SIGNER_ENCODE_INFO with CMS fields unsupported\n");
579 return FALSE;
581 if (!signer->pCertInfo->SerialNumber.cbData)
583 SetLastError(E_INVALIDARG);
584 return FALSE;
586 if (!signer->pCertInfo->Issuer.cbData)
588 SetLastError(E_INVALIDARG);
589 return FALSE;
591 if (!signer->hCryptProv)
593 SetLastError(E_INVALIDARG);
594 return FALSE;
596 if (!CertOIDToAlgId(signer->HashAlgorithm.pszObjId))
598 SetLastError(CRYPT_E_UNKNOWN_ALGO);
599 return FALSE;
601 return TRUE;
604 typedef struct _CSignerInfo
606 HCRYPTPROV prov;
607 HCRYPTHASH hash;
608 HCRYPTKEY key;
609 CMSG_SIGNER_INFO info;
610 } CSignerInfo;
612 static BOOL CRYPT_CopyBlob(CRYPT_DATA_BLOB *out, const CRYPT_DATA_BLOB *in)
614 BOOL ret = TRUE;
616 out->cbData = in->cbData;
617 if (out->cbData)
619 out->pbData = CryptMemAlloc(out->cbData);
620 if (out->pbData)
621 memcpy(out->pbData, in->pbData, out->cbData);
622 else
623 ret = FALSE;
625 else
626 out->pbData = NULL;
627 return ret;
630 static BOOL CRYPT_CopyAttribute(CRYPT_ATTRIBUTE *out, const CRYPT_ATTRIBUTE *in)
632 BOOL ret = TRUE;
634 /* Assumption: algorithm IDs will point to static strings, not stack-based
635 * ones, so copying the pointer values is safe.
637 out->pszObjId = in->pszObjId;
638 out->cValue = in->cValue;
639 if (out->cValue)
641 out->rgValue = CryptMemAlloc(out->cValue * sizeof(CRYPT_DATA_BLOB));
642 if (out->rgValue)
644 DWORD i;
646 memset(out->rgValue, 0, out->cValue * sizeof(CRYPT_DATA_BLOB));
647 for (i = 0; ret && i < out->cValue; i++)
648 ret = CRYPT_CopyBlob(&out->rgValue[i], &in->rgValue[i]);
650 else
651 ret = FALSE;
653 return ret;
656 static BOOL CRYPT_CopyAttributes(CRYPT_ATTRIBUTES *out,
657 const CRYPT_ATTRIBUTES *in)
659 BOOL ret = TRUE;
661 out->cAttr = in->cAttr;
662 if (out->cAttr)
664 out->rgAttr = CryptMemAlloc(out->cAttr * sizeof(CRYPT_ATTRIBUTE));
665 if (out->rgAttr)
667 DWORD i;
669 memset(out->rgAttr, 0, out->cAttr * sizeof(CRYPT_ATTRIBUTE));
670 for (i = 0; ret && i < out->cAttr; i++)
671 ret = CRYPT_CopyAttribute(&out->rgAttr[i], &in->rgAttr[i]);
673 else
674 ret = FALSE;
676 else
677 out->rgAttr = NULL;
678 return ret;
681 static BOOL CSignerInfo_Construct(CSignerInfo *out,
682 CMSG_SIGNER_ENCODE_INFO_WITH_CMS *in, DWORD open_flags)
684 ALG_ID algID;
685 BOOL ret;
687 out->prov = in->hCryptProv;
688 if (!(open_flags & CMSG_CRYPT_RELEASE_CONTEXT_FLAG))
689 CryptContextAddRef(out->prov, NULL, 0);
690 algID = CertOIDToAlgId(in->HashAlgorithm.pszObjId);
691 ret = CryptCreateHash(out->prov, algID, 0, 0, &out->hash);
692 if (ret)
694 /* Note: needs to change if CMS fields are supported */
695 out->info.dwVersion = CMSG_SIGNER_INFO_V1;
696 ret = CRYPT_CopyBlob(&out->info.Issuer, &in->pCertInfo->Issuer);
697 if (ret)
698 ret = CRYPT_CopyBlob(&out->info.SerialNumber,
699 &in->pCertInfo->SerialNumber);
700 /* Assumption: algorithm IDs will point to static strings, not
701 * stack-based ones, so copying the pointer values is safe.
703 out->info.HashAlgorithm.pszObjId = in->HashAlgorithm.pszObjId;
704 if (ret)
705 ret = CRYPT_CopyBlob(&out->info.HashAlgorithm.Parameters,
706 &in->HashAlgorithm.Parameters);
707 memset(&out->info.HashEncryptionAlgorithm, 0,
708 sizeof(out->info.HashEncryptionAlgorithm));
709 if (ret)
710 ret = CRYPT_CopyAttributes(&out->info.AuthAttrs,
711 (CRYPT_ATTRIBUTES *)&in->cAuthAttr);
712 if (ret)
713 ret = CRYPT_CopyAttributes(&out->info.UnauthAttrs,
714 (CRYPT_ATTRIBUTES *)&in->cUnauthAttr);
716 return ret;
719 static void CSignerInfo_Free(CSignerInfo *signer)
721 DWORD i, j;
723 CryptDestroyKey(signer->key);
724 CryptDestroyHash(signer->hash);
725 CryptReleaseContext(signer->prov, 0);
726 CryptMemFree(signer->info.Issuer.pbData);
727 CryptMemFree(signer->info.SerialNumber.pbData);
728 CryptMemFree(signer->info.HashAlgorithm.Parameters.pbData);
729 CryptMemFree(signer->info.EncryptedHash.pbData);
730 for (i = 0; i < signer->info.AuthAttrs.cAttr; i++)
732 for (j = 0; j < signer->info.AuthAttrs.rgAttr[i].cValue; j++)
733 CryptMemFree(signer->info.AuthAttrs.rgAttr[i].rgValue[j].pbData);
734 CryptMemFree(signer->info.AuthAttrs.rgAttr[i].rgValue);
736 CryptMemFree(signer->info.AuthAttrs.rgAttr);
737 for (i = 0; i < signer->info.UnauthAttrs.cAttr; i++)
739 for (j = 0; j < signer->info.UnauthAttrs.rgAttr[i].cValue; j++)
740 CryptMemFree(signer->info.UnauthAttrs.rgAttr[i].rgValue[j].pbData);
741 CryptMemFree(signer->info.UnauthAttrs.rgAttr[i].rgValue);
743 CryptMemFree(signer->info.UnauthAttrs.rgAttr);
746 typedef struct _CSignedEncodeMsg
748 CryptMsgBase base;
749 CRYPT_DATA_BLOB data;
750 DWORD cSigners;
751 CSignerInfo *signers;
752 } CSignedEncodeMsg;
754 static void CSignedEncodeMsg_Close(HCRYPTMSG hCryptMsg)
756 CSignedEncodeMsg *msg = (CSignedEncodeMsg *)hCryptMsg;
757 DWORD i;
759 CryptMemFree(msg->data.pbData);
760 for (i = 0; i < msg->cSigners; i++)
761 CSignerInfo_Free(&msg->signers[i]);
762 CryptMemFree(msg->signers);
765 static BOOL CSignedEncodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
766 DWORD dwIndex, void *pvData, DWORD *pcbData)
768 CSignedEncodeMsg *msg = (CSignedEncodeMsg *)hCryptMsg;
769 BOOL ret = FALSE;
771 switch (dwParamType)
773 case CMSG_COMPUTED_HASH_PARAM:
774 if (dwIndex >= msg->cSigners)
775 SetLastError(CRYPT_E_INVALID_INDEX);
776 else
777 ret = CryptGetHashParam(msg->signers[dwIndex].hash, HP_HASHVAL,
778 pvData, pcbData, 0);
779 break;
780 default:
781 FIXME("unimplemented for %d\n", dwParamType);
782 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
784 return ret;
787 static BOOL CSignedEncodeMsg_UpdateHash(CSignedEncodeMsg *msg,
788 const BYTE *pbData, DWORD cbData)
790 DWORD i;
791 BOOL ret = TRUE;
793 TRACE("(%p, %p, %d)\n", msg, pbData, cbData);
795 for (i = 0; ret && i < msg->cSigners; i++)
796 ret = CryptHashData(msg->signers[i].hash, pbData, cbData, 0);
797 return ret;
800 static void CRYPT_ReverseBytes(CRYPT_HASH_BLOB *hash)
802 DWORD i;
803 BYTE tmp;
805 for (i = 0; i < hash->cbData / 2; i++)
807 tmp = hash->pbData[hash->cbData - i - 1];
808 hash->pbData[hash->cbData - i - 1] = hash->pbData[i];
809 hash->pbData[i] = tmp;
813 static BOOL CSignedEncodeMsg_Sign(CSignedEncodeMsg *msg)
815 DWORD i;
816 BOOL ret = TRUE;
818 TRACE("(%p)\n", msg);
820 for (i = 0; ret && i < msg->cSigners; i++)
822 ret = CryptSignHashW(msg->signers[i].hash, AT_SIGNATURE, NULL, 0, NULL,
823 &msg->signers[i].info.EncryptedHash.cbData);
824 if (ret)
826 msg->signers[i].info.EncryptedHash.pbData =
827 CryptMemAlloc(msg->signers[i].info.EncryptedHash.cbData);
828 if (msg->signers[i].info.EncryptedHash.pbData)
830 ret = CryptSignHashW(msg->signers[i].hash, AT_SIGNATURE, NULL,
831 0, msg->signers[i].info.EncryptedHash.pbData,
832 &msg->signers[i].info.EncryptedHash.cbData);
833 if (ret)
834 CRYPT_ReverseBytes(&msg->signers[i].info.EncryptedHash);
836 else
837 ret = FALSE;
840 return ret;
843 static BOOL CSignedEncodeMsg_Update(HCRYPTMSG hCryptMsg, const BYTE *pbData,
844 DWORD cbData, BOOL fFinal)
846 CSignedEncodeMsg *msg = (CSignedEncodeMsg *)hCryptMsg;
847 BOOL ret = FALSE;
849 if (msg->base.streamed || (msg->base.open_flags & CMSG_DETACHED_FLAG))
851 ret = CSignedEncodeMsg_UpdateHash(msg, pbData, cbData);
852 /* FIXME: hash authenticated attributes on final update */
853 if (ret && fFinal)
854 ret = CSignedEncodeMsg_Sign(msg);
855 if (msg->base.streamed)
856 FIXME("streamed partial stub\n");
858 else
860 if (!fFinal)
861 SetLastError(CRYPT_E_MSG_ERROR);
862 else
864 if (cbData)
866 msg->data.pbData = CryptMemAlloc(cbData);
867 if (msg->data.pbData)
869 memcpy(msg->data.pbData, pbData, cbData);
870 msg->data.cbData = cbData;
871 ret = TRUE;
874 else
875 ret = TRUE;
876 if (ret)
877 ret = CSignedEncodeMsg_UpdateHash(msg, pbData, cbData);
878 /* FIXME: hash authenticated attributes */
879 if (ret)
880 ret = CSignedEncodeMsg_Sign(msg);
883 return ret;
886 static HCRYPTMSG CSignedEncodeMsg_Open(DWORD dwFlags,
887 const void *pvMsgEncodeInfo, LPSTR pszInnerContentObjID,
888 PCMSG_STREAM_INFO pStreamInfo)
890 const CMSG_SIGNED_ENCODE_INFO_WITH_CMS *info =
891 (const CMSG_SIGNED_ENCODE_INFO_WITH_CMS *)pvMsgEncodeInfo;
892 DWORD i;
893 CSignedEncodeMsg *msg;
895 if (info->cbSize != sizeof(CMSG_SIGNED_ENCODE_INFO) &&
896 info->cbSize != sizeof(CMSG_SIGNED_ENCODE_INFO_WITH_CMS))
898 SetLastError(E_INVALIDARG);
899 return NULL;
901 if (info->cbSize == sizeof(CMSG_SIGNED_ENCODE_INFO_WITH_CMS))
903 FIXME("CMSG_SIGNED_ENCODE_INFO with CMS fields unsupported\n");
904 return NULL;
906 for (i = 0; i < info->cSigners; i++)
907 if (!CRYPT_IsValidSigner(&info->rgSigners[i]))
908 return NULL;
909 msg = CryptMemAlloc(sizeof(CSignedEncodeMsg));
910 if (msg)
912 BOOL ret = TRUE;
914 CryptMsgBase_Init((CryptMsgBase *)msg, dwFlags, pStreamInfo,
915 CSignedEncodeMsg_Close, CSignedEncodeMsg_GetParam,
916 CSignedEncodeMsg_Update);
917 msg->data.cbData = 0;
918 msg->data.pbData = NULL;
919 msg->cSigners = 0;
920 if (info->cSigners)
922 msg->signers = CryptMemAlloc(info->cSigners * sizeof(CSignerInfo));
923 if (msg->signers)
925 msg->cSigners = info->cSigners;
926 memset(msg->signers, 0, msg->cSigners * sizeof(CSignerInfo));
927 for (i = 0; ret && i < msg->cSigners; i++)
928 ret = CSignerInfo_Construct(&msg->signers[i],
929 &info->rgSigners[i], dwFlags);
931 else
932 ret = FALSE;
934 if (!ret)
936 CSignedEncodeMsg_Close(msg);
937 msg = NULL;
940 return msg;
943 static inline const char *MSG_TYPE_STR(DWORD type)
945 switch (type)
947 #define _x(x) case (x): return #x
948 _x(CMSG_DATA);
949 _x(CMSG_SIGNED);
950 _x(CMSG_ENVELOPED);
951 _x(CMSG_SIGNED_AND_ENVELOPED);
952 _x(CMSG_HASHED);
953 _x(CMSG_ENCRYPTED);
954 #undef _x
955 default:
956 return wine_dbg_sprintf("unknown (%d)", type);
960 HCRYPTMSG WINAPI CryptMsgOpenToEncode(DWORD dwMsgEncodingType, DWORD dwFlags,
961 DWORD dwMsgType, const void *pvMsgEncodeInfo, LPSTR pszInnerContentObjID,
962 PCMSG_STREAM_INFO pStreamInfo)
964 HCRYPTMSG msg = NULL;
966 TRACE("(%08x, %08x, %08x, %p, %s, %p)\n", dwMsgEncodingType, dwFlags,
967 dwMsgType, pvMsgEncodeInfo, debugstr_a(pszInnerContentObjID), pStreamInfo);
969 if (GET_CMSG_ENCODING_TYPE(dwMsgEncodingType) != PKCS_7_ASN_ENCODING)
971 SetLastError(E_INVALIDARG);
972 return NULL;
974 switch (dwMsgType)
976 case CMSG_DATA:
977 msg = CDataEncodeMsg_Open(dwFlags, pvMsgEncodeInfo,
978 pszInnerContentObjID, pStreamInfo);
979 break;
980 case CMSG_HASHED:
981 msg = CHashEncodeMsg_Open(dwFlags, pvMsgEncodeInfo,
982 pszInnerContentObjID, pStreamInfo);
983 break;
984 case CMSG_SIGNED:
985 msg = CSignedEncodeMsg_Open(dwFlags, pvMsgEncodeInfo,
986 pszInnerContentObjID, pStreamInfo);
987 break;
988 case CMSG_ENVELOPED:
989 FIXME("unimplemented for type %s\n", MSG_TYPE_STR(dwMsgType));
990 break;
991 case CMSG_SIGNED_AND_ENVELOPED:
992 case CMSG_ENCRYPTED:
993 /* defined but invalid, fall through */
994 default:
995 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
997 return msg;
1000 typedef struct _CDecodeMsg
1002 CryptMsgBase base;
1003 DWORD type;
1004 HCRYPTPROV crypt_prov;
1005 HCRYPTHASH hash;
1006 CRYPT_DATA_BLOB msg_data;
1007 PCONTEXT_PROPERTY_LIST properties;
1008 } CDecodeMsg;
1010 static void CDecodeMsg_Close(HCRYPTMSG hCryptMsg)
1012 CDecodeMsg *msg = (CDecodeMsg *)hCryptMsg;
1014 if (msg->base.open_flags & CMSG_CRYPT_RELEASE_CONTEXT_FLAG)
1015 CryptReleaseContext(msg->crypt_prov, 0);
1016 CryptDestroyHash(msg->hash);
1017 CryptMemFree(msg->msg_data.pbData);
1018 ContextPropertyList_Free(msg->properties);
1021 static BOOL CDecodeMsg_CopyData(CDecodeMsg *msg, const BYTE *pbData,
1022 DWORD cbData)
1024 BOOL ret = TRUE;
1026 if (cbData)
1028 if (msg->msg_data.cbData)
1029 msg->msg_data.pbData = CryptMemRealloc(msg->msg_data.pbData,
1030 msg->msg_data.cbData + cbData);
1031 else
1032 msg->msg_data.pbData = CryptMemAlloc(cbData);
1033 if (msg->msg_data.pbData)
1035 memcpy(msg->msg_data.pbData + msg->msg_data.cbData, pbData, cbData);
1036 msg->msg_data.cbData += cbData;
1038 else
1039 ret = FALSE;
1041 return ret;
1044 static BOOL CDecodeMsg_DecodeDataContent(CDecodeMsg *msg, CRYPT_DER_BLOB *blob)
1046 BOOL ret;
1047 CRYPT_DATA_BLOB *data;
1048 DWORD size;
1050 ret = CryptDecodeObjectEx(X509_ASN_ENCODING, X509_OCTET_STRING,
1051 blob->pbData, blob->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, (LPBYTE)&data,
1052 &size);
1053 if (ret)
1055 ret = ContextPropertyList_SetProperty(msg->properties,
1056 CMSG_CONTENT_PARAM, data->pbData, data->cbData);
1057 LocalFree(data);
1059 return ret;
1062 static void CDecodeMsg_SaveAlgorithmID(CDecodeMsg *msg, DWORD param,
1063 const CRYPT_ALGORITHM_IDENTIFIER *id)
1065 static const BYTE nullParams[] = { ASN_NULL, 0 };
1066 CRYPT_ALGORITHM_IDENTIFIER *copy;
1067 DWORD len = sizeof(CRYPT_ALGORITHM_IDENTIFIER);
1069 /* Linearize algorithm id */
1070 len += strlen(id->pszObjId) + 1;
1071 len += id->Parameters.cbData;
1072 copy = CryptMemAlloc(len);
1073 if (copy)
1075 copy->pszObjId =
1076 (LPSTR)((BYTE *)copy + sizeof(CRYPT_ALGORITHM_IDENTIFIER));
1077 strcpy(copy->pszObjId, id->pszObjId);
1078 copy->Parameters.pbData = (BYTE *)copy->pszObjId + strlen(id->pszObjId)
1079 + 1;
1080 /* Trick: omit NULL parameters */
1081 if (id->Parameters.cbData == sizeof(nullParams) &&
1082 !memcmp(id->Parameters.pbData, nullParams, sizeof(nullParams)))
1084 copy->Parameters.cbData = 0;
1085 len -= sizeof(nullParams);
1087 else
1088 copy->Parameters.cbData = id->Parameters.cbData;
1089 if (copy->Parameters.cbData)
1090 memcpy(copy->Parameters.pbData, id->Parameters.pbData,
1091 id->Parameters.cbData);
1092 ContextPropertyList_SetProperty(msg->properties, param, (BYTE *)copy,
1093 len);
1094 CryptMemFree(copy);
1098 static inline void CRYPT_FixUpAlgorithmID(CRYPT_ALGORITHM_IDENTIFIER *id)
1100 id->pszObjId = (LPSTR)((BYTE *)id + sizeof(CRYPT_ALGORITHM_IDENTIFIER));
1101 id->Parameters.pbData = (BYTE *)id->pszObjId + strlen(id->pszObjId) + 1;
1104 static BOOL CDecodeMsg_DecodeHashedContent(CDecodeMsg *msg,
1105 CRYPT_DER_BLOB *blob)
1107 BOOL ret;
1108 CRYPT_DIGESTED_DATA *digestedData;
1109 DWORD size;
1111 ret = CRYPT_AsnDecodePKCSDigestedData(blob->pbData, blob->cbData,
1112 CRYPT_DECODE_ALLOC_FLAG, NULL, (CRYPT_DIGESTED_DATA *)&digestedData,
1113 &size);
1114 if (ret)
1116 msg->type = CMSG_HASHED;
1117 ContextPropertyList_SetProperty(msg->properties, CMSG_VERSION_PARAM,
1118 (const BYTE *)&digestedData->version, sizeof(digestedData->version));
1119 CDecodeMsg_SaveAlgorithmID(msg, CMSG_HASH_ALGORITHM_PARAM,
1120 &digestedData->DigestAlgorithm);
1121 ContextPropertyList_SetProperty(msg->properties,
1122 CMSG_INNER_CONTENT_TYPE_PARAM,
1123 (const BYTE *)digestedData->ContentInfo.pszObjId,
1124 digestedData->ContentInfo.pszObjId ?
1125 strlen(digestedData->ContentInfo.pszObjId) + 1 : 0);
1126 if (digestedData->ContentInfo.Content.cbData)
1127 CDecodeMsg_DecodeDataContent(msg,
1128 &digestedData->ContentInfo.Content);
1129 else
1130 ContextPropertyList_SetProperty(msg->properties,
1131 CMSG_CONTENT_PARAM, NULL, 0);
1132 ContextPropertyList_SetProperty(msg->properties, CMSG_HASH_DATA_PARAM,
1133 digestedData->hash.pbData, digestedData->hash.cbData);
1134 LocalFree(digestedData);
1136 return ret;
1139 /* Decodes the content in blob as the type given, and updates the value
1140 * (type, parameters, etc.) of msg based on what blob contains.
1141 * It doesn't just use msg's type, to allow a recursive call from an implicitly
1142 * typed message once the outer content info has been decoded.
1144 static BOOL CDecodeMsg_DecodeContent(CDecodeMsg *msg, CRYPT_DER_BLOB *blob,
1145 DWORD type)
1147 BOOL ret;
1149 switch (type)
1151 case CMSG_DATA:
1152 if ((ret = CDecodeMsg_DecodeDataContent(msg, blob)))
1153 msg->type = CMSG_DATA;
1154 break;
1155 case CMSG_HASHED:
1156 if ((ret = CDecodeMsg_DecodeHashedContent(msg, blob)))
1157 msg->type = CMSG_HASHED;
1158 break;
1159 case CMSG_ENVELOPED:
1160 case CMSG_SIGNED:
1161 FIXME("unimplemented for type %s\n", MSG_TYPE_STR(type));
1162 ret = TRUE;
1163 break;
1164 default:
1166 CRYPT_CONTENT_INFO *info;
1167 DWORD size;
1169 ret = CryptDecodeObjectEx(X509_ASN_ENCODING, PKCS_CONTENT_INFO,
1170 msg->msg_data.pbData, msg->msg_data.cbData, CRYPT_DECODE_ALLOC_FLAG,
1171 NULL, (LPBYTE)&info, &size);
1172 if (ret)
1174 if (!strcmp(info->pszObjId, szOID_RSA_data))
1175 ret = CDecodeMsg_DecodeContent(msg, &info->Content, CMSG_DATA);
1176 else if (!strcmp(info->pszObjId, szOID_RSA_digestedData))
1177 ret = CDecodeMsg_DecodeContent(msg, &info->Content,
1178 CMSG_HASHED);
1179 else if (!strcmp(info->pszObjId, szOID_RSA_envelopedData))
1180 ret = CDecodeMsg_DecodeContent(msg, &info->Content,
1181 CMSG_ENVELOPED);
1182 else if (!strcmp(info->pszObjId, szOID_RSA_signedData))
1183 ret = CDecodeMsg_DecodeContent(msg, &info->Content,
1184 CMSG_SIGNED);
1185 else
1187 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1188 ret = FALSE;
1190 LocalFree(info);
1194 return ret;
1197 static BOOL CDecodeMsg_Update(HCRYPTMSG hCryptMsg, const BYTE *pbData,
1198 DWORD cbData, BOOL fFinal)
1200 CDecodeMsg *msg = (CDecodeMsg *)hCryptMsg;
1201 BOOL ret = FALSE;
1203 TRACE("(%p, %p, %d, %d)\n", hCryptMsg, pbData, cbData, fFinal);
1205 if (msg->base.streamed)
1207 ret = CDecodeMsg_CopyData(msg, pbData, cbData);
1208 FIXME("(%p, %p, %d, %d): streamed update stub\n", hCryptMsg, pbData,
1209 cbData, fFinal);
1211 else
1213 if (!fFinal)
1214 SetLastError(CRYPT_E_MSG_ERROR);
1215 else
1217 ret = CDecodeMsg_CopyData(msg, pbData, cbData);
1218 if (ret)
1219 ret = CDecodeMsg_DecodeContent(msg, &msg->msg_data, msg->type);
1223 return ret;
1226 static BOOL CDecodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
1227 DWORD dwIndex, void *pvData, DWORD *pcbData)
1229 CDecodeMsg *msg = (CDecodeMsg *)hCryptMsg;
1230 BOOL ret = FALSE;
1232 switch (dwParamType)
1234 case CMSG_TYPE_PARAM:
1235 ret = CRYPT_CopyParam(pvData, pcbData, (const BYTE *)&msg->type,
1236 sizeof(msg->type));
1237 break;
1238 case CMSG_HASH_ALGORITHM_PARAM:
1240 CRYPT_DATA_BLOB blob;
1242 ret = ContextPropertyList_FindProperty(msg->properties, dwParamType,
1243 &blob);
1244 if (ret)
1246 ret = CRYPT_CopyParam(pvData, pcbData, blob.pbData, blob.cbData);
1247 if (ret && pvData)
1248 CRYPT_FixUpAlgorithmID((CRYPT_ALGORITHM_IDENTIFIER *)pvData);
1250 else
1251 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1252 break;
1254 case CMSG_COMPUTED_HASH_PARAM:
1255 if (!msg->hash)
1257 CRYPT_ALGORITHM_IDENTIFIER *hashAlgoID = NULL;
1258 DWORD size = 0;
1259 ALG_ID algID = 0;
1261 CryptMsgGetParam(msg, CMSG_HASH_ALGORITHM_PARAM, 0, NULL, &size);
1262 hashAlgoID = CryptMemAlloc(size);
1263 ret = CryptMsgGetParam(msg, CMSG_HASH_ALGORITHM_PARAM, 0,
1264 hashAlgoID, &size);
1265 if (ret)
1266 algID = CertOIDToAlgId(hashAlgoID->pszObjId);
1267 ret = CryptCreateHash(msg->crypt_prov, algID, 0, 0, &msg->hash);
1268 if (ret)
1270 CRYPT_DATA_BLOB content;
1272 ret = ContextPropertyList_FindProperty(msg->properties,
1273 CMSG_CONTENT_PARAM, &content);
1274 if (ret)
1275 ret = CryptHashData(msg->hash, content.pbData,
1276 content.cbData, 0);
1278 CryptMemFree(hashAlgoID);
1280 else
1281 ret = TRUE;
1282 if (ret)
1283 ret = CryptGetHashParam(msg->hash, HP_HASHVAL, pvData, pcbData, 0);
1284 break;
1285 default:
1287 CRYPT_DATA_BLOB blob;
1289 ret = ContextPropertyList_FindProperty(msg->properties, dwParamType,
1290 &blob);
1291 if (ret)
1292 ret = CRYPT_CopyParam(pvData, pcbData, blob.pbData, blob.cbData);
1293 else
1294 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1297 return ret;
1300 HCRYPTMSG WINAPI CryptMsgOpenToDecode(DWORD dwMsgEncodingType, DWORD dwFlags,
1301 DWORD dwMsgType, HCRYPTPROV hCryptProv, PCERT_INFO pRecipientInfo,
1302 PCMSG_STREAM_INFO pStreamInfo)
1304 CDecodeMsg *msg;
1306 TRACE("(%08x, %08x, %08x, %08lx, %p, %p)\n", dwMsgEncodingType,
1307 dwFlags, dwMsgType, hCryptProv, pRecipientInfo, pStreamInfo);
1309 if (GET_CMSG_ENCODING_TYPE(dwMsgEncodingType) != PKCS_7_ASN_ENCODING)
1311 SetLastError(E_INVALIDARG);
1312 return NULL;
1314 msg = CryptMemAlloc(sizeof(CDecodeMsg));
1315 if (msg)
1317 CryptMsgBase_Init((CryptMsgBase *)msg, dwFlags, pStreamInfo,
1318 CDecodeMsg_Close, CDecodeMsg_GetParam, CDecodeMsg_Update);
1319 msg->type = dwMsgType;
1320 if (hCryptProv)
1321 msg->crypt_prov = hCryptProv;
1322 else
1324 msg->crypt_prov = CRYPT_GetDefaultProvider();
1325 msg->base.open_flags &= ~CMSG_CRYPT_RELEASE_CONTEXT_FLAG;
1327 msg->hash = 0;
1328 msg->msg_data.cbData = 0;
1329 msg->msg_data.pbData = NULL;
1330 msg->properties = ContextPropertyList_Create();
1332 return msg;
1335 HCRYPTMSG WINAPI CryptMsgDuplicate(HCRYPTMSG hCryptMsg)
1337 TRACE("(%p)\n", hCryptMsg);
1339 if (hCryptMsg)
1341 CryptMsgBase *msg = (CryptMsgBase *)hCryptMsg;
1343 InterlockedIncrement(&msg->ref);
1345 return hCryptMsg;
1348 BOOL WINAPI CryptMsgClose(HCRYPTMSG hCryptMsg)
1350 TRACE("(%p)\n", hCryptMsg);
1352 if (hCryptMsg)
1354 CryptMsgBase *msg = (CryptMsgBase *)hCryptMsg;
1356 if (InterlockedDecrement(&msg->ref) == 0)
1358 TRACE("freeing %p\n", msg);
1359 if (msg->close)
1360 msg->close(msg);
1361 CryptMemFree(msg);
1364 return TRUE;
1367 BOOL WINAPI CryptMsgUpdate(HCRYPTMSG hCryptMsg, const BYTE *pbData,
1368 DWORD cbData, BOOL fFinal)
1370 CryptMsgBase *msg = (CryptMsgBase *)hCryptMsg;
1371 BOOL ret = FALSE;
1373 TRACE("(%p, %p, %d, %d)\n", hCryptMsg, pbData, cbData, fFinal);
1375 if (msg->state == MsgStateFinalized)
1376 SetLastError(CRYPT_E_MSG_ERROR);
1377 else
1379 ret = msg->update(hCryptMsg, pbData, cbData, fFinal);
1380 msg->state = MsgStateUpdated;
1381 if (fFinal)
1382 msg->state = MsgStateFinalized;
1384 return ret;
1387 BOOL WINAPI CryptMsgGetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
1388 DWORD dwIndex, void *pvData, DWORD *pcbData)
1390 CryptMsgBase *msg = (CryptMsgBase *)hCryptMsg;
1392 TRACE("(%p, %d, %d, %p, %p)\n", hCryptMsg, dwParamType, dwIndex,
1393 pvData, pcbData);
1394 return msg->get_param(hCryptMsg, dwParamType, dwIndex, pvData, pcbData);