store.c 43.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright 2002 Mike McCormack for CodeWeavers
 * Copyright 2004-2006 Juan Lang
 *
 * 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
17
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18 19 20 21 22 23 24 25
 *
 * FIXME:
 * - The concept of physical stores and locations isn't implemented.  (This
 *   doesn't mean registry stores et al aren't implemented.  See the PSDK for
 *   registering and enumerating physical stores and locations.)
 * - Many flags, options and whatnot are unimplemented.
 */

26 27 28
#include "config.h"
#include "wine/port.h"

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#include <assert.h>
#include <stdarg.h>
#include "windef.h"
#include "winbase.h"
#include "winnls.h"
#include "winreg.h"
#include "winuser.h"
#include "wincrypt.h"
#include "wine/debug.h"
#include "wine/exception.h"
#include "crypt32_private.h"

WINE_DEFAULT_DEBUG_CHANNEL(crypt);

static const WINE_CONTEXT_INTERFACE gCertInterface = {
    (CreateContextFunc)CertCreateCertificateContext,
    (AddContextToStoreFunc)CertAddCertificateContextToStore,
    (AddEncodedContextToStoreFunc)CertAddEncodedCertificateToStore,
    (EnumContextsInStoreFunc)CertEnumCertificatesInStore,
48
    (EnumPropertiesFunc)CertEnumCertificateContextProperties,
49 50 51 52 53
    (GetContextPropertyFunc)CertGetCertificateContextProperty,
    (SetContextPropertyFunc)CertSetCertificateContextProperty,
    (SerializeElementFunc)CertSerializeCertificateStoreElement,
    (DeleteContextFunc)CertDeleteCertificateFromStore,
};
54
const WINE_CONTEXT_INTERFACE *pCertInterface = &gCertInterface;
55 56 57 58 59 60

static const WINE_CONTEXT_INTERFACE gCRLInterface = {
    (CreateContextFunc)CertCreateCRLContext,
    (AddContextToStoreFunc)CertAddCRLContextToStore,
    (AddEncodedContextToStoreFunc)CertAddEncodedCRLToStore,
    (EnumContextsInStoreFunc)CertEnumCRLsInStore,
61
    (EnumPropertiesFunc)CertEnumCRLContextProperties,
62 63 64 65 66
    (GetContextPropertyFunc)CertGetCRLContextProperty,
    (SetContextPropertyFunc)CertSetCRLContextProperty,
    (SerializeElementFunc)CertSerializeCRLStoreElement,
    (DeleteContextFunc)CertDeleteCRLFromStore,
};
67
const WINE_CONTEXT_INTERFACE *pCRLInterface = &gCRLInterface;
68 69 70 71 72 73

static const WINE_CONTEXT_INTERFACE gCTLInterface = {
    (CreateContextFunc)CertCreateCTLContext,
    (AddContextToStoreFunc)CertAddCTLContextToStore,
    (AddEncodedContextToStoreFunc)CertAddEncodedCTLToStore,
    (EnumContextsInStoreFunc)CertEnumCTLsInStore,
74
    (EnumPropertiesFunc)CertEnumCTLContextProperties,
75 76 77 78 79
    (GetContextPropertyFunc)CertGetCTLContextProperty,
    (SetContextPropertyFunc)CertSetCTLContextProperty,
    (SerializeElementFunc)CertSerializeCTLStoreElement,
    (DeleteContextFunc)CertDeleteCTLFromStore,
};
80
const WINE_CONTEXT_INTERFACE *pCTLInterface = &gCTLInterface;
81 82 83 84

typedef struct _WINE_MEMSTORE
{
    WINECRYPT_CERTSTORE hdr;
85
    CRITICAL_SECTION cs;
86 87 88
    struct list certs;
    struct list crls;
    struct list ctls;
89
} WINE_MEMSTORE;
90

91
void CRYPT_InitStore(WINECRYPT_CERTSTORE *store, DWORD dwFlags, CertStoreType type, const store_vtbl_t *vtbl)
92 93 94 95 96
{
    store->ref = 1;
    store->dwMagic = WINE_CRYPTCERTSTORE_MAGIC;
    store->type = type;
    store->dwOpenFlags = dwFlags;
97
    store->vtbl = vtbl;
98 99 100
    store->properties = NULL;
}

101
void CRYPT_FreeStore(WINECRYPT_CERTSTORE *store)
102 103 104
{
    if (store->properties)
        ContextPropertyList_Free(store->properties);
105
    store->dwMagic = 0;
106
    CryptMemFree(store);
107 108
}

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
BOOL WINAPI I_CertUpdateStore(HCERTSTORE store1, HCERTSTORE store2, DWORD unk0,
 DWORD unk1)
{
    static BOOL warned = FALSE;
    const WINE_CONTEXT_INTERFACE * const interfaces[] = { pCertInterface,
     pCRLInterface, pCTLInterface };
    DWORD i;

    TRACE("(%p, %p, %08x, %08x)\n", store1, store2, unk0, unk1);
    if (!warned)
    {
        FIXME("semi-stub\n");
        warned = TRUE;
    }

    /* Poor-man's resync:  empty first store, then add everything from second
     * store to it.
     */
    for (i = 0; i < sizeof(interfaces) / sizeof(interfaces[0]); i++)
    {
        const void *context;

        do {
            context = interfaces[i]->enumContextsInStore(store1, NULL);
            if (context)
                interfaces[i]->deleteFromStore(context);
        } while (context);
        do {
            context = interfaces[i]->enumContextsInStore(store2, context);
            if (context)
                interfaces[i]->addContextToStore(store1, context,
                 CERT_STORE_ADD_ALWAYS, NULL);
        } while (context);
    }
    return TRUE;
}

146 147
static BOOL MemStore_addContext(WINE_MEMSTORE *store, struct list *list, context_t *orig_context,
 context_t *existing, context_t **ret_context, BOOL use_link)
148
{
149
    context_t *context;
150

151
    context = orig_context->vtbl->clone(orig_context, &store->hdr, use_link);
152 153 154
    if (!context)
        return FALSE;

155 156 157 158 159 160 161 162
    TRACE("adding %p\n", context);
    EnterCriticalSection(&store->cs);
    if (existing) {
        context->u.entry.prev = existing->u.entry.prev;
        context->u.entry.next = existing->u.entry.next;
        context->u.entry.prev->next = &context->u.entry;
        context->u.entry.next->prev = &context->u.entry;
        list_init(&existing->u.entry);
163 164
        if(!existing->ref)
            Context_Release(existing);
165 166 167 168 169
    }else {
        list_add_head(list, &context->u.entry);
    }
    LeaveCriticalSection(&store->cs);

170
    if(ret_context)
171
        *ret_context = context;
172 173
    else
        Context_Release(context);
174
    return TRUE;
175 176
}

177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
static context_t *MemStore_enumContext(WINE_MEMSTORE *store, struct list *list, context_t *prev)
{
    struct list *next;
    context_t *ret;

    EnterCriticalSection(&store->cs);
    if (prev) {
        next = list_next(list, &prev->u.entry);
        Context_Release(prev);
    }else {
        next = list_next(list, list);
    }
    LeaveCriticalSection(&store->cs);

    if (!next) {
        SetLastError(CRYPT_E_NOT_FOUND);
        return NULL;
    }

    ret = LIST_ENTRY(next, context_t, u.entry);
    Context_AddRef(ret);
    return ret;
}

