[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | ac0f8be | 2010-11-12 12:03:54 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 5 | #include "crypto/openssl_util.h" |
[email protected] | ac0f8be | 2010-11-12 12:03:54 | [diff] [blame] | 6 | |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
[email protected] | ac0f8be | 2010-11-12 12:03:54 | [diff] [blame] | 9 | |
davidben | 6004dc5 | 2017-02-03 04:15:29 | [diff] [blame] | 10 | #include <string> |
| 11 | |
[email protected] | ac0f8be | 2010-11-12 12:03:54 | [diff] [blame] | 12 | #include "base/logging.h" |
[email protected] | daf079a | 2013-04-17 21:42:40 | [diff] [blame] | 13 | #include "base/strings/string_piece.h" |
tfarina | 29a3a174 | 2016-10-28 18:47:33 | [diff] [blame] | 14 | #include "third_party/boringssl/src/include/openssl/crypto.h" |
| 15 | #include "third_party/boringssl/src/include/openssl/err.h" |
[email protected] | ac0f8be | 2010-11-12 12:03:54 | [diff] [blame] | 16 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 17 | namespace crypto { |
[email protected] | ac0f8be | 2010-11-12 12:03:54 | [diff] [blame] | 18 | |
[email protected] | 31383472 | 2010-11-17 09:57:18 | [diff] [blame] | 19 | namespace { |
| 20 | |
[email protected] | 84c49812 | 2010-12-12 22:30:50 | [diff] [blame] | 21 | // Callback routine for OpenSSL to print error messages. |str| is a |
| 22 | // NULL-terminated string of length |len| containing diagnostic information |
| 23 | // such as the library, function and reason for the error, the file and line |
| 24 | // where the error originated, plus potentially any context-specific |
| 25 | // information about the error. |context| contains a pointer to user-supplied |
| 26 | // data, which is currently unused. |
| 27 | // If this callback returns a value <= 0, OpenSSL will stop processing the |
| 28 | // error queue and return, otherwise it will continue calling this function |
| 29 | // until all errors have been removed from the queue. |
| 30 | int OpenSSLErrorCallback(const char* str, size_t len, void* context) { |
[email protected] | dba10cbe | 2011-04-15 13:09:26 | [diff] [blame] | 31 | DVLOG(1) << "\t" << base::StringPiece(str, len); |
[email protected] | 84c49812 | 2010-12-12 22:30:50 | [diff] [blame] | 32 | return 1; |
| 33 | } |
| 34 | |
[email protected] | 31383472 | 2010-11-17 09:57:18 | [diff] [blame] | 35 | } // namespace |
| 36 | |
| 37 | void EnsureOpenSSLInit() { |
davidben | 689d713 | 2016-03-29 15:13:20 | [diff] [blame] | 38 | // CRYPTO_library_init may be safely called concurrently. |
| 39 | CRYPTO_library_init(); |
[email protected] | 31383472 | 2010-11-17 09:57:18 | [diff] [blame] | 40 | } |
| 41 | |
[email protected] | be796bb | 2010-11-18 15:43:43 | [diff] [blame] | 42 | void ClearOpenSSLERRStack(const tracked_objects::Location& location) { |
wez | 8ccfd32a | 2017-03-13 22:54:27 | [diff] [blame] | 43 | if (DCHECK_IS_ON() && VLOG_IS_ON(1)) { |
eroman | 46f7dd8 | 2015-03-24 01:40:04 | [diff] [blame] | 44 | uint32_t error_num = ERR_peek_error(); |
[email protected] | ac0f8be | 2010-11-12 12:03:54 | [diff] [blame] | 45 | if (error_num == 0) |
| 46 | return; |
| 47 | |
[email protected] | be796bb | 2010-11-18 15:43:43 | [diff] [blame] | 48 | std::string message; |
| 49 | location.Write(true, true, &message); |
| 50 | DVLOG(1) << "OpenSSL ERR_get_error stack from " << message; |
[email protected] | 84c49812 | 2010-12-12 22:30:50 | [diff] [blame] | 51 | ERR_print_errors_cb(&OpenSSLErrorCallback, NULL); |
[email protected] | ac0f8be | 2010-11-12 12:03:54 | [diff] [blame] | 52 | } else { |
| 53 | ERR_clear_error(); |
| 54 | } |
| 55 | } |
| 56 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 57 | } // namespace crypto |