[email protected] | cf21188 | 2012-07-11 07:19:14 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 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] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 5 | #ifndef CRYPTO_OPENSSL_UTIL_H_ |
| 6 | #define CRYPTO_OPENSSL_UTIL_H_ |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 7 | |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 8 | #include <stddef.h> |
Erik Chen | c073e80 | 2020-04-22 21:21:54 | [diff] [blame] | 9 | #include <string.h> |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 10 | |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 11 | #include "base/location.h" |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 12 | #include "base/macros.h" |
[email protected] | fb10ca0 | 2011-10-06 19:17:36 | [diff] [blame] | 13 | #include "crypto/crypto_export.h" |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 14 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 15 | namespace crypto { |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 16 | |
| 17 | // Provides a buffer of at least MIN_SIZE bytes, for use when calling OpenSSL's |
| 18 | // SHA256, HMAC, etc functions, adapting the buffer sizing rules to meet those |
eroman | 01db75e | 2016-08-25 22:15:38 | [diff] [blame] | 19 | // of our base wrapper APIs. |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 20 | // This allows the library to write directly to the caller's buffer if it is of |
| 21 | // sufficient size, but if not it will write to temporary |min_sized_buffer_| |
| 22 | // of required size and then its content is automatically copied out on |
| 23 | // destruction, with truncation as appropriate. |
| 24 | template<int MIN_SIZE> |
| 25 | class ScopedOpenSSLSafeSizeBuffer { |
| 26 | public: |
| 27 | ScopedOpenSSLSafeSizeBuffer(unsigned char* output, size_t output_len) |
| 28 | : output_(output), |
| 29 | output_len_(output_len) { |
| 30 | } |
| 31 | |
| 32 | ~ScopedOpenSSLSafeSizeBuffer() { |
| 33 | if (output_len_ < MIN_SIZE) { |
| 34 | // Copy the temporary buffer out, truncating as needed. |
| 35 | memcpy(output_, min_sized_buffer_, output_len_); |
| 36 | } |
| 37 | // else... any writing already happened directly into |output_|. |
| 38 | } |
| 39 | |
| 40 | unsigned char* safe_buffer() { |
| 41 | return output_len_ < MIN_SIZE ? min_sized_buffer_ : output_; |
| 42 | } |
| 43 | |
| 44 | private: |
[email protected] | a2c92a1c | 2012-04-03 12:32:14 | [diff] [blame] | 45 | // Pointer to the caller's data area and its associated size, where data |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 46 | // written via safe_buffer() will [eventually] end up. |
| 47 | unsigned char* output_; |
| 48 | size_t output_len_; |
| 49 | |
| 50 | // Temporary buffer writen into in the case where the caller's |
| 51 | // buffer is not of sufficient size. |
| 52 | unsigned char min_sized_buffer_[MIN_SIZE]; |
[email protected] | ac0f8be | 2010-11-12 12:03:54 | [diff] [blame] | 53 | |
| 54 | DISALLOW_COPY_AND_ASSIGN(ScopedOpenSSLSafeSizeBuffer); |
| 55 | }; |
| 56 | |
[email protected] | 31383472 | 2010-11-17 09:57:18 | [diff] [blame] | 57 | // Initialize OpenSSL if it isn't already initialized. This must be called |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 58 | // before any other OpenSSL functions though it is safe and cheap to call this |
| 59 | // multiple times. |
[email protected] | 31383472 | 2010-11-17 09:57:18 | [diff] [blame] | 60 | // This function is thread-safe, and OpenSSL will only ever be initialized once. |
| 61 | // OpenSSL will be properly shut down on program exit. |
bauerb | 01c4fff | 2016-01-23 02:18:04 | [diff] [blame] | 62 | CRYPTO_EXPORT void EnsureOpenSSLInit(); |
[email protected] | 31383472 | 2010-11-17 09:57:18 | [diff] [blame] | 63 | |
[email protected] | ac0f8be | 2010-11-12 12:03:54 | [diff] [blame] | 64 | // 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] | 65 | // are send to VLOG(1), on a release build they are disregarded. In most |
| 66 | // cases you should pass FROM_HERE as the |location|. |
Brett Wilson | 1c99002 | 2017-09-12 20:11:15 | [diff] [blame] | 67 | CRYPTO_EXPORT void ClearOpenSSLERRStack(const base::Location& location); |
[email protected] | be796bb | 2010-11-18 15:43:43 | [diff] [blame] | 68 | |
| 69 | // Place an instance of this class on the call stack to automatically clear |
| 70 | // the OpenSSL error stack on function exit. |
| 71 | class OpenSSLErrStackTracer { |
| 72 | public: |
| 73 | // Pass FROM_HERE as |location|, to help track the source of OpenSSL error |
| 74 | // messages. Note any diagnostic emitted will be tagged with the location of |
| 75 | // the constructor call as it's not possible to trace a destructor's callsite. |
Brett Wilson | 1c99002 | 2017-09-12 20:11:15 | [diff] [blame] | 76 | explicit OpenSSLErrStackTracer(const base::Location& location) |
[email protected] | be796bb | 2010-11-18 15:43:43 | [diff] [blame] | 77 | : location_(location) { |
| 78 | EnsureOpenSSLInit(); |
| 79 | } |
| 80 | ~OpenSSLErrStackTracer() { |
| 81 | ClearOpenSSLERRStack(location_); |
| 82 | } |
| 83 | |
| 84 | private: |
Brett Wilson | 1c99002 | 2017-09-12 20:11:15 | [diff] [blame] | 85 | const base::Location location_; |
[email protected] | be796bb | 2010-11-18 15:43:43 | [diff] [blame] | 86 | |
| 87 | DISALLOW_IMPLICIT_CONSTRUCTORS(OpenSSLErrStackTracer); |
| 88 | }; |
[email protected] | ac0f8be | 2010-11-12 12:03:54 | [diff] [blame] | 89 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 90 | } // namespace crypto |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 91 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 92 | #endif // CRYPTO_OPENSSL_UTIL_H_ |