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
24 #include "wine/debug.h"
25 #include "wine/exception.h"
26 #include "crypt32_private.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
30 /* Called when a message's ref count reaches zero. Free any message-specific
33 typedef void (*CryptMsgCloseFunc)(HCRYPTMSG msg);
35 typedef BOOL (*CryptMsgGetParamFunc)(HCRYPTMSG hCryptMsg, DWORD dwParamType,
36 DWORD dwIndex, void *pvData, DWORD *pcbData);
38 typedef BOOL (*CryptMsgUpdateFunc)(HCRYPTMSG hCryptMsg, const BYTE *pbData,
39 DWORD cbData, BOOL fFinal);
41 typedef enum _CryptMsgState {
47 typedef struct _CryptMsgBase
52 CMSG_STREAM_INFO stream_info;
54 CryptMsgCloseFunc close;
55 CryptMsgUpdateFunc update;
56 CryptMsgGetParamFunc get_param;
59 static inline void CryptMsgBase_Init(CryptMsgBase *msg, DWORD dwFlags,
60 PCMSG_STREAM_INFO pStreamInfo, CryptMsgCloseFunc close,
61 CryptMsgGetParamFunc get_param, CryptMsgUpdateFunc update)
64 msg->open_flags = dwFlags;
68 memcpy(&msg->stream_info, pStreamInfo, sizeof(msg->stream_info));
72 msg->streamed = FALSE;
73 memset(&msg->stream_info, 0, sizeof(msg->stream_info));
76 msg->get_param = get_param;
78 msg->state = MsgStateInit;
81 typedef struct _CDataEncodeMsg
84 DWORD bare_content_len;
88 static const BYTE empty_data_content[] = { 0x04,0x00 };
90 static void CDataEncodeMsg_Close(HCRYPTMSG hCryptMsg)
92 CDataEncodeMsg *msg = (CDataEncodeMsg *)hCryptMsg;
94 if (msg->bare_content != empty_data_content)
95 LocalFree(msg->bare_content);
98 static WINAPI BOOL CRYPT_EncodeContentLength(DWORD dwCertEncodingType,
99 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
100 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
102 const CDataEncodeMsg *msg = (const CDataEncodeMsg *)pvStructInfo;
106 /* Trick: report bytes needed based on total message length, even though
107 * the message isn't available yet. The caller will use the length
108 * reported here to encode its length.
110 CRYPT_EncodeLen(msg->base.stream_info.cbContent, NULL, &lenBytes);
112 *pcbEncoded = 1 + lenBytes + msg->base.stream_info.cbContent;
115 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
116 pcbEncoded, 1 + lenBytes)))
118 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
119 pbEncoded = *(BYTE **)pbEncoded;
120 *pbEncoded++ = ASN_OCTETSTRING;
121 CRYPT_EncodeLen(msg->base.stream_info.cbContent, pbEncoded,
128 static BOOL CRYPT_EncodeDataContentInfoHeader(CDataEncodeMsg *msg,
129 CRYPT_DATA_BLOB *header)
133 if (msg->base.streamed && msg->base.stream_info.cbContent == 0xffffffff)
135 FIXME("unimplemented for indefinite-length encoding\n");
137 header->pbData = NULL;
142 struct AsnConstructedItem constructed = { 0, msg,
143 CRYPT_EncodeContentLength };
144 struct AsnEncodeSequenceItem items[2] = {
145 { szOID_RSA_data, CRYPT_AsnEncodeOid, 0 },
146 { &constructed, CRYPT_AsnEncodeConstructed, 0 },
149 ret = CRYPT_AsnEncodeSequence(X509_ASN_ENCODING, items,
150 sizeof(items) / sizeof(items[0]), CRYPT_ENCODE_ALLOC_FLAG, NULL,
151 (LPBYTE)&header->pbData, &header->cbData);
154 /* Trick: subtract the content length from the reported length,
155 * as the actual content hasn't come yet.
157 header->cbData -= msg->base.stream_info.cbContent;
163 static BOOL CDataEncodeMsg_Update(HCRYPTMSG hCryptMsg, const BYTE *pbData,
164 DWORD cbData, BOOL fFinal)
166 CDataEncodeMsg *msg = (CDataEncodeMsg *)hCryptMsg;
169 if (msg->base.streamed)
173 if (msg->base.state != MsgStateUpdated)
175 CRYPT_DATA_BLOB header;
177 ret = CRYPT_EncodeDataContentInfoHeader(msg, &header);
180 ret = msg->base.stream_info.pfnStreamOutput(
181 msg->base.stream_info.pvArg, header.pbData, header.cbData,
183 LocalFree(header.pbData);
187 ret = msg->base.stream_info.pfnStreamOutput(
188 msg->base.stream_info.pvArg, (BYTE *)pbData, cbData,
192 if (msg->base.stream_info.cbContent == 0xffffffff)
194 BYTE indefinite_trailer[6] = { 0 };
196 ret = msg->base.stream_info.pfnStreamOutput(
197 msg->base.stream_info.pvArg, (BYTE *)pbData, cbData,
200 ret = msg->base.stream_info.pfnStreamOutput(
201 msg->base.stream_info.pvArg, indefinite_trailer,
202 sizeof(indefinite_trailer), TRUE);
205 ret = msg->base.stream_info.pfnStreamOutput(
206 msg->base.stream_info.pvArg, (BYTE *)pbData, cbData, TRUE);
211 SetLastError(STATUS_ACCESS_VIOLATION);
220 if (msg->base.open_flags & CMSG_DETACHED_FLAG)
221 SetLastError(E_INVALIDARG);
223 SetLastError(CRYPT_E_MSG_ERROR);
228 SetLastError(E_INVALIDARG);
231 CRYPT_DATA_BLOB blob = { cbData, (LPBYTE)pbData };
233 /* non-streamed data messages don't allow non-final updates,
234 * don't bother checking whether data already exist, they can't.
236 ret = CryptEncodeObjectEx(X509_ASN_ENCODING, X509_OCTET_STRING,
237 &blob, CRYPT_ENCODE_ALLOC_FLAG, NULL, &msg->bare_content,
238 &msg->bare_content_len);
245 static BOOL CRYPT_CopyParam(void *pvData, DWORD *pcbData, const void *src,
252 else if (*pcbData < len)
255 SetLastError(ERROR_MORE_DATA);
261 memcpy(pvData, src, len);
266 static BOOL CDataEncodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
267 DWORD dwIndex, void *pvData, DWORD *pcbData)
269 CDataEncodeMsg *msg = (CDataEncodeMsg *)hCryptMsg;
274 case CMSG_CONTENT_PARAM:
275 if (msg->base.streamed)
276 SetLastError(E_INVALIDARG);
279 CRYPT_CONTENT_INFO info;
280 char rsa_data[] = "1.2.840.113549.1.7.1";
282 info.pszObjId = rsa_data;
283 info.Content.cbData = msg->bare_content_len;
284 info.Content.pbData = msg->bare_content;
285 ret = CryptEncodeObject(X509_ASN_ENCODING, PKCS_CONTENT_INFO, &info,
289 case CMSG_BARE_CONTENT_PARAM:
290 if (msg->base.streamed)
291 SetLastError(E_INVALIDARG);
293 ret = CRYPT_CopyParam(pvData, pcbData, msg->bare_content,
294 msg->bare_content_len);
297 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
302 static HCRYPTMSG CDataEncodeMsg_Open(DWORD dwFlags, const void *pvMsgEncodeInfo,
303 LPSTR pszInnerContentObjID, PCMSG_STREAM_INFO pStreamInfo)
309 SetLastError(E_INVALIDARG);
312 msg = CryptMemAlloc(sizeof(CDataEncodeMsg));
315 CryptMsgBase_Init((CryptMsgBase *)msg, dwFlags, pStreamInfo,
316 CDataEncodeMsg_Close, CDataEncodeMsg_GetParam, CDataEncodeMsg_Update);
317 msg->bare_content_len = sizeof(empty_data_content);
318 msg->bare_content = (LPBYTE)empty_data_content;
320 return (HCRYPTMSG)msg;
323 typedef struct _CHashEncodeMsg
328 CRYPT_DATA_BLOB data;
331 static void CHashEncodeMsg_Close(HCRYPTMSG hCryptMsg)
333 CHashEncodeMsg *msg = (CHashEncodeMsg *)hCryptMsg;
335 CryptMemFree(msg->data.pbData);
336 CryptDestroyHash(msg->hash);
337 if (msg->base.open_flags & CMSG_CRYPT_RELEASE_CONTEXT_FLAG)
338 CryptReleaseContext(msg->prov, 0);
341 static BOOL CRYPT_EncodePKCSDigestedData(CHashEncodeMsg *msg, void *pvData,
346 DWORD size = sizeof(algID);
348 ret = CryptGetHashParam(msg->hash, HP_ALGID, (BYTE *)&algID, &size, 0);
351 CRYPT_DIGESTED_DATA digestedData = { 0 };
352 char oid_rsa_data[] = szOID_RSA_data;
354 digestedData.version = CMSG_HASHED_DATA_PKCS_1_5_VERSION;
355 digestedData.DigestAlgorithm.pszObjId = (LPSTR)CertAlgIdToOID(algID);
356 /* FIXME: what about digestedData.DigestAlgorithm.Parameters? */
357 /* Quirk: OID is only encoded messages if an update has happened */
358 if (msg->base.state != MsgStateInit)
359 digestedData.ContentInfo.pszObjId = oid_rsa_data;
360 if (!(msg->base.open_flags & CMSG_DETACHED_FLAG) && msg->data.cbData)
362 ret = CRYPT_AsnEncodeOctets(0, NULL, &msg->data,
363 CRYPT_ENCODE_ALLOC_FLAG, NULL,
364 (LPBYTE)&digestedData.ContentInfo.Content.pbData,
365 &digestedData.ContentInfo.Content.cbData);
367 if (msg->base.state == MsgStateFinalized)
369 size = sizeof(DWORD);
370 ret = CryptGetHashParam(msg->hash, HP_HASHSIZE,
371 (LPBYTE)&digestedData.hash.cbData, &size, 0);
374 digestedData.hash.pbData = CryptMemAlloc(
375 digestedData.hash.cbData);
376 ret = CryptGetHashParam(msg->hash, HP_HASHVAL,
377 digestedData.hash.pbData, &digestedData.hash.cbData, 0);
381 ret = CRYPT_AsnEncodePKCSDigestedData(&digestedData, pvData,
383 CryptMemFree(digestedData.hash.pbData);
384 LocalFree(digestedData.ContentInfo.Content.pbData);
389 static BOOL CHashEncodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
390 DWORD dwIndex, void *pvData, DWORD *pcbData)
392 CHashEncodeMsg *msg = (CHashEncodeMsg *)hCryptMsg;
395 TRACE("(%p, %d, %d, %p, %p)\n", hCryptMsg, dwParamType, dwIndex,
400 case CMSG_BARE_CONTENT_PARAM:
401 if (msg->base.streamed)
402 SetLastError(E_INVALIDARG);
404 ret = CRYPT_EncodePKCSDigestedData(msg, pvData, pcbData);
406 case CMSG_CONTENT_PARAM:
408 CRYPT_CONTENT_INFO info;
410 ret = CryptMsgGetParam(hCryptMsg, CMSG_BARE_CONTENT_PARAM, 0, NULL,
411 &info.Content.cbData);
414 info.Content.pbData = CryptMemAlloc(info.Content.cbData);
415 if (info.Content.pbData)
417 ret = CryptMsgGetParam(hCryptMsg, CMSG_BARE_CONTENT_PARAM, 0,
418 info.Content.pbData, &info.Content.cbData);
421 char oid_rsa_hashed[] = szOID_RSA_hashedData;
423 info.pszObjId = oid_rsa_hashed;
424 ret = CryptEncodeObjectEx(X509_ASN_ENCODING,
425 PKCS_CONTENT_INFO, &info, 0, NULL, pvData, pcbData);
427 CryptMemFree(info.Content.pbData);
434 case CMSG_COMPUTED_HASH_PARAM:
435 ret = CryptGetHashParam(msg->hash, HP_HASHVAL, (BYTE *)pvData, pcbData,
438 case CMSG_VERSION_PARAM:
439 if (msg->base.state != MsgStateFinalized)
440 SetLastError(CRYPT_E_MSG_ERROR);
443 DWORD version = CMSG_HASHED_DATA_PKCS_1_5_VERSION;
445 /* Since the data are always encoded as octets, the version is
446 * always 0 (see rfc3852, section 7)
448 ret = CRYPT_CopyParam(pvData, pcbData, &version, sizeof(version));
457 static BOOL CHashEncodeMsg_Update(HCRYPTMSG hCryptMsg, const BYTE *pbData,
458 DWORD cbData, BOOL fFinal)
460 CHashEncodeMsg *msg = (CHashEncodeMsg *)hCryptMsg;
463 TRACE("(%p, %p, %d, %d)\n", hCryptMsg, pbData, cbData, fFinal);
465 if (msg->base.streamed || (msg->base.open_flags & CMSG_DETACHED_FLAG))
467 /* Doesn't do much, as stream output is never called, and you
468 * can't get the content.
470 ret = CryptHashData(msg->hash, pbData, cbData, 0);
475 SetLastError(CRYPT_E_MSG_ERROR);
478 ret = CryptHashData(msg->hash, pbData, cbData, 0);
481 msg->data.pbData = CryptMemAlloc(cbData);
482 if (msg->data.pbData)
484 memcpy(msg->data.pbData + msg->data.cbData, pbData, cbData);
485 msg->data.cbData += cbData;
495 static HCRYPTMSG CHashEncodeMsg_Open(DWORD dwFlags, const void *pvMsgEncodeInfo,
496 LPSTR pszInnerContentObjID, PCMSG_STREAM_INFO pStreamInfo)
499 const CMSG_HASHED_ENCODE_INFO *info =
500 (const CMSG_HASHED_ENCODE_INFO *)pvMsgEncodeInfo;
504 if (info->cbSize != sizeof(CMSG_HASHED_ENCODE_INFO))
506 SetLastError(E_INVALIDARG);
509 if (!(algID = CertOIDToAlgId(info->HashAlgorithm.pszObjId)))
511 SetLastError(CRYPT_E_UNKNOWN_ALGO);
514 if (info->hCryptProv)
515 prov = info->hCryptProv;
518 prov = CRYPT_GetDefaultProvider();
519 dwFlags &= ~CMSG_CRYPT_RELEASE_CONTEXT_FLAG;
521 msg = CryptMemAlloc(sizeof(CHashEncodeMsg));
524 CryptMsgBase_Init((CryptMsgBase *)msg, dwFlags, pStreamInfo,
525 CHashEncodeMsg_Close, CHashEncodeMsg_GetParam, CHashEncodeMsg_Update);
527 msg->data.cbData = 0;
528 msg->data.pbData = NULL;
529 if (!CryptCreateHash(prov, algID, 0, 0, &msg->hash))
535 return (HCRYPTMSG)msg;
538 typedef struct _CMSG_SIGNER_ENCODE_INFO_WITH_CMS
541 PCERT_INFO pCertInfo;
542 HCRYPTPROV hCryptProv;
544 CRYPT_ALGORITHM_IDENTIFIER HashAlgorithm;
547 PCRYPT_ATTRIBUTE rgAuthAttr;
549 PCRYPT_ATTRIBUTE rgUnauthAttr;
551 CRYPT_ALGORITHM_IDENTIFIER HashEncryptionAlgorithm;
552 void *pvHashEncryptionAuxInfo;
553 } CMSG_SIGNER_ENCODE_INFO_WITH_CMS, *PCMSG_SIGNER_ENCODE_INFO_WITH_CMS;
555 typedef struct _CMSG_SIGNED_ENCODE_INFO_WITH_CMS
559 PCMSG_SIGNER_ENCODE_INFO_WITH_CMS rgSigners;
561 PCERT_BLOB rgCertEncoded;
563 PCRL_BLOB rgCrlEncoded;
564 DWORD cAttrCertEncoded;
565 PCERT_BLOB rgAttrCertEncoded;
566 } CMSG_SIGNED_ENCODE_INFO_WITH_CMS, *PCMSG_SIGNED_ENCODE_INFO_WITH_CMS;
568 static BOOL CRYPT_IsValidSigner(CMSG_SIGNER_ENCODE_INFO_WITH_CMS *signer)
570 if (signer->cbSize != sizeof(CMSG_SIGNER_ENCODE_INFO) &&
571 signer->cbSize != sizeof(CMSG_SIGNER_ENCODE_INFO_WITH_CMS))
573 SetLastError(E_INVALIDARG);
576 if (signer->cbSize == sizeof(CMSG_SIGNER_ENCODE_INFO_WITH_CMS))
578 FIXME("CMSG_SIGNER_ENCODE_INFO with CMS fields unsupported\n");
581 if (!signer->pCertInfo->SerialNumber.cbData)
583 SetLastError(E_INVALIDARG);
586 if (!signer->pCertInfo->Issuer.cbData)
588 SetLastError(E_INVALIDARG);
591 if (!signer->hCryptProv)
593 SetLastError(E_INVALIDARG);
596 if (!CertOIDToAlgId(signer->HashAlgorithm.pszObjId))
598 SetLastError(CRYPT_E_UNKNOWN_ALGO);
604 typedef struct _CSignerHandles
607 HCRYPTHASH contentHash;
608 HCRYPTHASH authAttrHash;
612 static BOOL CRYPT_ConstructBlob(CRYPT_DATA_BLOB *out, const CRYPT_DATA_BLOB *in)
616 out->cbData = in->cbData;
619 out->pbData = CryptMemAlloc(out->cbData);
621 memcpy(out->pbData, in->pbData, out->cbData);
630 typedef struct _BlobArray
633 PCRYPT_DATA_BLOB blobs;
636 static BOOL CRYPT_ConstructBlobArray(BlobArray *out, const BlobArray *in)
640 out->cBlobs = in->cBlobs;
643 out->blobs = CryptMemAlloc(out->cBlobs * sizeof(CRYPT_DATA_BLOB));
648 memset(out->blobs, 0, out->cBlobs * sizeof(CRYPT_DATA_BLOB));
649 for (i = 0; ret && i < out->cBlobs; i++)
650 ret = CRYPT_ConstructBlob(&out->blobs[i], &in->blobs[i]);
658 static void CRYPT_FreeBlobArray(BlobArray *array)
662 for (i = 0; i < array->cBlobs; i++)
663 CryptMemFree(array->blobs[i].pbData);
664 CryptMemFree(array->blobs);
667 static BOOL CRYPT_ConstructAttribute(CRYPT_ATTRIBUTE *out,
668 const CRYPT_ATTRIBUTE *in)
672 out->pszObjId = CryptMemAlloc(strlen(in->pszObjId) + 1);
675 strcpy(out->pszObjId, in->pszObjId);
676 ret = CRYPT_ConstructBlobArray((BlobArray *)&out->cValue,
677 (const BlobArray *)&in->cValue);
684 static BOOL CRYPT_ConstructAttributes(CRYPT_ATTRIBUTES *out,
685 const CRYPT_ATTRIBUTES *in)
689 out->cAttr = in->cAttr;
692 out->rgAttr = CryptMemAlloc(out->cAttr * sizeof(CRYPT_ATTRIBUTE));
697 memset(out->rgAttr, 0, out->cAttr * sizeof(CRYPT_ATTRIBUTE));
698 for (i = 0; ret && i < out->cAttr; i++)
699 ret = CRYPT_ConstructAttribute(&out->rgAttr[i], &in->rgAttr[i]);
709 /* Constructs both a CSignerHandles and a CMSG_SIGNER_INFO from a
710 * CMSG_SIGNER_ENCODE_INFO_WITH_CMS.
712 static BOOL CSignerInfo_Construct(CSignerHandles *handles,
713 CMSG_SIGNER_INFO *info, CMSG_SIGNER_ENCODE_INFO_WITH_CMS *in, DWORD open_flags)
718 handles->prov = in->hCryptProv;
719 if (!(open_flags & CMSG_CRYPT_RELEASE_CONTEXT_FLAG))
720 CryptContextAddRef(handles->prov, NULL, 0);
721 algID = CertOIDToAlgId(in->HashAlgorithm.pszObjId);
722 ret = CryptCreateHash(handles->prov, algID, 0, 0, &handles->contentHash);
723 if (ret && in->cAuthAttr)
724 ret = CryptCreateHash(handles->prov, algID, 0, 0,
725 &handles->authAttrHash);
728 /* Note: needs to change if CMS fields are supported */
729 info->dwVersion = CMSG_SIGNER_INFO_V1;
730 ret = CRYPT_ConstructBlob(&info->Issuer, &in->pCertInfo->Issuer);
732 ret = CRYPT_ConstructBlob(&info->SerialNumber,
733 &in->pCertInfo->SerialNumber);
734 /* Assumption: algorithm IDs will point to static strings, not
735 * stack-based ones, so copying the pointer values is safe.
737 info->HashAlgorithm.pszObjId = in->HashAlgorithm.pszObjId;
739 ret = CRYPT_ConstructBlob(&info->HashAlgorithm.Parameters,
740 &in->HashAlgorithm.Parameters);
741 memset(&info->HashEncryptionAlgorithm, 0,
742 sizeof(info->HashEncryptionAlgorithm));
744 ret = CRYPT_ConstructAttributes(&info->AuthAttrs,
745 (CRYPT_ATTRIBUTES *)&in->cAuthAttr);
747 ret = CRYPT_ConstructAttributes(&info->UnauthAttrs,
748 (CRYPT_ATTRIBUTES *)&in->cUnauthAttr);
753 static void CSignerInfo_Free(CMSG_SIGNER_INFO *info)
757 CryptMemFree(info->Issuer.pbData);
758 CryptMemFree(info->SerialNumber.pbData);
759 CryptMemFree(info->HashAlgorithm.Parameters.pbData);
760 CryptMemFree(info->EncryptedHash.pbData);
761 for (i = 0; i < info->AuthAttrs.cAttr; i++)
763 for (j = 0; j < info->AuthAttrs.rgAttr[i].cValue; j++)
764 CryptMemFree(info->AuthAttrs.rgAttr[i].rgValue[j].pbData);
765 CryptMemFree(info->AuthAttrs.rgAttr[i].rgValue);
766 CryptMemFree(info->AuthAttrs.rgAttr[i].pszObjId);
768 CryptMemFree(info->AuthAttrs.rgAttr);
769 for (i = 0; i < info->UnauthAttrs.cAttr; i++)
771 for (j = 0; j < info->UnauthAttrs.rgAttr[i].cValue; j++)
772 CryptMemFree(info->UnauthAttrs.rgAttr[i].rgValue[j].pbData);
773 CryptMemFree(info->UnauthAttrs.rgAttr[i].rgValue);
774 CryptMemFree(info->UnauthAttrs.rgAttr[i].pszObjId);
776 CryptMemFree(info->UnauthAttrs.rgAttr);
779 typedef struct _CSignedEncodeMsg
782 CRYPT_DATA_BLOB data;
783 CRYPT_SIGNED_INFO info;
784 CSignerHandles *signerHandles;
787 static void CSignedEncodeMsg_Close(HCRYPTMSG hCryptMsg)
789 CSignedEncodeMsg *msg = (CSignedEncodeMsg *)hCryptMsg;
792 CryptMemFree(msg->data.pbData);
793 CRYPT_FreeBlobArray((BlobArray *)&msg->info.cCertEncoded);
794 CRYPT_FreeBlobArray((BlobArray *)&msg->info.cCrlEncoded);
795 for (i = 0; i < msg->info.cSignerInfo; i++)
797 CSignerInfo_Free(&msg->info.rgSignerInfo[i]);
798 CryptDestroyKey(msg->signerHandles[i].key);
799 CryptDestroyHash(msg->signerHandles[i].contentHash);
800 CryptDestroyHash(msg->signerHandles[i].authAttrHash);
801 CryptReleaseContext(msg->signerHandles[i].prov, 0);
803 CryptMemFree(msg->signerHandles);
804 CryptMemFree(msg->info.rgSignerInfo);
807 static BOOL CSignedEncodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
808 DWORD dwIndex, void *pvData, DWORD *pcbData)
810 CSignedEncodeMsg *msg = (CSignedEncodeMsg *)hCryptMsg;
815 case CMSG_CONTENT_PARAM:
817 CRYPT_CONTENT_INFO info;
819 ret = CryptMsgGetParam(hCryptMsg, CMSG_BARE_CONTENT_PARAM, 0, NULL,
820 &info.Content.cbData);
823 info.Content.pbData = CryptMemAlloc(info.Content.cbData);
824 if (info.Content.pbData)
826 ret = CryptMsgGetParam(hCryptMsg, CMSG_BARE_CONTENT_PARAM, 0,
827 info.Content.pbData, &info.Content.cbData);
830 char oid_rsa_signed[] = szOID_RSA_signedData;
832 info.pszObjId = oid_rsa_signed;
833 ret = CryptEncodeObjectEx(X509_ASN_ENCODING,
834 PKCS_CONTENT_INFO, &info, 0, NULL, pvData, pcbData);
836 CryptMemFree(info.Content.pbData);
843 case CMSG_BARE_CONTENT_PARAM:
845 CRYPT_SIGNED_INFO info;
846 char oid_rsa_data[] = szOID_RSA_data;
848 memcpy(&info, &msg->info, sizeof(info));
849 /* Quirk: OID is only encoded messages if an update has happened */
850 if (msg->base.state != MsgStateInit)
851 info.content.pszObjId = oid_rsa_data;
853 info.content.pszObjId = NULL;
854 if (msg->data.cbData)
856 CRYPT_DATA_BLOB blob = { msg->data.cbData, msg->data.pbData };
858 ret = CryptEncodeObjectEx(X509_ASN_ENCODING, X509_OCTET_STRING,
859 &blob, CRYPT_ENCODE_ALLOC_FLAG, NULL,
860 &info.content.Content.pbData, &info.content.Content.cbData);
864 info.content.Content.cbData = 0;
865 info.content.Content.pbData = NULL;
870 ret = CRYPT_AsnEncodePKCSSignedInfo(&info, pvData, pcbData);
871 LocalFree(info.content.Content.pbData);
875 case CMSG_COMPUTED_HASH_PARAM:
876 if (dwIndex >= msg->info.cSignerInfo)
877 SetLastError(CRYPT_E_INVALID_INDEX);
879 ret = CryptGetHashParam(msg->signerHandles[dwIndex].contentHash,
880 HP_HASHVAL, pvData, pcbData, 0);
882 case CMSG_ENCODED_SIGNER:
883 if (dwIndex >= msg->info.cSignerInfo)
884 SetLastError(CRYPT_E_INVALID_INDEX);
886 ret = CryptEncodeObjectEx(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
887 PKCS7_SIGNER_INFO, &msg->info.rgSignerInfo[dwIndex], 0, NULL,
890 case CMSG_VERSION_PARAM:
891 ret = CRYPT_CopyParam(pvData, pcbData, &msg->info.version,
892 sizeof(msg->info.version));
895 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
900 static BOOL CSignedEncodeMsg_UpdateHash(CSignedEncodeMsg *msg,
901 const BYTE *pbData, DWORD cbData)
906 TRACE("(%p, %p, %d)\n", msg, pbData, cbData);
908 for (i = 0; ret && i < msg->info.cSignerInfo; i++)
909 ret = CryptHashData(msg->signerHandles[i].contentHash, pbData, cbData,
914 static BOOL CRYPT_AppendAttribute(CRYPT_ATTRIBUTES *out,
915 const CRYPT_ATTRIBUTE *in)
919 out->rgAttr = CryptMemRealloc(out->rgAttr,
920 (out->cAttr + 1) * sizeof(CRYPT_ATTRIBUTE));
923 ret = CRYPT_ConstructAttribute(&out->rgAttr[out->cAttr], in);
930 static BOOL CSignedEncodeMsg_AppendMessageDigestAttribute(CSignedEncodeMsg *msg,
935 CRYPT_HASH_BLOB hash = { 0, NULL }, encodedHash = { 0, NULL };
936 char messageDigest[] = szOID_RSA_messageDigest;
937 CRYPT_ATTRIBUTE messageDigestAttr = { messageDigest, 1, &encodedHash };
939 size = sizeof(DWORD);
940 ret = CryptGetHashParam(msg->signerHandles[signerIndex].contentHash,
941 HP_HASHSIZE, (LPBYTE)&hash.cbData, &size, 0);
944 hash.pbData = CryptMemAlloc(hash.cbData);
945 ret = CryptGetHashParam(msg->signerHandles[signerIndex].contentHash,
946 HP_HASHVAL, hash.pbData, &hash.cbData, 0);
949 ret = CRYPT_AsnEncodeOctets(0, NULL, &hash, CRYPT_ENCODE_ALLOC_FLAG,
950 NULL, (LPBYTE)&encodedHash.pbData, &encodedHash.cbData);
953 ret = CRYPT_AppendAttribute(
954 &msg->info.rgSignerInfo[signerIndex].AuthAttrs,
956 LocalFree(encodedHash.pbData);
959 CryptMemFree(hash.pbData);
964 static BOOL CSignedEncodeMsg_UpdateAuthenticatedAttributes(
965 CSignedEncodeMsg *msg)
970 TRACE("(%p)\n", msg);
972 for (i = 0; ret && i < msg->info.cSignerInfo; i++)
974 if (msg->info.rgSignerInfo[i].AuthAttrs.cAttr)
976 BYTE oid_rsa_data_encoded[] = { 0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,
977 0x0d,0x01,0x07,0x01 };
978 CRYPT_DATA_BLOB content = { sizeof(oid_rsa_data_encoded),
979 oid_rsa_data_encoded };
980 char contentType[] = szOID_RSA_contentType;
981 CRYPT_ATTRIBUTE contentTypeAttr = { contentType, 1, &content };
983 /* FIXME: does this depend on inner OID? */
984 ret = CRYPT_AppendAttribute(&msg->info.rgSignerInfo[i].AuthAttrs,
987 ret = CSignedEncodeMsg_AppendMessageDigestAttribute(msg, i);
993 ret = CryptEncodeObjectEx(X509_ASN_ENCODING, PKCS_ATTRIBUTES,
994 &msg->info.rgSignerInfo[i].AuthAttrs, CRYPT_ENCODE_ALLOC_FLAG,
995 NULL, (LPBYTE)&encodedAttrs, &size);
998 ret = CryptHashData(msg->signerHandles[i].authAttrHash,
999 encodedAttrs, size, 0);
1000 LocalFree(encodedAttrs);
1005 TRACE("returning %d\n", ret);
1009 static void CRYPT_ReverseBytes(CRYPT_HASH_BLOB *hash)
1014 for (i = 0; i < hash->cbData / 2; i++)
1016 tmp = hash->pbData[hash->cbData - i - 1];
1017 hash->pbData[hash->cbData - i - 1] = hash->pbData[i];
1018 hash->pbData[i] = tmp;
1022 static BOOL CSignedEncodeMsg_Sign(CSignedEncodeMsg *msg)
1027 TRACE("(%p)\n", msg);
1029 for (i = 0; ret && i < msg->info.cSignerInfo; i++)
1033 if (msg->info.rgSignerInfo[i].AuthAttrs.cAttr)
1034 hash = msg->signerHandles[i].authAttrHash;
1036 hash = msg->signerHandles[i].contentHash;
1037 ret = CryptSignHashW(hash, AT_SIGNATURE, NULL, 0, NULL,
1038 &msg->info.rgSignerInfo[i].EncryptedHash.cbData);
1041 msg->info.rgSignerInfo[i].EncryptedHash.pbData =
1042 CryptMemAlloc(msg->info.rgSignerInfo[i].EncryptedHash.cbData);
1043 if (msg->info.rgSignerInfo[i].EncryptedHash.pbData)
1045 ret = CryptSignHashW(hash, AT_SIGNATURE, NULL, 0,
1046 msg->info.rgSignerInfo[i].EncryptedHash.pbData,
1047 &msg->info.rgSignerInfo[i].EncryptedHash.cbData);
1049 CRYPT_ReverseBytes(&msg->info.rgSignerInfo[i].EncryptedHash);
1058 static BOOL CSignedEncodeMsg_Update(HCRYPTMSG hCryptMsg, const BYTE *pbData,
1059 DWORD cbData, BOOL fFinal)
1061 CSignedEncodeMsg *msg = (CSignedEncodeMsg *)hCryptMsg;
1064 if (msg->base.streamed || (msg->base.open_flags & CMSG_DETACHED_FLAG))
1066 ret = CSignedEncodeMsg_UpdateHash(msg, pbData, cbData);
1069 ret = CSignedEncodeMsg_UpdateAuthenticatedAttributes(msg);
1071 ret = CSignedEncodeMsg_Sign(msg);
1073 if (msg->base.streamed)
1074 FIXME("streamed partial stub\n");
1079 SetLastError(CRYPT_E_MSG_ERROR);
1084 msg->data.pbData = CryptMemAlloc(cbData);
1085 if (msg->data.pbData)
1087 memcpy(msg->data.pbData, pbData, cbData);
1088 msg->data.cbData = cbData;
1095 ret = CSignedEncodeMsg_UpdateHash(msg, pbData, cbData);
1097 ret = CSignedEncodeMsg_UpdateAuthenticatedAttributes(msg);
1099 ret = CSignedEncodeMsg_Sign(msg);
1105 static HCRYPTMSG CSignedEncodeMsg_Open(DWORD dwFlags,
1106 const void *pvMsgEncodeInfo, LPSTR pszInnerContentObjID,
1107 PCMSG_STREAM_INFO pStreamInfo)
1109 const CMSG_SIGNED_ENCODE_INFO_WITH_CMS *info =
1110 (const CMSG_SIGNED_ENCODE_INFO_WITH_CMS *)pvMsgEncodeInfo;
1112 CSignedEncodeMsg *msg;
1114 if (info->cbSize != sizeof(CMSG_SIGNED_ENCODE_INFO) &&
1115 info->cbSize != sizeof(CMSG_SIGNED_ENCODE_INFO_WITH_CMS))
1117 SetLastError(E_INVALIDARG);
1120 if (info->cbSize == sizeof(CMSG_SIGNED_ENCODE_INFO_WITH_CMS))
1122 FIXME("CMSG_SIGNED_ENCODE_INFO with CMS fields unsupported\n");
1125 for (i = 0; i < info->cSigners; i++)
1126 if (!CRYPT_IsValidSigner(&info->rgSigners[i]))
1128 msg = CryptMemAlloc(sizeof(CSignedEncodeMsg));
1133 CryptMsgBase_Init((CryptMsgBase *)msg, dwFlags, pStreamInfo,
1134 CSignedEncodeMsg_Close, CSignedEncodeMsg_GetParam,
1135 CSignedEncodeMsg_Update);
1136 msg->data.cbData = 0;
1137 msg->data.pbData = NULL;
1138 memset(&msg->info, 0, sizeof(msg->info));
1139 msg->info.version = CMSG_SIGNED_DATA_V1;
1142 msg->signerHandles =
1143 CryptMemAlloc(info->cSigners * sizeof(CSignerHandles));
1144 if (msg->signerHandles)
1145 msg->info.rgSignerInfo =
1146 CryptMemAlloc(info->cSigners * sizeof(CMSG_SIGNER_INFO));
1150 msg->info.rgSignerInfo = NULL;
1152 if (msg->info.rgSignerInfo)
1154 msg->info.cSignerInfo = info->cSigners;
1155 memset(msg->signerHandles, 0,
1156 msg->info.cSignerInfo * sizeof(CSignerHandles));
1157 memset(msg->info.rgSignerInfo, 0,
1158 msg->info.cSignerInfo * sizeof(CMSG_SIGNER_INFO));
1159 for (i = 0; ret && i < msg->info.cSignerInfo; i++)
1160 ret = CSignerInfo_Construct(&msg->signerHandles[i],
1161 &msg->info.rgSignerInfo[i], &info->rgSigners[i], dwFlags);
1167 ret = CRYPT_ConstructBlobArray((BlobArray *)&msg->info.cCertEncoded,
1168 (const BlobArray *)&info->cCertEncoded);
1170 ret = CRYPT_ConstructBlobArray((BlobArray *)&msg->info.cCrlEncoded,
1171 (const BlobArray *)&info->cCrlEncoded);
1174 CSignedEncodeMsg_Close(msg);
1181 static inline const char *MSG_TYPE_STR(DWORD type)
1185 #define _x(x) case (x): return #x
1189 _x(CMSG_SIGNED_AND_ENVELOPED);
1194 return wine_dbg_sprintf("unknown (%d)", type);
1198 HCRYPTMSG WINAPI CryptMsgOpenToEncode(DWORD dwMsgEncodingType, DWORD dwFlags,
1199 DWORD dwMsgType, const void *pvMsgEncodeInfo, LPSTR pszInnerContentObjID,
1200 PCMSG_STREAM_INFO pStreamInfo)
1202 HCRYPTMSG msg = NULL;
1204 TRACE("(%08x, %08x, %08x, %p, %s, %p)\n", dwMsgEncodingType, dwFlags,
1205 dwMsgType, pvMsgEncodeInfo, debugstr_a(pszInnerContentObjID), pStreamInfo);
1207 if (GET_CMSG_ENCODING_TYPE(dwMsgEncodingType) != PKCS_7_ASN_ENCODING)
1209 SetLastError(E_INVALIDARG);
1215 msg = CDataEncodeMsg_Open(dwFlags, pvMsgEncodeInfo,
1216 pszInnerContentObjID, pStreamInfo);
1219 msg = CHashEncodeMsg_Open(dwFlags, pvMsgEncodeInfo,
1220 pszInnerContentObjID, pStreamInfo);
1223 msg = CSignedEncodeMsg_Open(dwFlags, pvMsgEncodeInfo,
1224 pszInnerContentObjID, pStreamInfo);
1226 case CMSG_ENVELOPED:
1227 FIXME("unimplemented for type %s\n", MSG_TYPE_STR(dwMsgType));
1229 case CMSG_SIGNED_AND_ENVELOPED:
1230 case CMSG_ENCRYPTED:
1231 /* defined but invalid, fall through */
1233 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1238 typedef struct _CDecodeMsg
1242 HCRYPTPROV crypt_prov;
1245 CRYPT_SIGNED_INFO *signedInfo;
1247 CRYPT_DATA_BLOB msg_data;
1248 PCONTEXT_PROPERTY_LIST properties;
1251 static void CDecodeMsg_Close(HCRYPTMSG hCryptMsg)
1253 CDecodeMsg *msg = (CDecodeMsg *)hCryptMsg;
1255 if (msg->base.open_flags & CMSG_CRYPT_RELEASE_CONTEXT_FLAG)
1256 CryptReleaseContext(msg->crypt_prov, 0);
1261 CryptDestroyHash(msg->u.hash);
1264 LocalFree(msg->u.signedInfo);
1267 CryptMemFree(msg->msg_data.pbData);
1268 ContextPropertyList_Free(msg->properties);
1271 static BOOL CDecodeMsg_CopyData(CDecodeMsg *msg, const BYTE *pbData,
1278 if (msg->msg_data.cbData)
1279 msg->msg_data.pbData = CryptMemRealloc(msg->msg_data.pbData,
1280 msg->msg_data.cbData + cbData);
1282 msg->msg_data.pbData = CryptMemAlloc(cbData);
1283 if (msg->msg_data.pbData)
1285 memcpy(msg->msg_data.pbData + msg->msg_data.cbData, pbData, cbData);
1286 msg->msg_data.cbData += cbData;
1294 static BOOL CDecodeMsg_DecodeDataContent(CDecodeMsg *msg, CRYPT_DER_BLOB *blob)
1297 CRYPT_DATA_BLOB *data;
1300 ret = CryptDecodeObjectEx(X509_ASN_ENCODING, X509_OCTET_STRING,
1301 blob->pbData, blob->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, (LPBYTE)&data,
1305 ret = ContextPropertyList_SetProperty(msg->properties,
1306 CMSG_CONTENT_PARAM, data->pbData, data->cbData);
1312 static void CDecodeMsg_SaveAlgorithmID(CDecodeMsg *msg, DWORD param,
1313 const CRYPT_ALGORITHM_IDENTIFIER *id)
1315 static const BYTE nullParams[] = { ASN_NULL, 0 };
1316 CRYPT_ALGORITHM_IDENTIFIER *copy;
1317 DWORD len = sizeof(CRYPT_ALGORITHM_IDENTIFIER);
1319 /* Linearize algorithm id */
1320 len += strlen(id->pszObjId) + 1;
1321 len += id->Parameters.cbData;
1322 copy = CryptMemAlloc(len);
1326 (LPSTR)((BYTE *)copy + sizeof(CRYPT_ALGORITHM_IDENTIFIER));
1327 strcpy(copy->pszObjId, id->pszObjId);
1328 copy->Parameters.pbData = (BYTE *)copy->pszObjId + strlen(id->pszObjId)
1330 /* Trick: omit NULL parameters */
1331 if (id->Parameters.cbData == sizeof(nullParams) &&
1332 !memcmp(id->Parameters.pbData, nullParams, sizeof(nullParams)))
1334 copy->Parameters.cbData = 0;
1335 len -= sizeof(nullParams);
1338 copy->Parameters.cbData = id->Parameters.cbData;
1339 if (copy->Parameters.cbData)
1340 memcpy(copy->Parameters.pbData, id->Parameters.pbData,
1341 id->Parameters.cbData);
1342 ContextPropertyList_SetProperty(msg->properties, param, (BYTE *)copy,
1348 static inline void CRYPT_FixUpAlgorithmID(CRYPT_ALGORITHM_IDENTIFIER *id)
1350 id->pszObjId = (LPSTR)((BYTE *)id + sizeof(CRYPT_ALGORITHM_IDENTIFIER));
1351 id->Parameters.pbData = (BYTE *)id->pszObjId + strlen(id->pszObjId) + 1;
1354 static BOOL CDecodeMsg_DecodeHashedContent(CDecodeMsg *msg,
1355 CRYPT_DER_BLOB *blob)
1358 CRYPT_DIGESTED_DATA *digestedData;
1361 ret = CRYPT_AsnDecodePKCSDigestedData(blob->pbData, blob->cbData,
1362 CRYPT_DECODE_ALLOC_FLAG, NULL, (CRYPT_DIGESTED_DATA *)&digestedData,
1366 ContextPropertyList_SetProperty(msg->properties, CMSG_VERSION_PARAM,
1367 (const BYTE *)&digestedData->version, sizeof(digestedData->version));
1368 CDecodeMsg_SaveAlgorithmID(msg, CMSG_HASH_ALGORITHM_PARAM,
1369 &digestedData->DigestAlgorithm);
1370 ContextPropertyList_SetProperty(msg->properties,
1371 CMSG_INNER_CONTENT_TYPE_PARAM,
1372 (const BYTE *)digestedData->ContentInfo.pszObjId,
1373 digestedData->ContentInfo.pszObjId ?
1374 strlen(digestedData->ContentInfo.pszObjId) + 1 : 0);
1375 if (digestedData->ContentInfo.Content.cbData)
1376 CDecodeMsg_DecodeDataContent(msg,
1377 &digestedData->ContentInfo.Content);
1379 ContextPropertyList_SetProperty(msg->properties,
1380 CMSG_CONTENT_PARAM, NULL, 0);
1381 ContextPropertyList_SetProperty(msg->properties, CMSG_HASH_DATA_PARAM,
1382 digestedData->hash.pbData, digestedData->hash.cbData);
1383 LocalFree(digestedData);
1388 static BOOL CDecodeMsg_DecodeSignedContent(CDecodeMsg *msg,
1389 CRYPT_DER_BLOB *blob)
1392 CRYPT_SIGNED_INFO *signedInfo;
1395 ret = CRYPT_AsnDecodePKCSSignedInfo(blob->pbData, blob->cbData,
1396 CRYPT_DECODE_ALLOC_FLAG, NULL, (CRYPT_SIGNED_INFO *)&signedInfo,
1399 msg->u.signedInfo = signedInfo;
1402 /* Decodes the content in blob as the type given, and updates the value
1403 * (type, parameters, etc.) of msg based on what blob contains.
1404 * It doesn't just use msg's type, to allow a recursive call from an implicitly
1405 * typed message once the outer content info has been decoded.
1407 static BOOL CDecodeMsg_DecodeContent(CDecodeMsg *msg, CRYPT_DER_BLOB *blob,
1415 if ((ret = CDecodeMsg_DecodeDataContent(msg, blob)))
1416 msg->type = CMSG_DATA;
1419 if ((ret = CDecodeMsg_DecodeHashedContent(msg, blob)))
1420 msg->type = CMSG_HASHED;
1422 case CMSG_ENVELOPED:
1423 FIXME("unimplemented for type %s\n", MSG_TYPE_STR(type));
1427 if ((ret = CDecodeMsg_DecodeSignedContent(msg, blob)))
1428 msg->type = CMSG_SIGNED;
1432 CRYPT_CONTENT_INFO *info;
1435 ret = CryptDecodeObjectEx(X509_ASN_ENCODING, PKCS_CONTENT_INFO,
1436 msg->msg_data.pbData, msg->msg_data.cbData, CRYPT_DECODE_ALLOC_FLAG,
1437 NULL, (LPBYTE)&info, &size);
1440 if (!strcmp(info->pszObjId, szOID_RSA_data))
1441 ret = CDecodeMsg_DecodeContent(msg, &info->Content, CMSG_DATA);
1442 else if (!strcmp(info->pszObjId, szOID_RSA_digestedData))
1443 ret = CDecodeMsg_DecodeContent(msg, &info->Content,
1445 else if (!strcmp(info->pszObjId, szOID_RSA_envelopedData))
1446 ret = CDecodeMsg_DecodeContent(msg, &info->Content,
1448 else if (!strcmp(info->pszObjId, szOID_RSA_signedData))
1449 ret = CDecodeMsg_DecodeContent(msg, &info->Content,
1453 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1463 static BOOL CDecodeMsg_Update(HCRYPTMSG hCryptMsg, const BYTE *pbData,
1464 DWORD cbData, BOOL fFinal)
1466 CDecodeMsg *msg = (CDecodeMsg *)hCryptMsg;
1469 TRACE("(%p, %p, %d, %d)\n", hCryptMsg, pbData, cbData, fFinal);
1471 if (msg->base.streamed)
1473 ret = CDecodeMsg_CopyData(msg, pbData, cbData);
1474 FIXME("(%p, %p, %d, %d): streamed update stub\n", hCryptMsg, pbData,
1480 SetLastError(CRYPT_E_MSG_ERROR);
1483 ret = CDecodeMsg_CopyData(msg, pbData, cbData);
1485 ret = CDecodeMsg_DecodeContent(msg, &msg->msg_data, msg->type);
1492 static BOOL CDecodeHashMsg_GetParam(CDecodeMsg *msg, DWORD dwParamType,
1493 DWORD dwIndex, void *pvData, DWORD *pcbData)
1497 switch (dwParamType)
1499 case CMSG_TYPE_PARAM:
1500 ret = CRYPT_CopyParam(pvData, pcbData, &msg->type, sizeof(msg->type));
1502 case CMSG_HASH_ALGORITHM_PARAM:
1504 CRYPT_DATA_BLOB blob;
1506 ret = ContextPropertyList_FindProperty(msg->properties, dwParamType,
1510 ret = CRYPT_CopyParam(pvData, pcbData, blob.pbData, blob.cbData);
1512 CRYPT_FixUpAlgorithmID((CRYPT_ALGORITHM_IDENTIFIER *)pvData);
1515 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1518 case CMSG_COMPUTED_HASH_PARAM:
1521 CRYPT_ALGORITHM_IDENTIFIER *hashAlgoID = NULL;
1525 CryptMsgGetParam(msg, CMSG_HASH_ALGORITHM_PARAM, 0, NULL, &size);
1526 hashAlgoID = CryptMemAlloc(size);
1527 ret = CryptMsgGetParam(msg, CMSG_HASH_ALGORITHM_PARAM, 0,
1530 algID = CertOIDToAlgId(hashAlgoID->pszObjId);
1531 ret = CryptCreateHash(msg->crypt_prov, algID, 0, 0, &msg->u.hash);
1534 CRYPT_DATA_BLOB content;
1536 ret = ContextPropertyList_FindProperty(msg->properties,
1537 CMSG_CONTENT_PARAM, &content);
1539 ret = CryptHashData(msg->u.hash, content.pbData,
1542 CryptMemFree(hashAlgoID);
1547 ret = CryptGetHashParam(msg->u.hash, HP_HASHVAL, pvData, pcbData,
1552 CRYPT_DATA_BLOB blob;
1554 ret = ContextPropertyList_FindProperty(msg->properties, dwParamType,
1557 ret = CRYPT_CopyParam(pvData, pcbData, blob.pbData, blob.cbData);
1559 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1565 /* nextData is an in/out parameter - on input it's the memory location in
1566 * which a copy of in's data should be made, and on output it's the memory
1567 * location immediately after out's copy of in's data.
1569 static inline void CRYPT_CopyBlob(CRYPT_DATA_BLOB *out,
1570 const CRYPT_DATA_BLOB *in, LPBYTE *nextData)
1572 out->cbData = in->cbData;
1575 out->pbData = *nextData;
1576 memcpy(out->pbData, in->pbData, in->cbData);
1577 *nextData += in->cbData;
1581 static inline void CRYPT_CopyAlgorithmId(CRYPT_ALGORITHM_IDENTIFIER *out,
1582 const CRYPT_ALGORITHM_IDENTIFIER *in, LPBYTE *nextData)
1586 out->pszObjId = (LPSTR)*nextData;
1587 strcpy(out->pszObjId, in->pszObjId);
1588 *nextData += strlen(out->pszObjId) + 1;
1590 CRYPT_CopyBlob(&out->Parameters, &in->Parameters, nextData);
1593 static inline void CRYPT_CopyAttributes(CRYPT_ATTRIBUTES *out,
1594 const CRYPT_ATTRIBUTES *in, LPBYTE *nextData)
1596 out->cAttr = in->cAttr;
1601 if ((*nextData - (LPBYTE)0) % sizeof(DWORD))
1602 *nextData += (*nextData - (LPBYTE)0) % sizeof(DWORD);
1603 out->rgAttr = (CRYPT_ATTRIBUTE *)*nextData;
1604 *nextData += in->cAttr * sizeof(CRYPT_ATTRIBUTE);
1605 for (i = 0; i < in->cAttr; i++)
1607 if (in->rgAttr[i].pszObjId)
1609 out->rgAttr[i].pszObjId = (LPSTR)*nextData;
1610 strcpy(out->rgAttr[i].pszObjId, in->rgAttr[i].pszObjId);
1611 *nextData += strlen(in->rgAttr[i].pszObjId) + 1;
1613 if (in->rgAttr[i].cValue)
1617 out->rgAttr[i].cValue = in->rgAttr[i].cValue;
1618 if ((*nextData - (LPBYTE)0) % sizeof(DWORD))
1619 *nextData += (*nextData - (LPBYTE)0) % sizeof(DWORD);
1620 out->rgAttr[i].rgValue = (PCRYPT_DATA_BLOB)*nextData;
1621 for (j = 0; j < in->rgAttr[i].cValue; j++)
1622 CRYPT_CopyBlob(&out->rgAttr[i].rgValue[j],
1623 &in->rgAttr[i].rgValue[j], nextData);
1629 static DWORD CRYPT_SizeOfAttributes(const CRYPT_ATTRIBUTES *attr)
1631 DWORD size = attr->cAttr * sizeof(CRYPT_ATTRIBUTE), i, j;
1633 for (i = 0; i < attr->cAttr; i++)
1635 if (attr->rgAttr[i].pszObjId)
1636 size += strlen(attr->rgAttr[i].pszObjId) + 1;
1638 if (size % sizeof(DWORD))
1639 size += size % sizeof(DWORD);
1640 size += attr->rgAttr[i].cValue * sizeof(CRYPT_DATA_BLOB);
1641 for (j = 0; j < attr->rgAttr[i].cValue; j++)
1642 size += attr->rgAttr[i].rgValue[j].cbData;
1647 static BOOL CRYPT_CopySignerInfo(void *pvData, DWORD *pcbData,
1648 const CMSG_SIGNER_INFO *in)
1650 DWORD size = sizeof(CMSG_SIGNER_INFO);
1653 size += in->Issuer.cbData;
1654 size += in->SerialNumber.cbData;
1655 if (in->HashAlgorithm.pszObjId)
1656 size += strlen(in->HashAlgorithm.pszObjId) + 1;
1657 size += in->HashAlgorithm.Parameters.cbData;
1658 if (in->HashEncryptionAlgorithm.pszObjId)
1659 size += strlen(in->HashEncryptionAlgorithm.pszObjId) + 1;
1660 size += in->HashEncryptionAlgorithm.Parameters.cbData;
1661 size += in->EncryptedHash.cbData;
1663 if (size % sizeof(DWORD))
1664 size += size % sizeof(DWORD);
1665 size += CRYPT_SizeOfAttributes(&in->AuthAttrs);
1666 size += CRYPT_SizeOfAttributes(&in->UnauthAttrs);
1672 else if (*pcbData < size)
1675 SetLastError(ERROR_MORE_DATA);
1680 LPBYTE nextData = (BYTE *)pvData + sizeof(CMSG_SIGNER_INFO);
1681 CMSG_SIGNER_INFO *out = (CMSG_SIGNER_INFO *)pvData;
1683 out->dwVersion = in->dwVersion;
1684 CRYPT_CopyBlob(&out->Issuer, &in->Issuer, &nextData);
1685 CRYPT_CopyBlob(&out->SerialNumber, &in->SerialNumber, &nextData);
1686 CRYPT_CopyAlgorithmId(&out->HashAlgorithm, &in->HashAlgorithm,
1688 CRYPT_CopyAlgorithmId(&out->HashEncryptionAlgorithm,
1689 &in->HashEncryptionAlgorithm, &nextData);
1690 CRYPT_CopyBlob(&out->EncryptedHash, &in->EncryptedHash, &nextData);
1692 if ((nextData - (LPBYTE)0) % sizeof(DWORD))
1693 nextData += (nextData - (LPBYTE)0) % sizeof(DWORD);
1694 CRYPT_CopyAttributes(&out->AuthAttrs, &in->AuthAttrs, &nextData);
1695 CRYPT_CopyAttributes(&out->UnauthAttrs, &in->UnauthAttrs, &nextData);
1701 static BOOL CRYPT_CopySignerCertInfo(void *pvData, DWORD *pcbData,
1702 const CMSG_SIGNER_INFO *in)
1704 DWORD size = sizeof(CERT_INFO);
1707 size += in->Issuer.cbData;
1708 size += in->SerialNumber.cbData;
1714 else if (*pcbData < size)
1717 SetLastError(ERROR_MORE_DATA);
1722 LPBYTE nextData = (BYTE *)pvData + sizeof(CERT_INFO);
1723 CERT_INFO *out = (CERT_INFO *)pvData;
1725 memset(out, 0, sizeof(CERT_INFO));
1726 CRYPT_CopyBlob(&out->Issuer, &in->Issuer, &nextData);
1727 CRYPT_CopyBlob(&out->SerialNumber, &in->SerialNumber, &nextData);
1733 static BOOL CDecodeSignedMsg_GetParam(CDecodeMsg *msg, DWORD dwParamType,
1734 DWORD dwIndex, void *pvData, DWORD *pcbData)
1738 switch (dwParamType)
1740 case CMSG_TYPE_PARAM:
1741 ret = CRYPT_CopyParam(pvData, pcbData, &msg->type, sizeof(msg->type));
1743 case CMSG_CONTENT_PARAM:
1744 if (msg->u.signedInfo)
1746 if (!strcmp(msg->u.signedInfo->content.pszObjId, szOID_RSA_data))
1748 CRYPT_DATA_BLOB *blob;
1751 ret = CryptDecodeObjectEx(X509_ASN_ENCODING, X509_OCTET_STRING,
1752 msg->u.signedInfo->content.Content.pbData,
1753 msg->u.signedInfo->content.Content.cbData,
1754 CRYPT_DECODE_ALLOC_FLAG, NULL, (LPBYTE)&blob, &size);
1757 ret = CRYPT_CopyParam(pvData, pcbData, blob->pbData,
1763 ret = CRYPT_CopyParam(pvData, pcbData,
1764 msg->u.signedInfo->content.Content.pbData,
1765 msg->u.signedInfo->content.Content.cbData);
1768 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1770 case CMSG_INNER_CONTENT_TYPE_PARAM:
1771 if (msg->u.signedInfo)
1772 ret = CRYPT_CopyParam(pvData, pcbData,
1773 msg->u.signedInfo->content.pszObjId,
1774 strlen(msg->u.signedInfo->content.pszObjId) + 1);
1776 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1778 case CMSG_SIGNER_COUNT_PARAM:
1779 if (msg->u.signedInfo)
1780 ret = CRYPT_CopyParam(pvData, pcbData,
1781 &msg->u.signedInfo->cSignerInfo, sizeof(DWORD));
1783 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1785 case CMSG_SIGNER_INFO_PARAM:
1786 if (msg->u.signedInfo)
1788 if (dwIndex >= msg->u.signedInfo->cSignerInfo)
1789 SetLastError(CRYPT_E_INVALID_INDEX);
1791 ret = CRYPT_CopySignerInfo(pvData, pcbData,
1792 &msg->u.signedInfo->rgSignerInfo[dwIndex]);
1795 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1797 case CMSG_SIGNER_CERT_INFO_PARAM:
1798 if (msg->u.signedInfo)
1800 if (dwIndex >= msg->u.signedInfo->cSignerInfo)
1801 SetLastError(CRYPT_E_INVALID_INDEX);
1803 ret = CRYPT_CopySignerCertInfo(pvData, pcbData,
1804 &msg->u.signedInfo->rgSignerInfo[dwIndex]);
1807 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1809 case CMSG_CERT_COUNT_PARAM:
1810 if (msg->u.signedInfo)
1811 ret = CRYPT_CopyParam(pvData, pcbData,
1812 &msg->u.signedInfo->cCertEncoded, sizeof(DWORD));
1814 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1816 case CMSG_CERT_PARAM:
1817 if (msg->u.signedInfo)
1819 if (dwIndex >= msg->u.signedInfo->cCertEncoded)
1820 SetLastError(CRYPT_E_INVALID_INDEX);
1822 ret = CRYPT_CopyParam(pvData, pcbData,
1823 msg->u.signedInfo->rgCertEncoded[dwIndex].pbData,
1824 msg->u.signedInfo->rgCertEncoded[dwIndex].cbData);
1827 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1829 case CMSG_CRL_COUNT_PARAM:
1830 if (msg->u.signedInfo)
1831 ret = CRYPT_CopyParam(pvData, pcbData,
1832 &msg->u.signedInfo->cCrlEncoded, sizeof(DWORD));
1834 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1836 case CMSG_CRL_PARAM:
1837 if (msg->u.signedInfo)
1839 if (dwIndex >= msg->u.signedInfo->cCrlEncoded)
1840 SetLastError(CRYPT_E_INVALID_INDEX);
1842 ret = CRYPT_CopyParam(pvData, pcbData,
1843 msg->u.signedInfo->rgCrlEncoded[dwIndex].pbData,
1844 msg->u.signedInfo->rgCrlEncoded[dwIndex].cbData);
1847 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1849 case CMSG_ATTR_CERT_COUNT_PARAM:
1850 if (msg->u.signedInfo)
1852 DWORD attrCertCount = 0;
1854 ret = CRYPT_CopyParam(pvData, pcbData,
1855 &attrCertCount, sizeof(DWORD));
1858 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1860 case CMSG_ATTR_CERT_PARAM:
1861 if (msg->u.signedInfo)
1862 SetLastError(CRYPT_E_INVALID_INDEX);
1864 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1867 FIXME("unimplemented for %d\n", dwParamType);
1868 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1873 static BOOL CDecodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
1874 DWORD dwIndex, void *pvData, DWORD *pcbData)
1876 CDecodeMsg *msg = (CDecodeMsg *)hCryptMsg;
1882 ret = CDecodeHashMsg_GetParam(msg, dwParamType, dwIndex, pvData,
1886 ret = CDecodeSignedMsg_GetParam(msg, dwParamType, dwIndex, pvData,
1890 switch (dwParamType)
1892 case CMSG_TYPE_PARAM:
1893 ret = CRYPT_CopyParam(pvData, pcbData, &msg->type,
1898 CRYPT_DATA_BLOB blob;
1900 ret = ContextPropertyList_FindProperty(msg->properties, dwParamType,
1903 ret = CRYPT_CopyParam(pvData, pcbData, blob.pbData,
1906 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1913 HCRYPTMSG WINAPI CryptMsgOpenToDecode(DWORD dwMsgEncodingType, DWORD dwFlags,
1914 DWORD dwMsgType, HCRYPTPROV_LEGACY hCryptProv, PCERT_INFO pRecipientInfo,
1915 PCMSG_STREAM_INFO pStreamInfo)
1919 TRACE("(%08x, %08x, %08x, %08lx, %p, %p)\n", dwMsgEncodingType,
1920 dwFlags, dwMsgType, hCryptProv, pRecipientInfo, pStreamInfo);
1922 if (GET_CMSG_ENCODING_TYPE(dwMsgEncodingType) != PKCS_7_ASN_ENCODING)
1924 SetLastError(E_INVALIDARG);
1927 msg = CryptMemAlloc(sizeof(CDecodeMsg));
1930 CryptMsgBase_Init((CryptMsgBase *)msg, dwFlags, pStreamInfo,
1931 CDecodeMsg_Close, CDecodeMsg_GetParam, CDecodeMsg_Update);
1932 msg->type = dwMsgType;
1934 msg->crypt_prov = hCryptProv;
1937 msg->crypt_prov = CRYPT_GetDefaultProvider();
1938 msg->base.open_flags &= ~CMSG_CRYPT_RELEASE_CONTEXT_FLAG;
1940 memset(&msg->u, 0, sizeof(msg->u));
1941 msg->msg_data.cbData = 0;
1942 msg->msg_data.pbData = NULL;
1943 msg->properties = ContextPropertyList_Create();
1948 HCRYPTMSG WINAPI CryptMsgDuplicate(HCRYPTMSG hCryptMsg)
1950 TRACE("(%p)\n", hCryptMsg);
1954 CryptMsgBase *msg = (CryptMsgBase *)hCryptMsg;
1956 InterlockedIncrement(&msg->ref);
1961 BOOL WINAPI CryptMsgClose(HCRYPTMSG hCryptMsg)
1963 TRACE("(%p)\n", hCryptMsg);
1967 CryptMsgBase *msg = (CryptMsgBase *)hCryptMsg;
1969 if (InterlockedDecrement(&msg->ref) == 0)
1971 TRACE("freeing %p\n", msg);
1980 BOOL WINAPI CryptMsgUpdate(HCRYPTMSG hCryptMsg, const BYTE *pbData,
1981 DWORD cbData, BOOL fFinal)
1983 CryptMsgBase *msg = (CryptMsgBase *)hCryptMsg;
1986 TRACE("(%p, %p, %d, %d)\n", hCryptMsg, pbData, cbData, fFinal);
1988 if (msg->state == MsgStateFinalized)
1989 SetLastError(CRYPT_E_MSG_ERROR);
1992 ret = msg->update(hCryptMsg, pbData, cbData, fFinal);
1993 msg->state = MsgStateUpdated;
1995 msg->state = MsgStateFinalized;
2000 BOOL WINAPI CryptMsgGetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
2001 DWORD dwIndex, void *pvData, DWORD *pcbData)
2003 CryptMsgBase *msg = (CryptMsgBase *)hCryptMsg;
2005 TRACE("(%p, %d, %d, %p, %p)\n", hCryptMsg, dwParamType, dwIndex,
2007 return msg->get_param(hCryptMsg, dwParamType, dwIndex, pvData, pcbData);
2010 BOOL WINAPI CryptMsgControl(HCRYPTMSG hCryptMsg, DWORD dwFlags,
2011 DWORD dwCtrlType, const void *pvCtrlPara)
2013 FIXME("(%p, %08x, %d, %p): stub\n", hCryptMsg, dwFlags, dwCtrlType,