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 | 14 // A helper class that takes care of destroying OpenSSL objects when it goes out |
| 15 // of scope. | 15 // of scope. |
| 16 template <typename T, void (*destructor)(T*)> | 16 template <typename T, void (*destructor)(T*)> |
| 17 class ScopedOpenSSL { | 17 class ScopedOpenSSL { |
| 18 public: | 18 public: |
| 19 explicit ScopedOpenSSL(T* ptr_) : ptr_(ptr_) { } | 19 ScopedOpenSSL() : ptr_(NULL) { } |
| 20 explicit ScopedOpenSSL(T* ptr) : ptr_(ptr) { } | |
| 20 ~ScopedOpenSSL() { if (ptr_) (*destructor)(ptr_); } | 21 ~ScopedOpenSSL() { if (ptr_) (*destructor)(ptr_); } |
| 21 | 22 |
| 22 T* get() const { return ptr_; } | 23 T* get() const { return ptr_; } |
| 24 void reset(T* ptr) { | |
| 25 if (ptr != ptr_) { | |
| 26 if (ptr_) (*destructor)(ptr_); | |
| 27 ptr_ = ptr; | |
| 28 } | |
| 29 } | |
| 23 | 30 |
| 24 private: | 31 private: |
| 25 T* ptr_; | 32 T* ptr_; |
| 26 }; | 33 }; |
| 27 | 34 |
| 28 // Provides a buffer of at least MIN_SIZE bytes, for use when calling OpenSSL's | 35 // Provides a buffer of at least MIN_SIZE bytes, for use when calling OpenSSL's |
| 29 // SHA256, HMAC, etc functions, adapting the buffer sizing rules to meet those | 36 // SHA256, HMAC, etc functions, adapting the buffer sizing rules to meet those |
| 30 // of the our base wrapper APIs. | 37 // of the our base wrapper APIs. |
| 31 // This allows the library to write directly to the caller's buffer if it is of | 38 // This allows the library to write directly to the caller's buffer if it is of |
| 32 // sufficient size, but if not it will write to temporary |min_sized_buffer_| | 39 // sufficient size, but if not it will write to temporary |min_sized_buffer_| |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 65 DISALLOW_COPY_AND_ASSIGN(ScopedOpenSSLSafeSizeBuffer); | 72 DISALLOW_COPY_AND_ASSIGN(ScopedOpenSSLSafeSizeBuffer); |
| 66 }; | 73 }; |
| 67 | 74 |
| 68 // Initialize OpenSSL if it isn't already initialized. This must be called | 75 // Initialize OpenSSL if it isn't already initialized. This must be called |
| 69 // before any other OpenSSL functions. | 76 // before any other OpenSSL functions. |
| 70 // This function is thread-safe, and OpenSSL will only ever be initialized once. | 77 // This function is thread-safe, and OpenSSL will only ever be initialized once. |
| 71 // OpenSSL will be properly shut down on program exit. | 78 // OpenSSL will be properly shut down on program exit. |
| 72 void EnsureOpenSSLInit(); | 79 void EnsureOpenSSLInit(); |
| 73 | 80 |
| 74 // Drains the OpenSSL ERR_get_error stack. On a debug build the error codes | 81 // Drains the OpenSSL ERR_get_error stack. On a debug build the error codes |
| 75 // are send to VLOG(1), on a release build they are disregarded. | 82 // are send to VLOG(1), on a release build they are disregarded. An optional |
| 83 // |message| can be passed, which will be prepended to the error stack output | |
| 84 // if present. | |
| 85 void ClearOpenSSLERRStack(const char* message); | |
| 76 void ClearOpenSSLERRStack(); | 86 void ClearOpenSSLERRStack(); |
| 77 | 87 |
| 88 // Place an instance of this class on the call stack to automatically clear | |
| 89 // the OpenSSL error stack on function exit. The |message| passed, if not null, | |
| 90 // we be included in any error dump. | |
|
bulach
2010/11/17 14:35:09
s/we be/will be/
but perhaps even more direct:
If
joth
2010/11/17 14:49:31
Done.
| |
| 91 class OpenSSLErrStackTracer { | |
| 92 public: | |
| 93 OpenSSLErrStackTracer(const char* message) : message_(message) {} | |
|
bulach
2010/11/17 14:35:09
explicit
joth
2010/11/17 14:49:31
Done.
| |
| 94 ~OpenSSLErrStackTracer() { ClearOpenSSLERRStack(message_); } | |
| 95 | |
| 96 private: | |
| 97 const char* message_; | |
|
bulach
2010/11/17 14:35:09
const char* const
joth
2010/11/17 14:49:31
Done.
(Although, this rule is mostly useful for gl
| |
| 98 }; | |
| 99 | |
| 78 } // namespace base | 100 } // namespace base |
| 79 | 101 |
| 80 #endif // BASE_OPENSSL_UTIL_H_ | 102 #endif // BASE_OPENSSL_UTIL_H_ |
| OLD | NEW |