msxml3: Support VT_I2 variants tracing.
[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             msg = NULL;
1454         }
1455     }
1456     return msg;
1457 }
1458
1459 typedef struct _CMSG_ENVELOPED_ENCODE_INFO_WITH_CMS
1460 {
1461     DWORD                       cbSize;
1462     HCRYPTPROV_LEGACY           hCryptProv;
1463     CRYPT_ALGORITHM_IDENTIFIER  ContentEncryptionAlgorithm;
1464     void                       *pvEncryptionAuxInfo;
1465     DWORD                       cRecipients;
1466     PCERT_INFO                 *rgpRecipientCert;
1467     PCMSG_RECIPIENT_ENCODE_INFO rgCmsRecipients;
1468     DWORD                       cCertEncoded;
1469     PCERT_BLOB                  rgCertEncoded;
1470     DWORD                       cCrlEncoded;
1471     PCRL_BLOB                   rgCrlEncoded;
1472     DWORD                       cAttrCertEncoded;
1473     PCERT_BLOB                  rgAttrCertEncoded;
1474     DWORD                       cUnprotectedAttr;
1475     PCRYPT_ATTRIBUTE            rgUnprotectedAttr;
1476 } CMSG_ENVELOPED_ENCODE_INFO_WITH_CMS, *PCMSG_ENVELOPED_ENCODE_INFO_WITH_CMS;
1477
1478 typedef struct _CEnvelopedEncodeMsg
1479 {
1480     CryptMsgBase                   base;
1481     CRYPT_ALGORITHM_IDENTIFIER     algo;
1482     HCRYPTPROV                     prov;
1483     HCRYPTKEY                      key;
1484     DWORD                          cRecipientInfo;
1485     CMSG_KEY_TRANS_RECIPIENT_INFO *recipientInfo;
1486     CRYPT_DATA_BLOB                data;
1487 } CEnvelopedEncodeMsg;
1488
1489 static BOOL CRYPT_ConstructAlgorithmId(CRYPT_ALGORITHM_IDENTIFIER *out,
1490  const CRYPT_ALGORITHM_IDENTIFIER *in)
1491 {
1492     out->pszObjId = CryptMemAlloc(strlen(in->pszObjId) + 1);
1493     if (out->pszObjId)
1494     {
1495         strcpy(out->pszObjId, in->pszObjId);
1496         return CRYPT_ConstructBlob(&out->Parameters, &in->Parameters);
1497     }
1498     else
1499         return FALSE;
1500 }
1501
1502 static BOOL CRYPT_ConstructBitBlob(CRYPT_BIT_BLOB *out, const CRYPT_BIT_BLOB *in)
1503 {
1504     out->cbData = in->cbData;
1505     out->cUnusedBits = in->cUnusedBits;
1506     if (out->cbData)
1507     {
1508         out->pbData = CryptMemAlloc(out->cbData);
1509         if (out->pbData)
1510             memcpy(out->pbData, in->pbData, out->cbData);
1511         else
1512             return FALSE;
1513     }
1514     else
1515         out->pbData = NULL;
1516     return TRUE;
1517 }
1518
1519 static BOOL CRYPT_GenKey(CMSG_CONTENT_ENCRYPT_INFO *info, ALG_ID algID)
1520 {
1521     static HCRYPTOIDFUNCSET set = NULL;
1522     PFN_CMSG_GEN_CONTENT_ENCRYPT_KEY genKeyFunc = NULL;
1523     HCRYPTOIDFUNCADDR hFunc;
1524     BOOL ret;
1525
1526     if (!set)
1527         set = CryptInitOIDFunctionSet(CMSG_OID_GEN_CONTENT_ENCRYPT_KEY_FUNC, 0);
1528     CryptGetOIDFunctionAddress(set, X509_ASN_ENCODING,
1529      info->ContentEncryptionAlgorithm.pszObjId, 0, (void **)&genKeyFunc, &hFunc);
1530     if (genKeyFunc)
1531     {
1532         ret = genKeyFunc(info, 0, NULL);
1533         CryptFreeOIDFunctionAddress(hFunc, 0);
1534     }
1535     else
1536         ret = CryptGenKey(info->hCryptProv, algID, CRYPT_EXPORTABLE,
1537          &info->hContentEncryptKey);
1538     return ret;
1539 }
1540
1541 static BOOL WINAPI CRYPT_ExportKeyTrans(
1542  PCMSG_CONTENT_ENCRYPT_INFO pContentEncryptInfo,
1543  PCMSG_KEY_TRANS_RECIPIENT_ENCODE_INFO pKeyTransEncodeInfo,
1544  PCMSG_KEY_TRANS_ENCRYPT_INFO pKeyTransEncryptInfo,
1545  DWORD dwFlags, void *pvReserved)
1546 {
1547     CERT_PUBLIC_KEY_INFO keyInfo;
1548     HCRYPTKEY expKey;
1549     BOOL ret;
1550
1551     ret = CRYPT_ConstructAlgorithmId(&keyInfo.Algorithm,
1552         &pKeyTransEncodeInfo->KeyEncryptionAlgorithm);
1553     if (ret)
1554         CRYPT_ConstructBitBlob(&keyInfo.PublicKey,
1555          &pKeyTransEncodeInfo->RecipientPublicKey);
1556
1557     if (ret)
1558         ret = CryptImportPublicKeyInfo(pKeyTransEncodeInfo->hCryptProv,
1559          X509_ASN_ENCODING, &keyInfo, &expKey);
1560     if (ret)
1561     {
1562         DWORD size;
1563
1564         ret = CryptExportKey(pContentEncryptInfo->hContentEncryptKey, expKey,
1565          SIMPLEBLOB, 0, NULL, &size);
1566         if (ret)
1567         {
1568             BYTE *keyBlob;
1569
1570             keyBlob = CryptMemAlloc(size);
1571             if (keyBlob)
1572             {
1573                 ret = CryptExportKey(pContentEncryptInfo->hContentEncryptKey,
1574                  expKey, SIMPLEBLOB, 0, keyBlob, &size);
1575                 if (ret)
1576                 {
1577                     DWORD head = sizeof(BLOBHEADER) + sizeof(ALG_ID);
1578
1579                     pKeyTransEncryptInfo->EncryptedKey.pbData =
1580                      CryptMemAlloc(size - head);
1581                     if (pKeyTransEncryptInfo->EncryptedKey.pbData)
1582                     {
1583                         DWORD i, k = 0;
1584
1585                         pKeyTransEncryptInfo->EncryptedKey.cbData = size - head;
1586                         for (i = size - 1; i >= head; --i, ++k)
1587                             pKeyTransEncryptInfo->EncryptedKey.pbData[k] =
1588                              keyBlob[i];
1589                     }
1590                     else
1591                         ret = FALSE;
1592                 }
1593                 CryptMemFree(keyBlob);
1594             }
1595             else
1596                 ret = FALSE;
1597         }
1598         CryptDestroyKey(expKey);
1599     }
1600
1601     CryptMemFree(keyInfo.PublicKey.pbData);
1602     CryptMemFree(keyInfo.Algorithm.pszObjId);
1603     CryptMemFree(keyInfo.Algorithm.Parameters.pbData);
1604     return ret;
1605 }
1606
1607 static BOOL CRYPT_ExportEncryptedKey(CMSG_CONTENT_ENCRYPT_INFO *info, DWORD i,
1608  CRYPT_DATA_BLOB *key)
1609 {
1610     static HCRYPTOIDFUNCSET set = NULL;
1611     PFN_CMSG_EXPORT_KEY_TRANS exportKeyFunc = NULL;
1612     HCRYPTOIDFUNCADDR hFunc = NULL;
1613     CMSG_KEY_TRANS_RECIPIENT_ENCODE_INFO *encodeInfo =
1614      info->rgCmsRecipients[i].u.pKeyTrans;
1615     CMSG_KEY_TRANS_ENCRYPT_INFO encryptInfo;
1616     BOOL ret;
1617
1618     memset(&encryptInfo, 0, sizeof(encryptInfo));
1619     encryptInfo.cbSize = sizeof(encryptInfo);
1620     encryptInfo.dwRecipientIndex = i;
1621     ret = CRYPT_ConstructAlgorithmId(&encryptInfo.KeyEncryptionAlgorithm,
1622      &encodeInfo->KeyEncryptionAlgorithm);
1623
1624     if (!set)
1625         set = CryptInitOIDFunctionSet(CMSG_OID_EXPORT_KEY_TRANS_FUNC, 0);
1626     CryptGetOIDFunctionAddress(set, X509_ASN_ENCODING,
1627      encryptInfo.KeyEncryptionAlgorithm.pszObjId, 0, (void **)&exportKeyFunc,
1628      &hFunc);
1629     if (!exportKeyFunc)
1630         exportKeyFunc = CRYPT_ExportKeyTrans;
1631     if (ret)
1632     {
1633         ret = exportKeyFunc(info, encodeInfo, &encryptInfo, 0, NULL);
1634         if (ret)
1635         {
1636             key->cbData = encryptInfo.EncryptedKey.cbData;
1637             key->pbData = encryptInfo.EncryptedKey.pbData;
1638         }
1639     }
1640     if (hFunc)
1641         CryptFreeOIDFunctionAddress(hFunc, 0);
1642
1643     CryptMemFree(encryptInfo.KeyEncryptionAlgorithm.pszObjId);
1644     CryptMemFree(encryptInfo.KeyEncryptionAlgorithm.Parameters.pbData);
1645     return ret;
1646 }
1647
1648 static LPVOID WINAPI mem_alloc(size_t size)
1649 {
1650     return HeapAlloc(GetProcessHeap(), 0, size);
1651 }
1652
1653 static VOID WINAPI mem_free(LPVOID pv)
1654 {
1655     HeapFree(GetProcessHeap(), 0, pv);
1656 }
1657
1658
1659 static BOOL CContentEncryptInfo_Construct(CMSG_CONTENT_ENCRYPT_INFO *info,
1660  const CMSG_ENVELOPED_ENCODE_INFO_WITH_CMS *in, HCRYPTPROV prov)
1661 {
1662     BOOL ret;
1663
1664     info->cbSize = sizeof(CMSG_CONTENT_ENCRYPT_INFO);
1665     info->hCryptProv = prov;
1666     ret = CRYPT_ConstructAlgorithmId(&info->ContentEncryptionAlgorithm,
1667      &in->ContentEncryptionAlgorithm);
1668     info->pvEncryptionAuxInfo = in->pvEncryptionAuxInfo;
1669     info->cRecipients = in->cRecipients;
1670     if (ret)
1671     {
1672         info->rgCmsRecipients = CryptMemAlloc(in->cRecipients *
1673          sizeof(CMSG_RECIPIENT_ENCODE_INFO));
1674         if (info->rgCmsRecipients)
1675         {
1676             DWORD i;
1677
1678             for (i = 0; ret && i < in->cRecipients; ++i)
1679             {
1680                 CMSG_KEY_TRANS_RECIPIENT_ENCODE_INFO *encodeInfo;
1681                 CERT_INFO *cert = in->rgpRecipientCert[i];
1682
1683                 info->rgCmsRecipients[i].dwRecipientChoice =
1684                  CMSG_KEY_TRANS_RECIPIENT;
1685                 encodeInfo = CryptMemAlloc(sizeof(*encodeInfo));
1686                 info->rgCmsRecipients[i].u.pKeyTrans = encodeInfo;
1687                 if (encodeInfo)
1688                 {
1689                     encodeInfo->cbSize = sizeof(*encodeInfo);
1690                     ret = CRYPT_ConstructAlgorithmId(
1691                      &encodeInfo->KeyEncryptionAlgorithm,
1692                      &cert->SubjectPublicKeyInfo.Algorithm);
1693                     encodeInfo->pvKeyEncryptionAuxInfo = NULL;
1694                     encodeInfo->hCryptProv = prov;
1695                     if (ret)
1696                         ret = CRYPT_ConstructBitBlob(
1697                          &encodeInfo->RecipientPublicKey,
1698                          &cert->SubjectPublicKeyInfo.PublicKey);
1699                     if (ret)
1700                         ret = CRYPT_ConstructBlob(
1701                          &encodeInfo->RecipientId.u.IssuerSerialNumber.Issuer,
1702                          &cert->Issuer);
1703                     if (ret)
1704                         ret = CRYPT_ConstructBlob(
1705                          &encodeInfo->RecipientId.u.IssuerSerialNumber.SerialNumber,
1706                          &cert->SerialNumber);
1707                 }
1708                 else
1709                     ret = FALSE;
1710             }
1711         }
1712         else
1713             ret = FALSE;
1714     }
1715     info->pfnAlloc = mem_alloc;
1716     info->pfnFree = mem_free;
1717     return ret;
1718 }
1719
1720 static void CContentEncryptInfo_Free(CMSG_CONTENT_ENCRYPT_INFO *info)
1721 {
1722     CryptMemFree(info->ContentEncryptionAlgorithm.pszObjId);
1723     CryptMemFree(info->ContentEncryptionAlgorithm.Parameters.pbData);
1724     if (info->rgCmsRecipients)
1725     {
1726         DWORD i;
1727
1728         for (i = 0; i < info->cRecipients; ++i)
1729         {
1730             CMSG_KEY_TRANS_RECIPIENT_ENCODE_INFO *encodeInfo =
1731              info->rgCmsRecipients[i].u.pKeyTrans;
1732
1733             CryptMemFree(encodeInfo->KeyEncryptionAlgorithm.pszObjId);
1734             CryptMemFree(encodeInfo->KeyEncryptionAlgorithm.Parameters.pbData);
1735             CryptMemFree(encodeInfo->RecipientPublicKey.pbData);
1736             CryptMemFree(
1737              encodeInfo->RecipientId.u.IssuerSerialNumber.Issuer.pbData);
1738             CryptMemFree(
1739              encodeInfo->RecipientId.u.IssuerSerialNumber.SerialNumber.pbData);
1740             CryptMemFree(encodeInfo);
1741         }
1742         CryptMemFree(info->rgCmsRecipients);
1743     }
1744 }
1745
1746 static BOOL CRecipientInfo_Construct(CMSG_KEY_TRANS_RECIPIENT_INFO *info,
1747  const CERT_INFO *cert, CRYPT_DATA_BLOB *key)
1748 {
1749     BOOL ret;
1750
1751     info->dwVersion = CMSG_KEY_TRANS_PKCS_1_5_VERSION;
1752     info->RecipientId.dwIdChoice = CERT_ID_ISSUER_SERIAL_NUMBER;
1753     ret = CRYPT_ConstructBlob(&info->RecipientId.u.IssuerSerialNumber.Issuer,
1754      &cert->Issuer);
1755     if (ret)
1756         ret = CRYPT_ConstructBlob(
1757          &info->RecipientId.u.IssuerSerialNumber.SerialNumber,
1758          &cert->SerialNumber);
1759     if (ret)
1760         ret = CRYPT_ConstructAlgorithmId(&info->KeyEncryptionAlgorithm,
1761          &cert->SubjectPublicKeyInfo.Algorithm);
1762     info->EncryptedKey.cbData = key->cbData;
1763     info->EncryptedKey.pbData = key->pbData;
1764     return ret;
1765 }
1766
1767 static void CRecipientInfo_Free(CMSG_KEY_TRANS_RECIPIENT_INFO *info)
1768 {
1769     CryptMemFree(info->RecipientId.u.IssuerSerialNumber.Issuer.pbData);
1770     CryptMemFree(info->RecipientId.u.IssuerSerialNumber.SerialNumber.pbData);
1771     CryptMemFree(info->KeyEncryptionAlgorithm.pszObjId);
1772     CryptMemFree(info->KeyEncryptionAlgorithm.Parameters.pbData);
1773     CryptMemFree(info->EncryptedKey.pbData);
1774 }
1775
1776 static void CEnvelopedEncodeMsg_Close(HCRYPTMSG hCryptMsg)
1777 {
1778     CEnvelopedEncodeMsg *msg = hCryptMsg;
1779
1780     CryptMemFree(msg->algo.pszObjId);
1781     CryptMemFree(msg->algo.Parameters.pbData);
1782     if (msg->base.open_flags & CMSG_CRYPT_RELEASE_CONTEXT_FLAG)
1783         CryptReleaseContext(msg->prov, 0);
1784     CryptDestroyKey(msg->key);
1785     if (msg->recipientInfo)
1786     {
1787         DWORD i;
1788
1789         for (i = 0; i < msg->cRecipientInfo; ++i)
1790             CRecipientInfo_Free(&msg->recipientInfo[i]);
1791         CryptMemFree(msg->recipientInfo);
1792     }
1793     CryptMemFree(msg->data.pbData);
1794 }
1795
1796 static BOOL CEnvelopedEncodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
1797  DWORD dwIndex, void *pvData, DWORD *pcbData)
1798 {
1799     CEnvelopedEncodeMsg *msg = hCryptMsg;
1800     BOOL ret = FALSE;
1801
1802     switch (dwParamType)
1803     {
1804     case CMSG_BARE_CONTENT_PARAM:
1805         if (msg->base.streamed)
1806             SetLastError(E_INVALIDARG);
1807         else
1808         {
1809             char oid_rsa_data[] = szOID_RSA_data;
1810             CRYPT_ENVELOPED_DATA envelopedData = {
1811              CMSG_ENVELOPED_DATA_PKCS_1_5_VERSION, msg->cRecipientInfo,
1812              msg->recipientInfo, { oid_rsa_data, {
1813                msg->algo.pszObjId,
1814                { msg->algo.Parameters.cbData, msg->algo.Parameters.pbData }
1815               },
1816               { msg->data.cbData, msg->data.pbData }
1817              }
1818             };
1819
1820             ret = CRYPT_AsnEncodePKCSEnvelopedData(&envelopedData, pvData,
1821              pcbData);
1822         }
1823         break;
1824     case CMSG_CONTENT_PARAM:
1825     {
1826         CRYPT_CONTENT_INFO info;
1827
1828         ret = CryptMsgGetParam(hCryptMsg, CMSG_BARE_CONTENT_PARAM, 0, NULL,
1829          &info.Content.cbData);
1830         if (ret)
1831         {
1832             info.Content.pbData = CryptMemAlloc(info.Content.cbData);
1833             if (info.Content.pbData)
1834             {
1835                 ret = CryptMsgGetParam(hCryptMsg, CMSG_BARE_CONTENT_PARAM, 0,
1836                  info.Content.pbData, &info.Content.cbData);
1837                 if (ret)
1838                 {
1839                     char oid_rsa_enveloped[] = szOID_RSA_envelopedData;
1840
1841                     info.pszObjId = oid_rsa_enveloped;
1842                     ret = CryptEncodeObjectEx(X509_ASN_ENCODING,
1843                      PKCS_CONTENT_INFO, &info, 0, NULL, pvData, pcbData);
1844                 }
1845                 CryptMemFree(info.Content.pbData);
1846             }
1847             else
1848                 ret = FALSE;
1849         }
1850         break;
1851     }
1852     default:
1853         SetLastError(CRYPT_E_INVALID_MSG_TYPE);
1854     }
1855     return ret;
1856 }
1857
1858 static BOOL CEnvelopedEncodeMsg_Update(HCRYPTMSG hCryptMsg, const BYTE *pbData,
1859  DWORD cbData, BOOL fFinal)
1860 {
1861     CEnvelopedEncodeMsg *msg = hCryptMsg;
1862     BOOL ret = FALSE;
1863
1864     if (msg->base.state == MsgStateFinalized)
1865         SetLastError(CRYPT_E_MSG_ERROR);
1866     else if (msg->base.streamed)
1867     {
1868         FIXME("streamed stub\n");
1869         msg->base.state = fFinal ? MsgStateFinalized : MsgStateUpdated;
1870         ret = TRUE;
1871     }
1872     else
1873     {
1874         if (!fFinal)
1875         {
1876             if (msg->base.open_flags & CMSG_DETACHED_FLAG)
1877                 SetLastError(E_INVALIDARG);
1878             else
1879                 SetLastError(CRYPT_E_MSG_ERROR);
1880         }
1881         else
1882         {
1883             if (cbData)
1884             {
1885                 DWORD dataLen = cbData;
1886
1887                 msg->data.cbData = cbData;
1888                 msg->data.pbData = CryptMemAlloc(cbData);
1889                 if (msg->data.pbData)
1890                 {
1891                     memcpy(msg->data.pbData, pbData, cbData);
1892                     ret = CryptEncrypt(msg->key, 0, TRUE, 0, msg->data.pbData,
1893                      &dataLen, msg->data.cbData);
1894                     msg->data.cbData = dataLen;
1895                     if (dataLen > cbData)
1896                     {
1897                         msg->data.pbData = CryptMemRealloc(msg->data.pbData,
1898                          dataLen);
1899                         if (msg->data.pbData)
1900                         {
1901                             dataLen = cbData;
1902                             ret = CryptEncrypt(msg->key, 0, TRUE, 0,
1903                              msg->data.pbData, &dataLen, msg->data.cbData);
1904                         }
1905                         else
1906                             ret = FALSE;
1907                     }
1908                     if (!ret)
1909                         CryptMemFree(msg->data.pbData);
1910                 }
1911                 else
1912                     ret = FALSE;
1913                 if (!ret)
1914                 {
1915                     msg->data.cbData = 0;
1916                     msg->data.pbData = NULL;
1917                 }
1918             }
1919             else
1920                 ret = TRUE;
1921             msg->base.state = MsgStateFinalized;
1922         }
1923     }
1924     return ret;
1925 }
1926
1927 static HCRYPTMSG CEnvelopedEncodeMsg_Open(DWORD dwFlags,
1928  const void *pvMsgEncodeInfo, LPCSTR pszInnerContentObjID,
1929  PCMSG_STREAM_INFO pStreamInfo)
1930 {
1931     CEnvelopedEncodeMsg *msg;
1932     const CMSG_ENVELOPED_ENCODE_INFO_WITH_CMS *info = pvMsgEncodeInfo;
1933     HCRYPTPROV prov;
1934     ALG_ID algID;
1935
1936     if (info->cbSize != sizeof(CMSG_ENVELOPED_ENCODE_INFO) &&
1937      info->cbSize != sizeof(CMSG_ENVELOPED_ENCODE_INFO_WITH_CMS))
1938     {
1939         SetLastError(E_INVALIDARG);
1940         return NULL;
1941     }
1942     if (info->cbSize == sizeof(CMSG_ENVELOPED_ENCODE_INFO_WITH_CMS))
1943         FIXME("CMS fields unsupported\n");
1944     if (!(algID = CertOIDToAlgId(info->ContentEncryptionAlgorithm.pszObjId)))
1945     {
1946         SetLastError(CRYPT_E_UNKNOWN_ALGO);
1947         return NULL;
1948     }
1949     if (info->cRecipients && !info->rgpRecipientCert)
1950     {
1951         SetLastError(E_INVALIDARG);
1952         return NULL;
1953     }
1954     if (info->hCryptProv)
1955         prov = info->hCryptProv;
1956     else
1957     {
1958         prov = CRYPT_GetDefaultProvider();
1959         dwFlags &= ~CMSG_CRYPT_RELEASE_CONTEXT_FLAG;
1960     }
1961     msg = CryptMemAlloc(sizeof(CEnvelopedEncodeMsg));
1962     if (msg)
1963     {
1964         CRYPT_DATA_BLOB encryptedKey = { 0, NULL };
1965         CMSG_CONTENT_ENCRYPT_INFO encryptInfo;
1966         BOOL ret;
1967         DWORD i;
1968
1969         CryptMsgBase_Init((CryptMsgBase *)msg, dwFlags, pStreamInfo,
1970          CEnvelopedEncodeMsg_Close, CEnvelopedEncodeMsg_GetParam,
1971          CEnvelopedEncodeMsg_Update, CRYPT_DefaultMsgControl);
1972         ret = CRYPT_ConstructAlgorithmId(&msg->algo,
1973          &info->ContentEncryptionAlgorithm);
1974         msg->prov = prov;
1975         msg->data.cbData = 0;
1976         msg->data.pbData = NULL;
1977         msg->cRecipientInfo = info->cRecipients;
1978         msg->recipientInfo = CryptMemAlloc(info->cRecipients *
1979          sizeof(CMSG_KEY_TRANS_RECIPIENT_INFO));
1980         if (!msg->recipientInfo)
1981             ret = FALSE;
1982         memset(&encryptInfo, 0, sizeof(encryptInfo));
1983         if (ret)
1984         {
1985             ret = CContentEncryptInfo_Construct(&encryptInfo, info, prov);
1986             if (ret)
1987             {
1988                 ret = CRYPT_GenKey(&encryptInfo, algID);
1989                 if (ret)
1990                     msg->key = encryptInfo.hContentEncryptKey;
1991             }
1992         }
1993         for (i = 0; ret && i < msg->cRecipientInfo; ++i)
1994         {
1995             ret = CRYPT_ExportEncryptedKey(&encryptInfo, i, &encryptedKey);
1996             if (ret)
1997                 ret = CRecipientInfo_Construct(&msg->recipientInfo[i],
1998                  info->rgpRecipientCert[i], &encryptedKey);
1999         }
2000         CContentEncryptInfo_Free(&encryptInfo);
2001         if (!ret)
2002         {
2003             CryptMsgClose(msg);
2004             msg = NULL;
2005         }
2006     }
2007     if (!msg && (dwFlags & CMSG_CRYPT_RELEASE_CONTEXT_FLAG))
2008         CryptReleaseContext(prov, 0);
2009     return msg;
2010 }
2011
2012 HCRYPTMSG WINAPI CryptMsgOpenToEncode(DWORD dwMsgEncodingType, DWORD dwFlags,
2013  DWORD dwMsgType, const void *pvMsgEncodeInfo, LPSTR pszInnerContentObjID,
2014  PCMSG_STREAM_INFO pStreamInfo)
2015 {
2016     HCRYPTMSG msg = NULL;
2017
2018     TRACE("(%08x, %08x, %08x, %p, %s, %p)\n", dwMsgEncodingType, dwFlags,
2019      dwMsgType, pvMsgEncodeInfo, debugstr_a(pszInnerContentObjID), pStreamInfo);
2020
2021     if (GET_CMSG_ENCODING_TYPE(dwMsgEncodingType) != PKCS_7_ASN_ENCODING)
2022     {
2023         SetLastError(E_INVALIDARG);
2024         return NULL;
2025     }
2026     switch (dwMsgType)
2027     {
2028     case CMSG_DATA:
2029         msg = CDataEncodeMsg_Open(dwFlags, pvMsgEncodeInfo,
2030          pszInnerContentObjID, pStreamInfo);
2031         break;
2032     case CMSG_HASHED:
2033         msg = CHashEncodeMsg_Open(dwFlags, pvMsgEncodeInfo,
2034          pszInnerContentObjID, pStreamInfo);
2035         break;
2036     case CMSG_SIGNED:
2037         msg = CSignedEncodeMsg_Open(dwFlags, pvMsgEncodeInfo,
2038          pszInnerContentObjID, pStreamInfo);
2039         break;
2040     case CMSG_ENVELOPED:
2041         msg = CEnvelopedEncodeMsg_Open(dwFlags, pvMsgEncodeInfo,
2042          pszInnerContentObjID, pStreamInfo);
2043         break;
2044     case CMSG_SIGNED_AND_ENVELOPED:
2045     case CMSG_ENCRYPTED:
2046         /* defined but invalid, fall through */
2047     default:
2048         SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2049     }
2050     return msg;
2051 }
2052
2053 typedef struct _CEnvelopedDecodeMsg
2054 {
2055     CRYPT_ENVELOPED_DATA *data;
2056     HCRYPTPROV            crypt_prov;
2057     CRYPT_DATA_BLOB       content;
2058     BOOL                  decrypted;
2059 } CEnvelopedDecodeMsg;
2060
2061 typedef struct _CDecodeMsg
2062 {
2063     CryptMsgBase           base;
2064     DWORD                  type;
2065     HCRYPTPROV             crypt_prov;
2066     union {
2067         HCRYPTHASH          hash;
2068         CSignedMsgData      signed_data;
2069         CEnvelopedDecodeMsg enveloped_data;
2070     } u;
2071     CRYPT_DATA_BLOB        msg_data;
2072     CRYPT_DATA_BLOB        detached_data;
2073     PCONTEXT_PROPERTY_LIST properties;
2074 } CDecodeMsg;
2075
2076 static void CDecodeMsg_Close(HCRYPTMSG hCryptMsg)
2077 {
2078     CDecodeMsg *msg = hCryptMsg;
2079
2080     if (msg->base.open_flags & CMSG_CRYPT_RELEASE_CONTEXT_FLAG)
2081         CryptReleaseContext(msg->crypt_prov, 0);
2082     switch (msg->type)
2083     {
2084     case CMSG_HASHED:
2085         if (msg->u.hash)
2086             CryptDestroyHash(msg->u.hash);
2087         break;
2088     case CMSG_ENVELOPED:
2089         if (msg->u.enveloped_data.crypt_prov)
2090             CryptReleaseContext(msg->u.enveloped_data.crypt_prov, 0);
2091         LocalFree(msg->u.enveloped_data.data);
2092         CryptMemFree(msg->u.enveloped_data.content.pbData);
2093         break;
2094     case CMSG_SIGNED:
2095         if (msg->u.signed_data.info)
2096         {
2097             LocalFree(msg->u.signed_data.info);
2098             CSignedMsgData_CloseHandles(&msg->u.signed_data);
2099         }
2100         break;
2101     }
2102     CryptMemFree(msg->msg_data.pbData);
2103     CryptMemFree(msg->detached_data.pbData);
2104     ContextPropertyList_Free(msg->properties);
2105 }
2106
2107 static BOOL CDecodeMsg_CopyData(CRYPT_DATA_BLOB *blob, const BYTE *pbData,
2108  DWORD cbData)
2109 {
2110     BOOL ret = TRUE;
2111
2112     if (cbData)
2113     {
2114         if (blob->cbData)
2115             blob->pbData = CryptMemRealloc(blob->pbData,
2116              blob->cbData + cbData);
2117         else
2118             blob->pbData = CryptMemAlloc(cbData);
2119         if (blob->pbData)
2120         {
2121             memcpy(blob->pbData + blob->cbData, pbData, cbData);
2122             blob->cbData += cbData;
2123         }
2124         else
2125             ret = FALSE;
2126     }
2127     return ret;
2128 }
2129
2130 static BOOL CDecodeMsg_DecodeDataContent(CDecodeMsg *msg, const CRYPT_DER_BLOB *blob)
2131 {
2132     BOOL ret;
2133     CRYPT_DATA_BLOB *data;
2134     DWORD size;
2135
2136     ret = CryptDecodeObjectEx(X509_ASN_ENCODING, X509_OCTET_STRING,
2137      blob->pbData, blob->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, &data, &size);
2138     if (ret)
2139     {
2140         ret = ContextPropertyList_SetProperty(msg->properties,
2141          CMSG_CONTENT_PARAM, data->pbData, data->cbData);
2142         LocalFree(data);
2143     }
2144     return ret;
2145 }
2146
2147 static void CDecodeMsg_SaveAlgorithmID(CDecodeMsg *msg, DWORD param,
2148  const CRYPT_ALGORITHM_IDENTIFIER *id)
2149 {
2150     static const BYTE nullParams[] = { ASN_NULL, 0 };
2151     CRYPT_ALGORITHM_IDENTIFIER *copy;
2152     DWORD len = sizeof(CRYPT_ALGORITHM_IDENTIFIER);
2153
2154     /* Linearize algorithm id */
2155     len += strlen(id->pszObjId) + 1;
2156     len += id->Parameters.cbData;
2157     copy = CryptMemAlloc(len);
2158     if (copy)
2159     {
2160         copy->pszObjId =
2161          (LPSTR)((BYTE *)copy + sizeof(CRYPT_ALGORITHM_IDENTIFIER));
2162         strcpy(copy->pszObjId, id->pszObjId);
2163         copy->Parameters.pbData = (BYTE *)copy->pszObjId + strlen(id->pszObjId)
2164          + 1;
2165         /* Trick:  omit NULL parameters */
2166         if (id->Parameters.cbData == sizeof(nullParams) &&
2167          !memcmp(id->Parameters.pbData, nullParams, sizeof(nullParams)))
2168         {
2169             copy->Parameters.cbData = 0;
2170             len -= sizeof(nullParams);
2171         }
2172         else
2173             copy->Parameters.cbData = id->Parameters.cbData;
2174         if (copy->Parameters.cbData)
2175             memcpy(copy->Parameters.pbData, id->Parameters.pbData,
2176              id->Parameters.cbData);
2177         ContextPropertyList_SetProperty(msg->properties, param, (BYTE *)copy,
2178          len);
2179         CryptMemFree(copy);
2180     }
2181 }
2182
2183 static inline void CRYPT_FixUpAlgorithmID(CRYPT_ALGORITHM_IDENTIFIER *id)
2184 {
2185     id->pszObjId = (LPSTR)((BYTE *)id + sizeof(CRYPT_ALGORITHM_IDENTIFIER));
2186     id->Parameters.pbData = (BYTE *)id->pszObjId + strlen(id->pszObjId) + 1;
2187 }
2188
2189 static BOOL CDecodeMsg_DecodeHashedContent(CDecodeMsg *msg,
2190  const CRYPT_DER_BLOB *blob)
2191 {
2192     BOOL ret;
2193     CRYPT_DIGESTED_DATA *digestedData;
2194     DWORD size;
2195
2196     ret = CRYPT_AsnDecodePKCSDigestedData(blob->pbData, blob->cbData,
2197      CRYPT_DECODE_ALLOC_FLAG, NULL, (CRYPT_DIGESTED_DATA *)&digestedData,
2198      &size);
2199     if (ret)
2200     {
2201         ContextPropertyList_SetProperty(msg->properties, CMSG_VERSION_PARAM,
2202          (const BYTE *)&digestedData->version, sizeof(digestedData->version));
2203         CDecodeMsg_SaveAlgorithmID(msg, CMSG_HASH_ALGORITHM_PARAM,
2204          &digestedData->DigestAlgorithm);
2205         ContextPropertyList_SetProperty(msg->properties,
2206          CMSG_INNER_CONTENT_TYPE_PARAM,
2207          (const BYTE *)digestedData->ContentInfo.pszObjId,
2208          digestedData->ContentInfo.pszObjId ?
2209          strlen(digestedData->ContentInfo.pszObjId) + 1 : 0);
2210         if (!(msg->base.open_flags & CMSG_DETACHED_FLAG))
2211         {
2212             if (digestedData->ContentInfo.Content.cbData)
2213                 CDecodeMsg_DecodeDataContent(msg,
2214                  &digestedData->ContentInfo.Content);
2215             else
2216                 ContextPropertyList_SetProperty(msg->properties,
2217                  CMSG_CONTENT_PARAM, NULL, 0);
2218         }
2219         ContextPropertyList_SetProperty(msg->properties, CMSG_HASH_DATA_PARAM,
2220          digestedData->hash.pbData, digestedData->hash.cbData);
2221         LocalFree(digestedData);
2222     }
2223     return ret;
2224 }
2225
2226 static BOOL CDecodeMsg_DecodeEnvelopedContent(CDecodeMsg *msg,
2227  const CRYPT_DER_BLOB *blob)
2228 {
2229     BOOL ret;
2230     CRYPT_ENVELOPED_DATA *envelopedData;
2231     DWORD size;
2232
2233     ret = CRYPT_AsnDecodePKCSEnvelopedData(blob->pbData, blob->cbData,
2234      CRYPT_DECODE_ALLOC_FLAG, NULL, (CRYPT_ENVELOPED_DATA *)&envelopedData,
2235      &size);
2236     if (ret)
2237         msg->u.enveloped_data.data = envelopedData;
2238     return ret;
2239 }
2240
2241 static BOOL CDecodeMsg_DecodeSignedContent(CDecodeMsg *msg,
2242  const CRYPT_DER_BLOB *blob)
2243 {
2244     BOOL ret;
2245     CRYPT_SIGNED_INFO *signedInfo;
2246     DWORD size;
2247
2248     ret = CRYPT_AsnDecodeCMSSignedInfo(blob->pbData, blob->cbData,
2249      CRYPT_DECODE_ALLOC_FLAG, NULL, (CRYPT_SIGNED_INFO *)&signedInfo,
2250      &size);
2251     if (ret)
2252         msg->u.signed_data.info = signedInfo;
2253     return ret;
2254 }
2255
2256 /* Decodes the content in blob as the type given, and updates the value
2257  * (type, parameters, etc.) of msg based on what blob contains.
2258  * It doesn't just use msg's type, to allow a recursive call from an implicitly
2259  * typed message once the outer content info has been decoded.
2260  */
2261 static BOOL CDecodeMsg_DecodeContent(CDecodeMsg *msg, const CRYPT_DER_BLOB *blob,
2262  DWORD type)
2263 {
2264     BOOL ret;
2265
2266     switch (type)
2267     {
2268     case CMSG_DATA:
2269         if ((ret = CDecodeMsg_DecodeDataContent(msg, blob)))
2270             msg->type = CMSG_DATA;
2271         break;
2272     case CMSG_HASHED:
2273         if ((ret = CDecodeMsg_DecodeHashedContent(msg, blob)))
2274             msg->type = CMSG_HASHED;
2275         break;
2276     case CMSG_ENVELOPED:
2277         if ((ret = CDecodeMsg_DecodeEnvelopedContent(msg, blob)))
2278             msg->type = CMSG_ENVELOPED;
2279         break;
2280     case CMSG_SIGNED:
2281         if ((ret = CDecodeMsg_DecodeSignedContent(msg, blob)))
2282             msg->type = CMSG_SIGNED;
2283         break;
2284     default:
2285     {
2286         CRYPT_CONTENT_INFO *info;
2287         DWORD size;
2288
2289         ret = CryptDecodeObjectEx(X509_ASN_ENCODING, PKCS_CONTENT_INFO,
2290          msg->msg_data.pbData, msg->msg_data.cbData, CRYPT_DECODE_ALLOC_FLAG,
2291          NULL, &info, &size);
2292         if (ret)
2293         {
2294             if (!strcmp(info->pszObjId, szOID_RSA_data))
2295                 ret = CDecodeMsg_DecodeContent(msg, &info->Content, CMSG_DATA);
2296             else if (!strcmp(info->pszObjId, szOID_RSA_digestedData))
2297                 ret = CDecodeMsg_DecodeContent(msg, &info->Content,
2298                  CMSG_HASHED);
2299             else if (!strcmp(info->pszObjId, szOID_RSA_envelopedData))
2300                 ret = CDecodeMsg_DecodeContent(msg, &info->Content,
2301                  CMSG_ENVELOPED);
2302             else if (!strcmp(info->pszObjId, szOID_RSA_signedData))
2303                 ret = CDecodeMsg_DecodeContent(msg, &info->Content,
2304                  CMSG_SIGNED);
2305             else
2306             {
2307                 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2308                 ret = FALSE;
2309             }
2310             LocalFree(info);
2311         }
2312     }
2313     }
2314     return ret;
2315 }
2316
2317 static BOOL CDecodeMsg_FinalizeHashedContent(CDecodeMsg *msg,
2318  CRYPT_DER_BLOB *blob)
2319 {
2320     CRYPT_ALGORITHM_IDENTIFIER *hashAlgoID = NULL;
2321     DWORD size = 0;
2322     ALG_ID algID = 0;
2323     BOOL ret;
2324
2325     CryptMsgGetParam(msg, CMSG_HASH_ALGORITHM_PARAM, 0, NULL, &size);
2326     hashAlgoID = CryptMemAlloc(size);
2327     ret = CryptMsgGetParam(msg, CMSG_HASH_ALGORITHM_PARAM, 0, hashAlgoID,
2328      &size);
2329     if (ret)
2330         algID = CertOIDToAlgId(hashAlgoID->pszObjId);
2331     ret = CryptCreateHash(msg->crypt_prov, algID, 0, 0, &msg->u.hash);
2332     if (ret)
2333     {
2334         CRYPT_DATA_BLOB content;
2335
2336         if (msg->base.open_flags & CMSG_DETACHED_FLAG)
2337         {
2338             /* Unlike for non-detached messages, the data were never stored as
2339              * the content param, but were saved in msg->detached_data instead.
2340              */
2341             content.pbData = msg->detached_data.pbData;
2342             content.cbData = msg->detached_data.cbData;
2343         }
2344         else
2345             ret = ContextPropertyList_FindProperty(msg->properties,
2346              CMSG_CONTENT_PARAM, &content);
2347         if (ret)
2348             ret = CryptHashData(msg->u.hash, content.pbData, content.cbData, 0);
2349     }
2350     CryptMemFree(hashAlgoID);
2351     return ret;
2352 }
2353
2354 static BOOL CDecodeMsg_FinalizeEnvelopedContent(CDecodeMsg *msg,
2355  CRYPT_DER_BLOB *blob)
2356 {
2357     CRYPT_DATA_BLOB *content;
2358
2359     if (msg->base.open_flags & CMSG_DETACHED_FLAG)
2360         content = &msg->detached_data;
2361     else
2362         content =
2363          &msg->u.enveloped_data.data->encryptedContentInfo.encryptedContent;
2364
2365     return CRYPT_ConstructBlob(&msg->u.enveloped_data.content, content);
2366 }
2367
2368 static BOOL CDecodeMsg_FinalizeSignedContent(CDecodeMsg *msg,
2369  CRYPT_DER_BLOB *blob)
2370 {
2371     BOOL ret;
2372     DWORD i, size;
2373
2374     ret = CSignedMsgData_AllocateHandles(&msg->u.signed_data);
2375     for (i = 0; ret && i < msg->u.signed_data.info->cSignerInfo; i++)
2376         ret = CSignedMsgData_ConstructSignerHandles(&msg->u.signed_data, i,
2377          msg->crypt_prov);
2378     if (ret)
2379     {
2380         CRYPT_DATA_BLOB *content;
2381
2382         /* Now that we have all the content, update the hash handles with
2383          * it.  If the message is a detached message, the content is stored
2384          * in msg->detached_data rather than in the signed message's
2385          * content.
2386          */
2387         if (msg->base.open_flags & CMSG_DETACHED_FLAG)
2388             content = &msg->detached_data;
2389         else
2390             content = &msg->u.signed_data.info->content.Content;
2391         if (content->cbData)
2392         {
2393             /* If the message is not detached, have to decode the message's
2394              * content if the type is szOID_RSA_data.
2395              */
2396             if (!(msg->base.open_flags & CMSG_DETACHED_FLAG) &&
2397              !strcmp(msg->u.signed_data.info->content.pszObjId,
2398              szOID_RSA_data))
2399             {
2400                 CRYPT_DATA_BLOB *rsa_blob;
2401
2402                 ret = CryptDecodeObjectEx(X509_ASN_ENCODING,
2403                  X509_OCTET_STRING, content->pbData, content->cbData,
2404                  CRYPT_DECODE_ALLOC_FLAG, NULL, &rsa_blob, &size);
2405                 if (ret)
2406                 {
2407                     ret = CSignedMsgData_Update(&msg->u.signed_data,
2408                      rsa_blob->pbData, rsa_blob->cbData, TRUE, Verify);
2409                     LocalFree(rsa_blob);
2410                 }
2411             }
2412             else
2413                 ret = CSignedMsgData_Update(&msg->u.signed_data,
2414                  content->pbData, content->cbData, TRUE, Verify);
2415         }
2416     }
2417     return ret;
2418 }
2419
2420 static BOOL CDecodeMsg_FinalizeContent(CDecodeMsg *msg, CRYPT_DER_BLOB *blob)
2421 {
2422     BOOL ret = FALSE;
2423
2424     switch (msg->type)
2425     {
2426     case CMSG_HASHED:
2427         ret = CDecodeMsg_FinalizeHashedContent(msg, blob);
2428         break;
2429     case CMSG_ENVELOPED:
2430         ret = CDecodeMsg_FinalizeEnvelopedContent(msg, blob);
2431         break;
2432     case CMSG_SIGNED:
2433         ret = CDecodeMsg_FinalizeSignedContent(msg, blob);
2434         break;
2435     default:
2436         ret = TRUE;
2437     }
2438     return ret;
2439 }
2440
2441 static BOOL CDecodeMsg_Update(HCRYPTMSG hCryptMsg, const BYTE *pbData,
2442  DWORD cbData, BOOL fFinal)
2443 {
2444     CDecodeMsg *msg = hCryptMsg;
2445     BOOL ret = FALSE;
2446
2447     TRACE("(%p, %p, %d, %d)\n", hCryptMsg, pbData, cbData, fFinal);
2448
2449     if (msg->base.state == MsgStateFinalized)
2450         SetLastError(CRYPT_E_MSG_ERROR);
2451     else if (msg->base.streamed)
2452     {
2453         FIXME("(%p, %p, %d, %d): streamed update stub\n", hCryptMsg, pbData,
2454          cbData, fFinal);
2455         switch (msg->base.state)
2456         {
2457         case MsgStateInit:
2458             ret = CDecodeMsg_CopyData(&msg->msg_data, pbData, cbData);
2459             if (fFinal)
2460             {
2461                 if (msg->base.open_flags & CMSG_DETACHED_FLAG)
2462                     msg->base.state = MsgStateDataFinalized;
2463                 else
2464                     msg->base.state = MsgStateFinalized;
2465             }
2466             else
2467                 msg->base.state = MsgStateUpdated;
2468             break;
2469         case MsgStateUpdated:
2470             ret = CDecodeMsg_CopyData(&msg->msg_data, pbData, cbData);
2471             if (fFinal)
2472             {
2473                 if (msg->base.open_flags & CMSG_DETACHED_FLAG)
2474                     msg->base.state = MsgStateDataFinalized;
2475                 else
2476                     msg->base.state = MsgStateFinalized;
2477             }
2478             break;
2479         case MsgStateDataFinalized:
2480             ret = CDecodeMsg_CopyData(&msg->detached_data, pbData, cbData);
2481             if (fFinal)
2482                 msg->base.state = MsgStateFinalized;
2483             break;
2484         default:
2485             SetLastError(CRYPT_E_MSG_ERROR);
2486             break;
2487         }
2488     }
2489     else
2490     {
2491         if (!fFinal)
2492             SetLastError(CRYPT_E_MSG_ERROR);
2493         else
2494         {
2495             switch (msg->base.state)
2496             {
2497             case MsgStateInit:
2498                 ret = CDecodeMsg_CopyData(&msg->msg_data, pbData, cbData);
2499                 if (msg->base.open_flags & CMSG_DETACHED_FLAG)
2500                     msg->base.state = MsgStateDataFinalized;
2501                 else
2502                     msg->base.state = MsgStateFinalized;
2503                 break;
2504             case MsgStateDataFinalized:
2505                 ret = CDecodeMsg_CopyData(&msg->detached_data, pbData, cbData);
2506                 msg->base.state = MsgStateFinalized;
2507                 break;
2508             default:
2509                 SetLastError(CRYPT_E_MSG_ERROR);
2510             }
2511         }
2512     }
2513     if (ret && fFinal &&
2514      ((msg->base.open_flags & CMSG_DETACHED_FLAG && msg->base.state ==
2515      MsgStateDataFinalized) ||
2516      (!(msg->base.open_flags & CMSG_DETACHED_FLAG) && msg->base.state ==
2517      MsgStateFinalized)))
2518         ret = CDecodeMsg_DecodeContent(msg, &msg->msg_data, msg->type);
2519     if (ret && msg->base.state == MsgStateFinalized)
2520         ret = CDecodeMsg_FinalizeContent(msg, &msg->msg_data);
2521     return ret;
2522 }
2523
2524 static BOOL CDecodeHashMsg_GetParam(CDecodeMsg *msg, DWORD dwParamType,
2525  DWORD dwIndex, void *pvData, DWORD *pcbData)
2526 {
2527     BOOL ret = FALSE;
2528
2529     switch (dwParamType)
2530     {
2531     case CMSG_TYPE_PARAM:
2532         ret = CRYPT_CopyParam(pvData, pcbData, &msg->type, sizeof(msg->type));
2533         break;
2534     case CMSG_HASH_ALGORITHM_PARAM:
2535     {
2536         CRYPT_DATA_BLOB blob;
2537
2538         ret = ContextPropertyList_FindProperty(msg->properties, dwParamType,
2539          &blob);
2540         if (ret)
2541         {
2542             ret = CRYPT_CopyParam(pvData, pcbData, blob.pbData, blob.cbData);
2543             if (ret && pvData)
2544                 CRYPT_FixUpAlgorithmID(pvData);
2545         }
2546         else
2547             SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2548         break;
2549     }
2550     case CMSG_COMPUTED_HASH_PARAM:
2551         ret = CryptGetHashParam(msg->u.hash, HP_HASHVAL, pvData, pcbData, 0);
2552         break;
2553     default:
2554     {
2555         CRYPT_DATA_BLOB blob;
2556
2557         ret = ContextPropertyList_FindProperty(msg->properties, dwParamType,
2558          &blob);
2559         if (ret)
2560             ret = CRYPT_CopyParam(pvData, pcbData, blob.pbData, blob.cbData);
2561         else
2562             SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2563     }
2564     }
2565     return ret;
2566 }
2567
2568 /* nextData is an in/out parameter - on input it's the memory location in
2569  * which a copy of in's data should be made, and on output it's the memory
2570  * location immediately after out's copy of in's data.
2571  */
2572 static inline void CRYPT_CopyBlob(CRYPT_DATA_BLOB *out,
2573  const CRYPT_DATA_BLOB *in, LPBYTE *nextData)
2574 {
2575     out->cbData = in->cbData;
2576     if (in->cbData)
2577     {
2578         out->pbData = *nextData;
2579         memcpy(out->pbData, in->pbData, in->cbData);
2580         *nextData += in->cbData;
2581     }
2582 }
2583
2584 static inline void CRYPT_CopyAlgorithmId(CRYPT_ALGORITHM_IDENTIFIER *out,
2585  const CRYPT_ALGORITHM_IDENTIFIER *in, LPBYTE *nextData)
2586 {
2587     if (in->pszObjId)
2588     {
2589         out->pszObjId = (LPSTR)*nextData;
2590         strcpy(out->pszObjId, in->pszObjId);
2591         *nextData += strlen(out->pszObjId) + 1;
2592     }
2593     CRYPT_CopyBlob(&out->Parameters, &in->Parameters, nextData);
2594 }
2595
2596 static inline void CRYPT_CopyAttributes(CRYPT_ATTRIBUTES *out,
2597  const CRYPT_ATTRIBUTES *in, LPBYTE *nextData)
2598 {
2599     out->cAttr = in->cAttr;
2600     if (in->cAttr)
2601     {
2602         DWORD i;
2603
2604         *nextData = POINTER_ALIGN_DWORD_PTR(*nextData);
2605         out->rgAttr = (CRYPT_ATTRIBUTE *)*nextData;
2606         *nextData += in->cAttr * sizeof(CRYPT_ATTRIBUTE);
2607         for (i = 0; i < in->cAttr; i++)
2608         {
2609             if (in->rgAttr[i].pszObjId)
2610             {
2611                 out->rgAttr[i].pszObjId = (LPSTR)*nextData;
2612                 strcpy(out->rgAttr[i].pszObjId, in->rgAttr[i].pszObjId);
2613                 *nextData += strlen(in->rgAttr[i].pszObjId) + 1;
2614             }
2615             if (in->rgAttr[i].cValue)
2616             {
2617                 DWORD j;
2618
2619                 out->rgAttr[i].cValue = in->rgAttr[i].cValue;
2620                 *nextData = POINTER_ALIGN_DWORD_PTR(*nextData);
2621                 out->rgAttr[i].rgValue = (PCRYPT_DATA_BLOB)*nextData;
2622                 *nextData += in->rgAttr[i].cValue * sizeof(CRYPT_DATA_BLOB);
2623                 for (j = 0; j < in->rgAttr[i].cValue; j++)
2624                     CRYPT_CopyBlob(&out->rgAttr[i].rgValue[j],
2625                      &in->rgAttr[i].rgValue[j], nextData);
2626             }
2627         }
2628     }
2629 }
2630
2631 static DWORD CRYPT_SizeOfAttributes(const CRYPT_ATTRIBUTES *attr)
2632 {
2633     DWORD size = attr->cAttr * sizeof(CRYPT_ATTRIBUTE), i, j;
2634
2635     for (i = 0; i < attr->cAttr; i++)
2636     {
2637         if (attr->rgAttr[i].pszObjId)
2638             size += strlen(attr->rgAttr[i].pszObjId) + 1;
2639         /* align pointer */
2640         size = ALIGN_DWORD_PTR(size);
2641         size += attr->rgAttr[i].cValue * sizeof(CRYPT_DATA_BLOB);
2642         for (j = 0; j < attr->rgAttr[i].cValue; j++)
2643             size += attr->rgAttr[i].rgValue[j].cbData;
2644     }
2645     /* align pointer again to be conservative */
2646     size = ALIGN_DWORD_PTR(size);
2647     return size;
2648 }
2649
2650 static DWORD CRYPT_SizeOfKeyIdAsIssuerAndSerial(const CRYPT_DATA_BLOB *keyId)
2651 {
2652     static char oid_key_rdn[] = szOID_KEYID_RDN;
2653     DWORD size = 0;
2654     CERT_RDN_ATTR attr;
2655     CERT_RDN rdn = { 1, &attr };
2656     CERT_NAME_INFO name = { 1, &rdn };
2657
2658     attr.pszObjId = oid_key_rdn;
2659     attr.dwValueType = CERT_RDN_OCTET_STRING;
2660     attr.Value.cbData = keyId->cbData;
2661     attr.Value.pbData = keyId->pbData;
2662     if (CryptEncodeObject(X509_ASN_ENCODING, X509_NAME, &name, NULL, &size))
2663         size++; /* Only include size of special zero serial number on success */
2664     return size;
2665 }
2666
2667 static BOOL CRYPT_CopyKeyIdAsIssuerAndSerial(CERT_NAME_BLOB *issuer,
2668  CRYPT_INTEGER_BLOB *serialNumber, const CRYPT_DATA_BLOB *keyId, DWORD encodedLen,
2669  LPBYTE *nextData)
2670 {
2671     static char oid_key_rdn[] = szOID_KEYID_RDN;
2672     CERT_RDN_ATTR attr;
2673     CERT_RDN rdn = { 1, &attr };
2674     CERT_NAME_INFO name = { 1, &rdn };
2675     BOOL ret;
2676
2677     /* Encode special zero serial number */
2678     serialNumber->cbData = 1;
2679     serialNumber->pbData = *nextData;
2680     **nextData = 0;
2681     (*nextData)++;
2682     /* Encode issuer */
2683     issuer->pbData = *nextData;
2684     attr.pszObjId = oid_key_rdn;
2685     attr.dwValueType = CERT_RDN_OCTET_STRING;
2686     attr.Value.cbData = keyId->cbData;
2687     attr.Value.pbData = keyId->pbData;
2688     ret = CryptEncodeObject(X509_ASN_ENCODING, X509_NAME, &name, *nextData,
2689      &encodedLen);
2690     if (ret)
2691     {
2692         *nextData += encodedLen;
2693         issuer->cbData = encodedLen;
2694     }
2695     return ret;
2696 }
2697
2698 static BOOL CRYPT_CopySignerInfo(void *pvData, DWORD *pcbData,
2699  const CMSG_CMS_SIGNER_INFO *in)
2700 {
2701     DWORD size = sizeof(CMSG_SIGNER_INFO), rdnSize = 0;
2702     BOOL ret;
2703
2704     TRACE("(%p, %d, %p)\n", pvData, pvData ? *pcbData : 0, in);
2705
2706     if (in->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
2707     {
2708         size += in->SignerId.u.IssuerSerialNumber.Issuer.cbData;
2709         size += in->SignerId.u.IssuerSerialNumber.SerialNumber.cbData;
2710     }
2711     else
2712     {
2713         rdnSize = CRYPT_SizeOfKeyIdAsIssuerAndSerial(&in->SignerId.u.KeyId);
2714         size += rdnSize;
2715     }
2716     if (in->HashAlgorithm.pszObjId)
2717         size += strlen(in->HashAlgorithm.pszObjId) + 1;
2718     size += in->HashAlgorithm.Parameters.cbData;
2719     if (in->HashEncryptionAlgorithm.pszObjId)
2720         size += strlen(in->HashEncryptionAlgorithm.pszObjId) + 1;
2721     size += in->HashEncryptionAlgorithm.Parameters.cbData;
2722     size += in->EncryptedHash.cbData;
2723     /* align pointer */
2724     size = ALIGN_DWORD_PTR(size);
2725     size += CRYPT_SizeOfAttributes(&in->AuthAttrs);
2726     size += CRYPT_SizeOfAttributes(&in->UnauthAttrs);
2727     if (!pvData)
2728     {
2729         *pcbData = size;
2730         ret = TRUE;
2731     }
2732     else if (*pcbData < size)
2733     {
2734         *pcbData = size;
2735         SetLastError(ERROR_MORE_DATA);
2736         ret = FALSE;
2737     }
2738     else
2739     {
2740         LPBYTE nextData = (BYTE *)pvData + sizeof(CMSG_SIGNER_INFO);
2741         CMSG_SIGNER_INFO *out = pvData;
2742
2743         ret = TRUE;
2744         out->dwVersion = in->dwVersion;
2745         if (in->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
2746         {
2747             CRYPT_CopyBlob(&out->Issuer,
2748              &in->SignerId.u.IssuerSerialNumber.Issuer, &nextData);
2749             CRYPT_CopyBlob(&out->SerialNumber,
2750              &in->SignerId.u.IssuerSerialNumber.SerialNumber, &nextData);
2751         }
2752         else
2753             ret = CRYPT_CopyKeyIdAsIssuerAndSerial(&out->Issuer, &out->SerialNumber,
2754              &in->SignerId.u.KeyId, rdnSize, &nextData);
2755         if (ret)
2756         {
2757             CRYPT_CopyAlgorithmId(&out->HashAlgorithm, &in->HashAlgorithm,
2758              &nextData);
2759             CRYPT_CopyAlgorithmId(&out->HashEncryptionAlgorithm,
2760              &in->HashEncryptionAlgorithm, &nextData);
2761             CRYPT_CopyBlob(&out->EncryptedHash, &in->EncryptedHash, &nextData);
2762             nextData = POINTER_ALIGN_DWORD_PTR(nextData);
2763             CRYPT_CopyAttributes(&out->AuthAttrs, &in->AuthAttrs, &nextData);
2764             CRYPT_CopyAttributes(&out->UnauthAttrs, &in->UnauthAttrs, &nextData);
2765         }
2766     }
2767     TRACE("returning %d\n", ret);
2768     return ret;
2769 }
2770
2771 static BOOL CRYPT_CopyCMSSignerInfo(void *pvData, DWORD *pcbData,
2772  const CMSG_CMS_SIGNER_INFO *in)
2773 {
2774     DWORD size = sizeof(CMSG_CMS_SIGNER_INFO);
2775     BOOL ret;
2776
2777     TRACE("(%p, %d, %p)\n", pvData, pvData ? *pcbData : 0, in);
2778
2779     if (in->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
2780     {
2781         size += in->SignerId.u.IssuerSerialNumber.Issuer.cbData;
2782         size += in->SignerId.u.IssuerSerialNumber.SerialNumber.cbData;
2783     }
2784     else
2785         size += in->SignerId.u.KeyId.cbData;
2786     if (in->HashAlgorithm.pszObjId)
2787         size += strlen(in->HashAlgorithm.pszObjId) + 1;
2788     size += in->HashAlgorithm.Parameters.cbData;
2789     if (in->HashEncryptionAlgorithm.pszObjId)
2790         size += strlen(in->HashEncryptionAlgorithm.pszObjId) + 1;
2791     size += in->HashEncryptionAlgorithm.Parameters.cbData;
2792     size += in->EncryptedHash.cbData;
2793     /* align pointer */
2794     size = ALIGN_DWORD_PTR(size);
2795     size += CRYPT_SizeOfAttributes(&in->AuthAttrs);
2796     size += CRYPT_SizeOfAttributes(&in->UnauthAttrs);
2797     if (!pvData)
2798     {
2799         *pcbData = size;
2800         ret = TRUE;
2801     }
2802     else if (*pcbData < size)
2803     {
2804         *pcbData = size;
2805         SetLastError(ERROR_MORE_DATA);
2806         ret = FALSE;
2807     }
2808     else
2809     {
2810         LPBYTE nextData = (BYTE *)pvData + sizeof(CMSG_CMS_SIGNER_INFO);
2811         CMSG_CMS_SIGNER_INFO *out = pvData;
2812
2813         out->dwVersion = in->dwVersion;
2814         out->SignerId.dwIdChoice = in->SignerId.dwIdChoice;
2815         if (in->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
2816         {
2817             CRYPT_CopyBlob(&out->SignerId.u.IssuerSerialNumber.Issuer,
2818              &in->SignerId.u.IssuerSerialNumber.Issuer, &nextData);
2819             CRYPT_CopyBlob(&out->SignerId.u.IssuerSerialNumber.SerialNumber,
2820              &in->SignerId.u.IssuerSerialNumber.SerialNumber, &nextData);
2821         }
2822         else
2823             CRYPT_CopyBlob(&out->SignerId.u.KeyId, &in->SignerId.u.KeyId, &nextData);
2824         CRYPT_CopyAlgorithmId(&out->HashAlgorithm, &in->HashAlgorithm,
2825          &nextData);
2826         CRYPT_CopyAlgorithmId(&out->HashEncryptionAlgorithm,
2827          &in->HashEncryptionAlgorithm, &nextData);
2828         CRYPT_CopyBlob(&out->EncryptedHash, &in->EncryptedHash, &nextData);
2829         nextData = POINTER_ALIGN_DWORD_PTR(nextData);
2830         CRYPT_CopyAttributes(&out->AuthAttrs, &in->AuthAttrs, &nextData);
2831         CRYPT_CopyAttributes(&out->UnauthAttrs, &in->UnauthAttrs, &nextData);
2832         ret = TRUE;
2833     }
2834     TRACE("returning %d\n", ret);
2835     return ret;
2836 }
2837
2838 static BOOL CRYPT_CopySignerCertInfo(void *pvData, DWORD *pcbData,
2839  const CMSG_CMS_SIGNER_INFO *in)
2840 {
2841     DWORD size = sizeof(CERT_INFO), rdnSize = 0;
2842     BOOL ret;
2843
2844     TRACE("(%p, %d, %p)\n", pvData, pvData ? *pcbData : 0, in);
2845
2846     if (in->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
2847     {
2848         size += in->SignerId.u.IssuerSerialNumber.Issuer.cbData;
2849         size += in->SignerId.u.IssuerSerialNumber.SerialNumber.cbData;
2850     }
2851     else
2852     {
2853         rdnSize = CRYPT_SizeOfKeyIdAsIssuerAndSerial(&in->SignerId.u.KeyId);
2854         size += rdnSize;
2855     }
2856     if (!pvData)
2857     {
2858         *pcbData = size;
2859         ret = TRUE;
2860     }
2861     else if (*pcbData < size)
2862     {
2863         *pcbData = size;
2864         SetLastError(ERROR_MORE_DATA);
2865         ret = FALSE;
2866     }
2867     else
2868     {
2869         LPBYTE nextData = (BYTE *)pvData + sizeof(CERT_INFO);
2870         CERT_INFO *out = pvData;
2871
2872         memset(out, 0, sizeof(CERT_INFO));
2873         if (in->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
2874         {
2875             CRYPT_CopyBlob(&out->Issuer,
2876              &in->SignerId.u.IssuerSerialNumber.Issuer, &nextData);
2877             CRYPT_CopyBlob(&out->SerialNumber,
2878              &in->SignerId.u.IssuerSerialNumber.SerialNumber, &nextData);
2879             ret = TRUE;
2880         }
2881         else
2882             ret = CRYPT_CopyKeyIdAsIssuerAndSerial(&out->Issuer, &out->SerialNumber,
2883              &in->SignerId.u.KeyId, rdnSize, &nextData);
2884     }
2885     TRACE("returning %d\n", ret);
2886     return ret;
2887 }
2888
2889 static BOOL CRYPT_CopyRecipientInfo(void *pvData, DWORD *pcbData,
2890  const CERT_ISSUER_SERIAL_NUMBER *in)
2891 {
2892     DWORD size = sizeof(CERT_INFO);
2893     BOOL ret;
2894
2895     TRACE("(%p, %d, %p)\n", pvData, pvData ? *pcbData : 0, in);
2896
2897     size += in->SerialNumber.cbData;
2898     size += in->Issuer.cbData;
2899     if (!pvData)
2900     {
2901         *pcbData = size;
2902         ret = TRUE;
2903     }
2904     else if (*pcbData < size)
2905     {
2906         *pcbData = size;
2907         SetLastError(ERROR_MORE_DATA);
2908         ret = FALSE;
2909     }
2910     else
2911     {
2912         LPBYTE nextData = (BYTE *)pvData + sizeof(CERT_INFO);
2913         CERT_INFO *out = pvData;
2914
2915         CRYPT_CopyBlob(&out->SerialNumber, &in->SerialNumber, &nextData);
2916         CRYPT_CopyBlob(&out->Issuer, &in->Issuer, &nextData);
2917         ret = TRUE;
2918     }
2919     TRACE("returning %d\n", ret);
2920     return ret;
2921 }
2922
2923 static BOOL CDecodeEnvelopedMsg_GetParam(CDecodeMsg *msg, DWORD dwParamType,
2924  DWORD dwIndex, void *pvData, DWORD *pcbData)
2925 {
2926     BOOL ret = FALSE;
2927
2928     switch (dwParamType)
2929     {
2930     case CMSG_TYPE_PARAM:
2931         ret = CRYPT_CopyParam(pvData, pcbData, &msg->type, sizeof(msg->type));
2932         break;
2933     case CMSG_CONTENT_PARAM:
2934         if (msg->u.enveloped_data.data)
2935             ret = CRYPT_CopyParam(pvData, pcbData,
2936              msg->u.enveloped_data.content.pbData,
2937              msg->u.enveloped_data.content.cbData);
2938         else
2939             SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2940         break;
2941     case CMSG_RECIPIENT_COUNT_PARAM:
2942         if (msg->u.enveloped_data.data)
2943             ret = CRYPT_CopyParam(pvData, pcbData,
2944              &msg->u.enveloped_data.data->cRecipientInfo, sizeof(DWORD));
2945         else
2946             SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2947         break;
2948     case CMSG_RECIPIENT_INFO_PARAM:
2949         if (msg->u.enveloped_data.data)
2950         {
2951             if (dwIndex < msg->u.enveloped_data.data->cRecipientInfo)
2952             {
2953                 PCMSG_KEY_TRANS_RECIPIENT_INFO recipientInfo =
2954                  &msg->u.enveloped_data.data->rgRecipientInfo[dwIndex];
2955
2956                 ret = CRYPT_CopyRecipientInfo(pvData, pcbData,
2957                  &recipientInfo->RecipientId.u.IssuerSerialNumber);
2958             }
2959             else
2960                 SetLastError(CRYPT_E_INVALID_INDEX);
2961         }
2962         else
2963             SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2964         break;
2965     default:
2966         FIXME("unimplemented for %d\n", dwParamType);
2967         SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2968     }
2969     return ret;
2970 }
2971
2972 static BOOL CDecodeSignedMsg_GetParam(CDecodeMsg *msg, DWORD dwParamType,
2973  DWORD dwIndex, void *pvData, DWORD *pcbData)
2974 {
2975     BOOL ret = FALSE;
2976
2977     switch (dwParamType)
2978     {
2979     case CMSG_TYPE_PARAM:
2980         ret = CRYPT_CopyParam(pvData, pcbData, &msg->type, sizeof(msg->type));
2981         break;
2982     case CMSG_CONTENT_PARAM:
2983         if (msg->u.signed_data.info)
2984         {
2985             if (!strcmp(msg->u.signed_data.info->content.pszObjId,
2986              szOID_RSA_data))
2987             {
2988                 CRYPT_DATA_BLOB *blob;
2989                 DWORD size;
2990
2991                 ret = CryptDecodeObjectEx(X509_ASN_ENCODING, X509_OCTET_STRING,
2992                  msg->u.signed_data.info->content.Content.pbData,
2993                  msg->u.signed_data.info->content.Content.cbData,
2994                  CRYPT_DECODE_ALLOC_FLAG, NULL, &blob, &size);
2995                 if (ret)
2996                 {
2997                     ret = CRYPT_CopyParam(pvData, pcbData, blob->pbData,
2998                      blob->cbData);
2999                     LocalFree(blob);
3000                 }
3001             }
3002             else
3003                 ret = CRYPT_CopyParam(pvData, pcbData,
3004                  msg->u.signed_data.info->content.Content.pbData,
3005                  msg->u.signed_data.info->content.Content.cbData);
3006         }
3007         else
3008             SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3009         break;
3010     case CMSG_INNER_CONTENT_TYPE_PARAM:
3011         if (msg->u.signed_data.info)
3012             ret = CRYPT_CopyParam(pvData, pcbData,
3013              msg->u.signed_data.info->content.pszObjId,
3014              strlen(msg->u.signed_data.info->content.pszObjId) + 1);
3015         else
3016             SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3017         break;
3018     case CMSG_SIGNER_COUNT_PARAM:
3019         if (msg->u.signed_data.info)
3020             ret = CRYPT_CopyParam(pvData, pcbData,
3021              &msg->u.signed_data.info->cSignerInfo, sizeof(DWORD));
3022         else
3023             SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3024         break;
3025     case CMSG_SIGNER_INFO_PARAM:
3026         if (msg->u.signed_data.info)
3027         {
3028             if (dwIndex >= msg->u.signed_data.info->cSignerInfo)
3029                 SetLastError(CRYPT_E_INVALID_INDEX);
3030             else
3031                 ret = CRYPT_CopySignerInfo(pvData, pcbData,
3032                  &msg->u.signed_data.info->rgSignerInfo[dwIndex]);
3033         }
3034         else
3035             SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3036         break;
3037     case CMSG_SIGNER_CERT_INFO_PARAM:
3038         if (msg->u.signed_data.info)
3039         {
3040             if (dwIndex >= msg->u.signed_data.info->cSignerInfo)
3041                 SetLastError(CRYPT_E_INVALID_INDEX);
3042             else
3043                 ret = CRYPT_CopySignerCertInfo(pvData, pcbData,
3044                  &msg->u.signed_data.info->rgSignerInfo[dwIndex]);
3045         }
3046         else
3047             SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3048         break;
3049     case CMSG_CERT_COUNT_PARAM:
3050         if (msg->u.signed_data.info)
3051             ret = CRYPT_CopyParam(pvData, pcbData,
3052              &msg->u.signed_data.info->cCertEncoded, sizeof(DWORD));
3053         else
3054             SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3055         break;
3056     case CMSG_CERT_PARAM:
3057         if (msg->u.signed_data.info)
3058         {
3059             if (dwIndex >= msg->u.signed_data.info->cCertEncoded)
3060                 SetLastError(CRYPT_E_INVALID_INDEX);
3061             else
3062                 ret = CRYPT_CopyParam(pvData, pcbData,
3063                  msg->u.signed_data.info->rgCertEncoded[dwIndex].pbData,
3064                  msg->u.signed_data.info->rgCertEncoded[dwIndex].cbData);
3065         }
3066         else
3067             SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3068         break;
3069     case CMSG_CRL_COUNT_PARAM:
3070         if (msg->u.signed_data.info)
3071             ret = CRYPT_CopyParam(pvData, pcbData,
3072              &msg->u.signed_data.info->cCrlEncoded, sizeof(DWORD));
3073         else
3074             SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3075         break;
3076     case CMSG_CRL_PARAM:
3077         if (msg->u.signed_data.info)
3078         {
3079             if (dwIndex >= msg->u.signed_data.info->cCrlEncoded)
3080                 SetLastError(CRYPT_E_INVALID_INDEX);
3081             else
3082                 ret = CRYPT_CopyParam(pvData, pcbData,
3083                  msg->u.signed_data.info->rgCrlEncoded[dwIndex].pbData,
3084                  msg->u.signed_data.info->rgCrlEncoded[dwIndex].cbData);
3085         }
3086         else
3087             SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3088         break;
3089     case CMSG_COMPUTED_HASH_PARAM:
3090         if (msg->u.signed_data.info)
3091         {
3092             if (dwIndex >= msg->u.signed_data.cSignerHandle)
3093                 SetLastError(CRYPT_E_INVALID_INDEX);
3094             else
3095                 ret = CryptGetHashParam(
3096                  msg->u.signed_data.signerHandles[dwIndex].contentHash,
3097                  HP_HASHVAL, pvData, pcbData, 0);
3098         }
3099         else
3100             SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3101         break;
3102     case CMSG_ENCODED_SIGNER:
3103         if (msg->u.signed_data.info)
3104         {
3105             if (dwIndex >= msg->u.signed_data.info->cSignerInfo)
3106                 SetLastError(CRYPT_E_INVALID_INDEX);
3107             else
3108                 ret = CryptEncodeObjectEx(
3109                  X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, CMS_SIGNER_INFO,
3110                  &msg->u.signed_data.info->rgSignerInfo[dwIndex], 0, NULL,
3111                  pvData, pcbData);
3112         }
3113         else
3114             SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3115         break;
3116     case CMSG_ATTR_CERT_COUNT_PARAM:
3117         if (msg->u.signed_data.info)
3118         {
3119             DWORD attrCertCount = 0;
3120
3121             ret = CRYPT_CopyParam(pvData, pcbData,
3122              &attrCertCount, sizeof(DWORD));
3123         }
3124         else
3125             SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3126         break;
3127     case CMSG_ATTR_CERT_PARAM:
3128         if (msg->u.signed_data.info)
3129             SetLastError(CRYPT_E_INVALID_INDEX);
3130         else
3131             SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3132         break;
3133     case CMSG_CMS_SIGNER_INFO_PARAM:
3134         if (msg->u.signed_data.info)
3135         {
3136             if (dwIndex >= msg->u.signed_data.info->cSignerInfo)
3137                 SetLastError(CRYPT_E_INVALID_INDEX);
3138             else
3139                 ret = CRYPT_CopyCMSSignerInfo(pvData, pcbData,
3140                  &msg->u.signed_data.info->rgSignerInfo[dwIndex]);
3141         }
3142         else
3143             SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3144         break;
3145     default:
3146         FIXME("unimplemented for %d\n", dwParamType);
3147         SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3148     }
3149     return ret;
3150 }
3151
3152 static BOOL CDecodeMsg_GetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
3153  DWORD dwIndex, void *pvData, DWORD *pcbData)
3154 {
3155     CDecodeMsg *msg = hCryptMsg;
3156     BOOL ret = FALSE;
3157
3158     switch (msg->type)
3159     {
3160     case CMSG_HASHED:
3161         ret = CDecodeHashMsg_GetParam(msg, dwParamType, dwIndex, pvData,
3162          pcbData);
3163         break;
3164     case CMSG_ENVELOPED:
3165         ret = CDecodeEnvelopedMsg_GetParam(msg, dwParamType, dwIndex, pvData,
3166          pcbData);
3167         break;
3168     case CMSG_SIGNED:
3169         ret = CDecodeSignedMsg_GetParam(msg, dwParamType, dwIndex, pvData,
3170          pcbData);
3171         break;
3172     default:
3173         switch (dwParamType)
3174         {
3175         case CMSG_TYPE_PARAM:
3176             ret = CRYPT_CopyParam(pvData, pcbData, &msg->type,
3177              sizeof(msg->type));
3178             break;
3179         default:
3180         {
3181             CRYPT_DATA_BLOB blob;
3182
3183             ret = ContextPropertyList_FindProperty(msg->properties, dwParamType,
3184              &blob);
3185             if (ret)
3186                 ret = CRYPT_CopyParam(pvData, pcbData, blob.pbData,
3187                  blob.cbData);
3188             else
3189                 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3190         }
3191         }
3192     }
3193     return ret;
3194 }
3195
3196 static BOOL CDecodeHashMsg_VerifyHash(CDecodeMsg *msg)
3197 {
3198     BOOL ret;
3199     CRYPT_DATA_BLOB hashBlob;
3200
3201     ret = ContextPropertyList_FindProperty(msg->properties,
3202      CMSG_HASH_DATA_PARAM, &hashBlob);
3203     if (ret)
3204     {
3205         DWORD computedHashSize = 0;
3206
3207         ret = CDecodeHashMsg_GetParam(msg, CMSG_COMPUTED_HASH_PARAM, 0, NULL,
3208          &computedHashSize);
3209         if (hashBlob.cbData == computedHashSize)
3210         {
3211             LPBYTE computedHash = CryptMemAlloc(computedHashSize);
3212
3213             if (computedHash)
3214             {
3215                 ret = CDecodeHashMsg_GetParam(msg, CMSG_COMPUTED_HASH_PARAM, 0,
3216                  computedHash, &computedHashSize);
3217                 if (ret)
3218                 {
3219                     if (memcmp(hashBlob.pbData, computedHash, hashBlob.cbData))
3220                     {
3221                         SetLastError(CRYPT_E_HASH_VALUE);
3222                         ret = FALSE;
3223                     }
3224                 }
3225                 CryptMemFree(computedHash);
3226             }
3227             else
3228             {
3229                 SetLastError(ERROR_OUTOFMEMORY);
3230                 ret = FALSE;
3231             }
3232         }
3233         else
3234         {
3235             SetLastError(CRYPT_E_HASH_VALUE);
3236             ret = FALSE;
3237         }
3238     }
3239     return ret;
3240 }
3241
3242 static BOOL CDecodeSignedMsg_VerifySignatureWithKey(CDecodeMsg *msg,
3243  HCRYPTPROV prov, DWORD signerIndex, PCERT_PUBLIC_KEY_INFO keyInfo)
3244 {
3245     HCRYPTKEY key;
3246     BOOL ret;
3247
3248     if (!prov)
3249         prov = msg->crypt_prov;
3250     ret = CryptImportPublicKeyInfo(prov, X509_ASN_ENCODING, keyInfo, &key);
3251     if (ret)
3252     {
3253         HCRYPTHASH hash;
3254         CRYPT_HASH_BLOB reversedHash;
3255
3256         if (msg->u.signed_data.info->rgSignerInfo[signerIndex].AuthAttrs.cAttr)
3257             hash = msg->u.signed_data.signerHandles[signerIndex].authAttrHash;
3258         else
3259             hash = msg->u.signed_data.signerHandles[signerIndex].contentHash;
3260         ret = CRYPT_ConstructBlob(&reversedHash,
3261          &msg->u.signed_data.info->rgSignerInfo[signerIndex].EncryptedHash);
3262         if (ret)
3263         {
3264             CRYPT_ReverseBytes(&reversedHash);
3265             ret = CryptVerifySignatureW(hash, reversedHash.pbData,
3266              reversedHash.cbData, key, NULL, 0);
3267             CryptMemFree(reversedHash.pbData);
3268         }
3269         CryptDestroyKey(key);
3270     }
3271     return ret;
3272 }
3273
3274 static BOOL CDecodeSignedMsg_VerifySignature(CDecodeMsg *msg, PCERT_INFO info)
3275 {
3276     BOOL ret = FALSE;
3277     DWORD i;
3278
3279     if (!msg->u.signed_data.signerHandles)
3280     {
3281         SetLastError(NTE_BAD_SIGNATURE);
3282         return FALSE;
3283     }
3284     for (i = 0; !ret && i < msg->u.signed_data.info->cSignerInfo; i++)
3285     {
3286         PCMSG_CMS_SIGNER_INFO signerInfo =
3287          &msg->u.signed_data.info->rgSignerInfo[i];
3288
3289         if (signerInfo->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
3290         {
3291             ret = CertCompareCertificateName(X509_ASN_ENCODING,
3292              &signerInfo->SignerId.u.IssuerSerialNumber.Issuer,
3293              &info->Issuer);
3294             if (ret)
3295             {
3296                 ret = CertCompareIntegerBlob(
3297                  &signerInfo->SignerId.u.IssuerSerialNumber.SerialNumber,
3298                  &info->SerialNumber);
3299                 if (ret)
3300                     break;
3301             }
3302         }
3303         else
3304         {
3305             FIXME("signer %d: unimplemented for key id\n", i);
3306         }
3307     }
3308     if (ret)
3309         ret = CDecodeSignedMsg_VerifySignatureWithKey(msg, 0, i,
3310          &info->SubjectPublicKeyInfo);
3311     else
3312         SetLastError(CRYPT_E_SIGNER_NOT_FOUND);
3313
3314     return ret;
3315 }
3316
3317 static BOOL CDecodeSignedMsg_VerifySignatureEx(CDecodeMsg *msg,
3318  PCMSG_CTRL_VERIFY_SIGNATURE_EX_PARA para)
3319 {
3320     BOOL ret = FALSE;
3321
3322     if (para->cbSize != sizeof(CMSG_CTRL_VERIFY_SIGNATURE_EX_PARA))
3323         SetLastError(ERROR_INVALID_PARAMETER);
3324     else if (para->dwSignerIndex >= msg->u.signed_data.info->cSignerInfo)
3325         SetLastError(CRYPT_E_SIGNER_NOT_FOUND);
3326     else if (!msg->u.signed_data.signerHandles)
3327         SetLastError(NTE_BAD_SIGNATURE);
3328     else
3329     {
3330         switch (para->dwSignerType)
3331         {
3332         case CMSG_VERIFY_SIGNER_PUBKEY:
3333             ret = CDecodeSignedMsg_VerifySignatureWithKey(msg,
3334              para->hCryptProv, para->dwSignerIndex, para->pvSigner);
3335             break;
3336         case CMSG_VERIFY_SIGNER_CERT:
3337         {
3338             PCCERT_CONTEXT cert = para->pvSigner;
3339
3340             ret = CDecodeSignedMsg_VerifySignatureWithKey(msg, para->hCryptProv,
3341              para->dwSignerIndex, &cert->pCertInfo->SubjectPublicKeyInfo);
3342             break;
3343         }
3344         default:
3345             FIXME("unimplemented for signer type %d\n", para->dwSignerType);
3346             SetLastError(CRYPT_E_SIGNER_NOT_FOUND);
3347         }
3348     }
3349     return ret;
3350 }
3351
3352 static BOOL WINAPI CRYPT_ImportKeyTrans(
3353  PCRYPT_ALGORITHM_IDENTIFIER pContentEncryptionAlgorithm,
3354  PCMSG_CTRL_KEY_TRANS_DECRYPT_PARA pKeyTransDecryptPara, DWORD dwFlags,
3355  void *pvReserved, HCRYPTKEY *phContentEncryptKey)
3356 {
3357     BOOL ret;
3358     HCRYPTKEY key;
3359
3360     ret = CryptGetUserKey(pKeyTransDecryptPara->hCryptProv,
3361      pKeyTransDecryptPara->dwKeySpec ? pKeyTransDecryptPara->dwKeySpec :
3362      AT_KEYEXCHANGE, &key);
3363     if (ret)
3364     {
3365         CMSG_KEY_TRANS_RECIPIENT_INFO *info =
3366          &pKeyTransDecryptPara->pKeyTrans[pKeyTransDecryptPara->dwRecipientIndex];
3367         CRYPT_DATA_BLOB *encryptedKey = &info->EncryptedKey;
3368         DWORD size = encryptedKey->cbData + sizeof(BLOBHEADER) + sizeof(ALG_ID);
3369         BYTE *keyBlob = CryptMemAlloc(size);
3370
3371         if (keyBlob)
3372         {
3373             DWORD i, k = size - 1;
3374             BLOBHEADER *blobHeader = (BLOBHEADER *)keyBlob;
3375             ALG_ID *algID = (ALG_ID *)(keyBlob + sizeof(BLOBHEADER));
3376
3377             blobHeader->bType = SIMPLEBLOB;
3378             blobHeader->bVersion = CUR_BLOB_VERSION;
3379             blobHeader->reserved = 0;
3380             blobHeader->aiKeyAlg = CertOIDToAlgId(
3381              pContentEncryptionAlgorithm->pszObjId);
3382             *algID = CertOIDToAlgId(info->KeyEncryptionAlgorithm.pszObjId);
3383             for (i = 0; i < encryptedKey->cbData; ++i, --k)
3384                 keyBlob[k] = encryptedKey->pbData[i];
3385
3386             ret = CryptImportKey(pKeyTransDecryptPara->hCryptProv, keyBlob,
3387              size, key, 0, phContentEncryptKey);
3388             CryptMemFree(keyBlob);
3389         }
3390         else
3391             ret = FALSE;
3392         CryptDestroyKey(key);
3393     }
3394     return ret;
3395 }
3396
3397 static BOOL CRYPT_ImportEncryptedKey(PCRYPT_ALGORITHM_IDENTIFIER contEncrAlg,
3398  PCMSG_CTRL_DECRYPT_PARA para, PCMSG_KEY_TRANS_RECIPIENT_INFO info,
3399  HCRYPTKEY *key)
3400 {
3401     static HCRYPTOIDFUNCSET set = NULL;
3402     PFN_CMSG_IMPORT_KEY_TRANS importKeyFunc = NULL;
3403     HCRYPTOIDFUNCADDR hFunc = NULL;
3404     CMSG_CTRL_KEY_TRANS_DECRYPT_PARA decryptPara;
3405     BOOL ret;
3406
3407     memset(&decryptPara, 0, sizeof(decryptPara));
3408     decryptPara.cbSize = sizeof(decryptPara);
3409     decryptPara.hCryptProv = para->hCryptProv;
3410     decryptPara.dwKeySpec = para->dwKeySpec;
3411     decryptPara.pKeyTrans = info;
3412     decryptPara.dwRecipientIndex = para->dwRecipientIndex;
3413
3414     if (!set)
3415         set = CryptInitOIDFunctionSet(CMSG_OID_IMPORT_KEY_TRANS_FUNC, 0);
3416     CryptGetOIDFunctionAddress(set, X509_ASN_ENCODING, contEncrAlg->pszObjId, 0,
3417      (void **)&importKeyFunc, &hFunc);
3418     if (!importKeyFunc)
3419         importKeyFunc = CRYPT_ImportKeyTrans;
3420     ret = importKeyFunc(contEncrAlg, &decryptPara, 0, NULL, key);
3421     if (hFunc)
3422         CryptFreeOIDFunctionAddress(hFunc, 0);
3423     return ret;
3424 }
3425
3426 static BOOL CDecodeEnvelopedMsg_CrtlDecrypt(CDecodeMsg *msg,
3427  PCMSG_CTRL_DECRYPT_PARA para)
3428 {
3429     BOOL ret = FALSE;
3430     CEnvelopedDecodeMsg *enveloped_data = &msg->u.enveloped_data;
3431     CRYPT_ENVELOPED_DATA *data = enveloped_data->data;
3432
3433     if (para->cbSize != sizeof(CMSG_CTRL_DECRYPT_PARA))
3434         SetLastError(E_INVALIDARG);
3435     else if (!data)
3436         SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3437     else if (para->dwRecipientIndex >= data->cRecipientInfo)
3438         SetLastError(CRYPT_E_INVALID_INDEX);
3439     else if (enveloped_data->decrypted)
3440         SetLastError(CRYPT_E_ALREADY_DECRYPTED);
3441     else if (!para->hCryptProv)
3442         SetLastError(ERROR_INVALID_PARAMETER);
3443     else if (enveloped_data->content.cbData)
3444     {
3445         HCRYPTKEY key;
3446
3447         ret = CRYPT_ImportEncryptedKey(
3448          &data->encryptedContentInfo.contentEncryptionAlgorithm, para,
3449          data->rgRecipientInfo, &key);
3450         if (ret)
3451         {
3452             ret = CryptDecrypt(key, 0, TRUE, 0, enveloped_data->content.pbData,
3453              &enveloped_data->content.cbData);
3454             CryptDestroyKey(key);
3455         }
3456     }
3457     else
3458         ret = TRUE;
3459     if (ret)
3460         enveloped_data->decrypted = TRUE;
3461     return ret;
3462 }
3463
3464 static BOOL CDecodeMsg_Control(HCRYPTMSG hCryptMsg, DWORD dwFlags,
3465  DWORD dwCtrlType, const void *pvCtrlPara)
3466 {
3467     CDecodeMsg *msg = hCryptMsg;
3468     BOOL ret = FALSE;
3469
3470     switch (dwCtrlType)
3471     {
3472     case CMSG_CTRL_VERIFY_SIGNATURE:
3473         switch (msg->type)
3474         {
3475         case CMSG_SIGNED:
3476             ret = CDecodeSignedMsg_VerifySignature(msg, (PCERT_INFO)pvCtrlPara);
3477             break;
3478         default:
3479             SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3480         }
3481         break;
3482     case CMSG_CTRL_DECRYPT:
3483         switch (msg->type)
3484         {
3485         case CMSG_ENVELOPED:
3486             ret = CDecodeEnvelopedMsg_CrtlDecrypt(msg,
3487              (PCMSG_CTRL_DECRYPT_PARA)pvCtrlPara);
3488             if (ret && (dwFlags & CMSG_CRYPT_RELEASE_CONTEXT_FLAG))
3489                 msg->u.enveloped_data.crypt_prov =
3490                  ((PCMSG_CTRL_DECRYPT_PARA)pvCtrlPara)->hCryptProv;
3491             break;
3492         default:
3493             SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3494         }
3495         break;
3496     case CMSG_CTRL_VERIFY_HASH:
3497         switch (msg->type)
3498         {
3499         case CMSG_HASHED:
3500             ret = CDecodeHashMsg_VerifyHash(msg);
3501             break;
3502         default:
3503             SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3504         }
3505         break;
3506     case CMSG_CTRL_VERIFY_SIGNATURE_EX:
3507         switch (msg->type)
3508         {
3509         case CMSG_SIGNED:
3510             ret = CDecodeSignedMsg_VerifySignatureEx(msg,
3511              (PCMSG_CTRL_VERIFY_SIGNATURE_EX_PARA)pvCtrlPara);
3512             break;
3513         default:
3514             SetLastError(CRYPT_E_INVALID_MSG_TYPE);
3515         }
3516         break;
3517     default:
3518         SetLastError(CRYPT_E_CONTROL_TYPE);
3519     }
3520     return ret;
3521 }
3522
3523 HCRYPTMSG WINAPI CryptMsgOpenToDecode(DWORD dwMsgEncodingType, DWORD dwFlags,
3524  DWORD dwMsgType, HCRYPTPROV_LEGACY hCryptProv, PCERT_INFO pRecipientInfo,
3525  PCMSG_STREAM_INFO pStreamInfo)
3526 {
3527     CDecodeMsg *msg;
3528
3529     TRACE("(%08x, %08x, %08x, %08lx, %p, %p)\n", dwMsgEncodingType,
3530      dwFlags, dwMsgType, hCryptProv, pRecipientInfo, pStreamInfo);
3531
3532     if (GET_CMSG_ENCODING_TYPE(dwMsgEncodingType) != PKCS_7_ASN_ENCODING)
3533     {
3534         SetLastError(E_INVALIDARG);
3535         return NULL;
3536     }
3537     msg = CryptMemAlloc(sizeof(CDecodeMsg));
3538     if (msg)
3539     {
3540         CryptMsgBase_Init((CryptMsgBase *)msg, dwFlags, pStreamInfo,
3541          CDecodeMsg_Close, CDecodeMsg_GetParam, CDecodeMsg_Update,
3542          CDecodeMsg_Control);
3543         msg->type = dwMsgType;
3544         if (hCryptProv)
3545             msg->crypt_prov = hCryptProv;
3546         else
3547         {
3548             msg->crypt_prov = CRYPT_GetDefaultProvider();
3549             msg->base.open_flags &= ~CMSG_CRYPT_RELEASE_CONTEXT_FLAG;
3550         }
3551         memset(&msg->u, 0, sizeof(msg->u));
3552         msg->msg_data.cbData = 0;
3553         msg->msg_data.pbData = NULL;
3554         msg->detached_data.cbData = 0;
3555         msg->detached_data.pbData = NULL;
3556         msg->properties = ContextPropertyList_Create();
3557     }
3558     return msg;
3559 }
3560
3561 HCRYPTMSG WINAPI CryptMsgDuplicate(HCRYPTMSG hCryptMsg)
3562 {
3563     TRACE("(%p)\n", hCryptMsg);
3564
3565     if (hCryptMsg)
3566     {
3567         CryptMsgBase *msg = hCryptMsg;
3568
3569         InterlockedIncrement(&msg->ref);
3570     }
3571     return hCryptMsg;
3572 }
3573
3574 BOOL WINAPI CryptMsgClose(HCRYPTMSG hCryptMsg)
3575 {
3576     TRACE("(%p)\n", hCryptMsg);
3577
3578     if (hCryptMsg)
3579     {
3580         CryptMsgBase *msg = hCryptMsg;
3581
3582         if (InterlockedDecrement(&msg->ref) == 0)
3583         {
3584             TRACE("freeing %p\n", msg);
3585             if (msg->close)
3586                 msg->close(msg);
3587             CryptMemFree(msg);
3588         }
3589     }
3590     return TRUE;
3591 }
3592
3593 BOOL WINAPI CryptMsgUpdate(HCRYPTMSG hCryptMsg, const BYTE *pbData,
3594  DWORD cbData, BOOL fFinal)
3595 {
3596     CryptMsgBase *msg = hCryptMsg;
3597
3598     TRACE("(%p, %p, %d, %d)\n", hCryptMsg, pbData, cbData, fFinal);
3599
3600     return msg->update(hCryptMsg, pbData, cbData, fFinal);
3601 }
3602
3603 BOOL WINAPI CryptMsgGetParam(HCRYPTMSG hCryptMsg, DWORD dwParamType,
3604  DWORD dwIndex, void *pvData, DWORD *pcbData)
3605 {
3606     CryptMsgBase *msg = hCryptMsg;
3607
3608     TRACE("(%p, %d, %d, %p, %p)\n", hCryptMsg, dwParamType, dwIndex,
3609      pvData, pcbData);
3610     return msg->get_param(hCryptMsg, dwParamType, dwIndex, pvData, pcbData);
3611 }
3612
3613 BOOL WINAPI CryptMsgControl(HCRYPTMSG hCryptMsg, DWORD dwFlags,
3614  DWORD dwCtrlType, const void *pvCtrlPara)
3615 {
3616     CryptMsgBase *msg = hCryptMsg;
3617
3618     TRACE("(%p, %08x, %d, %p)\n", hCryptMsg, dwFlags, dwCtrlType,
3619      pvCtrlPara);
3620     return msg->control(hCryptMsg, dwFlags, dwCtrlType, pvCtrlPara);
3621 }
3622
3623 static CERT_INFO *CRYPT_GetSignerCertInfoFromMsg(HCRYPTMSG msg,
3624  DWORD dwSignerIndex)
3625 {
3626     CERT_INFO *certInfo = NULL;
3627     DWORD size;
3628
3629     if (CryptMsgGetParam(msg, CMSG_SIGNER_CERT_INFO_PARAM, dwSignerIndex, NULL,
3630      &size))
3631     {
3632         certInfo = CryptMemAlloc(size);
3633         if (certInfo)
3634         {
3635             if (!CryptMsgGetParam(msg, CMSG_SIGNER_CERT_INFO_PARAM,
3636              dwSignerIndex, certInfo, &size))
3637             {
3638                 CryptMemFree(certInfo);
3639                 certInfo = NULL;
3640             }
3641         }
3642     }
3643     return certInfo;
3644 }
3645
3646 BOOL WINAPI CryptMsgGetAndVerifySigner(HCRYPTMSG hCryptMsg, DWORD cSignerStore,
3647  HCERTSTORE *rghSignerStore, DWORD dwFlags, PCCERT_CONTEXT *ppSigner,
3648  DWORD *pdwSignerIndex)
3649 {
3650     HCERTSTORE store;
3651     DWORD i, signerIndex = 0;
3652     PCCERT_CONTEXT signerCert = NULL;
3653     BOOL ret = FALSE;
3654
3655     TRACE("(%p, %d, %p, %08x, %p, %p)\n", hCryptMsg, cSignerStore,
3656      rghSignerStore, dwFlags, ppSigner, pdwSignerIndex);
3657
3658     /* Clear output parameters */
3659     if (ppSigner)
3660         *ppSigner = NULL;
3661     if (pdwSignerIndex && !(dwFlags & CMSG_USE_SIGNER_INDEX_FLAG))
3662         *pdwSignerIndex = 0;
3663
3664     /* Create store to search for signer certificates */
3665     store = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
3666      CERT_STORE_CREATE_NEW_FLAG, NULL);
3667     if (!(dwFlags & CMSG_TRUSTED_SIGNER_FLAG))
3668     {
3669         HCERTSTORE msgStore = CertOpenStore(CERT_STORE_PROV_MSG, 0, 0, 0,
3670          hCryptMsg);
3671
3672         CertAddStoreToCollection(store, msgStore, 0, 0);
3673         CertCloseStore(msgStore, 0);
3674     }
3675     for (i = 0; i < cSignerStore; i++)
3676         CertAddStoreToCollection(store, rghSignerStore[i], 0, 0);
3677
3678     /* Find signer cert */
3679     if (dwFlags & CMSG_USE_SIGNER_INDEX_FLAG)
3680     {
3681         CERT_INFO *signer = CRYPT_GetSignerCertInfoFromMsg(hCryptMsg,
3682          *pdwSignerIndex);
3683
3684         if (signer)
3685         {
3686             signerIndex = *pdwSignerIndex;
3687             signerCert = CertFindCertificateInStore(store, X509_ASN_ENCODING,
3688              0, CERT_FIND_SUBJECT_CERT, signer, NULL);
3689             CryptMemFree(signer);
3690         }
3691     }
3692     else
3693     {
3694         DWORD count, size = sizeof(count);
3695
3696         if (CryptMsgGetParam(hCryptMsg, CMSG_SIGNER_COUNT_PARAM, 0, &count,
3697          &size))
3698         {
3699             for (i = 0; !signerCert && i < count; i++)
3700             {
3701                 CERT_INFO *signer = CRYPT_GetSignerCertInfoFromMsg(hCryptMsg,
3702                  i);
3703
3704                 if (signer)
3705                 {
3706                     signerCert = CertFindCertificateInStore(store,
3707                      X509_ASN_ENCODING, 0, CERT_FIND_SUBJECT_CERT, signer,
3708                      NULL);
3709                     if (signerCert)
3710                         signerIndex = i;
3711                     CryptMemFree(signer);
3712                 }
3713             }
3714         }
3715         if (!signerCert)
3716             SetLastError(CRYPT_E_NO_TRUSTED_SIGNER);
3717     }
3718     if (signerCert)
3719     {
3720         if (!(dwFlags & CMSG_SIGNER_ONLY_FLAG))
3721             ret = CryptMsgControl(hCryptMsg, 0, CMSG_CTRL_VERIFY_SIGNATURE,
3722              signerCert->pCertInfo);
3723         else
3724             ret = TRUE;
3725         if (ret)
3726         {
3727             if (ppSigner)
3728                 *ppSigner = CertDuplicateCertificateContext(signerCert);
3729             if (pdwSignerIndex)
3730                 *pdwSignerIndex = signerIndex;
3731         }
3732         CertFreeCertificateContext(signerCert);
3733     }
3734
3735     CertCloseStore(store, 0);
3736     return ret;
3737 }
3738
3739 BOOL WINAPI CryptMsgVerifyCountersignatureEncodedEx(HCRYPTPROV_LEGACY hCryptProv,
3740  DWORD dwEncodingType, PBYTE pbSignerInfo, DWORD cbSignerInfo,
3741  PBYTE pbSignerInfoCountersignature, DWORD cbSignerInfoCountersignature,
3742  DWORD dwSignerType, void *pvSigner, DWORD dwFlags, void *pvReserved)
3743 {
3744     FIXME("(%08lx, %08x, %p, %d, %p, %d, %d, %p, %08x, %p): stub\n", hCryptProv,
3745      dwEncodingType, pbSignerInfo, cbSignerInfo, pbSignerInfoCountersignature,
3746      cbSignerInfoCountersignature, dwSignerType, pvSigner, dwFlags, pvReserved);
3747     return FALSE;
3748 }
3749
3750 BOOL WINAPI CryptMsgEncodeAndSignCTL(DWORD dwMsgEncodingType,
3751  PCTL_INFO pCtlInfo, PCMSG_SIGNED_ENCODE_INFO pSignInfo, DWORD dwFlags,
3752  BYTE *pbEncoded, DWORD *pcbEncoded)
3753 {
3754     BOOL ret;
3755     BYTE *pbCtlContent;
3756     DWORD cbCtlContent;
3757
3758     TRACE("(%08x, %p, %p, %08x, %p, %p)\n", dwMsgEncodingType, pCtlInfo,
3759      pSignInfo, dwFlags, pbEncoded, pcbEncoded);
3760
3761     if (dwFlags)
3762     {
3763         FIXME("unimplemented for flags %08x\n", dwFlags);
3764         return FALSE;
3765     }
3766     if ((ret = CryptEncodeObjectEx(dwMsgEncodingType, PKCS_CTL, pCtlInfo,
3767      CRYPT_ENCODE_ALLOC_FLAG, NULL, &pbCtlContent, &cbCtlContent)))
3768     {
3769         ret = CryptMsgSignCTL(dwMsgEncodingType, pbCtlContent, cbCtlContent,
3770          pSignInfo, dwFlags, pbEncoded, pcbEncoded);
3771         LocalFree(pbCtlContent);
3772     }
3773     return ret;
3774 }
3775
3776 BOOL WINAPI CryptMsgSignCTL(DWORD dwMsgEncodingType, BYTE *pbCtlContent,
3777  DWORD cbCtlContent, PCMSG_SIGNED_ENCODE_INFO pSignInfo, DWORD dwFlags,
3778  BYTE *pbEncoded, DWORD *pcbEncoded)
3779 {
3780     static char oid_ctl[] = szOID_CTL;
3781     BOOL ret;
3782     HCRYPTMSG msg;
3783
3784     TRACE("(%08x, %p, %d, %p, %08x, %p, %p)\n", dwMsgEncodingType,
3785      pbCtlContent, cbCtlContent, pSignInfo, dwFlags, pbEncoded, pcbEncoded);
3786
3787     if (dwFlags)
3788     {
3789         FIXME("unimplemented for flags %08x\n", dwFlags);
3790         return FALSE;
3791     }
3792     msg = CryptMsgOpenToEncode(dwMsgEncodingType, 0, CMSG_SIGNED, pSignInfo,
3793      oid_ctl, NULL);
3794     if (msg)
3795     {
3796         ret = CryptMsgUpdate(msg, pbCtlContent, cbCtlContent, TRUE);
3797         if (ret)
3798             ret = CryptMsgGetParam(msg, CMSG_CONTENT_PARAM, 0, pbEncoded,
3799              pcbEncoded);
3800         CryptMsgClose(msg);
3801     }
3802     else
3803         ret = FALSE;
3804     return ret;
3805 }