| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/ownership/owner_settings_service.h" | 5 #include "components/ownership/owner_settings_service.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 13 #include "base/task_runner.h" | 13 #include "base/task_runner.h" |
| 14 #include "base/task_runner_util.h" | 14 #include "base/task_runner_util.h" |
| 15 #include "base/values.h" |
| 15 #include "components/ownership/owner_key_util.h" | 16 #include "components/ownership/owner_key_util.h" |
| 16 #include "crypto/signature_creator.h" | 17 #include "crypto/signature_creator.h" |
| 17 | 18 |
| 18 namespace em = enterprise_management; | 19 namespace em = enterprise_management; |
| 19 | 20 |
| 20 namespace ownership { | 21 namespace ownership { |
| 21 | 22 |
| 22 namespace { | 23 namespace { |
| 23 | 24 |
| 24 std::string AssembleAndSignPolicy(scoped_ptr<em::PolicyData> policy, | 25 scoped_ptr<em::PolicyFetchResponse> AssembleAndSignPolicy( |
| 25 crypto::RSAPrivateKey* private_key) { | 26 scoped_ptr<em::PolicyData> policy, |
| 27 crypto::RSAPrivateKey* private_key) { |
| 26 // Assemble the policy. | 28 // Assemble the policy. |
| 27 em::PolicyFetchResponse policy_response; | 29 scoped_ptr<em::PolicyFetchResponse> policy_response( |
| 28 if (!policy->SerializeToString(policy_response.mutable_policy_data())) { | 30 new em::PolicyFetchResponse()); |
| 31 if (!policy->SerializeToString(policy_response->mutable_policy_data())) { |
| 29 LOG(ERROR) << "Failed to encode policy payload."; | 32 LOG(ERROR) << "Failed to encode policy payload."; |
| 30 return std::string(); | 33 return scoped_ptr<em::PolicyFetchResponse>(nullptr).Pass(); |
| 31 } | 34 } |
| 32 | 35 |
| 33 // Generate the signature. | 36 // Generate the signature. |
| 34 scoped_ptr<crypto::SignatureCreator> signature_creator( | 37 scoped_ptr<crypto::SignatureCreator> signature_creator( |
| 35 crypto::SignatureCreator::Create(private_key, | 38 crypto::SignatureCreator::Create(private_key, |
| 36 crypto::SignatureCreator::SHA1)); | 39 crypto::SignatureCreator::SHA1)); |
| 37 signature_creator->Update( | 40 signature_creator->Update( |
| 38 reinterpret_cast<const uint8*>(policy_response.policy_data().c_str()), | 41 reinterpret_cast<const uint8*>(policy_response->policy_data().c_str()), |
| 39 policy_response.policy_data().size()); | 42 policy_response->policy_data().size()); |
| 40 std::vector<uint8> signature_bytes; | 43 std::vector<uint8> signature_bytes; |
| 41 std::string policy_blob; | 44 std::string policy_blob; |
| 42 if (!signature_creator->Final(&signature_bytes)) { | 45 if (!signature_creator->Final(&signature_bytes)) { |
| 43 LOG(ERROR) << "Failed to create policy signature."; | 46 LOG(ERROR) << "Failed to create policy signature."; |
| 44 return std::string(); | 47 return scoped_ptr<em::PolicyFetchResponse>(nullptr).Pass(); |
| 45 } | 48 } |
| 46 | 49 |
| 47 policy_response.mutable_policy_data_signature()->assign( | 50 policy_response->mutable_policy_data_signature()->assign( |
| 48 reinterpret_cast<const char*>(vector_as_array(&signature_bytes)), | 51 reinterpret_cast<const char*>(vector_as_array(&signature_bytes)), |
| 49 signature_bytes.size()); | 52 signature_bytes.size()); |
| 50 return policy_response.SerializeAsString(); | 53 return policy_response.Pass(); |
| 51 } | 54 } |
| 52 | 55 |
| 53 } // namepace | 56 } // namepace |
| 54 | 57 |
| 55 OwnerSettingsService::OwnerSettingsService( | 58 OwnerSettingsService::OwnerSettingsService( |
| 56 const scoped_refptr<ownership::OwnerKeyUtil>& owner_key_util) | 59 const scoped_refptr<ownership::OwnerKeyUtil>& owner_key_util) |
| 57 : owner_key_util_(owner_key_util), weak_factory_(this) { | 60 : owner_key_util_(owner_key_util), weak_factory_(this) { |
| 58 } | 61 } |
| 59 | 62 |
| 60 OwnerSettingsService::~OwnerSettingsService() { | 63 OwnerSettingsService::~OwnerSettingsService() { |
| 61 DCHECK(thread_checker_.CalledOnValidThread()); | 64 DCHECK(thread_checker_.CalledOnValidThread()); |
| 62 } | 65 } |
| 63 | 66 |
| 67 void OwnerSettingsService::AddObserver(Observer* observer) { |
| 68 if (observer && !observers_.HasObserver(observer)) |
| 69 observers_.AddObserver(observer); |
| 70 } |
| 71 |
| 72 void OwnerSettingsService::RemoveObserver(Observer* observer) { |
| 73 observers_.RemoveObserver(observer); |
| 74 } |
| 75 |
| 64 bool OwnerSettingsService::IsOwner() { | 76 bool OwnerSettingsService::IsOwner() { |
| 65 DCHECK(thread_checker_.CalledOnValidThread()); | 77 DCHECK(thread_checker_.CalledOnValidThread()); |
| 66 return private_key_.get() && private_key_->key(); | 78 return private_key_.get() && private_key_->key(); |
| 67 } | 79 } |
| 68 | 80 |
| 69 void OwnerSettingsService::IsOwnerAsync(const IsOwnerCallback& callback) { | 81 void OwnerSettingsService::IsOwnerAsync(const IsOwnerCallback& callback) { |
| 70 DCHECK(thread_checker_.CalledOnValidThread()); | 82 DCHECK(thread_checker_.CalledOnValidThread()); |
| 71 if (private_key_.get()) { | 83 if (private_key_.get()) { |
| 72 base::MessageLoop::current()->PostTask(FROM_HERE, | 84 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 73 base::Bind(callback, IsOwner())); | 85 base::Bind(callback, IsOwner())); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 84 if (!task_runner || !IsOwner()) | 96 if (!task_runner || !IsOwner()) |
| 85 return false; | 97 return false; |
| 86 return base::PostTaskAndReplyWithResult( | 98 return base::PostTaskAndReplyWithResult( |
| 87 task_runner, | 99 task_runner, |
| 88 FROM_HERE, | 100 FROM_HERE, |
| 89 base::Bind( | 101 base::Bind( |
| 90 &AssembleAndSignPolicy, base::Passed(&policy), private_key_->key()), | 102 &AssembleAndSignPolicy, base::Passed(&policy), private_key_->key()), |
| 91 callback); | 103 callback); |
| 92 } | 104 } |
| 93 | 105 |
| 106 bool OwnerSettingsService::SetBoolean(const std::string& setting, bool value) { |
| 107 DCHECK(thread_checker_.CalledOnValidThread()); |
| 108 base::FundamentalValue in_value(value); |
| 109 return Set(setting, in_value); |
| 110 } |
| 111 |
| 112 bool OwnerSettingsService::SetInteger(const std::string& setting, int value) { |
| 113 DCHECK(thread_checker_.CalledOnValidThread()); |
| 114 base::FundamentalValue in_value(value); |
| 115 return Set(setting, in_value); |
| 116 } |
| 117 |
| 118 bool OwnerSettingsService::SetDouble(const std::string& setting, double value) { |
| 119 DCHECK(thread_checker_.CalledOnValidThread()); |
| 120 base::FundamentalValue in_value(value); |
| 121 return Set(setting, in_value); |
| 122 } |
| 123 |
| 124 bool OwnerSettingsService::SetString(const std::string& setting, |
| 125 const std::string& value) { |
| 126 DCHECK(thread_checker_.CalledOnValidThread()); |
| 127 base::StringValue in_value(value); |
| 128 return Set(setting, in_value); |
| 129 } |
| 130 |
| 94 void OwnerSettingsService::ReloadKeypair() { | 131 void OwnerSettingsService::ReloadKeypair() { |
| 95 ReloadKeypairImpl( | 132 ReloadKeypairImpl( |
| 96 base::Bind(&OwnerSettingsService::OnKeypairLoaded, as_weak_ptr())); | 133 base::Bind(&OwnerSettingsService::OnKeypairLoaded, as_weak_ptr())); |
| 97 } | 134 } |
| 98 | 135 |
| 99 void OwnerSettingsService::OnKeypairLoaded( | 136 void OwnerSettingsService::OnKeypairLoaded( |
| 100 const scoped_refptr<PublicKey>& public_key, | 137 const scoped_refptr<PublicKey>& public_key, |
| 101 const scoped_refptr<PrivateKey>& private_key) { | 138 const scoped_refptr<PrivateKey>& private_key) { |
| 102 DCHECK(thread_checker_.CalledOnValidThread()); | 139 DCHECK(thread_checker_.CalledOnValidThread()); |
| 103 | 140 |
| 104 public_key_ = public_key; | 141 public_key_ = public_key; |
| 105 private_key_ = private_key; | 142 private_key_ = private_key; |
| 106 | 143 |
| 107 const bool is_owner = IsOwner(); | 144 const bool is_owner = IsOwner(); |
| 108 std::vector<IsOwnerCallback> is_owner_callbacks; | 145 std::vector<IsOwnerCallback> is_owner_callbacks; |
| 109 is_owner_callbacks.swap(pending_is_owner_callbacks_); | 146 is_owner_callbacks.swap(pending_is_owner_callbacks_); |
| 110 for (std::vector<IsOwnerCallback>::iterator it(is_owner_callbacks.begin()); | 147 for (std::vector<IsOwnerCallback>::iterator it(is_owner_callbacks.begin()); |
| 111 it != is_owner_callbacks.end(); | 148 it != is_owner_callbacks.end(); |
| 112 ++it) { | 149 ++it) { |
| 113 it->Run(is_owner); | 150 it->Run(is_owner); |
| 114 } | 151 } |
| 115 | 152 |
| 116 OnPostKeypairLoadedActions(); | 153 OnPostKeypairLoadedActions(); |
| 117 } | 154 } |
| 118 | 155 |
| 119 } // namespace ownership | 156 } // namespace ownership |
| OLD | NEW |