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" | |
16 #include "components/ownership/owner_key_util.h" | 15 #include "components/ownership/owner_key_util.h" |
17 #include "crypto/signature_creator.h" | 16 #include "crypto/signature_creator.h" |
18 | 17 |
19 namespace em = enterprise_management; | 18 namespace em = enterprise_management; |
20 | 19 |
21 namespace ownership { | 20 namespace ownership { |
22 | 21 |
23 namespace { | 22 namespace { |
24 | 23 |
25 scoped_ptr<em::PolicyFetchResponse> AssembleAndSignPolicy( | 24 std::string AssembleAndSignPolicy(scoped_ptr<em::PolicyData> policy, |
26 scoped_ptr<em::PolicyData> policy, | 25 crypto::RSAPrivateKey* private_key) { |
27 crypto::RSAPrivateKey* private_key) { | |
28 // Assemble the policy. | 26 // Assemble the policy. |
29 scoped_ptr<em::PolicyFetchResponse> policy_response( | 27 em::PolicyFetchResponse policy_response; |
30 new em::PolicyFetchResponse()); | 28 if (!policy->SerializeToString(policy_response.mutable_policy_data())) { |
31 if (!policy->SerializeToString(policy_response->mutable_policy_data())) { | |
32 LOG(ERROR) << "Failed to encode policy payload."; | 29 LOG(ERROR) << "Failed to encode policy payload."; |
33 return scoped_ptr<em::PolicyFetchResponse>(nullptr).Pass(); | 30 return std::string(); |
34 } | 31 } |
35 | 32 |
36 // Generate the signature. | 33 // Generate the signature. |
37 scoped_ptr<crypto::SignatureCreator> signature_creator( | 34 scoped_ptr<crypto::SignatureCreator> signature_creator( |
38 crypto::SignatureCreator::Create(private_key, | 35 crypto::SignatureCreator::Create(private_key, |
39 crypto::SignatureCreator::SHA1)); | 36 crypto::SignatureCreator::SHA1)); |
40 signature_creator->Update( | 37 signature_creator->Update( |
41 reinterpret_cast<const uint8*>(policy_response->policy_data().c_str()), | 38 reinterpret_cast<const uint8*>(policy_response.policy_data().c_str()), |
42 policy_response->policy_data().size()); | 39 policy_response.policy_data().size()); |
43 std::vector<uint8> signature_bytes; | 40 std::vector<uint8> signature_bytes; |
44 std::string policy_blob; | 41 std::string policy_blob; |
45 if (!signature_creator->Final(&signature_bytes)) { | 42 if (!signature_creator->Final(&signature_bytes)) { |
46 LOG(ERROR) << "Failed to create policy signature."; | 43 LOG(ERROR) << "Failed to create policy signature."; |
47 return scoped_ptr<em::PolicyFetchResponse>(nullptr).Pass(); | 44 return std::string(); |
48 } | 45 } |
49 | 46 |
50 policy_response->mutable_policy_data_signature()->assign( | 47 policy_response.mutable_policy_data_signature()->assign( |
51 reinterpret_cast<const char*>(vector_as_array(&signature_bytes)), | 48 reinterpret_cast<const char*>(vector_as_array(&signature_bytes)), |
52 signature_bytes.size()); | 49 signature_bytes.size()); |
53 return policy_response.Pass(); | 50 return policy_response.SerializeAsString(); |
54 } | 51 } |
55 | 52 |
56 } // namepace | 53 } // namepace |
57 | 54 |
58 OwnerSettingsService::OwnerSettingsService( | 55 OwnerSettingsService::OwnerSettingsService( |
59 const scoped_refptr<ownership::OwnerKeyUtil>& owner_key_util) | 56 const scoped_refptr<ownership::OwnerKeyUtil>& owner_key_util) |
60 : owner_key_util_(owner_key_util), weak_factory_(this) { | 57 : owner_key_util_(owner_key_util), weak_factory_(this) { |
61 } | 58 } |
62 | 59 |
63 OwnerSettingsService::~OwnerSettingsService() { | 60 OwnerSettingsService::~OwnerSettingsService() { |
64 DCHECK(thread_checker_.CalledOnValidThread()); | 61 DCHECK(thread_checker_.CalledOnValidThread()); |
65 } | 62 } |
66 | 63 |
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 | |
76 bool OwnerSettingsService::IsOwner() { | 64 bool OwnerSettingsService::IsOwner() { |
77 DCHECK(thread_checker_.CalledOnValidThread()); | 65 DCHECK(thread_checker_.CalledOnValidThread()); |
78 return private_key_.get() && private_key_->key(); | 66 return private_key_.get() && private_key_->key(); |
79 } | 67 } |
80 | 68 |
81 void OwnerSettingsService::IsOwnerAsync(const IsOwnerCallback& callback) { | 69 void OwnerSettingsService::IsOwnerAsync(const IsOwnerCallback& callback) { |
82 DCHECK(thread_checker_.CalledOnValidThread()); | 70 DCHECK(thread_checker_.CalledOnValidThread()); |
83 if (private_key_.get()) { | 71 if (private_key_.get()) { |
84 base::MessageLoop::current()->PostTask(FROM_HERE, | 72 base::MessageLoop::current()->PostTask(FROM_HERE, |
85 base::Bind(callback, IsOwner())); | 73 base::Bind(callback, IsOwner())); |
(...skipping 10 matching lines...) Expand all Loading... |
96 if (!task_runner || !IsOwner()) | 84 if (!task_runner || !IsOwner()) |
97 return false; | 85 return false; |
98 return base::PostTaskAndReplyWithResult( | 86 return base::PostTaskAndReplyWithResult( |
99 task_runner, | 87 task_runner, |
100 FROM_HERE, | 88 FROM_HERE, |
101 base::Bind( | 89 base::Bind( |
102 &AssembleAndSignPolicy, base::Passed(&policy), private_key_->key()), | 90 &AssembleAndSignPolicy, base::Passed(&policy), private_key_->key()), |
103 callback); | 91 callback); |
104 } | 92 } |
105 | 93 |
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 | |
131 void OwnerSettingsService::ReloadKeypair() { | 94 void OwnerSettingsService::ReloadKeypair() { |
132 ReloadKeypairImpl( | 95 ReloadKeypairImpl( |
133 base::Bind(&OwnerSettingsService::OnKeypairLoaded, as_weak_ptr())); | 96 base::Bind(&OwnerSettingsService::OnKeypairLoaded, as_weak_ptr())); |
134 } | 97 } |
135 | 98 |
136 void OwnerSettingsService::OnKeypairLoaded( | 99 void OwnerSettingsService::OnKeypairLoaded( |
137 const scoped_refptr<PublicKey>& public_key, | 100 const scoped_refptr<PublicKey>& public_key, |
138 const scoped_refptr<PrivateKey>& private_key) { | 101 const scoped_refptr<PrivateKey>& private_key) { |
139 DCHECK(thread_checker_.CalledOnValidThread()); | 102 DCHECK(thread_checker_.CalledOnValidThread()); |
140 | 103 |
141 public_key_ = public_key; | 104 public_key_ = public_key; |
142 private_key_ = private_key; | 105 private_key_ = private_key; |
143 | 106 |
144 const bool is_owner = IsOwner(); | 107 const bool is_owner = IsOwner(); |
145 std::vector<IsOwnerCallback> is_owner_callbacks; | 108 std::vector<IsOwnerCallback> is_owner_callbacks; |
146 is_owner_callbacks.swap(pending_is_owner_callbacks_); | 109 is_owner_callbacks.swap(pending_is_owner_callbacks_); |
147 for (std::vector<IsOwnerCallback>::iterator it(is_owner_callbacks.begin()); | 110 for (std::vector<IsOwnerCallback>::iterator it(is_owner_callbacks.begin()); |
148 it != is_owner_callbacks.end(); | 111 it != is_owner_callbacks.end(); |
149 ++it) { | 112 ++it) { |
150 it->Run(is_owner); | 113 it->Run(is_owner); |
151 } | 114 } |
152 | 115 |
153 OnPostKeypairLoadedActions(); | 116 OnPostKeypairLoadedActions(); |
154 } | 117 } |
155 | 118 |
156 } // namespace ownership | 119 } // namespace ownership |
OLD | NEW |