push 0c258732cb75565633c2f709ca58b227c9f8ae60
[wine/hacks.git] / dlls / crypt32 / msg.c
blob30ec62a4fab290635212a9c12c005571b05a88ee
1 /*
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
18 #include <stdarg.h>
19 #include "windef.h"
20 #include "winbase.h"
21 #include "wincrypt.h"
22 #include "snmp.h"
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
31 * data here.
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 {
42 MsgStateInit,
43 MsgStateUpdated,
44 MsgStateFinalized
45 } CryptMsgState;
47 typedef struct _CryptMsgBase
49 LONG ref;
50 DWORD open_flags;
51 BOOL streamed;
52 CMSG_STREAM_INFO stream_info;
53 CryptMsgState state;
54 CryptMsgCloseFunc close;
55 CryptMsgUpdateFunc update;
56 CryptMsgGetParamFunc get_param;
57 } CryptMsgBase;
59 static inline void CryptMsgBase_Init(CryptMsgBase *msg, DWORD dwFlags,
60 PCMSG_STREAM_INFO pStreamInfo, CryptMsgCloseFunc close,
61 CryptMsgGetParamFunc get_param, CryptMsgUpdateFunc update)
63 msg->ref = 1;
64 msg->open_flags = dwFlags;
65 if (pStreamInfo)
67 msg->streamed = TRUE;
68 memcpy(&msg->stream_info, pStreamInfo, sizeof(msg->stream_info));
70 else
72 msg->streamed = FALSE;
73 memset(&msg->stream_info, 0, sizeof(msg->stream_info));
75 msg->close = close;
76 msg->get_param = get_param;
77 msg->update = update;
78 msg->state = MsgStateInit;
81 typedef struct _CDataEncodeMsg
83 CryptMsgBase base;
84 DWORD bare_content_len;
85 LPBYTE bare_content;
86 } CDataEncodeMsg;
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;
103 DWORD lenBytes;
104 BOOL ret = TRUE;
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);
111 if (!pbEncoded)
112 *pcbEncoded = 1 + lenBytes + msg->base.stream_info.cbContent;
113 else
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,
122 &lenBytes);
125 return ret;
128 static BOOL CRYPT_EncodeDataContentInfoHeader(CDataEncodeMsg *msg,
129 CRYPT_DATA_BLOB *header)
131 BOOL ret;
133 if (msg->base.streamed && msg->base.stream_info.cbContent == 0xffffffff)
135 FIXME("unimplemented for indefinite-length encoding\n");
136 header->cbData = 0;
137 header->pbData = NULL;
138 ret = TRUE;
140 else
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);
152 if (ret)
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;
160 return ret;
163 static BOOL CDataEncodeMsg_Update(HCRYPTMSG hCryptMsg, const BYTE *pbData,
164 DWORD cbData, BOOL fFinal)
166 CDataEncodeMsg *msg = (CDataEncodeMsg *)hCryptMsg;
167 BOOL ret = FALSE;
169 if (msg->base.streamed)
171 __TRY
173 if (msg->base.state != MsgStateUpdated)
175 CRYPT_DATA_BLOB header;
177 ret = CRYPT_EncodeDataContentInfoHeader(msg, &header);
178 if (ret)
180 ret = msg->base.stream_info.pfnStreamOutput(
181 msg->base.stream_info.pvArg, header.pbData, header.cbData,
182 FALSE);
183 LocalFree(header.pbData);
186 if (!fFinal)
187 ret = msg->base.stream_info.pfnStreamOutput(
188 msg->base.stream_info.pvArg, (BYTE *)pbData, cbData,
189 FALSE);
190 else
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,
198 FALSE);
199 if (ret)
200 ret = msg->base.stream_info.pfnStreamOutput(
201 msg->base.stream_info.pvArg, indefinite_trailer,
202 sizeof(indefinite_trailer), TRUE);
204 else
205 ret = msg->base.stream_info.pfnStreamOutput(
206 msg->base.stream_info.pvArg, (BYTE *)pbData, cbData, TRUE);
209 __EXCEPT_PAGE_FAULT
211 SetLastError(STATUS_ACCESS_VIOLATION);
213 __ENDTRY;
215 else
217 if (!fFinal)
219 if (msg->base.open_flags & CMSG_DETACHED_FLAG)
220 SetLastError(E_INVALIDARG);
221 else
222 SetLastError(CRYPT_E_MSG_ERROR);
224 else
226 if (!cbData)
227 SetLastError(E_INVALIDARG);
228 else
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);
241 return ret;
244 static BOOL CRYPT_CopyParam(void *pvData, DWORD *pcbData, const void *src,
245 DWORD len)
247 BOOL ret = TRUE;
249 if (!pvData)
250 *pcbData = len;
251 else if (*pcbData < len)
253 *pcbData = len;
254 SetLastError(ERROR_MORE_DATA);
255 ret = FALSE;
257 else
259 *pcbData = len;
260 memcpy(pvData, src, len);
262 return ret;
265 static BOOL CDataEncodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
266 DWORD dwIndex, void *pvData, DWORD *pcbData)
268 CDataEncodeMsg *msg = (CDataEncodeMsg *)hCryptMsg;
269 BOOL ret = FALSE;
271 switch (dwParamType)
273 case CMSG_CONTENT_PARAM:
274 if (msg->base.streamed)
275 SetLastError(E_INVALIDARG);
276 else
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,
285 pvData, pcbData);
287 break;
288 case CMSG_BARE_CONTENT_PARAM:
289 if (msg->base.streamed)
290 SetLastError(E_INVALIDARG);
291 else
292 ret = CRYPT_CopyParam(pvData, pcbData, msg->bare_content,
293 msg->bare_content_len);
294 break;
295 default:
296 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
298 return ret;
301 static HCRYPTMSG CDataEncodeMsg_Open(DWORD dwFlags, const void *pvMsgEncodeInfo,
302 LPSTR pszInnerContentObjID, PCMSG_STREAM_INFO pStreamInfo)
304 CDataEncodeMsg *msg;
306 if (pvMsgEncodeInfo)
308 SetLastError(E_INVALIDARG);
309 return NULL;
311 msg = CryptMemAlloc(sizeof(CDataEncodeMsg));
312 if (msg)
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
324 CryptMsgBase base;
325 HCRYPTPROV prov;
326 HCRYPTHASH hash;
327 CRYPT_DATA_BLOB data;
328 } CHashEncodeMsg;
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,
341 DWORD *pcbData)
343 BOOL ret;
344 ALG_ID algID;
345 DWORD size = sizeof(algID);
347 ret = CryptGetHashParam(msg->hash, HP_ALGID, (BYTE *)&algID, &size, 0);
348 if (ret)
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);
371 if (ret)
373 digestedData.hash.pbData = CryptMemAlloc(
374 digestedData.hash.cbData);
375 ret = CryptGetHashParam(msg->hash, HP_HASHVAL,
376 digestedData.hash.pbData, &digestedData.hash.cbData, 0);
379 if (ret)
380 ret = CRYPT_AsnEncodePKCSDigestedData(&digestedData, pvData,
381 pcbData);
382 CryptMemFree(digestedData.hash.pbData);
383 LocalFree(digestedData.ContentInfo.Content.pbData);
385 return ret;
388 static BOOL CHashEncodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
389 DWORD dwIndex, void *pvData, DWORD *pcbData)
391 CHashEncodeMsg *msg = (CHashEncodeMsg *)hCryptMsg;
392 BOOL ret = FALSE;
394 TRACE("(%p, %d, %d, %p, %p)\n", hCryptMsg, dwParamType, dwIndex,
395 pvData, pcbData);
397 switch (dwParamType)
399 case CMSG_BARE_CONTENT_PARAM:
400 if (msg->base.streamed)
401 SetLastError(E_INVALIDARG);
402 else
403 ret = CRYPT_EncodePKCSDigestedData(msg, pvData, pcbData);
404 break;
405 case CMSG_CONTENT_PARAM:
407 CRYPT_CONTENT_INFO info;
409 ret = CryptMsgGetParam(hCryptMsg, CMSG_BARE_CONTENT_PARAM, 0, NULL,
410 &info.Content.cbData);
411 if (ret)
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);
418 if (ret)
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);
428 else
429 ret = FALSE;
431 break;
433 case CMSG_COMPUTED_HASH_PARAM:
434 ret = CryptGetHashParam(msg->hash, HP_HASHVAL, (BYTE *)pvData, pcbData,
436 break;
437 case CMSG_VERSION_PARAM:
438 if (msg->base.state != MsgStateFinalized)
439 SetLastError(CRYPT_E_MSG_ERROR);
440 else
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, &version, sizeof(version));
449 break;
450 default:
451 ret = FALSE;
453 return ret;
456 static BOOL CHashEncodeMsg_Update(HCRYPTMSG hCryptMsg, const BYTE *pbData,
457 DWORD cbData, BOOL fFinal)
459 CHashEncodeMsg *msg = (CHashEncodeMsg *)hCryptMsg;
460 BOOL ret = FALSE;
462 TRACE("(%p, %p, %d, %d)\n", hCryptMsg, pbData, cbData, fFinal);
464 if (msg->base.streamed || (msg->base.open_flags & CMSG_DETACHED_FLAG))
466 /* Doesn't do much, as stream output is never called, and you
467 * can't get the content.
469 ret = CryptHashData(msg->hash, pbData, cbData, 0);
471 else
473 if (!fFinal)
474 SetLastError(CRYPT_E_MSG_ERROR);
475 else
477 ret = CryptHashData(msg->hash, pbData, cbData, 0);
478 if (ret)
480 msg->data.pbData = CryptMemAlloc(cbData);
481 if (msg->data.pbData)
483 memcpy(msg->data.pbData + msg->data.cbData, pbData, cbData);
484 msg->data.cbData += cbData;
486 else
487 ret = FALSE;
491 return ret;
494 static HCRYPTMSG CHashEncodeMsg_Open(DWORD dwFlags, const void *pvMsgEncodeInfo,
495 LPSTR pszInnerContentObjID, PCMSG_STREAM_INFO pStreamInfo)
497 CHashEncodeMsg *msg;
498 const CMSG_HASHED_ENCODE_INFO *info =
499 (const CMSG_HASHED_ENCODE_INFO *)pvMsgEncodeInfo;
500 HCRYPTPROV prov;
501 ALG_ID algID;
503 if (info->cbSize != sizeof(CMSG_HASHED_ENCODE_INFO))
505 SetLastError(E_INVALIDARG);
506 return NULL;
508 if (!(algID = CertOIDToAlgId(info->HashAlgorithm.pszObjId)))
510 SetLastError(CRYPT_E_UNKNOWN_ALGO);
511 return NULL;
513 if (info->hCryptProv)
514 prov = info->hCryptProv;
515 else
517 prov = CRYPT_GetDefaultProvider();
518 dwFlags &= ~CMSG_CRYPT_RELEASE_CONTEXT_FLAG;
520 msg = CryptMemAlloc(sizeof(CHashEncodeMsg));
521 if (msg)
523 CryptMsgBase_Init((CryptMsgBase *)msg, dwFlags, pStreamInfo,
524 CHashEncodeMsg_Close, CHashEncodeMsg_GetParam, CHashEncodeMsg_Update);
525 msg->prov = prov;
526 msg->data.cbData = 0;
527 msg->data.pbData = NULL;
528 if (!CryptCreateHash(prov, algID, 0, 0, &msg->hash))
530 CryptMsgClose(msg);
531 msg = NULL;
534 return (HCRYPTMSG)msg;
537 typedef struct _CMSG_SIGNER_ENCODE_INFO_WITH_CMS
539 DWORD cbSize;
540 PCERT_INFO pCertInfo;
541 HCRYPTPROV hCryptProv;
542 DWORD dwKeySpec;
543 CRYPT_ALGORITHM_IDENTIFIER HashAlgorithm;
544 void *pvHashAuxInfo;
545 DWORD cAuthAttr;
546 PCRYPT_ATTRIBUTE rgAuthAttr;
547 DWORD cUnauthAttr;
548 PCRYPT_ATTRIBUTE rgUnauthAttr;
549 CERT_ID SignerId;
550 CRYPT_ALGORITHM_IDENTIFIER HashEncryptionAlgorithm;
551 void *pvHashEncryptionAuxInfo;
552 } CMSG_SIGNER_ENCODE_INFO_WITH_CMS, *PCMSG_SIGNER_ENCODE_INFO_WITH_CMS;
554 typedef struct _CMSG_SIGNED_ENCODE_INFO_WITH_CMS
556 DWORD cbSize;
557 DWORD cSigners;
558 PCMSG_SIGNER_ENCODE_INFO_WITH_CMS rgSigners;
559 DWORD cCertEncoded;
560 PCERT_BLOB rgCertEncoded;
561 DWORD cCrlEncoded;
562 PCRL_BLOB rgCrlEncoded;
563 DWORD cAttrCertEncoded;
564 PCERT_BLOB rgAttrCertEncoded;
565 } CMSG_SIGNED_ENCODE_INFO_WITH_CMS, *PCMSG_SIGNED_ENCODE_INFO_WITH_CMS;
567 static BOOL CRYPT_IsValidSigner(CMSG_SIGNER_ENCODE_INFO_WITH_CMS *signer)
569 if (signer->cbSize != sizeof(CMSG_SIGNER_ENCODE_INFO) &&
570 signer->cbSize != sizeof(CMSG_SIGNER_ENCODE_INFO_WITH_CMS))
572 SetLastError(E_INVALIDARG);
573 return FALSE;
575 if (signer->cbSize == sizeof(CMSG_SIGNER_ENCODE_INFO_WITH_CMS))
577 FIXME("CMSG_SIGNER_ENCODE_INFO with CMS fields unsupported\n");
578 return FALSE;
580 if (!signer->pCertInfo->SerialNumber.cbData)
582 SetLastError(E_INVALIDARG);
583 return FALSE;
585 if (!signer->pCertInfo->Issuer.cbData)
587 SetLastError(E_INVALIDARG);
588 return FALSE;
590 if (!signer->hCryptProv)
592 SetLastError(E_INVALIDARG);
593 return FALSE;
595 if (!CertOIDToAlgId(signer->HashAlgorithm.pszObjId))
597 SetLastError(CRYPT_E_UNKNOWN_ALGO);
598 return FALSE;
600 return TRUE;
603 typedef struct _CSignerHandles
605 HCRYPTPROV prov;
606 HCRYPTHASH contentHash;
607 HCRYPTHASH authAttrHash;
608 HCRYPTKEY key;
609 } CSignerHandles;
611 static BOOL CRYPT_ConstructBlob(CRYPT_DATA_BLOB *out, const CRYPT_DATA_BLOB *in)
613 BOOL ret = TRUE;
615 out->cbData = in->cbData;
616 if (out->cbData)
618 out->pbData = CryptMemAlloc(out->cbData);
619 if (out->pbData)
620 memcpy(out->pbData, in->pbData, out->cbData);
621 else
622 ret = FALSE;
624 else
625 out->pbData = NULL;
626 return ret;
629 typedef struct _BlobArray
631 DWORD cBlobs;
632 PCRYPT_DATA_BLOB blobs;
633 } BlobArray;
635 static BOOL CRYPT_ConstructBlobArray(BlobArray *out, const BlobArray *in)
637 BOOL ret = TRUE;
639 out->cBlobs = in->cBlobs;
640 if (out->cBlobs)
642 out->blobs = CryptMemAlloc(out->cBlobs * sizeof(CRYPT_DATA_BLOB));
643 if (out->blobs)
645 DWORD i;
647 memset(out->blobs, 0, out->cBlobs * sizeof(CRYPT_DATA_BLOB));
648 for (i = 0; ret && i < out->cBlobs; i++)
649 ret = CRYPT_ConstructBlob(&out->blobs[i], &in->blobs[i]);
651 else
652 ret = FALSE;
654 return ret;
657 static void CRYPT_FreeBlobArray(BlobArray *array)
659 DWORD i;
661 for (i = 0; i < array->cBlobs; i++)
662 CryptMemFree(array->blobs[i].pbData);
663 CryptMemFree(array->blobs);
666 static BOOL CRYPT_ConstructAttribute(CRYPT_ATTRIBUTE *out,
667 const CRYPT_ATTRIBUTE *in)
669 BOOL ret;
671 out->pszObjId = CryptMemAlloc(strlen(in->pszObjId) + 1);
672 if (out->pszObjId)
674 strcpy(out->pszObjId, in->pszObjId);
675 ret = CRYPT_ConstructBlobArray((BlobArray *)&out->cValue,
676 (const BlobArray *)&in->cValue);
678 else
679 ret = FALSE;
680 return ret;
683 static BOOL CRYPT_ConstructAttributes(CRYPT_ATTRIBUTES *out,
684 const CRYPT_ATTRIBUTES *in)
686 BOOL ret = TRUE;
688 out->cAttr = in->cAttr;
689 if (out->cAttr)
691 out->rgAttr = CryptMemAlloc(out->cAttr * sizeof(CRYPT_ATTRIBUTE));
692 if (out->rgAttr)
694 DWORD i;
696 memset(out->rgAttr, 0, out->cAttr * sizeof(CRYPT_ATTRIBUTE));
697 for (i = 0; ret && i < out->cAttr; i++)
698 ret = CRYPT_ConstructAttribute(&out->rgAttr[i], &in->rgAttr[i]);
700 else
701 ret = FALSE;
703 else
704 out->rgAttr = NULL;
705 return ret;
708 /* Constructs both a CSignerHandles and a CMSG_SIGNER_INFO from a
709 * CMSG_SIGNER_ENCODE_INFO_WITH_CMS.
711 static BOOL CSignerInfo_Construct(CSignerHandles *handles,
712 CMSG_SIGNER_INFO *info, CMSG_SIGNER_ENCODE_INFO_WITH_CMS *in, DWORD open_flags)
714 ALG_ID algID;
715 BOOL ret;
717 handles->prov = in->hCryptProv;
718 if (!(open_flags & CMSG_CRYPT_RELEASE_CONTEXT_FLAG))
719 CryptContextAddRef(handles->prov, NULL, 0);
720 algID = CertOIDToAlgId(in->HashAlgorithm.pszObjId);
721 ret = CryptCreateHash(handles->prov, algID, 0, 0, &handles->contentHash);
722 if (ret && in->cAuthAttr)
723 ret = CryptCreateHash(handles->prov, algID, 0, 0,
724 &handles->authAttrHash);
725 if (ret)
727 /* Note: needs to change if CMS fields are supported */
728 info->dwVersion = CMSG_SIGNER_INFO_V1;
729 ret = CRYPT_ConstructBlob(&info->Issuer, &in->pCertInfo->Issuer);
730 if (ret)
731 ret = CRYPT_ConstructBlob(&info->SerialNumber,
732 &in->pCertInfo->SerialNumber);
733 /* Assumption: algorithm IDs will point to static strings, not
734 * stack-based ones, so copying the pointer values is safe.
736 info->HashAlgorithm.pszObjId = in->HashAlgorithm.pszObjId;
737 if (ret)
738 ret = CRYPT_ConstructBlob(&info->HashAlgorithm.Parameters,
739 &in->HashAlgorithm.Parameters);
740 memset(&info->HashEncryptionAlgorithm, 0,
741 sizeof(info->HashEncryptionAlgorithm));
742 if (ret)
743 ret = CRYPT_ConstructAttributes(&info->AuthAttrs,
744 (CRYPT_ATTRIBUTES *)&in->cAuthAttr);
745 if (ret)
746 ret = CRYPT_ConstructAttributes(&info->UnauthAttrs,
747 (CRYPT_ATTRIBUTES *)&in->cUnauthAttr);
749 return ret;
752 static void CSignerInfo_Free(CMSG_SIGNER_INFO *info)
754 DWORD i, j;
756 CryptMemFree(info->Issuer.pbData);
757 CryptMemFree(info->SerialNumber.pbData);
758 CryptMemFree(info->HashAlgorithm.Parameters.pbData);
759 CryptMemFree(info->EncryptedHash.pbData);
760 for (i = 0; i < info->AuthAttrs.cAttr; i++)
762 for (j = 0; j < info->AuthAttrs.rgAttr[i].cValue; j++)
763 CryptMemFree(info->AuthAttrs.rgAttr[i].rgValue[j].pbData);
764 CryptMemFree(info->AuthAttrs.rgAttr[i].rgValue);
765 CryptMemFree(info->AuthAttrs.rgAttr[i].pszObjId);
767 CryptMemFree(info->AuthAttrs.rgAttr);
768 for (i = 0; i < info->UnauthAttrs.cAttr; i++)
770 for (j = 0; j < info->UnauthAttrs.rgAttr[i].cValue; j++)
771 CryptMemFree(info->UnauthAttrs.rgAttr[i].rgValue[j].pbData);
772 CryptMemFree(info->UnauthAttrs.rgAttr[i].rgValue);
773 CryptMemFree(info->UnauthAttrs.rgAttr[i].pszObjId);
775 CryptMemFree(info->UnauthAttrs.rgAttr);
778 typedef struct _CSignedEncodeMsg
780 CryptMsgBase base;
781 CRYPT_DATA_BLOB data;
782 CRYPT_SIGNED_INFO info;
783 CSignerHandles *signerHandles;
784 } CSignedEncodeMsg;
786 static void CSignedEncodeMsg_Close(HCRYPTMSG hCryptMsg)
788 CSignedEncodeMsg *msg = (CSignedEncodeMsg *)hCryptMsg;
789 DWORD i;
791 CryptMemFree(msg->data.pbData);
792 CRYPT_FreeBlobArray((BlobArray *)&msg->info.cCertEncoded);
793 CRYPT_FreeBlobArray((BlobArray *)&msg->info.cCrlEncoded);
794 for (i = 0; i < msg->info.cSignerInfo; i++)
796 CSignerInfo_Free(&msg->info.rgSignerInfo[i]);
797 CryptDestroyKey(msg->signerHandles[i].key);
798 CryptDestroyHash(msg->signerHandles[i].contentHash);
799 CryptDestroyHash(msg->signerHandles[i].authAttrHash);
800 CryptReleaseContext(msg->signerHandles[i].prov, 0);
802 CryptMemFree(msg->signerHandles);
803 CryptMemFree(msg->info.rgSignerInfo);
806 static BOOL CSignedEncodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
807 DWORD dwIndex, void *pvData, DWORD *pcbData)
809 CSignedEncodeMsg *msg = (CSignedEncodeMsg *)hCryptMsg;
810 BOOL ret = FALSE;
812 switch (dwParamType)
814 case CMSG_CONTENT_PARAM:
816 CRYPT_CONTENT_INFO info;
818 ret = CryptMsgGetParam(hCryptMsg, CMSG_BARE_CONTENT_PARAM, 0, NULL,
819 &info.Content.cbData);
820 if (ret)
822 info.Content.pbData = CryptMemAlloc(info.Content.cbData);
823 if (info.Content.pbData)
825 ret = CryptMsgGetParam(hCryptMsg, CMSG_BARE_CONTENT_PARAM, 0,
826 info.Content.pbData, &info.Content.cbData);
827 if (ret)
829 char oid_rsa_signed[] = szOID_RSA_signedData;
831 info.pszObjId = oid_rsa_signed;
832 ret = CryptEncodeObjectEx(X509_ASN_ENCODING,
833 PKCS_CONTENT_INFO, &info, 0, NULL, pvData, pcbData);
835 CryptMemFree(info.Content.pbData);
837 else
838 ret = FALSE;
840 break;
842 case CMSG_BARE_CONTENT_PARAM:
844 CRYPT_SIGNED_INFO info;
845 char oid_rsa_data[] = szOID_RSA_data;
847 memcpy(&info, &msg->info, sizeof(info));
848 /* Quirk: OID is only encoded messages if an update has happened */
849 if (msg->base.state != MsgStateInit)
850 info.content.pszObjId = oid_rsa_data;
851 else
852 info.content.pszObjId = NULL;
853 if (msg->data.cbData)
855 CRYPT_DATA_BLOB blob = { msg->data.cbData, msg->data.pbData };
857 ret = CryptEncodeObjectEx(X509_ASN_ENCODING, X509_OCTET_STRING,
858 &blob, CRYPT_ENCODE_ALLOC_FLAG, NULL,
859 &info.content.Content.pbData, &info.content.Content.cbData);
861 else
863 info.content.Content.cbData = 0;
864 info.content.Content.pbData = NULL;
865 ret = TRUE;
867 if (ret)
869 ret = CRYPT_AsnEncodePKCSSignedInfo(&info, pvData, pcbData);
870 LocalFree(info.content.Content.pbData);
872 break;
874 case CMSG_COMPUTED_HASH_PARAM:
875 if (dwIndex >= msg->info.cSignerInfo)
876 SetLastError(CRYPT_E_INVALID_INDEX);
877 else
878 ret = CryptGetHashParam(msg->signerHandles[dwIndex].contentHash,
879 HP_HASHVAL, pvData, pcbData, 0);
880 break;
881 case CMSG_ENCODED_SIGNER:
882 if (dwIndex >= msg->info.cSignerInfo)
883 SetLastError(CRYPT_E_INVALID_INDEX);
884 else
885 ret = CryptEncodeObjectEx(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
886 PKCS7_SIGNER_INFO, &msg->info.rgSignerInfo[dwIndex], 0, NULL,
887 pvData, pcbData);
888 break;
889 case CMSG_VERSION_PARAM:
890 ret = CRYPT_CopyParam(pvData, pcbData, &msg->info.version,
891 sizeof(msg->info.version));
892 break;
893 default:
894 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
896 return ret;
899 static BOOL CSignedEncodeMsg_UpdateHash(CSignedEncodeMsg *msg,
900 const BYTE *pbData, DWORD cbData)
902 DWORD i;
903 BOOL ret = TRUE;
905 TRACE("(%p, %p, %d)\n", msg, pbData, cbData);
907 for (i = 0; ret && i < msg->info.cSignerInfo; i++)
908 ret = CryptHashData(msg->signerHandles[i].contentHash, pbData, cbData,
910 return ret;
913 static BOOL CRYPT_AppendAttribute(CRYPT_ATTRIBUTES *out,
914 const CRYPT_ATTRIBUTE *in)
916 BOOL ret = FALSE;
918 out->rgAttr = CryptMemRealloc(out->rgAttr,
919 (out->cAttr + 1) * sizeof(CRYPT_ATTRIBUTE));
920 if (out->rgAttr)
922 ret = CRYPT_ConstructAttribute(&out->rgAttr[out->cAttr], in);
923 if (ret)
924 out->cAttr++;
926 return ret;
929 static BOOL CSignedEncodeMsg_AppendMessageDigestAttribute(CSignedEncodeMsg *msg,
930 DWORD signerIndex)
932 BOOL ret;
933 DWORD size;
934 CRYPT_HASH_BLOB hash = { 0, NULL }, encodedHash = { 0, NULL };
935 char messageDigest[] = szOID_RSA_messageDigest;
936 CRYPT_ATTRIBUTE messageDigestAttr = { messageDigest, 1, &encodedHash };
938 size = sizeof(DWORD);
939 ret = CryptGetHashParam(msg->signerHandles[signerIndex].contentHash,
940 HP_HASHSIZE, (LPBYTE)&hash.cbData, &size, 0);
941 if (ret)
943 hash.pbData = CryptMemAlloc(hash.cbData);
944 ret = CryptGetHashParam(msg->signerHandles[signerIndex].contentHash,
945 HP_HASHVAL, hash.pbData, &hash.cbData, 0);
946 if (ret)
948 ret = CRYPT_AsnEncodeOctets(0, NULL, &hash, CRYPT_ENCODE_ALLOC_FLAG,
949 NULL, (LPBYTE)&encodedHash.pbData, &encodedHash.cbData);
950 if (ret)
952 ret = CRYPT_AppendAttribute(
953 &msg->info.rgSignerInfo[signerIndex].AuthAttrs,
954 &messageDigestAttr);
955 LocalFree(encodedHash.pbData);
958 CryptMemFree(hash.pbData);
960 return ret;
963 static BOOL CSignedEncodeMsg_UpdateAuthenticatedAttributes(
964 CSignedEncodeMsg *msg)
966 DWORD i;
967 BOOL ret = TRUE;
969 TRACE("(%p)\n", msg);
971 for (i = 0; ret && i < msg->info.cSignerInfo; i++)
973 if (msg->info.rgSignerInfo[i].AuthAttrs.cAttr)
975 BYTE oid_rsa_data_encoded[] = { 0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,
976 0x0d,0x01,0x07,0x01 };
977 CRYPT_DATA_BLOB content = { sizeof(oid_rsa_data_encoded),
978 oid_rsa_data_encoded };
979 char contentType[] = szOID_RSA_contentType;
980 CRYPT_ATTRIBUTE contentTypeAttr = { contentType, 1, &content };
982 /* FIXME: does this depend on inner OID? */
983 ret = CRYPT_AppendAttribute(&msg->info.rgSignerInfo[i].AuthAttrs,
984 &contentTypeAttr);
985 if (ret)
986 ret = CSignedEncodeMsg_AppendMessageDigestAttribute(msg, i);
987 if (ret)
989 LPBYTE encodedAttrs;
990 DWORD size;
992 ret = CryptEncodeObjectEx(X509_ASN_ENCODING, PKCS_ATTRIBUTES,
993 &msg->info.rgSignerInfo[i].AuthAttrs, CRYPT_ENCODE_ALLOC_FLAG,
994 NULL, (LPBYTE)&encodedAttrs, &size);
995 if (ret)
997 ret = CryptHashData(msg->signerHandles[i].authAttrHash,
998 encodedAttrs, size, 0);
999 LocalFree(encodedAttrs);
1004 TRACE("returning %d\n", ret);
1005 return ret;
1008 static void CRYPT_ReverseBytes(CRYPT_HASH_BLOB *hash)
1010 DWORD i;
1011 BYTE tmp;
1013 for (i = 0; i < hash->cbData / 2; i++)
1015 tmp = hash->pbData[hash->cbData - i - 1];
1016 hash->pbData[hash->cbData - i - 1] = hash->pbData[i];
1017 hash->pbData[i] = tmp;
1021 static BOOL CSignedEncodeMsg_Sign(CSignedEncodeMsg *msg)
1023 DWORD i;
1024 BOOL ret = TRUE;
1026 TRACE("(%p)\n", msg);
1028 for (i = 0; ret && i < msg->info.cSignerInfo; i++)
1030 HCRYPTHASH hash;
1032 if (msg->info.rgSignerInfo[i].AuthAttrs.cAttr)
1033 hash = msg->signerHandles[i].authAttrHash;
1034 else
1035 hash = msg->signerHandles[i].contentHash;
1036 ret = CryptSignHashW(hash, AT_SIGNATURE, NULL, 0, NULL,
1037 &msg->info.rgSignerInfo[i].EncryptedHash.cbData);
1038 if (ret)
1040 msg->info.rgSignerInfo[i].EncryptedHash.pbData =
1041 CryptMemAlloc(msg->info.rgSignerInfo[i].EncryptedHash.cbData);
1042 if (msg->info.rgSignerInfo[i].EncryptedHash.pbData)
1044 ret = CryptSignHashW(hash, AT_SIGNATURE, NULL, 0,
1045 msg->info.rgSignerInfo[i].EncryptedHash.pbData,
1046 &msg->info.rgSignerInfo[i].EncryptedHash.cbData);
1047 if (ret)
1048 CRYPT_ReverseBytes(&msg->info.rgSignerInfo[i].EncryptedHash);
1050 else
1051 ret = FALSE;
1054 return ret;
1057 static BOOL CSignedEncodeMsg_Update(HCRYPTMSG hCryptMsg, const BYTE *pbData,
1058 DWORD cbData, BOOL fFinal)
1060 CSignedEncodeMsg *msg = (CSignedEncodeMsg *)hCryptMsg;
1061 BOOL ret = FALSE;
1063 if (msg->base.streamed || (msg->base.open_flags & CMSG_DETACHED_FLAG))
1065 ret = CSignedEncodeMsg_UpdateHash(msg, pbData, cbData);
1066 if (ret && fFinal)
1068 ret = CSignedEncodeMsg_UpdateAuthenticatedAttributes(msg);
1069 if (ret)
1070 ret = CSignedEncodeMsg_Sign(msg);
1072 if (msg->base.streamed)
1073 FIXME("streamed partial stub\n");
1075 else
1077 if (!fFinal)
1078 SetLastError(CRYPT_E_MSG_ERROR);
1079 else
1081 if (cbData)
1083 msg->data.pbData = CryptMemAlloc(cbData);
1084 if (msg->data.pbData)
1086 memcpy(msg->data.pbData, pbData, cbData);
1087 msg->data.cbData = cbData;
1088 ret = TRUE;
1091 else
1092 ret = TRUE;
1093 if (ret)
1094 ret = CSignedEncodeMsg_UpdateHash(msg, pbData, cbData);
1095 if (ret)
1096 ret = CSignedEncodeMsg_UpdateAuthenticatedAttributes(msg);
1097 if (ret)
1098 ret = CSignedEncodeMsg_Sign(msg);
1101 return ret;
1104 static HCRYPTMSG CSignedEncodeMsg_Open(DWORD dwFlags,
1105 const void *pvMsgEncodeInfo, LPSTR pszInnerContentObjID,
1106 PCMSG_STREAM_INFO pStreamInfo)
1108 const CMSG_SIGNED_ENCODE_INFO_WITH_CMS *info =
1109 (const CMSG_SIGNED_ENCODE_INFO_WITH_CMS *)pvMsgEncodeInfo;
1110 DWORD i;
1111 CSignedEncodeMsg *msg;
1113 if (info->cbSize != sizeof(CMSG_SIGNED_ENCODE_INFO) &&
1114 info->cbSize != sizeof(CMSG_SIGNED_ENCODE_INFO_WITH_CMS))
1116 SetLastError(E_INVALIDARG);
1117 return NULL;
1119 if (info->cbSize == sizeof(CMSG_SIGNED_ENCODE_INFO_WITH_CMS))
1121 FIXME("CMSG_SIGNED_ENCODE_INFO with CMS fields unsupported\n");
1122 return NULL;
1124 for (i = 0; i < info->cSigners; i++)
1125 if (!CRYPT_IsValidSigner(&info->rgSigners[i]))
1126 return NULL;
1127 msg = CryptMemAlloc(sizeof(CSignedEncodeMsg));
1128 if (msg)
1130 BOOL ret = TRUE;
1132 CryptMsgBase_Init((CryptMsgBase *)msg, dwFlags, pStreamInfo,
1133 CSignedEncodeMsg_Close, CSignedEncodeMsg_GetParam,
1134 CSignedEncodeMsg_Update);
1135 msg->data.cbData = 0;
1136 msg->data.pbData = NULL;
1137 memset(&msg->info, 0, sizeof(msg->info));
1138 msg->info.version = CMSG_SIGNED_DATA_V1;
1139 if (info->cSigners)
1141 msg->signerHandles =
1142 CryptMemAlloc(info->cSigners * sizeof(CSignerHandles));
1143 if (msg->signerHandles)
1144 msg->info.rgSignerInfo =
1145 CryptMemAlloc(info->cSigners * sizeof(CMSG_SIGNER_INFO));
1146 else
1148 ret = FALSE;
1149 msg->info.rgSignerInfo = NULL;
1151 if (msg->info.rgSignerInfo)
1153 msg->info.cSignerInfo = info->cSigners;
1154 memset(msg->signerHandles, 0,
1155 msg->info.cSignerInfo * sizeof(CSignerHandles));
1156 memset(msg->info.rgSignerInfo, 0,
1157 msg->info.cSignerInfo * sizeof(CMSG_SIGNER_INFO));
1158 for (i = 0; ret && i < msg->info.cSignerInfo; i++)
1159 ret = CSignerInfo_Construct(&msg->signerHandles[i],
1160 &msg->info.rgSignerInfo[i], &info->rgSigners[i], dwFlags);
1162 else
1163 ret = FALSE;
1165 if (ret)
1166 ret = CRYPT_ConstructBlobArray((BlobArray *)&msg->info.cCertEncoded,
1167 (const BlobArray *)&info->cCertEncoded);
1168 if (ret)
1169 ret = CRYPT_ConstructBlobArray((BlobArray *)&msg->info.cCrlEncoded,
1170 (const BlobArray *)&info->cCrlEncoded);
1171 if (!ret)
1173 CSignedEncodeMsg_Close(msg);
1174 msg = NULL;
1177 return msg;
1180 static inline const char *MSG_TYPE_STR(DWORD type)
1182 switch (type)
1184 #define _x(x) case (x): return #x
1185 _x(CMSG_DATA);
1186 _x(CMSG_SIGNED);
1187 _x(CMSG_ENVELOPED);
1188 _x(CMSG_SIGNED_AND_ENVELOPED);
1189 _x(CMSG_HASHED);
1190 _x(CMSG_ENCRYPTED);
1191 #undef _x
1192 default:
1193 return wine_dbg_sprintf("unknown (%d)", type);
1197 HCRYPTMSG WINAPI CryptMsgOpenToEncode(DWORD dwMsgEncodingType, DWORD dwFlags,
1198 DWORD dwMsgType, const void *pvMsgEncodeInfo, LPSTR pszInnerContentObjID,
1199 PCMSG_STREAM_INFO pStreamInfo)
1201 HCRYPTMSG msg = NULL;
1203 TRACE("(%08x, %08x, %08x, %p, %s, %p)\n", dwMsgEncodingType, dwFlags,
1204 dwMsgType, pvMsgEncodeInfo, debugstr_a(pszInnerContentObjID), pStreamInfo);
1206 if (GET_CMSG_ENCODING_TYPE(dwMsgEncodingType) != PKCS_7_ASN_ENCODING)
1208 SetLastError(E_INVALIDARG);
1209 return NULL;
1211 switch (dwMsgType)
1213 case CMSG_DATA:
1214 msg = CDataEncodeMsg_Open(dwFlags, pvMsgEncodeInfo,
1215 pszInnerContentObjID, pStreamInfo);
1216 break;
1217 case CMSG_HASHED:
1218 msg = CHashEncodeMsg_Open(dwFlags, pvMsgEncodeInfo,
1219 pszInnerContentObjID, pStreamInfo);
1220 break;
1221 case CMSG_SIGNED:
1222 msg = CSignedEncodeMsg_Open(dwFlags, pvMsgEncodeInfo,
1223 pszInnerContentObjID, pStreamInfo);
1224 break;
1225 case CMSG_ENVELOPED:
1226 FIXME("unimplemented for type %s\n", MSG_TYPE_STR(dwMsgType));
1227 break;
1228 case CMSG_SIGNED_AND_ENVELOPED:
1229 case CMSG_ENCRYPTED:
1230 /* defined but invalid, fall through */
1231 default:
1232 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1234 return msg;
1237 typedef struct _CDecodeMsg
1239 CryptMsgBase base;
1240 DWORD type;
1241 HCRYPTPROV crypt_prov;
1242 union {
1243 HCRYPTHASH hash;
1244 CRYPT_SIGNED_INFO *signedInfo;
1245 } u;
1246 CRYPT_DATA_BLOB msg_data;
1247 PCONTEXT_PROPERTY_LIST properties;
1248 } CDecodeMsg;
1250 static void CDecodeMsg_Close(HCRYPTMSG hCryptMsg)
1252 CDecodeMsg *msg = (CDecodeMsg *)hCryptMsg;
1254 if (msg->base.open_flags & CMSG_CRYPT_RELEASE_CONTEXT_FLAG)
1255 CryptReleaseContext(msg->crypt_prov, 0);
1256 switch (msg->type)
1258 case CMSG_HASHED:
1259 if (msg->u.hash)
1260 CryptDestroyHash(msg->u.hash);
1261 break;
1262 case CMSG_SIGNED:
1263 LocalFree(msg->u.signedInfo);
1264 break;
1266 CryptMemFree(msg->msg_data.pbData);
1267 ContextPropertyList_Free(msg->properties);
1270 static BOOL CDecodeMsg_CopyData(CDecodeMsg *msg, const BYTE *pbData,
1271 DWORD cbData)
1273 BOOL ret = TRUE;
1275 if (cbData)
1277 if (msg->msg_data.cbData)
1278 msg->msg_data.pbData = CryptMemRealloc(msg->msg_data.pbData,
1279 msg->msg_data.cbData + cbData);
1280 else
1281 msg->msg_data.pbData = CryptMemAlloc(cbData);
1282 if (msg->msg_data.pbData)
1284 memcpy(msg->msg_data.pbData + msg->msg_data.cbData, pbData, cbData);
1285 msg->msg_data.cbData += cbData;
1287 else
1288 ret = FALSE;
1290 return ret;
1293 static BOOL CDecodeMsg_DecodeDataContent(CDecodeMsg *msg, CRYPT_DER_BLOB *blob)
1295 BOOL ret;
1296 CRYPT_DATA_BLOB *data;
1297 DWORD size;
1299 ret = CryptDecodeObjectEx(X509_ASN_ENCODING, X509_OCTET_STRING,
1300 blob->pbData, blob->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, (LPBYTE)&data,
1301 &size);
1302 if (ret)
1304 ret = ContextPropertyList_SetProperty(msg->properties,
1305 CMSG_CONTENT_PARAM, data->pbData, data->cbData);
1306 LocalFree(data);
1308 return ret;
1311 static void CDecodeMsg_SaveAlgorithmID(CDecodeMsg *msg, DWORD param,
1312 const CRYPT_ALGORITHM_IDENTIFIER *id)
1314 static const BYTE nullParams[] = { ASN_NULL, 0 };
1315 CRYPT_ALGORITHM_IDENTIFIER *copy;
1316 DWORD len = sizeof(CRYPT_ALGORITHM_IDENTIFIER);
1318 /* Linearize algorithm id */
1319 len += strlen(id->pszObjId) + 1;
1320 len += id->Parameters.cbData;
1321 copy = CryptMemAlloc(len);
1322 if (copy)
1324 copy->pszObjId =
1325 (LPSTR)((BYTE *)copy + sizeof(CRYPT_ALGORITHM_IDENTIFIER));
1326 strcpy(copy->pszObjId, id->pszObjId);
1327 copy->Parameters.pbData = (BYTE *)copy->pszObjId + strlen(id->pszObjId)
1328 + 1;
1329 /* Trick: omit NULL parameters */
1330 if (id->Parameters.cbData == sizeof(nullParams) &&
1331 !memcmp(id->Parameters.pbData, nullParams, sizeof(nullParams)))
1333 copy->Parameters.cbData = 0;
1334 len -= sizeof(nullParams);
1336 else
1337 copy->Parameters.cbData = id->Parameters.cbData;
1338 if (copy->Parameters.cbData)
1339 memcpy(copy->Parameters.pbData, id->Parameters.pbData,
1340 id->Parameters.cbData);
1341 ContextPropertyList_SetProperty(msg->properties, param, (BYTE *)copy,
1342 len);
1343 CryptMemFree(copy);
1347 static inline void CRYPT_FixUpAlgorithmID(CRYPT_ALGORITHM_IDENTIFIER *id)
1349 id->pszObjId = (LPSTR)((BYTE *)id + sizeof(CRYPT_ALGORITHM_IDENTIFIER));
1350 id->Parameters.pbData = (BYTE *)id->pszObjId + strlen(id->pszObjId) + 1;
1353 static BOOL CDecodeMsg_DecodeHashedContent(CDecodeMsg *msg,
1354 CRYPT_DER_BLOB *blob)
1356 BOOL ret;
1357 CRYPT_DIGESTED_DATA *digestedData;
1358 DWORD size;
1360 ret = CRYPT_AsnDecodePKCSDigestedData(blob->pbData, blob->cbData,
1361 CRYPT_DECODE_ALLOC_FLAG, NULL, (CRYPT_DIGESTED_DATA *)&digestedData,
1362 &size);
1363 if (ret)
1365 ContextPropertyList_SetProperty(msg->properties, CMSG_VERSION_PARAM,
1366 (const BYTE *)&digestedData->version, sizeof(digestedData->version));
1367 CDecodeMsg_SaveAlgorithmID(msg, CMSG_HASH_ALGORITHM_PARAM,
1368 &digestedData->DigestAlgorithm);
1369 ContextPropertyList_SetProperty(msg->properties,
1370 CMSG_INNER_CONTENT_TYPE_PARAM,
1371 (const BYTE *)digestedData->ContentInfo.pszObjId,
1372 digestedData->ContentInfo.pszObjId ?
1373 strlen(digestedData->ContentInfo.pszObjId) + 1 : 0);
1374 if (digestedData->ContentInfo.Content.cbData)
1375 CDecodeMsg_DecodeDataContent(msg,
1376 &digestedData->ContentInfo.Content);
1377 else
1378 ContextPropertyList_SetProperty(msg->properties,
1379 CMSG_CONTENT_PARAM, NULL, 0);
1380 ContextPropertyList_SetProperty(msg->properties, CMSG_HASH_DATA_PARAM,
1381 digestedData->hash.pbData, digestedData->hash.cbData);
1382 LocalFree(digestedData);
1384 return ret;
1387 static BOOL CDecodeMsg_DecodeSignedContent(CDecodeMsg *msg,
1388 CRYPT_DER_BLOB *blob)
1390 BOOL ret;
1391 CRYPT_SIGNED_INFO *signedInfo;
1392 DWORD size;
1394 ret = CRYPT_AsnDecodePKCSSignedInfo(blob->pbData, blob->cbData,
1395 CRYPT_DECODE_ALLOC_FLAG, NULL, (CRYPT_SIGNED_INFO *)&signedInfo,
1396 &size);
1397 if (ret)
1398 msg->u.signedInfo = signedInfo;
1399 return ret;
1401 /* Decodes the content in blob as the type given, and updates the value
1402 * (type, parameters, etc.) of msg based on what blob contains.
1403 * It doesn't just use msg's type, to allow a recursive call from an implicitly
1404 * typed message once the outer content info has been decoded.
1406 static BOOL CDecodeMsg_DecodeContent(CDecodeMsg *msg, CRYPT_DER_BLOB *blob,
1407 DWORD type)
1409 BOOL ret;
1411 switch (type)
1413 case CMSG_DATA:
1414 if ((ret = CDecodeMsg_DecodeDataContent(msg, blob)))
1415 msg->type = CMSG_DATA;
1416 break;
1417 case CMSG_HASHED:
1418 if ((ret = CDecodeMsg_DecodeHashedContent(msg, blob)))
1419 msg->type = CMSG_HASHED;
1420 break;
1421 case CMSG_ENVELOPED:
1422 FIXME("unimplemented for type %s\n", MSG_TYPE_STR(type));
1423 ret = TRUE;
1424 break;
1425 case CMSG_SIGNED:
1426 if ((ret = CDecodeMsg_DecodeSignedContent(msg, blob)))
1427 msg->type = CMSG_SIGNED;
1428 break;
1429 default:
1431 CRYPT_CONTENT_INFO *info;
1432 DWORD size;
1434 ret = CryptDecodeObjectEx(X509_ASN_ENCODING, PKCS_CONTENT_INFO,
1435 msg->msg_data.pbData, msg->msg_data.cbData, CRYPT_DECODE_ALLOC_FLAG,
1436 NULL, (LPBYTE)&info, &size);
1437 if (ret)
1439 if (!strcmp(info->pszObjId, szOID_RSA_data))
1440 ret = CDecodeMsg_DecodeContent(msg, &info->Content, CMSG_DATA);
1441 else if (!strcmp(info->pszObjId, szOID_RSA_digestedData))
1442 ret = CDecodeMsg_DecodeContent(msg, &info->Content,
1443 CMSG_HASHED);
1444 else if (!strcmp(info->pszObjId, szOID_RSA_envelopedData))
1445 ret = CDecodeMsg_DecodeContent(msg, &info->Content,
1446 CMSG_ENVELOPED);
1447 else if (!strcmp(info->pszObjId, szOID_RSA_signedData))
1448 ret = CDecodeMsg_DecodeContent(msg, &info->Content,
1449 CMSG_SIGNED);
1450 else
1452 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1453 ret = FALSE;
1455 LocalFree(info);
1459 return ret;
1462 static BOOL CDecodeMsg_Update(HCRYPTMSG hCryptMsg, const BYTE *pbData,
1463 DWORD cbData, BOOL fFinal)
1465 CDecodeMsg *msg = (CDecodeMsg *)hCryptMsg;
1466 BOOL ret = FALSE;
1468 TRACE("(%p, %p, %d, %d)\n", hCryptMsg, pbData, cbData, fFinal);
1470 if (msg->base.streamed)
1472 ret = CDecodeMsg_CopyData(msg, pbData, cbData);
1473 FIXME("(%p, %p, %d, %d): streamed update stub\n", hCryptMsg, pbData,
1474 cbData, fFinal);
1476 else
1478 if (!fFinal)
1479 SetLastError(CRYPT_E_MSG_ERROR);
1480 else
1482 ret = CDecodeMsg_CopyData(msg, pbData, cbData);
1483 if (ret)
1484 ret = CDecodeMsg_DecodeContent(msg, &msg->msg_data, msg->type);
1488 return ret;
1491 static BOOL CDecodeHashMsg_GetParam(CDecodeMsg *msg, DWORD dwParamType,
1492 DWORD dwIndex, void *pvData, DWORD *pcbData)
1494 BOOL ret = FALSE;
1496 switch (dwParamType)
1498 case CMSG_TYPE_PARAM:
1499 ret = CRYPT_CopyParam(pvData, pcbData, &msg->type, sizeof(msg->type));
1500 break;
1501 case CMSG_HASH_ALGORITHM_PARAM:
1503 CRYPT_DATA_BLOB blob;
1505 ret = ContextPropertyList_FindProperty(msg->properties, dwParamType,
1506 &blob);
1507 if (ret)
1509 ret = CRYPT_CopyParam(pvData, pcbData, blob.pbData, blob.cbData);
1510 if (ret && pvData)
1511 CRYPT_FixUpAlgorithmID((CRYPT_ALGORITHM_IDENTIFIER *)pvData);
1513 else
1514 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1515 break;
1517 case CMSG_COMPUTED_HASH_PARAM:
1518 if (!msg->u.hash)
1520 CRYPT_ALGORITHM_IDENTIFIER *hashAlgoID = NULL;
1521 DWORD size = 0;
1522 ALG_ID algID = 0;
1524 CryptMsgGetParam(msg, CMSG_HASH_ALGORITHM_PARAM, 0, NULL, &size);
1525 hashAlgoID = CryptMemAlloc(size);
1526 ret = CryptMsgGetParam(msg, CMSG_HASH_ALGORITHM_PARAM, 0,
1527 hashAlgoID, &size);
1528 if (ret)
1529 algID = CertOIDToAlgId(hashAlgoID->pszObjId);
1530 ret = CryptCreateHash(msg->crypt_prov, algID, 0, 0, &msg->u.hash);
1531 if (ret)
1533 CRYPT_DATA_BLOB content;
1535 ret = ContextPropertyList_FindProperty(msg->properties,
1536 CMSG_CONTENT_PARAM, &content);
1537 if (ret)
1538 ret = CryptHashData(msg->u.hash, content.pbData,
1539 content.cbData, 0);
1541 CryptMemFree(hashAlgoID);
1543 else
1544 ret = TRUE;
1545 if (ret)
1546 ret = CryptGetHashParam(msg->u.hash, HP_HASHVAL, pvData, pcbData,
1548 break;
1549 default:
1551 CRYPT_DATA_BLOB blob;
1553 ret = ContextPropertyList_FindProperty(msg->properties, dwParamType,
1554 &blob);
1555 if (ret)
1556 ret = CRYPT_CopyParam(pvData, pcbData, blob.pbData, blob.cbData);
1557 else
1558 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1561 return ret;
1564 /* nextData is an in/out parameter - on input it's the memory location in
1565 * which a copy of in's data should be made, and on output it's the memory
1566 * location immediately after out's copy of in's data.
1568 static inline void CRYPT_CopyBlob(CRYPT_DATA_BLOB *out,
1569 const CRYPT_DATA_BLOB *in, LPBYTE *nextData)
1571 out->cbData = in->cbData;
1572 if (in->cbData)
1574 out->pbData = *nextData;
1575 memcpy(out->pbData, in->pbData, in->cbData);
1576 *nextData += in->cbData;
1580 static inline void CRYPT_CopyAlgorithmId(CRYPT_ALGORITHM_IDENTIFIER *out,
1581 const CRYPT_ALGORITHM_IDENTIFIER *in, LPBYTE *nextData)
1583 if (in->pszObjId)
1585 out->pszObjId = (LPSTR)*nextData;
1586 strcpy(out->pszObjId, in->pszObjId);
1587 *nextData += strlen(out->pszObjId) + 1;
1589 CRYPT_CopyBlob(&out->Parameters, &in->Parameters, nextData);
1592 static inline void CRYPT_CopyAttributes(CRYPT_ATTRIBUTES *out,
1593 const CRYPT_ATTRIBUTES *in, LPBYTE *nextData)
1595 out->cAttr = in->cAttr;
1596 if (in->cAttr)
1598 DWORD i;
1600 if ((*nextData - (LPBYTE)0) % sizeof(DWORD))
1601 *nextData += (*nextData - (LPBYTE)0) % sizeof(DWORD);
1602 out->rgAttr = (CRYPT_ATTRIBUTE *)*nextData;
1603 *nextData += in->cAttr * sizeof(CRYPT_ATTRIBUTE);
1604 for (i = 0; i < in->cAttr; i++)
1606 if (in->rgAttr[i].pszObjId)
1608 out->rgAttr[i].pszObjId = (LPSTR)*nextData;
1609 strcpy(out->rgAttr[i].pszObjId, in->rgAttr[i].pszObjId);
1610 *nextData += strlen(in->rgAttr[i].pszObjId) + 1;
1612 if (in->rgAttr[i].cValue)
1614 DWORD j;
1616 out->rgAttr[i].cValue = in->rgAttr[i].cValue;
1617 if ((*nextData - (LPBYTE)0) % sizeof(DWORD))
1618 *nextData += (*nextData - (LPBYTE)0) % sizeof(DWORD);
1619 out->rgAttr[i].rgValue = (PCRYPT_DATA_BLOB)*nextData;
1620 for (j = 0; j < in->rgAttr[i].cValue; j++)
1621 CRYPT_CopyBlob(&out->rgAttr[i].rgValue[j],
1622 &in->rgAttr[i].rgValue[j], nextData);
1628 static DWORD CRYPT_SizeOfAttributes(const CRYPT_ATTRIBUTES *attr)
1630 DWORD size = attr->cAttr * sizeof(CRYPT_ATTRIBUTE), i, j;
1632 for (i = 0; i < attr->cAttr; i++)
1634 if (attr->rgAttr[i].pszObjId)
1635 size += strlen(attr->rgAttr[i].pszObjId) + 1;
1636 /* align pointer */
1637 if (size % sizeof(DWORD))
1638 size += size % sizeof(DWORD);
1639 size += attr->rgAttr[i].cValue * sizeof(CRYPT_DATA_BLOB);
1640 for (j = 0; j < attr->rgAttr[i].cValue; j++)
1641 size += attr->rgAttr[i].rgValue[j].cbData;
1643 return size;
1646 static BOOL CRYPT_CopySignerInfo(void *pvData, DWORD *pcbData,
1647 const CMSG_SIGNER_INFO *in)
1649 DWORD size = sizeof(CMSG_SIGNER_INFO);
1650 BOOL ret;
1652 size += in->Issuer.cbData;
1653 size += in->SerialNumber.cbData;
1654 if (in->HashAlgorithm.pszObjId)
1655 size += strlen(in->HashAlgorithm.pszObjId) + 1;
1656 size += in->HashAlgorithm.Parameters.cbData;
1657 if (in->HashEncryptionAlgorithm.pszObjId)
1658 size += strlen(in->HashEncryptionAlgorithm.pszObjId) + 1;
1659 size += in->HashEncryptionAlgorithm.Parameters.cbData;
1660 size += in->EncryptedHash.cbData;
1661 /* align pointer */
1662 if (size % sizeof(DWORD))
1663 size += size % sizeof(DWORD);
1664 size += CRYPT_SizeOfAttributes(&in->AuthAttrs);
1665 size += CRYPT_SizeOfAttributes(&in->UnauthAttrs);
1666 if (!pvData)
1668 *pcbData = size;
1669 ret = TRUE;
1671 else if (*pcbData < size)
1673 *pcbData = size;
1674 SetLastError(ERROR_MORE_DATA);
1675 ret = FALSE;
1677 else
1679 LPBYTE nextData = (BYTE *)pvData + sizeof(CMSG_SIGNER_INFO);
1680 CMSG_SIGNER_INFO *out = (CMSG_SIGNER_INFO *)pvData;
1682 out->dwVersion = in->dwVersion;
1683 CRYPT_CopyBlob(&out->Issuer, &in->Issuer, &nextData);
1684 CRYPT_CopyBlob(&out->SerialNumber, &in->SerialNumber, &nextData);
1685 CRYPT_CopyAlgorithmId(&out->HashAlgorithm, &in->HashAlgorithm,
1686 &nextData);
1687 CRYPT_CopyAlgorithmId(&out->HashEncryptionAlgorithm,
1688 &in->HashEncryptionAlgorithm, &nextData);
1689 CRYPT_CopyBlob(&out->EncryptedHash, &in->EncryptedHash, &nextData);
1690 /* align pointer */
1691 if ((nextData - (LPBYTE)0) % sizeof(DWORD))
1692 nextData += (nextData - (LPBYTE)0) % sizeof(DWORD);
1693 CRYPT_CopyAttributes(&out->AuthAttrs, &in->AuthAttrs, &nextData);
1694 CRYPT_CopyAttributes(&out->UnauthAttrs, &in->UnauthAttrs, &nextData);
1695 ret = TRUE;
1697 return ret;
1700 static BOOL CRYPT_CopySignerCertInfo(void *pvData, DWORD *pcbData,
1701 const CMSG_SIGNER_INFO *in)
1703 DWORD size = sizeof(CERT_INFO);
1704 BOOL ret;
1706 size += in->Issuer.cbData;
1707 size += in->SerialNumber.cbData;
1708 if (!pvData)
1710 *pcbData = size;
1711 ret = TRUE;
1713 else if (*pcbData < size)
1715 *pcbData = size;
1716 SetLastError(ERROR_MORE_DATA);
1717 ret = FALSE;
1719 else
1721 LPBYTE nextData = (BYTE *)pvData + sizeof(CERT_INFO);
1722 CERT_INFO *out = (CERT_INFO *)pvData;
1724 memset(out, 0, sizeof(CERT_INFO));
1725 CRYPT_CopyBlob(&out->Issuer, &in->Issuer, &nextData);
1726 CRYPT_CopyBlob(&out->SerialNumber, &in->SerialNumber, &nextData);
1727 ret = TRUE;
1729 return ret;
1732 static BOOL CDecodeSignedMsg_GetParam(CDecodeMsg *msg, DWORD dwParamType,
1733 DWORD dwIndex, void *pvData, DWORD *pcbData)
1735 BOOL ret = FALSE;
1737 switch (dwParamType)
1739 case CMSG_TYPE_PARAM:
1740 ret = CRYPT_CopyParam(pvData, pcbData, &msg->type, sizeof(msg->type));
1741 break;
1742 case CMSG_CONTENT_PARAM:
1743 if (msg->u.signedInfo)
1745 CRYPT_DATA_BLOB *blob;
1746 DWORD size;
1748 /* FIXME: does this depend on inner content type? */
1749 ret = CryptDecodeObjectEx(X509_ASN_ENCODING, X509_OCTET_STRING,
1750 msg->u.signedInfo->content.Content.pbData,
1751 msg->u.signedInfo->content.Content.cbData, CRYPT_DECODE_ALLOC_FLAG,
1752 NULL, (LPBYTE)&blob, &size);
1753 if (ret)
1755 ret = CRYPT_CopyParam(pvData, pcbData, blob->pbData,
1756 blob->cbData);
1757 LocalFree(blob);
1760 else
1761 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1762 break;
1763 case CMSG_INNER_CONTENT_TYPE_PARAM:
1764 if (msg->u.signedInfo)
1765 ret = CRYPT_CopyParam(pvData, pcbData,
1766 msg->u.signedInfo->content.pszObjId,
1767 strlen(msg->u.signedInfo->content.pszObjId) + 1);
1768 else
1769 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1770 break;
1771 case CMSG_SIGNER_COUNT_PARAM:
1772 if (msg->u.signedInfo)
1773 ret = CRYPT_CopyParam(pvData, pcbData,
1774 &msg->u.signedInfo->cSignerInfo, sizeof(DWORD));
1775 else
1776 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1777 break;
1778 case CMSG_SIGNER_INFO_PARAM:
1779 if (msg->u.signedInfo)
1781 if (dwIndex >= msg->u.signedInfo->cSignerInfo)
1782 SetLastError(CRYPT_E_INVALID_INDEX);
1783 else
1784 ret = CRYPT_CopySignerInfo(pvData, pcbData,
1785 &msg->u.signedInfo->rgSignerInfo[dwIndex]);
1787 else
1788 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1789 break;
1790 case CMSG_SIGNER_CERT_INFO_PARAM:
1791 if (msg->u.signedInfo)
1793 if (dwIndex >= msg->u.signedInfo->cSignerInfo)
1794 SetLastError(CRYPT_E_INVALID_INDEX);
1795 else
1796 ret = CRYPT_CopySignerCertInfo(pvData, pcbData,
1797 &msg->u.signedInfo->rgSignerInfo[dwIndex]);
1799 else
1800 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1801 break;
1802 case CMSG_CERT_COUNT_PARAM:
1803 if (msg->u.signedInfo)
1804 ret = CRYPT_CopyParam(pvData, pcbData,
1805 &msg->u.signedInfo->cCertEncoded, sizeof(DWORD));
1806 else
1807 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1808 break;
1809 case CMSG_CERT_PARAM:
1810 if (msg->u.signedInfo)
1812 if (dwIndex >= msg->u.signedInfo->cCertEncoded)
1813 SetLastError(CRYPT_E_INVALID_INDEX);
1814 else
1815 ret = CRYPT_CopyParam(pvData, pcbData,
1816 msg->u.signedInfo->rgCertEncoded[dwIndex].pbData,
1817 msg->u.signedInfo->rgCertEncoded[dwIndex].cbData);
1819 else
1820 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1821 break;
1822 case CMSG_CRL_COUNT_PARAM:
1823 if (msg->u.signedInfo)
1824 ret = CRYPT_CopyParam(pvData, pcbData,
1825 &msg->u.signedInfo->cCrlEncoded, sizeof(DWORD));
1826 else
1827 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1828 break;
1829 case CMSG_CRL_PARAM:
1830 if (msg->u.signedInfo)
1832 if (dwIndex >= msg->u.signedInfo->cCrlEncoded)
1833 SetLastError(CRYPT_E_INVALID_INDEX);
1834 else
1835 ret = CRYPT_CopyParam(pvData, pcbData,
1836 msg->u.signedInfo->rgCrlEncoded[dwIndex].pbData,
1837 msg->u.signedInfo->rgCrlEncoded[dwIndex].cbData);
1839 else
1840 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1841 break;
1842 case CMSG_ATTR_CERT_COUNT_PARAM:
1843 if (msg->u.signedInfo)
1845 DWORD attrCertCount = 0;
1847 ret = CRYPT_CopyParam(pvData, pcbData,
1848 &attrCertCount, sizeof(DWORD));
1850 else
1851 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1852 break;
1853 case CMSG_ATTR_CERT_PARAM:
1854 if (msg->u.signedInfo)
1855 SetLastError(CRYPT_E_INVALID_INDEX);
1856 else
1857 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1858 break;
1859 default:
1860 FIXME("unimplemented for %d\n", dwParamType);
1861 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1863 return ret;
1866 static BOOL CDecodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
1867 DWORD dwIndex, void *pvData, DWORD *pcbData)
1869 CDecodeMsg *msg = (CDecodeMsg *)hCryptMsg;
1870 BOOL ret = FALSE;
1872 switch (msg->type)
1874 case CMSG_HASHED:
1875 ret = CDecodeHashMsg_GetParam(msg, dwParamType, dwIndex, pvData,
1876 pcbData);
1877 break;
1878 case CMSG_SIGNED:
1879 ret = CDecodeSignedMsg_GetParam(msg, dwParamType, dwIndex, pvData,
1880 pcbData);
1881 break;
1882 default:
1883 switch (dwParamType)
1885 case CMSG_TYPE_PARAM:
1886 ret = CRYPT_CopyParam(pvData, pcbData, &msg->type,
1887 sizeof(msg->type));
1888 break;
1889 default:
1891 CRYPT_DATA_BLOB blob;
1893 ret = ContextPropertyList_FindProperty(msg->properties, dwParamType,
1894 &blob);
1895 if (ret)
1896 ret = CRYPT_CopyParam(pvData, pcbData, blob.pbData,
1897 blob.cbData);
1898 else
1899 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1903 return ret;
1906 HCRYPTMSG WINAPI CryptMsgOpenToDecode(DWORD dwMsgEncodingType, DWORD dwFlags,
1907 DWORD dwMsgType, HCRYPTPROV_LEGACY hCryptProv, PCERT_INFO pRecipientInfo,
1908 PCMSG_STREAM_INFO pStreamInfo)
1910 CDecodeMsg *msg;
1912 TRACE("(%08x, %08x, %08x, %08lx, %p, %p)\n", dwMsgEncodingType,
1913 dwFlags, dwMsgType, hCryptProv, pRecipientInfo, pStreamInfo);
1915 if (GET_CMSG_ENCODING_TYPE(dwMsgEncodingType) != PKCS_7_ASN_ENCODING)
1917 SetLastError(E_INVALIDARG);
1918 return NULL;
1920 msg = CryptMemAlloc(sizeof(CDecodeMsg));
1921 if (msg)
1923 CryptMsgBase_Init((CryptMsgBase *)msg, dwFlags, pStreamInfo,
1924 CDecodeMsg_Close, CDecodeMsg_GetParam, CDecodeMsg_Update);
1925 msg->type = dwMsgType;
1926 if (hCryptProv)
1927 msg->crypt_prov = hCryptProv;
1928 else
1930 msg->crypt_prov = CRYPT_GetDefaultProvider();
1931 msg->base.open_flags &= ~CMSG_CRYPT_RELEASE_CONTEXT_FLAG;
1933 memset(&msg->u, 0, sizeof(msg->u));
1934 msg->msg_data.cbData = 0;
1935 msg->msg_data.pbData = NULL;
1936 msg->properties = ContextPropertyList_Create();
1938 return msg;
1941 HCRYPTMSG WINAPI CryptMsgDuplicate(HCRYPTMSG hCryptMsg)
1943 TRACE("(%p)\n", hCryptMsg);
1945 if (hCryptMsg)
1947 CryptMsgBase *msg = (CryptMsgBase *)hCryptMsg;
1949 InterlockedIncrement(&msg->ref);
1951 return hCryptMsg;
1954 BOOL WINAPI CryptMsgClose(HCRYPTMSG hCryptMsg)
1956 TRACE("(%p)\n", hCryptMsg);
1958 if (hCryptMsg)
1960 CryptMsgBase *msg = (CryptMsgBase *)hCryptMsg;
1962 if (InterlockedDecrement(&msg->ref) == 0)
1964 TRACE("freeing %p\n", msg);
1965 if (msg->close)
1966 msg->close(msg);
1967 CryptMemFree(msg);
1970 return TRUE;
1973 BOOL WINAPI CryptMsgUpdate(HCRYPTMSG hCryptMsg, const BYTE *pbData,
1974 DWORD cbData, BOOL fFinal)
1976 CryptMsgBase *msg = (CryptMsgBase *)hCryptMsg;
1977 BOOL ret = FALSE;
1979 TRACE("(%p, %p, %d, %d)\n", hCryptMsg, pbData, cbData, fFinal);
1981 if (msg->state == MsgStateFinalized)
1982 SetLastError(CRYPT_E_MSG_ERROR);
1983 else
1985 ret = msg->update(hCryptMsg, pbData, cbData, fFinal);
1986 msg->state = MsgStateUpdated;
1987 if (fFinal)
1988 msg->state = MsgStateFinalized;
1990 return ret;
1993 BOOL WINAPI CryptMsgGetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
1994 DWORD dwIndex, void *pvData, DWORD *pcbData)
1996 CryptMsgBase *msg = (CryptMsgBase *)hCryptMsg;
1998 TRACE("(%p, %d, %d, %p, %p)\n", hCryptMsg, dwParamType, dwIndex,
1999 pvData, pcbData);
2000 return msg->get_param(hCryptMsg, dwParamType, dwIndex, pvData, pcbData);
2003 BOOL WINAPI CryptMsgControl(HCRYPTMSG hCryptMsg, DWORD dwFlags,
2004 DWORD dwCtrlType, const void *pvCtrlPara)
2006 FIXME("(%p, %08x, %d, %p): stub\n", hCryptMsg, dwFlags, dwCtrlType,
2007 pvCtrlPara);
2008 return TRUE;