mshtml: Added IHTMLScriptElement::put_text implementation.
[wine] / dlls / crypt32 / msg.c
1 /*
2  * Copyright 2007 Juan Lang
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include "config.h"
20 #include "wine/port.h"
21
22 #include <stdarg.h>
23 #define NONAMELESSUNION
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wincrypt.h"
27 #include "snmp.h"
28
29 #include "wine/debug.h"
30 #include "wine/exception.h"
31 #include "crypt32_private.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
34
35 /* Called when a message's ref count reaches zero.  Free any message-specific
36  * data here.
37  */
38 typedef void (*CryptMsgCloseFunc)(HCRYPTMSG msg);
39
40 typedef BOOL (*CryptMsgGetParamFunc)(HCRYPTMSG hCryptMsg, DWORD dwParamType,
41  DWORD dwIndex, void *pvData, DWORD *pcbData);
42
43 typedef BOOL (*CryptMsgUpdateFunc)(HCRYPTMSG hCryptMsg, const BYTE *pbData,
44  DWORD cbData, BOOL fFinal);
45
46 typedef BOOL (*CryptMsgControlFunc)(HCRYPTMSG hCryptMsg, DWORD dwFlags,
47  DWORD dwCtrlType, const void *pvCtrlPara);
48
49 static BOOL CRYPT_DefaultMsgControl(HCRYPTMSG hCryptMsg, DWORD dwFlags,
50  DWORD dwCtrlType, const void *pvCtrlPara)
51 {
52     TRACE("(%p, %08x, %d, %p)\n", hCryptMsg, dwFlags, dwCtrlType, pvCtrlPara);
53     SetLastError(E_INVALIDARG);
54     return FALSE;
55 }
56
57 typedef enum _CryptMsgState {
58     MsgStateInit,
59     MsgStateUpdated,
60     MsgStateDataFinalized,
61     MsgStateFinalized
62 } CryptMsgState;
63
64 typedef struct _CryptMsgBase
65 {
66     LONG                 ref;
67     DWORD                open_flags;
68     BOOL                 streamed;
69     CMSG_STREAM_INFO     stream_info;
70     CryptMsgState        state;
71     CryptMsgCloseFunc    close;
72     CryptMsgUpdateFunc   update;
73     CryptMsgGetParamFunc get_param;
74     CryptMsgControlFunc  control;
75 } CryptMsgBase;
76
77 static inline void CryptMsgBase_Init(CryptMsgBase *msg, DWORD dwFlags,
78  PCMSG_STREAM_INFO pStreamInfo, CryptMsgCloseFunc close,
79  CryptMsgGetParamFunc get_param, CryptMsgUpdateFunc update,
80  CryptMsgControlFunc control)
81 {
82     msg->ref = 1;
83     msg->open_flags = dwFlags;
84     if (pStreamInfo)
85     {
86         msg->streamed = TRUE;
87         msg->stream_info = *pStreamInfo;
88     }
89     else
90     {
91         msg->streamed = FALSE;
92         memset(&msg->stream_info, 0, sizeof(msg->stream_info));
93     }
94     msg->close = close;
95     msg->get_param = get_param;
96     msg->update = update;
97     msg->control = control;
98     msg->state = MsgStateInit;
99 }
100
101 typedef struct _CDataEncodeMsg
102 {
103     CryptMsgBase base;
104     DWORD        bare_content_len;
105     LPBYTE       bare_content;
106 } CDataEncodeMsg;
107
108 static const BYTE empty_data_content[] = { 0x04,0x00 };
109
110 static void CDataEncodeMsg_Close(HCRYPTMSG hCryptMsg)
111 {
112     CDataEncodeMsg *msg = hCryptMsg;
113
114     if (msg->bare_content != empty_data_content)
115         LocalFree(msg->bare_content);
116 }
117
118 static BOOL WINAPI CRYPT_EncodeContentLength(DWORD dwCertEncodingType,
119  LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
120  PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
121 {
122     DWORD dataLen = *(DWORD *)pvStructInfo;
123     DWORD lenBytes;
124     BOOL ret = TRUE;
125
126     /* Trick:  report bytes needed based on total message length, even though
127      * the message isn't available yet.  The caller will use the length
128      * reported here to encode its length.
129      */
130     CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
131     if (!pbEncoded)
132         *pcbEncoded = 1 + lenBytes + dataLen;
133     else
134     {
135         if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
136          pcbEncoded, 1 + lenBytes)))
137         {
138             if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
139                 pbEncoded = *(BYTE **)pbEncoded;
140             *pbEncoded++ = ASN_OCTETSTRING;
141             CRYPT_EncodeLen(dataLen, pbEncoded,
142              &lenBytes);
143         }
144     }
145     return ret;
146 }
147
148 static BOOL CRYPT_EncodeDataContentInfoHeader(const CDataEncodeMsg *msg,
149  CRYPT_DATA_BLOB *header)
150 {
151     BOOL ret;
152
153     if (msg->base.streamed && msg->base.stream_info.cbContent == 0xffffffff)
154     {
155         static const BYTE headerValue[] = { 0x30,0x80,0x06,0x09,0x2a,0x86,0x48,
156          0x86,0xf7,0x0d,0x01,0x07,0x01,0xa0,0x80,0x24,0x80 };
157
158         header->pbData = LocalAlloc(0, sizeof(headerValue));
159         if (header->pbData)
160         {
161             header->cbData = sizeof(headerValue);
162             memcpy(header->pbData, headerValue, sizeof(headerValue));
163             ret = TRUE;
164         }
165         else
166             ret = FALSE;
167     }
168     else
169     {
170         struct AsnConstructedItem constructed = { 0,
171          &msg->base.stream_info.cbContent, CRYPT_EncodeContentLength };
172         struct AsnEncodeSequenceItem items[2] = {
173          { szOID_RSA_data, CRYPT_AsnEncodeOid, 0 },
174          { &constructed,   CRYPT_AsnEncodeConstructed, 0 },
175         };
176
177         ret = CRYPT_AsnEncodeSequence(X509_ASN_ENCODING, items,
178          sizeof(items) / sizeof(items[0]), CRYPT_ENCODE_ALLOC_FLAG, NULL,
179          (LPBYTE)&header->pbData, &header->cbData);
180         if (ret)
181         {
182             /* Trick:  subtract the content length from the reported length,
183              * as the actual content hasn't come yet.
184              */
185             header->cbData -= msg->base.stream_info.cbContent;
186         }
187     }
188     return ret;
189 }
190
191 static BOOL CDataEncodeMsg_Update(HCRYPTMSG hCryptMsg, const BYTE *pbData,
192  DWORD cbData, BOOL fFinal)
193 {
194     CDataEncodeMsg *msg = hCryptMsg;
195     BOOL ret = FALSE;
196
197     if (msg->base.state == MsgStateFinalized)
198         SetLastError(CRYPT_E_MSG_ERROR);
199     else if (msg->base.streamed)
200     {
201         __TRY
202         {
203             if (msg->base.state != MsgStateUpdated)
204             {
205                 CRYPT_DATA_BLOB header;
206
207                 ret = CRYPT_EncodeDataContentInfoHeader(msg, &header);
208                 if (ret)
209                 {
210                     ret = msg->base.stream_info.pfnStreamOutput(
211                      msg->base.stream_info.pvArg, header.pbData, header.cbData,
212                      FALSE);
213                     LocalFree(header.pbData);
214                 }
215             }
216             /* Curiously, every indefinite-length streamed update appears to
217              * get its own tag and length, regardless of fFinal.
218              */
219             if (msg->base.stream_info.cbContent == 0xffffffff)
220             {
221                 BYTE *header;
222                 DWORD headerLen;
223
224                 ret = CRYPT_EncodeContentLength(X509_ASN_ENCODING, NULL,
225                  &cbData, CRYPT_ENCODE_ALLOC_FLAG, NULL, (BYTE *)&header,
226                  &headerLen);
227                 if (ret)
228                 {
229                     ret = msg->base.stream_info.pfnStreamOutput(
230                      msg->base.stream_info.pvArg, header, headerLen,
231                      FALSE);
232                     LocalFree(header);
233                 }
234             }
235             if (!fFinal)
236             {
237                 ret = msg->base.stream_info.pfnStreamOutput(
238                  msg->base.stream_info.pvArg, (BYTE *)pbData, cbData,
239                  FALSE);
240                 msg->base.state = MsgStateUpdated;
241             }
242             else
243             {
244                 msg->base.state = MsgStateFinalized;
245                 if (msg->base.stream_info.cbContent == 0xffffffff)
246                 {
247                     BYTE indefinite_trailer[6] = { 0 };
248
249                     ret = msg->base.stream_info.pfnStreamOutput(
250                      msg->base.stream_info.pvArg, (BYTE *)pbData, cbData,
251                      FALSE);
252                     if (ret)
253                         ret = msg->base.stream_info.pfnStreamOutput(
254                          msg->base.stream_info.pvArg, indefinite_trailer,
255                          sizeof(indefinite_trailer), TRUE);
256                 }
257                 else
258                     ret = msg->base.stream_info.pfnStreamOutput(
259                      msg->base.stream_info.pvArg, (BYTE *)pbData, cbData, TRUE);
260             }
261         }
262         __EXCEPT_PAGE_FAULT
263         {
264             SetLastError(STATUS_ACCESS_VIOLATION);
265             ret = FALSE;
266         }
267         __ENDTRY;
268     }
269     else
270     {
271         if (!fFinal)
272         {
273             if (msg->base.open_flags & CMSG_DETACHED_FLAG)
274                 SetLastError(E_INVALIDARG);
275             else
276                 SetLastError(CRYPT_E_MSG_ERROR);
277         }
278         else
279         {
280             CRYPT_DATA_BLOB blob = { cbData, (LPBYTE)pbData };
281
282             msg->base.state = MsgStateFinalized;
283             /* non-streamed data messages don't allow non-final updates,
284              * don't bother checking whether data already exist, they can't.
285              */
286             ret = CryptEncodeObjectEx(X509_ASN_ENCODING, X509_OCTET_STRING,
287              &blob, CRYPT_ENCODE_ALLOC_FLAG, NULL, &msg->bare_content,
288              &msg->bare_content_len);
289         }
290     }
291     return ret;
292 }
293
294 static BOOL CRYPT_CopyParam(void *pvData, DWORD *pcbData, const void *src,
295  DWORD len)
296 {
297     BOOL ret = TRUE;
298
299     if (!pvData)
300         *pcbData = len;
301     else if (*pcbData < len)
302     {
303         *pcbData = len;
304         SetLastError(ERROR_MORE_DATA);
305         ret = FALSE;
306     }
307     else
308     {
309         *pcbData = len;
310         memcpy(pvData, src, len);
311     }
312     return ret;
313 }
314
315 static BOOL CDataEncodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
316  DWORD dwIndex, void *pvData, DWORD *pcbData)
317 {
318     CDataEncodeMsg *msg = hCryptMsg;
319     BOOL ret = FALSE;
320
321     switch (dwParamType)
322     {
323     case CMSG_CONTENT_PARAM:
324         if (msg->base.streamed)
325             SetLastError(E_INVALIDARG);
326         else
327         {
328             CRYPT_CONTENT_INFO info;
329             char rsa_data[] = "1.2.840.113549.1.7.1";
330
331             info.pszObjId = rsa_data;
332             info.Content.cbData = msg->bare_content_len;
333             info.Content.pbData = msg->bare_content;
334             ret = CryptEncodeObject(X509_ASN_ENCODING, PKCS_CONTENT_INFO, &info,
335              pvData, pcbData);
336         }
337         break;
338     case CMSG_BARE_CONTENT_PARAM:
339         if (msg->base.streamed)
340             SetLastError(E_INVALIDARG);
341         else
342             ret = CRYPT_CopyParam(pvData, pcbData, msg->bare_content,
343              msg->bare_content_len);
344         break;
345     default:
346         SetLastError(CRYPT_E_INVALID_MSG_TYPE);
347     }
348     return ret;
349 }
350
351 static HCRYPTMSG CDataEncodeMsg_Open(DWORD dwFlags, const void *pvMsgEncodeInfo,
352  LPSTR pszInnerContentObjID, PCMSG_STREAM_INFO pStreamInfo)
353 {
354     CDataEncodeMsg *msg;
355
356     if (pvMsgEncodeInfo)
357     {
358         SetLastError(E_INVALIDARG);
359         return NULL;
360     }
361     msg = CryptMemAlloc(sizeof(CDataEncodeMsg));
362     if (msg)
363     {
364         CryptMsgBase_Init((CryptMsgBase *)msg, dwFlags, pStreamInfo,
365          CDataEncodeMsg_Close, CDataEncodeMsg_GetParam, CDataEncodeMsg_Update,
366          CRYPT_DefaultMsgControl);
367         msg->bare_content_len = sizeof(empty_data_content);
368         msg->bare_content = (LPBYTE)empty_data_content;
369     }
370     return msg;
371 }
372
373 typedef struct _CHashEncodeMsg
374 {
375     CryptMsgBase    base;
376     HCRYPTPROV      prov;
377     HCRYPTHASH      hash;
378     CRYPT_DATA_BLOB data;
379 } CHashEncodeMsg;
380
381 static void CHashEncodeMsg_Close(HCRYPTMSG hCryptMsg)
382 {
383     CHashEncodeMsg *msg = hCryptMsg;
384
385     CryptMemFree(msg->data.pbData);
386     CryptDestroyHash(msg->hash);
387     if (msg->base.open_flags & CMSG_CRYPT_RELEASE_CONTEXT_FLAG)
388         CryptReleaseContext(msg->prov, 0);
389 }
390
391 static BOOL CRYPT_EncodePKCSDigestedData(CHashEncodeMsg *msg, void *pvData,
392  DWORD *pcbData)
393 {
394     BOOL ret;
395     ALG_ID algID;
396     DWORD size = sizeof(algID);
397
398     ret = CryptGetHashParam(msg->hash, HP_ALGID, (BYTE *)&algID, &size, 0);
399     if (ret)
400     {
401         CRYPT_DIGESTED_DATA digestedData = { 0 };
402         char oid_rsa_data[] = szOID_RSA_data;
403
404         digestedData.version = CMSG_HASHED_DATA_PKCS_1_5_VERSION;
405         digestedData.DigestAlgorithm.pszObjId = (LPSTR)CertAlgIdToOID(algID);
406         /* FIXME: what about digestedData.DigestAlgorithm.Parameters? */
407         /* Quirk:  OID is only encoded messages if an update has happened */
408         if (msg->base.state != MsgStateInit)
409             digestedData.ContentInfo.pszObjId = oid_rsa_data;
410         if (!(msg->base.open_flags & CMSG_DETACHED_FLAG) && msg->data.cbData)
411         {
412             ret = CRYPT_AsnEncodeOctets(0, NULL, &msg->data,
413              CRYPT_ENCODE_ALLOC_FLAG, NULL,
414              (LPBYTE)&digestedData.ContentInfo.Content.pbData,
415              &digestedData.ContentInfo.Content.cbData);
416         }
417         if (msg->base.state == MsgStateFinalized)
418         {
419             size = sizeof(DWORD);
420             ret = CryptGetHashParam(msg->hash, HP_HASHSIZE,
421              (LPBYTE)&digestedData.hash.cbData, &size, 0);
422             if (ret)
423             {
424                 digestedData.hash.pbData = CryptMemAlloc(
425                  digestedData.hash.cbData);
426                 ret = CryptGetHashParam(msg->hash, HP_HASHVAL,
427                  digestedData.hash.pbData, &digestedData.hash.cbData, 0);
428             }
429         }
430         if (ret)
431             ret = CRYPT_AsnEncodePKCSDigestedData(&digestedData, pvData,
432              pcbData);
433         CryptMemFree(digestedData.hash.pbData);
434         LocalFree(digestedData.ContentInfo.Content.pbData);
435     }
436     return ret;
437 }
438
439 static BOOL CHashEncodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
440  DWORD dwIndex, void *pvData, DWORD *pcbData)
441 {
442     CHashEncodeMsg *msg = hCryptMsg;
443     BOOL ret = FALSE;
444
445     TRACE("(%p, %d, %d, %p, %p)\n", hCryptMsg, dwParamType, dwIndex,
446      pvData, pcbData);
447
448     switch (dwParamType)
449     {
450     case CMSG_BARE_CONTENT_PARAM:
451         if (msg->base.streamed)
452             SetLastError(E_INVALIDARG);
453         else
454             ret = CRYPT_EncodePKCSDigestedData(msg, pvData, pcbData);
455         break;
456     case CMSG_CONTENT_PARAM:
457     {
458         CRYPT_CONTENT_INFO info;
459
460         ret = CryptMsgGetParam(hCryptMsg, CMSG_BARE_CONTENT_PARAM, 0, NULL,
461          &info.Content.cbData);
462         if (ret)
463         {
464             info.Content.pbData = CryptMemAlloc(info.Content.cbData);
465             if (info.Content.pbData)
466             {
467                 ret = CryptMsgGetParam(hCryptMsg, CMSG_BARE_CONTENT_PARAM, 0,
468                  info.Content.pbData, &info.Content.cbData);
469                 if (ret)
470                 {
471                     char oid_rsa_hashed[] = szOID_RSA_hashedData;
472
473                     info.pszObjId = oid_rsa_hashed;
474                     ret = CryptEncodeObjectEx(X509_ASN_ENCODING,
475                      PKCS_CONTENT_INFO, &info, 0, NULL, pvData, pcbData);
476                 }
477                 CryptMemFree(info.Content.pbData);
478             }
479             else
480                 ret = FALSE;
481         }
482         break;
483     }
484     case CMSG_COMPUTED_HASH_PARAM:
485         ret = CryptGetHashParam(msg->hash, HP_HASHVAL, pvData, pcbData, 0);
486         break;
487     case CMSG_VERSION_PARAM:
488         if (msg->base.state != MsgStateFinalized)
489             SetLastError(CRYPT_E_MSG_ERROR);
490         else
491         {
492             DWORD version = CMSG_HASHED_DATA_PKCS_1_5_VERSION;
493
494             /* Since the data are always encoded as octets, the version is
495              * always 0 (see rfc3852, section 7)
496              */
497             ret = CRYPT_CopyParam(pvData, pcbData, &version, sizeof(version));
498         }
499         break;
500     default:
501         SetLastError(CRYPT_E_INVALID_MSG_TYPE);
502     }
503     return ret;
504 }
505
506 static BOOL CHashEncodeMsg_Update(HCRYPTMSG hCryptMsg, const BYTE *pbData,
507  DWORD cbData, BOOL fFinal)
508 {
509     CHashEncodeMsg *msg = hCryptMsg;
510     BOOL ret = FALSE;
511
512     TRACE("(%p, %p, %d, %d)\n", hCryptMsg, pbData, cbData, fFinal);
513
514     if (msg->base.state == MsgStateFinalized)
515         SetLastError(CRYPT_E_MSG_ERROR);
516     else if (msg->base.streamed || (msg->base.open_flags & CMSG_DETACHED_FLAG))
517     {
518         /* Doesn't do much, as stream output is never called, and you
519          * can't get the content.
520          */
521         ret = CryptHashData(msg->hash, pbData, cbData, 0);
522         msg->base.state = fFinal ? MsgStateFinalized : MsgStateUpdated;
523     }
524     else
525     {
526         if (!fFinal)
527             SetLastError(CRYPT_E_MSG_ERROR);
528         else
529         {
530             ret = CryptHashData(msg->hash, pbData, cbData, 0);
531             if (ret)
532             {
533                 msg->data.pbData = CryptMemAlloc(cbData);
534                 if (msg->data.pbData)
535                 {
536                     memcpy(msg->data.pbData + msg->data.cbData, pbData, cbData);
537                     msg->data.cbData += cbData;
538                 }
539                 else
540                     ret = FALSE;
541             }
542             msg->base.state = MsgStateFinalized;
543         }
544     }
545     return ret;
546 }
547
548 static HCRYPTMSG CHashEncodeMsg_Open(DWORD dwFlags, const void *pvMsgEncodeInfo,
549  LPSTR pszInnerContentObjID, PCMSG_STREAM_INFO pStreamInfo)
550 {
551     CHashEncodeMsg *msg;
552     const CMSG_HASHED_ENCODE_INFO *info = pvMsgEncodeInfo;
553     HCRYPTPROV prov;
554     ALG_ID algID;
555
556     if (info->cbSize != sizeof(CMSG_HASHED_ENCODE_INFO))
557     {
558         SetLastError(E_INVALIDARG);
559         return NULL;
560     }
561     if (!(algID = CertOIDToAlgId(info->HashAlgorithm.pszObjId)))
562     {
563         SetLastError(CRYPT_E_UNKNOWN_ALGO);
564         return NULL;
565     }
566     if (info->hCryptProv)
567         prov = info->hCryptProv;
568     else
569     {
570         prov = CRYPT_GetDefaultProvider();
571         dwFlags &= ~CMSG_CRYPT_RELEASE_CONTEXT_FLAG;
572     }
573     msg = CryptMemAlloc(sizeof(CHashEncodeMsg));
574     if (msg)
575     {
576         CryptMsgBase_Init((CryptMsgBase *)msg, dwFlags, pStreamInfo,
577          CHashEncodeMsg_Close, CHashEncodeMsg_GetParam, CHashEncodeMsg_Update,
578          CRYPT_DefaultMsgControl);
579         msg->prov = prov;
580         msg->data.cbData = 0;
581         msg->data.pbData = NULL;
582         if (!CryptCreateHash(prov, algID, 0, 0, &msg->hash))
583         {
584             CryptMsgClose(msg);
585             msg = NULL;
586         }
587     }
588     return msg;
589 }
590
591 typedef struct _CMSG_SIGNER_ENCODE_INFO_WITH_CMS
592 {
593     DWORD                      cbSize;
594     PCERT_INFO                 pCertInfo;
595     HCRYPTPROV                 hCryptProv;
596     DWORD                      dwKeySpec;
597     CRYPT_ALGORITHM_IDENTIFIER HashAlgorithm;
598     void                      *pvHashAuxInfo;
599     DWORD                      cAuthAttr;
600     PCRYPT_ATTRIBUTE           rgAuthAttr;
601     DWORD                      cUnauthAttr;
602     PCRYPT_ATTRIBUTE           rgUnauthAttr;
603     CERT_ID                    SignerId;
604     CRYPT_ALGORITHM_IDENTIFIER HashEncryptionAlgorithm;
605     void                      *pvHashEncryptionAuxInfo;
606 } CMSG_SIGNER_ENCODE_INFO_WITH_CMS, *PCMSG_SIGNER_ENCODE_INFO_WITH_CMS;
607
608 typedef struct _CMSG_SIGNED_ENCODE_INFO_WITH_CMS
609 {
610     DWORD                             cbSize;
611     DWORD                             cSigners;
612     PCMSG_SIGNER_ENCODE_INFO_WITH_CMS rgSigners;
613     DWORD                             cCertEncoded;
614     PCERT_BLOB                        rgCertEncoded;
615     DWORD                             cCrlEncoded;
616     PCRL_BLOB                         rgCrlEncoded;
617     DWORD                             cAttrCertEncoded;
618     PCERT_BLOB                        rgAttrCertEncoded;
619 } CMSG_SIGNED_ENCODE_INFO_WITH_CMS, *PCMSG_SIGNED_ENCODE_INFO_WITH_CMS;
620
621 static BOOL CRYPT_IsValidSigner(const CMSG_SIGNER_ENCODE_INFO_WITH_CMS *signer)
622 {
623     if (signer->cbSize != sizeof(CMSG_SIGNER_ENCODE_INFO) &&
624      signer->cbSize != sizeof(CMSG_SIGNER_ENCODE_INFO_WITH_CMS))
625     {
626         SetLastError(E_INVALIDARG);
627         return FALSE;
628     }
629     if (signer->cbSize == sizeof(CMSG_SIGNER_ENCODE_INFO))
630     {
631         if (!signer->pCertInfo->SerialNumber.cbData)
632         {
633             SetLastError(E_INVALIDARG);
634             return FALSE;
635         }
636         if (!signer->pCertInfo->Issuer.cbData)
637         {
638             SetLastError(E_INVALIDARG);
639             return FALSE;
640         }
641     }
642     else if (signer->cbSize == sizeof(CMSG_SIGNER_ENCODE_INFO_WITH_CMS))
643     {
644         switch (signer->SignerId.dwIdChoice)
645         {
646         case 0:
647             if (!signer->pCertInfo->SerialNumber.cbData)
648             {
649                 SetLastError(E_INVALIDARG);
650                 return FALSE;
651             }
652             if (!signer->pCertInfo->Issuer.cbData)
653             {
654                 SetLastError(E_INVALIDARG);
655                 return FALSE;
656             }
657             break;
658         case CERT_ID_ISSUER_SERIAL_NUMBER:
659             if (!signer->SignerId.u.IssuerSerialNumber.SerialNumber.cbData)
660             {
661                 SetLastError(E_INVALIDARG);
662                 return FALSE;
663             }
664             if (!signer->SignerId.u.IssuerSerialNumber.Issuer.cbData)
665             {
666                 SetLastError(E_INVALIDARG);
667                 return FALSE;
668             }
669             break;
670         case CERT_ID_KEY_IDENTIFIER:
671             if (!signer->SignerId.u.KeyId.cbData)
672             {
673                 SetLastError(E_INVALIDARG);
674                 return FALSE;
675             }
676             break;
677         default:
678             SetLastError(E_INVALIDARG);
679         }
680         if (signer->HashEncryptionAlgorithm.pszObjId)
681         {
682             FIXME("CMSG_SIGNER_ENCODE_INFO with CMS fields unsupported\n");
683             return FALSE;
684         }
685     }
686     if (!signer->hCryptProv)
687     {
688         SetLastError(E_INVALIDARG);
689         return FALSE;
690     }
691     if (!CertOIDToAlgId(signer->HashAlgorithm.pszObjId))
692     {
693         SetLastError(CRYPT_E_UNKNOWN_ALGO);
694         return FALSE;
695     }
696     return TRUE;
697 }
698
699 static BOOL CRYPT_ConstructBlob(CRYPT_DATA_BLOB *out, const CRYPT_DATA_BLOB *in)
700 {
701     BOOL ret = TRUE;
702
703     out->cbData = in->cbData;
704     if (out->cbData)
705     {
706         out->pbData = CryptMemAlloc(out->cbData);
707         if (out->pbData)
708             memcpy(out->pbData, in->pbData, out->cbData);
709         else
710             ret = FALSE;
711     }
712     else
713         out->pbData = NULL;
714     return ret;
715 }
716
717 static BOOL CRYPT_ConstructBlobArray(DWORD *outCBlobs,
718  PCRYPT_DATA_BLOB *outPBlobs, DWORD cBlobs, const CRYPT_DATA_BLOB *pBlobs)
719 {
720     BOOL ret = TRUE;
721
722     *outCBlobs = cBlobs;
723     if (cBlobs)
724     {
725         *outPBlobs = CryptMemAlloc(cBlobs * sizeof(CRYPT_DATA_BLOB));
726         if (*outPBlobs)
727         {
728             DWORD i;
729
730             memset(*outPBlobs, 0, cBlobs * sizeof(CRYPT_DATA_BLOB));
731             for (i = 0; ret && i < cBlobs; i++)
732                 ret = CRYPT_ConstructBlob(&(*outPBlobs)[i], &pBlobs[i]);
733         }
734         else
735             ret = FALSE;
736     }
737     return ret;
738 }
739
740 static void CRYPT_FreeBlobArray(DWORD cBlobs, PCRYPT_DATA_BLOB blobs)
741 {
742     DWORD i;
743
744     for (i = 0; i < cBlobs; i++)
745         CryptMemFree(blobs[i].pbData);
746     CryptMemFree(blobs);
747 }
748
749 static BOOL CRYPT_ConstructAttribute(CRYPT_ATTRIBUTE *out,
750  const CRYPT_ATTRIBUTE *in)
751 {
752     BOOL ret;
753
754     out->pszObjId = CryptMemAlloc(strlen(in->pszObjId) + 1);
755     if (out->pszObjId)
756     {
757         strcpy(out->pszObjId, in->pszObjId);
758         ret = CRYPT_ConstructBlobArray(&out->cValue, &out->rgValue,
759          in->cValue, in->rgValue);
760     }
761     else
762         ret = FALSE;
763     return ret;
764 }
765
766 static BOOL CRYPT_ConstructAttributes(CRYPT_ATTRIBUTES *out,
767  const CRYPT_ATTRIBUTES *in)
768 {
769     BOOL ret = TRUE;
770
771     out->cAttr = in->cAttr;
772     if (out->cAttr)
773     {
774         out->rgAttr = CryptMemAlloc(out->cAttr * sizeof(CRYPT_ATTRIBUTE));
775         if (out->rgAttr)
776         {
777             DWORD i;
778
779             memset(out->rgAttr, 0, out->cAttr * sizeof(CRYPT_ATTRIBUTE));
780             for (i = 0; ret && i < out->cAttr; i++)
781                 ret = CRYPT_ConstructAttribute(&out->rgAttr[i], &in->rgAttr[i]);
782         }
783         else
784             ret = FALSE;
785     }
786     else
787         out->rgAttr = NULL;
788     return ret;
789 }
790
791 /* Constructs a CMSG_CMS_SIGNER_INFO from a CMSG_SIGNER_ENCODE_INFO_WITH_CMS. */
792 static BOOL CSignerInfo_Construct(CMSG_CMS_SIGNER_INFO *info,
793  const CMSG_SIGNER_ENCODE_INFO_WITH_CMS *in)
794 {
795     BOOL ret;
796
797     if (in->cbSize == sizeof(CMSG_SIGNER_ENCODE_INFO))
798     {
799         info->dwVersion = CMSG_SIGNER_INFO_V1;
800         ret = CRYPT_ConstructBlob(&info->SignerId.u.IssuerSerialNumber.Issuer,
801          &in->pCertInfo->Issuer);
802         if (ret)
803             ret = CRYPT_ConstructBlob(
804              &info->SignerId.u.IssuerSerialNumber.SerialNumber,
805              &in->pCertInfo->SerialNumber);
806         info->SignerId.dwIdChoice = CERT_ID_ISSUER_SERIAL_NUMBER;
807         info->HashEncryptionAlgorithm.pszObjId =
808          in->pCertInfo->SubjectPublicKeyInfo.Algorithm.pszObjId;
809         if (ret)
810             ret = CRYPT_ConstructBlob(&info->HashEncryptionAlgorithm.Parameters,
811              &in->pCertInfo->SubjectPublicKeyInfo.Algorithm.Parameters);
812     }
813     else
814     {
815         const CRYPT_ALGORITHM_IDENTIFIER *pEncrAlg;
816
817         /* Implicitly in->cbSize == sizeof(CMSG_SIGNER_ENCODE_INFO_WITH_CMS).
818          * See CRYPT_IsValidSigner.
819          */
820         if (!in->SignerId.dwIdChoice)
821         {
822             info->dwVersion = CMSG_SIGNER_INFO_V1;
823             ret = CRYPT_ConstructBlob(&info->SignerId.u.IssuerSerialNumber.Issuer,
824              &in->pCertInfo->Issuer);
825             if (ret)
826                 ret = CRYPT_ConstructBlob(
827                  &info->SignerId.u.IssuerSerialNumber.SerialNumber,
828                  &in->pCertInfo->SerialNumber);
829             info->SignerId.dwIdChoice = CERT_ID_ISSUER_SERIAL_NUMBER;
830         }
831         else if (in->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
832         {
833             info->dwVersion = CMSG_SIGNER_INFO_V1;
834             info->SignerId.dwIdChoice = CERT_ID_ISSUER_SERIAL_NUMBER;
835             ret = CRYPT_ConstructBlob(&info->SignerId.u.IssuerSerialNumber.Issuer,
836              &in->SignerId.u.IssuerSerialNumber.Issuer);
837             if (ret)
838                 ret = CRYPT_ConstructBlob(
839                  &info->SignerId.u.IssuerSerialNumber.SerialNumber,
840                  &in->SignerId.u.IssuerSerialNumber.SerialNumber);
841         }
842         else
843         {
844             /* Implicitly dwIdChoice == CERT_ID_KEY_IDENTIFIER */
845             info->dwVersion = CMSG_SIGNER_INFO_V3;
846             info->SignerId.dwIdChoice = CERT_ID_KEY_IDENTIFIER;
847             ret = CRYPT_ConstructBlob(&info->SignerId.u.KeyId,
848              &in->SignerId.u.KeyId);
849         }
850         pEncrAlg = in->HashEncryptionAlgorithm.pszObjId ?
851          &in->HashEncryptionAlgorithm :
852          &in->pCertInfo->SubjectPublicKeyInfo.Algorithm;
853         info->HashEncryptionAlgorithm.pszObjId = pEncrAlg->pszObjId;
854         if (ret)
855             ret = CRYPT_ConstructBlob(&info->HashEncryptionAlgorithm.Parameters,
856              &pEncrAlg->Parameters);
857     }
858     /* Assumption:  algorithm IDs will point to static strings, not
859      * stack-based ones, so copying the pointer values is safe.
860      */
861     info->HashAlgorithm.pszObjId = in->HashAlgorithm.pszObjId;
862     if (ret)
863         ret = CRYPT_ConstructBlob(&info->HashAlgorithm.Parameters,
864          &in->HashAlgorithm.Parameters);
865     if (ret)
866         ret = CRYPT_ConstructAttributes(&info->AuthAttrs,
867          (CRYPT_ATTRIBUTES *)&in->cAuthAttr);
868     if (ret)
869         ret = CRYPT_ConstructAttributes(&info->UnauthAttrs,
870          (CRYPT_ATTRIBUTES *)&in->cUnauthAttr);
871     return ret;
872 }
873
874 static void CSignerInfo_Free(CMSG_CMS_SIGNER_INFO *info)
875 {
876     DWORD i, j;
877
878     if (info->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
879     {
880         CryptMemFree(info->SignerId.u.IssuerSerialNumber.Issuer.pbData);
881         CryptMemFree(info->SignerId.u.IssuerSerialNumber.SerialNumber.pbData);
882     }
883     else
884         CryptMemFree(info->SignerId.u.KeyId.pbData);
885     CryptMemFree(info->HashAlgorithm.Parameters.pbData);
886     CryptMemFree(info->HashEncryptionAlgorithm.Parameters.pbData);
887     CryptMemFree(info->EncryptedHash.pbData);
888     for (i = 0; i < info->AuthAttrs.cAttr; i++)
889     {
890         for (j = 0; j < info->AuthAttrs.rgAttr[i].cValue; j++)
891             CryptMemFree(info->AuthAttrs.rgAttr[i].rgValue[j].pbData);
892         CryptMemFree(info->AuthAttrs.rgAttr[i].rgValue);
893         CryptMemFree(info->AuthAttrs.rgAttr[i].pszObjId);
894     }
895     CryptMemFree(info->AuthAttrs.rgAttr);
896     for (i = 0; i < info->UnauthAttrs.cAttr; i++)
897     {
898         for (j = 0; j < info->UnauthAttrs.rgAttr[i].cValue; j++)
899             CryptMemFree(info->UnauthAttrs.rgAttr[i].rgValue[j].pbData);
900         CryptMemFree(info->UnauthAttrs.rgAttr[i].rgValue);
901         CryptMemFree(info->UnauthAttrs.rgAttr[i].pszObjId);
902     }
903     CryptMemFree(info->UnauthAttrs.rgAttr);
904 }
905
906 typedef struct _CSignerHandles
907 {
908     HCRYPTHASH contentHash;
909     HCRYPTHASH authAttrHash;
910 } CSignerHandles;
911
912 typedef struct _CSignedMsgData
913 {
914     CRYPT_SIGNED_INFO *info;
915     DWORD              cSignerHandle;
916     CSignerHandles    *signerHandles;
917 } CSignedMsgData;
918
919 /* Constructs the signer handles for the signerIndex'th signer of msg_data.
920  * Assumes signerIndex is a valid idnex, and that msg_data's info has already
921  * been constructed.
922  */
923 static BOOL CSignedMsgData_ConstructSignerHandles(CSignedMsgData *msg_data,
924  DWORD signerIndex, HCRYPTPROV crypt_prov)
925 {
926     ALG_ID algID;
927     BOOL ret;
928
929     algID = CertOIDToAlgId(
930      msg_data->info->rgSignerInfo[signerIndex].HashAlgorithm.pszObjId);
931     ret = CryptCreateHash(crypt_prov, algID, 0, 0,
932      &msg_data->signerHandles->contentHash);
933     if (ret && msg_data->info->rgSignerInfo[signerIndex].AuthAttrs.cAttr > 0)
934         ret = CryptCreateHash(crypt_prov, algID, 0, 0,
935          &msg_data->signerHandles->authAttrHash);
936     return ret;
937 }
938
939 /* Allocates a CSignedMsgData's handles.  Assumes its info has already been
940  * constructed.
941  */
942 static BOOL CSignedMsgData_AllocateHandles(CSignedMsgData *msg_data)
943 {
944     BOOL ret = TRUE;
945
946     if (msg_data->info->cSignerInfo)
947     {
948         msg_data->signerHandles =
949          CryptMemAlloc(msg_data->info->cSignerInfo * sizeof(CSignerHandles));
950         if (msg_data->signerHandles)
951         {
952             msg_data->cSignerHandle = msg_data->info->cSignerInfo;
953             memset(msg_data->signerHandles, 0,
954              msg_data->info->cSignerInfo * sizeof(CSignerHandles));
955         }
956         else
957         {
958             msg_data->cSignerHandle = 0;
959             ret = FALSE;
960         }
961     }
962     else
963     {
964         msg_data->cSignerHandle = 0;
965         msg_data->signerHandles = NULL;
966     }
967     return ret;
968 }
969
970 static void CSignedMsgData_CloseHandles(CSignedMsgData *msg_data)
971 {
972     DWORD i;
973
974     for (i = 0; i < msg_data->cSignerHandle; i++)
975     {
976         if (msg_data->signerHandles[i].contentHash)
977             CryptDestroyHash(msg_data->signerHandles[i].contentHash);
978         if (msg_data->signerHandles[i].authAttrHash)
979             CryptDestroyHash(msg_data->signerHandles[i].authAttrHash);
980     }
981     CryptMemFree(msg_data->signerHandles);
982     msg_data->signerHandles = NULL;
983     msg_data->cSignerHandle = 0;
984 }
985
986 static BOOL CSignedMsgData_UpdateHash(CSignedMsgData *msg_data,
987  const BYTE *pbData, DWORD cbData)
988 {
989     DWORD i;
990     BOOL ret = TRUE;
991
992     for (i = 0; ret && i < msg_data->cSignerHandle; i++)
993         ret = CryptHashData(msg_data->signerHandles[i].contentHash, pbData,
994          cbData, 0);
995     return ret;
996 }
997
998 static BOOL CRYPT_AppendAttribute(CRYPT_ATTRIBUTES *out,
999  const CRYPT_ATTRIBUTE *in)
1000 {
1001     BOOL ret = FALSE;
1002
1003     out->rgAttr = CryptMemRealloc(out->rgAttr,
1004      (out->cAttr + 1) * sizeof(CRYPT_ATTRIBUTE));
1005     if (out->rgAttr)
1006     {
1007         ret = CRYPT_ConstructAttribute(&out->rgAttr[out->cAttr], in);
1008         if (ret)
1009             out->cAttr++;
1010     }
1011     return ret;
1012 }
1013
1014 static BOOL CSignedMsgData_AppendMessageDigestAttribute(
1015  CSignedMsgData *msg_data, DWORD signerIndex)
1016 {
1017     BOOL ret;
1018     DWORD size;
1019     CRYPT_HASH_BLOB hash = { 0, NULL }, encodedHash = { 0, NULL };
1020     char messageDigest[] = szOID_RSA_messageDigest;
1021     CRYPT_ATTRIBUTE messageDigestAttr = { messageDigest, 1, &encodedHash };
1022
1023     size = sizeof(DWORD);
1024     ret = CryptGetHashParam(
1025      msg_data->signerHandles[signerIndex].contentHash, HP_HASHSIZE,
1026      (LPBYTE)&hash.cbData, &size, 0);
1027     if (ret)
1028     {
1029         hash.pbData = CryptMemAlloc(hash.cbData);
1030         ret = CryptGetHashParam(
1031          msg_data->signerHandles[signerIndex].contentHash, HP_HASHVAL,
1032          hash.pbData, &hash.cbData, 0);
1033         if (ret)
1034         {
1035             ret = CRYPT_AsnEncodeOctets(0, NULL, &hash, CRYPT_ENCODE_ALLOC_FLAG,
1036              NULL, (LPBYTE)&encodedHash.pbData, &encodedHash.cbData);
1037             if (ret)
1038             {
1039                 ret = CRYPT_AppendAttribute(
1040                  &msg_data->info->rgSignerInfo[signerIndex].AuthAttrs,
1041                  &messageDigestAttr);
1042                 LocalFree(encodedHash.pbData);
1043             }
1044         }
1045         CryptMemFree(hash.pbData);
1046     }
1047     return ret;
1048 }
1049
1050 typedef enum {
1051     Sign,
1052     Verify
1053 } SignOrVerify;
1054
1055 static BOOL CSignedMsgData_UpdateAuthenticatedAttributes(
1056  CSignedMsgData *msg_data, SignOrVerify flag)
1057 {
1058     DWORD i;
1059     BOOL ret = TRUE;
1060
1061     TRACE("(%p)\n", msg_data);
1062
1063     for (i = 0; ret && i < msg_data->info->cSignerInfo; i++)
1064     {
1065         if (msg_data->info->rgSignerInfo[i].AuthAttrs.cAttr)
1066         {
1067             if (flag == Sign)
1068             {
1069                 BYTE oid_rsa_data_encoded[] = { 0x06,0x09,0x2a,0x86,0x48,0x86,
1070                  0xf7,0x0d,0x01,0x07,0x01 };
1071                 CRYPT_DATA_BLOB content = { sizeof(oid_rsa_data_encoded),
1072                  oid_rsa_data_encoded };
1073                 char contentType[] = szOID_RSA_contentType;
1074                 CRYPT_ATTRIBUTE contentTypeAttr = { contentType, 1, &content };
1075
1076                 /* FIXME: does this depend on inner OID? */
1077                 ret = CRYPT_AppendAttribute(
1078                  &msg_data->info->rgSignerInfo[i].AuthAttrs, &contentTypeAttr);
1079                 if (ret)
1080                     ret = CSignedMsgData_AppendMessageDigestAttribute(msg_data,
1081                      i);
1082             }
1083             if (ret)
1084             {
1085                 LPBYTE encodedAttrs;
1086                 DWORD size;
1087
1088                 ret = CryptEncodeObjectEx(X509_ASN_ENCODING, PKCS_ATTRIBUTES,
1089                  &msg_data->info->rgSignerInfo[i].AuthAttrs,
1090                  CRYPT_ENCODE_ALLOC_FLAG, NULL, &encodedAttrs, &size);
1091                 if (ret)
1092                 {
1093                     ret = CryptHashData(
1094                      msg_data->signerHandles[i].authAttrHash, encodedAttrs,
1095                      size, 0);
1096                     LocalFree(encodedAttrs);
1097                 }
1098             }
1099         }
1100     }
1101     TRACE("returning %d\n", ret);
1102     return ret;
1103 }
1104
1105 static void CRYPT_ReverseBytes(CRYPT_HASH_BLOB *hash)
1106 {
1107     DWORD i;
1108     BYTE tmp;
1109
1110     for (i = 0; i < hash->cbData / 2; i++)
1111     {
1112         tmp = hash->pbData[hash->cbData - i - 1];
1113         hash->pbData[hash->cbData - i - 1] = hash->pbData[i];
1114         hash->pbData[i] = tmp;
1115     }
1116 }
1117
1118 static BOOL CSignedMsgData_Sign(CSignedMsgData *msg_data)
1119 {
1120     DWORD i;
1121     BOOL ret = TRUE;
1122
1123     TRACE("(%p)\n", msg_data);
1124
1125     for (i = 0; ret && i < msg_data->info->cSignerInfo; i++)
1126     {
1127         HCRYPTHASH hash;
1128
1129         if (msg_data->info->rgSignerInfo[i].AuthAttrs.cAttr)
1130             hash = msg_data->signerHandles[i].authAttrHash;
1131         else
1132             hash = msg_data->signerHandles[i].contentHash;
1133         ret = CryptSignHashW(hash, AT_SIGNATURE, NULL, 0, NULL,
1134          &msg_data->info->rgSignerInfo[i].EncryptedHash.cbData);
1135         if (ret)
1136         {
1137             msg_data->info->rgSignerInfo[i].EncryptedHash.pbData =
1138              CryptMemAlloc(
1139              msg_data->info->rgSignerInfo[i].EncryptedHash.cbData);
1140             if (msg_data->info->rgSignerInfo[i].EncryptedHash.pbData)
1141             {
1142                 ret = CryptSignHashW(hash, AT_SIGNATURE, NULL, 0,
1143                  msg_data->info->rgSignerInfo[i].EncryptedHash.pbData,
1144                  &msg_data->info->rgSignerInfo[i].EncryptedHash.cbData);
1145                 if (ret)
1146                     CRYPT_ReverseBytes(
1147                      &msg_data->info->rgSignerInfo[i].EncryptedHash);
1148             }
1149             else
1150                 ret = FALSE;
1151         }
1152     }
1153     return ret;
1154 }
1155
1156 static BOOL CSignedMsgData_Update(CSignedMsgData *msg_data,
1157  const BYTE *pbData, DWORD cbData, BOOL fFinal, SignOrVerify flag)
1158 {
1159     BOOL ret = CSignedMsgData_UpdateHash(msg_data, pbData, cbData);
1160
1161     if (ret && fFinal)
1162     {
1163         ret = CSignedMsgData_UpdateAuthenticatedAttributes(msg_data, flag);
1164         if (ret && flag == Sign)
1165             ret = CSignedMsgData_Sign(msg_data);
1166     }
1167     return ret;
1168 }
1169
1170 typedef struct _CSignedEncodeMsg
1171 {
1172     CryptMsgBase    base;
1173     LPSTR           innerOID;
1174     CRYPT_DATA_BLOB data;
1175     CSignedMsgData  msg_data;
1176 } CSignedEncodeMsg;
1177
1178 static void CSignedEncodeMsg_Close(HCRYPTMSG hCryptMsg)
1179 {
1180     CSignedEncodeMsg *msg = hCryptMsg;
1181     DWORD i;
1182
1183     CryptMemFree(msg->innerOID);
1184     CryptMemFree(msg->data.pbData);
1185     CRYPT_FreeBlobArray(msg->msg_data.info->cCertEncoded,
1186      msg->msg_data.info->rgCertEncoded);
1187     CRYPT_FreeBlobArray(msg->msg_data.info->cCrlEncoded,
1188      msg->msg_data.info->rgCrlEncoded);
1189     for (i = 0; i < msg->msg_data.info->cSignerInfo; i++)
1190         CSignerInfo_Free(&msg->msg_data.info->rgSignerInfo[i]);
1191     CSignedMsgData_CloseHandles(&msg->msg_data);
1192     CryptMemFree(msg->msg_data.info->rgSignerInfo);
1193     CryptMemFree(msg->msg_data.info);
1194 }
1195
1196 static BOOL CSignedEncodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
1197  DWORD dwIndex, void *pvData, DWORD *pcbData)
1198 {
1199     CSignedEncodeMsg *msg = hCryptMsg;
1200     BOOL ret = FALSE;
1201
1202     switch (dwParamType)
1203     {
1204     case CMSG_CONTENT_PARAM:
1205     {
1206         CRYPT_CONTENT_INFO info;
1207
1208         ret = CryptMsgGetParam(hCryptMsg, CMSG_BARE_CONTENT_PARAM, 0, NULL,
1209          &info.Content.cbData);
1210         if (ret)
1211         {
1212             info.Content.pbData = CryptMemAlloc(info.Content.cbData);
1213             if (info.Content.pbData)
1214             {
1215                 ret = CryptMsgGetParam(hCryptMsg, CMSG_BARE_CONTENT_PARAM, 0,
1216                  info.Content.pbData, &info.Content.cbData);
1217                 if (ret)
1218                 {
1219                     char oid_rsa_signed[] = szOID_RSA_signedData;
1220
1221                     info.pszObjId = oid_rsa_signed;
1222                     ret = CryptEncodeObjectEx(X509_ASN_ENCODING,
1223                      PKCS_CONTENT_INFO, &info, 0, NULL, pvData, pcbData);
1224                 }
1225                 CryptMemFree(info.Content.pbData);
1226             }
1227             else
1228                 ret = FALSE;
1229         }
1230         break;
1231     }
1232     case CMSG_BARE_CONTENT_PARAM:
1233     {
1234         CRYPT_SIGNED_INFO info;
1235         BOOL freeContent = FALSE;
1236
1237         info = *msg->msg_data.info;
1238         if (!msg->innerOID || !strcmp(msg->innerOID, szOID_RSA_data))
1239         {
1240             char oid_rsa_data[] = szOID_RSA_data;
1241
1242             /* Quirk:  OID is only encoded messages if an update has happened */
1243             if (msg->base.state != MsgStateInit)
1244                 info.content.pszObjId = oid_rsa_data;
1245             else
1246                 info.content.pszObjId = NULL;
1247             if (msg->data.cbData)
1248             {
1249                 CRYPT_DATA_BLOB blob = { msg->data.cbData, msg->data.pbData };
1250
1251                 ret = CryptEncodeObjectEx(X509_ASN_ENCODING, X509_OCTET_STRING,
1252                  &blob, CRYPT_ENCODE_ALLOC_FLAG, NULL,
1253                  &info.content.Content.pbData, &info.content.Content.cbData);
1254                 freeContent = TRUE;
1255             }
1256             else
1257             {
1258                 info.content.Content.cbData = 0;
1259                 info.content.Content.pbData = NULL;
1260                 ret = TRUE;
1261             }
1262         }
1263         else
1264         {
1265             info.content.pszObjId = msg->innerOID;
1266             info.content.Content.cbData = msg->data.cbData;
1267             info.content.Content.pbData = msg->data.pbData;
1268             ret = TRUE;
1269         }
1270         if (ret)
1271         {
1272             ret = CRYPT_AsnEncodeCMSSignedInfo(&info, pvData, pcbData);
1273             if (freeContent)
1274                 LocalFree(info.content.Content.pbData);
1275         }
1276         break;
1277     }
1278     case CMSG_COMPUTED_HASH_PARAM:
1279         if (dwIndex >= msg->msg_data.cSignerHandle)
1280             SetLastError(CRYPT_E_INVALID_INDEX);
1281         else
1282             ret = CryptGetHashParam(
1283              msg->msg_data.signerHandles[dwIndex].contentHash, HP_HASHVAL,
1284              pvData, pcbData, 0);
1285         break;
1286     case CMSG_ENCODED_SIGNER:
1287         if (dwIndex >= msg->msg_data.info->cSignerInfo)
1288             SetLastError(CRYPT_E_INVALID_INDEX);
1289         else
1290             ret = CryptEncodeObjectEx(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
1291              CMS_SIGNER_INFO, &msg->msg_data.info->rgSignerInfo[dwIndex], 0,
1292              NULL, pvData, pcbData);
1293         break;
1294     case CMSG_VERSION_PARAM:
1295         ret = CRYPT_CopyParam(pvData, pcbData, &msg->msg_data.info->version,
1296          sizeof(msg->msg_data.info->version));
1297         break;
1298     default:
1299         SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1300     }
1301     return ret;
1302 }
1303
1304 static BOOL CSignedEncodeMsg_Update(HCRYPTMSG hCryptMsg, const BYTE *pbData,
1305  DWORD cbData, BOOL fFinal)
1306 {
1307     CSignedEncodeMsg *msg = hCryptMsg;
1308     BOOL ret = FALSE;
1309
1310     if (msg->base.state == MsgStateFinalized)
1311         SetLastError(CRYPT_E_MSG_ERROR);
1312     else if (msg->base.streamed || (msg->base.open_flags & CMSG_DETACHED_FLAG))
1313     {
1314         ret = CSignedMsgData_Update(&msg->msg_data, pbData, cbData, fFinal,
1315          Sign);
1316         if (msg->base.streamed)
1317             FIXME("streamed partial stub\n");
1318         msg->base.state = fFinal ? MsgStateFinalized : MsgStateUpdated;
1319     }
1320     else
1321     {
1322         if (!fFinal)
1323             SetLastError(CRYPT_E_MSG_ERROR);
1324         else
1325         {
1326             if (cbData)
1327             {
1328                 msg->data.pbData = CryptMemAlloc(cbData);
1329                 if (msg->data.pbData)
1330                 {
1331                     memcpy(msg->data.pbData, pbData, cbData);
1332                     msg->data.cbData = cbData;
1333                     ret = TRUE;
1334                 }
1335             }
1336             else
1337                 ret = TRUE;
1338             if (ret)
1339                 ret = CSignedMsgData_Update(&msg->msg_data, pbData, cbData,
1340                  fFinal, Sign);
1341             msg->base.state = MsgStateFinalized;
1342         }
1343     }
1344     return ret;
1345 }
1346
1347 static HCRYPTMSG CSignedEncodeMsg_Open(DWORD dwFlags,
1348  const void *pvMsgEncodeInfo, LPCSTR pszInnerContentObjID,
1349  PCMSG_STREAM_INFO pStreamInfo)
1350 {
1351     const CMSG_SIGNED_ENCODE_INFO_WITH_CMS *info = pvMsgEncodeInfo;
1352     DWORD i;
1353     CSignedEncodeMsg *msg;
1354
1355     if (info->cbSize != sizeof(CMSG_SIGNED_ENCODE_INFO) &&
1356      info->cbSize != sizeof(CMSG_SIGNED_ENCODE_INFO_WITH_CMS))
1357     {
1358         SetLastError(E_INVALIDARG);
1359         return NULL;
1360     }
1361     if (info->cbSize == sizeof(CMSG_SIGNED_ENCODE_INFO_WITH_CMS) &&
1362      info->cAttrCertEncoded)
1363     {
1364         FIXME("CMSG_SIGNED_ENCODE_INFO with CMS fields unsupported\n");
1365         return NULL;
1366     }
1367     for (i = 0; i < info->cSigners; i++)
1368         if (!CRYPT_IsValidSigner(&info->rgSigners[i]))
1369             return NULL;
1370     msg = CryptMemAlloc(sizeof(CSignedEncodeMsg));
1371     if (msg)
1372     {
1373         BOOL ret = TRUE;
1374
1375         CryptMsgBase_Init((CryptMsgBase *)msg, dwFlags, pStreamInfo,
1376          CSignedEncodeMsg_Close, CSignedEncodeMsg_GetParam,
1377          CSignedEncodeMsg_Update, CRYPT_DefaultMsgControl);
1378         if (pszInnerContentObjID)
1379         {
1380             msg->innerOID = CryptMemAlloc(strlen(pszInnerContentObjID) + 1);
1381             if (msg->innerOID)
1382                 strcpy(msg->innerOID, pszInnerContentObjID);
1383             else
1384                 ret = FALSE;
1385         }
1386         else
1387             msg->innerOID = NULL;
1388         msg->data.cbData = 0;
1389         msg->data.pbData = NULL;
1390         if (ret)
1391             msg->msg_data.info = CryptMemAlloc(sizeof(CRYPT_SIGNED_INFO));
1392         else
1393             msg->msg_data.info = NULL;
1394         if (msg->msg_data.info)
1395         {
1396             memset(msg->msg_data.info, 0, sizeof(CRYPT_SIGNED_INFO));
1397             msg->msg_data.info->version = CMSG_SIGNED_DATA_V1;
1398         }
1399         else
1400             ret = FALSE;
1401         if (ret)
1402         {
1403             if (info->cSigners)
1404             {
1405                 msg->msg_data.info->rgSignerInfo =
1406                  CryptMemAlloc(info->cSigners * sizeof(CMSG_CMS_SIGNER_INFO));
1407                 if (msg->msg_data.info->rgSignerInfo)
1408                 {
1409                     msg->msg_data.info->cSignerInfo = info->cSigners;
1410                     memset(msg->msg_data.info->rgSignerInfo, 0,
1411                      msg->msg_data.info->cSignerInfo *
1412                      sizeof(CMSG_CMS_SIGNER_INFO));
1413                     ret = CSignedMsgData_AllocateHandles(&msg->msg_data);
1414                     for (i = 0; ret && i < msg->msg_data.info->cSignerInfo; i++)
1415                     {
1416                         if (info->rgSigners[i].SignerId.dwIdChoice ==
1417                          CERT_ID_KEY_IDENTIFIER)
1418                             msg->msg_data.info->version = CMSG_SIGNED_DATA_V3;
1419                         ret = CSignerInfo_Construct(
1420                          &msg->msg_data.info->rgSignerInfo[i],
1421                          &info->rgSigners[i]);
1422                         if (ret)
1423                         {
1424                             ret = CSignedMsgData_ConstructSignerHandles(
1425                              &msg->msg_data, i, info->rgSigners[i].hCryptProv);
1426                             if (dwFlags & CMSG_CRYPT_RELEASE_CONTEXT_FLAG)
1427                                 CryptReleaseContext(info->rgSigners[i].hCryptProv,
1428                                  0);
1429                         }
1430                     }
1431                 }
1432                 else
1433                     ret = FALSE;
1434             }
1435             else
1436             {
1437                 msg->msg_data.info->cSignerInfo = 0;
1438                 msg->msg_data.signerHandles = NULL;
1439                 msg->msg_data.cSignerHandle = 0;
1440             }
1441         }
1442         if (ret)
1443             ret = CRYPT_ConstructBlobArray(&msg->msg_data.info->cCertEncoded,
1444              &msg->msg_data.info->rgCertEncoded, info->cCertEncoded,
1445              info->rgCertEncoded);
1446         if (ret)
1447             ret = CRYPT_ConstructBlobArray(&msg->msg_data.info->cCrlEncoded,
1448              &msg->msg_data.info->rgCrlEncoded, info->cCrlEncoded,
1449              info->rgCrlEncoded);
1450         if (!ret)
1451         {
1452             CSignedEncodeMsg_Close(msg);
1453             CryptMemFree(msg);
1454             msg = NULL;
1455         }
1456     }
1457     return msg;
1458 }
1459
1460 typedef struct _CMSG_ENVELOPED_ENCODE_INFO_WITH_CMS
1461 {
1462     DWORD                       cbSize;
1463     HCRYPTPROV_LEGACY           hCryptProv;
1464     CRYPT_ALGORITHM_IDENTIFIER  ContentEncryptionAlgorithm;
1465     void                       *pvEncryptionAuxInfo;
1466     DWORD                       cRecipients;
1467     PCERT_INFO                 *rgpRecipientCert;
1468     PCMSG_RECIPIENT_ENCODE_INFO rgCmsRecipients;
1469     DWORD                       cCertEncoded;
1470     PCERT_BLOB                  rgCertEncoded;
1471     DWORD                       cCrlEncoded;
1472     PCRL_BLOB                   rgCrlEncoded;
1473     DWORD                       cAttrCertEncoded;
1474     PCERT_BLOB                  rgAttrCertEncoded;
1475     DWORD                       cUnprotectedAttr;
1476     PCRYPT_ATTRIBUTE            rgUnprotectedAttr;
1477 } CMSG_ENVELOPED_ENCODE_INFO_WITH_CMS, *PCMSG_ENVELOPED_ENCODE_INFO_WITH_CMS;
1478
1479 typedef struct _CEnvelopedEncodeMsg
1480 {
1481     CryptMsgBase                   base;
1482     CRYPT_ALGORITHM_IDENTIFIER     algo;
1483     HCRYPTPROV                     prov;
1484     HCRYPTKEY                      key;
1485     DWORD                          cRecipientInfo;
1486     CMSG_KEY_TRANS_RECIPIENT_INFO *recipientInfo;
1487     CRYPT_DATA_BLOB                data;
1488 } CEnvelopedEncodeMsg;
1489
1490 static BOOL CRYPT_ConstructAlgorithmId(CRYPT_ALGORITHM_IDENTIFIER *out,
1491  const CRYPT_ALGORITHM_IDENTIFIER *in)
1492 {
1493     out->pszObjId = CryptMemAlloc(strlen(in->pszObjId) + 1);
1494     if (out->pszObjId)
1495     {
1496         strcpy(out->pszObjId, in->pszObjId);
1497         return CRYPT_ConstructBlob(&out->Parameters, &in->Parameters);
1498     }
1499     else
1500         return FALSE;
1501 }
1502
1503 static BOOL CRYPT_ConstructBitBlob(CRYPT_BIT_BLOB *out, const CRYPT_BIT_BLOB *in)
1504 {
1505     out->cbData = in->cbData;
1506     out->cUnusedBits = in->cUnusedBits;
1507     if (out->cbData)
1508     {
1509         out->pbData = CryptMemAlloc(out->cbData);
1510         if (out->pbData)
1511             memcpy(out->pbData, in->pbData, out->cbData);
1512         else
1513             return FALSE;
1514     }
1515     else
1516         out->pbData = NULL;
1517     return TRUE;
1518 }
1519
1520 static BOOL CRYPT_GenKey(CMSG_CONTENT_ENCRYPT_INFO *info, ALG_ID algID)
1521 {
1522     static HCRYPTOIDFUNCSET set = NULL;
1523     PFN_CMSG_GEN_CONTENT_ENCRYPT_KEY genKeyFunc = NULL;
1524     HCRYPTOIDFUNCADDR hFunc;
1525     BOOL ret;
1526
1527     if (!set)
1528         set = CryptInitOIDFunctionSet(CMSG_OID_GEN_CONTENT_ENCRYPT_KEY_FUNC, 0);
1529     CryptGetOIDFunctionAddress(set, X509_ASN_ENCODING,
1530      info->ContentEncryptionAlgorithm.pszObjId, 0, (void **)&genKeyFunc, &hFunc);
1531     if (genKeyFunc)
1532     {
1533         ret = genKeyFunc(info, 0, NULL);
1534         CryptFreeOIDFunctionAddress(hFunc, 0);
1535     }
1536     else
1537         ret = CryptGenKey(info->hCryptProv, algID, CRYPT_EXPORTABLE,
1538          &info->hContentEncryptKey);
1539     return ret;
1540 }
1541
1542 static BOOL WINAPI CRYPT_ExportKeyTrans(
1543  PCMSG_CONTENT_ENCRYPT_INFO pContentEncryptInfo,
1544  PCMSG_KEY_TRANS_RECIPIENT_ENCODE_INFO pKeyTransEncodeInfo,
1545  PCMSG_KEY_TRANS_ENCRYPT_INFO pKeyTransEncryptInfo,
1546  DWORD dwFlags, void *pvReserved)
1547 {
1548     CERT_PUBLIC_KEY_INFO keyInfo;
1549     HCRYPTKEY expKey;
1550     BOOL ret;
1551
1552     ret = CRYPT_ConstructAlgorithmId(&keyInfo.Algorithm,
1553         &pKeyTransEncodeInfo->KeyEncryptionAlgorithm);
1554     if (ret)
1555         CRYPT_ConstructBitBlob(&keyInfo.PublicKey,
1556          &pKeyTransEncodeInfo->RecipientPublicKey);
1557
1558     if (ret)
1559         ret = CryptImportPublicKeyInfo(pKeyTransEncodeInfo->hCryptProv,
1560          X509_ASN_ENCODING, &keyInfo, &expKey);
1561     if (ret)
1562     {
1563         DWORD size;
1564
1565         ret = CryptExportKey(pContentEncryptInfo->hContentEncryptKey, expKey,
1566          SIMPLEBLOB, 0, NULL, &size);
1567         if (ret)
1568         {
1569             BYTE *keyBlob;
1570
1571             keyBlob = CryptMemAlloc(size);
1572             if (keyBlob)
1573             {
1574                 ret = CryptExportKey(pContentEncryptInfo->hContentEncryptKey,
1575                  expKey, SIMPLEBLOB, 0, keyBlob, &size);
1576                 if (ret)
1577                 {
1578                     DWORD head = sizeof(BLOBHEADER) + sizeof(ALG_ID);
1579
1580                     pKeyTransEncryptInfo->EncryptedKey.pbData =
1581                      CryptMemAlloc(size - head);
1582                     if (pKeyTransEncryptInfo->EncryptedKey.pbData)
1583                     {
1584                         DWORD i, k = 0;
1585
1586                         pKeyTransEncryptInfo->EncryptedKey.cbData = size - head;
1587                         for (i = size - 1; i >= head; --i, ++k)
1588                             pKeyTransEncryptInfo->EncryptedKey.pbData[k] =
1589                              keyBlob[i];
1590                     }
1591                     else
1592                         ret = FALSE;
1593                 }
1594                 CryptMemFree(keyBlob);
1595             }
1596             else
1597                 ret = FALSE;
1598         }
1599         CryptDestroyKey(expKey);
1600     }
1601
1602     CryptMemFree(keyInfo.PublicKey.pbData);
1603     CryptMemFree(keyInfo.Algorithm.pszObjId);
1604     CryptMemFree(keyInfo.Algorithm.Parameters.pbData);
1605     return ret;
1606 }
1607
1608 static BOOL CRYPT_ExportEncryptedKey(CMSG_CONTENT_ENCRYPT_INFO *info, DWORD i,
1609  CRYPT_DATA_BLOB *key)
1610 {
1611     static HCRYPTOIDFUNCSET set = NULL;
1612     PFN_CMSG_EXPORT_KEY_TRANS exportKeyFunc = NULL;
1613     HCRYPTOIDFUNCADDR hFunc = NULL;
1614     CMSG_KEY_TRANS_RECIPIENT_ENCODE_INFO *encodeInfo =
1615      info->rgCmsRecipients[i].u.pKeyTrans;
1616     CMSG_KEY_TRANS_ENCRYPT_INFO encryptInfo;
1617     BOOL ret;
1618
1619     memset(&encryptInfo, 0, sizeof(encryptInfo));
1620     encryptInfo.cbSize = sizeof(encryptInfo);
1621     encryptInfo.dwRecipientIndex = i;
1622     ret = CRYPT_ConstructAlgorithmId(&encryptInfo.KeyEncryptionAlgorithm,
1623      &encodeInfo->KeyEncryptionAlgorithm);
1624
1625     if (!set)
1626         set = CryptInitOIDFunctionSet(CMSG_OID_EXPORT_KEY_TRANS_FUNC, 0);
1627     CryptGetOIDFunctionAddress(set, X509_ASN_ENCODING,
1628      encryptInfo.KeyEncryptionAlgorithm.pszObjId, 0, (void **)&exportKeyFunc,
1629      &hFunc);
1630     if (!exportKeyFunc)
1631         exportKeyFunc = CRYPT_ExportKeyTrans;
1632     if (ret)
1633     {
1634         ret = exportKeyFunc(info, encodeInfo, &encryptInfo, 0, NULL);
1635         if (ret)
1636         {
1637             key->cbData = encryptInfo.EncryptedKey.cbData;
1638             key->pbData = encryptInfo.EncryptedKey.pbData;
1639         }
1640     }
1641     if (hFunc)
1642         CryptFreeOIDFunctionAddress(hFunc, 0);
1643
1644     CryptMemFree(encryptInfo.KeyEncryptionAlgorithm.pszObjId);
1645     CryptMemFree(encryptInfo.KeyEncryptionAlgorithm.Parameters.pbData);
1646     return ret;
1647 }
1648
1649 static LPVOID WINAPI mem_alloc(size_t size)
1650 {
1651     return HeapAlloc(GetProcessHeap(), 0, size);
1652 }
1653
1654 static VOID WINAPI mem_free(LPVOID pv)
1655 {
1656     HeapFree(GetProcessHeap(), 0, pv);
1657 }
1658
1659
1660 static BOOL CContentEncryptInfo_Construct(CMSG_CONTENT_ENCRYPT_INFO *info,
1661  const CMSG_ENVELOPED_ENCODE_INFO_WITH_CMS *in, HCRYPTPROV prov)
1662 {
1663     BOOL ret;
1664
1665     info->cbSize = sizeof(CMSG_CONTENT_ENCRYPT_INFO);
1666     info->hCryptProv = prov;
1667     ret = CRYPT_ConstructAlgorithmId(&info->ContentEncryptionAlgorithm,
1668      &in->ContentEncryptionAlgorithm);
1669     info->pvEncryptionAuxInfo = in->pvEncryptionAuxInfo;
1670     info->cRecipients = in->cRecipients;
1671     if (ret)
1672     {
1673         info->rgCmsRecipients = CryptMemAlloc(in->cRecipients *
1674          sizeof(CMSG_RECIPIENT_ENCODE_INFO));
1675         if (info->rgCmsRecipients)
1676         {
1677             DWORD i;
1678
1679             for (i = 0; ret && i < in->cRecipients; ++i)
1680             {
1681                 CMSG_KEY_TRANS_RECIPIENT_ENCODE_INFO *encodeInfo;
1682                 CERT_INFO *cert = in->rgpRecipientCert[i];
1683
1684                 info->rgCmsRecipients[i].dwRecipientChoice =
1685                  CMSG_KEY_TRANS_RECIPIENT;
1686                 encodeInfo = CryptMemAlloc(sizeof(*encodeInfo));
1687                 info->rgCmsRecipients[i].u.pKeyTrans = encodeInfo;
1688                 if (encodeInfo)
1689                 {
1690                     encodeInfo->cbSize = sizeof(*encodeInfo);
1691                     ret = CRYPT_ConstructAlgorithmId(
1692                      &encodeInfo->KeyEncryptionAlgorithm,
1693                      &cert->SubjectPublicKeyInfo.Algorithm);
1694                     encodeInfo->pvKeyEncryptionAuxInfo = NULL;
1695                     encodeInfo->hCryptProv = prov;
1696                     if (ret)
1697                         ret = CRYPT_ConstructBitBlob(
1698                          &encodeInfo->RecipientPublicKey,
1699                          &cert->SubjectPublicKeyInfo.PublicKey);
1700                     if (ret)
1701                         ret = CRYPT_ConstructBlob(
1702                          &encodeInfo->RecipientId.u.IssuerSerialNumber.Issuer,
1703                          &cert->Issuer);
1704                     if (ret)
1705                         ret = CRYPT_ConstructBlob(
1706                          &encodeInfo->RecipientId.u.IssuerSerialNumber.SerialNumber,
1707                          &cert->SerialNumber);
1708                 }
1709                 else
1710                     ret = FALSE;
1711             }
1712         }
1713         else
1714             ret = FALSE;
1715     }
1716     info->pfnAlloc = mem_alloc;
1717     info->pfnFree = mem_free;
1718     return ret;
1719 }
1720
1721 static void CContentEncryptInfo_Free(CMSG_CONTENT_ENCRYPT_INFO *info)
1722 {
1723     CryptMemFree(info->ContentEncryptionAlgorithm.pszObjId);
1724     CryptMemFree(info->ContentEncryptionAlgorithm.Parameters.pbData);
1725     if (info->rgCmsRecipients)
1726     {
1727         DWORD i;
1728
1729         for (i = 0; i < info->cRecipients; ++i)
1730         {
1731             CMSG_KEY_TRANS_RECIPIENT_ENCODE_INFO *encodeInfo =
1732              info->rgCmsRecipients[i].u.pKeyTrans;
1733
1734             CryptMemFree(encodeInfo->KeyEncryptionAlgorithm.pszObjId);
1735             CryptMemFree(encodeInfo->KeyEncryptionAlgorithm.Parameters.pbData);
1736             CryptMemFree(encodeInfo->RecipientPublicKey.pbData);
1737             CryptMemFree(
1738              encodeInfo->RecipientId.u.IssuerSerialNumber.Issuer.pbData);
1739             CryptMemFree(
1740              encodeInfo->RecipientId.u.IssuerSerialNumber.SerialNumber.pbData);
1741             CryptMemFree(encodeInfo);
1742         }
1743         CryptMemFree(info->rgCmsRecipients);
1744     }
1745 }
1746
1747 static BOOL CRecipientInfo_Construct(CMSG_KEY_TRANS_RECIPIENT_INFO *info,
1748  const CERT_INFO *cert, CRYPT_DATA_BLOB *key)
1749 {
1750     BOOL ret;
1751
1752     info->dwVersion = CMSG_KEY_TRANS_PKCS_1_5_VERSION;
1753     info->RecipientId.dwIdChoice = CERT_ID_ISSUER_SERIAL_NUMBER;
1754     ret = CRYPT_ConstructBlob(&info->RecipientId.u.IssuerSerialNumber.Issuer,
1755      &cert->Issuer);
1756     if (ret)
1757         ret = CRYPT_ConstructBlob(
1758          &info->RecipientId.u.IssuerSerialNumber.SerialNumber,
1759          &cert->SerialNumber);
1760     if (ret)
1761         ret = CRYPT_ConstructAlgorithmId(&info->KeyEncryptionAlgorithm,
1762          &cert->SubjectPublicKeyInfo.Algorithm);
1763     info->EncryptedKey.cbData = key->cbData;
1764     info->EncryptedKey.pbData = key->pbData;
1765     return ret;
1766 }
1767
1768 static void CRecipientInfo_Free(CMSG_KEY_TRANS_RECIPIENT_INFO *info)
1769 {
1770     CryptMemFree(info->RecipientId.u.IssuerSerialNumber.Issuer.pbData);
1771     CryptMemFree(info->RecipientId.u.IssuerSerialNumber.SerialNumber.pbData);
1772     CryptMemFree(info->KeyEncryptionAlgorithm.pszObjId);
1773     CryptMemFree(info->KeyEncryptionAlgorithm.Parameters.pbData);
1774     CryptMemFree(info->EncryptedKey.pbData);
1775 }
1776
1777 static void CEnvelopedEncodeMsg_Close(HCRYPTMSG hCryptMsg)
1778 {
1779     CEnvelopedEncodeMsg *msg = hCryptMsg;
1780
1781     CryptMemFree(msg->algo.pszObjId);
1782     CryptMemFree(msg->algo.Parameters.pbData);
1783     if (msg->base.open_flags & CMSG_CRYPT_RELEASE_CONTEXT_FLAG)
1784         CryptReleaseContext(msg->prov, 0);
1785     CryptDestroyKey(msg->key);
1786     if (msg->recipientInfo)
1787     {
1788         DWORD i;
1789
1790         for (i = 0; i < msg->cRecipientInfo; ++i)
1791             CRecipientInfo_Free(&msg->recipientInfo[i]);
1792         CryptMemFree(msg->recipientInfo);
1793     }
1794     CryptMemFree(msg->data.pbData);
1795 }
1796
1797 static BOOL CEnvelopedEncodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
1798  DWORD dwIndex, void *pvData, DWORD *pcbData)
1799 {
1800     CEnvelopedEncodeMsg *msg = hCryptMsg;
1801     BOOL ret = FALSE;
1802
1803     switch (dwParamType)
1804     {
1805     case CMSG_BARE_CONTENT_PARAM:
1806         if (msg->base.streamed)
1807             SetLastError(E_INVALIDARG);
1808         else
1809         {
1810             char oid_rsa_data[] = szOID_RSA_data;
1811             CRYPT_ENVELOPED_DATA envelopedData = {
1812              CMSG_ENVELOPED_DATA_PKCS_1_5_VERSION, msg->cRecipientInfo,
1813              msg->recipientInfo, { oid_rsa_data, {
1814                msg->algo.pszObjId,
1815                { msg->algo.Parameters.cbData, msg->algo.Parameters.pbData }
1816               },
1817               { msg->data.cbData, msg->data.pbData }
1818              }
1819             };
1820
1821             ret = CRYPT_AsnEncodePKCSEnvelopedData(&envelopedData, pvData,
1822              pcbData);
1823         }
1824         break;
1825     case CMSG_CONTENT_PARAM:
1826     {
1827         CRYPT_CONTENT_INFO info;
1828
1829         ret = CryptMsgGetParam(hCryptMsg, CMSG_BARE_CONTENT_PARAM, 0, NULL,
1830          &info.Content.cbData);
1831         if (ret)
1832         {
1833             info.Content.pbData = CryptMemAlloc(info.Content.cbData);
1834             if (info.Content.pbData)
1835             {
1836                 ret = CryptMsgGetParam(hCryptMsg, CMSG_BARE_CONTENT_PARAM, 0,
1837                  info.Content.pbData, &info.Content.cbData);
1838                 if (ret)
1839                 {
1840                     char oid_rsa_enveloped[] = szOID_RSA_envelopedData;
1841
1842                     info.pszObjId = oid_rsa_enveloped;
1843                     ret = CryptEncodeObjectEx(X509_ASN_ENCODING,
1844                      PKCS_CONTENT_INFO, &info, 0, NULL, pvData, pcbData);
1845                 }
1846                 CryptMemFree(info.Content.pbData);
1847             }
1848             else
1849                 ret = FALSE;
1850         }
1851         break;
1852     }
1853     default:
1854         SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1855     }
1856     return ret;
1857 }
1858
1859 static BOOL CEnvelopedEncodeMsg_Update(HCRYPTMSG hCryptMsg, const BYTE *pbData,
1860  DWORD cbData, BOOL fFinal)
1861 {
1862     CEnvelopedEncodeMsg *msg = hCryptMsg;
1863     BOOL ret = FALSE;
1864
1865     if (msg->base.state == MsgStateFinalized)
1866         SetLastError(CRYPT_E_MSG_ERROR);
1867     else if (msg->base.streamed)
1868     {
1869         FIXME("streamed stub\n");
1870         msg->base.state = fFinal ? MsgStateFinalized : MsgStateUpdated;
1871         ret = TRUE;
1872     }
1873     else
1874     {
1875         if (!fFinal)
1876         {
1877             if (msg->base.open_flags & CMSG_DETACHED_FLAG)
1878                 SetLastError(E_INVALIDARG);
1879             else
1880                 SetLastError(CRYPT_E_MSG_ERROR);
1881         }
1882         else
1883         {
1884             if (cbData)
1885             {
1886                 DWORD dataLen = cbData;
1887
1888                 msg->data.cbData = cbData;
1889                 msg->data.pbData = CryptMemAlloc(cbData);
1890                 if (msg->data.pbData)
1891                 {
1892                     memcpy(msg->data.pbData, pbData, cbData);
1893                     ret = CryptEncrypt(msg->key, 0, TRUE, 0, msg->data.pbData,
1894                      &dataLen, msg->data.cbData);
1895                     msg->data.cbData = dataLen;
1896                     if (dataLen > cbData)
1897                     {
1898                         msg->data.pbData = CryptMemRealloc(msg->data.pbData,
1899                          dataLen);
1900                         if (msg->data.pbData)
1901                         {
1902                             dataLen = cbData;
1903                             ret = CryptEncrypt(msg->key, 0, TRUE, 0,
1904                              msg->data.pbData, &dataLen, msg->data.cbData);
1905                         }
1906                         else
1907                             ret = FALSE;
1908                     }
1909                     if (!ret)
1910                         CryptMemFree(msg->data.pbData);
1911                 }
1912                 else
1913                     ret = FALSE;
1914                 if (!ret)
1915                 {
1916                     msg->data.cbData = 0;
1917                     msg->data.pbData = NULL;
1918                 }
1919             }
1920             else
1921                 ret = TRUE;
1922             msg->base.state = MsgStateFinalized;
1923         }
1924     }
1925     return ret;
1926 }
1927
1928 static HCRYPTMSG CEnvelopedEncodeMsg_Open(DWORD dwFlags,
1929  const void *pvMsgEncodeInfo, LPCSTR pszInnerContentObjID,
1930  PCMSG_STREAM_INFO pStreamInfo)
1931 {
1932     CEnvelopedEncodeMsg *msg;
1933     const CMSG_ENVELOPED_ENCODE_INFO_WITH_CMS *info = pvMsgEncodeInfo;
1934     HCRYPTPROV prov;
1935     ALG_ID algID;
1936
1937     if (info->cbSize != sizeof(CMSG_ENVELOPED_ENCODE_INFO) &&
1938      info->cbSize != sizeof(CMSG_ENVELOPED_ENCODE_INFO_WITH_CMS))
1939     {
1940         SetLastError(E_INVALIDARG);
1941         return NULL;
1942     }
1943     if (info->cbSize == sizeof(CMSG_ENVELOPED_ENCODE_INFO_WITH_CMS))
1944         FIXME("CMS fields unsupported\n");
1945     if (!(algID = CertOIDToAlgId(info->ContentEncryptionAlgorithm.pszObjId)))
1946     {
1947         SetLastError(CRYPT_E_UNKNOWN_ALGO);
1948         return NULL;
1949     }
1950     if (info->cRecipients && !info->rgpRecipientCert)
1951     {
1952         SetLastError(E_INVALIDARG);
1953         return NULL;
1954     }
1955     if (info->hCryptProv)
1956         prov = info->hCryptProv;
1957     else
1958     {
1959         prov = CRYPT_GetDefaultProvider();
1960         dwFlags &= ~CMSG_CRYPT_RELEASE_CONTEXT_FLAG;
1961     }
1962     msg = CryptMemAlloc(sizeof(CEnvelopedEncodeMsg));
1963     if (msg)
1964     {
1965         CRYPT_DATA_BLOB encryptedKey = { 0, NULL };
1966         CMSG_CONTENT_ENCRYPT_INFO encryptInfo;
1967         BOOL ret;
1968         DWORD i;
1969
1970         CryptMsgBase_Init((CryptMsgBase *)msg, dwFlags, pStreamInfo,
1971          CEnvelopedEncodeMsg_Close, CEnvelopedEncodeMsg_GetParam,
1972          CEnvelopedEncodeMsg_Update, CRYPT_DefaultMsgControl);
1973         ret = CRYPT_ConstructAlgorithmId(&msg->algo,
1974          &info->ContentEncryptionAlgorithm);
1975         msg->prov = prov;
1976         msg->data.cbData = 0;
1977         msg->data.pbData = NULL;
1978         msg->cRecipientInfo = info->cRecipients;
1979         msg->recipientInfo = CryptMemAlloc(info->cRecipients *
1980          sizeof(CMSG_KEY_TRANS_RECIPIENT_INFO));
1981         if (!msg->recipientInfo)
1982             ret = FALSE;
1983         memset(&encryptInfo, 0, sizeof(encryptInfo));
1984         if (ret)
1985         {
1986             ret = CContentEncryptInfo_Construct(&encryptInfo, info, prov);
1987             if (ret)
1988             {
1989                 ret = CRYPT_GenKey(&encryptInfo, algID);
1990                 if (ret)
1991                     msg->key = encryptInfo.hContentEncryptKey;
1992             }
1993         }
1994         for (i = 0; ret && i < msg->cRecipientInfo; ++i)
1995         {
1996             ret = CRYPT_ExportEncryptedKey(&encryptInfo, i, &encryptedKey);
1997             if (ret)
1998                 ret = CRecipientInfo_Construct(&msg->recipientInfo[i],
1999                  info->rgpRecipientCert[i], &encryptedKey);
2000         }
2001         CContentEncryptInfo_Free(&encryptInfo);
2002         if (!ret)
2003         {
2004             CryptMsgClose(msg);
2005             msg = NULL;
2006         }
2007     }
2008     if (!msg && (dwFlags & CMSG_CRYPT_RELEASE_CONTEXT_FLAG))
2009         CryptReleaseContext(prov, 0);
2010     return msg;
2011 }
2012
2013 HCRYPTMSG WINAPI CryptMsgOpenToEncode(DWORD dwMsgEncodingType, DWORD dwFlags,
2014  DWORD dwMsgType, const void *pvMsgEncodeInfo, LPSTR pszInnerContentObjID,
2015  PCMSG_STREAM_INFO pStreamInfo)
2016 {
2017     HCRYPTMSG msg = NULL;
2018
2019     TRACE("(%08x, %08x, %08x, %p, %s, %p)\n", dwMsgEncodingType, dwFlags,
2020      dwMsgType, pvMsgEncodeInfo, debugstr_a(pszInnerContentObjID), pStreamInfo);
2021
2022     if (GET_CMSG_ENCODING_TYPE(dwMsgEncodingType) != PKCS_7_ASN_ENCODING)
2023     {
2024         SetLastError(E_INVALIDARG);
2025         return NULL;
2026     }
2027     switch (dwMsgType)
2028     {
2029     case CMSG_DATA:
2030         msg = CDataEncodeMsg_Open(dwFlags, pvMsgEncodeInfo,
2031          pszInnerContentObjID, pStreamInfo);
2032         break;
2033     case CMSG_HASHED:
2034         msg = CHashEncodeMsg_Open(dwFlags, pvMsgEncodeInfo,
2035          pszInnerContentObjID, pStreamInfo);
2036         break;
2037     case CMSG_SIGNED:
2038         msg = CSignedEncodeMsg_Open(dwFlags, pvMsgEncodeInfo,
2039          pszInnerContentObjID, pStreamInfo);
2040         break;
2041     case CMSG_ENVELOPED:
2042         msg = CEnvelopedEncodeMsg_Open(dwFlags, pvMsgEncodeInfo,
2043          pszInnerContentObjID, pStreamInfo);
2044         break;
2045     case CMSG_SIGNED_AND_ENVELOPED:
2046     case CMSG_ENCRYPTED:
2047         /* defined but invalid, fall through */
2048     default:
2049         SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2050     }
2051     return msg;
2052 }
2053
2054 typedef struct _CEnvelopedDecodeMsg
2055 {
2056     CRYPT_ENVELOPED_DATA *data;
2057     HCRYPTPROV            crypt_prov;
2058     CRYPT_DATA_BLOB       content;
2059     BOOL                  decrypted;
2060 } CEnvelopedDecodeMsg;
2061
2062 typedef struct _CDecodeMsg
2063 {
2064     CryptMsgBase           base;
2065     DWORD                  type;
2066     HCRYPTPROV             crypt_prov;
2067     union {
2068         HCRYPTHASH          hash;
2069         CSignedMsgData      signed_data;
2070         CEnvelopedDecodeMsg enveloped_data;
2071     } u;
2072     CRYPT_DATA_BLOB        msg_data;
2073     CRYPT_DATA_BLOB        detached_data;
2074     PCONTEXT_PROPERTY_LIST properties;
2075 } CDecodeMsg;
2076
2077 static void CDecodeMsg_Close(HCRYPTMSG hCryptMsg)
2078 {
2079     CDecodeMsg *msg = hCryptMsg;
2080
2081     if (msg->base.open_flags & CMSG_CRYPT_RELEASE_CONTEXT_FLAG)
2082         CryptReleaseContext(msg->crypt_prov, 0);
2083     switch (msg->type)
2084     {
2085     case CMSG_HASHED:
2086         if (msg->u.hash)
2087             CryptDestroyHash(msg->u.hash);
2088         break;
2089     case CMSG_ENVELOPED:
2090         if (msg->u.enveloped_data.crypt_prov)
2091             CryptReleaseContext(msg->u.enveloped_data.crypt_prov, 0);
2092         LocalFree(msg->u.enveloped_data.data);
2093         CryptMemFree(msg->u.enveloped_data.content.pbData);
2094         break;
2095     case CMSG_SIGNED:
2096         if (msg->u.signed_data.info)
2097         {
2098             LocalFree(msg->u.signed_data.info);
2099             CSignedMsgData_CloseHandles(&msg->u.signed_data);
2100         }
2101         break;
2102     }
2103     CryptMemFree(msg->msg_data.pbData);
2104     CryptMemFree(msg->detached_data.pbData);
2105     ContextPropertyList_Free(msg->properties);
2106 }
2107
2108 static BOOL CDecodeMsg_CopyData(CRYPT_DATA_BLOB *blob, const BYTE *pbData,
2109  DWORD cbData)
2110 {
2111     BOOL ret = TRUE;
2112
2113     if (cbData)
2114     {
2115         if (blob->cbData)
2116             blob->pbData = CryptMemRealloc(blob->pbData,
2117              blob->cbData + cbData);
2118         else
2119             blob->pbData = CryptMemAlloc(cbData);
2120         if (blob->pbData)
2121         {
2122             memcpy(blob->pbData + blob->cbData, pbData, cbData);
2123             blob->cbData += cbData;
2124         }
2125         else
2126             ret = FALSE;
2127     }
2128     return ret;
2129 }
2130
2131 static BOOL CDecodeMsg_DecodeDataContent(CDecodeMsg *msg, const CRYPT_DER_BLOB *blob)
2132 {
2133     BOOL ret;
2134     CRYPT_DATA_BLOB *data;
2135     DWORD size;
2136
2137     ret = CryptDecodeObjectEx(X509_ASN_ENCODING, X509_OCTET_STRING,
2138      blob->pbData, blob->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, &data, &size);
2139     if (ret)
2140     {
2141         ret = ContextPropertyList_SetProperty(msg->properties,
2142          CMSG_CONTENT_PARAM, data->pbData, data->cbData);
2143         LocalFree(data);
2144     }
2145     return ret;
2146 }
2147
2148 static void CDecodeMsg_SaveAlgorithmID(CDecodeMsg *msg, DWORD param,
2149  const CRYPT_ALGORITHM_IDENTIFIER *id)
2150 {
2151     static const BYTE nullParams[] = { ASN_NULL, 0 };
2152     CRYPT_ALGORITHM_IDENTIFIER *copy;
2153     DWORD len = sizeof(CRYPT_ALGORITHM_IDENTIFIER);
2154
2155     /* Linearize algorithm id */
2156     len += strlen(id->pszObjId) + 1;
2157     len += id->Parameters.cbData;
2158     copy = CryptMemAlloc(len);
2159     if (copy)
2160     {
2161         copy->pszObjId =
2162          (LPSTR)((BYTE *)copy + sizeof(CRYPT_ALGORITHM_IDENTIFIER));
2163         strcpy(copy->pszObjId, id->pszObjId);
2164         copy->Parameters.pbData = (BYTE *)copy->pszObjId + strlen(id->pszObjId)
2165          + 1;
2166         /* Trick:  omit NULL parameters */
2167         if (id->Parameters.cbData == sizeof(nullParams) &&
2168          !memcmp(id->Parameters.pbData, nullParams, sizeof(nullParams)))
2169         {
2170             copy->Parameters.cbData = 0;
2171             len -= sizeof(nullParams);
2172         }
2173         else
2174             copy->Parameters.cbData = id->Parameters.cbData;
2175         if (copy->Parameters.cbData)
2176             memcpy(copy->Parameters.pbData, id->Parameters.pbData,
2177              id->Parameters.cbData);
2178         ContextPropertyList_SetProperty(msg->properties, param, (BYTE *)copy,
2179          len);
2180         CryptMemFree(copy);
2181     }
2182 }
2183
2184 static inline void CRYPT_FixUpAlgorithmID(CRYPT_ALGORITHM_IDENTIFIER *id)
2185 {
2186     id->pszObjId = (LPSTR)((BYTE *)id + sizeof(CRYPT_ALGORITHM_IDENTIFIER));
2187     id->Parameters.pbData = (BYTE *)id->pszObjId + strlen(id->pszObjId) + 1;
2188 }
2189
2190 static BOOL CDecodeMsg_DecodeHashedContent(CDecodeMsg *msg,
2191  const CRYPT_DER_BLOB *blob)
2192 {
2193     BOOL ret;
2194     CRYPT_DIGESTED_DATA *digestedData;
2195     DWORD size;
2196
2197     ret = CRYPT_AsnDecodePKCSDigestedData(blob->pbData, blob->cbData,
2198      CRYPT_DECODE_ALLOC_FLAG, NULL, (CRYPT_DIGESTED_DATA *)&digestedData,
2199      &size);
2200     if (ret)
2201     {
2202         ContextPropertyList_SetProperty(msg->properties, CMSG_VERSION_PARAM,
2203          (const BYTE *)&digestedData->version, sizeof(digestedData->version));
2204         CDecodeMsg_SaveAlgorithmID(msg, CMSG_HASH_ALGORITHM_PARAM,
2205          &digestedData->DigestAlgorithm);
2206         ContextPropertyList_SetProperty(msg->properties,
2207          CMSG_INNER_CONTENT_TYPE_PARAM,
2208          (const BYTE *)digestedData->ContentInfo.pszObjId,
2209          digestedData->ContentInfo.pszObjId ?
2210          strlen(digestedData->ContentInfo.pszObjId) + 1 : 0);
2211         if (!(msg->base.open_flags & CMSG_DETACHED_FLAG))
2212         {
2213             if (digestedData->ContentInfo.Content.cbData)
2214                 CDecodeMsg_DecodeDataContent(msg,
2215                  &digestedData->ContentInfo.Content);
2216             else
2217                 ContextPropertyList_SetProperty(msg->properties,
2218                  CMSG_CONTENT_PARAM, NULL, 0);
2219         }
2220         ContextPropertyList_SetProperty(msg->properties, CMSG_HASH_DATA_PARAM,
2221          digestedData->hash.pbData, digestedData->hash.cbData);
2222         LocalFree(digestedData);
2223     }
2224     return ret;
2225 }
2226
2227 static BOOL CDecodeMsg_DecodeEnvelopedContent(CDecodeMsg *msg,
2228  const CRYPT_DER_BLOB *blob)
2229 {
2230     BOOL ret;
2231     CRYPT_ENVELOPED_DATA *envelopedData;
2232     DWORD size;
2233
2234     ret = CRYPT_AsnDecodePKCSEnvelopedData(blob->pbData, blob->cbData,
2235      CRYPT_DECODE_ALLOC_FLAG, NULL, (CRYPT_ENVELOPED_DATA *)&envelopedData,
2236      &size);
2237     if (ret)
2238         msg->u.enveloped_data.data = envelopedData;
2239     return ret;
2240 }
2241
2242 static BOOL CDecodeMsg_DecodeSignedContent(CDecodeMsg *msg,
2243  const CRYPT_DER_BLOB *blob)
2244 {
2245     BOOL ret;
2246     CRYPT_SIGNED_INFO *signedInfo;
2247     DWORD size;
2248
2249     ret = CRYPT_AsnDecodeCMSSignedInfo(blob->pbData, blob->cbData,
2250      CRYPT_DECODE_ALLOC_FLAG, NULL, (CRYPT_SIGNED_INFO *)&signedInfo,
2251      &size);
2252     if (ret)
2253         msg->u.signed_data.info = signedInfo;
2254     return ret;
2255 }
2256
2257 /* Decodes the content in blob as the type given, and updates the value
2258  * (type, parameters, etc.) of msg based on what blob contains.
2259  * It doesn't just use msg's type, to allow a recursive call from an implicitly
2260  * typed message once the outer content info has been decoded.
2261  */
2262 static BOOL CDecodeMsg_DecodeContent(CDecodeMsg *msg, const CRYPT_DER_BLOB *blob,
2263  DWORD type)
2264 {
2265     BOOL ret;
2266
2267     switch (type)
2268     {
2269     case CMSG_DATA:
2270         if ((ret = CDecodeMsg_DecodeDataContent(msg, blob)))
2271             msg->type = CMSG_DATA;
2272         break;
2273     case CMSG_HASHED:
2274         if ((ret = CDecodeMsg_DecodeHashedContent(msg, blob)))
2275             msg->type = CMSG_HASHED;
2276         break;
2277     case CMSG_ENVELOPED:
2278         if ((ret = CDecodeMsg_DecodeEnvelopedContent(msg, blob)))
2279             msg->type = CMSG_ENVELOPED;
2280         break;
2281     case CMSG_SIGNED:
2282         if ((ret = CDecodeMsg_DecodeSignedContent(msg, blob)))
2283             msg->type = CMSG_SIGNED;
2284         break;
2285     default:
2286     {
2287         CRYPT_CONTENT_INFO *info;
2288         DWORD size;
2289
2290         ret = CryptDecodeObjectEx(X509_ASN_ENCODING, PKCS_CONTENT_INFO,
2291          msg->msg_data.pbData, msg->msg_data.cbData, CRYPT_DECODE_ALLOC_FLAG,
2292          NULL, &info, &size);
2293         if (ret)
2294         {
2295             if (!strcmp(info->pszObjId, szOID_RSA_data))
2296                 ret = CDecodeMsg_DecodeContent(msg, &info->Content, CMSG_DATA);
2297             else if (!strcmp(info->pszObjId, szOID_RSA_digestedData))
2298                 ret = CDecodeMsg_DecodeContent(msg, &info->Content,
2299                  CMSG_HASHED);
2300             else if (!strcmp(info->pszObjId, szOID_RSA_envelopedData))
2301                 ret = CDecodeMsg_DecodeContent(msg, &info->Content,
2302                  CMSG_ENVELOPED);
2303             else if (!strcmp(info->pszObjId, szOID_RSA_signedData))
2304                 ret = CDecodeMsg_DecodeContent(msg, &info->Content,
2305                  CMSG_SIGNED);
2306             else
2307             {
2308                 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2309                 ret = FALSE;
2310             }
2311             LocalFree(info);
2312         }
2313     }
2314     }
2315     return ret;
2316 }
2317
2318 static BOOL CDecodeMsg_FinalizeHashedContent(CDecodeMsg *msg,
2319  CRYPT_DER_BLOB *blob)
2320 {
2321     CRYPT_ALGORITHM_IDENTIFIER *hashAlgoID = NULL;
2322     DWORD size = 0;
2323     ALG_ID algID = 0;
2324     BOOL ret;
2325
2326     CryptMsgGetParam(msg, CMSG_HASH_ALGORITHM_PARAM, 0, NULL, &size);
2327     hashAlgoID = CryptMemAlloc(size);
2328     ret = CryptMsgGetParam(msg, CMSG_HASH_ALGORITHM_PARAM, 0, hashAlgoID,
2329      &size);
2330     if (ret)
2331         algID = CertOIDToAlgId(hashAlgoID->pszObjId);
2332     ret = CryptCreateHash(msg->crypt_prov, algID, 0, 0, &msg->u.hash);
2333     if (ret)
2334     {
2335         CRYPT_DATA_BLOB content;
2336
2337         if (msg->base.open_flags & CMSG_DETACHED_FLAG)
2338         {
2339             /* Unlike for non-detached messages, the data were never stored as
2340              * the content param, but were saved in msg->detached_data instead.
2341              */
2342             content.pbData = msg->detached_data.pbData;
2343             content.cbData = msg->detached_data.cbData;
2344         }
2345         else
2346             ret = ContextPropertyList_FindProperty(msg->properties,
2347              CMSG_CONTENT_PARAM, &content);
2348         if (ret)
2349             ret = CryptHashData(msg->u.hash, content.pbData, content.cbData, 0);
2350     }
2351     CryptMemFree(hashAlgoID);
2352     return ret;
2353 }
2354
2355 static BOOL CDecodeMsg_FinalizeEnvelopedContent(CDecodeMsg *msg,
2356  CRYPT_DER_BLOB *blob)
2357 {
2358     CRYPT_DATA_BLOB *content;
2359
2360     if (msg->base.open_flags & CMSG_DETACHED_FLAG)
2361         content = &msg->detached_data;
2362     else
2363         content =
2364          &msg->u.enveloped_data.data->encryptedContentInfo.encryptedContent;
2365
2366     return CRYPT_ConstructBlob(&msg->u.enveloped_data.content, content);
2367 }
2368
2369 static BOOL CDecodeMsg_FinalizeSignedContent(CDecodeMsg *msg,
2370  CRYPT_DER_BLOB *blob)
2371 {
2372     BOOL ret;
2373     DWORD i, size;
2374
2375     ret = CSignedMsgData_AllocateHandles(&msg->u.signed_data);
2376     for (i = 0; ret && i < msg->u.signed_data.info->cSignerInfo; i++)
2377         ret = CSignedMsgData_ConstructSignerHandles(&msg->u.signed_data, i,
2378          msg->crypt_prov);
2379     if (ret)
2380     {
2381         CRYPT_DATA_BLOB *content;
2382
2383         /* Now that we have all the content, update the hash handles with
2384          * it.  If the message is a detached message, the content is stored
2385          * in msg->detached_data rather than in the signed message's
2386          * content.
2387          */
2388         if (msg->base.open_flags & CMSG_DETACHED_FLAG)
2389             content = &msg->detached_data;
2390         else
2391             content = &msg->u.signed_data.info->content.Content;
2392         if (content->cbData)
2393         {
2394             /* If the message is not detached, have to decode the message's
2395              * content if the type is szOID_RSA_data.
2396              */
2397             if (!(msg->base.open_flags & CMSG_DETACHED_FLAG) &&
2398              !strcmp(msg->u.signed_data.info->content.pszObjId,
2399              szOID_RSA_data))
2400             {
2401                 CRYPT_DATA_BLOB *rsa_blob;
2402
2403                 ret = CryptDecodeObjectEx(X509_ASN_ENCODING,
2404                  X509_OCTET_STRING, content->pbData, content->cbData,
2405                  CRYPT_DECODE_ALLOC_FLAG, NULL, &rsa_blob, &size);
2406                 if (ret)
2407                 {
2408                     ret = CSignedMsgData_Update(&msg->u.signed_data,
2409                      rsa_blob->pbData, rsa_blob->cbData, TRUE, Verify);
2410                     LocalFree(rsa_blob);
2411                 }
2412             }
2413             else
2414                 ret = CSignedMsgData_Update(&msg->u.signed_data,
2415                  content->pbData, content->cbData, TRUE, Verify);
2416         }
2417     }
2418     return ret;
2419 }
2420
2421 static BOOL CDecodeMsg_FinalizeContent(CDecodeMsg *msg, CRYPT_DER_BLOB *blob)
2422 {
2423     BOOL ret = FALSE;
2424
2425     switch (msg->type)
2426     {
2427     case CMSG_HASHED:
2428         ret = CDecodeMsg_FinalizeHashedContent(msg, blob);
2429         break;
2430     case CMSG_ENVELOPED:
2431         ret = CDecodeMsg_FinalizeEnvelopedContent(msg, blob);
2432         break;
2433     case CMSG_SIGNED:
2434         ret = CDecodeMsg_FinalizeSignedContent(msg, blob);
2435         break;
2436     default:
2437         ret = TRUE;
2438     }
2439     return ret;
2440 }
2441
2442 static BOOL CDecodeMsg_Update(HCRYPTMSG hCryptMsg, const BYTE *pbData,
2443  DWORD cbData, BOOL fFinal)
2444 {
2445     CDecodeMsg *msg = hCryptMsg;
2446     BOOL ret = FALSE;
2447
2448     TRACE("(%p, %p, %d, %d)\n", hCryptMsg, pbData, cbData, fFinal);
2449
2450     if (msg->base.state == MsgStateFinalized)
2451         SetLastError(CRYPT_E_MSG_ERROR);
2452     else if (msg->base.streamed)
2453     {
2454         FIXME("(%p, %p, %d, %d): streamed update stub\n", hCryptMsg, pbData,
2455          cbData, fFinal);
2456         switch (msg->base.state)
2457         {
2458         case MsgStateInit:
2459             ret = CDecodeMsg_CopyData(&msg->msg_data, pbData, cbData);
2460             if (fFinal)
2461             {
2462                 if (msg->base.open_flags & CMSG_DETACHED_FLAG)
2463                     msg->base.state = MsgStateDataFinalized;
2464                 else
2465                     msg->base.state = MsgStateFinalized;
2466             }
2467             else
2468                 msg->base.state = MsgStateUpdated;
2469             break;
2470         case MsgStateUpdated:
2471             ret = CDecodeMsg_CopyData(&msg->msg_data, pbData, cbData);
2472             if (fFinal)
2473             {
2474                 if (msg->base.open_flags & CMSG_DETACHED_FLAG)
2475                     msg->base.state = MsgStateDataFinalized;
2476                 else
2477                     msg->base.state = MsgStateFinalized;
2478             }
2479             break;
2480         case MsgStateDataFinalized:
2481             ret = CDecodeMsg_CopyData(&msg->detached_data, pbData, cbData);
2482             if (fFinal)
2483                 msg->base.state = MsgStateFinalized;
2484             break;
2485         default:
2486             SetLastError(CRYPT_E_MSG_ERROR);
2487             break;
2488         }
2489     }
2490     else
2491     {
2492         if (!fFinal)
2493             SetLastError(CRYPT_E_MSG_ERROR);
2494         else
2495         {
2496             switch (msg->base.state)
2497             {
2498             case MsgStateInit:
2499                 ret = CDecodeMsg_CopyData(&msg->msg_data, pbData, cbData);
2500                 if (msg->base.open_flags & CMSG_DETACHED_FLAG)
2501                     msg->base.state = MsgStateDataFinalized;
2502                 else
2503                     msg->base.state = MsgStateFinalized;
2504                 break;
2505             case MsgStateDataFinalized:
2506                 ret = CDecodeMsg_CopyData(&msg->detached_data, pbData, cbData);
2507                 msg->base.state = MsgStateFinalized;
2508                 break;
2509             default:
2510                 SetLastError(CRYPT_E_MSG_ERROR);
2511             }
2512         }
2513     }
2514     if (ret && fFinal &&
2515      ((msg->base.open_flags & CMSG_DETACHED_FLAG && msg->base.state ==
2516      MsgStateDataFinalized) ||
2517      (!(msg->base.open_flags & CMSG_DETACHED_FLAG) && msg->base.state ==
2518      MsgStateFinalized)))
2519         ret = CDecodeMsg_DecodeContent(msg, &msg->msg_data, msg->type);
2520     if (ret && msg->base.state == MsgStateFinalized)
2521         ret = CDecodeMsg_FinalizeContent(msg, &msg->msg_data);
2522     return ret;
2523 }
2524
2525 static BOOL CDecodeHashMsg_GetParam(CDecodeMsg *msg, DWORD dwParamType,
2526  DWORD dwIndex, void *pvData, DWORD *pcbData)
2527 {
2528     BOOL ret = FALSE;
2529
2530     switch (dwParamType)
2531     {
2532     case CMSG_TYPE_PARAM:
2533         ret = CRYPT_CopyParam(pvData, pcbData, &msg->type, sizeof(msg->type));
2534         break;
2535     case CMSG_HASH_ALGORITHM_PARAM:
2536     {
2537         CRYPT_DATA_BLOB blob;
2538
2539         ret = ContextPropertyList_FindProperty(msg->properties, dwParamType,
2540          &blob);
2541         if (ret)
2542         {
2543             ret = CRYPT_CopyParam(pvData, pcbData, blob.pbData, blob.cbData);
2544             if (ret && pvData)
2545                 CRYPT_FixUpAlgorithmID(pvData);
2546         }
2547         else
2548             SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2549         break;
2550     }
2551     case CMSG_COMPUTED_HASH_PARAM:
2552         ret = CryptGetHashParam(msg->u.hash, HP_HASHVAL, pvData, pcbData, 0);
2553         break;
2554     default:
2555     {
2556         CRYPT_DATA_BLOB blob;
2557
2558         ret = ContextPropertyList_FindProperty(msg->properties, dwParamType,
2559          &blob);
2560         if (ret)
2561             ret = CRYPT_CopyParam(pvData, pcbData, blob.pbData, blob.cbData);
2562         else
2563             SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2564     }
2565     }
2566     return ret;
2567 }
2568
2569 /* nextData is an in/out parameter - on input it's the memory location in
2570  * which a copy of in's data should be made, and on output it's the memory
2571  * location immediately after out's copy of in's data.
2572  */
2573 static inline void CRYPT_CopyBlob(CRYPT_DATA_BLOB *out,
2574  const CRYPT_DATA_BLOB *in, LPBYTE *nextData)
2575 {
2576     out->cbData = in->cbData;
2577     if (in->cbData)
2578     {
2579         out->pbData = *nextData;
2580         memcpy(out->pbData, in->pbData, in->cbData);
2581         *nextData += in->cbData;
2582     }
2583 }
2584
2585 static inline void CRYPT_CopyAlgorithmId(CRYPT_ALGORITHM_IDENTIFIER *out,
2586  const CRYPT_ALGORITHM_IDENTIFIER *in, LPBYTE *nextData)
2587 {
2588     if (in->pszObjId)
2589     {
2590         out->pszObjId = (LPSTR)*nextData;
2591         strcpy(out->pszObjId, in->pszObjId);
2592         *nextData += strlen(out->pszObjId) + 1;
2593     }
2594     CRYPT_CopyBlob(&out->Parameters, &in->Parameters, nextData);
2595 }
2596
2597 static inline void CRYPT_CopyAttributes(CRYPT_ATTRIBUTES *out,
2598  const CRYPT_ATTRIBUTES *in, LPBYTE *nextData)
2599 {
2600     out->cAttr = in->cAttr;
2601     if (in->cAttr)
2602     {
2603         DWORD i;
2604
2605         *nextData = POINTER_ALIGN_DWORD_PTR(*nextData);
2606         out->rgAttr = (CRYPT_ATTRIBUTE *)*nextData;
2607         *nextData += in->cAttr * sizeof(CRYPT_ATTRIBUTE);
2608         for (i = 0; i < in->cAttr; i++)
2609         {
2610             if (in->rgAttr[i].pszObjId)
2611             {
2612                 out->rgAttr[i].pszObjId = (LPSTR)*nextData;
2613                 strcpy(out->rgAttr[i].pszObjId, in->rgAttr[i].pszObjId);
2614                 *nextData += strlen(in->rgAttr[i].pszObjId) + 1;
2615             }
2616             if (in->rgAttr[i].cValue)
2617             {
2618                 DWORD j;
2619
2620                 out->rgAttr[i].cValue = in->rgAttr[i].cValue;
2621                 *nextData = POINTER_ALIGN_DWORD_PTR(*nextData);
2622                 out->rgAttr[i].rgValue = (PCRYPT_DATA_BLOB)*nextData;
2623                 *nextData += in->rgAttr[i].cValue * sizeof(CRYPT_DATA_BLOB);
2624                 for (j = 0; j < in->rgAttr[i].cValue; j++)
2625                     CRYPT_CopyBlob(&out->rgAttr[i].rgValue[j],
2626                      &in->rgAttr[i].rgValue[j], nextData);
2627             }
2628         }
2629     }
2630 }
2631
2632 static DWORD CRYPT_SizeOfAttributes(const CRYPT_ATTRIBUTES *attr)
2633 {
2634     DWORD size = attr->cAttr * sizeof(CRYPT_ATTRIBUTE), i, j;
2635
2636     for (i = 0; i < attr->cAttr; i++)
2637     {
2638         if (attr->rgAttr[i].pszObjId)
2639             size += strlen(attr->rgAttr[i].pszObjId) + 1;
2640         /* align pointer */
2641         size = ALIGN_DWORD_PTR(size);
2642         size += attr->rgAttr[i].cValue * sizeof(CRYPT_DATA_BLOB);
2643         for (j = 0; j < attr->rgAttr[i].cValue; j++)
2644             size += attr->rgAttr[i].rgValue[j].cbData;
2645     }
2646     /* align pointer again to be conservative */
2647     size = ALIGN_DWORD_PTR(size);
2648     return size;
2649 }
2650
2651 static DWORD CRYPT_SizeOfKeyIdAsIssuerAndSerial(const CRYPT_DATA_BLOB *keyId)
2652 {
2653     static char oid_key_rdn[] = szOID_KEYID_RDN;
2654     DWORD size = 0;
2655     CERT_RDN_ATTR attr;
2656     CERT_RDN rdn = { 1, &attr };
2657     CERT_NAME_INFO name = { 1, &rdn };
2658
2659     attr.pszObjId = oid_key_rdn;
2660     attr.dwValueType = CERT_RDN_OCTET_STRING;
2661     attr.Value.cbData = keyId->cbData;
2662     attr.Value.pbData = keyId->pbData;
2663     if (CryptEncodeObject(X509_ASN_ENCODING, X509_NAME, &name, NULL, &size))
2664         size++; /* Only include size of special zero serial number on success */
2665     return size;
2666 }
2667
2668 static BOOL CRYPT_CopyKeyIdAsIssuerAndSerial(CERT_NAME_BLOB *issuer,
2669  CRYPT_INTEGER_BLOB *serialNumber, const CRYPT_DATA_BLOB *keyId, DWORD encodedLen,
2670  LPBYTE *nextData)
2671 {
2672     static char oid_key_rdn[] = szOID_KEYID_RDN;
2673     CERT_RDN_ATTR attr;
2674     CERT_RDN rdn = { 1, &attr };
2675     CERT_NAME_INFO name = { 1, &rdn };
2676     BOOL ret;
2677
2678     /* Encode special zero serial number */
2679     serialNumber->cbData = 1;
2680     serialNumber->pbData = *nextData;
2681     **nextData = 0;
2682     (*nextData)++;
2683     /* Encode issuer */
2684     issuer->pbData = *nextData;
2685     attr.pszObjId = oid_key_rdn;
2686     attr.dwValueType = CERT_RDN_OCTET_STRING;
2687     attr.Value.cbData = keyId->cbData;
2688     attr.Value.pbData = keyId->pbData;
2689     ret = CryptEncodeObject(X509_ASN_ENCODING, X509_NAME, &name, *nextData,
2690      &encodedLen);
2691     if (ret)
2692     {
2693         *nextData += encodedLen;
2694         issuer->cbData = encodedLen;
2695     }
2696     return ret;
2697 }
2698
2699 static BOOL CRYPT_CopySignerInfo(void *pvData, DWORD *pcbData,
2700  const CMSG_CMS_SIGNER_INFO *in)
2701 {
2702     DWORD size = sizeof(CMSG_SIGNER_INFO), rdnSize = 0;
2703     BOOL ret;
2704
2705     TRACE("(%p, %d, %p)\n", pvData, pvData ? *pcbData : 0, in);
2706
2707     if (in->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
2708     {
2709         size += in->SignerId.u.IssuerSerialNumber.Issuer.cbData;
2710         size += in->SignerId.u.IssuerSerialNumber.SerialNumber.cbData;
2711     }
2712     else
2713     {
2714         rdnSize = CRYPT_SizeOfKeyIdAsIssuerAndSerial(&in->SignerId.u.KeyId);
2715         size += rdnSize;
2716     }
2717     if (in->HashAlgorithm.pszObjId)
2718         size += strlen(in->HashAlgorithm.pszObjId) + 1;
2719     size += in->HashAlgorithm.Parameters.cbData;
2720     if (in->HashEncryptionAlgorithm.pszObjId)
2721         size += strlen(in->HashEncryptionAlgorithm.pszObjId) + 1;
2722     size += in->HashEncryptionAlgorithm.Parameters.cbData;
2723     size += in->EncryptedHash.cbData;
2724     /* align pointer */
2725     size = ALIGN_DWORD_PTR(size);
2726     size += CRYPT_SizeOfAttributes(&in->AuthAttrs);
2727     size += CRYPT_SizeOfAttributes(&in->UnauthAttrs);
2728     if (!pvData)
2729     {
2730         *pcbData = size;
2731         ret = TRUE;
2732     }
2733     else if (*pcbData < size)
2734     {
2735         *pcbData = size;
2736         SetLastError(ERROR_MORE_DATA);
2737         ret = FALSE;
2738     }
2739     else
2740     {
2741         LPBYTE nextData = (BYTE *)pvData + sizeof(CMSG_SIGNER_INFO);
2742         CMSG_SIGNER_INFO *out = pvData;
2743
2744         ret = TRUE;
2745         out->dwVersion = in->dwVersion;
2746         if (in->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
2747         {
2748             CRYPT_CopyBlob(&out->Issuer,
2749              &in->SignerId.u.IssuerSerialNumber.Issuer, &nextData);
2750             CRYPT_CopyBlob(&out->SerialNumber,
2751              &in->SignerId.u.IssuerSerialNumber.SerialNumber, &nextData);
2752         }
2753         else
2754             ret = CRYPT_CopyKeyIdAsIssuerAndSerial(&out->Issuer, &out->SerialNumber,
2755              &in->SignerId.u.KeyId, rdnSize, &nextData);
2756         if (ret)
2757         {
2758             CRYPT_CopyAlgorithmId(&out->HashAlgorithm, &in->HashAlgorithm,
2759              &nextData);
2760             CRYPT_CopyAlgorithmId(&out->HashEncryptionAlgorithm,
2761              &in->HashEncryptionAlgorithm, &nextData);
2762             CRYPT_CopyBlob(&out->EncryptedHash, &in->EncryptedHash, &nextData);
2763             nextData = POINTER_ALIGN_DWORD_PTR(nextData);
2764             CRYPT_CopyAttributes(&out->AuthAttrs, &in->AuthAttrs, &nextData);
2765             CRYPT_CopyAttributes(&out->UnauthAttrs, &in->UnauthAttrs, &nextData);
2766         }
2767     }
2768     TRACE("returning %d\n", ret);
2769     return ret;
2770 }
2771
2772 static BOOL CRYPT_CopyCMSSignerInfo(void *pvData, DWORD *pcbData,
2773  const CMSG_CMS_SIGNER_INFO *in)
2774 {
2775     DWORD size = sizeof(CMSG_CMS_SIGNER_INFO);
2776     BOOL ret;
2777
2778     TRACE("(%p, %d, %p)\n", pvData, pvData ? *pcbData : 0, in);
2779
2780     if (in->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
2781     {
2782         size += in->SignerId.u.IssuerSerialNumber.Issuer.cbData;
2783         size += in->SignerId.u.IssuerSerialNumber.SerialNumber.cbData;
2784     }
2785     else
2786         size += in->SignerId.u.KeyId.cbData;
2787     if (in->HashAlgorithm.pszObjId)
2788         size += strlen(in->HashAlgorithm.pszObjId) + 1;
2789     size += in->HashAlgorithm.Parameters.cbData;
2790     if (in->HashEncryptionAlgorithm.pszObjId)
2791         size += strlen(in->HashEncryptionAlgorithm.pszObjId) + 1;
2792     size += in->HashEncryptionAlgorithm.Parameters.cbData;
2793     size += in->EncryptedHash.cbData;
2794     /* align pointer */
2795     size = ALIGN_DWORD_PTR(size);
2796     size += CRYPT_SizeOfAttributes(&in->AuthAttrs);
2797     size += CRYPT_SizeOfAttributes(&in->UnauthAttrs);
2798     if (!pvData)
2799     {
2800         *pcbData = size;
2801         ret = TRUE;
2802     }
2803     else if (*pcbData < size)
2804     {
2805         *pcbData = size;
2806         SetLastError(ERROR_MORE_DATA);
2807         ret = FALSE;
2808     }
2809     else
2810     {
2811         LPBYTE nextData = (BYTE *)pvData + sizeof(CMSG_CMS_SIGNER_INFO);
2812         CMSG_CMS_SIGNER_INFO *out = pvData;
2813
2814         out->dwVersion = in->dwVersion;
2815         out->SignerId.dwIdChoice = in->SignerId.dwIdChoice;
2816         if (in->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
2817         {
2818             CRYPT_CopyBlob(&out->SignerId.u.IssuerSerialNumber.Issuer,
2819              &in->SignerId.u.IssuerSerialNumber.Issuer, &nextData);
2820             CRYPT_CopyBlob(&out->SignerId.u.IssuerSerialNumber.SerialNumber,
2821              &in->SignerId.u.IssuerSerialNumber.SerialNumber, &nextData);
2822         }
2823         else
2824             CRYPT_CopyBlob(&out->SignerId.u.KeyId, &in->SignerId.u.KeyId, &nextData);
2825         CRYPT_CopyAlgorithmId(&out->HashAlgorithm, &in->HashAlgorithm,
2826          &nextData);
2827         CRYPT_CopyAlgorithmId(&out->HashEncryptionAlgorithm,
2828          &in->HashEncryptionAlgorithm, &nextData);
2829         CRYPT_CopyBlob(&out->EncryptedHash, &in->EncryptedHash, &nextData);
2830         nextData = POINTER_ALIGN_DWORD_PTR(nextData);
2831         CRYPT_CopyAttributes(&out->AuthAttrs, &in->AuthAttrs, &nextData);
2832         CRYPT_CopyAttributes(&out->UnauthAttrs, &in->UnauthAttrs, &nextData);
2833         ret = TRUE;
2834     }
2835     TRACE("returning %d\n", ret);
2836     return ret;
2837 }
2838
2839 static BOOL CRYPT_CopySignerCertInfo(void *pvData, DWORD *pcbData,
2840  const CMSG_CMS_SIGNER_INFO *in)
2841 {
2842     DWORD size = sizeof(CERT_INFO), rdnSize = 0;
2843     BOOL ret;
2844
2845     TRACE("(%p, %d, %p)\n", pvData, pvData ? *pcbData : 0, in);
2846
2847     if (in->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
2848     {
2849         size += in->SignerId.u.IssuerSerialNumber.Issuer.cbData;
2850         size += in->SignerId.u.IssuerSerialNumber.SerialNumber.cbData;
2851     }
2852     else
2853     {
2854         rdnSize = CRYPT_SizeOfKeyIdAsIssuerAndSerial(&in->SignerId.u.KeyId);
2855         size += rdnSize;
2856     }
2857     if (!pvData)
2858     {
2859         *pcbData = size;
2860         ret = TRUE;
2861     }
2862     else if (*pcbData < size)
2863     {
2864         *pcbData = size;
2865         SetLastError(ERROR_MORE_DATA);
2866         ret = FALSE;
2867     }
2868     else
2869     {
2870         LPBYTE nextData = (BYTE *)pvData + sizeof(CERT_INFO);
2871         CERT_INFO *out = pvData;
2872
2873         memset(out, 0, sizeof(CERT_INFO));
2874         if (in->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
2875         {
2876             CRYPT_CopyBlob(&out->Issuer,
2877              &in->SignerId.u.IssuerSerialNumber.Issuer, &nextData);
2878             CRYPT_CopyBlob(&out->SerialNumber,
2879              &in->SignerId.u.IssuerSerialNumber.SerialNumber, &nextData);
2880             ret = TRUE;
2881         }
2882         else
2883             ret = CRYPT_CopyKeyIdAsIssuerAndSerial(&out->Issuer, &out->SerialNumber,
2884              &in->SignerId.u.KeyId, rdnSize, &nextData);
2885     }
2886     TRACE("returning %d\n", ret);
2887     return ret;
2888 }
2889
2890 static BOOL CRYPT_CopyRecipientInfo(void *pvData, DWORD *pcbData,
2891  const CERT_ISSUER_SERIAL_NUMBER *in)
2892 {
2893     DWORD size = sizeof(CERT_INFO);
2894     BOOL ret;
2895
2896     TRACE("(%p, %d, %p)\n", pvData, pvData ? *pcbData : 0, in);
2897
2898     size += in->SerialNumber.cbData;
2899     size += in->Issuer.cbData;
2900     if (!pvData)
2901     {
2902         *pcbData = size;
2903         ret = TRUE;
2904     }
2905     else if (*pcbData < size)
2906     {
2907         *pcbData = size;
2908         SetLastError(ERROR_MORE_DATA);
2909         ret = FALSE;
2910     }
2911     else
2912     {
2913         LPBYTE nextData = (BYTE *)pvData + sizeof(CERT_INFO);
2914         CERT_INFO *out = pvData;
2915
2916         CRYPT_CopyBlob(&out->SerialNumber, &in->SerialNumber, &nextData);
2917         CRYPT_CopyBlob(&out->Issuer, &in->Issuer, &nextData);
2918         ret = TRUE;
2919     }
2920     TRACE("returning %d\n", ret);
2921     return ret;
2922 }
2923
2924 static BOOL CDecodeEnvelopedMsg_GetParam(CDecodeMsg *msg, DWORD dwParamType,
2925  DWORD dwIndex, void *pvData, DWORD *pcbData)
2926 {
2927     BOOL ret = FALSE;
2928
2929     switch (dwParamType)
2930     {
2931     case CMSG_TYPE_PARAM:
2932         ret = CRYPT_CopyParam(pvData, pcbData, &msg->type, sizeof(msg->type));
2933         break;
2934     case CMSG_CONTENT_PARAM:
2935         if (msg->u.enveloped_data.data)
2936             ret = CRYPT_CopyParam(pvData, pcbData,
2937              msg->u.enveloped_data.content.pbData,
2938              msg->u.enveloped_data.content.cbData);
2939         else
2940             SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2941         break;
2942     case CMSG_RECIPIENT_COUNT_PARAM:
2943         if (msg->u.enveloped_data.data)
2944             ret = CRYPT_CopyParam(pvData, pcbData,
2945              &msg->u.enveloped_data.data->cRecipientInfo, sizeof(DWORD));
2946         else
2947             SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2948         break;
2949     case CMSG_RECIPIENT_INFO_PARAM:
2950         if (msg->u.enveloped_data.data)
2951         {
2952             if (dwIndex < msg->u.enveloped_data.data->cRecipientInfo)
2953             {
2954                 PCMSG_KEY_TRANS_RECIPIENT_INFO recipientInfo =
2955                  &msg->u.enveloped_data.data->rgRecipientInfo[dwIndex];
2956
2957                 ret = CRYPT_CopyRecipientInfo(pvData, pcbData,
2958                  &recipientInfo->RecipientId.u.IssuerSerialNumber);
2959             }
2960             else
2961                 SetLastError(CRYPT_E_INVALID_INDEX);
2962         }
2963         else
2964             SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2965         break;
2966     default:
2967         FIXME("unimplemented for %d\n", dwParamType);
2968         SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2969     }
2970     return ret;
2971 }
2972
2973 static BOOL CDecodeSignedMsg_GetParam(CDecodeMsg *msg, DWORD dwParamType,
2974  DWORD dwIndex, void *pvData, DWORD *pcbData)
2975 {
2976     BOOL ret = FALSE;
2977
2978     switch (dwParamType)
2979     {
2980     case CMSG_TYPE_PARAM:
2981         ret = CRYPT_CopyParam(pvData, pcbData, &msg->type, sizeof(msg->type));
2982         break;
2983     case CMSG_CONTENT_PARAM:
2984         if (msg->u.signed_data.info)
2985         {
2986             if (!strcmp(msg->u.signed_data.info->content.pszObjId,
2987              szOID_RSA_data))
2988             {
2989                 CRYPT_DATA_BLOB *blob;
2990                 DWORD size;
2991
2992                 ret = CryptDecodeObjectEx(X509_ASN_ENCODING, X509_OCTET_STRING,
2993                  msg->u.signed_data.info->content.Content.pbData,
2994                  msg->u.signed_data.info->content.Content.cbData,
2995                  CRYPT_DECODE_ALLOC_FLAG, NULL, &blob, &size);
2996                 if (ret)
2997                 {
2998                     ret = CRYPT_CopyParam(pvData, pcbData, blob->pbData,
2999                      blob->cbData);
3000                     LocalFree(blob);
3001                 }
3002             }
3003             else
3004                 ret = CRYPT_CopyParam(pvData, pcbData,
3005                  msg->u.signed_data.info->content.Content.pbData,
3006                  msg->u.signed_data.info->content.Content.cbData);
3007         }
3008         else
3009             SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3010         break;
3011     case CMSG_INNER_CONTENT_TYPE_PARAM:
3012         if (msg->u.signed_data.info)
3013             ret = CRYPT_CopyParam(pvData, pcbData,
3014              msg->u.signed_data.info->content.pszObjId,
3015              strlen(msg->u.signed_data.info->content.pszObjId) + 1);
3016         else
3017             SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3018         break;
3019     case CMSG_SIGNER_COUNT_PARAM:
3020         if (msg->u.signed_data.info)
3021             ret = CRYPT_CopyParam(pvData, pcbData,
3022              &msg->u.signed_data.info->cSignerInfo, sizeof(DWORD));
3023         else
3024             SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3025         break;
3026     case CMSG_SIGNER_INFO_PARAM:
3027         if (msg->u.signed_data.info)
3028         {
3029             if (dwIndex >= msg->u.signed_data.info->cSignerInfo)
3030                 SetLastError(CRYPT_E_INVALID_INDEX);
3031             else
3032                 ret = CRYPT_CopySignerInfo(pvData, pcbData,
3033                  &msg->u.signed_data.info->rgSignerInfo[dwIndex]);
3034         }
3035         else
3036             SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3037         break;
3038     case CMSG_SIGNER_CERT_INFO_PARAM:
3039         if (msg->u.signed_data.info)
3040         {
3041             if (dwIndex >= msg->u.signed_data.info->cSignerInfo)
3042                 SetLastError(CRYPT_E_INVALID_INDEX);
3043             else
3044                 ret = CRYPT_CopySignerCertInfo(pvData, pcbData,
3045                  &msg->u.signed_data.info->rgSignerInfo[dwIndex]);
3046         }
3047         else
3048             SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3049         break;
3050     case CMSG_CERT_COUNT_PARAM:
3051         if (msg->u.signed_data.info)
3052             ret = CRYPT_CopyParam(pvData, pcbData,
3053              &msg->u.signed_data.info->cCertEncoded, sizeof(DWORD));
3054         else
3055             SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3056         break;
3057     case CMSG_CERT_PARAM:
3058         if (msg->u.signed_data.info)
3059         {
3060             if (dwIndex >= msg->u.signed_data.info->cCertEncoded)
3061                 SetLastError(CRYPT_E_INVALID_INDEX);
3062             else
3063                 ret = CRYPT_CopyParam(pvData, pcbData,
3064                  msg->u.signed_data.info->rgCertEncoded[dwIndex].pbData,
3065                  msg->u.signed_data.info->rgCertEncoded[dwIndex].cbData);
3066         }
3067         else
3068             SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3069         break;
3070     case CMSG_CRL_COUNT_PARAM:
3071         if (msg->u.signed_data.info)
3072             ret = CRYPT_CopyParam(pvData, pcbData,
3073              &msg->u.signed_data.info->cCrlEncoded, sizeof(DWORD));
3074         else
3075             SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3076         break;
3077     case CMSG_CRL_PARAM:
3078         if (msg->u.signed_data.info)
3079         {
3080             if (dwIndex >= msg->u.signed_data.info->cCrlEncoded)
3081                 SetLastError(CRYPT_E_INVALID_INDEX);
3082             else
3083                 ret = CRYPT_CopyParam(pvData, pcbData,
3084                  msg->u.signed_data.info->rgCrlEncoded[dwIndex].pbData,
3085                  msg->u.signed_data.info->rgCrlEncoded[dwIndex].cbData);
3086         }
3087         else
3088             SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3089         break;
3090     case CMSG_COMPUTED_HASH_PARAM:
3091         if (msg->u.signed_data.info)
3092         {
3093             if (dwIndex >= msg->u.signed_data.cSignerHandle)
3094                 SetLastError(CRYPT_E_INVALID_INDEX);
3095             else
3096                 ret = CryptGetHashParam(
3097                  msg->u.signed_data.signerHandles[dwIndex].contentHash,
3098                  HP_HASHVAL, pvData, pcbData, 0);
3099         }
3100         else
3101             SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3102         break;
3103     case CMSG_ENCODED_SIGNER:
3104         if (msg->u.signed_data.info)
3105         {
3106             if (dwIndex >= msg->u.signed_data.info->cSignerInfo)
3107                 SetLastError(CRYPT_E_INVALID_INDEX);
3108             else
3109                 ret = CryptEncodeObjectEx(
3110                  X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, CMS_SIGNER_INFO,
3111                  &msg->u.signed_data.info->rgSignerInfo[dwIndex], 0, NULL,
3112                  pvData, pcbData);
3113         }
3114         else
3115             SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3116         break;
3117     case CMSG_ATTR_CERT_COUNT_PARAM:
3118         if (msg->u.signed_data.info)
3119         {
3120             DWORD attrCertCount = 0;
3121
3122             ret = CRYPT_CopyParam(pvData, pcbData,
3123              &attrCertCount, sizeof(DWORD));
3124         }
3125         else
3126             SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3127         break;
3128     case CMSG_ATTR_CERT_PARAM:
3129         if (msg->u.signed_data.info)
3130             SetLastError(CRYPT_E_INVALID_INDEX);
3131         else
3132             SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3133         break;
3134     case CMSG_CMS_SIGNER_INFO_PARAM:
3135         if (msg->u.signed_data.info)
3136         {
3137             if (dwIndex >= msg->u.signed_data.info->cSignerInfo)
3138                 SetLastError(CRYPT_E_INVALID_INDEX);
3139             else
3140                 ret = CRYPT_CopyCMSSignerInfo(pvData, pcbData,
3141                  &msg->u.signed_data.info->rgSignerInfo[dwIndex]);
3142         }
3143         else
3144             SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3145         break;
3146     default:
3147         FIXME("unimplemented for %d\n", dwParamType);
3148         SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3149     }
3150     return ret;
3151 }
3152
3153 static BOOL CDecodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
3154  DWORD dwIndex, void *pvData, DWORD *pcbData)
3155 {
3156     CDecodeMsg *msg = hCryptMsg;
3157     BOOL ret = FALSE;
3158
3159     switch (msg->type)
3160     {
3161     case CMSG_HASHED:
3162         ret = CDecodeHashMsg_GetParam(msg, dwParamType, dwIndex, pvData,
3163          pcbData);
3164         break;
3165     case CMSG_ENVELOPED:
3166         ret = CDecodeEnvelopedMsg_GetParam(msg, dwParamType, dwIndex, pvData,
3167          pcbData);
3168         break;
3169     case CMSG_SIGNED:
3170         ret = CDecodeSignedMsg_GetParam(msg, dwParamType, dwIndex, pvData,
3171          pcbData);
3172         break;
3173     default:
3174         switch (dwParamType)
3175         {
3176         case CMSG_TYPE_PARAM:
3177             ret = CRYPT_CopyParam(pvData, pcbData, &msg->type,
3178              sizeof(msg->type));
3179             break;
3180         default:
3181         {
3182             CRYPT_DATA_BLOB blob;
3183
3184             ret = ContextPropertyList_FindProperty(msg->properties, dwParamType,
3185              &blob);
3186             if (ret)
3187                 ret = CRYPT_CopyParam(pvData, pcbData, blob.pbData,
3188                  blob.cbData);
3189             else
3190                 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3191         }
3192         }
3193     }
3194     return ret;
3195 }
3196
3197 static BOOL CDecodeHashMsg_VerifyHash(CDecodeMsg *msg)
3198 {
3199     BOOL ret;
3200     CRYPT_DATA_BLOB hashBlob;
3201
3202     ret = ContextPropertyList_FindProperty(msg->properties,
3203      CMSG_HASH_DATA_PARAM, &hashBlob);
3204     if (ret)
3205     {
3206         DWORD computedHashSize = 0;
3207
3208         ret = CDecodeHashMsg_GetParam(msg, CMSG_COMPUTED_HASH_PARAM, 0, NULL,
3209          &computedHashSize);
3210         if (hashBlob.cbData == computedHashSize)
3211         {
3212             LPBYTE computedHash = CryptMemAlloc(computedHashSize);
3213
3214             if (computedHash)
3215             {
3216                 ret = CDecodeHashMsg_GetParam(msg, CMSG_COMPUTED_HASH_PARAM, 0,
3217                  computedHash, &computedHashSize);
3218                 if (ret)
3219                 {
3220                     if (memcmp(hashBlob.pbData, computedHash, hashBlob.cbData))
3221                     {
3222                         SetLastError(CRYPT_E_HASH_VALUE);
3223                         ret = FALSE;
3224                     }
3225                 }
3226                 CryptMemFree(computedHash);
3227             }
3228             else
3229             {
3230                 SetLastError(ERROR_OUTOFMEMORY);
3231                 ret = FALSE;
3232             }
3233         }
3234         else
3235         {
3236             SetLastError(CRYPT_E_HASH_VALUE);
3237             ret = FALSE;
3238         }
3239     }
3240     return ret;
3241 }
3242
3243 static BOOL CDecodeSignedMsg_VerifySignatureWithKey(CDecodeMsg *msg,
3244  HCRYPTPROV prov, DWORD signerIndex, PCERT_PUBLIC_KEY_INFO keyInfo)
3245 {
3246     HCRYPTKEY key;
3247     BOOL ret;
3248
3249     if (!prov)
3250         prov = msg->crypt_prov;
3251     ret = CryptImportPublicKeyInfo(prov, X509_ASN_ENCODING, keyInfo, &key);
3252     if (ret)
3253     {
3254         HCRYPTHASH hash;
3255         CRYPT_HASH_BLOB reversedHash;
3256
3257         if (msg->u.signed_data.info->rgSignerInfo[signerIndex].AuthAttrs.cAttr)
3258             hash = msg->u.signed_data.signerHandles[signerIndex].authAttrHash;
3259         else
3260             hash = msg->u.signed_data.signerHandles[signerIndex].contentHash;
3261         ret = CRYPT_ConstructBlob(&reversedHash,
3262          &msg->u.signed_data.info->rgSignerInfo[signerIndex].EncryptedHash);
3263         if (ret)
3264         {
3265             CRYPT_ReverseBytes(&reversedHash);
3266             ret = CryptVerifySignatureW(hash, reversedHash.pbData,
3267              reversedHash.cbData, key, NULL, 0);
3268             CryptMemFree(reversedHash.pbData);
3269         }
3270         CryptDestroyKey(key);
3271     }
3272     return ret;
3273 }
3274
3275 static BOOL CDecodeSignedMsg_VerifySignature(CDecodeMsg *msg, PCERT_INFO info)
3276 {
3277     BOOL ret = FALSE;
3278     DWORD i;
3279
3280     if (!msg->u.signed_data.signerHandles)
3281     {
3282         SetLastError(NTE_BAD_SIGNATURE);
3283         return FALSE;
3284     }
3285     for (i = 0; !ret && i < msg->u.signed_data.info->cSignerInfo; i++)
3286     {
3287         PCMSG_CMS_SIGNER_INFO signerInfo =
3288          &msg->u.signed_data.info->rgSignerInfo[i];
3289
3290         if (signerInfo->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
3291         {
3292             ret = CertCompareCertificateName(X509_ASN_ENCODING,
3293              &signerInfo->SignerId.u.IssuerSerialNumber.Issuer,
3294              &info->Issuer);
3295             if (ret)
3296             {
3297                 ret = CertCompareIntegerBlob(
3298                  &signerInfo->SignerId.u.IssuerSerialNumber.SerialNumber,
3299                  &info->SerialNumber);
3300                 if (ret)
3301                     break;
3302             }
3303         }
3304         else
3305         {
3306             FIXME("signer %d: unimplemented for key id\n", i);
3307         }
3308     }
3309     if (ret)
3310         ret = CDecodeSignedMsg_VerifySignatureWithKey(msg, 0, i,
3311          &info->SubjectPublicKeyInfo);
3312     else
3313         SetLastError(CRYPT_E_SIGNER_NOT_FOUND);
3314
3315     return ret;
3316 }
3317
3318 static BOOL CDecodeSignedMsg_VerifySignatureEx(CDecodeMsg *msg,
3319  PCMSG_CTRL_VERIFY_SIGNATURE_EX_PARA para)
3320 {
3321     BOOL ret = FALSE;
3322
3323     if (para->cbSize != sizeof(CMSG_CTRL_VERIFY_SIGNATURE_EX_PARA))
3324         SetLastError(ERROR_INVALID_PARAMETER);
3325     else if (para->dwSignerIndex >= msg->u.signed_data.info->cSignerInfo)
3326         SetLastError(CRYPT_E_SIGNER_NOT_FOUND);
3327     else if (!msg->u.signed_data.signerHandles)
3328         SetLastError(NTE_BAD_SIGNATURE);
3329     else
3330     {
3331         switch (para->dwSignerType)
3332         {
3333         case CMSG_VERIFY_SIGNER_PUBKEY:
3334             ret = CDecodeSignedMsg_VerifySignatureWithKey(msg,
3335              para->hCryptProv, para->dwSignerIndex, para->pvSigner);
3336             break;
3337         case CMSG_VERIFY_SIGNER_CERT:
3338         {
3339             PCCERT_CONTEXT cert = para->pvSigner;
3340
3341             ret = CDecodeSignedMsg_VerifySignatureWithKey(msg, para->hCryptProv,
3342              para->dwSignerIndex, &cert->pCertInfo->SubjectPublicKeyInfo);
3343             break;
3344         }
3345         default:
3346             FIXME("unimplemented for signer type %d\n", para->dwSignerType);
3347             SetLastError(CRYPT_E_SIGNER_NOT_FOUND);
3348         }
3349     }
3350     return ret;
3351 }
3352
3353 static BOOL WINAPI CRYPT_ImportKeyTrans(
3354  PCRYPT_ALGORITHM_IDENTIFIER pContentEncryptionAlgorithm,
3355  PCMSG_CTRL_KEY_TRANS_DECRYPT_PARA pKeyTransDecryptPara, DWORD dwFlags,
3356  void *pvReserved, HCRYPTKEY *phContentEncryptKey)
3357 {
3358     BOOL ret;
3359     HCRYPTKEY key;
3360
3361     ret = CryptGetUserKey(pKeyTransDecryptPara->hCryptProv,
3362      pKeyTransDecryptPara->dwKeySpec ? pKeyTransDecryptPara->dwKeySpec :
3363      AT_KEYEXCHANGE, &key);
3364     if (ret)
3365     {
3366         CMSG_KEY_TRANS_RECIPIENT_INFO *info =
3367          &pKeyTransDecryptPara->pKeyTrans[pKeyTransDecryptPara->dwRecipientIndex];
3368         CRYPT_DATA_BLOB *encryptedKey = &info->EncryptedKey;
3369         DWORD size = encryptedKey->cbData + sizeof(BLOBHEADER) + sizeof(ALG_ID);
3370         BYTE *keyBlob = CryptMemAlloc(size);
3371
3372         if (keyBlob)
3373         {
3374             DWORD i, k = size - 1;
3375             BLOBHEADER *blobHeader = (BLOBHEADER *)keyBlob;
3376             ALG_ID *algID = (ALG_ID *)(keyBlob + sizeof(BLOBHEADER));
3377
3378             blobHeader->bType = SIMPLEBLOB;
3379             blobHeader->bVersion = CUR_BLOB_VERSION;
3380             blobHeader->reserved = 0;
3381             blobHeader->aiKeyAlg = CertOIDToAlgId(
3382              pContentEncryptionAlgorithm->pszObjId);
3383             *algID = CertOIDToAlgId(info->KeyEncryptionAlgorithm.pszObjId);
3384             for (i = 0; i < encryptedKey->cbData; ++i, --k)
3385                 keyBlob[k] = encryptedKey->pbData[i];
3386
3387             ret = CryptImportKey(pKeyTransDecryptPara->hCryptProv, keyBlob,
3388              size, key, 0, phContentEncryptKey);
3389             CryptMemFree(keyBlob);
3390         }
3391         else
3392             ret = FALSE;
3393         CryptDestroyKey(key);
3394     }
3395     return ret;
3396 }
3397
3398 static BOOL CRYPT_ImportEncryptedKey(PCRYPT_ALGORITHM_IDENTIFIER contEncrAlg,
3399  PCMSG_CTRL_DECRYPT_PARA para, PCMSG_KEY_TRANS_RECIPIENT_INFO info,
3400  HCRYPTKEY *key)
3401 {
3402     static HCRYPTOIDFUNCSET set = NULL;
3403     PFN_CMSG_IMPORT_KEY_TRANS importKeyFunc = NULL;
3404     HCRYPTOIDFUNCADDR hFunc = NULL;
3405     CMSG_CTRL_KEY_TRANS_DECRYPT_PARA decryptPara;
3406     BOOL ret;
3407
3408     memset(&decryptPara, 0, sizeof(decryptPara));
3409     decryptPara.cbSize = sizeof(decryptPara);
3410     decryptPara.hCryptProv = para->hCryptProv;
3411     decryptPara.dwKeySpec = para->dwKeySpec;
3412     decryptPara.pKeyTrans = info;
3413     decryptPara.dwRecipientIndex = para->dwRecipientIndex;
3414
3415     if (!set)
3416         set = CryptInitOIDFunctionSet(CMSG_OID_IMPORT_KEY_TRANS_FUNC, 0);
3417     CryptGetOIDFunctionAddress(set, X509_ASN_ENCODING, contEncrAlg->pszObjId, 0,
3418      (void **)&importKeyFunc, &hFunc);
3419     if (!importKeyFunc)
3420         importKeyFunc = CRYPT_ImportKeyTrans;
3421     ret = importKeyFunc(contEncrAlg, &decryptPara, 0, NULL, key);
3422     if (hFunc)
3423         CryptFreeOIDFunctionAddress(hFunc, 0);
3424     return ret;
3425 }
3426
3427 static BOOL CDecodeEnvelopedMsg_CrtlDecrypt(CDecodeMsg *msg,
3428  PCMSG_CTRL_DECRYPT_PARA para)
3429 {
3430     BOOL ret = FALSE;
3431     CEnvelopedDecodeMsg *enveloped_data = &msg->u.enveloped_data;
3432     CRYPT_ENVELOPED_DATA *data = enveloped_data->data;
3433
3434     if (para->cbSize != sizeof(CMSG_CTRL_DECRYPT_PARA))
3435         SetLastError(E_INVALIDARG);
3436     else if (!data)
3437         SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3438     else if (para->dwRecipientIndex >= data->cRecipientInfo)
3439         SetLastError(CRYPT_E_INVALID_INDEX);
3440     else if (enveloped_data->decrypted)
3441         SetLastError(CRYPT_E_ALREADY_DECRYPTED);
3442     else if (!para->hCryptProv)
3443         SetLastError(ERROR_INVALID_PARAMETER);
3444     else if (enveloped_data->content.cbData)
3445     {
3446         HCRYPTKEY key;
3447
3448         ret = CRYPT_ImportEncryptedKey(
3449          &data->encryptedContentInfo.contentEncryptionAlgorithm, para,
3450          data->rgRecipientInfo, &key);
3451         if (ret)
3452         {
3453             ret = CryptDecrypt(key, 0, TRUE, 0, enveloped_data->content.pbData,
3454              &enveloped_data->content.cbData);
3455             CryptDestroyKey(key);
3456         }
3457     }
3458     else
3459         ret = TRUE;
3460     if (ret)
3461         enveloped_data->decrypted = TRUE;
3462     return ret;
3463 }
3464
3465 static BOOL CDecodeMsg_Control(HCRYPTMSG hCryptMsg, DWORD dwFlags,
3466  DWORD dwCtrlType, const void *pvCtrlPara)
3467 {
3468     CDecodeMsg *msg = hCryptMsg;
3469     BOOL ret = FALSE;
3470
3471     switch (dwCtrlType)
3472     {
3473     case CMSG_CTRL_VERIFY_SIGNATURE:
3474         switch (msg->type)
3475         {
3476         case CMSG_SIGNED:
3477             ret = CDecodeSignedMsg_VerifySignature(msg, (PCERT_INFO)pvCtrlPara);
3478             break;
3479         default:
3480             SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3481         }
3482         break;
3483     case CMSG_CTRL_DECRYPT:
3484         switch (msg->type)
3485         {
3486         case CMSG_ENVELOPED:
3487             ret = CDecodeEnvelopedMsg_CrtlDecrypt(msg,
3488              (PCMSG_CTRL_DECRYPT_PARA)pvCtrlPara);
3489             if (ret && (dwFlags & CMSG_CRYPT_RELEASE_CONTEXT_FLAG))
3490                 msg->u.enveloped_data.crypt_prov =
3491                  ((PCMSG_CTRL_DECRYPT_PARA)pvCtrlPara)->hCryptProv;
3492             break;
3493         default:
3494             SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3495         }
3496         break;
3497     case CMSG_CTRL_VERIFY_HASH:
3498         switch (msg->type)
3499         {
3500         case CMSG_HASHED:
3501             ret = CDecodeHashMsg_VerifyHash(msg);
3502             break;
3503         default:
3504             SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3505         }
3506         break;
3507     case CMSG_CTRL_VERIFY_SIGNATURE_EX:
3508         switch (msg->type)
3509         {
3510         case CMSG_SIGNED:
3511             ret = CDecodeSignedMsg_VerifySignatureEx(msg,
3512              (PCMSG_CTRL_VERIFY_SIGNATURE_EX_PARA)pvCtrlPara);
3513             break;
3514         default:
3515             SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3516         }
3517         break;
3518     default:
3519         SetLastError(CRYPT_E_CONTROL_TYPE);
3520     }
3521     return ret;
3522 }
3523
3524 HCRYPTMSG WINAPI CryptMsgOpenToDecode(DWORD dwMsgEncodingType, DWORD dwFlags,
3525  DWORD dwMsgType, HCRYPTPROV_LEGACY hCryptProv, PCERT_INFO pRecipientInfo,
3526  PCMSG_STREAM_INFO pStreamInfo)
3527 {
3528     CDecodeMsg *msg;
3529
3530     TRACE("(%08x, %08x, %08x, %08lx, %p, %p)\n", dwMsgEncodingType,
3531      dwFlags, dwMsgType, hCryptProv, pRecipientInfo, pStreamInfo);
3532
3533     if (GET_CMSG_ENCODING_TYPE(dwMsgEncodingType) != PKCS_7_ASN_ENCODING)
3534     {
3535         SetLastError(E_INVALIDARG);
3536         return NULL;
3537     }
3538     msg = CryptMemAlloc(sizeof(CDecodeMsg));
3539     if (msg)
3540     {
3541         CryptMsgBase_Init((CryptMsgBase *)msg, dwFlags, pStreamInfo,
3542          CDecodeMsg_Close, CDecodeMsg_GetParam, CDecodeMsg_Update,
3543          CDecodeMsg_Control);
3544         msg->type = dwMsgType;
3545         if (hCryptProv)
3546             msg->crypt_prov = hCryptProv;
3547         else
3548         {
3549             msg->crypt_prov = CRYPT_GetDefaultProvider();
3550             msg->base.open_flags &= ~CMSG_CRYPT_RELEASE_CONTEXT_FLAG;
3551         }
3552         memset(&msg->u, 0, sizeof(msg->u));
3553         msg->msg_data.cbData = 0;
3554         msg->msg_data.pbData = NULL;
3555         msg->detached_data.cbData = 0;
3556         msg->detached_data.pbData = NULL;
3557         msg->properties = ContextPropertyList_Create();
3558     }
3559     return msg;
3560 }
3561
3562 HCRYPTMSG WINAPI CryptMsgDuplicate(HCRYPTMSG hCryptMsg)
3563 {
3564     TRACE("(%p)\n", hCryptMsg);
3565
3566     if (hCryptMsg)
3567     {
3568         CryptMsgBase *msg = hCryptMsg;
3569
3570         InterlockedIncrement(&msg->ref);
3571     }
3572     return hCryptMsg;
3573 }
3574
3575 BOOL WINAPI CryptMsgClose(HCRYPTMSG hCryptMsg)
3576 {
3577     TRACE("(%p)\n", hCryptMsg);
3578
3579     if (hCryptMsg)
3580     {
3581         CryptMsgBase *msg = hCryptMsg;
3582
3583         if (InterlockedDecrement(&msg->ref) == 0)
3584         {
3585             TRACE("freeing %p\n", msg);
3586             if (msg->close)
3587                 msg->close(msg);
3588             CryptMemFree(msg);
3589         }
3590     }
3591     return TRUE;
3592 }
3593
3594 BOOL WINAPI CryptMsgUpdate(HCRYPTMSG hCryptMsg, const BYTE *pbData,
3595  DWORD cbData, BOOL fFinal)
3596 {
3597     CryptMsgBase *msg = hCryptMsg;
3598
3599     TRACE("(%p, %p, %d, %d)\n", hCryptMsg, pbData, cbData, fFinal);
3600
3601     return msg->update(hCryptMsg, pbData, cbData, fFinal);
3602 }
3603
3604 BOOL WINAPI CryptMsgGetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
3605  DWORD dwIndex, void *pvData, DWORD *pcbData)
3606 {
3607     CryptMsgBase *msg = hCryptMsg;
3608
3609     TRACE("(%p, %d, %d, %p, %p)\n", hCryptMsg, dwParamType, dwIndex,
3610      pvData, pcbData);
3611     return msg->get_param(hCryptMsg, dwParamType, dwIndex, pvData, pcbData);
3612 }
3613
3614 BOOL WINAPI CryptMsgControl(HCRYPTMSG hCryptMsg, DWORD dwFlags,
3615  DWORD dwCtrlType, const void *pvCtrlPara)
3616 {
3617     CryptMsgBase *msg = hCryptMsg;
3618
3619     TRACE("(%p, %08x, %d, %p)\n", hCryptMsg, dwFlags, dwCtrlType,
3620      pvCtrlPara);
3621     return msg->control(hCryptMsg, dwFlags, dwCtrlType, pvCtrlPara);
3622 }
3623
3624 static CERT_INFO *CRYPT_GetSignerCertInfoFromMsg(HCRYPTMSG msg,
3625  DWORD dwSignerIndex)
3626 {
3627     CERT_INFO *certInfo = NULL;
3628     DWORD size;
3629
3630     if (CryptMsgGetParam(msg, CMSG_SIGNER_CERT_INFO_PARAM, dwSignerIndex, NULL,
3631      &size))
3632     {
3633         certInfo = CryptMemAlloc(size);
3634         if (certInfo)
3635         {
3636             if (!CryptMsgGetParam(msg, CMSG_SIGNER_CERT_INFO_PARAM,
3637              dwSignerIndex, certInfo, &size))
3638             {
3639                 CryptMemFree(certInfo);
3640                 certInfo = NULL;
3641             }
3642         }
3643     }
3644     return certInfo;
3645 }
3646
3647 BOOL WINAPI CryptMsgGetAndVerifySigner(HCRYPTMSG hCryptMsg, DWORD cSignerStore,
3648  HCERTSTORE *rghSignerStore, DWORD dwFlags, PCCERT_CONTEXT *ppSigner,
3649  DWORD *pdwSignerIndex)
3650 {
3651     HCERTSTORE store;
3652     DWORD i, signerIndex = 0;
3653     PCCERT_CONTEXT signerCert = NULL;
3654     BOOL ret = FALSE;
3655
3656     TRACE("(%p, %d, %p, %08x, %p, %p)\n", hCryptMsg, cSignerStore,
3657      rghSignerStore, dwFlags, ppSigner, pdwSignerIndex);
3658
3659     /* Clear output parameters */
3660     if (ppSigner)
3661         *ppSigner = NULL;
3662     if (pdwSignerIndex && !(dwFlags & CMSG_USE_SIGNER_INDEX_FLAG))
3663         *pdwSignerIndex = 0;
3664
3665     /* Create store to search for signer certificates */
3666     store = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
3667      CERT_STORE_CREATE_NEW_FLAG, NULL);
3668     if (!(dwFlags & CMSG_TRUSTED_SIGNER_FLAG))
3669     {
3670         HCERTSTORE msgStore = CertOpenStore(CERT_STORE_PROV_MSG, 0, 0, 0,
3671          hCryptMsg);
3672
3673         CertAddStoreToCollection(store, msgStore, 0, 0);
3674         CertCloseStore(msgStore, 0);
3675     }
3676     for (i = 0; i < cSignerStore; i++)
3677         CertAddStoreToCollection(store, rghSignerStore[i], 0, 0);
3678
3679     /* Find signer cert */
3680     if (dwFlags & CMSG_USE_SIGNER_INDEX_FLAG)
3681     {
3682         CERT_INFO *signer = CRYPT_GetSignerCertInfoFromMsg(hCryptMsg,
3683          *pdwSignerIndex);
3684
3685         if (signer)
3686         {
3687             signerIndex = *pdwSignerIndex;
3688             signerCert = CertFindCertificateInStore(store, X509_ASN_ENCODING,
3689              0, CERT_FIND_SUBJECT_CERT, signer, NULL);
3690             CryptMemFree(signer);
3691         }
3692     }
3693     else
3694     {
3695         DWORD count, size = sizeof(count);
3696
3697         if (CryptMsgGetParam(hCryptMsg, CMSG_SIGNER_COUNT_PARAM, 0, &count,
3698          &size))
3699         {
3700             for (i = 0; !signerCert && i < count; i++)
3701             {
3702                 CERT_INFO *signer = CRYPT_GetSignerCertInfoFromMsg(hCryptMsg,
3703                  i);
3704
3705                 if (signer)
3706                 {
3707                     signerCert = CertFindCertificateInStore(store,
3708                      X509_ASN_ENCODING, 0, CERT_FIND_SUBJECT_CERT, signer,
3709                      NULL);
3710                     if (signerCert)
3711                         signerIndex = i;
3712                     CryptMemFree(signer);
3713                 }
3714             }
3715         }
3716         if (!signerCert)
3717             SetLastError(CRYPT_E_NO_TRUSTED_SIGNER);
3718     }
3719     if (signerCert)
3720     {
3721         if (!(dwFlags & CMSG_SIGNER_ONLY_FLAG))
3722             ret = CryptMsgControl(hCryptMsg, 0, CMSG_CTRL_VERIFY_SIGNATURE,
3723              signerCert->pCertInfo);
3724         else
3725             ret = TRUE;
3726         if (ret)
3727         {
3728             if (ppSigner)
3729                 *ppSigner = CertDuplicateCertificateContext(signerCert);
3730             if (pdwSignerIndex)
3731                 *pdwSignerIndex = signerIndex;
3732         }
3733         CertFreeCertificateContext(signerCert);
3734     }
3735
3736     CertCloseStore(store, 0);
3737     return ret;
3738 }
3739
3740 BOOL WINAPI CryptMsgVerifyCountersignatureEncodedEx(HCRYPTPROV_LEGACY hCryptProv,
3741  DWORD dwEncodingType, PBYTE pbSignerInfo, DWORD cbSignerInfo,
3742  PBYTE pbSignerInfoCountersignature, DWORD cbSignerInfoCountersignature,
3743  DWORD dwSignerType, void *pvSigner, DWORD dwFlags, void *pvReserved)
3744 {
3745     FIXME("(%08lx, %08x, %p, %d, %p, %d, %d, %p, %08x, %p): stub\n", hCryptProv,
3746      dwEncodingType, pbSignerInfo, cbSignerInfo, pbSignerInfoCountersignature,
3747      cbSignerInfoCountersignature, dwSignerType, pvSigner, dwFlags, pvReserved);
3748     return FALSE;
3749 }
3750
3751 BOOL WINAPI CryptMsgEncodeAndSignCTL(DWORD dwMsgEncodingType,
3752  PCTL_INFO pCtlInfo, PCMSG_SIGNED_ENCODE_INFO pSignInfo, DWORD dwFlags,
3753  BYTE *pbEncoded, DWORD *pcbEncoded)
3754 {
3755     BOOL ret;
3756     BYTE *pbCtlContent;
3757     DWORD cbCtlContent;
3758
3759     TRACE("(%08x, %p, %p, %08x, %p, %p)\n", dwMsgEncodingType, pCtlInfo,
3760      pSignInfo, dwFlags, pbEncoded, pcbEncoded);
3761
3762     if (dwFlags)
3763     {
3764         FIXME("unimplemented for flags %08x\n", dwFlags);
3765         return FALSE;
3766     }
3767     if ((ret = CryptEncodeObjectEx(dwMsgEncodingType, PKCS_CTL, pCtlInfo,
3768      CRYPT_ENCODE_ALLOC_FLAG, NULL, &pbCtlContent, &cbCtlContent)))
3769     {
3770         ret = CryptMsgSignCTL(dwMsgEncodingType, pbCtlContent, cbCtlContent,
3771          pSignInfo, dwFlags, pbEncoded, pcbEncoded);
3772         LocalFree(pbCtlContent);
3773     }
3774     return ret;
3775 }
3776
3777 BOOL WINAPI CryptMsgSignCTL(DWORD dwMsgEncodingType, BYTE *pbCtlContent,
3778  DWORD cbCtlContent, PCMSG_SIGNED_ENCODE_INFO pSignInfo, DWORD dwFlags,
3779  BYTE *pbEncoded, DWORD *pcbEncoded)
3780 {
3781     static char oid_ctl[] = szOID_CTL;
3782     BOOL ret;
3783     HCRYPTMSG msg;
3784
3785     TRACE("(%08x, %p, %d, %p, %08x, %p, %p)\n", dwMsgEncodingType,
3786      pbCtlContent, cbCtlContent, pSignInfo, dwFlags, pbEncoded, pcbEncoded);
3787
3788     if (dwFlags)
3789     {
3790         FIXME("unimplemented for flags %08x\n", dwFlags);
3791         return FALSE;
3792     }
3793     msg = CryptMsgOpenToEncode(dwMsgEncodingType, 0, CMSG_SIGNED, pSignInfo,
3794      oid_ctl, NULL);
3795     if (msg)
3796     {
3797         ret = CryptMsgUpdate(msg, pbCtlContent, cbCtlContent, TRUE);
3798         if (ret)
3799             ret = CryptMsgGetParam(msg, CMSG_CONTENT_PARAM, 0, pbEncoded,
3800              pcbEncoded);
3801         CryptMsgClose(msg);
3802     }
3803     else
3804         ret = FALSE;
3805     return ret;
3806 }