tls_mod.c 13 KB

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