crypt.c 50.4 KB
Newer Older
1 2 3
/* Unit test suite for wintrust crypt functions
 *
 * Copyright 2007 Paul Vriens
4
 * Copyright 2008 Hans Leidekker for CodeWeavers
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 *
 */

#include <stdarg.h>
#include <stdio.h>

#include "windows.h"
#include "mscat.h"

#include "wine/test.h"

30 31
static char selfname[MAX_PATH];
static CHAR CURR_DIR[MAX_PATH];
32 33
static CHAR catroot[MAX_PATH];
static CHAR catroot2[MAX_PATH];
34

35 36 37 38
static const WCHAR hashmeW[] = {'h','a','s','h','m','e',0};
static const WCHAR attr1W[] = {'a','t','t','r','1',0};
static const WCHAR attr2W[] = {'a','t','t','r','2',0};

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
/*
 * Minimalistic catalog file. To reconstruct, save text below as winetest.cdf,
 * convert to DOS line endings and run 'makecat /cat winetest.cdf'
 */

/*
[CatalogHeader]
Name=winetest.cat
ResultDir=.\
PublicVersion=0x00000001
EncodingType=
CATATTR1=0x10010001:attr1:value1
CATATTR2=0x10010001:attr2:value2

[CatalogFiles]
hashme=.\winetest.cdf
*/

57 58 59 60 61 62 63 64 65 66 67 68
static const CHAR test_cdf[] =
    "[CatalogHeader]\r\n"
    "Name=winetest.cat\r\n"
    "ResultDir=.\\\r\n"
    "PublicVersion=0x00000001\r\n"
    "EncodingType=\r\n"
    "CATATTR1=0x10010001:attr1:value1\r\n"
    "CATATTR2=0x10010001:attr2:value2\r\n"
    "\r\n"
    "[CatalogFiles]\r\n"
    "hashme=.\\winetest.cdf\r\n";

69
static const BYTE test_catalog[] = {
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    0x30, 0x82, 0x01, 0xbc, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x02, 0xa0,
    0x82, 0x01, 0xad, 0x30, 0x82, 0x01, 0xa9, 0x02, 0x01, 0x01, 0x31, 0x00, 0x30, 0x82, 0x01, 0x9e,
    0x06, 0x09, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x0a, 0x01, 0xa0, 0x82, 0x01, 0x8f, 0x30,
    0x82, 0x01, 0x8b, 0x30, 0x0c, 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x0c, 0x01,
    0x01, 0x04, 0x10, 0xfa, 0x55, 0x2c, 0xc2, 0xf6, 0xcc, 0xdd, 0x11, 0x2a, 0x9c, 0x00, 0x14, 0x22,
    0xec, 0x8f, 0x3b, 0x17, 0x0d, 0x30, 0x38, 0x31, 0x32, 0x31, 0x38, 0x31, 0x31, 0x32, 0x36, 0x34,
    0x38, 0x5a, 0x30, 0x0e, 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x0c, 0x01, 0x02,
    0x05, 0x00, 0x30, 0x81, 0xdd, 0x30, 0x81, 0xda, 0x04, 0x0e, 0x68, 0x00, 0x61, 0x00, 0x73, 0x00,
    0x68, 0x00, 0x6d, 0x00, 0x65, 0x00, 0x00, 0x00, 0x31, 0x81, 0xc7, 0x30, 0x61, 0x06, 0x0a, 0x2b,
    0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x02, 0x01, 0x04, 0x31, 0x53, 0x30, 0x51, 0x30, 0x2c, 0x06,
    0x0a, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x02, 0x01, 0x19, 0xa2, 0x1e, 0x80, 0x1c, 0x00,
    0x3c, 0x00, 0x3c, 0x00, 0x3c, 0x00, 0x4f, 0x00, 0x62, 0x00, 0x73, 0x00, 0x6f, 0x00, 0x6c, 0x00,
    0x65, 0x00, 0x74, 0x00, 0x65, 0x00, 0x3e, 0x00, 0x3e, 0x00, 0x3e, 0x30, 0x21, 0x30, 0x09, 0x06,
    0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14, 0xed, 0xd6, 0x9c, 0x9c, 0xb2, 0xfc,
    0xaa, 0x03, 0xe8, 0xd3, 0x20, 0xf6, 0xab, 0x28, 0xc3, 0xff, 0xbd, 0x07, 0x36, 0xf5, 0x30, 0x62,
    0x06, 0x0a, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x0c, 0x02, 0x02, 0x31, 0x54, 0x30, 0x52,
    0x1e, 0x4c, 0x00, 0x7b, 0x00, 0x44, 0x00, 0x45, 0x00, 0x33, 0x00, 0x35, 0x00, 0x31, 0x00, 0x41,
    0x00, 0x34, 0x00, 0x32, 0x00, 0x2d, 0x00, 0x38, 0x00, 0x45, 0x00, 0x35, 0x00, 0x39, 0x00, 0x2d,
    0x00, 0x31, 0x00, 0x31, 0x00, 0x44, 0x00, 0x30, 0x00, 0x2d, 0x00, 0x38, 0x00, 0x43, 0x00, 0x34,
    0x00, 0x37, 0x00, 0x2d, 0x00, 0x30, 0x00, 0x30, 0x00, 0x43, 0x00, 0x30, 0x00, 0x34, 0x00, 0x46,
    0x00, 0x43, 0x00, 0x32, 0x00, 0x39, 0x00, 0x35, 0x00, 0x45, 0x00, 0x45, 0x00, 0x7d, 0x02, 0x02,
    0x02, 0x00, 0xa0, 0x6a, 0x30, 0x68, 0x30, 0x32, 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82,
    0x37, 0x0c, 0x02, 0x01, 0x04, 0x24, 0x30, 0x22, 0x1e, 0x0a, 0x00, 0x61, 0x00, 0x74, 0x00, 0x74,
    0x00, 0x72, 0x00, 0x32, 0x02, 0x04, 0x10, 0x01, 0x00, 0x01, 0x04, 0x0e, 0x76, 0x00, 0x61, 0x00,
    0x6c, 0x00, 0x75, 0x00, 0x65, 0x00, 0x32, 0x00, 0x00, 0x00, 0x30, 0x32, 0x06, 0x0a, 0x2b, 0x06,
    0x01, 0x04, 0x01, 0x82, 0x37, 0x0c, 0x02, 0x01, 0x04, 0x24, 0x30, 0x22, 0x1e, 0x0a, 0x00, 0x61,
    0x00, 0x74, 0x00, 0x74, 0x00, 0x72, 0x00, 0x31, 0x02, 0x04, 0x10, 0x01, 0x00, 0x01, 0x04, 0x0e,
    0x76, 0x00, 0x61, 0x00, 0x6c, 0x00, 0x75, 0x00, 0x65, 0x00, 0x31, 0x00, 0x00, 0x00, 0x31, 0x00,
};

100 101
static BOOL (WINAPI * pCryptCATAdminAcquireContext)(HCATADMIN*, const GUID*, DWORD);
static BOOL (WINAPI * pCryptCATAdminReleaseContext)(HCATADMIN, DWORD);
102
static BOOL (WINAPI * pCryptCATAdminCalcHashFromFileHandle)(HANDLE hFile, DWORD*, BYTE*, DWORD);
103 104 105
static HCATINFO (WINAPI * pCryptCATAdminAddCatalog)(HCATADMIN, PWSTR, PWSTR, DWORD);
static BOOL (WINAPI * pCryptCATAdminRemoveCatalog)(HCATADMIN, LPCWSTR, DWORD);
static BOOL (WINAPI * pCryptCATAdminReleaseCatalogContext)(HCATADMIN, HCATINFO, DWORD);
106 107
static HANDLE (WINAPI * pCryptCATOpen)(LPWSTR, DWORD, HCRYPTPROV, DWORD, DWORD);
static BOOL (WINAPI * pCryptCATCatalogInfoFromContext)(HCATINFO, CATALOG_INFO *, DWORD);
108
static BOOL (WINAPI * pCryptCATCDFClose)(CRYPTCATCDF *);
109 110 111 112
static CRYPTCATATTRIBUTE * (WINAPI * pCryptCATCDFEnumCatAttributes)(CRYPTCATCDF *, CRYPTCATATTRIBUTE *,
                                                                    PFN_CDF_PARSE_ERROR_CALLBACK);
