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.
5 #include "components/ownership/owner_settings_service.h"
7 #include "base/basictypes.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 "base/values.h"
16 #include "components/ownership/owner_key_util.h"
17 #include "crypto/signature_creator.h"
19 namespace em
= enterprise_management
;
25 scoped_ptr
<em::PolicyFetchResponse
> AssembleAndSignPolicy(
26 scoped_ptr
<em::PolicyData
> policy
,
27 crypto::RSAPrivateKey
* private_key
) {
28 // Assemble the policy.
29 scoped_ptr
<em::PolicyFetchResponse
> policy_response(
30 new em::PolicyFetchResponse());
31 if (!policy
->SerializeToString(policy_response
->mutable_policy_data())) {
32 LOG(ERROR
) << "Failed to encode policy payload.";
33 return scoped_ptr
<em::PolicyFetchResponse
>(nullptr).Pass();
36 // Generate the signature.
37 scoped_ptr
<crypto::SignatureCreator
> signature_creator(
38 crypto::SignatureCreator::Create(private_key
,
39 crypto::SignatureCreator::SHA1
));
40 signature_creator
->Update(
41 reinterpret_cast<const uint8
*>(policy_response
->policy_data().c_str()),
42 policy_response
->policy_data().size());
43 std::vector
<uint8
> signature_bytes
;
44 std::string policy_blob
;
45 if (!signature_creator
->Final(&signature_bytes
)) {
46 LOG(ERROR
) << "Failed to create policy signature.";
47 return scoped_ptr
<em::PolicyFetchResponse
>(nullptr).Pass();
50 policy_response
->mutable_policy_data_signature()->assign(
51 reinterpret_cast<const char*>(vector_as_array(&signature_bytes
)),
52 signature_bytes
.size());
53 return policy_response
.Pass();
58 OwnerSettingsService::OwnerSettingsService(
59 const scoped_refptr
<ownership::OwnerKeyUtil
>& owner_key_util
)
60 : owner_key_util_(owner_key_util
), weak_factory_(this) {
63 OwnerSettingsService::~OwnerSettingsService() {
64 DCHECK(thread_checker_
.CalledOnValidThread());
67 void OwnerSettingsService::AddObserver(Observer
* observer
) {
68 if (observer
&& !observers_
.HasObserver(observer
))
69 observers_
.AddObserver(observer
);
72 void OwnerSettingsService::RemoveObserver(Observer
* observer
) {
73 observers_
.RemoveObserver(observer
);
76 bool OwnerSettingsService::IsOwner() {
77 DCHECK(thread_checker_
.CalledOnValidThread());
78 return private_key_
.get() && private_key_
->key();
81 void OwnerSettingsService::IsOwnerAsync(const IsOwnerCallback
& callback
) {
82 DCHECK(thread_checker_
.CalledOnValidThread());
83 if (private_key_
.get()) {
84 base::MessageLoop::current()->PostTask(FROM_HERE
,
85 base::Bind(callback
, IsOwner()));
87 pending_is_owner_callbacks_
.push_back(callback
);
91 bool OwnerSettingsService::AssembleAndSignPolicyAsync(
92 base::TaskRunner
* task_runner
,
93 scoped_ptr
<em::PolicyData
> policy
,
94 const AssembleAndSignPolicyAsyncCallback
& callback
) {
95 DCHECK(thread_checker_
.CalledOnValidThread());
96 if (!task_runner
|| !IsOwner())
98 return base::PostTaskAndReplyWithResult(
102 &AssembleAndSignPolicy
, base::Passed(&policy
), private_key_
->key()),
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
);
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
);
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
);
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
);
131 void OwnerSettingsService::ReloadKeypair() {
133 base::Bind(&OwnerSettingsService::OnKeypairLoaded
, as_weak_ptr()));
136 void OwnerSettingsService::OnKeypairLoaded(
137 const scoped_refptr
<PublicKey
>& public_key
,
138 const scoped_refptr
<PrivateKey
>& private_key
) {
139 DCHECK(thread_checker_
.CalledOnValidThread());
141 public_key_
= public_key
;
142 private_key_
= private_key
;
144 const bool is_owner
= IsOwner();
145 std::vector
<IsOwnerCallback
> is_owner_callbacks
;
146 is_owner_callbacks
.swap(pending_is_owner_callbacks_
);
147 for (std::vector
<IsOwnerCallback
>::iterator
it(is_owner_callbacks
.begin());
148 it
!= is_owner_callbacks
.end();
153 OnPostKeypairLoadedActions();
156 } // namespace ownership