forked from bumptech/stud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshctx.c
320 lines (245 loc) · 7.16 KB
/
shctx.c
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
* shctx.c
*
* Copyright (C) 2011 EXCELIANCE
*
* Author: Emeric Brun - [email protected]
*
*/
#include <sys/mman.h>
#ifdef USE_SYSCALL_FUTEX
#include <unistd.h>
#include <linux/futex.h>
#include <sys/syscall.h>
#else /* USE_SYSCALL_FUTEX */
#include <pthread.h>
#endif /* USE_SYSCALL_FUTEX */
#include "ebtree/ebmbtree.h"
#include "shctx.h"
#ifndef SHSESS_MAX_DATA_LEN
#define SHSESS_MAX_DATA_LEN 512
#endif
struct shared_session {
struct ebmb_node key;
unsigned char key_data[SSL_MAX_SSL_SESSION_ID_LENGTH];
int data_len;
unsigned char data[SHSESS_MAX_DATA_LEN];
struct shared_session *p;
struct shared_session *n;
};
struct shared_context {
#ifdef USE_SYSCALL_FUTEX
unsigned int waiters;
#else /* USE_SYSCALL_FUTEX */
pthread_mutex_t mutex;
#endif
struct shared_session head;
};
/* Static shared context */
static struct shared_context *shctx = NULL;
/* Lock functions */
#ifdef USE_SYSCALL_FUTEX
static inline unsigned int xchg(unsigned int *ptr, unsigned int x)
{
__asm volatile("lock xchgl %0,%1"
: "=r" (x), "+m" (*ptr)
: "0" (x)
: "memory");
return x;
}
static inline unsigned int cmpxchg(unsigned int *ptr, unsigned int old, unsigned int new)
{
unsigned int ret;
__asm volatile("lock cmpxchgl %2,%1"
: "=a" (ret), "+m" (*ptr)
: "r" (new), "0" (old)
: "memory");
return ret;
}
static inline unsigned char atomic_inc(unsigned int *ptr)
{
unsigned char ret;
__asm volatile("lock incl %0\n"
"setne %1\n"
: "+m" (*ptr), "=qm" (ret)
:
: "memory");
return ret;
}
static inline unsigned char atomic_dec(unsigned int *ptr)
{
unsigned char ret;
__asm volatile("lock decl %0\n"
"setne %1\n"
: "+m" (*ptr), "=qm" (ret)
:
: "memory");
return ret;
}
static inline void shared_context_lock(void)
{
unsigned int x;
x = cmpxchg(&shctx->waiters, 0, 1);
if (x) {
if (x != 2)
x = xchg(&shctx->waiters, 2);
while (x) {
syscall(SYS_futex, &shctx->waiters, FUTEX_WAIT, 2, NULL, 0, 0);
x = xchg(&shctx->waiters, 2);
}
}
}
static inline void shared_context_unlock(void)
{
if (atomic_dec(&shctx->waiters)) {
shctx->waiters = 0;
syscall(SYS_futex, &shctx->waiters, FUTEX_WAKE, 1, NULL, 0, 0);
}
}
#else /* USE_SYSCALL_FUTEX */
#define shared_context_lock(v) pthread_mutex_lock(&shctx->mutex)
#define shared_context_unlock(v) pthread_mutex_unlock(&shctx->mutex)
#endif
/* List Macros */
#define shared_session_remove(a) (a)->n->p = (a)->p; \
(a)->p->n = (a)->n;
#define shared_session_movelast(a) shared_session_remove(a) \
(a)->n = &shctx->head; \
(a)->p = shctx->head.p; \
shctx->head.p->n = a; \
shctx->head.p = a;
#define shared_session_movefirst(a) shared_session_remove(a) \
(a)->p = &shctx->head; \
(a)->n = shctx->head.n; \
shctx->head.n->p = a; \
shctx->head.n = a;
/* SSL context callbacks */
int shctx_new_cb(SSL *ssl, SSL_SESSION *sess) {
(void)ssl;
struct shared_session *retshs;
struct shared_session *lastshs;
unsigned char *val_tmp;
int val_len;
if (sess->session_id_length > SSL_MAX_SSL_SESSION_ID_LENGTH)
return 1;
val_len=i2d_SSL_SESSION(sess, NULL);
if(val_len > SHSESS_MAX_DATA_LEN)
return 1;
shared_context_lock();
lastshs = shctx->head.p;
ebmb_delete(&lastshs->key);
memset(lastshs->key_data, 0, SSL_MAX_SSL_SESSION_ID_LENGTH);
memcpy(lastshs->key_data, sess->session_id, sess->session_id_length);
retshs = (struct shared_session *)ebmb_insert(&shctx->head.key.node.branches, &lastshs->key, SSL_MAX_SSL_SESSION_ID_LENGTH);
retshs->data_len = val_len;
val_tmp=retshs->data;
i2d_SSL_SESSION(sess, &val_tmp);
shared_session_movefirst(retshs);
shared_context_unlock();
return 1; /* leave the session in local cache for reuse */
/* Avoid warnings */
ssl = NULL;
}
SSL_SESSION *shctx_get_cb(SSL *ssl, unsigned char *key, int key_len, int *do_copy) {
(void)ssl;
struct shared_session *retshs;
unsigned char *val_tmp=NULL;
SSL_SESSION *sess;
*do_copy = 0; /* allow the session to be freed autmatically */
if (key_len == SSL_MAX_SSL_SESSION_ID_LENGTH) {
shared_context_lock();
retshs = (struct shared_session *)ebmb_lookup(&shctx->head.key.node.branches, key, key_len);
}
else if (key_len < SSL_MAX_SSL_SESSION_ID_LENGTH) {
unsigned char tmp_key[SSL_MAX_SSL_SESSION_ID_LENGTH];
memset(tmp_key, 0, SSL_MAX_SSL_SESSION_ID_LENGTH);
memcpy(tmp_key, key, key_len);
shared_context_lock();
retshs = (struct shared_session *)ebmb_lookup(&shctx->head.key.node.branches, tmp_key, SSL_MAX_SSL_SESSION_ID_LENGTH);
}
else
return NULL;
if(!retshs) {
shared_context_unlock();
return NULL;
}
val_tmp=retshs->data;
sess=d2i_SSL_SESSION(NULL, (const unsigned char **)&val_tmp, retshs->data_len);
shared_session_movefirst(retshs);
shared_context_unlock();
return sess;
/* Avoid warnings */
ssl = NULL;
}
void shctx_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess)
{
(void)ctx;
struct shared_session *retshs;
if (sess->session_id_length == SSL_MAX_SSL_SESSION_ID_LENGTH) {
shared_context_lock();
retshs = (struct shared_session *)ebmb_lookup(&shctx->head.key.node.branches, sess->session_id, sess->session_id_length);
}
else if (sess->session_id_length < SSL_MAX_SSL_SESSION_ID_LENGTH) {
unsigned char key[SSL_MAX_SSL_SESSION_ID_LENGTH];
memset(key, 0, SSL_MAX_SSL_SESSION_ID_LENGTH);
memcpy(key, sess->session_id, sess->session_id_length);
shared_context_lock();
retshs = (struct shared_session *)ebmb_lookup(&shctx->head.key.node.branches, key, SSL_MAX_SSL_SESSION_ID_LENGTH);
}
else
return;
if(!retshs) {
shared_context_unlock();
return;
}
ebmb_delete(&retshs->key);
shared_session_movelast(retshs);
shared_context_unlock();
return;
/* Avoid warnings */
ctx = NULL;
}
/* Init shared memory context if not allocated and set SSL context callbacks
* size is the max number of stored session
* Returns: -1 on alloc failure, size if performs context alloc, and 0 if just perform
* callbacks registration */
int shared_context_init(SSL_CTX *ctx, int size)
{
int ret = 0;
if (!shctx) {
int i;
#ifndef USE_SYSCALL_FUTEX
pthread_mutexattr_t attr;
#endif
struct shared_session *prev,*cur;
shctx = (struct shared_context *)mmap(NULL, sizeof(struct shared_context)+(size*sizeof(struct shared_session)),
PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
if (!shctx)
return -1;
#ifdef USE_SYSCALL_FUTEX
shctx->waiters = 0;
#else /* USE_SYSCALL_FUTEX */
pthread_mutexattr_init(&attr);
pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
pthread_mutex_init(&shctx->mutex, &attr);
#endif
memset(&shctx->head.key, 0, sizeof(struct ebmb_node));
/* No duplicate authorized: */
shctx->head.key.node.branches.b[1] = (void *)1;
cur = &shctx->head;
for ( i = 0 ; i < size ; i++) {
prev = cur;
cur = (struct shared_session *)((char *)prev + sizeof(struct shared_session));
prev->n = cur;
cur->p = prev;
}
cur->n = &shctx->head;
shctx->head.p = cur;
ret = size;
}
/* Set callbacks */
SSL_CTX_sess_set_new_cb(ctx, shctx_new_cb);
SSL_CTX_sess_set_get_cb(ctx, shctx_get_cb);
SSL_CTX_sess_set_remove_cb(ctx, shctx_remove_cb);
return ret;
}