Reactos
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
30static const BYTE dataEmptyBareContent[] = { 0x04,0x00 };
31static const BYTE dataEmptyContent[] = {
320x30,0x0f,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x01,0xa0,0x02,
330x04,0x00 };
34static const BYTE signedEmptyBareContent[] = {
350x30,0x50,0x02,0x01,0x01,0x31,0x0e,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,
360xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x02,0x06,0x00,0x31,0x37,0x30,0x35,0x02,
370x01,0x01,0x30,0x1a,0x30,0x15,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,
380x13,0x0a,0x4a,0x75,0x61,0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x02,0x01,0x01,
390x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,
400x04,0x06,0x00,0x05,0x00,0x04,0x00 };
41static const BYTE signedEmptyContent[] = {
420x30,0x5f,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x02,0xa0,0x52,
430x30,0x50,0x02,0x01,0x01,0x31,0x0e,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,
440xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x02,0x06,0x00,0x31,0x37,0x30,0x35,0x02,
450x01,0x01,0x30,0x1a,0x30,0x15,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,
460x13,0x0a,0x4a,0x75,0x61,0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x02,0x01,0x01,
470x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,
480x04,0x06,0x00,0x05,0x00,0x04,0x00 };
49
50static 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 %ld\n", count);
57 ok(GetLastError() == E_INVALIDARG, "Expected E_INVALIDARG, got %08lx\n",
58 GetLastError());
59 SetLastError(0xdeadbeef);
60 count = CryptGetMessageSignerCount(PKCS_7_ASN_ENCODING, NULL, 0);
61 ok(count == -1, "Expected -1, got %ld\n", count);
62 ok(GetLastError() == CRYPT_E_ASN1_EOD,
63 "Expected CRYPT_E_ASN1_EOD, got %08lx\n", GetLastError());
64 SetLastError(0xdeadbeef);
65 count = CryptGetMessageSignerCount(PKCS_7_ASN_ENCODING,
66 dataEmptyBareContent, sizeof(dataEmptyBareContent));
67 ok(count == -1, "Expected -1, got %ld\n", count);
68 ok(GetLastError() == CRYPT_E_ASN1_BADTAG,
69 "Expected CRYPT_E_ASN1_BADTAG, got %08lx\n", GetLastError());
70 SetLastError(0xdeadbeef);
71 count = CryptGetMessageSignerCount(PKCS_7_ASN_ENCODING,
72 dataEmptyContent, sizeof(dataEmptyContent));
73 ok(count == -1, "Expected -1, got %ld\n", count);
74 ok(GetLastError() == CRYPT_E_INVALID_MSG_TYPE,
75 "Expected CRYPT_E_INVALID_MSG_TYPE, got %08lx\n", GetLastError());
76 SetLastError(0xdeadbeef);
77 count = CryptGetMessageSignerCount(PKCS_7_ASN_ENCODING,
78 signedEmptyBareContent, sizeof(signedEmptyBareContent));
79 ok(count == -1, "Expected -1, got %ld\n", count);
80 ok(GetLastError() == CRYPT_E_ASN1_BADTAG,
81 "Expected CRYPT_E_ASN1_BADTAG, got %08lx\n", GetLastError());
82 count = CryptGetMessageSignerCount(PKCS_7_ASN_ENCODING,
83 signedEmptyContent, sizeof(signedEmptyContent));
84 ok(count == 1, "Expected 1, got %ld\n", count);
85}
86
87static BYTE detachedHashContent[] = {
880x30,0x3f,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x05,0xa0,0x32,
890x30,0x30,0x02,0x01,0x00,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,
900x02,0x05,0x05,0x00,0x30,0x0b,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,
910x07,0x01,0x04,0x10,0x08,0xd6,0xc0,0x5a,0x21,0x51,0x2a,0x79,0xa1,0xdf,0xeb,
920x9d,0x2a,0x8f,0x26,0x2f };
93static const BYTE msgData[] = { 1, 2, 3, 4 };
94
95static void test_verify_detached_message_hash(void)
96{
97 BOOL ret;
98 CRYPT_HASH_MESSAGE_PARA para;
99 DWORD size, hashSize;
100 const BYTE *pMsgData = msgData;
101 BYTE hash[16];
102
103 if (0)
104 {
105 CryptVerifyDetachedMessageHash(NULL, NULL, 0, 0, NULL, NULL, NULL,
106 NULL);
107 }
108 memset(¶, 0, sizeof(para));
109 SetLastError(0xdeadbeef);
110 ret = CryptVerifyDetachedMessageHash(¶, NULL, 0, 0, NULL, NULL, NULL,
111 NULL);
112 ok(!ret && GetLastError() == E_INVALIDARG,
113 "expected E_INVALIDARG, got %08lx\n", GetLastError());
114 para.cbSize = sizeof(para);
115 SetLastError(0xdeadbeef);
116 ret = CryptVerifyDetachedMessageHash(¶, NULL, 0, 0, NULL, NULL, NULL,
117 NULL);
118 ok(!ret && GetLastError() == E_INVALIDARG,
119 "expected E_INVALIDARG, got %08lx\n", GetLastError());
120 para.dwMsgEncodingType = PKCS_7_ASN_ENCODING;
121 SetLastError(0xdeadbeef);
122 ret = CryptVerifyDetachedMessageHash(¶, NULL, 0, 0, NULL, NULL, NULL,
123 NULL);
124 ok(!ret && GetLastError() == CRYPT_E_ASN1_EOD,
125 "expected CRYPT_E_ASN1_EOD, got %08lx\n", GetLastError());
126 para.dwMsgEncodingType = X509_ASN_ENCODING;
127 SetLastError(0xdeadbeef);
128 ret = CryptVerifyDetachedMessageHash(¶, NULL, 0, 0, NULL, NULL, NULL,
129 NULL);
130 ok(!ret && GetLastError() == E_INVALIDARG,
131 "expected E_INVALIDARG, got %08lx\n", GetLastError());
132 para.dwMsgEncodingType = X509_ASN_ENCODING | PKCS_7_ASN_ENCODING;
133 SetLastError(0xdeadbeef);
134 ret = CryptVerifyDetachedMessageHash(¶, NULL, 0, 0, NULL, NULL, NULL,
135 NULL);
136 ok(!ret && GetLastError() == CRYPT_E_ASN1_EOD,
137 "expected CRYPT_E_ASN1_EOD, got %08lx\n", GetLastError());
138 /* Curiously, passing no data to hash succeeds.. */
139 ret = CryptVerifyDetachedMessageHash(¶, detachedHashContent,
140 sizeof(detachedHashContent), 0, NULL, NULL, NULL, NULL);
141 todo_wine
142 ok(ret, "CryptVerifyDetachedMessageHash failed: %08lx\n", GetLastError());
143 /* as does passing the actual content of the message to hash.. */
144 size = sizeof(msgData);
145 pMsgData = msgData;
146 ret = CryptVerifyDetachedMessageHash(¶, detachedHashContent,
147 sizeof(detachedHashContent), 1, &pMsgData, &size, NULL, NULL);
148 ok(ret, "CryptVerifyDetachedMessageHash failed: %08lx\n", GetLastError());
149 /* while passing data to hash that isn't the content of the message fails.
150 */
151 size = sizeof(detachedHashContent);
152 pMsgData = detachedHashContent;
153 SetLastError(0xdeadbeef);
154 ret = CryptVerifyDetachedMessageHash(¶, detachedHashContent,
155 sizeof(detachedHashContent), 1, &pMsgData, &size, NULL, NULL);
156 ok(!ret && GetLastError() == CRYPT_E_HASH_VALUE,
157 "expected CRYPT_E_HASH_VALUE, got %08lx\n", GetLastError());
158 /* Getting the size of the hash while passing no hash data causes the
159 * hash to be checked (and fail.)
160 */
161 SetLastError(0xdeadbeef);
162 ret = CryptVerifyDetachedMessageHash(¶, detachedHashContent,
163 sizeof(detachedHashContent), 0, NULL, NULL, NULL, &hashSize);
164 ok(!ret && GetLastError() == CRYPT_E_HASH_VALUE,
165 "expected CRYPT_E_HASH_VALUE, got %08lx\n", GetLastError());
166 size = sizeof(msgData);
167 pMsgData = msgData;
168 ret = CryptVerifyDetachedMessageHash(¶, detachedHashContent,
169 sizeof(detachedHashContent), 1, &pMsgData, &size, NULL, &hashSize);
170 ok(ret, "CryptVerifyDetachedMessageHash failed: %08lx\n", GetLastError());
171 ok(hashSize == sizeof(hash), "unexpected size %ld\n", hashSize);
172 hashSize = 1;
173 SetLastError(0xdeadbeef);
174 ret = CryptVerifyDetachedMessageHash(¶, detachedHashContent,
175 sizeof(detachedHashContent), 1, &pMsgData, &size, hash, &hashSize);
176 ok(!ret && GetLastError() == ERROR_MORE_DATA,
177 "expected ERROR_MORE_DATA, got %08lx\n", GetLastError());
178 hashSize = sizeof(hash);
179 ret = CryptVerifyDetachedMessageHash(¶, detachedHashContent,
180 sizeof(detachedHashContent), 1, &pMsgData, &size, hash, &hashSize);
181 ok(ret, "CryptVerifyDetachedMessageHash failed: %08lx\n", GetLastError());
182}
183
184static BYTE hashContent[] = {
1850x30,0x47,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x05,0xa0,0x3a,
1860x30,0x38,0x02,0x01,0x00,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,
1870x02,0x05,0x05,0x00,0x30,0x13,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,
1880x07,0x01,0xa0,0x06,0x04,0x04,0x01,0x02,0x03,0x04,0x04,0x10,0x08,0xd6,0xc0,
1890x5a,0x21,0x51,0x2a,0x79,0xa1,0xdf,0xeb,0x9d,0x2a,0x8f,0x26,0x2f };
190
191static void test_verify_message_hash(void)
192{
193 BOOL ret;
194 CRYPT_HASH_MESSAGE_PARA para;
195 DWORD size;
196 BYTE *buf = NULL;
197
198 memset(¶, 0, sizeof(para));
199 /* Crash */
200 if (0)
201 ret = CryptVerifyMessageHash(NULL, NULL, 0, NULL, NULL, NULL, NULL);
202 SetLastError(0xdeadbeef);
203 ret = CryptVerifyMessageHash(¶, NULL, 0, NULL, NULL, NULL, NULL);
204 ok(!ret && GetLastError() == E_INVALIDARG,
205 "expected E_INVALIDARG, got %08lx\n", GetLastError());
206 para.cbSize = sizeof(para);
207 SetLastError(0xdeadbeef);
208 ret = CryptVerifyMessageHash(¶, NULL, 0, NULL, NULL, NULL, NULL);
209 ok(!ret && GetLastError() == E_INVALIDARG,
210 "expected E_INVALIDARG, got %08lx\n", GetLastError());
211 para.dwMsgEncodingType = PKCS_7_ASN_ENCODING;
212 SetLastError(0xdeadbeef);
213 ret = CryptVerifyMessageHash(¶, NULL, 0, NULL, NULL, NULL, NULL);
214 ok(!ret, "Expected 0, got %d\n", ret);
215 ok(GetLastError() == CRYPT_E_ASN1_EOD,
216 "Expected CRYPT_E_ASN1_EOD, got %08lx\n", GetLastError());
217 /* Verifying the hash of a detached message succeeds? */
218 ret = CryptVerifyMessageHash(¶, detachedHashContent,
219 sizeof(detachedHashContent), NULL, NULL, NULL, NULL);
220 todo_wine
221 ok(ret, "CryptVerifyMessageHash failed: %08lx\n", GetLastError());
222 /* As does verifying the hash of a regular message. */
223 ret = CryptVerifyMessageHash(¶, hashContent, sizeof(hashContent),
224 NULL, NULL, NULL, NULL);
225 ok(ret, "CryptVerifyMessageHash failed: %08lx\n", GetLastError());
226 ret = CryptVerifyMessageHash(¶, hashContent, sizeof(hashContent),
227 NULL, &size, NULL, NULL);
228 ok(ret, "CryptVerifyMessageHash failed: %08lx\n", GetLastError());
229 if (ret)
230 buf = CryptMemAlloc(size);
231 if (buf)
232 {
233 size = 1;
234 ret = CryptVerifyMessageHash(¶, hashContent, sizeof(hashContent),
235 buf, &size, NULL, NULL);
236 ok(!ret && GetLastError() == ERROR_MORE_DATA,
237 "expected ERROR_MORE_DATA, got %08lx\n", GetLastError());
238 ret = CryptVerifyMessageHash(¶, hashContent, sizeof(hashContent),
239 buf, &size, NULL, NULL);
240 ok(ret, "CryptVerifyMessageHash failed: %08lx\n", GetLastError());
241 ok(size == sizeof(msgData), "unexpected size %ld\n", size);
242 ok(!memcmp(buf, msgData, size), "unexpected value\n");
243 CryptMemFree(buf);
244 }
245}
246
247static const BYTE signedWithCertContent[] = {
2480x30,0x82,0x01,0x32,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x02,
2490xa0,0x82,0x01,0x23,0x30,0x82,0x01,0x1f,0x02,0x01,0x01,0x31,0x0e,0x30,0x0c,
2500x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x13,0x06,
2510x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x01,0xa0,0x06,0x04,0x04,0x01,
2520x02,0x03,0x04,0xa0,0x7c,0x30,0x7a,0x02,0x01,0x01,0x30,0x02,0x06,0x00,0x30,
2530x15,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,
2540x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x30,0x22,0x18,0x0f,0x31,0x36,0x30,0x31,
2550x30,0x31,0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x18,0x0f,0x31,0x36,
2560x30,0x31,0x30,0x31,0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x30,0x15,
2570x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,
2580x20,0x4c,0x61,0x6e,0x67,0x00,0x30,0x07,0x30,0x02,0x06,0x00,0x03,0x01,0x00,
2590xa3,0x16,0x30,0x14,0x30,0x12,0x06,0x03,0x55,0x1d,0x13,0x01,0x01,0xff,0x04,
2600x08,0x30,0x06,0x01,0x01,0xff,0x02,0x01,0x01,0x31,0x77,0x30,0x75,0x02,0x01,
2610x01,0x30,0x1a,0x30,0x15,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,
2620x0a,0x4a,0x75,0x61,0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x02,0x01,0x01,0x30,
2630x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x04,
2640x06,0x00,0x05,0x00,0x04,0x40,0x81,0xa6,0x70,0xb3,0xef,0x59,0xd1,0x66,0xd1,
2650x9b,0xc0,0x9a,0xb6,0x9a,0x5e,0x6d,0x6f,0x6d,0x0d,0x59,0xa9,0xaa,0x6e,0xe9,
2660x2c,0xa0,0x1e,0xee,0xc2,0x60,0xbc,0x59,0xbe,0x3f,0x63,0x06,0x8d,0xc9,0x11,
2670x1d,0x23,0x64,0x92,0xef,0x2e,0xfc,0x57,0x29,0xa4,0xaf,0xe0,0xee,0x93,0x19,
2680x39,0x51,0xe4,0x44,0xb8,0x0b,0x28,0xf4,0xa8,0x0d };
269static const BYTE signedContent[] = {
2700x30,0x81,0xb2,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x02,0xa0,
2710x81,0xa4,0x30,0x81,0xa1,0x02,0x01,0x01,0x31,0x0e,0x30,0x0c,0x06,0x08,0x2a,
2720x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x13,0x06,0x09,0x2a,0x86,
2730x48,0x86,0xf7,0x0d,0x01,0x07,0x01,0xa0,0x06,0x04,0x04,0x01,0x02,0x03,0x04,
2740x31,0x77,0x30,0x75,0x02,0x01,0x01,0x30,0x1a,0x30,0x15,0x31,0x13,0x30,0x11,
2750x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,0x20,0x4c,0x61,0x6e,
2760x67,0x00,0x02,0x01,0x01,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,
2770x02,0x05,0x05,0x00,0x30,0x04,0x06,0x00,0x05,0x00,0x04,0x40,0x81,0xa6,0x70,
2780xb3,0xef,0x59,0xd1,0x66,0xd1,0x9b,0xc0,0x9a,0xb6,0x9a,0x5e,0x6d,0x6f,0x6d,
2790x0d,0x59,0xa9,0xaa,0x6e,0xe9,0x2c,0xa0,0x1e,0xee,0xc2,0x60,0xbc,0x59,0xbe,
2800x3f,0x63,0x06,0x8d,0xc9,0x11,0x1d,0x23,0x64,0x92,0xef,0x2e,0xfc,0x57,0x29,
2810xa4,0xaf,0xe0,0xee,0x93,0x19,0x39,0x51,0xe4,0x44,0xb8,0x0b,0x28,0xf4,0xa8,
2820x0d };
283static const BYTE detachedSignedContent[] = {
2840x30,0x81,0xaa,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x02,0xa0,
2850x81,0x9c,0x30,0x81,0x99,0x02,0x01,0x01,0x31,0x0e,0x30,0x0c,0x06,0x08,0x2a,
2860x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x0b,0x06,0x09,0x2a,0x86,
2870x48,0x86,0xf7,0x0d,0x01,0x07,0x01,0x31,0x77,0x30,0x75,0x02,0x01,0x01,0x30,
2880x1a,0x30,0x15,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,
2890x75,0x61,0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x02,0x01,0x01,0x30,0x0c,0x06,
2900x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x04,0x06,0x00,
2910x05,0x00,0x04,0x40,0x81,0xa6,0x70,0xb3,0xef,0x59,0xd1,0x66,0xd1,0x9b,0xc0,
2920x9a,0xb6,0x9a,0x5e,0x6d,0x6f,0x6d,0x0d,0x59,0xa9,0xaa,0x6e,0xe9,0x2c,0xa0,
2930x1e,0xee,0xc2,0x60,0xbc,0x59,0xbe,0x3f,0x63,0x06,0x8d,0xc9,0x11,0x1d,0x23,
2940x64,0x92,0xef,0x2e,0xfc,0x57,0x29,0xa4,0xaf,0xe0,0xee,0x93,0x19,0x39,0x51,
2950xe4,0x44,0xb8,0x0b,0x28,0xf4,0xa8,0x0d };
296static const BYTE v1CertWithValidPubKey[] = {
2970x30,0x81,0xcf,0x02,0x01,0x01,0x30,0x02,0x06,0x00,0x30,0x15,0x31,0x13,0x30,
2980x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,0x20,0x4c,0x61,
2990x6e,0x67,0x00,0x30,0x22,0x18,0x0f,0x31,0x36,0x30,0x31,0x30,0x31,0x30,0x31,
3000x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x18,0x0f,0x31,0x36,0x30,0x31,0x30,0x31,
3010x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x30,0x15,0x31,0x13,0x30,0x11,
3020x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,0x20,0x4c,0x61,0x6e,
3030x67,0x00,0x30,0x5c,0x30,0x0d,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,
3040x01,0x01,0x05,0x00,0x03,0x4b,0x00,0x30,0x48,0x02,0x41,0x00,0xe2,0x54,0x3a,
3050xa7,0x83,0xb1,0x27,0x14,0x3e,0x59,0xbb,0xb4,0x53,0xe6,0x1f,0xe7,0x5d,0xf1,
3060x21,0x68,0xad,0x85,0x53,0xdb,0x6b,0x1e,0xeb,0x65,0x97,0x03,0x86,0x60,0xde,
3070xf3,0x6c,0x38,0x75,0xe0,0x4c,0x61,0xbb,0xbc,0x62,0x17,0xa9,0xcd,0x79,0x3f,
3080x21,0x4e,0x96,0xcb,0x0e,0xdc,0x61,0x94,0x30,0x18,0x10,0x6b,0xd0,0x1c,0x10,
3090x79,0x02,0x03,0x01,0x00,0x01,0xa3,0x16,0x30,0x14,0x30,0x12,0x06,0x03,0x55,
3100x1d,0x13,0x01,0x01,0xff,0x04,0x08,0x30,0x06,0x01,0x01,0xff,0x02,0x01,0x01 };
311
312static PCCERT_CONTEXT WINAPI msg_get_signer_callback(void *pvArg,
313 DWORD certEncodingType, PCERT_INFO signerId, HCERTSTORE store)
314{
315 return CertCreateCertificateContext(X509_ASN_ENCODING,
316 v1CertWithValidPubKey, sizeof(v1CertWithValidPubKey));
317}
318
319static void test_verify_detached_message_signature(void)
320{
321 CRYPT_VERIFY_MESSAGE_PARA para;
322 BOOL ret;
323 const BYTE *pContent;
324 DWORD cbContent;
325
326 memset(¶, 0, sizeof(para));
327 SetLastError(0xdeadbeef);
328 ret = CryptVerifyDetachedMessageSignature(NULL, 0, NULL, 0, 0, NULL,
329 NULL, NULL);
330 ok(!ret && GetLastError() == E_INVALIDARG,
331 "Expected E_INVALIDARG, got %08lx\n", GetLastError());
332 SetLastError(0xdeadbeef);
333 ret = CryptVerifyDetachedMessageSignature(¶, 0, NULL, 0, 0, NULL,
334 NULL, NULL);
335 ok(!ret && GetLastError() == E_INVALIDARG,
336 "Expected E_INVALIDARG, got %08lx\n", GetLastError());
337 para.cbSize = sizeof(para);
338 SetLastError(0xdeadbeef);
339 ret = CryptVerifyDetachedMessageSignature(¶, 0, NULL, 0, 0, NULL,
340 NULL, NULL);
341 ok(!ret && GetLastError() == E_INVALIDARG,
342 "Expected E_INVALIDARG, got %08lx\n", GetLastError());
343 para.dwMsgAndCertEncodingType = X509_ASN_ENCODING;
344 SetLastError(0xdeadbeef);
345 ret = CryptVerifyDetachedMessageSignature(¶, 0, NULL, 0, 0, NULL,
346 NULL, NULL);
347 ok(!ret && GetLastError() == E_INVALIDARG,
348 "Expected E_INVALIDARG, got %08lx\n", GetLastError());
349 para.dwMsgAndCertEncodingType = PKCS_7_ASN_ENCODING;
350 SetLastError(0xdeadbeef);
351 ret = CryptVerifyDetachedMessageSignature(¶, 0, NULL, 0, 0, NULL,
352 NULL, NULL);
353 ok(!ret, "Expected 0, got %d\n", ret);
354 ok(GetLastError() == CRYPT_E_ASN1_EOD,
355 "Expected CRYPT_E_ASN1_EOD, got %08lx\n", GetLastError());
356 /* None of these messages contains a cert in the message itself, so the
357 * default callback isn't able to verify their signature.
358 */
359 SetLastError(0xdeadbeef);
360 ret = CryptVerifyDetachedMessageSignature(¶, 0, signedWithCertContent,
361 sizeof(signedWithCertContent), 0, NULL, NULL, NULL);
362 ok(!ret, "Expected 0, got %d\n", ret);
363 todo_wine
364 ok(GetLastError() == CRYPT_E_NOT_FOUND,
365 "Expected CRYPT_E_NOT_FOUND, got %08lx\n", GetLastError());
366 SetLastError(0xdeadbeef);
367 ret = CryptVerifyDetachedMessageSignature(¶, 0, signedContent,
368 sizeof(signedContent), 0, NULL, NULL, NULL);
369 ok(!ret, "Expected 0, got %d\n", ret);
370 ok(GetLastError() == CRYPT_E_NOT_FOUND,
371 "Expected CRYPT_E_NOT_FOUND, got %08lx\n", GetLastError());
372 SetLastError(0xdeadbeef);
373 ret = CryptVerifyDetachedMessageSignature(¶, 0, detachedSignedContent,
374 sizeof(detachedSignedContent), 0, NULL, NULL, NULL);
375 ok(!ret, "Expected 0, got %d\n", ret);
376 ok(GetLastError() == CRYPT_E_NOT_FOUND,
377 "Expected CRYPT_E_NOT_FOUND, got %08lx\n", GetLastError());
378 SetLastError(0xdeadbeef);
379 pContent = msgData;
380 cbContent = sizeof(msgData);
381 ret = CryptVerifyDetachedMessageSignature(¶, 0, detachedSignedContent,
382 sizeof(detachedSignedContent), 1, &pContent, &cbContent, NULL);
383 ok(!ret, "Expected 0, got %d\n", ret);
384 ok(GetLastError() == CRYPT_E_NOT_FOUND,
385 "Expected CRYPT_E_NOT_FOUND, got %08lx\n", GetLastError());
386 /* Passing the correct callback results in success */
387 para.pfnGetSignerCertificate = msg_get_signer_callback;
388 ret = CryptVerifyDetachedMessageSignature(¶, 0, detachedSignedContent,
389 sizeof(detachedSignedContent), 1, &pContent, &cbContent, NULL);
390 ok(ret, "CryptVerifyDetachedMessageSignature failed: %08lx\n",
391 GetLastError());
392 /* Not passing the correct data to be signed results in the signature not
393 * matching.
394 */
395 SetLastError(0xdeadbeef);
396 ret = CryptVerifyDetachedMessageSignature(¶, 0, detachedSignedContent,
397 sizeof(detachedSignedContent), 0, NULL, NULL, NULL);
398 ok(!ret, "Expected 0, got %d\n", ret);
399 ok(GetLastError() == NTE_BAD_SIGNATURE,
400 "Expected NTE_BAD_SIGNATURE, got %08lx\n", GetLastError());
401}
402
403static const BYTE signedWithCertEmptyContent[] = {
4040x30,0x81,0xdf,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x02,0xa0,
4050x81,0xd1,0x30,0x81,0xce,0x02,0x01,0x01,0x31,0x0e,0x30,0x0c,0x06,0x08,0x2a,
4060x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x02,0x06,0x00,0xa0,0x7c,
4070x30,0x7a,0x02,0x01,0x01,0x30,0x02,0x06,0x00,0x30,0x15,0x31,0x13,0x30,0x11,
4080x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,0x20,0x4c,0x61,0x6e,
4090x67,0x00,0x30,0x22,0x18,0x0f,0x31,0x36,0x30,0x31,0x30,0x31,0x30,0x31,0x30,
4100x30,0x30,0x30,0x30,0x30,0x5a,0x18,0x0f,0x31,0x36,0x30,0x31,0x30,0x31,0x30,
4110x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x30,0x15,0x31,0x13,0x30,0x11,0x06,
4120x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,0x20,0x4c,0x61,0x6e,0x67,
4130x00,0x30,0x07,0x30,0x02,0x06,0x00,0x03,0x01,0x00,0xa3,0x16,0x30,0x14,0x30,
4140x12,0x06,0x03,0x55,0x1d,0x13,0x01,0x01,0xff,0x04,0x08,0x30,0x06,0x01,0x01,
4150xff,0x02,0x01,0x01,0x31,0x37,0x30,0x35,0x02,0x01,0x01,0x30,0x1a,0x30,0x15,
4160x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,
4170x20,0x4c,0x61,0x6e,0x67,0x00,0x02,0x01,0x01,0x30,0x0c,0x06,0x08,0x2a,0x86,
4180x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x04,0x06,0x00,0x05,0x00,0x04,
4190x00 };
420static const BYTE signedWithCertWithPubKeyContent[] = {
4210x30,0x81,0xfc,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x02,0xa0,
4220x81,0xee,0x30,0x81,0xeb,0x02,0x01,0x01,0x31,0x0e,0x30,0x0c,0x06,0x08,0x2a,
4230x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x02,0x06,0x00,0xa0,0x81,
4240x98,0x30,0x81,0x95,0x02,0x01,0x01,0x30,0x02,0x06,0x00,0x30,0x15,0x31,0x13,
4250x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,0x20,0x4c,
4260x61,0x6e,0x67,0x00,0x30,0x22,0x18,0x0f,0x31,0x36,0x30,0x31,0x30,0x31,0x30,
4270x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x18,0x0f,0x31,0x36,0x30,0x31,0x30,
4280x31,0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x30,0x15,0x31,0x13,0x30,
4290x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,0x20,0x4c,0x61,
4300x6e,0x67,0x00,0x30,0x22,0x30,0x0d,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,
4310x01,0x01,0x01,0x05,0x00,0x03,0x11,0x00,0x00,0x01,0x02,0x03,0x04,0x05,0x06,
4320x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0xa3,0x16,0x30,0x14,0x30,0x12,
4330x06,0x03,0x55,0x1d,0x13,0x01,0x01,0xff,0x04,0x08,0x30,0x06,0x01,0x01,0xff,
4340x02,0x01,0x01,0x31,0x37,0x30,0x35,0x02,0x01,0x01,0x30,0x1a,0x30,0x15,0x31,
4350x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,0x20,
4360x4c,0x61,0x6e,0x67,0x00,0x02,0x01,0x01,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,
4370x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x04,0x06,0x00,0x05,0x00,0x04,0x00 };
438static const BYTE signedWithCertWithValidPubKeyContent[] = {
4390x30,0x82,0x01,0x89,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x02,
4400xa0,0x82,0x01,0x7a,0x30,0x82,0x01,0x76,0x02,0x01,0x01,0x31,0x0e,0x30,0x0c,
4410x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x13,0x06,
4420x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x01,0xa0,0x06,0x04,0x04,0x01,
4430x02,0x03,0x04,0xa0,0x81,0xd2,0x30,0x81,0xcf,0x02,0x01,0x01,0x30,0x02,0x06,
4440x00,0x30,0x15,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,
4450x75,0x61,0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x30,0x22,0x18,0x0f,0x31,0x36,
4460x30,0x31,0x30,0x31,0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x18,0x0f,
4470x31,0x36,0x30,0x31,0x30,0x31,0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,
4480x30,0x15,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,
4490x61,0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x30,0x5c,0x30,0x0d,0x06,0x09,0x2a,
4500x86,0x48,0x86,0xf7,0x0d,0x01,0x01,0x01,0x05,0x00,0x03,0x4b,0x00,0x30,0x48,
4510x02,0x41,0x00,0xe2,0x54,0x3a,0xa7,0x83,0xb1,0x27,0x14,0x3e,0x59,0xbb,0xb4,
4520x53,0xe6,0x1f,0xe7,0x5d,0xf1,0x21,0x68,0xad,0x85,0x53,0xdb,0x6b,0x1e,0xeb,
4530x65,0x97,0x03,0x86,0x60,0xde,0xf3,0x6c,0x38,0x75,0xe0,0x4c,0x61,0xbb,0xbc,
4540x62,0x17,0xa9,0xcd,0x79,0x3f,0x21,0x4e,0x96,0xcb,0x0e,0xdc,0x61,0x94,0x30,
4550x18,0x10,0x6b,0xd0,0x1c,0x10,0x79,0x02,0x03,0x01,0x00,0x01,0xa3,0x16,0x30,
4560x14,0x30,0x12,0x06,0x03,0x55,0x1d,0x13,0x01,0x01,0xff,0x04,0x08,0x30,0x06,
4570x01,0x01,0xff,0x02,0x01,0x01,0x31,0x77,0x30,0x75,0x02,0x01,0x01,0x30,0x1a,
4580x30,0x15,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,
4590x61,0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x02,0x01,0x01,0x30,0x0c,0x06,0x08,
4600x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x04,0x06,0x00,0x05,
4610x00,0x04,0x40,0x81,0xa6,0x70,0xb3,0xef,0x59,0xd1,0x66,0xd1,0x9b,0xc0,0x9a,
4620xb6,0x9a,0x5e,0x6d,0x6f,0x6d,0x0d,0x59,0xa9,0xaa,0x6e,0xe9,0x2c,0xa0,0x1e,
4630xee,0xc2,0x60,0xbc,0x59,0xbe,0x3f,0x63,0x06,0x8d,0xc9,0x11,0x1d,0x23,0x64,
4640x92,0xef,0x2e,0xfc,0x57,0x29,0xa4,0xaf,0xe0,0xee,0x93,0x19,0x39,0x51,0xe4,
4650x44,0xb8,0x0b,0x28,0xf4,0xa8,0x0d };
466
467static void test_verify_message_signature(void)
468{
469 BOOL ret;
470 CRYPT_VERIFY_MESSAGE_PARA para = { 0 };
471 PCCERT_CONTEXT cert;
472 DWORD cbDecoded;
473 BYTE decoded[sizeof(msgData)];
474
475 SetLastError(0xdeadbeef);
476 ret = CryptVerifyMessageSignature(NULL, 0, NULL, 0, NULL, 0, NULL);
477 ok(!ret && GetLastError() == E_INVALIDARG,
478 "Expected E_INVALIDARG, got %08lx\n", GetLastError());
479 /* Is cbDecoded set when invalid parameters are passed? */
480 cbDecoded = 0xdeadbeef;
481 ret = CryptVerifyMessageSignature(NULL, 0, NULL, 0, NULL, &cbDecoded,
482 NULL);
483 ok(!ret && GetLastError() == E_INVALIDARG,
484 "Expected E_INVALIDARG, got %08lx\n", GetLastError());
485 ok(cbDecoded == 0, "expected 0, got %08lx\n", cbDecoded);
486 SetLastError(0xdeadbeef);
487 ret = CryptVerifyMessageSignature(¶, 0, NULL, 0, NULL, 0, NULL);
488 ok(!ret && GetLastError() == E_INVALIDARG,
489 "Expected E_INVALIDARG, got %08lx\n", GetLastError());
490 para.cbSize = sizeof(para);
491 SetLastError(0xdeadbeef);
492 ret = CryptVerifyMessageSignature(¶, 0, NULL, 0, NULL, 0, NULL);
493 ok(!ret && GetLastError() == E_INVALIDARG,
494 "Expected E_INVALIDARG, got %08lx\n", GetLastError());
495 para.cbSize = 0;
496 para.dwMsgAndCertEncodingType = PKCS_7_ASN_ENCODING;
497 SetLastError(0xdeadbeef);
498 ret = CryptVerifyMessageSignature(¶, 0, NULL, 0, NULL, 0, NULL);
499 ok(!ret && GetLastError() == E_INVALIDARG,
500 "Expected E_INVALIDARG, got %08lx\n", GetLastError());
501 para.cbSize = sizeof(para);
502 SetLastError(0xdeadbeef);
503 ret = CryptVerifyMessageSignature(¶, 0, NULL, 0, NULL, 0, NULL);
504 ok(!ret && GetLastError() == CRYPT_E_ASN1_EOD,
505 "Expected CRYPT_E_ASN1_EOD, got %08lx\n", GetLastError());
506 /* Check whether cert is set on error */
507 cert = (PCCERT_CONTEXT)0xdeadbeef;
508 ret = CryptVerifyMessageSignature(¶, 0, NULL, 0, NULL, 0, &cert);
509 ok(!ret && GetLastError() == CRYPT_E_ASN1_EOD,
510 "Expected CRYPT_E_ASN1_EOD, got %08lx\n", GetLastError());
511 ok(cert == NULL, "Expected NULL cert\n");
512 /* Check whether cbDecoded is set on error */
513 cbDecoded = 0xdeadbeef;
514 ret = CryptVerifyMessageSignature(¶, 0, NULL, 0, NULL, &cbDecoded,
515 NULL);
516 ok(!ret && GetLastError() == CRYPT_E_ASN1_EOD,
517 "Expected CRYPT_E_ASN1_EOD, got %08lx\n", GetLastError());
518 ok(!cbDecoded, "Expected 0\n");
519 SetLastError(0xdeadbeef);
520 ret = CryptVerifyMessageSignature(¶, 0, dataEmptyBareContent,
521 sizeof(dataEmptyBareContent), NULL, 0, NULL);
522 ok(!ret && GetLastError() == CRYPT_E_ASN1_BADTAG,
523 "Expected CRYPT_E_ASN1_BADTAG, got %08lx\n", GetLastError());
524 ok(GetLastError() == CRYPT_E_ASN1_BADTAG,
525 "Expected CRYPT_E_ASN1_BADTAG, got %08lx\n", GetLastError());
526 SetLastError(0xdeadbeef);
527 ret = CryptVerifyMessageSignature(¶, 0, dataEmptyContent,
528 sizeof(dataEmptyContent), NULL, 0, NULL);
529 ok(!ret && GetLastError() == CRYPT_E_UNEXPECTED_MSG_TYPE,
530 "Expected CRYPT_E_UNEXPECTED_MSG_TYPE, got %08lx\n", GetLastError());
531 SetLastError(0xdeadbeef);
532 ret = CryptVerifyMessageSignature(¶, 0, signedEmptyBareContent,
533 sizeof(signedEmptyBareContent), NULL, 0, NULL);
534 ok(!ret && GetLastError() == CRYPT_E_ASN1_BADTAG,
535 "Expected CRYPT_E_ASN1_BADTAG, got %08lx\n", GetLastError());
536 SetLastError(0xdeadbeef);
537 ret = CryptVerifyMessageSignature(¶, 0, signedEmptyContent,
538 sizeof(signedEmptyContent), NULL, 0, NULL);
539 ok(!ret && GetLastError() == CRYPT_E_NOT_FOUND,
540 "Expected CRYPT_E_NOT_FOUND, got %08lx\n", GetLastError());
541 SetLastError(0xdeadbeef);
542 ret = CryptVerifyMessageSignature(¶, 0, signedContent,
543 sizeof(signedContent), NULL, 0, NULL);
544 ok(!ret && GetLastError() == CRYPT_E_NOT_FOUND,
545 "Expected CRYPT_E_NOT_FOUND, got %08lx\n", GetLastError());
546 /* FIXME: Windows fails with CRYPT_E_NOT_FOUND for these messages, but
547 * their signer certs have invalid public keys that fail to decode. In
548 * Wine therefore the failure is an ASN error. Need some messages with
549 * valid public keys and invalid signatures to check against.
550 */
551 ret = CryptVerifyMessageSignature(¶, 0, signedWithCertEmptyContent,
552 sizeof(signedWithCertEmptyContent), NULL, 0, NULL);
553 ok(!ret, "Expected failure\n");
554 ret = CryptVerifyMessageSignature(¶, 0, signedWithCertContent,
555 sizeof(signedWithCertContent), NULL, 0, NULL);
556 ok(!ret, "Expected failure\n");
557 ret = CryptVerifyMessageSignature(¶, 0, signedWithCertWithPubKeyContent,
558 sizeof(signedWithCertWithPubKeyContent), NULL, 0, NULL);
559 ok(!ret, "Expected failure\n");
560 /* Apparently, an output pcbDecoded parameter is expected. */
561 ret = CryptVerifyMessageSignature(¶, 0,
562 signedWithCertWithValidPubKeyContent,
563 sizeof(signedWithCertWithValidPubKeyContent), NULL, 0, NULL);
564 todo_wine
565 ok(!ret, "Expected failure\n");
566 /* Finally, a message signed with a valid public key verifies successfully
567 */
568 cbDecoded = 0xdeadbeef;
569 ret = CryptVerifyMessageSignature(¶, 0,
570 signedWithCertWithValidPubKeyContent,
571 sizeof(signedWithCertWithValidPubKeyContent), NULL, &cbDecoded, NULL);
572 ok(ret, "CryptVerifyMessageSignature failed: %08lx\n", GetLastError());
573 ok(cbDecoded == sizeof(msgData), "expected 4, got %ld\n", cbDecoded);
574 cbDecoded = 0;
575 ret = CryptVerifyMessageSignature(¶, 0,
576 signedWithCertWithValidPubKeyContent,
577 sizeof(signedWithCertWithValidPubKeyContent), NULL, &cbDecoded, NULL);
578 /* Setting cbDecoded to 0 succeeds when a NULL buffer is provided */
579 ok(ret, "CryptVerifyMessageSignature failed: %08lx\n", GetLastError());
580 ok(cbDecoded == sizeof(msgData), "expected 4, got %ld\n", cbDecoded);
581 cbDecoded = 0;
582 ret = CryptVerifyMessageSignature(¶, 0,
583 signedWithCertWithValidPubKeyContent,
584 sizeof(signedWithCertWithValidPubKeyContent), decoded, &cbDecoded, NULL);
585 /* When a non-NULL buffer is provided, cbDecoded must not be too small */
586 ok(!ret && GetLastError() == ERROR_MORE_DATA,
587 "expected ERROR_MORE_DATA, got %ld (%08lx)\n", GetLastError(),
588 GetLastError());
589}
590
591static const BYTE detachedHashBlob[] = {
5920x30,0x3f,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x05,0xa0,0x32,
5930x30,0x30,0x02,0x01,0x00,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,
5940x02,0x05,0x05,0x00,0x30,0x0b,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,
5950x07,0x01,0x04,0x10,0x2d,0x1b,0xbc,0x1f,0xc7,0xab,0x36,0x8d,0xdb,0x95,0xe6,
5960x24,0xb9,0x66,0x7c,0x21 };
597static const BYTE hashBlob[] = {
5980x30,0x47,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x05,0xa0,0x3a,
5990x30,0x38,0x02,0x01,0x00,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,
6000x02,0x05,0x05,0x00,0x30,0x13,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,
6010x07,0x01,0xa0,0x06,0x04,0x04,0xde,0xad,0xbe,0xef,0x04,0x10,0x2f,0x24,0x92,
6020x30,0xa8,0xe7,0xc2,0xbf,0x60,0x05,0xcc,0xd2,0x67,0x92,0x59,0xec };
603static const BYTE hashVal[] = {
6040x2d,0x1b,0xbc,0x1f,0xc7,0xab,0x36,0x8d,0xdb,0x95,0xe6,0x24,0xb9,0x66,0x7c,
6050x21 };
606
607static void test_hash_message(void)
608{
609 BOOL ret;
610 CRYPT_HASH_MESSAGE_PARA para;
611 static const BYTE blob1[] = { 0xde, 0xad, 0xbe, 0xef };
612 static const BYTE blob2[] = { 0xba, 0xad, 0xf0, 0x0d };
613 const BYTE *toHash[] = { blob1, blob2 };
614 DWORD hashSize[] = { sizeof(blob1), sizeof(blob2) };
615 DWORD hashedBlobSize, computedHashSize;
616 static char oid_rsa_md5[] = szOID_RSA_MD5;
617 LPBYTE hashedBlob, computedHash;
618
619 /* Crash
620 ret = CryptHashMessage(NULL, FALSE, 0, NULL, 0, NULL, NULL, NULL, NULL);
621 */
622 memset(¶, 0, sizeof(para));
623 SetLastError(0xdeadbeef);
624 ret = CryptHashMessage(¶, FALSE, 0, NULL, NULL, NULL, NULL, NULL, NULL);
625 ok(!ret && GetLastError() == E_INVALIDARG,
626 "expected E_INVALIDARG, got 0x%08lx\n", GetLastError());
627 para.cbSize = sizeof(para);
628 /* Not quite sure what "success" means in this case, but it does succeed */
629 SetLastError(0xdeadbeef);
630 ret = CryptHashMessage(¶, FALSE, 0, NULL, NULL, NULL, NULL, NULL, NULL);
631 ok(ret, "CryptHashMessage failed: 0x%08lx\n", GetLastError());
632 /* With a bogus encoding type it "succeeds" */
633 para.dwMsgEncodingType = 0xdeadbeef;
634 SetLastError(0xdeadbeef);
635 ret = CryptHashMessage(¶, FALSE, 0, NULL, NULL, NULL, NULL, NULL, NULL);
636 ok(ret, "CryptHashMessage failed: 0x%08lx\n", GetLastError());
637 /* According to MSDN, the third parameter (cToBeHashed) must be 1 if the
638 * second parameter (fDetached) is FALSE, but again it "succeeds."
639 */
640 SetLastError(0xdeadbeef);
641 ret = CryptHashMessage(¶, FALSE, 2, NULL, NULL, NULL, NULL, NULL, NULL);
642 ok(ret, "CryptHashMessage failed: 0x%08lx\n", GetLastError());
643 /* Even passing parameters to hash results in "success." */
644 SetLastError(0xdeadbeef);
645 ret = CryptHashMessage(¶, FALSE, 2, toHash, hashSize, NULL, NULL, NULL,
646 NULL);
647 ok(ret, "CryptHashMessage failed: 0x%08lx\n", GetLastError());
648 /* Try again with a valid encoding type */
649 para.dwMsgEncodingType = PKCS_7_ASN_ENCODING;
650 SetLastError(0xdeadbeef);
651 ret = CryptHashMessage(¶, FALSE, 2, NULL, NULL, NULL, NULL, NULL, NULL);
652 ok(ret, "CryptHashMessage failed: 0x%08lx\n", GetLastError());
653 /* And with valid data to hash */
654 SetLastError(0xdeadbeef);
655 ret = CryptHashMessage(¶, FALSE, 2, toHash, hashSize, NULL, NULL, NULL,
656 NULL);
657 ok(ret, "CryptHashMessage failed: 0x%08lx\n", GetLastError());
658 /* But requesting the size of the hashed blob and indicating there's data
659 * to hash results in a crash
660 */
661 if (0)
662 {
663 CryptHashMessage(¶, FALSE, 2, NULL, NULL, NULL,
664 &hashedBlobSize, NULL, NULL);
665 }
666 /* Passing a valid pointer for the data to hash fails, as the hash
667 * algorithm is finally checked.
668 */
669 SetLastError(0xdeadbeef);
670 ret = CryptHashMessage(¶, FALSE, 2, toHash, hashSize, NULL,
671 &hashedBlobSize, NULL, NULL);
672 ok(!ret &&
673 (GetLastError() == CRYPT_E_UNKNOWN_ALGO ||
674 GetLastError() == CRYPT_E_OID_FORMAT), /* Vista */
675 "expected CRYPT_E_UNKNOWN_ALGO or CRYPT_E_OID_FORMAT, got 0x%08lx (%ld)\n",
676 GetLastError(), GetLastError());
677 para.HashAlgorithm.pszObjId = oid_rsa_md5;
678 /* With a valid hash algorithm, this succeeds, even though fDetached is
679 * FALSE.
680 */
681 SetLastError(0xdeadbeef);
682 ret = CryptHashMessage(¶, FALSE, 2, toHash, hashSize, NULL,
683 &hashedBlobSize, NULL, NULL);
684 todo_wine
685 ok(ret, "CryptHashMessage failed: 0x%08lx\n", GetLastError());
686 if (ret)
687 {
688 /* Actually attempting to get the hashed data fails, perhaps because
689 * detached is FALSE.
690 */
691 hashedBlob = malloc(hashedBlobSize);
692 SetLastError(0xdeadbeef);
693 ret = CryptHashMessage(¶, FALSE, 2, toHash, hashSize, hashedBlob,
694 &hashedBlobSize, NULL, NULL);
695 ok(!ret && GetLastError() == CRYPT_E_MSG_ERROR,
696 "expected CRYPT_E_MSG_ERROR, got 0x%08lx (%ld)\n", GetLastError(),
697 GetLastError());
698 free(hashedBlob);
699 }
700 /* Repeating tests with fDetached = TRUE results in success */
701 SetLastError(0xdeadbeef);
702 ret = CryptHashMessage(¶, TRUE, 2, toHash, hashSize, NULL,
703 &hashedBlobSize, NULL, NULL);
704 ok(ret, "CryptHashMessage failed: 0x%08lx\n", GetLastError());
705 if (ret)
706 {
707 hashedBlob = malloc(hashedBlobSize);
708 SetLastError(0xdeadbeef);
709 ret = CryptHashMessage(¶, TRUE, 2, toHash, hashSize, hashedBlob,
710 &hashedBlobSize, NULL, NULL);
711 ok(ret, "CryptHashMessage failed: 0x%08lx\n", GetLastError());
712 ok(hashedBlobSize == sizeof(detachedHashBlob),
713 "unexpected size of detached blob %ld\n", hashedBlobSize);
714 ok(!memcmp(hashedBlob, detachedHashBlob, hashedBlobSize),
715 "unexpected detached blob value\n");
716 free(hashedBlob);
717 }
718 /* Hashing a single item with fDetached = FALSE also succeeds */
719 SetLastError(0xdeadbeef);
720 ret = CryptHashMessage(¶, FALSE, 1, toHash, hashSize, NULL,
721 &hashedBlobSize, NULL, NULL);
722 ok(ret, "CryptHashMessage failed: 0x%08lx\n", GetLastError());
723 if (ret)
724 {
725 hashedBlob = malloc(hashedBlobSize);
726 ret = CryptHashMessage(¶, FALSE, 1, toHash, hashSize, hashedBlob,
727 &hashedBlobSize, NULL, NULL);
728 ok(ret, "CryptHashMessage failed: 0x%08lx\n", GetLastError());
729 ok(hashedBlobSize == sizeof(hashBlob),
730 "unexpected size of detached blob %ld\n", hashedBlobSize);
731 ok(!memcmp(hashedBlob, hashBlob, hashedBlobSize),
732 "unexpected detached blob value\n");
733 free(hashedBlob);
734 }
735 /* Check the computed hash value too. You don't need to get the encoded
736 * blob to get it.
737 */
738 computedHashSize = 0xdeadbeef;
739 ret = CryptHashMessage(¶, TRUE, 2, toHash, hashSize, NULL,
740 &hashedBlobSize, NULL, &computedHashSize);
741 ok(ret, "CryptHashMessage failed: 0x%08lx\n", GetLastError());
742 ok(computedHashSize == 16, "expected hash size of 16, got %ld\n",
743 computedHashSize);
744 if (ret)
745 {
746 computedHash = malloc(computedHashSize);
747 SetLastError(0xdeadbeef);
748 ret = CryptHashMessage(¶, TRUE, 2, toHash, hashSize, NULL,
749 &hashedBlobSize, computedHash, &computedHashSize);
750 ok(ret, "CryptHashMessage failed: 0x%08lx\n", GetLastError());
751 ok(computedHashSize == sizeof(hashVal),
752 "unexpected size of hash value %ld\n", computedHashSize);
753 ok(!memcmp(computedHash, hashVal, computedHashSize),
754 "unexpected value\n");
755 free(computedHash);
756 }
757}
758
759static const BYTE publicPrivateKeyPair[] = {
7600x07,0x02,0x00,0x00,0x00,0x24,0x00,0x00,0x52,0x53,0x41,0x32,0x00,0x02,0x00,
7610x00,0x01,0x00,0x01,0x00,0x9b,0xd9,0x60,0xd9,0x5b,0x09,0x50,0x9e,0x09,0x94,
7620x1e,0x6a,0x06,0x1d,0xdd,0x39,0xc5,0x96,0x17,0xe3,0xb9,0x0c,0x71,0x9c,0xf7,
7630xc1,0x07,0x7b,0xd7,0x4a,0xaa,0x8a,0x3e,0xcd,0x78,0x3c,0x4c,0x95,0x98,0x28,
7640x29,0x2d,0xe0,0xfc,0xe6,0x4f,0x95,0xca,0x87,0x92,0xdd,0xa3,0x8d,0xf0,0x39,
7650xf3,0x1b,0x87,0x64,0x82,0x99,0xc0,0xa9,0xe8,0x87,0x86,0x2e,0x72,0x07,0x07,
7660x8f,0x45,0x54,0x51,0x2f,0x51,0xd0,0x60,0x97,0x48,0x54,0x0e,0x78,0xb5,0x7e,
7670x2b,0x9d,0xca,0x81,0xa8,0xa8,0x00,0x57,0x69,0xa6,0xf7,0x4d,0x45,0xe0,0xf7,
7680xfa,0xd2,0xeb,0xaa,0xb8,0x06,0x34,0xce,0xf0,0x9d,0x2b,0x76,0x8a,0x4f,0x70,
7690x51,0x90,0x33,0x72,0xcb,0x81,0x85,0x7e,0x35,0x2e,0xfb,0x81,0xf0,0xc7,0x85,
7700xa5,0x75,0xf9,0x2d,0x00,0x71,0x66,0x36,0xfe,0x22,0xd6,0xc9,0x36,0x61,0x9b,
7710x64,0x92,0xe8,0x25,0x38,0x35,0xeb,0x0c,0x84,0x83,0x76,0x42,0x90,0xf7,0x73,
7720x91,0xdc,0x43,0x83,0x07,0x77,0xc9,0x1b,0x3f,0x74,0xc0,0xbe,0x18,0x97,0xd6,
7730x86,0xe5,0xfa,0x28,0x7c,0xf7,0x8d,0x89,0xb1,0x93,0xac,0x48,0x3c,0xa1,0x02,
7740xfa,0xc6,0x1c,0xa0,0xb5,0xe8,0x4f,0xd7,0xd1,0x33,0x63,0x8b,0x7e,0xf1,0x94,
7750x56,0x07,0xbc,0x6e,0x0c,0xbd,0xa0,0x15,0xba,0x99,0x5d,0xb7,0x5e,0x09,0xf2,
7760x1b,0x46,0x85,0x61,0x91,0x6a,0x78,0x31,0xb5,0xf0,0xba,0x20,0xf5,0x7a,0xb4,
7770x8e,0xd3,0x50,0x87,0xf8,0xf3,0xe4,0xd9,0xab,0x6f,0x0e,0x59,0x42,0xac,0x7d,
7780xb1,0x8c,0xea,0x33,0x54,0x08,0x38,0xc9,0xcd,0xac,0x10,0x19,0x4a,0xba,0x89,
7790xdc,0xb6,0x73,0xef,0xec,0x56,0x93,0xd6,0xf2,0x4b,0xba,0x50,0x2d,0x8f,0x15,
7800xed,0x8b,0xb5,0x67,0xc8,0xfc,0x51,0x5f };
781static const BYTE cert1[] = {
7820x30,0x81,0xd0,0x30,0x81,0xbe,0xa0,0x03,0x02,0x01,0x02,0x02,0x10,0x20,0x42,
7830x68,0x69,0xe9,0xea,0x61,0x83,0x11,0xdf,0xc0,0x24,0x2f,0x3b,0xad,0x40,0x30,
7840x09,0x06,0x05,0x2b,0x0e,0x03,0x02,0x1d,0x05,0x00,0x30,0x0c,0x31,0x0a,0x30,
7850x08,0x06,0x03,0x55,0x04,0x03,0x13,0x01,0x4e,0x30,0x20,0x17,0x0d,0x31,0x30,
7860x30,0x39,0x31,0x34,0x31,0x33,0x31,0x39,0x30,0x39,0x5a,0x18,0x0f,0x33,0x30,
7870x31,0x30,0x30,0x39,0x31,0x34,0x31,0x33,0x31,0x39,0x30,0x39,0x5a,0x30,0x0c,
7880x31,0x0a,0x30,0x08,0x06,0x03,0x55,0x04,0x03,0x13,0x01,0x4e,0x30,0x5c,0x30,
7890x0d,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x01,0x01,0x05,0x00,0x03,
7900x4b,0x00,0x30,0x48,0x02,0x41,0x00,0xe8,0xa9,0xc0,0x99,0x82,0x64,0x87,0x1b,
7910xf3,0x39,0xf0,0x8d,0xa3,0xdd,0x92,0x87,0xca,0x95,0x4f,0xe6,0xfc,0xe0,0x2d,
7920x29,0x28,0x98,0x95,0x4c,0x3c,0x78,0xcd,0x3e,0x8a,0xaa,0x4a,0xd7,0x7b,0x07,
7930xc1,0xf7,0x9c,0x71,0x0c,0xb9,0xe3,0x17,0x96,0xc5,0x39,0xdd,0x1d,0x06,0x6a,
7940x1e,0x94,0x09,0x9e,0x50,0x09,0x5b,0xd9,0x60,0xd9,0x9b,0x02,0x03,0x01,0x00,
7950x01,0x30,0x09,0x06,0x05,0x2b,0x0e,0x03,0x02,0x1d,0x05,0x00,0x03,0x02,0x00,
7960xc1 };
797static const BYTE cert2[] = {
7980x30,0x82,0x01,0x15,0x30,0x82,0x01,0x02,0xa0,0x03,0x02,0x01,0x02,0x02,0x10,
7990x1c,0xf2,0x1f,0xec,0x6b,0xdc,0x36,0xbf,0x4a,0xd7,0xe1,0x6c,0x84,0x85,0xcd,
8000x2e,0x30,0x09,0x06,0x05,0x2b,0x0e,0x03,0x02,0x1d,0x05,0x00,0x30,0x0c,0x31,
8010x0a,0x30,0x08,0x06,0x03,0x55,0x04,0x03,0x13,0x01,0x58,0x30,0x20,0x17,0x0d,
8020x31,0x30,0x30,0x37,0x31,0x32,0x31,0x31,0x33,0x37,0x35,0x36,0x5a,0x18,0x0f,
8030x33,0x30,0x31,0x30,0x30,0x37,0x31,0x32,0x31,0x31,0x33,0x37,0x35,0x36,0x5a,
8040x30,0x0c,0x31,0x0a,0x30,0x08,0x06,0x03,0x55,0x04,0x03,0x13,0x01,0x58,0x30,
8050x81,0x9f,0x30,0x0d,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x01,0x01,
8060x05,0x00,0x03,0x81,0x8d,0x00,0x30,0x81,0x89,0x02,0x81,0x81,0x00,0xab,0xed,
8070x6e,0xe0,0x00,0x3c,0xcf,0x2d,0x2b,0xda,0x05,0x88,0x6a,0x7e,0xed,0x60,0x30,
8080x24,0xef,0x6c,0x6b,0xad,0x28,0x9b,0x14,0x90,0xf6,0xd0,0x96,0x79,0x6d,0xad,
8090xac,0x46,0x14,0x7b,0x0e,0xfe,0xa9,0x8a,0x05,0x5a,0xc8,0x84,0x38,0x44,0xf9,
8100xce,0xb2,0xe6,0xde,0x5b,0x80,0x0b,0x15,0xff,0x1b,0x60,0x3f,0xba,0xb2,0xfe,
8110x6e,0xf5,0xdc,0x54,0x33,0xfc,0xfc,0x79,0x0a,0x10,0xa4,0x23,0x6d,0x67,0xeb,
8120x16,0xb2,0x92,0xbf,0x63,0x42,0x17,0x0a,0xde,0xe6,0xab,0x8e,0xf7,0x8e,0x41,
8130x8c,0x04,0xe8,0xe2,0x38,0x73,0xd3,0x82,0xd7,0xd1,0xee,0xd3,0xa6,0x54,0x8c,
8140xcd,0x0b,0x93,0xda,0x63,0x55,0x0d,0x1f,0x68,0x5c,0x30,0xee,0xad,0x2d,0xd5,
8150x40,0x56,0xe0,0xd8,0xc7,0xef,0x02,0x03,0x01,0x00,0x01,0x30,0x09,0x06,0x05,
8160x2b,0x0e,0x03,0x02,0x1d,0x05,0x00,0x03,0x02,0x00,0x06 };
817static const BYTE crl[] = {
8180x30,0x81,0xc2,0x30,0x7e,0x02,0x01,0x01,0x30,0x0d,0x06,0x09,0x2a,0x86,0x48,
8190x86,0xf7,0x0d,0x01,0x01,0x05,0x05,0x00,0x30,0x3c,0x31,0x0b,0x30,0x09,0x06,
8200x03,0x55,0x04,0x06,0x13,0x02,0x52,0x55,0x31,0x0c,0x30,0x0a,0x06,0x03,0x55,
8210x04,0x08,0x13,0x03,0x53,0x50,0x62,0x31,0x0c,0x30,0x0a,0x06,0x03,0x55,0x04,
8220x07,0x13,0x03,0x53,0x50,0x62,0x31,0x11,0x30,0x0f,0x06,0x03,0x55,0x04,0x0a,
8230x13,0x08,0x45,0x74,0x65,0x72,0x73,0x6f,0x66,0x74,0x17,0x0d,0x31,0x30,0x30,
8240x36,0x32,0x38,0x31,0x32,0x35,0x31,0x32,0x37,0x5a,0x17,0x0d,0x31,0x30,0x30,
8250x37,0x32,0x38,0x31,0x32,0x35,0x31,0x32,0x37,0x5a,0xa0,0x0e,0x30,0x0c,0x30,
8260x0a,0x06,0x03,0x55,0x1d,0x14,0x04,0x03,0x02,0x01,0x00,0x30,0x0d,0x06,0x09,
8270x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x01,0x05,0x05,0x00,0x03,0x31,0x00,0x83,
8280x35,0x9c,0xf5,0x35,0x5c,0xc1,0x20,0x81,0x80,0x5c,0x35,0x56,0xaf,0xb3,0x27,
8290x15,0xc6,0xdd,0x24,0xe1,0xff,0xb9,0xf9,0x19,0x21,0xed,0x5e,0x1b,0xff,0x72,
8300xc3,0x33,0xf6,0x9f,0xcb,0xde,0x84,0x0b,0x12,0x84,0xad,0x48,0x90,0x9d,0xdd,
8310x89,0xbb };
832static const BYTE signedHashForEmptyMessage[] = {
8330x30,0x81,0xbb,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x02,0xa0,
8340x81,0xad,0x30,0x81,0xaa,0x02,0x01,0x01,0x31,0x0e,0x30,0x0c,0x06,0x08,0x2a,
8350x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x0b,0x06,0x09,0x2a,0x86,
8360x48,0x86,0xf7,0x0d,0x01,0x07,0x01,0x31,0x81,0x87,0x30,0x81,0x84,0x02,0x01,
8370x01,0x30,0x20,0x30,0x0c,0x31,0x0a,0x30,0x08,0x06,0x03,0x55,0x04,0x03,0x13,
8380x01,0x4e,0x02,0x10,0x20,0x42,0x68,0x69,0xe9,0xea,0x61,0x83,0x11,0xdf,0xc0,
8390x24,0x2f,0x3b,0xad,0x40,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,
8400x02,0x05,0x05,0x00,0x30,0x0d,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,
8410x01,0x01,0x05,0x00,0x04,0x40,0xe1,0xee,0xca,0x98,0x16,0x23,0x5a,0x34,0xfd,
8420x91,0x69,0x97,0x1e,0x16,0xe4,0x57,0x45,0xad,0xc9,0x5d,0x2e,0xda,0x92,0xbf,
8430xee,0x2f,0xb1,0xaa,0x32,0xfa,0x07,0x4e,0x63,0xfd,0xe1,0x52,0x17,0xd0,0xa4,
8440x49,0x30,0x54,0x4d,0x12,0xa0,0x6a,0x1c,0x64,0xea,0xc7,0x50,0x49,0xa5,0xca,
8450xc3,0x71,0xa4,0xf7,0x8c,0x25,0xe4,0x1a,0xca,0x89 };
846static const BYTE signedEmptyMessage[] = {
8470x30,0x81,0xbb,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x02,0xa0,
8480x81,0xad,0x30,0x81,0xaa,0x02,0x01,0x01,0x31,0x0e,0x30,0x0c,0x06,0x08,0x2a,
8490x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x0b,0x06,0x09,0x2a,0x86,
8500x48,0x86,0xf7,0x0d,0x01,0x07,0x01,0x31,0x81,0x87,0x30,0x81,0x84,0x02,0x01,
8510x01,0x30,0x20,0x30,0x0c,0x31,0x0a,0x30,0x08,0x06,0x03,0x55,0x04,0x03,0x13,
8520x01,0x4e,0x02,0x10,0x20,0x42,0x68,0x69,0xe9,0xea,0x61,0x83,0x11,0xdf,0xc0,
8530x24,0x2f,0x3b,0xad,0x40,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,
8540x02,0x05,0x05,0x00,0x30,0x0d,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,
8550x01,0x01,0x05,0x00,0x04,0x40,0xe1,0xee,0xca,0x98,0x16,0x23,0x5a,0x34,0xfd,
8560x91,0x69,0x97,0x1e,0x16,0xe4,0x57,0x45,0xad,0xc9,0x5d,0x2e,0xda,0x92,0xbf,
8570xee,0x2f,0xb1,0xaa,0x32,0xfa,0x07,0x4e,0x63,0xfd,0xe1,0x52,0x17,0xd0,0xa4,
8580x49,0x30,0x54,0x4d,0x12,0xa0,0x6a,0x1c,0x64,0xea,0xc7,0x50,0x49,0xa5,0xca,
8590xc3,0x71,0xa4,0xf7,0x8c,0x25,0xe4,0x1a,0xca,0x89 };
860static const BYTE signedHash[] = {
8610x30,0x81,0xbb,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x02,0xa0,
8620x81,0xad,0x30,0x81,0xaa,0x02,0x01,0x01,0x31,0x0e,0x30,0x0c,0x06,0x08,0x2a,
8630x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x0b,0x06,0x09,0x2a,0x86,
8640x48,0x86,0xf7,0x0d,0x01,0x07,0x01,0x31,0x81,0x87,0x30,0x81,0x84,0x02,0x01,
8650x01,0x30,0x20,0x30,0x0c,0x31,0x0a,0x30,0x08,0x06,0x03,0x55,0x04,0x03,0x13,
8660x01,0x4e,0x02,0x10,0x20,0x42,0x68,0x69,0xe9,0xea,0x61,0x83,0x11,0xdf,0xc0,
8670x24,0x2f,0x3b,0xad,0x40,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,
8680x02,0x05,0x05,0x00,0x30,0x0d,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,
8690x01,0x01,0x05,0x00,0x04,0x40,0x1e,0x04,0xa4,0xe3,0x90,0x54,0xed,0xcb,0x94,
8700xa2,0xbe,0x81,0x73,0x7e,0x05,0xf2,0x82,0xd3,0x3a,0x26,0x96,0x7a,0x53,0xcd,
8710x05,0xc3,0x09,0x69,0x3d,0x12,0x6c,0xb1,0xb0,0xab,0x0e,0xa1,0xec,0x1b,0xa1,
8720xff,0x01,0x9c,0x49,0x9f,0x4b,0x69,0x59,0x74,0x20,0x9f,0xb0,0x19,0x95,0xe7,
8730xed,0x1e,0x84,0xeb,0xe2,0x53,0x2c,0xa6,0x43,0xdf };
874static const BYTE signedHashWithCert[] = {
8750x30,0x82,0x01,0x93,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x02,
8760xa0,0x82,0x01,0x84,0x30,0x82,0x01,0x80,0x02,0x01,0x01,0x31,0x0e,0x30,0x0c,
8770x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x0b,0x06,
8780x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x01,0xa0,0x81,0xd3,0x30,0x81,
8790xd0,0x30,0x81,0xbe,0xa0,0x03,0x02,0x01,0x02,0x02,0x10,0x20,0x42,0x68,0x69,
8800xe9,0xea,0x61,0x83,0x11,0xdf,0xc0,0x24,0x2f,0x3b,0xad,0x40,0x30,0x09,0x06,
8810x05,0x2b,0x0e,0x03,0x02,0x1d,0x05,0x00,0x30,0x0c,0x31,0x0a,0x30,0x08,0x06,
8820x03,0x55,0x04,0x03,0x13,0x01,0x4e,0x30,0x20,0x17,0x0d,0x31,0x30,0x30,0x39,
8830x31,0x34,0x31,0x33,0x31,0x39,0x30,0x39,0x5a,0x18,0x0f,0x33,0x30,0x31,0x30,
8840x30,0x39,0x31,0x34,0x31,0x33,0x31,0x39,0x30,0x39,0x5a,0x30,0x0c,0x31,0x0a,
8850x30,0x08,0x06,0x03,0x55,0x04,0x03,0x13,0x01,0x4e,0x30,0x5c,0x30,0x0d,0x06,
8860x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x01,0x01,0x05,0x00,0x03,0x4b,0x00,
8870x30,0x48,0x02,0x41,0x00,0xe8,0xa9,0xc0,0x99,0x82,0x64,0x87,0x1b,0xf3,0x39,
8880xf0,0x8d,0xa3,0xdd,0x92,0x87,0xca,0x95,0x4f,0xe6,0xfc,0xe0,0x2d,0x29,0x28,
8890x98,0x95,0x4c,0x3c,0x78,0xcd,0x3e,0x8a,0xaa,0x4a,0xd7,0x7b,0x07,0xc1,0xf7,
8900x9c,0x71,0x0c,0xb9,0xe3,0x17,0x96,0xc5,0x39,0xdd,0x1d,0x06,0x6a,0x1e,0x94,
8910x09,0x9e,0x50,0x09,0x5b,0xd9,0x60,0xd9,0x9b,0x02,0x03,0x01,0x00,0x01,0x30,
8920x09,0x06,0x05,0x2b,0x0e,0x03,0x02,0x1d,0x05,0x00,0x03,0x02,0x00,0xc1,0x31,
8930x81,0x87,0x30,0x81,0x84,0x02,0x01,0x01,0x30,0x20,0x30,0x0c,0x31,0x0a,0x30,
8940x08,0x06,0x03,0x55,0x04,0x03,0x13,0x01,0x4e,0x02,0x10,0x20,0x42,0x68,0x69,
8950xe9,0xea,0x61,0x83,0x11,0xdf,0xc0,0x24,0x2f,0x3b,0xad,0x40,0x30,0x0c,0x06,
8960x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x0d,0x06,0x09,
8970x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x01,0x01,0x05,0x00,0x04,0x40,0x1e,0x04,
8980xa4,0xe3,0x90,0x54,0xed,0xcb,0x94,0xa2,0xbe,0x81,0x73,0x7e,0x05,0xf2,0x82,
8990xd3,0x3a,0x26,0x96,0x7a,0x53,0xcd,0x05,0xc3,0x09,0x69,0x3d,0x12,0x6c,0xb1,
9000xb0,0xab,0x0e,0xa1,0xec,0x1b,0xa1,0xff,0x01,0x9c,0x49,0x9f,0x4b,0x69,0x59,
9010x74,0x20,0x9f,0xb0,0x19,0x95,0xe7,0xed,0x1e,0x84,0xeb,0xe2,0x53,0x2c,0xa6,
9020x43,0xdf };
903static const BYTE signedHashWithCRL[] = {
9040x30,0x82,0x01,0x85,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x02,
9050xa0,0x82,0x01,0x76,0x30,0x82,0x01,0x72,0x02,0x01,0x01,0x31,0x0e,0x30,0x0c,
9060x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x0b,0x06,
9070x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x01,0xa1,0x81,0xc5,0x30,0x81,
9080xc2,0x30,0x7e,0x02,0x01,0x01,0x30,0x0d,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,
9090x0d,0x01,0x01,0x05,0x05,0x00,0x30,0x3c,0x31,0x0b,0x30,0x09,0x06,0x03,0x55,
9100x04,0x06,0x13,0x02,0x52,0x55,0x31,0x0c,0x30,0x0a,0x06,0x03,0x55,0x04,0x08,
9110x13,0x03,0x53,0x50,0x62,0x31,0x0c,0x30,0x0a,0x06,0x03,0x55,0x04,0x07,0x13,
9120x03,0x53,0x50,0x62,0x31,0x11,0x30,0x0f,0x06,0x03,0x55,0x04,0x0a,0x13,0x08,
9130x45,0x74,0x65,0x72,0x73,0x6f,0x66,0x74,0x17,0x0d,0x31,0x30,0x30,0x36,0x32,
9140x38,0x31,0x32,0x35,0x31,0x32,0x37,0x5a,0x17,0x0d,0x31,0x30,0x30,0x37,0x32,
9150x38,0x31,0x32,0x35,0x31,0x32,0x37,0x5a,0xa0,0x0e,0x30,0x0c,0x30,0x0a,0x06,
9160x03,0x55,0x1d,0x14,0x04,0x03,0x02,0x01,0x00,0x30,0x0d,0x06,0x09,0x2a,0x86,
9170x48,0x86,0xf7,0x0d,0x01,0x01,0x05,0x05,0x00,0x03,0x31,0x00,0x83,0x35,0x9c,
9180xf5,0x35,0x5c,0xc1,0x20,0x81,0x80,0x5c,0x35,0x56,0xaf,0xb3,0x27,0x15,0xc6,
9190xdd,0x24,0xe1,0xff,0xb9,0xf9,0x19,0x21,0xed,0x5e,0x1b,0xff,0x72,0xc3,0x33,
9200xf6,0x9f,0xcb,0xde,0x84,0x0b,0x12,0x84,0xad,0x48,0x90,0x9d,0xdd,0x89,0xbb,
9210x31,0x81,0x87,0x30,0x81,0x84,0x02,0x01,0x01,0x30,0x20,0x30,0x0c,0x31,0x0a,
9220x30,0x08,0x06,0x03,0x55,0x04,0x03,0x13,0x01,0x4e,0x02,0x10,0x20,0x42,0x68,
9230x69,0xe9,0xea,0x61,0x83,0x11,0xdf,0xc0,0x24,0x2f,0x3b,0xad,0x40,0x30,0x0c,
9240x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x0d,0x06,
9250x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x01,0x01,0x05,0x00,0x04,0x40,0x1e,
9260x04,0xa4,0xe3,0x90,0x54,0xed,0xcb,0x94,0xa2,0xbe,0x81,0x73,0x7e,0x05,0xf2,
9270x82,0xd3,0x3a,0x26,0x96,0x7a,0x53,0xcd,0x05,0xc3,0x09,0x69,0x3d,0x12,0x6c,
9280xb1,0xb0,0xab,0x0e,0xa1,0xec,0x1b,0xa1,0xff,0x01,0x9c,0x49,0x9f,0x4b,0x69,
9290x59,0x74,0x20,0x9f,0xb0,0x19,0x95,0xe7,0xed,0x1e,0x84,0xeb,0xe2,0x53,0x2c,
9300xa6,0x43,0xdf };
931static const BYTE signedData[] = {
9320x30,0x81,0xc3,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x02,0xa0,
9330x81,0xb5,0x30,0x81,0xb2,0x02,0x01,0x01,0x31,0x0e,0x30,0x0c,0x06,0x08,0x2a,
9340x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x13,0x06,0x09,0x2a,0x86,
9350x48,0x86,0xf7,0x0d,0x01,0x07,0x01,0xa0,0x06,0x04,0x04,0x01,0x02,0x03,0x04,
9360x31,0x81,0x87,0x30,0x81,0x84,0x02,0x01,0x01,0x30,0x20,0x30,0x0c,0x31,0x0a,
9370x30,0x08,0x06,0x03,0x55,0x04,0x03,0x13,0x01,0x4e,0x02,0x10,0x20,0x42,0x68,
9380x69,0xe9,0xea,0x61,0x83,0x11,0xdf,0xc0,0x24,0x2f,0x3b,0xad,0x40,0x30,0x0c,
9390x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x0d,0x06,
9400x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x01,0x01,0x05,0x00,0x04,0x40,0xe4,
9410x69,0xf5,0x62,0xfb,0x3a,0x7d,0x1c,0x7b,0x8b,0xcc,0xfc,0x6e,0x8e,0x91,0x85,
9420xcf,0x3c,0xb8,0xfd,0x8a,0xac,0x81,0x96,0xa0,0x42,0xac,0x88,0xc4,0x48,0xe8,
9430x43,0x64,0xd1,0x38,0xd2,0x6c,0xc4,0xd4,0x9b,0x9a,0xd4,0x33,0x02,0xef,0x88,
9440xef,0x98,0x2d,0xac,0xad,0xc1,0x93,0x60,0xc4,0x3a,0xdc,0xa7,0xd6,0x97,0x70,
9450x01,0xc1,0x84 };
946
947static void test_sign_message(void)
948{
949 BOOL ret;
950 CRYPT_SIGN_MESSAGE_PARA para;
951 static char oid_rsa_md5[] = szOID_RSA_MD5;
952 static const BYTE blob1[] = { 0x01, 0x02, 0x03, 0x04 };
953 static const BYTE blob2[] = { 0x11, 0x12, 0x13, 0x14 };
954 const BYTE *toSign[] = { blob1, blob2 };
955 DWORD signSize[] = { sizeof(blob1), sizeof(blob2) };
956 LPBYTE signedBlob;
957 DWORD signedBlobSize;
958 PCCRL_CONTEXT crlContext;
959 CERT_KEY_CONTEXT keyContext;
960 HCRYPTPROV hCryptProv = 0;
961 HCRYPTKEY hKey = 0;
962
963 memset(¶, 0, sizeof(para));
964 SetLastError(0xdeadbeef);
965 ret = CryptSignMessage(¶, FALSE, 0, NULL, NULL, NULL, &signedBlobSize);
966 ok(!ret &&
967 (GetLastError() == E_INVALIDARG ||
968 GetLastError() == ERROR_ARITHMETIC_OVERFLOW), /* Win7 */
969 "expected E_INVALIDARG or ERROR_ARITHMETIC_OVERFLOW, got %08lx\n",
970 GetLastError());
971 para.cbSize = sizeof(para);
972 para.dwMsgEncodingType = X509_ASN_ENCODING;
973 SetLastError(0xdeadbeef);
974 signedBlobSize = 255;
975 ret = CryptSignMessage(¶, FALSE, 0, NULL, NULL, NULL, &signedBlobSize);
976 ok(!ret && GetLastError() == E_INVALIDARG,
977 "expected E_INVALIDARG, got %08lx\n", GetLastError());
978 ok(!signedBlobSize, "unexpected size %ld\n", signedBlobSize);
979 para.dwMsgEncodingType = PKCS_7_ASN_ENCODING;
980 SetLastError(0xdeadbeef);
981 signedBlobSize = 0;
982 ret = CryptSignMessage(¶, FALSE, 0, NULL, NULL, NULL, &signedBlobSize);
983 ok(ret, "CryptSignMessage failed: %08lx\n", GetLastError());
984 todo_wine
985 ok(signedBlobSize, "bad size\n");
986
987 SetLastError(0xdeadbeef);
988 ret = CryptAcquireContextA(&hCryptProv, NULL, NULL, PROV_RSA_FULL,
989 CRYPT_VERIFYCONTEXT);
990 ok(ret, "CryptAcquireContextA failed: %08lx\n", GetLastError());
991 SetLastError(0xdeadbeef);
992 ret = CryptImportKey(hCryptProv, publicPrivateKeyPair,
993 sizeof(publicPrivateKeyPair), 0, 0, &hKey);
994 ok(ret, "CryptImportKey failed: %08lx\n", GetLastError());
995
996 para.dwMsgEncodingType = X509_ASN_ENCODING | PKCS_7_ASN_ENCODING;
997 SetLastError(0xdeadbeef);
998 para.pSigningCert = CertCreateCertificateContext(X509_ASN_ENCODING |
999 PKCS_7_ASN_ENCODING, cert1, sizeof(cert1));
1000 ok(para.pSigningCert != NULL, "CertCreateCertificateContext failed: %08lx\n",
1001 GetLastError());
1002 para.HashAlgorithm.pszObjId = oid_rsa_md5;
1003
1004 memset(&keyContext, 0, sizeof(keyContext));
1005 keyContext.cbSize = sizeof(keyContext);
1006 keyContext.hCryptProv = hCryptProv;
1007 keyContext.dwKeySpec = AT_SIGNATURE;
1008 SetLastError(0xdeadbeef);
1009 ret = CertSetCertificateContextProperty(para.pSigningCert,
1010 CERT_KEY_CONTEXT_PROP_ID, 0, &keyContext);
1011 ok(ret, "CertSetCertificateContextProperty failed: %08lx\n", GetLastError());
1012
1013 SetLastError(0xdeadbeef);
1014 signedBlobSize = 0;
1015 ret = CryptSignMessage(¶, TRUE, 0, NULL, NULL, NULL, &signedBlobSize);
1016 ok(ret, "CryptSignMessage failed: %08lx\n", GetLastError());
1017 signedBlob = CryptMemAlloc(signedBlobSize);
1018 if (signedBlob)
1019 {
1020 SetLastError(0xdeadbeef);
1021 ret = CryptSignMessage(¶, TRUE, 0, NULL, NULL, signedBlob,
1022 &signedBlobSize);
1023 ok(ret, "CryptSignMessage failed: %08lx\n", GetLastError());
1024 ok(signedBlobSize == sizeof(signedHashForEmptyMessage),
1025 "unexpected size %ld\n", signedBlobSize);
1026 ok(!memcmp(signedBlob, signedHashForEmptyMessage, signedBlobSize),
1027 "unexpected value\n");
1028 CryptMemFree(signedBlob);
1029 }
1030
1031 SetLastError(0xdeadbeef);
1032 signedBlobSize = 0;
1033 ret = CryptSignMessage(¶, FALSE, 0, NULL, NULL, NULL, &signedBlobSize);
1034 ok(ret, "CryptSignMessage failed: %08lx\n", GetLastError());
1035 signedBlob = CryptMemAlloc(signedBlobSize);
1036 if (signedBlob)
1037 {
1038 SetLastError(0xdeadbeef);
1039 ret = CryptSignMessage(¶, FALSE, 0, NULL, NULL, signedBlob,
1040 &signedBlobSize);
1041 ok(ret, "CryptSignMessage failed: %08lx\n", GetLastError());
1042 ok(signedBlobSize == sizeof(signedEmptyMessage), "unexpected size %ld\n",
1043 signedBlobSize);
1044 ok(!memcmp(signedBlob, signedEmptyMessage, signedBlobSize),
1045 "unexpected value\n");
1046 CryptMemFree(signedBlob);
1047 }
1048
1049 SetLastError(0xdeadbeef);
1050 signedBlobSize = 0;
1051 ret = CryptSignMessage(¶, TRUE, 2, toSign, signSize, NULL,
1052 &signedBlobSize);
1053 ok(ret, "CryptSignMessage failed: %08lx\n", GetLastError());
1054 signedBlob = CryptMemAlloc(signedBlobSize);
1055 if (signedBlob)
1056 {
1057 SetLastError(0xdeadbeef);
1058 ret = CryptSignMessage(¶, TRUE, 2, toSign, signSize, signedBlob,
1059 &signedBlobSize);
1060 ok(ret, "CryptSignMessage failed: %08lx\n", GetLastError());
1061 ok(signedBlobSize == sizeof(signedHash),
1062 "unexpected size of signed blob %ld\n", signedBlobSize);
1063 ok(!memcmp(signedBlob, signedHash, signedBlobSize),
1064 "unexpected value\n");
1065 CryptMemFree(signedBlob);
1066 }
1067
1068 para.cMsgCert = 1;
1069 para.rgpMsgCert = ¶.pSigningCert;
1070
1071 SetLastError(0xdeadbeef);
1072 signedBlobSize = 0;
1073 ret = CryptSignMessage(¶, TRUE, 2, toSign, signSize, NULL,
1074 &signedBlobSize);
1075 ok(ret, "CryptSignMessage failed: %08lx\n", GetLastError());
1076 signedBlob = CryptMemAlloc(signedBlobSize);
1077 if (signedBlob)
1078 {
1079 SetLastError(0xdeadbeef);
1080 ret = CryptSignMessage(¶, TRUE, 2, toSign, signSize, signedBlob,
1081 &signedBlobSize);
1082 ok(ret, "CryptSignMessage failed: %08lx\n", GetLastError());
1083 ok(signedBlobSize == sizeof(signedHashWithCert),
1084 "unexpected size of signed blob %ld\n", signedBlobSize);
1085 ok(!memcmp(signedBlob, signedHashWithCert, signedBlobSize),
1086 "unexpected value\n");
1087 CryptMemFree(signedBlob);
1088 }
1089
1090 para.cMsgCert = 0;
1091 para.rgpMsgCert = NULL;
1092 para.cMsgCrl = 1;
1093 SetLastError(0xdeadbeef);
1094 crlContext = CertCreateCRLContext(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
1095 crl, sizeof(crl));
1096 ok(crlContext != NULL, "CertCreateCRLContext failed: %08lx\n",
1097 GetLastError());
1098 para.rgpMsgCrl = &crlContext;
1099
1100 SetLastError(0xdeadbeef);
1101 signedBlobSize = 0;
1102 ret = CryptSignMessage(¶, TRUE, 2, toSign, signSize, NULL,
1103 &signedBlobSize);
1104 ok(ret, "CryptSignMessage failed: %08lx\n", GetLastError());
1105 signedBlob = CryptMemAlloc(signedBlobSize);
1106 if (signedBlob)
1107 {
1108 SetLastError(0xdeadbeef);
1109 ret = CryptSignMessage(¶, TRUE, 2, toSign, signSize, signedBlob,
1110 &signedBlobSize);
1111 ok(ret, "CryptSignMessage failed: %08lx\n", GetLastError());
1112 ok(signedBlobSize == sizeof(signedHashWithCRL),
1113 "unexpected size of signed blob %ld\n", signedBlobSize);
1114 ok(!memcmp(signedBlob, signedHashWithCRL, signedBlobSize),
1115 "unexpected value\n");
1116 CryptMemFree(signedBlob);
1117 }
1118
1119 CertFreeCRLContext(crlContext);
1120 para.cMsgCrl = 0;
1121 para.rgpMsgCrl = NULL;
1122
1123 SetLastError(0xdeadbeef);
1124 signedBlobSize = 0;
1125 ret = CryptSignMessage(¶, FALSE, 1, toSign, signSize, NULL,
1126 &signedBlobSize);
1127 ok(ret, "CryptSignMessage failed: %08lx\n", GetLastError());
1128 signedBlob = CryptMemAlloc(signedBlobSize);
1129 if (signedBlob)
1130 {
1131 SetLastError(0xdeadbeef);
1132 ret = CryptSignMessage(¶, FALSE, 1, toSign, signSize, signedBlob,
1133 &signedBlobSize);
1134 ok(ret, "CryptSignMessage failed: %08lx\n", GetLastError());
1135 ok(signedBlobSize == sizeof(signedData),
1136 "unexpected size of signed blob %ld\n", signedBlobSize);
1137 ok(!memcmp(signedBlob, signedData, signedBlobSize),
1138 "unexpected value\n");
1139 CryptMemFree(signedBlob);
1140 }
1141
1142 if (para.pSigningCert)
1143 CertFreeCertificateContext(para.pSigningCert);
1144 if (hKey)
1145 CryptDestroyKey(hKey);
1146 if (hCryptProv)
1147 CryptReleaseContext(hCryptProv, 0);
1148}
1149
1150static const BYTE encryptedMessage[] = {
11510x30,0x31,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x03,0xa0,0x24,
11520x30,0x22,0x02,0x01,0x00,0x31,0x00,0x30,0x1b,0x06,0x09,0x2a,0x86,0x48,0x86,
11530xf7,0x0d,0x01,0x07,0x01,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,
11540x03,0x04,0x05,0x00,0x80,0x00 };
1155
1156static void test_encrypt_message(void)
1157{
1158 BOOL ret;
1159 CRYPT_ENCRYPT_MESSAGE_PARA para;
1160 static char oid_rsa_rc4[] = szOID_RSA_RC4;
1161 static const BYTE blob[] = { 0x01, 0x02, 0x03, 0x04 };
1162 PCCERT_CONTEXT certs[2];
1163 HCRYPTPROV hCryptProv = 0;
1164 LPBYTE encryptedBlob;
1165 DWORD encryptedBlobSize;
1166
1167 SetLastError(0xdeadbeef);
1168 ret = CryptAcquireContextA(&hCryptProv, NULL, NULL, PROV_RSA_FULL,
1169 CRYPT_VERIFYCONTEXT);
1170 ok(ret, "CryptAcquireContextA failed: %08lx\n", GetLastError());
1171
1172 SetLastError(0xdeadbeef);
1173 certs[0] = CertCreateCertificateContext(X509_ASN_ENCODING |
1174 PKCS_7_ASN_ENCODING, cert1, sizeof(cert1));
1175 ok(certs[0] != NULL, "CertCreateCertificateContext failed: %08lx\n",
1176 GetLastError());
1177 SetLastError(0xdeadbeef);
1178 certs[1] = CertCreateCertificateContext(X509_ASN_ENCODING |
1179 PKCS_7_ASN_ENCODING, cert2, sizeof(cert2));
1180 ok(certs[1] != NULL, "CertCreateCertificateContext failed: %08lx\n",
1181 GetLastError());
1182
1183 memset(¶, 0, sizeof(para));
1184 SetLastError(0xdeadbeef);
1185 encryptedBlobSize = 255;
1186 ret = CryptEncryptMessage(¶, 0, NULL, NULL, 0, NULL,
1187 &encryptedBlobSize);
1188 ok(!ret && GetLastError() == E_INVALIDARG,
1189 "expected E_INVALIDARG, got %08lx\n", GetLastError());
1190 ok(!encryptedBlobSize, "unexpected size %ld\n", encryptedBlobSize);
1191 para.cbSize = sizeof(para);
1192 para.dwMsgEncodingType = X509_ASN_ENCODING;
1193 SetLastError(0xdeadbeef);
1194 encryptedBlobSize = 255;
1195 ret = CryptEncryptMessage(¶, 0, NULL, NULL, 0, NULL,
1196 &encryptedBlobSize);
1197 ok(!ret && GetLastError() == E_INVALIDARG,
1198 "expected E_INVALIDARG, got %08lx\n", GetLastError());
1199 ok(!encryptedBlobSize, "unexpected size %ld\n", encryptedBlobSize);
1200 para.dwMsgEncodingType = PKCS_7_ASN_ENCODING;
1201 SetLastError(0xdeadbeef);
1202 encryptedBlobSize = 255;
1203 ret = CryptEncryptMessage(¶, 0, NULL, NULL, 0, NULL,
1204 &encryptedBlobSize);
1205 ok(!ret && GetLastError() == CRYPT_E_UNKNOWN_ALGO,
1206 "expected CRYPT_E_UNKNOWN_ALGO or E_INVALIDARG, got %08lx\n",
1207 GetLastError());
1208 ok(!encryptedBlobSize, "unexpected size %ld\n", encryptedBlobSize);
1209
1210 para.hCryptProv = hCryptProv;
1211 para.ContentEncryptionAlgorithm.pszObjId = oid_rsa_rc4;
1212
1213 SetLastError(0xdeadbeef);
1214 encryptedBlobSize = 0;
1215 ret = CryptEncryptMessage(¶, 0, NULL, NULL, 0, NULL,
1216 &encryptedBlobSize);
1217 ok(ret, "CryptEncryptMessage failed: %08lx\n", GetLastError());
1218 if (ret)
1219 {
1220 encryptedBlob = CryptMemAlloc(encryptedBlobSize);
1221 if (encryptedBlob)
1222 {
1223 SetLastError(0xdeadbeef);
1224 ret = CryptEncryptMessage(¶, 0, NULL, NULL, 0, encryptedBlob,
1225 &encryptedBlobSize);
1226 ok(ret, "CryptEncryptMessage failed: %08lx\n", GetLastError());
1227 ok(encryptedBlobSize == sizeof(encryptedMessage),
1228 "unexpected size of encrypted blob %ld\n", encryptedBlobSize);
1229 ok(!memcmp(encryptedBlob, encryptedMessage, encryptedBlobSize),
1230 "unexpected value\n");
1231 CryptMemFree(encryptedBlob);
1232 }
1233 }
1234
1235 SetLastError(0xdeadbeef);
1236 encryptedBlobSize = 0;
1237 ret = CryptEncryptMessage(¶, 2, certs, NULL, 0, NULL,
1238 &encryptedBlobSize);
1239 ok(ret, "CryptEncryptMessage failed: %08lx\n", GetLastError());
1240 if (ret)
1241 {
1242 encryptedBlob = CryptMemAlloc(encryptedBlobSize);
1243 if (encryptedBlob)
1244 {
1245 SetLastError(0xdeadbeef);
1246 ret = CryptEncryptMessage(¶, 2, certs, NULL, 0, encryptedBlob,
1247 &encryptedBlobSize);
1248 ok(ret, "CryptEncryptMessage failed: %08lx\n", GetLastError());
1249 CryptMemFree(encryptedBlob);
1250 }
1251 }
1252
1253 SetLastError(0xdeadbeef);
1254 encryptedBlobSize = 0;
1255 ret = CryptEncryptMessage(¶, 0, NULL, blob, sizeof(blob), NULL,
1256 &encryptedBlobSize);
1257 ok(ret, "CryptEncryptMessage failed: %08lx\n", GetLastError());
1258 if (ret)
1259 {
1260 encryptedBlob = CryptMemAlloc(encryptedBlobSize);
1261 if (encryptedBlob)
1262 {
1263 SetLastError(0xdeadbeef);
1264 ret = CryptEncryptMessage(¶, 0, NULL, blob, sizeof(blob),
1265 encryptedBlob, &encryptedBlobSize);
1266 ok(ret, "CryptEncryptMessage failed: %08lx\n", GetLastError());
1267 if (ret)
1268 {
1269 ok(encryptedBlobSize == 55,
1270 "unexpected size of encrypted blob %ld\n", encryptedBlobSize);
1271 }
1272 CryptMemFree(encryptedBlob);
1273 }
1274 }
1275
1276 SetLastError(0xdeadbeef);
1277 encryptedBlobSize = 0;
1278 ret = CryptEncryptMessage(¶, 2, certs, blob, sizeof(blob), NULL,
1279 &encryptedBlobSize);
1280 ok(ret, "CryptEncryptMessage failed: %08lx\n", GetLastError());
1281 if (ret)
1282 {
1283 encryptedBlob = CryptMemAlloc(encryptedBlobSize);
1284 if (encryptedBlob)
1285 {
1286 SetLastError(0xdeadbeef);
1287 ret = CryptEncryptMessage(¶, 2, certs, blob, sizeof(blob),
1288 encryptedBlob, &encryptedBlobSize);
1289 ok(ret, "CryptEncryptMessage failed: %08lx\n", GetLastError());
1290 CryptMemFree(encryptedBlob);
1291 }
1292 }
1293
1294 if (certs[0])
1295 CertFreeCertificateContext(certs[0]);
1296 if (certs[1])
1297 CertFreeCertificateContext(certs[1]);
1298 if (hCryptProv)
1299 CryptReleaseContext(hCryptProv, 0);
1300}
1301
1302START_TEST(message)
1303{
1304 test_msg_get_signer_count();
1305 test_verify_detached_message_hash();
1306 test_verify_message_hash();
1307 test_verify_detached_message_signature();
1308 test_verify_message_signature();
1309 test_hash_message();
1310 test_sign_message();
1311 test_encrypt_message();
1312}