BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file BSecurity.c |
||
3 | * @author Ambroz Bizjak <ambrop7@gmail.com> |
||
4 | * |
||
5 | * @section LICENSE |
||
6 | * |
||
7 | * Redistribution and use in source and binary forms, with or without |
||
8 | * modification, are permitted provided that the following conditions are met: |
||
9 | * 1. Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * 2. Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
13 | * documentation and/or other materials provided with the distribution. |
||
14 | * 3. Neither the name of the author nor the |
||
15 | * names of its contributors may be used to endorse or promote products |
||
16 | * derived from this software without specific prior written permission. |
||
17 | * |
||
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
||
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
||
20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
||
21 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
||
22 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
||
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||
24 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
||
25 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||
27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
28 | */ |
||
29 | |||
30 | #include <stddef.h> |
||
31 | |||
32 | #ifdef BADVPN_THREADWORK_USE_PTHREAD |
||
33 | #include <pthread.h> |
||
34 | #endif |
||
35 | |||
36 | #include <openssl/crypto.h> |
||
37 | |||
38 | #include <misc/debug.h> |
||
39 | #include <misc/balloc.h> |
||
40 | |||
41 | #include <security/BSecurity.h> |
||
42 | |||
43 | int bsecurity_initialized = 0; |
||
44 | |||
45 | #ifdef BADVPN_THREADWORK_USE_PTHREAD |
||
46 | pthread_mutex_t *bsecurity_locks; |
||
47 | int bsecurity_num_locks; |
||
48 | #endif |
||
49 | |||
50 | #ifdef BADVPN_THREADWORK_USE_PTHREAD |
||
51 | |||
52 | static unsigned long id_callback (void) |
||
53 | { |
||
54 | ASSERT(bsecurity_initialized) |
||
55 | |||
56 | return (unsigned long)pthread_self(); |
||
57 | } |
||
58 | |||
59 | static void locking_callback (int mode, int type, const char *file, int line) |
||
60 | { |
||
61 | ASSERT(bsecurity_initialized) |
||
62 | ASSERT(type >= 0) |
||
63 | ASSERT(type < bsecurity_num_locks) |
||
64 | |||
65 | if ((mode & CRYPTO_LOCK)) { |
||
66 | ASSERT_FORCE(pthread_mutex_lock(&bsecurity_locks[type]) == 0) |
||
67 | } else { |
||
68 | ASSERT_FORCE(pthread_mutex_unlock(&bsecurity_locks[type]) == 0) |
||
69 | } |
||
70 | } |
||
71 | |||
72 | #endif |
||
73 | |||
74 | int BSecurity_GlobalInitThreadSafe (void) |
||
75 | { |
||
76 | ASSERT(!bsecurity_initialized) |
||
77 | |||
78 | #ifdef BADVPN_THREADWORK_USE_PTHREAD |
||
79 | |||
80 | // get number of locks |
||
81 | int num_locks = CRYPTO_num_locks(); |
||
82 | ASSERT_FORCE(num_locks >= 0) |
||
83 | |||
84 | // alloc locks array |
||
85 | if (!(bsecurity_locks = BAllocArray(num_locks, sizeof(bsecurity_locks[0])))) { |
||
86 | goto fail0; |
||
87 | } |
||
88 | |||
89 | // init locks |
||
90 | bsecurity_num_locks = 0; |
||
91 | for (int i = 0; i < num_locks; i++) { |
||
92 | if (pthread_mutex_init(&bsecurity_locks[i], NULL) != 0) { |
||
93 | goto fail1; |
||
94 | } |
||
95 | bsecurity_num_locks++; |
||
96 | } |
||
97 | |||
98 | #endif |
||
99 | |||
100 | bsecurity_initialized = 1; |
||
101 | |||
102 | #ifdef BADVPN_THREADWORK_USE_PTHREAD |
||
103 | CRYPTO_set_id_callback(id_callback); |
||
104 | CRYPTO_set_locking_callback(locking_callback); |
||
105 | #endif |
||
106 | |||
107 | return 1; |
||
108 | |||
109 | #ifdef BADVPN_THREADWORK_USE_PTHREAD |
||
110 | fail1: |
||
111 | while (bsecurity_num_locks > 0) { |
||
112 | ASSERT_FORCE(pthread_mutex_destroy(&bsecurity_locks[bsecurity_num_locks - 1]) == 0) |
||
113 | bsecurity_num_locks--; |
||
114 | } |
||
115 | BFree(bsecurity_locks); |
||
116 | fail0: |
||
117 | return 0; |
||
118 | #endif |
||
119 | } |
||
120 | |||
121 | void BSecurity_GlobalFreeThreadSafe (void) |
||
122 | { |
||
123 | ASSERT(bsecurity_initialized) |
||
124 | |||
125 | #ifdef BADVPN_THREADWORK_USE_PTHREAD |
||
126 | |||
127 | // remove callbacks |
||
128 | CRYPTO_set_locking_callback(NULL); |
||
129 | CRYPTO_set_id_callback(NULL); |
||
130 | |||
131 | // free locks |
||
132 | while (bsecurity_num_locks > 0) { |
||
133 | ASSERT_FORCE(pthread_mutex_destroy(&bsecurity_locks[bsecurity_num_locks - 1]) == 0) |
||
134 | bsecurity_num_locks--; |
||
135 | } |
||
136 | |||
137 | // free locks array |
||
138 | BFree(bsecurity_locks); |
||
139 | |||
140 | #endif |
||
141 | |||
142 | bsecurity_initialized = 0; |
||
143 | } |
||
144 | |||
145 | void BSecurity_GlobalAssertThreadSafe (int thread_safe) |
||
146 | { |
||
147 | ASSERT(thread_safe == 0 || thread_safe == 1) |
||
148 | ASSERT(!(thread_safe) || bsecurity_initialized) |
||
149 | } |