Bartosz Fabianowski | c4ee8bf | 2012-01-25 16:31:26 +0100 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Julian Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Alex Deymo | 3c8d4ab | 2015-09-02 17:15:00 -0700 | [diff] [blame] | 5 | #include "policy/device_policy_impl.h" |
Julian Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 6 | |
Ben Chan | cb10464 | 2014-09-05 08:21:06 -0700 | [diff] [blame] | 7 | #include <base/files/file_util.h> |
Chris Masone | f847659 | 2013-02-12 10:18:30 -0800 | [diff] [blame] | 8 | #include <base/logging.h> |
Alex Vakulenko | f2418e5 | 2014-09-04 08:26:42 -0700 | [diff] [blame] | 9 | #include <base/macros.h> |
Julian Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 10 | #include <openssl/evp.h> |
| 11 | #include <openssl/x509.h> |
| 12 | |
Bertrand SIMONNET | b54b6dc | 2014-07-02 12:13:56 -0700 | [diff] [blame] | 13 | #include <set> |
Julian Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 14 | #include <string> |
| 15 | |
Julian Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 16 | #include "bindings/chrome_device_policy.pb.h" |
| 17 | #include "bindings/device_management_backend.pb.h" |
| 18 | |
| 19 | namespace policy { |
| 20 | |
| 21 | namespace { |
| 22 | const char kPolicyPath[] = "/var/lib/whitelist/policy"; |
| 23 | const 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 Chan | c7b97b8 | 2014-04-24 00:04:42 -0700 | [diff] [blame] | 27 | bool ReadPublicKeyFromFile(const base::FilePath& key_file, |
| 28 | std::string* public_key) { |
| 29 | if (!base::PathExists(key_file)) |
Julian Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 30 | return false; |
| 31 | public_key->clear(); |
Ben Chan | c7b97b8 | 2014-04-24 00:04:42 -0700 | [diff] [blame] | 32 | if (!base::ReadFileToString(key_file, public_key) || public_key->empty()) { |
Julian Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 33 | 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 Vakulenko | ef47ede | 2014-07-31 11:44:27 -0700 | [diff] [blame] | 40 | bool VerifySignature(const std::string& signed_data, |
| 41 | const std::string& signature, |
| 42 | const std::string& public_key) { |
Julian Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 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 | |
Alex Vakulenko | 9d99bc6 | 2014-08-28 13:42:01 -0700 | [diff] [blame] | 55 | EVP_PKEY* public_key_ssl = d2i_PUBKEY_bio(bio, nullptr); |
Julian Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 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()); |
Alex Vakulenko | 9d99bc6 | 2014-08-28 13:42:01 -0700 | [diff] [blame] | 64 | int rv = EVP_VerifyInit_ex(&ctx, digest, nullptr); |
Julian Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 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 Nissler | c13bcb3 | 2012-05-23 20:46:33 +0200 | [diff] [blame] | 79 | // Decodes the connection type enum from the device settings protobuf to string |
| 80 | // representations. The strings must match the connection manager definitions. |
| 81 | std::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 Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 96 | } // namespace |
| 97 | |
| 98 | DevicePolicyImpl::DevicePolicyImpl() |
| 99 | : policy_path_(kPolicyPath), |
| 100 | keyfile_path_(kPublicKeyPath) { |
| 101 | } |
| 102 | |
| 103 | DevicePolicyImpl::~DevicePolicyImpl() { |
| 104 | } |
| 105 | |
| 106 | bool DevicePolicyImpl::LoadPolicy() { |
| 107 | if (!VerifyPolicyFiles()) { |
Julian Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 108 | return false; |
| 109 | } |
| 110 | |
| 111 | std::string polstr; |
Ben Chan | c7b97b8 | 2014-04-24 00:04:42 -0700 | [diff] [blame] | 112 | if (!base::ReadFileToString(policy_path_, &polstr) || polstr.empty()) { |
Julian Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 113 | 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 Fabianowski | c4ee8bf | 2012-01-25 16:31:26 +0100 | [diff] [blame] | 120 | policy_data_.ParseFromString(policy_.policy_data()); |
| 121 | if (!policy_data_.has_policy_value()) { |
Julian Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 122 | 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 Fabianowski | c4ee8bf | 2012-01-25 16:31:26 +0100 | [diff] [blame] | 132 | device_policy_.ParseFromString(policy_data_.policy_value()); |
Julian Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 133 | return true; |
| 134 | } |
| 135 | |
Julian Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 136 | bool 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 Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 144 | bool 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 Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 156 | bool 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 Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 164 | bool 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 Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 171 | bool 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 Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 178 | bool 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 Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 186 | bool 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 Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 193 | bool 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 Fabianowski | a6732cd | 2012-02-07 11:54:21 +0100 | [diff] [blame] | 200 | bool 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 Fabianowski | a6732cd | 2012-02-07 11:54:21 +0100 | [diff] [blame] | 213 | bool 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 Fabianowski | a6732cd | 2012-02-07 11:54:21 +0100 | [diff] [blame] | 227 | bool 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 Fabianowski | 43bd0ad | 2012-02-15 17:06:01 -0800 | [diff] [blame] | 240 | bool DevicePolicyImpl::GetEphemeralUsersEnabled( |
| 241 | bool* ephemeral_users_enabled) const { |
| 242 | if (!device_policy_.has_ephemeral_users_enabled()) |
Bartosz Fabianowski | a6732cd | 2012-02-07 11:54:21 +0100 | [diff] [blame] | 243 | return false; |
Bartosz Fabianowski | 43bd0ad | 2012-02-15 17:06:01 -0800 | [diff] [blame] | 244 | *ephemeral_users_enabled = |
| 245 | device_policy_.ephemeral_users_enabled().ephemeral_users_enabled(); |
Bartosz Fabianowski | a6732cd | 2012-02-07 11:54:21 +0100 | [diff] [blame] | 246 | return true; |
| 247 | } |
| 248 | |
Patrick Dubroy | 9d979cb | 2011-08-01 15:52:35 +0200 | [diff] [blame] | 249 | bool 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 Pastarmov | b168485 | 2012-07-06 15:57:29 +0200 | [diff] [blame] | 263 | bool 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 Srinivasan | cab5d3e | 2012-03-19 11:44:48 -0700 | [diff] [blame] | 277 | bool 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 Srinivasan | cab5d3e | 2012-03-19 11:44:48 -0700 | [diff] [blame] | 291 | bool 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 Srinivasan | d7c86ae | 2012-05-31 19:24:56 -0700 | [diff] [blame] | 305 | bool DevicePolicyImpl::GetScatterFactorInSeconds( |
Ben Chan | 43473aa | 2014-08-07 01:02:47 -0700 | [diff] [blame] | 306 | int64_t* scatter_factor_in_seconds) const { |
Jay Srinivasan | d7c86ae | 2012-05-31 19:24:56 -0700 | [diff] [blame] | 307 | 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 Nissler | c13bcb3 | 2012-05-23 20:46:33 +0200 | [diff] [blame] | 319 | bool 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 Fabianowski | a6732cd | 2012-02-07 11:54:21 +0100 | [diff] [blame] | 337 | bool 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 Fabianowski | c4ee8bf | 2012-01-25 16:31:26 +0100 | [diff] [blame] | 351 | bool 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 Srinivasan | 4056357 | 2013-05-14 18:11:14 -0700 | [diff] [blame] | 363 | bool 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 Deymo | a7e76a4 | 2014-08-27 17:58:34 -0700 | [diff] [blame] | 371 | if (!proto.has_http_downloads_enabled()) |
| 372 | return false; |
| 373 | |
Jay Srinivasan | 4056357 | 2013-05-14 18:11:14 -0700 | [diff] [blame] | 374 | *http_downloads_enabled = proto.http_downloads_enabled(); |
| 375 | return true; |
| 376 | } |
| 377 | |
David Zeuthen | 4d65f72 | 2013-09-09 17:19:48 -0700 | [diff] [blame] | 378 | bool 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 Zeuthen | 72765fa | 2014-09-22 16:39:45 -0400 | [diff] [blame] | 385 | if (!proto.has_p2p_enabled()) |
| 386 | return false; |
| 387 | |
David Zeuthen | 4d65f72 | 2013-09-09 17:19:48 -0700 | [diff] [blame] | 388 | *au_p2p_enabled = proto.p2p_enabled(); |
| 389 | return true; |
| 390 | } |
| 391 | |
Xiyuan Xia | b2f006f | 2016-02-04 08:55:21 -0800 | [diff] [blame] | 392 | bool 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 Palatin | 31dae17 | 2016-02-18 09:46:55 -0800 | [diff] [blame] | 408 | bool 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 Chaturvedi | b91af3b | 2017-04-21 18:21:23 -0400 | [diff] [blame^] | 425 | bool 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 Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 460 | bool DevicePolicyImpl::VerifyPolicyFiles() { |
| 461 | // Both the policy and its signature have to exist. |
Ben Chan | c7b97b8 | 2014-04-24 00:04:42 -0700 | [diff] [blame] | 462 | if (!base::PathExists(policy_path_) || !base::PathExists(keyfile_path_)) { |
Julian Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 463 | 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 | |
| 481 | bool 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 Chan | c7b97b8 | 2014-04-24 00:04:42 -0700 | [diff] [blame] | 486 | if (!ReadPublicKeyFromFile(base::FilePath(keyfile_path_), &public_key)) { |
Julian Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 487 | 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 |