push 5b66f3b36e7323272378316c923222b908f6e2d3
[wine/hacks.git] / dlls / crypt32 / message.c
blobfe4d4ec1ce5278ab270b2214c7a87192c53d4412
1 /*
2 * Copyright 2007 Juan Lang
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <stdarg.h>
20 #include "windef.h"
21 #include "winbase.h"
22 #include "wincrypt.h"
24 #include "wine/debug.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
28 HCERTSTORE WINAPI CryptGetMessageCertificates(DWORD dwMsgAndCertEncodingType,
29 HCRYPTPROV_LEGACY hCryptProv, DWORD dwFlags, const BYTE* pbSignedBlob,
30 DWORD cbSignedBlob)
32 CRYPT_DATA_BLOB blob = { cbSignedBlob, (LPBYTE)pbSignedBlob };
34 TRACE("(%08x, %ld, %d08x %p, %d)\n", dwMsgAndCertEncodingType, hCryptProv,
35 dwFlags, pbSignedBlob, cbSignedBlob);
37 return CertOpenStore(CERT_STORE_PROV_PKCS7, dwMsgAndCertEncodingType,
38 hCryptProv, dwFlags, &blob);
41 LONG WINAPI CryptGetMessageSignerCount(DWORD dwMsgEncodingType,
42 const BYTE *pbSignedBlob, DWORD cbSignedBlob)
44 HCRYPTMSG msg;
45 LONG count = -1;
47 TRACE("(%08x, %p, %d)\n", dwMsgEncodingType, pbSignedBlob, cbSignedBlob);
49 msg = CryptMsgOpenToDecode(dwMsgEncodingType, 0, 0, 0, NULL, NULL);
50 if (msg)
52 if (CryptMsgUpdate(msg, pbSignedBlob, cbSignedBlob, TRUE))
54 DWORD size = sizeof(count);
56 CryptMsgGetParam(msg, CMSG_SIGNER_COUNT_PARAM, 0, &count, &size);
58 CryptMsgClose(msg);
60 return count;
63 static BOOL CRYPT_CopyParam(void *pvData, DWORD *pcbData, const void *src,
64 DWORD len)
66 BOOL ret = TRUE;
68 if (!pvData)
69 *pcbData = len;
70 else if (*pcbData < len)
72 *pcbData = len;
73 SetLastError(ERROR_MORE_DATA);
74 ret = FALSE;
76 else
78 *pcbData = len;
79 memcpy(pvData, src, len);
81 return ret;
84 static CERT_INFO *CRYPT_GetSignerCertInfoFromMsg(HCRYPTMSG msg,
85 DWORD dwSignerIndex)
87 CERT_INFO *certInfo = NULL;
88 DWORD size;
90 if (CryptMsgGetParam(msg, CMSG_SIGNER_CERT_INFO_PARAM, dwSignerIndex, NULL,
91 &size))
93 certInfo = CryptMemAlloc(size);
94 if (certInfo)
96 if (!CryptMsgGetParam(msg, CMSG_SIGNER_CERT_INFO_PARAM,
97 dwSignerIndex, certInfo, &size))
99 CryptMemFree(certInfo);
100 certInfo = NULL;
104 return certInfo;
107 static PCCERT_CONTEXT WINAPI CRYPT_DefaultGetSignerCertificate(void *pvGetArg,
108 DWORD dwCertEncodingType, PCERT_INFO pSignerId, HCERTSTORE hMsgCertStore)
110 return CertFindCertificateInStore(hMsgCertStore, dwCertEncodingType, 0,
111 CERT_FIND_SUBJECT_CERT, pSignerId, NULL);
114 static inline PCCERT_CONTEXT CRYPT_GetSignerCertificate(HCRYPTMSG msg,
115 PCRYPT_VERIFY_MESSAGE_PARA pVerifyPara, PCERT_INFO certInfo, HCERTSTORE store)
117 PFN_CRYPT_GET_SIGNER_CERTIFICATE getCert;
119 if (pVerifyPara->pfnGetSignerCertificate)
120 getCert = pVerifyPara->pfnGetSignerCertificate;
121 else
122 getCert = CRYPT_DefaultGetSignerCertificate;
123 return getCert(pVerifyPara->pvGetArg,
124 pVerifyPara->dwMsgAndCertEncodingType, certInfo, store);
127 BOOL WINAPI CryptVerifyMessageSignature(PCRYPT_VERIFY_MESSAGE_PARA pVerifyPara,
128 DWORD dwSignerIndex, const BYTE* pbSignedBlob, DWORD cbSignedBlob,
129 BYTE* pbDecoded, DWORD* pcbDecoded, PCCERT_CONTEXT* ppSignerCert)
131 BOOL ret = FALSE;
132 DWORD size;
133 CRYPT_CONTENT_INFO *contentInfo;
135 TRACE("(%p, %d, %p, %d, %p, %p, %p)\n",
136 pVerifyPara, dwSignerIndex, pbSignedBlob, cbSignedBlob,
137 pbDecoded, pcbDecoded, ppSignerCert);
139 if (ppSignerCert)
140 *ppSignerCert = NULL;
141 if (pcbDecoded)
142 *pcbDecoded = 0;
143 if (!pVerifyPara ||
144 pVerifyPara->cbSize != sizeof(CRYPT_VERIFY_MESSAGE_PARA) ||
145 GET_CMSG_ENCODING_TYPE(pVerifyPara->dwMsgAndCertEncodingType) !=
146 PKCS_7_ASN_ENCODING)
148 SetLastError(E_INVALIDARG);
149 return FALSE;
152 ret = CryptDecodeObjectEx(pVerifyPara->dwMsgAndCertEncodingType,
153 PKCS_CONTENT_INFO, pbSignedBlob, cbSignedBlob,
154 CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG, NULL,
155 (LPBYTE)&contentInfo, &size);
156 if (ret)
158 if (strcmp(contentInfo->pszObjId, szOID_RSA_signedData))
160 SetLastError(CRYPT_E_UNEXPECTED_MSG_TYPE);
161 ret = FALSE;
163 else
165 HCRYPTMSG msg = CryptMsgOpenToDecode(
166 pVerifyPara->dwMsgAndCertEncodingType, 0, CMSG_SIGNED,
167 pVerifyPara->hCryptProv, NULL, NULL);
169 if (msg)
171 ret = CryptMsgUpdate(msg, contentInfo->Content.pbData,
172 contentInfo->Content.cbData, TRUE);
173 if (ret && pcbDecoded)
174 ret = CRYPT_CopyParam(pbDecoded, pcbDecoded,
175 contentInfo->Content.pbData, contentInfo->Content.cbData);
176 if (ret)
178 CERT_INFO *certInfo = CRYPT_GetSignerCertInfoFromMsg(msg,
179 dwSignerIndex);
181 ret = FALSE;
182 if (certInfo)
184 HCERTSTORE store = CertOpenStore(CERT_STORE_PROV_MSG,
185 pVerifyPara->dwMsgAndCertEncodingType,
186 pVerifyPara->hCryptProv, 0, msg);
188 if (store)
190 PCCERT_CONTEXT cert = CRYPT_GetSignerCertificate(
191 msg, pVerifyPara, certInfo, store);
193 if (cert)
195 ret = CryptMsgControl(msg, 0,
196 CMSG_CTRL_VERIFY_SIGNATURE, cert->pCertInfo);
197 if (ret && ppSignerCert)
198 *ppSignerCert = cert;
199 else
200 CertFreeCertificateContext(cert);
202 CertCloseStore(store, 0);
205 CryptMemFree(certInfo);
207 CryptMsgClose(msg);
210 LocalFree(contentInfo);
212 TRACE("returning %d\n", ret);
213 return ret;
216 BOOL WINAPI CryptHashMessage(PCRYPT_HASH_MESSAGE_PARA pHashPara,
217 BOOL fDetachedHash, DWORD cToBeHashed, const BYTE *rgpbToBeHashed[],
218 DWORD rgcbToBeHashed[], BYTE *pbHashedBlob, DWORD *pcbHashedBlob,
219 BYTE *pbComputedHash, DWORD *pcbComputedHash)
221 DWORD i, flags;
222 BOOL ret = FALSE;
223 HCRYPTMSG msg;
224 CMSG_HASHED_ENCODE_INFO info;
226 TRACE("(%p, %d, %d, %p, %p, %p, %p, %p, %p)\n", pHashPara, fDetachedHash,
227 cToBeHashed, rgpbToBeHashed, rgcbToBeHashed, pbHashedBlob, pcbHashedBlob,
228 pbComputedHash, pcbComputedHash);
230 if (pHashPara->cbSize != sizeof(CRYPT_HASH_MESSAGE_PARA))
232 SetLastError(E_INVALIDARG);
233 return FALSE;
235 /* Native seems to ignore any encoding type other than the expected
236 * PKCS_7_ASN_ENCODING
238 if (GET_CMSG_ENCODING_TYPE(pHashPara->dwMsgEncodingType) !=
239 PKCS_7_ASN_ENCODING)
240 return TRUE;
241 /* Native also seems to do nothing if the output parameter isn't given */
242 if (!pcbHashedBlob)
243 return TRUE;
245 flags = fDetachedHash ? CMSG_DETACHED_FLAG : 0;
246 memset(&info, 0, sizeof(info));
247 info.cbSize = sizeof(info);
248 info.hCryptProv = pHashPara->hCryptProv;
249 memcpy(&info.HashAlgorithm, &pHashPara->HashAlgorithm,
250 sizeof(info.HashAlgorithm));
251 info.pvHashAuxInfo = pHashPara->pvHashAuxInfo;
252 msg = CryptMsgOpenToEncode(pHashPara->dwMsgEncodingType, flags, CMSG_HASHED,
253 &info, NULL, NULL);
254 if (msg)
256 for (i = 0, ret = TRUE; ret && i < cToBeHashed; i++)
257 ret = CryptMsgUpdate(msg, rgpbToBeHashed[i], rgcbToBeHashed[i],
258 i == cToBeHashed - 1 ? TRUE : FALSE);
259 if (ret)
261 ret = CryptMsgGetParam(msg, CMSG_CONTENT_PARAM, 0, pbHashedBlob,
262 pcbHashedBlob);
263 if (ret && pcbComputedHash)
264 ret = CryptMsgGetParam(msg, CMSG_COMPUTED_HASH_PARAM, 0,
265 pbComputedHash, pcbComputedHash);
267 CryptMsgClose(msg);
269 return ret;