| | 307 | ret = _tls_init_threads(); |
|---|
| | 308 | if (ret) { |
|---|
| | 309 | out_log(LEVEL_CRITICAL, "_tls_init_threads failed (out of memory?)"); |
|---|
| | 310 | str_deallocate(tls_certificate); |
|---|
| | 311 | str_deallocate(tls_certificate_key); |
|---|
| | 312 | str_deallocate(tls_ca_file); |
|---|
| | 313 | str_deallocate(tls_ca_path); |
|---|
| | 314 | return 1; |
|---|
| | 315 | } |
|---|
| | 316 | |
|---|
| | 317 | CRYPTO_set_locking_callback((void(*)(int, int, const char *, int))_openssl_static_lock_callback); |
|---|
| | 318 | /* TODO: set dynamic locking callbacks */ |
|---|
| | 319 | |
|---|
| | 328 | /*************** _tls_init_threads *******************/ |
|---|
| | 329 | static int _tls_init_threads(void) { |
|---|
| | 330 | int static_locks_req; |
|---|
| | 331 | int i; |
|---|
| | 332 | int j; |
|---|
| | 333 | |
|---|
| | 334 | /* determine how many static locks OpenSSL requires */ |
|---|
| | 335 | static_locks_req = CRYPTO_num_locks(); |
|---|
| | 336 | if (static_locks_req > 0) { |
|---|
| | 337 | openssl_static_lock = wzd_malloc(sizeof(wzd_mutex_t *) * static_locks_req); |
|---|
| | 338 | if (!openssl_static_lock) |
|---|
| | 339 | return -1; |
|---|
| | 340 | /* create each mutex so it is ready to be used */ |
|---|
| | 341 | for (i = 0; i < static_locks_req; i++) { |
|---|
| | 342 | openssl_static_lock[i] = wzd_mutex_create(0); |
|---|
| | 343 | if (!openssl_static_lock[i]) { |
|---|
| | 344 | for (j = 0; j < i; j++) |
|---|
| | 345 | wzd_mutex_destroy(openssl_static_lock[j]); |
|---|
| | 346 | wzd_free(openssl_static_lock); |
|---|
| | 347 | openssl_static_lock = NULL; |
|---|
| | 348 | return -1; |
|---|
| | 349 | } |
|---|
| | 350 | } |
|---|
| | 351 | } |
|---|
| | 352 | |
|---|
| | 353 | /* TODO: init dynamic lock array (it can grow/shrink later) */ |
|---|
| | 354 | |
|---|
| | 355 | openssl_static_lock_num = static_locks_req; |
|---|
| | 356 | return 0; |
|---|
| | 357 | } |
|---|
| | 358 | |
|---|
| | 359 | |
|---|
| | 383 | /*************** _tls_exit_threads *******************/ |
|---|
| | 384 | static void _tls_exit_threads(void) { |
|---|
| | 385 | int i; |
|---|
| | 386 | |
|---|
| | 387 | for (i = 0; i < openssl_static_lock_num; i++) |
|---|
| | 388 | wzd_mutex_destroy(openssl_static_lock[i]); |
|---|
| | 389 | wzd_free(openssl_static_lock); |
|---|
| | 390 | |
|---|
| | 391 | /* TODO: free up dynamic lock array */ |
|---|
| | 392 | return; |
|---|
| | 393 | } |
|---|
| | 394 | |
|---|
| | 395 | /*************** _openssl_static_lock_callback *******/ |
|---|
| | 396 | static void _openssl_static_lock_callback(int mode, int n, const char *file, int line) { |
|---|
| | 397 | if (mode & CRYPTO_LOCK) |
|---|
| | 398 | wzd_mutex_lock(openssl_static_lock[n]); |
|---|
| | 399 | else if (mode & CRYPTO_UNLOCK) |
|---|
| | 400 | wzd_mutex_unlock(openssl_static_lock[n]); |
|---|
| | 401 | return; |
|---|
| | 402 | } |
|---|