You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

173 lines
5.0 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2005, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. // @file Contains utility classes that make it easier to use SecBuffers
  28. #ifndef TALK_BASE_SEC_BUFFER_H__
  29. #define TALK_BASE_SEC_BUFFER_H__
  30. namespace talk_base {
  31. // A base class for CSecBuffer<T>. Contains
  32. // all implementation that does not require
  33. // template arguments.
  34. class CSecBufferBase : public SecBuffer {
  35. public:
  36. CSecBufferBase() {
  37. Clear();
  38. }
  39. // Uses the SSPI to free a pointer, must be
  40. // used for buffers returned from SSPI APIs.
  41. static void FreeSSPI(void *ptr) {
  42. if ( ptr ) {
  43. SECURITY_STATUS status;
  44. status = ::FreeContextBuffer(ptr);
  45. ASSERT(SEC_E_OK == status); // "Freeing context buffer"
  46. }
  47. }
  48. // Deletes a buffer with operator delete
  49. static void FreeDelete(void *ptr) {
  50. delete [] reinterpret_cast<char*>(ptr);
  51. }
  52. // A noop delete, for buffers over other
  53. // people's memory
  54. static void FreeNone(void *ptr) {
  55. }
  56. protected:
  57. // Clears the buffer to EMPTY & NULL
  58. void Clear() {
  59. this->BufferType = SECBUFFER_EMPTY;
  60. this->cbBuffer = 0;
  61. this->pvBuffer = NULL;
  62. }
  63. };
  64. // Wrapper class for SecBuffer to take care
  65. // of initialization and destruction.
  66. template <void (*pfnFreeBuffer)(void *ptr)>
  67. class CSecBuffer: public CSecBufferBase {
  68. public:
  69. // Initializes buffer to empty & NULL
  70. CSecBuffer() {
  71. }
  72. // Frees any allocated memory
  73. ~CSecBuffer() {
  74. Release();
  75. }
  76. // Frees the buffer appropriately, and re-nulls
  77. void Release() {
  78. pfnFreeBuffer(this->pvBuffer);
  79. Clear();
  80. }
  81. private:
  82. // A placeholder function for compile-time asserts on the class
  83. void CompileAsserts() {
  84. // never invoked...
  85. assert(false); // _T("Notreached")
  86. // This class must not extend the size of SecBuffer, since
  87. // we use arrays of CSecBuffer in CSecBufferBundle below
  88. cassert(sizeof(CSecBuffer<SSPIFree> == sizeof(SecBuffer)));
  89. }
  90. };
  91. // Contains all generic implementation for the
  92. // SecBufferBundle class
  93. class SecBufferBundleBase {
  94. public:
  95. };
  96. // A template class that bundles a SecBufferDesc with
  97. // one or more SecBuffers for convenience. Can take
  98. // care of deallocating buffers appropriately, as indicated
  99. // by pfnFreeBuffer function.
  100. // By default does no deallocation.
  101. template <int num_buffers,
  102. void (*pfnFreeBuffer)(void *ptr) = CSecBufferBase::FreeNone>
  103. class CSecBufferBundle : public SecBufferBundleBase {
  104. public:
  105. // Constructs a security buffer bundle with num_buffers
  106. // buffers, all of which are empty and nulled.
  107. CSecBufferBundle() {
  108. desc_.ulVersion = SECBUFFER_VERSION;
  109. desc_.cBuffers = num_buffers;
  110. desc_.pBuffers = buffers_;
  111. }
  112. // Frees all currently used buffers.
  113. ~CSecBufferBundle() {
  114. Release();
  115. }
  116. // Accessor for the descriptor
  117. PSecBufferDesc desc() {
  118. return &desc_;
  119. }
  120. // Accessor for the descriptor
  121. const PSecBufferDesc desc() const {
  122. return &desc_;
  123. }
  124. // returns the i-th security buffer
  125. SecBuffer &operator[] (size_t num) {
  126. ASSERT(num < num_buffers); // "Buffer index out of bounds"
  127. return buffers_[num];
  128. }
  129. // returns the i-th security buffer
  130. const SecBuffer &operator[] (size_t num) const {
  131. ASSERT(num < num_buffers); // "Buffer index out of bounds"
  132. return buffers_[num];
  133. }
  134. // Frees all non-NULL security buffers,
  135. // using the deallocation function
  136. void Release() {
  137. for ( size_t i = 0; i < num_buffers; ++i ) {
  138. buffers_[i].Release();
  139. }
  140. }
  141. private:
  142. // Our descriptor
  143. SecBufferDesc desc_;
  144. // Our bundled buffers, each takes care of its own
  145. // initialization and destruction
  146. CSecBuffer<pfnFreeBuffer> buffers_[num_buffers];
  147. };
  148. } // namespace talk_base
  149. #endif // TALK_BASE_SEC_BUFFER_H__