201 202 203 204 205 206 207 208 209 210 211 212
static BOOL MemStore_deleteContext(WINE_MEMSTORE *store, context_t *context)
{
    BOOL in_list = FALSE;

    EnterCriticalSection(&store->cs);
    if (!list_empty(&context->u.entry)) {
        list_remove(&context->u.entry);
        list_init(&context->u.entry);
        in_list = TRUE;
    }
    LeaveCriticalSection(&store->cs);

213 214
    if(in_list && !context->ref)
        Context_Free(context);
215 216
    return TRUE;
}
217

218 219 220 221 222 223 224 225
static void free_contexts(struct list *list)
{
    context_t *context, *next;

    LIST_FOR_EACH_ENTRY_SAFE(context, next, list, context_t, u.entry)
    {
        TRACE("freeing %p\n", context);
        list_remove(&context->u.entry);
226
        Context_Free(context);
227 228 229
    }
}

230 231 232 233 234 235 236
static void MemStore_releaseContext(WINECRYPT_CERTSTORE *store, context_t *context)
{
    /* Free the context only if it's not in a list. Otherwise it may be reused later. */
    if(list_empty(&context->u.entry))
        Context_Free(context);
}

237 238 239 240 241 242 243 244 245
static BOOL MemStore_addCert(WINECRYPT_CERTSTORE *store, context_t *cert,
 context_t *toReplace, context_t **ppStoreContext, BOOL use_link)
{
    WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;

    TRACE("(%p, %p, %p, %p)\n", store, cert, toReplace, ppStoreContext);
    return MemStore_addContext(ms, &ms->certs, cert, toReplace, ppStoreContext, use_link);
}

246
static context_t *MemStore_enumCert(WINECRYPT_CERTSTORE *store, context_t *prev)
247 248 249
{
    WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;

250
    TRACE("(%p, %p)\n", store, prev);
Juan Lang's avatar
Juan Lang committed
251

252
    return MemStore_enumContext(ms, &ms->certs, prev);
253 254
}

255
static BOOL MemStore_deleteCert(WINECRYPT_CERTSTORE *store, context_t *context)
256
{
Juan Lang's avatar
Juan Lang committed
257
    WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
258

259
    TRACE("(%p, %p)\n", store, context);
260

261
    return MemStore_deleteContext(ms, context);
262 263
}

264 265
static BOOL MemStore_addCRL(WINECRYPT_CERTSTORE *store, context_t *crl,
 context_t *toReplace, context_t **ppStoreContext, BOOL use_link)
266 267 268 269 270
{
    WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;

    TRACE("(%p, %p, %p, %p)\n", store, crl, toReplace, ppStoreContext);

271
    return MemStore_addContext(ms, &ms->crls, crl, toReplace, ppStoreContext, use_link);
272 273
}

274
static context_t *MemStore_enumCRL(WINECRYPT_CERTSTORE *store, context_t *prev)
275 276 277
{
    WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;

278
    TRACE("(%p, %p)\n", store, prev);
279

280
    return MemStore_enumContext(ms, &ms->crls, prev);
281 282
}

283
static BOOL MemStore_deleteCRL(WINECRYPT_CERTSTORE *store, context_t *context)
284 285 286
{
    WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;

287
    TRACE("(%p, %p)\n", store, context);
288

289
    return MemStore_deleteContext(ms, context);
290 291
}

292 293
static BOOL MemStore_addCTL(WINECRYPT_CERTSTORE *store, context_t *ctl,
 context_t *toReplace, context_t **ppStoreContext, BOOL use_link)
294 295 296 297 298
{
    WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;

    TRACE("(%p, %p, %p, %p)\n", store, ctl, toReplace, ppStoreContext);

299
    return MemStore_addContext(ms, &ms->ctls, ctl, toReplace, ppStoreContext, use_link);
300 301
}

302
static context_t *MemStore_enumCTL(WINECRYPT_CERTSTORE *store, context_t *prev)
303 304 305
{
    WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;

306
    TRACE("(%p, %p)\n", store, prev);
307

308
    return MemStore_enumContext(ms, &ms->ctls, prev);
309 310
}

311
static BOOL MemStore_deleteCTL(WINECRYPT_CERTSTORE *store, context_t *context)
312 313 314
{
    WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;

315
    TRACE("(%p, %p)\n", store, context);
316

317
    return MemStore_deleteContext(ms, context);
318 319
}

320 321 322 323 324 325
static void MemStore_addref(WINECRYPT_CERTSTORE *store)
{
    LONG ref = InterlockedIncrement(&store->ref);
    TRACE("ref = %d\n", ref);
}

326
static DWORD MemStore_release(WINECRYPT_CERTSTORE *cert_store, DWORD flags)
327
{
328
    WINE_MEMSTORE *store = (WINE_MEMSTORE*)cert_store;
329
    LONG ref;
330

331
    if(flags & ~CERT_CLOSE_STORE_CHECK_FLAG)
332 333 334 335 336
        FIXME("Unimplemented flags %x\n", flags);

    ref = InterlockedDecrement(&store->hdr.ref);
    TRACE("(%p) ref=%d\n", store, ref);
    if(ref)
337
        return (flags & CERT_CLOSE_STORE_CHECK_FLAG) ? CRYPT_E_PENDING_CLOSE : ERROR_SUCCESS;
338

339 340 341
    free_contexts(&store->certs);
    free_contexts(&store->crls);
    free_contexts(&store->ctls);
342 343
    store->cs.DebugInfo->Spare[0] = 0;
    DeleteCriticalSection(&store->cs);
344 345
    CRYPT_FreeStore(&store->hdr);
    return ERROR_SUCCESS;
346 347
}

348 349 350 351 352 353 354 355
static BOOL MemStore_control(WINECRYPT_CERTSTORE *store, DWORD dwFlags,
 DWORD dwCtrlType, void const *pvCtrlPara)
{
    SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
    return FALSE;
}

static const store_vtbl_t MemStoreVtbl = {
356
    MemStore_addref,
357
    MemStore_release,
358
    MemStore_releaseContext,
359 360 361 362 363 364 365 366 367 368 369 370 371 372
    MemStore_control,
    {
        MemStore_addCert,
        MemStore_enumCert,
        MemStore_deleteCert
    }, {
        MemStore_addCRL,
        MemStore_enumCRL,
        MemStore_deleteCRL
    }, {
        MemStore_addCTL,
        MemStore_enumCTL,
        MemStore_deleteCTL
    }
373 374
};

