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"
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
,
23 HCRYPTPROV crypt_provider
,
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
;
44 friend struct base::DefaultLazyInstanceTraits
<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
);
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
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
);
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
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
,
94 HCRYPTPROV crypt_provider
,
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
)
107 BOOL ok
= g_capi_injector
.Get().original_function(store_provider
, encoding
,
108 crypt_provider
, flags
,
109 store_name
, memory_store
,
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
116 if (!ok
|| TestRootCerts::GetInstance()->IsEmpty() ||
117 (flags
& CERT_SYSTEM_STORE_RELOCATE_FLAG
) ||
118 lstrcmpiW(reinterpret_cast<LPCWSTR
>(store_name
), L
"root"))
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);
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
143 g_capi_injector
.Get();
145 BOOL ok
= CertAddCertificateContextToStore(
146 temporary_roots_
, certificate
->os_cert_handle(),
147 CERT_STORE_ADD_NEW
, NULL
);
149 // If the certificate is already added, return successfully.
150 return GetLastError() == CRYPT_E_EXISTS
;
157 void TestRootCerts::Clear() {
160 for (PCCERT_CONTEXT prev_cert
=
161 CertEnumCertificatesInStore(temporary_roots_
, NULL
);
163 prev_cert
= CertEnumCertificatesInStore(temporary_roots_
, NULL
))
164 CertDeleteCertificateFromStore(prev_cert
);
167 bool TestRootCerts::IsEmpty() const {
171 HCERTCHAINENGINE
TestRootCerts::GetChainEngine() const {
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
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
);
200 TestRootCerts::~TestRootCerts() {
201 CertCloseStore(temporary_roots_
, 0);
204 void TestRootCerts::Init() {
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_
);