tls_domain.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  1. /*
  2. * Copyright (C) 2001-2003 FhG FOKUS
  3. * Copyright (C) 2005,2006 iptelorg GmbH
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /**
  18. * SIP-router TLS support :: Virtual domain configuration support
  19. * @file
  20. * @ingroup tls
  21. * Module: @ref tls
  22. */
  23. #include <stdlib.h>
  24. #include <openssl/ssl.h>
  25. #include <openssl/opensslv.h>
  26. #if OPENSSL_VERSION_NUMBER >= 0x00907000L
  27. # include <openssl/ui.h>
  28. #endif
  29. #include "../../ut.h"
  30. #include "../../mem/shm_mem.h"
  31. #include "../../pt.h"
  32. #include "../../cfg/cfg.h"
  33. #include "tls_server.h"
  34. #include "tls_util.h"
  35. #include "tls_mod.h"
  36. #include "tls_init.h"
  37. #include "tls_domain.h"
  38. #include "tls_cfg.h"
  39. /**
  40. * @brief Create a new TLS domain structure
  41. *
  42. * Create a new domain structure in new allocated shared memory.
  43. * @param type domain Type
  44. * @param ip domain IP
  45. * @param port domain port
  46. * @return new domain
  47. */
  48. tls_domain_t* tls_new_domain(int type, struct ip_addr *ip, unsigned short port)
  49. {
  50. tls_domain_t* d;
  51. d = shm_malloc(sizeof(tls_domain_t));
  52. if (d == NULL) {
  53. ERR("Memory allocation failure\n");
  54. return 0;
  55. }
  56. memset(d, '\0', sizeof(tls_domain_t));
  57. d->type = type;
  58. if (ip) memcpy(&d->ip, ip, sizeof(struct ip_addr));
  59. d->port = port;
  60. d->verify_cert = -1;
  61. d->verify_depth = -1;
  62. d->require_cert = -1;
  63. return d;
  64. }
  65. /**
  66. * @brief Free all memory used by TLS configuration domain
  67. * @param d freed domain
  68. */
  69. void tls_free_domain(tls_domain_t* d)
  70. {
  71. int i;
  72. int procs_no;
  73. if (!d) return;
  74. if (d->ctx) {
  75. procs_no=get_max_procs();
  76. for(i = 0; i < procs_no; i++) {
  77. if (d->ctx[i]) SSL_CTX_free(d->ctx[i]);
  78. }
  79. shm_free(d->ctx);
  80. }
  81. if (d->cipher_list.s) shm_free(d->cipher_list.s);
  82. if (d->ca_file.s) shm_free(d->ca_file.s);
  83. if (d->crl_file.s) shm_free(d->crl_file.s);
  84. if (d->pkey_file.s) shm_free(d->pkey_file.s);
  85. if (d->cert_file.s) shm_free(d->cert_file.s);
  86. shm_free(d);
  87. }
  88. /**
  89. * @brief Free TLS configuration structure
  90. * @param cfg freed configuration
  91. */
  92. void tls_free_cfg(tls_domains_cfg_t* cfg)
  93. {
  94. tls_domain_t* p;
  95. while(cfg->srv_list) {
  96. p = cfg->srv_list;
  97. cfg->srv_list = cfg->srv_list->next;
  98. tls_free_domain(p);
  99. }
  100. while(cfg->cli_list) {
  101. p = cfg->cli_list;
  102. cfg->cli_list = cfg->cli_list->next;
  103. tls_free_domain(p);
  104. }
  105. if (cfg->srv_default) tls_free_domain(cfg->srv_default);
  106. if (cfg->cli_default) tls_free_domain(cfg->cli_default);
  107. }
  108. /**
  109. * @brief Destroy all TLS configuration data
  110. */
  111. void tls_destroy_cfg(void)
  112. {
  113. tls_domains_cfg_t* ptr;
  114. if (tls_domains_cfg_lock) {
  115. lock_destroy(tls_domains_cfg_lock);
  116. lock_dealloc(tls_domains_cfg_lock);
  117. tls_domains_cfg_lock = 0;
  118. }
  119. if (tls_domains_cfg) {
  120. while(*tls_domains_cfg) {
  121. ptr = *tls_domains_cfg;
  122. *tls_domains_cfg = (*tls_domains_cfg)->next;
  123. tls_free_cfg(ptr);
  124. }
  125. shm_free(tls_domains_cfg);
  126. tls_domains_cfg = 0;
  127. }
  128. }
  129. /**
  130. * @brief Generate TLS domain identifier
  131. * @param d printed domain
  132. * @return printed domain, with zero termination
  133. */
  134. char* tls_domain_str(tls_domain_t* d)
  135. {
  136. static char buf[1024];
  137. char* p;
  138. buf[0] = '\0';
  139. p = buf;
  140. p = strcat(p, d->type & TLS_DOMAIN_SRV ? "TLSs<" : "TLSc<");
  141. if (d->type & TLS_DOMAIN_DEF) {
  142. p = strcat(p, "default>");
  143. } else {
  144. p = strcat(p, ip_addr2a(&d->ip));
  145. p = strcat(p, ":");
  146. p = strcat(p, int2str(d->port, 0));
  147. p = strcat(p, ">");
  148. }
  149. return buf;
  150. }
  151. /**
  152. * @brief Initialize TLS domain parameters that have not been configured yet
  153. *
  154. * Initialize TLS domain parameters that have not been configured from
  155. * parent domain (usually one of default domains)
  156. * @param d initialized domain
  157. * @param parent parent domain
  158. * @return 0 on success, -1 on error
  159. */
  160. static int fill_missing(tls_domain_t* d, tls_domain_t* parent)
  161. {
  162. if (d->method == TLS_METHOD_UNSPEC) d->method = parent->method;
  163. LOG(L_INFO, "%s: tls_method=%d\n", tls_domain_str(d), d->method);
  164. if (d->method < 1 || d->method >= TLS_METHOD_MAX) {
  165. ERR("%s: Invalid TLS method value\n", tls_domain_str(d));
  166. return -1;
  167. }
  168. if (!d->cert_file.s) {
  169. if (shm_asciiz_dup(&d->cert_file.s, parent->cert_file.s) < 0)
  170. return -1;
  171. d->cert_file.len = parent->cert_file.len;
  172. }
  173. LOG(L_INFO, "%s: certificate='%s'\n", tls_domain_str(d), d->cert_file.s);
  174. if (!d->ca_file.s){
  175. if (shm_asciiz_dup(&d->ca_file.s, parent->ca_file.s) < 0)
  176. return -1;
  177. d->ca_file.len = parent->ca_file.len;
  178. }
  179. LOG(L_INFO, "%s: ca_list='%s'\n", tls_domain_str(d), d->ca_file.s);
  180. if (!d->crl_file.s) {
  181. if (shm_asciiz_dup(&d->crl_file.s, parent->crl_file.s) < 0)
  182. return -1;
  183. d->crl_file.len = parent->crl_file.len;
  184. }
  185. LOG(L_INFO, "%s: crl='%s'\n", tls_domain_str(d), d->crl_file.s);
  186. if (d->require_cert == -1) d->require_cert = parent->require_cert;
  187. LOG(L_INFO, "%s: require_certificate=%d\n", tls_domain_str(d),
  188. d->require_cert);
  189. if (!d->cipher_list.s) {
  190. if ( shm_asciiz_dup(&d->cipher_list.s, parent->cipher_list.s) < 0)
  191. return -1;
  192. d->cipher_list.len = parent->cipher_list.len;
  193. }
  194. LOG(L_INFO, "%s: cipher_list='%s'\n", tls_domain_str(d), d->cipher_list.s);
  195. if (!d->pkey_file.s) {
  196. if (shm_asciiz_dup(&d->pkey_file.s, parent->pkey_file.s) < 0)
  197. return -1;
  198. d->pkey_file.len = parent->pkey_file.len;
  199. }
  200. LOG(L_INFO, "%s: private_key='%s'\n", tls_domain_str(d), d->pkey_file.s);
  201. if (d->verify_cert == -1) d->verify_cert = parent->verify_cert;
  202. LOG(L_INFO, "%s: verify_certificate=%d\n", tls_domain_str(d),
  203. d->verify_cert);
  204. if (d->verify_depth == -1) d->verify_depth = parent->verify_depth;
  205. LOG(L_INFO, "%s: verify_depth=%d\n", tls_domain_str(d), d->verify_depth);
  206. return 0;
  207. }
  208. /**
  209. * @brief Called for ctx, with 2 args
  210. * @param ctx SSL context
  211. * @param larg ?
  212. * @param parg ?
  213. * @return return 0 on succes, <0 on critical error
  214. */
  215. typedef int (*per_ctx_cbk_f)(SSL_CTX* ctx, long larg, void* parg);
  216. /**
  217. * @brief Execute callback on all the CTX'es on a domain
  218. * @param d domain
  219. * @param ctx_cbk callback function
  220. * @param l1 parameter passed to the callback
  221. * @param p2 parameter passed to the callback
  222. * @return 0 on success, <0 on error
  223. */
  224. static int tls_domain_foreach_CTX(tls_domain_t* d, per_ctx_cbk_f ctx_cbk,
  225. long l1, void* p2)
  226. {
  227. int i,ret;
  228. int procs_no;
  229. procs_no=get_max_procs();
  230. for(i = 0; i < procs_no; i++) {
  231. if ((ret=ctx_cbk(d->ctx[i], l1, p2))<0)
  232. return ret;
  233. }
  234. return 0;
  235. }
  236. /**
  237. * @brief Execute callback on all the CTX'es on in a domain list
  238. * @param d domain
  239. * @param ctx_cbk callback function
  240. * @param l1 parameter passed to the callback
  241. * @param p2 parameter passed to the callback
  242. * @return 0 on success, <0 on error
  243. */
  244. static int tls_foreach_CTX_in_domain_lst(tls_domain_t* d,
  245. per_ctx_cbk_f ctx_cbk,
  246. long l1, void* p2)
  247. {
  248. int ret;
  249. for (; d; d=d->next)
  250. if ((ret=tls_domain_foreach_CTX(d, ctx_cbk, l1, p2))<0)
  251. return ret;
  252. return 0;
  253. }
  254. /**
  255. * @brief Execute callback on all the CTX'es in all the srv domains in a tls cfg
  256. * @param cfg tls cfg.
  257. * @param ctx_cbk callback function
  258. * @param l1 parameter passed to the callback
  259. * @param p2 parameter passed to the callback
  260. * @return 0 on success, <0 on error
  261. */
  262. static int tls_foreach_CTX_in_srv_domains(tls_domains_cfg_t* cfg,
  263. per_ctx_cbk_f ctx_cbk,
  264. long l1, void* p2)
  265. {
  266. int ret;
  267. if ((ret = tls_domain_foreach_CTX(cfg->srv_default, ctx_cbk, l1, p2)) < 0)
  268. return ret;
  269. if ((ret = tls_foreach_CTX_in_domain_lst(cfg->srv_list, ctx_cbk, l1, p2))
  270. < 0)
  271. return ret;
  272. return 0;
  273. }
  274. /**
  275. * @brief Execute callback on all the CTX'es in all the client domains in a tls cfg
  276. * @param cfg tls cfg.
  277. * @param ctx_cbk callback function
  278. * @param l1 parameter passed to the callback
  279. * @param p2 parameter passed to the callback
  280. * @return 0 on success, <0 on error.
  281. */
  282. static int tls_foreach_CTX_in_cli_domains(tls_domains_cfg_t* cfg,
  283. per_ctx_cbk_f ctx_cbk,
  284. long l1, void* p2)
  285. {
  286. int ret;
  287. if ((ret = tls_domain_foreach_CTX(cfg->cli_default, ctx_cbk, l1, p2)) < 0)
  288. return ret;
  289. if ((ret = tls_foreach_CTX_in_domain_lst(cfg->cli_list, ctx_cbk, l1, p2))
  290. < 0)
  291. return ret;
  292. return 0;
  293. }
  294. /**
  295. * @brief Execute callback on all the CTX'es in all the domains in a tls cfg
  296. * @param cfg tls cfg
  297. * @param ctx_cbk callback function
  298. * @param l1 parameter passed to the callback
  299. * @param p2 parameter passed to the callback
  300. * @return 0 on success, <0 on error
  301. */
  302. static int tls_foreach_CTX_in_cfg(tls_domains_cfg_t* cfg,
  303. per_ctx_cbk_f ctx_cbk,
  304. long l1, void* p2)
  305. {
  306. int ret;
  307. if ((ret = tls_foreach_CTX_in_srv_domains(cfg, ctx_cbk, l1, p2)) < 0)
  308. return ret;
  309. if ((ret = tls_foreach_CTX_in_cli_domains(cfg, ctx_cbk, l1, p2)) < 0)
  310. return ret;
  311. return 0;
  312. }
  313. /**
  314. * @brief Fix pathnames when loading domain keys or other list
  315. *
  316. * Fix pathnames, to be used when loading the domain key, cert, ca list a.s.o.
  317. * It will replace path with a fixed shm allocated version. Assumes path->s
  318. * was shm allocated.
  319. * @param path path to be fixed. If it starts with '.' or '/' is left alone
  320. * (forced "relative" or "absolute" path). Otherwise the path is considered
  321. * to be relative to the main config file directory
  322. * (e.g. for /etc/ser/ser.cfg => /etc/ser/\<path\>).
  323. * @return 0 on success, -1 on error
  324. */
  325. int fix_shm_pathname(str* path)
  326. {
  327. str new_path;
  328. char* abs_path;
  329. if (path->s && path->len && *path->s != '.' && *path->s != '/') {
  330. abs_path = get_abs_pathname(0, path);
  331. if (abs_path == 0) return -1;
  332. new_path.len = strlen(abs_path);
  333. new_path.s = shm_malloc(new_path.len + 1);
  334. memcpy(new_path.s, abs_path, new_path.len);
  335. new_path.s[new_path.len] = 0;
  336. shm_free(path->s);
  337. *path = new_path;
  338. }
  339. return 0;
  340. }
  341. /**
  342. * @brief Load certificate from file
  343. * @param d domain
  344. * @return 0 if not configured or on success, -1 on error
  345. */
  346. static int load_cert(tls_domain_t* d)
  347. {
  348. int i;
  349. int procs_no;
  350. if (!d->cert_file.s || !d->cert_file.len) {
  351. DBG("%s: No certificate configured\n", tls_domain_str(d));
  352. return 0;
  353. }
  354. if (fix_shm_pathname(&d->cert_file) < 0)
  355. return -1;
  356. procs_no=get_max_procs();
  357. for(i = 0; i < procs_no; i++) {
  358. if (!SSL_CTX_use_certificate_chain_file(d->ctx[i], d->cert_file.s)) {
  359. ERR("%s: Unable to load certificate file '%s'\n",
  360. tls_domain_str(d), d->cert_file.s);
  361. TLS_ERR("load_cert:");
  362. return -1;
  363. }
  364. }
  365. return 0;
  366. }
  367. /**
  368. * @brief Load CA list from file
  369. * @param d domain
  370. * @return 0 if not configured or on success, -1 on error
  371. */
  372. static int load_ca_list(tls_domain_t* d)
  373. {
  374. int i;
  375. int procs_no;
  376. if (!d->ca_file.s || !d->ca_file.len) {
  377. DBG("%s: No CA list configured\n", tls_domain_str(d));
  378. return 0;
  379. }
  380. if (fix_shm_pathname(&d->ca_file) < 0)
  381. return -1;
  382. procs_no=get_max_procs();
  383. for(i = 0; i < procs_no; i++) {
  384. if (SSL_CTX_load_verify_locations(d->ctx[i], d->ca_file.s, 0) != 1) {
  385. ERR("%s: Unable to load CA list '%s'\n", tls_domain_str(d),
  386. d->ca_file.s);
  387. TLS_ERR("load_ca_list:");
  388. return -1;
  389. }
  390. SSL_CTX_set_client_CA_list(d->ctx[i],
  391. SSL_load_client_CA_file(d->ca_file.s));
  392. if (SSL_CTX_get_client_CA_list(d->ctx[i]) == 0) {
  393. ERR("%s: Error while setting client CA list\n", tls_domain_str(d));
  394. TLS_ERR("load_ca_list:");
  395. return -1;
  396. }
  397. }
  398. return 0;
  399. }
  400. /**
  401. * @brief Load CRL from file
  402. * @param d domain
  403. * @return 0 if not configured or on success, -1 on error
  404. */
  405. static int load_crl(tls_domain_t* d)
  406. {
  407. int i;
  408. int procs_no;
  409. X509_STORE* store;
  410. if (!d->crl_file.s) {
  411. DBG("%s: No CRL configured\n", tls_domain_str(d));
  412. return 0;
  413. }
  414. if (fix_shm_pathname(&d->crl_file) < 0)
  415. return -1;
  416. LOG(L_INFO, "%s: Certificate revocation lists will be checked (%.*s)\n",
  417. tls_domain_str(d), d->crl_file.len, d->crl_file.s);
  418. procs_no=get_max_procs();
  419. for(i = 0; i < procs_no; i++) {
  420. if (SSL_CTX_load_verify_locations(d->ctx[i], d->crl_file.s, 0) != 1) {
  421. ERR("%s: Unable to load certificate revocation list '%s'\n",
  422. tls_domain_str(d), d->crl_file.s);
  423. TLS_ERR("load_crl:");
  424. return -1;
  425. }
  426. store = SSL_CTX_get_cert_store(d->ctx[i]);
  427. X509_STORE_set_flags(store,
  428. X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL);
  429. }
  430. return 0;
  431. }
  432. #define C_DEF_NO_KRB5 "DEFAULT:!KRB5"
  433. #define C_DEF_NO_KRB5_LEN (sizeof(C_DEF_NO_KRB5)-1)
  434. #define C_NO_KRB5_SUFFIX ":!KRB5"
  435. #define C_NO_KRB5_SUFFIX_LEN (sizeof(C_NO_KRB5_SUFFIX)-1)
  436. /**
  437. * @brief Configure cipher list
  438. * @param d domain
  439. * @return 0 on success, -1 on error
  440. */
  441. static int set_cipher_list(tls_domain_t* d)
  442. {
  443. int i;
  444. int procs_no;
  445. char* cipher_list;
  446. cipher_list=d->cipher_list.s;
  447. #ifdef TLS_KSSL_WORKARROUND
  448. if (openssl_kssl_malloc_bug) { /* is openssl bug #1467 present ? */
  449. if (d->cipher_list.s==0) {
  450. /* use "DEFAULT:!KRB5" */
  451. cipher_list="DEFAULT:!KRB5";
  452. } else {
  453. /* append ":!KRB5" */
  454. cipher_list=shm_malloc(d->cipher_list.len+C_NO_KRB5_SUFFIX_LEN+1);
  455. if (cipher_list) {
  456. memcpy(cipher_list, d->cipher_list.s, d->cipher_list.len);
  457. memcpy(cipher_list+d->cipher_list.len, C_NO_KRB5_SUFFIX,
  458. C_NO_KRB5_SUFFIX_LEN);
  459. cipher_list[d->cipher_list.len+C_NO_KRB5_SUFFIX_LEN]=0;
  460. shm_free(d->cipher_list.s);
  461. d->cipher_list.s=cipher_list;
  462. d->cipher_list.len+=C_NO_KRB5_SUFFIX_LEN;
  463. }
  464. }
  465. }
  466. #endif /* TLS_KSSL_WORKARROUND */
  467. if (!cipher_list) return 0;
  468. procs_no=get_max_procs();
  469. for(i = 0; i < procs_no; i++) {
  470. if (SSL_CTX_set_cipher_list(d->ctx[i], cipher_list) == 0 ) {
  471. ERR("%s: Failure to set SSL context cipher list \"%s\"\n",
  472. tls_domain_str(d), cipher_list);
  473. return -1;
  474. }
  475. }
  476. return 0;
  477. }
  478. /**
  479. * @brief Enable/disable TLS certificate verification
  480. * @param d domain
  481. * @return 0
  482. */
  483. static int set_verification(tls_domain_t* d)
  484. {
  485. int verify_mode, i;
  486. int procs_no;
  487. if (d->require_cert) {
  488. verify_mode = SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
  489. LOG(L_INFO, "%s: %s MUST present valid certificate\n",
  490. tls_domain_str(d), d->type & TLS_DOMAIN_SRV ? "Client" : "Server");
  491. } else {
  492. if (d->verify_cert) {
  493. verify_mode = SSL_VERIFY_PEER;
  494. if (d->type & TLS_DOMAIN_SRV) {
  495. LOG(L_INFO, "%s: IF client provides certificate then it"
  496. " MUST be valid\n", tls_domain_str(d));
  497. } else {
  498. LOG(L_INFO, "%s: Server MUST present valid certificate\n",
  499. tls_domain_str(d));
  500. }
  501. } else {
  502. verify_mode = SSL_VERIFY_NONE;
  503. if (d->type & TLS_DOMAIN_SRV) {
  504. LOG(L_INFO, "%s: No client certificate required and no checks"
  505. " performed\n", tls_domain_str(d));
  506. } else {
  507. LOG(L_INFO, "%s: Server MAY present invalid certificate\n",
  508. tls_domain_str(d));
  509. }
  510. }
  511. }
  512. procs_no=get_max_procs();
  513. for(i = 0; i < procs_no; i++) {
  514. SSL_CTX_set_verify(d->ctx[i], verify_mode, 0);
  515. SSL_CTX_set_verify_depth(d->ctx[i], d->verify_depth);
  516. }
  517. return 0;
  518. }
  519. /* This callback function is executed when libssl processes the SSL
  520. * handshake and does SSL record layer stuff. It's used to trap
  521. * client-initiated renegotiations.
  522. */
  523. static void sr_ssl_ctx_info_callback(const SSL *ssl, int event, int ret)
  524. {
  525. struct tls_extra_data* data = 0;
  526. int tls_dbg;
  527. if (event & SSL_CB_HANDSHAKE_START) {
  528. tls_dbg = cfg_get(tls, tls_cfg, debug);
  529. LOG(tls_dbg, "SSL handshake started\n");
  530. if(data==0)
  531. data = (struct tls_extra_data*)SSL_get_app_data(ssl);
  532. if(data->flags & F_TLS_CON_HANDSHAKED) {
  533. LOG(tls_dbg, "SSL renegotiation initiated by client\n");
  534. data->flags |= F_TLS_CON_RENEGOTIATION;
  535. }
  536. }
  537. if (event & SSL_CB_HANDSHAKE_DONE) {
  538. tls_dbg = cfg_get(tls, tls_cfg, debug);
  539. if(data==0)
  540. data = (struct tls_extra_data*)SSL_get_app_data(ssl);
  541. LOG(tls_dbg, "SSL handshake done\n");
  542. /* CVE-2009-3555 - disable renegotiation */
  543. if (ssl->s3) {
  544. LOG(tls_dbg, "SSL disable renegotiation\n");
  545. ssl->s3->flags |= SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS;
  546. }
  547. data->flags |= F_TLS_CON_HANDSHAKED;
  548. }
  549. }
  550. /**
  551. * @brief Configure generic SSL parameters
  552. * @param d domain
  553. * @return 0
  554. */
  555. static int set_ssl_options(tls_domain_t* d)
  556. {
  557. int i;
  558. int procs_no;
  559. long options;
  560. #if OPENSSL_VERSION_NUMBER >= 0x00908000L
  561. long ssl_version;
  562. STACK_OF(SSL_COMP)* comp_methods;
  563. #endif
  564. procs_no=get_max_procs();
  565. options=SSL_OP_ALL; /* all the bug workarrounds by default */
  566. #if OPENSSL_VERSION_NUMBER >= 0x00907000L
  567. options|=SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
  568. SSL_OP_CIPHER_SERVER_PREFERENCE;
  569. #if OPENSSL_VERSION_NUMBER >= 0x00908000L
  570. ssl_version=SSLeay();
  571. if ((ssl_version >= 0x0090800L) && (ssl_version < 0x0090803fL)){
  572. /* if 0.9.8 <= openssl version < 0.9.8c and compression support is
  573. * enabled disable SSL_OP_TLS_BLOCK_PADDING_BUG (set by SSL_OP_ALL),
  574. * see openssl #1204 http://rt.openssl.org/Ticket/Display.html?id=1204
  575. */
  576. comp_methods=SSL_COMP_get_compression_methods();
  577. if (comp_methods && (sk_SSL_COMP_num(comp_methods) > 0)){
  578. options &= ~SSL_OP_TLS_BLOCK_PADDING_BUG;
  579. LOG(L_WARN, "tls: set_ssl_options: openssl "
  580. "SSL_OP_TLS_BLOCK_PADDING bug workaround enabled "
  581. "(openssl version %lx)\n", ssl_version);
  582. }else{
  583. LOG(L_INFO, "tls: set_ssl_options: detected openssl version (%lx) "
  584. " has the SSL_OP_TLS_BLOCK_PADDING bug, but compression "
  585. " is disabled so no workaround is needed\n", ssl_version);
  586. }
  587. }
  588. # endif
  589. #endif
  590. for(i = 0; i < procs_no; i++) {
  591. SSL_CTX_set_options(d->ctx[i], options);
  592. if(sr_tls_renegotiation==0)
  593. SSL_CTX_set_info_callback(d->ctx[i], sr_ssl_ctx_info_callback);
  594. }
  595. return 0;
  596. }
  597. /**
  598. * @brief Configure TLS session cache parameters
  599. * @param d domain
  600. * @return 0
  601. */
  602. static int set_session_cache(tls_domain_t* d)
  603. {
  604. int i;
  605. int procs_no;
  606. str tls_session_id;
  607. procs_no=get_max_procs();
  608. tls_session_id=cfg_get(tls, tls_cfg, session_id);
  609. for(i = 0; i < procs_no; i++) {
  610. /* janakj: I am not sure if session cache makes sense in ser, session
  611. * cache is stored in SSL_CTX and we have one SSL_CTX per process,
  612. * thus sessions among processes will not be reused
  613. */
  614. SSL_CTX_set_session_cache_mode(d->ctx[i],
  615. cfg_get(tls, tls_cfg, session_cache) ? SSL_SESS_CACHE_SERVER :
  616. SSL_SESS_CACHE_OFF);
  617. /* not really needed is SSL_SESS_CACHE_OFF */
  618. SSL_CTX_set_session_id_context(d->ctx[i],
  619. (unsigned char*)tls_session_id.s, tls_session_id.len);
  620. }
  621. return 0;
  622. }
  623. /**
  624. * @brief TLS SSL_CTX_set_mode and SSL_CTX_clear_mode wrapper
  625. * @param ctx SSL context
  626. * @param mode SSL_MODE_*
  627. * @param clear if set to !=0 will do a clear, else (==0) a set
  628. * @return 0 (always succeeds)
  629. */
  630. static int tls_ssl_ctx_mode(SSL_CTX* ctx, long mode, void* clear)
  631. {
  632. if (clear)
  633. #if OPENSSL_VERSION_NUMBER >= 0x01000000L || \
  634. defined SSL_CTX_clear_mode
  635. SSL_CTX_clear_mode(ctx, mode);
  636. #else
  637. return -1;
  638. #endif
  639. else
  640. SSL_CTX_set_mode(ctx, mode);
  641. return 0;
  642. }
  643. /**
  644. * @brief TLS set ctx->free_list_max_len
  645. * @param ctx TLS context
  646. * @param val value (<0 ignored)
  647. * @param unused unused
  648. * @return 0 (always succeeds)
  649. */
  650. static int tls_ssl_ctx_set_freelist(SSL_CTX* ctx, long val, void* unused)
  651. {
  652. if (val >= 0)
  653. #if OPENSSL_VERSION_NUMBER >= 0x01000000L
  654. #ifndef OPENSSL_NO_BUF_FREELISTS
  655. ctx->freelist_max_len = val;
  656. #endif
  657. #endif
  658. #if defined (OPENSSL_NO_BUF_FREELISTS) || OPENSSL_VERSION_NUMBER < 0x01000000L
  659. return -1;
  660. #endif
  661. return 0;
  662. }
  663. /**
  664. * @brief TLS SSL_CTX_set_max_send_fragment wrapper
  665. * @param ctx TLS context
  666. * @param val value (<0 ignored). Should be between 512 and 16k
  667. * @param unused unused
  668. * @return 0 on success, < 0 on failure (invalid value)
  669. */
  670. static int tls_ssl_ctx_set_max_send_fragment(SSL_CTX* ctx, long val, void* unused)
  671. {
  672. if (val >= 0)
  673. #if OPENSSL_VERSION_NUMBER >= 0x00909000L
  674. return SSL_CTX_set_max_send_fragment(ctx, val) -1;
  675. #else
  676. return -1;
  677. #endif
  678. return 0;
  679. }
  680. /**
  681. * @brief TLS SSL_CTX_set_read_ahead wrapper
  682. * @param ctx TLS context
  683. * @param val value (<0 ignored, 0 or >0)
  684. * @param unused unused
  685. * @return 0 (always success).
  686. */
  687. static int tls_ssl_ctx_set_read_ahead(SSL_CTX* ctx, long val, void* unused)
  688. {
  689. if (val >= 0)
  690. SSL_CTX_set_read_ahead(ctx, val);
  691. return 0;
  692. }
  693. /**
  694. * @brief Initialize all domain attributes from default domains if necessary
  695. * @param d initialized TLS domain
  696. * @param def default TLS domains
  697. */
  698. static int fix_domain(tls_domain_t* d, tls_domain_t* def)
  699. {
  700. int i;
  701. int procs_no;
  702. if (fill_missing(d, def) < 0) return -1;
  703. procs_no=get_max_procs();
  704. d->ctx = (SSL_CTX**)shm_malloc(sizeof(SSL_CTX*) * procs_no);
  705. if (!d->ctx) {
  706. ERR("%s: Cannot allocate shared memory\n", tls_domain_str(d));
  707. return -1;
  708. }
  709. memset(d->ctx, 0, sizeof(SSL_CTX*) * procs_no);
  710. for(i = 0; i < procs_no; i++) {
  711. d->ctx[i] = SSL_CTX_new((SSL_METHOD*)ssl_methods[d->method - 1]);
  712. if (d->ctx[i] == NULL) {
  713. ERR("%s: Cannot create SSL context\n", tls_domain_str(d));
  714. return -1;
  715. }
  716. }
  717. if (load_cert(d) < 0) return -1;
  718. if (load_ca_list(d) < 0) return -1;
  719. if (load_crl(d) < 0) return -1;
  720. if (set_cipher_list(d) < 0) return -1;
  721. if (set_verification(d) < 0) return -1;
  722. if (set_ssl_options(d) < 0) return -1;
  723. if (set_session_cache(d) < 0) return -1;
  724. return 0;
  725. }
  726. /**
  727. * @brief Password callback, ask for private key password on CLI
  728. * @param buf buffer
  729. * @param size buffer size
  730. * @param rwflag not used
  731. * @param filename filename
  732. * @return length of password on success, 0 on error
  733. */
  734. static int passwd_cb(char *buf, int size, int rwflag, void *filename)
  735. {
  736. #if OPENSSL_VERSION_NUMBER >= 0x00907000L
  737. UI *ui;
  738. const char *prompt;
  739. ui = UI_new();
  740. if (ui == NULL)
  741. goto err;
  742. prompt = UI_construct_prompt(ui, "passphrase", filename);
  743. UI_add_input_string(ui, prompt, 0, buf, 0, size - 1);
  744. UI_process(ui);
  745. UI_free(ui);
  746. return strlen(buf);
  747. err:
  748. ERR("passwd_cb: Error in passwd_cb\n");
  749. if (ui) {
  750. UI_free(ui);
  751. }
  752. return 0;
  753. #else
  754. if (des_read_pw_string(buf, size-1, "Enter Private Key password:", 0)) {
  755. ERR("Error in passwd_cb\n");
  756. return 0;
  757. }
  758. return strlen(buf);
  759. #endif
  760. }
  761. /**
  762. * @brief Load a private key from a file
  763. * @param d TLS domain
  764. * @return 0 on success, -1 on error
  765. */
  766. static int load_private_key(tls_domain_t* d)
  767. {
  768. int idx, ret_pwd, i;
  769. int procs_no;
  770. if (!d->pkey_file.s || !d->pkey_file.len) {
  771. DBG("%s: No private key specified\n", tls_domain_str(d));
  772. return 0;
  773. }
  774. if (fix_shm_pathname(&d->pkey_file) < 0)
  775. return -1;
  776. procs_no=get_max_procs();
  777. for(i = 0; i < procs_no; i++) {
  778. SSL_CTX_set_default_passwd_cb(d->ctx[i], passwd_cb);
  779. SSL_CTX_set_default_passwd_cb_userdata(d->ctx[i], d->pkey_file.s);
  780. for(idx = 0, ret_pwd = 0; idx < 3; idx++) {
  781. ret_pwd = SSL_CTX_use_PrivateKey_file(d->ctx[i], d->pkey_file.s,
  782. SSL_FILETYPE_PEM);
  783. if (ret_pwd) {
  784. break;
  785. } else {
  786. ERR("%s: Unable to load private key '%s'\n",
  787. tls_domain_str(d), d->pkey_file.s);
  788. TLS_ERR("load_private_key:");
  789. continue;
  790. }
  791. }
  792. if (!ret_pwd) {
  793. ERR("%s: Unable to load private key file '%s'\n",
  794. tls_domain_str(d), d->pkey_file.s);
  795. TLS_ERR("load_private_key:");
  796. return -1;
  797. }
  798. if (!SSL_CTX_check_private_key(d->ctx[i])) {
  799. ERR("%s: Key '%s' does not match the public key of the"
  800. " certificate\n", tls_domain_str(d), d->pkey_file.s);
  801. TLS_ERR("load_private_key:");
  802. return -1;
  803. }
  804. }
  805. DBG("%s: Key '%s' successfuly loaded\n",
  806. tls_domain_str(d), d->pkey_file.s);
  807. return 0;
  808. }
  809. /**
  810. * @brief Initialize attributes of all domains from default domains if necessary
  811. *
  812. * Initialize attributes of all domains from default domains if necessary,
  813. * fill in missing parameters.
  814. * @param cfg initialized domain
  815. * @param srv_defaults server defaults
  816. * @param cli_defaults command line interface defaults
  817. * @return 0 on success, -1 on error
  818. */
  819. int tls_fix_domains_cfg(tls_domains_cfg_t* cfg, tls_domain_t* srv_defaults,
  820. tls_domain_t* cli_defaults)
  821. {
  822. tls_domain_t* d;
  823. int ssl_mode_release_buffers;
  824. int ssl_freelist_max_len;
  825. int ssl_max_send_fragment;
  826. int ssl_read_ahead;
  827. if (!cfg->cli_default) {
  828. cfg->cli_default = tls_new_domain(TLS_DOMAIN_DEF | TLS_DOMAIN_CLI,
  829. 0, 0);
  830. }
  831. if (!cfg->srv_default) {
  832. cfg->srv_default = tls_new_domain(TLS_DOMAIN_DEF | TLS_DOMAIN_SRV,
  833. 0, 0);
  834. }
  835. if (fix_domain(cfg->srv_default, srv_defaults) < 0) return -1;
  836. if (fix_domain(cfg->cli_default, cli_defaults) < 0) return -1;
  837. d = cfg->srv_list;
  838. while (d) {
  839. if (fix_domain(d, srv_defaults) < 0) return -1;
  840. d = d->next;
  841. }
  842. d = cfg->cli_list;
  843. while (d) {
  844. if (fix_domain(d, cli_defaults) < 0) return -1;
  845. d = d->next;
  846. }
  847. /* Ask for passwords as the last step */
  848. d = cfg->srv_list;
  849. while(d) {
  850. if (load_private_key(d) < 0) return -1;
  851. d = d->next;
  852. }
  853. d = cfg->cli_list;
  854. while(d) {
  855. if (load_private_key(d) < 0) return -1;
  856. d = d->next;
  857. }
  858. if (load_private_key(cfg->srv_default) < 0) return -1;
  859. if (load_private_key(cfg->cli_default) < 0) return -1;
  860. /* set various global per CTX options
  861. * (done here to show possible missing features messages only once)
  862. */
  863. ssl_mode_release_buffers = cfg_get(tls, tls_cfg, ssl_release_buffers);
  864. ssl_freelist_max_len = cfg_get(tls, tls_cfg, ssl_freelist_max);
  865. ssl_max_send_fragment = cfg_get(tls, tls_cfg, ssl_max_send_fragment);
  866. ssl_read_ahead = cfg_get(tls, tls_cfg, ssl_read_ahead);
  867. #if OPENSSL_VERSION_NUMBER >= 0x01000000L
  868. /* set SSL_MODE_RELEASE_BUFFERS if ssl_mode_release_buffers !=0,
  869. reset if == 0 and ignore if < 0 */
  870. /* only in >= 1.0.0 */
  871. if (ssl_mode_release_buffers >= 0 &&
  872. tls_foreach_CTX_in_cfg(cfg, tls_ssl_ctx_mode, SSL_MODE_RELEASE_BUFFERS,
  873. (void*)(long)(ssl_mode_release_buffers==0))
  874. < 0) {
  875. ERR("invalid ssl_release_buffers value (%d)\n",
  876. ssl_mode_release_buffers);
  877. return -1;
  878. }
  879. #else
  880. if (ssl_mode_release_buffers > 0)
  881. ERR("cannot change openssl mode_release_buffers, the openssl version"
  882. " is too old (need at least 1.0.0)\n");
  883. #endif
  884. /* only in >= 1.0.0 */
  885. #if OPENSSL_VERSION_NUMBER >= 0x01000000L
  886. #ifndef OPENSSL_NO_BUF_FREELISTS
  887. if (tls_foreach_CTX_in_cfg(cfg, tls_ssl_ctx_set_freelist,
  888. ssl_freelist_max_len, 0) < 0) {
  889. ERR("invalid ssl_freelist_max_len value (%d)\n",
  890. ssl_freelist_max_len);
  891. return -1;
  892. }
  893. #endif
  894. #endif
  895. #if defined (OPENSSL_NO_BUF_FREELISTS) || OPENSSL_VERSION_NUMBER < 0x01000000L
  896. if (ssl_freelist_max_len >= 0)
  897. ERR("cannot change openssl freelist_max_len, openssl too old"
  898. "(needed at least 1.0.0) or compiled without freelist support"
  899. " (OPENSSL_NO_BUF_FREELIST)\n");
  900. #endif
  901. #if OPENSSL_VERSION_NUMBER >= 0x00909000L
  902. /* only in >= 0.9.9 */
  903. if (tls_foreach_CTX_in_cfg(cfg, tls_ssl_ctx_set_max_send_fragment,
  904. ssl_max_send_fragment, 0) < 0) {
  905. ERR("invalid ssl_max_send_fragment value (%d)\n",
  906. ssl_max_send_fragment);
  907. return -1;
  908. }
  909. #else
  910. if (ssl_max_send_fragment > 0)
  911. ERR("cannot change openssl max_send_fragment, the openssl version"
  912. " is too old (need at least 0.9.9)\n");
  913. #endif
  914. if (tls_foreach_CTX_in_cfg(cfg, tls_ssl_ctx_set_read_ahead,
  915. ssl_read_ahead, 0) < 0) {
  916. ERR("invalid ssl_read_ahead value (%d)\n", ssl_read_ahead);
  917. return -1;
  918. }
  919. /* set options for SSL_write:
  920. SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER - needed when queueing
  921. clear text for a future write (WANTS_READ). In this case the
  922. buffer address will change for the repeated SSL_write() and
  923. without this option it will trigger the openssl sanity checks.
  924. SSL_MODE_ENABLE_PARTIAL_WRITE - needed to deal with potentially
  925. huge multi-record writes that don't fit in the default buffer
  926. (the default buffer must have space for at least 1 record) */
  927. if (tls_foreach_CTX_in_cfg(cfg, tls_ssl_ctx_mode,
  928. SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
  929. SSL_MODE_ENABLE_PARTIAL_WRITE,
  930. 0) < 0) {
  931. ERR("could not set SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER and"
  932. " SSL_MODE_ENABLE_PARTIAL_WRITE\n");
  933. return -1;
  934. }
  935. return 0;
  936. }
  937. /**
  938. * @brief Create new configuration structure
  939. *
  940. * Create new configuration structure in new allocated shared memory
  941. * @return configuration structure or zero on error
  942. */
  943. tls_domains_cfg_t* tls_new_cfg(void)
  944. {
  945. tls_domains_cfg_t* r;
  946. r = (tls_domains_cfg_t*)shm_malloc(sizeof(tls_domains_cfg_t));
  947. if (!r) {
  948. ERR("No memory left\n");
  949. return 0;
  950. }
  951. memset(r, 0, sizeof(tls_domains_cfg_t));
  952. return r;
  953. }
  954. /**
  955. * @brief Lookup TLS configuration based on type, ip, and port
  956. * @param cfg configuration set
  957. * @param type type of configuration
  958. * @param ip IP for configuration
  959. * @param port port for configuration
  960. * @return found configuration or default, if not found
  961. */
  962. tls_domain_t* tls_lookup_cfg(tls_domains_cfg_t* cfg, int type,
  963. struct ip_addr* ip, unsigned short port)
  964. {
  965. tls_domain_t *p;
  966. if (type & TLS_DOMAIN_DEF) {
  967. if (type & TLS_DOMAIN_SRV) return cfg->srv_default;
  968. else return cfg->cli_default;
  969. } else {
  970. if (type & TLS_DOMAIN_SRV) p = cfg->srv_list;
  971. else p = cfg->cli_list;
  972. }
  973. while (p) {
  974. if ((p->port == port) && ip_addr_cmp(&p->ip, ip))
  975. return p;
  976. p = p->next;
  977. }
  978. /* No matching domain found, return default */
  979. if (type & TLS_DOMAIN_SRV) return cfg->srv_default;
  980. else return cfg->cli_default;
  981. }
  982. /**
  983. * @brief Check whether configuration domain exists
  984. * @param cfg configuration set
  985. * @param d checked domain
  986. * @return 1 if domain exists, 0 if its not exists
  987. */
  988. static int domain_exists(tls_domains_cfg_t* cfg, tls_domain_t* d)
  989. {
  990. tls_domain_t *p;
  991. if (d->type & TLS_DOMAIN_DEF) {
  992. if (d->type & TLS_DOMAIN_SRV) return cfg->srv_default != NULL;
  993. else return cfg->cli_default != NULL;
  994. } else {
  995. if (d->type & TLS_DOMAIN_SRV) p = cfg->srv_list;
  996. else p = cfg->cli_list;
  997. }
  998. while (p) {
  999. if ((p->port == d->port) && ip_addr_cmp(&p->ip, &d->ip))
  1000. return 1;
  1001. p = p->next;
  1002. }
  1003. return 0;
  1004. }
  1005. /**
  1006. * @brief Add a domain to the configuration set
  1007. * @param cfg configuration set
  1008. * @param d TLS domain
  1009. * @return 1 if domain already exists, 0 after addition, -1 on error
  1010. */
  1011. int tls_add_domain(tls_domains_cfg_t* cfg, tls_domain_t* d)
  1012. {
  1013. if (!cfg) {
  1014. ERR("TLS configuration structure missing\n");
  1015. return -1;
  1016. }
  1017. /* Make sure the domain does not exist */
  1018. if (domain_exists(cfg, d)) return 1;
  1019. if (d->type & TLS_DOMAIN_DEF) {
  1020. if (d->type & TLS_DOMAIN_CLI) {
  1021. cfg->cli_default = d;
  1022. } else {
  1023. cfg->srv_default = d;
  1024. }
  1025. } else {
  1026. if (d->type & TLS_DOMAIN_SRV) {
  1027. d->next = cfg->srv_list;
  1028. cfg->srv_list = d;
  1029. } else {
  1030. d->next = cfg->cli_list;
  1031. cfg->cli_list = d;
  1032. }
  1033. }
  1034. return 0;
  1035. }