Aktualizr
C++ SOTA Client
All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
sslinit.cc
1 #include "sslinit.h"
2 #include <crypto/openssl_compat.h>
3 #include <openssl/ssl.h>
4 #include <pthread.h>
5 
6 #if AKTUALIZR_OPENSSL_PRE_11
7 static pthread_mutex_t *lock_cs;
8 static long *lock_count;
9 
10 // static void pthreads_locking_callback(int mode, int type, const char *, int);
11 // static unsigned long pthreads_thread_id(void);
12 
13 void pthreads_locking_callback(int mode, int type, const char *, int) {
14 #if 0
15  fprintf(stderr, "thread=%4d mode=%s lock=%s %s:%d\n",
16  CRYPTO_thread_id(),
17  (mode & CRYPTO_LOCK) ? "l" : "u",
18  (type & CRYPTO_READ) ? "r" : "w", file, line);
19 #endif
20 #if 0
21  if (CRYPTO_LOCK_SSL_CERT == type)
22  fprintf(stderr, "(t,m,f,l) %ld %d %s %d\n",
23  CRYPTO_thread_id(), mode, file, line);
24 #endif
25  if (mode & CRYPTO_LOCK) {
26  pthread_mutex_lock(&(lock_cs[type]));
27  lock_count[type]++;
28  } else {
29  pthread_mutex_unlock(&(lock_cs[type]));
30  }
31 }
32 
33 unsigned long pthreads_thread_id(void) {
34  unsigned long ret;
35 
36  ret = (unsigned long)pthread_self();
37  return (ret);
38 }
39 
40 void openssl_callbacks_setup(void) {
41  int i;
42 
43  lock_cs =
44  (pthread_mutex_t *)OPENSSL_malloc(static_cast<int>((unsigned long)CRYPTO_num_locks() * sizeof(pthread_mutex_t)));
45  lock_count = (long *)OPENSSL_malloc(static_cast<int>((unsigned long)CRYPTO_num_locks() * sizeof(long)));
46  if (!lock_cs || !lock_count) {
47  /* Nothing we can do about this...void function! */
48  if (lock_cs) OPENSSL_free(lock_cs);
49  if (lock_count) OPENSSL_free(lock_count);
50  return;
51  }
52  for (i = 0; i < CRYPTO_num_locks(); i++) {
53  lock_count[i] = 0;
54  pthread_mutex_init(&(lock_cs[i]), NULL);
55  }
56 
57  CRYPTO_set_id_callback((unsigned long (*)())pthreads_thread_id);
58  CRYPTO_set_locking_callback(pthreads_locking_callback);
59 }
60 
61 void openssl_callbacks_cleanup(void) {
62  int i;
63 
64  CRYPTO_set_locking_callback(NULL);
65  for (i = 0; i < CRYPTO_num_locks(); i++) {
66  pthread_mutex_destroy(&(lock_cs[i]));
67  }
68  OPENSSL_free(lock_cs);
69  OPENSSL_free(lock_count);
70 }
71 #else
72 void openssl_callbacks_setup(void){};
73 void openssl_callbacks_cleanup(void){};
74 #endif