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 "components/ownership/owner_key_util.h"
16 #include "crypto/signature_creator.h"
18 namespace em
= enterprise_management
;
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.";
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.";
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();
54 OwnerSettingsService::OwnerSettingsService(
55 const scoped_refptr
<ownership::OwnerKeyUtil
>& owner_key_util
)
56 : owner_key_util_(owner_key_util
), weak_factory_(this) {
59 OwnerSettingsService::~OwnerSettingsService() {
60 DCHECK(thread_checker_
.CalledOnValidThread());
63 bool OwnerSettingsService::IsOwner() {
64 DCHECK(thread_checker_
.CalledOnValidThread());
65 return private_key_
.get() && private_key_
->key();
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()));
74 pending_is_owner_callbacks_
.push_back(callback
);
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())
85 return base::PostTaskAndReplyWithResult(
89 &AssembleAndSignPolicy
, base::Passed(&policy
), private_key_
->key()),
93 void OwnerSettingsService::ReloadKeypair() {
95 base::Bind(&OwnerSettingsService::OnKeypairLoaded
, as_weak_ptr()));
98 void OwnerSettingsService::OnKeypairLoaded(
99 const scoped_refptr
<PublicKey
>& public_key
,
100 const scoped_refptr
<PrivateKey
>& private_key
) {
101 DCHECK(thread_checker_
.CalledOnValidThread());
103 public_key_
= public_key
;
104 private_key_
= private_key
;
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();
115 OnPostKeypairLoadedActions();
118 } // namespace ownership