utils.py 9.8 KB

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