[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | ac0f8be | 2010-11-12 12:03:54 | [diff] [blame] | 5 | #ifndef BASE_OPENSSL_UTIL_H_ |
| 6 | #define BASE_OPENSSL_UTIL_H_ |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 7 | #pragma once |
| 8 | |
| 9 | #include "base/basictypes.h" |
[email protected] | ac0f8be | 2010-11-12 12:03:54 | [diff] [blame] | 10 | #include "base/tracked.h" |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 11 | |
| 12 | namespace base { |
| 13 | |
[email protected] | 31383472 | 2010-11-17 09:57:18 | [diff] [blame] | 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 ScopedOpenSSL { |
| 18 | public: |
[email protected] | be796bb | 2010-11-18 15:43:43 | [diff] [blame^] | 19 | ScopedOpenSSL() : ptr_(NULL) { } |
| 20 | explicit ScopedOpenSSL(T* ptr) : ptr_(ptr) { } |
[email protected] | 31383472 | 2010-11-17 09:57:18 | [diff] [blame] | 21 | ~ScopedOpenSSL() { if (ptr_) (*destructor)(ptr_); } |
| 22 | |
| 23 | T* get() const { return ptr_; } |
[email protected] | be796bb | 2010-11-18 15:43:43 | [diff] [blame^] | 24 | void reset(T* ptr) { |
| 25 | if (ptr != ptr_) { |
| 26 | if (ptr_) (*destructor)(ptr_); |
| 27 | ptr_ = ptr; |
| 28 | } |
| 29 | } |
[email protected] | 31383472 | 2010-11-17 09:57:18 | [diff] [blame] | 30 | |
| 31 | private: |
| 32 | T* ptr_; |
| 33 | }; |
| 34 | |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 35 | // Provides a buffer of at least MIN_SIZE bytes, for use when calling OpenSSL's |
| 36 | // SHA256, HMAC, etc functions, adapting the buffer sizing rules to meet those |
| 37 | // of the our base wrapper APIs. |
| 38 | // This allows the library to write directly to the caller's buffer if it is of |
| 39 | // sufficient size, but if not it will write to temporary |min_sized_buffer_| |
| 40 | // of required size and then its content is automatically copied out on |
| 41 | // destruction, with truncation as appropriate. |
| 42 | template<int MIN_SIZE> |
| 43 | class ScopedOpenSSLSafeSizeBuffer { |
| 44 | public: |
| 45 | ScopedOpenSSLSafeSizeBuffer(unsigned char* output, size_t output_len) |
| 46 | : output_(output), |
| 47 | output_len_(output_len) { |
| 48 | } |
| 49 | |
| 50 | ~ScopedOpenSSLSafeSizeBuffer() { |
| 51 | if (output_len_ < MIN_SIZE) { |
| 52 | // Copy the temporary buffer out, truncating as needed. |
| 53 | memcpy(output_, min_sized_buffer_, output_len_); |
| 54 | } |
| 55 | // else... any writing already happened directly into |output_|. |
| 56 | } |
| 57 | |
| 58 | unsigned char* safe_buffer() { |
| 59 | return output_len_ < MIN_SIZE ? min_sized_buffer_ : output_; |
| 60 | } |
| 61 | |
| 62 | private: |
| 63 | // Pointer to the caller's data area and it's associated size, where data |
| 64 | // written via safe_buffer() will [eventually] end up. |
| 65 | unsigned char* output_; |
| 66 | size_t output_len_; |
| 67 | |
| 68 | // Temporary buffer writen into in the case where the caller's |
| 69 | // buffer is not of sufficient size. |
| 70 | unsigned char min_sized_buffer_[MIN_SIZE]; |
[email protected] | ac0f8be | 2010-11-12 12:03:54 | [diff] [blame] | 71 | |
| 72 | DISALLOW_COPY_AND_ASSIGN(ScopedOpenSSLSafeSizeBuffer); |
| 73 | }; |
| 74 | |
[email protected] | 31383472 | 2010-11-17 09:57:18 | [diff] [blame] | 75 | // Initialize OpenSSL if it isn't already initialized. This must be called |
| 76 | // before any other OpenSSL functions. |
| 77 | // This function is thread-safe, and OpenSSL will only ever be initialized once. |
| 78 | // OpenSSL will be properly shut down on program exit. |
| 79 | void EnsureOpenSSLInit(); |
| 80 | |
[email protected] | ac0f8be | 2010-11-12 12:03:54 | [diff] [blame] | 81 | // Drains the OpenSSL ERR_get_error stack. On a debug build the error codes |
[email protected] | be796bb | 2010-11-18 15:43:43 | [diff] [blame^] | 82 | // are send to VLOG(1), on a release build they are disregarded. In most |
| 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. |
| 88 | class OpenSSLErrStackTracer { |
| 89 | public: |
| 90 | // Pass FROM_HERE as |location|, to help track the source of OpenSSL error |
| 91 | // messages. Note any diagnostic emitted will be tagged with the location of |
| 92 | // the constructor call as it's not possible to trace a destructor's callsite. |
| 93 | explicit OpenSSLErrStackTracer(const tracked_objects::Location& location) |
| 94 | : location_(location) { |
| 95 | EnsureOpenSSLInit(); |
| 96 | } |
| 97 | ~OpenSSLErrStackTracer() { |
| 98 | ClearOpenSSLERRStack(location_); |
| 99 | } |
| 100 | |
| 101 | private: |
| 102 | const tracked_objects::Location location_; |
| 103 | |
| 104 | DISALLOW_IMPLICIT_CONSTRUCTORS(OpenSSLErrStackTracer); |
| 105 | }; |
[email protected] | ac0f8be | 2010-11-12 12:03:54 | [diff] [blame] | 106 | |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 107 | } // namespace base |
| 108 | |
[email protected] | ac0f8be | 2010-11-12 12:03:54 | [diff] [blame] | 109 | #endif // BASE_OPENSSL_UTIL_H_ |