375 376 377
static WINECRYPT_CERTSTORE *CRYPT_MemOpenStore(HCRYPTPROV hCryptProv,
 DWORD dwFlags, const void *pvPara)
{
378
    WINE_MEMSTORE *store;
379

380
    TRACE("(%ld, %08x, %p)\n", hCryptProv, dwFlags, pvPara);
381 382 383 384 385 386 387 388 389 390 391 392

    if (dwFlags & CERT_STORE_DELETE_FLAG)
    {
        SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
        store = NULL;
    }
    else
    {
        store = CryptMemAlloc(sizeof(WINE_MEMSTORE));
        if (store)
        {
            memset(store, 0, sizeof(WINE_MEMSTORE));
393
            CRYPT_InitStore(&store->hdr, dwFlags, StoreTypeMem, &MemStoreVtbl);
394 395
            InitializeCriticalSection(&store->cs);
            store->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": ContextList.cs");
396 397 398
            list_init(&store->certs);
            list_init(&store->crls);
            list_init(&store->ctls);
399 400 401
            /* Mem store doesn't need crypto provider, so close it */
            if (hCryptProv && !(dwFlags & CERT_STORE_NO_CRYPT_RELEASE_FLAG))
                CryptReleaseContext(hCryptProv, 0);
402 403
        }
    }
404
    return (WINECRYPT_CERTSTORE*)store;
405 406
}

407 408
static const WCHAR rootW[] = { 'R','o','o','t',0 };

409
static WINECRYPT_CERTSTORE *CRYPT_SysRegOpenStoreW(HCRYPTPROV hCryptProv,
410 411 412
 DWORD dwFlags, const void *pvPara)
{
    static const WCHAR fmt[] = { '%','s','\\','%','s',0 };
413
    LPCWSTR storeName = pvPara;
414
    LPWSTR storePath;
415
    WINECRYPT_CERTSTORE *store = NULL;
416 417 418
    HKEY root;
    LPCWSTR base;

419
    TRACE("(%ld, %08x, %s)\n", hCryptProv, dwFlags,
420
     debugstr_w(pvPara));
421 422 423

    if (!pvPara)
    {
424
        SetLastError(E_INVALIDARG);
425 426
        return NULL;
    }
427 428 429 430 431 432 433 434
    /* FIXME:  In Windows, the root store (even the current user location) is
     * protected:  adding to it or removing from it present a user interface,
     * and the keys are owned by the system process, not the current user.
     * Wine's registry doesn't implement access controls, so a similar
     * mechanism isn't possible yet.
     */
    if ((dwFlags & CERT_SYSTEM_STORE_LOCATION_MASK) ==
     CERT_SYSTEM_STORE_LOCAL_MACHINE && !lstrcmpiW(storeName, rootW))
435
        return CRYPT_RootOpenStore(hCryptProv, dwFlags);
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 479

    switch (dwFlags & CERT_SYSTEM_STORE_LOCATION_MASK)
    {
    case CERT_SYSTEM_STORE_LOCAL_MACHINE:
        root = HKEY_LOCAL_MACHINE;
        base = CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH;
        break;
    case CERT_SYSTEM_STORE_CURRENT_USER:
        root = HKEY_CURRENT_USER;
        base = CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH;
        break;
    case CERT_SYSTEM_STORE_CURRENT_SERVICE:
        /* hklm\Software\Microsoft\Cryptography\Services\servicename\
         * SystemCertificates
         */
        FIXME("CERT_SYSTEM_STORE_CURRENT_SERVICE, %s: stub\n",
         debugstr_w(storeName));
        return NULL;
    case CERT_SYSTEM_STORE_SERVICES:
        /* hklm\Software\Microsoft\Cryptography\Services\servicename\
         * SystemCertificates
         */
        FIXME("CERT_SYSTEM_STORE_SERVICES, %s: stub\n",
         debugstr_w(storeName));
        return NULL;
    case CERT_SYSTEM_STORE_USERS:
        /* hku\user sid\Software\Microsoft\SystemCertificates */
        FIXME("CERT_SYSTEM_STORE_USERS, %s: stub\n",
         debugstr_w(storeName));
        return NULL;
    case CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY:
        root = HKEY_CURRENT_USER;
        base = CERT_GROUP_POLICY_SYSTEM_STORE_REGPATH;
        break;
    case CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY:
        root = HKEY_LOCAL_MACHINE;
        base = CERT_GROUP_POLICY_SYSTEM_STORE_REGPATH;
        break;
    case CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE:
        /* hklm\Software\Microsoft\EnterpriseCertificates */
        FIXME("CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE, %s: stub\n",
         debugstr_w(storeName));
        return NULL;
    default:
480
        SetLastError(E_INVALIDARG);
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
        return NULL;
    }

    storePath = CryptMemAlloc((lstrlenW(base) + lstrlenW(storeName) + 2) *
     sizeof(WCHAR));
    if (storePath)
    {
        LONG rc;
        HKEY key;
        REGSAM sam = dwFlags & CERT_STORE_READONLY_FLAG ? KEY_READ :
            KEY_ALL_ACCESS;

        wsprintfW(storePath, fmt, base, storeName);
        if (dwFlags & CERT_STORE_OPEN_EXISTING_FLAG)
            rc = RegOpenKeyExW(root, storePath, 0, sam, &key);
        else
        {
            DWORD disp;

            rc = RegCreateKeyExW(root, storePath, 0, NULL, 0, sam, NULL,
                                 &key, &disp);
            if (!rc && dwFlags & CERT_STORE_CREATE_NEW_FLAG &&
                disp == REG_OPENED_EXISTING_KEY)
            {
                RegCloseKey(key);
                rc = ERROR_FILE_EXISTS;
            }
        }
        if (!rc)
        {
            store = CRYPT_RegOpenStore(hCryptProv, dwFlags, key);
            RegCloseKey(key);
        }
        else
            SetLastError(rc);
        CryptMemFree(storePath);
    }
    return store;
}

521
static WINECRYPT_CERTSTORE *CRYPT_SysRegOpenStoreA(HCRYPTPROV hCryptProv,
522 523 524
 DWORD dwFlags, const void *pvPara)
{
    int len;
525
    WINECRYPT_CERTSTORE *ret = NULL;
526

527
    TRACE("(%ld, %08x, %s)\n", hCryptProv, dwFlags,
528
     debugstr_a(pvPara));
529 530 531 532 533 534

    if (!pvPara)
    {
        SetLastError(ERROR_FILE_NOT_FOUND);
        return NULL;
    }
535
    len = MultiByteToWideChar(CP_ACP, 0, pvPara, -1, NULL, 0);
536 537 538 539 540 541
    if (len)
    {
        LPWSTR storeName = CryptMemAlloc(len * sizeof(WCHAR));

        if (storeName)
        {
542
            MultiByteToWideChar(CP_ACP, 0, pvPara, -1, storeName, len);
543 544 545 546 547 548 549
            ret = CRYPT_SysRegOpenStoreW(hCryptProv, dwFlags, storeName);
            CryptMemFree(storeName);
        }
    }
    return ret;
}

