Upload upstream chromium 67.0.3396
[platform/framework/web/chromium-efl.git] / crypto / openssl_util.cc
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
7 #include <stddef.h>
8 #include <stdint.h>
9
10 #include <string>
11
12 #include "base/logging.h"
13 #include "base/strings/string_piece.h"
14 #include "third_party/boringssl/src/include/openssl/crypto.h"
15 #include "third_party/boringssl/src/include/openssl/err.h"
16
17 namespace crypto {
18
19 namespace {
20
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) {
31   DVLOG(1) << "\t" << base::StringPiece(str, len);
32   return 1;
33 }
34
35 }  // namespace
36
37 void EnsureOpenSSLInit() {
38   // CRYPTO_library_init may be safely called concurrently.
39   CRYPTO_library_init();
40 }
41
42 void ClearOpenSSLERRStack(const base::Location& location) {
43   if (DCHECK_IS_ON() && VLOG_IS_ON(1)) {
44     uint32_t error_num = ERR_peek_error();
45     if (error_num == 0)
46       return;
47
48     DVLOG(1) << "OpenSSL ERR_get_error stack from " << location.ToString();
49     ERR_print_errors_cb(&OpenSSLErrorCallback, NULL);
50   } else {
51     ERR_clear_error();
52   }
53 }
54
55 }  // namespace crypto