utils.py 9.3 KB

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