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. In most |
76 void ClearOpenSSLERRStack(); | 83 // cases you should pass FROM_HERE as the |location|. |
| 84 void ClearOpenSSLERRStack(const tracked_objects::Location& location); |
| 85 |
| 86 // Place an instance of this class on the call stack to automatically clear |
| 87 // the OpenSSL error stack on function exit. Note any diagnostic will be emitted |
| 88 // with the location of construction, as it's not possible to track the callsite |
| 89 // of the destructor. |
| 90 class OpenSSLErrStackTracer { |
| 91 public: |
| 92 explicit OpenSSLErrStackTracer(const tracked_objects::Location& location) |
| 93 : location_(location) { |
| 94 EnsureOpenSSLInit(); |
| 95 } |
| 96 ~OpenSSLErrStackTracer() { ClearOpenSSLERRStack(location_); } |
| 97 |
| 98 private: |
| 99 const tracked_objects::Location location_; |
| 100 |
| 101 DISALLOW_IMPLICIT_CONSTRUCTORS(OpenSSLErrStackTracer); |
| 102 }; |
77 | 103 |
78 } // namespace base | 104 } // namespace base |
79 | 105 |
80 #endif // BASE_OPENSSL_UTIL_H_ | 106 #endif // BASE_OPENSSL_UTIL_H_ |
OLD | NEW |