push ecbf641448c0bfa9cef0bbdfb59876e45db6a304
[wine/hacks.git] / dlls / crypt32 / tests / message.c
blobd515d9b4049f5bf04db417f6dd892a02d1d1ea5e
1 /*
2 * Unit test suite for crypt32.dll's Crypt*Message functions
4 * Copyright 2007-2008 Juan Lang
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.
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.
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
21 #include <stdio.h>
22 #include <stdarg.h>
23 #include <windef.h>
24 #include <winbase.h>
25 #include <winerror.h>
26 #include <wincrypt.h>
28 #include "wine/test.h"
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 };
50 static void test_msg_get_signer_count(void)
52 LONG count;
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);
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 };
100 static void test_verify_detached_message_hash(void)
102 BOOL ret;
103 CRYPT_HASH_MESSAGE_PARA para;
104 DWORD size, hashSize;
105 const BYTE *pMsgData = msgData;
106 BYTE hash[16];
108 if (0)
110 ret = CryptVerifyDetachedMessageHash(NULL, NULL, 0, 0, NULL, NULL, NULL,
111 NULL);
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.
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.)
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());
193 static const BYTE signedContent[] = {
194 0x30,0x81,0xb2,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x02,0xa0,
195 0x81,0xa4,0x30,0x81,0xa1,0x02,0x01,0x01,0x31,0x0e,0x30,0x0c,0x06,0x08,0x2a,
196 0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x13,0x06,0x09,0x2a,0x86,
197 0x48,0x86,0xf7,0x0d,0x01,0x07,0x01,0xa0,0x06,0x04,0x04,0x01,0x02,0x03,0x04,
198 0x31,0x77,0x30,0x75,0x02,0x01,0x01,0x30,0x1a,0x30,0x15,0x31,0x13,0x30,0x11,
199 0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,0x20,0x4c,0x61,0x6e,
200 0x67,0x00,0x02,0x01,0x01,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,
201 0x02,0x05,0x05,0x00,0x30,0x04,0x06,0x00,0x05,0x00,0x04,0x40,0x81,0xa6,0x70,
202 0xb3,0xef,0x59,0xd1,0x66,0xd1,0x9b,0xc0,0x9a,0xb6,0x9a,0x5e,0x6d,0x6f,0x6d,
203 0x0d,0x59,0xa9,0xaa,0x6e,0xe9,0x2c,0xa0,0x1e,0xee,0xc2,0x60,0xbc,0x59,0xbe,
204 0x3f,0x63,0x06,0x8d,0xc9,0x11,0x1d,0x23,0x64,0x92,0xef,0x2e,0xfc,0x57,0x29,
205 0xa4,0xaf,0xe0,0xee,0x93,0x19,0x39,0x51,0xe4,0x44,0xb8,0x0b,0x28,0xf4,0xa8,
206 0x0d };
207 static const BYTE signedWithCertEmptyContent[] = {
208 0x30,0x81,0xdf,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x02,0xa0,
209 0x81,0xd1,0x30,0x81,0xce,0x02,0x01,0x01,0x31,0x0e,0x30,0x0c,0x06,0x08,0x2a,
210 0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x02,0x06,0x00,0xa0,0x7c,
211 0x30,0x7a,0x02,0x01,0x01,0x30,0x02,0x06,0x00,0x30,0x15,0x31,0x13,0x30,0x11,
212 0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,0x20,0x4c,0x61,0x6e,
213 0x67,0x00,0x30,0x22,0x18,0x0f,0x31,0x36,0x30,0x31,0x30,0x31,0x30,0x31,0x30,
214 0x30,0x30,0x30,0x30,0x30,0x5a,0x18,0x0f,0x31,0x36,0x30,0x31,0x30,0x31,0x30,
215 0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x30,0x15,0x31,0x13,0x30,0x11,0x06,
216 0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,0x20,0x4c,0x61,0x6e,0x67,
217 0x00,0x30,0x07,0x30,0x02,0x06,0x00,0x03,0x01,0x00,0xa3,0x16,0x30,0x14,0x30,
218 0x12,0x06,0x03,0x55,0x1d,0x13,0x01,0x01,0xff,0x04,0x08,0x30,0x06,0x01,0x01,
219 0xff,0x02,0x01,0x01,0x31,0x37,0x30,0x35,0x02,0x01,0x01,0x30,0x1a,0x30,0x15,
220 0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,
221 0x20,0x4c,0x61,0x6e,0x67,0x00,0x02,0x01,0x01,0x30,0x0c,0x06,0x08,0x2a,0x86,
222 0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x04,0x06,0x00,0x05,0x00,0x04,
223 0x00 };
224 static const BYTE signedWithCertContent[] = {
225 0x30,0x82,0x01,0x32,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x02,
226 0xa0,0x82,0x01,0x23,0x30,0x82,0x01,0x1f,0x02,0x01,0x01,0x31,0x0e,0x30,0x0c,
227 0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x13,0x06,
228 0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x01,0xa0,0x06,0x04,0x04,0x01,
229 0x02,0x03,0x04,0xa0,0x7c,0x30,0x7a,0x02,0x01,0x01,0x30,0x02,0x06,0x00,0x30,
230 0x15,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,
231 0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x30,0x22,0x18,0x0f,0x31,0x36,0x30,0x31,
232 0x30,0x31,0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x18,0x0f,0x31,0x36,
233 0x30,0x31,0x30,0x31,0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x30,0x15,
234 0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,
235 0x20,0x4c,0x61,0x6e,0x67,0x00,0x30,0x07,0x30,0x02,0x06,0x00,0x03,0x01,0x00,
236 0xa3,0x16,0x30,0x14,0x30,0x12,0x06,0x03,0x55,0x1d,0x13,0x01,0x01,0xff,0x04,
237 0x08,0x30,0x06,0x01,0x01,0xff,0x02,0x01,0x01,0x31,0x77,0x30,0x75,0x02,0x01,
238 0x01,0x30,0x1a,0x30,0x15,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,
239 0x0a,0x4a,0x75,0x61,0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x02,0x01,0x01,0x30,
240 0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x04,
241 0x06,0x00,0x05,0x00,0x04,0x40,0x81,0xa6,0x70,0xb3,0xef,0x59,0xd1,0x66,0xd1,
242 0x9b,0xc0,0x9a,0xb6,0x9a,0x5e,0x6d,0x6f,0x6d,0x0d,0x59,0xa9,0xaa,0x6e,0xe9,
243 0x2c,0xa0,0x1e,0xee,0xc2,0x60,0xbc,0x59,0xbe,0x3f,0x63,0x06,0x8d,0xc9,0x11,
244 0x1d,0x23,0x64,0x92,0xef,0x2e,0xfc,0x57,0x29,0xa4,0xaf,0xe0,0xee,0x93,0x19,
245 0x39,0x51,0xe4,0x44,0xb8,0x0b,0x28,0xf4,0xa8,0x0d };
246 static const BYTE signedWithCertWithPubKeyContent[] = {
247 0x30,0x81,0xfc,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x02,0xa0,
248 0x81,0xee,0x30,0x81,0xeb,0x02,0x01,0x01,0x31,0x0e,0x30,0x0c,0x06,0x08,0x2a,
249 0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x02,0x06,0x00,0xa0,0x81,
250 0x98,0x30,0x81,0x95,0x02,0x01,0x01,0x30,0x02,0x06,0x00,0x30,0x15,0x31,0x13,
251 0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,0x20,0x4c,
252 0x61,0x6e,0x67,0x00,0x30,0x22,0x18,0x0f,0x31,0x36,0x30,0x31,0x30,0x31,0x30,
253 0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x18,0x0f,0x31,0x36,0x30,0x31,0x30,
254 0x31,0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x30,0x15,0x31,0x13,0x30,
255 0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,0x20,0x4c,0x61,
256 0x6e,0x67,0x00,0x30,0x22,0x30,0x0d,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,
257 0x01,0x01,0x01,0x05,0x00,0x03,0x11,0x00,0x00,0x01,0x02,0x03,0x04,0x05,0x06,
258 0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0xa3,0x16,0x30,0x14,0x30,0x12,
259 0x06,0x03,0x55,0x1d,0x13,0x01,0x01,0xff,0x04,0x08,0x30,0x06,0x01,0x01,0xff,
260 0x02,0x01,0x01,0x31,0x37,0x30,0x35,0x02,0x01,0x01,0x30,0x1a,0x30,0x15,0x31,
261 0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,0x20,
262 0x4c,0x61,0x6e,0x67,0x00,0x02,0x01,0x01,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,
263 0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x04,0x06,0x00,0x05,0x00,0x04,0x00 };
265 static void test_verify_message_signature(void)
267 BOOL ret;
268 CRYPT_VERIFY_MESSAGE_PARA para = { 0 };
269 PCCERT_CONTEXT cert;
270 DWORD cbDecoded;
272 SetLastError(0xdeadbeef);
273 ret = CryptVerifyMessageSignature(NULL, 0, NULL, 0, NULL, 0, NULL);
274 ok(!ret && GetLastError() == E_INVALIDARG,
275 "Expected E_INVALIDARG, got %08x\n", GetLastError());
276 SetLastError(0xdeadbeef);
277 ret = CryptVerifyMessageSignature(&para, 0, NULL, 0, NULL, 0, NULL);
278 ok(!ret && GetLastError() == E_INVALIDARG,
279 "Expected E_INVALIDARG, got %08x\n", GetLastError());
280 para.cbSize = sizeof(para);
281 SetLastError(0xdeadbeef);
282 ret = CryptVerifyMessageSignature(&para, 0, NULL, 0, NULL, 0, NULL);
283 ok(!ret && GetLastError() == E_INVALIDARG,
284 "Expected E_INVALIDARG, got %08x\n", GetLastError());
285 para.cbSize = 0;
286 para.dwMsgAndCertEncodingType = PKCS_7_ASN_ENCODING;
287 SetLastError(0xdeadbeef);
288 ret = CryptVerifyMessageSignature(&para, 0, NULL, 0, NULL, 0, NULL);
289 ok(!ret && GetLastError() == E_INVALIDARG,
290 "Expected E_INVALIDARG, got %08x\n", GetLastError());
291 para.cbSize = sizeof(para);
292 SetLastError(0xdeadbeef);
293 ret = CryptVerifyMessageSignature(&para, 0, NULL, 0, NULL, 0, NULL);
294 ok(!ret &&
295 (GetLastError() == CRYPT_E_ASN1_EOD ||
296 GetLastError() == OSS_BAD_ARG), /* win9x */
297 "Expected CRYPT_E_ASN1_EOD, got %08x\n", GetLastError());
298 /* Check whether cert is set on error */
299 cert = (PCCERT_CONTEXT)0xdeadbeef;
300 ret = CryptVerifyMessageSignature(&para, 0, NULL, 0, NULL, 0, &cert);
301 ok(cert == NULL, "Expected NULL cert\n");
302 /* Check whether cbDecoded is set on error */
303 cbDecoded = 0xdeadbeef;
304 ret = CryptVerifyMessageSignature(&para, 0, NULL, 0, NULL, &cbDecoded,
305 NULL);
306 ok(!cbDecoded, "Expected 0\n");
307 SetLastError(0xdeadbeef);
308 ret = CryptVerifyMessageSignature(&para, 0, dataEmptyBareContent,
309 sizeof(dataEmptyBareContent), NULL, 0, NULL);
310 ok(GetLastError() == CRYPT_E_ASN1_BADTAG ||
311 GetLastError() == OSS_PDU_MISMATCH, /* win9x */
312 "Expected CRYPT_E_ASN1_BADTAG, got %08x\n", GetLastError());
313 SetLastError(0xdeadbeef);
314 ret = CryptVerifyMessageSignature(&para, 0, dataEmptyContent,
315 sizeof(dataEmptyContent), NULL, 0, NULL);
316 ok(!ret && GetLastError() == CRYPT_E_UNEXPECTED_MSG_TYPE,
317 "Expected CRYPT_E_UNEXPECTED_MSG_TYPE, got %08x\n", GetLastError());
318 SetLastError(0xdeadbeef);
319 ret = CryptVerifyMessageSignature(&para, 0, signedEmptyBareContent,
320 sizeof(signedEmptyBareContent), NULL, 0, NULL);
321 ok(!ret &&
322 (GetLastError() == CRYPT_E_ASN1_BADTAG ||
323 GetLastError() == OSS_DATA_ERROR), /* win9x */
324 "Expected CRYPT_E_ASN1_BADTAG, got %08x\n", GetLastError());
325 SetLastError(0xdeadbeef);
326 ret = CryptVerifyMessageSignature(&para, 0, signedEmptyContent,
327 sizeof(signedEmptyContent), NULL, 0, NULL);
328 ok(!ret &&
329 (GetLastError() == CRYPT_E_NOT_FOUND ||
330 GetLastError() == OSS_DATA_ERROR), /* win9x */
331 "Expected CRYPT_E_NOT_FOUND, got %08x\n", GetLastError());
332 SetLastError(0xdeadbeef);
333 ret = CryptVerifyMessageSignature(&para, 0, signedContent,
334 sizeof(signedContent), NULL, 0, NULL);
335 ok(!ret &&
336 (GetLastError() == CRYPT_E_NOT_FOUND ||
337 GetLastError() == OSS_DATA_ERROR), /* win9x */
338 "Expected CRYPT_E_NOT_FOUND, got %08x\n", GetLastError());
339 /* FIXME: Windows fails with CRYPT_E_NOT_FOUND for these messages, but
340 * their signer certs have invalid public keys that fail to decode. In
341 * Wine therefore the failure is an ASN error. Need some messages with
342 * valid public keys and invalid signatures to check against.
344 ret = CryptVerifyMessageSignature(&para, 0, signedWithCertEmptyContent,
345 sizeof(signedWithCertEmptyContent), NULL, 0, NULL);
346 ok(!ret, "Expected failure\n");
347 ret = CryptVerifyMessageSignature(&para, 0, signedWithCertContent,
348 sizeof(signedWithCertContent), NULL, 0, NULL);
349 ok(!ret, "Expected failure\n");
350 ret = CryptVerifyMessageSignature(&para, 0, signedWithCertWithPubKeyContent,
351 sizeof(signedWithCertWithPubKeyContent), NULL, 0, NULL);
352 ok(!ret, "Expected failure\n");
355 static const BYTE detachedHashBlob[] = {
356 0x30,0x3f,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x05,0xa0,0x32,
357 0x30,0x30,0x02,0x01,0x00,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,
358 0x02,0x05,0x05,0x00,0x30,0x0b,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,
359 0x07,0x01,0x04,0x10,0x2d,0x1b,0xbc,0x1f,0xc7,0xab,0x36,0x8d,0xdb,0x95,0xe6,
360 0x24,0xb9,0x66,0x7c,0x21 };
361 static const BYTE hashBlob[] = {
362 0x30,0x47,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x05,0xa0,0x3a,
363 0x30,0x38,0x02,0x01,0x00,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,
364 0x02,0x05,0x05,0x00,0x30,0x13,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,
365 0x07,0x01,0xa0,0x06,0x04,0x04,0xde,0xad,0xbe,0xef,0x04,0x10,0x2f,0x24,0x92,
366 0x30,0xa8,0xe7,0xc2,0xbf,0x60,0x05,0xcc,0xd2,0x67,0x92,0x59,0xec };
367 static const BYTE hashVal[] = {
368 0x2d,0x1b,0xbc,0x1f,0xc7,0xab,0x36,0x8d,0xdb,0x95,0xe6,0x24,0xb9,0x66,0x7c,
369 0x21 };
371 static void test_hash_message(void)
373 BOOL ret;
374 CRYPT_HASH_MESSAGE_PARA para;
375 static const BYTE blob1[] = { 0xde, 0xad, 0xbe, 0xef };
376 static const BYTE blob2[] = { 0xba, 0xad, 0xf0, 0x0d };
377 const BYTE *toHash[] = { blob1, blob2 };
378 DWORD hashSize[] = { sizeof(blob1), sizeof(blob2) };
379 DWORD hashedBlobSize, computedHashSize;
380 static char oid_rsa_md5[] = szOID_RSA_MD5;
381 LPBYTE hashedBlob, computedHash;
383 /* Crash
384 ret = CryptHashMessage(NULL, FALSE, 0, NULL, 0, NULL, NULL, NULL, NULL);
386 memset(&para, 0, sizeof(para));
387 SetLastError(0xdeadbeef);
388 ret = CryptHashMessage(&para, FALSE, 0, NULL, NULL, NULL, NULL, NULL, NULL);
389 ok(!ret && GetLastError() == E_INVALIDARG,
390 "expected E_INVALIDARG, got 0x%08x\n", GetLastError());
391 para.cbSize = sizeof(para);
392 /* Not quite sure what "success" means in this case, but it does succeed */
393 SetLastError(0xdeadbeef);
394 ret = CryptHashMessage(&para, FALSE, 0, NULL, NULL, NULL, NULL, NULL, NULL);
395 ok(ret, "CryptHashMessage failed: 0x%08x\n", GetLastError());
396 /* With a bogus encoding type it "succeeds" */
397 para.dwMsgEncodingType = 0xdeadbeef;
398 SetLastError(0xdeadbeef);
399 ret = CryptHashMessage(&para, FALSE, 0, NULL, NULL, NULL, NULL, NULL, NULL);
400 ok(ret, "CryptHashMessage failed: 0x%08x\n", GetLastError());
401 /* According to MSDN, the third parameter (cToBeHashed) must be 1 if the
402 * second parameter (fDetached) is FALSE, but again it "succeeds."
404 SetLastError(0xdeadbeef);
405 ret = CryptHashMessage(&para, FALSE, 2, NULL, NULL, NULL, NULL, NULL, NULL);
406 ok(ret, "CryptHashMessage failed: 0x%08x\n", GetLastError());
407 /* Even passing parameters to hash results in "success." */
408 SetLastError(0xdeadbeef);
409 ret = CryptHashMessage(&para, FALSE, 2, toHash, hashSize, NULL, NULL, NULL,
410 NULL);
411 /* Try again with a valid encoding type */
412 para.dwMsgEncodingType = PKCS_7_ASN_ENCODING;
413 SetLastError(0xdeadbeef);
414 ret = CryptHashMessage(&para, FALSE, 2, NULL, NULL, NULL, NULL, NULL, NULL);
415 ok(ret, "CryptHashMessage failed: 0x%08x\n", GetLastError());
416 /* And with valid data to hash */
417 SetLastError(0xdeadbeef);
418 ret = CryptHashMessage(&para, FALSE, 2, toHash, hashSize, NULL, NULL, NULL,
419 NULL);
420 ok(ret, "CryptHashMessage failed: 0x%08x\n", GetLastError());
421 /* But requesting the size of the hashed blob and indicating there's data
422 * to hash results in a crash
424 if (0)
426 ret = CryptHashMessage(&para, FALSE, 2, NULL, NULL, NULL,
427 &hashedBlobSize, NULL, NULL);
429 /* Passing a valid pointer for the data to hash fails, as the hash
430 * algorithm is finally checked.
432 SetLastError(0xdeadbeef);
433 ret = CryptHashMessage(&para, FALSE, 2, toHash, hashSize, NULL,
434 &hashedBlobSize, NULL, NULL);
435 ok(!ret &&
436 (GetLastError() == CRYPT_E_UNKNOWN_ALGO ||
437 GetLastError() == CRYPT_E_OID_FORMAT), /* Vista */
438 "expected CRYPT_E_UNKNOWN_ALGO or CRYPT_E_OID_FORMAT, got 0x%08x (%d)\n",
439 GetLastError(), GetLastError());
440 para.HashAlgorithm.pszObjId = oid_rsa_md5;
441 /* With a valid hash algorithm, this succeeds, even though fDetached is
442 * FALSE.
444 SetLastError(0xdeadbeef);
445 ret = CryptHashMessage(&para, FALSE, 2, toHash, hashSize, NULL,
446 &hashedBlobSize, NULL, NULL);
447 todo_wine
448 ok(ret, "CryptHashMessage failed: 0x%08x\n", GetLastError());
449 if (ret)
451 /* Actually attempting to get the hashed data fails, perhaps because
452 * detached is FALSE.
454 hashedBlob = HeapAlloc(GetProcessHeap(), 0, hashedBlobSize);
455 SetLastError(0xdeadbeef);
456 ret = CryptHashMessage(&para, FALSE, 2, toHash, hashSize, hashedBlob,
457 &hashedBlobSize, NULL, NULL);
458 ok(!ret && GetLastError() == CRYPT_E_MSG_ERROR,
459 "expected CRYPT_E_MSG_ERROR, got 0x%08x (%d)\n", GetLastError(),
460 GetLastError());
461 HeapFree(GetProcessHeap(), 0, hashedBlob);
463 /* Repeating tests with fDetached = TRUE results in success */
464 SetLastError(0xdeadbeef);
465 ret = CryptHashMessage(&para, TRUE, 2, toHash, hashSize, NULL,
466 &hashedBlobSize, NULL, NULL);
467 ok(ret, "CryptHashMessage failed: 0x%08x\n", GetLastError());
468 if (ret)
470 hashedBlob = HeapAlloc(GetProcessHeap(), 0, hashedBlobSize);
471 SetLastError(0xdeadbeef);
472 ret = CryptHashMessage(&para, TRUE, 2, toHash, hashSize, hashedBlob,
473 &hashedBlobSize, NULL, NULL);
474 ok(ret, "CryptHashMessage failed: 0x%08x\n", GetLastError());
475 ok(hashedBlobSize == sizeof(detachedHashBlob),
476 "unexpected size of detached blob %d\n", hashedBlobSize);
477 ok(!memcmp(hashedBlob, detachedHashBlob, hashedBlobSize),
478 "unexpected detached blob value\n");
479 HeapFree(GetProcessHeap(), 0, hashedBlob);
481 /* Hashing a single item with fDetached = FALSE also succeeds */
482 SetLastError(0xdeadbeef);
483 ret = CryptHashMessage(&para, FALSE, 1, toHash, hashSize, NULL,
484 &hashedBlobSize, NULL, NULL);
485 ok(ret, "CryptHashMessage failed: 0x%08x\n", GetLastError());
486 if (ret)
488 hashedBlob = HeapAlloc(GetProcessHeap(), 0, hashedBlobSize);
489 ret = CryptHashMessage(&para, FALSE, 1, toHash, hashSize, hashedBlob,
490 &hashedBlobSize, NULL, NULL);
491 ok(ret, "CryptHashMessage failed: 0x%08x\n", GetLastError());
492 ok(hashedBlobSize == sizeof(hashBlob),
493 "unexpected size of detached blob %d\n", hashedBlobSize);
494 ok(!memcmp(hashedBlob, hashBlob, hashedBlobSize),
495 "unexpected detached blob value\n");
496 HeapFree(GetProcessHeap(), 0, hashedBlob);
498 /* Check the computed hash value too. You don't need to get the encoded
499 * blob to get it.
501 computedHashSize = 0xdeadbeef;
502 ret = CryptHashMessage(&para, TRUE, 2, toHash, hashSize, NULL,
503 &hashedBlobSize, NULL, &computedHashSize);
504 ok(ret, "CryptHashMessage failed: 0x%08x\n", GetLastError());
505 ok(computedHashSize == 16, "expected hash size of 16, got %d\n",
506 computedHashSize);
507 if (ret)
509 computedHash = HeapAlloc(GetProcessHeap(), 0, computedHashSize);
510 SetLastError(0xdeadbeef);
511 ret = CryptHashMessage(&para, TRUE, 2, toHash, hashSize, NULL,
512 &hashedBlobSize, computedHash, &computedHashSize);
513 ok(computedHashSize == sizeof(hashVal),
514 "unexpected size of hash value %d\n", computedHashSize);
515 ok(!memcmp(computedHash, hashVal, computedHashSize),
516 "unexpected value\n");
517 HeapFree(GetProcessHeap(), 0, computedHash);
521 START_TEST(message)
523 test_msg_get_signer_count();
524 test_verify_detached_message_hash();
525 test_verify_message_signature();
526 test_hash_message();