blob: e13f173b8321e5df396c00d31495935aade43834 [file] [log] [blame]
[email protected]67f92bc32012-01-26 01:56:191// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]32765f82010-12-16 00:01:372// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]6e7845ae2013-03-29 21:48:115#include "net/cert/test_root_certs.h"
[email protected]32765f82010-12-16 00:01:376
wtc69f8ea82015-06-04 00:08:137#include <stdint.h>
8
[email protected]32765f82010-12-16 00:01:379#include "base/lazy_instance.h"
10#include "base/logging.h"
[email protected]a89e0942011-09-26 16:06:4711#include "base/win/win_util.h"
[email protected]6e7845ae2013-03-29 21:48:1112#include "net/cert/x509_certificate.h"
[email protected]32765f82010-12-16 00:01:3713
14namespace net {
15
16namespace {
17
18// Provides a CertDllOpenStoreProv callback provider function, to be called
19// by CertOpenStore when the CERT_STORE_PROV_SYSTEM_W store is opened. See
20// http://msdn.microsoft.com/en-us/library/aa376043(VS.85).aspx.
21BOOL WINAPI InterceptedOpenStoreW(LPCSTR store_provider,
22 DWORD encoding,
23 HCRYPTPROV crypt_provider,
24 DWORD flags,
25 const void* extra,
26 HCERTSTORE memory_store,
27 PCERT_STORE_PROV_INFO store_info);
28
29// CryptoAPIInjector is used to inject a store provider function for system
30// certificate stores before the one provided internally by Crypt32.dll.
31// Once injected, there is no way to remove, so every call to open a system
32// store will be redirected to the injected function.
33struct CryptoAPIInjector {
34 // The previous default function for opening system stores. For most
35 // configurations, this should point to Crypt32's internal
36 // I_CertDllOpenSystemStoreProvW function.
37 PFN_CERT_DLL_OPEN_STORE_PROV_FUNC original_function;
38
39 // The handle that CryptoAPI uses to ensure the DLL implementing
40 // |original_function| remains loaded in memory.
41 HCRYPTOIDFUNCADDR original_handle;
42
43 private:
scottmg5e65e3a2017-03-08 08:48:4644 friend struct base::LazyInstanceTraitsBase<CryptoAPIInjector>;
[email protected]32765f82010-12-16 00:01:3745
46 CryptoAPIInjector()
47 : original_function(NULL),
48 original_handle(NULL) {
49 HCRYPTOIDFUNCSET registered_functions =
50 CryptInitOIDFunctionSet(CRYPT_OID_OPEN_STORE_PROV_FUNC, 0);
51
52 // Preserve the original handler function in |original_function|. If other
53 // functions are overridden, they will also need to be preserved.
54 BOOL ok = CryptGetOIDFunctionAddress(
55 registered_functions, 0, CERT_STORE_PROV_SYSTEM_W, 0,
56 reinterpret_cast<void**>(&original_function), &original_handle);
57 DCHECK(ok);
58
59 // For now, intercept only the numeric form of the system store
60 // function, CERT_STORE_PROV_SYSTEM_W (0x0A), which is what Crypt32
61 // functionality uses exclusively. Depending on the machine that tests
62 // are being run on, it may prove necessary to also intercept
63 // sz_CERT_STORE_PROV_SYSTEM_[A/W] and CERT_STORE_PROV_SYSTEM_A, based
64 // on whether or not any third-party CryptoAPI modules have been
65 // installed.
66 const CRYPT_OID_FUNC_ENTRY kFunctionToIntercept =
67 { CERT_STORE_PROV_SYSTEM_W, &InterceptedOpenStoreW };
68
69 // Inject kFunctionToIntercept at the front of the linked list that
70 // crypt32 uses when CertOpenStore is called, replacing the existing
71 // registered function.
72 ok = CryptInstallOIDFunctionAddress(NULL, 0,
73 CRYPT_OID_OPEN_STORE_PROV_FUNC, 1,
74 &kFunctionToIntercept,
75 CRYPT_INSTALL_OID_FUNC_BEFORE_FLAG);
76 DCHECK(ok);
77 }
78
79 // This is never called, because this object is intentionally leaked.
80 // Certificate verification happens on a non-joinable worker thread, which
81 // may still be running when ~AtExitManager is called, so the LazyInstance
82 // must be leaky.
83 ~CryptoAPIInjector() {
84 original_function = NULL;
85 CryptFreeOIDFunctionAddress(original_handle, NULL);
86 }
87};
88
[email protected]67f92bc32012-01-26 01:56:1989base::LazyInstance<CryptoAPIInjector>::Leaky
[email protected]6de0fd1d2011-11-15 13:31:4990 g_capi_injector = LAZY_INSTANCE_INITIALIZER;
[email protected]32765f82010-12-16 00:01:3791
92BOOL WINAPI InterceptedOpenStoreW(LPCSTR store_provider,
93 DWORD encoding,
94 HCRYPTPROV crypt_provider,
95 DWORD flags,
96 const void* store_name,
97 HCERTSTORE memory_store,
98 PCERT_STORE_PROV_INFO store_info) {
99 // If the high word is all zeroes, then |store_provider| is a numeric ID.
100 // Otherwise, it's a pointer to a null-terminated ASCII string. See the
101 // documentation for CryptGetOIDFunctionAddress for more information.
brucedawsond3beca72015-12-10 02:22:44102 uintptr_t store_as_uintptr = reinterpret_cast<uintptr_t>(store_provider);
103 if (store_as_uintptr > 0xFFFF || store_provider != CERT_STORE_PROV_SYSTEM_W ||
[email protected]32765f82010-12-16 00:01:37104 !g_capi_injector.Get().original_function)
105 return FALSE;
106
107 BOOL ok = g_capi_injector.Get().original_function(store_provider, encoding,
108 crypt_provider, flags,
109 store_name, memory_store,
110 store_info);
111 // Only the Root store should have certificates injected. If
112 // CERT_SYSTEM_STORE_RELOCATE_FLAG is set, then |store_name| points to a
113 // CERT_SYSTEM_STORE_RELOCATE_PARA structure, rather than a
114 // NULL-terminated wide string, so check before making a string
115 // comparison.
116 if (!ok || TestRootCerts::GetInstance()->IsEmpty() ||
117 (flags & CERT_SYSTEM_STORE_RELOCATE_FLAG) ||
118 lstrcmpiW(reinterpret_cast<LPCWSTR>(store_name), L"root"))
119 return ok;
120
121 // The result of CertOpenStore with CERT_STORE_PROV_SYSTEM_W is documented
122 // to be a collection store, and that appears to hold for |memory_store|.
123 // Attempting to add an individual certificate to |memory_store| causes
124 // the request to be forwarded to the first physical store in the
125 // collection that accepts modifications, which will cause a secure
126 // confirmation dialog to be displayed, confirming the user wishes to
127 // trust the certificate. However, appending a store to the collection
128 // will merely modify the temporary collection store, and will not persist
129 // any changes to the underlying physical store. When the |memory_store| is
130 // searched to see if a certificate is in the Root store, all the
131 // underlying stores in the collection will be searched, and any certificate
132 // in temporary_roots() will be found and seen as trusted.
133 return CertAddStoreToCollection(
134 memory_store, TestRootCerts::GetInstance()->temporary_roots(), 0, 0);
135}
136
137} // namespace
138
139bool TestRootCerts::Add(X509Certificate* certificate) {
140 // Ensure that the default CryptoAPI functionality has been intercepted.
141 // If a test certificate is never added, then no interception should
142 // happen.
143 g_capi_injector.Get();
144
145 BOOL ok = CertAddCertificateContextToStore(
146 temporary_roots_, certificate->os_cert_handle(),
147 CERT_STORE_ADD_NEW, NULL);
148 if (!ok) {
149 // If the certificate is already added, return successfully.
thakis9230c1d2015-12-10 00:36:25150 return GetLastError() == static_cast<DWORD>(CRYPT_E_EXISTS);
[email protected]32765f82010-12-16 00:01:37151 }
152
153 empty_ = false;
154 return true;
155}
156
157void TestRootCerts::Clear() {
158 empty_ = true;
159
[email protected]6547ccdb2014-07-08 23:22:43160 for (PCCERT_CONTEXT prev_cert =
161 CertEnumCertificatesInStore(temporary_roots_, NULL);
162 prev_cert;
163 prev_cert = CertEnumCertificatesInStore(temporary_roots_, NULL))
[email protected]32765f82010-12-16 00:01:37164 CertDeleteCertificateFromStore(prev_cert);
165}
166
167bool TestRootCerts::IsEmpty() const {
168 return empty_;
169}
170
171HCERTCHAINENGINE TestRootCerts::GetChainEngine() const {
172 if (IsEmpty())
173 return NULL; // Default chain engine will suffice.
174
martijn274a3f42016-05-20 11:50:04175 // Windows versions before 8 don't accept the struct size for later versions.
[email protected]a89e0942011-09-26 16:06:47176 // We report the size of the old struct since we don't need the new members.
177 static const DWORD kSizeofCertChainEngineConfig =
martijn274a3f42016-05-20 11:50:04178 SIZEOF_STRUCT_WITH_SPECIFIED_LAST_MEMBER(CERT_CHAIN_ENGINE_CONFIG,
179 hExclusiveTrustedPeople);
[email protected]a89e0942011-09-26 16:06:47180
[email protected]32765f82010-12-16 00:01:37181 // Each HCERTCHAINENGINE caches both the configured system stores and
182 // information about each chain that has been built. In order to ensure
183 // that changes to |temporary_roots_| are properly propagated and that the
184 // various caches are flushed, when at least one certificate is added,
185 // return a new chain engine for every call. Each chain engine creation
186 // should re-open the root store, ensuring the most recent changes are
187 // visible.
188 CERT_CHAIN_ENGINE_CONFIG engine_config = {
[email protected]a89e0942011-09-26 16:06:47189 kSizeofCertChainEngineConfig
[email protected]32765f82010-12-16 00:01:37190 };
191 engine_config.dwFlags =
192 CERT_CHAIN_ENABLE_CACHE_AUTO_UPDATE |
193 CERT_CHAIN_ENABLE_SHARE_STORE;
194 HCERTCHAINENGINE chain_engine = NULL;
195 BOOL ok = CertCreateCertificateChainEngine(&engine_config, &chain_engine);
196 DCHECK(ok);
197 return chain_engine;
198}
199
200TestRootCerts::~TestRootCerts() {
201 CertCloseStore(temporary_roots_, 0);
202}
203
204void TestRootCerts::Init() {
205 empty_ = true;
206 temporary_roots_ = CertOpenStore(
207 CERT_STORE_PROV_MEMORY, 0, NULL,
208 CERT_STORE_DEFER_CLOSE_UNTIL_LAST_FREE_FLAG, NULL);
209 DCHECK(temporary_roots_);
210}
211
212} // namespace net