550
static WINECRYPT_CERTSTORE *CRYPT_SysOpenStoreW(HCRYPTPROV hCryptProv,
551 552 553 554 555
 DWORD dwFlags, const void *pvPara)
{
    HCERTSTORE store = 0;
    BOOL ret;

556
    TRACE("(%ld, %08x, %s)\n", hCryptProv, dwFlags,
557
     debugstr_w(pvPara));
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585

    if (!pvPara)
    {
        SetLastError(ERROR_FILE_NOT_FOUND);
        return NULL;
    }
    /* This returns a different error than system registry stores if the
     * location is invalid.
     */
    switch (dwFlags & CERT_SYSTEM_STORE_LOCATION_MASK)
    {
    case CERT_SYSTEM_STORE_LOCAL_MACHINE:
    case CERT_SYSTEM_STORE_CURRENT_USER:
    case CERT_SYSTEM_STORE_CURRENT_SERVICE:
    case CERT_SYSTEM_STORE_SERVICES:
    case CERT_SYSTEM_STORE_USERS:
    case CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY:
    case CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY:
    case CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE:
        ret = TRUE;
        break;
    default:
        SetLastError(ERROR_FILE_NOT_FOUND);
        ret = FALSE;
    }
    if (ret)
    {
        HCERTSTORE regStore = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_W,
586
         0, 0, dwFlags, pvPara);
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604

        if (regStore)
        {
            store = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
             CERT_STORE_CREATE_NEW_FLAG, NULL);
            CertAddStoreToCollection(store, regStore,
             dwFlags & CERT_STORE_READONLY_FLAG ? 0 :
             CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
            CertCloseStore(regStore, 0);
            /* CERT_SYSTEM_STORE_CURRENT_USER returns both the HKCU and HKLM
             * stores.
             */
            if ((dwFlags & CERT_SYSTEM_STORE_LOCATION_MASK) ==
             CERT_SYSTEM_STORE_CURRENT_USER)
            {
                dwFlags &= ~CERT_SYSTEM_STORE_CURRENT_USER;
                dwFlags |= CERT_SYSTEM_STORE_LOCAL_MACHINE;
                regStore = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_W, 0,
605
                 0, dwFlags, pvPara);
606 607 608 609 610 611 612 613
                if (regStore)
                {
                    CertAddStoreToCollection(store, regStore,
                     dwFlags & CERT_STORE_READONLY_FLAG ? 0 :
                     CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
                    CertCloseStore(regStore, 0);
                }
            }
614 615 616
            /* System store doesn't need crypto provider, so close it */
            if (hCryptProv && !(dwFlags & CERT_STORE_NO_CRYPT_RELEASE_FLAG))
                CryptReleaseContext(hCryptProv, 0);
617 618
        }
    }
619
    return store;
620 621
}

622
static WINECRYPT_CERTSTORE *CRYPT_SysOpenStoreA(HCRYPTPROV hCryptProv,
623 624 625
 DWORD dwFlags, const void *pvPara)
{
    int len;
626
    WINECRYPT_CERTSTORE *ret = NULL;
627

628
    TRACE("(%ld, %08x, %s)\n", hCryptProv, dwFlags,
629
     debugstr_a(pvPara));
630 631 632 633 634 635

    if (!pvPara)
    {
        SetLastError(ERROR_FILE_NOT_FOUND);
        return NULL;
    }
636
    len = MultiByteToWideChar(CP_ACP, 0, pvPara, -1, NULL, 0);
637 638 639 640 641 642
    if (len)
    {
        LPWSTR storeName = CryptMemAlloc(len * sizeof(WCHAR));

        if (storeName)
        {
643
            MultiByteToWideChar(CP_ACP, 0, pvPara, -1, storeName, len);
644 645 646 647 648 649 650
            ret = CRYPT_SysOpenStoreW(hCryptProv, dwFlags, storeName);
            CryptMemFree(storeName);
        }
    }
    return ret;
}

651 652
static void WINAPI CRYPT_MsgCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
{
653
    HCRYPTMSG msg = hCertStore;
654

655 656
    TRACE("(%p, %08x)\n", msg, dwFlags);
    CryptMsgClose(msg);
657 658 659 660 661 662
}

static void *msgProvFuncs[] = {
    CRYPT_MsgCloseStore,
};

663
static WINECRYPT_CERTSTORE *CRYPT_MsgOpenStore(HCRYPTPROV hCryptProv,
664 665
 DWORD dwFlags, const void *pvPara)
{
666
    WINECRYPT_CERTSTORE *store = NULL;
667
    HCRYPTMSG msg = (HCRYPTMSG)pvPara;
668
    WINECRYPT_CERTSTORE *memStore;
669 670 671

    TRACE("(%ld, %08x, %p)\n", hCryptProv, dwFlags, pvPara);

672
    memStore = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
673
     CERT_STORE_CREATE_NEW_FLAG, NULL);
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
    if (memStore)
    {
        BOOL ret;
        DWORD size, count, i;

        size = sizeof(count);
        ret = CryptMsgGetParam(msg, CMSG_CERT_COUNT_PARAM, 0, &count, &size);
        for (i = 0; ret && i < count; i++)
        {
            size = 0;
            ret = CryptMsgGetParam(msg, CMSG_CERT_PARAM, i, NULL, &size);
            if (ret)
            {
                LPBYTE buf = CryptMemAlloc(size);

                if (buf)
                {
                    ret = CryptMsgGetParam(msg, CMSG_CERT_PARAM, i, buf, &size);
                    if (ret)
                        ret = CertAddEncodedCertificateToStore(memStore,
                         X509_ASN_ENCODING, buf, size, CERT_STORE_ADD_ALWAYS,
                         NULL);
                    CryptMemFree(buf);
                }
            }
        }
        size = sizeof(count);
        ret = CryptMsgGetParam(msg, CMSG_CRL_COUNT_PARAM, 0, &count, &size);
        for (i = 0; ret && i < count; i++)
        {
            size = 0;
            ret = CryptMsgGetParam(msg, CMSG_CRL_PARAM, i, NULL, &size);
            if (ret)
            {
                LPBYTE buf = CryptMemAlloc(size);

                if (buf)
                {
                    ret = CryptMsgGetParam(msg, CMSG_CRL_PARAM, i, buf, &size);
                    if (ret)
                        ret = CertAddEncodedCRLToStore(memStore,
                         X509_ASN_ENCODING, buf, size, CERT_STORE_ADD_ALWAYS,
                         NULL);
                    CryptMemFree(buf);
                }
            }
        }
        if (ret)
        {
723 724 725 726 727 728 729 730 731 732 733
            CERT_STORE_PROV_INFO provInfo = { 0 };

            provInfo.cbSize = sizeof(provInfo);
            provInfo.cStoreProvFunc = sizeof(msgProvFuncs) /
             sizeof(msgProvFuncs[0]);
            provInfo.rgpvStoreProvFunc = msgProvFuncs;
            provInfo.hStoreProv = CryptMsgDuplicate(msg);
            store = CRYPT_ProvCreateStore(dwFlags, memStore, &provInfo);
            /* Msg store doesn't need crypto provider, so close it */
            if (hCryptProv && !(dwFlags & CERT_STORE_NO_CRYPT_RELEASE_FLAG))
                CryptReleaseContext(hCryptProv, 0);
734 735 736 737 738 739 740 741
        }
        else
            CertCloseStore(memStore, 0);
    }
    TRACE("returning %p\n", store);
    return store;
}

