tls_domain.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. #ifndef _TLS_DOMAIN_H
  26. #define _TLS_DOMAIN_H
  27. #include "../../str.h"
  28. #include "../../ip_addr.h"
  29. #include "../../atomic_ops.h"
  30. #include <openssl/ssl.h>
  31. #define TLS_OP_SSLv2_PLUS 0
  32. #define TLS_OP_SSLv3_PLUS (TLS_OP_SSLv2_PLUS | SSL_OP_NO_SSLv2)
  33. #define TLS_OP_TLSv1_PLUS (TLS_OP_SSLv3_PLUS | SSL_OP_NO_SSLv3)
  34. #ifdef SSL_OP_NO_TLSv1
  35. # define TLS_OP_TLSv1_1_PLUS (TLS_OP_TLSv1_PLUS | SSL_OP_NO_TLSv1)
  36. # ifdef SSL_OP_NO_TLSv1_1
  37. # define TLS_OP_TLSv1_2_PLUS (TLS_OP_TLSv1_1_PLUS | SSL_OP_NO_TLSv1_1)
  38. # endif /*SSL_OP_NO_TLSv1_1*/
  39. #endif /*SSL_OP_NO_TLSv1*/
  40. /**
  41. * Available TLS methods
  42. */
  43. enum tls_method {
  44. TLS_METHOD_UNSPEC = 0,
  45. TLS_USE_SSLv23_cli,
  46. TLS_USE_SSLv23_srv,
  47. TLS_USE_SSLv23, /* any SSL/TLS version */
  48. TLS_USE_SSLv2_cli,
  49. TLS_USE_SSLv2_srv,
  50. TLS_USE_SSLv2, /* only SSLv2 (deprecated) */
  51. TLS_USE_SSLv3_cli,
  52. TLS_USE_SSLv3_srv,
  53. TLS_USE_SSLv3, /* only SSLv3 (insecure) */
  54. TLS_USE_TLSv1_cli,
  55. TLS_USE_TLSv1_srv,
  56. TLS_USE_TLSv1, /* only TLSv1.0 */
  57. TLS_USE_TLSv1_1_cli,
  58. TLS_USE_TLSv1_1_srv,
  59. TLS_USE_TLSv1_1, /* only TLSv1.1 */
  60. TLS_USE_TLSv1_2_cli,
  61. TLS_USE_TLSv1_2_srv,
  62. TLS_USE_TLSv1_2, /* only TLSv1.2 */
  63. TLS_USE_TLSvRANGE, /* placeholder - TLSvX ranges must be after it */
  64. TLS_USE_TLSv1_PLUS, /* TLSv1.0 or greater */
  65. TLS_USE_TLSv1_1_PLUS, /* TLSv1.1 or greater */
  66. TLS_USE_TLSv1_2_PLUS, /* TLSv1.1 or greater */
  67. TLS_METHOD_MAX
  68. };
  69. /**
  70. * TLS configuration domain type
  71. */
  72. enum tls_domain_type {
  73. TLS_DOMAIN_DEF = (1 << 0), /**< Default domain */
  74. TLS_DOMAIN_SRV = (1 << 1), /**< Server domain */
  75. TLS_DOMAIN_CLI = (1 << 2) /**< Client domain */
  76. };
  77. /**
  78. * separate configuration per ip:port
  79. */
  80. typedef struct tls_domain {
  81. int type;
  82. struct ip_addr ip;
  83. unsigned short port;
  84. SSL_CTX** ctx;
  85. str cert_file;
  86. str pkey_file;
  87. int verify_cert;
  88. int verify_depth;
  89. str ca_file;
  90. int require_cert;
  91. str cipher_list;
  92. enum tls_method method;
  93. str crl_file;
  94. str server_name;
  95. struct tls_domain* next;
  96. } tls_domain_t;
  97. /**
  98. * TLS configuration structures
  99. */
  100. typedef struct tls_domains_cfg {
  101. tls_domain_t* srv_default; /**< Default server domain */
  102. tls_domain_t* cli_default; /**< Default client domain */
  103. tls_domain_t* srv_list; /**< Server domain list */
  104. tls_domain_t* cli_list; /**< Client domain list */
  105. struct tls_domains_cfg* next; /**< Next element in the garbage list */
  106. atomic_t ref_count; /**< How many connections use this configuration */
  107. } tls_domains_cfg_t;
  108. /**
  109. * @brief Create a new TLS domain structure
  110. *
  111. * Create a new domain structure in new allocated shared memory.
  112. * @param type domain Type
  113. * @param ip domain IP
  114. * @param port domain port
  115. * @return new domain
  116. */
  117. tls_domain_t *tls_new_domain(int type, struct ip_addr *ip,
  118. unsigned short port);
  119. /**
  120. * @brief Free all memory used by TLS configuration domain
  121. * @param d freed domain
  122. */
  123. void tls_free_domain(tls_domain_t* d);
  124. /**
  125. * @brief Generate TLS domain identifier
  126. * @param d printed domain
  127. * @return printed domain, with zero termination
  128. */
  129. char* tls_domain_str(tls_domain_t* d);
  130. /**
  131. * @brief Create new TLS configuration structure
  132. *
  133. * Create new configuration structure in new allocated shared memory.
  134. * @return configuration structure or zero on error
  135. */
  136. tls_domains_cfg_t* tls_new_cfg(void);
  137. /**
  138. * @brief Add a domain to the configuration set
  139. * @param cfg configuration set
  140. * @param d TLS domain
  141. * @return 1 if domain already exists, 0 after addition, -1 on error
  142. */
  143. int tls_add_domain(tls_domains_cfg_t* cfg, tls_domain_t* d);
  144. /**
  145. * @brief Initialize attributes of all domains from default domains if necessary
  146. *
  147. * Initialize attributes of all domains from default domains if necessary,
  148. * fill in missing parameters.
  149. * @param cfg initialized domain
  150. * @param srv_defaults server defaults
  151. * @param cli_defaults command line interface defaults
  152. * @return 0 on success, -1 on error
  153. */
  154. int tls_fix_domains_cfg(tls_domains_cfg_t* cfg, tls_domain_t* srv_defaults,
  155. tls_domain_t* cli_defaults);
  156. /**
  157. * @brief Lookup TLS configuration based on type, ip, and port
  158. * @param cfg configuration set
  159. * @param type type of configuration
  160. * @param ip IP for configuration
  161. * @param port port for configuration
  162. * @param sname server name
  163. * @return found configuration or default, if not found
  164. */
  165. tls_domain_t* tls_lookup_cfg(tls_domains_cfg_t* cfg, int type,
  166. struct ip_addr* ip, unsigned short port, str *sname);
  167. /**
  168. * @brief Free TLS configuration structure
  169. * @param cfg freed configuration
  170. */
  171. void tls_free_cfg(tls_domains_cfg_t* cfg);
  172. /**
  173. * @brief Destroy all TLS configuration data
  174. */
  175. void tls_destroy_cfg(void);
  176. #endif /* _TLS_DOMAIN_H */