• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (C) 2015 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 "tpm_manager/server/tpm2_status_impl.h"
18 
19 #include <base/logging.h>
20 #include <trunks/error_codes.h>
21 #include <trunks/tpm_generated.h>
22 #include <trunks/trunks_factory_impl.h>
23 
24 using trunks::TPM_RC;
25 using trunks::TPM_RC_SUCCESS;
26 
27 namespace tpm_manager {
28 
Tpm2StatusImpl()29 Tpm2StatusImpl::Tpm2StatusImpl()
30   : default_trunks_factory_(new trunks::TrunksFactoryImpl()),
31     trunks_factory_(default_trunks_factory_.get()),
32     trunks_tpm_state_(trunks_factory_->GetTpmState()) {}
33 
Tpm2StatusImpl(trunks::TrunksFactory * factory)34 Tpm2StatusImpl::Tpm2StatusImpl(trunks::TrunksFactory* factory)
35   : trunks_factory_(factory),
36     trunks_tpm_state_(trunks_factory_->GetTpmState()) {}
37 
IsTpmEnabled()38 bool Tpm2StatusImpl::IsTpmEnabled() {
39   if (!initialized_) {
40     Refresh();
41   }
42   return trunks_tpm_state_->IsEnabled();
43 }
44 
IsTpmOwned()45 bool Tpm2StatusImpl::IsTpmOwned() {
46   if (!is_owned_) {
47     Refresh();
48   }
49   is_owned_ = trunks_tpm_state_->IsOwned();
50   return is_owned_;
51 }
52 
GetDictionaryAttackInfo(int * counter,int * threshold,bool * lockout,int * seconds_remaining)53 bool Tpm2StatusImpl::GetDictionaryAttackInfo(int* counter,
54                                              int* threshold,
55                                              bool* lockout,
56                                              int* seconds_remaining) {
57   if (!Refresh()) {
58     return false;
59   }
60   if (counter) {
61     *counter = trunks_tpm_state_->GetLockoutCounter();
62   }
63   if (threshold) {
64     *threshold = trunks_tpm_state_->GetLockoutThreshold();
65   }
66   if (lockout) {
67     *lockout = trunks_tpm_state_->IsInLockout();
68   }
69   if (seconds_remaining) {
70     *seconds_remaining = trunks_tpm_state_->GetLockoutCounter() *
71                          trunks_tpm_state_->GetLockoutInterval();
72   }
73   return true;
74 }
75 
Refresh()76 bool Tpm2StatusImpl::Refresh() {
77   TPM_RC result = trunks_tpm_state_->Initialize();
78   if (result != TPM_RC_SUCCESS) {
79     LOG(WARNING) << "Error initializing trunks tpm state: "
80                  << trunks::GetErrorString(result);
81     return false;
82   }
83   initialized_ = true;
84   return true;
85 }
86 
87 }  // namespace tpm_manager
88