742
static WINECRYPT_CERTSTORE *CRYPT_PKCSOpenStore(HCRYPTPROV hCryptProv,
743 744 745
 DWORD dwFlags, const void *pvPara)
{
    HCRYPTMSG msg;
746
    WINECRYPT_CERTSTORE *store = NULL;
747
    const CRYPT_DATA_BLOB *data = pvPara;
748
    BOOL ret;
749 750
    DWORD msgOpenFlags = dwFlags & CERT_STORE_NO_CRYPT_RELEASE_FLAG ? 0 :
     CMSG_CRYPT_RELEASE_CONTEXT_FLAG;
751 752 753

    TRACE("(%ld, %08x, %p)\n", hCryptProv, dwFlags, pvPara);

754 755
    msg = CryptMsgOpenToDecode(PKCS_7_ASN_ENCODING, msgOpenFlags, CMSG_SIGNED,
     hCryptProv, NULL, NULL);
756 757 758 759
    ret = CryptMsgUpdate(msg, data->pbData, data->cbData, TRUE);
    if (!ret)
    {
        CryptMsgClose(msg);
760 761
        msg = CryptMsgOpenToDecode(PKCS_7_ASN_ENCODING, msgOpenFlags, 0,
         hCryptProv, NULL, NULL);
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
        ret = CryptMsgUpdate(msg, data->pbData, data->cbData, TRUE);
        if (ret)
        {
            DWORD type, size = sizeof(type);

            /* Only signed messages are allowed, check type */
            ret = CryptMsgGetParam(msg, CMSG_TYPE_PARAM, 0, &type, &size);
            if (ret && type != CMSG_SIGNED)
            {
                SetLastError(CRYPT_E_INVALID_MSG_TYPE);
                ret = FALSE;
            }
        }
    }
    if (ret)
777
        store = CRYPT_MsgOpenStore(0, dwFlags, msg);
778 779 780 781 782
    CryptMsgClose(msg);
    TRACE("returning %p\n", store);
    return store;
}

783
static WINECRYPT_CERTSTORE *CRYPT_SerializedOpenStore(HCRYPTPROV hCryptProv,
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
 DWORD dwFlags, const void *pvPara)
{
    HCERTSTORE store;
    const CRYPT_DATA_BLOB *data = pvPara;

    TRACE("(%ld, %08x, %p)\n", hCryptProv, dwFlags, pvPara);

    if (dwFlags & CERT_STORE_DELETE_FLAG)
    {
        SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
        return NULL;
    }

    store = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
     CERT_STORE_CREATE_NEW_FLAG, NULL);
    if (store)
    {
        if (!CRYPT_ReadSerializedStoreFromBlob(data, store))
        {
            CertCloseStore(store, 0);
            store = NULL;
        }
    }
    TRACE("returning %p\n", store);
808
    return (WINECRYPT_CERTSTORE*)store;
809 810
}

811
static WINECRYPT_CERTSTORE *CRYPT_PhysOpenStoreW(HCRYPTPROV hCryptProv,
812 813 814
 DWORD dwFlags, const void *pvPara)
{
    if (dwFlags & CERT_SYSTEM_STORE_RELOCATE_FLAG)
815
        FIXME("(%ld, %08x, %p): stub\n", hCryptProv, dwFlags, pvPara);
816
    else
817
        FIXME("(%ld, %08x, %s): stub\n", hCryptProv, dwFlags,
818
         debugstr_w(pvPara));
819 820 821
    return NULL;
}

822
HCERTSTORE WINAPI CertOpenStore(LPCSTR lpszStoreProvider,
823
 DWORD dwMsgAndCertEncodingType, HCRYPTPROV_LEGACY hCryptProv, DWORD dwFlags,
824 825 826 827 828
 const void* pvPara)
{
    WINECRYPT_CERTSTORE *hcs;
    StoreOpenFunc openFunc = NULL;

829
    TRACE("(%s, %08x, %08lx, %08x, %p)\n", debugstr_a(lpszStoreProvider),
830 831
          dwMsgAndCertEncodingType, hCryptProv, dwFlags, pvPara);

832
    if (IS_INTOID(lpszStoreProvider))
833 834 835
    {
        switch (LOWORD(lpszStoreProvider))
        {
836
        case LOWORD(CERT_STORE_PROV_MSG):
837 838
            openFunc = CRYPT_MsgOpenStore;
            break;
839
        case LOWORD(CERT_STORE_PROV_MEMORY):
840 841
            openFunc = CRYPT_MemOpenStore;
            break;
842
        case LOWORD(CERT_STORE_PROV_FILE):
843 844
            openFunc = CRYPT_FileOpenStore;
            break;
845
        case LOWORD(CERT_STORE_PROV_PKCS7):
846 847
            openFunc = CRYPT_PKCSOpenStore;
            break;
848 849 850
        case LOWORD(CERT_STORE_PROV_SERIALIZED):
            openFunc = CRYPT_SerializedOpenStore;
            break;
851
        case LOWORD(CERT_STORE_PROV_REG):
852 853
            openFunc = CRYPT_RegOpenStore;
            break;
854
        case LOWORD(CERT_STORE_PROV_FILENAME_A):
855 856
            openFunc = CRYPT_FileNameOpenStoreA;
            break;
857
        case LOWORD(CERT_STORE_PROV_FILENAME_W):
858 859
            openFunc = CRYPT_FileNameOpenStoreW;
            break;
860
        case LOWORD(CERT_STORE_PROV_COLLECTION):
861 862
            openFunc = CRYPT_CollectionOpenStore;
            break;
863
        case LOWORD(CERT_STORE_PROV_SYSTEM_A):
864 865
            openFunc = CRYPT_SysOpenStoreA;
            break;
866
        case LOWORD(CERT_STORE_PROV_SYSTEM_W):
867 868
            openFunc = CRYPT_SysOpenStoreW;
            break;
869
        case LOWORD(CERT_STORE_PROV_SYSTEM_REGISTRY_A):
870 871
            openFunc = CRYPT_SysRegOpenStoreA;
            break;
872
        case LOWORD(CERT_STORE_PROV_SYSTEM_REGISTRY_W):
873 874
            openFunc = CRYPT_SysRegOpenStoreW;
            break;
875
        case LOWORD(CERT_STORE_PROV_PHYSICAL_W):
876 877
            openFunc = CRYPT_PhysOpenStoreW;
            break;
878 879 880 881 882 883 884
        default:
            if (LOWORD(lpszStoreProvider))
                FIXME("unimplemented type %d\n", LOWORD(lpszStoreProvider));
        }
    }
    else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_MEMORY))
        openFunc = CRYPT_MemOpenStore;
885 886
    else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_FILENAME_W))
        openFunc = CRYPT_FileOpenStore;
887 888
    else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_SYSTEM))
        openFunc = CRYPT_SysOpenStoreW;
889 890
    else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_PKCS7))
        openFunc = CRYPT_PKCSOpenStore;
891 892
    else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_SERIALIZED))
        openFunc = CRYPT_SerializedOpenStore;
893 894 895 896 897 898 899 900 901 902 903 904 905 906 907
    else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_COLLECTION))
        openFunc = CRYPT_CollectionOpenStore;
    else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_SYSTEM_REGISTRY))
        openFunc = CRYPT_SysRegOpenStoreW;
    else
    {
        FIXME("unimplemented type %s\n", lpszStoreProvider);
        openFunc = NULL;
    }

    if (!openFunc)
        hcs = CRYPT_ProvOpenStore(lpszStoreProvider, dwMsgAndCertEncodingType,
         hCryptProv, dwFlags, pvPara);
    else
        hcs = openFunc(hCryptProv, dwFlags, pvPara);
