utils.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. # This file is part of Radicale - CalDAV and CardDAV server
  2. # Copyright © 2014 Jean-Marc Martins
  3. # Copyright © 2012-2017 Guillaume Ayoub
  4. # Copyright © 2017-2018 Unrud <unrud@outlook.com>
  5. # Copyright © 2024-2025 Peter Bieringer <pb@bieringer.de>
  6. #
  7. # This library is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This library is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  19. import ssl
  20. import sys
  21. from importlib import import_module, metadata
  22. from typing import Callable, Sequence, Tuple, Type, TypeVar, Union
  23. from radicale import config
  24. from radicale.log import logger
  25. _T_co = TypeVar("_T_co", covariant=True)
  26. RADICALE_MODULES: Sequence[str] = ("radicale", "vobject", "passlib", "defusedxml",
  27. "bcrypt",
  28. "pika",
  29. "ldap",
  30. "ldap3",
  31. "pam")
  32. # IPv4 (host, port) and IPv6 (host, port, flowinfo, scopeid)
  33. ADDRESS_TYPE = Union[Tuple[Union[str, bytes, bytearray], int],
  34. Tuple[str, int, int, int]]
  35. def load_plugin(internal_types: Sequence[str], module_name: str,
  36. class_name: str, base_class: Type[_T_co],
  37. configuration: "config.Configuration") -> _T_co:
  38. type_: Union[str, Callable] = configuration.get(module_name, "type")
  39. if callable(type_):
  40. logger.info("%s type is %r", module_name, type_)
  41. return type_(configuration)
  42. if type_ in internal_types:
  43. module = "radicale.%s.%s" % (module_name, type_)
  44. else:
  45. module = type_
  46. try:
  47. class_ = getattr(import_module(module), class_name)
  48. except Exception as e:
  49. raise RuntimeError("Failed to load %s module %r: %s" %
  50. (module_name, module, e)) from e
  51. logger.info("%s type is %r", module_name, module)
  52. return class_(configuration)
  53. def package_version(name):
  54. return metadata.version(name)
  55. def packages_version():
  56. versions = []
  57. versions.append("python=%s.%s.%s" % (sys.version_info[0], sys.version_info[1], sys.version_info[2]))
  58. for pkg in RADICALE_MODULES:
  59. try:
  60. versions.append("%s=%s" % (pkg, package_version(pkg)))
  61. except Exception:
  62. try:
  63. versions.append("%s=%s" % (pkg, package_version("python-" + pkg)))
  64. except Exception:
  65. versions.append("%s=%s" % (pkg, "n/a"))
  66. return " ".join(versions)
  67. def format_address(address: ADDRESS_TYPE) -> str:
  68. host, port, *_ = address
  69. if not isinstance(host, str):
  70. raise NotImplementedError("Unsupported address format: %r" %
  71. (address,))
  72. if host.find(":") == -1:
  73. return "%s:%d" % (host, port)
  74. else:
  75. return "[%s]:%d" % (host, port)
  76. def ssl_context_options_by_protocol(protocol: str, ssl_context_options):
  77. logger.debug("SSL protocol string: '%s' and current SSL context options: '0x%x'", protocol, ssl_context_options)
  78. # disable any protocol by default
  79. logger.debug("SSL context options, disable ALL by default")
  80. ssl_context_options |= ssl.OP_NO_SSLv2
  81. ssl_context_options |= ssl.OP_NO_SSLv3
  82. ssl_context_options |= ssl.OP_NO_TLSv1
  83. ssl_context_options |= ssl.OP_NO_TLSv1_1
  84. ssl_context_options |= ssl.OP_NO_TLSv1_2
  85. ssl_context_options |= ssl.OP_NO_TLSv1_3
  86. logger.debug("SSL cleared SSL context options: '0x%x'", ssl_context_options)
  87. for entry in protocol.split():
  88. entry = entry.strip('+') # remove trailing '+'
  89. if entry == "ALL":
  90. logger.debug("SSL context options, enable ALL (some maybe not supported by underlying OpenSSL, SSLv2 not enabled at all)")
  91. ssl_context_options &= ~ssl.OP_NO_SSLv3
  92. ssl_context_options &= ~ssl.OP_NO_TLSv1
  93. ssl_context_options &= ~ssl.OP_NO_TLSv1_1
  94. ssl_context_options &= ~ssl.OP_NO_TLSv1_2
  95. ssl_context_options &= ~ssl.OP_NO_TLSv1_3
  96. elif entry == "SSLv2":
  97. logger.warning("SSL context options, ignore SSLv2 (totally insecure)")
  98. elif entry == "SSLv3":
  99. ssl_context_options &= ~ssl.OP_NO_SSLv3
  100. logger.debug("SSL context options, enable SSLv3 (maybe not supported by underlying OpenSSL)")
  101. elif entry == "TLSv1":
  102. ssl_context_options &= ~ssl.OP_NO_TLSv1
  103. logger.debug("SSL context options, enable TLSv1 (maybe not supported by underlying OpenSSL)")
  104. elif entry == "TLSv1.1":
  105. logger.debug("SSL context options, enable TLSv1.1 (maybe not supported by underlying OpenSSL)")
  106. ssl_context_options &= ~ssl.OP_NO_TLSv1_1
  107. elif entry == "TLSv1.2":
  108. logger.debug("SSL context options, enable TLSv1.2")
  109. ssl_context_options &= ~ssl.OP_NO_TLSv1_2
  110. elif entry == "TLSv1.3":
  111. logger.debug("SSL context options, enable TLSv1.3")
  112. ssl_context_options &= ~ssl.OP_NO_TLSv1_3
  113. elif entry == "-ALL":
  114. logger.debug("SSL context options, disable ALL")
  115. ssl_context_options |= ssl.OP_NO_SSLv2
  116. ssl_context_options |= ssl.OP_NO_SSLv3
  117. ssl_context_options |= ssl.OP_NO_TLSv1
  118. ssl_context_options |= ssl.OP_NO_TLSv1_1
  119. ssl_context_options |= ssl.OP_NO_TLSv1_2
  120. ssl_context_options |= ssl.OP_NO_TLSv1_3
  121. elif entry == "-SSLv2":
  122. ssl_context_options |= ssl.OP_NO_SSLv2
  123. logger.debug("SSL context options, disable SSLv2")
  124. elif entry == "-SSLv3":
  125. ssl_context_options |= ssl.OP_NO_SSLv3
  126. logger.debug("SSL context options, disable SSLv3")
  127. elif entry == "-TLSv1":
  128. logger.debug("SSL context options, disable TLSv1")
  129. ssl_context_options |= ssl.OP_NO_TLSv1
  130. elif entry == "-TLSv1.1":
  131. logger.debug("SSL context options, disable TLSv1.1")
  132. ssl_context_options |= ssl.OP_NO_TLSv1_1
  133. elif entry == "-TLSv1.2":
  134. logger.debug("SSL context options, disable TLSv1.2")
  135. ssl_context_options |= ssl.OP_NO_TLSv1_2
  136. elif entry == "-TLSv1.3":
  137. logger.debug("SSL context options, disable TLSv1.3")
  138. ssl_context_options |= ssl.OP_NO_TLSv1_3
  139. else:
  140. raise RuntimeError("SSL protocol config contains unsupported entry '%s'" % (entry))
  141. logger.debug("SSL resulting context options: '0x%x'", ssl_context_options)
  142. return ssl_context_options
  143. def ssl_context_minimum_version_by_options(ssl_context_options):
  144. logger.debug("SSL calculate minimum version by context options: '0x%x'", ssl_context_options)
  145. ssl_context_minimum_version = ssl.TLSVersion.SSLv3 # default
  146. if ((ssl_context_options & ssl.OP_NO_SSLv3) and (ssl_context_minimum_version == ssl.TLSVersion.SSLv3)):
  147. ssl_context_minimum_version = ssl.TLSVersion.TLSv1
  148. if ((ssl_context_options & ssl.OP_NO_TLSv1) and (ssl_context_minimum_version == ssl.TLSVersion.TLSv1)):
  149. ssl_context_minimum_version = ssl.TLSVersion.TLSv1_1
  150. if ((ssl_context_options & ssl.OP_NO_TLSv1_1) and (ssl_context_minimum_version == ssl.TLSVersion.TLSv1_1)):
  151. ssl_context_minimum_version = ssl.TLSVersion.TLSv1_2
  152. if ((ssl_context_options & ssl.OP_NO_TLSv1_2) and (ssl_context_minimum_version == ssl.TLSVersion.TLSv1_2)):
  153. ssl_context_minimum_version = ssl.TLSVersion.TLSv1_3
  154. if ((ssl_context_options & ssl.OP_NO_TLSv1_3) and (ssl_context_minimum_version == ssl.TLSVersion.TLSv1_3)):
  155. ssl_context_minimum_version = 0 # all disabled
  156. logger.debug("SSL context options: '0x%x' results in minimum version: %s", ssl_context_options, ssl_context_minimum_version)
  157. return ssl_context_minimum_version
  158. def ssl_context_maximum_version_by_options(ssl_context_options):
  159. logger.debug("SSL calculate maximum version by context options: '0x%x'", ssl_context_options)
  160. ssl_context_maximum_version = ssl.TLSVersion.TLSv1_3 # default
  161. if ((ssl_context_options & ssl.OP_NO_TLSv1_3) and (ssl_context_maximum_version == ssl.TLSVersion.TLSv1_3)):
  162. ssl_context_maximum_version = ssl.TLSVersion.TLSv1_2
  163. if ((ssl_context_options & ssl.OP_NO_TLSv1_2) and (ssl_context_maximum_version == ssl.TLSVersion.TLSv1_2)):
  164. ssl_context_maximum_version = ssl.TLSVersion.TLSv1_1
  165. if ((ssl_context_options & ssl.OP_NO_TLSv1_1) and (ssl_context_maximum_version == ssl.TLSVersion.TLSv1_1)):
  166. ssl_context_maximum_version = ssl.TLSVersion.TLSv1
  167. if ((ssl_context_options & ssl.OP_NO_TLSv1) and (ssl_context_maximum_version == ssl.TLSVersion.TLSv1)):
  168. ssl_context_maximum_version = ssl.TLSVersion.SSLv3
  169. if ((ssl_context_options & ssl.OP_NO_SSLv3) and (ssl_context_maximum_version == ssl.TLSVersion.SSLv3)):
  170. ssl_context_maximum_version = 0
  171. logger.debug("SSL context options: '0x%x' results in maximum version: %s", ssl_context_options, ssl_context_maximum_version)
  172. return ssl_context_maximum_version
  173. def ssl_get_protocols(context):
  174. protocols = []
  175. if not (context.options & ssl.OP_NO_SSLv3):
  176. if (context.minimum_version < ssl.TLSVersion.TLSv1):
  177. protocols.append("SSLv3")
  178. if not (context.options & ssl.OP_NO_TLSv1):
  179. if (context.minimum_version < ssl.TLSVersion.TLSv1_1) and (context.maximum_version >= ssl.TLSVersion.TLSv1):
  180. protocols.append("TLSv1")
  181. if not (context.options & ssl.OP_NO_TLSv1_1):
  182. if (context.minimum_version < ssl.TLSVersion.TLSv1_2) and (context.maximum_version >= ssl.TLSVersion.TLSv1_1):
  183. protocols.append("TLSv1.1")
  184. if not (context.options & ssl.OP_NO_TLSv1_2):
  185. if (context.minimum_version <= ssl.TLSVersion.TLSv1_2) and (context.maximum_version >= ssl.TLSVersion.TLSv1_2):
  186. protocols.append("TLSv1.2")
  187. if not (context.options & ssl.OP_NO_TLSv1_3):
  188. if (context.minimum_version <= ssl.TLSVersion.TLSv1_3) and (context.maximum_version >= ssl.TLSVersion.TLSv1_3):
  189. protocols.append("TLSv1.3")
  190. return protocols