1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/policy/user_network_configuration_updater.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/logging.h"
#include "base/values.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/chromeos/login/users/user.h"
#include "chrome/browser/chromeos/net/onc_utils.h"
#include "chrome/browser/net/nss_context.h"
#include "chrome/browser/profiles/profile.h"
#include "chromeos/network/managed_network_configuration_handler.h"
#include "chromeos/network/onc/onc_certificate_importer_impl.h"
#include "content/public/browser/notification_source.h"
#include "net/cert/x509_certificate.h"
#include "policy/policy_constants.h"
namespace {
bool skip_certificate_importer_creation_for_test = false;
} // namespace
namespace policy {
UserNetworkConfigurationUpdater::~UserNetworkConfigurationUpdater() {}
// static
scoped_ptr<UserNetworkConfigurationUpdater>
UserNetworkConfigurationUpdater::CreateForUserPolicy(
Profile* profile,
bool allow_trusted_certs_from_policy,
const chromeos::User& user,
PolicyService* policy_service,
chromeos::ManagedNetworkConfigurationHandler* network_config_handler) {
scoped_ptr<UserNetworkConfigurationUpdater> updater(
new UserNetworkConfigurationUpdater(profile,
allow_trusted_certs_from_policy,
user,
policy_service,
network_config_handler));
updater->Init();
return updater.Pass();
}
void UserNetworkConfigurationUpdater::AddTrustedCertsObserver(
WebTrustedCertsObserver* observer) {
observer_list_.AddObserver(observer);
}
void UserNetworkConfigurationUpdater::RemoveTrustedCertsObserver(
WebTrustedCertsObserver* observer) {
observer_list_.RemoveObserver(observer);
}
UserNetworkConfigurationUpdater::UserNetworkConfigurationUpdater(
Profile* profile,
bool allow_trusted_certs_from_policy,
const chromeos::User& user,
PolicyService* policy_service,
chromeos::ManagedNetworkConfigurationHandler* network_config_handler)
: NetworkConfigurationUpdater(onc::ONC_SOURCE_USER_POLICY,
key::kOpenNetworkConfiguration,
policy_service,
network_config_handler),
allow_trusted_certificates_from_policy_(allow_trusted_certs_from_policy),
user_(&user),
weak_factory_(this) {
// The updater is created with |certificate_importer_| unset and is
// responsible for creating it. This requires |GetNSSCertDatabaseForProfile|
// call, which is not safe before the profile initialization is finalized.
// Thus, listen for PROFILE_ADDED notification, on which |cert_importer_|
// creation should start. This behaviour can be disabled in tests.
if (!skip_certificate_importer_creation_for_test) {
registrar_.Add(this,
chrome::NOTIFICATION_PROFILE_ADDED,
content::Source<Profile>(profile));
}
}
void UserNetworkConfigurationUpdater::SetCertificateImporterForTest(
scoped_ptr<chromeos::onc::CertificateImporter> certificate_importer) {
SetCertificateImporter(certificate_importer.Pass());
}
// static
void UserNetworkConfigurationUpdater::
SetSkipCertificateImporterCreationForTest(bool skip) {
skip_certificate_importer_creation_for_test = skip;
}
void UserNetworkConfigurationUpdater::GetWebTrustedCertificates(
net::CertificateList* certs) const {
*certs = web_trust_certs_;
}
void UserNetworkConfigurationUpdater::ImportCertificates(
const base::ListValue& certificates_onc) {
// If certificate importer is not yet set, cache the certificate onc. It will
// be imported when the certificate importer gets set.
if (!certificate_importer_) {
pending_certificates_onc_.reset(certificates_onc.DeepCopy());
return;
}
web_trust_certs_.clear();
certificate_importer_->ImportCertificates(
certificates_onc,
onc_source_,
allow_trusted_certificates_from_policy_ ? &web_trust_certs_ : NULL);
NotifyTrustAnchorsChanged();
}
void UserNetworkConfigurationUpdater::ApplyNetworkPolicy(
base::ListValue* network_configs_onc,
base::DictionaryValue* global_network_config) {
DCHECK(user_);
chromeos::onc::ExpandStringPlaceholdersInNetworksForUser(user_,
network_configs_onc);
network_config_handler_->SetPolicy(onc_source_,
user_->username_hash(),
*network_configs_onc,
*global_network_config);
}
void UserNetworkConfigurationUpdater::Observe(
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
DCHECK_EQ(type, chrome::NOTIFICATION_PROFILE_ADDED);
Profile* profile = content::Source<Profile>(source).ptr();
if (skip_certificate_importer_creation_for_test)
return;
GetNSSCertDatabaseForProfile(
profile,
base::Bind(
&UserNetworkConfigurationUpdater::CreateAndSetCertificateImporter,
weak_factory_.GetWeakPtr()));
}
void UserNetworkConfigurationUpdater::CreateAndSetCertificateImporter(
net::NSSCertDatabase* database) {
DCHECK(database);
SetCertificateImporter(scoped_ptr<chromeos::onc::CertificateImporter>(
new chromeos::onc::CertificateImporterImpl(database)));
}
void UserNetworkConfigurationUpdater::SetCertificateImporter(
scoped_ptr<chromeos::onc::CertificateImporter> certificate_importer) {
certificate_importer_ = certificate_importer.Pass();
if (pending_certificates_onc_)
ImportCertificates(*pending_certificates_onc_);
pending_certificates_onc_.reset();
}
void UserNetworkConfigurationUpdater::NotifyTrustAnchorsChanged() {
FOR_EACH_OBSERVER(WebTrustedCertsObserver,
observer_list_,
OnTrustAnchorsChanged(web_trust_certs_));
}
} // namespace policy
|