Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_OPENSSL_UTIL_H_ | 5 #ifndef BASE_OPENSSL_UTIL_H_ |
| 6 #define BASE_OPENSSL_UTIL_H_ | 6 #define BASE_OPENSSL_UTIL_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/tracked.h" | 10 #include "base/tracked.h" |
| 11 | 11 |
| 12 namespace base { | 12 namespace base { |
| 13 | 13 |
| 14 // A helper class that takes care of destroying OpenSSL objects when it goes out | |
| 15 // of scope. | |
| 16 template <typename T, void (*destructor)(T*)> | |
| 17 class ScopedSSL { | |
|
wtc
2010/11/16 15:35:07
This template class should be named ScopedOpenSSL.
joth
2010/11/16 16:07:19
Done.
| |
| 18 public: | |
| 19 explicit ScopedSSL(T* ptr_) : ptr_(ptr_) { } | |
| 20 ~ScopedSSL() { if (ptr_) (*destructor)(ptr_); } | |
| 21 | |
| 22 T* get() const { return ptr_; } | |
| 23 | |
| 24 private: | |
| 25 T* ptr_; | |
| 26 }; | |
| 27 | |
| 14 // Provides a buffer of at least MIN_SIZE bytes, for use when calling OpenSSL's | 28 // Provides a buffer of at least MIN_SIZE bytes, for use when calling OpenSSL's |
| 15 // SHA256, HMAC, etc functions, adapting the buffer sizing rules to meet those | 29 // SHA256, HMAC, etc functions, adapting the buffer sizing rules to meet those |
| 16 // of the our base wrapper APIs. | 30 // of the our base wrapper APIs. |
| 17 // This allows the library to write directly to the caller's buffer if it is of | 31 // This allows the library to write directly to the caller's buffer if it is of |
| 18 // sufficient size, but if not it will write to temporary |min_sized_buffer_| | 32 // sufficient size, but if not it will write to temporary |min_sized_buffer_| |
| 19 // of required size and then its content is automatically copied out on | 33 // of required size and then its content is automatically copied out on |
| 20 // destruction, with truncation as appropriate. | 34 // destruction, with truncation as appropriate. |
| 21 template<int MIN_SIZE> | 35 template<int MIN_SIZE> |
| 22 class ScopedOpenSSLSafeSizeBuffer { | 36 class ScopedOpenSSLSafeSizeBuffer { |
| 23 public: | 37 public: |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 44 unsigned char* output_; | 58 unsigned char* output_; |
| 45 size_t output_len_; | 59 size_t output_len_; |
| 46 | 60 |
| 47 // Temporary buffer writen into in the case where the caller's | 61 // Temporary buffer writen into in the case where the caller's |
| 48 // buffer is not of sufficient size. | 62 // buffer is not of sufficient size. |
| 49 unsigned char min_sized_buffer_[MIN_SIZE]; | 63 unsigned char min_sized_buffer_[MIN_SIZE]; |
| 50 | 64 |
| 51 DISALLOW_COPY_AND_ASSIGN(ScopedOpenSSLSafeSizeBuffer); | 65 DISALLOW_COPY_AND_ASSIGN(ScopedOpenSSLSafeSizeBuffer); |
| 52 }; | 66 }; |
| 53 | 67 |
| 68 // Initialize OpenSSL if it isn't already initialized. This must be called | |
| 69 // before any other OpenSSL functions. | |
| 70 // This function is thread-safe, and OpenSSL will only ever be initialized once. | |
| 71 // OpenSSL will be properly shut down on program exit. | |
| 72 void EnsureOpenSSLInit(); | |
| 73 | |
| 54 // Drains the OpenSSL ERR_get_error stack. On a debug build the error codes | 74 // Drains the OpenSSL ERR_get_error stack. On a debug build the error codes |
| 55 // are send to VLOG(1), on a release build they are disregarded. | 75 // are send to VLOG(1), on a release build they are disregarded. |
| 56 void ClearOpenSSLERRStack(); | 76 void ClearOpenSSLERRStack(); |
| 57 | 77 |
| 58 } // namespace base | 78 } // namespace base |
| 59 | 79 |
| 60 #endif // BASE_OPENSSL_UTIL_H_ | 80 #endif // BASE_OPENSSL_UTIL_H_ |
| OLD | NEW |