openssl_util.cc 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. #include "crypto/openssl_util.h"
  5. #include <stddef.h>
  6. #include <stdint.h>
  7. #include <string>
  8. #include "base/logging.h"
  9. #include "base/strings/string_piece.h"
  10. #include "third_party/boringssl/src/include/openssl/crypto.h"
  11. #include "third_party/boringssl/src/include/openssl/err.h"
  12. namespace crypto {
  13. namespace {
  14. // Callback routine for OpenSSL to print error messages. |str| is a
  15. // NULL-terminated string of length |len| containing diagnostic information
  16. // such as the library, function and reason for the error, the file and line
  17. // where the error originated, plus potentially any context-specific
  18. // information about the error. |context| contains a pointer to user-supplied
  19. // data, which is currently unused.
  20. // If this callback returns a value <= 0, OpenSSL will stop processing the
  21. // error queue and return, otherwise it will continue calling this function
  22. // until all errors have been removed from the queue.
  23. int OpenSSLErrorCallback(const char* str, size_t len, void* context) {
  24. DVLOG(1) << "\t" << base::StringPiece(str, len);
  25. return 1;
  26. }
  27. } // namespace
  28. void EnsureOpenSSLInit() {
  29. // CRYPTO_library_init may be safely called concurrently.
  30. CRYPTO_library_init();
  31. }
  32. void ClearOpenSSLERRStack(const base::Location& location) {
  33. if (DCHECK_IS_ON() && VLOG_IS_ON(1)) {
  34. uint32_t error_num = ERR_peek_error();
  35. if (error_num == 0)
  36. return;
  37. DVLOG(1) << "OpenSSL ERR_get_error stack from " << location.ToString();
  38. ERR_print_errors_cb(&OpenSSLErrorCallback, NULL);
  39. } else {
  40. ERR_clear_error();
  41. }
  42. }
  43. } // namespace crypto