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))
637 if (!signer->pCertInfo->SerialNumber.cbData)
639 SetLastError(E_INVALIDARG);
642 if (!signer->pCertInfo->Issuer.cbData)
644 SetLastError(E_INVALIDARG);
648 else if (signer->cbSize == sizeof(CMSG_SIGNER_ENCODE_INFO_WITH_CMS))
650 switch (signer->SignerId.dwIdChoice)
653 if (!signer->pCertInfo->SerialNumber.cbData)
655 SetLastError(E_INVALIDARG);
658 if (!signer->pCertInfo->Issuer.cbData)
660 SetLastError(E_INVALIDARG);
664 case CERT_ID_ISSUER_SERIAL_NUMBER:
665 if (!signer->SignerId.IssuerSerialNumber.SerialNumber.cbData)
667 SetLastError(E_INVALIDARG);
670 if (!signer->SignerId.IssuerSerialNumber.Issuer.cbData)
672 SetLastError(E_INVALIDARG);
676 case CERT_ID_KEY_IDENTIFIER:
677 if (!signer->SignerId.KeyId.cbData)
679 SetLastError(E_INVALIDARG);
684 SetLastError(E_INVALIDARG);
686 if (signer->HashEncryptionAlgorithm.pszObjId)
688 FIXME("CMSG_SIGNER_ENCODE_INFO with CMS fields unsupported\n");
692 if (!signer->hCryptProv)
694 SetLastError(E_INVALIDARG);
697 if (!CertOIDToAlgId(signer->HashAlgorithm.pszObjId))
699 SetLastError(CRYPT_E_UNKNOWN_ALGO);
705 static BOOL CRYPT_ConstructBlob(CRYPT_DATA_BLOB *out, const CRYPT_DATA_BLOB *in)
709 out->cbData = in->cbData;
712 out->pbData = CryptMemAlloc(out->cbData);
714 memcpy(out->pbData, in->pbData, out->cbData);
723 typedef struct _BlobArray
726 PCRYPT_DATA_BLOB blobs;
729 static BOOL CRYPT_ConstructBlobArray(BlobArray *out, const BlobArray *in)
733 out->cBlobs = in->cBlobs;
736 out->blobs = CryptMemAlloc(out->cBlobs * sizeof(CRYPT_DATA_BLOB));
741 memset(out->blobs, 0, out->cBlobs * sizeof(CRYPT_DATA_BLOB));
742 for (i = 0; ret && i < out->cBlobs; i++)
743 ret = CRYPT_ConstructBlob(&out->blobs[i], &in->blobs[i]);
751 static void CRYPT_FreeBlobArray(BlobArray *array)
755 for (i = 0; i < array->cBlobs; i++)
756 CryptMemFree(array->blobs[i].pbData);
757 CryptMemFree(array->blobs);
760 static BOOL CRYPT_ConstructAttribute(CRYPT_ATTRIBUTE *out,
761 const CRYPT_ATTRIBUTE *in)
765 out->pszObjId = CryptMemAlloc(strlen(in->pszObjId) + 1);
768 strcpy(out->pszObjId, in->pszObjId);
769 ret = CRYPT_ConstructBlobArray((BlobArray *)&out->cValue,
770 (const BlobArray *)&in->cValue);
777 static BOOL CRYPT_ConstructAttributes(CRYPT_ATTRIBUTES *out,
778 const CRYPT_ATTRIBUTES *in)
782 out->cAttr = in->cAttr;
785 out->rgAttr = CryptMemAlloc(out->cAttr * sizeof(CRYPT_ATTRIBUTE));
790 memset(out->rgAttr, 0, out->cAttr * sizeof(CRYPT_ATTRIBUTE));
791 for (i = 0; ret && i < out->cAttr; i++)
792 ret = CRYPT_ConstructAttribute(&out->rgAttr[i], &in->rgAttr[i]);
802 /* Constructs a CMSG_CMS_SIGNER_INFO from a CMSG_SIGNER_ENCODE_INFO_WITH_CMS. */
803 static BOOL CSignerInfo_Construct(CMSG_CMS_SIGNER_INFO *info,
804 const CMSG_SIGNER_ENCODE_INFO_WITH_CMS *in)
808 if (in->cbSize == sizeof(CMSG_SIGNER_ENCODE_INFO))
810 info->dwVersion = CMSG_SIGNER_INFO_V1;
811 ret = CRYPT_ConstructBlob(&info->SignerId.IssuerSerialNumber.Issuer,
812 &in->pCertInfo->Issuer);
814 ret = CRYPT_ConstructBlob(
815 &info->SignerId.IssuerSerialNumber.SerialNumber,
816 &in->pCertInfo->SerialNumber);
817 info->SignerId.dwIdChoice = CERT_ID_ISSUER_SERIAL_NUMBER;
821 /* Implicitly in->cbSize == sizeof(CMSG_SIGNER_ENCODE_INFO_WITH_CMS).
822 * See CRYPT_IsValidSigner.
824 if (!in->SignerId.dwIdChoice)
826 info->dwVersion = CMSG_SIGNER_INFO_V1;
827 ret = CRYPT_ConstructBlob(&info->SignerId.IssuerSerialNumber.Issuer,
828 &in->pCertInfo->Issuer);
830 ret = CRYPT_ConstructBlob(
831 &info->SignerId.IssuerSerialNumber.SerialNumber,
832 &in->pCertInfo->SerialNumber);
833 info->SignerId.dwIdChoice = CERT_ID_ISSUER_SERIAL_NUMBER;
835 else if (in->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
837 info->dwVersion = CMSG_SIGNER_INFO_V1;
838 info->SignerId.dwIdChoice = CERT_ID_ISSUER_SERIAL_NUMBER;
839 ret = CRYPT_ConstructBlob(&info->SignerId.IssuerSerialNumber.Issuer,
840 &in->SignerId.IssuerSerialNumber.Issuer);
842 ret = CRYPT_ConstructBlob(
843 &info->SignerId.IssuerSerialNumber.SerialNumber,
844 &in->SignerId.IssuerSerialNumber.SerialNumber);
848 /* Implicitly dwIdChoice == CERT_ID_KEY_IDENTIFIER */
849 info->dwVersion = CMSG_SIGNER_INFO_V3;
850 info->SignerId.dwIdChoice = CERT_ID_KEY_IDENTIFIER;
851 ret = CRYPT_ConstructBlob(&info->SignerId.KeyId,
852 &in->SignerId.KeyId);
855 /* Assumption: algorithm IDs will point to static strings, not
856 * stack-based ones, so copying the pointer values is safe.
858 info->HashAlgorithm.pszObjId = in->HashAlgorithm.pszObjId;
860 ret = CRYPT_ConstructBlob(&info->HashAlgorithm.Parameters,
861 &in->HashAlgorithm.Parameters);
862 memset(&info->HashEncryptionAlgorithm, 0,
863 sizeof(info->HashEncryptionAlgorithm));
865 ret = CRYPT_ConstructAttributes(&info->AuthAttrs,
866 (CRYPT_ATTRIBUTES *)&in->cAuthAttr);
868 ret = CRYPT_ConstructAttributes(&info->UnauthAttrs,
869 (CRYPT_ATTRIBUTES *)&in->cUnauthAttr);
873 static void CSignerInfo_Free(CMSG_CMS_SIGNER_INFO *info)
877 if (info->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
879 CryptMemFree(info->SignerId.IssuerSerialNumber.Issuer.pbData);
880 CryptMemFree(info->SignerId.IssuerSerialNumber.SerialNumber.pbData);
883 CryptMemFree(info->SignerId.KeyId.pbData);
884 CryptMemFree(info->HashAlgorithm.Parameters.pbData);
885 CryptMemFree(info->EncryptedHash.pbData);
886 for (i = 0; i < info->AuthAttrs.cAttr; i++)
888 for (j = 0; j < info->AuthAttrs.rgAttr[i].cValue; j++)
889 CryptMemFree(info->AuthAttrs.rgAttr[i].rgValue[j].pbData);
890 CryptMemFree(info->AuthAttrs.rgAttr[i].rgValue);
891 CryptMemFree(info->AuthAttrs.rgAttr[i].pszObjId);
893 CryptMemFree(info->AuthAttrs.rgAttr);
894 for (i = 0; i < info->UnauthAttrs.cAttr; i++)
896 for (j = 0; j < info->UnauthAttrs.rgAttr[i].cValue; j++)
897 CryptMemFree(info->UnauthAttrs.rgAttr[i].rgValue[j].pbData);
898 CryptMemFree(info->UnauthAttrs.rgAttr[i].rgValue);
899 CryptMemFree(info->UnauthAttrs.rgAttr[i].pszObjId);
901 CryptMemFree(info->UnauthAttrs.rgAttr);
904 typedef struct _CSignerHandles
906 HCRYPTHASH contentHash;
907 HCRYPTHASH authAttrHash;
910 typedef struct _CSignedMsgData
912 CRYPT_SIGNED_INFO *info;
914 CSignerHandles *signerHandles;
917 /* Constructs the signer handles for the signerIndex'th signer of msg_data.
918 * Assumes signerIndex is a valid idnex, and that msg_data's info has already
921 static BOOL CSignedMsgData_ConstructSignerHandles(CSignedMsgData *msg_data,
922 DWORD signerIndex, HCRYPTPROV crypt_prov)
927 algID = CertOIDToAlgId(
928 msg_data->info->rgSignerInfo[signerIndex].HashAlgorithm.pszObjId);
929 ret = CryptCreateHash(crypt_prov, algID, 0, 0,
930 &msg_data->signerHandles->contentHash);
931 if (ret && msg_data->info->rgSignerInfo[signerIndex].AuthAttrs.cAttr > 0)
932 ret = CryptCreateHash(crypt_prov, algID, 0, 0,
933 &msg_data->signerHandles->authAttrHash);
937 /* Allocates a CSignedMsgData's handles. Assumes its info has already been
940 static BOOL CSignedMsgData_AllocateHandles(CSignedMsgData *msg_data)
944 if (msg_data->info->cSignerInfo)
946 msg_data->signerHandles =
947 CryptMemAlloc(msg_data->info->cSignerInfo * sizeof(CSignerHandles));
948 if (msg_data->signerHandles)
950 msg_data->cSignerHandle = msg_data->info->cSignerInfo;
951 memset(msg_data->signerHandles, 0,
952 msg_data->info->cSignerInfo * sizeof(CSignerHandles));
956 msg_data->cSignerHandle = 0;
962 msg_data->cSignerHandle = 0;
963 msg_data->signerHandles = NULL;
968 static void CSignedMsgData_CloseHandles(CSignedMsgData *msg_data)
972 for (i = 0; i < msg_data->cSignerHandle; i++)
974 if (msg_data->signerHandles[i].contentHash)
975 CryptDestroyHash(msg_data->signerHandles[i].contentHash);
976 if (msg_data->signerHandles[i].authAttrHash)
977 CryptDestroyHash(msg_data->signerHandles[i].authAttrHash);
979 CryptMemFree(msg_data->signerHandles);
980 msg_data->signerHandles = NULL;
981 msg_data->cSignerHandle = 0;
984 static BOOL CSignedMsgData_UpdateHash(CSignedMsgData *msg_data,
985 const BYTE *pbData, DWORD cbData)
990 for (i = 0; ret && i < msg_data->cSignerHandle; i++)
991 ret = CryptHashData(msg_data->signerHandles[i].contentHash, pbData,
996 static BOOL CRYPT_AppendAttribute(CRYPT_ATTRIBUTES *out,
997 const CRYPT_ATTRIBUTE *in)
1001 out->rgAttr = CryptMemRealloc(out->rgAttr,
1002 (out->cAttr + 1) * sizeof(CRYPT_ATTRIBUTE));
1005 ret = CRYPT_ConstructAttribute(&out->rgAttr[out->cAttr], in);
1012 static BOOL CSignedMsgData_AppendMessageDigestAttribute(
1013 CSignedMsgData *msg_data, DWORD signerIndex)
1017 CRYPT_HASH_BLOB hash = { 0, NULL }, encodedHash = { 0, NULL };
1018 char messageDigest[] = szOID_RSA_messageDigest;
1019 CRYPT_ATTRIBUTE messageDigestAttr = { messageDigest, 1, &encodedHash };
1021 size = sizeof(DWORD);
1022 ret = CryptGetHashParam(
1023 msg_data->signerHandles[signerIndex].contentHash, HP_HASHSIZE,
1024 (LPBYTE)&hash.cbData, &size, 0);
1027 hash.pbData = CryptMemAlloc(hash.cbData);
1028 ret = CryptGetHashParam(
1029 msg_data->signerHandles[signerIndex].contentHash, HP_HASHVAL,
1030 hash.pbData, &hash.cbData, 0);
1033 ret = CRYPT_AsnEncodeOctets(0, NULL, &hash, CRYPT_ENCODE_ALLOC_FLAG,
1034 NULL, (LPBYTE)&encodedHash.pbData, &encodedHash.cbData);
1037 ret = CRYPT_AppendAttribute(
1038 &msg_data->info->rgSignerInfo[signerIndex].AuthAttrs,
1039 &messageDigestAttr);
1040 LocalFree(encodedHash.pbData);
1043 CryptMemFree(hash.pbData);
1053 static BOOL CSignedMsgData_UpdateAuthenticatedAttributes(
1054 CSignedMsgData *msg_data, SignOrVerify flag)
1059 TRACE("(%p)\n", msg_data);
1061 for (i = 0; ret && i < msg_data->info->cSignerInfo; i++)
1063 if (msg_data->info->rgSignerInfo[i].AuthAttrs.cAttr)
1067 BYTE oid_rsa_data_encoded[] = { 0x06,0x09,0x2a,0x86,0x48,0x86,
1068 0xf7,0x0d,0x01,0x07,0x01 };
1069 CRYPT_DATA_BLOB content = { sizeof(oid_rsa_data_encoded),
1070 oid_rsa_data_encoded };
1071 char contentType[] = szOID_RSA_contentType;
1072 CRYPT_ATTRIBUTE contentTypeAttr = { contentType, 1, &content };
1074 /* FIXME: does this depend on inner OID? */
1075 ret = CRYPT_AppendAttribute(
1076 &msg_data->info->rgSignerInfo[i].AuthAttrs, &contentTypeAttr);
1078 ret = CSignedMsgData_AppendMessageDigestAttribute(msg_data,
1083 LPBYTE encodedAttrs;
1086 ret = CryptEncodeObjectEx(X509_ASN_ENCODING, PKCS_ATTRIBUTES,
1087 &msg_data->info->rgSignerInfo[i].AuthAttrs,
1088 CRYPT_ENCODE_ALLOC_FLAG, NULL, (LPBYTE)&encodedAttrs, &size);
1091 ret = CryptHashData(
1092 msg_data->signerHandles[i].authAttrHash, encodedAttrs,
1094 LocalFree(encodedAttrs);
1099 TRACE("returning %d\n", ret);
1103 static void CRYPT_ReverseBytes(CRYPT_HASH_BLOB *hash)
1108 for (i = 0; i < hash->cbData / 2; i++)
1110 tmp = hash->pbData[hash->cbData - i - 1];
1111 hash->pbData[hash->cbData - i - 1] = hash->pbData[i];
1112 hash->pbData[i] = tmp;
1116 static BOOL CSignedMsgData_Sign(CSignedMsgData *msg_data)
1121 TRACE("(%p)\n", msg_data);
1123 for (i = 0; ret && i < msg_data->info->cSignerInfo; i++)
1127 if (msg_data->info->rgSignerInfo[i].AuthAttrs.cAttr)
1128 hash = msg_data->signerHandles[i].authAttrHash;
1130 hash = msg_data->signerHandles[i].contentHash;
1131 ret = CryptSignHashW(hash, AT_SIGNATURE, NULL, 0, NULL,
1132 &msg_data->info->rgSignerInfo[i].EncryptedHash.cbData);
1135 msg_data->info->rgSignerInfo[i].EncryptedHash.pbData =
1137 msg_data->info->rgSignerInfo[i].EncryptedHash.cbData);
1138 if (msg_data->info->rgSignerInfo[i].EncryptedHash.pbData)
1140 ret = CryptSignHashW(hash, AT_SIGNATURE, NULL, 0,
1141 msg_data->info->rgSignerInfo[i].EncryptedHash.pbData,
1142 &msg_data->info->rgSignerInfo[i].EncryptedHash.cbData);
1145 &msg_data->info->rgSignerInfo[i].EncryptedHash);
1154 static BOOL CSignedMsgData_Update(CSignedMsgData *msg_data,
1155 const BYTE *pbData, DWORD cbData, BOOL fFinal, SignOrVerify flag)
1157 BOOL ret = CSignedMsgData_UpdateHash(msg_data, pbData, cbData);
1161 ret = CSignedMsgData_UpdateAuthenticatedAttributes(msg_data, flag);
1162 if (ret && flag == Sign)
1163 ret = CSignedMsgData_Sign(msg_data);
1168 typedef struct _CSignedEncodeMsg
1171 CRYPT_DATA_BLOB data;
1172 CSignedMsgData msg_data;
1175 static void CSignedEncodeMsg_Close(HCRYPTMSG hCryptMsg)
1177 CSignedEncodeMsg *msg = (CSignedEncodeMsg *)hCryptMsg;
1180 CryptMemFree(msg->data.pbData);
1181 CRYPT_FreeBlobArray((BlobArray *)&msg->msg_data.info->cCertEncoded);
1182 CRYPT_FreeBlobArray((BlobArray *)&msg->msg_data.info->cCrlEncoded);
1183 for (i = 0; i < msg->msg_data.info->cSignerInfo; i++)
1184 CSignerInfo_Free(&msg->msg_data.info->rgSignerInfo[i]);
1185 CSignedMsgData_CloseHandles(&msg->msg_data);
1186 CryptMemFree(msg->msg_data.info->rgSignerInfo);
1187 CryptMemFree(msg->msg_data.info);
1190 static BOOL CSignedEncodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
1191 DWORD dwIndex, void *pvData, DWORD *pcbData)
1193 CSignedEncodeMsg *msg = (CSignedEncodeMsg *)hCryptMsg;
1196 switch (dwParamType)
1198 case CMSG_CONTENT_PARAM:
1200 CRYPT_CONTENT_INFO info;
1202 ret = CryptMsgGetParam(hCryptMsg, CMSG_BARE_CONTENT_PARAM, 0, NULL,
1203 &info.Content.cbData);
1206 info.Content.pbData = CryptMemAlloc(info.Content.cbData);
1207 if (info.Content.pbData)
1209 ret = CryptMsgGetParam(hCryptMsg, CMSG_BARE_CONTENT_PARAM, 0,
1210 info.Content.pbData, &info.Content.cbData);
1213 char oid_rsa_signed[] = szOID_RSA_signedData;
1215 info.pszObjId = oid_rsa_signed;
1216 ret = CryptEncodeObjectEx(X509_ASN_ENCODING,
1217 PKCS_CONTENT_INFO, &info, 0, NULL, pvData, pcbData);
1219 CryptMemFree(info.Content.pbData);
1226 case CMSG_BARE_CONTENT_PARAM:
1228 CRYPT_SIGNED_INFO info;
1229 char oid_rsa_data[] = szOID_RSA_data;
1231 info = *msg->msg_data.info;
1232 /* Quirk: OID is only encoded messages if an update has happened */
1233 if (msg->base.state != MsgStateInit)
1234 info.content.pszObjId = oid_rsa_data;
1236 info.content.pszObjId = NULL;
1237 if (msg->data.cbData)
1239 CRYPT_DATA_BLOB blob = { msg->data.cbData, msg->data.pbData };
1241 ret = CryptEncodeObjectEx(X509_ASN_ENCODING, X509_OCTET_STRING,
1242 &blob, CRYPT_ENCODE_ALLOC_FLAG, NULL,
1243 &info.content.Content.pbData, &info.content.Content.cbData);
1247 info.content.Content.cbData = 0;
1248 info.content.Content.pbData = NULL;
1253 ret = CRYPT_AsnEncodeCMSSignedInfo(&info, pvData, pcbData);
1254 LocalFree(info.content.Content.pbData);
1258 case CMSG_COMPUTED_HASH_PARAM:
1259 if (dwIndex >= msg->msg_data.cSignerHandle)
1260 SetLastError(CRYPT_E_INVALID_INDEX);
1262 ret = CryptGetHashParam(
1263 msg->msg_data.signerHandles[dwIndex].contentHash, HP_HASHVAL,
1264 pvData, pcbData, 0);
1266 case CMSG_ENCODED_SIGNER:
1267 if (dwIndex >= msg->msg_data.info->cSignerInfo)
1268 SetLastError(CRYPT_E_INVALID_INDEX);
1270 ret = CryptEncodeObjectEx(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
1271 CMS_SIGNER_INFO, &msg->msg_data.info->rgSignerInfo[dwIndex], 0,
1272 NULL, pvData, pcbData);
1274 case CMSG_VERSION_PARAM:
1275 ret = CRYPT_CopyParam(pvData, pcbData, &msg->msg_data.info->version,
1276 sizeof(msg->msg_data.info->version));
1279 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1284 static BOOL CSignedEncodeMsg_Update(HCRYPTMSG hCryptMsg, const BYTE *pbData,
1285 DWORD cbData, BOOL fFinal)
1287 CSignedEncodeMsg *msg = (CSignedEncodeMsg *)hCryptMsg;
1290 if (msg->base.state == MsgStateFinalized)
1291 SetLastError(CRYPT_E_MSG_ERROR);
1292 else if (msg->base.streamed || (msg->base.open_flags & CMSG_DETACHED_FLAG))
1294 ret = CSignedMsgData_Update(&msg->msg_data, pbData, cbData, fFinal,
1296 if (msg->base.streamed)
1297 FIXME("streamed partial stub\n");
1298 msg->base.state = fFinal ? MsgStateFinalized : MsgStateUpdated;
1303 SetLastError(CRYPT_E_MSG_ERROR);
1308 msg->data.pbData = CryptMemAlloc(cbData);
1309 if (msg->data.pbData)
1311 memcpy(msg->data.pbData, pbData, cbData);
1312 msg->data.cbData = cbData;
1319 ret = CSignedMsgData_Update(&msg->msg_data, pbData, cbData,
1321 msg->base.state = MsgStateFinalized;
1327 static HCRYPTMSG CSignedEncodeMsg_Open(DWORD dwFlags,
1328 const void *pvMsgEncodeInfo, LPSTR pszInnerContentObjID,
1329 PCMSG_STREAM_INFO pStreamInfo)
1331 const CMSG_SIGNED_ENCODE_INFO_WITH_CMS *info =
1332 (const CMSG_SIGNED_ENCODE_INFO_WITH_CMS *)pvMsgEncodeInfo;
1334 CSignedEncodeMsg *msg;
1336 if (info->cbSize != sizeof(CMSG_SIGNED_ENCODE_INFO) &&
1337 info->cbSize != sizeof(CMSG_SIGNED_ENCODE_INFO_WITH_CMS))
1339 SetLastError(E_INVALIDARG);
1342 if (info->cbSize == sizeof(CMSG_SIGNED_ENCODE_INFO_WITH_CMS) &&
1343 info->rgAttrCertEncoded)
1345 FIXME("CMSG_SIGNED_ENCODE_INFO with CMS fields unsupported\n");
1348 for (i = 0; i < info->cSigners; i++)
1349 if (!CRYPT_IsValidSigner(&info->rgSigners[i]))
1351 msg = CryptMemAlloc(sizeof(CSignedEncodeMsg));
1356 CryptMsgBase_Init((CryptMsgBase *)msg, dwFlags, pStreamInfo,
1357 CSignedEncodeMsg_Close, CSignedEncodeMsg_GetParam,
1358 CSignedEncodeMsg_Update, CRYPT_DefaultMsgControl);
1359 msg->data.cbData = 0;
1360 msg->data.pbData = NULL;
1361 msg->msg_data.info = CryptMemAlloc(sizeof(CRYPT_SIGNED_INFO));
1362 if (msg->msg_data.info)
1364 memset(msg->msg_data.info, 0, sizeof(CRYPT_SIGNED_INFO));
1365 msg->msg_data.info->version = CMSG_SIGNED_DATA_V1;
1373 msg->msg_data.info->rgSignerInfo =
1374 CryptMemAlloc(info->cSigners * sizeof(CMSG_CMS_SIGNER_INFO));
1375 if (msg->msg_data.info->rgSignerInfo)
1377 msg->msg_data.info->cSignerInfo = info->cSigners;
1378 memset(msg->msg_data.info->rgSignerInfo, 0,
1379 msg->msg_data.info->cSignerInfo *
1380 sizeof(CMSG_CMS_SIGNER_INFO));
1381 ret = CSignedMsgData_AllocateHandles(&msg->msg_data);
1382 for (i = 0; ret && i < msg->msg_data.info->cSignerInfo; i++)
1384 ret = CSignerInfo_Construct(
1385 &msg->msg_data.info->rgSignerInfo[i],
1386 &info->rgSigners[i]);
1389 ret = CSignedMsgData_ConstructSignerHandles(
1390 &msg->msg_data, i, info->rgSigners[i].hCryptProv);
1391 if (dwFlags & CMSG_CRYPT_RELEASE_CONTEXT_FLAG)
1392 CryptReleaseContext(info->rgSigners[i].hCryptProv,
1402 msg->msg_data.info->cSignerInfo = 0;
1403 msg->msg_data.signerHandles = NULL;
1404 msg->msg_data.cSignerHandle = 0;
1408 ret = CRYPT_ConstructBlobArray(
1409 (BlobArray *)&msg->msg_data.info->cCertEncoded,
1410 (const BlobArray *)&info->cCertEncoded);
1412 ret = CRYPT_ConstructBlobArray(
1413 (BlobArray *)&msg->msg_data.info->cCrlEncoded,
1414 (const BlobArray *)&info->cCrlEncoded);
1417 CSignedEncodeMsg_Close(msg);
1424 static inline const char *MSG_TYPE_STR(DWORD type)
1428 #define _x(x) case (x): return #x
1432 _x(CMSG_SIGNED_AND_ENVELOPED);
1437 return wine_dbg_sprintf("unknown (%d)", type);
1441 HCRYPTMSG WINAPI CryptMsgOpenToEncode(DWORD dwMsgEncodingType, DWORD dwFlags,
1442 DWORD dwMsgType, const void *pvMsgEncodeInfo, LPSTR pszInnerContentObjID,
1443 PCMSG_STREAM_INFO pStreamInfo)
1445 HCRYPTMSG msg = NULL;
1447 TRACE("(%08x, %08x, %08x, %p, %s, %p)\n", dwMsgEncodingType, dwFlags,
1448 dwMsgType, pvMsgEncodeInfo, debugstr_a(pszInnerContentObjID), pStreamInfo);
1450 if (GET_CMSG_ENCODING_TYPE(dwMsgEncodingType) != PKCS_7_ASN_ENCODING)
1452 SetLastError(E_INVALIDARG);
1458 msg = CDataEncodeMsg_Open(dwFlags, pvMsgEncodeInfo,
1459 pszInnerContentObjID, pStreamInfo);
1462 msg = CHashEncodeMsg_Open(dwFlags, pvMsgEncodeInfo,
1463 pszInnerContentObjID, pStreamInfo);
1466 msg = CSignedEncodeMsg_Open(dwFlags, pvMsgEncodeInfo,
1467 pszInnerContentObjID, pStreamInfo);
1469 case CMSG_ENVELOPED:
1470 FIXME("unimplemented for type %s\n", MSG_TYPE_STR(dwMsgType));
1472 case CMSG_SIGNED_AND_ENVELOPED:
1473 case CMSG_ENCRYPTED:
1474 /* defined but invalid, fall through */
1476 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1481 typedef struct _CDecodeMsg
1485 HCRYPTPROV crypt_prov;
1488 CSignedMsgData signed_data;
1490 CRYPT_DATA_BLOB msg_data;
1491 PCONTEXT_PROPERTY_LIST properties;
1494 static void CDecodeMsg_Close(HCRYPTMSG hCryptMsg)
1496 CDecodeMsg *msg = (CDecodeMsg *)hCryptMsg;
1498 if (msg->base.open_flags & CMSG_CRYPT_RELEASE_CONTEXT_FLAG)
1499 CryptReleaseContext(msg->crypt_prov, 0);
1504 CryptDestroyHash(msg->u.hash);
1507 if (msg->u.signed_data.info)
1509 LocalFree(msg->u.signed_data.info);
1510 CSignedMsgData_CloseHandles(&msg->u.signed_data);
1514 CryptMemFree(msg->msg_data.pbData);
1515 ContextPropertyList_Free(msg->properties);
1518 static BOOL CDecodeMsg_CopyData(CDecodeMsg *msg, const BYTE *pbData,
1525 if (msg->msg_data.cbData)
1526 msg->msg_data.pbData = CryptMemRealloc(msg->msg_data.pbData,
1527 msg->msg_data.cbData + cbData);
1529 msg->msg_data.pbData = CryptMemAlloc(cbData);
1530 if (msg->msg_data.pbData)
1532 memcpy(msg->msg_data.pbData + msg->msg_data.cbData, pbData, cbData);
1533 msg->msg_data.cbData += cbData;
1541 static BOOL CDecodeMsg_DecodeDataContent(CDecodeMsg *msg, CRYPT_DER_BLOB *blob)
1544 CRYPT_DATA_BLOB *data;
1547 ret = CryptDecodeObjectEx(X509_ASN_ENCODING, X509_OCTET_STRING,
1548 blob->pbData, blob->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, (LPBYTE)&data,
1552 ret = ContextPropertyList_SetProperty(msg->properties,
1553 CMSG_CONTENT_PARAM, data->pbData, data->cbData);
1559 static void CDecodeMsg_SaveAlgorithmID(CDecodeMsg *msg, DWORD param,
1560 const CRYPT_ALGORITHM_IDENTIFIER *id)
1562 static const BYTE nullParams[] = { ASN_NULL, 0 };
1563 CRYPT_ALGORITHM_IDENTIFIER *copy;
1564 DWORD len = sizeof(CRYPT_ALGORITHM_IDENTIFIER);
1566 /* Linearize algorithm id */
1567 len += strlen(id->pszObjId) + 1;
1568 len += id->Parameters.cbData;
1569 copy = CryptMemAlloc(len);
1573 (LPSTR)((BYTE *)copy + sizeof(CRYPT_ALGORITHM_IDENTIFIER));
1574 strcpy(copy->pszObjId, id->pszObjId);
1575 copy->Parameters.pbData = (BYTE *)copy->pszObjId + strlen(id->pszObjId)
1577 /* Trick: omit NULL parameters */
1578 if (id->Parameters.cbData == sizeof(nullParams) &&
1579 !memcmp(id->Parameters.pbData, nullParams, sizeof(nullParams)))
1581 copy->Parameters.cbData = 0;
1582 len -= sizeof(nullParams);
1585 copy->Parameters.cbData = id->Parameters.cbData;
1586 if (copy->Parameters.cbData)
1587 memcpy(copy->Parameters.pbData, id->Parameters.pbData,
1588 id->Parameters.cbData);
1589 ContextPropertyList_SetProperty(msg->properties, param, (BYTE *)copy,
1595 static inline void CRYPT_FixUpAlgorithmID(CRYPT_ALGORITHM_IDENTIFIER *id)
1597 id->pszObjId = (LPSTR)((BYTE *)id + sizeof(CRYPT_ALGORITHM_IDENTIFIER));
1598 id->Parameters.pbData = (BYTE *)id->pszObjId + strlen(id->pszObjId) + 1;
1601 static BOOL CDecodeMsg_DecodeHashedContent(CDecodeMsg *msg,
1602 CRYPT_DER_BLOB *blob)
1605 CRYPT_DIGESTED_DATA *digestedData;
1608 ret = CRYPT_AsnDecodePKCSDigestedData(blob->pbData, blob->cbData,
1609 CRYPT_DECODE_ALLOC_FLAG, NULL, (CRYPT_DIGESTED_DATA *)&digestedData,
1613 ContextPropertyList_SetProperty(msg->properties, CMSG_VERSION_PARAM,
1614 (const BYTE *)&digestedData->version, sizeof(digestedData->version));
1615 CDecodeMsg_SaveAlgorithmID(msg, CMSG_HASH_ALGORITHM_PARAM,
1616 &digestedData->DigestAlgorithm);
1617 ContextPropertyList_SetProperty(msg->properties,
1618 CMSG_INNER_CONTENT_TYPE_PARAM,
1619 (const BYTE *)digestedData->ContentInfo.pszObjId,
1620 digestedData->ContentInfo.pszObjId ?
1621 strlen(digestedData->ContentInfo.pszObjId) + 1 : 0);
1622 if (digestedData->ContentInfo.Content.cbData)
1623 CDecodeMsg_DecodeDataContent(msg,
1624 &digestedData->ContentInfo.Content);
1626 ContextPropertyList_SetProperty(msg->properties,
1627 CMSG_CONTENT_PARAM, NULL, 0);
1628 ContextPropertyList_SetProperty(msg->properties, CMSG_HASH_DATA_PARAM,
1629 digestedData->hash.pbData, digestedData->hash.cbData);
1630 LocalFree(digestedData);
1635 static BOOL CDecodeMsg_DecodeSignedContent(CDecodeMsg *msg,
1636 CRYPT_DER_BLOB *blob)
1639 CRYPT_SIGNED_INFO *signedInfo;
1642 ret = CRYPT_AsnDecodeCMSSignedInfo(blob->pbData, blob->cbData,
1643 CRYPT_DECODE_ALLOC_FLAG, NULL, (CRYPT_SIGNED_INFO *)&signedInfo,
1649 msg->u.signed_data.info = signedInfo;
1650 ret = CSignedMsgData_AllocateHandles(&msg->u.signed_data);
1651 for (i = 0; ret && i < msg->u.signed_data.info->cSignerInfo; i++)
1652 ret = CSignedMsgData_ConstructSignerHandles(&msg->u.signed_data, i,
1656 /* Now that we have all the content, update the hash handles with
1657 * it. Have to decode it if the type is szOID_RSA_data.
1659 if (msg->u.signed_data.info->content.Content.cbData)
1661 if (!strcmp(msg->u.signed_data.info->content.pszObjId,
1664 CRYPT_DATA_BLOB *blob;
1666 ret = CryptDecodeObjectEx(X509_ASN_ENCODING,
1668 msg->u.signed_data.info->content.Content.pbData,
1669 msg->u.signed_data.info->content.Content.cbData,
1670 CRYPT_DECODE_ALLOC_FLAG, NULL, (LPBYTE)&blob, &size);
1673 ret = CSignedMsgData_Update(&msg->u.signed_data,
1674 blob->pbData, blob->cbData, TRUE, Verify);
1679 ret = CSignedMsgData_Update(&msg->u.signed_data,
1680 msg->u.signed_data.info->content.Content.pbData,
1681 msg->u.signed_data.info->content.Content.cbData, TRUE,
1688 /* Decodes the content in blob as the type given, and updates the value
1689 * (type, parameters, etc.) of msg based on what blob contains.
1690 * It doesn't just use msg's type, to allow a recursive call from an implicitly
1691 * typed message once the outer content info has been decoded.
1693 static BOOL CDecodeMsg_DecodeContent(CDecodeMsg *msg, CRYPT_DER_BLOB *blob,
1701 if ((ret = CDecodeMsg_DecodeDataContent(msg, blob)))
1702 msg->type = CMSG_DATA;
1705 if ((ret = CDecodeMsg_DecodeHashedContent(msg, blob)))
1706 msg->type = CMSG_HASHED;
1708 case CMSG_ENVELOPED:
1709 FIXME("unimplemented for type %s\n", MSG_TYPE_STR(type));
1713 if ((ret = CDecodeMsg_DecodeSignedContent(msg, blob)))
1714 msg->type = CMSG_SIGNED;
1718 CRYPT_CONTENT_INFO *info;
1721 ret = CryptDecodeObjectEx(X509_ASN_ENCODING, PKCS_CONTENT_INFO,
1722 msg->msg_data.pbData, msg->msg_data.cbData, CRYPT_DECODE_ALLOC_FLAG,
1723 NULL, (LPBYTE)&info, &size);
1726 if (!strcmp(info->pszObjId, szOID_RSA_data))
1727 ret = CDecodeMsg_DecodeContent(msg, &info->Content, CMSG_DATA);
1728 else if (!strcmp(info->pszObjId, szOID_RSA_digestedData))
1729 ret = CDecodeMsg_DecodeContent(msg, &info->Content,
1731 else if (!strcmp(info->pszObjId, szOID_RSA_envelopedData))
1732 ret = CDecodeMsg_DecodeContent(msg, &info->Content,
1734 else if (!strcmp(info->pszObjId, szOID_RSA_signedData))
1735 ret = CDecodeMsg_DecodeContent(msg, &info->Content,
1739 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1749 static BOOL CDecodeMsg_Update(HCRYPTMSG hCryptMsg, const BYTE *pbData,
1750 DWORD cbData, BOOL fFinal)
1752 CDecodeMsg *msg = (CDecodeMsg *)hCryptMsg;
1755 TRACE("(%p, %p, %d, %d)\n", hCryptMsg, pbData, cbData, fFinal);
1757 if (msg->base.state == MsgStateFinalized)
1758 SetLastError(CRYPT_E_MSG_ERROR);
1759 else if (msg->base.streamed)
1761 FIXME("(%p, %p, %d, %d): streamed update stub\n", hCryptMsg, pbData,
1765 if (msg->base.open_flags & CMSG_DETACHED_FLAG &&
1766 msg->base.state != MsgStateDataFinalized)
1768 ret = CDecodeMsg_CopyData(msg, pbData, cbData);
1769 msg->base.state = MsgStateDataFinalized;
1771 ret = CDecodeMsg_DecodeContent(msg, &msg->msg_data,
1776 FIXME("(%p, %p, %d, %d): detached update stub\n", hCryptMsg,
1777 pbData, cbData, fFinal);
1779 msg->base.state = MsgStateFinalized;
1784 ret = CDecodeMsg_CopyData(msg, pbData, cbData);
1785 if (msg->base.state == MsgStateInit)
1786 msg->base.state = MsgStateUpdated;
1792 SetLastError(CRYPT_E_MSG_ERROR);
1795 if (msg->base.state == MsgStateInit)
1797 ret = CDecodeMsg_CopyData(msg, pbData, cbData);
1799 ret = CDecodeMsg_DecodeContent(msg, &msg->msg_data,
1801 if (msg->base.open_flags & CMSG_DETACHED_FLAG)
1802 msg->base.state = MsgStateDataFinalized;
1804 msg->base.state = MsgStateFinalized;
1806 else if (msg->base.state == MsgStateDataFinalized)
1808 FIXME("(%p, %p, %d, %d): detached update stub\n", hCryptMsg,
1809 pbData, cbData, fFinal);
1811 msg->base.state = MsgStateFinalized;
1818 static BOOL CDecodeHashMsg_GetParam(CDecodeMsg *msg, DWORD dwParamType,
1819 DWORD dwIndex, void *pvData, DWORD *pcbData)
1823 switch (dwParamType)
1825 case CMSG_TYPE_PARAM:
1826 ret = CRYPT_CopyParam(pvData, pcbData, &msg->type, sizeof(msg->type));
1828 case CMSG_HASH_ALGORITHM_PARAM:
1830 CRYPT_DATA_BLOB blob;
1832 ret = ContextPropertyList_FindProperty(msg->properties, dwParamType,
1836 ret = CRYPT_CopyParam(pvData, pcbData, blob.pbData, blob.cbData);
1838 CRYPT_FixUpAlgorithmID((CRYPT_ALGORITHM_IDENTIFIER *)pvData);
1841 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1844 case CMSG_COMPUTED_HASH_PARAM:
1847 CRYPT_ALGORITHM_IDENTIFIER *hashAlgoID = NULL;
1851 CryptMsgGetParam(msg, CMSG_HASH_ALGORITHM_PARAM, 0, NULL, &size);
1852 hashAlgoID = CryptMemAlloc(size);
1853 ret = CryptMsgGetParam(msg, CMSG_HASH_ALGORITHM_PARAM, 0,
1856 algID = CertOIDToAlgId(hashAlgoID->pszObjId);
1857 ret = CryptCreateHash(msg->crypt_prov, algID, 0, 0, &msg->u.hash);
1860 CRYPT_DATA_BLOB content;
1862 ret = ContextPropertyList_FindProperty(msg->properties,
1863 CMSG_CONTENT_PARAM, &content);
1865 ret = CryptHashData(msg->u.hash, content.pbData,
1868 CryptMemFree(hashAlgoID);
1873 ret = CryptGetHashParam(msg->u.hash, HP_HASHVAL, pvData, pcbData,
1878 CRYPT_DATA_BLOB blob;
1880 ret = ContextPropertyList_FindProperty(msg->properties, dwParamType,
1883 ret = CRYPT_CopyParam(pvData, pcbData, blob.pbData, blob.cbData);
1885 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1891 /* nextData is an in/out parameter - on input it's the memory location in
1892 * which a copy of in's data should be made, and on output it's the memory
1893 * location immediately after out's copy of in's data.
1895 static inline void CRYPT_CopyBlob(CRYPT_DATA_BLOB *out,
1896 const CRYPT_DATA_BLOB *in, LPBYTE *nextData)
1898 out->cbData = in->cbData;
1901 out->pbData = *nextData;
1902 memcpy(out->pbData, in->pbData, in->cbData);
1903 *nextData += in->cbData;
1907 static inline void CRYPT_CopyAlgorithmId(CRYPT_ALGORITHM_IDENTIFIER *out,
1908 const CRYPT_ALGORITHM_IDENTIFIER *in, LPBYTE *nextData)
1912 out->pszObjId = (LPSTR)*nextData;
1913 strcpy(out->pszObjId, in->pszObjId);
1914 *nextData += strlen(out->pszObjId) + 1;
1916 CRYPT_CopyBlob(&out->Parameters, &in->Parameters, nextData);
1919 static inline void CRYPT_CopyAttributes(CRYPT_ATTRIBUTES *out,
1920 const CRYPT_ATTRIBUTES *in, LPBYTE *nextData)
1922 out->cAttr = in->cAttr;
1927 if ((*nextData - (LPBYTE)0) % sizeof(DWORD_PTR))
1928 *nextData += (*nextData - (LPBYTE)0) % sizeof(DWORD_PTR);
1929 out->rgAttr = (CRYPT_ATTRIBUTE *)*nextData;
1930 *nextData += in->cAttr * sizeof(CRYPT_ATTRIBUTE);
1931 for (i = 0; i < in->cAttr; i++)
1933 if (in->rgAttr[i].pszObjId)
1935 out->rgAttr[i].pszObjId = (LPSTR)*nextData;
1936 strcpy(out->rgAttr[i].pszObjId, in->rgAttr[i].pszObjId);
1937 *nextData += strlen(in->rgAttr[i].pszObjId) + 1;
1939 if (in->rgAttr[i].cValue)
1943 out->rgAttr[i].cValue = in->rgAttr[i].cValue;
1944 if ((*nextData - (LPBYTE)0) % sizeof(DWORD_PTR))
1945 *nextData += (*nextData - (LPBYTE)0) % sizeof(DWORD_PTR);
1946 out->rgAttr[i].rgValue = (PCRYPT_DATA_BLOB)*nextData;
1947 *nextData += in->rgAttr[i].cValue * sizeof(CRYPT_DATA_BLOB);
1948 for (j = 0; j < in->rgAttr[i].cValue; j++)
1949 CRYPT_CopyBlob(&out->rgAttr[i].rgValue[j],
1950 &in->rgAttr[i].rgValue[j], nextData);
1956 static DWORD CRYPT_SizeOfAttributes(const CRYPT_ATTRIBUTES *attr)
1958 DWORD size = attr->cAttr * sizeof(CRYPT_ATTRIBUTE), i, j;
1960 for (i = 0; i < attr->cAttr; i++)
1962 if (attr->rgAttr[i].pszObjId)
1963 size += strlen(attr->rgAttr[i].pszObjId) + 1;
1965 if (size % sizeof(DWORD_PTR))
1966 size += size % sizeof(DWORD_PTR);
1967 size += attr->rgAttr[i].cValue * sizeof(CRYPT_DATA_BLOB);
1968 for (j = 0; j < attr->rgAttr[i].cValue; j++)
1969 size += attr->rgAttr[i].rgValue[j].cbData;
1971 /* align pointer again to be conservative */
1972 if (size % sizeof(DWORD_PTR))
1973 size += size % sizeof(DWORD_PTR);
1977 static BOOL CRYPT_CopySignerInfo(void *pvData, DWORD *pcbData,
1978 const CMSG_CMS_SIGNER_INFO *in)
1980 DWORD size = sizeof(CMSG_SIGNER_INFO);
1983 TRACE("(%p, %d, %p)\n", pvData, pvData ? *pcbData : 0, in);
1985 if (in->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
1987 size += in->SignerId.IssuerSerialNumber.Issuer.cbData;
1988 size += in->SignerId.IssuerSerialNumber.SerialNumber.cbData;
1992 FIXME("unsupported for key id\n");
1995 if (in->HashAlgorithm.pszObjId)
1996 size += strlen(in->HashAlgorithm.pszObjId) + 1;
1997 size += in->HashAlgorithm.Parameters.cbData;
1998 if (in->HashEncryptionAlgorithm.pszObjId)
1999 size += strlen(in->HashEncryptionAlgorithm.pszObjId) + 1;
2000 size += in->HashEncryptionAlgorithm.Parameters.cbData;
2001 size += in->EncryptedHash.cbData;
2003 if (size % sizeof(DWORD_PTR))
2004 size += size % sizeof(DWORD_PTR);
2005 size += CRYPT_SizeOfAttributes(&in->AuthAttrs);
2006 size += CRYPT_SizeOfAttributes(&in->UnauthAttrs);
2012 else if (*pcbData < size)
2015 SetLastError(ERROR_MORE_DATA);
2020 LPBYTE nextData = (BYTE *)pvData + sizeof(CMSG_SIGNER_INFO);
2021 CMSG_SIGNER_INFO *out = (CMSG_SIGNER_INFO *)pvData;
2023 out->dwVersion = in->dwVersion;
2024 if (in->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
2026 CRYPT_CopyBlob(&out->Issuer,
2027 &in->SignerId.IssuerSerialNumber.Issuer, &nextData);
2028 CRYPT_CopyBlob(&out->SerialNumber,
2029 &in->SignerId.IssuerSerialNumber.SerialNumber, &nextData);
2031 CRYPT_CopyAlgorithmId(&out->HashAlgorithm, &in->HashAlgorithm,
2033 CRYPT_CopyAlgorithmId(&out->HashEncryptionAlgorithm,
2034 &in->HashEncryptionAlgorithm, &nextData);
2035 CRYPT_CopyBlob(&out->EncryptedHash, &in->EncryptedHash, &nextData);
2037 if ((nextData - (LPBYTE)0) % sizeof(DWORD_PTR))
2038 nextData += (nextData - (LPBYTE)0) % sizeof(DWORD_PTR);
2039 CRYPT_CopyAttributes(&out->AuthAttrs, &in->AuthAttrs, &nextData);
2040 CRYPT_CopyAttributes(&out->UnauthAttrs, &in->UnauthAttrs, &nextData);
2043 TRACE("returning %d\n", ret);
2047 static BOOL CRYPT_CopySignerCertInfo(void *pvData, DWORD *pcbData,
2048 const CMSG_CMS_SIGNER_INFO *in)
2050 DWORD size = sizeof(CERT_INFO);
2053 TRACE("(%p, %d, %p)\n", pvData, pvData ? *pcbData : 0, in);
2055 if (in->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
2057 size += in->SignerId.IssuerSerialNumber.Issuer.cbData;
2058 size += in->SignerId.IssuerSerialNumber.SerialNumber.cbData;
2062 FIXME("unimplemented for key id\n");
2070 else if (*pcbData < size)
2073 SetLastError(ERROR_MORE_DATA);
2078 LPBYTE nextData = (BYTE *)pvData + sizeof(CERT_INFO);
2079 CERT_INFO *out = (CERT_INFO *)pvData;
2081 memset(out, 0, sizeof(CERT_INFO));
2082 CRYPT_CopyBlob(&out->Issuer,
2083 &in->SignerId.IssuerSerialNumber.Issuer, &nextData);
2084 CRYPT_CopyBlob(&out->SerialNumber,
2085 &in->SignerId.IssuerSerialNumber.SerialNumber, &nextData);
2088 TRACE("returning %d\n", ret);
2092 static BOOL CDecodeSignedMsg_GetParam(CDecodeMsg *msg, DWORD dwParamType,
2093 DWORD dwIndex, void *pvData, DWORD *pcbData)
2097 switch (dwParamType)
2099 case CMSG_TYPE_PARAM:
2100 ret = CRYPT_CopyParam(pvData, pcbData, &msg->type, sizeof(msg->type));
2102 case CMSG_CONTENT_PARAM:
2103 if (msg->u.signed_data.info)
2105 if (!strcmp(msg->u.signed_data.info->content.pszObjId,
2108 CRYPT_DATA_BLOB *blob;
2111 ret = CryptDecodeObjectEx(X509_ASN_ENCODING, X509_OCTET_STRING,
2112 msg->u.signed_data.info->content.Content.pbData,
2113 msg->u.signed_data.info->content.Content.cbData,
2114 CRYPT_DECODE_ALLOC_FLAG, NULL, (LPBYTE)&blob, &size);
2117 ret = CRYPT_CopyParam(pvData, pcbData, blob->pbData,
2123 ret = CRYPT_CopyParam(pvData, pcbData,
2124 msg->u.signed_data.info->content.Content.pbData,
2125 msg->u.signed_data.info->content.Content.cbData);
2128 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2130 case CMSG_INNER_CONTENT_TYPE_PARAM:
2131 if (msg->u.signed_data.info)
2132 ret = CRYPT_CopyParam(pvData, pcbData,
2133 msg->u.signed_data.info->content.pszObjId,
2134 strlen(msg->u.signed_data.info->content.pszObjId) + 1);
2136 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2138 case CMSG_SIGNER_COUNT_PARAM:
2139 if (msg->u.signed_data.info)
2140 ret = CRYPT_CopyParam(pvData, pcbData,
2141 &msg->u.signed_data.info->cSignerInfo, sizeof(DWORD));
2143 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2145 case CMSG_SIGNER_INFO_PARAM:
2146 if (msg->u.signed_data.info)
2148 if (dwIndex >= msg->u.signed_data.info->cSignerInfo)
2149 SetLastError(CRYPT_E_INVALID_INDEX);
2151 ret = CRYPT_CopySignerInfo(pvData, pcbData,
2152 &msg->u.signed_data.info->rgSignerInfo[dwIndex]);
2155 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2157 case CMSG_SIGNER_CERT_INFO_PARAM:
2158 if (msg->u.signed_data.info)
2160 if (dwIndex >= msg->u.signed_data.info->cSignerInfo)
2161 SetLastError(CRYPT_E_INVALID_INDEX);
2163 ret = CRYPT_CopySignerCertInfo(pvData, pcbData,
2164 &msg->u.signed_data.info->rgSignerInfo[dwIndex]);
2167 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2169 case CMSG_CERT_COUNT_PARAM:
2170 if (msg->u.signed_data.info)
2171 ret = CRYPT_CopyParam(pvData, pcbData,
2172 &msg->u.signed_data.info->cCertEncoded, sizeof(DWORD));
2174 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2176 case CMSG_CERT_PARAM:
2177 if (msg->u.signed_data.info)
2179 if (dwIndex >= msg->u.signed_data.info->cCertEncoded)
2180 SetLastError(CRYPT_E_INVALID_INDEX);
2182 ret = CRYPT_CopyParam(pvData, pcbData,
2183 msg->u.signed_data.info->rgCertEncoded[dwIndex].pbData,
2184 msg->u.signed_data.info->rgCertEncoded[dwIndex].cbData);
2187 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2189 case CMSG_CRL_COUNT_PARAM:
2190 if (msg->u.signed_data.info)
2191 ret = CRYPT_CopyParam(pvData, pcbData,
2192 &msg->u.signed_data.info->cCrlEncoded, sizeof(DWORD));
2194 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2196 case CMSG_CRL_PARAM:
2197 if (msg->u.signed_data.info)
2199 if (dwIndex >= msg->u.signed_data.info->cCrlEncoded)
2200 SetLastError(CRYPT_E_INVALID_INDEX);
2202 ret = CRYPT_CopyParam(pvData, pcbData,
2203 msg->u.signed_data.info->rgCrlEncoded[dwIndex].pbData,
2204 msg->u.signed_data.info->rgCrlEncoded[dwIndex].cbData);
2207 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2209 case CMSG_COMPUTED_HASH_PARAM:
2210 if (msg->u.signed_data.info)
2212 if (dwIndex >= msg->u.signed_data.cSignerHandle)
2213 SetLastError(CRYPT_E_INVALID_INDEX);
2215 ret = CryptGetHashParam(
2216 msg->u.signed_data.signerHandles[dwIndex].contentHash,
2217 HP_HASHVAL, pvData, pcbData, 0);
2220 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2222 case CMSG_ATTR_CERT_COUNT_PARAM:
2223 if (msg->u.signed_data.info)
2225 DWORD attrCertCount = 0;
2227 ret = CRYPT_CopyParam(pvData, pcbData,
2228 &attrCertCount, sizeof(DWORD));
2231 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2233 case CMSG_ATTR_CERT_PARAM:
2234 if (msg->u.signed_data.info)
2235 SetLastError(CRYPT_E_INVALID_INDEX);
2237 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2240 FIXME("unimplemented for %d\n", dwParamType);
2241 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2246 static BOOL CDecodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
2247 DWORD dwIndex, void *pvData, DWORD *pcbData)
2249 CDecodeMsg *msg = (CDecodeMsg *)hCryptMsg;
2255 ret = CDecodeHashMsg_GetParam(msg, dwParamType, dwIndex, pvData,
2259 ret = CDecodeSignedMsg_GetParam(msg, dwParamType, dwIndex, pvData,
2263 switch (dwParamType)
2265 case CMSG_TYPE_PARAM:
2266 ret = CRYPT_CopyParam(pvData, pcbData, &msg->type,
2271 CRYPT_DATA_BLOB blob;
2273 ret = ContextPropertyList_FindProperty(msg->properties, dwParamType,
2276 ret = CRYPT_CopyParam(pvData, pcbData, blob.pbData,
2279 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2286 static BOOL CDecodeHashMsg_VerifyHash(CDecodeMsg *msg)
2289 CRYPT_DATA_BLOB hashBlob;
2291 ret = ContextPropertyList_FindProperty(msg->properties,
2292 CMSG_HASH_DATA_PARAM, &hashBlob);
2295 DWORD computedHashSize = 0;
2297 ret = CDecodeHashMsg_GetParam(msg, CMSG_COMPUTED_HASH_PARAM, 0, NULL,
2299 if (hashBlob.cbData == computedHashSize)
2301 LPBYTE computedHash = CryptMemAlloc(computedHashSize);
2305 ret = CDecodeHashMsg_GetParam(msg, CMSG_COMPUTED_HASH_PARAM, 0,
2306 computedHash, &computedHashSize);
2308 ret = !memcmp(hashBlob.pbData, computedHash,
2310 CryptMemFree(computedHash);
2319 static BOOL CDecodeSignedMsg_VerifySignatureWithKey(CDecodeMsg *msg,
2320 HCRYPTPROV prov, DWORD signerIndex, PCERT_PUBLIC_KEY_INFO keyInfo)
2326 prov = msg->crypt_prov;
2327 ret = CryptImportPublicKeyInfo(prov, X509_ASN_ENCODING, keyInfo, &key);
2331 CRYPT_HASH_BLOB reversedHash;
2333 if (msg->u.signed_data.info->rgSignerInfo[signerIndex].AuthAttrs.cAttr)
2334 hash = msg->u.signed_data.signerHandles[signerIndex].authAttrHash;
2336 hash = msg->u.signed_data.signerHandles[signerIndex].contentHash;
2337 ret = CRYPT_ConstructBlob(&reversedHash,
2338 &msg->u.signed_data.info->rgSignerInfo[signerIndex].EncryptedHash);
2341 CRYPT_ReverseBytes(&reversedHash);
2342 ret = CryptVerifySignatureW(hash, reversedHash.pbData,
2343 reversedHash.cbData, key, NULL, 0);
2344 CryptMemFree(reversedHash.pbData);
2346 CryptDestroyKey(key);
2351 static BOOL CDecodeSignedMsg_VerifySignature(CDecodeMsg *msg, PCERT_INFO info)
2356 for (i = 0; !ret && i < msg->u.signed_data.info->cSignerInfo; i++)
2358 PCMSG_CMS_SIGNER_INFO signerInfo =
2359 &msg->u.signed_data.info->rgSignerInfo[i];
2361 if (signerInfo->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
2363 ret = CertCompareCertificateName(X509_ASN_ENCODING,
2364 &signerInfo->SignerId.IssuerSerialNumber.Issuer,
2368 ret = CertCompareIntegerBlob(
2369 &signerInfo->SignerId.IssuerSerialNumber.SerialNumber,
2370 &info->SerialNumber);
2377 FIXME("signer %d: unimplemented for key id\n", i);
2381 ret = CDecodeSignedMsg_VerifySignatureWithKey(msg, 0, i,
2382 &info->SubjectPublicKeyInfo);
2384 SetLastError(CRYPT_E_SIGNER_NOT_FOUND);
2389 static BOOL CDecodeSignedMsg_VerifySignatureEx(CDecodeMsg *msg,
2390 PCMSG_CTRL_VERIFY_SIGNATURE_EX_PARA para)
2394 if (para->cbSize != sizeof(CMSG_CTRL_VERIFY_SIGNATURE_EX_PARA))
2395 SetLastError(ERROR_INVALID_PARAMETER);
2396 else if (para->dwSignerIndex >= msg->u.signed_data.info->cSignerInfo)
2397 SetLastError(CRYPT_E_SIGNER_NOT_FOUND);
2400 switch (para->dwSignerType)
2402 case CMSG_VERIFY_SIGNER_PUBKEY:
2403 ret = CDecodeSignedMsg_VerifySignatureWithKey(msg,
2404 para->hCryptProv, para->dwSignerIndex,
2405 (PCERT_PUBLIC_KEY_INFO)para->pvSigner);
2407 case CMSG_VERIFY_SIGNER_CERT:
2409 PCCERT_CONTEXT cert = (PCCERT_CONTEXT)para->pvSigner;
2411 ret = CDecodeSignedMsg_VerifySignatureWithKey(msg, para->hCryptProv,
2412 para->dwSignerIndex, &cert->pCertInfo->SubjectPublicKeyInfo);
2416 FIXME("unimplemented for signer type %d\n", para->dwSignerType);
2417 SetLastError(CRYPT_E_SIGNER_NOT_FOUND);
2423 static BOOL CDecodeMsg_Control(HCRYPTMSG hCryptMsg, DWORD dwFlags,
2424 DWORD dwCtrlType, const void *pvCtrlPara)
2426 CDecodeMsg *msg = (CDecodeMsg *)hCryptMsg;
2431 case CMSG_CTRL_VERIFY_SIGNATURE:
2435 ret = CDecodeSignedMsg_VerifySignature(msg, (PCERT_INFO)pvCtrlPara);
2438 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2441 case CMSG_CTRL_DECRYPT:
2445 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2448 case CMSG_CTRL_VERIFY_HASH:
2452 ret = CDecodeHashMsg_VerifyHash(msg);
2455 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2458 case CMSG_CTRL_VERIFY_SIGNATURE_EX:
2462 ret = CDecodeSignedMsg_VerifySignatureEx(msg,
2463 (PCMSG_CTRL_VERIFY_SIGNATURE_EX_PARA)pvCtrlPara);
2466 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2470 SetLastError(CRYPT_E_CONTROL_TYPE);
2475 HCRYPTMSG WINAPI CryptMsgOpenToDecode(DWORD dwMsgEncodingType, DWORD dwFlags,
2476 DWORD dwMsgType, HCRYPTPROV_LEGACY hCryptProv, PCERT_INFO pRecipientInfo,
2477 PCMSG_STREAM_INFO pStreamInfo)
2481 TRACE("(%08x, %08x, %08x, %08lx, %p, %p)\n", dwMsgEncodingType,
2482 dwFlags, dwMsgType, hCryptProv, pRecipientInfo, pStreamInfo);
2484 if (GET_CMSG_ENCODING_TYPE(dwMsgEncodingType) != PKCS_7_ASN_ENCODING)
2486 SetLastError(E_INVALIDARG);
2489 msg = CryptMemAlloc(sizeof(CDecodeMsg));
2492 CryptMsgBase_Init((CryptMsgBase *)msg, dwFlags, pStreamInfo,
2493 CDecodeMsg_Close, CDecodeMsg_GetParam, CDecodeMsg_Update,
2494 CDecodeMsg_Control);
2495 msg->type = dwMsgType;
2497 msg->crypt_prov = hCryptProv;
2500 msg->crypt_prov = CRYPT_GetDefaultProvider();
2501 msg->base.open_flags &= ~CMSG_CRYPT_RELEASE_CONTEXT_FLAG;
2503 memset(&msg->u, 0, sizeof(msg->u));
2504 msg->msg_data.cbData = 0;
2505 msg->msg_data.pbData = NULL;
2506 msg->properties = ContextPropertyList_Create();
2511 HCRYPTMSG WINAPI CryptMsgDuplicate(HCRYPTMSG hCryptMsg)
2513 TRACE("(%p)\n", hCryptMsg);
2517 CryptMsgBase *msg = (CryptMsgBase *)hCryptMsg;
2519 InterlockedIncrement(&msg->ref);
2524 BOOL WINAPI CryptMsgClose(HCRYPTMSG hCryptMsg)
2526 TRACE("(%p)\n", hCryptMsg);
2530 CryptMsgBase *msg = (CryptMsgBase *)hCryptMsg;
2532 if (InterlockedDecrement(&msg->ref) == 0)
2534 TRACE("freeing %p\n", msg);
2543 BOOL WINAPI CryptMsgUpdate(HCRYPTMSG hCryptMsg, const BYTE *pbData,
2544 DWORD cbData, BOOL fFinal)
2546 CryptMsgBase *msg = (CryptMsgBase *)hCryptMsg;
2548 TRACE("(%p, %p, %d, %d)\n", hCryptMsg, pbData, cbData, fFinal);
2550 return msg->update(hCryptMsg, pbData, cbData, fFinal);
2553 BOOL WINAPI CryptMsgGetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
2554 DWORD dwIndex, void *pvData, DWORD *pcbData)
2556 CryptMsgBase *msg = (CryptMsgBase *)hCryptMsg;
2558 TRACE("(%p, %d, %d, %p, %p)\n", hCryptMsg, dwParamType, dwIndex,
2560 return msg->get_param(hCryptMsg, dwParamType, dwIndex, pvData, pcbData);
2563 BOOL WINAPI CryptMsgControl(HCRYPTMSG hCryptMsg, DWORD dwFlags,
2564 DWORD dwCtrlType, const void *pvCtrlPara)
2566 CryptMsgBase *msg = (CryptMsgBase *)hCryptMsg;
2568 TRACE("(%p, %08x, %d, %p)\n", hCryptMsg, dwFlags, dwCtrlType,
2570 return msg->control(hCryptMsg, dwFlags, dwCtrlType, pvCtrlPara);
2573 static CERT_INFO *CRYPT_GetSignerCertInfoFromMsg(HCRYPTMSG msg,
2574 DWORD dwSignerIndex)
2576 CERT_INFO *certInfo = NULL;
2579 if (CryptMsgGetParam(msg, CMSG_SIGNER_CERT_INFO_PARAM, dwSignerIndex, NULL,
2582 certInfo = CryptMemAlloc(size);
2585 if (!CryptMsgGetParam(msg, CMSG_SIGNER_CERT_INFO_PARAM,
2586 dwSignerIndex, certInfo, &size))
2588 CryptMemFree(certInfo);
2596 BOOL WINAPI CryptMsgGetAndVerifySigner(HCRYPTMSG hCryptMsg, DWORD cSignerStore,
2597 HCERTSTORE *rghSignerStore, DWORD dwFlags, PCCERT_CONTEXT *ppSigner,
2598 DWORD *pdwSignerIndex)
2601 DWORD i, signerIndex;
2602 PCCERT_CONTEXT signerCert = NULL;
2605 TRACE("(%p, %d, %p, %08x, %p, %p)\n", hCryptMsg, cSignerStore,
2606 rghSignerStore, dwFlags, ppSigner, pdwSignerIndex);
2608 /* Clear output parameters */
2611 if (pdwSignerIndex && !(dwFlags & CMSG_USE_SIGNER_INDEX_FLAG))
2612 *pdwSignerIndex = 0;
2614 /* Create store to search for signer certificates */
2615 store = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
2616 CERT_STORE_CREATE_NEW_FLAG, NULL);
2617 if (!(dwFlags & CMSG_TRUSTED_SIGNER_FLAG))
2619 HCERTSTORE msgStore = CertOpenStore(CERT_STORE_PROV_MSG, 0, 0, 0,
2622 CertAddStoreToCollection(store, msgStore, 0, 0);
2623 CertCloseStore(msgStore, 0);
2625 for (i = 0; i < cSignerStore; i++)
2626 CertAddStoreToCollection(store, rghSignerStore[i], 0, 0);
2628 /* Find signer cert */
2629 if (dwFlags & CMSG_USE_SIGNER_INDEX_FLAG)
2631 CERT_INFO *signer = CRYPT_GetSignerCertInfoFromMsg(hCryptMsg,
2636 signerIndex = *pdwSignerIndex;
2637 signerCert = CertFindCertificateInStore(store, X509_ASN_ENCODING,
2638 0, CERT_FIND_SUBJECT_CERT, signer, NULL);
2639 CryptMemFree(signer);
2644 DWORD count, size = sizeof(count);
2646 if (CryptMsgGetParam(hCryptMsg, CMSG_SIGNER_COUNT_PARAM, 0, &count,
2649 for (i = 0; !signerCert && i < count; i++)
2651 CERT_INFO *signer = CRYPT_GetSignerCertInfoFromMsg(hCryptMsg,
2656 signerCert = CertFindCertificateInStore(store,
2657 X509_ASN_ENCODING, 0, CERT_FIND_SUBJECT_CERT, signer,
2661 CryptMemFree(signer);
2666 SetLastError(CRYPT_E_NO_TRUSTED_SIGNER);
2670 if (!(dwFlags & CMSG_SIGNER_ONLY_FLAG))
2671 ret = CryptMsgControl(hCryptMsg, 0, CMSG_CTRL_VERIFY_SIGNATURE,
2672 signerCert->pCertInfo);
2678 *ppSigner = CertDuplicateCertificateContext(signerCert);
2680 *pdwSignerIndex = signerIndex;
2682 CertFreeCertificateContext(signerCert);
2685 CertCloseStore(store, 0);
2689 BOOL WINAPI CryptMsgVerifyCountersignatureEncodedEx(HCRYPTPROV_LEGACY hCryptProv,
2690 DWORD dwEncodingType, PBYTE pbSignerInfo, DWORD cbSignerInfo,
2691 PBYTE pbSignerInfoCountersignature, DWORD cbSignerInfoCountersignature,
2692 DWORD dwSignerType, void *pvSigner, DWORD dwFlags, void *pvReserved)
2694 FIXME("(%08lx, %08x, %p, %d, %p, %d, %d, %p, %08x, %p): stub\n", hCryptProv,
2695 dwEncodingType, pbSignerInfo, cbSignerInfo, pbSignerInfoCountersignature,
2696 cbSignerInfoCountersignature, dwSignerType, pvSigner, dwFlags, pvReserved);