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 
21 #include "trunks/blob_parser.h"
22 #include "trunks/hmac_session_impl.h"
23 #include "trunks/password_authorization_delegate.h"
24 #include "trunks/policy_session_impl.h"
25 #include "trunks/session_manager_impl.h"
26 #include "trunks/tpm_generated.h"
27 #include "trunks/tpm_state_impl.h"
28 #include "trunks/tpm_utility_impl.h"
29 #if defined(USE_BINDER_IPC)
30 #include "trunks/trunks_binder_proxy.h"
31 #else
32 #include "trunks/trunks_dbus_proxy.h"
33 #endif
34 
35 namespace trunks {
36 
TrunksFactoryImpl(bool failure_is_fatal)37 TrunksFactoryImpl::TrunksFactoryImpl(bool failure_is_fatal) {
38 #if defined(USE_BINDER_IPC)
39   default_transceiver_.reset(new TrunksBinderProxy());
40 #else
41   default_transceiver_.reset(new TrunksDBusProxy());
42 #endif
43   transceiver_ = default_transceiver_.get();
44   tpm_.reset(new Tpm(transceiver_));
45   if (!transceiver_->Init()) {
46     if (failure_is_fatal) {
47       LOG(FATAL) << "Error initializing default IPC proxy.";
48     } else {
49       LOG(ERROR) << "Error initializing default IPC proxy.";
50     }
51   }
52 }
53 
TrunksFactoryImpl(CommandTransceiver * transceiver)54 TrunksFactoryImpl::TrunksFactoryImpl(CommandTransceiver* transceiver) {
55   transceiver_ = transceiver;
56   tpm_.reset(new Tpm(transceiver_));
57 }
58 
~TrunksFactoryImpl()59 TrunksFactoryImpl::~TrunksFactoryImpl() {}
60 
GetTpm() const61 Tpm* TrunksFactoryImpl::GetTpm() const {
62   return tpm_.get();
63 }
64 
GetTpmState() const65 scoped_ptr<TpmState> TrunksFactoryImpl::GetTpmState() const {
66   return scoped_ptr<TpmState>(new TpmStateImpl(*this));
67 }
68 
GetTpmUtility() const69 scoped_ptr<TpmUtility> TrunksFactoryImpl::GetTpmUtility() const {
70   return scoped_ptr<TpmUtility>(new TpmUtilityImpl(*this));
71 }
72 
GetPasswordAuthorization(const std::string & password) const73 scoped_ptr<AuthorizationDelegate> TrunksFactoryImpl::GetPasswordAuthorization(
74     const std::string& password) const {
75   return scoped_ptr<AuthorizationDelegate>(
76       new PasswordAuthorizationDelegate(password));
77 }
78 
GetSessionManager() const79 scoped_ptr<SessionManager> TrunksFactoryImpl::GetSessionManager() const {
80   return scoped_ptr<SessionManager>(new SessionManagerImpl(*this));
81 }
82 
GetHmacSession() const83 scoped_ptr<HmacSession> TrunksFactoryImpl::GetHmacSession() const {
84   return scoped_ptr<HmacSession>(new HmacSessionImpl(*this));
85 }
86 
GetPolicySession() const87 scoped_ptr<PolicySession> TrunksFactoryImpl::GetPolicySession() const {
88   return scoped_ptr<PolicySession>(new PolicySessionImpl(*this, TPM_SE_POLICY));
89 }
90 
GetTrialSession() const91 scoped_ptr<PolicySession> TrunksFactoryImpl::GetTrialSession() const {
92   return scoped_ptr<PolicySession>(new PolicySessionImpl(*this, TPM_SE_TRIAL));
93 }
94 
GetBlobParser() const95 scoped_ptr<BlobParser> TrunksFactoryImpl::GetBlobParser() const {
96   return scoped_ptr<BlobParser>(new BlobParser());
97 }
98 
99 }  // namespace trunks
100