blob: 65be615a50519dbaf317746bc76a2faa1110cb5b [file] [log] [blame]
[email protected]3b63f8f42011-03-28 01:54:151// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]ac0f8be2010-11-12 12:03:542// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]4b559b4d2011-04-14 17:37:145#include "crypto/openssl_util.h"
[email protected]ac0f8be2010-11-12 12:03:546
avidd373b8b2015-12-21 21:34:437#include <stddef.h>
8#include <stdint.h>
[email protected]ac0f8be2010-11-12 12:03:549
davidben6004dc52017-02-03 04:15:2910#include <string>
11
[email protected]ac0f8be2010-11-12 12:03:5412#include "base/logging.h"
[email protected]daf079a2013-04-17 21:42:4013#include "base/strings/string_piece.h"
tfarina29a3a1742016-10-28 18:47:3314#include "third_party/boringssl/src/include/openssl/crypto.h"
15#include "third_party/boringssl/src/include/openssl/err.h"
[email protected]ac0f8be2010-11-12 12:03:5416
[email protected]4b559b4d2011-04-14 17:37:1417namespace crypto {
[email protected]ac0f8be2010-11-12 12:03:5418
[email protected]313834722010-11-17 09:57:1819namespace {
20
[email protected]84c498122010-12-12 22:30:5021// 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.
30int OpenSSLErrorCallback(const char* str, size_t len, void* context) {
[email protected]dba10cbe2011-04-15 13:09:2631 DVLOG(1) << "\t" << base::StringPiece(str, len);
[email protected]84c498122010-12-12 22:30:5032 return 1;
33}
34
[email protected]313834722010-11-17 09:57:1835} // namespace
36
37void EnsureOpenSSLInit() {
davidben689d7132016-03-29 15:13:2038 // CRYPTO_library_init may be safely called concurrently.
39 CRYPTO_library_init();
[email protected]313834722010-11-17 09:57:1840}
41
[email protected]be796bb2010-11-18 15:43:4342void ClearOpenSSLERRStack(const tracked_objects::Location& location) {
wez8ccfd32a2017-03-13 22:54:2743 if (DCHECK_IS_ON() && VLOG_IS_ON(1)) {
eroman46f7dd82015-03-24 01:40:0444 uint32_t error_num = ERR_peek_error();
[email protected]ac0f8be2010-11-12 12:03:5445 if (error_num == 0)
46 return;
47
[email protected]be796bb2010-11-18 15:43:4348 std::string message;
49 location.Write(true, true, &message);
50 DVLOG(1) << "OpenSSL ERR_get_error stack from " << message;
[email protected]84c498122010-12-12 22:30:5051 ERR_print_errors_cb(&OpenSSLErrorCallback, NULL);
[email protected]ac0f8be2010-11-12 12:03:5452 } else {
53 ERR_clear_error();
54 }
55}
56
[email protected]4b559b4d2011-04-14 17:37:1457} // namespace crypto