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);
219 if (msg->base.open_flags & CMSG_DETACHED_FLAG)
220 SetLastError(E_INVALIDARG);
222 SetLastError(CRYPT_E_MSG_ERROR);
227 SetLastError(E_INVALIDARG);
230 CRYPT_DATA_BLOB blob = { cbData, (LPBYTE)pbData };
232 /* non-streamed data messages don't allow non-final updates,
233 * don't bother checking whether data already exist, they can't.
235 ret = CryptEncodeObjectEx(X509_ASN_ENCODING, X509_OCTET_STRING,
236 &blob, CRYPT_ENCODE_ALLOC_FLAG, NULL, &msg->bare_content,
237 &msg->bare_content_len);
244 static BOOL CRYPT_CopyParam(void *pvData, DWORD *pcbData, const BYTE *src,
251 else if (*pcbData < len)
254 SetLastError(ERROR_MORE_DATA);
260 memcpy(pvData, src, len);
265 static BOOL CDataEncodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
266 DWORD dwIndex, void *pvData, DWORD *pcbData)
268 CDataEncodeMsg *msg = (CDataEncodeMsg *)hCryptMsg;
273 case CMSG_CONTENT_PARAM:
274 if (msg->base.streamed)
275 SetLastError(E_INVALIDARG);
278 CRYPT_CONTENT_INFO info;
279 char rsa_data[] = "1.2.840.113549.1.7.1";
281 info.pszObjId = rsa_data;
282 info.Content.cbData = msg->bare_content_len;
283 info.Content.pbData = msg->bare_content;
284 ret = CryptEncodeObject(X509_ASN_ENCODING, PKCS_CONTENT_INFO, &info,
288 case CMSG_BARE_CONTENT_PARAM:
289 if (msg->base.streamed)
290 SetLastError(E_INVALIDARG);
292 ret = CRYPT_CopyParam(pvData, pcbData, msg->bare_content,
293 msg->bare_content_len);
296 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
301 static HCRYPTMSG CDataEncodeMsg_Open(DWORD dwFlags, const void *pvMsgEncodeInfo,
302 LPSTR pszInnerContentObjID, PCMSG_STREAM_INFO pStreamInfo)
308 SetLastError(E_INVALIDARG);
311 msg = CryptMemAlloc(sizeof(CDataEncodeMsg));
314 CryptMsgBase_Init((CryptMsgBase *)msg, dwFlags, pStreamInfo,
315 CDataEncodeMsg_Close, CDataEncodeMsg_GetParam, CDataEncodeMsg_Update);
316 msg->bare_content_len = sizeof(empty_data_content);
317 msg->bare_content = (LPBYTE)empty_data_content;
319 return (HCRYPTMSG)msg;
322 typedef struct _CHashEncodeMsg
327 CRYPT_DATA_BLOB data;
330 static void CHashEncodeMsg_Close(HCRYPTMSG hCryptMsg)
332 CHashEncodeMsg *msg = (CHashEncodeMsg *)hCryptMsg;
334 CryptMemFree(msg->data.pbData);
335 CryptDestroyHash(msg->hash);
336 if (msg->base.open_flags & CMSG_CRYPT_RELEASE_CONTEXT_FLAG)
337 CryptReleaseContext(msg->prov, 0);
340 static BOOL CRYPT_EncodePKCSDigestedData(CHashEncodeMsg *msg, void *pvData,
345 DWORD size = sizeof(algID);
347 ret = CryptGetHashParam(msg->hash, HP_ALGID, (BYTE *)&algID, &size, 0);
350 CRYPT_DIGESTED_DATA digestedData = { 0 };
351 char oid_rsa_data[] = szOID_RSA_data;
353 digestedData.version = CMSG_HASHED_DATA_PKCS_1_5_VERSION;
354 digestedData.DigestAlgorithm.pszObjId = (LPSTR)CertAlgIdToOID(algID);
355 /* FIXME: what about digestedData.DigestAlgorithm.Parameters? */
356 /* Quirk: OID is only encoded messages if an update has happened */
357 if (msg->base.state != MsgStateInit)
358 digestedData.ContentInfo.pszObjId = oid_rsa_data;
359 if (!(msg->base.open_flags & CMSG_DETACHED_FLAG) && msg->data.cbData)
361 ret = CRYPT_AsnEncodeOctets(0, NULL, &msg->data,
362 CRYPT_ENCODE_ALLOC_FLAG, NULL,
363 (LPBYTE)&digestedData.ContentInfo.Content.pbData,
364 &digestedData.ContentInfo.Content.cbData);
366 if (msg->base.state == MsgStateFinalized)
368 size = sizeof(DWORD);
369 ret = CryptGetHashParam(msg->hash, HP_HASHSIZE,
370 (LPBYTE)&digestedData.hash.cbData, &size, 0);
373 digestedData.hash.pbData = CryptMemAlloc(
374 digestedData.hash.cbData);
375 ret = CryptGetHashParam(msg->hash, HP_HASHVAL,
376 digestedData.hash.pbData, &digestedData.hash.cbData, 0);
380 ret = CRYPT_AsnEncodePKCSDigestedData(&digestedData, pvData,
382 CryptMemFree(digestedData.hash.pbData);
383 LocalFree(digestedData.ContentInfo.Content.pbData);
388 static BOOL CHashEncodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
389 DWORD dwIndex, void *pvData, DWORD *pcbData)
391 CHashEncodeMsg *msg = (CHashEncodeMsg *)hCryptMsg;
394 TRACE("(%p, %d, %d, %p, %p)\n", hCryptMsg, dwParamType, dwIndex,
399 case CMSG_BARE_CONTENT_PARAM:
400 if (msg->base.streamed)
401 SetLastError(E_INVALIDARG);
403 ret = CRYPT_EncodePKCSDigestedData(msg, pvData, pcbData);
405 case CMSG_CONTENT_PARAM:
407 CRYPT_CONTENT_INFO info;
409 ret = CryptMsgGetParam(hCryptMsg, CMSG_BARE_CONTENT_PARAM, 0, NULL,
410 &info.Content.cbData);
413 info.Content.pbData = CryptMemAlloc(info.Content.cbData);
414 if (info.Content.pbData)
416 ret = CryptMsgGetParam(hCryptMsg, CMSG_BARE_CONTENT_PARAM, 0,
417 info.Content.pbData, &info.Content.cbData);
420 char oid_rsa_hashed[] = szOID_RSA_hashedData;
422 info.pszObjId = oid_rsa_hashed;
423 ret = CryptEncodeObjectEx(X509_ASN_ENCODING,
424 PKCS_CONTENT_INFO, &info, 0, NULL, pvData, pcbData);
426 CryptMemFree(info.Content.pbData);
433 case CMSG_COMPUTED_HASH_PARAM:
434 ret = CryptGetHashParam(msg->hash, HP_HASHVAL, (BYTE *)pvData, pcbData,
437 case CMSG_VERSION_PARAM:
438 if (msg->base.state != MsgStateFinalized)
439 SetLastError(CRYPT_E_MSG_ERROR);
442 DWORD version = CMSG_HASHED_DATA_PKCS_1_5_VERSION;
444 /* Since the data are always encoded as octets, the version is
445 * always 0 (see rfc3852, section 7)
447 ret = CRYPT_CopyParam(pvData, pcbData, (const BYTE *)&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
611 static BOOL CRYPT_CopyBlob(CRYPT_DATA_BLOB *out, const CRYPT_DATA_BLOB *in)
615 out->cbData = in->cbData;
618 out->pbData = CryptMemAlloc(out->cbData);
620 memcpy(out->pbData, in->pbData, out->cbData);
629 typedef struct _BlobArray
632 PCRYPT_DATA_BLOB blobs;
635 static BOOL CRYPT_CopyBlobArray(BlobArray *out, const BlobArray *in)
639 out->cBlobs = in->cBlobs;
642 out->blobs = CryptMemAlloc(out->cBlobs * sizeof(CRYPT_DATA_BLOB));
647 memset(out->blobs, 0, out->cBlobs * sizeof(CRYPT_DATA_BLOB));
648 for (i = 0; ret && i < out->cBlobs; i++)
649 ret = CRYPT_CopyBlob(&out->blobs[i], &in->blobs[i]);
657 static void CRYPT_FreeBlobArray(BlobArray *array)
661 for (i = 0; i < array->cBlobs; i++)
662 CryptMemFree(array->blobs[i].pbData);
663 CryptMemFree(array->blobs);
666 static BOOL CRYPT_CopyAttribute(CRYPT_ATTRIBUTE *out, const CRYPT_ATTRIBUTE *in)
668 /* Assumption: algorithm IDs will point to static strings, not stack-based
669 * ones, so copying the pointer values is safe.
671 out->pszObjId = in->pszObjId;
672 return CRYPT_CopyBlobArray((BlobArray *)&out->cValue,
673 (const BlobArray *)&in->cValue);
676 static BOOL CRYPT_CopyAttributes(CRYPT_ATTRIBUTES *out,
677 const CRYPT_ATTRIBUTES *in)
681 out->cAttr = in->cAttr;
684 out->rgAttr = CryptMemAlloc(out->cAttr * sizeof(CRYPT_ATTRIBUTE));
689 memset(out->rgAttr, 0, out->cAttr * sizeof(CRYPT_ATTRIBUTE));
690 for (i = 0; ret && i < out->cAttr; i++)
691 ret = CRYPT_CopyAttribute(&out->rgAttr[i], &in->rgAttr[i]);
701 /* Constructs both a CSignerHandles and a CMSG_SIGNER_INFO from a
702 * CMSG_SIGNER_ENCODE_INFO_WITH_CMS.
704 static BOOL CSignerInfo_Construct(CSignerHandles *handles,
705 CMSG_SIGNER_INFO *info, CMSG_SIGNER_ENCODE_INFO_WITH_CMS *in, DWORD open_flags)
710 handles->prov = in->hCryptProv;
711 if (!(open_flags & CMSG_CRYPT_RELEASE_CONTEXT_FLAG))
712 CryptContextAddRef(handles->prov, NULL, 0);
713 algID = CertOIDToAlgId(in->HashAlgorithm.pszObjId);
714 ret = CryptCreateHash(handles->prov, algID, 0, 0, &handles->hash);
717 /* Note: needs to change if CMS fields are supported */
718 info->dwVersion = CMSG_SIGNER_INFO_V1;
719 ret = CRYPT_CopyBlob(&info->Issuer, &in->pCertInfo->Issuer);
721 ret = CRYPT_CopyBlob(&info->SerialNumber,
722 &in->pCertInfo->SerialNumber);
723 /* Assumption: algorithm IDs will point to static strings, not
724 * stack-based ones, so copying the pointer values is safe.
726 info->HashAlgorithm.pszObjId = in->HashAlgorithm.pszObjId;
728 ret = CRYPT_CopyBlob(&info->HashAlgorithm.Parameters,
729 &in->HashAlgorithm.Parameters);
730 memset(&info->HashEncryptionAlgorithm, 0,
731 sizeof(info->HashEncryptionAlgorithm));
733 ret = CRYPT_CopyAttributes(&info->AuthAttrs,
734 (CRYPT_ATTRIBUTES *)&in->cAuthAttr);
736 ret = CRYPT_CopyAttributes(&info->UnauthAttrs,
737 (CRYPT_ATTRIBUTES *)&in->cUnauthAttr);
742 static void CSignerInfo_Free(CMSG_SIGNER_INFO *info)
746 CryptMemFree(info->Issuer.pbData);
747 CryptMemFree(info->SerialNumber.pbData);
748 CryptMemFree(info->HashAlgorithm.Parameters.pbData);
749 CryptMemFree(info->EncryptedHash.pbData);
750 for (i = 0; i < info->AuthAttrs.cAttr; i++)
752 for (j = 0; j < info->AuthAttrs.rgAttr[i].cValue; j++)
753 CryptMemFree(info->AuthAttrs.rgAttr[i].rgValue[j].pbData);
754 CryptMemFree(info->AuthAttrs.rgAttr[i].rgValue);
756 CryptMemFree(info->AuthAttrs.rgAttr);
757 for (i = 0; i < info->UnauthAttrs.cAttr; i++)
759 for (j = 0; j < info->UnauthAttrs.rgAttr[i].cValue; j++)
760 CryptMemFree(info->UnauthAttrs.rgAttr[i].rgValue[j].pbData);
761 CryptMemFree(info->UnauthAttrs.rgAttr[i].rgValue);
763 CryptMemFree(info->UnauthAttrs.rgAttr);
766 typedef struct _CSignedEncodeMsg
769 CRYPT_DATA_BLOB data;
770 CRYPT_SIGNED_INFO info;
771 CSignerHandles *signerHandles;
774 static void CSignedEncodeMsg_Close(HCRYPTMSG hCryptMsg)
776 CSignedEncodeMsg *msg = (CSignedEncodeMsg *)hCryptMsg;
779 CryptMemFree(msg->data.pbData);
780 CRYPT_FreeBlobArray((BlobArray *)&msg->info.cCertEncoded);
781 CRYPT_FreeBlobArray((BlobArray *)&msg->info.cCrlEncoded);
782 for (i = 0; i < msg->info.cSignerInfo; i++)
784 CSignerInfo_Free(&msg->info.rgSignerInfo[i]);
785 CryptDestroyKey(msg->signerHandles[i].key);
786 CryptDestroyHash(msg->signerHandles[i].hash);
787 CryptReleaseContext(msg->signerHandles[i].prov, 0);
789 CryptMemFree(msg->signerHandles);
790 CryptMemFree(msg->info.rgSignerInfo);
793 static BOOL CSignedEncodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
794 DWORD dwIndex, void *pvData, DWORD *pcbData)
796 CSignedEncodeMsg *msg = (CSignedEncodeMsg *)hCryptMsg;
801 case CMSG_CONTENT_PARAM:
803 CRYPT_CONTENT_INFO info;
805 ret = CryptMsgGetParam(hCryptMsg, CMSG_BARE_CONTENT_PARAM, 0, NULL,
806 &info.Content.cbData);
809 info.Content.pbData = CryptMemAlloc(info.Content.cbData);
810 if (info.Content.pbData)
812 ret = CryptMsgGetParam(hCryptMsg, CMSG_BARE_CONTENT_PARAM, 0,
813 info.Content.pbData, &info.Content.cbData);
816 char oid_rsa_signed[] = szOID_RSA_signedData;
818 info.pszObjId = oid_rsa_signed;
819 ret = CryptEncodeObjectEx(X509_ASN_ENCODING,
820 PKCS_CONTENT_INFO, &info, 0, NULL, pvData, pcbData);
822 CryptMemFree(info.Content.pbData);
829 case CMSG_BARE_CONTENT_PARAM:
831 CRYPT_SIGNED_INFO info;
832 char oid_rsa_data[] = szOID_RSA_data;
834 memcpy(&info, &msg->info, sizeof(info));
835 /* Quirk: OID is only encoded messages if an update has happened */
836 if (msg->base.state != MsgStateInit)
837 info.content.pszObjId = oid_rsa_data;
839 info.content.pszObjId = NULL;
840 if (msg->data.cbData)
842 CRYPT_DATA_BLOB blob = { msg->data.cbData, msg->data.pbData };
844 ret = CryptEncodeObjectEx(X509_ASN_ENCODING, X509_OCTET_STRING,
845 &blob, CRYPT_ENCODE_ALLOC_FLAG, NULL,
846 &info.content.Content.pbData, &info.content.Content.cbData);
850 info.content.Content.cbData = 0;
851 info.content.Content.pbData = NULL;
856 ret = CRYPT_AsnEncodePKCSSignedInfo(&info, pvData, pcbData);
857 LocalFree(info.content.Content.pbData);
861 case CMSG_COMPUTED_HASH_PARAM:
862 if (dwIndex >= msg->info.cSignerInfo)
863 SetLastError(CRYPT_E_INVALID_INDEX);
865 ret = CryptGetHashParam(msg->signerHandles[dwIndex].hash,
866 HP_HASHVAL, pvData, pcbData, 0);
868 case CMSG_VERSION_PARAM:
869 ret = CRYPT_CopyParam(pvData, pcbData, (const BYTE *)&msg->info.version,
870 sizeof(msg->info.version));
873 FIXME("unimplemented for %d\n", dwParamType);
874 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
879 static BOOL CSignedEncodeMsg_UpdateHash(CSignedEncodeMsg *msg,
880 const BYTE *pbData, DWORD cbData)
885 TRACE("(%p, %p, %d)\n", msg, pbData, cbData);
887 for (i = 0; ret && i < msg->info.cSignerInfo; i++)
888 ret = CryptHashData(msg->signerHandles[i].hash, pbData, cbData, 0);
892 static void CRYPT_ReverseBytes(CRYPT_HASH_BLOB *hash)
897 for (i = 0; i < hash->cbData / 2; i++)
899 tmp = hash->pbData[hash->cbData - i - 1];
900 hash->pbData[hash->cbData - i - 1] = hash->pbData[i];
901 hash->pbData[i] = tmp;
905 static BOOL CSignedEncodeMsg_Sign(CSignedEncodeMsg *msg)
910 TRACE("(%p)\n", msg);
912 for (i = 0; ret && i < msg->info.cSignerInfo; i++)
914 ret = CryptSignHashW(msg->signerHandles[i].hash, AT_SIGNATURE, NULL, 0,
915 NULL, &msg->info.rgSignerInfo[i].EncryptedHash.cbData);
918 msg->info.rgSignerInfo[i].EncryptedHash.pbData =
919 CryptMemAlloc(msg->info.rgSignerInfo[i].EncryptedHash.cbData);
920 if (msg->info.rgSignerInfo[i].EncryptedHash.pbData)
922 ret = CryptSignHashW(msg->signerHandles[i].hash, AT_SIGNATURE,
923 NULL, 0, msg->info.rgSignerInfo[i].EncryptedHash.pbData,
924 &msg->info.rgSignerInfo[i].EncryptedHash.cbData);
926 CRYPT_ReverseBytes(&msg->info.rgSignerInfo[i].EncryptedHash);
935 static BOOL CSignedEncodeMsg_Update(HCRYPTMSG hCryptMsg, const BYTE *pbData,
936 DWORD cbData, BOOL fFinal)
938 CSignedEncodeMsg *msg = (CSignedEncodeMsg *)hCryptMsg;
941 if (msg->base.streamed || (msg->base.open_flags & CMSG_DETACHED_FLAG))
943 ret = CSignedEncodeMsg_UpdateHash(msg, pbData, cbData);
944 /* FIXME: hash authenticated attributes on final update */
946 ret = CSignedEncodeMsg_Sign(msg);
947 if (msg->base.streamed)
948 FIXME("streamed partial stub\n");
953 SetLastError(CRYPT_E_MSG_ERROR);
958 msg->data.pbData = CryptMemAlloc(cbData);
959 if (msg->data.pbData)
961 memcpy(msg->data.pbData, pbData, cbData);
962 msg->data.cbData = cbData;
969 ret = CSignedEncodeMsg_UpdateHash(msg, pbData, cbData);
970 /* FIXME: hash authenticated attributes */
972 ret = CSignedEncodeMsg_Sign(msg);
978 static HCRYPTMSG CSignedEncodeMsg_Open(DWORD dwFlags,
979 const void *pvMsgEncodeInfo, LPSTR pszInnerContentObjID,
980 PCMSG_STREAM_INFO pStreamInfo)
982 const CMSG_SIGNED_ENCODE_INFO_WITH_CMS *info =
983 (const CMSG_SIGNED_ENCODE_INFO_WITH_CMS *)pvMsgEncodeInfo;
985 CSignedEncodeMsg *msg;
987 if (info->cbSize != sizeof(CMSG_SIGNED_ENCODE_INFO) &&
988 info->cbSize != sizeof(CMSG_SIGNED_ENCODE_INFO_WITH_CMS))
990 SetLastError(E_INVALIDARG);
993 if (info->cbSize == sizeof(CMSG_SIGNED_ENCODE_INFO_WITH_CMS))
995 FIXME("CMSG_SIGNED_ENCODE_INFO with CMS fields unsupported\n");
998 for (i = 0; i < info->cSigners; i++)
999 if (!CRYPT_IsValidSigner(&info->rgSigners[i]))
1001 msg = CryptMemAlloc(sizeof(CSignedEncodeMsg));
1006 CryptMsgBase_Init((CryptMsgBase *)msg, dwFlags, pStreamInfo,
1007 CSignedEncodeMsg_Close, CSignedEncodeMsg_GetParam,
1008 CSignedEncodeMsg_Update);
1009 msg->data.cbData = 0;
1010 msg->data.pbData = NULL;
1011 memset(&msg->info, 0, sizeof(msg->info));
1012 msg->info.version = CMSG_SIGNED_DATA_V1;
1015 msg->signerHandles =
1016 CryptMemAlloc(info->cSigners * sizeof(CSignerHandles));
1017 if (msg->signerHandles)
1018 msg->info.rgSignerInfo =
1019 CryptMemAlloc(info->cSigners * sizeof(CMSG_SIGNER_INFO));
1023 msg->info.rgSignerInfo = NULL;
1025 if (msg->info.rgSignerInfo)
1027 msg->info.cSignerInfo = info->cSigners;
1028 memset(msg->signerHandles, 0,
1029 msg->info.cSignerInfo * sizeof(CSignerHandles));
1030 memset(msg->info.rgSignerInfo, 0,
1031 msg->info.cSignerInfo * sizeof(CMSG_SIGNER_INFO));
1032 for (i = 0; ret && i < msg->info.cSignerInfo; i++)
1033 ret = CSignerInfo_Construct(&msg->signerHandles[i],
1034 &msg->info.rgSignerInfo[i], &info->rgSigners[i], dwFlags);
1040 ret = CRYPT_CopyBlobArray((BlobArray *)&msg->info.cCertEncoded,
1041 (const BlobArray *)&info->cCertEncoded);
1043 ret = CRYPT_CopyBlobArray((BlobArray *)&msg->info.cCrlEncoded,
1044 (const BlobArray *)&info->cCrlEncoded);
1047 CSignedEncodeMsg_Close(msg);
1054 static inline const char *MSG_TYPE_STR(DWORD type)
1058 #define _x(x) case (x): return #x
1062 _x(CMSG_SIGNED_AND_ENVELOPED);
1067 return wine_dbg_sprintf("unknown (%d)", type);
1071 HCRYPTMSG WINAPI CryptMsgOpenToEncode(DWORD dwMsgEncodingType, DWORD dwFlags,
1072 DWORD dwMsgType, const void *pvMsgEncodeInfo, LPSTR pszInnerContentObjID,
1073 PCMSG_STREAM_INFO pStreamInfo)
1075 HCRYPTMSG msg = NULL;
1077 TRACE("(%08x, %08x, %08x, %p, %s, %p)\n", dwMsgEncodingType, dwFlags,
1078 dwMsgType, pvMsgEncodeInfo, debugstr_a(pszInnerContentObjID), pStreamInfo);
1080 if (GET_CMSG_ENCODING_TYPE(dwMsgEncodingType) != PKCS_7_ASN_ENCODING)
1082 SetLastError(E_INVALIDARG);
1088 msg = CDataEncodeMsg_Open(dwFlags, pvMsgEncodeInfo,
1089 pszInnerContentObjID, pStreamInfo);
1092 msg = CHashEncodeMsg_Open(dwFlags, pvMsgEncodeInfo,
1093 pszInnerContentObjID, pStreamInfo);
1096 msg = CSignedEncodeMsg_Open(dwFlags, pvMsgEncodeInfo,
1097 pszInnerContentObjID, pStreamInfo);
1099 case CMSG_ENVELOPED:
1100 FIXME("unimplemented for type %s\n", MSG_TYPE_STR(dwMsgType));
1102 case CMSG_SIGNED_AND_ENVELOPED:
1103 case CMSG_ENCRYPTED:
1104 /* defined but invalid, fall through */
1106 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1111 typedef struct _CDecodeMsg
1115 HCRYPTPROV crypt_prov;
1117 CRYPT_DATA_BLOB msg_data;
1118 PCONTEXT_PROPERTY_LIST properties;
1121 static void CDecodeMsg_Close(HCRYPTMSG hCryptMsg)
1123 CDecodeMsg *msg = (CDecodeMsg *)hCryptMsg;
1125 if (msg->base.open_flags & CMSG_CRYPT_RELEASE_CONTEXT_FLAG)
1126 CryptReleaseContext(msg->crypt_prov, 0);
1127 CryptDestroyHash(msg->hash);
1128 CryptMemFree(msg->msg_data.pbData);
1129 ContextPropertyList_Free(msg->properties);
1132 static BOOL CDecodeMsg_CopyData(CDecodeMsg *msg, const BYTE *pbData,
1139 if (msg->msg_data.cbData)
1140 msg->msg_data.pbData = CryptMemRealloc(msg->msg_data.pbData,
1141 msg->msg_data.cbData + cbData);
1143 msg->msg_data.pbData = CryptMemAlloc(cbData);
1144 if (msg->msg_data.pbData)
1146 memcpy(msg->msg_data.pbData + msg->msg_data.cbData, pbData, cbData);
1147 msg->msg_data.cbData += cbData;
1155 static BOOL CDecodeMsg_DecodeDataContent(CDecodeMsg *msg, CRYPT_DER_BLOB *blob)
1158 CRYPT_DATA_BLOB *data;
1161 ret = CryptDecodeObjectEx(X509_ASN_ENCODING, X509_OCTET_STRING,
1162 blob->pbData, blob->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, (LPBYTE)&data,
1166 ret = ContextPropertyList_SetProperty(msg->properties,
1167 CMSG_CONTENT_PARAM, data->pbData, data->cbData);
1173 static void CDecodeMsg_SaveAlgorithmID(CDecodeMsg *msg, DWORD param,
1174 const CRYPT_ALGORITHM_IDENTIFIER *id)
1176 static const BYTE nullParams[] = { ASN_NULL, 0 };
1177 CRYPT_ALGORITHM_IDENTIFIER *copy;
1178 DWORD len = sizeof(CRYPT_ALGORITHM_IDENTIFIER);
1180 /* Linearize algorithm id */
1181 len += strlen(id->pszObjId) + 1;
1182 len += id->Parameters.cbData;
1183 copy = CryptMemAlloc(len);
1187 (LPSTR)((BYTE *)copy + sizeof(CRYPT_ALGORITHM_IDENTIFIER));
1188 strcpy(copy->pszObjId, id->pszObjId);
1189 copy->Parameters.pbData = (BYTE *)copy->pszObjId + strlen(id->pszObjId)
1191 /* Trick: omit NULL parameters */
1192 if (id->Parameters.cbData == sizeof(nullParams) &&
1193 !memcmp(id->Parameters.pbData, nullParams, sizeof(nullParams)))
1195 copy->Parameters.cbData = 0;
1196 len -= sizeof(nullParams);
1199 copy->Parameters.cbData = id->Parameters.cbData;
1200 if (copy->Parameters.cbData)
1201 memcpy(copy->Parameters.pbData, id->Parameters.pbData,
1202 id->Parameters.cbData);
1203 ContextPropertyList_SetProperty(msg->properties, param, (BYTE *)copy,
1209 static inline void CRYPT_FixUpAlgorithmID(CRYPT_ALGORITHM_IDENTIFIER *id)
1211 id->pszObjId = (LPSTR)((BYTE *)id + sizeof(CRYPT_ALGORITHM_IDENTIFIER));
1212 id->Parameters.pbData = (BYTE *)id->pszObjId + strlen(id->pszObjId) + 1;
1215 static BOOL CDecodeMsg_DecodeHashedContent(CDecodeMsg *msg,
1216 CRYPT_DER_BLOB *blob)
1219 CRYPT_DIGESTED_DATA *digestedData;
1222 ret = CRYPT_AsnDecodePKCSDigestedData(blob->pbData, blob->cbData,
1223 CRYPT_DECODE_ALLOC_FLAG, NULL, (CRYPT_DIGESTED_DATA *)&digestedData,
1227 msg->type = CMSG_HASHED;
1228 ContextPropertyList_SetProperty(msg->properties, CMSG_VERSION_PARAM,
1229 (const BYTE *)&digestedData->version, sizeof(digestedData->version));
1230 CDecodeMsg_SaveAlgorithmID(msg, CMSG_HASH_ALGORITHM_PARAM,
1231 &digestedData->DigestAlgorithm);
1232 ContextPropertyList_SetProperty(msg->properties,
1233 CMSG_INNER_CONTENT_TYPE_PARAM,
1234 (const BYTE *)digestedData->ContentInfo.pszObjId,
1235 digestedData->ContentInfo.pszObjId ?
1236 strlen(digestedData->ContentInfo.pszObjId) + 1 : 0);
1237 if (digestedData->ContentInfo.Content.cbData)
1238 CDecodeMsg_DecodeDataContent(msg,
1239 &digestedData->ContentInfo.Content);
1241 ContextPropertyList_SetProperty(msg->properties,
1242 CMSG_CONTENT_PARAM, NULL, 0);
1243 ContextPropertyList_SetProperty(msg->properties, CMSG_HASH_DATA_PARAM,
1244 digestedData->hash.pbData, digestedData->hash.cbData);
1245 LocalFree(digestedData);
1250 /* Decodes the content in blob as the type given, and updates the value
1251 * (type, parameters, etc.) of msg based on what blob contains.
1252 * It doesn't just use msg's type, to allow a recursive call from an implicitly
1253 * typed message once the outer content info has been decoded.
1255 static BOOL CDecodeMsg_DecodeContent(CDecodeMsg *msg, CRYPT_DER_BLOB *blob,
1263 if ((ret = CDecodeMsg_DecodeDataContent(msg, blob)))
1264 msg->type = CMSG_DATA;
1267 if ((ret = CDecodeMsg_DecodeHashedContent(msg, blob)))
1268 msg->type = CMSG_HASHED;
1270 case CMSG_ENVELOPED:
1272 FIXME("unimplemented for type %s\n", MSG_TYPE_STR(type));
1277 CRYPT_CONTENT_INFO *info;
1280 ret = CryptDecodeObjectEx(X509_ASN_ENCODING, PKCS_CONTENT_INFO,
1281 msg->msg_data.pbData, msg->msg_data.cbData, CRYPT_DECODE_ALLOC_FLAG,
1282 NULL, (LPBYTE)&info, &size);
1285 if (!strcmp(info->pszObjId, szOID_RSA_data))
1286 ret = CDecodeMsg_DecodeContent(msg, &info->Content, CMSG_DATA);
1287 else if (!strcmp(info->pszObjId, szOID_RSA_digestedData))
1288 ret = CDecodeMsg_DecodeContent(msg, &info->Content,
1290 else if (!strcmp(info->pszObjId, szOID_RSA_envelopedData))
1291 ret = CDecodeMsg_DecodeContent(msg, &info->Content,
1293 else if (!strcmp(info->pszObjId, szOID_RSA_signedData))
1294 ret = CDecodeMsg_DecodeContent(msg, &info->Content,
1298 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1308 static BOOL CDecodeMsg_Update(HCRYPTMSG hCryptMsg, const BYTE *pbData,
1309 DWORD cbData, BOOL fFinal)
1311 CDecodeMsg *msg = (CDecodeMsg *)hCryptMsg;
1314 TRACE("(%p, %p, %d, %d)\n", hCryptMsg, pbData, cbData, fFinal);
1316 if (msg->base.streamed)
1318 ret = CDecodeMsg_CopyData(msg, pbData, cbData);
1319 FIXME("(%p, %p, %d, %d): streamed update stub\n", hCryptMsg, pbData,
1325 SetLastError(CRYPT_E_MSG_ERROR);
1328 ret = CDecodeMsg_CopyData(msg, pbData, cbData);
1330 ret = CDecodeMsg_DecodeContent(msg, &msg->msg_data, msg->type);
1337 static BOOL CDecodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
1338 DWORD dwIndex, void *pvData, DWORD *pcbData)
1340 CDecodeMsg *msg = (CDecodeMsg *)hCryptMsg;
1343 switch (dwParamType)
1345 case CMSG_TYPE_PARAM:
1346 ret = CRYPT_CopyParam(pvData, pcbData, (const BYTE *)&msg->type,
1349 case CMSG_HASH_ALGORITHM_PARAM:
1351 CRYPT_DATA_BLOB blob;
1353 ret = ContextPropertyList_FindProperty(msg->properties, dwParamType,
1357 ret = CRYPT_CopyParam(pvData, pcbData, blob.pbData, blob.cbData);
1359 CRYPT_FixUpAlgorithmID((CRYPT_ALGORITHM_IDENTIFIER *)pvData);
1362 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1365 case CMSG_COMPUTED_HASH_PARAM:
1368 CRYPT_ALGORITHM_IDENTIFIER *hashAlgoID = NULL;
1372 CryptMsgGetParam(msg, CMSG_HASH_ALGORITHM_PARAM, 0, NULL, &size);
1373 hashAlgoID = CryptMemAlloc(size);
1374 ret = CryptMsgGetParam(msg, CMSG_HASH_ALGORITHM_PARAM, 0,
1377 algID = CertOIDToAlgId(hashAlgoID->pszObjId);
1378 ret = CryptCreateHash(msg->crypt_prov, algID, 0, 0, &msg->hash);
1381 CRYPT_DATA_BLOB content;
1383 ret = ContextPropertyList_FindProperty(msg->properties,
1384 CMSG_CONTENT_PARAM, &content);
1386 ret = CryptHashData(msg->hash, content.pbData,
1389 CryptMemFree(hashAlgoID);
1394 ret = CryptGetHashParam(msg->hash, HP_HASHVAL, pvData, pcbData, 0);
1398 CRYPT_DATA_BLOB blob;
1400 ret = ContextPropertyList_FindProperty(msg->properties, dwParamType,
1403 ret = CRYPT_CopyParam(pvData, pcbData, blob.pbData, blob.cbData);
1405 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1411 HCRYPTMSG WINAPI CryptMsgOpenToDecode(DWORD dwMsgEncodingType, DWORD dwFlags,
1412 DWORD dwMsgType, HCRYPTPROV hCryptProv, PCERT_INFO pRecipientInfo,
1413 PCMSG_STREAM_INFO pStreamInfo)
1417 TRACE("(%08x, %08x, %08x, %08lx, %p, %p)\n", dwMsgEncodingType,
1418 dwFlags, dwMsgType, hCryptProv, pRecipientInfo, pStreamInfo);
1420 if (GET_CMSG_ENCODING_TYPE(dwMsgEncodingType) != PKCS_7_ASN_ENCODING)
1422 SetLastError(E_INVALIDARG);
1425 msg = CryptMemAlloc(sizeof(CDecodeMsg));
1428 CryptMsgBase_Init((CryptMsgBase *)msg, dwFlags, pStreamInfo,
1429 CDecodeMsg_Close, CDecodeMsg_GetParam, CDecodeMsg_Update);
1430 msg->type = dwMsgType;
1432 msg->crypt_prov = hCryptProv;
1435 msg->crypt_prov = CRYPT_GetDefaultProvider();
1436 msg->base.open_flags &= ~CMSG_CRYPT_RELEASE_CONTEXT_FLAG;
1439 msg->msg_data.cbData = 0;
1440 msg->msg_data.pbData = NULL;
1441 msg->properties = ContextPropertyList_Create();
1446 HCRYPTMSG WINAPI CryptMsgDuplicate(HCRYPTMSG hCryptMsg)
1448 TRACE("(%p)\n", hCryptMsg);
1452 CryptMsgBase *msg = (CryptMsgBase *)hCryptMsg;
1454 InterlockedIncrement(&msg->ref);
1459 BOOL WINAPI CryptMsgClose(HCRYPTMSG hCryptMsg)
1461 TRACE("(%p)\n", hCryptMsg);
1465 CryptMsgBase *msg = (CryptMsgBase *)hCryptMsg;
1467 if (InterlockedDecrement(&msg->ref) == 0)
1469 TRACE("freeing %p\n", msg);
1478 BOOL WINAPI CryptMsgUpdate(HCRYPTMSG hCryptMsg, const BYTE *pbData,
1479 DWORD cbData, BOOL fFinal)
1481 CryptMsgBase *msg = (CryptMsgBase *)hCryptMsg;
1484 TRACE("(%p, %p, %d, %d)\n", hCryptMsg, pbData, cbData, fFinal);
1486 if (msg->state == MsgStateFinalized)
1487 SetLastError(CRYPT_E_MSG_ERROR);
1490 ret = msg->update(hCryptMsg, pbData, cbData, fFinal);
1491 msg->state = MsgStateUpdated;
1493 msg->state = MsgStateFinalized;
1498 BOOL WINAPI CryptMsgGetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
1499 DWORD dwIndex, void *pvData, DWORD *pcbData)
1501 CryptMsgBase *msg = (CryptMsgBase *)hCryptMsg;
1503 TRACE("(%p, %d, %d, %p, %p)\n", hCryptMsg, dwParamType, dwIndex,
1505 return msg->get_param(hCryptMsg, dwParamType, dwIndex, pvData, pcbData);