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