1bbef5dff2b94fef72012e721cd6124cd87621af4Utkarsh Sanghi// 2bbef5dff2b94fef72012e721cd6124cd87621af4Utkarsh Sanghi// Copyright (C) 2014 The Android Open Source Project 3bbef5dff2b94fef72012e721cd6124cd87621af4Utkarsh Sanghi// 4bbef5dff2b94fef72012e721cd6124cd87621af4Utkarsh Sanghi// Licensed under the Apache License, Version 2.0 (the "License"); 5bbef5dff2b94fef72012e721cd6124cd87621af4Utkarsh Sanghi// you may not use this file except in compliance with the License. 6bbef5dff2b94fef72012e721cd6124cd87621af4Utkarsh Sanghi// You may obtain a copy of the License at 7bbef5dff2b94fef72012e721cd6124cd87621af4Utkarsh Sanghi// 8bbef5dff2b94fef72012e721cd6124cd87621af4Utkarsh Sanghi// http://www.apache.org/licenses/LICENSE-2.0 9bbef5dff2b94fef72012e721cd6124cd87621af4Utkarsh Sanghi// 10bbef5dff2b94fef72012e721cd6124cd87621af4Utkarsh Sanghi// Unless required by applicable law or agreed to in writing, software 11bbef5dff2b94fef72012e721cd6124cd87621af4Utkarsh Sanghi// distributed under the License is distributed on an "AS IS" BASIS, 12bbef5dff2b94fef72012e721cd6124cd87621af4Utkarsh Sanghi// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13bbef5dff2b94fef72012e721cd6124cd87621af4Utkarsh Sanghi// See the License for the specific language governing permissions and 14bbef5dff2b94fef72012e721cd6124cd87621af4Utkarsh Sanghi// limitations under the License. 15bbef5dff2b94fef72012e721cd6124cd87621af4Utkarsh Sanghi// 1635af244e7cf856a02e46ec8f186f36c53582757dUtkarsh Sanghi 1735af244e7cf856a02e46ec8f186f36c53582757dUtkarsh Sanghi#ifndef TRUNKS_SCOPED_KEY_HANDLE_H_ 1835af244e7cf856a02e46ec8f186f36c53582757dUtkarsh Sanghi#define TRUNKS_SCOPED_KEY_HANDLE_H_ 1935af244e7cf856a02e46ec8f186f36c53582757dUtkarsh Sanghi 2035af244e7cf856a02e46ec8f186f36c53582757dUtkarsh Sanghi#include "trunks/tpm_generated.h" 21ef87f3e63b93ff722a8cbcee9c54d55d085221a3Darren Krahn#include "trunks/trunks_export.h" 2235af244e7cf856a02e46ec8f186f36c53582757dUtkarsh Sanghi#include "trunks/trunks_factory.h" 2335af244e7cf856a02e46ec8f186f36c53582757dUtkarsh Sanghi 2435af244e7cf856a02e46ec8f186f36c53582757dUtkarsh Sanghinamespace trunks { 2535af244e7cf856a02e46ec8f186f36c53582757dUtkarsh Sanghi 26ff7f2da556b21253a52abbc82e7cf7bee54a850eUtkarsh Sanghi// This class is used to wrap a Key or NV ram handle given by the TPM. 27ff7f2da556b21253a52abbc82e7cf7bee54a850eUtkarsh Sanghi// It provides a destructor that cleans up TPM resources associated with 28ff7f2da556b21253a52abbc82e7cf7bee54a850eUtkarsh Sanghi// that handle. 29ef87f3e63b93ff722a8cbcee9c54d55d085221a3Darren Krahnclass TRUNKS_EXPORT ScopedKeyHandle { 3035af244e7cf856a02e46ec8f186f36c53582757dUtkarsh Sanghi public: 3135af244e7cf856a02e46ec8f186f36c53582757dUtkarsh Sanghi // We provide a factory to the constructor so that we can later free 3235af244e7cf856a02e46ec8f186f36c53582757dUtkarsh Sanghi // resources associated with the handle. 3335af244e7cf856a02e46ec8f186f36c53582757dUtkarsh Sanghi explicit ScopedKeyHandle(const TrunksFactory& factory); 3435af244e7cf856a02e46ec8f186f36c53582757dUtkarsh Sanghi ScopedKeyHandle(const TrunksFactory& factory, TPM_HANDLE handle); 3535af244e7cf856a02e46ec8f186f36c53582757dUtkarsh Sanghi virtual ~ScopedKeyHandle(); 3635af244e7cf856a02e46ec8f186f36c53582757dUtkarsh Sanghi 3735af244e7cf856a02e46ec8f186f36c53582757dUtkarsh Sanghi // This method releases the TPM_HANDLE associated with this class. 3835af244e7cf856a02e46ec8f186f36c53582757dUtkarsh Sanghi // It returns the handle that was previously wrapped, and returns 3935af244e7cf856a02e46ec8f186f36c53582757dUtkarsh Sanghi // INVALID_HANDLE if the previous handle was unset. 4035af244e7cf856a02e46ec8f186f36c53582757dUtkarsh Sanghi virtual TPM_HANDLE release(); 4135af244e7cf856a02e46ec8f186f36c53582757dUtkarsh Sanghi 4235af244e7cf856a02e46ec8f186f36c53582757dUtkarsh Sanghi // This method flushes all context associated with the current handle, 4335af244e7cf856a02e46ec8f186f36c53582757dUtkarsh Sanghi // and has the class wrap |new_handle| 4435af244e7cf856a02e46ec8f186f36c53582757dUtkarsh Sanghi virtual void reset(TPM_HANDLE new_handle); 4535af244e7cf856a02e46ec8f186f36c53582757dUtkarsh Sanghi 4635af244e7cf856a02e46ec8f186f36c53582757dUtkarsh Sanghi // This method flushes all context associated with the current handle, 4735af244e7cf856a02e46ec8f186f36c53582757dUtkarsh Sanghi // and resets the internal handle of the class to the uninitialized value. 4835af244e7cf856a02e46ec8f186f36c53582757dUtkarsh Sanghi // Note: After reset() this class should not be used again till a new handle 4935af244e7cf856a02e46ec8f186f36c53582757dUtkarsh Sanghi // is injected. 5035af244e7cf856a02e46ec8f186f36c53582757dUtkarsh Sanghi virtual void reset(); 5135af244e7cf856a02e46ec8f186f36c53582757dUtkarsh Sanghi 525499a0e04e12b6280d826a2ca91e99227aa0ba05Utkarsh Sanghi // This method returns a pointer to the handle associated with this class. 535499a0e04e12b6280d826a2ca91e99227aa0ba05Utkarsh Sanghi // This method does not transfer ownership. 545499a0e04e12b6280d826a2ca91e99227aa0ba05Utkarsh Sanghi virtual TPM_HANDLE* ptr(); 555499a0e04e12b6280d826a2ca91e99227aa0ba05Utkarsh Sanghi 5635af244e7cf856a02e46ec8f186f36c53582757dUtkarsh Sanghi // This method returns the handle currectly associated with the class. 575499a0e04e12b6280d826a2ca91e99227aa0ba05Utkarsh Sanghi // This method does not transfer ownership, therefore the handle returned 5835af244e7cf856a02e46ec8f186f36c53582757dUtkarsh Sanghi // might be stale. 594ede7fcc1571b23867536b2506900fc3987c2dd5Darren Krahn virtual TPM_HANDLE get() const; 6035af244e7cf856a02e46ec8f186f36c53582757dUtkarsh Sanghi 6135af244e7cf856a02e46ec8f186f36c53582757dUtkarsh Sanghi private: 6235af244e7cf856a02e46ec8f186f36c53582757dUtkarsh Sanghi const TrunksFactory& factory_; 6335af244e7cf856a02e46ec8f186f36c53582757dUtkarsh Sanghi TPM_HANDLE handle_; 6435af244e7cf856a02e46ec8f186f36c53582757dUtkarsh Sanghi void FlushHandleContext(TPM_HANDLE handle); 6535af244e7cf856a02e46ec8f186f36c53582757dUtkarsh Sanghi 6635af244e7cf856a02e46ec8f186f36c53582757dUtkarsh Sanghi DISALLOW_COPY_AND_ASSIGN(ScopedKeyHandle); 6735af244e7cf856a02e46ec8f186f36c53582757dUtkarsh Sanghi}; 6835af244e7cf856a02e46ec8f186f36c53582757dUtkarsh Sanghi 6935af244e7cf856a02e46ec8f186f36c53582757dUtkarsh Sanghi} // namespace trunks 7035af244e7cf856a02e46ec8f186f36c53582757dUtkarsh Sanghi 7135af244e7cf856a02e46ec8f186f36c53582757dUtkarsh Sanghi#endif // TRUNKS_SCOPED_KEY_HANDLE_H_ 72