utils.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. "dateutil",
  28. "bcrypt",
  29. "pika",
  30. "ldap",
  31. "ldap3",
  32. "pam")
  33. # IPv4 (host, port) and IPv6 (host, port, flowinfo, scopeid)
  34. ADDRESS_TYPE = Union[Tuple[Union[str, bytes, bytearray], int],
  35. Tuple[str, int, int, int]]
  36. def load_plugin(internal_types: Sequence[str], module_name: str,
  37. class_name: str, base_class: Type[_T_co],
  38. configuration: "config.Configuration") -> _T_co:
  39. type_: Union[str, Callable] = configuration.get(module_name, "type")
  40. if callable(type_):
  41. logger.info("%s type is %r", module_name, type_)
  42. return type_(configuration)
  43. if type_ in internal_types:
  44. module = "radicale.%s.%s" % (module_name, type_)
  45. else:
  46. module = type_
  47. try:
  48. class_ = getattr(import_module(module), class_name)
  49. except Exception as e:
  50. raise RuntimeError("Failed to load %s module %r: %s" %
  51. (module_name, module, e)) from e
  52. logger.info("%s type is %r", module_name, module)
  53. return class_(configuration)
  54. def package_version(name):
  55. return metadata.version(name)
  56. def packages_version():
  57. versions = []
  58. versions.append("python=%s.%s.%s" % (sys.version_info[0], sys.version_info[1], sys.version_info[2]))
  59. for pkg in RADICALE_MODULES:
  60. try:
  61. versions.append("%s=%s" % (pkg, package_version(pkg)))
  62. except Exception:
  63. try:
  64. versions.append("%s=%s" % (pkg, package_version("python-" + pkg)))
  65. except Exception:
  66. versions.append("%s=%s" % (pkg, "n/a"))
  67. return " ".join(versions)
  68. def format_address(address: ADDRESS_TYPE) -> str:
  69. host, port, *_ = address
  70. if not isinstance(host, str):
  71. raise NotImplementedError("Unsupported address format: %r" %
  72. (address,))
  73. if host.find(":") == -1:
  74. return "%s:%d" % (host, port)
  75. else:
  76. return "[%s]:%d" % (host, port)
  77. def ssl_context_options_by_protocol(protocol: str, ssl_context_options):
  78. logger.debug("SSL protocol string: '%s' and current SSL context options: '0x%x'", protocol, ssl_context_options)
  79. # disable any protocol by default
  80. logger.debug("SSL context options, disable ALL by default")
  81. ssl_context_options |= ssl.OP_NO_SSLv2
  82. ssl_context_options |= ssl.OP_NO_SSLv3
  83. ssl_context_options |= ssl.OP_NO_TLSv1
  84. ssl_context_options |= ssl.OP_NO_TLSv1_1
  85. ssl_context_options |= ssl.OP_NO_TLSv1_2
  86. ssl_context_options |= ssl.OP_NO_TLSv1_3
  87. logger.debug("SSL cleared SSL context options: '0x%x'", ssl_context_options)
  88. for entry in protocol.split():
  89. entry = entry.strip('+') # remove trailing '+'
  90. if entry == "ALL":
  91. logger.debug("SSL context options, enable ALL (some maybe not supported by underlying OpenSSL, SSLv2 not enabled at all)")
  92. ssl_context_options &= ~ssl.OP_NO_SSLv3
  93. ssl_context_options &= ~ssl.OP_NO_TLSv1
  94. ssl_context_options &= ~ssl.OP_NO_TLSv1_1
  95. ssl_context_options &= ~ssl.OP_NO_TLSv1_2
  96. ssl_context_options &= ~ssl.OP_NO_TLSv1_3
  97. elif entry == "SSLv2":
  98. logger.warning("SSL context options, ignore SSLv2 (totally insecure)")
  99. elif entry == "SSLv3":
  100. ssl_context_options &= ~ssl.OP_NO_SSLv3
  101. logger.debug("SSL context options, enable SSLv3 (maybe not supported by underlying OpenSSL)")
  102. elif entry == "TLSv1":
  103. ssl_context_options &= ~ssl.OP_NO_TLSv1
  104. logger.debug("SSL context options, enable TLSv1 (maybe not supported by underlying OpenSSL)")
  105. elif entry == "TLSv1.1":
  106. logger.debug("SSL context options, enable TLSv1.1 (maybe not supported by underlying OpenSSL)")
  107. ssl_context_options &= ~ssl.OP_NO_TLSv1_1
  108. elif entry == "TLSv1.2":
  109. logger.debug("SSL context options, enable TLSv1.2")
  110. ssl_context_options &= ~ssl.OP_NO_TLSv1_2
  111. elif entry == "TLSv1.3":
  112. logger.debug("SSL context options, enable TLSv1.3")
  113. ssl_context_options &= ~ssl.OP_NO_TLSv1_3
  114. elif entry == "-ALL":
  115. logger.debug("SSL context options, disable ALL")
  116. ssl_context_options |= ssl.OP_NO_SSLv2
  117. ssl_context_options |= ssl.OP_NO_SSLv3
  118. ssl_context_options |= ssl.OP_NO_TLSv1
  119. ssl_context_options |= ssl.OP_NO_TLSv1_1
  120. ssl_context_options |= ssl.OP_NO_TLSv1_2
  121. ssl_context_options |= ssl.OP_NO_TLSv1_3
  122. elif entry == "-SSLv2":
  123. ssl_context_options |= ssl.OP_NO_SSLv2
  124. logger.debug("SSL context options, disable SSLv2")
  125. elif entry == "-SSLv3":
  126. ssl_context_options |= ssl.OP_NO_SSLv3
  127. logger.debug("SSL context options, disable SSLv3")
  128. elif entry == "-TLSv1":
  129. logger.debug("SSL context options, disable TLSv1")
  130. ssl_context_options |= ssl.OP_NO_TLSv1
  131. elif entry == "-TLSv1.1":
  132. logger.debug("SSL context options, disable TLSv1.1")
  133. ssl_context_options |= ssl.OP_NO_TLSv1_1
  134. elif entry == "-TLSv1.2":
  135. logger.debug("SSL context options, disable TLSv1.2")
  136. ssl_context_options |= ssl.OP_NO_TLSv1_2
  137. elif entry == "-TLSv1.3":
  138. logger.debug("SSL context options, disable TLSv1.3")
  139. ssl_context_options |= ssl.OP_NO_TLSv1_3
  140. else:
  141. raise RuntimeError("SSL protocol config contains unsupported entry '%s'" % (entry))
  142. logger.debug("SSL resulting context options: '0x%x'", ssl_context_options)
  143. return ssl_context_options
  144. def ssl_context_minimum_version_by_options(ssl_context_options):
  145. logger.debug("SSL calculate minimum version by context options: '0x%x'", ssl_context_options)
  146. ssl_context_minimum_version = ssl.TLSVersion.SSLv3 # default
  147. if ((ssl_context_options & ssl.OP_NO_SSLv3) and (ssl_context_minimum_version == ssl.TLSVersion.SSLv3)):
  148. ssl_context_minimum_version = ssl.TLSVersion.TLSv1
  149. if ((ssl_context_options & ssl.OP_NO_TLSv1) and (ssl_context_minimum_version == ssl.TLSVersion.TLSv1)):
  150. ssl_context_minimum_version = ssl.TLSVersion.TLSv1_1
  151. if ((ssl_context_options & ssl.OP_NO_TLSv1_1) and (ssl_context_minimum_version == ssl.TLSVersion.TLSv1_1)):
  152. ssl_context_minimum_version = ssl.TLSVersion.TLSv1_2
  153. if ((ssl_context_options & ssl.OP_NO_TLSv1_2) and (ssl_context_minimum_version == ssl.TLSVersion.TLSv1_2)):
  154. ssl_context_minimum_version = ssl.TLSVersion.TLSv1_3
  155. if ((ssl_context_options & ssl.OP_NO_TLSv1_3) and (ssl_context_minimum_version == ssl.TLSVersion.TLSv1_3)):
  156. ssl_context_minimum_version = 0 # all disabled
  157. logger.debug("SSL context options: '0x%x' results in minimum version: %s", ssl_context_options, ssl_context_minimum_version)
  158. return ssl_context_minimum_version
  159. def ssl_context_maximum_version_by_options(ssl_context_options):
  160. logger.debug("SSL calculate maximum version by context options: '0x%x'", ssl_context_options)
  161. ssl_context_maximum_version = ssl.TLSVersion.TLSv1_3 # default
  162. if ((ssl_context_options & ssl.OP_NO_TLSv1_3) and (ssl_context_maximum_version == ssl.TLSVersion.TLSv1_3)):
  163. ssl_context_maximum_version = ssl.TLSVersion.TLSv1_2
  164. if ((ssl_context_options & ssl.OP_NO_TLSv1_2) and (ssl_context_maximum_version == ssl.TLSVersion.TLSv1_2)):
  165. ssl_context_maximum_version = ssl.TLSVersion.TLSv1_1
  166. if ((ssl_context_options & ssl.OP_NO_TLSv1_1) and (ssl_context_maximum_version == ssl.TLSVersion.TLSv1_1)):
  167. ssl_context_maximum_version = ssl.TLSVersion.TLSv1
  168. if ((ssl_context_options & ssl.OP_NO_TLSv1) and (ssl_context_maximum_version == ssl.TLSVersion.TLSv1)):
  169. ssl_context_maximum_version = ssl.TLSVersion.SSLv3
  170. if ((ssl_context_options & ssl.OP_NO_SSLv3) and (ssl_context_maximum_version == ssl.TLSVersion.SSLv3)):
  171. ssl_context_maximum_version = 0
  172. logger.debug("SSL context options: '0x%x' results in maximum version: %s", ssl_context_options, ssl_context_maximum_version)
  173. return ssl_context_maximum_version
  174. def ssl_get_protocols(context):
  175. protocols = []
  176. if not (context.options & ssl.OP_NO_SSLv3):
  177. if (context.minimum_version < ssl.TLSVersion.TLSv1):
  178. protocols.append("SSLv3")
  179. if not (context.options & ssl.OP_NO_TLSv1):
  180. if (context.minimum_version < ssl.TLSVersion.TLSv1_1) and (context.maximum_version >= ssl.TLSVersion.TLSv1):
  181. protocols.append("TLSv1")
  182. if not (context.options & ssl.OP_NO_TLSv1_1):
  183. if (context.minimum_version < ssl.TLSVersion.TLSv1_2) and (context.maximum_version >= ssl.TLSVersion.TLSv1_1):
  184. protocols.append("TLSv1.1")
  185. if not (context.options & ssl.OP_NO_TLSv1_2):
  186. if (context.minimum_version <= ssl.TLSVersion.TLSv1_2) and (context.maximum_version >= ssl.TLSVersion.TLSv1_2):
  187. protocols.append("TLSv1.2")
  188. if not (context.options & ssl.OP_NO_TLSv1_3):
  189. if (context.minimum_version <= ssl.TLSVersion.TLSv1_3) and (context.maximum_version >= ssl.TLSVersion.TLSv1_3):
  190. protocols.append("TLSv1.3")
  191. return protocols