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"
23 #define NONAMELESSUNION
29 #include "wine/debug.h"
30 #include "wine/exception.h"
31 #include "crypt32_private.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
35 /* Called when a message's ref count reaches zero. Free any message-specific
38 typedef void (*CryptMsgCloseFunc)(HCRYPTMSG msg);
40 typedef BOOL (*CryptMsgGetParamFunc)(HCRYPTMSG hCryptMsg, DWORD dwParamType,
41 DWORD dwIndex, void *pvData, DWORD *pcbData);
43 typedef BOOL (*CryptMsgUpdateFunc)(HCRYPTMSG hCryptMsg, const BYTE *pbData,
44 DWORD cbData, BOOL fFinal);
46 typedef BOOL (*CryptMsgControlFunc)(HCRYPTMSG hCryptMsg, DWORD dwFlags,
47 DWORD dwCtrlType, const void *pvCtrlPara);
49 static BOOL CRYPT_DefaultMsgControl(HCRYPTMSG hCryptMsg, DWORD dwFlags,
50 DWORD dwCtrlType, const void *pvCtrlPara)
52 TRACE("(%p, %08x, %d, %p)\n", hCryptMsg, dwFlags, dwCtrlType, pvCtrlPara);
53 SetLastError(E_INVALIDARG);
57 typedef enum _CryptMsgState {
60 MsgStateDataFinalized,
64 typedef struct _CryptMsgBase
69 CMSG_STREAM_INFO stream_info;
71 CryptMsgCloseFunc close;
72 CryptMsgUpdateFunc update;
73 CryptMsgGetParamFunc get_param;
74 CryptMsgControlFunc control;
77 static inline void CryptMsgBase_Init(CryptMsgBase *msg, DWORD dwFlags,
78 PCMSG_STREAM_INFO pStreamInfo, CryptMsgCloseFunc close,
79 CryptMsgGetParamFunc get_param, CryptMsgUpdateFunc update,
80 CryptMsgControlFunc control)
83 msg->open_flags = dwFlags;
87 msg->stream_info = *pStreamInfo;
91 msg->streamed = FALSE;
92 memset(&msg->stream_info, 0, sizeof(msg->stream_info));
95 msg->get_param = get_param;
97 msg->control = control;
98 msg->state = MsgStateInit;
101 typedef struct _CDataEncodeMsg
104 DWORD bare_content_len;
108 static const BYTE empty_data_content[] = { 0x04,0x00 };
110 static void CDataEncodeMsg_Close(HCRYPTMSG hCryptMsg)
112 CDataEncodeMsg *msg = (CDataEncodeMsg *)hCryptMsg;
114 if (msg->bare_content != empty_data_content)
115 LocalFree(msg->bare_content);
118 static BOOL WINAPI CRYPT_EncodeContentLength(DWORD dwCertEncodingType,
119 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
120 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
122 DWORD dataLen = *(DWORD *)pvStructInfo;
126 /* Trick: report bytes needed based on total message length, even though
127 * the message isn't available yet. The caller will use the length
128 * reported here to encode its length.
130 CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
132 *pcbEncoded = 1 + lenBytes + dataLen;
135 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
136 pcbEncoded, 1 + lenBytes)))
138 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
139 pbEncoded = *(BYTE **)pbEncoded;
140 *pbEncoded++ = ASN_OCTETSTRING;
141 CRYPT_EncodeLen(dataLen, pbEncoded,
148 static BOOL CRYPT_EncodeDataContentInfoHeader(CDataEncodeMsg *msg,
149 CRYPT_DATA_BLOB *header)
153 if (msg->base.streamed && msg->base.stream_info.cbContent == 0xffffffff)
155 static const BYTE headerValue[] = { 0x30,0x80,0x06,0x09,0x2a,0x86,0x48,
156 0x86,0xf7,0x0d,0x01,0x07,0x01,0xa0,0x80,0x24,0x80 };
158 header->pbData = LocalAlloc(0, sizeof(headerValue));
161 header->cbData = sizeof(headerValue);
162 memcpy(header->pbData, headerValue, sizeof(headerValue));
170 struct AsnConstructedItem constructed = { 0,
171 &msg->base.stream_info.cbContent, CRYPT_EncodeContentLength };
172 struct AsnEncodeSequenceItem items[2] = {
173 { szOID_RSA_data, CRYPT_AsnEncodeOid, 0 },
174 { &constructed, CRYPT_AsnEncodeConstructed, 0 },
177 ret = CRYPT_AsnEncodeSequence(X509_ASN_ENCODING, items,
178 sizeof(items) / sizeof(items[0]), CRYPT_ENCODE_ALLOC_FLAG, NULL,
179 (LPBYTE)&header->pbData, &header->cbData);
182 /* Trick: subtract the content length from the reported length,
183 * as the actual content hasn't come yet.
185 header->cbData -= msg->base.stream_info.cbContent;
191 static BOOL CDataEncodeMsg_Update(HCRYPTMSG hCryptMsg, const BYTE *pbData,
192 DWORD cbData, BOOL fFinal)
194 CDataEncodeMsg *msg = (CDataEncodeMsg *)hCryptMsg;
197 if (msg->base.state == MsgStateFinalized)
198 SetLastError(CRYPT_E_MSG_ERROR);
199 else if (msg->base.streamed)
203 if (msg->base.state != MsgStateUpdated)
205 CRYPT_DATA_BLOB header;
207 ret = CRYPT_EncodeDataContentInfoHeader(msg, &header);
210 ret = msg->base.stream_info.pfnStreamOutput(
211 msg->base.stream_info.pvArg, header.pbData, header.cbData,
213 LocalFree(header.pbData);
216 /* Curiously, every indefinite-length streamed update appears to
217 * get its own tag and length, regardless of fFinal.
219 if (msg->base.stream_info.cbContent == 0xffffffff)
224 ret = CRYPT_EncodeContentLength(X509_ASN_ENCODING, NULL,
225 &cbData, CRYPT_ENCODE_ALLOC_FLAG, NULL, (BYTE *)&header,
229 ret = msg->base.stream_info.pfnStreamOutput(
230 msg->base.stream_info.pvArg, header, headerLen,
237 ret = msg->base.stream_info.pfnStreamOutput(
238 msg->base.stream_info.pvArg, (BYTE *)pbData, cbData,
240 msg->base.state = MsgStateUpdated;
244 msg->base.state = MsgStateFinalized;
245 if (msg->base.stream_info.cbContent == 0xffffffff)
247 BYTE indefinite_trailer[6] = { 0 };
249 ret = msg->base.stream_info.pfnStreamOutput(
250 msg->base.stream_info.pvArg, (BYTE *)pbData, cbData,
253 ret = msg->base.stream_info.pfnStreamOutput(
254 msg->base.stream_info.pvArg, indefinite_trailer,
255 sizeof(indefinite_trailer), TRUE);
258 ret = msg->base.stream_info.pfnStreamOutput(
259 msg->base.stream_info.pvArg, (BYTE *)pbData, cbData, TRUE);
264 SetLastError(STATUS_ACCESS_VIOLATION);
273 if (msg->base.open_flags & CMSG_DETACHED_FLAG)
274 SetLastError(E_INVALIDARG);
276 SetLastError(CRYPT_E_MSG_ERROR);
280 msg->base.state = MsgStateFinalized;
282 SetLastError(E_INVALIDARG);
285 CRYPT_DATA_BLOB blob = { cbData, (LPBYTE)pbData };
287 /* non-streamed data messages don't allow non-final updates,
288 * don't bother checking whether data already exist, they can't.
290 ret = CryptEncodeObjectEx(X509_ASN_ENCODING, X509_OCTET_STRING,
291 &blob, CRYPT_ENCODE_ALLOC_FLAG, NULL, &msg->bare_content,
292 &msg->bare_content_len);
299 static BOOL CRYPT_CopyParam(void *pvData, DWORD *pcbData, const void *src,
306 else if (*pcbData < len)
309 SetLastError(ERROR_MORE_DATA);
315 memcpy(pvData, src, len);
320 static BOOL CDataEncodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
321 DWORD dwIndex, void *pvData, DWORD *pcbData)
323 CDataEncodeMsg *msg = (CDataEncodeMsg *)hCryptMsg;
328 case CMSG_CONTENT_PARAM:
329 if (msg->base.streamed)
330 SetLastError(E_INVALIDARG);
333 CRYPT_CONTENT_INFO info;
334 char rsa_data[] = "1.2.840.113549.1.7.1";
336 info.pszObjId = rsa_data;
337 info.Content.cbData = msg->bare_content_len;
338 info.Content.pbData = msg->bare_content;
339 ret = CryptEncodeObject(X509_ASN_ENCODING, PKCS_CONTENT_INFO, &info,
343 case CMSG_BARE_CONTENT_PARAM:
344 if (msg->base.streamed)
345 SetLastError(E_INVALIDARG);
347 ret = CRYPT_CopyParam(pvData, pcbData, msg->bare_content,
348 msg->bare_content_len);
351 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
356 static HCRYPTMSG CDataEncodeMsg_Open(DWORD dwFlags, const void *pvMsgEncodeInfo,
357 LPSTR pszInnerContentObjID, PCMSG_STREAM_INFO pStreamInfo)
363 SetLastError(E_INVALIDARG);
366 msg = CryptMemAlloc(sizeof(CDataEncodeMsg));
369 CryptMsgBase_Init((CryptMsgBase *)msg, dwFlags, pStreamInfo,
370 CDataEncodeMsg_Close, CDataEncodeMsg_GetParam, CDataEncodeMsg_Update,
371 CRYPT_DefaultMsgControl);
372 msg->bare_content_len = sizeof(empty_data_content);
373 msg->bare_content = (LPBYTE)empty_data_content;
375 return (HCRYPTMSG)msg;
378 typedef struct _CHashEncodeMsg
383 CRYPT_DATA_BLOB data;
386 static void CHashEncodeMsg_Close(HCRYPTMSG hCryptMsg)
388 CHashEncodeMsg *msg = (CHashEncodeMsg *)hCryptMsg;
390 CryptMemFree(msg->data.pbData);
391 CryptDestroyHash(msg->hash);
392 if (msg->base.open_flags & CMSG_CRYPT_RELEASE_CONTEXT_FLAG)
393 CryptReleaseContext(msg->prov, 0);
396 static BOOL CRYPT_EncodePKCSDigestedData(CHashEncodeMsg *msg, void *pvData,
401 DWORD size = sizeof(algID);
403 ret = CryptGetHashParam(msg->hash, HP_ALGID, (BYTE *)&algID, &size, 0);
406 CRYPT_DIGESTED_DATA digestedData = { 0 };
407 char oid_rsa_data[] = szOID_RSA_data;
409 digestedData.version = CMSG_HASHED_DATA_PKCS_1_5_VERSION;
410 digestedData.DigestAlgorithm.pszObjId = (LPSTR)CertAlgIdToOID(algID);
411 /* FIXME: what about digestedData.DigestAlgorithm.Parameters? */
412 /* Quirk: OID is only encoded messages if an update has happened */
413 if (msg->base.state != MsgStateInit)
414 digestedData.ContentInfo.pszObjId = oid_rsa_data;
415 if (!(msg->base.open_flags & CMSG_DETACHED_FLAG) && msg->data.cbData)
417 ret = CRYPT_AsnEncodeOctets(0, NULL, &msg->data,
418 CRYPT_ENCODE_ALLOC_FLAG, NULL,
419 (LPBYTE)&digestedData.ContentInfo.Content.pbData,
420 &digestedData.ContentInfo.Content.cbData);
422 if (msg->base.state == MsgStateFinalized)
424 size = sizeof(DWORD);
425 ret = CryptGetHashParam(msg->hash, HP_HASHSIZE,
426 (LPBYTE)&digestedData.hash.cbData, &size, 0);
429 digestedData.hash.pbData = CryptMemAlloc(
430 digestedData.hash.cbData);
431 ret = CryptGetHashParam(msg->hash, HP_HASHVAL,
432 digestedData.hash.pbData, &digestedData.hash.cbData, 0);
436 ret = CRYPT_AsnEncodePKCSDigestedData(&digestedData, pvData,
438 CryptMemFree(digestedData.hash.pbData);
439 LocalFree(digestedData.ContentInfo.Content.pbData);
444 static BOOL CHashEncodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
445 DWORD dwIndex, void *pvData, DWORD *pcbData)
447 CHashEncodeMsg *msg = (CHashEncodeMsg *)hCryptMsg;
450 TRACE("(%p, %d, %d, %p, %p)\n", hCryptMsg, dwParamType, dwIndex,
455 case CMSG_BARE_CONTENT_PARAM:
456 if (msg->base.streamed)
457 SetLastError(E_INVALIDARG);
459 ret = CRYPT_EncodePKCSDigestedData(msg, pvData, pcbData);
461 case CMSG_CONTENT_PARAM:
463 CRYPT_CONTENT_INFO info;
465 ret = CryptMsgGetParam(hCryptMsg, CMSG_BARE_CONTENT_PARAM, 0, NULL,
466 &info.Content.cbData);
469 info.Content.pbData = CryptMemAlloc(info.Content.cbData);
470 if (info.Content.pbData)
472 ret = CryptMsgGetParam(hCryptMsg, CMSG_BARE_CONTENT_PARAM, 0,
473 info.Content.pbData, &info.Content.cbData);
476 char oid_rsa_hashed[] = szOID_RSA_hashedData;
478 info.pszObjId = oid_rsa_hashed;
479 ret = CryptEncodeObjectEx(X509_ASN_ENCODING,
480 PKCS_CONTENT_INFO, &info, 0, NULL, pvData, pcbData);
482 CryptMemFree(info.Content.pbData);
489 case CMSG_COMPUTED_HASH_PARAM:
490 ret = CryptGetHashParam(msg->hash, HP_HASHVAL, (BYTE *)pvData, pcbData,
493 case CMSG_VERSION_PARAM:
494 if (msg->base.state != MsgStateFinalized)
495 SetLastError(CRYPT_E_MSG_ERROR);
498 DWORD version = CMSG_HASHED_DATA_PKCS_1_5_VERSION;
500 /* Since the data are always encoded as octets, the version is
501 * always 0 (see rfc3852, section 7)
503 ret = CRYPT_CopyParam(pvData, pcbData, &version, sizeof(version));
507 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
512 static BOOL CHashEncodeMsg_Update(HCRYPTMSG hCryptMsg, const BYTE *pbData,
513 DWORD cbData, BOOL fFinal)
515 CHashEncodeMsg *msg = (CHashEncodeMsg *)hCryptMsg;
518 TRACE("(%p, %p, %d, %d)\n", hCryptMsg, pbData, cbData, fFinal);
520 if (msg->base.state == MsgStateFinalized)
521 SetLastError(CRYPT_E_MSG_ERROR);
522 else if (msg->base.streamed || (msg->base.open_flags & CMSG_DETACHED_FLAG))
524 /* Doesn't do much, as stream output is never called, and you
525 * can't get the content.
527 ret = CryptHashData(msg->hash, pbData, cbData, 0);
528 msg->base.state = fFinal ? MsgStateFinalized : MsgStateUpdated;
533 SetLastError(CRYPT_E_MSG_ERROR);
536 ret = CryptHashData(msg->hash, pbData, cbData, 0);
539 msg->data.pbData = CryptMemAlloc(cbData);
540 if (msg->data.pbData)
542 memcpy(msg->data.pbData + msg->data.cbData, pbData, cbData);
543 msg->data.cbData += cbData;
548 msg->base.state = MsgStateFinalized;
554 static HCRYPTMSG CHashEncodeMsg_Open(DWORD dwFlags, const void *pvMsgEncodeInfo,
555 LPSTR pszInnerContentObjID, PCMSG_STREAM_INFO pStreamInfo)
558 const CMSG_HASHED_ENCODE_INFO *info =
559 (const CMSG_HASHED_ENCODE_INFO *)pvMsgEncodeInfo;
563 if (info->cbSize != sizeof(CMSG_HASHED_ENCODE_INFO))
565 SetLastError(E_INVALIDARG);
568 if (!(algID = CertOIDToAlgId(info->HashAlgorithm.pszObjId)))
570 SetLastError(CRYPT_E_UNKNOWN_ALGO);
573 if (info->hCryptProv)
574 prov = info->hCryptProv;
577 prov = CRYPT_GetDefaultProvider();
578 dwFlags &= ~CMSG_CRYPT_RELEASE_CONTEXT_FLAG;
580 msg = CryptMemAlloc(sizeof(CHashEncodeMsg));
583 CryptMsgBase_Init((CryptMsgBase *)msg, dwFlags, pStreamInfo,
584 CHashEncodeMsg_Close, CHashEncodeMsg_GetParam, CHashEncodeMsg_Update,
585 CRYPT_DefaultMsgControl);
587 msg->data.cbData = 0;
588 msg->data.pbData = NULL;
589 if (!CryptCreateHash(prov, algID, 0, 0, &msg->hash))
595 return (HCRYPTMSG)msg;
598 typedef struct _CMSG_SIGNER_ENCODE_INFO_WITH_CMS
601 PCERT_INFO pCertInfo;
602 HCRYPTPROV hCryptProv;
604 CRYPT_ALGORITHM_IDENTIFIER HashAlgorithm;
607 PCRYPT_ATTRIBUTE rgAuthAttr;
609 PCRYPT_ATTRIBUTE rgUnauthAttr;
611 CRYPT_ALGORITHM_IDENTIFIER HashEncryptionAlgorithm;
612 void *pvHashEncryptionAuxInfo;
613 } CMSG_SIGNER_ENCODE_INFO_WITH_CMS, *PCMSG_SIGNER_ENCODE_INFO_WITH_CMS;
615 typedef struct _CMSG_SIGNED_ENCODE_INFO_WITH_CMS
619 PCMSG_SIGNER_ENCODE_INFO_WITH_CMS rgSigners;
621 PCERT_BLOB rgCertEncoded;
623 PCRL_BLOB rgCrlEncoded;
624 DWORD cAttrCertEncoded;
625 PCERT_BLOB rgAttrCertEncoded;
626 } CMSG_SIGNED_ENCODE_INFO_WITH_CMS, *PCMSG_SIGNED_ENCODE_INFO_WITH_CMS;
628 static BOOL CRYPT_IsValidSigner(CMSG_SIGNER_ENCODE_INFO_WITH_CMS *signer)
630 if (signer->cbSize != sizeof(CMSG_SIGNER_ENCODE_INFO) &&
631 signer->cbSize != sizeof(CMSG_SIGNER_ENCODE_INFO_WITH_CMS))
633 SetLastError(E_INVALIDARG);
636 if (signer->cbSize == sizeof(CMSG_SIGNER_ENCODE_INFO))
638 if (!signer->pCertInfo->SerialNumber.cbData)
640 SetLastError(E_INVALIDARG);
643 if (!signer->pCertInfo->Issuer.cbData)
645 SetLastError(E_INVALIDARG);
649 else if (signer->cbSize == sizeof(CMSG_SIGNER_ENCODE_INFO_WITH_CMS))
651 switch (signer->SignerId.dwIdChoice)
654 if (!signer->pCertInfo->SerialNumber.cbData)
656 SetLastError(E_INVALIDARG);
659 if (!signer->pCertInfo->Issuer.cbData)
661 SetLastError(E_INVALIDARG);
665 case CERT_ID_ISSUER_SERIAL_NUMBER:
666 if (!signer->SignerId.u.IssuerSerialNumber.SerialNumber.cbData)
668 SetLastError(E_INVALIDARG);
671 if (!signer->SignerId.u.IssuerSerialNumber.Issuer.cbData)
673 SetLastError(E_INVALIDARG);
677 case CERT_ID_KEY_IDENTIFIER:
678 if (!signer->SignerId.u.KeyId.cbData)
680 SetLastError(E_INVALIDARG);
685 SetLastError(E_INVALIDARG);
687 if (signer->HashEncryptionAlgorithm.pszObjId)
689 FIXME("CMSG_SIGNER_ENCODE_INFO with CMS fields unsupported\n");
693 if (!signer->hCryptProv)
695 SetLastError(E_INVALIDARG);
698 if (!CertOIDToAlgId(signer->HashAlgorithm.pszObjId))
700 SetLastError(CRYPT_E_UNKNOWN_ALGO);
706 static BOOL CRYPT_ConstructBlob(CRYPT_DATA_BLOB *out, const CRYPT_DATA_BLOB *in)
710 out->cbData = in->cbData;
713 out->pbData = CryptMemAlloc(out->cbData);
715 memcpy(out->pbData, in->pbData, out->cbData);
724 typedef struct _BlobArray
727 PCRYPT_DATA_BLOB blobs;
730 static BOOL CRYPT_ConstructBlobArray(BlobArray *out, const BlobArray *in)
734 out->cBlobs = in->cBlobs;
737 out->blobs = CryptMemAlloc(out->cBlobs * sizeof(CRYPT_DATA_BLOB));
742 memset(out->blobs, 0, out->cBlobs * sizeof(CRYPT_DATA_BLOB));
743 for (i = 0; ret && i < out->cBlobs; i++)
744 ret = CRYPT_ConstructBlob(&out->blobs[i], &in->blobs[i]);
752 static void CRYPT_FreeBlobArray(BlobArray *array)
756 for (i = 0; i < array->cBlobs; i++)
757 CryptMemFree(array->blobs[i].pbData);
758 CryptMemFree(array->blobs);
761 static BOOL CRYPT_ConstructAttribute(CRYPT_ATTRIBUTE *out,
762 const CRYPT_ATTRIBUTE *in)
766 out->pszObjId = CryptMemAlloc(strlen(in->pszObjId) + 1);
769 strcpy(out->pszObjId, in->pszObjId);
770 ret = CRYPT_ConstructBlobArray((BlobArray *)&out->cValue,
771 (const BlobArray *)&in->cValue);
778 static BOOL CRYPT_ConstructAttributes(CRYPT_ATTRIBUTES *out,
779 const CRYPT_ATTRIBUTES *in)
783 out->cAttr = in->cAttr;
786 out->rgAttr = CryptMemAlloc(out->cAttr * sizeof(CRYPT_ATTRIBUTE));
791 memset(out->rgAttr, 0, out->cAttr * sizeof(CRYPT_ATTRIBUTE));
792 for (i = 0; ret && i < out->cAttr; i++)
793 ret = CRYPT_ConstructAttribute(&out->rgAttr[i], &in->rgAttr[i]);
803 /* Constructs a CMSG_CMS_SIGNER_INFO from a CMSG_SIGNER_ENCODE_INFO_WITH_CMS. */
804 static BOOL CSignerInfo_Construct(CMSG_CMS_SIGNER_INFO *info,
805 const CMSG_SIGNER_ENCODE_INFO_WITH_CMS *in)
809 if (in->cbSize == sizeof(CMSG_SIGNER_ENCODE_INFO))
811 info->dwVersion = CMSG_SIGNER_INFO_V1;
812 ret = CRYPT_ConstructBlob(&info->SignerId.u.IssuerSerialNumber.Issuer,
813 &in->pCertInfo->Issuer);
815 ret = CRYPT_ConstructBlob(
816 &info->SignerId.u.IssuerSerialNumber.SerialNumber,
817 &in->pCertInfo->SerialNumber);
818 info->SignerId.dwIdChoice = CERT_ID_ISSUER_SERIAL_NUMBER;
822 /* Implicitly in->cbSize == sizeof(CMSG_SIGNER_ENCODE_INFO_WITH_CMS).
823 * See CRYPT_IsValidSigner.
825 if (!in->SignerId.dwIdChoice)
827 info->dwVersion = CMSG_SIGNER_INFO_V1;
828 ret = CRYPT_ConstructBlob(&info->SignerId.u.IssuerSerialNumber.Issuer,
829 &in->pCertInfo->Issuer);
831 ret = CRYPT_ConstructBlob(
832 &info->SignerId.u.IssuerSerialNumber.SerialNumber,
833 &in->pCertInfo->SerialNumber);
834 info->SignerId.dwIdChoice = CERT_ID_ISSUER_SERIAL_NUMBER;
836 else if (in->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
838 info->dwVersion = CMSG_SIGNER_INFO_V1;
839 info->SignerId.dwIdChoice = CERT_ID_ISSUER_SERIAL_NUMBER;
840 ret = CRYPT_ConstructBlob(&info->SignerId.u.IssuerSerialNumber.Issuer,
841 &in->SignerId.u.IssuerSerialNumber.Issuer);
843 ret = CRYPT_ConstructBlob(
844 &info->SignerId.u.IssuerSerialNumber.SerialNumber,
845 &in->SignerId.u.IssuerSerialNumber.SerialNumber);
849 /* Implicitly dwIdChoice == CERT_ID_KEY_IDENTIFIER */
850 info->dwVersion = CMSG_SIGNER_INFO_V3;
851 info->SignerId.dwIdChoice = CERT_ID_KEY_IDENTIFIER;
852 ret = CRYPT_ConstructBlob(&info->SignerId.u.KeyId,
853 &in->SignerId.u.KeyId);
856 /* Assumption: algorithm IDs will point to static strings, not
857 * stack-based ones, so copying the pointer values is safe.
859 info->HashAlgorithm.pszObjId = in->HashAlgorithm.pszObjId;
861 ret = CRYPT_ConstructBlob(&info->HashAlgorithm.Parameters,
862 &in->HashAlgorithm.Parameters);
863 memset(&info->HashEncryptionAlgorithm, 0,
864 sizeof(info->HashEncryptionAlgorithm));
866 ret = CRYPT_ConstructAttributes(&info->AuthAttrs,
867 (CRYPT_ATTRIBUTES *)&in->cAuthAttr);
869 ret = CRYPT_ConstructAttributes(&info->UnauthAttrs,
870 (CRYPT_ATTRIBUTES *)&in->cUnauthAttr);
874 static void CSignerInfo_Free(CMSG_CMS_SIGNER_INFO *info)
878 if (info->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
880 CryptMemFree(info->SignerId.u.IssuerSerialNumber.Issuer.pbData);
881 CryptMemFree(info->SignerId.u.IssuerSerialNumber.SerialNumber.pbData);
884 CryptMemFree(info->SignerId.u.KeyId.pbData);
885 CryptMemFree(info->HashAlgorithm.Parameters.pbData);
886 CryptMemFree(info->EncryptedHash.pbData);
887 for (i = 0; i < info->AuthAttrs.cAttr; i++)
889 for (j = 0; j < info->AuthAttrs.rgAttr[i].cValue; j++)
890 CryptMemFree(info->AuthAttrs.rgAttr[i].rgValue[j].pbData);
891 CryptMemFree(info->AuthAttrs.rgAttr[i].rgValue);
892 CryptMemFree(info->AuthAttrs.rgAttr[i].pszObjId);
894 CryptMemFree(info->AuthAttrs.rgAttr);
895 for (i = 0; i < info->UnauthAttrs.cAttr; i++)
897 for (j = 0; j < info->UnauthAttrs.rgAttr[i].cValue; j++)
898 CryptMemFree(info->UnauthAttrs.rgAttr[i].rgValue[j].pbData);
899 CryptMemFree(info->UnauthAttrs.rgAttr[i].rgValue);
900 CryptMemFree(info->UnauthAttrs.rgAttr[i].pszObjId);
902 CryptMemFree(info->UnauthAttrs.rgAttr);
905 typedef struct _CSignerHandles
907 HCRYPTHASH contentHash;
908 HCRYPTHASH authAttrHash;
911 typedef struct _CSignedMsgData
913 CRYPT_SIGNED_INFO *info;
915 CSignerHandles *signerHandles;
918 /* Constructs the signer handles for the signerIndex'th signer of msg_data.
919 * Assumes signerIndex is a valid idnex, and that msg_data's info has already
922 static BOOL CSignedMsgData_ConstructSignerHandles(CSignedMsgData *msg_data,
923 DWORD signerIndex, HCRYPTPROV crypt_prov)
928 algID = CertOIDToAlgId(
929 msg_data->info->rgSignerInfo[signerIndex].HashAlgorithm.pszObjId);
930 ret = CryptCreateHash(crypt_prov, algID, 0, 0,
931 &msg_data->signerHandles->contentHash);
932 if (ret && msg_data->info->rgSignerInfo[signerIndex].AuthAttrs.cAttr > 0)
933 ret = CryptCreateHash(crypt_prov, algID, 0, 0,
934 &msg_data->signerHandles->authAttrHash);
938 /* Allocates a CSignedMsgData's handles. Assumes its info has already been
941 static BOOL CSignedMsgData_AllocateHandles(CSignedMsgData *msg_data)
945 if (msg_data->info->cSignerInfo)
947 msg_data->signerHandles =
948 CryptMemAlloc(msg_data->info->cSignerInfo * sizeof(CSignerHandles));
949 if (msg_data->signerHandles)
951 msg_data->cSignerHandle = msg_data->info->cSignerInfo;
952 memset(msg_data->signerHandles, 0,
953 msg_data->info->cSignerInfo * sizeof(CSignerHandles));
957 msg_data->cSignerHandle = 0;
963 msg_data->cSignerHandle = 0;
964 msg_data->signerHandles = NULL;
969 static void CSignedMsgData_CloseHandles(CSignedMsgData *msg_data)
973 for (i = 0; i < msg_data->cSignerHandle; i++)
975 if (msg_data->signerHandles[i].contentHash)
976 CryptDestroyHash(msg_data->signerHandles[i].contentHash);
977 if (msg_data->signerHandles[i].authAttrHash)
978 CryptDestroyHash(msg_data->signerHandles[i].authAttrHash);
980 CryptMemFree(msg_data->signerHandles);
981 msg_data->signerHandles = NULL;
982 msg_data->cSignerHandle = 0;
985 static BOOL CSignedMsgData_UpdateHash(CSignedMsgData *msg_data,
986 const BYTE *pbData, DWORD cbData)
991 for (i = 0; ret && i < msg_data->cSignerHandle; i++)
992 ret = CryptHashData(msg_data->signerHandles[i].contentHash, pbData,
997 static BOOL CRYPT_AppendAttribute(CRYPT_ATTRIBUTES *out,
998 const CRYPT_ATTRIBUTE *in)
1002 out->rgAttr = CryptMemRealloc(out->rgAttr,
1003 (out->cAttr + 1) * sizeof(CRYPT_ATTRIBUTE));
1006 ret = CRYPT_ConstructAttribute(&out->rgAttr[out->cAttr], in);
1013 static BOOL CSignedMsgData_AppendMessageDigestAttribute(
1014 CSignedMsgData *msg_data, DWORD signerIndex)
1018 CRYPT_HASH_BLOB hash = { 0, NULL }, encodedHash = { 0, NULL };
1019 char messageDigest[] = szOID_RSA_messageDigest;
1020 CRYPT_ATTRIBUTE messageDigestAttr = { messageDigest, 1, &encodedHash };
1022 size = sizeof(DWORD);
1023 ret = CryptGetHashParam(
1024 msg_data->signerHandles[signerIndex].contentHash, HP_HASHSIZE,
1025 (LPBYTE)&hash.cbData, &size, 0);
1028 hash.pbData = CryptMemAlloc(hash.cbData);
1029 ret = CryptGetHashParam(
1030 msg_data->signerHandles[signerIndex].contentHash, HP_HASHVAL,
1031 hash.pbData, &hash.cbData, 0);
1034 ret = CRYPT_AsnEncodeOctets(0, NULL, &hash, CRYPT_ENCODE_ALLOC_FLAG,
1035 NULL, (LPBYTE)&encodedHash.pbData, &encodedHash.cbData);
1038 ret = CRYPT_AppendAttribute(
1039 &msg_data->info->rgSignerInfo[signerIndex].AuthAttrs,
1040 &messageDigestAttr);
1041 LocalFree(encodedHash.pbData);
1044 CryptMemFree(hash.pbData);
1054 static BOOL CSignedMsgData_UpdateAuthenticatedAttributes(
1055 CSignedMsgData *msg_data, SignOrVerify flag)
1060 TRACE("(%p)\n", msg_data);
1062 for (i = 0; ret && i < msg_data->info->cSignerInfo; i++)
1064 if (msg_data->info->rgSignerInfo[i].AuthAttrs.cAttr)
1068 BYTE oid_rsa_data_encoded[] = { 0x06,0x09,0x2a,0x86,0x48,0x86,
1069 0xf7,0x0d,0x01,0x07,0x01 };
1070 CRYPT_DATA_BLOB content = { sizeof(oid_rsa_data_encoded),
1071 oid_rsa_data_encoded };
1072 char contentType[] = szOID_RSA_contentType;
1073 CRYPT_ATTRIBUTE contentTypeAttr = { contentType, 1, &content };
1075 /* FIXME: does this depend on inner OID? */
1076 ret = CRYPT_AppendAttribute(
1077 &msg_data->info->rgSignerInfo[i].AuthAttrs, &contentTypeAttr);
1079 ret = CSignedMsgData_AppendMessageDigestAttribute(msg_data,
1084 LPBYTE encodedAttrs;
1087 ret = CryptEncodeObjectEx(X509_ASN_ENCODING, PKCS_ATTRIBUTES,
1088 &msg_data->info->rgSignerInfo[i].AuthAttrs,
1089 CRYPT_ENCODE_ALLOC_FLAG, NULL, (LPBYTE)&encodedAttrs, &size);
1092 ret = CryptHashData(
1093 msg_data->signerHandles[i].authAttrHash, encodedAttrs,
1095 LocalFree(encodedAttrs);
1100 TRACE("returning %d\n", ret);
1104 static void CRYPT_ReverseBytes(CRYPT_HASH_BLOB *hash)
1109 for (i = 0; i < hash->cbData / 2; i++)
1111 tmp = hash->pbData[hash->cbData - i - 1];
1112 hash->pbData[hash->cbData - i - 1] = hash->pbData[i];
1113 hash->pbData[i] = tmp;
1117 static BOOL CSignedMsgData_Sign(CSignedMsgData *msg_data)
1122 TRACE("(%p)\n", msg_data);
1124 for (i = 0; ret && i < msg_data->info->cSignerInfo; i++)
1128 if (msg_data->info->rgSignerInfo[i].AuthAttrs.cAttr)
1129 hash = msg_data->signerHandles[i].authAttrHash;
1131 hash = msg_data->signerHandles[i].contentHash;
1132 ret = CryptSignHashW(hash, AT_SIGNATURE, NULL, 0, NULL,
1133 &msg_data->info->rgSignerInfo[i].EncryptedHash.cbData);
1136 msg_data->info->rgSignerInfo[i].EncryptedHash.pbData =
1138 msg_data->info->rgSignerInfo[i].EncryptedHash.cbData);
1139 if (msg_data->info->rgSignerInfo[i].EncryptedHash.pbData)
1141 ret = CryptSignHashW(hash, AT_SIGNATURE, NULL, 0,
1142 msg_data->info->rgSignerInfo[i].EncryptedHash.pbData,
1143 &msg_data->info->rgSignerInfo[i].EncryptedHash.cbData);
1146 &msg_data->info->rgSignerInfo[i].EncryptedHash);
1155 static BOOL CSignedMsgData_Update(CSignedMsgData *msg_data,
1156 const BYTE *pbData, DWORD cbData, BOOL fFinal, SignOrVerify flag)
1158 BOOL ret = CSignedMsgData_UpdateHash(msg_data, pbData, cbData);
1162 ret = CSignedMsgData_UpdateAuthenticatedAttributes(msg_data, flag);
1163 if (ret && flag == Sign)
1164 ret = CSignedMsgData_Sign(msg_data);
1169 typedef struct _CSignedEncodeMsg
1173 CRYPT_DATA_BLOB data;
1174 CSignedMsgData msg_data;
1177 static void CSignedEncodeMsg_Close(HCRYPTMSG hCryptMsg)
1179 CSignedEncodeMsg *msg = (CSignedEncodeMsg *)hCryptMsg;
1182 CryptMemFree(msg->innerOID);
1183 CryptMemFree(msg->data.pbData);
1184 CRYPT_FreeBlobArray((BlobArray *)&msg->msg_data.info->cCertEncoded);
1185 CRYPT_FreeBlobArray((BlobArray *)&msg->msg_data.info->cCrlEncoded);
1186 for (i = 0; i < msg->msg_data.info->cSignerInfo; i++)
1187 CSignerInfo_Free(&msg->msg_data.info->rgSignerInfo[i]);
1188 CSignedMsgData_CloseHandles(&msg->msg_data);
1189 CryptMemFree(msg->msg_data.info->rgSignerInfo);
1190 CryptMemFree(msg->msg_data.info);
1193 static BOOL CSignedEncodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
1194 DWORD dwIndex, void *pvData, DWORD *pcbData)
1196 CSignedEncodeMsg *msg = (CSignedEncodeMsg *)hCryptMsg;
1199 switch (dwParamType)
1201 case CMSG_CONTENT_PARAM:
1203 CRYPT_CONTENT_INFO info;
1205 ret = CryptMsgGetParam(hCryptMsg, CMSG_BARE_CONTENT_PARAM, 0, NULL,
1206 &info.Content.cbData);
1209 info.Content.pbData = CryptMemAlloc(info.Content.cbData);
1210 if (info.Content.pbData)
1212 ret = CryptMsgGetParam(hCryptMsg, CMSG_BARE_CONTENT_PARAM, 0,
1213 info.Content.pbData, &info.Content.cbData);
1216 char oid_rsa_signed[] = szOID_RSA_signedData;
1218 info.pszObjId = oid_rsa_signed;
1219 ret = CryptEncodeObjectEx(X509_ASN_ENCODING,
1220 PKCS_CONTENT_INFO, &info, 0, NULL, pvData, pcbData);
1222 CryptMemFree(info.Content.pbData);
1229 case CMSG_BARE_CONTENT_PARAM:
1231 CRYPT_SIGNED_INFO info;
1232 BOOL freeContent = FALSE;
1234 info = *msg->msg_data.info;
1235 if (!msg->innerOID || !strcmp(msg->innerOID, szOID_RSA_data))
1237 char oid_rsa_data[] = szOID_RSA_data;
1239 /* Quirk: OID is only encoded messages if an update has happened */
1240 if (msg->base.state != MsgStateInit)
1241 info.content.pszObjId = oid_rsa_data;
1243 info.content.pszObjId = NULL;
1244 if (msg->data.cbData)
1246 CRYPT_DATA_BLOB blob = { msg->data.cbData, msg->data.pbData };
1248 ret = CryptEncodeObjectEx(X509_ASN_ENCODING, X509_OCTET_STRING,
1249 &blob, CRYPT_ENCODE_ALLOC_FLAG, NULL,
1250 &info.content.Content.pbData, &info.content.Content.cbData);
1255 info.content.Content.cbData = 0;
1256 info.content.Content.pbData = NULL;
1262 info.content.pszObjId = msg->innerOID;
1263 info.content.Content.cbData = msg->data.cbData;
1264 info.content.Content.pbData = msg->data.pbData;
1269 ret = CRYPT_AsnEncodeCMSSignedInfo(&info, pvData, pcbData);
1271 LocalFree(info.content.Content.pbData);
1275 case CMSG_COMPUTED_HASH_PARAM:
1276 if (dwIndex >= msg->msg_data.cSignerHandle)
1277 SetLastError(CRYPT_E_INVALID_INDEX);
1279 ret = CryptGetHashParam(
1280 msg->msg_data.signerHandles[dwIndex].contentHash, HP_HASHVAL,
1281 pvData, pcbData, 0);
1283 case CMSG_ENCODED_SIGNER:
1284 if (dwIndex >= msg->msg_data.info->cSignerInfo)
1285 SetLastError(CRYPT_E_INVALID_INDEX);
1287 ret = CryptEncodeObjectEx(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
1288 CMS_SIGNER_INFO, &msg->msg_data.info->rgSignerInfo[dwIndex], 0,
1289 NULL, pvData, pcbData);
1291 case CMSG_VERSION_PARAM:
1292 ret = CRYPT_CopyParam(pvData, pcbData, &msg->msg_data.info->version,
1293 sizeof(msg->msg_data.info->version));
1296 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1301 static BOOL CSignedEncodeMsg_Update(HCRYPTMSG hCryptMsg, const BYTE *pbData,
1302 DWORD cbData, BOOL fFinal)
1304 CSignedEncodeMsg *msg = (CSignedEncodeMsg *)hCryptMsg;
1307 if (msg->base.state == MsgStateFinalized)
1308 SetLastError(CRYPT_E_MSG_ERROR);
1309 else if (msg->base.streamed || (msg->base.open_flags & CMSG_DETACHED_FLAG))
1311 ret = CSignedMsgData_Update(&msg->msg_data, pbData, cbData, fFinal,
1313 if (msg->base.streamed)
1314 FIXME("streamed partial stub\n");
1315 msg->base.state = fFinal ? MsgStateFinalized : MsgStateUpdated;
1320 SetLastError(CRYPT_E_MSG_ERROR);
1325 msg->data.pbData = CryptMemAlloc(cbData);
1326 if (msg->data.pbData)
1328 memcpy(msg->data.pbData, pbData, cbData);
1329 msg->data.cbData = cbData;
1336 ret = CSignedMsgData_Update(&msg->msg_data, pbData, cbData,
1338 msg->base.state = MsgStateFinalized;
1344 static HCRYPTMSG CSignedEncodeMsg_Open(DWORD dwFlags,
1345 const void *pvMsgEncodeInfo, LPSTR pszInnerContentObjID,
1346 PCMSG_STREAM_INFO pStreamInfo)
1348 const CMSG_SIGNED_ENCODE_INFO_WITH_CMS *info =
1349 (const CMSG_SIGNED_ENCODE_INFO_WITH_CMS *)pvMsgEncodeInfo;
1351 CSignedEncodeMsg *msg;
1353 if (info->cbSize != sizeof(CMSG_SIGNED_ENCODE_INFO) &&
1354 info->cbSize != sizeof(CMSG_SIGNED_ENCODE_INFO_WITH_CMS))
1356 SetLastError(E_INVALIDARG);
1359 if (info->cbSize == sizeof(CMSG_SIGNED_ENCODE_INFO_WITH_CMS) &&
1360 info->cAttrCertEncoded)
1362 FIXME("CMSG_SIGNED_ENCODE_INFO with CMS fields unsupported\n");
1365 for (i = 0; i < info->cSigners; i++)
1366 if (!CRYPT_IsValidSigner(&info->rgSigners[i]))
1368 msg = CryptMemAlloc(sizeof(CSignedEncodeMsg));
1373 CryptMsgBase_Init((CryptMsgBase *)msg, dwFlags, pStreamInfo,
1374 CSignedEncodeMsg_Close, CSignedEncodeMsg_GetParam,
1375 CSignedEncodeMsg_Update, CRYPT_DefaultMsgControl);
1376 if (pszInnerContentObjID)
1378 msg->innerOID = CryptMemAlloc(strlen(pszInnerContentObjID) + 1);
1380 strcpy(msg->innerOID, pszInnerContentObjID);
1385 msg->innerOID = NULL;
1386 msg->data.cbData = 0;
1387 msg->data.pbData = NULL;
1389 msg->msg_data.info = CryptMemAlloc(sizeof(CRYPT_SIGNED_INFO));
1391 msg->msg_data.info = NULL;
1392 if (msg->msg_data.info)
1394 memset(msg->msg_data.info, 0, sizeof(CRYPT_SIGNED_INFO));
1395 msg->msg_data.info->version = CMSG_SIGNED_DATA_V1;
1403 msg->msg_data.info->rgSignerInfo =
1404 CryptMemAlloc(info->cSigners * sizeof(CMSG_CMS_SIGNER_INFO));
1405 if (msg->msg_data.info->rgSignerInfo)
1407 msg->msg_data.info->cSignerInfo = info->cSigners;
1408 memset(msg->msg_data.info->rgSignerInfo, 0,
1409 msg->msg_data.info->cSignerInfo *
1410 sizeof(CMSG_CMS_SIGNER_INFO));
1411 ret = CSignedMsgData_AllocateHandles(&msg->msg_data);
1412 for (i = 0; ret && i < msg->msg_data.info->cSignerInfo; i++)
1414 if (info->rgSigners[i].SignerId.dwIdChoice ==
1415 CERT_ID_KEY_IDENTIFIER)
1416 msg->msg_data.info->version = CMSG_SIGNED_DATA_V3;
1417 ret = CSignerInfo_Construct(
1418 &msg->msg_data.info->rgSignerInfo[i],
1419 &info->rgSigners[i]);
1422 ret = CSignedMsgData_ConstructSignerHandles(
1423 &msg->msg_data, i, info->rgSigners[i].hCryptProv);
1424 if (dwFlags & CMSG_CRYPT_RELEASE_CONTEXT_FLAG)
1425 CryptReleaseContext(info->rgSigners[i].hCryptProv,
1435 msg->msg_data.info->cSignerInfo = 0;
1436 msg->msg_data.signerHandles = NULL;
1437 msg->msg_data.cSignerHandle = 0;
1441 ret = CRYPT_ConstructBlobArray(
1442 (BlobArray *)&msg->msg_data.info->cCertEncoded,
1443 (const BlobArray *)&info->cCertEncoded);
1445 ret = CRYPT_ConstructBlobArray(
1446 (BlobArray *)&msg->msg_data.info->cCrlEncoded,
1447 (const BlobArray *)&info->cCrlEncoded);
1450 CSignedEncodeMsg_Close(msg);
1457 HCRYPTMSG WINAPI CryptMsgOpenToEncode(DWORD dwMsgEncodingType, DWORD dwFlags,
1458 DWORD dwMsgType, const void *pvMsgEncodeInfo, LPSTR pszInnerContentObjID,
1459 PCMSG_STREAM_INFO pStreamInfo)
1461 HCRYPTMSG msg = NULL;
1463 TRACE("(%08x, %08x, %08x, %p, %s, %p)\n", dwMsgEncodingType, dwFlags,
1464 dwMsgType, pvMsgEncodeInfo, debugstr_a(pszInnerContentObjID), pStreamInfo);
1466 if (GET_CMSG_ENCODING_TYPE(dwMsgEncodingType) != PKCS_7_ASN_ENCODING)
1468 SetLastError(E_INVALIDARG);
1474 msg = CDataEncodeMsg_Open(dwFlags, pvMsgEncodeInfo,
1475 pszInnerContentObjID, pStreamInfo);
1478 msg = CHashEncodeMsg_Open(dwFlags, pvMsgEncodeInfo,
1479 pszInnerContentObjID, pStreamInfo);
1482 msg = CSignedEncodeMsg_Open(dwFlags, pvMsgEncodeInfo,
1483 pszInnerContentObjID, pStreamInfo);
1485 case CMSG_ENVELOPED:
1486 FIXME("unimplemented for type CMSG_ENVELOPED\n");
1488 case CMSG_SIGNED_AND_ENVELOPED:
1489 case CMSG_ENCRYPTED:
1490 /* defined but invalid, fall through */
1492 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1497 typedef struct _CDecodeMsg
1501 HCRYPTPROV crypt_prov;
1504 CSignedMsgData signed_data;
1506 CRYPT_DATA_BLOB msg_data;
1507 CRYPT_DATA_BLOB detached_data;
1508 PCONTEXT_PROPERTY_LIST properties;
1511 static void CDecodeMsg_Close(HCRYPTMSG hCryptMsg)
1513 CDecodeMsg *msg = (CDecodeMsg *)hCryptMsg;
1515 if (msg->base.open_flags & CMSG_CRYPT_RELEASE_CONTEXT_FLAG)
1516 CryptReleaseContext(msg->crypt_prov, 0);
1521 CryptDestroyHash(msg->u.hash);
1524 if (msg->u.signed_data.info)
1526 LocalFree(msg->u.signed_data.info);
1527 CSignedMsgData_CloseHandles(&msg->u.signed_data);
1531 CryptMemFree(msg->msg_data.pbData);
1532 CryptMemFree(msg->detached_data.pbData);
1533 ContextPropertyList_Free(msg->properties);
1536 static BOOL CDecodeMsg_CopyData(CRYPT_DATA_BLOB *blob, const BYTE *pbData,
1544 blob->pbData = CryptMemRealloc(blob->pbData,
1545 blob->cbData + cbData);
1547 blob->pbData = CryptMemAlloc(cbData);
1550 memcpy(blob->pbData + blob->cbData, pbData, cbData);
1551 blob->cbData += cbData;
1559 static BOOL CDecodeMsg_DecodeDataContent(CDecodeMsg *msg, CRYPT_DER_BLOB *blob)
1562 CRYPT_DATA_BLOB *data;
1565 ret = CryptDecodeObjectEx(X509_ASN_ENCODING, X509_OCTET_STRING,
1566 blob->pbData, blob->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, (LPBYTE)&data,
1570 ret = ContextPropertyList_SetProperty(msg->properties,
1571 CMSG_CONTENT_PARAM, data->pbData, data->cbData);
1577 static void CDecodeMsg_SaveAlgorithmID(CDecodeMsg *msg, DWORD param,
1578 const CRYPT_ALGORITHM_IDENTIFIER *id)
1580 static const BYTE nullParams[] = { ASN_NULL, 0 };
1581 CRYPT_ALGORITHM_IDENTIFIER *copy;
1582 DWORD len = sizeof(CRYPT_ALGORITHM_IDENTIFIER);
1584 /* Linearize algorithm id */
1585 len += strlen(id->pszObjId) + 1;
1586 len += id->Parameters.cbData;
1587 copy = CryptMemAlloc(len);
1591 (LPSTR)((BYTE *)copy + sizeof(CRYPT_ALGORITHM_IDENTIFIER));
1592 strcpy(copy->pszObjId, id->pszObjId);
1593 copy->Parameters.pbData = (BYTE *)copy->pszObjId + strlen(id->pszObjId)
1595 /* Trick: omit NULL parameters */
1596 if (id->Parameters.cbData == sizeof(nullParams) &&
1597 !memcmp(id->Parameters.pbData, nullParams, sizeof(nullParams)))
1599 copy->Parameters.cbData = 0;
1600 len -= sizeof(nullParams);
1603 copy->Parameters.cbData = id->Parameters.cbData;
1604 if (copy->Parameters.cbData)
1605 memcpy(copy->Parameters.pbData, id->Parameters.pbData,
1606 id->Parameters.cbData);
1607 ContextPropertyList_SetProperty(msg->properties, param, (BYTE *)copy,
1613 static inline void CRYPT_FixUpAlgorithmID(CRYPT_ALGORITHM_IDENTIFIER *id)
1615 id->pszObjId = (LPSTR)((BYTE *)id + sizeof(CRYPT_ALGORITHM_IDENTIFIER));
1616 id->Parameters.pbData = (BYTE *)id->pszObjId + strlen(id->pszObjId) + 1;
1619 static BOOL CDecodeMsg_DecodeHashedContent(CDecodeMsg *msg,
1620 CRYPT_DER_BLOB *blob)
1623 CRYPT_DIGESTED_DATA *digestedData;
1626 ret = CRYPT_AsnDecodePKCSDigestedData(blob->pbData, blob->cbData,
1627 CRYPT_DECODE_ALLOC_FLAG, NULL, (CRYPT_DIGESTED_DATA *)&digestedData,
1631 ContextPropertyList_SetProperty(msg->properties, CMSG_VERSION_PARAM,
1632 (const BYTE *)&digestedData->version, sizeof(digestedData->version));
1633 CDecodeMsg_SaveAlgorithmID(msg, CMSG_HASH_ALGORITHM_PARAM,
1634 &digestedData->DigestAlgorithm);
1635 ContextPropertyList_SetProperty(msg->properties,
1636 CMSG_INNER_CONTENT_TYPE_PARAM,
1637 (const BYTE *)digestedData->ContentInfo.pszObjId,
1638 digestedData->ContentInfo.pszObjId ?
1639 strlen(digestedData->ContentInfo.pszObjId) + 1 : 0);
1640 if (!(msg->base.open_flags & CMSG_DETACHED_FLAG))
1642 if (digestedData->ContentInfo.Content.cbData)
1643 CDecodeMsg_DecodeDataContent(msg,
1644 &digestedData->ContentInfo.Content);
1646 ContextPropertyList_SetProperty(msg->properties,
1647 CMSG_CONTENT_PARAM, NULL, 0);
1649 ContextPropertyList_SetProperty(msg->properties, CMSG_HASH_DATA_PARAM,
1650 digestedData->hash.pbData, digestedData->hash.cbData);
1651 LocalFree(digestedData);
1656 static BOOL CDecodeMsg_DecodeSignedContent(CDecodeMsg *msg,
1657 CRYPT_DER_BLOB *blob)
1660 CRYPT_SIGNED_INFO *signedInfo;
1663 ret = CRYPT_AsnDecodeCMSSignedInfo(blob->pbData, blob->cbData,
1664 CRYPT_DECODE_ALLOC_FLAG, NULL, (CRYPT_SIGNED_INFO *)&signedInfo,
1667 msg->u.signed_data.info = signedInfo;
1671 /* Decodes the content in blob as the type given, and updates the value
1672 * (type, parameters, etc.) of msg based on what blob contains.
1673 * It doesn't just use msg's type, to allow a recursive call from an implicitly
1674 * typed message once the outer content info has been decoded.
1676 static BOOL CDecodeMsg_DecodeContent(CDecodeMsg *msg, CRYPT_DER_BLOB *blob,
1684 if ((ret = CDecodeMsg_DecodeDataContent(msg, blob)))
1685 msg->type = CMSG_DATA;
1688 if ((ret = CDecodeMsg_DecodeHashedContent(msg, blob)))
1689 msg->type = CMSG_HASHED;
1691 case CMSG_ENVELOPED:
1692 FIXME("unimplemented for type CMSG_ENVELOPED\n");
1696 if ((ret = CDecodeMsg_DecodeSignedContent(msg, blob)))
1697 msg->type = CMSG_SIGNED;
1701 CRYPT_CONTENT_INFO *info;
1704 ret = CryptDecodeObjectEx(X509_ASN_ENCODING, PKCS_CONTENT_INFO,
1705 msg->msg_data.pbData, msg->msg_data.cbData, CRYPT_DECODE_ALLOC_FLAG,
1706 NULL, (LPBYTE)&info, &size);
1709 if (!strcmp(info->pszObjId, szOID_RSA_data))
1710 ret = CDecodeMsg_DecodeContent(msg, &info->Content, CMSG_DATA);
1711 else if (!strcmp(info->pszObjId, szOID_RSA_digestedData))
1712 ret = CDecodeMsg_DecodeContent(msg, &info->Content,
1714 else if (!strcmp(info->pszObjId, szOID_RSA_envelopedData))
1715 ret = CDecodeMsg_DecodeContent(msg, &info->Content,
1717 else if (!strcmp(info->pszObjId, szOID_RSA_signedData))
1718 ret = CDecodeMsg_DecodeContent(msg, &info->Content,
1722 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1732 static BOOL CDecodeMsg_FinalizeHashedContent(CDecodeMsg *msg,
1733 CRYPT_DER_BLOB *blob)
1735 CRYPT_ALGORITHM_IDENTIFIER *hashAlgoID = NULL;
1740 CryptMsgGetParam(msg, CMSG_HASH_ALGORITHM_PARAM, 0, NULL, &size);
1741 hashAlgoID = CryptMemAlloc(size);
1742 ret = CryptMsgGetParam(msg, CMSG_HASH_ALGORITHM_PARAM, 0, hashAlgoID,
1745 algID = CertOIDToAlgId(hashAlgoID->pszObjId);
1746 ret = CryptCreateHash(msg->crypt_prov, algID, 0, 0, &msg->u.hash);
1749 CRYPT_DATA_BLOB content;
1751 if (msg->base.open_flags & CMSG_DETACHED_FLAG)
1753 /* Unlike for non-detached messages, the data were never stored as
1754 * the content param, but were saved in msg->detached_data instead.
1756 content.pbData = msg->detached_data.pbData;
1757 content.cbData = msg->detached_data.cbData;
1760 ret = ContextPropertyList_FindProperty(msg->properties,
1761 CMSG_CONTENT_PARAM, &content);
1763 ret = CryptHashData(msg->u.hash, content.pbData, content.cbData, 0);
1765 CryptMemFree(hashAlgoID);
1769 static BOOL CDecodeMsg_FinalizeSignedContent(CDecodeMsg *msg,
1770 CRYPT_DER_BLOB *blob)
1775 ret = CSignedMsgData_AllocateHandles(&msg->u.signed_data);
1776 for (i = 0; ret && i < msg->u.signed_data.info->cSignerInfo; i++)
1777 ret = CSignedMsgData_ConstructSignerHandles(&msg->u.signed_data, i,
1781 CRYPT_DATA_BLOB *content;
1783 /* Now that we have all the content, update the hash handles with
1784 * it. If the message is a detached message, the content is stored
1785 * in msg->detached_data rather than in the signed message's
1788 if (msg->base.open_flags & CMSG_DETACHED_FLAG)
1789 content = &msg->detached_data;
1791 content = &msg->u.signed_data.info->content.Content;
1792 if (content->cbData)
1794 /* If the message is not detached, have to decode the message's
1795 * content if the type is szOID_RSA_data.
1797 if (!(msg->base.open_flags & CMSG_DETACHED_FLAG) &&
1798 !strcmp(msg->u.signed_data.info->content.pszObjId,
1801 CRYPT_DATA_BLOB *blob;
1803 ret = CryptDecodeObjectEx(X509_ASN_ENCODING,
1804 X509_OCTET_STRING, content->pbData, content->cbData,
1805 CRYPT_DECODE_ALLOC_FLAG, NULL, (LPBYTE)&blob, &size);
1808 ret = CSignedMsgData_Update(&msg->u.signed_data,
1809 blob->pbData, blob->cbData, TRUE, Verify);
1814 ret = CSignedMsgData_Update(&msg->u.signed_data,
1815 content->pbData, content->cbData, TRUE, Verify);
1821 static BOOL CDecodeMsg_FinalizeContent(CDecodeMsg *msg, CRYPT_DER_BLOB *blob)
1828 ret = CDecodeMsg_FinalizeHashedContent(msg, blob);
1831 ret = CDecodeMsg_FinalizeSignedContent(msg, blob);
1839 static BOOL CDecodeMsg_Update(HCRYPTMSG hCryptMsg, const BYTE *pbData,
1840 DWORD cbData, BOOL fFinal)
1842 CDecodeMsg *msg = (CDecodeMsg *)hCryptMsg;
1845 TRACE("(%p, %p, %d, %d)\n", hCryptMsg, pbData, cbData, fFinal);
1847 if (msg->base.state == MsgStateFinalized)
1848 SetLastError(CRYPT_E_MSG_ERROR);
1849 else if (msg->base.streamed)
1851 FIXME("(%p, %p, %d, %d): streamed update stub\n", hCryptMsg, pbData,
1853 switch (msg->base.state)
1856 ret = CDecodeMsg_CopyData(&msg->msg_data, pbData, cbData);
1859 if (msg->base.open_flags & CMSG_DETACHED_FLAG)
1860 msg->base.state = MsgStateDataFinalized;
1862 msg->base.state = MsgStateFinalized;
1865 msg->base.state = MsgStateUpdated;
1867 case MsgStateUpdated:
1868 ret = CDecodeMsg_CopyData(&msg->msg_data, pbData, cbData);
1871 if (msg->base.open_flags & CMSG_DETACHED_FLAG)
1872 msg->base.state = MsgStateDataFinalized;
1874 msg->base.state = MsgStateFinalized;
1877 case MsgStateDataFinalized:
1878 ret = CDecodeMsg_CopyData(&msg->detached_data, pbData, cbData);
1880 msg->base.state = MsgStateFinalized;
1883 SetLastError(CRYPT_E_MSG_ERROR);
1890 SetLastError(CRYPT_E_MSG_ERROR);
1893 switch (msg->base.state)
1896 ret = CDecodeMsg_CopyData(&msg->msg_data, pbData, cbData);
1897 if (msg->base.open_flags & CMSG_DETACHED_FLAG)
1898 msg->base.state = MsgStateDataFinalized;
1900 msg->base.state = MsgStateFinalized;
1902 case MsgStateDataFinalized:
1903 ret = CDecodeMsg_CopyData(&msg->detached_data, pbData, cbData);
1904 msg->base.state = MsgStateFinalized;
1907 SetLastError(CRYPT_E_MSG_ERROR);
1911 if (ret && fFinal &&
1912 ((msg->base.open_flags & CMSG_DETACHED_FLAG && msg->base.state ==
1913 MsgStateDataFinalized) ||
1914 (!(msg->base.open_flags & CMSG_DETACHED_FLAG) && msg->base.state ==
1915 MsgStateFinalized)))
1916 ret = CDecodeMsg_DecodeContent(msg, &msg->msg_data, msg->type);
1917 if (ret && msg->base.state == MsgStateFinalized)
1918 ret = CDecodeMsg_FinalizeContent(msg, &msg->msg_data);
1922 static BOOL CDecodeHashMsg_GetParam(CDecodeMsg *msg, DWORD dwParamType,
1923 DWORD dwIndex, void *pvData, DWORD *pcbData)
1927 switch (dwParamType)
1929 case CMSG_TYPE_PARAM:
1930 ret = CRYPT_CopyParam(pvData, pcbData, &msg->type, sizeof(msg->type));
1932 case CMSG_HASH_ALGORITHM_PARAM:
1934 CRYPT_DATA_BLOB blob;
1936 ret = ContextPropertyList_FindProperty(msg->properties, dwParamType,
1940 ret = CRYPT_CopyParam(pvData, pcbData, blob.pbData, blob.cbData);
1942 CRYPT_FixUpAlgorithmID((CRYPT_ALGORITHM_IDENTIFIER *)pvData);
1945 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1948 case CMSG_COMPUTED_HASH_PARAM:
1949 ret = CryptGetHashParam(msg->u.hash, HP_HASHVAL, pvData, pcbData, 0);
1953 CRYPT_DATA_BLOB blob;
1955 ret = ContextPropertyList_FindProperty(msg->properties, dwParamType,
1958 ret = CRYPT_CopyParam(pvData, pcbData, blob.pbData, blob.cbData);
1960 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1966 /* nextData is an in/out parameter - on input it's the memory location in
1967 * which a copy of in's data should be made, and on output it's the memory
1968 * location immediately after out's copy of in's data.
1970 static inline void CRYPT_CopyBlob(CRYPT_DATA_BLOB *out,
1971 const CRYPT_DATA_BLOB *in, LPBYTE *nextData)
1973 out->cbData = in->cbData;
1976 out->pbData = *nextData;
1977 memcpy(out->pbData, in->pbData, in->cbData);
1978 *nextData += in->cbData;
1982 static inline void CRYPT_CopyAlgorithmId(CRYPT_ALGORITHM_IDENTIFIER *out,
1983 const CRYPT_ALGORITHM_IDENTIFIER *in, LPBYTE *nextData)
1987 out->pszObjId = (LPSTR)*nextData;
1988 strcpy(out->pszObjId, in->pszObjId);
1989 *nextData += strlen(out->pszObjId) + 1;
1991 CRYPT_CopyBlob(&out->Parameters, &in->Parameters, nextData);
1994 static inline void CRYPT_CopyAttributes(CRYPT_ATTRIBUTES *out,
1995 const CRYPT_ATTRIBUTES *in, LPBYTE *nextData)
1997 out->cAttr = in->cAttr;
2002 *nextData = POINTER_ALIGN_DWORD_PTR(*nextData);
2003 out->rgAttr = (CRYPT_ATTRIBUTE *)*nextData;
2004 *nextData += in->cAttr * sizeof(CRYPT_ATTRIBUTE);
2005 for (i = 0; i < in->cAttr; i++)
2007 if (in->rgAttr[i].pszObjId)
2009 out->rgAttr[i].pszObjId = (LPSTR)*nextData;
2010 strcpy(out->rgAttr[i].pszObjId, in->rgAttr[i].pszObjId);
2011 *nextData += strlen(in->rgAttr[i].pszObjId) + 1;
2013 if (in->rgAttr[i].cValue)
2017 out->rgAttr[i].cValue = in->rgAttr[i].cValue;
2018 *nextData = POINTER_ALIGN_DWORD_PTR(*nextData);
2019 out->rgAttr[i].rgValue = (PCRYPT_DATA_BLOB)*nextData;
2020 *nextData += in->rgAttr[i].cValue * sizeof(CRYPT_DATA_BLOB);
2021 for (j = 0; j < in->rgAttr[i].cValue; j++)
2022 CRYPT_CopyBlob(&out->rgAttr[i].rgValue[j],
2023 &in->rgAttr[i].rgValue[j], nextData);
2029 static DWORD CRYPT_SizeOfAttributes(const CRYPT_ATTRIBUTES *attr)
2031 DWORD size = attr->cAttr * sizeof(CRYPT_ATTRIBUTE), i, j;
2033 for (i = 0; i < attr->cAttr; i++)
2035 if (attr->rgAttr[i].pszObjId)
2036 size += strlen(attr->rgAttr[i].pszObjId) + 1;
2038 size = ALIGN_DWORD_PTR(size);
2039 size += attr->rgAttr[i].cValue * sizeof(CRYPT_DATA_BLOB);
2040 for (j = 0; j < attr->rgAttr[i].cValue; j++)
2041 size += attr->rgAttr[i].rgValue[j].cbData;
2043 /* align pointer again to be conservative */
2044 size = ALIGN_DWORD_PTR(size);
2048 static DWORD CRYPT_SizeOfKeyIdAsIssuerAndSerial(const CRYPT_DATA_BLOB *keyId)
2050 static char oid_key_rdn[] = szOID_KEYID_RDN;
2053 CERT_RDN rdn = { 1, &attr };
2054 CERT_NAME_INFO name = { 1, &rdn };
2056 attr.pszObjId = oid_key_rdn;
2057 attr.dwValueType = CERT_RDN_OCTET_STRING;
2058 attr.Value.cbData = keyId->cbData;
2059 attr.Value.pbData = keyId->pbData;
2060 if (CryptEncodeObject(X509_ASN_ENCODING, X509_NAME, &name, NULL, &size))
2061 size++; /* Only include size of special zero serial number on success */
2065 static BOOL CRYPT_CopyKeyIdAsIssuerAndSerial(CERT_NAME_BLOB *issuer,
2066 CRYPT_INTEGER_BLOB *serialNumber, const CRYPT_DATA_BLOB *keyId, DWORD encodedLen,
2069 static char oid_key_rdn[] = szOID_KEYID_RDN;
2071 CERT_RDN rdn = { 1, &attr };
2072 CERT_NAME_INFO name = { 1, &rdn };
2075 /* Encode special zero serial number */
2076 serialNumber->cbData = 1;
2077 serialNumber->pbData = *nextData;
2081 issuer->pbData = *nextData;
2082 attr.pszObjId = oid_key_rdn;
2083 attr.dwValueType = CERT_RDN_OCTET_STRING;
2084 attr.Value.cbData = keyId->cbData;
2085 attr.Value.pbData = keyId->pbData;
2086 ret = CryptEncodeObject(X509_ASN_ENCODING, X509_NAME, &name, *nextData,
2090 *nextData += encodedLen;
2091 issuer->cbData = encodedLen;
2096 static BOOL CRYPT_CopySignerInfo(void *pvData, DWORD *pcbData,
2097 const CMSG_CMS_SIGNER_INFO *in)
2099 DWORD size = sizeof(CMSG_SIGNER_INFO), rdnSize = 0;
2102 TRACE("(%p, %d, %p)\n", pvData, pvData ? *pcbData : 0, in);
2104 if (in->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
2106 size += in->SignerId.u.IssuerSerialNumber.Issuer.cbData;
2107 size += in->SignerId.u.IssuerSerialNumber.SerialNumber.cbData;
2111 rdnSize = CRYPT_SizeOfKeyIdAsIssuerAndSerial(&in->SignerId.u.KeyId);
2114 if (in->HashAlgorithm.pszObjId)
2115 size += strlen(in->HashAlgorithm.pszObjId) + 1;
2116 size += in->HashAlgorithm.Parameters.cbData;
2117 if (in->HashEncryptionAlgorithm.pszObjId)
2118 size += strlen(in->HashEncryptionAlgorithm.pszObjId) + 1;
2119 size += in->HashEncryptionAlgorithm.Parameters.cbData;
2120 size += in->EncryptedHash.cbData;
2122 size = ALIGN_DWORD_PTR(size);
2123 size += CRYPT_SizeOfAttributes(&in->AuthAttrs);
2124 size += CRYPT_SizeOfAttributes(&in->UnauthAttrs);
2130 else if (*pcbData < size)
2133 SetLastError(ERROR_MORE_DATA);
2138 LPBYTE nextData = (BYTE *)pvData + sizeof(CMSG_SIGNER_INFO);
2139 CMSG_SIGNER_INFO *out = (CMSG_SIGNER_INFO *)pvData;
2142 out->dwVersion = in->dwVersion;
2143 if (in->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
2145 CRYPT_CopyBlob(&out->Issuer,
2146 &in->SignerId.u.IssuerSerialNumber.Issuer, &nextData);
2147 CRYPT_CopyBlob(&out->SerialNumber,
2148 &in->SignerId.u.IssuerSerialNumber.SerialNumber, &nextData);
2151 ret = CRYPT_CopyKeyIdAsIssuerAndSerial(&out->Issuer, &out->SerialNumber,
2152 &in->SignerId.u.KeyId, rdnSize, &nextData);
2155 CRYPT_CopyAlgorithmId(&out->HashAlgorithm, &in->HashAlgorithm,
2157 CRYPT_CopyAlgorithmId(&out->HashEncryptionAlgorithm,
2158 &in->HashEncryptionAlgorithm, &nextData);
2159 CRYPT_CopyBlob(&out->EncryptedHash, &in->EncryptedHash, &nextData);
2160 nextData = POINTER_ALIGN_DWORD_PTR(nextData);
2161 CRYPT_CopyAttributes(&out->AuthAttrs, &in->AuthAttrs, &nextData);
2162 CRYPT_CopyAttributes(&out->UnauthAttrs, &in->UnauthAttrs, &nextData);
2165 TRACE("returning %d\n", ret);
2169 static BOOL CRYPT_CopyCMSSignerInfo(void *pvData, DWORD *pcbData,
2170 const CMSG_CMS_SIGNER_INFO *in)
2172 DWORD size = sizeof(CMSG_CMS_SIGNER_INFO);
2175 TRACE("(%p, %d, %p)\n", pvData, pvData ? *pcbData : 0, in);
2177 if (in->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
2179 size += in->SignerId.u.IssuerSerialNumber.Issuer.cbData;
2180 size += in->SignerId.u.IssuerSerialNumber.SerialNumber.cbData;
2183 size += in->SignerId.u.KeyId.cbData;
2184 if (in->HashAlgorithm.pszObjId)
2185 size += strlen(in->HashAlgorithm.pszObjId) + 1;
2186 size += in->HashAlgorithm.Parameters.cbData;
2187 if (in->HashEncryptionAlgorithm.pszObjId)
2188 size += strlen(in->HashEncryptionAlgorithm.pszObjId) + 1;
2189 size += in->HashEncryptionAlgorithm.Parameters.cbData;
2190 size += in->EncryptedHash.cbData;
2192 size = ALIGN_DWORD_PTR(size);
2193 size += CRYPT_SizeOfAttributes(&in->AuthAttrs);
2194 size += CRYPT_SizeOfAttributes(&in->UnauthAttrs);
2200 else if (*pcbData < size)
2203 SetLastError(ERROR_MORE_DATA);
2208 LPBYTE nextData = (BYTE *)pvData + sizeof(CMSG_CMS_SIGNER_INFO);
2209 CMSG_CMS_SIGNER_INFO *out = (CMSG_CMS_SIGNER_INFO *)pvData;
2211 out->dwVersion = in->dwVersion;
2212 out->SignerId.dwIdChoice = in->SignerId.dwIdChoice;
2213 if (in->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
2215 CRYPT_CopyBlob(&out->SignerId.u.IssuerSerialNumber.Issuer,
2216 &in->SignerId.u.IssuerSerialNumber.Issuer, &nextData);
2217 CRYPT_CopyBlob(&out->SignerId.u.IssuerSerialNumber.SerialNumber,
2218 &in->SignerId.u.IssuerSerialNumber.SerialNumber, &nextData);
2221 CRYPT_CopyBlob(&out->SignerId.u.KeyId, &in->SignerId.u.KeyId, &nextData);
2222 CRYPT_CopyAlgorithmId(&out->HashAlgorithm, &in->HashAlgorithm,
2224 CRYPT_CopyAlgorithmId(&out->HashEncryptionAlgorithm,
2225 &in->HashEncryptionAlgorithm, &nextData);
2226 CRYPT_CopyBlob(&out->EncryptedHash, &in->EncryptedHash, &nextData);
2227 nextData = POINTER_ALIGN_DWORD_PTR(nextData);
2228 CRYPT_CopyAttributes(&out->AuthAttrs, &in->AuthAttrs, &nextData);
2229 CRYPT_CopyAttributes(&out->UnauthAttrs, &in->UnauthAttrs, &nextData);
2232 TRACE("returning %d\n", ret);
2236 static BOOL CRYPT_CopySignerCertInfo(void *pvData, DWORD *pcbData,
2237 const CMSG_CMS_SIGNER_INFO *in)
2239 DWORD size = sizeof(CERT_INFO), rdnSize = 0;
2242 TRACE("(%p, %d, %p)\n", pvData, pvData ? *pcbData : 0, in);
2244 if (in->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
2246 size += in->SignerId.u.IssuerSerialNumber.Issuer.cbData;
2247 size += in->SignerId.u.IssuerSerialNumber.SerialNumber.cbData;
2251 rdnSize = CRYPT_SizeOfKeyIdAsIssuerAndSerial(&in->SignerId.u.KeyId);
2259 else if (*pcbData < size)
2262 SetLastError(ERROR_MORE_DATA);
2267 LPBYTE nextData = (BYTE *)pvData + sizeof(CERT_INFO);
2268 CERT_INFO *out = (CERT_INFO *)pvData;
2270 memset(out, 0, sizeof(CERT_INFO));
2271 if (in->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
2273 CRYPT_CopyBlob(&out->Issuer,
2274 &in->SignerId.u.IssuerSerialNumber.Issuer, &nextData);
2275 CRYPT_CopyBlob(&out->SerialNumber,
2276 &in->SignerId.u.IssuerSerialNumber.SerialNumber, &nextData);
2280 ret = CRYPT_CopyKeyIdAsIssuerAndSerial(&out->Issuer, &out->SerialNumber,
2281 &in->SignerId.u.KeyId, rdnSize, &nextData);
2283 TRACE("returning %d\n", ret);
2287 static BOOL CDecodeSignedMsg_GetParam(CDecodeMsg *msg, DWORD dwParamType,
2288 DWORD dwIndex, void *pvData, DWORD *pcbData)
2292 switch (dwParamType)
2294 case CMSG_TYPE_PARAM:
2295 ret = CRYPT_CopyParam(pvData, pcbData, &msg->type, sizeof(msg->type));
2297 case CMSG_CONTENT_PARAM:
2298 if (msg->u.signed_data.info)
2300 if (!strcmp(msg->u.signed_data.info->content.pszObjId,
2303 CRYPT_DATA_BLOB *blob;
2306 ret = CryptDecodeObjectEx(X509_ASN_ENCODING, X509_OCTET_STRING,
2307 msg->u.signed_data.info->content.Content.pbData,
2308 msg->u.signed_data.info->content.Content.cbData,
2309 CRYPT_DECODE_ALLOC_FLAG, NULL, (LPBYTE)&blob, &size);
2312 ret = CRYPT_CopyParam(pvData, pcbData, blob->pbData,
2318 ret = CRYPT_CopyParam(pvData, pcbData,
2319 msg->u.signed_data.info->content.Content.pbData,
2320 msg->u.signed_data.info->content.Content.cbData);
2323 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2325 case CMSG_INNER_CONTENT_TYPE_PARAM:
2326 if (msg->u.signed_data.info)
2327 ret = CRYPT_CopyParam(pvData, pcbData,
2328 msg->u.signed_data.info->content.pszObjId,
2329 strlen(msg->u.signed_data.info->content.pszObjId) + 1);
2331 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2333 case CMSG_SIGNER_COUNT_PARAM:
2334 if (msg->u.signed_data.info)
2335 ret = CRYPT_CopyParam(pvData, pcbData,
2336 &msg->u.signed_data.info->cSignerInfo, sizeof(DWORD));
2338 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2340 case CMSG_SIGNER_INFO_PARAM:
2341 if (msg->u.signed_data.info)
2343 if (dwIndex >= msg->u.signed_data.info->cSignerInfo)
2344 SetLastError(CRYPT_E_INVALID_INDEX);
2346 ret = CRYPT_CopySignerInfo(pvData, pcbData,
2347 &msg->u.signed_data.info->rgSignerInfo[dwIndex]);
2350 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2352 case CMSG_SIGNER_CERT_INFO_PARAM:
2353 if (msg->u.signed_data.info)
2355 if (dwIndex >= msg->u.signed_data.info->cSignerInfo)
2356 SetLastError(CRYPT_E_INVALID_INDEX);
2358 ret = CRYPT_CopySignerCertInfo(pvData, pcbData,
2359 &msg->u.signed_data.info->rgSignerInfo[dwIndex]);
2362 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2364 case CMSG_CERT_COUNT_PARAM:
2365 if (msg->u.signed_data.info)
2366 ret = CRYPT_CopyParam(pvData, pcbData,
2367 &msg->u.signed_data.info->cCertEncoded, sizeof(DWORD));
2369 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2371 case CMSG_CERT_PARAM:
2372 if (msg->u.signed_data.info)
2374 if (dwIndex >= msg->u.signed_data.info->cCertEncoded)
2375 SetLastError(CRYPT_E_INVALID_INDEX);
2377 ret = CRYPT_CopyParam(pvData, pcbData,
2378 msg->u.signed_data.info->rgCertEncoded[dwIndex].pbData,
2379 msg->u.signed_data.info->rgCertEncoded[dwIndex].cbData);
2382 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2384 case CMSG_CRL_COUNT_PARAM:
2385 if (msg->u.signed_data.info)
2386 ret = CRYPT_CopyParam(pvData, pcbData,
2387 &msg->u.signed_data.info->cCrlEncoded, sizeof(DWORD));
2389 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2391 case CMSG_CRL_PARAM:
2392 if (msg->u.signed_data.info)
2394 if (dwIndex >= msg->u.signed_data.info->cCrlEncoded)
2395 SetLastError(CRYPT_E_INVALID_INDEX);
2397 ret = CRYPT_CopyParam(pvData, pcbData,
2398 msg->u.signed_data.info->rgCrlEncoded[dwIndex].pbData,
2399 msg->u.signed_data.info->rgCrlEncoded[dwIndex].cbData);
2402 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2404 case CMSG_COMPUTED_HASH_PARAM:
2405 if (msg->u.signed_data.info)
2407 if (dwIndex >= msg->u.signed_data.cSignerHandle)
2408 SetLastError(CRYPT_E_INVALID_INDEX);
2410 ret = CryptGetHashParam(
2411 msg->u.signed_data.signerHandles[dwIndex].contentHash,
2412 HP_HASHVAL, pvData, pcbData, 0);
2415 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2417 case CMSG_ATTR_CERT_COUNT_PARAM:
2418 if (msg->u.signed_data.info)
2420 DWORD attrCertCount = 0;
2422 ret = CRYPT_CopyParam(pvData, pcbData,
2423 &attrCertCount, sizeof(DWORD));
2426 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2428 case CMSG_ATTR_CERT_PARAM:
2429 if (msg->u.signed_data.info)
2430 SetLastError(CRYPT_E_INVALID_INDEX);
2432 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2434 case CMSG_CMS_SIGNER_INFO_PARAM:
2435 if (msg->u.signed_data.info)
2437 if (dwIndex >= msg->u.signed_data.info->cSignerInfo)
2438 SetLastError(CRYPT_E_INVALID_INDEX);
2440 ret = CRYPT_CopyCMSSignerInfo(pvData, pcbData,
2441 &msg->u.signed_data.info->rgSignerInfo[dwIndex]);
2444 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2447 FIXME("unimplemented for %d\n", dwParamType);
2448 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2453 static BOOL CDecodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
2454 DWORD dwIndex, void *pvData, DWORD *pcbData)
2456 CDecodeMsg *msg = (CDecodeMsg *)hCryptMsg;
2462 ret = CDecodeHashMsg_GetParam(msg, dwParamType, dwIndex, pvData,
2466 ret = CDecodeSignedMsg_GetParam(msg, dwParamType, dwIndex, pvData,
2470 switch (dwParamType)
2472 case CMSG_TYPE_PARAM:
2473 ret = CRYPT_CopyParam(pvData, pcbData, &msg->type,
2478 CRYPT_DATA_BLOB blob;
2480 ret = ContextPropertyList_FindProperty(msg->properties, dwParamType,
2483 ret = CRYPT_CopyParam(pvData, pcbData, blob.pbData,
2486 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2493 static BOOL CDecodeHashMsg_VerifyHash(CDecodeMsg *msg)
2496 CRYPT_DATA_BLOB hashBlob;
2498 ret = ContextPropertyList_FindProperty(msg->properties,
2499 CMSG_HASH_DATA_PARAM, &hashBlob);
2502 DWORD computedHashSize = 0;
2504 ret = CDecodeHashMsg_GetParam(msg, CMSG_COMPUTED_HASH_PARAM, 0, NULL,
2506 if (hashBlob.cbData == computedHashSize)
2508 LPBYTE computedHash = CryptMemAlloc(computedHashSize);
2512 ret = CDecodeHashMsg_GetParam(msg, CMSG_COMPUTED_HASH_PARAM, 0,
2513 computedHash, &computedHashSize);
2516 if (memcmp(hashBlob.pbData, computedHash, hashBlob.cbData))
2518 SetLastError(CRYPT_E_HASH_VALUE);
2522 CryptMemFree(computedHash);
2526 SetLastError(ERROR_OUTOFMEMORY);
2532 SetLastError(CRYPT_E_HASH_VALUE);
2539 static BOOL CDecodeSignedMsg_VerifySignatureWithKey(CDecodeMsg *msg,
2540 HCRYPTPROV prov, DWORD signerIndex, PCERT_PUBLIC_KEY_INFO keyInfo)
2546 prov = msg->crypt_prov;
2547 ret = CryptImportPublicKeyInfo(prov, X509_ASN_ENCODING, keyInfo, &key);
2551 CRYPT_HASH_BLOB reversedHash;
2553 if (msg->u.signed_data.info->rgSignerInfo[signerIndex].AuthAttrs.cAttr)
2554 hash = msg->u.signed_data.signerHandles[signerIndex].authAttrHash;
2556 hash = msg->u.signed_data.signerHandles[signerIndex].contentHash;
2557 ret = CRYPT_ConstructBlob(&reversedHash,
2558 &msg->u.signed_data.info->rgSignerInfo[signerIndex].EncryptedHash);
2561 CRYPT_ReverseBytes(&reversedHash);
2562 ret = CryptVerifySignatureW(hash, reversedHash.pbData,
2563 reversedHash.cbData, key, NULL, 0);
2564 CryptMemFree(reversedHash.pbData);
2566 CryptDestroyKey(key);
2571 static BOOL CDecodeSignedMsg_VerifySignature(CDecodeMsg *msg, PCERT_INFO info)
2576 if (!msg->u.signed_data.signerHandles)
2578 SetLastError(NTE_BAD_SIGNATURE);
2581 for (i = 0; !ret && i < msg->u.signed_data.info->cSignerInfo; i++)
2583 PCMSG_CMS_SIGNER_INFO signerInfo =
2584 &msg->u.signed_data.info->rgSignerInfo[i];
2586 if (signerInfo->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
2588 ret = CertCompareCertificateName(X509_ASN_ENCODING,
2589 &signerInfo->SignerId.u.IssuerSerialNumber.Issuer,
2593 ret = CertCompareIntegerBlob(
2594 &signerInfo->SignerId.u.IssuerSerialNumber.SerialNumber,
2595 &info->SerialNumber);
2602 FIXME("signer %d: unimplemented for key id\n", i);
2606 ret = CDecodeSignedMsg_VerifySignatureWithKey(msg, 0, i,
2607 &info->SubjectPublicKeyInfo);
2609 SetLastError(CRYPT_E_SIGNER_NOT_FOUND);
2614 static BOOL CDecodeSignedMsg_VerifySignatureEx(CDecodeMsg *msg,
2615 PCMSG_CTRL_VERIFY_SIGNATURE_EX_PARA para)
2619 if (para->cbSize != sizeof(CMSG_CTRL_VERIFY_SIGNATURE_EX_PARA))
2620 SetLastError(ERROR_INVALID_PARAMETER);
2621 else if (para->dwSignerIndex >= msg->u.signed_data.info->cSignerInfo)
2622 SetLastError(CRYPT_E_SIGNER_NOT_FOUND);
2623 else if (!msg->u.signed_data.signerHandles)
2624 SetLastError(NTE_BAD_SIGNATURE);
2627 switch (para->dwSignerType)
2629 case CMSG_VERIFY_SIGNER_PUBKEY:
2630 ret = CDecodeSignedMsg_VerifySignatureWithKey(msg,
2631 para->hCryptProv, para->dwSignerIndex,
2632 (PCERT_PUBLIC_KEY_INFO)para->pvSigner);
2634 case CMSG_VERIFY_SIGNER_CERT:
2636 PCCERT_CONTEXT cert = (PCCERT_CONTEXT)para->pvSigner;
2638 ret = CDecodeSignedMsg_VerifySignatureWithKey(msg, para->hCryptProv,
2639 para->dwSignerIndex, &cert->pCertInfo->SubjectPublicKeyInfo);
2643 FIXME("unimplemented for signer type %d\n", para->dwSignerType);
2644 SetLastError(CRYPT_E_SIGNER_NOT_FOUND);
2650 static BOOL CDecodeMsg_Control(HCRYPTMSG hCryptMsg, DWORD dwFlags,
2651 DWORD dwCtrlType, const void *pvCtrlPara)
2653 CDecodeMsg *msg = (CDecodeMsg *)hCryptMsg;
2658 case CMSG_CTRL_VERIFY_SIGNATURE:
2662 ret = CDecodeSignedMsg_VerifySignature(msg, (PCERT_INFO)pvCtrlPara);
2665 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2668 case CMSG_CTRL_DECRYPT:
2672 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2675 case CMSG_CTRL_VERIFY_HASH:
2679 ret = CDecodeHashMsg_VerifyHash(msg);
2682 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2685 case CMSG_CTRL_VERIFY_SIGNATURE_EX:
2689 ret = CDecodeSignedMsg_VerifySignatureEx(msg,
2690 (PCMSG_CTRL_VERIFY_SIGNATURE_EX_PARA)pvCtrlPara);
2693 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2697 SetLastError(CRYPT_E_CONTROL_TYPE);
2702 HCRYPTMSG WINAPI CryptMsgOpenToDecode(DWORD dwMsgEncodingType, DWORD dwFlags,
2703 DWORD dwMsgType, HCRYPTPROV_LEGACY hCryptProv, PCERT_INFO pRecipientInfo,
2704 PCMSG_STREAM_INFO pStreamInfo)
2708 TRACE("(%08x, %08x, %08x, %08lx, %p, %p)\n", dwMsgEncodingType,
2709 dwFlags, dwMsgType, hCryptProv, pRecipientInfo, pStreamInfo);
2711 if (GET_CMSG_ENCODING_TYPE(dwMsgEncodingType) != PKCS_7_ASN_ENCODING)
2713 SetLastError(E_INVALIDARG);
2716 msg = CryptMemAlloc(sizeof(CDecodeMsg));
2719 CryptMsgBase_Init((CryptMsgBase *)msg, dwFlags, pStreamInfo,
2720 CDecodeMsg_Close, CDecodeMsg_GetParam, CDecodeMsg_Update,
2721 CDecodeMsg_Control);
2722 msg->type = dwMsgType;
2724 msg->crypt_prov = hCryptProv;
2727 msg->crypt_prov = CRYPT_GetDefaultProvider();
2728 msg->base.open_flags &= ~CMSG_CRYPT_RELEASE_CONTEXT_FLAG;
2730 memset(&msg->u, 0, sizeof(msg->u));
2731 msg->msg_data.cbData = 0;
2732 msg->msg_data.pbData = NULL;
2733 msg->detached_data.cbData = 0;
2734 msg->detached_data.pbData = NULL;
2735 msg->properties = ContextPropertyList_Create();
2740 HCRYPTMSG WINAPI CryptMsgDuplicate(HCRYPTMSG hCryptMsg)
2742 TRACE("(%p)\n", hCryptMsg);
2746 CryptMsgBase *msg = (CryptMsgBase *)hCryptMsg;
2748 InterlockedIncrement(&msg->ref);
2753 BOOL WINAPI CryptMsgClose(HCRYPTMSG hCryptMsg)
2755 TRACE("(%p)\n", hCryptMsg);
2759 CryptMsgBase *msg = (CryptMsgBase *)hCryptMsg;
2761 if (InterlockedDecrement(&msg->ref) == 0)
2763 TRACE("freeing %p\n", msg);
2772 BOOL WINAPI CryptMsgUpdate(HCRYPTMSG hCryptMsg, const BYTE *pbData,
2773 DWORD cbData, BOOL fFinal)
2775 CryptMsgBase *msg = (CryptMsgBase *)hCryptMsg;
2777 TRACE("(%p, %p, %d, %d)\n", hCryptMsg, pbData, cbData, fFinal);
2779 return msg->update(hCryptMsg, pbData, cbData, fFinal);
2782 BOOL WINAPI CryptMsgGetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
2783 DWORD dwIndex, void *pvData, DWORD *pcbData)
2785 CryptMsgBase *msg = (CryptMsgBase *)hCryptMsg;
2787 TRACE("(%p, %d, %d, %p, %p)\n", hCryptMsg, dwParamType, dwIndex,
2789 return msg->get_param(hCryptMsg, dwParamType, dwIndex, pvData, pcbData);
2792 BOOL WINAPI CryptMsgControl(HCRYPTMSG hCryptMsg, DWORD dwFlags,
2793 DWORD dwCtrlType, const void *pvCtrlPara)
2795 CryptMsgBase *msg = (CryptMsgBase *)hCryptMsg;
2797 TRACE("(%p, %08x, %d, %p)\n", hCryptMsg, dwFlags, dwCtrlType,
2799 return msg->control(hCryptMsg, dwFlags, dwCtrlType, pvCtrlPara);
2802 static CERT_INFO *CRYPT_GetSignerCertInfoFromMsg(HCRYPTMSG msg,
2803 DWORD dwSignerIndex)
2805 CERT_INFO *certInfo = NULL;
2808 if (CryptMsgGetParam(msg, CMSG_SIGNER_CERT_INFO_PARAM, dwSignerIndex, NULL,
2811 certInfo = CryptMemAlloc(size);
2814 if (!CryptMsgGetParam(msg, CMSG_SIGNER_CERT_INFO_PARAM,
2815 dwSignerIndex, certInfo, &size))
2817 CryptMemFree(certInfo);
2825 BOOL WINAPI CryptMsgGetAndVerifySigner(HCRYPTMSG hCryptMsg, DWORD cSignerStore,
2826 HCERTSTORE *rghSignerStore, DWORD dwFlags, PCCERT_CONTEXT *ppSigner,
2827 DWORD *pdwSignerIndex)
2830 DWORD i, signerIndex = 0;
2831 PCCERT_CONTEXT signerCert = NULL;
2834 TRACE("(%p, %d, %p, %08x, %p, %p)\n", hCryptMsg, cSignerStore,
2835 rghSignerStore, dwFlags, ppSigner, pdwSignerIndex);
2837 /* Clear output parameters */
2840 if (pdwSignerIndex && !(dwFlags & CMSG_USE_SIGNER_INDEX_FLAG))
2841 *pdwSignerIndex = 0;
2843 /* Create store to search for signer certificates */
2844 store = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
2845 CERT_STORE_CREATE_NEW_FLAG, NULL);
2846 if (!(dwFlags & CMSG_TRUSTED_SIGNER_FLAG))
2848 HCERTSTORE msgStore = CertOpenStore(CERT_STORE_PROV_MSG, 0, 0, 0,
2851 CertAddStoreToCollection(store, msgStore, 0, 0);
2852 CertCloseStore(msgStore, 0);
2854 for (i = 0; i < cSignerStore; i++)
2855 CertAddStoreToCollection(store, rghSignerStore[i], 0, 0);
2857 /* Find signer cert */
2858 if (dwFlags & CMSG_USE_SIGNER_INDEX_FLAG)
2860 CERT_INFO *signer = CRYPT_GetSignerCertInfoFromMsg(hCryptMsg,
2865 signerIndex = *pdwSignerIndex;
2866 signerCert = CertFindCertificateInStore(store, X509_ASN_ENCODING,
2867 0, CERT_FIND_SUBJECT_CERT, signer, NULL);
2868 CryptMemFree(signer);
2873 DWORD count, size = sizeof(count);
2875 if (CryptMsgGetParam(hCryptMsg, CMSG_SIGNER_COUNT_PARAM, 0, &count,
2878 for (i = 0; !signerCert && i < count; i++)
2880 CERT_INFO *signer = CRYPT_GetSignerCertInfoFromMsg(hCryptMsg,
2885 signerCert = CertFindCertificateInStore(store,
2886 X509_ASN_ENCODING, 0, CERT_FIND_SUBJECT_CERT, signer,
2890 CryptMemFree(signer);
2895 SetLastError(CRYPT_E_NO_TRUSTED_SIGNER);
2899 if (!(dwFlags & CMSG_SIGNER_ONLY_FLAG))
2900 ret = CryptMsgControl(hCryptMsg, 0, CMSG_CTRL_VERIFY_SIGNATURE,
2901 signerCert->pCertInfo);
2907 *ppSigner = CertDuplicateCertificateContext(signerCert);
2909 *pdwSignerIndex = signerIndex;
2911 CertFreeCertificateContext(signerCert);
2914 CertCloseStore(store, 0);
2918 BOOL WINAPI CryptMsgVerifyCountersignatureEncodedEx(HCRYPTPROV_LEGACY hCryptProv,
2919 DWORD dwEncodingType, PBYTE pbSignerInfo, DWORD cbSignerInfo,
2920 PBYTE pbSignerInfoCountersignature, DWORD cbSignerInfoCountersignature,
2921 DWORD dwSignerType, void *pvSigner, DWORD dwFlags, void *pvReserved)
2923 FIXME("(%08lx, %08x, %p, %d, %p, %d, %d, %p, %08x, %p): stub\n", hCryptProv,
2924 dwEncodingType, pbSignerInfo, cbSignerInfo, pbSignerInfoCountersignature,
2925 cbSignerInfoCountersignature, dwSignerType, pvSigner, dwFlags, pvReserved);
2929 BOOL WINAPI CryptMsgEncodeAndSignCTL(DWORD dwMsgEncodingType,
2930 PCTL_INFO pCtlInfo, PCMSG_SIGNED_ENCODE_INFO pSignInfo, DWORD dwFlags,
2931 BYTE *pbEncoded, DWORD *pcbEncoded)
2937 TRACE("(%08x, %p, %p, %08x, %p, %p)\n", dwMsgEncodingType, pCtlInfo,
2938 pSignInfo, dwFlags, pbEncoded, pcbEncoded);
2942 FIXME("unimplemented for flags %08x\n", dwFlags);
2945 if ((ret = CryptEncodeObjectEx(dwMsgEncodingType, PKCS_CTL, pCtlInfo,
2946 CRYPT_ENCODE_ALLOC_FLAG, NULL, &pbCtlContent, &cbCtlContent)))
2948 ret = CryptMsgSignCTL(dwMsgEncodingType, pbCtlContent, cbCtlContent,
2949 pSignInfo, dwFlags, pbEncoded, pcbEncoded);
2950 LocalFree(pbCtlContent);
2955 BOOL WINAPI CryptMsgSignCTL(DWORD dwMsgEncodingType, BYTE *pbCtlContent,
2956 DWORD cbCtlContent, PCMSG_SIGNED_ENCODE_INFO pSignInfo, DWORD dwFlags,
2957 BYTE *pbEncoded, DWORD *pcbEncoded)
2959 static char oid_ctl[] = szOID_CTL;
2963 TRACE("(%08x, %p, %d, %p, %08x, %p, %p)\n", dwMsgEncodingType,
2964 pbCtlContent, cbCtlContent, pSignInfo, dwFlags, pbEncoded, pcbEncoded);
2968 FIXME("unimplemented for flags %08x\n", dwFlags);
2971 msg = CryptMsgOpenToEncode(dwMsgEncodingType, 0, CMSG_SIGNED, pSignInfo,
2975 ret = CryptMsgUpdate(msg, pbCtlContent, cbCtlContent, TRUE);
2977 ret = CryptMsgGetParam(msg, CMSG_CONTENT_PARAM, 0, pbEncoded,