| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/ownership/owner_settings_service.h" | |
| 6 | |
| 7 #include "base/basictypes.h" | |
| 8 #include "base/bind.h" | |
| 9 #include "base/callback.h" | |
| 10 #include "base/location.h" | |
| 11 #include "base/logging.h" | |
| 12 #include "base/message_loop/message_loop.h" | |
| 13 #include "base/task_runner.h" | |
| 14 #include "base/task_runner_util.h" | |
| 15 #include "components/ownership/owner_key_util.h" | |
| 16 #include "crypto/signature_creator.h" | |
| 17 | |
| 18 namespace em = enterprise_management; | |
| 19 | |
| 20 namespace ownership { | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 std::string AssembleAndSignPolicy(scoped_ptr<em::PolicyData> policy, | |
| 25 crypto::RSAPrivateKey* private_key) { | |
| 26 // Assemble the policy. | |
| 27 em::PolicyFetchResponse policy_response; | |
| 28 if (!policy->SerializeToString(policy_response.mutable_policy_data())) { | |
| 29 LOG(ERROR) << "Failed to encode policy payload."; | |
| 30 return std::string(); | |
| 31 } | |
| 32 | |
| 33 // Generate the signature. | |
| 34 scoped_ptr<crypto::SignatureCreator> signature_creator( | |
| 35 crypto::SignatureCreator::Create(private_key)); | |
| 36 signature_creator->Update( | |
| 37 reinterpret_cast<const uint8*>(policy_response.policy_data().c_str()), | |
| 38 policy_response.policy_data().size()); | |
| 39 std::vector<uint8> signature_bytes; | |
| 40 std::string policy_blob; | |
| 41 if (!signature_creator->Final(&signature_bytes)) { | |
| 42 LOG(ERROR) << "Failed to create policy signature."; | |
| 43 return std::string(); | |
| 44 } | |
| 45 | |
| 46 policy_response.mutable_policy_data_signature()->assign( | |
| 47 reinterpret_cast<const char*>(vector_as_array(&signature_bytes)), | |
| 48 signature_bytes.size()); | |
| 49 return policy_response.SerializeAsString(); | |
| 50 } | |
| 51 | |
| 52 } // namepace | |
| 53 | |
| 54 OwnerSettingsService::OwnerSettingsService( | |
| 55 const scoped_refptr<ownership::OwnerKeyUtil>& owner_key_util) | |
| 56 : owner_key_util_(owner_key_util), weak_factory_(this) { | |
| 57 } | |
| 58 | |
| 59 OwnerSettingsService::~OwnerSettingsService() { | |
| 60 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 61 } | |
| 62 | |
| 63 bool OwnerSettingsService::IsOwner() { | |
| 64 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 65 return private_key_.get() && private_key_->key(); | |
| 66 } | |
| 67 | |
| 68 void OwnerSettingsService::IsOwnerAsync(const IsOwnerCallback& callback) { | |
| 69 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 70 if (private_key_.get()) { | |
| 71 base::MessageLoop::current()->PostTask(FROM_HERE, | |
| 72 base::Bind(callback, IsOwner())); | |
| 73 } else { | |
| 74 pending_is_owner_callbacks_.push_back(callback); | |
| 75 } | |
| 76 } | |
| 77 | |
| 78 bool OwnerSettingsService::AssembleAndSignPolicyAsync( | |
| 79 base::TaskRunner* task_runner, | |
| 80 scoped_ptr<em::PolicyData> policy, | |
| 81 const AssembleAndSignPolicyAsyncCallback& callback) { | |
| 82 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 83 if (!task_runner || !IsOwner()) | |
| 84 return false; | |
| 85 return base::PostTaskAndReplyWithResult( | |
| 86 task_runner, | |
| 87 FROM_HERE, | |
| 88 base::Bind( | |
| 89 &AssembleAndSignPolicy, base::Passed(&policy), private_key_->key()), | |
| 90 callback); | |
| 91 } | |
| 92 | |
| 93 void OwnerSettingsService::ReloadKeypair() { | |
| 94 ReloadKeypairImpl( | |
| 95 base::Bind(&OwnerSettingsService::OnKeypairLoaded, as_weak_ptr())); | |
| 96 } | |
| 97 | |
| 98 void OwnerSettingsService::OnKeypairLoaded( | |
| 99 const scoped_refptr<PublicKey>& public_key, | |
| 100 const scoped_refptr<PrivateKey>& private_key) { | |
| 101 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 102 | |
| 103 public_key_ = public_key; | |
| 104 private_key_ = private_key; | |
| 105 | |
| 106 const bool is_owner = IsOwner(); | |
| 107 std::vector<IsOwnerCallback> is_owner_callbacks; | |
| 108 is_owner_callbacks.swap(pending_is_owner_callbacks_); | |
| 109 for (std::vector<IsOwnerCallback>::iterator it(is_owner_callbacks.begin()); | |
| 110 it != is_owner_callbacks.end(); | |
| 111 ++it) { | |
| 112 it->Run(is_owner); | |
| 113 } | |
| 114 | |
| 115 OnPostKeypairLoadedActions(); | |
| 116 } | |
| 117 | |
| 118 } // namespace ownership | |
| OLD | NEW |