Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 1 | /* |
| 2 | * shctx.c - shared context management functions for SSL |
| 3 | * |
| 4 | * Copyright (C) 2011-2012 EXCELIANCE |
| 5 | * |
| 6 | * Author: Emeric Brun - emeric@exceliance.fr |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License |
| 10 | * as published by the Free Software Foundation; either version |
| 11 | * 2 of the License, or (at your option) any later version. |
| 12 | */ |
| 13 | |
| 14 | #include <sys/mman.h> |
Emeric Brun | 9faf071 | 2012-09-25 11:11:16 +0200 | [diff] [blame] | 15 | #ifndef USE_PRIVATE_CACHE |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 16 | #ifdef USE_SYSCALL_FUTEX |
| 17 | #include <unistd.h> |
Willy Tarreau | 18b2059 | 2012-09-04 12:26:26 +0200 | [diff] [blame] | 18 | #ifndef u32 |
| 19 | #define u32 unsigned int |
| 20 | #endif |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 21 | #include <linux/futex.h> |
| 22 | #include <sys/syscall.h> |
| 23 | #else /* USE_SYSCALL_FUTEX */ |
| 24 | #include <pthread.h> |
| 25 | #endif /* USE_SYSCALL_FUTEX */ |
Emeric Brun | 9faf071 | 2012-09-25 11:11:16 +0200 | [diff] [blame] | 26 | #endif |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 27 | #include <arpa/inet.h> |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 28 | #include "ebmbtree.h" |
| 29 | #include "proto/shctx.h" |
| 30 | |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 31 | struct shsess_packet_hdr { |
| 32 | unsigned int eol; |
| 33 | unsigned char final:1; |
| 34 | unsigned char seq:7; |
| 35 | unsigned char id[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
| 36 | }; |
| 37 | |
| 38 | struct shsess_packet { |
| 39 | unsigned char version; |
| 40 | unsigned char sig[SHA_DIGEST_LENGTH]; |
| 41 | struct shsess_packet_hdr hdr; |
| 42 | unsigned char data[0]; |
| 43 | }; |
| 44 | |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 45 | struct shared_session { |
| 46 | struct ebmb_node key; |
| 47 | unsigned char key_data[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 48 | unsigned char data[SHSESS_BLOCK_MIN_SIZE]; |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 49 | }; |
| 50 | |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 51 | struct shared_block { |
| 52 | union { |
| 53 | struct shared_session session; |
| 54 | unsigned char data[sizeof(struct shared_session)]; |
| 55 | } data; |
| 56 | short int data_len; |
| 57 | struct shared_block *p; |
| 58 | struct shared_block *n; |
| 59 | }; |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 60 | |
| 61 | struct shared_context { |
Emeric Brun | 9faf071 | 2012-09-25 11:11:16 +0200 | [diff] [blame] | 62 | #ifndef USE_PRIVATE_CACHE |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 63 | #ifdef USE_SYSCALL_FUTEX |
| 64 | unsigned int waiters; |
| 65 | #else /* USE_SYSCALL_FUTEX */ |
| 66 | pthread_mutex_t mutex; |
| 67 | #endif |
Emeric Brun | 9faf071 | 2012-09-25 11:11:16 +0200 | [diff] [blame] | 68 | #endif |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 69 | struct shsess_packet_hdr upd; |
| 70 | unsigned char data[SHSESS_MAX_DATA_LEN]; |
| 71 | short int data_len; |
| 72 | struct shared_block active; |
| 73 | struct shared_block free; |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | /* Static shared context */ |
| 77 | static struct shared_context *shctx = NULL; |
Emeric Brun | 9faf071 | 2012-09-25 11:11:16 +0200 | [diff] [blame] | 78 | #ifndef USE_PRIVATE_CACHE |
Emeric Brun | 4b3091e | 2012-09-24 15:48:52 +0200 | [diff] [blame] | 79 | static int use_shared_mem = 0; |
Emeric Brun | 9faf071 | 2012-09-25 11:11:16 +0200 | [diff] [blame] | 80 | #endif |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 81 | |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 82 | /* Lock functions */ |
Emeric Brun | 9faf071 | 2012-09-25 11:11:16 +0200 | [diff] [blame] | 83 | #ifdef USE_PRIVATE_CACHE |
Willy Tarreau | 338a4fc | 2012-10-18 15:11:52 +0200 | [diff] [blame] | 84 | #define shared_context_lock() |
| 85 | #define shared_context_unlock() |
Emeric Brun | 9faf071 | 2012-09-25 11:11:16 +0200 | [diff] [blame] | 86 | |
| 87 | #else |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 88 | #ifdef USE_SYSCALL_FUTEX |
Emeric Brun | ce08baa | 2012-10-04 17:28:25 +0200 | [diff] [blame] | 89 | #if defined (__i486__) || defined (__i586__) || defined (__i686__) || defined (__x86_64__) |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 90 | static inline unsigned int xchg(unsigned int *ptr, unsigned int x) |
| 91 | { |
| 92 | __asm volatile("lock xchgl %0,%1" |
| 93 | : "=r" (x), "+m" (*ptr) |
| 94 | : "0" (x) |
| 95 | : "memory"); |
| 96 | return x; |
| 97 | } |
| 98 | |
| 99 | static inline unsigned int cmpxchg(unsigned int *ptr, unsigned int old, unsigned int new) |
| 100 | { |
| 101 | unsigned int ret; |
| 102 | |
| 103 | __asm volatile("lock cmpxchgl %2,%1" |
| 104 | : "=a" (ret), "+m" (*ptr) |
| 105 | : "r" (new), "0" (old) |
| 106 | : "memory"); |
| 107 | return ret; |
| 108 | } |
| 109 | |
| 110 | static inline unsigned char atomic_dec(unsigned int *ptr) |
| 111 | { |
| 112 | unsigned char ret; |
| 113 | __asm volatile("lock decl %0\n" |
| 114 | "setne %1\n" |
| 115 | : "+m" (*ptr), "=qm" (ret) |
| 116 | : |
| 117 | : "memory"); |
| 118 | return ret; |
| 119 | } |
| 120 | |
| 121 | #else /* if no x86_64 or i586 arch: use less optimized gcc >= 4.1 built-ins */ |
| 122 | static inline unsigned int xchg(unsigned int *ptr, unsigned int x) |
| 123 | { |
| 124 | return __sync_lock_test_and_set(ptr, x); |
| 125 | } |
| 126 | |
| 127 | static inline unsigned int cmpxchg(unsigned int *ptr, unsigned int old, unsigned int new) |
| 128 | { |
| 129 | return __sync_val_compare_and_swap(ptr, old, new); |
| 130 | } |
| 131 | |
| 132 | static inline unsigned char atomic_dec(unsigned int *ptr) |
| 133 | { |
| 134 | return __sync_sub_and_fetch(ptr, 1) ? 1 : 0; |
| 135 | } |
| 136 | |
| 137 | #endif |
| 138 | |
Emeric Brun | 4b3091e | 2012-09-24 15:48:52 +0200 | [diff] [blame] | 139 | static inline void _shared_context_lock(void) |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 140 | { |
| 141 | unsigned int x; |
| 142 | |
| 143 | x = cmpxchg(&shctx->waiters, 0, 1); |
| 144 | if (x) { |
| 145 | if (x != 2) |
| 146 | x = xchg(&shctx->waiters, 2); |
| 147 | |
| 148 | while (x) { |
| 149 | syscall(SYS_futex, &shctx->waiters, FUTEX_WAIT, 2, NULL, 0, 0); |
| 150 | x = xchg(&shctx->waiters, 2); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
Emeric Brun | 4b3091e | 2012-09-24 15:48:52 +0200 | [diff] [blame] | 155 | static inline void _shared_context_unlock(void) |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 156 | { |
| 157 | if (atomic_dec(&shctx->waiters)) { |
| 158 | shctx->waiters = 0; |
| 159 | syscall(SYS_futex, &shctx->waiters, FUTEX_WAKE, 1, NULL, 0, 0); |
| 160 | } |
| 161 | } |
| 162 | |
Willy Tarreau | 338a4fc | 2012-10-18 15:11:52 +0200 | [diff] [blame] | 163 | #define shared_context_lock() if (use_shared_mem) _shared_context_lock() |
Emeric Brun | 4b3091e | 2012-09-24 15:48:52 +0200 | [diff] [blame] | 164 | |
Willy Tarreau | 338a4fc | 2012-10-18 15:11:52 +0200 | [diff] [blame] | 165 | #define shared_context_unlock() if (use_shared_mem) _shared_context_unlock() |
Emeric Brun | 4b3091e | 2012-09-24 15:48:52 +0200 | [diff] [blame] | 166 | |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 167 | #else /* USE_SYSCALL_FUTEX */ |
| 168 | |
Willy Tarreau | 338a4fc | 2012-10-18 15:11:52 +0200 | [diff] [blame] | 169 | #define shared_context_lock() if (use_shared_mem) pthread_mutex_lock(&shctx->mutex) |
Emeric Brun | 4b3091e | 2012-09-24 15:48:52 +0200 | [diff] [blame] | 170 | |
Willy Tarreau | 338a4fc | 2012-10-18 15:11:52 +0200 | [diff] [blame] | 171 | #define shared_context_unlock() if (use_shared_mem) pthread_mutex_unlock(&shctx->mutex) |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 172 | |
| 173 | #endif |
Emeric Brun | 9faf071 | 2012-09-25 11:11:16 +0200 | [diff] [blame] | 174 | #endif |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 175 | |
| 176 | /* List Macros */ |
| 177 | |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 178 | #define shblock_unset(s) (s)->n->p = (s)->p; \ |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 179 | (s)->p->n = (s)->n; |
| 180 | |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 181 | #define shblock_set_free(s) shblock_unset(s) \ |
| 182 | (s)->n = &shctx->free; \ |
| 183 | (s)->p = shctx->free.p; \ |
| 184 | shctx->free.p->n = s; \ |
| 185 | shctx->free.p = s; |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 186 | |
| 187 | |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 188 | #define shblock_set_active(s) shblock_unset(s) \ |
| 189 | (s)->n = &shctx->active; \ |
| 190 | (s)->p = shctx->active.p; \ |
| 191 | shctx->active.p->n = s; \ |
| 192 | shctx->active.p = s; |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 193 | |
| 194 | |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 195 | /* Tree Macros */ |
| 196 | |
| 197 | #define shsess_tree_delete(s) ebmb_delete(&(s)->key); |
| 198 | |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 199 | #define shsess_tree_insert(s) (struct shared_session *)ebmb_insert(&shctx->active.data.session.key.node.branches, \ |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 200 | &(s)->key, SSL_MAX_SSL_SESSION_ID_LENGTH); |
| 201 | |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 202 | #define shsess_tree_lookup(k) (struct shared_session *)ebmb_lookup(&shctx->active.data.session.key.node.branches, \ |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 203 | (k), SSL_MAX_SSL_SESSION_ID_LENGTH); |
| 204 | |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 205 | /* shared session functions */ |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 206 | |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 207 | /* Free session blocks, returns number of freed blocks */ |
| 208 | static int shsess_free(struct shared_session *shsess) |
| 209 | { |
| 210 | struct shared_block *block; |
| 211 | int ret = 1; |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 212 | |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 213 | if (((struct shared_block *)shsess)->data_len <= sizeof(shsess->data)) { |
| 214 | shblock_set_free((struct shared_block *)shsess); |
| 215 | return ret; |
| 216 | } |
| 217 | block = ((struct shared_block *)shsess)->n; |
| 218 | shblock_set_free((struct shared_block *)shsess); |
| 219 | while (1) { |
| 220 | struct shared_block *next; |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 221 | |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 222 | if (block->data_len <= sizeof(block->data)) { |
| 223 | /* last block */ |
| 224 | shblock_set_free(block); |
| 225 | ret++; |
| 226 | break; |
| 227 | } |
| 228 | next = block->n; |
| 229 | shblock_set_free(block); |
| 230 | ret++; |
| 231 | block = next; |
| 232 | } |
| 233 | return ret; |
| 234 | } |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 235 | |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 236 | /* This function frees enough blocks to store a new session of data_len. |
| 237 | * Returns a ptr on a free block if it succeeds, or NULL if there are not |
| 238 | * enough blocks to store that session. |
| 239 | */ |
| 240 | static struct shared_session *shsess_get_next(int data_len) |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 241 | { |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 242 | int head = 0; |
| 243 | struct shared_block *b; |
| 244 | |
| 245 | b = shctx->free.n; |
| 246 | while (b != &shctx->free) { |
| 247 | if (!head) { |
| 248 | data_len -= sizeof(b->data.session.data); |
| 249 | head = 1; |
| 250 | } |
| 251 | else |
| 252 | data_len -= sizeof(b->data.data); |
| 253 | if (data_len <= 0) |
| 254 | return &shctx->free.n->data.session; |
| 255 | b = b->n; |
| 256 | } |
| 257 | b = shctx->active.n; |
| 258 | while (b != &shctx->active) { |
| 259 | int freed; |
| 260 | |
| 261 | shsess_tree_delete(&b->data.session); |
| 262 | freed = shsess_free(&b->data.session); |
| 263 | if (!head) |
| 264 | data_len -= sizeof(b->data.session.data) + (freed-1)*sizeof(b->data.data); |
| 265 | else |
| 266 | data_len -= freed*sizeof(b->data.data); |
| 267 | if (data_len <= 0) |
| 268 | return &shctx->free.n->data.session; |
| 269 | b = shctx->active.n; |
| 270 | } |
| 271 | return NULL; |
| 272 | } |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 273 | |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 274 | /* store a session into the cache |
| 275 | * s_id : session id padded with zero to SSL_MAX_SSL_SESSION_ID_LENGTH |
| 276 | * data: asn1 encoded session |
| 277 | * data_len: asn1 encoded session length |
| 278 | * Returns 1 id session was stored (else 0) |
| 279 | */ |
| 280 | static int shsess_store(unsigned char *s_id, unsigned char *data, int data_len) |
| 281 | { |
| 282 | struct shared_session *shsess, *oldshsess; |
| 283 | |
| 284 | shsess = shsess_get_next(data_len); |
| 285 | if (!shsess) { |
| 286 | /* Could not retrieve enough free blocks to store that session */ |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 287 | return 0; |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 288 | } |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 289 | |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 290 | /* prepare key */ |
| 291 | memcpy(shsess->key_data, s_id, SSL_MAX_SSL_SESSION_ID_LENGTH); |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 292 | |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 293 | /* it returns the already existing node |
| 294 | or current node if none, never returns null */ |
| 295 | oldshsess = shsess_tree_insert(shsess); |
| 296 | if (oldshsess != shsess) { |
| 297 | /* free all blocks used by old node */ |
| 298 | shsess_free(oldshsess); |
| 299 | shsess = oldshsess; |
| 300 | } |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 301 | |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 302 | ((struct shared_block *)shsess)->data_len = data_len; |
| 303 | if (data_len <= sizeof(shsess->data)) { |
| 304 | /* Store on a single block */ |
| 305 | memcpy(shsess->data, data, data_len); |
| 306 | shblock_set_active((struct shared_block *)shsess); |
| 307 | } |
| 308 | else { |
| 309 | unsigned char *p; |
| 310 | /* Store on multiple blocks */ |
| 311 | int cur_len; |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 312 | |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 313 | memcpy(shsess->data, data, sizeof(shsess->data)); |
| 314 | p = data + sizeof(shsess->data); |
| 315 | cur_len = data_len - sizeof(shsess->data); |
| 316 | shblock_set_active((struct shared_block *)shsess); |
| 317 | while (1) { |
| 318 | /* Store next data on free block. |
| 319 | * shsess_get_next guarantees that there are enough |
| 320 | * free blocks in queue. |
| 321 | */ |
| 322 | struct shared_block *block; |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 323 | |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 324 | block = shctx->free.n; |
| 325 | if (cur_len <= sizeof(block->data)) { |
| 326 | /* This is the last block */ |
| 327 | block->data_len = cur_len; |
| 328 | memcpy(block->data.data, p, cur_len); |
| 329 | shblock_set_active(block); |
| 330 | break; |
| 331 | } |
| 332 | /* Intermediate block */ |
| 333 | block->data_len = cur_len; |
| 334 | memcpy(block->data.data, p, sizeof(block->data)); |
| 335 | p += sizeof(block->data.data); |
| 336 | cur_len -= sizeof(block->data.data); |
| 337 | shblock_set_active(block); |
| 338 | } |
| 339 | } |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 340 | |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 341 | return 1; |
| 342 | } |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 343 | |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 344 | |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 345 | /* SSL context callbacks */ |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 346 | |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 347 | /* SSL callback used on new session creation */ |
| 348 | int shctx_new_cb(SSL *ssl, SSL_SESSION *sess) |
| 349 | { |
| 350 | unsigned char encsess[sizeof(struct shsess_packet)+SHSESS_MAX_DATA_LEN]; |
| 351 | struct shsess_packet *packet = (struct shsess_packet *)encsess; |
| 352 | unsigned char *p; |
Emeric Brun | f27af0d | 2013-04-26 18:56:49 +0200 | [diff] [blame] | 353 | int data_len, sid_length, sid_ctx_length; |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 354 | |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 355 | |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 356 | /* Session id is already stored in to key and session id is known |
| 357 | * so we dont store it to keep size. |
| 358 | */ |
| 359 | sid_length = sess->session_id_length; |
| 360 | sess->session_id_length = 0; |
Emeric Brun | f27af0d | 2013-04-26 18:56:49 +0200 | [diff] [blame] | 361 | sid_ctx_length = sess->sid_ctx_length; |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 362 | sess->sid_ctx_length = 0; |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 363 | |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 364 | /* check if buffer is large enough for the ASN1 encoded session */ |
| 365 | data_len = i2d_SSL_SESSION(sess, NULL); |
| 366 | if (data_len > SHSESS_MAX_DATA_LEN) |
| 367 | goto err; |
| 368 | |
| 369 | /* process ASN1 session encoding before the lock */ |
| 370 | p = packet->data; |
| 371 | i2d_SSL_SESSION(sess, &p); |
| 372 | |
| 373 | memcpy(packet->hdr.id, sess->session_id, sid_length); |
| 374 | if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) |
| 375 | memset(&packet->hdr.id[sid_length], 0, SSL_MAX_SSL_SESSION_ID_LENGTH-sid_length); |
| 376 | |
| 377 | shared_context_lock(); |
| 378 | |
| 379 | /* store to cache */ |
| 380 | shsess_store(packet->hdr.id, packet->data, data_len); |
| 381 | |
| 382 | shared_context_unlock(); |
| 383 | |
| 384 | err: |
| 385 | /* reset original length values */ |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 386 | sess->session_id_length = sid_length; |
Emeric Brun | f27af0d | 2013-04-26 18:56:49 +0200 | [diff] [blame] | 387 | sess->sid_ctx_length = sid_ctx_length; |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 388 | |
| 389 | return 0; /* do not increment session reference count */ |
| 390 | } |
| 391 | |
| 392 | /* SSL callback used on lookup an existing session cause none found in internal cache */ |
| 393 | SSL_SESSION *shctx_get_cb(SSL *ssl, unsigned char *key, int key_len, int *do_copy) |
| 394 | { |
| 395 | struct shared_session *shsess; |
| 396 | unsigned char data[SHSESS_MAX_DATA_LEN], *p; |
| 397 | unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 398 | int data_len; |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 399 | SSL_SESSION *sess; |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 400 | |
| 401 | /* allow the session to be freed automatically by openssl */ |
| 402 | *do_copy = 0; |
| 403 | |
| 404 | /* tree key is zeros padded sessionid */ |
| 405 | if (key_len < SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 406 | memcpy(tmpkey, key, key_len); |
| 407 | memset(tmpkey + key_len, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - key_len); |
| 408 | key = tmpkey; |
| 409 | } |
| 410 | |
| 411 | /* lock cache */ |
| 412 | shared_context_lock(); |
| 413 | |
| 414 | /* lookup for session */ |
| 415 | shsess = shsess_tree_lookup(key); |
| 416 | if (!shsess) { |
| 417 | /* no session found: unlock cache and exit */ |
| 418 | shared_context_unlock(); |
| 419 | return NULL; |
| 420 | } |
| 421 | |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 422 | data_len = ((struct shared_block *)shsess)->data_len; |
| 423 | if (data_len <= sizeof(shsess->data)) { |
| 424 | /* Session stored on single block */ |
| 425 | memcpy(data, shsess->data, data_len); |
| 426 | shblock_set_active((struct shared_block *)shsess); |
| 427 | } |
| 428 | else { |
| 429 | /* Session stored on multiple blocks */ |
| 430 | struct shared_block *block; |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 431 | |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 432 | memcpy(data, shsess->data, sizeof(shsess->data)); |
| 433 | p = data + sizeof(shsess->data); |
| 434 | block = ((struct shared_block *)shsess)->n; |
| 435 | shblock_set_active((struct shared_block *)shsess); |
| 436 | while (1) { |
| 437 | /* Retrieve data from next block */ |
| 438 | struct shared_block *next; |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 439 | |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 440 | if (block->data_len <= sizeof(block->data.data)) { |
| 441 | /* This is the last block */ |
| 442 | memcpy(p, block->data.data, block->data_len); |
| 443 | p += block->data_len; |
| 444 | shblock_set_active(block); |
| 445 | break; |
| 446 | } |
| 447 | /* Intermediate block */ |
| 448 | memcpy(p, block->data.data, sizeof(block->data.data)); |
| 449 | p += sizeof(block->data.data); |
| 450 | next = block->n; |
| 451 | shblock_set_active(block); |
| 452 | block = next; |
| 453 | } |
| 454 | } |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 455 | |
| 456 | shared_context_unlock(); |
| 457 | |
| 458 | /* decode ASN1 session */ |
| 459 | p = data; |
| 460 | sess = d2i_SSL_SESSION(NULL, (const unsigned char **)&p, data_len); |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 461 | /* Reset session id and session id contenxt */ |
| 462 | if (sess) { |
| 463 | memcpy(sess->session_id, key, key_len); |
| 464 | sess->session_id_length = key_len; |
Emeric Brun | f27af0d | 2013-04-26 18:56:49 +0200 | [diff] [blame] | 465 | memcpy(sess->sid_ctx, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME)); |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 466 | sess->sid_ctx_length = ssl->sid_ctx_length; |
| 467 | } |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 468 | |
| 469 | return sess; |
| 470 | } |
| 471 | |
| 472 | /* SSL callback used to signal session is no more used in internal cache */ |
| 473 | void shctx_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess) |
| 474 | { |
| 475 | struct shared_session *shsess; |
| 476 | unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
| 477 | unsigned char *key = sess->session_id; |
| 478 | (void)ctx; |
| 479 | |
| 480 | /* tree key is zeros padded sessionid */ |
| 481 | if (sess->session_id_length < SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 482 | memcpy(tmpkey, sess->session_id, sess->session_id_length); |
| 483 | memset(tmpkey+sess->session_id_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - sess->session_id_length); |
| 484 | key = tmpkey; |
| 485 | } |
| 486 | |
| 487 | shared_context_lock(); |
| 488 | |
| 489 | /* lookup for session */ |
| 490 | shsess = shsess_tree_lookup(key); |
| 491 | if (shsess) { |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 492 | /* free session */ |
| 493 | shsess_tree_delete(shsess); |
| 494 | shsess_free(shsess); |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | /* unlock cache */ |
| 498 | shared_context_unlock(); |
| 499 | } |
| 500 | |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 501 | /* Allocate shared memory context. |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 502 | * <size> is maximum cached sessions. |
Emeric Brun | 22890a1 | 2012-12-28 14:41:32 +0100 | [diff] [blame] | 503 | * If <size> is set to less or equal to 0, ssl cache is disabled. |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 504 | * Returns: -1 on alloc failure, <size> if it performs context alloc, |
| 505 | * and 0 if cache is already allocated. |
| 506 | */ |
Emeric Brun | 4b3091e | 2012-09-24 15:48:52 +0200 | [diff] [blame] | 507 | int shared_context_init(int size, int shared) |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 508 | { |
| 509 | int i; |
Emeric Brun | 9faf071 | 2012-09-25 11:11:16 +0200 | [diff] [blame] | 510 | #ifndef USE_PRIVATE_CACHE |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 511 | #ifndef USE_SYSCALL_FUTEX |
| 512 | pthread_mutexattr_t attr; |
| 513 | #endif /* USE_SYSCALL_FUTEX */ |
Emeric Brun | 9faf071 | 2012-09-25 11:11:16 +0200 | [diff] [blame] | 514 | #endif |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 515 | struct shared_block *prev,*cur; |
Emeric Brun | 4b3091e | 2012-09-24 15:48:52 +0200 | [diff] [blame] | 516 | int maptype = MAP_PRIVATE; |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 517 | |
| 518 | if (shctx) |
| 519 | return 0; |
| 520 | |
| 521 | if (size<=0) |
Emeric Brun | 22890a1 | 2012-12-28 14:41:32 +0100 | [diff] [blame] | 522 | return 0; |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 523 | |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 524 | /* Increate size by one to reserve one node for lookup */ |
| 525 | size++; |
Emeric Brun | 9faf071 | 2012-09-25 11:11:16 +0200 | [diff] [blame] | 526 | #ifndef USE_PRIVATE_CACHE |
Emeric Brun | 4b3091e | 2012-09-24 15:48:52 +0200 | [diff] [blame] | 527 | if (shared) |
| 528 | maptype = MAP_SHARED; |
Emeric Brun | 9faf071 | 2012-09-25 11:11:16 +0200 | [diff] [blame] | 529 | #endif |
Emeric Brun | 4b3091e | 2012-09-24 15:48:52 +0200 | [diff] [blame] | 530 | |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 531 | shctx = (struct shared_context *)mmap(NULL, sizeof(struct shared_context)+(size*sizeof(struct shared_block)), |
Emeric Brun | 4b3091e | 2012-09-24 15:48:52 +0200 | [diff] [blame] | 532 | PROT_READ | PROT_WRITE, maptype | MAP_ANON, -1, 0); |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 533 | if (!shctx || shctx == MAP_FAILED) { |
| 534 | shctx = NULL; |
| 535 | return -1; |
| 536 | } |
| 537 | |
Emeric Brun | 9faf071 | 2012-09-25 11:11:16 +0200 | [diff] [blame] | 538 | #ifndef USE_PRIVATE_CACHE |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 539 | #ifdef USE_SYSCALL_FUTEX |
| 540 | shctx->waiters = 0; |
| 541 | #else |
| 542 | pthread_mutexattr_init(&attr); |
| 543 | pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED); |
| 544 | pthread_mutex_init(&shctx->mutex, &attr); |
| 545 | #endif |
Emeric Brun | 4b3091e | 2012-09-24 15:48:52 +0200 | [diff] [blame] | 546 | if (maptype == MAP_SHARED) |
| 547 | use_shared_mem = 1; |
Emeric Brun | 9faf071 | 2012-09-25 11:11:16 +0200 | [diff] [blame] | 548 | #endif |
Emeric Brun | 4b3091e | 2012-09-24 15:48:52 +0200 | [diff] [blame] | 549 | |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 550 | memset(&shctx->active.data.session.key, 0, sizeof(struct ebmb_node)); |
| 551 | memset(&shctx->free.data.session.key, 0, sizeof(struct ebmb_node)); |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 552 | |
| 553 | /* No duplicate authorized in tree: */ |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 554 | shctx->active.data.session.key.node.branches = EB_ROOT_UNIQUE; |
| 555 | |
| 556 | /* Init remote update cache */ |
| 557 | shctx->upd.eol = 0; |
| 558 | shctx->upd.seq = 0; |
| 559 | shctx->data_len = 0; |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 560 | |
| 561 | cur = &shctx->active; |
| 562 | cur->n = cur->p = cur; |
| 563 | |
| 564 | cur = &shctx->free; |
| 565 | for (i = 0 ; i < size ; i++) { |
| 566 | prev = cur; |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 567 | cur = (struct shared_block *)((char *)prev + sizeof(struct shared_block)); |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 568 | prev->n = cur; |
| 569 | cur->p = prev; |
| 570 | } |
| 571 | cur->n = &shctx->free; |
| 572 | shctx->free.p = cur; |
| 573 | |
| 574 | return size; |
| 575 | } |
| 576 | |
| 577 | |
| 578 | /* Set session cache mode to server and disable openssl internal cache. |
| 579 | * Set shared cache callbacks on an ssl context. |
| 580 | * Shared context MUST be firstly initialized */ |
| 581 | void shared_context_set_cache(SSL_CTX *ctx) |
| 582 | { |
Emeric Brun | 786991e | 2012-11-26 18:37:12 +0100 | [diff] [blame] | 583 | SSL_CTX_set_session_id_context(ctx, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME)); |
| 584 | |
Emeric Brun | 22890a1 | 2012-12-28 14:41:32 +0100 | [diff] [blame] | 585 | if (!shctx) { |
| 586 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF); |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 587 | return; |
Emeric Brun | 22890a1 | 2012-12-28 14:41:32 +0100 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER | |
| 591 | SSL_SESS_CACHE_NO_INTERNAL | |
| 592 | SSL_SESS_CACHE_NO_AUTO_CLEAR); |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 593 | |
| 594 | /* Set callbacks */ |
| 595 | SSL_CTX_sess_set_new_cb(ctx, shctx_new_cb); |
| 596 | SSL_CTX_sess_set_get_cb(ctx, shctx_get_cb); |
| 597 | SSL_CTX_sess_set_remove_cb(ctx, shctx_remove_cb); |
| 598 | } |