tls_domain.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396
  1. /*
  2. * TLS module
  3. *
  4. * Copyright (C) 2005,2006 iptelorg GmbH
  5. * Copyright (C) 2013 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. /**
  20. * Kamailio TLS support :: Virtual domain configuration support
  21. * @file
  22. * @ingroup tls
  23. * Module: @ref tls
  24. */
  25. #include <stdlib.h>
  26. #include <openssl/ssl.h>
  27. #include <openssl/opensslv.h>
  28. #if OPENSSL_VERSION_NUMBER >= 0x00907000L
  29. # include <openssl/ui.h>
  30. #endif
  31. #include "../../ut.h"
  32. #include "../../mem/shm_mem.h"
  33. #include "../../pt.h"
  34. #include "../../cfg/cfg.h"
  35. #include "../../dprint.h"
  36. #include "tls_config.h"
  37. #include "tls_server.h"
  38. #include "tls_util.h"
  39. #include "tls_mod.h"
  40. #include "tls_init.h"
  41. #include "tls_domain.h"
  42. #include "tls_cfg.h"
  43. /*
  44. * ECDHE is enabled only on OpenSSL 1.0.0e and later.
  45. * See http://www.openssl.org/news/secadv_20110906.txt
  46. * for details.
  47. */
  48. #ifndef OPENSSL_NO_ECDH
  49. static void setup_ecdh(SSL_CTX *ctx)
  50. {
  51. EC_KEY *ecdh;
  52. if (SSLeay() < 0x1000005fL) {
  53. return;
  54. }
  55. ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
  56. SSL_CTX_set_options(ctx, SSL_OP_SINGLE_ECDH_USE);
  57. SSL_CTX_set_tmp_ecdh(ctx, ecdh);
  58. EC_KEY_free(ecdh);
  59. }
  60. #endif
  61. #ifndef OPENSSL_NO_DH
  62. static unsigned char dh3072_p[] = {
  63. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xC9,0x0F,0xDA,0xA2,
  64. 0x21,0x68,0xC2,0x34,0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
  65. 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,0x02,0x0B,0xBE,0xA6,
  66. 0x3B,0x13,0x9B,0x22,0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
  67. 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,0x30,0x2B,0x0A,0x6D,
  68. 0xF2,0x5F,0x14,0x37,0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
  69. 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,0xF4,0x4C,0x42,0xE9,
  70. 0xA6,0x37,0xED,0x6B,0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
  71. 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,0xAE,0x9F,0x24,0x11,
  72. 0x7C,0x4B,0x1F,0xE6,0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
  73. 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,0x98,0xDA,0x48,0x36,
  74. 0x1C,0x55,0xD3,0x9A,0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
  75. 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,0x1C,0x62,0xF3,0x56,
  76. 0x20,0x85,0x52,0xBB,0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
  77. 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,0xF1,0x74,0x6C,0x08,
  78. 0xCA,0x18,0x21,0x7C,0x32,0x90,0x5E,0x46,0x2E,0x36,0xCE,0x3B,
  79. 0xE3,0x9E,0x77,0x2C,0x18,0x0E,0x86,0x03,0x9B,0x27,0x83,0xA2,
  80. 0xEC,0x07,0xA2,0x8F,0xB5,0xC5,0x5D,0xF0,0x6F,0x4C,0x52,0xC9,
  81. 0xDE,0x2B,0xCB,0xF6,0x95,0x58,0x17,0x18,0x39,0x95,0x49,0x7C,
  82. 0xEA,0x95,0x6A,0xE5,0x15,0xD2,0x26,0x18,0x98,0xFA,0x05,0x10,
  83. 0x15,0x72,0x8E,0x5A,0x8A,0xAA,0xC4,0x2D,0xAD,0x33,0x17,0x0D,
  84. 0x04,0x50,0x7A,0x33,0xA8,0x55,0x21,0xAB,0xDF,0x1C,0xBA,0x64,
  85. 0xEC,0xFB,0x85,0x04,0x58,0xDB,0xEF,0x0A,0x8A,0xEA,0x71,0x57,
  86. 0x5D,0x06,0x0C,0x7D,0xB3,0x97,0x0F,0x85,0xA6,0xE1,0xE4,0xC7,
  87. 0xAB,0xF5,0xAE,0x8C,0xDB,0x09,0x33,0xD7,0x1E,0x8C,0x94,0xE0,
  88. 0x4A,0x25,0x61,0x9D,0xCE,0xE3,0xD2,0x26,0x1A,0xD2,0xEE,0x6B,
  89. 0xF1,0x2F,0xFA,0x06,0xD9,0x8A,0x08,0x64,0xD8,0x76,0x02,0x73,
  90. 0x3E,0xC8,0x6A,0x64,0x52,0x1F,0x2B,0x18,0x17,0x7B,0x20,0x0C,
  91. 0xBB,0xE1,0x17,0x57,0x7A,0x61,0x5D,0x6C,0x77,0x09,0x88,0xC0,
  92. 0xBA,0xD9,0x46,0xE2,0x08,0xE2,0x4F,0xA0,0x74,0xE5,0xAB,0x31,
  93. 0x43,0xDB,0x5B,0xFC,0xE0,0xFD,0x10,0x8E,0x4B,0x82,0xD1,0x20,
  94. 0xA9,0x3A,0xD2,0xCA,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
  95. };
  96. static unsigned char dh3072_g[] = { 0x02 };
  97. static void setup_dh(SSL_CTX *ctx)
  98. {
  99. DH *dh;
  100. dh = DH_new();
  101. if (dh == NULL) {
  102. return;
  103. }
  104. dh->p = BN_bin2bn(dh3072_p, sizeof(dh3072_p), NULL);
  105. dh->g = BN_bin2bn(dh3072_g, sizeof(dh3072_g), NULL);
  106. if (dh->p == NULL || dh->g == NULL) {
  107. DH_free(dh);
  108. return;
  109. }
  110. SSL_CTX_set_options(ctx, SSL_OP_SINGLE_DH_USE);
  111. SSL_CTX_set_tmp_dh(ctx, dh);
  112. DH_free(dh);
  113. }
  114. #endif
  115. /**
  116. * @brief Create a new TLS domain structure
  117. *
  118. * Create a new domain structure in new allocated shared memory.
  119. * @param type domain Type
  120. * @param ip domain IP
  121. * @param port domain port
  122. * @return new domain
  123. */
  124. tls_domain_t* tls_new_domain(int type, struct ip_addr *ip, unsigned short port)
  125. {
  126. tls_domain_t* d;
  127. d = shm_malloc(sizeof(tls_domain_t));
  128. if (d == NULL) {
  129. ERR("Memory allocation failure\n");
  130. return 0;
  131. }
  132. memset(d, '\0', sizeof(tls_domain_t));
  133. d->type = type;
  134. if (ip) memcpy(&d->ip, ip, sizeof(struct ip_addr));
  135. d->port = port;
  136. d->verify_cert = -1;
  137. d->verify_depth = -1;
  138. d->require_cert = -1;
  139. return d;
  140. }
  141. /**
  142. * @brief Free all memory used by TLS configuration domain
  143. * @param d freed domain
  144. */
  145. void tls_free_domain(tls_domain_t* d)
  146. {
  147. int i;
  148. int procs_no;
  149. if (!d) return;
  150. if (d->ctx) {
  151. procs_no=get_max_procs();
  152. for(i = 0; i < procs_no; i++) {
  153. if (d->ctx[i]) SSL_CTX_free(d->ctx[i]);
  154. }
  155. shm_free(d->ctx);
  156. }
  157. if (d->cipher_list.s) shm_free(d->cipher_list.s);
  158. if (d->ca_file.s) shm_free(d->ca_file.s);
  159. if (d->crl_file.s) shm_free(d->crl_file.s);
  160. if (d->pkey_file.s) shm_free(d->pkey_file.s);
  161. if (d->cert_file.s) shm_free(d->cert_file.s);
  162. shm_free(d);
  163. }
  164. /**
  165. * @brief Free TLS configuration structure
  166. * @param cfg freed configuration
  167. */
  168. void tls_free_cfg(tls_domains_cfg_t* cfg)
  169. {
  170. tls_domain_t* p;
  171. while(cfg->srv_list) {
  172. p = cfg->srv_list;
  173. cfg->srv_list = cfg->srv_list->next;
  174. tls_free_domain(p);
  175. }
  176. while(cfg->cli_list) {
  177. p = cfg->cli_list;
  178. cfg->cli_list = cfg->cli_list->next;
  179. tls_free_domain(p);
  180. }
  181. if (cfg->srv_default) tls_free_domain(cfg->srv_default);
  182. if (cfg->cli_default) tls_free_domain(cfg->cli_default);
  183. shm_free(cfg);
  184. }
  185. /**
  186. * @brief Destroy all TLS configuration data
  187. */
  188. void tls_destroy_cfg(void)
  189. {
  190. tls_domains_cfg_t* ptr;
  191. if (tls_domains_cfg_lock) {
  192. lock_destroy(tls_domains_cfg_lock);
  193. lock_dealloc(tls_domains_cfg_lock);
  194. tls_domains_cfg_lock = 0;
  195. }
  196. if (tls_domains_cfg) {
  197. while(*tls_domains_cfg) {
  198. ptr = *tls_domains_cfg;
  199. *tls_domains_cfg = (*tls_domains_cfg)->next;
  200. tls_free_cfg(ptr);
  201. }
  202. shm_free(tls_domains_cfg);
  203. tls_domains_cfg = 0;
  204. }
  205. }
  206. /**
  207. * @brief Generate TLS domain identifier
  208. * @param d printed domain
  209. * @return printed domain, with zero termination
  210. */
  211. char* tls_domain_str(tls_domain_t* d)
  212. {
  213. static char buf[1024];
  214. char* p;
  215. buf[0] = '\0';
  216. p = buf;
  217. p = strcat(p, d->type & TLS_DOMAIN_SRV ? "TLSs<" : "TLSc<");
  218. if (d->type & TLS_DOMAIN_DEF) {
  219. p = strcat(p, "default>");
  220. } else {
  221. p = strcat(p, ip_addr2a(&d->ip));
  222. p = strcat(p, ":");
  223. p = strcat(p, int2str(d->port, 0));
  224. p = strcat(p, ">");
  225. }
  226. return buf;
  227. }
  228. /**
  229. * @brief Initialize TLS domain parameters that have not been configured yet
  230. *
  231. * Initialize TLS domain parameters that have not been configured from
  232. * parent domain (usually one of default domains)
  233. * @param d initialized domain
  234. * @param parent parent domain
  235. * @return 0 on success, -1 on error
  236. */
  237. static int fill_missing(tls_domain_t* d, tls_domain_t* parent)
  238. {
  239. if (d->method == TLS_METHOD_UNSPEC) d->method = parent->method;
  240. LOG(L_INFO, "%s: tls_method=%d\n", tls_domain_str(d), d->method);
  241. if (d->method < 1 || d->method >= TLS_METHOD_MAX) {
  242. ERR("%s: Invalid TLS method value\n", tls_domain_str(d));
  243. return -1;
  244. }
  245. if (!d->cert_file.s) {
  246. if (shm_asciiz_dup(&d->cert_file.s, parent->cert_file.s) < 0)
  247. return -1;
  248. d->cert_file.len = parent->cert_file.len;
  249. }
  250. LOG(L_INFO, "%s: certificate='%s'\n", tls_domain_str(d), d->cert_file.s);
  251. if (!d->ca_file.s){
  252. if (shm_asciiz_dup(&d->ca_file.s, parent->ca_file.s) < 0)
  253. return -1;
  254. d->ca_file.len = parent->ca_file.len;
  255. }
  256. LOG(L_INFO, "%s: ca_list='%s'\n", tls_domain_str(d), d->ca_file.s);
  257. if (!d->crl_file.s) {
  258. if (shm_asciiz_dup(&d->crl_file.s, parent->crl_file.s) < 0)
  259. return -1;
  260. d->crl_file.len = parent->crl_file.len;
  261. }
  262. LOG(L_INFO, "%s: crl='%s'\n", tls_domain_str(d), d->crl_file.s);
  263. if (d->require_cert == -1) d->require_cert = parent->require_cert;
  264. LOG(L_INFO, "%s: require_certificate=%d\n", tls_domain_str(d),
  265. d->require_cert);
  266. if (!d->cipher_list.s) {
  267. if ( shm_asciiz_dup(&d->cipher_list.s, parent->cipher_list.s) < 0)
  268. return -1;
  269. d->cipher_list.len = parent->cipher_list.len;
  270. }
  271. LOG(L_INFO, "%s: cipher_list='%s'\n", tls_domain_str(d), d->cipher_list.s);
  272. if (!d->pkey_file.s) {
  273. if (shm_asciiz_dup(&d->pkey_file.s, parent->pkey_file.s) < 0)
  274. return -1;
  275. d->pkey_file.len = parent->pkey_file.len;
  276. }
  277. LOG(L_INFO, "%s: private_key='%s'\n", tls_domain_str(d), d->pkey_file.s);
  278. if (d->verify_cert == -1) d->verify_cert = parent->verify_cert;
  279. LOG(L_INFO, "%s: verify_certificate=%d\n", tls_domain_str(d),
  280. d->verify_cert);
  281. if (d->verify_depth == -1) d->verify_depth = parent->verify_depth;
  282. LOG(L_INFO, "%s: verify_depth=%d\n", tls_domain_str(d), d->verify_depth);
  283. return 0;
  284. }
  285. /**
  286. * @brief Called for ctx, with 2 args
  287. * @param ctx SSL context
  288. * @param larg ?
  289. * @param parg ?
  290. * @return return 0 on succes, <0 on critical error
  291. */
  292. typedef int (*per_ctx_cbk_f)(SSL_CTX* ctx, long larg, void* parg);
  293. /**
  294. * @brief Execute callback on all the CTX'es on a domain
  295. * @param d domain
  296. * @param ctx_cbk callback function
  297. * @param l1 parameter passed to the callback
  298. * @param p2 parameter passed to the callback
  299. * @return 0 on success, <0 on error
  300. */
  301. static int tls_domain_foreach_CTX(tls_domain_t* d, per_ctx_cbk_f ctx_cbk,
  302. long l1, void* p2)
  303. {
  304. int i,ret;
  305. int procs_no;
  306. procs_no=get_max_procs();
  307. for(i = 0; i < procs_no; i++) {
  308. if ((ret=ctx_cbk(d->ctx[i], l1, p2))<0)
  309. return ret;
  310. }
  311. return 0;
  312. }
  313. /**
  314. * @brief Execute callback on all the CTX'es on in a domain list
  315. * @param d domain
  316. * @param ctx_cbk callback function
  317. * @param l1 parameter passed to the callback
  318. * @param p2 parameter passed to the callback
  319. * @return 0 on success, <0 on error
  320. */
  321. static int tls_foreach_CTX_in_domain_lst(tls_domain_t* d,
  322. per_ctx_cbk_f ctx_cbk,
  323. long l1, void* p2)
  324. {
  325. int ret;
  326. for (; d; d=d->next)
  327. if ((ret=tls_domain_foreach_CTX(d, ctx_cbk, l1, p2))<0)
  328. return ret;
  329. return 0;
  330. }
  331. /**
  332. * @brief Execute callback on all the CTX'es in all the srv domains in a tls cfg
  333. * @param cfg tls cfg.
  334. * @param ctx_cbk callback function
  335. * @param l1 parameter passed to the callback
  336. * @param p2 parameter passed to the callback
  337. * @return 0 on success, <0 on error
  338. */
  339. static int tls_foreach_CTX_in_srv_domains(tls_domains_cfg_t* cfg,
  340. per_ctx_cbk_f ctx_cbk,
  341. long l1, void* p2)
  342. {
  343. int ret;
  344. if ((ret = tls_domain_foreach_CTX(cfg->srv_default, ctx_cbk, l1, p2)) < 0)
  345. return ret;
  346. if ((ret = tls_foreach_CTX_in_domain_lst(cfg->srv_list, ctx_cbk, l1, p2))
  347. < 0)
  348. return ret;
  349. return 0;
  350. }
  351. /**
  352. * @brief Execute callback on all the CTX'es in all the client domains in a tls cfg
  353. * @param cfg tls cfg.
  354. * @param ctx_cbk callback function
  355. * @param l1 parameter passed to the callback
  356. * @param p2 parameter passed to the callback
  357. * @return 0 on success, <0 on error.
  358. */
  359. static int tls_foreach_CTX_in_cli_domains(tls_domains_cfg_t* cfg,
  360. per_ctx_cbk_f ctx_cbk,
  361. long l1, void* p2)
  362. {
  363. int ret;
  364. if ((ret = tls_domain_foreach_CTX(cfg->cli_default, ctx_cbk, l1, p2)) < 0)
  365. return ret;
  366. if ((ret = tls_foreach_CTX_in_domain_lst(cfg->cli_list, ctx_cbk, l1, p2))
  367. < 0)
  368. return ret;
  369. return 0;
  370. }
  371. /**
  372. * @brief Execute callback on all the CTX'es in all the domains in a tls cfg
  373. * @param cfg tls cfg
  374. * @param ctx_cbk callback function
  375. * @param l1 parameter passed to the callback
  376. * @param p2 parameter passed to the callback
  377. * @return 0 on success, <0 on error
  378. */
  379. static int tls_foreach_CTX_in_cfg(tls_domains_cfg_t* cfg,
  380. per_ctx_cbk_f ctx_cbk,
  381. long l1, void* p2)
  382. {
  383. int ret;
  384. if ((ret = tls_foreach_CTX_in_srv_domains(cfg, ctx_cbk, l1, p2)) < 0)
  385. return ret;
  386. if ((ret = tls_foreach_CTX_in_cli_domains(cfg, ctx_cbk, l1, p2)) < 0)
  387. return ret;
  388. return 0;
  389. }
  390. /**
  391. * @brief Fix pathnames when loading domain keys or other list
  392. *
  393. * Fix pathnames, to be used when loading the domain key, cert, ca list a.s.o.
  394. * It will replace path with a fixed shm allocated version. Assumes path->s
  395. * was shm allocated.
  396. * @param path path to be fixed. If it starts with '.' or '/' is left alone
  397. * (forced "relative" or "absolute" path). Otherwise the path is considered
  398. * to be relative to the main config file directory
  399. * (e.g. for /etc/ser/ser.cfg => /etc/ser/\<path\>).
  400. * @return 0 on success, -1 on error
  401. */
  402. int fix_shm_pathname(str* path)
  403. {
  404. str new_path;
  405. char* abs_path;
  406. if (path->s && path->len && *path->s != '.' && *path->s != '/') {
  407. abs_path = get_abs_pathname(0, path);
  408. if (abs_path == 0) return -1;
  409. new_path.len = strlen(abs_path);
  410. new_path.s = shm_malloc(new_path.len + 1);
  411. memcpy(new_path.s, abs_path, new_path.len);
  412. new_path.s[new_path.len] = 0;
  413. shm_free(path->s);
  414. *path = new_path;
  415. }
  416. return 0;
  417. }
  418. /**
  419. * @brief Load certificate from file
  420. * @param d domain
  421. * @return 0 if not configured or on success, -1 on error
  422. */
  423. static int load_cert(tls_domain_t* d)
  424. {
  425. int i;
  426. int procs_no;
  427. if (!d->cert_file.s || !d->cert_file.len) {
  428. DBG("%s: No certificate configured\n", tls_domain_str(d));
  429. return 0;
  430. }
  431. if (fix_shm_pathname(&d->cert_file) < 0)
  432. return -1;
  433. procs_no=get_max_procs();
  434. for(i = 0; i < procs_no; i++) {
  435. if (!SSL_CTX_use_certificate_chain_file(d->ctx[i], d->cert_file.s)) {
  436. ERR("%s: Unable to load certificate file '%s'\n",
  437. tls_domain_str(d), d->cert_file.s);
  438. TLS_ERR("load_cert:");
  439. return -1;
  440. }
  441. }
  442. return 0;
  443. }
  444. /**
  445. * @brief Load CA list from file
  446. * @param d domain
  447. * @return 0 if not configured or on success, -1 on error
  448. */
  449. static int load_ca_list(tls_domain_t* d)
  450. {
  451. int i;
  452. int procs_no;
  453. if (!d->ca_file.s || !d->ca_file.len) {
  454. DBG("%s: No CA list configured\n", tls_domain_str(d));
  455. return 0;
  456. }
  457. if (fix_shm_pathname(&d->ca_file) < 0)
  458. return -1;
  459. procs_no=get_max_procs();
  460. for(i = 0; i < procs_no; i++) {
  461. if (SSL_CTX_load_verify_locations(d->ctx[i], d->ca_file.s, 0) != 1) {
  462. ERR("%s: Unable to load CA list '%s'\n", tls_domain_str(d),
  463. d->ca_file.s);
  464. TLS_ERR("load_ca_list:");
  465. return -1;
  466. }
  467. SSL_CTX_set_client_CA_list(d->ctx[i],
  468. SSL_load_client_CA_file(d->ca_file.s));
  469. if (SSL_CTX_get_client_CA_list(d->ctx[i]) == 0) {
  470. ERR("%s: Error while setting client CA list\n", tls_domain_str(d));
  471. TLS_ERR("load_ca_list:");
  472. return -1;
  473. }
  474. }
  475. return 0;
  476. }
  477. /**
  478. * @brief Load CRL from file
  479. * @param d domain
  480. * @return 0 if not configured or on success, -1 on error
  481. */
  482. static int load_crl(tls_domain_t* d)
  483. {
  484. int i;
  485. int procs_no;
  486. X509_STORE* store;
  487. if (!d->crl_file.s) {
  488. DBG("%s: No CRL configured\n", tls_domain_str(d));
  489. return 0;
  490. }
  491. if (fix_shm_pathname(&d->crl_file) < 0)
  492. return -1;
  493. LOG(L_INFO, "%s: Certificate revocation lists will be checked (%.*s)\n",
  494. tls_domain_str(d), d->crl_file.len, d->crl_file.s);
  495. procs_no=get_max_procs();
  496. for(i = 0; i < procs_no; i++) {
  497. if (SSL_CTX_load_verify_locations(d->ctx[i], d->crl_file.s, 0) != 1) {
  498. ERR("%s: Unable to load certificate revocation list '%s'\n",
  499. tls_domain_str(d), d->crl_file.s);
  500. TLS_ERR("load_crl:");
  501. return -1;
  502. }
  503. store = SSL_CTX_get_cert_store(d->ctx[i]);
  504. X509_STORE_set_flags(store,
  505. X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL);
  506. }
  507. return 0;
  508. }
  509. #define C_DEF_NO_KRB5 "DEFAULT:!KRB5"
  510. #define C_DEF_NO_KRB5_LEN (sizeof(C_DEF_NO_KRB5)-1)
  511. #define C_NO_KRB5_SUFFIX ":!KRB5"
  512. #define C_NO_KRB5_SUFFIX_LEN (sizeof(C_NO_KRB5_SUFFIX)-1)
  513. /**
  514. * @brief Configure cipher list
  515. * @param d domain
  516. * @return 0 on success, -1 on error
  517. */
  518. static int set_cipher_list(tls_domain_t* d)
  519. {
  520. int i;
  521. int procs_no;
  522. char* cipher_list;
  523. cipher_list=d->cipher_list.s;
  524. #ifdef TLS_KSSL_WORKARROUND
  525. if (openssl_kssl_malloc_bug) { /* is openssl bug #1467 present ? */
  526. if (d->cipher_list.s==0) {
  527. /* use "DEFAULT:!KRB5" */
  528. cipher_list="DEFAULT:!KRB5";
  529. } else {
  530. /* append ":!KRB5" */
  531. cipher_list=shm_malloc(d->cipher_list.len+C_NO_KRB5_SUFFIX_LEN+1);
  532. if (cipher_list) {
  533. memcpy(cipher_list, d->cipher_list.s, d->cipher_list.len);
  534. memcpy(cipher_list+d->cipher_list.len, C_NO_KRB5_SUFFIX,
  535. C_NO_KRB5_SUFFIX_LEN);
  536. cipher_list[d->cipher_list.len+C_NO_KRB5_SUFFIX_LEN]=0;
  537. shm_free(d->cipher_list.s);
  538. d->cipher_list.s=cipher_list;
  539. d->cipher_list.len+=C_NO_KRB5_SUFFIX_LEN;
  540. }
  541. }
  542. }
  543. #endif /* TLS_KSSL_WORKARROUND */
  544. if (!cipher_list) return 0;
  545. procs_no=get_max_procs();
  546. for(i = 0; i < procs_no; i++) {
  547. if (SSL_CTX_set_cipher_list(d->ctx[i], cipher_list) == 0 ) {
  548. ERR("%s: Failure to set SSL context cipher list \"%s\"\n",
  549. tls_domain_str(d), cipher_list);
  550. return -1;
  551. }
  552. #ifndef OPENSSL_NO_ECDH
  553. setup_ecdh(d->ctx[i]);
  554. #endif
  555. #ifndef OPENSSL_NO_DH
  556. setup_dh(d->ctx[i]);
  557. #endif
  558. }
  559. return 0;
  560. }
  561. /**
  562. * @brief Enable/disable TLS certificate verification
  563. * @param d domain
  564. * @return 0
  565. */
  566. static int set_verification(tls_domain_t* d)
  567. {
  568. int verify_mode, i;
  569. int procs_no;
  570. if (d->require_cert) {
  571. verify_mode = SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
  572. LOG(L_INFO, "%s: %s MUST present valid certificate\n",
  573. tls_domain_str(d), d->type & TLS_DOMAIN_SRV ? "Client" : "Server");
  574. } else {
  575. if (d->verify_cert) {
  576. verify_mode = SSL_VERIFY_PEER;
  577. if (d->type & TLS_DOMAIN_SRV) {
  578. LOG(L_INFO, "%s: IF client provides certificate then it"
  579. " MUST be valid\n", tls_domain_str(d));
  580. } else {
  581. LOG(L_INFO, "%s: Server MUST present valid certificate\n",
  582. tls_domain_str(d));
  583. }
  584. } else {
  585. verify_mode = SSL_VERIFY_NONE;
  586. if (d->type & TLS_DOMAIN_SRV) {
  587. LOG(L_INFO, "%s: No client certificate required and no checks"
  588. " performed\n", tls_domain_str(d));
  589. } else {
  590. LOG(L_INFO, "%s: Server MAY present invalid certificate\n",
  591. tls_domain_str(d));
  592. }
  593. }
  594. }
  595. procs_no=get_max_procs();
  596. for(i = 0; i < procs_no; i++) {
  597. SSL_CTX_set_verify(d->ctx[i], verify_mode, 0);
  598. SSL_CTX_set_verify_depth(d->ctx[i], d->verify_depth);
  599. }
  600. return 0;
  601. }
  602. /* This callback function is executed when libssl processes the SSL
  603. * handshake and does SSL record layer stuff. It's used to trap
  604. * client-initiated renegotiations.
  605. */
  606. static void sr_ssl_ctx_info_callback(const SSL *ssl, int event, int ret)
  607. {
  608. struct tls_extra_data* data = 0;
  609. int tls_dbg;
  610. if (event & SSL_CB_HANDSHAKE_START) {
  611. tls_dbg = cfg_get(tls, tls_cfg, debug);
  612. LOG(tls_dbg, "SSL handshake started\n");
  613. if(data==0)
  614. data = (struct tls_extra_data*)SSL_get_app_data(ssl);
  615. if(data->flags & F_TLS_CON_HANDSHAKED) {
  616. LOG(tls_dbg, "SSL renegotiation initiated by client\n");
  617. data->flags |= F_TLS_CON_RENEGOTIATION;
  618. }
  619. }
  620. if (event & SSL_CB_HANDSHAKE_DONE) {
  621. tls_dbg = cfg_get(tls, tls_cfg, debug);
  622. if(data==0)
  623. data = (struct tls_extra_data*)SSL_get_app_data(ssl);
  624. LOG(tls_dbg, "SSL handshake done\n");
  625. /* CVE-2009-3555 - disable renegotiation */
  626. if (ssl->s3) {
  627. LOG(tls_dbg, "SSL disable renegotiation\n");
  628. ssl->s3->flags |= SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS;
  629. }
  630. data->flags |= F_TLS_CON_HANDSHAKED;
  631. }
  632. }
  633. /**
  634. * @brief Configure generic SSL parameters
  635. * @param d domain
  636. * @return 0
  637. */
  638. static int set_ssl_options(tls_domain_t* d)
  639. {
  640. int i;
  641. int procs_no;
  642. long options;
  643. #if OPENSSL_VERSION_NUMBER >= 0x00908000L
  644. long ssl_version;
  645. STACK_OF(SSL_COMP)* comp_methods;
  646. #endif
  647. procs_no=get_max_procs();
  648. options=SSL_OP_ALL; /* all the bug workarrounds by default */
  649. #if OPENSSL_VERSION_NUMBER >= 0x00907000L
  650. options|=SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
  651. SSL_OP_CIPHER_SERVER_PREFERENCE;
  652. #if OPENSSL_VERSION_NUMBER >= 0x00908000L
  653. ssl_version=SSLeay();
  654. if ((ssl_version >= 0x0090800L) && (ssl_version < 0x0090803fL)){
  655. /* if 0.9.8 <= openssl version < 0.9.8c and compression support is
  656. * enabled disable SSL_OP_TLS_BLOCK_PADDING_BUG (set by SSL_OP_ALL),
  657. * see openssl #1204 http://rt.openssl.org/Ticket/Display.html?id=1204
  658. */
  659. comp_methods=SSL_COMP_get_compression_methods();
  660. if (comp_methods && (sk_SSL_COMP_num(comp_methods) > 0)){
  661. options &= ~SSL_OP_TLS_BLOCK_PADDING_BUG;
  662. LOG(L_WARN, "tls: set_ssl_options: openssl "
  663. "SSL_OP_TLS_BLOCK_PADDING bug workaround enabled "
  664. "(openssl version %lx)\n", ssl_version);
  665. }else{
  666. LOG(L_INFO, "tls: set_ssl_options: detected openssl version (%lx) "
  667. " has the SSL_OP_TLS_BLOCK_PADDING bug, but compression "
  668. " is disabled so no workaround is needed\n", ssl_version);
  669. }
  670. }
  671. # endif
  672. #endif
  673. for(i = 0; i < procs_no; i++) {
  674. SSL_CTX_set_options(d->ctx[i], options);
  675. if(sr_tls_renegotiation==0)
  676. SSL_CTX_set_info_callback(d->ctx[i], sr_ssl_ctx_info_callback);
  677. }
  678. return 0;
  679. }
  680. /**
  681. * @brief Configure TLS session cache parameters
  682. * @param d domain
  683. * @return 0
  684. */
  685. static int set_session_cache(tls_domain_t* d)
  686. {
  687. int i;
  688. int procs_no;
  689. str tls_session_id;
  690. procs_no=get_max_procs();
  691. tls_session_id=cfg_get(tls, tls_cfg, session_id);
  692. for(i = 0; i < procs_no; i++) {
  693. /* janakj: I am not sure if session cache makes sense in ser, session
  694. * cache is stored in SSL_CTX and we have one SSL_CTX per process,
  695. * thus sessions among processes will not be reused
  696. */
  697. SSL_CTX_set_session_cache_mode(d->ctx[i],
  698. cfg_get(tls, tls_cfg, session_cache) ? SSL_SESS_CACHE_SERVER :
  699. SSL_SESS_CACHE_OFF);
  700. /* not really needed is SSL_SESS_CACHE_OFF */
  701. SSL_CTX_set_session_id_context(d->ctx[i],
  702. (unsigned char*)tls_session_id.s, tls_session_id.len);
  703. }
  704. return 0;
  705. }
  706. /**
  707. * @brief TLS SSL_CTX_set_mode and SSL_CTX_clear_mode wrapper
  708. * @param ctx SSL context
  709. * @param mode SSL_MODE_*
  710. * @param clear if set to !=0 will do a clear, else (==0) a set
  711. * @return 0 (always succeeds)
  712. */
  713. static int tls_ssl_ctx_mode(SSL_CTX* ctx, long mode, void* clear)
  714. {
  715. if (clear)
  716. #if OPENSSL_VERSION_NUMBER >= 0x01000000L || \
  717. defined SSL_CTX_clear_mode
  718. SSL_CTX_clear_mode(ctx, mode);
  719. #else
  720. return -1;
  721. #endif
  722. else
  723. SSL_CTX_set_mode(ctx, mode);
  724. return 0;
  725. }
  726. /**
  727. * @brief TLS set ctx->free_list_max_len
  728. * @param ctx TLS context
  729. * @param val value (<0 ignored)
  730. * @param unused unused
  731. * @return 0 (always succeeds)
  732. */
  733. static int tls_ssl_ctx_set_freelist(SSL_CTX* ctx, long val, void* unused)
  734. {
  735. if (val >= 0)
  736. #if OPENSSL_VERSION_NUMBER >= 0x01000000L
  737. #ifndef OPENSSL_NO_BUF_FREELISTS
  738. ctx->freelist_max_len = val;
  739. #endif
  740. #endif
  741. #if defined (OPENSSL_NO_BUF_FREELISTS) || OPENSSL_VERSION_NUMBER < 0x01000000L
  742. return -1;
  743. #endif
  744. return 0;
  745. }
  746. /**
  747. * @brief TLS SSL_CTX_set_max_send_fragment wrapper
  748. * @param ctx TLS context
  749. * @param val value (<0 ignored). Should be between 512 and 16k
  750. * @param unused unused
  751. * @return 0 on success, < 0 on failure (invalid value)
  752. */
  753. static int tls_ssl_ctx_set_max_send_fragment(SSL_CTX* ctx, long val, void* unused)
  754. {
  755. if (val >= 0)
  756. #if OPENSSL_VERSION_NUMBER >= 0x00909000L
  757. return SSL_CTX_set_max_send_fragment(ctx, val) -1;
  758. #else
  759. return -1;
  760. #endif
  761. return 0;
  762. }
  763. /**
  764. * @brief TLS SSL_CTX_set_read_ahead wrapper
  765. * @param ctx TLS context
  766. * @param val value (<0 ignored, 0 or >0)
  767. * @param unused unused
  768. * @return 0 (always success).
  769. */
  770. static int tls_ssl_ctx_set_read_ahead(SSL_CTX* ctx, long val, void* unused)
  771. {
  772. if (val >= 0)
  773. SSL_CTX_set_read_ahead(ctx, val);
  774. return 0;
  775. }
  776. #ifndef OPENSSL_NO_TLSEXT
  777. /**
  778. * @brief SNI callback function
  779. *
  780. * callback on server_name -> trigger context switch if a TLS domain
  781. * for the server_name is found (checks socket too) */
  782. static int tls_server_name_cb(SSL *ssl, int *ad, void *private)
  783. {
  784. tls_domain_t *orig_domain, *new_domain;
  785. str server_name;
  786. orig_domain = (tls_domain_t*)private;
  787. server_name.s = (char*)SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
  788. if (server_name.s) {
  789. LM_DBG("received server_name (TLS extension): '%s'\n", server_name.s);
  790. } else {
  791. LM_DBG("SSL_get_servername returned NULL: return SSL_TLSEXT_ERR_NOACK\n");
  792. return SSL_TLSEXT_ERR_NOACK;
  793. }
  794. server_name.len = strlen(server_name.s);
  795. new_domain = tls_lookup_cfg(*tls_domains_cfg, TLS_DOMAIN_SRV,
  796. &orig_domain->ip, orig_domain->port, &server_name);
  797. if (new_domain==NULL) {
  798. LM_DBG("TLS domain for socket [%s:%d] and server_name='%s' "
  799. "not found\n", ip_addr2a(&orig_domain->ip),
  800. orig_domain->port, server_name.s);
  801. /* we do not perform SSL_CTX switching, thus the default server domain
  802. for this socket (or the default server domain) will be used. */
  803. return SSL_TLSEXT_ERR_ALERT_WARNING;
  804. }
  805. LM_DBG("TLS cfg domain selected for received server name [%s]:"
  806. " socket [%s:%d] server name='%s' -"
  807. " switching SSL CTX to %p dom %p%s\n",
  808. server_name.s, ip_addr2a(&new_domain->ip),
  809. new_domain->port, ZSW(new_domain->server_name.s),
  810. new_domain->ctx[process_no], new_domain,
  811. (new_domain->type & TLS_DOMAIN_DEF)?" (default)":"");
  812. SSL_set_SSL_CTX(ssl, new_domain->ctx[process_no]);
  813. /* SSL_set_SSL_CTX only sets the correct certificate parameters, but does
  814. set the proper verify options. Thus this will be done manually! */
  815. SSL_set_options(ssl, SSL_CTX_get_options(ssl->ctx));
  816. if ((SSL_get_verify_mode(ssl) == SSL_VERIFY_NONE) ||
  817. (SSL_num_renegotiations(ssl) == 0)) {
  818. /*
  819. * Only initialize the verification settings from the ctx
  820. * if they are not yet set, or if we're called when a new
  821. * SSL connection is set up (num_renegotiations == 0).
  822. * Otherwise, we would possibly reset a per-directory
  823. * configuration which was put into effect by ssl_hook_access.
  824. */
  825. SSL_set_verify(ssl, SSL_CTX_get_verify_mode(ssl->ctx),
  826. SSL_CTX_get_verify_callback(ssl->ctx));
  827. }
  828. return SSL_TLSEXT_ERR_OK;
  829. }
  830. #endif
  831. /**
  832. * @brief Initialize all domain attributes from default domains if necessary
  833. * @param d initialized TLS domain
  834. * @param def default TLS domains
  835. */
  836. static int fix_domain(tls_domain_t* d, tls_domain_t* def)
  837. {
  838. int i;
  839. int procs_no;
  840. if (fill_missing(d, def) < 0) return -1;
  841. procs_no=get_max_procs();
  842. d->ctx = (SSL_CTX**)shm_malloc(sizeof(SSL_CTX*) * procs_no);
  843. if (!d->ctx) {
  844. ERR("%s: Cannot allocate shared memory\n", tls_domain_str(d));
  845. return -1;
  846. }
  847. if(d->method>TLS_USE_TLSvRANGE) {
  848. LM_DBG("using tls methods range: %d\n", d->method);
  849. } else {
  850. LM_DBG("using one tls method version: %d\n", d->method);
  851. }
  852. memset(d->ctx, 0, sizeof(SSL_CTX*) * procs_no);
  853. for(i = 0; i < procs_no; i++) {
  854. if(d->method>TLS_USE_TLSvRANGE) {
  855. d->ctx[i] = SSL_CTX_new(SSLv23_method());
  856. } else {
  857. d->ctx[i] = SSL_CTX_new((SSL_METHOD*)ssl_methods[d->method - 1]);
  858. }
  859. if (d->ctx[i] == NULL) {
  860. ERR("%s: Cannot create SSL context\n", tls_domain_str(d));
  861. return -1;
  862. }
  863. if(d->method>TLS_USE_TLSvRANGE) {
  864. SSL_CTX_set_options(d->ctx[i], (long)ssl_methods[d->method - 1]);
  865. }
  866. #ifndef OPENSSL_NO_TLSEXT
  867. /*
  868. * check server domains for server_name extension and register
  869. * callback function
  870. */
  871. if ((d->type & TLS_DOMAIN_SRV) && d->server_name.len>0) {
  872. if (!SSL_CTX_set_tlsext_servername_callback(d->ctx[i], tls_server_name_cb)) {
  873. LM_ERR("register server_name callback handler for socket "
  874. "[%s:%d], server_name='%s' failed for proc %d\n",
  875. ip_addr2a(&d->ip), d->port, d->server_name.s, i);
  876. return -1;
  877. }
  878. if (!SSL_CTX_set_tlsext_servername_arg(d->ctx[i], d)) {
  879. LM_ERR("register server_name callback handler data for socket "
  880. "[%s:%d], server_name='%s' failed for proc %d\n",
  881. ip_addr2a(&d->ip), d->port, d->server_name.s, i);
  882. return -1;
  883. }
  884. }
  885. #endif
  886. }
  887. #ifndef OPENSSL_NO_TLSEXT
  888. if ((d->type & TLS_DOMAIN_SRV) && d->server_name.len>0) {
  889. LM_NOTICE("registered server_name callback handler for socket "
  890. "[%s:%d], server_name='%s' ...\n", ip_addr2a(&d->ip), d->port,
  891. d->server_name.s);
  892. }
  893. #endif
  894. if (load_cert(d) < 0) return -1;
  895. if (load_ca_list(d) < 0) return -1;
  896. if (load_crl(d) < 0) return -1;
  897. if (set_cipher_list(d) < 0) return -1;
  898. if (set_verification(d) < 0) return -1;
  899. if (set_ssl_options(d) < 0) return -1;
  900. if (set_session_cache(d) < 0) return -1;
  901. return 0;
  902. }
  903. /**
  904. * @brief Password callback, ask for private key password on CLI
  905. * @param buf buffer
  906. * @param size buffer size
  907. * @param rwflag not used
  908. * @param filename filename
  909. * @return length of password on success, 0 on error
  910. */
  911. static int passwd_cb(char *buf, int size, int rwflag, void *filename)
  912. {
  913. #if OPENSSL_VERSION_NUMBER >= 0x00907000L
  914. UI *ui;
  915. const char *prompt;
  916. ui = UI_new();
  917. if (ui == NULL)
  918. goto err;
  919. prompt = UI_construct_prompt(ui, "passphrase", filename);
  920. UI_add_input_string(ui, prompt, 0, buf, 0, size - 1);
  921. UI_process(ui);
  922. UI_free(ui);
  923. return strlen(buf);
  924. err:
  925. ERR("passwd_cb: Error in passwd_cb\n");
  926. if (ui) {
  927. UI_free(ui);
  928. }
  929. return 0;
  930. #else
  931. if (des_read_pw_string(buf, size-1, "Enter Private Key password:", 0)) {
  932. ERR("Error in passwd_cb\n");
  933. return 0;
  934. }
  935. return strlen(buf);
  936. #endif
  937. }
  938. /**
  939. * @brief Load a private key from a file
  940. * @param d TLS domain
  941. * @return 0 on success, -1 on error
  942. */
  943. static int load_private_key(tls_domain_t* d)
  944. {
  945. int idx, ret_pwd, i;
  946. int procs_no;
  947. if (!d->pkey_file.s || !d->pkey_file.len) {
  948. DBG("%s: No private key specified\n", tls_domain_str(d));
  949. return 0;
  950. }
  951. if (fix_shm_pathname(&d->pkey_file) < 0)
  952. return -1;
  953. procs_no=get_max_procs();
  954. for(i = 0; i < procs_no; i++) {
  955. SSL_CTX_set_default_passwd_cb(d->ctx[i], passwd_cb);
  956. SSL_CTX_set_default_passwd_cb_userdata(d->ctx[i], d->pkey_file.s);
  957. for(idx = 0, ret_pwd = 0; idx < 3; idx++) {
  958. ret_pwd = SSL_CTX_use_PrivateKey_file(d->ctx[i], d->pkey_file.s,
  959. SSL_FILETYPE_PEM);
  960. if (ret_pwd) {
  961. break;
  962. } else {
  963. ERR("%s: Unable to load private key '%s'\n",
  964. tls_domain_str(d), d->pkey_file.s);
  965. TLS_ERR("load_private_key:");
  966. continue;
  967. }
  968. }
  969. if (!ret_pwd) {
  970. ERR("%s: Unable to load private key file '%s'\n",
  971. tls_domain_str(d), d->pkey_file.s);
  972. TLS_ERR("load_private_key:");
  973. return -1;
  974. }
  975. if (!SSL_CTX_check_private_key(d->ctx[i])) {
  976. ERR("%s: Key '%s' does not match the public key of the"
  977. " certificate\n", tls_domain_str(d), d->pkey_file.s);
  978. TLS_ERR("load_private_key:");
  979. return -1;
  980. }
  981. }
  982. DBG("%s: Key '%s' successfuly loaded\n",
  983. tls_domain_str(d), d->pkey_file.s);
  984. return 0;
  985. }
  986. /**
  987. * @brief Initialize attributes of all domains from default domains if necessary
  988. *
  989. * Initialize attributes of all domains from default domains if necessary,
  990. * fill in missing parameters.
  991. * @param cfg initialized domain
  992. * @param srv_defaults server defaults
  993. * @param cli_defaults command line interface defaults
  994. * @return 0 on success, -1 on error
  995. */
  996. int tls_fix_domains_cfg(tls_domains_cfg_t* cfg, tls_domain_t* srv_defaults,
  997. tls_domain_t* cli_defaults)
  998. {
  999. tls_domain_t* d;
  1000. int ssl_mode_release_buffers;
  1001. int ssl_freelist_max_len;
  1002. int ssl_max_send_fragment;
  1003. int ssl_read_ahead;
  1004. if (!cfg->cli_default) {
  1005. cfg->cli_default = tls_new_domain(TLS_DOMAIN_DEF | TLS_DOMAIN_CLI,
  1006. 0, 0);
  1007. }
  1008. if (!cfg->srv_default) {
  1009. cfg->srv_default = tls_new_domain(TLS_DOMAIN_DEF | TLS_DOMAIN_SRV,
  1010. 0, 0);
  1011. }
  1012. if (fix_domain(cfg->srv_default, srv_defaults) < 0) return -1;
  1013. if (fix_domain(cfg->cli_default, cli_defaults) < 0) return -1;
  1014. d = cfg->srv_list;
  1015. while (d) {
  1016. if (fix_domain(d, srv_defaults) < 0) return -1;
  1017. d = d->next;
  1018. }
  1019. d = cfg->cli_list;
  1020. while (d) {
  1021. if (fix_domain(d, cli_defaults) < 0) return -1;
  1022. d = d->next;
  1023. }
  1024. /* Ask for passwords as the last step */
  1025. d = cfg->srv_list;
  1026. while(d) {
  1027. if (load_private_key(d) < 0) return -1;
  1028. d = d->next;
  1029. }
  1030. d = cfg->cli_list;
  1031. while(d) {
  1032. if (load_private_key(d) < 0) return -1;
  1033. d = d->next;
  1034. }
  1035. if (load_private_key(cfg->srv_default) < 0) return -1;
  1036. if (load_private_key(cfg->cli_default) < 0) return -1;
  1037. /* set various global per CTX options
  1038. * (done here to show possible missing features messages only once)
  1039. */
  1040. ssl_mode_release_buffers = cfg_get(tls, tls_cfg, ssl_release_buffers);
  1041. ssl_freelist_max_len = cfg_get(tls, tls_cfg, ssl_freelist_max);
  1042. ssl_max_send_fragment = cfg_get(tls, tls_cfg, ssl_max_send_fragment);
  1043. ssl_read_ahead = cfg_get(tls, tls_cfg, ssl_read_ahead);
  1044. #if OPENSSL_VERSION_NUMBER >= 0x01000000L
  1045. /* set SSL_MODE_RELEASE_BUFFERS if ssl_mode_release_buffers !=0,
  1046. reset if == 0 and ignore if < 0 */
  1047. /* only in >= 1.0.0 */
  1048. if (ssl_mode_release_buffers >= 0 &&
  1049. tls_foreach_CTX_in_cfg(cfg, tls_ssl_ctx_mode, SSL_MODE_RELEASE_BUFFERS,
  1050. (void*)(long)(ssl_mode_release_buffers==0))
  1051. < 0) {
  1052. ERR("invalid ssl_release_buffers value (%d)\n",
  1053. ssl_mode_release_buffers);
  1054. return -1;
  1055. }
  1056. #else
  1057. if (ssl_mode_release_buffers > 0)
  1058. ERR("cannot change openssl mode_release_buffers, the openssl version"
  1059. " is too old (need at least 1.0.0)\n");
  1060. #endif
  1061. /* only in >= 1.0.0 */
  1062. #if OPENSSL_VERSION_NUMBER >= 0x01000000L
  1063. #ifndef OPENSSL_NO_BUF_FREELISTS
  1064. if (tls_foreach_CTX_in_cfg(cfg, tls_ssl_ctx_set_freelist,
  1065. ssl_freelist_max_len, 0) < 0) {
  1066. ERR("invalid ssl_freelist_max_len value (%d)\n",
  1067. ssl_freelist_max_len);
  1068. return -1;
  1069. }
  1070. #endif
  1071. #endif
  1072. #if defined (OPENSSL_NO_BUF_FREELISTS) || OPENSSL_VERSION_NUMBER < 0x01000000L
  1073. if (ssl_freelist_max_len >= 0)
  1074. ERR("cannot change openssl freelist_max_len, openssl too old"
  1075. "(needed at least 1.0.0) or compiled without freelist support"
  1076. " (OPENSSL_NO_BUF_FREELIST)\n");
  1077. #endif
  1078. #if OPENSSL_VERSION_NUMBER >= 0x00909000L
  1079. /* only in >= 0.9.9 */
  1080. if (tls_foreach_CTX_in_cfg(cfg, tls_ssl_ctx_set_max_send_fragment,
  1081. ssl_max_send_fragment, 0) < 0) {
  1082. ERR("invalid ssl_max_send_fragment value (%d)\n",
  1083. ssl_max_send_fragment);
  1084. return -1;
  1085. }
  1086. #else
  1087. if (ssl_max_send_fragment > 0)
  1088. ERR("cannot change openssl max_send_fragment, the openssl version"
  1089. " is too old (need at least 0.9.9)\n");
  1090. #endif
  1091. if (tls_foreach_CTX_in_cfg(cfg, tls_ssl_ctx_set_read_ahead,
  1092. ssl_read_ahead, 0) < 0) {
  1093. ERR("invalid ssl_read_ahead value (%d)\n", ssl_read_ahead);
  1094. return -1;
  1095. }
  1096. /* set options for SSL_write:
  1097. SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER - needed when queueing
  1098. clear text for a future write (WANTS_READ). In this case the
  1099. buffer address will change for the repeated SSL_write() and
  1100. without this option it will trigger the openssl sanity checks.
  1101. SSL_MODE_ENABLE_PARTIAL_WRITE - needed to deal with potentially
  1102. huge multi-record writes that don't fit in the default buffer
  1103. (the default buffer must have space for at least 1 record) */
  1104. if (tls_foreach_CTX_in_cfg(cfg, tls_ssl_ctx_mode,
  1105. SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
  1106. SSL_MODE_ENABLE_PARTIAL_WRITE,
  1107. 0) < 0) {
  1108. ERR("could not set SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER and"
  1109. " SSL_MODE_ENABLE_PARTIAL_WRITE\n");
  1110. return -1;
  1111. }
  1112. return 0;
  1113. }
  1114. /**
  1115. * @brief Create new configuration structure
  1116. *
  1117. * Create new configuration structure in new allocated shared memory
  1118. * @return configuration structure or zero on error
  1119. */
  1120. tls_domains_cfg_t* tls_new_cfg(void)
  1121. {
  1122. tls_domains_cfg_t* r;
  1123. r = (tls_domains_cfg_t*)shm_malloc(sizeof(tls_domains_cfg_t));
  1124. if (!r) {
  1125. ERR("No memory left\n");
  1126. return 0;
  1127. }
  1128. memset(r, 0, sizeof(tls_domains_cfg_t));
  1129. atomic_set(&r->ref_count, 0);
  1130. return r;
  1131. }
  1132. /**
  1133. * @brief Lookup TLS configuration based on type, ip, and port
  1134. * @param cfg configuration set
  1135. * @param type type of configuration
  1136. * @param ip IP for configuration
  1137. * @param port port for configuration
  1138. * @return found configuration or default, if not found
  1139. */
  1140. tls_domain_t* tls_lookup_cfg(tls_domains_cfg_t* cfg, int type,
  1141. struct ip_addr* ip, unsigned short port, str *sname)
  1142. {
  1143. tls_domain_t *p;
  1144. if (type & TLS_DOMAIN_DEF) {
  1145. if (type & TLS_DOMAIN_SRV) return cfg->srv_default;
  1146. else return cfg->cli_default;
  1147. } else {
  1148. if (type & TLS_DOMAIN_SRV) p = cfg->srv_list;
  1149. else p = cfg->cli_list;
  1150. }
  1151. while (p) {
  1152. if(sname) {
  1153. LM_DBG("comparing addr: [%s:%d] [%s:%d] -- sni: [%.*s] [%.*s]\n",
  1154. ip_addr2a(&p->ip), p->port, ip_addr2a(ip), port,
  1155. p->server_name.len, ZSW(p->server_name.s),
  1156. sname->len, ZSW(sname->s));
  1157. }
  1158. if ((p->port==0 || p->port == port) && ip_addr_cmp(&p->ip, ip)) {
  1159. if(sname && sname->len>0) {
  1160. if(p->server_name.s && p->server_name.len==sname->len
  1161. && strncasecmp(p->server_name.s, sname->s, sname->len)==0) {
  1162. LM_DBG("socket+server_name based TLS server domain found\n");
  1163. return p;
  1164. }
  1165. } else {
  1166. return p;
  1167. }
  1168. }
  1169. p = p->next;
  1170. }
  1171. /* No matching domain found, return default */
  1172. if (type & TLS_DOMAIN_SRV) return cfg->srv_default;
  1173. else return cfg->cli_default;
  1174. }
  1175. /**
  1176. * @brief Check whether configuration domain exists
  1177. * @param cfg configuration set
  1178. * @param d checked domain
  1179. * @return 1 if domain exists, 0 if its not exists
  1180. */
  1181. static int domain_exists(tls_domains_cfg_t* cfg, tls_domain_t* d)
  1182. {
  1183. tls_domain_t *p;
  1184. if (d->type & TLS_DOMAIN_DEF) {
  1185. if (d->type & TLS_DOMAIN_SRV) return cfg->srv_default != NULL;
  1186. else return cfg->cli_default != NULL;
  1187. } else {
  1188. if (d->type & TLS_DOMAIN_SRV) p = cfg->srv_list;
  1189. else p = cfg->cli_list;
  1190. }
  1191. while (p) {
  1192. if ((p->port == d->port) && ip_addr_cmp(&p->ip, &d->ip)) {
  1193. if(p->server_name.len==0) {
  1194. LM_WARN("another tls domain with same address was defined"
  1195. " and no server name provided\n");
  1196. return 1;
  1197. }
  1198. }
  1199. p = p->next;
  1200. }
  1201. return 0;
  1202. }
  1203. /**
  1204. * @brief Add a domain to the configuration set
  1205. * @param cfg configuration set
  1206. * @param d TLS domain
  1207. * @return 1 if domain already exists, 0 after addition, -1 on error
  1208. */
  1209. int tls_add_domain(tls_domains_cfg_t* cfg, tls_domain_t* d)
  1210. {
  1211. if (!cfg) {
  1212. ERR("TLS configuration structure missing\n");
  1213. return -1;
  1214. }
  1215. /* Make sure the domain does not exist */
  1216. if (domain_exists(cfg, d)) return 1;
  1217. if (d->type & TLS_DOMAIN_DEF) {
  1218. if (d->type & TLS_DOMAIN_CLI) {
  1219. cfg->cli_default = d;
  1220. } else {
  1221. cfg->srv_default = d;
  1222. }
  1223. } else {
  1224. if (d->type & TLS_DOMAIN_SRV) {
  1225. d->next = cfg->srv_list;
  1226. cfg->srv_list = d;
  1227. } else {
  1228. d->next = cfg->cli_list;
  1229. cfg->cli_list = d;
  1230. }
  1231. }
  1232. return 0;
  1233. }