Home | History | Annotate | Download | only in server
      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 #ifndef TPM_MANAGER_SERVER_TPM2_NVRAM_IMPL_H_
     18 #define TPM_MANAGER_SERVER_TPM2_NVRAM_IMPL_H_
     19 
     20 #include "tpm_manager/server/tpm_nvram.h"
     21 
     22 #include <memory>
     23 #include <string>
     24 
     25 #include <base/macros.h>
     26 #include <base/memory/scoped_ptr.h>
     27 #include <trunks/trunks_factory.h>
     28 
     29 #include "tpm_manager/server/local_data_store.h"
     30 
     31 namespace tpm_manager {
     32 
     33 class Tpm2NvramImpl : public TpmNvram {
     34  public:
     35   // Does not take ownership of |local_data_store|.
     36   explicit Tpm2NvramImpl(LocalDataStore* local_data_store);
     37   // Does not take ownership of |local_data_store|, but takes ownership of
     38   // |factory|.
     39   Tpm2NvramImpl(std::unique_ptr<trunks::TrunksFactory> factory,
     40                 LocalDataStore* local_data_store);
     41   ~Tpm2NvramImpl() override = default;
     42 
     43   // TpmNvram methods.
     44   bool DefineNvram(uint32_t index, size_t length) override;
     45   bool DestroyNvram(uint32_t index) override;
     46   bool WriteNvram(uint32_t index, const std::string& data) override;
     47   bool ReadNvram(uint32_t index, std::string* data) override;
     48   bool IsNvramDefined(uint32_t index, bool* defined) override;
     49   bool IsNvramLocked(uint32_t index, bool* locked) override;
     50   bool GetNvramSize(uint32_t index, size_t* size) override;
     51 
     52  private:
     53   // Initializes the connection to the Tpm2.0 and starts an authorization
     54   // session.
     55   // Note: there are no guarantees about the authorization value loaded into
     56   // |trunks_session_| at the end of this method.
     57   bool Initialize();
     58 
     59   // This method initializes and ensures that a valid owner password is
     60   // available. When this method returns, |owner_password_| will be loaded
     61   // into |trunks_session_|.
     62   bool InitializeWithOwnerPassword();
     63 
     64   std::unique_ptr<trunks::TrunksFactory> trunks_factory_;
     65   LocalDataStore* local_data_store_;
     66   bool initialized_;
     67   std::string owner_password_;
     68   scoped_ptr<trunks::HmacSession> trunks_session_;
     69   scoped_ptr<trunks::TpmUtility> trunks_utility_;
     70 
     71   friend class Tpm2NvramTest;
     72   DISALLOW_COPY_AND_ASSIGN(Tpm2NvramImpl);
     73 };
     74 
     75 }  // namespace tpm_manager
     76 
     77 #endif  // TPM_MANAGER_SERVER_TPM2_NVRAM_IMPL_H_
     78