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 CRYPT_DATA_BLOB blob = { cbData, (LPBYTE)pbData };
282 msg->base.state = MsgStateFinalized;
283 /* non-streamed data messages don't allow non-final updates,
284 * don't bother checking whether data already exist, they can't.
286 ret = CryptEncodeObjectEx(X509_ASN_ENCODING, X509_OCTET_STRING,
287 &blob, CRYPT_ENCODE_ALLOC_FLAG, NULL, &msg->bare_content,
288 &msg->bare_content_len);
294 static BOOL CRYPT_CopyParam(void *pvData, DWORD *pcbData, const void *src,
301 else if (*pcbData < len)
304 SetLastError(ERROR_MORE_DATA);
310 memcpy(pvData, src, len);
315 static BOOL CDataEncodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
316 DWORD dwIndex, void *pvData, DWORD *pcbData)
318 CDataEncodeMsg *msg = hCryptMsg;
323 case CMSG_CONTENT_PARAM:
324 if (msg->base.streamed)
325 SetLastError(E_INVALIDARG);
328 CRYPT_CONTENT_INFO info;
329 char rsa_data[] = "1.2.840.113549.1.7.1";
331 info.pszObjId = rsa_data;
332 info.Content.cbData = msg->bare_content_len;
333 info.Content.pbData = msg->bare_content;
334 ret = CryptEncodeObject(X509_ASN_ENCODING, PKCS_CONTENT_INFO, &info,
338 case CMSG_BARE_CONTENT_PARAM:
339 if (msg->base.streamed)
340 SetLastError(E_INVALIDARG);
342 ret = CRYPT_CopyParam(pvData, pcbData, msg->bare_content,
343 msg->bare_content_len);
346 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
351 static HCRYPTMSG CDataEncodeMsg_Open(DWORD dwFlags, const void *pvMsgEncodeInfo,
352 LPSTR pszInnerContentObjID, PCMSG_STREAM_INFO pStreamInfo)
358 SetLastError(E_INVALIDARG);
361 msg = CryptMemAlloc(sizeof(CDataEncodeMsg));
364 CryptMsgBase_Init((CryptMsgBase *)msg, dwFlags, pStreamInfo,
365 CDataEncodeMsg_Close, CDataEncodeMsg_GetParam, CDataEncodeMsg_Update,
366 CRYPT_DefaultMsgControl);
367 msg->bare_content_len = sizeof(empty_data_content);
368 msg->bare_content = (LPBYTE)empty_data_content;
373 typedef struct _CHashEncodeMsg
378 CRYPT_DATA_BLOB data;
381 static void CHashEncodeMsg_Close(HCRYPTMSG hCryptMsg)
383 CHashEncodeMsg *msg = hCryptMsg;
385 CryptMemFree(msg->data.pbData);
386 CryptDestroyHash(msg->hash);
387 if (msg->base.open_flags & CMSG_CRYPT_RELEASE_CONTEXT_FLAG)
388 CryptReleaseContext(msg->prov, 0);
391 static BOOL CRYPT_EncodePKCSDigestedData(CHashEncodeMsg *msg, void *pvData,
396 DWORD size = sizeof(algID);
398 ret = CryptGetHashParam(msg->hash, HP_ALGID, (BYTE *)&algID, &size, 0);
401 CRYPT_DIGESTED_DATA digestedData = { 0 };
402 char oid_rsa_data[] = szOID_RSA_data;
404 digestedData.version = CMSG_HASHED_DATA_PKCS_1_5_VERSION;
405 digestedData.DigestAlgorithm.pszObjId = (LPSTR)CertAlgIdToOID(algID);
406 /* FIXME: what about digestedData.DigestAlgorithm.Parameters? */
407 /* Quirk: OID is only encoded messages if an update has happened */
408 if (msg->base.state != MsgStateInit)
409 digestedData.ContentInfo.pszObjId = oid_rsa_data;
410 if (!(msg->base.open_flags & CMSG_DETACHED_FLAG) && msg->data.cbData)
412 ret = CRYPT_AsnEncodeOctets(0, NULL, &msg->data,
413 CRYPT_ENCODE_ALLOC_FLAG, NULL,
414 (LPBYTE)&digestedData.ContentInfo.Content.pbData,
415 &digestedData.ContentInfo.Content.cbData);
417 if (msg->base.state == MsgStateFinalized)
419 size = sizeof(DWORD);
420 ret = CryptGetHashParam(msg->hash, HP_HASHSIZE,
421 (LPBYTE)&digestedData.hash.cbData, &size, 0);
424 digestedData.hash.pbData = CryptMemAlloc(
425 digestedData.hash.cbData);
426 ret = CryptGetHashParam(msg->hash, HP_HASHVAL,
427 digestedData.hash.pbData, &digestedData.hash.cbData, 0);
431 ret = CRYPT_AsnEncodePKCSDigestedData(&digestedData, pvData,
433 CryptMemFree(digestedData.hash.pbData);
434 LocalFree(digestedData.ContentInfo.Content.pbData);
439 static BOOL CHashEncodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
440 DWORD dwIndex, void *pvData, DWORD *pcbData)
442 CHashEncodeMsg *msg = hCryptMsg;
445 TRACE("(%p, %d, %d, %p, %p)\n", hCryptMsg, dwParamType, dwIndex,
450 case CMSG_BARE_CONTENT_PARAM:
451 if (msg->base.streamed)
452 SetLastError(E_INVALIDARG);
454 ret = CRYPT_EncodePKCSDigestedData(msg, pvData, pcbData);
456 case CMSG_CONTENT_PARAM:
458 CRYPT_CONTENT_INFO info;
460 ret = CryptMsgGetParam(hCryptMsg, CMSG_BARE_CONTENT_PARAM, 0, NULL,
461 &info.Content.cbData);
464 info.Content.pbData = CryptMemAlloc(info.Content.cbData);
465 if (info.Content.pbData)
467 ret = CryptMsgGetParam(hCryptMsg, CMSG_BARE_CONTENT_PARAM, 0,
468 info.Content.pbData, &info.Content.cbData);
471 char oid_rsa_hashed[] = szOID_RSA_hashedData;
473 info.pszObjId = oid_rsa_hashed;
474 ret = CryptEncodeObjectEx(X509_ASN_ENCODING,
475 PKCS_CONTENT_INFO, &info, 0, NULL, pvData, pcbData);
477 CryptMemFree(info.Content.pbData);
484 case CMSG_COMPUTED_HASH_PARAM:
485 ret = CryptGetHashParam(msg->hash, HP_HASHVAL, pvData, pcbData, 0);
487 case CMSG_VERSION_PARAM:
488 if (msg->base.state != MsgStateFinalized)
489 SetLastError(CRYPT_E_MSG_ERROR);
492 DWORD version = CMSG_HASHED_DATA_PKCS_1_5_VERSION;
494 /* Since the data are always encoded as octets, the version is
495 * always 0 (see rfc3852, section 7)
497 ret = CRYPT_CopyParam(pvData, pcbData, &version, sizeof(version));
501 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
506 static BOOL CHashEncodeMsg_Update(HCRYPTMSG hCryptMsg, const BYTE *pbData,
507 DWORD cbData, BOOL fFinal)
509 CHashEncodeMsg *msg = hCryptMsg;
512 TRACE("(%p, %p, %d, %d)\n", hCryptMsg, pbData, cbData, fFinal);
514 if (msg->base.state == MsgStateFinalized)
515 SetLastError(CRYPT_E_MSG_ERROR);
516 else if (msg->base.streamed || (msg->base.open_flags & CMSG_DETACHED_FLAG))
518 /* Doesn't do much, as stream output is never called, and you
519 * can't get the content.
521 ret = CryptHashData(msg->hash, pbData, cbData, 0);
522 msg->base.state = fFinal ? MsgStateFinalized : MsgStateUpdated;
527 SetLastError(CRYPT_E_MSG_ERROR);
530 ret = CryptHashData(msg->hash, pbData, cbData, 0);
533 msg->data.pbData = CryptMemAlloc(cbData);
534 if (msg->data.pbData)
536 memcpy(msg->data.pbData + msg->data.cbData, pbData, cbData);
537 msg->data.cbData += cbData;
542 msg->base.state = MsgStateFinalized;
548 static HCRYPTMSG CHashEncodeMsg_Open(DWORD dwFlags, const void *pvMsgEncodeInfo,
549 LPSTR pszInnerContentObjID, PCMSG_STREAM_INFO pStreamInfo)
552 const CMSG_HASHED_ENCODE_INFO *info = pvMsgEncodeInfo;
556 if (info->cbSize != sizeof(CMSG_HASHED_ENCODE_INFO))
558 SetLastError(E_INVALIDARG);
561 if (!(algID = CertOIDToAlgId(info->HashAlgorithm.pszObjId)))
563 SetLastError(CRYPT_E_UNKNOWN_ALGO);
566 if (info->hCryptProv)
567 prov = info->hCryptProv;
570 prov = CRYPT_GetDefaultProvider();
571 dwFlags &= ~CMSG_CRYPT_RELEASE_CONTEXT_FLAG;
573 msg = CryptMemAlloc(sizeof(CHashEncodeMsg));
576 CryptMsgBase_Init((CryptMsgBase *)msg, dwFlags, pStreamInfo,
577 CHashEncodeMsg_Close, CHashEncodeMsg_GetParam, CHashEncodeMsg_Update,
578 CRYPT_DefaultMsgControl);
580 msg->data.cbData = 0;
581 msg->data.pbData = NULL;
582 if (!CryptCreateHash(prov, algID, 0, 0, &msg->hash))
591 typedef struct _CMSG_SIGNER_ENCODE_INFO_WITH_CMS
594 PCERT_INFO pCertInfo;
595 HCRYPTPROV hCryptProv;
597 CRYPT_ALGORITHM_IDENTIFIER HashAlgorithm;
600 PCRYPT_ATTRIBUTE rgAuthAttr;
602 PCRYPT_ATTRIBUTE rgUnauthAttr;
604 CRYPT_ALGORITHM_IDENTIFIER HashEncryptionAlgorithm;
605 void *pvHashEncryptionAuxInfo;
606 } CMSG_SIGNER_ENCODE_INFO_WITH_CMS, *PCMSG_SIGNER_ENCODE_INFO_WITH_CMS;
608 typedef struct _CMSG_SIGNED_ENCODE_INFO_WITH_CMS
612 PCMSG_SIGNER_ENCODE_INFO_WITH_CMS rgSigners;
614 PCERT_BLOB rgCertEncoded;
616 PCRL_BLOB rgCrlEncoded;
617 DWORD cAttrCertEncoded;
618 PCERT_BLOB rgAttrCertEncoded;
619 } CMSG_SIGNED_ENCODE_INFO_WITH_CMS, *PCMSG_SIGNED_ENCODE_INFO_WITH_CMS;
621 static BOOL CRYPT_IsValidSigner(const CMSG_SIGNER_ENCODE_INFO_WITH_CMS *signer)
623 if (signer->cbSize != sizeof(CMSG_SIGNER_ENCODE_INFO) &&
624 signer->cbSize != sizeof(CMSG_SIGNER_ENCODE_INFO_WITH_CMS))
626 SetLastError(E_INVALIDARG);
629 if (signer->cbSize == sizeof(CMSG_SIGNER_ENCODE_INFO))
631 if (!signer->pCertInfo->SerialNumber.cbData)
633 SetLastError(E_INVALIDARG);
636 if (!signer->pCertInfo->Issuer.cbData)
638 SetLastError(E_INVALIDARG);
642 else if (signer->cbSize == sizeof(CMSG_SIGNER_ENCODE_INFO_WITH_CMS))
644 switch (signer->SignerId.dwIdChoice)
647 if (!signer->pCertInfo->SerialNumber.cbData)
649 SetLastError(E_INVALIDARG);
652 if (!signer->pCertInfo->Issuer.cbData)
654 SetLastError(E_INVALIDARG);
658 case CERT_ID_ISSUER_SERIAL_NUMBER:
659 if (!signer->SignerId.u.IssuerSerialNumber.SerialNumber.cbData)
661 SetLastError(E_INVALIDARG);
664 if (!signer->SignerId.u.IssuerSerialNumber.Issuer.cbData)
666 SetLastError(E_INVALIDARG);
670 case CERT_ID_KEY_IDENTIFIER:
671 if (!signer->SignerId.u.KeyId.cbData)
673 SetLastError(E_INVALIDARG);
678 SetLastError(E_INVALIDARG);
680 if (signer->HashEncryptionAlgorithm.pszObjId)
682 FIXME("CMSG_SIGNER_ENCODE_INFO with CMS fields unsupported\n");
686 if (!signer->hCryptProv)
688 SetLastError(E_INVALIDARG);
691 if (!CertOIDToAlgId(signer->HashAlgorithm.pszObjId))
693 SetLastError(CRYPT_E_UNKNOWN_ALGO);
699 static BOOL CRYPT_ConstructBlob(CRYPT_DATA_BLOB *out, const CRYPT_DATA_BLOB *in)
703 out->cbData = in->cbData;
706 out->pbData = CryptMemAlloc(out->cbData);
708 memcpy(out->pbData, in->pbData, out->cbData);
717 static BOOL CRYPT_ConstructBlobArray(DWORD *outCBlobs,
718 PCRYPT_DATA_BLOB *outPBlobs, DWORD cBlobs, const CRYPT_DATA_BLOB *pBlobs)
725 *outPBlobs = CryptMemAlloc(cBlobs * sizeof(CRYPT_DATA_BLOB));
730 memset(*outPBlobs, 0, cBlobs * sizeof(CRYPT_DATA_BLOB));
731 for (i = 0; ret && i < cBlobs; i++)
732 ret = CRYPT_ConstructBlob(&(*outPBlobs)[i], &pBlobs[i]);
740 static void CRYPT_FreeBlobArray(DWORD cBlobs, PCRYPT_DATA_BLOB blobs)
744 for (i = 0; i < cBlobs; i++)
745 CryptMemFree(blobs[i].pbData);
749 static BOOL CRYPT_ConstructAttribute(CRYPT_ATTRIBUTE *out,
750 const CRYPT_ATTRIBUTE *in)
754 out->pszObjId = CryptMemAlloc(strlen(in->pszObjId) + 1);
757 strcpy(out->pszObjId, in->pszObjId);
758 ret = CRYPT_ConstructBlobArray(&out->cValue, &out->rgValue,
759 in->cValue, in->rgValue);
766 static BOOL CRYPT_ConstructAttributes(CRYPT_ATTRIBUTES *out,
767 const CRYPT_ATTRIBUTES *in)
771 out->cAttr = in->cAttr;
774 out->rgAttr = CryptMemAlloc(out->cAttr * sizeof(CRYPT_ATTRIBUTE));
779 memset(out->rgAttr, 0, out->cAttr * sizeof(CRYPT_ATTRIBUTE));
780 for (i = 0; ret && i < out->cAttr; i++)
781 ret = CRYPT_ConstructAttribute(&out->rgAttr[i], &in->rgAttr[i]);
791 /* Constructs a CMSG_CMS_SIGNER_INFO from a CMSG_SIGNER_ENCODE_INFO_WITH_CMS. */
792 static BOOL CSignerInfo_Construct(CMSG_CMS_SIGNER_INFO *info,
793 const CMSG_SIGNER_ENCODE_INFO_WITH_CMS *in)
797 if (in->cbSize == sizeof(CMSG_SIGNER_ENCODE_INFO))
799 info->dwVersion = CMSG_SIGNER_INFO_V1;
800 ret = CRYPT_ConstructBlob(&info->SignerId.u.IssuerSerialNumber.Issuer,
801 &in->pCertInfo->Issuer);
803 ret = CRYPT_ConstructBlob(
804 &info->SignerId.u.IssuerSerialNumber.SerialNumber,
805 &in->pCertInfo->SerialNumber);
806 info->SignerId.dwIdChoice = CERT_ID_ISSUER_SERIAL_NUMBER;
807 info->HashEncryptionAlgorithm.pszObjId =
808 in->pCertInfo->SubjectPublicKeyInfo.Algorithm.pszObjId;
810 ret = CRYPT_ConstructBlob(&info->HashEncryptionAlgorithm.Parameters,
811 &in->pCertInfo->SubjectPublicKeyInfo.Algorithm.Parameters);
815 const CRYPT_ALGORITHM_IDENTIFIER *pEncrAlg;
817 /* Implicitly in->cbSize == sizeof(CMSG_SIGNER_ENCODE_INFO_WITH_CMS).
818 * See CRYPT_IsValidSigner.
820 if (!in->SignerId.dwIdChoice)
822 info->dwVersion = CMSG_SIGNER_INFO_V1;
823 ret = CRYPT_ConstructBlob(&info->SignerId.u.IssuerSerialNumber.Issuer,
824 &in->pCertInfo->Issuer);
826 ret = CRYPT_ConstructBlob(
827 &info->SignerId.u.IssuerSerialNumber.SerialNumber,
828 &in->pCertInfo->SerialNumber);
829 info->SignerId.dwIdChoice = CERT_ID_ISSUER_SERIAL_NUMBER;
831 else if (in->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
833 info->dwVersion = CMSG_SIGNER_INFO_V1;
834 info->SignerId.dwIdChoice = CERT_ID_ISSUER_SERIAL_NUMBER;
835 ret = CRYPT_ConstructBlob(&info->SignerId.u.IssuerSerialNumber.Issuer,
836 &in->SignerId.u.IssuerSerialNumber.Issuer);
838 ret = CRYPT_ConstructBlob(
839 &info->SignerId.u.IssuerSerialNumber.SerialNumber,
840 &in->SignerId.u.IssuerSerialNumber.SerialNumber);
844 /* Implicitly dwIdChoice == CERT_ID_KEY_IDENTIFIER */
845 info->dwVersion = CMSG_SIGNER_INFO_V3;
846 info->SignerId.dwIdChoice = CERT_ID_KEY_IDENTIFIER;
847 ret = CRYPT_ConstructBlob(&info->SignerId.u.KeyId,
848 &in->SignerId.u.KeyId);
850 pEncrAlg = in->HashEncryptionAlgorithm.pszObjId ?
851 &in->HashEncryptionAlgorithm :
852 &in->pCertInfo->SubjectPublicKeyInfo.Algorithm;
853 info->HashEncryptionAlgorithm.pszObjId = pEncrAlg->pszObjId;
855 ret = CRYPT_ConstructBlob(&info->HashEncryptionAlgorithm.Parameters,
856 &pEncrAlg->Parameters);
858 /* Assumption: algorithm IDs will point to static strings, not
859 * stack-based ones, so copying the pointer values is safe.
861 info->HashAlgorithm.pszObjId = in->HashAlgorithm.pszObjId;
863 ret = CRYPT_ConstructBlob(&info->HashAlgorithm.Parameters,
864 &in->HashAlgorithm.Parameters);
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->HashEncryptionAlgorithm.Parameters.pbData);
887 CryptMemFree(info->EncryptedHash.pbData);
888 for (i = 0; i < info->AuthAttrs.cAttr; i++)
890 for (j = 0; j < info->AuthAttrs.rgAttr[i].cValue; j++)
891 CryptMemFree(info->AuthAttrs.rgAttr[i].rgValue[j].pbData);
892 CryptMemFree(info->AuthAttrs.rgAttr[i].rgValue);
893 CryptMemFree(info->AuthAttrs.rgAttr[i].pszObjId);
895 CryptMemFree(info->AuthAttrs.rgAttr);
896 for (i = 0; i < info->UnauthAttrs.cAttr; i++)
898 for (j = 0; j < info->UnauthAttrs.rgAttr[i].cValue; j++)
899 CryptMemFree(info->UnauthAttrs.rgAttr[i].rgValue[j].pbData);
900 CryptMemFree(info->UnauthAttrs.rgAttr[i].rgValue);
901 CryptMemFree(info->UnauthAttrs.rgAttr[i].pszObjId);
903 CryptMemFree(info->UnauthAttrs.rgAttr);
906 typedef struct _CSignerHandles
908 HCRYPTHASH contentHash;
909 HCRYPTHASH authAttrHash;
912 typedef struct _CSignedMsgData
914 CRYPT_SIGNED_INFO *info;
916 CSignerHandles *signerHandles;
919 /* Constructs the signer handles for the signerIndex'th signer of msg_data.
920 * Assumes signerIndex is a valid idnex, and that msg_data's info has already
923 static BOOL CSignedMsgData_ConstructSignerHandles(CSignedMsgData *msg_data,
924 DWORD signerIndex, HCRYPTPROV crypt_prov)
929 algID = CertOIDToAlgId(
930 msg_data->info->rgSignerInfo[signerIndex].HashAlgorithm.pszObjId);
931 ret = CryptCreateHash(crypt_prov, algID, 0, 0,
932 &msg_data->signerHandles->contentHash);
933 if (ret && msg_data->info->rgSignerInfo[signerIndex].AuthAttrs.cAttr > 0)
934 ret = CryptCreateHash(crypt_prov, algID, 0, 0,
935 &msg_data->signerHandles->authAttrHash);
939 /* Allocates a CSignedMsgData's handles. Assumes its info has already been
942 static BOOL CSignedMsgData_AllocateHandles(CSignedMsgData *msg_data)
946 if (msg_data->info->cSignerInfo)
948 msg_data->signerHandles =
949 CryptMemAlloc(msg_data->info->cSignerInfo * sizeof(CSignerHandles));
950 if (msg_data->signerHandles)
952 msg_data->cSignerHandle = msg_data->info->cSignerInfo;
953 memset(msg_data->signerHandles, 0,
954 msg_data->info->cSignerInfo * sizeof(CSignerHandles));
958 msg_data->cSignerHandle = 0;
964 msg_data->cSignerHandle = 0;
965 msg_data->signerHandles = NULL;
970 static void CSignedMsgData_CloseHandles(CSignedMsgData *msg_data)
974 for (i = 0; i < msg_data->cSignerHandle; i++)
976 if (msg_data->signerHandles[i].contentHash)
977 CryptDestroyHash(msg_data->signerHandles[i].contentHash);
978 if (msg_data->signerHandles[i].authAttrHash)
979 CryptDestroyHash(msg_data->signerHandles[i].authAttrHash);
981 CryptMemFree(msg_data->signerHandles);
982 msg_data->signerHandles = NULL;
983 msg_data->cSignerHandle = 0;
986 static BOOL CSignedMsgData_UpdateHash(CSignedMsgData *msg_data,
987 const BYTE *pbData, DWORD cbData)
992 for (i = 0; ret && i < msg_data->cSignerHandle; i++)
993 ret = CryptHashData(msg_data->signerHandles[i].contentHash, pbData,
998 static BOOL CRYPT_AppendAttribute(CRYPT_ATTRIBUTES *out,
999 const CRYPT_ATTRIBUTE *in)
1003 out->rgAttr = CryptMemRealloc(out->rgAttr,
1004 (out->cAttr + 1) * sizeof(CRYPT_ATTRIBUTE));
1007 ret = CRYPT_ConstructAttribute(&out->rgAttr[out->cAttr], in);
1014 static BOOL CSignedMsgData_AppendMessageDigestAttribute(
1015 CSignedMsgData *msg_data, DWORD signerIndex)
1019 CRYPT_HASH_BLOB hash = { 0, NULL }, encodedHash = { 0, NULL };
1020 char messageDigest[] = szOID_RSA_messageDigest;
1021 CRYPT_ATTRIBUTE messageDigestAttr = { messageDigest, 1, &encodedHash };
1023 size = sizeof(DWORD);
1024 ret = CryptGetHashParam(
1025 msg_data->signerHandles[signerIndex].contentHash, HP_HASHSIZE,
1026 (LPBYTE)&hash.cbData, &size, 0);
1029 hash.pbData = CryptMemAlloc(hash.cbData);
1030 ret = CryptGetHashParam(
1031 msg_data->signerHandles[signerIndex].contentHash, HP_HASHVAL,
1032 hash.pbData, &hash.cbData, 0);
1035 ret = CRYPT_AsnEncodeOctets(0, NULL, &hash, CRYPT_ENCODE_ALLOC_FLAG,
1036 NULL, (LPBYTE)&encodedHash.pbData, &encodedHash.cbData);
1039 ret = CRYPT_AppendAttribute(
1040 &msg_data->info->rgSignerInfo[signerIndex].AuthAttrs,
1041 &messageDigestAttr);
1042 LocalFree(encodedHash.pbData);
1045 CryptMemFree(hash.pbData);
1055 static BOOL CSignedMsgData_UpdateAuthenticatedAttributes(
1056 CSignedMsgData *msg_data, SignOrVerify flag)
1061 TRACE("(%p)\n", msg_data);
1063 for (i = 0; ret && i < msg_data->info->cSignerInfo; i++)
1065 if (msg_data->info->rgSignerInfo[i].AuthAttrs.cAttr)
1069 BYTE oid_rsa_data_encoded[] = { 0x06,0x09,0x2a,0x86,0x48,0x86,
1070 0xf7,0x0d,0x01,0x07,0x01 };
1071 CRYPT_DATA_BLOB content = { sizeof(oid_rsa_data_encoded),
1072 oid_rsa_data_encoded };
1073 char contentType[] = szOID_RSA_contentType;
1074 CRYPT_ATTRIBUTE contentTypeAttr = { contentType, 1, &content };
1076 /* FIXME: does this depend on inner OID? */
1077 ret = CRYPT_AppendAttribute(
1078 &msg_data->info->rgSignerInfo[i].AuthAttrs, &contentTypeAttr);
1080 ret = CSignedMsgData_AppendMessageDigestAttribute(msg_data,
1085 LPBYTE encodedAttrs;
1088 ret = CryptEncodeObjectEx(X509_ASN_ENCODING, PKCS_ATTRIBUTES,
1089 &msg_data->info->rgSignerInfo[i].AuthAttrs,
1090 CRYPT_ENCODE_ALLOC_FLAG, NULL, &encodedAttrs, &size);
1093 ret = CryptHashData(
1094 msg_data->signerHandles[i].authAttrHash, encodedAttrs,
1096 LocalFree(encodedAttrs);
1101 TRACE("returning %d\n", ret);
1105 static void CRYPT_ReverseBytes(CRYPT_HASH_BLOB *hash)
1110 for (i = 0; i < hash->cbData / 2; i++)
1112 tmp = hash->pbData[hash->cbData - i - 1];
1113 hash->pbData[hash->cbData - i - 1] = hash->pbData[i];
1114 hash->pbData[i] = tmp;
1118 static BOOL CSignedMsgData_Sign(CSignedMsgData *msg_data)
1123 TRACE("(%p)\n", msg_data);
1125 for (i = 0; ret && i < msg_data->info->cSignerInfo; i++)
1129 if (msg_data->info->rgSignerInfo[i].AuthAttrs.cAttr)
1130 hash = msg_data->signerHandles[i].authAttrHash;
1132 hash = msg_data->signerHandles[i].contentHash;
1133 ret = CryptSignHashW(hash, AT_SIGNATURE, NULL, 0, NULL,
1134 &msg_data->info->rgSignerInfo[i].EncryptedHash.cbData);
1137 msg_data->info->rgSignerInfo[i].EncryptedHash.pbData =
1139 msg_data->info->rgSignerInfo[i].EncryptedHash.cbData);
1140 if (msg_data->info->rgSignerInfo[i].EncryptedHash.pbData)
1142 ret = CryptSignHashW(hash, AT_SIGNATURE, NULL, 0,
1143 msg_data->info->rgSignerInfo[i].EncryptedHash.pbData,
1144 &msg_data->info->rgSignerInfo[i].EncryptedHash.cbData);
1147 &msg_data->info->rgSignerInfo[i].EncryptedHash);
1156 static BOOL CSignedMsgData_Update(CSignedMsgData *msg_data,
1157 const BYTE *pbData, DWORD cbData, BOOL fFinal, SignOrVerify flag)
1159 BOOL ret = CSignedMsgData_UpdateHash(msg_data, pbData, cbData);
1163 ret = CSignedMsgData_UpdateAuthenticatedAttributes(msg_data, flag);
1164 if (ret && flag == Sign)
1165 ret = CSignedMsgData_Sign(msg_data);
1170 typedef struct _CSignedEncodeMsg
1174 CRYPT_DATA_BLOB data;
1175 CSignedMsgData msg_data;
1178 static void CSignedEncodeMsg_Close(HCRYPTMSG hCryptMsg)
1180 CSignedEncodeMsg *msg = hCryptMsg;
1183 CryptMemFree(msg->innerOID);
1184 CryptMemFree(msg->data.pbData);
1185 CRYPT_FreeBlobArray(msg->msg_data.info->cCertEncoded,
1186 msg->msg_data.info->rgCertEncoded);
1187 CRYPT_FreeBlobArray(msg->msg_data.info->cCrlEncoded,
1188 msg->msg_data.info->rgCrlEncoded);
1189 for (i = 0; i < msg->msg_data.info->cSignerInfo; i++)
1190 CSignerInfo_Free(&msg->msg_data.info->rgSignerInfo[i]);
1191 CSignedMsgData_CloseHandles(&msg->msg_data);
1192 CryptMemFree(msg->msg_data.info->rgSignerInfo);
1193 CryptMemFree(msg->msg_data.info);
1196 static BOOL CSignedEncodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
1197 DWORD dwIndex, void *pvData, DWORD *pcbData)
1199 CSignedEncodeMsg *msg = hCryptMsg;
1202 switch (dwParamType)
1204 case CMSG_CONTENT_PARAM:
1206 CRYPT_CONTENT_INFO info;
1208 ret = CryptMsgGetParam(hCryptMsg, CMSG_BARE_CONTENT_PARAM, 0, NULL,
1209 &info.Content.cbData);
1212 info.Content.pbData = CryptMemAlloc(info.Content.cbData);
1213 if (info.Content.pbData)
1215 ret = CryptMsgGetParam(hCryptMsg, CMSG_BARE_CONTENT_PARAM, 0,
1216 info.Content.pbData, &info.Content.cbData);
1219 char oid_rsa_signed[] = szOID_RSA_signedData;
1221 info.pszObjId = oid_rsa_signed;
1222 ret = CryptEncodeObjectEx(X509_ASN_ENCODING,
1223 PKCS_CONTENT_INFO, &info, 0, NULL, pvData, pcbData);
1225 CryptMemFree(info.Content.pbData);
1232 case CMSG_BARE_CONTENT_PARAM:
1234 CRYPT_SIGNED_INFO info;
1235 BOOL freeContent = FALSE;
1237 info = *msg->msg_data.info;
1238 if (!msg->innerOID || !strcmp(msg->innerOID, szOID_RSA_data))
1240 char oid_rsa_data[] = szOID_RSA_data;
1242 /* Quirk: OID is only encoded messages if an update has happened */
1243 if (msg->base.state != MsgStateInit)
1244 info.content.pszObjId = oid_rsa_data;
1246 info.content.pszObjId = NULL;
1247 if (msg->data.cbData)
1249 CRYPT_DATA_BLOB blob = { msg->data.cbData, msg->data.pbData };
1251 ret = CryptEncodeObjectEx(X509_ASN_ENCODING, X509_OCTET_STRING,
1252 &blob, CRYPT_ENCODE_ALLOC_FLAG, NULL,
1253 &info.content.Content.pbData, &info.content.Content.cbData);
1258 info.content.Content.cbData = 0;
1259 info.content.Content.pbData = NULL;
1265 info.content.pszObjId = msg->innerOID;
1266 info.content.Content.cbData = msg->data.cbData;
1267 info.content.Content.pbData = msg->data.pbData;
1272 ret = CRYPT_AsnEncodeCMSSignedInfo(&info, pvData, pcbData);
1274 LocalFree(info.content.Content.pbData);
1278 case CMSG_COMPUTED_HASH_PARAM:
1279 if (dwIndex >= msg->msg_data.cSignerHandle)
1280 SetLastError(CRYPT_E_INVALID_INDEX);
1282 ret = CryptGetHashParam(
1283 msg->msg_data.signerHandles[dwIndex].contentHash, HP_HASHVAL,
1284 pvData, pcbData, 0);
1286 case CMSG_ENCODED_SIGNER:
1287 if (dwIndex >= msg->msg_data.info->cSignerInfo)
1288 SetLastError(CRYPT_E_INVALID_INDEX);
1290 ret = CryptEncodeObjectEx(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
1291 CMS_SIGNER_INFO, &msg->msg_data.info->rgSignerInfo[dwIndex], 0,
1292 NULL, pvData, pcbData);
1294 case CMSG_VERSION_PARAM:
1295 ret = CRYPT_CopyParam(pvData, pcbData, &msg->msg_data.info->version,
1296 sizeof(msg->msg_data.info->version));
1299 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1304 static BOOL CSignedEncodeMsg_Update(HCRYPTMSG hCryptMsg, const BYTE *pbData,
1305 DWORD cbData, BOOL fFinal)
1307 CSignedEncodeMsg *msg = hCryptMsg;
1310 if (msg->base.state == MsgStateFinalized)
1311 SetLastError(CRYPT_E_MSG_ERROR);
1312 else if (msg->base.streamed || (msg->base.open_flags & CMSG_DETACHED_FLAG))
1314 ret = CSignedMsgData_Update(&msg->msg_data, pbData, cbData, fFinal,
1316 if (msg->base.streamed)
1317 FIXME("streamed partial stub\n");
1318 msg->base.state = fFinal ? MsgStateFinalized : MsgStateUpdated;
1323 SetLastError(CRYPT_E_MSG_ERROR);
1328 msg->data.pbData = CryptMemAlloc(cbData);
1329 if (msg->data.pbData)
1331 memcpy(msg->data.pbData, pbData, cbData);
1332 msg->data.cbData = cbData;
1339 ret = CSignedMsgData_Update(&msg->msg_data, pbData, cbData,
1341 msg->base.state = MsgStateFinalized;
1347 static HCRYPTMSG CSignedEncodeMsg_Open(DWORD dwFlags,
1348 const void *pvMsgEncodeInfo, LPCSTR pszInnerContentObjID,
1349 PCMSG_STREAM_INFO pStreamInfo)
1351 const CMSG_SIGNED_ENCODE_INFO_WITH_CMS *info = pvMsgEncodeInfo;
1353 CSignedEncodeMsg *msg;
1355 if (info->cbSize != sizeof(CMSG_SIGNED_ENCODE_INFO) &&
1356 info->cbSize != sizeof(CMSG_SIGNED_ENCODE_INFO_WITH_CMS))
1358 SetLastError(E_INVALIDARG);
1361 if (info->cbSize == sizeof(CMSG_SIGNED_ENCODE_INFO_WITH_CMS) &&
1362 info->cAttrCertEncoded)
1364 FIXME("CMSG_SIGNED_ENCODE_INFO with CMS fields unsupported\n");
1367 for (i = 0; i < info->cSigners; i++)
1368 if (!CRYPT_IsValidSigner(&info->rgSigners[i]))
1370 msg = CryptMemAlloc(sizeof(CSignedEncodeMsg));
1375 CryptMsgBase_Init((CryptMsgBase *)msg, dwFlags, pStreamInfo,
1376 CSignedEncodeMsg_Close, CSignedEncodeMsg_GetParam,
1377 CSignedEncodeMsg_Update, CRYPT_DefaultMsgControl);
1378 if (pszInnerContentObjID)
1380 msg->innerOID = CryptMemAlloc(strlen(pszInnerContentObjID) + 1);
1382 strcpy(msg->innerOID, pszInnerContentObjID);
1387 msg->innerOID = NULL;
1388 msg->data.cbData = 0;
1389 msg->data.pbData = NULL;
1391 msg->msg_data.info = CryptMemAlloc(sizeof(CRYPT_SIGNED_INFO));
1393 msg->msg_data.info = NULL;
1394 if (msg->msg_data.info)
1396 memset(msg->msg_data.info, 0, sizeof(CRYPT_SIGNED_INFO));
1397 msg->msg_data.info->version = CMSG_SIGNED_DATA_V1;
1405 msg->msg_data.info->rgSignerInfo =
1406 CryptMemAlloc(info->cSigners * sizeof(CMSG_CMS_SIGNER_INFO));
1407 if (msg->msg_data.info->rgSignerInfo)
1409 msg->msg_data.info->cSignerInfo = info->cSigners;
1410 memset(msg->msg_data.info->rgSignerInfo, 0,
1411 msg->msg_data.info->cSignerInfo *
1412 sizeof(CMSG_CMS_SIGNER_INFO));
1413 ret = CSignedMsgData_AllocateHandles(&msg->msg_data);
1414 for (i = 0; ret && i < msg->msg_data.info->cSignerInfo; i++)
1416 if (info->rgSigners[i].SignerId.dwIdChoice ==
1417 CERT_ID_KEY_IDENTIFIER)
1418 msg->msg_data.info->version = CMSG_SIGNED_DATA_V3;
1419 ret = CSignerInfo_Construct(
1420 &msg->msg_data.info->rgSignerInfo[i],
1421 &info->rgSigners[i]);
1424 ret = CSignedMsgData_ConstructSignerHandles(
1425 &msg->msg_data, i, info->rgSigners[i].hCryptProv);
1426 if (dwFlags & CMSG_CRYPT_RELEASE_CONTEXT_FLAG)
1427 CryptReleaseContext(info->rgSigners[i].hCryptProv,
1437 msg->msg_data.info->cSignerInfo = 0;
1438 msg->msg_data.signerHandles = NULL;
1439 msg->msg_data.cSignerHandle = 0;
1443 ret = CRYPT_ConstructBlobArray(&msg->msg_data.info->cCertEncoded,
1444 &msg->msg_data.info->rgCertEncoded, info->cCertEncoded,
1445 info->rgCertEncoded);
1447 ret = CRYPT_ConstructBlobArray(&msg->msg_data.info->cCrlEncoded,
1448 &msg->msg_data.info->rgCrlEncoded, info->cCrlEncoded,
1449 info->rgCrlEncoded);
1452 CSignedEncodeMsg_Close(msg);
1459 typedef struct _CMSG_ENVELOPED_ENCODE_INFO_WITH_CMS
1462 HCRYPTPROV_LEGACY hCryptProv;
1463 CRYPT_ALGORITHM_IDENTIFIER ContentEncryptionAlgorithm;
1464 void *pvEncryptionAuxInfo;
1466 PCERT_INFO *rgpRecipientCert;
1467 PCMSG_RECIPIENT_ENCODE_INFO rgCmsRecipients;
1469 PCERT_BLOB rgCertEncoded;
1471 PCRL_BLOB rgCrlEncoded;
1472 DWORD cAttrCertEncoded;
1473 PCERT_BLOB rgAttrCertEncoded;
1474 DWORD cUnprotectedAttr;
1475 PCRYPT_ATTRIBUTE rgUnprotectedAttr;
1476 } CMSG_ENVELOPED_ENCODE_INFO_WITH_CMS, *PCMSG_ENVELOPED_ENCODE_INFO_WITH_CMS;
1478 typedef struct _CEnvelopedEncodeMsg
1481 CRYPT_ALGORITHM_IDENTIFIER algo;
1484 DWORD cRecipientInfo;
1485 CMSG_KEY_TRANS_RECIPIENT_INFO *recipientInfo;
1486 CRYPT_DATA_BLOB data;
1487 } CEnvelopedEncodeMsg;
1489 static BOOL CRYPT_ConstructAlgorithmId(CRYPT_ALGORITHM_IDENTIFIER *out,
1490 const CRYPT_ALGORITHM_IDENTIFIER *in)
1492 out->pszObjId = CryptMemAlloc(strlen(in->pszObjId) + 1);
1495 strcpy(out->pszObjId, in->pszObjId);
1496 return CRYPT_ConstructBlob(&out->Parameters, &in->Parameters);
1502 static BOOL CRYPT_ConstructBitBlob(CRYPT_BIT_BLOB *out, const CRYPT_BIT_BLOB *in)
1504 out->cbData = in->cbData;
1505 out->cUnusedBits = in->cUnusedBits;
1508 out->pbData = CryptMemAlloc(out->cbData);
1510 memcpy(out->pbData, in->pbData, out->cbData);
1519 static BOOL CRYPT_GenKey(CMSG_CONTENT_ENCRYPT_INFO *info, ALG_ID algID)
1521 static HCRYPTOIDFUNCSET set = NULL;
1522 PFN_CMSG_GEN_CONTENT_ENCRYPT_KEY genKeyFunc = NULL;
1523 HCRYPTOIDFUNCADDR hFunc;
1527 set = CryptInitOIDFunctionSet(CMSG_OID_GEN_CONTENT_ENCRYPT_KEY_FUNC, 0);
1528 CryptGetOIDFunctionAddress(set, X509_ASN_ENCODING,
1529 info->ContentEncryptionAlgorithm.pszObjId, 0, (void **)&genKeyFunc, &hFunc);
1532 ret = genKeyFunc(info, 0, NULL);
1533 CryptFreeOIDFunctionAddress(hFunc, 0);
1536 ret = CryptGenKey(info->hCryptProv, algID, CRYPT_EXPORTABLE,
1537 &info->hContentEncryptKey);
1541 static BOOL WINAPI CRYPT_ExportKeyTrans(
1542 PCMSG_CONTENT_ENCRYPT_INFO pContentEncryptInfo,
1543 PCMSG_KEY_TRANS_RECIPIENT_ENCODE_INFO pKeyTransEncodeInfo,
1544 PCMSG_KEY_TRANS_ENCRYPT_INFO pKeyTransEncryptInfo,
1545 DWORD dwFlags, void *pvReserved)
1547 CERT_PUBLIC_KEY_INFO keyInfo;
1551 ret = CRYPT_ConstructAlgorithmId(&keyInfo.Algorithm,
1552 &pKeyTransEncodeInfo->KeyEncryptionAlgorithm);
1554 CRYPT_ConstructBitBlob(&keyInfo.PublicKey,
1555 &pKeyTransEncodeInfo->RecipientPublicKey);
1558 ret = CryptImportPublicKeyInfo(pKeyTransEncodeInfo->hCryptProv,
1559 X509_ASN_ENCODING, &keyInfo, &expKey);
1565 ret = CryptExportKey(pContentEncryptInfo->hContentEncryptKey, expKey,
1566 SIMPLEBLOB, 0, NULL, &size);
1567 keyBlob = CryptMemAlloc(size);
1570 ret = CryptExportKey(pContentEncryptInfo->hContentEncryptKey,
1571 expKey, SIMPLEBLOB, 0, keyBlob, &size);
1574 DWORD head = sizeof(BLOBHEADER) + sizeof(ALG_ID);
1576 pKeyTransEncryptInfo->EncryptedKey.pbData =
1577 CryptMemAlloc(size - head);
1578 if (pKeyTransEncryptInfo->EncryptedKey.pbData)
1582 pKeyTransEncryptInfo->EncryptedKey.cbData = size - head;
1583 for (i = size - 1; i >= head; --i, ++k)
1584 pKeyTransEncryptInfo->EncryptedKey.pbData[k] =
1590 CryptMemFree(keyBlob);
1594 CryptDestroyKey(expKey);
1597 CryptMemFree(keyInfo.PublicKey.pbData);
1598 CryptMemFree(keyInfo.Algorithm.pszObjId);
1599 CryptMemFree(keyInfo.Algorithm.Parameters.pbData);
1603 static BOOL CRYPT_ExportEncryptedKey(CMSG_CONTENT_ENCRYPT_INFO *info, DWORD i,
1604 CRYPT_DATA_BLOB *key)
1606 static HCRYPTOIDFUNCSET set = NULL;
1607 PFN_CMSG_EXPORT_KEY_TRANS exportKeyFunc = NULL;
1608 HCRYPTOIDFUNCADDR hFunc = NULL;
1609 CMSG_KEY_TRANS_RECIPIENT_ENCODE_INFO *encodeInfo =
1610 info->rgCmsRecipients[i].u.pKeyTrans;
1611 CMSG_KEY_TRANS_ENCRYPT_INFO encryptInfo;
1614 memset(&encryptInfo, 0, sizeof(encryptInfo));
1615 encryptInfo.cbSize = sizeof(encryptInfo);
1616 encryptInfo.dwRecipientIndex = i;
1617 ret = CRYPT_ConstructAlgorithmId(&encryptInfo.KeyEncryptionAlgorithm,
1618 &encodeInfo->KeyEncryptionAlgorithm);
1621 set = CryptInitOIDFunctionSet(CMSG_OID_EXPORT_KEY_TRANS_FUNC, 0);
1622 CryptGetOIDFunctionAddress(set, X509_ASN_ENCODING,
1623 encryptInfo.KeyEncryptionAlgorithm.pszObjId, 0, (void **)&exportKeyFunc,
1626 exportKeyFunc = CRYPT_ExportKeyTrans;
1629 ret = exportKeyFunc(info, encodeInfo, &encryptInfo, 0, NULL);
1632 key->cbData = encryptInfo.EncryptedKey.cbData;
1633 key->pbData = encryptInfo.EncryptedKey.pbData;
1637 CryptFreeOIDFunctionAddress(hFunc, 0);
1639 CryptMemFree(encryptInfo.KeyEncryptionAlgorithm.pszObjId);
1640 CryptMemFree(encryptInfo.KeyEncryptionAlgorithm.Parameters.pbData);
1644 static LPVOID WINAPI mem_alloc(size_t size)
1646 return HeapAlloc(GetProcessHeap(), 0, size);
1649 static VOID WINAPI mem_free(LPVOID pv)
1651 HeapFree(GetProcessHeap(), 0, pv);
1655 static BOOL CContentEncryptInfo_Construct(CMSG_CONTENT_ENCRYPT_INFO *info,
1656 const CMSG_ENVELOPED_ENCODE_INFO_WITH_CMS *in, HCRYPTPROV prov)
1660 info->cbSize = sizeof(CMSG_CONTENT_ENCRYPT_INFO);
1661 info->hCryptProv = prov;
1662 ret = CRYPT_ConstructAlgorithmId(&info->ContentEncryptionAlgorithm,
1663 &in->ContentEncryptionAlgorithm);
1664 info->pvEncryptionAuxInfo = in->pvEncryptionAuxInfo;
1665 info->cRecipients = in->cRecipients;
1668 info->rgCmsRecipients = CryptMemAlloc(in->cRecipients *
1669 sizeof(CMSG_RECIPIENT_ENCODE_INFO));
1670 if (info->rgCmsRecipients)
1674 for (i = 0; ret && i < in->cRecipients; ++i)
1676 CMSG_KEY_TRANS_RECIPIENT_ENCODE_INFO *encodeInfo;
1677 CERT_INFO *cert = in->rgpRecipientCert[i];
1679 info->rgCmsRecipients[i].dwRecipientChoice =
1680 CMSG_KEY_TRANS_RECIPIENT;
1681 encodeInfo = CryptMemAlloc(sizeof(*encodeInfo));
1682 info->rgCmsRecipients[i].u.pKeyTrans = encodeInfo;
1685 encodeInfo->cbSize = sizeof(*encodeInfo);
1686 ret = CRYPT_ConstructAlgorithmId(
1687 &encodeInfo->KeyEncryptionAlgorithm,
1688 &cert->SubjectPublicKeyInfo.Algorithm);
1689 encodeInfo->pvKeyEncryptionAuxInfo = NULL;
1690 encodeInfo->hCryptProv = prov;
1692 ret = CRYPT_ConstructBitBlob(
1693 &encodeInfo->RecipientPublicKey,
1694 &cert->SubjectPublicKeyInfo.PublicKey);
1696 ret = CRYPT_ConstructBlob(
1697 &encodeInfo->RecipientId.u.IssuerSerialNumber.Issuer,
1700 ret = CRYPT_ConstructBlob(
1701 &encodeInfo->RecipientId.u.IssuerSerialNumber.SerialNumber,
1702 &cert->SerialNumber);
1711 info->pfnAlloc = mem_alloc;
1712 info->pfnFree = mem_free;
1716 static void CContentEncryptInfo_Free(CMSG_CONTENT_ENCRYPT_INFO *info)
1718 CryptMemFree(info->ContentEncryptionAlgorithm.pszObjId);
1719 CryptMemFree(info->ContentEncryptionAlgorithm.Parameters.pbData);
1720 if (info->rgCmsRecipients)
1724 for (i = 0; i < info->cRecipients; ++i)
1726 CMSG_KEY_TRANS_RECIPIENT_ENCODE_INFO *encodeInfo =
1727 info->rgCmsRecipients[i].u.pKeyTrans;
1729 CryptMemFree(encodeInfo->KeyEncryptionAlgorithm.pszObjId);
1730 CryptMemFree(encodeInfo->KeyEncryptionAlgorithm.Parameters.pbData);
1731 CryptMemFree(encodeInfo->RecipientPublicKey.pbData);
1733 encodeInfo->RecipientId.u.IssuerSerialNumber.Issuer.pbData);
1735 encodeInfo->RecipientId.u.IssuerSerialNumber.SerialNumber.pbData);
1736 CryptMemFree(encodeInfo);
1738 CryptMemFree(info->rgCmsRecipients);
1742 static BOOL CRecipientInfo_Construct(CMSG_KEY_TRANS_RECIPIENT_INFO *info,
1743 const CERT_INFO *cert, CRYPT_DATA_BLOB *key)
1747 info->dwVersion = CMSG_KEY_TRANS_PKCS_1_5_VERSION;
1748 info->RecipientId.dwIdChoice = CERT_ID_ISSUER_SERIAL_NUMBER;
1749 ret = CRYPT_ConstructBlob(&info->RecipientId.u.IssuerSerialNumber.Issuer,
1752 ret = CRYPT_ConstructBlob(
1753 &info->RecipientId.u.IssuerSerialNumber.SerialNumber,
1754 &cert->SerialNumber);
1756 ret = CRYPT_ConstructAlgorithmId(&info->KeyEncryptionAlgorithm,
1757 &cert->SubjectPublicKeyInfo.Algorithm);
1758 info->EncryptedKey.cbData = key->cbData;
1759 info->EncryptedKey.pbData = key->pbData;
1763 static void CRecipientInfo_Free(CMSG_KEY_TRANS_RECIPIENT_INFO *info)
1765 CryptMemFree(info->RecipientId.u.IssuerSerialNumber.Issuer.pbData);
1766 CryptMemFree(info->RecipientId.u.IssuerSerialNumber.SerialNumber.pbData);
1767 CryptMemFree(info->KeyEncryptionAlgorithm.pszObjId);
1768 CryptMemFree(info->KeyEncryptionAlgorithm.Parameters.pbData);
1769 CryptMemFree(info->EncryptedKey.pbData);
1772 static void CEnvelopedEncodeMsg_Close(HCRYPTMSG hCryptMsg)
1774 CEnvelopedEncodeMsg *msg = hCryptMsg;
1776 CryptMemFree(msg->algo.pszObjId);
1777 CryptMemFree(msg->algo.Parameters.pbData);
1778 if (msg->base.open_flags & CMSG_CRYPT_RELEASE_CONTEXT_FLAG)
1779 CryptReleaseContext(msg->prov, 0);
1780 CryptDestroyKey(msg->key);
1781 if (msg->recipientInfo)
1785 for (i = 0; i < msg->cRecipientInfo; ++i)
1786 CRecipientInfo_Free(&msg->recipientInfo[i]);
1787 CryptMemFree(msg->recipientInfo);
1789 CryptMemFree(msg->data.pbData);
1792 static BOOL CEnvelopedEncodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
1793 DWORD dwIndex, void *pvData, DWORD *pcbData)
1795 CEnvelopedEncodeMsg *msg = hCryptMsg;
1798 switch (dwParamType)
1800 case CMSG_BARE_CONTENT_PARAM:
1801 if (msg->base.streamed)
1802 SetLastError(E_INVALIDARG);
1805 char oid_rsa_data[] = szOID_RSA_data;
1806 CRYPT_ENVELOPED_DATA envelopedData = {
1807 CMSG_ENVELOPED_DATA_PKCS_1_5_VERSION, msg->cRecipientInfo,
1808 msg->recipientInfo, { oid_rsa_data, msg->algo, msg->data }
1811 ret = CRYPT_AsnEncodePKCSEnvelopedData(&envelopedData, pvData,
1815 case CMSG_CONTENT_PARAM:
1817 CRYPT_CONTENT_INFO info;
1819 ret = CryptMsgGetParam(hCryptMsg, CMSG_BARE_CONTENT_PARAM, 0, NULL,
1820 &info.Content.cbData);
1823 info.Content.pbData = CryptMemAlloc(info.Content.cbData);
1824 if (info.Content.pbData)
1826 ret = CryptMsgGetParam(hCryptMsg, CMSG_BARE_CONTENT_PARAM, 0,
1827 info.Content.pbData, &info.Content.cbData);
1830 char oid_rsa_enveloped[] = szOID_RSA_envelopedData;
1832 info.pszObjId = oid_rsa_enveloped;
1833 ret = CryptEncodeObjectEx(X509_ASN_ENCODING,
1834 PKCS_CONTENT_INFO, &info, 0, NULL, pvData, pcbData);
1836 CryptMemFree(info.Content.pbData);
1844 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1849 static BOOL CEnvelopedEncodeMsg_Update(HCRYPTMSG hCryptMsg, const BYTE *pbData,
1850 DWORD cbData, BOOL fFinal)
1852 CEnvelopedEncodeMsg *msg = hCryptMsg;
1855 if (msg->base.state == MsgStateFinalized)
1856 SetLastError(CRYPT_E_MSG_ERROR);
1857 else if (msg->base.streamed)
1859 FIXME("streamed stub\n");
1860 msg->base.state = fFinal ? MsgStateFinalized : MsgStateUpdated;
1867 if (msg->base.open_flags & CMSG_DETACHED_FLAG)
1868 SetLastError(E_INVALIDARG);
1870 SetLastError(CRYPT_E_MSG_ERROR);
1876 DWORD dataLen = cbData;
1878 msg->data.cbData = cbData;
1879 msg->data.pbData = CryptMemAlloc(cbData);
1880 if (msg->data.pbData)
1882 memcpy(msg->data.pbData, pbData, cbData);
1883 ret = CryptEncrypt(msg->key, 0, TRUE, 0, msg->data.pbData,
1884 &dataLen, msg->data.cbData);
1885 msg->data.cbData = dataLen;
1886 if (dataLen > cbData)
1888 msg->data.pbData = CryptMemRealloc(msg->data.pbData,
1890 if (msg->data.pbData)
1893 ret = CryptEncrypt(msg->key, 0, TRUE, 0,
1894 msg->data.pbData, &dataLen, msg->data.cbData);
1900 CryptMemFree(msg->data.pbData);
1906 msg->data.cbData = 0;
1907 msg->data.pbData = NULL;
1912 msg->base.state = MsgStateFinalized;
1918 static HCRYPTMSG CEnvelopedEncodeMsg_Open(DWORD dwFlags,
1919 const void *pvMsgEncodeInfo, LPCSTR pszInnerContentObjID,
1920 PCMSG_STREAM_INFO pStreamInfo)
1922 CEnvelopedEncodeMsg *msg;
1923 const CMSG_ENVELOPED_ENCODE_INFO_WITH_CMS *info = pvMsgEncodeInfo;
1927 if (info->cbSize != sizeof(CMSG_ENVELOPED_ENCODE_INFO) &&
1928 info->cbSize != sizeof(CMSG_ENVELOPED_ENCODE_INFO_WITH_CMS))
1930 SetLastError(E_INVALIDARG);
1933 if (info->cbSize == sizeof(CMSG_ENVELOPED_ENCODE_INFO_WITH_CMS))
1934 FIXME("CMS fields unsupported\n");
1935 if (!(algID = CertOIDToAlgId(info->ContentEncryptionAlgorithm.pszObjId)))
1937 SetLastError(CRYPT_E_UNKNOWN_ALGO);
1940 if (info->cRecipients && !info->rgpRecipientCert)
1942 SetLastError(E_INVALIDARG);
1945 if (info->hCryptProv)
1946 prov = info->hCryptProv;
1949 prov = CRYPT_GetDefaultProvider();
1950 dwFlags &= ~CMSG_CRYPT_RELEASE_CONTEXT_FLAG;
1952 msg = CryptMemAlloc(sizeof(CEnvelopedEncodeMsg));
1955 CRYPT_DATA_BLOB encryptedKey = { 0, NULL };
1956 CMSG_CONTENT_ENCRYPT_INFO encryptInfo;
1960 CryptMsgBase_Init((CryptMsgBase *)msg, dwFlags, pStreamInfo,
1961 CEnvelopedEncodeMsg_Close, CEnvelopedEncodeMsg_GetParam,
1962 CEnvelopedEncodeMsg_Update, CRYPT_DefaultMsgControl);
1963 ret = CRYPT_ConstructAlgorithmId(&msg->algo,
1964 &info->ContentEncryptionAlgorithm);
1966 msg->data.cbData = 0;
1967 msg->data.pbData = NULL;
1968 msg->cRecipientInfo = info->cRecipients;
1969 msg->recipientInfo = CryptMemAlloc(info->cRecipients *
1970 sizeof(CMSG_KEY_TRANS_RECIPIENT_INFO));
1971 if (!msg->recipientInfo)
1973 memset(&encryptInfo, 0, sizeof(encryptInfo));
1976 ret = CContentEncryptInfo_Construct(&encryptInfo, info, prov);
1979 ret = CRYPT_GenKey(&encryptInfo, algID);
1981 msg->key = encryptInfo.hContentEncryptKey;
1984 for (i = 0; ret && i < msg->cRecipientInfo; ++i)
1986 ret = CRYPT_ExportEncryptedKey(&encryptInfo, i, &encryptedKey);
1988 ret = CRecipientInfo_Construct(&msg->recipientInfo[i],
1989 info->rgpRecipientCert[i], &encryptedKey);
1991 CContentEncryptInfo_Free(&encryptInfo);
1998 if (!msg && (dwFlags & CMSG_CRYPT_RELEASE_CONTEXT_FLAG))
1999 CryptReleaseContext(prov, 0);
2003 HCRYPTMSG WINAPI CryptMsgOpenToEncode(DWORD dwMsgEncodingType, DWORD dwFlags,
2004 DWORD dwMsgType, const void *pvMsgEncodeInfo, LPSTR pszInnerContentObjID,
2005 PCMSG_STREAM_INFO pStreamInfo)
2007 HCRYPTMSG msg = NULL;
2009 TRACE("(%08x, %08x, %08x, %p, %s, %p)\n", dwMsgEncodingType, dwFlags,
2010 dwMsgType, pvMsgEncodeInfo, debugstr_a(pszInnerContentObjID), pStreamInfo);
2012 if (GET_CMSG_ENCODING_TYPE(dwMsgEncodingType) != PKCS_7_ASN_ENCODING)
2014 SetLastError(E_INVALIDARG);
2020 msg = CDataEncodeMsg_Open(dwFlags, pvMsgEncodeInfo,
2021 pszInnerContentObjID, pStreamInfo);
2024 msg = CHashEncodeMsg_Open(dwFlags, pvMsgEncodeInfo,
2025 pszInnerContentObjID, pStreamInfo);
2028 msg = CSignedEncodeMsg_Open(dwFlags, pvMsgEncodeInfo,
2029 pszInnerContentObjID, pStreamInfo);
2031 case CMSG_ENVELOPED:
2032 msg = CEnvelopedEncodeMsg_Open(dwFlags, pvMsgEncodeInfo,
2033 pszInnerContentObjID, pStreamInfo);
2035 case CMSG_SIGNED_AND_ENVELOPED:
2036 case CMSG_ENCRYPTED:
2037 /* defined but invalid, fall through */
2039 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2044 typedef struct _CEnvelopedDecodeMsg
2046 CRYPT_ENVELOPED_DATA *data;
2047 HCRYPTPROV crypt_prov;
2048 CRYPT_DATA_BLOB content;
2050 } CEnvelopedDecodeMsg;
2052 typedef struct _CDecodeMsg
2056 HCRYPTPROV crypt_prov;
2059 CSignedMsgData signed_data;
2060 CEnvelopedDecodeMsg enveloped_data;
2062 CRYPT_DATA_BLOB msg_data;
2063 CRYPT_DATA_BLOB detached_data;
2064 PCONTEXT_PROPERTY_LIST properties;
2067 static void CDecodeMsg_Close(HCRYPTMSG hCryptMsg)
2069 CDecodeMsg *msg = hCryptMsg;
2071 if (msg->base.open_flags & CMSG_CRYPT_RELEASE_CONTEXT_FLAG)
2072 CryptReleaseContext(msg->crypt_prov, 0);
2077 CryptDestroyHash(msg->u.hash);
2079 case CMSG_ENVELOPED:
2080 if (msg->u.enveloped_data.crypt_prov)
2081 CryptReleaseContext(msg->u.enveloped_data.crypt_prov, 0);
2082 LocalFree(msg->u.enveloped_data.data);
2083 CryptMemFree(msg->u.enveloped_data.content.pbData);
2086 if (msg->u.signed_data.info)
2088 LocalFree(msg->u.signed_data.info);
2089 CSignedMsgData_CloseHandles(&msg->u.signed_data);
2093 CryptMemFree(msg->msg_data.pbData);
2094 CryptMemFree(msg->detached_data.pbData);
2095 ContextPropertyList_Free(msg->properties);
2098 static BOOL CDecodeMsg_CopyData(CRYPT_DATA_BLOB *blob, const BYTE *pbData,
2106 blob->pbData = CryptMemRealloc(blob->pbData,
2107 blob->cbData + cbData);
2109 blob->pbData = CryptMemAlloc(cbData);
2112 memcpy(blob->pbData + blob->cbData, pbData, cbData);
2113 blob->cbData += cbData;
2121 static BOOL CDecodeMsg_DecodeDataContent(CDecodeMsg *msg, const CRYPT_DER_BLOB *blob)
2124 CRYPT_DATA_BLOB *data;
2127 ret = CryptDecodeObjectEx(X509_ASN_ENCODING, X509_OCTET_STRING,
2128 blob->pbData, blob->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, &data, &size);
2131 ret = ContextPropertyList_SetProperty(msg->properties,
2132 CMSG_CONTENT_PARAM, data->pbData, data->cbData);
2138 static void CDecodeMsg_SaveAlgorithmID(CDecodeMsg *msg, DWORD param,
2139 const CRYPT_ALGORITHM_IDENTIFIER *id)
2141 static const BYTE nullParams[] = { ASN_NULL, 0 };
2142 CRYPT_ALGORITHM_IDENTIFIER *copy;
2143 DWORD len = sizeof(CRYPT_ALGORITHM_IDENTIFIER);
2145 /* Linearize algorithm id */
2146 len += strlen(id->pszObjId) + 1;
2147 len += id->Parameters.cbData;
2148 copy = CryptMemAlloc(len);
2152 (LPSTR)((BYTE *)copy + sizeof(CRYPT_ALGORITHM_IDENTIFIER));
2153 strcpy(copy->pszObjId, id->pszObjId);
2154 copy->Parameters.pbData = (BYTE *)copy->pszObjId + strlen(id->pszObjId)
2156 /* Trick: omit NULL parameters */
2157 if (id->Parameters.cbData == sizeof(nullParams) &&
2158 !memcmp(id->Parameters.pbData, nullParams, sizeof(nullParams)))
2160 copy->Parameters.cbData = 0;
2161 len -= sizeof(nullParams);
2164 copy->Parameters.cbData = id->Parameters.cbData;
2165 if (copy->Parameters.cbData)
2166 memcpy(copy->Parameters.pbData, id->Parameters.pbData,
2167 id->Parameters.cbData);
2168 ContextPropertyList_SetProperty(msg->properties, param, (BYTE *)copy,
2174 static inline void CRYPT_FixUpAlgorithmID(CRYPT_ALGORITHM_IDENTIFIER *id)
2176 id->pszObjId = (LPSTR)((BYTE *)id + sizeof(CRYPT_ALGORITHM_IDENTIFIER));
2177 id->Parameters.pbData = (BYTE *)id->pszObjId + strlen(id->pszObjId) + 1;
2180 static BOOL CDecodeMsg_DecodeHashedContent(CDecodeMsg *msg,
2181 const CRYPT_DER_BLOB *blob)
2184 CRYPT_DIGESTED_DATA *digestedData;
2187 ret = CRYPT_AsnDecodePKCSDigestedData(blob->pbData, blob->cbData,
2188 CRYPT_DECODE_ALLOC_FLAG, NULL, (CRYPT_DIGESTED_DATA *)&digestedData,
2192 ContextPropertyList_SetProperty(msg->properties, CMSG_VERSION_PARAM,
2193 (const BYTE *)&digestedData->version, sizeof(digestedData->version));
2194 CDecodeMsg_SaveAlgorithmID(msg, CMSG_HASH_ALGORITHM_PARAM,
2195 &digestedData->DigestAlgorithm);
2196 ContextPropertyList_SetProperty(msg->properties,
2197 CMSG_INNER_CONTENT_TYPE_PARAM,
2198 (const BYTE *)digestedData->ContentInfo.pszObjId,
2199 digestedData->ContentInfo.pszObjId ?
2200 strlen(digestedData->ContentInfo.pszObjId) + 1 : 0);
2201 if (!(msg->base.open_flags & CMSG_DETACHED_FLAG))
2203 if (digestedData->ContentInfo.Content.cbData)
2204 CDecodeMsg_DecodeDataContent(msg,
2205 &digestedData->ContentInfo.Content);
2207 ContextPropertyList_SetProperty(msg->properties,
2208 CMSG_CONTENT_PARAM, NULL, 0);
2210 ContextPropertyList_SetProperty(msg->properties, CMSG_HASH_DATA_PARAM,
2211 digestedData->hash.pbData, digestedData->hash.cbData);
2212 LocalFree(digestedData);
2217 static BOOL CDecodeMsg_DecodeEnvelopedContent(CDecodeMsg *msg,
2218 const CRYPT_DER_BLOB *blob)
2221 CRYPT_ENVELOPED_DATA *envelopedData;
2224 ret = CRYPT_AsnDecodePKCSEnvelopedData(blob->pbData, blob->cbData,
2225 CRYPT_DECODE_ALLOC_FLAG, NULL, (CRYPT_ENVELOPED_DATA *)&envelopedData,
2228 msg->u.enveloped_data.data = envelopedData;
2232 static BOOL CDecodeMsg_DecodeSignedContent(CDecodeMsg *msg,
2233 const CRYPT_DER_BLOB *blob)
2236 CRYPT_SIGNED_INFO *signedInfo;
2239 ret = CRYPT_AsnDecodeCMSSignedInfo(blob->pbData, blob->cbData,
2240 CRYPT_DECODE_ALLOC_FLAG, NULL, (CRYPT_SIGNED_INFO *)&signedInfo,
2243 msg->u.signed_data.info = signedInfo;
2247 /* Decodes the content in blob as the type given, and updates the value
2248 * (type, parameters, etc.) of msg based on what blob contains.
2249 * It doesn't just use msg's type, to allow a recursive call from an implicitly
2250 * typed message once the outer content info has been decoded.
2252 static BOOL CDecodeMsg_DecodeContent(CDecodeMsg *msg, const CRYPT_DER_BLOB *blob,
2260 if ((ret = CDecodeMsg_DecodeDataContent(msg, blob)))
2261 msg->type = CMSG_DATA;
2264 if ((ret = CDecodeMsg_DecodeHashedContent(msg, blob)))
2265 msg->type = CMSG_HASHED;
2267 case CMSG_ENVELOPED:
2268 if ((ret = CDecodeMsg_DecodeEnvelopedContent(msg, blob)))
2269 msg->type = CMSG_ENVELOPED;
2272 if ((ret = CDecodeMsg_DecodeSignedContent(msg, blob)))
2273 msg->type = CMSG_SIGNED;
2277 CRYPT_CONTENT_INFO *info;
2280 ret = CryptDecodeObjectEx(X509_ASN_ENCODING, PKCS_CONTENT_INFO,
2281 msg->msg_data.pbData, msg->msg_data.cbData, CRYPT_DECODE_ALLOC_FLAG,
2282 NULL, &info, &size);
2285 if (!strcmp(info->pszObjId, szOID_RSA_data))
2286 ret = CDecodeMsg_DecodeContent(msg, &info->Content, CMSG_DATA);
2287 else if (!strcmp(info->pszObjId, szOID_RSA_digestedData))
2288 ret = CDecodeMsg_DecodeContent(msg, &info->Content,
2290 else if (!strcmp(info->pszObjId, szOID_RSA_envelopedData))
2291 ret = CDecodeMsg_DecodeContent(msg, &info->Content,
2293 else if (!strcmp(info->pszObjId, szOID_RSA_signedData))
2294 ret = CDecodeMsg_DecodeContent(msg, &info->Content,
2298 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2308 static BOOL CDecodeMsg_FinalizeHashedContent(CDecodeMsg *msg,
2309 CRYPT_DER_BLOB *blob)
2311 CRYPT_ALGORITHM_IDENTIFIER *hashAlgoID = NULL;
2316 CryptMsgGetParam(msg, CMSG_HASH_ALGORITHM_PARAM, 0, NULL, &size);
2317 hashAlgoID = CryptMemAlloc(size);
2318 ret = CryptMsgGetParam(msg, CMSG_HASH_ALGORITHM_PARAM, 0, hashAlgoID,
2321 algID = CertOIDToAlgId(hashAlgoID->pszObjId);
2322 ret = CryptCreateHash(msg->crypt_prov, algID, 0, 0, &msg->u.hash);
2325 CRYPT_DATA_BLOB content;
2327 if (msg->base.open_flags & CMSG_DETACHED_FLAG)
2329 /* Unlike for non-detached messages, the data were never stored as
2330 * the content param, but were saved in msg->detached_data instead.
2332 content.pbData = msg->detached_data.pbData;
2333 content.cbData = msg->detached_data.cbData;
2336 ret = ContextPropertyList_FindProperty(msg->properties,
2337 CMSG_CONTENT_PARAM, &content);
2339 ret = CryptHashData(msg->u.hash, content.pbData, content.cbData, 0);
2341 CryptMemFree(hashAlgoID);
2345 static BOOL CDecodeMsg_FinalizeEnvelopedContent(CDecodeMsg *msg,
2346 CRYPT_DER_BLOB *blob)
2348 CRYPT_DATA_BLOB *content;
2350 if (msg->base.open_flags & CMSG_DETACHED_FLAG)
2351 content = &msg->detached_data;
2354 &msg->u.enveloped_data.data->encryptedContentInfo.encryptedContent;
2356 return CRYPT_ConstructBlob(&msg->u.enveloped_data.content, content);
2359 static BOOL CDecodeMsg_FinalizeSignedContent(CDecodeMsg *msg,
2360 CRYPT_DER_BLOB *blob)
2365 ret = CSignedMsgData_AllocateHandles(&msg->u.signed_data);
2366 for (i = 0; ret && i < msg->u.signed_data.info->cSignerInfo; i++)
2367 ret = CSignedMsgData_ConstructSignerHandles(&msg->u.signed_data, i,
2371 CRYPT_DATA_BLOB *content;
2373 /* Now that we have all the content, update the hash handles with
2374 * it. If the message is a detached message, the content is stored
2375 * in msg->detached_data rather than in the signed message's
2378 if (msg->base.open_flags & CMSG_DETACHED_FLAG)
2379 content = &msg->detached_data;
2381 content = &msg->u.signed_data.info->content.Content;
2382 if (content->cbData)
2384 /* If the message is not detached, have to decode the message's
2385 * content if the type is szOID_RSA_data.
2387 if (!(msg->base.open_flags & CMSG_DETACHED_FLAG) &&
2388 !strcmp(msg->u.signed_data.info->content.pszObjId,
2391 CRYPT_DATA_BLOB *blob;
2393 ret = CryptDecodeObjectEx(X509_ASN_ENCODING,
2394 X509_OCTET_STRING, content->pbData, content->cbData,
2395 CRYPT_DECODE_ALLOC_FLAG, NULL, &blob, &size);
2398 ret = CSignedMsgData_Update(&msg->u.signed_data,
2399 blob->pbData, blob->cbData, TRUE, Verify);
2404 ret = CSignedMsgData_Update(&msg->u.signed_data,
2405 content->pbData, content->cbData, TRUE, Verify);
2411 static BOOL CDecodeMsg_FinalizeContent(CDecodeMsg *msg, CRYPT_DER_BLOB *blob)
2418 ret = CDecodeMsg_FinalizeHashedContent(msg, blob);
2420 case CMSG_ENVELOPED:
2421 ret = CDecodeMsg_FinalizeEnvelopedContent(msg, blob);
2424 ret = CDecodeMsg_FinalizeSignedContent(msg, blob);
2432 static BOOL CDecodeMsg_Update(HCRYPTMSG hCryptMsg, const BYTE *pbData,
2433 DWORD cbData, BOOL fFinal)
2435 CDecodeMsg *msg = hCryptMsg;
2438 TRACE("(%p, %p, %d, %d)\n", hCryptMsg, pbData, cbData, fFinal);
2440 if (msg->base.state == MsgStateFinalized)
2441 SetLastError(CRYPT_E_MSG_ERROR);
2442 else if (msg->base.streamed)
2444 FIXME("(%p, %p, %d, %d): streamed update stub\n", hCryptMsg, pbData,
2446 switch (msg->base.state)
2449 ret = CDecodeMsg_CopyData(&msg->msg_data, pbData, cbData);
2452 if (msg->base.open_flags & CMSG_DETACHED_FLAG)
2453 msg->base.state = MsgStateDataFinalized;
2455 msg->base.state = MsgStateFinalized;
2458 msg->base.state = MsgStateUpdated;
2460 case MsgStateUpdated:
2461 ret = CDecodeMsg_CopyData(&msg->msg_data, pbData, cbData);
2464 if (msg->base.open_flags & CMSG_DETACHED_FLAG)
2465 msg->base.state = MsgStateDataFinalized;
2467 msg->base.state = MsgStateFinalized;
2470 case MsgStateDataFinalized:
2471 ret = CDecodeMsg_CopyData(&msg->detached_data, pbData, cbData);
2473 msg->base.state = MsgStateFinalized;
2476 SetLastError(CRYPT_E_MSG_ERROR);
2483 SetLastError(CRYPT_E_MSG_ERROR);
2486 switch (msg->base.state)
2489 ret = CDecodeMsg_CopyData(&msg->msg_data, pbData, cbData);
2490 if (msg->base.open_flags & CMSG_DETACHED_FLAG)
2491 msg->base.state = MsgStateDataFinalized;
2493 msg->base.state = MsgStateFinalized;
2495 case MsgStateDataFinalized:
2496 ret = CDecodeMsg_CopyData(&msg->detached_data, pbData, cbData);
2497 msg->base.state = MsgStateFinalized;
2500 SetLastError(CRYPT_E_MSG_ERROR);
2504 if (ret && fFinal &&
2505 ((msg->base.open_flags & CMSG_DETACHED_FLAG && msg->base.state ==
2506 MsgStateDataFinalized) ||
2507 (!(msg->base.open_flags & CMSG_DETACHED_FLAG) && msg->base.state ==
2508 MsgStateFinalized)))
2509 ret = CDecodeMsg_DecodeContent(msg, &msg->msg_data, msg->type);
2510 if (ret && msg->base.state == MsgStateFinalized)
2511 ret = CDecodeMsg_FinalizeContent(msg, &msg->msg_data);
2515 static BOOL CDecodeHashMsg_GetParam(CDecodeMsg *msg, DWORD dwParamType,
2516 DWORD dwIndex, void *pvData, DWORD *pcbData)
2520 switch (dwParamType)
2522 case CMSG_TYPE_PARAM:
2523 ret = CRYPT_CopyParam(pvData, pcbData, &msg->type, sizeof(msg->type));
2525 case CMSG_HASH_ALGORITHM_PARAM:
2527 CRYPT_DATA_BLOB blob;
2529 ret = ContextPropertyList_FindProperty(msg->properties, dwParamType,
2533 ret = CRYPT_CopyParam(pvData, pcbData, blob.pbData, blob.cbData);
2535 CRYPT_FixUpAlgorithmID(pvData);
2538 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2541 case CMSG_COMPUTED_HASH_PARAM:
2542 ret = CryptGetHashParam(msg->u.hash, HP_HASHVAL, pvData, pcbData, 0);
2546 CRYPT_DATA_BLOB blob;
2548 ret = ContextPropertyList_FindProperty(msg->properties, dwParamType,
2551 ret = CRYPT_CopyParam(pvData, pcbData, blob.pbData, blob.cbData);
2553 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2559 /* nextData is an in/out parameter - on input it's the memory location in
2560 * which a copy of in's data should be made, and on output it's the memory
2561 * location immediately after out's copy of in's data.
2563 static inline void CRYPT_CopyBlob(CRYPT_DATA_BLOB *out,
2564 const CRYPT_DATA_BLOB *in, LPBYTE *nextData)
2566 out->cbData = in->cbData;
2569 out->pbData = *nextData;
2570 memcpy(out->pbData, in->pbData, in->cbData);
2571 *nextData += in->cbData;
2575 static inline void CRYPT_CopyAlgorithmId(CRYPT_ALGORITHM_IDENTIFIER *out,
2576 const CRYPT_ALGORITHM_IDENTIFIER *in, LPBYTE *nextData)
2580 out->pszObjId = (LPSTR)*nextData;
2581 strcpy(out->pszObjId, in->pszObjId);
2582 *nextData += strlen(out->pszObjId) + 1;
2584 CRYPT_CopyBlob(&out->Parameters, &in->Parameters, nextData);
2587 static inline void CRYPT_CopyAttributes(CRYPT_ATTRIBUTES *out,
2588 const CRYPT_ATTRIBUTES *in, LPBYTE *nextData)
2590 out->cAttr = in->cAttr;
2595 *nextData = POINTER_ALIGN_DWORD_PTR(*nextData);
2596 out->rgAttr = (CRYPT_ATTRIBUTE *)*nextData;
2597 *nextData += in->cAttr * sizeof(CRYPT_ATTRIBUTE);
2598 for (i = 0; i < in->cAttr; i++)
2600 if (in->rgAttr[i].pszObjId)
2602 out->rgAttr[i].pszObjId = (LPSTR)*nextData;
2603 strcpy(out->rgAttr[i].pszObjId, in->rgAttr[i].pszObjId);
2604 *nextData += strlen(in->rgAttr[i].pszObjId) + 1;
2606 if (in->rgAttr[i].cValue)
2610 out->rgAttr[i].cValue = in->rgAttr[i].cValue;
2611 *nextData = POINTER_ALIGN_DWORD_PTR(*nextData);
2612 out->rgAttr[i].rgValue = (PCRYPT_DATA_BLOB)*nextData;
2613 *nextData += in->rgAttr[i].cValue * sizeof(CRYPT_DATA_BLOB);
2614 for (j = 0; j < in->rgAttr[i].cValue; j++)
2615 CRYPT_CopyBlob(&out->rgAttr[i].rgValue[j],
2616 &in->rgAttr[i].rgValue[j], nextData);
2622 static DWORD CRYPT_SizeOfAttributes(const CRYPT_ATTRIBUTES *attr)
2624 DWORD size = attr->cAttr * sizeof(CRYPT_ATTRIBUTE), i, j;
2626 for (i = 0; i < attr->cAttr; i++)
2628 if (attr->rgAttr[i].pszObjId)
2629 size += strlen(attr->rgAttr[i].pszObjId) + 1;
2631 size = ALIGN_DWORD_PTR(size);
2632 size += attr->rgAttr[i].cValue * sizeof(CRYPT_DATA_BLOB);
2633 for (j = 0; j < attr->rgAttr[i].cValue; j++)
2634 size += attr->rgAttr[i].rgValue[j].cbData;
2636 /* align pointer again to be conservative */
2637 size = ALIGN_DWORD_PTR(size);
2641 static DWORD CRYPT_SizeOfKeyIdAsIssuerAndSerial(const CRYPT_DATA_BLOB *keyId)
2643 static char oid_key_rdn[] = szOID_KEYID_RDN;
2646 CERT_RDN rdn = { 1, &attr };
2647 CERT_NAME_INFO name = { 1, &rdn };
2649 attr.pszObjId = oid_key_rdn;
2650 attr.dwValueType = CERT_RDN_OCTET_STRING;
2651 attr.Value.cbData = keyId->cbData;
2652 attr.Value.pbData = keyId->pbData;
2653 if (CryptEncodeObject(X509_ASN_ENCODING, X509_NAME, &name, NULL, &size))
2654 size++; /* Only include size of special zero serial number on success */
2658 static BOOL CRYPT_CopyKeyIdAsIssuerAndSerial(CERT_NAME_BLOB *issuer,
2659 CRYPT_INTEGER_BLOB *serialNumber, const CRYPT_DATA_BLOB *keyId, DWORD encodedLen,
2662 static char oid_key_rdn[] = szOID_KEYID_RDN;
2664 CERT_RDN rdn = { 1, &attr };
2665 CERT_NAME_INFO name = { 1, &rdn };
2668 /* Encode special zero serial number */
2669 serialNumber->cbData = 1;
2670 serialNumber->pbData = *nextData;
2674 issuer->pbData = *nextData;
2675 attr.pszObjId = oid_key_rdn;
2676 attr.dwValueType = CERT_RDN_OCTET_STRING;
2677 attr.Value.cbData = keyId->cbData;
2678 attr.Value.pbData = keyId->pbData;
2679 ret = CryptEncodeObject(X509_ASN_ENCODING, X509_NAME, &name, *nextData,
2683 *nextData += encodedLen;
2684 issuer->cbData = encodedLen;
2689 static BOOL CRYPT_CopySignerInfo(void *pvData, DWORD *pcbData,
2690 const CMSG_CMS_SIGNER_INFO *in)
2692 DWORD size = sizeof(CMSG_SIGNER_INFO), rdnSize = 0;
2695 TRACE("(%p, %d, %p)\n", pvData, pvData ? *pcbData : 0, in);
2697 if (in->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
2699 size += in->SignerId.u.IssuerSerialNumber.Issuer.cbData;
2700 size += in->SignerId.u.IssuerSerialNumber.SerialNumber.cbData;
2704 rdnSize = CRYPT_SizeOfKeyIdAsIssuerAndSerial(&in->SignerId.u.KeyId);
2707 if (in->HashAlgorithm.pszObjId)
2708 size += strlen(in->HashAlgorithm.pszObjId) + 1;
2709 size += in->HashAlgorithm.Parameters.cbData;
2710 if (in->HashEncryptionAlgorithm.pszObjId)
2711 size += strlen(in->HashEncryptionAlgorithm.pszObjId) + 1;
2712 size += in->HashEncryptionAlgorithm.Parameters.cbData;
2713 size += in->EncryptedHash.cbData;
2715 size = ALIGN_DWORD_PTR(size);
2716 size += CRYPT_SizeOfAttributes(&in->AuthAttrs);
2717 size += CRYPT_SizeOfAttributes(&in->UnauthAttrs);
2723 else if (*pcbData < size)
2726 SetLastError(ERROR_MORE_DATA);
2731 LPBYTE nextData = (BYTE *)pvData + sizeof(CMSG_SIGNER_INFO);
2732 CMSG_SIGNER_INFO *out = pvData;
2735 out->dwVersion = in->dwVersion;
2736 if (in->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
2738 CRYPT_CopyBlob(&out->Issuer,
2739 &in->SignerId.u.IssuerSerialNumber.Issuer, &nextData);
2740 CRYPT_CopyBlob(&out->SerialNumber,
2741 &in->SignerId.u.IssuerSerialNumber.SerialNumber, &nextData);
2744 ret = CRYPT_CopyKeyIdAsIssuerAndSerial(&out->Issuer, &out->SerialNumber,
2745 &in->SignerId.u.KeyId, rdnSize, &nextData);
2748 CRYPT_CopyAlgorithmId(&out->HashAlgorithm, &in->HashAlgorithm,
2750 CRYPT_CopyAlgorithmId(&out->HashEncryptionAlgorithm,
2751 &in->HashEncryptionAlgorithm, &nextData);
2752 CRYPT_CopyBlob(&out->EncryptedHash, &in->EncryptedHash, &nextData);
2753 nextData = POINTER_ALIGN_DWORD_PTR(nextData);
2754 CRYPT_CopyAttributes(&out->AuthAttrs, &in->AuthAttrs, &nextData);
2755 CRYPT_CopyAttributes(&out->UnauthAttrs, &in->UnauthAttrs, &nextData);
2758 TRACE("returning %d\n", ret);
2762 static BOOL CRYPT_CopyCMSSignerInfo(void *pvData, DWORD *pcbData,
2763 const CMSG_CMS_SIGNER_INFO *in)
2765 DWORD size = sizeof(CMSG_CMS_SIGNER_INFO);
2768 TRACE("(%p, %d, %p)\n", pvData, pvData ? *pcbData : 0, in);
2770 if (in->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
2772 size += in->SignerId.u.IssuerSerialNumber.Issuer.cbData;
2773 size += in->SignerId.u.IssuerSerialNumber.SerialNumber.cbData;
2776 size += in->SignerId.u.KeyId.cbData;
2777 if (in->HashAlgorithm.pszObjId)
2778 size += strlen(in->HashAlgorithm.pszObjId) + 1;
2779 size += in->HashAlgorithm.Parameters.cbData;
2780 if (in->HashEncryptionAlgorithm.pszObjId)
2781 size += strlen(in->HashEncryptionAlgorithm.pszObjId) + 1;
2782 size += in->HashEncryptionAlgorithm.Parameters.cbData;
2783 size += in->EncryptedHash.cbData;
2785 size = ALIGN_DWORD_PTR(size);
2786 size += CRYPT_SizeOfAttributes(&in->AuthAttrs);
2787 size += CRYPT_SizeOfAttributes(&in->UnauthAttrs);
2793 else if (*pcbData < size)
2796 SetLastError(ERROR_MORE_DATA);
2801 LPBYTE nextData = (BYTE *)pvData + sizeof(CMSG_CMS_SIGNER_INFO);
2802 CMSG_CMS_SIGNER_INFO *out = pvData;
2804 out->dwVersion = in->dwVersion;
2805 out->SignerId.dwIdChoice = in->SignerId.dwIdChoice;
2806 if (in->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
2808 CRYPT_CopyBlob(&out->SignerId.u.IssuerSerialNumber.Issuer,
2809 &in->SignerId.u.IssuerSerialNumber.Issuer, &nextData);
2810 CRYPT_CopyBlob(&out->SignerId.u.IssuerSerialNumber.SerialNumber,
2811 &in->SignerId.u.IssuerSerialNumber.SerialNumber, &nextData);
2814 CRYPT_CopyBlob(&out->SignerId.u.KeyId, &in->SignerId.u.KeyId, &nextData);
2815 CRYPT_CopyAlgorithmId(&out->HashAlgorithm, &in->HashAlgorithm,
2817 CRYPT_CopyAlgorithmId(&out->HashEncryptionAlgorithm,
2818 &in->HashEncryptionAlgorithm, &nextData);
2819 CRYPT_CopyBlob(&out->EncryptedHash, &in->EncryptedHash, &nextData);
2820 nextData = POINTER_ALIGN_DWORD_PTR(nextData);
2821 CRYPT_CopyAttributes(&out->AuthAttrs, &in->AuthAttrs, &nextData);
2822 CRYPT_CopyAttributes(&out->UnauthAttrs, &in->UnauthAttrs, &nextData);
2825 TRACE("returning %d\n", ret);
2829 static BOOL CRYPT_CopySignerCertInfo(void *pvData, DWORD *pcbData,
2830 const CMSG_CMS_SIGNER_INFO *in)
2832 DWORD size = sizeof(CERT_INFO), rdnSize = 0;
2835 TRACE("(%p, %d, %p)\n", pvData, pvData ? *pcbData : 0, in);
2837 if (in->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
2839 size += in->SignerId.u.IssuerSerialNumber.Issuer.cbData;
2840 size += in->SignerId.u.IssuerSerialNumber.SerialNumber.cbData;
2844 rdnSize = CRYPT_SizeOfKeyIdAsIssuerAndSerial(&in->SignerId.u.KeyId);
2852 else if (*pcbData < size)
2855 SetLastError(ERROR_MORE_DATA);
2860 LPBYTE nextData = (BYTE *)pvData + sizeof(CERT_INFO);
2861 CERT_INFO *out = pvData;
2863 memset(out, 0, sizeof(CERT_INFO));
2864 if (in->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
2866 CRYPT_CopyBlob(&out->Issuer,
2867 &in->SignerId.u.IssuerSerialNumber.Issuer, &nextData);
2868 CRYPT_CopyBlob(&out->SerialNumber,
2869 &in->SignerId.u.IssuerSerialNumber.SerialNumber, &nextData);
2873 ret = CRYPT_CopyKeyIdAsIssuerAndSerial(&out->Issuer, &out->SerialNumber,
2874 &in->SignerId.u.KeyId, rdnSize, &nextData);
2876 TRACE("returning %d\n", ret);
2880 static BOOL CRYPT_CopyRecipientInfo(void *pvData, DWORD *pcbData,
2881 const CERT_ISSUER_SERIAL_NUMBER *in)
2883 DWORD size = sizeof(CERT_INFO);
2886 TRACE("(%p, %d, %p)\n", pvData, pvData ? *pcbData : 0, in);
2888 size += in->SerialNumber.cbData;
2889 size += in->Issuer.cbData;
2895 else if (*pcbData < size)
2898 SetLastError(ERROR_MORE_DATA);
2903 LPBYTE nextData = (BYTE *)pvData + sizeof(CERT_INFO);
2904 CERT_INFO *out = pvData;
2906 CRYPT_CopyBlob(&out->SerialNumber, &in->SerialNumber, &nextData);
2907 CRYPT_CopyBlob(&out->Issuer, &in->Issuer, &nextData);
2910 TRACE("returning %d\n", ret);
2914 static BOOL CDecodeEnvelopedMsg_GetParam(CDecodeMsg *msg, DWORD dwParamType,
2915 DWORD dwIndex, void *pvData, DWORD *pcbData)
2919 switch (dwParamType)
2921 case CMSG_TYPE_PARAM:
2922 ret = CRYPT_CopyParam(pvData, pcbData, &msg->type, sizeof(msg->type));
2924 case CMSG_CONTENT_PARAM:
2925 if (msg->u.enveloped_data.data)
2926 ret = CRYPT_CopyParam(pvData, pcbData,
2927 msg->u.enveloped_data.content.pbData,
2928 msg->u.enveloped_data.content.cbData);
2930 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2932 case CMSG_RECIPIENT_COUNT_PARAM:
2933 if (msg->u.enveloped_data.data)
2934 ret = CRYPT_CopyParam(pvData, pcbData,
2935 &msg->u.enveloped_data.data->cRecipientInfo, sizeof(DWORD));
2937 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2939 case CMSG_RECIPIENT_INFO_PARAM:
2940 if (msg->u.enveloped_data.data)
2942 if (dwIndex < msg->u.enveloped_data.data->cRecipientInfo)
2944 PCMSG_KEY_TRANS_RECIPIENT_INFO recipientInfo =
2945 &msg->u.enveloped_data.data->rgRecipientInfo[dwIndex];
2947 ret = CRYPT_CopyRecipientInfo(pvData, pcbData,
2948 &recipientInfo->RecipientId.u.IssuerSerialNumber);
2951 SetLastError(CRYPT_E_INVALID_INDEX);
2954 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2957 FIXME("unimplemented for %d\n", dwParamType);
2958 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2963 static BOOL CDecodeSignedMsg_GetParam(CDecodeMsg *msg, DWORD dwParamType,
2964 DWORD dwIndex, void *pvData, DWORD *pcbData)
2968 switch (dwParamType)
2970 case CMSG_TYPE_PARAM:
2971 ret = CRYPT_CopyParam(pvData, pcbData, &msg->type, sizeof(msg->type));
2973 case CMSG_CONTENT_PARAM:
2974 if (msg->u.signed_data.info)
2976 if (!strcmp(msg->u.signed_data.info->content.pszObjId,
2979 CRYPT_DATA_BLOB *blob;
2982 ret = CryptDecodeObjectEx(X509_ASN_ENCODING, X509_OCTET_STRING,
2983 msg->u.signed_data.info->content.Content.pbData,
2984 msg->u.signed_data.info->content.Content.cbData,
2985 CRYPT_DECODE_ALLOC_FLAG, NULL, &blob, &size);
2988 ret = CRYPT_CopyParam(pvData, pcbData, blob->pbData,
2994 ret = CRYPT_CopyParam(pvData, pcbData,
2995 msg->u.signed_data.info->content.Content.pbData,
2996 msg->u.signed_data.info->content.Content.cbData);
2999 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3001 case CMSG_INNER_CONTENT_TYPE_PARAM:
3002 if (msg->u.signed_data.info)
3003 ret = CRYPT_CopyParam(pvData, pcbData,
3004 msg->u.signed_data.info->content.pszObjId,
3005 strlen(msg->u.signed_data.info->content.pszObjId) + 1);
3007 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3009 case CMSG_SIGNER_COUNT_PARAM:
3010 if (msg->u.signed_data.info)
3011 ret = CRYPT_CopyParam(pvData, pcbData,
3012 &msg->u.signed_data.info->cSignerInfo, sizeof(DWORD));
3014 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3016 case CMSG_SIGNER_INFO_PARAM:
3017 if (msg->u.signed_data.info)
3019 if (dwIndex >= msg->u.signed_data.info->cSignerInfo)
3020 SetLastError(CRYPT_E_INVALID_INDEX);
3022 ret = CRYPT_CopySignerInfo(pvData, pcbData,
3023 &msg->u.signed_data.info->rgSignerInfo[dwIndex]);
3026 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3028 case CMSG_SIGNER_CERT_INFO_PARAM:
3029 if (msg->u.signed_data.info)
3031 if (dwIndex >= msg->u.signed_data.info->cSignerInfo)
3032 SetLastError(CRYPT_E_INVALID_INDEX);
3034 ret = CRYPT_CopySignerCertInfo(pvData, pcbData,
3035 &msg->u.signed_data.info->rgSignerInfo[dwIndex]);
3038 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3040 case CMSG_CERT_COUNT_PARAM:
3041 if (msg->u.signed_data.info)
3042 ret = CRYPT_CopyParam(pvData, pcbData,
3043 &msg->u.signed_data.info->cCertEncoded, sizeof(DWORD));
3045 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3047 case CMSG_CERT_PARAM:
3048 if (msg->u.signed_data.info)
3050 if (dwIndex >= msg->u.signed_data.info->cCertEncoded)
3051 SetLastError(CRYPT_E_INVALID_INDEX);
3053 ret = CRYPT_CopyParam(pvData, pcbData,
3054 msg->u.signed_data.info->rgCertEncoded[dwIndex].pbData,
3055 msg->u.signed_data.info->rgCertEncoded[dwIndex].cbData);
3058 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3060 case CMSG_CRL_COUNT_PARAM:
3061 if (msg->u.signed_data.info)
3062 ret = CRYPT_CopyParam(pvData, pcbData,
3063 &msg->u.signed_data.info->cCrlEncoded, sizeof(DWORD));
3065 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3067 case CMSG_CRL_PARAM:
3068 if (msg->u.signed_data.info)
3070 if (dwIndex >= msg->u.signed_data.info->cCrlEncoded)
3071 SetLastError(CRYPT_E_INVALID_INDEX);
3073 ret = CRYPT_CopyParam(pvData, pcbData,
3074 msg->u.signed_data.info->rgCrlEncoded[dwIndex].pbData,
3075 msg->u.signed_data.info->rgCrlEncoded[dwIndex].cbData);
3078 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3080 case CMSG_COMPUTED_HASH_PARAM:
3081 if (msg->u.signed_data.info)
3083 if (dwIndex >= msg->u.signed_data.cSignerHandle)
3084 SetLastError(CRYPT_E_INVALID_INDEX);
3086 ret = CryptGetHashParam(
3087 msg->u.signed_data.signerHandles[dwIndex].contentHash,
3088 HP_HASHVAL, pvData, pcbData, 0);
3091 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3093 case CMSG_ENCODED_SIGNER:
3094 if (msg->u.signed_data.info)
3096 if (dwIndex >= msg->u.signed_data.info->cSignerInfo)
3097 SetLastError(CRYPT_E_INVALID_INDEX);
3099 ret = CryptEncodeObjectEx(
3100 X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, CMS_SIGNER_INFO,
3101 &msg->u.signed_data.info->rgSignerInfo[dwIndex], 0, NULL,
3105 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3107 case CMSG_ATTR_CERT_COUNT_PARAM:
3108 if (msg->u.signed_data.info)
3110 DWORD attrCertCount = 0;
3112 ret = CRYPT_CopyParam(pvData, pcbData,
3113 &attrCertCount, sizeof(DWORD));
3116 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3118 case CMSG_ATTR_CERT_PARAM:
3119 if (msg->u.signed_data.info)
3120 SetLastError(CRYPT_E_INVALID_INDEX);
3122 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3124 case CMSG_CMS_SIGNER_INFO_PARAM:
3125 if (msg->u.signed_data.info)
3127 if (dwIndex >= msg->u.signed_data.info->cSignerInfo)
3128 SetLastError(CRYPT_E_INVALID_INDEX);
3130 ret = CRYPT_CopyCMSSignerInfo(pvData, pcbData,
3131 &msg->u.signed_data.info->rgSignerInfo[dwIndex]);
3134 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3137 FIXME("unimplemented for %d\n", dwParamType);
3138 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3143 static BOOL CDecodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
3144 DWORD dwIndex, void *pvData, DWORD *pcbData)
3146 CDecodeMsg *msg = hCryptMsg;
3152 ret = CDecodeHashMsg_GetParam(msg, dwParamType, dwIndex, pvData,
3155 case CMSG_ENVELOPED:
3156 ret = CDecodeEnvelopedMsg_GetParam(msg, dwParamType, dwIndex, pvData,
3160 ret = CDecodeSignedMsg_GetParam(msg, dwParamType, dwIndex, pvData,
3164 switch (dwParamType)
3166 case CMSG_TYPE_PARAM:
3167 ret = CRYPT_CopyParam(pvData, pcbData, &msg->type,
3172 CRYPT_DATA_BLOB blob;
3174 ret = ContextPropertyList_FindProperty(msg->properties, dwParamType,
3177 ret = CRYPT_CopyParam(pvData, pcbData, blob.pbData,
3180 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3187 static BOOL CDecodeHashMsg_VerifyHash(CDecodeMsg *msg)
3190 CRYPT_DATA_BLOB hashBlob;
3192 ret = ContextPropertyList_FindProperty(msg->properties,
3193 CMSG_HASH_DATA_PARAM, &hashBlob);
3196 DWORD computedHashSize = 0;
3198 ret = CDecodeHashMsg_GetParam(msg, CMSG_COMPUTED_HASH_PARAM, 0, NULL,
3200 if (hashBlob.cbData == computedHashSize)
3202 LPBYTE computedHash = CryptMemAlloc(computedHashSize);
3206 ret = CDecodeHashMsg_GetParam(msg, CMSG_COMPUTED_HASH_PARAM, 0,
3207 computedHash, &computedHashSize);
3210 if (memcmp(hashBlob.pbData, computedHash, hashBlob.cbData))
3212 SetLastError(CRYPT_E_HASH_VALUE);
3216 CryptMemFree(computedHash);
3220 SetLastError(ERROR_OUTOFMEMORY);
3226 SetLastError(CRYPT_E_HASH_VALUE);
3233 static BOOL CDecodeSignedMsg_VerifySignatureWithKey(CDecodeMsg *msg,
3234 HCRYPTPROV prov, DWORD signerIndex, PCERT_PUBLIC_KEY_INFO keyInfo)
3240 prov = msg->crypt_prov;
3241 ret = CryptImportPublicKeyInfo(prov, X509_ASN_ENCODING, keyInfo, &key);
3245 CRYPT_HASH_BLOB reversedHash;
3247 if (msg->u.signed_data.info->rgSignerInfo[signerIndex].AuthAttrs.cAttr)
3248 hash = msg->u.signed_data.signerHandles[signerIndex].authAttrHash;
3250 hash = msg->u.signed_data.signerHandles[signerIndex].contentHash;
3251 ret = CRYPT_ConstructBlob(&reversedHash,
3252 &msg->u.signed_data.info->rgSignerInfo[signerIndex].EncryptedHash);
3255 CRYPT_ReverseBytes(&reversedHash);
3256 ret = CryptVerifySignatureW(hash, reversedHash.pbData,
3257 reversedHash.cbData, key, NULL, 0);
3258 CryptMemFree(reversedHash.pbData);
3260 CryptDestroyKey(key);
3265 static BOOL CDecodeSignedMsg_VerifySignature(CDecodeMsg *msg, PCERT_INFO info)
3270 if (!msg->u.signed_data.signerHandles)
3272 SetLastError(NTE_BAD_SIGNATURE);
3275 for (i = 0; !ret && i < msg->u.signed_data.info->cSignerInfo; i++)
3277 PCMSG_CMS_SIGNER_INFO signerInfo =
3278 &msg->u.signed_data.info->rgSignerInfo[i];
3280 if (signerInfo->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
3282 ret = CertCompareCertificateName(X509_ASN_ENCODING,
3283 &signerInfo->SignerId.u.IssuerSerialNumber.Issuer,
3287 ret = CertCompareIntegerBlob(
3288 &signerInfo->SignerId.u.IssuerSerialNumber.SerialNumber,
3289 &info->SerialNumber);
3296 FIXME("signer %d: unimplemented for key id\n", i);
3300 ret = CDecodeSignedMsg_VerifySignatureWithKey(msg, 0, i,
3301 &info->SubjectPublicKeyInfo);
3303 SetLastError(CRYPT_E_SIGNER_NOT_FOUND);
3308 static BOOL CDecodeSignedMsg_VerifySignatureEx(CDecodeMsg *msg,
3309 PCMSG_CTRL_VERIFY_SIGNATURE_EX_PARA para)
3313 if (para->cbSize != sizeof(CMSG_CTRL_VERIFY_SIGNATURE_EX_PARA))
3314 SetLastError(ERROR_INVALID_PARAMETER);
3315 else if (para->dwSignerIndex >= msg->u.signed_data.info->cSignerInfo)
3316 SetLastError(CRYPT_E_SIGNER_NOT_FOUND);
3317 else if (!msg->u.signed_data.signerHandles)
3318 SetLastError(NTE_BAD_SIGNATURE);
3321 switch (para->dwSignerType)
3323 case CMSG_VERIFY_SIGNER_PUBKEY:
3324 ret = CDecodeSignedMsg_VerifySignatureWithKey(msg,
3325 para->hCryptProv, para->dwSignerIndex, para->pvSigner);
3327 case CMSG_VERIFY_SIGNER_CERT:
3329 PCCERT_CONTEXT cert = para->pvSigner;
3331 ret = CDecodeSignedMsg_VerifySignatureWithKey(msg, para->hCryptProv,
3332 para->dwSignerIndex, &cert->pCertInfo->SubjectPublicKeyInfo);
3336 FIXME("unimplemented for signer type %d\n", para->dwSignerType);
3337 SetLastError(CRYPT_E_SIGNER_NOT_FOUND);
3343 static BOOL WINAPI CRYPT_ImportKeyTrans(
3344 PCRYPT_ALGORITHM_IDENTIFIER pContentEncryptionAlgorithm,
3345 PCMSG_CTRL_KEY_TRANS_DECRYPT_PARA pKeyTransDecryptPara, DWORD dwFlags,
3346 void *pvReserved, HCRYPTKEY *phContentEncryptKey)
3351 ret = CryptGetUserKey(pKeyTransDecryptPara->hCryptProv,
3352 pKeyTransDecryptPara->dwKeySpec ? pKeyTransDecryptPara->dwKeySpec :
3353 AT_KEYEXCHANGE, &key);
3356 CMSG_KEY_TRANS_RECIPIENT_INFO *info =
3357 &pKeyTransDecryptPara->pKeyTrans[pKeyTransDecryptPara->dwRecipientIndex];
3358 CRYPT_DATA_BLOB *encryptedKey = &info->EncryptedKey;
3359 DWORD size = encryptedKey->cbData + sizeof(BLOBHEADER) + sizeof(ALG_ID);
3360 BYTE *keyBlob = CryptMemAlloc(size);
3364 DWORD i, k = size - 1;
3365 BLOBHEADER *blobHeader = (BLOBHEADER *)keyBlob;
3366 ALG_ID *algID = (ALG_ID *)(keyBlob + sizeof(BLOBHEADER));
3368 blobHeader->bType = SIMPLEBLOB;
3369 blobHeader->bVersion = CUR_BLOB_VERSION;
3370 blobHeader->reserved = 0;
3371 blobHeader->aiKeyAlg = CertOIDToAlgId(
3372 pContentEncryptionAlgorithm->pszObjId);
3373 *algID = CertOIDToAlgId(info->KeyEncryptionAlgorithm.pszObjId);
3374 for (i = 0; i < encryptedKey->cbData; ++i, --k)
3375 keyBlob[k] = encryptedKey->pbData[i];
3377 ret = CryptImportKey(pKeyTransDecryptPara->hCryptProv, keyBlob,
3378 size, key, 0, phContentEncryptKey);
3379 CryptMemFree(keyBlob);
3383 CryptDestroyKey(key);
3388 static BOOL CRYPT_ImportEncryptedKey(PCRYPT_ALGORITHM_IDENTIFIER contEncrAlg,
3389 PCMSG_CTRL_DECRYPT_PARA para, PCMSG_KEY_TRANS_RECIPIENT_INFO info,
3392 static HCRYPTOIDFUNCSET set = NULL;
3393 PFN_CMSG_IMPORT_KEY_TRANS importKeyFunc = NULL;
3394 HCRYPTOIDFUNCADDR hFunc = NULL;
3395 CMSG_CTRL_KEY_TRANS_DECRYPT_PARA decryptPara;
3398 memset(&decryptPara, 0, sizeof(decryptPara));
3399 decryptPara.cbSize = sizeof(decryptPara);
3400 decryptPara.hCryptProv = para->hCryptProv;
3401 decryptPara.dwKeySpec = para->dwKeySpec;
3402 decryptPara.pKeyTrans = info;
3403 decryptPara.dwRecipientIndex = para->dwRecipientIndex;
3406 set = CryptInitOIDFunctionSet(CMSG_OID_IMPORT_KEY_TRANS_FUNC, 0);
3407 CryptGetOIDFunctionAddress(set, X509_ASN_ENCODING, contEncrAlg->pszObjId, 0,
3408 (void **)&importKeyFunc, &hFunc);
3410 importKeyFunc = CRYPT_ImportKeyTrans;
3411 ret = importKeyFunc(contEncrAlg, &decryptPara, 0, NULL, key);
3413 CryptFreeOIDFunctionAddress(hFunc, 0);
3417 static BOOL CDecodeEnvelopedMsg_CrtlDecrypt(CDecodeMsg *msg,
3418 PCMSG_CTRL_DECRYPT_PARA para)
3421 CEnvelopedDecodeMsg *enveloped_data = &msg->u.enveloped_data;
3422 CRYPT_ENVELOPED_DATA *data = enveloped_data->data;
3424 if (para->cbSize != sizeof(CMSG_CTRL_DECRYPT_PARA))
3425 SetLastError(E_INVALIDARG);
3427 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3428 else if (para->dwRecipientIndex >= data->cRecipientInfo)
3429 SetLastError(CRYPT_E_INVALID_INDEX);
3430 else if (enveloped_data->decrypted)
3431 SetLastError(CRYPT_E_ALREADY_DECRYPTED);
3432 else if (!para->hCryptProv)
3433 SetLastError(ERROR_INVALID_PARAMETER);
3434 else if (enveloped_data->content.cbData)
3438 ret = CRYPT_ImportEncryptedKey(
3439 &data->encryptedContentInfo.contentEncryptionAlgorithm, para,
3440 data->rgRecipientInfo, &key);
3443 ret = CryptDecrypt(key, 0, TRUE, 0, enveloped_data->content.pbData,
3444 &enveloped_data->content.cbData);
3445 CryptDestroyKey(key);
3451 enveloped_data->decrypted = TRUE;
3455 static BOOL CDecodeMsg_Control(HCRYPTMSG hCryptMsg, DWORD dwFlags,
3456 DWORD dwCtrlType, const void *pvCtrlPara)
3458 CDecodeMsg *msg = hCryptMsg;
3463 case CMSG_CTRL_VERIFY_SIGNATURE:
3467 ret = CDecodeSignedMsg_VerifySignature(msg, (PCERT_INFO)pvCtrlPara);
3470 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3473 case CMSG_CTRL_DECRYPT:
3476 case CMSG_ENVELOPED:
3477 ret = CDecodeEnvelopedMsg_CrtlDecrypt(msg,
3478 (PCMSG_CTRL_DECRYPT_PARA)pvCtrlPara);
3479 if (ret && (dwFlags & CMSG_CRYPT_RELEASE_CONTEXT_FLAG))
3480 msg->u.enveloped_data.crypt_prov =
3481 ((PCMSG_CTRL_DECRYPT_PARA)pvCtrlPara)->hCryptProv;
3484 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3487 case CMSG_CTRL_VERIFY_HASH:
3491 ret = CDecodeHashMsg_VerifyHash(msg);
3494 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3497 case CMSG_CTRL_VERIFY_SIGNATURE_EX:
3501 ret = CDecodeSignedMsg_VerifySignatureEx(msg,
3502 (PCMSG_CTRL_VERIFY_SIGNATURE_EX_PARA)pvCtrlPara);
3505 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3509 SetLastError(CRYPT_E_CONTROL_TYPE);
3514 HCRYPTMSG WINAPI CryptMsgOpenToDecode(DWORD dwMsgEncodingType, DWORD dwFlags,
3515 DWORD dwMsgType, HCRYPTPROV_LEGACY hCryptProv, PCERT_INFO pRecipientInfo,
3516 PCMSG_STREAM_INFO pStreamInfo)
3520 TRACE("(%08x, %08x, %08x, %08lx, %p, %p)\n", dwMsgEncodingType,
3521 dwFlags, dwMsgType, hCryptProv, pRecipientInfo, pStreamInfo);
3523 if (GET_CMSG_ENCODING_TYPE(dwMsgEncodingType) != PKCS_7_ASN_ENCODING)
3525 SetLastError(E_INVALIDARG);
3528 msg = CryptMemAlloc(sizeof(CDecodeMsg));
3531 CryptMsgBase_Init((CryptMsgBase *)msg, dwFlags, pStreamInfo,
3532 CDecodeMsg_Close, CDecodeMsg_GetParam, CDecodeMsg_Update,
3533 CDecodeMsg_Control);
3534 msg->type = dwMsgType;
3536 msg->crypt_prov = hCryptProv;
3539 msg->crypt_prov = CRYPT_GetDefaultProvider();
3540 msg->base.open_flags &= ~CMSG_CRYPT_RELEASE_CONTEXT_FLAG;
3542 memset(&msg->u, 0, sizeof(msg->u));
3543 msg->msg_data.cbData = 0;
3544 msg->msg_data.pbData = NULL;
3545 msg->detached_data.cbData = 0;
3546 msg->detached_data.pbData = NULL;
3547 msg->properties = ContextPropertyList_Create();
3552 HCRYPTMSG WINAPI CryptMsgDuplicate(HCRYPTMSG hCryptMsg)
3554 TRACE("(%p)\n", hCryptMsg);
3558 CryptMsgBase *msg = hCryptMsg;
3560 InterlockedIncrement(&msg->ref);
3565 BOOL WINAPI CryptMsgClose(HCRYPTMSG hCryptMsg)
3567 TRACE("(%p)\n", hCryptMsg);
3571 CryptMsgBase *msg = hCryptMsg;
3573 if (InterlockedDecrement(&msg->ref) == 0)
3575 TRACE("freeing %p\n", msg);
3584 BOOL WINAPI CryptMsgUpdate(HCRYPTMSG hCryptMsg, const BYTE *pbData,
3585 DWORD cbData, BOOL fFinal)
3587 CryptMsgBase *msg = hCryptMsg;
3589 TRACE("(%p, %p, %d, %d)\n", hCryptMsg, pbData, cbData, fFinal);
3591 return msg->update(hCryptMsg, pbData, cbData, fFinal);
3594 BOOL WINAPI CryptMsgGetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
3595 DWORD dwIndex, void *pvData, DWORD *pcbData)
3597 CryptMsgBase *msg = hCryptMsg;
3599 TRACE("(%p, %d, %d, %p, %p)\n", hCryptMsg, dwParamType, dwIndex,
3601 return msg->get_param(hCryptMsg, dwParamType, dwIndex, pvData, pcbData);
3604 BOOL WINAPI CryptMsgControl(HCRYPTMSG hCryptMsg, DWORD dwFlags,
3605 DWORD dwCtrlType, const void *pvCtrlPara)
3607 CryptMsgBase *msg = hCryptMsg;
3609 TRACE("(%p, %08x, %d, %p)\n", hCryptMsg, dwFlags, dwCtrlType,
3611 return msg->control(hCryptMsg, dwFlags, dwCtrlType, pvCtrlPara);
3614 static CERT_INFO *CRYPT_GetSignerCertInfoFromMsg(HCRYPTMSG msg,
3615 DWORD dwSignerIndex)
3617 CERT_INFO *certInfo = NULL;
3620 if (CryptMsgGetParam(msg, CMSG_SIGNER_CERT_INFO_PARAM, dwSignerIndex, NULL,
3623 certInfo = CryptMemAlloc(size);
3626 if (!CryptMsgGetParam(msg, CMSG_SIGNER_CERT_INFO_PARAM,
3627 dwSignerIndex, certInfo, &size))
3629 CryptMemFree(certInfo);
3637 BOOL WINAPI CryptMsgGetAndVerifySigner(HCRYPTMSG hCryptMsg, DWORD cSignerStore,
3638 HCERTSTORE *rghSignerStore, DWORD dwFlags, PCCERT_CONTEXT *ppSigner,
3639 DWORD *pdwSignerIndex)
3642 DWORD i, signerIndex = 0;
3643 PCCERT_CONTEXT signerCert = NULL;
3646 TRACE("(%p, %d, %p, %08x, %p, %p)\n", hCryptMsg, cSignerStore,
3647 rghSignerStore, dwFlags, ppSigner, pdwSignerIndex);
3649 /* Clear output parameters */
3652 if (pdwSignerIndex && !(dwFlags & CMSG_USE_SIGNER_INDEX_FLAG))
3653 *pdwSignerIndex = 0;
3655 /* Create store to search for signer certificates */
3656 store = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
3657 CERT_STORE_CREATE_NEW_FLAG, NULL);
3658 if (!(dwFlags & CMSG_TRUSTED_SIGNER_FLAG))
3660 HCERTSTORE msgStore = CertOpenStore(CERT_STORE_PROV_MSG, 0, 0, 0,
3663 CertAddStoreToCollection(store, msgStore, 0, 0);
3664 CertCloseStore(msgStore, 0);
3666 for (i = 0; i < cSignerStore; i++)
3667 CertAddStoreToCollection(store, rghSignerStore[i], 0, 0);
3669 /* Find signer cert */
3670 if (dwFlags & CMSG_USE_SIGNER_INDEX_FLAG)
3672 CERT_INFO *signer = CRYPT_GetSignerCertInfoFromMsg(hCryptMsg,
3677 signerIndex = *pdwSignerIndex;
3678 signerCert = CertFindCertificateInStore(store, X509_ASN_ENCODING,
3679 0, CERT_FIND_SUBJECT_CERT, signer, NULL);
3680 CryptMemFree(signer);
3685 DWORD count, size = sizeof(count);
3687 if (CryptMsgGetParam(hCryptMsg, CMSG_SIGNER_COUNT_PARAM, 0, &count,
3690 for (i = 0; !signerCert && i < count; i++)
3692 CERT_INFO *signer = CRYPT_GetSignerCertInfoFromMsg(hCryptMsg,
3697 signerCert = CertFindCertificateInStore(store,
3698 X509_ASN_ENCODING, 0, CERT_FIND_SUBJECT_CERT, signer,
3702 CryptMemFree(signer);
3707 SetLastError(CRYPT_E_NO_TRUSTED_SIGNER);
3711 if (!(dwFlags & CMSG_SIGNER_ONLY_FLAG))
3712 ret = CryptMsgControl(hCryptMsg, 0, CMSG_CTRL_VERIFY_SIGNATURE,
3713 signerCert->pCertInfo);
3719 *ppSigner = CertDuplicateCertificateContext(signerCert);
3721 *pdwSignerIndex = signerIndex;
3723 CertFreeCertificateContext(signerCert);
3726 CertCloseStore(store, 0);
3730 BOOL WINAPI CryptMsgVerifyCountersignatureEncodedEx(HCRYPTPROV_LEGACY hCryptProv,
3731 DWORD dwEncodingType, PBYTE pbSignerInfo, DWORD cbSignerInfo,
3732 PBYTE pbSignerInfoCountersignature, DWORD cbSignerInfoCountersignature,
3733 DWORD dwSignerType, void *pvSigner, DWORD dwFlags, void *pvReserved)
3735 FIXME("(%08lx, %08x, %p, %d, %p, %d, %d, %p, %08x, %p): stub\n", hCryptProv,
3736 dwEncodingType, pbSignerInfo, cbSignerInfo, pbSignerInfoCountersignature,
3737 cbSignerInfoCountersignature, dwSignerType, pvSigner, dwFlags, pvReserved);
3741 BOOL WINAPI CryptMsgEncodeAndSignCTL(DWORD dwMsgEncodingType,
3742 PCTL_INFO pCtlInfo, PCMSG_SIGNED_ENCODE_INFO pSignInfo, DWORD dwFlags,
3743 BYTE *pbEncoded, DWORD *pcbEncoded)
3749 TRACE("(%08x, %p, %p, %08x, %p, %p)\n", dwMsgEncodingType, pCtlInfo,
3750 pSignInfo, dwFlags, pbEncoded, pcbEncoded);
3754 FIXME("unimplemented for flags %08x\n", dwFlags);
3757 if ((ret = CryptEncodeObjectEx(dwMsgEncodingType, PKCS_CTL, pCtlInfo,
3758 CRYPT_ENCODE_ALLOC_FLAG, NULL, &pbCtlContent, &cbCtlContent)))
3760 ret = CryptMsgSignCTL(dwMsgEncodingType, pbCtlContent, cbCtlContent,
3761 pSignInfo, dwFlags, pbEncoded, pcbEncoded);
3762 LocalFree(pbCtlContent);
3767 BOOL WINAPI CryptMsgSignCTL(DWORD dwMsgEncodingType, BYTE *pbCtlContent,
3768 DWORD cbCtlContent, PCMSG_SIGNED_ENCODE_INFO pSignInfo, DWORD dwFlags,
3769 BYTE *pbEncoded, DWORD *pcbEncoded)
3771 static char oid_ctl[] = szOID_CTL;
3775 TRACE("(%08x, %p, %d, %p, %08x, %p, %p)\n", dwMsgEncodingType,
3776 pbCtlContent, cbCtlContent, pSignInfo, dwFlags, pbEncoded, pcbEncoded);
3780 FIXME("unimplemented for flags %08x\n", dwFlags);
3783 msg = CryptMsgOpenToEncode(dwMsgEncodingType, 0, CMSG_SIGNED, pSignInfo,
3787 ret = CryptMsgUpdate(msg, pbCtlContent, cbCtlContent, TRUE);
3789 ret = CryptMsgGetParam(msg, CMSG_CONTENT_PARAM, 0, pbEncoded,