QUIC - enable persisting of QUICServerInfo (server config) to disk
[chromium-blink-merge.git] / crypto / openssl_util.cc
blob94bf246a7cdbe3d8d02bfdae2acc70c6272d32a0
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.
5 #include "crypto/openssl_util.h"
7 #include <openssl/err.h>
8 #include <openssl/ssl.h>
9 #include <openssl/cpu.h>
11 #include "base/logging.h"
12 #include "base/memory/scoped_vector.h"
13 #include "base/memory/singleton.h"
14 #include "base/strings/string_piece.h"
15 #include "base/synchronization/lock.h"
16 #include "build/build_config.h"
18 #if defined(OS_ANDROID) && defined(ARCH_CPU_ARMEL)
19 #include <cpu-features.h>
20 #endif
22 namespace crypto {
24 namespace {
26 void CurrentThreadId(CRYPTO_THREADID* id) {
27 CRYPTO_THREADID_set_numeric(
28 id, static_cast<unsigned long>(base::PlatformThread::CurrentId()));
31 // Singleton for initializing and cleaning up the OpenSSL library.
32 class OpenSSLInitSingleton {
33 public:
34 static OpenSSLInitSingleton* GetInstance() {
35 // We allow the SSL environment to leak for multiple reasons:
36 // - it is used from a non-joinable worker thread that is not stopped on
37 // shutdown, hence may still be using OpenSSL library after the AtExit
38 // runner has completed.
39 // - There are other OpenSSL related singletons (e.g. the client socket
40 // context) who's cleanup depends on the global environment here, but
41 // we can't control the order the AtExit handlers will run in so
42 // allowing the global environment to leak at least ensures it is
43 // available for those other singletons to reliably cleanup.
44 return Singleton<OpenSSLInitSingleton,
45 LeakySingletonTraits<OpenSSLInitSingleton> >::get();
47 private:
48 friend struct DefaultSingletonTraits<OpenSSLInitSingleton>;
49 OpenSSLInitSingleton() {
50 SSL_load_error_strings();
51 SSL_library_init();
52 OpenSSL_add_all_algorithms();
53 int num_locks = CRYPTO_num_locks();
54 locks_.reserve(num_locks);
55 for (int i = 0; i < num_locks; ++i)
56 locks_.push_back(new base::Lock());
57 CRYPTO_set_locking_callback(LockingCallback);
58 CRYPTO_THREADID_set_callback(CurrentThreadId);
60 #if defined(OS_ANDROID) && defined(ARCH_CPU_ARMEL)
61 const bool has_neon =
62 (android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_NEON) != 0;
63 if (has_neon)
64 CRYPTO_set_NEON_capable(1);
65 #endif
68 ~OpenSSLInitSingleton() {
69 CRYPTO_set_locking_callback(NULL);
70 EVP_cleanup();
71 ERR_free_strings();
74 static void LockingCallback(int mode, int n, const char* file, int line) {
75 OpenSSLInitSingleton::GetInstance()->OnLockingCallback(mode, n, file, line);
78 void OnLockingCallback(int mode, int n, const char* file, int line) {
79 CHECK_LT(static_cast<size_t>(n), locks_.size());
80 if (mode & CRYPTO_LOCK)
81 locks_[n]->Acquire();
82 else
83 locks_[n]->Release();
86 // These locks are used and managed by OpenSSL via LockingCallback().
87 ScopedVector<base::Lock> locks_;
89 DISALLOW_COPY_AND_ASSIGN(OpenSSLInitSingleton);
92 // Callback routine for OpenSSL to print error messages. |str| is a
93 // NULL-terminated string of length |len| containing diagnostic information
94 // such as the library, function and reason for the error, the file and line
95 // where the error originated, plus potentially any context-specific
96 // information about the error. |context| contains a pointer to user-supplied
97 // data, which is currently unused.
98 // If this callback returns a value <= 0, OpenSSL will stop processing the
99 // error queue and return, otherwise it will continue calling this function
100 // until all errors have been removed from the queue.
101 int OpenSSLErrorCallback(const char* str, size_t len, void* context) {
102 DVLOG(1) << "\t" << base::StringPiece(str, len);
103 return 1;
106 } // namespace
108 void EnsureOpenSSLInit() {
109 (void)OpenSSLInitSingleton::GetInstance();
112 void ClearOpenSSLERRStack(const tracked_objects::Location& location) {
113 if (logging::DEBUG_MODE && VLOG_IS_ON(1)) {
114 int error_num = ERR_peek_error();
115 if (error_num == 0)
116 return;
118 std::string message;
119 location.Write(true, true, &message);
120 DVLOG(1) << "OpenSSL ERR_get_error stack from " << message;
121 ERR_print_errors_cb(&OpenSSLErrorCallback, NULL);
122 } else {
123 ERR_clear_error();
127 } // namespace crypto