1*6777b538SAndroid Build Coastguard Worker // Copyright 2011 The Chromium Authors
2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file.
4*6777b538SAndroid Build Coastguard Worker
5*6777b538SAndroid Build Coastguard Worker #include "crypto/openssl_util.h"
6*6777b538SAndroid Build Coastguard Worker
7*6777b538SAndroid Build Coastguard Worker #include <stddef.h>
8*6777b538SAndroid Build Coastguard Worker #include <stdint.h>
9*6777b538SAndroid Build Coastguard Worker
10*6777b538SAndroid Build Coastguard Worker #include <string_view>
11*6777b538SAndroid Build Coastguard Worker
12*6777b538SAndroid Build Coastguard Worker #include "base/logging.h"
13*6777b538SAndroid Build Coastguard Worker #include "third_party/boringssl/src/include/openssl/crypto.h"
14*6777b538SAndroid Build Coastguard Worker #include "third_party/boringssl/src/include/openssl/err.h"
15*6777b538SAndroid Build Coastguard Worker
16*6777b538SAndroid Build Coastguard Worker namespace crypto {
17*6777b538SAndroid Build Coastguard Worker
18*6777b538SAndroid Build Coastguard Worker namespace {
19*6777b538SAndroid Build Coastguard Worker
20*6777b538SAndroid Build Coastguard Worker // Callback routine for OpenSSL to print error messages. |str| is a
21*6777b538SAndroid Build Coastguard Worker // NULL-terminated string of length |len| containing diagnostic information
22*6777b538SAndroid Build Coastguard Worker // such as the library, function and reason for the error, the file and line
23*6777b538SAndroid Build Coastguard Worker // where the error originated, plus potentially any context-specific
24*6777b538SAndroid Build Coastguard Worker // information about the error. |context| contains a pointer to user-supplied
25*6777b538SAndroid Build Coastguard Worker // data, which is currently unused.
26*6777b538SAndroid Build Coastguard Worker // If this callback returns a value <= 0, OpenSSL will stop processing the
27*6777b538SAndroid Build Coastguard Worker // error queue and return, otherwise it will continue calling this function
28*6777b538SAndroid Build Coastguard Worker // until all errors have been removed from the queue.
OpenSSLErrorCallback(const char * str,size_t len,void * context)29*6777b538SAndroid Build Coastguard Worker int OpenSSLErrorCallback(const char* str, size_t len, void* context) {
30*6777b538SAndroid Build Coastguard Worker DVLOG(1) << "\t" << std::string_view(str, len);
31*6777b538SAndroid Build Coastguard Worker return 1;
32*6777b538SAndroid Build Coastguard Worker }
33*6777b538SAndroid Build Coastguard Worker
34*6777b538SAndroid Build Coastguard Worker } // namespace
35*6777b538SAndroid Build Coastguard Worker
EnsureOpenSSLInit()36*6777b538SAndroid Build Coastguard Worker void EnsureOpenSSLInit() {
37*6777b538SAndroid Build Coastguard Worker // CRYPTO_library_init may be safely called concurrently.
38*6777b538SAndroid Build Coastguard Worker CRYPTO_library_init();
39*6777b538SAndroid Build Coastguard Worker }
40*6777b538SAndroid Build Coastguard Worker
ClearOpenSSLERRStack(const base::Location & location)41*6777b538SAndroid Build Coastguard Worker void ClearOpenSSLERRStack(const base::Location& location) {
42*6777b538SAndroid Build Coastguard Worker if (DCHECK_IS_ON() && VLOG_IS_ON(1)) {
43*6777b538SAndroid Build Coastguard Worker uint32_t error_num = ERR_peek_error();
44*6777b538SAndroid Build Coastguard Worker if (error_num == 0)
45*6777b538SAndroid Build Coastguard Worker return;
46*6777b538SAndroid Build Coastguard Worker
47*6777b538SAndroid Build Coastguard Worker DVLOG(1) << "OpenSSL ERR_get_error stack from " << location.ToString();
48*6777b538SAndroid Build Coastguard Worker ERR_print_errors_cb(&OpenSSLErrorCallback, NULL);
49*6777b538SAndroid Build Coastguard Worker } else {
50*6777b538SAndroid Build Coastguard Worker ERR_clear_error();
51*6777b538SAndroid Build Coastguard Worker }
52*6777b538SAndroid Build Coastguard Worker }
53*6777b538SAndroid Build Coastguard Worker
54*6777b538SAndroid Build Coastguard Worker } // namespace crypto
55