static LPWSTR (WINAPI * pCryptCATCDFEnumMembersByCDFTagEx)(CRYPTCATCDF *, LPWSTR, PFN_CDF_PARSE_ERROR_CALLBACK,
                                                           CRYPTCATMEMBER **, BOOL, LPVOID);
113
static CRYPTCATCDF * (WINAPI * pCryptCATCDFOpen)(LPWSTR, PFN_CDF_PARSE_ERROR_CALLBACK);
114
static CRYPTCATATTRIBUTE * (WINAPI * pCryptCATEnumerateCatAttr)(HANDLE, CRYPTCATATTRIBUTE *);
115 116 117
static CRYPTCATMEMBER * (WINAPI * pCryptCATEnumerateMember)(HANDLE, CRYPTCATMEMBER *);
static CRYPTCATATTRIBUTE * (WINAPI * pCryptCATEnumerateAttr)(HANDLE, CRYPTCATMEMBER *, CRYPTCATATTRIBUTE *);
static BOOL (WINAPI * pCryptCATClose)(HANDLE);
118 119
static pCryptSIPGetSignedDataMsg pGetSignedDataMsg;
static pCryptSIPPutSignedDataMsg pPutSignedDataMsg;
120

121 122 123 124
static void InitFunctionPtrs(void)
{
    HMODULE hWintrust = GetModuleHandleA("wintrust.dll");

125 126 127 128 129 130 131 132
#define WINTRUST_GET_PROC(func) \
    p ## func = (void*)GetProcAddress(hWintrust, #func); \
    if(!p ## func) { \
      trace("GetProcAddress(%s) failed\n", #func); \
    }

    WINTRUST_GET_PROC(CryptCATAdminAcquireContext)
    WINTRUST_GET_PROC(CryptCATAdminReleaseContext)
133
    WINTRUST_GET_PROC(CryptCATAdminCalcHashFromFileHandle)
134 135 136
    WINTRUST_GET_PROC(CryptCATAdminAddCatalog)
    WINTRUST_GET_PROC(CryptCATAdminRemoveCatalog)
    WINTRUST_GET_PROC(CryptCATAdminReleaseCatalogContext)
137 138
    WINTRUST_GET_PROC(CryptCATOpen)
    WINTRUST_GET_PROC(CryptCATCatalogInfoFromContext)
139
    WINTRUST_GET_PROC(CryptCATCDFClose)
140 141
    WINTRUST_GET_PROC(CryptCATCDFEnumCatAttributes)
    WINTRUST_GET_PROC(CryptCATCDFEnumMembersByCDFTagEx)
142
    WINTRUST_GET_PROC(CryptCATCDFOpen)
143
    WINTRUST_GET_PROC(CryptCATEnumerateCatAttr)
144 145 146
    WINTRUST_GET_PROC(CryptCATEnumerateMember)
    WINTRUST_GET_PROC(CryptCATEnumerateAttr)
    WINTRUST_GET_PROC(CryptCATClose)
147

148
#undef WINTRUST_GET_PROC
149 150 151 152 153 154 155 156

    pGetSignedDataMsg = (void*)GetProcAddress(hWintrust, "CryptSIPGetSignedDataMsg");
    if(!pGetSignedDataMsg)
        trace("GetProcAddress(CryptSIPGetSignedDataMsg) failed\n");

    pPutSignedDataMsg = (void*)GetProcAddress(hWintrust, "CryptSIPPutSignedDataMsg");
    if(!pPutSignedDataMsg)
        trace("GetProcAddress(CryptSIPPutSignedDataMsg) failed\n");
157 158
}

159 160
static GUID dummy = {0xdeadbeef,0xdead,0xbeef,{0xde,0xad,0xbe,0xef,0xde,0xad,0xbe,0xef}};

161 162 163 164 165
static void test_context(void)
{
    BOOL ret;
    HCATADMIN hca;
    static GUID unknown = { 0xC689AABA, 0x8E78, 0x11D0, { 0x8C,0x47,0x00,0xC0,0x4F,0xC2,0x95,0xEE }}; /* WINTRUST.DLL */
166
    CHAR dummydir[MAX_PATH];
167
    DWORD attrs;
168

169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
    /* When CryptCATAdminAcquireContext is successful it will create
     * several directories if they don't exist:
     *
     * ...\system32\CatRoot\{GUID}, this directory holds the .cat files
     * ...\system32\CatRoot2\{GUID}  (WinXP and up), here we find the catalog database for that GUID
     *
     * Windows Vista uses lowercase catroot and catroot2.
     *
     * When passed a NULL GUID it will create the following directories although on
     * WinXP and up these directories are already present when Windows is installed:
     *
     * ...\system32\CatRoot\{127D0A1D-4EF2-11D1-8608-00C04FC295EE}
     * ...\system32\CatRoot2\{127D0A1D-4EF2-11D1-8608-00C04FC295EE} (WinXP up)
     *
     * TODO: Find out what this GUID is/does.
     *
Austin English's avatar
Austin English committed
185
     * On WinXP and up there is also a TimeStamp file in some of directories that
186 187 188 189 190 191 192 193 194 195 196
     * seem to indicate the last change to the catalog database for that GUID.
     *
     * On Windows 2000 some files are created/updated:
     *
     * ...\system32\CatRoot\SYSMAST.cbk
     * ...\system32\CatRoot\SYSMAST.cbd
     * ...\system32\CatRoot\{GUID}\CATMAST.cbk
     * ...\system32\CatRoot\{GUID}\CATMAST.cbd
     *
     */

197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
    /* All NULL */
    SetLastError(0xdeadbeef);
    ret = pCryptCATAdminAcquireContext(NULL, NULL, 0);
    ok(!ret, "Expected failure\n");
    ok(GetLastError() == ERROR_INVALID_PARAMETER,
       "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());

    /* NULL GUID */
    ret = pCryptCATAdminAcquireContext(&hca, NULL, 0);
    ok(ret, "Expected success\n");
    ok(hca != NULL, "Expected a context handle, got NULL\n");

    /* All NULL */
    SetLastError(0xdeadbeef);
    ret = pCryptCATAdminReleaseContext(NULL, 0);
    ok(!ret, "Expected failure\n");
    ok(GetLastError() == ERROR_INVALID_PARAMETER,
       "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());

    /* Proper release */
    SetLastError(0xdeadbeef);
    ret = pCryptCATAdminReleaseContext(hca, 0);
219
    ok(ret, "Expected success, got FALSE with %d\n", GetLastError());
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234

    /* Try to release a second time */
    SetLastError(0xdeadbeef);
    ret = pCryptCATAdminReleaseContext(hca, 0);
    ok(!ret, "Expected failure\n");
    ok(GetLastError() == ERROR_INVALID_PARAMETER,
       "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());

    /* NULL context handle and dummy GUID */
    SetLastError(0xdeadbeef);
    ret = pCryptCATAdminAcquireContext(NULL, &dummy, 0);
    ok(!ret, "Expected failure\n");
    ok(GetLastError() == ERROR_INVALID_PARAMETER,
       "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());

235 236 237 238 239 240 241 242 243 244
    /* Correct context handle and dummy GUID
     *
     * The tests run in the past unfortunately made sure that some directories were created.
     *
     * FIXME:
     * We don't want to mess too much with these for now so we should delete only the ones
     * that shouldn't be there like the deadbeef ones. We first have to figure out if it's
     * save to remove files and directories from CatRoot/CatRoot2.
     */

245
    ret = pCryptCATAdminAcquireContext(&hca, &dummy, 0);
246 247 248 249 250 251
    ok(ret || GetLastError() == ERROR_ACCESS_DENIED, "CryptCATAdminAcquireContext failed %u\n", GetLastError());
    if (!ret && GetLastError() == ERROR_ACCESS_DENIED)
    {
        win_skip("Not running as administrator\n");
        return;
    }
252 253
    ok(hca != NULL, "Expected a context handle, got NULL\n");

254
    attrs = GetFileAttributes(catroot);
255
    ok(attrs != INVALID_FILE_ATTRIBUTES, "Expected the CatRoot directory to exist\n");
256 257 258 259 260

    /* Windows creates the GUID directory in capitals */
    lstrcpyA(dummydir, catroot);
    lstrcatA(dummydir, "\\{DEADBEEF-DEAD-BEEF-DEAD-BEEFDEADBEEF}");
    attrs = GetFileAttributes(dummydir);
261 262
    ok(attrs != INVALID_FILE_ATTRIBUTES,
       "Expected CatRoot\\{DEADBEEF-DEAD-BEEF-DEAD-BEEFDEADBEEF} directory to exist\n");
263 264 265 266 267 268 269 270 271 272 273 274

    /* Only present on XP or higher. */
    attrs = GetFileAttributes(catroot2);
    if (attrs != INVALID_FILE_ATTRIBUTES)
    {
        lstrcpyA(dummydir, catroot2);
        lstrcatA(dummydir, "\\{DEADBEEF-DEAD-BEEF-DEAD-BEEFDEADBEEF}");
        attrs = GetFileAttributes(dummydir);
        ok(attrs != INVALID_FILE_ATTRIBUTES,
            "Expected CatRoot2\\{DEADBEEF-DEAD-BEEF-DEAD-BEEFDEADBEEF} directory to exist\n");
    }

275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
    ret = pCryptCATAdminReleaseContext(hca, 0);
    ok(ret, "Expected success\n");

    /* Correct context handle and GUID */
    ret = pCryptCATAdminAcquireContext(&hca, &unknown, 0);
    ok(ret, "Expected success\n");
    ok(hca != NULL, "Expected a context handle, got NULL\n");

    ret = pCryptCATAdminReleaseContext(hca, 0);
    ok(ret, "Expected success\n");

    /* Flags not equal to 0 */
    ret = pCryptCATAdminAcquireContext(&hca, &unknown, 1);
    ok(ret, "Expected success\n");
    ok(hca != NULL, "Expected a context handle, got NULL\n");

    ret = pCryptCATAdminReleaseContext(hca, 0);
    ok(ret, "Expected success\n");
}

295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
/* TODO: Check whether SHA-1 is the algorithm that's always used */
static void test_calchash(void)
{
    BOOL ret;
    HANDLE file;
    DWORD hashsize = 0;
    BYTE* hash;
    BYTE expectedhash[20] = {0x3a,0xa1,0x19,0x08,0xec,0xa6,0x0d,0x2e,0x7e,0xcc,0x7a,0xca,0xf5,0xb8,0x2e,0x62,0x6a,0xda,0xf0,0x19};
    CHAR temp[MAX_PATH];
    DWORD written;

    /* All NULL */
    SetLastError(0xdeadbeef);
    ret = pCryptCATAdminCalcHashFromFileHandle(NULL, NULL, NULL, 0);
    ok(!ret, "Expected failure\n");
    ok(GetLastError() == ERROR_INVALID_PARAMETER,
       "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());

    /* NULL filehandle, rest is legal */
    SetLastError(0xdeadbeef);
    ret = pCryptCATAdminCalcHashFromFileHandle(NULL, &hashsize, NULL, 0);
    ok(!ret, "Expected failure\n");
    ok(GetLastError() == ERROR_INVALID_PARAMETER,
       "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());

    /* Correct filehandle, rest is NULL */
    file = CreateFileA(selfname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
    SetLastError(0xdeadbeef);
    ret = pCryptCATAdminCalcHashFromFileHandle(file, NULL, NULL, 0);
    ok(!ret, "Expected failure\n");
    ok(GetLastError() == ERROR_INVALID_PARAMETER,
       "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
    CloseHandle(file);

    /* All OK, but dwFlags set to 1 */
    file = CreateFileA(selfname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
    SetLastError(0xdeadbeef);
    ret = pCryptCATAdminCalcHashFromFileHandle(file, &hashsize, NULL, 1);
    ok(!ret, "Expected failure\n");
    ok(GetLastError() == ERROR_INVALID_PARAMETER,
       "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
    CloseHandle(file);

    /* All OK, requesting the size of the hash */
339 340
    file = CreateFileA(selfname, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
    ok(file != INVALID_HANDLE_VALUE, "CreateFile failed %u\n", GetLastError());
341 342
    SetLastError(0xdeadbeef);
    ret = pCryptCATAdminCalcHashFromFileHandle(file, &hashsize, NULL, 0);
343
    ok(ret, "Expected success %u\n", GetLastError());
344 345 346 347 348 349 350 351
    ok(hashsize == 20," Expected a hash size of 20, got %d\n", hashsize);
    ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
       "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
    CloseHandle(file);

    /* All OK, retrieve the hash
     * Double the hash buffer to see what happens to the size parameter
     */
352
    file = CreateFileA(selfname, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
353 354 355 356
    hashsize *= 2;
    hash = HeapAlloc(GetProcessHeap(), 0, hashsize);
    SetLastError(0xdeadbeef);
    ret = pCryptCATAdminCalcHashFromFileHandle(file, &hashsize, hash, 0);
357
    ok(ret, "Expected success %u\n", GetLastError());
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
    ok(hashsize == 20," Expected a hash size of 20, got %d\n", hashsize);
    ok(GetLastError() == ERROR_SUCCESS,
       "Expected ERROR_SUCCESS, got %d\n", GetLastError());
    CloseHandle(file);
    HeapFree(GetProcessHeap(), 0, hash);

    /* Do the same test with a file created and filled by ourselves (and we thus
     * have a known hash for).
     */
    GetTempFileNameA(CURR_DIR, "hsh", 0, temp); 
    file = CreateFileA(temp, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
    WriteFile(file, "Text in this file is needed to create a know hash", 49, &written, NULL);
    CloseHandle(file);

    /* All OK, first request the size and then retrieve the hash */
    file = CreateFileA(temp, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
    hashsize = 0;
    pCryptCATAdminCalcHashFromFileHandle(file, &hashsize, NULL, 0);
    hash = HeapAlloc(GetProcessHeap(), 0, hashsize);
    SetLastError(0xdeadbeef);
    ret = pCryptCATAdminCalcHashFromFileHandle(file, &hashsize, hash, 0);
    ok(ret, "Expected success\n");
    ok(GetLastError() == ERROR_SUCCESS,
       "Expected ERROR_SUCCESS, got %d\n", GetLastError());
382 383 384
    ok(hashsize == sizeof(expectedhash) &&
       !memcmp(hash, expectedhash, sizeof(expectedhash)),
       "Hashes didn't match\n");
385 386 387 388 389 390
    CloseHandle(file);

    HeapFree(GetProcessHeap(), 0, hash);
    DeleteFileA(temp);
}

391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
static void test_CryptCATOpen(void)
{
    HANDLE hcat;
    char empty[MAX_PATH];
    WCHAR emptyW[MAX_PATH];
    HANDLE file;
    BOOL ret;

    SetLastError(0xdeadbeef);
    hcat = pCryptCATOpen(NULL, 0, 0, 0, 0);
    ok(hcat == INVALID_HANDLE_VALUE, "CryptCATOpen succeeded\n");
    ok(GetLastError() == ERROR_INVALID_PARAMETER,
       "Expected ERROR_INVALID_PARAMETER, got %08x\n", GetLastError());

    if (!GetTempFileNameA(CURR_DIR, "cat", 0, empty)) return;

    file = CreateFileA(empty, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
    ok(file != INVALID_HANDLE_VALUE, "CreateFileA failed %u\n", GetLastError());
    CloseHandle(file);
    MultiByteToWideChar(CP_ACP, 0, empty, -1, emptyW, MAX_PATH);

    hcat = pCryptCATOpen(emptyW, 0, 0, 0, 0);
    todo_wine
    ok(hcat != INVALID_HANDLE_VALUE, "Expected a correct handle\n");

    ret = pCryptCATClose(hcat);
    todo_wine
    ok(ret, "CryptCATClose failed\n");
    DeleteFileA(empty);
}

422 423 424
static DWORD error_area;
static DWORD local_error;

425 426
static void WINAPI cdf_callback(DWORD area, DWORD error, WCHAR* line)
{
427 428 429 430 431
    ok(error_area != -2, "Didn't expect cdf_callback() to be called (%08x, %08x)\n",
       area, error);

    error_area = area;
    local_error = error;
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
}

static void test_CryptCATCDF_params(void)
{
    static WCHAR nonexistent[] = {'d','e','a','d','b','e','e','f','.','c','d','f',0};
    CRYPTCATCDF *catcdf;
    BOOL ret;

    if (!pCryptCATCDFOpen)
    {
        win_skip("CryptCATCDFOpen is not available\n");
        return;
    }

    SetLastError(0xdeadbeef);
    catcdf = pCryptCATCDFOpen(NULL, NULL);
    ok(catcdf == NULL, "CryptCATCDFOpen succeeded\n");
    todo_wine
    ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());

    SetLastError(0xdeadbeef);
    catcdf = pCryptCATCDFOpen(NULL, cdf_callback);
    ok(catcdf == NULL, "CryptCATCDFOpen succeeded\n");
    todo_wine
    ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());

    /* File doesn't exist */
    SetLastError(0xdeadbeef);
    catcdf = pCryptCATCDFOpen(nonexistent, cdf_callback);
    ok(catcdf == NULL, "CryptCATCDFOpen succeeded\n");
    todo_wine
    ok(GetLastError() == ERROR_FILE_NOT_FOUND, "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());

    SetLastError(0xdeadbeef);
    ret = pCryptCATCDFClose(NULL);
    ok(!ret, "Expected failure\n");
    todo_wine
    ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());

    catcdf = NULL;
    SetLastError(0xdeadbeef);
    ret = pCryptCATCDFClose(catcdf);
    ok(!ret, "Expected failure\n");
    todo_wine
    ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
}

479
/* FIXME: Once Wine can create catalog files we should use the created catalog file in this test */
480 481 482
static void test_CryptCATAdminAddRemoveCatalog(void)
{
    static WCHAR basenameW[] = {'w','i','n','e','t','e','s','t','.','c','a','t',0};
483
    static CHAR basename[] = "winetest.cat";
484 485
    HCATADMIN hcatadmin;
    HCATINFO hcatinfo;
486
    CATALOG_INFO info;
487 488
    WCHAR tmpfileW[MAX_PATH];
    char tmpfile[MAX_PATH];
489 490
    char catfile[MAX_PATH], catfilepath[MAX_PATH], *p;
    WCHAR catfileW[MAX_PATH];
491 492 493
    HANDLE file;
    DWORD error, written;
    BOOL ret;
494
    DWORD attrs;
495

496 497 498 499 500 501 502
    if (!pCryptCATAdminRemoveCatalog)
    {
        /* NT4 and W2K do have CryptCATAdminAddCatalog !! */
        win_skip("CryptCATAdminRemoveCatalog is not available\n");
        return;
    }

503 504 505
    if (!GetTempFileNameA(CURR_DIR, "cat", 0, tmpfile)) return;
    DeleteFileA(tmpfile);
    file = CreateFileA(tmpfile, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL);
506
    ok(file != INVALID_HANDLE_VALUE, "CreateFileA failed %u\n", GetLastError());
507 508 509
    CloseHandle(file);

    ret = pCryptCATAdminAcquireContext(&hcatadmin, &dummy, 0);
510 511 512 513 514 515
    ok(ret || GetLastError() == ERROR_ACCESS_DENIED, "CryptCATAdminAcquireContext failed %u\n", GetLastError());
    if (!ret && GetLastError() == ERROR_ACCESS_DENIED)
    {
        win_skip("Not running as administrator\n");
        return;
    }
516 517 518 519 520

    SetLastError(0xdeadbeef);
    hcatinfo = pCryptCATAdminAddCatalog(NULL, NULL, NULL, 0);
    error = GetLastError();
    ok(hcatinfo == NULL, "CryptCATAdminAddCatalog succeeded\n");
521
    ok(error == ERROR_INVALID_PARAMETER, "got %u expected ERROR_INVALID_PARAMETER\n", GetLastError());
522 523 524 525 526

    SetLastError(0xdeadbeef);
    hcatinfo = pCryptCATAdminAddCatalog(hcatadmin, NULL, NULL, 0);
    error = GetLastError();
    ok(hcatinfo == NULL, "CryptCATAdminAddCatalog succeeded\n");
527
    ok(error == ERROR_INVALID_PARAMETER, "got %u expected ERROR_INVALID_PARAMETER\n", GetLastError());
528 529 530 531 532 533 534 535 536 537

    MultiByteToWideChar(0, 0, tmpfile, -1, tmpfileW, MAX_PATH);

    SetLastError(0xdeadbeef);
    hcatinfo = pCryptCATAdminAddCatalog(hcatadmin, tmpfileW, basenameW, 0);
    error = GetLastError();
    todo_wine {
    ok(hcatinfo == NULL, "CryptCATAdminAddCatalog succeeded\n");
    ok(error == ERROR_BAD_FORMAT, "got %u expected ERROR_BAD_FORMAT\n", GetLastError());
    }
538 539
    if (hcatinfo != NULL)
        pCryptCATAdminReleaseCatalogContext(hcatadmin, hcatinfo, 0);
540 541 542 543 544

    SetLastError(0xdeadbeef);
    hcatinfo = pCryptCATAdminAddCatalog(hcatadmin, tmpfileW, basenameW, 1);
    error = GetLastError();
    ok(hcatinfo == NULL, "CryptCATAdminAddCatalog succeeded\n");
545
    ok(error == ERROR_INVALID_PARAMETER, "got %u expected ERROR_INVALID_PARAMETER\n", GetLastError());
546 547 548 549 550 551 552 553 554

    SetLastError(0xdeadbeef);
    hcatinfo = pCryptCATAdminAddCatalog(hcatadmin, tmpfileW, NULL, 0);
    error = GetLastError();
    ok(hcatinfo == NULL, "CryptCATAdminAddCatalog succeeded\n");
    todo_wine ok(error == ERROR_BAD_FORMAT, "got %u expected ERROR_BAD_FORMAT\n", GetLastError());

    DeleteFileA(tmpfile);
    file = CreateFileA(tmpfile, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL);
555
    ok(file != INVALID_HANDLE_VALUE, "CreateFileA failed %u\n", GetLastError());
556 557 558
    WriteFile(file, test_catalog, sizeof(test_catalog), &written, NULL);
    CloseHandle(file);

559
    /* Unique name will be created */
560 561 562
    hcatinfo = pCryptCATAdminAddCatalog(hcatadmin, tmpfileW, NULL, 0);
    todo_wine ok(hcatinfo != NULL, "CryptCATAdminAddCatalog failed %u\n", GetLastError());

563 564 565 566 567 568 569 570 571 572
    info.cbStruct = sizeof(info);
    info.wszCatalogFile[0] = 0;
    ret = pCryptCATCatalogInfoFromContext(hcatinfo, &info, 0);
    todo_wine
    {
    ok(ret, "CryptCATCatalogInfoFromContext failed %u\n", GetLastError());
    ok(info.wszCatalogFile[0] != 0, "Expected a filename\n");
    }
    WideCharToMultiByte(CP_ACP, 0, info.wszCatalogFile, -1, catfile, MAX_PATH, 0, 0);
    if ((p = strrchr(catfile, '\\'))) p++;
573
    memset(catfileW, 0, sizeof(catfileW));
574 575
    MultiByteToWideChar(0, 0, p, -1, catfileW, MAX_PATH);

576 577 578 579 580
    /* Set the file attributes so we can check what happens with them during the 'copy' */
    attrs = FILE_ATTRIBUTE_READONLY;
    ret = SetFileAttributesA(tmpfile, attrs);
    ok(ret, "SetFileAttributesA failed : %u\n", GetLastError());

581
    /* winetest.cat will be created */
582 583 584
    hcatinfo = pCryptCATAdminAddCatalog(hcatadmin, tmpfileW, basenameW, 0);
    ok(hcatinfo != NULL, "CryptCATAdminAddCatalog failed %u\n", GetLastError());

585 586 587 588
    lstrcpyA(catfilepath, catroot);
    lstrcatA(catfilepath, "\\{DEADBEEF-DEAD-BEEF-DEAD-BEEFDEADBEEF}\\winetest.cat");
    attrs = GetFileAttributes(catfilepath);
    ok(attrs != INVALID_FILE_ATTRIBUTES, "Expected %s to exist\n", catfilepath);
589 590 591 592
    todo_wine
    ok(attrs == FILE_ATTRIBUTE_SYSTEM ||
       attrs == (FILE_ATTRIBUTE_NOT_CONTENT_INDEXED | FILE_ATTRIBUTE_SYSTEM), /* Vista */
       "File has wrong attributes : %08x\n", attrs);
593

594 595 596 597
    info.cbStruct = sizeof(info);
    info.wszCatalogFile[0] = 0;
    ret = pCryptCATCatalogInfoFromContext(hcatinfo, &info, 0);
    ok(ret, "CryptCATCatalogInfoFromContext failed %u\n", GetLastError());
598 599 600 601
    ok(info.wszCatalogFile[0] != 0, "Expected a filename\n");
    WideCharToMultiByte(CP_ACP, 0, info.wszCatalogFile, -1, catfile, MAX_PATH, 0, 0);
    if ((p = strrchr(catfile, '\\'))) p++;
    ok(!lstrcmpA(basename, p), "Expected %s, got %s\n", basename, p);
602

603 604 605
    ret = pCryptCATAdminReleaseCatalogContext(hcatadmin, hcatinfo, 0);
    ok(ret, "CryptCATAdminReleaseCatalogContext failed %u\n", GetLastError());

606 607 608 609 610 611 612 613
    /* Remove the catalog file with the unique name */
    ret = pCryptCATAdminRemoveCatalog(hcatadmin, catfileW, 0);
    ok(ret, "CryptCATAdminRemoveCatalog failed %u\n", GetLastError());

    /* Remove the winetest.cat catalog file, first with the full path. This should not succeed
     * according to MSDN */
    ret = pCryptCATAdminRemoveCatalog(hcatadmin, info.wszCatalogFile, 0);
    ok(ret, "CryptCATAdminRemoveCatalog failed %u\n", GetLastError());
614
    /* The call succeeded with the full path but the file is not removed */
615 616
    attrs = GetFileAttributes(catfilepath);
    ok(attrs != INVALID_FILE_ATTRIBUTES, "Expected %s to exist\n", catfilepath);
617
    /* Given only the filename the file is removed */
618
    ret = pCryptCATAdminRemoveCatalog(hcatadmin, basenameW, 0);
619
    ok(ret, "CryptCATAdminRemoveCatalog failed %u\n", GetLastError());
620 621
    attrs = GetFileAttributes(catfilepath);
    ok(attrs == INVALID_FILE_ATTRIBUTES, "Expected %s to be removed\n", catfilepath);
622 623 624 625

    ret = pCryptCATAdminReleaseContext(hcatadmin, 0);
    ok(ret, "CryptCATAdminReleaseContext failed %u\n", GetLastError());

626 627 628
    /* Set the attributes so we can delete the file */
    attrs = FILE_ATTRIBUTE_NORMAL;
    ret = SetFileAttributesA(tmpfile, attrs);
629
    ok(ret, "SetFileAttributesA failed %u\n", GetLastError());
630 631 632
    DeleteFileA(tmpfile);
}

633
static void test_catalog_properties(CHAR *catfile, int attributes, int members)
634 635 636 637 638 639 640 641
{
    static const GUID subject = {0xde351a42,0x8e59,0x11d0,{0x8c,0x47,0x00,0xc0,0x4f,0xc2,0x95,0xee}};

    HANDLE hcat;
    CRYPTCATMEMBER *m;
    CRYPTCATATTRIBUTE *attr;
    char catalog[MAX_PATH];
    WCHAR catalogW[MAX_PATH];
642
    DWORD attrs;
643
    BOOL ret;
644
    int attrcount = 0, membercount = 0;
645

646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
    /* FIXME: Wine can't create catalog files out of catalog definition files yet. Remove this piece
     * once wine is fixed
     */
    attrs = GetFileAttributesA(catfile);
    if (attrs == INVALID_FILE_ATTRIBUTES)
    {
        HANDLE file;
        DWORD written;

        trace("Creating the catalog file\n");
        if (!GetTempFileNameA(CURR_DIR, "cat", 0, catalog)) return;
        file = CreateFileA(catalog, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
        ok(file != INVALID_HANDLE_VALUE, "CreateFileA failed %u\n", GetLastError());
        WriteFile(file, test_catalog, sizeof(test_catalog), &written, NULL);
        CloseHandle(file);

        attributes = 2;
        members = 1;
664
        MultiByteToWideChar(CP_ACP, 0, catalog, -1, catalogW, MAX_PATH);
665 666
    }
    else
667 668 669 670
    {
        MultiByteToWideChar(CP_ACP, 0, catfile, -1, catalogW, MAX_PATH);
        catalog[0] = 0;
    }
671 672

    hcat = pCryptCATOpen(catalogW, 0, 0, 0, 0);
673 674 675 676 677
    if (hcat == INVALID_HANDLE_VALUE && members == 0)
    {
        win_skip("CryptCATOpen on W2K can't handle catalog files with no members\n");
        return;
    }
678 679 680 681 682
    ok(hcat != INVALID_HANDLE_VALUE, "CryptCATOpen failed %u\n", GetLastError());

    m = pCryptCATEnumerateMember(NULL, NULL);
    ok(m == NULL, "CryptCATEnumerateMember succeeded\n");

683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
    m = NULL;
    while ((m = pCryptCATEnumerateMember(hcat, m)))
    {
        ok(m->cbStruct == sizeof(CRYPTCATMEMBER), "unexpected size %u\n", m->cbStruct);
        todo_wine ok(!lstrcmpW(m->pwszReferenceTag, hashmeW), "unexpected tag\n");
        ok(!memcmp(&m->gSubjectType, &subject, sizeof(subject)), "guid differs\n");
        ok(!m->fdwMemberFlags, "got %x expected 0\n", m->fdwMemberFlags);
        ok(m->dwCertVersion == 0x200, "got %x expected 0x200\n", m->dwCertVersion);
        ok(!m->dwReserved, "got %x expected 0\n", m->dwReserved);
        ok(m->hReserved == NULL, "got %p expected NULL\n", m->hReserved);

        attr = pCryptCATEnumerateAttr(hcat, m, NULL);
        ok(attr == NULL, "CryptCATEnumerateAttr succeeded\n");

        membercount++;
    }
699
    ok(membercount == members, "Expected %d members, got %d\n", members, membercount);
700 701 702 703 704 705 706

    attr = pCryptCATEnumerateAttr(NULL, NULL, NULL);
    ok(attr == NULL, "CryptCATEnumerateAttr succeeded\n");

    attr = pCryptCATEnumerateAttr(hcat, NULL, NULL);
    ok(attr == NULL, "CryptCATEnumerateAttr succeeded\n");

707 708 709 710 711 712 713 714 715 716
    attr = NULL;
    while ((attr = pCryptCATEnumerateCatAttr(hcat, attr)))
    {
        ok(!lstrcmpW(attr->pwszReferenceTag, attr1W) ||
           !lstrcmpW(attr->pwszReferenceTag, attr2W),
           "Expected 'attr1' or 'attr2'\n");

        attrcount++;
    }
    todo_wine
717
    ok(attrcount == attributes, "Expected %d catalog attributes, got %d\n", attributes, attrcount);
718

719 720
    ret = pCryptCATClose(hcat);
    ok(ret, "CryptCATClose failed\n");
721
    if (catalog[0]) DeleteFileA( catalog );
722
}
723

724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
static void test_create_catalog_file(void)
{
    static CHAR  catfileA[] = "winetest.cat";
    static CHAR  cdffileA[] = "winetest.cdf";
    static WCHAR cdffileW[] = {'w','i','n','e','t','e','s','t','.','c','d','f',0};
    CRYPTCATCDF *catcdf;
    CRYPTCATATTRIBUTE *catattr;
    CRYPTCATMEMBER *catmember;
    WCHAR  *catmembertag;
    DWORD written, attrs;
    HANDLE file;
    BOOL ret;
    int attrcount, membercount;

    if (!pCryptCATCDFOpen)
    {
        win_skip("CryptCATCDFOpen is not available\n");
        return;
    }

    /* Create the cdf file */
    file = CreateFileA(cdffileA, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    ok(file != INVALID_HANDLE_VALUE, "CreateFileA failed %u\n", GetLastError());
    WriteFile(file, test_cdf, sizeof(test_cdf) - 1, &written, NULL);
    CloseHandle(file);

    /* Don't enumerate attributes and members */
    trace("No attribs and members\n");
    SetLastError(0xdeadbeef);
    catcdf = pCryptCATCDFOpen(cdffileW, NULL);
    todo_wine
    {
    ok(catcdf != NULL, "CryptCATCDFOpen failed\n");
    ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
    }

    ret = pCryptCATCDFClose(catcdf);
    todo_wine
    {
    ok(ret, "Expected success\n");
    ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
    }

    attrs = GetFileAttributesA(catfileA);
    todo_wine
    ok(attrs != INVALID_FILE_ATTRIBUTES, "Expected the catalog file to exist\n");

    test_catalog_properties(catfileA, 0, 0);
    DeleteFileA(catfileA);

    /* Only enumerate the attributes */
    trace("Only attributes\n");
    attrcount = membercount = 0;
    catcdf = pCryptCATCDFOpen(cdffileW, NULL);

    catattr = NULL;
    while ((catattr = pCryptCATCDFEnumCatAttributes(catcdf, catattr, NULL)))
    {
        ok(!lstrcmpW(catattr->pwszReferenceTag, attr1W) ||
           !lstrcmpW(catattr->pwszReferenceTag, attr2W),
           "Expected 'attr1' or 'attr2'\n");

        attrcount++;
    }
    todo_wine
    ok(attrcount == 2, "Expected 2 attributes, got %d\n", attrcount);

    pCryptCATCDFClose(catcdf);
792
    /* Even though the resulting catalog file shows the attributes, they will not be enumerated */
793 794 795 796 797
    test_catalog_properties(catfileA, 0, 0);
    DeleteFileA(catfileA);

    /* Only enumerate the members */
    trace("Only members\n");
798
    membercount = 0;
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
    catcdf = pCryptCATCDFOpen(cdffileW, NULL);

    catmember = NULL;
    catmembertag = NULL;
    while ((catmembertag = pCryptCATCDFEnumMembersByCDFTagEx(catcdf, catmembertag, NULL, &catmember, FALSE, NULL)))
    {
        ok(!lstrcmpW(catmembertag, hashmeW), "Expected 'hashme'\n");
        membercount++;
    }
    todo_wine
    ok(membercount == 1, "Expected 1 member, got %d\n", membercount);

    pCryptCATCDFClose(catcdf);
    test_catalog_properties(catfileA, 0, 1);
    DeleteFileA(catfileA);

    /* Enumerate members and attributes */
    trace("Attributes and members\n");
    attrcount = membercount = 0;
    catcdf = pCryptCATCDFOpen(cdffileW, NULL);

    catattr = NULL;
    while ((catattr = pCryptCATCDFEnumCatAttributes(catcdf, catattr, NULL)))
        attrcount++;
    todo_wine
    ok(attrcount == 2, "Expected 2 attributes, got %d\n", attrcount);

    catmember = NULL;
    catmembertag = NULL;
    while ((catmembertag = pCryptCATCDFEnumMembersByCDFTagEx(catcdf, catmembertag, NULL, &catmember, FALSE, NULL)))
        membercount++;
    todo_wine
    ok(membercount == 1, "Expected 1 member, got %d\n", membercount);

    pCryptCATCDFClose(catcdf);
    test_catalog_properties(catfileA, 2, 1);
    DeleteFileA(catfileA);

    DeleteFileA(cdffileA);
838 839
}

840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905
static void create_cdf_file(const CHAR *filename, const CHAR *contents)
{
    HANDLE file;
    DWORD written;

    file = CreateFileA(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    ok(file != INVALID_HANDLE_VALUE, "CreateFileA failed %u\n", GetLastError());
    WriteFile(file, contents, lstrlenA(contents), &written, NULL);
    CloseHandle(file);
}

#define CHECK_EXPECT(a, b) \
    do { \
        ok(a == error_area, "Expected %08x, got %08x\n", a, error_area); \
        ok(b == local_error, "Expected %08x, got %08x\n", b, local_error); \
    } while (0)

/* Clear the variables (can't use 0) */
#define CLEAR_EXPECT \
    error_area = local_error = -1

/* Set both variables so the callback routine can check if a call to it was unexpected */
#define SET_UNEXPECTED \
    error_area = local_error = -2

static void test_cdf_parsing(void)
{
    static CHAR  catfileA[] = "tempfile.cat";
    static CHAR  cdffileA[] = "tempfile.cdf";
    static WCHAR cdffileW[] = {'t','e','m','p','f','i','l','e','.','c','d','f',0};
    CHAR cdf_contents[4096];
    CRYPTCATCDF *catcdf;
    CRYPTCATATTRIBUTE *catattr;
    CRYPTCATMEMBER *catmember;
    WCHAR  *catmembertag;

    if (!pCryptCATCDFOpen)
    {
        win_skip("CryptCATCDFOpen is not available\n");
        return;
    }

    /* Empty file */
    DeleteFileA(cdffileA);
    create_cdf_file(cdffileA, "");

    CLEAR_EXPECT;
    SetLastError(0xdeadbeef);
    catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
    todo_wine
    CHECK_EXPECT(CRYPTCAT_E_AREA_HEADER, CRYPTCAT_E_CDF_TAGNOTFOUND);
    ok(catcdf == NULL, "CryptCATCDFOpen succeeded\n");
    todo_wine
    ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
    DeleteFileA(cdffileA);
    ok(!DeleteFileA(catfileA), "Didn't expect a catalog file to be created\n");

    /* Just the header */
    lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
    create_cdf_file(cdffileA, cdf_contents);

    SET_UNEXPECTED;
    SetLastError(0xdeadbeef);
    catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
    ok(catcdf == NULL, "CryptCATCDFOpen succeeded\n");
    todo_wine
906
    ok(GetLastError() == ERROR_SHARING_VIOLATION,
907
        "Expected ERROR_SHARING_VIOLATION, got %d\n", GetLastError());
908 909 910 911 912 913 914 915 916 917 918 919 920
    DeleteFileA(cdffileA);

    /* Header and member only */
    lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
    lstrcatA(cdf_contents, "[CatalogFiles]\r\n");
    lstrcatA(cdf_contents, "hashme=.\\tempfile.cdf\r\n");
    create_cdf_file(cdffileA, cdf_contents);

    SET_UNEXPECTED;
    SetLastError(0xdeadbeef);
    catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
    ok(catcdf == NULL, "CryptCATCDFOpen succeeded\n");
    todo_wine
921
    ok(GetLastError() == ERROR_SHARING_VIOLATION,
922
        "Expected ERROR_SHARING_VIOLATION, got %d\n", GetLastError());
923 924 925 926 927 928 929 930 931 932 933 934 935
    DeleteFileA(cdffileA);
    ok(!DeleteFileA(catfileA), "Didn't expect a catalog file to be created\n");

    /* Header and Name (no value) */
    lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
    lstrcatA(cdf_contents, "Name=\r\n");
    create_cdf_file(cdffileA, cdf_contents);

    SET_UNEXPECTED;
    SetLastError(0xdeadbeef);
    catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
    ok(catcdf == NULL, "CryptCATCDFOpen succeeded\n");
    todo_wine
936
    ok(GetLastError() == ERROR_SHARING_VIOLATION,
937
        "Expected ERROR_SHARING_VIOLATION, got %d\n", GetLastError());
938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958
    DeleteFileA(cdffileA);
    ok(!DeleteFileA(catfileA), "Didn't expect a catalog file to be created\n");

    /* Header and Name */
    lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
    lstrcatA(cdf_contents, "Name=tempfile.cat\r\n");
    create_cdf_file(cdffileA, cdf_contents);

    SET_UNEXPECTED;
    SetLastError(0xdeadbeef);
    catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
    todo_wine
    {
    ok(catcdf != NULL, "CryptCATCDFOpen failed\n");
    ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
    }
    pCryptCATCDFClose(catcdf);
    DeleteFileA(cdffileA);
    todo_wine
    ok(DeleteFileA(catfileA), "Expected a catalog file to be created\n");

959
    /* Header and nonexistent member */
960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
    lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
    lstrcatA(cdf_contents, "Name=tempfile.cat\r\n");
    lstrcatA(cdf_contents, "[CatalogFiles]\r\n");
    lstrcatA(cdf_contents, "hashme=.\\deadbeef.cdf\r\n");
    create_cdf_file(cdffileA, cdf_contents);

    SET_UNEXPECTED;
    SetLastError(0xdeadbeef);
    catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
    todo_wine
    {
    ok(catcdf != NULL, "CryptCATCDFOpen failed\n");
    ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
    }
    /* Loop through the members */
    CLEAR_EXPECT;
    catmember = NULL;
    catmembertag = NULL;
    while ((catmembertag = pCryptCATCDFEnumMembersByCDFTagEx(catcdf, catmembertag, cdf_callback, &catmember, FALSE, NULL))) ;
    todo_wine
    CHECK_EXPECT(CRYPTCAT_E_AREA_MEMBER, CRYPTCAT_E_CDF_MEMBER_FILENOTFOUND);
    pCryptCATCDFClose(catcdf);
    DeleteFileA(cdffileA);
    todo_wine
    ok(DeleteFileA(catfileA), "Expected a catalog file to be created\n");

    /* Header, correct member but no explicit newline */
    lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
    lstrcatA(cdf_contents, "Name=tempfile.cat\r\n");
    lstrcatA(cdf_contents, "[CatalogFiles]\r\n");
    lstrcatA(cdf_contents, "hashme=.\\tempfile.cdf\r");
    create_cdf_file(cdffileA, cdf_contents);

    SET_UNEXPECTED;
    SetLastError(0xdeadbeef);
    catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
    todo_wine
    {
    ok(catcdf != NULL, "CryptCATCDFOpen failed\n");
    ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
    }
    /* Loop through the members */
    CLEAR_EXPECT;
    catmember = NULL;
    catmembertag = NULL;
    while ((catmembertag = pCryptCATCDFEnumMembersByCDFTagEx(catcdf, catmembertag, cdf_callback, &catmember, FALSE, NULL))) ;
    todo_wine
    CHECK_EXPECT(CRYPTCAT_E_AREA_MEMBER, CRYPTCAT_E_CDF_MEMBER_FILE_PATH);
    pCryptCATCDFClose(catcdf);
    DeleteFileA(cdffileA);
    todo_wine
    ok(DeleteFileA(catfileA), "Expected a catalog file to be created\n");

    /* Header and 2 duplicate members */
    lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
    lstrcatA(cdf_contents, "Name=tempfile.cat\r\n");
    lstrcatA(cdf_contents, "[CatalogFiles]\r\n");
    lstrcatA(cdf_contents, "hashme=.\\tempfile.cdf\r\n");
    lstrcatA(cdf_contents, "hashme=.\\tempfile.cdf\r\n");
    create_cdf_file(cdffileA, cdf_contents);

    SET_UNEXPECTED;
    SetLastError(0xdeadbeef);
    catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
    todo_wine
    {
    ok(catcdf != NULL, "CryptCATCDFOpen failed\n");
    ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
    }
    /* Loop through the members */
    SET_UNEXPECTED;
    catmember = NULL;
    catmembertag = NULL;
    while ((catmembertag = pCryptCATCDFEnumMembersByCDFTagEx(catcdf, catmembertag, cdf_callback, &catmember, FALSE, NULL))) ;
    pCryptCATCDFClose(catcdf);
    test_catalog_properties(catfileA, 0, 1);
    DeleteFileA(cdffileA);
    todo_wine
    ok(DeleteFileA(catfileA), "Expected a catalog file to be created\n");

    /* Wrong attribute */
    lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
    lstrcatA(cdf_contents, "Name=tempfile.cat\r\n");
    lstrcatA(cdf_contents, "CATATTR1=0x10010001:attr1\r\n");
    create_cdf_file(cdffileA, cdf_contents);

    SET_UNEXPECTED;
    SetLastError(0xdeadbeef);
    catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
    todo_wine
    {
    ok(catcdf != NULL, "CryptCATCDFOpen failed\n");
    ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
    }
    /* Loop through the attributes */
    CLEAR_EXPECT;
    catattr = NULL;
    while ((catattr = pCryptCATCDFEnumCatAttributes(catcdf, catattr, cdf_callback))) ;
    todo_wine
    CHECK_EXPECT(CRYPTCAT_E_AREA_ATTRIBUTE, CRYPTCAT_E_CDF_ATTR_TOOFEWVALUES);
    pCryptCATCDFClose(catcdf);
    DeleteFileA(cdffileA);
    todo_wine
    ok(DeleteFileA(catfileA), "Expected a catalog file to be created\n");

    /* Two identical attributes */
    lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
    lstrcatA(cdf_contents, "Name=tempfile.cat\r\n");
    lstrcatA(cdf_contents, "CATATTR1=0x10010001:attr1:value1\r\n");
    lstrcatA(cdf_contents, "CATATTR1=0x10010001:attr1:value1\r\n");
    lstrcatA(cdf_contents, "[CatalogFiles]\r\n");
    lstrcatA(cdf_contents, "hashme=.\\tempfile.cdf\r\n");
    create_cdf_file(cdffileA, cdf_contents);

    SET_UNEXPECTED;
    SetLastError(0xdeadbeef);
    catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
    todo_wine
    {
    ok(catcdf != NULL, "CryptCATCDFOpen failed\n");
    ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
    }
    /* Loop through the members */
    SET_UNEXPECTED;
    catmember = NULL;
    catmembertag = NULL;
    while ((catmembertag = pCryptCATCDFEnumMembersByCDFTagEx(catcdf, catmembertag, cdf_callback, &catmember, FALSE, NULL))) ;
    /* Loop through the attributes */
    SET_UNEXPECTED;
    catattr = NULL;
    while ((catattr = pCryptCATCDFEnumCatAttributes(catcdf, catattr, cdf_callback))) ;
    pCryptCATCDFClose(catcdf);
    test_catalog_properties(catfileA, 1, 1);
    DeleteFileA(cdffileA);
    todo_wine
    ok(DeleteFileA(catfileA), "Expected a catalog file to be created\n");

    /* Two different attribute values with the same tag */
    lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
    lstrcatA(cdf_contents, "Name=tempfile.cat\r\n");
    lstrcatA(cdf_contents, "CATATTR1=0x10010001:attr1:value1\r\n");
    lstrcatA(cdf_contents, "CATATTR1=0x10010001:attr2:value2\r\n");
    lstrcatA(cdf_contents, "[CatalogFiles]\r\n");
    lstrcatA(cdf_contents, "hashme=.\\tempfile.cdf\r\n");
    create_cdf_file(cdffileA, cdf_contents);

    SET_UNEXPECTED;
    SetLastError(0xdeadbeef);
    catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
    todo_wine
    {
    ok(catcdf != NULL, "CryptCATCDFOpen failed\n");
    ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
    }
    /* Loop through the members */
    SET_UNEXPECTED;
    catmember = NULL;
    catmembertag = NULL;
    while ((catmembertag = pCryptCATCDFEnumMembersByCDFTagEx(catcdf, catmembertag, cdf_callback, &catmember, FALSE, NULL))) ;
    /* Loop through the attributes */
    SET_UNEXPECTED;
    catattr = NULL;
    while ((catattr = pCryptCATCDFEnumCatAttributes(catcdf, catattr, cdf_callback))) ;
    pCryptCATCDFClose(catcdf);
    test_catalog_properties(catfileA, 1, 1);
    DeleteFileA(cdffileA);
    todo_wine
    ok(DeleteFileA(catfileA), "Expected a catalog file to be created\n");
}
1129

1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
static const struct
{
    WORD e_magic;      /* 00: MZ Header signature */
    WORD unused[29];
    DWORD e_lfanew;    /* 3c: Offset to extended header */
} dos_header =
{
    IMAGE_DOS_SIGNATURE, { 0 }, sizeof(dos_header)
};

static IMAGE_NT_HEADERS nt_header =
{
    IMAGE_NT_SIGNATURE, /* Signature */
    {
        IMAGE_FILE_MACHINE_I386, /* Machine */
        1, /* NumberOfSections */
        0, /* TimeDateStamp */
        0, /* PointerToSymbolTable */
        0, /* NumberOfSymbols */
        sizeof(IMAGE_OPTIONAL_HEADER), /* SizeOfOptionalHeader */
        IMAGE_FILE_EXECUTABLE_IMAGE /* Characteristics */
    },
    {
        IMAGE_NT_OPTIONAL_HDR_MAGIC, /* Magic */
        2, /* MajorLinkerVersion */
        15, /* MinorLinkerVersion */
        0, /* SizeOfCode */
        0, /* SizeOfInitializedData */
        0, /* SizeOfUninitializedData */
        0, /* AddressOfEntryPoint */
        0x10, /* BaseOfCode, also serves as e_lfanew in the truncated MZ header */
1161
#ifndef _WIN64
1162
        0, /* BaseOfData */
1163
#endif
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
        0x10000000, /* ImageBase */
        0, /* SectionAlignment */
        0, /* FileAlignment */
        4, /* MajorOperatingSystemVersion */
        0, /* MinorOperatingSystemVersion */
        1, /* MajorImageVersion */
        0, /* MinorImageVersion */
        4, /* MajorSubsystemVersion */
        0, /* MinorSubsystemVersion */
        0, /* Win32VersionValue */
        0x200, /* SizeOfImage */
        sizeof(dos_header) + sizeof(nt_header), /* SizeOfHeaders */
        0, /* CheckSum */
        IMAGE_SUBSYSTEM_WINDOWS_CUI, /* Subsystem */
        0, /* DllCharacteristics */
        0, /* SizeOfStackReserve */
        0, /* SizeOfStackCommit */
        0, /* SizeOfHeapReserve */
        3, /* SizeOfHeapCommit */
        2, /* LoaderFlags */
        1, /* NumberOfRvaAndSizes */
        { { 0 } } /* DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES] */
    }
};

static void test_sip(void)
{
    static WCHAR nameW[] = {'t','e','s','t','.','e','x','e',0};
    SIP_SUBJECTINFO info;
    DWORD index, encoding, size;
    HANDLE file;
    GUID guid;
    BOOL ret;
    char buf[1024];

    file = CreateFileW(nameW, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    ok(file != INVALID_HANDLE_VALUE, "can't create file\n");
    if(file == INVALID_HANDLE_VALUE)
        return;
    WriteFile(file, &dos_header, sizeof(dos_header), &size, NULL);
    WriteFile(file, &nt_header, sizeof(nt_header), &size, NULL);
    memset(buf, 0, sizeof(buf));
    WriteFile(file, buf, 0x200 - sizeof(dos_header) - sizeof(nt_header), &size, NULL);
    CloseHandle(file);

    file= CreateFileW(nameW, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    ok(file != INVALID_HANDLE_VALUE, "can't open file\n");

    memset(&info, 0, sizeof(SIP_SUBJECTINFO));
    info.cbSize = sizeof(SIP_SUBJECTINFO);
    info.pgSubjectType = &guid;
    ret = CryptSIPRetrieveSubjectGuid(NULL, file, info.pgSubjectType);
    ok(ret, "CryptSIPRetrieveSubjectGuid failed (%x)\n", GetLastError());

    ret = pPutSignedDataMsg(&info, X509_ASN_ENCODING, &index, 4, (BYTE*)"test");
    ok(!ret, "CryptSIPPutSignedDataMsg succeedded\n");
    index = GetLastError();
    ok(index == ERROR_PATH_NOT_FOUND, "GetLastError returned %x\n", index);

    info.hFile = file;
    info.pwsFileName = nameW;
    ret = pPutSignedDataMsg(&info, X509_ASN_ENCODING, &index, 4, (BYTE*)"test");
    ok(!ret, "CryptSIPPutSignedDataMsg succeedded\n");
    index = GetLastError();
    todo_wine ok(index == ERROR_INVALID_PARAMETER, "GetLastError returned %x\n", index);

    info.hFile = INVALID_HANDLE_VALUE;
    info.pwsFileName = nameW;
    ret = pPutSignedDataMsg(&info, X509_ASN_ENCODING, &index, 4, (BYTE*)"test");
    ok(!ret, "CryptSIPPutSignedDataMsg succeedded\n");
    index = GetLastError();
    ok(index == ERROR_SHARING_VIOLATION, "GetLastError returned %x\n", index);

    CloseHandle(file);
    file= CreateFileW(nameW, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

    info.hFile = file;
    info.pwsFileName = (void*)0xdeadbeef;
    ret = pPutSignedDataMsg(&info, X509_ASN_ENCODING, &index, 4, (BYTE*)"test");
    ok(ret, "CryptSIPPutSignedDataMsg failed (%x)\n", GetLastError());
    ok(index == 0, "index = %x\n", index);

    CloseHandle(file);
    file= CreateFileW(nameW, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

    info.hFile = INVALID_HANDLE_VALUE;
    info.pwsFileName = nameW;
    ret = pPutSignedDataMsg(&info, X509_ASN_ENCODING, &index, 14, (BYTE*)"longer message");
    ok(ret, "CryptSIPPutSignedDataMsg failed (%x)\n", GetLastError());
    ok(index == 1, "index = %x\n", index);

    size = 0;
    encoding = 0xdeadbeef;
    ret = pGetSignedDataMsg(&info, &encoding, 0, &size, NULL);
    ok(ret, "CryptSIPGetSignedDataMsg failed (%x)\n", GetLastError());
    ok(encoding == 0xdeadbeef, "encoding = %x\n", encoding);
    ok(size == 16, "size = %d\n", size);

    ret = pGetSignedDataMsg(&info, &encoding, 0, &size, (BYTE*)buf);
    ok(ret, "CryptSIPGetSignedDataMsg failed (%x)\n", GetLastError());
    ok(encoding == (X509_ASN_ENCODING|PKCS_7_ASN_ENCODING), "encoding = %x\n", encoding);
    ok(size == 8, "size = %d\n", size);
    ok(!memcmp(buf, "test\0\0\0\0", 8), "buf = %s\n", buf);

    size = 0;
    encoding = 0xdeadbeef;
    ret = pGetSignedDataMsg(&info, &encoding, 1, &size, NULL);
    ok(ret, "CryptSIPGetSignedDataMsg failed (%x)\n", GetLastError());
    ok(encoding == 0xdeadbeef, "encoding = %x\n", encoding);
    ok(size == 24, "size = %d\n", size);

    ret = pGetSignedDataMsg(&info, &encoding, 1, &size, (BYTE*)buf);
    ok(ret, "CryptSIPGetSignedDataMsg failed (%x)\n", GetLastError());
    ok(encoding == (X509_ASN_ENCODING|PKCS_7_ASN_ENCODING), "encoding = %x\n", encoding);
    ok(size == 16, "size = %d\n", size);
    ok(!strcmp(buf, "longer message"), "buf = %s\n", buf);

    CryptReleaseContext(info.hProv, 0);
    CloseHandle(file);
    DeleteFileW(nameW);
}

1286 1287
START_TEST(crypt)
{
1288
    char** myARGV;
1289
    char sysdir[MAX_PATH];
1290

1291
    InitFunctionPtrs();
1292

1293 1294 1295 1296 1297 1298
    if (!pCryptCATAdminAcquireContext)
    {
        win_skip("CryptCATAdmin functions are not available\n");
        return;
    }

1299 1300 1301 1302 1303
    GetSystemDirectoryA(sysdir, MAX_PATH);
    lstrcpyA(catroot, sysdir);
    lstrcatA(catroot, "\\CatRoot");
    lstrcpyA(catroot2, sysdir);
    lstrcatA(catroot2, "\\CatRoot2");
1304

1305
    winetest_get_mainargs(&myARGV);
1306 1307 1308 1309
    strcpy(selfname, myARGV[0]);

    GetCurrentDirectoryA(MAX_PATH, CURR_DIR);
   
1310
    test_context();
1311
    test_calchash();
1312
    test_CryptCATOpen();
1313 1314
    /* Parameter checking only */
    test_CryptCATCDF_params();
1315 1316
    /* Test the parsing of a cdf file */
    test_cdf_parsing();
1317
    /* Create a catalog file out of our own catalog definition file */
1318
    test_create_catalog_file();
1319
    test_CryptCATAdminAddRemoveCatalog();
1320
    test_sip();
1321
}