908
    return hcs;
909 910
}

911
HCERTSTORE WINAPI CertOpenSystemStoreA(HCRYPTPROV_LEGACY hProv,
912 913 914 915
 LPCSTR szSubSystemProtocol)
{
    if (!szSubSystemProtocol)
    {
916
        SetLastError(E_INVALIDARG);
917 918 919 920 921 922
        return 0;
    }
    return CertOpenStore(CERT_STORE_PROV_SYSTEM_A, 0, hProv,
     CERT_SYSTEM_STORE_CURRENT_USER, szSubSystemProtocol);
}

923
HCERTSTORE WINAPI CertOpenSystemStoreW(HCRYPTPROV_LEGACY hProv,
924 925 926 927
 LPCWSTR szSubSystemProtocol)
{
    if (!szSubSystemProtocol)
    {
928
        SetLastError(E_INVALIDARG);
929 930 931 932 933 934
        return 0;
    }
    return CertOpenStore(CERT_STORE_PROV_SYSTEM_W, 0, hProv,
     CERT_SYSTEM_STORE_CURRENT_USER, szSubSystemProtocol);
}

935
PCCERT_CONTEXT WINAPI CertEnumCertificatesInStore(HCERTSTORE hCertStore, PCCERT_CONTEXT pPrev)
936
{
937
    cert_t *prev = pPrev ? cert_from_ptr(pPrev) : NULL, *ret;
938
    WINECRYPT_CERTSTORE *hcs = hCertStore;
939 940 941 942 943 944 945

    TRACE("(%p, %p)\n", hCertStore, pPrev);
    if (!hCertStore)
        ret = NULL;
    else if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
        ret = NULL;
    else
946 947
        ret = (cert_t*)hcs->vtbl->certs.enumContext(hcs, prev ? &prev->base : NULL);
    return ret ? &ret->ctx : NULL;
948 949 950 951
}

BOOL WINAPI CertDeleteCertificateFromStore(PCCERT_CONTEXT pCertContext)
{
952
    WINECRYPT_CERTSTORE *hcs;
953 954 955 956

    TRACE("(%p)\n", pCertContext);

    if (!pCertContext)
957
        return TRUE;
958

959 960
    hcs = pCertContext->hCertStore;

961 962 963 964
    if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
        return FALSE;

    return hcs->vtbl->certs.delete(hcs, &cert_from_ptr(pCertContext)->base);
965 966
}

967 968 969
BOOL WINAPI CertAddCRLContextToStore(HCERTSTORE hCertStore,
 PCCRL_CONTEXT pCrlContext, DWORD dwAddDisposition,
 PCCRL_CONTEXT* ppStoreContext)
970
{
971
    WINECRYPT_CERTSTORE *store = hCertStore;
972 973
    BOOL ret = TRUE;
    PCCRL_CONTEXT toAdd = NULL, existing = NULL;
974

975
    TRACE("(%p, %p, %08x, %p)\n", hCertStore, pCrlContext,
976
     dwAddDisposition, ppStoreContext);
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
    /* Weird case to pass a test */
    if (dwAddDisposition == 0)
    {
        SetLastError(STATUS_ACCESS_VIOLATION);
        return FALSE;
    }
    if (dwAddDisposition != CERT_STORE_ADD_ALWAYS)
    {
        existing = CertFindCRLInStore(hCertStore, 0, 0, CRL_FIND_EXISTING,
         pCrlContext, NULL);
    }

    switch (dwAddDisposition)
    {
    case CERT_STORE_ADD_ALWAYS:
        toAdd = CertDuplicateCRLContext(pCrlContext);
        break;
    case CERT_STORE_ADD_NEW:
        if (existing)
        {
            TRACE("found matching CRL, not adding\n");
            SetLastError(CRYPT_E_EXISTS);
            ret = FALSE;
        }
        else
            toAdd = CertDuplicateCRLContext(pCrlContext);
        break;
    case CERT_STORE_ADD_NEWER:
        if (existing)
        {
            LONG newer = CompareFileTime(&existing->pCrlInfo->ThisUpdate,
             &pCrlContext->pCrlInfo->ThisUpdate);

            if (newer < 0)
                toAdd = CertDuplicateCRLContext(pCrlContext);
            else
            {
                TRACE("existing CRL is newer, not adding\n");
                SetLastError(CRYPT_E_EXISTS);
                ret = FALSE;
            }
        }
        else
            toAdd = CertDuplicateCRLContext(pCrlContext);
        break;
1023 1024 1025 1026 1027 1028 1029 1030 1031
    case CERT_STORE_ADD_NEWER_INHERIT_PROPERTIES:
        if (existing)
        {
            LONG newer = CompareFileTime(&existing->pCrlInfo->ThisUpdate,
             &pCrlContext->pCrlInfo->ThisUpdate);

            if (newer < 0)
            {
                toAdd = CertDuplicateCRLContext(pCrlContext);
1032
                Context_CopyProperties(toAdd, existing);
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
            }
            else
            {
                TRACE("existing CRL is newer, not adding\n");
                SetLastError(CRYPT_E_EXISTS);
                ret = FALSE;
            }
        }
        else
            toAdd = CertDuplicateCRLContext(pCrlContext);
        break;
1044 1045 1046 1047 1048 1049
    case CERT_STORE_ADD_REPLACE_EXISTING:
        toAdd = CertDuplicateCRLContext(pCrlContext);
        break;
    case CERT_STORE_ADD_REPLACE_EXISTING_INHERIT_PROPERTIES:
        toAdd = CertDuplicateCRLContext(pCrlContext);
        if (existing)
1050
            Context_CopyProperties(toAdd, existing);
1051 1052 1053
        break;
    case CERT_STORE_ADD_USE_EXISTING:
        if (existing)
1054
        {
1055
            Context_CopyProperties(existing, pCrlContext);
1056 1057 1058 1059 1060
            if (ppStoreContext)
                *ppStoreContext = CertDuplicateCRLContext(existing);
        }
        else
            toAdd = CertDuplicateCRLContext(pCrlContext);
1061 1062
        break;
    default:
1063
        FIXME("Unimplemented add disposition %d\n", dwAddDisposition);
1064 1065 1066 1067 1068
        ret = FALSE;
    }

    if (toAdd)
    {
1069 1070 1071
        if (store) {
            context_t *ret_context;
            ret = store->vtbl->crls.addContext(store, context_from_ptr(toAdd),
1072
             existing ? context_from_ptr(existing) : NULL, ppStoreContext ? &ret_context : NULL, FALSE);
1073 1074 1075
            if (ret && ppStoreContext)
                *ppStoreContext = context_ptr(ret_context);
        }else if (ppStoreContext) {
1076
            *ppStoreContext = CertDuplicateCRLContext(toAdd);
1077
        }
1078 1079
        CertFreeCRLContext(toAdd);
    }
1080 1081
    if (existing)
        CertFreeCRLContext(existing);
1082 1083 1084

    TRACE("returning %d\n", ret);
    return ret;
1085 1086 1087 1088
}

