tls_config.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * $Id$
  3. *
  4. * TLS module - Configuration file parser
  5. *
  6. * Copyright (C) 2001-2003 FhG FOKUS
  7. * Copyright (C) 2004,2005 Free Software Foundation, Inc.
  8. * Copyright (C) 2005,2006 iptelorg GmbH
  9. *
  10. * This file is part of SIP-router, a free SIP server.
  11. *
  12. * SIP-router is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version
  16. *
  17. * SIP-router is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. *
  26. */
  27. /*!
  28. * \file
  29. * \brief SIP-router TLS support :: Configuration file parser
  30. * \ingroup tls
  31. * Module: \ref tls
  32. */
  33. #include "tls_config.h"
  34. #include "tls_domain.h"
  35. #include "tls_mod.h"
  36. #include "tls_util.h"
  37. #include "../../cfg_parser.h"
  38. #include "../../resolve.h"
  39. #include "../../mem/mem.h"
  40. #include "../../dprint.h"
  41. #include "../../trim.h"
  42. #include "../../ut.h"
  43. #include "../../cfg/cfg.h"
  44. static tls_domains_cfg_t* cfg = NULL;
  45. static tls_domain_t* domain = NULL;
  46. #ifdef USE_IPV6
  47. static int parse_ipv6(struct ip_addr* ip, cfg_token_t* token,
  48. cfg_parser_t* st)
  49. {
  50. int ret;
  51. cfg_token_t t;
  52. struct ip_addr* ipv6;
  53. str ip6_str;
  54. while(1) {
  55. ret = cfg_get_token(&t, st, 0);
  56. if (ret != 0) goto err;
  57. if (t.type == ']') break;
  58. if (t.type != CFG_TOKEN_ALPHA && t.type != ':') goto err;
  59. }
  60. ip6_str.s = t.val.s;
  61. ip6_str.len = (int)(long)(t.val.s - ip6_str.s);
  62. ipv6 = str2ip6(&ip6_str);
  63. if (ipv6 == 0) goto err;
  64. *ip = *ipv6;
  65. return 0;
  66. err:
  67. ERR("%s:%d:%d: Invalid IPv6 address\n",
  68. st->file, token->start.line, token->start.col);
  69. return -1;
  70. }
  71. #endif /* USE_IPV6 */
  72. static int parse_ipv4(struct ip_addr* ip, cfg_token_t* token,
  73. cfg_parser_t* st)
  74. {
  75. int ret, i;
  76. cfg_token_t t;
  77. unsigned int v;
  78. ip->af = AF_INET;
  79. ip->len = 4;
  80. if (str2int(&token->val, &v) < 0) goto err;
  81. if (v < 0 || v > 255) goto err;
  82. ip->u.addr[0] = v;
  83. for(i = 1; i < 4; i++) {
  84. ret = cfg_get_token(&t, st, 0);
  85. if (ret < 0) return -1;
  86. if (ret > 0 || t.type != '.') goto err;
  87. ret = cfg_get_token(&t, st, 0);
  88. if (ret < 0) return -1;
  89. if (ret > 0 || t.type != CFG_TOKEN_ALPHA) goto err;
  90. if (str2int(&t.val, &v) < 0) goto err;
  91. if (v < 0 || v > 255) goto err;
  92. ip->u.addr[i] = v;
  93. }
  94. return 0;
  95. err:
  96. ERR("%s:%d:%d: Invalid IPv4 address\n",
  97. st->file, token->start.line, token->start.col);
  98. return -1;
  99. }
  100. static cfg_option_t methods[] = {
  101. {"SSLv2", .val = TLS_USE_SSLv2},
  102. {"SSLv3", .val = TLS_USE_SSLv3},
  103. {"SSLv23", .val = TLS_USE_SSLv23},
  104. {"TLSv1", .val = TLS_USE_TLSv1},
  105. {0}
  106. };
  107. static cfg_option_t domain_types[] = {
  108. {"server", .val = TLS_DOMAIN_SRV},
  109. {"srv", .val = TLS_DOMAIN_SRV},
  110. {"s", .val = TLS_DOMAIN_SRV},
  111. {"client", .val = TLS_DOMAIN_CLI},
  112. {"cli", .val = TLS_DOMAIN_CLI},
  113. {"c", .val = TLS_DOMAIN_CLI},
  114. {0}
  115. };
  116. static cfg_option_t token_default[] = {
  117. {"default"},
  118. {"def"},
  119. {"*"},
  120. {0}
  121. };
  122. static cfg_option_t options[] = {
  123. {"method", .param = methods, .f = cfg_parse_enum_opt},
  124. {"tls_method", .param = methods, .f = cfg_parse_enum_opt},
  125. {"verify_certificate", .f = cfg_parse_bool_opt},
  126. {"verify_cert", .f = cfg_parse_bool_opt},
  127. {"verify_depth", .f = cfg_parse_int_opt},
  128. {"require_certificate", .f = cfg_parse_bool_opt},
  129. {"require_cert", .f = cfg_parse_bool_opt},
  130. {"private_key", .f = cfg_parse_str_opt, .flags = CFG_STR_SHMMEM},
  131. {"pkey_file", .f = cfg_parse_str_opt, .flags = CFG_STR_SHMMEM},
  132. {"calist_file", .f = cfg_parse_str_opt, .flags = CFG_STR_SHMMEM},
  133. {"certificate", .f = cfg_parse_str_opt, .flags = CFG_STR_SHMMEM},
  134. {"cert_file", .f = cfg_parse_str_opt, .flags = CFG_STR_SHMMEM},
  135. {"cipher_list", .f = cfg_parse_str_opt, .flags = CFG_STR_SHMMEM},
  136. {"ca_list", .f = cfg_parse_str_opt, .flags = CFG_STR_SHMMEM},
  137. {"crl", .f = cfg_parse_str_opt, .flags = CFG_STR_SHMMEM},
  138. {0}
  139. };
  140. static void update_opt_variables(void)
  141. {
  142. int i;
  143. for(i = 0; methods[i].name; i++) {
  144. methods[i].param = &domain->method;
  145. }
  146. options[2].param = &domain->verify_cert;
  147. options[3].param = &domain->verify_cert;
  148. options[4].param = &domain->verify_depth;
  149. options[5].param = &domain->require_cert;
  150. options[6].param = &domain->require_cert;
  151. options[7].param = &domain->pkey_file;
  152. options[8].param = &domain->pkey_file;
  153. options[9].param = &domain->ca_file;
  154. options[10].param = &domain->cert_file;
  155. options[11].param = &domain->cert_file;
  156. options[12].param = &domain->cipher_list;
  157. options[13].param = &domain->ca_file;
  158. options[14].param = &domain->crl_file;
  159. }
  160. static int parse_hostport(int* type, struct ip_addr* ip, unsigned int* port,
  161. cfg_token_t* token, cfg_parser_t* st)
  162. {
  163. int ret;
  164. cfg_token_t t;
  165. cfg_option_t* opt;
  166. ret = cfg_get_token(&t, st, 0);
  167. if (ret < 0) return -1;
  168. if (ret > 0) {
  169. ERR("%s:%d:%d: Missing IP address\n", st->file,
  170. token->start.line, token->start.col);
  171. return -1;
  172. }
  173. if (t.type == '[') {
  174. #ifdef USE_IPV6
  175. if (parse_ipv6(ip, &t, st) < 0) return -1;
  176. #else
  177. ERR("%s:%d:%d: IPv6 address not supported (compiled without IPv6"
  178. " support)\n",
  179. st->file, t.start.line, t.start.col);
  180. return -1;
  181. #endif /* USE_IPV6 */
  182. } else if (t.type == CFG_TOKEN_ALPHA) {
  183. opt = cfg_lookup_token(token_default, &t.val);
  184. if (opt) {
  185. *type = TLS_DOMAIN_DEF;
  186. /* Default domain */
  187. return 0;
  188. } else {
  189. if (parse_ipv4(ip, &t, st) < 0) return -1;
  190. }
  191. } else {
  192. ERR("%s:%d:%d: Syntax error, IP address expected\n",
  193. st->file, t.start.line, t.start.col);
  194. return -1;
  195. }
  196. *type = 0;
  197. /* Parse port */
  198. ret = cfg_get_token(&t, st, 0);
  199. if (ret < 0) return -1;
  200. if (ret > 0) {
  201. ERR("%s:%d:%d: Syntax error, ':' expected\n", st->file, st->line,
  202. st->col);
  203. return -1;
  204. }
  205. if (t.type != ':') {
  206. ERR("%s:%d:%d: Syntax error, ':' expected\n",
  207. st->file, t.start.line, t.start.col);
  208. return -1;
  209. }
  210. ret = cfg_get_token(&t, st, 0);
  211. if (ret < 0) return -1;
  212. if (ret > 0) {
  213. ERR("%s:%d:%d: Premature end of file, port number missing\n",
  214. st->file, t.start.line, t.start.col);
  215. return -1;
  216. }
  217. if (t.type != CFG_TOKEN_ALPHA || (str2int(&t.val, port) < 0)) {
  218. ERR("%s:%d:%d: Invalid port number '%.*s'\n",
  219. st->file, t.start.line, t.start.col, STR_FMT(&t.val));
  220. return -1;
  221. }
  222. return 0;
  223. }
  224. static int parse_domain(void* param, cfg_parser_t* st, unsigned int flags)
  225. {
  226. cfg_token_t t;
  227. int ret;
  228. cfg_option_t* opt;
  229. int type;
  230. struct ip_addr ip;
  231. unsigned int port;
  232. memset(&ip, 0, sizeof(struct ip_addr));
  233. ret = cfg_get_token(&t, st, 0);
  234. if (ret < 0) return -1;
  235. if (ret > 0) {
  236. ERR("%s:%d:%d: TLS domain type missing\n",
  237. st->file, st->line, st->col);
  238. return -1;
  239. }
  240. if (t.type != CFG_TOKEN_ALPHA ||
  241. ((opt = cfg_lookup_token(domain_types, &t.val)) == NULL)) {
  242. ERR("%s:%d:%d: Invalid TLS domain type %d:'%.*s'\n",
  243. st->file, t.start.line, t.start.col, t.type, STR_FMT(&t.val));
  244. return -1;
  245. }
  246. ret = cfg_get_token(&t, st, 0);
  247. if (ret < 0) return -1;
  248. if (ret > 0) {
  249. ERR("%s:%d:%d: TLS domain IP address missing\n",
  250. st->file, st->line, st->col);
  251. return -1;
  252. }
  253. if (t.type != ':') {
  254. ERR("%s:%d:%d: Syntax error, ':' expected\n",
  255. st->file, t.start.line, t.start.col);
  256. return -1;
  257. }
  258. port = 0;
  259. if (parse_hostport(&type, &ip, &port, &t, st) < 0) return -1;
  260. ret = cfg_get_token(&t, st, 0);
  261. if (ret < 0) return -1;
  262. if (ret > 0) {
  263. ERR("%s:%d:%d: Closing ']' missing\n",
  264. st->file, st->line, st->col);
  265. return -1;
  266. }
  267. if (t.type != ']') {
  268. ERR("%s:%d:%d: Syntax error, ']' expected\n",
  269. st->file, t.start.line, t.start.col);
  270. return -1;
  271. }
  272. if (cfg_eat_eol(st, flags)) return -1;
  273. if ((domain = tls_new_domain(opt->val | type, &ip, port)) == NULL) {
  274. ERR("%s:%d: Cannot create TLS domain structure\n", st->file, st->line);
  275. return -1;
  276. }
  277. ret = tls_add_domain(cfg, domain);
  278. if (ret < 0) {
  279. ERR("%s:%d: Error while creating TLS domain structure\n", st->file,
  280. st->line);
  281. tls_free_domain(domain);
  282. return -1;
  283. } else if (ret == 1) {
  284. ERR("%s:%d: Duplicate TLS domain (appears earlier in the config file)\n",
  285. st->file, st->line);
  286. tls_free_domain(domain);
  287. return -1;
  288. }
  289. update_opt_variables();
  290. cfg_set_options(st, options);
  291. return 0;
  292. }
  293. /*
  294. * Create configuration structures from configuration file
  295. */
  296. tls_domains_cfg_t* tls_load_config(str* filename)
  297. {
  298. cfg_parser_t* parser;
  299. str empty;
  300. parser = NULL;
  301. if ((cfg = tls_new_cfg()) == NULL) goto error;
  302. empty.s = 0;
  303. empty.len = 0;
  304. if ((parser = cfg_parser_init(&empty, filename)) == NULL) {
  305. ERR("tls: Error while initializing configuration file parser.\n");
  306. goto error;
  307. }
  308. cfg_section_parser(parser, parse_domain, NULL);
  309. if (sr_cfg_parse(parser)) goto error;
  310. cfg_parser_close(parser);
  311. return cfg;
  312. error:
  313. if (parser) cfg_parser_close(parser);
  314. if (cfg) tls_free_cfg(cfg);
  315. return 0;
  316. }
  317. /*
  318. * Convert TLS method string to integer
  319. */
  320. int tls_parse_method(str* method)
  321. {
  322. cfg_option_t* opt;
  323. if (!method) {
  324. BUG("Invalid parameter value\n");
  325. return -1;
  326. }
  327. opt = cfg_lookup_token(methods, method);
  328. if (!opt) return -1;
  329. return opt->val;
  330. }