Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / net / cert / test_root_certs_win.cc
blob4e941e2a0e29c010673bfd592b37855befb71eb4
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/cert/test_root_certs.h"
7 #include "base/basictypes.h"
8 #include "base/lazy_instance.h"
9 #include "base/logging.h"
10 #include "base/win/win_util.h"
11 #include "base/win/windows_version.h"
12 #include "net/cert/x509_certificate.h"
14 namespace net {
16 namespace {
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.
21 BOOL 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);
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.
33 struct 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;
39 // The handle that CryptoAPI uses to ensure the DLL implementing
40 // |original_function| remains loaded in memory.
41 HCRYPTOIDFUNCADDR original_handle;
43 private:
44 friend struct base::DefaultLazyInstanceTraits<CryptoAPIInjector>;
46 CryptoAPIInjector()
47 : original_function(NULL),
48 original_handle(NULL) {
49 HCRYPTOIDFUNCSET registered_functions =
50 CryptInitOIDFunctionSet(CRYPT_OID_OPEN_STORE_PROV_FUNC, 0);
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);
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 };
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);
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);
89 base::LazyInstance<CryptoAPIInjector>::Leaky
90 g_capi_injector = LAZY_INSTANCE_INITIALIZER;
92 BOOL 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.
102 uint32 store_as_uint = reinterpret_cast<uint32>(store_provider);
103 if (store_as_uint > 0xFFFF || store_provider != CERT_STORE_PROV_SYSTEM_W ||
104 !g_capi_injector.Get().original_function)
105 return FALSE;
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;
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);
137 } // namespace
139 bool 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();
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.
150 return GetLastError() == CRYPT_E_EXISTS;
153 empty_ = false;
154 return true;
157 void TestRootCerts::Clear() {
158 empty_ = true;
160 for (PCCERT_CONTEXT prev_cert =
161 CertEnumCertificatesInStore(temporary_roots_, NULL);
162 prev_cert;
163 prev_cert = CertEnumCertificatesInStore(temporary_roots_, NULL))
164 CertDeleteCertificateFromStore(prev_cert);
167 bool TestRootCerts::IsEmpty() const {
168 return empty_;
171 HCERTCHAINENGINE TestRootCerts::GetChainEngine() const {
172 if (IsEmpty())
173 return NULL; // Default chain engine will suffice.
175 // Windows versions before 7 don't accept the struct size for later versions.
176 // We report the size of the old struct since we don't need the new members.
177 static const DWORD kSizeofCertChainEngineConfig =
178 SIZEOF_STRUCT_WITH_SPECIFIED_LAST_MEMBER(
179 CERT_CHAIN_ENGINE_CONFIG, CycleDetectionModulus);
181 // 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 = {
189 kSizeofCertChainEngineConfig
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;
200 TestRootCerts::~TestRootCerts() {
201 CertCloseStore(temporary_roots_, 0);
204 void 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_);
212 } // namespace net