crypt32: Implement CryptVerifyMessageHash.
[wine] / dlls / crypt32 / tests / message.c
1 /*
2  * Unit test suite for crypt32.dll's Crypt*Message functions
3  *
4  * Copyright 2007-2008 Juan Lang
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdio.h>
22 #include <stdarg.h>
23 #include <windef.h>
24 #include <winbase.h>
25 #include <winerror.h>
26 #include <wincrypt.h>
27
28 #include "wine/test.h"
29
30 static const BYTE dataEmptyBareContent[] = { 0x04,0x00 };
31 static const BYTE dataEmptyContent[] = {
32 0x30,0x0f,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x01,0xa0,0x02,
33 0x04,0x00 };
34 static const BYTE signedEmptyBareContent[] = {
35 0x30,0x50,0x02,0x01,0x01,0x31,0x0e,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,
36 0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x02,0x06,0x00,0x31,0x37,0x30,0x35,0x02,
37 0x01,0x01,0x30,0x1a,0x30,0x15,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,
38 0x13,0x0a,0x4a,0x75,0x61,0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x02,0x01,0x01,
39 0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,
40 0x04,0x06,0x00,0x05,0x00,0x04,0x00 };
41 static const BYTE signedEmptyContent[] = {
42 0x30,0x5f,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x02,0xa0,0x52,
43 0x30,0x50,0x02,0x01,0x01,0x31,0x0e,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,
44 0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x02,0x06,0x00,0x31,0x37,0x30,0x35,0x02,
45 0x01,0x01,0x30,0x1a,0x30,0x15,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,
46 0x13,0x0a,0x4a,0x75,0x61,0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x02,0x01,0x01,
47 0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,
48 0x04,0x06,0x00,0x05,0x00,0x04,0x00 };
49
50 static void test_msg_get_signer_count(void)
51 {
52     LONG count;
53
54     SetLastError(0xdeadbeef);
55     count = CryptGetMessageSignerCount(0, NULL, 0);
56     ok(count == -1, "Expected -1, got %d\n", count);
57     ok(GetLastError() == E_INVALIDARG, "Expected E_INVALIDARG, got %08x\n",
58      GetLastError());
59     SetLastError(0xdeadbeef);
60     count = CryptGetMessageSignerCount(PKCS_7_ASN_ENCODING, NULL, 0);
61     ok(count == -1, "Expected -1, got %d\n", count);
62     ok(GetLastError() == CRYPT_E_ASN1_EOD ||
63        GetLastError() == OSS_BAD_ARG, /* win9x */
64      "Expected CRYPT_E_ASN1_EOD, got %08x\n", GetLastError());
65     SetLastError(0xdeadbeef);
66     count = CryptGetMessageSignerCount(PKCS_7_ASN_ENCODING,
67      dataEmptyBareContent, sizeof(dataEmptyBareContent));
68     ok(count == -1, "Expected -1, got %d\n", count);
69     ok(GetLastError() == CRYPT_E_ASN1_BADTAG ||
70        GetLastError() == OSS_PDU_MISMATCH, /* win9x */
71      "Expected CRYPT_E_ASN1_BADTAG, got %08x\n", GetLastError());
72     SetLastError(0xdeadbeef);
73     count = CryptGetMessageSignerCount(PKCS_7_ASN_ENCODING,
74      dataEmptyContent, sizeof(dataEmptyContent));
75     ok(count == -1, "Expected -1, got %d\n", count);
76     ok(GetLastError() == CRYPT_E_INVALID_MSG_TYPE,
77      "Expected CRYPT_E_INVALID_MSG_TYPE, got %08x\n", GetLastError());
78     SetLastError(0xdeadbeef);
79     count = CryptGetMessageSignerCount(PKCS_7_ASN_ENCODING,
80      signedEmptyBareContent, sizeof(signedEmptyBareContent));
81     ok(count == -1, "Expected -1, got %d\n", count);
82     ok(GetLastError() == CRYPT_E_ASN1_BADTAG ||
83        GetLastError() == OSS_DATA_ERROR, /* win9x */
84      "Expected CRYPT_E_ASN1_BADTAG, got %08x\n", GetLastError());
85     count = CryptGetMessageSignerCount(PKCS_7_ASN_ENCODING,
86      signedEmptyContent, sizeof(signedEmptyContent));
87     ok(count == 1 ||
88        broken(count == -1), /* win9x */
89        "Expected 1, got %d\n", count);
90 }
91
92 static BYTE detachedHashContent[] = {
93 0x30,0x3f,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x05,0xa0,0x32,
94 0x30,0x30,0x02,0x01,0x00,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,
95 0x02,0x05,0x05,0x00,0x30,0x0b,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,
96 0x07,0x01,0x04,0x10,0x08,0xd6,0xc0,0x5a,0x21,0x51,0x2a,0x79,0xa1,0xdf,0xeb,
97 0x9d,0x2a,0x8f,0x26,0x2f };
98 static const BYTE msgData[] = { 1, 2, 3, 4 };
99
100 static void test_verify_detached_message_hash(void)
101 {
102     BOOL ret;
103     CRYPT_HASH_MESSAGE_PARA para;
104     DWORD size, hashSize;
105     const BYTE *pMsgData = msgData;
106     BYTE hash[16];
107
108     if (0)
109     {
110         ret = CryptVerifyDetachedMessageHash(NULL, NULL, 0, 0, NULL, NULL, NULL,
111          NULL);
112     }
113     memset(&para, 0, sizeof(para));
114     SetLastError(0xdeadbeef);
115     ret = CryptVerifyDetachedMessageHash(&para, NULL, 0, 0, NULL, NULL, NULL,
116      NULL);
117     ok(!ret && GetLastError() == E_INVALIDARG,
118      "expected E_INVALIDARG, got %08x\n", GetLastError());
119     para.cbSize = sizeof(para);
120     SetLastError(0xdeadbeef);
121     ret = CryptVerifyDetachedMessageHash(&para, NULL, 0, 0, NULL, NULL, NULL,
122      NULL);
123     ok(!ret && GetLastError() == E_INVALIDARG,
124      "expected E_INVALIDARG, got %08x\n", GetLastError());
125     para.dwMsgEncodingType = PKCS_7_ASN_ENCODING;
126     SetLastError(0xdeadbeef);
127     ret = CryptVerifyDetachedMessageHash(&para, NULL, 0, 0, NULL, NULL, NULL,
128      NULL);
129     ok(!ret &&
130      (GetLastError() == CRYPT_E_ASN1_EOD ||
131       GetLastError() == OSS_BAD_ARG), /* win9x */
132      "expected CRYPT_E_ASN1_EOD, got %08x\n", GetLastError());
133     para.dwMsgEncodingType = X509_ASN_ENCODING;
134     SetLastError(0xdeadbeef);
135     ret = CryptVerifyDetachedMessageHash(&para, NULL, 0, 0, NULL, NULL, NULL,
136      NULL);
137     ok(!ret && GetLastError() == E_INVALIDARG,
138      "expected E_INVALIDARG, got %08x\n", GetLastError());
139     para.dwMsgEncodingType = X509_ASN_ENCODING | PKCS_7_ASN_ENCODING;
140     SetLastError(0xdeadbeef);
141     ret = CryptVerifyDetachedMessageHash(&para, NULL, 0, 0, NULL, NULL, NULL,
142      NULL);
143     ok(!ret &&
144      (GetLastError() == CRYPT_E_ASN1_EOD ||
145       GetLastError() == OSS_BAD_ARG), /* win9x */
146      "expected CRYPT_E_ASN1_EOD, got %08x\n", GetLastError());
147     /* Curiously, passing no data to hash succeeds.. */
148     ret = CryptVerifyDetachedMessageHash(&para, detachedHashContent,
149      sizeof(detachedHashContent), 0, NULL, NULL, NULL, NULL);
150     todo_wine
151     ok(ret, "CryptVerifyDetachedMessageHash failed: %08x\n", GetLastError());
152     /* as does passing the actual content of the message to hash.. */
153     size = sizeof(msgData);
154     pMsgData = msgData;
155     ret = CryptVerifyDetachedMessageHash(&para, detachedHashContent,
156      sizeof(detachedHashContent), 1, &pMsgData, &size, NULL, NULL);
157     ok(ret, "CryptVerifyDetachedMessageHash failed: %08x\n", GetLastError());
158     /* while passing data to hash that isn't the content of the message fails.
159      */
160     size = sizeof(detachedHashContent);
161     pMsgData = detachedHashContent;
162     SetLastError(0xdeadbeef);
163     ret = CryptVerifyDetachedMessageHash(&para, detachedHashContent,
164      sizeof(detachedHashContent), 1, &pMsgData, &size, NULL, NULL);
165     ok(!ret && GetLastError() == CRYPT_E_HASH_VALUE,
166      "expected CRYPT_E_HASH_VALUE, got %08x\n", GetLastError());
167     /* Getting the size of the hash while passing no hash data causes the
168      * hash to be checked (and fail.)
169      */
170     SetLastError(0xdeadbeef);
171     ret = CryptVerifyDetachedMessageHash(&para, detachedHashContent,
172      sizeof(detachedHashContent), 0, NULL, NULL, NULL, &hashSize);
173     ok(!ret && GetLastError() == CRYPT_E_HASH_VALUE,
174      "expected CRYPT_E_HASH_VALUE, got %08x\n", GetLastError());
175     size = sizeof(msgData);
176     pMsgData = msgData;
177     ret = CryptVerifyDetachedMessageHash(&para, detachedHashContent,
178      sizeof(detachedHashContent), 1, &pMsgData, &size, NULL, &hashSize);
179     ok(ret, "CryptVerifyDetachedMessageHash failed: %08x\n", GetLastError());
180     ok(hashSize == sizeof(hash), "unexpected size %d\n", hashSize);
181     hashSize = 1;
182     SetLastError(0xdeadbeef);
183     ret = CryptVerifyDetachedMessageHash(&para, detachedHashContent,
184      sizeof(detachedHashContent), 1, &pMsgData, &size, hash, &hashSize);
185     ok(!ret && GetLastError() == ERROR_MORE_DATA,
186      "expected ERROR_MORE_DATA, got %08x\n", GetLastError());
187     hashSize = sizeof(hash);
188     ret = CryptVerifyDetachedMessageHash(&para, detachedHashContent,
189      sizeof(detachedHashContent), 1, &pMsgData, &size, hash, &hashSize);
190     ok(ret, "CryptVerifyDetachedMessageHash failed: %08x\n", GetLastError());
191 }
192
193 static BYTE hashContent[] = {
194 0x30,0x47,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x05,0xa0,0x3a,
195 0x30,0x38,0x02,0x01,0x00,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,
196 0x02,0x05,0x05,0x00,0x30,0x13,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,
197 0x07,0x01,0xa0,0x06,0x04,0x04,0x01,0x02,0x03,0x04,0x04,0x10,0x08,0xd6,0xc0,
198 0x5a,0x21,0x51,0x2a,0x79,0xa1,0xdf,0xeb,0x9d,0x2a,0x8f,0x26,0x2f };
199
200 static void test_verify_message_hash(void)
201 {
202     BOOL ret;
203     CRYPT_HASH_MESSAGE_PARA para;
204     DWORD size;
205     BYTE *buf = NULL;
206
207     memset(&para, 0, sizeof(para));
208     /* Crash */
209     if (0)
210         ret = CryptVerifyMessageHash(NULL, NULL, 0, NULL, NULL, NULL, NULL);
211     SetLastError(0xdeadbeef);
212     ret = CryptVerifyMessageHash(&para, NULL, 0, NULL, NULL, NULL, NULL);
213     ok(!ret && GetLastError() == E_INVALIDARG,
214      "expected E_INVALIDARG, got %08x\n", GetLastError());
215     para.cbSize = sizeof(para);
216     SetLastError(0xdeadbeef);
217     ret = CryptVerifyMessageHash(&para, NULL, 0, NULL, NULL, NULL, NULL);
218     ok(!ret && GetLastError() == E_INVALIDARG,
219      "expected E_INVALIDARG, got %08x\n", GetLastError());
220     para.dwMsgEncodingType = PKCS_7_ASN_ENCODING;
221     SetLastError(0xdeadbeef);
222     ret = CryptVerifyMessageHash(&para, NULL, 0, NULL, NULL, NULL, NULL);
223     ok(!ret && GetLastError() == CRYPT_E_ASN1_EOD,
224      "expected CRYPT_E_ASN1_EOD, got %08x\n", GetLastError());
225     /* Verifying the hash of a detached message succeeds? */
226     ret = CryptVerifyMessageHash(&para, detachedHashContent,
227      sizeof(detachedHashContent), NULL, NULL, NULL, NULL);
228     todo_wine
229     ok(ret, "CryptVerifyMessageHash failed: %08x\n", GetLastError());
230     /* As does verifying the hash of a regular message. */
231     ret = CryptVerifyMessageHash(&para, hashContent, sizeof(hashContent),
232      NULL, NULL, NULL, NULL);
233     ok(ret, "CryptVerifyMessageHash failed: %08x\n", GetLastError());
234     ret = CryptVerifyMessageHash(&para, hashContent, sizeof(hashContent),
235      NULL, &size, NULL, NULL);
236     ok(ret, "CryptVerifyMessageHash failed: %08x\n", GetLastError());
237     if (ret)
238         buf = CryptMemAlloc(size);
239     if (buf)
240     {
241         size = 1;
242         ret = CryptVerifyMessageHash(&para, hashContent, sizeof(hashContent),
243          buf, &size, NULL, NULL);
244         ok(!ret && GetLastError() == ERROR_MORE_DATA,
245          "expected ERROR_MORE_DATA, got %08x\n", GetLastError());
246         ret = CryptVerifyMessageHash(&para, hashContent, sizeof(hashContent),
247          buf, &size, NULL, NULL);
248         ok(ret, "CryptVerifyMessageHash failed: %08x\n", GetLastError());
249         ok(size == sizeof(msgData), "unexpected size %d\n", size);
250         ok(!memcmp(buf, msgData, size), "unexpected value\n");
251         CryptMemFree(buf);
252     }
253 }
254
255 static const BYTE signedContent[] = {
256 0x30,0x81,0xb2,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x02,0xa0,
257 0x81,0xa4,0x30,0x81,0xa1,0x02,0x01,0x01,0x31,0x0e,0x30,0x0c,0x06,0x08,0x2a,
258 0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x13,0x06,0x09,0x2a,0x86,
259 0x48,0x86,0xf7,0x0d,0x01,0x07,0x01,0xa0,0x06,0x04,0x04,0x01,0x02,0x03,0x04,
260 0x31,0x77,0x30,0x75,0x02,0x01,0x01,0x30,0x1a,0x30,0x15,0x31,0x13,0x30,0x11,
261 0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,0x20,0x4c,0x61,0x6e,
262 0x67,0x00,0x02,0x01,0x01,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,
263 0x02,0x05,0x05,0x00,0x30,0x04,0x06,0x00,0x05,0x00,0x04,0x40,0x81,0xa6,0x70,
264 0xb3,0xef,0x59,0xd1,0x66,0xd1,0x9b,0xc0,0x9a,0xb6,0x9a,0x5e,0x6d,0x6f,0x6d,
265 0x0d,0x59,0xa9,0xaa,0x6e,0xe9,0x2c,0xa0,0x1e,0xee,0xc2,0x60,0xbc,0x59,0xbe,
266 0x3f,0x63,0x06,0x8d,0xc9,0x11,0x1d,0x23,0x64,0x92,0xef,0x2e,0xfc,0x57,0x29,
267 0xa4,0xaf,0xe0,0xee,0x93,0x19,0x39,0x51,0xe4,0x44,0xb8,0x0b,0x28,0xf4,0xa8,
268 0x0d };
269 static const BYTE signedWithCertEmptyContent[] = {
270 0x30,0x81,0xdf,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x02,0xa0,
271 0x81,0xd1,0x30,0x81,0xce,0x02,0x01,0x01,0x31,0x0e,0x30,0x0c,0x06,0x08,0x2a,
272 0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x02,0x06,0x00,0xa0,0x7c,
273 0x30,0x7a,0x02,0x01,0x01,0x30,0x02,0x06,0x00,0x30,0x15,0x31,0x13,0x30,0x11,
274 0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,0x20,0x4c,0x61,0x6e,
275 0x67,0x00,0x30,0x22,0x18,0x0f,0x31,0x36,0x30,0x31,0x30,0x31,0x30,0x31,0x30,
276 0x30,0x30,0x30,0x30,0x30,0x5a,0x18,0x0f,0x31,0x36,0x30,0x31,0x30,0x31,0x30,
277 0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x30,0x15,0x31,0x13,0x30,0x11,0x06,
278 0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,0x20,0x4c,0x61,0x6e,0x67,
279 0x00,0x30,0x07,0x30,0x02,0x06,0x00,0x03,0x01,0x00,0xa3,0x16,0x30,0x14,0x30,
280 0x12,0x06,0x03,0x55,0x1d,0x13,0x01,0x01,0xff,0x04,0x08,0x30,0x06,0x01,0x01,
281 0xff,0x02,0x01,0x01,0x31,0x37,0x30,0x35,0x02,0x01,0x01,0x30,0x1a,0x30,0x15,
282 0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,
283 0x20,0x4c,0x61,0x6e,0x67,0x00,0x02,0x01,0x01,0x30,0x0c,0x06,0x08,0x2a,0x86,
284 0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x04,0x06,0x00,0x05,0x00,0x04,
285 0x00 };
286 static const BYTE signedWithCertContent[] = {
287 0x30,0x82,0x01,0x32,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x02,
288 0xa0,0x82,0x01,0x23,0x30,0x82,0x01,0x1f,0x02,0x01,0x01,0x31,0x0e,0x30,0x0c,
289 0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x13,0x06,
290 0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x01,0xa0,0x06,0x04,0x04,0x01,
291 0x02,0x03,0x04,0xa0,0x7c,0x30,0x7a,0x02,0x01,0x01,0x30,0x02,0x06,0x00,0x30,
292 0x15,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,
293 0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x30,0x22,0x18,0x0f,0x31,0x36,0x30,0x31,
294 0x30,0x31,0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x18,0x0f,0x31,0x36,
295 0x30,0x31,0x30,0x31,0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x30,0x15,
296 0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,
297 0x20,0x4c,0x61,0x6e,0x67,0x00,0x30,0x07,0x30,0x02,0x06,0x00,0x03,0x01,0x00,
298 0xa3,0x16,0x30,0x14,0x30,0x12,0x06,0x03,0x55,0x1d,0x13,0x01,0x01,0xff,0x04,
299 0x08,0x30,0x06,0x01,0x01,0xff,0x02,0x01,0x01,0x31,0x77,0x30,0x75,0x02,0x01,
300 0x01,0x30,0x1a,0x30,0x15,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,
301 0x0a,0x4a,0x75,0x61,0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x02,0x01,0x01,0x30,
302 0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x04,
303 0x06,0x00,0x05,0x00,0x04,0x40,0x81,0xa6,0x70,0xb3,0xef,0x59,0xd1,0x66,0xd1,
304 0x9b,0xc0,0x9a,0xb6,0x9a,0x5e,0x6d,0x6f,0x6d,0x0d,0x59,0xa9,0xaa,0x6e,0xe9,
305 0x2c,0xa0,0x1e,0xee,0xc2,0x60,0xbc,0x59,0xbe,0x3f,0x63,0x06,0x8d,0xc9,0x11,
306 0x1d,0x23,0x64,0x92,0xef,0x2e,0xfc,0x57,0x29,0xa4,0xaf,0xe0,0xee,0x93,0x19,
307 0x39,0x51,0xe4,0x44,0xb8,0x0b,0x28,0xf4,0xa8,0x0d };
308 static const BYTE signedWithCertWithPubKeyContent[] = {
309 0x30,0x81,0xfc,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x02,0xa0,
310 0x81,0xee,0x30,0x81,0xeb,0x02,0x01,0x01,0x31,0x0e,0x30,0x0c,0x06,0x08,0x2a,
311 0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x02,0x06,0x00,0xa0,0x81,
312 0x98,0x30,0x81,0x95,0x02,0x01,0x01,0x30,0x02,0x06,0x00,0x30,0x15,0x31,0x13,
313 0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,0x20,0x4c,
314 0x61,0x6e,0x67,0x00,0x30,0x22,0x18,0x0f,0x31,0x36,0x30,0x31,0x30,0x31,0x30,
315 0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x18,0x0f,0x31,0x36,0x30,0x31,0x30,
316 0x31,0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x30,0x15,0x31,0x13,0x30,
317 0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,0x20,0x4c,0x61,
318 0x6e,0x67,0x00,0x30,0x22,0x30,0x0d,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,
319 0x01,0x01,0x01,0x05,0x00,0x03,0x11,0x00,0x00,0x01,0x02,0x03,0x04,0x05,0x06,
320 0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0xa3,0x16,0x30,0x14,0x30,0x12,
321 0x06,0x03,0x55,0x1d,0x13,0x01,0x01,0xff,0x04,0x08,0x30,0x06,0x01,0x01,0xff,
322 0x02,0x01,0x01,0x31,0x37,0x30,0x35,0x02,0x01,0x01,0x30,0x1a,0x30,0x15,0x31,
323 0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,0x20,
324 0x4c,0x61,0x6e,0x67,0x00,0x02,0x01,0x01,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,
325 0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x04,0x06,0x00,0x05,0x00,0x04,0x00 };
326
327 static void test_verify_message_signature(void)
328 {
329     BOOL ret;
330     CRYPT_VERIFY_MESSAGE_PARA para = { 0 };
331     PCCERT_CONTEXT cert;
332     DWORD cbDecoded;
333
334     SetLastError(0xdeadbeef);
335     ret = CryptVerifyMessageSignature(NULL, 0, NULL, 0, NULL, 0, NULL);
336     ok(!ret && GetLastError() == E_INVALIDARG,
337      "Expected E_INVALIDARG, got %08x\n", GetLastError());
338     SetLastError(0xdeadbeef);
339     ret = CryptVerifyMessageSignature(&para, 0, NULL, 0, NULL, 0, NULL);
340     ok(!ret && GetLastError() == E_INVALIDARG,
341      "Expected E_INVALIDARG, got %08x\n", GetLastError());
342     para.cbSize = sizeof(para);
343     SetLastError(0xdeadbeef);
344     ret = CryptVerifyMessageSignature(&para, 0, NULL, 0, NULL, 0, NULL);
345     ok(!ret && GetLastError() == E_INVALIDARG,
346      "Expected E_INVALIDARG, got %08x\n", GetLastError());
347     para.cbSize = 0;
348     para.dwMsgAndCertEncodingType = PKCS_7_ASN_ENCODING;
349     SetLastError(0xdeadbeef);
350     ret = CryptVerifyMessageSignature(&para, 0, NULL, 0, NULL, 0, NULL);
351     ok(!ret && GetLastError() == E_INVALIDARG,
352      "Expected E_INVALIDARG, got %08x\n", GetLastError());
353     para.cbSize = sizeof(para);
354     SetLastError(0xdeadbeef);
355     ret = CryptVerifyMessageSignature(&para, 0, NULL, 0, NULL, 0, NULL);
356     ok(!ret &&
357      (GetLastError() == CRYPT_E_ASN1_EOD ||
358       GetLastError() == OSS_BAD_ARG), /* win9x */
359      "Expected CRYPT_E_ASN1_EOD, got %08x\n", GetLastError());
360     /* Check whether cert is set on error */
361     cert = (PCCERT_CONTEXT)0xdeadbeef;
362     ret = CryptVerifyMessageSignature(&para, 0, NULL, 0, NULL, 0, &cert);
363     ok(cert == NULL, "Expected NULL cert\n");
364     /* Check whether cbDecoded is set on error */
365     cbDecoded = 0xdeadbeef;
366     ret = CryptVerifyMessageSignature(&para, 0, NULL, 0, NULL, &cbDecoded,
367      NULL);
368     ok(!cbDecoded, "Expected 0\n");
369     SetLastError(0xdeadbeef);
370     ret = CryptVerifyMessageSignature(&para, 0, dataEmptyBareContent,
371      sizeof(dataEmptyBareContent), NULL, 0, NULL);
372     ok(GetLastError() == CRYPT_E_ASN1_BADTAG ||
373      GetLastError() == OSS_PDU_MISMATCH, /* win9x */
374      "Expected CRYPT_E_ASN1_BADTAG, got %08x\n", GetLastError());
375     SetLastError(0xdeadbeef);
376     ret = CryptVerifyMessageSignature(&para, 0, dataEmptyContent,
377      sizeof(dataEmptyContent), NULL, 0, NULL);
378     ok(!ret && GetLastError() == CRYPT_E_UNEXPECTED_MSG_TYPE,
379      "Expected CRYPT_E_UNEXPECTED_MSG_TYPE, got %08x\n", GetLastError());
380     SetLastError(0xdeadbeef);
381     ret = CryptVerifyMessageSignature(&para, 0, signedEmptyBareContent,
382      sizeof(signedEmptyBareContent), NULL, 0, NULL);
383     ok(!ret &&
384      (GetLastError() == CRYPT_E_ASN1_BADTAG ||
385       GetLastError() == OSS_DATA_ERROR), /* win9x */
386      "Expected CRYPT_E_ASN1_BADTAG, got %08x\n", GetLastError());
387     SetLastError(0xdeadbeef);
388     ret = CryptVerifyMessageSignature(&para, 0, signedEmptyContent,
389      sizeof(signedEmptyContent), NULL, 0, NULL);
390     ok(!ret &&
391      (GetLastError() == CRYPT_E_NOT_FOUND ||
392       GetLastError() == OSS_DATA_ERROR), /* win9x */
393      "Expected CRYPT_E_NOT_FOUND, got %08x\n", GetLastError());
394     SetLastError(0xdeadbeef);
395     ret = CryptVerifyMessageSignature(&para, 0, signedContent,
396      sizeof(signedContent), NULL, 0, NULL);
397     ok(!ret &&
398      (GetLastError() == CRYPT_E_NOT_FOUND ||
399       GetLastError() == OSS_DATA_ERROR), /* win9x */
400      "Expected CRYPT_E_NOT_FOUND, got %08x\n", GetLastError());
401     /* FIXME: Windows fails with CRYPT_E_NOT_FOUND for these messages, but
402      * their signer certs have invalid public keys that fail to decode.  In
403      * Wine therefore the failure is an ASN error.  Need some messages with
404      * valid public keys and invalid signatures to check against.
405      */
406     ret = CryptVerifyMessageSignature(&para, 0, signedWithCertEmptyContent,
407      sizeof(signedWithCertEmptyContent), NULL, 0, NULL);
408     ok(!ret, "Expected failure\n");
409     ret = CryptVerifyMessageSignature(&para, 0, signedWithCertContent,
410      sizeof(signedWithCertContent), NULL, 0, NULL);
411     ok(!ret, "Expected failure\n");
412     ret = CryptVerifyMessageSignature(&para, 0, signedWithCertWithPubKeyContent,
413      sizeof(signedWithCertWithPubKeyContent), NULL, 0, NULL);
414     ok(!ret, "Expected failure\n");
415 }
416
417 static const BYTE detachedHashBlob[] = {
418 0x30,0x3f,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x05,0xa0,0x32,
419 0x30,0x30,0x02,0x01,0x00,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,
420 0x02,0x05,0x05,0x00,0x30,0x0b,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,
421 0x07,0x01,0x04,0x10,0x2d,0x1b,0xbc,0x1f,0xc7,0xab,0x36,0x8d,0xdb,0x95,0xe6,
422 0x24,0xb9,0x66,0x7c,0x21 };
423 static const BYTE hashBlob[] = {
424 0x30,0x47,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x05,0xa0,0x3a,
425 0x30,0x38,0x02,0x01,0x00,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,
426 0x02,0x05,0x05,0x00,0x30,0x13,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,
427 0x07,0x01,0xa0,0x06,0x04,0x04,0xde,0xad,0xbe,0xef,0x04,0x10,0x2f,0x24,0x92,
428 0x30,0xa8,0xe7,0xc2,0xbf,0x60,0x05,0xcc,0xd2,0x67,0x92,0x59,0xec };
429 static const BYTE hashVal[] = {
430 0x2d,0x1b,0xbc,0x1f,0xc7,0xab,0x36,0x8d,0xdb,0x95,0xe6,0x24,0xb9,0x66,0x7c,
431 0x21 };
432
433 static void test_hash_message(void)
434 {
435     BOOL ret;
436     CRYPT_HASH_MESSAGE_PARA para;
437     static const BYTE blob1[] = { 0xde, 0xad, 0xbe, 0xef };
438     static const BYTE blob2[] = { 0xba, 0xad, 0xf0, 0x0d };
439     const BYTE *toHash[] = { blob1, blob2 };
440     DWORD hashSize[] = { sizeof(blob1), sizeof(blob2) };
441     DWORD hashedBlobSize, computedHashSize;
442     static char oid_rsa_md5[] = szOID_RSA_MD5;
443     LPBYTE hashedBlob, computedHash;
444
445     /* Crash
446     ret = CryptHashMessage(NULL, FALSE, 0, NULL, 0, NULL, NULL, NULL, NULL);
447      */
448     memset(&para, 0, sizeof(para));
449     SetLastError(0xdeadbeef);
450     ret = CryptHashMessage(&para, FALSE, 0, NULL, NULL, NULL, NULL, NULL, NULL);
451     ok(!ret && GetLastError() == E_INVALIDARG,
452      "expected E_INVALIDARG, got 0x%08x\n", GetLastError());
453     para.cbSize = sizeof(para);
454     /* Not quite sure what "success" means in this case, but it does succeed */
455     SetLastError(0xdeadbeef);
456     ret = CryptHashMessage(&para, FALSE, 0, NULL, NULL, NULL, NULL, NULL, NULL);
457     ok(ret, "CryptHashMessage failed: 0x%08x\n", GetLastError());
458     /* With a bogus encoding type it "succeeds" */
459     para.dwMsgEncodingType = 0xdeadbeef;
460     SetLastError(0xdeadbeef);
461     ret = CryptHashMessage(&para, FALSE, 0, NULL, NULL, NULL, NULL, NULL, NULL);
462     ok(ret, "CryptHashMessage failed: 0x%08x\n", GetLastError());
463     /* According to MSDN, the third parameter (cToBeHashed) must be 1 if the
464      * second parameter (fDetached) is FALSE, but again it "succeeds."
465      */
466     SetLastError(0xdeadbeef);
467     ret = CryptHashMessage(&para, FALSE, 2, NULL, NULL, NULL, NULL, NULL, NULL);
468     ok(ret, "CryptHashMessage failed: 0x%08x\n", GetLastError());
469     /* Even passing parameters to hash results in "success." */
470     SetLastError(0xdeadbeef);
471     ret = CryptHashMessage(&para, FALSE, 2, toHash, hashSize, NULL, NULL, NULL,
472      NULL);
473     /* Try again with a valid encoding type */
474     para.dwMsgEncodingType = PKCS_7_ASN_ENCODING;
475     SetLastError(0xdeadbeef);
476     ret = CryptHashMessage(&para, FALSE, 2, NULL, NULL, NULL, NULL, NULL, NULL);
477     ok(ret, "CryptHashMessage failed: 0x%08x\n", GetLastError());
478     /* And with valid data to hash */
479     SetLastError(0xdeadbeef);
480     ret = CryptHashMessage(&para, FALSE, 2, toHash, hashSize, NULL, NULL, NULL,
481      NULL);
482     ok(ret, "CryptHashMessage failed: 0x%08x\n", GetLastError());
483     /* But requesting the size of the hashed blob and indicating there's data
484      * to hash results in a crash
485      */
486     if (0)
487     {
488         ret = CryptHashMessage(&para, FALSE, 2, NULL, NULL, NULL,
489          &hashedBlobSize, NULL, NULL);
490     }
491     /* Passing a valid pointer for the data to hash fails, as the hash
492      * algorithm is finally checked.
493      */
494     SetLastError(0xdeadbeef);
495     ret = CryptHashMessage(&para, FALSE, 2, toHash, hashSize, NULL,
496      &hashedBlobSize, NULL, NULL);
497     ok(!ret &&
498      (GetLastError() == CRYPT_E_UNKNOWN_ALGO ||
499       GetLastError() == CRYPT_E_OID_FORMAT), /* Vista */
500      "expected CRYPT_E_UNKNOWN_ALGO or CRYPT_E_OID_FORMAT, got 0x%08x (%d)\n",
501      GetLastError(), GetLastError());
502     para.HashAlgorithm.pszObjId = oid_rsa_md5;
503     /* With a valid hash algorithm, this succeeds, even though fDetached is
504      * FALSE.
505      */
506     SetLastError(0xdeadbeef);
507     ret = CryptHashMessage(&para, FALSE, 2, toHash, hashSize, NULL,
508      &hashedBlobSize, NULL, NULL);
509     todo_wine
510     ok(ret, "CryptHashMessage failed: 0x%08x\n", GetLastError());
511     if (ret)
512     {
513         /* Actually attempting to get the hashed data fails, perhaps because
514          * detached is FALSE.
515          */
516         hashedBlob = HeapAlloc(GetProcessHeap(), 0, hashedBlobSize);
517         SetLastError(0xdeadbeef);
518         ret = CryptHashMessage(&para, FALSE, 2, toHash, hashSize, hashedBlob,
519          &hashedBlobSize, NULL, NULL);
520         ok(!ret && GetLastError() == CRYPT_E_MSG_ERROR,
521          "expected CRYPT_E_MSG_ERROR, got 0x%08x (%d)\n", GetLastError(),
522          GetLastError());
523         HeapFree(GetProcessHeap(), 0, hashedBlob);
524     }
525     /* Repeating tests with fDetached = TRUE results in success */
526     SetLastError(0xdeadbeef);
527     ret = CryptHashMessage(&para, TRUE, 2, toHash, hashSize, NULL,
528      &hashedBlobSize, NULL, NULL);
529     ok(ret, "CryptHashMessage failed: 0x%08x\n", GetLastError());
530     if (ret)
531     {
532         hashedBlob = HeapAlloc(GetProcessHeap(), 0, hashedBlobSize);
533         SetLastError(0xdeadbeef);
534         ret = CryptHashMessage(&para, TRUE, 2, toHash, hashSize, hashedBlob,
535          &hashedBlobSize, NULL, NULL);
536         ok(ret, "CryptHashMessage failed: 0x%08x\n", GetLastError());
537         ok(hashedBlobSize == sizeof(detachedHashBlob),
538          "unexpected size of detached blob %d\n", hashedBlobSize);
539         ok(!memcmp(hashedBlob, detachedHashBlob, hashedBlobSize),
540          "unexpected detached blob value\n");
541         HeapFree(GetProcessHeap(), 0, hashedBlob);
542     }
543     /* Hashing a single item with fDetached = FALSE also succeeds */
544     SetLastError(0xdeadbeef);
545     ret = CryptHashMessage(&para, FALSE, 1, toHash, hashSize, NULL,
546      &hashedBlobSize, NULL, NULL);
547     ok(ret, "CryptHashMessage failed: 0x%08x\n", GetLastError());
548     if (ret)
549     {
550         hashedBlob = HeapAlloc(GetProcessHeap(), 0, hashedBlobSize);
551         ret = CryptHashMessage(&para, FALSE, 1, toHash, hashSize, hashedBlob,
552          &hashedBlobSize, NULL, NULL);
553         ok(ret, "CryptHashMessage failed: 0x%08x\n", GetLastError());
554         ok(hashedBlobSize == sizeof(hashBlob),
555          "unexpected size of detached blob %d\n", hashedBlobSize);
556         ok(!memcmp(hashedBlob, hashBlob, hashedBlobSize),
557          "unexpected detached blob value\n");
558         HeapFree(GetProcessHeap(), 0, hashedBlob);
559     }
560     /* Check the computed hash value too.  You don't need to get the encoded
561      * blob to get it.
562      */
563     computedHashSize = 0xdeadbeef;
564     ret = CryptHashMessage(&para, TRUE, 2, toHash, hashSize, NULL,
565      &hashedBlobSize, NULL, &computedHashSize);
566     ok(ret, "CryptHashMessage failed: 0x%08x\n", GetLastError());
567     ok(computedHashSize == 16, "expected hash size of 16, got %d\n",
568      computedHashSize);
569     if (ret)
570     {
571         computedHash = HeapAlloc(GetProcessHeap(), 0, computedHashSize);
572         SetLastError(0xdeadbeef);
573         ret = CryptHashMessage(&para, TRUE, 2, toHash, hashSize, NULL,
574          &hashedBlobSize, computedHash, &computedHashSize);
575         ok(computedHashSize == sizeof(hashVal),
576          "unexpected size of hash value %d\n", computedHashSize);
577         ok(!memcmp(computedHash, hashVal, computedHashSize),
578          "unexpected value\n");
579         HeapFree(GetProcessHeap(), 0, computedHash);
580     }
581 }
582
583 START_TEST(message)
584 {
585     test_msg_get_signer_count();
586     test_verify_detached_message_hash();
587     test_verify_message_hash();
588     test_verify_message_signature();
589     test_hash_message();
590 }