Signed-off-by: Rémi Bernon rbernon@codeweavers.com --- dlls/ntoskrnl.exe/tests/ntoskrnl.c | 133 +++++++++++++++-------------- 1 file changed, 71 insertions(+), 62 deletions(-)
diff --git a/dlls/ntoskrnl.exe/tests/ntoskrnl.c b/dlls/ntoskrnl.exe/tests/ntoskrnl.c index b7926232cfb..99487cc0116 100644 --- a/dlls/ntoskrnl.exe/tests/ntoskrnl.c +++ b/dlls/ntoskrnl.exe/tests/ntoskrnl.c @@ -85,15 +85,9 @@ static void load_resource(const WCHAR *name, WCHAR *filename) CloseHandle( file ); }
-struct testsign_context -{ - const CERT_CONTEXT *cert, *root_cert, *publisher_cert; - HCERTSTORE root_store, publisher_store; -}; - static const WCHAR container_name[] = L"wine_testsign";
-static BOOL testsign_create_cert(struct testsign_context *ctx) +static const CERT_CONTEXT *testsign_create_cert(void) { BYTE encoded_name[100], encoded_key_id[200], public_key_info_buffer[1000]; BYTE hash_buffer[16], cert_buffer[1000], provider_nameA[100], serial[16]; @@ -101,16 +95,16 @@ static BOOL testsign_create_cert(struct testsign_context *ctx) CRYPT_KEY_PROV_INFO provider_info = {0}; CRYPT_ALGORITHM_IDENTIFIER algid = {0}; CERT_AUTHORITY_KEY_ID_INFO key_info; + HCERTSTORE root_store, pub_store; CERT_INFO cert_info = {0}; WCHAR provider_nameW[100]; + const CERT_CONTEXT *cert; CERT_EXTENSION extension; HCRYPTPROV provider; HCRYPTKEY key; DWORD size; BOOL ret;
- memset(ctx, 0, sizeof(*ctx)); - ret = CryptAcquireContextW(&provider, container_name, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET); if (!ret && GetLastError() == NTE_EXISTS) { @@ -176,8 +170,8 @@ static BOOL testsign_create_cert(struct testsign_context *ctx) X509_CERT_TO_BE_SIGNED, &cert_info, &algid, NULL, cert_buffer, &size); ok(ret, "Failed to create certificate, error %#x\n", GetLastError());
- ctx->cert = CertCreateCertificateContext(X509_ASN_ENCODING, cert_buffer, size); - ok(!!ctx->cert, "Failed to create context, error %#x\n", GetLastError()); + cert = CertCreateCertificateContext(X509_ASN_ENCODING, cert_buffer, size); + ok(!!cert, "Failed to create context, error %#x\n", GetLastError());
size = sizeof(provider_nameA); ret = CryptGetProvParam(provider, PP_NAME, provider_nameA, &size, 0); @@ -188,70 +182,85 @@ static BOOL testsign_create_cert(struct testsign_context *ctx) provider_info.pwszProvName = provider_nameW; provider_info.dwProvType = PROV_RSA_FULL; provider_info.dwKeySpec = AT_SIGNATURE; - ret = CertSetCertificateContextProperty(ctx->cert, CERT_KEY_PROV_INFO_PROP_ID, 0, &provider_info); + ret = CertSetCertificateContextProperty(cert, CERT_KEY_PROV_INFO_PROP_ID, 0, &provider_info); ok(ret, "Failed to set provider info, error %#x\n", GetLastError());
ret = CryptReleaseContext(provider, 0); ok(ret, "failed to release context, error %u\n", GetLastError());
- ctx->root_store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_A, 0, 0, CERT_SYSTEM_STORE_LOCAL_MACHINE, "root"); - if (!ctx->root_store && GetLastError() == ERROR_ACCESS_DENIED) + root_store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_A, 0, 0, CERT_SYSTEM_STORE_LOCAL_MACHINE, "root"); + if (!root_store && GetLastError() == ERROR_ACCESS_DENIED) { skip("Failed to open root store.\n"); - - ret = CertFreeCertificateContext(ctx->cert); + ret = CertFreeCertificateContext(cert); ok(ret, "Failed to free certificate, error %u\n", GetLastError()); - - return FALSE; + return NULL; } - ok(!!ctx->root_store, "Failed to open store, error %u\n", GetLastError()); - ret = CertAddCertificateContextToStore(ctx->root_store, ctx->cert, CERT_STORE_ADD_ALWAYS, &ctx->root_cert); + ok(!!root_store, "Failed to open store, error %u\n", GetLastError()); + ret = CertAddCertificateContextToStore(root_store, cert, CERT_STORE_ADD_ALWAYS, NULL); if (!ret && GetLastError() == ERROR_ACCESS_DENIED) { skip("Failed to add self-signed certificate to store.\n");
- ret = CertFreeCertificateContext(ctx->cert); + ret = CertFreeCertificateContext(cert); ok(ret, "Failed to free certificate, error %u\n", GetLastError()); - ret = CertCloseStore(ctx->root_store, CERT_CLOSE_STORE_CHECK_FLAG); + ret = CertCloseStore(root_store, CERT_CLOSE_STORE_CHECK_FLAG); ok(ret, "Failed to close store, error %u\n", GetLastError());
- return FALSE; + return NULL; } ok(ret, "Failed to add certificate, error %u\n", GetLastError()); + ret = CertCloseStore(root_store, CERT_CLOSE_STORE_CHECK_FLAG); + ok(ret, "Failed to close store, error %u\n", GetLastError());
- ctx->publisher_store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_A, 0, 0, + pub_store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_A, 0, 0, CERT_SYSTEM_STORE_LOCAL_MACHINE, "trustedpublisher"); - ok(!!ctx->publisher_store, "Failed to open store, error %u\n", GetLastError()); - ret = CertAddCertificateContextToStore(ctx->publisher_store, ctx->cert, - CERT_STORE_ADD_ALWAYS, &ctx->publisher_cert); + ok(!!pub_store, "Failed to open store, error %u\n", GetLastError()); + ret = CertAddCertificateContextToStore(pub_store, cert, CERT_STORE_ADD_ALWAYS, NULL); ok(ret, "Failed to add certificate, error %u\n", GetLastError()); + ret = CertCloseStore(pub_store, CERT_CLOSE_STORE_CHECK_FLAG); + ok(ret, "Failed to close store, error %u\n", GetLastError());
- return TRUE; + return cert; }
-static void testsign_cleanup(struct testsign_context *ctx) +static void testsign_cleanup(const CERT_CONTEXT *cert) { + HCERTSTORE root_store, pub_store; + const CERT_CONTEXT *store_cert; HCRYPTPROV provider; BOOL ret;
- ret = CertFreeCertificateContext(ctx->cert); - ok(ret, "Failed to free certificate, error %u\n", GetLastError()); - - ret = CertDeleteCertificateFromStore(ctx->root_cert); + root_store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_A, 0, 0, + CERT_SYSTEM_STORE_LOCAL_MACHINE, "root"); + ok(!!root_store, "Failed to open store, error %u\n", GetLastError()); + store_cert = CertFindCertificateInStore(root_store, X509_ASN_ENCODING, 0, + CERT_FIND_EXISTING, cert, NULL); + ok(!!store_cert, "Failed to find root certificate, error %u\n", GetLastError()); + ret = CertDeleteCertificateFromStore(store_cert); ok(ret, "Failed to remove certificate, error %u\n", GetLastError()); - ret = CertCloseStore(ctx->root_store, CERT_CLOSE_STORE_CHECK_FLAG); + ret = CertCloseStore(root_store, CERT_CLOSE_STORE_CHECK_FLAG); ok(ret, "Failed to close store, error %u\n", GetLastError());
- ret = CertDeleteCertificateFromStore(ctx->publisher_cert); + pub_store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_A, 0, 0, + CERT_SYSTEM_STORE_LOCAL_MACHINE, "trustedpublisher"); + ok(!!pub_store, "Failed to open store, error %u\n", GetLastError()); + store_cert = CertFindCertificateInStore(pub_store, X509_ASN_ENCODING, 0, + CERT_FIND_EXISTING, cert, NULL); + ok(!!store_cert, "Failed to find publisher certificate, error %u\n", GetLastError()); + ret = CertDeleteCertificateFromStore(store_cert); ok(ret, "Failed to remove certificate, error %u\n", GetLastError()); - ret = CertCloseStore(ctx->publisher_store, CERT_CLOSE_STORE_CHECK_FLAG); + ret = CertCloseStore(pub_store, CERT_CLOSE_STORE_CHECK_FLAG); ok(ret, "Failed to close store, error %u\n", GetLastError());
+ ret = CertFreeCertificateContext(cert); + ok(ret, "Failed to free certificate, error %u\n", GetLastError()); + ret = CryptAcquireContextW(&provider, container_name, NULL, PROV_RSA_FULL, CRYPT_DELETEKEYSET); ok(ret, "Failed to delete container, error %#x\n", GetLastError()); }
-static void testsign_sign(struct testsign_context *ctx, const WCHAR *filename) +static void testsign_sign(const CERT_CONTEXT *cert, const WCHAR *filename) { SIGNER_ATTR_AUTHCODE authcode = {sizeof(authcode)}; SIGNER_SIGNATURE_INFO signature = {sizeof(signature)}; @@ -268,7 +277,7 @@ static void testsign_sign(struct testsign_context *ctx, const WCHAR *filename) file.pwszFileName = (WCHAR *)filename; cert_info.dwCertChoice = 2; cert_info.pCertStoreInfo = &store; - store.pSigningCert = ctx->cert; + store.pSigningCert = cert; store.dwCertPolicy = 0; signature.algidHash = CALG_SHA_256; signature.dwAttrChoice = SIGNER_AUTHCODE_ATTR; @@ -298,7 +307,7 @@ static void unload_driver(SC_HANDLE service) CloseServiceHandle(service); }
-static SC_HANDLE load_driver(struct testsign_context *ctx, WCHAR *filename, +static SC_HANDLE load_driver(const CERT_CONTEXT *cert, WCHAR *filename, const WCHAR *resname, const WCHAR *driver_name) { SC_HANDLE manager, service; @@ -316,7 +325,7 @@ static SC_HANDLE load_driver(struct testsign_context *ctx, WCHAR *filename, if (service) unload_driver(service);
load_resource(resname, filename); - testsign_sign(ctx, filename); + testsign_sign(cert, filename); trace("Trying to load driver %s\n", debugstr_w(filename));
service = CreateServiceW(manager, driver_name, driver_name, @@ -1136,13 +1145,13 @@ static void test_object_info(void) CloseHandle(file); }
-static void test_driver3(struct testsign_context *ctx) +static void test_driver3(const CERT_CONTEXT *cert) { WCHAR filename[MAX_PATH]; SC_HANDLE service; BOOL ret;
- service = load_driver(ctx, filename, L"driver3.dll", L"WineTestDriver3"); + service = load_driver(cert, filename, L"driver3.dll", L"WineTestDriver3"); ok(service != NULL, "driver3 failed to load\n");
ret = StartServiceA(service, 0, NULL); @@ -1216,14 +1225,14 @@ static DWORD WINAPI wsk_test_thread(void *parameter) return TRUE; }
-static void test_driver_netio(struct testsign_context *ctx) +static void test_driver_netio(const CERT_CONTEXT *cert) { WCHAR filename[MAX_PATH]; SC_HANDLE service; HANDLE hthread; BOOL ret;
- if (!(service = load_driver(ctx, filename, L"driver_netio.dll", L"winetest_netio"))) + if (!(service = load_driver(cert, filename, L"driver_netio.dll", L"winetest_netio"))) return;
if (!start_driver(service, TRUE)) @@ -1413,7 +1422,7 @@ static void pnp_driver_stop(void) ok(ret || GetLastError() == ERROR_FILE_NOT_FOUND, "Failed to delete file, error %u\n", GetLastError()); }
-static void pnp_driver_start(struct testsign_context *ctx, const WCHAR *resource) +static void pnp_driver_start(const CERT_CONTEXT *cert, const WCHAR *resource) { static const WCHAR hardware_id[] = L"test_hardware_id\0"; SP_DEVINFO_DATA device = {sizeof(SP_DEVINFO_DATA)}; @@ -1456,7 +1465,7 @@ static void pnp_driver_start(struct testsign_context *ctx, const WCHAR *resource ret = CryptCATClose(catalog); ok(ret, "Failed to close catalog, error %u\n", GetLastError());
- testsign_sign(ctx, L"winetest.cat"); + testsign_sign(cert, L"winetest.cat");
/* Install the driver. */
@@ -1838,7 +1847,7 @@ static void test_pnp_devices(void) UnregisterClassA("ntoskrnl_test_wc", GetModuleHandleA(NULL)); }
-static void test_pnp_driver(struct testsign_context *ctx) +static void test_pnp_driver(const CERT_CONTEXT *cert) { char cwd[MAX_PATH], tempdir[MAX_PATH];
@@ -1846,7 +1855,7 @@ static void test_pnp_driver(struct testsign_context *ctx) GetTempPathA(ARRAY_SIZE(tempdir), tempdir); SetCurrentDirectoryA(tempdir);
- pnp_driver_start(ctx, L"driver_pnp.dll"); + pnp_driver_start(cert, L"driver_pnp.dll"); test_pnp_devices(); pnp_driver_stop();
@@ -3323,7 +3332,7 @@ static void test_hid_device(DWORD report_id, DWORD polled) winetest_pop_context(); }
-static void test_hid_driver(struct testsign_context *ctx, DWORD report_id, DWORD polled) +static void test_hid_driver(const CERT_CONTEXT *cert, DWORD report_id, DWORD polled) { char cwd[MAX_PATH], tempdir[MAX_PATH]; LSTATUS status; @@ -3342,7 +3351,7 @@ static void test_hid_driver(struct testsign_context *ctx, DWORD report_id, DWORD status = RegSetValueExW(hkey, L"PolledMode", 0, REG_DWORD, (void *)&polled, sizeof(polled)); ok(!status, "RegSetValueExW returned %#x\n", status);
- pnp_driver_start(ctx, L"driver_hid.dll"); + pnp_driver_start(cert, L"driver_hid.dll"); test_hid_device(report_id, polled); pnp_driver_stop();
@@ -3352,8 +3361,8 @@ static void test_hid_driver(struct testsign_context *ctx, DWORD report_id, DWORD START_TEST(ntoskrnl) { WCHAR filename[MAX_PATH], filename2[MAX_PATH]; - struct testsign_context ctx; SC_HANDLE service, service2; + const CERT_CONTEXT *cert; BOOL ret, is_wow64; HANDLE mapping; DWORD written; @@ -3372,7 +3381,7 @@ START_TEST(ntoskrnl) return; }
- if (!testsign_create_cert(&ctx)) + if (!(cert = testsign_create_cert())) return;
mapping = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, @@ -3388,7 +3397,7 @@ START_TEST(ntoskrnl) ok(okfile != INVALID_HANDLE_VALUE, "failed to create file, error %u\n", GetLastError());
subtest("driver"); - if (!(service = load_driver(&ctx, filename, L"driver.dll", L"WineTestDriver"))) + if (!(service = load_driver(cert, filename, L"driver.dll", L"WineTestDriver"))) goto out;
if (!start_driver(service, FALSE)) @@ -3396,7 +3405,7 @@ START_TEST(ntoskrnl) DeleteFileW(filename); goto out; } - service2 = load_driver(&ctx, filename2, L"driver2.dll", L"WineTestDriver2"); + service2 = load_driver(cert, filename2, L"driver2.dll", L"WineTestDriver2");
device = CreateFileA("\\.\WineTestDriver", 0, 0, NULL, OPEN_EXISTING, 0, NULL); ok(device != INVALID_HANDLE_VALUE, "failed to open device: %u\n", GetLastError()); @@ -3428,21 +3437,21 @@ START_TEST(ntoskrnl)
cat_okfile();
- test_driver3(&ctx); + test_driver3(cert); subtest("driver_netio"); - test_driver_netio(&ctx); + test_driver_netio(cert);
subtest("driver_pnp"); - test_pnp_driver(&ctx); + test_pnp_driver(cert);
subtest("driver_hid"); - test_hid_driver(&ctx, 0, FALSE); - test_hid_driver(&ctx, 1, FALSE); - test_hid_driver(&ctx, 0, TRUE); - test_hid_driver(&ctx, 1, TRUE); + test_hid_driver(cert, 0, FALSE); + test_hid_driver(cert, 1, FALSE); + test_hid_driver(cert, 0, TRUE); + test_hid_driver(cert, 1, TRUE);
out: - testsign_cleanup(&ctx); + testsign_cleanup(cert); UnmapViewOfFile(test_data); CloseHandle(mapping); CloseHandle(okfile);