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() { |
(...skipping 23 matching lines...) Expand all Loading... |
84 if (!task_runner || !IsOwner()) | 87 if (!task_runner || !IsOwner()) |
85 return false; | 88 return false; |
86 return base::PostTaskAndReplyWithResult( | 89 return base::PostTaskAndReplyWithResult( |
87 task_runner, | 90 task_runner, |
88 FROM_HERE, | 91 FROM_HERE, |
89 base::Bind( | 92 base::Bind( |
90 &AssembleAndSignPolicy, base::Passed(&policy), private_key_->key()), | 93 &AssembleAndSignPolicy, base::Passed(&policy), private_key_->key()), |
91 callback); | 94 callback); |
92 } | 95 } |
93 | 96 |
| 97 void OwnerSettingsService::SetBoolean(const std::string& setting, bool value) { |
| 98 DCHECK(thread_checker_.CalledOnValidThread()); |
| 99 base::FundamentalValue in_value(value); |
| 100 Set(setting, in_value); |
| 101 } |
| 102 |
| 103 void OwnerSettingsService::SetInteger(const std::string& setting, int value) { |
| 104 DCHECK(thread_checker_.CalledOnValidThread()); |
| 105 base::FundamentalValue in_value(value); |
| 106 Set(setting, in_value); |
| 107 } |
| 108 |
| 109 void OwnerSettingsService::SetDouble(const std::string& setting, double value) { |
| 110 DCHECK(thread_checker_.CalledOnValidThread()); |
| 111 base::FundamentalValue in_value(value); |
| 112 Set(setting, in_value); |
| 113 } |
| 114 |
| 115 void OwnerSettingsService::SetString(const std::string& setting, |
| 116 const std::string& value) { |
| 117 DCHECK(thread_checker_.CalledOnValidThread()); |
| 118 base::StringValue in_value(value); |
| 119 Set(setting, in_value); |
| 120 } |
| 121 |
94 void OwnerSettingsService::ReloadKeypair() { | 122 void OwnerSettingsService::ReloadKeypair() { |
95 ReloadKeypairImpl( | 123 ReloadKeypairImpl( |
96 base::Bind(&OwnerSettingsService::OnKeypairLoaded, as_weak_ptr())); | 124 base::Bind(&OwnerSettingsService::OnKeypairLoaded, as_weak_ptr())); |
97 } | 125 } |
98 | 126 |
99 void OwnerSettingsService::OnKeypairLoaded( | 127 void OwnerSettingsService::OnKeypairLoaded( |
100 const scoped_refptr<PublicKey>& public_key, | 128 const scoped_refptr<PublicKey>& public_key, |
101 const scoped_refptr<PrivateKey>& private_key) { | 129 const scoped_refptr<PrivateKey>& private_key) { |
102 DCHECK(thread_checker_.CalledOnValidThread()); | 130 DCHECK(thread_checker_.CalledOnValidThread()); |
103 | 131 |
104 public_key_ = public_key; | 132 public_key_ = public_key; |
105 private_key_ = private_key; | 133 private_key_ = private_key; |
106 | 134 |
107 const bool is_owner = IsOwner(); | 135 const bool is_owner = IsOwner(); |
108 std::vector<IsOwnerCallback> is_owner_callbacks; | 136 std::vector<IsOwnerCallback> is_owner_callbacks; |
109 is_owner_callbacks.swap(pending_is_owner_callbacks_); | 137 is_owner_callbacks.swap(pending_is_owner_callbacks_); |
110 for (std::vector<IsOwnerCallback>::iterator it(is_owner_callbacks.begin()); | 138 for (std::vector<IsOwnerCallback>::iterator it(is_owner_callbacks.begin()); |
111 it != is_owner_callbacks.end(); | 139 it != is_owner_callbacks.end(); |
112 ++it) { | 140 ++it) { |
113 it->Run(is_owner); | 141 it->Run(is_owner); |
114 } | 142 } |
115 | 143 |
116 OnPostKeypairLoadedActions(); | 144 OnPostKeypairLoadedActions(); |
117 } | 145 } |
118 | 146 |
119 } // namespace ownership | 147 } // namespace ownership |
OLD | NEW |