tls_init.c 20 KB

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