BOOL WINAPI CertDeleteCRLFromStore(PCCRL_CONTEXT pCrlContext)
{
1089
    WINECRYPT_CERTSTORE *hcs;
1090 1091 1092 1093 1094
    BOOL ret;

    TRACE("(%p)\n", pCrlContext);

    if (!pCrlContext)
1095
        return TRUE;
1096

1097 1098
    hcs = pCrlContext->hCertStore;

1099 1100 1101 1102 1103 1104
    if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
        return FALSE;

    ret = hcs->vtbl->crls.delete(hcs, &crl_from_ptr(pCrlContext)->base);
    if (ret)
        ret = CertFreeCRLContext(pCrlContext);
1105
    return ret;
1106 1107
}

1108
PCCRL_CONTEXT WINAPI CertEnumCRLsInStore(HCERTSTORE hCertStore, PCCRL_CONTEXT pPrev)
1109
{
1110
    crl_t *ret, *prev = pPrev ? crl_from_ptr(pPrev) : NULL;
1111
    WINECRYPT_CERTSTORE *hcs = hCertStore;
1112 1113 1114 1115 1116 1117 1118

    TRACE("(%p, %p)\n", hCertStore, pPrev);
    if (!hCertStore)
        ret = NULL;
    else if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
        ret = NULL;
    else
1119 1120
        ret = (crl_t*)hcs->vtbl->crls.enumContext(hcs, prev ? &prev->base : NULL);
    return ret ? &ret->ctx : NULL;
1121 1122
}

1123 1124
HCERTSTORE WINAPI CertDuplicateStore(HCERTSTORE hCertStore)
{
1125
    WINECRYPT_CERTSTORE *hcs = hCertStore;
1126 1127 1128 1129

    TRACE("(%p)\n", hCertStore);

    if (hcs && hcs->dwMagic == WINE_CRYPTCERTSTORE_MAGIC)
1130
        hcs->vtbl->addref(hcs);
1131 1132
    return hCertStore;
}
1133 1134 1135

BOOL WINAPI CertCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
{
1136
    WINECRYPT_CERTSTORE *hcs = hCertStore;
1137
    DWORD res;
1138

1139
    TRACE("(%p, %08x)\n", hCertStore, dwFlags);
1140 1141 1142 1143 1144 1145 1146

    if( ! hCertStore )
        return TRUE;

    if ( hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC )
        return FALSE;

1147 1148 1149 1150
    res = hcs->vtbl->release(hcs, dwFlags);
    if (res != ERROR_SUCCESS) {
        SetLastError(res);
        return FALSE;
1151
    }
1152

1153 1154 1155 1156 1157 1158
    return TRUE;
}

BOOL WINAPI CertControlStore(HCERTSTORE hCertStore, DWORD dwFlags,
 DWORD dwCtrlType, void const *pvCtrlPara)
{
1159
    WINECRYPT_CERTSTORE *hcs = hCertStore;
1160 1161
    BOOL ret;

1162
    TRACE("(%p, %08x, %d, %p)\n", hCertStore, dwFlags, dwCtrlType,
1163 1164 1165 1166 1167 1168 1169 1170
     pvCtrlPara);

    if (!hcs)
        ret = FALSE;
    else if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
        ret = FALSE;
    else
    {
1171 1172
        if (hcs->vtbl->control)
            ret = hcs->vtbl->control(hcs, dwFlags, dwCtrlType, pvCtrlPara);
1173 1174 1175 1176 1177 1178
        else
            ret = TRUE;
    }
    return ret;
}

1179 1180 1181
BOOL WINAPI CertGetStoreProperty(HCERTSTORE hCertStore, DWORD dwPropId,
 void *pvData, DWORD *pcbData)
{
1182
    WINECRYPT_CERTSTORE *store = hCertStore;
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
    BOOL ret = FALSE;

    TRACE("(%p, %d, %p, %p)\n", hCertStore, dwPropId, pvData, pcbData);

    switch (dwPropId)
    {
    case CERT_ACCESS_STATE_PROP_ID:
        if (!pvData)
        {
            *pcbData = sizeof(DWORD);
            ret = TRUE;
        }
        else if (*pcbData < sizeof(DWORD))
        {
            SetLastError(ERROR_MORE_DATA);
            *pcbData = sizeof(DWORD);
        }
        else
        {
1202 1203 1204 1205 1206 1207
            DWORD state = 0;

            if (store->type != StoreTypeMem &&
             !(store->dwOpenFlags & CERT_STORE_READONLY_FLAG))
                state |= CERT_ACCESS_STATE_WRITE_PERSIST_FLAG;
            *(DWORD *)pvData = state;
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
            ret = TRUE;
        }
        break;
    default:
        if (store->properties)
        {
            CRYPT_DATA_BLOB blob;

            ret = ContextPropertyList_FindProperty(store->properties, dwPropId,
             &blob);
            if (ret)
            {
                if (!pvData)
                    *pcbData = blob.cbData;
                else if (*pcbData < blob.cbData)
                {
                    SetLastError(ERROR_MORE_DATA);
                    *pcbData = blob.cbData;
                    ret = FALSE;
                }
                else
                {
                    memcpy(pvData, blob.pbData, blob.cbData);
                    *pcbData = blob.cbData;
                }
            }
            else
                SetLastError(CRYPT_E_NOT_FOUND);
        }
        else
            SetLastError(CRYPT_E_NOT_FOUND);
    }
    return ret;
}

BOOL WINAPI CertSetStoreProperty(HCERTSTORE hCertStore, DWORD dwPropId,
 DWORD dwFlags, const void *pvData)
{
1246
    WINECRYPT_CERTSTORE *store = hCertStore;
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
    BOOL ret = FALSE;

    TRACE("(%p, %d, %08x, %p)\n", hCertStore, dwPropId, dwFlags, pvData);

    if (!store->properties)
        store->properties = ContextPropertyList_Create();
    switch (dwPropId)
    {
    case CERT_ACCESS_STATE_PROP_ID:
        SetLastError(E_INVALIDARG);
        break;
    default:
        if (pvData)
        {
1261
            const CRYPT_DATA_BLOB *blob = pvData;
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274

            ret = ContextPropertyList_SetProperty(store->properties, dwPropId,
             blob->pbData, blob->cbData);
        }
        else
        {
            ContextPropertyList_RemoveProperty(store->properties, dwPropId);
            ret = TRUE;
        }
    }
    return ret;
}

