1 // Copyright 2014 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.
7 #include "base/macros.h"
8 #include "base/strings/stringprintf.h"
9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/chromeos/policy/user_network_configuration_updater.h"
11 #include "chrome/browser/extensions/extension_apitest.h"
12 #include "chrome/browser/net/nss_context.h"
13 #include "chrome/browser/net/url_request_mock_util.h"
14 #include "components/policy/core/browser/browser_policy_connector.h"
15 #include "components/policy/core/common/mock_configuration_policy_provider.h"
16 #include "components/policy/core/common/policy_map.h"
17 #include "content/public/browser/notification_service.h"
18 #include "content/public/common/content_switches.h"
19 #include "content/public/test/test_utils.h"
20 #include "content/public/test/test_utils.h"
21 #include "content/test/net/url_request_mock_http_job.h"
22 #include "crypto/nss_util.h"
23 #include "net/base/net_errors.h"
24 #include "net/cert/nss_cert_database.h"
25 #include "policy/policy_constants.h"
26 #include "testing/gmock/include/gmock/gmock.h"
30 // The test extension has a certificate referencing this private key.
32 // openssl genrsa > privkey.pem
33 // openssl pkcs8 -inform pem -in privkey.pem -topk8
34 // -outform der -out privkey8.der -nocrypt
35 // xxd -i privkey8.der
36 const unsigned char privateKeyPkcs8[] = {
37 0x30, 0x82, 0x01, 0x55, 0x02, 0x01, 0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a,
38 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82,
39 0x01, 0x3f, 0x30, 0x82, 0x01, 0x3b, 0x02, 0x01, 0x00, 0x02, 0x41, 0x00,
40 0xc7, 0xc1, 0x4d, 0xd5, 0xdc, 0x3a, 0x2e, 0x1f, 0x42, 0x30, 0x3d, 0x21,
41 0x1e, 0xa2, 0x1f, 0x60, 0xcb, 0x71, 0x11, 0x53, 0xb0, 0x75, 0xa0, 0x62,
42 0xfe, 0x5e, 0x0a, 0xde, 0xb0, 0x0f, 0x48, 0x97, 0x5e, 0x42,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x37, 0xde, 0xc7,
58 0xd1, 0x13, 0x55, 0xcd, 0x0a, 0x8c, 0x40, 0xcd, 0xb1, 0xed, 0xa5, 0xf1,
59 0x7d, 0x33, 0x64, 0x64, 0x5c, 0xfe, 0x5c, 0x6a, 0x34, 0x03, 0xb8, 0xc7,
60 0x02, 0x20, 0x17, 0xe1, 0xb5, 0x52, 0x3e, 0xfa, 0xc5, 0xc1, 0x80, 0xa7,
61 0x38, 0x88, 0x18, 0xca, 0x7b, 0x64, 0x3c, 0x93, 0x99, 0x61, 0x34, 0x87,
62 0x52, 0x27, 0x41, 0x37, 0xcc, 0x65, 0xf7, 0xa7, 0xcd, 0xc7, 0x02, 0x21,
63 0x00, 0x8a, 0x17, 0x7f, 0xf9, 0x45, 0xf3, 0xfd, 0xf7, 0x96, 0x62, 0xf3,
64 0x7a, 0x09, 0xfb, 0xe9, 0x9e, 0xc7, 0x7a, 0x1f, 0x53, 0x1a, 0xb8, 0xd5,
65 0x88, 0x9d, 0xd4, 0x79, 0x57, 0x88, 0x68, 0x72, 0x6f};
67 const base::FilePath::CharType kTestExtensionDir[] =
68 FILE_PATH_LITERAL("extensions/api_test/enterprise_platform_keys");
69 const base::FilePath::CharType kUpdateManifestFileName[] =
70 FILE_PATH_LITERAL("update_manifest.xml");
72 // The managed_storage extension has a key defined in its manifest, so that
73 // its extension ID is well-known and the policy system can push policies for
75 const char kTestExtensionID[] = "aecpbnckhoppanpmefllkdkohionpmig";
77 void DidGetCertDatabase(base::RunLoop* loop, net::NSSCertDatabase* cert_db) {
78 // In order to use a prepared certificate, import a private key at first for
79 // which the Javscript test will import the certificate.
82 // NSS requires non-const data even though it is just for input.
83 const_cast<unsigned char*>((const unsigned char*)privateKeyPkcs8),
84 arraysize(privateKeyPkcs8)};
86 crypto::ScopedPK11Slot private_slot(cert_db->GetPrivateSlot());
87 SECKEYPrivateKey* seckey_private_key = NULL;
89 PK11_ImportDERPrivateKeyInfoAndReturnKey(private_slot.get(),
101 class EnterprisePlatformKeysTest : public ExtensionApiTest {
103 virtual void SetUp() {
104 policy::UserNetworkConfigurationUpdater::
105 SetSkipCertificateImporterCreationForTest(true /*skip*/);
106 ExtensionApiTest::SetUp();
109 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
110 ExtensionApiTest::SetUpCommandLine(command_line);
112 // Enable the WebCrypto API.
113 command_line->AppendSwitch(
114 switches::kEnableExperimentalWebPlatformFeatures);
117 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
118 ExtensionApiTest::SetUpInProcessBrowserTestFixture();
120 EXPECT_CALL(policy_provider_, IsInitializationComplete(testing::_))
121 .WillRepeatedly(testing::Return(true));
122 policy_provider_.SetAutoRefresh();
123 policy::BrowserPolicyConnector::SetPolicyProviderForTesting(
127 virtual void SetUpOnMainThread() OVERRIDE {
128 ExtensionApiTest::SetUpOnMainThread();
130 // Enable the URLRequestMock, which is required for force-installing the
131 // test extension through policy.
132 content::BrowserThread::PostTask(
133 content::BrowserThread::IO,
135 base::Bind(chrome_browser_net::SetUrlRequestMocksEnabled, true));
139 content::BrowserThread::PostTask(
140 content::BrowserThread::IO,
142 base::Bind(&EnterprisePlatformKeysTest::SetupTestNSSDBOnIOThread,
143 base::Unretained(this),
150 GetNSSCertDatabaseForProfile(browser()->profile(),
151 base::Bind(&DidGetCertDatabase, &loop));
158 virtual void CleanUpOnMainThread() OVERRIDE {
160 content::BrowserThread::PostTask(
161 content::BrowserThread::IO,
163 base::Bind(&EnterprisePlatformKeysTest::DeleteTestNSSDBOnIOThread,
164 base::Unretained(this),
167 ExtensionApiTest::CleanUpOnMainThread();
171 void SetupTestNSSDBOnIOThread(base::RunLoop* loop) {
172 test_nssdb_.reset(new crypto::ScopedTestNSSDB);
173 content::BrowserThread::PostTask(
174 content::BrowserThread::UI, FROM_HERE, loop->QuitClosure());
177 void DeleteTestNSSDBOnIOThread(base::RunLoop* loop) {
179 content::BrowserThread::PostTask(
180 content::BrowserThread::UI, FROM_HERE, loop->QuitClosure());
184 // Extensions that are force-installed come from an update URL, which
185 // defaults to the webstore. Use a mock URL for this test with an update
186 // manifest that includes the crx file of the test extension.
187 base::FilePath update_manifest_path =
188 base::FilePath(kTestExtensionDir).Append(kUpdateManifestFileName);
189 GURL update_manifest_url(
190 content::URLRequestMockHTTPJob::GetMockUrl(update_manifest_path));
192 scoped_ptr<base::ListValue> forcelist(new base::ListValue);
193 forcelist->AppendString(base::StringPrintf(
194 "%s;%s", kTestExtensionID, update_manifest_url.spec().c_str()));
196 policy::PolicyMap policy;
197 policy.Set(policy::key::kExtensionInstallForcelist,
198 policy::POLICY_LEVEL_MANDATORY,
199 policy::POLICY_SCOPE_MACHINE,
203 // Set the policy and wait until the extension is installed.
204 content::WindowedNotificationObserver observer(
205 chrome::NOTIFICATION_EXTENSION_INSTALLED_DEPRECATED,
206 content::NotificationService::AllSources());
207 policy_provider_.UpdateChromePolicy(policy);
211 scoped_ptr<crypto::ScopedTestNSSDB> test_nssdb_;
212 policy::MockConfigurationPolicyProvider policy_provider_;
217 IN_PROC_BROWSER_TEST_F(EnterprisePlatformKeysTest, Basic) {
218 ASSERT_TRUE(RunExtensionSubtest(
220 base::StringPrintf("chrome-extension://%s/basic.html", kTestExtensionID)))