blob: de29b394677d14459e535804819888c3ac50a977 [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
Alex Deymo3c8d4ab2015-09-02 17:15:00 -07005#include "policy/device_policy_impl.h"
Julian Pastarmov2638fbc2011-07-20 14:30:46 +02006
Ben Chancb104642014-09-05 08:21:06 -07007#include <base/files/file_util.h>
Chris Masonef8476592013-02-12 10:18:30 -08008#include <base/logging.h>
Alex Vakulenkof2418e52014-09-04 08:26:42 -07009#include <base/macros.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|.
Alex Vakulenkoef47ede2014-07-31 11:44:27 -070040bool VerifySignature(const std::string& signed_data,
41 const std::string& signature,
42 const std::string& public_key) {
Julian Pastarmov2638fbc2011-07-20 14:30:46 +020043 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
Alex Vakulenko9d99bc62014-08-28 13:42:01 -070055 EVP_PKEY* public_key_ssl = d2i_PUBKEY_bio(bio, nullptr);
Julian Pastarmov2638fbc2011-07-20 14:30:46 +020056 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());
Alex Vakulenko9d99bc62014-08-28 13:42:01 -070064 int rv = EVP_VerifyInit_ex(&ctx, digest, nullptr);
Julian Pastarmov2638fbc2011-07-20 14:30:46 +020065 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
Patrick Dubroy9d979cb2011-08-01 15:52:35 +0200249bool DevicePolicyImpl::GetReleaseChannel(
250 std::string* release_channel) const {
251 if (!device_policy_.has_release_channel())
252 return false;
253
254 const enterprise_management::ReleaseChannelProto& proto =
255 device_policy_.release_channel();
256 if (!proto.has_release_channel())
257 return false;
258
259 *release_channel = proto.release_channel();
260 return true;
261}
262
Julian Pastarmovb1684852012-07-06 15:57:29 +0200263bool DevicePolicyImpl::GetReleaseChannelDelegated(
264 bool* release_channel_delegated) const {
265 if (!device_policy_.has_release_channel())
266 return false;
267
268 const enterprise_management::ReleaseChannelProto& proto =
269 device_policy_.release_channel();
270 if (!proto.has_release_channel_delegated())
271 return false;
272
273 *release_channel_delegated = proto.release_channel_delegated();
274 return true;
275}
276
Jay Srinivasancab5d3e2012-03-19 11:44:48 -0700277bool DevicePolicyImpl::GetUpdateDisabled(
278 bool* update_disabled) const {
279 if (!device_policy_.has_auto_update_settings())
280 return false;
281
282 const enterprise_management::AutoUpdateSettingsProto& proto =
283 device_policy_.auto_update_settings();
284 if (!proto.has_update_disabled())
285 return false;
286
287 *update_disabled = proto.update_disabled();
288 return true;
289}
290
Jay Srinivasancab5d3e2012-03-19 11:44:48 -0700291bool DevicePolicyImpl::GetTargetVersionPrefix(
292 std::string* target_version_prefix) const {
293 if (!device_policy_.has_auto_update_settings())
294 return false;
295
296 const enterprise_management::AutoUpdateSettingsProto& proto =
297 device_policy_.auto_update_settings();
298 if (!proto.has_target_version_prefix())
299 return false;
300
301 *target_version_prefix = proto.target_version_prefix();
302 return true;
303}
304
Jay Srinivasand7c86ae2012-05-31 19:24:56 -0700305bool DevicePolicyImpl::GetScatterFactorInSeconds(
Ben Chan43473aa2014-08-07 01:02:47 -0700306 int64_t* scatter_factor_in_seconds) const {
Jay Srinivasand7c86ae2012-05-31 19:24:56 -0700307 if (!device_policy_.has_auto_update_settings())
308 return false;
309
310 const enterprise_management::AutoUpdateSettingsProto& proto =
311 device_policy_.auto_update_settings();
312 if (!proto.has_scatter_factor_in_seconds())
313 return false;
314
315 *scatter_factor_in_seconds = proto.scatter_factor_in_seconds();
316 return true;
317}
318
Mattias Nisslerc13bcb32012-05-23 20:46:33 +0200319bool DevicePolicyImpl::GetAllowedConnectionTypesForUpdate(
320 std::set<std::string>* connection_types) const {
321 if (!device_policy_.has_auto_update_settings())
322 return false;
323
324 const enterprise_management::AutoUpdateSettingsProto& proto =
325 device_policy_.auto_update_settings();
326 if (proto.allowed_connection_types_size() <= 0)
327 return false;
328
329 for (int i = 0; i < proto.allowed_connection_types_size(); i++) {
330 std::string type = DecodeConnectionType(proto.allowed_connection_types(i));
331 if (!type.empty())
332 connection_types->insert(type);
333 }
334 return true;
335}
336
Bartosz Fabianowskia6732cd2012-02-07 11:54:21 +0100337bool DevicePolicyImpl::GetOpenNetworkConfiguration(
338 std::string* open_network_configuration) const {
339 if (!device_policy_.has_open_network_configuration())
340 return false;
341
342 const enterprise_management::DeviceOpenNetworkConfigurationProto& proto =
343 device_policy_.open_network_configuration();
344 if (!proto.has_open_network_configuration())
345 return false;
346
347 *open_network_configuration = proto.open_network_configuration();
348 return true;
349}
350
Bartosz Fabianowskic4ee8bf2012-01-25 16:31:26 +0100351bool DevicePolicyImpl::GetOwner(std::string* owner) const {
352 // The device is enterprise enrolled iff a request token exists.
353 if (policy_data_.has_request_token()) {
354 *owner = "";
355 return true;
356 }
357 if (!policy_data_.has_username())
358 return false;
359 *owner = policy_data_.username();
360 return true;
361}
362
Jay Srinivasan40563572013-05-14 18:11:14 -0700363bool DevicePolicyImpl::GetHttpDownloadsEnabled(
364 bool* http_downloads_enabled) const {
365 if (!device_policy_.has_auto_update_settings())
366 return false;
367
368 const enterprise_management::AutoUpdateSettingsProto& proto =
369 device_policy_.auto_update_settings();
370
Alex Deymoa7e76a42014-08-27 17:58:34 -0700371 if (!proto.has_http_downloads_enabled())
372 return false;
373
Jay Srinivasan40563572013-05-14 18:11:14 -0700374 *http_downloads_enabled = proto.http_downloads_enabled();
375 return true;
376}
377
David Zeuthen4d65f722013-09-09 17:19:48 -0700378bool DevicePolicyImpl::GetAuP2PEnabled(bool* au_p2p_enabled) const {
379 if (!device_policy_.has_auto_update_settings())
380 return false;
381
382 const enterprise_management::AutoUpdateSettingsProto& proto =
383 device_policy_.auto_update_settings();
384
David Zeuthen72765fa2014-09-22 16:39:45 -0400385 if (!proto.has_p2p_enabled())
386 return false;
387
David Zeuthen4d65f722013-09-09 17:19:48 -0700388 *au_p2p_enabled = proto.p2p_enabled();
389 return true;
390}
391
Xiyuan Xiab2f006f2016-02-04 08:55:21 -0800392bool DevicePolicyImpl::GetAllowKioskAppControlChromeVersion(
393 bool* allow_kiosk_app_control_chrome_version) const {
394 if (!device_policy_.has_allow_kiosk_app_control_chrome_version())
395 return false;
396
397 const enterprise_management::AllowKioskAppControlChromeVersionProto& proto =
398 device_policy_.allow_kiosk_app_control_chrome_version();
399
400 if (!proto.has_allow_kiosk_app_control_chrome_version())
401 return false;
402
403 *allow_kiosk_app_control_chrome_version =
404 proto.allow_kiosk_app_control_chrome_version();
405 return true;
406}
407
Vincent Palatin31dae172016-02-18 09:46:55 -0800408bool DevicePolicyImpl::GetUsbDetachableWhitelist(
409 std::vector<UsbDeviceId>* usb_whitelist) const {
410 if (!device_policy_.has_usb_detachable_whitelist())
411 return false;
412 const enterprise_management::UsbDetachableWhitelistProto& proto =
413 device_policy_.usb_detachable_whitelist();
414 usb_whitelist->clear();
415 for (int i = 0; i < proto.id_size(); i++) {
416 const ::enterprise_management::UsbDeviceIdProto& id = proto.id(i);
417 UsbDeviceId dev_id;
418 dev_id.vendor_id = id.has_vendor_id() ? id.vendor_id() : 0;
419 dev_id.product_id = id.has_product_id() ? id.product_id() : 0;
420 usb_whitelist->push_back(dev_id);
421 }
422 return true;
423}
424
Rahul Chaturvedib91af3b2017-04-21 18:21:23 -0400425bool DevicePolicyImpl::GetAutoLaunchedKioskAppId(
426 std::string* app_id_out) const {
427 if (!device_policy_.has_device_local_accounts())
428 return false;
429
430 const enterprise_management::DeviceLocalAccountsProto& local_accounts =
431 device_policy_.device_local_accounts();
432
433 // For auto-launched kiosk apps, the delay needs to be 0.
434 if (local_accounts.has_auto_login_delay() &&
435 local_accounts.auto_login_delay() != 0)
436 return false;
437
438 for (const enterprise_management::DeviceLocalAccountInfoProto& account :
439 local_accounts.account()) {
440 // If this isn't an auto-login account, move to the next one.
441 if (account.account_id() != local_accounts.auto_login_id())
442 continue;
443
444 // If the auto-launched account is not a kiosk app, bail out, we aren't
445 // running in auto-launched kiosk mode.
446 if (account.type() !=
447 enterprise_management::DeviceLocalAccountInfoProto::
448 ACCOUNT_TYPE_KIOSK_APP) {
449 return false;
450 }
451
452 *app_id_out = account.kiosk_app().app_id();
453 return true;
454 }
455
456 // No auto-launched account found.
457 return false;
458}
459
Julian Pastarmov2638fbc2011-07-20 14:30:46 +0200460bool DevicePolicyImpl::VerifyPolicyFiles() {
461 // Both the policy and its signature have to exist.
Ben Chanc7b97b82014-04-24 00:04:42 -0700462 if (!base::PathExists(policy_path_) || !base::PathExists(keyfile_path_)) {
Julian Pastarmov2638fbc2011-07-20 14:30:46 +0200463 return false;
464 }
465
466 // Check if the policy and signature file is owned by root.
467 struct stat file_stat;
468 stat(policy_path_.value().c_str(), &file_stat);
469 if (file_stat.st_uid != 0) {
470 LOG(ERROR) << "Policy file is not owned by root!";
471 return false;
472 }
473 stat(keyfile_path_.value().c_str(), &file_stat);
474 if (file_stat.st_uid != 0) {
475 LOG(ERROR) << "Policy signature file is not owned by root!";
476 return false;
477 }
478 return true;
479}
480
481bool DevicePolicyImpl::VerifyPolicySignature() {
482 if (policy_.has_policy_data_signature()) {
483 std::string policy_data = policy_.policy_data();
484 std::string policy_data_signature = policy_.policy_data_signature();
485 std::string public_key;
Ben Chanc7b97b82014-04-24 00:04:42 -0700486 if (!ReadPublicKeyFromFile(base::FilePath(keyfile_path_), &public_key)) {
Julian Pastarmov2638fbc2011-07-20 14:30:46 +0200487 LOG(ERROR) << "Could not read owner key off disk";
488 return false;
489 }
490 if (!VerifySignature(policy_data, policy_data_signature, public_key)) {
491 LOG(ERROR) << "Signature does not match the data or can not be verified!";
492 return false;
493 }
494 return true;
495 }
496 LOG(ERROR) << "The policy blob is not signed!";
497 return false;
498}
499
500} // namespace policy