1 // Copyright 2022 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 "quiche/common/quiche_crypto_logging.h"
6
7 #include <cstdint>
8 #include <string>
9
10 #include "absl/base/macros.h"
11 #include "absl/status/status.h"
12 #include "absl/strings/str_cat.h"
13 #include "absl/strings/string_view.h"
14 #include "openssl/err.h"
15 #include "quiche/common/platform/api/quiche_logging.h"
16
17 namespace quiche {
DLogOpenSslErrors()18 void DLogOpenSslErrors() {
19 #ifdef NDEBUG
20 // Clear OpenSSL error stack.
21 ClearOpenSslErrors();
22 #else
23 while (uint32_t error = ERR_get_error()) {
24 char buf[120];
25 ERR_error_string_n(error, buf, ABSL_ARRAYSIZE(buf));
26 QUICHE_DLOG(ERROR) << "OpenSSL error: " << buf;
27 }
28 #endif
29 }
30
ClearOpenSslErrors()31 void ClearOpenSslErrors() {
32 while (ERR_get_error()) {
33 }
34 }
35
SslErrorAsStatus(absl::string_view msg,absl::StatusCode code)36 absl::Status SslErrorAsStatus(absl::string_view msg, absl::StatusCode code) {
37 std::string message;
38 absl::StrAppend(&message, msg, "OpenSSL error: ");
39 while (uint32_t error = ERR_get_error()) {
40 char buf[120];
41 ERR_error_string_n(error, buf, ABSL_ARRAYSIZE(buf));
42 absl::StrAppend(&message, buf);
43 }
44 return absl::Status(code, message);
45 }
46
47 } // namespace quiche
48