blob: b888447877e6f3c22451e30acf1b957472b18d82 [file] [log] [blame]
Bartosz Fabianowskic4ee8bf2012-01-25 16:31:26 +01001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Julian Pastarmov2638fbc2011-07-20 14:30:46 +02002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Ben Chanc7b97b82014-04-24 00:04:42 -07005#include "chromeos/policy/device_policy_impl.h"
Julian Pastarmov2638fbc2011-07-20 14:30:46 +02006
Chris Masonef8476592013-02-12 10:18:30 -08007#include <base/basictypes.h>
Chris Masonef8476592013-02-12 10:18:30 -08008#include <base/file_util.h>
9#include <base/logging.h>
Julian Pastarmov2638fbc2011-07-20 14:30:46 +020010#include <openssl/evp.h>
11#include <openssl/x509.h>
12
Bertrand SIMONNETb54b6dc2014-07-02 12:13:56 -070013#include <set>
Julian Pastarmov2638fbc2011-07-20 14:30:46 +020014#include <string>
15
Julian Pastarmov2638fbc2011-07-20 14:30:46 +020016#include "bindings/chrome_device_policy.pb.h"
17#include "bindings/device_management_backend.pb.h"
18
19namespace policy {
20
21namespace {
22const char kPolicyPath[] = "/var/lib/whitelist/policy";
23const char kPublicKeyPath[] = "/var/lib/whitelist/owner.key";
24
25// Reads the public key used to sign the policy from |key_file| and stores it
26// in |public_key|. Returns true on success.
Ben Chanc7b97b82014-04-24 00:04:42 -070027bool ReadPublicKeyFromFile(const base::FilePath& key_file,
28 std::string* public_key) {
29 if (!base::PathExists(key_file))
Julian Pastarmov2638fbc2011-07-20 14:30:46 +020030 return false;
31 public_key->clear();
Ben Chanc7b97b82014-04-24 00:04:42 -070032 if (!base::ReadFileToString(key_file, public_key) || public_key->empty()) {
Julian Pastarmov2638fbc2011-07-20 14:30:46 +020033 LOG(ERROR) << "Could not read public key off disk";
34 return false;
35 }
36 return true;
37}
38
39// Verifies that the |signed_data| has correct |signature| with |public_key|.
40bool VerifySignature(std::string& signed_data,
41 std::string& signature,
42 std::string& public_key) {
43 EVP_MD_CTX ctx;
44 EVP_MD_CTX_init(&ctx);
45
46 const EVP_MD* digest = EVP_sha1();
47
48 char* key = const_cast<char*>(public_key.data());
49 BIO* bio = BIO_new_mem_buf(key, public_key.length());
50 if (!bio) {
51 EVP_MD_CTX_cleanup(&ctx);
52 return false;
53 }
54
55 EVP_PKEY* public_key_ssl = d2i_PUBKEY_bio(bio, NULL);
56 if (!public_key_ssl) {
57 BIO_free_all(bio);
58 EVP_MD_CTX_cleanup(&ctx);
59 return false;
60 }
61
62 const unsigned char* sig =
63 reinterpret_cast<const unsigned char*>(signature.data());
64 int rv = EVP_VerifyInit_ex(&ctx, digest, NULL);
65 if (rv == 1) {
66 EVP_VerifyUpdate(&ctx, signed_data.data(), signed_data.length());
67 rv = EVP_VerifyFinal(&ctx,
68 sig, signature.length(),
69 public_key_ssl);
70 }
71
72 EVP_PKEY_free(public_key_ssl);
73 BIO_free_all(bio);
74 EVP_MD_CTX_cleanup(&ctx);
75
76 return rv == 1;
77}
78
Mattias Nisslerc13bcb32012-05-23 20:46:33 +020079// Decodes the connection type enum from the device settings protobuf to string
80// representations. The strings must match the connection manager definitions.
81std::string DecodeConnectionType(int type) {
82 static const char* const kConnectionTypes[] = {
83 "ethernet",
84 "wifi",
85 "wimax",
86 "bluetooth",
87 "cellular",
88 };
89
90 if (type < 0 || type >= static_cast<int>(arraysize(kConnectionTypes)))
91 return std::string();
92
93 return kConnectionTypes[type];
94}
95
Julian Pastarmov2638fbc2011-07-20 14:30:46 +020096} // namespace
97
98DevicePolicyImpl::DevicePolicyImpl()
99 : policy_path_(kPolicyPath),
100 keyfile_path_(kPublicKeyPath) {
101}
102
103DevicePolicyImpl::~DevicePolicyImpl() {
104}
105
106bool DevicePolicyImpl::LoadPolicy() {
107 if (!VerifyPolicyFiles()) {
Julian Pastarmov2638fbc2011-07-20 14:30:46 +0200108 return false;
109 }
110
111 std::string polstr;
Ben Chanc7b97b82014-04-24 00:04:42 -0700112 if (!base::ReadFileToString(policy_path_, &polstr) || polstr.empty()) {
Julian Pastarmov2638fbc2011-07-20 14:30:46 +0200113 LOG(ERROR) << "Could not read policy off disk";
114 return false;
115 }
116 if (!policy_.ParseFromString(polstr) || !policy_.has_policy_data()) {
117 LOG(ERROR) << "Policy on disk could not be parsed!";
118 return false;
119 }
Bartosz Fabianowskic4ee8bf2012-01-25 16:31:26 +0100120 policy_data_.ParseFromString(policy_.policy_data());
121 if (!policy_data_.has_policy_value()) {
Julian Pastarmov2638fbc2011-07-20 14:30:46 +0200122 LOG(ERROR) << "Policy on disk could not be parsed!";
123 return false;
124 }
125
126 // Make sure the signature is still valid.
127 if (!VerifyPolicySignature()) {
128 LOG(ERROR) << "Policy signature verification failed!";
129 return false;
130 }
131
Bartosz Fabianowskic4ee8bf2012-01-25 16:31:26 +0100132 device_policy_.ParseFromString(policy_data_.policy_value());
Julian Pastarmov2638fbc2011-07-20 14:30:46 +0200133 return true;
134}
135
Julian Pastarmov2638fbc2011-07-20 14:30:46 +0200136bool DevicePolicyImpl::GetPolicyRefreshRate(int* rate) const {
137 if (!device_policy_.has_device_policy_refresh_rate())
138 return false;
139 *rate = static_cast<int>(
140 device_policy_.device_policy_refresh_rate().device_policy_refresh_rate());
141 return true;
142}
143
Julian Pastarmov2638fbc2011-07-20 14:30:46 +0200144bool DevicePolicyImpl::GetUserWhitelist(
145 std::vector<std::string>* user_whitelist) const {
146 if (!device_policy_.has_user_whitelist())
147 return false;
148 const enterprise_management::UserWhitelistProto& proto =
149 device_policy_.user_whitelist();
150 user_whitelist->clear();
151 for (int i = 0; i < proto.user_whitelist_size(); i++)
152 user_whitelist->push_back(proto.user_whitelist(i));
153 return true;
154}
155
Julian Pastarmov2638fbc2011-07-20 14:30:46 +0200156bool DevicePolicyImpl::GetGuestModeEnabled(bool* guest_mode_enabled) const {
157 if (!device_policy_.has_guest_mode_enabled())
158 return false;
159 *guest_mode_enabled =
160 device_policy_.guest_mode_enabled().guest_mode_enabled();
161 return true;
162}
163
Julian Pastarmov2638fbc2011-07-20 14:30:46 +0200164bool DevicePolicyImpl::GetCameraEnabled(bool* camera_enabled) const {
165 if (!device_policy_.has_camera_enabled())
166 return false;
167 *camera_enabled = device_policy_.camera_enabled().camera_enabled();
168 return true;
169}
170
Julian Pastarmov2638fbc2011-07-20 14:30:46 +0200171bool DevicePolicyImpl::GetShowUserNames(bool* show_user_names) const {
172 if (!device_policy_.has_show_user_names())
173 return false;
174 *show_user_names = device_policy_.show_user_names().show_user_names();
175 return true;
176}
177
Julian Pastarmov2638fbc2011-07-20 14:30:46 +0200178bool DevicePolicyImpl::GetDataRoamingEnabled(bool* data_roaming_enabled) const {
179 if (!device_policy_.has_data_roaming_enabled())
180 return false;
181 *data_roaming_enabled =
182 device_policy_.data_roaming_enabled().data_roaming_enabled();
183 return true;
184}
185
Julian Pastarmov2638fbc2011-07-20 14:30:46 +0200186bool DevicePolicyImpl::GetAllowNewUsers(bool* allow_new_users) const {
187 if (!device_policy_.has_allow_new_users())
188 return false;
189 *allow_new_users = device_policy_.allow_new_users().allow_new_users();
190 return true;
191}
192
Julian Pastarmov2638fbc2011-07-20 14:30:46 +0200193bool DevicePolicyImpl::GetMetricsEnabled(bool* metrics_enabled) const {
194 if (!device_policy_.has_metrics_enabled())
195 return false;
196 *metrics_enabled = device_policy_.metrics_enabled().metrics_enabled();
197 return true;
198}
199
Bartosz Fabianowskia6732cd2012-02-07 11:54:21 +0100200bool DevicePolicyImpl::GetReportVersionInfo(bool* report_version_info) const {
201 if (!device_policy_.has_device_reporting())
202 return false;
203
204 const enterprise_management::DeviceReportingProto& proto =
205 device_policy_.device_reporting();
206 if (!proto.has_report_version_info())
207 return false;
208
209 *report_version_info = proto.report_version_info();
210 return true;
211}
212
Bartosz Fabianowskia6732cd2012-02-07 11:54:21 +0100213bool DevicePolicyImpl::GetReportActivityTimes(
214 bool* report_activity_times) const {
215 if (!device_policy_.has_device_reporting())
216 return false;
217
218 const enterprise_management::DeviceReportingProto& proto =
219 device_policy_.device_reporting();
220 if (!proto.has_report_activity_times())
221 return false;
222
223 *report_activity_times = proto.report_activity_times();
224 return true;
225}
226
Bartosz Fabianowskia6732cd2012-02-07 11:54:21 +0100227bool DevicePolicyImpl::GetReportBootMode(bool* report_boot_mode) const {
228 if (!device_policy_.has_device_reporting())
229 return false;
230
231 const enterprise_management::DeviceReportingProto& proto =
232 device_policy_.device_reporting();
233 if (!proto.has_report_boot_mode())
234 return false;
235
236 *report_boot_mode = proto.report_boot_mode();
237 return true;
238}
239
Bartosz Fabianowski43bd0ad2012-02-15 17:06:01 -0800240bool DevicePolicyImpl::GetEphemeralUsersEnabled(
241 bool* ephemeral_users_enabled) const {
242 if (!device_policy_.has_ephemeral_users_enabled())
Bartosz Fabianowskia6732cd2012-02-07 11:54:21 +0100243 return false;
Bartosz Fabianowski43bd0ad2012-02-15 17:06:01 -0800244 *ephemeral_users_enabled =
245 device_policy_.ephemeral_users_enabled().ephemeral_users_enabled();
Bartosz Fabianowskia6732cd2012-02-07 11:54:21 +0100246 return true;
247}
248
Julian Pastarmov2638fbc2011-07-20 14:30:46 +0200249bool DevicePolicyImpl::GetProxyMode(std::string* proxy_mode) const {
250 if (!device_policy_.has_device_proxy_settings())
251 return false;
252
253 const enterprise_management::DeviceProxySettingsProto& proto =
254 device_policy_.device_proxy_settings();
255 if (!proto.has_proxy_mode())
256 return false;
257
258 *proxy_mode = proto.proxy_mode();
259 return true;
260}
261
Julian Pastarmov2638fbc2011-07-20 14:30:46 +0200262bool DevicePolicyImpl::GetProxyServer(std::string* proxy_server) const {
263 if (!device_policy_.has_device_proxy_settings())
264 return false;
265
266 const enterprise_management::DeviceProxySettingsProto& proto =
267 device_policy_.device_proxy_settings();
268 if (!proto.has_proxy_server())
269 return false;
270
271 *proxy_server = proto.proxy_server();
272 return true;
273}
274
Julian Pastarmov2638fbc2011-07-20 14:30:46 +0200275bool DevicePolicyImpl::GetProxyPacUrl(std::string* proxy_pac) const {
276 if (!device_policy_.has_device_proxy_settings())
277 return false;
278
279 const enterprise_management::DeviceProxySettingsProto& proto =
280 device_policy_.device_proxy_settings();
281 if (!proto.has_proxy_pac_url())
282 return false;
283
284 *proxy_pac = proto.proxy_pac_url();
285 return true;
286}
287
Julian Pastarmov2638fbc2011-07-20 14:30:46 +0200288bool DevicePolicyImpl::GetProxyBypassList(
289 std::string* proxy_bypass_list) const {
290 if (!device_policy_.has_device_proxy_settings())
291 return false;
292
293 const enterprise_management::DeviceProxySettingsProto& proto =
294 device_policy_.device_proxy_settings();
295 if (!proto.has_proxy_bypass_list())
296 return false;
297
298 *proxy_bypass_list = proto.proxy_bypass_list();
299 return true;
300}
301
Patrick Dubroy9d979cb2011-08-01 15:52:35 +0200302bool DevicePolicyImpl::GetReleaseChannel(
303 std::string* release_channel) const {
304 if (!device_policy_.has_release_channel())
305 return false;
306
307 const enterprise_management::ReleaseChannelProto& proto =
308 device_policy_.release_channel();
309 if (!proto.has_release_channel())
310 return false;
311
312 *release_channel = proto.release_channel();
313 return true;
314}
315
Julian Pastarmovb1684852012-07-06 15:57:29 +0200316bool DevicePolicyImpl::GetReleaseChannelDelegated(
317 bool* release_channel_delegated) const {
318 if (!device_policy_.has_release_channel())
319 return false;
320
321 const enterprise_management::ReleaseChannelProto& proto =
322 device_policy_.release_channel();
323 if (!proto.has_release_channel_delegated())
324 return false;
325
326 *release_channel_delegated = proto.release_channel_delegated();
327 return true;
328}
329
Jay Srinivasancab5d3e2012-03-19 11:44:48 -0700330bool DevicePolicyImpl::GetUpdateDisabled(
331 bool* update_disabled) const {
332 if (!device_policy_.has_auto_update_settings())
333 return false;
334
335 const enterprise_management::AutoUpdateSettingsProto& proto =
336 device_policy_.auto_update_settings();
337 if (!proto.has_update_disabled())
338 return false;
339
340 *update_disabled = proto.update_disabled();
341 return true;
342}
343
Jay Srinivasancab5d3e2012-03-19 11:44:48 -0700344bool DevicePolicyImpl::GetTargetVersionPrefix(
345 std::string* target_version_prefix) const {
346 if (!device_policy_.has_auto_update_settings())
347 return false;
348
349 const enterprise_management::AutoUpdateSettingsProto& proto =
350 device_policy_.auto_update_settings();
351 if (!proto.has_target_version_prefix())
352 return false;
353
354 *target_version_prefix = proto.target_version_prefix();
355 return true;
356}
357
Jay Srinivasand7c86ae2012-05-31 19:24:56 -0700358bool DevicePolicyImpl::GetScatterFactorInSeconds(
359 int64* scatter_factor_in_seconds) const {
360 if (!device_policy_.has_auto_update_settings())
361 return false;
362
363 const enterprise_management::AutoUpdateSettingsProto& proto =
364 device_policy_.auto_update_settings();
365 if (!proto.has_scatter_factor_in_seconds())
366 return false;
367
368 *scatter_factor_in_seconds = proto.scatter_factor_in_seconds();
369 return true;
370}
371
Mattias Nisslerc13bcb32012-05-23 20:46:33 +0200372bool DevicePolicyImpl::GetAllowedConnectionTypesForUpdate(
373 std::set<std::string>* connection_types) const {
374 if (!device_policy_.has_auto_update_settings())
375 return false;
376
377 const enterprise_management::AutoUpdateSettingsProto& proto =
378 device_policy_.auto_update_settings();
379 if (proto.allowed_connection_types_size() <= 0)
380 return false;
381
382 for (int i = 0; i < proto.allowed_connection_types_size(); i++) {
383 std::string type = DecodeConnectionType(proto.allowed_connection_types(i));
384 if (!type.empty())
385 connection_types->insert(type);
386 }
387 return true;
388}
389
Bartosz Fabianowskia6732cd2012-02-07 11:54:21 +0100390bool DevicePolicyImpl::GetOpenNetworkConfiguration(
391 std::string* open_network_configuration) const {
392 if (!device_policy_.has_open_network_configuration())
393 return false;
394
395 const enterprise_management::DeviceOpenNetworkConfigurationProto& proto =
396 device_policy_.open_network_configuration();
397 if (!proto.has_open_network_configuration())
398 return false;
399
400 *open_network_configuration = proto.open_network_configuration();
401 return true;
402}
403
Bartosz Fabianowskic4ee8bf2012-01-25 16:31:26 +0100404bool DevicePolicyImpl::GetOwner(std::string* owner) const {
405 // The device is enterprise enrolled iff a request token exists.
406 if (policy_data_.has_request_token()) {
407 *owner = "";
408 return true;
409 }
410 if (!policy_data_.has_username())
411 return false;
412 *owner = policy_data_.username();
413 return true;
414}
415
Jay Srinivasan40563572013-05-14 18:11:14 -0700416bool DevicePolicyImpl::GetHttpDownloadsEnabled(
417 bool* http_downloads_enabled) const {
418 if (!device_policy_.has_auto_update_settings())
419 return false;
420
421 const enterprise_management::AutoUpdateSettingsProto& proto =
422 device_policy_.auto_update_settings();
423
424 *http_downloads_enabled = proto.http_downloads_enabled();
425 return true;
426}
427
David Zeuthen4d65f722013-09-09 17:19:48 -0700428bool DevicePolicyImpl::GetAuP2PEnabled(bool* au_p2p_enabled) const {
429 if (!device_policy_.has_auto_update_settings())
430 return false;
431
432 const enterprise_management::AutoUpdateSettingsProto& proto =
433 device_policy_.auto_update_settings();
434
435 *au_p2p_enabled = proto.p2p_enabled();
436 return true;
437}
438
Julian Pastarmov2638fbc2011-07-20 14:30:46 +0200439bool DevicePolicyImpl::VerifyPolicyFiles() {
440 // Both the policy and its signature have to exist.
Ben Chanc7b97b82014-04-24 00:04:42 -0700441 if (!base::PathExists(policy_path_) || !base::PathExists(keyfile_path_)) {
Julian Pastarmov2638fbc2011-07-20 14:30:46 +0200442 return false;
443 }
444
445 // Check if the policy and signature file is owned by root.
446 struct stat file_stat;
447 stat(policy_path_.value().c_str(), &file_stat);
448 if (file_stat.st_uid != 0) {
449 LOG(ERROR) << "Policy file is not owned by root!";
450 return false;
451 }
452 stat(keyfile_path_.value().c_str(), &file_stat);
453 if (file_stat.st_uid != 0) {
454 LOG(ERROR) << "Policy signature file is not owned by root!";
455 return false;
456 }
457 return true;
458}
459
460bool DevicePolicyImpl::VerifyPolicySignature() {
461 if (policy_.has_policy_data_signature()) {
462 std::string policy_data = policy_.policy_data();
463 std::string policy_data_signature = policy_.policy_data_signature();
464 std::string public_key;
Ben Chanc7b97b82014-04-24 00:04:42 -0700465 if (!ReadPublicKeyFromFile(base::FilePath(keyfile_path_), &public_key)) {
Julian Pastarmov2638fbc2011-07-20 14:30:46 +0200466 LOG(ERROR) << "Could not read owner key off disk";
467 return false;
468 }
469 if (!VerifySignature(policy_data, policy_data_signature, public_key)) {
470 LOG(ERROR) << "Signature does not match the data or can not be verified!";
471 return false;
472 }
473 return true;
474 }
475 LOG(ERROR) << "The policy blob is not signed!";
476 return false;
477}
478
479} // namespace policy