1 //
2 // Copyright (C) 2014 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #include "trunks/trunks_factory_impl.h"
18 
19 #include <base/logging.h>
20 #include <base/memory/ptr_util.h>
21 
22 #include "trunks/blob_parser.h"
23 #include "trunks/hmac_session_impl.h"
24 #include "trunks/password_authorization_delegate.h"
25 #include "trunks/policy_session_impl.h"
26 #include "trunks/session_manager_impl.h"
27 #include "trunks/tpm_generated.h"
28 #include "trunks/tpm_state_impl.h"
29 #include "trunks/tpm_utility_impl.h"
30 #if defined(USE_BINDER_IPC)
31 #include "trunks/trunks_binder_proxy.h"
32 #else
33 #include "trunks/trunks_dbus_proxy.h"
34 #endif
35 
36 namespace trunks {
37 
TrunksFactoryImpl()38 TrunksFactoryImpl::TrunksFactoryImpl() {
39 #if defined(USE_BINDER_IPC)
40   default_transceiver_.reset(new TrunksBinderProxy());
41 #else
42   default_transceiver_.reset(new TrunksDBusProxy());
43 #endif
44   transceiver_ = default_transceiver_.get();
45 }
46 
TrunksFactoryImpl(CommandTransceiver * transceiver)47 TrunksFactoryImpl::TrunksFactoryImpl(CommandTransceiver* transceiver) {
48   transceiver_ = transceiver;
49 }
50 
~TrunksFactoryImpl()51 TrunksFactoryImpl::~TrunksFactoryImpl() {}
52 
Initialize()53 bool TrunksFactoryImpl::Initialize() {
54   if (initialized_) {
55     return true;
56   }
57   tpm_.reset(new Tpm(transceiver_));
58   if (transceiver_ != default_transceiver_.get()) {
59     initialized_ = true;
60   } else {
61     initialized_ = transceiver_->Init();
62     if (!initialized_) {
63       LOG(WARNING) << "Failed to initialize the trunks IPC proxy; "
64                    << "trunksd is not ready.";
65     }
66   }
67   return initialized_;
68 }
69 
GetTpm() const70 Tpm* TrunksFactoryImpl::GetTpm() const {
71   return tpm_.get();
72 }
73 
GetTpmState() const74 std::unique_ptr<TpmState> TrunksFactoryImpl::GetTpmState() const {
75   return base::MakeUnique<TpmStateImpl>(*this);
76 }
77 
GetTpmUtility() const78 std::unique_ptr<TpmUtility> TrunksFactoryImpl::GetTpmUtility() const {
79   return base::MakeUnique<TpmUtilityImpl>(*this);
80 }
81 
82 std::unique_ptr<AuthorizationDelegate>
GetPasswordAuthorization(const std::string & password) const83 TrunksFactoryImpl::GetPasswordAuthorization(const std::string& password) const {
84   return base::MakeUnique<PasswordAuthorizationDelegate>(password);
85 }
86 
GetSessionManager() const87 std::unique_ptr<SessionManager> TrunksFactoryImpl::GetSessionManager() const {
88   return base::MakeUnique<SessionManagerImpl>(*this);
89 }
90 
GetHmacSession() const91 std::unique_ptr<HmacSession> TrunksFactoryImpl::GetHmacSession() const {
92   return base::MakeUnique<HmacSessionImpl>(*this);
93 }
94 
GetPolicySession() const95 std::unique_ptr<PolicySession> TrunksFactoryImpl::GetPolicySession() const {
96   return base::MakeUnique<PolicySessionImpl>(*this, TPM_SE_POLICY);
97 }
98 
GetTrialSession() const99 std::unique_ptr<PolicySession> TrunksFactoryImpl::GetTrialSession() const {
100   return base::MakeUnique<PolicySessionImpl>(*this, TPM_SE_TRIAL);
101 }
102 
GetBlobParser() const103 std::unique_ptr<BlobParser> TrunksFactoryImpl::GetBlobParser() const {
104   return base::MakeUnique<BlobParser>();
105 }
106 
107 }  // namespace trunks
108