Alex Vakulenko | f602473 | 2016-01-22 16:52:43 -0800 | [diff] [blame] | 1 | // Copyright (c) 2011 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 | |
| 5 | #include "crypto/openssl_util.h" |
| 6 | |
Darren Krahn | 1633416 | 2016-03-01 14:38:19 -0800 | [diff] [blame] | 7 | #if defined(OPENSSL_IS_BORINGSSL) |
Alex Vakulenko | f602473 | 2016-01-22 16:52:43 -0800 | [diff] [blame] | 8 | #include <openssl/cpu.h> |
Alex Vakulenko | 4577922 | 2016-03-17 10:36:19 -0700 | [diff] [blame] | 9 | #else |
| 10 | #include <openssl/ssl.h> |
Darren Krahn | 1633416 | 2016-03-01 14:38:19 -0800 | [diff] [blame] | 11 | #endif |
Alex Vakulenko | 4577922 | 2016-03-17 10:36:19 -0700 | [diff] [blame] | 12 | #include <openssl/crypto.h> |
Luis Hector Chavez | 94ffa55 | 2016-05-25 15:29:35 -0700 | [diff] [blame] | 13 | #include <openssl/err.h> |
Alex Vakulenko | 2485474 | 2016-01-22 16:55:13 -0800 | [diff] [blame] | 14 | #include <stddef.h> |
| 15 | #include <stdint.h> |
Alex Vakulenko | f602473 | 2016-01-22 16:52:43 -0800 | [diff] [blame] | 16 | |
Jay Civelli | 3a83cdd | 2017-03-22 17:31:44 -0700 | [diff] [blame^] | 17 | #include <string> |
| 18 | |
Alex Vakulenko | f602473 | 2016-01-22 16:52:43 -0800 | [diff] [blame] | 19 | #include "base/logging.h" |
Alex Vakulenko | f602473 | 2016-01-22 16:52:43 -0800 | [diff] [blame] | 20 | #include "base/strings/string_piece.h" |
Alex Vakulenko | f602473 | 2016-01-22 16:52:43 -0800 | [diff] [blame] | 21 | |
| 22 | namespace crypto { |
| 23 | |
| 24 | namespace { |
| 25 | |
Alex Vakulenko | f602473 | 2016-01-22 16:52:43 -0800 | [diff] [blame] | 26 | // Callback routine for OpenSSL to print error messages. |str| is a |
| 27 | // NULL-terminated string of length |len| containing diagnostic information |
| 28 | // such as the library, function and reason for the error, the file and line |
| 29 | // where the error originated, plus potentially any context-specific |
| 30 | // information about the error. |context| contains a pointer to user-supplied |
| 31 | // data, which is currently unused. |
| 32 | // If this callback returns a value <= 0, OpenSSL will stop processing the |
| 33 | // error queue and return, otherwise it will continue calling this function |
| 34 | // until all errors have been removed from the queue. |
| 35 | int OpenSSLErrorCallback(const char* str, size_t len, void* context) { |
| 36 | DVLOG(1) << "\t" << base::StringPiece(str, len); |
| 37 | return 1; |
| 38 | } |
| 39 | |
| 40 | } // namespace |
| 41 | |
| 42 | void EnsureOpenSSLInit() { |
Luis Hector Chavez | 94ffa55 | 2016-05-25 15:29:35 -0700 | [diff] [blame] | 43 | #if defined(OPENSSL_IS_BORINGSSL) |
| 44 | // CRYPTO_library_init may be safely called concurrently. |
| 45 | CRYPTO_library_init(); |
| 46 | #else |
| 47 | SSL_library_init(); |
| 48 | #endif |
Alex Vakulenko | f602473 | 2016-01-22 16:52:43 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | void ClearOpenSSLERRStack(const tracked_objects::Location& location) { |
Jay Civelli | 3a83cdd | 2017-03-22 17:31:44 -0700 | [diff] [blame^] | 52 | if (DCHECK_IS_ON() && VLOG_IS_ON(1)) { |
Alex Vakulenko | f602473 | 2016-01-22 16:52:43 -0800 | [diff] [blame] | 53 | uint32_t error_num = ERR_peek_error(); |
| 54 | if (error_num == 0) |
| 55 | return; |
| 56 | |
| 57 | std::string message; |
| 58 | location.Write(true, true, &message); |
| 59 | DVLOG(1) << "OpenSSL ERR_get_error stack from " << message; |
| 60 | ERR_print_errors_cb(&OpenSSLErrorCallback, NULL); |
| 61 | } else { |
| 62 | ERR_clear_error(); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | } // namespace crypto |