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 #ifndef TRUNKS_TPM_STATE_IMPL_H_
18 #define TRUNKS_TPM_STATE_IMPL_H_
19 
20 #include "trunks/tpm_state.h"
21 
22 #include <map>
23 
24 #include <base/callback.h>
25 #include <base/macros.h>
26 
27 #include "trunks/tpm_generated.h"
28 #include "trunks/trunks_export.h"
29 
30 namespace trunks {
31 
32 class TrunksFactory;
33 
34 // TpmStateImpl is the default implementation of the TpmState interface.
35 class TRUNKS_EXPORT TpmStateImpl : public TpmState {
36  public:
37   explicit TpmStateImpl(const TrunksFactory& factory);
38   ~TpmStateImpl() override = default;
39 
40   // TpmState methods.
41   TPM_RC Initialize() override;
42   bool IsOwnerPasswordSet() override;
43   bool IsEndorsementPasswordSet() override;
44   bool IsLockoutPasswordSet() override;
45   bool IsOwned() override;
46   bool IsInLockout() override;
47   bool IsPlatformHierarchyEnabled() override;
48   bool IsStorageHierarchyEnabled() override;
49   bool IsEndorsementHierarchyEnabled() override;
50   bool IsEnabled() override;
51   bool WasShutdownOrderly() override;
52   bool IsRSASupported() override;
53   bool IsECCSupported() override;
54   uint32_t GetLockoutCounter() override;
55   uint32_t GetLockoutThreshold() override;
56   uint32_t GetLockoutInterval() override;
57   uint32_t GetLockoutRecovery() override;
58   uint32_t GetMaxNVSize() override;
59   bool GetTpmProperty(TPM_PT property, uint32_t* value) override;
60   bool GetAlgorithmProperties(TPM_ALG_ID algorithm,
61                               TPMA_ALGORITHM* properties) override;
62 
63  private:
64   // This helper method calls TPM2_GetCapability in a loop until all available
65   // capabilities of the given type are sent to the |callback|. The callback
66   // returns the next property value to query if there is more data available or
67   // 0 if the capability data was empty.
68   using CapabilityCallback = base::Callback<uint32_t(const TPMU_CAPABILITIES&)>;
69   TPM_RC GetCapability(const CapabilityCallback& callback,
70                        TPM_CAP capability,
71                        uint32_t property,
72                        uint32_t max_properties_per_call);
73   // Queries TPM properties and populates tpm_properties_.
74   TPM_RC CacheTpmProperties();
75   // Queries algorithm properties and populates algorithm_properties_.
76   TPM_RC CacheAlgorithmProperties();
77 
78   const TrunksFactory& factory_;
79   bool initialized_{false};
80   std::map<TPM_PT, uint32_t> tpm_properties_;
81   std::map<TPM_ALG_ID, TPMA_ALGORITHM> algorithm_properties_;
82 
83   DISALLOW_COPY_AND_ASSIGN(TpmStateImpl);
84 };
85 
86 }  // namespace trunks
87 
88 #endif  // TRUNKS_TPM_STATE_IMPL_H_
89