OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // Implements a simple framework for scoping TSS values. |
| 6 // Based on chrome's base/scoped_ptr_malloc implementation. |
| 7 // |
| 8 // Example usage: |
| 9 // ScopedTssContext context_handle; |
| 10 // TSS_RESULT result; |
| 11 // if (!OpenAndConnectTpm(context_handle.get(), &result)) |
| 12 // ... |
| 13 // ScopedTssKey srk(context_handle); |
| 14 // if (!LoadSrk(context_handle, srk_handle.get(), &result)) |
| 15 // ... |
| 16 // |
| 17 // See the bottom of this file for common typedefs. |
| 18 |
| 19 #include <assert.h> |
| 20 #include <trousers/tss.h> |
| 21 #include <trousers/trousers.h> |
| 22 |
| 23 #include <base/compiler_specific.h> |
| 24 #include <vector> |
| 25 |
| 26 #ifndef CRYPTOHOME_SCOPED_TSS_TYPE_H_ |
| 27 #define CRYPTOHOME_SCOPED_TSS_TYPE_H_ |
| 28 |
| 29 namespace cryptohome { |
| 30 |
| 31 class ScopedTssContextRelease { |
| 32 public: |
| 33 inline void operator()(TSS_HCONTEXT unused, TSS_HCONTEXT context) const { |
| 34 // Usually, only |context| is used, but if the ScopedTssContext is |
| 35 // used slightly differently, it may end up with a context in |unused|. |
| 36 // For now, treat that as a bug. |
| 37 assert(unused == 0); |
| 38 if (context) |
| 39 Tspi_Context_Close(context); |
| 40 } |
| 41 }; |
| 42 |
| 43 class ScopedTssMemoryRelease { |
| 44 public: |
| 45 inline void operator()(TSS_HCONTEXT context, BYTE* memory) const { |
| 46 // TODO(wad) make the test code friendly for assert()ing context/memory != 0 |
| 47 if (context && memory) |
| 48 Tspi_Context_FreeMemory(context, memory); |
| 49 } |
| 50 }; |
| 51 |
| 52 class ScopedTssObjectRelease { |
| 53 public: |
| 54 inline void operator()(TSS_HCONTEXT context, TSS_HOBJECT handle) const { |
| 55 // TODO(wad) make the test code friendly for assert() context/handle != 0 |
| 56 if (context && handle) |
| 57 Tspi_Context_CloseObject(context, handle); |
| 58 } |
| 59 }; |
| 60 |
| 61 // Provide a basic scoped container for TSS managed objects. |
| 62 template<class TssType, class ReleaseProc = ScopedTssObjectRelease> |
| 63 class ScopedTssType { |
| 64 public: |
| 65 explicit ScopedTssType(TSS_HCONTEXT c = 0, TssType t = 0) : |
| 66 context_(c), |
| 67 type_(t) {} |
| 68 virtual ~ScopedTssType() { |
| 69 release_(context_, type_); |
| 70 } |
| 71 |
| 72 // Allow typecasting to TssType. |
| 73 operator TssType() { return type_; } |
| 74 |
| 75 // Allow direct referencing of the wrapped value. |
| 76 TssType* ptr() { |
| 77 return &type_; |
| 78 } |
| 79 |
| 80 // Returns the assigned context. |
| 81 virtual TSS_HCONTEXT context() { |
| 82 return context_; |
| 83 } |
| 84 |
| 85 virtual TssType release() WARN_UNUSED_RESULT { |
| 86 TssType tmp = type_; |
| 87 type_ = 0; |
| 88 context_ = 0; |
| 89 return tmp; |
| 90 } |
| 91 |
| 92 virtual void reset(TSS_HCONTEXT c = 0, TssType t = 0) { |
| 93 release_(context_, type_); |
| 94 context_ = c; |
| 95 type_ = t; |
| 96 } |
| 97 |
| 98 private: |
| 99 static ReleaseProc const release_; |
| 100 TSS_HCONTEXT context_; |
| 101 TssType type_; |
| 102 }; |
| 103 |
| 104 // Provide clear-cut helpers for the common cases. |
| 105 typedef ScopedTssType<TSS_HCONTEXT, ScopedTssContextRelease> ScopedTssContext; |
| 106 typedef ScopedTssType<BYTE*, ScopedTssMemoryRelease> ScopedTssMemory; |
| 107 |
| 108 typedef ScopedTssType<TSS_HOBJECT> ScopedTssObject; |
| 109 typedef ScopedTssType<TSS_HKEY> ScopedTssKey; |
| 110 typedef ScopedTssType<TSS_HPOLICY> ScopedTssPolicy; |
| 111 typedef ScopedTssType<TSS_HPCRS> ScopedTssPcrs; |
| 112 |
| 113 } // namespace cryptohome |
| 114 |
| 115 #endif // CRYPTOHOME_SCOPED_TSS_TYPE_H_ |
OLD | NEW |