tls_init.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. /*
  2. * $Id$
  3. *
  4. * TLS module - OpenSSL initialization funtions
  5. *
  6. * Copyright (C) 2001-2003 FhG FOKUS
  7. * Copyright (C) 2004,2005 Free Software Foundation, Inc.
  8. * Copyright (C) 2005,2006 iptelorg GmbH
  9. *
  10. * This file is part of SIP-router, a free SIP server.
  11. *
  12. * SIP-router is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version
  16. *
  17. * SIP-router is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. */
  26. /*
  27. * History:
  28. * --------
  29. * 2007-01-26 openssl kerberos malloc bug detection/workaround (andrei)
  30. * 2007-02-23 openssl low memory bugs workaround (andrei)
  31. */
  32. /*! \defgroup tls SIP-router TLS support
  33. *
  34. * This modules implements SIP over TCP with TLS encryption.
  35. * Make sure you read the README file that describes configuration
  36. * of TLS for single servers and servers hosting multiple domains,
  37. * and thus using multiple SSL/TLS certificates.
  38. *
  39. *
  40. */
  41. /*!
  42. * \file
  43. * \brief SIP-router TLS support :: Initialization
  44. * \ingroup tls
  45. * Module: \ref tls
  46. */
  47. #include <stdio.h>
  48. #include <sys/types.h>
  49. #include <netinet/in_systm.h>
  50. #include <netinet/in.h>
  51. #include <netinet/tcp.h>
  52. #include <netinet/ip.h>
  53. #include <unistd.h>
  54. #include <string.h>
  55. #include <openssl/ssl.h>
  56. #include "../../dprint.h"
  57. #include "../../mem/shm_mem.h"
  58. #include "../../tcp_init.h"
  59. #include "../../socket_info.h"
  60. #include "../../pt.h"
  61. #include "../../cfg/cfg.h"
  62. #include "../../cfg/cfg_ctx.h"
  63. #include "tls_verify.h"
  64. #include "tls_domain.h"
  65. #include "tls_util.h"
  66. #include "tls_mod.h"
  67. #include "tls_init.h"
  68. #include "tls_locking.h"
  69. #include "tls_ct_wrq.h"
  70. #include "tls_cfg.h"
  71. /* will be set to 1 when the TLS env is initialized to make destroy safe */
  72. static int tls_mod_initialized = 0;
  73. #if OPENSSL_VERSION_NUMBER < 0x00907000L
  74. # warning ""
  75. # warning "==============================================================="
  76. # warning " Your version of OpenSSL is < 0.9.7."
  77. # warning " Upgrade for better compatibility, features and security fixes!"
  78. # warning "==============================================================="
  79. # warning ""
  80. #endif
  81. /* replace openssl zlib compression with our version if necessary
  82. * (the openssl zlib compression uses the wrong malloc, see
  83. * openssl #1468): 0.9.8-dev < version <0.9.8e-beta1 */
  84. #if OPENSSL_VERSION_NUMBER >= 0x00908000L /* 0.9.8-dev */ && \
  85. OPENSSL_VERSION_NUMBER < 0x00908051L /* 0.9.8.e-beta1 */
  86. # ifndef OPENSSL_NO_COMP
  87. # warning "openssl zlib compression bug workaround enabled"
  88. # endif
  89. # define TLS_FIX_ZLIB_COMPRESSION
  90. # include "fixed_c_zlib.h"
  91. #endif
  92. #ifdef TLS_KSSL_WORKARROUND
  93. #if OPENSSL_VERSION_NUMBER < 0x00908050L
  94. # warning "openssl lib compiled with kerberos support which introduces a bug\
  95. (wrong malloc/free used in kssl.c) -- attempting workaround"
  96. # warning "NOTE: if you don't link libssl staticaly don't try running the \
  97. compiled code on a system with a differently compiled openssl (it's safer \
  98. to compile on the _target_ system)"
  99. #endif /* OPENSSL_VERSION_NUMBER */
  100. #endif /* TLS_KSSL_WORKARROUND */
  101. /* openssl < 1. 0 */
  102. #if OPENSSL_VERSION_NUMBER < 0x01000000L
  103. # warning "openssl < 1.0: no TLS extensions or server name support"
  104. #endif /* OPENSSL_VERION < 1.0 */
  105. #ifndef OPENSSL_NO_COMP
  106. #define TLS_COMP_SUPPORT
  107. #else
  108. #undef TLS_COMP_SUPPORT
  109. #endif
  110. #ifndef OPENSSL_NO_KRB5
  111. #define TLS_KERBEROS_SUPPORT
  112. #else
  113. #undef TLS_KERBEROS_SUPPORT
  114. #endif
  115. #ifdef TLS_KSSL_WORKARROUND
  116. int openssl_kssl_malloc_bug=0; /* is openssl bug #1467 present ? */
  117. #endif
  118. const SSL_METHOD* ssl_methods[TLS_USE_SSLv23 + 1];
  119. #ifdef NO_TLS_MALLOC_DBG
  120. #undef TLS_MALLOC_DBG /* extra malloc debug info from openssl */
  121. #endif /* NO_TLS_MALLOC_DBG */
  122. /*
  123. * Wrappers around SER shared memory functions
  124. * (which can be macros)
  125. */
  126. #ifdef TLS_MALLOC_DBG
  127. #warning "tls module compiled with malloc debugging info (extra overhead)"
  128. #include <execinfo.h>
  129. /*
  130. #define RAND_NULL_MALLOC (1024)
  131. #define NULL_GRACE_PERIOD 10U
  132. */
  133. inline static char* buf_append(char* buf, char* end, char* str, int str_len)
  134. {
  135. if ( (buf+str_len)<end){
  136. memcpy(buf, str, str_len);
  137. return buf+str_len;
  138. }
  139. return 0;
  140. }
  141. inline static int backtrace2str(char* buf, int size)
  142. {
  143. void* bt[32];
  144. int bt_size, i;
  145. char** bt_strs;
  146. char* p;
  147. char* end;
  148. char* next;
  149. char* s;
  150. char* e;
  151. p=buf; end=buf+size;
  152. bt_size=backtrace(bt, sizeof(bt)/sizeof(bt[0]));
  153. bt_strs=backtrace_symbols(bt, bt_size);
  154. if (bt_strs){
  155. p=buf; end=buf+size;
  156. /*if (bt_size>16) bt_size=16;*/ /* go up only 12 entries */
  157. for (i=3; i< bt_size; i++){
  158. /* try to isolate only the function name*/
  159. s=strchr(bt_strs[i], '(');
  160. if (s && ((e=strchr(s, ')'))!=0)){
  161. s++;
  162. }else if ((s=strchr(bt_strs[i], '['))!=0){
  163. e=s+strlen(s);
  164. }else{
  165. s=bt_strs[i]; e=s+strlen(s); /* add thw whole string */
  166. }
  167. next=buf_append(p, end, s, (int)(long)(e-s));
  168. if (next==0) break;
  169. else p=next;
  170. if (p<end){
  171. *p=':'; /* separator */
  172. p++;
  173. }else break;
  174. }
  175. if (p==buf){
  176. *p=0;
  177. p++;
  178. }else
  179. *(p-1)=0;
  180. free(bt_strs);
  181. }
  182. return (int)(long)(p-buf);
  183. }
  184. static void* ser_malloc(size_t size, const char* file, int line)
  185. {
  186. void *p;
  187. char bt_buf[1024];
  188. int s;
  189. #ifdef RAND_NULL_MALLOC
  190. static ticks_t st=0;
  191. /* start random null returns only after
  192. * NULL_GRACE_PERIOD from first call */
  193. if (st==0) st=get_ticks();
  194. if (((get_ticks()-st)<NULL_GRACE_PERIOD) || (random()%RAND_NULL_MALLOC)){
  195. #endif
  196. s=backtrace2str(bt_buf, sizeof(bt_buf));
  197. /* ugly hack: keep the bt inside the alloc'ed fragment */
  198. p=_shm_malloc(size+s, file, "via ser_malloc", line);
  199. if (p==0){
  200. LOG(L_CRIT, "tsl: ser_malloc(%d)[%s:%d]==null, bt: %s\n",
  201. size, file, line, bt_buf);
  202. }else{
  203. memcpy(p+size, bt_buf, s);
  204. ((struct qm_frag*)((char*)p-sizeof(struct qm_frag)))->func=
  205. p+size;
  206. }
  207. #ifdef RAND_NULL_MALLOC
  208. }else{
  209. p=0;
  210. backtrace2str(bt_buf, sizeof(bt_buf));
  211. LOG(L_CRIT, "tsl: random ser_malloc(%d)[%s:%d]"
  212. " returning null - bt: %s\n",
  213. size, file, line, bt_buf);
  214. }
  215. #endif
  216. return p;
  217. }
  218. static void* ser_realloc(void *ptr, size_t size, const char* file, int line)
  219. {
  220. void *p;
  221. char bt_buf[1024];
  222. int s;
  223. #ifdef RAND_NULL_MALLOC
  224. static ticks_t st=0;
  225. /* start random null returns only after
  226. * NULL_GRACE_PERIOD from first call */
  227. if (st==0) st=get_ticks();
  228. if (((get_ticks()-st)<NULL_GRACE_PERIOD) || (random()%RAND_NULL_MALLOC)){
  229. #endif
  230. s=backtrace2str(bt_buf, sizeof(bt_buf));
  231. p=_shm_realloc(ptr, size+s, file, "via ser_realloc", line);
  232. if (p==0){
  233. LOG(L_CRIT, "tsl: ser_realloc(%p, %d)[%s:%d]==null, bt: %s\n",
  234. ptr, size, file, line, bt_buf);
  235. }else{
  236. memcpy(p+size, bt_buf, s);
  237. ((struct qm_frag*)((char*)p-sizeof(struct qm_frag)))->func=
  238. p+size;
  239. }
  240. #ifdef RAND_NULL_MALLOC
  241. }else{
  242. p=0;
  243. backtrace2str(bt_buf, sizeof(bt_buf));
  244. LOG(L_CRIT, "tsl: random ser_realloc(%p, %d)[%s:%d]"
  245. " returning null - bt: %s\n", ptr, size, file, line,
  246. bt_buf);
  247. }
  248. #endif
  249. return p;
  250. }
  251. #else /*TLS_MALLOC_DBG */
  252. static void* ser_malloc(size_t size)
  253. {
  254. return shm_malloc(size);
  255. }
  256. static void* ser_realloc(void *ptr, size_t size)
  257. {
  258. return shm_realloc(ptr, size);
  259. }
  260. #endif
  261. static void ser_free(void *ptr)
  262. {
  263. /* The memory functions provided to openssl needs to behave like standard
  264. * memory functions, i.e. free(). Therefore, ser_free must accept NULL
  265. * pointers, see: http://openssl.6102.n7.nabble.com/Custom-free-routine-is-invoked-with-NULL-argument-in-openssl-1-0-1-td25937.html
  266. * As shm_free() aborts on null pointers, we have to check for null pointer
  267. * here in the wrapper function.
  268. */
  269. if (ptr) {
  270. shm_free(ptr);
  271. }
  272. }
  273. /*
  274. * Initialize TLS socket
  275. */
  276. int tls_h_init_si(struct socket_info *si)
  277. {
  278. int ret;
  279. /*
  280. * reuse tcp initialization
  281. */
  282. ret = tcp_init(si);
  283. if (ret != 0) {
  284. ERR("Error while initializing TCP part of TLS socket %.*s:%d\n",
  285. si->address_str.len, si->address_str.s, si->port_no);
  286. goto error;
  287. }
  288. si->proto = PROTO_TLS;
  289. return 0;
  290. error:
  291. if (si->socket != -1) {
  292. close(si->socket);
  293. si->socket = -1;
  294. }
  295. return ret;
  296. }
  297. /*
  298. * initialize ssl methods
  299. */
  300. static void init_ssl_methods(void)
  301. {
  302. #ifndef OPENSSL_NO_SSL2
  303. ssl_methods[TLS_USE_SSLv2_cli - 1] = SSLv2_client_method();
  304. ssl_methods[TLS_USE_SSLv2_srv - 1] = SSLv2_server_method();
  305. ssl_methods[TLS_USE_SSLv2 - 1] = SSLv2_method();
  306. #endif
  307. ssl_methods[TLS_USE_SSLv3_cli - 1] = SSLv3_client_method();
  308. ssl_methods[TLS_USE_SSLv3_srv - 1] = SSLv3_server_method();
  309. ssl_methods[TLS_USE_SSLv3 - 1] = SSLv3_method();
  310. ssl_methods[TLS_USE_TLSv1_cli - 1] = TLSv1_client_method();
  311. ssl_methods[TLS_USE_TLSv1_srv - 1] = TLSv1_server_method();
  312. ssl_methods[TLS_USE_TLSv1 - 1] = TLSv1_method();
  313. ssl_methods[TLS_USE_SSLv23_cli - 1] = SSLv23_client_method();
  314. ssl_methods[TLS_USE_SSLv23_srv - 1] = SSLv23_server_method();
  315. ssl_methods[TLS_USE_SSLv23 - 1] = SSLv23_method();
  316. }
  317. /*
  318. * Fix openssl compression bugs if necessary
  319. */
  320. static int init_tls_compression(void)
  321. {
  322. #if OPENSSL_VERSION_NUMBER >= 0x00908000L
  323. int n, r;
  324. STACK_OF(SSL_COMP)* comp_methods;
  325. SSL_COMP* zlib_comp;
  326. long ssl_version;
  327. /* disabling compression */
  328. # ifndef SSL_COMP_ZLIB_IDX
  329. # define SSL_COMP_ZLIB_IDX 1 /* openssl/ssl/ssl_ciph.c:84 */
  330. # endif
  331. comp_methods = SSL_COMP_get_compression_methods();
  332. if (comp_methods == 0) {
  333. LOG(L_INFO, "tls: init_tls: compression support disabled in the"
  334. " openssl lib\n");
  335. goto end; /* nothing to do, exit */
  336. } else if (cfg_get(tls, tls_cfg, disable_compression)){
  337. LOG(L_INFO, "tls: init_tls: disabling compression...\n");
  338. sk_SSL_COMP_zero(comp_methods);
  339. }else{
  340. ssl_version=SSLeay();
  341. /* replace openssl zlib compression with our version if necessary
  342. * (the openssl zlib compression uses the wrong malloc, see
  343. * openssl #1468): 0.9.8-dev < version <0.9.8e-beta1 */
  344. if ((ssl_version >= 0x00908000L) && (ssl_version < 0x00908051L)){
  345. /* the above SSL_COMP_get_compression_methods() call has the side
  346. * effect of initializing the compression stack (if not already
  347. * initialized) => after it zlib is initialized and in the stack */
  348. /* find zlib_comp (cannot use ssl3_comp_find, not exported) */
  349. n = sk_SSL_COMP_num(comp_methods);
  350. zlib_comp = 0;
  351. for (r = 0; r < n; r++) {
  352. zlib_comp = sk_SSL_COMP_value(comp_methods, r);
  353. DBG("tls: init_tls: found compression method %p id %d\n",
  354. zlib_comp, zlib_comp->id);
  355. if (zlib_comp->id == SSL_COMP_ZLIB_IDX) {
  356. DBG("tls: init_tls: found zlib compression (%d)\n",
  357. SSL_COMP_ZLIB_IDX);
  358. break /* found */;
  359. } else {
  360. zlib_comp = 0;
  361. }
  362. }
  363. if (zlib_comp == 0) {
  364. LOG(L_INFO, "tls: init_tls: no openssl zlib compression "
  365. "found\n");
  366. }else{
  367. LOG(L_WARN, "tls: init_tls: detected openssl lib with "
  368. "known zlib compression bug: \"%s\" (0x%08lx)\n",
  369. SSLeay_version(SSLEAY_VERSION), ssl_version);
  370. # ifdef TLS_FIX_ZLIB_COMPRESSION
  371. LOG(L_WARN, "tls: init_tls: enabling openssl zlib compression "
  372. "bug workaround (replacing zlib COMP method with "
  373. "our own version)\n");
  374. /* hack: make sure that the CRYPTO_EX_INDEX_COMP class is empty
  375. * and it does not contain any free_ex_data from the
  376. * built-in zlib. This can happen if the current openssl
  377. * zlib malloc fix patch is used (CRYPTO_get_ex_new_index() in
  378. * COMP_zlib()). Unfortunately the only way
  379. * to do this it to cleanup all the ex_data stuff.
  380. * It should be safe if this is executed before SSL_init()
  381. * (only the COMP class is initialized before).
  382. */
  383. CRYPTO_cleanup_all_ex_data();
  384. if (fixed_c_zlib_init() != 0) {
  385. LOG(L_CRIT, "tls: init_tls: BUG: failed to initialize zlib"
  386. " compression fix, disabling compression...\n");
  387. sk_SSL_COMP_zero(comp_methods); /* delete compression */
  388. goto end;
  389. }
  390. /* "fix" it */
  391. zlib_comp->method = &zlib_method;
  392. # else
  393. LOG(L_WARN, "tls: init_tls: disabling openssl zlib "
  394. "compression \n");
  395. zlib_comp=sk_SSL_COMP_delete(comp_methods, r);
  396. if (zlib_comp)
  397. OPENSSL_free(zlib_comp);
  398. # endif
  399. }
  400. }
  401. }
  402. end:
  403. #endif /* OPENSSL_VERSION_NUMBER >= 0.9.8 */
  404. return 0;
  405. }
  406. /**
  407. * tls pre-init function
  408. */
  409. int tls_pre_init(void)
  410. {
  411. /*
  412. * this has to be called before any function calling CRYPTO_malloc,
  413. * CRYPTO_malloc will set allow_customize in openssl to 0
  414. */
  415. #ifdef TLS_MALLOC_DBG
  416. if (!CRYPTO_set_mem_ex_functions(ser_malloc, ser_realloc, ser_free)) {
  417. #else
  418. if (!CRYPTO_set_mem_functions(ser_malloc, ser_realloc, ser_free)) {
  419. #endif
  420. ERR("Unable to set the memory allocation functions\n");
  421. return -1;
  422. }
  423. if (tls_init_locks()<0)
  424. return -1;
  425. init_tls_compression();
  426. return 0;
  427. }
  428. /*
  429. * First step of TLS initialization
  430. */
  431. int init_tls_h(void)
  432. {
  433. /*struct socket_info* si;*/
  434. long ssl_version;
  435. int lib_kerberos;
  436. int lib_zlib;
  437. int kerberos_support;
  438. int comp_support;
  439. const char* lib_cflags;
  440. int low_mem_threshold1;
  441. int low_mem_threshold2;
  442. str tls_grp;
  443. str s;
  444. cfg_ctx_t* cfg_ctx;
  445. #if OPENSSL_VERSION_NUMBER < 0x00907000L
  446. WARN("You are using an old version of OpenSSL (< 0.9.7). Upgrade!\n");
  447. #endif
  448. ssl_version=SSLeay();
  449. /* check if version have the same major minor and fix level
  450. * (e.g. 0.9.8a & 0.9.8c are ok, but 0.9.8 and 0.9.9x are not) */
  451. if ((ssl_version>>8)!=(OPENSSL_VERSION_NUMBER>>8)){
  452. LOG(L_CRIT, "ERROR: tls: init_tls_h: installed openssl library "
  453. "version is too different from the library the ser tls module "
  454. "was compiled with: installed \"%s\" (0x%08lx), compiled "
  455. "\"%s\" (0x%08lx).\n"
  456. " Please make sure a compatible version is used"
  457. " (tls_force_run in ser.cfg will override this check)\n",
  458. SSLeay_version(SSLEAY_VERSION), ssl_version,
  459. OPENSSL_VERSION_TEXT, (long)OPENSSL_VERSION_NUMBER);
  460. if (cfg_get(tls, tls_cfg, force_run))
  461. LOG(L_WARN, "tls: init_tls_h: tls_force_run turned on, ignoring "
  462. " openssl version mismatch\n");
  463. else
  464. return -1; /* safer to exit */
  465. }
  466. #ifdef TLS_KERBEROS_SUPPORT
  467. kerberos_support=1;
  468. #else
  469. kerberos_support=0;
  470. #endif
  471. #ifdef TLS_COMP_SUPPORT
  472. comp_support=1;
  473. #else
  474. comp_support=0;
  475. #endif
  476. /* attempt to guess if the library was compiled with kerberos or
  477. * compression support from the cflags */
  478. lib_cflags=SSLeay_version(SSLEAY_CFLAGS);
  479. lib_kerberos=0;
  480. lib_zlib=0;
  481. if ((lib_cflags==0) || strstr(lib_cflags, "not available")){
  482. lib_kerberos=-1;
  483. lib_zlib=-1;
  484. }else{
  485. if (strstr(lib_cflags, "-DZLIB"))
  486. lib_zlib=1;
  487. if (strstr(lib_cflags, "-DKRB5_"))
  488. lib_kerberos=1;
  489. }
  490. LOG(L_INFO, "tls: _init_tls_h: compiled with openssl version "
  491. "\"%s\" (0x%08lx), kerberos support: %s, compression: %s\n",
  492. OPENSSL_VERSION_TEXT, (long)OPENSSL_VERSION_NUMBER,
  493. kerberos_support?"on":"off", comp_support?"on":"off");
  494. LOG(L_INFO, "tls: init_tls_h: installed openssl library version "
  495. "\"%s\" (0x%08lx), kerberos support: %s, "
  496. " zlib compression: %s"
  497. "\n %s\n",
  498. SSLeay_version(SSLEAY_VERSION), ssl_version,
  499. (lib_kerberos==1)?"on":(lib_kerberos==0)?"off":"unknown",
  500. (lib_zlib==1)?"on":(lib_zlib==0)?"off":"unknown",
  501. SSLeay_version(SSLEAY_CFLAGS));
  502. if (lib_kerberos!=kerberos_support){
  503. if (lib_kerberos!=-1){
  504. LOG(L_CRIT, "ERROR: tls: init_tls_h: openssl compile options"
  505. " mismatch: library has kerberos support"
  506. " %s and ser tls %s (unstable configuration)\n"
  507. " (tls_force_run in ser.cfg will override this"
  508. " check)\n",
  509. lib_kerberos?"enabled":"disabled",
  510. kerberos_support?"enabled":"disabled"
  511. );
  512. if (cfg_get(tls, tls_cfg, force_run))
  513. LOG(L_WARN, "tls: init_tls_h: tls_force_run turned on, "
  514. "ignoring kerberos support mismatch\n");
  515. else
  516. return -1; /* exit, is safer */
  517. }else{
  518. LOG(L_WARN, "WARNING: tls: init_tls_h: openssl compile options"
  519. " missing -- cannot detect if kerberos support is"
  520. " enabled. Possible unstable configuration\n");
  521. }
  522. }
  523. #ifdef TLS_KSSL_WORKARROUND
  524. /* if openssl compiled with kerberos support, and openssl < 0.9.8e-dev
  525. * or openssl between 0.9.9-dev and 0.9.9-beta1 apply workaround for
  526. * openssl bug #1467 */
  527. if (ssl_version < 0x00908050L ||
  528. (ssl_version >= 0x00909000L && ssl_version < 0x00909001L)){
  529. openssl_kssl_malloc_bug=1;
  530. LOG(L_WARN, "tls: init_tls_h: openssl kerberos malloc bug detected, "
  531. " kerberos support will be disabled...\n");
  532. }
  533. #endif
  534. /* set free memory threshold for openssl bug #1491 workaround */
  535. low_mem_threshold1 = cfg_get(tls, tls_cfg, low_mem_threshold1);
  536. low_mem_threshold2 = cfg_get(tls, tls_cfg, low_mem_threshold2);
  537. if (low_mem_threshold1<0){
  538. /* default */
  539. low_mem_threshold1=512*1024*get_max_procs();
  540. }else
  541. low_mem_threshold1*=1024; /* KB */
  542. if (low_mem_threshold2<0){
  543. /* default */
  544. low_mem_threshold2=256*1024*get_max_procs();
  545. }else
  546. low_mem_threshold2*=1024; /* KB */
  547. if ((low_mem_threshold1==0) || (low_mem_threshold2==0))
  548. LOG(L_WARN, "tls: openssl bug #1491 (crash/mem leaks on low memory)"
  549. " workarround disabled\n");
  550. else
  551. LOG(L_WARN, "tls: openssl bug #1491 (crash/mem leaks on low memory)"
  552. " workaround enabled (on low memory tls operations will fail"
  553. " preemptively) with free memory thresholds %d and %d bytes\n",
  554. low_mem_threshold1, low_mem_threshold2);
  555. if (shm_available()==(unsigned long)(-1)){
  556. LOG(L_WARN, "tls: ser compiled without MALLOC_STATS support:"
  557. " the workaround for low mem. openssl bugs will _not_ "
  558. "work\n");
  559. low_mem_threshold1=0;
  560. low_mem_threshold2=0;
  561. }
  562. if ((low_mem_threshold1 != cfg_get(tls, tls_cfg, low_mem_threshold1)) ||
  563. (low_mem_threshold2 != cfg_get(tls, tls_cfg, low_mem_threshold2))) {
  564. /* ugly hack to set the initial values for the mem tresholds */
  565. if (cfg_register_ctx(&cfg_ctx, 0)) {
  566. ERR("failed to register cfg context\n");
  567. return -1;
  568. }
  569. tls_grp.s = "tls";
  570. tls_grp.len = strlen(tls_grp.s);
  571. s.s = "low_mem_threshold1";
  572. s.len = strlen(s.s);
  573. if (low_mem_threshold1 != cfg_get(tls, tls_cfg, low_mem_threshold1) &&
  574. cfg_set_now_int(cfg_ctx, &tls_grp, NULL /* group id */, &s, low_mem_threshold1)) {
  575. ERR("failed to set tls.low_mem_threshold1 to %d\n",
  576. low_mem_threshold1);
  577. return -1;
  578. }
  579. s.s = "low_mem_threshold2";
  580. s.len = strlen(s.s);
  581. if (low_mem_threshold2 != cfg_get(tls, tls_cfg, low_mem_threshold2) &&
  582. cfg_set_now_int(cfg_ctx, &tls_grp, NULL /* group id */, &s, low_mem_threshold2)) {
  583. ERR("failed to set tls.low_mem_threshold1 to %d\n",
  584. low_mem_threshold2);
  585. return -1;
  586. }
  587. }
  588. SSL_library_init();
  589. SSL_load_error_strings();
  590. init_ssl_methods();
  591. tls_mod_initialized = 1;
  592. return 0;
  593. }
  594. /*
  595. * Make sure that all server domains in the configuration have corresponding
  596. * listening socket in SER
  597. */
  598. int tls_check_sockets(tls_domains_cfg_t* cfg)
  599. {
  600. tls_domain_t* d;
  601. if (!cfg) return 0;
  602. d = cfg->srv_list;
  603. while(d) {
  604. if (d->ip.len && !find_si(&d->ip, d->port, PROTO_TLS)) {
  605. ERR("%s: No listening socket found\n", tls_domain_str(d));
  606. return -1;
  607. }
  608. d = d->next;
  609. }
  610. return 0;
  611. }
  612. /*
  613. * TLS cleanup when SER exits
  614. */
  615. void destroy_tls_h(void)
  616. {
  617. DBG("tls module final tls destroy\n");
  618. if(tls_mod_initialized > 0)
  619. ERR_free_strings();
  620. /* TODO: free all the ctx'es */
  621. tls_destroy_cfg();
  622. tls_destroy_locks();
  623. tls_ct_wq_destroy();
  624. }