crypt32/tests: Test wildcards in subject alternative name.
[wine] / dlls / crypt32 / message.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 <stdarg.h>
20 #include "windef.h"
21 #include "winbase.h"
22 #include "wincrypt.h"
23
24 #include "wine/debug.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
27
28 HCERTSTORE WINAPI CryptGetMessageCertificates(DWORD dwMsgAndCertEncodingType,
29  HCRYPTPROV_LEGACY hCryptProv, DWORD dwFlags, const BYTE* pbSignedBlob,
30  DWORD cbSignedBlob)
31 {
32     CRYPT_DATA_BLOB blob = { cbSignedBlob, (LPBYTE)pbSignedBlob };
33
34     TRACE("(%08x, %ld, %d08x %p, %d)\n", dwMsgAndCertEncodingType, hCryptProv,
35      dwFlags, pbSignedBlob, cbSignedBlob);
36
37     return CertOpenStore(CERT_STORE_PROV_PKCS7, dwMsgAndCertEncodingType,
38      hCryptProv, dwFlags, &blob);
39 }
40
41 LONG WINAPI CryptGetMessageSignerCount(DWORD dwMsgEncodingType,
42  const BYTE *pbSignedBlob, DWORD cbSignedBlob)
43 {
44     HCRYPTMSG msg;
45     LONG count = -1;
46
47     TRACE("(%08x, %p, %d)\n", dwMsgEncodingType, pbSignedBlob, cbSignedBlob);
48
49     msg = CryptMsgOpenToDecode(dwMsgEncodingType, 0, 0, 0, NULL, NULL);
50     if (msg)
51     {
52         if (CryptMsgUpdate(msg, pbSignedBlob, cbSignedBlob, TRUE))
53         {
54             DWORD size = sizeof(count);
55
56             CryptMsgGetParam(msg, CMSG_SIGNER_COUNT_PARAM, 0, &count, &size);
57         }
58         CryptMsgClose(msg);
59     }
60     return count;
61 }
62
63 static CERT_INFO *CRYPT_GetSignerCertInfoFromMsg(HCRYPTMSG msg,
64  DWORD dwSignerIndex)
65 {
66     CERT_INFO *certInfo = NULL;
67     DWORD size;
68
69     if (CryptMsgGetParam(msg, CMSG_SIGNER_CERT_INFO_PARAM, dwSignerIndex, NULL,
70      &size))
71     {
72         certInfo = CryptMemAlloc(size);
73         if (certInfo)
74         {
75             if (!CryptMsgGetParam(msg, CMSG_SIGNER_CERT_INFO_PARAM,
76              dwSignerIndex, certInfo, &size))
77             {
78                 CryptMemFree(certInfo);
79                 certInfo = NULL;
80             }
81         }
82     }
83     else
84         SetLastError(CRYPT_E_UNEXPECTED_MSG_TYPE);
85     return certInfo;
86 }
87
88 static PCCERT_CONTEXT WINAPI CRYPT_DefaultGetSignerCertificate(void *pvGetArg,
89  DWORD dwCertEncodingType, PCERT_INFO pSignerId, HCERTSTORE hMsgCertStore)
90 {
91     return CertFindCertificateInStore(hMsgCertStore, dwCertEncodingType, 0,
92      CERT_FIND_SUBJECT_CERT, pSignerId, NULL);
93 }
94
95 static inline PCCERT_CONTEXT CRYPT_GetSignerCertificate(HCRYPTMSG msg,
96  PCRYPT_VERIFY_MESSAGE_PARA pVerifyPara, PCERT_INFO certInfo, HCERTSTORE store)
97 {
98     PFN_CRYPT_GET_SIGNER_CERTIFICATE getCert;
99
100     if (pVerifyPara->pfnGetSignerCertificate)
101         getCert = pVerifyPara->pfnGetSignerCertificate;
102     else
103         getCert = CRYPT_DefaultGetSignerCertificate;
104     return getCert(pVerifyPara->pvGetArg,
105      pVerifyPara->dwMsgAndCertEncodingType, certInfo, store);
106 }
107
108 BOOL WINAPI CryptVerifyDetachedMessageSignature(
109  PCRYPT_VERIFY_MESSAGE_PARA pVerifyPara, DWORD dwSignerIndex,
110  const BYTE *pbDetachedSignBlob, DWORD cbDetachedSignBlob, DWORD cToBeSigned,
111  const BYTE *rgpbToBeSigned[], DWORD rgcbToBeSigned[],
112  PCCERT_CONTEXT *ppSignerCert)
113 {
114     BOOL ret = FALSE;
115     HCRYPTMSG msg;
116
117     TRACE("(%p, %d, %p, %d, %d, %p, %p, %p)\n", pVerifyPara, dwSignerIndex,
118      pbDetachedSignBlob, cbDetachedSignBlob, cToBeSigned, rgpbToBeSigned,
119      rgcbToBeSigned, ppSignerCert);
120
121     if (ppSignerCert)
122         *ppSignerCert = NULL;
123     if (!pVerifyPara ||
124      pVerifyPara->cbSize != sizeof(CRYPT_VERIFY_MESSAGE_PARA) ||
125      GET_CMSG_ENCODING_TYPE(pVerifyPara->dwMsgAndCertEncodingType) !=
126      PKCS_7_ASN_ENCODING)
127     {
128         SetLastError(E_INVALIDARG);
129         return FALSE;
130     }
131
132     msg = CryptMsgOpenToDecode(pVerifyPara->dwMsgAndCertEncodingType,
133      CMSG_DETACHED_FLAG, 0, pVerifyPara->hCryptProv, NULL, NULL);
134     if (msg)
135     {
136         ret = CryptMsgUpdate(msg, pbDetachedSignBlob, cbDetachedSignBlob, TRUE);
137         if (ret)
138         {
139             DWORD i;
140
141             for (i = 0; ret && i < cToBeSigned; i++)
142                 ret = CryptMsgUpdate(msg, rgpbToBeSigned[i], rgcbToBeSigned[i],
143                  i == cToBeSigned - 1 ? TRUE : FALSE);
144         }
145         if (ret)
146         {
147             CERT_INFO *certInfo = CRYPT_GetSignerCertInfoFromMsg(msg,
148              dwSignerIndex);
149
150             ret = FALSE;
151             if (certInfo)
152             {
153                 HCERTSTORE store = CertOpenStore(CERT_STORE_PROV_MSG,
154                  pVerifyPara->dwMsgAndCertEncodingType,
155                  pVerifyPara->hCryptProv, 0, msg);
156
157                 if (store)
158                 {
159                     PCCERT_CONTEXT cert = CRYPT_GetSignerCertificate(
160                      msg, pVerifyPara, certInfo, store);
161
162                     if (cert)
163                     {
164                         ret = CryptMsgControl(msg, 0,
165                          CMSG_CTRL_VERIFY_SIGNATURE, cert->pCertInfo);
166                         if (ret && ppSignerCert)
167                             *ppSignerCert = cert;
168                         else
169                             CertFreeCertificateContext(cert);
170                     }
171                     else
172                         SetLastError(CRYPT_E_NOT_FOUND);
173                     CertCloseStore(store, 0);
174                 }
175                 CryptMemFree(certInfo);
176             }
177         }
178         CryptMsgClose(msg);
179     }
180     TRACE("returning %d\n", ret);
181     return ret;
182 }
183
184 BOOL WINAPI CryptVerifyMessageSignature(PCRYPT_VERIFY_MESSAGE_PARA pVerifyPara,
185  DWORD dwSignerIndex, const BYTE* pbSignedBlob, DWORD cbSignedBlob,
186  BYTE* pbDecoded, DWORD* pcbDecoded, PCCERT_CONTEXT* ppSignerCert)
187 {
188     BOOL ret = FALSE;
189     HCRYPTMSG msg;
190
191     TRACE("(%p, %d, %p, %d, %p, %p, %p)\n",
192      pVerifyPara, dwSignerIndex, pbSignedBlob, cbSignedBlob,
193      pbDecoded, pcbDecoded, ppSignerCert);
194
195     if (ppSignerCert)
196         *ppSignerCert = NULL;
197     if (pcbDecoded)
198         *pcbDecoded = 0;
199     if (!pVerifyPara ||
200      pVerifyPara->cbSize != sizeof(CRYPT_VERIFY_MESSAGE_PARA) ||
201      GET_CMSG_ENCODING_TYPE(pVerifyPara->dwMsgAndCertEncodingType) !=
202      PKCS_7_ASN_ENCODING)
203     {
204         SetLastError(E_INVALIDARG);
205         return FALSE;
206     }
207
208     msg = CryptMsgOpenToDecode(pVerifyPara->dwMsgAndCertEncodingType, 0, 0,
209      pVerifyPara->hCryptProv, NULL, NULL);
210     if (msg)
211     {
212         ret = CryptMsgUpdate(msg, pbSignedBlob, cbSignedBlob, TRUE);
213         if (ret && pcbDecoded)
214             ret = CryptMsgGetParam(msg, CMSG_CONTENT_PARAM, 0, pbDecoded,
215              pcbDecoded);
216         if (ret)
217         {
218             CERT_INFO *certInfo = CRYPT_GetSignerCertInfoFromMsg(msg,
219              dwSignerIndex);
220
221             ret = FALSE;
222             if (certInfo)
223             {
224                 HCERTSTORE store = CertOpenStore(CERT_STORE_PROV_MSG,
225                  pVerifyPara->dwMsgAndCertEncodingType,
226                  pVerifyPara->hCryptProv, 0, msg);
227
228                 if (store)
229                 {
230                     PCCERT_CONTEXT cert = CRYPT_GetSignerCertificate(
231                      msg, pVerifyPara, certInfo, store);
232
233                     if (cert)
234                     {
235                         ret = CryptMsgControl(msg, 0,
236                          CMSG_CTRL_VERIFY_SIGNATURE, cert->pCertInfo);
237                         if (ret && ppSignerCert)
238                             *ppSignerCert = cert;
239                         else
240                             CertFreeCertificateContext(cert);
241                     }
242                     CertCloseStore(store, 0);
243                 }
244             }
245             CryptMemFree(certInfo);
246         }
247         CryptMsgClose(msg);
248     }
249     TRACE("returning %d\n", ret);
250     return ret;
251 }
252
253 BOOL WINAPI CryptHashMessage(PCRYPT_HASH_MESSAGE_PARA pHashPara,
254  BOOL fDetachedHash, DWORD cToBeHashed, const BYTE *rgpbToBeHashed[],
255  DWORD rgcbToBeHashed[], BYTE *pbHashedBlob, DWORD *pcbHashedBlob,
256  BYTE *pbComputedHash, DWORD *pcbComputedHash)
257 {
258     DWORD i, flags;
259     BOOL ret = FALSE;
260     HCRYPTMSG msg;
261     CMSG_HASHED_ENCODE_INFO info;
262
263     TRACE("(%p, %d, %d, %p, %p, %p, %p, %p, %p)\n", pHashPara, fDetachedHash,
264      cToBeHashed, rgpbToBeHashed, rgcbToBeHashed, pbHashedBlob, pcbHashedBlob,
265      pbComputedHash, pcbComputedHash);
266
267     if (pHashPara->cbSize != sizeof(CRYPT_HASH_MESSAGE_PARA))
268     {
269         SetLastError(E_INVALIDARG);
270         return FALSE;
271     }
272     /* Native seems to ignore any encoding type other than the expected
273      * PKCS_7_ASN_ENCODING
274      */
275     if (GET_CMSG_ENCODING_TYPE(pHashPara->dwMsgEncodingType) !=
276      PKCS_7_ASN_ENCODING)
277         return TRUE;
278     /* Native also seems to do nothing if the output parameter isn't given */
279     if (!pcbHashedBlob)
280         return TRUE;
281
282     flags = fDetachedHash ? CMSG_DETACHED_FLAG : 0;
283     memset(&info, 0, sizeof(info));
284     info.cbSize = sizeof(info);
285     info.hCryptProv = pHashPara->hCryptProv;
286     memcpy(&info.HashAlgorithm, &pHashPara->HashAlgorithm,
287      sizeof(info.HashAlgorithm));
288     info.pvHashAuxInfo = pHashPara->pvHashAuxInfo;
289     msg = CryptMsgOpenToEncode(pHashPara->dwMsgEncodingType, flags, CMSG_HASHED,
290      &info, NULL, NULL);
291     if (msg)
292     {
293         for (i = 0, ret = TRUE; ret && i < cToBeHashed; i++)
294             ret = CryptMsgUpdate(msg, rgpbToBeHashed[i], rgcbToBeHashed[i],
295              i == cToBeHashed - 1 ? TRUE : FALSE);
296         if (ret)
297         {
298             ret = CryptMsgGetParam(msg, CMSG_CONTENT_PARAM, 0, pbHashedBlob,
299              pcbHashedBlob);
300             if (ret && pcbComputedHash)
301                 ret = CryptMsgGetParam(msg, CMSG_COMPUTED_HASH_PARAM, 0,
302                  pbComputedHash, pcbComputedHash);
303         }
304         CryptMsgClose(msg);
305     }
306     return ret;
307 }
308
309 BOOL WINAPI CryptVerifyDetachedMessageHash(PCRYPT_HASH_MESSAGE_PARA pHashPara,
310  BYTE *pbDetachedHashBlob, DWORD cbDetachedHashBlob, DWORD cToBeHashed,
311  const BYTE *rgpbToBeHashed[], DWORD rgcbToBeHashed[], BYTE *pbComputedHash,
312  DWORD *pcbComputedHash)
313 {
314     HCRYPTMSG msg;
315     BOOL ret = FALSE;
316
317     TRACE("(%p, %p, %d, %d, %p, %p, %p, %p)\n", pHashPara, pbDetachedHashBlob,
318      cbDetachedHashBlob, cToBeHashed, rgpbToBeHashed, rgcbToBeHashed,
319      pbComputedHash, pcbComputedHash);
320
321     if (pHashPara->cbSize != sizeof(CRYPT_HASH_MESSAGE_PARA))
322     {
323         SetLastError(E_INVALIDARG);
324         return FALSE;
325     }
326     if (GET_CMSG_ENCODING_TYPE(pHashPara->dwMsgEncodingType) !=
327      PKCS_7_ASN_ENCODING)
328     {
329         SetLastError(E_INVALIDARG);
330         return FALSE;
331     }
332     msg = CryptMsgOpenToDecode(pHashPara->dwMsgEncodingType, CMSG_DETACHED_FLAG,
333      0, pHashPara->hCryptProv, NULL, NULL);
334     if (msg)
335     {
336         DWORD i;
337
338         ret = CryptMsgUpdate(msg, pbDetachedHashBlob, cbDetachedHashBlob, TRUE);
339         if (ret)
340         {
341             if (cToBeHashed)
342             {
343                 for (i = 0; ret && i < cToBeHashed; i++)
344                 {
345                     ret = CryptMsgUpdate(msg, rgpbToBeHashed[i],
346                      rgcbToBeHashed[i], i == cToBeHashed - 1 ? TRUE : FALSE);
347                 }
348             }
349             else
350                 ret = CryptMsgUpdate(msg, NULL, 0, TRUE);
351         }
352         if (ret)
353         {
354             ret = CryptMsgControl(msg, 0, CMSG_CTRL_VERIFY_HASH, NULL);
355             if (ret && pcbComputedHash)
356                 ret = CryptMsgGetParam(msg, CMSG_COMPUTED_HASH_PARAM, 0,
357                  pbComputedHash, pcbComputedHash);
358         }
359         CryptMsgClose(msg);
360     }
361     return ret;
362 }
363
364 BOOL WINAPI CryptVerifyMessageHash(PCRYPT_HASH_MESSAGE_PARA pHashPara,
365  BYTE *pbHashedBlob, DWORD cbHashedBlob, BYTE *pbToBeHashed,
366  DWORD *pcbToBeHashed, BYTE *pbComputedHash, DWORD *pcbComputedHash)
367 {
368     HCRYPTMSG msg;
369     BOOL ret = FALSE;
370
371     TRACE("(%p, %p, %d, %p, %p, %p, %p)\n", pHashPara, pbHashedBlob,
372      cbHashedBlob, pbToBeHashed, pcbToBeHashed, pbComputedHash,
373      pcbComputedHash);
374
375     if (pHashPara->cbSize != sizeof(CRYPT_HASH_MESSAGE_PARA))
376     {
377         SetLastError(E_INVALIDARG);
378         return FALSE;
379     }
380     if (GET_CMSG_ENCODING_TYPE(pHashPara->dwMsgEncodingType) !=
381      PKCS_7_ASN_ENCODING)
382     {
383         SetLastError(E_INVALIDARG);
384         return FALSE;
385     }
386     msg = CryptMsgOpenToDecode(pHashPara->dwMsgEncodingType, 0, 0,
387      pHashPara->hCryptProv, NULL, NULL);
388     if (msg)
389     {
390         ret = CryptMsgUpdate(msg, pbHashedBlob, cbHashedBlob, TRUE);
391         if (ret)
392         {
393             ret = CryptMsgControl(msg, 0, CMSG_CTRL_VERIFY_HASH, NULL);
394             if (ret && pcbToBeHashed)
395                 ret = CryptMsgGetParam(msg, CMSG_CONTENT_PARAM, 0,
396                  pbToBeHashed, pcbToBeHashed);
397             if (ret && pcbComputedHash)
398                 ret = CryptMsgGetParam(msg, CMSG_COMPUTED_HASH_PARAM, 0,
399                  pbComputedHash, pcbComputedHash);
400         }
401         CryptMsgClose(msg);
402     }
403     return ret;
404 }