[M120 Migration][MM] Fix EME AD insert issue
[platform/framework/web/chromium-efl.git] / crypto / openssl_util.cc
1 // Copyright 2011 The Chromium Authors
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_view>
11
12 #include "base/logging.h"
13 #include "third_party/boringssl/src/include/openssl/crypto.h"
14 #include "third_party/boringssl/src/include/openssl/err.h"
15
16 namespace crypto {
17
18 namespace {
19
20 // Callback routine for OpenSSL to print error messages. |str| is a
21 // NULL-terminated string of length |len| containing diagnostic information
22 // such as the library, function and reason for the error, the file and line
23 // where the error originated, plus potentially any context-specific
24 // information about the error. |context| contains a pointer to user-supplied
25 // data, which is currently unused.
26 // If this callback returns a value <= 0, OpenSSL will stop processing the
27 // error queue and return, otherwise it will continue calling this function
28 // until all errors have been removed from the queue.
29 int OpenSSLErrorCallback(const char* str, size_t len, void* context) {
30   DVLOG(1) << "\t" << std::string_view(str, len);
31   return 1;
32 }
33
34 }  // namespace
35
36 void EnsureOpenSSLInit() {
37   // CRYPTO_library_init may be safely called concurrently.
38   CRYPTO_library_init();
39 }
40
41 void ClearOpenSSLERRStack(const base::Location& location) {
42   if (DCHECK_IS_ON() && VLOG_IS_ON(1)) {
43     uint32_t error_num = ERR_peek_error();
44     if (error_num == 0)
45       return;
46
47     DVLOG(1) << "OpenSSL ERR_get_error stack from " << location.ToString();
48     ERR_print_errors_cb(&OpenSSLErrorCallback, NULL);
49   } else {
50     ERR_clear_error();
51   }
52 }
53
54 }  // namespace crypto