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
20 #include "wine/port.h"
28 #include "wine/debug.h"
29 #include "wine/exception.h"
30 #include "crypt32_private.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
34 /* Called when a message's ref count reaches zero. Free any message-specific
37 typedef void (*CryptMsgCloseFunc)(HCRYPTMSG msg);
39 typedef BOOL (*CryptMsgGetParamFunc)(HCRYPTMSG hCryptMsg, DWORD dwParamType,
40 DWORD dwIndex, void *pvData, DWORD *pcbData);
42 typedef BOOL (*CryptMsgUpdateFunc)(HCRYPTMSG hCryptMsg, const BYTE *pbData,
43 DWORD cbData, BOOL fFinal);
45 typedef BOOL (*CryptMsgControlFunc)(HCRYPTMSG hCryptMsg, DWORD dwFlags,
46 DWORD dwCtrlType, const void *pvCtrlPara);
48 BOOL CRYPT_DefaultMsgControl(HCRYPTMSG hCryptMsg, DWORD dwFlags,
49 DWORD dwCtrlType, const void *pvCtrlPara)
51 TRACE("(%p, %08x, %d, %p)\n", hCryptMsg, dwFlags, dwCtrlType, pvCtrlPara);
52 SetLastError(E_INVALIDARG);
56 typedef enum _CryptMsgState {
59 MsgStateDataFinalized,
63 typedef struct _CryptMsgBase
68 CMSG_STREAM_INFO stream_info;
70 CryptMsgCloseFunc close;
71 CryptMsgUpdateFunc update;
72 CryptMsgGetParamFunc get_param;
73 CryptMsgControlFunc control;
76 static inline void CryptMsgBase_Init(CryptMsgBase *msg, DWORD dwFlags,
77 PCMSG_STREAM_INFO pStreamInfo, CryptMsgCloseFunc close,
78 CryptMsgGetParamFunc get_param, CryptMsgUpdateFunc update,
79 CryptMsgControlFunc control)
82 msg->open_flags = dwFlags;
86 msg->stream_info = *pStreamInfo;
90 msg->streamed = FALSE;
91 memset(&msg->stream_info, 0, sizeof(msg->stream_info));
94 msg->get_param = get_param;
96 msg->control = control;
97 msg->state = MsgStateInit;
100 typedef struct _CDataEncodeMsg
103 DWORD bare_content_len;
107 static const BYTE empty_data_content[] = { 0x04,0x00 };
109 static void CDataEncodeMsg_Close(HCRYPTMSG hCryptMsg)
111 CDataEncodeMsg *msg = (CDataEncodeMsg *)hCryptMsg;
113 if (msg->bare_content != empty_data_content)
114 LocalFree(msg->bare_content);
117 static BOOL WINAPI CRYPT_EncodeContentLength(DWORD dwCertEncodingType,
118 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
119 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
121 DWORD dataLen = *(DWORD *)pvStructInfo;
125 /* Trick: report bytes needed based on total message length, even though
126 * the message isn't available yet. The caller will use the length
127 * reported here to encode its length.
129 CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
131 *pcbEncoded = 1 + lenBytes + dataLen;
134 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
135 pcbEncoded, 1 + lenBytes)))
137 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
138 pbEncoded = *(BYTE **)pbEncoded;
139 *pbEncoded++ = ASN_OCTETSTRING;
140 CRYPT_EncodeLen(dataLen, pbEncoded,
147 static BOOL CRYPT_EncodeDataContentInfoHeader(CDataEncodeMsg *msg,
148 CRYPT_DATA_BLOB *header)
152 if (msg->base.streamed && msg->base.stream_info.cbContent == 0xffffffff)
154 static const BYTE headerValue[] = { 0x30,0x80,0x06,0x09,0x2a,0x86,0x48,
155 0x86,0xf7,0x0d,0x01,0x07,0x01,0xa0,0x80,0x24,0x80 };
157 header->pbData = LocalAlloc(0, sizeof(headerValue));
160 header->cbData = sizeof(headerValue);
161 memcpy(header->pbData, headerValue, sizeof(headerValue));
169 struct AsnConstructedItem constructed = { 0,
170 &msg->base.stream_info.cbContent, CRYPT_EncodeContentLength };
171 struct AsnEncodeSequenceItem items[2] = {
172 { szOID_RSA_data, CRYPT_AsnEncodeOid, 0 },
173 { &constructed, CRYPT_AsnEncodeConstructed, 0 },
176 ret = CRYPT_AsnEncodeSequence(X509_ASN_ENCODING, items,
177 sizeof(items) / sizeof(items[0]), CRYPT_ENCODE_ALLOC_FLAG, NULL,
178 (LPBYTE)&header->pbData, &header->cbData);
181 /* Trick: subtract the content length from the reported length,
182 * as the actual content hasn't come yet.
184 header->cbData -= msg->base.stream_info.cbContent;
190 static BOOL CDataEncodeMsg_Update(HCRYPTMSG hCryptMsg, const BYTE *pbData,
191 DWORD cbData, BOOL fFinal)
193 CDataEncodeMsg *msg = (CDataEncodeMsg *)hCryptMsg;
196 if (msg->base.state == MsgStateFinalized)
197 SetLastError(CRYPT_E_MSG_ERROR);
198 else if (msg->base.streamed)
202 if (msg->base.state != MsgStateUpdated)
204 CRYPT_DATA_BLOB header;
206 ret = CRYPT_EncodeDataContentInfoHeader(msg, &header);
209 ret = msg->base.stream_info.pfnStreamOutput(
210 msg->base.stream_info.pvArg, header.pbData, header.cbData,
212 LocalFree(header.pbData);
215 /* Curiously, every indefinite-length streamed update appears to
216 * get its own tag and length, regardless of fFinal.
218 if (msg->base.stream_info.cbContent == 0xffffffff)
223 ret = CRYPT_EncodeContentLength(X509_ASN_ENCODING, NULL,
224 &cbData, CRYPT_ENCODE_ALLOC_FLAG, NULL, (BYTE *)&header,
228 ret = msg->base.stream_info.pfnStreamOutput(
229 msg->base.stream_info.pvArg, header, headerLen,
236 ret = msg->base.stream_info.pfnStreamOutput(
237 msg->base.stream_info.pvArg, (BYTE *)pbData, cbData,
239 msg->base.state = MsgStateUpdated;
243 msg->base.state = MsgStateFinalized;
244 if (msg->base.stream_info.cbContent == 0xffffffff)
246 BYTE indefinite_trailer[6] = { 0 };
248 ret = msg->base.stream_info.pfnStreamOutput(
249 msg->base.stream_info.pvArg, (BYTE *)pbData, cbData,
252 ret = msg->base.stream_info.pfnStreamOutput(
253 msg->base.stream_info.pvArg, indefinite_trailer,
254 sizeof(indefinite_trailer), TRUE);
257 ret = msg->base.stream_info.pfnStreamOutput(
258 msg->base.stream_info.pvArg, (BYTE *)pbData, cbData, TRUE);
263 SetLastError(STATUS_ACCESS_VIOLATION);
272 if (msg->base.open_flags & CMSG_DETACHED_FLAG)
273 SetLastError(E_INVALIDARG);
275 SetLastError(CRYPT_E_MSG_ERROR);
279 msg->base.state = MsgStateFinalized;
281 SetLastError(E_INVALIDARG);
284 CRYPT_DATA_BLOB blob = { cbData, (LPBYTE)pbData };
286 /* non-streamed data messages don't allow non-final updates,
287 * don't bother checking whether data already exist, they can't.
289 ret = CryptEncodeObjectEx(X509_ASN_ENCODING, X509_OCTET_STRING,
290 &blob, CRYPT_ENCODE_ALLOC_FLAG, NULL, &msg->bare_content,
291 &msg->bare_content_len);
298 static BOOL CRYPT_CopyParam(void *pvData, DWORD *pcbData, const void *src,
305 else if (*pcbData < len)
308 SetLastError(ERROR_MORE_DATA);
314 memcpy(pvData, src, len);
319 static BOOL CDataEncodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
320 DWORD dwIndex, void *pvData, DWORD *pcbData)
322 CDataEncodeMsg *msg = (CDataEncodeMsg *)hCryptMsg;
327 case CMSG_CONTENT_PARAM:
328 if (msg->base.streamed)
329 SetLastError(E_INVALIDARG);
332 CRYPT_CONTENT_INFO info;
333 char rsa_data[] = "1.2.840.113549.1.7.1";
335 info.pszObjId = rsa_data;
336 info.Content.cbData = msg->bare_content_len;
337 info.Content.pbData = msg->bare_content;
338 ret = CryptEncodeObject(X509_ASN_ENCODING, PKCS_CONTENT_INFO, &info,
342 case CMSG_BARE_CONTENT_PARAM:
343 if (msg->base.streamed)
344 SetLastError(E_INVALIDARG);
346 ret = CRYPT_CopyParam(pvData, pcbData, msg->bare_content,
347 msg->bare_content_len);
350 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
355 static HCRYPTMSG CDataEncodeMsg_Open(DWORD dwFlags, const void *pvMsgEncodeInfo,
356 LPSTR pszInnerContentObjID, PCMSG_STREAM_INFO pStreamInfo)
362 SetLastError(E_INVALIDARG);
365 msg = CryptMemAlloc(sizeof(CDataEncodeMsg));
368 CryptMsgBase_Init((CryptMsgBase *)msg, dwFlags, pStreamInfo,
369 CDataEncodeMsg_Close, CDataEncodeMsg_GetParam, CDataEncodeMsg_Update,
370 CRYPT_DefaultMsgControl);
371 msg->bare_content_len = sizeof(empty_data_content);
372 msg->bare_content = (LPBYTE)empty_data_content;
374 return (HCRYPTMSG)msg;
377 typedef struct _CHashEncodeMsg
382 CRYPT_DATA_BLOB data;
385 static void CHashEncodeMsg_Close(HCRYPTMSG hCryptMsg)
387 CHashEncodeMsg *msg = (CHashEncodeMsg *)hCryptMsg;
389 CryptMemFree(msg->data.pbData);
390 CryptDestroyHash(msg->hash);
391 if (msg->base.open_flags & CMSG_CRYPT_RELEASE_CONTEXT_FLAG)
392 CryptReleaseContext(msg->prov, 0);
395 static BOOL CRYPT_EncodePKCSDigestedData(CHashEncodeMsg *msg, void *pvData,
400 DWORD size = sizeof(algID);
402 ret = CryptGetHashParam(msg->hash, HP_ALGID, (BYTE *)&algID, &size, 0);
405 CRYPT_DIGESTED_DATA digestedData = { 0 };
406 char oid_rsa_data[] = szOID_RSA_data;
408 digestedData.version = CMSG_HASHED_DATA_PKCS_1_5_VERSION;
409 digestedData.DigestAlgorithm.pszObjId = (LPSTR)CertAlgIdToOID(algID);
410 /* FIXME: what about digestedData.DigestAlgorithm.Parameters? */
411 /* Quirk: OID is only encoded messages if an update has happened */
412 if (msg->base.state != MsgStateInit)
413 digestedData.ContentInfo.pszObjId = oid_rsa_data;
414 if (!(msg->base.open_flags & CMSG_DETACHED_FLAG) && msg->data.cbData)
416 ret = CRYPT_AsnEncodeOctets(0, NULL, &msg->data,
417 CRYPT_ENCODE_ALLOC_FLAG, NULL,
418 (LPBYTE)&digestedData.ContentInfo.Content.pbData,
419 &digestedData.ContentInfo.Content.cbData);
421 if (msg->base.state == MsgStateFinalized)
423 size = sizeof(DWORD);
424 ret = CryptGetHashParam(msg->hash, HP_HASHSIZE,
425 (LPBYTE)&digestedData.hash.cbData, &size, 0);
428 digestedData.hash.pbData = CryptMemAlloc(
429 digestedData.hash.cbData);
430 ret = CryptGetHashParam(msg->hash, HP_HASHVAL,
431 digestedData.hash.pbData, &digestedData.hash.cbData, 0);
435 ret = CRYPT_AsnEncodePKCSDigestedData(&digestedData, pvData,
437 CryptMemFree(digestedData.hash.pbData);
438 LocalFree(digestedData.ContentInfo.Content.pbData);
443 static BOOL CHashEncodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
444 DWORD dwIndex, void *pvData, DWORD *pcbData)
446 CHashEncodeMsg *msg = (CHashEncodeMsg *)hCryptMsg;
449 TRACE("(%p, %d, %d, %p, %p)\n", hCryptMsg, dwParamType, dwIndex,
454 case CMSG_BARE_CONTENT_PARAM:
455 if (msg->base.streamed)
456 SetLastError(E_INVALIDARG);
458 ret = CRYPT_EncodePKCSDigestedData(msg, pvData, pcbData);
460 case CMSG_CONTENT_PARAM:
462 CRYPT_CONTENT_INFO info;
464 ret = CryptMsgGetParam(hCryptMsg, CMSG_BARE_CONTENT_PARAM, 0, NULL,
465 &info.Content.cbData);
468 info.Content.pbData = CryptMemAlloc(info.Content.cbData);
469 if (info.Content.pbData)
471 ret = CryptMsgGetParam(hCryptMsg, CMSG_BARE_CONTENT_PARAM, 0,
472 info.Content.pbData, &info.Content.cbData);
475 char oid_rsa_hashed[] = szOID_RSA_hashedData;
477 info.pszObjId = oid_rsa_hashed;
478 ret = CryptEncodeObjectEx(X509_ASN_ENCODING,
479 PKCS_CONTENT_INFO, &info, 0, NULL, pvData, pcbData);
481 CryptMemFree(info.Content.pbData);
488 case CMSG_COMPUTED_HASH_PARAM:
489 ret = CryptGetHashParam(msg->hash, HP_HASHVAL, (BYTE *)pvData, pcbData,
492 case CMSG_VERSION_PARAM:
493 if (msg->base.state != MsgStateFinalized)
494 SetLastError(CRYPT_E_MSG_ERROR);
497 DWORD version = CMSG_HASHED_DATA_PKCS_1_5_VERSION;
499 /* Since the data are always encoded as octets, the version is
500 * always 0 (see rfc3852, section 7)
502 ret = CRYPT_CopyParam(pvData, pcbData, &version, sizeof(version));
506 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
511 static BOOL CHashEncodeMsg_Update(HCRYPTMSG hCryptMsg, const BYTE *pbData,
512 DWORD cbData, BOOL fFinal)
514 CHashEncodeMsg *msg = (CHashEncodeMsg *)hCryptMsg;
517 TRACE("(%p, %p, %d, %d)\n", hCryptMsg, pbData, cbData, fFinal);
519 if (msg->base.state == MsgStateFinalized)
520 SetLastError(CRYPT_E_MSG_ERROR);
521 else if (msg->base.streamed || (msg->base.open_flags & CMSG_DETACHED_FLAG))
523 /* Doesn't do much, as stream output is never called, and you
524 * can't get the content.
526 ret = CryptHashData(msg->hash, pbData, cbData, 0);
527 msg->base.state = fFinal ? MsgStateFinalized : MsgStateUpdated;
532 SetLastError(CRYPT_E_MSG_ERROR);
535 ret = CryptHashData(msg->hash, pbData, cbData, 0);
538 msg->data.pbData = CryptMemAlloc(cbData);
539 if (msg->data.pbData)
541 memcpy(msg->data.pbData + msg->data.cbData, pbData, cbData);
542 msg->data.cbData += cbData;
547 msg->base.state = MsgStateFinalized;
553 static HCRYPTMSG CHashEncodeMsg_Open(DWORD dwFlags, const void *pvMsgEncodeInfo,
554 LPSTR pszInnerContentObjID, PCMSG_STREAM_INFO pStreamInfo)
557 const CMSG_HASHED_ENCODE_INFO *info =
558 (const CMSG_HASHED_ENCODE_INFO *)pvMsgEncodeInfo;
562 if (info->cbSize != sizeof(CMSG_HASHED_ENCODE_INFO))
564 SetLastError(E_INVALIDARG);
567 if (!(algID = CertOIDToAlgId(info->HashAlgorithm.pszObjId)))
569 SetLastError(CRYPT_E_UNKNOWN_ALGO);
572 if (info->hCryptProv)
573 prov = info->hCryptProv;
576 prov = CRYPT_GetDefaultProvider();
577 dwFlags &= ~CMSG_CRYPT_RELEASE_CONTEXT_FLAG;
579 msg = CryptMemAlloc(sizeof(CHashEncodeMsg));
582 CryptMsgBase_Init((CryptMsgBase *)msg, dwFlags, pStreamInfo,
583 CHashEncodeMsg_Close, CHashEncodeMsg_GetParam, CHashEncodeMsg_Update,
584 CRYPT_DefaultMsgControl);
586 msg->data.cbData = 0;
587 msg->data.pbData = NULL;
588 if (!CryptCreateHash(prov, algID, 0, 0, &msg->hash))
594 return (HCRYPTMSG)msg;
597 typedef struct _CMSG_SIGNER_ENCODE_INFO_WITH_CMS
600 PCERT_INFO pCertInfo;
601 HCRYPTPROV hCryptProv;
603 CRYPT_ALGORITHM_IDENTIFIER HashAlgorithm;
606 PCRYPT_ATTRIBUTE rgAuthAttr;
608 PCRYPT_ATTRIBUTE rgUnauthAttr;
610 CRYPT_ALGORITHM_IDENTIFIER HashEncryptionAlgorithm;
611 void *pvHashEncryptionAuxInfo;
612 } CMSG_SIGNER_ENCODE_INFO_WITH_CMS, *PCMSG_SIGNER_ENCODE_INFO_WITH_CMS;
614 typedef struct _CMSG_SIGNED_ENCODE_INFO_WITH_CMS
618 PCMSG_SIGNER_ENCODE_INFO_WITH_CMS rgSigners;
620 PCERT_BLOB rgCertEncoded;
622 PCRL_BLOB rgCrlEncoded;
623 DWORD cAttrCertEncoded;
624 PCERT_BLOB rgAttrCertEncoded;
625 } CMSG_SIGNED_ENCODE_INFO_WITH_CMS, *PCMSG_SIGNED_ENCODE_INFO_WITH_CMS;
627 static BOOL CRYPT_IsValidSigner(CMSG_SIGNER_ENCODE_INFO_WITH_CMS *signer)
629 if (signer->cbSize != sizeof(CMSG_SIGNER_ENCODE_INFO) &&
630 signer->cbSize != sizeof(CMSG_SIGNER_ENCODE_INFO_WITH_CMS))
632 SetLastError(E_INVALIDARG);
635 if (signer->cbSize == sizeof(CMSG_SIGNER_ENCODE_INFO_WITH_CMS))
637 FIXME("CMSG_SIGNER_ENCODE_INFO with CMS fields unsupported\n");
640 if (!signer->pCertInfo->SerialNumber.cbData)
642 SetLastError(E_INVALIDARG);
645 if (!signer->pCertInfo->Issuer.cbData)
647 SetLastError(E_INVALIDARG);
650 if (!signer->hCryptProv)
652 SetLastError(E_INVALIDARG);
655 if (!CertOIDToAlgId(signer->HashAlgorithm.pszObjId))
657 SetLastError(CRYPT_E_UNKNOWN_ALGO);
663 static BOOL CRYPT_ConstructBlob(CRYPT_DATA_BLOB *out, const CRYPT_DATA_BLOB *in)
667 out->cbData = in->cbData;
670 out->pbData = CryptMemAlloc(out->cbData);
672 memcpy(out->pbData, in->pbData, out->cbData);
681 typedef struct _BlobArray
684 PCRYPT_DATA_BLOB blobs;
687 static BOOL CRYPT_ConstructBlobArray(BlobArray *out, const BlobArray *in)
691 out->cBlobs = in->cBlobs;
694 out->blobs = CryptMemAlloc(out->cBlobs * sizeof(CRYPT_DATA_BLOB));
699 memset(out->blobs, 0, out->cBlobs * sizeof(CRYPT_DATA_BLOB));
700 for (i = 0; ret && i < out->cBlobs; i++)
701 ret = CRYPT_ConstructBlob(&out->blobs[i], &in->blobs[i]);
709 static void CRYPT_FreeBlobArray(BlobArray *array)
713 for (i = 0; i < array->cBlobs; i++)
714 CryptMemFree(array->blobs[i].pbData);
715 CryptMemFree(array->blobs);
718 static BOOL CRYPT_ConstructAttribute(CRYPT_ATTRIBUTE *out,
719 const CRYPT_ATTRIBUTE *in)
723 out->pszObjId = CryptMemAlloc(strlen(in->pszObjId) + 1);
726 strcpy(out->pszObjId, in->pszObjId);
727 ret = CRYPT_ConstructBlobArray((BlobArray *)&out->cValue,
728 (const BlobArray *)&in->cValue);
735 static BOOL CRYPT_ConstructAttributes(CRYPT_ATTRIBUTES *out,
736 const CRYPT_ATTRIBUTES *in)
740 out->cAttr = in->cAttr;
743 out->rgAttr = CryptMemAlloc(out->cAttr * sizeof(CRYPT_ATTRIBUTE));
748 memset(out->rgAttr, 0, out->cAttr * sizeof(CRYPT_ATTRIBUTE));
749 for (i = 0; ret && i < out->cAttr; i++)
750 ret = CRYPT_ConstructAttribute(&out->rgAttr[i], &in->rgAttr[i]);
760 /* Constructs a CMSG_SIGNER_INFO from a CMSG_SIGNER_ENCODE_INFO_WITH_CMS. */
761 static BOOL CSignerInfo_Construct(CMSG_SIGNER_INFO *info,
762 CMSG_SIGNER_ENCODE_INFO_WITH_CMS *in)
766 /* Note: needs to change if CMS fields are supported */
767 info->dwVersion = CMSG_SIGNER_INFO_V1;
768 ret = CRYPT_ConstructBlob(&info->Issuer, &in->pCertInfo->Issuer);
770 ret = CRYPT_ConstructBlob(&info->SerialNumber,
771 &in->pCertInfo->SerialNumber);
772 /* Assumption: algorithm IDs will point to static strings, not
773 * stack-based ones, so copying the pointer values is safe.
775 info->HashAlgorithm.pszObjId = in->HashAlgorithm.pszObjId;
777 ret = CRYPT_ConstructBlob(&info->HashAlgorithm.Parameters,
778 &in->HashAlgorithm.Parameters);
779 memset(&info->HashEncryptionAlgorithm, 0,
780 sizeof(info->HashEncryptionAlgorithm));
782 ret = CRYPT_ConstructAttributes(&info->AuthAttrs,
783 (CRYPT_ATTRIBUTES *)&in->cAuthAttr);
785 ret = CRYPT_ConstructAttributes(&info->UnauthAttrs,
786 (CRYPT_ATTRIBUTES *)&in->cUnauthAttr);
790 static void CSignerInfo_Free(CMSG_SIGNER_INFO *info)
794 CryptMemFree(info->Issuer.pbData);
795 CryptMemFree(info->SerialNumber.pbData);
796 CryptMemFree(info->HashAlgorithm.Parameters.pbData);
797 CryptMemFree(info->EncryptedHash.pbData);
798 for (i = 0; i < info->AuthAttrs.cAttr; i++)
800 for (j = 0; j < info->AuthAttrs.rgAttr[i].cValue; j++)
801 CryptMemFree(info->AuthAttrs.rgAttr[i].rgValue[j].pbData);
802 CryptMemFree(info->AuthAttrs.rgAttr[i].rgValue);
803 CryptMemFree(info->AuthAttrs.rgAttr[i].pszObjId);
805 CryptMemFree(info->AuthAttrs.rgAttr);
806 for (i = 0; i < info->UnauthAttrs.cAttr; i++)
808 for (j = 0; j < info->UnauthAttrs.rgAttr[i].cValue; j++)
809 CryptMemFree(info->UnauthAttrs.rgAttr[i].rgValue[j].pbData);
810 CryptMemFree(info->UnauthAttrs.rgAttr[i].rgValue);
811 CryptMemFree(info->UnauthAttrs.rgAttr[i].pszObjId);
813 CryptMemFree(info->UnauthAttrs.rgAttr);
816 typedef struct _CSignerHandles
818 HCRYPTHASH contentHash;
819 HCRYPTHASH authAttrHash;
822 typedef struct _CSignedMsgData
824 CRYPT_SIGNED_INFO *info;
826 CSignerHandles *signerHandles;
829 /* Constructs the signer handles for the signerIndex'th signer of msg_data.
830 * Assumes signerIndex is a valid idnex, and that msg_data's info has already
833 static BOOL CSignedMsgData_ConstructSignerHandles(CSignedMsgData *msg_data,
834 DWORD signerIndex, HCRYPTPROV crypt_prov)
839 algID = CertOIDToAlgId(
840 msg_data->info->rgSignerInfo[signerIndex].HashAlgorithm.pszObjId);
841 ret = CryptCreateHash(crypt_prov, algID, 0, 0,
842 &msg_data->signerHandles->contentHash);
843 if (ret && msg_data->info->rgSignerInfo[signerIndex].AuthAttrs.cAttr > 0)
844 ret = CryptCreateHash(crypt_prov, algID, 0, 0,
845 &msg_data->signerHandles->authAttrHash);
849 /* Allocates a CSignedMsgData's handles. Assumes its info has already been
852 static BOOL CSignedMsgData_AllocateHandles(CSignedMsgData *msg_data)
856 if (msg_data->info->cSignerInfo)
858 msg_data->signerHandles =
859 CryptMemAlloc(msg_data->info->cSignerInfo * sizeof(CSignerHandles));
860 if (msg_data->signerHandles)
862 msg_data->cSignerHandle = msg_data->info->cSignerInfo;
863 memset(msg_data->signerHandles, 0,
864 msg_data->info->cSignerInfo * sizeof(CSignerHandles));
868 msg_data->cSignerHandle = 0;
874 msg_data->cSignerHandle = 0;
875 msg_data->signerHandles = NULL;
880 static void CSignedMsgData_CloseHandles(CSignedMsgData *msg_data)
884 for (i = 0; i < msg_data->cSignerHandle; i++)
886 if (msg_data->signerHandles[i].contentHash)
887 CryptDestroyHash(msg_data->signerHandles[i].contentHash);
888 if (msg_data->signerHandles[i].authAttrHash)
889 CryptDestroyHash(msg_data->signerHandles[i].authAttrHash);
891 CryptMemFree(msg_data->signerHandles);
892 msg_data->signerHandles = NULL;
893 msg_data->cSignerHandle = 0;
896 static BOOL CSignedMsgData_UpdateHash(CSignedMsgData *msg_data,
897 const BYTE *pbData, DWORD cbData)
902 for (i = 0; ret && i < msg_data->cSignerHandle; i++)
903 ret = CryptHashData(msg_data->signerHandles[i].contentHash, pbData,
908 static BOOL CRYPT_AppendAttribute(CRYPT_ATTRIBUTES *out,
909 const CRYPT_ATTRIBUTE *in)
913 out->rgAttr = CryptMemRealloc(out->rgAttr,
914 (out->cAttr + 1) * sizeof(CRYPT_ATTRIBUTE));
917 ret = CRYPT_ConstructAttribute(&out->rgAttr[out->cAttr], in);
924 static BOOL CSignedMsgData_AppendMessageDigestAttribute(
925 CSignedMsgData *msg_data, DWORD signerIndex)
929 CRYPT_HASH_BLOB hash = { 0, NULL }, encodedHash = { 0, NULL };
930 char messageDigest[] = szOID_RSA_messageDigest;
931 CRYPT_ATTRIBUTE messageDigestAttr = { messageDigest, 1, &encodedHash };
933 size = sizeof(DWORD);
934 ret = CryptGetHashParam(
935 msg_data->signerHandles[signerIndex].contentHash, HP_HASHSIZE,
936 (LPBYTE)&hash.cbData, &size, 0);
939 hash.pbData = CryptMemAlloc(hash.cbData);
940 ret = CryptGetHashParam(
941 msg_data->signerHandles[signerIndex].contentHash, HP_HASHVAL,
942 hash.pbData, &hash.cbData, 0);
945 ret = CRYPT_AsnEncodeOctets(0, NULL, &hash, CRYPT_ENCODE_ALLOC_FLAG,
946 NULL, (LPBYTE)&encodedHash.pbData, &encodedHash.cbData);
949 ret = CRYPT_AppendAttribute(
950 &msg_data->info->rgSignerInfo[signerIndex].AuthAttrs,
952 LocalFree(encodedHash.pbData);
955 CryptMemFree(hash.pbData);
965 static BOOL CSignedMsgData_UpdateAuthenticatedAttributes(
966 CSignedMsgData *msg_data, SignOrVerify flag)
971 TRACE("(%p)\n", msg_data);
973 for (i = 0; ret && i < msg_data->info->cSignerInfo; i++)
975 if (msg_data->info->rgSignerInfo[i].AuthAttrs.cAttr)
979 BYTE oid_rsa_data_encoded[] = { 0x06,0x09,0x2a,0x86,0x48,0x86,
980 0xf7,0x0d,0x01,0x07,0x01 };
981 CRYPT_DATA_BLOB content = { sizeof(oid_rsa_data_encoded),
982 oid_rsa_data_encoded };
983 char contentType[] = szOID_RSA_contentType;
984 CRYPT_ATTRIBUTE contentTypeAttr = { contentType, 1, &content };
986 /* FIXME: does this depend on inner OID? */
987 ret = CRYPT_AppendAttribute(
988 &msg_data->info->rgSignerInfo[i].AuthAttrs, &contentTypeAttr);
990 ret = CSignedMsgData_AppendMessageDigestAttribute(msg_data,
998 ret = CryptEncodeObjectEx(X509_ASN_ENCODING, PKCS_ATTRIBUTES,
999 &msg_data->info->rgSignerInfo[i].AuthAttrs,
1000 CRYPT_ENCODE_ALLOC_FLAG, NULL, (LPBYTE)&encodedAttrs, &size);
1003 ret = CryptHashData(
1004 msg_data->signerHandles[i].authAttrHash, encodedAttrs,
1006 LocalFree(encodedAttrs);
1011 TRACE("returning %d\n", ret);
1015 static void CRYPT_ReverseBytes(CRYPT_HASH_BLOB *hash)
1020 for (i = 0; i < hash->cbData / 2; i++)
1022 tmp = hash->pbData[hash->cbData - i - 1];
1023 hash->pbData[hash->cbData - i - 1] = hash->pbData[i];
1024 hash->pbData[i] = tmp;
1028 static BOOL CSignedMsgData_Sign(CSignedMsgData *msg_data)
1033 TRACE("(%p)\n", msg_data);
1035 for (i = 0; ret && i < msg_data->info->cSignerInfo; i++)
1039 if (msg_data->info->rgSignerInfo[i].AuthAttrs.cAttr)
1040 hash = msg_data->signerHandles[i].authAttrHash;
1042 hash = msg_data->signerHandles[i].contentHash;
1043 ret = CryptSignHashW(hash, AT_SIGNATURE, NULL, 0, NULL,
1044 &msg_data->info->rgSignerInfo[i].EncryptedHash.cbData);
1047 msg_data->info->rgSignerInfo[i].EncryptedHash.pbData =
1049 msg_data->info->rgSignerInfo[i].EncryptedHash.cbData);
1050 if (msg_data->info->rgSignerInfo[i].EncryptedHash.pbData)
1052 ret = CryptSignHashW(hash, AT_SIGNATURE, NULL, 0,
1053 msg_data->info->rgSignerInfo[i].EncryptedHash.pbData,
1054 &msg_data->info->rgSignerInfo[i].EncryptedHash.cbData);
1057 &msg_data->info->rgSignerInfo[i].EncryptedHash);
1066 static BOOL CSignedMsgData_Update(CSignedMsgData *msg_data,
1067 const BYTE *pbData, DWORD cbData, BOOL fFinal, SignOrVerify flag)
1069 BOOL ret = CSignedMsgData_UpdateHash(msg_data, pbData, cbData);
1073 ret = CSignedMsgData_UpdateAuthenticatedAttributes(msg_data, flag);
1074 if (ret && flag == Sign)
1075 ret = CSignedMsgData_Sign(msg_data);
1080 typedef struct _CSignedEncodeMsg
1083 CRYPT_DATA_BLOB data;
1084 CSignedMsgData msg_data;
1087 static void CSignedEncodeMsg_Close(HCRYPTMSG hCryptMsg)
1089 CSignedEncodeMsg *msg = (CSignedEncodeMsg *)hCryptMsg;
1092 CryptMemFree(msg->data.pbData);
1093 CRYPT_FreeBlobArray((BlobArray *)&msg->msg_data.info->cCertEncoded);
1094 CRYPT_FreeBlobArray((BlobArray *)&msg->msg_data.info->cCrlEncoded);
1095 for (i = 0; i < msg->msg_data.info->cSignerInfo; i++)
1096 CSignerInfo_Free(&msg->msg_data.info->rgSignerInfo[i]);
1097 CSignedMsgData_CloseHandles(&msg->msg_data);
1098 CryptMemFree(msg->msg_data.info->rgSignerInfo);
1099 CryptMemFree(msg->msg_data.info);
1102 static BOOL CSignedEncodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
1103 DWORD dwIndex, void *pvData, DWORD *pcbData)
1105 CSignedEncodeMsg *msg = (CSignedEncodeMsg *)hCryptMsg;
1108 switch (dwParamType)
1110 case CMSG_CONTENT_PARAM:
1112 CRYPT_CONTENT_INFO info;
1114 ret = CryptMsgGetParam(hCryptMsg, CMSG_BARE_CONTENT_PARAM, 0, NULL,
1115 &info.Content.cbData);
1118 info.Content.pbData = CryptMemAlloc(info.Content.cbData);
1119 if (info.Content.pbData)
1121 ret = CryptMsgGetParam(hCryptMsg, CMSG_BARE_CONTENT_PARAM, 0,
1122 info.Content.pbData, &info.Content.cbData);
1125 char oid_rsa_signed[] = szOID_RSA_signedData;
1127 info.pszObjId = oid_rsa_signed;
1128 ret = CryptEncodeObjectEx(X509_ASN_ENCODING,
1129 PKCS_CONTENT_INFO, &info, 0, NULL, pvData, pcbData);
1131 CryptMemFree(info.Content.pbData);
1138 case CMSG_BARE_CONTENT_PARAM:
1140 CRYPT_SIGNED_INFO info;
1141 char oid_rsa_data[] = szOID_RSA_data;
1143 info = *msg->msg_data.info;
1144 /* Quirk: OID is only encoded messages if an update has happened */
1145 if (msg->base.state != MsgStateInit)
1146 info.content.pszObjId = oid_rsa_data;
1148 info.content.pszObjId = NULL;
1149 if (msg->data.cbData)
1151 CRYPT_DATA_BLOB blob = { msg->data.cbData, msg->data.pbData };
1153 ret = CryptEncodeObjectEx(X509_ASN_ENCODING, X509_OCTET_STRING,
1154 &blob, CRYPT_ENCODE_ALLOC_FLAG, NULL,
1155 &info.content.Content.pbData, &info.content.Content.cbData);
1159 info.content.Content.cbData = 0;
1160 info.content.Content.pbData = NULL;
1165 ret = CRYPT_AsnEncodePKCSSignedInfo(&info, pvData, pcbData);
1166 LocalFree(info.content.Content.pbData);
1170 case CMSG_COMPUTED_HASH_PARAM:
1171 if (dwIndex >= msg->msg_data.cSignerHandle)
1172 SetLastError(CRYPT_E_INVALID_INDEX);
1174 ret = CryptGetHashParam(
1175 msg->msg_data.signerHandles[dwIndex].contentHash, HP_HASHVAL,
1176 pvData, pcbData, 0);
1178 case CMSG_ENCODED_SIGNER:
1179 if (dwIndex >= msg->msg_data.info->cSignerInfo)
1180 SetLastError(CRYPT_E_INVALID_INDEX);
1182 ret = CryptEncodeObjectEx(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
1183 PKCS7_SIGNER_INFO, &msg->msg_data.info->rgSignerInfo[dwIndex], 0,
1184 NULL, pvData, pcbData);
1186 case CMSG_VERSION_PARAM:
1187 ret = CRYPT_CopyParam(pvData, pcbData, &msg->msg_data.info->version,
1188 sizeof(msg->msg_data.info->version));
1191 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1196 static BOOL CSignedEncodeMsg_Update(HCRYPTMSG hCryptMsg, const BYTE *pbData,
1197 DWORD cbData, BOOL fFinal)
1199 CSignedEncodeMsg *msg = (CSignedEncodeMsg *)hCryptMsg;
1202 if (msg->base.state == MsgStateFinalized)
1203 SetLastError(CRYPT_E_MSG_ERROR);
1204 else if (msg->base.streamed || (msg->base.open_flags & CMSG_DETACHED_FLAG))
1206 ret = CSignedMsgData_Update(&msg->msg_data, pbData, cbData, fFinal,
1208 if (msg->base.streamed)
1209 FIXME("streamed partial stub\n");
1210 msg->base.state = fFinal ? MsgStateFinalized : MsgStateUpdated;
1215 SetLastError(CRYPT_E_MSG_ERROR);
1220 msg->data.pbData = CryptMemAlloc(cbData);
1221 if (msg->data.pbData)
1223 memcpy(msg->data.pbData, pbData, cbData);
1224 msg->data.cbData = cbData;
1231 ret = CSignedMsgData_Update(&msg->msg_data, pbData, cbData,
1233 msg->base.state = MsgStateFinalized;
1239 static HCRYPTMSG CSignedEncodeMsg_Open(DWORD dwFlags,
1240 const void *pvMsgEncodeInfo, LPSTR pszInnerContentObjID,
1241 PCMSG_STREAM_INFO pStreamInfo)
1243 const CMSG_SIGNED_ENCODE_INFO_WITH_CMS *info =
1244 (const CMSG_SIGNED_ENCODE_INFO_WITH_CMS *)pvMsgEncodeInfo;
1246 CSignedEncodeMsg *msg;
1248 if (info->cbSize != sizeof(CMSG_SIGNED_ENCODE_INFO) &&
1249 info->cbSize != sizeof(CMSG_SIGNED_ENCODE_INFO_WITH_CMS))
1251 SetLastError(E_INVALIDARG);
1254 if (info->cbSize == sizeof(CMSG_SIGNED_ENCODE_INFO_WITH_CMS))
1256 FIXME("CMSG_SIGNED_ENCODE_INFO with CMS fields unsupported\n");
1259 for (i = 0; i < info->cSigners; i++)
1260 if (!CRYPT_IsValidSigner(&info->rgSigners[i]))
1262 msg = CryptMemAlloc(sizeof(CSignedEncodeMsg));
1267 CryptMsgBase_Init((CryptMsgBase *)msg, dwFlags, pStreamInfo,
1268 CSignedEncodeMsg_Close, CSignedEncodeMsg_GetParam,
1269 CSignedEncodeMsg_Update, CRYPT_DefaultMsgControl);
1270 msg->data.cbData = 0;
1271 msg->data.pbData = NULL;
1272 msg->msg_data.info = CryptMemAlloc(sizeof(CRYPT_SIGNED_INFO));
1273 if (msg->msg_data.info)
1275 memset(msg->msg_data.info, 0, sizeof(CRYPT_SIGNED_INFO));
1276 msg->msg_data.info->version = CMSG_SIGNED_DATA_V1;
1284 msg->msg_data.info->rgSignerInfo =
1285 CryptMemAlloc(info->cSigners * sizeof(CMSG_SIGNER_INFO));
1286 if (msg->msg_data.info->rgSignerInfo)
1288 msg->msg_data.info->cSignerInfo = info->cSigners;
1289 memset(msg->msg_data.info->rgSignerInfo, 0,
1290 msg->msg_data.info->cSignerInfo * sizeof(CMSG_SIGNER_INFO));
1291 ret = CSignedMsgData_AllocateHandles(&msg->msg_data);
1292 for (i = 0; ret && i < msg->msg_data.info->cSignerInfo; i++)
1294 ret = CSignerInfo_Construct(
1295 &msg->msg_data.info->rgSignerInfo[i],
1296 &info->rgSigners[i]);
1299 ret = CSignedMsgData_ConstructSignerHandles(
1300 &msg->msg_data, i, info->rgSigners[i].hCryptProv);
1301 if (dwFlags & CMSG_CRYPT_RELEASE_CONTEXT_FLAG)
1302 CryptReleaseContext(info->rgSigners[i].hCryptProv,
1312 msg->msg_data.info->cSignerInfo = 0;
1313 msg->msg_data.signerHandles = NULL;
1314 msg->msg_data.cSignerHandle = 0;
1318 ret = CRYPT_ConstructBlobArray(
1319 (BlobArray *)&msg->msg_data.info->cCertEncoded,
1320 (const BlobArray *)&info->cCertEncoded);
1322 ret = CRYPT_ConstructBlobArray(
1323 (BlobArray *)&msg->msg_data.info->cCrlEncoded,
1324 (const BlobArray *)&info->cCrlEncoded);
1327 CSignedEncodeMsg_Close(msg);
1334 static inline const char *MSG_TYPE_STR(DWORD type)
1338 #define _x(x) case (x): return #x
1342 _x(CMSG_SIGNED_AND_ENVELOPED);
1347 return wine_dbg_sprintf("unknown (%d)", type);
1351 HCRYPTMSG WINAPI CryptMsgOpenToEncode(DWORD dwMsgEncodingType, DWORD dwFlags,
1352 DWORD dwMsgType, const void *pvMsgEncodeInfo, LPSTR pszInnerContentObjID,
1353 PCMSG_STREAM_INFO pStreamInfo)
1355 HCRYPTMSG msg = NULL;
1357 TRACE("(%08x, %08x, %08x, %p, %s, %p)\n", dwMsgEncodingType, dwFlags,
1358 dwMsgType, pvMsgEncodeInfo, debugstr_a(pszInnerContentObjID), pStreamInfo);
1360 if (GET_CMSG_ENCODING_TYPE(dwMsgEncodingType) != PKCS_7_ASN_ENCODING)
1362 SetLastError(E_INVALIDARG);
1368 msg = CDataEncodeMsg_Open(dwFlags, pvMsgEncodeInfo,
1369 pszInnerContentObjID, pStreamInfo);
1372 msg = CHashEncodeMsg_Open(dwFlags, pvMsgEncodeInfo,
1373 pszInnerContentObjID, pStreamInfo);
1376 msg = CSignedEncodeMsg_Open(dwFlags, pvMsgEncodeInfo,
1377 pszInnerContentObjID, pStreamInfo);
1379 case CMSG_ENVELOPED:
1380 FIXME("unimplemented for type %s\n", MSG_TYPE_STR(dwMsgType));
1382 case CMSG_SIGNED_AND_ENVELOPED:
1383 case CMSG_ENCRYPTED:
1384 /* defined but invalid, fall through */
1386 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1391 typedef struct _CDecodeMsg
1395 HCRYPTPROV crypt_prov;
1398 CSignedMsgData signed_data;
1400 CRYPT_DATA_BLOB msg_data;
1401 PCONTEXT_PROPERTY_LIST properties;
1404 static void CDecodeMsg_Close(HCRYPTMSG hCryptMsg)
1406 CDecodeMsg *msg = (CDecodeMsg *)hCryptMsg;
1408 if (msg->base.open_flags & CMSG_CRYPT_RELEASE_CONTEXT_FLAG)
1409 CryptReleaseContext(msg->crypt_prov, 0);
1414 CryptDestroyHash(msg->u.hash);
1417 if (msg->u.signed_data.info)
1419 LocalFree(msg->u.signed_data.info);
1420 CSignedMsgData_CloseHandles(&msg->u.signed_data);
1424 CryptMemFree(msg->msg_data.pbData);
1425 ContextPropertyList_Free(msg->properties);
1428 static BOOL CDecodeMsg_CopyData(CDecodeMsg *msg, const BYTE *pbData,
1435 if (msg->msg_data.cbData)
1436 msg->msg_data.pbData = CryptMemRealloc(msg->msg_data.pbData,
1437 msg->msg_data.cbData + cbData);
1439 msg->msg_data.pbData = CryptMemAlloc(cbData);
1440 if (msg->msg_data.pbData)
1442 memcpy(msg->msg_data.pbData + msg->msg_data.cbData, pbData, cbData);
1443 msg->msg_data.cbData += cbData;
1451 static BOOL CDecodeMsg_DecodeDataContent(CDecodeMsg *msg, CRYPT_DER_BLOB *blob)
1454 CRYPT_DATA_BLOB *data;
1457 ret = CryptDecodeObjectEx(X509_ASN_ENCODING, X509_OCTET_STRING,
1458 blob->pbData, blob->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, (LPBYTE)&data,
1462 ret = ContextPropertyList_SetProperty(msg->properties,
1463 CMSG_CONTENT_PARAM, data->pbData, data->cbData);
1469 static void CDecodeMsg_SaveAlgorithmID(CDecodeMsg *msg, DWORD param,
1470 const CRYPT_ALGORITHM_IDENTIFIER *id)
1472 static const BYTE nullParams[] = { ASN_NULL, 0 };
1473 CRYPT_ALGORITHM_IDENTIFIER *copy;
1474 DWORD len = sizeof(CRYPT_ALGORITHM_IDENTIFIER);
1476 /* Linearize algorithm id */
1477 len += strlen(id->pszObjId) + 1;
1478 len += id->Parameters.cbData;
1479 copy = CryptMemAlloc(len);
1483 (LPSTR)((BYTE *)copy + sizeof(CRYPT_ALGORITHM_IDENTIFIER));
1484 strcpy(copy->pszObjId, id->pszObjId);
1485 copy->Parameters.pbData = (BYTE *)copy->pszObjId + strlen(id->pszObjId)
1487 /* Trick: omit NULL parameters */
1488 if (id->Parameters.cbData == sizeof(nullParams) &&
1489 !memcmp(id->Parameters.pbData, nullParams, sizeof(nullParams)))
1491 copy->Parameters.cbData = 0;
1492 len -= sizeof(nullParams);
1495 copy->Parameters.cbData = id->Parameters.cbData;
1496 if (copy->Parameters.cbData)
1497 memcpy(copy->Parameters.pbData, id->Parameters.pbData,
1498 id->Parameters.cbData);
1499 ContextPropertyList_SetProperty(msg->properties, param, (BYTE *)copy,
1505 static inline void CRYPT_FixUpAlgorithmID(CRYPT_ALGORITHM_IDENTIFIER *id)
1507 id->pszObjId = (LPSTR)((BYTE *)id + sizeof(CRYPT_ALGORITHM_IDENTIFIER));
1508 id->Parameters.pbData = (BYTE *)id->pszObjId + strlen(id->pszObjId) + 1;
1511 static BOOL CDecodeMsg_DecodeHashedContent(CDecodeMsg *msg,
1512 CRYPT_DER_BLOB *blob)
1515 CRYPT_DIGESTED_DATA *digestedData;
1518 ret = CRYPT_AsnDecodePKCSDigestedData(blob->pbData, blob->cbData,
1519 CRYPT_DECODE_ALLOC_FLAG, NULL, (CRYPT_DIGESTED_DATA *)&digestedData,
1523 ContextPropertyList_SetProperty(msg->properties, CMSG_VERSION_PARAM,
1524 (const BYTE *)&digestedData->version, sizeof(digestedData->version));
1525 CDecodeMsg_SaveAlgorithmID(msg, CMSG_HASH_ALGORITHM_PARAM,
1526 &digestedData->DigestAlgorithm);
1527 ContextPropertyList_SetProperty(msg->properties,
1528 CMSG_INNER_CONTENT_TYPE_PARAM,
1529 (const BYTE *)digestedData->ContentInfo.pszObjId,
1530 digestedData->ContentInfo.pszObjId ?
1531 strlen(digestedData->ContentInfo.pszObjId) + 1 : 0);
1532 if (digestedData->ContentInfo.Content.cbData)
1533 CDecodeMsg_DecodeDataContent(msg,
1534 &digestedData->ContentInfo.Content);
1536 ContextPropertyList_SetProperty(msg->properties,
1537 CMSG_CONTENT_PARAM, NULL, 0);
1538 ContextPropertyList_SetProperty(msg->properties, CMSG_HASH_DATA_PARAM,
1539 digestedData->hash.pbData, digestedData->hash.cbData);
1540 LocalFree(digestedData);
1545 static BOOL CDecodeMsg_DecodeSignedContent(CDecodeMsg *msg,
1546 CRYPT_DER_BLOB *blob)
1549 CRYPT_SIGNED_INFO *signedInfo;
1552 ret = CRYPT_AsnDecodePKCSSignedInfo(blob->pbData, blob->cbData,
1553 CRYPT_DECODE_ALLOC_FLAG, NULL, (CRYPT_SIGNED_INFO *)&signedInfo,
1559 msg->u.signed_data.info = signedInfo;
1560 ret = CSignedMsgData_AllocateHandles(&msg->u.signed_data);
1561 for (i = 0; ret && i < msg->u.signed_data.info->cSignerInfo; i++)
1562 ret = CSignedMsgData_ConstructSignerHandles(&msg->u.signed_data, i,
1566 /* Now that we have all the content, update the hash handles with
1567 * it. Have to decode it if the type is szOID_RSA_data.
1569 if (msg->u.signed_data.info->content.Content.cbData)
1571 if (!strcmp(msg->u.signed_data.info->content.pszObjId,
1574 CRYPT_DATA_BLOB *blob;
1576 ret = CryptDecodeObjectEx(X509_ASN_ENCODING,
1578 msg->u.signed_data.info->content.Content.pbData,
1579 msg->u.signed_data.info->content.Content.cbData,
1580 CRYPT_DECODE_ALLOC_FLAG, NULL, (LPBYTE)&blob, &size);
1583 ret = CSignedMsgData_Update(&msg->u.signed_data,
1584 blob->pbData, blob->cbData, TRUE, Verify);
1589 ret = CSignedMsgData_Update(&msg->u.signed_data,
1590 msg->u.signed_data.info->content.Content.pbData,
1591 msg->u.signed_data.info->content.Content.cbData, TRUE,
1598 /* Decodes the content in blob as the type given, and updates the value
1599 * (type, parameters, etc.) of msg based on what blob contains.
1600 * It doesn't just use msg's type, to allow a recursive call from an implicitly
1601 * typed message once the outer content info has been decoded.
1603 static BOOL CDecodeMsg_DecodeContent(CDecodeMsg *msg, CRYPT_DER_BLOB *blob,
1611 if ((ret = CDecodeMsg_DecodeDataContent(msg, blob)))
1612 msg->type = CMSG_DATA;
1615 if ((ret = CDecodeMsg_DecodeHashedContent(msg, blob)))
1616 msg->type = CMSG_HASHED;
1618 case CMSG_ENVELOPED:
1619 FIXME("unimplemented for type %s\n", MSG_TYPE_STR(type));
1623 if ((ret = CDecodeMsg_DecodeSignedContent(msg, blob)))
1624 msg->type = CMSG_SIGNED;
1628 CRYPT_CONTENT_INFO *info;
1631 ret = CryptDecodeObjectEx(X509_ASN_ENCODING, PKCS_CONTENT_INFO,
1632 msg->msg_data.pbData, msg->msg_data.cbData, CRYPT_DECODE_ALLOC_FLAG,
1633 NULL, (LPBYTE)&info, &size);
1636 if (!strcmp(info->pszObjId, szOID_RSA_data))
1637 ret = CDecodeMsg_DecodeContent(msg, &info->Content, CMSG_DATA);
1638 else if (!strcmp(info->pszObjId, szOID_RSA_digestedData))
1639 ret = CDecodeMsg_DecodeContent(msg, &info->Content,
1641 else if (!strcmp(info->pszObjId, szOID_RSA_envelopedData))
1642 ret = CDecodeMsg_DecodeContent(msg, &info->Content,
1644 else if (!strcmp(info->pszObjId, szOID_RSA_signedData))
1645 ret = CDecodeMsg_DecodeContent(msg, &info->Content,
1649 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1659 static BOOL CDecodeMsg_Update(HCRYPTMSG hCryptMsg, const BYTE *pbData,
1660 DWORD cbData, BOOL fFinal)
1662 CDecodeMsg *msg = (CDecodeMsg *)hCryptMsg;
1665 TRACE("(%p, %p, %d, %d)\n", hCryptMsg, pbData, cbData, fFinal);
1667 if (msg->base.state == MsgStateFinalized)
1668 SetLastError(CRYPT_E_MSG_ERROR);
1669 else if (msg->base.streamed)
1671 FIXME("(%p, %p, %d, %d): streamed update stub\n", hCryptMsg, pbData,
1675 if (msg->base.open_flags & CMSG_DETACHED_FLAG &&
1676 msg->base.state != MsgStateDataFinalized)
1678 ret = CDecodeMsg_CopyData(msg, pbData, cbData);
1679 msg->base.state = MsgStateDataFinalized;
1681 ret = CDecodeMsg_DecodeContent(msg, &msg->msg_data,
1686 FIXME("(%p, %p, %d, %d): detached update stub\n", hCryptMsg,
1687 pbData, cbData, fFinal);
1689 msg->base.state = MsgStateFinalized;
1694 ret = CDecodeMsg_CopyData(msg, pbData, cbData);
1695 if (msg->base.state == MsgStateInit)
1696 msg->base.state = MsgStateUpdated;
1702 SetLastError(CRYPT_E_MSG_ERROR);
1705 if (msg->base.state == MsgStateInit)
1707 ret = CDecodeMsg_CopyData(msg, pbData, cbData);
1709 ret = CDecodeMsg_DecodeContent(msg, &msg->msg_data,
1711 if (msg->base.open_flags & CMSG_DETACHED_FLAG)
1712 msg->base.state = MsgStateDataFinalized;
1714 msg->base.state = MsgStateFinalized;
1716 else if (msg->base.state == MsgStateDataFinalized)
1718 FIXME("(%p, %p, %d, %d): detached update stub\n", hCryptMsg,
1719 pbData, cbData, fFinal);
1721 msg->base.state = MsgStateFinalized;
1728 static BOOL CDecodeHashMsg_GetParam(CDecodeMsg *msg, DWORD dwParamType,
1729 DWORD dwIndex, void *pvData, DWORD *pcbData)
1733 switch (dwParamType)
1735 case CMSG_TYPE_PARAM:
1736 ret = CRYPT_CopyParam(pvData, pcbData, &msg->type, sizeof(msg->type));
1738 case CMSG_HASH_ALGORITHM_PARAM:
1740 CRYPT_DATA_BLOB blob;
1742 ret = ContextPropertyList_FindProperty(msg->properties, dwParamType,
1746 ret = CRYPT_CopyParam(pvData, pcbData, blob.pbData, blob.cbData);
1748 CRYPT_FixUpAlgorithmID((CRYPT_ALGORITHM_IDENTIFIER *)pvData);
1751 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1754 case CMSG_COMPUTED_HASH_PARAM:
1757 CRYPT_ALGORITHM_IDENTIFIER *hashAlgoID = NULL;
1761 CryptMsgGetParam(msg, CMSG_HASH_ALGORITHM_PARAM, 0, NULL, &size);
1762 hashAlgoID = CryptMemAlloc(size);
1763 ret = CryptMsgGetParam(msg, CMSG_HASH_ALGORITHM_PARAM, 0,
1766 algID = CertOIDToAlgId(hashAlgoID->pszObjId);
1767 ret = CryptCreateHash(msg->crypt_prov, algID, 0, 0, &msg->u.hash);
1770 CRYPT_DATA_BLOB content;
1772 ret = ContextPropertyList_FindProperty(msg->properties,
1773 CMSG_CONTENT_PARAM, &content);
1775 ret = CryptHashData(msg->u.hash, content.pbData,
1778 CryptMemFree(hashAlgoID);
1783 ret = CryptGetHashParam(msg->u.hash, HP_HASHVAL, pvData, pcbData,
1788 CRYPT_DATA_BLOB blob;
1790 ret = ContextPropertyList_FindProperty(msg->properties, dwParamType,
1793 ret = CRYPT_CopyParam(pvData, pcbData, blob.pbData, blob.cbData);
1795 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1801 /* nextData is an in/out parameter - on input it's the memory location in
1802 * which a copy of in's data should be made, and on output it's the memory
1803 * location immediately after out's copy of in's data.
1805 static inline void CRYPT_CopyBlob(CRYPT_DATA_BLOB *out,
1806 const CRYPT_DATA_BLOB *in, LPBYTE *nextData)
1808 out->cbData = in->cbData;
1811 out->pbData = *nextData;
1812 memcpy(out->pbData, in->pbData, in->cbData);
1813 *nextData += in->cbData;
1817 static inline void CRYPT_CopyAlgorithmId(CRYPT_ALGORITHM_IDENTIFIER *out,
1818 const CRYPT_ALGORITHM_IDENTIFIER *in, LPBYTE *nextData)
1822 out->pszObjId = (LPSTR)*nextData;
1823 strcpy(out->pszObjId, in->pszObjId);
1824 *nextData += strlen(out->pszObjId) + 1;
1826 CRYPT_CopyBlob(&out->Parameters, &in->Parameters, nextData);
1829 static inline void CRYPT_CopyAttributes(CRYPT_ATTRIBUTES *out,
1830 const CRYPT_ATTRIBUTES *in, LPBYTE *nextData)
1832 out->cAttr = in->cAttr;
1837 if ((*nextData - (LPBYTE)0) % sizeof(DWORD_PTR))
1838 *nextData += (*nextData - (LPBYTE)0) % sizeof(DWORD_PTR);
1839 out->rgAttr = (CRYPT_ATTRIBUTE *)*nextData;
1840 *nextData += in->cAttr * sizeof(CRYPT_ATTRIBUTE);
1841 for (i = 0; i < in->cAttr; i++)
1843 if (in->rgAttr[i].pszObjId)
1845 out->rgAttr[i].pszObjId = (LPSTR)*nextData;
1846 strcpy(out->rgAttr[i].pszObjId, in->rgAttr[i].pszObjId);
1847 *nextData += strlen(in->rgAttr[i].pszObjId) + 1;
1849 if (in->rgAttr[i].cValue)
1853 out->rgAttr[i].cValue = in->rgAttr[i].cValue;
1854 if ((*nextData - (LPBYTE)0) % sizeof(DWORD_PTR))
1855 *nextData += (*nextData - (LPBYTE)0) % sizeof(DWORD_PTR);
1856 out->rgAttr[i].rgValue = (PCRYPT_DATA_BLOB)*nextData;
1857 *nextData += in->rgAttr[i].cValue * sizeof(CRYPT_DATA_BLOB);
1858 for (j = 0; j < in->rgAttr[i].cValue; j++)
1859 CRYPT_CopyBlob(&out->rgAttr[i].rgValue[j],
1860 &in->rgAttr[i].rgValue[j], nextData);
1866 static DWORD CRYPT_SizeOfAttributes(const CRYPT_ATTRIBUTES *attr)
1868 DWORD size = attr->cAttr * sizeof(CRYPT_ATTRIBUTE), i, j;
1870 for (i = 0; i < attr->cAttr; i++)
1872 if (attr->rgAttr[i].pszObjId)
1873 size += strlen(attr->rgAttr[i].pszObjId) + 1;
1875 if (size % sizeof(DWORD_PTR))
1876 size += size % sizeof(DWORD_PTR);
1877 size += attr->rgAttr[i].cValue * sizeof(CRYPT_DATA_BLOB);
1878 for (j = 0; j < attr->rgAttr[i].cValue; j++)
1879 size += attr->rgAttr[i].rgValue[j].cbData;
1881 /* align pointer again to be conservative */
1882 if (size % sizeof(DWORD_PTR))
1883 size += size % sizeof(DWORD_PTR);
1887 static BOOL CRYPT_CopySignerInfo(void *pvData, DWORD *pcbData,
1888 const CMSG_SIGNER_INFO *in)
1890 DWORD size = sizeof(CMSG_SIGNER_INFO);
1893 TRACE("(%p, %d, %p)\n", pvData, pvData ? *pcbData : 0, in);
1895 size += in->Issuer.cbData;
1896 size += in->SerialNumber.cbData;
1897 if (in->HashAlgorithm.pszObjId)
1898 size += strlen(in->HashAlgorithm.pszObjId) + 1;
1899 size += in->HashAlgorithm.Parameters.cbData;
1900 if (in->HashEncryptionAlgorithm.pszObjId)
1901 size += strlen(in->HashEncryptionAlgorithm.pszObjId) + 1;
1902 size += in->HashEncryptionAlgorithm.Parameters.cbData;
1903 size += in->EncryptedHash.cbData;
1905 if (size % sizeof(DWORD_PTR))
1906 size += size % sizeof(DWORD_PTR);
1907 size += CRYPT_SizeOfAttributes(&in->AuthAttrs);
1908 size += CRYPT_SizeOfAttributes(&in->UnauthAttrs);
1914 else if (*pcbData < size)
1917 SetLastError(ERROR_MORE_DATA);
1922 LPBYTE nextData = (BYTE *)pvData + sizeof(CMSG_SIGNER_INFO);
1923 CMSG_SIGNER_INFO *out = (CMSG_SIGNER_INFO *)pvData;
1925 out->dwVersion = in->dwVersion;
1926 CRYPT_CopyBlob(&out->Issuer, &in->Issuer, &nextData);
1927 CRYPT_CopyBlob(&out->SerialNumber, &in->SerialNumber, &nextData);
1928 CRYPT_CopyAlgorithmId(&out->HashAlgorithm, &in->HashAlgorithm,
1930 CRYPT_CopyAlgorithmId(&out->HashEncryptionAlgorithm,
1931 &in->HashEncryptionAlgorithm, &nextData);
1932 CRYPT_CopyBlob(&out->EncryptedHash, &in->EncryptedHash, &nextData);
1934 if ((nextData - (LPBYTE)0) % sizeof(DWORD_PTR))
1935 nextData += (nextData - (LPBYTE)0) % sizeof(DWORD_PTR);
1936 CRYPT_CopyAttributes(&out->AuthAttrs, &in->AuthAttrs, &nextData);
1937 CRYPT_CopyAttributes(&out->UnauthAttrs, &in->UnauthAttrs, &nextData);
1940 TRACE("returning %d\n", ret);
1944 static BOOL CRYPT_CopySignerCertInfo(void *pvData, DWORD *pcbData,
1945 const CMSG_SIGNER_INFO *in)
1947 DWORD size = sizeof(CERT_INFO);
1950 TRACE("(%p, %d, %p)\n", pvData, pvData ? *pcbData : 0, in);
1952 size += in->Issuer.cbData;
1953 size += in->SerialNumber.cbData;
1959 else if (*pcbData < size)
1962 SetLastError(ERROR_MORE_DATA);
1967 LPBYTE nextData = (BYTE *)pvData + sizeof(CERT_INFO);
1968 CERT_INFO *out = (CERT_INFO *)pvData;
1970 memset(out, 0, sizeof(CERT_INFO));
1971 CRYPT_CopyBlob(&out->Issuer, &in->Issuer, &nextData);
1972 CRYPT_CopyBlob(&out->SerialNumber, &in->SerialNumber, &nextData);
1975 TRACE("returning %d\n", ret);
1979 static BOOL CDecodeSignedMsg_GetParam(CDecodeMsg *msg, DWORD dwParamType,
1980 DWORD dwIndex, void *pvData, DWORD *pcbData)
1984 switch (dwParamType)
1986 case CMSG_TYPE_PARAM:
1987 ret = CRYPT_CopyParam(pvData, pcbData, &msg->type, sizeof(msg->type));
1989 case CMSG_CONTENT_PARAM:
1990 if (msg->u.signed_data.info)
1992 if (!strcmp(msg->u.signed_data.info->content.pszObjId,
1995 CRYPT_DATA_BLOB *blob;
1998 ret = CryptDecodeObjectEx(X509_ASN_ENCODING, X509_OCTET_STRING,
1999 msg->u.signed_data.info->content.Content.pbData,
2000 msg->u.signed_data.info->content.Content.cbData,
2001 CRYPT_DECODE_ALLOC_FLAG, NULL, (LPBYTE)&blob, &size);
2004 ret = CRYPT_CopyParam(pvData, pcbData, blob->pbData,
2010 ret = CRYPT_CopyParam(pvData, pcbData,
2011 msg->u.signed_data.info->content.Content.pbData,
2012 msg->u.signed_data.info->content.Content.cbData);
2015 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2017 case CMSG_INNER_CONTENT_TYPE_PARAM:
2018 if (msg->u.signed_data.info)
2019 ret = CRYPT_CopyParam(pvData, pcbData,
2020 msg->u.signed_data.info->content.pszObjId,
2021 strlen(msg->u.signed_data.info->content.pszObjId) + 1);
2023 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2025 case CMSG_SIGNER_COUNT_PARAM:
2026 if (msg->u.signed_data.info)
2027 ret = CRYPT_CopyParam(pvData, pcbData,
2028 &msg->u.signed_data.info->cSignerInfo, sizeof(DWORD));
2030 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2032 case CMSG_SIGNER_INFO_PARAM:
2033 if (msg->u.signed_data.info)
2035 if (dwIndex >= msg->u.signed_data.info->cSignerInfo)
2036 SetLastError(CRYPT_E_INVALID_INDEX);
2038 ret = CRYPT_CopySignerInfo(pvData, pcbData,
2039 &msg->u.signed_data.info->rgSignerInfo[dwIndex]);
2042 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2044 case CMSG_SIGNER_CERT_INFO_PARAM:
2045 if (msg->u.signed_data.info)
2047 if (dwIndex >= msg->u.signed_data.info->cSignerInfo)
2048 SetLastError(CRYPT_E_INVALID_INDEX);
2050 ret = CRYPT_CopySignerCertInfo(pvData, pcbData,
2051 &msg->u.signed_data.info->rgSignerInfo[dwIndex]);
2054 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2056 case CMSG_CERT_COUNT_PARAM:
2057 if (msg->u.signed_data.info)
2058 ret = CRYPT_CopyParam(pvData, pcbData,
2059 &msg->u.signed_data.info->cCertEncoded, sizeof(DWORD));
2061 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2063 case CMSG_CERT_PARAM:
2064 if (msg->u.signed_data.info)
2066 if (dwIndex >= msg->u.signed_data.info->cCertEncoded)
2067 SetLastError(CRYPT_E_INVALID_INDEX);
2069 ret = CRYPT_CopyParam(pvData, pcbData,
2070 msg->u.signed_data.info->rgCertEncoded[dwIndex].pbData,
2071 msg->u.signed_data.info->rgCertEncoded[dwIndex].cbData);
2074 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2076 case CMSG_CRL_COUNT_PARAM:
2077 if (msg->u.signed_data.info)
2078 ret = CRYPT_CopyParam(pvData, pcbData,
2079 &msg->u.signed_data.info->cCrlEncoded, sizeof(DWORD));
2081 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2083 case CMSG_CRL_PARAM:
2084 if (msg->u.signed_data.info)
2086 if (dwIndex >= msg->u.signed_data.info->cCrlEncoded)
2087 SetLastError(CRYPT_E_INVALID_INDEX);
2089 ret = CRYPT_CopyParam(pvData, pcbData,
2090 msg->u.signed_data.info->rgCrlEncoded[dwIndex].pbData,
2091 msg->u.signed_data.info->rgCrlEncoded[dwIndex].cbData);
2094 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2096 case CMSG_COMPUTED_HASH_PARAM:
2097 if (msg->u.signed_data.info)
2099 if (dwIndex >= msg->u.signed_data.cSignerHandle)
2100 SetLastError(CRYPT_E_INVALID_INDEX);
2102 ret = CryptGetHashParam(
2103 msg->u.signed_data.signerHandles[dwIndex].contentHash,
2104 HP_HASHVAL, pvData, pcbData, 0);
2107 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2109 case CMSG_ATTR_CERT_COUNT_PARAM:
2110 if (msg->u.signed_data.info)
2112 DWORD attrCertCount = 0;
2114 ret = CRYPT_CopyParam(pvData, pcbData,
2115 &attrCertCount, sizeof(DWORD));
2118 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2120 case CMSG_ATTR_CERT_PARAM:
2121 if (msg->u.signed_data.info)
2122 SetLastError(CRYPT_E_INVALID_INDEX);
2124 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2127 FIXME("unimplemented for %d\n", dwParamType);
2128 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2133 static BOOL CDecodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
2134 DWORD dwIndex, void *pvData, DWORD *pcbData)
2136 CDecodeMsg *msg = (CDecodeMsg *)hCryptMsg;
2142 ret = CDecodeHashMsg_GetParam(msg, dwParamType, dwIndex, pvData,
2146 ret = CDecodeSignedMsg_GetParam(msg, dwParamType, dwIndex, pvData,
2150 switch (dwParamType)
2152 case CMSG_TYPE_PARAM:
2153 ret = CRYPT_CopyParam(pvData, pcbData, &msg->type,
2158 CRYPT_DATA_BLOB blob;
2160 ret = ContextPropertyList_FindProperty(msg->properties, dwParamType,
2163 ret = CRYPT_CopyParam(pvData, pcbData, blob.pbData,
2166 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2173 static BOOL CDecodeHashMsg_VerifyHash(CDecodeMsg *msg)
2176 CRYPT_DATA_BLOB hashBlob;
2178 ret = ContextPropertyList_FindProperty(msg->properties,
2179 CMSG_HASH_DATA_PARAM, &hashBlob);
2182 DWORD computedHashSize = 0;
2184 ret = CDecodeHashMsg_GetParam(msg, CMSG_COMPUTED_HASH_PARAM, 0, NULL,
2186 if (hashBlob.cbData == computedHashSize)
2188 LPBYTE computedHash = CryptMemAlloc(computedHashSize);
2192 ret = CDecodeHashMsg_GetParam(msg, CMSG_COMPUTED_HASH_PARAM, 0,
2193 computedHash, &computedHashSize);
2195 ret = !memcmp(hashBlob.pbData, computedHash,
2197 CryptMemFree(computedHash);
2206 static BOOL CDecodeSignedMsg_VerifySignatureWithKey(CDecodeMsg *msg,
2207 HCRYPTPROV prov, DWORD signerIndex, PCERT_PUBLIC_KEY_INFO keyInfo)
2213 prov = msg->crypt_prov;
2214 ret = CryptImportPublicKeyInfo(prov, X509_ASN_ENCODING, keyInfo, &key);
2218 CRYPT_HASH_BLOB reversedHash;
2220 if (msg->u.signed_data.info->rgSignerInfo[signerIndex].AuthAttrs.cAttr)
2221 hash = msg->u.signed_data.signerHandles[signerIndex].authAttrHash;
2223 hash = msg->u.signed_data.signerHandles[signerIndex].contentHash;
2224 ret = CRYPT_ConstructBlob(&reversedHash,
2225 &msg->u.signed_data.info->rgSignerInfo[signerIndex].EncryptedHash);
2228 CRYPT_ReverseBytes(&reversedHash);
2229 ret = CryptVerifySignatureW(hash, reversedHash.pbData,
2230 reversedHash.cbData, key, NULL, 0);
2231 CryptMemFree(reversedHash.pbData);
2233 CryptDestroyKey(key);
2238 static BOOL CDecodeSignedMsg_VerifySignature(CDecodeMsg *msg, PCERT_INFO info)
2243 for (i = 0; !ret && i < msg->u.signed_data.info->cSignerInfo; i++)
2245 ret = CertCompareCertificateName(X509_ASN_ENCODING,
2246 &msg->u.signed_data.info->rgSignerInfo[i].Issuer, &info->Issuer);
2249 ret = CertCompareIntegerBlob(
2250 &msg->u.signed_data.info->rgSignerInfo[i].SerialNumber,
2251 &info->SerialNumber);
2257 ret = CDecodeSignedMsg_VerifySignatureWithKey(msg, 0, i,
2258 &info->SubjectPublicKeyInfo);
2260 SetLastError(CRYPT_E_SIGNER_NOT_FOUND);
2265 static BOOL CDecodeSignedMsg_VerifySignatureEx(CDecodeMsg *msg,
2266 PCMSG_CTRL_VERIFY_SIGNATURE_EX_PARA para)
2270 if (para->cbSize != sizeof(CMSG_CTRL_VERIFY_SIGNATURE_EX_PARA))
2271 SetLastError(ERROR_INVALID_PARAMETER);
2272 else if (para->dwSignerIndex >= msg->u.signed_data.info->cSignerInfo)
2273 SetLastError(CRYPT_E_SIGNER_NOT_FOUND);
2276 switch (para->dwSignerType)
2278 case CMSG_VERIFY_SIGNER_PUBKEY:
2279 ret = CDecodeSignedMsg_VerifySignatureWithKey(msg,
2280 para->hCryptProv, para->dwSignerIndex,
2281 (PCERT_PUBLIC_KEY_INFO)para->pvSigner);
2283 case CMSG_VERIFY_SIGNER_CERT:
2285 PCCERT_CONTEXT cert = (PCCERT_CONTEXT)para->pvSigner;
2287 ret = CDecodeSignedMsg_VerifySignatureWithKey(msg, para->hCryptProv,
2288 para->dwSignerIndex, &cert->pCertInfo->SubjectPublicKeyInfo);
2292 FIXME("unimplemented for signer type %d\n", para->dwSignerType);
2293 SetLastError(CRYPT_E_SIGNER_NOT_FOUND);
2299 static BOOL CDecodeMsg_Control(HCRYPTMSG hCryptMsg, DWORD dwFlags,
2300 DWORD dwCtrlType, const void *pvCtrlPara)
2302 CDecodeMsg *msg = (CDecodeMsg *)hCryptMsg;
2307 case CMSG_CTRL_VERIFY_SIGNATURE:
2311 ret = CDecodeSignedMsg_VerifySignature(msg, (PCERT_INFO)pvCtrlPara);
2314 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2317 case CMSG_CTRL_DECRYPT:
2321 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2324 case CMSG_CTRL_VERIFY_HASH:
2328 ret = CDecodeHashMsg_VerifyHash(msg);
2331 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2334 case CMSG_CTRL_VERIFY_SIGNATURE_EX:
2338 ret = CDecodeSignedMsg_VerifySignatureEx(msg,
2339 (PCMSG_CTRL_VERIFY_SIGNATURE_EX_PARA)pvCtrlPara);
2342 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2346 SetLastError(CRYPT_E_CONTROL_TYPE);
2351 HCRYPTMSG WINAPI CryptMsgOpenToDecode(DWORD dwMsgEncodingType, DWORD dwFlags,
2352 DWORD dwMsgType, HCRYPTPROV_LEGACY hCryptProv, PCERT_INFO pRecipientInfo,
2353 PCMSG_STREAM_INFO pStreamInfo)
2357 TRACE("(%08x, %08x, %08x, %08lx, %p, %p)\n", dwMsgEncodingType,
2358 dwFlags, dwMsgType, hCryptProv, pRecipientInfo, pStreamInfo);
2360 if (GET_CMSG_ENCODING_TYPE(dwMsgEncodingType) != PKCS_7_ASN_ENCODING)
2362 SetLastError(E_INVALIDARG);
2365 msg = CryptMemAlloc(sizeof(CDecodeMsg));
2368 CryptMsgBase_Init((CryptMsgBase *)msg, dwFlags, pStreamInfo,
2369 CDecodeMsg_Close, CDecodeMsg_GetParam, CDecodeMsg_Update,
2370 CDecodeMsg_Control);
2371 msg->type = dwMsgType;
2373 msg->crypt_prov = hCryptProv;
2376 msg->crypt_prov = CRYPT_GetDefaultProvider();
2377 msg->base.open_flags &= ~CMSG_CRYPT_RELEASE_CONTEXT_FLAG;
2379 memset(&msg->u, 0, sizeof(msg->u));
2380 msg->msg_data.cbData = 0;
2381 msg->msg_data.pbData = NULL;
2382 msg->properties = ContextPropertyList_Create();
2387 HCRYPTMSG WINAPI CryptMsgDuplicate(HCRYPTMSG hCryptMsg)
2389 TRACE("(%p)\n", hCryptMsg);
2393 CryptMsgBase *msg = (CryptMsgBase *)hCryptMsg;
2395 InterlockedIncrement(&msg->ref);
2400 BOOL WINAPI CryptMsgClose(HCRYPTMSG hCryptMsg)
2402 TRACE("(%p)\n", hCryptMsg);
2406 CryptMsgBase *msg = (CryptMsgBase *)hCryptMsg;
2408 if (InterlockedDecrement(&msg->ref) == 0)
2410 TRACE("freeing %p\n", msg);
2419 BOOL WINAPI CryptMsgUpdate(HCRYPTMSG hCryptMsg, const BYTE *pbData,
2420 DWORD cbData, BOOL fFinal)
2422 CryptMsgBase *msg = (CryptMsgBase *)hCryptMsg;
2424 TRACE("(%p, %p, %d, %d)\n", hCryptMsg, pbData, cbData, fFinal);
2426 return msg->update(hCryptMsg, pbData, cbData, fFinal);
2429 BOOL WINAPI CryptMsgGetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
2430 DWORD dwIndex, void *pvData, DWORD *pcbData)
2432 CryptMsgBase *msg = (CryptMsgBase *)hCryptMsg;
2434 TRACE("(%p, %d, %d, %p, %p)\n", hCryptMsg, dwParamType, dwIndex,
2436 return msg->get_param(hCryptMsg, dwParamType, dwIndex, pvData, pcbData);
2439 BOOL WINAPI CryptMsgControl(HCRYPTMSG hCryptMsg, DWORD dwFlags,
2440 DWORD dwCtrlType, const void *pvCtrlPara)
2442 CryptMsgBase *msg = (CryptMsgBase *)hCryptMsg;
2444 TRACE("(%p, %08x, %d, %p)\n", hCryptMsg, dwFlags, dwCtrlType,
2446 return msg->control(hCryptMsg, dwFlags, dwCtrlType, pvCtrlPara);
2449 static CERT_INFO *CRYPT_GetSignerCertInfoFromMsg(HCRYPTMSG msg,
2450 DWORD dwSignerIndex)
2452 CERT_INFO *certInfo = NULL;
2455 if (CryptMsgGetParam(msg, CMSG_SIGNER_CERT_INFO_PARAM, dwSignerIndex, NULL,
2458 certInfo = CryptMemAlloc(size);
2461 if (!CryptMsgGetParam(msg, CMSG_SIGNER_CERT_INFO_PARAM,
2462 dwSignerIndex, certInfo, &size))
2464 CryptMemFree(certInfo);
2472 BOOL WINAPI CryptMsgGetAndVerifySigner(HCRYPTMSG hCryptMsg, DWORD cSignerStore,
2473 HCERTSTORE *rghSignerStore, DWORD dwFlags, PCCERT_CONTEXT *ppSigner,
2474 DWORD *pdwSignerIndex)
2477 DWORD i, signerIndex;
2478 PCCERT_CONTEXT signerCert = NULL;
2481 TRACE("(%p, %d, %p, %08x, %p, %p)\n", hCryptMsg, cSignerStore,
2482 rghSignerStore, dwFlags, ppSigner, pdwSignerIndex);
2484 /* Clear output parameters */
2487 if (pdwSignerIndex && !(dwFlags & CMSG_USE_SIGNER_INDEX_FLAG))
2488 *pdwSignerIndex = 0;
2490 /* Create store to search for signer certificates */
2491 store = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
2492 CERT_STORE_CREATE_NEW_FLAG, NULL);
2493 if (!(dwFlags & CMSG_TRUSTED_SIGNER_FLAG))
2495 HCERTSTORE msgStore = CertOpenStore(CERT_STORE_PROV_MSG, 0, 0, 0,
2498 CertAddStoreToCollection(store, msgStore, 0, 0);
2499 CertCloseStore(msgStore, 0);
2501 for (i = 0; i < cSignerStore; i++)
2502 CertAddStoreToCollection(store, rghSignerStore[i], 0, 0);
2504 /* Find signer cert */
2505 if (dwFlags & CMSG_USE_SIGNER_INDEX_FLAG)
2507 CERT_INFO *signer = CRYPT_GetSignerCertInfoFromMsg(hCryptMsg,
2512 signerIndex = *pdwSignerIndex;
2513 signerCert = CertFindCertificateInStore(store, X509_ASN_ENCODING,
2514 0, CERT_FIND_SUBJECT_CERT, signer, NULL);
2515 CryptMemFree(signer);
2520 DWORD count, size = sizeof(count);
2522 if (CryptMsgGetParam(hCryptMsg, CMSG_SIGNER_COUNT_PARAM, 0, &count,
2525 for (i = 0; !signerCert && i < count; i++)
2527 CERT_INFO *signer = CRYPT_GetSignerCertInfoFromMsg(hCryptMsg,
2532 signerCert = CertFindCertificateInStore(store,
2533 X509_ASN_ENCODING, 0, CERT_FIND_SUBJECT_CERT, signer,
2537 CryptMemFree(signer);
2542 SetLastError(CRYPT_E_NO_TRUSTED_SIGNER);
2546 if (!(dwFlags & CMSG_SIGNER_ONLY_FLAG))
2547 ret = CryptMsgControl(hCryptMsg, 0, CMSG_CTRL_VERIFY_SIGNATURE,
2548 signerCert->pCertInfo);
2554 *ppSigner = CertDuplicateCertificateContext(signerCert);
2556 *pdwSignerIndex = signerIndex;
2558 CertFreeCertificateContext(signerCert);
2561 CertCloseStore(store, 0);