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