// Copyright 2019 The Chromium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef UTIL_CRYPTO_OPENSSL_UTIL_H_ #define UTIL_CRYPTO_OPENSSL_UTIL_H_ #include #include #include "platform/base/error.h" #include "platform/base/location.h" // LOCAL PATCH (breadcast): dropped SSLErrorCodeToError()/GetSSLError(), // which format an SSL_get_error() result and reference BoringSSL's // SSL_error_description() (not part of system OpenSSL's public API). Unused // here -- the TLS CASTV2 control channel is handled by the existing // rust_cast-based Rust code, not this vendored openscreen subset, which only // needs the general ERR_*/AES pieces. See vendor/openscreen/PATCHES.md. namespace openscreen { // Initialize OpenSSL if it isn't already initialized. This must be called // before any other OpenSSL functions though it is safe and cheap to call this // multiple times. // This function is thread-safe, and OpenSSL will only ever be initialized once. // OpenSSL will be properly shut down on program exit. // Multiple sequential calls to EnsureOpenSSLInit or EnsureOpenSSLCleanup are // ignored by OpenSSL itself. void EnsureOpenSSLInit(); // Drains the OpenSSL ERR_get_error stack. On a debug build the error codes // are send to VLOG(1), on a release build they are disregarded. In most // cases you should pass CURRENT_LOCATION as the `location`. void ClearOpenSSLERRStack(const Location& location); // Place an instance of this class on the call stack to automatically clear // the OpenSSL error stack on function exit. class OpenSSLErrStackTracer { public: // Pass CURRENT_LOCATION as `location`, to help track the source of OpenSSL // error messages. Note any diagnostic emitted will be tagged with the // location of the constructor call as it's not possible to trace a // destructor's callsite. explicit OpenSSLErrStackTracer(const Location& location) : location_(location) { EnsureOpenSSLInit(); } OpenSSLErrStackTracer(const OpenSSLErrStackTracer&) = delete; OpenSSLErrStackTracer(OpenSSLErrStackTracer&&) noexcept = delete; OpenSSLErrStackTracer& operator=(const OpenSSLErrStackTracer&) = delete; OpenSSLErrStackTracer& operator=(OpenSSLErrStackTracer&&) = delete; ~OpenSSLErrStackTracer() { ClearOpenSSLERRStack(location_); } private: const Location location_; }; } // namespace openscreen #endif // UTIL_CRYPTO_OPENSSL_UTIL_H_