utils.py 9.2 KB

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