| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | |
| 3 * | |
| 4 * Use of this source code is governed by a BSD-style license | |
| 5 * that can be found in the LICENSE file in the root of the source | |
| 6 * tree. An additional intellectual property rights grant can be found | |
| 7 * in the file PATENTS. All contributing project authors may | |
| 8 * be found in the AUTHORS file in the root of the source tree. | |
| 9 */ | |
| 10 | |
| 11 // @file Contains utility classes that make it easier to use SecBuffers | |
| 12 | |
| 13 #ifndef WEBRTC_BASE_SEC_BUFFER_H__ | |
| 14 #define WEBRTC_BASE_SEC_BUFFER_H__ | |
| 15 | |
| 16 namespace rtc { | |
| 17 | |
| 18 // A base class for CSecBuffer<T>. Contains | |
| 19 // all implementation that does not require | |
| 20 // template arguments. | |
| 21 class CSecBufferBase : public SecBuffer { | |
| 22 public: | |
| 23 CSecBufferBase() { | |
| 24 Clear(); | |
| 25 } | |
| 26 | |
| 27 // Uses the SSPI to free a pointer, must be | |
| 28 // used for buffers returned from SSPI APIs. | |
| 29 static void FreeSSPI(void *ptr) { | |
| 30 if ( ptr ) { | |
| 31 SECURITY_STATUS status; | |
| 32 status = ::FreeContextBuffer(ptr); | |
| 33 ASSERT(SEC_E_OK == status); // "Freeing context buffer" | |
| 34 } | |
| 35 } | |
| 36 | |
| 37 // Deletes a buffer with operator delete | |
| 38 static void FreeDelete(void *ptr) { | |
| 39 delete [] reinterpret_cast<char*>(ptr); | |
| 40 } | |
| 41 | |
| 42 // A noop delete, for buffers over other | |
| 43 // people's memory | |
| 44 static void FreeNone(void *ptr) { | |
| 45 } | |
| 46 | |
| 47 protected: | |
| 48 // Clears the buffer to EMPTY & NULL | |
| 49 void Clear() { | |
| 50 this->BufferType = SECBUFFER_EMPTY; | |
| 51 this->cbBuffer = 0; | |
| 52 this->pvBuffer = NULL; | |
| 53 } | |
| 54 }; | |
| 55 | |
| 56 // Wrapper class for SecBuffer to take care | |
| 57 // of initialization and destruction. | |
| 58 template <void (*pfnFreeBuffer)(void *ptr)> | |
| 59 class CSecBuffer: public CSecBufferBase { | |
| 60 public: | |
| 61 // Initializes buffer to empty & NULL | |
| 62 CSecBuffer() { | |
| 63 } | |
| 64 | |
| 65 // Frees any allocated memory | |
| 66 ~CSecBuffer() { | |
| 67 Release(); | |
| 68 } | |
| 69 | |
| 70 // Frees the buffer appropriately, and re-nulls | |
| 71 void Release() { | |
| 72 pfnFreeBuffer(this->pvBuffer); | |
| 73 Clear(); | |
| 74 } | |
| 75 | |
| 76 // This class must not extend the size of SecBuffer, since we use arrays of | |
| 77 // CSecBuffer in CSecBufferBundle below. | |
| 78 static_assert(sizeof(CSecBuffer<pfnFreeBuffer>) == sizeof(SecBuffer), ""); | |
| 79 }; | |
| 80 | |
| 81 // Contains all generic implementation for the | |
| 82 // SecBufferBundle class | |
| 83 class SecBufferBundleBase { | |
| 84 public: | |
| 85 }; | |
| 86 | |
| 87 // A template class that bundles a SecBufferDesc with | |
| 88 // one or more SecBuffers for convenience. Can take | |
| 89 // care of deallocating buffers appropriately, as indicated | |
| 90 // by pfnFreeBuffer function. | |
| 91 // By default does no deallocation. | |
| 92 template <int num_buffers, | |
| 93 void (*pfnFreeBuffer)(void *ptr) = CSecBufferBase::FreeNone> | |
| 94 class CSecBufferBundle : public SecBufferBundleBase { | |
| 95 public: | |
| 96 // Constructs a security buffer bundle with num_buffers | |
| 97 // buffers, all of which are empty and nulled. | |
| 98 CSecBufferBundle() { | |
| 99 desc_.ulVersion = SECBUFFER_VERSION; | |
| 100 desc_.cBuffers = num_buffers; | |
| 101 desc_.pBuffers = buffers_; | |
| 102 } | |
| 103 | |
| 104 // Frees all currently used buffers. | |
| 105 ~CSecBufferBundle() { | |
| 106 Release(); | |
| 107 } | |
| 108 | |
| 109 // Accessor for the descriptor | |
| 110 PSecBufferDesc desc() { | |
| 111 return &desc_; | |
| 112 } | |
| 113 | |
| 114 // Accessor for the descriptor | |
| 115 PSecBufferDesc desc() const { | |
| 116 return &desc_; | |
| 117 } | |
| 118 | |
| 119 // returns the i-th security buffer | |
| 120 SecBuffer &operator[] (size_t num) { | |
| 121 ASSERT(num < num_buffers); // "Buffer index out of bounds" | |
| 122 return buffers_[num]; | |
| 123 } | |
| 124 | |
| 125 // returns the i-th security buffer | |
| 126 const SecBuffer &operator[] (size_t num) const { | |
| 127 ASSERT(num < num_buffers); // "Buffer index out of bounds" | |
| 128 return buffers_[num]; | |
| 129 } | |
| 130 | |
| 131 // Frees all non-NULL security buffers, | |
| 132 // using the deallocation function | |
| 133 void Release() { | |
| 134 for ( size_t i = 0; i < num_buffers; ++i ) { | |
| 135 buffers_[i].Release(); | |
| 136 } | |
| 137 } | |
| 138 | |
| 139 private: | |
| 140 // Our descriptor | |
| 141 SecBufferDesc desc_; | |
| 142 // Our bundled buffers, each takes care of its own | |
| 143 // initialization and destruction | |
| 144 CSecBuffer<pfnFreeBuffer> buffers_[num_buffers]; | |
| 145 }; | |
| 146 | |
| 147 } // namespace rtc | |
| 148 | |
| 149 #endif // WEBRTC_BASE_SEC_BUFFER_H__ | |
| OLD | NEW |