1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
static LONG CRYPT_OpenParentStore(DWORD dwFlags,
    void *pvSystemStoreLocationPara, HKEY *key)
{
    HKEY root;
    LPCWSTR base;

    TRACE("(%08x, %p)\n", dwFlags, pvSystemStoreLocationPara);

    switch (dwFlags & CERT_SYSTEM_STORE_LOCATION_MASK)
    {
    case CERT_SYSTEM_STORE_LOCAL_MACHINE:
        root = HKEY_LOCAL_MACHINE;
        base = CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH;
        break;
    case CERT_SYSTEM_STORE_CURRENT_USER:
        root = HKEY_CURRENT_USER;
        base = CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH;
        break;
    case CERT_SYSTEM_STORE_CURRENT_SERVICE:
        /* hklm\Software\Microsoft\Cryptography\Services\servicename\
         * SystemCertificates
         */
        FIXME("CERT_SYSTEM_STORE_CURRENT_SERVICE\n");
        return ERROR_FILE_NOT_FOUND;
    case CERT_SYSTEM_STORE_SERVICES:
        /* hklm\Software\Microsoft\Cryptography\Services\servicename\
         * SystemCertificates
         */
1303
        FIXME("CERT_SYSTEM_STORE_SERVICES\n");
1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
        return ERROR_FILE_NOT_FOUND;
    case CERT_SYSTEM_STORE_USERS:
        /* hku\user sid\Software\Microsoft\SystemCertificates */
        FIXME("CERT_SYSTEM_STORE_USERS\n");
        return ERROR_FILE_NOT_FOUND;
    case CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY:
        root = HKEY_CURRENT_USER;
        base = CERT_GROUP_POLICY_SYSTEM_STORE_REGPATH;
        break;
    case CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY:
        root = HKEY_LOCAL_MACHINE;
        base = CERT_GROUP_POLICY_SYSTEM_STORE_REGPATH;
        break;
    case CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE:
        /* hklm\Software\Microsoft\EnterpriseCertificates */
        FIXME("CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE\n");
        return ERROR_FILE_NOT_FOUND;
    default:
        return ERROR_FILE_NOT_FOUND;
    }

    return RegOpenKeyExW(root, base, 0, KEY_READ, key);
}

BOOL WINAPI CertEnumSystemStore(DWORD dwFlags, void *pvSystemStoreLocationPara,
    void *pvArg, PFN_CERT_ENUM_SYSTEM_STORE pfnEnum)
{
    BOOL ret = FALSE;
    LONG rc;
    HKEY key;
1334
    CERT_SYSTEM_STORE_INFO info = { sizeof(info) };
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351

    TRACE("(%08x, %p, %p, %p)\n", dwFlags, pvSystemStoreLocationPara, pvArg,
        pfnEnum);

    rc = CRYPT_OpenParentStore(dwFlags, pvArg, &key);
    if (!rc)
    {
        DWORD index = 0;

        ret = TRUE;
        do {
            WCHAR name[MAX_PATH];
            DWORD size = sizeof(name) / sizeof(name[0]);

            rc = RegEnumKeyExW(key, index++, name, &size, NULL, NULL, NULL,
                NULL);
            if (!rc)
1352
                ret = pfnEnum(name, dwFlags, &info, NULL, pvArg);
1353 1354 1355 1356 1357 1358
        } while (ret && !rc);
        if (ret && rc != ERROR_NO_MORE_ITEMS)
            SetLastError(rc);
    }
    else
        SetLastError(rc);
1359 1360 1361 1362 1363 1364
    /* Include root store for the local machine location (it isn't in the
     * registry)
     */
    if (ret && (dwFlags & CERT_SYSTEM_STORE_LOCATION_MASK) ==
     CERT_SYSTEM_STORE_LOCAL_MACHINE)
        ret = pfnEnum(rootW, dwFlags, &info, NULL, pvArg);
1365 1366
    return ret;
}
1367 1368 1369 1370 1371 1372 1373 1374

BOOL WINAPI CertEnumPhysicalStore(const void *pvSystemStore, DWORD dwFlags,
 void *pvArg, PFN_CERT_ENUM_PHYSICAL_STORE pfnEnum)
{
    if (dwFlags & CERT_SYSTEM_STORE_RELOCATE_FLAG)
        FIXME("(%p, %08x, %p, %p): stub\n", pvSystemStore, dwFlags, pvArg,
         pfnEnum);
    else
1375
        FIXME("(%s, %08x, %p, %p): stub\n", debugstr_w(pvSystemStore),
1376 1377 1378 1379
         dwFlags, pvArg,
         pfnEnum);
    return FALSE;
}
1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391

BOOL WINAPI CertRegisterPhysicalStore(const void *pvSystemStore, DWORD dwFlags,
 LPCWSTR pwszStoreName, PCERT_PHYSICAL_STORE_INFO pStoreInfo, void *pvReserved)
{
    if (dwFlags & CERT_SYSTEM_STORE_RELOCATE_FLAG)
        FIXME("(%p, %08x, %s, %p, %p): stub\n", pvSystemStore, dwFlags,
         debugstr_w(pwszStoreName), pStoreInfo, pvReserved);
    else
        FIXME("(%s, %08x, %s, %p, %p): stub\n", debugstr_w(pvSystemStore),
         dwFlags, debugstr_w(pwszStoreName), pStoreInfo, pvReserved);
    return FALSE;
}
1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403

static void EmptyStore_addref(WINECRYPT_CERTSTORE *store)
{
    TRACE("(%p)\n", store);
}

static DWORD EmptyStore_release(WINECRYPT_CERTSTORE *store, DWORD flags)
{
    TRACE("(%p)\n", store);
    return E_UNEXPECTED;
}

1404 1405 1406 1407 1408
static void EmptyStore_releaseContext(WINECRYPT_CERTSTORE *store, context_t *context)
{
    Context_Free(context);
}

1409 1410
static BOOL EmptyStore_add(WINECRYPT_CERTSTORE *store, context_t *context,
 context_t *replace, context_t **ret_context, BOOL use_link)
1411 1412 1413 1414 1415
{
    TRACE("(%p, %p, %p, %p)\n", store, context, replace, ret_context);

    /* FIXME: We should clone the context */
    if(ret_context) {
1416
        Context_AddRef(context);
1417 1418 1419 1420 1421 1422
        *ret_context = context;
    }

    return TRUE;
}

1423
static context_t *EmptyStore_enum(WINECRYPT_CERTSTORE *store, context_t *prev)
1424 1425 1426 1427
{
    TRACE("(%p, %p)\n", store, prev);

    SetLastError(CRYPT_E_NOT_FOUND);
1428
    return NULL;
1429 1430
}

1431
static BOOL EmptyStore_delete(WINECRYPT_CERTSTORE *store, context_t *context)
1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446
{
    return TRUE;
}

static BOOL EmptyStore_control(WINECRYPT_CERTSTORE *store, DWORD flags, DWORD ctrl_type, void const *ctrl_para)
{
    TRACE("()\n");

    SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
    return FALSE;
}

static const store_vtbl_t EmptyStoreVtbl = {
    EmptyStore_addref,
    EmptyStore_release,
1447
    EmptyStore_releaseContext,
1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469
    EmptyStore_control,
    {
        EmptyStore_add,
        EmptyStore_enum,
        EmptyStore_delete
    }, {
        EmptyStore_add,
        EmptyStore_enum,
        EmptyStore_delete
    }, {
        EmptyStore_add,
        EmptyStore_enum,
        EmptyStore_delete
    }
};

WINECRYPT_CERTSTORE empty_store;

void init_empty_store(void)
{
    CRYPT_InitStore(&empty_store, CERT_STORE_READONLY_FLAG, StoreTypeEmpty, &EmptyStoreVtbl);
}