openssl_util.h 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright (c) 2012 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. #ifndef CRYPTO_OPENSSL_UTIL_H_
  5. #define CRYPTO_OPENSSL_UTIL_H_
  6. #include <stddef.h>
  7. #include <string.h>
  8. #include "base/location.h"
  9. #include "base/memory/raw_ptr.h"
  10. #include "crypto/crypto_export.h"
  11. namespace crypto {
  12. // Provides a buffer of at least MIN_SIZE bytes, for use when calling OpenSSL's
  13. // SHA256, HMAC, etc functions, adapting the buffer sizing rules to meet those
  14. // of our base wrapper APIs.
  15. // This allows the library to write directly to the caller's buffer if it is of
  16. // sufficient size, but if not it will write to temporary |min_sized_buffer_|
  17. // of required size and then its content is automatically copied out on
  18. // destruction, with truncation as appropriate.
  19. template<int MIN_SIZE>
  20. class ScopedOpenSSLSafeSizeBuffer {
  21. public:
  22. ScopedOpenSSLSafeSizeBuffer(unsigned char* output, size_t output_len)
  23. : output_(output),
  24. output_len_(output_len) {
  25. }
  26. ScopedOpenSSLSafeSizeBuffer(const ScopedOpenSSLSafeSizeBuffer&) = delete;
  27. ScopedOpenSSLSafeSizeBuffer& operator=(const ScopedOpenSSLSafeSizeBuffer&) =
  28. delete;
  29. ~ScopedOpenSSLSafeSizeBuffer() {
  30. if (output_len_ < MIN_SIZE) {
  31. // Copy the temporary buffer out, truncating as needed.
  32. memcpy(output_, min_sized_buffer_, output_len_);
  33. }
  34. // else... any writing already happened directly into |output_|.
  35. }
  36. unsigned char* safe_buffer() {
  37. return output_len_ < MIN_SIZE ? min_sized_buffer_ : output_.get();
  38. }
  39. private:
  40. // Pointer to the caller's data area and its associated size, where data
  41. // written via safe_buffer() will [eventually] end up.
  42. raw_ptr<unsigned char> output_;
  43. size_t output_len_;
  44. // Temporary buffer writen into in the case where the caller's
  45. // buffer is not of sufficient size.
  46. unsigned char min_sized_buffer_[MIN_SIZE];
  47. };
  48. // Initialize OpenSSL if it isn't already initialized. This must be called
  49. // before any other OpenSSL functions though it is safe and cheap to call this
  50. // multiple times.
  51. // This function is thread-safe, and OpenSSL will only ever be initialized once.
  52. // OpenSSL will be properly shut down on program exit.
  53. CRYPTO_EXPORT void EnsureOpenSSLInit();
  54. // 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. In most
  56. // cases you should pass FROM_HERE as the |location|.
  57. CRYPTO_EXPORT void ClearOpenSSLERRStack(const base::Location& location);
  58. // Place an instance of this class on the call stack to automatically clear
  59. // the OpenSSL error stack on function exit.
  60. class OpenSSLErrStackTracer {
  61. public:
  62. OpenSSLErrStackTracer() = delete;
  63. // Pass FROM_HERE as |location|, to help track the source of OpenSSL error
  64. // messages. Note any diagnostic emitted will be tagged with the location of
  65. // the constructor call as it's not possible to trace a destructor's callsite.
  66. explicit OpenSSLErrStackTracer(const base::Location& location)
  67. : location_(location) {
  68. EnsureOpenSSLInit();
  69. }
  70. OpenSSLErrStackTracer(const OpenSSLErrStackTracer&) = delete;
  71. OpenSSLErrStackTracer& operator=(const OpenSSLErrStackTracer&) = delete;
  72. ~OpenSSLErrStackTracer() {
  73. ClearOpenSSLERRStack(location_);
  74. }
  75. private:
  76. const base::Location location_;
  77. };
  78. } // namespace crypto
  79. #endif // CRYPTO_OPENSSL_UTIL_H_