tls_mod.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /*
  2. * $Id$
  3. *
  4. * TLS module - module interface
  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. * History:
  27. * -------
  28. * 2003-03-11: New module interface (janakj)
  29. * 2003-03-16: flags export parameter added (janakj)
  30. * 2003-04-05: default_uri #define used (jiri)
  31. * 2003-04-06: db connection closed in mod_init (janakj)
  32. * 2004-06-06 updated to the new DB api, cleanup: static dbf & handler,
  33. * calls to domain_db_{bind,init,close,ver} (andrei)
  34. * 2007-02-09 updated to the new tls_hooks api and renamed tls hooks hanlder
  35. * functions to avoid conflicts: s/tls_/tls_h_/ (andrei)
  36. * 2010-03-19 new parameters to control advanced openssl lib options
  37. * (mostly work on 1.0.0+): ssl_release_buffers, ssl_read_ahead,
  38. * ssl_freelist_max_len, ssl_max_send_fragment (andrei)
  39. * 2010-05-27 migrated to the runtime cfg framework (andrei)
  40. */
  41. /** SIP-router TLS support :: Module interface.
  42. * @file
  43. * @ingroup tls
  44. * Module: @ref tls
  45. */
  46. #include <sys/types.h>
  47. #include <sys/socket.h>
  48. #include <arpa/inet.h>
  49. #include "../../locking.h"
  50. #include "../../sr_module.h"
  51. #include "../../ip_addr.h"
  52. #include "../../trim.h"
  53. #include "../../globals.h"
  54. #include "../../timer_ticks.h"
  55. #include "../../timer.h" /* ticks_t */
  56. #include "../../tls_hooks.h"
  57. #include "../../ut.h"
  58. #include "../../shm_init.h"
  59. #include "../../rpc_lookup.h"
  60. #include "../../cfg/cfg.h"
  61. #include "tls_init.h"
  62. #include "tls_server.h"
  63. #include "tls_domain.h"
  64. #include "tls_select.h"
  65. #include "tls_config.h"
  66. #include "tls_rpc.h"
  67. #include "tls_util.h"
  68. #include "tls_mod.h"
  69. #include "tls_cfg.h"
  70. #ifndef TLS_HOOKS
  71. #error "TLS_HOOKS must be defined, or the tls module won't work"
  72. #endif
  73. #ifdef CORE_TLS
  74. #error "conflict: CORE_TLS must _not_ be defined"
  75. #endif
  76. /*
  77. * FIXME:
  78. * - How do we ask for secret key password ? Mod_init is called after
  79. * daemonize and thus has no console access
  80. * - forward_tls and t_relay_to_tls should be here
  81. * add tls_log
  82. * - Currently it is not possible to reset certificate in a domain,
  83. * for example if you specify client certificate in the default client
  84. * domain then there is no way to define another client domain which would
  85. * have no client certificate configured
  86. */
  87. /*
  88. * Module management function prototypes
  89. */
  90. static int mod_init(void);
  91. static int mod_child(int rank);
  92. static void destroy(void);
  93. static int is_peer_verified(struct sip_msg* msg, char* foo, char* foo2);
  94. MODULE_VERSION
  95. /*
  96. * Default settings when modparams are used
  97. */
  98. static tls_domain_t mod_params = {
  99. TLS_DOMAIN_DEF | TLS_DOMAIN_SRV, /* Domain Type */
  100. {}, /* IP address */
  101. 0, /* Port number */
  102. 0, /* SSL ctx */
  103. STR_STATIC_INIT(TLS_CERT_FILE), /* Certificate file */
  104. STR_STATIC_INIT(TLS_PKEY_FILE), /* Private key file */
  105. 0, /* Verify certificate */
  106. 9, /* Verify depth */
  107. STR_STATIC_INIT(TLS_CA_FILE), /* CA file */
  108. 0, /* Require certificate */
  109. {0, }, /* Cipher list */
  110. TLS_USE_TLSv1, /* TLS method */
  111. STR_STATIC_INIT(TLS_CRL_FILE), /* Certificate revocation list */
  112. 0 /* next */
  113. };
  114. /*
  115. * Default settings for server domains when using external config file
  116. */
  117. tls_domain_t srv_defaults = {
  118. TLS_DOMAIN_DEF | TLS_DOMAIN_SRV, /* Domain Type */
  119. {}, /* IP address */
  120. 0, /* Port number */
  121. 0, /* SSL ctx */
  122. STR_STATIC_INIT(TLS_CERT_FILE), /* Certificate file */
  123. STR_STATIC_INIT(TLS_PKEY_FILE), /* Private key file */
  124. 0, /* Verify certificate */
  125. 9, /* Verify depth */
  126. STR_STATIC_INIT(TLS_CA_FILE), /* CA file */
  127. 0, /* Require certificate */
  128. {0, 0}, /* Cipher list */
  129. TLS_USE_TLSv1, /* TLS method */
  130. STR_STATIC_INIT(TLS_CRL_FILE), /* Certificate revocation list */
  131. 0 /* next */
  132. };
  133. /*
  134. * Default settings for client domains when using external config file
  135. */
  136. tls_domain_t cli_defaults = {
  137. TLS_DOMAIN_DEF | TLS_DOMAIN_CLI, /* Domain Type */
  138. {}, /* IP address */
  139. 0, /* Port number */
  140. 0, /* SSL ctx */
  141. {0, 0}, /* Certificate file */
  142. {0, 0}, /* Private key file */
  143. 0, /* Verify certificate */
  144. 9, /* Verify depth */
  145. STR_STATIC_INIT(TLS_CA_FILE), /* CA file */
  146. 0, /* Require certificate */
  147. {0, 0}, /* Cipher list */
  148. TLS_USE_TLSv1, /* TLS method */
  149. {0, 0}, /* Certificate revocation list */
  150. 0 /* next */
  151. };
  152. /* Current TLS configuration */
  153. tls_domains_cfg_t** tls_domains_cfg = NULL;
  154. /* List lock, used by garbage collector */
  155. gen_lock_t* tls_domains_cfg_lock = NULL;
  156. int sr_tls_renegotiation = 0;
  157. /*
  158. * Exported functions
  159. */
  160. static cmd_export_t cmds[] = {
  161. {"is_peer_verified", (cmd_function)is_peer_verified, 0, 0, 0,
  162. REQUEST_ROUTE},
  163. {0,0,0,0,0,0}
  164. };
  165. /*
  166. * Exported parameters
  167. */
  168. static param_export_t params[] = {
  169. {"tls_method", PARAM_STR, &default_tls_cfg.method },
  170. {"verify_certificate", PARAM_INT, &default_tls_cfg.verify_cert },
  171. {"verify_depth", PARAM_INT, &default_tls_cfg.verify_depth },
  172. {"require_certificate", PARAM_INT, &default_tls_cfg.require_cert },
  173. {"private_key", PARAM_STR, &default_tls_cfg.private_key },
  174. {"ca_list", PARAM_STR, &default_tls_cfg.ca_list },
  175. {"certificate", PARAM_STR, &default_tls_cfg.certificate },
  176. {"crl", PARAM_STR, &default_tls_cfg.crl },
  177. {"cipher_list", PARAM_STR, &default_tls_cfg.cipher_list },
  178. {"connection_timeout", PARAM_INT, &default_tls_cfg.con_lifetime },
  179. {"tls_log", PARAM_INT, &default_tls_cfg.log },
  180. {"tls_debug", PARAM_INT, &default_tls_cfg.debug },
  181. {"session_cache", PARAM_INT, &default_tls_cfg.session_cache},
  182. {"session_id", PARAM_STR, &default_tls_cfg.session_id },
  183. {"config", PARAM_STR, &default_tls_cfg.config_file },
  184. {"tls_disable_compression", PARAM_INT,
  185. &default_tls_cfg.disable_compression},
  186. {"ssl_release_buffers", PARAM_INT, &default_tls_cfg.ssl_release_buffers},
  187. {"ssl_freelist_max_len", PARAM_INT, &default_tls_cfg.ssl_freelist_max},
  188. {"ssl_max_send_fragment", PARAM_INT,
  189. &default_tls_cfg.ssl_max_send_fragment},
  190. {"ssl_read_ahead", PARAM_INT, &default_tls_cfg.ssl_read_ahead},
  191. {"send_close_notify", PARAM_INT, &default_tls_cfg.send_close_notify},
  192. {"con_ct_wq_max", PARAM_INT, &default_tls_cfg.con_ct_wq_max},
  193. {"ct_wq_max", PARAM_INT, &default_tls_cfg.ct_wq_max},
  194. {"ct_wq_blk_size", PARAM_INT, &default_tls_cfg.ct_wq_blk_size},
  195. {"tls_force_run", PARAM_INT, &default_tls_cfg.force_run},
  196. {"low_mem_threshold1", PARAM_INT, &default_tls_cfg.low_mem_threshold1},
  197. {"low_mem_threshold2", PARAM_INT, &default_tls_cfg.low_mem_threshold2},
  198. {"renegotiation", PARAM_INT, &sr_tls_renegotiation},
  199. {0, 0, 0}
  200. };
  201. /*
  202. * Module interface
  203. */
  204. struct module_exports exports = {
  205. "tls",
  206. DEFAULT_DLFLAGS, /* dlopen flags */
  207. cmds, /* Exported functions */
  208. params, /* Exported parameters */
  209. 0, /* exported statistics */
  210. 0, /* exported MI functions */
  211. tls_pv, /* exported pseudo-variables */
  212. 0, /* extra processes */
  213. mod_init, /* module initialization function */
  214. 0, /* response function */
  215. destroy, /* destroy function */
  216. mod_child /* child initialization function */
  217. };
  218. static struct tls_hooks tls_h = {
  219. tls_read_f,
  220. tls_encode_f,
  221. tls_h_tcpconn_init,
  222. tls_h_tcpconn_clean,
  223. tls_h_close,
  224. tls_h_init_si,
  225. init_tls_h,
  226. destroy_tls_h
  227. };
  228. #if 0
  229. /*
  230. * Create TLS configuration from modparams
  231. */
  232. static tls_domains_cfg_t* tls_use_modparams(void)
  233. {
  234. tls_domains_cfg_t* ret;
  235. ret = tls_new_cfg();
  236. if (!ret) return;
  237. }
  238. #endif
  239. int mod_register(char *path, int *dlflags, void *p1, void *p2)
  240. {
  241. /* shm is used, be sure it is initialized */
  242. if(!shm_initialized() && init_shm()<0)
  243. return -1;
  244. if(tls_pre_init()<0)
  245. return -1;
  246. return 0;
  247. }
  248. static int mod_init(void)
  249. {
  250. int method;
  251. if (tls_disable){
  252. LOG(L_WARN, "WARNING: tls: mod_init: tls support is disabled "
  253. "(set enable_tls=1 in the config to enable it)\n");
  254. return 0;
  255. }
  256. if (fix_tls_cfg(&default_tls_cfg) < 0 ) {
  257. ERR("initial tls configuration fixup failed\n");
  258. return -1;
  259. }
  260. /* declare configuration */
  261. if (cfg_declare("tls", tls_cfg_def, &default_tls_cfg,
  262. cfg_sizeof(tls), &tls_cfg)) {
  263. ERR("failed to register the configuration\n");
  264. return -1;
  265. }
  266. /* Convert tls_method parameter to integer */
  267. method = tls_parse_method(&cfg_get(tls, tls_cfg, method));
  268. if (method < 0) {
  269. ERR("Invalid tls_method parameter value\n");
  270. return -1;
  271. }
  272. /* fill mod_params */
  273. mod_params.method = method;
  274. mod_params.verify_cert = cfg_get(tls, tls_cfg, verify_cert);
  275. mod_params.verify_depth = cfg_get(tls, tls_cfg, verify_depth);
  276. mod_params.require_cert = cfg_get(tls, tls_cfg, require_cert);
  277. mod_params.pkey_file = cfg_get(tls, tls_cfg, private_key);
  278. mod_params.ca_file = cfg_get(tls, tls_cfg, ca_list);
  279. mod_params.crl_file = cfg_get(tls, tls_cfg, crl);
  280. mod_params.cert_file = cfg_get(tls, tls_cfg, certificate);
  281. mod_params.cipher_list = cfg_get(tls, tls_cfg, cipher_list);
  282. tls_domains_cfg =
  283. (tls_domains_cfg_t**)shm_malloc(sizeof(tls_domains_cfg_t*));
  284. if (!tls_domains_cfg) {
  285. ERR("Not enough shared memory left\n");
  286. goto error;
  287. }
  288. *tls_domains_cfg = NULL;
  289. register_tls_hooks(&tls_h);
  290. register_select_table(tls_sel);
  291. /* register the rpc interface */
  292. if (rpc_register_array(tls_rpc)!=0) {
  293. LOG(L_ERR, "failed to register RPC commands\n");
  294. goto error;
  295. }
  296. /* if (init_tls() < 0) return -1; */
  297. tls_domains_cfg_lock = lock_alloc();
  298. if (tls_domains_cfg_lock == 0) {
  299. ERR("Unable to create TLS configuration lock\n");
  300. goto error;
  301. }
  302. if (lock_init(tls_domains_cfg_lock) == 0) {
  303. lock_dealloc(tls_domains_cfg_lock);
  304. ERR("Unable to initialize TLS configuration lock\n");
  305. goto error;
  306. }
  307. if (tls_ct_wq_init() < 0) {
  308. ERR("Unable to initialize TLS buffering\n");
  309. goto error;
  310. }
  311. if (cfg_get(tls, tls_cfg, config_file).s) {
  312. *tls_domains_cfg =
  313. tls_load_config(&cfg_get(tls, tls_cfg, config_file));
  314. if (!(*tls_domains_cfg)) goto error;
  315. } else {
  316. *tls_domains_cfg = tls_new_cfg();
  317. if (!(*tls_domains_cfg)) goto error;
  318. }
  319. if (tls_check_sockets(*tls_domains_cfg) < 0)
  320. goto error;
  321. return 0;
  322. error:
  323. destroy_tls_h();
  324. return -1;
  325. }
  326. static int mod_child(int rank)
  327. {
  328. if (tls_disable || (tls_domains_cfg==0))
  329. return 0;
  330. /* fix tls config only from the main proc/PROC_INIT., when we know
  331. * the exact process number and before any other process starts*/
  332. if (rank == PROC_INIT){
  333. if (cfg_get(tls, tls_cfg, config_file).s){
  334. if (tls_fix_domains_cfg(*tls_domains_cfg,
  335. &srv_defaults, &cli_defaults) < 0)
  336. return -1;
  337. }else{
  338. if (tls_fix_domains_cfg(*tls_domains_cfg,
  339. &mod_params, &mod_params) < 0)
  340. return -1;
  341. }
  342. }
  343. return 0;
  344. }
  345. static void destroy(void)
  346. {
  347. /* tls is destroyed via the registered destroy_tls_h callback
  348. => nothing to do here */
  349. }
  350. static int is_peer_verified(struct sip_msg* msg, char* foo, char* foo2)
  351. {
  352. struct tcp_connection *c;
  353. SSL *ssl;
  354. long ssl_verify;
  355. X509 *x509_cert;
  356. DBG("started...\n");
  357. if (msg->rcv.proto != PROTO_TLS) {
  358. ERR("proto != TLS --> peer can't be verified, return -1\n");
  359. return -1;
  360. }
  361. DBG("trying to find TCP connection of received message...\n");
  362. c = tcpconn_get(msg->rcv.proto_reserved1, 0, 0, 0,
  363. cfg_get(tls, tls_cfg, con_lifetime));
  364. if (c && c->type != PROTO_TLS) {
  365. ERR("Connection found but is not TLS\n");
  366. tcpconn_put(c);
  367. return -1;
  368. }
  369. if (!c->extra_data) {
  370. LM_ERR("no extra_data specified in TLS/TCP connection found."
  371. " This should not happen... return -1\n");
  372. tcpconn_put(c);
  373. return -1;
  374. }
  375. ssl = ((struct tls_extra_data*)c->extra_data)->ssl;
  376. ssl_verify = SSL_get_verify_result(ssl);
  377. if ( ssl_verify != X509_V_OK ) {
  378. LM_WARN("verification of presented certificate failed... return -1\n");
  379. tcpconn_put(c);
  380. return -1;
  381. }
  382. /* now, we have only valid peer certificates or peers without certificates.
  383. * Thus we have to check for the existence of a peer certificate
  384. */
  385. x509_cert = SSL_get_peer_certificate(ssl);
  386. if ( x509_cert == NULL ) {
  387. LM_INFO("tlsops:is_peer_verified: WARNING: peer did not present "
  388. "a certificate. Thus it could not be verified... return -1\n");
  389. tcpconn_put(c);
  390. return -1;
  391. }
  392. X509_free(x509_cert);
  393. tcpconn_put(c);
  394. LM_DBG("tlsops:is_peer_verified: peer is successfully verified"
  395. "...done\n");
  396. return 1;
  397. }