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