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 = 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(const 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 = 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 = 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;
378 typedef struct _CHashEncodeMsg
383 CRYPT_DATA_BLOB data;
386 static void CHashEncodeMsg_Close(HCRYPTMSG hCryptMsg)
388 CHashEncodeMsg *msg = 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 = 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, pvData, pcbData, 0);
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 = 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 = pvMsgEncodeInfo;
561 if (info->cbSize != sizeof(CMSG_HASHED_ENCODE_INFO))
563 SetLastError(E_INVALIDARG);
566 if (!(algID = CertOIDToAlgId(info->HashAlgorithm.pszObjId)))
568 SetLastError(CRYPT_E_UNKNOWN_ALGO);
571 if (info->hCryptProv)
572 prov = info->hCryptProv;
575 prov = CRYPT_GetDefaultProvider();
576 dwFlags &= ~CMSG_CRYPT_RELEASE_CONTEXT_FLAG;
578 msg = CryptMemAlloc(sizeof(CHashEncodeMsg));
581 CryptMsgBase_Init((CryptMsgBase *)msg, dwFlags, pStreamInfo,
582 CHashEncodeMsg_Close, CHashEncodeMsg_GetParam, CHashEncodeMsg_Update,
583 CRYPT_DefaultMsgControl);
585 msg->data.cbData = 0;
586 msg->data.pbData = NULL;
587 if (!CryptCreateHash(prov, algID, 0, 0, &msg->hash))
596 typedef struct _CMSG_SIGNER_ENCODE_INFO_WITH_CMS
599 PCERT_INFO pCertInfo;
600 HCRYPTPROV hCryptProv;
602 CRYPT_ALGORITHM_IDENTIFIER HashAlgorithm;
605 PCRYPT_ATTRIBUTE rgAuthAttr;
607 PCRYPT_ATTRIBUTE rgUnauthAttr;
609 CRYPT_ALGORITHM_IDENTIFIER HashEncryptionAlgorithm;
610 void *pvHashEncryptionAuxInfo;
611 } CMSG_SIGNER_ENCODE_INFO_WITH_CMS, *PCMSG_SIGNER_ENCODE_INFO_WITH_CMS;
613 typedef struct _CMSG_SIGNED_ENCODE_INFO_WITH_CMS
617 PCMSG_SIGNER_ENCODE_INFO_WITH_CMS rgSigners;
619 PCERT_BLOB rgCertEncoded;
621 PCRL_BLOB rgCrlEncoded;
622 DWORD cAttrCertEncoded;
623 PCERT_BLOB rgAttrCertEncoded;
624 } CMSG_SIGNED_ENCODE_INFO_WITH_CMS, *PCMSG_SIGNED_ENCODE_INFO_WITH_CMS;
626 static BOOL CRYPT_IsValidSigner(const CMSG_SIGNER_ENCODE_INFO_WITH_CMS *signer)
628 if (signer->cbSize != sizeof(CMSG_SIGNER_ENCODE_INFO) &&
629 signer->cbSize != sizeof(CMSG_SIGNER_ENCODE_INFO_WITH_CMS))
631 SetLastError(E_INVALIDARG);
634 if (signer->cbSize == sizeof(CMSG_SIGNER_ENCODE_INFO))
636 if (!signer->pCertInfo->SerialNumber.cbData)
638 SetLastError(E_INVALIDARG);
641 if (!signer->pCertInfo->Issuer.cbData)
643 SetLastError(E_INVALIDARG);
647 else if (signer->cbSize == sizeof(CMSG_SIGNER_ENCODE_INFO_WITH_CMS))
649 switch (signer->SignerId.dwIdChoice)
652 if (!signer->pCertInfo->SerialNumber.cbData)
654 SetLastError(E_INVALIDARG);
657 if (!signer->pCertInfo->Issuer.cbData)
659 SetLastError(E_INVALIDARG);
663 case CERT_ID_ISSUER_SERIAL_NUMBER:
664 if (!signer->SignerId.u.IssuerSerialNumber.SerialNumber.cbData)
666 SetLastError(E_INVALIDARG);
669 if (!signer->SignerId.u.IssuerSerialNumber.Issuer.cbData)
671 SetLastError(E_INVALIDARG);
675 case CERT_ID_KEY_IDENTIFIER:
676 if (!signer->SignerId.u.KeyId.cbData)
678 SetLastError(E_INVALIDARG);
683 SetLastError(E_INVALIDARG);
685 if (signer->HashEncryptionAlgorithm.pszObjId)
687 FIXME("CMSG_SIGNER_ENCODE_INFO with CMS fields unsupported\n");
691 if (!signer->hCryptProv)
693 SetLastError(E_INVALIDARG);
696 if (!CertOIDToAlgId(signer->HashAlgorithm.pszObjId))
698 SetLastError(CRYPT_E_UNKNOWN_ALGO);
704 static BOOL CRYPT_ConstructBlob(CRYPT_DATA_BLOB *out, const CRYPT_DATA_BLOB *in)
708 out->cbData = in->cbData;
711 out->pbData = CryptMemAlloc(out->cbData);
713 memcpy(out->pbData, in->pbData, out->cbData);
722 static BOOL CRYPT_ConstructBlobArray(DWORD *outCBlobs,
723 PCRYPT_DATA_BLOB *outPBlobs, DWORD cBlobs, const CRYPT_DATA_BLOB *pBlobs)
730 *outPBlobs = CryptMemAlloc(cBlobs * sizeof(CRYPT_DATA_BLOB));
735 memset(*outPBlobs, 0, cBlobs * sizeof(CRYPT_DATA_BLOB));
736 for (i = 0; ret && i < cBlobs; i++)
737 ret = CRYPT_ConstructBlob(&(*outPBlobs)[i], &pBlobs[i]);
745 static void CRYPT_FreeBlobArray(DWORD cBlobs, PCRYPT_DATA_BLOB blobs)
749 for (i = 0; i < cBlobs; i++)
750 CryptMemFree(blobs[i].pbData);
754 static BOOL CRYPT_ConstructAttribute(CRYPT_ATTRIBUTE *out,
755 const CRYPT_ATTRIBUTE *in)
759 out->pszObjId = CryptMemAlloc(strlen(in->pszObjId) + 1);
762 strcpy(out->pszObjId, in->pszObjId);
763 ret = CRYPT_ConstructBlobArray(&out->cValue, &out->rgValue,
764 in->cValue, in->rgValue);
771 static BOOL CRYPT_ConstructAttributes(CRYPT_ATTRIBUTES *out,
772 const CRYPT_ATTRIBUTES *in)
776 out->cAttr = in->cAttr;
779 out->rgAttr = CryptMemAlloc(out->cAttr * sizeof(CRYPT_ATTRIBUTE));
784 memset(out->rgAttr, 0, out->cAttr * sizeof(CRYPT_ATTRIBUTE));
785 for (i = 0; ret && i < out->cAttr; i++)
786 ret = CRYPT_ConstructAttribute(&out->rgAttr[i], &in->rgAttr[i]);
796 /* Constructs a CMSG_CMS_SIGNER_INFO from a CMSG_SIGNER_ENCODE_INFO_WITH_CMS. */
797 static BOOL CSignerInfo_Construct(CMSG_CMS_SIGNER_INFO *info,
798 const CMSG_SIGNER_ENCODE_INFO_WITH_CMS *in)
802 if (in->cbSize == sizeof(CMSG_SIGNER_ENCODE_INFO))
804 info->dwVersion = CMSG_SIGNER_INFO_V1;
805 ret = CRYPT_ConstructBlob(&info->SignerId.u.IssuerSerialNumber.Issuer,
806 &in->pCertInfo->Issuer);
808 ret = CRYPT_ConstructBlob(
809 &info->SignerId.u.IssuerSerialNumber.SerialNumber,
810 &in->pCertInfo->SerialNumber);
811 info->SignerId.dwIdChoice = CERT_ID_ISSUER_SERIAL_NUMBER;
812 info->HashEncryptionAlgorithm.pszObjId =
813 in->pCertInfo->SubjectPublicKeyInfo.Algorithm.pszObjId;
815 ret = CRYPT_ConstructBlob(&info->HashEncryptionAlgorithm.Parameters,
816 &in->pCertInfo->SubjectPublicKeyInfo.Algorithm.Parameters);
820 const CRYPT_ALGORITHM_IDENTIFIER *pEncrAlg;
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);
855 pEncrAlg = in->HashEncryptionAlgorithm.pszObjId ?
856 &in->HashEncryptionAlgorithm :
857 &in->pCertInfo->SubjectPublicKeyInfo.Algorithm;
858 info->HashEncryptionAlgorithm.pszObjId = pEncrAlg->pszObjId;
860 ret = CRYPT_ConstructBlob(&info->HashEncryptionAlgorithm.Parameters,
861 &pEncrAlg->Parameters);
863 /* Assumption: algorithm IDs will point to static strings, not
864 * stack-based ones, so copying the pointer values is safe.
866 info->HashAlgorithm.pszObjId = in->HashAlgorithm.pszObjId;
868 ret = CRYPT_ConstructBlob(&info->HashAlgorithm.Parameters,
869 &in->HashAlgorithm.Parameters);
871 ret = CRYPT_ConstructAttributes(&info->AuthAttrs,
872 (CRYPT_ATTRIBUTES *)&in->cAuthAttr);
874 ret = CRYPT_ConstructAttributes(&info->UnauthAttrs,
875 (CRYPT_ATTRIBUTES *)&in->cUnauthAttr);
879 static void CSignerInfo_Free(CMSG_CMS_SIGNER_INFO *info)
883 if (info->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
885 CryptMemFree(info->SignerId.u.IssuerSerialNumber.Issuer.pbData);
886 CryptMemFree(info->SignerId.u.IssuerSerialNumber.SerialNumber.pbData);
889 CryptMemFree(info->SignerId.u.KeyId.pbData);
890 CryptMemFree(info->HashAlgorithm.Parameters.pbData);
891 CryptMemFree(info->HashEncryptionAlgorithm.Parameters.pbData);
892 CryptMemFree(info->EncryptedHash.pbData);
893 for (i = 0; i < info->AuthAttrs.cAttr; i++)
895 for (j = 0; j < info->AuthAttrs.rgAttr[i].cValue; j++)
896 CryptMemFree(info->AuthAttrs.rgAttr[i].rgValue[j].pbData);
897 CryptMemFree(info->AuthAttrs.rgAttr[i].rgValue);
898 CryptMemFree(info->AuthAttrs.rgAttr[i].pszObjId);
900 CryptMemFree(info->AuthAttrs.rgAttr);
901 for (i = 0; i < info->UnauthAttrs.cAttr; i++)
903 for (j = 0; j < info->UnauthAttrs.rgAttr[i].cValue; j++)
904 CryptMemFree(info->UnauthAttrs.rgAttr[i].rgValue[j].pbData);
905 CryptMemFree(info->UnauthAttrs.rgAttr[i].rgValue);
906 CryptMemFree(info->UnauthAttrs.rgAttr[i].pszObjId);
908 CryptMemFree(info->UnauthAttrs.rgAttr);
911 typedef struct _CSignerHandles
913 HCRYPTHASH contentHash;
914 HCRYPTHASH authAttrHash;
917 typedef struct _CSignedMsgData
919 CRYPT_SIGNED_INFO *info;
921 CSignerHandles *signerHandles;
924 /* Constructs the signer handles for the signerIndex'th signer of msg_data.
925 * Assumes signerIndex is a valid idnex, and that msg_data's info has already
928 static BOOL CSignedMsgData_ConstructSignerHandles(CSignedMsgData *msg_data,
929 DWORD signerIndex, HCRYPTPROV crypt_prov)
934 algID = CertOIDToAlgId(
935 msg_data->info->rgSignerInfo[signerIndex].HashAlgorithm.pszObjId);
936 ret = CryptCreateHash(crypt_prov, algID, 0, 0,
937 &msg_data->signerHandles->contentHash);
938 if (ret && msg_data->info->rgSignerInfo[signerIndex].AuthAttrs.cAttr > 0)
939 ret = CryptCreateHash(crypt_prov, algID, 0, 0,
940 &msg_data->signerHandles->authAttrHash);
944 /* Allocates a CSignedMsgData's handles. Assumes its info has already been
947 static BOOL CSignedMsgData_AllocateHandles(CSignedMsgData *msg_data)
951 if (msg_data->info->cSignerInfo)
953 msg_data->signerHandles =
954 CryptMemAlloc(msg_data->info->cSignerInfo * sizeof(CSignerHandles));
955 if (msg_data->signerHandles)
957 msg_data->cSignerHandle = msg_data->info->cSignerInfo;
958 memset(msg_data->signerHandles, 0,
959 msg_data->info->cSignerInfo * sizeof(CSignerHandles));
963 msg_data->cSignerHandle = 0;
969 msg_data->cSignerHandle = 0;
970 msg_data->signerHandles = NULL;
975 static void CSignedMsgData_CloseHandles(CSignedMsgData *msg_data)
979 for (i = 0; i < msg_data->cSignerHandle; i++)
981 if (msg_data->signerHandles[i].contentHash)
982 CryptDestroyHash(msg_data->signerHandles[i].contentHash);
983 if (msg_data->signerHandles[i].authAttrHash)
984 CryptDestroyHash(msg_data->signerHandles[i].authAttrHash);
986 CryptMemFree(msg_data->signerHandles);
987 msg_data->signerHandles = NULL;
988 msg_data->cSignerHandle = 0;
991 static BOOL CSignedMsgData_UpdateHash(CSignedMsgData *msg_data,
992 const BYTE *pbData, DWORD cbData)
997 for (i = 0; ret && i < msg_data->cSignerHandle; i++)
998 ret = CryptHashData(msg_data->signerHandles[i].contentHash, pbData,
1003 static BOOL CRYPT_AppendAttribute(CRYPT_ATTRIBUTES *out,
1004 const CRYPT_ATTRIBUTE *in)
1008 out->rgAttr = CryptMemRealloc(out->rgAttr,
1009 (out->cAttr + 1) * sizeof(CRYPT_ATTRIBUTE));
1012 ret = CRYPT_ConstructAttribute(&out->rgAttr[out->cAttr], in);
1019 static BOOL CSignedMsgData_AppendMessageDigestAttribute(
1020 CSignedMsgData *msg_data, DWORD signerIndex)
1024 CRYPT_HASH_BLOB hash = { 0, NULL }, encodedHash = { 0, NULL };
1025 char messageDigest[] = szOID_RSA_messageDigest;
1026 CRYPT_ATTRIBUTE messageDigestAttr = { messageDigest, 1, &encodedHash };
1028 size = sizeof(DWORD);
1029 ret = CryptGetHashParam(
1030 msg_data->signerHandles[signerIndex].contentHash, HP_HASHSIZE,
1031 (LPBYTE)&hash.cbData, &size, 0);
1034 hash.pbData = CryptMemAlloc(hash.cbData);
1035 ret = CryptGetHashParam(
1036 msg_data->signerHandles[signerIndex].contentHash, HP_HASHVAL,
1037 hash.pbData, &hash.cbData, 0);
1040 ret = CRYPT_AsnEncodeOctets(0, NULL, &hash, CRYPT_ENCODE_ALLOC_FLAG,
1041 NULL, (LPBYTE)&encodedHash.pbData, &encodedHash.cbData);
1044 ret = CRYPT_AppendAttribute(
1045 &msg_data->info->rgSignerInfo[signerIndex].AuthAttrs,
1046 &messageDigestAttr);
1047 LocalFree(encodedHash.pbData);
1050 CryptMemFree(hash.pbData);
1060 static BOOL CSignedMsgData_UpdateAuthenticatedAttributes(
1061 CSignedMsgData *msg_data, SignOrVerify flag)
1066 TRACE("(%p)\n", msg_data);
1068 for (i = 0; ret && i < msg_data->info->cSignerInfo; i++)
1070 if (msg_data->info->rgSignerInfo[i].AuthAttrs.cAttr)
1074 BYTE oid_rsa_data_encoded[] = { 0x06,0x09,0x2a,0x86,0x48,0x86,
1075 0xf7,0x0d,0x01,0x07,0x01 };
1076 CRYPT_DATA_BLOB content = { sizeof(oid_rsa_data_encoded),
1077 oid_rsa_data_encoded };
1078 char contentType[] = szOID_RSA_contentType;
1079 CRYPT_ATTRIBUTE contentTypeAttr = { contentType, 1, &content };
1081 /* FIXME: does this depend on inner OID? */
1082 ret = CRYPT_AppendAttribute(
1083 &msg_data->info->rgSignerInfo[i].AuthAttrs, &contentTypeAttr);
1085 ret = CSignedMsgData_AppendMessageDigestAttribute(msg_data,
1090 LPBYTE encodedAttrs;
1093 ret = CryptEncodeObjectEx(X509_ASN_ENCODING, PKCS_ATTRIBUTES,
1094 &msg_data->info->rgSignerInfo[i].AuthAttrs,
1095 CRYPT_ENCODE_ALLOC_FLAG, NULL, &encodedAttrs, &size);
1098 ret = CryptHashData(
1099 msg_data->signerHandles[i].authAttrHash, encodedAttrs,
1101 LocalFree(encodedAttrs);
1106 TRACE("returning %d\n", ret);
1110 static void CRYPT_ReverseBytes(CRYPT_HASH_BLOB *hash)
1115 for (i = 0; i < hash->cbData / 2; i++)
1117 tmp = hash->pbData[hash->cbData - i - 1];
1118 hash->pbData[hash->cbData - i - 1] = hash->pbData[i];
1119 hash->pbData[i] = tmp;
1123 static BOOL CSignedMsgData_Sign(CSignedMsgData *msg_data)
1128 TRACE("(%p)\n", msg_data);
1130 for (i = 0; ret && i < msg_data->info->cSignerInfo; i++)
1134 if (msg_data->info->rgSignerInfo[i].AuthAttrs.cAttr)
1135 hash = msg_data->signerHandles[i].authAttrHash;
1137 hash = msg_data->signerHandles[i].contentHash;
1138 ret = CryptSignHashW(hash, AT_SIGNATURE, NULL, 0, NULL,
1139 &msg_data->info->rgSignerInfo[i].EncryptedHash.cbData);
1142 msg_data->info->rgSignerInfo[i].EncryptedHash.pbData =
1144 msg_data->info->rgSignerInfo[i].EncryptedHash.cbData);
1145 if (msg_data->info->rgSignerInfo[i].EncryptedHash.pbData)
1147 ret = CryptSignHashW(hash, AT_SIGNATURE, NULL, 0,
1148 msg_data->info->rgSignerInfo[i].EncryptedHash.pbData,
1149 &msg_data->info->rgSignerInfo[i].EncryptedHash.cbData);
1152 &msg_data->info->rgSignerInfo[i].EncryptedHash);
1161 static BOOL CSignedMsgData_Update(CSignedMsgData *msg_data,
1162 const BYTE *pbData, DWORD cbData, BOOL fFinal, SignOrVerify flag)
1164 BOOL ret = CSignedMsgData_UpdateHash(msg_data, pbData, cbData);
1168 ret = CSignedMsgData_UpdateAuthenticatedAttributes(msg_data, flag);
1169 if (ret && flag == Sign)
1170 ret = CSignedMsgData_Sign(msg_data);
1175 typedef struct _CSignedEncodeMsg
1179 CRYPT_DATA_BLOB data;
1180 CSignedMsgData msg_data;
1183 static void CSignedEncodeMsg_Close(HCRYPTMSG hCryptMsg)
1185 CSignedEncodeMsg *msg = hCryptMsg;
1188 CryptMemFree(msg->innerOID);
1189 CryptMemFree(msg->data.pbData);
1190 CRYPT_FreeBlobArray(msg->msg_data.info->cCertEncoded,
1191 msg->msg_data.info->rgCertEncoded);
1192 CRYPT_FreeBlobArray(msg->msg_data.info->cCrlEncoded,
1193 msg->msg_data.info->rgCrlEncoded);
1194 for (i = 0; i < msg->msg_data.info->cSignerInfo; i++)
1195 CSignerInfo_Free(&msg->msg_data.info->rgSignerInfo[i]);
1196 CSignedMsgData_CloseHandles(&msg->msg_data);
1197 CryptMemFree(msg->msg_data.info->rgSignerInfo);
1198 CryptMemFree(msg->msg_data.info);
1201 static BOOL CSignedEncodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
1202 DWORD dwIndex, void *pvData, DWORD *pcbData)
1204 CSignedEncodeMsg *msg = hCryptMsg;
1207 switch (dwParamType)
1209 case CMSG_CONTENT_PARAM:
1211 CRYPT_CONTENT_INFO info;
1213 ret = CryptMsgGetParam(hCryptMsg, CMSG_BARE_CONTENT_PARAM, 0, NULL,
1214 &info.Content.cbData);
1217 info.Content.pbData = CryptMemAlloc(info.Content.cbData);
1218 if (info.Content.pbData)
1220 ret = CryptMsgGetParam(hCryptMsg, CMSG_BARE_CONTENT_PARAM, 0,
1221 info.Content.pbData, &info.Content.cbData);
1224 char oid_rsa_signed[] = szOID_RSA_signedData;
1226 info.pszObjId = oid_rsa_signed;
1227 ret = CryptEncodeObjectEx(X509_ASN_ENCODING,
1228 PKCS_CONTENT_INFO, &info, 0, NULL, pvData, pcbData);
1230 CryptMemFree(info.Content.pbData);
1237 case CMSG_BARE_CONTENT_PARAM:
1239 CRYPT_SIGNED_INFO info;
1240 BOOL freeContent = FALSE;
1242 info = *msg->msg_data.info;
1243 if (!msg->innerOID || !strcmp(msg->innerOID, szOID_RSA_data))
1245 char oid_rsa_data[] = szOID_RSA_data;
1247 /* Quirk: OID is only encoded messages if an update has happened */
1248 if (msg->base.state != MsgStateInit)
1249 info.content.pszObjId = oid_rsa_data;
1251 info.content.pszObjId = NULL;
1252 if (msg->data.cbData)
1254 CRYPT_DATA_BLOB blob = { msg->data.cbData, msg->data.pbData };
1256 ret = CryptEncodeObjectEx(X509_ASN_ENCODING, X509_OCTET_STRING,
1257 &blob, CRYPT_ENCODE_ALLOC_FLAG, NULL,
1258 &info.content.Content.pbData, &info.content.Content.cbData);
1263 info.content.Content.cbData = 0;
1264 info.content.Content.pbData = NULL;
1270 info.content.pszObjId = msg->innerOID;
1271 info.content.Content.cbData = msg->data.cbData;
1272 info.content.Content.pbData = msg->data.pbData;
1277 ret = CRYPT_AsnEncodeCMSSignedInfo(&info, pvData, pcbData);
1279 LocalFree(info.content.Content.pbData);
1283 case CMSG_COMPUTED_HASH_PARAM:
1284 if (dwIndex >= msg->msg_data.cSignerHandle)
1285 SetLastError(CRYPT_E_INVALID_INDEX);
1287 ret = CryptGetHashParam(
1288 msg->msg_data.signerHandles[dwIndex].contentHash, HP_HASHVAL,
1289 pvData, pcbData, 0);
1291 case CMSG_ENCODED_SIGNER:
1292 if (dwIndex >= msg->msg_data.info->cSignerInfo)
1293 SetLastError(CRYPT_E_INVALID_INDEX);
1295 ret = CryptEncodeObjectEx(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
1296 CMS_SIGNER_INFO, &msg->msg_data.info->rgSignerInfo[dwIndex], 0,
1297 NULL, pvData, pcbData);
1299 case CMSG_VERSION_PARAM:
1300 ret = CRYPT_CopyParam(pvData, pcbData, &msg->msg_data.info->version,
1301 sizeof(msg->msg_data.info->version));
1304 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1309 static BOOL CSignedEncodeMsg_Update(HCRYPTMSG hCryptMsg, const BYTE *pbData,
1310 DWORD cbData, BOOL fFinal)
1312 CSignedEncodeMsg *msg = hCryptMsg;
1315 if (msg->base.state == MsgStateFinalized)
1316 SetLastError(CRYPT_E_MSG_ERROR);
1317 else if (msg->base.streamed || (msg->base.open_flags & CMSG_DETACHED_FLAG))
1319 ret = CSignedMsgData_Update(&msg->msg_data, pbData, cbData, fFinal,
1321 if (msg->base.streamed)
1322 FIXME("streamed partial stub\n");
1323 msg->base.state = fFinal ? MsgStateFinalized : MsgStateUpdated;
1328 SetLastError(CRYPT_E_MSG_ERROR);
1333 msg->data.pbData = CryptMemAlloc(cbData);
1334 if (msg->data.pbData)
1336 memcpy(msg->data.pbData, pbData, cbData);
1337 msg->data.cbData = cbData;
1344 ret = CSignedMsgData_Update(&msg->msg_data, pbData, cbData,
1346 msg->base.state = MsgStateFinalized;
1352 static HCRYPTMSG CSignedEncodeMsg_Open(DWORD dwFlags,
1353 const void *pvMsgEncodeInfo, LPCSTR pszInnerContentObjID,
1354 PCMSG_STREAM_INFO pStreamInfo)
1356 const CMSG_SIGNED_ENCODE_INFO_WITH_CMS *info = pvMsgEncodeInfo;
1358 CSignedEncodeMsg *msg;
1360 if (info->cbSize != sizeof(CMSG_SIGNED_ENCODE_INFO) &&
1361 info->cbSize != sizeof(CMSG_SIGNED_ENCODE_INFO_WITH_CMS))
1363 SetLastError(E_INVALIDARG);
1366 if (info->cbSize == sizeof(CMSG_SIGNED_ENCODE_INFO_WITH_CMS) &&
1367 info->cAttrCertEncoded)
1369 FIXME("CMSG_SIGNED_ENCODE_INFO with CMS fields unsupported\n");
1372 for (i = 0; i < info->cSigners; i++)
1373 if (!CRYPT_IsValidSigner(&info->rgSigners[i]))
1375 msg = CryptMemAlloc(sizeof(CSignedEncodeMsg));
1380 CryptMsgBase_Init((CryptMsgBase *)msg, dwFlags, pStreamInfo,
1381 CSignedEncodeMsg_Close, CSignedEncodeMsg_GetParam,
1382 CSignedEncodeMsg_Update, CRYPT_DefaultMsgControl);
1383 if (pszInnerContentObjID)
1385 msg->innerOID = CryptMemAlloc(strlen(pszInnerContentObjID) + 1);
1387 strcpy(msg->innerOID, pszInnerContentObjID);
1392 msg->innerOID = NULL;
1393 msg->data.cbData = 0;
1394 msg->data.pbData = NULL;
1396 msg->msg_data.info = CryptMemAlloc(sizeof(CRYPT_SIGNED_INFO));
1398 msg->msg_data.info = NULL;
1399 if (msg->msg_data.info)
1401 memset(msg->msg_data.info, 0, sizeof(CRYPT_SIGNED_INFO));
1402 msg->msg_data.info->version = CMSG_SIGNED_DATA_V1;
1410 msg->msg_data.info->rgSignerInfo =
1411 CryptMemAlloc(info->cSigners * sizeof(CMSG_CMS_SIGNER_INFO));
1412 if (msg->msg_data.info->rgSignerInfo)
1414 msg->msg_data.info->cSignerInfo = info->cSigners;
1415 memset(msg->msg_data.info->rgSignerInfo, 0,
1416 msg->msg_data.info->cSignerInfo *
1417 sizeof(CMSG_CMS_SIGNER_INFO));
1418 ret = CSignedMsgData_AllocateHandles(&msg->msg_data);
1419 for (i = 0; ret && i < msg->msg_data.info->cSignerInfo; i++)
1421 if (info->rgSigners[i].SignerId.dwIdChoice ==
1422 CERT_ID_KEY_IDENTIFIER)
1423 msg->msg_data.info->version = CMSG_SIGNED_DATA_V3;
1424 ret = CSignerInfo_Construct(
1425 &msg->msg_data.info->rgSignerInfo[i],
1426 &info->rgSigners[i]);
1429 ret = CSignedMsgData_ConstructSignerHandles(
1430 &msg->msg_data, i, info->rgSigners[i].hCryptProv);
1431 if (dwFlags & CMSG_CRYPT_RELEASE_CONTEXT_FLAG)
1432 CryptReleaseContext(info->rgSigners[i].hCryptProv,
1442 msg->msg_data.info->cSignerInfo = 0;
1443 msg->msg_data.signerHandles = NULL;
1444 msg->msg_data.cSignerHandle = 0;
1448 ret = CRYPT_ConstructBlobArray(&msg->msg_data.info->cCertEncoded,
1449 &msg->msg_data.info->rgCertEncoded, info->cCertEncoded,
1450 info->rgCertEncoded);
1452 ret = CRYPT_ConstructBlobArray(&msg->msg_data.info->cCrlEncoded,
1453 &msg->msg_data.info->rgCrlEncoded, info->cCrlEncoded,
1454 info->rgCrlEncoded);
1457 CSignedEncodeMsg_Close(msg);
1464 typedef struct _CMSG_ENVELOPED_ENCODE_INFO_WITH_CMS
1467 HCRYPTPROV_LEGACY hCryptProv;
1468 CRYPT_ALGORITHM_IDENTIFIER ContentEncryptionAlgorithm;
1469 void *pvEncryptionAuxInfo;
1471 PCERT_INFO *rgpRecipientCert;
1472 PCMSG_RECIPIENT_ENCODE_INFO rgCmsRecipients;
1474 PCERT_BLOB rgCertEncoded;
1476 PCRL_BLOB rgCrlEncoded;
1477 DWORD cAttrCertEncoded;
1478 PCERT_BLOB rgAttrCertEncoded;
1479 DWORD cUnprotectedAttr;
1480 PCRYPT_ATTRIBUTE rgUnprotectedAttr;
1481 } CMSG_ENVELOPED_ENCODE_INFO_WITH_CMS, *PCMSG_ENVELOPED_ENCODE_INFO_WITH_CMS;
1483 typedef struct _CEnvelopedEncodeMsg
1487 } CEnvelopedEncodeMsg;
1489 static void CEnvelopedEncodeMsg_Close(HCRYPTMSG hCryptMsg)
1491 CEnvelopedEncodeMsg *msg = hCryptMsg;
1493 if (msg->base.open_flags & CMSG_CRYPT_RELEASE_CONTEXT_FLAG)
1494 CryptReleaseContext(msg->prov, 0);
1497 static BOOL CEnvelopedEncodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
1498 DWORD dwIndex, void *pvData, DWORD *pcbData)
1500 FIXME("(%p, %d, %d, %p, %p): stub\n", hCryptMsg, dwParamType, dwIndex,
1505 static BOOL CEnvelopedEncodeMsg_Update(HCRYPTMSG hCryptMsg, const BYTE *pbData,
1506 DWORD cbData, BOOL fFinal)
1508 FIXME("(%p, %p, %d, %d): stub\n", hCryptMsg, pbData, cbData, fFinal);
1512 static HCRYPTMSG CEnvelopedEncodeMsg_Open(DWORD dwFlags,
1513 const void *pvMsgEncodeInfo, LPCSTR pszInnerContentObjID,
1514 PCMSG_STREAM_INFO pStreamInfo)
1516 CEnvelopedEncodeMsg *msg;
1517 const CMSG_ENVELOPED_ENCODE_INFO_WITH_CMS *info = pvMsgEncodeInfo;
1521 if (info->cbSize != sizeof(CMSG_ENVELOPED_ENCODE_INFO) &&
1522 info->cbSize != sizeof(CMSG_ENVELOPED_ENCODE_INFO_WITH_CMS))
1524 SetLastError(E_INVALIDARG);
1527 if (info->cbSize == sizeof(CMSG_ENVELOPED_ENCODE_INFO_WITH_CMS))
1528 FIXME("CMS fields unsupported\n");
1529 if (!(algID = CertOIDToAlgId(info->ContentEncryptionAlgorithm.pszObjId)))
1531 SetLastError(CRYPT_E_UNKNOWN_ALGO);
1534 if (info->cRecipients && !info->rgpRecipientCert)
1536 SetLastError(E_INVALIDARG);
1539 if (info->hCryptProv)
1540 prov = info->hCryptProv;
1543 prov = CRYPT_GetDefaultProvider();
1544 dwFlags &= ~CMSG_CRYPT_RELEASE_CONTEXT_FLAG;
1546 msg = CryptMemAlloc(sizeof(CEnvelopedEncodeMsg));
1549 CryptMsgBase_Init((CryptMsgBase *)msg, dwFlags, pStreamInfo,
1550 CEnvelopedEncodeMsg_Close, CEnvelopedEncodeMsg_GetParam,
1551 CEnvelopedEncodeMsg_Update, CRYPT_DefaultMsgControl);
1554 if (!msg && (dwFlags & CMSG_CRYPT_RELEASE_CONTEXT_FLAG))
1555 CryptReleaseContext(prov, 0);
1559 HCRYPTMSG WINAPI CryptMsgOpenToEncode(DWORD dwMsgEncodingType, DWORD dwFlags,
1560 DWORD dwMsgType, const void *pvMsgEncodeInfo, LPSTR pszInnerContentObjID,
1561 PCMSG_STREAM_INFO pStreamInfo)
1563 HCRYPTMSG msg = NULL;
1565 TRACE("(%08x, %08x, %08x, %p, %s, %p)\n", dwMsgEncodingType, dwFlags,
1566 dwMsgType, pvMsgEncodeInfo, debugstr_a(pszInnerContentObjID), pStreamInfo);
1568 if (GET_CMSG_ENCODING_TYPE(dwMsgEncodingType) != PKCS_7_ASN_ENCODING)
1570 SetLastError(E_INVALIDARG);
1576 msg = CDataEncodeMsg_Open(dwFlags, pvMsgEncodeInfo,
1577 pszInnerContentObjID, pStreamInfo);
1580 msg = CHashEncodeMsg_Open(dwFlags, pvMsgEncodeInfo,
1581 pszInnerContentObjID, pStreamInfo);
1584 msg = CSignedEncodeMsg_Open(dwFlags, pvMsgEncodeInfo,
1585 pszInnerContentObjID, pStreamInfo);
1587 case CMSG_ENVELOPED:
1588 msg = CEnvelopedEncodeMsg_Open(dwFlags, pvMsgEncodeInfo,
1589 pszInnerContentObjID, pStreamInfo);
1591 case CMSG_SIGNED_AND_ENVELOPED:
1592 case CMSG_ENCRYPTED:
1593 /* defined but invalid, fall through */
1595 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1600 typedef struct _CDecodeMsg
1604 HCRYPTPROV crypt_prov;
1607 CSignedMsgData signed_data;
1609 CRYPT_DATA_BLOB msg_data;
1610 CRYPT_DATA_BLOB detached_data;
1611 PCONTEXT_PROPERTY_LIST properties;
1614 static void CDecodeMsg_Close(HCRYPTMSG hCryptMsg)
1616 CDecodeMsg *msg = hCryptMsg;
1618 if (msg->base.open_flags & CMSG_CRYPT_RELEASE_CONTEXT_FLAG)
1619 CryptReleaseContext(msg->crypt_prov, 0);
1624 CryptDestroyHash(msg->u.hash);
1627 if (msg->u.signed_data.info)
1629 LocalFree(msg->u.signed_data.info);
1630 CSignedMsgData_CloseHandles(&msg->u.signed_data);
1634 CryptMemFree(msg->msg_data.pbData);
1635 CryptMemFree(msg->detached_data.pbData);
1636 ContextPropertyList_Free(msg->properties);
1639 static BOOL CDecodeMsg_CopyData(CRYPT_DATA_BLOB *blob, const BYTE *pbData,
1647 blob->pbData = CryptMemRealloc(blob->pbData,
1648 blob->cbData + cbData);
1650 blob->pbData = CryptMemAlloc(cbData);
1653 memcpy(blob->pbData + blob->cbData, pbData, cbData);
1654 blob->cbData += cbData;
1662 static BOOL CDecodeMsg_DecodeDataContent(CDecodeMsg *msg, const CRYPT_DER_BLOB *blob)
1665 CRYPT_DATA_BLOB *data;
1668 ret = CryptDecodeObjectEx(X509_ASN_ENCODING, X509_OCTET_STRING,
1669 blob->pbData, blob->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, &data, &size);
1672 ret = ContextPropertyList_SetProperty(msg->properties,
1673 CMSG_CONTENT_PARAM, data->pbData, data->cbData);
1679 static void CDecodeMsg_SaveAlgorithmID(CDecodeMsg *msg, DWORD param,
1680 const CRYPT_ALGORITHM_IDENTIFIER *id)
1682 static const BYTE nullParams[] = { ASN_NULL, 0 };
1683 CRYPT_ALGORITHM_IDENTIFIER *copy;
1684 DWORD len = sizeof(CRYPT_ALGORITHM_IDENTIFIER);
1686 /* Linearize algorithm id */
1687 len += strlen(id->pszObjId) + 1;
1688 len += id->Parameters.cbData;
1689 copy = CryptMemAlloc(len);
1693 (LPSTR)((BYTE *)copy + sizeof(CRYPT_ALGORITHM_IDENTIFIER));
1694 strcpy(copy->pszObjId, id->pszObjId);
1695 copy->Parameters.pbData = (BYTE *)copy->pszObjId + strlen(id->pszObjId)
1697 /* Trick: omit NULL parameters */
1698 if (id->Parameters.cbData == sizeof(nullParams) &&
1699 !memcmp(id->Parameters.pbData, nullParams, sizeof(nullParams)))
1701 copy->Parameters.cbData = 0;
1702 len -= sizeof(nullParams);
1705 copy->Parameters.cbData = id->Parameters.cbData;
1706 if (copy->Parameters.cbData)
1707 memcpy(copy->Parameters.pbData, id->Parameters.pbData,
1708 id->Parameters.cbData);
1709 ContextPropertyList_SetProperty(msg->properties, param, (BYTE *)copy,
1715 static inline void CRYPT_FixUpAlgorithmID(CRYPT_ALGORITHM_IDENTIFIER *id)
1717 id->pszObjId = (LPSTR)((BYTE *)id + sizeof(CRYPT_ALGORITHM_IDENTIFIER));
1718 id->Parameters.pbData = (BYTE *)id->pszObjId + strlen(id->pszObjId) + 1;
1721 static BOOL CDecodeMsg_DecodeHashedContent(CDecodeMsg *msg,
1722 const CRYPT_DER_BLOB *blob)
1725 CRYPT_DIGESTED_DATA *digestedData;
1728 ret = CRYPT_AsnDecodePKCSDigestedData(blob->pbData, blob->cbData,
1729 CRYPT_DECODE_ALLOC_FLAG, NULL, (CRYPT_DIGESTED_DATA *)&digestedData,
1733 ContextPropertyList_SetProperty(msg->properties, CMSG_VERSION_PARAM,
1734 (const BYTE *)&digestedData->version, sizeof(digestedData->version));
1735 CDecodeMsg_SaveAlgorithmID(msg, CMSG_HASH_ALGORITHM_PARAM,
1736 &digestedData->DigestAlgorithm);
1737 ContextPropertyList_SetProperty(msg->properties,
1738 CMSG_INNER_CONTENT_TYPE_PARAM,
1739 (const BYTE *)digestedData->ContentInfo.pszObjId,
1740 digestedData->ContentInfo.pszObjId ?
1741 strlen(digestedData->ContentInfo.pszObjId) + 1 : 0);
1742 if (!(msg->base.open_flags & CMSG_DETACHED_FLAG))
1744 if (digestedData->ContentInfo.Content.cbData)
1745 CDecodeMsg_DecodeDataContent(msg,
1746 &digestedData->ContentInfo.Content);
1748 ContextPropertyList_SetProperty(msg->properties,
1749 CMSG_CONTENT_PARAM, NULL, 0);
1751 ContextPropertyList_SetProperty(msg->properties, CMSG_HASH_DATA_PARAM,
1752 digestedData->hash.pbData, digestedData->hash.cbData);
1753 LocalFree(digestedData);
1758 static BOOL CDecodeMsg_DecodeSignedContent(CDecodeMsg *msg,
1759 const CRYPT_DER_BLOB *blob)
1762 CRYPT_SIGNED_INFO *signedInfo;
1765 ret = CRYPT_AsnDecodeCMSSignedInfo(blob->pbData, blob->cbData,
1766 CRYPT_DECODE_ALLOC_FLAG, NULL, (CRYPT_SIGNED_INFO *)&signedInfo,
1769 msg->u.signed_data.info = signedInfo;
1773 /* Decodes the content in blob as the type given, and updates the value
1774 * (type, parameters, etc.) of msg based on what blob contains.
1775 * It doesn't just use msg's type, to allow a recursive call from an implicitly
1776 * typed message once the outer content info has been decoded.
1778 static BOOL CDecodeMsg_DecodeContent(CDecodeMsg *msg, const CRYPT_DER_BLOB *blob,
1786 if ((ret = CDecodeMsg_DecodeDataContent(msg, blob)))
1787 msg->type = CMSG_DATA;
1790 if ((ret = CDecodeMsg_DecodeHashedContent(msg, blob)))
1791 msg->type = CMSG_HASHED;
1793 case CMSG_ENVELOPED:
1794 FIXME("unimplemented for type CMSG_ENVELOPED\n");
1798 if ((ret = CDecodeMsg_DecodeSignedContent(msg, blob)))
1799 msg->type = CMSG_SIGNED;
1803 CRYPT_CONTENT_INFO *info;
1806 ret = CryptDecodeObjectEx(X509_ASN_ENCODING, PKCS_CONTENT_INFO,
1807 msg->msg_data.pbData, msg->msg_data.cbData, CRYPT_DECODE_ALLOC_FLAG,
1808 NULL, &info, &size);
1811 if (!strcmp(info->pszObjId, szOID_RSA_data))
1812 ret = CDecodeMsg_DecodeContent(msg, &info->Content, CMSG_DATA);
1813 else if (!strcmp(info->pszObjId, szOID_RSA_digestedData))
1814 ret = CDecodeMsg_DecodeContent(msg, &info->Content,
1816 else if (!strcmp(info->pszObjId, szOID_RSA_envelopedData))
1817 ret = CDecodeMsg_DecodeContent(msg, &info->Content,
1819 else if (!strcmp(info->pszObjId, szOID_RSA_signedData))
1820 ret = CDecodeMsg_DecodeContent(msg, &info->Content,
1824 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1834 static BOOL CDecodeMsg_FinalizeHashedContent(CDecodeMsg *msg,
1835 CRYPT_DER_BLOB *blob)
1837 CRYPT_ALGORITHM_IDENTIFIER *hashAlgoID = NULL;
1842 CryptMsgGetParam(msg, CMSG_HASH_ALGORITHM_PARAM, 0, NULL, &size);
1843 hashAlgoID = CryptMemAlloc(size);
1844 ret = CryptMsgGetParam(msg, CMSG_HASH_ALGORITHM_PARAM, 0, hashAlgoID,
1847 algID = CertOIDToAlgId(hashAlgoID->pszObjId);
1848 ret = CryptCreateHash(msg->crypt_prov, algID, 0, 0, &msg->u.hash);
1851 CRYPT_DATA_BLOB content;
1853 if (msg->base.open_flags & CMSG_DETACHED_FLAG)
1855 /* Unlike for non-detached messages, the data were never stored as
1856 * the content param, but were saved in msg->detached_data instead.
1858 content.pbData = msg->detached_data.pbData;
1859 content.cbData = msg->detached_data.cbData;
1862 ret = ContextPropertyList_FindProperty(msg->properties,
1863 CMSG_CONTENT_PARAM, &content);
1865 ret = CryptHashData(msg->u.hash, content.pbData, content.cbData, 0);
1867 CryptMemFree(hashAlgoID);
1871 static BOOL CDecodeMsg_FinalizeSignedContent(CDecodeMsg *msg,
1872 CRYPT_DER_BLOB *blob)
1877 ret = CSignedMsgData_AllocateHandles(&msg->u.signed_data);
1878 for (i = 0; ret && i < msg->u.signed_data.info->cSignerInfo; i++)
1879 ret = CSignedMsgData_ConstructSignerHandles(&msg->u.signed_data, i,
1883 CRYPT_DATA_BLOB *content;
1885 /* Now that we have all the content, update the hash handles with
1886 * it. If the message is a detached message, the content is stored
1887 * in msg->detached_data rather than in the signed message's
1890 if (msg->base.open_flags & CMSG_DETACHED_FLAG)
1891 content = &msg->detached_data;
1893 content = &msg->u.signed_data.info->content.Content;
1894 if (content->cbData)
1896 /* If the message is not detached, have to decode the message's
1897 * content if the type is szOID_RSA_data.
1899 if (!(msg->base.open_flags & CMSG_DETACHED_FLAG) &&
1900 !strcmp(msg->u.signed_data.info->content.pszObjId,
1903 CRYPT_DATA_BLOB *blob;
1905 ret = CryptDecodeObjectEx(X509_ASN_ENCODING,
1906 X509_OCTET_STRING, content->pbData, content->cbData,
1907 CRYPT_DECODE_ALLOC_FLAG, NULL, &blob, &size);
1910 ret = CSignedMsgData_Update(&msg->u.signed_data,
1911 blob->pbData, blob->cbData, TRUE, Verify);
1916 ret = CSignedMsgData_Update(&msg->u.signed_data,
1917 content->pbData, content->cbData, TRUE, Verify);
1923 static BOOL CDecodeMsg_FinalizeContent(CDecodeMsg *msg, CRYPT_DER_BLOB *blob)
1930 ret = CDecodeMsg_FinalizeHashedContent(msg, blob);
1933 ret = CDecodeMsg_FinalizeSignedContent(msg, blob);
1941 static BOOL CDecodeMsg_Update(HCRYPTMSG hCryptMsg, const BYTE *pbData,
1942 DWORD cbData, BOOL fFinal)
1944 CDecodeMsg *msg = hCryptMsg;
1947 TRACE("(%p, %p, %d, %d)\n", hCryptMsg, pbData, cbData, fFinal);
1949 if (msg->base.state == MsgStateFinalized)
1950 SetLastError(CRYPT_E_MSG_ERROR);
1951 else if (msg->base.streamed)
1953 FIXME("(%p, %p, %d, %d): streamed update stub\n", hCryptMsg, pbData,
1955 switch (msg->base.state)
1958 ret = CDecodeMsg_CopyData(&msg->msg_data, pbData, cbData);
1961 if (msg->base.open_flags & CMSG_DETACHED_FLAG)
1962 msg->base.state = MsgStateDataFinalized;
1964 msg->base.state = MsgStateFinalized;
1967 msg->base.state = MsgStateUpdated;
1969 case MsgStateUpdated:
1970 ret = CDecodeMsg_CopyData(&msg->msg_data, pbData, cbData);
1973 if (msg->base.open_flags & CMSG_DETACHED_FLAG)
1974 msg->base.state = MsgStateDataFinalized;
1976 msg->base.state = MsgStateFinalized;
1979 case MsgStateDataFinalized:
1980 ret = CDecodeMsg_CopyData(&msg->detached_data, pbData, cbData);
1982 msg->base.state = MsgStateFinalized;
1985 SetLastError(CRYPT_E_MSG_ERROR);
1992 SetLastError(CRYPT_E_MSG_ERROR);
1995 switch (msg->base.state)
1998 ret = CDecodeMsg_CopyData(&msg->msg_data, pbData, cbData);
1999 if (msg->base.open_flags & CMSG_DETACHED_FLAG)
2000 msg->base.state = MsgStateDataFinalized;
2002 msg->base.state = MsgStateFinalized;
2004 case MsgStateDataFinalized:
2005 ret = CDecodeMsg_CopyData(&msg->detached_data, pbData, cbData);
2006 msg->base.state = MsgStateFinalized;
2009 SetLastError(CRYPT_E_MSG_ERROR);
2013 if (ret && fFinal &&
2014 ((msg->base.open_flags & CMSG_DETACHED_FLAG && msg->base.state ==
2015 MsgStateDataFinalized) ||
2016 (!(msg->base.open_flags & CMSG_DETACHED_FLAG) && msg->base.state ==
2017 MsgStateFinalized)))
2018 ret = CDecodeMsg_DecodeContent(msg, &msg->msg_data, msg->type);
2019 if (ret && msg->base.state == MsgStateFinalized)
2020 ret = CDecodeMsg_FinalizeContent(msg, &msg->msg_data);
2024 static BOOL CDecodeHashMsg_GetParam(CDecodeMsg *msg, DWORD dwParamType,
2025 DWORD dwIndex, void *pvData, DWORD *pcbData)
2029 switch (dwParamType)
2031 case CMSG_TYPE_PARAM:
2032 ret = CRYPT_CopyParam(pvData, pcbData, &msg->type, sizeof(msg->type));
2034 case CMSG_HASH_ALGORITHM_PARAM:
2036 CRYPT_DATA_BLOB blob;
2038 ret = ContextPropertyList_FindProperty(msg->properties, dwParamType,
2042 ret = CRYPT_CopyParam(pvData, pcbData, blob.pbData, blob.cbData);
2044 CRYPT_FixUpAlgorithmID(pvData);
2047 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2050 case CMSG_COMPUTED_HASH_PARAM:
2051 ret = CryptGetHashParam(msg->u.hash, HP_HASHVAL, pvData, pcbData, 0);
2055 CRYPT_DATA_BLOB blob;
2057 ret = ContextPropertyList_FindProperty(msg->properties, dwParamType,
2060 ret = CRYPT_CopyParam(pvData, pcbData, blob.pbData, blob.cbData);
2062 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2068 /* nextData is an in/out parameter - on input it's the memory location in
2069 * which a copy of in's data should be made, and on output it's the memory
2070 * location immediately after out's copy of in's data.
2072 static inline void CRYPT_CopyBlob(CRYPT_DATA_BLOB *out,
2073 const CRYPT_DATA_BLOB *in, LPBYTE *nextData)
2075 out->cbData = in->cbData;
2078 out->pbData = *nextData;
2079 memcpy(out->pbData, in->pbData, in->cbData);
2080 *nextData += in->cbData;
2084 static inline void CRYPT_CopyAlgorithmId(CRYPT_ALGORITHM_IDENTIFIER *out,
2085 const CRYPT_ALGORITHM_IDENTIFIER *in, LPBYTE *nextData)
2089 out->pszObjId = (LPSTR)*nextData;
2090 strcpy(out->pszObjId, in->pszObjId);
2091 *nextData += strlen(out->pszObjId) + 1;
2093 CRYPT_CopyBlob(&out->Parameters, &in->Parameters, nextData);
2096 static inline void CRYPT_CopyAttributes(CRYPT_ATTRIBUTES *out,
2097 const CRYPT_ATTRIBUTES *in, LPBYTE *nextData)
2099 out->cAttr = in->cAttr;
2104 *nextData = POINTER_ALIGN_DWORD_PTR(*nextData);
2105 out->rgAttr = (CRYPT_ATTRIBUTE *)*nextData;
2106 *nextData += in->cAttr * sizeof(CRYPT_ATTRIBUTE);
2107 for (i = 0; i < in->cAttr; i++)
2109 if (in->rgAttr[i].pszObjId)
2111 out->rgAttr[i].pszObjId = (LPSTR)*nextData;
2112 strcpy(out->rgAttr[i].pszObjId, in->rgAttr[i].pszObjId);
2113 *nextData += strlen(in->rgAttr[i].pszObjId) + 1;
2115 if (in->rgAttr[i].cValue)
2119 out->rgAttr[i].cValue = in->rgAttr[i].cValue;
2120 *nextData = POINTER_ALIGN_DWORD_PTR(*nextData);
2121 out->rgAttr[i].rgValue = (PCRYPT_DATA_BLOB)*nextData;
2122 *nextData += in->rgAttr[i].cValue * sizeof(CRYPT_DATA_BLOB);
2123 for (j = 0; j < in->rgAttr[i].cValue; j++)
2124 CRYPT_CopyBlob(&out->rgAttr[i].rgValue[j],
2125 &in->rgAttr[i].rgValue[j], nextData);
2131 static DWORD CRYPT_SizeOfAttributes(const CRYPT_ATTRIBUTES *attr)
2133 DWORD size = attr->cAttr * sizeof(CRYPT_ATTRIBUTE), i, j;
2135 for (i = 0; i < attr->cAttr; i++)
2137 if (attr->rgAttr[i].pszObjId)
2138 size += strlen(attr->rgAttr[i].pszObjId) + 1;
2140 size = ALIGN_DWORD_PTR(size);
2141 size += attr->rgAttr[i].cValue * sizeof(CRYPT_DATA_BLOB);
2142 for (j = 0; j < attr->rgAttr[i].cValue; j++)
2143 size += attr->rgAttr[i].rgValue[j].cbData;
2145 /* align pointer again to be conservative */
2146 size = ALIGN_DWORD_PTR(size);
2150 static DWORD CRYPT_SizeOfKeyIdAsIssuerAndSerial(const CRYPT_DATA_BLOB *keyId)
2152 static char oid_key_rdn[] = szOID_KEYID_RDN;
2155 CERT_RDN rdn = { 1, &attr };
2156 CERT_NAME_INFO name = { 1, &rdn };
2158 attr.pszObjId = oid_key_rdn;
2159 attr.dwValueType = CERT_RDN_OCTET_STRING;
2160 attr.Value.cbData = keyId->cbData;
2161 attr.Value.pbData = keyId->pbData;
2162 if (CryptEncodeObject(X509_ASN_ENCODING, X509_NAME, &name, NULL, &size))
2163 size++; /* Only include size of special zero serial number on success */
2167 static BOOL CRYPT_CopyKeyIdAsIssuerAndSerial(CERT_NAME_BLOB *issuer,
2168 CRYPT_INTEGER_BLOB *serialNumber, const CRYPT_DATA_BLOB *keyId, DWORD encodedLen,
2171 static char oid_key_rdn[] = szOID_KEYID_RDN;
2173 CERT_RDN rdn = { 1, &attr };
2174 CERT_NAME_INFO name = { 1, &rdn };
2177 /* Encode special zero serial number */
2178 serialNumber->cbData = 1;
2179 serialNumber->pbData = *nextData;
2183 issuer->pbData = *nextData;
2184 attr.pszObjId = oid_key_rdn;
2185 attr.dwValueType = CERT_RDN_OCTET_STRING;
2186 attr.Value.cbData = keyId->cbData;
2187 attr.Value.pbData = keyId->pbData;
2188 ret = CryptEncodeObject(X509_ASN_ENCODING, X509_NAME, &name, *nextData,
2192 *nextData += encodedLen;
2193 issuer->cbData = encodedLen;
2198 static BOOL CRYPT_CopySignerInfo(void *pvData, DWORD *pcbData,
2199 const CMSG_CMS_SIGNER_INFO *in)
2201 DWORD size = sizeof(CMSG_SIGNER_INFO), rdnSize = 0;
2204 TRACE("(%p, %d, %p)\n", pvData, pvData ? *pcbData : 0, in);
2206 if (in->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
2208 size += in->SignerId.u.IssuerSerialNumber.Issuer.cbData;
2209 size += in->SignerId.u.IssuerSerialNumber.SerialNumber.cbData;
2213 rdnSize = CRYPT_SizeOfKeyIdAsIssuerAndSerial(&in->SignerId.u.KeyId);
2216 if (in->HashAlgorithm.pszObjId)
2217 size += strlen(in->HashAlgorithm.pszObjId) + 1;
2218 size += in->HashAlgorithm.Parameters.cbData;
2219 if (in->HashEncryptionAlgorithm.pszObjId)
2220 size += strlen(in->HashEncryptionAlgorithm.pszObjId) + 1;
2221 size += in->HashEncryptionAlgorithm.Parameters.cbData;
2222 size += in->EncryptedHash.cbData;
2224 size = ALIGN_DWORD_PTR(size);
2225 size += CRYPT_SizeOfAttributes(&in->AuthAttrs);
2226 size += CRYPT_SizeOfAttributes(&in->UnauthAttrs);
2232 else if (*pcbData < size)
2235 SetLastError(ERROR_MORE_DATA);
2240 LPBYTE nextData = (BYTE *)pvData + sizeof(CMSG_SIGNER_INFO);
2241 CMSG_SIGNER_INFO *out = pvData;
2244 out->dwVersion = in->dwVersion;
2245 if (in->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
2247 CRYPT_CopyBlob(&out->Issuer,
2248 &in->SignerId.u.IssuerSerialNumber.Issuer, &nextData);
2249 CRYPT_CopyBlob(&out->SerialNumber,
2250 &in->SignerId.u.IssuerSerialNumber.SerialNumber, &nextData);
2253 ret = CRYPT_CopyKeyIdAsIssuerAndSerial(&out->Issuer, &out->SerialNumber,
2254 &in->SignerId.u.KeyId, rdnSize, &nextData);
2257 CRYPT_CopyAlgorithmId(&out->HashAlgorithm, &in->HashAlgorithm,
2259 CRYPT_CopyAlgorithmId(&out->HashEncryptionAlgorithm,
2260 &in->HashEncryptionAlgorithm, &nextData);
2261 CRYPT_CopyBlob(&out->EncryptedHash, &in->EncryptedHash, &nextData);
2262 nextData = POINTER_ALIGN_DWORD_PTR(nextData);
2263 CRYPT_CopyAttributes(&out->AuthAttrs, &in->AuthAttrs, &nextData);
2264 CRYPT_CopyAttributes(&out->UnauthAttrs, &in->UnauthAttrs, &nextData);
2267 TRACE("returning %d\n", ret);
2271 static BOOL CRYPT_CopyCMSSignerInfo(void *pvData, DWORD *pcbData,
2272 const CMSG_CMS_SIGNER_INFO *in)
2274 DWORD size = sizeof(CMSG_CMS_SIGNER_INFO);
2277 TRACE("(%p, %d, %p)\n", pvData, pvData ? *pcbData : 0, in);
2279 if (in->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
2281 size += in->SignerId.u.IssuerSerialNumber.Issuer.cbData;
2282 size += in->SignerId.u.IssuerSerialNumber.SerialNumber.cbData;
2285 size += in->SignerId.u.KeyId.cbData;
2286 if (in->HashAlgorithm.pszObjId)
2287 size += strlen(in->HashAlgorithm.pszObjId) + 1;
2288 size += in->HashAlgorithm.Parameters.cbData;
2289 if (in->HashEncryptionAlgorithm.pszObjId)
2290 size += strlen(in->HashEncryptionAlgorithm.pszObjId) + 1;
2291 size += in->HashEncryptionAlgorithm.Parameters.cbData;
2292 size += in->EncryptedHash.cbData;
2294 size = ALIGN_DWORD_PTR(size);
2295 size += CRYPT_SizeOfAttributes(&in->AuthAttrs);
2296 size += CRYPT_SizeOfAttributes(&in->UnauthAttrs);
2302 else if (*pcbData < size)
2305 SetLastError(ERROR_MORE_DATA);
2310 LPBYTE nextData = (BYTE *)pvData + sizeof(CMSG_CMS_SIGNER_INFO);
2311 CMSG_CMS_SIGNER_INFO *out = pvData;
2313 out->dwVersion = in->dwVersion;
2314 out->SignerId.dwIdChoice = in->SignerId.dwIdChoice;
2315 if (in->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
2317 CRYPT_CopyBlob(&out->SignerId.u.IssuerSerialNumber.Issuer,
2318 &in->SignerId.u.IssuerSerialNumber.Issuer, &nextData);
2319 CRYPT_CopyBlob(&out->SignerId.u.IssuerSerialNumber.SerialNumber,
2320 &in->SignerId.u.IssuerSerialNumber.SerialNumber, &nextData);
2323 CRYPT_CopyBlob(&out->SignerId.u.KeyId, &in->SignerId.u.KeyId, &nextData);
2324 CRYPT_CopyAlgorithmId(&out->HashAlgorithm, &in->HashAlgorithm,
2326 CRYPT_CopyAlgorithmId(&out->HashEncryptionAlgorithm,
2327 &in->HashEncryptionAlgorithm, &nextData);
2328 CRYPT_CopyBlob(&out->EncryptedHash, &in->EncryptedHash, &nextData);
2329 nextData = POINTER_ALIGN_DWORD_PTR(nextData);
2330 CRYPT_CopyAttributes(&out->AuthAttrs, &in->AuthAttrs, &nextData);
2331 CRYPT_CopyAttributes(&out->UnauthAttrs, &in->UnauthAttrs, &nextData);
2334 TRACE("returning %d\n", ret);
2338 static BOOL CRYPT_CopySignerCertInfo(void *pvData, DWORD *pcbData,
2339 const CMSG_CMS_SIGNER_INFO *in)
2341 DWORD size = sizeof(CERT_INFO), rdnSize = 0;
2344 TRACE("(%p, %d, %p)\n", pvData, pvData ? *pcbData : 0, in);
2346 if (in->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
2348 size += in->SignerId.u.IssuerSerialNumber.Issuer.cbData;
2349 size += in->SignerId.u.IssuerSerialNumber.SerialNumber.cbData;
2353 rdnSize = CRYPT_SizeOfKeyIdAsIssuerAndSerial(&in->SignerId.u.KeyId);
2361 else if (*pcbData < size)
2364 SetLastError(ERROR_MORE_DATA);
2369 LPBYTE nextData = (BYTE *)pvData + sizeof(CERT_INFO);
2370 CERT_INFO *out = pvData;
2372 memset(out, 0, sizeof(CERT_INFO));
2373 if (in->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
2375 CRYPT_CopyBlob(&out->Issuer,
2376 &in->SignerId.u.IssuerSerialNumber.Issuer, &nextData);
2377 CRYPT_CopyBlob(&out->SerialNumber,
2378 &in->SignerId.u.IssuerSerialNumber.SerialNumber, &nextData);
2382 ret = CRYPT_CopyKeyIdAsIssuerAndSerial(&out->Issuer, &out->SerialNumber,
2383 &in->SignerId.u.KeyId, rdnSize, &nextData);
2385 TRACE("returning %d\n", ret);
2389 static BOOL CDecodeSignedMsg_GetParam(CDecodeMsg *msg, DWORD dwParamType,
2390 DWORD dwIndex, void *pvData, DWORD *pcbData)
2394 switch (dwParamType)
2396 case CMSG_TYPE_PARAM:
2397 ret = CRYPT_CopyParam(pvData, pcbData, &msg->type, sizeof(msg->type));
2399 case CMSG_CONTENT_PARAM:
2400 if (msg->u.signed_data.info)
2402 if (!strcmp(msg->u.signed_data.info->content.pszObjId,
2405 CRYPT_DATA_BLOB *blob;
2408 ret = CryptDecodeObjectEx(X509_ASN_ENCODING, X509_OCTET_STRING,
2409 msg->u.signed_data.info->content.Content.pbData,
2410 msg->u.signed_data.info->content.Content.cbData,
2411 CRYPT_DECODE_ALLOC_FLAG, NULL, &blob, &size);
2414 ret = CRYPT_CopyParam(pvData, pcbData, blob->pbData,
2420 ret = CRYPT_CopyParam(pvData, pcbData,
2421 msg->u.signed_data.info->content.Content.pbData,
2422 msg->u.signed_data.info->content.Content.cbData);
2425 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2427 case CMSG_INNER_CONTENT_TYPE_PARAM:
2428 if (msg->u.signed_data.info)
2429 ret = CRYPT_CopyParam(pvData, pcbData,
2430 msg->u.signed_data.info->content.pszObjId,
2431 strlen(msg->u.signed_data.info->content.pszObjId) + 1);
2433 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2435 case CMSG_SIGNER_COUNT_PARAM:
2436 if (msg->u.signed_data.info)
2437 ret = CRYPT_CopyParam(pvData, pcbData,
2438 &msg->u.signed_data.info->cSignerInfo, sizeof(DWORD));
2440 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2442 case CMSG_SIGNER_INFO_PARAM:
2443 if (msg->u.signed_data.info)
2445 if (dwIndex >= msg->u.signed_data.info->cSignerInfo)
2446 SetLastError(CRYPT_E_INVALID_INDEX);
2448 ret = CRYPT_CopySignerInfo(pvData, pcbData,
2449 &msg->u.signed_data.info->rgSignerInfo[dwIndex]);
2452 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2454 case CMSG_SIGNER_CERT_INFO_PARAM:
2455 if (msg->u.signed_data.info)
2457 if (dwIndex >= msg->u.signed_data.info->cSignerInfo)
2458 SetLastError(CRYPT_E_INVALID_INDEX);
2460 ret = CRYPT_CopySignerCertInfo(pvData, pcbData,
2461 &msg->u.signed_data.info->rgSignerInfo[dwIndex]);
2464 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2466 case CMSG_CERT_COUNT_PARAM:
2467 if (msg->u.signed_data.info)
2468 ret = CRYPT_CopyParam(pvData, pcbData,
2469 &msg->u.signed_data.info->cCertEncoded, sizeof(DWORD));
2471 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2473 case CMSG_CERT_PARAM:
2474 if (msg->u.signed_data.info)
2476 if (dwIndex >= msg->u.signed_data.info->cCertEncoded)
2477 SetLastError(CRYPT_E_INVALID_INDEX);
2479 ret = CRYPT_CopyParam(pvData, pcbData,
2480 msg->u.signed_data.info->rgCertEncoded[dwIndex].pbData,
2481 msg->u.signed_data.info->rgCertEncoded[dwIndex].cbData);
2484 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2486 case CMSG_CRL_COUNT_PARAM:
2487 if (msg->u.signed_data.info)
2488 ret = CRYPT_CopyParam(pvData, pcbData,
2489 &msg->u.signed_data.info->cCrlEncoded, sizeof(DWORD));
2491 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2493 case CMSG_CRL_PARAM:
2494 if (msg->u.signed_data.info)
2496 if (dwIndex >= msg->u.signed_data.info->cCrlEncoded)
2497 SetLastError(CRYPT_E_INVALID_INDEX);
2499 ret = CRYPT_CopyParam(pvData, pcbData,
2500 msg->u.signed_data.info->rgCrlEncoded[dwIndex].pbData,
2501 msg->u.signed_data.info->rgCrlEncoded[dwIndex].cbData);
2504 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2506 case CMSG_COMPUTED_HASH_PARAM:
2507 if (msg->u.signed_data.info)
2509 if (dwIndex >= msg->u.signed_data.cSignerHandle)
2510 SetLastError(CRYPT_E_INVALID_INDEX);
2512 ret = CryptGetHashParam(
2513 msg->u.signed_data.signerHandles[dwIndex].contentHash,
2514 HP_HASHVAL, pvData, pcbData, 0);
2517 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2519 case CMSG_ENCODED_SIGNER:
2520 if (msg->u.signed_data.info)
2522 if (dwIndex >= msg->u.signed_data.info->cSignerInfo)
2523 SetLastError(CRYPT_E_INVALID_INDEX);
2525 ret = CryptEncodeObjectEx(
2526 X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, CMS_SIGNER_INFO,
2527 &msg->u.signed_data.info->rgSignerInfo[dwIndex], 0, NULL,
2531 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2533 case CMSG_ATTR_CERT_COUNT_PARAM:
2534 if (msg->u.signed_data.info)
2536 DWORD attrCertCount = 0;
2538 ret = CRYPT_CopyParam(pvData, pcbData,
2539 &attrCertCount, sizeof(DWORD));
2542 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2544 case CMSG_ATTR_CERT_PARAM:
2545 if (msg->u.signed_data.info)
2546 SetLastError(CRYPT_E_INVALID_INDEX);
2548 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2550 case CMSG_CMS_SIGNER_INFO_PARAM:
2551 if (msg->u.signed_data.info)
2553 if (dwIndex >= msg->u.signed_data.info->cSignerInfo)
2554 SetLastError(CRYPT_E_INVALID_INDEX);
2556 ret = CRYPT_CopyCMSSignerInfo(pvData, pcbData,
2557 &msg->u.signed_data.info->rgSignerInfo[dwIndex]);
2560 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2563 FIXME("unimplemented for %d\n", dwParamType);
2564 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2569 static BOOL CDecodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
2570 DWORD dwIndex, void *pvData, DWORD *pcbData)
2572 CDecodeMsg *msg = hCryptMsg;
2578 ret = CDecodeHashMsg_GetParam(msg, dwParamType, dwIndex, pvData,
2582 ret = CDecodeSignedMsg_GetParam(msg, dwParamType, dwIndex, pvData,
2586 switch (dwParamType)
2588 case CMSG_TYPE_PARAM:
2589 ret = CRYPT_CopyParam(pvData, pcbData, &msg->type,
2594 CRYPT_DATA_BLOB blob;
2596 ret = ContextPropertyList_FindProperty(msg->properties, dwParamType,
2599 ret = CRYPT_CopyParam(pvData, pcbData, blob.pbData,
2602 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2609 static BOOL CDecodeHashMsg_VerifyHash(CDecodeMsg *msg)
2612 CRYPT_DATA_BLOB hashBlob;
2614 ret = ContextPropertyList_FindProperty(msg->properties,
2615 CMSG_HASH_DATA_PARAM, &hashBlob);
2618 DWORD computedHashSize = 0;
2620 ret = CDecodeHashMsg_GetParam(msg, CMSG_COMPUTED_HASH_PARAM, 0, NULL,
2622 if (hashBlob.cbData == computedHashSize)
2624 LPBYTE computedHash = CryptMemAlloc(computedHashSize);
2628 ret = CDecodeHashMsg_GetParam(msg, CMSG_COMPUTED_HASH_PARAM, 0,
2629 computedHash, &computedHashSize);
2632 if (memcmp(hashBlob.pbData, computedHash, hashBlob.cbData))
2634 SetLastError(CRYPT_E_HASH_VALUE);
2638 CryptMemFree(computedHash);
2642 SetLastError(ERROR_OUTOFMEMORY);
2648 SetLastError(CRYPT_E_HASH_VALUE);
2655 static BOOL CDecodeSignedMsg_VerifySignatureWithKey(CDecodeMsg *msg,
2656 HCRYPTPROV prov, DWORD signerIndex, PCERT_PUBLIC_KEY_INFO keyInfo)
2662 prov = msg->crypt_prov;
2663 ret = CryptImportPublicKeyInfo(prov, X509_ASN_ENCODING, keyInfo, &key);
2667 CRYPT_HASH_BLOB reversedHash;
2669 if (msg->u.signed_data.info->rgSignerInfo[signerIndex].AuthAttrs.cAttr)
2670 hash = msg->u.signed_data.signerHandles[signerIndex].authAttrHash;
2672 hash = msg->u.signed_data.signerHandles[signerIndex].contentHash;
2673 ret = CRYPT_ConstructBlob(&reversedHash,
2674 &msg->u.signed_data.info->rgSignerInfo[signerIndex].EncryptedHash);
2677 CRYPT_ReverseBytes(&reversedHash);
2678 ret = CryptVerifySignatureW(hash, reversedHash.pbData,
2679 reversedHash.cbData, key, NULL, 0);
2680 CryptMemFree(reversedHash.pbData);
2682 CryptDestroyKey(key);
2687 static BOOL CDecodeSignedMsg_VerifySignature(CDecodeMsg *msg, PCERT_INFO info)
2692 if (!msg->u.signed_data.signerHandles)
2694 SetLastError(NTE_BAD_SIGNATURE);
2697 for (i = 0; !ret && i < msg->u.signed_data.info->cSignerInfo; i++)
2699 PCMSG_CMS_SIGNER_INFO signerInfo =
2700 &msg->u.signed_data.info->rgSignerInfo[i];
2702 if (signerInfo->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
2704 ret = CertCompareCertificateName(X509_ASN_ENCODING,
2705 &signerInfo->SignerId.u.IssuerSerialNumber.Issuer,
2709 ret = CertCompareIntegerBlob(
2710 &signerInfo->SignerId.u.IssuerSerialNumber.SerialNumber,
2711 &info->SerialNumber);
2718 FIXME("signer %d: unimplemented for key id\n", i);
2722 ret = CDecodeSignedMsg_VerifySignatureWithKey(msg, 0, i,
2723 &info->SubjectPublicKeyInfo);
2725 SetLastError(CRYPT_E_SIGNER_NOT_FOUND);
2730 static BOOL CDecodeSignedMsg_VerifySignatureEx(CDecodeMsg *msg,
2731 PCMSG_CTRL_VERIFY_SIGNATURE_EX_PARA para)
2735 if (para->cbSize != sizeof(CMSG_CTRL_VERIFY_SIGNATURE_EX_PARA))
2736 SetLastError(ERROR_INVALID_PARAMETER);
2737 else if (para->dwSignerIndex >= msg->u.signed_data.info->cSignerInfo)
2738 SetLastError(CRYPT_E_SIGNER_NOT_FOUND);
2739 else if (!msg->u.signed_data.signerHandles)
2740 SetLastError(NTE_BAD_SIGNATURE);
2743 switch (para->dwSignerType)
2745 case CMSG_VERIFY_SIGNER_PUBKEY:
2746 ret = CDecodeSignedMsg_VerifySignatureWithKey(msg,
2747 para->hCryptProv, para->dwSignerIndex, para->pvSigner);
2749 case CMSG_VERIFY_SIGNER_CERT:
2751 PCCERT_CONTEXT cert = para->pvSigner;
2753 ret = CDecodeSignedMsg_VerifySignatureWithKey(msg, para->hCryptProv,
2754 para->dwSignerIndex, &cert->pCertInfo->SubjectPublicKeyInfo);
2758 FIXME("unimplemented for signer type %d\n", para->dwSignerType);
2759 SetLastError(CRYPT_E_SIGNER_NOT_FOUND);
2765 static BOOL CDecodeMsg_Control(HCRYPTMSG hCryptMsg, DWORD dwFlags,
2766 DWORD dwCtrlType, const void *pvCtrlPara)
2768 CDecodeMsg *msg = hCryptMsg;
2773 case CMSG_CTRL_VERIFY_SIGNATURE:
2777 ret = CDecodeSignedMsg_VerifySignature(msg, (PCERT_INFO)pvCtrlPara);
2780 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2783 case CMSG_CTRL_DECRYPT:
2787 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2790 case CMSG_CTRL_VERIFY_HASH:
2794 ret = CDecodeHashMsg_VerifyHash(msg);
2797 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2800 case CMSG_CTRL_VERIFY_SIGNATURE_EX:
2804 ret = CDecodeSignedMsg_VerifySignatureEx(msg,
2805 (PCMSG_CTRL_VERIFY_SIGNATURE_EX_PARA)pvCtrlPara);
2808 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2812 SetLastError(CRYPT_E_CONTROL_TYPE);
2817 HCRYPTMSG WINAPI CryptMsgOpenToDecode(DWORD dwMsgEncodingType, DWORD dwFlags,
2818 DWORD dwMsgType, HCRYPTPROV_LEGACY hCryptProv, PCERT_INFO pRecipientInfo,
2819 PCMSG_STREAM_INFO pStreamInfo)
2823 TRACE("(%08x, %08x, %08x, %08lx, %p, %p)\n", dwMsgEncodingType,
2824 dwFlags, dwMsgType, hCryptProv, pRecipientInfo, pStreamInfo);
2826 if (GET_CMSG_ENCODING_TYPE(dwMsgEncodingType) != PKCS_7_ASN_ENCODING)
2828 SetLastError(E_INVALIDARG);
2831 msg = CryptMemAlloc(sizeof(CDecodeMsg));
2834 CryptMsgBase_Init((CryptMsgBase *)msg, dwFlags, pStreamInfo,
2835 CDecodeMsg_Close, CDecodeMsg_GetParam, CDecodeMsg_Update,
2836 CDecodeMsg_Control);
2837 msg->type = dwMsgType;
2839 msg->crypt_prov = hCryptProv;
2842 msg->crypt_prov = CRYPT_GetDefaultProvider();
2843 msg->base.open_flags &= ~CMSG_CRYPT_RELEASE_CONTEXT_FLAG;
2845 memset(&msg->u, 0, sizeof(msg->u));
2846 msg->msg_data.cbData = 0;
2847 msg->msg_data.pbData = NULL;
2848 msg->detached_data.cbData = 0;
2849 msg->detached_data.pbData = NULL;
2850 msg->properties = ContextPropertyList_Create();
2855 HCRYPTMSG WINAPI CryptMsgDuplicate(HCRYPTMSG hCryptMsg)
2857 TRACE("(%p)\n", hCryptMsg);
2861 CryptMsgBase *msg = hCryptMsg;
2863 InterlockedIncrement(&msg->ref);
2868 BOOL WINAPI CryptMsgClose(HCRYPTMSG hCryptMsg)
2870 TRACE("(%p)\n", hCryptMsg);
2874 CryptMsgBase *msg = hCryptMsg;
2876 if (InterlockedDecrement(&msg->ref) == 0)
2878 TRACE("freeing %p\n", msg);
2887 BOOL WINAPI CryptMsgUpdate(HCRYPTMSG hCryptMsg, const BYTE *pbData,
2888 DWORD cbData, BOOL fFinal)
2890 CryptMsgBase *msg = hCryptMsg;
2892 TRACE("(%p, %p, %d, %d)\n", hCryptMsg, pbData, cbData, fFinal);
2894 return msg->update(hCryptMsg, pbData, cbData, fFinal);
2897 BOOL WINAPI CryptMsgGetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
2898 DWORD dwIndex, void *pvData, DWORD *pcbData)
2900 CryptMsgBase *msg = hCryptMsg;
2902 TRACE("(%p, %d, %d, %p, %p)\n", hCryptMsg, dwParamType, dwIndex,
2904 return msg->get_param(hCryptMsg, dwParamType, dwIndex, pvData, pcbData);
2907 BOOL WINAPI CryptMsgControl(HCRYPTMSG hCryptMsg, DWORD dwFlags,
2908 DWORD dwCtrlType, const void *pvCtrlPara)
2910 CryptMsgBase *msg = hCryptMsg;
2912 TRACE("(%p, %08x, %d, %p)\n", hCryptMsg, dwFlags, dwCtrlType,
2914 return msg->control(hCryptMsg, dwFlags, dwCtrlType, pvCtrlPara);
2917 static CERT_INFO *CRYPT_GetSignerCertInfoFromMsg(HCRYPTMSG msg,
2918 DWORD dwSignerIndex)
2920 CERT_INFO *certInfo = NULL;
2923 if (CryptMsgGetParam(msg, CMSG_SIGNER_CERT_INFO_PARAM, dwSignerIndex, NULL,
2926 certInfo = CryptMemAlloc(size);
2929 if (!CryptMsgGetParam(msg, CMSG_SIGNER_CERT_INFO_PARAM,
2930 dwSignerIndex, certInfo, &size))
2932 CryptMemFree(certInfo);
2940 BOOL WINAPI CryptMsgGetAndVerifySigner(HCRYPTMSG hCryptMsg, DWORD cSignerStore,
2941 HCERTSTORE *rghSignerStore, DWORD dwFlags, PCCERT_CONTEXT *ppSigner,
2942 DWORD *pdwSignerIndex)
2945 DWORD i, signerIndex = 0;
2946 PCCERT_CONTEXT signerCert = NULL;
2949 TRACE("(%p, %d, %p, %08x, %p, %p)\n", hCryptMsg, cSignerStore,
2950 rghSignerStore, dwFlags, ppSigner, pdwSignerIndex);
2952 /* Clear output parameters */
2955 if (pdwSignerIndex && !(dwFlags & CMSG_USE_SIGNER_INDEX_FLAG))
2956 *pdwSignerIndex = 0;
2958 /* Create store to search for signer certificates */
2959 store = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
2960 CERT_STORE_CREATE_NEW_FLAG, NULL);
2961 if (!(dwFlags & CMSG_TRUSTED_SIGNER_FLAG))
2963 HCERTSTORE msgStore = CertOpenStore(CERT_STORE_PROV_MSG, 0, 0, 0,
2966 CertAddStoreToCollection(store, msgStore, 0, 0);
2967 CertCloseStore(msgStore, 0);
2969 for (i = 0; i < cSignerStore; i++)
2970 CertAddStoreToCollection(store, rghSignerStore[i], 0, 0);
2972 /* Find signer cert */
2973 if (dwFlags & CMSG_USE_SIGNER_INDEX_FLAG)
2975 CERT_INFO *signer = CRYPT_GetSignerCertInfoFromMsg(hCryptMsg,
2980 signerIndex = *pdwSignerIndex;
2981 signerCert = CertFindCertificateInStore(store, X509_ASN_ENCODING,
2982 0, CERT_FIND_SUBJECT_CERT, signer, NULL);
2983 CryptMemFree(signer);
2988 DWORD count, size = sizeof(count);
2990 if (CryptMsgGetParam(hCryptMsg, CMSG_SIGNER_COUNT_PARAM, 0, &count,
2993 for (i = 0; !signerCert && i < count; i++)
2995 CERT_INFO *signer = CRYPT_GetSignerCertInfoFromMsg(hCryptMsg,
3000 signerCert = CertFindCertificateInStore(store,
3001 X509_ASN_ENCODING, 0, CERT_FIND_SUBJECT_CERT, signer,
3005 CryptMemFree(signer);
3010 SetLastError(CRYPT_E_NO_TRUSTED_SIGNER);
3014 if (!(dwFlags & CMSG_SIGNER_ONLY_FLAG))
3015 ret = CryptMsgControl(hCryptMsg, 0, CMSG_CTRL_VERIFY_SIGNATURE,
3016 signerCert->pCertInfo);
3022 *ppSigner = CertDuplicateCertificateContext(signerCert);
3024 *pdwSignerIndex = signerIndex;
3026 CertFreeCertificateContext(signerCert);
3029 CertCloseStore(store, 0);
3033 BOOL WINAPI CryptMsgVerifyCountersignatureEncodedEx(HCRYPTPROV_LEGACY hCryptProv,
3034 DWORD dwEncodingType, PBYTE pbSignerInfo, DWORD cbSignerInfo,
3035 PBYTE pbSignerInfoCountersignature, DWORD cbSignerInfoCountersignature,
3036 DWORD dwSignerType, void *pvSigner, DWORD dwFlags, void *pvReserved)
3038 FIXME("(%08lx, %08x, %p, %d, %p, %d, %d, %p, %08x, %p): stub\n", hCryptProv,
3039 dwEncodingType, pbSignerInfo, cbSignerInfo, pbSignerInfoCountersignature,
3040 cbSignerInfoCountersignature, dwSignerType, pvSigner, dwFlags, pvReserved);
3044 BOOL WINAPI CryptMsgEncodeAndSignCTL(DWORD dwMsgEncodingType,
3045 PCTL_INFO pCtlInfo, PCMSG_SIGNED_ENCODE_INFO pSignInfo, DWORD dwFlags,
3046 BYTE *pbEncoded, DWORD *pcbEncoded)
3052 TRACE("(%08x, %p, %p, %08x, %p, %p)\n", dwMsgEncodingType, pCtlInfo,
3053 pSignInfo, dwFlags, pbEncoded, pcbEncoded);
3057 FIXME("unimplemented for flags %08x\n", dwFlags);
3060 if ((ret = CryptEncodeObjectEx(dwMsgEncodingType, PKCS_CTL, pCtlInfo,
3061 CRYPT_ENCODE_ALLOC_FLAG, NULL, &pbCtlContent, &cbCtlContent)))
3063 ret = CryptMsgSignCTL(dwMsgEncodingType, pbCtlContent, cbCtlContent,
3064 pSignInfo, dwFlags, pbEncoded, pcbEncoded);
3065 LocalFree(pbCtlContent);
3070 BOOL WINAPI CryptMsgSignCTL(DWORD dwMsgEncodingType, BYTE *pbCtlContent,
3071 DWORD cbCtlContent, PCMSG_SIGNED_ENCODE_INFO pSignInfo, DWORD dwFlags,
3072 BYTE *pbEncoded, DWORD *pcbEncoded)
3074 static char oid_ctl[] = szOID_CTL;
3078 TRACE("(%08x, %p, %d, %p, %08x, %p, %p)\n", dwMsgEncodingType,
3079 pbCtlContent, cbCtlContent, pSignInfo, dwFlags, pbEncoded, pcbEncoded);
3083 FIXME("unimplemented for flags %08x\n", dwFlags);
3086 msg = CryptMsgOpenToEncode(dwMsgEncodingType, 0, CMSG_SIGNED, pSignInfo,
3090 ret = CryptMsgUpdate(msg, pbCtlContent, cbCtlContent, TRUE);
3092 ret = CryptMsgGetParam(msg, CMSG_CONTENT_PARAM, 0, pbEncoded,