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 | |
Ben Chan | c7b97b8 | 2014-04-24 00:04:42 -0700 | [diff] [blame] | 5 | #include "chromeos/policy/device_policy_impl.h" |
Julian Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 6 | |
Chris Masone | f847659 | 2013-02-12 10:18:30 -0800 | [diff] [blame] | 7 | #include <base/basictypes.h> |
Chris Masone | f847659 | 2013-02-12 10:18:30 -0800 | [diff] [blame] | 8 | #include <base/file_util.h> |
| 9 | #include <base/logging.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|. |
| 40 | bool 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 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 | |
Julian Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 249 | bool 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 Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 262 | bool 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 Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 275 | bool 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 Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 288 | bool 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 Dubroy | 9d979cb | 2011-08-01 15:52:35 +0200 | [diff] [blame] | 302 | bool 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 Pastarmov | b168485 | 2012-07-06 15:57:29 +0200 | [diff] [blame] | 316 | bool 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 Srinivasan | cab5d3e | 2012-03-19 11:44:48 -0700 | [diff] [blame] | 330 | bool 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 Srinivasan | cab5d3e | 2012-03-19 11:44:48 -0700 | [diff] [blame] | 344 | bool 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 Srinivasan | d7c86ae | 2012-05-31 19:24:56 -0700 | [diff] [blame] | 358 | bool 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 Nissler | c13bcb3 | 2012-05-23 20:46:33 +0200 | [diff] [blame] | 372 | bool 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 Fabianowski | a6732cd | 2012-02-07 11:54:21 +0100 | [diff] [blame] | 390 | bool 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 Fabianowski | c4ee8bf | 2012-01-25 16:31:26 +0100 | [diff] [blame] | 404 | bool 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 Srinivasan | 4056357 | 2013-05-14 18:11:14 -0700 | [diff] [blame] | 416 | bool 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 Zeuthen | 4d65f72 | 2013-09-09 17:19:48 -0700 | [diff] [blame] | 428 | bool 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 Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 439 | bool DevicePolicyImpl::VerifyPolicyFiles() { |
| 440 | // Both the policy and its signature have to exist. |
Ben Chan | c7b97b8 | 2014-04-24 00:04:42 -0700 | [diff] [blame] | 441 | if (!base::PathExists(policy_path_) || !base::PathExists(keyfile_path_)) { |
Julian Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 442 | 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 | |
| 460 | bool 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 Chan | c7b97b8 | 2014-04-24 00:04:42 -0700 | [diff] [blame] | 465 | if (!ReadPublicKeyFromFile(base::FilePath(keyfile_path_), &public_key)) { |
Julian Pastarmov | 2638fbc | 2011-07-20 14:30:46 +0200 | [diff] [blame] | 466 | 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 |