aboutsummaryrefslogtreecommitdiff
path: root/crypto/openssl_init.cc
blob: e06a2a8f5c3f3804c1b7d7aa529dec939495b175 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
 * Copyright 2019 Google Inc.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "crypto/openssl_init.h"

#if !defined(OPENSSL_IS_BORINGSSL)
#include <pthread.h>

#include <mutex>  // NOLINT(build/c++11): only using std::call_once, not mutex.

#include "glog/logging.h"
#include "crypto/openssl.inc"
#endif

namespace private_join_and_compute {
#if !defined(OPENSSL_IS_BORINGSSL)
namespace {

void CryptoNewThreadID(CRYPTO_THREADID* tid);
void CryptoLockingCallback(int mode, int n, const char* file, int line);

class OpenSSLInit {
 public:
  OpenSSLInit() : mutexes(CRYPTO_num_locks()) {
    for (int i = 0; i < CRYPTO_num_locks(); ++i) {
      pthread_mutex_init(&(mutexes[i]), nullptr);
    }
  }

  void LoadErrStrings() {
    ERR_load_BN_strings();
    ERR_load_BUF_strings();
    ERR_load_CRYPTO_strings();
    ERR_load_EC_strings();
    ERR_load_ERR_strings();
    ERR_load_EVP_strings();
    ERR_load_RAND_strings();
  }

  void InitLocking() {
    CRYPTO_THREADID_set_callback(CryptoNewThreadID);
    CRYPTO_set_locking_callback(CryptoLockingCallback);
  }

  ~OpenSSLInit() {
    CRYPTO_set_locking_callback(nullptr);
    for (int i = 0; i < CRYPTO_num_locks(); ++i) {
      pthread_mutex_destroy(&(mutexes[i]));
    }
    ERR_free_strings();
  }

  std::vector<pthread_mutex_t> mutexes;
};

static std::once_flag init_flag;
static OpenSSLInit openssl_init;

void CryptoNewThreadID(CRYPTO_THREADID* tid) {
#if defined(OS_NACL)  // pthread_t is a pointer type in native client.
    CRYPTO_THREADID_set_pointer(tid, pthread_self());
#else
    CRYPTO_THREADID_set_numeric(tid, static_cast<uint64_t>(pthread_self()));
#endif
}

// See crypto/threads/mmtest.c for usage in OpenSSL library.
void CryptoLockingCallback(int mode, int n, const char* file, int line) {
  CHECK_GE(n, 0);
  pthread_mutex_t* mutex = &(openssl_init.mutexes[n]);
  if (mode & CRYPTO_LOCK) {
    pthread_mutex_lock(mutex);
  } else {
    pthread_mutex_unlock(mutex);
  }
}

static void OpenSSLInitHelper() {
  openssl_init.LoadErrStrings();
  openssl_init.InitLocking();
}

}  // namespace
#endif

void OpenSSLInit() {
#if !defined(OPENSSL_IS_BORINGSSL)
  std::call_once(init_flag, OpenSSLInitHelper);
#endif
}

}  // namespace private_join_and_compute